对uboot、Linux、rootfs的深刻理解 uboot2012改进: 学习使用cmd,添加cmd_menu.c 了解U_BOOT_CMD的用法: #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ U_BOOT_CMD_COMPLETE(name,maxargs,rep,cmd,usage,help,NULL) #define U_BOOT_CMD_COMPLETE(name,maxargs,rep,cmd,usage,help,comp) \ cmd_tbl_t __u_boot_cmd_##name Struct_Section = \ U_BOOT_CMD_MKENT_COMPLETE(name,maxargs,rep,cmd,usage,help,comp) #define U_BOOT_CMD_MKENT_COMPLETE(name,maxargs,rep,cmd,usage,help,comp) \ {#name, maxargs, rep, cmd, usage, _CMD_HELP(help) _CMD_COMPLETE(comp)} U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) 1 name 命令的名字 2 maxargs 最大命令行参数个数 3 rep 4 cmd 命令 5 usage 用法 6 帮助
其实我们只关心前面4个宏参数,U_BOOT_CMD的最终定义是这样的: #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) cmd_tbl_t __u_boot_cmd_##name Struct_Section = \ {#name, maxargs, rep, cmd, usage, _CMD_HELP(help) _CMD_COMPLETE(comp)} 比如: U_BOOT_CMD( menu, 3, 1, do_menu, "sure you have prepared file by tftp, and press corresponding key", "sure you have prepared file by tftp \n" "press corresponding key, the u-boot will help you download file to the suited memory location \n" ); 得到: cmd_tbl_t __u_boot_cmd_menu Struct_Section = {"menu", 3, 1, do_menu, ...} #define Struct_Section __attribute__((unused, section(".u_boot_cmd"), \ aligned(4))) //将这个结构体链接到.u_boot_cmd输入段
__attribute__: 需要了解一下gcc的__attribute__的编绎属性,__attribute__主要用于改变所声明或定义的函数或数据的特性 它有很多子项,用于改变作用对象的特性 struct cmd_tbl_s { char *name; /* Command Name */ int maxargs; /* maximum number of arguments */ int repeatable; /* autorepeat allowed? */ /* Implementation function */ int (*cmd)(struct cmd_tbl_s *, int, int, char * const []); char *usage; /* Usage message (short) */ #ifdef CONFIG_SYS_LONGHELP char *help; /* Help message (long) */ #endif #ifdef CONFIG_AUTO_COMPLETE /* do auto completion on the arguments */ int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]); #endif }; typedef struct cmd_tbl_s cmd_tbl_t
简单一点: struct cmd_tbl_s __u_boot_cmd_menu = { .name = "menu", .maxargs = 3, .repeatable = 1, .cmd = do_menu } 从int (*cmd)(struct cmd_tbl_s *, int, int, char * const []); 看出do_menu应该有4个参数 输入 menu 命令应该就会执行do_menu函数
为u-boot添加menu菜单:
在common目录下添加这个程序:https://www.cnblogs.com/zhu-g5may/p/10084942.html
修改Makefile,编译进内核
效果:
CPUID: 32440001 FCLK: 400 MHz HCLK: 100 MHz PCLK: 50 MHz DRAM: 64 MiB WARNING: Caches not enabled Flash: 0 KB NAND: 256 MiB In: serial Out: serial Err: serial Net: dm9000 Hit any key to stop autoboot: 0 SMDK2440 # SMDK2440 # SMDK2440 # SMDK2440 # SMDK2440 # menu
##### u-boot cmd menu ##### [o] download u-boot to nor [n] download u-boot to nand [k] download kernel to nand [f] download rootfs to nand [b] boot the system [q] quit from menu