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/05/19 16:51:18 UTC
[06/18] incubator-mynewt-core git commit: imgmgr;
support downloading core files.
imgmgr; support downloading core files.
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/dfbac675
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/dfbac675
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/dfbac675
Branch: refs/heads/develop
Commit: dfbac6757f9158e4627eeeb0de6b43ab9afc87bf
Parents: a7d054c
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue May 17 15:22:19 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu May 19 09:34:27 2016 -0700
----------------------------------------------------------------------
libs/imgmgr/include/imgmgr/imgmgr.h | 11 +-
libs/imgmgr/pkg.yml | 4 +
libs/imgmgr/src/imgmgr.c | 19 +++-
libs/imgmgr/src/imgmgr_coredump.c | 178 +++++++++++++++++++++++++++++++
libs/imgmgr/src/imgmgr_priv.h | 5 +-
5 files changed, 213 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dfbac675/libs/imgmgr/include/imgmgr/imgmgr.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/include/imgmgr/imgmgr.h b/libs/imgmgr/include/imgmgr/imgmgr.h
index 511f1e0..e5e3596 100644
--- a/libs/imgmgr/include/imgmgr/imgmgr.h
+++ b/libs/imgmgr/include/imgmgr/imgmgr.h
@@ -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,
@@ -26,11 +26,15 @@
#define IMGMGR_NMGR_OP_FILE 3
#define IMGMGR_NMGR_OP_LIST2 4
#define IMGMGR_NMGR_OP_BOOT2 5
+#define IMGMGR_NMGR_OP_CORELIST 6
+#define IMGMGR_NMGR_OP_CORELOAD 7
#define IMGMGR_NMGR_MAX_MSG 120
#define IMGMGR_NMGR_MAX_NAME 64
#define IMGMGR_NMGR_MAX_VER 25 /* 255.255.65535.4294967295\0 */
+#define IMGMGR_HASH_LEN 32
+
int imgmgr_module_init(void);
struct image_version;
@@ -45,4 +49,9 @@ int imgr_ver_parse(char *src, struct image_version *ver);
*/
int imgr_ver_str(struct image_version *ver, char *dst);
+/*
+ * Given flash_map slot id, read in image_version and/or image hash.
+ */
+int imgr_read_info(int area_id, struct image_version *ver, uint8_t *hash);
+
#endif /* _IMGMGR_H */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dfbac675/libs/imgmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/imgmgr/pkg.yml b/libs/imgmgr/pkg.yml
index 93b8b00..ed02f32 100644
--- a/libs/imgmgr/pkg.yml
+++ b/libs/imgmgr/pkg.yml
@@ -30,3 +30,7 @@ pkg.deps:
pkg.deps.FS:
- fs/fs
pkg.cflags.FS: -DFS_PRESENT
+
+pkg.deps.COREDUMP:
+ - sys/coredump
+pkg.cflags.COREDUMP: -DCOREDUMP_PRESENT
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dfbac675/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index e12fc73..b730d66 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -66,8 +66,25 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
[IMGMGR_NMGR_OP_BOOT2] = {
.nh_read = imgr_boot2_read,
.nh_write = imgr_boot2_write
+ },
+ [IMGMGR_NMGR_OP_CORELIST] = {
+#ifdef COREDUMP_PRESENT
+ .nh_read = imgr_core_list,
+ .nh_write = imgr_noop,
+#else
+ .nh_read = imgr_noop,
+ .nh_write = imgr_noop
+#endif
+ },
+ [IMGMGR_NMGR_OP_CORELOAD] = {
+#ifdef COREDUMP_PRESENT
+ .nh_read = imgr_core_load,
+ .nh_write = imgr_core_erase,
+#else
+ .nh_read = imgr_noop,
+ .nh_write = imgr_noop
+#endif
}
-
};
static struct nmgr_group imgr_nmgr_group = {
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dfbac675/libs/imgmgr/src/imgmgr_coredump.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_coredump.c b/libs/imgmgr/src/imgmgr_coredump.c
new file mode 100644
index 0000000..6a09a5a
--- /dev/null
+++ b/libs/imgmgr/src/imgmgr_coredump.c
@@ -0,0 +1,178 @@
+/**
+ * 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.
+ */
+#ifdef COREDUMP_PRESENT
+
+#include <limits.h>
+
+#include <hal/flash_map.h>
+#include <newtmgr/newtmgr.h>
+#include <coredump/coredump.h>
+#include <util/base64.h>
+
+#include "imgmgr/imgmgr.h"
+#include "imgmgr_priv.h"
+
+int
+imgr_core_list(struct nmgr_jbuf *njb)
+{
+ const struct flash_area *fa;
+ struct coredump_header hdr;
+ struct json_encoder *enc;
+ struct json_value jv;
+ int rc;
+
+ rc = flash_area_open(FLASH_AREA_CORE, &fa);
+ if (rc) {
+ rc = NMGR_ERR_EINVAL;
+ } else {
+ rc = flash_area_read(fa, 0, &hdr, sizeof(hdr));
+ if (rc != 0) {
+ rc = NMGR_ERR_EINVAL;
+ } else if (hdr.ch_magic != COREDUMP_MAGIC) {
+ rc = NMGR_ERR_ENOENT;
+ } else {
+ rc = 0;
+ }
+ }
+
+ enc = &njb->njb_enc;
+
+ json_encode_object_start(enc);
+ JSON_VALUE_INT(&jv, rc);
+ json_encode_object_entry(enc, "rc", &jv);
+ json_encode_object_finish(enc);
+
+ return 0;
+}
+
+int
+imgr_core_load(struct nmgr_jbuf *njb)
+{
+ unsigned int off = UINT_MAX;
+ const struct json_attr_t dload_attr[2] = {
+ [0] = {
+ .attribute = "off",
+ .type = t_uinteger,
+ .addr.uinteger = &off
+ }
+ };
+ int rc;
+ int sz;
+ const struct flash_area *fa;
+ char data[IMGMGR_NMGR_MAX_MSG];
+ char encoded[BASE64_ENCODE_SIZE(IMGMGR_NMGR_MAX_MSG)];
+ struct coredump_header *hdr;
+ struct json_encoder *enc;
+ struct json_value jv;
+
+ hdr = (struct coredump_header *)data;
+
+ rc = json_read_object(&njb->njb_buf, dload_attr);
+ if (rc || off == UINT_MAX) {
+ rc = NMGR_ERR_EINVAL;
+ goto err;
+ }
+
+ rc = flash_area_open(FLASH_AREA_CORE, &fa);
+ if (rc) {
+ rc = NMGR_ERR_EINVAL;
+ goto err;
+ }
+
+ rc = flash_area_read(fa, 0, hdr, sizeof(*hdr));
+ if (rc) {
+ rc = NMGR_ERR_EINVAL;
+ goto err_close;
+ }
+ if (hdr->ch_magic != COREDUMP_MAGIC) {
+ rc = NMGR_ERR_ENOENT;
+ goto err_close;
+ }
+ if (off > hdr->ch_size) {
+ off = hdr->ch_size;
+ }
+ sz = hdr->ch_size - off;
+ if (sz > sizeof(data)) {
+ sz = sizeof(data);
+ }
+
+ rc = flash_area_read(fa, off, data, sz);
+ if (rc) {
+ rc = NMGR_ERR_EINVAL;
+ goto err_close;
+ }
+
+ sz = base64_encode(data, sz, encoded, 1);
+
+ enc = &njb->njb_enc;
+
+ json_encode_object_start(enc);
+ JSON_VALUE_INT(&jv, 0);
+ json_encode_object_entry(enc, "rc", &jv);
+
+ JSON_VALUE_INT(&jv, off);
+ json_encode_object_entry(enc, "off", &jv);
+
+ JSON_VALUE_STRINGN(&jv, encoded, sz);
+ json_encode_object_entry(enc, "data", &jv);
+ json_encode_object_finish(enc);
+
+ flash_area_close(fa);
+ return 0;
+
+err_close:
+ flash_area_close(fa);
+err:
+ nmgr_jbuf_setoerr(njb, rc);
+ return 0;
+}
+
+/*
+ * Erase the area if it has a coredump, or the header is empty.
+ */
+int
+imgr_core_erase(struct nmgr_jbuf *njb)
+{
+ struct coredump_header hdr;
+ const struct flash_area *fa;
+ int rc;
+
+ rc = flash_area_open(FLASH_AREA_CORE, &fa);
+ if (rc) {
+ rc = NMGR_ERR_EINVAL;
+ goto err;
+ }
+
+ rc = flash_area_read(fa, 0, &hdr, sizeof(hdr));
+ if (rc == 0 &&
+ (hdr.ch_magic == COREDUMP_MAGIC || hdr.ch_magic == 0xffffffff)) {
+ rc = flash_area_erase(fa, 0, fa->fa_size);
+ if (rc) {
+ rc = NMGR_ERR_EINVAL;
+ }
+ }
+ rc = 0;
+
+ flash_area_close(fa);
+err:
+ nmgr_jbuf_setoerr(njb, rc);
+ return 0;
+}
+
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dfbac675/libs/imgmgr/src/imgmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_priv.h b/libs/imgmgr/src/imgmgr_priv.h
index 04323af..e8dcde3 100644
--- a/libs/imgmgr/src/imgmgr_priv.h
+++ b/libs/imgmgr/src/imgmgr_priv.h
@@ -25,7 +25,6 @@
#define IMGMGR_MAX_IMGS 2
#define IMGMGR_HASH_STR 48
-#define IMGMGR_HASH_LEN 32
/*
* When accompanied by image, it's this structure followed by data.
@@ -102,8 +101,10 @@ int imgr_boot2_read(struct nmgr_jbuf *);
int imgr_boot2_write(struct nmgr_jbuf *);
int imgr_file_upload(struct nmgr_jbuf *);
int imgr_file_download(struct nmgr_jbuf *);
+int imgr_core_list(struct nmgr_jbuf *);
+int imgr_core_load(struct nmgr_jbuf *);
+int imgr_core_erase(struct nmgr_jbuf *);
-int imgr_read_info(int area_id, struct image_version *ver, uint8_t *hash);
int imgr_find_by_ver(struct image_version *find, uint8_t *hash);
int imgr_find_by_hash(uint8_t *find, struct image_version *ver);