1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UPDATER_PTABLE_H 17 #define UPDATER_PTABLE_H 18 19 #include "macros.h" 20 #include "json_node.h" 21 22 namespace Updater { 23 class Ptable { 24 public: 25 Ptable() = default; 26 DISALLOW_COPY_MOVE(Ptable); ~Ptable()27 virtual ~Ptable() {} 28 29 static constexpr uint32_t GPT_PARTITION_TYPE_GUID_LEN = 16; 30 static constexpr const char *PREFIX_SYS_CLASS_BLOCK = "/sys/class/block/sd"; 31 32 struct PtnInfo { 33 uint64_t startAddr {}; 34 uint64_t partitionSize {}; 35 uint8_t partitionTypeGuid[GPT_PARTITION_TYPE_GUID_LEN] {}; 36 uint32_t lun {}; 37 std::string dispName {}; 38 }; 39 40 std::vector<PtnInfo> GetPtablePartitionInfo() const; 41 uint32_t GetPtablePartitionNum() const; 42 bool InitPtable(); 43 uint32_t GetDefaultImageSize() const; 44 void PrintPtableInfo() const; 45 void PrintPtableInfo(const std::vector<PtnInfo> &ptnInfo) const; 46 bool GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptnInfo, int32_t &index); 47 48 virtual bool ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t imgBufSize) = 0; 49 virtual bool LoadPtableFromDevice() = 0; 50 virtual bool WritePartitionTable() = 0; 51 52 protected: 53 const std::string USERDATA_PARTITION = "USERDATA"; 54 static constexpr uint32_t PARTITION_ENTRY_SIZE = 128; 55 static constexpr uint32_t MAX_PARTITION_NUM = 128; 56 static constexpr uint32_t FIRST_LBA_OFFSET = 32; 57 static constexpr uint32_t LAST_LBA_OFFSET = 40; 58 static constexpr uint32_t GPT_PARTITION_NAME_OFFSET = 56; 59 static constexpr uint32_t MAX_GPT_NAME_SIZE = 72; 60 static constexpr uint32_t PARTITION_ENTRIES_OFFSET = 72; 61 static constexpr uint32_t PARTITION_CRC_OFFSET = 88; 62 static constexpr uint32_t GPT_DISP_NAME_LEN = 32; 63 static constexpr uint64_t DEFAULT_SECTOR_NUM = (4 * 1024 * 1024 * 2 - 1); 64 static constexpr uint32_t BACKUP_HEADER_OFFSET = 32; 65 static constexpr uint32_t LAST_USABLE_LBA_OFFSET = 48; 66 static constexpr uint32_t PARTITION_ENTRY_LAST_LBA = 40; 67 static constexpr uint32_t PARTITION_COUNT_OFFSET = 80; 68 static constexpr uint32_t PENTRY_SIZE_OFFSET = 84; 69 static constexpr uint32_t HEADER_CRC_OFFSET = 16; 70 static constexpr uint32_t GPT_CRC_LEN = 92; 71 static constexpr uint32_t GPT_ENTRYS_SIZE = 128 * 128; 72 73 // set 32 bits data PUT_LONG(uint8_t * x,const uint32_t y)74 inline void PUT_LONG(uint8_t *x, const uint32_t y) 75 { 76 *(x) = (y) & 0xff; 77 *((x) + 1) = ((y) >> 8) & 0xff; 78 *((x) + 2) = ((y) >> 16) & 0xff; 79 *((x) + 3) = ((y) >> 24) & 0xff; 80 } 81 82 // set 64 bits data PUT_LONG_LONG(uint8_t * x,const uint64_t y)83 inline void PUT_LONG_LONG(uint8_t *x, const uint64_t y) 84 { 85 *(x) = (y) & 0xff; 86 *((x) + 1) = (((y) >> 8) & 0xff); 87 *((x) + 2) = (((y) >> 16) & 0xff); 88 *((x) + 3) = (((y) >> 24) & 0xff); 89 *((x) + 4) = (((y) >> 32) & 0xff); 90 *((x) + 5) = (((y) >> 40) & 0xff); 91 *((x) + 6) = (((y) >> 48) & 0xff); 92 *((x) + 7) = (((y) >> 56) & 0xff); 93 } 94 95 // LWORD = 4 bytes (32 bits) GET_LWORD_FROM_BYTE(const uint8_t * x)96 inline uint32_t GET_LWORD_FROM_BYTE(const uint8_t *x) 97 { 98 uint32_t res = static_cast<unsigned int>(*x) | 99 (static_cast<unsigned int>(*(x + 1)) << 8) | 100 (static_cast<unsigned int>(*(x + 2)) << 16) | 101 (static_cast<unsigned int>(*(x + 3)) << 24); 102 return res; 103 } 104 105 // LLWORD = 8 bytes (64 bits) GET_LLWORD_FROM_BYTE(const uint8_t * x)106 inline uint64_t GET_LLWORD_FROM_BYTE(const uint8_t *x) 107 { 108 uint64_t res = static_cast<unsigned long long>(*x) | 109 (static_cast<unsigned long long>(*(x + 1)) << 8) | 110 (static_cast<unsigned long long>(*(x + 2)) << 16) | 111 (static_cast<unsigned long long>(*(x + 3)) << 24) | 112 (static_cast<unsigned long long>(*(x + 4)) << 32) | 113 (static_cast<unsigned long long>(*(x + 5)) << 40) | 114 (static_cast<unsigned long long>(*(x + 6)) << 48) | 115 (static_cast<unsigned long long>(*(x + 7)) << 56); 116 return res; 117 } 118 119 struct GPTHeaderInfo { 120 uint32_t headerSize {}; 121 uint32_t partitionEntrySize {}; 122 uint32_t maxPartitionCount {}; 123 uint64_t firstUsableLba {}; 124 }; 125 126 struct PtableData { 127 bool dataValid {}; 128 uint32_t emmcGptDataLen {}; 129 uint32_t lbaLen {}; 130 uint32_t gptHeaderLen {}; 131 uint32_t blockSize {}; 132 uint32_t imgLuSize {}; 133 uint32_t startLunNumber {}; 134 uint32_t writeDeviceLunSize {}; 135 uint32_t defaultLunNum {}; 136 }; 137 138 std::vector<PtnInfo> partitionInfo_; 139 PtableData ptableData_; 140 141 PtableData GetPtableData() const; 142 bool MemReadWithOffset(const std::string &filePath, const uint64_t offset, 143 uint8_t *outData, const uint32_t dataSize); 144 bool CheckProtectiveMbr(const uint8_t *gptImage, const uint32_t imgLen); 145 bool CheckIfValidGpt(const uint8_t *gptImage, const uint32_t gptImageLen); 146 bool GetCapacity(const std::string &filePath, uint64_t &lunCapacity); 147 bool GetPartitionGptHeaderInfo(const uint8_t *buffer, const uint32_t bufferLen, GPTHeaderInfo& gptHeaderInfo); 148 bool PartitionCheckGptHeader(const uint8_t *gptImage, const uint32_t len, const uint64_t lbaNum, 149 const uint32_t blockSize, const GPTHeaderInfo& gptHeaderInfo); 150 void ParsePartitionName(const uint8_t *data, const uint32_t dataLen, 151 std::string &name, const uint32_t nameLen); 152 uint32_t CalculateCrc32(const uint8_t *buffer, const uint32_t len); 153 bool WritePtablePartition(const std::string &path, uint64_t offset, const uint8_t *buffer, uint32_t size); 154 bool CheckFileExist(const std::string &fileName); 155 bool WriteBufferToPath(const std::string &path, const uint64_t offset, const uint8_t *buffer, const uint32_t size); 156 157 private: 158 static constexpr uint64_t MBR_MAGIC_NUM_POS = 0x1FE; 159 static constexpr uint8_t MBR_MAGIC_NUM_0 = 0x55; 160 static constexpr uint8_t MBR_MAGIC_NUM_1 = 0xAA; 161 static constexpr uint32_t MBR_GPT_MAX_NUM = 4; // one disk has most 4 main partitions 162 static constexpr uint32_t MBR_GPT_ENTRY = 0x1BE; 163 static constexpr uint32_t MBR_GPT_ENTRY_SIZE = 0x010; 164 static constexpr uint32_t GPT_TYPE_SIGN_OFFSET = 0x04; 165 static constexpr uint32_t MBR_PROTECTIVE_GPT_TYPE = 0xEE; 166 static constexpr uint64_t EFI_MAGIC_NUMBER = 0x5452415020494645; // GPT SIGNATURE(8 bytes), little-end, (EFI PART) 167 static constexpr uint32_t SECTOR_SIZE = 512; 168 static constexpr uint32_t LBA_LENGTH = SECTOR_SIZE; 169 static constexpr uint32_t HEADER_SIZE_OFFSET = 12; 170 static constexpr uint32_t FIRST_USABLE_LBA_OFFSET = 40; 171 static constexpr uint32_t GPT_HEADER_SIZE = 92; 172 static constexpr uint32_t PRIMARY_HEADER_OFFSET = 24; 173 static constexpr uint32_t MIN_PARTITION_ARRAY_SIZE = 0x4000; 174 175 bool VerifyMbrMagicNum(const uint8_t *buffer, const uint32_t size); 176 uint32_t Reflect(uint32_t data, const uint32_t len); 177 178 bool CheckGptHeader(uint8_t *buffer, const uint32_t bufferLen, const uint64_t lbaNum, 179 const GPTHeaderInfo& gptHeaderInfo); 180 void SetPartitionName(const std::string &name, uint8_t *data, const uint32_t size); 181 bool ParsePtableDataNode(const JsonNode &ptableDataNode); 182 bool ParsePtableData(); 183 }; 184 } // namespace Updater 185 #endif // UPDATER_PTABLE_H 186