Firmware updating method
By adopting USB HID modules and multiple mechanisms in embedded devices, the problem of complex firmware updates in the prior art and dependence on specific drivers is solved, and convenient, general, safe and reliable firmware updates are achieved.
Patent Information
- Application Number
- CN202510145377.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-10
- Publication Date
- 2025-03-11
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The prior art requires specific drivers when firmware updates without disassembling embedded devices, and the operation is complicated, making it difficult to achieve convenient and universal firmware updates.
The USB HID module is used for firmware updates. Through multiple mechanisms such as partitioning memory areas, setting cache areas, and adopting frame checking and whole packet checking, the security and reliability of firmware updates are ensured.
Remote firmware updates without specific drivers are implemented, which improves the convenience and versatility of updates, and ensures the security and reliability of firmware updates.
Smart Images

Figure CN119597322A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of software deployment, and in particular relates to a firmware updating method. Background Art
[0002] Currently, embedded devices mainly update firmware through two channels: One is to burn directly through the program burning port, but after the embedded device is installed, the program burning port will be sealed inside, and the firmware cannot be updated without removing the outer shell. In addition, manual burning requires complex operating steps and technical requirements.
[0003] The second is to perform remote updates through serial ports, networks, USB and other interfaces using standard protocols or manufacturer-defined protocols. It allows developers to remotely update firmware without disassembling embedded devices, which is particularly important for maintaining and upgrading embedded devices.
[0004] Among them, USB interface upgrades usually use the DFU (Download Firmware Update) mode, but the DFU mode usually requires a specific driver. USB HID (Human Interface Device) is a standard device class for USB devices, which defines devices for human-computer interaction. These devices include not only devices with physical human-computer interfaces, but also other devices that comply with the HID category specification. The operating system usually comes with a driver for the HID class, and users do not need to develop additional drivers, and can directly use API system calls to complete communication.
[0005] Therefore, it is necessary to develop a firmware update method based on the USB HID module. Summary of the invention
[0006] In view of the current status and shortcomings of the above-mentioned prior art, an object of the present invention is to provide a firmware updating method.
[0007] A firmware update method comprises the following steps: Step S1, the embedded device is powered on or reset; the embedded device is equipped with a memory, a USB HID module and a processor module, and the embedded device is connected to the PC via the USB HID module; the memory includes a boot loading area, a first storage area and a second storage area; Step S2, determining whether the embedded device needs a firmware upgrade: if a firmware upgrade is required, proceed to the firmware upgrade process of step S3; otherwise, proceed to step S4; Step S3, firmware upgrade process: the PC sends an upgrade instruction to the embedded device through the USB HID module, and the embedded device determines whether to upgrade after determining the version; if it decides to upgrade, the embedded device stores the upgrade data sent by the PC into the second storage area and verifies it; after the verification is passed, the embedded device copies the upgrade data to the first storage area and verifies it again, and reports the upgrade success after success; Step S4, check whether the verification of the first storage area passes; if the verification fails, proceed to the next step; otherwise, jump to the first storage area to run; Step S5, check whether the verification of the second storage area passes; if the verification fails, request the upgrade package from the PC through the USB HID module, and go to step S3; otherwise, copy the data in the second storage area to the first storage area, and go to step S4.
[0008] Further, step S3 includes the following steps: S301: The PC sends a firmware upgrade instruction to the embedded device via the USB HID module; S302: After receiving the firmware upgrade instruction, the embedded device determines whether the program version in the firmware upgrade instruction is consistent with the current program version: if the versions are inconsistent, a confirmation message is replied and the boot loading mode is entered, and the data in the second storage area is erased at the same time; if the versions are consistent, a denial message is replied and the upgrade is terminated; S303: The PC sends the firmware upgrade data through the USB HID module; the embedded device stores the firmware upgrade data into the second storage area by writing into the buffer; S304: After the embedded device completes the acceptance, the firmware upgrade data stored in the second storage area is verified; if the verification passes, step S305 is executed; if the verification fails, the upgrade failure is reported through the USB HID module; S305: The embedded device copies the data in the second storage area to the first storage area, and performs a whole-packet verification on the data in the first storage area; if the verification passes, the upgrade is successful; S306: The embedded device reports a successful upgrade data packet.
[0009] Further, the software upgrade instruction includes the version number information of the data to be upgraded, the upgrade package length, the upgrade package checksum and frame checksum value; Firmware upgrade data, including frame sequence number, data length, upgrade package data and frame check value; The upgrade success data packet contains version number information, upgrade result and frame check value.
[0010] Further, the buffer module includes a write buffer, an allocation agent module and a write-back agent module; The data input end of the write buffer is connected to the distribution agent module; the distribution agent module is connected to the PC end through the USBHID module; the distribution agent module is responsible for managing and controlling the data writing operation; The data output end of the write buffer is connected to the write-back proxy module; the write-back proxy module is connected to the second storage area; the write-back proxy module is responsible for monitoring and managing the modified cache lines in the write buffer, and writing the data back to the second storage area.
[0011] Furthermore, the buffer module includes a read buffer; the read buffer is connected to a first storage area and a second storage area; the read buffer includes a first read buffer and a second read buffer; the first read buffer and the second read buffer use a ping-pong buffering method to achieve continuous reading and writing of data.
[0012] Further, in step S303, the PC waits for the embedded device to enter the boot loading mode successfully, and then starts to send the upgrade data. After receiving the data, the embedded device performs a frame check, and replies with a confirmation frame after the check passes, and stores the data in the second storage area.
[0013] Further, in step S305, this solution adopts a dynamic write-back data judgment solution to continuously monitor the usage status of the read buffer; then calculates a real-time replacement index, and when the read buffer needs to write a new data file and the remaining space in the read buffer is insufficient, the data file corresponding to the replacement index is used as a removal file according to the replacement index; The calculation formula of the replacement index is as follows:
[0014] Among them, replacementIndex is the replacement index; T Ni represents the normalized access time; T i Indicates the access time of the i-th data file; C Ni represents the normalized access capacity; C i represents the access capacity of the i-th data file; λ represents the level weight in the range of [0, 1]; i is the sequence number of the data file, and n is the total number of data files.
[0015] Furthermore, in a read operation, firstly try to read the data file from the read buffer, if the data file can be read, directly read the data file, and then update the replacement index; otherwise, transfer the target data file from the first access area to the read buffer, and then read the data from the read buffer; In a write operation, if the target data files are not in the read buffer and there is enough space in the read buffer, these target data files will be updated to the read buffer; when the remaining space in the read buffer is no longer sufficient, the data files corresponding to the replacement index will be removed according to the replacement index.
[0016] This solution has the following advantages: The USB HID protocol is used to implement remote firmware updates for embedded devices without the need for specific drivers, which improves the convenience and versatility of firmware updates. The invention ensures the security and reliability of firmware updates by dividing storage areas, setting buffer areas, and adopting multiple mechanisms such as frame verification and whole packet verification. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 is a flow chart of the present invention; Figure 2 is a device connection diagram of the present invention; Figure 3 is a flowchart of the firmware upgrade process in step S3; Figure 4 It is a schematic diagram of the structure of an embedded device; Figure 5 It is a schematic diagram of the file access window; Figure 6 It is the write energy consumption diagram of this scheme. DETAILED DESCRIPTION
[0018] The present invention will be further described in detail below in conjunction with the accompanying drawings.
[0019] Terminology explanation: USB (Universal Serial Bus): Universal Serial Bus is a serial bus standard and a technical specification for input and output interfaces. It is used to connect computers with external devices such as printers, scanners, digital cameras, etc. to achieve high-speed data transmission.
[0020] USB HID (Universal Serial Bus-Human Interface Device): Human interface device is a type of USB device. It includes a wide range of devices, such as keyboards, mice, game controllers, etc. These devices communicate with computers through USB interfaces to achieve human-computer interaction. The operating system usually comes with a USB-HID class driver, and users do not need to develop additional drivers.
[0021] BootLoader mode: Boot loader mode, which is responsible for loading and starting the main application when the system is powered on or reset. BootLoader mode provides the system with the necessary hardware initialization and operating environment to ensure that the system can start and run the main application normally.
[0022] Program burning port burning requires the device to be disassembled. Some devices only have USB as the external interface, and the current common DFU mode requires a specific driver. The present invention uses a USB HID module to upgrade the firmware without disassembling the device or requiring a specific driver. After connecting to the PC, the host computer software can be used to upgrade the firmware.
[0023] The present invention mainly solves the problem of updating the firmware of an embedded device without removing the outer shell.
[0024] like Figure 1 As shown, a firmware update method comprises the following steps: Step S1, the embedded device is powered on or reset.
[0025] Step S2, determine whether the embedded device needs a firmware upgrade: if a firmware upgrade is required, enter the firmware upgrade process of step S3; otherwise, enter step S4.
[0026] Step S3, firmware upgrade process; Figure 3 shown.
[0027] S301: The embedded device is connected to the PC via a USB HID module, and the PC sends a firmware upgrade instruction to the embedded device via the USB HID module; Figure 2 shown.
[0028] The firmware upgrade command contains the version number information of the data to be upgraded, the upgrade package length, the upgrade package checksum and frame checksum values, and adopts the following format: like Figure 4 As shown, the embedded device has a built-in memory, a buffer module, a USB HID module and a processor module.
[0029] The memory includes a boot loading area (BootLoad), a first storage area and a second storage area. The memory of this solution adopts flash memory.
[0030] The boot loading area stores programs that run at startup; The first storage area stores application running programs; The second storage area stores the received upgrade data.
[0031] In view of the fast USB speed, the storage area byte erase speed is slow, and the sector erase speed is fast. In the case of large data volume, a cache module is set in the RAM (random access memory). When the received data reaches the memory sector size or other conditions, the cache module data is written into the second storage area or the first storage area in the sector erase mode.
[0032] The buffer module includes a write buffer, an allocation agent module, a write-back agent module, Read the buffer.
[0033] The data input end of the write buffer is connected to the distribution agent module; the distribution agent module is connected to the PC end through the USBHID module. The distribution agent module is responsible for managing and controlling the data writing operation.
[0034] The data output end of the write buffer is connected to the write-back proxy module; the write-back proxy module is connected to the second storage area; the write-back proxy module is responsible for monitoring and managing the modified cache lines in the write buffer to ensure that the data is written back to the second storage area at an appropriate time (such as when the cache line is replaced or the system is idle).
[0035] The read buffer is connected to a first storage area and a second storage area.
[0036] The read buffer includes a first read buffer and a second read buffer. The first read buffer and the second read buffer use a ping-pong buffer to implement continuous reading and writing of data. In a write operation, one read buffer (such as the first read buffer) is used to pack compressed data, and the other read buffer (such as the second read buffer) is used to write the compressed data into a memory. This mechanism ensures real-time read and write performance of data files.
[0037] S302: After receiving the firmware upgrade instruction, the embedded device determines whether the program version in the firmware upgrade instruction is consistent with the current program version: if the versions are inconsistent, a confirmation message is replied and the boot loading mode is entered, and the data in the second storage area is erased at the same time; if the versions are consistent, a denial message is replied and the upgrade is terminated; S303: The PC sends the firmware upgrade data through the USB HID module; the embedded device stores the firmware upgrade data into the second storage area by writing into the buffer; After the PC waits for the embedded device to enter the boot loading mode successfully, it starts to send the upgrade data. After receiving the data, the embedded device performs frame verification, replies with a confirmation frame after the verification passes, and stores the data in the second storage area at the same time.
[0038] Firmware upgrade data, including frame sequence number, data length, upgrade package data and frame check value, can be in the following format: On the PC side, the data is sent to the allocation agent through the USB HID module; the allocation agent transfers the data to the write buffer. The write buffer is used to temporarily store the data received from the USB HID module. When the amount of data in the write buffer reaches or exceeds the size of a sector, the write operation is triggered to write the data in the write buffer to the second storage area in sector erase mode. Clear the cache area or update the pointer (the pointer is used to track the current amount of data in the cache area) to receive new data. The storage area is erased in sectors, which is faster than byte-level erase.
[0039] S304: After the embedded device completes the acceptance, the firmware upgrade data stored in the second storage area is verified; if the verification passes, step S305 is executed; if the verification fails, the upgrade failure is reported through the USB HID module; S305: The embedded device copies the data in the second storage area to the first storage area, and performs a whole-packet verification on the data in the first storage area; if the verification passes, the upgrade is successful; In traditional solutions, when the buffer space is insufficient, the data in the buffer will be written back to the access device, which will generate write-back overhead. The mainstream algorithms for writing back data on the market mainly include LRU and FAB.
[0040] The LRU (Least Recently Used) algorithm is a memory management algorithm that is mainly used to optimize cache management. The core idea of the LRU algorithm is to eliminate the least recently used data, that is, if a piece of data has not been accessed in the recent period of time, it is likely that it will not be frequently used in the future, so it can be removed from the cache to make room for more frequently used data. This strategy helps to improve the cache hit rate and reduce memory waste.
[0041] FAB organizes buffer data in the form of data blocks based on the LRU algorithm. Each block aggregates data pages belonging to the same flash block. When the available space in the buffer is lower than the set threshold, the block with the most data pages will be swapped out of the memory.
[0042] This solution adopts a dynamic write-back data judgment solution to continuously monitor the usage status of the read buffer, including which data files are being used and which data files are idle; then it calculates the real-time replacement index. When the read buffer needs to write a new data file and the remaining space in the read buffer is insufficient, the data file corresponding to the replacement index is removed according to the replacement index.
[0043] The calculation formula of the replacement index is as follows:
[0044] Among them, replacementIndex is the replacement index; TNi represents the normalized access time; T i Indicates the access time of the i-th data file; C Ni represents the normalized access capacity; C i represents the access capacity of the i-th data file; λ represents the level weight in the range of [0, 1]; i is the sequence number of the data file, and n is the total number of data files.
[0045] In the read operation, first try to read the data file from the read buffer. If the data file can be read, read the data file directly and then update the replacement index; otherwise, transfer the target data file from the first access area to the read buffer and then read the data from the read buffer.
[0046] The pseudo code for reading data is as follows: 1. If (target data are in write-buffer) { 2. Read data from write-buffer; 3. Update replacementIndex;} 4. Else { 5. Copy target data from flash memory to read-buffer;} 6. End;} In a write operation, if the target data files are not in the read buffer and there is enough space in the read buffer, these target data files will be updated to the read buffer; when the remaining space in the read buffer is no longer sufficient, the data files corresponding to the replacement index will be removed according to the replacement index.
[0047] The pseudo code for the write operation is as follows: 1. If (target data are in write-buffer) { 2. While (write-buffer has insufficient space) { 3. Replace the file in flash memory according to replacementIndex;} 4. Update data; 5. Update replacementIndex;} 6. Else { 7. While (write-buffer has insufficient space) { 8. Replace file in flash memory according to replacementIndex;} 9. Store data in write-buffer; 10. Record replacementIndex;} 11. End;} Figure 5 This is a schematic diagram of the file access window. Figure 5 In the figure, arrows indicate file sorting, letters A, B, C, D, F, etc. indicate file nouns, and the dotted box is the file access window.
[0048] Use the file access window to observe recent file usage and dynamically adjust the λ value accordingly to achieve better results. Figure 5 As shown in the figure, the files saved in the file access window will be frequently re-accessed within a period of time, so λ=0.1 is used to solve the temporal locality problem in the system. On the contrary, if the files in the file access window maintain a certain order, the system will solve the spatial locality problem by λ=0.9. Otherwise, this solution will focus on both temporal locality and spatial locality, and λ=0.5 at this time.
[0049] Evaluate the energy consumption of LRU, FAB and this solution, such as Figure 6 Normalized energy consumption shown. Energy consumption of performing 1200 write operations for 40 data sizes ranging from 20KB to 1.5MB. The first read buffer and the second read buffer are both 4MB. Since the energy consumption of page read is about 8% of the energy consumption of page write, read operations are ignored in the evaluation. Figure 6 In the figure, the Y-axis is the normalized write operation energy consumption, and the X-axis is the number of write operations. Overall, the energy consumption of this scheme is 15% lower than that of LRU and 35% lower than that of FAB. Specifically, when the number of write operations is less than 600, the energy consumption of LRU is higher than that of FAB, because the access behavior of LRU is based on spatial locality, so it will replace data more frequently than FAB. Therefore, during this period, this scheme will focus on spatial locality. After 700 write operations, the write-back cost of FAB will be much higher than that of LRU, because the access behavior of FAB is based on temporal locality. Therefore, this scheme can adjust λ after 700 operations to adapt to temporal locality.
[0050] S306: The embedded device reports a successful upgrade data packet.
[0051] The upgrade success data packet contains version number information, upgrade result and frame check value, and can be in the following format: Step S4, check whether the verification of the first storage area passes; if the verification fails, go to the next step; otherwise, jump to the first storage area to run.
[0052] Step S5, check whether the verification of the second storage area passes; if the verification fails, request the upgrade package from the PC through the USB HID module, and go to step S3; otherwise, copy the data in the second storage area to the first storage area, and go to step S4.
[0053] The present invention realizes remote firmware update of embedded devices by adopting USB HID protocol, without the need for specific drivers, thus improving the convenience and versatility of update. At the same time, the security and reliability of firmware update are ensured by dividing storage area, setting buffer area, adopting frame check and whole packet check and other multiple mechanisms. In addition, the present invention also provides detailed data format and security and reliability considerations, so that the PC end can effectively interact with the embedded device to ensure reliable data storage and reading speed.
[0054] In terms of reliable data storage: 1. Each frame of data communication has a checksum response to ensure the reliability of the data received by the device; 2. After the data is successfully received, it is stored in the second storage area. The first storage area will be overwritten only after the entire firmware verification passes; 3. When copying data to the first storage area, the data in the second storage area will not be erased or written to ensure the reliability of the firmware upgrade; 4. After the device is started, there is also a verification process for the storage area data in BootLoader mode to ensure that the program can run normally.
[0055] In terms of read speed, this solution sets a buffer. The energy consumption and delay of the buffer are less than the storage area, and the buffer can be updated in situ. Therefore, updating the data in the buffer reduces energy consumption, reduces write operations, and allows more erase operations. Among them: 1. For unidirectional data transmission, this solution sets a write buffer, and slows down the write buffer by allocating proxy modules and write proxy modules, thereby increasing the write speed of the second storage area. The write buffer adopts a first-in-first-out management strategy.
[0056] 2. For bidirectional data transmission, this solution connects the first storage area and the second storage area with a read buffer, and performs dynamic write-back data judgment based on the replacement index, and uses the data file corresponding to the replacement index as the removal file, thereby reducing the write-back overhead. This solution is not only applicable to data transmission between the first storage area and the second storage area, but also to data transmission between the first storage area and the second storage area and the outside world.
[0057] This solution is another mode: when the efficiency of data transmission is not important, the buffer module can be deleted, the second storage area is directly connected to the PC through the USB HID module, and the first storage area and the second storage area are also directly connected. This mode is also within the protection scope of this solution.
[0058] It is understandable that those skilled in the art can make equivalent substitutions or changes based on the technical solution and inventive concept of the present invention, and all these changes or substitutions should fall within the protection scope of the claims attached to the present invention.
Claims
1. A firmware update method, characterized in that: It includes the following steps: Step S1, power on or reset the embedded device; An embedded device is provided with a memory, a USB HID module and a processor module, and the embedded device is connected to a PC via the USB HID module; the memory includes a boot loading area, a first storage area and a second storage area; Step S2, determining whether the embedded device needs a firmware upgrade: if a firmware upgrade is required, proceeding to the firmware upgrade process of step S3; Otherwise, proceed to step S4; Step S3, firmware upgrade process: the PC sends an upgrade instruction to the embedded device through the USB HID module, and the embedded device determines whether to upgrade after determining the version; If it is decided to upgrade, the embedded device stores the upgrade data sent by the PC into the second storage area and verifies it; After the verification is passed, the embedded device copies the upgrade data to the first storage area and verifies it again, and reports the upgrade success after success; Step S4, checking whether the verification of the first storage area passes; If the verification fails, go to the next step; Otherwise, jump to the first storage area to run; Step S5, checking whether the verification of the second storage area passes; If the verification fails, the upgrade package is requested from the PC via the USB HID module, and the process goes to step S3; Otherwise, the data in the second storage area is copied to the first storage area, and the process goes to step S4.
2. A firmware update method according to claim 1, characterized in that: Step S3 includes the following steps: S301: The PC sends a firmware upgrade instruction to the embedded device via the USB HID module; S302: After receiving the firmware upgrade instruction, the embedded device determines whether the program version in the firmware upgrade instruction is consistent with the current program version: if the versions are inconsistent, a confirmation message is replied and the boot loading mode is entered, and the data in the second storage area is erased at the same time; if the versions are consistent, a denial message is replied and the upgrade is terminated; S303: The PC sends the firmware upgrade data through the USB HID module; the embedded device stores the firmware upgrade data into the second storage area by writing into the buffer; S304: After the embedded device completes the acceptance, the firmware upgrade data stored in the second storage area is verified; if the verification passes, step S305 is executed; if the verification fails, the upgrade failure is reported through the USB HID module; S305: The embedded device copies the data in the second storage area to the first storage area, and performs a whole-packet verification on the data in the first storage area; if the verification passes, the upgrade is successful; S306: The embedded device reports a successful upgrade data packet.
3. A firmware update method according to claim 2, characterized in that: Firmware upgrade instructions, including the version number information of the data to be upgraded, the upgrade package length, the upgrade package checksum and frame checksum value; Firmware upgrade data, including frame sequence number, data length, upgrade package data and frame check value; The upgrade success data packet contains version number information, upgrade result and frame check value.
4. A firmware update method according to claim 2, characterized in that: The buffer module includes a write buffer, an allocation agent module and a write-back agent module; The data input end of the write buffer is connected to the distribution agent module; the distribution agent module is connected to the PC end through the USB HID module; the distribution agent module is responsible for managing and controlling the data writing operation; The data output end of the write buffer is connected to the write-back proxy module; the write-back proxy module is connected to the second storage area; the write-back proxy module is responsible for monitoring and managing the modified cache lines in the write buffer, and writing the data back to the second storage area.
5. A firmware update method according to claim 2 or 4, characterized in that: The buffer module includes a read buffer; the read buffer is connected to a first storage area and a second storage area; the read buffer includes a first read buffer and a second read buffer; the first read buffer and the second read buffer use a ping-pong buffer to achieve continuous reading and writing of data.
6. A firmware update method according to claim 2 or 3, characterized in that: In step S303, the PC waits for the embedded device to enter the boot loading mode successfully, and then starts to send the upgrade data. After receiving the data, the embedded device performs frame verification, and replies with a confirmation frame after the verification passes, and stores the data in the second storage area.
7. A firmware update method according to claim 2, characterized in that: In step S305, this solution adopts a dynamic write-back data judgment solution to continuously monitor the usage status of the read buffer; then calculates a real-time replacement index, and when the read buffer needs to write a new data file and the remaining space in the read buffer is insufficient, the data file corresponding to the replacement index is used as a removal file according to the replacement index; The calculation formula of the replacement index is as follows: ; Among them, replacementIndex is the replacement index; T Ni represents the normalized access time; T i Indicates the access time of the i-th data file; C Ni represents the normalized access capacity; C i represents the access capacity of the i-th data file; λ represents the level weight in the range of [0, 1]; i is the sequence number of the data file, and n is the total number of data files.
8. A firmware update method according to claim 7, characterized in that: In the read operation, first try to read the data file from the read buffer, if the data file can be read, directly read the data file, and then update the replacement index; otherwise, transfer the target data file from the first access area to the read buffer, and then read the data from the read buffer; In a write operation, if the target data files are not in the read buffer and there is enough space in the read buffer, these target data files will be updated to the read buffer; when the remaining space in the read buffer is no longer sufficient, the data files corresponding to the replacement index will be removed according to the replacement index.
Citation Information
Patent Citations
Software upgrading method based on USB
CN109857434A
Enhanced OTA upgrading method based on Internet of Things
CN110333891A
Dish-washing machine software burning method, device and equipment
CN111522562A
Method for upgrading firmware based on chip usbhid, terminal equipment and computer readable storage medium
CN112947978A