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 #include <limits.h>
16 #include "udid.h"
17
18 #ifdef OHOS_LITE
19 #include "hal_sys_param.h"
20 #endif
21 #include "init_param.h"
22 #include "param_comm.h"
23 #include "securec.h"
24 #include "sysparam_errno.h"
25
GetSerial_(void)26 INIT_LOCAL_API const char *GetSerial_(void)
27 {
28 #ifdef OHOS_LITE
29 return HalGetSerial();
30 #else
31 static char *ohosSerial = NULL;
32 if (ohosSerial != NULL) {
33 return ohosSerial;
34 }
35
36 char *value;
37 BEGET_CHECK((value = (char *)calloc(1, PARAM_VALUE_LEN_MAX)) != NULL, return NULL);
38 uint32_t len = PARAM_VALUE_LEN_MAX;
39 int ret = SystemGetParameter("ohos.boot.sn", value, &len);
40 BEGET_CHECK(ret == 0, free(value);
41 return NULL);
42 if (ohosSerial != NULL) {
43 free(value);
44 return ohosSerial;
45 }
46 ohosSerial = value;
47 return ohosSerial;
48 #endif
49 }
50
GetUdidFromParam(char * udid,uint32_t size)51 INIT_LOCAL_API int GetUdidFromParam(char *udid, uint32_t size)
52 {
53 uint32_t len = size;
54 int ret = SystemGetParameter("const.product.udid", udid, &len);
55 BEGET_CHECK(ret != 0, return ret);
56
57 len = size;
58 ret = SystemGetParameter("const.product.devUdid", udid, &len);
59 BEGET_CHECK(ret != 0, return ret);
60 return ret;
61 }
62
GetDevUdid_(char * udid,int size)63 INIT_LOCAL_API int GetDevUdid_(char *udid, int size)
64 {
65 if (size < UDID_LEN || udid == NULL) {
66 return EC_FAILURE;
67 }
68
69 int ret = GetUdidFromParam(udid, (uint32_t)size);
70 BEGET_CHECK(ret != 0, return ret);
71
72 #ifdef OHOS_LITE
73 ret = CalcDevUdid(udid, size);
74 #endif
75 return ret;
76 }
77
GetDiskSN_(char * diskSN,int size)78 INIT_LOCAL_API int GetDiskSN_(char *diskSN, int size)
79 {
80 if (size < DISK_SN_LEN || diskSN == NULL) {
81 return EC_FAILURE;
82 }
83 char diskSNPath[DISK_SN_PATH_LEN] = {0};
84 uint32_t diskSNPathLen = DISK_SN_PATH_LEN;
85 if (SystemGetParameter("const.disk.sn.filepath", diskSNPath, &diskSNPathLen) != 0) {
86 BEGET_LOGE("const.disk.sn.filepath read failed");
87 return EC_FAILURE;
88 }
89 char realDiskSNPath[PATH_MAX] = {0};
90 if (realpath(diskSNPath, realDiskSNPath) == NULL) {
91 BEGET_LOGE("GetDiskSN_ path file failed");
92 return EC_FAILURE;
93 }
94 FILE *file = fopen(realDiskSNPath, "r");
95 if (file == NULL) {
96 BEGET_LOGE("GetDiskSN_ open file failed");
97 return EC_FAILURE;
98 }
99 if (fscanf_s(file, "%s", diskSN, size) <= 0) {
100 BEGET_LOGE("GetDiskSN_ read file failed");
101 if (fclose(file) != 0) {
102 BEGET_LOGE("GetDiskSN_ close file failed");
103 }
104 return EC_FAILURE;
105 }
106 if (fclose(file) != 0) {
107 BEGET_LOGE("GetDiskSN_ close file failed");
108 }
109 return 0;
110 }