Firmware upgrading method and electronic device
By detecting changes in the boot communication partition address and setting a priority mode, backing up the boot communication partition, and storing parameters using environment variables, the problem of data loss and excessive space requirements caused by partition table changes in existing technologies is solved, enabling a flexible firmware upgrade process.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- FUZHOU ROCKCHIP SEMICON
- Filing Date
- 2026-04-10
- Publication Date
- 2026-07-31
AI Technical Summary
Existing firmware upgrade methods cannot automatically retain data in user data partitions and other readable and writable partitions when the partition table changes arbitrarily, and backups need to be performed in the upgrade mode firmware, which increases the space requirements.
By determining whether the address of the boot communication partition in the partition table to be updated is different from the current partition table, and setting the boot loader firmware priority mode when necessary, backing up the boot communication partition, and using environment variables to store upgrade parameters, the system avoids directly updating the boot communication partition, thus ensuring the normal update of the boot loader firmware.
It enables automatic adjustment of the upgrade order when the partition table structure is unrestricted, avoiding problems such as data loss and excessive space requirements, and ensuring the success and integrity of the firmware upgrade process.
Smart Images

Figure CN122489338A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of embedded technology, and more particularly to firmware upgrade methods and electronic devices. Background Technology
[0002] Existing firmware upgrade methods that support arbitrary partition table changes typically have specific requirements regarding the position of certain partitions in the partition table, such as requiring the last partition in the partition table to be a user data partition. Furthermore, they usually cannot automatically retain data in user data partitions and other read / write partitions when their address spaces change. Additionally, existing firmware upgrade methods that support arbitrary partition table changes typically require backing up the upgrade mode firmware if the upgrade operation needs to be performed within the upgrade mode firmware itself. This is to address the issue of anomalies occurring during the upgrade of the upgrade mode firmware itself, preventing re-entry into upgrade mode, which increases the space requirements. Summary of the Invention
[0003] This invention provides a firmware upgrade method and apparatus, and an electronic device, which can realize a firmware upgrade process without limiting the partition table structure.
[0004] In one aspect of the present invention, a firmware upgrade method is provided. The method includes: obtaining a firmware upgrade package, the firmware upgrade package being constructed to include a partition table file to be updated and a target bootloader firmware; if it is determined that a first address space of the boot communication partition in the partition table to be updated is different from a second address space of the boot communication partition in the current partition table, and if it is checked that the first address space overwrites or corrupts a third address space of the bootloader firmware partition in the current partition table, then setting the update mode to a bootloader firmware priority mode; backing up the boot communication partition in the current partition table, backing up the parameters required to upgrade the target bootloader firmware in environment variables, obtaining the parameters from the environment variables to complete the upgrade of the target bootloader firmware, and then completing the update of the boot communication partition in the partition table to be updated; and updating the current partition table to the partition table to be updated.
[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 firmware upgrade method.
[0006] According to the technical solution of the present invention, if it is determined that there is a partition table to be updated in the firmware upgrade package, and the first address space of the boot communication partition in the partition table to be updated is different from the second address space of the boot communication partition in the current partition table, it indicates that the address of the boot communication partition will change during the firmware upgrade package upgrade process. Further, it is determined whether the changed address of the boot communication partition in the partition table to be updated will damage the third address space of the bootloader firmware partition in the current partition table. If it will damage the third address space, it indicates that updating the boot communication partition first during the firmware upgrade package update process may cause the bootloader firmware to fail to boot. Therefore, the update mode is set to bootloader firmware priority mode. During the upgrade process, the boot communication partition in the current partition table is backed up so that it can be retrieved after the update fails. At the same time, the parameters required by the target bootloader firmware in the upgrade firmware package are backed up in the environment variables. The parameters are retrieved from the environment variables to complete the upgrade of the target bootloader firmware. In this way, the bootloader firmware is updated without updating the boot communication partition. After the update is successful, the boot communication partition is updated to avoid the boot communication partition damaging the current bootloader firmware, allowing normal entry into the subsequent upgrade process. Attached Figure Description
[0007] Figure 1 This is a flowchart of a firmware upgrade method according to an embodiment of the present invention; Figure 2 This is a flowchart illustrating the application of the firmware upgrade method of this invention in a real-world scenario. Figure 3 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0008] 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.
[0009] In existing technologies, the partition table usually remains unchanged during firmware updates. Even if changes are allowed, there are special requirements for the location of certain partitions, which limits the flexibility of firmware upgrade package configuration.
[0010] To address at least the aforementioned technical problems, this disclosure provides a firmware upgrade method. According to this disclosure, the upgrade process is determined based on whether the address of the boot communication partition in the partition table to be updated has changed, and whether the change affects the result of the bootloader firmware partition in the current partition table. If there is no impact, the boot communication partition in the partition table to be updated can be updated first, and then the target bootloader firmware can be upgraded. If there is an impact, the target bootloader firmware needs to be upgraded first, and then the boot communication partition can be updated. In this way, embodiments of this disclosure can automatically change the update order of the boot communication partition in the partition table to be updated and the target bootloader firmware based on the correspondence between the address of the boot communication partition in the partition table to be updated and the addresses of the boot communication partition and the bootloader firmware partition in the current partition table, without restricting the partition addresses in the partition table to be updated, without limiting the partition addresses in the partition table to be updated.
[0011] 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.
[0012] Figure 1 This is a flowchart illustrating a firmware upgrade method 100 according to an embodiment of the present disclosure. (Refer to...) Figure 1 The method 100 includes the following steps 102 to 106.
[0013] In step 102, a firmware upgrade package is obtained, which is constructed to include a partition table to be updated and a target bootloader firmware.
[0014] In step 104, if it is determined that the first address space of the boot communication partition in the partition table to be updated is different from the second address space of the boot communication partition in the current partition table, and if it is checked that the first address space overwrites or destroys the third address space of the boot loader firmware partition in the current partition table, then the update mode is set to boot loader firmware priority mode.
[0015] In some embodiments, the method 100 may further include: when an instruction to upgrade the firmware upgrade package is detected, if the first address space is different from the second address space, then a boot communication partition change flag is set to true; if the first address space is the same as the second address space, then a boot communication partition change flag is set to no; if the boot communication partition change flag is no, then the parameters are directly obtained from the current boot communication partition to complete the upgrade of the target bootloader firmware; if the boot communication partition change flag is true and the update mode is not the bootloader firmware priority mode, then after completing the update of the boot communication partition in the partition table to be updated, the parameters are obtained from the current boot communication partition to complete the upgrade of the target bootloader firmware.
[0016] In this way, if the boot communication partition address remains unchanged, there is no need to update the boot communication partition; the parameters can be directly obtained from the current boot communication partition to complete the upgrade of the target bootloader firmware. If the boot communication partition address changes, but the change does not affect the current bootloader firmware partition in the current partition table, the normal upgrade process can be followed: first update the boot communication partition in the partition table to be updated, and then update the target bootloader firmware. This way, the update will not fail due to a conflict between the partition address in the partition table to be updated and the partition address in the current partition table.
[0017] In some embodiments, when an instruction to upgrade the firmware upgrade package is detected, a second-stage upgrade instruction is written to a first preset location in the boot communication partition and the terminal is restarted.
[0018] In this way, by writing the second-stage instruction, the terminal enters the firmware upgrade state. Even if the terminal loses power during the upgrade process, it can still know the current execution stage of the upgrade process by reading the first preset location of the boot communication partition, and thus enter the corresponding upgrade process, avoiding the problem that the terminal cannot continue to start due to unexpected stop during the upgrade process.
[0019] In some embodiments, the method 100 may further include: when an instruction to upgrade the firmware upgrade package is detected, writing a first-stage upgrade instruction in a first preset location of the boot communication partition and restarting the terminal; reading the current upgrade instruction in the first preset location, and if the current upgrade instruction is the first-stage upgrade instruction, setting the current upgrade instruction to a second-stage upgrade instruction and writing the second-stage upgrade instruction into the first preset location.
[0020] In this way, the first-stage upgrade command is set when entering upgrade mode, and the second-stage upgrade command is set when the first-stage upgrade command is detected to enter the formal upgrade process, making the execution hierarchy of the steps clearer.
[0021] In some embodiments, the method 100 may further include: when an instruction to upgrade the firmware upgrade package is detected, constructing initial upgrade metadata based on first information of the firmware upgrade package, second information of the external storage medium, third information of the terminal itself, and fourth information configured by the user, and storing the initial upgrade metadata in a second preset location of the boot communication partition; reading the current upgrade instruction in the first preset location, and if the current upgrade instruction is a second-stage upgrade instruction, guiding the terminal to enter the current upgrade mode firmware; reading the initial upgrade metadata from the second preset location through the current upgrade mode firmware, and obtaining the firmware image file according to the firmware upgrade package path in the first information. The process involves: creating second-stage upgrade metadata, configuring a preset image file path and an image file information list within the second-stage upgrade metadata; setting the boot communication partition change flag and the update mode within the second-stage upgrade metadata, wherein the image file information list includes multiple image file information items; saving the firmware image file to the preset image file path and assigning an image file information item to each firmware image file; saving the partition table to be updated to the preset image file path and assigning an image file information item to the partition table to be updated; and setting the current upgrade instruction to the third-stage upgrade instruction and writing the third-stage upgrade instruction to the first preset location.
[0022] In this way, the image file in the firmware upgrade package is matched with an image file information item separately to facilitate the subsequent search for the corresponding image file. An image file information item is also assigned to the partition table to be updated, which can also facilitate the search in subsequent steps. At the same time, the initial upgrade metadata and the second-stage upgrade metadata are constructed, and the parameters required for the firmware upgrade package upgrade process are saved separately for direct use in subsequent steps.
[0023] In some embodiments, the method 100 may further include: when creating second-stage upgrade metadata, configuring a list of readable and writable partitions to be backed up in the second-stage upgrade metadata; reading the current upgrade instruction in the first preset location; if the current upgrade instruction is the third-stage upgrade instruction, obtaining a backup flag in the first information; if the backup flag is true, reading the list of readable and writable partitions to be backed up, creating a backup image file with the partition name of the partition to be backed up in the list of readable and writable partitions to be backed up under the preset image file path, backing up the corresponding partition to be backed up to the backup image file according to the current partition table, and allocating an image file information item for each backup image file; if the boot communication partition change flag is true, creating a backup boot communication partition image file with the partition name of the current boot communication partition as the file name under the preset image file path, and backing up the current boot communication partition to the backup boot communication partition image file according to the current partition table; and setting the current upgrade instruction to a fourth-stage upgrade instruction and writing the fourth-stage upgrade instruction to the first preset location.
[0024] In this way, if the backup flag is true, it means that there is a list of readable and writable partitions to be backed up. Then, each partition recorded in the list of readable and writable partitions to be backed up is backed up in the preset image file path. In this way, after the firmware upgrade package is completed, the data in the list of readable and writable partitions to be backed up can be retained and the data loss can be avoided.
[0025] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is a fourth-stage upgrade instruction, determining whether the update mode is the bootloader firmware priority mode; if yes, setting the current upgrade instruction to a fifth-stage upgrade instruction and writing the fifth-stage upgrade instruction into the first preset location; otherwise, determining whether the boot communication partition change flag is true; if the boot communication partition change flag is no, setting the current upgrade instruction to a fifth-stage upgrade instruction and writing the fifth-stage upgrade instruction into the first preset location; and if the boot communication partition change flag is true, and the update mode is not the bootloader firmware priority mode, updating the boot communication partition to be updated in the firmware upgrade package according to the partition table to be updated, setting the first starting address of the boot communication partition in the current partition table to the second starting address of the boot communication partition in the partition table to be updated, and setting the first partition size of the boot communication partition in the current partition table; and setting the current upgrade instruction to a fifth-stage upgrade instruction and writing the fifth-stage upgrade instruction into the first preset location.
[0026] In this approach, if the update mode prioritizes the bootloader firmware, it means that the boot communication partition in the partition table to be updated will affect the bootloader firmware partition in the current partition table, and therefore cannot be updated first. If the boot communication partition change marker is "No," it means the boot communication partition does not need to be updated. If the boot communication partition change marker is "No" and the update mode is not prioritized for the bootloader firmware partition, it means that updating the boot communication partition in the partition table to be updated will not affect the bootloader firmware partition in the current partition table, and the update of the boot communication partition can be completed first. This method adapts the update process to different situations without restricting the configuration in the partition table to be updated.
[0027] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is a fifth-stage upgrade instruction, backing up the current bootloader firmware partition in the current partition table to the bootloader firmware backup partition in the partition table to be updated, setting the starting address of the current bootloader firmware partition to the third starting address of the bootloader firmware backup partition, and setting the partition size of the current bootloader firmware partition to the third partition size of the bootloader firmware backup partition; if the boot communication partition change flag is no or the update mode is not bootloader firmware... In priority mode, the current upgrade command is set to the sixth-stage upgrade command, and the sixth-stage upgrade command is written to the first preset location; if the update mode is the bootloader firmware priority mode, a boot communication partition backup image file is created under the preset image file path, with the partition name of the current boot communication partition in the current partition table as the file name, and the current boot communication partition is backed up to the boot communication partition backup image file according to the current partition table; and the parameters required to upgrade the target bootloader firmware are backed up in the environment variables, the current upgrade command is set to the sixth-stage upgrade command, and the sixth-stage upgrade command is written to the first preset location.
[0028] In this way, if the update mode prioritizes the bootloader firmware, since the bootloader firmware partition in the current partition table overlaps with the boot communication partition in the partition table to be updated, the boot communication partition is backed up to a preset image file path to avoid unexpected situations during the overwrite process. This way, even if the boot communication partition is damaged, it can be restored and the firmware upgrade package upgrade process can continue.
[0029] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is a sixth-stage upgrade instruction, determining whether the boot communication partition change flag is true; if not, obtaining the parameters from the current boot communication partition to complete the upgrade of the target bootloader firmware; if the boot communication partition change flag is true and the update mode is not the bootloader firmware priority mode, obtaining the parameters from the current boot communication partition to complete the upgrade of the target bootloader firmware; if the update mode is the bootloader firmware priority mode, obtaining the parameters from the environment variables to complete the upgrade of the target bootloader firmware; and setting the current upgrade instruction to a seventh-stage upgrade instruction, and writing the seventh-stage upgrade instruction into the first preset location.
[0030] In this way, if the boot communication partition remains unchanged, the corresponding parameters can be directly obtained from the boot communication partition to complete the upgrade of the target bootloader firmware. If the boot communication partition changes but does not affect the bootloader firmware partition in the current partition table, the corresponding parameters can also be directly obtained from the boot communication partition because the boot communication partition has been updated during the execution of the fourth-stage upgrade instruction. For the update mode that prioritizes the bootloader firmware, since the boot communication partition has changed but has not yet been updated, the pre-stored parameters are obtained from the environment variables to complete the upgrade of the target bootloader firmware. Different upgrade processes are automatically matched according to different upgrade requirements to complete the upgrade process without controlling the configuration in the partition table to be updated.
[0031] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is the seventh stage upgrade instruction, determining whether the update mode is the bootloader firmware priority mode; if so, obtaining the boot communication partition image file from the preset image file path, writing the boot communication partition image file into the fourth starting address of the boot communication partition in the current partition table, setting the current upgrade instruction to the eighth stage upgrade instruction, and writing the eighth stage upgrade instruction into the first preset location; otherwise, directly setting the current upgrade instruction to the eighth stage upgrade instruction and writing the eighth stage upgrade instruction into the first preset location.
[0032] In this way, when the update mode is set to bootloader firmware priority mode, the boot communication partition has not yet been updated when the target bootloader firmware is updated. Therefore, the boot communication partition is updated after the target bootloader firmware is updated to avoid data errors.
[0033] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is an eighth-stage upgrade instruction, determining the format of the firmware upgrade package; if the firmware upgrade package is a non-Android format, obtaining other image files to be updated besides the image file of the target bootloader firmware and the image file of the partition table to be updated according to the preset image file path, writing the image files to be updated into the preset location of the partition table to be updated, setting the current upgrade instruction to a ninth-stage upgrade instruction, and writing the ninth-stage upgrade instruction into the first preset location; and if the firmware upgrade package is an Android format, obtaining the target upgrade mode firmware image file from the preset image file path, writing the target upgrade mode firmware image file into the starting address of the upgrade mode firmware partition in the partition table to be updated, setting the current upgrade instruction to a tenth-stage upgrade instruction, and writing the tenth-stage upgrade instruction into the first preset location.
[0034] In this way, for non-Android firmware upgrade packages, the upgrade of the image file that has not yet been upgraded can be completed directly. For Android firmware upgrade packages, the target upgrade mode firmware image file needs to be upgraded first before other image files can be upgraded. Therefore, the subsequent steps are different. This achieves automatic matching of upgrade package formats in the upgrade process.
[0035] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is a ninth-stage upgrade instruction, then starting the upgrade mode firmware; determining whether the backup flag in the first information is true; if not, then formatting the current user data partition and formatting each readable and writable partition in the list of readable and writable partitions to be backed up; setting the current upgrade instruction to a thirteenth-stage upgrade instruction; and writing the thirteenth-stage upgrade instruction to the first preset location; and if the backup flag in the first information is true, then obtaining the corresponding target image file according to the target image file information item corresponding to the current user data partition and the list of readable and writable partitions to be backed up; restoring the current user data partition and each readable and writable partition in the list of readable and writable partitions to be backed up according to the target image file; setting the current upgrade instruction to a thirteenth-stage upgrade instruction; and writing the thirteenth-stage upgrade instruction to the first preset location.
[0036] In this way, when the firmware upgrade package is not in Android format, after upgrading all image files in the firmware upgrade package, it is further determined whether the backup flag is true. If it is true, it means that there is backed-up data that needs to be restored, and the corresponding backed-up data is restored. Otherwise, it means that there is no backed-up data that needs to be restored, and the subsequent steps can be directly performed.
[0037] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is a tenth-stage upgrade instruction, then using the Android standard Android format package firmware upgrade engine to upgrade the firmware upgrade package under the firmware upgrade package path in the first information, and skipping the upgrade of the target bootloader firmware, the target upgrade mode firmware, and the partition table to be updated during the upgrade process; and setting the current upgrade instruction to a ninth-stage upgrade instruction, and writing the ninth-stage upgrade instruction into the first preset location.
[0038] In this way, if the firmware upgrade package is in Android format, after updating the partition table to be updated, the target bootloader firmware, and the target upgrade mode firmware, the update process for other firmware will continue. If the update is complete, it will be further determined whether there is any backed-up data that needs to be restored, thereby avoiding the problem of data loss after the firmware upgrade package is updated.
[0039] In some embodiments, the method 100 may further include: reading the current upgrade instruction in the first preset location; if the current upgrade instruction is a thirteenth-stage upgrade instruction, then reading the initial upgrade metadata; checking whether the backup marker and the user data partition encryption marker in the initial upgrade metadata are both true; if so, then creating an upgrade completion marker file; deleting the firmware upgrade package under the firmware upgrade package path in the initial upgrade metadata, and deleting all image files under the preset image file path; if there is no upgrade completion marker file, then clearing the initial upgrade metadata and all files and directories under the backup directory path of the initial upgrade metadata; and after clearing the second-stage upgrade metadata and the first preset location, restarting the terminal.
[0040] In this way, after the firmware upgrade package is completed, all kinds of parameters used in the update process are deleted to avoid data reading errors during subsequent upgrades of new firmware packages.
[0041] In some embodiments, after reading the current upgrade instruction in the first preset location, the method further includes: saving the current upgrade instruction in an upgrade instruction environment variable; and deleting the upgrade instruction environment variable after the current upgrade instruction is updated.
[0042] In this way, environment variables are used as temporary storage for the current upgrade command. In the event of unexpected situations such as power failure, the current upgrade command can be retrieved from the environment variables to determine the current execution stage of the upgrade process, so that the upgrade process can continue at any time, thus avoiding the situation where the terminal becomes "bricked" after an unexpected situation occurs.
[0043] In step 106, the boot communication partition in the current partition table is backed up, the parameters required to upgrade the target boot loader firmware are backed up in the environment variables, the parameters are obtained from the environment variables to complete the upgrade of the target boot loader firmware, and then the boot communication partition in the partition table to be updated is updated.
[0044] In some embodiments, the method 100 may further include: when an instruction to upgrade the firmware upgrade package is detected, determining whether user data partition backup information has been received; if so, backing up the current user data partition; and restoring the current user data partition after the firmware upgrade package upgrade is completed. In this way, if a user needs to back up their user data partition, they can back it up before officially starting the firmware upgrade package upgrade, and then restore the backed-up user data partition after the firmware upgrade package upgrade is completed.
[0045] In this way, data in the user data partition can be saved during the firmware upgrade process, thus avoiding data loss.
[0046] In step 108, the current partition table is updated to the partition table to be updated.
[0047] The following will describe, through examples, the application scenarios of the firmware upgrade method and device according to embodiments of the present invention.
[0048] Figure 2 This is a flowchart illustrating a firmware upgrade method according to an embodiment of the present invention in a real-world scenario. (Refer to...) Figure 2 The method includes steps 201 to 215.
[0049] In step 201, a firmware upgrade package is constructed, including a new partition table with a preset name to be updated. The firmware upgrade package contains the partition table, which includes a boot communication partition. The boot communication partition itself is readable and writable, but the firmware upgrade package does not include the corresponding image file.
[0050] When the firmware upgrade client runs, it first checks if an upgrade completion marker file with a preset name exists in the preset path of the preset read / write partition. If it exists, it reads the initial upgrade metadata from the second preset location in the boot communication partition used to store the initial upgrade metadata and checks its validity. If the initial upgrade metadata is valid, it mounts the external storage medium according to the file system type of the external storage medium. In the root directory of the external storage medium, it retrieves the backup file archive with a preset name from the backup directory path of the initial upgrade metadata. It calculates the digital digest value of the backup file archive according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata and compares the calculated digital digest value with the backup data digital digest value of the initial upgrade metadata. If they match, it decompresses the backup file archive to the mount directory of the user data partition according to the preset decompression algorithm, then deletes the backup file archive and the upgrade completion marker file, and clears the initial upgrade metadata from the preset location in the boot communication partition used to store the initial upgrade metadata.
[0051] Checking the validity of the initial upgrade metadata means first determining whether the magic number of the initial upgrade metadata is the same as the preset magic number. If not, the initial upgrade metadata is invalid. If the magic number is the same as the preset magic number, then the digital digest value of the content consisting of the external storage medium file system type, firmware upgrade package path, user data partition encryption mark, key partition name, backup mark, backup type, backup data digital digest value, and backup directory path of the initial upgrade metadata is calculated according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata. The calculated digital digest value is then compared with the metadata digital digest value of the initial upgrade metadata. If they match, the initial upgrade metadata is valid; otherwise, it is invalid.
[0052] When the firmware upgrade client detects that a new firmware upgrade package needs to be upgraded, it obtains the firmware upgrade package and performs a validity check on the firmware upgrade package. If the firmware upgrade package is invalid, the firmware upgrade process is exited; if the firmware upgrade package is valid, the following steps (2) are executed.
[0053] Step (2): Check if the device has an external storage medium (such as a USB flash drive or SD card) connected. If so, save the obtained firmware upgrade package to the preset path of the external storage medium and then execute the following step (3); otherwise, prompt the user to insert an external storage medium. When the external storage medium is detected to be inserted and usable, save the obtained firmware upgrade package to the preset path of the external storage medium and then execute the following step (3).
[0054] Step (3): Construct initial upgrade metadata consisting of a magic number, a digital digest algorithm type, a metadata digital digest value, an external storage medium file system type, a firmware upgrade package path, a user data partition encryption marker, a key partition name, a backup marker, a backup type, a backup data digital digest value, and a backup directory path. Set the magic number of the initial upgrade metadata to a preset initial upgrade metadata magic number, and set the digital digest algorithm type of the initial upgrade metadata to a preset digital digest algorithm type; obtain the file system type of the external storage medium, and set the file system type of the external storage medium in the initial upgrade metadata to the obtained file system type of the external storage medium. Set the firmware upgrade package path of the initial upgrade metadata to a preset firmware upgrade package path relative to the root directory of the external storage medium.
[0055] Read the fstab file (e.g., / vendor / etc / fstab.rk30board) in the system's default path, check if the default user data partition (e.g., data) is encrypted. If not, set the encryption flag of the user data partition in the initial upgrade metadata to no; otherwise, set the encryption flag of the user data partition in the initial upgrade metadata to true, obtain the partition name (e.g., metadata) corresponding to the encryption / decryption key, and save it to the key partition name in the initial upgrade metadata.
[0056] Step (4): Remind the user that there is a change in the partition table during this upgrade and that the partition table needs to be upgraded. Ask if the user data partition needs to be backed up. If not, set the backup flag of the initial upgrade metadata to no and then execute the following step (5); otherwise, set the backup flag of the initial upgrade metadata to true and check if the encryption flag of the user data partition in the initial upgrade metadata is true. If the encryption flag of the user data partition is no, execute step (4-1). If the encryption flag of the user data partition is true, execute step (4-2).
[0057] (4-1) Prompt the user whether to perform a full backup of the user data partition. If yes, set the backup type of the initial upgrade metadata to full backup. If no, set the backup type of the initial upgrade metadata to file backup, and let the user specify the list of files or directories to be backed up under the user data partition. Then, save the list of files or directories to be backed up to the preset backup directory of the external storage medium according to the same directory hierarchy. Set the backup directory path of the initial upgrade metadata to the path of the preset backup directory relative to the root directory of the external storage medium. Compress all contents under the backup directory path of the initial upgrade metadata according to the preset compression algorithm to generate a backup file archive with a preset name. Calculate the digital digest value of the backup file archive according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata, and save the calculated digital digest value to the backup data digital digest value of the initial upgrade metadata. Then delete all other directories and files under the backup directory path of the initial upgrade metadata except for the backup file archive with the preset name, and execute the following steps (5).
[0058] (4-2) Set the backup type of the initial upgrade metadata to file backup, and let the user specify the list of files or directories to be backed up under the user data partition. Then, save the list of files or directories to be backed up to the preset backup directory of the external storage medium according to the same directory hierarchy. Set the backup directory path of the initial upgrade metadata to the path of the preset backup directory relative to the root directory of the external storage medium. Compress all contents under the backup directory path of the initial upgrade metadata according to the preset compression algorithm to generate a backup file archive with a preset name. Calculate the digital digest value of the backup file archive according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata, and save the calculated digital digest value to the backup data digital digest value of the initial upgrade metadata. Then delete all other directories and files under the backup directory path of the initial upgrade metadata except for the backup file archive with the preset name. Then execute the following step (5).
[0059] Step (5): Calculate the digital digest value of the content consisting of the external storage medium file system type, firmware upgrade package path, user data partition encryption mark, key partition name, backup mark, backup type, backup data digital digest value, and backup directory path of the initial upgrade metadata according to the digital digest algorithm specified by the initial upgrade metadata type. Save the calculated digital digest value to the metadata digital digest value of the initial upgrade metadata. Write the initial upgrade metadata to the preset location in the boot communication partition used to store the initial upgrade metadata. Write the preset first-stage upgrade command to the first preset location in the boot communication partition used to store the current upgrade command. The device restarts.
[0060] Step 202: After obtaining the current upgrade command, verify the validity of the parameters used when upgrading the firmware upgrade package, including: the bootloader firmware checks whether there is a preset upgrade command information environment variable in its environment variables. If there is an upgrade command information environment variable and the corresponding upgrade command information environment variable value is a preset valid upgrade command, then the value of the upgrade command information environment variable is taken as the current upgrade command, and then the following steps (2-3) are executed; if there is no preset upgrade command information environment variable, then the following steps (2-2) are executed.
[0061] The preset valid upgrade command refers to the upgrade command being one of the preset first-stage upgrade command, second-stage upgrade command, third-stage upgrade command, fourth-stage upgrade command, fifth-stage upgrade command, sixth-stage upgrade command, seventh-stage upgrade command, eighth-stage upgrade command, ninth-stage upgrade command, tenth-stage upgrade command, eleventh-stage upgrade command, twelfth-stage upgrade command, or thirteenth-stage upgrade command.
[0062] Step (2-2) reads the upgrade command from the first preset position of the boot communication partition, and uses the read upgrade command as the current upgrade command, and then executes the following steps (2-3).
[0063] Step (2-3) Check the validity of the initial upgrade metadata and / or the second-stage upgrade metadata based on the value of the current upgrade instruction. If the corresponding metadata is invalid, exit the firmware upgrade process. Checking the legality of the initial upgrade metadata means first determining whether the magic number of the initial upgrade metadata is the preset magic number. If not, the initial upgrade metadata is invalid. If it is, then the digital digest value of the content consisting of the file system type of the external storage medium, the firmware upgrade package path, the user data partition encryption mark, the key partition name, the backup mark, the backup type, the backup data digital digest value, and the backup directory path of the initial upgrade metadata is calculated according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata. The calculated digital digest value is then compared with the metadata digital digest value of the initial upgrade metadata. If they match, the initial upgrade metadata is valid; otherwise, it is invalid.
[0064] Checking the legality of the second-stage upgrade metadata involves first determining whether the magic number of the second-stage upgrade metadata is the preset magic number. If not, the second-stage upgrade metadata is invalid. If it is, then the digital digest value of the contents, consisting of the firmware upgrade package format, image file path, image file information list, backup image information list, list of read / write partitions to be backed up, boot communication partition change marker, and update mode, is calculated according to the digital digest algorithm specified by the digital digest algorithm type of the second-stage upgrade metadata. The calculated digital digest value is then compared with the digital digest value of the metadata of the second-stage upgrade metadata. If they match, the second-stage upgrade metadata is valid; otherwise, it is invalid.
[0065] If the current upgrade command is a first-stage upgrade command or a second-stage upgrade command, the initial upgrade metadata is read from the first preset position of the boot communication partition, and the validity of the initial upgrade metadata is checked. If the initial upgrade metadata is invalid, the firmware upgrade process is exited.
[0066] If the current upgrade command is a third-stage upgrade command, a fourth-stage upgrade command, a fifth-stage upgrade command, a sixth-stage upgrade command, a seventh-stage upgrade command, an eighth-stage upgrade command, a ninth-stage upgrade command, a tenth-stage upgrade command, an eleventh-stage upgrade command, a twelfth-stage upgrade command, or a thirteenth-stage upgrade command, then the initial upgrade metadata is read from the second preset location in the boot communication partition used to store the initial upgrade metadata, and the second-stage upgrade metadata is read from the third preset location in the boot communication partition used to store the second-stage upgrade metadata. The validity of the initial upgrade metadata and the second-stage upgrade metadata is checked. If either the initial upgrade metadata or the second-stage upgrade metadata is invalid, the firmware upgrade process is exited.
[0067] Step 203: Enter the first-stage upgrade instruction execution process, including: checking whether the current upgrade instruction is a preset first-stage upgrade instruction; if so, creating a new preset upgrade instruction information environment variable in the environment variables and setting the value of the upgrade instruction information environment variable to the preset first-stage upgrade instruction. Setting the current upgrade instruction to the preset second-stage upgrade instruction and writing the current upgrade instruction to the first preset location in the boot communication partition, deleting the upgrade instruction information environment variable, and then booting the device into upgrade mode firmware.
[0068] Step 204: Enter the second-stage upgrade instruction execution process, construct the initial upgrade metadata for the second stage and perform a full backup, including: checking whether the current upgrade instruction is the preset second-stage upgrade instruction; if so, guide the device into the upgrade mode firmware.
[0069] The initial upgrade metadata is read from the preset location in the boot communication partition where the initial upgrade metadata is stored, and the external storage medium is mounted according to the file system type of the external storage medium containing the initial upgrade metadata.
[0070] The second-stage upgrade metadata is constructed, consisting of a magic number, a digital digest algorithm type, a metadata digital digest value, a firmware upgrade package format, an image file path, an image file information list, a backup image information list, a list of readable and writable partitions to be backed up, a boot communication partition change marker, and an update mode. All of the second-stage upgrade metadata is initialized to empty (e.g., all 0s). The image file information list and the backup image information list are both lists composed of image file information items, each consisting of an image file name and an image file digital digest value. The list of readable and writable partitions to be backed up is a list of partition names.
[0071] Set the magic number of the second-stage upgrade metadata to the preset magic number of the second-stage upgrade metadata, set the digital digest algorithm type of the second-stage upgrade metadata to the preset digital digest algorithm type, and set the image file path of the second-stage upgrade metadata to the preset image file path relative to the root directory of the external storage medium.
[0072] The firmware upgrade package format under the firmware upgrade package path of the initial upgrade metadata is detected, and the corresponding firmware image file is extracted to the image file path of the second-stage upgrade metadata, including the following steps (a) to (c).
[0073] Step (a): Check if a file with the .br extension exists in the firmware upgrade package. If not, proceed to step (b). If it exists, set the firmware upgrade package format in the second-stage upgrade metadata to Android format. Find an unused image file information item from the image file information list in the second-stage upgrade metadata as the bootloader firmware image file information item. Read the bootloader firmware from the firmware upgrade package and save it as the bootloader firmware image file in the image file path of the second-stage upgrade metadata. Set the image file name of the bootloader firmware image file information item to the file name of the bootloader firmware image file in the image file path. Calculate the digital digest of the bootloader firmware image file using the digital digest algorithm type in the second-stage upgrade metadata and save it to the image file digital digest value of the bootloader firmware image file information item.
[0074] Find an unused image file information item from the image file information list of the second-stage upgrade metadata and use it as the upgrade mode firmware file information item. Read the upgrade mode firmware from the firmware upgrade package and save it as the upgrade mode firmware image file to the image file path of the second-stage upgrade metadata; set the image file name of the upgrade mode firmware file information item to the file name of the upgrade mode firmware image file under the image file path; calculate the digital digest of the upgrade mode firmware image file using the digital digest algorithm type of the second-stage upgrade metadata and save it to the image file digital digest value of the upgrade mode firmware file information item.
[0075] Find an unused image file information item from the image file information list of the second-stage upgrade metadata and use it as the partition table file information item. Then, read the partition table to be updated with a preset name from the firmware upgrade package and save it as the partition table file in the image file path of the second-stage upgrade metadata; set the image file name of the partition table file information item to the file name of the partition table file in the image file path; calculate the digital digest of the partition table file using the digital digest algorithm type of the stage 2 upgrade metadata and save it to the image file digital digest value of the partition table file information item.
[0076] Step (b): Check if the upgrade package contains a payload.bin file. If it does not, proceed to step (c); otherwise, notify the user that the upgrade package is invalid and exit the firmware upgrade process. This is because if a payload.bin file exists, it means the upgrade package is an Android standard AB upgrade package, which cannot be used in upgrade mode.
[0077] Step (c) sets the firmware upgrade package format for Phase 2 upgrade metadata to a firmware image file. Non-Android firmware installation packages also include their bootloader firmware, upgrade mode firmware, and partition table.
[0078] Each image file in the upgrade package is read sequentially. For each image file read, it is first saved to the image file path in the second-stage upgrade metadata. Then, an unused image file information item is found in the image file information list of the second-stage upgrade metadata as the current image file information item. The image file name of the current image file information item is set to the name of the corresponding image file. The digital digest algorithm type of the second-stage upgrade metadata is used to calculate the digital digest of the corresponding image file and saved to the image file digital digest value of the current image file information item.
[0079] Check if the address space of the boot communication partition in the partition table to be updated is the same as the address space of the boot communication partition in the current partition table. If so, set the boot communication partition change flag in the second-stage upgrade metadata to no; otherwise, set the boot communication partition change flag in the second-stage upgrade metadata to true.
[0080] The system checks if the boot communication partition change flag in the second-stage upgrade metadata is true. If so, it checks if the address space of the boot communication partition in the new partition table overwrites or corrupts the address space of the bootloader firmware partition in the current partition table. If so, it sets the update mode of the second-stage upgrade metadata to bootloader firmware partition priority. If the address space of the boot communication partition in the new partition table does not overwrite or corrupt the address space of the bootloader firmware partition in the current partition table, it means that the boot communication partition can be updated first. The advantage of this is that after the boot communication partition is updated, subsequent stages can significantly reduce the use of environment variables, and the updated boot communication partition can be read and written directly. However, if the address space of the boot communication partition in the new partition table overwrites or corrupts the address space of the bootloader firmware partition in the current partition table, then the boot communication partition cannot be updated first. Updating the boot communication partition first would corrupt the bootloader firmware, potentially causing the system to fail to boot. Therefore, in this case, bootloader firmware partition priority is used, and the bootloader firmware is updated and upgraded first.
[0081] First, check if the backup flag in the initial upgrade metadata is true. If so, check if the encryption flag for the user data partition in the initial upgrade metadata is false. If so, check if the backup type in the initial upgrade metadata is a full backup. If so, open the block device node of the user data partition and create an empty user data partition image file (e.g., userdata.img) with the partition name as its filename under the image file path of the second-stage upgrade metadata. Then, continuously read all data from the block device node corresponding to the user data partition and save it to the corresponding user data partition image file under the image file path. Find an unused image file information item from the backup image information list of the second-stage upgrade metadata and use it as the user data partition backup image file information item. Set the image file name of the user data partition backup image file information item to the user data partition image file under the image file path. Calculate the digital digest of the user data partition image file using the digital digest algorithm type of the stage 2 upgrade metadata and save it to the image file digital digest value of the user data partition backup image file information item.
[0082] The system saves the partition names of all preset readable and writable partitions (excluding the user data partition and boot communication partition) to the list of readable and writable partitions to be backed up in the Phase 2 upgrade metadata. It calculates a digital digest value for the content consisting of the firmware upgrade package format, image file path, image file information list, backup image information list, list of readable and writable partitions to be backed up, boot communication partition change marker, and update mode, based on the digital digest algorithm specified by the digital digest algorithm type of the Phase 2 upgrade metadata. This calculated digital digest value is then saved to the metadata digital digest value of the Phase 2 upgrade metadata. The Phase 2 upgrade metadata is written to the preset location in the boot communication partition used to store the Phase 2 upgrade metadata. Preset Phase 3 upgrade commands are written to the preset location in the boot communication partition used to store upgrade commands, and the device restarts. Preset readable and writable partitions, excluding the user data partition and boot communication partition, typically include cache partitions, metadata partitions, etc.
[0083] Step 205: Enter the third-stage upgrade instruction execution process. Complete the backup of each readable and writable partition in the list of readable and writable partitions to be backed up in the initial upgrade metadata of the second stage. If the boot communication partition change marker is true, back up the boot communication partition, including: checking whether the current upgrade instruction is a third-stage upgrade instruction; if so, create a new preset upgrade instruction information environment variable in the environment variables and set the value of the upgrade instruction information environment variable to the preset third-stage upgrade instruction. Read the initial upgrade metadata from the preset location in the boot communication partition used to store the initial upgrade metadata, and mount the external storage medium according to the file system type of the external storage medium of the initial upgrade metadata.
[0084] The second-stage upgrade metadata is read from the preset location in the boot communication partition used to store the second-stage upgrade metadata. The backup flag of the initial upgrade metadata is checked; if true, the current partition table is read from the preset partition table address space of the storage medium. For each readable and writable partition name in the list of readable and writable partitions to be backed up in the second-stage upgrade metadata, an empty image file with the corresponding partition name as its filename is first created under the image file path of the second-stage upgrade metadata. Then, all data of the corresponding partition is continuously read from the address space of the corresponding partition name in the current partition table and saved accordingly to the corresponding image file under the image file path. Finally, an unused image file information item is found sequentially from the backup image information list of the second-stage upgrade metadata as the current image file information item. The image file name of the current image file information item is set to the name of the corresponding image file. The digital digest algorithm type of the second-stage upgrade metadata is used to calculate the digital digest of the corresponding image file and saved to the image file digital digest value of the current image file information item.
[0085] Check if the boot communication partition change flag in the second-stage upgrade metadata is true. If it is, first create an empty boot communication partition image file with the partition name of the boot communication partition as the file name in the image file path of the second-stage upgrade metadata; then continuously read all the data of the partition from the partition name in the address space of the current partition table and save it to the boot communication partition image file in the image file path.
[0086] The digital digest value is calculated based on the digital digest algorithm specified by the digital digest algorithm type of the second-stage upgrade metadata. The digital digest value consists of the firmware upgrade package format, image file path, image file information list, backup image information list, list of read / write partitions to be backed up, boot communication partition change marker and update mode of the second-stage upgrade metadata. The calculated digital digest value is then saved to the metadata digital digest value of the second-stage upgrade metadata.
[0087] Set the current upgrade command to the preset fourth-stage upgrade command, and write the current upgrade command to the preset location in the boot communication partition used to store upgrade commands. Delete the upgrade command information environment variable.
[0088] Step 206: Enter the fourth stage upgrade instruction execution process. When the address of the boot communication partition in the partition table to be updated changes and the address change does not affect the boot loader firmware partition in the current partition table, update the boot communication partition, including: checking whether the current upgrade instruction is the preset fourth stage upgrade instruction. If so, create a new preset upgrade instruction information environment variable in the environment variables and set the value of the upgrade instruction information environment variable to the preset fourth stage upgrade instruction.
[0089] Check if a preset update mode environment variable exists in the environment variables. If it does not exist, create a new preset update mode environment variable in the environment variables. Read the second-stage upgrade metadata from the third preset location of the boot communication partition and set the value of the update mode environment variable to the update mode of the second-stage upgrade metadata.
[0090] Check if the value of the update mode environment variable is "boot loader firmware partition priority". If so, set the current upgrade command to the preset fifth stage upgrade command and write the current upgrade command to the first preset location in the boot communication partition to save the current upgrade command. Delete the update mode environment variable and delete the upgrade command information environment variable. Otherwise, execute the following steps (2-6-2).
[0091] Step (2-6-2): Check if a preset environment variable for boot communication partition change flag exists in the environment variables. If it does not exist, create a new preset environment variable for boot communication partition change flag in the environment variables. Read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata. Set the value of the environment variable for boot communication partition change flag to the boot communication partition change flag of the second-stage upgrade metadata.
[0092] Check if the boot communication partition change flag environment variable is true. If it is, execute the following steps (2-6-3); otherwise, set the current upgrade command to the preset fifth-stage upgrade command, write the current upgrade command to the first preset position in the boot communication partition, delete the update mode environment variable, delete the boot communication partition change flag environment variable, and delete the upgrade command information environment variable.
[0093] Step (2-6-3): Check if a preset external storage media file system type environment variable exists in the environment variables. If it does not exist, create a new preset external storage media file system type environment variable in the environment variables. Read the initial upgrade metadata from the preset location used to save the initial upgrade metadata in the boot communication partition, and set the value of the external storage media file system type environment variable to the external storage media file system type of the initial upgrade metadata.
[0094] Check if a preset image file path environment variable exists in the environment variables. If it does not exist, create a new preset image file path environment variable in the environment variables. Read the second-stage upgrade metadata from the third preset location of the boot communication partition and set the value of the image file path environment variable to the image file path of the second-stage upgrade metadata.
[0095] Check if a preset digital digest algorithm type environment variable exists in the environment variables. If it does not exist, create a new preset digital digest algorithm type environment variable in the environment variables. Read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata, and set the value of the digital digest algorithm type environment variable to the digital digest algorithm type of the second-stage upgrade metadata.
[0096] Check if a preset environment variable for the new partition table digital digest value exists. If it does not exist, create a new environment variable for the new partition table digital digest value. Read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata. Find the image file information item corresponding to the partition table file with the preset name from the image file information list of the second-stage upgrade metadata and use it as the partition table file information item. Set the value of the new partition table digital digest value environment variable to the image file digital digest value of the partition table file information item.
[0097] The external storage medium is mounted according to the external storage medium file system type environment variable. A partition table file with a preset name is retrieved from the external storage medium as a new partition table based on the image file path environment variable. A digital digest of the new partition table is calculated based on the digital digest algorithm type environment variable. The calculated digital digest is compared with the digital digest value of the new partition table environment variable. If they do not match, an error message is displayed, and the firmware upgrade process is exited; otherwise, the following steps (2-6-4) are executed.
[0098] Step (2-6-4): Obtain the boot communication partition image file from the external storage medium according to the image file path specified by the image file path environment variable. Write the boot communication partition image file to the starting address of the boot communication partition in the partition table to be updated. The size written is the minimum value between the size of the boot communication partition image file and the size of the boot communication partition in the partition table to be updated. Normally, the size of the boot communication partition image file is set to be smaller than the size of the boot communication partition in the partition table. This is a precautionary operation to avoid accidentally writing the boot communication partition image file and damaging other partitions. The boot communication partition image file includes the current upgrade instruction stored in the first preset location, the initial upgrade metadata stored in the second preset location, and the second-stage upgrade metadata stored in the third preset location.
[0099] The current partition table is read from the preset partition table address space of the storage medium, the starting address of the boot communication partition in the current partition table is modified to the starting address of the boot communication partition in the partition table to be updated, and the partition size of the boot communication partition in the current partition table is modified to the partition size of the boot communication partition in the partition table to be updated.
[0100] Write the modified current partition table to the preset partition table address space of the storage medium.
[0101] Set the current upgrade instruction to the fifth stage upgrade instruction, and write the current upgrade instruction to the fourth preset location in the address space corresponding to the boot communication partition in the partition table to be updated to store the upgrade instruction. Delete the update mode environment variable, delete the boot communication partition change marker environment variable, delete the external storage medium file system type environment variable, delete the image file path environment variable, delete the digital digest algorithm type environment variable, delete the new partition table digital digest value, and delete the upgrade instruction information environment variable.
[0102] Step 207: Enter the fifth stage upgrade instruction execution process, back up the bootloader firmware partition in the current partition table. If the update mode is the bootloader firmware priority mode, back up the boot communication partition in the current partition table and save the parameters required for upgrading the target bootloader firmware in the environment variables, including: checking whether the current upgrade instruction is the preset stage upgrade instruction. If so, create a new preset upgrade instruction information environment variable in the environment variables and set the value of the upgrade instruction information environment variable to the preset fifth stage upgrade instruction.
[0103] The current partition table is read from the preset partition table address space of the storage medium. The second-stage upgrade metadata is read from the preset location in the boot communication partition used to store the second-stage upgrade metadata. The initial upgrade metadata is read from the preset location in the boot communication partition used to store the initial upgrade metadata, and the external storage medium is mounted according to the file system type of the external storage medium based on the initial upgrade metadata.
[0104] Find the image file information item corresponding to the partition table file with the preset name from the image file information list of the second-stage upgrade metadata and use it as the partition table file information item.
[0105] Based on the image file path of the second-stage upgrade metadata, a partition table file with a preset name is obtained from the external storage medium as the partition table to be updated. The digital digest of the new partition table is calculated according to the digital digest algorithm type of the second-stage upgrade metadata. The calculated digital digest is compared with the image file digital digest value of the partition table file information item. If they are inconsistent, an error is prompted and the firmware upgrade process is exited; otherwise, the following steps (2-7-4) are executed.
[0106] Step (2-7-4): The starting address and size of the bootloader firmware partition in the current partition table are respectively called the starting address and size of the old bootloader firmware partition.
[0107] After the bootloader firmware partition in the partition table to be updated, find a firmware partition to be upgraded with a partition size no smaller than that of the old bootloader firmware partition as the bootloader firmware backup partition. The firmware partition to be upgraded cannot be the boot communication partition.
[0108] Read data equal to the size of the old bootloader firmware partition from the starting address of the old bootloader firmware partition, and then write the read data to the starting address of the bootloader firmware backup partition, with the written data size equal to the size of the old bootloader firmware partition.
[0109] Modify the starting address of the bootloader firmware partition in the current partition table to the starting address of the bootloader firmware backup partition. Write the modified current partition table to the preset partition table address space on the storage medium.
[0110] Check if the update mode of the second-stage upgrade metadata is bootloader firmware partition priority. If not, set the current upgrade command to the sixth-stage upgrade command and write the current upgrade command to the preset location in the boot communication partition used to save the upgrade command, and delete the upgrade command information environment variable. If the update mode is bootloader firmware partition priority mode, then execute the following step (2-7-5). If the boot communication partition was updated to the boot communication partition address in the partition table to be updated in step (2-6-4), then write the current upgrade command to the fourth preset location in the new boot communication partition here.
[0111] Step (2-7-5): Check if the boot communication partition change flag in the second-stage upgrade metadata is true. If not, set the current upgrade command to the sixth-stage upgrade command and write the current upgrade command to the first preset position in the boot communication partition, and delete the upgrade command information environment variable. If it is true, execute the following step (2-7-6). Here, the boot communication partition change flag is checked again as a redundancy check to increase reliability.
[0112] Step (2-7-6): Check if a boot communication partition image file exists in the image file path of the second-stage upgrade metadata. If it exists, delete it. Because the boot communication partition is writable, if a boot communication partition image file already exists, the file is likely outdated and cannot accurately reflect the latest state of the boot communication partition. Therefore, it needs to be deleted, and the latest state of the boot communication partition should be backed up to the boot communication partition image file.
[0113] In the second stage of upgrading metadata, an empty boot communication partition image file is created under the image file path, with the partition name of the boot communication partition as the file name. Then, all data of the partition is continuously read from the address space of the current partition table from the partition name of the boot communication partition and saved accordingly to the boot communication partition image file under the image file path.
[0114] Check if a preset update mode environment variable exists in the environment variables. If it does not exist, create a new preset update mode environment variable in the environment variables and set the value of the update mode environment variable to the update mode of the second-stage upgrade metadata.
[0115] Check if a preset environment variable for boot communication partition change flag exists in the environment variables. If it does not exist, create a new preset environment variable for boot communication partition change flag in the environment variables and set the value of the environment variable for boot communication partition change flag to the boot communication partition change flag of the second-stage upgrade metadata.
[0116] Check if a preset external storage media file system type environment variable exists in the environment variables. If it does not exist, create a new preset external storage media file system type environment variable in the environment variables and set the value of the external storage media file system type environment variable to the external storage media file system type of the initial upgrade metadata.
[0117] Check if a preset image file path environment variable exists in the environment variables. If it does not exist, create a new preset image file path environment variable in the environment variables and set the value of the image file path environment variable to the image file path of the second-stage upgrade metadata.
[0118] Check if a preset digital digest algorithm type environment variable exists in the environment variables. If it does not exist, create a new preset digital digest algorithm type environment variable in the environment variables and set the value of the digital digest algorithm type environment variable to the digital digest algorithm type of the second-stage upgrade metadata.
[0119] Check if a preset environment variable for the new partition table digital digest value exists. If it does not exist, create a new environment variable for the new partition table digital digest value. Find the image file information item corresponding to the partition table file with the preset name from the image file information list of the second-stage upgrade metadata, and set the value of the new partition table digital digest value environment variable to the image file digital digest value of the partition table file information item.
[0120] Check if a preset bootloader firmware digital digest value environment variable exists in the environment variables. If it does not exist, create a new preset bootloader firmware digital digest value environment variable in the environment variables. Find the image file information item corresponding to the bootloader firmware from the image file information list of the second-stage upgrade metadata as the bootloader firmware file information item, and set the value of the bootloader firmware digital digest value environment variable to the image file digital digest value of the bootloader firmware file information item.
[0121] The current upgrade command is set to the sixth-stage upgrade command, and the current upgrade command is written to the preset location in the boot communication partition used to save the upgrade command. The environment variable for upgrade command information is set to the preset sixth-stage upgrade command.
[0122] Step 208: Enter the sixth-stage upgrade instruction execution process. If the update mode is the bootloader firmware priority mode, then obtain the parameters required to upgrade the target bootloader firmware from the environment variables to complete the upgrade of the target bootloader firmware; otherwise, obtain the parameters from the boot communication partition to complete the upgrade of the target bootloader firmware, and update the current partition table to the partition table to be updated, including: checking whether the current upgrade instruction is a preset sixth-stage upgrade instruction; if so, checking whether there is a preset upgrade instruction information environment variable in the environment variables; if not, creating a preset upgrade instruction information environment variable in the environment variables, and setting the value of the upgrade instruction information environment variable to the preset sixth-stage upgrade instruction.
[0123] Read the current partition table from the preset partition table address space of the storage medium. Check if a preset update mode environment variable exists in the environment variables. If not, create a new preset update mode environment variable in the environment variables. Read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata, and set the update mode environment variable to the update mode of the second-stage upgrade metadata.
[0124] Check if a preset environment variable for boot communication partition change flag exists in the environment variables. If it does not exist, create a new preset environment variable for boot communication partition change flag in the environment variables. Read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata, and set the value of the environment variable for boot communication partition change flag to the boot communication partition change flag of the second-stage upgrade metadata.
[0125] Check if the value of the boot communication partition change flag environment variable is true. If not, proceed to step (2-8-4). If it is true, check if the value of the update mode environment variable is boot boot firmware partition priority. If not, proceed to step (2-8-4). If it is true, proceed to step (2-8-5).
[0126] Step (2-8-4): Read the initial upgrade metadata from the preset location in the boot communication partition used to store the initial upgrade metadata; mount the external storage medium according to the file system type of the external storage medium for the initial upgrade metadata; read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata; find the image file information item corresponding to the partition table file with the preset name from the image file information list of the second-stage upgrade metadata as the partition table file information item; obtain the partition table file with the preset name from the external storage medium according to the image file path of the second-stage upgrade metadata as the partition table to be updated; obtain the target bootloader firmware image file from the external storage medium according to the image file path of the second-stage upgrade metadata; write the target bootloader firmware image file to the starting address of the bootloader firmware partition in the partition table to be updated, with the size being the minimum between the size of the target bootloader firmware image file and the size of the bootloader firmware partition in the partition table to be updated; similarly, this limitation can prevent partition overflow in case of unexpected situations. Write the partition table to be updated to the preset partition table address space of the storage medium; set the current upgrade instruction to the preset seventh-stage upgrade instruction, and write the current upgrade instruction to the fourth preset location in the boot communication partition used to save the upgrade instruction, and delete the upgrade instruction information environment variable.
[0127] Step (2-8-4) includes steps (2-8-4-1) to (2-8-4-3).
[0128] Step (2-8-4-1): Read the initial upgrade metadata from the preset location in the boot communication partition used to store the initial upgrade metadata, and mount the external storage medium according to the file system type of the external storage medium of the initial upgrade metadata.
[0129] Read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata.
[0130] The second-stage upgrade metadata is read from the preset location in the boot communication partition used to store the second-stage upgrade metadata. The image file information item corresponding to the preset-named partition table file is found from the image file information list of the second-stage upgrade metadata and used as the partition table file information item. The preset-named partition table file is retrieved from the external storage medium as the new partition table based on the image file path of the second-stage upgrade metadata. The digital digest of the new partition table is calculated according to the digital digest algorithm type of the second-stage upgrade metadata, and the calculated digital digest is compared with the image file digital digest value of the partition table file information item. If they do not match, an error is displayed, and the firmware upgrade process exits; otherwise, the following steps (2-8-4-2) are executed.
[0131] Step (2-8-4-2): Locate the image file information item corresponding to the bootloader firmware from the image file information list of the second-stage upgrade metadata as the bootloader firmware file information item. Obtain the bootloader firmware image file from the external storage medium according to the image file path of the second-stage upgrade metadata. Calculate the digital digest of the bootloader firmware image file according to the digital digest algorithm type of the second-stage upgrade metadata. Compare the calculated digital digest with the image file digital digest value of the bootloader firmware file information item. If they are inconsistent, prompt an error and exit the firmware upgrade process; otherwise, proceed to step (2-8-4-3).
[0132] Step (2-8-4-3): Write the bootloader firmware image file to the starting address of the bootloader firmware partition in the partition table to be updated. The size written is the minimum between the size of the bootloader firmware image file and the size of the bootloader firmware partition in the partition table to be updated. Write the partition table to be updated to the preset partition table address space on the storage medium, completing the update of the partition table to be updated. Set the current upgrade command to the preset seventh-stage upgrade command and write the current upgrade command to the preset location in the boot communication partition used to save the upgrade command. Delete the upgrade command information environment variable.
[0133] Step (2-8-5): Obtain the following environment variables from the environment variables: external storage media file system type environment variable, image file path environment variable, digital digest algorithm type environment variable, new partition table digital digest value environment variable, and bootloader firmware digital digest value environment variable; mount the external storage media according to the external storage media file system type environment variable; obtain a partition table file with a preset name from the external storage media as the new partition table according to the image file path specified by the image file path environment variable; obtain the bootloader firmware image file from the external storage media according to the image file path specified by the image file path environment variable; write the bootloader firmware image file to the starting address of the bootloader firmware partition in the new partition table, with the size being the minimum between the size of the bootloader firmware image file and the size of the bootloader firmware partition in the new partition table; write the partition table to be updated to the preset partition table address space of the storage media; set the upgrade command information environment variable to the preset seventh-stage upgrade command.
[0134] Step (2-8-5) includes steps (2-8-5-1) through (2-8-5-3). Step (2-8-5-1): Obtain the following environment variables from the environment variables: external storage media file system type environment variable, image file path environment variable, digital digest algorithm type environment variable, partition table digital digest value environment variable to be updated environment variable, and bootloader firmware digital digest value environment variable.
[0135] The external storage medium is mounted according to the external storage medium file system type environment variable. A partition table file with a preset name is retrieved from the external storage medium as a new partition table based on the image file path environment variable. A digital digest of the new partition table is calculated based on the digital digest algorithm type environment variable. The calculated digital digest is compared with the digital digest value of the new partition table environment variable. If they do not match, an error message is displayed, and the firmware upgrade process exits; otherwise, the following steps (2-8-5-2) are executed.
[0136] Step (2-8-5-2): Obtain the bootloader firmware image file from the external storage medium according to the image file path specified by the image file path environment variable. Calculate the digital digest of the bootloader firmware image file according to the digital digest algorithm type environment variable. Compare the calculated digital digest with the bootloader firmware digital digest value environment variable. If they are inconsistent, prompt an error and exit the firmware upgrade process; otherwise, proceed to step (2-8-5-3).
[0137] Step (2-8-5-3): Write the bootloader firmware image file to the starting address of the bootloader firmware partition in the new partition table. The size written is the minimum between the size of the bootloader firmware image file and the size of the bootloader firmware partition in the partition table to be updated. Write the partition table to be updated to the preset partition table address space on the storage medium. Set the upgrade command information environment variable to the preset seventh-stage upgrade command.
[0138] Step 209: Enter the seventh stage upgrade process. If the update mode is bootloader firmware priority, then update the boot communication partition, including: checking whether the current upgrade command is the preset seventh stage upgrade command. If so, check whether the preset upgrade command information environment variable exists in the environment variables. If not, create a new preset upgrade command information environment variable in the environment variables and set the value of the upgrade command information environment variable to the preset seventh stage upgrade command.
[0139] The current partition table is read from the preset partition table address space of the storage medium. Here, the storage medium is the terminal's current storage medium, distinct from the previous external storage medium.
[0140] Retrieve the preset update mode environment variable and the boot communication partition change marker environment variable from the environment variables respectively.
[0141] Check if the value of the environment variable for the change flag of the boot communication partition is true. If it is not true, proceed to the following step (2-9-4). If it is true, check if the value of the environment variable for the update mode is boot boot firmware partition priority. If it is not true, proceed to the following step (2-9-4). If the update mode is boot boot firmware partition priority, proceed to the following step (2-9-5).
[0142] Step (2-9-4): Set the current upgrade command to the preset eighth-stage upgrade command, write the current upgrade command to the fourth preset location in the boot communication partition used to save the upgrade command, and delete the upgrade command information environment variable.
[0143] Step (2-9-5): Obtain the external storage media file system type environment variable and the image file path environment variable from the environment variables respectively.
[0144] The external storage medium is mounted according to the external storage medium file system type environment variable. The boot communication partition image file is obtained from the external storage medium according to the image file path specified by the image file path environment variable. The boot communication partition image file is written to the starting address of the boot communication partition in the current partition table (at this time, the partition table to be updated is used as the current partition table), and the size written is the minimum value between the size of the boot communication partition image file and the size of the boot communication partition in the current partition table.
[0145] Set the current upgrade command to the preset eighth-stage upgrade command, write the current upgrade command to the fourth preset location in the boot communication partition used to save upgrade commands, and delete the upgrade command information environment variable.
[0146] Step 210: Enter the eighth stage upgrade instruction execution process. If the firmware upgrade package is in Android format, the upgrade of the target upgrade mode firmware in the firmware upgrade package will be completed. If the firmware package is not in Android format, the upgrade of other firmware in the firmware upgrade package except for the bootloader firmware will be completed directly. This includes: checking whether the current upgrade instruction is the preset eighth stage upgrade instruction. If so, a preset upgrade instruction information environment variable will be created in the environment variables, and the value of the upgrade instruction information environment variable will be set to the preset eighth stage upgrade instruction.
[0147] Delete the environment variables for update mode, boot communication partition change marker, external storage media file system type, image file path, digital digest algorithm type, new partition table digital digest value, and boot loader firmware digital digest value.
[0148] The current partition table is read from the preset partition table address space of the storage medium. At this time, the current partition table has been updated to the partition table to be updated.
[0149] The initial upgrade metadata is read from the preset location in the boot communication partition where the initial upgrade metadata is stored, and the external storage medium is mounted according to the file system type of the external storage medium containing the initial upgrade metadata.
[0150] Read the second-stage upgrade metadata from the preset location in the boot communication partition where the second-stage upgrade metadata is stored. Check whether the firmware upgrade package format of the second-stage upgrade metadata is an Android format package. If it is, execute the following step (2-10-2); otherwise, execute the following step (2-10-3).
[0151] Step (2-10-2): Locate the image file information item corresponding to the upgrade mode firmware from the image file information list of the second-stage upgrade meta-information. Obtain the upgrade mode firmware image file from the external storage medium according to the image file path of the second-stage upgrade meta-information. Calculate the digital digest of the upgrade mode firmware image file according to the digital digest algorithm type of the second-stage upgrade meta-information. Compare the calculated digital digest with the image file digital digest value of the upgrade mode firmware image file information item. If they do not match, prompt an error and exit the firmware upgrade process. If they match, write the upgrade mode firmware image file to the starting address of the upgrade mode firmware partition. The write size is the minimum value between the size of the upgrade mode firmware image file and the size of the upgrade mode firmware partition. Set the current upgrade command to the preset tenth-stage upgrade command and write the current upgrade command to the fourth preset location in the boot communication partition used to save the upgrade command. Delete the upgrade command information environment variable.
[0152] Step (2-10-3): Sequentially search the image file information list of the second-stage upgrade metadata for the image file information item other than the bootloader firmware file information item and the partition table file information item as the current image file information item. Based on the image file path in the second-stage upgrade metadata, retrieve the firmware image file to be upgraded from the external storage medium corresponding to the image file name of the current image file information item. Calculate the digital digest of the firmware image file to be upgraded according to the digital digest algorithm type of the second-stage upgrade metadata, and compare the calculated digital digest with the image file digital digest value of the current image file information item. If they do not match, prompt an error and exit the firmware upgrade process; if they match, write the firmware image file to be upgraded to the starting address of the firmware partition corresponding to the firmware image file, with the writing size being the minimum between the size of the firmware image file to be upgraded and the size of the firmware partition corresponding to the firmware image file.
[0153] If all the firmware image files corresponding to the image file information items in the image file information list of the second-stage upgrade metadata, except for the bootloader firmware file information item and the partition table file information item, are successfully written to the address space of the firmware partition corresponding to the corresponding firmware image file, then the current upgrade instruction is set to the preset ninth-stage upgrade instruction, and the current upgrade instruction is written to the fourth preset location in the boot communication partition used to save the upgrade instruction, and the upgrade instruction information environment variable is deleted; otherwise, an error is prompted and the firmware upgrade process is exited.
[0154] Step 211: Enter the ninth stage upgrade instruction execution process, indicating that the non-Android format firmware upgrade package update is complete, and guide to the twelfth stage upgrade instruction, including: checking whether the current upgrade instruction is the preset ninth stage upgrade instruction; if so, creating a new preset upgrade instruction information environment variable in the environment variables and setting the value of the upgrade instruction information environment variable to the preset ninth stage upgrade instruction; setting the current upgrade instruction to the preset twelfth stage upgrade instruction, writing the current upgrade instruction to the preset location in the boot communication partition used to save the upgrade instruction, and deleting the upgrade instruction information environment variable.
[0155] Step 212: Enter the tenth stage upgrade instruction execution process, indicating that the Android format firmware upgrade package has been updated and the system will guide you to the eleventh stage upgrade instruction. This includes: checking whether the current upgrade instruction is the preset tenth stage upgrade instruction. If so, a preset upgrade instruction information environment variable will be created in the environment variables, and the value of the upgrade instruction information environment variable will be set to the preset tenth stage upgrade instruction.
[0156] Set the current upgrade command to the preset eleventh stage upgrade command, write the current upgrade command to the preset location in the boot communication partition used to save upgrade commands, and delete the upgrade command information environment variable.
[0157] Step 213: Enter the eleventh stage upgrade instruction execution process. When the firmware upgrade package is in Android format, continue to complete the firmware upgrade process except for the bootloader firmware and upgrade mode firmware, including: checking whether the current upgrade instruction is the preset eleventh stage upgrade instruction. If so, the device is booted into upgrade mode firmware.
[0158] The external storage medium is mounted based on its file system type according to the initial upgrade metadata. The firmware upgrade package in the path specified in the initial upgrade metadata is upgraded using the standard Android format package firmware upgrade engine. During the upgrade process, upgrades to the bootloader firmware and upgrade mode firmware are skipped; upgrades to the partition table file are also skipped. The current upgrade command is set to the twelfth-stage upgrade command and written to a preset location in the boot communication partition used to store upgrade commands. Step 214: Enter the twelfth stage upgrade instruction execution process and complete the recovery of backup data, including: checking whether the current upgrade instruction is the preset twelfth stage upgrade instruction. If so, guide the device into upgrade mode firmware.
[0159] Mount the external storage medium according to the file system type of the external storage medium in the initial upgrade metadata. Check if the backup flag in the initial upgrade metadata is true. If not, proceed to step (3-4-2); if yes, proceed to step (3-4-3).
[0160] Step (3-4-2): Format the user data partition, and at the same time format each readable and writable partition in the list of readable and writable partitions to be backed up for the stage 2 upgrade metadata; set the current upgrade instruction to the preset thirteenth stage upgrade instruction, and write the current upgrade instruction to the preset location in the boot communication partition used to save the upgrade instruction.
[0161] Step (3-4-3) involves restoring the data in each of the read-write partitions in the backup list of the user data partition and the second-stage upgrade metadata; setting the current upgrade command to the preset thirteenth-stage upgrade command; and writing the current upgrade command to the preset location in the boot communication partition used to save the upgrade command. Step (3-4-3) includes steps (3-4-3-1) to (3-4-3-4). Step (3-4-3-1): Read the current partition table from the preset partition table address space of the storage medium. Check if the user data partition encryption flag in the initial upgrade metadata is true. If not, the user data partition is not encrypted, and proceed to step (3-4-3-2); if it is true, the user data partition is encrypted, and proceed to step (3-4-3-3).
[0162] Step (3-4-3-2): Check if the backup type of the initial upgrade metadata is a full backup. If not, format and mount the user data partition. Then, in the root directory of the external storage medium, obtain the backup file archive with the preset name located in the backup directory path of the initial upgrade metadata. Calculate the digital digest value of the backup file archive according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata. Compare the calculated digital digest value with the digital digest value of the backup data of the initial upgrade metadata. If they match, decompress the backup file archive to the mounted directory of the user data partition according to the preset decompression algorithm, and then execute step (3-4-3-4); otherwise, execute step (a). Step (a): Locate the image file information item corresponding to the user data partition image file from the backup image information list of the second-stage upgrade metadata as the user data partition backup image file information item. Obtain the user data partition image file from the external storage medium according to the image file path of the second-stage upgrade metadata. Calculate the digital digest of the user data partition image file according to the digital digest algorithm type of the second-stage upgrade metadata. Compare the calculated digital digest with the image file digital digest value of the user data partition backup image file information item. If they are inconsistent, prompt an error and exit the firmware upgrade process; otherwise, proceed as follows (b).
[0163] Step (b): Calculate whether the size of the user data partition image file under the image file path of the second-stage upgrade metadata is greater than the size of the user data partition in the current partition table. If so, mount the user data partition image file to the temporary data directory, then format and mount the user data partition. Copy all files and directories under the temporary data directory to the mount directory of the user data partition according to the corresponding directory hierarchy. Otherwise, open the block device node of the user data partition and the user data partition image file, write all data in the user data partition image file to the block device node of the user data partition in sequence, and after writing, expand the file system capacity of the user data partition to the actual size of the user data partition in the current partition table. Then execute the following steps (3-4-3-4). During the copying of the user data partition, the size issue does not need to be considered. First, the actual data is usually smaller than the entire user data partition image file. Second, even if "all files and directories under the temporary data directory" exceed the size of the user data partition, this copying will only result in copy failure or the excess data cannot be copied; it will not overwrite the size exceeding the user data partition, because the file system itself will make the judgment, just like copying files on a PC. For example, copying the data of a 16GB file to a 10GB C drive will not cause the extra 6GB of data to corrupt the C drive.
[0164] Step (3-4-3-3) formats the user data partition and the partition corresponding to the key partition name of the initial upgrade metadata. Then execute the following step (3-4-3-4).
[0165] Step (3-4-3-4): Sequentially search the backup image information list of the second-stage upgrade metadata for the image file information item other than the user data partition backup image file information item, and use it as the current image file information item. Based on the image file path in the second-stage upgrade metadata, retrieve the image file to be restored from the external storage medium corresponding to the image file name of the current image file information item. Calculate the digital digest of the image file to be restored according to the digital digest algorithm type of the second-stage upgrade metadata, and compare the calculated digital digest with the image file digital digest value of the current image file information item. If they do not match, prompt an error and exit the firmware upgrade process; otherwise, calculate... If the size of the image file to be recovered is greater than the size of the corresponding readable and writable partition in the current partition table, mount the image file to be recovered to the temporary data directory, then format and mount the corresponding readable and writable partition, and copy all files and directories under the temporary data directory to the mount directory of the corresponding readable and writable partition according to the corresponding directory hierarchy; otherwise, open the block device node of the corresponding readable and writable partition and the corresponding image file to be recovered, write all the data in the image file to be recovered to the block device node of the corresponding readable and writable partition in sequence, and after the writing is completed, expand the file system capacity of the corresponding readable and writable partition to the actual size of the corresponding readable and writable partition in the current partition table.
[0166] After all image file information items in the backup image information list of the second-stage upgrade metadata have been processed except for the user data partition backup image file information item, the current upgrade command is set to the preset thirteenth-stage upgrade command, and the current upgrade command is written to the preset location in the boot communication partition used to save the upgrade command.
[0167] Step 215: Enter the thirteenth stage upgrade instruction execution process to complete the firmware upgrade package upgrade, including: checking whether the current upgrade instruction is the preset thirteenth stage upgrade instruction. If so, guide the device into the firmware upgrade mode.
[0168] Read the initial upgrade metadata from the preset location in the boot communication partition where it is stored. Check if the backup flag and the user data partition encryption flag of the initial upgrade metadata are both true. If so, create an upgrade completion flag file with a preset name in the preset path of the preset read-write partition. Corresponding to steps (3-4-3-3) and step 201, the firmware upgrade client only needs to operate based on this file if the user data partition is encrypted. If the user data partition is encrypted, the recovery of data in the user data partition should be completed by the firmware upgrade client after the firmware upgrade package is upgraded and entered the system.
[0169] The external storage medium is mounted according to the file system type of the external storage medium containing the initial upgrade metadata. The second-stage upgrade metadata is read from the preset location in the boot communication partition where the second-stage upgrade metadata is stored.
[0170] Delete the firmware upgrade package under the firmware upgrade package path of the initial upgrade metadata, and delete all image files under the image file path of the second-stage upgrade metadata.
[0171] Check if an upgrade completion marker file with a preset name exists in the preset path of the preset read / write partition. If it does not exist, clear the initial upgrade metadata from the preset location used to store the initial upgrade metadata in the boot communication partition, delete all files and directories in the backup directory path of the initial upgrade metadata, and then proceed to step (4-1). If it exists, proceed directly to step (4-1). Step (4-1): Clear the second-stage upgrade metadata in the preset location of the boot communication partition used to save the second-stage upgrade metadata, clear the upgrade instructions in the preset location of the boot communication partition used to save the upgrade instructions, and restart the device to enter the system after normal upgrade.
[0172] 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, a processor 304, and a computer program stored in the memory 302 and executable on the processor. When the processor 304 executes the computer program, it implements the various steps of the firmware upgrade method as described above.
[0173] In summary, the firmware upgrade method and electronic device provided by this invention can support firmware upgrades with arbitrary changes to the partition table, and there are no requirements regarding the position of all partitions in the partition table. Simultaneously, it adaptively supports firmware upgrades in scenarios where the partition table remains unchanged or changes arbitrarily. Furthermore, by jointly storing upgrade instructions through environment variables and the boot communication partition, it ensures the reliability of the upgrade instruction information itself during the upgrade process, guaranteeing the safety and reliability of the upgrade process. Even in the event of an anomaly such as a power outage, the upgrade can be restarted from the unfinished stage after power is restored. Furthermore, it can automatically retain the data in the user data partition and other readable and writable partitions during the upgrade process even if the address spaces of the user data partition and other readable and writable partitions change. Furthermore, by upgrading the upgrade mode firmware in the bootloader firmware, and ensuring the upgrade mode firmware upgrade is successful before entering the upgrade mode firmware to perform other operations, it ensures reliable upgrades to the upgrade mode firmware without creating and using a backup partition, and also ensures reliable upgrades to the upgrade mode firmware itself without creating and using a backup partition. Furthermore, when the address space of the boot communication partition changes, this solution can ensure the safe transfer of data within the boot communication partition without creating or using a backup partition by leveraging environment variables. Compared to existing firmware upgrade methods using ordinary external storage, this solution ensures a safe and reliable upgrade process. Even in the event of an anomaly such as a power outage, the upgrade can be restarted from the unfinished stage after power is restored. It also supports preserving data in user-readable and writable partitions after the upgrade, even if the partition table changes. Simultaneously, this solution upgrades the upgrade mode firmware within the bootloader firmware, ensuring a successful upgrade before entering the upgrade mode firmware to perform other operations. This ensures reliable upgrades to the upgrade mode firmware without creating or using a backup partition. Furthermore, when the address space of the boot communication partition changes, this solution can ensure the safe transfer of data within the boot communication partition without creating or using a backup partition by leveraging environment variables.
[0174] 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's 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 firmware upgrade method characterized by comprising: include: Obtain a firmware upgrade package, which is constructed to include a partition table to be updated and a target bootloader firmware. If it is determined that the first address space of the boot communication partition in the partition table to be updated is different from the second address space of the boot communication partition in the current partition table, and if it is found that the first address space overwrites and destroys the third address space of the boot loader firmware partition in the current partition table, then the update mode is set to boot loader firmware priority mode. Back up the boot communication partition in the current partition table, back up the parameters required to upgrade the target boot loader firmware in the environment variables, obtain the parameters from the environment variables to complete the upgrade of the target boot loader firmware, and then complete the update of the boot communication partition in the partition table to be updated. as well as Update the current partition table to the partition table to be updated.
2. The method of claim 1, wherein, Also includes: When an instruction to upgrade the firmware upgrade package is detected, if the first address space is different from the second address space, the boot communication partition change flag is set to true; if the first address space is the same as the second address space, the boot communication partition change flag is set to no. If the boot communication partition change flag is no, the parameters are directly obtained from the current boot communication partition to complete the upgrade of the target boot loader firmware; If the boot communication partition change flag is true and the update mode is not the boot loader firmware priority mode, then after completing the update of the boot communication partition in the partition table to be updated, the parameters are obtained from the current boot communication partition to complete the upgrade of the target boot loader firmware.
3. The method of claim 1, wherein, Also includes: When an instruction to upgrade the firmware upgrade package is detected, determine whether user data partition backup information has been received. If so, back up the current user data partition. as well as After completing the firmware upgrade, restore the current user data partition.
4. The method of claim 1, wherein, Also includes: When an instruction to upgrade the firmware upgrade package is detected, a second-stage upgrade instruction is written to the first preset location in the boot communication partition and the terminal is restarted.
5. The method of claim 1, wherein, Also includes: When an instruction to upgrade the firmware upgrade package is detected, a first-stage upgrade instruction is written to the first preset location of the boot communication partition and the terminal is restarted; as well as Read the current upgrade instruction in the first preset position. If the current upgrade instruction is the first stage upgrade instruction, set the current upgrade instruction to the second stage upgrade instruction and write the second stage upgrade instruction into the first preset position.
6. The method according to claim 4 or 5, characterized in that, Also includes: When an instruction to upgrade the firmware upgrade package is detected, initial upgrade metadata is constructed based on the first information of the firmware upgrade package, the second information of the external storage medium, the third information of the terminal itself, and the fourth information configured by the user, and the initial upgrade metadata is stored in the second preset location of the boot communication partition; Read the current upgrade instruction in the first preset position; if the current upgrade instruction is the second stage upgrade instruction, then guide the terminal to enter the current upgrade mode firmware. The firmware in the current upgrade mode reads the initial upgrade metadata from the second preset location and obtains the firmware image file according to the firmware upgrade package path in the first information; Create second-stage upgrade metadata, configure the preset image file path and image file information list in the second-stage upgrade metadata, and set the boot communication partition change flag and the update mode in the second-stage upgrade metadata. The image file information list includes multiple image file information items. Save the firmware image file to the preset image file path, and assign an image file information item to each firmware image file; Save the partition table to be updated to the preset image file path, and assign an image file information item to the partition table to be updated; as well as Set the current upgrade command to the third-stage upgrade command and write the third-stage upgrade command into the first preset location.
7. The method according to claim 6, characterized in that, Also includes: When creating the second-stage upgrade metadata, configure the list of readable and writable partitions to be backed up in the second-stage upgrade metadata; Read the current upgrade instruction in the first preset position. If the current upgrade instruction is the third stage upgrade instruction, obtain the backup flag in the first information. If the backup flag is true, read the list of readable and writable partitions to be backed up, and create a backup image file with the partition name of the partition to be backed up in the list of readable and writable partitions to be backed up under the preset image file path. Back up the corresponding partition to be backed up to the backup image file according to the current partition table, and assign an image file information item to each backup image file. If the boot communication partition change flag is true, a backup boot communication partition image file with the partition name of the current boot communication partition as the file name is created under the preset image file path, and the current boot communication partition is backed up to the backup boot communication partition image file according to the current partition table. as well as Set the current upgrade command to the fourth stage upgrade command and write the fourth stage upgrade command into the first preset location.
8. The method according to claim 4 or 5, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is a fourth-stage upgrade instruction, determine whether the update mode is the bootloader firmware priority mode. If so, then set the current upgrade instruction to the fifth stage upgrade instruction and write the fifth stage upgrade instruction into the first preset position; Otherwise, determine whether the guidance communication partition change flag is true; If the boot communication partition change flag is no, then the current upgrade instruction is set to the fifth stage upgrade instruction, and the fifth stage upgrade instruction is written to the first preset position; If the boot communication partition change flag is true, and the update mode is not the boot loader firmware priority mode, then update the boot communication partition to be updated in the firmware upgrade package according to the partition to be updated table, and set the first starting address of the boot communication partition in the current partition table to the second starting address of the boot communication partition in the partition to be updated table, and set the first partition size of the boot communication partition in the current partition table to the second partition size of the boot communication partition in the partition to be updated table. as well as The current upgrade instruction is set as the fifth-stage upgrade instruction, and the fifth-stage upgrade instruction is written into the fourth preset position in the address space corresponding to the boot communication partition in the partition table to be updated.
9. The method according to claim 6, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is a fifth-stage upgrade instruction, back up the current bootloader firmware partition in the current partition table to the bootloader firmware backup partition in the partition table to be updated, set the starting address of the current bootloader firmware partition to the third starting address of the bootloader firmware backup partition, and set the partition size of the current bootloader firmware partition to the third partition size of the bootloader firmware backup partition. If the boot communication partition change flag is no or the update mode is not the bootloader firmware priority mode, then the current upgrade instruction is set to the sixth stage upgrade instruction, and the sixth stage upgrade instruction is written to the first preset position. If the update mode is the bootloader firmware priority mode, then a boot communication partition backup image file is created under the preset image file path, with the partition name of the current boot communication partition in the current partition table as the file name, and the current boot communication partition is backed up to the boot communication partition backup image file according to the current partition table; as well as The parameters required to upgrade the target bootloader firmware are backed up in the environment variables, the current upgrade command is set to the sixth stage upgrade command, and the sixth stage upgrade command is written to the first preset location.
10. The method according to claim 4 or 5, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is the sixth stage upgrade instruction, determine whether the boot communication partition change flag is true. If not, obtain the parameter from the current boot communication partition to complete the upgrade of the target boot program firmware. If the boot communication partition change flag is true and the update mode is not the bootloader firmware priority mode, then the parameters are obtained from the current boot communication partition to complete the upgrade of the target bootloader firmware; If the update mode is the bootloader firmware priority mode, then the parameter is obtained from the environment variable to complete the upgrade of the target bootloader firmware; as well as Set the current upgrade instruction to the seventh-stage upgrade instruction and write the seventh-stage upgrade instruction into the first preset location.
11. The method according to claim 6, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is a seventh-stage upgrade instruction, determine whether the update mode is the bootloader firmware priority mode. If so, the boot communication partition image file is obtained from the preset image file path, the boot communication partition image file is written to the fourth starting address of the boot communication partition in the current partition table, and the current upgrade instruction is set to the eighth stage upgrade instruction, and the eighth stage upgrade instruction is written to the first preset position. Otherwise, directly set the current upgrade instruction to the eighth-stage upgrade instruction and write the eighth-stage upgrade instruction into the first preset position.
12. The method according to claim 6, characterized in that, Also includes: Read the current upgrade instruction in the first preset position; if the current upgrade instruction is the eighth stage upgrade instruction, then determine the format of the firmware upgrade package. If the firmware upgrade package is not in Android format, then according to the preset image file path, obtain other image files to be updated besides the image file of the target bootloader firmware and the image file of the partition table to be updated, write the image files to be updated into the preset position of the partition table to be updated, set the current upgrade instruction to the ninth stage upgrade instruction, and write the ninth stage upgrade instruction into the first preset position. as well as If the firmware upgrade package is in Android format, the target upgrade mode firmware image file is obtained from the preset image file path, and the target upgrade mode firmware image file is written to the starting address of the upgrade mode firmware partition in the partition table file to be updated. The current upgrade instruction is set as the tenth stage upgrade instruction, and the tenth stage upgrade instruction is written to the first preset position.
13. The method according to claim 7, characterized in that, Also includes: Read the current upgrade instruction in the first preset location. If the current upgrade instruction is the ninth stage upgrade instruction, start the upgrade mode firmware. Determine whether the backup flag in the first information is true. If not, format the current user data partition and format each readable and writable partition in the list of readable and writable partitions to be backed up. Set the current upgrade instruction to the thirteenth stage upgrade instruction and write the thirteenth stage upgrade instruction to the first preset location. as well as If the backup flag in the first information is true, then obtain the corresponding target image file according to the target image file information item corresponding to the current user data partition and the list of readable and writable partitions to be backed up, restore each readable and writable partition in the current user data partition and the list of readable and writable partitions to be backed up according to the target image file, set the current upgrade instruction to the thirteenth stage upgrade instruction, and write the thirteenth stage upgrade instruction to the first preset position.
14. The method according to claim 6, characterized in that, Also includes: Read the current upgrade instruction in the first preset location. If the current upgrade instruction is the tenth stage upgrade instruction, use the Android standard Android format package firmware upgrade engine to upgrade the firmware upgrade package under the firmware upgrade package path in the first information, and skip the upgrade of the target bootloader firmware, the target upgrade mode firmware and the partition table file to be updated during the upgrade process. as well as Set the current upgrade command to the ninth stage upgrade command and write the ninth stage upgrade command into the first preset location.
15. The method according to claim 6, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is the thirteenth stage upgrade instruction, read the initial upgrade metadata and check whether the backup flag and user data partition encryption flag in the initial upgrade metadata are both true. If so, create an upgrade completion flag file. Delete the firmware upgrade package under the firmware upgrade package path in the initial upgrade metadata, and delete all image files under the preset image file path; If no upgrade completion marker file exists, then clear the initial upgrade metadata and all files and directories under the backup directory path of the initial upgrade metadata; as well as After clearing the second-stage upgrade metadata and the first preset location, the terminal is restarted.
16. The method according to claim 6, characterized in that, After reading the current upgrade instruction in the first preset location, the process also includes: Save the current upgrade command in the upgrade command environment variable; and After the current upgrade instruction is updated, the upgrade instruction environment variable is deleted.
17. An electronic device, characterized in that, include: The memory is configured to store executable programs; as well as The processor is configured to execute the program to perform the firmware upgrade method according to any one of claims 1 to 16.