[Feature][ZXW-265]merge P56U03 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: I873f6df64e2605a77b8b8bfec35b21e7f33c5444
diff --git a/ap/app/fs_check/mtd.c b/ap/app/fs_check/mtd.c
index 633b625..c2ef0e6 100755
--- a/ap/app/fs_check/mtd.c
+++ b/ap/app/fs_check/mtd.c
@@ -7,6 +7,7 @@
 #include <sys/stat.h>

 #include <fcntl.h>

 #include <sys/vfs.h>

+#include <assert.h>

 

 

 #include <mtd/mtd-abi.h>

@@ -198,8 +199,8 @@
 

     struct  stat st		= {0};

 	

-	unsigned char read_buf[BUF_MAX_LEN] = {0};

-    unsigned char ubidev_path[MAX_PATH] = {0};

+	char read_buf[BUF_MAX_LEN] = {0};

+	char ubidev_path[MAX_PATH] = {0};

 

 	for (; mytmp < UBI_DEV_MAX_NUM; mytmp++)

 	{

@@ -246,15 +247,18 @@
 *            p_file:  Îļþ

 *            len:    ³¤¶È

 *            (OUT)

-* ·µ »Ø Öµ£º³É¹¦·µ»Ø¶ÁÈ¡µÄÎļþ, ·ñÔò·µ»Ø0

+* ·µ »Ø Öµ Îļþ¶ÁÈ¡³É¹¦Çé¿ö£¨1¡¢·µ»Ø·ÇNULL£»2¡¢¿ÕÎļþ·µ»ØNULLÇÒlenµÈÓÚ0£©£¬

+          Îļþ¶Áȡʧ°ÜÇé¿ö£º·µ»ØNULL£¬ÇÒlen´óÓÚ0

 * ÆäËü˵Ã÷£º·µ»ØÖµµ÷ÓÃÕßÊÍ·Å

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

-static unsigned char *read_file(const char *p_file, unsigned int *len)

+static char *read_file(const char *p_file, unsigned int *len)

 {

 	FILE * fd  		= 0;

     struct stat buf = {0};

-	unsigned char * p_buf  = NULL;

+	char * p_buf  = NULL;

+	size_t read_size;

 

+	*len = 0xffff; /* init file length > 0 for error */

 	if(p_file == NULL)

 		return NULL;

 	

@@ -264,8 +268,14 @@
 		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file stat %s failed\n", p_file);

         return NULL;

     }

-	

-	fd = fopen(p_file, "ro");

+	if (buf.st_size == 0)

+	{

+		*len = 0; /* empty file */

+		return NULL;

+	}

+	*len = buf.st_size;

+

+	fd = fopen(p_file, "rb");

 	if(!fd) 

 	{

 		printf("read_file open %s fail\n", p_file);

@@ -282,7 +292,8 @@
 		return NULL;

 	}

 

-	if(fread(p_buf,1, buf.st_size,fd) < 1) 

+	read_size = fread(p_buf,1, buf.st_size,fd);

+	if (read_size != buf.st_size)

 	{

 		printf("read_file fread %s fail\n", p_file);

 		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file fread %s fail\n", p_file);

@@ -290,9 +301,17 @@
 		free(p_buf);

 		return NULL;

 	}

+	if (ferror(fd))

+	{

+		clearerr(fd);

+		printf("read_file ferror %s fail\n", p_file);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file ferror %s fail\n", p_file);

+		fclose(fd);

+		free(p_buf);

+		return NULL;

+	}

 	fclose(fd);

-	*len = buf.st_size;

-	

+

     return p_buf;

 }

 

@@ -308,48 +327,52 @@
 **************************************************************************/

 static int compare_file(const char *p_dst_file, const char *p_src_file)

 {

-	unsigned char *p_dst;

-	unsigned char *p_src;

+	char *p_dst;

+	char *p_src;

 	unsigned int dst_len;

 	unsigned int src_len;

+	int ret = 0;

 

 	if(p_dst_file == NULL || p_src_file == NULL)

 		return -1;

 

 	p_dst = read_file(p_dst_file, &dst_len);

-	if(!p_dst)

-		return -1;

-	

 	p_src = read_file(p_src_file, &src_len);

-	if(!p_src)

+	if ((dst_len == 0) && (src_len == 0))

 	{

-		free(p_dst);

-		return -1;

+		assert(p_dst == NULL);

+		assert(p_src == NULL);

+		return 0; //both empty file

 	}

-	

+

 	if(dst_len != src_len)

 	{

-		free(p_src);

-		free(p_dst);

-		

 		printf("compare_file size dstbuf = %d, srcbuf = %d\n", dst_len, src_len);

 		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "compare_file size dstbuf = %d, srcbuf = %d\n", dst_len, src_len);

-		return -1;

+		ret = -1;

+		goto out;

 	}

 

 	if(memcmp(p_src, p_dst, src_len) != 0)

 	{

-		free(p_src);

-		free(p_dst);

 		printf("compare_file memcmp not same\n");

 		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "compare_file memcmp not same\n");

-		return -1;

+		ret = -1;

+		goto out;

 	}

+	ret = 0;

 

-	free(p_src);

-	free(p_dst);

+out:

+	if (p_src)

+	{

+		free(p_src);

+	}

+	if (p_dst)

+	{

+		free(p_dst);

+	}

 	

-    return 0;

+	return ret;

 }

 

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

@@ -405,6 +428,101 @@
 	}

     return 0;

 }

+

+/* read success return 0, file not exist return 0, other return -1*/

+static int file_read_test(const char *filename)

+{

+	char *buf;

+	size_t read_size;

+	struct stat file_stat;

+	int result = stat(filename, &file_stat);

+

+	if (result == -1 && errno == ENOENT)

+	{

+		return 0; /* file not exist */

+	}

+	else

+	{

+		if (result != 0)

+		{

+			return -1;

+		}

+	}

+

+	buf = read_file(filename, &read_size);

+	if (buf == NULL)

+	{

+		if (read_size == 0)

+		{

+			return 0; /* empty file */

+		}

+		else

+		{

+			return -1; /* file read error */

+		}

+	}

+	else

+	{

+		free(buf);

+		return 0;

+	}

+}

+

+/**************************************************************************

+* º¯ÊýÃû³Æ£º check_files_read

+* ¹¦ÄÜÃèÊö£º ¼ì²éuserdataÎļþϵͳϵÄÎļþreadÊÇ·ñÕý³£

+* ²ÎÊý˵Ã÷£º ÎÞ

+* ·µ »Ø Öµ£º¼ì²éÕý³£·µ»Ø0, ·ñÔò·µ»Ø-1£¬½øÐÐuserdata·ÖÇøµÄ²Á³ý

+* ÆäËü˵Ã÷£º

+**************************************************************************/

+static int check_files_read()

+{

+	if(file_read_test(NV_FS_RW_MAIN_PATH) < 0)

+	{

+		printf("fs_check file: %s  read loss \n",NV_FS_RW_MAIN_PATH);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s  read loss \n",NV_FS_RW_MAIN_PATH);

+		return -1;//²é¿´rw_workÎļþÊÇ·ñ¿É¶Á

+	}

+	if(file_read_test(NV_FS_RW_BACKUP_PATH) < 0)

+	{

+		printf("fs_check file: %s  read loss \n",NV_FS_RW_BACKUP_PATH);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s  read loss \n",NV_FS_RW_BACKUP_PATH);

+		return -1;//²é¿´rw_backupÎļþÊÇ·ñ¿É¶Á

+	}

+	if(file_read_test(NV_FS_FAC_SYMBOL_PATH) < 0)

+	{

+		printf("fs_check file: %s  read loss \n",NV_FS_FAC_SYMBOL_PATH);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s  read loss \n",NV_FS_FAC_SYMBOL_PATH);

+		return -1;//²é¿´fac_flagÎļþÊÇ·ñ¿É¶Á

+	}

+	if(file_read_test(NV_FS_RW_MAIN_SYMBOL_PATH) < 0)

+	{

+		printf("fs_check file: %s  read loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s  read loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);

+		return -1;//²é¿´work_flagÎļþÊÇ·ñ¿É¶Á

+	}

+	if(file_read_test(NV_FS_RW_BACKUP_SYMBOL_PATH) < 0)

+	{

+		printf("fs_check file: %s  read loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s  read loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);

+		return -1;//²é¿´backup_flagÎļþÊÇ·ñ¿É¶Á

+	}

+	if(file_read_test(NV_FS_RW_AP_NV_MAIN_PATH) < 0)

+	{

+		printf("fs_check file: %s  read loss \n",NV_FS_RW_AP_NV_MAIN_PATH);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s  read loss \n",NV_FS_RW_AP_NV_MAIN_PATH);

+		return -1;//²é¿´apϹ¤×÷nvÎļþÊÇ·ñ¿É¶Á

+	}

+	if(file_read_test(NV_FS_RW_AP_NV_BACKUP_PATH) < 0)

+	{

+		printf("fs_check file: %s  read loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);

+		sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s  read loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);

+		return -1;//²é¿´apϱ¸·ÝnvÎļþÊÇ·ñ¿É¶Á

+	}

+

+	return 0;

+}

+

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

 * º¯ÊýÃû³Æ£º check_userdata_is_space_enough

 * ¹¦ÄÜÃèÊö£º ¼ì²éuserdata·ÖÇøÏÂÃæµÄ¿Õ¼äÊÇ·ñ×ã¹»£¬²»×ãʱÐèÒªÖØÐ»ָ´userdata·ÖÇø

@@ -427,8 +545,8 @@
     struct statfs diskinfo;

     statfs("/mnt/userdata", &diskinfo);

 

-    //printf("fs_check f_bsize = %d, f_blocks = %d, f_bfree = %d, f_bavail = %d, f_files = %d, , f_ffree = %d\n", \

-    //    diskinfo.f_bsize, diskinfo.f_blocks, diskinfo.f_bfree, diskinfo.f_bavail, diskinfo.f_files, diskinfo.f_ffree);

+    printf("fs_check f_bsize = %d, f_blocks = %d, f_bfree = %d, f_bavail = %d, f_files = %d, , f_ffree = %d\n", \

+        diskinfo.f_bsize, diskinfo.f_blocks, diskinfo.f_bfree, diskinfo.f_bavail, diskinfo.f_files, diskinfo.f_ffree);

 

     //»ñȡʣÓà¿Õ¼äÊÇ·ñ´óÓÚµÈÓÚÁ½¸öblock

     if ((diskinfo.f_bsize * diskinfo.f_bfree  < CONFIG_BLOCK_SIZE * USERDATA_RESET_FREE_BLOCK_LEVEL) || 

@@ -507,7 +625,11 @@
 	*/

 	if(access(NV_FS_RW_HASH_WORK_PATH, F_OK) < 0)

 		return 0;

-	

+

+	if (check_files_read() < 0)

+	{

+		return -1;//userdata·ÖÇøÏµÄÎļþ¶ÁÊÇ·ñÕý³£

+	}

 	if(compare_file(NV_FS_RW_HASH_FAC_PATH, NV_FS_RW_HASH_WORK_PATH) == 0)

 	{

 		if(check_files_access() < 0)

@@ -787,6 +909,7 @@
 			continue;

 		}

 

+		memset(buf, 0xff, meminfo.erasesize);

 		ret = fread(buf, 1, meminfo.erasesize, fp);

 		if(ret < 0)

 		{