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 #include "hal_sys_param.h"
16
17 #include <emmc_if.h>
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24
25 #include "securec.h"
26
27 #define STR_MAX 65
28 #define CID_LENGTH 16
29 #define EOK 0
30 #define TWO_TIMES 2
31 #define DIGITAL_CID_LENGTH (CID_LENGTH * TWO_TIMES + 1)
32 #define HEX_OF_BINARY_BITS 4
33 #define LAST_FOUR_BINARY_DIGITS 16
34 #define DIVIDE_NUMBER_AND_LETTERS 10
35 #define CID_ERROR (-1)
36 #define CID_OK 1
37
38 static const char OHOS_DEVICE_TYPE[] = {"****"};
39 static const char OHOS_DISPLAY_VERSION[] = {"OpenHarmony 1.0.1"};
40 static const char OHOS_MANUFACTURE[] = {"****"};
41 static const char OHOS_BRAND[] = {"****"};
42 static const char OHOS_MARKET_NAME[] = {"****"};
43 static const char OHOS_PRODUCT_SERIES[] = {"****"};
44 static const char OHOS_PRODUCT_MODEL[] = {"****"};
45 static const char OHOS_SOFTWARE_MODEL[] = {"****"};
46 static const char OHOS_HARDWARE_MODEL[] = {"****"};
47 static const char OHOS_HARDWARE_PROFILE[] = {"aout:true,display:true"};
48 static const char OHOS_BOOTLOADER_VERSION[] = {"bootloader"};
49 static const char OHOS_ABI_LIST[] = {"****"};
50 static const int OHOS_FIRST_API_VERSION = 1;
51
HalGetDeviceType(void)52 const char* HalGetDeviceType(void)
53 {
54 return OHOS_DEVICE_TYPE;
55 }
56
HalGetManufacture(void)57 const char* HalGetManufacture(void)
58 {
59 return OHOS_MANUFACTURE;
60 }
61
HalGetBrand(void)62 const char* HalGetBrand(void)
63 {
64 return OHOS_BRAND;
65 }
66
HalGetMarketName(void)67 const char* HalGetMarketName(void)
68 {
69 return OHOS_MARKET_NAME;
70 }
71
HalGetProductSeries(void)72 const char* HalGetProductSeries(void)
73 {
74 return OHOS_PRODUCT_SERIES;
75 }
76
HalGetProductModel(void)77 const char* HalGetProductModel(void)
78 {
79 return OHOS_PRODUCT_MODEL;
80 }
81
HalGetSoftwareModel(void)82 const char* HalGetSoftwareModel(void)
83 {
84 return OHOS_SOFTWARE_MODEL;
85 }
86
HalGetHardwareModel(void)87 const char* HalGetHardwareModel(void)
88 {
89 return OHOS_HARDWARE_MODEL;
90 }
91
HalGetHardwareProfile(void)92 const char* HalGetHardwareProfile(void)
93 {
94 return OHOS_HARDWARE_PROFILE;
95 }
96
GetDigitalCidLocation(int i)97 static int GetDigitalCidLocation(int i)
98 {
99 return ((i / HEX_OF_BINARY_BITS + 1) * HEX_OF_BINARY_BITS - (i % HEX_OF_BINARY_BITS) - 1) * TWO_TIMES;
100 }
101
TranslateCid(uint8_t * cid,uint32_t cidLen,char * digitalCid,uint32_t digitalCidLen)102 static int32_t TranslateCid(uint8_t *cid, uint32_t cidLen, char *digitalCid, uint32_t digitalCidLen)
103 {
104 if (cid == NULL || digitalCid == NULL || cidLen * TWO_TIMES + 1 != digitalCidLen) {
105 return CID_ERROR;
106 }
107 uint8_t tmp;
108 for (int i = 0; i < cidLen; i++) {
109 tmp = cid[i] / LAST_FOUR_BINARY_DIGITS;
110 if (tmp < DIVIDE_NUMBER_AND_LETTERS) {
111 digitalCid[GetDigitalCidLocation(i)] = tmp + '0';
112 } else {
113 digitalCid[GetDigitalCidLocation(i)] = tmp - DIVIDE_NUMBER_AND_LETTERS + 'a';
114 }
115 tmp = cid[i] % LAST_FOUR_BINARY_DIGITS;
116 if (tmp < DIVIDE_NUMBER_AND_LETTERS) {
117 digitalCid[GetDigitalCidLocation(i) + 1] = tmp + '0';
118 } else {
119 digitalCid[GetDigitalCidLocation(i) + 1] = tmp - DIVIDE_NUMBER_AND_LETTERS + 'a';
120 }
121 }
122 digitalCid[digitalCidLen - 1] = '\0';
123 return CID_OK;
124 }
125
Getcid(char * str,int strlength)126 static int32_t Getcid(char * str, int strlength)
127 {
128 if (str == NULL) {
129 return CID_ERROR;
130 }
131 uint8_t cid[CID_LENGTH] = {0};
132 DevHandle handle = EmmcOpen(0);
133 if (handle == NULL) {
134 return CID_ERROR;
135 }
136 if (EmmcGetCid(handle, cid, CID_LENGTH) != HDF_SUCCESS) {
137 EmmcClose(handle);
138 return CID_ERROR;
139 }
140 EmmcClose(handle);
141 char digitalCid[DIGITAL_CID_LENGTH] = {0};
142 if (TranslateCid(cid, CID_LENGTH, digitalCid, DIGITAL_CID_LENGTH) != CID_OK) {
143 return CID_ERROR;
144 }
145 if (strncpy_s(str, strlength, digitalCid, strlen(digitalCid)) != EOK) {
146 return CID_ERROR;
147 }
148 return CID_OK;
149 }
150
HalGetSerial(void)151 const char* HalGetSerial(void)
152 {
153 static char str[STR_MAX] = {0};
154 if (strlen(str) > 0) {
155 return str;
156 }
157 if (Getcid(str, STR_MAX) != CID_OK) {
158 return NULL;
159 }
160 return str;
161 }
162
HalGetBootloaderVersion(void)163 const char* HalGetBootloaderVersion(void)
164 {
165 return OHOS_BOOTLOADER_VERSION;
166 }
167
HalGetAbiList(void)168 const char* HalGetAbiList(void)
169 {
170 return OHOS_ABI_LIST;
171 }
172
HalGetDisplayVersion(void)173 const char* HalGetDisplayVersion(void)
174 {
175 return OHOS_DISPLAY_VERSION;
176 }
177
HalGetIncrementalVersion(void)178 const char* HalGetIncrementalVersion(void)
179 {
180 return INCREMENTAL_VERSION;
181 }
182
HalGetBuildType(void)183 const char* HalGetBuildType(void)
184 {
185 return BUILD_TYPE;
186 }
187
HalGetBuildUser(void)188 const char* HalGetBuildUser(void)
189 {
190 return BUILD_USER;
191 }
192
HalGetBuildHost(void)193 const char* HalGetBuildHost(void)
194 {
195 return BUILD_HOST;
196 }
197
HalGetBuildTime(void)198 const char* HalGetBuildTime(void)
199 {
200 return BUILD_TIME;
201 }
202
HalGetFirstApiVersion(void)203 int HalGetFirstApiVersion(void)
204 {
205 return OHOS_FIRST_API_VERSION;
206 }
207