[bugfix][T108][bug-view-1120/1161] ota maybe block if repeat upgrade no reboot and get current system faild

Only Configure: No
Affected branch:LYNQ_SDK_ASR_T108_V05.04.01.00
Affected module: fota
Is it affected on IC: only ASR
Self-test: yes
Doc Update: no

Change-Id: I7e5de43bf03aec9188c1891e9482895de9765007
diff --git a/marvell/services/ota/otad.c b/marvell/services/ota/otad.c
old mode 100644
new mode 100755
index 710ddcc..e0176cb
--- a/marvell/services/ota/otad.c
+++ b/marvell/services/ota/otad.c
@@ -1634,15 +1634,16 @@
 	return 0;

 }

 

-static void __download_throgh_sd(const char * url, struct image_process_context * context)

+static int __download_throgh_sd(const char * url, struct image_process_context * context)

 {

 	int fd = -1;

 	int total = 0, all = 0;

 	char * buf = NULL;

+	int ret = -1;

 

 	if (url == NULL || context == NULL) {

 		OTA_ERR("Bad parameters!\n");

-		return;

+		goto done;

 	}

 	fd = open(url, O_RDONLY);

 	if (fd < 0) {

@@ -1673,11 +1674,16 @@
 			OTA_ERR("read firmware failed!\n");

 			goto done;

 		}

-		firmware_download_cb(buf, remain, all, context);

+

+		ret = firmware_download_cb(buf, remain, all, context);

+		if (ret < 0)

+			goto done;

 		total -= remain;

 	}

+

 	flush_flash(context);

 	context->download_result = 1;

+	ret = 0;

 done:

 	if (fd >= 0) {

 		close(fd);

@@ -1687,7 +1693,7 @@
 		free(buf);

 		buf = NULL;

 	}

-	return;

+	return ret;

 }

 

 

@@ -2487,6 +2493,9 @@
 		return -1;

 	}

 

+	if (context->download_result != 1)

+		return -1;

+

 	ImginfoTable *tmp = p;

 	while (tmp) {

 		lseek(tmp->fd, 0, SEEK_SET);

@@ -2666,15 +2675,15 @@
 	return client;

 }

 

-static void __download_throgh_udp(int fd, int size, struct image_process_context * context)

+static int __download_throgh_udp(int fd, int size, struct image_process_context * context)

 {

 	int total = size;

 	char * buf = malloc(4096);

-	int res=0;

+	int ret = -1;

 

 	if (buf == NULL) {

 		OTA_ERR("memory issue at __download_through_udp!\n");

-		return;

+		goto done;

 	}

 

 	// for debug

@@ -2704,8 +2713,8 @@
 			goto repeat;

 		}

 

-		res=firmware_download_cb(buf, recved, size, context);

-		if(res<0){

+		ret=firmware_download_cb(buf, recved, size, context);

+		if(ret<0){

             goto done;

 		}

 		total -= recved;

@@ -2717,12 +2726,13 @@
 #endif

 		context->download_result = 1;

 	}

+	ret = 0;

 done:

 	if (buf) {

 		free(buf);

 		buf = NULL;

 	}

-	return;

+	return ret;

 }

 

 

@@ -2959,7 +2969,9 @@
 			if (c >= 0) {

 				if(erase_ota_fbf_area())

 					goto done;

-				__download_throgh_udp(c, size, &context);

+				ret = __download_throgh_udp(c, size, &context);

+				if (ret < 0)

+					goto done;

 			}

 		}

 		if (s >= 0) close(s);

@@ -2968,7 +2980,9 @@
 		// download from SD

 		if(erase_ota_fbf_area())

 			goto done;

-		__download_throgh_sd(url, &context);

+		ret = __download_throgh_sd(url, &context);

+		if (ret < 0)

+			goto done;

 	} else {

 		// assert?

 		OTA_ERR("Unknow download type\n");

@@ -3070,6 +3084,7 @@
 	} else {

 		OTA_ERR("Error! update already in processing or be processed\n");

 		blobmsg_add_string(&b, "response", "download failed (update already in processing or be processed)");

+		notify_download_end(1);

 		ret = -1;

 	}

 

diff --git a/mbtk/libql_lib_v2_rilv2/ql_absys_api.c b/mbtk/libql_lib_v2_rilv2/ql_absys_api.c
index 5d0b253..6ef07d8 100755
--- a/mbtk/libql_lib_v2_rilv2/ql_absys_api.c
+++ b/mbtk/libql_lib_v2_rilv2/ql_absys_api.c
@@ -10,24 +10,32 @@
 
 int ql_absys_get_cur_active_part(absystem_t *cur_system)
 {
-    int active;
 
-    active = mbtk_fota_get_active_absys_type();
-    if (active == 0)
+    FILE *file = NULL;
+    char cmdline[1024] = {0}; 
+    
+    file = fopen("/proc/cmdline", "r");
+    if(file == NULL)
     {
-        *cur_system = SYSTEM_A;
-    }
-    else if (active == 1)
-    {
-        *cur_system = SYSTEM_B;
-    }
-    else
-    {
-        LOGE("ql_absys_get_cur_active_part fail");
-        return -1;
+        LOGE("Failed to open /proc/cmdline");
+        return -1; 
     }
 
+    if(fgets(cmdline, sizeof(cmdline), file) != NULL)
+    {
+        if(strstr(cmdline, "system=a") != NULL) 
+        {
+            *cur_system = SYSTEM_A;
+        }
+        else
+        {
+            *cur_system = SYSTEM_B;
+        }
+    }
+
+    fclose(file); 
     return 0;
+
 }
 
 int ql_absys_switch(void)
diff --git a/mbtk/libql_lib_v2_rilv2/ql_fota_api.c b/mbtk/libql_lib_v2_rilv2/ql_fota_api.c
index 9b833da..3dac4c9 100755
--- a/mbtk/libql_lib_v2_rilv2/ql_fota_api.c
+++ b/mbtk/libql_lib_v2_rilv2/ql_fota_api.c
@@ -16,6 +16,7 @@
 
 #define StatFunc(x,y) stat(x,y)
 
+static int s_ota_flag = -1;
 int funstat(  char *filename)
 {
     int ret = 0;
@@ -52,11 +53,18 @@
     int ret = 0;
     int len = 0;
 
-    ret = mbtk_fota_init(ql_fota_cb);
-    if (ret)
+    if(s_ota_flag == -1)
     {
-        LOGE("ql_abfota_start_update init fail");
-        return -1;
+        ret = mbtk_fota_init(ql_fota_cb);
+        if (ret)
+        {
+            LOGE("ql_abfota_start_update init fail");
+            return -1;
+        }
+        else
+        {
+            s_ota_flag = 0;
+        }
     }
 
     memset(addr_buf, 0, sizeof(addr_buf));