• 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_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 bool LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) = 0;
37     bool 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     bool WritePtableWithFile();
44     bool LoadPartitionInfoWithFile();
45 #ifdef UPDATER_UT
SetPtable(std::unique_ptr<Ptable> && ptable)46     void SetPtable(std::unique_ptr<Ptable> &&ptable)
47     {
48         pPtable_ = std::move(ptable);
49     }
GetPtable(void)50     std::unique_ptr<Ptable> GetPtable(void)
51     {
52         return std::move(pPtable_);
53     }
SetDeviceStorageType(StorageType storageType)54     void SetDeviceStorageType(StorageType storageType)
55     {
56         storage_ = storageType;
57     }
58 #endif
59 
60     std::unique_ptr<Ptable> pPtable_;
61     StorageType storage_ = StorageType::STORAGE_UNKNOWN;
62     static std::string ptbImgTag_;
63 
64 protected:
65     PtableManager();
66     void InitPtablePtr();
67     bool InitPtableManager();
68     void SetDeviceStorageType();
69     bool IsUfsDevice();
70     bool IsPartitionChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
71         const std::vector<Ptable::PtnInfo> &pkgPtnInfo, const std::string &partitionName);
72     bool IsPtableChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
73         const std::vector<Ptable::PtnInfo> &pkgPtnInfo);
74     int32_t GetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> &ptnInfo, const std::string &name);
75 
76     StorageType GetDeviceStorageType();
77 
78 private:
79     bool IsCompositePtable();
80     uint32_t GetBootdevType();
81     void InitCompositePtable();
82 
83     static inline std::unordered_map<uint32_t, PtableConstructor> ptableMap_;
84 };
85 
86 
87 class PackagePtable : public PtableManager {
88 public:
89     DISALLOW_COPY_MOVE(PackagePtable);
~PackagePtable()90     ~PackagePtable() override  {}
GetInstance()91     static PackagePtable& GetInstance()
92     {
93         static PackagePtable instance;
94         return instance;
95     }
96 
97     bool LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override;
98 
99 protected:
100     PackagePtable();
101     bool GetPtableBufferFromPkg(Hpackage::PkgManager *pkgManager, uint8_t *&imageBuf, uint32_t size);
102 };
103 
104 
105 class DevicePtable : public PtableManager {
106 public:
107     DISALLOW_COPY_MOVE(DevicePtable);
~DevicePtable()108     ~DevicePtable() override {}
GetInstance()109     static DevicePtable& GetInstance()
110     {
111         static DevicePtable instance;
112         return instance;
113     }
114 
115     bool LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override;
116     bool ComparePtable(PtableManager &newPtbManager);
117     bool ComparePartition(PtableManager &newPtbManager, const std::string partitionName);
118 protected:
119     DevicePtable();
120 };
121 } // namespace Updater
122 #endif // UPDATER_PTABLE_MANAGER_H