blob: 7c53f339ed4cbf91bdfa10c296e1f3188cc196f0 [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
45 switch(item_type) {
46 case MBTK_DEVICE_INFO_ITEM_BASIC:
47 {
48 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) {
49 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
50 return -1;
51 }
52 break;
53 }
54 case MBTK_DEVICE_INFO_ITEM_FOTA:
55 {
56 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) {
57 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
58 return -1;
59 }
60 break;
61 }
62 case MBTK_DEVICE_INFO_ITEM_MODEM:
63 {
64 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) {
65 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
66 return -1;
67 }
68 break;
69 }
70 case MBTK_DEVICE_INFO_ITEM_LOG:
71 {
72 if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) {
73 LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
74 return -1;
75 }
76 break;
77 }
78 default:
79 {
80 LOGE("Item type[%d] error.", item_type);
81 return -1;
82 }
83 }
84
85 mbtk_partition_info_t *partition_info = mbtk_partition_get();
86 if(partition_info == NULL) {
87 LOGE("mbtk_partition_get() fail.");
88 return -1;
89 }
90
91 i = 0;
92 char dev[32] = {0};
93 while(i < MBTK_PARTITION_NUM_MAX) {
94 if(partition_info[i].used && strcmp(partition_info[i].name, MBTK_DEVICE_INFO_PARTITION_NAME) == 0) {
95 snprintf(dev, 32, "/dev/%s", partition_info[i].dev);
b.liubb5e7682024-02-28 20:13:04 +080096 LOGD("%s -> %s", partition_info[i].name, dev);
b.liu3a41a312024-02-28 09:57:39 +080097 break;
98 }
99 i++;
100 }
101 if(strlen(dev) == 0) {
102 LOGE("DEV is null.");
103 return -1;
104 }
105
106 fd = open(dev, O_RDONLY);
107 if (fd < 0) {
108 LOGE("Fatal error: can't open device_info %s\n", dev);
109 return -1;
110 }
111
112 len = read(fd, &info_header, sizeof(mbtk_device_info_header_t));
113 if (len != sizeof(mbtk_device_info_header_t)) {
114 LOGE("Fatal error: read %d bytes(expect %d)\n", len, sizeof(mbtk_device_info_header_t));
115 close(fd);
116 goto fail;
117 }
118
119 if(info_header.tag != MBTK_DEVICE_INFO_PARTITION_TAG) {
120 LOGE("TAG error : %08x", info_header.tag);
121 goto fail;
122 }
123
124 if(info_header.version != MBTK_DEVICE_INFO_CURR_VERSION) {
125 LOGE("Version error : %d", info_header.version);
126 goto fail;
127 }
128
129 LOGD("Item count:%d", info_header.item_count);
130
131 if(info_header.item_header[item_type].addr == 0) {
132 LOGE("No found item : %d", item_type);
133 goto fail;
134 }
135
136 lseek(fd, info_header.item_header[item_type].addr, SEEK_SET);
137 if (read(fd, item_ptr, item_size) != item_size) {
138 LOGE("Read fail:%d", errno);
139 goto fail;
140 }
141
142 close(fd);
143 return 0;
144
145fail:
146 close(fd);
147 return -1;
148}
149
b.liuf678f992024-05-08 15:23:10 +0800150int mbtk_dev_info_write(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
151{
152 if(item_ptr == NULL) {
153 LOGE("ARG error.");
154 return -1;
155 }
156
157 switch(item_type) {
158 case MBTK_DEVICE_INFO_ITEM_BASIC:
159 {
160 if(item_size != sizeof(mbtk_device_info_basic_t)) {
161 LOGE("item_size != sizeof(mbtk_device_info_basic_t)\n\r");
162 return -1;
163 }
164 break;
165 }
166 case MBTK_DEVICE_INFO_ITEM_FOTA:
167 {
168 if(item_size != sizeof(mbtk_device_info_fota_t)) {
169 LOGE("item_size != sizeof(mbtk_device_info_fota_t)\n\r");
170 return -1;
171 }
172 break;
173 }
174 case MBTK_DEVICE_INFO_ITEM_MODEM:
175 {
176 if(item_size != sizeof(mbtk_device_info_modem_t)) {
177 LOGE("item_size != sizeof(mbtk_device_info_modem_t)\n\r");
178 return -1;
179 }
180 break;
181 }
182 case MBTK_DEVICE_INFO_ITEM_LOG:
183 {
184 if(item_size != sizeof(mbtk_device_info_log_t)) {
185 LOGE("item_size != sizeof(mbtk_device_info_log_t)\n\r");
186 return -1;
187 }
188 break;
189 }
190 default:
191 {
192 LOGE("Item type[%d] error.\n\r", item_type);
193 return -1;
194 }
195 }
196
197 mbtk_partition_info_t info;
198 memset(&info, 0x0, sizeof(mbtk_partition_info_t));
199 if(mbtk_partition_get_by_name("device_info", &info)) {
200 LOGE("mbtk_partition_get_by_name() fail.");
201 return -1;
202 }
203
204 LOGD("device_info name : %s, dev : %s, addr : %08x, size : %08x, erase_size : %08x", info.name,
205 info.dev, info.partition_start, info.partition_size, info.erase_size);
206
207 if(info.erase_size <= 0 || info.partition_size <= 0) {
208 LOGE("partition info error.");
209 return -1;
210 }
211
212 int fd = open(info.dev, O_RDWR);
213 if (fd < 0) {
214 LOGE("Fatal error: can't open device_info %s\n", info.dev);
215 return -1;
216 }
217
218 char *mtd_buff = (char*)malloc(info.erase_size);
219 if(mtd_buff == NULL) {
220 LOGE("malloc() failed\n");
221 return -1;
222 }
223 memset(mtd_buff, 0xFF, info.erase_size);
224 int len = read(fd, mtd_buff, info.erase_size);
225 if (len != info.erase_size) {
226 LOGE("Fatal error: read %d[%d] bytes(expect %d)\n", len, errno, info.erase_size);
227 goto fail;
228 }
229
230 struct erase_info_user mtdEraseInfo;
231 if (lseek(fd, 0, SEEK_SET) < 0)
232 {
233 LOGE("seek failed\n");
234 return -1;
235 }
236
237 mtdEraseInfo.length = info.partition_size;
238 mtdEraseInfo.start = 0;
239 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
240 ioctl(fd, MEMERASE, &mtdEraseInfo);
241
242 mbtk_device_info_header_t *info_header = (mbtk_device_info_header_t*)mtd_buff;
243 memcpy(mtd_buff + info_header->item_header[item_type].addr, item_ptr, item_size);
244
245 lseek(fd, 0, SEEK_SET);
246 if (write(fd, mtd_buff, info.erase_size) != info.erase_size) {
247 LOGE("Write fail:%d", errno);
248 goto fail;
249 }
250
251 if(mtd_buff) {
252 free(mtd_buff);
253 }
254
255 close(fd);
256 return 0;
257
258fail:
259 close(fd);
260 return -1;
261}
262
b.liu762260b2025-02-18 18:21:55 +0800263int mbtk_dev_info_revision_get(char *revision_out, char *revision_in)
264{
265 mbtk_device_info_basic_t info_basic;
266 memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
267 int result = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_BASIC, &info_basic, sizeof(mbtk_device_info_basic_t));
268 if(result) {
269 LOGE("mbtk_dev_info_read(BASIC) fail.");
270 return -1;
271 }
b.liuf678f992024-05-08 15:23:10 +0800272
b.liu762260b2025-02-18 18:21:55 +0800273 if(strlen((char*)info_basic.revision_out) > 0) {
274 memcpy(revision_out, info_basic.revision_out, strlen((char*)info_basic.revision_out));
275 }
276 if(strlen((char*)info_basic.revision_in) > 0) {
277 memcpy(revision_in, info_basic.revision_in, strlen((char*)info_basic.revision_in));
278 }
279
280 return 0;
281}