blob: 37653fc8262a52f6e9edbb0a87213665b4a9d6eb [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
55
56/*******************************************************************************
57 * Type definitions *
58 ******************************************************************************/
59typedef struct
60{
61 unsigned int mtd_totalsize; // mtd device total size
62 unsigned int mtd_pageperblock; // mtd device page per block
63 unsigned int mtd_blocksize; // mtd device block size
64 unsigned int mtd_pagesize; // mtd device page size
65 unsigned int mtd_oobsize; // mtd device oob size
66 int parti_file_desc; // partition update file description
67} partition_mtd_info_t;
68
69
70typedef enum
71{
72 DEVICE_MTD = 0,
73 DEVICE_ZFTL = 1,
74 DEVICE_MTD_BLOCK,
75} device_type_t;
76
77
78/*******************************************************************************
79 * Local variable definitions *
80 ******************************************************************************/
81
82
83/*******************************************************************************
84 * Global variable definitions *
85 ******************************************************************************/
86
87
88/*******************************************************************************
89 * Local function declarations *
90 ******************************************************************************/
91static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len);
92static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len);
93
94static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index);
95static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index);
96
97static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src);
98
99static int get_current_system();
100
101
102/*******************************************************************************
103 * Local function implementations *
104 ******************************************************************************/
105static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len)
106{
107 FILE *fp_mtd = 0;
108 char buf[128];
109 char *line_str;
110
111 if (!o_mtd_path_len)
112 {
113 return -1;
114 }
115
116 fp_mtd = fopen("/proc/mtd", "r+");
117 if (NULL == fp_mtd)
118 {
119 flags_err("open file error: %s", strerror(errno));
120 return -1;
121 }
122 // printf("[libmtd]: partition name:%s\n", i_parti_name);
123
124 while (1)
125 {
126 int matches = 0;
127 char mtdname[64] = {0};
128 int mtdnum = 0;
129 unsigned int mtdsize, mtderasesize;
130 memset(buf, 0, sizeof(buf));
131 line_str = fgets(buf, sizeof(buf) - 1, fp_mtd);
132
133 if (NULL == line_str)
134 {
135 flags_err("get info from mtd error: %s", strerror(errno));
136 fclose(fp_mtd);
137 return -1;
138 }
139 // mtd5: 00100000 00020000 "fotaflag"
140 matches = sscanf(buf, "mtd%d: %x %x \"%63[^\"]",
141 &mtdnum, &mtdsize, &mtderasesize, mtdname);
142 mtdname[63] = '\0';
143
144 if ((matches == 4) && (strcmp(mtdname, i_parti_name) == 0))
145 {
146 memset(o_mtd_path, 0, o_mtd_path_len);
147 if (device_type == DEVICE_MTD_BLOCK)
148 {
149 snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtdblock%d", mtdnum);
150 }
151 else if (device_type == DEVICE_MTD)
152 {
153 snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtd%d", mtdnum);
154 }
155 else if (device_type == DEVICE_ZFTL)
156 {
157 snprintf(o_mtd_path, o_mtd_path_len, "/dev/zftl%d", mtdnum);
158 }
159 else
160 {
161 flags_err("unknown device type %d", device_type);
162 fclose(fp_mtd);
163 return -1;
164 }
165 // printf("[libmtd]: o_mtd_path=[%s]\n", o_mtd_path);
166 break;
167 }
168 }
169 fclose(fp_mtd);
170 return 0;
171}
172
173
174static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len)
175{
176 struct erase_info_user erase_info;
177 int write_len = 0;
178 long long offs = 0;
179
180 erase_info.start = index * p_mtd_info->mtd_blocksize;
181 erase_info.length = p_mtd_info->mtd_blocksize;
182
183 offs = (long long)index * p_mtd_info->mtd_blocksize;
184
185 if (ioctl(p_mtd_info->parti_file_desc, WRITEENABLE, 0) != 0)
186 {
187 flags_err("failed to enable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
188
189 return -1;
190 }
191
192 if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
193 {
194 flags_err("ioctl [MEMGETBADBLOCK] block: %d, change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
195
196 return BAD_BLOCK;
197 }
198
199 if (ioctl(p_mtd_info->parti_file_desc, MEMERASE, &erase_info) < 0)
200 {
201 flags_err("ioctl [MEMERASE] block: %d fail, errno=%d, strerror=[%s]", index, errno, strerror(errno));
202
203 return -1;
204 }
205
206 if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
207 {
208 flags_err("ioctl [MEMGETBADBLOCK] block:%d , change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
209
210 return BAD_BLOCK;
211 }
212
213 if (lseek(p_mtd_info->parti_file_desc, index * p_mtd_info->mtd_blocksize, SEEK_SET) < 0)
214 {
215 flags_err("lseek fail, errno=%d, strerror=[%s]", errno, strerror(errno));
216
217 return -1;
218 }
219
220 write_len = write(p_mtd_info->parti_file_desc, content, p_mtd_info->mtd_blocksize);
221
222 if (write_len != p_mtd_info->mtd_blocksize)
223 {
224 flags_err("write flash fail, errno=%d, strerror=[%s]", errno, strerror(errno));
225
226 return -1;
227 }
228
229 if (ioctl(p_mtd_info->parti_file_desc, WRITEDISABLE, 0) != 0)
230 {
231 flags_err("failed to disable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
232 }
233
234 return 0;
235}
236
237
238static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index)
239{
240 int ret = -1;
241 int fd_dst = -1;
242 char mtd_path[MAX_PATH_LEN] = {0};
243 struct mtd_info_user meminfo = {0};
244 partition_mtd_info_t mtd_info = {0};
245 int index = 0;
246 int block_num = 0;
247 int good_index = 0;
248 int read_len = 0;
249
250 long long offs = 0;
251
252 int block_flag = GOOD_BLOCK;
253
254 ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
255 if (ret < 0)
256 {
257 flags_err("partition [%s] not find", PARTITION_NAME_FLAGS);
258 goto error;
259 }
260
261 if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
262 {
263 flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
264 goto error;
265 }
266
267 mtd_info.parti_file_desc = fd_dst;
268
269 if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
270 {
271 flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
272 goto error_close;
273 }
274
275 mtd_info.mtd_blocksize = meminfo.erasesize;
276 mtd_info.mtd_oobsize = meminfo.oobsize;
277 mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
278 mtd_info.mtd_pagesize = meminfo.writesize;
279 mtd_info.mtd_totalsize = meminfo.size;
280
281 block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
282
283 for (index = 0; (good_index < 2 && index < block_num); index++)
284 {
285
286 offs = index * mtd_info.mtd_blocksize;
287 if (ioctl(mtd_info.parti_file_desc, MEMGETBADBLOCK, &offs) == 0)
288 {
289 block_flag = GOOD_BLOCK;
290 }
291 else
292 {
293 flags_err("flags block [%d] is bad, errno=%d, strerror=[%s]", index, errno, strerror(errno));
294 block_flag = BAD_BLOCK;
295 }
296
297 if (block_flag == GOOD_BLOCK)
298 {
299 if (lseek(mtd_info.parti_file_desc, index * mtd_info.mtd_blocksize, SEEK_SET) < 0)
300 {
301 flags_err("lseek error, errno=%d, strerror=[%s]", errno, strerror(errno));
302 goto error_close;
303 }
304
305 if (good_index == 0)
306 {
307 read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_main, sizeof(T_FLAGS_INFO));
308 *p_main_index = index;
309 }
310 else if (good_index == 1)
311 {
312 read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_backup, sizeof(T_FLAGS_INFO));
313 *p_backup_index = index;
314 }
315 else
316 {
317 break;
318 }
319
320 if (read_len < sizeof(T_FLAGS_INFO))
321 {
322 flags_err("read len (%d) < need len (%d)", read_len, sizeof(T_FLAGS_INFO));
323 goto error_close;
324 }
325
326 good_index++;
327 }
328
329 }
330
331 close(fd_dst);
332
333 return 0;
334
335error_close:
336 close(fd_dst);
337
338error:
339 return -1;
340}
341
342
343static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index)
344{
345 int ret = -1;
346 int fd_dst = -1;
347 char mtd_path[MAX_PATH_LEN] = {0};
348 struct mtd_info_user meminfo = {0};
349 partition_mtd_info_t mtd_info = {0};
350
351 unsigned char *real_write_content = NULL;
352
353 int index = 0;
354 int block_num = 0;
355 int main_index = *p_main_index;
356 int back_index = *p_backup_index;
357
358 ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
359 if (ret < 0)
360 {
361 flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
362
363 return -1;
364 }
365
366 if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
367 {
368 flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
369 return -1;
370 }
371
372 if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
373 {
374 flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
375 goto error;
376 }
377
378 mtd_info.parti_file_desc = fd_dst;
379 mtd_info.mtd_blocksize = meminfo.erasesize;
380 mtd_info.mtd_oobsize = meminfo.oobsize;
381 mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
382 mtd_info.mtd_pagesize = meminfo.writesize;
383 mtd_info.mtd_totalsize = meminfo.size;
384
385 block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
386
387 real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
388 if (NULL == real_write_content)
389 {
390 flags_err("malloc block fail");
391 goto error;
392 }
393
394 memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
395 memcpy(real_write_content, (char *)p_flags_info, sizeof(T_FLAGS_INFO));
396
397 flags_log("begin to write main flags");
398
399 for (index = 0; index < block_num; index++)
400 {
401 if (index == back_index)
402 {
403 continue;
404 }
405
406 ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
407 if (ret == 0)
408 {
409 // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
410 flags_log("main flags location: [%d]->[%d]", main_index, index);
411 main_index = index;
412 break;
413 }
414 else if (ret == BAD_BLOCK)
415 {
416 // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
417 flags_log("flags block index [%d] is bad", index);
418 continue;
419 }
420 else
421 {
422 flags_err("write main flags fail");
423 main_index = -1;
424 break;
425 }
426 }
427
428 flags_log("begin to write backup flags");
429
430 for (index = 0; index < block_num; index++)
431 {
432 if (index == main_index)
433 {
434 continue;
435 }
436
437 flags_log("write backup to [%d] block", index);
438 ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
439 if (ret == 0)
440 {
441 // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
442 flags_log("backup flags location: [%d]->[%d]", back_index, index);
443 back_index = index;
444 break;
445 }
446 else if (ret == BAD_BLOCK)
447 {
448 // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
449 continue;
450 }
451 else
452 {
453 flags_err("write backup flags fail");
454 back_index = -1;
455 break;
456 }
457 }
458
459 if (main_index == -1 && back_index == -1)
460 {
461 goto error;
462 }
463 else
464 {
465 ret = 0;
466 goto end;
467 }
468
469error:
470 ret = -1;
471
472end:
473 close(fd_dst);
474
475 if(NULL != real_write_content)
476 {
477 free(real_write_content);
478 real_write_content = NULL;
479 }
480
481 return ret;
482}
483
484
485static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src)
486{
487 dst->magic_start = src->magic_start;
488
489 dst->boot_fota_flag.boot_to = src->boot_fota_flag.boot_to;
490 dst->boot_fota_flag.fota_status = src->boot_fota_flag.fota_status;
491 dst->boot_fota_flag.system.status = src->boot_fota_flag.system.status;
492 dst->boot_fota_flag.system.try_cnt = src->boot_fota_flag.system.try_cnt;
493 dst->boot_fota_flag.system2.status = src->boot_fota_flag.system2.status;
494 dst->boot_fota_flag.system2.try_cnt = src->boot_fota_flag.system2.try_cnt;
495
496 dst->boot_env.dualsys_type = src->boot_env.dualsys_type;
497 strncpy(dst->boot_env.system_boot_env, src->boot_env.system_boot_env, sizeof(dst->boot_env.system_boot_env));
498 strncpy(dst->boot_env.system2_boot_env, src->boot_env.system2_boot_env, sizeof(dst->boot_env.system2_boot_env));
499
500 dst->ubifs_status.fs_status = src->ubifs_status.fs_status;
501 strncpy(dst->ubifs_status.fs_mtd_name, src->ubifs_status.fs_mtd_name, sizeof(dst->ubifs_status.fs_mtd_name));
502 strncpy(dst->ubifs_status.fs_ubi_vol_name, src->ubifs_status.fs_ubi_vol_name, sizeof(dst->ubifs_status.fs_ubi_vol_name));
503
504 dst->magic_end = src->magic_end;
505
506 return;
507}
508
509
510static int get_current_system()
511{
512 char buf[1024] = {0};
513
514 char *para = NULL;
515 int matches = 0;
516
517 FILE *fd_cmd = NULL;
518 char *line_str = NULL;
519
520 int current_system = -1;
521
522
523 fd_cmd = fopen(FILE_PATH_PROC_CMDLINE, "r");
524 if (!fd_cmd)
525 {
526 flags_err("open file %s error, error:%s", FILE_PATH_PROC_CMDLINE, strerror(errno));
527 return SYSTEM_INDEX_UNKNOWN;
528 }
529
530 while (!feof(fd_cmd))
531 {
532 memset(buf, 0, sizeof(buf));
533 line_str = fgets(buf, sizeof(buf), fd_cmd);
534
535 if (NULL == line_str)
536 {
537 flags_err("get info from /proc/cmdline error:%s", strerror(errno));
538 goto end;
539 }
540
541 flags_log("buff:%s", buf);
542
543 para = strtok(buf, " ");
544 while (para)
545 {
546 flags_log("para:%s", para);
547 if (strncmp(para, PROC_CMDLINE_SYSTEM_A_FLAG, strlen(PROC_CMDLINE_SYSTEM_A_FLAG)) == 0)
548 {
549 current_system = SYSTEM_INDEX_1;
550 goto end;
551 }
552 else if (strncmp(para, PROC_CMDLINE_SYSTEM_B_FLAG, strlen(PROC_CMDLINE_SYSTEM_B_FLAG)) == 0)
553 {
554 current_system = SYSTEM_INDEX_2;
555 goto end;
556 }
557 else
558 {
559 //:
560 }
561 para = strtok(NULL, " ");
562 }
563 }
564
565end:
566 if (fd_cmd)
567 {
568 fclose(fd_cmd);
569 }
570
571 return current_system;
572}
573
574
575/*******************************************************************************
576 * Global function implementations *
577 ******************************************************************************/
578int flags_init()
579{
580 T_FLAGS_INFO flags_info = {0};
581 int main_index = 0;
582 int back_index = 1;
583 int ret = -1;
584 int fd_dst = -1;
585 char mtd_path[MAX_PATH_LEN] = {0};
586 struct mtd_info_user meminfo = {0};
587 partition_mtd_info_t mtd_info = {0};
588
589 unsigned char *real_write_content = NULL;
590 T_FLAGS_INFO *p_write = NULL;
591
592 int index = 0;
593 int block_num = 0;
594 int already_write = 0;
595
596 flags_info.magic_start = FLAGS_MAGIC;
597
598 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
599 flags_info.boot_fota_flag.fota_status = 0;
600 flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
601 flags_info.boot_fota_flag.system.try_cnt = 0;
602 flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
603 flags_info.boot_fota_flag.system2.try_cnt = 0;
604
605 flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
606
607 flags_info.ubifs_status.fs_status = 0;
608
609 flags_info.magic_end = FLAGS_MAGIC;
610
611 ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
612 if (ret < 0)
613 {
614 flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
615
616 return -1;
617 }
618
619 if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
620 {
621 flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
622
623 return -1;
624 }
625
626 if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
627 {
628 flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
629
630 goto error;
631 }
632
633 mtd_info.parti_file_desc = fd_dst;
634 mtd_info.mtd_blocksize = meminfo.erasesize;
635 mtd_info.mtd_oobsize = meminfo.oobsize;
636 mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
637 mtd_info.mtd_pagesize = meminfo.writesize;
638 mtd_info.mtd_totalsize = meminfo.size;
639
640 block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
641
642 real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
643 if (NULL == real_write_content)
644 {
645 flags_err("malloc for block fail");
646
647 goto error;
648 }
649
650 memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
651 memcpy(real_write_content, (char *)(&flags_info), sizeof(T_FLAGS_INFO));
652
653 for (index = 0; index < block_num; index++)
654 {
655 if (already_write >= FLAGS_INIT_VALID_BLOCKS_NUM)
656 {
657 break;
658 }
659
660 ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
661 if (ret == 0)
662 {
663 already_write++;
664 flags_log("write init valid block num: %d", already_write);
665
666 continue;
667 }
668 else if (BAD_BLOCK == ret)
669 {
670 continue;
671 }
672 }
673
674 if (already_write >= 2)
675 {
676 flags_log("init system status success, alread write block: %d", already_write);
677 ret = 0;
678 }
679 else
680 {
681 flags_log("init system status fail, alread write block: %d", already_write);
682 ret = -1;
683 }
684
685 goto end;
686
687error:
688 ret = -1;
689
690end:
691 close(fd_dst);
692
693 if(NULL != real_write_content)
694 {
695 free(real_write_content);
696 real_write_content = NULL;
697 }
698
699 return ret;
700}
701
702
703int flags_get(T_FLAGS_INFO *p_flags_info)
704{
705 T_FLAGS_INFO main_flag = {0};
706 T_FLAGS_INFO backup_flag = {0};
707 int main_index = 0;
708 int backup_index = 1;
709
710 if (NULL == p_flags_info)
711 {
712 flags_err("invalid param NULL");
713
714 return -1;
715 }
716
717 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
718 {
719 flags_err("get flags info fail");
720
721 return -1;
722 }
723
724 if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
725 {
726 copy_flags_info(p_flags_info, &main_flag);
727
728 return 0;
729 }
730
731 if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
732 {
733 copy_flags_info(p_flags_info, &backup_flag);
734
735 return 0;
736 }
737
738 flags_err("do not find valid flags info");
739
740 return -1;
741}
742
743
744int flags_set(T_FLAGS_INFO *p_flags_info)
745{
746 T_FLAGS_INFO main_flag = {0};
747 T_FLAGS_INFO backup_flag = {0};
748 int main_index = 0;
749 int backup_index = 1;
750
751 if (NULL == p_flags_info)
752 {
753 flags_err("invalid param NULL");
754
755 return -1;
756 }
757
758 if ((FLAGS_MAGIC != p_flags_info->magic_start) || (FLAGS_MAGIC != p_flags_info->magic_end))
759 {
760 flags_err("invalid magic");
761
762 return -1;
763 }
764
765 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
766 {
767 flags_err("get flags info fail");
768
769 return -1;
770 }
771
772 if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)
773 {
774 flags_err("set ubifs status fail");
775
776 return -1;
777 }
778
779 return 0;
780}
781
782
783int flags_get_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
784{
785 T_FLAGS_INFO main_flag = {0};
786 T_FLAGS_INFO backup_flag = {0};
787 int main_index = 0;
788 int backup_index = 1;
789
790 if (NULL == p_ubifs_status)
791 {
792 flags_err("invalid param NULL");
793
794 return -1;
795 }
796
797 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
798 {
799 flags_err("get flags info fail");
800
801 return -1;
802 }
803
804 if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
805 {
806 p_ubifs_status->fs_status = main_flag.ubifs_status.fs_status;
807 strncpy(p_ubifs_status->fs_mtd_name, main_flag.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
808 strncpy(p_ubifs_status->fs_ubi_vol_name, main_flag.ubifs_status.fs_ubi_vol_name, sizeof(p_ubifs_status->fs_ubi_vol_name));
809
810 return 0;
811 }
812
813 if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
814 {
815 p_ubifs_status->fs_status = backup_flag.ubifs_status.fs_status;
816 strncpy(p_ubifs_status->fs_mtd_name, backup_flag.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
817 strncpy(p_ubifs_status->fs_ubi_vol_name, backup_flag.ubifs_status.fs_ubi_vol_name, sizeof(p_ubifs_status->fs_ubi_vol_name));
818
819 return 0;
820 }
821
822 flags_err("do not find valid flags info");
823
824 return -1;
825}
826
827
828int flags_set_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
829{
830 T_FLAGS_INFO main_flag = {0};
831 T_FLAGS_INFO backup_flag = {0};
832 int main_index = 0;
833 int backup_index = 1;
834
835 T_FLAGS_INFO *flags_info = NULL;
836
837 if (NULL == p_ubifs_status)
838 {
839 flags_err("invalid param NULL");
840
841 return -1;
842 }
843
844 if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
845 {
846 flags_err("get flags info fail");
847
848 return -1;
849 }
850
851 if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
852 {
853 flags_info = &main_flag;
854 }
855 else if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
856 {
857 flags_info = &backup_flag;
858 }
859 else
860 {
861 flags_err("get ubifs status invalid");
862
863 return -1;
864 }
865
866 memcpy(&(flags_info->ubifs_status), p_ubifs_status, sizeof(T_UBIFS_STATUS));
867
868 if (set_flags_info(flags_info, &main_index, &backup_index) != 0)
869 {
870 flags_err("set ubifs status fail");
871
872 return -1;
873 }
874
875 return 0;
876}
877
878
879int flags_get_current_system()
880{
881 int current = get_current_system();
882
883 if (current == 1)
884 {
885 return DUAL_SYSTEM;
886 }
887 else if (current == 2)
888 {
889 return DUAL_SYSTEM2;
890 }
891
892 return -1;
893}
894