From d4d8fdb1f9871b45d8474d8844163df54f597cd8 Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 3 Mar 2025 17:04:33 +0800 Subject: [PATCH 1/6] for ab updater Signed-off-by: LeonShu --- services/ptable_parse/ptable.cpp | 50 +++++++++++++++++++---- services/ptable_parse/ptable.h | 9 ++++ services/ptable_parse/ufs_ptable.cpp | 1 + test/fuzztest/updatermain_fuzzer/BUILD.gn | 1 + test/unittest/BUILD.gn | 1 + test/unittest/service_test/BUILD.gn | 1 + test/unittest/utils/BUILD.gn | 1 + utils/include/utils.h | 6 +++ utils/utils.cpp | 9 ++++ 9 files changed, 71 insertions(+), 8 deletions(-) diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index 4cb8e286..fdc190d2 100644 --- a/services/ptable_parse/ptable.cpp +++ b/services/ptable_parse/ptable.cpp @@ -456,7 +456,8 @@ void Ptable::PrintPtableInfo() const for (size_t i = 0; i < partitionInfo_.size(); i++) { LOG(INFO) << "ptable.entry[" << i << "].name=" << partitionInfo_[i].dispName.c_str() << ", startAddr=0x" << std::hex << partitionInfo_[i].startAddr << ", size=0x" << - partitionInfo_[i].partitionSize << ", lun=" << std::dec << partitionInfo_[i].lun; + partitionInfo_[i].partitionSize << ", lun=" << std::dec << partitionInfo_[i].lun + << ", partType=" << partitionInfo_[i].partType; } LOG(INFO) << "ptnInfo : ==========================================="; } @@ -473,11 +474,27 @@ void Ptable::PrintPtableInfo(const std::vector &ptnInfo) const for (size_t i = 0; i < ptnInfo.size(); i++) { LOG(INFO) << "ptable.entry[" << i << "].name=" << ptnInfo[i].dispName.c_str() << ", startAddr=0x" << std::hex << ptnInfo[i].startAddr << ", size=0x" << ptnInfo[i].partitionSize << ", lun=" << - std::dec << ptnInfo[i].lun; + std::dec << ptnInfo[i].lun << ", partType=" << partitionInfo_[i].partType; } LOG(INFO) << "ptnInfo : ==========================================="; } +void Ptable::SetPartitionType(const std::string &partName, PtnInfo &ptnInfo) +{ + if (PARTITION_AB_SUFFIX_SIZE > partName.size()) { + ptnInfo.partType = PARTITION_NORMAL_TYPE; + return; + } + std::strint partSuffix = partName.substr(partName.size() - PARTITION_AB_SUFFIX_SIZE, + PARTITION_AB_SUFFIX_SIZE); + if (strcasecmp(partSuffix.c_str(), PARTITION_A_SUFFIX) == 0 || + strcasecmp(partSuffix.c_str(), PARTITION_B_SUFFIX) == 0) { + ptnInfo.partType = PARTITION_AB_TYPE; + return; + } + ptnInfo.partType = PARTITION_NORMAL_TYPE; +} + void Ptable::ParsePartitionName(const uint8_t *data, const uint32_t dataLen, std::string &name, const uint32_t nameLen) { @@ -527,13 +544,30 @@ bool Ptable::GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptn LOG(ERROR) << "get partition failed! partitionInfo_ is empty"; return false; } - for (int32_t i = 0; i < static_cast(partitionInfo_.size()); i++) { - if (partitionInfo_[i].dispName.size() == partitionName.size() && - strcasecmp(partitionInfo_[i].dispName.c_str(), partitionName.c_str()) == 0) { - index = i; - ptnInfo = partitionInfo_[i]; - return true; + auto findPart = [&ptnInfo, &index, this] (const std::string &partitionName) { + for (int32_t i = 0; i < static_cast(partitionInfo_.size()); i++) { + if (partitionInfo_[i].dispName.size() == partitionName.size() && + strcasecmp(partitionInfo_[i].dispName.c_str(), partitionName.c_str()) == 0) { + index = i; + ptnInfo = partitionInfo_[i]; + return true; + } } + return false; + } + if (findPart(partitionName)) { + LOG(INFO) << "find partition name " << partitionName; + return true; + } + std::string partitioNameAB = partitionName; + if (Utils::IsUpdaterMode()) { + partitionNameAB += PARTITION_A_SUFFIX; + } else { + partitionNameAB += (Utils::GetABDevCurrentSlot() == SLOT_A ? PARTITION_B_SUFFIX : PARTITION_A_SUFFIX); + } + if (findPart(partitionNameAB) { + LOG(INFO) << "find partitionAB name " << partitionNameAB; + return true; } LOG(ERROR) << "get partition info failed! Not found partition:" << partitionName; return false; diff --git a/services/ptable_parse/ptable.h b/services/ptable_parse/ptable.h index 970060fd..f25fa585 100644 --- a/services/ptable_parse/ptable.h +++ b/services/ptable_parse/ptable.h @@ -34,6 +34,13 @@ public: static constexpr uint32_t GPT_PARTITION_TYPE_GUID_LEN = 16; static constexpr const char *PREFIX_SYS_CLASS_BLOCK = "/sys/class/block/sd"; + static constexpr const char *PARTITION_NORMAL_TYPE = "normal"; + static constexpr const char *PARTITION_AB_TYPE = "ab"; + static constexpr const char *PARTITION_A_SUFFIX = "_a"; + static constexpr const char *PARTITION_B_SUFFIX = "_b"; + static_assert(std::char_traits::length(PARTITION_A_SUFFIX) == + std::char_traits::length(PARTITION_B_SUFFIX), "a suffix should equal with b"); + static constexpr size_t PARTITION_AB_SUFFIX_SIZE = std::char_traits::length(PARTITION_A_SUFFIX); struct PtnInfo { uint64_t startAddr {}; @@ -43,6 +50,7 @@ public: int gptEntryBufOffset {}; bool isTailPart {false}; std::string dispName {}; + std::string partType {PARTITION_NORMAL_TYPE}; std::string writeMode {"WRITE_RAW"}; std::string writePath {}; }; @@ -222,6 +230,7 @@ public: bool ChangeGpt(uint8_t *gptBuf, uint64_t gptSize, GptParseInfo gptInfo, PtnInfo &modifyInfo); bool AdjustGpt(uint8_t *ptnInfoBuf, uint64_t bufSize, const std::string &ptnName, uint64_t preLastLBA, uint64_t lastPtnLastLBA); + void SetPartitionType(const std::string &partName, PtnInfo &ptnInfo); private: static constexpr uint64_t MBR_MAGIC_NUM_POS = 0x1FE; diff --git a/services/ptable_parse/ufs_ptable.cpp b/services/ptable_parse/ufs_ptable.cpp index 856bb1ab..4a0989fa 100644 --- a/services/ptable_parse/ufs_ptable.cpp +++ b/services/ptable_parse/ufs_ptable.cpp @@ -192,6 +192,7 @@ void UfsPtable::UfsReadGptEntry(const uint8_t *gptImage, const uint32_t lun, const uint8_t *nameOffset = data + (j * PARTITION_ENTRY_SIZE + GPT_PARTITION_NAME_OFFSET); // 2 bytes for 1 charactor of partition name ParsePartitionName(nameOffset, MAX_GPT_NAME_SIZE, newPtnInfo.dispName, MAX_GPT_NAME_SIZE / 2); + SetPartitionType(newPtnInfo.dispName, newPtnInfo); (void)memcpy_s(newPtnInfo.partitionTypeGuid, sizeof(newPtnInfo.partitionTypeGuid), typeGuid, sizeof(typeGuid)); newPtnInfo.isTailPart = tailPartFlag; diff --git a/test/fuzztest/updatermain_fuzzer/BUILD.gn b/test/fuzztest/updatermain_fuzzer/BUILD.gn index d1ba9a26..8c7f4c87 100644 --- a/test/fuzztest/updatermain_fuzzer/BUILD.gn +++ b/test/fuzztest/updatermain_fuzzer/BUILD.gn @@ -57,6 +57,7 @@ ohos_fuzztest("UpdaterMainFuzzTest") { "bzip2:libbz2", "cJSON:cjson", "init:libbegetutil_static", + "init:libfsmanager_static_real", "libdrm:libdrm", "lz4:liblz4_static", "openssl:libcrypto_shared", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 727089de..d3acde58 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -169,6 +169,7 @@ ohos_unittest("updater_unittest") { "googletest:gtest_main", "hilog:libhilog_base", "init:libbegetutil_static", + "init:libfsmanager_static_real", "libuv:uv", "lz4:liblz4_static", "openssl:libcrypto_shared", diff --git a/test/unittest/service_test/BUILD.gn b/test/unittest/service_test/BUILD.gn index 207e9383..0d08aaeb 100644 --- a/test/unittest/service_test/BUILD.gn +++ b/test/unittest/service_test/BUILD.gn @@ -70,6 +70,7 @@ ohos_unittest("updater_service_unittest") { "cJSON:cjson", "hilog:libhilog", "init:libbegetutil_static", + "init:libfsmanager_static_real", "libdrm:libdrm", "openssl:libcrypto_shared", "openssl:libssl_shared", diff --git a/test/unittest/utils/BUILD.gn b/test/unittest/utils/BUILD.gn index 01ccce86..6d0a4a8f 100644 --- a/test/unittest/utils/BUILD.gn +++ b/test/unittest/utils/BUILD.gn @@ -47,6 +47,7 @@ ohos_unittest("utils_test") { "googletest:gtest_main", "hilog:libhilog", "init:libbegetutil_static", + "init:libfsmanager_static_real", "lz4:liblz4_static", "openssl:libcrypto_shared", "openssl:libssl_static", diff --git a/utils/include/utils.h b/utils/include/utils.h index 11170dda..03fb60e7 100644 --- a/utils/include/utils.h +++ b/utils/include/utils.h @@ -24,6 +24,11 @@ #include namespace Updater { +enum SlotType { + NOT_AB, + SLOT_A, + SLOT_B, +}; namespace Utils { constexpr int N_BIN = 2; constexpr int N_OCT = 8; @@ -109,6 +114,7 @@ bool ConvertToLong(const std::string &str, int32_t &value); bool ConvertToUnsignedLong(const std::string &str, uint32_t &value, int base = 0); bool ConvertToDouble(const std::string &str, double &value); bool ConvertToFloat(const std::string &str, float &value); +SlotType GetABDevCurrentSlot(void); #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 854e135c..d117e7f7 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -41,6 +41,7 @@ #include "securec.h" #include "updater/updater_const.h" #include "scope_guard.h" +#include "fs_manager.h" namespace Updater { using namespace Hpackage; @@ -1052,6 +1053,14 @@ bool ConvertToFloat(const std::string &str, float &value) return true; } +bool ConvertToFloat(const std::string &str, float &value) +{ + if (GetBootSlots() <= 1) { + return NOT_AB; + } + return GetCurrentSlot() == 1 ? SLOT_A : SLOT_B; +} + #ifndef __WIN32 void SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode) { -- Gitee From 37e54e72cc3c79f42142fc7f79af55b4b4368c2e Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 3 Mar 2025 09:27:34 +0000 Subject: [PATCH 2/6] update services/ptable_parse/ptable.cpp. Signed-off-by: LeonShu --- services/ptable_parse/ptable.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index fdc190d2..d44b2aea 100644 --- a/services/ptable_parse/ptable.cpp +++ b/services/ptable_parse/ptable.cpp @@ -485,7 +485,7 @@ void Ptable::SetPartitionType(const std::string &partName, PtnInfo &ptnInfo) ptnInfo.partType = PARTITION_NORMAL_TYPE; return; } - std::strint partSuffix = partName.substr(partName.size() - PARTITION_AB_SUFFIX_SIZE, + std::string partSuffix = partName.substr(partName.size() - PARTITION_AB_SUFFIX_SIZE, PARTITION_AB_SUFFIX_SIZE); if (strcasecmp(partSuffix.c_str(), PARTITION_A_SUFFIX) == 0 || strcasecmp(partSuffix.c_str(), PARTITION_B_SUFFIX) == 0) { @@ -554,12 +554,12 @@ bool Ptable::GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptn } } return false; - } + }; if (findPart(partitionName)) { LOG(INFO) << "find partition name " << partitionName; return true; } - std::string partitioNameAB = partitionName; + std::string partitionNameAB = partitionName; if (Utils::IsUpdaterMode()) { partitionNameAB += PARTITION_A_SUFFIX; } else { -- Gitee From 05b69cbb32380bf73f8f9a6c6aaf07bacbf44884 Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 3 Mar 2025 11:57:26 +0000 Subject: [PATCH 3/6] update utils/utils.cpp. Signed-off-by: LeonShu --- utils/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.cpp b/utils/utils.cpp index d117e7f7..d037e2b5 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -1053,7 +1053,7 @@ bool ConvertToFloat(const std::string &str, float &value) return true; } -bool ConvertToFloat(const std::string &str, float &value) +bool GetABDevCurrentSlot(const std::string &str, float &value) { if (GetBootSlots() <= 1) { return NOT_AB; -- Gitee From 07626f6f5284f99cea978255f762dda678ff04c1 Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 3 Mar 2025 11:58:29 +0000 Subject: [PATCH 4/6] update utils/utils.cpp. Signed-off-by: LeonShu --- utils/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.cpp b/utils/utils.cpp index d037e2b5..f4b92702 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -1053,7 +1053,7 @@ bool ConvertToFloat(const std::string &str, float &value) return true; } -bool GetABDevCurrentSlot(const std::string &str, float &value) +bool GetABDevCurrentSlot(void) { if (GetBootSlots() <= 1) { return NOT_AB; -- Gitee From 8a6c75e354318b95c5017005459299ec5ece7d52 Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 3 Mar 2025 12:10:37 +0000 Subject: [PATCH 5/6] update services/ptable_parse/ptable.cpp. Signed-off-by: LeonShu --- services/ptable_parse/ptable.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index d44b2aea..75210d99 100644 --- a/services/ptable_parse/ptable.cpp +++ b/services/ptable_parse/ptable.cpp @@ -561,11 +561,11 @@ bool Ptable::GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptn } std::string partitionNameAB = partitionName; if (Utils::IsUpdaterMode()) { - partitionNameAB += PARTITION_A_SUFFIX; + partitionNameAB += (Utils::GetABDevCurrentSlot() == SLOT_A ? PARTITION_A_SUFFIX : PARTITION_B_SUFFIX); } else { partitionNameAB += (Utils::GetABDevCurrentSlot() == SLOT_A ? PARTITION_B_SUFFIX : PARTITION_A_SUFFIX); } - if (findPart(partitionNameAB) { + if (findPart(partitionNameAB)) { LOG(INFO) << "find partitionAB name " << partitionNameAB; return true; } -- Gitee From 54bf7ea732aa8e3e2d92acf349c2bc63ea9ab0b4 Mon Sep 17 00:00:00 2001 From: LeonShu Date: Mon, 3 Mar 2025 12:22:05 +0000 Subject: [PATCH 6/6] update utils/utils.cpp. Signed-off-by: LeonShu --- utils/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.cpp b/utils/utils.cpp index f4b92702..965758d5 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -1053,7 +1053,7 @@ bool ConvertToFloat(const std::string &str, float &value) return true; } -bool GetABDevCurrentSlot(void) +SlotType GetABDevCurrentSlot(void) { if (GetBootSlots() <= 1) { return NOT_AB; -- Gitee