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