blob: 4aa87dc32655e80ab4b490583c3e2addae0fc64b [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.liu3a41a312024-02-28 09:57:39 +080023
24#include "mbtk_device.h"
25#include "mbtk_log.h"
26#include "mbtk_str.h"
27#include "mbtk_mtd.h"
28
29/*
b.liubb5e7682024-02-28 20:13:04 +080030* MBTK_DEVICE_INFO_ITEM_BASIC - mbtk_device_info_basic_t
31* MBTK_DEVICE_INFO_ITEM_FOTA - mbtk_device_info_fota_t
32* MBTK_DEVICE_INFO_ITEM_MODEM - mbtk_device_info_modem_t
33* MBTK_DEVICE_INFO_ITEM_LOG - mbtk_device_info_log_t
b.liu3a41a312024-02-28 09:57:39 +080034*/
35int mbtk_dev_info_read(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
36{
37 int fd, len, i;
38 mbtk_device_info_header_t info_header;
39 memset(&info_header, 0, sizeof(mbtk_device_info_header_t));
40
41 switch(item_type) {
42 case MBTK_DEVICE_INFO_ITEM_BASIC:
43 {
44 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) {
45 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
46 return -1;
47 }
48 break;
49 }
50 case MBTK_DEVICE_INFO_ITEM_FOTA:
51 {
52 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) {
53 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
54 return -1;
55 }
56 break;
57 }
58 case MBTK_DEVICE_INFO_ITEM_MODEM:
59 {
60 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) {
61 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
62 return -1;
63 }
64 break;
65 }
66 case MBTK_DEVICE_INFO_ITEM_LOG:
67 {
68 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) {
69 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
70 return -1;
71 }
72 break;
73 }
74 default:
75 {
76 LOGE("Item type[%d] error.", item_type);
77 return -1;
78 }
79 }
80
81 mbtk_partition_info_t *partition_info = mbtk_partition_get();
82 if(partition_info == NULL) {
83 LOGE("mbtk_partition_get() fail.");
84 return -1;
85 }
86
87 i = 0;
88 char dev[32] = {0};
89 while(i < MBTK_PARTITION_NUM_MAX) {
90 if(partition_info[i].used && strcmp(partition_info[i].name, MBTK_DEVICE_INFO_PARTITION_NAME) == 0) {
91 snprintf(dev, 32, "/dev/%s", partition_info[i].dev);
b.liubb5e7682024-02-28 20:13:04 +080092 LOGD("%s -> %s", partition_info[i].name, dev);
b.liu3a41a312024-02-28 09:57:39 +080093 break;
94 }
95 i++;
96 }
97 if(strlen(dev) == 0) {
98 LOGE("DEV is null.");
99 return -1;
100 }
101
102 fd = open(dev, O_RDONLY);
103 if (fd < 0) {
104 LOGE("Fatal error: can't open device_info %s\n", dev);
105 return -1;
106 }
107
108 len = read(fd, &info_header, sizeof(mbtk_device_info_header_t));
109 if (len != sizeof(mbtk_device_info_header_t)) {
110 LOGE("Fatal error: read %d bytes(expect %d)\n", len, sizeof(mbtk_device_info_header_t));
111 close(fd);
112 goto fail;
113 }
114
115 if(info_header.tag != MBTK_DEVICE_INFO_PARTITION_TAG) {
116 LOGE("TAG error : %08x", info_header.tag);
117 goto fail;
118 }
119
120 if(info_header.version != MBTK_DEVICE_INFO_CURR_VERSION) {
121 LOGE("Version error : %d", info_header.version);
122 goto fail;
123 }
124
125 LOGD("Item count:%d", info_header.item_count);
126
127 if(info_header.item_header[item_type].addr == 0) {
128 LOGE("No found item : %d", item_type);
129 goto fail;
130 }
131
132 lseek(fd, info_header.item_header[item_type].addr, SEEK_SET);
133 if (read(fd, item_ptr, item_size) != item_size) {
134 LOGE("Read fail:%d", errno);
135 goto fail;
136 }
137
138 close(fd);
139 return 0;
140
141fail:
142 close(fd);
143 return -1;
144}
145