• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "parameter_hal.h"
17 
18 #include <cstring>
19 #include <fstream>
20 #include <openssl/sha.h>
21 #include <securec.h>
22 
23 #include "parameters.h"
24 #include "sysparam_errno.h"
25 #include "string_ex.h"
26 #include "init_param.h"
27 
28 static const char *g_emptyStr = "";
29 
30 static const char DEF_OHOS_SERIAL[] = {"1234567890"};
31 #ifdef USE_MTK_EMMC
32 static const char SN_FILE[] = {"/proc/bootdevice/cid"};
33 #else
34 static const char SN_FILE[] = {"/sys/block/mmcblk0/device/cid"};
35 #endif
36 static const int DEV_BUF_LENGTH = 3;
37 static const int DEV_BUF_MAX_LENGTH = 1024;
38 static const int DEV_UUID_LENGTH = 65;
39 
IsValidValue(const char * value,unsigned int len)40 static bool IsValidValue(const char *value, unsigned int len)
41 {
42     if (value == nullptr) {
43         return false;
44     }
45     const std::string strValue(value);
46     if (strValue.length() + 1 > len) {
47         return false;
48     }
49     return true;
50 }
51 
HalGetParameter(const char * key,const char * def,char * value,unsigned int len)52 int HalGetParameter(const char *key, const char *def, char *value, unsigned int len)
53 {
54     if ((key == nullptr) || (value == nullptr)) {
55         return EC_INVALID;
56     }
57     const std::string strKey(key);
58     std::string res = OHOS::system::GetParameter(strKey, "");
59     if (res == "") {
60         if (!IsValidValue(def, len)) {
61             return EC_INVALID;
62         }
63         if (sprintf_s(value, len, "%s", def) < 0) {
64             return EC_FAILURE;
65         }
66         return EC_SUCCESS;
67     }
68 
69     const char *result = res.c_str();
70     if (!IsValidValue(result, len)) {
71         return EC_INVALID;
72     }
73     if (sprintf_s(value, len, "%s", result) <= 0) {
74         return EC_FAILURE;
75     }
76 
77     return EC_SUCCESS;
78 }
79 
HalGetIntParameter(const char * key,int def)80 int HalGetIntParameter(const char *key, int def)
81 {
82     const std::string strKey(key);
83     return OHOS::system::GetIntParameter(strKey, def);
84 }
85 
HalSetParameter(const char * key,const char * value)86 int HalSetParameter(const char *key, const char *value)
87 {
88     if ((key == nullptr) || (value == nullptr)) {
89         return EC_INVALID;
90     }
91     const std::string strKey(key);
92     const std::string strVal(value);
93     bool ret = OHOS::system::SetParameter(strKey, strVal);
94     return ret ? EC_SUCCESS : EC_FAILURE;
95 }
96 
GetProperty(const std::string & key,const char ** paramHolder)97 const char *GetProperty(const std::string &key, const char **paramHolder)
98 {
99     if (paramHolder == nullptr) {
100         return nullptr;
101     }
102     if (*paramHolder != nullptr) {
103         return *paramHolder;
104     }
105     std::string result = OHOS::system::GetParameter(key, "");
106     const char *res = strdup(result.c_str());
107     if (res == nullptr) {
108         return g_emptyStr;
109     }
110     *paramHolder = res;
111     return *paramHolder;
112 }
113 
HalGetDeviceType()114 const char *HalGetDeviceType()
115 {
116     static const char *productType = nullptr;
117     return GetProperty("const.build.characteristics", &productType);
118 }
119 
HalGetProductModel()120 const char *HalGetProductModel()
121 {
122     static const char *productModel = nullptr;
123     return GetProperty("const.product.model", &productModel);
124 }
125 
HalGetManufacture()126 const char *HalGetManufacture()
127 {
128     static const char *productManufacture = nullptr;
129     return GetProperty("const.product.manufacturer", &productManufacture);
130 }
131 
HalGetBrand()132 const char *HalGetBrand()
133 {
134     static const char *productBrand = nullptr;
135     return GetProperty("const.product.brand", &productBrand);
136 }
137 
HalGetMarketName()138 const char *HalGetMarketName()
139 {
140     static const char *marketName = nullptr;
141     return GetProperty("const.product.name", &marketName);
142 }
143 
HalGetProductSeries()144 const char *HalGetProductSeries()
145 {
146     static const char *productSeries = nullptr;
147     return GetProperty("const.build.product", &productSeries);
148 }
149 
HalGetSoftwareModel()150 const char *HalGetSoftwareModel()
151 {
152     static const char *softwareModel = nullptr;
153     return GetProperty("const.software.model", &softwareModel);
154 }
155 
HalGetHardwareModel()156 const char *HalGetHardwareModel()
157 {
158     static const char *hardwareModel = nullptr;
159     return GetProperty("const.product.hardwareversion", &hardwareModel);
160 }
161 
HalGetHardwareProfile()162 const char *HalGetHardwareProfile()
163 {
164     static const char *hardwareProfile = nullptr;
165     return GetProperty("const.product.hardwareprofile", &hardwareProfile);
166 }
167 
HalGetSerial()168 const char *HalGetSerial()
169 {
170     static const char* ohos_serial = nullptr;
171     if (ohos_serial != nullptr) {
172         return ohos_serial;
173     }
174     std::ifstream infile;
175     infile.open(SN_FILE);
176     std::string value;
177     infile >> value;
178     if (value.empty()) {
179         ohos_serial = DEF_OHOS_SERIAL;
180     } else {
181         std::string sn = OHOS::ReplaceStr(value, ":", "");
182         const char* res = strdup(sn.c_str());
183         if (res == nullptr) {
184             return DEF_OHOS_SERIAL;
185         }
186         ohos_serial = res;
187     }
188     return ohos_serial;
189 }
190 
HalGetAbiList()191 const char *HalGetAbiList()
192 {
193     static const char *productAbiList = nullptr;
194     return GetProperty("const.product.cpu.abilist", &productAbiList);
195 }
196 
HalGetBootloaderVersion()197 const char *HalGetBootloaderVersion()
198 {
199     static const char *productBootloader = nullptr;
200     return GetProperty("const.product.bootloader.version", &productBootloader);
201 }
202 
HalGetSecurityPatchTag()203 const char *HalGetSecurityPatchTag()
204 {
205     static const char *securityPatchTag = nullptr;
206     return GetProperty("const.ohos.version.security_patch", &securityPatchTag);
207 }
208 
HalGetFirstApiVersion()209 int HalGetFirstApiVersion()
210 {
211     static const char *firstApiVersion = nullptr;
212     return atoi(GetProperty("const.product.firstapiversion", &firstApiVersion));
213 }
214 
HalGetDisplayVersion()215 const char *HalGetDisplayVersion()
216 {
217     static const char *displayVersion = nullptr;
218     return GetProperty("const.product.software.version", &displayVersion);
219 }
220 
HalGetIncrementalVersion()221 const char *HalGetIncrementalVersion()
222 {
223     static const char *incrementalVersion = nullptr;
224     return GetProperty("const.product.incremental.version", &incrementalVersion);
225 }
226 
HalGetOsReleaseType()227 const char *HalGetOsReleaseType()
228 {
229     static const char *osReleaseType = nullptr;
230     return GetProperty("const.ohos.releasetype", &osReleaseType);
231 }
232 
HalGetSdkApiVersion()233 const char *HalGetSdkApiVersion()
234 {
235     static const char *sdkApiVersion = nullptr;
236     return GetProperty("const.ohos.apiversion", &sdkApiVersion);
237 }
HalGetBuildRootHash()238 const char *HalGetBuildRootHash()
239 {
240     static const char *buildRootHash = nullptr;
241     return GetProperty("const.ohos.buildroothash", &buildRootHash);
242 }
243 
HalGetSdkApiLevel()244 const char *HalGetSdkApiLevel()
245 {
246     static const char *sdkApiLevel = nullptr;
247     return GetProperty("const.ohos.sdkapilevel", &sdkApiLevel);
248 }
249 
HalGetOSName()250 const char *HalGetOSName()
251 {
252     static const char *osName = nullptr;
253     return GetProperty("const.ohos.name", &osName);
254 }
255 
HalGetBuildType()256 const char *HalGetBuildType()
257 {
258     static const char *buildType = nullptr;
259     return GetProperty("const.product.build.type", &buildType);
260 }
261 
HalGetBuildUser()262 const char *HalGetBuildUser()
263 {
264     static const char *buildUser = nullptr;
265     return GetProperty("const.product.build.user", &buildUser);
266 }
267 
HalGetBuildHost()268 const char *HalGetBuildHost()
269 {
270     static const char *buildHost = nullptr;
271     return GetProperty("const.product.build.host", &buildHost);
272 }
273 
HalGetBuildTime()274 const char *HalGetBuildTime()
275 {
276     static const char *buildTime = nullptr;
277     return GetProperty("const.product.build.date", &buildTime);
278 }
279 
HalWaitParameter(const char * key,const char * value,int timeout)280 int HalWaitParameter(const char *key, const char *value, int timeout)
281 {
282     if ((key == nullptr) || (value == nullptr)) {
283         return EC_INVALID;
284     }
285     return OHOS::system::WaitParameter(key, value, timeout);
286 }
287 
HalFindParameter(const char * key)288 unsigned int HalFindParameter(const char *key)
289 {
290     if (key == nullptr) {
291         return EC_INVALID;
292     }
293 
294     unsigned int handle = 0;
295     int ret = SystemFindParameter(key, &handle);
296     if (ret != 0) {
297         return static_cast<unsigned int>(-1);
298     }
299     return handle;
300 }
301 
HalGetParameterCommitId(unsigned int handle)302 unsigned int HalGetParameterCommitId(unsigned int handle)
303 {
304     return OHOS::system::GetParameterCommitId(handle);
305 }
306 
HalGetParameterName(unsigned int handle,char * name,unsigned int len)307 int HalGetParameterName(unsigned int handle, char *name, unsigned int len)
308 {
309     if (name == nullptr) {
310         return EC_INVALID;
311     }
312     std::string data = OHOS::system::GetParameterName(handle);
313     if (data.empty()) {
314         return EC_INVALID;
315     }
316     return strcpy_s(name, len, data.c_str());
317 }
318 
HalGetParameterValue(unsigned int handle,char * value,unsigned int len)319 int HalGetParameterValue(unsigned int handle, char *value, unsigned int len)
320 {
321     if (value == nullptr) {
322         return EC_INVALID;
323     }
324     std::string data = OHOS::system::GetParameterValue(handle);
325     if (data.empty()) {
326         return EC_INVALID;
327     }
328     return strcpy_s(value, len, data.c_str());
329 }
330 
HalGetSha256Value(const char * input,char * udid,int udidSize)331 static int HalGetSha256Value(const char *input, char *udid, int udidSize)
332 {
333     if (input == nullptr || udid == nullptr) {
334         return EC_FAILURE;
335     }
336     char buf[DEV_BUF_LENGTH] = { 0 };
337     unsigned char hash[SHA256_DIGEST_LENGTH] = { 0 };
338     SHA256_CTX sha256;
339     if ((SHA256_Init(&sha256) == 0) || (SHA256_Update(&sha256, input, strlen(input)) == 0) ||
340         (SHA256_Final(hash, &sha256) == 0)) {
341         return EC_FAILURE;
342     }
343 
344     for (size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
345         unsigned char value = hash[i];
346         (void)memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH);
347         (void)sprintf_s(buf, sizeof(buf), "%02X", value);
348         if (strcat_s(udid, udidSize, buf) != 0) {
349             return EC_FAILURE;
350         }
351     }
352     return EC_SUCCESS;
353 }
354 
HalGetDevUdid(char * udid,int size)355 int HalGetDevUdid(char *udid, int size)
356 {
357     if (size < DEV_UUID_LENGTH) {
358         return EC_INVALID;
359     }
360 
361     const char *manufacture = HalGetManufacture();
362     const char *model = HalGetProductModel();
363     const char *sn = HalGetSerial();
364     if (manufacture == nullptr || model == nullptr || sn == nullptr) {
365         return EC_INVALID;
366     }
367     int tmpSize = strlen(manufacture) + strlen(model) + strlen(sn) + 1;
368     if (tmpSize <= 0 || tmpSize > DEV_BUF_MAX_LENGTH) {
369         return EC_INVALID;
370     }
371     char *tmp = (char *)malloc(tmpSize);
372     if (tmp == nullptr) {
373         return EC_SYSTEM_ERR;
374     }
375 
376     (void)memset_s(tmp, tmpSize, 0, tmpSize);
377     if ((strcat_s(tmp, tmpSize, manufacture) != 0) || (strcat_s(tmp, tmpSize, model) != 0) ||
378         (strcat_s(tmp, tmpSize, sn) != 0)) {
379         free(tmp);
380         return EC_SYSTEM_ERR;
381     }
382 
383     int ret = HalGetSha256Value(tmp, udid, size);
384     free(tmp);
385     return ret;
386 }
387