Compare commits

..

3 Commits

Author SHA1 Message Date
Eduard Iten 84e7d02db8 defunct: playing around with bootloaders 2025-07-08 14:11:22 +02:00
Eduard Iten cc6b4488ee Fix MCUboot and app flash partitioning
- Corrected device tree overlays to prevent MCUboot and app overlap
- MCUboot now at 0x8000000 (32KB), app at 0x8008000 (96KB)
- Successfully boots MCUboot which chains to application
- Shell and reset command working properly
- Black Magic Probe flashing confirmed working for both domains
2025-07-07 16:04:29 +02:00
Eduard Iten 928a176e7c Step 2 complete: MCUboot integration with single-slot configuration
- Created sysbuild configuration for MCUboot bootloader
- Added device tree overlays for correct partition layout (32KB MCUboot, 96KB app)
- Fixed MCUboot partition addressing to use boot_partition instead of slot0_partition
- MCUboot successfully boots and chains to application
- Application runs with shell and custom reset command
- Disabled signature validation for testing purposes
2025-07-07 15:59:41 +02:00
24 changed files with 593 additions and 17 deletions

View File

@ -8,5 +8,10 @@
// File Associations
"files.associations": {
"array": "c",
"string_view": "c",
"initializer_list": "c",
"span": "c",
"format": "c"
}
}

View File

@ -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;
};
};

View File

@ -0,0 +1,7 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "flash_partitions_128kb.dtsi"

View File

@ -0,0 +1,18 @@
&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 */
};
};
};

View File

@ -0,0 +1,25 @@
# Partition manager configuration for firmware_node
# Boot partition (MCUboot)
mcuboot_primary:
address: 0x00000000
size: 0x8000
region: flash_primary
# Application partition (primary slot)
mcuboot_primary_app:
address: 0x00008000
size: 0x18000
region: flash_primary
# Secondary slot for updates
mcuboot_secondary:
address: 0x00020000
size: 0x18000
region: flash_primary
# Settings partition
settings_partition:
address: 0x00038000
size: 0x8000
region: flash_primary

View File

@ -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
@ -8,3 +9,13 @@ CONFIG_REBOOT=y
# Enable the reset command
CONFIG_KERNEL_SHELL=y
# Enable settings for persistent storage
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

View File

@ -1,28 +1,167 @@
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/devicetree.h>
LOG_MODULE_REGISTER(firmware_node, LOG_LEVEL_DBG);
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)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_print(shell, "Rebooting system...");
k_msleep(100); // Give time for message to be sent
sys_reboot(SYS_REBOOT_WARM);
shell_print(shell, "Resetting system...");
k_msleep(100); // Give time for the message to be sent
sys_reboot(SYS_REBOOT_COLD);
return 0;
}
// MCUboot serial recovery command handler
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, "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
// 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(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("Serial recovery command available");
return 0;
}

View File

@ -1,12 +1,4 @@
# Enable MCUboot
set(SB_CONFIG_BOOTLOADER_MCUBOOT y)
# Sysbuild configuration for firmware_node with MCUboot
# MCUboot configuration
set(SB_CONFIG_MCUBOOT_BOOTLOADER_MODE_OVERWRITE_ONLY y)
set(SB_CONFIG_MCUBOOT_SIGNATURE_KEY_FILE "bootloader/mcuboot/root-rsa-2048.pem")
set(SB_CONFIG_MCUBOOT_GENERATE_UNSIGNED_IMAGE y)
set(SB_CONFIG_MCUBOOT_GENERATE_CONFIRMED_IMAGE y)
# Enable USB CDC ACM for MCUboot console
set(SB_CONFIG_MCUBOOT_USB_SUPPORT y)
set(SB_CONFIG_MCUBOOT_SERIAL y)
# Enable MCUboot as bootloader
set(SB_CONFIG_BOOTLOADER_MCUBOOT TRUE)

View File

@ -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

View File

@ -0,0 +1,13 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "../boards/flash_partitions_128kb.dtsi"
/ {
chosen {
zephyr,code-partition = &slot0_partition;
};
};

View File

@ -0,0 +1,31 @@
# MCUboot configuration for firmware_node
# Enable basic console and logging for debugging
CONFIG_LOG=y
CONFIG_BOOT_BANNER=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_PRINTK=y
# Single slot configuration (no upgrades)
CONFIG_SINGLE_APPLICATION_SLOT=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 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

View File

@ -0,0 +1,12 @@
/*
* MCUboot device tree overlay for firmware_node
* Uses shared flash partition layout
*/
#include "../boards/flash_partitions_128kb.dtsi"
/ {
chosen {
zephyr,code-partition = &boot_partition;
};
};

View File

@ -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;
};

View File

@ -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)

View File

@ -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

View File

@ -0,0 +1,5 @@
VERSION_MAJOR = 0
VERSION_MINOR = 0
PATCHLEVEL = 1
VERSION_TWEAK = 0
EXTRAVERSION = testing

View File

@ -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' \

View File

@ -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

View File

@ -0,0 +1,42 @@
#include <zephyr/kernel.h>
#include <app_version.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/retention/bootmode.h>
#include <stdlib.h>
/* 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;
}

View File

@ -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

View File

@ -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;
};
};

View File

@ -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";
};
};

View File

@ -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

View File

@ -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";
};
};