U-boot relocation code analysis (I found an article on the Internet when I learned U-boot, which is awesome)

Source: Internet
Author: User

Overview

The relocate code copies the bootloader itself from flash to the SDRAM, so that it can jump to the SDRAM for execution. The reason why the system needs to be relocated is that the execution speed in Flash is relatively slow, and after the system is reset, it is always indicated by the 0x00000000 address.

Relocation code, at/U-boot/CPU/B0/start. S:

Relocate:/* relocate U-boot to ram
*/

ADR r0, _ start/* R0 <-current position of code
*/

LDR R1, _ text_base/* test if we run from flash or RAM
*/

CMP r0, R1/* Don't reloc during debug
*/

Beq stack_setup

LDR R2, _ armboot_start

LDR R3, _ bss_start

Sub R2, R3, R2/* R2 <-size of armboot
*/

Add R2, R0, R2/* R2 <-source end address
*/

Copy_loop:

Ldmia R0 !, {R3-r10}/* copy from Source Address [R0]
*/

Stmia R1 !, {R3-r10}/* Copy to target address [R1] */

CMP r0, R2/* Until source end address [R2]
*/

Ble copy_loop

The code above first determines whether a relocation is required. If necessary, first determine the source base address, source size, and target base address of the replication, and then use R3 ~ R13 is the medium, and the bootloader is copied to the SDRAM.

Analysis

Copy_loop is easy to understand. Here we mainly analyze the first two commands at relocate:

1. ADR r0, _ start

ADR is a pseudo-command. The assembler always tries to generate commands such as Add/sub for it (here) to load the target register as the base address of the PC. The following is the disassembly code generated by arm-elf-objdump:

C700048: e24f0050 sub r0, PC, #80
; 0x50

The e24f0050 is the machine code corresponding to the command, and the c700048 is the address for storing the machine code (in hexadecimal format ). How does this address come from? Answers to the following questions in/U-boot/config. mk:

Ldflags + =-bstatic-T $ (ldscript)-ttext $ (text_base) $ (platform_ldflags)

The preceding macro specifies the command line parameter for connection.-ttext is set. text Segment address, while text_base in/U-boot/board /... /config. MK is defined as 0x0c700000. The information is recorded in the program image file in hard-coded format.
= Text_base = 0x0c700000:

Disassembly of section. Text:

0c700000 <_ Start>:

C700000: ea00000a B c700030 <reset>
...

However, the program image is written to flash and executed, and the flash Address starts from 0x00000000. Therefore, the first command of the program image is aligned to 0x00000000. Correspondingly, the address of this ADR command should be aligned to 0x00000048, and R0 is equal to 0 after execution.

2. LDR R1, _ text_base

The following is the disassembly code generated by arm-elf-objdump:

C70004c: e51f1034 LDR R1, [PC, #-52]
; C700020 <_ text_base>

It can be seen that the LDR here does not simply load the 4 bytes at the _ text_base address to R1, but also calculates the source address based on PC. PC = 0x4c + 8 = 0x54, so this command sets 0x54
-52 = 0x20 4 bytes (that is, text_base, that is, 0x0c700000) are loaded to R1.

3. determine the size of the source

Through the above analysis, we already have a concept: the actual execution address of the program may be different from the loading address specified during the connection. We have obtained the start address of the bootloader code, which is stored in R0. We also need to calculate the end address of the bootloader code. Runtime end address =
Start address + code segment size during running. The code segment size is obtained from the expected start address of the. BSS segment-the expected start address of the. Text Segment.

Summary

Use the-ttext option during connection to hard encode the address of the. Text Segment to the program image. Although the actual execution address of the program image is inconsistent with the expected execution address in flash, the relative addressing is performed by using the PC as the base address before relocate, this makes the code execution irrelevant to the actual load address.

Related Article

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.