Method, apparatus and medium for relocating bootloader firmware
By generating an executable bootloader U-Boot firmware relocation method, apparatus, device and medium, the relocation technology of the U-Boot firmware is obtained, which solves the boot problem of the embedded operating system in the prior art and enables the normal boot of the embedded operating system.
Patent Information
- Application Number
- CN202211666027.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-23
- Publication Date
- 2026-02-17
- Estimated Expiration
- 2042-12-23
AI Technical Summary
How to relocate the code of the U-Boot program to enable the normal startup of the embedded operating system.
By generating an executable bootloader U-Boot firmware, the relocation tuple data, immediate relocation stack, and other code relocation-related content are obtained. The U-Boot firmware is copied into memory and its relocation offset address is determined. The relevant content is corrected to obtain the relocation-processed U-Boot firmware.
The embedded operating system was successfully booted, and the target device's operating system was guided by the U-Boot firmware after code relocation processing.
Smart Images

Figure CN115981741B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device and medium for relocating bootloader firmware. Background Technology
[0002] The Universal Boot Loader (U-Boot) is used to boot an embedded operating system, including loading boot parameters, loading the device tree, starting the operating system kernel, and mounting the root file system, so that the embedded operating system can start and run normally.
[0003] Currently, after the embedded operating system is powered on or reset, the U-Boot program copies its own program from the storage medium to the off-chip synchronous dynamic random-access memory (SDRAM), and then jumps to the SDRAM to execute the U-Boot program, thereby booting the embedded operating system.
[0004] When executing the U-Boot program in SDRAM, code relocation is required for it to run correctly. However, how to perform code relocation of the U-Boot program to boot the embedded operating system is a technical problem that must be solved. Summary of the Invention
[0005] This application provides a method, apparatus, device, and medium for relocating bootloader firmware to solve the problem of how to relocate the code of the U-Boot program in order to boot an embedded operating system.
[0006] Firstly, this application provides a method for relocating the bootloader U-Boot firmware, comprising:
[0007] The second electronic device preprocesses the initial U-Boot firmware to obtain the processed target U-Boot firmware. The initial U-Boot firmware includes: relocation tuple data, an immediate relocation stack, and a reserved relocation data segment. The relocation tuple data represents the data to be relocated. The immediate relocation stack stores the address information corresponding to the symbols called across files. The relocation data segment includes a global offset table generated using the relocation tuples corresponding to the target type in the relocation tuple data.
[0008] After receiving an external boot command, the processor of the first electronic device starts the target U-Boot firmware that supports relocation in the memory to perform initialization operations;
[0009] The processor performs memory address relocation processing on a global offset table and an immediate number relocation stack corresponding to the target U-Boot firmware, updates the global offset table and the immediate number relocation stack, and obtains the target U-Boot firmware after the relocation processing.
[0010] The processor runs the target U-Boot firmware in the memory space and performs positioning jump according to target information recorded by the target U-Boot firmware, thereby guiding the operating system carried by the first electronic device to start.
[0011] In a second aspect, the present application provides a relocation device of a bootloader U-Boot firmware, comprising:
[0012] The receiving module is configured to receive a starting instruction.
[0013] The processing module is configured to start a target U-Boot firmware supporting relocation in the memory to perform an initialization operation, perform memory address relocation processing on a global offset table and an immediate number relocation stack corresponding to the target U-Boot firmware, update the global offset table and the immediate number relocation stack, and obtain the target U-Boot firmware after the relocation processing.
[0014] The running module is configured to run the target U-Boot firmware in the memory space and perform positioning jump according to target information recorded by the target U-Boot firmware, thereby guiding the operating system to start.
[0015] In a third aspect, the present application provides an electronic device, comprising a processor and a memory in communication connection with the processor.
[0016] The memory stores computer execution instructions.
[0017] The memory is used for temporarily storing operation data required by the processor to execute the computer execution instructions stored in the memory.
[0018] The processor executes the computer execution instructions stored in the memory to implement the method in any one of the first aspect.
[0019] In a fourth aspect, the present application provides a computer readable storage medium, wherein the computer readable storage medium stores computer execution instructions, and the computer execution instructions are used for implementing the method in any one of the first aspect when executed by a processor.
[0020] In a fifth aspect, the present application provides a computer program product, comprising a computer program, and the computer program is used for implementing the method in any one of the first aspect when executed by a processor.
[0021] The bootloader firmware relocation method, apparatus, device, and medium provided in this application generate an executable bootloader U-Boot firmware, obtain relocation tuple data, immediate relocation stack, and other code relocation-related content in the U-Boot firmware, copy the U-Boot firmware into memory and determine the offset address for relocating the U-Boot firmware, and then correct the relocation tuple data, immediate relocation stack, and other related content in the U-Boot firmware using the offset address to obtain the relocated U-Boot firmware, thereby guiding the embedded operating system of the target device to start normally. Attached Figure Description
[0022] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0023] Figure 1 This is a schematic diagram of an existing process for booting an embedded operating system via U-Boot firmware;
[0024] Figure 2 A flowchart illustrating a method for relocating U-Boot firmware provided in this application embodiment;
[0025] Figure 3 A flowchart illustrating a method for preprocessing initial U-Boot firmware provided in this application embodiment;
[0026] Figure 4 A flowchart illustrating a memory address relocation processing method provided in an embodiment of this application;
[0027] Figure 5 A schematic diagram of a relocation device for a bootloader U-Boot firmware provided in this application embodiment;
[0028] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.
[0029] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0030] The exemplary embodiments will be described in detail herein with reference to the attached drawings. In the following description, like reference numerals refer to like elements throughout the description. The following exemplary embodiments are not representative of all embodiments consistent with the present application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of the present application, as detailed in the appended claims.
[0031] Firstly, the terms involved in the present application are explained:
[0032] Code relocation: refers to moving the code from the storage address to the running address for running. For example, the code is stored in the Flash of the device, but the code requires to be run at another specified address, so it is necessary to assign a copy of the code to the specified address for running.
[0033] Universal Boot Loader (U-Boot): refers to a program used to boot the embedded operating system, including loading startup parameters, loading device trees, starting operating system kernels, mounting root file systems, etc., so that the embedded operating system can be normally started and run.
[0034] The following takes the start of the embedded operating system as an example to explain how to boot the start of the embedded operating system through the U-Boot firmware. Figure 1 The existing flowchart for booting the embedded operating system through the U-Boot firmware is shown in FIG. 1. As shown in FIG. 1, the flowchart can include: Figure 1
[0035] S101, powering on or resetting the target device,
[0036] The target device may, for example, be any device using the embedded operating system, such as a smart phone, a tablet computer mobile device, a portable audio and video player, a car machine device, a smart robot, etc.
[0037] S102, judging the startup mode of the U-Boot firmware.
[0038] The U-Boot firmware is stored in the memory of the target device where the embedded operating system is located, which may, for example, be a NOR Flash, a NAND Flash, a Low Pin Count (LPC) memory, etc. The processor judges the startup mode of the program in the U-Boot firmware according to the type of the memory where the U-Boot firmware is located, and starts and executes the program of the U-Boot firmware from the 0 address of the corresponding memory.
[0039] S103, mapping the memory 0 address to an entry address, and running the program in the U-Boot firmware from the entry address.
[0040] The processor determines to start and execute the U-Boot firmware according to the starting mode of the U-Boot firmware. The processor maps the 0 address of the memory where the U-Boot is located to an entry address (virtual address) of a virtual address, and runs the U-Boot firmware from the entry address. The entry address is determined according to the architecture of the processor.
[0041] S104, memory initialization and memory training.
[0042] How to perform the memory initialization and memory training of the target device can refer to the prior art, and the present application will not be repeated here.
[0043] S105, relocating the program code in the U-Boot firmware to the memory.
[0044] Since the program code in the U-Boot firmware includes location-dependent code and location-independent code, the location-independent code has no requirement for the address at runtime, i.e. the address of the location-independent code can be any address. The location-dependent code is an absolute address and must be run at the corresponding address. Therefore, how the processor relocates the program in the U-Boot firmware to the memory for running.
[0045] In addition, since the processor needs to complete the operation of running the U-Boot firmware, executing the program code relocation in the U-Boot firmware, etc. through the program counter (PC), and the U-Boot firmware is initially run in the memory and needs to be relocated to run in the memory. Therefore, the address of the unit where the instruction included in the PC is located also needs to be corrected from the memory to the memory by code relocation.
[0046] S106, running the U-Boot firmware in the memory.
[0047] When the processor runs the U-Boot firmware in the memory, the U-Boot firmware loads the boot parameters, system kernel, device tree, etc. of the embedded operating system. The boot parameters, system kernel, etc. of the embedded operating system can be determined according to the actual needs of the embedded operating system, and the device tree of the embedded operating system can be determined according to the actual situation of the devices possessed or connected by the target device, which is not limited by the present application.
[0048] S107, starting the system kernel.
[0049] The system kernel is a kernel state corresponding to the embedded operating system, and includes a set of function modules responsible for managing the system.
[0050] S108, mounting a root file system.
[0051] The root file system is the first file system mounted when the system kernel is started, and the system kernel code image file is saved in the root file system, and the system boot program loads some basic initialization scripts and services into the memory to run after the root file system is mounted.
[0052] S109, executing an application program.
[0053] The application program in the embedded operating system is executed to perform the functions and services of the embedded operating system.
[0054] However, how to relocate the program in the U-Boot firmware in step S105 is a problem that must be solved.
[0055] Therefore, the present application provides a bootloader U-Boot firmware relocation method, which generates an executable bootloader U-Boot firmware, obtains the relocation tuple data, immediate number relocation stack and other code relocation related contents in the U-Boot firmware, copies the U-Boot firmware into the memory and determines the offset address of the U-Boot firmware to be relocated, and then corrects the relocation tuple data, immediate number relocation stack and other code relocation related contents in the U-Boot firmware through the offset address, obtains the U-Boot firmware after relocation processing, and thus bootstraps the embedded operating system of the target device to start normally.
[0056] The technical solutions of the present application and how the technical solutions solve the above technical problems will be described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of the present application will be described below with reference to the accompanying drawings.
[0057] The execution subject of the U-Boot firmware relocation method provided in the present application can be a processor of a first electronic device, i.e., a processor in the aforementioned target device. The memory of the first electronic device includes a target U-Boot firmware supporting relocation, to perform relocation processing on the U-Boot firmware. The target U-Boot firmware is generated by a relocation component, which can be a driver, program code software, or a medium storing relevant execution code, such as a U disk, etc. Alternatively, the relocation component can also be an entity device integrated or installed with relevant execution code, such as a chip, a microcontroller unit (MCU), a computer, etc.
[0058] The U-Boot firmware relocation method is described below by taking the relocation component as an example of an electronic device (a second electronic device). Figure 2 A flowchart of the U-Boot firmware relocation method provided in the present application is shown in FIG. 1. As shown in the figure, the method can include the following steps. Figure 2
[0059] S201, the second electronic device pre-processes an initial U-Boot firmware to obtain a processed target U-Boot firmware.
[0060] The initial U-Boot firmware includes relocation tuple data, an immediate number relocation stack, and a reserved relocation data segment.
[0061] The relocation tuple data is used to indicate data to be relocated. The relocation tuple data can include position-related code data in the initial U-Boot firmware, i.e., code types that need to be relocated when the initial U-Boot firmware runs in the memory. The relocation tuple data is used to describe position-related codes that need to be corrected in position when the U-Boot firmware is relocated.
[0062] The immediate number relocation stack is used to store address information of symbols called across files. For example, when a C symbol in a file B needs to be called in a file A, the address of the C symbol in the file A can be obtained through address offset data of the C symbol stored in the immediate number relocation stack. That is, the address of the C symbol in the file A = the address of the C symbol in the file B + the address offset data of the C symbol in the immediate number relocation stack.
[0063] The reserved relocation data section is a data section reserved in the U-Boot firmware, and is used to store the relocation tuple data. The start address and data section size of the reserved relocation data section can be determined according to actual needs. The reserved relocation data section includes a global offset table generated by using the relocation tuple corresponding to the target type in the relocation tuple data when the second electronic device pre-processes the initial U-Boot firmware, and the relocation tuple corresponding to the target type is the relocation tuple including the base address in the initial U-Boot firmware.
[0064] The relocation data section of the target U-Boot firmware supporting relocation includes a global offset table generated by using the relocation tuple corresponding to the target type in the relocation tuple data, and the relocation tuple corresponding to the target type is the relocation tuple including the base address in the initial U-Boot firmware. The relocation tuple corresponding to the target type includes tuple data related to the position-related code in the target U-Boot firmware supporting relocation, which can be, for example, relocation text, relocation read-only data, relocation data, and the like related to code relocation. The base address is the base address of the relocation tuple corresponding to the target type in the storage medium.
[0065] In a possible implementation, the pre-processing of the initial U-Boot firmware relocation data can include, for example, obtaining the relocation tuple corresponding to the target type by performing operations such as parsing, screening, and the like on the data related to relocation included in the initial U-Boot firmware. The global offset table is obtained according to the relocation tuple corresponding to the target type, and then the target U-Boot firmware supporting relocation is obtained.
[0066] In another possible implementation, the pre-processing of the initial U-Boot firmware relocation data can include, in addition to the above-mentioned operations such as parsing and screening on the data related to relocation included in the initial U-Boot firmware, at least one operation such as sorting, address conversion, and the like on the data related to relocation. The global offset table is obtained according to the relocation tuple corresponding to the target type, and then the target U-Boot firmware supporting relocation is obtained.
[0067] After obtaining the target U-Boot firmware supporting relocation, the method can further include: burning the target U-Boot firmware into a storage space corresponding to the first address of the memory of the first electronic device, so that the first electronic device can run and relocate the U-Boot firmware, and boot the embedded operating system according to the U-Boot firmware after relocation.
[0068] The memory may be, for example, a flash memory of the target device, such as a NOR flash memory, a NAND flash memory, or the like. The first address is an address of the target U-Boot firmware running with support of relocation, which may be, for example, a fixed entry address determined according to a processor of the first electronic device.
[0069] The U-Boot firmware may be burned into the memory of the first electronic device by the second electronic device, or may be burned into the memory of the first electronic device by another device after the target U-Boot firmware is generated by the second electronic device and distributed to the other device, and the present application does not limit the burning method.
[0070] S202, after the processor of the first electronic device receives an external startup instruction, the target U-Boot firmware with support of relocation is started in the memory to perform an initialization operation.
[0071] The startup instruction is used to start the first electronic device to power on or reset the first electronic device. The present application does not limit the source and sending method of the startup instruction, as long as the first electronic device can receive the instruction. The target U-Boot firmware is the target U-Boot firmware generated in S201.
[0072] The memory may be, for example, any flash memory in the first electronic device, and the flash memory will not be described here. The initialization operation may include initializing the processor, initializing the memory, initializing the memory allocation of the target U-Boot firmware, and the like.
[0073] S203, the processor performs memory address relocation processing on the global offset table and the immediate relocation stack corresponding to the target U-Boot firmware, updates the global offset table and the immediate relocation stack, and obtains the target U-Boot firmware after relocation processing.
[0074] The data corresponding to the contents in the global offset table and the immediate relocation stack of the target U-Boot firmware is address-dependent code, and the addresses of these data need to be relocated when the target U-Boot firmware runs on the memory or on the memory.
[0075] The memory address relocation processing may calculate the corrected address of the data in the global offset table and the immediate relocation stack of the target U-Boot firmware running on the memory according to the base address corresponding to the data in the memory and the offset address obtained when the target U-Boot firmware is copied to the memory.
[0076] Then, the obtained modified address is stored into the target U-Boot firmware as a new address of data in the global offset table and the immediate relocation stack corresponding to the target U-Boot firmware, so as to update the addresses of the global offset table and the immediate relocation stack in the target U-Boot firmware, and the updating process is the relocation process. After the updating, the target U-Boot firmware after the relocation is obtained.
[0077] The step can further include that the processor performs memory address relocation processing on the program counter. The program counter is used to indicate an address of a next instruction to be processed by the processor, and the instruction is an instruction for starting the embedded operating system in the processor. The memory address relocation processing can calculate a modified address of data in the program counter running on the memory according to a corresponding base address of the program counter in the memory and the obtained offset address when the target U-Boot firmware is copied into the memory. Then, the obtained modified address is stored as a new address of the data in the program counter.
[0078] Correspondingly, the processor runs the target U-Boot firmware in the memory space and performs positioning jump according to the target information recorded in the target U-Boot firmware, and the positioning jump further includes that a new address of data in the program counter is addressed, so as to position and jump the data in the program counter from the memory to run in the memory space.
[0079] Optionally, the memory address relocation processing can be performed on the program counter, the global offset table and the immediate relocation stack of the target U-Boot firmware by a relocation middleware. The relocation middleware can be a driver, program code software, a medium storing relevant execution code, such as a U disk, or an entity device integrated or installed with relevant execution code, such as a chip, a microcontroller unit (MCU), a computer, an electronic device, etc. When the first electronic device is powered on or reset, the relocation middleware is run to perform the above-mentioned modification of the base address of the program counter, the global offset table and the data in the immediate relocation stack of the U-Boot firmware.
[0080] S204, the processor runs the target U-Boot firmware in the memory space and performs positioning jump according to the target information recorded in the target U-Boot firmware, so as to guide the operating system carried by the first electronic device to start.
[0081] The target information of the target U-Boot firmware record includes a global offset table and new addresses of data in an immediate relocation stack, and according to the global offset table and the new addresses of data in the immediate relocation stack, the processor obtains a relocation tuple in the global offset table and data in the immediate relocation stack by addressing the new addresses.
[0082] When the processor of the first electronic device runs the U-Boot firmware after relocation processing in the memory, the processor can jump to the memory space by locating the relocation tuple and the data in the immediate relocation stack, so as to load the start-up parameters, the system kernel and the device tree of the embedded operating system on the first electronic device, thereby starting the kernel of the embedded operating system, mounting the root file system of the embedded operating system, and completing the start-up of the embedded operating system.
[0083] The U-Boot firmware relocation method provided by the present application obtains a global offset table and an immediate relocation stack included in an executable U-Boot firmware of a boot loader, uses offset addresses obtained by copying the U-Boot firmware into the memory to perform code relocation on the global offset table, the immediate relocation stack and data in a program counter, and obtains the U-Boot firmware after relocation processing. Then, the U-Boot firmware after relocation processing is run in the memory, thereby booting and starting the embedded operating system of the first electronic device.
[0084] Next, how the second electronic device obtains the initial U-Boot firmware is described.
[0085] Implementation manner A: obtaining the initial U-Boot firmware by compiling U-Boot source code.
[0086] The initial U-Boot firmware is an U-Boot firmware in an executable and linkable format (ELF) generated by compiling U-Boot source code.
[0087] The U-Boot source code is compiled, a target link parameter is added, and the target link parameter is used to write relocation tuple data. The target link parameter is used to retain relocation-related content (i.e., relocation tuple data) in the U-Boot firmware generated by the U-Boot source code in the executable file of the U-Boot firmware. The target link parameter can be, for example, an -emit-relocs parameter. How to write relocation tuple data by using the -emit-relocs parameter can refer to the prior art, and the present application will not be described again.
[0088] The immediate number relocation stack is generated and stored in a preset address space to obtain the initial U-Boot firmware. The preset address space can be selected according to actual needs, for example, the preset address space is a macro definition target interval. The immediate number relocation stack is automatically generated during the compilation of the source code of the U-Boot, that is, whether to generate the immediate number relocation stack is determined according to the source code of the U-Boot. When the selected source code of the U-Boot has the function of generating the immediate number relocation stack, the source code of the U-Boot is compiled, and the immediate number relocation stack is automatically generated. The macro definition target interval can be determined according to actual needs, and the present application does not limit this. The macro definition target interval can be the _GLOBAL_OFFSET_TABLE-sdata_end address interval. In the macro definition target interval, the address data included in the immediate number relocation stack is aligned in a fixed byte number, so that the processor can correctly access the address data included in the immediate number relocation stack. The fixed byte number is related to the bit number of the processor used by the target device, for example, when the bit number of the register of the processor is 64 bits, the alignment is 8 bytes; when the bit number of the register of the processor is 32 bits, the alignment is 4 bytes, and the like.
[0089] Implementation manner B: obtaining the initial U-Boot firmware which has been compiled according to the U-Boot source code from other devices. The other devices can compile the U-Boot source code by using the method in the implementation manner A, so as to obtain the initial U-Boot firmware.
[0090] Next, how the second electronic device pre-processes the initial U-Boot firmware to obtain the target U-Boot firmware supporting relocation in step S201 is described in detail.
[0091] Figure 3 A flowchart of a method for pre-processing the initial U-Boot firmware provided by the embodiment of the present application is shown in FIG. 3. Figure 3 As shown in FIG. 3, step S201 can include:
[0092] S301: The second electronic device obtains the relocation tuple of the target type from the relocation tuple data.
[0093] The target type corresponds to a relocation tuple in the initial U-Boot firmware. The data included in the initial U-Boot firmware can be divided into multiple data segments according to a name attribute, such as text, read-only data, data, relocation text, relocation read-only data, relocation data, and the like. For each data segment, the attribute of the data segment includes a name, a type, an address, a data size, and the like. The data included in the initial U-Boot firmware, and the division and processing of the data segments are related to the architecture of the processor and the actual generated initial U-Boot firmware, and can refer to the prior art, which will not be described here.
[0094] The relocation tuple data can include relocation text, relocation read-only data, relocation data, and the like in the U-Boot firmware. By parsing the above data segments, the tuple data of the data segment related to code relocation can be parsed from the data included in the initial U-Boot firmware, so as to obtain the relocation tuple data.
[0095] The parsing can be performed by selecting the data segment related to code relocation according to the name attribute in the above data segment, and then obtaining the data included in the data segment as the relocation tuple data. For example, the relocation tuple data obtained by parsing the relocation text, the relocation read-only data, and the relocation data can include an offset, an information, a type, a value, and the like. For example, Table 1 is a relocation tuple data provided by an embodiment of the present application:
[0096] Table 1
[0097] Offset Info Type Value …… 000001d640 00000000000 R_NONE - …… 000001d654 41daf0000001 R_32 00000000001d438 …… 000001d688 41dbd0000002 R_64 90000001c06da68 …… …… …… …… …… ……
[0098] Offset is an offset attribute of the relocation tuple data, representing an offset of the data relative to an initial position in the initial U-Boot firmware, thereby representing an address of the data in the initial U-Boot firmware. Info is an information attribute of the relocation tuple data. Type is a type attribute of the relocation tuple data. Value is a value attribute of the relocation tuple data. For the type attribute of the relocation tuple data, R_NONE, R_32, R_64, R_RELATIVE, and the like can be included.
[0099] According to the processor used by the target device, a target type is determined from the type attribute of the repositioning tuple data. The target type may be determined according to the number of bits of the register of the processor, for example. For example, when the processor is a 64-bit processor, the repositioning tuple data includes a 64-bit corresponding type attribute (e.g., R_64 in Table 1) and a 32-bit corresponding type attribute (e.g., R_32 in Table 2), and the 64-bit corresponding type is selected as the target type; when the processor is a 32-bit processor, the repositioning tuple data includes a 32-bit corresponding type attribute and does not include a 64-bit corresponding type attribute, and the 32-bit corresponding type is selected as the target type, and so on.
[0100] In S302, the second electronic device sorts the repositioning tuples of the target type according to the size relationship of the base addresses of the repositioning tuples of the target type.
[0101] Optionally, the repositioning tuples of the target type are sorted according to the descending relationship of the base addresses of the repositioning tuples of the target type, and the base address of the first repositioning tuple of the target type is the largest base address.
[0102] Optionally, the repositioning tuples of the target type are sorted according to the ascending relationship of the base addresses of the repositioning tuples of the target type, and the base address of the first repositioning tuple of the target type is the smallest base address.
[0103] In S303, the second electronic device performs offset statistics on the sorted repositioning tuples of the target type, and generates a global offset table.
[0104] The offset statistics may be performed on the repositioning tuples of the target type according to the base address of the repositioning tuples of the target type.
[0105] In a possible implementation, the second electronic device retains the base address of the repositioning tuple of the first target type, and sets the base addresses of the repositioning tuples of the remaining target types as the relative base addresses of the repositioning tuples of the previous target types, to obtain the processed repositioning tuples of the target types.
[0106] For example, the base address of the relocation tuple of the first target type is 0x900000001c007728, the base address of the relocation tuple of the second target type is 900000001c00774c, and the base address of the relocation tuple of the third target type is 900000001c00779c. According to the method, the base address of the relocation tuple of the second target type is a relative base address relative to the base address of the relocation tuple of the first target type, and the relative base address is 0x24. Similarly, the base address of the relocation tuple of the third target type is a relative base address relative to the base address of the relocation tuple of the second target type, and the relative base address is 0x50.
[0107] In another possible implementation, the second electronic device retains the base address of the relocation tuple of the first target type, and sets the base addresses of the relocation tuples of the remaining target types as relative base addresses relative to the base address of the relocation tuple of the first target type, to obtain the processed target-type relocation tuples.
[0108] By this method, it is possible to retain only the base address of the relocation tuple of the first target type, and store the base addresses of the relocation tuples of the other target types as relative base addresses represented by address offsets, so as to compress the global offset table storing the target-type relocation tuples to a smaller storage space, and improve the utilization of the storage space.
[0109] All the target-type relocation tuples are sequentially stored in the global offset table according to the sorting method in step S302, and the global offset table only stores the base address of the relocation tuple of the first target type and the relative base addresses of the relocation tuples of the other target types.
[0110] S304, the second electronic device writes the global offset table into the relocation data section to obtain the target U-Boot firmware.
[0111] The relocation data section is the relocation data section mentioned in step S201. After storing the relocation tuple data in the relocation data section, a specific type of data can be added to indicate the address range of the relocation tuple data stored in the data section. The difference between the target U-Boot firmware supporting relocation and the initial U-Boot firmware is that the target U-Boot firmware supporting relocation includes the global offset table in addition to the data in the initial U-Boot firmware.
[0112] It should be noted that the embodiment of the present application provides an exemplary way of how to save the storage space of the global offset table by reserving the relative base address. It should be understood that other ways can also be used to implement the above-described way of setting the relative base address. For example, the above step S303 can be replaced by the following way: the base address of the relocation tuple of the first target type is reserved, and the base addresses of the relocation tuples of the remaining target types are set to the relative base address of the relocation tuple of the first target type to obtain the processed target type relocation tuple.
[0113] The method for preprocessing the initial U-Boot firmware relocation data provided by the present application reduces the storage space for storing the target type relocation data by screening the types of the relocation data, obtaining the target type relocation data, and processing the base address of the target type relocation data into a relative base address, thereby improving the space utilization. Then, the global offset table is obtained through the target type relocation data, the preprocessing operation of the initial U-Boot firmware relocation data is completed, and a basis is provided for supporting the target U-Boot firmware supporting relocation in subsequent relocation, thereby further being used for booting the embedded operating system.
[0114] For ease of understanding, the following describes how to obtain the target type relocation tuple in step S301 by taking the processor of the target device as an example of the LoongArch architecture. It should be understood that this example is only one possible implementation provided by the present application, and the present application is not limited to the processor architecture in this example.
[0115] When the processor is of the LoongArch architecture, the entry address (virtual address) to which the processor maps the 0 address of the memory where the U-Boot firmware is located can be 0x900000001c000000. The processor of the LoongArch architecture runs the U-Boot firmware at the entry address.
[0116] Under the LoongArch processor architecture, the relocation component is an executable file of loongarch-reloc generated by compiling the loongarch-reloc.c file, and is mainly used for preprocessing the initial U-Boot firmware relocation data to obtain the target U-Boot firmware supporting relocation.
[0117] Under the LoongArch processor architecture, all the data included in the initial U-Boot firmware can be as shown in Table 2.
[0118] Table 2
[0119]
[0120] Among all the data included in the initial U-Boot firmware as shown in Table 2, the relocation tuple data is contained. The relocation tuple data can be obtained by parsing, screening, etc. all the data included in the initial U-Boot firmware.
[0121] The attributes of the data included in the initial U-Boot firmware include Name, Type, Address, Size, etc. The data included in the initial U-Boot firmware also includes other data with Name attribute, such as.got,.u_boot_list, etc. The other data with Name attribute is related to the actually generated initial U-Boot firmware, and can be referred to the prior art, which will not be described here. The relocation tuple data can include the relocation text (.rela.text), relocation read-only data (.rela.rodata), relocation data (.rela.data), etc. in the U-Boot firmware related to code relocation, and the relocation tuple data can be obtained by parsing the above tuple data related to code relocation.
[0122] The relocation tuple data obtained by parsing the above tuple data related to code relocation can include attributes such as offset, information, type, value, etc. For example, Table 3 is a kind of relocation tuple data provided by the embodiments of the present application:
[0123] Table 3
[0124]
[0125] Among them, Offset is the offset attribute of the relocation tuple data, Info is the information attribute of the relocation tuple data, Type is the type attribute of the relocation tuple data, and Value is the value attribute of the relocation tuple data. For the type attribute of the relocation tuple data, it can include R_LARCH_NONE, R_LARCH_32, R_LARCH_64, R_LARCH_RELATIVE, etc.
[0126] According to the processor used by the target device, a target type is determined from the type attribute of the relocation tuple data. The target type may, for example, be determined according to the number of bits of a register of the processor. For example, when the processor is a 64-bit processor, the relocation tuple data includes a 64-bit corresponding type attribute (e.g., R_LARCH_64 in Table 3) and a 32-bit corresponding type attribute (e.g., R_LARCH_32 in Table 3), and the 64-bit corresponding type is selected as the target type; when the processor is a 32-bit processor, the relocation tuple data includes a 32-bit corresponding type attribute and does not include a 64-bit corresponding type attribute, and the 32-bit corresponding type is selected as the target type, and the like.
[0127] According to the target type of the relocation tuple data obtained above, a global offset table is established, and the global offset table is written back to the reserved relocation data segment in the U-Boot firmware. For example, when the relocation tuple data is as shown in Table 3 and the processor is a 64-bit processor, the global offset table may, for example, be as shown in Table 4:
[0128] Table 4
[0129]
[0130] That is, the global offset table includes only the relocation tuple data of the R_LARCH_64 type, and the data content included in the global offset table includes only the offset attribute of the relocation tuple data, the type attribute of the relocation tuple data, and the value attribute of the relocation tuple data.
[0131] Under the LoongArch processor architecture, the reserved relocation data segment may, for example, be a.data.reloc data segment, the start address of the data segment is _rel_start, the data segment size is 48k bits, and the address range of the relocation tuple data stored in the data segment can be indicated by adding preset data type data after the last relocation tuple data stored in the data segment. The preset data type data may, for example, be data of the NONE type.
[0132] Next, how the processor of the first electronic device performs memory address relocation processing on the global offset table and the immediate number relocation stack corresponding to the target U-Boot firmware in step S203 is described in detail.
[0133] Figure 4 A flowchart of a memory address relocation processing method provided by an embodiment of the present application is shown in FIG. 3. As shown in FIG. 3, step S203 may, for example, include: Figure 4
[0134] S401, the processor copies the target U-Boot firmware from the storage to the memory to obtain a target address at which the target U-Boot firmware runs in the memory.
[0135] The target U-Boot firmware includes a memory allocation function, which allocates a memory address for the target U-Boot firmware in the memory when the processor copies the target U-Boot firmware from the storage to the memory. The processor obtains the target address at which the target U-Boot firmware runs in the memory according to the memory address allocated by the memory allocation function for the target U-Boot firmware.
[0136] When the target U-Boot firmware is copied from the storage to the memory, since the address-independent code is not an absolute address, the relocation of the position-independent code in the target U-Boot firmware is automatically completed when the copying is completed, but the copying operation cannot complete the relocation of the absolute address.
[0137] S402, the processor obtains an offset address corresponding to the target U-Boot firmware according to the running address of the target U-Boot firmware in the storage and the target address.
[0138] The offset of the running address is obtained by subtracting the running address of the target U-Boot firmware in the storage from the target address at which the target U-Boot firmware runs in the memory, and the offset is taken as the offset address corresponding to the target U-Boot firmware. For example, the running address of the target U-Boot firmware in the storage is 0x900000001c000000, and the target address at which the target U-Boot firmware runs in the memory is 0x900000000f000000, so the offset of the running address is -0xD000000.
[0139] S403, the processor corrects the base address corresponding to each target type in the relocation tuple of the global offset table by using the offset address.
[0140] The method of correcting the base address of the relocation tuple of each target type in the global offset table can be expressed by the following formula (1):
[0141] After_Address1 = Before_Address1 + Offset (1)
[0142] Wherein, After_Address1 is the base address of the target type of relocation tuple after modification; Before_Address1 is the base address of the target type of relocation tuple before modification, and Offset is the offset address. For example, the base address of the target type of relocation tuple before modification is 0000000d08c1, and the offset address is -0xD000000, and the base address of the target type of relocation tuple after modification can be obtained according to the base address before modification and the offset address.
[0143] By modifying the base address of the relocation tuple of each target type in the global offset table, the relocation processing of the relocation text, the relocation read-only data, the relocation data and the like related to code relocation in the target U-Boot firmware is completed.
[0144] S404, the processor modifies the base address in the address information corresponding to the cross-file called symbol in the immediate number relocation stack by using the offset address.
[0145] The method for modifying the base address in the address information corresponding to the cross-file called symbol in the immediate number relocation stack can be expressed by the following formula (2) for example:
[0146] After_Address2 = Before_Address2 + Offset (2)
[0147] After_Address2 is the base address in the address information of the called symbol in the immediate number relocation stack after modification; Before_Address2 is the base address in the address information of each called symbol in the immediate number relocation stack before modification; and Offset is the offset address. The modification method of the address information corresponding to the cross-file called symbol in the immediate number relocation stack is the same as the modification method of the target type of relocation tuple, which will not be described herein.
[0148] S405, the processor modifies the base address of the program counter by using the offset address.
[0149] The method for modifying the base address of the program counter can be expressed by the following formula (3) for example:
[0150] After_Address3 = Before_Address3 + Offset (3)
[0151] After_Address3 is the base address of the program counter after modification; Before_Address3 is the base address of the program counter before modification; and Offset is the offset address. The modification method of the address information of the program counter is the same as the modification method of the target type of relocation tuple, which will not be described herein.
[0152] By modifying the base address of the program counter, the processor is enabled to run the target U-Boot firmware from the modified base address (an address in the memory).
[0153] Figure 5 A structural schematic diagram of a bootloader U-Boot firmware relocation device provided by an embodiment of the present application is shown in FIG. 1. As shown in the figure, the device comprises a receiving module 11, a processing module 12, and a running module 13. Figure 5
[0154] The receiving module 11 is configured to receive a start instruction.
[0155] The processing module 12 is configured to start a target U-Boot firmware supporting relocation in a memory to perform an initialization operation. The processing module 12 performs memory address relocation processing on a global offset table and an immediate relocation stack of the target U-Boot firmware, updates the global offset table and the immediate relocation stack, and obtains the target U-Boot firmware after the relocation processing.
[0156] The running module 13 is configured to run the target U-Boot firmware after the relocation processing in the memory space, and perform a positioning jump according to target information recorded by the target U-Boot firmware, thereby guiding a start of an operating system.
[0157] In a possible implementation, the processing module 12 is specifically configured to copy the target U-Boot firmware from the memory to the memory, to obtain an address at which the target U-Boot firmware runs in the memory. According to a corresponding running address of the target U-Boot firmware in the memory and the target address, an offset address corresponding to the target U-Boot firmware is obtained. The offset address is used to modify a base address corresponding to each target type in each relocation tuple in the global offset table. The offset address is used to modify a base address in address information corresponding to a cross-file call symbol in the immediate relocation stack. The offset address is used to modify a base address of a program counter.
[0158] The bootloader U-Boot firmware relocation device provided by the embodiment of the present application can perform the bootloader U-Boot firmware relocation method in the method embodiment, and has similar implementation principles and technical effects, which will not be described herein again.
[0159] Figure 6 A structural schematic diagram of an electronic device is provided in the embodiments of the present application. The smart electronic device is configured to execute the U-Boot firmware relocation method of the foregoing method embodiments. The smart electronic device is configured to generate the target U-Boot firmware, for example, can be the second electronic device where the foregoing relocation component is located; the smart electronic device is configured to execute the U-Boot firmware relocation method, for example, can be the first electronic device. As shown in Figure 6 The electronic device 600 can include at least one processor 601, a memory 602, and a memory 603. In a possible implementation, the electronic device 600 can further include a communication interface 604.
[0160] The memory 602 is configured to store a program. Specifically, the program can include program code including computer operation instructions.
[0161] The memory 602 can include a high-speed RAM memory, and can further include a non-volatile memory such as at least one disk memory.
[0162] The processor 601 is configured to execute the computer operation instructions stored in the memory 602 to implement the method described in the foregoing method embodiments. The processor 601 can be a CPU, or an ASIC, or one or more integrated circuits configured to implement the embodiments of the present application.
[0163] The memory 603 is configured to temporarily store operation data required for executing the computer operation instructions stored in the memory 602 when the processor 601 executes the computer operation instructions.
[0164] Optionally, the processor 601 can communicate and interact with an external device through the communication interface 604. When the electronic device is the second electronic device where the relocation component is located, the external device can be the first electronic device.
[0165] In a specific implementation, if the communication interface 604, the memory 603, the storage 602 and the processor 601 are implemented independently, the communication interface 604, the memory 603, the storage 602 and the processor 601 can be connected to each other through a bus and complete communication with each other. The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc., but it does not mean that there is only one bus or one type of bus.
[0166] Optionally, in a specific implementation, if the communication interface 604, the memory 603, the storage 602 and the processor 601 are integrated on a chip, the communication interface 604, the memory 603, the storage 602 and the processor 601 can complete communication through an internal interface.
[0167] The application also provides a computer readable storage medium, which can include a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various media that can store program codes. Specifically, the computer readable storage medium stores program instructions, and the program instructions are used for the method in the above embodiments.
[0168] The application also provides a computer program product, which includes execution instructions stored in a readable storage medium. At least one processor of an electronic device can read the execution instructions from the readable storage medium, and the at least one processor executes the execution instructions to make the electronic device implement the method provided in the various embodiments.
[0169] Finally, it should be noted that: the above embodiments are only used to illustrate the technical solutions of the application, and not to limit them; although the application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or make equivalent replacement for part or all of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the application.
Claims
1. A method for relocating a U-Boot firmware, characterized in that, The method comprises the following steps: The second electronic device pre-processes the initial U-Boot firmware to obtain the target U-Boot firmware after processing; The initial U-Boot firmware comprises: relocation tuple data, an immediate number relocation stack, and a reserved relocation data segment; the relocation tuple data is used to indicate data to be subjected to code relocation; the immediate number relocation stack is used to store address information corresponding to a symbol of a cross-file call; the relocation data segment comprises a global offset table generated by using a relocation tuple corresponding to a target type in the relocation tuple data; After the processor of the first electronic device receives an external starting instruction, the processor starts the target U-Boot firmware supporting relocation in the memory to perform an initialization operation; The processor performs memory address relocation processing on the global offset table and the immediate number relocation stack corresponding to the target U-Boot firmware, updates the global offset table and the immediate number relocation stack, and obtains the target U-Boot firmware after relocation processing; The processor runs the target U-Boot firmware in the memory space and performs positioning and jumping according to target information recorded by the target U-Boot firmware, thereby guiding the starting of an operating system carried by the first electronic device; the target information comprises: a base address corresponding to each relocation tuple of each target type in the global offset table after correction, a base address in address information corresponding to a symbol of a cross-file call in the immediate number relocation stack after correction, and a base address of a program counter after correction.
2. The method of claim 1, wherein, The pre-processing of the initial U-Boot firmware by the second electronic device comprises the following steps: Obtaining relocation tuples of a target type from the relocation tuple data; the relocation tuples corresponding to the target type are relocation tuple data comprising a base address in the initial U-Boot firmware; According to a size relationship of base addresses corresponding to the relocation tuples of the target type, the relocation tuples of the target type are sorted; Offset statistics are performed on the sorted relocation tuples of the target type to generate the global offset table; The global offset table is written into the relocation data segment to obtain the target U-Boot firmware.
3. The method of claim 2, wherein, The offset statistics performed on the sorted relocation tuples of the target type to generate the global offset table comprise the following steps: The base address of the first relocation tuple of the target type is reserved, and the base addresses of the remaining relocation tuples of the target type are set as relative base addresses of the previous relocation tuples of the target type to obtain the relocation tuples of the target type after processing; According to the relocation tuples of the target type after processing, the global offset table is generated.
4. The method according to any one of claims 1 to 3, characterized in that, Before the pre-processing of the initial U-Boot firmware by the second electronic device, the method further comprises the following steps: The second electronic device compiles source code of the initial U-Boot according to target linking parameters to generate the relocation tuple data and the immediate number relocation stack, and obtains the initial U-Boot firmware.
5. The method according to any one of claims 1 to 3, characterized in that, The method further comprises the following steps: The second electronic device burns the target U-Boot firmware into a storage space corresponding to the first address of the memory.
6. The method according to any one of claims 1 to 3, characterized in that, The processor performs memory address relocation processing on a global offset table and an immediate number relocation stack corresponding to the target U-Boot firmware, including: copying the target U-Boot firmware from the memory to the memory to obtain a target address at which the target U-Boot firmware runs in the memory; obtaining an offset address corresponding to the target U-Boot firmware according to a running address corresponding to the target U-Boot firmware in the memory and the target address; using the offset address to respectively correct base addresses corresponding to each target type in each relocation tuple in the global offset table; using the offset address to correct a base address in address information corresponding to a cross-file call symbol in the immediate number relocation stack; using the offset address to correct a base address of a program counter.
7. The method of claim 6, wherein the processor runs the target U-Boot firmware in the memory space and performs positioning and jumping according to target information recorded by the target U-Boot firmware, including: addressing the relocation tuples, the immediate number relocation stack, and the program counter included in the target U-Boot firmware by using the corrected base addresses corresponding to each target type in each relocation tuple in the global offset table, the corrected base address in the address information corresponding to the cross-file call symbol in the immediate number relocation stack, and the corrected base address of the program counter; wherein the relocation tuples, the immediate number relocation stack, and the program counter are used to guide the operating system carried by the first electronic device to start. The apparatus includes:
8. An apparatus for relocating a bootloader U-Boot firmware, characterized in that, a receiving module configured to receive a starting instruction; a processing module configured to pre-process an initial U-Boot firmware to obtain a processed target U-Boot firmware, start the target U-Boot firmware supporting relocation in a memory to perform an initialization operation, perform memory address relocation processing on a global offset table and an immediate number relocation stack corresponding to the target U-Boot firmware, update the global offset table and the immediate number relocation stack, and obtain the target U-Boot firmware after relocation processing; the initial U-Boot firmware includes relocation tuple data, an immediate number relocation stack, and a reserved relocation data segment; the relocation tuple data is used to indicate data to be subjected to code relocation; the immediate number relocation stack is used to store address information corresponding to a cross-file call symbol; and the relocation data segment includes a global offset table generated by using a relocation tuple corresponding to a target type in the relocation tuple data. The running module is configured to run the target U-Boot firmware in the memory space and perform a positioning jump according to target information recorded by the target U-Boot firmware, thereby guiding the operating system to start; the target information comprises: a base address corresponding to each target type of a relocation tuple in the modified global offset table, a base address in address information of a symbol corresponding to a cross-file call in the modified immediate number relocation stack, and a base address of a program counter.
9. An electronic device, comprising: Comprise: A processor, a memory, and a storage connected in communication with the processor; The storage stores computer execution instructions; The memory is used for temporarily storing operation data required by the processor to execute the computer execution instructions stored in the storage; The processor executes the computer execution instructions stored in the storage to implement the method of any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer readable storage medium stores computer execution instructions, and the computer execution instructions are executed by the processor to implement the method of any one of claims 1-7.
Citation Information
Patent Citations
Quick start method of X86 flight control computer
CN101515239A
Storage Optimization System
US20100235569A1