OTA介绍:
随着设备系统日新月异,用户如何及时获取系统的更新,体验新版本带来的新的体验,以及提升系统的稳定和安全性成为了每个厂商都面临的严峻问题。
OTA(Over the Air)提供对设备远程升级的能力。升级子系统对用户屏蔽了底层芯片的差异,对外提供了统一的升级接口。基于接口进行二次开发后,可以让厂商的设备(如IP摄像头等)轻松支持远程升级能力,即通过移动通信的接口从远程服务器下载新的软件更新包,对自身系统进行升级,从而满足厂商的应用管理需求。
OpenHarmony OTA升级方式:
从OTA包的升级方式来说分为本地升级和网络OTA升级。
OTA包类型
从OTA包的类型来说,可分为全量OTA包、差分(增量)OTA包、变分区OTA包。
OTA实现原理:利用升级包制作工具,将编译出的版本打包生成升级包。厂商设备集成 OTA 升级能力后,将升级包上传至服务器,通过升级应用下载升级包,触发并完成升级。
OpenHarmony采用AB升级机制,即设备有一套备份的B系统,在A系统运行时,可以在正常使用的状态下,静默更新B系统,升级成功后,重启切换新系统,实现版本更新的机制。
OpenHarmony OTA升级的约束与限制
:
本章介绍OpenHarmony 4.0 OTA全量包的制作和本地升级步骤。
为了验证OTA的升级成功与否,可在升级包制作前,对源码添加易于识别的差异,例如:
修改授权管理应用TAG信息
applications/standard/permission_manager/permissionmanager/src/main/ets/ServiceExtAbility/ServiceExtAbility.ts
import deviceInfo from '@ohos.deviceInfo';
-var TAG = "PermissionManager_Log:";
+var TAG = "PermissionManager_Log A:";
const BG_COLOR = '#00000000'
全量编译源码
$ ./build.sh --product-name=rk3568 --ccache
得到系统镜像文件images和二进制升级文件updater_binary:
out/rk3568/packages/phone/images/
├── boot_linux.img
├── chip_prod.img
├── config.cfg
├── MiniLoaderAll.bin
├── parameter.txt
├── ramdisk.img
├── resource.img
├── sys_prod.img
├── system.img
├── uboot.img
├── updater.img
├── userdata.img
└── vendor.img
OTA_Package/
├── BOARD.list #取自device/board/hisilicon/hispark_taurus/linux/updater/config/BOARD.list
├── updater_specified_config.xml #取自device/board/hisilicon/hispark_taurus/linux/system/updater_specified_config.xml
└── VERSION.mbn #取自device/board/hisilicon/hispark_taurus/linux/updater/config/VERSION.mbn
#BOARD.list 添加一行"RK3568",内容如下:
HI3516
RK3568
#updater_specified_config.xml, 修改softVersion(可与VERSION.mbn保持一致)和compVer, 需注意compType需为0,表示全量;内容如下:
head info
./vendor.img
./system.img
#VERSION.mbn 内容如下
OpenHarmony 4.0.10.13
注意:
BOARD.list RK3568可省略,但HI3516必须有,否则OTA包校验失败而导致OTA升级失败,因为代码匹配BOARD时,固定了默认值:
base/updater/updater/utils/utils.cpp
std::string GetLocalBoardId()
{
return "HI3516";
}
#!/bin/bash
SRC_TOP_DIR=~/OHOS/OpenHarmony_v4.0_Release
OTA_DIR_PATH=$SRC_TOP_DIR/OTA_Package
default_ota_package_name="updater_full.zip"
Packaging_tools_path=$SRC_TOP_DIR/base/update/packaging_tools
build_type=$1
if [ "$build_type" = "--full" ];then
build_type_src="full OTA package"
elif [ "$build_type" = "--incre" ];then
if [ '$2' ];then
incre_source_package=$2
else
echo "miss source package param, eg: ./source.zip"
return
fi
build_type_src="incremental OTA package"
elif [ $# == 0 ];then
echo "Param is empty !"
echo "--full: build full OTA package"
echo "--incre: build incremental OTA package"
return
fi
echo "build OpenHarnomy 4.0 OTA $build_type_src"
echo "(1)执行目录: "$SRC_TOP_DIR
echo "(2)创建目录"
if [ -d "$OTA_DIR_PATH" ]; then
if [ -d "$OTA_DIR_PATH/target_package" ]; then
#重置target_package/
rm -rf $OTA_DIR_PATH/target_package
mkdir $OTA_DIR_PATH/target_package
mkdir $OTA_DIR_PATH/target_package/updater_config
fi
if [ ! -d "$OTA_DIR_PATH/output_package" ]; then
#创建output_package/
mkdir $OTA_DIR_PATH/output_package
fi
else
mkdir $OTA_DIR_PATH
mkdir $OTA_DIR_PATH/target_package
mkdir $OTA_DIR_PATH/target_package/updater_config
mkdir $OTA_DIR_PATH/output_package
fi
#packaging_tools/下创建文件夹临时保存签名证书
if [ ! -d "$Packaging_tools_path/sign_cert" ]; then
mkdir $Packaging_tools_path/sign_cert
fi
echo "(3)复制配置文件"
cp $OTA_DIR_PATH/BOARD.list $OTA_DIR_PATH/target_package/updater_config/BOARD.list
cp $OTA_DIR_PATH/VERSION.mbn $OTA_DIR_PATH/target_package/updater_config/VERSION.mbn
cp $OTA_DIR_PATH/updater_specified_config.xml $OTA_DIR_PATH/target_package/updater_config/updater_specified_config.xml
cp $SRC_TOP_DIR/base/update/updater/test/unittest/test_data/src/rsa_private_key2048.pem $OTA_DIR_PATH/
cp $SRC_TOP_DIR/base/update/updater/test/unittest/test_data/src/signing_cert.crt $Packaging_tools_path/sign_cert/
echo "(4)复制编译生成的文件"
cp $SRC_TOP_DIR/out/rk3568/packages/phone/system/bin/updater_binary $OTA_DIR_PATH/target_package/
cp -rf $SRC_TOP_DIR/out/rk3568/packages/phone/images/* $OTA_DIR_PATH/target_package/
echo "(5)切换到OTA工具目录: base/update/packaging_tools"
cd $Packaging_tools_path
echo "(6)执行OTA制作……"
data=$(date +%F%T )
data=${data//-/}
data=${data//:/}
if [ "$build_type" = "--full" ];then
#full OTA package
python3 build_update.py $OTA_DIR_PATH/target_package/ $OTA_DIR_PATH/output_package/ -pk $OTA_DIR_PATH/rsa_private_key2048.pem
if [ -f "$OTA_DIR_PATH/output_package/$default_ota_package_name" ]; then
output_OTA_package=$OTA_DIR_PATH/output_package/updater_full_$data.zip
else
echo "Not found $OTA_DIR_PATH/output_package/$default_ota_package_name !"
fi
elif [ "$build_type" = "--incre" ];then
#copy lib
mkdir $Packaging_tools_path/lib/
cp -rf $SRC_TOP_DIR/out/rk3568/clang_x64/thirdparty/e2fsprogs/* $Packaging_tools_path/lib/
cp -rf $SRC_TOP_DIR/out/rk3568/clang_x64/updater/updater/diff $Packaging_tools_path/lib/
#incremental OTA package
python3 build_update.py $OTA_DIR_PATH/target_package/ $OTA_DIR_PATH/output_package/ -s $incre_source_package -pk $OTA_DIR_PATH/rsa_private_key2048.pem
if [ -f "$OTA_DIR_PATH/output_package/$default_ota_package_name" ]; then
output_OTA_package=$OTA_DIR_PATH/output_package/updater_incremental_$data.zip
else
echo "Not found $OTA_DIR_PATH/output_package/$default_ota_package_name !"
fi
fi
#build source.zip
cd $OTA_DIR_PATH/target_package && zip -r $OTA_DIR_PATH/output_package/updater_source_$data.zip *
#重命名OTA包
mv $OTA_DIR_PATH/output_package/$default_ota_package_name $output_OTA_package
rm -rf $Packaging_tools_path/sign_cert/
cd $SRC_TOP_DIR
echo -e "(7)脚本执行完毕,请手动检查结果:\n$output_OTA_package \n$OTA_DIR_PATH/output_package/updater_source_$data.zip"
$ source build_ota.sh --full
build OpenHarnomy 4.0 OTA full OTA package
(1)执行目录: /home/xxx/OHOS/OpenHarmony_v4.0_Release
(2)创建目录
(3)复制配置文件
(4)复制编译生成的文件
(5)切换到OTA工具目录: base/update/packaging_tools
(6)执行OTA制作……
2023-09-12 08:52:19 INFO : VERSION.mbn file parsing complete! path: /home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/target_package/updater_config/VERSION.mbn
2023-09-12 08:52:19 INFO : BOARD.list file parsing complete! path: /home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/target_package/updater_config/BOARD.list
2023-09-12 08:52:19 INFO : XML file parsing completed!
2023-09-12 08:52:19 INFO : []
2023-09-12 08:52:19 INFO : Image vendor full processing completed
2023-09-12 08:52:23 INFO : Image system full processing completed
2023-09-12 08:52:23 INFO : All full image processing completed! image count: 2
2023-09-12 08:52:23 INFO : Get hash content success! path: /home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/target_package/updater_config/VERSION.mbn
2023-09-12 08:52:23 INFO : Get hash content success! path: /home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/target_package/updater_config/BOARD.list
2023-09-12 08:52:24 INFO : Get hash content success! path: /home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/target_package/full_imagevendor4nxap3oq
2023-09-12 08:52:32 INFO : Get hash content success! path: /home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/target_package/full_imagesystemoe5jbzgf
2023-09-12 08:52:32 INFO : Write package header complete
2023-09-12 08:52:32 INFO : Add component b'/version_list'
2023-09-12 08:52:32 INFO : component information StartOffset:180
2023-09-12 08:52:32 INFO : Add component to package StartOffset:1184
2023-09-12 08:52:32 INFO : Write component complete ComponentSize:20
2023-09-12 08:52:32 INFO : Add component b'/board_list'
2023-09-12 08:52:32 INFO : component information StartOffset:267
2023-09-12 08:52:32 INFO : Add component to package StartOffset:1204
2023-09-12 08:52:32 INFO : Write component complete ComponentSize:14
2023-09-12 08:52:32 INFO : Add component b'/vendor'
2023-09-12 08:52:32 INFO : component information StartOffset:354
2023-09-12 08:52:32 INFO : Add component to package StartOffset:1218
2023-09-12 08:52:32 INFO : Write component complete ComponentSize:268431360
2023-09-12 08:52:32 INFO : Add component b'/system'
2023-09-12 08:52:32 INFO : component information StartOffset:441
2023-09-12 08:52:32 INFO : Add component to package StartOffset:268432578
2023-09-12 08:52:35 INFO : Write component complete ComponentSize:1610608640
2023-09-12 08:52:35 INFO : Write update package complete
2023-09-12 08:52:40 INFO : .bin package signing success! SignOffset: 544
2023-09-12 08:52:40 INFO : Create update package .bin complete! path: /home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/output_package/update_bin-wvjeznc7
2023-09-12 08:52:40 INFO : Verse-script.us generation complete!
2023-09-12 08:52:40 INFO : loadScript.us generation complete!
2023-09-12 08:53:11 INFO : []
2023-09-12 08:53:12 INFO : Resource cleaning completed!
adding: boot_linux.img (deflated 80%)
adding: chip_prod.img (deflated 100%)
adding: config.cfg (deflated 91%)
adding: MiniLoaderAll.bin (deflated 30%)
adding: parameter.txt (deflated 44%)
adding: ramdisk.img (deflated 0%)
adding: resource.img (deflated 99%)
adding: sys_prod.img (deflated 100%)
adding: system.img (deflated 82%)
adding: uboot.img (deflated 61%)
adding: updater_binary (deflated 36%)
adding: updater_config/ (stored 0%)
adding: updater_config/BOARD.list (stored 0%)
adding: updater_config/VERSION.mbn (stored 0%)
adding: updater_config/updater_specified_config.xml (deflated 49%)
adding: updater.img (deflated 0%)
adding: userdata.img (deflated 100%)
adding: vendor.img (deflated 91%)
(7)脚本执行完毕,请手动检查结果:
/home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/output_package/updater_full_20230912110840.zip
/home/xxx/OHOS/OpenHarmony_v4.0_Release/OTA_Package/output_package/updater_source_20230912110840.zip
脚本编译完成后,OTA_Package结构如下
OTA_Package/
├── BOARD.list
├── output_package
│ └── updater_full_20230912110840.zip #全量包
│ └── updater_source_20230912110840.zip #可用作制作差异包的source包
├── rsa_private_key2048.pem
├── target_package
│ ├── boot_linux.img
│ ├── chip_prod.img
│ ├── config.cfg
│ ├── MiniLoaderAll.bin
│ ├── parameter.txt
│ ├── ramdisk.img
│ ├── resource.img
│ ├── sys_prod.img
│ ├── system.img
│ ├── uboot.img
│ ├── updater_binary
│ ├── updater_config
│ │ ├── BOARD.list
│ │ ├── updater_specified_config.xml
│ │ └── VERSION.mbn
│ ├── updater.img
│ ├── userdata.img
│ └── vendor.img
├── updater_specified_config.xml
└── VERSION.mbn
将制作好的全量包放入设备,并设置reboot模式
> hdc file send xxx\OHOS\OpenHarmony_v4.0_Release\OTA_Package\output_package\updater_full_20230912110840.zip /data/updater/updater.zip
FileTransfer finish, Size:315251542, File count = 1, time:35248ms rate:8943.81kB/s
> hdc shell
#write_updater updater /data/updater/updater.zip
#reboot updater
OpenHarmony 3.2 OTA本地升级
升级成功后,设备自动重启。
打开桌面音乐应用
弹出用于授权弹框
使用IDE 过滤“PermissionManager_Log A”,结果如下
08-05 20:26:14.659 1659-1659/? I A0fefe/JsApp: PermissionManager_Log A:ServiceExtensionAbility onCreate, ability name is com.ohos.permissionmanager.GrantAbility
08-05 20:26:14.664 1659-1659/? I A0fefe/JsApp: PermissionManager_Log A:ServiceExtensionAbility onRequest. start id is 1
08-05 20:26:14.664 1659-1659/? I A0fefe/JsApp: PermissionManager_Log A:want: {"deviceId":"","bundleName":"com.ohos.permissionmanager","abilityName":"com.ohos.permissionmanager.GrantAbility","moduleName":"permissionmanager","uri":"","type":"","flags":0,"action":"","parameters":{"component.startup.newRules":true,"moduleName":"permissionmanager","ohos.aafwk.param.callerPid":1625,"ohos.aafwk.param.callerToken":537601482,"ohos.aafwk.param.callerUid":20010030,"ohos.ability.params.callback":{"type":"RemoteObject","value":{}},"ohos.ability.params.token":{"type":"RemoteObject","value":{}},"ohos.user.grant.permission":["ohos.permission.DISTRIBUTED_DATASYNC"],"ohos.user.grant.permission.state":[1]},"entities":[]}
08-05 20:26:14.669 1659-1659/? I A0fefe/JsApp: PermissionManager_Log A:create window
说明全量OTA升级成功且生效。
若升级失败,重启设备后取出/data/updater/log/updater_log,结合代码进行具体分析。
> hdc file recv /data/updater/log/updater_log C:\Users\xxx\Desktop\ota.log
例如:
现象:OTA全量升级,升级进度条即将跑完时,提示升级失败。
log部分内容如下:
2017-08-05 12:24:11 [INFO]UPDATER pkg_upgradefile.cpp 306 : Component packedSize 1610608640 unpackedSize 1610608640 /system
2017-08-05 12:24:30 [INFO]UPDATER pkg_pkgfile.cpp 70 : ExtractFile /data/updater/build_tools.zip.tmp
2017-08-05 12:24:30 [INFO]UPDATER pkg_zipfile.cpp 578 : packedSize: 1067369 unpackedSize: 1067800 offset header: 314190358 data: 314190403
2017-08-05 12:24:30 [INFO]UPDATER pkg_zipfile.cpp 161 : LoadPackage /data/updater/build_tools.zip.tmp :1067800
2017-08-05 12:24:30 [INFO]UPDATER pkg_zipfile.cpp 634 : packedSize: 94 unpackedSize: 156 offset header: 0 data: 45 Verse-script.us
2017-08-05 12:24:30 [INFO]UPDATER pkg_zipfile.cpp 634 : packedSize: 1067341 unpackedSize: 1661888 offset header: 139 data: 183 updater_binary
2017-08-05 12:24:30 [INFO]UPDATER pkg_zipfile.cpp 634 : packedSize: 31 unpackedSize: 33 offset header: 1067524 data: 1067567 loadScript.us
2017-08-05 12:24:30 [INFO]UPDATER updater.cpp 211 : Package bin file verified. start to install package...
2017-08-05 12:24:30 [INFO]UPDATER pkg_pkgfile.cpp 70 : ExtractFile /board_list
2017-08-05 12:24:30 [INFO]UPDATER pkg_upgradefile.cpp 515 : Unpack /board_list data offset:1205 packedSize:6 unpackedSize:6
2017-08-05 12:24:30 [INFO]UPDATER updater_preprocess.cpp 86 : Check board list success
2017-08-05 12:24:30 [INFO]UPDATER updater_preprocess.cpp 60 : current version:OpenHarmony 4.0.10.13
2017-08-05 12:24:30 [INFO]UPDATER pkg_pkgfile.cpp 70 : ExtractFile /version_list
2017-08-05 12:24:30 [INFO]UPDATER pkg_upgradefile.cpp 515 : Unpack /version_list data offset:1184 packedSize:21 unpackedSize:21
2017-08-05 12:24:30 [ERROR]UPDATER updater.cpp 213 : Version Check Fail...
2017-08-05 12:24:32 [INFO]UPDATER misc_info.cpp 94 : WriteUpdaterMiscMsg::misc path : /dev/block/platform/fe310000.sdhci/by-name/misc
2017-08-05 12:24:32 [INFO]UPDATER updater_ui_facade.cpp 101 : current progress 52
2017-08-05 12:24:34 [INFO]UPDATER updater_ui_facade.cpp 101 : current progress 76
2017-08-05 12:24:36 [INFO]UPDATER updater_ui_facade.cpp 101 : current progress 88
2017-08-05 12:24:38 [INFO]UPDATER updater_ui_facade.cpp 101 : current progress 94
2017-08-05 12:24:40 [INFO]UPDATER updater_ui_facade.cpp 101 : current progress 97
2017-08-05 12:24:42 [INFO]UPDATER updater_ui_facade.cpp 101 : current progress 98
2017-08-05 12:24:44 [INFO]UPDATER updater_ui_facade.cpp 101 : current progress 99
2017-08-05 12:24:46 [ERROR]UPDATER updater_main.cpp 417 : InstallUpdaterPackage failed! Pkg is /data/updater/updater.zip
2017-08-05 12:24:46 [ERROR]UPDATER utils.cpp 453 : open result file failed
2017-08-05 12:24:46 [ERROR]UPDATER dump.h 44 : 1
2017-08-05 12:24:46 [ERROR]UPDATER updater_main.cpp 437 : read result file error /data/updater/updater_result
2017-08-05 12:24:46 [INFO]UPDATER updater_main.cpp 455 : post over, writeBuffer = /data/updater/updater.zip|fail
2017-08-05 12:24:46 [INFO]UPDATER utils.cpp 465 : WriteDumpResult: /data/updater/updater.zip|fail
2017-08-05 12:25:00 [INFO]UPDATER updater_ui.cpp 103 : On Label Reboot
2017-08-05 12:25:00 [INFO]UPDATER utils.cpp 522 : updater mode
2017-08-05 12:25:00 [INFO]UPDATER mount.cpp 125 : /data already mounted
2017-08-05 12:25:00 [INFO]UPDATER mount.cpp 125 : /data/updater/log already mounted
分析:从log可以看出升级至99%时,安装OTA包异常,问题原因是Version Check Fail…,于是检查updater_specified_config.xml和VERSION.mbn,版本信息错位的写为OpenHarmony 4.0.10.14。
解决方案:修改updater_specified_config.xml和VERSION.mbn中版本信息与设备当前软件版本OpenHarmony 4.0.10.13一致,重新制作OTA全量包后升级。
为了方便后续验证差分包升级是否有效,添加差异方便验证:
applications/standard/permission_manager/permissionmanager/src/main/ets/ServiceExtAbility/ServiceExtAbility.ts
import deviceInfo from '@ohos.deviceInfo';
-var TAG = "PermissionManager_Log:";
+var TAG = "PermissionManager_Log B:";
const BG_COLOR = '#00000000'
全量编译源码
$ ./build.sh -p rk3568 --ccache
得到系统镜像文件images和二进制升级文件updater_binary:
out/rk3568/packages/phone/images/
├── boot_linux.img
├── chip_prod.img
├── config.cfg
├── MiniLoaderAll.bin
├── parameter.txt
├── ramdisk.img
├── resource.img
├── sys_prod.img
├── system.img
├── uboot.img
├── updater.img
├── userdata.img
└── vendor.img
基于全量包的基础上修改OTA配置文件
#updater_specified_config.xml, 修改softVersion(与VERSION.mbn保持一致)和compVer, 需注意compType需为1,表示增量;内容如下:
head info
./vendor.img
./system.img
#VERSION.mbn ,版本号需大于source.zip版本号
OpenHarmony 4.0.10.14
运行build_ota.sh脚本
$ source build_ota.sh --incre ./OTA_Package/output_package/updater_source_20230912110840.zip
运行报错,缺少部分lib库,分析处理中,有差分包制作成功的大佬还请评论区指点一二。