You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2017/11/20 19:31:44 UTC

[GitHub] mkiiskila closed pull request #669: imgmgr; imgmr_erase() and imgr_upload() had shared code regarding

mkiiskila closed pull request #669: imgmgr; imgmr_erase() and imgr_upload() had shared code regarding
URL: https://github.com/apache/mynewt-core/pull/669
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/mgmt/imgmgr/include/imgmgr/imgmgr.h b/mgmt/imgmgr/include/imgmgr/imgmgr.h
index c766f86d4..88a490534 100644
--- a/mgmt/imgmgr/include/imgmgr/imgmgr.h
+++ b/mgmt/imgmgr/include/imgmgr/imgmgr.h
@@ -73,6 +73,7 @@ uint8_t imgmgr_state_flags(int query_slot);
 int imgmgr_state_slot_in_use(int slot);
 int imgmgr_state_set_pending(int slot, int permanent);
 int imgmgr_state_confirm(void);
+int imgmgr_find_best_area_id(void);
 
 #ifdef __cplusplus
 }
diff --git a/mgmt/imgmgr/src/imgmgr.c b/mgmt/imgmgr/src/imgmgr.c
index d59aa9ce0..fb4272561 100644
--- a/mgmt/imgmgr/src/imgmgr.c
+++ b/mgmt/imgmgr/src/imgmgr.c
@@ -264,15 +264,13 @@ imgr_find_by_hash(uint8_t *find, struct image_version *ver)
     return -1;
 }
 
-static int
-imgr_erase(struct mgmt_cbuf *cb)
+int
+imgmgr_find_best_area_id(void)
 {
     struct image_version ver;
-    int area_id;
     int best = -1;
-    int rc;
     int i;
-    CborError g_err = CborNoError;
+    int rc;
 
     for (i = 0; i < 2; i++) {
         rc = imgr_read_info(i, &ver, NULL, NULL);
@@ -282,7 +280,7 @@ imgr_erase(struct mgmt_cbuf *cb)
         if (rc == 0) {
             /* Image in slot is ok. */
             if (imgmgr_state_slot_in_use(i)) {
-                /* Slot is in use; can't erase to this. */
+                /* Slot is in use; can't use this. */
                 continue;
             } else {
                 /*
@@ -297,7 +295,20 @@ imgr_erase(struct mgmt_cbuf *cb)
         break;
     }
     if (best >= 0) {
-        area_id = flash_area_id_from_image_slot(best);
+        best = flash_area_id_from_image_slot(best);
+    }
+    return best;
+}
+
+static int
+imgr_erase(struct mgmt_cbuf *cb)
+{
+    int area_id;
+    int rc;
+    CborError g_err = CborNoError;
+
+    area_id = imgmgr_find_best_area_id();
+    if (area_id >= 0) {
         if (imgr_state.upload.fa) {
             flash_area_close(imgr_state.upload.fa);
             imgr_state.upload.fa = NULL;
@@ -317,10 +328,6 @@ imgr_erase(struct mgmt_cbuf *cb)
         return MGMT_ERR_ENOMEM;
     }
 
-    if (!imgr_state.upload.fa) {
-        return MGMT_ERR_EINVAL;
-    }
-
     g_err |= cbor_encode_text_stringz(&cb->encoder, "rc");
     g_err |= cbor_encode_int(&cb->encoder, MGMT_ERR_EOK);
 
@@ -359,12 +366,9 @@ imgr_upload(struct mgmt_cbuf *cb)
         },
         [3] = { 0 },
     };
-    struct image_version ver;
     struct image_header *hdr;
     int area_id;
-    int best;
     int rc;
-    int i;
     bool empty = false;
     CborError g_err = CborNoError;
 
@@ -390,32 +394,9 @@ imgr_upload(struct mgmt_cbuf *cb)
          */
         imgr_state.upload.off = 0;
         imgr_state.upload.size = size;
-        best = -1;
 
-        for (i = 0; i < 2; i++) {
-            rc = imgr_read_info(i, &ver, NULL, NULL);
-            if (rc < 0) {
-                continue;
-            }
-            if (rc == 0) {
-                /* Image in slot is ok. */
-                if (imgmgr_state_slot_in_use(i)) {
-                    /* Slot is in use; can't upload to this. */
-                    continue;
-                } else {
-                    /*
-                     * Not active slot, but image is ok. Use it if there are
-                     * no better candidates.
-                     */
-                    best = i;
-                }
-                continue;
-            }
-            best = i;
-            break;
-        }
-        if (best >= 0) {
-            area_id = flash_area_id_from_image_slot(best);
+        area_id = imgmgr_find_best_area_id();
+        if (area_id >= 0) {
             if (imgr_state.upload.fa) {
                 flash_area_close(imgr_state.upload.fa);
                 imgr_state.upload.fa = NULL;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services