u-boot for bf561中的命令实现

 
硬件平台:bf561
软件平台:u-boot-1.1.6,gcc for blackfin,visual dsp 5.0
我们知道,u-boot的运行过程是首先进行一些初始化化工作,然后在一个死循环中不断接收串口的命令并进行解释执行,下面我们就看看执行部分代码的实现,见common/main.c中的run_command:
int run_command (const char *cmd, int flag)
{
       while (*str) {
        …
 
              /* find macros in this token and replace them */
              process_macros (token, finaltoken);
 
              /* Extract arguments */
              if ((argc = parse_line (finaltoken, argv)) == 0) {
                     rc = -1;   /* no command at all */
                     continue;
              }
 
              /* Look up command in command table */
              if ((cmdtp = find_cmd(argv[0])) == NULL) {
                     printf ("Unknown command '%s' - try 'help'/n", argv[0]);
                     rc = -1;   /* give up after bad command */
                     continue;
              }
 
              /* found - check max args */
              if (argc > cmdtp->maxargs) {
                     printf ("Usage:/n%s/n", cmdtp->usage);
                     rc = -1;
                     continue;
              }
        …
 
              /* OK - call function to do the command */
              if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
                     rc = -1;
              }
 
              repeatable &= cmdtp->repeatable;
 
              /* Did the user stop this? */
              if (had_ctrlc ())
                     return 0; /* if stopped then not repeatable */
       }
 
       return rc ? rc : repeatable;
}
很简单的一个过程,扩展宏定义 -> 分析命令及其参数 -> 查找命令 -> 执行命令,有意思的地方在查找命令上(common/command.c):
cmd_tbl_t *find_cmd (const char *cmd)
{
       cmd_tbl_t *cmdtp;
       cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start;     /*Init value */
       const char *p;
       int len;
       int n_found = 0;
 
       /*
        * Some commands allow length modifiers (like "cp.b");
        * compare command name only until first dot.
        */
       len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
 
       for (cmdtp = &__u_boot_cmd_start;
            cmdtp != &__u_boot_cmd_end;
            cmdtp++) {
              if (strncmp (cmd, cmdtp->name, len) == 0) {
                     if (len == strlen (cmdtp->name))
                            return cmdtp; /* full match */
 
                     cmdtp_temp = cmdtp;   /* abbreviated command ? */
                     n_found++;
              }
       }
       if (n_found == 1) {                     /* exactly one match */
              return cmdtp_temp;
       }
 
       return NULL; /* not found or ambiguous command */
}
看起来还是很简单的一个过程,在一个命令数组中查找是否有指定名称的命令。问题是, 在这里使用的两个符号 __u_boot_cmd_start __u_boot_cmd_end ,在所有的 C 文件中都找不到它们的定义,那么它们的空间从哪里来呢?这些分散在不同文件中的结构体又是如何能够放在同一个数组中呢?
答案就在board/bf561-ezkit/u-boot.lds.s中,这个文件其实就是一个链接文件,类似于VDSP中的LDF文件,see see:
 ___u_boot_cmd_start = .;
 .u_boot_cmd : { *(.u_boot_cmd) }
 ___u_boot_cmd_end = .;
这几句话的意思其实就是指示 链接器将所有.u_boot_cmd数据段中的内容全部放在一起,而且___u_boot_cmd_start和___u_boot_cmd_end是不会占用任何存储空间的,它们只是用来指示地址的两个符号而已。那么数据段的定义在哪里呢?看看U_BOOT_CMD的宏定义吧(include/command.h):
#define Struct_Section __attribute__ ((unused,section (".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, help}
__attribute__ ((unused,section (".u_boot_cmd")))就指示编译器将这些用U_BOOT_CMD定义的结构体放在.u_boot_cmd这个数据段中。
如果要在VDSP中编译u-boot,那么就需要在LDF文件中也定义这样一个数据段:
      .u_boot_cmd
      {
               ___u_boot_cmd_start = .;
               INPUT_SECTIONS(common.dlb(.u_boot_cmd) common.dlb(__u_boot_cmd))
               ___u_boot_cmd_end = .;
      } > MEM_SDRAM_U_BOOT
不过让人郁闷的是: 如果在一个定义命令的 C 文件中没有一个函数被其它文件引用, VDSP 在链接时将认为这是一个多余的文件,从而不会将这个文件中的函数链接进来,当然就无法使用其中定义的这些命令,如 cmd_load.c
解决的办法可以是在这些文件中添加一个空函数,并在主函数中调用它们,这样VDSP就会把这个文件链接进来了。
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌云阁主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值