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