Serial data is received and passed to run_command () function, run_command () function call common / command.c implemented find_cmd () function to find the u_boot_list command section, and returns cmd_tbl_t structure. Then run_command () function returns a pointer cmd_tbl_t structure used in the function call do_hello (), thereby completing execution of the command
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 */ }
In u-boot.1.1.6 / board / 100ask24x0 / u-boot.lds in
__u_boot_cmd_start = .; .u_boot_cmd : { *(.u_boot_cmd) } __u_boot_cmd_end = .;
In u-boot.1.1.6 / include / command.h in
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}
Struct_Section the __attribute__ #define ((unused, sectionTop ( ".u_boot_cmd")))
// can be interpreted as: instantiating a cmd_tbl_t type structure, and save it in the section where .u_boot_cmd
In u-boot.1.1.6 / include / command.h, the following definitions cmd_tbl_t
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 *[]); char *usage; /* Usage message (short) */ #ifdef CFG_LONGHELP char *help; /* Help message (long) */ #endif #ifdef CONFIG_AUTO_COMPLETE /* do auto completion on the arguments */ int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]); #endif };
typedef struct cmd_tbl_s cmd_tbl_t;
In u-boot.1.1.6 / common, the new file cmd_hello.c
#include <common.h> #include <command.h> int do_hello ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] ) { printf("this is hello!\n"); return 0; } U_BOOT_CMD( hello, 1, 0, do_hello, "hello\t- this cmd is hello\n", "this cmd is hello,too\n" );
After addition cmd_hello.o behind u-boot.1.1.6 / common / Makefile of COBJS; in the root directory make, programming; hello new commands can occur