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 44 protected: 45 PtableManager(); 46 void InitPtablePtr(); 47 bool InitPtableManager(); 48 void SetDeviceStorageType(); 49 bool IsUfsDevice(); 50 bool IsPartitionChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo, 51 const std::vector<Ptable::PtnInfo> &pkgPtnInfo, const std::string &partitionName); 52 bool IsPtableChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo, 53 const std::vector<Ptable::PtnInfo> &pkgPtnInfo); 54 int32_t GetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> &ptnInfo, const std::string &name); 55 56 StorageType GetDeviceStorageType() const; 57 }; 58 59 60 class PackagePtable : public PtableManager { 61 public: 62 DISALLOW_COPY_MOVE(PackagePtable); ~PackagePtable()63 ~PackagePtable() override {} GetInstance()64 static PackagePtable& GetInstance() 65 { 66 static PackagePtable instance; 67 return instance; 68 } 69 70 void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override; 71 72 private: 73 PackagePtable(); 74 bool GetPtableBufferFromPkg(Hpackage::PkgManager *pkgManager, uint8_t *&imageBuf, uint32_t size); 75 }; 76 77 78 class DevicePtable : public PtableManager { 79 public: 80 DISALLOW_COPY_MOVE(DevicePtable); ~DevicePtable()81 ~DevicePtable() override {} GetInstance()82 static DevicePtable& GetInstance() 83 { 84 static DevicePtable instance; 85 return instance; 86 } 87 88 void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override; 89 bool ComparePtable(PtableManager &newPtbManager); 90 bool ComparePartition(PtableManager &newPtbManager, const std::string partitionName); 91 private: 92 DevicePtable(); 93 }; 94 } // namespace Updater 95 #endif // UPDATER_PTABLE_MANAGER_H