Fix mbtk_otad for dfota.

Change-Id: I928fba9e76a79c8223685a13b5b053b67496973e
diff --git a/mbtk/mbtk_otad/Makefile b/mbtk/mbtk_otad/Makefile
index 1ddd589..add853e 100755
--- a/mbtk/mbtk_otad/Makefile
+++ b/mbtk/mbtk_otad/Makefile
@@ -13,8 +13,8 @@
 # -Wno-error=unused-function
 CFLAGS +=
 
-# -DCONFIG_AB_SYSTEM_DFOTA
-DEFINE += -DCONFIG_AB_SYSTEM -DCONFIG_AB_SYSTEM_DFOTA
+# -DCONFIG_AB_SYSTEM  -DCONFIG_AB_SYSTEM_DFOTA
+DEFINE +=
 
 MY_FILES_PATH:=$(LOCAL_PATH)/src
 
diff --git a/mbtk/mbtk_otad/src/otad.c b/mbtk/mbtk_otad/src/otad.c
index 3cb6db3..bbe4cb8 100755
--- a/mbtk/mbtk_otad/src/otad.c
+++ b/mbtk/mbtk_otad/src/otad.c
@@ -27,17 +27,27 @@
 #include "libhttpclient/libhttpclient.h"
 #include "otad.h"
 
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 #define MEMLOCKPRIV    _IO('M', 25)
 #define MEMUNLOCKPRIV  _IO('M', 26)
 
+#define OTA_ADDR_DEF 0x2a60000
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
 static int complete_read(int fd, char *buf, int size);
 static int complete_write(int fd, char *buf, int size);
 #include "tim.h"
 
-#if !defined(CONFIG_AB_SYSTEM_DFOTA)
+//#if !defined(CONFIG_AB_SYSTEM_DFOTA)
 #include "tim.c"
-#endif
+//#endif
 
 #include "getfotav.c"
 
@@ -70,6 +80,7 @@
 };
 static int update_state = UPDATE_STATE_IDLE;
 static int update_oemd;
+static bool is_dfota = FALSE; // Default not dfota.
 
 struct version_info {
 	char version[OTA_MAX_STRING_LEN];
@@ -77,10 +88,10 @@
 	char * release_note;
 };
 
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 #define MIN_RLS_VERSION 846
 
-#if !defined(CONFIG_AB_SYSTEM_DFOTA)
+//#if !defined(CONFIG_AB_SYSTEM_DFOTA)
 static int upgrade_precheck(char *fotav)
 {
 	char *p;
@@ -103,7 +114,7 @@
 	/* support upgrade */
 	return 0;
 }
-#endif
+//#endif
 
 static int complete_read(int fd, char *buf, int size);
 static int complete_write(int fd, char *buf, int size);
@@ -142,7 +153,7 @@
 	unsigned int emmc_block_size; /* 512B */
 	unsigned int fbf_addr;
 	char mtd_asrflag[64];
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 	int mtd_cnt;
 	struct image_mtd_info image_mtd_info[MAX_MTD_PARTITION_CNT];
 	unsigned int cpuid;
@@ -286,7 +297,7 @@
 	unsigned int Flash_Erase_Size;
 	unsigned int Img_ID;
 	unsigned int Image_In_TIM;
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 	int fd;
 	char name[32];
 	char dev[16];
@@ -407,7 +418,7 @@
 	unsigned int dfota_n_of_images;
 	unsigned int dfota_need_copy_only;
 	unsigned int dfota_conpy_len;
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 	unsigned int active_slot; /* prev active slot */
 	unsigned int temp_active_slot; /* current active slot */
 	unsigned int reboot_cnt;
@@ -674,7 +685,7 @@
 	return 0;
 }
 
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 static int get_inactive_system()
 {
 	if (get_asr_flag(&gAsrFlag)) {
@@ -811,7 +822,7 @@
 	sprintf(ddev, "/dev/%s", dst);
 	int fdSrc = open(sdev, O_RDWR | O_SYNC);
 	int fdDst = open(ddev, O_RDWR | O_SYNC);
-#ifndef CONFIG_PARTITION_EMMC
+#if 1//ndef CONFIG_PARTITION_EMMC
 	struct erase_info_user mtdEraseInfo;
 	struct mtd_info_user mtdInfo;
 	if (ioctl(fdDst, MEMGETINFO, &mtdInfo)) {
@@ -842,16 +853,16 @@
 
 		if (complete_read(fdSrc, buf, n) < 0)
 			goto err;
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 		ioctl(fdDst, MEMUNLOCK, &mtdEraseInfo);
 		ioctl(fdDst, MEMERASE, &mtdEraseInfo);
-#endif
+//#endif
 		if (complete_write(fdDst, buf, n) < 0)
 			goto err;
 		size -= n;
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 		mtdEraseInfo.start += mtdInfo.erasesize;
-#endif
+//#endif
 	}
 
 	ret = 0;
@@ -865,7 +876,7 @@
 	return ret;
 }
 
-#if !defined(CONFIG_AB_SYSTEM_DFOTA)
+#if 1//!defined(CONFIG_AB_SYSTEM_DFOTA)
 static int get_file_size(const char *path)
 {
 	int filesize = -1;
@@ -1002,7 +1013,7 @@
 		img_start = pImage_11->First_Sector<<TR069_FBF_HEADER_SIZE;
 
 		temp_table = image_table;
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 		if (pImage_11->Image_ID == FOTA_FBFVERSION_IMAGEID) {
 			server_cfg.fotav_offset_in_fbf = img_start;
 			OTA_DEBUG("%s: fotav_offset_in_fbf = 0x%x\n", __func__, server_cfg.fotav_offset_in_fbf);
@@ -1030,7 +1041,7 @@
 		if (pImage_11->Image_ID == IMAGE_ID_OBM)
 			obm_real_size = pImage_11->length;
 
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 		/* the unit of address in fbf was: block */
 		pImage_11->Flash_Start_Address *= server_cfg.emmc_block_size;
 #endif
@@ -1093,7 +1104,7 @@
 		if (pImage_11->Image_ID == OEMDDENTIFIER)
 			update_oemd = 1;
 		image_table->next = temp_table;
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 		char dev[32] = {0};
 		sprintf(dev, "/dev/%s", pImageMtdInfo->dev);
 		if (image_table->Img_ID == IMAGE_ID_OBM) {
@@ -1149,13 +1160,15 @@
 	OTA_DEBUG("  all header size: %d\n", (pMasterHeader->deviceHeaderOffset[0] + sizeof(struct DeviceHeader_11_tr069) +
 		sizeof(ImageStruct_11) * pDevHeader_11->nOfImages));
 	OTA_DEBUG("  already processed: %d\n", context->processed_cnt);
-#ifdef CONFIG_AB_SYSTEM
-#ifndef CONFIG_AB_SYSTEM_DFOTA
+//#ifdef CONFIG_AB_SYSTEM
+//#ifndef CONFIG_AB_SYSTEM_DFOTA
 	/* once parse image ok, clear the synced flag immediately */
-	gAsrFlag.synced = 0;
-	write_asr_flag(&gAsrFlag);
-#endif
-#endif
+    if(!is_dfota) {
+        gAsrFlag.synced = 0;
+        write_asr_flag(&gAsrFlag);
+    }
+//#endif
+//#endif
 	return (pMasterHeader->deviceHeaderOffset[0] + sizeof(struct DeviceHeader_11_tr069) +
 		sizeof(ImageStruct_11) * pDevHeader_11->nOfImages) - context->processed_cnt;
 }
@@ -1164,7 +1177,7 @@
 {
 	ImginfoTable * temp = context->image_info_table;
 	while (temp) {
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 		OTA_DEBUG("Get Image from start: 0x%08x, len: 0x%08x, flashoffset: 0x%08x(%s)\n",
 			temp->Img_Start_Address, temp->Img_Len, temp->Flash_Start_Address, temp->name);
 #else
@@ -1199,10 +1212,12 @@
 	struct image_state * temp = context->image_state_list;
 	while (temp) {
 		struct image_state * backup = temp->next_image;
-#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
-		close(temp->image_info->fd);
-		temp->image_info->fd = -1;
-#endif
+//#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
+        if(!is_dfota) {
+    		close(temp->image_info->fd);
+    		temp->image_info->fd = -1;
+        }
+//#endif
 		free(temp);
 		temp = backup;
 	}
@@ -1219,7 +1234,7 @@
 	return checksum;
 }
 
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+#if 1//!defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
 static int mark_current_image_processed(struct image_process_context * context)
 {
 	struct image_state * temp = context->image_state_list;
@@ -1329,212 +1344,216 @@
 		context->ota_download_notification_cnt = ota_download_progress;
 		notify_progress(context->ota_download_notification_cnt);
 	}
-#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
-	/* len = 4096 for every packet except for the last one */
-	if (total <= FBF_FILE_SECTOR_SIZE) {
-		OTA_ERR("Error file total size %d...\n", total);
-		return -1;
-	}
+// #if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(!is_dfota) {
+    	/* len = 4096 for every packet except for the last one */
+    	if (total <= FBF_FILE_SECTOR_SIZE) {
+    		OTA_ERR("Error file total size %d...\n", total);
+    		return -1;
+    	}
 
-	/* 1. FBF Head sit one the first sector of the FBF file, check it */
-	if (context->state == HEADER) {
-		ret = tr069_fbf_parse(data, len, context);
-		if (ret < 0) {
-			OTA_ERR("Failed, fbf parse failed...\n");
-			return -1;
-		}
+    	/* 1. FBF Head sit one the first sector of the FBF file, check it */
+    	if (context->state == HEADER) {
+    		ret = tr069_fbf_parse(data, len, context);
+    		if (ret < 0) {
+    			OTA_ERR("Failed, fbf parse failed...\n");
+    			return -1;
+    		}
 
-		if (context->image_info_table) {
-			context->state = CONTENT;
-			dump_table(context);
-			// Build image list for trace all image download result
-			build_image_list(context);
-		}
-		context->processed_cnt = len;
-	} else if (context->state == CONTENT) {
-		if (context->processed_cnt < 0x2000) {
-			OTA_DEBUG("skip 2nd 4K...\n");
-            // Add by liubin
-            revision_out_find(data, len, context->processed_cnt);
-            // End by liubin
+    		if (context->image_info_table) {
+    			context->state = CONTENT;
+    			dump_table(context);
+    			// Build image list for trace all image download result
+    			build_image_list(context);
+    		}
+    		context->processed_cnt = len;
+    	} else if (context->state == CONTENT) {
+    		if (context->processed_cnt < 0x2000) {
+    			OTA_DEBUG("skip 2nd 4K...\n");
+                // Add by liubin
+                revision_out_find(data, len, context->processed_cnt);
+                // End by liubin
 
-			context->processed_cnt += len;
-			return 0;
-		}
+    			context->processed_cnt += len;
+    			return 0;
+    		}
 
-		if (context->flash_cache_index < server_cfg.block_size) {
-			/* flash_cache_index increase by 4096 */
-			char *pos = context->flash_cache + context->flash_cache_index;
-			memcpy(pos, data, len); // len = 4096 here
-			context->flash_cache_index += len;
-			context->processed_cnt += len;
-		}
+    		if (context->flash_cache_index < server_cfg.block_size) {
+    			/* flash_cache_index increase by 4096 */
+    			char *pos = context->flash_cache + context->flash_cache_index;
+    			memcpy(pos, data, len); // len = 4096 here
+    			context->flash_cache_index += len;
+    			context->processed_cnt += len;
+    		}
 
-		if (server_cfg.fotav_offset_in_fbf &&
-			context->processed_cnt == (server_cfg.fotav_offset_in_fbf + 0x1000))
-		{
-			memcpy(server_cfg.fotav, context->flash_cache, 128);
-			temp = strlen(server_cfg.fotav);
-			if (server_cfg.fotav[temp - 1] == ';')
-				server_cfg.fotav[temp - 1] = 0; /* remove last ; */
-			OTA_DEBUG("%s: got fota version: %s\n", __func__, server_cfg.fotav);
-			if (upgrade_precheck(server_cfg.fotav))
-				exit(-1);
-		}
+    		if (server_cfg.fotav_offset_in_fbf &&
+    			context->processed_cnt == (server_cfg.fotav_offset_in_fbf + 0x1000))
+    		{
+    			memcpy(server_cfg.fotav, context->flash_cache, 128);
+    			temp = strlen(server_cfg.fotav);
+    			if (server_cfg.fotav[temp - 1] == ';')
+    				server_cfg.fotav[temp - 1] = 0; /* remove last ; */
+    			OTA_DEBUG("%s: got fota version: %s\n", __func__, server_cfg.fotav);
+    			if (upgrade_precheck(server_cfg.fotav))
+    				exit(-1);
+    		}
 
-		//if (context->flash_cache_index == server_cfg.block_size) {
-			/* flush the cache to flash */
-			ImginfoTable *p;
-			p = context->image_info_table;
-			while (p)
-			{
-				int image_len = p->Img_Len;
-//				bool flush_cache = false;
-				/* FBF_FILE_SECTOR_SIZE alignment */
-				image_len = ((image_len + FBF_FILE_SECTOR_SIZE - 1) & (~ (FBF_FILE_SECTOR_SIZE - 1)));
-				int total_block = ((image_len + server_cfg.block_size - 1) & (~ (server_cfg.block_size - 1))) / server_cfg.block_size;
-				if (context->processed_cnt > p->Img_Start_Address) {
-					if (context->processed_cnt < (p->Img_Start_Address + image_len)) {
-						if (context->flash_cache_index < server_cfg.block_size) {
-							break;
-						}
-					}
-					if (context->processed_cnt == (p->Img_Start_Address + image_len) ||
-						context->flash_cache_index == server_cfg.block_size) {
-						p->cs = fbf_checksum((unsigned char *)context->flash_cache, context->flash_cache_index, p->cs);
-						int offset = context->processed_cnt - p->Img_Start_Address;
-						struct erase_info_user mtdEraseInfo;
-						offset += p->Flash_Start_Address - p->Block_Start_Address;
-						int block = ((offset + server_cfg.block_size - 1) & (~ (server_cfg.block_size - 1))) / server_cfg.block_size;
-						int start = server_cfg.block_size * (block - 1);
-						OTA_DEBUG("@ context->processed_cnt: 0x%x, p->Img_Start_Address: 0x%x, len: 0x%x, start: 0x%x\n",
-							context->processed_cnt, p->Img_Start_Address, image_len, start);
-						OTA_DEBUG("@ found, dev: %s, name: %s, block: %d(total: %d), cs: 0x%x(expect: 0x%x), index: 0x%x\n",
-							p->dev, p->name, block - 1,total_block,p->cs, p->Img_Checksum, context->flash_cache_index);
+    		//if (context->flash_cache_index == server_cfg.block_size) {
+    			/* flush the cache to flash */
+    			ImginfoTable *p;
+    			p = context->image_info_table;
+    			while (p)
+    			{
+    				int image_len = p->Img_Len;
+    //				bool flush_cache = false;
+    				/* FBF_FILE_SECTOR_SIZE alignment */
+    				image_len = ((image_len + FBF_FILE_SECTOR_SIZE - 1) & (~ (FBF_FILE_SECTOR_SIZE - 1)));
+    				int total_block = ((image_len + server_cfg.block_size - 1) & (~ (server_cfg.block_size - 1))) / server_cfg.block_size;
+    				if (context->processed_cnt > p->Img_Start_Address) {
+    					if (context->processed_cnt < (p->Img_Start_Address + image_len)) {
+    						if (context->flash_cache_index < server_cfg.block_size) {
+    							break;
+    						}
+    					}
+    					if (context->processed_cnt == (p->Img_Start_Address + image_len) ||
+    						context->flash_cache_index == server_cfg.block_size) {
+    						p->cs = fbf_checksum((unsigned char *)context->flash_cache, context->flash_cache_index, p->cs);
+    						int offset = context->processed_cnt - p->Img_Start_Address;
+    						struct erase_info_user mtdEraseInfo;
+    						offset += p->Flash_Start_Address - p->Block_Start_Address;
+    						int block = ((offset + server_cfg.block_size - 1) & (~ (server_cfg.block_size - 1))) / server_cfg.block_size;
+    						int start = server_cfg.block_size * (block - 1);
+    						OTA_DEBUG("@ context->processed_cnt: 0x%x, p->Img_Start_Address: 0x%x, len: 0x%x, start: 0x%x\n",
+    							context->processed_cnt, p->Img_Start_Address, image_len, start);
+    						OTA_DEBUG("@ found, dev: %s, name: %s, block: %d(total: %d), cs: 0x%x(expect: 0x%x), index: 0x%x\n",
+    							p->dev, p->name, block - 1,total_block,p->cs, p->Img_Checksum, context->flash_cache_index);
 
-						if (p->fd < 0) {
-							OTA_ERR("mtd device open failed...\n");
-							return -1;
-						}
+    						if (p->fd < 0) {
+    							OTA_ERR("mtd device open failed...\n");
+    							return -1;
+    						}
 
-						if (p->erased == 0 && strstr(p->name, "oem_data")) {
-							OTA_DEBUG("Need to Erase all of %s(%s)\n", p->dev, p->name);
-							mtdEraseInfo.length = p->partition_size;
-							mtdEraseInfo.start = 0;
-							#ifndef CONFIG_PARTITION_EMMC
-								ioctl(p->fd, MEMUNLOCK, &mtdEraseInfo);
-								ioctl(p->fd, MEMERASE, &mtdEraseInfo);
-							#endif
-							p->erased = 1;
-						}
+    						if (p->erased == 0 && strstr(p->name, "oem_data")) {
+    							OTA_DEBUG("Need to Erase all of %s(%s)\n", p->dev, p->name);
+    							mtdEraseInfo.length = p->partition_size;
+    							mtdEraseInfo.start = 0;
+    							#ifndef CONFIG_PARTITION_EMMC
+    								ioctl(p->fd, MEMUNLOCK, &mtdEraseInfo);
+    								ioctl(p->fd, MEMERASE, &mtdEraseInfo);
+    							#endif
+    							p->erased = 1;
+    						}
 
-						if (p->Img_ID != IMAGE_ID_OBM && p->Img_ID != IMAGE_ID_TIMH) {
-							ret = lseek(p->fd, start, SEEK_SET);
-							if (ret < 0)
-							{
-								OTA_ERR("seek failed\n");
-								return -1;
-							}
+    						if (p->Img_ID != IMAGE_ID_OBM && p->Img_ID != IMAGE_ID_TIMH) {
+    							ret = lseek(p->fd, start, SEEK_SET);
+    							if (ret < 0)
+    							{
+    								OTA_ERR("seek failed\n");
+    								return -1;
+    							}
 
-							mtdEraseInfo.length = server_cfg.block_size;
-							mtdEraseInfo.start = start;
-							#ifndef CONFIG_PARTITION_EMMC
-								ioctl(p->fd, MEMUNLOCK, &mtdEraseInfo);
-								ioctl(p->fd, MEMERASE, &mtdEraseInfo);
-							#endif
-						}
+    							mtdEraseInfo.length = server_cfg.block_size;
+    							mtdEraseInfo.start = start;
+    							#ifndef CONFIG_PARTITION_EMMC
+    								ioctl(p->fd, MEMUNLOCK, &mtdEraseInfo);
+    								ioctl(p->fd, MEMERASE, &mtdEraseInfo);
+    							#endif
+    						}
 
-						ret = write(p->fd, context->flash_cache, context->flash_cache_index);
-						if (ret != context->flash_cache_index) {
-							OTA_ERR("error: write incomplete! %d/%d\n", ret, context->flash_cache_index);
-							return -1;
-						}
+    						ret = write(p->fd, context->flash_cache, context->flash_cache_index);
+    						if (ret != context->flash_cache_index) {
+    							OTA_ERR("error: write incomplete! %d/%d\n", ret, context->flash_cache_index);
+    							return -1;
+    						}
 
-						memset(context->flash_cache, 0, server_cfg.block_size);
-						context->flash_cache_index = 0;
-						break;
-					}
-				}
-				p = p->next;
-			}
+    						memset(context->flash_cache, 0, server_cfg.block_size);
+    						context->flash_cache_index = 0;
+    						break;
+    					}
+    				}
+    				p = p->next;
+    			}
 
-			if (!p) {
-				//OTA_ERR("not found image, ignore, %d...\n", context->processed_cnt);
-				memset(context->flash_cache, 0, server_cfg.block_size);
-				context->flash_cache_index = 0;
-			}
-		//}
-	}
-#else
-	if (context->flash_cb) context->flash_cb(context, data, len);
-	while (len > 0) {
-		switch (context->state) {
-			case HEADER: {
-				OTA_DEBUG("Header cosume %d, len %d\n", context->processed_cnt,len);
-				if ((context->processed_cnt + len) < MINI_SIZE) {
-					OTA_DEBUG("FBF header not complete, continue recv\n");
-					context->processed_cnt += len;
-					return 0;
-				}
-				ret = tr069_fbf_parse(context->flash_cache, len, context);
-				if (ret < 0) {
-					OTA_ERR("Failed, fbf parse failed...\n");
-					context->processed_cnt += len;
-					len -= len;
-					return -1;
-				}
-				len -= ret;
-				data += ret;
-				context->processed_cnt += ret;
-				if (context->image_info_table) {
-					context->state = CONTENT;
-					dump_table(context);
-					// Build image list for trace all image download result
-					build_image_list(context);
-				}
-				break;
-			}
-			case CONTENT:
-			default: {
-				ImginfoTable * temp;
-				if (!context->current_process) {
-					int discard;
-					temp = context->image_info_table;
-					while (temp) {
-						if (temp->Img_Start_Address <= (context->processed_cnt + len) &&
-							temp->Img_Start_Address >= context->processed_cnt) {
-							OTA_DEBUG("Find new image: start_address 0x%08x, image length %d, write to 0x%08x\n",
-								temp->Img_Start_Address, temp->Img_Len, temp->Flash_Start_Address);
-							if(temp->Img_Len!=0){
-								break;
-							}
-						}
-						temp = temp->next;
-					}
-					if (!temp) {
-						OTA_DEBUG("Current data[0x%08x~0x%08x] not contain any content\n",
-							context->processed_cnt, context->processed_cnt + len);
-						context->processed_cnt += len;
-						return 0;
-					}
-					discard = temp->Img_Start_Address - context->processed_cnt;
-					if (discard) OTA_DEBUG("Have discard %d...\n", discard);
-					len -= discard;
-					data += discard;
-					context->processed_cnt += discard;
-					context->current_process = temp;
-				}
-				ret = image_check(context, data, len);
-				len -= ret;
-				data += ret;
-			}
-		}
-	};
-#endif
+    			if (!p) {
+    				//OTA_ERR("not found image, ignore, %d...\n", context->processed_cnt);
+    				memset(context->flash_cache, 0, server_cfg.block_size);
+    				context->flash_cache_index = 0;
+    			}
+    		//}
+    	}
+    }
+//#else
+    else {
+    	if (context->flash_cb) context->flash_cb(context, data, len);
+    	while (len > 0) {
+    		switch (context->state) {
+    			case HEADER: {
+    				OTA_DEBUG("Header cosume %d, len %d\n", context->processed_cnt,len);
+    				if ((context->processed_cnt + len) < MINI_SIZE) {
+    					OTA_DEBUG("FBF header not complete, continue recv\n");
+    					context->processed_cnt += len;
+    					return 0;
+    				}
+    				ret = tr069_fbf_parse(context->flash_cache, len, context);
+    				if (ret < 0) {
+    					OTA_ERR("Failed, fbf parse failed...\n");
+    					context->processed_cnt += len;
+    					len -= len;
+    					return -1;
+    				}
+    				len -= ret;
+    				data += ret;
+    				context->processed_cnt += ret;
+    				if (context->image_info_table) {
+    					context->state = CONTENT;
+    					dump_table(context);
+    					// Build image list for trace all image download result
+    					build_image_list(context);
+    				}
+    				break;
+    			}
+    			case CONTENT:
+    			default: {
+    				ImginfoTable * temp;
+    				if (!context->current_process) {
+    					int discard;
+    					temp = context->image_info_table;
+    					while (temp) {
+    						if (temp->Img_Start_Address <= (context->processed_cnt + len) &&
+    							temp->Img_Start_Address >= context->processed_cnt) {
+    							OTA_DEBUG("Find new image: start_address 0x%08x, image length %d, write to 0x%08x\n",
+    								temp->Img_Start_Address, temp->Img_Len, temp->Flash_Start_Address);
+    							if(temp->Img_Len!=0){
+    								break;
+    							}
+    						}
+    						temp = temp->next;
+    					}
+    					if (!temp) {
+    						OTA_DEBUG("Current data[0x%08x~0x%08x] not contain any content\n",
+    							context->processed_cnt, context->processed_cnt + len);
+    						context->processed_cnt += len;
+    						return 0;
+    					}
+    					discard = temp->Img_Start_Address - context->processed_cnt;
+    					if (discard) OTA_DEBUG("Have discard %d...\n", discard);
+    					len -= discard;
+    					data += discard;
+    					context->processed_cnt += discard;
+    					context->current_process = temp;
+    				}
+    				ret = image_check(context, data, len);
+    				len -= ret;
+    				data += ret;
+    			}
+    		}
+    	};
+    }
+//#endif
 	return 0;
 }
 
-#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
+#if 1//defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
 static char *gTempbuf = NULL;
 static int gTempbufPos = 0;
 static int gTotalBytes = 0;
@@ -1620,23 +1639,25 @@
 
 static int flush_flash(struct image_process_context * context)
 {
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
-	int ret;
-	int block_cnt;
-	if (context->flash_cache_index && context->flash_cache) {
-		block_cnt = ((context->processed_cnt) / server_cfg.block_size);
-		if (((context->processed_cnt) % server_cfg.block_size) != 0) {
-			block_cnt++;;
-		}
-		OTA_DEBUG("flush at block %d\n", block_cnt);
-		ret = lseek(context->fd, server_cfg.block_size * block_cnt, SEEK_SET);
-		if (ret < 0) {
-			OTA_ERR("seek failed!\n");
-			return -1;
-		}
-		write(context->fd, context->flash_cache, server_cfg.block_size);
-	}
-#endif
+//#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(is_dfota) {
+    	int ret;
+    	int block_cnt;
+    	if (context->flash_cache_index && context->flash_cache) {
+    		block_cnt = ((context->processed_cnt) / server_cfg.block_size);
+    		if (((context->processed_cnt) % server_cfg.block_size) != 0) {
+    			block_cnt++;;
+    		}
+    		OTA_DEBUG("flush at block %d\n", block_cnt);
+    		ret = lseek(context->fd, server_cfg.block_size * block_cnt, SEEK_SET);
+    		if (ret < 0) {
+    			OTA_ERR("seek failed!\n");
+    			return -1;
+    		}
+    		write(context->fd, context->flash_cache, server_cfg.block_size);
+    	}
+    }
+//#endif
 	sync();
 	return 0;
 }
@@ -1739,11 +1760,15 @@
 
 	OTA_DEBUG("Try access %s through http\n", url);
 	http_client_setopt(client, HTTPCLIENT_OPT_URL, url);
-#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
-	http_client_setopt(client, HTTPCLIENT_OPT_RESPONSECB, firmware_download_cb_ab);
-#else
-	http_client_setopt(client, HTTPCLIENT_OPT_RESPONSECB, firmware_download_cb);
-#endif
+//#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(!is_dfota) {
+	    http_client_setopt(client, HTTPCLIENT_OPT_RESPONSECB, firmware_download_cb_ab);
+    }
+//#else
+    else {
+	    http_client_setopt(client, HTTPCLIENT_OPT_RESPONSECB, firmware_download_cb);
+    }
+//#endif
 	http_client_setopt(client, HTTPCLIENT_OPT_METHOD, HTTPCLIENT_REQUEST_GET);
 	http_client_setopt(client, HTTPCLIENT_OPT_RESPONSECB_DATA, context);
 	http_client_perform(client);
@@ -1757,7 +1782,7 @@
 #endif
 }
 
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+#if 1//!defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
 static int push2ram(struct image_process_context * context, char * data, int len)
 {
 	while (len && context->flash_cache) {
@@ -1868,8 +1893,8 @@
 #endif
 
 
-#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
-#ifdef CONFIG_PARTITION_EMMC
+#if 1//defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
+#if 0//def CONFIG_PARTITION_EMMC
 static int emmc_boot_lock_unlock(int no, int lock)
 {
 	char cmd[64] = {0};
@@ -1881,7 +1906,7 @@
 
 static int mtd_erase(int fd, unsigned int offset, unsigned int length)
 {
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 	struct erase_info_user erase;
 	OTA_DEBUG("%s: offset: 0x%x, length: 0x%x\n", __func__, offset, length);
 	if (lseek(fd, offset, SEEK_SET) < 0) {
@@ -1897,11 +1922,11 @@
 		OTA_ERR("%s: erase failed. err: %d\n", __func__, errno);
 		return -1;
 	}
-#endif
+//#endif
 	return 0;
 }
 
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 static int is_all_ff(char *buf, int size)
 {
 	for (int i = 0; i < size; i++) {
@@ -1910,7 +1935,7 @@
 	}
 	return 1;
 }
-#endif
+//#endif
 
 static int __upgrade_timh(int fd, unsigned int offset, const char *file,
 	unsigned int size, char *dst_buf, unsigned int dst_buf_size, unsigned int pagesize)
@@ -1952,7 +1977,7 @@
 		dstsize = size;
 	}
 
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 	if (dstsize > server_cfg.max_timh_size) {
 		/* 1. write all timh */
 		if (complete_write(fd, dst, server_cfg.max_timh_size) < 0) {
@@ -1972,14 +1997,14 @@
 			}
 		}
 	} else {
-#endif
+//#endif
 		if (complete_write(fd, dst, dstsize) < 0) {
 			OTA_ERR("%s: write failed\n", __func__);
 			goto err;
 		}
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 	}
-#endif
+//#endif
 
 	r = 0;
 err:
@@ -2135,7 +2160,7 @@
 	/* put this file right after the obm */
 	char *timh_bk_file = NULL;
 
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 	int emmc_boot_part = 0;
 	if (Image_In_Tim == TIMH_INC) {
 		bootdev = "/dev/mmcblk1boot0";
@@ -2188,7 +2213,7 @@
 		goto err;
 	}
 
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 	// no bbm for emmc
 #else
 	ioctl(fd, MEMUNLOCKPRIV, NULL);
@@ -2224,7 +2249,7 @@
 		goto err;
 	}
 
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 	/* unlock */
 	emmc_boot_lock_unlock(emmc_boot_part, 0);
 #endif
@@ -2242,7 +2267,7 @@
 		goto err;
 	}
 
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 	/* unlock */
 	emmc_boot_lock_unlock(emmc_boot_part, 1);
 #endif
@@ -2251,9 +2276,9 @@
 	r = 0;
 err:
 	if (fd >= 0) {
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 		ioctl(fd, MEMLOCKPRIV, NULL);
-#endif
+//#endif
 		close(fd);
 	}
 
@@ -2321,7 +2346,7 @@
 	revision = (server_cfg.cpuid >> 16) & 0xff;
 	context->same_tim = 0;
 
-#ifndef CONFIG_PARTITION_EMMC
+//#ifndef CONFIG_PARTITION_EMMC
 	switch (cpuid) {
 	case 0x1802:
 	case 0x1826:
@@ -2344,7 +2369,7 @@
 		server_cfg.max_timh_size = 16 * 1024;
 		break;
 	}
-#endif
+//#endif
 
 	OTA_DEBUG("%s: dual tim: %d, same tim: %d\n", __func__, context->dual_tim, context->same_tim);
 	if (access(tmp_obm_main_file, F_OK) || access(tmp_timh_main_file, F_OK)) {
@@ -2406,133 +2431,137 @@
 
 static int image_recheck(struct image_process_context * context)
 {
-#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
-	ImginfoTable *p = context->image_info_table;
-	char *cache = malloc(FBF_FILE_SECTOR_SIZE);
-	int flag = 0;
-	if (!cache) {
-		OTA_ERR("no memory\n");
-		return -1;
-	}
+//#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(!is_dfota) {
+    	ImginfoTable *p = context->image_info_table;
+    	char *cache = malloc(FBF_FILE_SECTOR_SIZE);
+    	int flag = 0;
+    	if (!cache) {
+    		OTA_ERR("no memory\n");
+    		return -1;
+    	}
 
-	ImginfoTable *tmp = p;
-	while (tmp) {
-		lseek(tmp->fd, 0, SEEK_SET);
-		tmp = tmp->next;
-	}
+    	ImginfoTable *tmp = p;
+    	while (tmp) {
+    		lseek(tmp->fd, 0, SEEK_SET);
+    		tmp = tmp->next;
+    	}
 
-	while (p) {
-		p->cs = 0;
-		OTA_DEBUG("recheck %s, %s, %d\n", p->dev, p->name, p->Flash_Start_Address - p->Block_Start_Address);
-		if (p->Img_ID == IMAGE_ID_OBM || p->Img_ID == IMAGE_ID_TIMH)
-			lseek(p->fd, 0, SEEK_SET);
-		else
-			lseek(p->fd, p->Flash_Start_Address - p->Block_Start_Address, SEEK_SET);
-/*
-		int ret = lseek(p->fd, 0, SEEK_SET);
-		if (ret < 0)
-		{
-			OTA_ERR("seek failed, %s\n", p->name);
-			flag = -1;
-			goto next;
-		}
-*/
-		int remain = p->Img_Len;
-		while (remain > 0) {
-			int size = remain > FBF_FILE_SECTOR_SIZE ? FBF_FILE_SECTOR_SIZE : remain;
-			memset(cache, 0, FBF_FILE_SECTOR_SIZE);
-			int n = read(p->fd, cache, size);
-			if (n != size) {
-				OTA_ERR("read failed, read %d(expect %d), %s\n", n, size, p->name);
-				flag = -1;
-				goto next;
-			}
-			p->cs = fbf_checksum((unsigned char *)cache, FBF_FILE_SECTOR_SIZE, p->cs);
-			remain -= n;
-		}
+    	while (p) {
+    		p->cs = 0;
+    		OTA_DEBUG("recheck %s, %s, %d\n", p->dev, p->name, p->Flash_Start_Address - p->Block_Start_Address);
+    		if (p->Img_ID == IMAGE_ID_OBM || p->Img_ID == IMAGE_ID_TIMH)
+    			lseek(p->fd, 0, SEEK_SET);
+    		else
+    			lseek(p->fd, p->Flash_Start_Address - p->Block_Start_Address, SEEK_SET);
+    /*
+    		int ret = lseek(p->fd, 0, SEEK_SET);
+    		if (ret < 0)
+    		{
+    			OTA_ERR("seek failed, %s\n", p->name);
+    			flag = -1;
+    			goto next;
+    		}
+    */
+    		int remain = p->Img_Len;
+    		while (remain > 0) {
+    			int size = remain > FBF_FILE_SECTOR_SIZE ? FBF_FILE_SECTOR_SIZE : remain;
+    			memset(cache, 0, FBF_FILE_SECTOR_SIZE);
+    			int n = read(p->fd, cache, size);
+    			if (n != size) {
+    				OTA_ERR("read failed, read %d(expect %d), %s\n", n, size, p->name);
+    				flag = -1;
+    				goto next;
+    			}
+    			p->cs = fbf_checksum((unsigned char *)cache, FBF_FILE_SECTOR_SIZE, p->cs);
+    			remain -= n;
+    		}
 
-		if (p->cs != p->Img_Checksum) {
-			OTA_ERR("%s, cs: 0x%x(expect 0x%x)\n", p->name, p->cs, p->Img_Checksum);
-			flag = -1;
-			goto next;
-		} else {
-			OTA_DEBUG("recheck %s, %s OK, cs: 0x%x(expect 0x%x)...\n", p->dev, p->name, p->cs, p->Img_Checksum);
-		}
-next:
-		p = p->next;
-	}
+    		if (p->cs != p->Img_Checksum) {
+    			OTA_ERR("%s, cs: 0x%x(expect 0x%x)\n", p->name, p->cs, p->Img_Checksum);
+    			flag = -1;
+    			goto next;
+    		} else {
+    			OTA_DEBUG("recheck %s, %s OK, cs: 0x%x(expect 0x%x)...\n", p->dev, p->name, p->cs, p->Img_Checksum);
+    		}
+    next:
+    		p = p->next;
+    	}
 
-	if (flag == 0) {
-		if (upgrade_bootloader(context) < 0) {
-			flag = -1;
-			OTA_ERR("%s: upgrade bootloader failed, don't reboot or powerdown...\n",
-				__func__);
-			return flag;
-		}
+    	if (flag == 0) {
+    		if (upgrade_bootloader(context) < 0) {
+    			flag = -1;
+    			OTA_ERR("%s: upgrade bootloader failed, don't reboot or powerdown...\n",
+    				__func__);
+    			return flag;
+    		}
 
-		/* all image write ok, update the active system slot */
-		//#define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->MEMBER)
-		int slot = gInActiveSystem == SYSTEM_A ? 'a' : 'b';
-		/* tell boot to update NVM files from oemd */
-		if (update_oemd)
-			system("touch /NVM/oemd && sync");
-		gAsrFlag.temp_active_slot = slot;
-		gAsrFlag.reboot_cnt = 0;
-		gAsrFlag.synced = 0;
+    		/* all image write ok, update the active system slot */
+    		//#define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->MEMBER)
+    		int slot = gInActiveSystem == SYSTEM_A ? 'a' : 'b';
+    		/* tell boot to update NVM files from oemd */
+    		if (update_oemd)
+    			system("touch /NVM/oemd && sync");
+    		gAsrFlag.temp_active_slot = slot;
+    		gAsrFlag.reboot_cnt = 0;
+    		gAsrFlag.synced = 0;
 
-		if (gInActiveSystem == SYSTEM_A) {
-			OTA_DEBUG("prev mversion a: %s updated to %s\n", gAsrFlag.mversion, server_cfg.fotav);
-			memset(gAsrFlag.mversion, 0, 128);
-			strncpy(gAsrFlag.mversion, server_cfg.fotav, 128);
-		} else {
-			OTA_DEBUG("prev mversion b: %s updated to %s\n", gAsrFlag.mversion, server_cfg.fotav);
-			memset(gAsrFlag.MVersion_B, 0, 128);
-			strncpy(gAsrFlag.MVersion_B, server_cfg.fotav, 128);
-		}
+    		if (gInActiveSystem == SYSTEM_A) {
+    			OTA_DEBUG("prev mversion a: %s updated to %s\n", gAsrFlag.mversion, server_cfg.fotav);
+    			memset(gAsrFlag.mversion, 0, 128);
+    			strncpy(gAsrFlag.mversion, server_cfg.fotav, 128);
+    		} else {
+    			OTA_DEBUG("prev mversion b: %s updated to %s\n", gAsrFlag.mversion, server_cfg.fotav);
+    			memset(gAsrFlag.MVersion_B, 0, 128);
+    			strncpy(gAsrFlag.MVersion_B, server_cfg.fotav, 128);
+    		}
 
-		write_asr_flag(&gAsrFlag);
-	}
-	return flag;
-#else
-    struct image_state * image_state_list, * image_state_list_org;
-	int file_size = 0;
-	image_state_list_org = image_state_list = context->image_state_list;
-	if ((context->image_state_list == NULL) || (context->download_result != 1)) {
-		return -1;
-	}
+    		write_asr_flag(&gAsrFlag);
+    	}
+    	return flag;
+    }
+//#else
+    else {
+        struct image_state * image_state_list, * image_state_list_org;
+    	int file_size = 0;
+    	image_state_list_org = image_state_list = context->image_state_list;
+    	if ((context->image_state_list == NULL) || (context->download_result != 1)) {
+    		return -1;
+    	}
 
-	while (image_state_list) {
-		OTA_DEBUG("The image(0x%08x) process result %d\n",
-			image_state_list->image_info->Img_Checksum, image_state_list->result);
-		if (!image_state_list->result) return -1;;
-		image_state_list = image_state_list->next_image;
-	}
+    	while (image_state_list) {
+    		OTA_DEBUG("The image(0x%08x) process result %d\n",
+    			image_state_list->image_info->Img_Checksum, image_state_list->result);
+    		if (!image_state_list->result) return -1;;
+    		image_state_list = image_state_list->next_image;
+    	}
 
-	if (!image_state_list && image_state_list_org) {
-		if (verify_flash_image(context, &file_size)) {
-			return -1;
-		}
-	}
-	/* set dlflag for non-AB */
-	/* tell boot to update NVM files from oemd */
-	if (update_oemd)
-		system("touch /NVM/oemd && sync");
+    	if (!image_state_list && image_state_list_org) {
+    		if (verify_flash_image(context, &file_size)) {
+    			return -1;
+    		}
+    	}
+    	/* set dlflag for non-AB */
+    	/* tell boot to update NVM files from oemd */
+    	if (update_oemd)
+    		system("touch /NVM/oemd && sync");
 
-	if(get_asr_flag(&gAsrFlag)) {
-		OTA_ERR("Fail to get asr flag\n");
-		return -1;
-	}
-	gAsrFlag.upgrade_flag = 1;
-	gAsrFlag.fbf_flash_address = server_cfg.fbf_addr + server_cfg.block_size;
-	gAsrFlag.fbf_file_size = context->processed_cnt;
-	gAsrFlag.upgrade_method = context->upgrade_method;
-	gAsrFlag.dfota_n_of_images = 0xFFFFFFFF;
+    	if(get_asr_flag(&gAsrFlag)) {
+    		OTA_ERR("Fail to get asr flag\n");
+    		return -1;
+    	}
+    	gAsrFlag.upgrade_flag = 1;
+    	gAsrFlag.fbf_flash_address = server_cfg.fbf_addr + server_cfg.block_size;
+    	gAsrFlag.fbf_file_size = context->processed_cnt;
+    	gAsrFlag.upgrade_method = context->upgrade_method;
+    	gAsrFlag.dfota_n_of_images = 0xFFFFFFFF;
 
-	if(write_asr_flag(&gAsrFlag)) {
-		OTA_ERR("Fail to write asr flag\n");
-		return -1;
-	}
-#endif
+    	if(write_asr_flag(&gAsrFlag)) {
+    		OTA_ERR("Fail to write asr flag\n");
+    		return -1;
+    	}
+    }
+//#endif
 	return 0;
 }
 
@@ -2636,9 +2665,11 @@
 	};
 	OTA_DEBUG("__download_throgh_udp received_size[%d],file_size[%d]\n", download_method_ctx.received_size,download_method_ctx.file_size);
 	if(download_method_ctx.received_size == download_method_ctx.file_size){
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
-		flush_flash(context);
-#endif
+//#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+        if(is_dfota) {
+            flush_flash(context);
+        }
+//#endif
 		context->download_result = 1;
 	}
 done:
@@ -2654,9 +2685,12 @@
 
 static int erase_ota_fbf_area(void)
 {
-#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
-	return 0;
-#else
+//#if defined(CONFIG_AB_SYSTEM) && !defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(!is_dfota) {
+        return 0;
+    }
+//#else
+    else {
 		struct erase_info_user mtdEraseInfo;
 		struct mtd_info_user mtdInfo;
 		int fd = open(server_cfg.mtd_fbf, O_RDWR | O_SYNC);
@@ -2674,7 +2708,8 @@
 		close(fd);
 		fd = -1;
 		sync();
-#endif
+    }
+//#endif
 		return 0;
 
 }
@@ -2726,15 +2761,17 @@
 	context = udp_context;
 
 	if(size!=0){
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
-		context->fd = open(server_cfg.mtd_fbf, O_RDWR | O_SYNC);
-		if (context->fd < 0) {
-			OTA_ERR("Cannot open MTD device!\n");
-			notify_download_end(0);
-			update_state = UPDATE_STATE_FAILED;
-			return ret;
-		}
-#endif
+//#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+        if(is_dfota) {
+    		context->fd = open(server_cfg.mtd_fbf, O_RDWR | O_SYNC);
+    		if (context->fd < 0) {
+    			OTA_ERR("Cannot open MTD device!\n");
+    			notify_download_end(0);
+    			update_state = UPDATE_STATE_FAILED;
+    			return ret;
+    		}
+        }
+//#endif
 		notify_download_start();
 		_ota_download_progress = ota_download_progress = 0;
 
@@ -2746,9 +2783,9 @@
 			OTA_ERR("Cannot malloc memory for download cache\n");
 			goto done;
 		}
-#ifdef CONFIG_AB_SYSTEM
+//#ifdef CONFIG_AB_SYSTEM
 		memset(context->flash_cache, 0, server_cfg.block_size);
-#endif
+//#endif
 		context->checksum_cache = malloc(CHECKSUM_CACHE_SIZE);
 		if (!context->checksum_cache) {
 			OTA_ERR("Cannot malloc memory for download cache\n");
@@ -2840,15 +2877,17 @@
 	struct image_process_context context = {0};
 	int ret = -1;
 
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
-	context.fd = open(server_cfg.mtd_fbf, O_RDWR | O_SYNC);
-	if (context.fd < 0) {
-		OTA_ERR("Cannot open MTD device!\n");
-		notify_download_end(0);
-		update_state = UPDATE_STATE_FAILED;
-		return ret;
-	}
-#endif
+//#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(is_dfota) {
+    	context.fd = open(server_cfg.mtd_fbf, O_RDWR | O_SYNC);
+    	if (context.fd < 0) {
+    		OTA_ERR("Cannot open MTD device!\n");
+    		notify_download_end(0);
+    		update_state = UPDATE_STATE_FAILED;
+    		return ret;
+    	}
+    }
+//#endif
 	notify_download_start();
 
 	_ota_download_progress = ota_download_progress = 0;
@@ -2858,9 +2897,9 @@
 		OTA_ERR("Cannot malloc memory for download cache\n");
 		goto done;
 	}
-#ifdef CONFIG_AB_SYSTEM
+//#ifdef CONFIG_AB_SYSTEM
 	memset(context.flash_cache, 0, server_cfg.block_size);
-#endif
+//#endif
 	context.checksum_cache = malloc(CHECKSUM_CACHE_SIZE);
 	if (!context.checksum_cache) {
 		OTA_ERR("Cannot malloc memory for download cache\n");
@@ -2910,10 +2949,12 @@
 	username = NULL;
 	if (psw) free(psw);
 	psw = NULL;
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
-	close(context.fd);
-	context.fd = -1;
-#endif
+//#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(is_dfota) {
+    	close(context.fd);
+    	context.fd = -1;
+    }
+//#endif
 	// Change by mbtk
 	if ((image_recheck(&context) == 0) && !Download_flag) {
 		update_state = UPDATE_STATE_UPDATED;
@@ -3385,8 +3426,8 @@
 	return atoi(content);
 }
 
-#ifdef CONFIG_AB_SYSTEM
-#ifndef CONFIG_PARTITION_EMMC
+//#ifdef CONFIG_AB_SYSTEM
+//#ifndef CONFIG_PARTITION_EMMC
 static int get_mtd_pagesize(int mtd, unsigned int *pagesize)
 {
 	struct mtd_info_user mtdinfo;
@@ -3412,8 +3453,8 @@
 
 	return 0;
 }
-#endif
-#endif
+//#endif
+//#endif
 
 static int init_cfg(const char * uci_path)
 {
@@ -3472,7 +3513,7 @@
 	}
 #endif
 
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 	fp = fopen("/proc/emmc", "r");
 #else
 	fp = fopen("/proc/mtd", "r");
@@ -3482,10 +3523,10 @@
 		return -1;
 	}
 
-#ifdef CONFIG_AB_SYSTEM
+//#ifdef CONFIG_AB_SYSTEM
 	server_cfg.mtd_cnt = 0;
 	OTA_DEBUG("Start to retrive the mtd partition info...\n");
-	#ifdef CONFIG_PARTITION_EMMC
+	#if 0//def CONFIG_PARTITION_EMMC
 		server_cfg.emmc_block_size = 512; /* emmc */
 		server_cfg.block_size = 0x20000; /* only used for buffer cache size */
 
@@ -3512,7 +3553,7 @@
 			return -1;
 		}
 	#endif
-#endif
+//#endif
 
     /*
     root@OpenWrt:~# cat /proc/mtd
@@ -3560,7 +3601,7 @@
 		int cnt = 0;
 		int index = 0;
 		char * p = buf;
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 		/* skip prev ' ' */
 		while ((index < (MAX_LINE_SIZE-1)) && (buf[index] == ' '))
 			index++;
@@ -3583,7 +3624,7 @@
 			}
 			index++;
 		}
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 		if (strncmp(name, "dev", 3) == 0)
 			continue;
 		/* retrive the mtd partition info */
@@ -3610,7 +3651,7 @@
 		sscanf(size, "%x", &pCurrentMtdInfo->size);
 		sscanf(erasesize, "%x", &pCurrentMtdInfo->erasesize);
 
-#ifdef CONFIG_PARTITION_EMMC
+#if 0//def CONFIG_PARTITION_EMMC
 		/* dev:        size     start     name */
 		/* the "erasesize" field represent "start" block in emmc */
 		pCurrentMtdInfo->flash_start_offset = pCurrentMtdInfo->erasesize * server_cfg.emmc_block_size;
@@ -3631,8 +3672,8 @@
 				snprintf(server_cfg.mtd_asrflag, 64, "/dev/mtdblock%d", bln);
 			#endif
 		}
-#ifdef CONFIG_AB_SYSTEM_DFOTA
-		else if (strstr(pCurrentMtdInfo->name, "OTA")) {
+//#ifdef CONFIG_AB_SYSTEM_DFOTA
+		else if (is_dfota && strstr(pCurrentMtdInfo->name, "OTA")) {
 			sscanf(size, "%x", &server_cfg.fbf_length);
 			#ifndef CONFIG_PARTITION_EMMC
 				sscanf(erasesize, "%x", &server_cfg.block_size);
@@ -3645,7 +3686,7 @@
 			OTA_DEBUG(" erasesize: %08x\n", server_cfg.block_size);
 			OTA_DEBUG(" tag: %s\n", tag);
 		}
-#endif
+//#endif
 		server_cfg.mtd_cnt++;
 #else
 		if (strncmp(tag, "\"asr_flag\"", 10) == 0) {
@@ -3691,7 +3732,7 @@
 		memset(buf, 0, MAX_LINE_SIZE);
 	}
 	fclose(fp);
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 	gInActiveSystem = get_inactive_system();
 	if (gInActiveSystem != SYSTEM_B && gInActiveSystem != SYSTEM_A) {
 		OTA_ERR("FATAL ERROR, no legal inactive system flag exist...");
@@ -3713,7 +3754,7 @@
 	char *p_mversion;
 	p_mversion = AsrFlag.mversion;
 
-#ifdef CONFIG_AB_SYSTEM
+#if 1//def CONFIG_AB_SYSTEM
 	if (gInActiveSystem == SYSTEM_A)
 		p_mversion = AsrFlag.MVersion_B;
 #endif
@@ -3784,6 +3825,7 @@
 	return ret;
 }
 
+// mbtk_otad -a fota/dfota -b 0x00000000
 int main(int argc, char **argv)
 {
 	int ret;
@@ -3839,14 +3881,43 @@
 	ret = ubus_add_object(ctx, &ota_object);
 
 	memset(&server_cfg, 0, sizeof(server_cfg));
+
+    if(argc > 1) {
+        int ch;
+        while((ch = getopt(argc, argv, "a:b:"))!= -1) {
+            switch(ch)
+            {
+                case 'a':
+                    if(strcmp(optarg, "fota") == 0) {
+                        is_dfota = FALSE;
+                    } else if(strcmp(optarg, "dfota") == 0) {
+                        is_dfota = TRUE;
+                    } else {
+                        LOGE("Must be fota/dfota.\n");
+                        return -1;
+                    }
+                    break;
+                case 'b':
+                    if(strlen(optarg) > 0)
+                        server_cfg.fbf_addr = (unsigned int)strtoul(optarg, NULL, 0);
+                    break;
+                default:
+                    LOGD("mbtk_otad -a fota/dfota -b 0x00000000");
+                    return -1;
+            }
+        }
+    }
+
     // ota
 	init_cfg(NULL);
-#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
-	if (server_cfg.fbf_length == 0 || server_cfg.block_size == 0) {
-		OTA_ERR("Config file incorrect!\n");
-		return -1;
-	}
-#endif
+//#if !defined(CONFIG_AB_SYSTEM) || defined(CONFIG_AB_SYSTEM_DFOTA)
+    if(is_dfota) {
+    	if (server_cfg.fbf_length == 0 || server_cfg.block_size == 0) {
+    		OTA_ERR("Config file incorrect!\n");
+    		return -1;
+    	}
+    }
+//#endif
 	if (server_cfg.progress_notify == 0) {
 		server_cfg.progress_notify = 10;
 	}
@@ -3857,7 +3928,7 @@
 		server_cfg.first_interval = 1;
 	}
 	if (server_cfg.fbf_addr == 0) {
-		server_cfg.fbf_addr = 0x2a60000;
+		server_cfg.fbf_addr = OTA_ADDR_DEF;
 	}
 
 	if (strlen(server_cfg.server_url) != 0) {