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 35 private: 36 static constexpr uint32_t TMP_DATA_SIZE = 32 * 1024; 37 static constexpr uint32_t MAX_LUN_NUMBERS = 26; 38 static constexpr uint32_t MIN_UFS_WRITE_SIZE = 4096; 39 40 struct UfsPartitionDataInfo { 41 bool isGptVaild; 42 uint32_t lunIndex; 43 uint64_t lunSize; // lun device density 44 uint32_t writeDataLen; // data len written to UFS 45 uint8_t data[TMP_DATA_SIZE]; // ptable image data 46 }; 47 48 uint32_t deviceLunNum_ { 0 }; 49 std::vector<UfsPartitionDataInfo> ufsPtnDataInfo_; 50 51 uint64_t GetDeviceLunCapacity(const uint32_t lunIndex); 52 bool UfsReadGpt(const uint8_t *gptImage, const uint32_t len, const uint32_t lun, const uint32_t blockSize); 53 bool ParseGptHeaderByUfsLun(const uint8_t *gptImage, const uint32_t len, const uint32_t lun, 54 const uint32_t blockSize); 55 void UfsPatchGptHeader(UfsPartitionDataInfo &ptnDataInfo, const uint32_t blockSize); 56 uint32_t LoadAllLunPartitions(); 57 bool ReadAndCheckMbr(const uint32_t lunIndex, const uint32_t blockSize); 58 bool LoadPartitionInfoFromLun(const uint32_t lunIndex, const uint32_t imgLen); 59 int32_t GetLunNumFromNode(const std::string &ufsNode); 60 void SetDeviceLunNum(); 61 uint8_t *GetPtableImageUfsLunPmbrStart(uint8_t *imageBuf, const uint32_t lunIndex); 62 uint8_t *GetPtableImageUfsLunGptHeaderStart(uint8_t *imageBuf, const uint32_t lunIndex); 63 uint8_t *GetPtableImageUfsLunEntryStart(uint8_t *imageBuf, const uint32_t lunIndex); 64 }; 65 } // namespace Updater 66 #endif // UPDATER_UFS_PTABLE_H