blob: 392429781a92e4ac47e4b683b6aaf5506191d3df [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#include "mbtk_device.h"
b.liu472cfaf2024-12-19 19:08:19 +080032
b.liu3a41a312024-02-28 09:57:39 +080033/*
b.liubb5e7682024-02-28 20:13:04 +080034* MBTK_DEVICE_INFO_ITEM_BASIC - mbtk_device_info_basic_t
35* MBTK_DEVICE_INFO_ITEM_FOTA - mbtk_device_info_fota_t
36* MBTK_DEVICE_INFO_ITEM_MODEM - mbtk_device_info_modem_t
37* MBTK_DEVICE_INFO_ITEM_LOG - mbtk_device_info_log_t
b.liu3a41a312024-02-28 09:57:39 +080038*/
39int mbtk_dev_info_read(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
40{
41 int fd, len, i;
42 mbtk_device_info_header_t info_header;
43 memset(&info_header, 0, sizeof(mbtk_device_info_header_t));
44
b.liu3a41a312024-02-28 09:57:39 +080045 mbtk_partition_info_t *partition_info = mbtk_partition_get();
46 if(partition_info == NULL) {
47 LOGE("mbtk_partition_get() fail.");
48 return -1;
49 }
50
51 i = 0;
52 char dev[32] = {0};
53 while(i < MBTK_PARTITION_NUM_MAX) {
54 if(partition_info[i].used && strcmp(partition_info[i].name, MBTK_DEVICE_INFO_PARTITION_NAME) == 0) {
55 snprintf(dev, 32, "/dev/%s", partition_info[i].dev);
b.liubb5e7682024-02-28 20:13:04 +080056 LOGD("%s -> %s", partition_info[i].name, dev);
b.liu3a41a312024-02-28 09:57:39 +080057 break;
58 }
59 i++;
60 }
61 if(strlen(dev) == 0) {
62 LOGE("DEV is null.");
63 return -1;
64 }
65
66 fd = open(dev, O_RDONLY);
67 if (fd < 0) {
68 LOGE("Fatal error: can't open device_info %s\n", dev);
69 return -1;
70 }
71
72 len = read(fd, &info_header, sizeof(mbtk_device_info_header_t));
73 if (len != sizeof(mbtk_device_info_header_t)) {
74 LOGE("Fatal error: read %d bytes(expect %d)\n", len, sizeof(mbtk_device_info_header_t));
75 close(fd);
76 goto fail;
77 }
78
79 if(info_header.tag != MBTK_DEVICE_INFO_PARTITION_TAG) {
80 LOGE("TAG error : %08x", info_header.tag);
81 goto fail;
82 }
83
b.liub7530d22025-06-16 19:49:05 +080084 LOGD("Dev info version : %d, Item count:%d", info_header.version, info_header.item_count);
b.liu3a41a312024-02-28 09:57:39 +080085
86 if(info_header.item_header[item_type].addr == 0) {
87 LOGE("No found item : %d", item_type);
88 goto fail;
89 }
90
91 lseek(fd, info_header.item_header[item_type].addr, SEEK_SET);
b.liub7530d22025-06-16 19:49:05 +080092
93
94 switch(item_type) {
95 case MBTK_DEVICE_INFO_ITEM_BASIC:
96 {
97 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) {
98 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
99 goto fail;
100 }
101
102 mbtk_device_info_basic_t *basic_ptr = (mbtk_device_info_basic_t*)item_ptr;
103 basic_ptr->version = (mbtk_device_info_version_enum)info_header.version;
104 if(basic_ptr->version == DEV_INFO_VERSION_V1) {
105 if (read(fd, &(basic_ptr->basic.v1), sizeof(mbtk_device_info_basic_v1_t)) !=
106 sizeof(mbtk_device_info_basic_v1_t)) {
107 LOGE("Read fail:%d", errno);
108 goto fail;
109 }
110 } else {
111 if (read(fd, &(basic_ptr->basic.v2), sizeof(mbtk_device_info_basic_v2_t)) !=
112 sizeof(mbtk_device_info_basic_v2_t)) {
113 LOGE("Read fail:%d", errno);
114 goto fail;
115 }
116 }
117 break;
118 }
119 case MBTK_DEVICE_INFO_ITEM_FOTA:
120 {
121 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) {
122 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
123 goto fail;
124 }
125
126 mbtk_device_info_fota_t *fota_ptr = (mbtk_device_info_fota_t*)item_ptr;
127 fota_ptr->version = (mbtk_device_info_version_enum)info_header.version;
128 if(fota_ptr->version == DEV_INFO_VERSION_V1) {
129 if (read(fd, &(fota_ptr->fota.v1), sizeof(mbtk_device_info_fota_v1_t)) !=
130 sizeof(mbtk_device_info_fota_v1_t)) {
131 LOGE("Read fail:%d", errno);
132 goto fail;
133 }
134 } else {
135 if (read(fd, &(fota_ptr->fota.v2), sizeof(mbtk_device_info_fota_v2_t)) !=
136 sizeof(mbtk_device_info_fota_v2_t)) {
137 LOGE("Read fail:%d", errno);
138 goto fail;
139 }
140 }
141 break;
142 }
143 case MBTK_DEVICE_INFO_ITEM_MODEM:
144 {
145 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) {
146 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
147 goto fail;
148 }
149
150 mbtk_device_info_modem_t *modem_ptr = (mbtk_device_info_modem_t*)item_ptr;
151 modem_ptr->version = (mbtk_device_info_version_enum)info_header.version;
152 if(modem_ptr->version == DEV_INFO_VERSION_V1) {
153 if (read(fd, &(modem_ptr->modem.v1), sizeof(mbtk_device_info_modem_v1_t)) !=
154 sizeof(mbtk_device_info_modem_v1_t)) {
155 LOGE("Read fail:%d", errno);
156 goto fail;
157 }
158 } else {
159 if (read(fd, &(modem_ptr->modem.v2), sizeof(mbtk_device_info_modem_v2_t)) !=
160 sizeof(mbtk_device_info_modem_v2_t)) {
161 LOGE("Read fail:%d", errno);
162 goto fail;
163 }
164 }
165 break;
166 }
167 case MBTK_DEVICE_INFO_ITEM_LOG:
168 {
169 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) {
170 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
171 goto fail;
172 }
173
174 mbtk_device_info_log_t *log_ptr = (mbtk_device_info_log_t*)item_ptr;
175 log_ptr->version = (mbtk_device_info_version_enum)info_header.version;
176 if(log_ptr->version == DEV_INFO_VERSION_V1) {
177 if (read(fd, &(log_ptr->log.v1), sizeof(mbtk_device_info_log_v1_t)) !=
178 sizeof(mbtk_device_info_log_v1_t)) {
179 LOGE("Read fail:%d", errno);
180 goto fail;
181 }
182 } else {
183 if (read(fd, &(log_ptr->log.v2), sizeof(mbtk_device_info_log_v2_t)) !=
184 sizeof(mbtk_device_info_log_v2_t)) {
185 LOGE("Read fail:%d", errno);
186 goto fail;
187 }
188 }
189 break;
190 }
191 default:
192 {
193 LOGE("Item type[%d] error.", item_type);
194 goto fail;
195 }
b.liu3a41a312024-02-28 09:57:39 +0800196 }
197
198 close(fd);
199 return 0;
200
201fail:
202 close(fd);
203 return -1;
204}
205
b.liuf678f992024-05-08 15:23:10 +0800206int mbtk_dev_info_write(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
207{
208 if(item_ptr == NULL) {
209 LOGE("ARG error.");
210 return -1;
211 }
212
b.liuf678f992024-05-08 15:23:10 +0800213 mbtk_partition_info_t info;
214 memset(&info, 0x0, sizeof(mbtk_partition_info_t));
b.liub7530d22025-06-16 19:49:05 +0800215 if(mbtk_partition_get_by_name(MBTK_DEVICE_INFO_PARTITION_NAME, &info)) {
b.liuf678f992024-05-08 15:23:10 +0800216 LOGE("mbtk_partition_get_by_name() fail.");
217 return -1;
218 }
219
220 LOGD("device_info name : %s, dev : %s, addr : %08x, size : %08x, erase_size : %08x", info.name,
221 info.dev, info.partition_start, info.partition_size, info.erase_size);
222
223 if(info.erase_size <= 0 || info.partition_size <= 0) {
224 LOGE("partition info error.");
225 return -1;
226 }
227
228 int fd = open(info.dev, O_RDWR);
229 if (fd < 0) {
230 LOGE("Fatal error: can't open device_info %s\n", info.dev);
231 return -1;
232 }
233
234 char *mtd_buff = (char*)malloc(info.erase_size);
235 if(mtd_buff == NULL) {
236 LOGE("malloc() failed\n");
b.liub7530d22025-06-16 19:49:05 +0800237 return -1;
b.liuf678f992024-05-08 15:23:10 +0800238 }
239 memset(mtd_buff, 0xFF, info.erase_size);
240 int len = read(fd, mtd_buff, info.erase_size);
241 if (len != info.erase_size) {
242 LOGE("Fatal error: read %d[%d] bytes(expect %d)\n", len, errno, info.erase_size);
243 goto fail;
244 }
245
246 struct erase_info_user mtdEraseInfo;
247 if (lseek(fd, 0, SEEK_SET) < 0)
248 {
249 LOGE("seek failed\n");
250 return -1;
251 }
252
253 mtdEraseInfo.length = info.partition_size;
254 mtdEraseInfo.start = 0;
255 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
256 ioctl(fd, MEMERASE, &mtdEraseInfo);
257
258 mbtk_device_info_header_t *info_header = (mbtk_device_info_header_t*)mtd_buff;
b.liub7530d22025-06-16 19:49:05 +0800259 switch(item_type) {
260 case MBTK_DEVICE_INFO_ITEM_BASIC:
261 {
262 if(item_size != sizeof(mbtk_device_info_basic_t)) {
263 LOGE("item_size != sizeof(mbtk_device_info_basic_t)\n\r");
264 goto fail;
265 }
266
267 mbtk_device_info_basic_t *basic_ptr = (mbtk_device_info_basic_t*)item_ptr;
268 if(info_header->version != basic_ptr->version) {
269 LOGE("basic version error.");
270 goto fail;
271 }
272
273 if(basic_ptr->version == DEV_INFO_VERSION_V1) {
274 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(basic_ptr->basic.v1),
275 sizeof(mbtk_device_info_basic_v1_t));
276 } else {
277 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(basic_ptr->basic.v2),
278 sizeof(mbtk_device_info_basic_v2_t));
279 }
280 break;
281 }
282 case MBTK_DEVICE_INFO_ITEM_FOTA:
283 {
284 if(item_size != sizeof(mbtk_device_info_fota_t)) {
285 LOGE("item_size != sizeof(mbtk_device_info_fota_t)\n\r");
286 goto fail;
287 }
288
289 mbtk_device_info_fota_t *fota_ptr = (mbtk_device_info_fota_t*)item_ptr;
290 if(info_header->version != fota_ptr->version) {
291 LOGE("fota version error.");
292 goto fail;
293 }
294
295 if(fota_ptr->version == DEV_INFO_VERSION_V1) {
296 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(fota_ptr->fota.v1),
297 sizeof(mbtk_device_info_fota_v1_t));
298 } else {
299 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(fota_ptr->fota.v2),
300 sizeof(mbtk_device_info_fota_v2_t));
301 }
302 break;
303 }
304 case MBTK_DEVICE_INFO_ITEM_MODEM:
305 {
306 if(item_size != sizeof(mbtk_device_info_modem_t)) {
307 LOGE("item_size != sizeof(mbtk_device_info_modem_t)\n\r");
308 goto fail;
309 }
310
311 mbtk_device_info_modem_t *modem_ptr = (mbtk_device_info_modem_t*)item_ptr;
312 if(info_header->version != modem_ptr->version) {
313 LOGE("modem version error.");
314 goto fail;
315 }
316
317 if(modem_ptr->version == DEV_INFO_VERSION_V1) {
318 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(modem_ptr->modem.v1),
319 sizeof(mbtk_device_info_modem_v1_t));
320 } else {
321 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(modem_ptr->modem.v2),
322 sizeof(mbtk_device_info_modem_v2_t));
323 }
324 break;
325 }
326 case MBTK_DEVICE_INFO_ITEM_LOG:
327 {
328 if(item_size != sizeof(mbtk_device_info_log_t)) {
329 LOGE("item_size != sizeof(mbtk_device_info_log_t)\n\r");
330 goto fail;
331 }
332
333 mbtk_device_info_log_t *log_ptr = (mbtk_device_info_log_t*)item_ptr;
334 if(info_header->version != log_ptr->version) {
335 LOGE("log version error.");
336 goto fail;
337 }
338
339 if(log_ptr->version == DEV_INFO_VERSION_V1) {
340 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(log_ptr->log.v1),
341 sizeof(mbtk_device_info_log_v1_t));
342 } else {
343 memcpy(mtd_buff + info_header->item_header[item_type].addr, &(log_ptr->log.v2),
344 sizeof(mbtk_device_info_log_v2_t));
345 }
346 break;
347 }
348 default:
349 {
350 LOGE("Item type[%d] error.\n\r", item_type);
351 goto fail;
352 }
353 }
b.liuf678f992024-05-08 15:23:10 +0800354
355 lseek(fd, 0, SEEK_SET);
356 if (write(fd, mtd_buff, info.erase_size) != info.erase_size) {
357 LOGE("Write fail:%d", errno);
358 goto fail;
359 }
360
361 if(mtd_buff) {
362 free(mtd_buff);
363 }
364
365 close(fd);
366 return 0;
367
368fail:
369 close(fd);
370 return -1;
371}
372
b.liu6339dca2025-02-20 17:47:23 +0800373int mbtk_dev_info_revision_get(char *revision_out, char *revision_in, char *project_cust, char *band_area,
374 char* build_time)
b.liu762260b2025-02-18 18:21:55 +0800375{
376 mbtk_device_info_basic_t info_basic;
377 memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
378 int result = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_BASIC, &info_basic, sizeof(mbtk_device_info_basic_t));
379 if(result) {
380 LOGE("mbtk_dev_info_read(BASIC) fail.");
381 return -1;
382 }
b.liuf678f992024-05-08 15:23:10 +0800383
b.liu6339dca2025-02-20 17:47:23 +0800384 mbtk_device_info_modem_t info_modem;
385 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
386 result = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_MODEM, &info_modem, sizeof(mbtk_device_info_modem_t));
387 if(result) {
388 LOGE("mbtk_dev_info_read(MODEM) fail.");
389 return -1;
390 }
391
b.liub7530d22025-06-16 19:49:05 +0800392 mbtk_modem_band_area_enum modem_band_area = MBTK_MODEM_BAND_AREA_ALL;
393 if(info_basic.version == DEV_INFO_VERSION_V1) {
394 if(strlen((char*)info_basic.basic.v1.revision_out) > 0) {
395 memcpy(revision_out, info_basic.basic.v1.revision_out, strlen((char*)info_basic.basic.v1.revision_out));
396 }
397 if(strlen((char*)info_basic.basic.v1.revision_in) > 0) {
398 memcpy(revision_in, info_basic.basic.v1.revision_in, strlen((char*)info_basic.basic.v1.revision_in));
399 }
400 if(strlen((char*)info_basic.basic.v1.project_cust) > 0) {
401 memcpy(project_cust, info_basic.basic.v1.project_cust, strlen((char*)info_basic.basic.v1.project_cust));
402 }
403 if(strlen((char*)info_basic.basic.v1.build_time) > 0) {
404 memcpy(build_time, info_basic.basic.v1.build_time, strlen((char*)info_basic.basic.v1.build_time));
405 }
406
407 modem_band_area = info_modem.modem.v1.band_area;
408 } else {
409 if(strlen((char*)info_basic.basic.v2.revision_out) > 0) {
410 memcpy(revision_out, info_basic.basic.v2.revision_out, strlen((char*)info_basic.basic.v2.revision_out));
411 }
412 if(strlen((char*)info_basic.basic.v2.revision_in) > 0) {
413 memcpy(revision_in, info_basic.basic.v2.revision_in, strlen((char*)info_basic.basic.v2.revision_in));
414 }
415 if(strlen((char*)info_basic.basic.v2.project_cust) > 0) {
416 memcpy(project_cust, info_basic.basic.v2.project_cust, strlen((char*)info_basic.basic.v2.project_cust));
417 }
418 if(strlen((char*)info_basic.basic.v2.build_time) > 0) {
419 memcpy(build_time, info_basic.basic.v2.build_time, strlen((char*)info_basic.basic.v2.build_time));
420 }
421
422 modem_band_area = info_modem.modem.v2.band_area;
b.liu6339dca2025-02-20 17:47:23 +0800423 }
424
b.liub7530d22025-06-16 19:49:05 +0800425 switch(modem_band_area) {
b.liu6339dca2025-02-20 17:47:23 +0800426 case MBTK_MODEM_BAND_AREA_CN:
427 memcpy(band_area, "CN", 2);
428 break;
429 case MBTK_MODEM_BAND_AREA_EU:
430 memcpy(band_area, "EU", 2);
431 break;
432 case MBTK_MODEM_BAND_AREA_SA:
433 memcpy(band_area, "SA", 2);
434 break;
435 default:
436 memcpy(band_area, "DEF", 3);
437 break;
438 }
b.liu762260b2025-02-18 18:21:55 +0800439
440 return 0;
441}