Firmware upgrading method and electronic device
By backing up the bootloader firmware during the firmware upgrade process and updating the target firmware in stages, the problem of damage or incompatibility between the bootloader firmware and the upgrade mode firmware during the upgrade process is solved, ensuring that the device can be upgraded and started normally.
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
In existing firmware upgrade solutions, the bootloader firmware and upgrade mode firmware are prone to damage or incompatibility during the upgrade process, which can prevent the device from re-entering upgrade mode or even prevent the device from booting.
During the firmware upgrade process, the current bootloader firmware is backed up first, and the target upgrade mode firmware is installed after the target bootloader firmware is successfully updated. Other firmware upgrades are completed in stages to ensure that the device can be restored to the previous stage in case of an anomaly to avoid the device failing to boot.
It enables the device to enter upgrade mode normally even in case of unexpected situations during firmware upgrade, ensuring that the device can be fully upgraded and avoiding boot problems caused by firmware corruption or incompatibility.
Smart Images

Figure CN122489103A_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] In existing firmware upgrade solutions based on upgrade mode, if an accident (such as a power outage) occurs while upgrading the bootloader firmware (such as Uboot) and the upgrade mode firmware (such as recovery), the bootloader firmware and the upgrade mode firmware will be damaged, making it impossible to re-enter the upgrade mode firmware for a re-upgrade, thus bricking the device.
[0003] Furthermore, existing firmware upgrade solutions suffer from compatibility issues between the bootloader firmware and the upgrade mode firmware. If an unexpected event occurs during the upgrade process for either the bootloader firmware or the upgrade mode firmware, the incompatibility between them can prevent the user from re-entering the upgrade mode firmware for further upgrades. For example, some specific configurations of the new bootloader firmware may cause incompatibility with the old upgrade mode firmware, preventing the new bootloader firmware from correctly loading the old upgrade mode firmware or causing the old upgrade mode firmware to boot abnormally. If the new bootloader firmware is successfully upgraded within the upgrade mode firmware and a power outage occurs before the new upgrade mode firmware upgrade, the new bootloader firmware will fail to load the old upgrade mode firmware correctly or cause the old upgrade mode firmware to boot abnormally. Summary of the Invention
[0004] This invention provides a firmware upgrade method and an electronic device that can still enter the upgrade mode and perform firmware upgrades normally even if unexpected events occur during the firmware upgrade process.
[0005] 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 including a target bootloader firmware and a target upgrade mode firmware; when an instruction to upgrade the firmware upgrade package is detected, backing up the current bootloader firmware and updating the current bootloader firmware to the target bootloader firmware; and after the target bootloader firmware is successfully updated, installing the target upgrade mode firmware and completing the upgrade process of other firmware according to the firmware upgrade package.
[0006] 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 firmware upgrade method described above.
[0007] According to the technical solution of the present invention, a firmware upgrade package is obtained. When an instruction to upgrade the firmware upgrade package is detected, the current bootloader firmware is backed up first, and then the current bootloader firmware is upgraded to the target bootloader firmware in the firmware upgrade package. In this way, even if a problem occurs during the upgrade process, the boot process can be completed using the backed-up current bootloader firmware, avoiding the device from failing to boot. After confirming that the target bootloader firmware has been successfully updated, the target upgrade mode firmware is installed. In this way, even if an unexpected situation occurs during the installation of the target upgrade mode firmware, the installation process can be restarted based on the successfully updated target bootloader firmware, further avoiding the device from failing to boot. After both the target bootloader firmware and the target upgrade mode firmware have been updated, the upgrade process of other firmware in the firmware upgrade package continues, ensuring that the complete upgrade process of the firmware upgrade package can be completed. Attached Figure Description
[0008] 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
[0009] 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.
[0010] In existing technologies, if the firmware boot process or the upgrade process of the bootloader firmware or upgrade mode firmware fails, it will be impossible to continue booting other firmware, resulting in the terminal being unable to upgrade normally or restore the firmware before the upgrade, thus making the device "bricked".
[0011] To address at least the aforementioned technical problems, this disclosure provides a firmware upgrade method. According to this disclosure, when starting to upgrade the firmware upgrade package, the current bootloader firmware is first backed up. After ensuring the target bootloader firmware in the firmware upgrade package is successfully updated, the target upgrade mode firmware in the firmware upgrade package is then updated. After the target upgrade mode firmware is updated, other firmware in the firmware upgrade package is updated based on the target upgrade mode firmware. In this way, according to embodiments of this disclosure, the current bootloader firmware can be restored to continue normal terminal startup if the target bootloader firmware update fails. Furthermore, by using a phased update approach, the target upgrade mode firmware is updated only after the target bootloader firmware update is successful, and other firmware in the firmware upgrade package is updated only after the target upgrade mode firmware update is successful. This facilitates reverting to the previous stage at any time in case of an anomaly, thus avoiding situations where the terminal cannot boot.
[0012] 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.
[0013] 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.
[0014] In step 102, a firmware upgrade package is obtained, which includes a target bootloader firmware and a target upgrade mode firmware.
[0015] In some embodiments, obtaining a firmware upgrade package includes: obtaining a firmware upgrade package containing a user data incompatibility file with a preset name, wherein the user data incompatibility file is stored in the firmware upgrade package when there is a target firmware that is incompatible with the current user data partition, and the user data incompatibility file stores a first file system type of the target user data partition after the target firmware upgrade.
[0016] In this way, to address the issue of incompatibility between user data partitions before and after firmware upgrades, a user data incompatibility file is saved in advance. This file stores the first file system type of the user data partition after the firmware upgrade, and the current user data partition is backed up when a firmware upgrade package is needed.
[0017] In some embodiments, the method 100 may further include: when an instruction to upgrade the firmware upgrade package is detected, if the firmware upgrade package contains user data incompatible files, backing up the current user data partition; and after completing the upgrade process of other firmware according to the firmware upgrade package, restoring the backed-up current user data partition.
[0018] In this way, data can be restored through the backed-up current user data partition after the firmware upgrade package is completed, avoiding the problem of data loss in the user data partition after the firmware upgrade.
[0019] In some embodiments, backing up the current user data partition includes: if the encryption flag of the current user data partition is not specified, obtaining the second file system type of the current user data partition and determining whether the first file system type is the same as the second file system type; if they are different or the encryption flag of the current user data partition is specified, setting the backup type in the initial upgrade metadata to file backup, receiving the data to be backed up, storing the data to be backed up in a preset backup directory on the external storage medium, and setting the backup directory path in the initial upgrade metadata to a path relative to the root directory of the external storage medium; and if they are the same and the received backup configuration is full backup, setting the backup type in the initial upgrade metadata to full backup, storing all data in the current user data partition in a preset backup directory on the external storage medium, and setting the backup directory path in the initial upgrade metadata to a path relative to the root directory of the external storage medium.
[0020] In this way, the system determines whether to perform a file backup or a full backup based on the encryption marker and file system type of the current user data partition. For file backups, users can select the files to be backed up, reducing the amount of data that needs to be backed up and speeding up the recovery process after a successful firmware upgrade. A full backup completely avoids data loss. If the data to be backed up is stored in a preset backup directory on an external storage medium, the data can be accessed during the firmware upgrade process to complete the backup.
[0021] In step 104, when an instruction to upgrade the firmware upgrade package is detected, the current bootloader firmware is backed up and the current bootloader firmware is updated to the target bootloader firmware.
[0022] In some embodiments, the method 100 may further include: when an instruction to upgrade the firmware upgrade package is detected, writing a second-stage upgrade instruction in a first preset location in the boot communication partition and restarting the terminal.
[0023] In this way, when it is confirmed that a firmware upgrade package needs to be upgraded, the second-stage upgrade instruction is directly written to prepare the upgrade information, thereby improving execution efficiency.
[0024] 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 in 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, creating a new upgrade instruction information environment variable in the environment variables and using the first-stage upgrade instruction as the value of the upgrade instruction information environment variable; setting the current upgrade instruction to a second-stage upgrade instruction, writing the second-stage upgrade instruction in the first preset location, and deleting the upgrade instruction information environment variable.
[0025] In this way, an upgrade command information environment variable is established at the start of the upgrade process. This environment variable is a special, fixed storage area that is typically only directly accessible to the bootloader firmware. This allows the system to determine the current execution stage by comparing the value in the environment variable with the value of the current upgrade command stored in a first preset location, even if problems occur during execution. This avoids the problem of being unable to enter the upgrade process after a firmware upgrade failure. Compared to directly writing the second-stage upgrade command, writing the first-stage upgrade command first indicates that the upgrade process has begun, and then writing the second-stage upgrade command to guide the specific execution steps. This approach is logically clearer and facilitates pinpointing the timing of any failures.
[0026] 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, obtaining the firmware upgrade package format and firmware image file from the first information; creating second-stage upgrade metadata, configuring a preset image file path and an image file information list in the second-stage upgrade metadata. The image file information list includes multiple image file information items; image files are read sequentially from the firmware upgrade package; for each target image file read, the target image file is stored in the preset image file path, and an unused image file information item is obtained from the image file information list as the target file information item of the target image file, and the target file information item is saved in a target file information item file named after the file name of the target image file; the image file includes the image file corresponding to the target bootloader firmware and the image file corresponding to the target upgrade mode firmware; the second-stage upgrade metadata is written to the third preset location of the boot communication partition, and the current upgrade instruction is set to the third-stage upgrade instruction, the third-stage upgrade instruction is written to the first preset location, and the terminal is restarted.
[0027] In this way, image file information items are assigned to the image files that need to be upgraded in the firmware upgrade package, and the image files are saved in the preset image file path. In the subsequent upgrade process, the path where the image file is located can be found according to the image file information items. Furthermore, it is possible to make a separate judgment on whether each image file should be upgraded, so as to realize a staged upgrade process, that is, first upgrade the target boot loader firmware and then upgrade the target upgrade mode firmware, so that if the upgrade fails, it can return to the previous stage at any time to avoid the device freezing.
[0028] In some embodiments, after reading the current upgrade instruction in the first preset location, the process may further include: checking whether an upgrade instruction information environment variable exists in the environment variables through the current bootloader firmware; if so, obtaining the current upgrade instruction based on the value of the upgrade instruction information environment variable; otherwise, obtaining the current upgrade instruction from the first preset location; and reading initial upgrade metadata or second-stage upgrade metadata from the boot communication partition, verifying the legality of the initial upgrade metadata or second-stage upgrade metadata based on the current upgrade instruction, and executing the current upgrade instruction if both verifications pass.
[0029] In this way, before executing the current upgrade command, the validity of the initial upgrade metadata or the second-stage upgrade metadata is checked. If the verification is successful, the current upgrade command will continue to be executed. If the verification fails, the upgrade process will be exited. This method avoids security problems caused by the tampering of the initial upgrade metadata and the second-stage upgrade metadata.
[0030] 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 third-stage upgrade instruction, then creating a new upgrade instruction information environment variable in the environment variables and setting the third-stage upgrade instruction as the value of the upgrade instruction information environment variable; obtaining the first starting address and the first partition size of the current bootloader firmware partition in the current partition table; creating a first starting address environment variable and a first partition size environment variable in the environment variables, setting the value of the first starting address environment variable to the first starting address, and setting the value of the first partition size environment variable to the first partition size; finding a backup partition in the current partition table whose partition size is greater than or equal to the first partition size, reading data of the first partition size starting from the first starting address, and writing the data to the second starting address of the backup partition; modifying the first starting address of the current bootloader firmware partition in the current partition table to the second starting address; setting the current upgrade instruction to a fourth-stage upgrade instruction, writing the fourth-stage upgrade instruction to the first preset location, and deleting the upgrade instruction information environment variable.
[0031] In this way, the current bootloader firmware is backed up from the first starting address to the second starting address, and the current partition table is modified accordingly. The first starting address and the size of the first partition are recorded in the environment variables. Then, if the upgrade fails during the subsequent upgrade of the target bootloader firmware, the current bootloader firmware saved in the second starting address can be retrieved from the current partition table for restoration, thereby avoiding the problem of the terminal becoming unusable after freezing during the upgrade stage.
[0032] 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 fourth-stage upgrade instruction, then creating a new upgrade instruction information environment variable in the environment variables and using the fourth-stage upgrade instruction as the value of the upgrade instruction information environment variable; reading the first starting address environment variable and the first partition size environment variable, writing the first image file corresponding to the target bootloader firmware into the first starting address stored in the first starting address environment variable; modifying the second starting address in the current partition table to the first starting address; setting the current upgrade instruction to the fifth-stage upgrade instruction, writing the fifth-stage upgrade instruction into the first preset location and deleting the upgrade instruction information environment variable.
[0033] If the write operation is successful, it indicates that the target bootloader firmware has been successfully updated. At this point, the address in the current partition table is modified to the first starting address, allowing normal access to the target bootloader firmware to complete the subsequent firmware update process. If the write operation fails, the current partition table will still store the second starting address corresponding to the current bootloader firmware, allowing normal restoration of the system before the update. This avoids situations where the device is completely unable to boot.
[0034] 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, creating a new upgrade instruction information environment variable in the environment variables and using the fifth-stage upgrade instruction as the value of the upgrade instruction information environment variable; deleting the first starting address environment variable and the first partition size environment variable; setting the current upgrade instruction to a sixth-stage upgrade instruction, writing the sixth-stage upgrade instruction to the first preset location and deleting the upgrade instruction information environment variable. It can be seen that if the fifth-stage upgrade instruction can be successfully read, it indicates that the current partition table has been modified normally. At this point, deleting the first starting address environment variable and the first partition size environment variable, and modifying the current upgrade instruction to the sixth-stage upgrade instruction, allows the subsequent stage upgrade process to continue.
[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 sixth-stage upgrade instruction, then creating a new upgrade instruction information environment variable in the environment variables and using the sixth-stage upgrade instruction as the value of the upgrade instruction information environment variable; reading the second-stage upgrade meta-information from the third preset location, obtaining the firmware upgrade package format in the second-stage upgrade meta-information; if the firmware upgrade package format is Android format, then searching for the second image file information item corresponding to the target upgrade mode firmware from the image file information list of the second-stage upgrade meta-information; reading the fourth starting address and the fourth partition size corresponding to the upgrade mode firmware partition from the current partition table; obtaining the second image file corresponding to the target upgrade mode firmware according to the image file path and the second image file information item in the second-stage upgrade meta-information, and writing the second image file to the fourth starting address; and setting the current upgrade instruction to an eighth-stage upgrade instruction, writing the eighth-stage upgrade instruction to the first preset location and deleting the upgrade instruction information environment variable.
[0036] In this way, after the target bootloader firmware is successfully updated, the upgrade mode firmware continues to be updated. If it is in Android format, the second image file corresponding to the upgrade mode firmware in the firmware upgrade package is obtained according to the image file path and the second image file information item saved in the second-stage upgrade metadata to complete the update process, so as to complete the update process of other firmware to be updated in subsequent stages through the target upgrade mode firmware.
[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 an eighth-stage upgrade instruction, then creating a new upgrade instruction information environment variable in the environment variables and using the eighth-stage upgrade instruction as the value of the upgrade instruction information environment variable; setting the current upgrade instruction to a ninth-stage upgrade instruction, writing the ninth-stage upgrade instruction into the first preset location and deleting the upgrade instruction information environment variable.
[0038] In this way, after confirming that the firmware update in upgrade mode is complete, the subsequent stages of the update process will continue to be executed, so as to avoid the problem of the terminal failing to boot due to the failure of the firmware update in upgrade mode.
[0039] In step 106, after the target bootloader firmware is successfully updated, the target upgrade mode firmware is installed and other firmware upgrades are completed according to the firmware upgrade package.
[0040] In some embodiments, after obtaining the firmware upgrade package format in the second-stage upgrade metadata, the method may further include: if the firmware upgrade package format is not Android format, searching for a third image file information item other than the first image file information item from the image file information list of the second-stage upgrade metadata; reading the fifth starting address and fifth partition size corresponding to each third image file information item from the partition table; obtaining at least one third image file according to the image file path in the second-stage upgrade metadata and the third image file information item; writing the third image file to the fifth starting address corresponding to the third image file; and after all the third image files are successfully written, setting the current upgrade instruction to the seventh-stage upgrade instruction, writing the seventh-stage upgrade instruction to the first preset position, and deleting the upgrade instruction information environment variable.
[0041] In this way, when the firmware upgrade package format is not Android format, the third image file corresponding to each firmware to be upgraded, except for the target bootloader firmware, is written into the fifth starting address corresponding to the third image file in the current partition table to complete the update process of other firmware and realize the complete upgrade process of the firmware upgrade package.
[0042] In some embodiments, after creating the second-stage upgrade metadata, the method may further include: setting a user data incompatibility flag in the second-stage upgrade metadata; if the firmware upgrade package contains target firmware that is incompatible with the user data partition, then setting the user data incompatibility flag to true; determining whether the backup flag of the initial upgrade metadata is true; if so, determining whether the backup type of the initial upgrade metadata is a full backup; if it is a full backup, then creating a user data partition image file with the partition name of the current user data partition as the filename under the preset image file path; opening the block device node of the current user data partition and saving the data in the block device node to the user data partition image file; obtaining an unused image file information item from the image file information list as the fourth file information item of the user data partition image file, and saving the fourth file information item in a fourth file information item file named after the filename of the user data partition image file.
[0043] In this way, after creating the new second-stage upgrade metadata, if there are user incompatibility flags and the backup type is full backup, the full backup process is completed. This way, after the subsequent update is completed, the data can be restored based on the full backup.
[0044] 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 seventh-stage upgrade instruction, then creating a new upgrade instruction information environment variable in the environment variables and using the seventh-stage upgrade instruction as the value of the upgrade instruction information environment variable; reading the user incompatibility flag in the second-stage upgrade metadata; if it is true, then setting the current upgrade instruction to a tenth-stage upgrade instruction, writing the tenth-stage upgrade instruction into the first preset location and deleting the upgrade instruction information environment variable; if it is not true, then clearing the first preset location, the second preset location, and the third preset location of the boot communication partition and deleting the upgrade instruction information environment variable, thereby completing the upgrade process.
[0045] In this way, if there is a target firmware that is incompatible with the user data partition, the user data incompatibility flag in the second-stage upgrade metadata will be set to true, and the current user data partition will be backed up as a user data partition image file. During the upgrade process, if the user data partition is incompatible before and after the upgrade, the backup and recovery process can be completed through the backed-up user data partition image file, avoiding the loss of data in the current user data partition.
[0046] 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 guiding the terminal to enter the target upgrade mode firmware; using the Android standard Android format package firmware upgrade engine to upgrade the firmware upgrade package under the firmware upgrade package path of the initial upgrade metadata through the target upgrade mode firmware, and skipping the upgrade of the target bootloader firmware and the target upgrade mode firmware during the upgrade process; verifying whether the user data incompatibility flag in the second-stage upgrade metadata is true; if so, setting the current upgrade instruction to a tenth-stage upgrade instruction and writing the tenth-stage upgrade instruction into the first preset location; otherwise, setting the current upgrade instruction to an eleventh-stage upgrade instruction and writing the eleventh-stage upgrade instruction into the first preset location.
[0047] In this way, if it is an Android format firmware upgrade package, the standard Android format package firmware upgrade engine can be used to upgrade the firmware to be upgraded. Since the target bootloader and the target upgrade mode firmware have been upgraded in the previous steps, the upgrade process of the two can be skipped and the upgrade of other firmware to be upgraded can be continued. This method leaves a trace of each stage of the upgrade so that the firmware upgrade process can be restored at any time.
[0048] In some embodiments, after completing the upgrade process of other firmware according to the firmware upgrade package, the process includes: restoring the backed-up current user data partition. In this way, if the target data partition for the firmware upgrade and the current data partition are incompatible, the current data partition is backed up first, and the data in the backed-up current data partition is restored after the upgrade is completed. This avoids data loss caused by incompatibility of user data partitions after the firmware upgrade.
[0049] In some embodiments, the current upgrade instruction in the first preset location is read. If the current upgrade instruction is a tenth-stage upgrade instruction, the terminal is guided to enter the target upgrade mode firmware. The fourth file information item and the user data incompatible file information item corresponding to the image file of the user data partition are searched from the image file information list of the second-stage upgrade meta-information using the target upgrade mode firmware. The user data incompatible file is obtained according to the image file path in the second-stage upgrade meta-information and the user data incompatible file information item, and the first file system type is read from the user data incompatible file. The user data partition image file is obtained according to the image file path and the fourth file information item. The user data partition is formatted according to the first file system type. After formatting, the user data partition image file is restored to the user data partition. The current upgrade instruction is set to the eleventh-stage upgrade instruction, and the eleventh-stage upgrade instruction is written to the first preset location.
[0050] In this way, when the user data partition format is mismatched before and after the firmware upgrade, the first file system type is obtained from the incompatible file pre-saved in the firmware upgrade package, and the user data partition is formatted according to the first file system type so that the user data partition can adapt to the requirements after the firmware upgrade. After formatting, the user data partition image file is restored to the user data partition, thus avoiding the problem of data loss in the user data partition while solving the compatibility problem.
[0051] 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 eleventh-stage upgrade instruction, then guiding the terminal into the target upgrade mode firmware; deleting the firmware upgrade package under the firmware upgrade package path of the initial upgrade metadata through the target upgrade mode firmware, and deleting all image files under the image file path of the second-stage upgrade metadata; after clearing the first preset location and the third preset location of the boot communication partition, restarting the terminal to complete the upgrade process of the firmware upgrade package.
[0052] In this way, after reading the eleventh-stage upgrade instruction, it means that the upgrade process of the firmware to be upgraded in the firmware upgrade package has been completed. At this time, the intermediate data saved during the terminal upgrade process is deleted to ensure that the terminal can be used normally after restarting and will not enter the upgrade process again.
[0053] Figure 2 This is a flowchart illustrating the application of 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 214.
[0054] In step 201, obtain the firmware upgrade package. If this upgrade will cause the upgraded firmware to be incompatible with the current user data partition (e.g., userdata), a user data incompatibility file with a preset name will be included in the firmware upgrade package. The user data incompatibility file with the preset name must contain the file system type of the target user data partition after the upgrade. Regardless of whether it is in Android format, a bootloader firmware exists.
[0055] When the firmware upgrade client runs, it first checks whether an upgrade completion marker file with a preset name (corresponding to the upgrade completion marker file created in stage 11) exists in the preset path of the preset read-write partition. If it exists, it reads the initial upgrade metadata from the preset location in the boot communication partition used to store the initial upgrade metadata and checks the validity of the initial upgrade metadata. If the initial upgrade metadata is valid, it mounts the external storage medium according to the file system type of the external storage medium of the initial upgrade metadata. In the root directory of the external storage medium, it obtains the backup file compressed package with a preset name in the backup directory path of the initial upgrade metadata. It calculates the digital digest value of the backup file compressed package 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 compressed package to the mount directory of the user data partition according to the preset decompression algorithm, then deletes the backup file compressed package and the upgrade completion marker file, and clears the initial upgrade metadata in the preset location in the boot communication partition used to store the initial upgrade metadata.
[0056] 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. Otherwise, the digital digest value of the content consisting of the file system type of the external storage medium, 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.
[0057] In step 202, the initial upgrade metadata is constructed, including steps 2021 to 2025.
[0058] In step 2021, when the firmware upgrade client detects a new firmware upgrade package that needs to be upgraded, it obtains the firmware upgrade package and performs a validity check on it. If the firmware upgrade package is invalid, the firmware upgrade process is terminated; if the firmware upgrade package is valid, step 2022 is executed. The validity check of the firmware upgrade package can be performed through methods such as signature verification, which are not limited here.
[0059] In step 2022, 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 step 2023. 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 step 2024.
[0060] In step 2023, an initial upgrade metadata is constructed, 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. The magic number and digital digest algorithm type of the initial upgrade metadata are set to a preset value. The file system type of the external storage medium is obtained, and the file system type of the external storage medium in the initial upgrade metadata is set to the obtained file system type of the external storage medium. The firmware upgrade package path of the initial upgrade metadata is set to a preset path relative to the root directory of the external storage medium.
[0061] 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., userdata) is encrypted. If not, set the encryption flag of the user data partition in the initial upgrade metadata to no; if it is encrypted, 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.
[0062] Check if there is a user data incompatibility file with a preset name in the firmware upgrade package. If it exists, proceed to step 2024; otherwise, set the backup flag for the initial upgrade metadata to no and proceed to step 2025.
[0063] In step 2024, remind the user that this upgrade will cause the data in the user data partition (such as userdata) to be unavailable after the upgrade, and the user data partition needs to be reformatted. 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 2025; otherwise, set the backup flag of the initial upgrade metadata to true, 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-1-1.
[0064] In step 4-1, read the fstab file (e.g., / vendor / etc / fstab.rk30board) under the system's preset path and check whether the file system type of the preset user data partition is the same as the file system type of the upgraded user data partition in the user data incompatibility file with the preset name in the firmware upgrade package. If they are different, proceed to step 4-1-1; if they are the same, proceed to step 4-1-2.
[0065] In step 4-1-1, the backup type of the initial upgrade metadata is set to file backup, and the user specifies the list of files or directories to be backed up under the user data partition. Then, the list of files or directories to be backed up is saved to the preset backup directory of the external storage medium according to the same directory hierarchy. The backup directory path of the initial upgrade metadata is set to the path of the preset backup directory relative to the root directory of the external storage medium. All contents under the backup directory path of the initial upgrade metadata are compressed according to the preset compression algorithm to generate a backup file archive with a preset name. The digital digest value of the backup file archive is calculated according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata, and the calculated digital digest value is saved to the backup data digital digest value of the initial upgrade metadata. Then, 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 are deleted. Then, proceed to step 2025.
[0066] In step 4-1-2, the user is prompted whether to perform a full backup of the user data partition. If yes, the backup type of the initial upgrade metadata is set to full backup. If no, the process returns to step 4-1-1.
[0067] In step 2025, the digital digest value of the contents 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 meta information is calculated according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade meta information, and the calculated digital digest value is saved to the meta information digital digest value of the initial upgrade meta information.
[0068] The initial upgrade metadata is written to a preset location in the boot communication partition used to store the initial upgrade metadata. The preset first-stage upgrade command is written to a preset location in the boot communication partition used to store the upgrade command, and the device restarts.
[0069] In step 203, before executing the current upgrade command, the validity of the initial upgrade metadata or the second-stage upgrade metadata is determined, 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 takes the value of a preset valid upgrade command, then the value of the upgrade command information environment variable is taken as the current upgrade command, and then step 2032 is executed; otherwise, step 2031 is executed.
[0070] Among them, 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, and eleventh-stage upgrade command.
[0071] In step 2031, the upgrade command is read from the preset location in the boot communication partition where the upgrade command is stored, and the read upgrade command is used as the current upgrade command. Then, step 2032 is executed. If a valid upgrade command cannot be obtained in step 2031, the firmware upgrade process is exited.
[0072] In step 2032, the validity of the initial upgrade metadata or the second-stage upgrade metadata is checked according to the value of the current upgrade instruction. If either is invalid, the firmware upgrade process is exited; otherwise, the current upgrade instruction is executed.
[0073] 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. Otherwise, the digital digest value of the content consisting of the file system type of the external storage medium, 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.
[0074] 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. Otherwise, a digital digest value is calculated based on the digital digest algorithm specified by the digital digest algorithm type of the second-stage upgrade metadata. This digital digest value consists of the firmware upgrade package format, image file path, image file information list, backup image information list, and user data incompatibility flag. 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.
[0075] In step 204, check if the current upgrade command is a first-stage upgrade command. If so, 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 first-stage upgrade command.
[0076] Set the current upgrade command to the preset second-stage upgrade command, 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.
[0077] In step 205, check whether the current upgrade command is the preset second-stage upgrade command. If so, guide the terminal into the upgrade mode firmware.
[0078] 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.
[0079] Construct the second-stage upgrade metadata, consisting of a magic number, digital digest algorithm type, metadata digital digest value, firmware upgrade package format, image file path, image file information list, backup image information list, and user data incompatibility flag. Initialize all second-stage upgrade metadata to empty (e.g., all 0s). The image file information list and backup image information list are both lists composed of image file information items, each consisting of the image file name and the image file's digital digest value.
[0080] 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.
[0081] In step 3-2-2, 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. Specifically, this includes the following steps a to c.
[0082] In step a), check if a file with the .br extension exists in the firmware upgrade package. If not, proceed to step b; otherwise, 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 target bootloader firmware image file information item. Read the target bootloader firmware from the firmware upgrade package and save it as the target bootloader firmware image file to the image file path in the second-stage upgrade metadata. Set the image file name of the target bootloader firmware image file information item to the file name of the bootloader firmware image file under the image file path, that is, save the firmware image file information item in file 1, and set the file name of file 1 to the file name of the bootloader firmware image file corresponding to the image file information item. Calculate the digital digest of the bootloader 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 bootloader firmware image file information item.
[0083] Find an unused image file information item from the image file information list of the second-stage upgrade metadata and use it as the target upgrade mode firmware file information item. Read the target upgrade mode firmware from the firmware upgrade package and save it as the target upgrade mode firmware image file to the image file path of the second-stage upgrade metadata; set the image file name of the target 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.
[0084] If a user data incompatible file with a preset name exists in the firmware upgrade package, firstly, an unused image file information item is found in the image file information list of the second-stage upgrade metadata and designated as the user data incompatible file information item. Then, the user data incompatible file with the preset name is read from the firmware upgrade package and saved as the user data incompatible file to the image file path of the second-stage upgrade metadata; the image file name of the user data incompatible file information item is set to the file name of the user data incompatible file under the image file path; the digital digest algorithm type of the second-stage upgrade metadata is used to calculate the digital digest of the user data incompatible file and saved to the image file digital digest value of the user data incompatible file information item.
[0085] In step b, check if the payload.bin file exists in the upgrade package. If it does not exist, proceed to step c; otherwise, remind the user that the upgrade package is invalid and exit the firmware upgrade process. This is because if the payload.bin file exists, it means that the upgrade package is an Android standard AB upgrade package, which cannot be used in upgrade mode.
[0086] Step c: Set the firmware upgrade package format for the second-stage upgrade metadata to a firmware image file package. The firmware upgrade package reads each image file 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 from the image file information list in the second-stage upgrade metadata and designated 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 the result is saved to the image file digital digest value of the current image file information item. Specifically, each image file in the firmware upgrade package is read through the image file path specified in step 3-2-2.
[0087] Step 3-2-3: Check if there is a user data incompatibility file with a preset name in the firmware upgrade package. If it does not exist, set the user data incompatibility flag in the second-stage upgrade metadata to no; otherwise, set the user data incompatibility flag in the second-stage upgrade metadata to true.
[0088] Step 3-2-4: Check if the user data incompatibility flag in the second-stage upgrade metadata is true. If so, check if the backup flag in the initial upgrade metadata is true. If so, check if the user data partition encryption flag in the initial upgrade metadata is false. If so, check if the backup type of 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 as the user data partition backup image file information item, and 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 second-stage upgrade metadata and save it to the image file digital digest value of the user data partition backup image file information item.
[0089] In step 3-2-5, the digital digest value of the content consisting of the firmware upgrade package format, image file path, image file information list, backup image information list, and user data incompatibility flag of the second-stage upgrade metadata is calculated according to the digital digest algorithm specified by the digital digest algorithm type of the second-stage upgrade metadata, and the calculated digital digest value is saved to the metadata digital digest value of the second-stage upgrade metadata; the second-stage upgrade metadata is written to the preset location in the boot communication partition used to save the second-stage upgrade metadata, and the preset third-stage upgrade command is written to the preset location in the boot communication partition used to save the upgrade command, and the device restarts.
[0090] In step 206, the backup process of the current bootloader firmware is executed. It checks whether the current upgrade command is the preset third-stage upgrade command. If so, a new preset upgrade command information environment variable is created in the environment variables, and the value of the upgrade command information environment variable is set to the third-stage upgrade command. The current partition table is read from the preset partition table address space of the storage medium, and then step 2-7-2 is executed.
[0091] In step 2-7-2, the starting address and size of the bootloader firmware partition in the current partition table are referred to as the bootloader firmware partition starting address and bootloader firmware partition size, respectively. Check if a preset bootloader firmware partition starting address environment variable exists. If not, create a new preset bootloader firmware partition starting address environment variable and set its value to the bootloader firmware partition starting address. This second check is a preventative measure, as previously created environments may lose data due to abnormal power outages. This second check ensures that the bootloader firmware partition starting address environment variable is available after entering subsequent programs. Check if a preset environment variable for the bootloader firmware partition size exists in the environment variables. If it does not exist, create a new environment variable for the bootloader firmware partition size in the environment variables and set its value to the bootloader firmware partition size.
[0092] Find a firmware partition to be upgraded in the current partition table whose size is not smaller than that of the bootloader firmware partition, and use it as the bootloader firmware backup partition. The firmware partition to be upgraded cannot be the bootloader firmware partition, boot communication partition, user data partition, or any preset read / write partition (such as cache partition, metadata partition, etc.).
[0093] Read data of the same size as the bootloader firmware partition from the starting address of the bootloader firmware partition, and then write the read data (i.e. the current bootloader firmware) to the starting address of the bootloader firmware backup partition, with the written data size being the same as the bootloader firmware partition size.
[0094] 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.
[0095] The current upgrade command is set to the preset fourth-stage upgrade command, and written to the preset location in the boot communication partition used to save upgrade commands. The upgrade command information environment variable is deleted. No restart is needed here, as the bootloader firmware has already booted; the fourth-stage command can be executed directly to upgrade the bootloader firmware. If an error occurs during the upgrade process, a restart will load the bootloader firmware from the configured bootloader firmware backup partition.
[0096] In step 207, the target bootloader firmware update process is executed. It checks if the current upgrade command is a preset fourth-stage upgrade command. If so, it checks if a preset upgrade command information environment variable exists in the environment variables. If not, it creates a new preset upgrade command information environment variable and sets its value to the preset fourth-stage upgrade command. The current partition table is read from the preset partition table address space of the storage medium. Initial upgrade metadata is read from the second preset location in the boot communication partition used to store initial upgrade metadata. The external storage medium is mounted according to the file system type of the external storage medium containing the initial upgrade metadata.
[0097] 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 bootloader firmware partition start address environment variable is obtained from the environment variables, and its value is used as the initial start address of the bootloader firmware partition. The bootloader firmware partition size environment variable is obtained from the environment variables, and its value is used as the initial size of the bootloader firmware partition. If an error occurs during the bootloader firmware upgrade, the restarted terminal will load the bootloader firmware from the bootloader firmware backup partition, thus continuing the bootloader firmware upgrade.
[0098] The image file information item corresponding to the target bootloader firmware is located in the image file information list of the second-stage upgrade metadata. The bootloader firmware image file is retrieved from the external storage medium according to the image file path in the second-stage upgrade metadata. A digital digest of the bootloader firmware image file is calculated based on the digital digest algorithm type in the second-stage upgrade metadata. The calculated digital digest is compared with the image file digital digest value of the bootloader firmware image file information item. If they do not match, an error is displayed, and the firmware upgrade process exits. Otherwise, the bootloader firmware image file is written to the initial starting address of the bootloader firmware partition. The write size is the minimum between the bootloader firmware image file size and the initial size of the bootloader firmware partition. Normally, when generating the bootloader firmware image file, its size is ensured not to exceed the initial size of the bootloader firmware partition. This check is a precautionary measure to prevent data writes exceeding the partition size from damaging subsequent partitions in case of unexpected situations.
[0099] Modify the starting address of the bootloader firmware partition in the current partition table to the initial starting address of the bootloader firmware partition, and write the modified current partition table into the preset partition table address space of the storage medium.
[0100] Set the current upgrade command to the preset fifth-stage upgrade command, 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.
[0101] In step 208, check if the current upgrade command is the preset fifth-stage upgrade command. If so, check if the preset upgrade command information environment variable exists in the environment variables. If it does not exist, create the preset upgrade command information environment variable in the environment variables and set its value to the preset fifth-stage upgrade command. A separate stage upgrade command is designed here to avoid abnormal power loss after modifying the partition table without completely deleting everything. For example, if deleting the "delete bootloader firmware partition start address environment variable" causes an abnormal power loss, and the reboot still uses the fourth-stage command, continuing to execute the fourth-stage command for the upgrade will cause problems. Therefore, this potential problem is avoided by using a separate stage.
[0102] Delete the environment variable for the starting address of the bootloader firmware partition, delete the environment variable for the size of the bootloader firmware partition, set the current upgrade command to the sixth stage upgrade command, write the current upgrade command to the preset location in the boot communication partition used to save the upgrade command, and delete the environment variable for upgrade command information.
[0103] In step 209, check if the current upgrade command is the preset sixth-stage upgrade command. If so, create a new preset upgrade command information environment variable and set its value to the sixth-stage upgrade command. Read the current partition table from the preset partition table address space of the storage medium. 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 containing the initial upgrade metadata.
[0104] Read the second-stage upgrade metadata from the preset location in the boot communication partition where the second-stage upgrade metadata is stored. Check if the firmware upgrade package format of the second-stage upgrade metadata is an Android format package. If it is, execute step 2-10-2 below; otherwise, execute step 2-10-3 below.
[0105] Step 2-10-2: Locate the image file information item corresponding to the target upgrade mode firmware from the image file information list of the second-stage upgrade metadata. Obtain the target upgrade mode 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 upgrade mode 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 upgrade mode firmware 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 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 eighth-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.
[0106] Step 2-10-3: Sequentially search the image file information list of the second-stage upgrade metadata for the image file information item excluding the bootloader firmware file information item and the user data incompatible file information item, and use it as the current image file information item. According to the image file path of 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 are inconsistent, prompt an error and exit the firmware upgrade process; otherwise, 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 value between the size of the firmware image file to be upgraded and the size of the firmware partition corresponding to the firmware image file.
[0107] When all the firmware image files corresponding to the image file information items in the image file information list of the second-stage upgrade metadata (excluding the bootloader firmware file information item and the user data incompatible file information item) are successfully written to the address space of the firmware partition corresponding to the corresponding firmware image file, and the firmware upgrade package upgrade is completed, the current upgrade command is set to the preset seventh-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, and the upgrade command information environment variable is deleted; otherwise, an error is displayed, and the firmware upgrade process is exited.
[0108] In step 210, check if the current upgrade command is the preset seventh-stage upgrade command. If so, create a new preset upgrade command information environment variable and set its value to the preset seventh-stage upgrade command. Read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata. Check if the user data incompatibility flag in the second-stage upgrade metadata is true. If it is, execute step 2-11-2; otherwise, execute step 2-11-3.
[0109] In step 2-11-2, set the current upgrade command to the preset tenth-stage upgrade command, 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.
[0110] Step 2-11-3: Clear the initial upgrade metadata in the preset location of the boot communication partition used to save the initial upgrade metadata, 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, delete the upgrade instruction information environment variable, and restart the device to enter the system after the normal upgrade.
[0111] In step 211, check if the current upgrade command is the preset eighth-stage upgrade command. If so, create a new preset upgrade command information environment variable and set its value to the preset eighth-stage upgrade command. Set the current upgrade command to the preset ninth-stage upgrade command and write it to the preset location in the boot communication partition used to save the upgrade command. Delete the upgrade command information environment variable.
[0112] In step 212, check if the current upgrade command is the preset ninth-stage upgrade command. If so, guide the device into upgrade mode firmware. Read the initial upgrade metadata from the preset location in the boot communication partition used to store the initial upgrade metadata, and read the second-stage upgrade metadata from the preset location in the boot communication partition used to store the second-stage upgrade metadata. Check the validity of the initial and second-stage upgrade metadata. If either the initial or second-stage upgrade metadata is invalid, exit the firmware upgrade process; otherwise, mount the external storage medium according to the file system type of the external storage medium in the initial upgrade metadata. Use the Android standard Android format package firmware upgrade engine to upgrade the firmware upgrade package path under the initial upgrade metadata. Skip the upgrades for the bootloader firmware and upgrade mode firmware during the upgrade process. Check if the user data incompatibility flag in the second-stage upgrade metadata is true. If yes, set the current upgrade command to the preset tenth-stage upgrade command and write the current upgrade command to the preset location in the boot communication partition used to store upgrade commands; otherwise, set the current upgrade command to the preset eleventh-stage upgrade command and write the current upgrade command to the preset location in the boot communication partition used to store upgrade commands.
[0113] Step 213, restoring the backed-up current user data partition, includes: checking if the current upgrade command is the preset tenth-stage upgrade command; if so, booting the device into upgrade mode firmware. Initial upgrade metadata is read from the preset location in the boot communication partition used to store initial upgrade metadata, and second-stage upgrade metadata is read from the preset location in the boot communication partition used to store second-stage upgrade metadata. The validity of the initial and second-stage upgrade metadata is checked. If at least one of the initial and second-stage upgrade metadata is invalid, the firmware upgrade process is exited; otherwise, the external storage medium is mounted according to the file system type of the external storage medium in the initial upgrade metadata. The user data incompatibility flag in the second-stage upgrade metadata is checked; if true, proceed to step 3-4-2; otherwise, proceed to step 3-4-3. Although the user data incompatibility flag check was performed before entering the tenth-stage upgrade command, in practice, power outages or other anomalies may still occur after entering the tenth-stage upgrade command. Therefore, a second check is performed before executing this step to prevent unexpected situations through redundant checks.
[0114] Step 3-4-2: Locate the image file information item corresponding to the user data incompatible file with the preset name from the image file information list of the second-stage upgrade metadata. Based on the image file path in the second-stage upgrade metadata, retrieve the user data incompatible file with the preset name from the external storage medium. Read the file system type of the upgraded user data partition from the user data incompatible file. Format the user data partition (e.g., userdata) according to the read file system type. After formatting, execute step 3-4-4.
[0115] Step 3-4-3: Set the current upgrade command to the preset eleventh stage upgrade command, and write the current upgrade command to the preset location in the boot communication partition used to save the upgrade command.
[0116] Step 3-4-4: Restore the data in the user data partition; set the current upgrade command to the preset eleventh-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 then execute the following steps 3-5. Specifically, this includes steps 3-4-3-1 to 3-4-3-4.
[0117] Step 3-4-3-1: Check if the encryption flag of the user data partition in the initial upgrade metadata is true. If it is not true, the user data partition is not encrypted. Proceed to step 3-4-3-2. Otherwise, the user data partition is encrypted. Proceed to step 3-4-3-3.
[0118] Step 3-4-3-2: Check if the backup type of the initial upgrade metadata is a full backup. If not, it means it is a file backup. Mount the user data partition, and then, in the root directory of the external storage medium, obtain the backup file compressed package with the preset name in the backup directory path of the initial upgrade metadata. Calculate the digital digest value of the backup file compressed package according to the digital digest algorithm specified by the digital digest algorithm type of the initial upgrade metadata, and compare the calculated digital digest value with the backup data digital digest value of the initial upgrade metadata. If they match, decompress the backup file compressed package to the mounted directory of the user data partition according to the preset decompression algorithm, and then execute the following steps 3-4-3-4; if it is a full backup, execute the following steps a to b.
[0119] Step a: Locate the image file information item corresponding to the user data partition image file in 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 in the second-stage upgrade metadata. Calculate the digital digest of the user data partition image file according to the digital digest algorithm type in 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 to step b.
[0120] 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, and 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 the 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.
[0121] Step 3-4-3-3: Format the partition corresponding to the key partition name in the initial upgrade metadata. Then execute step 3-4-3-4 as follows.
[0122] Step 3-4-3-4: Set the current upgrade command to the preset eleventh stage upgrade command, and write the current upgrade command to the preset location in the boot communication partition used to save upgrade commands.
[0123] In step 214, check if the current upgrade command is the preset eleventh-stage upgrade command. If so, guide the device into upgrade mode firmware. Read the initial upgrade metadata from the preset location in the boot communication partition used to store the initial upgrade metadata. Check if the backup flag of the initial upgrade metadata and the encryption flag of the user data partition are both true. If so, create a new upgrade completion flag file with a preset name in the preset path of the preset read-write partition.
[0124] 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.
[0125] 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.
[0126] 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 in the preset location used to store the initial upgrade metadata in the boot communication partition, and delete all files and directories in the backup directory path of the initial upgrade metadata.
[0127] Clear the second-stage upgrade metadata in the third preset location of the boot communication partition used to store the second-stage upgrade metadata, clear the current upgrade command in the first preset location of the boot communication partition used to store the current upgrade command, and the device restarts to enter the system after normal upgrade.
[0128] 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.
[0129] In summary, the firmware upgrade method and electronic device provided by this invention solve the problem of damage to the bootloader firmware and upgrade mode firmware due to unexpected events during the upgrade process, preventing the user from re-entering the upgrade mode firmware for further upgrades. It also solves the problem of potential mismatch issues between the upgrade mode firmware and the bootloader firmware, which could lead to unexpected events during the upgrade process and prevent the user from re-entering the upgrade mode firmware for upgrades. Finally, even if there is data incompatibility between the firmware versions before and after the upgrade, such as changing the file system type of the user data area (e.g., userdata) after the firmware upgrade, this solution can retain the data in the preset user data partition. Furthermore, this solution ensures reliable upgrades to the upgrade mode firmware itself without creating and using a backup partition; specifically, by upgrading the upgrade mode firmware within the bootloader firmware, ensuring a successful upgrade before entering the upgrade mode firmware to perform other operations, thus ensuring reliable upgrades to the upgrade mode firmware without creating and using a backup partition. Furthermore, this solution can still retain the data in the preset user data partition even when there is data incompatibility between the firmware versions before and after the upgrade. Specifically, by including a user data incompatibility file with a preset name in the upgrade package, performing a personalized backup of the data in the user data partition before the upgrade, and restoring the backed-up user data before the upgrade is completed, the solution can still retain the data in the preset user data partition even when there is data incompatibility between the firmware versions before and after the upgrade.
[0130] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A firmware upgrade method characterized by comprising: include: Obtain a firmware upgrade package, which includes a target bootloader firmware and a target upgrade mode firmware; When an instruction to upgrade the firmware upgrade package is detected, the current bootloader firmware is backed up and the current bootloader firmware is updated to the target bootloader firmware; as well as After the target bootloader firmware is successfully updated, the target upgrade mode firmware is installed, and the upgrade process of other firmware is completed according to the firmware upgrade package.
2. The method of claim 1, wherein, Obtaining the firmware upgrade package includes: Obtain a firmware upgrade package containing a user data incompatibility file with a preset name. The user data incompatibility file is stored in the firmware upgrade package when there is a target firmware that is incompatible with the current user data partition. The user data incompatibility file stores the first file system type of the target user data partition after the target firmware upgrade.
3. The method of claim 2, wherein, Also includes: When an instruction to upgrade the firmware upgrade package is detected, if the firmware upgrade package contains files that are incompatible with the user data, then the current user data partition is backed up. as well as After completing the upgrade process of other firmware according to the firmware upgrade package, restore the backed-up current user data partition.
4. The method of claim 3, wherein, The backup of the current user data partition includes: If the encryption flag of the current user data partition is not specified, then the second file system type of the current user data partition is obtained, and it is determined whether the first file system type is the same as the second file system type. If the encryption flag of the current user data partition is different or is yes, then the backup type in the initial upgrade metadata is set to file backup, and the data to be backed up is received. The data to be backed up is stored in the preset backup directory of the external storage medium, and the backup directory path in the initial upgrade metadata is set to the path of the preset backup directory relative to the root directory of the external storage medium; and If the backup configuration is the same and the received backup is a full backup, then the backup type in the initial upgrade metadata is set to full backup, and all data in the current user data partition is stored in the preset backup directory of the external storage medium. The backup directory path in the initial upgrade metadata is set to the path of the preset backup directory relative to the root directory of the external storage medium.
5. 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.
6. 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 in the boot communication partition and the terminal is restarted; Read the current upgrade instruction in the first preset location. If the current upgrade instruction is the first stage upgrade instruction, create a new upgrade instruction information environment variable in the environment variables and use the first stage upgrade instruction as the value of the upgrade instruction information environment variable. as well as Set the current upgrade command to the second-stage upgrade command, write the second-stage upgrade command into the first preset location, and then delete the upgrade command information environment variable.
7. The method according to claim 5 or 6, 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 a second-stage upgrade instruction, 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 to obtain the firmware upgrade package format and firmware image file from the first information; Create new second-stage upgrade metadata, and configure a preset image file path and an image file information list in the second-stage upgrade metadata. The image file information list includes multiple image file information items. Image files are read sequentially from the firmware upgrade package. For each target image file read, the target image file is stored in the preset image file path. An unused image file information item is obtained from the image file information list as the target file information item of the target image file. The target file information item is then saved in a target file information item file named after the file name of the target image file. The image file includes the image file corresponding to the target bootloader firmware and the image file corresponding to the target upgrade mode firmware; as well as Write the second-stage upgrade metadata into the third preset location of the boot communication partition, set the current upgrade instruction to the third-stage upgrade instruction, write the third-stage upgrade instruction into the first preset location, and restart the terminal.
8. The method according to claim 5 or 6, characterized in that, After reading the current upgrade instruction in the first preset location, the process also includes: The system checks the environment variables to see if an upgrade instruction information environment variable exists. If it does, the system retrieves the current upgrade instruction based on the value of the upgrade instruction information environment variable; otherwise, it retrieves the current upgrade instruction from the first preset location. Read the initial upgrade metadata or the second-stage upgrade metadata from the boot communication partition, verify the legality of the initial upgrade metadata or the second-stage upgrade metadata according to the current upgrade instruction, and execute the current upgrade instruction if both are verified.
9. The method according to claim 7, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is the third-stage upgrade instruction, create a new upgrade instruction information environment variable in the environment variables and use the third-stage upgrade instruction as the value of the upgrade instruction information environment variable. Obtain the first starting address and first partition size of the currently booting firmware partition as described in the current partition table; Create a new environment variable named "First Starting Address" and an environment variable named "First Partition Size" in the environment variables. Set the value of the environment variable named "First Starting Address" to the first starting address and set the value of the environment variable named "First Partition Size" to the first partition size. Find the backup partition in the current partition table whose partition size is greater than or equal to the first partition size, read data with a size equal to the first partition size starting from the first starting address, and write the data to the second starting address of the backup partition; Modify the first starting address of the bootloader firmware partition in the current partition table to the second starting address; as well as Set the current upgrade command to the fourth stage upgrade command, write the fourth stage upgrade command to the first preset location, and delete the upgrade command information environment variable.
10. The method according to claim 9, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is the fourth stage upgrade instruction, create a new upgrade instruction information environment variable in the environment variables and use the fourth stage upgrade instruction as the value of the upgrade instruction information environment variable. Read the first starting address environment variable and the first partition size environment variable, and write the first image file corresponding to the target bootloader firmware into the first starting address stored in the first starting address environment variable; as well as Modify the second starting address in the current partition table to the first starting address; Set the current upgrade command to the fifth stage upgrade command, write the fifth stage upgrade command to the first preset location, and delete the upgrade command information environment variable.
11. The method according to claim 10, 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, create a new upgrade instruction information environment variable in the environment variables and use the fifth-stage upgrade instruction as the value of the upgrade instruction information environment variable. as well as Delete the environment variable for the first starting address and the environment variable for the first partition size; Set the current upgrade command to the sixth stage upgrade command, write the sixth stage upgrade command to the first preset location, and delete the upgrade command information environment variable.
12. The method according to claim 7, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is a sixth-stage upgrade instruction, create a new upgrade instruction information environment variable in the environment variables and use the sixth-stage upgrade instruction as the value of the upgrade instruction information environment variable. Read the second stage upgrade meta information from the third preset position, obtain the firmware upgrade package format in the second stage upgrade meta information, and if the firmware upgrade package format is Android format, search for the second image file information item corresponding to the target upgrade mode firmware from the image file information list of the second stage upgrade meta information. Read the fourth starting address and the size of the fourth partition corresponding to the upgrade mode firmware partition from the current partition table; The second image file corresponding to the target upgrade mode firmware is obtained according to the image file path and the second image file information item in the second stage upgrade meta information, and the second image file is written to the fourth starting address; as well as Set the current upgrade command to the eighth stage upgrade command, write the eighth stage upgrade command to the first preset location, and delete the upgrade command information environment variable.
13. The method according to claim 12, characterized in that, Also includes: Read the current upgrade instruction from the first preset location. If the current upgrade instruction is an eighth-stage upgrade instruction, create a new upgrade instruction information environment variable and set the eighth-stage upgrade instruction as the value of the upgrade instruction information environment variable; and Set the current upgrade command to the ninth stage upgrade command, write the ninth stage upgrade command to the first preset location, and delete the upgrade command information environment variable.
14. The method according to claim 12, characterized in that, After obtaining the firmware upgrade package format from the second-stage upgrade metadata, the process also includes: If the firmware upgrade package format is not Android format, then search for the third image file information item other than the first image file information item from the image file information list of the second stage upgrade metadata; Read the fifth starting address and the fifth partition size corresponding to each third image file information item from the partition table; At least one third image file is obtained based on the image file path in the second-stage upgrade metadata and the third image file information item. Write the third image file to the fifth starting address corresponding to the third image file; and Once all the third image files have been successfully written, the current upgrade instruction is set to the seventh-stage upgrade instruction, the seventh-stage upgrade instruction is written to the first preset location, and the upgrade instruction information environment variable is deleted.
15. The method according to claim 14, characterized in that, After creating the new second-stage upgrade metadata, it also includes: In the second-stage upgrade metadata, a user data incompatibility flag is set. If the firmware upgrade package contains a target firmware that is incompatible with the user data partition, the user data incompatibility flag is set to true. Determine whether the backup flag of the initial upgrade metadata is true; if so, determine whether the backup type of the initial upgrade metadata is a full backup. If it is a full backup, a user data partition image file with the partition name of the current user data partition as the file name is created under the preset image file path; the block device node of the current user data partition is opened, and the data in the block device node is saved to the user data partition image file; Obtain an unused image file information item from the image file information list as the fourth file information item of the user data partition image file, and save the fourth file information item in a fourth file information item file named after the file name of the user data partition image file.
16. The method according to claim 15, 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, create a new upgrade instruction information environment variable in the environment variables and use the seventh-stage upgrade instruction as the value of the upgrade instruction information environment variable. Read the user incompatibility flag in the second-stage upgrade metadata. If it is true, set the current upgrade instruction to the tenth-stage upgrade instruction, write the tenth-stage upgrade instruction to the first preset position, and delete the upgrade instruction information environment variable. as well as If not true, then the first preset position, the second preset position, and the third preset position of the boot communication partition are cleared, and the upgrade instruction information environment variable is deleted to complete the upgrade process.
17. The method according to claim 13, characterized in that, Also includes: Read the current upgrade instruction in the first preset position; if the current upgrade instruction is the ninth stage upgrade instruction, then guide the terminal to enter the target upgrade mode firmware. The firmware upgrade package under the firmware upgrade package path of the initial upgrade metadata is upgraded using the Android standard Android format package firmware upgrade engine through the target upgrade mode firmware, and the upgrade of the target bootloader firmware and the target upgrade mode firmware is skipped during the upgrade process. as well as Verify whether the user data incompatibility flag in the second-stage upgrade metadata is true. If so, set the current upgrade instruction to the tenth-stage upgrade instruction and write the tenth-stage upgrade instruction into the first preset position; otherwise, set the current upgrade instruction to the eleventh-stage upgrade instruction and write the eleventh-stage upgrade instruction into the first preset position.
18. The method according to claim 14, characterized in that, Also includes: Read the current upgrade instruction in the first preset position; if the current upgrade instruction is the tenth stage upgrade instruction, guide the terminal to enter the target upgrade mode firmware. The target upgrade mode firmware searches the image file information list of the second stage upgrade metadata for the fourth file information item corresponding to the user data partition image file and the user data incompatible file information item. Based on the image file path in the second-stage upgrade metadata and the user data incompatible file information item, obtain the user data incompatible file, and read the first file system type from the user data incompatible file; The user data partition image file is obtained based on the image file path and the fourth file information item. The user data partition is formatted according to the first file system type. After formatting, the user data partition image file is restored to the user data partition. as well as Set the current upgrade instruction to the eleventh stage upgrade instruction, and write the eleventh stage upgrade instruction into the first preset location.
19. The method according to claim 18, characterized in that, Also includes: Read the current upgrade instruction in the first preset position. If the current upgrade instruction is the eleventh stage upgrade instruction, then guide the terminal to enter the target upgrade mode firmware. The firmware upgrade package under the firmware upgrade package path of the initial upgrade metadata is deleted through the target upgrade mode firmware, and all image files under the image file path of the second stage upgrade metadata are deleted; as well as After clearing the first and third preset locations of the boot communication partition, the terminal is restarted to complete the firmware upgrade package upgrade process.
20. 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 19.