且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

U-boot中怎么添加命令选项

更新时间:2021-12-20 23:52:37

在command.h中分析命令结构


/* 这是定义一个结构的属性,将其放在.u_boot_cmd这个段当中,相当于.data/.bss这些段 */
#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}
这样一来,凡通过U_BOOT_CMD定义的cmd_tbl_t变量会全部被放在.u_boot_cmd段当中(可以看UBOOT的链接脚本xxx.lds),具体怎么放是链接器的工作。
name :  命令的名字
maxargs:最大的参数
rep:是否可重复发命令
usage:短帮助信息
help:长帮助信息


增加一个hello命令,具体格式如下:把这个hello_common.c放在commoc文件夹下面
/*参考cmd_bootm.c文件*/
#include <common.h>
#include <watchdog.h>
#include <command.h>
#include <image.h>
#include <malloc.h>
#include <u-boot/zlib.h>
#include <bzlib.h>
#include <environment.h>
#include <lmb.h>
#include <linux/ctype.h>
#include <asm/byteorder.h>


int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
  printf("hello world!\n");
  return 0;
}


U_BOOT_CMD(
hello,CONFIG_SYS_MAXARGS,1,do_hello,
"just for test"
"boot application image from memory",
);