You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/05/19 23:53:13 UTC

[2/3] incubator-mynewt-core git commit: imgmgr; send response via newtmgr on failures. Add rc to all responses (except to image list). Reject image uploads if image is missing magic number.

imgmgr; send response via newtmgr on failures.
Add rc to all responses (except to image list).
Reject image uploads if image is missing magic number.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/910a7c69
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/910a7c69
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/910a7c69

Branch: refs/heads/develop
Commit: 910a7c692bb97c647f33490ea17fa4cc38fe246f
Parents: ac2c20a
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 29 16:36:01 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu May 19 16:50:29 2016 -0700

----------------------------------------------------------------------
 libs/imgmgr/src/imgmgr.c      | 57 ++++++++++++++++++++++++--------
 libs/imgmgr/src/imgmgr_boot.c | 66 ++++++++++++++++++++++++++++++++------
 libs/imgmgr/src/imgmgr_fs.c   | 65 ++++++++++++++++++++++++++-----------
 3 files changed, 148 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/910a7c69/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index b0a2f32..f583d51 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -337,15 +337,16 @@ imgr_upload(struct nmgr_jbuf *njb)
 
     rc = json_read_object(&njb->njb_buf, off_attr);
     if (rc || off == UINT_MAX) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
     len = strlen(img_data);
-    if (!len) {
-        return OS_EINVAL;
-    }
-    len = base64_decode(img_data, img_data);
-    if (len < 0) {
-        return OS_EINVAL;
+    if (len) {
+        len = base64_decode(img_data, img_data);
+        if (len < 0) {
+            rc = NMGR_ERR_EINVAL;
+            goto err;
+        }
     }
 
     if (off == 0) {
@@ -353,9 +354,14 @@ imgr_upload(struct nmgr_jbuf *njb)
             /*
              * Image header is the first thing in the image.
              */
-            return OS_EINVAL;
+            rc = NMGR_ERR_EINVAL;
+            goto err;
         }
         hdr = (struct image_header *)img_data;
+        if (hdr->ih_magic != IMAGE_MAGIC) {
+            rc = NMGR_ERR_EINVAL;
+            goto err;
+        }
 
         /*
          * New upload.
@@ -402,8 +408,15 @@ imgr_upload(struct nmgr_jbuf *njb)
             best = i;
         }
         if (best >= 0) {
+            if (imgr_state.upload.fa) {
+                flash_area_close(imgr_state.upload.fa);
+                imgr_state.upload.fa = NULL;
+            }
             rc = flash_area_open(best, &imgr_state.upload.fa);
-            assert(rc == 0);
+            if (rc) {
+                rc = NMGR_ERR_EINVAL;
+                goto err;
+            }
             /*
              * XXXX only erase if needed.
              */
@@ -413,24 +426,32 @@ imgr_upload(struct nmgr_jbuf *njb)
             /*
              * No slot where to upload!
              */
-            return OS_EINVAL;
+            rc = NMGR_ERR_ENOMEM;
+            goto err;
         }
     } else if (off != imgr_state.upload.off) {
         /*
          * Invalid offset. Drop the data, and respond with the offset we're
          * expecting data for.
          */
-        rc = 0;
         goto out;
     }
 
-    if (len && imgr_state.upload.fa) {
+    if (!imgr_state.upload.fa) {
+        rc = NMGR_ERR_EINVAL;
+        goto err;
+    }
+    if (len) {
         rc = flash_area_write(imgr_state.upload.fa, imgr_state.upload.off,
           img_data, len);
-        assert(rc == 0);
+        if (rc) {
+            rc = NMGR_ERR_EINVAL;
+            goto err_close;
+        }
         imgr_state.upload.off += len;
         if (imgr_state.upload.size == imgr_state.upload.off) {
             /* Done */
+            flash_area_close(imgr_state.upload.fa);
             imgr_state.upload.fa = NULL;
         }
     }
@@ -439,11 +460,21 @@ out:
 
     json_encode_object_start(enc);
 
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(enc, "rc", &jv);
+
     JSON_VALUE_UINT(&jv, imgr_state.upload.off);
     json_encode_object_entry(enc, "off", &jv);
+
     json_encode_object_finish(enc);
 
     return 0;
+err_close:
+    flash_area_close(imgr_state.upload.fa);
+    imgr_state.upload.fa = NULL;
+err:
+    nmgr_jbuf_setoerr(njb, rc);
+    return 0;
 }
 
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/910a7c69/libs/imgmgr/src/imgmgr_boot.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_boot.c b/libs/imgmgr/src/imgmgr_boot.c
index 2927f3e..05cfa59 100644
--- a/libs/imgmgr/src/imgmgr_boot.c
+++ b/libs/imgmgr/src/imgmgr_boot.c
@@ -66,6 +66,7 @@ imgr_boot_read(struct nmgr_jbuf *njb)
     int rc;
     struct json_encoder *enc;
     struct image_version ver;
+    struct json_value jv;
     uint8_t hash[IMGMGR_HASH_LEN];
 
     enc = &njb->njb_enc;
@@ -87,6 +88,9 @@ imgr_boot_read(struct nmgr_jbuf *njb)
         imgr_ver_jsonstr(enc, "active", &ver);
     }
 
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(enc, "rc", &jv);
+
     json_encode_object_finish(enc);
 
     return 0;
@@ -108,28 +112,48 @@ imgr_boot_write(struct nmgr_jbuf *njb)
             .attribute = NULL
         }
     };
+    struct json_encoder *enc;
+    struct json_value jv;
     int rc;
     struct image_version ver;
 
     rc = json_read_object(&njb->njb_buf, boot_write_attr);
     if (rc) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
 
     rc = imgr_ver_parse(boot_write_attr[0].addr.string, &ver);
     if (rc) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
 
     rc = imgr_find_by_ver(&ver, hash);
     if (rc < 0) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
     rc = boot_vect_write_test(&ver);
     if (rc) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
-    return rc;
+
+    enc = &njb->njb_enc;
+
+    json_encode_object_start(enc);
+
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(enc, "rc", &jv);
+
+    json_encode_object_finish(enc);
+
+    return 0;
+
+err:
+    nmgr_jbuf_setoerr(njb, rc);
+    return 0;
 }
 
 int
@@ -138,6 +162,7 @@ imgr_boot2_read(struct nmgr_jbuf *njb)
     int rc;
     struct json_encoder *enc;
     struct image_version ver;
+    struct json_value jv;
     uint8_t hash[IMGMGR_HASH_LEN];
 
     enc = &njb->njb_enc;
@@ -165,6 +190,9 @@ imgr_boot2_read(struct nmgr_jbuf *njb)
         imgr_hash_jsonstr(enc, "active", hash);
     }
 
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(enc, "rc", &jv);
+
     json_encode_object_finish(enc);
 
     return 0;
@@ -186,12 +214,15 @@ imgr_boot2_write(struct nmgr_jbuf *njb)
             .attribute = NULL
         }
     };
+    struct json_encoder *enc;
+    struct json_value jv;
     int rc;
     struct image_version ver;
 
     rc = json_read_object(&njb->njb_buf, boot_write_attr);
     if (rc) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
 
     base64_decode(hash_str, hash);
@@ -199,10 +230,27 @@ imgr_boot2_write(struct nmgr_jbuf *njb)
     if (rc >= 0) {
         rc = boot_vect_write_test(&ver);
         if (rc) {
-            return OS_EINVAL;
+            rc = NMGR_ERR_EUNKNOWN;
+            goto err;
         }
     } else {
-        rc = OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
-    return rc;
+
+    enc = &njb->njb_enc;
+
+    json_encode_object_start(enc);
+
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+
+    json_encode_object_finish(enc);
+
+    return 0;
+
+err:
+    nmgr_jbuf_setoerr(njb, rc);
+
+    return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/910a7c69/libs/imgmgr/src/imgmgr_fs.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_fs.c b/libs/imgmgr/src/imgmgr_fs.c
index 3ba5057..9ca3c7b 100644
--- a/libs/imgmgr/src/imgmgr_fs.c
+++ b/libs/imgmgr/src/imgmgr_fs.c
@@ -6,7 +6,7 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *  http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
@@ -62,21 +62,25 @@ imgr_file_download(struct nmgr_jbuf *njb)
 
     rc = json_read_object(&njb->njb_buf, dload_attr);
     if (rc || off == UINT_MAX) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
 
     rc = fs_open(tmp_str, FS_ACCESS_READ, &file);
     if (rc || !file) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_ENOMEM;
+        goto err;
     }
 
     rc = fs_seek(file, off);
     if (rc) {
-        goto err;
+        rc = NMGR_ERR_EUNKNOWN;
+        goto err_close;
     }
     rc = fs_read(file, 32, tmp_str, &out_len);
     if (rc) {
-        goto err;
+        rc = NMGR_ERR_EUNKNOWN;
+        goto err_close;
     }
 
     out_len = base64_encode(tmp_str, out_len, img_data, 1);
@@ -96,12 +100,18 @@ imgr_file_download(struct nmgr_jbuf *njb)
     }
     fs_close(file);
 
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+
     json_encode_object_finish(enc);
 
     return 0;
-err:
+
+err_close:
     fs_close(file);
-    return OS_EINVAL;
+err:
+    nmgr_jbuf_setoerr(njb, rc);
+    return 0;
 }
 
 int
@@ -144,13 +154,15 @@ imgr_file_upload(struct nmgr_jbuf *njb)
 
     rc = json_read_object(&njb->njb_buf, off_attr);
     if (rc || off == UINT_MAX) {
-        return OS_EINVAL;
+        rc = NMGR_ERR_EINVAL;
+        goto err;
     }
     len = strlen(img_data);
     if (len) {
         len = base64_decode(img_data, img_data);
         if (len < 0) {
-            return OS_EINVAL;
+            rc = NMGR_ERR_EINVAL;
+            goto err;
         }
     }
 
@@ -162,30 +174,36 @@ imgr_file_upload(struct nmgr_jbuf *njb)
         imgr_state.upload.size = size;
 
         if (!strlen(file_name)) {
-            return OS_EINVAL;
+            rc = NMGR_ERR_EINVAL;
+            goto err;
+        }
+        if (imgr_state.upload.file) {
+            fs_close(imgr_state.upload.file);
+            imgr_state.upload.file = NULL;
         }
         rc = fs_open(file_name, FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE,
           &imgr_state.upload.file);
         if (rc) {
-            return OS_EINVAL;
+            rc = NMGR_ERR_EINVAL;
+            goto err;
         }
     } else if (off != imgr_state.upload.off) {
         /*
          * Invalid offset. Drop the data, and respond with the offset we're
          * expecting data for.
          */
-        rc = 0;
         goto out;
     }
 
-    if (len && imgr_state.upload.file) {
+    if (!imgr_state.upload.file) {
+        rc = NMGR_ERR_EINVAL;
+        goto err;
+    }
+    if (len) {
         rc = fs_write(imgr_state.upload.file, img_data, len);
         if (rc) {
-            /*
-            fs_close(imgr_state.upload.file);
-            */
-            imgr_state.upload.file = NULL;
-            return OS_EINVAL;
+            rc = NMGR_ERR_EINVAL;
+            goto err_close;
         }
         imgr_state.upload.off += len;
         if (imgr_state.upload.size == imgr_state.upload.off) {
@@ -199,11 +217,22 @@ out:
 
     json_encode_object_start(enc);
 
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+
     JSON_VALUE_UINT(&jv, imgr_state.upload.off);
     json_encode_object_entry(enc, "off", &jv);
+
     json_encode_object_finish(enc);
 
     return 0;
+
+err_close:
+    fs_close(imgr_state.upload.file);
+    imgr_state.upload.file = NULL;
+err:
+    nmgr_jbuf_setoerr(njb, rc);
+    return 0;
 }
 
 #endif