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:19 UTC

[01/14] incubator-mynewt-core git commit: bootutil; call boot_clear_status() to clear state about ongoing update.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop a006be675 -> b0a8c8edb


bootutil; call boot_clear_status() to clear state about ongoing update.


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

Branch: refs/heads/develop
Commit: 1076c9dc96d5b3c184ddf8bdc0e535899e3ed6fd
Parents: 9441a8e
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Apr 12 16:32:44 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:44:56 2016 -0700

----------------------------------------------------------------------
 libs/bootutil/src/loader.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1076c9dc/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index ae0c91a..cb0cf68 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -694,7 +694,7 @@ boot_go(const struct boot_req *req, struct boot_rsp *rsp)
     rsp->br_image_addr = image_addrs[0].bil_address;
 
     /* After successful boot, there should not be a status file. */
-    fs_unlink(BOOT_PATH_STATUS);
+    boot_clear_status();
 
     /* If an image is being tested, it should only be booted into once. */
     boot_vect_delete_test();


[09/14] incubator-mynewt-core git commit: config; fix typo in comment.

Posted by ma...@apache.org.
config; fix typo in comment.


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

Branch: refs/heads/develop
Commit: 674723425e331a0995b27c1f72bdad80c50b2e53
Parents: 38b4b3e
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 15 16:39:15 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 sys/config/src/config_store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/67472342/sys/config/src/config_store.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_store.c b/sys/config/src/config_store.c
index 728581a..1caf600 100644
--- a/sys/config/src/config_store.c
+++ b/sys/config/src/config_store.c
@@ -75,7 +75,7 @@ conf_load(void)
 }
 
 /*
- * Append a sigle value to persisted config.
+ * Append a single value to persisted config.
  */
 int
 conf_save_one(const struct conf_handler *ch, const char *name, char *value)


[08/14] incubator-mynewt-core git commit: config; for safaety null terminate the line created of name and value.

Posted by ma...@apache.org.
config; for safaety null terminate the line created of name and value.


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

Branch: refs/heads/develop
Commit: 07cdc2ed4445cfd0528395408586daea0eb0f4b4
Parents: 6747234
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 15 18:28:14 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 sys/config/src/config_line.c | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/07cdc2ed/sys/config/src/config_line.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_line.c b/sys/config/src/config_line.c
index 631e470..e0eb79c 100644
--- a/sys/config/src/config_line.c
+++ b/sys/config/src/config_line.c
@@ -100,6 +100,7 @@ conf_line_make(char *dst, int dlen, const struct conf_handler *ch,
 
     memcpy(dst + off, value, vlen);
     off += vlen;
+    dst[off] = '\0';
 
     return off;
 }


[12/14] incubator-mynewt-core git commit: nffs; Don't call nffs_log_contents() at the end of nffs_detect().

Posted by ma...@apache.org.
nffs; Don't call nffs_log_contents() at the end of nffs_detect().


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

Branch: refs/heads/develop
Commit: b0a8c8edb0f98f128737c5577c3d9747ace57888
Parents: 39a6b3d
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Apr 18 09:46:42 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 fs/nffs/src/nffs_restore.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b0a8c8ed/fs/nffs/src/nffs_restore.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/nffs_restore.c b/fs/nffs/src/nffs_restore.c
index 06ea613..947a359 100644
--- a/fs/nffs/src/nffs_restore.c
+++ b/fs/nffs/src/nffs_restore.c
@@ -958,6 +958,7 @@ nffs_restore_corrupt_scratch(void)
     return 0;
 }
 
+#ifdef notnow
 static void
 nffs_log_contents(void)
 {
@@ -1012,6 +1013,7 @@ nffs_log_contents(void)
         }
     }
 }
+#endif
 
 /**
  * Searches for a valid nffs file system among the specified areas.  This
@@ -1146,9 +1148,10 @@ nffs_restore_full(const struct nffs_area_desc *area_descs)
         goto err;
     }
 
+#ifdef notnow
     NFFS_LOG(DEBUG, "CONTENTS\n");
     nffs_log_contents();
-
+#endif
     return 0;
 
 err:


[07/14] incubator-mynewt-core git commit: slinky; need to reserve more memory to store nffs sector array.

Posted by ma...@apache.org.
slinky; need to reserve more memory to store nffs sector array.


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

Branch: refs/heads/develop
Commit: bfeec37ccefb5b50506d9adc1b405f2952239d56
Parents: 705d334
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 15 16:35:44 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 apps/slinky/src/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bfeec37c/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c
index 3194df3..c9aa5af 100755
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@ -251,7 +251,7 @@ main(int argc, char **argv)
     int cnt;
 
     /* NFFS_AREA_MAX is defined in the BSP-specified bsp.h header file. */
-    struct nffs_area_desc descs[NFFS_AREA_MAX];
+    struct nffs_area_desc descs[NFFS_AREA_MAX + 1];
 
 #ifdef ARCH_sim
     mcu_sim_parse_args(argc, argv);


[05/14] incubator-mynewt-core git commit: fs; add 'cat' CLI command.

Posted by ma...@apache.org.
fs; add 'cat' CLI command.


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

Branch: refs/heads/develop
Commit: c56aca4ac6f78f626792e1fc6d9f01e6ebc81d38
Parents: 094ed50
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Apr 13 10:54:24 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:45:51 2016 -0700

----------------------------------------------------------------------
 fs/fs/src/fs_cli.c                     | 39 +++++++++++++++++++++++++++++
 libs/bootutil/include/bootutil/crc32.h | 26 -------------------
 2 files changed, 39 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c56aca4a/fs/fs/src/fs_cli.c
----------------------------------------------------------------------
diff --git a/fs/fs/src/fs_cli.c b/fs/fs/src/fs_cli.c
index 459533e..4498d87 100644
--- a/fs/fs/src/fs_cli.c
+++ b/fs/fs/src/fs_cli.c
@@ -31,6 +31,7 @@ static int fs_ls_cmd(int argc, char **argv);
 static int fs_rm_cmd(int argc, char **argv);
 static int fs_mkdir_cmd(int argc, char **argv);
 static int fs_mv_cmd(int argc, char **argv);
+static int fs_cat_cmd(int argc, char **argv);
 
 static struct shell_cmd fs_ls_struct = {
     .sc_cmd = "ls",
@@ -48,6 +49,10 @@ static struct shell_cmd fs_mv_struct = {
     .sc_cmd = "mv",
     .sc_cmd_func = fs_mv_cmd
 };
+static struct shell_cmd fs_cat_struct = {
+    .sc_cmd = "cat",
+    .sc_cmd_func = fs_cat_cmd
+};
 
 static void
 fs_ls_file(const char *name, struct fs_file *file)
@@ -180,6 +185,39 @@ out:
     return 0;
 }
 
+static int
+fs_cat_cmd(int argc, char **argv)
+{
+    int rc;
+    struct fs_file *file;
+    char buf[32];
+    uint32_t len;
+
+    if (argc != 2) {
+        console_printf("cat <filename>\n");
+        return -1;
+    }
+
+    rc = fs_open(argv[1], FS_ACCESS_READ, &file);
+    if (rc != FS_EOK) {
+        console_printf("Error opening %s - %d\n", argv[1], rc);
+        return -1;
+    }
+
+    do {
+        rc = fs_read(file, sizeof(buf), buf, &len);
+        if (rc != FS_EOK) {
+            console_printf("\nError reading %s - %d\n", argv[1], rc);
+            break;
+        }
+        console_write(buf, len);
+    } while (len > 0);
+
+    fs_close(file);
+
+    return 0;
+}
+
 void
 fs_cli_init(void)
 {
@@ -187,5 +225,6 @@ fs_cli_init(void)
     shell_cmd_register(&fs_rm_struct);
     shell_cmd_register(&fs_mkdir_struct);
     shell_cmd_register(&fs_mv_struct);
+    shell_cmd_register(&fs_cat_struct);
 }
 #endif /* SHELL_PRESENT */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c56aca4a/libs/bootutil/include/bootutil/crc32.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/include/bootutil/crc32.h b/libs/bootutil/include/bootutil/crc32.h
deleted file mode 100644
index 5dfe221..0000000
--- a/libs/bootutil/include/bootutil/crc32.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/**
- * 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 H_CRC32_
-#define H_CRC32_
-
-uint32_t crc32(uint32_t crc, const void *buf, size_t size);
-
-#endif
-


[10/14] incubator-mynewt-core git commit: boot; do not compile any logging in.

Posted by ma...@apache.org.
boot; do not compile any logging in.


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

Branch: refs/heads/develop
Commit: 819b4af653b57261a44a0859f6fe92d0c103bbc7
Parents: bfeec37
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 15 16:36:26 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 apps/boot/pkg.yml | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/819b4af6/apps/boot/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/boot/pkg.yml b/apps/boot/pkg.yml
index cdc6128..3564ace 100644
--- a/apps/boot/pkg.yml
+++ b/apps/boot/pkg.yml
@@ -34,3 +34,4 @@ pkg.deps:
     - libs/mbedtls
     - libs/os
     - libs/util
+pkg.cflags: -DLOG_LEVEL=255


[03/14] incubator-mynewt-core git commit: sys/config; convert some of the arguments to conf_save_one() to be const.

Posted by ma...@apache.org.
sys/config; convert some of the arguments to conf_save_one() to be const.


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

Branch: refs/heads/develop
Commit: 782dc45460a38cf686e72bc75f686f3a1b0762e2
Parents: c56aca4
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Apr 13 15:53:26 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:45:51 2016 -0700

----------------------------------------------------------------------
 sys/config/include/config/config.h | 3 ++-
 sys/config/src/config_fcb.c        | 8 ++++----
 sys/config/src/config_file.c       | 8 ++++----
 sys/config/src/config_line.c       | 4 ++--
 sys/config/src/config_priv.h       | 8 ++++----
 sys/config/src/config_store.c      | 2 +-
 6 files changed, 17 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/782dc454/sys/config/include/config/config.h
----------------------------------------------------------------------
diff --git a/sys/config/include/config/config.h b/sys/config/include/config/config.h
index a7956da..5ddf83a 100644
--- a/sys/config/include/config/config.h
+++ b/sys/config/include/config/config.h
@@ -57,7 +57,7 @@ int conf_register(struct conf_handler *);
 int conf_load(void);
 
 int conf_save(void);
-int conf_save_one(struct conf_handler *, char *name, char *var);
+int conf_save_one(const struct conf_handler *, const char *name, char *var);
 
 /*
   XXXX for later
@@ -74,6 +74,7 @@ int conf_value_from_str(char *val_str, enum conf_type type, void *vp,
 int conf_bytes_from_str(char *val_str, void *vp, int *len);
 char *conf_str_from_value(enum conf_type type, void *vp, char *buf,
   int buf_len);
+#define CONF_STR_FROM_BYTES_LEN(len) (((len) * 4 / 3) + 4)
 char *conf_str_from_bytes(void *vp, int vp_len, char *buf, int buf_len);
 
 #define CONF_VALUE_SET(str, type, val)                                  \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/782dc454/sys/config/src/config_fcb.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_fcb.c b/sys/config/src/config_fcb.c
index f872894..208eee1 100644
--- a/sys/config/src/config_fcb.c
+++ b/sys/config/src/config_fcb.c
@@ -36,8 +36,8 @@ struct conf_fcb_load_cb_arg {
 
 static int conf_fcb_load(struct conf_store *, load_cb cb, void *cb_arg);
 static int conf_fcb_save_start(struct conf_store *);
-static int conf_fcb_save(struct conf_store *, struct conf_handler *,
-  char *name, char *value);
+static int conf_fcb_save(struct conf_store *, const struct conf_handler *,
+  const char *name, const char *value);
 static int conf_fcb_save_end(struct conf_store *);
 
 static struct conf_store_itf conf_fcb_itf = {
@@ -251,8 +251,8 @@ conf_fcb_append(struct conf_fcb *cf, char *buf, int len)
 }
 
 static int
-conf_fcb_save(struct conf_store *cs, struct conf_handler *ch,
-  char *name, char *value)
+conf_fcb_save(struct conf_store *cs, const struct conf_handler *ch,
+  const char *name, const char *value)
 {
     struct conf_fcb *cf = (struct conf_fcb *)cs;
     char buf[CONF_MAX_NAME_LEN + CONF_MAX_VAL_LEN + 32];

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/782dc454/sys/config/src/config_file.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_file.c b/sys/config/src/config_file.c
index ff627d0..402038c 100644
--- a/sys/config/src/config_file.c
+++ b/sys/config/src/config_file.c
@@ -31,8 +31,8 @@
 
 static int conf_file_load(struct conf_store *, load_cb cb, void *cb_arg);
 static int conf_file_save_start(struct conf_store *);
-static int conf_file_save(struct conf_store *, struct conf_handler *ch,
-  char *name, char *value);
+static int conf_file_save(struct conf_store *, const struct conf_handler *ch,
+  const char *name, const char *value);
 static int conf_file_save_end(struct conf_store *);
 
 static struct conf_store_itf conf_file_itf = {
@@ -178,8 +178,8 @@ conf_file_save_start(struct conf_store *cs)
 }
 
 static int
-conf_file_save(struct conf_store *cs, struct conf_handler *ch,
-  char *name, char *value)
+conf_file_save(struct conf_store *cs, const struct conf_handler *ch,
+  const char *name, const char *value)
 {
     struct conf_file *cf = (struct conf_file *)cs;
     struct fs_file *file;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/782dc454/sys/config/src/config_line.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_line.c b/sys/config/src/config_line.c
index 7a18c9f..631e470 100644
--- a/sys/config/src/config_line.c
+++ b/sys/config/src/config_line.c
@@ -72,8 +72,8 @@ conf_line_parse(char *buf, char **namep, char **valp)
 }
 
 int
-conf_line_make(char *dst, int dlen, struct conf_handler *ch, char *name,
-  char *value)
+conf_line_make(char *dst, int dlen, const struct conf_handler *ch,
+  const char *name, const char *value)
 {
     int clen;
     int nlen;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/782dc454/sys/config/src/config_priv.h
----------------------------------------------------------------------
diff --git a/sys/config/src/config_priv.h b/sys/config/src/config_priv.h
index 7358133..6d5ea1a 100644
--- a/sys/config/src/config_priv.h
+++ b/sys/config/src/config_priv.h
@@ -28,8 +28,8 @@ int conf_json_line(struct json_buffer *jb, char *name, int nlen, char *value,
   int vlen);
 
 int conf_line_parse(char *buf, char **namep, char **valp);
-int conf_line_make(char *dst, int dlen, struct conf_handler *ch, char *name,
-  char *val);
+int conf_line_make(char *dst, int dlen, const struct conf_handler *ch,
+  const char *name, const char *val);
 
 /*
  * API for config storage.
@@ -38,8 +38,8 @@ typedef void (*load_cb)(char *name, char *val, void *cb_arg);
 struct conf_store_itf {
     int (*csi_load)(struct conf_store *cs, load_cb cb, void *cb_arg);
     int (*csi_save_start)(struct conf_store *cs);
-    int (*csi_save)(struct conf_store *cs, struct conf_handler *ch, char *name,
-      char *value);
+    int (*csi_save)(struct conf_store *cs, const struct conf_handler *ch,
+      const char *name, const char *value);
     int (*csi_save_end)(struct conf_store *cs);
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/782dc454/sys/config/src/config_store.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_store.c b/sys/config/src/config_store.c
index 887dfaa..728581a 100644
--- a/sys/config/src/config_store.c
+++ b/sys/config/src/config_store.c
@@ -78,7 +78,7 @@ conf_load(void)
  * Append a sigle value to persisted config.
  */
 int
-conf_save_one(struct conf_handler *ch, char *name, char *value)
+conf_save_one(const struct conf_handler *ch, const char *name, char *value)
 {
     struct conf_store *cs;
 


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

Posted by ma...@apache.org.
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)
 {


[11/14] incubator-mynewt-core git commit: bootutil; change the amount of status data we persist during upgrade. Try to use the full scratch area when moving data between areas, instead of copying it one sector at a time.

Posted by ma...@apache.org.
bootutil; change the amount of status data we persist during upgrade.
Try to use the full scratch area when moving data between areas,
instead of copying it one sector at a time.


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

Branch: refs/heads/develop
Commit: 705d334699c3d057a59503be76932b9bde944439
Parents: d27f80e
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 15 16:30:12 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 apps/boot/src/boot.c                    |   8 -
 libs/bootutil/include/bootutil/loader.h |  13 +-
 libs/bootutil/pkg.yml                   |   1 +
 libs/bootutil/src/bootutil_misc.c       |  66 ++--
 libs/bootutil/src/bootutil_priv.h       |  24 +-
 libs/bootutil/src/loader.c              | 441 +++++++--------------------
 libs/bootutil/src/test/boot_test.c      | 321 +++++++++----------
 7 files changed, 319 insertions(+), 555 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/705d3346/apps/boot/src/boot.c
----------------------------------------------------------------------
diff --git a/apps/boot/src/boot.c b/apps/boot/src/boot.c
index 9cee56d..d0bdb1c 100755
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@ -50,8 +50,6 @@ main(void)
 {
     struct nffs_area_desc nffs_descs[NFFS_AREA_MAX + 1];
     struct flash_area descs[AREA_DESC_MAX];
-    /** Contains indices of the areas which can contain image data. */
-    uint8_t img_areas[AREA_DESC_MAX];
     /** Areas representing the beginning of image slots. */
     uint8_t img_starts[2];
     int cnt;
@@ -60,7 +58,6 @@ main(void)
     int rc;
     struct boot_req req = {
         .br_area_descs = descs,
-        .br_image_areas = img_areas,
         .br_slot_areas = img_starts,
     };
 
@@ -90,10 +87,6 @@ main(void)
 
     req.br_num_image_areas = total;
 
-    for (cnt = 0; cnt < total; cnt++) {
-        img_areas[cnt] = cnt;
-    }
-
     /*
      * Make sure we have enough left to initialize the NFFS with the
      * right number of maximum areas otherwise the file-system will not
@@ -115,7 +108,6 @@ main(void)
         nffs_detect(nffs_descs);
     }
 
-    log_init();
     conf_init();
 
     rc = conf_file_src(&my_conf);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/705d3346/libs/bootutil/include/bootutil/loader.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/include/bootutil/loader.h b/libs/bootutil/include/bootutil/loader.h
index 1f82519..86f06ba 100644
--- a/libs/bootutil/include/bootutil/loader.h
+++ b/libs/bootutil/include/bootutil/loader.h
@@ -33,14 +33,8 @@ struct boot_req {
 
     /**
      * Array of indices of elements in the br_area_descs array; indicates which
-     * areas hold image data.
-     */
-    uint8_t *br_image_areas;
-
-    /**
-     * Array of indices of elements in the br_area_descs array; indicates which
-     * areas represent the beginning of an image slot.  This should be a subset
-     * of the br_image_areas array.
+     * areas represent the beginning of an image slot.  These are indices
+     * to br_area_descs array.
      */
     uint8_t *br_slot_areas;
 
@@ -49,7 +43,8 @@ struct boot_req {
      */
     uint8_t br_num_image_areas;
 
-    /** The index of the area to use as the image scratch area. */
+    /** The area to use as the image scratch area, index is
+	index to br_area_descs array, of the  */
     uint8_t br_scratch_area_idx;
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/705d3346/libs/bootutil/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/bootutil/pkg.yml b/libs/bootutil/pkg.yml
index c1bd59a..01b8c43 100644
--- a/libs/bootutil/pkg.yml
+++ b/libs/bootutil/pkg.yml
@@ -31,6 +31,7 @@ pkg.deps:
     - libs/testutil
     - libs/mbedtls
     - hw/hal
+    - sys/config
 
 pkg.cflags.IMAGE_KEYS_RSA: -DIMAGE_SIGNATURES_RSA
 pkg.cflags.IMAGE_KEYS_EC: -DIMAGE_SIGNATURES_EC

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/705d3346/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index d81dafe..eaaeaf7 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -29,7 +29,7 @@ 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 boot_status boot_saved;
 
 static struct conf_handler boot_conf_handler = {
     .ch_name = "boot",
@@ -48,18 +48,32 @@ boot_conf_set(int argc, char **argv, char *val)
     if (argc == 1) {
         if (!strcmp(argv[0], "main")) {
             len = sizeof(boot_main);
-            rc = conf_bytes_from_str(val, &boot_main, &len);
+            if (val) {
+                rc = conf_bytes_from_str(val, &boot_main, &len);
+            } else {
+                memset(&boot_main, 0, len);
+                rc = 0;
+            }
         } else if (!strcmp(argv[0], "test")) {
             len = sizeof(boot_test);
-            rc = conf_bytes_from_str(val, &boot_test, &len);
+            if (val) {
+                rc = conf_bytes_from_str(val, &boot_test, &len);
+            } else {
+                memset(&boot_test, 0, len);
+                rc = 0;
+            }
         } 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;
+            if (!val) {
+                boot_saved.state = 0;
+                rc = 0;
             } else {
-                boot_st_loaded = 0;
+                rc = conf_value_from_str(val, CONF_INT32,
+                  &boot_saved.state, sizeof(boot_saved.state));
             }
+        } else if (!strcmp(argv[0], "len")) {
+            conf_value_from_str(val, CONF_INT32, &boot_saved.length,
+              sizeof(boot_saved.length));
+            rc = 0;
         } else {
             rc = OS_ENOENT;
         }
@@ -107,7 +121,7 @@ boot_vect_read_main(struct image_version *out_ver)
     return boot_vect_read_one(out_ver, &boot_main);
 }
 
-int
+static int
 boot_vect_write_one(const char *name, struct image_version *ver)
 {
     char str[CONF_STR_FROM_BYTES_LEN(sizeof(struct image_version))];
@@ -215,11 +229,12 @@ bootutil_cfg_register(void)
 }
 
 int
-boot_read_status(void)
+boot_read_status(struct boot_status *bs)
 {
     conf_load();
 
-    return boot_st_loaded == 1;
+    *bs = boot_saved;
+    return (boot_saved.state != 0);
 }
 
 /**
@@ -235,29 +250,18 @@ boot_read_status(void)
  * @return                      0 on success; nonzero on failure.
  */
 int
-boot_write_status(void)
+boot_write_status(struct boot_status *bs)
 {
-    char *val_str;
-    char *rstr;
-    int rc = 0;
-    int len;
+    char str[12];
+    int rc;
 
-    len = CONF_STR_FROM_BYTES_LEN(boot_st_sz);
-    val_str = malloc(len);
-    if (!val_str) {
-        return BOOT_ENOMEM;
-    }
-    rstr = conf_str_from_bytes(boot_st, boot_st_sz, val_str, len);
-    if (!rstr) {
-        rc = BOOT_EFILE;
-    } else {
-        if (conf_save_one(&boot_conf_handler, "status", val_str)) {
-            rc = BOOT_EFLASH;
-        }
+    rc = conf_save_one(&boot_conf_handler, "status",
+      conf_str_from_value(CONF_INT32, &bs->state, str, sizeof(str)));
+    if (rc) {
+        return rc;
     }
-    free(val_str);
-
-    return rc;
+    return conf_save_one(&boot_conf_handler, "len",
+      conf_str_from_value(CONF_INT32, &bs->length, str, sizeof(str)));
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/705d3346/libs/bootutil/src/bootutil_priv.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_priv.h b/libs/bootutil/src/bootutil_priv.h
index c4b2d81..ddbbd5c 100644
--- a/libs/bootutil/src/bootutil_priv.h
+++ b/libs/bootutil/src/bootutil_priv.h
@@ -39,16 +39,8 @@ struct image_header;
 #define BOOT_TMPBUF_SZ  256
 
 struct boot_status {
-    uint32_t bs_img1_length;
-    uint32_t bs_img2_length;
-    /* Followed by sequence of boot status entries; file size indicates number
-     * of entries.
-     */
-};
-
-struct boot_status_entry {
-    uint8_t bse_image_num;
-    uint8_t bse_part_num;
+    uint32_t length;
+    uint32_t state;
 };
 
 /**
@@ -56,25 +48,17 @@ struct boot_status_entry {
  * 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;
 };
 
-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(void);
-int boot_write_status(void);
+int boot_read_status(struct boot_status *);
+int boot_write_status(struct boot_status *);
 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/705d3346/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index 906bc64..b3704ee 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -21,8 +21,8 @@
 #include <stddef.h>
 #include <inttypes.h>
 #include <string.h>
-#include <hal/hal_flash.h>
 #include <hal/flash_map.h>
+#include <hal/hal_flash.h>
 #include <os/os_malloc.h>
 #include "bootutil/loader.h"
 #include "bootutil/image.h"
@@ -38,8 +38,11 @@ static const struct boot_req *boot_req;
 /** Image headers read from flash. */
 struct image_header boot_img_hdrs[2];
 
-struct boot_state *boot_st;
-int boot_st_sz;
+static struct boot_status boot_state;
+
+#define BOOT_PERSIST(idx, st) (((idx) << 8) | (0xff & (st)))
+#define BOOT_PERSIST_IDX(st) (((st) >> 8) & 0xffffff)
+#define BOOT_PERSIST_ST(st) ((st) & 0xff)
 
 /**
  * Calculates the flash offset of the specified image slot.
@@ -122,90 +125,48 @@ boot_select_image_slot(void)
     return -1;
 }
 
-/**
- * Searches the current boot status for the specified image-num,part-num pair.
- *
- * @param image_num             The image number to search for.
- * @param part_num              The part number of the specified image to
- *                                  search for.
+/*
+ * How many sectors starting from sector[idx] can fit inside scratch.
  *
- * @return                      The area index containing the specified image
- *                              part number;
- *                              -1 if the part number is not present in flash.
  */
-static int
-boot_find_image_part(int image_num, int part_num)
+static uint32_t
+boot_copy_sz(int idx, int max_idx, int *cnt)
 {
     int i;
-
-    for (i = 0; i < boot_req->br_num_image_areas; i++) {
-        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];
+    uint32_t sz;
+    static uint32_t scratch_sz = 0;
+
+    if (!scratch_sz) {
+        for (i = boot_req->br_scratch_area_idx;
+             i < boot_req->br_num_image_areas;
+             i++) {
+            scratch_sz += boot_req->br_area_descs[i].fa_size;
         }
     }
-
-    return -1;
-}
-
-static int
-boot_slot_to_area_idx(int slot_num)
-{
-    int i;
-    uint8_t flash_id;
-    uint32_t address;
-
-    assert(slot_num >= 0 && slot_num < BOOT_NUM_SLOTS);
-
-    for (i = 0; boot_req->br_area_descs[i].fa_size != 0; i++) {
-        boot_slot_addr(slot_num, &flash_id, &address);
-        if (boot_req->br_area_descs[i].fa_off == address &&
-          boot_req->br_area_descs[i].fa_flash_id == flash_id) {
-
-            return i;
+    sz = 0;
+    *cnt = 0;
+    for (i = idx; i < max_idx; i++) {
+        if (sz + boot_req->br_area_descs[i].fa_size > scratch_sz) {
+            break;
         }
+        sz += boot_req->br_area_descs[i].fa_size;
+        *cnt = *cnt + 1;
     }
-
-    return -1;
+    return sz;
 }
 
-/**
- * Locates the specified area index within the array of image areas.
- *
- * @param area_idx              The area index to search for.
- *
- * @return                      The index of the element in the image area
- *                              array.  that contains the sought after area
- *                              index; -1 if the area index is not present.
- */
-static int
-boot_find_image_area_idx(int area_idx)
-{
-    int i;
-
-    for (i = 0; i < boot_req->br_num_image_areas; i++) {
-        if (boot_req->br_image_areas[i] == area_idx) {
-            return i;
-        }
-    }
-
-    return -1;
-}
 
 static int
-boot_erase_area(int area_idx)
+boot_erase_area(int area_idx, uint32_t sz)
 {
     const struct flash_area *area_desc;
     int rc;
 
     area_desc = boot_req->br_area_descs + area_idx;
-    rc = hal_flash_erase(area_desc->fa_flash_id, area_desc->fa_off,
-                         area_desc->fa_size);
+    rc = hal_flash_erase(area_desc->fa_flash_id, area_desc->fa_off, sz);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
-
     return 0;
 }
 
@@ -215,11 +176,12 @@ boot_erase_area(int area_idx)
  *
  * @param from_area_idx       The index of the source area.
  * @param to_area_idx         The index of the destination area.
+ * @param sz                  The number of bytes to move.
  *
  * @return                      0 on success; nonzero on failure.
  */
 static int
-boot_copy_area(int from_area_idx, int to_area_idx)
+boot_copy_area(int from_area_idx, int to_area_idx, uint32_t sz)
 {
     const struct flash_area *from_area_desc;
     const struct flash_area *to_area_desc;
@@ -237,11 +199,11 @@ boot_copy_area(int from_area_idx, int to_area_idx)
     assert(to_area_desc->fa_size >= from_area_desc->fa_size);
 
     off = 0;
-    while (off < from_area_desc->fa_size) {
-        if (from_area_desc->fa_size - off > sizeof buf) {
+    while (off < sz) {
+        if (sz - off > sizeof buf) {
             chunk_sz = sizeof buf;
         } else {
-            chunk_sz = from_area_desc->fa_size - off;
+            chunk_sz = sz - off;
         }
 
         from_addr = from_area_desc->fa_off + off;
@@ -267,212 +229,82 @@ boot_copy_area(int from_area_idx, int to_area_idx)
 /**
  * Swaps the contents of two flash areas.
  *
- * @param area_idx_1            The index of one area to swap.  This area
- *                                  must be part of the first image slot.
- * @param part_num_1            The image part number stored in the first
- *                                  area.
- * @param area_idx_2            The index of the other area to swap.  This
- *                                  area must be part of the second image
- *                                  slot.
- * @param part_num_2            The image part number stored in the second
- *                                  area.
- *
- * @return                      0 on success; nonzero on failure.
- */
-static int
-boot_move_area(int from_area_idx, int to_area_idx,
-                 int img_num, uint8_t part_num)
-{
-    int src_image_idx;
-    int dst_image_idx;
-    int rc;
-
-    src_image_idx = boot_find_image_area_idx(from_area_idx);
-    assert(src_image_idx != -1);
-
-    dst_image_idx = boot_find_image_area_idx(to_area_idx);
-    assert(dst_image_idx != -1);
-
-    rc = boot_erase_area(to_area_idx);
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = boot_copy_area(from_area_idx, to_area_idx);
-    if (rc != 0) {
-        return rc;
-    }
-
-    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;
-    }
-
-    rc = boot_erase_area(from_area_idx);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-/**
- * Swaps the contents of two flash areas.
- *
  * @param area_idx_1          The index of one area to swap.  This area
  *                                  must be part of the first image slot.
- * @param part_num_1            The image part number stored in the first
- *                                  area.
  * @param area_idx_2          The index of the other area to swap.  This
  *                                  area must be part of the second image
  *                                  slot.
- * @param part_num_2            The image part number stored in the second
- *                                  area.
- *
  * @return                      0 on success; nonzero on failure.
  */
 static int
-boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
-                  int area_idx_2, int img_num_2, uint8_t part_num_2)
+boot_swap_areas(int idx, uint32_t sz)
 {
-    int scratch_image_idx;
-    int image_idx_1;
-    int image_idx_2;
+    int area_idx_1;
+    int area_idx_2;
     int rc;
+    int state;
 
+    area_idx_1 = boot_req->br_slot_areas[0] + idx;
+    area_idx_2 = boot_req->br_slot_areas[1] + idx;
     assert(area_idx_1 != area_idx_2);
     assert(area_idx_1 != boot_req->br_scratch_area_idx);
     assert(area_idx_2 != boot_req->br_scratch_area_idx);
 
-    image_idx_1 = boot_find_image_area_idx(area_idx_1);
-    assert(image_idx_1 != -1);
-
-    image_idx_2 = boot_find_image_area_idx(area_idx_2);
-    assert(image_idx_2 != -1);
-
-    scratch_image_idx =
-        boot_find_image_area_idx(boot_req->br_scratch_area_idx);
-    assert(scratch_image_idx != -1);
-
-    rc = boot_erase_area(boot_req->br_scratch_area_idx);
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = boot_copy_area(area_idx_2, boot_req->br_scratch_area_idx);
-    if (rc != 0) {
-        return rc;
-    }
-
-    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();
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = boot_erase_area(area_idx_2);
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = boot_copy_area(area_idx_1, area_idx_2);
-    if (rc != 0) {
-        return rc;
-    }
+    state = BOOT_PERSIST_ST(boot_state.state);
+    if (state == 0) {
+        rc = boot_erase_area(boot_req->br_scratch_area_idx, sz);
+        if (rc != 0) {
+            return rc;
+        }
 
-    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;
-    }
+        rc = boot_copy_area(area_idx_2, boot_req->br_scratch_area_idx, sz);
+        if (rc != 0) {
+            return rc;
+        }
 
-    rc = boot_erase_area(area_idx_1);
-    if (rc != 0) {
-        return rc;
+        boot_state.state = BOOT_PERSIST(idx, 1);
+        rc = boot_write_status(&boot_state);
+        if (rc != 0) {
+            return rc;
+        }
+        state = 1;
     }
+    if (state == 1) {
+        rc = boot_erase_area(area_idx_2, sz);
+        if (rc != 0) {
+            return rc;
+        }
 
-    rc = boot_copy_area(boot_req->br_scratch_area_idx, area_idx_1);
-    if (rc != 0) {
-        return rc;
-    }
+        rc = boot_copy_area(area_idx_1, area_idx_2, sz);
+        if (rc != 0) {
+            return rc;
+        }
 
-    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;
+        boot_state.state = BOOT_PERSIST(idx, 2);
+        rc = boot_write_status(&boot_state);
+        if (rc != 0) {
+            return rc;
+        }
+        state = 2;
     }
-
-    return 0;
-}
-
-static int
-boot_fill_slot(int img_num, uint32_t img_length, int start_area_idx)
-{
-    const struct flash_area *area_desc;
-    uint32_t off;
-    int dst_image_area_idx;
-    int src_area_idx;
-    int dst_area_idx;
-    int src_img_num;
-    int dst_img_num;
-    int part_num;
-    int rc;
-
-    part_num = 0;
-    off = 0;
-    while (off < img_length) {
-        /* Determine which area contains the current part of the image that
-         * we want to boot.
-         */
-        src_area_idx = boot_find_image_part(img_num, part_num);
-        if (src_area_idx == -1) {
-            return BOOT_EBADIMAGE;
+    if (state == 2) {
+        rc = boot_erase_area(area_idx_1, sz);
+        if (rc != 0) {
+            return rc;
         }
 
-        /* Determine which area we want to copy the source to. */
-        dst_area_idx = start_area_idx + part_num;
-
-        if (src_area_idx != dst_area_idx) {
-            /* Determine what is currently in the destination area. */
-            dst_image_area_idx = boot_find_image_area_idx(dst_area_idx);
-
-            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
-                 * need to back up its contents.
-                 */
-                rc = boot_move_area(src_area_idx, dst_area_idx,
-                                    img_num, part_num);
-            } else {
-                /* Swap the two areas. */
-                src_img_num = img_num ^ 1;
-                dst_img_num = img_num;
-                rc = boot_swap_areas(src_area_idx, src_img_num, part_num,
-                                     dst_area_idx, dst_img_num, part_num);
-            }
-            if (rc != 0) {
-                return rc;
-            }
+        rc = boot_copy_area(boot_req->br_scratch_area_idx, area_idx_1, sz);
+        if (rc != 0) {
+            return rc;
         }
 
-        area_desc = boot_req->br_area_descs + dst_area_idx;
-        off += area_desc->fa_size;
-
-        part_num++;
+        boot_state.state = BOOT_PERSIST(idx + 1, 0);
+        rc = boot_write_status(&boot_state);
+        if (rc != 0) {
+            return rc;
+        }
+        state = 3;
     }
-
     return 0;
 }
 
@@ -486,59 +318,25 @@ boot_fill_slot(int img_num, uint32_t img_length, int start_area_idx)
  * @return                      0 on success; nonzero on failure.
  */
 static int
-boot_copy_image(uint32_t img1_length, uint32_t img2_length)
-{
-    int rc;
-
-    rc = boot_fill_slot(1, img2_length, boot_slot_to_area_idx(0));
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = boot_fill_slot(0, img1_length, boot_slot_to_area_idx(1));
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-/**
- * Builds a single default boot status for the specified image slot.
- */
-static void
-boot_build_status_one(int image_num, uint8_t flash_id, uint32_t addr,
-                      uint32_t length)
+boot_copy_image(void)
 {
-    uint32_t offset;
-    int area_idx = 0;
-    int part_num;
+    uint32_t off;
+    uint32_t sz;
     int i;
-
-    for (i = 0; i < boot_req->br_num_image_areas; i++) {
-        area_idx = boot_req->br_image_areas[i];
-        if (boot_req->br_area_descs[area_idx].fa_off == addr &&
-            boot_req->br_area_descs[area_idx].fa_flash_id == flash_id) {
-            break;
+    int cnt;
+    int rc;
+    int state_idx;
+
+    state_idx = BOOT_PERSIST_IDX(boot_state.state);
+    for (off = 0, i = 0; off < boot_state.length; off += sz, i += cnt) {
+        sz = boot_copy_sz(i, boot_req->br_slot_areas[1], &cnt);
+        if (i >= state_idx) {
+            rc = boot_swap_areas(i, sz);
+            assert(rc == 0);
         }
     }
 
-    assert(i < boot_req->br_num_image_areas);
-
-    offset = 0;
-    part_num = 0;
-    while (offset < length) {
-        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++;
-        i++;
-        area_idx++;
-    }
-
-    assert(i <= boot_req->br_num_image_areas);
+    return 0;
 }
 
 /**
@@ -550,30 +348,27 @@ boot_build_status_one(int image_num, uint8_t flash_id, uint32_t addr,
 static void
 boot_build_status(void)
 {
-    uint8_t flash_id;
-    uint32_t address;
-    uint32_t len;
-
-    memset(boot_st->entries, 0xff,
-           boot_req->br_num_image_areas * sizeof boot_st->entries[0]);
+    uint32_t len1;
+    uint32_t len2;
 
     if (boot_img_hdrs[0].ih_magic == IMAGE_MAGIC) {
-        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);
+        len1 = boot_img_hdrs[0].ih_hdr_size + boot_img_hdrs[0].ih_img_size +
+          boot_img_hdrs[0].ih_tlv_size;
     } else {
-        boot_st->status.bs_img1_length = 0;
+        len1 = 0;
     }
 
     if (boot_img_hdrs[1].ih_magic == IMAGE_MAGIC) {
-        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);
+        len2 = boot_img_hdrs[1].ih_hdr_size + boot_img_hdrs[1].ih_img_size +
+          boot_img_hdrs[0].ih_tlv_size;
     } else {
-        boot_st->status.bs_img2_length = 0;
+        len2 = 0;
     }
+    boot_state.length = len1;
+    if (len1 < len2) {
+        boot_state.length = len2;
+    }
+    boot_state.state = 0;
 }
 
 /**
@@ -604,18 +399,10 @@ 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_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;
-    }
-
-    if (boot_read_status()) {
+    if (boot_read_status(&boot_state)) {
         /* We are resuming an interrupted image copy. */
         /* XXX if copy has not actually started yet, validate image */
-        rc = boot_copy_image(boot_st->status.bs_img1_length,
-                             boot_st->status.bs_img2_length);
+        rc = boot_copy_image();
         if (rc != 0) {
             /* We failed to put the images back together; there is really no
              * solution here.
@@ -673,9 +460,7 @@ 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_st->status.bs_img1_length,
-                             boot_st->status.bs_img2_length);
-
+        rc = boot_copy_image();
         if (rc != 0) {
             /* We failed to put the images back together; there is really no
              * solution here.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/705d3346/libs/bootutil/src/test/boot_test.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/test/boot_test.c b/libs/bootutil/src/test/boot_test.c
index 9a3513c..2ea9c9d 100644
--- a/libs/bootutil/src/test/boot_test.c
+++ b/libs/bootutil/src/test/boot_test.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,
@@ -25,11 +25,13 @@
 #include <inttypes.h>
 #include "testutil/testutil.h"
 #include "hal/hal_flash.h"
+#include "hal/flash_map.h"
 #include "fs/fs.h"
-#include "fs/fsutil.h"
 #include "nffs/nffs.h"
+#include <config/config_file.h>
 #include "bootutil/image.h"
 #include "bootutil/loader.h"
+#include "bootutil/bootutil_misc.h"
 #include "../src/bootutil_priv.h"
 
 #include "mbedtls/sha256.h"
@@ -37,37 +39,26 @@
 #define BOOT_TEST_HEADER_SIZE       0x200
 
 /** Internal flash layout. */
-static struct nffs_area_desc boot_test_area_descs[] = {
-    [0] =  { 0x00000000, 16 * 1024 },
-    [1] =  { 0x00004000, 16 * 1024 },
-    [2] =  { 0x00008000, 16 * 1024 },
-    [3] =  { 0x0000c000, 16 * 1024 },
-    [4] =  { 0x00010000, 64 * 1024 },
-    [5] =  { 0x00020000, 128 * 1024 },
-    [6] =  { 0x00040000, 128 * 1024 },
-    [7] =  { 0x00060000, 128 * 1024 },
-    [8] =  { 0x00080000, 128 * 1024 },
-    [9] =  { 0x000a0000, 128 * 1024 },
-    [10] = { 0x000c0000, 128 * 1024 },
-    [11] = { 0x000e0000, 128 * 1024 },
-    { 0, 0 },
-};
-
-static const struct nffs_area_desc boot_test_format_descs[] = {
-    [0] =  { 0x00004000, 16 * 1024 },
-    [1] =  { 0x00008000, 16 * 1024 },
-    [2] =  { 0x0000c000, 16 * 1024 },
-    { 0, 0 },
+static struct flash_area boot_test_area_descs[] = {
+    [0] = { .fa_off = 0x00020000, .fa_size = 128 * 1024 },
+    [1] = { .fa_off = 0x00040000, .fa_size = 128 * 1024 },
+    [2] = { .fa_off = 0x00060000, .fa_size = 128 * 1024 },
+    [3] = { .fa_off = 0x00080000, .fa_size = 128 * 1024 },
+    [4] = { .fa_off = 0x000a0000, .fa_size = 128 * 1024 },
+    [5] = { .fa_off = 0x000c0000, .fa_size = 128 * 1024 },
+    [6] = { .fa_off = 0x000e0000, .fa_size = 128 * 1024 },
 };
 
-/** Contains indices of the areas which can contain image data. */
-static uint8_t boot_test_img_areas[] = {
-    5, 6, 7, 8, 9, 10, 11
+static const struct flash_area boot_test_format_descs[] = {
+    [0] = { .fa_off = 0x00004000, .fa_size = 16 * 1024 },
+    [1] = { .fa_off = 0x00008000, .fa_size = 16 * 1024 },
+    [2] = { .fa_off = 0x0000c000, .fa_size = 16 * 1024 },
+    [3] = { .fa_off = 0, .fa_size = 0 },
 };
 
 /** Areas representing the beginning of image slots. */
 static uint8_t boot_test_slot_areas[] = {
-    5, 8,
+    0, 3,
 };
 
 /** Flash offsets of the two image slots. */
@@ -79,11 +70,13 @@ static struct {
     { 0, 0x80000 },
 };
 
-#define BOOT_TEST_NUM_IMG_AREAS \
-    ((int)(sizeof boot_test_img_areas / sizeof boot_test_img_areas[0]))
+#define BOOT_TEST_AREA_IDX_SCRATCH 6
+
+#define MY_CONF_PATH "/cfg/run"
 
-#define BOOT_TEST_AREA_IDX_SCRATCH 11
-#define BOOT_TEST_IMG_AREA_IDX_SCRATCH 6
+static struct conf_file my_conf = {
+    .cf_name = MY_CONF_PATH
+};
 
 static uint8_t
 boot_test_util_byte_at(int img_msb, uint32_t image_offset)
@@ -100,60 +93,61 @@ boot_test_util_byte_at(int img_msb, uint32_t image_offset)
 static void
 boot_test_util_init_flash(void)
 {
-    const struct nffs_area_desc *area_desc;
+    const struct flash_area *area_desc;
     int rc;
+    struct nffs_area_desc nffs_descs[32];
+    int cnt;
 
     rc = hal_flash_init();
     TEST_ASSERT(rc == 0);
 
     for (area_desc = boot_test_area_descs;
-         area_desc->nad_length != 0;
+         area_desc->fa_size != 0;
          area_desc++) {
 
-        rc = hal_flash_erase(area_desc->nad_flash_id, area_desc->nad_offset,
-                             area_desc->nad_length);
+        rc = flash_area_erase(area_desc, 0, area_desc->fa_size);
         TEST_ASSERT(rc == 0);
     }
+    cnt = 32;
 
-    rc = nffs_init();
+    rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, nffs_descs);
     TEST_ASSERT(rc == 0);
 
-    rc = nffs_format(boot_test_format_descs);
+    rc = nffs_init();
     TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/boot");
+    rc = nffs_format(nffs_descs);
     TEST_ASSERT(rc == 0);
+
+    fs_mkdir("/cfg");
 }
 
 static void
 boot_test_util_copy_area(int from_area_idx, int to_area_idx)
 {
-    const struct nffs_area_desc *from_area_desc;
-    const struct nffs_area_desc *to_area_desc;
+    const struct flash_area *from_area_desc;
+    const struct flash_area *to_area_desc;
     void *buf;
     int rc;
 
     from_area_desc = boot_test_area_descs + from_area_idx;
     to_area_desc = boot_test_area_descs + to_area_idx;
 
-    TEST_ASSERT(from_area_desc->nad_length == to_area_desc->nad_length);
+    TEST_ASSERT(from_area_desc->fa_size == to_area_desc->fa_size);
 
-    buf = malloc(from_area_desc->nad_length);
+    buf = malloc(from_area_desc->fa_size);
     TEST_ASSERT(buf != NULL);
 
-    rc = hal_flash_read(from_area_desc->nad_flash_id,
-                        from_area_desc->nad_offset, buf,
-                        from_area_desc->nad_length);
+    rc = flash_area_read(from_area_desc, 0, buf,
+                         from_area_desc->fa_size);
     TEST_ASSERT(rc == 0);
 
-    rc = hal_flash_erase(to_area_desc->nad_flash_id,
-                         to_area_desc->nad_offset,
-                         to_area_desc->nad_length);
+    rc = flash_area_erase(to_area_desc,
+                          0,
+                          to_area_desc->fa_size);
     TEST_ASSERT(rc == 0);
 
-    rc = hal_flash_write(to_area_desc->nad_flash_id,
-                         to_area_desc->nad_offset, buf,
-                         to_area_desc->nad_length);
+    rc = flash_area_write(to_area_desc, 0, buf,
+                          to_area_desc->fa_size);
     TEST_ASSERT(rc == 0);
 
     free(buf);
@@ -162,8 +156,8 @@ boot_test_util_copy_area(int from_area_idx, int to_area_idx)
 static void
 boot_test_util_swap_areas(int area_idx1, int area_idx2)
 {
-    const struct nffs_area_desc *area_desc1;
-    const struct nffs_area_desc *area_desc2;
+    const struct flash_area *area_desc1;
+    const struct flash_area *area_desc2;
     void *buf1;
     void *buf2;
     int rc;
@@ -171,36 +165,30 @@ boot_test_util_swap_areas(int area_idx1, int area_idx2)
     area_desc1 = boot_test_area_descs + area_idx1;
     area_desc2 = boot_test_area_descs + area_idx2;
 
-    TEST_ASSERT(area_desc1->nad_length == area_desc2->nad_length);
+    TEST_ASSERT(area_desc1->fa_size == area_desc2->fa_size);
 
-    buf1 = malloc(area_desc1->nad_length);
+    buf1 = malloc(area_desc1->fa_size);
     TEST_ASSERT(buf1 != NULL);
 
-    buf2 = malloc(area_desc2->nad_length);
+    buf2 = malloc(area_desc2->fa_size);
     TEST_ASSERT(buf2 != NULL);
 
-    rc = hal_flash_read(area_desc1->nad_flash_id, area_desc1->nad_offset,
-                        buf1, area_desc1->nad_length);
+    rc = flash_area_read(area_desc1, 0, buf1, area_desc1->fa_size);
     TEST_ASSERT(rc == 0);
 
-    rc = hal_flash_read(area_desc2->nad_flash_id, area_desc2->nad_offset,
-                        buf2, area_desc2->nad_length);
+    rc = flash_area_read(area_desc2, 0, buf2, area_desc2->fa_size);
     TEST_ASSERT(rc == 0);
 
-    rc = hal_flash_erase(area_desc1->nad_flash_id, area_desc1->nad_offset,
-                         area_desc1->nad_length);
+    rc = flash_area_erase(area_desc1, 0, area_desc1->fa_size);
     TEST_ASSERT(rc == 0);
 
-    rc = hal_flash_erase(area_desc2->nad_flash_id, area_desc2->nad_offset,
-                         area_desc2->nad_length);
+    rc = flash_area_erase(area_desc2, 0, area_desc2->fa_size);
     TEST_ASSERT(rc == 0);
 
-    rc = hal_flash_write(area_desc1->nad_flash_id, area_desc1->nad_offset,
-                         buf2, area_desc1->nad_length);
+    rc = flash_area_write(area_desc1, 0, buf2, area_desc1->fa_size);
     TEST_ASSERT(rc == 0);
 
-    rc = hal_flash_write(area_desc2->nad_flash_id, area_desc2->nad_offset,
-                         buf1, area_desc2->nad_length);
+    rc = flash_area_write(area_desc2, 0, buf1, area_desc2->fa_size);
     TEST_ASSERT(rc == 0);
 
     free(buf1);
@@ -291,7 +279,7 @@ boot_test_util_write_hash(const struct image_header *hdr, int slot)
 }
 
 static void
-boot_test_util_verify_area(const struct nffs_area_desc *area_desc,
+boot_test_util_verify_area(const struct flash_area *area_desc,
                            const struct image_header *hdr,
                            uint32_t image_addr, int img_msb)
 {
@@ -309,13 +297,13 @@ boot_test_util_verify_area(const struct nffs_area_desc *area_desc,
     int rc;
     int i;
 
-    addr = area_desc->nad_offset;
+    addr = area_desc->fa_off;
 
     if (hdr != NULL) {
         img_size = hdr->ih_img_size;
 
         if (addr == image_addr) {
-            rc = hal_flash_read(area_desc->nad_flash_id, image_addr,
+            rc = hal_flash_read(area_desc->fa_flash_id, image_addr,
                                 &temp_hdr, sizeof temp_hdr);
             TEST_ASSERT(rc == 0);
             TEST_ASSERT(memcmp(&temp_hdr, hdr, sizeof *hdr) == 0);
@@ -326,7 +314,7 @@ boot_test_util_verify_area(const struct nffs_area_desc *area_desc,
         img_size = 0;
     }
 
-    area_end = area_desc->nad_offset + area_desc->nad_length;
+    area_end = area_desc->fa_off + area_desc->fa_size;
     img_end = image_addr + img_size;
     past_image = addr >= img_end;
 
@@ -346,7 +334,7 @@ boot_test_util_verify_area(const struct nffs_area_desc *area_desc,
             chunk_sz = rem_area;
         }
 
-        rc = hal_flash_read(area_desc->nad_flash_id, addr, buf, chunk_sz);
+        rc = hal_flash_read(area_desc->fa_flash_id, addr, buf, chunk_sz);
         TEST_ASSERT(rc == 0);
 
         for (i = 0; i < chunk_sz; i++) {
@@ -367,24 +355,55 @@ boot_test_util_verify_status_clear(void)
 {
     struct fs_file *file;
     int rc;
+    int empty = 1;
+    char *needle = "boot/status=";
+    int nlen = strlen(needle);
+    uint32_t len, hlen;
+    char *haystack, *ptr;
+
+    rc = fs_open(MY_CONF_PATH, FS_ACCESS_READ, &file);
+    if (rc != 0) {
+        return;
+    }
+    rc = fs_filelen(file, &len);
+    TEST_ASSERT(rc == 0);
+
+    haystack = malloc(len + 1);
+    TEST_ASSERT(haystack);
 
-    rc = fs_open(BOOT_PATH_STATUS, FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == FS_ENOENT);
+    rc = fs_read(file, len, haystack, &hlen);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(hlen == len);
+    haystack[len] = '\0';
+
+    fs_close(file);
+
+    ptr = haystack;
+    while ((ptr = strstr(ptr, needle))) {
+        if (ptr[nlen] == '\n') {
+            empty = 1;
+        } else {
+            empty = 0;
+        }
+        ptr += nlen;
+    }
+    TEST_ASSERT(empty == 1);
+    free(haystack);
 }
 
 static void
 boot_test_util_verify_flash(const struct image_header *hdr0, int orig_slot_0,
                             const struct image_header *hdr1, int orig_slot_1)
 {
-    const struct nffs_area_desc *area_desc;
+    const struct flash_area *area_desc;
     int area_idx;
 
-    area_idx = boot_test_img_areas[0];
+    area_idx = 0;
 
     while (1) {
         area_desc = boot_test_area_descs + area_idx;
-        if (area_desc->nad_offset == boot_test_img_addrs[1].address &&
-            area_desc->nad_flash_id == boot_test_img_addrs[1].flash_id) {
+        if (area_desc->fa_off == boot_test_img_addrs[1].address &&
+            area_desc->fa_flash_id == boot_test_img_addrs[1].flash_id) {
             break;
         }
 
@@ -405,6 +424,18 @@ boot_test_util_verify_flash(const struct image_header *hdr0, int orig_slot_0,
     }
 }
 
+TEST_CASE(boot_test_setup)
+{
+    int rc;
+
+    rc = conf_file_src(&my_conf);
+    assert(rc == 0);
+    rc = conf_file_dst(&my_conf);
+    assert(rc == 0);
+
+    bootutil_cfg_register();
+}
+
 TEST_CASE(boot_test_nv_ns_10)
 {
     struct boot_rsp rsp;
@@ -421,9 +452,8 @@ TEST_CASE(boot_test_nv_ns_10)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
     };
 
@@ -459,10 +489,9 @@ TEST_CASE(boot_test_nv_ns_01)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -505,10 +534,9 @@ TEST_CASE(boot_test_nv_ns_11)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -545,17 +573,16 @@ TEST_CASE(boot_test_vm_ns_10)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
     boot_test_util_write_image(&hdr, 0);
     boot_test_util_write_hash(&hdr, 0);
 
-    rc = fsutil_write_file(BOOT_PATH_MAIN, &hdr.ih_ver, sizeof hdr.ih_ver);
+    rc = boot_vect_write_main(&hdr.ih_ver);
     TEST_ASSERT(rc == 0);
 
     rc = boot_go(&req, &rsp);
@@ -574,7 +601,6 @@ TEST_CASE(boot_test_vm_ns_01)
     struct boot_rsp rsp;
     int rc;
 
-
     struct image_header hdr = {
         .ih_magic = IMAGE_MAGIC,
         .ih_tlv_size = 4 + 32,
@@ -586,17 +612,16 @@ TEST_CASE(boot_test_vm_ns_01)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
     boot_test_util_write_image(&hdr, 1);
     boot_test_util_write_hash(&hdr, 1);
 
-    rc = fsutil_write_file(BOOT_PATH_MAIN, &hdr.ih_ver, sizeof hdr.ih_ver);
+    rc = boot_vect_write_main(&hdr.ih_ver);
     TEST_ASSERT(rc == 0);
 
     rc = boot_go(&req, &rsp);
@@ -635,10 +660,9 @@ TEST_CASE(boot_test_vm_ns_11_a)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -647,7 +671,7 @@ TEST_CASE(boot_test_vm_ns_11_a)
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = fsutil_write_file(BOOT_PATH_MAIN, &hdr0.ih_ver, sizeof hdr0.ih_ver);
+    rc = boot_vect_write_main(&hdr0.ih_ver);
     TEST_ASSERT(rc == 0);
 
     rc = boot_go(&req, &rsp);
@@ -686,10 +710,9 @@ TEST_CASE(boot_test_vm_ns_11_b)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -698,7 +721,7 @@ TEST_CASE(boot_test_vm_ns_11_b)
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = fsutil_write_file(BOOT_PATH_MAIN, &hdr1.ih_ver, sizeof hdr1.ih_ver);
+    rc = boot_vect_write_main(&hdr1.ih_ver);
     TEST_ASSERT(rc == 0);
 
     rc = boot_go(&req, &rsp);
@@ -737,10 +760,9 @@ TEST_CASE(boot_test_vm_ns_11_2areas)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -749,7 +771,7 @@ TEST_CASE(boot_test_vm_ns_11_2areas)
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = fsutil_write_file(BOOT_PATH_MAIN, &hdr1.ih_ver, sizeof hdr1.ih_ver);
+    rc = boot_vect_write_main(&hdr1.ih_ver);
     TEST_ASSERT(rc == 0);
 
     rc = boot_go(&req, &rsp);
@@ -765,7 +787,6 @@ TEST_CASE(boot_test_vm_ns_11_2areas)
 
 TEST_CASE(boot_test_nv_bs_10)
 {
-    struct boot_status_entry entries[BOOT_TEST_NUM_IMG_AREAS];
     struct boot_status status;
     struct boot_rsp rsp;
     int rc;
@@ -781,27 +802,23 @@ TEST_CASE(boot_test_nv_bs_10)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
     boot_test_util_write_image(&hdr, 0);
     boot_test_util_write_hash(&hdr, 0);
-    boot_test_util_swap_areas(boot_test_img_areas[0],
-                                BOOT_TEST_AREA_IDX_SCRATCH);
-
-    memset(&status, 0xff, sizeof status);
-    status.bs_img2_length = hdr.ih_img_size;
+    boot_test_util_swap_areas(boot_test_slot_areas[1],
+      BOOT_TEST_AREA_IDX_SCRATCH);
 
-    memset(entries, 0xff, sizeof entries);
-    entries[BOOT_TEST_IMG_AREA_IDX_SCRATCH].bse_image_num = 1;
-    entries[BOOT_TEST_IMG_AREA_IDX_SCRATCH].bse_part_num = 0;
+    status.length = hdr.ih_hdr_size + hdr.ih_img_size + hdr.ih_tlv_size;
+    status.state = 1;
 
-    rc = boot_write_status(&status, entries, BOOT_TEST_NUM_IMG_AREAS);
+    rc = boot_write_status(&status);
     TEST_ASSERT(rc == 0);
+    conf_load();
 
     rc = boot_go(&req, &rsp);
     TEST_ASSERT(rc == 0);
@@ -816,9 +833,9 @@ TEST_CASE(boot_test_nv_bs_10)
 
 TEST_CASE(boot_test_nv_bs_11)
 {
-    struct boot_status_entry entries[BOOT_TEST_NUM_IMG_AREAS];
     struct boot_status status;
     struct boot_rsp rsp;
+    int len;
     int rc;
 
     struct image_header hdr0 = {
@@ -841,10 +858,9 @@ TEST_CASE(boot_test_nv_bs_11)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -852,20 +868,17 @@ 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);
-    boot_test_util_copy_area(boot_test_img_areas[0],
-                             BOOT_TEST_AREA_IDX_SCRATCH);
+    boot_test_util_copy_area(boot_test_slot_areas[1],
+      BOOT_TEST_AREA_IDX_SCRATCH);
 
-    memset(&status, 0xff, sizeof status);
-    status.bs_img1_length = hdr0.ih_img_size;
-    status.bs_img2_length = hdr1.ih_img_size;
-
-    memset(entries, 0xff, sizeof entries);
-    entries[3].bse_image_num = 1;
-    entries[3].bse_part_num = 0;
-    entries[BOOT_TEST_IMG_AREA_IDX_SCRATCH].bse_image_num = 0;
-    entries[BOOT_TEST_IMG_AREA_IDX_SCRATCH].bse_part_num = 0;
+    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
+    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
+    if (len > status.length) {
+        status.length = len;
+    }
+    status.state = 1;
 
-    rc = boot_write_status(&status, entries, BOOT_TEST_NUM_IMG_AREAS);
+    rc = boot_write_status(&status);
     TEST_ASSERT(rc == 0);
 
     rc = boot_go(&req, &rsp);
@@ -881,10 +894,10 @@ TEST_CASE(boot_test_nv_bs_11)
 
 TEST_CASE(boot_test_nv_bs_11_2areas)
 {
-    struct boot_status_entry entries[BOOT_TEST_NUM_IMG_AREAS];
     struct boot_status status;
     struct boot_rsp rsp;
     int rc;
+    int len;
 
     struct image_header hdr0 = {
         .ih_magic = IMAGE_MAGIC,
@@ -906,10 +919,9 @@ TEST_CASE(boot_test_nv_bs_11_2areas)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -917,23 +929,17 @@ TEST_CASE(boot_test_nv_bs_11_2areas)
     boot_test_util_write_hash(&hdr0, 0);
     boot_test_util_write_image(&hdr1, 1);
     boot_test_util_write_hash(&hdr1, 1);
-    boot_test_util_swap_areas(boot_test_img_areas[0], boot_test_img_areas[3]);
-
-    memset(&status, 0xff, sizeof status);
-    status.bs_img1_length = hdr0.ih_img_size;
-    status.bs_img2_length = hdr1.ih_img_size;
-
-    memset(entries, 0xff, sizeof entries);
-    entries[0].bse_image_num = 1;
-    entries[0].bse_part_num = 0;
-    entries[1].bse_image_num = 0;
-    entries[1].bse_part_num = 1;
-    entries[3].bse_image_num = 0;
-    entries[3].bse_part_num = 0;
-    entries[4].bse_image_num = 1;
-    entries[4].bse_part_num = 1;
-
-    rc = boot_write_status(&status, entries, BOOT_TEST_NUM_IMG_AREAS);
+    boot_test_util_swap_areas(boot_test_slot_areas[0],
+      boot_test_slot_areas[1]);
+
+    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
+    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
+    if (len > status.length) {
+        status.length = len;
+    }
+    status.state = 1 << 8;
+
+    rc = boot_write_status(&status);
     TEST_ASSERT(rc == 0);
 
     rc = boot_go(&req, &rsp);
@@ -973,10 +979,9 @@ TEST_CASE(boot_test_vb_ns_11)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
     };
 
     boot_test_util_init_flash();
@@ -985,10 +990,10 @@ TEST_CASE(boot_test_vb_ns_11)
     boot_test_util_write_hash(&hdr0, 0);
     boot_test_util_write_hash(&hdr1, 1);
 
-    rc = fsutil_write_file(BOOT_PATH_MAIN, &hdr0.ih_ver, sizeof hdr0.ih_ver);
+    rc = boot_vect_write_main(&hdr0.ih_ver);
     TEST_ASSERT(rc == 0);
 
-    rc = fsutil_write_file(BOOT_PATH_TEST, &hdr1.ih_ver, sizeof hdr1.ih_ver);
+    rc = boot_vect_write_test(&hdr1.ih_ver);
     TEST_ASSERT(rc == 0);
 
     /* First boot should use the test image. */
@@ -1032,9 +1037,8 @@ TEST_CASE(boot_test_no_hash)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
     };
 
@@ -1064,9 +1068,8 @@ TEST_CASE(boot_test_no_flag_has_hash)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
     };
 
@@ -1097,9 +1100,8 @@ TEST_CASE(boot_test_invalid_hash)
 
     struct boot_req req = {
         .br_area_descs = boot_test_area_descs,
-        .br_image_areas = boot_test_img_areas,
         .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_NUM_IMG_AREAS,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
         .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
     };
 
@@ -1123,6 +1125,7 @@ TEST_CASE(boot_test_invalid_hash)
 
 TEST_SUITE(boot_test_main)
 {
+    boot_test_setup();
     boot_test_nv_ns_10();
     boot_test_nv_ns_01();
     boot_test_nv_ns_11();


[04/14] incubator-mynewt-core git commit: bootutil; remove nffs dependency from loader.c; switch to using flash_area instead of nffs_desc_area to pass sector info.

Posted by ma...@apache.org.
bootutil; remove nffs dependency from loader.c;
switch to using flash_area instead of nffs_desc_area to pass
sector info.


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

Branch: refs/heads/develop
Commit: 094ed5043607c401a6983e4d64ff791e84458723
Parents: 1076c9d
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Apr 12 16:49:53 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:45:51 2016 -0700

----------------------------------------------------------------------
 apps/boot/src/boot.c                    |  8 ++---
 libs/bootutil/include/bootutil/loader.h |  6 +---
 libs/bootutil/src/loader.c              | 53 ++++++++++++++--------------
 3 files changed, 31 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/094ed504/apps/boot/src/boot.c
----------------------------------------------------------------------
diff --git a/apps/boot/src/boot.c b/apps/boot/src/boot.c
index 2b8a073..cd69642 100755
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@ -40,7 +40,7 @@ int
 main(void)
 {
     struct nffs_area_desc nffs_descs[NFFS_AREA_MAX + 1];
-    struct nffs_area_desc descs[AREA_DESC_MAX];
+    struct flash_area descs[AREA_DESC_MAX];
     /** Contains indices of the areas which can contain image data. */
     uint8_t img_areas[AREA_DESC_MAX];
     /** Areas representing the beginning of image slots. */
@@ -61,20 +61,20 @@ main(void)
     assert(rc == 0);
 
     cnt = BOOT_AREA_DESC_MAX;
-    rc = flash_area_to_nffs_desc(FLASH_AREA_IMAGE_0, &cnt, descs);
+    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &cnt, descs);
     img_starts[0] = 0;
     total = cnt;
 
     cnt = BOOT_AREA_DESC_MAX - total;
     assert(cnt >= 0);
-    rc = flash_area_to_nffs_desc(FLASH_AREA_IMAGE_1, &cnt, &descs[total]);
+    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &cnt, &descs[total]);
     assert(rc == 0);
     img_starts[1] = total;
     total += cnt;
 
     cnt = BOOT_AREA_DESC_MAX - total;
     assert(cnt >= 0);
-    rc = flash_area_to_nffs_desc(FLASH_AREA_IMAGE_SCRATCH, &cnt, &descs[total]);
+    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_SCRATCH, &cnt, &descs[total]);
     assert(rc == 0);
     req.br_scratch_area_idx = total;
     total += cnt;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/094ed504/libs/bootutil/include/bootutil/loader.h
----------------------------------------------------------------------
diff --git a/libs/bootutil/include/bootutil/loader.h b/libs/bootutil/include/bootutil/loader.h
index 559f4d4..1f82519 100644
--- a/libs/bootutil/include/bootutil/loader.h
+++ b/libs/bootutil/include/bootutil/loader.h
@@ -21,7 +21,6 @@
 #define H_LOADER_
 
 #include <inttypes.h>
-struct nffs_area_desc;
 struct image_header;
 
 /** A request object instructing the boot loader how to proceed. */
@@ -30,7 +29,7 @@ struct boot_req {
      * Array of area descriptors indicating the layout of flash(es); must
      * be terminated with a 0-length element.
      */
-    struct nffs_area_desc *br_area_descs;
+    struct flash_area *br_area_descs;
 
     /**
      * Array of indices of elements in the br_area_descs array; indicates which
@@ -52,9 +51,6 @@ struct boot_req {
 
     /** The index of the area to use as the image scratch area. */
     uint8_t br_scratch_area_idx;
-    
-    /** the location of the nffs area index within the area descriptors above*/
-    uint8_t br_nffs_area_idx;
 };
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/094ed504/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index cb0cf68..061e60e 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -21,10 +21,9 @@
 #include <stddef.h>
 #include <inttypes.h>
 #include <string.h>
-#include "hal/hal_flash.h"
+#include <hal/hal_flash.h>
+#include <hal/flash_map.h>
 #include "os/os_malloc.h"
-#include "nffs/nffs.h"
-#include "fs/fs.h"
 #include "bootutil/loader.h"
 #include "bootutil/image.h"
 #include "bootutil_priv.h"
@@ -50,15 +49,15 @@ static struct boot_state *boot;
 static void
 boot_slot_addr(int slot_num, uint8_t *flash_id, uint32_t *address)
 {
-    const struct nffs_area_desc *area_desc;
+    const struct flash_area *area_desc;
     uint8_t area_idx;
 
     assert(slot_num >= 0 && slot_num < BOOT_NUM_SLOTS);
 
     area_idx = boot_req->br_slot_areas[slot_num];
     area_desc = boot_req->br_area_descs + area_idx;
-    *flash_id = area_desc->nad_flash_id;
-    *address = area_desc->nad_offset;
+    *flash_id = area_desc->fa_flash_id;
+    *address = area_desc->fa_off;
 }
 
 /**
@@ -157,10 +156,10 @@ boot_slot_to_area_idx(int slot_num)
 
     assert(slot_num >= 0 && slot_num < BOOT_NUM_SLOTS);
 
-    for (i = 0; boot_req->br_area_descs[i].nad_length != 0; i++) {
+    for (i = 0; boot_req->br_area_descs[i].fa_size != 0; i++) {
         boot_slot_addr(slot_num, &flash_id, &address);
-        if (boot_req->br_area_descs[i].nad_offset == address &&
-          boot_req->br_area_descs[i].nad_flash_id == flash_id) {
+        if (boot_req->br_area_descs[i].fa_off == address &&
+          boot_req->br_area_descs[i].fa_flash_id == flash_id) {
 
             return i;
         }
@@ -195,12 +194,12 @@ boot_find_image_area_idx(int area_idx)
 static int
 boot_erase_area(int area_idx)
 {
-    const struct nffs_area_desc *area_desc;
+    const struct flash_area *area_desc;
     int rc;
 
     area_desc = boot_req->br_area_descs + area_idx;
-    rc = hal_flash_erase(area_desc->nad_flash_id, area_desc->nad_offset,
-                         area_desc->nad_length);
+    rc = hal_flash_erase(area_desc->fa_flash_id, area_desc->fa_off,
+                         area_desc->fa_size);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
@@ -220,8 +219,8 @@ boot_erase_area(int area_idx)
 static int
 boot_copy_area(int from_area_idx, int to_area_idx)
 {
-    const struct nffs_area_desc *from_area_desc;
-    const struct nffs_area_desc *to_area_desc;
+    const struct flash_area *from_area_desc;
+    const struct flash_area *to_area_desc;
     uint32_t from_addr;
     uint32_t to_addr;
     uint32_t off;
@@ -233,25 +232,25 @@ boot_copy_area(int from_area_idx, int to_area_idx)
     from_area_desc = boot_req->br_area_descs + from_area_idx;
     to_area_desc = boot_req->br_area_descs + to_area_idx;
 
-    assert(to_area_desc->nad_length >= from_area_desc->nad_length);
+    assert(to_area_desc->fa_size >= from_area_desc->fa_size);
 
     off = 0;
-    while (off < from_area_desc->nad_length) {
-        if (from_area_desc->nad_length - off > sizeof buf) {
+    while (off < from_area_desc->fa_size) {
+        if (from_area_desc->fa_size - off > sizeof buf) {
             chunk_sz = sizeof buf;
         } else {
-            chunk_sz = from_area_desc->nad_length - off;
+            chunk_sz = from_area_desc->fa_size - off;
         }
 
-        from_addr = from_area_desc->nad_offset + off;
-        rc = hal_flash_read(from_area_desc->nad_flash_id, from_addr, buf,
+        from_addr = from_area_desc->fa_off + off;
+        rc = hal_flash_read(from_area_desc->fa_flash_id, from_addr, buf,
                             chunk_sz);
         if (rc != 0) {
             return rc;
         }
 
-        to_addr = to_area_desc->nad_offset + off;
-        rc = hal_flash_write(to_area_desc->nad_flash_id, to_addr, buf,
+        to_addr = to_area_desc->fa_off + off;
+        rc = hal_flash_write(to_area_desc->fa_flash_id, to_addr, buf,
                              chunk_sz);
         if (rc != 0) {
             return rc;
@@ -418,7 +417,7 @@ boot_swap_areas(int area_idx_1, int img_num_1, uint8_t part_num_1,
 static int
 boot_fill_slot(int img_num, uint32_t img_length, int start_area_idx)
 {
-    const struct nffs_area_desc *area_desc;
+    const struct flash_area *area_desc;
     uint32_t off;
     int dst_image_area_idx;
     int src_area_idx;
@@ -467,7 +466,7 @@ boot_fill_slot(int img_num, uint32_t img_length, int start_area_idx)
         }
 
         area_desc = boot_req->br_area_descs + dst_area_idx;
-        off += area_desc->nad_length;
+        off += area_desc->fa_size;
 
         part_num++;
     }
@@ -516,8 +515,8 @@ boot_build_status_one(int image_num, uint8_t flash_id, uint32_t addr,
 
     for (i = 0; i < boot_req->br_num_image_areas; i++) {
         area_idx = boot_req->br_image_areas[i];
-        if (boot_req->br_area_descs[area_idx].nad_offset == addr &&
-            boot_req->br_area_descs[area_idx].nad_flash_id == flash_id) {
+        if (boot_req->br_area_descs[area_idx].fa_off == addr &&
+            boot_req->br_area_descs[area_idx].fa_flash_id == flash_id) {
             break;
         }
     }
@@ -531,7 +530,7 @@ boot_build_status_one(int image_num, uint8_t flash_id, uint32_t addr,
         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;
+        offset += boot_req->br_area_descs[area_idx].fa_size;
         part_num++;
         i++;
         area_idx++;


[14/14] incubator-mynewt-core git commit: config; don't write value to storage if it's not changing.

Posted by ma...@apache.org.
config; don't write value to storage if it's not changing.


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

Branch: refs/heads/develop
Commit: 39a6b3dff68e7f511175e58dd5753db6a6dea498
Parents: 07cdc2e
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 15 18:29:05 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 sys/config/src/config_store.c | 55 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39a6b3df/sys/config/src/config_store.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_store.c b/sys/config/src/config_store.c
index 1caf600..e0905a9 100644
--- a/sys/config/src/config_store.c
+++ b/sys/config/src/config_store.c
@@ -25,6 +25,12 @@
 #include "config/config.h"
 #include "config_priv.h"
 
+struct conf_dup_check_arg {
+    const char *name;
+    const char *val;
+    int is_dup;
+};
+
 struct conf_store_head conf_load_srcs = SLIST_HEAD_INITIALIZER(&conf_load_srcs);
 struct conf_store *conf_save_dst;
 
@@ -74,18 +80,65 @@ conf_load(void)
     return conf_commit(NULL);
 }
 
+static void
+conf_dup_check_cb(char *name, char *val, void *cb_arg)
+{
+    struct conf_dup_check_arg *cdca = (struct conf_dup_check_arg *)cb_arg;
+
+    if (strcmp(name, cdca->name)) {
+        return;
+    }
+    if (!val) {
+        if (!cdca->val) {
+            cdca->is_dup = 1;
+        } else {
+            cdca->is_dup = 0;
+        }
+    } else {
+        if (cdca->val && !strcmp(val, cdca->val)) {
+            cdca->is_dup = 1;
+        } else {
+            cdca->is_dup = 0;
+        }
+    }
+}
+
 /*
- * Append a single value to persisted config.
+ * Append a single value to persisted config. Don't store duplicate value.
  */
 int
 conf_save_one(const struct conf_handler *ch, const char *name, char *value)
 {
     struct conf_store *cs;
+    struct conf_dup_check_arg cdca;
+    char name_str[CONF_MAX_NAME_LEN];
+    int clen, nlen;
 
     cs = conf_save_dst;
     if (!cs) {
         return OS_ENOENT;
     }
+
+    /*
+     * Check if we're writing the same value again.
+     */
+    clen = strlen(ch->ch_name);
+    nlen = strlen(name);
+    if (clen + nlen + 1 > sizeof(name_str)) {
+        return OS_INVALID_PARM;
+    }
+    memcpy(name_str, ch->ch_name, clen);
+    name_str[clen++] = '/';
+    memcpy(name_str + clen, name, nlen);
+    name_str[clen + nlen] = '\0';
+
+    cdca.name = name_str;
+    cdca.val = value;
+    cdca.is_dup = 0;
+    cs->cs_itf->csi_load(cs, conf_dup_check_cb, &cdca);
+    if (cdca.is_dup == 1) {
+        return 0;
+    }
     return cs->cs_itf->csi_save(cs, ch, name, value);
 }
 


[02/14] incubator-mynewt-core git commit: bootutil; make boot_status persist write all data in single operation.

Posted by ma...@apache.org.
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 <ma...@runtime.io>
Authored: Tue Apr 12 16:02:05 2016 -0700
Committer: Marko Kiiskila <ma...@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


[13/14] incubator-mynewt-core git commit: config; error if val_str is NULL when calling conf_value_from_str().

Posted by ma...@apache.org.
config; error if val_str is NULL when calling conf_value_from_str().


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

Branch: refs/heads/develop
Commit: 38b4b3e0ddbd4bf9869347156b5b68524232cf99
Parents: 819b4af
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Apr 15 16:37:14 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Apr 18 10:49:42 2016 -0700

----------------------------------------------------------------------
 sys/config/src/config.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/38b4b3e0/sys/config/src/config.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config.c b/sys/config/src/config.c
index ceea6fa..bc6bf77 100644
--- a/sys/config/src/config.c
+++ b/sys/config/src/config.c
@@ -106,13 +106,20 @@ conf_value_from_str(char *val_str, enum conf_type type, void *vp, int maxlen)
     int32_t val;
     char *eptr;
 
+    if (!val_str) {
+        goto err;
+    }
     switch (type) {
     case CONF_INT8:
     case CONF_INT16:
     case CONF_INT32:
-        val = strtol(val_str, &eptr, 0);
-        if (*eptr != '\0') {
-            goto err;
+        if (val_str) {
+            val = strtol(val_str, &eptr, 0);
+            if (*eptr != '\0') {
+                goto err;
+            }
+        } else {
+            val = 0;
         }
         if (type == CONF_INT8) {
             if (val < INT8_MIN || val > UINT8_MAX) {