• 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 #ifdef __LITEOS_M__
16 #include "securec.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <limits.h>
20 #include "utils_file.h"
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include "attest_utils_log.h"
25 #include "attest_utils.h"
26 #include "attest_utils_file.h"
27 
28 static const uint32_t MAX_FILE_BYTES_LIMIT = 5120;
29 
30 #if defined(CHIP_VER_Hi3861)
31 #include <hi_tsensor.h>
32 
33 #define WRITE_FLASH_MAX_TEMPERATURE 80
34 
35 // 轻量设备温度监控
IsOverTemperatureLimit(void)36 bool IsOverTemperatureLimit(void)
37 {
38     hi_s16 temperature = 0;
39     int32_t ret = hi_tsensor_read_temperature(&temperature);
40     if (ret != HI_ERR_SUCCESS) {
41         ret = hi_tsensor_read_temperature(&temperature);
42         if (ret != HI_ERR_SUCCESS) {
43             ATTEST_LOG_ERROR("[IsOverTemperatureLimit]: Get temperature fail, ret = %d", ret);
44             return true;
45         }
46     }
47     ATTEST_LOG_DEBUG("[IsOverTemperatureLimit]: device's temperature = %d", temperature);
48     return (temperature >= WRITE_FLASH_MAX_TEMPERATURE);
49 }
50 
51 #else
52 
IsOverTemperatureLimit(void)53 bool IsOverTemperatureLimit(void)
54 {
55     return false;
56 }
57 
58 #endif // defined(CHIP_VER_Hi3861)
59 
GetFileSize(const char * path,const char * fileName,uint32_t * result)60 int32_t GetFileSize(const char* path, const char* fileName, uint32_t* result)
61 {
62     if (path == NULL || fileName == NULL || result == NULL) {
63         ATTEST_LOG_ERROR("[GetFileSize] Invalid parameter");
64         return ATTEST_ERR;
65     }
66     int32_t ret = UtilsFileStat(fileName, result);
67     if (ret < 0) {
68         return ATTEST_ERR;
69     }
70     return ATTEST_OK;
71 }
72 
WriteFile(const char * path,const char * fileName,const char * data,uint32_t dataLen)73 int32_t WriteFile(const char* path, const char* fileName, const char* data, uint32_t dataLen)
74 {
75     if (path == NULL || fileName == NULL || data == NULL || dataLen == 0) {
76         ATTEST_LOG_ERROR("[WriteFile] Invalid parameter");
77         return ATTEST_ERR;
78     }
79 
80     if (dataLen > MAX_FILE_BYTES_LIMIT) {
81         return ATTEST_ERR;
82     }
83     if (IsOverTemperatureLimit()) {
84         return ATTEST_ERR;
85     }
86 
87     int32_t fd = UtilsFileOpen(fileName, O_CREAT_FS | O_TRUNC_FS | O_RDWR_FS, 0);
88     if (fd < 0) {
89         ATTEST_LOG_ERROR("[WriteFile] : Open file failed");
90         return ATTEST_ERR;
91     }
92 
93     int ret = 0;
94     if (UtilsFileWrite(fd, data, dataLen) != (int32_t)dataLen) {
95         ATTEST_LOG_ERROR("[WriteFile] : Write data failed");
96         ret = ATTEST_ERR;
97     }
98     (void)UtilsFileClose(fd);
99     if (ret < 0) {
100         return ATTEST_ERR;
101     }
102     return ATTEST_OK;
103 }
104 
ReadFile(const char * path,const char * fileName,char * buffer,uint32_t bufferLen)105 int32_t ReadFile(const char* path, const char* fileName, char* buffer, uint32_t bufferLen)
106 {
107     if (path == NULL || fileName == NULL || buffer == NULL || bufferLen == 0) {
108         ATTEST_LOG_ERROR("[ReadFile] Invalid parameter");
109         return ATTEST_ERR;
110     }
111 
112     uint32_t fileSize = 0;
113     if (GetFileSize(path, fileName, &fileSize) != 0 || fileSize > bufferLen) {
114         ATTEST_LOG_ERROR("[ReadFile] Invalid fileSize");
115         return ATTEST_ERR;
116     }
117 
118     int32_t fd = UtilsFileOpen(fileName, O_EXCL_FS | O_RDWR_FS, 0);
119     if (fd < 0) {
120         ATTEST_LOG_ERROR("[ReadFile] : Open file failed");
121         return ATTEST_ERR;
122     }
123 
124     int32_t ret = 0;
125     ret = UtilsFileRead(fd, buffer, bufferLen);
126     (void)UtilsFileClose(fd);
127     if (ret < 0) {
128         return ATTEST_ERR;
129     }
130     return ATTEST_OK;
131 }
132 
CreateFile(const char * path,const char * fileName)133 int32_t CreateFile(const char* path, const char* fileName)
134 {
135     if (path == NULL || fileName == NULL) {
136         return ATTEST_ERR;
137     }
138 
139     int32_t fd = UtilsFileOpen(fileName, O_CREAT_FS, 0);
140     if (fd < 0) {
141         return ATTEST_ERR;
142     }
143     (void)UtilsFileClose(fd);
144     return ATTEST_OK;
145 }
146 
IsFileExist(const char * path,const char * fileName)147 bool IsFileExist(const char* path, const char* fileName)
148 {
149     if (path == NULL || fileName == NULL) {
150         return false;
151     }
152 
153     int32_t fd = UtilsFileOpen(fileName, O_RDONLY_FS, 0);
154     if (fd < 0) {
155         return false;
156     }
157     (void)UtilsFileClose(fd);
158     return true;
159 }
160 
DeleteFile(const char * path,const char * fileName)161 bool DeleteFile(const char* path, const char* fileName)
162 {
163     if (path == NULL || fileName == NULL) {
164         return false;
165     }
166     if (strlen(path) == 0 || strlen(fileName) == 0) {
167         return false;
168     }
169 
170     int32_t ret = UtilsFileDelete(fileName);
171     if (ret != 0) {
172         return false;
173     }
174     return true;
175 }
176 #endif // __LITEOS_M__
177