blob: d1cb7de6628f55c7ac95fa9c06a583742b686044 [file] [log] [blame]
b.liu3a41a312024-02-28 09:57:39 +08001/*
2* mbtk_device_info.c
3*
4* MBTK device partition information utils.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/2/27 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <errno.h>
b.liubb5e7682024-02-28 20:13:04 +080020#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
b.liuf678f992024-05-08 15:23:10 +080023#include <sys/ioctl.h>
24#include <mtd/mtd-user.h>
b.liu3a41a312024-02-28 09:57:39 +080025
b.liuf678f992024-05-08 15:23:10 +080026#include "mbtk_type.h"
b.liu3a41a312024-02-28 09:57:39 +080027#include "mbtk_log.h"
28#include "mbtk_str.h"
29#include "mbtk_mtd.h"
30
b.liu472cfaf2024-12-19 19:08:19 +080031#ifdef MBTK_DEV_INFO_VERSION_2
32#include "mbtk_device_v2.h"
33#else
34#include "mbtk_device.h"
35#endif
36
37
b.liu3a41a312024-02-28 09:57:39 +080038/*
b.liubb5e7682024-02-28 20:13:04 +080039* MBTK_DEVICE_INFO_ITEM_BASIC - mbtk_device_info_basic_t
40* MBTK_DEVICE_INFO_ITEM_FOTA - mbtk_device_info_fota_t
41* MBTK_DEVICE_INFO_ITEM_MODEM - mbtk_device_info_modem_t
42* MBTK_DEVICE_INFO_ITEM_LOG - mbtk_device_info_log_t
b.liu3a41a312024-02-28 09:57:39 +080043*/
44int mbtk_dev_info_read(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
45{
46 int fd, len, i;
47 mbtk_device_info_header_t info_header;
48 memset(&info_header, 0, sizeof(mbtk_device_info_header_t));
49
50 switch(item_type) {
51 case MBTK_DEVICE_INFO_ITEM_BASIC:
52 {
53 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) {
54 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
55 return -1;
56 }
57 break;
58 }
59 case MBTK_DEVICE_INFO_ITEM_FOTA:
60 {
61 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) {
62 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
63 return -1;
64 }
65 break;
66 }
67 case MBTK_DEVICE_INFO_ITEM_MODEM:
68 {
69 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) {
70 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
71 return -1;
72 }
73 break;
74 }
75 case MBTK_DEVICE_INFO_ITEM_LOG:
76 {
77 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) {
78 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
79 return -1;
80 }
81 break;
82 }
83 default:
84 {
85 LOGE("Item type[%d] error.", item_type);
86 return -1;
87 }
88 }
89
90 mbtk_partition_info_t *partition_info = mbtk_partition_get();
91 if(partition_info == NULL) {
92 LOGE("mbtk_partition_get() fail.");
93 return -1;
94 }
95
96 i = 0;
97 char dev[32] = {0};
98 while(i < MBTK_PARTITION_NUM_MAX) {
99 if(partition_info[i].used && strcmp(partition_info[i].name, MBTK_DEVICE_INFO_PARTITION_NAME) == 0) {
100 snprintf(dev, 32, "/dev/%s", partition_info[i].dev);
b.liubb5e7682024-02-28 20:13:04 +0800101 LOGD("%s -> %s", partition_info[i].name, dev);
b.liu3a41a312024-02-28 09:57:39 +0800102 break;
103 }
104 i++;
105 }
106 if(strlen(dev) == 0) {
107 LOGE("DEV is null.");
108 return -1;
109 }
110
111 fd = open(dev, O_RDONLY);
112 if (fd < 0) {
113 LOGE("Fatal error: can't open device_info %s\n", dev);
114 return -1;
115 }
116
117 len = read(fd, &info_header, sizeof(mbtk_device_info_header_t));
118 if (len != sizeof(mbtk_device_info_header_t)) {
119 LOGE("Fatal error: read %d bytes(expect %d)\n", len, sizeof(mbtk_device_info_header_t));
120 close(fd);
121 goto fail;
122 }
123
124 if(info_header.tag != MBTK_DEVICE_INFO_PARTITION_TAG) {
125 LOGE("TAG error : %08x", info_header.tag);
126 goto fail;
127 }
128
129 if(info_header.version != MBTK_DEVICE_INFO_CURR_VERSION) {
130 LOGE("Version error : %d", info_header.version);
131 goto fail;
132 }
133
134 LOGD("Item count:%d", info_header.item_count);
135
136 if(info_header.item_header[item_type].addr == 0) {
137 LOGE("No found item : %d", item_type);
138 goto fail;
139 }
140
141 lseek(fd, info_header.item_header[item_type].addr, SEEK_SET);
142 if (read(fd, item_ptr, item_size) != item_size) {
143 LOGE("Read fail:%d", errno);
144 goto fail;
145 }
146
147 close(fd);
148 return 0;
149
150fail:
151 close(fd);
152 return -1;
153}
154
b.liuf678f992024-05-08 15:23:10 +0800155int mbtk_dev_info_write(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
156{
157 if(item_ptr == NULL) {
158 LOGE("ARG error.");
159 return -1;
160 }
161
162 switch(item_type) {
163 case MBTK_DEVICE_INFO_ITEM_BASIC:
164 {
165 if(item_size != sizeof(mbtk_device_info_basic_t)) {
166 LOGE("item_size != sizeof(mbtk_device_info_basic_t)\n\r");
167 return -1;
168 }
169 break;
170 }
171 case MBTK_DEVICE_INFO_ITEM_FOTA:
172 {
173 if(item_size != sizeof(mbtk_device_info_fota_t)) {
174 LOGE("item_size != sizeof(mbtk_device_info_fota_t)\n\r");
175 return -1;
176 }
177 break;
178 }
179 case MBTK_DEVICE_INFO_ITEM_MODEM:
180 {
181 if(item_size != sizeof(mbtk_device_info_modem_t)) {
182 LOGE("item_size != sizeof(mbtk_device_info_modem_t)\n\r");
183 return -1;
184 }
185 break;
186 }
187 case MBTK_DEVICE_INFO_ITEM_LOG:
188 {
189 if(item_size != sizeof(mbtk_device_info_log_t)) {
190 LOGE("item_size != sizeof(mbtk_device_info_log_t)\n\r");
191 return -1;
192 }
193 break;
194 }
195 default:
196 {
197 LOGE("Item type[%d] error.\n\r", item_type);
198 return -1;
199 }
200 }
201
202 mbtk_partition_info_t info;
203 memset(&info, 0x0, sizeof(mbtk_partition_info_t));
204 if(mbtk_partition_get_by_name("device_info", &info)) {
205 LOGE("mbtk_partition_get_by_name() fail.");
206 return -1;
207 }
208
209 LOGD("device_info name : %s, dev : %s, addr : %08x, size : %08x, erase_size : %08x", info.name,
210 info.dev, info.partition_start, info.partition_size, info.erase_size);
211
212 if(info.erase_size <= 0 || info.partition_size <= 0) {
213 LOGE("partition info error.");
214 return -1;
215 }
216
217 int fd = open(info.dev, O_RDWR);
218 if (fd < 0) {
219 LOGE("Fatal error: can't open device_info %s\n", info.dev);
220 return -1;
221 }
222
223 char *mtd_buff = (char*)malloc(info.erase_size);
224 if(mtd_buff == NULL) {
225 LOGE("malloc() failed\n");
226 return -1;
227 }
228 memset(mtd_buff, 0xFF, info.erase_size);
229 int len = read(fd, mtd_buff, info.erase_size);
230 if (len != info.erase_size) {
231 LOGE("Fatal error: read %d[%d] bytes(expect %d)\n", len, errno, info.erase_size);
232 goto fail;
233 }
234
235 struct erase_info_user mtdEraseInfo;
236 if (lseek(fd, 0, SEEK_SET) < 0)
237 {
238 LOGE("seek failed\n");
239 return -1;
240 }
241
242 mtdEraseInfo.length = info.partition_size;
243 mtdEraseInfo.start = 0;
244 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
245 ioctl(fd, MEMERASE, &mtdEraseInfo);
246
247 mbtk_device_info_header_t *info_header = (mbtk_device_info_header_t*)mtd_buff;
248 memcpy(mtd_buff + info_header->item_header[item_type].addr, item_ptr, item_size);
249
250 lseek(fd, 0, SEEK_SET);
251 if (write(fd, mtd_buff, info.erase_size) != info.erase_size) {
252 LOGE("Write fail:%d", errno);
253 goto fail;
254 }
255
256 if(mtd_buff) {
257 free(mtd_buff);
258 }
259
260 close(fd);
261 return 0;
262
263fail:
264 close(fd);
265 return -1;
266}
267
268