diff --git a/bundle.json b/bundle.json index c997c816dd39918f0c3e2756e3a5d6bfa0b07e50..30af65ae7eb30daf6a61fb161b3c8760641bbd21 100644 --- a/bundle.json +++ b/bundle.json @@ -320,6 +320,13 @@ "header_base": "//base/update/updater/interfaces/kits/include" } }, + { + "name": "//base/update/updater/interfaces/kits/slot_info:libslotinfo_static", + "header": { + "header_files": [], + "header_base": "//base/update/updater/interfaces/kits/include" + } + }, { "name": "//base/update/updater/services/script:libthreadpool", "header": { 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..5d36d31f9464729360fc0cf6d0f6198d94a4b5dd --- /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 */ \ No newline at end of file diff --git a/interfaces/kits/slot_info/BUILD.gn b/interfaces/kits/slot_info/BUILD.gn index e513ffb5d2f6d478a628844c41d68d540e7e022d..74ae4b4eca0cc56fa677f0fc840ca01f0558a205 100644 --- a/interfaces/kits/slot_info/BUILD.gn +++ b/interfaces/kits/slot_info/BUILD.gn @@ -38,3 +38,23 @@ 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" +} 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 3e2f3a6856d89e4f3ae8110edc87fc061386ae0b..88b57aaab368308c04aecf26e943bd8d0eb48133 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -79,6 +79,7 @@ ohos_static_library("libupdater_static") { deps = updater_common_deps deps += [ "${updater_path}/interfaces/kits/misc_info:libmiscinfo", + "${updater_path}/interfaces/kits/slot_info:libslotinfo", "${updater_path}/services/sdcard_update:libsdupdate", ] diff --git a/services/flow_update/update_bin/BUILD.gn b/services/flow_update/update_bin/BUILD.gn index 83e3d6df06c77ba6f63f54c1d2ef0b610ecc16f4..48094b55e729316cf17f55fc6731987dcde88451 100644 --- a/services/flow_update/update_bin/BUILD.gn +++ b/services/flow_update/update_bin/BUILD.gn @@ -40,6 +40,7 @@ ohos_static_library("libBinFlowUpdate") { ] deps = [ "${updater_path}/interfaces/kits/packages:libpackageExt", + "${updater_path}/interfaces/kits/slot_info:libslotinfo_static", "${updater_path}/services/applypatch:libapplypatch", "${updater_path}/services/fs_manager:libfsmanager", "${updater_path}/services/log:libupdaterlog", diff --git a/services/flow_update/update_bin/component_processor.cpp b/services/flow_update/update_bin/component_processor.cpp index f2cfccc1e23acf10b895b821511ca44879814699..f5d4514b60d9eef128efbbbac099311199f1f0d1 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; @@ -260,7 +260,7 @@ int RawImgProcessor::GetWritePathAndOffset(const std::string &partitionName, std #ifndef UPDATER_UT if (partitionName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); writePath += suffix; } LOG(INFO) << "write partition path: " << writePath; diff --git a/services/stream_update/BUILD.gn b/services/stream_update/BUILD.gn index aabb09961f850b2fd95cc964fce86269ab434b37..236efcaedb5300cf8ee6037c6ef5029c660cee2b 100755 --- a/services/stream_update/BUILD.gn +++ b/services/stream_update/BUILD.gn @@ -44,6 +44,7 @@ ohos_static_library("libbinchunkupdate") { ] deps = [ "${updater_path}/interfaces/kits/packages:libpackageExt", + "${updater_path}/interfaces/kits/slot_info:libslotinfo_static", "${updater_path}/services/applypatch:libapplypatch", "${updater_path}/services/fs_manager:libfsmanager", "${updater_path}/services/log:libupdaterlog", diff --git a/services/stream_update/bin_chunk_update.cpp b/services/stream_update/bin_chunk_update.cpp index a9140fd93e0e05da3c3a37b68efc246c1f1e09d2..68acd84a5999c411f0573ded639aa9e1336db690 100755 --- a/services/stream_update/bin_chunk_update.cpp +++ b/services/stream_update/bin_chunk_update.cpp @@ -27,14 +27,16 @@ #include "fs_manager/mount.h" #include "log.h" #include "scope_guard.h" -#include "slot_info/slot_info.h" #include "utils.h" +#include "slot_info/slot_info_static.h" namespace Updater { using namespace Uscript; using namespace Hpackage; using namespace std::placeholders; +#define MISC_DEVICE_NODE "/dev/block/by-name/bootctrl" + constexpr const char *UPDATE_BIN_FILE = "update.bin"; constexpr const size_t HASH_BUFFER_SIZE = 50 * 1024; BinChunkUpdate::BinChunkUpdate(uint32_t maxBufSize) @@ -214,9 +216,7 @@ UpdateResultCode BinChunkUpdate::UpdateBinHash(uint8_t *data, uint32_t &len) return STREAM_UPDATE_FAILURE; } - //切换ab分区 #ifndef UPDATER_UT - SetActiveSlot(); #else int result = remove("/data/updater/test.txt"); if (result != 0) { @@ -519,9 +519,9 @@ bool BinChunkUpdate::OpenDevPath() if (updateInfo_.curPartition != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); targetPath += suffix; - GetActivePartitionSuffix(suffix); + GetActivePartitionSuffixStatic(suffix); srcPath += suffix; } @@ -762,7 +762,7 @@ std::string BinChunkUpdate::ComputeFileHash(const std::string &partitionName, std::string devPath = GetBlockDeviceByMountPoint(partitionName); if (partitionName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); devPath += suffix; } #else diff --git a/services/updater.cpp b/services/updater.cpp index a5b2429e8eaa9c5a5bfe7274722cb735e9b282ea..fa7aadae52c51b434886eac1a14ad2113365bb77 100644 --- a/services/updater.cpp +++ b/services/updater.cpp @@ -36,6 +36,7 @@ #include "package/packages_info.h" #include "parameter.h" #include "misc_info/misc_info.h" +#include "slot_info/slot_info.h" #ifdef WITH_SELINUX #include #include "selinux/selinux.h" @@ -550,6 +551,11 @@ static std::string GetBinaryPathFromBin(PkgManager::PkgManagerPtr pkgManager, Up fullPath = "/bin/updater_binary"; } + // 执行Binary成功后,切换分区 +#ifndef UPDATER_UT + SetActiveSlot(); +#endif + #ifdef UPDATER_UT fullPath = "/data/updater/updater_binary"; #endif @@ -565,6 +571,11 @@ static std::string GetBinaryPath(PkgManager::PkgManagerPtr pkgManager, UpdaterPa fullPath = "/bin/updater_binary"; } + // 执行Binary成功后,切换分区 +#ifndef UPDATER_UT + SetActiveSlot(); +#endif + #ifdef UPDATER_UT fullPath = "/data/updater/updater_binary"; #endif diff --git a/services/updater_binary/BUILD.gn b/services/updater_binary/BUILD.gn index 3f79682c84790f2295e8ab5454f75049032eb3f0..a5cf9c366adeff673e6959e86115a68b33c60257 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 c0953a4e154aa3270e8c1269d2bb58fdd420246c..a240bdc8785079d18f07364459be2827f065c950 100644 --- a/services/updater_binary/update_image_block.cpp +++ b/services/updater_binary/update_image_block.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "update_image_block.h" +#include "update_processor.h" #include #include #include @@ -30,7 +31,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,7 +182,7 @@ static int32_t GetUpdateBlockInfo(struct UpdateBlockInfo &infos, Uscript::UScrip #ifndef UPDATER_UT if (infos.partitionName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); infos.devPath += suffix; } #else @@ -465,7 +466,7 @@ int32_t UScriptInstructionBlockCheck::Execute(Uscript::UScriptEnv &env, Uscript: #ifndef UPDATER_UT if (partitionName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); devPath += suffix; } #else @@ -734,7 +735,7 @@ int32_t UScriptInstructionShaCheck::Execute(Uscript::UScriptEnv &env, Uscript::U #ifndef UPDATER_UT if (partitionName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); 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 5ce712dded0970f3c9c9a190f833553bb9261214..4bbee86f3f58fee6f78cf7294f07913ef0a28272 100644 --- a/services/updater_binary/update_image_patch.cpp +++ b/services/updater_binary/update_image_patch.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "update_image_patch.h" +#include "update_processor.h" #include #include #include @@ -35,7 +36,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; @@ -74,7 +75,7 @@ int32_t USInstrImagePatch::GetParam(Uscript::UScriptContext &context, ImagePatch #ifndef UPDATER_UT if (para.partName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); para.devPath += suffix; } #else @@ -265,7 +266,7 @@ int32_t USInstrImageShaCheck::GetParam(Uscript::UScriptContext &context, CheckPa #ifndef UPDATER_UT if (para.partName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); para.devPath += suffix; } #else diff --git a/services/updater_binary/update_processor.cpp b/services/updater_binary/update_processor.cpp index bec492cd4891658abc06787521b1452ad17dc22b..895edd1028b18c6fd19d3db23e1c1f7cc68217b4 100644 --- a/services/updater_binary/update_processor.cpp +++ b/services/updater_binary/update_processor.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "update_processor.h" #include #include @@ -31,7 +32,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,7 +461,7 @@ int UScriptInstructionRawImageWrite::GetWritePathAndOffset(const std::string &pa #ifndef UPDATER_UT if (partitionName != "/userdata") { std::string suffix = ""; - GetPartitionSuffix(suffix); + GetPartitionSuffixStatic(suffix); writePath += suffix; } LOG(INFO) << "write partition path: " << writePath; @@ -525,11 +526,7 @@ int ProcessUpdater(bool retry, int pipeFd, const std::string &packagePath, const } }); PkgManager::ReleasePackageInstance(pkgManager); -#ifndef UPDATER_UT - if (ret == 0) { - SetActiveSlot(); - } -#endif + return ret; } } // Updater \ No newline at end of file diff --git a/updater_default_cfg.gni b/updater_default_cfg.gni index e2036f204ba0e0f2f69774d693df1f297926b271..796286a3362b12408a1c9cf58b66df6af96a9cf6 100644 --- a/updater_default_cfg.gni +++ b/updater_default_cfg.gni @@ -45,7 +45,7 @@ template("updater_gen") { } else { ohos_executable(target_name) { if (!is_asan && !is_emulator && target_name == "updater_binary") { - static_link = false + static_link = true } forward_variables_from(invoker, "*")