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