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_UFS_PTABLE_H 17 #define UPDATER_UFS_PTABLE_H 18 #include <iostream> 19 #include "ptable.h" 20 21 namespace Updater { 22 class UfsPtable : public Ptable { 23 public: 24 UfsPtable() = default; 25 DISALLOW_COPY_MOVE(UfsPtable); ~UfsPtable()26 ~UfsPtable() override {} 27 28 uint32_t GetDeviceLunNum(); 29 bool ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t imgBufSize) override; 30 bool LoadPtableFromDevice() override; 31 bool WritePartitionTable() override; 32 bool EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::vector<PtnInfo> &modifyList) override; 33 bool GetPtableImageBuffer(uint8_t *imageBuf, const uint32_t imgBufSize) override; 34 bool CorrectBufByPtnList(uint8_t *imageBuf, uint64_t imgBufSize, const std::vector<PtnInfo> &srcInfo, 35 const std::vector<PtnInfo> &dstInfo) override; 36 bool SyncABLunPtableDevice(const int sourceSlot) override; 37 bool GetABLunPartitionInfo(const int sourceSlot, std::string &srcNode, 38 std::string &tgtNode, uint32_t &offset) override; 39 #ifndef UPDATER_UT 40 protected: 41 #else 42 public: 43 #endif 44 static constexpr uint32_t TMP_DATA_SIZE = 32 * 1024; 45 static constexpr uint32_t MAX_LUN_NUMBERS = 26; 46 static constexpr uint32_t MIN_UFS_WRITE_SIZE = 4096; 47 static constexpr uint32_t GPT_PTABLE_BACKUP_SIZE = 33; // back ptable at the end of lun 48 static constexpr uint32_t READ_LINK_BUFFER_LENTH = 1024; // readlink buffer size 49 50 struct UfsPartitionDataInfo { 51 bool isGptVaild; 52 uint32_t lunIndex; 53 uint64_t lunSize; // lun device density 54 uint32_t writeDataLen; // data len written to UFS 55 uint8_t data[TMP_DATA_SIZE]; // ptable image data 56 }; 57 58 uint32_t deviceLunNum_ { 0 }; 59 std::vector<UfsPartitionDataInfo> ufsPtnDataInfo_; 60 bool hasBackupPtable_ {false}; 61 62 virtual uint64_t GetDeviceLunCapacity(const uint32_t lunIndex); 63 virtual uint32_t GetPtableExtraOffset(void); 64 virtual uint32_t GetDeviceBlockSize(void); 65 virtual std::string GetDeviceLunNodePath(const uint32_t lun); 66 virtual int32_t GetLunNumFromNode(const std::string &ufsNode); 67 virtual bool CheckDeviceLunRemoveable(const uint32_t lunIndex); 68 virtual bool IsUsbPath(const uint32_t lunIndex); 69 #ifndef UPDATER_UT 70 private: 71 #else 72 public: 73 #endif 74 bool EditABPartition(uint8_t *gptImage, const uint32_t blockSize, const int sourceSlot); 75 void GetTgtPartitionName(std::string &name, const int sourceSlot); 76 bool ModifyBufferPartitionName(uint8_t *buffer, const uint32_t bufferSize, const int sourceSlot); 77 bool WriteBackupPartitionTable(uint32_t lunIdx, uint64_t lunSize); 78 bool UfsReadGpt(const uint8_t *gptImage, const uint32_t len, const uint32_t lun, const uint32_t blockSize); 79 void UfsReadGptEntry(const uint8_t *gptImage, const uint32_t lun, 80 const uint32_t blockSize, std::vector<PtnInfo>::iterator startIter); 81 bool ParseGptHeaderByUfsLun(const uint8_t *gptImage, const uint32_t len, const uint32_t lun, 82 const uint32_t blockSize); 83 void UfsPatchGptHeader(UfsPartitionDataInfo &ptnDataInfo, const uint32_t blockSize); 84 uint32_t LoadAllLunPartitions(); 85 bool ReadAndCheckMbr(const uint32_t lunIndex, const uint32_t blockSize); 86 bool LoadPartitionInfoFromLun(const uint32_t lunIndex, const uint32_t imgLen); 87 void SetDeviceLunNum(); 88 uint8_t *GetPtableImageUfsLunPmbrStart(uint8_t *imageBuf, const uint32_t lunIndex); 89 uint8_t *GetPtableImageUfsLunGptHeaderStart(uint8_t *imageBuf, const uint32_t lunIndex); 90 uint8_t *GetPtableImageUfsLunEntryStart(uint8_t *imageBuf, const uint32_t lunIndex); 91 }; 92 } // namespace Updater 93 #endif // UPDATER_UFS_PTABLE_H