ROM-based modifiable firmware generation method and device and storage medium

By inserting the pile driving module for the objective function in the ROM, modifyable firmware is generated, which solves the problem that the program cannot be modified after the ROM is solidified, and the remediation of the objective function and flexible modification of the patch function are achieved.

CN120029649AInactive Publication Date: 2025-05-23沐曦科技(成都)有限公司
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202510503454.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-22
Publication Date
2025-05-23
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

The read-only attribute of the ROM causes the program to be unable to be modified after solidification, which makes it difficult to upgrade software or fix vulnerabilities during the development and maintenance of embedded systems.

Method used

通过为每个待固化的目标函数插入打桩模块,生成基于ROM的可修改固件。打桩模块在目标函数被调用时,检查可读写存储器中的存储地址是否存在补丁函数,若存在则跳转执行补丁函数。

Benefits of technology

The objective function cured in ROM can be remediated by patch functions when problems occur, which solves the problem that cannot be modified after curing, and increases the flexibility of modifying the patch function.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120029649A_ABST
    Figure CN120029649A_ABST
Patent Text Reader

Abstract

The invention relates to the field of chip design, in particular to a ROM (Read Only Memory)-based modifiable firmware generation method, ROM-based modifiable firmware generation equipment and a storage medium, which are characterized in that N target functions with piling marks are acquired, piling processing is performed through a code injection unit, a piling source code is compiled to obtain a static library, and the static library is solidified into an ROM to generate firmware. Wherein the step of piling processing comprises the substeps of inserting a piling module into each objective function; when the ith target function is called, the piling module in the ith target function is used for judging whether the patch function of the ith target function in the storage address is null or not by searching the pointer, and if not, skipping to the address of the patch function in the read-write memory to start execution, and returning an execution result. The technical problem that the objective function cannot be modified when going wrong is solved. The storage address pointed by the target function is the address in the read-write memory, so that the modification flexibility of the patch function is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of chip design, and in particular to a method, device and storage medium for generating modifiable firmware based on ROM. Background Art

[0002] In the field of embedded systems, resource constraints are a common and critical issue. Due to the small processing power and memory capacity, system designers often need to find a balance between cost and performance. In this case, ROM (Read-Only Memory) becomes an attractive storage solution. ROM has the significant advantage of low cost, and due to its read-only feature, the data will not be modified due to accidental operation or system failure after being written, which brings higher reliability and stability to the system.

[0003] However, the read-only property of ROM also brings an unavoidable problem. Once the program is written into ROM and solidified, it cannot be modified. In the development and maintenance of embedded systems, it is often necessary to update the program, fix vulnerabilities, or optimize algorithms. Due to the unmodifiable nature of ROM, the system faces great difficulties when software upgrades are required. How to solve the problem that the program cannot be modified after solidification while enjoying the advantages of ROM's low cost and high stability has become a technical problem that needs to be solved urgently. Summary of the invention

[0004] In view of the above technical problems, the technical solution adopted by the present invention is: a method for generating a modifiable firmware based on ROM, the method comprising the following steps: S100, obtaining source code, where the source code includes N target functions with stubbing marks, each target function being a function to be solidified into a ROM and to be inserted into a stubbing module.

[0005] S200, the code injection unit performs a piling process to obtain a source code after piling; the steps of the piling process include: S210, scanning all source codes, and obtaining all the target functions according to the stubbing marks.

[0006] S220, defining a function pointer type according to the type of each target function.

[0007] S230, defining a global function table type according to the function pointer type, wherein the global function table type includes N member variables, each of the member variables is of the function pointer type and is used to point to the target function.

[0008] S240, specifying a storage address of the global function table type, and configuring a search pointer, wherein the search pointer points to a storage address of a read-write memory.

[0009] S250, inserting a pile driving module into each target function according to the search pointer; wherein, when the i-th target function is called, the pile driving module in the i-th target function is used to implement the following steps: accessing the patch function of the i-th target function in the storage address through the search pointer to see if it is empty; if not, jumping to the address of the patch function in the read-write memory to start execution, and returning the execution result.

[0010] S300, compile the stubbed source code to generate target code, obtain a static library, and fix the static library into the ROM to generate firmware.

[0011] In addition, the present invention also provides a non-transitory computer-readable storage medium, in which at least one instruction or at least one program is stored, and the at least one instruction or the at least one program is loaded and executed by a processor to implement the above method.

[0012] In addition, the present invention also provides an electronic device, including a processor and the above-mentioned non-transitory computer-readable storage medium.

[0013] The present invention has at least the following beneficial effects: The embodiment of the present invention provides a method, device and storage medium for generating modifiable firmware based on ROM, which is solidified into ROM by inserting a pile driving module for each target function to be solidified. The pile driving module can determine whether there is a patch function at the storage address in the specified read-write memory when the corresponding target function is called, and if so, jump to the patch function. The technical problem that the target function cannot be modified when there is a problem has been solved. In addition, since the storage address pointed to by the target function is an address in the read-write memory, the patch function can be added, deleted or modified according to demand, which increases the modification flexibility of the patch function. BRIEF DESCRIPTION OF THE DRAWINGS

[0014] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0015] Figure 1 A flow chart of a method for generating a modifiable firmware based on ROM provided by an embodiment of the present invention; Figure 2A flowchart of the steps of the piling process provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0016] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.

[0017] Unless otherwise defined, all technical and scientific terms used in the embodiments of the present invention have the same meanings as commonly understood by those skilled in the art.

[0018] See also Figure 1 , which shows a flow chart of a method for generating a modifiable firmware based on ROM, the method comprising the following steps: S100, obtaining source code, where the source code includes N target functions with stubbing marks, each target function being a function to be solidified into a ROM and to be inserted into a stubbing module.

[0019] In one implementation, the ROM (Read-Only Memory) is a mask ROM, a programmable ROM, or an erasable programmable ROM. Other types of ROMs also fall within the protection scope of the present invention.

[0020] Among them, once the code is fixed to the ROM, it cannot be modified. The ROM stores fixed information and data that are not easy to change.

[0021] In one embodiment, the source code stored in the ROM is an operating system and device drivers, and may also include a software timer and an asynchronous process that depend on the operating system. Other source codes stored in the ROM also fall within the protection scope of the present invention.

[0022] In one embodiment, the target function is a function that can be called by an application, wherein the application is stored in a readable and writable memory. After the target function is solidified into the ROM, if there is a problem with the target function, it will appear that the application has called a target function with a problem and cannot be executed. Since the target function cannot be modified, and there may be a mutual calling relationship between the various functional modules in the ROM, the entire program in the ROM cannot be remedied. In order to prevent such a situation from occurring, the method provided in the embodiment of the present invention is to remedy the target program with a problem in the ROM by patching.

[0023] In one embodiment, the source code is the entire project file, and other types of source code including the target function also fall within the protection scope of the present invention.

[0024] In one embodiment, the stubbing mark is a mark attached to the definition of the target function, which is used to indicate that the current target function is stubbing by the code injection unit. Wherein, the code injection unit is a script program, and other types of functions that are stubbing before compilation fall within the protection scope of the present invention.

[0025] In one embodiment, the piling mark is a designated number or a designated character, and other types of piling marks used to mark the piling modules to be inserted also fall within the protection scope of the present invention.

[0026] S200, the code injection unit performs a stubbing process to obtain a stubbing source code.

[0027] For further information, see Figure 2 , the steps of the piling process include: S210, scanning all source codes, and obtaining all the target functions according to the stubbing marks.

[0028] In one embodiment, a regular expression is used to match function definitions in all source codes, and a check is made to see whether the function definitions contain stub marks; when the stub marks are contained, the target function is obtained. Other methods of identifying the target function through stub marks fall within the protection scope of the present invention.

[0029] S220, defining a function pointer type according to the type of each target function.

[0030] In one embodiment, the step of defining the function pointer type includes: obtaining basic information of the target function, the basic information including the return value type, the number of parameters and the parameter type; and defining the function pointer type of the target function according to the basic information.

[0031] S230, defining a global function table type according to the function pointer type, wherein the global function table type includes N member variables, each of the member variables is of the function pointer type and is used to point to the target function.

[0032] In one implementation, the global function table type is a structure type.

[0033] The global function table type includes a series of members, each of which is a function pointer pointing to a target function, and the function pointer type is the function pointer type defined for each target function in S220. The global function table type is intended to allow different target functions to be called dynamically at runtime, and the functions pointed to by these function pointers are accessed by function names.

[0034] In one implementation, the naming of member variables follows a unified naming convention to facilitate reference in the code.

[0035] As an example, when the target function is send_msg, first define the function pointer type of this target function: int ( send_msg_ptr)( char, int, int). Then define the global function table type: structglobal_func_tb1{send_msg_ptr send_msg;}. Of course, when there are N target functions, it is necessary to define N function pointer types, and the global function table type includes N members. Here, only one send_msg function is used as the target function for example.

[0036] S240, specifying a storage address of the global function table type, and configuring a search pointer, wherein the search pointer points to a storage address of a read-write memory.

[0037] It should be noted that the global function table type is stored in a preset storage address, and the global function table type is searched through a search pointer.

[0038] S250, inserting a pile driving module into each target function according to the search pointer; wherein, when the i-th target function is called, the pile driving module in the i-th target function is used to implement the following steps: accessing the patch function of the i-th target function in the storage address through the search pointer to see if it is empty; if not, jumping to the address of the patch function in the read-write memory to start execution, and returning the execution result.

[0039] In one embodiment, after the piling module is inserted, the piling mark is modified to a piled mark.

[0040] In one embodiment, when the i-th target function is called, the step implemented by the pile driving module in the i-th target function also includes: accessing the patch function of the i-th target function in the storage address through the search pointer to see if it is empty, if it is empty, skipping the pile driving module and executing the i-th target function normally. If it needs to be explained, if there is no patch function, it means that the current target function can be called normally. If it cannot be called normally, the patch function of the target function that cannot be called will be rewritten, stored in a readable memory, and the patch function will be added to the storage address to restore the normal call of the target function.

[0041] In one embodiment, the code of the stubbing module is inserted before the first instruction in the i-th target function. When the application calls the i-th target function, if it finds that the patch function already exists, it skips the current function and directly jumps to the patch function for execution, and returns the execution result, avoiding the fatal problem that the program cannot be executed due to an error in calling the i-th target function, thereby achieving the purpose of remedying the i-th target function and allowing the program to execute normally.

[0042] S300, compile the stubbed source code to generate target code, obtain a static library, and fix the static library into the ROM.

[0043] In one embodiment, the configuration condition of the patch function is: when the application calls the target function in the static library, if an error occurs, a patch function is configured in the read-write memory for the target function that has an error. Alternatively, when the system is upgraded, a patch function is actively configured for the target function in the read-write memory. Other conditions that require the patch function to be configured in the read-write memory also fall within the protection scope of the present invention.

[0044] In one embodiment, the configuration step of the patch function includes: configuring patch functions of n target functions in the read-write memory; defining a search variable of a global function table type, and placing the search variable into the storage address; initializing the search variable to n member variables, and configuring the corresponding address of the patch function for each member variable, wherein the name of the member variable is the same as the name of the target function.

[0045] In one implementation, when a patch function of a target function is newly added in the read-write memory, a member variable for initializing a search variable is added to the storage address and an address of a patch function is newly added for the added member variable.

[0046] In one embodiment, when a patch function is added, after fixing the static library to the ROM, the process further includes: compiling the patch function to obtain a target code of the patch function, and linking the target code of the patch function with the target code of the static library to obtain an updated firmware.

[0047] After the program is solidified into ROM, even if an error occurs or an upgrade is required, the pointer can be searched to check whether there is a patch function in the address pointed to by the function pointer. If so, the patch function is executed and the current target function is no longer executed; otherwise, the current target function is executed, so as to achieve the purpose of remedying the problem through the patch function when there is a problem with the solidified target function.

[0048] In summary, the embodiment of the present invention provides a method for generating a modifiable firmware based on ROM, which is solidified into ROM by inserting a pile driving module for each target function to be solidified, and the pile driving module can make the corresponding target function be called, and judge whether there is a patch function at the storage address in the specified read-write memory, if there is, jump to the patch function, otherwise do not jump. The technical problem that the target function cannot be modified when there is a problem has been solved. In addition, since the storage address pointed to by the target function is an address in the read-write memory, the patch function can be added, deleted or modified according to demand, which increases the modification flexibility of the patch function.

[0049] An embodiment of the present invention also provides a non-transitory computer-readable storage medium, which can be set in an electronic device to store at least one instruction or at least one program related to implementing a method in a method embodiment. The at least one instruction or the at least one program is loaded and executed by the processor to implement the method provided in the above embodiment.

[0050] An embodiment of the present invention further provides an electronic device, comprising a processor and the aforementioned non-transitory computer-readable storage medium.

[0051] An embodiment of the present invention further provides a computer program product, which includes program code. When the program product is run on an electronic device, the program code is used to enable the electronic device to execute the steps of the method according to various exemplary embodiments of the present invention described above in this specification.

[0052] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.

[0053] Although some specific embodiments of the present invention have been described in detail by way of example, it should be understood by those skilled in the art that the above examples are for illustration only and are not intended to limit the scope of the present invention. It should also be understood by those skilled in the art that various modifications may be made to the embodiments without departing from the scope and spirit of the present invention. The scope of the present invention is defined by the appended claims.

Claims

1. A method for generating a modifiable firmware based on ROM, characterized in that: The method comprises the following steps: S100, obtaining source code, wherein the source code includes N target functions with stubbing marks, each target function being a function to be solidified into a ROM and to be inserted into a stubbing module; S200, the code injection unit performs a stubbing process to obtain a stubbing source code; The steps of the piling process include: S210, scanning all source codes, and obtaining all the target functions according to the stubbing marks; S220, defining a function pointer type according to the type of each target function; S230, defining a global function table type according to the function pointer type, wherein the global function table type includes N member variables, each of which is of the function pointer type and is used to point to the target function; S240, specifying a storage address of the global function table type and configuring a search pointer, wherein the search pointer points to a storage address of a read-write memory; S250, inserting a stubbing module into each target function according to the search pointer; wherein, when the i-th target function is called, the stubbing module in the i-th target function is used to implement the following steps: accessing the patch function of the i-th target function in the storage address through the search pointer to see if it is empty, and if not, jumping to the address of the patch function in the read-write memory to start execution, and returning the execution result; S300, compile the stubbed source code to generate target code, obtain a static library, and fix the static library into the ROM to generate firmware.

2. The method according to claim 1, characterized in that When the i-th target function is called, the steps implemented by the piling module in the i-th target function also include: accessing the patch function of the i-th target function in the storage address through the search pointer to see if it is empty; if so, skipping the piling module and executing the i-th target function normally.

3. The method according to claim 1, characterized in that The configuration condition of the patch function is: when the application calls the target function in the static library, if an error occurs, a patch function is configured in the read-write memory for the target function with the error.

4. The method according to claim 1, characterized in that: The configuration steps of the patch function include: configuring patch functions of n target functions in the read-write memory; defining a search variable of a global function table type, and placing the search variable into the storage address; initializing the search variable to n member variables, and configuring the corresponding address of the patch function for each member variable, wherein the name of the member variable is the same as the name of the target function.

5. The method according to claim 4, characterized in that When a patch function of a target function is newly added in the read-write memory, a member variable for initializing a search variable and an address of a patch function are newly added for the added member variable are added in the storage address.

6. The method according to claim 5, characterized in that When a patch function is added, after the static library is solidified into the ROM, the following steps are further performed: compiling the patch function to obtain a target code of the patch function, and linking the target code of the patch function with the target code of the static library to obtain an updated firmware.

7. The method according to claim 1, characterized in that S250 further includes: after the piling module is inserted, modifying the piling mark to a piling completed mark.

8. The method according to claim 1, characterized in that S220 also includes: obtaining basic information of the target function, the basic information including a return value type, a number of parameters, and a parameter type; and defining a function pointer type of the target function according to the basic information.

9. A non-transitory computer-readable storage medium, wherein at least one instruction or at least one program is stored in the storage medium, characterized in that: The at least one instruction or the at least one program is loaded and executed by the processor to implement the method according to any one of claims 1 to 8.

10. An electronic device, characterized in that: The invention comprises a processor and the non-transitory computer-readable storage medium as claimed in claim 9.

Citation Information

Patent Citations

  • Systems and methods for providing patchable ROM firmware

    CN110945475A

  • Patch program compiling method, program repairing method and related equipment

    CN113504934A

  • Method and device for automatically generating patch code, electronic equipment and storage medium

    CN116775090A

  • Method and System for Generating a ROM Patch

    US20160019054A1