Methods for upgrading firmware, electronic devices
By backing up critical partitions and updating the partition table during firmware upgrades, the problems of encrypted user data not being retained and upgrades being unreliable in existing technologies are solved, ensuring the reliability of the upgrade process and data integrity.
Patent Information
- Application Number
- CN202411306806.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-19
- Publication Date
- 2026-01-06
- Estimated Expiration
- 2044-09-19
AI Technical Summary
Existing technologies cannot retain data in encrypted user data partitions during the upgrade process, and cannot guarantee the reliability of the upgrade process, especially in the event of unexpected situations such as power outages, which may brick the device.
By reading upgrade instruction information from the bootloader firmware, determining command line parameters, and backing up the boot communication partition, metadata partition, bootloader firmware partition, recovery mode firmware partition, and temporary partition at different stages, and updating the partition table, it is ensured that encrypted user data can still be retained when the address space changes, and that the backup partition data is not destroyed in the event of an unexpected power failure.
This ensures that the device will not be bricked even if there is an unexpected power outage during the upgrade process, and guarantees that the data in the encrypted user data partition will still be retained after the upgrade, thus improving the reliability of firmware upgrades.
Smart Images

Figure CN119179505B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of embedded device technology, and in particular to methods for upgrading firmware and electronic devices. Background Technology
[0002] Existing solutions that support firmware partition table upgrades via Over-The-Air (OTA) typically cannot retain data in the user data partition and cannot guarantee complete reliability during the upgrade process; that is, an unexpected event at any point during the upgrade process can brick the device. Some solutions, while ensuring that an unexpected event at any point during the upgrade process will not brick the device and can retain data in the user data partition, require that the data in the user data partition be unencrypted. However, in many high-security scenarios, the device's user data partition (e.g., 'data') is usually encrypted. Summary of the Invention
[0003] This invention provides a method and electronic device for upgrading firmware, which can improve the reliability of firmware upgrades and retain the data in the encrypted user data partition after the upgrade.
[0004] In one aspect of the present invention, a method for upgrading firmware is provided. The method includes: reading upgrade instruction information from a first preset location in the bootloader firmware of a device, used for storing upgrade instructions; in a first stage where the upgrade instruction information corresponds to the first preset upgrade instruction, determining command line parameters based on the partition start address of the firmware storage area to be upgraded, the size of a preset temporary storage partition, the initial size of the data to be backed up, the header size of the data to be backed up, and the start address of a preset encrypted user data partition, and transmitting the command line parameters to the recovery mode firmware, wherein the data to be backed up includes a bootloader communication partition, a metadata partition, a bootloader firmware partition, and a recovery mode firmware partition; in a second stage where the upgrade instruction information corresponds to a second preset upgrade instruction, after backing up the data to be backed up and the temporary storage partition, updating the current partition table so that the bootloader in the current partition table... The program firmware partition, the recovery mode firmware partition, the metadata partition, the temporary storage partition, and the boot communication partition each point to their corresponding backup partitions, and the updated current partition table is upgraded to the address space where the primary partition table and the backup partition table are located in the storage medium; in the third stage corresponding to the upgrade instruction information and the third preset upgrade instruction, the boot program firmware partition and the recovery mode firmware partition are upgraded, and when the address space of the metadata partition is different in the current partition table and the new partition table, the metadata partition firmware in the backup area of the metadata partition is written to the address space where the metadata partition is located in the new partition table; and in the fourth stage corresponding to the upgrade instruction information and the fourth preset upgrade instruction, other firmware except for the boot program firmware and the recovery mode firmware are upgraded.
[0005] In another aspect of the invention, an electronic device is provided. The electronic device includes a memory configured to store an executable program; and a processor configured to execute the program to perform the aforementioned method for upgrading firmware.
[0006] According to the present invention, data, including the metadata partition, is backed up according to the first preset upgrade instruction and the second preset upgrade instruction. When the address space of the metadata partition is different in the current partition table and the new partition table, the metadata partition firmware in the metadata partition backup area is written to the address space of the metadata partition in the new partition table according to the third preset upgrade instruction. In this way, even if the address space of the metadata partition where the user data partition encryption key is stored changes before and after the upgrade, the data in the user data partition can still be retained after the upgrade. Since the boot communication partition, metadata partition, boot program firmware partition, recovery mode firmware partition and temporary storage partition have been backed up, the data in the backup partition will not be destroyed even if an unexpected power failure occurs during the upgrade process. This ensures that any unexpected event (such as a power failure) during the upgrade process will not brick the device, thereby improving the reliability of the firmware upgrade. The data in the encrypted user data partition is still retained after the upgrade. Attached Figure Description
[0007] Figure 1 This is a flowchart illustrating the steps of a firmware upgrade method according to an embodiment of the present invention.
[0008] Figure 2 This is a flowchart illustrating a method for upgrading firmware according to an embodiment of the present invention;
[0009] Figure 3 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0010] To explain in detail the technical content, objectives, and effects of the present invention, the following description is provided in conjunction with the embodiments and accompanying drawings.
[0011] While existing technologies can ensure that no unexpected event occurs during the upgrade process and that the data in the user data partition can be bricked, they require that the data in the user data partition be unencrypted. However, in many scenarios with high security requirements, the user data partition of the device is usually encrypted. In this case, existing technologies cannot retain the data in the user data partition after the upgrade and cannot guarantee the complete reliability of the upgrade process.
[0012] To address at least the aforementioned technical problems, this disclosure provides a method for upgrading firmware. According to embodiments of this disclosure, data, including a metadata partition, is backed up according to a first preset upgrade instruction and a second preset upgrade instruction. When the address spaces of the metadata partition differ between the current partition table and the new partition table, the metadata partition firmware from the backup metadata partition is written to the address space of the metadata partition in the new partition table according to a third preset upgrade instruction. In this way, even if the address space of the metadata partition containing the user data partition encryption key changes before and after the upgrade, the data in the user data partition can be retained after the upgrade. Furthermore, since the boot communication partition, metadata partition, bootloader firmware partition, recovery mode firmware partition, and temporary storage partition have been backed up, even an unexpected power outage during the upgrade process will not damage the data in the backup partitions. This ensures that any unexpected event (such as a power outage) during the upgrade process will not brick the device, thereby improving the reliability of the firmware upgrade, and the encrypted user data partition data is still retained after the upgrade.
[0013] In the following, the technical solutions according to this disclosure will be described with reference to specific embodiments and in conjunction with the accompanying drawings.
[0014] Figure 1 This is a flowchart illustrating a method 100 for upgrading firmware according to an embodiment of the present disclosure. (Refer to...) Figure 1 The method 100 includes the following steps 102 to 110.
[0015] In step 102, the upgrade instruction information in the first preset location used to store upgrade instructions in the boot communication partition of the device is read from the device's boot boot firmware.
[0016] In step 104, in the first stage corresponding to the upgrade instruction information and the first preset upgrade instruction, command line parameters are determined based on the partition start address of the firmware storage area to be upgraded, the size of the preset temporary storage partition, the initial size of the data to be backed up, the header size of the data to be backed up, and the start address of the preset encrypted user data partition. The command line parameters are then passed to the recovery mode firmware. The data to be backed up includes the boot communication partition, the metadata partition, the boot loader firmware partition, and the recovery mode firmware partition.
[0017] In some embodiments, a firmware partition table is obtained from the address space of a device storage partition table, and this firmware partition table is used as the current partition table; the first partition after the preset partition list in the current partition table is used as the firmware storage area to be upgraded, which is not a preset encrypted user data partition. The preset partition list includes the bootloader firmware partition, the recovery mode firmware partition, the boot communication partition, the metadata partition, and the temporary storage partition; the starting address of the partition of the firmware storage area to be upgraded is written into a command-line parameter; the size of the preset temporary storage partition is obtained from the current partition table, and the size of the preset temporary storage partition is written into the command-line parameter; the size of the boot communication partition, the size of the metadata partition, and the size of the bootloader firmware are obtained from the current partition table respectively. The initial size of the data to be backed up is obtained by adding the sizes of the partitions and the recovery mode firmware partition, the boot communication partition, the metadata partition, the bootloader firmware partition, and the recovery mode firmware partition; the initial size of the data to be backed up is written into the command line parameters; the header size of the data to be backed up is obtained by adding the preset size of the boot communication partition backup header, the preset size of the metadata partition backup header, the preset size of the bootloader firmware partition backup header, and the preset size of the recovery mode firmware partition backup header, and the header size of the data to be backed up is written into the command line parameters; and the starting address of the preset encrypted user data partition is obtained from the current partition table and written into the command line parameters.
[0018] In this way, the starting address of the firmware storage area to be upgraded, the size of the preset temporary storage partition, the initial size of the data to be backed up, the header size of the data to be backed up, and the starting address of the preset encrypted user data partition are written into the command line parameters for subsequent data backup.
[0019] In step 106, in the second stage corresponding to the upgrade instruction information and the second preset upgrade instruction, after backing up the data to be backed up and the temporary partition, the current partition table is updated so that the bootloader firmware partition, the recovery mode firmware partition, the metadata partition, the temporary partition and the boot communication partition in the current partition table point to the corresponding backup partitions, and the updated current partition table is upgraded to the address space where the main partition table and the backup partition table are located in the storage medium.
[0020] In some embodiments, the firmware partition table is obtained from the address space of the device storage partition table and used as the current partition table; the partition table file is obtained from the firmware storage area to be upgraded based on the metadata of the firmware storage area to be upgraded and used as the new partition table; the initial value of the flag indicating that the current partition table points to the backup area is set to false; it is determined whether the backup of all backup areas, including the bootloader firmware partition backup area, recovery mode firmware partition backup area, metadata partition backup area, temporary storage partition backup area, and boot communication partition backup area, has been completed, and whether the bootloader firmware partition, recovery mode firmware partition, metadata partition, temporary storage partition, and boot communication partition in the current partition table all point to their respective backup partitions; if all are true, then... The flag indicating that the current partition table points to the backup area is set to true; it is determined whether the flag indicating that the current partition table points to the backup area is true; if true, a third preset upgrade instruction is written to the first preset location of the boot communication partition; and if not true, the bootloader firmware partition, the recovery mode firmware partition, the metadata partition, the temporary storage partition, and the boot communication partition are backed up, and the current partition table is updated so that the bootloader firmware partition, the recovery mode firmware partition, the metadata partition, the temporary storage partition, and the boot communication partition in the current partition table point to the corresponding backup partitions, and the updated current partition table is upgraded to the address space where the primary partition table and the backup partition table are located in the storage medium.
[0021] In this way, determining whether a backup is complete by marking the current partition table as pointing to the backup area is simpler and more effective. Since the encryption key for the encrypted user data partition is stored in the metadata area, after backing up the bootloader firmware partition, recovery mode firmware partition, metadata partition, temporary storage partition, and boot communication partition, the current partition table is updated so that the bootloader firmware partition, recovery mode firmware partition, metadata partition, temporary storage partition, and boot communication partition in the current partition table point to the corresponding backup partition. When the address space of the metadata area changes before and after the upgrade (such as when the partition start address of the metadata area changes), the data in the encrypted user data partition can still be retained after the upgrade.
[0022] In some embodiments, the starting address and partition size of the recovery mode firmware partition are obtained from the new partition table and used as the starting address and partition size of the new recovery mode firmware; free space metadata is read from the third preset position of the boot communication partition, and it is determined whether the magic number in the free space metadata is a preset magic number; if not, the firmware upgrade process is exited; if so, it is determined whether the address obtained by adding the starting address of the new recovery mode firmware and the partition size of the new recovery mode firmware is greater than the starting address of the free space in the free space metadata; if greater, the firmware upgrade process is exited; if not greater, the bootloader firmware partition is backed up to the bootloader firmware partition backup area. The system then writes the bootloader firmware partition backup area metadata to the fourth preset position of the boot communication partition in the current partition table; backs up the recovery mode firmware partition to the recovery mode firmware partition backup area, and writes the recovery mode firmware partition backup area metadata to the fifth preset position of the boot communication partition in the current partition table; it determines whether the address space of the metadata partition is the same in the current partition table and the new partition table; if they are different, it backs up the metadata partition to the metadata partition backup area, and writes the metadata partition backup area metadata to the sixth preset position of the boot communication partition in the current partition table; it then determines whether the address space of the temporary storage partition is the same in the current partition table and the new partition table; if they are different... If the address spaces of the boot communication partition in the current partition table and the new partition table are the same, the temporary partition is backed up to the temporary partition backup area, and the temporary partition backup area metadata is written to the seventh preset position of the boot communication partition in the current partition table; it is determined whether the address spaces of the boot communication partition in the current partition table and the new partition table are the same; if they are different, the boot communication partition is backed up to the boot communication partition backup area, and the boot communication partition backup area metadata is written to the eighth preset position of the boot communication partition in the current partition table, and the boot communication partition backup area metadata is written to the preset position in the boot communication partition firmware in the boot communication partition backup area for storing the boot communication partition backup area metadata; the boot boot program firmware partition in the current partition table is... The starting address is set to the bootloader firmware starting address in the bootloader firmware partition backup header; the starting address of the recovery mode firmware partition in the current partition table is set to the recovery mode firmware starting address in the recovery mode firmware partition backup header; when the address space of the metadata partition is different in the current partition table and the new partition table, the starting address of the metadata partition in the current partition table is set to the metadata partition firmware starting address in the metadata partition backup header; when the address space of the temporary partition is different in the current partition table and the new partition table, the starting address of the temporary partition in the current partition table is set to the temporary partition firmware starting address in the temporary partition backup header.When the address spaces of the boot communication partition differ between the current partition table and the new partition table, the starting address of the boot communication partition in the current partition table is set to the starting address of the boot communication partition firmware in the boot communication partition backup header; the updated free space metadata is written to the third preset position of the boot communication partition in the updated current partition table, and the updated current partition table is upgraded to the address space of the primary partition table and the backup partition table in the storage medium; and the third preset upgrade instruction is written to the first preset position of the boot communication partition.
[0023] In this way, since the bootloader firmware partition, recovery mode firmware partition, metadata partition, temporary partition, and boot communication partition in the current partition table all point to the corresponding backup partitions, and the firmware storage area to be upgraded and all backup partitions are located after the bootloader firmware partition, recovery mode firmware partition, boot communication partition, metadata partition, and temporary partition in the old and new partition tables, the data in the backup partitions will not be destroyed even if an unexpected power failure occurs during the subsequent upgrade process. The upgrade can continue after power is restored, thereby improving the reliability of firmware upgrades.
[0024] In step 108, in the third stage corresponding to the upgrade instruction information and the third preset upgrade instruction, the bootloader firmware partition and the recovery mode firmware partition are upgraded. When the address space of the metadata partition is different in the current partition table and the new partition table, the metadata partition firmware in the metadata partition backup area is written to the address space where the metadata partition is located in the new partition table.
[0025] In some embodiments, a firmware partition table is obtained from the address space of the device storage partition table and used as the current partition table; a partition table file is obtained from the firmware storage area to be upgraded based on the metadata of the firmware storage area to be upgraded and used as a new partition table; if the current partition table and the new partition table are different, bootloader firmware image file data is obtained from the firmware storage area to be upgraded based on the metadata of the firmware storage area to be upgraded, and the bootloader firmware image file data is written into the address space of the bootloader firmware partition in the new partition table; recovery mode firmware image file data is obtained from the firmware storage area to be upgraded based on the metadata of the firmware storage area to be upgraded, and the recovery mode firmware image file data is written into the address space of the recovery mode firmware partition in the new partition table; when the address space of the metadata partition is different in the current partition table and the new partition table, the metadata partition firmware in the metadata partition backup area is written into the address space of the metadata partition in the new partition table; when the address space of the temporary partition is different in the current partition table and the new partition table, the metadata partition firmware in the metadata partition backup area is written into the address space of the metadata partition in the new partition table; when the address space of the temporary partition is different in the current partition table and the new partition table, the firmware in the temporary partition is written into the address space of the temporary partition. If the addresses of the temporary partitions in the new partition table and the current partition table are different, determine whether the size of the temporary partition in the new partition table is greater than or equal to the size of the temporary partition in the current partition table. If so, write the temporary partition firmware in the backup area of the temporary partition into the address space of the temporary partition in the new partition table. If the address spaces of the boot communication partition in the current partition table and the new partition table are different, write all the boot communication partition data in the current partition table into the address space of the boot communication partition in the new partition table. Upgrade the new partition table to the address space of the primary partition table and the address space of the backup partition table in the storage medium, and use the new partition table as the current partition table. If the current partition table and the new partition table are the same, determine whether there is a valid block list description file backup area metadata in the boot communication partition of the current partition table, and whether there is a valid block list description file backup header in the block list description file backup area. If both exist, write the fifth preset upgrade instruction into the first preset position of the boot communication partition. If not both exist, write the fourth preset upgrade instruction into the first preset position of the boot communication partition.
[0026] In this way, when upgrading the bootloader firmware partition and recovery mode firmware partition, if the address space of the metadata partition is different in the current partition table and the new partition table, the metadata partition firmware in the metadata partition backup area is written to the address space where the metadata partition is located in the new partition table, so as to retain the data in the encrypted user data partition after the upgrade.
[0027] In step 110, in the fourth stage corresponding to the upgrade instruction information and the fourth preset upgrade instruction, the firmware other than the bootloader firmware and the recovery mode firmware are upgraded.
[0028] In some embodiments, a firmware partition table is obtained from the address space of a device storage partition table and used as the current partition table; metadata of the firmware storage area to be upgraded is read from the ninth preset position in the boot communication partition of the current partition table, and it is determined whether the magic number in the metadata of the firmware storage area to be upgraded is a preset magic number; if not, the firmware upgrade process is exited; if yes, an upgrade image header of a preset size is read from the starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded; it is determined whether the magic number of the upgrade image header is a preset magic number; if not, a sixth preset upgrade instruction is written to the first preset position in the boot communication partition; if it is a preset magic number, the firmware image file data excluding the bootloader and the recovery mode are sequentially obtained from the firmware storage area to be upgraded according to the metadata of the firmware storage area to be upgraded. The system retrieves firmware image file data other than the firmware image file data and saves all such data to the reserved space of the dynamic random access memory. It then writes the other firmware image file data from the reserved space into the address space of the corresponding firmware partition in the new partition table. The system clears the metadata of the firmware storage area to be upgraded, the free area, the block list description file backup area, the boot loader firmware partition backup area, the recovery mode firmware partition backup area, the metadata partition backup area, the temporary storage partition backup area, and the boot communication partition backup area in the boot communication partition of the current partition table. It also clears the upgrade instruction information in the first preset position of the current partition table; writes a preset upgrade completion flag in the second preset position of the boot communication partition of the current partition table; and finally restarts the device to enter the upgraded system.
[0029] In this way, after upgrading the bootloader firmware and recovery mode firmware, other firmware can be upgraded to successfully complete the firmware upgrade. During this process, data in the storage area of the firmware to be upgraded may be corrupted. If an unexpected power failure occurs after the data is corrupted, the sixth preset upgrade instruction is written to the first preset location of the boot communication partition. This allows all files in the firmware upgrade package stored in the encrypted user data partition in the recovery mode firmware to be read out and saved to the storage area of the firmware to be upgraded according to the preset storage structure. Then, the fourth preset upgrade instruction is re-executed, thereby ensuring that the device will not become bricked and can restart the upgrade process in the event of an unexpected failure.
[0030] In some embodiments, the method 100 may further include: if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the fifth preset upgrade instruction, then load the boot recovery mode firmware and run the recovery mode firmware.
[0031] In some embodiments, the method 100 may further include: if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the sixth preset upgrade instruction, then obtaining the firmware partition table from the address space of the device storage partition table and using the firmware partition table as the current partition table; using the first partition after the preset partition list in the current partition table as the firmware storage area to be upgraded, wherein the firmware storage area to be upgraded is not a preset encrypted user data partition, and the preset partition list includes the bootloader firmware partition, the recovery mode firmware partition, the boot communication partition, the metadata partition, and the temporary storage partition; writing the partition start address of the firmware storage area to be upgraded into the command line parameter; loading and booting the recovery mode firmware and passing the command line parameter to the recovery mode firmware; and running the recovery mode firmware.
[0032] In this way, the first partition after the preset partition list in the current partition table is used as the firmware storage area to be upgraded. Even if there is an unexpected power failure, the data in the backup partition will not be destroyed, thus ensuring that the device will not become bricked in the event of an accident during the upgrade process and can continue to be upgraded, thereby improving the reliability of the upgrade.
[0033] In some embodiments, upgrade instruction information in the first preset location of the boot communication partition is read; it is determined whether the upgrade instruction information is a first preset upgrade instruction, a fifth preset upgrade instruction, or a sixth preset upgrade instruction; if not, the firmware upgrade process is exited; if so, the upgrade instruction information is used as the current upgrade instruction; if the current upgrade instruction is the first preset upgrade instruction, the temporary storage partition is mounted, and the firmware upgrade package under the preset path of the preset encrypted user data partition is read according to the block list description file under the preset path of the temporary storage partition; all files in the firmware upgrade package are read, and all files are saved to the firmware storage area to be upgraded according to the preset storage structure; if a new temporary storage partition is located in the temporary storage partition, the upgrade instruction information is used as the current upgrade instruction; if the ... If the current storage partition is smaller than the old temporary storage partition, then the block list description file under the preset path of the temporary storage partition is backed up to the block list description file backup area. Simultaneously, the block list description file backup area metadata is constructed and written to the tenth preset position of the boot communication partition; the firmware storage area metadata to be upgraded is constructed and written to the ninth preset position of the boot communication partition; the free area metadata is constructed and written to the third preset position of the boot communication partition; a second preset upgrade command is written to the first preset position of the boot communication partition, and the device is restarted; if the current upgrade command is the fifth preset upgrade command, then... After formatting the aforementioned temporary partition, the temporary partition is mounted. The block list description file backup area metadata is read from the tenth preset position of the boot communication partition, and it is determined whether the magic number in the block list description file backup area metadata is a preset magic number. If so, a block list description file backup header of a preset size is read from the starting address of the block list description file backup area in the block list description file backup area metadata. If the magic number in the block list description file backup header is a preset magic number, an empty block list description file is created under the preset path of the temporary partition, and a large number of blocks list description files are read starting from the starting address of the block list description file in the block list description file backup header. The data of the block list description file size in the backup header of the small block list description file is saved to the empty block list description file; a fourth preset upgrade instruction is written to the first preset position of the boot communication partition, and the device is restarted; if the current upgrade instruction is the sixth preset upgrade instruction, the partition start address of the firmware storage area to be upgraded is obtained from the command line parameters as the start address of the firmware storage area to be upgraded, and the temporary storage partition is mounted; the firmware upgrade package under the preset path of the preset encrypted user data partition is read according to the block list description file under the preset path of the temporary storage partition, and all files in the firmware upgrade package are read; all files are saved to the firmware storage area to be upgraded according to the preset storage structure;The system constructs metadata about the firmware to be upgraded storage area and writes it to the ninth preset position of the boot communication partition; it also writes a fourth preset upgrade command to the first preset position of the boot communication partition and restarts the device. This method enables the recovery mode firmware to run smoothly.
[0034] In some embodiments, prior to step 102, the method 100 may further include: constructing a firmware upgrade package, the firmware upgrade package including all firmware image files to be upgraded, including the bootloader firmware image file and the recovery mode firmware image file, and a partition table file with a preset name.
[0035] In some embodiments, prior to step 102, the method 100 may further include: saving the firmware upgrade package to be upgraded to a preset path in the user data partition; generating a block list occupied by the firmware upgrade package, decrypting the block data in the block list, rewriting the decrypted block data into the corresponding block, and saving the corresponding block list to a block list description file in a preset path in the temporary storage partition; and writing the first preset upgrade instruction at the first preset position in the boot communication partition, and then restarting the device.
[0036] In some embodiments, the method 100 may further include: in the recovery mode firmware, reading upgrade instruction information in the first preset position of the boot communication partition; if the upgrade instruction information corresponds to the first preset upgrade instruction, mounting the temporary storage partition; reading the firmware upgrade package in the preset path of the encrypted user data partition according to the block list description file in the preset path of the temporary storage partition, and saving all files read from the firmware upgrade package to the firmware storage area to be upgraded according to a preset storage structure; constructing metadata of the firmware storage area to be upgraded and writing it to the ninth preset position of the boot communication partition, and constructing metadata of the free area and writing it to the third preset position of the boot communication partition; and writing the second preset upgrade instruction in the first preset position of the boot communication partition, and then restarting the device.
[0037] In some embodiments, the method 100 may further include: if the current partition table already points to the backup partition, setting the current upgrade instruction to the third preset upgrade instruction; and writing the third preset upgrade instruction to the first preset position in the boot communication partition of the current partition table.
[0038] In some embodiments, the method 100 may further include: loading boot recovery mode firmware and passing command-line parameters to the recovery mode firmware; formatting the current temporary partition and creating an empty block list description file in a preset path of the temporary partition; reading data with a size equal to the block list description file in the backup header of the block list description file, starting from the start address of the block list description file in the backup header of the block list description file, and saving it to the newly created block list description file in the preset path of the temporary partition; and writing the fourth preset upgrade instruction at the first preset position in the boot communication partition, and then restarting the device.
[0039] In some embodiments, the method 100 may further include: loading boot recovery mode firmware and passing command line parameters to the recovery mode firmware; reading firmware upgrade packages from a preset path of the encrypted user data partition according to a block list description file under a preset path of the temporary storage partition, and saving all files read from the firmware upgrade packages to the firmware storage area to be upgraded according to a preset storage structure; constructing metadata of the firmware storage area to be upgraded and writing it to a ninth preset position in the boot communication partition; and writing the fourth preset upgrade instruction to the first preset position in the boot communication partition, and then restarting the device.
[0040] In some embodiments, the method 100 may further include: if there is no valid block list description file backup area metadata and block list description file backup area, setting the current upgrade instruction to the fourth preset upgrade instruction; and writing the fourth preset upgrade instruction to the first preset position in the boot communication partition of the current partition table.
[0041] In some embodiments, before step 102, the method 100 may further include: determining whether a preset upgrade completion marker exists in the second preset location of the boot communication partition; if it exists, outputting upgrade success information, deleting the firmware upgrade package under the preset path of the preset encrypted user data partition, and clearing the upgrade completion marker in the second preset location of the boot communication partition; if it does not exist, periodically determining whether there is a new firmware upgrade package to be upgraded; if so, obtaining the firmware upgrade package to be upgraded and saving the firmware upgrade package to be upgraded to the preset path of the preset encrypted user data partition; and verifying the legality of the firmware upgrade package to be upgraded. The system performs the following checks: If the validity check is invalid, the firmware upgrade package under the preset path of the preset encrypted user data partition is deleted, and the firmware upgrade process is exited; if the validity check is valid, a block list occupied by the firmware upgrade package under the preset path of the preset encrypted user data partition is generated through a preset decryption service, and the block data in the block list is decrypted to obtain decrypted data; the decrypted data is rewritten into the corresponding block, and the corresponding block list is saved to the block list description file under the preset path of the temporary storage partition; and the first preset upgrade instruction is written at the first preset position of the boot communication partition, and the device is restarted.
[0042] This method, which uses a preset upgrade completion marker to determine whether to enter the upgrade process, is more reliable. Furthermore, it performs a legality check on the detected firmware upgrade package to ensure the security of the firmware upgrade package, thereby guaranteeing the reliability of the firmware upgrade.
[0043] The following will describe, through examples, the application scenarios of the firmware upgrade method and device according to embodiments of the present invention.
[0044] Figure 2 This is a flowchart illustrating a method for upgrading firmware according to an embodiment of the present invention. Figure 2 As shown, the method includes steps 201 to 213.
[0045] In some embodiments, the preset encrypted user data partition remains unchanged when the firmware partition table changes; that is, the starting address and length of the preset encrypted user data partition remain the same in both the old and new partition tables. Furthermore, the address spaces of the bootloader firmware partition, boot communication partition, and recovery mode firmware partition in the firmware partition table precede the address spaces of various system firmware partitions (such as super or system) and the preset encrypted user data partition, and the preset encrypted user data partition is the last partition in the firmware partition table. Additionally, the length of the boot communication partition in the new partition table must be greater than or equal to the length of the boot communication partition in the old partition table, and the length of the metadata partition in the new partition table must be greater than or equal to the length of the metadata partition in the old partition table.
[0046] In step 201, a firmware upgrade package is constructed, which includes all firmware image files to be upgraded, including the bootloader firmware image file and the recovery mode firmware image file, as well as a partition table file with a preset name.
[0047] All firmware image files use the partition name corresponding to the image file as a prefix to the firmware image file name. For example, the prefix "uboot" of the uboot.img firmware image file is the uboot partition name.
[0048] The device contains read-write partitions including a pre-defined boot communication partition, a temporary storage partition, a metadata partition, and an encrypted user data partition. Two partition tables exist: a primary partition table and a backup partition table. The pre-defined boot communication partition (e.g., misc) is a raw partition used for communication between the system firmware, the bootloader firmware, and the upgrade mode firmware. The pre-defined temporary storage partition (e.g., cache) is a user-readable and write-compatible unencrypted file system partition used to store files generated during system operation. The pre-defined metadata partition (e.g., metadata) is a user-readable and write-compatible unencrypted file system partition used to store encryption keys for the user data area. The pre-defined encrypted user data partition (e.g., data) is a user-readable and write-compatible encrypted file system partition used to store user data.
[0049] In step 202, it is determined whether there is a preset upgrade completion mark in the second preset position of the boot communication partition. If there is, step 203 is executed; if not, step 204 is executed.
[0050] In step 203, an upgrade success message is output, the firmware upgrade package under the preset path of the preset encrypted user data partition is deleted, and the upgrade completion mark in the second preset location of the boot communication partition is cleared.
[0051] In step 204, it is periodically determined whether there is a new firmware upgrade package to be upgraded. If so, the firmware upgrade package to be upgraded is obtained and saved to a preset path in the preset encrypted user data partition. The firmware upgrade package to be upgraded is subjected to a legality check. If the legality check is invalid, the firmware upgrade package in the preset path in the preset encrypted user data partition is deleted and the firmware upgrade process is exited. If the legality check is valid, step 205 is executed.
[0052] In step 205, a list of blocks occupied by the firmware upgrade package under a preset path of the preset encrypted user data partition is generated by a preset decryption service (such as uncrypt under Android), and the block data in the block list is decrypted to obtain the decrypted data.
[0053] In step 206, the decrypted data is rewritten to the corresponding block, and the corresponding block list is saved to the block list description file (such as / cache / recovery / block.map under the cache partition) in the preset path of the temporary storage partition.
[0054] In step 207, a first preset upgrade command is written to the first preset location of the boot communication partition, and the device is restarted.
[0055] In step 208, if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the first preset upgrade instruction, then the command line parameters are determined based on the partition start address of the firmware storage area to be upgraded, the size of the preset temporary partition, the initial size of the data to be backed up, the header size of the data to be backed up, and the start address of the preset encrypted user data partition, and the command line parameters are passed to the recovery mode firmware. The data to be backed up includes the boot communication partition, the metadata partition, the boot boot firmware partition, and the recovery mode firmware partition, specifically including the following (1) to (9).
[0056] (1) Obtain the firmware partition table from the address space of the device storage partition table and use the firmware partition table as the current partition table.
[0057] Specifically, the primary partition table is read from the address space where the primary partition table is located, and its validity is verified. If the primary partition table is valid, it is used as the current partition table. If the primary partition table is invalid, the backup partition table is read from the address space of the backup partition table, and its validity is verified. If the backup partition table is valid, it is used as the current partition table.
[0058] (2) Take the first partition after the preset partition list in the current partition table as the firmware storage area to be upgraded (such as the super partition). This firmware storage area to be upgraded is not the preset encrypted user data partition. The preset partition list includes the bootloader firmware partition, recovery mode firmware partition, boot communication partition, metadata partition and temporary partition.
[0059] (3) Write the starting address of the partition of the firmware storage area to be upgraded into the command line parameters.
[0060] (4) Get the preset size of the temporary partition from the current partition table and write the preset size of the temporary partition into the command line parameters.
[0061] (5) Obtain the size of the boot communication partition, the size of the metadata partition, the size of the boot loader firmware partition, and the size of the recovery mode firmware partition from the current partition table, and add the size of the boot communication partition, the size of the metadata partition, the size of the boot loader firmware partition, and the size of the recovery mode firmware partition to obtain the initial size of the data to be backed up.
[0062] (6) Write the initial size of the data to be backed up into the command line parameters.
[0063] (7) The header size of the data to be backed up is obtained by adding the preset size of the boot communication partition backup header, the preset size of the metadata partition backup header, the preset size of the boot loader firmware partition backup header, and the preset size of the recovery mode firmware partition backup header, and the header size of the data to be backed up is written into the command line parameter.
[0064] (8) Obtain the starting address of the preset encrypted user data partition from the current partition table and write the starting address of the preset encrypted user data partition into the command line parameters.
[0065] (9) Load the boot recovery mode firmware and pass the command line parameters to the recovery mode firmware to run the recovery mode firmware.
[0066] In step 209, if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the second preset upgrade instruction, then after backing up the backup data and the temporary partition, the current partition table is updated so that the boot boot firmware partition, recovery mode firmware partition, metadata partition, temporary partition and boot communication partition in the current partition table point to the corresponding backup partitions respectively, and the updated current partition table is upgraded to the address space where the main partition table is located and the address space where the backup partition table is located in the storage medium, specifically including the following (1) to (19).
[0067] (1) Obtain the firmware partition table from the address space of the device storage partition table, and use the firmware partition table as the current partition table.
[0068] (2) Obtain the partition table file from the firmware storage area to be upgraded as a new partition table based on the metadata of the firmware storage area to be upgraded.
[0069] Specifically, the metadata of the firmware storage area to be upgraded is read from the ninth preset position of the boot communication partition, and it is checked whether the magic number in the metadata of the firmware storage area to be upgraded is a preset magic number. If it is not a preset magic number, the firmware upgrade process is exited. If it is a preset magic number, an upgrade image header of a preset size is read from the starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded. The magic number in the upgrade image header is checked whether it is a preset magic number. If not, the firmware upgrade process is exited. If it is, the partition table image file item is searched from the image file item list in the upgrade image header, and the starting address of the firmware storage area to be upgraded is added to the offset address of the image file in the partition table image file item in the firmware storage area to be upgraded to obtain the final address. Data with a length equal to the size of the image file in the partition table image file item is read from the final address as a new partition table.
[0070] (3) Set the initial value of the flag (such as pt_bak_flag) that the current partition table points to the backup area to false.
[0071] (4) Determine whether all backup areas, including the bootloader firmware partition backup area, recovery mode firmware partition backup area, metadata partition backup area, temporary partition backup area and boot communication partition backup area, have been backed up, and whether the bootloader firmware partition, recovery mode firmware partition, metadata partition, temporary partition and boot communication partition in the current partition table all point to the corresponding backup partition. If all of them do, set the flag that the current partition table points to the backup area to true, specifically including the following (a) to (o).
[0072] (a) Read the bootloader firmware partition backup area metadata from the fourth preset position of the boot communication partition in the current partition table, and check whether the magic number in the bootloader firmware partition backup area metadata is the preset magic number. If not, execute (5). If yes, read the bootloader firmware partition backup header of the preset size from the starting address of the bootloader firmware partition backup area in the bootloader firmware partition backup area metadata.
[0073] (b) Check if the magic number in the bootloader firmware partition backup header is the preset magic number. If it is not the preset magic number, execute (5). If it is the preset magic number, check if the starting address of the bootloader firmware partition in the current partition table is the same as the starting address of the bootloader firmware in the bootloader firmware partition backup header. If they are not the same, execute (5). If they are the same, read the recovery mode firmware partition backup metadata from the fifth preset position of the boot communication partition in the current partition table.
[0074] (c) Check if the magic number in the metadata of the recovery mode firmware partition backup area is the preset magic number. If not, execute (5). If yes, read the recovery mode firmware partition backup header of the preset size from the starting address of the recovery mode firmware partition backup area in the metadata of the recovery mode firmware partition backup area.
[0075] (d) Check if the magic number in the recovery mode firmware partition backup header is the preset magic number. If it is not the preset magic number, proceed to (5). If it is the preset magic number, check if the starting address of the recovery mode firmware partition in the current partition table is the same as the starting address of the recovery mode firmware in the recovery mode firmware partition backup header. If they are not the same, proceed to (5). If they are the same, check if the address space of the metadata partition in the current partition table is exactly the same as the address space of the metadata partition in the new partition table. If yes, proceed to step (h). If no, proceed to (e).
[0076] (e) Read the metadata partition backup area metadata information from the sixth preset position of the boot communication partition in the current partition table, and check whether the magic number in the metadata partition backup area metadata information is the preset magic number. If not, execute (5) as follows; if yes, execute (f).
[0077] (f) Read the metadata partition backup header of the preset size from the starting address of the metadata partition backup area in the metadata partition backup area metadata information, and check whether the magic number of the metadata partition backup header is the preset magic number. If not, execute (5); if yes, execute (g).
[0078] (g) Check if the starting address of the metadata partition in the current partition table is the same as the starting address of the metadata partition firmware in the metadata partition backup header. If not, execute (5); if yes, execute (h).
[0079] (h) Check if the address space of the temporary partition in the current partition table is exactly the same as the address space of the temporary partition in the new partition table. If yes, execute (l); otherwise, execute (i).
[0080] (i) Read the temporary partition backup area metadata from the seventh preset position of the boot communication partition in the current partition table, and check whether the magic number in the temporary partition backup area metadata is the preset magic number. If not, execute (5); if yes, execute (j).
[0081] (j) Read the start address of the temporary partition backup area from the metadata of the temporary partition backup area, and check whether the magic number of the temporary partition backup header is the preset magic number. If not, execute (5); if yes, execute (k).
[0082] (k) Check if the starting address of the temporary partition in the current partition table is the same as the starting address of the temporary partition firmware in the temporary partition backup header. If not, execute (5); if yes, execute (l).
[0083] (l) Check if the address space of the boot communication partition in the current partition table is exactly the same as the address space of the boot communication partition in the new partition table. If yes, set the flag that the current partition table points to the backup area to true and execute (5). If no, execute (m).
[0084] (m) Read the backup information of the boot communication partition from the eighth preset position of the boot communication partition in the current partition table, and check whether the magic number of the backup information of the boot communication partition is the preset magic number. If not, execute (5); if yes, execute (n).
[0085] (n) Read the boot communication partition backup header of the preset size from the starting address of the boot communication partition backup header metadata, and check whether the magic number of the boot communication partition backup header is the preset magic number. If not, execute (5); if yes, execute (o).
[0086] (o) Check if the starting address of the boot communication partition in the current partition table is the same as the starting address of the boot communication partition firmware in the boot communication partition backup header. If not, execute (5). If yes, set the flag that the current partition table points to the backup area to true and execute (5).
[0087] Having the same address space for two partitions means that the starting address and size of the corresponding partitions are the same.
[0088] (5) Determine whether the current partition table is true if the flag pointing to the backup area is true. If it is true, write the third preset upgrade instruction to the first preset position of the boot communication partition. If it is not true, execute (6).
[0089] (6) Obtain the starting address and partition size of the recovery mode firmware partition from the new partition table, and use them as the starting address and partition size of the new recovery mode firmware.
[0090] (7) Read the free zone metadata from the third preset position of the boot communication partition, and determine whether the magic number in the free zone metadata is the preset magic number. If not, exit the firmware upgrade process; if yes, execute (8).
[0091] (8) Determine whether the address obtained by adding the starting address of the new recovery mode firmware and the partition size of the new recovery mode firmware is greater than the starting address of the free area in the free area metadata. If it is greater, exit the firmware upgrade process. If it is not greater, back up the bootloader firmware partition to the bootloader firmware partition backup area and write the bootloader firmware partition backup area metadata in the fourth preset position of the boot communication partition in the current partition table, specifically including the following (a) to (d).
[0092] (a) Set the magic number in the bootloader firmware partition backup header to the preset bootloader firmware partition backup magic number; set the bootloader firmware start address in the bootloader firmware partition backup header to "free area start address in free area metadata + bootloader firmware partition backup header size"; set the bootloader firmware size in the bootloader firmware partition backup header to the partition size of the bootloader firmware partition in the current partition table.
[0093] (b) Obtain the starting address and size of the bootloader firmware partition in the current partition table, and use them as the starting address and size of the current bootloader firmware partition; read the size of the current bootloader firmware partition from the starting address, and write the read data to the starting address of the bootloader firmware in the bootloader firmware partition backup header, with the written data size being the bootloader firmware size in the bootloader firmware partition backup header; write the bootloader firmware partition backup header to the starting address of the free area in the free area metadata, with the written data size being the size of the bootloader firmware partition backup header.
[0094] (c) Construct the bootloader firmware partition backup area metadata and write the bootloader firmware partition backup area metadata to the preset location in the current partition table used to store the bootloader firmware partition backup area metadata.
[0095] Specifically, the magic number in the bootloader firmware partition backup area metadata is set to the preset magic number in the bootloader firmware partition backup area metadata; the starting address of the bootloader firmware partition backup area in the bootloader firmware partition backup area metadata is set to the starting address of the free area in the free area metadata; and the constructed bootloader firmware partition backup area metadata is written to the fourth preset position of the boot communication partition in the current partition table.
[0096] (d) Update the start address of the free area in the free area metadata to: original free area start address + size of the bootloader firmware partition backup header + size of the bootloader firmware in the bootloader firmware partition backup header; update the length of the free area in the free area metadata to: original free area length - size of the bootloader firmware partition backup header - size of the bootloader firmware in the bootloader firmware partition backup header.
[0097] The bootloader firmware partition backup area consists of a bootloader firmware partition backup header of a preset size and the bootloader firmware itself; the bootloader firmware partition backup header of the preset size consists of a preset magic number, the bootloader firmware starting address, and the bootloader firmware size; the bootloader firmware partition backup area metadata consists of a preset magic number and the starting address of the bootloader firmware partition backup area.
[0098] (9) Back up the recovery mode firmware partition to the recovery mode firmware partition backup area, and write the recovery mode firmware partition backup area metadata to the fifth preset position of the boot communication partition in the current partition table, specifically including the following (a) to (d).
[0099] (a) Set the magic number in the recovery mode firmware partition backup header to the preset magic number of the recovery mode firmware partition backup; set the recovery mode firmware start address in the recovery mode firmware partition backup header to "free area start address in free area metadata + size of recovery mode firmware partition backup header"; set the recovery mode firmware size in the recovery mode firmware partition backup header to the partition size of the recovery mode firmware partition in the current partition table.
[0100] (b) Obtain the starting address and size of the recovery mode firmware partition in the current partition table, and use them as the starting address and size of the current recovery mode firmware partition; read the size of the current recovery mode firmware partition from the starting address of the current recovery mode firmware, and write the read data to the starting address of the recovery mode firmware in the recovery mode firmware partition backup header, with the written data size being the recovery mode firmware size in the recovery mode firmware partition backup header; write the recovery mode firmware partition backup header to the starting address of the free area in the free area metadata, with the written data size being the size of the recovery mode firmware partition backup header.
[0101] (c) Construct the recovery mode firmware partition backup area metadata and write the recovery mode firmware partition backup area metadata to the preset location in the current partition table used to store the recovery mode firmware partition backup area metadata.
[0102] Specifically, the magic number in the metadata of the recovery mode firmware partition backup area is set to the preset magic number of the metadata of the recovery mode firmware partition backup area; the starting address of the recovery mode firmware partition backup area in the metadata of the recovery mode firmware partition backup area is set to the starting address of the free area in the metadata of the free area; and the constructed metadata of the recovery mode firmware partition backup area is written to the fifth preset position of the boot communication partition in the current partition table.
[0103] (d) Update the start address of the free area in the free area metadata to: original free area start address + size of the recovery mode firmware partition backup header + recovery mode firmware size in the recovery mode firmware partition backup header; update the length of the free area in the free area metadata to: original free area length - size of the recovery mode firmware partition backup header - recovery mode firmware size in the recovery mode firmware partition backup header.
[0104] The recovery mode firmware partition backup area consists of a preset-size recovery mode firmware partition backup header and the recovery mode firmware itself. The preset-size recovery mode firmware partition backup header comprises a preset magic number, the recovery mode firmware's starting address, and the recovery mode firmware's size. The recovery mode firmware partition backup area metadata consists of a preset magic number and the recovery mode firmware partition backup area's starting address.
[0105] (10) Determine whether the address space of the metadata partition is the same in the current partition table and the new partition table. If they are different, back up the metadata partition to the metadata partition backup area and write the metadata partition backup area metadata information in the sixth preset position of the boot communication partition in the current partition table, specifically including the following (a) to (e).
[0106] (a) Determine whether the address spaces of the metadata partition are the same in the current partition table and the new partition table. If they are the same, execute (11); if they are different, execute (b).
[0107] (b) Set the magic number in the metadata partition backup header to the preset metadata partition backup magic number; set the metadata partition firmware start address in the metadata partition backup header to "free zone start address in free zone metadata information + metadata partition backup header size"; set the metadata partition firmware size in the metadata partition backup header to the partition size of the metadata partition in the current partition table.
[0108] (c) Obtain the starting address and size of the metadata partition in the current partition table, and use them as the starting address and size of the current metadata partition; read the data of the current metadata partition size from the starting address of the current metadata partition, and write the read data to the starting address of the metadata partition firmware in the metadata partition backup header, with the written data size being the size of the metadata partition firmware in the metadata partition backup header; write the metadata partition backup header to the starting address of the free area in the free area metadata information, with the written data size being the size of the metadata partition backup header.
[0109] (d) Construct metadata partition backup zone metadata information and write the metadata partition backup zone metadata information to the sixth preset position of the boot communication partition in the current partition table.
[0110] Specifically, the magic number in the metadata partition backup area metadata information is set to the preset magic number of the metadata partition backup area metadata information; the starting address of the metadata partition backup area in the metadata partition backup area metadata information is set to the starting address of the free area in the free area metadata information; and the constructed metadata partition backup area metadata information is written to the sixth preset position of the boot communication partition in the current partition table.
[0111] (e) Update the start address of the free area in the free area metadata to: original free area start address + size of metadata partition backup header + size of metadata partition firmware in metadata partition backup header; update the length of the free area in the free area metadata to: original free area length - size of metadata partition backup header - size of metadata partition firmware in metadata partition backup header.
[0112] The metadata partition backup area metadata information is composed of a preset magic number and the starting address of the metadata partition backup area.
[0113] (11) Determine whether the address space of the temporary partition is the same in the current partition table and the new partition table. If they are different, back up the temporary partition to the temporary partition backup area and write the temporary partition backup area metadata in the seventh preset position of the boot communication partition in the current partition table, specifically including the following (a) to (e).
[0114] (a) Check if the address space of the temporary partition is the same in the current partition table and the new partition table. If they are the same, proceed to step (12). If they are different, proceed to step (b).
[0115] (b) Set the magic number in the temporary partition backup header to the preset magic number of the temporary partition backup; set the starting address of the temporary partition firmware in the temporary partition backup header to "the starting address of the free area in the free area metadata + the size of the temporary partition backup header"; set the size of the temporary partition firmware in the temporary partition backup header to the partition size of the temporary partition in the current partition table.
[0116] (c) Obtain the starting address and size of the temporary partition in the current partition table, and use them as the starting address and size of the current temporary partition; read the size of the current temporary partition from the starting address of the current temporary partition, and write the read data to the starting address of the temporary partition firmware in the temporary partition backup header, with the size of the written data being the size of the temporary partition firmware in the temporary partition backup header; write the temporary partition backup header to the starting address of the free area in the free area metadata, with the size of the written data being the size of the temporary partition backup header.
[0117] (d) Construct temporary partition backup area metadata and write the temporary partition backup area metadata to the seventh preset position of the boot communication partition in the current partition table.
[0118] Specifically, the magic number in the temporary partition backup area metadata is set to the preset magic number of the temporary partition backup area metadata; the starting address of the temporary partition backup area in the temporary partition backup area metadata is set to the starting address of the free area in the free area metadata; and the constructed temporary partition backup area metadata is written to the seventh preset position of the boot communication partition in the current partition table.
[0119] (e) Update the start address of the free area in the free area metadata to: original free area start address + size of temporary partition backup header + size of temporary partition firmware in temporary partition backup header; update the length of the free area in the free area metadata to: original free area length - size of temporary partition backup header - size of temporary partition firmware in temporary partition backup header.
[0120] The temporary partition backup area consists of a temporary partition backup header of a preset size and temporary partition firmware. The temporary partition backup header of the preset size consists of a preset magic number, the starting address of the temporary partition firmware, and the size of the temporary partition firmware. The metadata of the temporary partition backup area consists of a preset magic number and the starting address of the temporary partition backup area.
[0121] (12) Determine whether the address space of the boot communication partition is the same in the current partition table and the new partition table. If they are different, back up the boot communication partition to the boot communication partition backup area and write the boot communication partition backup area metadata to the eighth preset position of the boot communication partition in the current partition table. At the same time, write the boot communication partition backup area metadata to the preset position in the boot communication partition firmware in the boot communication partition backup area for storing the boot communication partition backup area metadata, specifically including the following (a) to (e).
[0122] (a) Check if the address space of the boot communication partition is the same in the current partition table and the new partition table. If they are the same, execute (13). If they are different, execute (b).
[0123] (b) Set the magic number in the boot communication partition backup header to the preset boot communication partition backup magic number; set the boot communication partition firmware start address in the boot communication partition backup header to "free area start address in free area metadata + size of boot communication partition backup header"; set the boot communication partition firmware size in the boot communication partition backup header to the partition size of the boot communication partition in the current partition table.
[0124] (c) Obtain the starting address and size of the boot communication partition in the current partition table, and use them as the starting address and size of the current boot communication partition; read the size of the current boot communication partition from the starting address of the current boot communication partition, and write the read data to the starting address of the boot communication partition firmware in the boot communication partition backup header, with the written data size being the size of the boot communication partition firmware in the boot communication partition backup header; write the boot communication partition backup header to the starting address of the free area in the free area metadata, with the written data size being the size of the boot communication partition backup header.
[0125] (d) Construct the backup information of the boot communication partition and write the backup information of the boot communication partition to the eighth preset position of the boot communication partition in the current partition table. At the same time, write the constructed backup information of the boot communication partition to the preset position in the firmware of the boot communication partition in the backup area of the boot communication partition.
[0126] Specifically, the magic number in the boot communication partition backup area metadata is set to a preset magic number; the starting address of the boot communication partition backup area in the boot communication partition backup area metadata is set to the starting address of the free area in the free area metadata; the constructed boot communication partition backup area metadata is written to the eighth preset position of the boot communication partition in the current partition table; and the constructed boot communication partition backup area metadata is written to the preset position in the boot communication partition firmware in the boot communication partition backup area used to store the boot communication partition backup area metadata.
[0127] (e) Update the start address of the free area in the free area metadata to: original free area start address + size of the boot communication partition backup header + size of the boot communication partition firmware in the boot communication partition backup header; update the length of the free area in the free area metadata to: original free area length - size of the boot communication partition backup header - size of the boot communication partition firmware in the boot communication partition backup header.
[0128] The boot communication partition backup area consists of a preset-size boot communication partition backup header and boot communication partition firmware. The preset-size boot communication partition backup header consists of a preset magic number, the starting address of the boot communication partition firmware, and the size of the boot communication partition firmware. The boot communication partition backup area metadata consists of a preset magic number and the starting address of the boot communication partition backup area.
[0129] (13) Set the starting address of the bootloader firmware partition in the current partition table to the starting address of the bootloader firmware in the bootloader firmware partition backup header.
[0130] (14) Set the starting address of the recovery mode firmware partition in the current partition table to the starting address of the recovery mode firmware in the recovery mode firmware partition backup header.
[0131] (15) When the address space of the metadata partition is different in the current partition table and the new partition table, set the starting address of the metadata partition in the current partition table to the starting address of the metadata partition firmware in the metadata partition backup header.
[0132] (16) When the address space of the temporary partition is different in the current partition table and the new partition table, set the starting address of the temporary partition in the current partition table to the starting address of the temporary partition firmware in the temporary partition backup header.
[0133] (17) When the address space of the boot communication partition is different in the current partition table and the new partition table, set the starting address of the boot communication partition in the current partition table to the starting address of the boot communication partition firmware in the boot communication partition backup header.
[0134] (18) Write the updated free space metadata into the third preset position of the boot communication partition in the updated current partition table, and upgrade the updated current partition table to the address space where the main partition table and the backup partition table are located in the storage medium.
[0135] (19) Write the third preset upgrade instruction into the first preset location of the boot communication partition.
[0136] In step 210, if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the third preset upgrade instruction, the boot program firmware partition and the recovery mode firmware partition are upgraded. When the address space of the metadata partition is different in the current partition table and the new partition table, the metadata partition firmware in the metadata partition backup area is written to the address space where the metadata partition is located in the new partition table, specifically including the following (1) to (10).
[0137] (1) Obtain the firmware partition table from the address space of the device storage partition table and use the firmware partition table as the current partition table.
[0138] (2) Obtain the partition table file from the firmware storage area to be upgraded as a new partition table based on the metadata of the firmware storage area to be upgraded.
[0139] (3) If the current partition table and the new partition table are different, execute (4) to (10); if the current partition table and the new partition table are the same, execute (10).
[0140] (4) Obtain the bootloader firmware image file data from the firmware storage area to be upgraded based on the metadata of the firmware storage area to be upgraded, and write the bootloader firmware image file data into the address space where the bootloader firmware partition is located in the new partition table.
[0141] Specifically, the system reads the metadata of the firmware storage area to be upgraded from the ninth preset position of the boot communication partition in the current partition table, and checks whether the magic number in the metadata of the firmware storage area to be upgraded is the preset magic number. If not, the firmware upgrade process is exited. If so, an upgrade image header of preset size is read from the starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded. The system checks whether the magic number in the upgrade image header is the preset magic number. If not, the firmware upgrade process is exited. If it is the preset magic number, the system searches for the bootloader firmware image file item in the image file item list in the upgrade image header, and reads data with a length equal to the size of the image file in the bootloader firmware image file item from the address "starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded + offset address of the image file in the bootloader firmware image file item in the firmware storage area to be upgraded" as the bootloader firmware image file data. The bootloader firmware image file data is then written to the address space where the bootloader firmware partition is located in the new partition table.
[0142] (5) Obtain the recovery mode firmware image file data from the firmware storage area to be upgraded based on the metadata of the firmware storage area to be upgraded, and write the recovery mode firmware image file data into the address space where the recovery mode firmware partition is located in the new partition table.
[0143] Specifically, the system reads the metadata of the firmware storage area to be upgraded from the ninth preset position of the boot communication partition in the current partition table, and checks whether the magic number in the metadata of the firmware storage area to be upgraded is the preset magic number. If not, the firmware upgrade process is exited. If so, the system reads the upgrade image header of the preset size from the starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded, and checks whether the magic number in the upgrade image header is the preset magic number. If not, the firmware upgrade process is exited. If it is the preset magic number, the system searches for the recovery mode firmware image file item in the image file item list in the upgrade image header. Data with a length equal to the size of the image file in the recovery mode firmware image file item is read from the address space where the recovery mode firmware partition is located in the new partition table.
[0144] (6) When the address space of the metadata partition is different in the current partition table and the new partition table, write the metadata partition firmware in the metadata partition backup area to the address space where the metadata partition is located in the new partition table.
[0145] Specifically, it determines whether the address spaces of the metadata partition in the current partition table and the new partition table are the same. If they are different, it reads the metadata partition backup area metadata information from the sixth preset position in the boot communication partition of the current partition table and checks whether the magic number in the metadata partition backup area metadata information is a preset magic number. If it is not a preset magic number, it exits the firmware upgrade process. If it is a preset magic number, it reads a metadata partition backup header of a preset size from the starting address of the metadata partition backup area in the metadata partition backup area metadata information. It checks whether the magic number in the metadata partition backup header is a preset magic number. If not, it exits the firmware upgrade process. If it is, it reads data with a length equal to the size of the metadata partition firmware in the metadata partition backup header from the starting address of the metadata partition firmware in the metadata partition backup header as the metadata partition firmware. It then writes the metadata partition firmware into the address space where the metadata partition is located in the new partition table.
[0146] (7) When the address spaces of the temporary partition in the current partition table and the new partition table are different, determine whether the size of the temporary partition in the new partition table is greater than or equal to the size of the temporary partition in the current partition table. If so, write the temporary partition firmware in the backup area of the temporary partition to the address space where the temporary partition is located in the new partition table.
[0147] Specifically, it determines whether the address spaces of the temporary partition in the current partition table and the new partition table are the same. If they are different, it checks whether the size of the temporary partition in the new partition table is greater than or equal to the size of the temporary partition in the current partition table. If so, it reads the temporary partition backup area metadata from the seventh preset position of the boot communication partition in the current partition table; it checks whether the magic number in the temporary partition backup area metadata is a preset magic number. If it is not a preset magic number, it exits the firmware upgrade process. If it is a preset magic number, it reads a temporary partition backup header of a preset size from the starting address of the temporary partition backup area in the temporary partition backup area metadata; it checks whether the magic number in the temporary partition backup header is a preset magic number. If not, it exits the firmware upgrade process. If so, it reads data with a length equal to the size of the temporary partition firmware in the temporary partition backup header from the starting address of the temporary partition firmware in the temporary partition backup header as the temporary partition firmware; and it writes the temporary partition firmware into the address space where the temporary partition is located in the new partition table.
[0148] (8) When the address space of the boot communication partition is different in the current partition table and the new partition table, write all the boot communication partition data in the current partition table to the address space of the boot communication partition in the new partition table.
[0149] (9) Upgrade the new partition table to the address space where the primary partition table and the backup partition table are located in the storage medium, and set the new partition table as the current partition table.
[0150] (10) Determine whether there is a valid block list description file backup area metadata in the current partition table's boot communication partition, and whether there is a valid block list description file backup header in the block list description file backup area. If both exist, write the fifth preset upgrade instruction to the first preset position of the boot communication partition. If not both exist, write the fourth preset upgrade instruction to the first preset position of the boot communication partition, specifically including the following (a) to (b).
[0151] (a) Read the block list description file backup area metadata from the tenth preset position of the boot communication partition of the current partition table, and check whether the magic number in the block list description file backup area metadata is the preset magic number. If not, write the fourth preset upgrade instruction to the first preset position of the boot communication partition. If yes, read the block list description file backup header of the preset size from the starting address of the block list description file backup area in the block list description file backup area metadata.
[0152] (b) Check if the magic number in the block list description file backup header is the preset magic number. If not, write the fourth preset upgrade instruction to the first preset location of the boot communication partition. If not, write the fifth preset upgrade instruction to the first preset location of the boot communication partition.
[0153] In step 211, if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the fifth preset upgrade instruction, then the boot recovery mode firmware is loaded and the recovery mode firmware is run.
[0154] In some embodiments, running recovery mode firmware may specifically include the following (1) to (5).
[0155] (1) Read the upgrade instruction information from the first preset location of the boot communication partition;
[0156] (2) Determine whether the upgrade instruction information is the first preset upgrade instruction, the fifth preset upgrade instruction, or the sixth preset upgrade instruction. If not, exit the firmware upgrade process. If so, use the upgrade instruction information as the current upgrade instruction.
[0157] (3) If the current upgrade instruction is the first preset upgrade instruction, then mount the temporary storage partition, and read the firmware upgrade package under the preset path of the preset encrypted user data partition according to the block list description file under the preset path of the temporary storage partition; read all files in the firmware upgrade package, and save all files to the firmware storage area to be upgraded according to the preset storage structure; if the new temporary storage partition is smaller than the old temporary storage partition, then back up the block list description file under the preset path of the temporary storage partition to the block list description file backup area, and at the same time construct the block list description file backup area metadata, and write the block list description file backup area metadata to the tenth preset position of the boot communication partition; construct the firmware storage area metadata to be upgraded, and write the firmware storage area metadata to the ninth preset position of the boot communication partition; construct the free area metadata, and write the free area metadata to the third preset position of the boot communication partition; write the second preset upgrade instruction in the first preset position of the boot communication partition, and restart the device, specifically including the following (a) to (p).
[0158] (a) Obtain the starting address of the firmware storage area to be upgraded from the command line parameters as the starting address of the firmware storage area to be upgraded; obtain the size of the temporary partition from the command line parameters as the size of the old temporary partition; obtain the initial size of the data to be backed up from the command line parameters as the initial size of the data to be backed up; obtain the header size of the data to be backed up from the command line parameters as the header size of the data to be backed up; and obtain the starting address of the encrypted user data partition from the command line parameters as the starting address of the user data partition.
[0159] (b) Construct an upgrade image header of a preset size; the upgrade image header includes a preset magic number and a preset size list of image file items; each image file item in the image file item list includes the image file partition name, the offset address of the image file in the storage area of the firmware to be upgraded, and the size of the image file.
[0160] (c) Set the magic number of the upgraded image header to the preset magic number.
[0161] (d) Mount the temporary storage partition, and read the firmware upgrade package in the preset path of the preset encrypted user data partition according to the block list description file in the preset path of the temporary storage partition. Calculate the sum of the sizes of all files in the firmware upgrade package as the total size of the files to be upgraded. Read the partition table file with a preset name in the firmware upgrade package file in the preset path of the preset encrypted user data partition according to the block list description file in the preset path of the temporary storage partition, and obtain the first partition after the preset partition list from the partition table file with the preset name as the comparison partition. Determine whether the starting address of the comparison partition is greater than the starting address of the firmware storage area to be upgraded. If so, update the starting address of the firmware storage area to be upgraded to the starting address of the comparison partition, and use "user data partition starting address - firmware storage area starting address" as the usable space size. If the new temporary storage partition size is greater than or equal to the old temporary storage partition size, determine "preset size..." If the sum of "upgrade image header + total size of files to be upgraded + initial size of data to be backed up + size of data to be backed up + size of temporary partition backup header + size of old temporary partition" is greater than the available space, then the firmware upgrade process is exited and the upgrade instruction information in the first preset location of the boot communication partition is cleared. If the new temporary partition size is smaller than the old temporary partition size, then the size of the block list description file under the preset path of the temporary partition is used as the block list description file size, and it is determined whether the sum of "upgrade image header of preset size + total size of files to be upgraded + initial size of data to be backed up + size of data to be backed up + size of temporary partition backup header of preset size + size of old temporary partition + size of block list description file backup header of preset size + size of block list description file" is greater than the available space. If so, then the firmware upgrade process is exited and the upgrade instruction information in the first preset location of the boot communication partition is cleared.
[0162] (e) Read the partition table file with the preset name from the firmware upgrade package file in the preset path of the preset encrypted user data partition according to the block list description file (such as / cache / recovery / block.map) in the preset path of the temporary partition; use the first image file item in the image file item list in the upgrade image header as the partition table image file item, and use the preset name of the partition table file as the image file partition name of the partition table image file item, use the size of the upgrade image header as the offset address of the image file of the partition table image file item in the storage area of the firmware to be upgraded, and use the size of the partition table file as the image file size of the partition table image file item; write the partition table file with the preset name to the offset address of the image file of the partition table image file item in the storage area of the firmware to be upgraded, and the data write size is the image file size of the partition table image file item.
[0163] (f) According to the block list description file under the preset path of the temporary storage partition, traverse each firmware image file to be upgraded in the firmware upgrade package under the preset path of the preset encrypted user data partition in a preset order; for each firmware image file to be upgraded read, starting from the second image file item in the image file item list of the upgrade image header, update the corresponding image file item in the image file item list of the upgrade image header in a sequential order, and at the same time write the corresponding firmware image file to the offset address of the image file of the corresponding image file item in the firmware storage area to be upgraded, with the data writing size being the size of the corresponding image file.
[0164] Specifically, for each firmware image file to be upgraded read: the corresponding image file item in the image file item list at the top of the upgrade image is taken as the current image file item; the file name prefix of the firmware image file to be upgraded is obtained as the image file partition name of the current image file item; the size of the firmware image file to be upgraded is obtained as the image file size of the current image file item; the "offset address of the image file in the firmware storage area to be upgraded + image file size" of the previous image file item of the current image file item is taken as the offset address of the image file in the firmware storage area to be upgraded of the current image file item; the corresponding firmware image file to be upgraded is written to the offset address of the image file in the firmware storage area to be upgraded of the current image file item, and the data writing size is the image file size of the current image file item.
[0165] (g) Write the header of the upgrade image of the preset size to the starting address of the firmware storage area to be upgraded.
[0166] (h) Obtain the partition table file with the preset name in the firmware upgrade package file under the preset path of the preset encrypted user data partition, and obtain the size of the temporary partition from the partition table file as the new temporary partition size; take "the starting address of the storage area of the firmware to be upgraded + the preset size of the upgrade image header + the total size of the file to be upgraded" as the starting address of the free area; take "the starting address of the user data partition - the starting address of the free area" as the length of the free area.
[0167] (i) If the new temporary partition size is greater than or equal to the old temporary partition size, then execute (n); otherwise, execute (j) through (p).
[0168] (j) Set the magic number in the block list description file backup header to the preset magic number of the block list description file backup; set the starting address of the block list description file in the block list description file backup header to "starting address of free area + size of block list description file backup header"; set the size of the block list description file in the block list description file backup header to the size of the block list description file under the preset path of the temporary storage partition.
[0169] (k) Write the block list description file under the preset path of the temporary partition to the starting address of the block list description file in the block list description file backup header, and write the data size as the size of the block list description file in the block list description file backup header; write the block list description file backup header to the starting address of the free area, and write the data size as the size of the block list description file backup header.
[0170] (l) Construct the block list description file backup area metadata and write the block list description file backup area metadata to the tenth preset position of the boot communication partition.
[0171] Specifically, the magic number in the block list description file backup area metadata is set to the preset magic number; the starting address of the block list description file backup area in the block list description file backup area metadata is set to the starting address of the free area; and the constructed block list description file backup area metadata is written to the tenth preset position of the boot communication partition.
[0172] (m) Update the starting address of the free area to: the original starting address of the free area + the size of the block list description file backup header + the size of the block list description file; update the length of the free area to: the original length of the free area - the size of the block list description file backup header - the size of the block list description file.
[0173] The block list description file backup area consists of a preset-size block list description file backup header and the block list description file itself. The preset-size block list description file backup header consists of a preset magic number, the starting address of the block list description file, and the size of the block list description file. The metadata of the block list description file backup area consists of a preset magic number and the starting address of the block list description file backup area.
[0174] (n) Construct the metadata of the firmware storage area to be upgraded, and write the metadata of the firmware storage area to be upgraded to the ninth preset position of the boot communication partition. The metadata of the firmware storage area to be upgraded is composed of a preset magic number and the starting address of the firmware storage area to be upgraded.
[0175] Specifically, the magic number in the metadata of the firmware storage area to be upgraded is set to the preset magic number of the metadata of the firmware storage area to be upgraded; the starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded is set to the starting address of the firmware storage area to be upgraded; and the constructed metadata of the firmware storage area to be upgraded is written to the ninth preset position of the boot communication partition.
[0176] (o) Construct free zone metadata and write the free zone metadata to the third preset location of the boot communication partition; the free zone metadata includes the preset magic number, the free zone start address and the free zone length.
[0177] Specifically, the magic number in the free area metadata is set to a preset magic number; the free area start address and free area length in the free area metadata are set to the free area start address and free area length calculated above, respectively.
[0178] (p) Write the second preset upgrade command in the first preset location of the boot communication partition and restart the device.
[0179] (4) If the current upgrade instruction is the fifth preset upgrade instruction, then format the current temporary partition and mount the temporary partition; read the block list description file backup area metadata from the tenth preset position of the boot communication partition, and determine whether the magic number in the block list description file backup area metadata is the preset magic number of the block list description file backup area metadata. If so, read the block list description file backup header of the preset size from the starting address of the block list description file backup area in the block list description file backup area metadata. If the magic number in the block list description file backup header is the preset magic number, then create an empty block list description file under the preset path of the temporary partition, and start from the starting address of the block list description file in the block list description file backup header, read data of the same size as the block list description file in the block list description file backup header and save it to the empty block list description file; write the fourth preset upgrade instruction at the first preset position of the boot communication partition and restart the device, specifically including the following (a) to (c).
[0180] (a) After formatting the current temporary partition, mount the temporary partition; read the block list description file backup area metadata from the tenth preset position of the boot communication partition, and determine whether the magic number in the block list description file backup area metadata is the preset magic number of the block list description file backup area metadata. If not, exit the firmware upgrade process; if yes, execute (b).
[0181] (b) Read the block list description file backup header of a preset size from the starting address of the block list description file backup area metadata, and determine whether the magic number in the block list description file backup header is the preset magic number. If not, exit the firmware upgrade process; if yes, execute (c).
[0182] (c) Create an empty block list description file (e.g., / cache / recovery / block.map) in the preset path of the temporary storage partition, and start reading data with a size equal to the size of the block list description file in the backup header of the block list description file from the starting address of the block list description file in the backup header of the block list description file and save it to the empty block list description file; write the fourth preset upgrade command in the first preset position of the boot communication partition and restart the device.
[0183] (5) If the current upgrade instruction is the sixth preset upgrade instruction, the partition start address of the firmware storage area to be upgraded is obtained from the command line parameters as the start address of the firmware storage area to be upgraded, and the temporary partition is mounted; the firmware upgrade package under the preset path of the preset encrypted user data partition is read according to the block list description file under the preset path of the temporary partition, and all files in the firmware upgrade package are read; all files are saved to the firmware storage area to be upgraded according to the preset storage structure; the metadata of the firmware storage area to be upgraded is constructed, and the metadata of the firmware storage area to be upgraded is written to the ninth preset position of the boot communication partition; the fourth preset upgrade instruction is written to the first preset position of the boot communication partition, and the device is restarted, specifically including the following (a) to (g).
[0184] (a) Obtain the starting address of the partition of the firmware storage area to be upgraded from the command line parameters as the starting address of the firmware storage area to be upgraded.
[0185] (b) Build an upgrade image header of a preset size.
[0186] (c) Set the magic number of the upgraded image header to the preset magic number.
[0187] (d) Mount the temporary storage partition, and read the partition table file with the preset name from the firmware upgrade package in the preset path of the preset encrypted user data partition according to the block list description file in the preset path of the temporary storage partition; use the first image file item in the image file item list in the upgrade image header as the partition table image file item; use the preset name of the partition table file as the image file partition name of the partition table image file item; use the size of the upgrade image header as the offset address of the image file of the partition table image file item in the storage area of the firmware to be upgraded; use the size of the partition table file as the image file size of the partition table image file item; write the partition table file with the preset name to the offset address of the image file of the partition table image file item in the storage area of the firmware to be upgraded, and the data written size is the image file size of the partition table image file item.
[0188] (e) According to the block list description file under the preset path of the temporary storage partition, traverse each firmware image file to be upgraded in the firmware upgrade package under the preset path of the preset encrypted user data partition in a preset order; for each firmware image file to be upgraded read, starting from the second image file item in the image file item list of the upgrade image header, update the corresponding image file item in the image file item list of the upgrade image header in a sequential order, and at the same time write the corresponding firmware image file to the offset address of the image file of the corresponding image file item in the firmware storage area to be upgraded, with the data writing size being the size of the corresponding image file.
[0189] Specifically, for each firmware image file to be upgraded read: the corresponding image file item in the image file item list in the upgrade image header is taken as the current image file item; the file name prefix of the firmware image file to be upgraded is obtained as the image file partition name of the current image file item; the size of the firmware image file to be upgraded is obtained as the image file size of the current image file item; the "offset address of the image file in the firmware storage area to be upgraded + image file size" of the previous image file item of the current image file item is taken as the offset address of the image file in the firmware storage area to be upgraded for the current image file item; the corresponding firmware image file to be upgraded is written to the offset address of the image file in the firmware storage area to be upgraded for the current image file item, and the data writing size is the image file size of the current image file item; the upgrade image header of the preset size is written to the starting address of the firmware storage area to be upgraded.
[0190] (f) Construct the metadata of the firmware storage area to be upgraded, and write the metadata of the firmware storage area to be upgraded to the ninth preset position of the boot communication partition.
[0191] Specifically, the magic number in the metadata of the firmware storage area to be upgraded is set to the preset magic number of the metadata of the firmware storage area to be upgraded; the starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded is set to the starting address of the firmware storage area to be upgraded; and the constructed metadata of the firmware storage area to be upgraded is written to the ninth preset position of the boot communication partition.
[0192] (g) Write the fourth preset upgrade command in the first preset position of the boot communication partition and restart the device.
[0193] In step 212, if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the fourth preset upgrade instruction, then the firmware other than the boot program firmware and recovery mode firmware will be upgraded, specifically including the following (1) to (8).
[0194] (1) Obtain the firmware partition table from the address space of the device storage partition table and use the firmware partition table as the current partition table.
[0195] (2) Read the metadata of the firmware storage area to be upgraded from the ninth preset position in the boot communication partition of the current partition table, and determine whether the magic number in the metadata of the firmware storage area to be upgraded is the preset magic number. If not, exit the firmware upgrade process. If yes, read the upgrade image header of the preset size from the starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded.
[0196] (3) Determine whether the magic number in the upgrade image header is the preset magic number. If it is not the preset magic number, write the sixth preset upgrade instruction to the first preset position of the boot communication partition. If it is the preset magic number, retrieve the firmware image file data other than the boot boot program firmware image file data and the recovery mode firmware image file data from the firmware storage area according to the metadata of the firmware storage area to be upgraded, and save all the other firmware image file data to the reserved space of the dynamic random access memory.
[0197] Specifically, the firmware image file items other than the partition table image file item, the bootloader firmware image file item, and the recovery mode firmware image file item are sequentially searched from the image file item list in the upgrade image header. For each firmware image file item to be processed, data with a length equal to the size of the image file in the firmware image file item to be processed is read from the "starting address of the firmware storage area to be upgraded in the metadata of the firmware storage area to be upgraded + offset address of the image file in the firmware image file item to be processed in the firmware storage area to be upgraded" and used as the reserved space for the firmware image file data to be processed in the dynamic random access memory.
[0198] (4) Write the data of other firmware image files in the reserved space to the address space of the corresponding firmware partition in the new partition table.
[0199] Specifically, the firmware image file data to be processed in the reserved space is written into the address space of the corresponding firmware partition in the new partition table.
[0200] (5) Clear the metadata of the firmware storage area to be upgraded, free area, block list description file backup area, boot boot program firmware partition backup area, recovery mode firmware partition backup area, metadata partition backup area, temporary partition backup area and boot communication partition backup area in the current partition table.
[0201] (6) Clear the upgrade instruction information in the first preset position of the current partition table.
[0202] (7) Write a preset upgrade completion mark in the second preset position of the boot communication partition in the current partition table.
[0203] (8) Restart the device to access the upgraded system.
[0204] In step 213, if the upgrade instruction information in the first preset location of the boot communication partition corresponds to the sixth preset upgrade instruction, then the firmware partition table is obtained from the address space of the device storage partition table, and the firmware partition table is used as the current partition table; the first partition after the preset partition list in the current partition table is used as the firmware storage area to be upgraded. The firmware storage area to be upgraded is not the preset encrypted user data partition. The preset partition list includes the boot program firmware partition, the recovery mode firmware partition, the boot communication partition, the metadata partition, and the temporary storage partition; the starting address of the partition of the firmware storage area to be upgraded is written to the command line parameter; the boot recovery mode firmware is loaded, and the command line parameter is passed to the recovery mode firmware; the recovery mode firmware is run.
[0205] According to another aspect of the invention, Figure 3 This is a schematic diagram illustrating an electronic device 300 according to an embodiment of the present invention. (Refer to...) Figure 3 The electronic device 300 includes a memory 302 and a processor 304. The memory 302 is configured to store a computer program that can run on the processor 304. When the processor 304 executes the computer program, it implements the various steps of the firmware upgrade method as described above.
[0206] In summary, the firmware upgrade method and electronic device provided by this invention back up data, including the metadata partition, according to a first preset upgrade instruction and a second preset upgrade instruction. When the address space of the metadata partition differs between the current partition table and the new partition table, the metadata partition firmware in the backup area of the metadata partition is written to the address space of the metadata partition in the new partition table according to a third preset upgrade instruction. This ensures that even if the address space of the metadata partition containing the user data partition encryption key changes before and after the upgrade, the data in the user data partition can be retained after the upgrade. Furthermore, since the boot communication partition, metadata partition, bootloader firmware partition, recovery mode firmware partition, and temporary storage partition have been backed up, even if problems occur during the upgrade process... Unexpected power outages will not damage the data in the backup partition, ensuring that no unexpected events during the upgrade process will brick the device, thus improving the reliability of firmware upgrades. Furthermore, the data in the encrypted user data partition is retained after the upgrade. Additionally, since the bootloader firmware partition, recovery mode firmware partition, metadata partition, temporary storage partition, and boot communication partition in the current partition table all point to their corresponding backup partitions, and the firmware storage area to be upgraded and all backup partitions are located after the bootloader firmware partition, recovery mode firmware partition, boot communication partition, metadata partition, and temporary storage partition in the old and new partition tables, even if an unexpected power outage occurs during subsequent upgrades, the data in the backup partitions will not be damaged, and the upgrade can continue after power is restored.
[0207] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A method of upgrading firmware, characterized by, The application comprises: In the startup boot firmware of the device, read the upgrade instruction information in the first preset position for saving the upgrade instruction in the boot communication partition; In the first stage corresponding to the first preset upgrade instruction, determine the command line parameters based on the partition starting address of the firmware storage area to be upgraded, the size of the preset temporary partition, the initial size of the data to be backed up, the head size of the data to be backed up, and the starting address of the preset encrypted user data partition, and pass the command line parameters to the recovery mode firmware, wherein the data to be backed up includes the boot communication partition, the metadata partition, the startup boot firmware partition, and the recovery mode firmware partition; In the second stage corresponding to the second preset upgrade instruction, obtain the firmware partition table from the address space of the device storage partition table, and use the firmware partition table as the current partition table. After backing up the data to be backed up and the temporary partition, update the current partition table so that the startup boot firmware partition, the recovery mode firmware partition, the metadata partition, the temporary partition, and the boot communication partition in the current partition table point to the corresponding backup partition respectively, and upgrade the updated current partition table to the address space where the main partition table and the backup partition table are located in the storage medium; In the third stage corresponding to the third preset upgrade instruction, obtain the partition table file from the firmware storage area to be upgraded as a new partition table according to the firmware storage area to be upgraded metadata, upgrade the startup boot firmware partition and the recovery mode firmware partition, and write the metadata partition firmware in the metadata partition backup area to the address space of the metadata partition in the new partition table when the address spaces of the metadata partition in the current partition table and the new partition table are different; And In the fourth stage corresponding to the fourth preset upgrade instruction, upgrade the firmware other than the startup boot firmware and the recovery mode firmware, including sequentially obtaining the firmware image file data other than the startup boot firmware image file data and the recovery mode firmware image file data from the firmware storage area to be upgraded according to the firmware storage area to be upgraded metadata, and saving all the other firmware image file data to the reserved space of the dynamic random access memory. The address spaces of the startup boot firmware partition, the boot communication partition, and the recovery mode firmware partition in the firmware partition table are located before the address spaces of various system firmware partitions and the preset encrypted user data partition. The metadata partition is a user-readable and writable non-encrypted file system partition for storing the encryption key of the user data partition. The temporary partition is a user-readable and writable non-encrypted file system partition for storing files generated during system operation. The boot communication partition is a bare partition for communication between system firmware, startup boot firmware, and upgrade mode firmware.
2. The method of claim 1, wherein, The application further comprises: Saving the firmware upgrade package to be upgraded to the preset path of the user data partition. A block list occupied by the firmware upgrade package is generated, block data in the block list is decrypted, the decrypted block data is written back to the corresponding block, and the corresponding block list is saved to a block list description file in a preset path of the temporary partition; and The first preset upgrade instruction is written in the first preset position in the boot communication partition, and then the device is restarted.
3. The method of claim 2, wherein, Further comprising: In the recovery mode firmware, the upgrade instruction information in the first preset position in the boot communication partition is read; If the upgrade instruction information corresponds to the first preset upgrade instruction, the temporary partition is mounted; The firmware upgrade package in the preset path of the encrypted user data partition is read according to the block list description file in the preset path of the temporary partition, and all files read from the firmware upgrade package are saved to the to-be-upgraded firmware storage area according to a preset storage structure; The to-be-upgraded firmware storage area meta information is constructed and written to the ninth preset position in the boot communication partition, and the free area meta information is constructed and written to the third preset position in the boot communication partition; and The second preset upgrade instruction is written in the first preset position in the boot communication partition, and then the device is restarted.
4. The method of claim 3, wherein, Further comprising: If the current partition table has pointed to the backup partition, the current upgrade instruction is set to the third preset upgrade instruction; and The third preset upgrade instruction is written in the first preset position in the boot communication partition of the current partition table.
5. The method of claim 4, wherein, Further comprising: The boot recovery mode firmware is loaded, and a command line parameter is passed to the recovery mode firmware; The current temporary partition is formatted, and a new empty block list description file is created in the preset path of the temporary partition; Data with a size of the block list description file size in the block list description file backup header is read from the block list description file backup header start address and saved to the new block list description file in the preset path of the temporary partition; and The fourth preset upgrade instruction is written in the first preset position in the boot communication partition, and then the device is restarted.
6. The method of claim 4, wherein, Further comprising: The boot recovery mode firmware is loaded, and a command line parameter is passed to the recovery mode firmware; The firmware upgrade package in the preset path of the encrypted user data partition is read according to the block list description file in the preset path of the temporary partition, and all files read from the firmware upgrade package are saved to the to-be-upgraded firmware storage area according to a preset storage structure; The to-be-upgraded firmware storage area meta information is constructed and written to the ninth preset position in the boot communication partition; and The fourth preset upgrade instruction is written in the first preset position in the boot communication partition, and then the device is restarted.
7. The method of claim 4, wherein, Further comprising: If there is no legal block list description file backup area meta information and block list description file backup area, the current upgrade instruction is set to the fourth preset upgrade instruction; and The fourth preset upgrade instruction is written in the first preset position in the boot communication partition of the current partition table.
8. The method of claim 1, wherein, Further comprising: Constructing a firmware upgrade package, the firmware upgrade package including all firmware image files to be upgraded and a partition table file with preset names including a startup bootloader firmware image file and a recovery mode firmware image file.
9. The method of claim 1, wherein, Further comprising: determining whether a preset upgrade completion marker exists in a second preset location of the boot communication partition; if so, outputting upgrade success information, deleting the firmware upgrade package under the preset path of the preset encrypted user data partition, and clearing the upgrade completion marker in the second preset location of the boot communication partition; if not, periodically determining whether a new firmware upgrade package to be upgraded exists; if so, obtaining the firmware upgrade package to be upgraded and saving the firmware upgrade package to be upgraded under the preset path of the preset encrypted user data partition; performing a legality check on the firmware upgrade package to be upgraded; if the legality check is illegal, deleting the firmware upgrade package under the preset path of the preset encrypted user data partition and exiting the firmware upgrade process; if the legality check is legal, generating a block list occupied by the firmware upgrade package under the preset path of the preset encrypted user data partition through a preset decryption service and decrypting block data in the block list to obtain decrypted data; re-writing the decrypted data into corresponding blocks and saving the corresponding block list into a block list description file under the preset path of the staging partition; and writing the first preset upgrade instruction into the first preset location of the boot communication partition and restarting the device. Determine command line parameters based on a partition start address of a firmware storage area to be upgraded, a size of a preset staging partition, an initial size of data to be backed up, a header size of the data to be backed up, and a start address of a preset encrypted user data partition, including:
10. The method of claim 1, wherein, taking a first partition after a preset partition list in the current partition table as the firmware storage area to be upgraded, the firmware storage area to be upgraded not being the preset encrypted user data partition, the preset partition list including the startup bootloader firmware partition, the recovery mode firmware partition, the boot communication partition, the metadata partition, and the staging partition; writing the partition start address of the firmware storage area to be upgraded into the command line parameters; obtaining the size of the preset staging partition from the current partition table and writing the size of the preset staging partition into the command line parameters; obtaining the size of the boot communication partition, the size of the metadata partition, the size of the startup bootloader firmware partition, and the size of the recovery mode firmware partition from the current partition table respectively, adding the size of the boot communication partition, the size of the metadata partition, the size of the startup bootloader firmware partition, and the size of the recovery mode firmware partition to obtain an initial size of data to be backed up; writing the initial size of the data to be backed up into the command line parameters; Adding the backup header of the boot communication partition of the preset size, the backup header of the metadata partition of the preset size, the backup header of the startup bootloader firmware partition of the preset size and the backup header of the recovery mode firmware partition of the preset size to obtain the header size of the data to be backed up, and writing the header size of the data to be backed up into the command line parameter; and Obtaining the starting address of the preset encrypted user data partition from the current partition table, and writing the starting address of the preset encrypted user data partition into the command line parameter.
11. The method of claim 1, wherein, After the backup of the data to be backed up and the temporary partition, updating the current partition table so that the startup bootloader firmware partition, the recovery mode firmware partition, the metadata partition, the temporary partition and the boot communication partition in the current partition table respectively point to the corresponding backup partition, and upgrading the updated current partition table to the address space where the main partition table and the backup partition table in the storage medium are located. Obtaining the firmware partition table from the address space of the device storage partition table, and taking the firmware partition table as the current partition table; Obtaining the partition table file from the firmware storage area to be upgraded according to the firmware storage area meta information, and taking the partition table file as the new partition table; Setting the initial value of the flag indicating that the current partition table has pointed to the backup area to false; Judging whether the backup of all backup areas including the startup bootloader firmware backup area, the recovery mode firmware backup area, the metadata backup area, the temporary backup area and the boot communication backup area has been completed, and whether the startup bootloader firmware partition, the recovery mode firmware partition, the metadata partition, the temporary partition and the boot communication partition in the current partition table have all respectively pointed to the corresponding backup partition; If yes, setting the flag indicating that the current partition table has pointed to the backup area to true; Judging whether the flag indicating that the current partition table has pointed to the backup area is true; If yes, writing the third preset upgrade instruction into the first preset position of the boot communication partition; and If no, after the backup of the startup bootloader firmware partition, the recovery mode firmware partition, the metadata partition, the temporary partition and the boot communication partition, updating the current partition table so that the startup bootloader firmware partition, the recovery mode firmware partition, the metadata partition, the temporary partition and the boot communication partition in the current partition table respectively point to the corresponding backup partition, and upgrading the updated current partition table to the address space where the main partition table and the backup partition table in the storage medium are located.
12. The method of claim 11, wherein, After the backup of the startup boot program firmware partition, the recovery mode firmware partition, the metadata partition, the staging partition and the boot communication partition, the current partition table is updated so that the startup boot program firmware partition, the recovery mode firmware partition, the metadata partition, the staging partition and the boot communication partition in the current partition table respectively point to the corresponding backup partitions, and the updated current partition table is upgraded to the address space where the main partition table and the backup partition table are located in the storage medium, including: the starting address and the partition size of the new recovery mode firmware are obtained from the new partition table as the starting address of the new recovery mode firmware and the partition size of the new recovery mode firmware; the free area meta information in the third preset position of the boot communication partition is read, and it is judged whether the magic number in the free area meta information is a preset magic number; if not, the firmware upgrade process is exited; if yes, it is judged whether the address obtained by adding the starting address of the new recovery mode firmware and the partition size of the new recovery mode firmware is greater than the starting address of the free area in the free area meta information; if greater, the firmware upgrade process is exited; if not greater, the startup boot program firmware partition is backed up to the startup boot program firmware partition backup area, and startup boot program firmware partition backup area meta information is written in the fourth preset position of the boot communication partition in the current partition table; the recovery mode firmware partition is backed up to the recovery mode firmware partition backup area, and recovery mode firmware partition backup area meta information is written in the fifth preset position of the boot communication partition in the current partition table; it is judged whether the address spaces of the metadata partition in the current partition table and the new partition table are the same; if different, the metadata partition is backed up to the metadata partition backup area, and metadata partition backup area meta information is written in the sixth preset position of the boot communication partition in the current partition table; it is judged whether the address spaces of the staging partition in the current partition table and the new partition table are the same; if different, the staging partition is backed up to the staging partition backup area, and staging partition backup area meta information is written in the seventh preset position of the boot communication partition in the current partition table; it is judged whether the address spaces of the boot communication partition in the current partition table and the new partition table are the same; if different, the boot communication partition is backed up to the boot communication partition backup area, and boot communication partition backup area meta information is written in the eighth preset position of the boot communication partition in the current partition table, and the boot communication partition backup area meta information is written in the preset position for saving boot communication partition backup area meta information in the boot communication partition firmware in the boot communication partition backup area; the starting address of the startup boot program firmware partition in the current partition table is set as the startup boot program firmware starting address in the startup boot program firmware partition backup header; setting a start address of the recovery mode firmware partition in the current partition table to a recovery mode firmware start address in a recovery mode firmware partition backup header; setting a start address of the metadata partition in the current partition table to a metadata partition firmware start address in a metadata partition backup header when the metadata partition is in different address spaces in the current partition table and the new partition table; setting a start address of the staging partition in the current partition table to a staging partition firmware start address in a staging partition backup header when the staging partition is in different address spaces in the current partition table and the new partition table; setting a start address of the boot communication partition in the current partition table to a boot communication partition firmware start address in a boot communication partition backup header when the boot communication partition is in different address spaces in the current partition table and the new partition table; writing the updated free area meta information to the third preset position of the boot communication partition in the updated current partition table, and upgrading the updated current partition table to the address space where the main partition table is located and the address space where the backup partition table is located in the storage medium; and writing a third preset upgrade instruction to the first preset position of the boot communication partition.
13. The method of claim 1, wherein, upgrading the startup boot program firmware partition and the recovery mode firmware partition, and writing metadata partition firmware in the metadata partition backup area to an address space where a metadata partition is located in the new partition table when the metadata partition is in different address spaces in the current partition table and the new partition table includes: obtaining a firmware partition table from an address space of a device storage partition table, and taking the firmware partition table as a current partition table; obtaining a partition table file from a to-be-upgraded firmware storage area as a new partition table according to firmware storage area meta information in the to-be-upgraded firmware storage area; if the current partition table and the new partition table are different, obtaining startup boot program firmware image file data from the to-be-upgraded firmware storage area according to the firmware storage area meta information, and writing the startup boot program firmware image file data to an address space where a startup boot program firmware partition is located in the new partition table; obtaining recovery mode firmware image file data from the to-be-upgraded firmware storage area according to the firmware storage area meta information, and writing the recovery mode firmware image file data to an address space where a recovery mode firmware partition is located in the new partition table; when the metadata partition is in different address spaces in the current partition table and the new partition table, writing metadata partition firmware in the metadata partition backup area to an address space where a metadata partition is located in the new partition table; when the staging partition is in different address spaces in the current partition table and the new partition table, determining whether a size of a staging partition in the new partition table is greater than or equal to a size of a staging partition in the current partition table; if yes, writing staging partition firmware in a staging partition backup area to an address space where a staging partition is located in the new partition table; when the address space of the boot communication partition in the current partition table and the address space of the boot communication partition in the new partition table are different, writing all boot communication partition data in the current partition table into the address space of the boot communication partition in the new partition table; upgrading the new partition table to the address space of the main partition table and the address space of the backup partition table in the storage medium, and taking the new partition table as the current partition table; if the current partition table and the new partition table are the same, judging whether there is a legal block list description file backup area meta information in the boot communication partition of the current partition table and whether there is a legal block list description file backup header in the block list description file backup area; if both exist, writing a fifth preset upgrade instruction into the first preset position of the boot communication partition; and if not both exist, writing a fourth preset upgrade instruction into the first preset position of the boot communication partition.
14. The method of claim 1, wherein, the upgrading of the other firmware except the startup boot program firmware and the recovery mode firmware comprises: obtaining a firmware partition table from the address space of the device storage partition table, and taking the firmware partition table as the current partition table; reading the to-be-upgraded firmware storage area meta information from a ninth preset position in the boot communication partition of the current partition table, and judging whether the magic number in the to-be-upgraded firmware storage area meta information is a preset magic number; if not, exiting the firmware upgrade process; if yes, reading a preset size of upgrade image header from a to-be-upgraded firmware storage area start address in the to-be-upgraded firmware storage area meta information; judging whether the magic number of the upgrade image header is a preset magic number; if not the preset magic number, writing a sixth preset upgrade instruction into the first preset position of the boot communication partition; if the preset magic number, sequentially obtaining, according to the to-be-upgraded firmware storage area meta information, other firmware image file data except the startup boot program firmware image file data and the recovery mode firmware image file data from the to-be-upgraded firmware storage area, and saving all the other firmware image file data to a reserved space of the dynamic random access memory; writing the other firmware image file data in the reserved space to the address space of the corresponding firmware partition in the new partition table; clearing the to-be-upgraded firmware storage area meta information, the idle area meta information, the block list description file backup area meta information, the startup boot program firmware partition backup area meta information, the recovery mode firmware partition backup area meta information, the meta data partition backup area meta information, the temporary storage partition backup area meta information and the boot communication partition backup area meta information in the boot communication partition of the current partition table; clearing the upgrade instruction information in the first preset position of the current partition table; writing a preset upgrade completion mark in a second preset position in the boot communication partition of the current partition table; and restarting the device to enter the upgraded system.
15. The method of claim 1, wherein, further comprising: if the upgrade instruction information in the first preset position of the boot communication partition corresponds to the fifth preset upgrade instruction, loading and booting the recovery mode firmware, and running the recovery mode firmware.
16. The method of claim 1, wherein, further comprising: If the upgrade instruction information in the first preset position of the boot communication partition corresponds to the sixth preset upgrade instruction, a firmware partition table is obtained from the address space of the device storage partition table, and the firmware partition table is taken as a current partition table; a first partition after a preset partition list in the current partition table is taken as a to-be-upgraded firmware storage area, the to-be-upgraded firmware storage area is not a preset encrypted user data partition, and the preset partition list includes the boot program firmware partition, the recovery mode firmware partition, the boot communication partition, the metadata partition, and the staging partition; a partition start address of the to-be-upgraded firmware storage area is written into the command line parameter; the boot recovery mode firmware is loaded, and the command line parameter is passed to the recovery mode firmware; and the recovery mode firmware is run.
17. The method according to claim 15 or 16, characterized in that, Running the recovery mode firmware includes: reading the upgrade instruction information in the first preset position of the boot communication partition; determining whether the upgrade instruction information is a first preset upgrade instruction, a fifth preset upgrade instruction, or a sixth preset upgrade instruction; if not, exiting the firmware upgrade process; if so, taking the upgrade instruction information as a current upgrade instruction; if the current upgrade instruction is the first preset upgrade instruction, mounting the staging partition, and reading a firmware upgrade package in a preset path of a preset encrypted user data partition according to a block list description file in the preset path of the staging partition; reading all files in the firmware upgrade package, and saving the all files to the to-be-upgraded firmware storage area according to a preset storage structure; if a new staging partition is smaller than an old staging partition, backing up a block list description file in a preset path of the staging partition to a block list description file backup area, constructing block list description file backup area metadata, and writing the block list description file backup area metadata to a tenth preset position of the boot communication partition; constructing to-be-upgraded firmware storage area metadata, and writing the to-be-upgraded firmware storage area metadata to a ninth preset position of the boot communication partition; constructing free area metadata, and writing the free area metadata to a third preset position of the boot communication partition; writing a second preset upgrade instruction to the first preset position of the boot communication partition, and restarting the device; if the current upgrade instruction is the fifth preset upgrade instruction, formatting and mounting the current staging partition; reading block list description file backup area metadata from the tenth preset position of the boot communication partition, and determining whether a magic number in the block list description file backup area metadata is a preset block list description file backup area metadata magic number; if so, reading a block list description file backup header of a preset size from a start address of a block list description file backup area in the block list description file backup area metadata; If the magic number in the backup header of the block list description file is the preset magic number, a new empty block list description file is created under a preset path of the staging partition, and data with a size of the block list description file size in the block list description file backup header is read from a block list description file start address in the block list description file backup header and saved to the empty block list description file; a fourth preset upgrade instruction is written in a first preset position of the boot communication partition, and the device is restarted; If the current upgrade instruction is the sixth preset upgrade instruction, a partition start address of the to-be-upgraded firmware storage area is obtained from the command line parameter as a to-be-upgraded firmware storage area start address, and the staging partition is mounted; a firmware upgrade package under a preset path of a preset encrypted user data partition is read according to a block list description file under a preset path of the staging partition, and all files in the firmware upgrade package are read; the all files are saved to the to-be-upgraded firmware storage area according to a preset storage structure; to-be-upgraded firmware storage area meta information is constructed, and the to-be-upgraded firmware storage area meta information is written into a ninth preset position of the boot communication partition; and a fourth preset upgrade instruction is written in a first preset position of the boot communication partition, and the device is restarted.
18. An electronic device, comprising: comprise: a memory configured to store an executable program; and a processor configured to execute the program to perform the method according to any one of claims 1 to 17.
Citation Information
Patent Citations
Firmware upgrading method and terminal
CN109634645A
Firmware upgrading method, electronic equipment and storage medium
CN116974617A