You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/08/24 00:53:41 UTC

[27/50] [abbrv] incubator-mynewt-core git commit: bootutil; move routines reading boot-copy-status from loader.c to bootutil_misc.c.

bootutil; move routines reading boot-copy-status from loader.c to
bootutil_misc.c.


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

Branch: refs/heads/master
Commit: c29be5acf72dfebe61e5ac80b7c1daa53174dfef
Parents: 52a8e9f
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Aug 17 10:48:42 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Fri Aug 19 15:11:49 2016 -0700

----------------------------------------------------------------------
 libs/bootutil/src/bootutil_misc.c | 118 ++++++++++++++++++++++++++++++
 libs/bootutil/src/bootutil_priv.h |   5 ++
 libs/bootutil/src/loader.c        | 126 ++++++---------------------------
 3 files changed, 146 insertions(+), 103 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c29be5ac/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index 8ae5599..3885af9 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -22,6 +22,7 @@
 #include <hal/hal_flash.h>
 #include <hal/flash_map.h>
 #include <os/os.h>
+#include <console/console.h>
 #include "bootutil/image.h"
 #include "bootutil_priv.h"
 
@@ -120,3 +121,120 @@ boot_read_image_header(struct boot_image_location *loc,
     return rc;
 }
 
+/*
+ * How far has the copy progressed?
+ */
+static void
+boot_read_status_bytes(struct boot_status *bs, uint8_t flash_id, uint32_t off)
+{
+    uint8_t status;
+
+    off -= sizeof(status) * 2;
+    while (1) {
+        hal_flash_read(flash_id, off, &status, sizeof(status));
+        if (status == 0xff) {
+            break;
+        }
+        off--;
+        if (bs->state == 2) {
+            bs->idx++;
+            bs->state = 0;
+        } else {
+            bs->state++;
+        }
+    }
+}
+
+/**
+ * Reads the boot status from the flash.  The boot status contains
+ * the current state of an interrupted image copy operation.  If the boot
+ * status is not present, or it indicates that previous copy finished,
+ * there is no operation in progress.
+ */
+int
+boot_read_status(struct boot_status *bs)
+{
+    struct boot_img_trailer bit;
+    uint8_t flash_id;
+    uint32_t off;
+
+    /*
+     * Check if boot_img_trailer is in scratch, or at the end of slot0.
+     */
+    boot_slot_magic(0, &bit);
+    if (bit.bit_start == BOOT_IMG_MAGIC && bit.bit_done == 0xffffffff) {
+        boot_magic_loc(0, &flash_id, &off);
+        boot_read_status_bytes(bs, flash_id, off);
+        console_printf("status in slot0, %lu/%lu\n", bs->idx, bs->state);
+        return 1;
+    }
+    boot_scratch_magic(&bit);
+    if (bit.bit_start == BOOT_IMG_MAGIC && bit.bit_done == 0xffffffff) {
+        boot_scratch_loc(&flash_id, &off);
+        boot_read_status_bytes(bs, flash_id, off);
+        console_printf("status in scratch, %lu/%lu\n", bs->idx, bs->state);
+        return 1;
+    }
+    return 0;
+}
+
+#include <hal/hal_system.h>
+
+/**
+ * Writes the supplied boot status to the flash file system.  The boot status
+ * contains the current state of an in-progress image copy operation.
+ *
+ * @param bs                    The boot status to write.
+ *
+ * @return                      0 on success; nonzero on failure.
+ */
+int
+boot_write_status(struct boot_status *bs)
+{
+    uint32_t off;
+    uint8_t flash_id;
+    uint8_t val;
+
+    if (bs->idx == 0) {
+        /*
+         * Write to scratch
+         */
+        boot_scratch_loc(&flash_id, &off);
+    } else {
+        /*
+         * Write to slot 0;
+         */
+        boot_magic_loc(0, &flash_id, &off);
+    }
+    off -= ((3 * sizeof(uint8_t)) * bs->idx +
+      sizeof(uint8_t) * (bs->state + 1));
+
+    console_printf("status write, %lu/%lu -> %lx\n", bs->idx, bs->state, off);
+
+    val = bs->state;
+    hal_flash_write(flash_id, off, &val, sizeof(val));
+
+    return 0;
+}
+
+/**
+ * Finalizes the copy-in-progress status on the flash.  The boot status
+ * contains the current state of an in-progress image copy operation.  By
+ * clearing this, it is implied that there is no copy operation in
+ * progress.
+ */
+void
+boot_clear_status(void)
+{
+    uint32_t off;
+    uint32_t val = BOOT_IMG_MAGIC;
+    uint8_t flash_id;
+
+    /*
+     * Write to slot 0;
+     */
+    boot_magic_loc(0, &flash_id, &off);
+    off += sizeof(uint32_t);
+    hal_flash_write(flash_id, off, &val, sizeof(val));
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c29be5ac/libs/bootutil/src/bootutil_priv.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_priv.h b/libs/bootutil/src/bootutil_priv.h
index 3de5dd9..ffdfcb2 100644
--- a/libs/bootutil/src/bootutil_priv.h
+++ b/libs/bootutil/src/bootutil_priv.h
@@ -62,5 +62,10 @@ int boot_write_status(struct boot_status *bs);
 int boot_read_status(struct boot_status *bs);
 void boot_clear_status(void);
 
+void boot_magic_loc(int slot_num, uint8_t *flash_id, uint32_t *off);
+void boot_scratch_loc(uint8_t *flash_id, uint32_t *off);
+void boot_slot_magic(int slot_num, struct boot_img_trailer *bit);
+void boot_scratch_magic(struct boot_img_trailer *bit);
+
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c29be5ac/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index 65d0fc8..22187a7 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -24,7 +24,6 @@
 #include <hal/flash_map.h>
 #include <hal/hal_flash.h>
 #include <os/os_malloc.h>
-#include <console/console.h>
 #include "bootutil/loader.h"
 #include "bootutil/image.h"
 #include "bootutil/bootutil_misc.h"
@@ -67,7 +66,22 @@ boot_slot_addr(int slot_num, struct boot_image_location *loc)
     loc->bil_address = area_desc->fa_off;
 }
 
-static void
+/*
+ * Status about copy-in-progress is either in slot0 (target slot) or
+ * in scratch area. It is in scratch area if the process is currently
+ * moving the last sector within image.
+ *
+ * If the copy-in-progress status is within the image slot, it will
+ * be at the end of the area.
+ * If the sector containing the boot copy status is in scratch, it's
+ * offset from beginning of scratch depends on how much of the image
+ * fits inside the scratch area.
+ *
+ * We start copy from the end of image, so boot-copy-status is in
+ * scratch when the first area is being moved. Otherwise it will be
+ * in slot 0.
+ */
+void
 boot_magic_loc(int slot_num, uint8_t *flash_id, uint32_t *off)
 {
     struct boot_img *b;
@@ -77,7 +91,7 @@ boot_magic_loc(int slot_num, uint8_t *flash_id, uint32_t *off)
     *off = b->area + b->loc.bil_address - sizeof(struct boot_img_trailer);
 }
 
-static void
+void
 boot_scratch_loc(uint8_t *flash_id, uint32_t *off)
 {
     struct flash_area *scratch;
@@ -85,11 +99,15 @@ boot_scratch_loc(uint8_t *flash_id, uint32_t *off)
 
     scratch = &boot_req->br_area_descs[boot_req->br_scratch_area_idx];
     *flash_id = scratch->fa_flash_id;
+
+    /*
+     * Calculate where the boot status would be, if it was copied to scratch.
+     */
     *off = boot_copy_sz(boot_req->br_slot_areas[1], &cnt);
     *off += (scratch->fa_off - sizeof(struct boot_img_trailer));
 }
 
-static void
+void
 boot_slot_magic(int slot_num, struct boot_img_trailer *bit)
 {
     uint32_t off;
@@ -100,7 +118,7 @@ boot_slot_magic(int slot_num, struct boot_img_trailer *bit)
     hal_flash_read(flash_id, off, bit, sizeof(*bit));
 }
 
-static void
+void
 boot_scratch_magic(struct boot_img_trailer *bit)
 {
     uint32_t off;
@@ -400,104 +418,6 @@ boot_copy_image(void)
     return 0;
 }
 
-
-/*
- * Is copy in progress?
- */
-static void
-boot_read_status_bytes(struct boot_status *bs, uint8_t flash_id, uint32_t off)
-{
-    uint8_t status;
-
-    off -= sizeof(status) * 2;
-    while (1) {
-        hal_flash_read(flash_id, off, &status, sizeof(status));
-        if (status == 0xff) {
-            break;
-        }
-        off--;
-        if (bs->state == 2) {
-            bs->idx++;
-            bs->state = 0;
-        } else {
-            bs->state++;
-        }
-    }
-}
-
-int
-boot_read_status(struct boot_status *bs)
-{
-    struct boot_img_trailer bit;
-    uint8_t flash_id;
-    uint32_t off;
-
-    /*
-     * Check if boot_img_trailer is in scratch, or at the end of slot0.
-     */
-    boot_slot_magic(0, &bit);
-    if (bit.bit_start == BOOT_IMG_MAGIC && bit.bit_done == 0xffffffff) {
-        boot_magic_loc(0, &flash_id, &off);
-        boot_read_status_bytes(bs, flash_id, off);
-        console_printf("status in slot0, %lu/%lu\n", bs->idx, bs->state);
-        return 1;
-    }
-    boot_scratch_magic(&bit);
-    if (bit.bit_start == BOOT_IMG_MAGIC && bit.bit_done == 0xffffffff) {
-        boot_scratch_loc(&flash_id, &off);
-        boot_read_status_bytes(bs, flash_id, off);
-        console_printf("status in scratch, %lu/%lu\n", bs->idx, bs->state);
-        return 1;
-    }
-    return 0;
-}
-
-#include <hal/hal_system.h>
-
-int
-boot_write_status(struct boot_status *bs)
-{
-    uint32_t off;
-    uint8_t flash_id;
-    uint8_t val;
-
-    if (bs->idx == 0) {
-        /*
-         * Write to scratch
-         */
-        boot_scratch_loc(&flash_id, &off);
-    } else {
-        /*
-         * Write to slot 0;
-         */
-        boot_magic_loc(0, &flash_id, &off);
-    }
-    off -= ((3 * sizeof(uint8_t)) * bs->idx +
-      sizeof(uint8_t) * (bs->state + 1));
-
-    console_printf("status write, %lu/%lu -> %lx\n", bs->idx, bs->state, off);
-
-    val = bs->state;
-    hal_flash_write(flash_id, off, &val, sizeof(val));
-
-    return 0;
-}
-
-void
-boot_clear_status(void)
-{
-    uint32_t off;
-    uint32_t val = BOOT_IMG_MAGIC;
-    uint8_t flash_id;
-
-    /*
-     * Write to slot 0;
-     */
-    boot_magic_loc(0, &flash_id, &off);
-    off += sizeof(uint32_t);
-    hal_flash_write(flash_id, off, &val, sizeof(val));
-}
-
 /**
  * Prepares the booting process.  Based on the information provided in the
  * request object, this function moves images around in flash as appropriate,