[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/lib/libflags/flags_api.c b/ap/lib/libflags/flags_api.c
index 151b5b3..ca52fc2 100755
--- a/ap/lib/libflags/flags_api.c
+++ b/ap/lib/libflags/flags_api.c
@@ -52,6 +52,11 @@
 #define SYSTEM_INDEX_1 (1)

 #define SYSTEM_INDEX_2 (2)

 

+#define CRC_LE_BITS 64

+#define CRC32_POLY_LE 0xedb88320

+#define LE_TABLE_ROWS (CRC_LE_BITS/8)

+#define LE_TABLE_SIZE 256

+

 

 /*******************************************************************************

  *                             Type definitions                                *

@@ -83,11 +88,16 @@
 /*******************************************************************************

  *						  Global variable definitions						   *

  ******************************************************************************/

+static unsigned int crc32table_le[LE_TABLE_ROWS][256];

 

 

 /*******************************************************************************

  * 					   Local function declarations							   *

  ******************************************************************************/

+static void crc32init_le_generic(const unsigned int polynomial, unsigned int (*tab)[256]);

+static unsigned int crc32_body(unsigned int crc, unsigned char const *buf, size_t len, const unsigned int (*tab)[256]);

+static unsigned int crc32_le_generic(unsigned int crc, unsigned char const *p, size_t len, const unsigned int (*tab)[256]);

+

 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);

 

@@ -102,6 +112,87 @@
 /*******************************************************************************

  * 					 Local function implementations 						   *

  ******************************************************************************/

+static void crc32init_le_generic(const unsigned int polynomial, unsigned int (*tab)[256])

+{

+	unsigned i, j;

+	unsigned int crc = 1;

+

+	tab[0][0] = 0;

+

+	for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {

+		crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);

+		for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)

+			tab[0][i + j] = crc ^ tab[0][j];

+	}

+	for (i = 0; i < LE_TABLE_SIZE; i++) {

+		crc = tab[0][i];

+		for (j = 1; j < LE_TABLE_ROWS; j++) {

+			crc = tab[0][crc & 0xff] ^ (crc >> 8);

+			tab[j][i] = crc;

+		}

+	}

+}

+

+

+static unsigned int crc32_body(unsigned int crc, unsigned char const *buf, size_t len, const unsigned int (*tab)[256])

+{

+#define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)

+#define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \

+		   t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])

+#define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \

+		   t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])

+

+	const unsigned int *b;

+	size_t    rem_len;

+

+	const unsigned int *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];

+	const unsigned int *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];

+	unsigned int q;

+	

+	if ((long)buf & 3 && len)

+	{

+		do

+		{

+			DO_CRC(*buf++);

+		} while ((--len) && ((long)buf)&3);

+	}

+	

+	rem_len = len & 7;

+	len = len >> 3;

+	b = (const unsigned int *)buf;

+	

+	for (--b; len; --len)

+	{

+		q = crc ^ *++b;

+		crc = DO_CRC8;

+		q = *++b;

+		crc ^= DO_CRC4;

+	}

+	

+	len = rem_len;

+	if (len)

+	{

+		unsigned char *p = (unsigned char *)(b + 1) - 1;

+		do

+		{

+			DO_CRC(*++p); /* use pre increment for speed */

+		} while (--len);

+	}

+	

+	return crc;

+#undef DO_CRC

+#undef DO_CRC4

+#undef DO_CRC8

+}

+

+

+static unsigned int crc32_le_generic(unsigned int crc, unsigned char const *p, size_t len, const unsigned int (*tab)[256])

+{

+	crc = crc32_body(crc, p, len, tab);

+	return crc;

+}

+

+

 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;

@@ -484,26 +575,7 @@
 

 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->nvro_flag = src->nvro_flag;

-

-	dst->magic_end = src->magic_end;

+	memcpy(dst, src, sizeof(T_FLAGS_INFO));

 

 	return;

 }

@@ -525,7 +597,7 @@
     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));

+        printf("Open file %s error, error:%s", FILE_PATH_PROC_CMDLINE, strerror(errno));

         return SYSTEM_INDEX_UNKNOWN;

     }

 

@@ -536,16 +608,16 @@
 

         if (NULL == line_str)

         {

-            flags_err("get info from /proc/cmdline error:%s", strerror(errno));

+            printf("get info from /proc/cmdline error:%s", strerror(errno));

             goto end;

         }

 

-        flags_log("buff:%s", buf);

+        printf("buff:%s", buf);

 

         para = strtok(buf, " ");

         while (para)

         {

-            flags_log("para:%s", para);

+            printf("para:%s", para);

             if (strncmp(para, PROC_CMDLINE_SYSTEM_A_FLAG, strlen(PROC_CMDLINE_SYSTEM_A_FLAG)) == 0)

             {

                 current_system = SYSTEM_INDEX_1;

@@ -595,6 +667,8 @@
 	int block_num = 0;

 	int already_write = 0;

 	

+	unsigned int crc_32 = 0;

+	

     flags_info.magic_start = FLAGS_MAGIC;

 	

     flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;

@@ -610,6 +684,12 @@
 	

     flags_info.magic_end = FLAGS_MAGIC;

 

+	crc32init_le();

+	crc_32 = crc32_le(0, (unsigned char const *)(&flags_info), sizeof(flags_info));

+	flags_log("init crc_32=%u", crc_32);

+

+	flags_info.crc32 = crc_32;

+

 	ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);

 	if (ret < 0)

 	{

@@ -706,9 +786,17 @@
 {

     T_FLAGS_INFO main_flag = {0};

     T_FLAGS_INFO backup_flag = {0};

+	T_FLAGS_INFO p_flags_info_tmp = {0};

+

     int main_index = 0;

     int backup_index = 1;

 

+	unsigned int crc32_main = 0;

+	unsigned int crc32_backup = 0;

+	

+	unsigned int crc32_le_main = 0;

+	unsigned int crc32_le_backup = 0;

+

 	if (NULL == p_flags_info)

 	{

 		flags_err("invalid param NULL");

@@ -722,15 +810,56 @@
 		

         return -1;

     }

+	

+	flags_log("main_flag crc32=%u", main_flag.crc32);

+	flags_log("backup_flag crc32=%u", backup_flag.crc32);

+	

+	if ((0 == main_flag.crc32) && (0 == backup_flag.crc32))

+	{

+		if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))

+		{

+			if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))

+			{

+				memcpy(&p_flags_info_tmp, &main_flag, sizeof(T_FLAGS_INFO));

+				crc32init_le();

+				p_flags_info_tmp.crc32 = 0;

+				p_flags_info_tmp.crc32 = crc32_le(0, (unsigned char const *)(&p_flags_info_tmp), sizeof(T_FLAGS_INFO));

+				flags_log("old version, set crc32=%u", p_flags_info_tmp.crc32);

+				

+				if (set_flags_info(&p_flags_info_tmp, &main_index, &backup_index) != 0)

+				{

+					flags_err("old version, set flags info fail");

+					return -1;

+				}

+				

+				copy_flags_info(p_flags_info, &main_flag);

+				return 0;

+			}

+		}

+	}

 

-    if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))

+	crc32_main = main_flag.crc32;

+	crc32_backup = backup_flag.crc32;

+

+	main_flag.crc32 = 0;

+	backup_flag.crc32 = 0;

+	

+	crc32init_le();

+	

+	crc32_le_main = crc32_le(0, (unsigned char const *)(&main_flag), sizeof(main_flag));

+	flags_log("crc32_le_main crc32=%u", crc32_le_main);

+	

+	crc32_le_backup = crc32_le(0, (unsigned char const *)(&backup_flag), sizeof(backup_flag));

+	flags_log("crc32_le_backup crc32=%u", crc32_le_backup);

+

+    if (crc32_main == crc32_le_main)

     {

         copy_flags_info(p_flags_info, &main_flag);

 		

         return 0;

     }

 	

-    if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))

+    if (crc32_backup == crc32_le_backup)

     {

         copy_flags_info(p_flags_info, &backup_flag);

 		

@@ -771,6 +900,11 @@
         return -1;

     }

 

+	crc32init_le();

+	p_flags_info->crc32 = 0;

+	p_flags_info->crc32 = crc32_le(0, (unsigned char const *)p_flags_info, sizeof(T_FLAGS_INFO));

+	flags_log("set crc32=%u", p_flags_info->crc32);

+

     if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)

     {

         flags_err("set ubifs status fail");

@@ -784,99 +918,58 @@
 

 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;

+	T_FLAGS_INFO p_flags_info = {0};

 	

 	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");

+	if (0 == flags_get(&p_flags_info))

+	{

+		p_ubifs_status->fs_status = p_flags_info.ubifs_status.fs_status;

+		strncpy(p_ubifs_status->fs_mtd_name, p_flags_info.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));

+		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));

 		

-        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;

+		return 0;

+	}

+	else

+	{

+	    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 p_flags_info = {0};

 	

-    T_FLAGS_INFO *flags_info = NULL;

-

 	if (NULL == p_ubifs_status)

 	{

 		flags_err("invalid param NULL");

-		

+		return -1;

+	}

+

+	if (0 != flags_get(&p_flags_info))

+	{

+		flags_err("get ubifs status invalid");

 		return -1;

 	}

 	

-    if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)

-    {

-    	flags_err("get flags info fail");

-		

-        return -1;

-    }

+	memcpy(&(p_flags_info.ubifs_status), p_ubifs_status, sizeof(T_UBIFS_STATUS));

 

-    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;

-    }

-

+	if (0 != flags_set(&p_flags_info))

+	{

+		flags_err("set ubifs status fail");

+		return -1;

+	}

+	

     return 0;

 }

 

+

 unsigned int flags_get_nvroflag(void)

 {

 	T_FLAGS_INFO t_flag = {0};

@@ -886,6 +979,7 @@
 	return t_flag.nvro_flag;

 }

 

+

 int flags_set_nvroflag(unsigned int flag)

 {

 	T_FLAGS_INFO t_flag = {0};

@@ -907,6 +1001,7 @@
 	return flags_set(&t_flag);

 }

 

+

 int flags_get_current_system()

 {

     int current = get_current_system();

@@ -923,3 +1018,84 @@
     return -1;

 }

 

+

+/* ´ËAPI½öÓÃÓÚµ÷²â£¬Õýʽ´úÂë²»¿ÉʹÓà */

+int flags_get_nocrc(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 (0 != memcmp(&main_flag, &backup_flag, sizeof(T_FLAGS_INFO)))

+	{

+	    flags_err("main flag and backup flag are different");

+        return -1;

+	}

+	

+	copy_flags_info(p_flags_info, &main_flag);

+	p_flags_info->crc32 = main_flag.crc32;

+

+    return 0;

+}

+

+

+/* ´ËAPI½öÓÃÓÚµ÷²â£¬Õýʽ´úÂë²»¿ÉʹÓà */

+int flags_set_nocrc(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;

+}

+

+

+void crc32init_le(void)

+{

+	crc32init_le_generic(CRC32_POLY_LE, crc32table_le);

+}

+

+

+unsigned int crc32_le(unsigned int crc, unsigned char const *p, size_t len)

+{

+	return crc32_le_generic(crc, p, len, (const unsigned int (*)[256])crc32table_le);

+}

+