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
45 static const int MAJOR_VERSION = 1;
46 static const int SENIOR_VERSION = 0;
47 static const int FEATURE_VERSION = 1;
48 static const int BUILD_VERSION = 0;
49
IsValidValue(const char * value,unsigned int len)50 static boolean IsValidValue(const char *value, unsigned int len)
51 {
52 if ((value == NULL) || !strlen(value) || (strlen(value) + 1 > len)) {
53 return FALSE;
54 }
55 return TRUE;
56 }
57
GetParameter(const char * key,const char * def,char * value,unsigned int len)58 int GetParameter(const char *key, const char *def, char *value, unsigned int len)
59 {
60 if ((key == NULL) || (value == NULL)) {
61 return EC_INVALID;
62 }
63 if (!CheckPermission()) {
64 return EC_FAILURE;
65 }
66 int ret = GetSysParam(key, value, len);
67 if (ret == EC_INVALID) {
68 return EC_INVALID;
69 }
70 if ((ret < 0) && IsValidValue(def, len)) {
71 if (strncpy_s(value, len, def, len - 1) != 0) {
72 return EC_FAILURE;
73 }
74 ret = (int)strlen(def);
75 }
76 return ret;
77 }
78
SetParameter(const char * key,const char * value)79 int SetParameter(const char *key, const char *value)
80 {
81 if ((key == NULL) || (value == NULL)) {
82 return EC_INVALID;
83 }
84 if (!CheckPermission()) {
85 return EC_FAILURE;
86 }
87 if (strncmp(key, FILE_RO, strlen(FILE_RO)) == 0) {
88 return EC_INVALID;
89 }
90
91 return SetSysParam(key, value);
92 }
93
GetDeviceType(void)94 const char *GetDeviceType(void)
95 {
96 return HalGetDeviceType();
97 }
98
GetManufacture(void)99 const char *GetManufacture(void)
100 {
101 return HalGetManufacture();
102 }
103
GetBrand(void)104 const char *GetBrand(void)
105 {
106 return HalGetBrand();
107 }
108
GetMarketName(void)109 const char *GetMarketName(void)
110 {
111 return HalGetMarketName();
112 }
113
GetProductSeries(void)114 const char *GetProductSeries(void)
115 {
116 return HalGetProductSeries();
117 }
118
GetProductModel(void)119 const char *GetProductModel(void)
120 {
121 return HalGetProductModel();
122 }
123
GetSoftwareModel(void)124 const char *GetSoftwareModel(void)
125 {
126 return HalGetSoftwareModel();
127 }
128
GetHardwareModel(void)129 const char *GetHardwareModel(void)
130 {
131 return HalGetHardwareModel();
132 }
133
GetHardwareProfile(void)134 const char *GetHardwareProfile(void)
135 {
136 return HalGetHardwareProfile();
137 }
138
GetSerial(void)139 const char *GetSerial(void)
140 {
141 return HalGetSerial();
142 }
143
GetBootloaderVersion(void)144 const char *GetBootloaderVersion(void)
145 {
146 return HalGetBootloaderVersion();
147 }
148
GetSecurityPatchTag(void)149 const char *GetSecurityPatchTag(void)
150 {
151 return OHOS_SECURITY_PATCH_TAG;
152 }
153
GetAbiList(void)154 const char *GetAbiList(void)
155 {
156 return HalGetAbiList();
157 }
158
BuildOSFullName(void)159 static const char *BuildOSFullName(void)
160 {
161 const char release[] = "Release";
162 char value[OS_FULL_NAME_LEN];
163 const char *releaseType = GetOsReleaseType();
164 int length;
165 if (strncmp(releaseType, release, sizeof(release) - 1) == 0) {
166 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d",
167 OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION);
168 } else {
169 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d(%s)",
170 OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION, releaseType);
171 }
172 if (length < 0) {
173 return EMPTY_STR;
174 }
175 const char *osFullName = strdup(value);
176 return osFullName;
177 }
178
GetOSFullName(void)179 const char *GetOSFullName(void)
180 {
181 static const char *osFullName = NULL;
182 if (osFullName != NULL) {
183 return osFullName;
184 }
185 osFullName = BuildOSFullName();
186 if (osFullName == NULL) {
187 return EMPTY_STR;
188 }
189 return osFullName;
190 }
191
BuildDisplayVersion(void)192 static const char *BuildDisplayVersion(void)
193 {
194 ssize_t len;
195 char patchValue[OHOS_PATCH_VERSION_LEN] = {0};
196 char displayValue[OHOS_DISPLAY_VERSION_LEN] = {0};
197 int fd = open(OHOS_PATCH_VERSION_FILE, O_RDONLY);
198 if (fd < 0) {
199 return NULL;
200 }
201 len = read(fd, patchValue, OHOS_PATCH_VERSION_LEN);
202 if (len < (ssize_t)strlen("version=")) {
203 close(fd);
204 return NULL;
205 }
206 close(fd);
207 if (patchValue[len - 1] == '\n') {
208 patchValue[len - 1] = '\0';
209 }
210 const char *versionValue = HalGetDisplayVersion();
211 const int versionLen = strlen(versionValue);
212 if (versionLen > 0) {
213 if (versionValue[versionLen - 1] != ')') {
214 len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s(%s)", versionValue,
215 patchValue + strlen("version="));
216 } else {
217 char tempValue[versionLen];
218 (void)memset_s(tempValue, versionLen, 0, versionLen);
219 if (strncpy_s(tempValue, versionLen, versionValue, versionLen - 1) != 0) {
220 return NULL;
221 }
222 tempValue[versionLen - 1] = '\0';
223 len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s%s)", tempValue,
224 patchValue + strlen("version="));
225 }
226 }
227 if (len < 0) {
228 return NULL;
229 }
230 return strdup(displayValue);
231 }
232
GetDisplayVersion(void)233 const char *GetDisplayVersion(void)
234 {
235 static const char *displayVersion = NULL;
236 if (displayVersion != NULL) {
237 return displayVersion;
238 }
239 displayVersion = BuildDisplayVersion();
240 if (displayVersion == NULL) {
241 return HalGetDisplayVersion();
242 }
243 return displayVersion;
244 }
245
GetSdkApiVersion(void)246 int GetSdkApiVersion(void)
247 {
248 return OHOS_SDK_API_VERSION;
249 }
250
GetFirstApiVersion(void)251 int GetFirstApiVersion(void)
252 {
253 return HalGetFirstApiVersion();
254 }
255
GetIncrementalVersion(void)256 const char *GetIncrementalVersion(void)
257 {
258 return HalGetIncrementalVersion();
259 }
260
BuildVersionId(void)261 static const char *BuildVersionId(void)
262 {
263 char value[VERSION_ID_LEN];
264 int len = sprintf_s(value, VERSION_ID_LEN, "%s/%s/%s/%s/%s/%s/%s/%d/%s/%s",
265 GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
266 GetOSFullName(), GetProductModel(), GetSoftwareModel(),
267 OHOS_SDK_API_VERSION, GetIncrementalVersion(), GetBuildType());
268 if (len < 0) {
269 return EMPTY_STR;
270 }
271 const char *versionId = strdup(value);
272 return versionId;
273 }
274
GetVersionId(void)275 const char *GetVersionId(void)
276 {
277 static const char *versionId = NULL;
278 if (versionId != NULL) {
279 return versionId;
280 }
281 versionId = BuildVersionId();
282 if (versionId == NULL) {
283 return EMPTY_STR;
284 }
285 return versionId;
286 }
287
GetBuildType(void)288 const char *GetBuildType(void)
289 {
290 return HalGetBuildType();
291 }
292
GetBuildUser(void)293 const char *GetBuildUser(void)
294 {
295 return HalGetBuildUser();
296 }
297
GetBuildHost(void)298 const char *GetBuildHost(void)
299 {
300 return HalGetBuildHost();
301 }
302
GetBuildTime(void)303 const char *GetBuildTime(void)
304 {
305 return HalGetBuildTime();
306 }
307
GetBuildRootHash(void)308 const char *GetBuildRootHash(void)
309 {
310 return BUILD_ROOTHASH;
311 }
312
GetOsReleaseType(void)313 const char *GetOsReleaseType(void)
314 {
315 return OHOS_RELEASE_TYPE;
316 }
317