Compare commits
No commits in common. "84e7d02db8d4f940315e6fd1a83eb78f0b0d425c" and "8255b2a672be805d42f405f62b105a3fe44802d6" have entirely different histories.
84e7d02db8
...
8255b2a672
|
|
@ -8,10 +8,5 @@
|
||||||
|
|
||||||
// File Associations
|
// File Associations
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"array": "c",
|
|
||||||
"string_view": "c",
|
|
||||||
"initializer_list": "c",
|
|
||||||
"span": "c",
|
|
||||||
"format": "c"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
/*
|
|
||||||
* 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;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2021 Nordic Semiconductor ASA
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "flash_partitions_128kb.dtsi"
|
|
||||||
|
|
@ -1,18 +0,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 */
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
# 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
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
# Enable Console and printk for logging
|
# Enable Console and printk for logging
|
||||||
CONFIG_CONSOLE=y
|
CONFIG_CONSOLE=y
|
||||||
CONFIG_LOG=y
|
CONFIG_LOG=y
|
||||||
CONFIG_LOG_PROCESS_THREAD=y
|
|
||||||
|
|
||||||
# Enable Shell
|
# Enable Shell
|
||||||
CONFIG_SHELL=y
|
CONFIG_SHELL=y
|
||||||
|
|
@ -9,13 +8,3 @@ CONFIG_REBOOT=y
|
||||||
|
|
||||||
# Enable the reset command
|
# Enable the reset command
|
||||||
CONFIG_KERNEL_SHELL=y
|
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
|
|
||||||
|
|
|
||||||
|
|
@ -1,167 +1,28 @@
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/logging/log.h>
|
|
||||||
#include <zephyr/shell/shell.h>
|
#include <zephyr/shell/shell.h>
|
||||||
#include <zephyr/sys/reboot.h>
|
#include <zephyr/sys/reboot.h>
|
||||||
#include <zephyr/drivers/flash.h>
|
#include <zephyr/logging/log.h>
|
||||||
#include <zephyr/storage/flash_map.h>
|
|
||||||
#include <zephyr/devicetree.h>
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(firmware_node, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(firmware_node, LOG_LEVEL_DBG);
|
||||||
|
|
||||||
// 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)
|
static int cmd_reset(const struct shell *shell, size_t argc, char **argv)
|
||||||
{
|
{
|
||||||
ARG_UNUSED(argc);
|
ARG_UNUSED(argc);
|
||||||
ARG_UNUSED(argv);
|
ARG_UNUSED(argv);
|
||||||
|
|
||||||
shell_print(shell, "Resetting system...");
|
shell_print(shell, "Rebooting system...");
|
||||||
k_msleep(100); // Give time for the message to be sent
|
k_msleep(100); // Give time for message to be sent
|
||||||
sys_reboot(SYS_REBOOT_COLD);
|
sys_reboot(SYS_REBOOT_WARM);
|
||||||
|
|
||||||
return 0;
|
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(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)
|
int main(void)
|
||||||
{
|
{
|
||||||
LOG_INF("Firmware Node starting up");
|
LOG_INF("Firmware Node starting up");
|
||||||
LOG_INF("Shell with reset command available");
|
LOG_INF("Shell with reset command available");
|
||||||
LOG_INF("Serial recovery command available");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,12 @@
|
||||||
# Sysbuild configuration for firmware_node with MCUboot
|
# Enable MCUboot
|
||||||
|
set(SB_CONFIG_BOOTLOADER_MCUBOOT y)
|
||||||
|
|
||||||
# Enable MCUboot as bootloader
|
# MCUboot configuration
|
||||||
set(SB_CONFIG_BOOTLOADER_MCUBOOT TRUE)
|
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)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
# Sysbuild configuration for firmware_node with MCUboot
|
|
||||||
|
|
||||||
# Enable MCUboot as bootloader
|
|
||||||
SB_CONFIG_BOOTLOADER_MCUBOOT=y
|
|
||||||
SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2021 Nordic Semiconductor ASA
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "../boards/flash_partitions_128kb.dtsi"
|
|
||||||
|
|
||||||
/ {
|
|
||||||
chosen {
|
|
||||||
zephyr,code-partition = &slot0_partition;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
# 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
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
/*
|
|
||||||
* MCUboot device tree overlay for firmware_node
|
|
||||||
* Uses shared flash partition layout
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "../boards/flash_partitions_128kb.dtsi"
|
|
||||||
|
|
||||||
/ {
|
|
||||||
chosen {
|
|
||||||
zephyr,code-partition = &boot_partition;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
/*
|
|
||||||
* 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;
|
|
||||||
};
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
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)
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
# 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
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
VERSION_MAJOR = 0
|
|
||||||
VERSION_MINOR = 0
|
|
||||||
PATCHLEVEL = 1
|
|
||||||
VERSION_TWEAK = 0
|
|
||||||
EXTRAVERSION = testing
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
#!/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' \
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
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
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
# Sysbuild configuration for firmware_node with MCUboot
|
|
||||||
|
|
||||||
# Enable MCUboot as bootloader
|
|
||||||
SB_CONFIG_BOOTLOADER_MCUBOOT=y
|
|
||||||
SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2021 Nordic Semiconductor ASA
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "flash_partitions_128kb.dtsi"
|
|
||||||
|
|
||||||
/ {
|
|
||||||
chosen {
|
|
||||||
zephyr,code-partition = &slot0_partition;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
/*
|
|
||||||
* 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";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
#
|
|
||||||
# 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
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
#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";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
Loading…
Reference in New Issue