Damage-proof embedded online upgrading method and system
By employing a multi-partition structure and CPIO format packet verification in embedded systems, the security and reliability issues of embedded online upgrade schemes are resolved, enabling automatic device recovery and efficient upgrades while reducing hardware resource requirements.
Patent Information
- Application Number
- CN202511865005.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-11
- Publication Date
- 2026-04-17
AI Technical Summary
Existing embedded online upgrade solutions suffer from insufficient security and reliability during the upgrade process, imperfect system recovery mechanisms, complex configuration and maintenance, high hardware resource consumption, or reliance on external services.
It adopts a multi-partition structure for a peer-to-peer system based on flash, utilizes a ring data buffer and CPIO format packet verification, and combines it with application layered writing technology to ensure that upgrade operations are performed on inactive spare partitions, and improves security through a layered verification mechanism.
It enables automatic device recovery and efficient upgrades during the upgrade process, reduces hardware resource requirements, improves security and upgrade efficiency, and simplifies system maintenance.
Smart Images

Figure CN121879812A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of embedded systems, and in particular to a method and system for preventing damage during online upgrades of embedded systems. Background Technology
[0002] With the increasing intelligence and networking of embedded devices, the demand for over-the-air (OTA) software upgrades in embedded systems (especially those based on real-time operating systems, RTOS) is growing. Traditional embedded system upgrade methods, such as local programming via wired connections like serial ports or USB, are not only cumbersome and inefficient, but also fail to meet the needs of remote maintenance and feature updates after device deployment. Furthermore, these methods heavily rely on the bootloader, which is fundamental to starting the operating system and is a small program that runs before the operating system kernel or user programs. This program initializes hardware devices and establishes a memory space mapping, bringing the system's hardware and software environment to a suitable state to prepare for the final call.
[0003] Currently, existing embedded online upgrade solutions mainly include the following types.
[0004] 1. Single-system upgrade solution: This solution has only one system image area. During the upgrade, the new firmware package needs to be downloaded to additional storage space first, and then verified and updated during the Bootloader stage. This method has obvious drawbacks: if an unexpected situation such as a power outage occurs during the upgrade process, the system image may be corrupted, causing the device to become unusable and unable to boot normally; at the same time, the upgrade process depends on the Bootloader, whose logic is complex and difficult to maintain, and requires additional storage space to temporarily store OTA upgrade files, increasing hardware costs.
[0005] 2. System solution with Recovery mode: This solution includes a separate, streamlined Recovery system in addition to the main system. Upgrades are performed within the Recovery system. While this solution improves reliability to some extent, its system configuration is complex, requiring the maintenance of two completely independent system environments (the main system and the Recovery system), leading to increased software development and maintenance workload, and relatively high consumption of storage resources (such as Flash).
[0006] 3. Other network upgrade solutions: Some solutions, although they adopt network download methods, rely on FTP, proprietary protocols, etc., which are insufficient in terms of security and universality; or they use public cloud servers for firmware distribution, which does not meet the requirements of some application scenarios with high requirements for data privacy and deployment autonomy.
[0007] In summary, existing technologies generally suffer from insufficient security and reliability during the upgrade process, imperfect system recovery mechanisms, complex configuration and maintenance, and high hardware resource consumption or reliance on external services. Therefore, there is an urgent need for an embedded online upgrade solution that can balance security and reliability, low resource consumption, ease of maintenance, and autonomous control. Summary of the Invention
[0008] In view of the above, the present invention proposes a damage-resistant embedded online upgrade method and system to solve at least one of the problems in the above-mentioned technologies.
[0009] This invention is achieved through the following technical solution. Firstly, it provides a damage-resistant embedded online upgrade method based on a multi-partition structure of a flash-based peer-to-peer system, wherein the partitions are completely equivalent and there are at least two partitions. The method includes the following steps: S10: Read the partition status to obtain the running partition status data and the upgrade partition status data, and create a circular data cache in flash; S21: If the running partition status data is normal, then read the executable file from the running partition and complete the normal program execution; S22: After downloading the data to be upgraded to the circular data buffer according to an upgrade instruction, the data in the circular data buffer is written into the upgrade partition, and after the data to be upgraded is completely written into the upgrade partition, the status of the upgrade partition is marked as pending update; S30: Restart the system according to the update mark, read the executable file from the updated upgrade partition according to the updated upgrade partition status, and complete the upgraded program execution.
[0010] Furthermore, in step S21, when the running partition status data is abnormal, the following steps are performed: Switch the running partition, mark the original upgrade partition as the running partition, and mark the original running partition as the upgrade partition. Read the executable file from the running partition to complete the normal program operation.
[0011] Further, step S22 includes: S221: Obtain an HTTP request with Range header information according to the upgrade instruction, and divide the data to be upgraded into multiple upgrade packages of the same size according to a preset byte offset. S222: The upgrade package is downloaded to the circular data buffer in sequence, and the circular data buffer writes the downloaded data package to the upgrade partition. After writing, the blocks in the circular data buffer are erased. S223: After the last packet of the data to be upgraded is written to the upgrade partition, update the partition information and mark the status of the upgrade partition as pending update.
[0012] Furthermore, the upgrade package is in CPIO format, wherein the components in the CPIO format are stored hierarchically.
[0013] Furthermore, before step S222 downloading the upgrade package to the circular data buffer, the method further includes: The components of the CPIO format package are subjected to layered verification. When a verification fails, the abnormal value and the abnormal layer data are returned, and the verification is re-downloaded until the verification is normal.
[0014] Furthermore, the storage space size of the ring-shaped data cache is the same as the size of the upgrade package.
[0015] On the other hand, the present invention also provides a damage-resistant embedded online upgrade system, based on a flash-based peer-to-peer multi-partition structure, wherein the partitions are completely peer-to-peer and there are at least two partitions. The upgrade system includes: System initialization unit: used to read partition status to obtain running partition status data and upgrade partition status data, and create a circular data cache in flash; Bootbloader unit: If the running partition status data is normal, it reads the executable file from the running partition and completes normal program execution; Upgrade partition update unit: After downloading the data to be upgraded to the circular data buffer according to an upgrade instruction, it writes the data in the circular data buffer into the upgrade partition, and after the data to be upgraded is completely written into the upgrade partition, it marks the status of the upgrade partition as pending update; System restart unit: Used to restart the system according to the update flag, read the executable file from the updated upgrade partition according to the updated upgrade partition status, and complete the execution of the upgraded program.
[0016] Furthermore, it also includes: Rollback unit: Used to switch the running partition and mark the original upgrade partition as the running partition when the running partition status data is abnormal.
[0017] Furthermore, the upgrade partition update unit includes: Packet splitting subunit: used to obtain an HTTP request with Range header information according to the upgrade instruction, and to split the data to be upgraded into multiple upgrade packages of the same size according to a preset byte offset. Streaming upgrade subunit: sequentially downloads the upgrade package to the circular data buffer, while the circular data buffer writes the downloaded data package to the upgrade partition, and erases the blocks in the circular data buffer after writing; Status update subunit: used to update partition information and mark the status of the upgrade partition as pending update after the last packet of the data to be upgraded is written to the upgrade partition.
[0018] Furthermore, the upgrade partition update unit also includes: Layered verification subunit: Used to perform layered verification on components in CPIO format packages. When a verification fails, the abnormal value and the abnormal layer data are returned, and the verification is re-downloaded until the verification is normal.
[0019] The anti-damage embedded online upgrade system of the present invention adopts multi-system partitioning, and the anti-damage embedded online upgrade method adopts core technologies such as CPIO format packet verification and application layered writing. The combination of the two brings the following beneficial effects: 1. Upgrade operations always target inactive backup partitions, ensuring the integrity of the main system partition remains unaffected. The bootloader only selects healthy partitions to execute main system tasks, giving the system industrial-grade upgrade fault tolerance. Even if an unexpected power outage occurs during upgrade file download or writing, the device can automatically and seamlessly restore to its pre-upgrade normal operating state after restarting.
[0020] 2. By employing a packet-based streaming write mechanism, the large-volume upgrade task is broken down into consecutive 1024-byte packets for processing. Parallel download and write pipelines ensure constant memory usage. This allows the solution to be easily deployed on low-cost embedded MCUs with extremely limited resources, significantly lowering the hardware barrier to implementing secure OTA functionality.
[0021] 3. The upgrade efficiency is significantly improved, and the service interruption time is shortened. The application layer directly calls the manufacturer's underlying erase and write functions, eliminating intermediate steps such as file system conversion, which improves the efficiency of writing data to Flash. HTTP download and Flash write operations are performed in parallel, eliminating the serial waiting time of "first download, then write" in the traditional solution. The total time is close to the slower of the two.
[0022] 4. By using the CPIO file format as a carrier, layered and modular security verification is achieved. Digital signature verification of the kernel and hash verification of the file system are possible. This refined "defense-in-depth" strategy offers far greater security than traditional methods that perform one-time verification of a single BIN file.
[0023] To better understand and implement this invention, the following detailed description is provided in conjunction with the accompanying drawings. Attached Figure Description
[0024] Figure 1 This is a structural block diagram of a damage-resistant embedded online upgrade system according to an embodiment of the present invention; Figure 2 For execution Figure 1 The flowchart illustrates the embedded online upgrade method for preventing damage to the system shown. Figure 3 This is a structural block diagram of a damage-resistant embedded online upgrade system according to a preferred embodiment of the present invention; Figure 4 For execution Figure 3 The flowchart shows a method for preventing damage through embedded online upgrades in the system shown. Detailed Implementation
[0025] Existing recovery solutions that can prevent damage rely on a dual-system architecture, where the storage of the two systems is independent. Therefore, if either system fails, the other system can repair it. However, because switching between the two systems is required, the architecture is very complex and consumes a lot of storage space.
[0026] This invention attempts to move all the complex upgrade logic (version checking, network communication, file verification, upgrade package parsing, and write scheduling) to the RTOS application layer, while the Bootloader's responsibilities are simplified to only the most basic partition switching and startup. Therefore, the complexity of the underlying system development and maintenance costs are greatly simplified in terms of architecture.
[0027] Specifically, the system employs a multi-partition structure based on flash memory, where the partitions are completely equivalent and there are at least two partitions. Please refer to [link to relevant documentation]. Figure 1 This invention provides a damage-resistant embedded online upgrade system, comprising: a system initialization unit 10, a bootloader unit 21, an upgrade partition update unit 22, and a system restart unit 30. The system initialization unit 10 and the bootloader unit 21 initialize the system state, select the corresponding partition to read the executable file based on the partition states of multiple partitions, and complete normal program execution. The upgrade partition update unit 22 downloads firmware information from the local HTTP server to the web client in parallel according to the upgrade instructions, relying on the data buffer created during system initialization to complete the background update of the embedded system. After the update is complete, the partition state is updated, and the system is restarted to complete the online upgrade. For details, please refer to [link to relevant documentation]. Figure 2 In a damage-resistant embedded online upgrade system, the working process of each component includes: Subsequently, when the embedded system begins to perform the upgrade, the system initialization unit 10 is used to execute step S10: read the partition status to obtain the running partition status data and the upgrade partition status data, and create a ring-shaped data buffer in the flash.
[0028] In this invention, the embedded system pre-partitions the flash memory to construct a multi-partition structure. This application uses two partitions, partition A and partition B, as an example. These two partitions are completely equivalent, and a structure is defined to manage the partition states. For example, / / Partition state management structure: typedef struct { int active_partition; / / The active partition number, for example: 0=A, 1=B int update_staged; / / Flag: 1 = Updates are pending. char firmware_ver
[32] ; / / Firmware version number uint32_t crc32; / / Integrity check value } ota_partition_info_t The information stored inside is the partition status, which mainly includes: - `active_partition`: Whether the currently running system is A or B. This is the most important state, telling the bootloader which system to start next.
[0029] - `update_staged`: Whether there is an update ready and waiting to be activated. After the main system successfully writes the new firmware to the alternate partition, it sets this flag to tell the bootloader: "Try switching partitions on the next boot."
[0030] - `firmware_ver`: The firmware version number for the corresponding partition.
[0031] - `crc32` / `hash`: Corresponds to the integrity check value of the firmware partition. Based on this, the upgrade task is executed. After reading the running partition status data, partition A is designated as the running partition, and partition B as the upgrade partition. At this time, a data buffer is simultaneously created. This data buffer can be used for data recovery or firmware upgrade. Simultaneously, partitions A and B are connected, and data is transferred according to specific instructions.
[0032] Bootbloader unit 21 is used to execute step S21: if the running partition status data is normal, then read the executable file from the running partition and complete the normal program operation.
[0033] In other words, the bootloader is only responsible for partition switching and startup, and does not participate in other operations, thus greatly reducing the complexity of the build and the maintenance cost.
[0034] In addition, to prevent the program from failing to start due to damage to the stored data of the running partition caused by abnormal operation of the embedded system, a rollback unit is also set in the Bootloader unit 21. This unit is used to switch the running partition and mark the original upgrade partition as the running partition when the running partition status data is abnormal.
[0035] At this point, the rollback unit checks the health status of the partitions. If the health status of the running partition A is abnormal, partition B is set as the running partition, and partition A is used as a backup partition for subsequent updates or repairs. This automatically avoids damaged partitions and fundamentally prevents the device from failing to boot. The bootloader's startup logic at this time is as follows: / / Bootloader startup logic pseudocode int bootloader_main(void) { ota_partition_info_t* part_info = (ota_partition_info_t*)PARTITION_INFO_ADDR; / / Check if the primary partition is healthy if (check_partition_healthy(part_info, PARTITION_A)) { jump_to_address(PARTITION_A_ADDR); / / Start the primary partition } else { / / If the primary partition fails, automatically roll back to the backup partition. jump_to_address(PARTITION_B_ADDR); / / Start the alternate partition } return 0; } The upgrade partition update unit 22 is used to perform step S22: after downloading the data to be upgraded to the circular data buffer according to an upgrade instruction, the data in the circular data buffer is written into the upgrade partition, and after the data to be upgraded is completely written into the upgrade partition, the status of the upgrade partition is marked as pending update.
[0036] That is, while executing the main system tasks in running partition A, the system upgrade is performed in upgrade partition B. First, all data is downloaded to the circular data buffer generated by the system initialization unit, and then downloaded to the upgrade partition. During the download process, all data is stored in the circular data buffer. Even if the network is interrupted, it will not affect running partition A and upgrade partition B, thus further improving the robustness of the system.
[0037] In another embodiment, please refer to Figure 3 and Figure 4 Considering that writing all data to the circular data buffer would result in excessive memory consumption, which would be limiting in embedded systems with limited hardware resources, the upgrade partition update unit 22 further includes: The HTTP request packet subunit 221 is used to perform step S221: obtain an HTTP request with Range header information according to the upgrade instruction, and divide the data to be upgraded into packets according to a preset byte offset to obtain multiple upgrade packets with the same data size.
[0038] The HTTP request uses a Range header, requesting the firmware in segments of fixed size (1024 bytes) instead of downloading the entire firmware at once. During production, an upgrade package is generated and transmitted to the circular data buffer. The size of this upgrade package can be dynamically adjusted or fixed to other sizes based on the network MTU, flash erase / write sector size, or the optimal protocol solution. The size of the circular data buffer is consistent with the size of the upgrade package.
[0039] The streaming write subunit 222 is used to perform step S222: sequentially downloading the upgrade package to the circular data buffer, while the circular data buffer writes the downloaded data package to the upgrade partition, and erases the blocks in the circular data buffer after writing.
[0040] The data packet is retrieved from the circular buffer, and the chip manufacturer's underlying Flash programming function is immediately invoked to write the data to the specified address in the upgrade partition (B area).
[0041] The HTTP request packet sub-unit 221 and the streaming write unit 222 run concurrently under the scheduling of the operating system. Download tasks do not need to wait for all upgrade packages to finish downloading, nor do write tasks wait for all packages to be downloaded before starting. They coordinate through buffers to achieve streaming writes. On the one hand, RAM usage is reduced from the MB level to the KB level, enabling high-end OTA functions to be implemented on a very low-cost MCU. On the other hand, download and write operations run in parallel, shortening the total upgrade time from T(download) + T(write) to close to Max(T(download), T(write)), significantly reducing the upgrade time window. It also naturally supports breakpoint resumption: based on HTTPRange requests, downloading and writing can resume from the point of interruption after a network interruption, improving the upgrade success rate in weak network environments.
[0042] The status update subunit 223 is used to execute step S223: after the last packet of the data to be upgraded is written to the upgrade partition, the partition information is updated and the status of the upgrade partition is marked as pending update.
[0043] At this time, the state of area B is set to 1, and the state of area A is set to 0.
[0044] Preferably, the upgrade package is in CPIO format, wherein the components in the CPIO format are stored hierarchically. See also... Figure 3 and Figure 4 Furthermore, the upgrade partition update unit 22 also includes a hierarchical verification subunit 224, used to: before downloading the upgrade package to the circular data cache area: The components of the CPIO format package are subjected to layered verification. When a verification fails, the abnormal value and the abnormal layer data are returned, and the verification is re-downloaded until the verification is normal.
[0045] By leveraging CPIO's ability to encapsulate multiple files, a "defense-in-depth" security system was constructed, addressing the challenge that traditional solutions typically only perform a one-time global verification of the entire firmware package. This invention, however, allows for independent, layered, and categorized fine-grained verification of different components within the CPIO package (such as the system kernel, root file system, configuration files, digital signature files, version manifests, etc.).
[0046] The implementation of a multi-layered security strategy, encompassing "core kernel signature verification, file system hash verification, and configuration syntax lookup," significantly enhances the ability to resist malicious tampering and transmission errors. Furthermore, when verification fails, the specific faulty component can be precisely located, avoiding a "one-size-fits-all" approach where a single non-core file issue leads to the rejection of the entire firmware package, thus improving the intelligence and reliability of the upgrade process.
[0047] Here, other archive formats that support file indexing and packaging can be used, such as TAR, ZIP, or custom binary package formats, as long as the format allows multiple independent files or data blocks to be contained within a single package and can be independently extracted and verified, thus falling within the protection scope of this invention. Furthermore, the verification algorithm is not limited to SHA256 or digital signatures. Depending on the security level requirements, other cryptographic hash functions (such as SHA-3, SM3) or symmetric / asymmetric encryption algorithms can be used for signature verification. Its core lies in utilizing the characteristics of structured packages to perform a more refined security check than a general verification.
[0048] The system restart subunit 30 is used to execute step S30: restart the system according to the update mark, read the executable file from the updated upgrade partition according to the updated upgrade partition status, and complete the upgraded program execution.
[0049] Based on the partition status information, the Bootbloader unit selects partition B as the main system to boot, while partition A can perform internal system updates offline through the circular buffer to maintain the synchronization status of partitions A and B.
[0050] The anti-damage embedded online upgrade system of the present invention adopts multi-system partitioning, and the anti-damage embedded online upgrade method adopts core technologies such as CPIO format packet verification and application layered writing. The combination of the two brings the following beneficial effects: 1. Upgrade operations always target inactive backup partitions, ensuring the integrity of the main system partition remains unaffected. The bootloader only selects healthy partitions to execute main system tasks, giving the system industrial-grade upgrade fault tolerance. Even if an unexpected power outage occurs during upgrade file download or writing, the device can automatically and seamlessly restore to its pre-upgrade normal operating state after restarting.
[0051] 2. By employing a packet-based streaming write mechanism, the large-volume upgrade task is broken down into consecutive 1024-byte packets for processing. Parallel download and write pipelines ensure constant memory usage. This allows the solution to be easily deployed on low-cost embedded MCUs with extremely limited resources, significantly lowering the hardware barrier to implementing secure OTA functionality.
[0052] 3. The upgrade efficiency is significantly improved, and the service interruption time is shortened. The application layer directly calls the manufacturer's underlying erase and write functions, eliminating intermediate steps such as file system conversion, which improves the efficiency of writing data to Flash. HTTP download and Flash write operations are performed in parallel, eliminating the serial waiting time of "first download, then write" in the traditional solution. The total time is close to the slower of the two.
[0053] 4. By using the CPIO file format as a carrier, layered and modular security verification is achieved. Digital signature verification of the kernel and hash verification of the file system are possible. This refined "defense-in-depth" strategy offers far greater security than traditional methods that perform one-time verification of a single BIN file.
[0054] The embodiments described above are merely examples of several implementations of the present invention, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and the present invention also intends to include these modifications and variations.
Claims
1. A damage-resistant embedded online upgrade method, characterized in that, A multi-partition structure for a peer-to-peer system based on flash memory, wherein the partitions are completely equivalent and there are at least two partitions; The online upgrade method includes the following steps: S10: Read the partition status to obtain the running partition status data and the upgrade partition status data, and create a circular data cache in flash; S21: If the running partition status data is normal, then read the executable file from the running partition and complete the normal program execution; S22: After downloading the data to be upgraded to the circular data buffer according to an upgrade instruction, the data in the circular data buffer is written into the upgrade partition, and after the data to be upgraded is completely written into the upgrade partition, the status of the upgrade partition is marked as pending update; S30: Restart the system according to the update mark, read the executable file from the updated upgrade partition according to the updated upgrade partition status, and complete the upgraded program execution.
2. The anti-damage embedded online upgrade method according to claim 1, characterized in that, In step S21, when the running partition status data is abnormal, the following steps are performed: Switch the running partition, mark the original upgrade partition as the running partition, and mark the original running partition as the upgrade partition. Read the executable file from the running partition to complete the normal program operation.
3. The anti-damage embedded online upgrade method according to claim 2, characterized in that, Step S22 includes: S221: Obtain an HTTP request with Range header information according to the upgrade instruction, and divide the data to be upgraded into multiple upgrade packages of the same size according to a preset byte offset. S222: The upgrade package is downloaded to the circular data buffer in sequence, and the circular data buffer writes the downloaded data package to the upgrade partition. After writing, the blocks in the circular data buffer are erased. S223: After the last packet of the data to be upgraded is written to the upgrade partition, update the partition information and mark the status of the upgrade partition as pending update.
4. The anti-damage embedded online upgrade method according to claim 3, characterized in that: The upgrade package is in CPIO format, wherein the components in the CPIO format are stored in layers.
5. The anti-damage embedded online upgrade method according to claim 4, characterized in that, Before step S222, which downloads the upgrade package to the circular data buffer, the method further includes: The components of the CPIO format package are subjected to layered verification. When a verification fails, the abnormal value and the abnormal layer data are returned, and the verification is re-downloaded until the verification is normal.
6. The method for preventing damage to an embedded online upgrade system according to claim 5, characterized in that: The storage space size of the ring-shaped data cache area is the same as the size of the upgrade package.
7. A damage-resistant embedded online upgrade system, characterized in that, A multi-partition structure for a flash-based peer-to-peer system, wherein the partitions are completely symmetrical, and there are at least two partitions, including: System initialization unit: used to read partition status to obtain running partition status data and upgrade partition status data, and create a circular data cache in flash; Bootbloader unit: If the running partition status data is normal, it reads the executable file from the running partition and completes normal program execution; Upgrade partition update unit: After downloading the data to be upgraded to the circular data buffer according to an upgrade instruction, it writes the data in the circular data buffer into the upgrade partition, and after the data to be upgraded is completely written into the upgrade partition, it marks the status of the upgrade partition as pending update; System restart unit: Used to restart the system according to the update flag, read the executable file from the updated upgrade partition according to the updated upgrade partition status, and complete the execution of the upgraded program.
8. The damage-resistant embedded online upgrade system according to claim 7, characterized in that, Also includes: Rollback unit: Used to switch the running partition and mark the original upgrade partition as the running partition when the running partition status data is abnormal.
9. The anti-damage embedded online upgrade system according to claim 8, characterized in that, The upgrade partition update unit includes: Packet splitting subunit: used to obtain an HTTP request with Range header information according to the upgrade instruction, and to split the data to be upgraded into multiple upgrade packages of the same size according to a preset byte offset. Streaming upgrade subunit: sequentially downloads the upgrade package to the circular data buffer, while the circular data buffer writes the downloaded data package to the upgrade partition, and erases the blocks in the circular data buffer after writing; Status update subunit: used to update partition information and mark the status of the upgrade partition as pending update after the last packet of the data to be upgraded is written to the upgrade partition.
10. The anti-damage embedded online upgrade system according to claim 9, characterized in that: The upgrade partition update unit also includes: Layered verification subunit: Used to perform layered verification on components in CPIO format packages. When a verification fails, the abnormal value and the abnormal layer data are returned, and the verification is re-downloaded until the verification is normal.
