diff --git a/bundle.json b/bundle.json index a591441e92f68e9f83c67435877c4245ca98f7ab..6c1ed649025124677bde0c714b4dd46513ad1445 100644 --- a/bundle.json +++ b/bundle.json @@ -326,6 +326,13 @@ "header_files": [], "header_base": "//base/update/updater/services/script/threadpool" } + }, + { + "name": "//base/update/updater/interfaces/kits/slot_info:libslotinfo_static", + "header": { + "header_files": [], + "header_base": "//base/update/updater/interfaces/kits/include" + } } ], "test": [ diff --git a/interfaces/kits/include/slot_info/slot_info_static.h b/interfaces/kits/include/slot_info/slot_info_static.h new file mode 100755 index 0000000000000000000000000000000000000000..67ce6fe77ec95c36d47f417e9745391846f222f5 --- /dev/null +++ b/interfaces/kits/include/slot_info/slot_info_static.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SLOT_INFO_STATIC_H +#define SLOT_INFO_STATIC_H + +#include +#include +#include +#include + +namespace Updater { +void GetPartitionSuffixStatic(std::string &suffix); +void GetActivePartitionSuffixStatic(std::string &suffix); +} // Updater +#endif /* SLOT_INFO_STATIC_H */ diff --git a/interfaces/kits/slot_info/BUILD.gn b/interfaces/kits/slot_info/BUILD.gn index 98db2059161ca9414e15416c8ad7c98e60b2def6..553650571951c70de07d2224bb44331e41545824 100644 --- a/interfaces/kits/slot_info/BUILD.gn +++ b/interfaces/kits/slot_info/BUILD.gn @@ -28,7 +28,7 @@ ohos_static_library("libslotinfo") { if (init_feature_ab_partition) { defines = [ "UPDATER_AB_SUPPORT" ] external_deps = [ - "c_utils:utils", + "c_utils:utilsbase", "drivers_interface_partitionslot:libpartitionslot_proxy_1.0", "hilog:libhilog_base", "init:libbegetutil_static", @@ -38,3 +38,24 @@ ohos_static_library("libslotinfo") { subsystem_name = "updater" part_name = "updater" } + +ohos_static_library("libslotinfo_static") { + sources = [ "slot_info_static.cpp" ] + + include_dirs = [ + "${updater_path}/interfaces/kits/include", + "${updater_path}/services/include", + "${updater_path}/utils/include", + ] + + if (init_feature_ab_partition) { + defines = [ "UPDATER_AB_SUPPORT" ] + external_deps = [ + "c_utils:utilsbase", + "hilog:libhilog_base", + ] + } + + subsystem_name = "updater" + part_name = "updater" +} \ No newline at end of file diff --git a/interfaces/kits/slot_info/slot_info.cpp b/interfaces/kits/slot_info/slot_info.cpp index 0948b5f59e0d5b65534fa8ddb67921120979ab19..8be93f73acd0f6e263482a426f818e70f71480d4 100644 --- a/interfaces/kits/slot_info/slot_info.cpp +++ b/interfaces/kits/slot_info/slot_info.cpp @@ -37,7 +37,8 @@ void SetActiveSlot() #else void GetPartitionSuffix(std::string &suffix) { - sptr partitionslot = + LOG(INFO) << "enter etPartitionSuffix"; + OHOS::sptr partitionslot = OHOS::HDI::Partitionslot::V1_0::IPartitionSlot::Get(true); if (partitionslot == nullptr) { LOG(ERROR) << "partitionslot ptr is nullptr"; @@ -62,7 +63,8 @@ void GetPartitionSuffix(std::string &suffix) void GetActivePartitionSuffix(std::string &suffix) { - sptr partitionslot = + LOG(INFO) << "enter GetActivePartitionSuffix"; + OHOS::sptr partitionslot = OHOS::HDI::Partitionslot::V1_0::IPartitionSlot::Get(true); if (partitionslot == nullptr) { LOG(ERROR) << "partitionslot ptr is nullptr"; @@ -86,7 +88,8 @@ void GetActivePartitionSuffix(std::string &suffix) void SetActiveSlot() { - sptr partitionslot = + LOG(INFO) << "enter SetActiveSlot"; + OHOS::sptr partitionslot = OHOS::HDI::Partitionslot::V1_0::IPartitionSlot::Get(true); if (partitionslot == nullptr) { LOG(ERROR) << "partitionslot ptr is nullptr"; diff --git a/interfaces/kits/slot_info/slot_info_static.cpp b/interfaces/kits/slot_info/slot_info_static.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bcd6a0e49128ca5c6247b0a3e9e94b98408f49f9 --- /dev/null +++ b/interfaces/kits/slot_info/slot_info_static.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "slot_info/slot_info_static.h" +#include "log/log.h" +#include + +namespace Updater { +#define MISC_DEVICE_NODE "/dev/block/by-name/bootctrl" +constexpr int32_t UPDATE_PARTITION_A = 1; +constexpr int32_t UPDATE_PARTITION_AB = 2; +constexpr off_t MISC_PARTITION_ACTIVE_SLOT_OFFSET = 1024; +constexpr off_t MISC_PARTITION_ACTIVE_SLOT_SIZE = 4; + +#ifndef UPDATER_AB_SUPPORT +void GetPartitionSuffixStatic(std::string &suffix) +{ + suffix = ""; +} +void GetActivePartitionSuffixStatic(std::string &suffix) +{ + suffix = ""; +} +#else +static int ReadMisc(off_t offset, off_t size) +{ + int fd = open(MISC_DEVICE_NODE, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (fd < 0) { + return -1; + } + if (lseek(fd, offset, SEEK_SET) < 0) { + LOG(ERROR) << "Failed lseek miscDevice"; + close(fd); + return -1; + } + int32_t slot = 0; + if (read(fd, &slot, size) != size) { + LOG(ERROR) <<"Failed read migic miscDevice"; + close(fd); + return -1; + } + LOG(INFO) << "Read slot:" << slot; + close(fd); + return slot; +} + +void GetPartitionSuffixStatic(std::string &suffix) +{ + // 1. 通过popen执行cat命令获取cmdline内容 + int32_t current_slot = ReadMisc(MISC_PARTITION_ACTIVE_SLOT_OFFSET, MISC_PARTITION_ACTIVE_SLOT_SIZE); + + LOG(INFO) << "current_slot: " << current_slot; + + // 4. 校验槽位有效性 + if (current_slot < UPDATE_PARTITION_A || current_slot > UPDATE_PARTITION_AB) { + LOG(ERROR) << "Invalid slot value: " << current_slot << ", expected 1 or 2"; + suffix = ""; + return; + } + + // 5. 生成目标分区后缀(取反当前槽位) + suffix = (current_slot == UPDATE_PARTITION_A) ? "_b" : "_a"; + LOG(INFO) << "Active slot: " << current_slot << ", target suffix: " << suffix; +} + +void GetActivePartitionSuffixStatic(std::string &suffix) +{ + // 1. 通过popen执行cat命令获取cmdline内容 + int32_t current_slot = ReadMisc(MISC_PARTITION_ACTIVE_SLOT_OFFSET, MISC_PARTITION_ACTIVE_SLOT_SIZE); + + LOG(INFO) << "current_slot: " << current_slot; + + // 4. 校验槽位有效性 + if (current_slot < UPDATE_PARTITION_A || current_slot > UPDATE_PARTITION_AB) { + LOG(ERROR) << "Invalid slot value: " << current_slot << ", expected 1 or 2"; + suffix = ""; + return; + } + + // 5. 生成目标分区后缀 + suffix = (current_slot == UPDATE_PARTITION_A) ? "_a" : "_b"; + LOG(INFO) << "Active slot: " << current_slot << ", target suffix: " << suffix; +} +#endif +} // updater \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn index f1c260f5391422ab84ef6d0e5fb9c02234288ef6..fdadaca6e0d0e01bd427489868e852826dd0bb9d 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -81,6 +81,7 @@ ohos_static_library("libupdater_static") { "${updater_path}/interfaces/kits/misc_info:libmiscinfo", "${updater_path}/services/sdcard_update:libsdupdate", "${updater_path}/interfaces/kits/slot_info:libslotinfo", + "${updater_path}/interfaces/kits/slot_info:libslotinfo_static", ] if (defined(use_ptable)) { @@ -99,6 +100,7 @@ ohos_static_library("libupdater_static") { "openssl:libcrypto_shared", "openssl:libssl_shared", "zlib:libz", + "drivers_interface_partitionslot:libpartitionslot_proxy_1.0", ] if (updater_ui_support) { external_deps += [ "drivers_interface_input:libinput_proxy_1.0" ] @@ -177,6 +179,7 @@ ohos_static_library("libupdater") { "openssl:libcrypto_shared", "openssl:libssl_shared", "zlib:libz", + "drivers_interface_partitionslot:libpartitionslot_proxy_1.0", ] # add updater custom library diff --git a/services/flow_update/update_bin/BUILD.gn b/services/flow_update/update_bin/BUILD.gn index 83e3d6df06c77ba6f63f54c1d2ef0b610ecc16f4..62f93ad87716904413376ba99e34525a83a75ee4 100644 --- a/services/flow_update/update_bin/BUILD.gn +++ b/services/flow_update/update_bin/BUILD.gn @@ -45,6 +45,7 @@ ohos_static_library("libBinFlowUpdate") { "${updater_path}/services/log:libupdaterlog", "${updater_path}/services/package:libupdaterpackage", "${updater_path}/utils:libutils", + "${updater_path}/interfaces/kits/slot_info:libslotinfo_static", ] external_deps = [ "bounds_checking_function:libsec_static", diff --git a/services/flow_update/update_bin/component_processor.cpp b/services/flow_update/update_bin/component_processor.cpp index f2cfccc1e23acf10b895b821511ca44879814699..54af30e2aeef580697dcdbea3c0000ba28044ea4 100644 --- a/services/flow_update/update_bin/component_processor.cpp +++ b/services/flow_update/update_bin/component_processor.cpp @@ -23,7 +23,7 @@ #ifdef UPDATER_USE_PTABLE #include "ptable_parse/ptable_manager.h" #endif -#include "slot_info/slot_info.h" +#include "slot_info/slot_info_static.h" #include "updater/updater_const.h" using namespace std; @@ -259,8 +259,7 @@ int RawImgProcessor::GetWritePathAndOffset(const std::string &partitionName, std #ifndef UPDATER_UT if (partitionName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); writePath += suffix; } LOG(INFO) << "write partition path: " << writePath; diff --git a/services/include/updater/updater.h b/services/include/updater/updater.h index de314e5ba6cdde786e58f621ec31cbef3548ae15..d46ba437d63499c5a16112690220fd36ec6465e7 100644 --- a/services/include/updater/updater.h +++ b/services/include/updater/updater.h @@ -96,6 +96,8 @@ void ProgressSmoothHandler(int beginProgress, int endProgress); UpdaterStatus SetUpdateSlotParam(UpdaterParams &upParams, bool isUpdateCurrSlot); +UpdaterStatus SetUpdateSuffixParam(); + UpdaterStatus ClearUpdateSlotParam(); UpdaterStatus DoInstallUpdaterPackage(Hpackage::PkgManager::PkgManagerPtr pkgManager, diff --git a/services/stream_update/BUILD.gn b/services/stream_update/BUILD.gn index aabb09961f850b2fd95cc964fce86269ab434b37..dc60b43679a0394bc39eb1bb2c421108142d0ecd 100755 --- a/services/stream_update/BUILD.gn +++ b/services/stream_update/BUILD.gn @@ -49,12 +49,14 @@ ohos_static_library("libbinchunkupdate") { "${updater_path}/services/log:libupdaterlog", "${updater_path}/services/package:libupdaterpackage", "${updater_path}/utils:libutils", + "${updater_path}/interfaces/kits/slot_info:libslotinfo_static", ] external_deps = [ "bounds_checking_function:libsec_static", "init:libbegetutil_static", "init:libfsmanager_static_real", "openssl:libcrypto_static", + "zlib:libz", ] public_configs = [ ":libstreamupdate_exported_headers" ] diff --git a/services/stream_update/bin_chunk_update.cpp b/services/stream_update/bin_chunk_update.cpp index a9140fd93e0e05da3c3a37b68efc246c1f1e09d2..53113f9b6f7cb84c11db8799cca0d775eb0ba5de 100755 --- a/services/stream_update/bin_chunk_update.cpp +++ b/services/stream_update/bin_chunk_update.cpp @@ -27,9 +27,13 @@ #include "fs_manager/mount.h" #include "log.h" #include "scope_guard.h" -#include "slot_info/slot_info.h" +#include "slot_info/slot_info_static.h" #include "utils.h" +#include // 支持ZIP_ZLIB和ZIP_GZIP +// #include // 支持ZIP_BZIP2 +// #include // 支持ZIP_LZMA + namespace Updater { using namespace Uscript; using namespace Hpackage; @@ -37,6 +41,12 @@ using namespace std::placeholders; constexpr const char *UPDATE_BIN_FILE = "update.bin"; constexpr const size_t HASH_BUFFER_SIZE = 50 * 1024; +constexpr size_t CHUNK_INFO_MAGIC_SIZE = 8; +constexpr size_t CHUNK_INFO_VERSION_SIZE = 8; +constexpr size_t CHUNK_INFO_RESERVED_SIZE = 232; +constexpr size_t CHUNK_DATA_PARTION_SIZE = 32; +constexpr size_t CHUNK_DATA_RESERVED_SIZE = 224; + BinChunkUpdate::BinChunkUpdate(uint32_t maxBufSize) { LOG(DEBUG) << "BinChunkUpdate::BinChunkUpdate enter"; @@ -112,6 +122,9 @@ UpdateResultCode BinChunkUpdate::ProcessBufferData() case BIN_UPDATE_HEAD_TIP: ret = UpdateBinHead(buffer_, curlen_); break; + case BIN_UPDATE_INFO_TIP: + ret = UpdateBinInfo(buffer_, curlen_); + break; case BIN_UPDATE_DATA_TIP: ret = UpdateBinData(buffer_, curlen_); break; @@ -216,7 +229,7 @@ UpdateResultCode BinChunkUpdate::UpdateBinHash(uint8_t *data, uint32_t &len) //切换ab分区 #ifndef UPDATER_UT - SetActiveSlot(); + // SetActiveSlot(); #else int result = remove("/data/updater/test.txt"); if (result != 0) { @@ -502,7 +515,7 @@ bool BinChunkUpdate::ReadPartitionData(uint8_t *data, uint32_t &len) } updateInfo_.curPartition = ""; - PkgFileImpl::ConvertBufferToString(updateInfo_.curPartition, {data + offset, tlv.length}); + PkgFileImpl::ConvertBufferToString(updateInfo_.curPartition, {data + offset, CHUNK_DATA_PARTION_SIZE}); LOG(DEBUG) << "PreWriteBin name " << updateInfo_.curPartition; return true; @@ -518,14 +531,13 @@ bool BinChunkUpdate::OpenDevPath() targetPath = devPath; if (updateInfo_.curPartition != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); targetPath += suffix; - GetActivePartitionSuffix(suffix); + suffix = Utils::GetUpdateActiveSuffix(); srcPath += suffix; } - LOG(DEBUG) << "ChunkInstallPreWrite curPartition:" << updateInfo_.curPartition + LOG(INFO) << "ChunkInstallPreWrite curPartition:" << updateInfo_.curPartition << " srcPath:" << srcPath << " targetPath:" << targetPath; updateInfo_.srcFd = open(srcPath.c_str(), O_RDWR | O_LARGEFILE); @@ -568,7 +580,7 @@ bool BinChunkUpdate::InitTransferParams() UpdateResultCode BinChunkUpdate::ChunkInstallDoWrite(uint8_t *data, uint32_t &len) { - LOG(DEBUG) << "BinChunkUpdate::ChunkInstallDoWrite enter"; + LOG(INFO) << "BinChunkUpdate::ChunkInstallDoWrite enter"; uint32_t offset = offset_; // 处理安装分区 @@ -622,9 +634,16 @@ bool BinChunkUpdate::ProcessPartition(uint8_t *data, uint32_t &len, uint32_t &of } std::string partition; - PkgFileImpl::ConvertBufferToString(partition, {data + offset, tlv.length}); + PkgFileImpl::ConvertBufferToString(partition, {data + offset, CHUNK_DATA_PARTION_SIZE}); LOG(DEBUG) << "partition:" << partition; - offset += tlv.length; + offset += CHUNK_DATA_PARTION_SIZE; + + std::string reservedData; + reservedData.assign(data + offset, data + offset + 224); + if (reservedData.size() != CHUNK_DATA_RESERVED_SIZE) { + LOG(ERROR) << " ERROR Reserved size:" << reservedData.size(); + } + offset += CHUNK_DATA_RESERVED_SIZE; if (partition != updateInfo_.curPartition) { updateInfo_.updateStep = CHUNK_INSTALL_STEP_POST; @@ -673,9 +692,9 @@ bool BinChunkUpdate::ProcessInstallData(uint8_t *data, uint32_t &len, uint32_t & } tlv2.type = ReadLE16(data + offset); - LOG(DEBUG) << "tlv2.type:" << tlv2.type; + LOG(INFO) << "tlv2.type:" << tlv2.type; tlv2.length = ReadLE32(data + offset + sizeof(uint16_t)); - LOG(DEBUG) << "tlv2.length:" << tlv2.length; + LOG(INFO) << "tlv2.length:" << tlv2.length; offset += sizeof(PkgTlvHI); if ((len - offset) < tlv2.length) { @@ -684,8 +703,22 @@ bool BinChunkUpdate::ProcessInstallData(uint8_t *data, uint32_t &len, uint32_t & return false; } - updateInfo_.transferParams->dataBuffer = data + offset; - updateInfo_.transferParams->dataBufferSize = tlv2.length; + // 提取数据 + std::vector compressed_data(data + offset, data + offset + tlv2.length); + + persistentDecompressedData_ = decompress_data(updateInfo_.compresionMethod, compressed_data); + + // if (decompressed_data.empty()) { + // LOG(ERROR) << "Decompression failed"; + // return false; + // } + + // Store the decompressed data in the updateInfo structure + updateInfo_.transferParams->dataBuffer = persistentDecompressedData_.data(); + updateInfo_.transferParams->dataBufferSize = persistentDecompressedData_.size(); + + // updateInfo_.transferParams->dataBuffer = data + offset; + // updateInfo_.transferParams->dataBufferSize = tlv2.length; offset += tlv2.length; return true; @@ -727,6 +760,68 @@ UpdateResultCode BinChunkUpdate::ChunkInstallPostWrite(uint8_t *data, uint32_t & return STREAM_UPDATE_SUCCESS; } +UpdateResultCode BinChunkUpdate::UpdateBinInfo(uint8_t *data, uint32_t &len) +{ + LOG(INFO) << "BinChunkUptdate::UpdateBinInfo enter"; + PkgTlvHH tlv; + uint32_t offset = offset_; + + if ((len - offset) < sizeof(PkgTlvHH)) { + LOG(INFO) << "needNewData"; + updateInfo_.needNewData = true; + return STREAM_UPDATE_SUCCESS; + } + + tlv.type = ReadLE16(data + offset); + LOG(INFO) << "tlv.type:" << tlv.type; + if (tlv.type != BIN_UPDATE_INFO_TIP) { + LOG(ERROR) << "Invalid chunkinfo type: 0x" << std::hex << tlv.type; + return STREAM_UPDATE_FAILURE; + } + + tlv.length = ReadLE16(data + offset + sizeof(uint16_t)); + LOG(INFO) << "tlv.length:" << tlv.length; + offset += sizeof(PkgTlvHH); + + if ((len - offset) < tlv.length) { + LOG(INFO) << "needNewData"; + updateInfo_.needNewData = true; + return STREAM_UPDATE_SUCCESS; + } + + updateInfo_.magicNum.assign(data + offset, data + offset + CHUNK_INFO_MAGIC_SIZE); + LOG(INFO) << "magicNum:" << updateInfo_.magicNum; + offset += CHUNK_INFO_MAGIC_SIZE; + + updateInfo_.fileFormatVeriosn.assign(data + offset, data + offset + CHUNK_INFO_VERSION_SIZE); + LOG(INFO) << "fileFormatVeriosn:" << updateInfo_.fileFormatVeriosn; + offset += CHUNK_INFO_VERSION_SIZE; + + updateInfo_.compresionMethod = ReadLE16(data + offset); + LOG(INFO) << "compresionMethod:" << updateInfo_.compresionMethod; + offset += sizeof(uint16_t); + + updateInfo_.signMethod = ReadLE16(data + offset); + LOG(INFO) << "signMethod:" << updateInfo_.signMethod; + offset += sizeof(uint16_t); + + updateInfo_.chunkListNum = ReadLE32(data + offset); + LOG(INFO) << "chunkListNum:" << updateInfo_.chunkListNum; + offset += sizeof(uint32_t); + + std::string reservedData; + reservedData.assign(data + offset, data + offset + CHUNK_INFO_RESERVED_SIZE); + if (reservedData.size() != CHUNK_INFO_RESERVED_SIZE) { + LOG(ERROR) << " ERROR Reserved size:" << reservedData.size(); + return STREAM_UPDATE_FAILURE; + } + offset += CHUNK_INFO_RESERVED_SIZE; + + offset_ = offset; + + return STREAM_UPDATE_SUCCESS; +} + UpdateResultCode BinChunkUpdate::UpdateBinData(uint8_t *data, uint32_t &len) { LOG(DEBUG) << "BinChunkUpdate::UpdateBinData enter"; @@ -761,8 +856,7 @@ std::string BinChunkUpdate::ComputeFileHash(const std::string &partitionName, #ifndef UPDATER_UT std::string devPath = GetBlockDeviceByMountPoint(partitionName); if (partitionName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); devPath += suffix; } #else @@ -812,4 +906,46 @@ bool BinChunkUpdate::VerifyPartitionHash(const std::string &partitionName, const return true; } +std::vector BinChunkUpdate::decompress_data(int compress_method, + const std::vector& compressed_data) +{ + LOG(DEBUG) << "BinChunkUpdate::decompress_data enter"; + switch(compress_method) { + case NO_COMPRESS: + return compressed_data; + + case ZIP_ZLIB: { + z_stream zs = {}; + if (inflateInit(&zs) != Z_OK) + LOG(ERROR) << "inflateInit failed"; + return {}; + + zs.next_in = const_cast(compressed_data.data()); + zs.avail_in = compressed_data.size(); + + std::vector output(compressed_data.size() * 5); + int ret; + do { + zs.next_out = output.data() + zs.total_out; + zs.avail_out = output.size() - zs.total_out; + ret = inflate(&zs, Z_FINISH); + if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) { + inflateEnd(&zs); + LOG(ERROR) << "zlib decompress error"; + } + if (zs.avail_out == 0) + output.resize(output.size() * 2); + } while (ret != Z_STREAM_END); + + inflateEnd(&zs); + output.resize(zs.total_out); + return output; + } + + default: + LOG(ERROR) << "zlib decompress error"; + return {}; + } +} + } // namespace Updater diff --git a/services/stream_update/bin_chunk_update.h b/services/stream_update/bin_chunk_update.h index 306326d693df3a368f5fb1411c0cd478c2a3a4ef..ff08d6be707be957d2d7d797fc9a7023f04e8587 100755 --- a/services/stream_update/bin_chunk_update.h +++ b/services/stream_update/bin_chunk_update.h @@ -49,10 +49,19 @@ using UpdateResultCode = enum { using BinUpdateTip = enum { BIN_UPDATE_ZIP_TIP = 0xaa, BIN_UPDATE_HEAD_TIP = 0x01, + BIN_UPDATE_INFO_TIP = 0x10, BIN_UPDATE_DATA_TIP = 0x12, BIN_UPDATE_HASH_TIP = 0x16 }; +using CompressMethod = enum { + NO_COMPRESS = 0, + ZIP_ZLIB = 4, + ZIP_GZIP = 8, + ZIP_BZIP2 = 12, + ZIP_LZMA = 16 +}; + using ChunkInstallStep = enum { CHUNK_INSTALL_STEP_PRE = 0, CHUNK_INSTALL_STEP_DO, @@ -66,6 +75,11 @@ struct BinChunkUpdateInfo { int srcFd; int targetFd; std::unique_ptr transferParams; + std::string magicNum; + std::string fileFormatVeriosn; + int16_t compresionMethod; + int16_t signMethod; + int32_t chunkListNum; std::string curPartition; std::string cmdLine; int patitionNum; @@ -90,6 +104,7 @@ private: UpdateResultCode ChunkInstallPostWrite(uint8_t *data, uint32_t &len); UpdateResultCode UpdateBinHead(uint8_t *data, uint32_t &len); + UpdateResultCode UpdateBinInfo(uint8_t *data, uint32_t &len); UpdateResultCode UpdateBinData(uint8_t *data, uint32_t &len); UpdateResultCode UpdateBinHash(uint8_t *data, uint32_t &len); UpdateResultCode UpdateBinOther(uint8_t *data, uint32_t &len); @@ -130,11 +145,15 @@ private: const std::map &dataLenInfos); std::string ComputeFileHash(const std::string& partitionName, const std::map &dataLenInfos); + std::vector decompress_data(int compress_method, + const std::vector& compressed_data); + Hpackage::PkgManager::PkgManagerPtr pkgManager_; uint8_t *buffer_ = nullptr; uint32_t maxBufSize_ = 0; uint32_t curlen_ = 0; uint32_t offset_ = 0; + std::vector persistentDecompressedData_; std::map> chunkInstallProcess_; BinChunkUpdateInfo updateInfo_ {}; diff --git a/services/updater.cpp b/services/updater.cpp index 84158f918c239146a026a12ee7c18a8a6e6cde2b..dcdfc20e4f4c6535ebae8128e98bbc5fc13ae839 100644 --- a/services/updater.cpp +++ b/services/updater.cpp @@ -52,6 +52,7 @@ #include "updater_ui_stub.h" #include "utils.h" #include "write_state/write_state.h" +#include "slot_info/slot_info_static.h" namespace Updater { using Updater::Utils::SplitString; @@ -310,7 +311,19 @@ UpdaterStatus SetUpdateSlotParam(UpdaterParams &upParams, bool isUpdateCurrSlot) } return UPDATE_SUCCESS; } - + +UpdaterStatus SetUpdateSuffixParam() +{ + LOG(INFO) << "enter setupdatesuffixparam"; + std::string suffix = ""; + GetPartitionSuffixStatic(suffix); + if (!Utils::SetUpdateSuffix(suffix)) { + LOG(ERROR) << "set update.part.suffix fail"; + return UPDATE_ERROR; + } + return UPDATE_SUCCESS; +} + UpdaterStatus ClearUpdateSlotParam() { if (!Utils::IsVabDevice()) { diff --git a/services/updater_binary/BUILD.gn b/services/updater_binary/BUILD.gn index e196cd239de036fca9d1ea4efc93d35232cfdfd5..977b7768d75002d50abd30bd41afe46239df8f3c 100644 --- a/services/updater_binary/BUILD.gn +++ b/services/updater_binary/BUILD.gn @@ -58,7 +58,7 @@ ohos_static_library("libupdater_binary") { deps = [ "${updater_path}/interfaces/kits/misc_info:libmiscinfo", - "${updater_path}/interfaces/kits/slot_info:libslotinfo", + "${updater_path}/interfaces/kits/slot_info:libslotinfo_static", "${updater_path}/services/applypatch:libapplypatch", "${updater_path}/services/diffpatch/patch:libpatch", "${updater_path}/services/flow_update/update_bin:libBinFlowUpdate", diff --git a/services/updater_binary/update_image_block.cpp b/services/updater_binary/update_image_block.cpp index 95a6048fcd6fb514d8a814864602d64516987ab0..f7f14201d6e666145fe83df5eaba40bece25f837 100644 --- a/services/updater_binary/update_image_block.cpp +++ b/services/updater_binary/update_image_block.cpp @@ -30,7 +30,7 @@ #include "updater/updater_const.h" #include "updater/hwfault_retry.h" #include "utils.h" -#include "slot_info/slot_info.h" +#include "slot_info/slot_info_static.h" using namespace Uscript; using namespace Hpackage; @@ -181,8 +181,7 @@ static int32_t GetUpdateBlockInfo(struct UpdateBlockInfo &infos, Uscript::UScrip infos.devPath = GetBlockDeviceByMountPoint(infos.partitionName); #ifndef UPDATER_UT if (infos.partitionName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); infos.devPath += suffix; } #else @@ -498,8 +497,7 @@ int32_t UScriptInstructionBlockCheck::Execute(Uscript::UScriptEnv &env, Uscript: auto devPath = GetBlockDeviceByMountPoint(partitionName); #ifndef UPDATER_UT if (partitionName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); devPath += suffix; } #else @@ -767,8 +765,7 @@ int32_t UScriptInstructionShaCheck::Execute(Uscript::UScriptEnv &env, Uscript::U } #ifndef UPDATER_UT if (partitionName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); devPath += suffix; } LOG(INFO) << "write partition path: " << devPath; diff --git a/services/updater_binary/update_image_patch.cpp b/services/updater_binary/update_image_patch.cpp index ec3b3c58496b4fe11ad57b95805e37ed5dec2126..968a3249c151bdad6e479f7666059b234f3cc063 100644 --- a/services/updater_binary/update_image_patch.cpp +++ b/services/updater_binary/update_image_patch.cpp @@ -35,7 +35,7 @@ #include "updater/updater_const.h" #include "updater/hwfault_retry.h" #include "utils.h" -#include "slot_info/slot_info.h" +#include "slot_info/slot_info_static.h" using namespace Uscript; using namespace Hpackage; @@ -73,8 +73,7 @@ int32_t USInstrImagePatch::GetParam(Uscript::UScriptContext &context, ImagePatch para.devPath = GetBlockDeviceByMountPoint(para.partName); #ifndef UPDATER_UT if (para.partName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); para.devPath += suffix; } #else @@ -264,8 +263,7 @@ int32_t USInstrImageShaCheck::GetParam(Uscript::UScriptContext &context, CheckPa para.devPath = GetBlockDeviceByMountPoint(para.partName); #ifndef UPDATER_UT if (para.partName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); para.devPath += suffix; } #else diff --git a/services/updater_binary/update_processor.cpp b/services/updater_binary/update_processor.cpp index 78a1732597899883131ba71743a6ac634c61fd43..a079e55b5a05e567783fe6a3c0bd9894ba85935a 100644 --- a/services/updater_binary/update_processor.cpp +++ b/services/updater_binary/update_processor.cpp @@ -31,7 +31,7 @@ #endif #include "script_instruction.h" #include "script_manager.h" -#include "slot_info/slot_info.h" +#include "slot_info/slot_info_static.h" #include "update_image_block.h" #include "update_image_patch.h" #include "update_partitions.h" @@ -460,8 +460,7 @@ int UScriptInstructionRawImageWrite::GetWritePathAndOffset(const std::string &pa #ifndef UPDATER_UT if (partitionName != "/userdata") { - std::string suffix = ""; - GetPartitionSuffix(suffix); + std::string suffix = Utils::GetUpdateSuffix(); writePath += suffix; } LOG(INFO) << "write partition path: " << writePath; diff --git a/services/updater_main.cpp b/services/updater_main.cpp index c92a607f81c6ad686c4025322223bfb2d6a97ee8..1d6adfa47f521ab7ed085bbdc219eb9a369c9cd7 100644 --- a/services/updater_main.cpp +++ b/services/updater_main.cpp @@ -673,6 +673,10 @@ static UpdaterStatus PreUpdatePackages(UpdaterParams &upParams) LOG(ERROR) << "SetUpdateSlotParam failed"; return UPDATE_ERROR; } + if(SetUpdateSuffixParam() != UPDATE_SUCCESS) { + LOG(ERROR) << "SetUpdateSuffixParam failed"; + return UPDATE_ERROR; + } // verify package first if (VerifyCommonFiles(upParams) != UPDATE_SUCCESS) { return UPDATE_CORRUPT; // verify package failed must return UPDATE_CORRUPT, ux need it !!! @@ -963,6 +967,10 @@ static UpdaterStatus PreSdcardUpdatePackages(UpdaterParams &upParams) LOG(ERROR) << "SetUpdateSlotParam failed"; return UPDATE_ERROR; } + if (SetUpdateSuffixParam() != UPDATE_SUCCESS) { + LOG(ERROR) << "SetUpdateSuffixParam failed"; + return UPDATE_ERROR; + } UpdaterStatus status = VerifyPackages(upParams); if (status != UPDATE_SUCCESS) { return UPDATE_CORRUPT; // verify package failed must return UPDATE_CORRUPT, ux need it !!! diff --git a/updater_default_cfg.gni b/updater_default_cfg.gni index 4f84e9f209e4975b037210e40543cd5e11e7766a..796286a3362b12408a1c9cf58b66df6af96a9cf6 100644 --- a/updater_default_cfg.gni +++ b/updater_default_cfg.gni @@ -13,7 +13,7 @@ import("//build/ohos.gni") -init_feature_ab_partition = false +init_feature_ab_partition = true declare_args() { updater_cfg_file = "" updater_ui_support = true diff --git a/utils/include/utils.h b/utils/include/utils.h index 7cefae4d3efbce8df5a2deb76b98bf8288f08cd1..c25486eb5212d88b061fdee285c16a6b666af537 100644 --- a/utils/include/utils.h +++ b/utils/include/utils.h @@ -117,7 +117,10 @@ bool ConvertToDouble(const std::string &str, double &value); bool ConvertToFloat(const std::string &str, float &value); bool IsVabDevice(); bool SetUpdateSlot(int updateSlot); -int GetUpdateSlot(); +bool SetUpdateSuffix(std::string stringsuffix); +int GetUpdateSlot();; +std::string GetUpdateSuffix(); +std::string GetUpdateActiveSuffix(); #ifndef __WIN32 void SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode); #endif diff --git a/utils/utils.cpp b/utils/utils.cpp index 4c81481c836aa1b33587c9db6fe6fb605ddd58fb..0453e545bdc441be6092385b962e5e05c9c60d59 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -1111,7 +1111,39 @@ bool SetUpdateSlot(int setSlot) } return false; } - + +bool SetUpdateSuffix(std::string stringsuffix) +{ + if (stringsuffix.empty()) { + LOG(ERROR) << "suffix is empty"; + return false; + } + int tryNum = 3; + int ret1 = 0; + int ret2 = 0; + int setSlot = 1; + std::string setSlotStr = std::to_string(setSlot); + while (tryNum-- > 0) { + ret1 = SetParameter("update.part.slot", setSlotStr.c_str()); + if (ret1 != 0) { + LOG(ERROR) << "set update.part.slot fail, ret1 = " << ret1; + } + ret2 = SetParameter("update.part.suffix", stringsuffix.c_str()); + if (ret2 != 0) { + LOG(ERROR) << "set update.part.suffix fail, ret2 = " << ret2; + continue; + } + if (strcmp(GetUpdateSuffix().c_str(), stringsuffix.c_str()) == 0) { + LOG(INFO) << "set update.part.suffix is " << stringsuffix; + return true; + } + if (tryNum != 0) { + sleep(1); + } + } + return false; +} + int GetUpdateSlot() { if (!IsVabDevice()) { @@ -1130,6 +1162,36 @@ int GetUpdateSlot() return updateSlot; } +std::string GetUpdateSuffix() +{ + char paramValue[PARAM_SIZE + 1] = {0}; + if (GetParameter("update.part.suffix", "", paramValue, sizeof(paramValue) - 1) <= 0) { + LOG(ERROR) << "get update.part.suffix failed"; + return std::string(paramValue); + } + LOG(INFO) << "GetUpdateSuffix = " << paramValue; + return std::string(paramValue); +} + +std::string GetUpdateActiveSuffix() +{ + char paramValue[PARAM_SIZE + 1] = {0}; + if (GetParameter("update.part.suffix", "", paramValue, sizeof(paramValue) - 1) <= 0) { + LOG(ERROR) << "get update.part.suffix failed"; + return std::string(paramValue); + } + if (strcmp(paramValue, "_a") == 0) { + strncpy(paramValue, "_b", sizeof(paramValue)); + } else if (strcmp(paramValue, "_b") == 0) { + strncpy(paramValue, "_a", sizeof(paramValue)); + } else { + LOG(ERROR) << "Unexpected suffix value: " << paramValue; + return std::string(paramValue); + } + LOG(INFO) << "GetUpdateActiveSuffix = " << paramValue; + return std::string(paramValue); +} + #ifndef __WIN32 void SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode) {