1. Field of the Invention
The invention relates to patching devices for patching ROM code, methods for patching ROM code, and electronic devices utilizing the same.
2. Description of the Related Art
Read-only memory (ROM) is usually used in electronic devices to store firmware. Because data stored in ROM cannot be modified, ROM malfunctions when there is an error in the firmware.
To overcome the problem, conventional techniques use a flash ROM to replace ROM. Flash ROM is non-volatile memory that can be electronically erased and reprogrammed.
However, flash ROM is much more expensive than ROM. For cost down, techniques capable of patching ROM code are required.
The invention provides patching devices for patching ROM code. The patching device comprises a storage unit, a comparing unit, a branch instruction generator and a multiplexer. The storage unit stores a source address and a destination address indicating the start of a flawed ROM function and the start of a patch function, respectively. The comparing unit compares an access address from a processor with the source address stored in the storage unit. The branch instruction generator generates a branch instruction for jumping from the access address to the destination address. The multiplexer has a first input terminal receiving an accessed instruction according to the access address, a second input terminal receiving the branch instruction, an output terminal coupled to the processor, and a control terminal receiving the comparison result of the comparing unit. The multiplexer outputs the accessed instruction when the access address does not match the source address, and outputs the branch instruction when the access address matches the source address.
The invention further discloses electronic devices comprising a ROM, a reprogrammable memory, a processor, and the aforementioned patching device. A patch function is stored to the reprogrammable memory by the processor to replace a flawed function in the ROM. The processor outputs an access address and accesses the ROM or the reprogrammable memory according to the access address. When detecting that the processor is going to access the flawed ROM function, the patching device provides the processor with a branch instruction to jump to the reprogrammable memory to access the patch function.
The invention further discloses methods for patching ROM code. The method stores the address of a flawed ROM function as a source address and stores the address of a patch function as a destination address. When the code in the ROM is processed, the method compares an access address outputted from a processor with the source address. When the access address matches the source address, the method generates a branch instruction for the processor to jump to the destination address and sends the branch instruction to the processor; otherwise an accessed instruction according to the access address is sent to the processor.
In another embodiment of the electronic device, the ROM stores a first function starting from a first address, the reprogrammable memory stores a second function starting from a second address, the patching device couples to the ROM and the reprogrammable memory, and the processor couples to the patching device. The patching device receives directive information from the processor and determines whether the processor is going to fetch the first function, and generates and returns a branch instruction to the processor when the processor is going to fetch the first function. After receiving the branch instruction, the processor executes the branch instruction to cause an unconditional jump to the second address and subsequently returns the second function.
The above and other advantages will become more apparent with reference to the following description taken in conjunction with the accompanying drawings.
The invention can be more fully understood by reading the subsequent detailed description and examples with references made to the accompanying drawings, wherein:
The following description shows some embodiments carrying out the invention. This description is made for the purpose of illustrating the general principles of the invention and should not be taken in a limiting sense. The scope of the invention is best determined by reference to the appended claims.
Referring to
Based on the comparison result of the comparing unit 112, the multiplexer 116 sends the accessed instruction 118 or the branch instruction 120 to the processor 108. When the comparison result is that the access address matches one of the source addresses (meaning that the processor 108 is going to access an instruction of one of the flawed ROM functions), the multiplexer 116 is controlled to output the branch instruction 120 instead of the access instruction 118 to the processor 108, so that the program execution omits the flawed ROM function and jump to the corresponding patch function. When the comparison result is that the access address does not match any of the source addresses (meaning that the processor is going to access one of the correct ROM functions or one of the patch functions), the multiplexer 116 outputs the accessed instruction 118 to the processor 108.
After receiving the access address, to generate the branch instruction 120, the branch instruction generator 114 accesses the storage unit to retrieve the destination address mapped by the access address (or matched source address) and calculates the offset between the destination address and the access address. The format of the branch instruction 120 may be shown in
In some embodiments, a portion of a reprogrammable memory is allocated as the patch code region 106. Electronic devices (such as shown in
The invention further discloses methods for patching ROM code.
In some embodiments of the invention, the step of generating the branch instruction (S308) further comprises calculating the offset between the destination address and the access (source) address and encapsulating the calculated offset in the branch instruction.
Details of the embodiments of the system and method illustrated in
Beginning from instruction of address A, the processor 108 fetches an instruction from the address A. After receiving directive information from the processor 108, the patching device 102 determines that the address A does not match any of source addresses stored in the storage unit 110 and returns the branch-with-link instruction of the address A to the processor 108. During execution of the returned branch instruction, the processor 108 first pushes an address of A+4 (i.e. increments the program counter by 4) into a stack or “link” register, calculates a jump address B according to the returned branch instruction, updates the program counter with the address B and directs the patching device 102 to return an instruction from the address B (arrow 402). After receiving the directive information from the processor 108, the patching device 102 determines that the address B matches a source address stored in the storage unit 110, retrieves the corresponding destination address C, generates a branch instruction containing the offset between the source address B and the destination address C and returns the generated branch instruction to the processor 108, where the branch instruction will cause a unconditional jump to the address C, (arrow 404). During execution of the returned branch instruction, the processor 108 calculates a jump address C according to the returned branch instruction, updates the program counter thereof with the address C and directs the patching device 102 to return an instruction from the address C (arrow 406). It is to be understood that, other than the execution of the branch-with-link instruction, the processor 108 does not push any address into the stack or “link” register as a return address during execution of the generated branch instruction. After receiving the directive information from the processor 108, the patching device 102 determines that the address C does not match any of source addresses stored in the storage unit 110 and returns an instruction of address C. After that, instructions of the patch function are sequentially compared with the source addresses by the patching device 102 and it is determined that the instructions do not match any of the source addresses. Thus, instructions of the patch function are fetched and executed by the processor 108 in sequence until the end of the patch function (i.e. a return instruction). During execution of the return instruction, the processor 108 pops out of the stack or “link” register to acquire the address A+4 and directs the patching device 102 to return an instruction from the address A+4 (arrow 408). Details of the subsequent instruction execution may be deduced by the analogy. It is to be observed that, with the disclosed embodiments of system and method, the flawed ROM function starting from address B is patched by the patch function starting from the instruction of address C.
While the invention has been described by way of example and in terms of preferred embodiment, it is to be understood that the invention is not limited thereto. To the contrary, it is intended to cover various modifications and similar arrangements (as would be apparent to those skilled in the art). Therefore, the scope of the appended claims should be accorded to the broadest interpretation so as to encompass all such modifications and similar arrangements.