[Feature][ZXW-241]merge P56U01 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: I7985538dde6951dd824c36149bf9a1e3ca23c734
diff --git a/boot/common/src/uboot/drivers/misc/load.c b/boot/common/src/uboot/drivers/misc/load.c
index 5c3a2b3..827201d 100755
--- a/boot/common/src/uboot/drivers/misc/load.c
+++ b/boot/common/src/uboot/drivers/misc/load.c
@@ -1228,9 +1228,11 @@
 	unsigned long crc = 0;
 	unsigned long crc_cal = 0;
 	u32 crc_size = sizeof(T_FLAGS_INFO);
+	char name[IMG_NAME_LEN];
 
 	flush_dcache_all();
-    flash_dmabuf_disable_flag = 1;
+    //flash_dmabuf_disable_flag = 1;
+	
 	flash = get_flash_ops();
 	type = read_boot_flashtype();
 
@@ -1246,53 +1248,57 @@
 	part_offset = entry->part_offset;
 	part_size = entry->part_size;
 	//while(flag);
-	if(type == IF_TYPE_NAND || type == IF_TYPE_SPI_NAND)
+
+    /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
+	for (off = part_offset; off < part_offset+part_size; off += nand->erasesize)
 	{
-        /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
-		for (off = part_offset; off < part_offset+part_size; off += nand->erasesize)
+        if (!(nand_block_isbad(nand,off)))
 		{
-            if (!(nand_block_isbad(nand,off)))
-			{
-	            blockNum += 1;
-			}   
+            blockNum += 1;
+		}   
+	
+		if((blockNum == 1) && (flag_one == 0))
+		{
+            work_area_offset = off;
+			flag_one = 1;
+		}
 		
-			if((blockNum == 1) && (flag_one == 0))
-			{
-	            work_area_offset = off;
-				flag_one = 1;
-			}
+		else if(blockNum == 2)
+		{
+             backup_area_offset = off;
+			 break;
+		}		
+	}
+
+	if(blockNum < 2)
+	{
+        printf("flags partition have not enough space!\n");
 			
-			else if(blockNum == 2)
-			{
-	             backup_area_offset = off;
-				 break;
-			}		
-		}
+		return -1;
+	}
+   
+	ret = flash->read(nand,(loff_t)work_area_offset,
+					  &fota_size,(u_char *)(fotaFlagInfo));
+	if( ret != 0 )
+	{
+		BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
+		return 1;
+	}
 
-		if(blockNum < 2)
-		{
-	        printf("flags partition have not enough space!\n");
-				
-			return -1;
-		}
-	   
-		ret = flash->read(nand,(loff_t)work_area_offset,
-						  &fota_size,(u_char *)(fotaFlagInfo));
-		if( ret != 0 )
-		{
-			BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
-			return 1;
-		}
+	/*crc32УÑé*/
+	crc = fotaFlagInfo->crc32_1;
+	fotaFlagInfo->crc32_1 = 0;
+	crc32init_le();
+	crc_cal = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
+	printf("crc is 0x%x,crc_cal is 0x%x\n",crc,crc_cal);
+	//printf("work_area_offset is 0x%x,backup_area_offset is 0x%x,crc_size is 0x%x\n",work_area_offset,backup_area_offset,crc_size);
 
-		/*crc32УÑé*/
-		crc = fotaFlagInfo->crc32;
-		fotaFlagInfo->crc32 = 0;
-		crc32init_le();
-    	crc_cal = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
-		printf("crc is 0x%x,crc_cal is 0x%x\n",crc,crc_cal);
-		
-		if(fotaFlagInfo->magic_start != FLAGS_MAGIC || (crc!= crc_cal && crc != 0))
+	memset(name, 0xff, IMG_NAME_LEN);
+	if(!memcmp(fotaFlagInfo->img_size[0].name, name, IMG_NAME_LEN)){
+		printf("image name is null.\n");
+		if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
 		{	
+		    printf("work_area_offset magic err.\n");
 			flush_dcache_all();
 			ret = flash->read(nand,(loff_t)backup_area_offset,
 						  &fota_size,(u_char *)(fotaFlagInfo));
@@ -1307,59 +1313,186 @@
 			{
 	            printf("flags magic err.\n");
 			    return -1;
-			}
-
-			/*crc32УÑé*/
-			crc = fotaFlagInfo->crc32;
-			fotaFlagInfo->crc32 = 0;
-			crc32init_le();
-	    	crc_cal = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
-
-			if(crc!= crc_cal && crc != 0)
-			{
-	            printf("flags crc err.\n");
-			    return -1;
-			}
-			
+			}			
 		}
+
 	}
-	else if(type == IF_TYPE_NOR)
-	{
-        work_area_offset = part_offset;
-	    backup_area_offset = part_offset + nor->nor[0].mtd.erasesize;
-		ret = nand_read(&(nor->nor[0].mtd), (loff_t)work_area_offset, 
-						&fota_size, (u_char *)(fotaFlagInfo));
-		if(ret)
+	else if(fotaFlagInfo->magic_start != FLAGS_MAGIC || (crc!= crc_cal && crc != 0))
+	{	
+		flush_dcache_all();
+		printf("work_area_offset magic is 0x%x.\n",fotaFlagInfo->magic_start);
+		ret = flash->read(nand,(loff_t)backup_area_offset,
+					  &fota_size,(u_char *)(fotaFlagInfo));
+		if(ret != 0)
 		{
-			BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
+			printf("read flags backup partition err.\n");
+			
 			return -1;
 		}
+		
 		if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
 		{
-            flush_dcache_all();
-			ret = nand_read(&(nor->nor[0].mtd), (loff_t)backup_area_offset, 
-						&fota_size, (u_char *)(fotaFlagInfo));
-			if(ret != 0)
-			{
-				printf("read flags backup partition err.\n");
-				
-				return -1;
-			}
-			
-			if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
-			{
-	            printf("flags magic err.\n");
-			    return -1;
-			}	
+		    printf("backup_area_offset magic is 0x%x.\n",fotaFlagInfo->magic_start);
+            printf("flags magic err.\n");
+		    return -1;
 		}
+
+		/*crc32УÑé*/
+		crc = fotaFlagInfo->crc32_1;
+		fotaFlagInfo->crc32_1 = 0;
+		crc32init_le();
+    	crc_cal = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
+
+		if(crc!= crc_cal && crc != 0)
+		{
+            printf("flags crc err.\n");
+		    return -1;
+		}
+		
 	}
-	
+
 	flash_dmabuf_disable_flag = 0;
 	flush_dcache_all();
 
 	return 0;
 }
 
+int write_flags(T_FLAGS_INFO *fotaFlagInfo)
+{
+    uint32_t off = 0;
+	uint32_t flags_size = 0;/*flags·ÖÇø2M*/
+	uint32_t blockNum = 0;
+	uint32_t work_area_offset = 0;
+	uint32_t backup_area_offset = 0;
+	int ret = 0;
+	int type = 0;
+	uchar * flags_name = "flags";
+	u32 fota_size = sizeof(T_FLAGS_INFO);
+	//fota_size = page_align(fota_size);
+	unsigned long crc = 0;
+	u32 crc_size = sizeof(T_FLAGS_INFO);
+	char name[IMG_NAME_LEN];
+	
+	nand_info_t *nand = &nand_info[nand_curr_device];
+	struct fsl_qspi *nor = &spi_nor_flash;
+	uint32_t part_offset = 0;
+	struct flash_ops *flash = NULL;
+	nand_erase_options_t opts;
+	
+	flush_dcache_all();
+	//flash_dmabuf_disable_flag = 1;
+
+	flash = get_flash_ops();
+	type = read_boot_flashtype();
+
+	/* ѰÕÒ·ÖÇø */
+	partition_entry_t * entry = find_partition_para(flags_name);
+	if( entry == NULL )
+	{
+		BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", flags_name);
+		return -1;
+	}
+	/* »ñµÃ·ÖÇøÊ×µØÖ·ºÍ´óС */
+	part_offset = entry->part_offset;
+	flags_size = entry->part_size;
+	
+	/*crc32*/
+	memset(name, 0xff, IMG_NAME_LEN);
+	if(!memcmp(fotaFlagInfo->img_size[0].name, name, IMG_NAME_LEN)){
+		printf("the image name is null.\n");
+		fotaFlagInfo->crc32_1 = 0;	
+	    memset((unsigned char*)(fotaFlagInfo->img_size[0].name), 0, (crc_size - 512));
+		crc32init_le();
+		crc = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
+		fotaFlagInfo->crc32_1 = crc;
+		printf("flags partition crc32_1 is 0x%x\n",crc);
+		
+	}else{
+		fotaFlagInfo->crc32 = 0;
+		crc32init_le();
+		crc = crc32_le(0,(unsigned char*)fotaFlagInfo,512);
+		fotaFlagInfo->crc32 = crc;
+		
+		fotaFlagInfo->crc32_1 = 0;
+		crc32init_le();
+		crc = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
+		fotaFlagInfo->crc32_1 = crc;
+		printf("flags partition crc32 is 0x%x,crc32_1 is 0x%x\n",fotaFlagInfo->crc32,fotaFlagInfo->crc32_1);
+	}	
+
+	/*дÈëflags·ÖÇø*/
+        
+   /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
+	for (off = part_offset; off < part_offset+flags_size; off += nand->erasesize)
+	{
+		if (!(nand_block_isbad(nand,off))) 
+			blockNum += 1;
+		
+		if(blockNum == 1)
+			work_area_offset = off;
+		
+		else if(blockNum == 2)
+		{
+			 backup_area_offset = off;
+			 break;
+		}		
+	}
+
+	if(blockNum < 2)
+	{
+		printf("flags partition have not enough space!\n");
+			
+		return -1;
+	}
+
+	//printf("work_area_offset is 0x%x,backup_area_offset is 0x%x,crc_size is 0x%x\n",work_area_offset,backup_area_offset,crc_size);
+
+	/*²Á³ýflagsÖ÷·ÖÇø*/
+	memset(&opts, 0, sizeof(opts));
+	opts.offset = work_area_offset;
+	opts.length = fota_size;
+	ret = flash->erase(nand,&opts);
+
+	if( ret != 0 )
+	{
+		BOOT_PRINTF(UBOOT_ERR, "[%s]: erase the flags imagefs work area error!\n", flags_name);
+		return 1;
+	}
+
+	ret = flash->write(nand,(loff_t)(work_area_offset),
+			  &fota_size,(u_char *)fotaFlagInfo,0);
+	
+	if( ret != 0 )
+	{
+		BOOT_PRINTF(UBOOT_ERR, "[%s]: write the flags imagefs work area error!\n", flags_name);
+		return 1;
+	}
+
+	/*²Á³ýflags±¸ÓÃÇø*/
+	memset(&opts, 0, sizeof(opts));
+	opts.offset = backup_area_offset;
+	opts.length = fota_size;
+	ret += flash->erase(nand,&opts);
+
+	if( ret != 0 )
+	{
+		BOOT_PRINTF(UBOOT_ERR, "[%s]: erase the flags imagefs backup area error!\n", flags_name);
+		return 1;
+	}
+	
+	ret += flash->write(nand,(loff_t)(backup_area_offset),
+			  &fota_size,(u_char *)fotaFlagInfo,0);
+	if( ret != 0 )
+	{
+		BOOT_PRINTF(UBOOT_ERR, "[%s]: write the flags imagefs backup area error!\n", flags_name);
+		return 1;
+	}
+	
+	flash_dmabuf_disable_flag = 0; 
+    flush_dcache_all();
+    return 0;
+}
+
 #endif
 /* ================================================================================
  *  get_fota_update_flag  :