• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef UPDATER_UT
37 protected:
38 #else
39 public:
40 #endif
41     static constexpr uint32_t TMP_DATA_SIZE = 32 * 1024;
42     static constexpr uint32_t MAX_LUN_NUMBERS = 26;
43     static constexpr uint32_t MIN_UFS_WRITE_SIZE = 4096;
44     static constexpr uint32_t GPT_PTABLE_BACKUP_SIZE = 33; // back ptable at the end of lun
45     static constexpr uint32_t READ_LINK_BUFFER_LENTH = 1024; // readlink buffer size
46 
47     struct UfsPartitionDataInfo {
48         bool isGptVaild;
49         uint32_t lunIndex;
50         uint64_t lunSize; // lun device density
51         uint32_t writeDataLen; // data len written to UFS
52         uint8_t data[TMP_DATA_SIZE]; // ptable image data
53     };
54 
55     uint32_t deviceLunNum_ { 0 };
56     std::vector<UfsPartitionDataInfo> ufsPtnDataInfo_;
57     bool hasBackupPtable_ {false};
58 
59     virtual uint64_t GetDeviceLunCapacity(const uint32_t lunIndex);
60     virtual uint32_t GetPtableExtraOffset(void);
61     virtual uint32_t GetDeviceBlockSize(void);
62     virtual std::string GetDeviceLunNodePath(const uint32_t lun);
63     virtual int32_t GetLunNumFromNode(const std::string &ufsNode);
64     virtual bool CheckDeviceLunRemoveable(const uint32_t lunIndex);
65     virtual bool IsUsbPath(const uint32_t lunIndex);
66 #ifndef UPDATER_UT
67 private:
68 #else
69 public:
70 #endif
71     bool WriteBackupPartitionTable(uint32_t lunIdx, uint64_t lunSize);
72     bool UfsReadGpt(const uint8_t *gptImage, const uint32_t len, const uint32_t lun, const uint32_t blockSize);
73     void UfsReadGptEntry(const uint8_t *gptImage, const uint32_t lun,
74                          const uint32_t blockSize, std::vector<PtnInfo>::iterator startIter);
75     bool ParseGptHeaderByUfsLun(const uint8_t *gptImage, const uint32_t len, const uint32_t lun,
76         const uint32_t blockSize);
77     void UfsPatchGptHeader(UfsPartitionDataInfo &ptnDataInfo, const uint32_t blockSize);
78     uint32_t LoadAllLunPartitions();
79     bool ReadAndCheckMbr(const uint32_t lunIndex, const uint32_t blockSize);
80     bool LoadPartitionInfoFromLun(const uint32_t lunIndex, const uint32_t imgLen);
81     void SetDeviceLunNum();
82     uint8_t *GetPtableImageUfsLunPmbrStart(uint8_t *imageBuf, const uint32_t lunIndex);
83     uint8_t *GetPtableImageUfsLunGptHeaderStart(uint8_t *imageBuf, const uint32_t lunIndex);
84     uint8_t *GetPtableImageUfsLunEntryStart(uint8_t *imageBuf, const uint32_t lunIndex);
85 };
86 } // namespace Updater
87 #endif // UPDATER_UFS_PTABLE_H