uboot增加命令

分析源码

首先从common/main.c里面开始分析:
查看函数:

void main_loop (void)                                                                      
{
...
run_command (s, 0);
...

这里会调用到run_command执行串口命令:

int run_command (const char *cmd, int flag) 
{
	...
1360         /* Look up command in command table */                                             
1361         if ((cmdtp = find_cmd(argv[0])) == NULL) {                                         
1362             printf ("Unknown command '%s' - try 'help'\n", argv[0]);                       
1363             rc = -1;    /* give up after bad command */                                    
1364             continue;                                                                      
1365         }  
	...
}

这里首先需要调用find_cmd去找到对应的命令

346 cmd_tbl_t *find_cmd (const char *cmd)                                                       
347 {                                                                                           
348     cmd_tbl_t *cmdtp;                                                                       
349     cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start;    /*Init value */                         
350     const char *p;                                                                          
351     int len;                                                                                
352     int n_found = 0;                                                                        
353                                                                                             
354     /*                                                                                      
355      * Some commands allow length modifiers (like "cp.b");                                  
356      * compare command name only until first dot.                                           
357      */                                                                                     
358     len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);                      
359                                                                                             
360     for (cmdtp = &__u_boot_cmd_start;                                                       
361          cmdtp != &__u_boot_cmd_end;                                                        
362          cmdtp++) {                                                                         
363         if (strncmp (cmd, cmdtp->name, len) == 0) {                                         
364             if (len == strlen (cmdtp->name))                                                
365                 return cmdtp;   /* full match */                                            
366                                                                                             
367             cmdtp_temp = cmdtp; /* abbreviated command ? */                                 
368             n_found++;                                                                      
369         }                                                                                   
370     }                                                                                       
371     if (n_found == 1) {         /* exactly one match */                                     
372         return cmdtp_temp;                                                                  
373     }                                                                                       
374                                                                                             
375     return NULL;    /* not found or ambiguous command */                                    
376 } 

重要的部分就是,从__u_boot_cmd_start, __u_boot_cmd_end这个两个地址拷贝所有支持的命令,然后比对查找。搜索整个工程,在链接脚本可以找到:

 97   . = .;
 98   __u_boot_cmd_start = .;                                                                   
 99   .u_boot_cmd : { *(.u_boot_cmd) }                                                          
100   __u_boot_cmd_end = .; 

继续搜索在那些地方使用了,u_boot_cmd.在include/common.h里面可以找到

 93 #define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))                    
 94                                                                                             
 95 #ifdef  CFG_LONGHELP                                                                        
 96                                                                                             
 97 #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \                                       
 98 cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}      
 99                                                                                             
100 #else   /* no long help info */                                                             
101                                                                                             
102 #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \                                       
103 cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage} 

可以看到,只要是使用了U_BOOT_CMD,都会被放进链接脚本指定的段里面。
搜索U_BOOT_CMD,就可以看到整个uboot里面的所有命令是怎样写的。

增加命令

仿照uboot里面已经有的命令,增加自己自定义的命令
在common目录下增加,my_cmd.c

#include <common.h>
#include <command.h>

int do_my_cmd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	printf("Hello word.\n");
	int   rcode = 0;
}

U_BOOT_CMD(
	my_cmd,	5,	1,	do_my_cmd,
	"hello word \n",
	"long hello word\n"
);

修改common目录下的Makefile,增加my_cmd.o重新编译即可

验证##### 100ask Bootloader for OpenJTAG

[n] Download u-boot to Nand Flash
[o] Download u-boot to Nor Flash
[c] Re-scan Nor Flash
[u] Copy bootloader from nand to nor
[v] Copy bootloader from nor to nand
[k] Download Linux kernel uImage
[j] Download root_jffs2 image
[y] Download root_yaffs image
[d] Download to SDRAM & Run
[z] Download zImage into RAM
[g] Boot linux from RAM
[f] Format the Nand Flash
[s] Set the boot parameters
[b] Boot the system
[r] Reboot u-boot
[q] Quit from menu
Enter your selection: q
OpenJTAG> my_cmd
Hello word.
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值