1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#ifndef UPDATER_PTABLE_MANAGER_H
17fb299fa2Sopenharmony_ci#define UPDATER_PTABLE_MANAGER_H
18fb299fa2Sopenharmony_ci
19fb299fa2Sopenharmony_ci#include "package/pkg_manager.h"
20fb299fa2Sopenharmony_ci#include "ufs_ptable.h"
21fb299fa2Sopenharmony_ci#include "emmc_ptable.h"
22fb299fa2Sopenharmony_ci
23fb299fa2Sopenharmony_cinamespace Updater {
24fb299fa2Sopenharmony_ciclass PtableManager {
25fb299fa2Sopenharmony_cipublic:
26fb299fa2Sopenharmony_ci    DISALLOW_COPY_MOVE(PtableManager);
27fb299fa2Sopenharmony_ci    virtual ~PtableManager() {}
28fb299fa2Sopenharmony_ci
29fb299fa2Sopenharmony_ci    using PtableConstructor = std::unique_ptr<Ptable> (*)();
30fb299fa2Sopenharmony_ci    enum class StorageType {
31fb299fa2Sopenharmony_ci        STORAGE_UNKNOWN,
32fb299fa2Sopenharmony_ci        STORAGE_EMMC,
33fb299fa2Sopenharmony_ci        STORAGE_UFS,
34fb299fa2Sopenharmony_ci    };
35fb299fa2Sopenharmony_ci
36fb299fa2Sopenharmony_ci    virtual void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) = 0;
37fb299fa2Sopenharmony_ci    void ReloadDevicePartition(Hpackage::PkgManager *pkgManager);
38fb299fa2Sopenharmony_ci    bool WritePtableToDevice();
39fb299fa2Sopenharmony_ci    void PrintPtableInfo();
40fb299fa2Sopenharmony_ci    bool GetPartionInfoByName(const std::string &partitionName, Ptable::PtnInfo &ptnInfo, int32_t &index);
41fb299fa2Sopenharmony_ci    bool GetPartionInfoByName(const std::string &partitionName, Ptable::PtnInfo &ptnInfo);
42fb299fa2Sopenharmony_ci    static void RegisterPtable(uint32_t bitIndex, PtableConstructor constructor);
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_ci    std::unique_ptr<Ptable> pPtable_;
45fb299fa2Sopenharmony_ci    StorageType storage_ = StorageType::STORAGE_UNKNOWN;
46fb299fa2Sopenharmony_ci    static std::string ptbImgTag_;
47fb299fa2Sopenharmony_ci
48fb299fa2Sopenharmony_ciprotected:
49fb299fa2Sopenharmony_ci    PtableManager();
50fb299fa2Sopenharmony_ci    void InitPtablePtr();
51fb299fa2Sopenharmony_ci    bool InitPtableManager();
52fb299fa2Sopenharmony_ci    void SetDeviceStorageType();
53fb299fa2Sopenharmony_ci    bool IsUfsDevice();
54fb299fa2Sopenharmony_ci    bool IsPartitionChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
55fb299fa2Sopenharmony_ci        const std::vector<Ptable::PtnInfo> &pkgPtnInfo, const std::string &partitionName);
56fb299fa2Sopenharmony_ci    bool IsPtableChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
57fb299fa2Sopenharmony_ci        const std::vector<Ptable::PtnInfo> &pkgPtnInfo);
58fb299fa2Sopenharmony_ci    int32_t GetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> &ptnInfo, const std::string &name);
59fb299fa2Sopenharmony_ci
60fb299fa2Sopenharmony_ci    StorageType GetDeviceStorageType();
61fb299fa2Sopenharmony_ci
62fb299fa2Sopenharmony_ciprivate:
63fb299fa2Sopenharmony_ci    bool IsCompositePtable();
64fb299fa2Sopenharmony_ci    uint32_t GetBootdevType();
65fb299fa2Sopenharmony_ci    void InitCompositePtable();
66fb299fa2Sopenharmony_ci
67fb299fa2Sopenharmony_ci    static inline std::unordered_map<uint32_t, PtableConstructor> ptableMap_;
68fb299fa2Sopenharmony_ci};
69fb299fa2Sopenharmony_ci
70fb299fa2Sopenharmony_ci
71fb299fa2Sopenharmony_ciclass PackagePtable : public PtableManager {
72fb299fa2Sopenharmony_cipublic:
73fb299fa2Sopenharmony_ci    DISALLOW_COPY_MOVE(PackagePtable);
74fb299fa2Sopenharmony_ci    ~PackagePtable() override  {}
75fb299fa2Sopenharmony_ci    static PackagePtable& GetInstance()
76fb299fa2Sopenharmony_ci    {
77fb299fa2Sopenharmony_ci        static PackagePtable instance;
78fb299fa2Sopenharmony_ci        return instance;
79fb299fa2Sopenharmony_ci    }
80fb299fa2Sopenharmony_ci
81fb299fa2Sopenharmony_ci    void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override;
82fb299fa2Sopenharmony_ci
83fb299fa2Sopenharmony_ciprotected:
84fb299fa2Sopenharmony_ci    PackagePtable();
85fb299fa2Sopenharmony_ci    bool GetPtableBufferFromPkg(Hpackage::PkgManager *pkgManager, uint8_t *&imageBuf, uint32_t size);
86fb299fa2Sopenharmony_ci};
87fb299fa2Sopenharmony_ci
88fb299fa2Sopenharmony_ci
89fb299fa2Sopenharmony_ciclass DevicePtable : public PtableManager {
90fb299fa2Sopenharmony_cipublic:
91fb299fa2Sopenharmony_ci    DISALLOW_COPY_MOVE(DevicePtable);
92fb299fa2Sopenharmony_ci    ~DevicePtable() override {}
93fb299fa2Sopenharmony_ci    static DevicePtable& GetInstance()
94fb299fa2Sopenharmony_ci    {
95fb299fa2Sopenharmony_ci        static DevicePtable instance;
96fb299fa2Sopenharmony_ci        return instance;
97fb299fa2Sopenharmony_ci    }
98fb299fa2Sopenharmony_ci
99fb299fa2Sopenharmony_ci    void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override;
100fb299fa2Sopenharmony_ci    bool ComparePtable(PtableManager &newPtbManager);
101fb299fa2Sopenharmony_ci    bool ComparePartition(PtableManager &newPtbManager, const std::string partitionName);
102fb299fa2Sopenharmony_ciprotected:
103fb299fa2Sopenharmony_ci    DevicePtable();
104fb299fa2Sopenharmony_ci};
105fb299fa2Sopenharmony_ci} // namespace Updater
106fb299fa2Sopenharmony_ci#endif // UPDATER_PTABLE_MANAGER_H