This is an automated email from the ASF dual-hosted git repository. sjanc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 2bdb248433d6d1a7ccc802f8a5f33518fc3f3e0b Author: Szymon Janc <[email protected]> AuthorDate: Tue Oct 6 19:14:02 2026 +0200 mgmt/mcumgr/img_mgmt: Remove OS abstraction layer mcumgr is now part of Mynewt core and there is no need for OS abstraction layer. Mynewt implementation is moved from port package to img_mgmt sources (img_mgmt_flash.c and img_mgmt_log.c) and img_mgmt_impl_* functions are renamed to img_mgmt_* and declared in private header. img_mgmt_config.h is removed and syscfg values are used directly. Mynewt port package is removed. Settings, logs and package init are moved from port package to img_mgmt package. No functional changes. Assisted-by: Claude:claude-opus-5-5 --- .../cmd/img_mgmt/include/img_mgmt/img_mgmt.h | 14 +- .../img_mgmt/include/img_mgmt/img_mgmt_config.h | 66 -------- .../cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h | 174 --------------------- mgmt/mcumgr/cmd/img_mgmt/pkg.yml | 5 +- mgmt/mcumgr/cmd/img_mgmt/port/mynewt/pkg.yml | 32 ---- mgmt/mcumgr/cmd/img_mgmt/port/mynewt/syscfg.yml | 33 ---- mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt.c | 37 ++--- .../src/mynewt_img_mgmt.c => src/img_mgmt_flash.c} | 30 ++-- .../mynewt_img_mgmt_log.c => src/img_mgmt_log.c} | 8 +- mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_priv.h | 126 +++++++++++++++ mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c | 29 ++-- mgmt/mcumgr/cmd/img_mgmt/syscfg.yml | 15 ++ 12 files changed, 207 insertions(+), 362 deletions(-) diff --git a/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt.h b/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt.h index 26c44801f..45f155726 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt.h +++ b/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt.h @@ -21,7 +21,7 @@ #define H_IMG_MGMT_ #include <inttypes.h> -#include "img_mgmt_config.h" +#include "syscfg/syscfg.h" #include "mgmt/mgmt.h" struct image_version; @@ -30,6 +30,12 @@ struct image_version; extern "C" { #endif +/* Number of updatable images */ +#define IMG_MGMT_UPDATABLE_IMAGE_NUMBER 1 + +/* Slot of currently running image */ +#define IMG_MGMT_BOOT_CURR_SLOT 0 + #define IMG_MGMT_HASH_STR 48 #define IMG_MGMT_HASH_LEN 32 #define IMG_MGMT_DATA_SHA_LEN 32 /* SHA256 */ @@ -80,7 +86,7 @@ struct img_mgmt_upload_req { unsigned long long int size; /* -1 if unspecified */ size_t data_len; size_t data_sha_len; - uint8_t img_data[IMG_MGMT_UL_CHUNK_SIZE]; + uint8_t img_data[MYNEWT_VAL(IMG_MGMT_UL_CHUNK_SIZE)]; uint8_t data_sha[IMG_MGMT_DATA_SHA_LEN]; bool upgrade; /* Only allow greater version numbers. */ }; @@ -96,7 +102,7 @@ struct img_mgmt_state { /** Hash of image data; used for resumption of a partial upload. */ uint8_t data_sha_len; uint8_t data_sha[IMG_MGMT_DATA_SHA_LEN]; -#if IMG_MGMT_LAZY_ERASE +#if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE) int sector_id; uint32_t sector_end; #endif @@ -256,7 +262,7 @@ void img_mgmt_dfu_started(void); void img_mgmt_dfu_pending(void); void img_mgmt_dfu_confirmed(void); -#if IMG_MGMT_VERBOSE_ERR +#if MYNEWT_VAL(IMG_MGMT_VERBOSE_ERR) int img_mgmt_error_rsp(struct mgmt_ctxt *ctxt, int rc, const char *rsn); extern const char *img_mgmt_err_str_app_reject; extern const char *img_mgmt_err_str_hdr_malformed; diff --git a/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt_config.h b/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt_config.h deleted file mode 100644 index abbee3635..000000000 --- a/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt_config.h +++ /dev/null @@ -1,66 +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_IMG_MGMT_CONFIG_ -#define H_IMG_MGMT_CONFIG_ - -/* Number of updatable images */ -#define IMG_MGMT_UPDATABLE_IMAGE_NUMBER 1 - -#if defined MYNEWT - -#include "syscfg/syscfg.h" - -#define IMG_MGMT_UL_CHUNK_SIZE MYNEWT_VAL(IMG_MGMT_UL_CHUNK_SIZE) -#define IMG_MGMT_VERBOSE_ERR MYNEWT_VAL(IMG_MGMT_VERBOSE_ERR) -#define IMG_MGMT_LAZY_ERASE MYNEWT_VAL(IMG_MGMT_LAZY_ERASE) -#define IMG_MGMT_DUMMY_HDR MYNEWT_VAL(IMG_MGMT_DUMMY_HDR) -#ifdef MYNEWT_VAL_IMG_MGMT_FRUGAL_LIST -#undef IMG_MGMT_FRUGAL_LIST -#define IMG_MGMT_FRUGAL_LIST MYNEWT_VAL(IMG_MGMT_FRUGAL_LIST) -#endif -#define IMG_MGMT_BOOT_CURR_SLOT 0 - -#elif defined __ZEPHYR__ - -#define IMG_MGMT_UL_CHUNK_SIZE CONFIG_IMG_MGMT_UL_CHUNK_SIZE -#define IMG_MGMT_VERBOSE_ERR CONFIG_IMG_MGMT_VERBOSE_ERR -#define IMG_MGMT_LAZY_ERASE CONFIG_IMG_ERASE_PROGRESSIVELY -#define IMG_MGMT_DUMMY_HDR CONFIG_IMG_MGMT_DUMMY_HDR -#define IMG_MGMT_BOOT_CURR_SLOT 0 -#ifdef CONFIG_IMG_MGMT_UPDATABLE_IMAGE_NUMBER -/* Up to two images are supported */ -#undef IMG_MGMT_UPDATABLE_IMAGE_NUMBER -#define IMG_MGMT_UPDATABLE_IMAGE_NUMBER CONFIG_IMG_MGMT_UPDATABLE_IMAGE_NUMBER -#endif -#ifdef CONFIG_IMG_MGMT_FRUGAL_LIST -#undef IMG_MGMT_FRUGAL_LIST -#define IMG_MGMT_FRUGAL_LIST CONFIG_IMG_MGMT_FRUGAL_LIST -#endif - -#else - -/* No direct support for this OS. The application needs to define the above - * settings itself. - */ -#error "Unknown OS/missing configuration" - -#endif - -#endif diff --git a/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h b/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h deleted file mode 100644 index eb0445c34..000000000 --- a/mgmt/mcumgr/cmd/img_mgmt/include/img_mgmt/img_mgmt_impl.h +++ /dev/null @@ -1,174 +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. - */ - -/** - * @file - * @brief Declares implementation-specific functions required by image - * management. The default stubs can be overridden with functions that - * are compatible with the host OS. - */ - -#ifndef H_IMG_MGMT_IMPL_ -#define H_IMG_MGMT_IMPL_ - -#include <stdbool.h> -#include <inttypes.h> -#include "img_mgmt/img_mgmt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Ensures the spare slot (slot 1) is fully erased. - * - * @return 0 on success, MGMT_ERR_[...] code on failure. - */ -int img_mgmt_impl_erase_slot(void); - -/** - * @brief Marks the image in the specified slot as pending. On the next reboot, - * the system will perform a boot of the specified image. - * - * @param slot The slot to mark as pending. In the typical - * use case, this is 1. - * @param permanent Whether the image should be used permanently or - * only tested once: - * 0=run image once, then confirm or - * revert. - * 1=run image forever. - * - * @return 0 on success, MGMT_ERR_[...] code on failure. - */ -int img_mgmt_impl_write_pending(int slot, bool permanent); - -/** - * @brief Marks the image in slot 0 as confirmed. The system will continue - * booting into the image in slot 0 until told to boot from a different slot. - * - * @return 0 on success, MGMT_ERR_[...] code on failure. - */ -int img_mgmt_impl_write_confirmed(void); - -/** - * @brief Reads the specified chunk of data from an image slot. - * - * @param slot The index of the slot to read from. - * @param offset The offset within the slot to read from. - * @param dst On success, the read data gets written here. - * @param num_bytes The number of byets to read. - * - * @return 0 on success, MGMT_ERR_[...] code on failure. - */ -int img_mgmt_impl_read(int slot, unsigned int offset, void *dst, - unsigned int num_bytes); - -/** - * @brief Writes the specified chunk of image data to slot 1. - * - * @param offset The offset within slot 1 to write to. - * @param data The image data to write. - * @param num_bytes The number of bytes to read. - * @param last Whether this chunk is the end of the image: - * false=additional image chunks are - * forthcoming. - * true=last image chunk; flush unwritten data - * to disk. - * - * @return 0 on success, MGMT_ERR_[...] code on failure. - */ -int img_mgmt_impl_write_image_data(unsigned int offset, const void *data, - unsigned int num_bytes, bool last); - -/** - * @brief Indicates the type of swap operation that will occur on the next - * reboot, if any, between provided slot and it's pair. - * Quering any slots of the same pair will give the same result. - * - * @param image An slot number; - * @return An IMG_MGMT_SWAP_TYPE_[...] code. - */ -int img_mgmt_impl_swap_type(int slot); - -/** - * Collects information about the specified image slot. - * - * @return Flags of the specified image slot - */ -uint8_t img_mgmt_state_flags(int query_slot); - -/** - * Erases image data at given offset - * - * @param offset The offset within slot 1 to erase at. - * @param num_bytes The number of bytes to erase. - * @return 0 on success, MGMT_ERR_[...] code on failure. - */ -int img_mgmt_impl_erase_image_data(unsigned int off, unsigned int num_bytes); - -/** - * Erases a flash sector as image upload crosses a sector boundary. - * Erasing the entire flash size at one time can take significant time, - * causing a bluetooth disconnect or significant battery sag. - * Instead we will erase immediately prior to crossing a sector. - * We could check for empty to increase efficiency, but instead we always erase - * for consistency and simplicity. - * - * @param off Offset that is about to be written - * @param len Number of bytes to be written - * - * @return 0 if success - * ERROR_CODE if could not erase sector - */ -int img_mgmt_impl_erase_if_needed(uint32_t off, uint32_t len); - -/** - * Verifies an upload request and indicates the actions that should be taken - * during processing of the request. This is a "read only" function in the - * sense that it doesn't write anything to flash and doesn't modify any global - * variables. - * - * @param req The upload request to inspect. - * @param action On success, gets populated with information - * about how to process the request. - * - * @return 0 if processing should occur; - * A MGMT_ERR code if an error response should be - * sent instead. - */ -int img_mgmt_impl_upload_inspect(const struct img_mgmt_upload_req *req, - struct img_mgmt_upload_action *action, - const char **errstr); - -#define ERASED_VAL_32(x) (((x) << 24) | ((x) << 16) | ((x) << 8) | (x)) -int img_mgmt_impl_erased_val(int slot, uint8_t *erased_val); - -int img_mgmt_impl_log_upload_start(int status); - -int img_mgmt_impl_log_upload_done(int status, const uint8_t *hashp); - -int img_mgmt_impl_log_pending(int status, const uint8_t *hash); - -int img_mgmt_impl_log_confirm(int status, const uint8_t *hash); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/mgmt/mcumgr/cmd/img_mgmt/pkg.yml b/mgmt/mcumgr/cmd/img_mgmt/pkg.yml index 608a0613d..bc21999ca 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/pkg.yml +++ b/mgmt/mcumgr/cmd/img_mgmt/pkg.yml @@ -24,10 +24,13 @@ pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: pkg.deps: - - '@apache-mynewt-core/mgmt/mcumgr/cmd/img_mgmt/port/mynewt' - '@apache-mynewt-core/boot/split' - '@apache-mynewt-core/encoding/base64' - '@apache-mynewt-core/sys/flash_map' - '@apache-mynewt-core/mgmt/mcumgr/mgmt' - '@apache-mynewt-core/encoding/cborattr' - '@apache-mynewt-core/mgmt/mcumgr/util' + - '@apache-mynewt-core/sys/log/modlog' + +pkg.init: + img_mgmt_module_init: 'MYNEWT_VAL(IMG_MGMT_SYSINIT_STAGE)' diff --git a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/pkg.yml b/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/pkg.yml deleted file mode 100644 index 33073de67..000000000 --- a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/pkg.yml +++ /dev/null @@ -1,32 +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. -# - -pkg.name: mgmt/mcumgr/cmd/img_mgmt/port/mynewt -pkg.description: 'Image management command handlers for - Apache Mynewt for mcumgr.' -pkg.author: "Apache Mynewt <[email protected]>" -pkg.homepage: "http://mynewt.apache.org/" -pkg.keywords: - -pkg.deps: - - '@apache-mynewt-core/mgmt/mcumgr/cmd/img_mgmt' - - '@apache-mynewt-core/sys/log/modlog' - -pkg.init: - img_mgmt_module_init: 'MYNEWT_VAL(IMG_MGMT_SYSINIT_STAGE)' diff --git a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/syscfg.yml b/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/syscfg.yml deleted file mode 100644 index 35c68ced7..000000000 --- a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/syscfg.yml +++ /dev/null @@ -1,33 +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. -# - -syscfg.defs: - IMG_MGMT_LOG_MOD: - description: 'Module ID for the image management structured log.' - value: 176 - IMG_MGMT_LOG_LVL: - description: 'Minimum level for the image management structured log.' - value: 15 # Log disabled by default. - IMG_MGMT_SYSINIT_STAGE: - description: System initalization stage for IMG_MGMT package - value: 501 - -syscfg.logs: - IMG_MGMT_LOG: - module: MYNEWT_VAL(IMG_MGMT_LOG_MOD) - level: MYNEWT_VAL(IMG_MGMT_LOG_LVL) diff --git a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt.c b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt.c index fbda7510d..1d611e5cb 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt.c +++ b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt.c @@ -26,9 +26,7 @@ #include "img_mgmt/image.h" #include "img_mgmt/img_mgmt.h" -#include "img_mgmt/img_mgmt_impl.h" #include "img_mgmt_priv.h" -#include "img_mgmt/img_mgmt_config.h" static mgmt_handler_fn img_mgmt_upload; static mgmt_handler_fn img_mgmt_erase; @@ -63,7 +61,7 @@ static struct mgmt_group img_mgmt_group = { .mg_group_id = MGMT_GROUP_ID_IMAGE, }; -#if IMG_MGMT_VERBOSE_ERR +#if MYNEWT_VAL(IMG_MGMT_VERBOSE_ERR) const char *img_mgmt_err_str_app_reject = "app reject"; const char *img_mgmt_err_str_hdr_malformed = "header malformed"; const char *img_mgmt_err_str_magic_mismatch = "magic mismatch"; @@ -84,7 +82,7 @@ img_mgmt_find_tlvs(int slot, size_t *start_off, size_t *end_off, uint16_t magic) struct image_tlv_info tlv_info; int rc; - rc = img_mgmt_impl_read(slot, *start_off, &tlv_info, sizeof tlv_info); + rc = img_mgmt_read(slot, *start_off, &tlv_info, sizeof tlv_info); if (rc != 0) { /* Read error. */ return MGMT_ERR_EUNKNOWN; @@ -112,7 +110,7 @@ img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash, uint32_t *flags) { -#if IMG_MGMT_DUMMY_HDR +#if MYNEWT_VAL(IMG_MGMT_DUMMY_HDR) uint8_t dummy_hash[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, @@ -146,12 +144,12 @@ img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash, uint32_t erased_val_32; int rc; - rc = img_mgmt_impl_erased_val(image_slot, &erased_val); + rc = img_mgmt_erased_val(image_slot, &erased_val); if (rc != 0) { return MGMT_ERR_EUNKNOWN; } - rc = img_mgmt_impl_read(image_slot, 0, &hdr, sizeof hdr); + rc = img_mgmt_read(image_slot, 0, &hdr, sizeof hdr); if (rc != 0) { return MGMT_ERR_EUNKNOWN; } @@ -199,7 +197,7 @@ img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash, hash_found = false; while (data_off + sizeof tlv <= data_end) { - rc = img_mgmt_impl_read(image_slot, data_off, &tlv, sizeof tlv); + rc = img_mgmt_read(image_slot, data_off, &tlv, sizeof tlv); if (rc != 0) { return MGMT_ERR_EUNKNOWN; } @@ -223,7 +221,7 @@ img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash, if (data_off + IMAGE_HASH_LEN > data_end) { return MGMT_ERR_EUNKNOWN; } - rc = img_mgmt_impl_read(image_slot, data_off, hash, IMAGE_HASH_LEN); + rc = img_mgmt_read(image_slot, data_off, hash, IMAGE_HASH_LEN); if (rc != 0) { return MGMT_ERR_EUNKNOWN; } @@ -279,7 +277,7 @@ img_mgmt_find_by_hash(uint8_t *find, struct image_version *ver) return -1; } -#if IMG_MGMT_VERBOSE_ERR +#if MYNEWT_VAL(IMG_MGMT_VERBOSE_ERR) int img_mgmt_error_rsp(struct mgmt_ctxt *ctxt, int rc, const char *rsn) { @@ -318,7 +316,7 @@ img_mgmt_erase(struct mgmt_ctxt *ctxt) } } - rc = img_mgmt_impl_erase_slot(); + rc = img_mgmt_erase_slot(); if (!rc) { img_mgmt_dfu_stopped(); @@ -372,7 +370,7 @@ img_mgmt_upload_log(bool is_first, bool is_last, int status) int rc; if (is_first) { - return img_mgmt_impl_log_upload_start(status); + return img_mgmt_log_upload_start(status); } if (is_last || status != 0) { @@ -384,7 +382,7 @@ img_mgmt_upload_log(bool is_first, bool is_last, int status) hashp = hash; } - return img_mgmt_impl_log_upload_done(status, hashp); + return img_mgmt_log_upload_done(status, hashp); } /* Nothing to log. */ @@ -461,7 +459,7 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt) } /* Determine what actions to take as a result of this request. */ - rc = img_mgmt_impl_upload_inspect(&req, &action, &errstr); + rc = img_mgmt_upload_inspect(&req, &action, &errstr); if (rc != 0) { img_mgmt_dfu_stopped(); return rc; @@ -508,14 +506,14 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt) memset(&g_img_mgmt_state.data_sha[req.data_sha_len], 0, IMG_MGMT_DATA_SHA_LEN - req.data_sha_len); -#if IMG_MGMT_LAZY_ERASE +#if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE) /* setup for lazy sector by sector erase */ g_img_mgmt_state.sector_id = -1; g_img_mgmt_state.sector_end = 0; #else /* erase the entire req.size all at once */ if (action.erase) { - rc = img_mgmt_impl_erase_image_data(0, req.size); + rc = img_mgmt_erase_image_data(0, req.size); if (rc != 0) { rc = MGMT_ERR_EUNKNOWN; errstr = img_mgmt_err_str_flash_erase_failed; @@ -529,9 +527,9 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt) /* Write the image data to flash. */ if (req.data_len != 0) { -#if IMG_MGMT_LAZY_ERASE +#if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE) /* erase as we cross sector boundaries */ - if (img_mgmt_impl_erase_if_needed(req.off, action.write_bytes) != 0) { + if (img_mgmt_erase_if_needed(req.off, action.write_bytes) != 0) { rc = MGMT_ERR_EUNKNOWN; errstr = img_mgmt_err_str_flash_erase_failed; goto end; @@ -542,8 +540,7 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt) last = true; } - rc = img_mgmt_impl_write_image_data(req.off, req.img_data, - action.write_bytes, last); + rc = img_mgmt_write_image_data(req.off, req.img_data, action.write_bytes, last); if (rc != 0) { rc = MGMT_ERR_EUNKNOWN; errstr = img_mgmt_err_str_flash_write_failed; diff --git a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_flash.c similarity index 94% rename from mgmt/mcumgr/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c rename to mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_flash.c index 1fda6a670..2706a38fd 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt.c +++ b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_flash.c @@ -19,7 +19,7 @@ #include "sysinit/sysinit.h" #include "mgmt/mgmt.h" -#include "img_mgmt/img_mgmt_impl.h" +#include "img_mgmt_priv.h" #include "img_mgmt/img_mgmt.h" #include "split/split.h" #include "flash_map/flash_map.h" @@ -113,8 +113,8 @@ img_mgmt_vercmp(const struct image_version *a, const struct image_version *b) * sent instead. */ int -img_mgmt_impl_upload_inspect(const struct img_mgmt_upload_req *req, - struct img_mgmt_upload_action *action, const char **errstr) +img_mgmt_upload_inspect(const struct img_mgmt_upload_req *req, + struct img_mgmt_upload_action *action, const char **errstr) { const struct image_header *hdr; const struct flash_area *fa; @@ -271,7 +271,7 @@ img_mgmt_impl_upload_inspect(const struct img_mgmt_upload_req *req, } int -img_mgmt_impl_erase_slot(void) +img_mgmt_erase_slot(void) { #ifndef FLASH_AREA_IMAGE_1 return MGMT_ERR_ENOTSUP; @@ -302,7 +302,7 @@ img_mgmt_impl_erase_slot(void) } int -img_mgmt_impl_write_pending(int slot, bool permanent) +img_mgmt_write_pending(int slot, bool permanent) { uint32_t image_flags; uint8_t state_flags; @@ -360,7 +360,7 @@ img_mgmt_impl_write_pending(int slot, bool permanent) } int -img_mgmt_impl_write_confirmed(void) +img_mgmt_write_confirmed(void) { int rc; @@ -387,7 +387,7 @@ img_mgmt_impl_write_confirmed(void) } int -img_mgmt_impl_read(int slot, unsigned int offset, void *dst, unsigned int num_bytes) +img_mgmt_read(int slot, unsigned int offset, void *dst, unsigned int num_bytes) { const struct flash_area *fa; int area_id; @@ -410,8 +410,8 @@ img_mgmt_impl_read(int slot, unsigned int offset, void *dst, unsigned int num_by #if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE) int -img_mgmt_impl_write_image_data(unsigned int offset, const void *data, - unsigned int num_bytes, bool last) +img_mgmt_write_image_data(unsigned int offset, const void *data, + unsigned int num_bytes, bool last) { #ifndef FLASH_AREA_IMAGE_1 return MGMT_ERR_ENOTSUP; @@ -460,8 +460,8 @@ err: #else int -img_mgmt_impl_write_image_data(unsigned int offset, const void *data, - unsigned int num_bytes, bool last) +img_mgmt_write_image_data(unsigned int offset, const void *data, + unsigned int num_bytes, bool last) { #ifndef FLASH_AREA_IMAGE_1 return MGMT_ERR_ENOTSUP; @@ -486,7 +486,7 @@ img_mgmt_impl_write_image_data(unsigned int offset, const void *data, #endif int -img_mgmt_impl_erase_image_data(unsigned int off, unsigned int num_bytes) +img_mgmt_erase_image_data(unsigned int off, unsigned int num_bytes) { #ifndef FLASH_AREA_IMAGE_1 return MGMT_ERR_ENOTSUP; @@ -511,7 +511,7 @@ img_mgmt_impl_erase_image_data(unsigned int off, unsigned int num_bytes) #if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE) int -img_mgmt_impl_erase_if_needed(uint32_t off, uint32_t len) +img_mgmt_erase_if_needed(uint32_t off, uint32_t len) { #ifndef FLASH_AREA_IMAGE_1 return MGMT_ERR_ENOTSUP; @@ -546,7 +546,7 @@ done: #endif int -img_mgmt_impl_swap_type(int slot) +img_mgmt_swap_type(int slot) { assert(slot == 0 || slot == 1); @@ -566,7 +566,7 @@ img_mgmt_impl_swap_type(int slot) } int -img_mgmt_impl_erased_val(int slot, uint8_t *erased_val) +img_mgmt_erased_val(int slot, uint8_t *erased_val) { const struct flash_area *fa; int rc; diff --git a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt_log.c b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_log.c similarity index 94% rename from mgmt/mcumgr/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt_log.c rename to mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_log.c index 8ded48ffa..fce709193 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/port/mynewt/src/mynewt_img_mgmt_log.c +++ b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_log.c @@ -116,25 +116,25 @@ img_mgmt_log_gen(const char *ev, int status, const uint8_t *hash) } int -img_mgmt_impl_log_upload_start(int status) +img_mgmt_log_upload_start(int status) { return img_mgmt_log_gen(IMG_MGMT_LOG_EV_UPSTART, status, NULL); } int -img_mgmt_impl_log_upload_done(int status, const uint8_t *hash) +img_mgmt_log_upload_done(int status, const uint8_t *hash) { return img_mgmt_log_gen(IMG_MGMT_LOG_EV_UPDONE, 0, hash); } int -img_mgmt_impl_log_pending(int status, const uint8_t *hash) +img_mgmt_log_pending(int status, const uint8_t *hash) { return img_mgmt_log_gen(IMG_MGMT_LOG_EV_PEND, status, hash); } int -img_mgmt_impl_log_confirm(int status, const uint8_t *hash) +img_mgmt_log_confirm(int status, const uint8_t *hash) { return img_mgmt_log_gen(IMG_MGMT_LOG_EV_CONF, status, hash); } diff --git a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_priv.h b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_priv.h index 363984f71..65b9a4ec5 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_priv.h +++ b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_priv.h @@ -20,7 +20,9 @@ #ifndef H_IMG_PRIV_ #define H_IMG_PRIV_ +#include <stdbool.h> #include <stdint.h> +#include "img_mgmt/img_mgmt.h" #ifdef __cplusplus extern "C" { @@ -81,6 +83,130 @@ int img_mgmt_state_read(struct mgmt_ctxt *ctxt); int img_mgmt_state_write(struct mgmt_ctxt *njb); int img_mgmt_ver_str(const struct image_version *ver, char *dst); +/** + * @brief Ensures the spare slot (slot 1) is fully erased. + * + * @return 0 on success, MGMT_ERR_[...] code on failure. + */ +int img_mgmt_erase_slot(void); + +/** + * @brief Marks the image in the specified slot as pending. On the next reboot, + * the system will perform a boot of the specified image. + * + * @param slot The slot to mark as pending. In the typical + * use case, this is 1. + * @param permanent Whether the image should be used permanently or + * only tested once: + * 0=run image once, then confirm or + * revert. + * 1=run image forever. + * + * @return 0 on success, MGMT_ERR_[...] code on failure. + */ +int img_mgmt_write_pending(int slot, bool permanent); + +/** + * @brief Marks the image in slot 0 as confirmed. The system will continue + * booting into the image in slot 0 until told to boot from a different slot. + * + * @return 0 on success, MGMT_ERR_[...] code on failure. + */ +int img_mgmt_write_confirmed(void); + +/** + * @brief Reads the specified chunk of data from an image slot. + * + * @param slot The index of the slot to read from. + * @param offset The offset within the slot to read from. + * @param dst On success, the read data gets written here. + * @param num_bytes The number of byets to read. + * + * @return 0 on success, MGMT_ERR_[...] code on failure. + */ +int img_mgmt_read(int slot, unsigned int offset, void *dst, unsigned int num_bytes); + +/** + * @brief Writes the specified chunk of image data to slot 1. + * + * @param offset The offset within slot 1 to write to. + * @param data The image data to write. + * @param num_bytes The number of bytes to read. + * @param last Whether this chunk is the end of the image: + * false=additional image chunks are + * forthcoming. + * true=last image chunk; flush unwritten data + * to disk. + * + * @return 0 on success, MGMT_ERR_[...] code on failure. + */ +int img_mgmt_write_image_data(unsigned int offset, const void *data, + unsigned int num_bytes, bool last); + +/** + * @brief Indicates the type of swap operation that will occur on the next + * reboot, if any, between provided slot and it's pair. + * Quering any slots of the same pair will give the same result. + * + * @param image An slot number; + * @return An IMG_MGMT_SWAP_TYPE_[...] code. + */ +int img_mgmt_swap_type(int slot); + +/** + * Erases image data at given offset + * + * @param offset The offset within slot 1 to erase at. + * @param num_bytes The number of bytes to erase. + * @return 0 on success, MGMT_ERR_[...] code on failure. + */ +int img_mgmt_erase_image_data(unsigned int off, unsigned int num_bytes); + +/** + * Erases a flash sector as image upload crosses a sector boundary. + * Erasing the entire flash size at one time can take significant time, + * causing a bluetooth disconnect or significant battery sag. + * Instead we will erase immediately prior to crossing a sector. + * We could check for empty to increase efficiency, but instead we always erase + * for consistency and simplicity. + * + * @param off Offset that is about to be written + * @param len Number of bytes to be written + * + * @return 0 if success + * ERROR_CODE if could not erase sector + */ +int img_mgmt_erase_if_needed(uint32_t off, uint32_t len); + +/** + * Verifies an upload request and indicates the actions that should be taken + * during processing of the request. This is a "read only" function in the + * sense that it doesn't write anything to flash and doesn't modify any global + * variables. + * + * @param req The upload request to inspect. + * @param action On success, gets populated with information + * about how to process the request. + * + * @return 0 if processing should occur; + * A MGMT_ERR code if an error response should be + * sent instead. + */ +int img_mgmt_upload_inspect(const struct img_mgmt_upload_req *req, + struct img_mgmt_upload_action *action, + const char **errstr); + +#define ERASED_VAL_32(x) (((x) << 24) | ((x) << 16) | ((x) << 8) | (x)) +int img_mgmt_erased_val(int slot, uint8_t *erased_val); + +int img_mgmt_log_upload_start(int status); + +int img_mgmt_log_upload_done(int status, const uint8_t *hashp); + +int img_mgmt_log_pending(int status, const uint8_t *hash); + +int img_mgmt_log_confirm(int status, const uint8_t *hash); + #ifdef __cplusplus } #endif diff --git a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c index 1047b9507..18b6cb4f0 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c +++ b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c @@ -25,7 +25,6 @@ #include "img_mgmt/img_mgmt.h" #include "img_mgmt/image.h" #include "img_mgmt_priv.h" -#include "img_mgmt/img_mgmt_impl.h" /** * Collects information about the specified image slot. @@ -41,7 +40,7 @@ img_mgmt_state_flags(int query_slot) /* Determine if this is is pending or confirmed (only applicable for * unified images and loaders. */ - swap_type = img_mgmt_impl_swap_type(query_slot); + swap_type = img_mgmt_swap_type(query_slot); switch (swap_type) { case IMG_MGMT_SWAP_TYPE_NONE: if (query_slot == IMG_MGMT_BOOT_CURR_SLOT) { @@ -134,7 +133,7 @@ img_mgmt_state_set_pending(int slot, int permanent) goto done; } - rc = img_mgmt_impl_write_pending(slot, permanent); + rc = img_mgmt_write_pending(slot, permanent); if (rc != 0) { rc = MGMT_ERR_EUNKNOWN; } @@ -148,9 +147,9 @@ done: } if (permanent) { - (void)img_mgmt_impl_log_confirm(rc, hashp); + (void)img_mgmt_log_confirm(rc, hashp); } else { - (void)img_mgmt_impl_log_pending(rc, hashp); + (void)img_mgmt_log_pending(rc, hashp); } return rc; @@ -171,14 +170,14 @@ img_mgmt_state_confirm(void) goto err; } - rc = img_mgmt_impl_write_confirmed(); + rc = img_mgmt_write_confirmed(); if (rc != 0) { rc = MGMT_ERR_EUNKNOWN; } img_mgmt_dfu_confirmed(); err: - return img_mgmt_impl_log_confirm(rc, NULL); + return img_mgmt_log_confirm(rc, NULL); } /** @@ -227,27 +226,31 @@ img_mgmt_state_read(struct mgmt_ctxt *ctxt) err |= cbor_encode_text_stringz(&image, "hash"); err |= cbor_encode_byte_string(&image, hash, IMAGE_HASH_LEN); - if (!IMG_MGMT_FRUGAL_LIST || !(flags & IMAGE_F_NON_BOOTABLE)) { + if (!MYNEWT_VAL(IMG_MGMT_FRUGAL_LIST) || !(flags & IMAGE_F_NON_BOOTABLE)) { err |= cbor_encode_text_stringz(&image, "bootable"); err |= cbor_encode_boolean(&image, !(flags & IMAGE_F_NON_BOOTABLE)); } - if (!IMG_MGMT_FRUGAL_LIST || (state_flags & IMG_MGMT_STATE_F_PENDING)) { + if (!MYNEWT_VAL(IMG_MGMT_FRUGAL_LIST) || + (state_flags & IMG_MGMT_STATE_F_PENDING)) { err |= cbor_encode_text_stringz(&image, "pending"); err |= cbor_encode_boolean(&image, state_flags & IMG_MGMT_STATE_F_PENDING); } - if (!IMG_MGMT_FRUGAL_LIST || (state_flags & IMG_MGMT_STATE_F_CONFIRMED)) { + if (!MYNEWT_VAL(IMG_MGMT_FRUGAL_LIST) || + (state_flags & IMG_MGMT_STATE_F_CONFIRMED)) { err |= cbor_encode_text_stringz(&image, "confirmed"); err |= cbor_encode_boolean(&image, state_flags & IMG_MGMT_STATE_F_CONFIRMED); } - if (!IMG_MGMT_FRUGAL_LIST || (state_flags & IMG_MGMT_STATE_F_ACTIVE)) { + if (!MYNEWT_VAL(IMG_MGMT_FRUGAL_LIST) || + (state_flags & IMG_MGMT_STATE_F_ACTIVE)) { err |= cbor_encode_text_stringz(&image, "active"); err |= cbor_encode_boolean(&image, state_flags & IMG_MGMT_STATE_F_ACTIVE); } - if (!IMG_MGMT_FRUGAL_LIST || (state_flags & IMG_MGMT_STATE_F_PERMANENT)) { + if (!MYNEWT_VAL(IMG_MGMT_FRUGAL_LIST) || + (state_flags & IMG_MGMT_STATE_F_PERMANENT)) { err |= cbor_encode_text_stringz(&image, "permanent"); err |= cbor_encode_boolean(&image, state_flags & IMG_MGMT_STATE_F_PERMANENT); } @@ -258,7 +261,7 @@ img_mgmt_state_read(struct mgmt_ctxt *ctxt) err |= cbor_encoder_close_container(&ctxt->encoder, &images); /* splitStatus is always 0 so in frugal list it is not present at all */ - if (!IMG_MGMT_FRUGAL_LIST) { + if (!MYNEWT_VAL(IMG_MGMT_FRUGAL_LIST)) { err |= cbor_encode_text_stringz(&ctxt->encoder, "splitStatus"); err |= cbor_encode_int(&ctxt->encoder, 0); } diff --git a/mgmt/mcumgr/cmd/img_mgmt/syscfg.yml b/mgmt/mcumgr/cmd/img_mgmt/syscfg.yml index 56c56f445..0faaeec75 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/syscfg.yml +++ b/mgmt/mcumgr/cmd/img_mgmt/syscfg.yml @@ -18,6 +18,16 @@ # syscfg.defs: + IMG_MGMT_LOG_MOD: + description: 'Module ID for the image management structured log.' + value: 176 + IMG_MGMT_LOG_LVL: + description: 'Minimum level for the image management structured log.' + value: 15 # Log disabled by default. + IMG_MGMT_SYSINIT_STAGE: + description: System initalization stage for IMG_MGMT package + value: 501 + IMG_MGMT_UL_CHUNK_SIZE: description: > Limits the maximum chunk size in image uploads. A buffer of this @@ -61,3 +71,8 @@ syscfg.vals.IMGMGR_DUMMY_HDR: syscfg.vals.IMGMGR_FRUGAL_LIST: IMG_MGMT_FRUGAL_LIST: MYNEWT_VAL(IMGMGR_FRUGAL_LIST) + +syscfg.logs: + IMG_MGMT_LOG: + module: MYNEWT_VAL(IMG_MGMT_LOG_MOD) + level: MYNEWT_VAL(IMG_MGMT_LOG_LVL)
