-
Notifications
You must be signed in to change notification settings - Fork 685
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
[RFC] Support for choosing image to boot in runtime #2044
Open
edersondisouza
wants to merge
2
commits into
mcu-tools:main
Choose a base branch
from
edersondisouza:runtime-source
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+297
−24
Open
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
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,46 @@ | ||
/* | ||
* Copyright (c) 2024 Intel Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef __FLASH_RUNTIME_SOURCES_H__ | ||
#define __FLASH_RUNTIME_SOURCES_H__ | ||
|
||
#include <inttypes.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/* | ||
* Get next flash map id. | ||
* | ||
* Implement this function to get the next flash map id. The function should | ||
* return true if the flash map id was successfully updated. If the reset | ||
* parameter is true, the function should reset the flash map id to the first | ||
* one. | ||
* | ||
* @param id Pointer to the flash map id. | ||
* @param reset If true, the function will reset the flash map id to the first | ||
* one. | ||
* @retval true If the flash map id was successfully updated. | ||
*/ | ||
bool flash_map_id_get_next(uint8_t *id, bool reset); | ||
|
||
/* | ||
* Get current flash map id. | ||
* | ||
* Implement this function to get the current flash map id. The function should | ||
* return true if the flash map id was successfully read. | ||
* | ||
* @param id Pointer to the flash map id. | ||
* @retval true If the flash map id was successfully read. | ||
*/ | ||
bool flash_map_id_get_current(uint8_t *id); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* __FLASH_RUNTIME_SOURCES_H__ */ |
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,56 @@ | ||
# Runtime chosen image sample application | ||
|
||
This sample demonstrates how to use a non flash storage to retrieve the image | ||
being booted. It was tested on a FRDM K64F. Both slots are used to store two | ||
different images. The image to be booted is selected based on a button press. | ||
|
||
## Build | ||
|
||
Build mcuboot. First, ensure ZEPHYR_SDK_INSTALL_DIR is defined. From the | ||
mcuboot directory, run the following commands: | ||
|
||
``` | ||
source <path-to-zephyr>/zephyr-env.sh | ||
west build -p -b frdm_k64f boot/zephyr/ -- -DBUILD_RUNTIME_SOURCE_SAMPLE=1 \ | ||
-DEXTRA_CONF_FILE="../../samples/runtime-source/zephyr/sample.conf" | ||
-DEXTRA_DTC_OVERLAY_FILE=../../samples/runtime-source/zephyr/boards/frdm_k64f.overlay | ||
west build -t flash | ||
``` | ||
|
||
Then, build the sample application to be loaded. We need to build it twice, one | ||
for each slot. From the sample | ||
app directory (mcuboot/samples/non-flash-source/zephyr/app), run: | ||
|
||
``` | ||
west build -p -b frdm_k64f . | ||
west flash | ||
``` | ||
|
||
Then change the overlay file to use the second slot. For instance, open | ||
`boards/frdm_k64f.overlay` and change the line: | ||
|
||
``` | ||
zephyr,code-partition = &slot0_partition; | ||
``` | ||
|
||
to: | ||
|
||
``` | ||
zephyr,code-partition = &slot1_partition; | ||
``` | ||
|
||
And build and flash again: | ||
|
||
``` | ||
west build -b frdm_k64f . | ||
west flash | ||
``` | ||
|
||
## Run | ||
|
||
Open a serial terminal to see the output and reset the board. It shall boot the | ||
image on slot0 by default. By keeping the SW2 button pressed during reset, the | ||
bootloader will randomly select the image to be booted. |
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,14 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(non_flash_backend_app) | ||
|
||
if (NOT DEFINED FROM_WHO) | ||
set(FROM_WHO Zephyr) | ||
endif() | ||
|
||
target_compile_definitions(app PRIVATE "-DMCUBOOT_HELLO_WORLD_FROM=\"${FROM_WHO}\"") | ||
|
||
target_sources(app PRIVATE src/main.c) |
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,5 @@ | ||
/ { | ||
chosen { | ||
zephyr,code-partition = &slot0_partition; | ||
}; | ||
}; |
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 @@ | ||
CONFIG_BOOTLOADER_MCUBOOT=y | ||
|
||
CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="./bootloader/mcuboot/root-rsa-2048.pem" |
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,15 @@ | ||
/* | ||
* Copyright (c) 2017 Linaro, Ltd. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/kernel.h> | ||
#include <zephyr/sys/printk.h> | ||
|
||
int main(void) | ||
{ | ||
printk("Hello World from %s on %s, slot %s!\n", | ||
MCUBOOT_HELLO_WORLD_FROM, CONFIG_BOARD, | ||
DT_PROP(DT_CHOSEN(zephyr_code_partition), label)); | ||
} |
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,60 @@ | ||
/* | ||
* Copyright (c) 2024 Intel Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <flash_map_backend/flash_map_backend.h> | ||
#include <zephyr/drivers/gpio.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/random/random.h> | ||
|
||
#define SW1_NODE DT_ALIAS(sw1) | ||
#if DT_NODE_HAS_STATUS(SW1_NODE, okay) | ||
static struct gpio_dt_spec sw1_spec = GPIO_DT_SPEC_GET(SW1_NODE, gpios); | ||
#endif | ||
|
||
static int curr_idx = -1; | ||
|
||
static uint8_t known_ids[] = { | ||
FIXED_PARTITION_ID(slot0_partition), | ||
FIXED_PARTITION_ID(slot1_partition), | ||
}; | ||
|
||
bool | ||
flash_map_id_get_next(uint8_t *id, bool reset) | ||
{ | ||
if (reset) { | ||
curr_idx = 0; | ||
#if DT_NODE_HAS_STATUS(SW1_NODE, okay) | ||
if (gpio_pin_configure_dt(&sw1_spec, GPIO_INPUT) == 0) { | ||
if (gpio_pin_get_dt(&sw1_spec) == 1) { | ||
curr_idx = sys_rand8_get() % ARRAY_SIZE(known_ids); | ||
printk("Booting from curr_idx = %d\n", curr_idx); | ||
} | ||
} | ||
#endif | ||
} else { | ||
curr_idx++; | ||
} | ||
|
||
if (curr_idx >= ARRAY_SIZE(known_ids)) { | ||
return false; | ||
} | ||
|
||
*id = known_ids[curr_idx]; | ||
|
||
return true; | ||
} | ||
|
||
bool | ||
flash_map_id_get_current(uint8_t *id) | ||
{ | ||
if (curr_idx == -1 || curr_idx >= ARRAY_SIZE(known_ids)) { | ||
return false; | ||
} | ||
|
||
*id = known_ids[curr_idx]; | ||
|
||
return true; | ||
} |
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,4 @@ | ||
CONFIG_FLASH_RUNTIME_SOURCES=y | ||
CONFIG_BOOT_SIGNATURE_TYPE_RSA=y | ||
CONFIG_TEST_RANDOM_GENERATOR=y | ||
CONFIG_ENTROPY_GENERATOR=y |
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.
avoid looping when feature not enabled.