blob: ca52fc2a00ccb8ccba8b1330eaadfebca7b42e64 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/**
2 * @file flags_api.c
3 * @brief flags·ÖÇø½Ó¿ÚʵÏÖ
4 *
5 * Copyright (C) 2023 Sanechips Technology Co., Ltd.
6 * @author
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. £¨±ØÑ¡£ºGPLv2 Licence£©
11 *
12 */
13
14
15/*******************************************************************************
16 * Include header files *
17 ******************************************************************************/
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <string.h>
22#include <errno.h>
23#include <sys/ioctl.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <mtd/mtd-abi.h>
27
28#include "pub_flags.h"
29
30#include "flags_log.h"
31#include "flags_api.h"
32
33
34/*******************************************************************************
35 * Macro definitions *
36 ******************************************************************************/
37#define MAX_PATH_LEN (256)
38
39#define PARTITION_NAME_FLAGS "flags"
40
41#define FLAGS_INIT_VALID_BLOCKS_NUM (8)
42
43#define GOOD_BLOCK (0)
44#define BAD_BLOCK (1)
45
46#define FILE_PATH_PROC_CMDLINE "/proc/cmdline"
47
48#define PROC_CMDLINE_SYSTEM_A_FLAG "system=system_a"
49#define PROC_CMDLINE_SYSTEM_B_FLAG "system=system_b"
50
51#define SYSTEM_INDEX_UNKNOWN (-1)
52#define SYSTEM_INDEX_1 (1)
53#define SYSTEM_INDEX_2 (2)
54
xf.lice873192023-11-08 17:10:35 -080055#define CRC_LE_BITS 64
56#define CRC32_POLY_LE 0xedb88320
57#define LE_TABLE_ROWS (CRC_LE_BITS/8)
58#define LE_TABLE_SIZE 256
59
xf.li6c8fc1e2023-08-12 00:11:09 -070060
61/*******************************************************************************
62 * Type definitions *
63 ******************************************************************************/
64typedef struct
65{
66 unsigned int mtd_totalsize; // mtd device total size
67 unsigned int mtd_pageperblock; // mtd device page per block
68 unsigned int mtd_blocksize; // mtd device block size
69 unsigned int mtd_pagesize; // mtd device page size
70 unsigned int mtd_oobsize; // mtd device oob size
71 int parti_file_desc; // partition update file description
72} partition_mtd_info_t;
73
74
75typedef enum
76{
77 DEVICE_MTD = 0,
78 DEVICE_ZFTL = 1,
79 DEVICE_MTD_BLOCK,
80} device_type_t;
81
82
83/*******************************************************************************
84 * Local variable definitions *
85 ******************************************************************************/
86
87
88/*******************************************************************************
89 * Global variable definitions *
90 ******************************************************************************/
xf.lice873192023-11-08 17:10:35 -080091static unsigned int crc32table_le[LE_TABLE_ROWS][256];
xf.li6c8fc1e2023-08-12 00:11:09 -070092
93
94/*******************************************************************************
95 * Local function declarations *
96 ******************************************************************************/
xf.lice873192023-11-08 17:10:35 -080097static void crc32init_le_generic(const unsigned int polynomial, unsigned int (*tab)[256]);
98static unsigned int crc32_body(unsigned int crc, unsigned char const *buf, size_t len, const unsigned int (*tab)[256]);
99static unsigned int crc32_le_generic(unsigned int crc, unsigned char const *p, size_t len, const unsigned int (*tab)[256]);
100
xf.li6c8fc1e2023-08-12 00:11:09 -0700101static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len);
102static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len);
103
104static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index);
105static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index);
106
107static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src);
108
109static int get_current_system();
110
111
112/*******************************************************************************
113 * Local function implementations *
114 ******************************************************************************/
xf.lice873192023-11-08 17:10:35 -0800115static void crc32init_le_generic(const unsigned int polynomial, unsigned int (*tab)[256])
116{
117 unsigned i, j;
118 unsigned int crc = 1;
119
120 tab[0][0] = 0;
121
122 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
123 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
124 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
125 tab[0][i + j] = crc ^ tab[0][j];
126 }
127 for (i = 0; i < LE_TABLE_SIZE; i++) {
128 crc = tab[0][i];
129 for (j = 1; j < LE_TABLE_ROWS; j++) {
130 crc = tab[0][crc & 0xff] ^ (crc >> 8);
131 tab[j][i] = crc;
132 }
133 }
134}
135
136
137static unsigned int crc32_body(unsigned int crc, unsigned char const *buf, size_t len, const unsigned int (*tab)[256])
138{
139#define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
140#define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
141 t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
142#define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
143 t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
144
145 const unsigned int *b;
146 size_t rem_len;
147
148 const unsigned int *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
149 const unsigned int *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
150 unsigned int q;
151
152 if ((long)buf & 3 && len)
153 {
154 do
155 {
156 DO_CRC(*buf++);
157 } while ((--len) && ((long)buf)&3);
158 }
159
160 rem_len = len & 7;
161 len = len >> 3;
162 b = (const unsigned int *)buf;
163
164 for (--b; len; --len)
165 {
166 q = crc ^ *++b;
167 crc = DO_CRC8;
168 q = *++b;
169 crc ^= DO_CRC4;
170 }
171
172 len = rem_len;
173 if (len)
174 {
175 unsigned char *p = (unsigned char *)(b + 1) - 1;
176 do
177 {
178 DO_CRC(*++p); /* use pre increment for speed */
179 } while (--len);
180 }
181
182 return crc;
183#undef DO_CRC
184#undef DO_CRC4
185#undef DO_CRC8
186}
187
188
189static unsigned int crc32_le_generic(unsigned int crc, unsigned char const *p, size_t len, const unsigned int (*tab)[256])
190{
191 crc = crc32_body(crc, p, len, tab);
192 return crc;
193}
194
195
xf.li6c8fc1e2023-08-12 00:11:09 -0700196static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len)
197{
198 FILE *fp_mtd = 0;
199 char buf[128];
200 char *line_str;
201
202 if (!o_mtd_path_len)
203 {
204 return -1;
205 }
206
207 fp_mtd = fopen("/proc/mtd", "r+");
208 if (NULL == fp_mtd)
209 {
210 flags_err("open file error: %s", strerror(errno));
211 return -1;
212 }
213 // printf("[libmtd]: partition name:%s\n", i_parti_name);
214
215 while (1)
216 {
217 int matches = 0;
218 char mtdname[64] = {0};
219 int mtdnum = 0;
220 unsigned int mtdsize, mtderasesize;
221 memset(buf, 0, sizeof(buf));
222 line_str = fgets(buf, sizeof(buf) - 1, fp_mtd);
223
224 if (NULL == line_str)
225 {
226 flags_err("get info from mtd error: %s", strerror(errno));
227 fclose(fp_mtd);
228 return -1;
229 }
230 // mtd5: 00100000 00020000 "fotaflag"
231 matches = sscanf(buf, "mtd%d: %x %x \"%63[^\"]",
232 &mtdnum, &mtdsize, &mtderasesize, mtdname);
233 mtdname[63] = '\0';
234
235 if ((matches == 4) && (strcmp(mtdname, i_parti_name) == 0))
236 {
237 memset(o_mtd_path, 0, o_mtd_path_len);
238 if (device_type == DEVICE_MTD_BLOCK)
239 {
240 snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtdblock%d", mtdnum);
241 }
242 else if (device_type == DEVICE_MTD)
243 {
244 snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtd%d", mtdnum);
245 }
246 else if (device_type == DEVICE_ZFTL)
247 {
248 snprintf(o_mtd_path, o_mtd_path_len, "/dev/zftl%d", mtdnum);
249 }
250 else
251 {
252 flags_err("unknown device type %d", device_type);
253 fclose(fp_mtd);
254 return -1;
255 }
256 // printf("[libmtd]: o_mtd_path=[%s]\n", o_mtd_path);
257 break;
258 }
259 }
260 fclose(fp_mtd);
261 return 0;
262}
263
264
265static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len)
266{
267 struct erase_info_user erase_info;
268 int write_len = 0;
269 long long offs = 0;
270
271 erase_info.start = index * p_mtd_info->mtd_blocksize;
272 erase_info.length = p_mtd_info->mtd_blocksize;
273
274 offs = (long long)index * p_mtd_info->mtd_blocksize;
275
276 if (ioctl(p_mtd_info->parti_file_desc, WRITEENABLE, 0) != 0)
277 {
278 flags_err("failed to enable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
279
280 return -1;
281 }
282
283 if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
284 {
285 flags_err("ioctl [MEMGETBADBLOCK] block: %d, change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
286
287 return BAD_BLOCK;
288 }
289
290 if (ioctl(p_mtd_info->parti_file_desc, MEMERASE, &erase_info) < 0)
291 {
292 flags_err("ioctl [MEMERASE] block: %d fail, errno=%d, strerror=[%s]", index, errno, strerror(errno));
293
294 return -1;
295 }
296
297 if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
298 {
299 flags_err("ioctl [MEMGETBADBLOCK] block:%d , change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
300
301 return BAD_BLOCK;
302 }
303
304 if (lseek(p_mtd_info->parti_file_desc, index * p_mtd_info->mtd_blocksize, SEEK_SET) < 0)
305 {
306 flags_err("lseek fail, errno=%d, strerror=[%s]", errno, strerror(errno));
307
308 return -1;
309 }
310
311 write_len = write(p_mtd_info->parti_file_desc, content, p_mtd_info->mtd_blocksize);
312
313 if (write_len != p_mtd_info->mtd_blocksize)
314 {
315 flags_err("write flash fail, errno=%d, strerror=[%s]", errno, strerror(errno));
316
317 return -1;
318 }
319
320 if (ioctl(p_mtd_info->parti_file_desc, WRITEDISABLE, 0) != 0)
321 {
322 flags_err("failed to disable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
323 }
324
325 return 0;
326}
327
328
329static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index)
330{
331 int ret = -1;
332 int fd_dst = -1;
333 char mtd_path[MAX_PATH_LEN] = {0};
334 struct mtd_info_user meminfo = {0};
335 partition_mtd_info_t mtd_info = {0};
336 int index = 0;
337 int block_num = 0;
338 int good_index = 0;
339 int read_len = 0;
340
341 long long offs = 0;
342
343 int block_flag = GOOD_BLOCK;
344
345 ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
346 if (ret < 0)
347 {
348 flags_err("partition [%s] not find", PARTITION_NAME_FLAGS);
349 goto error;
350 }
351
352 if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
353 {
354 flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
355 goto error;
356 }
357
358 mtd_info.parti_file_desc = fd_dst;
359
360 if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
361 {
362 flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
363 goto error_close;
364 }
365
366 mtd_info.mtd_blocksize = meminfo.erasesize;
367 mtd_info.mtd_oobsize = meminfo.oobsize;
368 mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
369 mtd_info.mtd_pagesize = meminfo.writesize;
370 mtd_info.mtd_totalsize = meminfo.size;
371
372 block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
373
374 for (index = 0; (good_index < 2 && index < block_num); index++)
375 {
376
377 offs = index * mtd_info.mtd_blocksize;
378 if (ioctl(mtd_info.parti_file_desc, MEMGETBADBLOCK, &offs) == 0)
379 {
380 block_flag = GOOD_BLOCK;
381 }
382 else
383 {
384 flags_err("flags block [%d] is bad, errno=%d, strerror=[%s]", index, errno, strerror(errno));
385 block_flag = BAD_BLOCK;
386 }
387
388 if (block_flag == GOOD_BLOCK)
389 {
390 if (lseek(mtd_info.parti_file_desc, index * mtd_info.mtd_blocksize, SEEK_SET) < 0)
391 {
392 flags_err("lseek error, errno=%d, strerror=[%s]", errno, strerror(errno));
393 goto error_close;
394 }
395
396 if (good_index == 0)
397 {
398 read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_main, sizeof(T_FLAGS_INFO));
399 *p_main_index = index;
400 }
401 else if (good_index == 1)
402 {
403 read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_backup, sizeof(T_FLAGS_INFO));
404 *p_backup_index = index;
405 }
406 else
407 {
408 break;
409 }
410
411 if (read_len < sizeof(T_FLAGS_INFO))
412 {
413 flags_err("read len (%d) < need len (%d)", read_len, sizeof(T_FLAGS_INFO));
414 goto error_close;
415 }
416
417 good_index++;
418 }
419
420 }
421
422 close(fd_dst);
423
424 return 0;
425
426error_close:
427 close(fd_dst);
428
429error:
430 return -1;
431}
432
433
434static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index)
435{
436 int ret = -1;
437 int fd_dst = -1;
438 char mtd_path[MAX_PATH_LEN] = {0};
439 struct mtd_info_user meminfo = {0};
440 partition_mtd_info_t mtd_info = {0};
441
442 unsigned char *real_write_content = NULL;
443
444 int index = 0;
445 int block_num = 0;
446 int main_index = *p_main_index;
447 int back_index = *p_backup_index;
448
449 ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
450 if (ret < 0)
451 {
452 flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
453
454 return -1;
455 }
456
457 if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
458 {
459 flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
460 return -1;
461 }
462
463 if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
464 {
465 flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
466 goto error;
467 }
468
469 mtd_info.parti_file_desc = fd_dst;
470 mtd_info.mtd_blocksize = meminfo.erasesize;
471 mtd_info.mtd_oobsize = meminfo.oobsize;
472 mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
473 mtd_info.mtd_pagesize = meminfo.writesize;
474 mtd_info.mtd_totalsize = meminfo.size;
475
476 block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
477
478 real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
479 if (NULL == real_write_content)
480 {
481 flags_err("malloc block fail");
482 goto error;
483 }
484
485 memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
486 memcpy(real_write_content, (char *)p_flags_info, sizeof(T_FLAGS_INFO));
487
488 flags_log("begin to write main flags");
489
490 for (index = 0; index < block_num; index++)
491 {
492 if (index == back_index)
493 {
494 continue;
495 }
496
497 ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
498 if (ret == 0)
499 {
500 // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
501 flags_log("main flags location: [%d]->[%d]", main_index, index);
502 main_index = index;
503 break;
504 }
505 else if (ret == BAD_BLOCK)
506 {
507 // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
508 flags_log("flags block index [%d] is bad", index);
509 continue;
510 }
511 else
512 {
513 flags_err("write main flags fail");
514 main_index = -1;
515 break;
516 }
517 }
518
519 flags_log("begin to write backup flags");
520
521 for (index = 0; index < block_num; index++)
522 {
523 if (index == main_index)
524 {
525 continue;
526 }
527
528 flags_log("write backup to [%d] block", index);
529 ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
530 if (ret == 0)
531 {
532 // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
533 flags_log("backup flags location: [%d]->[%d]", back_index, index);
534 back_index = index;
535 break;
536 }
537 else if (ret == BAD_BLOCK)
538 {
539 // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
540 continue;
541 }
542 else
543 {
544 flags_err("write backup flags fail");
545 back_index = -1;
546 break;
547 }
548 }
549
550 if (main_index == -1 && back_index == -1)
551 {
552 goto error;
553 }
554 else
555 {
556 ret = 0;
557 goto end;
558 }
559
560error:
561 ret = -1;
562
563end:
564 close(fd_dst);
565
566 if(NULL != real_write_content)
567 {
568 free(real_write_content);
569 real_write_content = NULL;
570 }
571
572 return ret;
573}
574
575
576static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src)
577{
xf.lice873192023-11-08 17:10:35 -0800578 memcpy(dst, src, sizeof(T_FLAGS_INFO));
xf.li6c8fc1e2023-08-12 00:11:09 -0700579
580 return;
581}
582
583
584static int get_current_system()
585{
586 char buf[1024] = {0};
587
588 char *para = NULL;
589 int matches = 0;
590
591 FILE *fd_cmd = NULL;
592 char *line_str = NULL;
593
594 int current_system = -1;
595
596
597 fd_cmd = fopen(FILE_PATH_PROC_CMDLINE, "r");
598 if (!fd_cmd)
599 {
xf.lice873192023-11-08 17:10:35 -0800600 printf("Open file %s error, error:%s", FILE_PATH_PROC_CMDLINE, strerror(errno));
xf.li6c8fc1e2023-08-12 00:11:09 -0700601 return SYSTEM_INDEX_UNKNOWN;
602 }
603
604 while (!feof(fd_cmd))
605 {
606 memset(buf, 0, sizeof(buf));
607 line_str = fgets(buf, sizeof(buf), fd_cmd);
608
609 if (NULL == line_str)
610 {
xf.lice873192023-11-08 17:10:35 -0800611 printf("get info from /proc/cmdline error:%s", strerror(errno));
xf.li6c8fc1e2023-08-12 00:11:09 -0700612 goto end;
613 }
614
xf.lice873192023-11-08 17:10:35 -0800615 printf("buff:%s", buf);
xf.li6c8fc1e2023-08-12 00:11:09 -0700616
617 para = strtok(buf, " ");
618 while (para)
619 {
xf.lice873192023-11-08 17:10:35 -0800620 printf("para:%s", para);
xf.li6c8fc1e2023-08-12 00:11:09 -0700621 if (strncmp(para, PROC_CMDLINE_SYSTEM_A_FLAG, strlen(PROC_CMDLINE_SYSTEM_A_FLAG)) == 0)
622 {
623 current_system = SYSTEM_INDEX_1;
624 goto end;
625 }
626 else if (strncmp(para, PROC_CMDLINE_SYSTEM_B_FLAG, strlen(PROC_CMDLINE_SYSTEM_B_FLAG)) == 0)
627 {
628 current_system = SYSTEM_INDEX_2;
629 goto end;
630 }
631 else
632 {
633 //:
634 }
635 para = strtok(NULL, " ");
636 }
637 }
638
639end:
640 if (fd_cmd)
641 {
642 fclose(fd_cmd);
643 }
644
645 return current_system;
646}
647
648
649/*******************************************************************************
650 * Global function implementations *
651 ******************************************************************************/
652int flags_init()
653{
654 T_FLAGS_INFO flags_info = {0};
655 int main_index = 0;
656 int back_index = 1;
657 int ret = -1;
658 int fd_dst = -1;
659 char mtd_path[MAX_PATH_LEN] = {0};
660 struct mtd_info_user meminfo = {0};
661 partition_mtd_info_t mtd_info = {0};
662
663 unsigned char *real_write_content = NULL;
664 T_FLAGS_INFO *p_write = NULL;
665
666 int index = 0;
667 int block_num = 0;
668 int already_write = 0;
669
xf.lice873192023-11-08 17:10:35 -0800670 unsigned int crc_32 = 0;
671
xf.li6c8fc1e2023-08-12 00:11:09 -0700672 flags_info.magic_start = FLAGS_MAGIC;
673
674 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
675 flags_info.boot_fota_flag.fota_status = 0;
676 flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
677 flags_info.boot_fota_flag.system.try_cnt = 0;
678 flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
679 flags_info.boot_fota_flag.system2.try_cnt = 0;
680
681 flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
682
683 flags_info.ubifs_status.fs_status = 0;
684
685 flags_info.magic_end = FLAGS_MAGIC;
686
xf.lice873192023-11-08 17:10:35 -0800687 crc32init_le();
688 crc_32 = crc32_le(0, (unsigned char const *)(&flags_info), sizeof(flags_info));
689 flags_log("init crc_32=%u", crc_32);
690
691 flags_info.crc32 = crc_32;
692
xf.li6c8fc1e2023-08-12 00:11:09 -0700693 ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
694 if (ret < 0)
695 {
696 flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
697
698 return -1;
699 }
700
701 if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
702 {
703 flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
704
705 return -1;
706 }
707
708 if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
709 {
710 flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
711
712 goto error;
713 }
714
715 mtd_info.parti_file_desc = fd_dst;
716 mtd_info.mtd_blocksize = meminfo.erasesize;
717 mtd_info.mtd_oobsize = meminfo.oobsize;
718 mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
719 mtd_info.mtd_pagesize = meminfo.writesize;
720 mtd_info.mtd_totalsize = meminfo.size;
721
722 block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
723
724 real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
725 if (NULL == real_write_content)
726 {
727 flags_err("malloc for block fail");
728
729 goto error;
730 }
731
732 memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
733 memcpy(real_write_content, (char *)(&flags_info), sizeof(T_FLAGS_INFO));
734
735 for (index = 0; index < block_num; index++)
736 {
737 if (already_write >= FLAGS_INIT_VALID_BLOCKS_NUM)
738 {
739 break;
740 }
741
742 ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
743 if (ret == 0)
744 {
745 already_write++;
746 flags_log("write init valid block num: %d", already_write);
747
748 continue;
749 }
750 else if (BAD_BLOCK == ret)
751 {
752 continue;
753 }
754 }
755
756 if (already_write >= 2)
757 {
758 flags_log("init system status success, alread write block: %d", already_write);
759 ret = 0;
760 }
761 else
762 {
763 flags_log("init system status fail, alread write block: %d", already_write);
764 ret = -1;
765 }
766
767 goto end;
768
769error:
770 ret = -1;
771
772end:
773 close(fd_dst);
774
775 if(NULL != real_write_content)
776 {
777 free(real_write_content);
778 real_write_content = NULL;
779 }
780
781 return ret;
782}
783
784
785int flags_get(T_FLAGS_INFO *p_flags_info)
786{
787 T_FLAGS_INFO main_flag = {0};
788 T_FLAGS_INFO backup_flag = {0};
xf.lice873192023-11-08 17:10:35 -0800789 T_FLAGS_INFO p_flags_info_tmp = {0};
790
xf.li6c8fc1e2023-08-12 00:11:09 -0700791 int main_index = 0;
792 int backup_index = 1;
793
xf.lice873192023-11-08 17:10:35 -0800794 unsigned int crc32_main = 0;
795 unsigned int crc32_backup = 0;
796
797 unsigned int crc32_le_main = 0;
798 unsigned int crc32_le_backup = 0;
799
xf.li6c8fc1e2023-08-12 00:11:09 -0700800 if (NULL == p_flags_info)
801 {
802 flags_err("invalid param NULL");
803
804 return -1;
805 }
806
807 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
808 {
809 flags_err("get flags info fail");
810
811 return -1;
812 }
xf.lice873192023-11-08 17:10:35 -0800813
814 flags_log("main_flag crc32=%u", main_flag.crc32);
815 flags_log("backup_flag crc32=%u", backup_flag.crc32);
816
817 if ((0 == main_flag.crc32) && (0 == backup_flag.crc32))
818 {
819 if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
820 {
821 if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
822 {
823 memcpy(&p_flags_info_tmp, &main_flag, sizeof(T_FLAGS_INFO));
824 crc32init_le();
825 p_flags_info_tmp.crc32 = 0;
826 p_flags_info_tmp.crc32 = crc32_le(0, (unsigned char const *)(&p_flags_info_tmp), sizeof(T_FLAGS_INFO));
827 flags_log("old version, set crc32=%u", p_flags_info_tmp.crc32);
828
829 if (set_flags_info(&p_flags_info_tmp, &main_index, &backup_index) != 0)
830 {
831 flags_err("old version, set flags info fail");
832 return -1;
833 }
834
835 copy_flags_info(p_flags_info, &main_flag);
836 return 0;
837 }
838 }
839 }
xf.li6c8fc1e2023-08-12 00:11:09 -0700840
xf.lice873192023-11-08 17:10:35 -0800841 crc32_main = main_flag.crc32;
842 crc32_backup = backup_flag.crc32;
843
844 main_flag.crc32 = 0;
845 backup_flag.crc32 = 0;
846
847 crc32init_le();
848
849 crc32_le_main = crc32_le(0, (unsigned char const *)(&main_flag), sizeof(main_flag));
850 flags_log("crc32_le_main crc32=%u", crc32_le_main);
851
852 crc32_le_backup = crc32_le(0, (unsigned char const *)(&backup_flag), sizeof(backup_flag));
853 flags_log("crc32_le_backup crc32=%u", crc32_le_backup);
854
855 if (crc32_main == crc32_le_main)
xf.li6c8fc1e2023-08-12 00:11:09 -0700856 {
857 copy_flags_info(p_flags_info, &main_flag);
858
859 return 0;
860 }
861
xf.lice873192023-11-08 17:10:35 -0800862 if (crc32_backup == crc32_le_backup)
xf.li6c8fc1e2023-08-12 00:11:09 -0700863 {
864 copy_flags_info(p_flags_info, &backup_flag);
865
866 return 0;
867 }
868
869 flags_err("do not find valid flags info");
870
871 return -1;
872}
873
874
875int flags_set(T_FLAGS_INFO *p_flags_info)
876{
877 T_FLAGS_INFO main_flag = {0};
878 T_FLAGS_INFO backup_flag = {0};
879 int main_index = 0;
880 int backup_index = 1;
881
882 if (NULL == p_flags_info)
883 {
884 flags_err("invalid param NULL");
885
886 return -1;
887 }
888
889 if ((FLAGS_MAGIC != p_flags_info->magic_start) || (FLAGS_MAGIC != p_flags_info->magic_end))
890 {
891 flags_err("invalid magic");
892
893 return -1;
894 }
895
896 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
897 {
898 flags_err("get flags info fail");
899
900 return -1;
901 }
902
xf.lice873192023-11-08 17:10:35 -0800903 crc32init_le();
904 p_flags_info->crc32 = 0;
905 p_flags_info->crc32 = crc32_le(0, (unsigned char const *)p_flags_info, sizeof(T_FLAGS_INFO));
906 flags_log("set crc32=%u", p_flags_info->crc32);
907
xf.li6c8fc1e2023-08-12 00:11:09 -0700908 if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)
909 {
910 flags_err("set ubifs status fail");
911
912 return -1;
913 }
914
915 return 0;
916}
917
918
919int flags_get_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
920{
xf.lice873192023-11-08 17:10:35 -0800921 T_FLAGS_INFO p_flags_info = {0};
xf.li6c8fc1e2023-08-12 00:11:09 -0700922
923 if (NULL == p_ubifs_status)
924 {
925 flags_err("invalid param NULL");
xf.li6c8fc1e2023-08-12 00:11:09 -0700926 return -1;
927 }
928
xf.lice873192023-11-08 17:10:35 -0800929 if (0 == flags_get(&p_flags_info))
930 {
931 p_ubifs_status->fs_status = p_flags_info.ubifs_status.fs_status;
932 strncpy(p_ubifs_status->fs_mtd_name, p_flags_info.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
933 strncpy(p_ubifs_status->fs_ubi_vol_name, p_flags_info.ubifs_status.fs_ubi_vol_name, sizeof(p_ubifs_status->fs_ubi_vol_name));
xf.li6c8fc1e2023-08-12 00:11:09 -0700934
xf.lice873192023-11-08 17:10:35 -0800935 return 0;
936 }
937 else
938 {
939 flags_err("do not find valid flags info");
940 return -1;
941 }
xf.li6c8fc1e2023-08-12 00:11:09 -0700942}
943
944
945int flags_set_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
946{
xf.lice873192023-11-08 17:10:35 -0800947 T_FLAGS_INFO p_flags_info = {0};
xf.li6c8fc1e2023-08-12 00:11:09 -0700948
xf.li6c8fc1e2023-08-12 00:11:09 -0700949 if (NULL == p_ubifs_status)
950 {
951 flags_err("invalid param NULL");
xf.lice873192023-11-08 17:10:35 -0800952 return -1;
953 }
954
955 if (0 != flags_get(&p_flags_info))
956 {
957 flags_err("get ubifs status invalid");
xf.li6c8fc1e2023-08-12 00:11:09 -0700958 return -1;
959 }
960
xf.lice873192023-11-08 17:10:35 -0800961 memcpy(&(p_flags_info.ubifs_status), p_ubifs_status, sizeof(T_UBIFS_STATUS));
xf.li6c8fc1e2023-08-12 00:11:09 -0700962
xf.lice873192023-11-08 17:10:35 -0800963 if (0 != flags_set(&p_flags_info))
964 {
965 flags_err("set ubifs status fail");
966 return -1;
967 }
968
xf.li6c8fc1e2023-08-12 00:11:09 -0700969 return 0;
970}
971
xf.lice873192023-11-08 17:10:35 -0800972
xf.liaa4d92f2023-09-13 00:18:58 -0700973unsigned int flags_get_nvroflag(void)
974{
975 T_FLAGS_INFO t_flag = {0};
976
977 if (flags_get(&t_flag) != 0)
978 return NVRO_INVALID;
979 return t_flag.nvro_flag;
980}
981
xf.lice873192023-11-08 17:10:35 -0800982
xf.liaa4d92f2023-09-13 00:18:58 -0700983int flags_set_nvroflag(unsigned int flag)
984{
985 T_FLAGS_INFO t_flag = {0};
986
987 if (flags_get(&t_flag) != 0)
988 return -1;
989 if (t_flag.nvro_flag == flag)
990 return 0;
991 if (flag == NVRO_RESTORING)
992 {
993 if (t_flag.nvro_flag != NVRO_BACKED_UP)
994 {
995 printf("[error]flags nvro only NVRO_BACKED_UP switch to NVRO_RESTORING\n");
996 return -1;
997 }
998 }
999 t_flag.nvro_flag = flag;
1000
1001 return flags_set(&t_flag);
1002}
xf.li6c8fc1e2023-08-12 00:11:09 -07001003
xf.lice873192023-11-08 17:10:35 -08001004
xf.li6c8fc1e2023-08-12 00:11:09 -07001005int flags_get_current_system()
1006{
1007 int current = get_current_system();
1008
1009 if (current == 1)
1010 {
1011 return DUAL_SYSTEM;
1012 }
1013 else if (current == 2)
1014 {
1015 return DUAL_SYSTEM2;
1016 }
1017
1018 return -1;
1019}
1020
xf.lice873192023-11-08 17:10:35 -08001021
1022/* ´ËAPI½öÓÃÓÚµ÷²â£¬Õýʽ´úÂë²»¿ÉʹÓà */
1023int flags_get_nocrc(T_FLAGS_INFO *p_flags_info)
1024{
1025 T_FLAGS_INFO main_flag = {0};
1026 T_FLAGS_INFO backup_flag = {0};
1027 int main_index = 0;
1028 int backup_index = 1;
1029
1030 if (NULL == p_flags_info)
1031 {
1032 flags_err("invalid param NULL");
1033 return -1;
1034 }
1035
1036 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
1037 {
1038 flags_err("get flags info fail");
1039 return -1;
1040 }
1041
1042 if (0 != memcmp(&main_flag, &backup_flag, sizeof(T_FLAGS_INFO)))
1043 {
1044 flags_err("main flag and backup flag are different");
1045 return -1;
1046 }
1047
1048 copy_flags_info(p_flags_info, &main_flag);
1049 p_flags_info->crc32 = main_flag.crc32;
1050
1051 return 0;
1052}
1053
1054
1055/* ´ËAPI½öÓÃÓÚµ÷²â£¬Õýʽ´úÂë²»¿ÉʹÓà */
1056int flags_set_nocrc(T_FLAGS_INFO *p_flags_info)
1057{
1058 T_FLAGS_INFO main_flag = {0};
1059 T_FLAGS_INFO backup_flag = {0};
1060 int main_index = 0;
1061 int backup_index = 1;
1062
1063 if (NULL == p_flags_info)
1064 {
1065 flags_err("invalid param NULL");
1066 return -1;
1067 }
1068
1069 if ((FLAGS_MAGIC != p_flags_info->magic_start) || (FLAGS_MAGIC != p_flags_info->magic_end))
1070 {
1071 flags_err("invalid magic");
1072 return -1;
1073 }
1074
1075 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
1076 {
1077 flags_err("get flags info fail");
1078 return -1;
1079 }
1080
1081 if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)
1082 {
1083 flags_err("set ubifs status fail");
1084 return -1;
1085 }
1086
1087 return 0;
1088}
1089
1090
1091void crc32init_le(void)
1092{
1093 crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
1094}
1095
1096
1097unsigned int crc32_le(unsigned int crc, unsigned char const *p, size_t len)
1098{
1099 return crc32_le_generic(crc, p, len, (const unsigned int (*)[256])crc32table_le);
1100}
1101