[Feature][ZXW-88]merge P50 version
Only Configure: No
Affected branch: master
Affected module: unknown
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No
Change-Id: I34667719d9e0e7e29e8e4368848601cde0a48408
diff --git a/ap/lib/libflags/Makefile b/ap/lib/libflags/Makefile
new file mode 100755
index 0000000..557d792
--- /dev/null
+++ b/ap/lib/libflags/Makefile
@@ -0,0 +1,37 @@
+#*******************************************************************************
+# include ZTE library makefile
+#*******************************************************************************
+include $(COMMON_MK)
+
+##############USER COMIZE BEGIN################
+
+LIB_STATIC = libflags.a
+LIB_SHARED = libflags.so
+
+OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
+
+#CFLAGS += -I$(zte_app_path)/at_ctl/inc
+#CFLAGS += -I$(zte_lib_path)/libatutils
+CFLAGS += -I$(LINUX_DIR)/include
+CFLAGS += -g
+
+
+##############USER COMIZE END##################
+
+CFLAGS += -I$(zte_app_path)/include
+CFLAGS += -fPIC
+LDFLAGS += -shared
+
+all: $(LIB_STATIC) $(LIB_SHARED)
+
+$(LIB_STATIC) : $(OBJS)
+ $(AR) rcs $(LIB_STATIC) $(OBJS)
+
+$(LIB_SHARED): $(OBJS)
+ $(CC) $(LDFLAGS) -o $@ $^
+
+romfs:
+ $(ROMFSINST) $(LIB_SHARED) /lib/$(LIB_SHARED)
+
+clean:
+ -$(RM) *.a *.o *.so $(LIB_SHARED) $(LIB_STATIC) $(OBJS)
diff --git a/ap/lib/libflags/flags_api.c b/ap/lib/libflags/flags_api.c
new file mode 100755
index 0000000..37653fc
--- /dev/null
+++ b/ap/lib/libflags/flags_api.c
@@ -0,0 +1,894 @@
+/**
+ * @file flags_api.c
+ * @brief flags·ÖÇø½Ó¿ÚʵÏÖ
+ *
+ * Copyright (C) 2023 Sanechips Technology Co., Ltd.
+ * @author
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation. £¨±ØÑ¡£ºGPLv2 Licence£©
+ *
+ */
+
+
+/*******************************************************************************
+ * Include header files *
+ ******************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <mtd/mtd-abi.h>
+
+#include "pub_flags.h"
+
+#include "flags_log.h"
+#include "flags_api.h"
+
+
+/*******************************************************************************
+ * Macro definitions *
+ ******************************************************************************/
+#define MAX_PATH_LEN (256)
+
+#define PARTITION_NAME_FLAGS "flags"
+
+#define FLAGS_INIT_VALID_BLOCKS_NUM (8)
+
+#define GOOD_BLOCK (0)
+#define BAD_BLOCK (1)
+
+#define FILE_PATH_PROC_CMDLINE "/proc/cmdline"
+
+#define PROC_CMDLINE_SYSTEM_A_FLAG "system=system_a"
+#define PROC_CMDLINE_SYSTEM_B_FLAG "system=system_b"
+
+#define SYSTEM_INDEX_UNKNOWN (-1)
+#define SYSTEM_INDEX_1 (1)
+#define SYSTEM_INDEX_2 (2)
+
+
+/*******************************************************************************
+ * Type definitions *
+ ******************************************************************************/
+typedef struct
+{
+ unsigned int mtd_totalsize; // mtd device total size
+ unsigned int mtd_pageperblock; // mtd device page per block
+ unsigned int mtd_blocksize; // mtd device block size
+ unsigned int mtd_pagesize; // mtd device page size
+ unsigned int mtd_oobsize; // mtd device oob size
+ int parti_file_desc; // partition update file description
+} partition_mtd_info_t;
+
+
+typedef enum
+{
+ DEVICE_MTD = 0,
+ DEVICE_ZFTL = 1,
+ DEVICE_MTD_BLOCK,
+} device_type_t;
+
+
+/*******************************************************************************
+ * Local variable definitions *
+ ******************************************************************************/
+
+
+/*******************************************************************************
+ * Global variable definitions *
+ ******************************************************************************/
+
+
+/*******************************************************************************
+ * Local function declarations *
+ ******************************************************************************/
+static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len);
+static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len);
+
+static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index);
+static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index);
+
+static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src);
+
+static int get_current_system();
+
+
+/*******************************************************************************
+ * Local function implementations *
+ ******************************************************************************/
+static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len)
+{
+ FILE *fp_mtd = 0;
+ char buf[128];
+ char *line_str;
+
+ if (!o_mtd_path_len)
+ {
+ return -1;
+ }
+
+ fp_mtd = fopen("/proc/mtd", "r+");
+ if (NULL == fp_mtd)
+ {
+ flags_err("open file error: %s", strerror(errno));
+ return -1;
+ }
+ // printf("[libmtd]: partition name:%s\n", i_parti_name);
+
+ while (1)
+ {
+ int matches = 0;
+ char mtdname[64] = {0};
+ int mtdnum = 0;
+ unsigned int mtdsize, mtderasesize;
+ memset(buf, 0, sizeof(buf));
+ line_str = fgets(buf, sizeof(buf) - 1, fp_mtd);
+
+ if (NULL == line_str)
+ {
+ flags_err("get info from mtd error: %s", strerror(errno));
+ fclose(fp_mtd);
+ return -1;
+ }
+ // mtd5: 00100000 00020000 "fotaflag"
+ matches = sscanf(buf, "mtd%d: %x %x \"%63[^\"]",
+ &mtdnum, &mtdsize, &mtderasesize, mtdname);
+ mtdname[63] = '\0';
+
+ if ((matches == 4) && (strcmp(mtdname, i_parti_name) == 0))
+ {
+ memset(o_mtd_path, 0, o_mtd_path_len);
+ if (device_type == DEVICE_MTD_BLOCK)
+ {
+ snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtdblock%d", mtdnum);
+ }
+ else if (device_type == DEVICE_MTD)
+ {
+ snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtd%d", mtdnum);
+ }
+ else if (device_type == DEVICE_ZFTL)
+ {
+ snprintf(o_mtd_path, o_mtd_path_len, "/dev/zftl%d", mtdnum);
+ }
+ else
+ {
+ flags_err("unknown device type %d", device_type);
+ fclose(fp_mtd);
+ return -1;
+ }
+ // printf("[libmtd]: o_mtd_path=[%s]\n", o_mtd_path);
+ break;
+ }
+ }
+ fclose(fp_mtd);
+ return 0;
+}
+
+
+static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len)
+{
+ struct erase_info_user erase_info;
+ int write_len = 0;
+ long long offs = 0;
+
+ erase_info.start = index * p_mtd_info->mtd_blocksize;
+ erase_info.length = p_mtd_info->mtd_blocksize;
+
+ offs = (long long)index * p_mtd_info->mtd_blocksize;
+
+ if (ioctl(p_mtd_info->parti_file_desc, WRITEENABLE, 0) != 0)
+ {
+ flags_err("failed to enable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
+
+ return -1;
+ }
+
+ if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
+ {
+ flags_err("ioctl [MEMGETBADBLOCK] block: %d, change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
+
+ return BAD_BLOCK;
+ }
+
+ if (ioctl(p_mtd_info->parti_file_desc, MEMERASE, &erase_info) < 0)
+ {
+ flags_err("ioctl [MEMERASE] block: %d fail, errno=%d, strerror=[%s]", index, errno, strerror(errno));
+
+ return -1;
+ }
+
+ if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
+ {
+ flags_err("ioctl [MEMGETBADBLOCK] block:%d , change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
+
+ return BAD_BLOCK;
+ }
+
+ if (lseek(p_mtd_info->parti_file_desc, index * p_mtd_info->mtd_blocksize, SEEK_SET) < 0)
+ {
+ flags_err("lseek fail, errno=%d, strerror=[%s]", errno, strerror(errno));
+
+ return -1;
+ }
+
+ write_len = write(p_mtd_info->parti_file_desc, content, p_mtd_info->mtd_blocksize);
+
+ if (write_len != p_mtd_info->mtd_blocksize)
+ {
+ flags_err("write flash fail, errno=%d, strerror=[%s]", errno, strerror(errno));
+
+ return -1;
+ }
+
+ if (ioctl(p_mtd_info->parti_file_desc, WRITEDISABLE, 0) != 0)
+ {
+ flags_err("failed to disable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
+ }
+
+ return 0;
+}
+
+
+static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index)
+{
+ int ret = -1;
+ int fd_dst = -1;
+ char mtd_path[MAX_PATH_LEN] = {0};
+ struct mtd_info_user meminfo = {0};
+ partition_mtd_info_t mtd_info = {0};
+ int index = 0;
+ int block_num = 0;
+ int good_index = 0;
+ int read_len = 0;
+
+ long long offs = 0;
+
+ int block_flag = GOOD_BLOCK;
+
+ ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
+ if (ret < 0)
+ {
+ flags_err("partition [%s] not find", PARTITION_NAME_FLAGS);
+ goto error;
+ }
+
+ if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
+ {
+ flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
+ goto error;
+ }
+
+ mtd_info.parti_file_desc = fd_dst;
+
+ if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
+ {
+ flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
+ goto error_close;
+ }
+
+ mtd_info.mtd_blocksize = meminfo.erasesize;
+ mtd_info.mtd_oobsize = meminfo.oobsize;
+ mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
+ mtd_info.mtd_pagesize = meminfo.writesize;
+ mtd_info.mtd_totalsize = meminfo.size;
+
+ block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
+
+ for (index = 0; (good_index < 2 && index < block_num); index++)
+ {
+
+ offs = index * mtd_info.mtd_blocksize;
+ if (ioctl(mtd_info.parti_file_desc, MEMGETBADBLOCK, &offs) == 0)
+ {
+ block_flag = GOOD_BLOCK;
+ }
+ else
+ {
+ flags_err("flags block [%d] is bad, errno=%d, strerror=[%s]", index, errno, strerror(errno));
+ block_flag = BAD_BLOCK;
+ }
+
+ if (block_flag == GOOD_BLOCK)
+ {
+ if (lseek(mtd_info.parti_file_desc, index * mtd_info.mtd_blocksize, SEEK_SET) < 0)
+ {
+ flags_err("lseek error, errno=%d, strerror=[%s]", errno, strerror(errno));
+ goto error_close;
+ }
+
+ if (good_index == 0)
+ {
+ read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_main, sizeof(T_FLAGS_INFO));
+ *p_main_index = index;
+ }
+ else if (good_index == 1)
+ {
+ read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_backup, sizeof(T_FLAGS_INFO));
+ *p_backup_index = index;
+ }
+ else
+ {
+ break;
+ }
+
+ if (read_len < sizeof(T_FLAGS_INFO))
+ {
+ flags_err("read len (%d) < need len (%d)", read_len, sizeof(T_FLAGS_INFO));
+ goto error_close;
+ }
+
+ good_index++;
+ }
+
+ }
+
+ close(fd_dst);
+
+ return 0;
+
+error_close:
+ close(fd_dst);
+
+error:
+ return -1;
+}
+
+
+static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index)
+{
+ int ret = -1;
+ int fd_dst = -1;
+ char mtd_path[MAX_PATH_LEN] = {0};
+ struct mtd_info_user meminfo = {0};
+ partition_mtd_info_t mtd_info = {0};
+
+ unsigned char *real_write_content = NULL;
+
+ int index = 0;
+ int block_num = 0;
+ int main_index = *p_main_index;
+ int back_index = *p_backup_index;
+
+ ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
+ if (ret < 0)
+ {
+ flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
+
+ return -1;
+ }
+
+ if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
+ {
+ flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
+ return -1;
+ }
+
+ if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
+ {
+ flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
+ goto error;
+ }
+
+ mtd_info.parti_file_desc = fd_dst;
+ mtd_info.mtd_blocksize = meminfo.erasesize;
+ mtd_info.mtd_oobsize = meminfo.oobsize;
+ mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
+ mtd_info.mtd_pagesize = meminfo.writesize;
+ mtd_info.mtd_totalsize = meminfo.size;
+
+ block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
+
+ real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
+ if (NULL == real_write_content)
+ {
+ flags_err("malloc block fail");
+ goto error;
+ }
+
+ memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
+ memcpy(real_write_content, (char *)p_flags_info, sizeof(T_FLAGS_INFO));
+
+ flags_log("begin to write main flags");
+
+ for (index = 0; index < block_num; index++)
+ {
+ if (index == back_index)
+ {
+ continue;
+ }
+
+ ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
+ if (ret == 0)
+ {
+ // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
+ flags_log("main flags location: [%d]->[%d]", main_index, index);
+ main_index = index;
+ break;
+ }
+ else if (ret == BAD_BLOCK)
+ {
+ // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
+ flags_log("flags block index [%d] is bad", index);
+ continue;
+ }
+ else
+ {
+ flags_err("write main flags fail");
+ main_index = -1;
+ break;
+ }
+ }
+
+ flags_log("begin to write backup flags");
+
+ for (index = 0; index < block_num; index++)
+ {
+ if (index == main_index)
+ {
+ continue;
+ }
+
+ flags_log("write backup to [%d] block", index);
+ ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
+ if (ret == 0)
+ {
+ // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
+ flags_log("backup flags location: [%d]->[%d]", back_index, index);
+ back_index = index;
+ break;
+ }
+ else if (ret == BAD_BLOCK)
+ {
+ // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
+ continue;
+ }
+ else
+ {
+ flags_err("write backup flags fail");
+ back_index = -1;
+ break;
+ }
+ }
+
+ if (main_index == -1 && back_index == -1)
+ {
+ goto error;
+ }
+ else
+ {
+ ret = 0;
+ goto end;
+ }
+
+error:
+ ret = -1;
+
+end:
+ close(fd_dst);
+
+ if(NULL != real_write_content)
+ {
+ free(real_write_content);
+ real_write_content = NULL;
+ }
+
+ return ret;
+}
+
+
+static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src)
+{
+ dst->magic_start = src->magic_start;
+
+ dst->boot_fota_flag.boot_to = src->boot_fota_flag.boot_to;
+ dst->boot_fota_flag.fota_status = src->boot_fota_flag.fota_status;
+ dst->boot_fota_flag.system.status = src->boot_fota_flag.system.status;
+ dst->boot_fota_flag.system.try_cnt = src->boot_fota_flag.system.try_cnt;
+ dst->boot_fota_flag.system2.status = src->boot_fota_flag.system2.status;
+ dst->boot_fota_flag.system2.try_cnt = src->boot_fota_flag.system2.try_cnt;
+
+ dst->boot_env.dualsys_type = src->boot_env.dualsys_type;
+ strncpy(dst->boot_env.system_boot_env, src->boot_env.system_boot_env, sizeof(dst->boot_env.system_boot_env));
+ strncpy(dst->boot_env.system2_boot_env, src->boot_env.system2_boot_env, sizeof(dst->boot_env.system2_boot_env));
+
+ dst->ubifs_status.fs_status = src->ubifs_status.fs_status;
+ strncpy(dst->ubifs_status.fs_mtd_name, src->ubifs_status.fs_mtd_name, sizeof(dst->ubifs_status.fs_mtd_name));
+ strncpy(dst->ubifs_status.fs_ubi_vol_name, src->ubifs_status.fs_ubi_vol_name, sizeof(dst->ubifs_status.fs_ubi_vol_name));
+
+ dst->magic_end = src->magic_end;
+
+ return;
+}
+
+
+static int get_current_system()
+{
+ char buf[1024] = {0};
+
+ char *para = NULL;
+ int matches = 0;
+
+ FILE *fd_cmd = NULL;
+ char *line_str = NULL;
+
+ int current_system = -1;
+
+
+ fd_cmd = fopen(FILE_PATH_PROC_CMDLINE, "r");
+ if (!fd_cmd)
+ {
+ flags_err("open file %s error, error:%s", FILE_PATH_PROC_CMDLINE, strerror(errno));
+ return SYSTEM_INDEX_UNKNOWN;
+ }
+
+ while (!feof(fd_cmd))
+ {
+ memset(buf, 0, sizeof(buf));
+ line_str = fgets(buf, sizeof(buf), fd_cmd);
+
+ if (NULL == line_str)
+ {
+ flags_err("get info from /proc/cmdline error:%s", strerror(errno));
+ goto end;
+ }
+
+ flags_log("buff:%s", buf);
+
+ para = strtok(buf, " ");
+ while (para)
+ {
+ flags_log("para:%s", para);
+ if (strncmp(para, PROC_CMDLINE_SYSTEM_A_FLAG, strlen(PROC_CMDLINE_SYSTEM_A_FLAG)) == 0)
+ {
+ current_system = SYSTEM_INDEX_1;
+ goto end;
+ }
+ else if (strncmp(para, PROC_CMDLINE_SYSTEM_B_FLAG, strlen(PROC_CMDLINE_SYSTEM_B_FLAG)) == 0)
+ {
+ current_system = SYSTEM_INDEX_2;
+ goto end;
+ }
+ else
+ {
+ //:
+ }
+ para = strtok(NULL, " ");
+ }
+ }
+
+end:
+ if (fd_cmd)
+ {
+ fclose(fd_cmd);
+ }
+
+ return current_system;
+}
+
+
+/*******************************************************************************
+ * Global function implementations *
+ ******************************************************************************/
+int flags_init()
+{
+ T_FLAGS_INFO flags_info = {0};
+ int main_index = 0;
+ int back_index = 1;
+ int ret = -1;
+ int fd_dst = -1;
+ char mtd_path[MAX_PATH_LEN] = {0};
+ struct mtd_info_user meminfo = {0};
+ partition_mtd_info_t mtd_info = {0};
+
+ unsigned char *real_write_content = NULL;
+ T_FLAGS_INFO *p_write = NULL;
+
+ int index = 0;
+ int block_num = 0;
+ int already_write = 0;
+
+ flags_info.magic_start = FLAGS_MAGIC;
+
+ flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
+ flags_info.boot_fota_flag.fota_status = 0;
+ flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
+ flags_info.boot_fota_flag.system.try_cnt = 0;
+ flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
+ flags_info.boot_fota_flag.system2.try_cnt = 0;
+
+ flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
+
+ flags_info.ubifs_status.fs_status = 0;
+
+ flags_info.magic_end = FLAGS_MAGIC;
+
+ ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
+ if (ret < 0)
+ {
+ flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
+
+ return -1;
+ }
+
+ if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
+ {
+ flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
+
+ return -1;
+ }
+
+ if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
+ {
+ flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
+
+ goto error;
+ }
+
+ mtd_info.parti_file_desc = fd_dst;
+ mtd_info.mtd_blocksize = meminfo.erasesize;
+ mtd_info.mtd_oobsize = meminfo.oobsize;
+ mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
+ mtd_info.mtd_pagesize = meminfo.writesize;
+ mtd_info.mtd_totalsize = meminfo.size;
+
+ block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
+
+ real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
+ if (NULL == real_write_content)
+ {
+ flags_err("malloc for block fail");
+
+ goto error;
+ }
+
+ memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
+ memcpy(real_write_content, (char *)(&flags_info), sizeof(T_FLAGS_INFO));
+
+ for (index = 0; index < block_num; index++)
+ {
+ if (already_write >= FLAGS_INIT_VALID_BLOCKS_NUM)
+ {
+ break;
+ }
+
+ ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
+ if (ret == 0)
+ {
+ already_write++;
+ flags_log("write init valid block num: %d", already_write);
+
+ continue;
+ }
+ else if (BAD_BLOCK == ret)
+ {
+ continue;
+ }
+ }
+
+ if (already_write >= 2)
+ {
+ flags_log("init system status success, alread write block: %d", already_write);
+ ret = 0;
+ }
+ else
+ {
+ flags_log("init system status fail, alread write block: %d", already_write);
+ ret = -1;
+ }
+
+ goto end;
+
+error:
+ ret = -1;
+
+end:
+ close(fd_dst);
+
+ if(NULL != real_write_content)
+ {
+ free(real_write_content);
+ real_write_content = NULL;
+ }
+
+ return ret;
+}
+
+
+int flags_get(T_FLAGS_INFO *p_flags_info)
+{
+ T_FLAGS_INFO main_flag = {0};
+ T_FLAGS_INFO backup_flag = {0};
+ int main_index = 0;
+ int backup_index = 1;
+
+ if (NULL == p_flags_info)
+ {
+ flags_err("invalid param NULL");
+
+ return -1;
+ }
+
+ if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
+ {
+ flags_err("get flags info fail");
+
+ return -1;
+ }
+
+ if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
+ {
+ copy_flags_info(p_flags_info, &main_flag);
+
+ return 0;
+ }
+
+ if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
+ {
+ copy_flags_info(p_flags_info, &backup_flag);
+
+ return 0;
+ }
+
+ flags_err("do not find valid flags info");
+
+ return -1;
+}
+
+
+int flags_set(T_FLAGS_INFO *p_flags_info)
+{
+ T_FLAGS_INFO main_flag = {0};
+ T_FLAGS_INFO backup_flag = {0};
+ int main_index = 0;
+ int backup_index = 1;
+
+ if (NULL == p_flags_info)
+ {
+ flags_err("invalid param NULL");
+
+ return -1;
+ }
+
+ if ((FLAGS_MAGIC != p_flags_info->magic_start) || (FLAGS_MAGIC != p_flags_info->magic_end))
+ {
+ flags_err("invalid magic");
+
+ return -1;
+ }
+
+ if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
+ {
+ flags_err("get flags info fail");
+
+ return -1;
+ }
+
+ if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)
+ {
+ flags_err("set ubifs status fail");
+
+ return -1;
+ }
+
+ return 0;
+}
+
+
+int flags_get_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
+{
+ T_FLAGS_INFO main_flag = {0};
+ T_FLAGS_INFO backup_flag = {0};
+ int main_index = 0;
+ int backup_index = 1;
+
+ if (NULL == p_ubifs_status)
+ {
+ flags_err("invalid param NULL");
+
+ return -1;
+ }
+
+ if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
+ {
+ flags_err("get flags info fail");
+
+ return -1;
+ }
+
+ if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
+ {
+ p_ubifs_status->fs_status = main_flag.ubifs_status.fs_status;
+ strncpy(p_ubifs_status->fs_mtd_name, main_flag.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
+ strncpy(p_ubifs_status->fs_ubi_vol_name, main_flag.ubifs_status.fs_ubi_vol_name, sizeof(p_ubifs_status->fs_ubi_vol_name));
+
+ return 0;
+ }
+
+ if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
+ {
+ p_ubifs_status->fs_status = backup_flag.ubifs_status.fs_status;
+ strncpy(p_ubifs_status->fs_mtd_name, backup_flag.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
+ strncpy(p_ubifs_status->fs_ubi_vol_name, backup_flag.ubifs_status.fs_ubi_vol_name, sizeof(p_ubifs_status->fs_ubi_vol_name));
+
+ return 0;
+ }
+
+ flags_err("do not find valid flags info");
+
+ return -1;
+}
+
+
+int flags_set_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
+{
+ T_FLAGS_INFO main_flag = {0};
+ T_FLAGS_INFO backup_flag = {0};
+ int main_index = 0;
+ int backup_index = 1;
+
+ T_FLAGS_INFO *flags_info = NULL;
+
+ if (NULL == p_ubifs_status)
+ {
+ flags_err("invalid param NULL");
+
+ return -1;
+ }
+
+ if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
+ {
+ flags_err("get flags info fail");
+
+ return -1;
+ }
+
+ if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
+ {
+ flags_info = &main_flag;
+ }
+ else if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
+ {
+ flags_info = &backup_flag;
+ }
+ else
+ {
+ flags_err("get ubifs status invalid");
+
+ return -1;
+ }
+
+ memcpy(&(flags_info->ubifs_status), p_ubifs_status, sizeof(T_UBIFS_STATUS));
+
+ if (set_flags_info(flags_info, &main_index, &backup_index) != 0)
+ {
+ flags_err("set ubifs status fail");
+
+ return -1;
+ }
+
+ return 0;
+}
+
+
+int flags_get_current_system()
+{
+ int current = get_current_system();
+
+ if (current == 1)
+ {
+ return DUAL_SYSTEM;
+ }
+ else if (current == 2)
+ {
+ return DUAL_SYSTEM2;
+ }
+
+ return -1;
+}
+
diff --git a/ap/lib/libflags/flags_log.h b/ap/lib/libflags/flags_log.h
new file mode 100755
index 0000000..4c6bd46
--- /dev/null
+++ b/ap/lib/libflags/flags_log.h
@@ -0,0 +1,10 @@
+#ifndef FLAGS_LOG_H
+#define FLAGS_LOG_H
+
+
+#define flags_log(fmt, args...) printf("[flags] [%s-%d] Log: " fmt "\n", __FUNCTION__, __LINE__, ##args)
+#define flags_err(fmt, args...) printf("[flags] [%s-%d] Err: " fmt "\n", __FUNCTION__, __LINE__, ##args)
+
+
+#endif // FLAGS_LOG_H
+