You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ut...@apache.org on 2021/06/16 10:21:22 UTC

[mynewt-mcumgr] 03/03: zephyr: Improve and rename img_mgmt_find_best_area_id

This is an automated email from the ASF dual-hosted git repository.

utzig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-mcumgr.git

commit e71063a3c01d6ea8b73a06f14c1d3006f2de9e7a
Author: Dominik Ermel <do...@nordicsemi.no>
AuthorDate: Wed Jun 9 13:15:52 2021 +0000

    zephyr: Improve and rename img_mgmt_find_best_area_id
    
    The img_mgmt_find_best_area_id has been renamed to
    img_mgmt_get_unused_slot_area_id and now takes one integer parameter,
    that is a slot number to check.
    The parameter may be -1, in which case the function will check the
    first two slots for availability to perform DFU.  In case when
    positive integer is provided the function will address directly
    selected slot, and will perform the same tests, as in auto-select,
    for first two slots, but in case of slot numbers above 1, it will
    just check if the slot has been defined.
    
    Signed-off-by: Dominik Ermel <do...@nordicsemi.no>
---
 cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c | 66 +++++++++++++-------------
 1 file changed, 32 insertions(+), 34 deletions(-)

diff --git a/cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c b/cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c
index 9eb9450..be139c4 100644
--- a/cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c
+++ b/cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c
@@ -93,7 +93,7 @@ zephyr_img_mgmt_flash_check_empty(uint8_t fa_id, bool *out_empty)
 /**
  * Get flash_area ID for a image slot number.
  */
-static uint8_t
+static int
 zephyr_img_mgmt_flash_area_id(int slot)
 {
     uint8_t fa_id;
@@ -108,48 +108,46 @@ zephyr_img_mgmt_flash_area_id(int slot)
         break;
 
     default:
-        assert(0);
-        fa_id = FLASH_AREA_ID(image_1);
+        fa_id = -1;
         break;
     }
 
     return fa_id;
 }
 
+/**
+ * The function will check if given slot is available, and allowed, for DFU;
+ * providing -1 as a parameter means find any unused and non-active available;
+ * if checks area positive, then area ID is returned, -1 is returned otherwise.
+ * Note that auto-selection is performed only between two first slots.
+ */
 static int
-img_mgmt_find_best_area_id(void)
+img_mgmt_get_unused_slot_area_id(int slot)
 {
-    struct image_version ver;
-    int best = -1;
-    int i;
-    int rc;
-
-    for (i = 0; i < 2; i++) {
-        rc = img_mgmt_read_info(i, &ver, NULL, NULL);
-        if (rc < 0) {
-            continue;
-        }
-        if (rc == 0) {
-            /* Image in slot is ok. */
-            if (img_mgmt_slot_in_use(i)) {
-                /* Slot is in use; can't use this. */
-                continue;
-            } else {
-                /*
-                 * Not active slot, but image is ok. Use it if there are
-                 * no better candidates.
-                 */
-                best = i;
+    /* Auto select slot; note that this is performed only between two first
+     * slots, at this pointi, which will require fix when Direct-XIP, which may
+     * support more slots, gets support within Zephyr. */
+    if (slot < -1) {
+        return -1;
+    } else if (slot == -1) {
+        for (slot = 0; slot < 2; slot++) {
+            if (img_mgmt_slot_in_use(slot) == 0) {
+                int area_id = zephyr_img_mgmt_flash_area_id(slot);
+                if (area_id != -1) {
+                    return area_id;
+                }
             }
-            continue;
         }
-        best = i;
-        break;
+        return -1;
     }
-    if (best >= 0) {
-        best = zephyr_img_mgmt_flash_area_id(best);
+    /* Direct selection; the first two slots are checked for being available
+     * and unused; the all other slots are just checked for availability. */
+    if (slot < 2) {
+        slot = img_mgmt_slot_in_use(slot) == 0 ? slot : -1;
     }
-    return best;
+
+    /* Return area ID for the slot or -1 */
+    return slot != -1  ? zephyr_img_mgmt_flash_area_id(slot) : -1;
 }
 
 /**
@@ -194,8 +192,8 @@ img_mgmt_impl_erase_slot(void)
     bool empty;
     int rc, best_id;
 
-    /* Select non-active slot */
-    best_id = img_mgmt_find_best_area_id();
+    /* Select any non-active, unused slot */
+    best_id = img_mgmt_get_unused_slot_area_id(-1);
     if (best_id < 0) {
         return MGMT_ERR_EUNKNOWN;
     }
@@ -492,7 +490,7 @@ img_mgmt_impl_upload_inspect(const struct img_mgmt_upload_req *req,
             }
         }
 
-        action->area_id = img_mgmt_find_best_area_id();
+        action->area_id = img_mgmt_get_unused_slot_area_id(-1);
         if (action->area_id < 0) {
             /* No slot where to upload! */
             *errstr = img_mgmt_err_str_no_slot;