2 * @copyright Copyright(c) 2018-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <sys/syscall.h>
26 #include "nv_hal_nvhallog.h"
31 const char *kMediaType[NVHALMEDIA_MAX] = {
33 "/var/local/lib/basesystem/ramd/BS/ns/backup_manager/rwdata/",
34 "/var/local/lib/basesystem/nv/BS/ns/backup_manager/rwdata/",
37 const int kMaxPath = 127; // Max length of path
42 EFrameworkunifiedStatus InitNv(void) {
43 return eFrameworkunifiedStatusOK;
49 EFrameworkunifiedStatus GetSizeNv(enum NvHalMedia media, const char *filename, uint32_t *size) {
50 if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) { // Argument range checking
51 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
52 return eFrameworkunifiedStatusInvldParam;
54 if (NULL == filename) { // NULL checking of arguments
55 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
56 return eFrameworkunifiedStatusInvldParam;
58 if (NULL == size) { // NULL checking of arguments
59 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "size is NULL.\n");
60 return eFrameworkunifiedStatusInvldParam;
63 memset(path, '\0', kMaxPath);
64 snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
65 // Geting the file size
66 struct stat file_stat;
67 if (0 > lstat(path, &file_stat)) {
68 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lstat() error, errno=%d\n", errno);
69 return eFrameworkunifiedStatusFileLoadError;
71 *size = file_stat.st_size;
72 return eFrameworkunifiedStatusOK;
78 * Reading data from memory device.
80 EFrameworkunifiedStatus ReadNv(enum NvHalMedia media, const char *filename, uint8_t *buffer, uint32_t size) {
81 if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) {
82 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
83 return eFrameworkunifiedStatusInvldParam;
85 if (NULL == filename) {
86 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
87 return eFrameworkunifiedStatusInvldParam;
90 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "buffer is NULL.\n");
91 return eFrameworkunifiedStatusInvldParam;
94 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid size:%d\n", size);
95 return eFrameworkunifiedStatusInvldParam;
98 memset(path, '\0', kMaxPath);
99 snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
100 struct stat file_stat;
101 if (0 > lstat(path, &file_stat)) {
102 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lstat() error, errno=%d\n", errno);
103 return eFrameworkunifiedStatusFail;
105 if (file_stat.st_size != size) {
106 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "size error:%ld != %d\n", file_stat.st_size, size);
107 return eFrameworkunifiedStatusFail;
109 int fd = open(path, O_RDONLY);
111 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "open(%s) error, errno=%d\n", path, errno);
112 return eFrameworkunifiedStatusFail;
115 // Read data by support EINTR
116 for (tot_read = 0; tot_read < static_cast<size_t>(size);) {
117 ssize_t read_size = pread(fd, &buffer[tot_read], static_cast<size_t>(size) - tot_read, tot_read);
118 if (-1 == read_size) {
119 if (errno == EINTR) {
122 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "pread() error, errno=%d\n", errno);
123 if (0 != close(fd)) {
124 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
126 return eFrameworkunifiedStatusFail;
129 tot_read += read_size;
131 if (0 != close(fd)) {
132 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
133 return eFrameworkunifiedStatusFail;
136 return eFrameworkunifiedStatusOK;
140 * Writing data to memory device.
142 EFrameworkunifiedStatus WriteNv(enum NvHalMedia media, const char *filename, uint8_t *buffer, uint32_t size) {
143 if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) {
144 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
145 return eFrameworkunifiedStatusInvldParam;
147 if (NULL == filename) {
148 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
149 return eFrameworkunifiedStatusInvldParam;
151 if (NULL == buffer) {
152 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "buffer is NULL.\n");
153 return eFrameworkunifiedStatusInvldParam;
156 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid size:%d\n", size);
157 return eFrameworkunifiedStatusInvldParam;
160 memset(path, '\0', kMaxPath);
161 snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
162 struct stat file_stat;
163 // Check file exists or not, mkdir first if file no exists.
164 if (0 > lstat(path, &file_stat)) {
165 char *dir_point = path;
166 char dir_buff[kMaxPath];
168 while ((dir_point = strchr(dir_point, '/'))) {
169 memset(dir_buff, '\0', kMaxPath);
170 memcpy(dir_buff, path, dir_point - path);
171 if (0 > mkdir(dir_buff, 0770)) {
172 if (EEXIST != errno) {
173 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "mkdir() error, errno=%d\n", errno);
174 return eFrameworkunifiedStatusFail;
183 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0660);
185 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "open(%s) error, errno=%d\n", path, errno);
186 return eFrameworkunifiedStatusFail;
189 // Write data by support EINTR
190 for (tot_written = 0; tot_written < static_cast<size_t>(size);) {
191 ssize_t write_size = pwrite(fd, &buffer[tot_written], static_cast<size_t>(size) - tot_written, tot_written);
192 if (0 >= write_size) {
193 if (-1 == write_size && errno == EINTR) {
196 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "pwrite() error, errno=%d\n", errno);
197 if (0 != close(fd)) {
198 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
200 return eFrameworkunifiedStatusFail;
203 tot_written += write_size;
205 if (0 != close(fd)) {
206 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "close() error, errno=%d\n", errno);
207 return eFrameworkunifiedStatusFail;
210 return eFrameworkunifiedStatusOK;
216 EFrameworkunifiedStatus DeleteNv(enum NvHalMedia media, const char *filename) {
217 if ((media < NVHALMEDIA_CACHEDRAM) || (media > NVHALMEDIA_NAND)) {
218 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid media type:%d\n", media);
219 return eFrameworkunifiedStatusInvldParam;
221 if (NULL == filename) {
222 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "filename is NULL.\n");
223 return eFrameworkunifiedStatusInvldParam;
227 memset(path, '\0', kMaxPath);
228 snprintf(path, kMaxPath, "%s%s", kMediaType[media], filename);
229 struct stat file_stat;
231 if (0 > lstat(path, &file_stat)) {
232 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lstat() error, errno=%d\n", errno);
233 return eFrameworkunifiedStatusFail;
236 if (0 != unlink(path)) {
237 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "unlink() error, errno=%d\n", errno);
238 return eFrameworkunifiedStatusFail;
241 return eFrameworkunifiedStatusOK;