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 #define LAST_PATITION_NAME "USERDATA" 19 20 #include "macros_updater.h" 21 #include "json_node.h" 22 23 namespace Updater { 24 #ifndef UPDATER_UT 25 constexpr const char *PTABLE_TEMP_PATH = "/tmp/update_ptable.img"; 26 #else 27 constexpr const char *PTABLE_TEMP_PATH = "/data/update/update_ptable.img"; 28 #endif 29 class Ptable { 30 public: 31 Ptable() = default; 32 DISALLOW_COPY_MOVE(Ptable); ~Ptable()33 virtual ~Ptable() {} 34 35 static constexpr uint32_t GPT_PARTITION_TYPE_GUID_LEN = 16; 36 static constexpr const char *PREFIX_SYS_CLASS_BLOCK = "/sys/class/block/sd"; 37 static constexpr const char *PARTITION_NORMAL_TYPE = "normal"; 38 static constexpr const char *PARTITION_AB_TYPE = "ab"; 39 static constexpr const char *PARTITION_A_SUFFIX = "_a"; 40 static constexpr const char *PARTITION_B_SUFFIX = "_b"; 41 static_assert(std::char_traits<char>::length(PARTITION_A_SUFFIX) == 42 std::char_traits<char>::length(PARTITION_B_SUFFIX), "a suffix should equal with b"); 43 static constexpr size_t PARTITION_AB_SUFFIX_SIZE = std::char_traits<char>::length(PARTITION_A_SUFFIX); 44 45 struct PtnInfo { 46 uint64_t startAddr {}; 47 uint64_t partitionSize {}; 48 uint8_t partitionTypeGuid[GPT_PARTITION_TYPE_GUID_LEN] {}; 49 uint32_t lun {}; 50 int gptEntryBufOffset {}; 51 bool isTailPart {false}; 52 std::string dispName {}; 53 std::string partType {PARTITION_NORMAL_TYPE}; 54 std::string writeMode {"WRITE_RAW"}; 55 std::string writePath {}; 56 }; 57 58 struct GptParseInfo { GptParseInfoGptParseInfo59 GptParseInfo(uint64_t imgBlockSize, uint64_t devBlockSize, uint64_t devDensity) 60 : imgBlockSize(imgBlockSize), devBlockSize(devBlockSize), devDensity(devDensity) {} 61 uint64_t imgBlockSize; 62 uint64_t devBlockSize; 63 uint64_t devDensity; 64 }; 65 std::vector<PtnInfo> GetPtablePartitionInfo() const; 66 uint32_t GetPtablePartitionNum() const; 67 void SetReservedSize(uint64_t reservedSize); 68 bool InitPtable(); 69 uint32_t GetDefaultImageSize() const; 70 void PrintPtableInfo() const; 71 void PrintPtableInfo(const std::vector<PtnInfo> &ptnInfo) const; 72 bool GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptnInfo, int32_t &index); 73 std::vector<PtnInfo>& GetPtablePartitionInfoInstance(); 74 bool LoadPtnInfo(const std::vector<PtnInfo>& ptnInfo); 75 bool WritePartitionBufToFile(uint8_t *ptbImgBuffer, const uint32_t imgBufSize); 76 bool ReadPartitionFileToBuffer(uint8_t *ptbImgBuffer, uint32_t &imgBufSize); 77 void DeletePartitionTmpFile(); 78 79 virtual bool ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t imgBufSize) = 0; 80 virtual bool LoadPtableFromDevice() = 0; 81 virtual bool WritePartitionTable() = 0; 82 virtual bool EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::vector<PtnInfo> &modifyList) = 0; 83 virtual bool GetPtableImageBuffer(uint8_t *imageBuf, const uint32_t imgBufSize) = 0; AddChildPtable(std::unique_ptr<Ptable> child)84 virtual void AddChildPtable(std::unique_ptr<Ptable> child) {} 85 virtual bool CorrectBufByPtnList(uint8_t *imageBuf, uint64_t imgBufSize, const std::vector<PtnInfo> &srcInfo, 86 const std::vector<PtnInfo> &dstInfo); 87 GetEndPtnIndex()88 int GetEndPtnIndex() 89 { 90 return endPtnIndex_; 91 } 92 GetUsrDataPtnIndex()93 int GetUsrDataPtnIndex() 94 { 95 return usrDataPtnIndex_; 96 } 97 SetUsrDataPtnIndex(int usrDataPtnIndex)98 void SetUsrDataPtnIndex(int usrDataPtnIndex) 99 { 100 usrDataPtnIndex_ = usrDataPtnIndex; 101 } 102 HasTailPart()103 bool HasTailPart() 104 { 105 return hasTailpart_; 106 } 107 ClearPtnInfo()108 void ClearPtnInfo() 109 { 110 partitionInfo_.clear(); 111 } 112 113 #ifndef UPDATER_UT 114 protected: 115 #else 116 public: 117 #endif 118 const std::string USERDATA_PARTITION = "USERDATA"; 119 static constexpr uint32_t PARTITION_ENTRY_SIZE = 128; 120 static constexpr uint32_t MAX_PARTITION_NUM = 128; 121 static constexpr uint32_t FIRST_LBA_OFFSET = 32; 122 static constexpr uint32_t LAST_LBA_OFFSET = 40; 123 static constexpr uint32_t GPT_PARTITION_NAME_OFFSET = 56; 124 static constexpr uint32_t MAX_GPT_NAME_SIZE = 72; 125 static constexpr uint32_t PARTITION_ENTRIES_OFFSET = 72; 126 static constexpr uint32_t PARTITION_CRC_OFFSET = 88; 127 static constexpr uint32_t GPT_DISP_NAME_LEN = 32; 128 static constexpr uint64_t DEFAULT_SECTOR_NUM = (4 * 1024 * 1024 * 2 - 1); 129 static constexpr uint32_t GPT_HEADER_OFFSET = 24; 130 static constexpr uint32_t BACKUP_HEADER_OFFSET = 32; 131 static constexpr uint32_t PARTITION_ENTRY_OFFSET = 72; 132 static constexpr uint32_t LAST_USABLE_LBA_OFFSET = 48; 133 static constexpr uint32_t PARTITION_ENTRY_LAST_LBA = 40; 134 static constexpr uint32_t PARTITION_COUNT_OFFSET = 80; 135 static constexpr uint32_t PENTRY_SIZE_OFFSET = 84; 136 static constexpr uint32_t HEADER_CRC_OFFSET = 16; 137 static constexpr uint32_t GPT_CRC_LEN = 92; 138 static constexpr uint32_t GPT_ENTRYS_SIZE = 128 * 128; 139 140 // set 32 bits data PUT_LONG(uint8_t * x,const uint32_t y)141 inline void PUT_LONG(uint8_t *x, const uint32_t y) 142 { 143 *(x) = (y) & 0xff; 144 *((x) + 1) = ((y) >> 8) & 0xff; 145 *((x) + 2) = ((y) >> 16) & 0xff; 146 *((x) + 3) = ((y) >> 24) & 0xff; 147 } 148 149 // set 64 bits data PUT_LONG_LONG(uint8_t * x,const uint64_t y)150 inline void PUT_LONG_LONG(uint8_t *x, const uint64_t y) 151 { 152 *(x) = (y) & 0xff; 153 *((x) + 1) = (((y) >> 8) & 0xff); 154 *((x) + 2) = (((y) >> 16) & 0xff); 155 *((x) + 3) = (((y) >> 24) & 0xff); 156 *((x) + 4) = (((y) >> 32) & 0xff); 157 *((x) + 5) = (((y) >> 40) & 0xff); 158 *((x) + 6) = (((y) >> 48) & 0xff); 159 *((x) + 7) = (((y) >> 56) & 0xff); 160 } 161 162 // LWORD = 4 bytes (32 bits) GET_LWORD_FROM_BYTE(const uint8_t * x)163 inline uint32_t GET_LWORD_FROM_BYTE(const uint8_t *x) 164 { 165 uint32_t res = static_cast<unsigned int>(*x) | 166 (static_cast<unsigned int>(*(x + 1)) << 8) | 167 (static_cast<unsigned int>(*(x + 2)) << 16) | 168 (static_cast<unsigned int>(*(x + 3)) << 24); 169 return res; 170 } 171 172 // LLWORD = 8 bytes (64 bits) GET_LLWORD_FROM_BYTE(const uint8_t * x)173 inline uint64_t GET_LLWORD_FROM_BYTE(const uint8_t *x) 174 { 175 uint64_t res = static_cast<unsigned long long>(*x) | 176 (static_cast<unsigned long long>(*(x + 1)) << 8) | 177 (static_cast<unsigned long long>(*(x + 2)) << 16) | 178 (static_cast<unsigned long long>(*(x + 3)) << 24) | 179 (static_cast<unsigned long long>(*(x + 4)) << 32) | 180 (static_cast<unsigned long long>(*(x + 5)) << 40) | 181 (static_cast<unsigned long long>(*(x + 6)) << 48) | 182 (static_cast<unsigned long long>(*(x + 7)) << 56); 183 return res; 184 } 185 186 struct GPTHeaderInfo { 187 uint32_t headerSize {}; 188 uint32_t partitionEntrySize {}; 189 uint32_t maxPartitionCount {}; 190 uint64_t firstUsableLba {}; 191 }; 192 193 struct PtableData { 194 bool dataValid {}; 195 uint32_t emmcGptDataLen {}; 196 uint32_t lbaLen {}; 197 uint32_t gptHeaderLen {}; 198 uint32_t blockSize {}; 199 uint32_t imgLuSize {}; 200 uint32_t startLunNumber {}; 201 uint32_t writeDeviceLunSize {}; 202 uint32_t defaultLunNum {}; 203 }; 204 205 std::vector<PtnInfo> partitionInfo_; 206 PtableData ptableData_; 207 uint64_t reservedSize_ {0}; 208 209 int startPtnIndex_ {-1}; 210 int endPtnIndex_ {-1}; 211 int usrDataPtnIndex_ {-1}; 212 bool hasTailpart_ {false}; 213 214 PtableData GetPtableData() const; 215 bool MemReadWithOffset(const std::string &filePath, const uint64_t offset, 216 uint8_t *outData, const uint32_t dataSize); 217 bool CheckProtectiveMbr(const uint8_t *gptImage, const uint32_t imgLen); 218 bool CheckIfValidGpt(const uint8_t *gptImage, const uint32_t gptImageLen); 219 bool GetCapacity(const std::string &filePath, uint64_t &lunCapacity); 220 bool GetPartitionGptHeaderInfo(const uint8_t *buffer, const uint32_t bufferLen, GPTHeaderInfo& gptHeaderInfo); 221 void PatchBackUpGptHeader(uint8_t *gptHeader, const uint32_t len, uint64_t backGptEntryStart); 222 bool PartitionCheckGptHeader(const uint8_t *gptImage, const uint32_t len, const uint64_t lbaNum, 223 const uint32_t blockSize, GPTHeaderInfo& gptHeaderInfo); 224 void ParsePartitionName(const uint8_t *data, const uint32_t dataLen, 225 std::string &name, const uint32_t nameLen); 226 uint32_t CalculateCrc32(const uint8_t *buffer, const uint32_t len); 227 bool WritePtablePartition(const std::string &path, uint64_t offset, const uint8_t *buffer, uint32_t size); 228 bool CheckFileExist(const std::string &fileName); 229 bool WriteBufferToPath(const std::string &path, const uint64_t offset, const uint8_t *buffer, const uint32_t size); 230 bool ChangeGpt(uint8_t *gptBuf, uint64_t gptSize, GptParseInfo gptInfo, PtnInfo &modifyInfo); 231 bool AdjustGpt(uint8_t *ptnInfoBuf, uint64_t bufSize, const std::string &ptnName, uint64_t preLastLBA, 232 uint64_t lastPtnLastLBA); 233 void SetPartitionType(const std::string &partName, PtnInfo &ptnInfo); 234 235 private: 236 static constexpr uint64_t MBR_MAGIC_NUM_POS = 0x1FE; 237 static constexpr uint8_t MBR_MAGIC_NUM_0 = 0x55; 238 static constexpr uint8_t MBR_MAGIC_NUM_1 = 0xAA; 239 static constexpr uint32_t MBR_GPT_MAX_NUM = 4; // one disk has most 4 main partitions 240 static constexpr uint32_t MBR_GPT_ENTRY = 0x1BE; 241 static constexpr uint32_t MBR_GPT_ENTRY_SIZE = 0x010; 242 static constexpr uint32_t GPT_TYPE_SIGN_OFFSET = 0x04; 243 static constexpr uint32_t MBR_PROTECTIVE_GPT_TYPE = 0xEE; 244 static constexpr uint64_t EFI_MAGIC_NUMBER = 0x5452415020494645; // GPT SIGNATURE(8 bytes), little-end, (EFI PART) 245 static constexpr uint32_t SECTOR_SIZE = 512; 246 static constexpr uint32_t LBA_LENGTH = SECTOR_SIZE; 247 static constexpr uint32_t HEADER_SIZE_OFFSET = 12; 248 static constexpr uint32_t FIRST_USABLE_LBA_OFFSET = 40; 249 static constexpr uint32_t GPT_HEADER_SIZE = 92; 250 static constexpr uint32_t PRIMARY_HEADER_OFFSET = 24; 251 static constexpr uint32_t MIN_PARTITION_ARRAY_SIZE = 0x4000; 252 253 bool VerifyMbrMagicNum(const uint8_t *buffer, const uint32_t size); 254 uint32_t Reflect(uint32_t data, const uint32_t len); 255 256 bool CheckGptHeader(uint8_t *buffer, const uint32_t bufferLen, const uint64_t lbaNum, 257 const GPTHeaderInfo& gptHeaderInfo); 258 void SetPartitionName(const std::string &name, uint8_t *data, const uint32_t size); 259 bool ParsePtableDataNode(const JsonNode &ptableDataNode); 260 bool ParsePtableData(); 261 }; 262 } // namespace Updater 263 #endif // UPDATER_PTABLE_H 264