blob: a0c9ff3ca65a99ccd89d9e7b95ec082a6a75aa3a [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;
xf.li7ccf8372024-03-07 00:08:02 -0800671 unsigned int crc_32_1 = 0;
672
xf.li6c8fc1e2023-08-12 00:11:09 -0700673 flags_info.magic_start = FLAGS_MAGIC;
674
675 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
676 flags_info.boot_fota_flag.fota_status = 0;
677 flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
678 flags_info.boot_fota_flag.system.try_cnt = 0;
679 flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
680 flags_info.boot_fota_flag.system2.try_cnt = 0;
681
682 flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
683
684 flags_info.ubifs_status.fs_status = 0;
685
686 flags_info.magic_end = FLAGS_MAGIC;
687
xf.lice873192023-11-08 17:10:35 -0800688 crc32init_le();
xf.li7ccf8372024-03-07 00:08:02 -0800689 crc_32 = crc32_le(0, (unsigned char const *)(&flags_info), 512);
xf.lice873192023-11-08 17:10:35 -0800690 flags_log("init crc_32=%u", crc_32);
691
692 flags_info.crc32 = crc_32;
xf.li7ccf8372024-03-07 00:08:02 -0800693
694 crc32init_le();
695 crc_32_1 = crc32_le(0, (unsigned char const *)(&flags_info), sizeof(flags_info));
696 flags_log("init crc_32_1=%u", crc_32_1);
697
698 flags_info.crc32_1 = crc_32_1;
xf.lice873192023-11-08 17:10:35 -0800699
xf.li6c8fc1e2023-08-12 00:11:09 -0700700 ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
701 if (ret < 0)
702 {
703 flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
704
705 return -1;
706 }
707
708 if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
709 {
710 flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
711
712 return -1;
713 }
714
715 if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
716 {
717 flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
718
719 goto error;
720 }
721
722 mtd_info.parti_file_desc = fd_dst;
723 mtd_info.mtd_blocksize = meminfo.erasesize;
724 mtd_info.mtd_oobsize = meminfo.oobsize;
725 mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
726 mtd_info.mtd_pagesize = meminfo.writesize;
727 mtd_info.mtd_totalsize = meminfo.size;
728
729 block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
730
731 real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
732 if (NULL == real_write_content)
733 {
734 flags_err("malloc for block fail");
735
736 goto error;
737 }
738
739 memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
740 memcpy(real_write_content, (char *)(&flags_info), sizeof(T_FLAGS_INFO));
741
742 for (index = 0; index < block_num; index++)
743 {
744 if (already_write >= FLAGS_INIT_VALID_BLOCKS_NUM)
745 {
746 break;
747 }
748
749 ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
750 if (ret == 0)
751 {
752 already_write++;
753 flags_log("write init valid block num: %d", already_write);
754
755 continue;
756 }
757 else if (BAD_BLOCK == ret)
758 {
759 continue;
760 }
761 }
762
763 if (already_write >= 2)
764 {
765 flags_log("init system status success, alread write block: %d", already_write);
766 ret = 0;
767 }
768 else
769 {
770 flags_log("init system status fail, alread write block: %d", already_write);
771 ret = -1;
772 }
773
774 goto end;
775
776error:
777 ret = -1;
778
779end:
780 close(fd_dst);
781
782 if(NULL != real_write_content)
783 {
784 free(real_write_content);
785 real_write_content = NULL;
786 }
787
788 return ret;
789}
790
791
792int flags_get(T_FLAGS_INFO *p_flags_info)
793{
794 T_FLAGS_INFO main_flag = {0};
795 T_FLAGS_INFO backup_flag = {0};
xf.lice873192023-11-08 17:10:35 -0800796 T_FLAGS_INFO p_flags_info_tmp = {0};
xf.li7ccf8372024-03-07 00:08:02 -0800797 char delta_F[IMG_NAME_LEN] = {0};
xf.lice873192023-11-08 17:10:35 -0800798
xf.li6c8fc1e2023-08-12 00:11:09 -0700799 int main_index = 0;
800 int backup_index = 1;
801
xf.lice873192023-11-08 17:10:35 -0800802 unsigned int crc32_main = 0;
803 unsigned int crc32_backup = 0;
804
805 unsigned int crc32_le_main = 0;
806 unsigned int crc32_le_backup = 0;
807
xf.li6c8fc1e2023-08-12 00:11:09 -0700808 if (NULL == p_flags_info)
809 {
810 flags_err("invalid param NULL");
811
812 return -1;
813 }
814
815 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
816 {
817 flags_err("get flags info fail");
818
819 return -1;
820 }
xf.lice873192023-11-08 17:10:35 -0800821
xf.li7ccf8372024-03-07 00:08:02 -0800822 flags_log("main_flag crc32=%u, crc32_1=%u", main_flag.crc32, main_flag.crc32_1);
823 flags_log("backup_flag crc32=%u, crc32_1=%u", backup_flag.crc32, backup_flag.crc32_1);
824
825 memset(delta_F, 0xFF, IMG_NAME_LEN);
826
827 if ((0 == memcmp(main_flag.img_size[0].name, delta_F, IMG_NAME_LEN)) && (0 == memcmp(backup_flag.img_size[0].name, delta_F, IMG_NAME_LEN)))
xf.lice873192023-11-08 17:10:35 -0800828 {
xf.li7ccf8372024-03-07 00:08:02 -0800829 memcpy(&p_flags_info_tmp, &main_flag, sizeof(T_FLAGS_INFO));
830 memset(p_flags_info_tmp.img_size[0].name, 0, sizeof(T_FLAGS_INFO)-512);
831
832 crc32init_le();
833 p_flags_info_tmp.crc32_1 = crc32_le(0, (unsigned char const *)(&p_flags_info_tmp), sizeof(T_FLAGS_INFO));
834 flags_log("fix old, set crc32_1=%u", p_flags_info_tmp.crc32_1);
835
836 if (set_flags_info(&p_flags_info_tmp, &main_index, &backup_index) != 0)
xf.lice873192023-11-08 17:10:35 -0800837 {
xf.li7ccf8372024-03-07 00:08:02 -0800838 flags_err("fix old, set flags info fail");
839 return -1;
xf.lice873192023-11-08 17:10:35 -0800840 }
xf.li7ccf8372024-03-07 00:08:02 -0800841
842 copy_flags_info(p_flags_info, &main_flag);
843 return 0;
xf.lice873192023-11-08 17:10:35 -0800844 }
xf.li6c8fc1e2023-08-12 00:11:09 -0700845
xf.li7ccf8372024-03-07 00:08:02 -0800846 crc32_main = main_flag.crc32_1;
847 crc32_backup = backup_flag.crc32_1;
xf.lice873192023-11-08 17:10:35 -0800848
xf.li7ccf8372024-03-07 00:08:02 -0800849 main_flag.crc32_1 = 0;
850 backup_flag.crc32_1 = 0;
xf.lice873192023-11-08 17:10:35 -0800851
852 crc32init_le();
853
854 crc32_le_main = crc32_le(0, (unsigned char const *)(&main_flag), sizeof(main_flag));
855 flags_log("crc32_le_main crc32=%u", crc32_le_main);
856
857 crc32_le_backup = crc32_le(0, (unsigned char const *)(&backup_flag), sizeof(backup_flag));
858 flags_log("crc32_le_backup crc32=%u", crc32_le_backup);
859
860 if (crc32_main == crc32_le_main)
xf.li6c8fc1e2023-08-12 00:11:09 -0700861 {
862 copy_flags_info(p_flags_info, &main_flag);
863
864 return 0;
865 }
866
xf.lice873192023-11-08 17:10:35 -0800867 if (crc32_backup == crc32_le_backup)
xf.li6c8fc1e2023-08-12 00:11:09 -0700868 {
869 copy_flags_info(p_flags_info, &backup_flag);
870
871 return 0;
872 }
873
874 flags_err("do not find valid flags info");
875
876 return -1;
877}
878
879
880int flags_set(T_FLAGS_INFO *p_flags_info)
881{
882 T_FLAGS_INFO main_flag = {0};
883 T_FLAGS_INFO backup_flag = {0};
884 int main_index = 0;
885 int backup_index = 1;
886
887 if (NULL == p_flags_info)
888 {
889 flags_err("invalid param NULL");
890
891 return -1;
892 }
893
894 if ((FLAGS_MAGIC != p_flags_info->magic_start) || (FLAGS_MAGIC != p_flags_info->magic_end))
895 {
896 flags_err("invalid magic");
897
898 return -1;
899 }
900
901 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
902 {
903 flags_err("get flags info fail");
904
905 return -1;
906 }
907
xf.lice873192023-11-08 17:10:35 -0800908 crc32init_le();
909 p_flags_info->crc32 = 0;
xf.li7ccf8372024-03-07 00:08:02 -0800910 p_flags_info->crc32 = crc32_le(0, (unsigned char const *)p_flags_info, 512);
xf.lice873192023-11-08 17:10:35 -0800911 flags_log("set crc32=%u", p_flags_info->crc32);
xf.li7ccf8372024-03-07 00:08:02 -0800912
913 crc32init_le();
914 p_flags_info->crc32_1 = 0;
915 p_flags_info->crc32_1 = crc32_le(0, (unsigned char const *)p_flags_info, sizeof(T_FLAGS_INFO));
916 flags_log("set crc32_1=%u", p_flags_info->crc32_1);
xf.lice873192023-11-08 17:10:35 -0800917
xf.li6c8fc1e2023-08-12 00:11:09 -0700918 if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)
919 {
920 flags_err("set ubifs status fail");
921
922 return -1;
923 }
924
925 return 0;
926}
927
928
929int flags_get_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
930{
xf.lice873192023-11-08 17:10:35 -0800931 T_FLAGS_INFO p_flags_info = {0};
xf.li6c8fc1e2023-08-12 00:11:09 -0700932
933 if (NULL == p_ubifs_status)
934 {
935 flags_err("invalid param NULL");
xf.li6c8fc1e2023-08-12 00:11:09 -0700936 return -1;
937 }
938
xf.lice873192023-11-08 17:10:35 -0800939 if (0 == flags_get(&p_flags_info))
940 {
941 p_ubifs_status->fs_status = p_flags_info.ubifs_status.fs_status;
942 strncpy(p_ubifs_status->fs_mtd_name, p_flags_info.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
943 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 -0700944
xf.lice873192023-11-08 17:10:35 -0800945 return 0;
946 }
947 else
948 {
949 flags_err("do not find valid flags info");
950 return -1;
951 }
xf.li6c8fc1e2023-08-12 00:11:09 -0700952}
953
954
955int flags_set_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
956{
xf.lice873192023-11-08 17:10:35 -0800957 T_FLAGS_INFO p_flags_info = {0};
xf.li6c8fc1e2023-08-12 00:11:09 -0700958
xf.li6c8fc1e2023-08-12 00:11:09 -0700959 if (NULL == p_ubifs_status)
960 {
961 flags_err("invalid param NULL");
xf.lice873192023-11-08 17:10:35 -0800962 return -1;
963 }
964
965 if (0 != flags_get(&p_flags_info))
966 {
967 flags_err("get ubifs status invalid");
xf.li6c8fc1e2023-08-12 00:11:09 -0700968 return -1;
969 }
970
xf.lice873192023-11-08 17:10:35 -0800971 memcpy(&(p_flags_info.ubifs_status), p_ubifs_status, sizeof(T_UBIFS_STATUS));
xf.li6c8fc1e2023-08-12 00:11:09 -0700972
xf.lice873192023-11-08 17:10:35 -0800973 if (0 != flags_set(&p_flags_info))
974 {
975 flags_err("set ubifs status fail");
976 return -1;
977 }
978
xf.li6c8fc1e2023-08-12 00:11:09 -0700979 return 0;
980}
981
xf.lice873192023-11-08 17:10:35 -0800982
xf.liaa4d92f2023-09-13 00:18:58 -0700983unsigned int flags_get_nvroflag(void)
984{
985 T_FLAGS_INFO t_flag = {0};
986
987 if (flags_get(&t_flag) != 0)
988 return NVRO_INVALID;
989 return t_flag.nvro_flag;
990}
991
xf.lice873192023-11-08 17:10:35 -0800992
xf.liaa4d92f2023-09-13 00:18:58 -0700993int flags_set_nvroflag(unsigned int flag)
994{
995 T_FLAGS_INFO t_flag = {0};
996
997 if (flags_get(&t_flag) != 0)
998 return -1;
999 if (t_flag.nvro_flag == flag)
1000 return 0;
1001 if (flag == NVRO_RESTORING)
1002 {
1003 if (t_flag.nvro_flag != NVRO_BACKED_UP)
1004 {
1005 printf("[error]flags nvro only NVRO_BACKED_UP switch to NVRO_RESTORING\n");
1006 return -1;
1007 }
1008 }
1009 t_flag.nvro_flag = flag;
1010
1011 return flags_set(&t_flag);
1012}
xf.li6c8fc1e2023-08-12 00:11:09 -07001013
xf.lice873192023-11-08 17:10:35 -08001014
xf.li6c8fc1e2023-08-12 00:11:09 -07001015int flags_get_current_system()
1016{
1017 int current = get_current_system();
1018
1019 if (current == 1)
1020 {
1021 return DUAL_SYSTEM;
1022 }
1023 else if (current == 2)
1024 {
1025 return DUAL_SYSTEM2;
1026 }
1027
1028 return -1;
1029}
1030
xf.lice873192023-11-08 17:10:35 -08001031
1032/* ´ËAPI½öÓÃÓÚµ÷²â£¬Õýʽ´úÂë²»¿ÉʹÓà */
1033int flags_get_nocrc(T_FLAGS_INFO *p_flags_info)
1034{
1035 T_FLAGS_INFO main_flag = {0};
1036 T_FLAGS_INFO backup_flag = {0};
1037 int main_index = 0;
1038 int backup_index = 1;
1039
1040 if (NULL == p_flags_info)
1041 {
1042 flags_err("invalid param NULL");
1043 return -1;
1044 }
1045
1046 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
1047 {
1048 flags_err("get flags info fail");
1049 return -1;
1050 }
1051
1052 if (0 != memcmp(&main_flag, &backup_flag, sizeof(T_FLAGS_INFO)))
1053 {
1054 flags_err("main flag and backup flag are different");
1055 return -1;
1056 }
1057
1058 copy_flags_info(p_flags_info, &main_flag);
1059 p_flags_info->crc32 = main_flag.crc32;
1060
1061 return 0;
1062}
1063
1064
1065/* ´ËAPI½öÓÃÓÚµ÷²â£¬Õýʽ´úÂë²»¿ÉʹÓà */
1066int flags_set_nocrc(T_FLAGS_INFO *p_flags_info)
1067{
1068 T_FLAGS_INFO main_flag = {0};
1069 T_FLAGS_INFO backup_flag = {0};
1070 int main_index = 0;
1071 int backup_index = 1;
1072
1073 if (NULL == p_flags_info)
1074 {
1075 flags_err("invalid param NULL");
1076 return -1;
1077 }
1078
1079 if ((FLAGS_MAGIC != p_flags_info->magic_start) || (FLAGS_MAGIC != p_flags_info->magic_end))
1080 {
1081 flags_err("invalid magic");
1082 return -1;
1083 }
1084
1085 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
1086 {
1087 flags_err("get flags info fail");
1088 return -1;
1089 }
1090
1091 if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)
1092 {
1093 flags_err("set ubifs status fail");
1094 return -1;
1095 }
1096
1097 return 0;
1098}
1099
1100
1101void crc32init_le(void)
1102{
1103 crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
1104}
1105
1106
1107unsigned int crc32_le(unsigned int crc, unsigned char const *p, size_t len)
1108{
1109 return crc32_le_generic(crc, p, len, (const unsigned int (*)[256])crc32table_le);
1110}
1111