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
16 #ifdef __LITEOS_M__
17
18 #include "attest_utils_log.h"
19 #include "attest_adapter.h"
20
21 #if defined(CHIP_VER_Hi3861)
22 #include <hi_mdm.h>
23
24 #define HI_NV_XTS_DEV_ATTEST_NET 0x60
25 #define HI_NV_NET_SIZE 164
26
27 typedef struct {
28 uint8_t nv_dev_attest_net[HI_NV_NET_SIZE];
29 } hi_nv_xts_dev_attest_net_cfg;
30
CopyNVData(char * dst,int32_t dstLen,unsigned char * src,int32_t srcLen)31 static int32_t CopyNVData(char *dst, int32_t dstLen, unsigned char *src, int32_t srcLen)
32 {
33 if (dst == NULL || src == NULL) {
34 ATTEST_LOG_ERROR("[CopyNVData] input paramter wrong");
35 return ATTEST_ERR;
36 }
37 int32_t dataLen = (dstLen < srcLen) ? dstLen : srcLen;
38 for (int32_t i = 0; i < dataLen; i++) {
39 dst[i] = (char)src[i];
40 }
41 return ATTEST_OK;
42 }
43
44 // 读取网络配置信息
AttestReadNetworkConfig(char * buffer,uint32_t bufferLen)45 int32_t AttestReadNetworkConfig(char* buffer, uint32_t bufferLen)
46 {
47 hi_nv_xts_dev_attest_net_cfg nv_net = { 0 };
48 int32_t ret = hi_nv_read(HI_NV_XTS_DEV_ATTEST_NET, (void*)&nv_net, sizeof(nv_net), 0);
49 if (ret != ATTEST_OK) {
50 ATTEST_LOG_ERROR("[AttestReadNetworkConfig] hi_nv_read failed");
51 return ATTEST_ERR;
52 }
53 return CopyNVData(buffer, bufferLen, nv_net.nv_dev_attest_net, sizeof(hi_nv_xts_dev_attest_net_cfg));
54 }
55
56 // 写入网络配置信息
AttestWriteNetworkConfig(const char * data,uint32_t len)57 int32_t AttestWriteNetworkConfig(const char* data, uint32_t len)
58 {
59 if (data == NULL || len > HI_NV_NET_SIZE) {
60 ATTEST_LOG_ERROR("[AttestWriteNetworkConfig] invalid parameter");
61 return ATTEST_ERR;
62 }
63 hi_nv_xts_dev_attest_net_cfg nv_net = { 0 };
64 int32_t ret = CopyNVData(nv_net.nv_dev_attest_net, sizeof(hi_nv_xts_dev_attest_net_cfg), data, len);
65 if (ret != ATTEST_OK) {
66 ATTEST_LOG_ERROR("[AttestWriteNetworkConfig] copy data to NV failed");
67 return ATTEST_ERR;
68 }
69 ret = hi_nv_write(HI_NV_XTS_DEV_ATTEST_NET, (void*)&nv_net, sizeof(nv_net), 0);
70 if (ret != ATTEST_OK) {
71 ATTEST_LOG_ERROR("[AttestWriteNetworkConfig] nv write failed");
72 return ATTEST_ERR;
73 }
74 return ATTEST_OK;
75 }
76
77 #else
78
AttestReadNetworkConfig(char * buffer,uint32_t bufferLen)79 int32_t AttestReadNetworkConfig(char* buffer, uint32_t bufferLen)
80 {
81 (void)buffer;
82 (void)bufferLen;
83 ATTEST_LOG_ERROR("[AttestReadNetworkConfig] Need to implement interface AttestReadNetworkConfig!");
84 return ATTEST_ERR;
85 }
86
AttestWriteNetworkConfig(const char * data,uint32_t bufferLen)87 int32_t AttestWriteNetworkConfig(const char* data, uint32_t bufferLen)
88 {
89 (void)data;
90 (void)bufferLen;
91 ATTEST_LOG_ERROR("[AttestWriteNetworkConfig] Need to implement interface AttestWriteNetworkConfig!");
92 return ATTEST_ERR;
93 }
94
95 #endif // defined(CHIP_VER_Hi3861)
96
97 #endif // __LITEOS_M__
98