Firmware upgrade method and electronic device
By backing up and updating the recovery mode firmware in the Android system, the problem of firmware upgrade failure caused by corrupted recovery mode firmware is solved, and self-repair and normal entry into recovery mode are achieved in the event of damage.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- FUZHOU ROCKCHIP SEMICON
- Filing Date
- 2026-04-24
- Publication Date
- 2026-07-31
AI Technical Summary
In Android systems, when the recovery mode firmware is corrupted, the device cannot enter recovery mode normally, resulting in firmware upgrade failure and rendering the device unusable.
The system backs up the current recovery mode firmware to the preset storage area on the first boot after firmware flashing, and updates the backup recovery mode firmware after each normal firmware upgrade. If the system cannot enter recovery mode normally, the system repairs the damaged current recovery mode firmware using the backup recovery mode firmware in normal mode.
In the event of firmware corruption in recovery mode, the device can automatically recover and enter recovery mode normally, ensuring the firmware upgrade process proceeds smoothly and preventing the device from becoming bricked.
Smart Images

Figure CN122489104A_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 Android systems that upgrade based on Recovery mode, when a firmware upgrade is needed, the device reboots, and then the recovery mode firmware is booted from the bootloader firmware, putting the device into recovery mode. All other firmware except the recovery mode firmware is then upgraded within recovery mode. After the firmware upgrade is complete, the device reboots into the upgraded system. In the upgraded Android system, the preset recovery mode firmware upgrade service (install-recovery.sh) performs the upgrade itself. The preset recovery mode firmware upgrade service (install-recovery.sh) runs after the device boots into the Android system. It first checks if the current recovery mode firmware is already the upgraded recovery mode firmware. If so, it writes a preset "upgrade successful" string to the system log; otherwise, it upgrades the recovery mode firmware according to the preset process. If the upgrade is successful, a preset "upgrade successful" string is written to the system log; otherwise, a preset "upgrade failed" string is written to the system log.
[0003] After a device leaves the factory, physical aging of the boot storage medium (such as eMMC), quality issues with the storage chip, or other unforeseen circumstances may cause corruption of the recovery mode firmware in the boot storage medium. Once the recovery mode firmware is corrupted, when attempting to upgrade the device's firmware, a firmware crash will occur during the boot process into recovery mode, preventing the firmware upgrade from being performed and the device from exiting recovery mode, thus rendering the device unusable. Summary of the Invention
[0004] This invention provides a firmware upgrade method and an electronic device that enables the firmware upgrade process to proceed normally when the firmware in recovery mode is corrupted.
[0005] In one aspect of the present invention, a firmware upgrade method is provided. The method includes: upon the first power-on after firmware flashing, entering recovery mode to back up the currently functional recovery mode firmware to a preset storage area as backup recovery mode firmware; after each normal firmware upgrade, updating the backup recovery mode firmware in the preset storage area according to the current recovery mode firmware; and when unable to enter recovery mode normally, starting normal mode and repairing the damaged current recovery mode firmware in normal mode using the backup recovery mode firmware in the preset storage area.
[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 aforementioned firmware upgrade method.
[0007] According to the technical solution of the present invention, the current recovery mode firmware is backed up when the device is powered on for the first time after the firmware is burned. After each normal firmware upgrade, the backup recovery mode firmware in the preset storage area is updated according to the current recovery mode firmware obtained after the upgrade. In this way, the latest backup of the current recovery mode firmware is always present in the preset storage area, so that it can serve as the recovery basis for restoring the current recovery mode firmware when it is damaged. When it is necessary to enter the recovery mode but cannot enter it normally, the normal mode is started first, and the current recovery mode firmware is repaired by the backup recovery mode firmware in the normal mode. Thus, even if the current recovery mode firmware is damaged, it can recover itself and enter the recovery mode normally. 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 A flowchart illustrating the application of the firmware upgrade method described above in a real-world scenario according to an embodiment of the present invention; 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, devices that undergo firmware upgrades by entering recovery mode have the problem of being unable to enter recovery mode normally after the recovery mode firmware is corrupted, leading to firmware crashes. Furthermore, the device is also unable to exit recovery mode firmware normally, rendering the device unusable.
[0011] To address at least the aforementioned technical problems, this disclosure provides a firmware upgrade method. According to this disclosure, a recovery mode firmware backup is performed on the device upon its first power-on after firmware flashing and after each normal firmware upgrade. If the device cannot enter recovery mode normally, it is guided into normal mode, where the recovery mode firmware is repaired using the backup. In this way, according to embodiments of this disclosure, in the event of a failure in the recovery mode firmware, the current recovery mode firmware can be repaired using the backup recovery mode firmware, enabling the device to enter recovery mode normally.
[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, upon the first power-on after firmware burning, the system enters recovery mode to back up the currently working recovery mode firmware to a preset storage area as backup recovery mode firmware.
[0015] In some embodiments, backing up the currently functioning recovery mode firmware to a preset storage area as a backup recovery mode firmware includes: if a factory reset instruction is read from the boot communication partition, deleting the files in the unencrypted data partition under a first preset path, and creating an empty recovery mode firmware image file under the first preset path; obtaining the partition size of the current recovery mode firmware partition, reading the partition size data starting from the beginning position of the block device node file corresponding to the current recovery mode firmware, and writing the read data into the recovery mode firmware image file as a first backup recovery mode firmware; constructing backup metadata including a first backup path and a second backup path, setting the first backup path to the first preset path, and setting the second backup path to a second preset path in the encrypted data partition; and clearing the factory reset instruction, exiting recovery mode, and restarting the device.
[0016] In this way, during the backup of the current recovery mode firmware, a first backup path is created in the unencrypted data partition, and a second backup path is created in the encrypted data partition. The first backup path in the unencrypted data partition facilitates direct access in normal mode for restoring corrupted recovery mode firmware. The second backup path in the encrypted data partition further enhances the security of the backup data. Because the encrypted data partition requires access permissions, it restricts the objects that can be accessed. It also provides a reliable data source for the corrupted current recovery mode firmware to complete the recovery process should the first backup recovery mode firmware in the unencrypted data partition be tampered with. Furthermore, recovery mode can only access the unencrypted data partition; the encrypted data partition can only be decrypted within the Android system (normal mode). Recovery mode cannot decrypt the encrypted data partition (a security design to prevent data leakage and corruption), therefore, it cannot directly access the encrypted data partition.
[0017] In step 104, after each normal firmware upgrade is completed, the backup recovery mode firmware in the preset storage area is updated according to the current recovery mode firmware.
[0018] In step 106, when it is impossible to enter the recovery mode normally, the normal mode is started, and the damaged current recovery mode firmware is repaired in the normal mode by the backup recovery mode firmware in the preset storage area.
[0019] In some embodiments, the method 100 may further include: if a recovery signal requiring entry into recovery mode is received, then the number of recovery mode boots is obtained, and it is determined whether the number of recovery mode boots is greater than the maximum number of boots. If so, it indicates that the recovery mode cannot be entered normally, the recovery signal is cleared and the device is restarted; otherwise, the number of recovery mode boots is increased by 1 and the device is entered into recovery mode; after entering recovery mode, the number of recovery mode boots is set to 0.
[0020] In this way, when entering recovery mode, the boot count is first checked. If the boot count exceeds the maximum, the recovery mode firmware is corrupted and cannot enter recovery mode normally. In this case, the recovery signal to boot the device into recovery mode is cleared and the device is restarted. The device will then enter normal mode instead of recovery mode, thus completing the repair of the recovery mode firmware. If the boot count has not exceeded the maximum, there may be other reasons for failing to enter recovery mode. In this case, the boot count is increased by 1, and the device is tried again. If the device successfully enters recovery mode, the boot count is reset to 0, indicating that the recovery mode firmware is usable.
[0021] In this embodiment, the recovery signal includes upgrade command, factory reset command, factory reset command, recovery mode check and backup command, and other commands that put the device into recovery mode.
[0022] In some embodiments, the method 100 may further include: in normal mode, obtaining the number of recovery mode boot attempts, determining whether the number of recovery mode boot attempts is greater than the maximum number of boot attempts; if so, it indicates that normal entry into recovery mode is not possible; after writing a recovery mode firmware corruption flag into the command line parameters of normal mode, the system firmware is booted into the system by the startup firmware. Repairing the corrupted current recovery mode firmware in normal mode using the backup recovery mode firmware in the preset storage area includes: if a recovery mode firmware corruption flag exists in the command line parameters, then repairing the current recovery mode firmware using the backup recovery mode firmware in the preset storage area.
[0023] In this way, if the number of boot attempts in recovery mode is greater than the maximum number of boot attempts in normal mode, a recovery mode firmware corruption flag is written directly into the command line parameters in normal mode, and the system is entered. After detecting the recovery mode firmware corruption flag, the system will repair the current recovery mode firmware by backing up the recovery mode firmware, thus completing the repair of the current recovery mode firmware and being able to enter recovery mode normally after receiving the recovery signal.
[0024] In some embodiments, the method 100 may further include: if upgrade metadata is read from the boot communication partition, backing up the current recovery mode firmware to a first temporary path as the pre-upgrade recovery mode firmware; obtaining the firmware upgrade package under the upgrade package path in the upgrade metadata, and completing the firmware upgrade process according to the firmware upgrade package; after the firmware upgrade package is completed, updating the backup recovery mode firmware in the preset storage area according to the current recovery mode firmware includes: after the upgrade is completed, backing up the current recovery mode firmware to a second temporary path as the post-upgrade recovery mode firmware; comparing whether the pre-upgrade recovery mode firmware is consistent with the post-upgrade recovery mode firmware, if so, creating an upgrade completion marker file and exiting recovery mode and restarting the device; otherwise, creating a check and backup instruction, and exiting recovery mode and restarting the device after creating the upgrade completion marker file; if the check and backup instruction is read from the boot communication partition, deleting the first backup recovery mode firmware in the first backup path in the backup metadata, and backing up the current recovery mode firmware to the first backup path as the new first backup recovery mode firmware; and writing the check and backup completion marker, clearing the check and backup instruction and restarting the device.
[0025] In this way, when upgrade metadata is read, indicating a firmware upgrade is needed, the current recovery mode firmware is backed up to the firmware before the upgrade. After the firmware upgrade process is complete, the upgraded current recovery mode firmware is backed up to the firmware after the upgrade. By comparing the firmware before and after the upgrade, it can be determined whether the recovery mode firmware has changed. If so, a check and backup command and an upgrade completion marker file are created, and the recovery mode is exited; otherwise, an upgrade completion marker file is created directly, and the recovery mode is exited. Upon detecting a check and backup command, the first backup recovery mode firmware under the first backup path is updated according to the current recovery mode firmware. That is, during the firmware upgrade process, it automatically monitors whether a recovery mode firmware update has occurred. If so, the backed-up first backup recovery mode firmware is updated to the updated current recovery mode firmware, ensuring that the backed-up recovery mode firmware is up-to-date.
[0026] In some embodiments, the method 100 may further include: if upgrade metadata is read from the boot communication partition, obtaining the upgrade package public key and verifying the signature data of the firmware upgrade package. If the verification fails, an upgrade package error is indicated, and the upgrade metadata is cleared before exiting recovery mode and restarting the device; and if the verification succeeds, the current recovery mode firmware is backed up to the first temporary path as the recovery mode firmware before the upgrade.
[0027] In this way, the signature data of the obtained firmware upgrade package is verified before firmware upgrade. If the verification fails, it means that the sender of the firmware upgrade package has not passed security verification or that the firmware upgrade package may have been tampered with during transmission. The system will directly indicate that the upgrade package is abnormal and clear the upgrade metadata, thereby ensuring the security of the firmware upgrade package used during the firmware upgrade process.
[0028] In some embodiments, the method 100 may further include: if a factory reset command is received, checking whether a first backup recovery mode firmware exists under the first backup path; if so, storing the first backup recovery mode firmware in memory, executing the factory reset command, and restoring the first backup recovery mode firmware in memory to the first backup path; and after creating a factory reset completion marker file, clearing the factory reset command, exiting the recovery mode, and restarting the device.
[0029] In this way, the first backup recovery mode firmware under the first backup path is stored in memory before the factory reset command is executed. This ensures that the first backup recovery mode firmware in memory is not deleted during the factory reset process, and can be restored to the first backup path after the factory reset command is completed. This avoids the situation where the backup recovery mode firmware is deleted during the factory reset process, resulting in the inability to recover the corrupted recovery mode firmware.
[0030] In some embodiments, the method 100 may further include: in normal mode, if a check and backup completion marker is detected, confirming whether a first backup recovery mode firmware exists under the first backup path; if so, verifying the first backup recovery mode firmware; if the verification is successful, updating the second backup recovery mode firmware under the second backup path according to the first backup recovery mode firmware, and clearing the check and backup completion marker.
[0031] In this way, in normal mode, if a check and backup completion marker is detected, it indicates that the first backup recovery mode firmware under the first backup path has been updated accordingly after the firmware upgrade. In normal mode, the encrypted data partition can be accessed normally, and the second backup recovery mode firmware under the second backup path can be updated according to the first backup recovery mode firmware, realizing dual backup of the encrypted and unencrypted data partitions and ensuring the security of the backup recovery mode firmware.
[0032] In some embodiments, the method 100 may further include: in normal mode, if a factory reset completion marker file is detected, confirming whether a first backup recovery mode firmware exists under the first backup path; if so, verifying the first backup recovery mode firmware; if the verification is successful, updating the second backup recovery mode firmware under the second backup path according to the first backup recovery mode firmware, and clearing the factory reset completion marker file.
[0033] In this way, if a factory reset completion marker file is detected in normal mode, it indicates that the factory reset command has been successfully executed. Because the factory reset command formats both unencrypted and encrypted data partitions, the first backup recovery mode firmware from the first backup path needs to be copied to the second backup path to complete the full backup. The backup recovery mode firmware in the first and second backup paths can also mutually vouch for each other to ensure the security of the recovery mode firmware backup.
[0034] In some embodiments, the method 100 may further include: in normal mode, if an upgrade completion marker file is detected, confirming whether a preset upgrade success string exists in the system log; if so, creating a recovery mode check and backup instruction and deleting the upgrade completion marker file; otherwise, confirming whether a preset upgrade failure string exists in the system log; if not, increasing the detection count by 1; if the detection count is greater than a detection count threshold, deleting the upgrade completion marker file and updating the first backup recovery mode firmware under the first backup path and the second backup recovery mode firmware under the second backup path; and if the detection count is less than the detection count threshold, returning to the step of confirming whether a preset upgrade success string exists in the system log.
[0035] If only the upgrade completion marker file is detected, but the check and backup completion marker is not detected, it indicates that the recovery mode firmware was not upgraded during the firmware upgrade process in recovery mode, and a recovery mode firmware upgrade is still required. Therefore, it is necessary to further check whether the upgrade success string exists in the system log. If so, it means that the upgrade process has been completed. A recovery mode check and backup command is created, and the upgrade completion marker file is deleted. This allows the system to re-enter recovery mode via the recovery mode check and backup command to complete the backup process of the upgraded recovery mode firmware. If the upgrade success string does not exist, it is then checked whether the upgrade failure string exists. If it does not exist, the check count is incremented by 1, and the system log is returned to check whether the upgrade success string exists. This process continues until the check count exceeds the threshold. Then, the upgrade completion marker file is deleted, and the first backup recovery mode firmware and the second backup recovery mode firmware are updated. This is because any firmware update will update the recovery mode firmware.
[0036] There are two possible timings for firmware updates in recovery mode: 1. By default, the Android system does not upgrade the recovery mode firmware in recovery mode. Instead, it upgrades the recovery mode firmware itself via a pre-defined recovery mode firmware upgrade service (install-recovery.sh) after the system boots into the Android system following the upgrade. Therefore, in recovery mode, the recovery mode firmware before the upgrade is consistent with the recovery mode firmware after the upgrade. 2. In some cases, if the upgrade process is changed, it may be possible to directly upgrade all firmware, including the recovery mode firmware, in recovery mode at once. In this case, the recovery mode firmware before the upgrade is inconsistent with the recovery mode firmware after the upgrade.
[0037] In some embodiments, updating the first backup recovery mode firmware under the first backup path and the second backup recovery mode firmware under the second backup path includes: creating a first backup existence flag and a second backup existence flag, and setting their initial values to false; checking whether the first backup recovery mode firmware exists under the first backup path, and if so, setting the first backup existence flag to true, otherwise setting the first backup existence flag to false; checking whether the second backup recovery mode firmware exists under the second backup path, and if so, setting the second backup existence flag to true, otherwise setting the second backup existence flag to false; if both the first backup existence flag and the second backup existence flag are false, writing a factory reset command and outputting a recovery mode firmware error; if the first backup existence flag is true and the second backup existence flag is false, obtaining the first backup recovery mode firmware from the first backup path and storing it in the second backup path as the second backup recovery mode firmware; and if the first backup existence flag is false and the second backup existence flag is true, obtaining the second backup recovery mode firmware from the second backup path and storing it in the first backup path as the first backup recovery mode firmware.
[0038] In this way, after the operation in recovery mode ends, the corresponding updates of the first backup recovery mode firmware under the first backup path and the second backup recovery mode firmware under the second backup path are performed based on the operation results left in recovery mode (upgrade completion marker file, check and backup completion marker, factory reset completion marker, etc.). After the update is completed, the existence of the two is checked so that the backup recovery mode firmware can be backed up normally under both the first backup path and the second backup path.
[0039] In some embodiments, repairing the corrupted current recovery mode firmware using the backup recovery mode firmware in the preset storage area in normal mode includes: if a new firmware upgrade request exists, checking if there is a recovery mode firmware corruption flag in the command line parameters; if so, verifying the first backup recovery mode firmware under the first backup path; if the verification passes, upgrading the current recovery mode firmware according to the first backup recovery mode firmware and updating the recovery mode exit metadata; if the verification fails, verifying the second backup recovery mode firmware under the second backup path; if the verification of the second backup recovery mode firmware passes, upgrading the current recovery mode firmware according to the second backup recovery mode firmware and updating the recovery mode exit metadata; if the verification of the second backup recovery mode firmware fails, indicating that the recovery mode firmware is corrupted and cannot be repaired, and exiting the firmware upgrade.
[0040] In this way, before entering recovery mode with a new firmware upgrade request, the system checks for a corrupted recovery mode firmware flag. If such a flag is found, the current recovery mode firmware is repaired using the backup recovery mode firmware from the first or second backup path before entering recovery mode. This ensures that the system can enter recovery mode normally during the firmware upgrade request process. Furthermore, before restoring the current recovery mode firmware, the backup recovery mode firmware is signed and verified. If the verification fails, the backup recovery mode firmware will not be used to back up the current recovery mode firmware, thus ensuring system security.
[0041] The following will describe, through examples, the application scenarios of the firmware upgrade method and electronic device according to embodiments of the present invention.
[0042] Figure 2 This is a flowchart illustrating the application of the above-described firmware upgrade method to a real-world scenario according to an embodiment of the present invention, including steps 201 to 207.
[0043] Step 201: Before the device leaves the factory, pre-written factory instructions are written into the device's boot communication partition. A binary image file with the same size as the boot communication partition and completely empty content is constructed as the boot communication partition image file. Then, the pre-written factory instructions are written into the pre-written location in the boot communication partition image file used to store the factory instructions. During the firmware flashing process before the device leaves the factory, the boot communication partition image file is written into the boot communication partition in the boot storage medium (such as eMMC).
[0044] Step 202: Establish security protection for the firmware upgrade package. During the compilation of the recovery mode firmware and the Android system firmware (e.g., super.img), a preset upgrade package public key is saved to a preset path in the recovery mode firmware, and a preset upgrade package public key is saved to a preset path in the Android system firmware. This allows the firmware upgrade package to be verified using the upgrade package public key in both recovery mode and normal mode after booting. The Android firmware upgrade package is compiled according to the Android system's preset process, and signed using a preset upgrade package private key to generate the final standard Android firmware upgrade package (e.g., update.zip). The generated standard Android firmware upgrade package is placed on a preset firmware upgrade server for firmware upgrades on corresponding devices.
[0045] Step 203: Start the device and run the bootloader firmware. Check if it is necessary to enter recovery mode. If so, verify whether the current recovery mode firmware is corrupted. If corrupted, exit and enter recovery mode; if not corrupted, enter recovery mode normally. This includes the following steps (1) to (9) executed by the bootloader firmware.
[0046] (1) Start the bootloader firmware, read the upgrade metadata from the preset location in the boot communication partition used to store upgrade metadata, and check whether the upgrade command in the upgrade metadata is the preset upgrade command. If yes, execute the following step (2); otherwise, execute the following step (3).
[0047] Step (2) includes steps (2-1) to (2-3).
[0048] Step (2-1): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata, and check whether the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata. If not, proceed to step (2-2); if yes, proceed to step (2-3).
[0049] Step (2-2): Initialize the recovery mode exit metadata. This involves setting the magic number of the recovery mode exit metadata to the preset magic number and setting the recovery mode boot count to 0. Then execute the following step (2-3).
[0050] Step (2-3): Check if the number of recovery mode boots in the recovery mode exit metadata is greater than the preset maximum number of recovery mode boots (e.g., 6). If not, increment the number of recovery mode boots in the recovery mode exit metadata by 1, and write the updated recovery mode exit metadata to the preset location in the boot communication partition used to save the recovery mode exit metadata, and then boot the device into recovery mode; otherwise, clear the upgrade command in the upgrade metadata, and write the updated upgrade metadata to the preset location in the boot communication partition used to save the upgrade metadata, and then restart the device.
[0051] Step (3): Start the boot program firmware, read the factory reset information from the preset location in the boot communication partition used to save the factory reset information, check whether the factory reset information is the preset factory reset instruction, if so, execute the following step (4), otherwise execute the following step (5).
[0052] Step (4): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata, check whether the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if not, initialize the recovery mode exit metadata, that is, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0053] Check if the number of recovery mode boot attempts in the recovery mode exit metadata is greater than the preset maximum number of recovery mode boot attempts. If not, increment the number of recovery mode boot attempts in the recovery mode exit metadata by 1, and write the updated recovery mode exit metadata to the preset location in the boot communication partition used to save the recovery mode exit metadata. Then, boot the device into recovery mode. Otherwise, clear the factory reset command in the preset location in the boot communication partition used to save the factory reset information, and then restart the device.
[0054] Step (5): Read the factory instructions from the preset location in the boot communication partition where the factory instructions are stored, and check whether the read factory instructions are the preset factory instructions. If they are, proceed to step (6); otherwise, proceed to step (7).
[0055] Step (6): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata, check whether the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if not, initialize the recovery mode exit metadata, that is, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0056] Check if the number of recovery mode boots in the recovery mode exit metadata is greater than the preset maximum number of recovery mode boots. If not, increment the number of recovery mode boots in the recovery mode exit metadata by 1, and write the updated recovery mode exit metadata to the preset location in the boot communication partition used to save the recovery mode exit metadata, and then boot the device into recovery mode; otherwise, clear the factory instructions in the preset location in the boot communication partition used to save the factory instructions, and then restart the device.
[0057] Step (7): Read the recovery mode check and backup instructions from the preset location in the boot communication partition where the recovery mode check and backup instructions are stored. Check whether the read recovery mode check and backup instructions are the preset recovery mode check and backup instructions. If so, execute the following step (8); otherwise, execute the following step (9).
[0058] Step (8): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata, check whether the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if not, initialize the recovery mode exit metadata, that is, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0059] Check if the number of recovery mode boot attempts in the recovery mode exit metadata is greater than the preset maximum number of recovery mode boot attempts. If not, increment the number of recovery mode boot attempts in the recovery mode exit metadata by 1, and write the updated recovery mode exit metadata to the preset location in the boot communication partition used to save recovery mode exit metadata. Then, boot the device into recovery mode. Otherwise, clear the recovery mode check and backup instructions in the preset location in the boot communication partition used to save recovery mode check and backup instructions, and then restart the device.
[0060] Step (9): Determine whether the device is in normal mode or recovery mode according to the preset process, including the following steps (9-1) to (9-2).
[0061] Step (9-1): If it is recovery mode, read the recovery mode exit metadata from the preset location in the boot communication partition used to store recovery mode exit metadata. Check if the magic number of the recovery mode exit metadata is the preset magic number. If not, initialize the recovery mode exit metadata, that is, set the magic number of the recovery mode exit metadata to the preset magic number, and set the recovery mode boot count of the recovery mode exit metadata to 0. In addition to upgrade commands, factory reset commands, factory reset commands, and recovery mode check and backup commands, there may be other preset conditions for entering recovery mode that need to be processed to cover all situations that require entering recovery mode.
[0062] Check if the number of recovery mode boot attempts in the recovery mode exit metadata is greater than the preset maximum number of recovery mode boot attempts. If not, increment the number of recovery mode boot attempts in the recovery mode exit metadata by 1, and write the updated recovery mode exit metadata to the preset location in the boot communication partition used to store the recovery mode exit metadata. Then, boot the device into recovery mode. Otherwise, clear the preset conditions that enable the device to enter recovery mode so that the device can boot in normal mode the next time it starts up, and then restart the device.
[0063] Step (9-2): If it is normal mode, read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata, check whether the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if not, initialize the recovery mode exit metadata, that is, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0064] Check if the number of recovery mode boot attempts in the recovery mode exit metadata is greater than the preset maximum number of recovery mode boot attempts. If so, write the preset recovery mode firmware corruption flag (such as recovery_corrupt) into the command line parameters of normal mode, and then boot the device's boot firmware, which in turn boots the Android system firmware to enter the Android system; otherwise, boot the device's boot firmware, which in turn boots the Android system firmware to enter the Android system.
[0065] Step 204: The device enters recovery mode, where the corresponding instructions are executed and the recovery mode firmware is updated. This includes the following steps (1) to (6).
[0066] Step (1): Update the recovery mode exit metadata. Read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata. Check if the magic number of the recovery mode exit metadata is the preset magic number. If it is, set the recovery mode boot count of the recovery mode exit metadata to 0; otherwise, initialize the recovery mode exit metadata, set the magic number of the recovery mode exit metadata to the preset magic number, and set the recovery mode boot count of the recovery mode exit metadata to 0. Write the updated recovery mode exit metadata to the preset location in the boot communication partition used to store the recovery mode exit metadata.
[0067] Step (2): Read the factory command from the preset location in the boot communication partition where the factory command is stored, and check whether the read factory command is the preset factory command. If not, proceed to step (3); otherwise, proceed to steps (2-1) to (2-3). That is, when the device is powered on for the first time after the firmware is burned, the factory command will be read from the preset location.
[0068] Step (2-1): Back up the current recovery mode firmware as a recovery mode firmware image file to the preset recovery mode firmware backup path (e.g., / cache / recovery / recovery.img) of the preset unencrypted data partition (e.g., cache), as the first backup recovery mode firmware.
[0069] Step (2-1) includes steps (a) through (c).
[0070] Step (a): Delete the recovery mode firmware image file under the preset recovery mode firmware backup path of the preset unencrypted data partition, and at the same time create an empty recovery mode firmware image file (such as recovery.img) under the preset recovery mode firmware backup path of the preset unencrypted data partition.
[0071] Step (b): Obtain the size of the recovery mode firmware partition based on the block device node file (e.g., / dev / block / by-name / recovery) of the recovery mode firmware. Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read the size of the recovery mode firmware partition and write the read data to the recovery mode firmware image file (e.g., / cache / recovery / recovery.img) located in the preset unencrypted data partition's preset recovery mode firmware backup path. The size of the written data is the size of the recovery mode firmware partition.
[0072] Step (c): Starting from the beginning of the recovery mode firmware image file under the preset recovery mode firmware backup path in the preset unencrypted data partition, read back the data of the recovery mode firmware partition size, and compare the read data with the corresponding data read from the block device node file of the recovery mode firmware in binary. If they are completely consistent, it means that the current recovery mode firmware has been successfully backed up to the recovery mode firmware image file under the preset recovery mode firmware backup path in the preset unencrypted data partition, and then execute the following step (2-2); otherwise, rewrite the block device node file corresponding to the recovery mode firmware into the recovery mode firmware image file under the preset recovery mode firmware backup path in the preset unencrypted data partition until the data of the two are completely consistent, and then execute the following step (2-2).
[0073] Step (2-2): Construct recovery mode firmware backup metadata consisting of a magic number, the first backup path of the recovery mode firmware, the second backup path of the recovery mode firmware, the digital digest algorithm type, and the digital digest of the recovery mode firmware. Set the magic number of the recovery mode firmware backup metadata to a preset magic number. Set the first backup path of the recovery mode firmware backup metadata to a preset backup path of the unencrypted data partition (e.g., / cache / recovery / recovery.img). Set the second backup path of the recovery mode firmware backup metadata to a preset backup path of the encrypted data partition (e.g., / data / recovery_bak / recovery.img). Set the digital digest algorithm type of the recovery mode firmware backup metadata to a preset digital digest algorithm. Calculate the digital digest of the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata according to the digital digest algorithm type of the recovery mode firmware backup metadata, and save the calculated digital digest to the digital digest of the recovery mode firmware backup metadata.
[0074] The completed recovery mode firmware backup metadata is written to a preset location in the boot communication partition used to store the recovery mode firmware backup metadata.
[0075] Step (2-3): Clear the factory instructions in the preset location used to save factory instructions in the boot communication partition, then exit recovery mode and restart the device.
[0076] Step (3): Read the upgrade metadata from the preset location in the boot communication partition used to store upgrade metadata, and check whether the upgrade instruction in the upgrade metadata is the preset upgrade instruction. If not, execute step (4); otherwise, execute steps (3-1) to (3-2).
[0077] Step (3-1): Verify the standard Android firmware upgrade package under the upgrade package path of the upgrade metadata using the corresponding upgrade package public key in the recovery mode firmware according to the preset process. If the verification fails, an upgrade package error is indicated, causing the upgrade to fail. Then, the upgrade metadata in the preset location used to store the upgrade metadata in the boot communication partition is cleared, and then the recovery mode is exited and the device is restarted; otherwise, proceed to step (3-2).
[0078] Step (3-2) includes steps (3-2-1) to (3-2-6).
[0079] Step (3-2-1): Clear the "Check and Backup Complete" marker in the preset location in the boot communication partition where it is stored. Because firmware upgrades cause recovery mode firmware updates, this "Check and Backup Complete" marker needs to be cleared to allow for a subsequent backup of the recovery mode firmware. Check if the backup of the recovery mode firmware image file before the upgrade has been completed. If the recovery mode firmware image file before the upgrade has been backed up, proceed to step (3-2-3); otherwise, proceed to step (3-2-2).
[0080] In one alternative implementation, step (3-2-1) includes steps (a) to (d).
[0081] Step (a): Clear the check and backup completion marker in the preset location in the boot communication partition where the check and backup completion marker is stored.
[0082] Step (b): Read the recovery mode firmware upgrade information from the preset location in the boot communication partition used to store the recovery mode firmware upgrade information, and check whether the magic number of the recovery mode firmware upgrade information is the preset magic number of the recovery mode firmware upgrade information. If not, proceed to step (3-2-2); otherwise, proceed to step (c).
[0083] Step (c): Check if the recovery mode firmware image file exists in the path of the recovery mode firmware image file before the upgrade. If it does not exist, proceed to step (3-2-2); otherwise, proceed to step (d).
[0084] Step (d): Calculate the digital digest of the pre-upgrade recovery mode firmware image file under the pre-upgrade recovery mode firmware image file path of the recovery mode firmware upgrade information according to the digital digest algorithm type of the recovery mode firmware upgrade information, and compare the calculated digital digest with the pre-upgrade recovery mode firmware digital digest of the recovery mode firmware upgrade information. If they are inconsistent, delete the pre-upgrade recovery mode firmware image file under the preset temporary file path and then execute step (3-2-2); otherwise, execute step (3-2-3).
[0085] Step (3-2-2): Back up the current recovery mode firmware as a recovery mode firmware image file to the preset temporary file path (e.g., / cache / pre_ota_recovery.img).
[0086] Construct recovery mode firmware upgrade information consisting of a magic number, a digital digest algorithm type, the path to the recovery mode firmware image file before the upgrade, the digital digest of the recovery mode firmware before the upgrade, the path to the recovery mode firmware image file after the upgrade, and the digital digest of the recovery mode firmware after the upgrade. Set the magic number of the recovery mode firmware upgrade information to a preset magic number, set the digital digest algorithm type to a preset digital digest algorithm, and set the path to the recovery mode firmware image file before the upgrade to a preset path under a preset temporary file path. Calculate the digital digest of the recovery mode firmware image file under the path to the recovery mode firmware image file before the upgrade based on the digital digest algorithm type, and save the calculated digital digest to the path to the recovery mode firmware image file before the upgrade. Write the updated recovery mode firmware upgrade information to a preset location in the boot communication partition used to store the recovery mode firmware upgrade information. Then execute the following steps (3-2-3).
[0087] In one alternative implementation, step (3-2-2) includes steps (a) to (c).
[0088] Step (a): Create an empty pre-upgrade recovery mode firmware image file in the preset temporary file path under the preset pre-upgrade recovery mode firmware image file path.
[0089] Step (b): Obtain the size of the recovery mode firmware partition based on the block device node file of the recovery mode firmware (e.g., / dev / block / by-name / recovery).
[0090] Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read the data of the recovery mode firmware partition size, and write the read data into the recovery mode firmware image file under the preset temporary file path (such as / cache / pre_ota_recovery.img). The size of the written data is the recovery mode firmware partition size.
[0091] Step (c): Starting from the beginning of the preset recovery mode firmware image file under the preset temporary file path, read back the data of the recovery mode firmware partition size, and compare the read data with the corresponding data read from the block device node file of the recovery mode firmware in binary. If they are completely consistent, it means that the current recovery mode firmware has been successfully backed up to the preset recovery mode firmware image file under the preset temporary file path. Then, construct the recovery mode firmware upgrade information composed of magic number, digital digest algorithm type, path of the recovery mode firmware image file before upgrade, digital digest of the recovery mode firmware before upgrade, path of the recovery mode firmware image file after upgrade, and digital digest of the recovery mode firmware after upgrade. Set the magic number of the recovery mode firmware upgrade information to the preset magic number of the recovery mode firmware upgrade information, set the digital digest algorithm type of the recovery mode firmware upgrade information to the preset digital digest algorithm, and set the path of the recovery mode firmware image file before upgrade to the preset path of the preset recovery mode firmware image file under the preset temporary file path. Calculate the digital digest of the pre-upgrade recovery mode firmware image file located at the pre-upgrade recovery mode firmware image file path based on the digital digest algorithm type of the recovery mode firmware upgrade information, and save the calculated digital digest to the pre-upgrade recovery mode firmware digital digest of the recovery mode firmware upgrade information. Write the updated recovery mode firmware upgrade information to the preset location in the boot communication partition used to store the recovery mode firmware upgrade information, and then execute the following steps (3-2-3). Otherwise, rewrite the block device node file corresponding to the recovery mode firmware to the pre-upgrade recovery mode firmware image file located at the preset temporary file path until the data of the two are completely consistent. Then construct the recovery mode firmware upgrade information composed of the magic number, digital digest algorithm type, pre-upgrade recovery mode firmware image file path, pre-upgrade recovery mode firmware digital digest, post-upgrade recovery mode firmware image file path, and post-upgrade recovery mode firmware digital digest. The magic number for the recovery mode firmware upgrade information is set to a preset magic number. The digital digest algorithm type for the recovery mode firmware upgrade information is set to a preset digital digest algorithm. The path to the pre-upgrade recovery mode firmware image file for the recovery mode firmware upgrade information is set to a preset path under the preset temporary file path. The digital digest of the pre-upgrade recovery mode firmware image file in the path to the pre-upgrade recovery mode firmware image file for the recovery mode firmware upgrade information is calculated based on the digital digest algorithm type, and the calculated digital digest is saved to the pre-upgrade recovery mode firmware digital digest file.Write the updated recovery mode firmware upgrade information to the preset location in the boot communication partition used to save the recovery mode firmware upgrade information, and finally execute the following steps (3-2-3).
[0092] Step (3-2-3): Based on the standard Android firmware upgrade package under the upgrade package path of the upgrade metadata, perform firmware upgrade according to the preset Android system upgrade process.
[0093] After the upgrade is complete, check if the firmware image file for recovery mode has been backed up. If the firmware image file for recovery mode has been backed up, proceed to step (3-2-5); otherwise, proceed to step (3-2-4).
[0094] In one alternative implementation, step (3-2-3) includes steps (a) through (c). Step (a): Based on the standard Android firmware upgrade package under the upgrade package path of the upgrade metadata, perform firmware upgrade according to the preset Android system upgrade process.
[0095] Step (b): After the upgrade is completed, check if the upgraded recovery mode firmware image file exists in the path of the recovery mode firmware upgrade information. If it does not exist, proceed to step (3-2-4); otherwise, proceed to step (c).
[0096] Step (c): Calculate the digital digest of the upgraded recovery mode firmware image file under the path of the upgraded recovery mode firmware image file in the recovery mode firmware upgrade information according to the digital digest algorithm type, and compare the calculated digital digest with the upgraded recovery mode firmware digital digest of the recovery mode firmware upgrade information. If they do not match, delete the upgraded recovery mode firmware image file under the preset temporary file path, and then execute the following step (3-2-4); otherwise, execute the following step (3-2-5).
[0097] Step (3-2-4): Back up the current recovery mode firmware again as a recovery mode firmware image file to the preset upgraded recovery mode firmware image file path under the preset temporary file path (e.g., / cache / after_ota_recovery.img).
[0098] Set the path of the upgraded recovery mode firmware image file in the recovery mode firmware upgrade information to the preset path of the upgraded recovery mode firmware image file under the preset temporary file path. Calculate the digital digest of the upgraded recovery mode firmware image file under the upgraded recovery mode firmware image file path according to the digital digest algorithm type of the recovery mode firmware upgrade information, and save the calculated digital digest to the upgraded recovery mode firmware digital digest of the recovery mode firmware upgrade information. Write the updated recovery mode firmware upgrade information to the preset location in the boot communication partition used to save the recovery mode firmware upgrade information. Then execute the following steps (3-2-5).
[0099] In an alternative implementation, step (3-2-4) includes steps (a) to (c).
[0100] Step (a): Create an empty post-upgrade recovery mode firmware image file in the preset temporary file path under the preset post-upgrade recovery mode firmware image file path.
[0101] Step (b): Obtain the size of the recovery mode firmware partition based on the block device node file of the recovery mode firmware (e.g., / dev / block / by-name / recovery).
[0102] Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read the data of the recovery mode firmware partition size, and write the read data into the upgraded recovery mode firmware image file (e.g., / cache / after_ota_recovery.img) under the preset temporary file path. The size of the written data is the recovery mode firmware partition size.
[0103] Step (c): Starting from the beginning of the upgraded recovery mode firmware image file under the preset temporary file path, read back the data of the recovery mode firmware partition size, and compare the read data with the corresponding data read from the block device node file of the recovery mode firmware. If they are completely consistent, it means that the current recovery mode firmware has been successfully backed up to the upgraded recovery mode firmware image file under the preset temporary file path. Then, set the upgraded recovery mode firmware image file path of the recovery mode firmware upgrade information to the preset upgraded recovery mode firmware image file path under the preset temporary file path. Calculate the digital digest of the upgraded recovery mode firmware image file under the upgraded recovery mode firmware image file path of the recovery mode firmware upgrade information according to the digital digest algorithm type of the recovery mode firmware upgrade information, and save the calculated digital digest to the upgraded recovery mode firmware digital digest of the recovery mode firmware upgrade information. Then update the recovery mode... Firmware upgrade information is written to a preset location in the boot communication partition used to save recovery mode firmware upgrade information, and then the following steps (3-2-5) are executed; otherwise, the block device node file corresponding to the recovery mode firmware is rewritten to the upgraded recovery mode firmware image file under the preset temporary file path, until the data of the two are completely consistent. Then, the upgraded recovery mode firmware image file path of the recovery mode firmware upgrade information is set to the preset upgraded recovery mode firmware image file path under the preset temporary file path. The digital digest of the upgraded recovery mode firmware image file under the upgraded recovery mode firmware image file path of the recovery mode firmware upgrade information is calculated according to the digital digest algorithm type of the recovery mode firmware upgrade information, and the calculated digital digest is saved to the upgraded recovery mode firmware digital digest of the recovery mode firmware upgrade information. The updated recovery mode firmware upgrade information is written to the preset location in the boot communication partition used to save recovery mode firmware upgrade information, and finally the following steps (3-2-5) are executed.
[0104] Step (3-2-5): Compare the digital digest of the recovery mode firmware before the upgrade with the digital digest of the recovery mode firmware after the upgrade. If they are different, it means that the recovery mode firmware has been upgraded in this firmware upgrade process. Write the preset recovery mode check and backup instructions to the preset location in the boot communication partition used to save the recovery mode check and backup instructions, and then execute the following step (3-2-6); otherwise, directly execute the following step (3-2-6).
[0105] Step (3-2-6): Delete the pre-upgrade recovery mode firmware image file in the preset temporary file path and the post-upgrade recovery mode firmware image file in the preset post-upgrade recovery mode path; create a preset upgrade completion marker file (e.g., / cache / recovery / update_result) in the preset path of the preset unencrypted readable and writable partition; clear the recovery mode firmware upgrade information in the preset location used to save recovery mode firmware upgrade information in the boot communication partition; clear the upgrade metadata in the preset location used to save upgrade metadata in the boot communication partition, then exit recovery mode and restart the device.
[0106] Step (4): Read the recovery mode check and backup instructions from the preset location in the boot communication partition where the recovery mode check and backup instructions are stored. Check whether the read recovery mode check and backup instructions are the preset recovery mode check and backup instructions. If not, proceed to step (5); otherwise, proceed to steps (4-1) to (4-4).
[0107] Step (4-1): Read the recovery mode firmware backup metadata from the preset location in the boot communication partition used to store the recovery mode firmware backup metadata. Check whether the magic number of the recovery mode firmware backup metadata is the preset magic number of the recovery mode firmware backup metadata. If not, proceed to step (4-2); otherwise, proceed to step (4-3).
[0108] Step (4-2): Set the magic number of the recovery mode firmware backup metadata to the preset magic number; set the first backup path of the recovery mode firmware backup metadata to the preset backup path of the unencrypted data partition (e.g., / cache / recovery / recovery.img); set the second backup path of the recovery mode firmware backup metadata to the preset backup path of the encrypted data partition (e.g., / data / recovery_bak / recovery.img); set the digital digest algorithm type of the recovery mode firmware backup metadata to the preset digital digest algorithm; then execute step (4-3). Here, the first and second backup paths are set again for safety, redundancy, and to ensure consistent settings and avoid potential data errors in abnormal situations.
[0109] Step (4-3): Delete the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata, and then back up the current recovery mode firmware to the recovery mode firmware first backup path of the recovery mode firmware backup metadata as a recovery mode firmware image file.
[0110] In one alternative implementation, step (4-3) includes steps (a) to (c).
[0111] Step (a): Delete the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata, and at the same time create an empty recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata.
[0112] Step (b): Obtain the size of the recovery mode firmware partition based on the block device node file of the recovery mode firmware (e.g., / dev / block / by-name / recovery).
[0113] Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read the data of the recovery mode firmware partition size, and write the read data into the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata. The size of the written data is the recovery mode firmware partition size.
[0114] Step (c): Starting from the beginning of the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata, read back the data of the recovery mode firmware partition size, and compare the read data with the corresponding data read from the block device node file of the recovery mode firmware in binary. If they are completely consistent, it means that the current recovery mode firmware has been successfully backed up to the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata, and then execute the following step (4-4); otherwise, rewrite the block device node file corresponding to the recovery mode firmware into the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata until the data of the two are completely consistent, and then execute the following step (4-4).
[0115] Step (4-4): Calculate the digital digest of the recovery mode firmware image file under the first backup path of the recovery mode firmware according to the digital digest algorithm type of the recovery mode firmware backup metadata, and save the calculated digital digest to the recovery mode firmware digital digest of the recovery mode firmware backup metadata. Write the updated recovery mode firmware backup metadata to the preset location in the boot communication partition used to store the recovery mode firmware backup metadata.
[0116] Write the preset check and backup completion flag in the preset location in the boot communication partition where the check and backup completion flag is stored.
[0117] Clear the recovery mode check and backup instructions from the default location in the boot communication partition where recovery mode check and backup instructions are stored, and then restart the device.
[0118] Step (5): Read the factory reset information from the preset location in the boot communication partition used to save the factory reset information, and check whether the factory reset information is the preset factory reset command. If not, execute the following step (6); otherwise, execute steps (5-1) to (5-6).
[0119] Step (5-1): Read the recovery mode firmware backup metadata from the preset location in the boot communication partition used to store the recovery mode firmware backup metadata, and check whether the magic number of the recovery mode firmware backup metadata is the preset magic number of the recovery mode firmware backup metadata. If not, proceed to step (5-2); otherwise, proceed to step (5-3).
[0120] Step (5-2): Write the preset factory instructions into the boot communication partition to save the preset location of the factory instructions, and then restart the device.
[0121] Step (5-3): Check if there is a recovery mode firmware image file in the first backup path of the recovery mode firmware backup metadata. If it exists, execute the following step (5-4); otherwise, complete the preset factory reset function according to the preset process, clear the factory reset command in the preset location used to save the factory reset information in the boot communication partition, and then exit recovery mode and restart the device.
[0122] Step (5-4): Back up the recovery mode firmware image file under the recovery mode firmware first backup path to the preset temporary memory file path under the preset factory reset recovery mode firmware image file path (e.g., / tmp / factory_reset / recovery.img). In one alternative implementation, step (5-4) includes steps (a) to (c).
[0123] Step (a): Create an empty firmware image file for the factory reset recovery mode under the preset temporary memory file path and the preset recovery mode firmware image file path.
[0124] Step (b): Calculate the size of the recovery mode firmware image file under the first backup path of the recovery mode firmware, which contains the backup metadata of the recovery mode firmware.
[0125] Starting from the beginning of the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata, read the data of the recovery mode firmware image file size, and write the read data into the recovery mode firmware image file before factory reset under the preset temporary memory file path (e.g., / tmp / factory_reset / recovery.img). The size of the written data is the size of the recovery mode firmware image file.
[0126] Step (c): Starting from the beginning of the preset temporary memory file path under the preset factory reset recovery mode firmware image file path, read back the data of the recovery mode firmware image file size, and compare the read data with the corresponding data read from the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata in binary. If they are completely consistent, proceed to step (5-5); otherwise, rewrite the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata into the preset temporary memory file path under the preset factory reset recovery mode firmware image file path, until the data of the two are completely consistent, and then proceed to step (5-5).
[0127] Step (5-5): Complete the preset factory reset function according to the preset process. Since the factory reset will reformat the unencrypted data partitions (such as cache) and encrypted data partitions (such as userdata) in the system, the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata will be deleted; then copy the preset recovery mode firmware image file before the factory reset under the preset temporary memory file path to the recovery mode firmware first backup path of the recovery mode firmware backup metadata.
[0128] In one alternative implementation, step (5-5) includes steps (a) to (d).
[0129] Step (a): Complete the preset factory reset function according to the preset process. Since the factory reset will reformat the unencrypted data partitions (such as cache) and encrypted data partitions (such as userdata) in the system, the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata will be deleted.
[0130] Step (b): Create an empty recovery mode firmware image file in the recovery mode firmware first backup path of the recovery mode firmware backup metadata.
[0131] Step (c): Calculate the size of the firmware image file for the recovery mode before factory reset under the preset temporary memory file path. The calculated size is the size of the recovery mode firmware image file.
[0132] Starting from the beginning of the recovery mode firmware image file under the preset temporary memory file path and the preset factory reset recovery mode firmware image file path, read the data of the recovery mode firmware image file size, and write the read data into the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata. The written data size is the recovery mode firmware image file size.
[0133] Step (d): Starting from the beginning of the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata, read back the data of the recovery mode firmware image file size, and compare the read data with the corresponding data read from the recovery mode firmware image file under the preset temporary memory file path under the preset factory reset recovery mode firmware image file path under the factory reset recovery mode firmware image file path under the factory reset recovery mode firmware image file path under the recovery mode firmware backup metadata until the data of the two are completely consistent, and then execute the following steps (5-6).
[0134] Steps (5-6): Create a preset factory reset completion marker file (e.g., / cache / recovery / factory_reset_done) in the preset path of the preset unencrypted readable and writable partition.
[0135] Clear the factory reset command from the preset location in the boot communication partition where factory reset information is saved, then exit recovery mode and restart the device.
[0136] (6) Perform the relevant preset operations according to the preset process, and finally exit the recovery mode and restart the device.
[0137] Step 205: After the device enters the Android system, run the recovery mode firmware detection service to verify whether the current recovery mode firmware has been backed up properly and update the backup recovery mode firmware under the first backup path and the second backup path.
[0138] In an alternative implementation, step 205 includes steps (1) to (4).
[0139] Step (1): Read the check and backup completion mark from the preset location in the boot communication partition where the check and backup completion mark is stored. Check whether the read check and backup completion mark is the preset check and backup completion mark. If not, proceed to step (2). Otherwise, check if there is a recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata. If so, calculate the digital digest of the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata according to the digital digest algorithm type of the recovery mode firmware backup metadata, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If they are consistent, delete the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata, copy the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata to the recovery mode firmware second backup path of the recovery mode firmware backup metadata, clear the check and backup completion mark in the preset location used to save the check and backup completion mark in the boot communication partition, and then perform the following steps (2).
[0140] In one alternative implementation, step (1) includes steps (1-1) to (1-4).
[0141] Step (1-1): Read the recovery mode firmware backup metadata from the preset location in the boot communication partition used to save the recovery mode firmware backup metadata. Check if the magic number of the recovery mode firmware backup metadata is the preset magic number of the recovery mode firmware backup metadata. If not, write the preset factory reset instruction to the preset location in the boot communication partition used to save the factory reset instruction, and then remind the user that the recovery mode firmware backup is abnormal and will be repaired on the next device restart. Exit the recovery mode firmware detection service; otherwise, proceed to step (1-2).
[0142] Step (1-2): Check if there is a recovery mode firmware image file in the first backup path of the recovery mode firmware backup metadata. If not, proceed to step (2); otherwise, proceed to step (1-3).
[0143] Step (1-3): Calculate the digital digest of the recovery mode firmware image file under the first backup path of the recovery mode firmware according to the digital digest algorithm type of the recovery mode firmware backup metadata, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If the two are inconsistent, proceed to step (2); otherwise, proceed to step (1-4).
[0144] Steps (1-4): Delete the recovery mode firmware image file in the second backup path of the recovery mode firmware, which contains the backup metadata of the recovery mode firmware.
[0145] Copy the recovery mode firmware image file from the first backup path of the recovery mode firmware backup metadata to the second backup path of the recovery mode firmware backup metadata.
[0146] Clear the check and backup completion markers in the preset location in the boot communication partition used to save the check and backup completion markers, and perform the following steps (2).
[0147] Step (2): Check if there is a preset factory reset completion marker file (such as / cache / recovery / factory_reset_done) in the preset path of the preset unencrypted readable and writable partition. If not, proceed to the following step (3). If it exists, check if there is a recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata. If so, calculate the digital digest of the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata according to the digital digest algorithm type of the recovery mode firmware backup metadata, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If they are consistent, delete the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata, copy the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata to the recovery mode firmware second backup path of the recovery mode firmware backup metadata, delete the preset factory reset completion marker file under the preset path of the preset unencrypted readable and writable partition, and then perform the following steps (3).
[0148] In one alternative implementation, step (2) includes steps (2-1) to (2-4).
[0149] Step (2-1): Read the recovery mode firmware backup metadata from the preset location in the boot communication partition used to save the recovery mode firmware backup metadata. Check if the magic number of the recovery mode firmware backup metadata is the preset magic number of the recovery mode firmware backup metadata. If not, write the preset factory reset instruction to the preset location in the boot communication partition used to save the factory reset instruction. Then, remind the user that the recovery mode firmware backup is abnormal and will be repaired during the next device restart. Exit the recovery mode firmware detection service. Otherwise, proceed to step (2-2).
[0150] Step (2-2): Check if there is a recovery mode firmware image file in the first backup path of the recovery mode firmware backup metadata. If not, proceed to step (3); if yes, proceed to step (2-3).
[0151] Step (2-3): Calculate the digital digest of the recovery mode firmware image file under the first backup path of the recovery mode firmware in the recovery mode firmware backup metadata according to the digital digest algorithm type, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If they are inconsistent, proceed to step (3); if they are consistent, proceed to step (2-4).
[0152] Step (2-4): Delete the recovery mode firmware image file in the second backup path of the recovery mode firmware, which contains the backup metadata of the recovery mode firmware.
[0153] Copy the recovery mode firmware image file from the first backup path of the recovery mode firmware backup metadata to the second backup path of the recovery mode firmware backup metadata.
[0154] Delete the preset factory reset completion marker file in the preset path of the preset unencrypted readable and writable partition. Perform the following steps (3).
[0155] Step (3): Check if there is a preset upgrade completion marker file in the preset path of the preset unencrypted readable and writable partition. If not, proceed to step (4). Otherwise, check if a preset upgrade success string exists in the system log. If so, write the preset recovery mode check and backup instructions to the preset location in the boot communication partition where recovery mode check and backup instructions are saved. Delete the preset upgrade completion marker file in the preset path of the preset unencrypted readable and writable partition. Remind the user that the backed-up recovery mode firmware is pending an update and will be updated on the next device reboot. Exit the recovery mode firmware detection service.
[0156] In one alternative implementation, step (3) includes steps (3-1) to (3-4).
[0157] Step (3-1): Check if there is a preset upgrade success string in the system log. If so, proceed to step (3-2); otherwise, proceed to step (3-3).
[0158] Step (3-2): Write the preset recovery mode check and backup instructions to the preset location in the boot communication partition where the recovery mode check and backup instructions are saved; delete the preset upgrade completion marker file in the preset path of the preset unencrypted readable and writable partition; remind the user that the backed-up recovery mode firmware is waiting to be updated and will be updated on the next device restart; and exit the recovery mode firmware detection service.
[0159] Step (3-3): Check if there is a preset upgrade failure string in the system log. If so, delete the preset upgrade completion marker file under the preset path of the preset unencrypted readable and writable partition, and then execute the following step (4); otherwise, execute the following step (3-4).
[0160] Step (3-4): Increment the detection count by 1. When the detection count exceeds the preset maximum detection count, delete the preset upgrade completion marker file under the preset path of the preset unencrypted readable and writable partition, and then execute step (4) as follows; otherwise, hibernate for the preset time, and then re-execute step (3-1) as above. Set the maximum detection count to avoid continuous detection, for example, a maximum of 6 detections. Under normal circumstances, 1-2 detections are sufficient.
[0161] Step (4): Based on the judgment results of whether there is an image file of the first backup recovery mode firmware under the first backup path and whether there is an image file of the second backup recovery mode firmware under the second backup path, update the first backup recovery mode firmware and the second backup recovery mode firmware to ensure that the contents of the first backup recovery mode firmware and the second backup recovery mode firmware are consistent and consistent with the current backup recovery mode firmware.
[0162] In one alternative implementation, step (4) includes steps (4-1) to (4-6). Step (4-1): Read the recovery mode firmware backup metadata from the preset location in the boot communication partition used to save the recovery mode firmware backup metadata. Check if the magic number of the recovery mode firmware backup metadata is the preset magic number of the recovery mode firmware backup metadata. If not, write the preset factory reset instruction to the preset location in the boot communication partition used to save the factory reset instruction. Then, remind the user that the recovery mode firmware backup is abnormal and will be repaired during the next device restart. Exit the recovery mode firmware detection service. Otherwise, proceed to step (4-2).
[0163] Step (4-2): Create a first backup existence flag and a second backup existence flag (e.g., Boolean variables), and set their initial values to false. Check if a recovery mode firmware image file exists in the first backup path of the recovery mode firmware in the recovery mode firmware backup metadata. If yes, set the first backup existence flag to true; otherwise, set it to false. Check if a recovery mode firmware image file exists in the second backup path of the recovery mode firmware in the recovery mode firmware backup metadata. If yes, set the second backup existence flag to true; otherwise, set it to false. If both the first and second backup existence flags are false, proceed to step (4-3). If both the first and second backup existence flags are true, proceed to step (4-4). If both the first and second backup existence flags are true, proceed to step (4-5). If both the first and second backup existence flags are true, proceed to step (4-6).
[0164] Step (4-3): Write the preset factory instructions into the boot communication partition to the preset location for saving the factory instructions, then remind the user that the recovery mode firmware backup is abnormal and will be repaired during the next device restart, and exit the recovery mode firmware detection service.
[0165] Step (4-4): Calculate the digital digest of the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata according to the digital digest algorithm type. Compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If they are inconsistent, it means that the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata is corrupted. Write the preset factory instructions to the preset location in the boot communication partition used to save the factory instructions, and then remind the user that the recovery mode firmware backup is abnormal. It will be repaired during the next device restart, and the recovery mode firmware detection service will be exited. Otherwise, copy the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata to the second backup path of the recovery mode firmware backup metadata, and exit the recovery mode firmware detection service.
[0166] Steps (4-5): Calculate the digital digest of the recovery mode firmware image file under the second backup path of the recovery mode firmware backup metadata based on the digital digest algorithm type. Compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If they are inconsistent, it indicates that the recovery mode firmware image file under the second backup path of the recovery mode firmware backup metadata is corrupted. Write the preset factory instructions to the preset location in the boot communication partition used to save the factory instructions, then remind the user that the recovery mode firmware backup is abnormal and will be repaired during the next device restart, and exit the recovery mode firmware detection service; otherwise, copy the recovery mode firmware image file under the second backup path of the recovery mode firmware backup metadata to the first backup path of the recovery mode firmware backup metadata, and exit the recovery mode firmware detection service.
[0167] Step (4-6): Calculate the digital digest of the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata according to the digital digest algorithm type, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If the two are inconsistent, delete the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata.
[0168] The digital digest algorithm type of the recovery mode firmware backup metadata is used to calculate the digital digest of the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata. The calculated digital digest is compared with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If the two are inconsistent, the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata is deleted.
[0169] Check if a recovery mode firmware image file exists in the first backup path of the recovery mode firmware. If it does, set the first backup existence flag to true; otherwise, set the first backup existence flag to false.
[0170] Check if a recovery mode firmware image file exists in the recovery mode firmware second backup path of the recovery mode firmware backup metadata. If it does, set the second backup existence flag to true; otherwise, set the second backup existence flag to false.
[0171] If both the first backup and the second backup are marked as false, proceed to step (4-3) above. If both the first backup and the second backup are marked as true, proceed to step (4-4) above. If both the first backup and the second backup are marked as true, proceed to step (4-5) above. If both the first backup and the second backup are marked as true, it means that the recovery mode firmware image files under the first backup path and the second backup path are both complete, and the recovery mode firmware detection service exits.
[0172] Step 206: After the device enters the Android system, the firmware upgrade service in the system runs to determine whether a firmware upgrade is needed and whether it can enter recovery mode normally. If it cannot enter recovery mode normally, the current recovery mode firmware is repaired by backing up the recovery mode firmware.
[0173] In one alternative implementation, step 206 includes steps (1) through (7). Step (1): Check if there is a firmware upgrade package that needs to be upgraded; if not, wait for the next new version check according to the preset process; otherwise, proceed to step (2).
[0174] Step (2): Check if there is a preset recovery mode firmware corruption flag (such as recovery_corrupt) in the command line parameters. If not, proceed to step (3); if there is a preset recovery mode firmware corruption flag (such as recovery_corrupt), it means that the recovery mode firmware is corrupted, and proceed to step (4).
[0175] Step (3): Delete the existing upgrade package under the preset upgrade package path, then obtain the standard Android firmware upgrade package according to the preset process and save the obtained standard Android firmware upgrade package to the preset upgrade package path. According to the preset process, use the corresponding upgrade package public key in the Android system firmware to verify the standard Android firmware upgrade package. If the verification fails, prompt that the upgrade package is abnormal, delete the standard Android firmware upgrade package under the preset upgrade package path, and then wait for the next new version check according to the preset process. If the verification is successful, construct the upgrade meta information composed of the upgrade command and the upgrade package path, set the upgrade command of the upgrade meta information to the preset upgrade command, set the upgrade package path of the upgrade meta information to the preset upgrade package path, and write the updated upgrade meta information to the preset location in the boot communication partition for saving the upgrade meta information, and then restart the device.
[0176] Step (4): Verify the first backup recovery mode firmware under the first backup path. If the verification is successful, restore the current recovery mode firmware using the first backup recovery mode firmware. Otherwise, verify the second backup recovery mode firmware under the second backup path. If the verification is successful, restore the current recovery mode firmware using the second backup recovery mode firmware.
[0177] In one alternative implementation, step (4) includes steps (4-1) to (4-5).
[0178] Step (4-1): Read the recovery mode firmware backup metadata from the preset location in the boot communication partition used to store the recovery mode firmware backup metadata. Check if the magic number of the recovery mode firmware backup metadata is the preset magic number of the recovery mode firmware backup metadata. If yes, proceed to step (4-2); otherwise, prompt that the recovery mode firmware is damaged and cannot be repaired, and exit the firmware upgrade service.
[0179] Step (4-2): Check if there is a recovery mode firmware image file in the first backup path of the recovery mode firmware backup metadata. If not, proceed to step (4-4). Otherwise, calculate the digital digest of the recovery mode firmware image file under the first backup path of the recovery mode firmware according to the digital digest algorithm type of the recovery mode firmware backup metadata, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If the two are completely consistent, proceed to step (4-3); otherwise, proceed to step (4-4).
[0180] Step (4-3) includes steps (4-3-1) to (4-3-3).
[0181] Step (4-3-1): Obtain the size of the recovery mode firmware partition based on the block device node file of the recovery mode firmware (e.g., / dev / block / by-name / recovery).
[0182] The size of the recovery mode firmware image file in the first backup path of the recovery mode firmware, which is used to calculate the recovery mode firmware backup metadata, is taken as the recovery mode firmware image file size.
[0183] Check if the size of the recovery mode firmware partition is smaller than the size of the recovery mode firmware image file. If so, prompt that the recovery mode firmware is corrupted and the recovery mode firmware partition is too small to recover, and exit the firmware upgrade service; otherwise, perform the following steps (4-3-2).
[0184] Step (4-3-2): Starting from the beginning of the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata, read the data of the recovery mode firmware image file size, and write it sequentially from the beginning of the block device node file corresponding to the recovery mode firmware, and finally write the data of the recovery mode firmware image file size.
[0185] Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read back the data of the recovery mode firmware image file size, and perform a binary comparison between the read data and the corresponding data of the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata. If they are completely consistent, it means that the damaged recovery mode firmware has been successfully repaired, and then proceed to the following step (4-3-3); otherwise, rewrite the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata into the block device node file corresponding to the recovery mode firmware until the data of the two are completely consistent, and then proceed to the following step (4-3-3).
[0186] Step (4-3-3): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store recovery mode exit metadata, check if the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if so, set the recovery mode boot count of the recovery mode exit metadata to 0; otherwise, initialize the recovery mode exit metadata, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0187] The updated recovery mode exit metadata is written to the preset location in the boot communication partition used to store recovery mode exit metadata. Then, the following step (5) is executed. Step (4-4): Check if there is a recovery mode firmware image file in the second backup path of the recovery mode firmware backup metadata. If not, prompt that the recovery mode firmware is damaged and cannot be repaired, and exit the firmware upgrade service. If so, calculate the digital digest of the recovery mode firmware image file under the second backup path of the recovery mode firmware according to the digital digest algorithm type of the recovery mode firmware backup metadata, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If the two are completely consistent, proceed with the following steps (4-5); otherwise, prompt that the recovery mode firmware is damaged and cannot be repaired, and exit the firmware upgrade service.
[0188] Steps (4-5) include steps (4-5-1) to (4-5-3).
[0189] Step (4-5-1): Obtain the size of the recovery mode firmware partition based on the block device node file of the recovery mode firmware (e.g., / dev / block / by-name / recovery).
[0190] The size of the recovery mode firmware image file in the second backup path of the recovery mode firmware, which is used to calculate the recovery mode firmware backup metadata, is taken as the recovery mode firmware image file size.
[0191] Check if the size of the recovery mode firmware partition is smaller than the size of the recovery mode firmware image file. If so, prompt that the recovery mode firmware is corrupted and the recovery mode firmware partition is too small to recover, and exit the firmware upgrade service; otherwise, perform the following steps (4-5-2).
[0192] Step (4-5-2): Starting from the beginning of the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata, read the data of the recovery mode firmware image file size, and write it sequentially from the beginning of the block device node file corresponding to the recovery mode firmware, and finally write the data of the recovery mode firmware image file size.
[0193] Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read back the data of the recovery mode firmware image file size, and perform a binary comparison between the read data and the corresponding data of the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata. If they are completely consistent, it means that the damaged recovery mode firmware has been successfully repaired, and then proceed to the following steps (4-5-3); otherwise, rewrite the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata into the block device node file corresponding to the recovery mode firmware until the data of the two are completely consistent, and then proceed to the following steps (4-5-3).
[0194] Step (4-5-3): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store recovery mode exit metadata, check if the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if so, set the recovery mode boot count of the recovery mode exit metadata to 0; otherwise, initialize the recovery mode exit metadata, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0195] The updated recovery mode exit metadata is written to the preset location in the boot communication partition used to store recovery mode exit metadata. Then, the following step (5) is executed. Step (5): Read the upgrade metadata from the preset location in the boot communication partition where the upgrade metadata is stored, and check if there is an upgrade package under the upgrade package path of the upgrade metadata. If there is, execute the following step (6); otherwise, execute the following step (7).
[0196] Step (6): According to the preset process, use the corresponding upgrade package public key in the Android system firmware to verify the standard Android firmware upgrade package under the upgrade package path of the upgrade metadata. If the verification fails, prompt that the upgrade package is abnormal, delete the standard Android firmware upgrade package under the upgrade package path of the upgrade metadata, clear the upgrade metadata, and write the cleared upgrade metadata to the preset location in the boot communication partition for saving the upgrade metadata. Then execute the following step (7): If the verification is successful, set the upgrade command of the upgrade metadata to the preset upgrade command, set the upgrade package path of the upgrade metadata to the preset upgrade package path, and write the updated upgrade metadata to the preset location in the boot communication partition for saving the upgrade metadata. Then restart the device.
[0197] Step (7): Obtain the standard Android firmware upgrade package according to the preset process and save the obtained standard Android firmware upgrade package to the preset upgrade package path. Verify the standard Android firmware upgrade package using the corresponding upgrade package public key in the Android system firmware according to the preset process. If the verification fails, prompt that the upgrade package is abnormal, delete the standard Android firmware upgrade package under the preset upgrade package path, and then wait for the next new version check according to the preset process. If the verification is successful, construct the upgrade meta information composed of the upgrade command and the upgrade package path, set the upgrade command of the upgrade meta information to the preset upgrade command, set the upgrade package path of the upgrade meta information to the preset upgrade package path, and write the updated upgrade meta information to the preset location in the boot communication partition for saving the upgrade meta information, and then restart the device.
[0198] Step 207: After the device enters the Android system, if it receives a factory reset command, it determines whether it can enter recovery mode normally. If it cannot enter recovery mode normally, it repairs the current recovery mode firmware by backing up the recovery mode firmware.
[0199] In one alternative implementation, step 207 includes steps (1) to (3).
[0200] Step (1): Check if there is a preset recovery mode firmware corruption flag (such as recovery_corrupt) in the command line parameters. If not, write the preset recovery factory settings command in the preset location used to save the recovery factory settings information in the boot communication partition, and then restart the device. If there is a preset recovery mode firmware corruption flag (such as recovery_corrupt), it means that the recovery mode firmware is corrupted. Then perform the following step (2).
[0201] Step (2) includes steps (2-1) to (2-3).
[0202] Step (2-1): Read the recovery mode firmware backup metadata from the preset location in the boot communication partition used to store the recovery mode firmware backup metadata. Check if the magic number of the recovery mode firmware backup metadata is the preset magic number of the recovery mode firmware backup metadata. If yes, proceed to step (2-2); otherwise, prompt that the recovery mode firmware is damaged and cannot be repaired, and exit the factory reset.
[0203] Step (2-2): Check if there is a recovery mode firmware image file in the first backup path of the recovery mode firmware backup metadata. If not, proceed to step (2-4). If so, calculate the digital digest of the recovery mode firmware image file under the first backup path of the recovery mode firmware according to the digital digest algorithm type of the recovery mode firmware backup metadata, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If the two are completely consistent, proceed to step (2-3); otherwise, proceed to step (2-4).
[0204] Step (2-3): Upgrade the recovery mode firmware based on the recovery mode firmware image file in the first backup path of the recovery mode firmware backup metadata, thereby repairing the damaged recovery mode firmware.
[0205] Update the recovery mode exit metadata. Read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata. Check if the magic number of the recovery mode exit metadata is the preset magic number. If it is, set the recovery mode boot count of the recovery mode exit metadata to 0; otherwise, initialize the recovery mode exit metadata, set the magic number of the recovery mode exit metadata to the preset magic number, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0206] Write the updated recovery mode exit metadata to the preset location in the boot communication partition used to store recovery mode exit metadata. Then execute the following step (3).
[0207] In one alternative implementation, step (2-3) includes steps (2-3-1) to (2-3-3).
[0208] Step (2-3-1): Obtain the size of the recovery mode firmware partition based on the block device node file of the recovery mode firmware (e.g., / dev / block / by-name / recovery).
[0209] The size of the recovery mode firmware image file in the first backup path of the recovery mode firmware, which is used to calculate the recovery mode firmware backup metadata, is taken as the recovery mode firmware image file size.
[0210] Check if the size of the recovery mode firmware partition is smaller than the size of the recovery mode firmware image file. If so, it will prompt that the recovery mode firmware is corrupted and the recovery mode firmware partition is too small to recover, and exit the factory reset. Otherwise, perform the following steps (2-3-2).
[0211] Step (2-3-2): Starting from the beginning of the recovery mode firmware image file under the recovery mode firmware first backup path of the recovery mode firmware backup metadata, read the data of the recovery mode firmware image file size, and write it sequentially from the beginning of the block device node file corresponding to the recovery mode firmware, and finally write the data of the recovery mode firmware image file size.
[0212] Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read back the size of the recovery mode firmware image file and perform a binary comparison with the corresponding data in the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata. If they are completely consistent, it means that the damaged recovery mode firmware has been successfully repaired, and then proceed to the following steps (2-3-3); otherwise, rewrite the recovery mode firmware image file under the first backup path of the recovery mode firmware backup metadata into the block device node file corresponding to the recovery mode firmware until the data of the two are completely consistent, and then proceed to the following steps (2-3-3).
[0213] Step (2-3-3): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store recovery mode exit metadata, check if the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if so, set the recovery mode boot count of the recovery mode exit metadata to 0; otherwise, initialize the recovery mode exit metadata, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0214] Write the updated recovery mode exit metadata to the preset location in the boot communication partition used to store recovery mode exit metadata. Then perform the following step (3). Step (2-4): Check if there is a recovery mode firmware image file in the second backup path of the recovery mode firmware backup metadata. If not, prompt that the recovery mode firmware is damaged and cannot be repaired, and exit the factory reset.
[0215] If so, calculate the digital digest of the recovery mode firmware image file under the second backup path of the recovery mode firmware according to the digital digest algorithm type of the recovery mode firmware backup metadata, and compare the calculated digital digest with the recovery mode firmware digital digest of the recovery mode firmware backup metadata. If the two are completely consistent, proceed with the following steps (2-5); otherwise, prompt that the recovery mode firmware is damaged and cannot be repaired, and exit the factory reset.
[0216] Step (2-5) upgrade the recovery mode firmware based on the recovery mode firmware image file in the second backup path of the recovery mode firmware backup metadata, thereby repairing the damaged recovery mode firmware.
[0217] Update the recovery mode exit metadata. Read the recovery mode exit metadata from the preset location in the boot communication partition used to store the recovery mode exit metadata. Check if the magic number of the recovery mode exit metadata is the preset magic number. If it is, set the recovery mode boot count of the recovery mode exit metadata to 0; otherwise, initialize the recovery mode exit metadata, set the magic number of the recovery mode exit metadata to the preset magic number, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0218] Write the updated recovery mode exit metadata to the preset location in the boot communication partition used to store recovery mode exit metadata. Then execute the following step (3).
[0219] In one alternative implementation, step (2-5) includes steps (2-5-1) to (2-5-3). Step (2-5-1): Obtain the size of the recovery mode firmware partition based on the block device node file of the recovery mode firmware (e.g., / dev / block / by-name / recovery).
[0220] The size of the recovery mode firmware image file in the second backup path of the recovery mode firmware, which is used to calculate the recovery mode firmware backup metadata, is taken as the recovery mode firmware image file size.
[0221] Check if the size of the recovery mode firmware partition is smaller than the size of the recovery mode firmware image file. If so, it will prompt that the recovery mode firmware is corrupted and the recovery mode firmware partition is too small to recover, and exit the factory reset. Otherwise, perform the following steps (2-5-2).
[0222] Step (2-5-2): Starting from the beginning of the recovery mode firmware image file under the recovery mode firmware second backup path of the recovery mode firmware backup metadata, read the data of the recovery mode firmware image file size, and write it sequentially from the beginning of the block device node file corresponding to the recovery mode firmware, and finally write the data of the recovery mode firmware image file size.
[0223] Starting from the beginning of the block device node file corresponding to the recovery mode firmware, read back the data of the recovery mode firmware image file size, and perform a binary comparison between the read data and the corresponding data of the recovery mode firmware image file under the second backup path of the recovery mode firmware backup metadata. If they are completely consistent, it means that the damaged recovery mode firmware has been successfully repaired, and then proceed to the following step (2-5-3); otherwise, rewrite the recovery mode firmware image file under the second backup path of the recovery mode firmware backup metadata into the block device node file corresponding to the recovery mode firmware until the data of the two are completely consistent, and then proceed to the following step (2-5-3).
[0224] Step (2-5-3): Read the recovery mode exit metadata from the preset location in the boot communication partition used to store recovery mode exit metadata, check if the magic number of the recovery mode exit metadata is the preset magic number of the recovery mode exit metadata, if so, set the recovery mode boot count of the recovery mode exit metadata to 0; otherwise, initialize the recovery mode exit metadata, set the magic number of the recovery mode exit metadata to the preset magic number of the recovery mode exit metadata, and set the recovery mode boot count of the recovery mode exit metadata to 0.
[0225] Write the updated recovery mode exit metadata to the preset location in the boot communication partition used to store recovery mode exit metadata. Then perform the following step (3). Step (3): Write the preset factory reset command in the preset location in the boot communication partition used to save the factory reset information, and then restart the device.
[0226] 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 3The 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 described above. Its beneficial effects are equivalent to the aforementioned firmware upgrade method, and will not be repeated here.
[0227] In summary, the firmware upgrade method and electronic device provided by this invention automatically back up the currently working recovery mode firmware to a preset unencrypted data partition when the device boots up for the first time after firmware burning, and updates the backup recovery mode firmware after each normal firmware upgrade. When the device cannot enter normal recovery mode for firmware upgrade, it automatically exits recovery mode and returns to the previous normal Android system; then, in the normal system, it uses the image file of the backup recovery mode firmware backed up in a preset path to repair the damaged current recovery mode firmware; finally, it re-enters recovery mode for firmware upgrade. This method effectively solves the problems of firmware failure due to damaged recovery mode firmware, and device bricking caused by the inability to exit recovery mode after entering recovery mode due to damaged recovery mode firmware. Furthermore, backing up the recovery mode firmware in preset unencrypted data partitions (such as cache) and preset encrypted data partitions (such as userdata) provides dual protection for the recovery mode firmware. Additionally, after each successful normal firmware upgrade of the recovery mode firmware itself, the device enters recovery mode once to check if the recovery mode is normal; only if the recovery mode is normal is the previously backed-up recovery mode firmware updated. To avoid issues with the upgraded firmware image file itself, which could corrupt the backup recovery mode firmware image file.
[0228] 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: Upon first boot after firmware flashing, enter recovery mode to back up the currently working recovery mode firmware to a preset storage area as backup recovery mode firmware; After each normal firmware upgrade is completed, the backup recovery mode firmware in the preset storage area is updated according to the current recovery mode firmware; as well as If the recovery mode cannot be entered normally, normal mode is started, and the damaged current recovery mode firmware is repaired in normal mode using the backup recovery mode firmware in the preset storage area.
2. The method of claim 1, wherein, Also includes: If a recovery signal requiring entry into recovery mode is received, the number of recovery mode boot attempts is obtained, and it is determined whether the number of recovery mode boot attempts is greater than the maximum number of boot attempts. If so, it means that recovery mode cannot be entered normally, the recovery signal is cleared and the device is restarted; otherwise, the number of recovery mode boot attempts is increased by 1 and the device is entered into recovery mode. After entering the recovery mode, set the number of boot attempts for the recovery mode to 0.
3. The method of claim 1, wherein, Also includes: In normal mode, the number of boot attempts in recovery mode is obtained. It is then determined whether this number exceeds the maximum number of boot attempts. If so, it indicates that normal entry into recovery mode is impossible. After writing a recovery mode firmware corruption flag into the command-line parameters in normal mode, the system firmware is then booted by the startup firmware to enter the system. The current recovery mode firmware that repairs the damaged firmware in the preset storage area during normal mode includes: If a recovery mode firmware corruption flag exists in the command-line parameters, the current recovery mode firmware will be repaired using the backup recovery mode firmware in the preset storage area.
4. The method of claim 1, wherein, Back up the currently working recovery mode firmware to the preset storage area as backup recovery mode firmware, including: If the factory instructions are read from the boot communication partition, the files in the first preset path in the unencrypted data partition are deleted, and an empty recovery mode firmware image file is created in the first preset path. Obtain the partition size of the current recovery mode firmware partition, start from the beginning of the block device node file corresponding to the current recovery mode firmware, read the partition size data, and write the read data into the recovery mode firmware image file as the first backup recovery mode firmware; Construct backup metadata including a first backup path and a second backup path, setting the first backup path to the first preset path, and setting the second backup path to the second preset path in the encrypted data partition; and After clearing the factory reset instructions, exit recovery mode and restart the device.
5. The method of claim 1, wherein, Also includes: If upgrade metadata is read from the boot communication partition, the current recovery mode firmware is backed up to the first temporary path as the recovery mode firmware before the upgrade. Obtain the firmware upgrade package under the upgrade package path in the upgrade metadata, and complete the firmware upgrade process based on the firmware upgrade package; After the firmware upgrade package is completed, updating the backup recovery mode firmware in the preset storage area according to the current recovery mode firmware includes: After the upgrade is complete, the current recovery mode firmware will be backed up to the second temporary path as the upgraded recovery mode firmware; Compare whether the firmware of the recovery mode before the upgrade is consistent with the firmware of the recovery mode after the upgrade. If so, create an upgrade completion marker file, exit recovery mode and restart the device; otherwise, create a check and backup command, and exit recovery mode and restart the device after creating the upgrade completion marker file. If the check and backup instructions are read from the boot communication partition, then after deleting the backup recovery mode firmware in the first backup path from the backup metadata, the current recovery mode firmware is backed up to the first backup path as the new backup recovery mode firmware; and After writing the check and backup completion marker, clear the check and backup command and restart the device.
6. The method of claim 5, wherein, Also includes: If upgrade metadata is read from the boot communication partition, the signature data of the firmware upgrade package is verified by obtaining the upgrade package public key. If the verification fails, an upgrade package error is indicated, and the upgrade metadata is cleared before exiting recovery mode and restarting the device. as well as If the verification is successful, the current recovery mode firmware will be backed up to the first temporary path as the recovery mode firmware before the upgrade.
7. The method of claim 4, wherein, Also includes: If a factory reset command is received, check if the first backup recovery mode firmware exists under the first backup path. If so, store the first backup recovery mode firmware in memory, execute the factory reset command, and restore the first backup recovery mode firmware in memory to the first backup path. as well as After creating the factory reset completion marker file, clear the factory reset command, exit the recovery mode, and restart the device.
8. The method of claim 5, wherein, Also includes: In normal mode, if a check and backup completion marker is detected, it is confirmed whether the first backup recovery mode firmware exists under the first backup path. If so, the first backup recovery mode firmware is verified. If the verification is successful, the second backup recovery mode firmware under the second backup path is updated according to the first backup recovery mode firmware, and the check and backup completion marker is cleared.
9. The method of claim 7, wherein, Also includes: In normal mode, if a factory reset completion marker file is detected, it is confirmed whether the first backup recovery mode firmware exists in the first backup path. If so, the first backup recovery mode firmware is verified. If the verification is successful, the second backup recovery mode firmware in the second backup path is updated according to the first backup recovery mode firmware, and the factory reset completion marker file is cleared.
10. The method of claim 5, wherein, Also includes: In normal mode, if an upgrade completion marker file is detected, check if a preset upgrade success string exists in the system log. If so, create a recovery mode check and backup command and delete the upgrade completion marker file. Otherwise, check if there is a preset upgrade failure string in the system log. If not, increase the number of checks by 1. If the number of checks is greater than the check count threshold, delete the upgrade completion marker file and update the first backup recovery mode firmware under the first backup path and the second backup recovery mode firmware under the second backup path. as well as If the number of detections is less than the detection threshold, then return to confirm whether a preset upgrade success string exists in the system log.
11. The method of claim 10, wherein, The updates to the first backup recovery mode firmware under the first backup path and the second backup recovery mode firmware under the second backup path include: Create a first backup existence flag and a second backup existence flag, and set their initial values to false; Check if the first backup recovery mode firmware exists under the first backup path. If it does, set the first backup existence flag to true; otherwise, set the first backup existence flag to false. Check if the second backup recovery mode firmware exists under the second backup path. If it does, set the second backup existence flag to true; otherwise, set the second backup existence flag to false. If both the first backup and the second backup are marked as false, then write the factory reset command and output a recovery mode firmware error. If the first backup exists and is marked as true, and the second backup exists and is marked as false, then the first backup recovery mode firmware is retrieved from the first backup path and stored in the second backup path as the second backup recovery mode firmware; and If the first backup exists and is marked as false, and the second backup exists and is marked as true, then the second backup recovery mode firmware is obtained from the second backup path and stored in the first backup path as the first backup recovery mode firmware.
12. The method according to claim 1, characterized in that, In normal mode, repairing the corrupted current recovery mode firmware using the backup recovery mode firmware in the preset storage area includes: If a new firmware upgrade request exists, check if a recovery mode firmware corruption flag exists in the command-line parameters. If so, verify the first backup recovery mode firmware under the first backup path. If the verification passes, upgrade the current recovery mode firmware according to the first backup recovery mode firmware and update the recovery mode exit metadata; and If the verification fails, the second backup recovery mode firmware under the second backup path will be verified. If the verification of the second backup recovery mode firmware passes, the current recovery mode firmware will be upgraded according to the second backup recovery mode firmware and the recovery mode exit metadata will be updated. If the verification of the second backup recovery mode firmware fails, the recovery mode firmware will be prompted as corrupted and cannot be repaired, and the firmware upgrade will be exited.
13. 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 12.