1e41f4b71Sopenharmony_ci# OTA Update 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Introduction 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci### Overview 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciWith the rapid development of device systems, it has become a critical challenge for device vendors to help their users to access system updates in a timely manner to experience the new features and improve the system stability and security 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciOver the Air (OTA) answers this challenge with the support for remote updating of device systems. By providing unified update APIs externally, the update subsystem shields the differences of underlying chips. After secondary development based on the update APIs, device vendors can easily implement remote updating for their devices, such as IP cameras. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci### Basic Concepts 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci- Full package: an update package that packs all images of the target version. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci- Differential package: an update package that packs the differential images between the source version and target version. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci### Principles 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciTo implement an OTA update, you first need to use the packaging tool to pack the build version into an update package and upload the update package to the OTA server. Devices integrating the OTA update capability can then detect the update package and download it to implement a remote update. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci<a href="#ab-update">A/B Update</a>: a typical application of OTA update. In this update mode, a device has a backup system B. When system A is running, system B is updated silently. After the update is successful, the device restarts and switches to the new system. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci### Constraints 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci- Only the open-source suites for devices powered by Hi3861, Hi3516D V300, and RK3568 are supported. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci- Devices developed based on Hi3516D V300 and RK3568 must support the SD card in the Virtual Festival of Aerobatic Teams (VFAT) format. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci- Generation of update packages can only be performed on the Linux system. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci- Currently, the mini and small systems support update using a full package, but not a differential package or an update package with partitions changed. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci- To implement an A/B update, ensure that the devices running the standard system support booting from partitions A and B. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci## Preparations 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci- On Windows, download and install the OpenSSL tool and configure environment variables. 44e41f4b71Sopenharmony_ci- Prepare the packaging tool. 45e41f4b71Sopenharmony_ci- Build version images using the packaging tool. 46e41f4b71Sopenharmony_ci- Copy the **diff** file in **out/rk3568/clang_x64/updater/updater/** and the **libext2fs.so**, **e2fsdriod**, **libext2_com_err.so**, and **libext2_misc.so** files in **out/rk3568/clang_x64/thirdparty/e2fsprogs** to the **packaging_tools/lib/** directory of the packaging tool. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci## How to Develop 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci<a href="#generating-a-public/private-key-pair">1. Use the OpenSSL tool to generate a public/private key pair. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci<a href="#making-an-update-package">2. Use the packaging tool to generate an update package.</a> 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci  <a href="#mini-and-small systems">2.1 Mini and small systems</a> 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci  <a href="#standard system">2.2 Standard system</a> 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci<a href="#uploading-the-update package">3. Upload the update package to the vendor's OTA server.</a> 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci<a href="#downloading-the-update-package">4. Download the update package from the vendor's OTA server.</a> 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci<a href="#integrating-the-OTA-update-capability">5. Integrate the OTA update capability. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci  <a href="#api-application-scenario-default">5.1 API application scenario (default)</a> 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci  <a href="#api-application-scenario-custom">5.2 API application scenario (custom)</a> 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci  <a href="#ab-update-scenario">5.3 A/B update scenario</a> 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci## How to Develop 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci### Generating a Public/Private Key Pair 76e41f4b71Sopenharmony_ci1. Use the OpenSSL tool to generate a public-private key pair. Place the **signing_cert.crt** file in the **packaging_tools/sign_cert/** directory of the packaging tool. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci3. Keep the private key file properly as this file stores the private key used to sign the update package. You need to specify the private key file in the command used for preparing the update package. The public key is used to verify the signature of the update package during the update. For the mini and small systems, the generated public key is preset in the code. The vendor needs to implement the **HotaHalGetPubKey** API to obtain the key. For the standard system, the generated public key is stored in the **/hisilicon/hi3516dv300/build/updater\_config/signing\_cert.crt** file in the **device** or **vendor** directory. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci5. For the mini and small systems that use the Hi3516D V300 suite, use the content in **third\_party\u-boot\u-boot-2020.01\product\hiupdate\verify\update\_public\_key.c** file of the U-Boot module. 81e41f4b71Sopenharmony_ci Example configuration: 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci ```c 84e41f4b71Sopenharmony_ci static unsigned char g_pub_key[] = { 85e41f4b71Sopenharmony_ci 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 86e41f4b71Sopenharmony_ci 0x00, 0xBF, 0xAA, 0xA5, 0xB3, 0xC2, 0x78, 0x5E, 87e41f4b71Sopenharmony_ci } 88e41f4b71Sopenharmony_ci ``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci### Making an Update Package 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci#### Mini and Small Systems 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci1. Create the **target\_package** folder with the following directory structure: 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci OTA.tag and config are not required for lightweight systems and small systems upgraded from AB. 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci ```text 101e41f4b71Sopenharmony_ci target_package 102e41f4b71Sopenharmony_ci ├── OTA.tag 103e41f4b71Sopenharmony_ci ├── config 104e41f4b71Sopenharmony_ci ├── {component_1} 105e41f4b71Sopenharmony_ci ├── {component_2} 106e41f4b71Sopenharmony_ci ├── ...... 107e41f4b71Sopenharmony_ci ├── {component_N} 108e41f4b71Sopenharmony_ci └── updater_config 109e41f4b71Sopenharmony_ci └── updater_specified_config.xml 110e41f4b71Sopenharmony_ci ``` 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci2. Place the components to be updated, including the image file (for example, **rootfs.img**), as the substitute of **{component\_N}** in the root directory of the **target\_package** folder. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci3. Configure the **updater\_specified\_config.xml** file in the **updater_config** folder. 115e41f4b71Sopenharmony_ci Example configuration: 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci ```xml 119e41f4b71Sopenharmony_ci <?xml version="1.0"?> 120e41f4b71Sopenharmony_ci <package> 121e41f4b71Sopenharmony_ci <head name="Component header information"> 122e41f4b71Sopenharmony_ci <info fileVersion="02" prdID="hisi" softVersion="OpenHarmony x.x" date="202x.xx.xx" time="xx:xx:xx">head info</info> 123e41f4b71Sopenharmony_ci </head> 124e41f4b71Sopenharmony_ci <group name="Component information"> 125e41f4b71Sopenharmony_ci <component compAddr="ota_tag" compId="27" resType="5" compType="0" compVer="1.0">./OTA.tag</component> 126e41f4b71Sopenharmony_ci <component compAddr="config" compId="23" resType="5" compType="0" compVer="1.0">./config</component> 127e41f4b71Sopenharmony_ci <component compAddr="bootloader" compId="24" resType="5" compType="0" compVer="1.0">./u-boot-xxxx.bin</component> 128e41f4b71Sopenharmony_ci </group> 129e41f4b71Sopenharmony_ci </package> 130e41f4b71Sopenharmony_ci ``` 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci **Table 1** Description of nodes in the component configuration file 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci | Type| Node Name| Node Label| Mandatory| Description| 135e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | -------- | 136e41f4b71Sopenharmony_ci | Header information (head node)| info| / | Yes| Head node information. Set this field to **head info**.| 137e41f4b71Sopenharmony_ci | Header information (head node)| info| fileVersion | Yes| Verification mode of the **update.bin** file. Set this field to **02**.| 138e41f4b71Sopenharmony_ci | Header information (head node)| info| prdID | Yes| Reserved. This field does not affect the generation of the update package.| 139e41f4b71Sopenharmony_ci | Header information (head node)| info| softVersion | Yes| Software version number, that is, the version number of the update package. Ensure that the version number is later than the basic version number and OpenHarmony is not followed by any other letter. Otherwise, the update cannot be performed.| 140e41f4b71Sopenharmony_ci | Header information (head node)| info| _date_ | Yes| Date when the update package is generated. This field is reserved and does not affect the generation of the update package.| 141e41f4b71Sopenharmony_ci | Header information (head node)| info| _time_ | Yes| Time when the update package is generated. This field is reserved and does not affect the generation of the update package.| 142e41f4b71Sopenharmony_ci | Component information (group node)| component| / | Yes| Content of this node: path of the component or image file to be packed into the update package. It is the root directory of the version package by default.| 143e41f4b71Sopenharmony_ci | Component information (group node)| component| compAddr | Yes| Name of the partition corresponding to the component, for example, **system** or **vendor**.| 144e41f4b71Sopenharmony_ci | Component information (group node)| component| compId | Yes| Component ID, which must be unique.| 145e41f4b71Sopenharmony_ci | Component information (group node)| component| resType | Yes| Reserved. This field does not affect the generation of the update package.| 146e41f4b71Sopenharmony_ci | Component information (group node)| component| compType | Yes| Image type, which can be a full or differential package. The value **0** indicates a full package, and value **1** indicates a differential package.| 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci > **NOTE** 149e41f4b71Sopenharmony_ci > As mini and small systems do not support updating with a differential package, **compType** must be set to **0**, other than **1**. 150e41f4b71Sopenharmony_ci > 151e41f4b71Sopenharmony_ci > For mini and small systems, an update package cannot be created by changing partitions. 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci4. Create the **OTA.tag** file, which contains the magic number of the update package. The magic number is fixed, as shown below: 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci ```text 156e41f4b71Sopenharmony_ci package_type:ota1234567890qwertw 157e41f4b71Sopenharmony_ci ``` 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci5. Create the **config** file, and configure the **bootargs** and **bootcmd** information in the file. 160e41f4b71Sopenharmony_ci Example configuration: 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci ```text 164e41f4b71Sopenharmony_ci setenv bootargs 'mem=128M console=ttyAMA0,115200 root=/dev/mmcblk0p3 rw rootfstype=ext4 rootwait blkdevparts=mmcblk0:1M 165e41f4b71Sopenharmony_ci (u-boot.bin),9M(kernel.bin),50M(rootfs_ext4.img),50M(userfs.img)' setenv bootcmd 'mmc read 0x0 0x82000000 0x800 0x4800;bootm 0x82000000' 166e41f4b71Sopenharmony_ci ``` 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci6. Generate the update package. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci ```text 171e41f4b71Sopenharmony_ci python build_update.py ./target_package/ ./output_package/ -pk ./rsa_private_key3072.pem -nz -nl2 172e41f4b71Sopenharmony_ci ``` 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci - **./target\_package/**: path of **target\_package** 175e41f4b71Sopenharmony_ci - **./output\_package/**: output path of the update package 176e41f4b71Sopenharmony_ci - -**pk ./rsa\_private\_key3072.pem**: path of the private key file 177e41f4b71Sopenharmony_ci - -**nz**: **not zip** mode 178e41f4b71Sopenharmony_ci - -**nl2**: non-standard system mode 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci#### Standard System 182e41f4b71Sopenharmony_ci1. Create the **target\_package** folder with the following directory structure: 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci ```text 186e41f4b71Sopenharmony_ci target_package 187e41f4b71Sopenharmony_ci ├── {component_1} 188e41f4b71Sopenharmony_ci ├── {component_2} 189e41f4b71Sopenharmony_ci ├── ...... 190e41f4b71Sopenharmony_ci ├── {component_N} 191e41f4b71Sopenharmony_ci └── updater_config 192e41f4b71Sopenharmony_ci ├── BOARD.list 193e41f4b71Sopenharmony_ci ├── VERSION.mbn 194e41f4b71Sopenharmony_ci └── updater_specified_config.xml 195e41f4b71Sopenharmony_ci ``` 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci2. Place the components to be updated, including their image file and **updater_binary** file, as the substitute of **{component\_N}** in the root directory of the **target\_package** folder. 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci Take RK3568 as an example. Place the image file in the following build directory: out/rk3568/packages/phone/images/. 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci Place the **updater_binary** file in the following build directory: out/rk3568/packages/phone/system/bin/. 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci3. Configure the component configuration file in the **updater\_config** folder. 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci4. Configure the list of products supported by the current update package in **BOARD.list** in the **updater\_config** folder. 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci Example configuration: 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci ```text 211e41f4b71Sopenharmony_ci HI3516 212e41f4b71Sopenharmony_ci RK3568 213e41f4b71Sopenharmony_ci ``` 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci Vendors can configure **Local BoardId** by calling **GetLocalBoardId()** in the **base/updater/updater/utils/utils.cpp** file. Ensure that **Local BoardId** configured in the **utils.cpp** file is present in **BOARD.list**. Otherwise, the update cannot be performed. 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci5. Configure the versions supported by the current update package in **VERSION.mbn** in the **updater\_config** folder. 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci Version number format: Hi3516DV300-eng 10 QP1A.XXXXXX.{Major version number (6 digits)}.XXX{Minor version number (3 digits)} 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci For example, **Hi3516DV300-eng 10 QP1A.190711.020**, where **190711** is the major version number, and **020** is the minor version number. 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci Example configuration: 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci ```text 227e41f4b71Sopenharmony_ci Hi3516DV300-eng 10 QP1A.190711.001 228e41f4b71Sopenharmony_ci Hi3516DV300-eng 10 QP1A.190711.020 229e41f4b71Sopenharmony_ci ``` 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci Ensure that the basic version number is contained in **VERSION.mbn**. 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci6. For update using an incremental (differential) package, also prepare a source version package (source\_package) in the same format as the target version package (target\_package), and then compress it as a **.zip** file, that is, **source\_package.zip**. 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci7. If you create an update package with partitions changed, also provide the partition table file named **partition\_file.xml**. You can specify the file using the **-pf** parameter. For details about the configuration nodes, see the description below. 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci The partition table is generated with the image. The format is as follows: 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci ```xml 241e41f4b71Sopenharmony_ci <?xml version="1.0" encoding="GB2312" ?> 242e41f4b71Sopenharmony_ci <Partition_Info> 243e41f4b71Sopenharmony_ci <Part Sel="1" PartitionName="Image 1" FlashType="Flash type" FileSystem="File system type" Start="Start address of the partition" Length="Size of the partition" SelectFile="Actual path of the image"/> 244e41f4b71Sopenharmony_ci <Part Sel="1" PartitionName="Image 2" FlashType="Flash type" FileSystem="File system type" Start="Start address of the partition" Length="Size of the partition" SelectFile="Actual path of the image"/> 245e41f4b71Sopenharmony_ci </Partition_Info> 246e41f4b71Sopenharmony_ci ``` 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci **Table 2** Description of labels in the partition table 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci | Name| Description| 251e41f4b71Sopenharmony_ci | -------- | -------- | 252e41f4b71Sopenharmony_ci | Sel | Whether the partition is effective. The value **1** indicates that the partition is effective, and value **0** indicates the opposite.| 253e41f4b71Sopenharmony_ci | PartitionName | Partition name, for example, **fastboot** or **boot**.| 254e41f4b71Sopenharmony_ci | FlashType | Flash type, for example, **emmc** or **ufs**.| 255e41f4b71Sopenharmony_ci | FileSystem | File system type, for example, **ext3/4** or **f2fs**. The value can also be **none**.| 256e41f4b71Sopenharmony_ci | Start | Start address of the partition, in MB. The start address of all partitions is **0**.| 257e41f4b71Sopenharmony_ci | Length | Size of the partition, in MB.| 258e41f4b71Sopenharmony_ci | SelectFile | Actual path of the image or file.| 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci8. Generate the update package. 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci **Full package** 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci Run the following command: 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci ```text 268e41f4b71Sopenharmony_ci python build_update.py ./target_package/ ./output_package/ -pk ./rsa_private_key2048.pem 269e41f4b71Sopenharmony_ci ``` 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci - **./target\_package/**: path of **target\_package** 272e41f4b71Sopenharmony_ci - **./output\_package/**: output path of the update package 273e41f4b71Sopenharmony_ci - -**pk ./rsa\_private\_key2048.pem**: path of the private key file 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci **Incremental (differential) package** 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci Run the following command: 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci ```text 281e41f4b71Sopenharmony_ci python build_update.py ./target_package/ ./output_package/ -s ./source_package.zip -pk ./rsa_private_key2048.pem 282e41f4b71Sopenharmony_ci ``` 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci - **./target\_package/**: path of **target\_package** 285e41f4b71Sopenharmony_ci - **./output\_package/**: output path of the update package 286e41f4b71Sopenharmony_ci - -**s ./source\_package.zip**: path of the **source\_package.zip** file. For update using a differential package, use the **-s** parameter to specify the source version package. 287e41f4b71Sopenharmony_ci - -**pk ./rsa\_private\_key2048.pem**: path of the private key file 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci **Update package with partitions changed** 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci Run the following command: 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci ```text 295e41f4b71Sopenharmony_ci python build_update.py ./target_package/ ./output_package/ -pk ./rsa_private_key2048.pem -pf ./partition_file.xml 296e41f4b71Sopenharmony_ci ``` 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci - **./target\_package/**: path of **target\_package** 299e41f4b71Sopenharmony_ci - **./output\_package/**: output path of the update package 300e41f4b71Sopenharmony_ci - -**pk ./rsa\_private\_key2048.pem**: path of the private key file 301e41f4b71Sopenharmony_ci - -**pf ./partition\_file.xml**: path of the partition table file 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci### Uploading the Update Package 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ciUpload the update package to the vendor's OTA server. 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci### **Downloading the Update Package** 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci1. Download the update package from the OTA server. 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci2. (Optional) Insert an SD card (with a capacity greater than 100 MB) if the device is developed based on Hi3516D V300. 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci### Integrating OTA Update Capabilities 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci1. For mini and small systems 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci - Call the dynamic library **libhota.so**. The corresponding header files **hota\_partition.h** and **hota\_updater.h** are located in **base\update\sys\_installer\_lite\interfaces\kits\**. 321e41f4b71Sopenharmony_ci - The **libhota.so** source code is located in **base\update\sys\_installer\_lite\frameworks\source**. 322e41f4b71Sopenharmony_ci - For details about how to use APIs, see *API Application Scenarios* and update APIs in *API Reference*. 323e41f4b71Sopenharmony_ci - If the development board needs to be adapted, see the **base\update\sys\_installer\_lite\hals\hal\_hota\_board.h** file. 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci2. For the standard system, see the [JS API Reference](../../application-dev/reference/apis/js-apis-update.md) for details. 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci#### API Application Scenario (Default) 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ciThe update package is generated by following the instructions provided in Generating a Public/Private Key Pair and Generating an Update Package. 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci##### **How to Develop** 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci1. Download the update package for the current device, and then call the **HotaInit** function to initialize the OTA module. 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci2. Call the **HotaWrite** function to verify, parse, and write data streams for the update into the device. 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci3. Call the **HotaRestart** function to restart the system for the update to take effect. Call the **HotaCancel** function if you want to cancel the update. 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci##### Sample Code 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci Perform an OTA update using the update package format and verification method provided by OpenHarmony. 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci```cpp 347e41f4b71Sopenharmony_ciint main(int argc, char **argv) 348e41f4b71Sopenharmony_ci{ 349e41f4b71Sopenharmony_ci printf("this is update print!\r\n"); 350e41f4b71Sopenharmony_ci if (HotaInit(NULL, NULL) < 0) { 351e41f4b71Sopenharmony_ci printf("ota update init fail!\r\n"); 352e41f4b71Sopenharmony_ci return -1; 353e41f4b71Sopenharmony_ci } 354e41f4b71Sopenharmony_ci int fd = open(OTA_PKG_FILE, O_RDWR, S_IRUSR | S_IWUSR); 355e41f4b71Sopenharmony_ci if (fd < 0) { 356e41f4b71Sopenharmony_ci printf("file open failed, fd = %d\r\n", fd); 357e41f4b71Sopenharmony_ci (void)HotaCancel(); 358e41f4b71Sopenharmony_ci return -1; 359e41f4b71Sopenharmony_ci } 360e41f4b71Sopenharmony_ci int offset = 0; 361e41f4b71Sopenharmony_ci int fileLen = lseek(fd, 0, SEEK_END); 362e41f4b71Sopenharmony_ci int leftLen = fileLen; 363e41f4b71Sopenharmony_ci while (leftLen > 0) { 364e41f4b71Sopenharmony_ci if (lseek(fd, offset, SEEK_SET) < 0) { 365e41f4b71Sopenharmony_ci close(fd); 366e41f4b71Sopenharmony_ci printf("lseek fail!\r\n"); 367e41f4b71Sopenharmony_ci (void)HotaCancel(); 368e41f4b71Sopenharmony_ci return -1; 369e41f4b71Sopenharmony_ci } 370e41f4b71Sopenharmony_ci int tmpLen = leftLen >= READ_BUF_LEN ? READ_BUF_LEN : leftLen; 371e41f4b71Sopenharmony_ci (void)memset_s(g_readBuf, READ_BUF_LEN, 0, READ_BUF_LEN); 372e41f4b71Sopenharmony_ci if (read(fd, g_readBuf, tmpLen) < 0) { 373e41f4b71Sopenharmony_ci close(fd); 374e41f4b71Sopenharmony_ci printf("read fail!\r\n"); 375e41f4b71Sopenharmony_ci (void)HotaCancel(); 376e41f4b71Sopenharmony_ci return -1; 377e41f4b71Sopenharmony_ci } 378e41f4b71Sopenharmony_ci if (HotaWrite((unsigned char *)g_readBuf, offset, tmpLen) != 0) { 379e41f4b71Sopenharmony_ci printf("ota write fail!\r\n"); 380e41f4b71Sopenharmony_ci close(fd); 381e41f4b71Sopenharmony_ci (void)HotaCancel(); 382e41f4b71Sopenharmony_ci return -1; 383e41f4b71Sopenharmony_ci } 384e41f4b71Sopenharmony_ci offset += READ_BUF_LEN; 385e41f4b71Sopenharmony_ci leftLen -= tmpLen; 386e41f4b71Sopenharmony_ci } 387e41f4b71Sopenharmony_ci close(fd); 388e41f4b71Sopenharmony_ci printf("ota write finish!\r\n"); 389e41f4b71Sopenharmony_ci printf("device will reboot in 10s...\r\n"); 390e41f4b71Sopenharmony_ci sleep(10); 391e41f4b71Sopenharmony_ci (void)HotaRestart(); 392e41f4b71Sopenharmony_ci return 0; 393e41f4b71Sopenharmony_ci} 394e41f4b71Sopenharmony_ci``` 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci#### API Application Scenario (Custom) 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ciThe update package is generated in other ways instead of following the instructions provided in "Generating a Public/Private Key Pair" and "Generating an Update Package." 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci##### **How to Develop** 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci1. Download the update package for the current device, and then call the **HotaInit** function to initialize the OTA module. 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci2. Call the **HotaSetPackageType** function to set the package type to **NOT\_USE\_DEFAULT\_PKG**. 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci3. Call the **HotaWrite** function to write data streams into the device. 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci4. Call the **HotaRead** function to read data. Vendors can choose whether to verify the data. 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci5. (Optional) Call the **HotaSetBootSettings** function to set the startup tag used for entering the U-Boot mode during system restarting. 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci6. Call the **HotaRestart** function to restart the system for the update to take effect. Call the **HotaCancel** function if you want to cancel the update. 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci 417e41f4b71Sopenharmony_ci##### Sample Code 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ci Perform an OTA update using the update package format and verification method not provided by OpenHarmony. 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ci```cpp 422e41f4b71Sopenharmony_ciint main(int argc, char **argv) 423e41f4b71Sopenharmony_ci{ 424e41f4b71Sopenharmony_ci printf("this is update print!\r\n"); 425e41f4b71Sopenharmony_ci if (HotaInit(NULL, NULL) < 0) { 426e41f4b71Sopenharmony_ci printf("ota update init fail!\r\n"); 427e41f4b71Sopenharmony_ci (void)HotaCancel(); 428e41f4b71Sopenharmony_ci return -1; 429e41f4b71Sopenharmony_ci } 430e41f4b71Sopenharmony_ci (void)HotaSetPackageType(NOT_USE_DEFAULT_PKG); 431e41f4b71Sopenharmony_ci int fd = open(OTA_PKG_FILE, O_RDWR, S_IRUSR | S_IWUSR); 432e41f4b71Sopenharmony_ci if (fd < 0) { 433e41f4b71Sopenharmony_ci printf("file open failed, fd = %d\r\n", fd); 434e41f4b71Sopenharmony_ci (void)HotaCancel(); 435e41f4b71Sopenharmony_ci return -1; 436e41f4b71Sopenharmony_ci } 437e41f4b71Sopenharmony_ci int offset = 0; 438e41f4b71Sopenharmony_ci int fileLen = lseek(fd, 0, SEEK_END); 439e41f4b71Sopenharmony_ci int leftLen = fileLen; 440e41f4b71Sopenharmony_ci while (leftLen > 0) { 441e41f4b71Sopenharmony_ci if (lseek(fd, offset, SEEK_SET) < 0) { 442e41f4b71Sopenharmony_ci close(fd); 443e41f4b71Sopenharmony_ci printf("lseek fail!\r\n"); 444e41f4b71Sopenharmony_ci (void)HotaCancel(); 445e41f4b71Sopenharmony_ci return -1; 446e41f4b71Sopenharmony_ci } 447e41f4b71Sopenharmony_ci int tmpLen = leftLen >= READ_BUF_LEN ? READ_BUF_LEN : leftLen; 448e41f4b71Sopenharmony_ci (void)memset_s(g_readBuf, READ_BUF_LEN, 0, READ_BUF_LEN); 449e41f4b71Sopenharmony_ci if (read(fd, g_readBuf, tmpLen) < 0) { 450e41f4b71Sopenharmony_ci close(fd); 451e41f4b71Sopenharmony_ci printf("read fail!\r\n"); 452e41f4b71Sopenharmony_ci (void)HotaCancel(); 453e41f4b71Sopenharmony_ci return -1; 454e41f4b71Sopenharmony_ci } 455e41f4b71Sopenharmony_ci if (HotaWrite((unsigned char *)g_readBuf, offset, tmpLen) != 0) { 456e41f4b71Sopenharmony_ci printf("ota write fail!\r\n"); 457e41f4b71Sopenharmony_ci close(fd); 458e41f4b71Sopenharmony_ci (void)HotaCancel(); 459e41f4b71Sopenharmony_ci return -1; 460e41f4b71Sopenharmony_ci } 461e41f4b71Sopenharmony_ci offset += READ_BUF_LEN; 462e41f4b71Sopenharmony_ci leftLen -= tmpLen; 463e41f4b71Sopenharmony_ci } 464e41f4b71Sopenharmony_ci close(fd); 465e41f4b71Sopenharmony_ci printf("ota write finish!\r\n"); 466e41f4b71Sopenharmony_ci leftLen = fileLen; 467e41f4b71Sopenharmony_ci while (leftLen > 0) { 468e41f4b71Sopenharmony_ci int tmpLen = leftLen >= READ_BUF_LEN ? READ_BUF_LEN : leftLen; 469e41f4b71Sopenharmony_ci (void)memset_s(g_readBuf, READ_BUF_LEN, 0, READ_BUF_LEN); 470e41f4b71Sopenharmony_ci if (HotaRead(offset, READ_BUF_LEN, (unsigned char *)g_readBuf) != 0) { 471e41f4b71Sopenharmony_ci printf("ota write fail!\r\n"); 472e41f4b71Sopenharmony_ci (void)HotaCancel(); 473e41f4b71Sopenharmony_ci return -1; 474e41f4b71Sopenharmony_ci } 475e41f4b71Sopenharmony_ci /* do your verify and parse */ 476e41f4b71Sopenharmony_ci offset += READ_BUF_LEN; 477e41f4b71Sopenharmony_ci leftLen -= tmpLen; 478e41f4b71Sopenharmony_ci } 479e41f4b71Sopenharmony_ci /* set your boot settings */ 480e41f4b71Sopenharmony_ci (void)HotaSetBootSettings(); 481e41f4b71Sopenharmony_ci printf("device will reboot in 10s...\r\n"); 482e41f4b71Sopenharmony_ci sleep(10); 483e41f4b71Sopenharmony_ci (void)HotaRestart(); 484e41f4b71Sopenharmony_ci return 0; 485e41f4b71Sopenharmony_ci} 486e41f4b71Sopenharmony_ci``` 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci##### Upgrading the System 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ciAn application calls APIs of the OTA module to perform functions such as signature verification of the update package, anti-rollback, as well as burning and flushing to disk. After the update is complete, the system automatically restarts. 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ciFor the mini and small systems that use the Hi3516D V300 open source suite, add the value of **LOCAL\_VERSION** to the version that needs to implement the anti-rollback function. For example, for **"ohos default 1.0"->"ohos default 1.1"**, add the value of **LOCAL\_VERSION** in **device\hisilicon\third\_party\uboot\u-boot-2020.01\product\hiupdate\ota\_update\ota\_local_info.c**. 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci Example configuration: 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci```cpp 498e41f4b71Sopenharmony_ciconst char *get_local_version(void) 499e41f4b71Sopenharmony_ci{ 500e41f4b71Sopenharmony_ci#if defined(CONFIG_TARGET_HI3516EV200) || \ 501e41f4b71Sopenharmony_ci defined(CONFIG_TARGET_HI3516DV300) 502e41f4b71Sopenharmony_ci#define LOCAL_VERSION "ohos default 1.0" /* increase: default release version */ 503e41f4b71Sopenharmony_ci``` 504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci#### A/B Update Scenario 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci##### How to Develop 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci1. Download the update package through the update application. 512e41f4b71Sopenharmony_ci2. Invoke update_service to start the system installation service through SAMGR. 513e41f4b71Sopenharmony_ci3. Let the system installation service perform a silent update. 514e41f4b71Sopenharmony_ci4. Activate the new version upon restarting. 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci##### How to Develop 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci- Invoke update_service to call JS APIs to implement the related service logic in an A/B update. 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ci 1. Displaying the update package installation progress: 522e41f4b71Sopenharmony_ci ```cpp 523e41f4b71Sopenharmony_ci on(eventType: "upgradeProgress", callback: UpdateProgressCallback): void; 524e41f4b71Sopenharmony_ci ``` 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci 2. Setting the activation policy (immediate restart, restart at night, and activation on next restart): 527e41f4b71Sopenharmony_ci ```cpp 528e41f4b71Sopenharmony_ci upgrade(apply) 529e41f4b71Sopenharmony_ci ``` 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ci- Invoke update_service to start the system installation service through SAMGR. 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci 1. Start the system installation service and set up an IPC connection. 535e41f4b71Sopenharmony_ci ```cpp 536e41f4b71Sopenharmony_ci int SysInstallerInit(void* callback) 537e41f4b71Sopenharmony_ci ``` 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci 2. Install the A/B update package in the specified path. 540e41f4b71Sopenharmony_ci ```cpp 541e41f4b71Sopenharmony_ci int StartUpdatePackageZip(string path) 542e41f4b71Sopenharmony_ci ``` 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci 3. Set the update progress callback. 545e41f4b71Sopenharmony_ci ```cpp 546e41f4b71Sopenharmony_ci int SetUpdateProgressCallback(void* callback) 547e41f4b71Sopenharmony_ci ``` 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ci 4. Obtain the installation status of the update package (0: not started; 1: installing; 2: installed). 550e41f4b71Sopenharmony_ci ```cpp 551e41f4b71Sopenharmony_ci int GetUpdateStatus() 552e41f4b71Sopenharmony_ci ``` 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci- Use HDI APIs to activate the new version. 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci 1. Obtain the current boot slot to determine the partition to be updated. 558e41f4b71Sopenharmony_ci ```cpp 559e41f4b71Sopenharmony_ci int GetCurrentSlot() 560e41f4b71Sopenharmony_ci ``` 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci 2. Upon completion of the update, switch the updated slot and restart the system for the new version to take effect. 563e41f4b71Sopenharmony_ci ```cpp 564e41f4b71Sopenharmony_ci int SetActiveBootSlot(int slot) 565e41f4b71Sopenharmony_ci ``` 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci 3. Upon starting of the update, set the slot of the partition to be updated to the **unbootable** state. 568e41f4b71Sopenharmony_ci ```cpp 569e41f4b71Sopenharmony_ci int setSlotUnbootable(int slot) 570e41f4b71Sopenharmony_ci ``` 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci 4. Obtain the number of slots. The value **1** indicates a common update, and the value **2** indicates an A/B update. 573e41f4b71Sopenharmony_ci ```cpp 574e41f4b71Sopenharmony_ci int32 GetSlotNum(void) 575e41f4b71Sopenharmony_ci ``` 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci 578e41f4b71Sopenharmony_ci##### FAQs 579e41f4b71Sopenharmony_ci 580e41f4b71Sopenharmony_ci1. An exception occurs during installation of the update package. 581e41f4b71Sopenharmony_ci<br>The system keeps running with the current version. It will attempt a new update in the next package search period. 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci2. An exception occurs during activation of the new version if the update package is installed in a non-boot partition. 584e41f4b71Sopenharmony_ci<br>Perform a rollback and set the partition to the **unbootable** state so that the system does not boot from this partition. 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci##### Verification 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ciIn normal cases, the device can download the update package from the OTA server in the background, perform a silent update, and then restart according to the preconfigured activation policy for the new version to take effect. 590