1 /*
2 * Copyright (c) 2020 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 <fcntl.h>
17 #include <securec.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "hal_sys_param.h"
22 #ifdef USE_MBEDTLS
23 #include "mbedtls/sha256.h"
24 #endif
25 #include "ohos_errno.h"
26 #include "param_adaptor.h"
27 #include "parameter.h"
28
29 #define FILE_RO "ro."
30 #define OS_FULL_NAME_LEN 128
31 #define VERSION_ID_LEN 256
32 #define HASH_LENGTH 32
33 #define DEV_BUF_LENGTH 3
34 #define DEV_BUF_MAX_LENGTH 1024
35 #define DEV_UUID_LENGTH 65
36 #define OHOS_DISPLAY_VERSION_LEN 128
37 #define OHOS_PATCH_VERSION_LEN 64
38 #define OHOS_PATCH_VERSION_FILE "/patch/pversion"
39
40 static const char OHOS_OS_NAME[] = { "OpenHarmony" };
41 static const int OHOS_SDK_API_VERSION = 6;
42 static const char OHOS_SECURITY_PATCH_TAG[] = {"2021-09-01"};
43 static const char OHOS_RELEASE_TYPE[] = { "Beta" };
44 static const char OHOS_DEFAULT_VALUE[] = { "Default" };
45
46 static const int MAJOR_VERSION = 1;
47 static const int SENIOR_VERSION = 0;
48 static const int FEATURE_VERSION = 1;
49 static const int BUILD_VERSION = 0;
50
IsValidValue(const char * value,unsigned int len)51 static boolean IsValidValue(const char *value, unsigned int len)
52 {
53 if ((value == NULL) || !strlen(value) || (strlen(value) + 1 > len)) {
54 return FALSE;
55 }
56 return TRUE;
57 }
58
GetParameter(const char * key,const char * def,char * value,unsigned int len)59 int GetParameter(const char *key, const char *def, char *value, unsigned int len)
60 {
61 if ((key == NULL) || (value == NULL)) {
62 return EC_INVALID;
63 }
64 if (!CheckPermission()) {
65 return EC_FAILURE;
66 }
67 int ret = GetSysParam(key, value, len);
68 if (ret == EC_INVALID) {
69 return EC_INVALID;
70 }
71 if ((ret < 0) && IsValidValue(def, len)) {
72 if (strncpy_s(value, len, def, len - 1) != 0) {
73 return EC_FAILURE;
74 }
75 ret = (int)strlen(def);
76 }
77 return ret;
78 }
79
SetParameter(const char * key,const char * value)80 int SetParameter(const char *key, const char *value)
81 {
82 if ((key == NULL) || (value == NULL)) {
83 return EC_INVALID;
84 }
85 if (!CheckPermission()) {
86 return EC_FAILURE;
87 }
88 if (strncmp(key, FILE_RO, strlen(FILE_RO)) == 0) {
89 return EC_INVALID;
90 }
91
92 return SetSysParam(key, value);
93 }
94
GetDeviceType(void)95 const char *GetDeviceType(void)
96 {
97 return HalGetDeviceType();
98 }
99
GetManufacture(void)100 const char *GetManufacture(void)
101 {
102 return HalGetManufacture();
103 }
104
GetBrand(void)105 const char *GetBrand(void)
106 {
107 return HalGetBrand();
108 }
109
GetMarketName(void)110 const char *GetMarketName(void)
111 {
112 return HalGetMarketName();
113 }
114
GetProductSeries(void)115 const char *GetProductSeries(void)
116 {
117 return HalGetProductSeries();
118 }
119
GetProductModel(void)120 const char *GetProductModel(void)
121 {
122 return HalGetProductModel();
123 }
124
GetSoftwareModel(void)125 const char *GetSoftwareModel(void)
126 {
127 return HalGetSoftwareModel();
128 }
129
GetHardwareModel(void)130 const char *GetHardwareModel(void)
131 {
132 return HalGetHardwareModel();
133 }
134
GetHardwareProfile(void)135 const char *GetHardwareProfile(void)
136 {
137 return HalGetHardwareProfile();
138 }
139
GetSerial(void)140 const char *GetSerial(void)
141 {
142 return HalGetSerial();
143 }
144
GetBootloaderVersion(void)145 const char *GetBootloaderVersion(void)
146 {
147 return HalGetBootloaderVersion();
148 }
149
GetSecurityPatchTag(void)150 const char *GetSecurityPatchTag(void)
151 {
152 return OHOS_SECURITY_PATCH_TAG;
153 }
154
GetAbiList(void)155 const char *GetAbiList(void)
156 {
157 return HalGetAbiList();
158 }
159
BuildOSFullName(void)160 static const char *BuildOSFullName(void)
161 {
162 const char release[] = "Release";
163 char value[OS_FULL_NAME_LEN];
164 const char *releaseType = GetOsReleaseType();
165 int length;
166 if (strncmp(releaseType, release, sizeof(release) - 1) == 0) {
167 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d",
168 OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION);
169 } else {
170 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d(%s)",
171 OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION, releaseType);
172 }
173 if (length < 0) {
174 return EMPTY_STR;
175 }
176 const char *osFullName = strdup(value);
177 return osFullName;
178 }
179
GetOSFullName(void)180 const char *GetOSFullName(void)
181 {
182 static const char *osFullName = NULL;
183 if (osFullName != NULL) {
184 return osFullName;
185 }
186 osFullName = BuildOSFullName();
187 if (osFullName == NULL) {
188 return EMPTY_STR;
189 }
190 return osFullName;
191 }
192
BuildDisplayVersion(void)193 static const char *BuildDisplayVersion(void)
194 {
195 ssize_t len;
196 char patchValue[OHOS_PATCH_VERSION_LEN] = {0};
197 char displayValue[OHOS_DISPLAY_VERSION_LEN] = {0};
198 int fd = open(OHOS_PATCH_VERSION_FILE, O_RDONLY);
199 if (fd < 0) {
200 return NULL;
201 }
202 len = read(fd, patchValue, OHOS_PATCH_VERSION_LEN);
203 if (len < (ssize_t)strlen("version=")) {
204 close(fd);
205 return NULL;
206 }
207 close(fd);
208 if (patchValue[len - 1] == '\n') {
209 patchValue[len - 1] = '\0';
210 }
211 const char *versionValue = HalGetDisplayVersion();
212 const int versionLen = strlen(versionValue);
213 if (versionLen > 0) {
214 if (versionValue[versionLen - 1] != ')') {
215 len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s(%s)", versionValue,
216 patchValue + strlen("version="));
217 } else {
218 char tempValue[versionLen];
219 (void)memset_s(tempValue, versionLen, 0, versionLen);
220 if (strncpy_s(tempValue, versionLen, versionValue, versionLen - 1) != 0) {
221 return NULL;
222 }
223 tempValue[versionLen - 1] = '\0';
224 len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s%s)", tempValue,
225 patchValue + strlen("version="));
226 }
227 }
228 if (len < 0) {
229 return NULL;
230 }
231 return strdup(displayValue);
232 }
233
GetDisplayVersion(void)234 const char *GetDisplayVersion(void)
235 {
236 static const char *displayVersion = NULL;
237 if (displayVersion != NULL) {
238 return displayVersion;
239 }
240 displayVersion = BuildDisplayVersion();
241 if (displayVersion == NULL) {
242 return HalGetDisplayVersion();
243 }
244 return displayVersion;
245 }
246
GetSdkApiVersion(void)247 int GetSdkApiVersion(void)
248 {
249 return OHOS_SDK_API_VERSION;
250 }
251
GetFirstApiVersion(void)252 int GetFirstApiVersion(void)
253 {
254 return HalGetFirstApiVersion();
255 }
256
GetIncrementalVersion(void)257 const char *GetIncrementalVersion(void)
258 {
259 return HalGetIncrementalVersion();
260 }
261
BuildVersionId(void)262 static const char *BuildVersionId(void)
263 {
264 char value[VERSION_ID_LEN];
265 int len = sprintf_s(value, VERSION_ID_LEN, "%s/%s/%s/%s/%s/%s/%s/%d/%s/%s",
266 GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
267 GetOSFullName(), GetProductModel(), GetSoftwareModel(),
268 OHOS_SDK_API_VERSION, GetIncrementalVersion(), GetBuildType());
269 if (len < 0) {
270 return EMPTY_STR;
271 }
272 const char *versionId = strdup(value);
273 return versionId;
274 }
275
GetVersionId(void)276 const char *GetVersionId(void)
277 {
278 static const char *versionId = NULL;
279 if (versionId != NULL) {
280 return versionId;
281 }
282 versionId = BuildVersionId();
283 if (versionId == NULL) {
284 return EMPTY_STR;
285 }
286 return versionId;
287 }
288
GetBuildType(void)289 const char *GetBuildType(void)
290 {
291 return HalGetBuildType();
292 }
293
GetBuildUser(void)294 const char *GetBuildUser(void)
295 {
296 return HalGetBuildUser();
297 }
298
GetBuildHost(void)299 const char *GetBuildHost(void)
300 {
301 return HalGetBuildHost();
302 }
303
GetBuildTime(void)304 const char *GetBuildTime(void)
305 {
306 return HalGetBuildTime();
307 }
308
GetBuildRootHash(void)309 const char *GetBuildRootHash(void)
310 {
311 return BUILD_ROOTHASH;
312 }
313
GetOsReleaseType(void)314 const char *GetOsReleaseType(void)
315 {
316 return OHOS_RELEASE_TYPE;
317 }
318
GetMajorVersion(void)319 int GetMajorVersion(void)
320 {
321 return 0;
322 }
323
GetSeniorVersion(void)324 int GetSeniorVersion(void)
325 {
326 return 0;
327 }
328
GetFeatureVersion(void)329 int GetFeatureVersion(void)
330 {
331 return 0;
332 }
333
GetBuildVersion(void)334 int GetBuildVersion(void)
335 {
336 return 0;
337 }
338
GetDistributionOSName(void)339 const char *GetDistributionOSName(void)
340 {
341 return OHOS_DEFAULT_VALUE;
342 }
343
GetDistributionOSVersion(void)344 const char *GetDistributionOSVersion(void)
345 {
346 return OHOS_DEFAULT_VALUE;
347 }
348
GetDistributionOSApiVersion(void)349 int GetDistributionOSApiVersion(void)
350 {
351 return 0;
352 }
353
GetDistributionOSReleaseType(void)354 const char *GetDistributionOSReleaseType(void)
355 {
356 return OHOS_DEFAULT_VALUE;
357 }
358
GetDevUdid(char * udid,int size)359 int GetDevUdid(char *udid, int size)
360 {
361 if (udid == NULL || size < 0) {
362 return -1;
363 }
364 return 0;
365 }