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/04/18 20:04:24 UTC

[06/14] incubator-mynewt-core git commit: bootutil/imgmgr; switch to using sys/config to communicate with each other.

bootutil/imgmgr; switch to using sys/config to communicate with each other.


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

Branch: refs/heads/develop
Commit: d27f80e1cdde69cb318039ecb63b5ec0bc1c8dc1
Parents: 782dc45
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Apr 13 16:13:23 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:48:21 2016 -0700

----------------------------------------------------------------------
 apps/boot/pkg.yml                              |   1 +
 apps/boot/src/boot.c                           |  24 ++-
 apps/slinky/src/main.c                         |  20 +-
 libs/bootutil/include/bootutil/bootutil_misc.h |  31 +++
 libs/bootutil/src/bootutil_misc.c              | 215 ++++++++++----------
 libs/bootutil/src/bootutil_priv.h              |  11 +-
 libs/bootutil/src/loader.c                     |  93 +++++----
 libs/imgmgr/src/imgmgr.c                       |   8 +-
 libs/imgmgr/src/imgmgr_boot.c                  | 116 +++++++++++
 libs/imgmgr/src/imgmgr_fs.c                    | 114 -----------
 10 files changed, 352 insertions(+), 281 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/apps/boot/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/boot/pkg.yml b/apps/boot/pkg.yml
index ed0965c..cdc6128 100644
--- a/apps/boot/pkg.yml
+++ b/apps/boot/pkg.yml
@@ -27,6 +27,7 @@ pkg.keywords:
 
 pkg.features: bootloader
 pkg.deps:
+    - sys/config
     - fs/nffs
     - libs/bootutil
     - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/apps/boot/src/boot.c
----------------------------------------------------------------------
diff --git a/apps/boot/src/boot.c b/apps/boot/src/boot.c
index cd69642..9cee56d 100755
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@ -26,16 +26,25 @@
 #include <hal/hal_system.h>
 #include <hal/hal_flash.h>
 #include <log/log.h>
+#include <config/config.h>
+#include <config/config_file.h>
 #include "fs/fs.h"
 #include "nffs/nffs.h"
 #include "bootutil/image.h"
 #include "bootutil/loader.h"
+#include "bootutil/bootutil_misc.h"
 
 /* we currently need extra nffs_area_descriptors for booting since the
  * boot code uses these to keep track of which block to write and copy.*/
 #define BOOT_AREA_DESC_MAX  (256)
 #define AREA_DESC_MAX       (BOOT_AREA_DESC_MAX)
 
+#define MY_CONFIG_FILE "/cfg/run"
+
+static struct conf_file my_conf = {
+    .cf_name = MY_CONFIG_FILE
+};
+
 int
 main(void)
 {
@@ -94,15 +103,10 @@ main(void)
     rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, nffs_descs);
     assert(rc == 0);
 
-    nffs_config.nc_num_inodes = 50;
-    nffs_config.nc_num_blocks = 50;
-    nffs_config.nc_num_cache_blocks = 32;
-
     /*
      * Initializes the flash driver and file system for use by the boot loader.
      */
     rc = nffs_init();
-
     if (rc == 0) {
         /* Look for an nffs file system in internal flash.  If no file
          * system gets detected, all subsequent file operations will fail,
@@ -111,10 +115,14 @@ main(void)
         nffs_detect(nffs_descs);
     }
 
-    /* Create the boot directory if it doesn't already exist. */
-    fs_mkdir("/boot");
-
     log_init();
+    conf_init();
+
+    rc = conf_file_src(&my_conf);
+    assert(rc == 0);
+    rc = conf_file_dst(&my_conf);
+    assert(rc == 0);
+    bootutil_cfg_register();
 
     rc = boot_go(&req, &rsp);
     assert(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c
index 43086bd..3194df3 100755
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@ -25,10 +25,12 @@
 #include <log/log.h>
 #include <stats/stats.h>
 #include <config/config.h>
+#include <config/config_file.h>
 #include <hal/flash_map.h>
 #include <fs/fs.h>
 #include <nffs/nffs.h>
 #include <newtmgr/newtmgr.h>
+#include <bootutil/bootutil_misc.h>
 #include <imgmgr/imgmgr.h>
 #include <assert.h>
 #include <string.h>
@@ -77,6 +79,14 @@ struct os_sem g_test_sem;
 /* For LED toggling */
 int g_led_pin;
 
+/* configuration file */
+#define MY_CONFIG_DIR  "/cfg"
+#define MY_CONFIG_FILE "/cfg/run"
+
+static struct conf_file my_conf = {
+    .cf_name = MY_CONFIG_FILE
+};
+
 #define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
 #define DEFAULT_MBUF_MPOOL_NBUFS (10)
 
@@ -251,7 +261,6 @@ main(int argc, char **argv)
     rc = conf_register(&test_conf_handler);
     assert(rc == 0);
 
-
     log_init();
     cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
     log_cbmem_handler_init(&log_cbmem_handler, &cbmem);
@@ -292,6 +301,12 @@ main(int argc, char **argv)
         assert(rc == 0);
     }
 
+    fs_mkdir(MY_CONFIG_DIR);
+    rc = conf_file_src(&my_conf);
+    assert(rc == 0);
+    rc = conf_file_dst(&my_conf);
+    assert(rc == 0);
+
     shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
                     SHELL_MAX_INPUT_LEN);
 
@@ -299,11 +314,14 @@ main(int argc, char **argv)
 
     nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
     imgmgr_module_init();
+    bootutil_cfg_register();
 
     stats_module_init();
 
     flash_test_init();
 
+    conf_load();
+
     rc = init_tasks();
     os_start();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/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
new file mode 100644
index 0000000..ff42ac8
--- /dev/null
+++ b/libs/bootutil/include/bootutil/bootutil_misc.h
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __BOOTUTIL_MISC_H_
+#define __BOOTUTIL_MISC_H_
+
+struct image_version;
+int boot_vect_read_test(struct image_version *out_ver);
+int boot_vect_read_main(struct image_version *out_ver);
+int boot_vect_write_test(struct image_version *ver);
+int boot_vect_write_main(struct image_version *ver);
+
+void bootutil_cfg_register(void);
+
+#endif /*  __BOOTUTIL_MISC_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index 7a8e114..d81dafe 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -6,7 +6,7 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *  http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
@@ -19,24 +19,64 @@
 
 #include <string.h>
 #include <inttypes.h>
-#include "hal/hal_flash.h"
-#include "fs/fs.h"
-#include "fs/fsutil.h"
-#include "bootutil/crc32.h"
+#include <hal/hal_flash.h>
+#include <config/config.h>
+#include <os/os.h>
 #include "bootutil/image.h"
 #include "bootutil_priv.h"
 
+static int boot_conf_set(int argc, char **argv, char *val);
+
+static struct image_version boot_main;
+static struct image_version boot_test;
+static uint8_t boot_st_loaded;
+
+static struct conf_handler boot_conf_handler = {
+    .ch_name = "boot",
+    .ch_get = NULL,
+    .ch_set = boot_conf_set,
+    .ch_commit = NULL,
+    .ch_export = NULL,
+};
+
 static int
-boot_vect_read_one(struct image_version *ver, const char *path)
+boot_conf_set(int argc, char **argv, char *val)
 {
-    uint32_t bytes_read;
     int rc;
+    int len;
+
+    if (argc == 1) {
+        if (!strcmp(argv[0], "main")) {
+            len = sizeof(boot_main);
+            rc = conf_bytes_from_str(val, &boot_main, &len);
+        } else if (!strcmp(argv[0], "test")) {
+            len = sizeof(boot_test);
+            rc = conf_bytes_from_str(val, &boot_test, &len);
+        } else if (!strcmp(argv[0], "status")) {
+            len = boot_st_sz;
+            rc = conf_bytes_from_str(val, boot_st, &len);
+            if (rc == 0 && len > 0) {
+                boot_st_loaded = 1;
+            } else {
+                boot_st_loaded = 0;
+            }
+        } else {
+            rc = OS_ENOENT;
+        }
+    } else {
+        rc = OS_ENOENT;
+    }
+    return rc;
+}
 
-    rc = fsutil_read_file(path, 0, sizeof *ver, ver, &bytes_read);
-    if (rc != 0 || bytes_read != sizeof *ver) {
+static int
+boot_vect_read_one(struct image_version *dst, struct image_version *src)
+{
+    if (src->iv_major == 0 && src->iv_minor == 0 &&
+      src->iv_revision == 0 && src->iv_build_num == 0) {
         return BOOT_EBADVECT;
     }
-
+    memcpy(dst, src, sizeof(*dst));
     return 0;
 }
 
@@ -51,10 +91,7 @@ boot_vect_read_one(struct image_version *ver, const char *path)
 int
 boot_vect_read_test(struct image_version *out_ver)
 {
-    int rc;
-
-    rc = boot_vect_read_one(out_ver, BOOT_PATH_TEST);
-    return rc;
+    return boot_vect_read_one(out_ver, &boot_test);
 }
 
 /**
@@ -67,24 +104,41 @@ boot_vect_read_test(struct image_version *out_ver)
 int
 boot_vect_read_main(struct image_version *out_ver)
 {
-    int rc;
+    return boot_vect_read_one(out_ver, &boot_main);
+}
 
-    rc = boot_vect_read_one(out_ver, BOOT_PATH_MAIN);
-    return rc;
+int
+boot_vect_write_one(const char *name, struct image_version *ver)
+{
+    char str[CONF_STR_FROM_BYTES_LEN(sizeof(struct image_version))];
+    char *to_store;
+
+    if (!ver) {
+        to_store = NULL;
+    } else {
+        if (!conf_str_from_bytes(ver, sizeof(*ver), str, sizeof(str))) {
+            return -1;
+        }
+        to_store = str;
+    }
+    return conf_save_one(&boot_conf_handler, name, to_store);
 }
 
 /**
- * Deletes the test image version number from the boot vector.
+ * Write the test image version number from the boot vector.
  *
  * @return                  0 on success; nonzero on failure.
  */
 int
-boot_vect_delete_test(void)
+boot_vect_write_test(struct image_version *ver)
 {
-    int rc;
-
-    rc = fs_unlink(BOOT_PATH_TEST);
-    return rc;
+    if (!ver) {
+        memset(&boot_test, 0, sizeof(boot_test));
+        return boot_vect_write_one("test", NULL);
+    } else {
+        memcpy(&boot_test, ver, sizeof(boot_test));
+        return boot_vect_write_one("test", &boot_test);
+    }
 }
 
 /**
@@ -93,12 +147,15 @@ boot_vect_delete_test(void)
  * @return                  0 on success; nonzero on failure.
  */
 int
-boot_vect_delete_main(void)
+boot_vect_write_main(struct image_version *ver)
 {
-    int rc;
-
-    rc = fs_unlink(BOOT_PATH_MAIN);
-    return rc;
+    if (!ver) {
+        memset(&boot_main, 0, sizeof(boot_main));
+        return boot_vect_write_one("main", NULL);
+    } else {
+        memcpy(&boot_main, ver, sizeof(boot_main));
+        return boot_vect_write_one("main", &boot_main);
+    }
 }
 
 static int
@@ -151,68 +208,18 @@ boot_read_image_headers(struct image_header *out_headers,
     }
 }
 
-/**
- * Reads the boot status from the flash file system.  The boot status contains
- * the current state of an interrupted image copy operation.  If the boot
- * status is not present in the file system, the implication is that there is
- * no copy operation in progress.
- *
- * @param out_status            On success, the boot status gets written here.
- * @param out_entries           On success, the array of boot entries gets
- *                                  written here.
- * @param num_areas             The number of flash areas capable of storing
- *                                  image data.  This is equal to the length of
- *                                  the out_entries array.
- *
- * @return                      0 on success; nonzero on failure.
- */
-int
-boot_read_status(struct boot_state *out_state, int num_areas)
+void
+bootutil_cfg_register(void)
 {
-    struct fs_file *file;
-    uint32_t bytes_read;
-    int rc;
-    int i;
-
-    rc = fs_open(BOOT_PATH_STATUS, FS_ACCESS_READ, &file);
-    if (rc != 0) {
-        rc = BOOT_EBADSTATUS;
-        goto done;
-    }
-
-    rc = fs_read(file, sizeof *out_state, out_state, &bytes_read);
-    if (rc != 0 || bytes_read != sizeof *out_state) {
-        rc = BOOT_EBADSTATUS;
-        goto done;
-    }
-
-    if (out_state->status.bs_img1_length == 0xffffffff) {
-        out_state->status.bs_img1_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_state->entries[i].bse_image_num == 0 &&
-            out_state->status.bs_img1_length == 0) {
-
-            rc = BOOT_EBADSTATUS;
-            goto done;
-        }
-        if (out_state->entries[i].bse_image_num == 1 &&
-            out_state->status.bs_img2_length == 0) {
-
-            rc = BOOT_EBADSTATUS;
-            goto done;
-        }
-    }
+    conf_register(&boot_conf_handler);
+}
 
-    rc = 0;
+int
+boot_read_status(void)
+{
+    conf_load();
 
-done:
-    fs_close(file);
-    return rc;
+    return boot_st_loaded == 1;
 }
 
 /**
@@ -228,26 +235,28 @@ done:
  * @return                      0 on success; nonzero on failure.
  */
 int
-boot_write_status(const struct boot_state *state, int num_areas)
+boot_write_status(void)
 {
-    struct fs_file *file;
-    int rc;
-
-    rc = fs_open(BOOT_PATH_STATUS, FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &file);
-    if (rc != 0) {
-        rc = BOOT_EFILE;
-        goto done;
+    char *val_str;
+    char *rstr;
+    int rc = 0;
+    int len;
+
+    len = CONF_STR_FROM_BYTES_LEN(boot_st_sz);
+    val_str = malloc(len);
+    if (!val_str) {
+        return BOOT_ENOMEM;
     }
-
-    rc = fs_write(file, state, sizeof *state);
-    if (rc != 0) {
+    rstr = conf_str_from_bytes(boot_st, boot_st_sz, val_str, len);
+    if (!rstr) {
         rc = BOOT_EFILE;
-        goto done;
+    } else {
+        if (conf_save_one(&boot_conf_handler, "status", val_str)) {
+            rc = BOOT_EFLASH;
+        }
     }
-    rc = 0;
+    free(val_str);
 
-done:
-    fs_close(file);
     return rc;
 }
 
@@ -260,5 +269,5 @@ done:
 void
 boot_clear_status(void)
 {
-    fs_unlink(BOOT_PATH_STATUS);
+    conf_save_one(&boot_conf_handler, "status", NULL);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/libs/bootutil/src/bootutil_priv.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_priv.h b/libs/bootutil/src/bootutil_priv.h
index 2b4dc3e..c4b2d81 100644
--- a/libs/bootutil/src/bootutil_priv.h
+++ b/libs/bootutil/src/bootutil_priv.h
@@ -67,15 +67,14 @@ struct boot_image_location {
     uint32_t bil_address;
 };
 
-int boot_vect_read_test(struct image_version *out_ver);
-int boot_vect_read_main(struct image_version *out_ver);
-int boot_vect_delete_test(void);
-int boot_vect_delete_main(void);
+extern struct boot_state *boot_st;
+extern int boot_st_sz;
+
 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_state *out_state, int num_areas);
-int boot_write_status(const struct boot_state *state, int num_areas);
+int boot_read_status(void);
+int boot_write_status(void);
 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/d27f80e1/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index 061e60e..906bc64 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -23,9 +23,10 @@
 #include <string.h>
 #include <hal/hal_flash.h>
 #include <hal/flash_map.h>
-#include "os/os_malloc.h"
+#include <os/os_malloc.h>
 #include "bootutil/loader.h"
 #include "bootutil/image.h"
+#include "bootutil/bootutil_misc.h"
 #include "bootutil_priv.h"
 
 /** Number of image slots in flash; currently limited to two. */
@@ -37,7 +38,8 @@ static const struct boot_req *boot_req;
 /** Image headers read from flash. */
 struct image_header boot_img_hdrs[2];
 
-static struct boot_state *boot;
+struct boot_state *boot_st;
+int boot_st_sz;
 
 /**
  * Calculates the flash offset of the specified image slot.
@@ -101,7 +103,7 @@ boot_select_image_slot(void)
     if (rc == 0) {
         slot = boot_find_image_slot(&ver);
         if (slot == -1) {
-            boot_vect_delete_test();
+            boot_vect_write_test(NULL);
         } else {
             return slot;
         }
@@ -111,7 +113,7 @@ boot_select_image_slot(void)
     if (rc == 0) {
         slot = boot_find_image_slot(&ver);
         if (slot == -1) {
-            boot_vect_delete_main();
+            boot_vect_write_main(NULL);
         } else {
             return slot;
         }
@@ -137,8 +139,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->entries[i].bse_image_num == image_num &&
-            boot->entries[i].bse_part_num == part_num) {
+        if (boot_st->entries[i].bse_image_num == image_num &&
+            boot_st->entries[i].bse_part_num == part_num) {
 
             return boot_req->br_image_areas[i];
         }
@@ -301,11 +303,11 @@ boot_move_area(int from_area_idx, int to_area_idx,
         return rc;
     }
 
-    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);
+    boot_st->entries[src_image_idx].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot_st->entries[src_image_idx].bse_part_num = BOOT_IMAGE_NUM_NONE;
+    boot_st->entries[dst_image_idx].bse_image_num = img_num;
+    boot_st->entries[dst_image_idx].bse_part_num = part_num;
+    rc = boot_write_status();
     if (rc != 0) {
         return rc;
     }
@@ -366,11 +368,11 @@ boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
         return rc;
     }
 
-    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;
+    boot_st->entries[scratch_image_idx] = boot_st->entries[image_idx_2];
+    boot_st->entries[image_idx_2].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot_st->entries[image_idx_2].bse_part_num = BOOT_IMAGE_NUM_NONE;
 
-    rc = boot_write_status(boot, boot_req->br_num_image_areas);
+    rc = boot_write_status();
     if (rc != 0) {
         return rc;
     }
@@ -385,10 +387,10 @@ boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
         return rc;
     }
 
-    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);
+    boot_st->entries[image_idx_2] = boot_st->entries[image_idx_1];
+    boot_st->entries[image_idx_1].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot_st->entries[image_idx_1].bse_part_num = BOOT_IMAGE_NUM_NONE;
+    rc = boot_write_status();
     if (rc != 0) {
         return rc;
     }
@@ -403,10 +405,10 @@ boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
         return rc;
     }
 
-    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);
+    boot_st->entries[image_idx_1] = boot_st->entries[scratch_image_idx];
+    boot_st->entries[scratch_image_idx].bse_image_num = BOOT_IMAGE_NUM_NONE;
+    boot_st->entries[scratch_image_idx].bse_part_num = BOOT_IMAGE_NUM_NONE;
+    rc = boot_write_status();
     if (rc != 0) {
         return rc;
     }
@@ -445,7 +447,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->entries[dst_image_area_idx].bse_image_num ==
+            if (boot_st->entries[dst_image_area_idx].bse_image_num ==
                 BOOT_IMAGE_NUM_NONE) {
 
                 /* The destination doesn't contain anything useful; we don't
@@ -526,9 +528,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->entries[i].bse_image_num == 0xff);
-        boot->entries[i].bse_image_num = image_num;
-        boot->entries[i].bse_part_num = part_num;
+        assert(boot_st->entries[i].bse_image_num == 0xff);
+        boot_st->entries[i].bse_image_num = image_num;
+        boot_st->entries[i].bse_part_num = part_num;
 
         offset += boot_req->br_area_descs[area_idx].fa_size;
         part_num++;
@@ -552,25 +554,25 @@ boot_build_status(void)
     uint32_t address;
     uint32_t len;
 
-    memset(boot->entries, 0xff,
-           boot_req->br_num_image_areas * sizeof boot->entries[0]);
+    memset(boot_st->entries, 0xff,
+           boot_req->br_num_image_areas * sizeof boot_st->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;
-        boot_status.bs_img1_length = len;
+        boot_st->status.bs_img1_length =
+	    boot_img_hdrs[0].ih_img_size + boot_img_hdrs[0].ih_tlv_size;
         boot_slot_addr(0, &flash_id, &address);
         boot_build_status_one(0, flash_id, address, len);
     } else {
-        boot->status.bs_img1_length = 0;
+        boot_st->status.bs_img1_length = 0;
     }
 
     if (boot_img_hdrs[1].ih_magic == IMAGE_MAGIC) {
-        len = boot_img_hdrs[1].ih_img_size + boot_img_hdrs[1].ih_tlv_size;
-        boot_status.bs_img2_length = len;
+        boot_st->status.bs_img2_length =
+	    boot_img_hdrs[1].ih_img_size + boot_img_hdrs[1].ih_tlv_size;
         boot_slot_addr(1, &flash_id, &address);
         boot_build_status_one(1, flash_id, address, len);
     } else {
-        boot->status.bs_img2_length = 0;
+        boot_st->status.bs_img2_length = 0;
     }
 }
 
@@ -602,17 +604,18 @@ 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 = malloc(sizeof(struct boot_state) +
-	req->br_num_image_areas * sizeof boot->entries[0]);
-    if (boot == NULL) {
+    boot_st_sz = sizeof(struct boot_state) +
+	req->br_num_image_areas * sizeof boot_st->entries[0];
+    boot_st = malloc(boot_st_sz);
+    if (boot_st == NULL) {
         return BOOT_ENOMEM;
     }
-    rc = boot_read_status(boot, boot_req->br_num_image_areas);
-    if (rc == 0) {
+
+    if (boot_read_status()) {
         /* 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_st->status.bs_img1_length,
+                             boot_st->status.bs_img2_length);
         if (rc != 0) {
             /* We failed to put the images back together; there is really no
              * solution here.
@@ -670,8 +673,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_st->status.bs_img1_length,
+                             boot_st->status.bs_img2_length);
 
         if (rc != 0) {
             /* We failed to put the images back together; there is really no
@@ -696,7 +699,7 @@ boot_go(const struct boot_req *req, struct boot_rsp *rsp)
     boot_clear_status();
 
     /* If an image is being tested, it should only be booted into once. */
-    boot_vect_delete_test();
+    boot_vect_write_test(NULL);
 
     return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index a38764d..ef42cd5 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -44,13 +44,13 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
     [IMGMGR_NMGR_OP_UPLOAD] = {
         .nh_read = imgr_noop,
         .nh_write = imgr_upload
-    }
-#ifdef FS_PRESENT
-    ,
+    },
     [IMGMGR_NMGR_OP_BOOT] = {
         .nh_read = imgr_boot_read,
         .nh_write = imgr_boot_write
-    },
+    }
+#ifdef FS_PRESENT
+    ,
     [IMGMGR_NMGR_OP_FILE] = {
         .nh_read = imgr_file_download,
         .nh_write = imgr_file_upload

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/libs/imgmgr/src/imgmgr_boot.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_boot.c b/libs/imgmgr/src/imgmgr_boot.c
new file mode 100644
index 0000000..8084b0d
--- /dev/null
+++ b/libs/imgmgr/src/imgmgr_boot.c
@@ -0,0 +1,116 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <os/os.h>
+#include <os/endian.h>
+
+#include <limits.h>
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <newtmgr/newtmgr.h>
+#include <bootutil/image.h>
+#include <bootutil/bootutil_misc.h>
+#include <fs/fs.h>
+#include <fs/fsutil.h>
+#include <json/json.h>
+#include <util/base64.h>
+#include <bsp/bsp.h>
+
+#include "imgmgr/imgmgr.h"
+#include "imgmgr_priv.h"
+
+static void
+imgr_ver_jsonstr(struct json_encoder *enc, char *key,
+  struct image_version *ver)
+{
+    struct json_value jv;
+    char ver_str[IMGMGR_NMGR_MAX_VER];
+    int ver_len;
+
+    ver_len = imgr_ver_str(ver, ver_str);
+    JSON_VALUE_STRINGN(&jv, ver_str, ver_len);
+    json_encode_object_entry(enc, key, &jv);
+}
+
+int
+imgr_boot_read(struct nmgr_jbuf *njb)
+{
+    int rc;
+    struct json_encoder *enc;
+    struct image_version ver;
+
+    enc = &njb->njb_enc;
+
+    json_encode_object_start(enc);
+
+    rc = boot_vect_read_test(&ver);
+    if (!rc) {
+        imgr_ver_jsonstr(enc, "test", &ver);
+    }
+
+    rc = boot_vect_read_main(&ver);
+    if (!rc) {
+        imgr_ver_jsonstr(enc, "main", &ver);
+    }
+
+    rc = imgr_read_ver(bsp_imgr_current_slot(), &ver);
+    if (!rc) {
+        imgr_ver_jsonstr(enc, "active", &ver);
+    }
+
+    json_encode_object_finish(enc);
+
+    return 0;
+}
+
+int
+imgr_boot_write(struct nmgr_jbuf *njb)
+{
+    char test_ver_str[28];
+    const struct json_attr_t boot_write_attr[2] = {
+        [0] = {
+            .attribute = "test",
+            .type = t_string,
+            .addr.string = test_ver_str,
+            .len = sizeof(test_ver_str),
+        },
+        [1] = {
+            .attribute = NULL
+        }
+    };
+    int rc;
+    struct image_version ver;
+
+    rc = json_read_object(&njb->njb_buf, boot_write_attr);
+    if (rc) {
+        return OS_EINVAL;
+    }
+
+    rc = imgr_ver_parse(boot_write_attr[0].addr.string, &ver);
+    if (rc) {
+        return OS_EINVAL;
+    }
+
+    rc = boot_vect_write_test(&ver);
+    if (rc) {
+        return OS_EINVAL;
+    }
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d27f80e1/libs/imgmgr/src/imgmgr_fs.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_fs.c b/libs/imgmgr/src/imgmgr_fs.c
index 6a0d577..bbf8d94 100644
--- a/libs/imgmgr/src/imgmgr_fs.c
+++ b/libs/imgmgr/src/imgmgr_fs.c
@@ -27,7 +27,6 @@
 #include <newtmgr/newtmgr.h>
 #include <bootutil/image.h>
 #include <fs/fs.h>
-#include <fs/fsutil.h>
 #include <json/json.h>
 #include <util/base64.h>
 #include <bsp/bsp.h>
@@ -35,120 +34,7 @@
 #include "imgmgr/imgmgr.h"
 #include "imgmgr_priv.h"
 
-/* XXX share with bootutil */
-#define BOOT_PATH		"/boot"
-#define BOOT_PATH_MAIN          "/boot/main"
-#define BOOT_PATH_TEST          "/boot/test"
-
 #ifdef FS_PRESENT
-static int
-imgr_read_file(const char *path, struct image_version *ver)
-{
-    uint32_t bytes_read;
-    int rc;
-
-    rc = fsutil_read_file(path, 0, sizeof(*ver), ver, &bytes_read);
-    if (rc != 0 || bytes_read != sizeof(*ver)) {
-        return -1;
-    }
-    return 0;
-}
-
-static int
-imgr_read_test(struct image_version *ver)
-{
-    return (imgr_read_file(BOOT_PATH_TEST, ver));
-}
-
-static int
-imgr_read_main(struct image_version *ver)
-{
-    return (imgr_read_file(BOOT_PATH_MAIN, ver));
-}
-
-static int
-imgr_write_file(const char *path, struct image_version *ver)
-{
-    return fsutil_write_file(path, ver, sizeof(*ver));
-}
-
-static void
-imgr_ver_jsonstr(struct json_encoder *enc, char *key,
-  struct image_version *ver)
-{
-    struct json_value jv;
-    char ver_str[IMGMGR_NMGR_MAX_VER];
-    int ver_len;
-
-    ver_len = imgr_ver_str(ver, ver_str);
-    JSON_VALUE_STRINGN(&jv, ver_str, ver_len);
-    json_encode_object_entry(enc, key, &jv);
-}
-
-int
-imgr_boot_read(struct nmgr_jbuf *njb)
-{
-    int rc;
-    struct json_encoder *enc;
-    struct image_version ver;
-
-    enc = &njb->njb_enc;
-
-    json_encode_object_start(enc);
-
-    rc = imgr_read_test(&ver);
-    if (!rc) {
-        imgr_ver_jsonstr(enc, "test", &ver);
-    }
-
-    rc = imgr_read_main(&ver);
-    if (!rc) {
-        imgr_ver_jsonstr(enc, "main", &ver);
-    }
-
-    rc = imgr_read_ver(bsp_imgr_current_slot(), &ver);
-    if (!rc) {
-        imgr_ver_jsonstr(enc, "active", &ver);
-    }
-
-    json_encode_object_finish(enc);
-
-    return 0;
-}
-
-int
-imgr_boot_write(struct nmgr_jbuf *njb)
-{
-    char test_ver_str[28];
-    const struct json_attr_t boot_write_attr[2] = {
-        [0] = {
-            .attribute = "test",
-            .type = t_string,
-            .addr.string = test_ver_str,
-            .len = sizeof(test_ver_str),
-        },
-        [1] = {
-            .attribute = NULL
-        }
-    };
-    int rc;
-    struct image_version ver;
-
-    rc = json_read_object(&njb->njb_buf, boot_write_attr);
-    if (rc) {
-        return OS_EINVAL;
-    }
-
-    rc = imgr_ver_parse(boot_write_attr[0].addr.string, &ver);
-    if (rc) {
-        return OS_EINVAL;
-    }
-
-    fs_mkdir(BOOT_PATH);
-    rc = imgr_write_file(BOOT_PATH_TEST, &ver);
-    return rc;
-}
-
 int
 imgr_file_download(struct nmgr_jbuf *njb)
 {