-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zephyr: Allow user-defined boot serial extensions #1788
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2021-2023 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/drivers/flash.h> | ||
#include <zephyr/mgmt/mcumgr/mgmt/mgmt_defines.h> | ||
#include <zephyr/mgmt/mcumgr/grp/zephyr/zephyr_basic.h> | ||
#include <../subsys/mgmt/mcumgr/transport/include/mgmt/mcumgr/transport/smp_internal.h> | ||
|
||
#include <flash_map_backend/flash_map_backend.h> | ||
#include <sysflash/sysflash.h> | ||
|
||
#include "bootutil/bootutil_log.h" | ||
#include "../boot_serial/src/boot_serial_priv.h" | ||
#include <zcbor_encode.h> | ||
|
||
#include "bootutil/image.h" | ||
#include "bootutil/bootutil_public.h" | ||
#include "bootutil/boot_hooks.h" | ||
|
||
#include <boot_serial/boot_serial_extensions.h> | ||
|
||
BOOT_LOG_MODULE_DECLARE(mcuboot); | ||
|
||
#ifdef CONFIG_BOOT_MGMT_CUSTOM_STORAGE_ERASE | ||
static int bs_custom_storage_erase(const struct nmgr_hdr *hdr, | ||
const char *buffer, int len, | ||
zcbor_state_t *cs) | ||
{ | ||
int rc; | ||
const struct flash_area *fa; | ||
|
||
(void)buffer; | ||
(void)len; | ||
|
||
if (hdr->nh_group != ZEPHYR_MGMT_GRP_BASIC || hdr->nh_op != NMGR_OP_WRITE || | ||
hdr->nh_id != ZEPHYR_MGMT_GRP_BASIC_CMD_ERASE_STORAGE) { | ||
return MGMT_ERR_ENOTSUP; | ||
} | ||
|
||
rc = flash_area_open(FIXED_PARTITION_ID(storage_partition), &fa); | ||
|
||
if (rc < 0) { | ||
BOOT_LOG_ERR("failed to open flash area"); | ||
} else { | ||
rc = flash_area_erase(fa, 0, flash_area_get_size(fa)); | ||
if (rc < 0) { | ||
BOOT_LOG_ERR("failed to erase flash area"); | ||
} | ||
flash_area_close(fa); | ||
} | ||
if (rc == 0) { | ||
rc = MGMT_ERR_OK; | ||
} else { | ||
rc = MGMT_ERR_EUNKNOWN; | ||
} | ||
|
||
zcbor_map_start_encode(cs, 10); | ||
zcbor_tstr_put_lit(cs, "rc"); | ||
zcbor_uint32_put(cs, rc); | ||
zcbor_map_end_encode(cs, 10); | ||
|
||
return rc; | ||
} | ||
|
||
MCUMGR_HANDLER_DEFINE(storage_erase, bs_custom_storage_erase); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2023 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/linker/iterable_sections.h> | ||
|
||
ITERABLE_SECTION_ROM(mcuboot_bs_custom_handlers, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2023 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef H_BOOT_SERIAL_EXTENTIONS_ | ||
#define H_BOOT_SERIAL_EXTENTIONS_ | ||
|
||
#include <zephyr/kernel.h> | ||
#include <zephyr/sys/util_macro.h> | ||
#include <zephyr/sys/iterable_sections.h> | ||
|
||
/** | ||
* Callback handler prototype for boot serial extensions. | ||
* | ||
* @param[in] hdr MCUmgr header | ||
* @param[in] buffer Buffer with first MCUmgr message | ||
* @param[in] len Length of data in buffer | ||
* @param[out] cs Response | ||
* | ||
* @return MGMT_ERR_ENOTSUP to run other handlers, other MGMT_ERR_* value | ||
* when expected handler has ran. | ||
*/ | ||
typedef int (*bs_custom_handler_cb)(const struct nmgr_hdr *hdr, | ||
const char *buffer, int len, | ||
zcbor_state_t *cs); | ||
|
||
struct mcuboot_bs_custom_handlers { | ||
const bs_custom_handler_cb handler; | ||
}; | ||
|
||
/* Used to create an iterable section containing a boot serial handler | ||
* function | ||
*/ | ||
#define MCUMGR_HANDLER_DEFINE(name, _handler) \ | ||
STRUCT_SECTION_ITERABLE(mcuboot_bs_custom_handlers, name) = { \ | ||
.handler = _handler, \ | ||
} | ||
|
||
#endif /* H_BOOT_SERIAL_EXTENTIONS_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
- Reworked boot serial extensions so that they can be used by modules | ||
or from user repositories by switching to iterable sections. | ||
- Removed Zephyr custom img list boot serial extension support. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this mean that there might be more than one handler called for the same buffer and mutiple responses being sent for one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it would be a 1:1 mapping, if there are 3 handlers registered, the first one will be called, if it is not valid for that then it must return MGMT_ERR_ENOTSUP, it will then check the next handler, if the next handler is valid, it will return something else (i.e. usually EOK but could return EINVAL or anything) and then the loop here will be broken out, the final handler would not be called