u-boot analysis __uboot command implementation

content

1. Principle of uboot command

2. Add command example


1. Principle of uboot command

Let's take a look at the run_command function.

Here is the command to get the input, which can also be processed when the command has a semicolon,

Then parse_line parses these commands, for example, the command string md.w 0 will be parsed into

argv[0]="md.w"

argv[1]="0"

After parsing and extracting the command and parameters, use the find_cmd function to find the structure corresponding to the command according to argv[0], and then assign it to the structure of the cmd_tbl_s type of cmdtp

This structure contains the name, the maximum parameter, and whether it is repeatable or not. Whether it is repeatable means that after you enter the command once and execute it, you do not need to enter the command again. Just press Enter to execute it again, and then cmd this The function pointer is the function corresponding to the command, then usage is short help information, help is long help information, when we only input help on the serial port, the short help information about each command comes out, if we enter help xx, What comes out is a long help message about the command.

Then let's take a look at the find_cmd function, where two variables appear in the find_cmd function

 The two variables _u_boot_cmd_start and _u_boot_cmd_end are not in the code, these two are defined in the connection script.

 Then we look at what the u_boot_cmd segment does, we search in the code to find

 Take the command bootm 0x30007EC0 as an example, which function does it execute after we enter bootm 0x30007EC0, we don't know, we searched for this bootm directly, and then we found a macro U_BOOT_CMD

We searched for this U_BOOT_CMD macro and found the definition of this macro, 

Then we expand U_BOOT_CMD, where ## is a hyphen, and then struct_Section is also a macro. After expanding the U_BOOT_CMD macro, it is

 After expansion, it is found that a structure __u_boot_cmd_bootm is defined here. The type of this structure is of type cmd_tbl_t. This structure also has an attribute (_attribute__), which forces its segment attribute to be set to ._uboot_cmd.

So all the things defined by the macro U_BOOT_CMD in the code are ultimately equivalent to defining a structure. What is so special about this structure, especially that he has an attribute that forces his segment to be specified as .u_boot_cmd, so say These final commands will be centrally stored in the u_boot_cmd segment.

2. Add command example

Next, let's see what needs to be done if we add a command. For example, we add a hello command to print hello world.

We follow the bootm command, first we create a new c file cmd_hello.c,

 After the code is written, modify the common/makefile,

 Then compile and reprogram u-boot.bin.

Guess you like

Origin blog.csdn.net/u013171226/article/details/123185102