From 862958757dfe145add11424c534d6419d116976d Mon Sep 17 00:00:00 2001 From: LeonShu Date: Tue, 4 Mar 2025 14:15:55 +0800 Subject: [PATCH 1/3] for ab updater Signed-off-by: LeonShu --- services/ptable_parse/BUILD.gn | 1 + services/ptable_parse/ptable.cpp | 51 +++++++++++++++++++++++----- services/ptable_parse/ptable.h | 9 +++++ services/ptable_parse/ufs_ptable.cpp | 1 + 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/services/ptable_parse/BUILD.gn b/services/ptable_parse/BUILD.gn index b54c369e..6a2a705a 100644 --- a/services/ptable_parse/BUILD.gn +++ b/services/ptable_parse/BUILD.gn @@ -63,6 +63,7 @@ ohos_static_library("libptableparse") { "bounds_checking_function:libsec_static", "bzip2:libbz2", "cJSON:cjson_static", + "init:libfsmanager_static_real", ] part_name = "updater" diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index 4cb8e286..b8f290da 100644 --- a/services/ptable_parse/ptable.cpp +++ b/services/ptable_parse/ptable.cpp @@ -23,6 +23,7 @@ #include "log/log.h" #include "securec.h" #include "utils.h" +#include "fs_manager.h" namespace Updater { constexpr const char *PTABLE_CONFIG_PATH = "/etc/ptable_data.json"; @@ -456,7 +457,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 +475,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::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) { + 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 +545,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 partitionNameAB = partitionName; + if (Utils::IsUpdaterMode()) { + partitionNameAB += (GetCurrentSlot() == 1 ? PARTITION_A_SUFFIX : PARTITION_B_SUFFIX); + } else { + partitionNameAB += (GetCurrentSlot() == 1 ? 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; -- Gitee From 3ee43edb6edb497c958e52365fef9fcd654b0f25 Mon Sep 17 00:00:00 2001 From: LeonShu Date: Tue, 4 Mar 2025 06:49:12 +0000 Subject: [PATCH 2/3] update services/ptable_parse/ptable.cpp. Signed-off-by: LeonShu --- services/ptable_parse/ptable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index b8f290da..e7d44f78 100644 --- a/services/ptable_parse/ptable.cpp +++ b/services/ptable_parse/ptable.cpp @@ -566,7 +566,7 @@ bool Ptable::GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptn } else { partitionNameAB += (GetCurrentSlot() == 1 ? PARTITION_B_SUFFIX : PARTITION_A_SUFFIX); } - if (findPart(partitionNameAB) { + if (findPart(partitionNameAB)) { LOG(INFO) << "find partitionAB name " << partitionNameAB; return true; } -- Gitee From b603555c22bbd3249f698b4825f911c9a43198cb Mon Sep 17 00:00:00 2001 From: LeonShu Date: Tue, 4 Mar 2025 11:37:07 +0000 Subject: [PATCH 3/3] update services/ptable_parse/ptable.cpp. Signed-off-by: LeonShu --- services/ptable_parse/ptable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index e7d44f78..5484d245 100644 --- a/services/ptable_parse/ptable.cpp +++ b/services/ptable_parse/ptable.cpp @@ -475,7 +475,7 @@ 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 << ", partType=" << partitionInfo_[i].partType; + std::dec << ptnInfo[i].lun << ", partType=" << ptnInfo[i].partType; } LOG(INFO) << "ptnInfo : ==========================================="; } -- Gitee