diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index 4cb8e2869e95ebcfce8030e2cdbcd849f7a71e4f..75210d99394bc3e925dfce08332c4dda3cdacf8f 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::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 +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 partitionNameAB = partitionName; + if (Utils::IsUpdaterMode()) { + 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)) { + 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; diff --git a/test/fuzztest/updatermain_fuzzer/BUILD.gn b/test/fuzztest/updatermain_fuzzer/BUILD.gn index d1ba9a26a546e9d1846b8e52f167c3da29613382..8c7f4c8710d74c025f13149b32c481e146591d14 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 727089dece428696d5d4e4b5f3d785a8119f1296..d3acde5801388a122966bf2ea17f687b4882a3a0 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 207e9383f138884137f9ec0f8ae20e90ccea2e5f..0d08aaebefcf72ed1dd1158b08a5d6c51fa20d39 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 01ccce86c8b073410372a065fbddd6f92189da4d..6d0a4a8fb751ac48cb41ed470e187bf8d354baa3 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 11170ddac0cabf48a86ecee9f8685adddfa3366c..03fb60e731822f1c542416960e8ab654d39ab296 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 854e135cf1671e66e9cf9df74e783c1cbda47c17..965758d5efcb39787cae6cba3a8ba925c335f74d 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; } +SlotType GetABDevCurrentSlot(void) +{ + 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) {