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/10/15 10:41:03 UTC

incubator-mynewt-core git commit: Boot loader - code cleanup.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop dc0fe7ff2 -> 231691369


Boot loader - code cleanup.


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

Branch: refs/heads/develop
Commit: 231691369af7d4566f72fc8df507f53e46543d5c
Parents: dc0fe7f
Author: Christopher Collins <cc...@apache.org>
Authored: Sat Oct 15 03:24:08 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Sat Oct 15 03:40:53 2016 -0700

----------------------------------------------------------------------
 boot/bootutil/include/bootutil/bootutil_misc.h |  7 ++---
 boot/bootutil/src/bootutil_misc.c              | 29 +++++++--------------
 boot/bootutil/src/loader.c                     | 16 +++---------
 boot/bootutil/test/src/boot_test.c             | 18 ++++++-------
 boot/split/include/split/split.h               |  2 ++
 boot/split/src/split.c                         | 13 +++++++++
 mgmt/imgmgr/src/imgmgr_cli.c                   |  2 +-
 mgmt/imgmgr/src/imgmgr_state.c                 | 15 ++++++-----
 sys/sysinit/include/sysinit/sysinit.h          |  2 +-
 9 files changed, 49 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/boot/bootutil/include/bootutil/bootutil_misc.h
----------------------------------------------------------------------
diff --git a/boot/bootutil/include/bootutil/bootutil_misc.h b/boot/bootutil/include/bootutil/bootutil_misc.h
index 87b0b28..a6c5357 100644
--- a/boot/bootutil/include/bootutil/bootutil_misc.h
+++ b/boot/bootutil/include/bootutil/bootutil_misc.h
@@ -31,11 +31,8 @@ extern "C" {
 #define BOOT_SWAP_TYPE_PERM     2
 
 int boot_swap_type(void);
-int boot_vect_write_test(int slot);
-int boot_vect_write_main(void);
-
-int boot_split_app_active_get(void);
-void boot_split_app_active_set(int active);
+int boot_set_pending(int slot);
+int boot_set_confirmed(void);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/boot/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c
index 0606215..d4fecdf 100644
--- a/boot/bootutil/src/bootutil_misc.c
+++ b/boot/bootutil/src/bootutil_misc.c
@@ -34,7 +34,6 @@
 #include "bootutil_priv.h"
 
 int boot_current_slot;
-int8_t boot_split_app_active;
 
 /*
  * Read the image trailer from a given slot.
@@ -93,13 +92,17 @@ boot_swap_type(void)
     }
 
     /* This should never happen. */
-    /* XXX: Remove this assert. */
+    /* XXX: This assert should be removed; it remains for now to help catch
+     * boot loader bugs.
+     */
     assert(0);
     return BOOT_SWAP_TYPE_NONE;
 }
 
 /**
- * Write the test image version number from the boot vector.
+ * Configures the system to test the image in the specified slot on the next
+ * reboot.  This image is only executed once unless it is confirmed while
+ * running.
  *
  * @param slot              The image slot to write to.  Note: this is an
  *                              image slot index, not a flash area ID.
@@ -107,7 +110,7 @@ boot_swap_type(void)
  * @return                  0 on success; nonzero on failure.
  */
 int
-boot_vect_write_test(int slot)
+boot_set_pending(int slot)
 {
     const struct flash_area *fap;
     uint32_t off;
@@ -131,14 +134,12 @@ boot_vect_write_test(int slot)
 }
 
 /**
- * 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.
+ * Confirms the currently-active image.
  *
  * @return                  0 on success; nonzero on failure.
  */
 int
-boot_vect_write_main(void)
+boot_set_confirmed(void)
 {
     const struct flash_area *fap;
     uint32_t off;
@@ -314,15 +315,3 @@ boot_set_copy_done(void)
     bit.bit_copy_done = 1;
     hal_flash_write(flash_id, off, &bit, 5);
 }
-
-int
-boot_split_app_active_get(void)
-{
-    return boot_split_app_active;
-}
-
-void
-boot_split_app_active_set(int active)
-{
-    boot_split_app_active = !!active;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/boot/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index f7d159e..fe9a272 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -327,9 +327,8 @@ boot_validate_state(int swap_type)
     return swap_type;
 }
 
-/*
+/**
  * How many sectors starting from sector[idx] can fit inside scratch.
- *
  */
 static uint32_t
 boot_copy_sz(int max_idx, int *cnt)
@@ -553,7 +552,6 @@ int
 boot_go(const struct boot_req *req, struct boot_rsp *rsp)
 {
     int swap_type;
-    int num_swaps;
     int slot;
 
     /* Set the global boot request object.  The remainder of the boot process
@@ -564,7 +562,6 @@ boot_go(const struct boot_req *req, struct boot_rsp *rsp)
     /* Attempt to read an image header from each slot. */
     boot_image_info();
 
-    num_swaps = 0;
     swap_type = boot_swap_type();
 
     /* Read the boot status to determine if an image copy operation was
@@ -572,26 +569,19 @@ boot_go(const struct boot_req *req, struct boot_rsp *rsp)
      * finish its task last time).
      */
     boot_read_status(&boot_state);
-    //if (boot_read_status(&boot_state)) {
-        ///* We are resuming an interrupted image copy. */
-        //boot_copy_image(swap_type);
-        //swap_type = BOOT_SWAP_TYPE_NONE;
-        //num_swaps++;
-    //}
 
     /* Check if we should initiate copy, or revert back to earlier image. */
     swap_type = boot_validate_state(swap_type);
 
     if (swap_type == BOOT_SWAP_TYPE_NONE) {
+        slot = 0;
         boot_state.idx = 0;
         boot_state.state = 0;
     } else {
-        num_swaps++;
+        slot = 1;
         boot_copy_image(swap_type);
     }
 
-    slot = num_swaps % 2;
-
     /* Always boot from the primary slot. */
     rsp->br_flash_id = boot_img[0].loc.bil_flash_id;
     rsp->br_image_addr = boot_img[0].loc.bil_address;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/boot/bootutil/test/src/boot_test.c
----------------------------------------------------------------------
diff --git a/boot/bootutil/test/src/boot_test.c b/boot/bootutil/test/src/boot_test.c
index cf69dcd..65715d6 100644
--- a/boot/bootutil/test/src/boot_test.c
+++ b/boot/bootutil/test/src/boot_test.c
@@ -630,7 +630,7 @@ TEST_CASE(boot_test_vm_ns_01)
     boot_test_util_write_image(&hdr, 1);
     boot_test_util_write_hash(&hdr, 1);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT(rc == 0);
 
     boot_test_util_verify_all(&req, BOOT_SWAP_TYPE_PERM, NULL, &hdr);
@@ -709,7 +709,7 @@ TEST_CASE(boot_test_vm_ns_11_b)
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT(rc == 0);
 
     boot_test_util_verify_all(&req, BOOT_SWAP_TYPE_TEMP, &hdr0, &hdr1);
@@ -751,7 +751,7 @@ TEST_CASE(boot_test_vm_ns_11_2areas)
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT(rc == 0);
 
     boot_test_util_verify_all(&req, BOOT_SWAP_TYPE_TEMP, &hdr0, &hdr1);
@@ -821,7 +821,7 @@ TEST_CASE(boot_test_nv_bs_11)
     boot_test_util_write_hash(&hdr0, 0);
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     boot_test_util_copy_area(5, BOOT_TEST_AREA_IDX_SCRATCH);
 
     boot_req_set(&req);
@@ -874,7 +874,7 @@ TEST_CASE(boot_test_nv_bs_11_2areas)
 
     boot_test_util_swap_areas(2, 5);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT_FATAL(rc == 0);
 
     status.idx = 1;
@@ -923,7 +923,7 @@ TEST_CASE(boot_test_vb_ns_11)
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT_FATAL(rc == 0);
 
     boot_test_util_verify_all(&req, BOOT_SWAP_TYPE_TEMP, &hdr0, &hdr1);
@@ -963,7 +963,7 @@ TEST_CASE(boot_test_no_hash)
     boot_test_util_write_hash(&hdr0, 0);
     boot_test_util_write_image(&hdr1, 1);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT_FATAL(rc == 0);
 
     boot_test_util_verify_all(&req, BOOT_SWAP_TYPE_NONE, &hdr0, NULL);
@@ -1004,7 +1004,7 @@ TEST_CASE(boot_test_no_flag_has_hash)
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT(rc == 0);
 
     boot_test_util_verify_all(&req, BOOT_SWAP_TYPE_NONE, &hdr0, NULL);
@@ -1052,7 +1052,7 @@ TEST_CASE(boot_test_invalid_hash)
       &tlv, sizeof(tlv));
     TEST_ASSERT(rc == 0);
 
-    rc = boot_vect_write_test(1);
+    rc = boot_set_pending(1);
     TEST_ASSERT(rc == 0);
 
     boot_test_util_verify_all(&req, BOOT_SWAP_TYPE_NONE, &hdr0, NULL);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/boot/split/include/split/split.h
----------------------------------------------------------------------
diff --git a/boot/split/include/split/split.h b/boot/split/include/split/split.h
index 3e12000..02c4023 100644
--- a/boot/split/include/split/split.h
+++ b/boot/split/include/split/split.h
@@ -73,6 +73,8 @@ int split_app_go(void **entry, int toboot);
 split_status_t split_check_status(void);
 split_mode_t split_mode_get(void);
 int split_mode_set(split_mode_t split_mode);
+int split_app_active_get(void);
+void split_app_active_set(int active);
 
 int split_write_split(split_mode_t mode);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/boot/split/src/split.c
----------------------------------------------------------------------
diff --git a/boot/split/src/split.c b/boot/split/src/split.c
index 62cca29..74bf516 100644
--- a/boot/split/src/split.c
+++ b/boot/split/src/split.c
@@ -31,6 +31,7 @@
 #define SPLIT_TOTAL_IMAGES  2
 
 static int8_t split_mode_cur;
+static int8_t split_app_active;
 
 void
 split_app_init(void)
@@ -71,6 +72,18 @@ split_mode_get(void)
 }
 
 int
+split_app_active_get(void)
+{
+    return split_app_active;
+}
+
+void
+split_app_active_set(int active)
+{
+    split_app_active = !!active;
+}
+
+int
 split_mode_set(split_mode_t split_mode)
 {
     if (split_mode < 0 || split_mode >= SPLIT_MODE_CNT) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/mgmt/imgmgr/src/imgmgr_cli.c
----------------------------------------------------------------------
diff --git a/mgmt/imgmgr/src/imgmgr_cli.c b/mgmt/imgmgr/src/imgmgr_cli.c
index ebc2353..b267369 100644
--- a/mgmt/imgmgr/src/imgmgr_cli.c
+++ b/mgmt/imgmgr/src/imgmgr_cli.c
@@ -99,7 +99,7 @@ imgr_cli_boot_set(char *hash_str)
         console_printf("Unknown img\n");
         return;
     }
-    rc = boot_vect_write_test(rc);
+    rc = boot_set_pending(rc);
     if (rc) {
         console_printf("Can't make img active\n");
         return;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/mgmt/imgmgr/src/imgmgr_state.c
----------------------------------------------------------------------
diff --git a/mgmt/imgmgr/src/imgmgr_state.c b/mgmt/imgmgr/src/imgmgr_state.c
index 3802b69..4bafa38 100644
--- a/mgmt/imgmgr/src/imgmgr_state.c
+++ b/mgmt/imgmgr/src/imgmgr_state.c
@@ -56,10 +56,13 @@ imgmgr_state_flags(int query_slot)
         break;
 
     case BOOT_SWAP_TYPE_TEMP:
-        if (query_slot == 1) {
+        if (query_slot == 0) {
+            flags |= IMGMGR_STATE_F_CONFIRMED;
+        } else if (query_slot == 1) {
             flags |= IMGMGR_STATE_F_PENDING;
         }
         break;
+
     case BOOT_SWAP_TYPE_PERM:
         if (query_slot == 0) {
             flags |= IMGMGR_STATE_F_ACTIVE;
@@ -73,7 +76,7 @@ imgmgr_state_flags(int query_slot)
      * currently running.
      */
     /* XXX: The slot 0 assumption only holds when running from flash. */
-    if (query_slot == 0 || boot_split_app_active_get()) {
+    if (query_slot == 0 || split_app_active_get()) {
         flags |= IMGMGR_STATE_F_ACTIVE;
     }
 
@@ -138,7 +141,7 @@ imgmgr_state_test_slot(int slot)
     int rc;
 
     state_flags = imgmgr_state_flags(slot);
-    split_app_active = boot_split_app_active_get();
+    split_app_active = split_app_active_get();
 
     /* Unconfirmed slots are always testable.  A confirmed slot can only be
      * tested if it is a loader in a split image setup.
@@ -158,7 +161,7 @@ imgmgr_state_test_slot(int slot)
         /* Unified image or loader. */
         if (!split_app_active) {
             /* No change in split status. */
-            rc = boot_vect_write_test(slot);
+            rc = boot_set_pending(slot);
             if (rc != 0) {
                 return MGMT_ERR_EUNKNOWN;
             }
@@ -188,13 +191,13 @@ imgmgr_state_confirm(void)
     }
 
     /* Confirm the unified image or loader in slot 0. */
-    rc = boot_vect_write_main();
+    rc = boot_set_confirmed();
     if (rc != 0) {
         return MGMT_ERR_EUNKNOWN;
     }
 
     /* If a split app in slot 1 is active, confirm it as well. */
-    if (boot_split_app_active_get()) {
+    if (split_app_active_get()) {
         rc = split_write_split(SPLIT_MODE_APP);
         if (rc != 0) {
             return MGMT_ERR_EUNKNOWN;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/23169136/sys/sysinit/include/sysinit/sysinit.h
----------------------------------------------------------------------
diff --git a/sys/sysinit/include/sysinit/sysinit.h b/sys/sysinit/include/sysinit/sysinit.h
index 4e6fcb7..4f0e432 100644
--- a/sys/sysinit/include/sysinit/sysinit.h
+++ b/sys/sysinit/include/sysinit/sysinit.h
@@ -57,7 +57,7 @@ void sysinit_app(void);
 #define sysinit() do                                                        \
 {                                                                           \
     /* Record that a split app is running; imgmgt needs to know this. */    \
-    boot_split_app_active_set(1);                                           \
+    split_app_active_set(1);                                           \
     sysinit_app();                                                          \
 } while (0)