zephyr mcuboot使用笔记

发布时间:2026/11/5 6:53:01
zephyr mcuboot使用笔记

zephyr mcuboot使用笔记

内容参考https://blog.csdn.net/weixin_44742767/article/details/158770644

创建bootloader工程

使用vscode workbench for zephyr创建工程



创建完成如下:

工程添加板级信息

1.修改默认设备树 zephyrproject\zephyr\boards\st\stm32f407_alientek
stm32f407_alientek.dts加入:
zephyr,code-partition = &slot0_partition;
&flash0 {…};
完整文件:

/* * Copyright (c) 2017 Linaro Limited * * SPDX-License-Identifier: Apache-2.0 *//dts-v1/;#include<st/f4/stm32f407Xg.dtsi>#include<st/f4/stm32f407z(e-g)tx-pinctrl.dtsi>#include<zephyr/dt-bindings/input/input-event-codes.h>/{model="STMicroelectronics STM32F4DISCOVERY board";compatible="st,stm32f4discovery";chosen{zephyr,console=&usart1;zephyr,shell-uart=&usart1;zephyr,sram=&sram0;zephyr,flash=&flash0;zephyr,code-partition=&slot0_partition;zephyr,dtcm=&ccm0;zephyr,canbus=&can2;zephyr,uart2=&usart2;/*与网口引脚冲突*/zephyr,uart3=&usart3;};leds{compatible="gpio-leds";/* red_led_0: led_0 { gpios = <&gpiof 9 GPIO_ACTIVE_LOW>; label = "User LD0"; }; */green_led_1:led_1{gpios=<&gpiof10GPIO_ACTIVE_LOW>;label="User LD1";};};gpio_keys{compatible="gpio-keys";/*正点原子开发板,按键低电平有效,SoC内部上拉,按键悬空时引脚被拉到高*/key_0:gpioe4_key{label="Key0";gpios=<&gpioe4(GPIO_PULL_UP|GPIO_ACTIVE_LOW)>;zephyr,code=<INPUT_KEY_0>;};key_1:gpioe3_key{label="Key1";gpios=<&gpioe3(GPIO_PULL_UP|GPIO_ACTIVE_LOW)>;zephyr,code=<INPUT_KEY_1>;};key_2:gpioe2_key{label="Key2";gpios=<&gpioe2(GPIO_PULL_UP|GPIO_ACTIVE_LOW)>;zephyr,code=<INPUT_KEY_2>;};};pwmleds:pwmleds{compatible="pwm-leds";red_pwm_led:pwm14_ch1_led{/*pwm14,channel1,周期100微秒,‌常规:占空比越大,高电平越长*/pwms=<&pwm141PWM_USEC(100)PWM_POLARITY_NORMAL>;};};aliases{led0=&red_pwm_led;/*红色led pwm输出*/led1=&green_led_1;/*绿色led gpio输出*/sw0=&key_0;/*按键0 gpio输入*/sw1=&key_1;sw2=&key_2;rtc=&rtc;die-temp0=&die_temp;volt-sensor0=&vref;volt-sensor1=&vbat;};zephyr,user{io-channels=<&adc15>,<&adc35>;};};&clk_lsi{status="okay";};&clk_hse{clock-frequency=<DT_FREQ_M(8)>;status="okay";};&pll{div-m=<8>;mul-n=<336>;div-p=<2>;div-q=<7>;clocks=<&clk_hse>;status="okay";};&rcc{clocks=<&pll>;clock-frequency=<DT_FREQ_M(168)>;ahb-prescaler=<1>;apb1-prescaler=<4>;apb2-prescaler=<2>;};// 扇区分布//Sector 0: 16 KB (0x0800_0000 - 0x0800_3FFF) ← 最小擦除单位//Sector 1: 16 KB (0x0800_4000 - 0x0800_7FFF)//Sector 2: 16 KB (0x0800_8000 - 0x0800_BFFF)//Sector 3: 16 KB (0x0800_C000 - 0x0800_FFFF)//Sector 4: 64 KB (0x0801_0000 - 0x0801_FFFF)//Sector 5: 128 KB (0x0802_0000 - 0x0803_FFFF) ← Slot 0 起始//Sector 6: 128 KB (0x0804_0000 - 0x0805_FFFF)//Sector 7: 128 KB (0x0806_0000 - 0x0807_FFFF) ← Slot 1 起始//Sector 8: 128 KB (0x0808_0000 - 0x0809_FFFF)//Sector 9: 128 KB (0x080A_0000 - 0x080B_FFFF) ← Scratch 起始//Sector 10: 128 KB (0x080C_0000 - 0x080D_FFFF)&flash0{status="okay";partitions{compatible="fixed-partitions";#address-cells=<1>;#size-cells=<1>;boot_partition:partition@0{label="mcuboot";reg=<0x00000000DT_SIZE_K(128)>;read-only;};slot0_partition:partition@20000{label="image-0";reg=<0x00020000DT_SIZE_K(256)>;};slot1_partition:partition@60000{label="image-1";reg=<0x00060000DT_SIZE_K(256)>;};scratch_partition:partition@a0000{label="image-scratch";reg=<0x000A0000DT_SIZE_K(256)>;};};};&usart1{pinctrl-0=<&usart1_tx_pa9&usart1_rx_pa10>;pinctrl-names="default";current-speed=<115200>;status="okay";};&usart2{pinctrl-0=<&usart2_tx_pa2&usart2_rx_pa3>;pinctrl-names="default";current-speed=<115200>;status="okay";};&usart3{pinctrl-0=<&usart3_tx_pb10&usart3_rx_pb11>;pinctrl-names="default";current-speed=<115200>;status="okay";};&timers2{status="okay";pwm2:pwm{status="okay";pinctrl-0=<&tim2_ch1_pa0>;pinctrl-names="default";};};/* PF9,红色LED灯,使用PWM14输出 */&timers14{status="okay";pwm14:pwm{status="okay";pinctrl-0=<&tim14_ch1_pf9>;pinctrl-names="default";};};&rtc{clocks=<&rccSTM32_CLOCK(APB1,28)>,<&rcc STM32_SRC_LSIRTC_SEL(2)>;status="okay";backup_regs{status="okay";};};zephyr_udc0:&usbotg_fs{pinctrl-0=<&usb_otg_fs_dm_pa11&usb_otg_fs_dp_pa12>;pinctrl-names="default";status="okay";};/*注意usb引脚复用,pa11,pa12*/&can1{pinctrl-0=<&can1_rx_pd0&can1_tx_pd1>;pinctrl-names="default";status="okay";};/*注意I2S引脚复用,pb12,pb13*/&can2{pinctrl-0=<&can2_rx_pb12&can2_tx_pb13>;pinctrl-names="default";status="okay";};&adc1{#address-cells=<1>;#size-cells=<0>;pinctrl-0=<&adc1_in5_pa5>;/*注意网口使用pa1*/pinctrl-names="default";st,adc-prescaler=<2>;st,adc-resolutions=<12>;status="okay";channel@5{reg=<5>;/* 硬件通道号,例如对应的 PA1 是 ADC1_IN1 */zephyr,gain="ADC_GAIN_1";/* 增益系数 1x */zephyr,reference="ADC_REF_INTERNAL";/* 使用内部参考电压 (通常为 VREF+) */zephyr,acquisition-time=<ADC_ACQ_TIME(ADC_ACQ_TIME_TICKS,480)>;zephyr,resolution=<12>;/* 采样分辨率 12位 */};};&adc3{#address-cells=<1>;#size-cells=<0>;pinctrl-0=<&adc3_in5_pf7>;/*接光敏电阻*/pinctrl-names="default";st,adc-prescaler=<4>;status="okay";channel@5{reg=<5>;/* 硬件通道号,例如对应的 PA1 是 ADC1_IN1 */zephyr,gain="ADC_GAIN_1";/* 增益系数 1x */zephyr,reference="ADC_REF_INTERNAL";/* 使用内部参考电压 (通常为 VREF+) */zephyr,acquisition-time=<ADC_ACQ_TIME_MAX>;zephyr,resolution=<12>;/* 采样分辨率 12位 */};};&die_temp{status="okay";};&vref{status="okay";};&vbat{status="okay";};&dma1{status="okay";};&plli2s{/* Minimize audio clock error (with i2s mck-enabled) for all of: * - 22.05kHz / 16b, 24b or 32b * - 44.1kHz / 16b, 24b or 32b * - 88.2kHz / 16b or 24b * Note: because the PLLI2S is dependent on the main PLL, the latter * should not be changed without checking impact on PLLI2S */div-m=<8>;mul-n=<271>;div-r=<2>;clocks=<&clk_hse>;status="okay";};&i2s2{mck-enabled;pinctrl-0=<&i2s2_ws_pb12&i2s2_ck_pb13&i2s2_sd_pc3&i2s2_mck_pc6>;//PC2 I2S_SDOUTpinctrl-names="default";clocks=<&rccSTM32_CLOCK(APB1,15)>,<&rcc STM32_SRC_PLLI2S_RI2S_SEL(0)>;status="okay";wm8978:wm8978@0{compatible="wlf,wm8978";reg=<0>;/* I2S 设备索引,一般从 0 开始 */status="okay";};};&i2c1{pinctrl-0=<&i2c1_scl_pb8&i2c1_sda_pb9>;pinctrl-names="default";clock-frequency=<I2C_BITRATE_STANDARD>;status="okay";eeprom0:eeprom@50{compatible="atmel,at24c02","atmel,at24";/*at24c02*/reg=<0x50>;size=<256>;pagesize=<8>;address-width=<8>;timeout=<5>;};};//sdio_d0_pc8 sdio_d1_pc9 sdio_d2_pc10 sdio_d3_pc11 sdio_ck_pc12 sdio_cmd_pd2&sdmmc1{status="okay";pinctrl-0=<&sdio_d0_pc8&sdio_d1_pc9&sdio_d2_pc10&sdio_d3_pc11&sdio_ck_pc12&sdio_cmd_pd2>;pinctrl-names="default";disk-name="SD";bus-width=<4>;clk-div=<4>;/*解决:日志出现stm32_sdmmc: sd read error 36*/};// &spi1 {// status = "okay";// /*PB3/PB4 和 JTAG 复用‌,jlink调试受影响 &spi1_nss_pb14 片选*/// pinctrl-0 = <&spi1_sck_pb3// &spi1_miso_pb4// &spi1_mosi_pb5>;// pinctrl-names = "default";// cs-gpios = <&gpiob 14 GPIO_ACTIVE_LOW>;// w25q128: w25q128@0 {// compatible = "jedec,spi-nor";// reg = <0>;// spi-max-frequency = <4000000>;// size = <0x8000000>;// jedec-id = [ef 40 18];// };// };&mac{status="okay";/* 1. 明确声明 MAC 与 PHY 的连接类型(LAN8720 选 "rmii") */phy-connection-type="rmii";/* 2. 指向你在 mdio 子节点中定义的 PHY 节点的标签名 */phy-handle=<&ethernet_phy>;/*定义文件在\modules\hal\stm32\dts\st\f4\stm32f407z(e-g)tx-pinctrl.dtsi*/pinctrl-0=<&eth_ref_clk_pa1&eth_crs_dv_pa7&eth_rxd0_pc4&eth_rxd1_pc5&eth_tx_en_pg11&eth_txd0_pg13&eth_txd1_pg14>;pinctrl-names="default";/* 绑定 MDIO 总线 */mdio{status="okay";pinctrl-0=<&eth_mdc_pc1&eth_mdio_pa2>;pinctrl-names="default";/* 注意:此处的标签名 ethernet_phy 要与上方的 phy-handle 严格一致 *//* LAN8720 PHY 节点配置 */ethernet_phy:ethernet-phy@0{compatible="ethernet-phy";reg=<0>;/* LAN8720 的 PHY 地址,通常为 0 或 1,根据硬件引脚 PHYAD0 决定 */status="okay";/* 💡 加入以下复位引脚配置 *//* 下方复位配置不起作用,但使用pd3做输出时,却可以复位。解决方法:工程添加配置 CONFIG_NET_L2_ETHERNET=y *//* GPIO_ACTIVE_LOW 对应 LAN8720 的低电平复位特性 */reset-gpios=<&gpiod3GPIO_ACTIVE_LOW>;/* 可选:复位延时参数(单位:微秒) *//* LAN8720 要求复位低电平至少维持 100微秒(us),这里给 50ms,足以让 LAN8720 彻底清醒 */reset-assert-duration-us=<50000>;};};};

2.添加bootloader工程板级配置
zephyrproject\bootloader\mcuboot\boot\zephyr\boards
增加 stm32f407_alientek.overlay

/{chosen{zephyr,code-partition=&boot_partition;};};

增加 stm32f407_alientek.conf

# 禁用自动计算,手动设置最大镜像扇区数(根据你的分区大小调整) CONFIG_BOOT_MAX_IMG_SECTORS_AUTO=n CONFIG_BOOT_MAX_IMG_SECTORS=16CONFIG_LOG=y CONFIG_MCUBOOT_LOG_LEVEL_DBG=y

3.编译bootloader工程,烧录。
日志:
[Rx][10:59:27.394] *** Using Zephyr OS build v4.4.0-5153-gb54e0ba58fde ***
I: Starting bootloader
D: context_boot_go
I: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Boot source: primary slot
I: Image index: 0, Swap type: none
D: boot_validate_slot: slot 0, expected_swap_type 0
D: Left boot_go with success == 0
E: Unable to find bootable image

创建app工程

参考以前的文章,不再详细说明。

1.修改工程配置

#Enable Zephyr application to be booted by MCUbootCONFIG_BOOTLOADER_MCUBOOT=y#CONFIG_BOOT_UPGRADE_ONLY=y #当 mcuboot 发现 slot1 里有新固件时,直接把新固件覆盖到 slot0 并运行,不需要做可回滚升级#Use thedefaultMCUBoot PEM keyfile(BOOT_SIGNATURE_KEY_FILE)CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="bootloader/mcuboot/root-rsa-2048.pem"

2.修改vscode launch脚本

3.编译,烧录。

4.日志
[Rx][11:01:07.923] *** Using Zephyr OS build v4.4.0-5153-gb54e0ba58fde ***
I: Starting bootloader
D: context_boot_go
I: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Boot source: primary slot
I: Image index: 0, Swap type: none
D: boot_validate_slot: slot 0, expected_swap_type 0
D: bootutil_img_validate: flash area 0x800837c
D: bootutil_img_hash
[Rx][11:01:08.094] D: bootutil_tlv_iter_begin: type 65535, prot == 0
D: bootutil_img_validate: TLV off 217780, end 218112
D: bootutil_tlv_iter_next: searching for 65535 (65535 is any) starting at 217780 ending at 218112
D: bootutil_tlv_iter_next: TLV 16 found at 217784 (size 32)
D: bootutil_img_validate: EXPECTED_HASH_TLV == 16
D: bootutil_tlv_iter_next: searching for 65535 (65535 is any) starting at 217816 ending at 218112
D: bootutil_tlv_iter_next: TLV 1 found at 217820 (size 32)
D: bootutil_img_validate: EXPECTED_KEY_TLV == 1
D: bootutil_find_key
D: bootutil_tlv_iter_next: searching for 65535 (65535 is any) starting at 217852 ending at 218112
D: bootutil_tlv_iter_next: TLV 32 found at 217856 (size 256)
D: bootutil_img_validate: EXPECTED_SIG_TLV == 32
D: bootutil_verify_sig: RSA key_id 0
[Rx][11:01:08.214] D: bootutil_tlv_iter_next: searching for 65535 (65535 is any) starting at 218112 ending at 218112
D: bootutil_tlv_iter_next: TLV 65535 not found
D: Left boot_go with success == 1
I: Bootloader chainload address offset: 0x20000
I: Image version: v0.0.0
[Rx][11:01:08.237]
I: Jumping to the first image slot