u-boot 2016.05 添加u-boot cmd

记录一下如何在u-boot 添加一个自己想要的命令。

    首先来看一下宏,include/command.h
    218 #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help)      \           
    219     U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)       
    U_BOOT_CMD 宏第一个参数是你的命令的名字。
                 第二个参数你最大参数个数
                 第三个参数是是否能复用。
                 第四个参数是实现的函数名
                 第五个简单说明。
                后面是help
  • 举例:
    nandecc.c
                                                                                   
    #include <common.h>                                                             
    #include <linux/mtd/mtd.h>                                                      
    #include <command.h>                                                            
    #include <console.h>                                                            
    #include <watchdog.h>                                                           
    #include <malloc.h>                                                             
    #include <asm/byteorder.h>                                                      
    #include <jffs2/jffs2.h>                                                        
    #include <nand.h>                                                               
    #include <asm/io.h>                                                             
    #include <asm/errno.h>                                                          
    #if defined(CONFIG_SOC_KEYSTONE)                                                
    #include <asm/ti-common/ti-gpmc.h>                                              
    #else                                                                           
    #include <asm/arch/mem.h>                                                       
    #endif                                                                          
    #include <linux/mtd/omap_gpmc.h>                                                
    #include <linux/mtd/nand_ecc.h>                                                 
    #include <linux/bch.h>                                                          
    #include <linux/compiler.h>                                                     
    #include <nand.h>                                                               
    #include <linux/mtd/omap_elm.h>                                                 
    #include <dm.h>                                                                 
                                                                                
                                                                                
    #ifdef  CONFIG_CMD_NANDECC                                                      
    extern void omap_nand_switch_ecc(nand_ecc_modes_t hardware, int32_t mode);      
    static int do_switch_ecc(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
    {                                                                               
        int type = 1;                                                               
                                                                                    
        if (argc < 2)                                                               
            goto usage;                                                             
                                                                                
        if (strncmp(argv[1], "hw", 2) == 0) {                                       
            if (argc == 3)                                                          
                type = simple_strtoul(argv[2], NULL, 10);                           
            omap_nand_switch_ecc(NAND_ECC_HW, type);                                
        }                                                                           
        else if (strncmp(argv[1], "sw", 2) == 0)                                    
            omap_nand_switch_ecc(NAND_ECC_NONE, 1);                                 
        else                                                                        
            goto usage;                                                             
                                                                                    
        return 0;                                                                   
                                                                                
    usage:                                                                          
        printf("Usage: nandecc %s\n", cmdtp->usage);                                
        return 1;                                                                   
    }                                                                               

                                                                                
    U_BOOT_CMD(                                                                     
               nandecc, 3, 1,  do_switch_ecc,                                       
               "Switch NAND ECC calculation algorithm b/w hardware and software",   
               "[sw|hw <hw_type>] \n"                                               
               "   [sw|hw]- Switch b/w hardware(hw) & software(sw) ecc algorithm\n" 
               "   hw_type- 0 for Hamming code\n"                                   
               "            4 for bch4\n"                                           
               "            8 for bch8\n"                                           
               "            16 for bch16\n"                                          
              );                                                                    
    #endif                                                                          
  • 将该文件添加进所属文件的Makefile,上述例子所在目录是cmd 目录。
    我将在该目录里面加入如下选项:
    cat cmd/Makefile
    
    //...
    98 obj-$(CONFIG_CMD_NANDECC) += nandecc.o
    //... 
  • 然后在你的config.h 里面添加相关宏定义开关:
    cat include/configs/am335x_sbc7109.h

    //...
    343 #define CONFIG_CMD_NANDECC  
    //...
  • 然后启动u-boot ,看是否有nandecc 这条命令。有即成功加入。
posted @ 2017-03-02 18:06  陈富林  阅读(500)  评论(0编辑  收藏  举报