Method, electronic device and storage medium for upgrading firmware
By building and writing upgrade metadata and calling the dual-system upgrade engine, the firmware of non-dual-system partitions can be restored or upgraded, thus solving the upgrade anomaly caused by the dependency relationship between A/B partitions and non-A/B partitions, and ensuring the stability and reliability of the Android system.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- FUZHOU ROCKCHIP SEMICON
- Filing Date
- 2024-12-05
- Publication Date
- 2026-04-10
AI Technical Summary
In the Android system, when there is a dependency between A/B partition firmware and non-A/B partition firmware, the existing methods are prone to causing upgrade anomalies, compromising the reliability of A/B upgrades, and leading to system crashes.
By obtaining the dual-system upgrade package, constructing upgrade metadata, writing it to a preset location in the boot communication partition, calling the dual-system upgrade engine to perform the upgrade, restarting the system after the upgrade is completed, ensuring the compatibility of the bootloader firmware, restoring or upgrading the firmware of the non-dual-system partition, writing an upgrade completion marker, and booting the Android system normally according to the preset process.
When there is a dependency relationship between A/B partition firmware and non-A/B partition firmware, ensure the reliability of firmware upgrades, avoid system crashes due to incompatibility, and ensure the integrity and reliability of the upgrade process.
Smart Images

Figure CN119960783B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of embedded devices, and in particular to a method for upgrading firmware, an electronic device, and a storage medium. BACKGROUND
[0002] There are two main firmware OTA (over-the-air) upgrade methods in the Android system, one is a recovery upgrade method, and the other is an A / B upgrade method.
[0003] The recovery upgrade method in the Android system is also referred to as a non-A / B upgrade method. When this upgrade method is used, all firmware to be upgraded in the Android system is required to have only one corresponding partition, such as a boot.img firmware corresponding to a unique boot partition, a system.img firmware corresponding to a unique system partition, and a uboot.img firmware corresponding to a unique uboot partition. When upgrading, the corresponding firmware is upgraded to the corresponding partition, such as upgrading the boot.img firmware to the boot partition.
[0004] When the A / B upgrade method in the Android system is used, all firmware to be upgraded in the Android system is required to have A and B two partitions, which exist in the form of suffixes _a and _b, such as a boot.img firmware being required to have boot_a and boot_b two partitions, and a uboot.img firmware being required to have uboot_a and uboot_b two partitions. All A partition firmware in the A / B system forms an A slot system of the A / B system, and all B partition firmware in the A / B system forms a B slot system of the A / B. When A / B upgrading is performed, if the device is currently running the A slot system, the firmware to be upgraded is upgraded to the B slot, and vice versa. Through this A / B slot method, the reliability of firmware upgrading in the Android system can be ensured.
[0005] The recovery upgrade method and the A / B upgrade method of the current Android system are incompatible, that is, if the A / B upgrade method is used, all firmware to be upgraded in the Android system is required to have A and B two partitions, which exist in the form of suffixes _a and _b. However, in some scenarios, due to space saving considerations, it is necessary to use A / B partitions for key partitions in the Android system, and to use non-A / B forms for non-key and large-space-occupying partitions (such as resource partitions and parameter configuration partitions existing in some products). When OTA upgrading is performed on such a system, the A / B upgrade in the Android system cannot upgrade the non-A / B partition firmware.
[0006] The existing method for supporting upgrading non-A / B firmware in an A / B system requires that there is no dependency between A / B partition firmware and non-A / B partition firmware. If there is a dependency between A / B partition firmware and non-A / B partition firmware, an abnormal upgrade may occur, leading to system crash, thereby damaging the reliability of A / B upgrade. For example, if there is a dependency between an A / B partition X (corresponding to X_a and X_b) and a non-A / B partition Y in the system, when the A / B upgrade upgrades X from X_a to X_b, the system runs X_b firmware after the A / B upgrade is completed. At this time, if the Y firmware in the non-A / B partition has not been upgraded (for example, the non-A / B firmware can be upgraded only after the A / B upgrade is completed, or the non-A / B firmware is unexpectedly powered off and restarted during the upgrade of the non-A / B firmware), the X_b firmware and the dependent Y firmware do not match, thereby causing the A / B system to crash, completely damaging the reliability of the A / B system and the upgrade. SUMMARY
[0007] The present application provides a method for upgrading firmware, an electronic device and a storage medium, which can ensure the reliability of firmware upgrade when there is a dependent coupling relationship between A / B partition firmware and non-A / B partition firmware.
[0008] In one aspect of the present application, a method for upgrading firmware is provided. The method comprises: obtaining a dual-system upgrade package, the dual-system upgrade package comprising a reserved partition firmware image file containing a non-dual-system partition image file to be upgraded; constructing upgrade metadata based on information associated with the non-dual-system partition image file and writing the upgrade metadata to a preset position of a boot communication partition; calling a dual-system upgrade engine to perform dual-system upgrade based on the dual-system upgrade package, and restarting the system after the upgrade is completed to guide the upgraded boot loader firmware; in the boot loader firmware, if the current boot slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual-system partition is different from the non-dual-system partition firmware corresponding to the pre-upgrade slot, then the firmware in the non-dual-system partition is restored to the non-dual-system partition firmware corresponding to the pre-upgrade slot; if the current boot slot is not equal to the pre-upgrade slot information in the upgrade metadata, and the current boot slot is not set as a successful boot mark, then a non-dual-system partition upgrade completion flag is read from the boot communication partition, and if the read non-dual-system partition upgrade completion flag is not a preset non-dual-system partition upgrade completion magic number, then the non-dual-system partition firmware is upgraded, and a preset non-dual-system partition upgrade completion magic number is written in the boot communication partition; and the Android system is normally guided to start according to a preset process.
[0009] In yet another aspect of the present application, 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 such that the electronic device performs the above-mentioned method of upgrading firmware.
[0010] In still another aspect of the present application, a computer readable medium is provided. The medium has stored thereon a computer program which is executed by a processor to implement the above-mentioned method of upgrading firmware.
[0011] According to the present application, the dual-system upgrading engine is invoked to perform dual-system upgrading based on the obtained dual-system upgrading package, and the system is restarted after the upgrading is completed, and the upgraded boot loader firmware is booted. In the boot loader firmware, if the current boot slot is equal to the pre-upgrading slot information, and the firmware in the non-dual-system partition is different from the non-dual-system partition firmware corresponding to the pre-upgrading slot, the firmware in the non-dual-system partition is restored to the non-dual-system partition firmware corresponding to the pre-upgrading slot. If the current boot slot is not equal to the pre-upgrading slot information in the upgrading metadata, and the current boot slot is not set as a successful boot mark, when the read non-dual-system partition upgrading completion mark is not a preset non-dual-system partition upgrading completion magic number, the non-dual-system partition firmware is upgraded, and the preset non-dual-system partition upgrading completion magic number is written in the boot communication partition, and the Android system is normally booted according to the preset process. The boot loader firmware is only normally booted according to the preset process to enter the upgraded Android system after the upgrading of the non-dual-system firmware is completed. In this way, the compatibility between the dual-system firmware and the non-dual-system firmware can be ensured, and the situation that the system cannot be booted due to the incompatibility between the upgraded dual-system firmware and the old non-dual-system firmware can be avoided. When there is a dependent coupling relationship between the dual-system partition firmware and the non-dual-system partition firmware, the reliability of the firmware upgrading can still be ensured. No matter when an exception occurs during the upgrading of the dual-system firmware or the non-dual-system firmware, the reliability of the entire upgrading process can be ensured. BRIEF DESCRIPTION OF DRAWINGS
[0012] Figure 1 A step flowchart of the method of upgrading firmware according to the embodiment of the present application;
[0013] Figure 2 A flowchart of the method of upgrading firmware according to the embodiment of the present application;
[0014] Figure 3 A structural schematic diagram of the electronic device according to the embodiment of the present application. DETAILED DESCRIPTION
[0015] To make the technical content, the achieved purposes and effects of the present application clear, the following will be described in detail in combination with the embodiments and the accompanying drawings.
[0016] In the prior art, the method of supporting upgrading non-A / B firmware in an A / B system requires that there be no dependency between A / B partition firmware and non-A / B partition firmware, and if there is a dependency between A / B partition firmware and non-A / B partition firmware, an abnormal upgrade may occur, leading to system crash, thereby damaging the reliability of A / B upgrade.
[0017] To solve at least the above technical problems, the present disclosure provides a method of upgrading firmware. According to the present disclosure, a dual-system upgrade engine is invoked to perform dual-system upgrade based on an obtained dual-system upgrade package, and after the upgrade is completed, the system is restarted, and the upgraded boot loader firmware is booted. In the boot loader firmware, if the current boot slot is equal to the pre-upgrade slot information, and the firmware in the non-dual-system partition is not the same as the non-dual-system partition firmware corresponding to the pre-upgrade slot, the firmware in the non-dual-system partition is restored to the non-dual-system partition firmware corresponding to the pre-upgrade slot. If the current boot slot is not equal to the pre-upgrade slot information in the upgrade metadata, and the current boot slot is not set as a successful boot flag, when the read non-dual-system partition upgrade completion flag is not a preset non-dual-system partition upgrade completion magic number, the non-dual-system partition firmware is upgraded, and the preset non-dual-system partition upgrade completion magic number is written in the boot communication partition, and the Android system is normally booted according to a preset process. In this way, since the boot loader firmware only enters the upgraded Android system according to the preset process after completing the upgrade of the non-A / B firmware, the compatibility between the A / B firmware and the non-A / B firmware can be ensured, and the situation that the system cannot be started due to the incompatibility between the upgraded A / B firmware and the old non-A / B firmware can be avoided. Moreover, when there is a dependency coupling relationship between the A / B partition firmware and the non-A / B partition firmware, the reliability of firmware upgrade can still be ensured, and no matter when an abnormality occurs during the upgrade of the A / B firmware or the non-A / B firmware, the reliability of the entire upgrade process can be ensured.
[0018] Hereinafter, the technical solution according to the present disclosure will be described with reference to specific embodiments and in conjunction with the accompanying drawings.
[0019] Figure 1 is a flowchart showing a method 100 of upgrading firmware according to an embodiment of the present disclosure. Referring to Figure 1 , the method 100 includes the following steps 102 to 112.
[0020] At step 102, a dual-system upgrade package is obtained, the dual-system upgrade package including a reserved partition firmware image file containing a non-dual-system partition image file to be upgraded.
[0021] In some embodiments, a configuration item of whether the boot loader firmware supports a file system is acquired. If the configuration item indicates that the boot loader firmware supports a file system, a reserved partition firmware image file with a preset file system is generated, the reserved partition firmware image file containing all non-dual system partition image files to be upgraded under a preset directory structure, and for each non-dual system partition image file to be upgraded, the name of the image file being prefixed with the name of the partition corresponding to the image file. If the configuration item indicates that the boot loader firmware does not support a file system, the reserved partition firmware image file without a preset file system is generated, the reserved partition firmware image file being an original image file composed of a file header of a preset size and a file body of a preset structure, the file body containing all non-dual system partition image file data to be upgraded. In this way, seamless integration with the existing Android A / B system upgrade process is achieved without any modification to the A / B upgrade engine in the Android system.
[0022] In step 104, upgrade metadata is constructed based on information associated with the non-dual system partition image file, and the upgrade metadata is written to a preset location of the boot communication partition.
[0023] In some embodiments, the method 100 can further include that a reserved partition (such as reserved_a and reserverd_b) corresponding to the reserved partition firmware image file is added in the partition table, and the reserved partition has a dual system form, the reserved partition firmware image file is generated according to a configuration item of whether the boot loader firmware supports a file system, and the reserved partition firmware image file is added to the dual system upgrade package in the process of constructing the dual system upgrade package. In some embodiments, the firmware in the reserved partition can be upgraded by the dual system upgrade engine.
[0024] In some embodiments, before step 102, the method 100 can further include that a non-dual system partition upgrade completion marker at a preset location in the boot communication partition is cleared, and the upgrade metadata magic number, the digital digest algorithm type, the metadata digital digest value, the upgrade instruction, the reserved partition image type, the pre-upgrade slot information, and the non-dual system partition image information list are determined.
[0025] In some embodiments, the upgrade metadata is generated according to the upgrade metadata magic number, the digital digest algorithm type, the metadata digital digest value, the upgrade instruction, the reserved partition image type, the pre-upgrade slot information, and the non-dual system partition image information list. In this way, by clearing the non-A / B partition upgrade completion marker at the preset location in the boot communication partition, and generating the upgrade metadata, the subsequent upgrade process is facilitated.
[0026] At step 106, the dual-system upgrade engine is invoked to perform a dual-system upgrade based on the dual-system upgrade package, and after the upgrade is completed, the system is restarted and the upgraded boot loader firmware is booted.
[0027] At step 108, in the boot loader firmware, if the current boot slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual-system partition is not the same as the non-dual-system partition firmware corresponding to the pre-upgrade slot, then the firmware in the non-dual-system partition is restored to the non-dual-system partition firmware corresponding to the pre-upgrade slot.
[0028] In some embodiments, if the current boot slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual-system partition is not the same as the non-dual-system partition firmware corresponding to the pre-upgrade slot, then the non-dual-system partition firmware corresponding to the pre-upgrade slot is written to the corresponding non-dual-system partition according to the current reserved partition and the reserved partition image type of the upgrade metadata, and the upgrade metadata in the preset position of the boot communication partition is cleared.
[0029] At step 110, if the current boot slot is not equal to the pre-upgrade slot information in the upgrade metadata, and the current boot slot is not set as a successful boot flag (such as successful), then the non-dual-system partition upgrade completion flag is read from the boot communication partition, and if the read non-dual-system partition upgrade completion flag is not a preset non-dual-system partition upgrade completion magic number, then the non-dual-system partition firmware is upgraded, and a preset non-dual-system partition upgrade completion magic number is written in the boot communication partition. In some embodiments, the non-dual-system partition firmware corresponding to the current boot slot is written to the corresponding non-dual-system partition according to the current reserved partition and the reserved partition image type of the upgrade metadata.
[0030] At step 112, the Android system is normally booted according to a preset procedure.
[0031] In some embodiments, before step 108, the method 100 can further include: checking the legality of the upgrade metadata in the preset location of the boot communication partition, and normally booting the Android system according to a preset procedure if the checking result is illegal. In some embodiments, the upgrade metadata is read from the preset location of the boot communication partition, and it is determined whether the upgrade metadata magic number of the upgrade metadata is a preset upgrade metadata magic number. If not, it is determined that the checking result is illegal. If yes, a digital digest value of data in an address space composed of the upgrade instruction, the reserved partition image type, the pre-upgrade slot information and the non-dual system partition image information list in the upgrade metadata is calculated according to the digital digest algorithm type of the upgrade metadata, and the calculated digital digest value is compared with the metadata digital digest value of the upgrade metadata. If the comparison result is inconsistent, it is determined that the checking result is illegal. If the comparison result is consistent, it is determined whether the upgrade instruction in the upgrade metadata is a preset non-dual system partition image upgrade instruction. If not, it is determined that the checking result is illegal. If yes, it is determined that the checking result is legal.
[0032] In this way, by checking the legality of the upgrade metadata, the security of the entire upgrade process is ensured.
[0033] In some embodiments, before step 108, the method 100 can further include: reading the Android dual system upgrade start control information from the boot communication partition, and obtaining the current start slot from the Android dual system upgrade start control information. In some embodiments, the method 100 can further include: if the current start slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual system partition is the same as the non-dual system partition firmware corresponding to the pre-upgrade slot, the upgrade metadata in the preset location of the boot communication partition is cleared, and the Android system is normally booted according to a preset procedure.
[0034] In this way, if an accident (such as power failure) occurs during the upgrade of the A / B firmware, since the A / B upgrade is not completed, the A / B upgrade engine in the Android system will not set the system to start from the upgraded slot, and when the system is powered on again, since the current start slot is equal to the pre-upgrade slot information in the upgrade metadata in the boot program firmware, and the firmware in the non-A / B partition is the same as the non-A / B partition firmware corresponding to the pre-upgrade slot, the upgrade metadata in the preset location of the boot communication partition is cleared at this time, and the Android system is normally booted according to a preset procedure. In this way, after entering the Android system, the upgrade can be restarted as needed to ensure the reliability of the upgrade.
[0035] In some embodiments, the method 100 can further include: if the read non-dual system partition upgrade completion flag is a preset non-dual system partition upgrade completion magic number, booting the Android system normally according to a preset procedure.
[0036] In this way, if an accident occurs (such as power failure) during the upgrade of the non-A / B firmware, since the boot loader firmware itself is A / B, and the upgrade of the non-A / B firmware has been completed by the A / B upgrade engine before the upgrade of the non-A / B firmware, and the upgrade of the boot loader firmware is started before accessing the content of the non-A / B partition firmware, the boot loader firmware will not crash due to the dependency between the boot loader firmware and the non-A / B partition firmware, and the boot loader firmware itself can always be started after the system is powered on again, so that the upgrade of the non-A / B firmware can continue after the system is powered on again; the boot loader firmware only boots the device into the upgraded Android system according to the preset procedure after the upgrade of the non-A / B firmware is completed, thereby ensuring the compatibility between the A / B firmware and the non-A / B firmware, and avoiding the situation that the upgraded A / B firmware cannot be started due to incompatibility with the old non-A / B firmware.
[0037] In some embodiments, the method 100 can further include: if the current boot slot is not equal to the pre-upgrade slot information in the upgrade metadata, and the current boot slot has been set as a successful boot flag, clearing the non-dual system partition upgrade completion flag and the upgrade metadata at a preset position in the boot communication partition, and booting the Android system normally according to a preset procedure.
[0038] In this way, if the upgrade is completed normally, but the upgraded A / B firmware itself has a problem, that is, the firmware slot after the upgrade cannot be set as a successful boot flag by the A / B upgrade engine, after the system is restarted for a preset number of times, the boot loader firmware started according to the A / B upgrade specification will return the current boot slot to the pre-upgrade slot, so that the old A / B firmware before the upgrade is used, at this time the current boot slot is equal to the pre-upgrade slot information, and the firmware in the non-A / B partition is different from the non-A / B partition firmware corresponding to the pre-upgrade slot, the non-A / B partition firmware corresponding to the pre-upgrade slot is written into the corresponding non-A / B partition according to the reserved partition and the reserved partition image type of the upgrade metadata, thereby ensuring the compatibility between the A / B firmware and the non-A / B firmware, avoiding the situation that the system cannot be started due to incompatibility between the old A / B firmware before the upgrade and the new non-A / B firmware, and finally clearing the upgrade metadata at a preset position in the boot communication partition, and booting the device into the Android system before the upgrade according to a preset procedure, so that the upgrade can be restarted as needed after entering the Android system, thereby effectively improving the reliability of the upgrade.
[0039] In the following, application scenarios of the method and the device for upgrading firmware according to embodiments of the present application will be described by examples.
[0040] Figure 2 is a flow chart illustrating the method for upgrading firmware according to embodiments of the present application, comprising the following steps 201 to 215.
[0041] In step 201, an A / B form of reserved partition (such as reserved_a and reserverd_b) is added in the partition table. In some embodiments, the firmware in the reserved partition can be upgraded by the A / B upgrade engine.
[0042] In step 202, different types of reserved partition firmware image files are generated according to the configuration item of whether the boot loader firmware supports file system, specifically comprising the following steps (1) to (3).
[0043] (1) Obtain the configuration item of whether the boot loader firmware supports file system.
[0044] (2) If the configuration item indicates that the boot loader firmware supports file system, generate a reserved partition firmware image file with a preset file system, which contains all the non-A / B partition image files to be upgraded under a preset directory structure, and for each non-A / B partition image file to be upgraded, the partition name corresponding to the image file is used as the prefix of the image file name.
[0045] For example, the prefix "param" of the non-A / B partition image file param.img is the partition name param corresponding to the non-A / B partition image file to be upgraded, that is, the image file name corresponding to the param partition is param.img.
[0046] (3) If the configuration item indicates that the boot loader firmware does not support file system, generate a reserved partition firmware image file without a preset file system, which is a raw image file composed of a file header of a preset size and a file body of a preset structure, the file body contains all the non-A / B partition image file data to be upgraded, and the file header is composed of a reserved image magic number, a digital digest algorithm type, an image file information digital digest, and an image file information item list, wherein the image file information item list is a list composed of image file information items, each image file information item is composed of an image name, an image data offset address, and an image data length, and specifically comprising the following steps (3-1) to (3-5).
[0047] (3-1) Set the reserved image magic number to a preset reserved image magic number, and set the digital digest algorithm type to a preset digital digest algorithm type.
[0048] (3-2) For each non-A / B partition image file to be upgraded, first set the image file information item in the image file information item list, and then save the non-A / B partition image file data to the corresponding file body address space of the reserved partition firmware image file (i.e. the address space composed of the image data offset address and the image data length of the corresponding image file information item).
[0049] In some embodiments, setting the image file information item in the image file information item list comprises:
[0050] Setting the image name in the image file information item as the partition name corresponding to the non-A / B partition image file; setting the image data offset address in the image file information item as the starting position of the non-A / B partition image file data in the file body; and setting the image data length in the image file information item as the size of the non-A / B partition image file.
[0051] (3-3) Calculate the digital digest of the image file information item list according to the digital digest algorithm type, and take it as the image file information digital digest.
[0052] (3-4) Generate the file header of the reserved partition firmware image file without the preset file system according to the reserved image magic number, the digital digest algorithm type, the image file information digital digest, and the image file information item list.
[0053] (3-5) Generate the file body of the reserved partition firmware image file without the preset file system with all the non-A / B partition image file data to be upgraded.
[0054] In step 203, an A / B (dual system) upgrade package is constructed, and the reserved partition firmware image file is added to the A / B upgrade package. The A / B upgrade package includes the reserved partition firmware image file, and the reserved partition firmware image file contains the non-A / B partition image file to be upgraded.
[0055] In step 204, the non-A / B partition upgrade completion marker at the preset position in the boot communication partition is cleared.
[0056] In step 205, the upgrade metadata magic number, the digital digest algorithm type, the metadata digital digest value, the upgrade instruction, the reserved partition image type, the pre-upgrade slot information, and the non-A / B partition image information list are determined. The non-A / B partition image information item list is a list composed of non-A / B partition image information items, and each non-A / B partition image information item is composed of an image name and an image digest value, and specifically includes the following steps (1) to (9).
[0057] (1) Set the upgrade metadata magic number to the preset upgrade metadata magic number.
[0058] (2) Set the digital digest algorithm type to the preset digital digest algorithm type.
[0059] (3) Set the upgrade instruction to the preset non-A / B partition image upgrade instruction.
[0060] (4) Obtain the current A / B slot information (i.e. the slot information before upgrade, a or b), and save the current A / B slot information to the slot information before upgrade.
[0061] (5) According to the current A / B slot information, take the current A / B slot partition corresponding to the reserved partition as the current reserved partition (for example, if the current A / B slot information is a, then reserved_a is the current reserved partition), and read the data of the preset size from the start position of the current reserved partition as the image file header.
[0062] (6) Judge whether the reserved image magic number in the image file header is the preset reserved image magic number, if yes, set the reserved partition image type to type one, and execute step (7), if not, set the reserved partition image type to type two, and execute step (8).
[0063] (7) Set the non-A / B partition image information list according to the image file header, which specifically includes the following steps (7-1) to (7-2).
[0064] (7-1) Calculate the digital digest of the image file information item list in the image file header according to the digital digest algorithm type in the image file header, and compare the calculated digital digest with the image file information digital digest in the image file header, if they are inconsistent, prompt an error and exit the upgrade process, if not, execute step (7-2).
[0065] (7-2) Traverse the image file information item list in the image file header, and for the traversed image file information item, execute steps (7-2-1) to (7-2-4).
[0066] (7-2-1) Determine the non-A / B partition block device node (such as / dev / block / by-name / param) corresponding to the image name (such as param) according to the corresponding image file information item; read all the contents in the determined corresponding non-A / B partition block device node as the corresponding image data; calculate the digital digest value of the corresponding image data according to the digital digest algorithm type of the upgrade metadata, and take the calculated digital digest value as the corresponding non-A / B partition image digital digest.
[0067] (7-2-2) According to the address space constituted by the mirror data offset address and the mirror data length of the corresponding mirror file information item, read the data with the mirror data length of the corresponding mirror file information item as the mirror file data at the mirror data offset address of the corresponding mirror file information item from the current reserved partition; calculate the digital digest value of the mirror file data according to the digital digest algorithm type of the upgrade metadata, and compare the calculated digital digest value with the corresponding non-A / B partition mirror digital digest, if they are inconsistent, prompt an error, exit the upgrade process, if not, execute steps (7-2-3) to (7-2-4).
[0068] (7-2-3) In order, acquire the unused non-A / B partition mirror information item in the non-A / B partition mirror information list in the upgrade metadata as the current non-A / B partition mirror information item; set the mirror name of the current non-A / B partition mirror information item as the mirror name of the corresponding mirror file information item; set the mirror digest value of the current non-A / B partition mirror information item as the corresponding non-A / B partition mirror digital digest calculated above.
[0069] (7-2-4) If the corresponding mirror file information item is the last item in the mirror file information item list, execute step (9), otherwise continue to traverse the next item in the mirror file information item list.
[0070] (8) Set the non-A / B partition mirror information list according to all non-A / B partition mirror files to be upgraded under the preset directory structure of the reserved partition, specifically including the following steps (8-1) to (8-2).
[0071] (8-1) Mount the current reserved partition in read-only mode according to the preset file system type of the reserved partition firmware mirror file.
[0072] (8-2) Traverse all non-A / B partition mirror files to be upgraded under the preset directory structure of the current reserved partition, and for each non-A / B partition mirror file traversed, execute the following steps (8-2-1) to (8-2-4).
[0073] (8-2-1) Calculate the digital digest value of the corresponding non-A / B partition mirror file according to the digital digest algorithm type of the upgrade metadata, and take the calculated digital digest value as the mirror file digital digest.
[0074] (8-2-2) Determine the partition name of the corresponding non-A / B partition according to the file name prefix of the corresponding non-A / B partition mirror file, and determine the non-A / B partition block device node corresponding to the non-A / B partition mirror file according to the partition name of the corresponding non-A / B partition.
[0075] read all the contents in the determined corresponding non-A / B partition block device node as corresponding non-A / B partition image data, calculate a digital digest value of the non-A / B partition image data according to the digital digest algorithm type of the upgrade metadata, compare the calculated digital digest value with the image file digital digest, if they are inconsistent, prompt an error, exit the upgrade process, if not, perform the following steps (8-2-3) to (8-2-4).
[0076] (8-2-3) sequentially obtain an unused non-A / B partition image information item in the non-A / B partition image information list in the upgrade metadata as a current non-A / B partition image information item; set the image name of the current non-A / B partition image information item as the partition name of the corresponding non-A / B partition; set the image digest value of the current non-A / B partition image information item as the corresponding image file digital digest calculated above.
[0077] (8-2-4) if the non-A / B partition image file is the last non-A / B partition image file under the preset directory structure of the current reserved partition, perform step (9), otherwise continue to traverse the next non-A / B partition image file under the preset directory structure.
[0078] (9) calculate a digital digest value of the data in the address space composed of the upgrade instruction, the reserved partition image type, the pre-upgrade slot information, and the non-A / B partition image information list according to the digital digest algorithm type, and take the calculated digital digest value as the metadata digital digest value.
[0079] In step 206, generate the upgrade metadata according to the upgrade metadata magic number, the digital digest algorithm type, the metadata digital digest value, the upgrade instruction, the reserved partition image type, the pre-upgrade slot information, and the non-A / B partition image information list.
[0080] In step 207, write the upgrade metadata to the preset position of the boot communication partition.
[0081] In step 208, call the A / B upgrade engine to perform A / B upgrade based on the A / B upgrade package, restart the system after the upgrade is completed, and boot the upgraded boot loader firmware.
[0082] In step 209, check the legality of the upgrade metadata in the preset position of the boot communication partition, if the check result is illegal, normally boot the Android system to start according to the preset process, if the check result is legal, perform step 210.
[0083] In some embodiments, checking the legality of the upgrade metadata in the preset position of the boot communication partition includes:
[0084] (1) reading the upgrade metadata from the preset location of the boot communication partition, and determining whether the upgrade metadata magic number of the upgrade metadata is the preset upgrade metadata magic number, if not, determining that the checking result is illegal, and if yes, calculating the digital digest value of the data in the address space composed of the upgrade instruction, the reserved partition image type, the pre-upgrade slot information and the non-A / B partition image information list in the upgrade metadata according to the digital digest algorithm type of the upgrade metadata.
[0085] (2) comparing the calculated digital digest value with the metadata digital digest value of the upgrade metadata, if the comparison result is inconsistent, determining that the checking result is illegal, and if the comparison result is consistent, determining whether the upgrade instruction in the upgrade metadata is the preset non-A / B partition image upgrade instruction, if not, determining that the checking result is illegal, and if yes, determining that the checking result is legal.
[0086] In step 210, the Android A / B upgrade start control information is read from the boot communication partition, and the current start slot is obtained from the Android A / B upgrade start control information.
[0087] In step 211, in the start bootloader firmware, it is determined whether the current start slot is equal to the pre-upgrade slot information in the upgrade metadata, if yes, it is checked whether the firmware in the non-A / B partition is the same as the non-A / B partition firmware corresponding to the pre-upgrade slot, if yes, it is indicated that the recovery is completed, and step 212 is executed, if not, step 213 is executed, and if not, it is determined according to the Android A / B upgrade start control information whether the current start slot has been successfully set with a start flag (such as successful), if not, step 214 is executed, and if yes, step 215 is executed.
[0088] In step 212, the upgrade metadata in the preset location of the boot communication partition is cleared, and the Android system is normally started according to the preset process.
[0089] In step 213, the non-A / B partition firmware corresponding to the pre-upgrade slot is written into the corresponding non-A / B partition according to the reserved partition image type of the current reserved partition and the upgrade metadata, the upgrade metadata in the preset location of the boot communication partition is cleared, and the Android system is normally started according to the preset process.
[0090] In some embodiments, it is checked whether the firmware in the non-A / B partition is the same as the non-A / B partition firmware corresponding to the pre-upgrade slot, if yes, it is indicated that the recovery is completed, and step 212 is executed, if not, step 213 is executed, and the specific steps (1) to (6) are as follows.
[0091] (1) set the recovery completion flag as completed.
[0092] (2) traverse the non-A / B partition mirror image information list in the upgrade metadata, and for each non-A / B partition mirror image information item in the list, perform steps (2-1) to (2-2).
[0093] (2-1) read all data in the partition corresponding to the mirror image name of the non-A / B partition mirror image information item as corresponding non-A / B partition mirror image data, calculate the digital digest value of the non-A / B partition mirror image data according to the digital digest algorithm type of the upgrade metadata, compare the calculated digital digest value with the mirror digest value of the non-A / B partition mirror image information item, if they are inconsistent, set the recovery completion flag as not completed, and perform step (3) below, otherwise, perform step (2-2).
[0094] (2-2) if the non-A / B partition mirror image information item is the last item in the non-A / B partition mirror image information list, perform step (3), otherwise, continue traversing the next item in the non-A / B partition mirror image information list.
[0095] (3) determine whether the recovery completion flag is completed, if so, clear the upgrade metadata at the preset location in the boot communication partition, and normally boot the Android system according to the preset process, if not, perform step (4).
[0096] (4) determine whether the reserved partition mirror image type of the upgrade metadata is type one, if so, perform step (5), otherwise, perform step (6).
[0097] (5) read data of a preset size from the start of the current reserved partition as a mirror file header, and write all non-A / B partition firmware data in the reserved partition to the corresponding non-A / B partition according to the mirror file information item list in the mirror file header, thereby restoring the firmware in the non-A / B partition to the non-A / B partition firmware corresponding to the slot before the upgrade, which includes steps (5-1) to (5-2) as follows.
[0098] (5-1) determine the current A / B slot partition corresponding to the reserved partition according to the pre-upgrade slot information in the upgrade metadata; take the determined current A / B slot partition corresponding to the reserved partition as the current reserved partition, for example, if the pre-upgrade slot information is a, then the determined current A / B slot partition corresponding to the reserved partition is reserved_a, so that reserved_a is the current reserved partition; read data of a preset size from the start of the current reserved partition as a mirror file header, and determine whether the reserved mirror magic number in the mirror file header is a preset reserved mirror magic number, if so, perform step (5-2) below, otherwise, perform step (6).
[0099] (5-2) Traversing the list of image file information items in the image file header, and for each image file information item, performing steps (5-2-1) to (5-2-3).
[0100] (5-2-1) Determining the partition name of the non-A / B partition to be upgraded according to the image name of the image file information item, referred to as the non-A / B partition to be restored.
[0101] (5-2-2) Moving the current reserved partition to the image data offset address of the corresponding image file information item, then reading data with a size of the image data length of the image file information item as image file data, and writing the read image file data to the non-A / B partition to be restored.
[0102] (5-2-3) If the image file information item is the last item in the list of image file information items, clearing the upgrade metadata at the preset position in the boot communication partition, and normally booting the Android system according to the preset process, otherwise, continuing to traverse the next item in the list of image file information items.
[0103] (6) Mounting the current reserved partition in read-only mode according to the preset file system type of the reserved partition firmware image file, and writing all non-A / B partition image files under the preset directory structure of the current reserved partition to the corresponding non-A / B partition, thereby restoring the firmware in the non-A / B partition to the non-A / B partition firmware corresponding to the slot before upgrading, specifically including steps (6-1) to (6-2).
[0104] (6-1) Determining the current A / B slot partition corresponding to the reserved partition according to the pre-upgrade slot information in the upgrade metadata; taking the determined current A / B slot partition corresponding to the reserved partition as the current reserved partition, for example, the pre-upgrade slot information is a, then the determined current A / B slot partition corresponding to the reserved partition is reserved_a, so that reserved_a is the current reserved partition; mounting the current reserved partition in read-only mode according to the preset file system type of the reserved partition firmware image file.
[0105] (6-2) Traversing all non-A / B partition image files under the preset directory structure of the current reserved partition, and for each non-A / B partition image file, performing steps (6-2-1) to (6-2-3).
[0106] (6-2-1) Determining the partition name of the non-A / B partition corresponding to the non-A / B partition image file according to the file name prefix of the corresponding non-A / B partition image file, referred to as the non-A / B partition to be restored.
[0107] (6-2-2) reading all data in the corresponding non-A / B partition image file as image file data, and writing the read image file data to the non-A / B partition to be recovered.
[0108] (6-2-3) if the non-A / B partition image file is the last non-A / B partition image file under the preset directory structure of the current reserved partition, clearing the upgrade metadata at the preset position in the boot communication partition, and normally booting the Android system according to the preset process, otherwise, continuing to traverse the next non-A / B partition image file under the preset directory structure.
[0109] In step 214, the non-A / B partition upgrade completion flag is read from the boot communication partition; if the read non-A / B partition upgrade completion flag is not the preset non-A / B partition upgrade completion magic number, the non-A / B partition firmware corresponding to the current boot slot is written to the corresponding non-A / B partition according to the reserved partition image type of the current reserved partition and the upgrade metadata, and after the preset non-A / B partition upgrade completion magic number is written in the boot communication partition, the Android system is normally booted according to the preset process; if the read non-A / B partition upgrade completion flag is the preset non-A / B partition upgrade completion magic number, the Android system is normally booted according to the preset process.
[0110] In step 215, the non-A / B partition upgrade completion flag and the upgrade metadata at the preset position in the boot communication partition are cleared, and the Android system is normally booted according to the preset process.
[0111] In some embodiments, according to the Android A / B upgrade startup control information, it is queried whether the current boot slot has been set successfully (such as successful), if not, step 214 is executed, if yes, step 215 is executed, which specifically includes steps (a) to (e).
[0112] (a) according to the Android A / B upgrade startup control information, it is queried whether the current boot slot has been set successfully (such as successful), if yes, the non-A / B partition upgrade completion flag and the upgrade metadata at the preset position in the boot communication partition are cleared, and the Android system is normally booted according to the preset process, if not, step (b) is executed.
[0113] (b) reading the non-A / B partition upgrade completion flag from the preset position corresponding to the preset non-A / B partition upgrade completion flag in the boot communication partition, judging whether the read non-A / B partition upgrade completion flag is the preset non-A / B partition upgrade completion magic number, if yes, the Android system is normally booted according to the preset process, if not, step (c) is executed.
[0114] (c) judging whether the reserved partition image type of the upgrade metadata is type one, if yes, executing step (d), if not, executing step (e).
[0115] (d) reading data of a preset size from a starting position of the current reserved partition as a mirror file header, writing all non-A / B partition firmware data in the reserved partition into corresponding non-A / B partitions according to a mirror file information item list in the mirror file header, thereby completing the upgrade of the non-A / B partition firmware, and writing a preset non-A / B partition upgrade completion magic number at a preset position in the boot communication partition, and normally booting the Android system according to a preset process, specifically including steps (d-1) to (d-2).
[0116] (d-1) determining a current A / B slot partition corresponding to the reserved partition according to the current startup slot, taking the determined current A / B slot partition corresponding to the reserved partition as the current reserved partition, for example, if the current startup slot is a, the determined current A / B slot partition corresponding to the reserved partition is reserved_a, so that reserved_a is the current reserved partition; reading data of a preset size from a starting position of the current reserved partition as a mirror file header, and judging whether a reserved mirror magic number in the mirror file header is a preset reserved mirror magic number, if yes, executing step (d-2), otherwise executing step (e).
[0117] (d-2) traversing a mirror file information item list in the mirror file header, and for the traversed mirror file information item, executing steps (d-2-1) to (d-2-3).
[0118] (d-2-1) determining a partition name of a non-A / B partition to be upgraded according to a mirror name of the mirror file information item, referred to as a non-A / B partition to be upgraded.
[0119] (d-2-2) shifting the current reserved partition to a mirror data offset address of the mirror file information item, reading data of a mirror data length of the mirror file information item as mirror file data, and writing the read mirror file data into the non-A / B partition to be upgraded.
[0120] (d-2-3) if the mirror file information item is the last item of the mirror file information item list, writing a preset non-A / B partition upgrade completion magic number at a preset position in the boot communication partition, and normally booting the Android system according to a preset process, otherwise, continuing to traverse a next item of the mirror file information item list.
[0121] (e) mounting the current reserved partition in read-only mode according to the preset file system type of the reserved partition firmware image file, and writing all the non-A / B partition image files to be upgraded under the preset directory structure of the current reserved partition into the corresponding non-A / B partition, so as to complete the upgrade of the non-A / B partition firmware, and writing a preset non-A / B partition upgrade completion magic number into a preset position corresponding to a preset non-A / B partition upgrade completion flag in the boot communication partition, and normally booting the Android system according to a preset procedure, specifically comprising the following steps (e-1) to (e-2).
[0122] (e-1) determining the current A / B slot partition corresponding to the reserved partition according to the current startup slot, taking the determined current A / B slot partition corresponding to the reserved partition as the current reserved partition, for example, if the current startup slot is a, then the determined current A / B slot partition corresponding to the reserved partition is reserved_a, so that reserved_a is the current reserved partition, and mounting the current reserved partition in read-only mode according to the preset file system type of the reserved partition firmware image file.
[0123] (e-2) traversing all the non-A / B partition image files to be upgraded under the preset directory structure of the current reserved partition, and performing steps (e-2-1) to (e-2-3) on the traversed non-A / B partition image file.
[0124] (e-2-1) determining the partition name of the non-A / B partition corresponding to the non-A / B partition image file according to the file name prefix of the non-A / B partition image file, referred to as the non-A / B partition to be upgraded.
[0125] (e-2-2) reading all the data in the non-A / B partition image file as image file data, and then writing the read image file data into the non-A / B partition to be upgraded.
[0126] (e-2-3) if the non-A / B partition image file is the last non-A / B partition image file under the preset directory structure of the current reserved partition, writing a preset non-A / B partition upgrade completion magic number into a preset position in the boot communication partition, and normally booting the Android system according to a preset procedure, otherwise, continuing to traverse the next non-A / B partition image file under the preset directory structure.
[0127] According to another aspect of the present application, Figure 3 is a schematic diagram of an electronic device 300 for upgrading firmware according to an embodiment of the present application. Referring to Figure 3 , the electronic device 300 comprises a memory 302, a processor 304, and an executable program stored on the memory and executable on the processor, and the processor executes the program to enable the electronic device to implement each step of the method for upgrading firmware as described above.
[0128] According to a further aspect of the present application, a computer readable medium is provided. The computer readable medium has stored thereon a computer program which, when executed by a processor, implements the method of upgrading firmware as described above.
[0129] To sum up, the method of upgrading firmware, the electronic device and the storage medium provided by the present application call the dual-system upgrading engine to perform dual-system upgrading based on the obtained dual-system upgrading package, restart the system after the upgrading is completed, and guide the upgraded boot firmware. In the boot firmware, if the current boot slot is equal to the pre-upgrading slot information, and the firmware in the non-dual-system partition is different from the non-dual-system partition firmware corresponding to the pre-upgrading slot, the firmware in the non-dual-system partition is restored to the non-dual-system partition firmware corresponding to the pre-upgrading slot. If the current boot slot is not equal to the pre-upgrading slot information in the upgrading metadata, and the current boot slot is not set as a successful boot mark, the non-dual-system partition firmware is upgraded when the read non-dual-system partition upgrading completion mark is not the preset non-dual-system partition upgrading completion magic number, and the preset non-dual-system partition upgrading completion magic number is written in the boot communication partition. The Android system is normally guided to start according to the preset process. Since the boot firmware only normally guides the device to enter the upgraded Android system according to the preset process after the upgrading of the non-A / B firmware is completed, the compatibility between the A / B firmware and the non-A / B firmware can be ensured, and the situation that the system cannot be started due to the incompatibility between the upgraded A / B firmware and the old non-A / B firmware can be avoided. In addition, when there is a dependent coupling relationship between the A / B partition firmware and the non-A / B partition firmware, the reliability of the firmware upgrading can still be ensured. Whether an exception occurs at any moment during the upgrading of the A / B firmware or the non-A / B firmware, the reliability of the entire upgrading process can be ensured. In addition, the present application can be seamlessly integrated with the existing Android A / B system upgrading process without any modification of the A / B upgrading engine in the Android system.
[0130] The above description is only an embodiment of the present application, and does not limit the patent scope of the present application. Any equivalent transformation or direct or indirect application in the related technical field based on the content of the specification and drawings of the present application is also included in the patent protection scope of the present application.
Claims
1. A method of upgrading firmware, characterized by, The method comprises the following steps: obtaining a dual-system upgrade package, wherein the dual-system upgrade package comprises a reserved partition firmware image file, and the reserved partition firmware image file contains a non-dual-system partition image file to be upgraded; constructing upgrade metadata based on information associated with the non-dual-system partition image file, and writing the upgrade metadata to a preset position of a boot communication partition; calling a dual-system upgrade engine to perform dual-system upgrade based on the dual-system upgrade package, and restarting the system after the upgrade is completed to guide the upgraded boot loader firmware; in the boot loader firmware, if the current boot slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual-system partition is different from the non-dual-system partition firmware corresponding to the pre-upgrade slot, then the firmware in the non-dual-system partition is restored to the non-dual-system partition firmware corresponding to the pre-upgrade slot; if the current boot slot is not equal to the pre-upgrade slot information in the upgrade metadata, and the current boot slot is not set as a successful boot mark, then reading a non-dual-system partition upgrade completion mark from the boot communication partition, if the read non-dual-system partition upgrade completion mark is not a preset non-dual-system partition upgrade completion magic number, then upgrading the non-dual-system partition firmware, and writing a preset non-dual-system partition upgrade completion magic number in the boot communication partition; and normally booting the Android system according to a preset process. The method further comprises the following steps: if the current boot slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual-system partition is the same as the non-dual-system partition firmware corresponding to the pre-upgrade slot, then clearing the upgrade metadata at the preset position in the boot communication partition; and 2. The method of claim 1, wherein, normally booting the Android system according to a preset process. The reserved partition corresponding to the reserved partition firmware image file is added in a partition table, and the reserved partition has a dual-system form. The reserved partition firmware image file is generated according to whether the configuration item of the boot loader firmware supports a file system, and the reserved partition firmware image file is added to the dual-system upgrade package in the process of constructing the dual-system upgrade package. Before obtaining the dual-system upgrade package, the method further comprises the following steps:
3. The method of claim 1, wherein, clearing a non-dual-system partition upgrade completion mark at the preset position in the boot communication partition; and determining the upgrade metadata magic number, the digital digest algorithm type, the metadata digital digest value, the upgrade instruction, the reserved partition image type, the pre-upgrade slot information, and the non-dual-system partition image information list.
4. The method of claim 1, wherein, The method further comprises the following steps: checking the legality of the upgrade metadata at the preset position in the boot communication partition; and if the checking result is illegal, then normally booting the Android system according to a preset process.
5. The method of claim 1, wherein, The checking of the legality of the upgrade metadata at the preset position in the boot communication partition comprises the following steps: 6. The method of claim 5, wherein, read the upgrade metadata from a preset position of the boot communication partition, and determine whether a magic number of the upgrade metadata is a preset magic number of the upgrade metadata; if not, determine that the checking result is illegal; if yes, calculate a digital digest value of data in an address space composed of an upgrade instruction, a reserved partition image type, pre-upgrade slot information, and a non-dual system partition image information list in the upgrade metadata according to a type of a digital digest algorithm of the upgrade metadata, and compare the calculated digital digest value with a metadata digital digest value of the upgrade metadata; if the comparison result is inconsistent, determine that the checking result is illegal; if the comparison result is consistent, determine whether the upgrade instruction in the upgrade metadata is a preset non-dual system partition image upgrade instruction; if not the preset non-dual system partition image upgrade instruction, determine that the checking result is illegal; and if the preset non-dual system partition image upgrade instruction, determine that the checking result is legal.
7. The method of claim 1, wherein, if the current startup slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual system partition is different from non-dual system partition firmware corresponding to the pre-upgrade slot, restoring the firmware in the non-dual system partition to the non-dual system partition firmware corresponding to the pre-upgrade slot includes: if the current startup slot is equal to the pre-upgrade slot information in the upgrade metadata, and the firmware in the non-dual system partition is different from non-dual system partition firmware corresponding to the pre-upgrade slot, writing the non-dual system partition firmware corresponding to the pre-upgrade slot into the corresponding non-dual system partition according to the current reserved partition and the reserved partition image type of the upgrade metadata; and clearing the upgrade metadata at the preset position in the boot communication partition, and normally booting the Android system according to a preset process.
8. The method of claim 1, wherein, upgrading the non-dual system partition firmware includes: writing the non-dual system partition firmware corresponding to the current startup slot into the corresponding non-dual system partition according to the current reserved partition and the reserved partition image type of the upgrade metadata.
9. The method of claim 1, wherein, Further comprising: if the read non-dual system partition upgrade completion flag is a preset non-dual system partition upgrade completion magic number, normally booting the Android system according to a preset process.
10. The method of claim 1, wherein, Further comprising: if the current startup slot is not equal to the pre-upgrade slot information in the upgrade metadata, and the current startup slot has been set as a successful startup flag, clearing the non-dual system partition upgrade completion flag and the upgrade metadata at the preset position in the boot communication partition, and normally booting the Android system according to a preset process.
11. The method of claim 1, wherein, Further comprising: reading Android dual system upgrade startup control information from the boot communication partition, and obtaining the current startup slot from the Android dual system upgrade startup control information.
12. An electronic device, comprising: including: a memory configured to store an executable program; and a processor configured to execute the program so that the electronic device performs the method according to any one of claims 1 to 11.
13. A computer readable medium having stored thereon a computer program, characterized in that The computer program is executed by the processor to implement the method according to any one of claims 1 to 11. The computer program is executed by the processor to implement the method according to any one of claims 1 to 11.
Citation Information
Patent Citations
FOTA firmware upgrading method and device based on Android system
CN113238771A
Method for upgrading firmware and electronic equipment
CN114706608A