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


The following commit(s) were added to refs/heads/master by this push:
     new 449bee7  Add image upload support for flash erased at 0
449bee7 is described below

commit 449bee75750ea4430d299b8da28c4f39b07f8b08
Author: Fabio Utzig <ut...@apache.org>
AuthorDate: Sun Nov 29 22:14:39 2020 -0300

    Add image upload support for flash erased at 0
    
    Add a new abstraction to the image port implementation to get the value
    a flash device is erased to, and update the image manager to expect the
    correct values when reading erased data at the image header, etc.
    
    Signed-off-by: Fabio Utzig <ut...@apache.org>
---
 cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h  |  3 +++
 cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c | 17 +++++++++++++++++
 cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c | 24 +++++++++++++++++++++++-
 cmd/img_mgmt/src/img_mgmt.c                    | 12 ++++++++++--
 4 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h 
b/cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h
index cff7dfa..53f60bf 100644
--- a/cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h
+++ b/cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h
@@ -154,6 +154,9 @@ int img_mgmt_impl_upload_inspect(const struct 
img_mgmt_upload_req *req,
                                  struct img_mgmt_upload_action *action,
                                  const char **errstr);
 
+#define ERASED_VAL_32(x) (((x) << 24) | ((x) << 16) | ((x) << 8) | (x))
+int img_mgmt_impl_erased_val(int slot, uint8_t *erased_val);
+
 int img_mgmt_impl_log_upload_start(int status);
 
 int img_mgmt_impl_log_upload_done(int status, const uint8_t *hashp);
diff --git a/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c 
b/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c
index b021638..8151001 100644
--- a/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c
+++ b/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c
@@ -527,6 +527,23 @@ img_mgmt_impl_swap_type(void)
     }
 }
 
+int
+img_mgmt_impl_erased_val(int slot, uint8_t *erased_val)
+{
+    const struct flash_area *fa;
+    int rc;
+
+    rc = flash_area_open(flash_area_id_from_image_slot(slot), &fa);
+    if (rc != 0) {
+      return MGMT_ERR_EUNKNOWN;
+    }
+
+    *erased_val = flash_area_erased_val(fa);
+    flash_area_close(fa);
+
+    return 0;
+}
+
 void
 img_mgmt_module_init(void)
 {
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 5e178d3..b4a4e3b 100644
--- a/cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c
+++ b/cmd/img_mgmt/port/zephyr/src/zephyr_img_mgmt.c
@@ -49,6 +49,8 @@ zephyr_img_mgmt_flash_check_empty(uint8_t fa_id, bool 
*out_empty)
     int bytes_to_read;
     int rc;
     int i;
+    uint8_t erased_val;
+    uint32_t erased_val_32;
 
     rc = flash_area_open(fa_id, &fa);
     if (rc != 0) {
@@ -57,6 +59,9 @@ zephyr_img_mgmt_flash_check_empty(uint8_t fa_id, bool 
*out_empty)
 
     assert(fa->fa_size % 4 == 0);
 
+    erased_val = flash_area_erased_val(fa);
+    erased_val_32 = ERASED_VAL_32(erased_val);
+
     end = fa->fa_size;
     for (addr = 0; addr < end; addr += sizeof data) {
         if (end - addr < sizeof data) {
@@ -72,7 +77,7 @@ zephyr_img_mgmt_flash_check_empty(uint8_t fa_id, bool 
*out_empty)
         }
 
         for (i = 0; i < bytes_to_read / 4; i++) {
-            if (data[i] != 0xffffffff) {
+            if (data[i] != erased_val_32) {
                 *out_empty = false;
                 flash_area_close(fa);
                 return 0;
@@ -552,3 +557,20 @@ img_mgmt_impl_upload_inspect(const struct 
img_mgmt_upload_req *req,
     action->proceed = true;
     return 0;
 }
+
+int
+img_mgmt_impl_erased_val(int slot, uint8_t *erased_val)
+{
+    const struct flash_area *fa;
+    int rc;
+
+    rc = flash_area_open(zephyr_img_mgmt_flash_area_id(slot), &fa);
+    if (rc != 0) {
+      return MGMT_ERR_EUNKNOWN;
+    }
+
+    *erased_val = flash_area_erased_val(fa);
+    flash_area_close(fa);
+
+    return 0;
+}
diff --git a/cmd/img_mgmt/src/img_mgmt.c b/cmd/img_mgmt/src/img_mgmt.c
index 249ba2b..029b4cc 100644
--- a/cmd/img_mgmt/src/img_mgmt.c
+++ b/cmd/img_mgmt/src/img_mgmt.c
@@ -137,21 +137,29 @@ img_mgmt_read_info(int image_slot, struct image_version 
*ver, uint8_t *hash,
     size_t data_off;
     size_t data_end;
     bool hash_found;
+    uint8_t erased_val;
+    uint32_t erased_val_32;
     int rc;
 
+    rc = img_mgmt_impl_erased_val(image_slot, &erased_val);
+    if (rc != 0) {
+        return MGMT_ERR_EUNKNOWN;
+    }
+
     rc = img_mgmt_impl_read(image_slot, 0, &hdr, sizeof hdr);
     if (rc != 0) {
         return MGMT_ERR_EUNKNOWN;
     }
 
     if (ver != NULL) {
-        memset(ver, 0xff, sizeof(*ver));
+        memset(ver, erased_val, sizeof(*ver));
     }
+    erased_val_32 = ERASED_VAL_32(erased_val);
     if (hdr.ih_magic == IMAGE_MAGIC) {
         if (ver != NULL) {
             memcpy(ver, &hdr.ih_ver, sizeof(*ver));
         }
-    } else if (hdr.ih_magic == 0xffffffff) {
+    } else if (hdr.ih_magic == erased_val_32) {
         return MGMT_ERR_ENOENT;
     } else {
         return MGMT_ERR_EUNKNOWN;

Reply via email to