diff --git a/services/ptable_parse/BUILD.gn b/services/ptable_parse/BUILD.gn index b54c369eb01d81e7e3e263b58f5cf4675e58e3cb..6a2a705a0960dcb7d07b0a2efecbb1a233222bd2 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 4cb8e2869e95ebcfce8030e2cdbcd849f7a71e4f..5484d245d77c1ac337050e7262870b1d916d12c7 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=" << ptnInfo[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 970060fd6e6963954ed15df35d4e25cbd2569407..f25fa585a48b1672111726592034c965ffcfcd3d 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 856bb1abd739fcff44def61a965167a9c0b93b2c..4a0989fa91451f08b0d2dc9788211224011778ff 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;