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