Skip to content
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

sysbuild: Add support for sysbuild-assigned MCUboot image IDs #17567

Merged
merged 10 commits into from
Oct 24, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ CONFIG_BOOT_SERIAL_IMG_GRP_IMAGE_STATE=y

CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y

CONFIG_FW_INFO_FIRMWARE_VERSION=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
&uart0 {
status = "okay";
current-speed = < 1000000 >;
};
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ CONFIG_BOOT_SERIAL_IMG_GRP_IMAGE_STATE=y
CONFIG_MCUBOOT_VERIFY_IMG_ADDRESS=n

CONFIG_BOOT_SERIAL_NO_APPLICATION=y

CONFIG_FW_INFO_FIRMWARE_VERSION=2
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ CONFIG_UART_CONSOLE=n
CONFIG_MCUBOOT_SERIAL=y
CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD=y
CONFIG_BOOT_SERIAL_IMG_GRP_IMAGE_STATE=y

CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y

CONFIG_FW_INFO_FIRMWARE_VERSION=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
&uart0 {
status = "okay";
current-speed = < 1000000 >;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};
7 changes: 6 additions & 1 deletion modules/mcuboot/boot/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ config PM_PARTITION_SIZE_MCUBOOT

config PM_PARTITION_SIZE_MCUBOOT
hex "Flash space allocated for the MCUboot partition" if !BOOT_USE_MIN_PARTITION_SIZE
default 0xb800 if MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 && SOC_SERIES_NRF54LX
default 0xbe00 if MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 && !SOC_SERIES_NRF54LX
default 0xc000
Copy link
Contributor

@nvlsianpu nvlsianpu Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default 0xc000 becomes dead assignment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is the default for child/parent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

help
Flash space set aside for the MCUboot partition.
Expand Down Expand Up @@ -103,7 +105,10 @@ config BOOT_ERASE_PROGRESSIVELY

config BOOT_IMAGE_ACCESS_HOOKS
bool
default y if UPDATEABLE_IMAGE_NUMBER > 1 && SOC_NRF5340_CPUAPP && PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY
# Child/parent check
default y if UPDATEABLE_IMAGE_NUMBER > 1 && SOC_NRF5340_CPUAPP && PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY && MCUBOOT_APPLICATION_IMAGE_NUMBER = -1
# Sysbuild check
default y if MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
depends on MCUBOOT

config BOOT_IMAGE_ACCESS_HOOK_NRF5340
Expand Down
20 changes: 14 additions & 6 deletions modules/mcuboot/hooks/nrf53_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@
#include "bootutil/fault_injection_hardening.h"
#include "flash_map_backend/flash_map_backend.h"

#define NET_CORE_SECONDARY_SLOT 1
#if CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
/* Sysbuild */
/* MCUboot image update image number */
#define NET_CORE_SECONDARY_IMAGE CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER
/* MCUboot serial recovery slot number */
#define NET_CORE_VIRTUAL_PRIMARY_SLOT (CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER * 2) + 1
#else
/* Legacy child/parent */
#define NET_CORE_SECONDARY_IMAGE 1
#define NET_CORE_VIRTUAL_PRIMARY_SLOT 3
#endif

#include <dfu/pcd.h>
#if defined(CONFIG_PCD_APP) && defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE) \
Expand Down Expand Up @@ -53,7 +62,6 @@ int pcd_version_cmp_net(const struct flash_area *fap, struct image_header *hdr)

firmware_info = fw_info_find((uint32_t)&read_buf);
if (firmware_info != NULL) {

if (firmware_info->version > version) {
return 1;
}
Expand All @@ -71,7 +79,7 @@ int pcd_version_cmp_net(const struct flash_area *fap, struct image_header *hdr)

int boot_read_image_header_hook(int img_index, int slot, struct image_header *img_head)
{
if (img_index == 1 && slot == 0) {
if (img_index == NET_CORE_SECONDARY_IMAGE && slot == 0) {
img_head->ih_magic = IMAGE_MAGIC;
img_head->ih_hdr_size = PM_MCUBOOT_PAD_SIZE;
img_head->ih_load_addr = PM_MCUBOOT_PRIMARY_1_ADDRESS;
Expand All @@ -90,7 +98,7 @@ int boot_read_image_header_hook(int img_index, int slot, struct image_header *im

fih_ret boot_image_check_hook(int img_index, int slot)
{
if (img_index == 1 && slot == 0) {
if (img_index == NET_CORE_SECONDARY_IMAGE && slot == 0) {
FIH_RET(FIH_SUCCESS);
}

Expand All @@ -106,7 +114,7 @@ int boot_perform_update_hook(int img_index, struct image_header *img_head,
int boot_read_swap_state_primary_slot_hook(int image_index,
struct boot_swap_state *state)
{
if (image_index == 1) {
if (image_index == NET_CORE_SECONDARY_IMAGE) {
/* Populate with fake data */
state->magic = BOOT_MAGIC_UNSET;
state->swap_type = BOOT_SWAP_TYPE_NONE;
Expand Down Expand Up @@ -160,7 +168,7 @@ int network_core_update(bool wait)
int boot_copy_region_post_hook(int img_index, const struct flash_area *area,
size_t size)
{
if (img_index == NET_CORE_SECONDARY_SLOT) {
if (img_index == NET_CORE_SECONDARY_IMAGE) {
return network_core_update(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ CONFIG_UART_CONSOLE=n
CONFIG_MCUBOOT_SERIAL=y
CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD=y
CONFIG_BOOT_SERIAL_IMG_GRP_IMAGE_STATE=y

CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y

CONFIG_FW_INFO_FIRMWARE_VERSION=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
&uart0 {
status = "okay";
current-speed = < 1000000 >;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ CONFIG_UART_CONSOLE=n
CONFIG_MCUBOOT_SERIAL=y
CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD=y
CONFIG_BOOT_SERIAL_IMG_GRP_IMAGE_STATE=y

CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y

CONFIG_FW_INFO_FIRMWARE_VERSION=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
&uart0 {
status = "okay";
current-speed = < 1000000 >;
};
1 change: 0 additions & 1 deletion samples/tfm/tfm_psa_template/sysbuild.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ SB_CONFIG_BOOTLOADER_MCUBOOT=y
SB_CONFIG_SECURE_BOOT_APPCORE=y
SB_CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256=y
SB_CONFIG_MCUBOOT_MODE_OVERWRITE_ONLY=y
SB_CONFIG_MCUBOOT_UPDATEABLE_IMAGES=2
SB_CONFIG_APPROTECT_LOCK=y
SB_CONFIG_SECURE_APPROTECT_LOCK=y
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
#
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

# unmodified copy from mcuboot

# MCUBoot settings
CONFIG_BOOT_MAX_IMG_SECTORS=512

Expand All @@ -20,6 +12,10 @@ CONFIG_CONSOLE=n
CONFIG_CONSOLE_HANDLER=n
CONFIG_UART_CONSOLE=n
CONFIG_MCUBOOT_SERIAL=y

CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD=y
CONFIG_BOOT_SERIAL_IMG_GRP_IMAGE_STATE=y

CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y

CONFIG_FW_INFO_FIRMWARE_VERSION=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
&uart0 {
status = "okay";
current-speed = < 1000000 >;
};
1 change: 1 addition & 0 deletions subsys/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ rsource "sdfw_services/Kconfig"
rsource "suit/Kconfig"
rsource "dult/Kconfig"
rsource "nrf_compress/Kconfig"
rsource "mcuboot_ids/Kconfig"
endmenu
49 changes: 49 additions & 0 deletions subsys/mcuboot_ids/Kconfig
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add help with something like (informative only, populated by the build-system) to each entry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why duplicate what is already there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, in prompt.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menu "MCUboot IDs (informative only, do not change)"

config MCUBOOT_APPLICATION_IMAGE_NUMBER
int "Application image number (informative only, do not change)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know sysbuild defined setting values will always take precedence over image defined values.

But is there no way we can avoid prompt settings purely for the purpose of sharing this info ?

side-note, do not change is strictly not required, cause if a user does change the value, then the value from sysbuild will overrule.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sysbuild they will be forced, the warning however is for child/parent because they can freely be changed with it (which could break things). I think you submitted 2 different PRs to allow options to be set without a prompt from sysbuild -> images upstream but don't think either ever got finished

default -1

config MCUBOOT_NETWORK_CORE_IMAGE_NUMBER
int "Netcore core image number (informative only, do not change)"
default -1

config MCUBOOT_WIFI_PATCHES_IMAGE_NUMBER
int "WiFi patches image number (informative only, do not change)"
default -1

config MCUBOOT_QSPI_XIP_IMAGE_NUMBER
int "QSPI XIP image number (informative only, do not change)"
default -1

config MCUBOOT_MCUBOOT_IMAGE_NUMBER
int "MCUboot (S0/S1) image number (informative only, do not change)"
default -1

if MCUBOOT

config MCUBOOT_MCUBOOT_S0_S1_VERSION_MAJOR
int "MCUboot (S0/S1) package major version number (informative only, do not change)"
default -1

config MCUBOOT_MCUBOOT_S0_S1_VERSION_MINOR
int "MCUboot (S0/S1) package minor version number (informative only, do not change)"
default -1

config MCUBOOT_MCUBOOT_S0_S1_VERSION_REVISION
int "MCUboot (S0/S1) package revision version number (informative only, do not change)"
default -1

config MCUBOOT_MCUBOOT_S0_S1_VERSION_BUILD_NUMBER
int "MCUboot (S0/S1) package build number version number (informative only, do not change)"
default -1

endif # MCUBOOT

endmenu
16 changes: 15 additions & 1 deletion subsys/nrf_compress/lzma/armthumb.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: 0BSD

Check failure on line 1 in subsys/nrf_compress/lzma/armthumb.c

View workflow job for this annotation

GitHub Actions / call-workflow / Run license checks on patch series (PR)

License Problem

"0BSD" license is not allowed for this file.

///////////////////////////////////////////////////////////////////////////////
//
Expand All @@ -7,17 +7,21 @@
///
// Authors: Igor Pavlov
// Lasse Collin
// With changes by Nordic Semiconductor ASA

Check failure on line 10 in subsys/nrf_compress/lzma/armthumb.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

C99_COMMENTS

subsys/nrf_compress/lzma/armthumb.c:10 do not use C99 // comments
//
///////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include "armthumb.h"

void arm_thumb_filter(uint8_t *buf, uint32_t buf_size, uint32_t pos, bool compress)
void arm_thumb_filter(uint8_t *buf, uint32_t buf_size, uint32_t pos, bool compress,
bool *end_part_match)
{
uint32_t i = 0;
uint32_t last_update_address = 0;

while ((i + 4) <= buf_size) {

if ((buf[i + 1] & 0xF8) == 0xF0 && (buf[i + 3] & 0xF8) == 0xF8) {
uint32_t dest;
uint32_t src = (((uint32_t)(buf[i + 1]) & 7) << 19)
Expand All @@ -26,6 +30,7 @@
| (uint32_t)(buf[i + 2]);

src <<= 1;
last_update_address = i;

if (compress) {
dest = pos + (uint32_t)(i) + 4 + src;
Expand All @@ -38,9 +43,18 @@
buf[i + 0] = (dest >> 11);
buf[i + 3] = 0xF8 | ((dest >> 8) & 0x7);
buf[i + 2] = (dest);

i += 2;
}

i += 2;
}

if (i == (buf_size - 2)) {
if (i > last_update_address && (buf[i + 1] & 0xF8) == 0xF0) {
*end_part_match = true;
} else {
*end_part_match = false;
}
}
}
3 changes: 2 additions & 1 deletion subsys/nrf_compress/lzma/armthumb.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: 0BSD

Check failure on line 1 in subsys/nrf_compress/lzma/armthumb.h

View workflow job for this annotation

GitHub Actions / call-workflow / Run license checks on patch series (PR)

License Problem

"0BSD" license is not allowed for this file.

///////////////////////////////////////////////////////////////////////////////
//
Expand All @@ -16,6 +16,7 @@
#include <stdint.h>
#include <stdbool.h>

void arm_thumb_filter(uint8_t *buf, uint32_t buf_size, uint32_t pos, bool compress);
void arm_thumb_filter(uint8_t *buf, uint32_t buf_size, uint32_t pos, bool compress,
bool *end_part_match);

#endif
43 changes: 40 additions & 3 deletions subsys/nrf_compress/src/arm_thumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ LOG_MODULE_REGISTER(nrf_compress_arm_thumb, CONFIG_NRF_COMPRESS_LOG_LEVEL);
BUILD_ASSERT((CONFIG_NRF_COMPRESS_CHUNK_SIZE % 4) == 0,
"CONFIG_NRF_COMPRESS_CHUNK_SIZE must be multiple of 4");

static uint8_t output_buffer[CONFIG_NRF_COMPRESS_CHUNK_SIZE];
/* Requires 2 extra bytes to allow checking cross-chunk 16-bit aligned ARM thumb instructions */
#define EXTRA_BUFFER_SIZE 2

static uint8_t output_buffer[CONFIG_NRF_COMPRESS_CHUNK_SIZE + EXTRA_BUFFER_SIZE];
static uint8_t temp_extra_buffer[EXTRA_BUFFER_SIZE];
static uint32_t data_position = 0;
static bool has_extra_buffer_data;

static int arm_thumb_init(void *inst)
{
data_position = 0;
has_extra_buffer_data = false;

return 0;
}
Expand All @@ -38,6 +44,7 @@ static int arm_thumb_deinit(void *inst)
static int arm_thumb_reset(void *inst)
{
data_position = 0;
has_extra_buffer_data = false;
memset(output_buffer, 0x00, sizeof(output_buffer));

return 0;
Expand All @@ -52,17 +59,47 @@ static int arm_thumb_decompress(void *inst, const uint8_t *input, size_t input_s
bool last_part, uint32_t *offset, uint8_t **output,
size_t *output_size)
{
bool end_part_match = false;
bool extra_buffer_used = false;

if (input_size > CONFIG_NRF_COMPRESS_CHUNK_SIZE) {
return -EINVAL;
}

memcpy(output_buffer, input, input_size);
arm_thumb_filter(output_buffer, input_size, data_position, false);
if (has_extra_buffer_data == true) {
/* Copy bytes from temporary holding buffer */
memcpy(output_buffer, temp_extra_buffer, sizeof(temp_extra_buffer));
memcpy(&output_buffer[sizeof(temp_extra_buffer)], input, input_size);
end_part_match = true;
extra_buffer_used = true;
has_extra_buffer_data = false;
input_size += sizeof(temp_extra_buffer);
} else {
memcpy(output_buffer, input, input_size);
}

arm_thumb_filter(output_buffer, input_size, data_position, false, &end_part_match);
data_position += input_size;
*offset = input_size;

if (extra_buffer_used) {
*offset -= sizeof(temp_extra_buffer);
}

*output = output_buffer;
*output_size = input_size;

if (end_part_match == true && !last_part) {
/* Partial match at end of input, need to cut the final 2 bytes off and stash
* them
*/
memcpy(temp_extra_buffer, &output_buffer[(input_size - sizeof(temp_extra_buffer))],
sizeof(temp_extra_buffer));
has_extra_buffer_data = true;
*output_size -= sizeof(temp_extra_buffer);
data_position -= sizeof(temp_extra_buffer);
}

return 0;
}

Expand Down
4 changes: 0 additions & 4 deletions subsys/nrf_security/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ config MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS
Promptless option used to control if the PSA Crypto core should have support for builtin keys or not.

config MBEDTLS_CFG_FILE
string "mbed TLS configuration file"
default "nrf-config.h"
help
Name of the config file for mbed TLS. This configuration file is used
in configurations with or without PSA APIs supported.

config MBEDTLS_PSA_CRYPTO_CONFIG
bool
Expand Down
Loading
Loading