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/08/19 22:12:10 UTC

[11/12] incubator-mynewt-core git commit: bootutil/imgmgr; output of boot now shows the fallback image.

bootutil/imgmgr; output of boot now shows the fallback image.


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/1869d9b8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/1869d9b8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/1869d9b8

Branch: refs/heads/develop
Commit: 1869d9b8c7ab4200e151a598bd9a12fd4acb69cf
Parents: 183be3a
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Aug 18 21:59:14 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Fri Aug 19 15:11:50 2016 -0700

----------------------------------------------------------------------
 libs/bootutil/include/bootutil/bootutil_misc.h |   6 +-
 libs/bootutil/src/bootutil_misc.c              | 117 ++++++++++++--------
 libs/imgmgr/src/imgmgr.c                       |   2 +-
 libs/imgmgr/src/imgmgr_boot.c                  |  11 +-
 4 files changed, 83 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1869d9b8/libs/bootutil/include/bootutil/bootutil_misc.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/include/bootutil/bootutil_misc.h b/libs/bootutil/include/bootutil/bootutil_misc.h
index c018bb8..2e3049d 100644
--- a/libs/bootutil/include/bootutil/bootutil_misc.h
+++ b/libs/bootutil/include/bootutil/bootutil_misc.h
@@ -20,11 +20,9 @@
 #ifndef __BOOTUTIL_MISC_H_
 #define __BOOTUTIL_MISC_H_
 
-struct image_version;
 int boot_vect_read_test(int *slot);
-int boot_vect_read_main(struct image_version *out_ver);
+int boot_vect_read_main(int *slot);
 int boot_vect_write_test(int slot);
-int boot_vect_write_main(struct image_version *ver);
-void boot_confirm_ok(void);
+int boot_vect_write_main(void);
 
 #endif /*  __BOOTUTIL_MISC_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1869d9b8/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index 595bb52..22ac921 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -26,10 +26,32 @@
 #include <os/os.h>
 #include <console/console.h>
 #include "bootutil/image.h"
+#include "bootutil/bootutil_misc.h"
 #include "bootutil_priv.h"
 
+/*
+ * Read the image trailer from a given slot.
+ */
+static int
+boot_vect_read_img_trailer(int slot, struct boot_img_trailer *bit)
+{
+    int rc;
+    const struct flash_area *fap;
+    uint32_t off;
+
+    rc = flash_area_open(slot, &fap);
+    if (rc) {
+        return rc;
+    }
+    off = fap->fa_size - sizeof(struct boot_img_trailer);
+    rc = flash_area_read(fap, off, bit, sizeof(*bit));
+    flash_area_close(fap);
+
+    return rc;
+}
+
 /**
- * Retrieves from the boot vector the version number of the test image (i.e.,
+ * Retrieves from the slot number of the test image (i.e.,
  * the image that has not been proven stable, and which will only run once).
  *
  * @param slot              On success, the slot number of image to boot.
@@ -39,19 +61,15 @@
 int
 boot_vect_read_test(int *slot)
 {
-    const struct flash_area *fap;
     struct boot_img_trailer bit;
     int i;
     int rc;
-    uint32_t off;
 
-    for (i = FLASH_AREA_IMAGE_1; i <= FLASH_AREA_IMAGE_1; i++) {
-        rc = flash_area_open(i, &fap);
-        if (rc) {
+    for (i = FLASH_AREA_IMAGE_0; i <= FLASH_AREA_IMAGE_1; i++) {
+        if (i == bsp_imgr_current_slot()) {
             continue;
         }
-        off = fap->fa_size - sizeof(struct boot_img_trailer);
-        rc = flash_area_read(fap, off, &bit, sizeof(bit));
+        rc = boot_vect_read_img_trailer(i, &bit);
         if (rc) {
             continue;
         }
@@ -64,16 +82,32 @@ boot_vect_read_test(int *slot)
 }
 
 /**
- * Retrieves from the boot vector the version number of the main image.
+ * Retrieves from the slot number of the main image. If this is
+ * different from test image slot, next restart will revert to main.
  *
  * @param out_ver           On success, the main version gets written here.
  *
  * @return                  0 on success; nonzero on failure.
  */
 int
-boot_vect_read_main(struct image_version *out_ver)
+boot_vect_read_main(int *slot)
 {
-    return -1;
+    int rc;
+    struct boot_img_trailer bit;
+
+    rc = boot_vect_read_img_trailer(FLASH_AREA_IMAGE_0, &bit);
+    assert(rc == 0);
+
+    if (bit.bit_copy_start != BOOT_IMG_MAGIC || bit.bit_img_ok != 0xff) {
+        /*
+         * If there never was copy that took place, or if the current
+         * image has been marked good, we'll keep booting it.
+         */
+        *slot = FLASH_AREA_IMAGE_0;
+    } else {
+        *slot = FLASH_AREA_IMAGE_1;
+    }
+    return 0;
 }
 
 /**
@@ -97,18 +131,43 @@ boot_vect_write_test(int slot)
     off = fap->fa_size - sizeof(struct boot_img_trailer);
     magic = BOOT_IMG_MAGIC;
 
-    return flash_area_write(fap, off, &magic, sizeof(magic));
+    rc = flash_area_write(fap, off, &magic, sizeof(magic));
+    flash_area_close(fap);
+
+    return rc;
 }
 
 /**
  * Deletes the main image version number from the boot vector.
+ * This must be called by the app to confirm that it is ok to keep booting
+ * to this image.
  *
  * @return                  0 on success; nonzero on failure.
  */
 int
-boot_vect_write_main(struct image_version *ver)
+boot_vect_write_main(void)
 {
-    return -1;
+    const struct flash_area *fap;
+    uint32_t off;
+    int rc;
+    uint8_t val;
+
+    /*
+     * Write to slot 0.
+     */
+    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
+    if (rc) {
+        return rc;
+    }
+
+    off = fap->fa_size - sizeof(struct boot_img_trailer);
+    off += (sizeof(uint32_t) + sizeof(uint8_t));
+    rc = flash_area_read(fap, off, &val, sizeof(val));
+    if (!rc && val == 0xff) {
+        val = 0;
+        rc = flash_area_write(fap, off, &val, sizeof(val));
+    }
+    return rc;
 }
 
 /**
@@ -261,33 +320,3 @@ boot_clear_status(void)
     off += sizeof(uint32_t);
     hal_flash_write(flash_id, off, &val, sizeof(val));
 }
-
-/*
- * This must be called by the app to confirm that it is ok to keep booting
- * to this image.
- *
- */
-void
-boot_confirm_ok(void)
-{
-    const struct flash_area *fap;
-    uint32_t off;
-    int rc;
-    uint8_t val;
-
-    /*
-     * Write to slot 0.
-     */
-    rc = flash_area_open(bsp_imgr_current_slot(), &fap);
-    if (rc) {
-        return;
-    }
-
-    off = fap->fa_size - sizeof(struct boot_img_trailer);
-    off += (sizeof(uint32_t) + sizeof(uint8_t));
-    rc = flash_area_read(fap, off, &val, sizeof(val));
-    if (!rc && val == 0xff) {
-        val = 0;
-        flash_area_write(fap, off, &val, sizeof(val));
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1869d9b8/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index aac8574..ebdf1a8 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -496,7 +496,7 @@ imgmgr_module_init(void)
     rc = nmgr_group_register(&imgr_nmgr_group);
     assert(rc == 0);
 
-    boot_confirm_ok();
+    boot_vect_write_main();
 
     return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1869d9b8/libs/imgmgr/src/imgmgr_boot.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_boot.c b/libs/imgmgr/src/imgmgr_boot.c
index c252b9c..f636a5e 100644
--- a/libs/imgmgr/src/imgmgr_boot.c
+++ b/libs/imgmgr/src/imgmgr_boot.c
@@ -80,9 +80,12 @@ imgr_boot_read(struct nmgr_jbuf *njb)
         }
     }
 
-    rc = boot_vect_read_main(&ver);
+    rc = boot_vect_read_main(&slot);
     if (!rc) {
-        imgr_ver_jsonstr(enc, "main", &ver);
+        rc = imgr_read_info(slot, &ver, hash);
+        if (!rc) {
+            imgr_ver_jsonstr(enc, "main", &ver);
+        }
     }
 
     rc = imgr_read_info(bsp_imgr_current_slot(), &ver, hash);
@@ -179,9 +182,9 @@ imgr_boot2_read(struct nmgr_jbuf *njb)
         }
     }
 
-    rc = boot_vect_read_main(&ver);
+    rc = boot_vect_read_main(&slot);
     if (!rc) {
-        rc = imgr_find_by_ver(&ver, hash);
+        rc = imgr_read_info(slot, &ver, hash);
         if (rc >= 0) {
             imgr_hash_jsonstr(enc, "main", hash);
         }