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