Commit 1017c43b authored by Ederson de Souza's avatar Ederson de Souza Committed by Jamie
Browse files

samples: Add a sample for runtime chosen image



A sample for runtime chose image on FRDM K64F. It provides implementation
for boot_go and flash area id related hooks, showing how an external
module implementing hooks can influence which images end up being booted
on the platform.

In this sample, one can influence from which slot image will be loaded
by pressing a button on the board.

For more details on how to build and test the samples, check the
provided README.md.

Signed-off-by: default avatarEderson de Souza <ederson.desouza@intel.com>
Signed-off-by: default avatarTom Burdick <thomas.burdick@intel.com>
parent 2aa6da15
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
# 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
sample directory, run the following commands:

```
  source <path-to-zephyr>/zephyr-env.sh

  west build -p -b frdm_k64f ../../../boot/zephyr/ -- \
      -DEXTRA_ZEPHYR_MODULES=$PWD/hooks -DEXTRA_CONF_FILE="$PWD/sample.conf"

  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 boot the one on slot1.
+14 −0
Original line number Diff line number Diff line
# 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)
+5 −0
Original line number Diff line number Diff line
/ {
	chosen {
		zephyr,code-partition = &slot0_partition;
	};
};
+3 −0
Original line number Diff line number Diff line
CONFIG_BOOTLOADER_MCUBOOT=y

CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="./bootloader/mcuboot/root-rsa-2048.pem"
+15 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2024 Intel Corporation
 *
 * 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));
}
Loading