Browse Source

Linux_SDK_V1.0.3

thead_admin 1 year ago
parent
commit
2056ebbf7e

+ 22 - 2
components/fota/fota/fota.c

@@ -108,6 +108,8 @@ static void fota_fail(fota_t *fota, fota_info_t *info)
 
 static int fota_prepare(fota_t *fota)
 {
+    int seek;
+
     if (!(fota->from_path && fota->to_path)) {
         LOGE(TAG, "fota->from_path or fota->to_path is NULL");
         return -EINVAL;
@@ -128,6 +130,8 @@ static int fota_prepare(fota_t *fota)
         goto error;
     }
 
+    seek = 0;
+do_seek:
     if (aos_kv_getint(KV_FOTA_OFFSET, &fota->offset) < 0) {
         if (aos_kv_setint(KV_FOTA_OFFSET, 0) < 0) {
             goto error;
@@ -139,12 +143,28 @@ static int fota_prepare(fota_t *fota)
 
     if (netio_seek(fota->from, fota->offset, SEEK_SET) != 0) {
         LOGD(TAG, "from seek error");
-        goto error;
+        fota->offset = 0;
+        if (aos_kv_setint(KV_FOTA_OFFSET, 0) < 0) {
+            goto error;
+        }
+        if (seek) {
+            goto error;
+        }
+        seek = 1;
+        goto do_seek;
     }
 
     if (netio_seek(fota->to, fota->offset, SEEK_SET) != 0) {
         LOGD(TAG, "to seek error");
-        goto error;
+        fota->offset = 0;
+        if (aos_kv_setint(KV_FOTA_OFFSET, 0) < 0) {
+            goto error;
+        }
+        if (seek) {
+            goto error;
+        }
+        seek = 1;
+        goto do_seek;
     }
 
     fota->status = FOTA_DOWNLOAD;

+ 25 - 1
solutions/fota-service/porting/flash.c

@@ -397,6 +397,18 @@ static int get_img_index(netio_t *io, size_t cur_offset)
     return -1;
 }
 
+static size_t get_img_length(netio_t *io)
+{
+    int i;
+    size_t length;
+
+    download_img_info_t *priv = (download_img_info_t *)io->private;
+    for (i = 0, length = priv->head_size; i < priv->image_count; i++) {
+        length += priv->img_info[i].img_size;
+    }
+    return length;
+}
+
 static int flash_open(netio_t *io, const char *path)
 {
     io->block_size = CONFIG_FOTA_BUFFER_SIZE;
@@ -508,7 +520,13 @@ static int _file_write(netio_t *io, int idx, uint8_t *buffer, int length)
              __func__, __LINE__, length, fp, fd, ret, errno);
         return -1;
     }
-    if (fp) fflush(fp);
+    if (fp) {
+        fflush(fp);
+        fsync(fileno(fp));
+    }
+    if (fd >= 0) {
+        fsync(fd);
+    }
     return ret;
 }
 
@@ -602,6 +620,8 @@ static int download_img_info_init_from_file(netio_t *io)
         ret = -1;
         goto out;
     }
+    close(fd);
+    fd = -1;
     ret = download_img_info_init(io, buffer, length, 0);
     if (ret < 0) {
         ret = -1;
@@ -692,6 +712,10 @@ static int flash_seek(netio_t *io, size_t offset, int whence)
             if (download_img_info_init_from_file(io) < 0) {
                 return -1;
             }
+            if (whence == SEEK_SET && offset >= get_img_length(io)) {
+                LOGE(TAG, "seek offset error.");
+                return -1;
+            }
         }
         idx = get_img_index(io, offset);
         if (idx < 0) {

+ 1 - 1
solutions/fota-service/porting/image.c

@@ -258,7 +258,7 @@ int set_fota_success_param(void)
     int bootlimit = 0, bootcount = 0;
     char buf[64] = {0}, cmd[128];
 
-    if ((fp = popen("fw_printenv -n upgrade_available", "r")) == NULL) {
+    if ((fp = popen("fw_printenv -n upgrade_available 2>/dev/null", "r")) == NULL) {
         return -1;
     }