[Feature][ZXW-179]merge P52U02 version

Only Configure: No
Affected branch: master
Affected module: unknow
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No

Change-Id: I4fa8f86757e71388ae88400914dae8b50cd00338
diff --git a/ap/app/flags_tool/src/main.c b/ap/app/flags_tool/src/main.c
index 7e64a78..e035ab9 100755
--- a/ap/app/flags_tool/src/main.c
+++ b/ap/app/flags_tool/src/main.c
@@ -56,17 +56,21 @@
 static int excute_command_init(char *option_para);
 static int excute_command_get(char *option_para);
 static int excute_command_set(char *option_para);
+static int excute_command_switch(char *option_para);
 static int excute_command_get_ubifs_status(char *option_para);
 static int excute_command_set_ubifs_status(char *option_para);
 
 static int excute_command_kernel_get(char *option_para);
 static int excute_command_kernel_set(char *option_para);
 
+static int excute_command_get_nocrc(char *option_para);
+static int excute_command_set_nocrc(char *option_para);
+
 
 /*******************************************************************************
  *                       Global variable declarations                          *
  ******************************************************************************/
-static char * g_short_string = "higsubtq";
+static char * g_short_string = "higswubtqac:";
 
 static struct option g_long_options[] =
 {
@@ -74,10 +78,13 @@
     {"init",				no_argument,	NULL,	'i'},
     {"get",					no_argument,	NULL,	'g'},
     {"set",					no_argument,	NULL,	's'},
+    {"switch",				no_argument,	NULL,	'w'},
     {"get-ubifs-status",	no_argument,	NULL,	'u'},
     {"set-ubifs-status",	no_argument,	NULL,	'b'},
     {"kernel-get",			no_argument,	NULL,	't'},
     {"kernel-set",			no_argument,	NULL,	'q'},
+    {"get-nocrc",			no_argument,	NULL,	'a'},
+    {"set-nocrc",			required_argument,	NULL,	'c'},
     {0, 0, 0, 0}
 };
 
@@ -87,10 +94,13 @@
     {'i',		excute_command_init},
     {'g',		excute_command_get},
     {'s',		excute_command_set},
+    {'w',		excute_command_switch},
     {'u',		excute_command_get_ubifs_status},
     {'b',		excute_command_set_ubifs_status},
     {'t',		excute_command_kernel_get},
-    {'q',		excute_command_kernel_set}
+    {'q',		excute_command_kernel_set},
+    {'a',		excute_command_get_nocrc},
+    {'c',		excute_command_set_nocrc}
 };
 
 
@@ -99,15 +109,18 @@
  ******************************************************************************/
 static void usage(void)
 {
-    printf("flags_tool [-higsub] \n "
+    printf("flags_tool [-higswub] \n "
 		" -h,	--help                  show usage \n"
 		"  -i,	--init                  init flags \n"
 		"  -g,	--get                   get flags data \n"
 		"  -s,	--set                   set flags data \n"
+		"  -w,	--switch                system A/B switch \n"
 		"  -u,	--get-ubifs-status      get ubifs status \n"
 		"  -b,	--set-ubifs-status      set ubifs status \n"
 		"  -t,	--kernel-get            kernel-get flags data \n"
-		"  -q,	--kernel-set            kernel-set flags data \n");
+		"  -q,	--kernel-set            kernel-set flags data \n"
+		"  -a,	--get-nocrc             get-nocrc flags data \n"
+		"  -c,	--set-nocrc             set-nocrc flags data \n");
 
 	return;
 }
@@ -140,6 +153,7 @@
 
 static int excute_command_get(char *option_para)
 {
+	int i = 0;
 	T_FLAGS_INFO flags_info = {0};
 
 	if (flags_get(&flags_info) != 0)
@@ -167,6 +181,15 @@
 		printf("ubifs_status.fs_mtd_name        = %s \n",	flags_info.ubifs_status.fs_mtd_name);
 		printf("ubifs_status.fs_ubi_vol_name    = %s \n",	flags_info.ubifs_status.fs_ubi_vol_name);
 		
+		printf("nvro_flag                       = %u \n",	flags_info.nvro_flag);
+
+		printf("ota_system                      = %d \n",   flags_info.ota_system);
+
+		for (i = 0; i < OTA_PARTITION_NUM_MAX; i++)
+		{
+			printf("index=%d, mtdnum=%d, len=%u          \n", i, flags_info.ota_partiton_info[i].mtdnum, flags_info.ota_partiton_info[i].len);
+		}
+		
 		printf("magic_end                       = 0x%x \n",	flags_info.magic_end);
 		
 	    return 0;
@@ -217,6 +240,37 @@
 	}
 }
 
+static int excute_command_switch(char *option_para)
+{
+	T_FLAGS_INFO flags_info = {0};
+	int ret = -1;
+
+	ret = flags_get(&flags_info);
+	if (ret < 0)
+	{
+		printf("flags switch flags_get fail\n");
+		return ret;
+	}
+	if (flags_info.boot_fota_flag.boot_to == DUAL_SYSTEM)
+	{
+		flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM2;  //A->B
+		printf("flags switch system A to system B\n");
+	}
+	else
+	{
+		flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;   //B->A
+		printf("flags switch system B to system A\n");
+	}
+	ret = flags_set(&flags_info);
+	if (ret < 0)
+	{
+		printf("flags switch flags_set fail\n");
+		return ret;
+	}
+	printf("flags AB system switch sucess\n");
+
+	return 0;
+}
 
 static int excute_command_get_ubifs_status(char *option_para)
 {
@@ -267,6 +321,7 @@
 
 static int excute_command_kernel_get(char *option_para)
 {
+	int i = 0;
 	int fd = -1;
 	T_FLAGS_INFO flags_info = {0};
 
@@ -302,6 +357,15 @@
 	printf("ubifs_status.fs_mtd_name        = %s \n",	flags_info.ubifs_status.fs_mtd_name);
 	printf("ubifs_status.fs_ubi_vol_name    = %s \n",	flags_info.ubifs_status.fs_ubi_vol_name);
 	
+	printf("nvro_flag                       = %u \n",	flags_info.nvro_flag);
+
+	printf("ota_system                      = %d \n",   flags_info.ota_system);
+
+	for (i = 0; i < OTA_PARTITION_NUM_MAX; i++)
+	{
+		printf("index=%d, mtdnum=%d, len=%u          \n", i, flags_info.ota_partiton_info[i].mtdnum, flags_info.ota_partiton_info[i].len);
+	}
+
 	printf("magic_end                       = 0x%x \n",	flags_info.magic_end);
 	
 	close(fd);
@@ -361,6 +425,141 @@
 }
 
 
+static int excute_command_get_nocrc(char *option_para)
+{
+	int i = 0;
+	T_FLAGS_INFO flags_info = {0};
+
+	if (flags_get_nocrc(&flags_info) != 0)
+	{
+		printf("Err: flags get fail \n");
+		return -1;
+	}
+	else
+	{
+		printf("magic_start                     = 0x%x \n",	flags_info.magic_start);
+		
+		printf("boot_fota_flag.boot_to          = 0x%x \n",	flags_info.boot_fota_flag.boot_to);
+		printf("boot_fota_flag.fota_status      = %u \n",	flags_info.boot_fota_flag.fota_status);
+		printf("boot_fota_flag.system.status    = 0x%x \n",	flags_info.boot_fota_flag.system.status);
+		printf("boot_fota_flag.system.try_cnt	= %d \n",	flags_info.boot_fota_flag.system.try_cnt);
+		printf("boot_fota_flag.system2.status	= 0x%x \n",	flags_info.boot_fota_flag.system2.status);
+		printf("boot_fota_flag.system2.try_cnt	= %d \n",	flags_info.boot_fota_flag.system2.try_cnt);
+		
+		printf("boot_env.dualsys_type           = 0x%x \n",	flags_info.boot_env.dualsys_type);
+		printf("boot_env.system_boot_env        = %s \n",	flags_info.boot_env.system_boot_env);
+		printf("boot_env.system2_boot_env       = %s \n",	flags_info.boot_env.system2_boot_env);
+		
+		printf("ubifs_status.fs_status          = %d \n",	flags_info.ubifs_status.fs_status);
+		printf("ubifs_status.fs_mtd_name        = %s \n",	flags_info.ubifs_status.fs_mtd_name);
+		printf("ubifs_status.fs_ubi_vol_name    = %s \n",	flags_info.ubifs_status.fs_ubi_vol_name);
+		
+		printf("nvro_flag                       = %u \n",	flags_info.nvro_flag);
+		printf("crc32                           = %u \n",	flags_info.crc32);
+		
+		printf("ota_system                      = %d \n",   flags_info.ota_system);
+
+		for (i = 0; i < OTA_PARTITION_NUM_MAX; i++)
+		{
+			printf("index=%d, mtdnum=%d, len=%u          \n", i, flags_info.ota_partiton_info[i].mtdnum, flags_info.ota_partiton_info[i].len);
+		}
+	
+		printf("magic_end                       = 0x%x \n",	flags_info.magic_end);
+		
+	    return 0;
+	}
+}
+
+
+static int excute_command_set_nocrc(char *option_para)
+{
+	T_FLAGS_INFO flags_info = {0};
+	
+	const char delim[2] = ",";
+	char *token_1 = NULL;
+	char *token_2 = NULL;
+	char *token_3 = NULL;
+	char *token_4 = NULL;
+	char *token_5 = NULL;
+	char *token_6 = NULL;
+	
+    if (NULL == option_para)
+    {
+        usage();
+        printf("Command input invalid value! null option parameters! \n");
+        return -1;
+    }
+	
+	if (flags_get_nocrc(&flags_info) != 0)
+	{
+		printf("Err: flags get fail \n");
+		return -1;
+	}
+
+	token_1 = strtok(option_para, delim);
+	if (NULL == token_1)
+	{
+		printf("token_1 delim fail: NULL \n");
+		return -1;
+	}
+	
+	token_2 = strtok(NULL, delim);
+	if (NULL == token_2)
+	{
+		printf("token_2 delim fail: NULL \n");
+		return -1;
+	}
+	
+	token_3 = strtok(NULL, delim);
+	if (NULL == token_3)
+	{
+		printf("token_3 delim fail: NULL \n");
+		return -1;
+	}
+	
+	token_4 = strtok(NULL, delim);
+	if (NULL == token_4)
+	{
+		printf("token_4 delim fail: NULL \n");
+		return -1;
+	}
+	
+	token_5 = strtok(NULL, delim);
+	if (NULL == token_5)
+	{
+		printf("token_5 delim fail: NULL \n");
+		return -1;
+	}
+
+	token_6 = strtok(NULL, delim);
+	if (NULL != token_6)
+	{
+		printf("too many command input, fail \n");
+		return -1;
+	}
+
+    flags_info.boot_fota_flag.system2.try_cnt = atoi(token_1);
+
+	strncpy(flags_info.boot_env.system_boot_env, token_2, sizeof(flags_info.boot_env.system_boot_env));
+
+	flags_info.ubifs_status.fs_status = atoi(token_3);
+	strncpy(flags_info.ubifs_status.fs_mtd_name, token_4, sizeof(flags_info.ubifs_status.fs_mtd_name));
+	
+    flags_info.magic_end = atoi(token_5);
+
+	if (flags_set_nocrc(&flags_info) != 0)
+	{
+		printf("Err: set flags fail \n");
+		return -1;
+	}
+	else
+	{
+		printf("Log: set flags success \n");
+		return 0;
+	}
+}
+
+
 /*******************************************************************************
  *                       Global function declarations                          *
  ******************************************************************************/