From 84e7d02db8d4f940315e6fd1a83eb78f0b0d425c Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Tue, 8 Jul 2025 14:11:22 +0200 Subject: [PATCH] defunct: playing around with bootloaders --- software/.vscode/settings.json | 5 + .../boards/flash_partitions_128kb.dtsi | 29 ++++ .../boards/weact_stm32g431_core.overlay | 25 +--- software/apps/firmware_node/prj.conf | 6 + software/apps/firmware_node/src/main.c | 133 +++++++++++++++++- software/apps/firmware_node/sysbuild.cmake | 4 + .../sysbuild/firmware_node.overlay | 19 +-- .../apps/firmware_node/sysbuild/mcuboot.conf | 30 +++- .../firmware_node/sysbuild/mcuboot.overlay | 24 +--- .../boards/weact_stm32g431_core.overlay | 33 +++++ .../apps/snippets/bootloader/CMakeLists.txt | 8 ++ software/apps/snippets/bootloader/README.md | 34 +++++ software/apps/snippets/bootloader/VERSION | 5 + software/apps/snippets/bootloader/erase.sh | 8 ++ software/apps/snippets/bootloader/prj.conf | 16 +++ software/apps/snippets/bootloader/src/main.c | 42 ++++++ .../apps/snippets/bootloader/sysbuild.conf | 5 + .../bootloader/sysbuild/bootloader.overlay | 13 ++ .../sysbuild/flash_partitions_128kb.dtsi | 62 ++++++++ .../snippets/bootloader/sysbuild/mcuboot.conf | 46 ++++++ .../bootloader/sysbuild/mcuboot.overlay | 17 +++ 21 files changed, 487 insertions(+), 77 deletions(-) create mode 100644 software/apps/firmware_node/boards/flash_partitions_128kb.dtsi create mode 100644 software/apps/firmware_node/sysbuild.cmake create mode 100644 software/apps/firmware_node/sysbuild/mcuboot/boards/weact_stm32g431_core.overlay create mode 100644 software/apps/snippets/bootloader/CMakeLists.txt create mode 100644 software/apps/snippets/bootloader/README.md create mode 100644 software/apps/snippets/bootloader/VERSION create mode 100755 software/apps/snippets/bootloader/erase.sh create mode 100644 software/apps/snippets/bootloader/prj.conf create mode 100644 software/apps/snippets/bootloader/src/main.c create mode 100644 software/apps/snippets/bootloader/sysbuild.conf create mode 100644 software/apps/snippets/bootloader/sysbuild/bootloader.overlay create mode 100644 software/apps/snippets/bootloader/sysbuild/flash_partitions_128kb.dtsi create mode 100644 software/apps/snippets/bootloader/sysbuild/mcuboot.conf create mode 100644 software/apps/snippets/bootloader/sysbuild/mcuboot.overlay diff --git a/software/.vscode/settings.json b/software/.vscode/settings.json index 4f59a24..995d283 100644 --- a/software/.vscode/settings.json +++ b/software/.vscode/settings.json @@ -8,5 +8,10 @@ // File Associations "files.associations": { + "array": "c", + "string_view": "c", + "initializer_list": "c", + "span": "c", + "format": "c" } } \ No newline at end of file diff --git a/software/apps/firmware_node/boards/flash_partitions_128kb.dtsi b/software/apps/firmware_node/boards/flash_partitions_128kb.dtsi new file mode 100644 index 0000000..d3cba74 --- /dev/null +++ b/software/apps/firmware_node/boards/flash_partitions_128kb.dtsi @@ -0,0 +1,29 @@ +/* + * Flash partition layout for STM32G431 (128KB total flash) + * MCUboot + single application slot configuration + */ + +&flash0 { + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + boot_partition: partition@0 { + label = "mcuboot"; + reg = <0x00000000 0x0000A000>; /* 40 KB for MCUboot */ + read-only; + }; + + slot0_partition: partition@A000 { + label = "image-0"; + reg = <0x0000A000 0x00016000>; /* 88 KB for application */ + }; + }; +}; + +/ { + chosen { + zephyr,code-partition = &slot0_partition; + }; +}; diff --git a/software/apps/firmware_node/boards/weact_stm32g431_core.overlay b/software/apps/firmware_node/boards/weact_stm32g431_core.overlay index 521d20d..c35b119 100644 --- a/software/apps/firmware_node/boards/weact_stm32g431_core.overlay +++ b/software/apps/firmware_node/boards/weact_stm32g431_core.overlay @@ -4,27 +4,4 @@ * SPDX-License-Identifier: Apache-2.0 */ -&flash0 { - partitions { - compatible = "fixed-partitions"; - #address-cells = <1>; - #size-cells = <1>; - - boot_partition: partition@0 { - label = "mcuboot"; - reg = <0x00000000 0x00008000>; /* 32 KB */ - read-only; - }; - - slot0_partition: partition@8000 { - label = "image-0"; - reg = <0x00008000 0x00018000>; /* 96 KB */ - }; - }; -}; - -/ { - chosen { - zephyr,code-partition = &slot0_partition; - }; -}; +#include "flash_partitions_128kb.dtsi" diff --git a/software/apps/firmware_node/prj.conf b/software/apps/firmware_node/prj.conf index 4bc36b8..201f959 100644 --- a/software/apps/firmware_node/prj.conf +++ b/software/apps/firmware_node/prj.conf @@ -1,6 +1,7 @@ # Enable Console and printk for logging CONFIG_CONSOLE=y CONFIG_LOG=y +CONFIG_LOG_PROCESS_THREAD=y # Enable Shell CONFIG_SHELL=y @@ -13,3 +14,8 @@ CONFIG_KERNEL_SHELL=y CONFIG_SETTINGS=y CONFIG_SETTINGS_NVS=y CONFIG_NVS=y + +# Enable Flash and Flash Map for image trailer manipulation +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_FLASH_PAGE_LAYOUT=y diff --git a/software/apps/firmware_node/src/main.c b/software/apps/firmware_node/src/main.c index e63d373..0ff737a 100644 --- a/software/apps/firmware_node/src/main.c +++ b/software/apps/firmware_node/src/main.c @@ -2,9 +2,83 @@ #include #include #include +#include +#include +#include LOG_MODULE_REGISTER(firmware_node, LOG_LEVEL_INF); +// Image header magic number (from MCUboot) +#define IMAGE_MAGIC 0x96f3b83d +#define IMAGE_HEADER_SIZE 32 + +// Function to invalidate current image and trigger serial recovery +static int invalidate_current_image(void) +{ + const struct flash_area *fa; + int rc; + + // Get the flash area for the current image slot (slot0_partition) + rc = flash_area_open(FIXED_PARTITION_ID(slot0_partition), &fa); + if (rc != 0) { + LOG_ERR("Failed to open flash area: %d", rc); + return rc; + } + + // Ensure the flash area is valid + if (fa->fa_id != FIXED_PARTITION_ID(slot0_partition)) { + LOG_ERR("Invalid flash area ID: %d", fa->fa_id); + flash_area_close(fa); + return -EINVAL; + } + + // Get the flash device associated with this area + // This is necessary to perform erase operations + + const struct device *flash_dev = flash_area_get_device(fa); + if (flash_dev == NULL) { + LOG_ERR("Failed to get flash device for area"); + flash_area_close(fa); + return -ENODEV; + } + + struct flash_pages_info page_info; + off_t last_block_offset; + + // Find the last block of the flash area + rc = flash_get_page_info_by_offs(flash_dev, fa->fa_off + fa->fa_size - 1, &page_info); + if (rc != 0) { + LOG_ERR("Failed to get page info: %d", rc); + flash_area_close(fa); + return rc; + } + + // Calculate the last block offset + rc = flash_get_page_info_by_offs(flash_dev, fa->fa_off + fa->fa_size - 1, &page_info); + if (rc != 0) { + LOG_ERR("Failed to get page info: %d", rc); + flash_area_close(fa); + return rc; + } + last_block_offset = page_info.start_offset; + + // Convert absolute flash offset to relative offset within the flash area + off_t relative_offset = last_block_offset - fa->fa_off; + + // Erase the image trailer/metadata at the end of the partition + LOG_INF("Erasing image trailer at absolute offset: %ld, relative offset: %ld, size: %d bytes", + last_block_offset, relative_offset, page_info.size); + rc = flash_area_erase(fa, relative_offset, page_info.size); + if (rc != 0) { + LOG_ERR("Failed to erase image trailer: %d", rc); + + } else { + LOG_INF("Image trailer erased successfully"); + } + + flash_area_close(fa); + return rc; +} // Custom reset command handler static int cmd_reset(const struct shell *shell, size_t argc, char **argv) { @@ -19,30 +93,75 @@ static int cmd_reset(const struct shell *shell, size_t argc, char **argv) } // MCUboot serial recovery command handler -static int cmd_mcuboot_recovery(const struct shell *shell, size_t argc, char **argv) +static int cmd_recovery(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); shell_print(shell, "Entering MCUboot serial recovery mode..."); - shell_print(shell, "System will reset and MCUboot will wait for image upload via mcumgr"); + shell_print(shell, "Corrupting current image magic to trigger recovery..."); + + // Invalidate the current image by corrupting its header + int rc = invalidate_current_image(); + if (rc != 0) { + shell_error(shell, "Failed to invalidate image: %d", rc); + return rc; + } + + shell_print(shell, "Image magic corrupted. System will reset and MCUboot will detect bad image."); + shell_print(shell, "MCUboot should show error and wait for recovery."); k_msleep(100); // Give time for the message to be sent - // TODO: Set a flag or trigger MCUboot serial recovery mode - // For now, just reset - MCUboot serial recovery needs to be enabled - sys_reboot(SYS_REBOOT_COLD); + // Reset the system - MCUboot will detect invalid image and enter serial recovery + // log_process(true); + // sys_reboot(SYS_REBOOT_COLD); return 0; } +// Command to show firmware info +static int cmd_info(const struct shell *shell, size_t argc, char **argv) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + + const struct flash_area *fa; + int rc = flash_area_open(FIXED_PARTITION_ID(slot0_partition), &fa); + + if (rc != 0) { + shell_error(shell, "Failed to open flash area: %d", rc); + return rc; + } + + // Read the first few bytes to check the image header + uint32_t magic; + rc = flash_area_read(fa, 0, &magic, sizeof(magic)); + if (rc == 0) { + shell_print(shell, "Image magic: 0x%08x", magic); + if (magic == IMAGE_MAGIC) { + shell_print(shell, "Image header is valid"); + shell_print(shell, "Image starts at flash offset: 0x%lx", (unsigned long)fa->fa_off); + shell_print(shell, "Image partition size: %d bytes", fa->fa_size); + } else { + shell_print(shell, "Image header is INVALID (expected 0x%08x)", IMAGE_MAGIC); + } + } else { + shell_error(shell, "Failed to read image header: %d", rc); + } + + flash_area_close(fa); + return 0; +} + SHELL_CMD_REGISTER(reset, NULL, "Reset the system", cmd_reset); -SHELL_CMD_REGISTER(mcuboot_recovery, NULL, "Enter MCUboot serial recovery mode", cmd_mcuboot_recovery); +SHELL_CMD_REGISTER(recovery, NULL, "Enter MCUboot serial recovery mode", cmd_recovery); +SHELL_CMD_REGISTER(info, NULL, "Show firmware info", cmd_info); int main(void) { LOG_INF("Firmware Node starting up"); LOG_INF("Shell with reset command available"); - LOG_INF("MCUboot serial recovery command available"); + LOG_INF("Serial recovery command available"); return 0; } diff --git a/software/apps/firmware_node/sysbuild.cmake b/software/apps/firmware_node/sysbuild.cmake new file mode 100644 index 0000000..bd92704 --- /dev/null +++ b/software/apps/firmware_node/sysbuild.cmake @@ -0,0 +1,4 @@ +# Sysbuild configuration for firmware_node with MCUboot + +# Enable MCUboot as bootloader +set(SB_CONFIG_BOOTLOADER_MCUBOOT TRUE) diff --git a/software/apps/firmware_node/sysbuild/firmware_node.overlay b/software/apps/firmware_node/sysbuild/firmware_node.overlay index 521d20d..47502b0 100644 --- a/software/apps/firmware_node/sysbuild/firmware_node.overlay +++ b/software/apps/firmware_node/sysbuild/firmware_node.overlay @@ -4,24 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -&flash0 { - partitions { - compatible = "fixed-partitions"; - #address-cells = <1>; - #size-cells = <1>; - - boot_partition: partition@0 { - label = "mcuboot"; - reg = <0x00000000 0x00008000>; /* 32 KB */ - read-only; - }; - - slot0_partition: partition@8000 { - label = "image-0"; - reg = <0x00008000 0x00018000>; /* 96 KB */ - }; - }; -}; +#include "../boards/flash_partitions_128kb.dtsi" / { chosen { diff --git a/software/apps/firmware_node/sysbuild/mcuboot.conf b/software/apps/firmware_node/sysbuild/mcuboot.conf index f4bff49..425ad84 100644 --- a/software/apps/firmware_node/sysbuild/mcuboot.conf +++ b/software/apps/firmware_node/sysbuild/mcuboot.conf @@ -1,15 +1,31 @@ # MCUboot configuration for firmware_node +# Enable basic console and logging for debugging CONFIG_LOG=y CONFIG_BOOT_BANNER=y -CONFIG_MCUBOOT_LOG_LEVEL_DBG=y -# Disable console in MCUboot to allow serial recovery -CONFIG_CONSOLE=n +CONFIG_CONSOLE=y +CONFIG_UART_CONSOLE=y +CONFIG_PRINTK=y + # Single slot configuration (no upgrades) CONFIG_SINGLE_APPLICATION_SLOT=y -# Enable MCUboot serial recovery for firmware updates -CONFIG_MCUBOOT_SERIAL=y -CONFIG_BOOT_SERIAL_CDC_ACM=y +# Enable serial recovery mode (temporarily commented out for debugging) +# CONFIG_MCUBOOT_SERIAL=y +# CONFIG_BOOT_SERIAL_UART=y +# CONFIG_BOOT_SERIAL_DETECT_PORT=y -# Disable signature validation for testing +# Disable signature validation for testing to save space CONFIG_BOOT_SIGNATURE_TYPE_NONE=y + +# Size optimizations to fit in 40KB flash +CONFIG_SIZE_OPTIMIZATIONS=y +CONFIG_CBPRINTF_NANO=y +CONFIG_MINIMAL_LIBC=y +CONFIG_ASSERT=n + +# Disable debug features for size +CONFIG_DEBUG_INFO=n +CONFIG_DEBUG_OPTIMIZATIONS=n + +# Minimal heap for size optimization +CONFIG_HEAP_MEM_POOL_SIZE=0 diff --git a/software/apps/firmware_node/sysbuild/mcuboot.overlay b/software/apps/firmware_node/sysbuild/mcuboot.overlay index d31c4cb..02cfc92 100644 --- a/software/apps/firmware_node/sysbuild/mcuboot.overlay +++ b/software/apps/firmware_node/sysbuild/mcuboot.overlay @@ -1,27 +1,9 @@ /* - * Copyright (c) 2021 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 + * MCUboot device tree overlay for firmware_node + * Uses shared flash partition layout */ -&flash0 { - partitions { - compatible = "fixed-partitions"; - #address-cells = <1>; - #size-cells = <1>; - - boot_partition: partition@0 { - label = "mcuboot"; - reg = <0x00000000 0x00008000>; /* 32 KB */ - read-only; - }; - - slot0_partition: partition@8000 { - label = "image-0"; - reg = <0x00008000 0x00018000>; /* 96 KB */ - }; - }; -}; +#include "../boards/flash_partitions_128kb.dtsi" / { chosen { diff --git a/software/apps/firmware_node/sysbuild/mcuboot/boards/weact_stm32g431_core.overlay b/software/apps/firmware_node/sysbuild/mcuboot/boards/weact_stm32g431_core.overlay new file mode 100644 index 0000000..5095098 --- /dev/null +++ b/software/apps/firmware_node/sysbuild/mcuboot/boards/weact_stm32g431_core.overlay @@ -0,0 +1,33 @@ +/* + * MCUboot specific overlay for weact_stm32g431_core + * This overlay defines flash partitions for MCUboot + */ + +&flash0 { + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + boot_partition: partition@0 { + label = "mcuboot"; + reg = <0x00000000 0x00008000>; + }; + slot0_partition: partition@8000 { + label = "image-0"; + reg = <0x00008000 0x0000E000>; + }; + slot1_partition: partition@16000 { + label = "image-1"; + reg = <0x00016000 0x0000E000>; + }; + storage_partition: partition@24000 { + label = "storage"; + reg = <0x00024000 0x00004000>; + }; + }; +}; + +&chosen { + zephyr,boot-partition = &boot_partition; +}; diff --git a/software/apps/snippets/bootloader/CMakeLists.txt b/software/apps/snippets/bootloader/CMakeLists.txt new file mode 100644 index 0000000..d3d9aa1 --- /dev/null +++ b/software/apps/snippets/bootloader/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.20) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(bootloader LANGUAGES C) +zephyr_include_directories(../../../include) +add_subdirectory(../../../lib lib) +target_sources(app PRIVATE src/main.c) diff --git a/software/apps/snippets/bootloader/README.md b/software/apps/snippets/bootloader/README.md new file mode 100644 index 0000000..acc6bb2 --- /dev/null +++ b/software/apps/snippets/bootloader/README.md @@ -0,0 +1,34 @@ +# Firmware Node Application + +This Zephyr application provides firmware management capabilities for the irrigation system. + +**Tested on Zephyr 4.1.99** + +## Features + +### Step 1: Shell with Reset Command +- Shell interface with custom "reset" command +- Warm reboot functionality + +### Planned Features +- MCUboot support with partition manager +- Firmware version display +- MCUmgr support for OTA updates + +## Building + +```bash +west build -p auto -b weact_stm32g431_core apps/firmware_node -- -DBOARD_FLASH_RUNNER=blackmagicprobe +``` + +## Flashing + +```bash +west flash +``` + +## Usage + +Connect to the device via serial console and use the shell: +- `reset` - Reboot the system +- `help` - Show available commands diff --git a/software/apps/snippets/bootloader/VERSION b/software/apps/snippets/bootloader/VERSION new file mode 100644 index 0000000..c4c3177 --- /dev/null +++ b/software/apps/snippets/bootloader/VERSION @@ -0,0 +1,5 @@ +VERSION_MAJOR = 0 +VERSION_MINOR = 0 +PATCHLEVEL = 1 +VERSION_TWEAK = 0 +EXTRAVERSION = testing diff --git a/software/apps/snippets/bootloader/erase.sh b/software/apps/snippets/bootloader/erase.sh new file mode 100755 index 0000000..5cb5d75 --- /dev/null +++ b/software/apps/snippets/bootloader/erase.sh @@ -0,0 +1,8 @@ +#!/bin/bash +/home/edi/zephyr-sdk-0.17.1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb \ + -ex 'target extended-remote /dev/ttyACM0' \ + -ex 'monitor swdp_scan' \ + -ex 'attach 1' \ + -ex 'monitor erase_mass' \ + -ex 'detach' \ + -ex 'quit' \ diff --git a/software/apps/snippets/bootloader/prj.conf b/software/apps/snippets/bootloader/prj.conf new file mode 100644 index 0000000..0cd0c89 --- /dev/null +++ b/software/apps/snippets/bootloader/prj.conf @@ -0,0 +1,16 @@ +CONFIG_SHELL=y +CONFIG_REBOOT=y + +# MCUboot support for recovery request function +CONFIG_MCUBOOT_BOOTUTIL_LIB=y +CONFIG_MCUBOOT_IMG_MANAGER=y +CONFIG_IMG_MANAGER=y + +# Flash and Stream Configuration (required for IMG_MANAGER) +CONFIG_FLASH=y +CONFIG_STREAM_FLASH=y + +# Retention system +CONFIG_RETENTION=y +CONFIG_RETENTION_BOOT_MODE=y +CONFIG_RETAINED_MEM=y \ No newline at end of file diff --git a/software/apps/snippets/bootloader/src/main.c b/software/apps/snippets/bootloader/src/main.c new file mode 100644 index 0000000..7c6ee39 --- /dev/null +++ b/software/apps/snippets/bootloader/src/main.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include + +/* Shell command handler for "reset" */ +static int cmd_reset(const struct shell *sh, size_t argc, char **argv) +{ + shell_print(sh, "Rebooting system..."); + k_sleep(K_MSEC(100)); // Optional delay for user to see the message + sys_reboot(SYS_REBOOT_WARM); + return 0; +} + +static int cmd_download(const struct shell *sh, size_t argc, char **argv) +{ + int rc; + + /* Set boot mode to serial recovery */ + rc = bootmode_set(BOOT_MODE_TYPE_BOOTLOADER); + if (rc < 0) { + shell_error(sh, "Failed to set boot mode: %d", rc); + return rc; + } + + shell_print(sh, "Boot mode set to recovery. Rebooting to bootloader..."); + k_sleep(K_MSEC(100)); + sys_reboot(SYS_REBOOT_WARM); + return 0; +} + +/* Register the shell command */ +SHELL_CMD_REGISTER(reset, NULL, "Reboot the system", cmd_reset); +SHELL_CMD_REGISTER(download, NULL, "Download firmware", cmd_download); + +int main(void){ + printk("Bootloader test version %s\n", APP_VERSION_EXTENDED_STRING); + return 0; +} \ No newline at end of file diff --git a/software/apps/snippets/bootloader/sysbuild.conf b/software/apps/snippets/bootloader/sysbuild.conf new file mode 100644 index 0000000..288d889 --- /dev/null +++ b/software/apps/snippets/bootloader/sysbuild.conf @@ -0,0 +1,5 @@ +# Sysbuild configuration for firmware_node with MCUboot + +# Enable MCUboot as bootloader +SB_CONFIG_BOOTLOADER_MCUBOOT=y +SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y \ No newline at end of file diff --git a/software/apps/snippets/bootloader/sysbuild/bootloader.overlay b/software/apps/snippets/bootloader/sysbuild/bootloader.overlay new file mode 100644 index 0000000..940d0a3 --- /dev/null +++ b/software/apps/snippets/bootloader/sysbuild/bootloader.overlay @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2021 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "flash_partitions_128kb.dtsi" + +/ { + chosen { + zephyr,code-partition = &slot0_partition; + }; +}; diff --git a/software/apps/snippets/bootloader/sysbuild/flash_partitions_128kb.dtsi b/software/apps/snippets/bootloader/sysbuild/flash_partitions_128kb.dtsi new file mode 100644 index 0000000..9bdcc39 --- /dev/null +++ b/software/apps/snippets/bootloader/sysbuild/flash_partitions_128kb.dtsi @@ -0,0 +1,62 @@ +/* + * Devicetree Overlay for 128KB Flash + * - MCUboot Bootloader (32KB) + * - Application Slot (96KB) + */ + +&flash0 { + /delete-node/ partitions; + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + boot_partition: partition@0 { + label = "mcuboot"; + reg = <0x00000000 DT_SIZE_K(32)>; + read-only; + }; + + slot0_partition: partition@8000 { + label = "image-0"; + reg = <0x00008000 DT_SIZE_K(96)>; + }; + }; +}; + +/* Add retention memory to the existing SRAM node */ +&sram0 { + #address-cells = <1>; + #size-cells = <1>; + + retainedmem { + compatible = "zephyr,retained-ram"; + status = "okay"; + #address-cells = <1>; + #size-cells = <1>; + + boot_mode: retention@7f00 { + compatible = "zephyr,retention"; + status = "okay"; + reg = <0x7f00 0x100>; + prefix = [08 04]; + checksum = <1>; + }; + }; +}; + +/ { + chosen { + zephyr,boot-mode = &boot_mode; + zephyr,console = &cdc_acm_uart0; + }; +}; + +&zephyr_udc0 { + status = "okay"; + + cdc_acm_uart0: cdc_acm_uart0 { + compatible = "zephyr,cdc-acm-uart"; + label = "CDC_ACM_0"; + }; +}; \ No newline at end of file diff --git a/software/apps/snippets/bootloader/sysbuild/mcuboot.conf b/software/apps/snippets/bootloader/sysbuild/mcuboot.conf new file mode 100644 index 0000000..b077146 --- /dev/null +++ b/software/apps/snippets/bootloader/sysbuild/mcuboot.conf @@ -0,0 +1,46 @@ +# +# MCUboot Configuration for Serial Recovery over USB-CDC +# + +# Enables serial recovery mode in MCUboot. +CONFIG_MCUBOOT_SERIAL=y + +# Tell MCUboot to check for a trigger to enter recovery +CONFIG_BOOT_SERIAL_BOOT_MODE=y + +# --- USB Stack Configuration --- +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_PRODUCT="MCUboot Serial Recovery" + +# Use USB CDC ACM for MCUboot serial recovery (not UART) +CONFIG_BOOT_SERIAL_CDC_ACM=y + +# --- Disable Zephyr Console to avoid conflicts --- +# MCUboot's serial_adapter doesn't work well with the general console subsystem. +CONFIG_UART_CONSOLE=n +CONFIG_CONSOLE_HANDLER=n +CONFIG_CONSOLE=n + +# --- Flash and Stream Configuration (required for IMG_MANAGER) --- +CONFIG_FLASH=y +CONFIG_STREAM_FLASH=y + +# --- mcumgr Configuration --- +# MCUMGR requires NET_BUF, even for serial transport. +CONFIG_NET_BUF=y +CONFIG_NET_LOG=n + +# Enables the mcumgr library and necessary command handlers +CONFIG_MCUMGR=y +CONFIG_IMG_MANAGER=y +CONFIG_MCUMGR_GRP_IMG=y +CONFIG_MCUMGR_GRP_OS=y + +# --- Retention Configuration --- +CONFIG_RETAINED_MEM=y +CONFIG_RETENTION=y +CONFIG_RETENTION_BOOT_MODE=y + +# --- Optional: Reduce memory usage --- +CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1024 \ No newline at end of file diff --git a/software/apps/snippets/bootloader/sysbuild/mcuboot.overlay b/software/apps/snippets/bootloader/sysbuild/mcuboot.overlay new file mode 100644 index 0000000..fbe5662 --- /dev/null +++ b/software/apps/snippets/bootloader/sysbuild/mcuboot.overlay @@ -0,0 +1,17 @@ +#include "flash_partitions_128kb.dtsi" + +/ { + chosen { + zephyr,code-partition = &boot_partition; + zephyr,console = &cdc_acm_uart0; + }; +}; + +&zephyr_udc0 { + status = "okay"; + + cdc_acm_uart0: cdc_acm_uart0 { + compatible = "zephyr,cdc-acm-uart"; + label = "CDC_ACM_0"; + }; +}; \ No newline at end of file