• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 SYS_INSTALLER_MODULE_FILE_H
17 #define SYS_INSTALLER_MODULE_FILE_H
18 
19 #include <list>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <unordered_map>
24 
25 #ifdef SUPPORT_HVB
26 #include "hvb.h"
27 #endif
28 #include "module_zip_helper.h"
29 namespace OHOS {
30 namespace SysInstaller {
31 namespace {
32 constexpr size_t FS_TYPE_MAX_SIZE = 10;
33 }
34 
35 constexpr const char *HMP_TRAIN_TYPE = "moduleTrain";
36 constexpr const char *HMP_APP_TYPE = "APP";
37 constexpr const char *HMP_SA_TYPE = "systemAbility";
38 constexpr const char *HMP_SA_TYPE_OLD = "SYSTEM ability";
39 constexpr const char *HMP_MIX_TYPE = "combineType";
40 constexpr const char *HMP_API_VERSION = "apiVersion";
41 constexpr const char *HMP_SA_SDK_VERSION = "saSdkVersion";
42 constexpr const char *HMP_MODULE_INFO = "moduleInfo";
43 
44 bool ExtractZipFile(ModuleZipHelper &helper, const std::string &fpInfo, std::string &buf);
45 bool ParseVersion(const std::string &version, const std::string &split, std::vector<std::string> &versionVec);
46 bool CompareHmpVersion(const std::vector<std::string> &smallVersion, const std::vector<std::string> &bigVersion);
47 bool CompareSaSdkVersion(const std::vector<std::string> &smallVersion, const std::vector<std::string> &bigVersion);
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 int32_t VerifyModulePackageSign(const std::string &fpInfo);
52 #ifdef __cplusplus
53 }
54 #endif
55 
56 struct SaVersion {
57     uint32_t apiVersion;
58     uint32_t versionCode;
59     uint32_t patchVersion;
60 
stringSaVersion61     operator std::string() const
62     {
63         return std::to_string(apiVersion) + std::to_string(versionCode) + std::to_string(patchVersion);
64     }
65 };
66 
67 struct SaInfo {
68     std::string saName;
69     int32_t saId {0};
70     SaVersion version;
71 };
72 
73 struct BundleInfo {
74     std::string bundleName;
75     std::string bundleVersion;
76 };
77 
78 struct ModuleInfo {
79     std::list<SaInfo> saInfoList {};
80     std::list<BundleInfo> bundleInfoList {};
81 };
82 
83 struct ModulePackageInfo {
84     std::string hmpName;
85     std::string version;
86     std::string displayVersion;
87     std::string saSdkVersion;
88     std::string type;
89     int apiVersion {-1};
90     std::unordered_map<std::string, ModuleInfo> moduleMap {};
91 };
92 
93 struct ImageStat {
94     uint32_t imageOffset;
95     uint32_t imageSize;
96     char fsType[FS_TYPE_MAX_SIZE];
97 };
98 
99 class ModuleFile {
100 public:
101     static std::unique_ptr<ModuleFile> Open(const std::string &fpInfo);
102     static bool CompareVersion(const ModuleFile &newFile, const ModuleFile &oldFile);
ModuleFile(const std::string & modulePath,const ModulePackageInfo & versionInfo,const std::optional<ImageStat> & imageStat)103     ModuleFile(const std::string &modulePath,
104                const ModulePackageInfo &versionInfo,
105                const std::optional<ImageStat> &imageStat)
106         : modulePath_(modulePath),
107           versionInfo_(versionInfo),
108           imageStat_(imageStat) {}
109     virtual ~ModuleFile();
110     ModuleFile(const ModuleFile&) = default;
111     ModuleFile& operator=(const ModuleFile&) = default;
112     ModuleFile(ModuleFile&&) = default;
113     ModuleFile& operator=(ModuleFile&&) = default;
114 
GetPath()115     const std::string &GetPath() const
116     {
117         return modulePath_;
118     }
GetVersionInfo()119     const ModulePackageInfo &GetVersionInfo() const
120     {
121         return versionInfo_;
122     }
GetImageStat()123     const std::optional<ImageStat> &GetImageStat() const
124     {
125         return imageStat_;
126     }
SetPath(const std::string & path)127     void SetPath(const std::string &path)
128     {
129         modulePath_ = path;
130     }
131     bool ProcessModuleUpdateVerityInfo(const std::string &partition) const;
132     bool VerifyModuleVerity();
133     void ClearVerifiedData();
134 #ifdef SUPPORT_HVB
GetVerifiedData()135     struct hvb_verified_data *GetVerifiedData() const
136     {
137         return vd_;
138     }
139 #endif
140 
141 private:
142     std::string modulePath_;
143     ModulePackageInfo versionInfo_;
144     std::optional<ImageStat> imageStat_;
145 
146 #ifdef SUPPORT_HVB
147     struct hvb_verified_data *vd_ = nullptr;
148 #endif
149 };
150 } // namespace SysInstaller
151 } // namespace OHOS
152 #endif // SYS_INSTALLER_MODULE_FILE_H