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_PTABLE_MANAGER_H
17 #define UPDATER_PTABLE_MANAGER_H
18 
19 #include "package/pkg_manager.h"
20 #include "ufs_ptable.h"
21 #include "emmc_ptable.h"
22 
23 namespace Updater {
24 class PtableManager {
25 public:
26     DISALLOW_COPY_MOVE(PtableManager);
~PtableManager()27     virtual ~PtableManager() {}
28 
29     using PtableConstructor = std::unique_ptr<Ptable> (*)();
30     enum class StorageType {
31         STORAGE_UNKNOWN,
32         STORAGE_EMMC,
33         STORAGE_UFS,
34     };
35 
36     virtual void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) = 0;
37     void ReloadDevicePartition(Hpackage::PkgManager *pkgManager);
38     bool WritePtableToDevice();
39     void PrintPtableInfo();
40     bool GetPartionInfoByName(const std::string &partitionName, Ptable::PtnInfo &ptnInfo, int32_t &index);
41     bool GetPartionInfoByName(const std::string &partitionName, Ptable::PtnInfo &ptnInfo);
42     static void RegisterPtable(uint32_t bitIndex, PtableConstructor constructor);
43 
44     std::unique_ptr<Ptable> pPtable_;
45     StorageType storage_ = StorageType::STORAGE_UNKNOWN;
46     static std::string ptbImgTag_;
47 
48 protected:
49     PtableManager();
50     void InitPtablePtr();
51     bool InitPtableManager();
52     void SetDeviceStorageType();
53     bool IsUfsDevice();
54     bool IsPartitionChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
55         const std::vector<Ptable::PtnInfo> &pkgPtnInfo, const std::string &partitionName);
56     bool IsPtableChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
57         const std::vector<Ptable::PtnInfo> &pkgPtnInfo);
58     int32_t GetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> &ptnInfo, const std::string &name);
59 
60     StorageType GetDeviceStorageType();
61 
62 private:
63     bool IsCompositePtable();
64     uint32_t GetBootdevType();
65     void InitCompositePtable();
66 
67     static inline std::unordered_map<uint32_t, PtableConstructor> ptableMap_;
68 };
69 
70 
71 class PackagePtable : public PtableManager {
72 public:
73     DISALLOW_COPY_MOVE(PackagePtable);
~PackagePtable()74     ~PackagePtable() override  {}
GetInstance()75     static PackagePtable& GetInstance()
76     {
77         static PackagePtable instance;
78         return instance;
79     }
80 
81     void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override;
82 
83 protected:
84     PackagePtable();
85     bool GetPtableBufferFromPkg(Hpackage::PkgManager *pkgManager, uint8_t *&imageBuf, uint32_t size);
86 };
87 
88 
89 class DevicePtable : public PtableManager {
90 public:
91     DISALLOW_COPY_MOVE(DevicePtable);
~DevicePtable()92     ~DevicePtable() override {}
GetInstance()93     static DevicePtable& GetInstance()
94     {
95         static DevicePtable instance;
96         return instance;
97     }
98 
99     void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override;
100     bool ComparePtable(PtableManager &newPtbManager);
101     bool ComparePartition(PtableManager &newPtbManager, const std::string partitionName);
102 protected:
103     DevicePtable();
104 };
105 } // namespace Updater
106 #endif // UPDATER_PTABLE_MANAGER_H