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