mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject [02/14] incubator-mynewt-core git commit: bootutil; make boot_status persist write all data in single operation.
Date Mon, 18 Apr 2016 18:04:20 GMT
bootutil; make boot_status persist write all data in single operation.


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

Branch: refs/heads/develop
Commit: 9441a8e91b4dc34aefa3cd83b9ec2c8c3af93ff8
Parents: a006be6
Author: Marko Kiiskila <marko@runtime.io>
Authored: Tue Apr 12 16:02:05 2016 -0700
Committer: Marko Kiiskila <marko@runtime.io>
Committed: Mon Apr 18 10:44:56 2016 -0700

----------------------------------------------------------------------
 libs/bootutil/src/bootutil_misc.c | 44 ++++++------------
 libs/bootutil/src/bootutil_priv.h | 19 +++++---
 libs/bootutil/src/loader.c        | 85 ++++++++++++++--------------------
 3 files changed, 62 insertions(+), 86 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9441a8e9/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index b82f5d3..7a8e114 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -167,9 +167,7 @@ boot_read_image_headers(struct image_header *out_headers,
  * @return                      0 on success; nonzero on failure.
  */
 int
-boot_read_status(struct boot_status *out_status,
-                 struct boot_status_entry *out_entries,
-                 int num_areas)
+boot_read_status(struct boot_state *out_state, int num_areas)
 {
     struct fs_file *file;
     uint32_t bytes_read;
@@ -182,35 +180,28 @@ boot_read_status(struct boot_status *out_status,
         goto done;
     }
 
-    rc = fs_read(file, sizeof *out_status, out_status, &bytes_read);
-    if (rc != 0 || bytes_read != sizeof *out_status) {
+    rc = fs_read(file, sizeof *out_state, out_state, &bytes_read);
+    if (rc != 0 || bytes_read != sizeof *out_state) {
         rc = BOOT_EBADSTATUS;
         goto done;
     }
 
-    rc = fs_read(file, num_areas * sizeof *out_entries, out_entries,
-                   &bytes_read);
-    if (rc != 0 || bytes_read != num_areas * sizeof *out_entries) {
-        rc = BOOT_EBADSTATUS;
-        goto done;
+    if (out_state->status.bs_img1_length == 0xffffffff) {
+        out_state->status.bs_img1_length = 0;
     }
-
-    if (out_status->bs_img1_length == 0xffffffff) {
-        out_status->bs_img1_length = 0;
-    }
-    if (out_status->bs_img2_length == 0xffffffff) {
-        out_status->bs_img2_length = 0;
+    if (out_state->status.bs_img2_length == 0xffffffff) {
+        out_state->status.bs_img2_length = 0;
     }
 
     for (i = 0; i < num_areas; i++) {
-        if (out_entries[i].bse_image_num == 0 &&
-            out_status->bs_img1_length == 0) {
+        if (out_state->entries[i].bse_image_num == 0 &&
+            out_state->status.bs_img1_length == 0) {
 
             rc = BOOT_EBADSTATUS;
             goto done;
         }
-        if (out_entries[i].bse_image_num == 1 &&
-            out_status->bs_img2_length == 0) {
+        if (out_state->entries[i].bse_image_num == 1 &&
+            out_state->status.bs_img2_length == 0) {
 
             rc = BOOT_EBADSTATUS;
             goto done;
@@ -237,9 +228,7 @@ done:
  * @return                      0 on success; nonzero on failure.
  */
 int
-boot_write_status(const struct boot_status *status,
-                  const struct boot_status_entry *entries,
-                  int num_areas)
+boot_write_status(const struct boot_state *state, int num_areas)
 {
     struct fs_file *file;
     int rc;
@@ -250,18 +239,11 @@ boot_write_status(const struct boot_status *status,
         goto done;
     }
 
-    rc = fs_write(file, status, sizeof *status);
-    if (rc != 0) {
-        rc = BOOT_EFILE;
-        goto done;
-    }
-
-    rc = fs_write(file, entries, num_areas * sizeof *entries);
+    rc = fs_write(file, state, sizeof *state);
     if (rc != 0) {
         rc = BOOT_EFILE;
         goto done;
     }
-
     rc = 0;
 
 done:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9441a8e9/libs/bootutil/src/bootutil_priv.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_priv.h b/libs/bootutil/src/bootutil_priv.h
index 13e259e..2b4dc3e 100644
--- a/libs/bootutil/src/bootutil_priv.h
+++ b/libs/bootutil/src/bootutil_priv.h
@@ -51,6 +51,17 @@ struct boot_status_entry {
     uint8_t bse_part_num;
 };
 
+/**
+ * The boot status header read from the file system, or generated if not
+ * present on disk.  The boot status indicates the state of the image slots in
+ * case the system was restarted while images were being moved in flash.
+ */
+struct boot_state {
+	struct boot_status status;
+	/** The entries associated with the boot status header. */
+	struct boot_status_entry entries[0];
+};
+
 struct boot_image_location {
     uint8_t bil_flash_id;
     uint32_t bil_address;
@@ -63,12 +74,8 @@ int boot_vect_delete_main(void);
 void boot_read_image_headers(struct image_header *out_headers,
                              const struct boot_image_location *addresses,
                              int num_addresses);
-int boot_read_status(struct boot_status *out_status,
-                     struct boot_status_entry *out_entries,
-                     int num_areas);
-int boot_write_status(const struct boot_status *status,
-                      const struct boot_status_entry *entries,
-                      int num_areas);
+int boot_read_status(struct boot_state *out_state, int num_areas);
+int boot_write_status(const struct boot_state *state, int num_areas);
 void boot_clear_status(void);
 
 int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9441a8e9/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index 130264c..ae0c91a 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -38,15 +38,7 @@ static const struct boot_req *boot_req;
 /** Image headers read from flash. */
 struct image_header boot_img_hdrs[2];
 
-/**
- * The boot status header read from the file system, or generated if not
- * present on disk.  The boot status indicates the state of the image slots in
- * case the system was restarted while images were being moved in flash.
- */
-struct boot_status boot_status;
-
-/** The entries associated with the boot status header. */
-struct boot_status_entry *boot_status_entries;
+static struct boot_state *boot;
 
 /**
  * Calculates the flash offset of the specified image slot.
@@ -146,8 +138,8 @@ boot_find_image_part(int image_num, int part_num)
     int i;
 
     for (i = 0; i < boot_req->br_num_image_areas; i++) {
-        if (boot_status_entries[i].bse_image_num == image_num &&
-            boot_status_entries[i].bse_part_num == part_num) {
+        if (boot->entries[i].bse_image_num == image_num &&
+            boot->entries[i].bse_part_num == part_num) {
 
             return boot_req->br_image_areas[i];
         }
@@ -310,12 +302,11 @@ boot_move_area(int from_area_idx, int to_area_idx,
         return rc;
     }
 
-    boot_status_entries[src_image_idx].bse_image_num = BOOT_IMAGE_NUM_NONE;
-    boot_status_entries[src_image_idx].bse_part_num = BOOT_IMAGE_NUM_NONE;
-    boot_status_entries[dst_image_idx].bse_image_num = img_num;
-    boot_status_entries[dst_image_idx].bse_part_num = part_num;
-    rc = boot_write_status(&boot_status, boot_status_entries,
-                           boot_req->br_num_image_areas);
+    boot->entries[src_image_idx].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot->entries[src_image_idx].bse_part_num = BOOT_IMAGE_NUM_NONE;
+    boot->entries[dst_image_idx].bse_image_num = img_num;
+    boot->entries[dst_image_idx].bse_part_num = part_num;
+    rc = boot_write_status(boot, boot_req->br_num_image_areas);
     if (rc != 0) {
         return rc;
     }
@@ -376,12 +367,11 @@ boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
         return rc;
     }
 
-    boot_status_entries[scratch_image_idx] = boot_status_entries[image_idx_2];
-    boot_status_entries[image_idx_2].bse_image_num = BOOT_IMAGE_NUM_NONE;
-    boot_status_entries[image_idx_2].bse_part_num = BOOT_IMAGE_NUM_NONE;
+    boot->entries[scratch_image_idx] = boot->entries[image_idx_2];
+    boot->entries[image_idx_2].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot->entries[image_idx_2].bse_part_num = BOOT_IMAGE_NUM_NONE;
 
-    rc = boot_write_status(&boot_status, boot_status_entries,
-                           boot_req->br_num_image_areas);
+    rc = boot_write_status(boot, boot_req->br_num_image_areas);
     if (rc != 0) {
         return rc;
     }
@@ -396,11 +386,10 @@ boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
         return rc;
     }
 
-    boot_status_entries[image_idx_2] = boot_status_entries[image_idx_1];
-    boot_status_entries[image_idx_1].bse_image_num = BOOT_IMAGE_NUM_NONE;
-    boot_status_entries[image_idx_1].bse_part_num = BOOT_IMAGE_NUM_NONE;
-    rc = boot_write_status(&boot_status, boot_status_entries,
-                           boot_req->br_num_image_areas);
+    boot->entries[image_idx_2] = boot->entries[image_idx_1];
+    boot->entries[image_idx_1].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot->entries[image_idx_1].bse_part_num = BOOT_IMAGE_NUM_NONE;
+    rc = boot_write_status(boot, boot_req->br_num_image_areas);
     if (rc != 0) {
         return rc;
     }
@@ -415,11 +404,10 @@ boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
         return rc;
     }
 
-    boot_status_entries[image_idx_1] = boot_status_entries[scratch_image_idx];
-    boot_status_entries[scratch_image_idx].bse_image_num = BOOT_IMAGE_NUM_NONE;
-    boot_status_entries[scratch_image_idx].bse_part_num = BOOT_IMAGE_NUM_NONE;
-    rc = boot_write_status(&boot_status, boot_status_entries,
-                           boot_req->br_num_image_areas);
+    boot->entries[image_idx_1] = boot->entries[scratch_image_idx];
+    boot->entries[scratch_image_idx].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot->entries[scratch_image_idx].bse_part_num = BOOT_IMAGE_NUM_NONE;
+    rc = boot_write_status(boot, boot_req->br_num_image_areas);
     if (rc != 0) {
         return rc;
     }
@@ -458,7 +446,7 @@ boot_fill_slot(int img_num, uint32_t img_length, int start_area_idx)
             /* Determine what is currently in the destination area. */
             dst_image_area_idx = boot_find_image_area_idx(dst_area_idx);
 
-            if (boot_status_entries[dst_image_area_idx].bse_image_num ==
+            if (boot->entries[dst_image_area_idx].bse_image_num ==
                 BOOT_IMAGE_NUM_NONE) {
 
                 /* The destination doesn't contain anything useful; we don't
@@ -539,9 +527,9 @@ boot_build_status_one(int image_num, uint8_t flash_id, uint32_t addr,
     offset = 0;
     part_num = 0;
     while (offset < length) {
-        assert(boot_status_entries[i].bse_image_num == 0xff);
-        boot_status_entries[i].bse_image_num = image_num;
-        boot_status_entries[i].bse_part_num = part_num;
+        assert(boot->entries[i].bse_image_num == 0xff);
+        boot->entries[i].bse_image_num = image_num;
+        boot->entries[i].bse_part_num = part_num;
 
         offset += boot_req->br_area_descs[area_idx].nad_length;
         part_num++;
@@ -565,8 +553,8 @@ boot_build_status(void)
     uint32_t address;
     uint32_t len;
 
-    memset(boot_status_entries, 0xff,
-           boot_req->br_num_image_areas * sizeof *boot_status_entries);
+    memset(boot->entries, 0xff,
+           boot_req->br_num_image_areas * sizeof boot->entries[0]);
 
     if (boot_img_hdrs[0].ih_magic == IMAGE_MAGIC) {
         len = boot_img_hdrs[0].ih_img_size + boot_img_hdrs[0].ih_tlv_size;
@@ -574,7 +562,7 @@ boot_build_status(void)
         boot_slot_addr(0, &flash_id, &address);
         boot_build_status_one(0, flash_id, address, len);
     } else {
-        boot_status.bs_img1_length = 0;
+        boot->status.bs_img1_length = 0;
     }
 
     if (boot_img_hdrs[1].ih_magic == IMAGE_MAGIC) {
@@ -583,7 +571,7 @@ boot_build_status(void)
         boot_slot_addr(1, &flash_id, &address);
         boot_build_status_one(1, flash_id, address, len);
     } else {
-        boot_status.bs_img2_length = 0;
+        boot->status.bs_img2_length = 0;
     }
 }
 
@@ -615,18 +603,17 @@ boot_go(const struct boot_req *req, struct boot_rsp *rsp)
      * interrupted (i.e., the system was reset before the boot loader could
      * finish its task last time).
      */
-    boot_status_entries =
-        malloc(req->br_num_image_areas * sizeof *boot_status_entries);
-    if (boot_status_entries == NULL) {
+    boot = malloc(sizeof(struct boot_state) +
+	req->br_num_image_areas * sizeof boot->entries[0]);
+    if (boot == NULL) {
         return BOOT_ENOMEM;
     }
-    rc = boot_read_status(&boot_status, boot_status_entries,
-                          boot_req->br_num_image_areas);
+    rc = boot_read_status(boot, boot_req->br_num_image_areas);
     if (rc == 0) {
         /* We are resuming an interrupted image copy. */
         /* XXX if copy has not actually started yet, validate image */
-        rc = boot_copy_image(boot_status.bs_img1_length,
-                             boot_status.bs_img2_length);
+        rc = boot_copy_image(boot->status.bs_img1_length,
+                             boot->status.bs_img2_length);
         if (rc != 0) {
             /* We failed to put the images back together; there is really no
              * solution here.
@@ -684,8 +671,8 @@ boot_go(const struct boot_req *req, struct boot_rsp *rsp)
         /* The user wants to run the image in the secondary slot.  The contents
          * of this slot need to moved to the primary slot.
          */
-        rc = boot_copy_image(boot_status.bs_img1_length,
-                             boot_status.bs_img2_length);
+        rc = boot_copy_image(boot->status.bs_img1_length,
+                             boot->status.bs_img2_length);
 
         if (rc != 0) {
             /* We failed to put the images back together; there is really no


Mime
View raw message