[MIPS-uboot] 3 mips u-boot analysis and Transplantation

Source: Internet
Author: User

Http://blog.yaabou.com /? P = 96

 

Note that MIPs has the pipeline visibility, so the next command following the jump command will be executed before the jump is executed. This is called Branch latency. However, the compiler hides this feature, but you can set ". Set noreorder" to disable the compiler from reorganizing the code order.

Each Board has its own lDs file. This is mainly used to describe the instructions generated by the compilation and the data placement location during the running process. For more information, see the LD manual. For example, board/dbau1x00/u-boot.lds.

Output_format (elf32-tradbigmips, elf32-tradbigmips, elf32-tradbigmips ")
/* The generated format is elf. Big end, MIPS */
Output_arch (MIPs)
/* The platform is MIPS */
Entry (_ start)/* The entry point is _ start */
Sections
{
. = 0 × 00000000;

. = Align (4 );
. Text:/* this is where the program is stored */
{
* (. Text)
}

. = Align (4);/* Indicates align in 4 bytes */
. Rodata: {* (sort_by_alignment (sort_by_name (. rodata *)))}

. = Align (4 );
. Data: {* (. Data )}

. = .;
_ Gp = align (16) + 0x7ff0;

. Got :{
_ Got_start =.;/* indicates the value of this address to _ got_start */
* (. Got)
_ Got_end = .;
}

. Sdata: {* (. sdata )}

. U_boot_cmd :{
_ U_boot_1__start = .;
* (. U_boot_cmd)
_ U_boot_1__end = .;
}

Uboot_end_data = .;
Num_got_entries = (_ got_end-_ got_start)> 2;

. = Align (4 );
. Sbss (noload): {* (. sbss )}
. BSS (noload): {* (. BSS). = align (4 );}
Uboot_end = .;
}

Next we will analyze the CPU/MIPS/start. s
Start with _ start. The first 128 characters (not bytes) are reserved for the exception entry point.
1. The first two are hard reset and soft reset, both of which jump to the reset.
2. The following figure shows some major bits of cp0 (coprocessor 0, which controls CPU by MIPS.
3. Disable the cache.
4. The following is interesting. Why do I have to jump? In this way, you can know the position of the Code, rather than the label value. For example, it may be in Ram or ROM.
Bal 1f
NOP
. Word _ GP
1:
Lw gp, 0 (RA)
5. Run lowlevel_init here.
. This is the first function we need to define.
. Since the stack is not initialized, only assembly can be used here. We can see that jalr is followed by a NOP, which is the branch delay slot, and nothing is executed here.
6. the following command executes the mips_cache_reset, which cleans up the data and command cache and sets it to the correct value. Then you can open the cache.
7.
Because our memory may not have been initially initialized (some will have initialization in lowlevel_init, but some will not ). However, if we use the C function, we need a stack.
Memory space. So here we run the mips_cache_lock command to lock the cache address, that is, the cache is used as the memory. Then we set the stack address to locked.
The highest address of the cache (because the stack grows down ). Now we can use the C function. Of course, you can't use malloc or waste stacks too much.
8. Here we go to the C initialization function -- board_init_f.

For MIPS, board_init_f is under lib_mips/board. C. In the board_init_f () function, some functions are initialized and Ram is divided.
/* In the end, Ram is like this.
-Ram 0x0000,000 0-
............
-^ SP ^-
-Boot Params-config_sys_bootparams_len BD-> bi_boot_params
-Global Data-sizeof (gd_t) Gd
-Board Info-sizeof (bd_t) Gd-> BD = BD
-Mallco (+ env)-config_sys_malloc_len + config_env_size
-Uboot code-16 KB
-Ram end-
*/
Let's take a look at what features are initialized. All initialized functions are in init_fnc_t * init_sequence.
Init_fnc_t * init_sequence [] = {
Board_early_init_f,/*, which must be initialized before. To use it, define config_board_early_init_f */
Timer_init,/* initialize the clock count, cp0 */
Env_init,/* save environment variables in Flash */
# Ifdef config_inca_ip
Incaip_set_cpuclk,/* set the CPU clock speed according to the cpuclk environment variable */
# Endif
Init_baudrate,/* Set Gd-> baudrate */Based on the baudrate environment variable */
Serial_init
,/* Set the serial speed, which needs to be written by ourselves (including other serial)
*/
Lele_init_f,/* Set Gd-> have_console = 1. If config_silent_console exists, view silent */
Display_banner,/* print uboot information */
Checkboard
,/* Check the board. You can print the device information here. We need to write it by ourselves.
*/
Init_func_ram,/* Set Gd-> ram_size. We need to write initdram by ourselves.
*/
Null,/* The Last null value must be retained. Check ended */
};
Finally, this function calls relocate_code (addr_sp, ID, ADDR ). Note that this function is not returned because it cannot be returned.

Now we are back at start. S. We can see that the parameters passed in the C language are A0, A1, and A2. The relocate_code job is to move the code to ram for execution. The job is as follows:
1. Move the GP pointer
2. Copy the code to ram.
3. Refresh the cache
4. Jump to the ram code (in_ram)

The main tasks of in_ram are to update got, clear BSS segments, and finally jump to board_init_r. We can see that the last parameter of board_init_r is assigned a value in the branch delay slot.
In fact, here we mainly talk about gots (global offset
Tables) This is the principle that uboot can jump to different spaces to run. The position-independent (PIC) is used during uboot compilation.
Code) (also known as position-independent executable
(PIE )). This is something that was introduced long ago in the absence of MMU. In order to run different processes simultaneously without MMU, their running address can be changed.
Gots is used to save all the global variable addresses, so we only need to change the value of gots. GP is the pointer to the gots position. This function must be specified during GCC Compilation
-FPIC. As we can see, we only need to change the value in gots and add the address offset.

Let's take a look at board_init_r. Here's the work:
1. copy the information of the CMD segment. Only cmd, name, usage, and usage are copied here. The help information string is still in flash.
2. Then initialize the malloc function. Note that env allocates space in malloc mode and copies it to ram.
3. Then stdio, serial port, entry function, and global change are initialized Based on Env.
4. Then initialize the network. Eth_initialize (Gd-> BD ). For MIPS, if config_net_multi is set. We need to write board_eth_init and cpu_eth_init by ourselves.
Two functions. The latter must be executed only when the returned value is less than 0.
5. Enter main_loop ().

Ethernet driver porting
For MIPS, if config_net_multi is set. The board_eth_init () and cpu_eth_init () functions are used. Only the former
The latter is executed only when the return value is smaller than 0. Here we need to initialize the struct eth_device, and then use eth_register () to register the network device. Mainly set
Init, halt, send, Recv, and other variables. If the MII read/write command is supported, you also need to use miiphy_register () to register two MII read/write
Functions.
After the registration is complete, U-boot does not automatically call init. Instead, it is called only when a network device is used. Every time you use a network interface, you must call halt before calling init.

When we use the command make (model) _ config, the following results will be generated.
(1) Soft link include/asm to include/ASM-(ARCH ).
(2) modify the include/config. h file and add # include <configs/(model). h> and # include <ASM/config. h>.
(3) In include/config. mk, set the variables arch, CPU, board, and vendor.
During compilation, makefile will contain the config. mk file in several directories. Board/XXX/(xxx/) config. mk has been described earlier. In
The cross compiler is specified in lib_xxx/config. mk and the compilation parameters related to the platform are added. In CPU/XXX/config. mk, system structure parameters are specified, as shown in figure
Size end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.