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.h" 21 #include "json_node.h" 22 23 namespace Updater { 24 class Ptable { 25 public: 26 Ptable() = default; 27 DISALLOW_COPY_MOVE(Ptable); ~Ptable()28 virtual ~Ptable() {} 29 30 static constexpr uint32_t GPT_PARTITION_TYPE_GUID_LEN = 16; 31 static constexpr const char *PREFIX_SYS_CLASS_BLOCK = "/sys/class/block/sd"; 32 33 struct PtnInfo { 34 uint64_t startAddr {}; 35 uint64_t partitionSize {}; 36 uint8_t partitionTypeGuid[GPT_PARTITION_TYPE_GUID_LEN] {}; 37 uint32_t lun {}; 38 std::string dispName {}; 39 }; 40 41 struct GptParseInfo { GptParseInfoGptParseInfo42 GptParseInfo(uint64_t imgBlockSize, uint64_t devBlockSize, uint64_t devDensity) 43 : imgBlockSize(imgBlockSize), devBlockSize(devBlockSize), devDensity(devDensity) {} 44 uint64_t imgBlockSize; 45 uint64_t devBlockSize; 46 uint64_t devDensity; 47 }; 48 std::vector<PtnInfo> GetPtablePartitionInfo() const; 49 uint32_t GetPtablePartitionNum() const; 50 bool InitPtable(); 51 uint32_t GetDefaultImageSize() const; 52 void PrintPtableInfo() const; 53 void PrintPtableInfo(const std::vector<PtnInfo> &ptnInfo) const; 54 bool GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptnInfo, int32_t &index); 55 std::vector<PtnInfo>& GetPtablePartitionInfoInstance(); 56 bool LoadPtnInfo(const std::vector<PtnInfo>& ptnInfo); 57 58 virtual bool ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t imgBufSize) = 0; 59 virtual bool LoadPtableFromDevice() = 0; 60 virtual bool WritePartitionTable() = 0; 61 virtual bool EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::vector<PtnInfo> &modifyList) = 0; 62 virtual bool GetPtableImageBuffer(uint8_t *imageBuf, const uint32_t imgBufSize) = 0; 63 64 protected: 65 const std::string USERDATA_PARTITION = "USERDATA"; 66 static constexpr uint32_t PARTITION_ENTRY_SIZE = 128; 67 static constexpr uint32_t MAX_PARTITION_NUM = 128; 68 static constexpr uint32_t FIRST_LBA_OFFSET = 32; 69 static constexpr uint32_t LAST_LBA_OFFSET = 40; 70 static constexpr uint32_t GPT_PARTITION_NAME_OFFSET = 56; 71 static constexpr uint32_t MAX_GPT_NAME_SIZE = 72; 72 static constexpr uint32_t PARTITION_ENTRIES_OFFSET = 72; 73 static constexpr uint32_t PARTITION_CRC_OFFSET = 88; 74 static constexpr uint32_t GPT_DISP_NAME_LEN = 32; 75 static constexpr uint64_t DEFAULT_SECTOR_NUM = (4 * 1024 * 1024 * 2 - 1); 76 static constexpr uint32_t BACKUP_HEADER_OFFSET = 32; 77 static constexpr uint32_t LAST_USABLE_LBA_OFFSET = 48; 78 static constexpr uint32_t PARTITION_ENTRY_LAST_LBA = 40; 79 static constexpr uint32_t PARTITION_COUNT_OFFSET = 80; 80 static constexpr uint32_t PENTRY_SIZE_OFFSET = 84; 81 static constexpr uint32_t HEADER_CRC_OFFSET = 16; 82 static constexpr uint32_t GPT_CRC_LEN = 92; 83 static constexpr uint32_t GPT_ENTRYS_SIZE = 128 * 128; 84 85 // set 32 bits data PUT_LONG(uint8_t * x,const uint32_t y)86 inline void PUT_LONG(uint8_t *x, const uint32_t y) 87 { 88 *(x) = (y) & 0xff; 89 *((x) + 1) = ((y) >> 8) & 0xff; 90 *((x) + 2) = ((y) >> 16) & 0xff; 91 *((x) + 3) = ((y) >> 24) & 0xff; 92 } 93 94 // set 64 bits data PUT_LONG_LONG(uint8_t * x,const uint64_t y)95 inline void PUT_LONG_LONG(uint8_t *x, const uint64_t y) 96 { 97 *(x) = (y) & 0xff; 98 *((x) + 1) = (((y) >> 8) & 0xff); 99 *((x) + 2) = (((y) >> 16) & 0xff); 100 *((x) + 3) = (((y) >> 24) & 0xff); 101 *((x) + 4) = (((y) >> 32) & 0xff); 102 *((x) + 5) = (((y) >> 40) & 0xff); 103 *((x) + 6) = (((y) >> 48) & 0xff); 104 *((x) + 7) = (((y) >> 56) & 0xff); 105 } 106 107 // LWORD = 4 bytes (32 bits) GET_LWORD_FROM_BYTE(const uint8_t * x)108 inline uint32_t GET_LWORD_FROM_BYTE(const uint8_t *x) 109 { 110 uint32_t res = static_cast<unsigned int>(*x) | 111 (static_cast<unsigned int>(*(x + 1)) << 8) | 112 (static_cast<unsigned int>(*(x + 2)) << 16) | 113 (static_cast<unsigned int>(*(x + 3)) << 24); 114 return res; 115 } 116 117 // LLWORD = 8 bytes (64 bits) GET_LLWORD_FROM_BYTE(const uint8_t * x)118 inline uint64_t GET_LLWORD_FROM_BYTE(const uint8_t *x) 119 { 120 uint64_t res = static_cast<unsigned long long>(*x) | 121 (static_cast<unsigned long long>(*(x + 1)) << 8) | 122 (static_cast<unsigned long long>(*(x + 2)) << 16) | 123 (static_cast<unsigned long long>(*(x + 3)) << 24) | 124 (static_cast<unsigned long long>(*(x + 4)) << 32) | 125 (static_cast<unsigned long long>(*(x + 5)) << 40) | 126 (static_cast<unsigned long long>(*(x + 6)) << 48) | 127 (static_cast<unsigned long long>(*(x + 7)) << 56); 128 return res; 129 } 130 131 struct GPTHeaderInfo { 132 uint32_t headerSize {}; 133 uint32_t partitionEntrySize {}; 134 uint32_t maxPartitionCount {}; 135 uint64_t firstUsableLba {}; 136 }; 137 138 struct PtableData { 139 bool dataValid {}; 140 uint32_t emmcGptDataLen {}; 141 uint32_t lbaLen {}; 142 uint32_t gptHeaderLen {}; 143 uint32_t blockSize {}; 144 uint32_t imgLuSize {}; 145 uint32_t startLunNumber {}; 146 uint32_t writeDeviceLunSize {}; 147 uint32_t defaultLunNum {}; 148 }; 149 150 std::vector<PtnInfo> partitionInfo_; 151 PtableData ptableData_; 152 153 PtableData GetPtableData() const; 154 bool MemReadWithOffset(const std::string &filePath, const uint64_t offset, 155 uint8_t *outData, const uint32_t dataSize); 156 bool CheckProtectiveMbr(const uint8_t *gptImage, const uint32_t imgLen); 157 bool CheckIfValidGpt(const uint8_t *gptImage, const uint32_t gptImageLen); 158 bool GetCapacity(const std::string &filePath, uint64_t &lunCapacity); 159 bool GetPartitionGptHeaderInfo(const uint8_t *buffer, const uint32_t bufferLen, GPTHeaderInfo& gptHeaderInfo); 160 bool PartitionCheckGptHeader(const uint8_t *gptImage, const uint32_t len, const uint64_t lbaNum, 161 const uint32_t blockSize, GPTHeaderInfo& gptHeaderInfo); 162 void ParsePartitionName(const uint8_t *data, const uint32_t dataLen, 163 std::string &name, const uint32_t nameLen); 164 uint32_t CalculateCrc32(const uint8_t *buffer, const uint32_t len); 165 bool WritePtablePartition(const std::string &path, uint64_t offset, const uint8_t *buffer, uint32_t size); 166 bool CheckFileExist(const std::string &fileName); 167 bool WriteBufferToPath(const std::string &path, const uint64_t offset, const uint8_t *buffer, const uint32_t size); 168 bool ChangeGpt(uint8_t *gptBuf, uint64_t gptSize, GptParseInfo gptInfo, PtnInfo &modifyInfo); 169 bool AdjustGpt(uint8_t *ptnInfoBuf, uint64_t bufSize, const std::string &ptnName, uint64_t preLastLBA, 170 uint64_t lastPtnLastLBA); 171 172 private: 173 static constexpr uint64_t MBR_MAGIC_NUM_POS = 0x1FE; 174 static constexpr uint8_t MBR_MAGIC_NUM_0 = 0x55; 175 static constexpr uint8_t MBR_MAGIC_NUM_1 = 0xAA; 176 static constexpr uint32_t MBR_GPT_MAX_NUM = 4; // one disk has most 4 main partitions 177 static constexpr uint32_t MBR_GPT_ENTRY = 0x1BE; 178 static constexpr uint32_t MBR_GPT_ENTRY_SIZE = 0x010; 179 static constexpr uint32_t GPT_TYPE_SIGN_OFFSET = 0x04; 180 static constexpr uint32_t MBR_PROTECTIVE_GPT_TYPE = 0xEE; 181 static constexpr uint64_t EFI_MAGIC_NUMBER = 0x5452415020494645; // GPT SIGNATURE(8 bytes), little-end, (EFI PART) 182 static constexpr uint32_t SECTOR_SIZE = 512; 183 static constexpr uint32_t LBA_LENGTH = SECTOR_SIZE; 184 static constexpr uint32_t HEADER_SIZE_OFFSET = 12; 185 static constexpr uint32_t FIRST_USABLE_LBA_OFFSET = 40; 186 static constexpr uint32_t GPT_HEADER_SIZE = 92; 187 static constexpr uint32_t PRIMARY_HEADER_OFFSET = 24; 188 static constexpr uint32_t MIN_PARTITION_ARRAY_SIZE = 0x4000; 189 190 bool VerifyMbrMagicNum(const uint8_t *buffer, const uint32_t size); 191 uint32_t Reflect(uint32_t data, const uint32_t len); 192 193 bool CheckGptHeader(uint8_t *buffer, const uint32_t bufferLen, const uint64_t lbaNum, 194 const GPTHeaderInfo& gptHeaderInfo); 195 void SetPartitionName(const std::string &name, uint8_t *data, const uint32_t size); 196 bool ParsePtableDataNode(const JsonNode &ptableDataNode); 197 bool ParsePtableData(); 198 }; 199 } // namespace Updater 200 #endif // UPDATER_PTABLE_H 201