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 
GetDigitalCidLocation(int i)38 static int GetDigitalCidLocation(int i)
39 {
40     return ((i / HEX_OF_BINARY_BITS + 1) * HEX_OF_BINARY_BITS - (i % HEX_OF_BINARY_BITS) - 1) * TWO_TIMES;
41 }
42 
TranslateCid(uint8_t *cid, uint32_t cidLen, char *digitalCid, uint32_t digitalCidLen)43 static int32_t TranslateCid(uint8_t *cid, uint32_t cidLen, char *digitalCid, uint32_t digitalCidLen)
44 {
45     if (cid == NULL || digitalCid == NULL || cidLen * TWO_TIMES + 1 != digitalCidLen) {
46         return CID_ERROR;
47     }
48     uint8_t tmp;
49     for (int i = 0; i < cidLen; i++) {
50         tmp = cid[i] / LAST_FOUR_BINARY_DIGITS;
51         if (tmp < DIVIDE_NUMBER_AND_LETTERS) {
52             digitalCid[GetDigitalCidLocation(i)] = tmp + '0';
53         } else {
54             digitalCid[GetDigitalCidLocation(i)] = tmp - DIVIDE_NUMBER_AND_LETTERS + 'a';
55         }
56         tmp = cid[i] % LAST_FOUR_BINARY_DIGITS;
57         if (tmp < DIVIDE_NUMBER_AND_LETTERS) {
58             digitalCid[GetDigitalCidLocation(i) + 1] = tmp + '0';
59         } else {
60             digitalCid[GetDigitalCidLocation(i) + 1] = tmp - DIVIDE_NUMBER_AND_LETTERS + 'a';
61         }
62     }
63     digitalCid[digitalCidLen - 1] = '\0';
64     return CID_OK;
65 }
66 
Getcid(char * str, int strlength)67 static int32_t Getcid(char * str, int strlength)
68 {
69     if (str == NULL) {
70         return CID_ERROR;
71     }
72     uint8_t cid[CID_LENGTH] = {0};
73     DevHandle handle = EmmcOpen(0);
74     if (handle == NULL) {
75         return CID_ERROR;
76     }
77     if (EmmcGetCid(handle, cid, CID_LENGTH) != HDF_SUCCESS) {
78         EmmcClose(handle);
79         return CID_ERROR;
80     }
81     EmmcClose(handle);
82     char digitalCid[DIGITAL_CID_LENGTH] = {0};
83     if (TranslateCid(cid, CID_LENGTH, digitalCid, DIGITAL_CID_LENGTH) != CID_OK) {
84         return CID_ERROR;
85     }
86     if (strncpy_s(str, strlength, digitalCid, strlen(digitalCid)) != EOK) {
87         return CID_ERROR;
88     }
89     return CID_OK;
90 }
91 
HalGetSerial(void)92 const char* HalGetSerial(void)
93 {
94     static char str[STR_MAX] = {0};
95     if (strlen(str) > 0) {
96         return str;
97     }
98     if (Getcid(str, STR_MAX) != CID_OK) {
99         return NULL;
100     }
101     return str;
102 }
103