V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
msmmbl
V2EX  ›  嵌入式开发

有玩 mcuboot 的吗,可否帮忙看看我的应用为啥启动不了。

  •  
  •   msmmbl · 25 天前 · 541 次点击

    最近在 stm32F030c8 上尝试使用 mcuboot ,方便今后 ota 。框架采用了 zephyr

    stm32F030c8 的闪存有 64KB ,为了方便调试,mcuboot 编译完了有 20 多 KB ,于是我就按照 24KB + 20KB + 20KB 给闪存分为了 boot_partition + slot0_partition + slot1_partition ,三个分区。后面调试通了打算把 boot 分区优化到 16KB 。

    mcuboot 直接从 github 上 pull 了最新的代码,然后加了一个 custom 的 board 进去。为了跑通流程,我在 prj.conf 中把:

    1. CONFIG_MAIN_STACK_SIZE 改成了 1024 ,不然会爆内存
    2. CONFIG_BOOT_SIGNATURE_TYPE_NONE ,把验证签名给关了,先跑通后面再开。

    之后就是 west build ,然后 upload 到板子上,开机上电,串口正常显示 mcuboot 启动。

    之后,写了一个简单的闪灯应用,用了 platform.io + zephyr ,同一套板子配置文件,先不分区直接 upload ,确认了程序能跑。

    然后按照说明,

    1. 在 prj.conf 中加了,CONFIG_BOOTLOADER_MCUBOOT=y ,CONFIG_FLASH_MAP=y 。
    2. 之后在 dts 把 flash 的分区信息加上
    3. 在 platform.ini 中加上 build_flags = -DPM_STATIC_YML_FILE=zephyr/pm_static.yml
    4. 创建 pm_static.yml 也把分区信息加上。

    一波操作之后生成了带签名的 firmware.elf 。上传到板子上发现不运行。

    生成的 firmware 是

    Printing content of signed image: firmware.mcuboot.bin
    
    #### Image header (offset: 0x0) ############################
    magic:              0x96f3b83d
    load_addr:          0x0
    hdr_size:           0x200
    protected_tlv_size: 0x0
    img_size:           0x3d92
    flags:              0x0
    version:            0.0.0+0
    ############################################################
    #### Payload (offset: 0x200) ###############################
    |                                                          |
    |              FW image (size: 0x3d92 Bytes)               |
    |                                                          |
    ############################################################
    #### TLV area (offset: 0x3f92) #############################
    magic:     0x6907
    area size: 0x150
            ---------------------------------------------
            type: SHA256 (0x10)
            len:  0x20
            data: 0x90 0x7f 0xa3 0xbc 0x5d 0xdc 0x34 0xa8
                  0xa9 0x06 0xa4 0x64 0x5d 0x73 0x79 0x24
                  0xea 0x62 0x6e 0x6e 0x37 0x99 0x5c 0x72
                  0x61 0x85 0xf1 0xda 0x45 0x72 0x87 0xaa
            ---------------------------------------------
            type: KEYHASH (0x1)
            len:  0x20
            data: 0xfc 0x57 0x01 0xdc 0x61 0x35 0xe1 0x32
                  0x38 0x47 0xbd 0xc4 0x0f 0x04 0xd2 0xe5
                  0xbe 0xe5 0x83 0x3b 0x23 0xc2 0x9f 0x93
                  0x59 0x3d 0x00 0x01 0x8c 0xfa 0x99 0x94
            ---------------------------------------------
            type: RSA2048 (0x20)
            len:  0x100
            data: 0x67 0x30 0x0a 0xf8 0xee 0xad 0xba 0xbf
                  0xac 0x22 0x6d 0x3a 0xac 0x76 0x9f 0x60
                  0xb0 0x2c 0x3f 0x5e 0x4d 0xf7 0x3f 0x0d
                  0x9b 0x24 0xbe 0x4a 0x70 0xa4 0x44 0x08
                  0x7c 0xe7 0x27 0xce 0x0d 0xca 0x97 0x18
                  0x3c 0xc3 0x15 0xbe 0xb0 0x0b 0xb5 0xf1
                  0x10 0xff 0x84 0x59 0xca 0xc2 0x29 0x42
                  0xf0 0x8c 0xbb 0xd1 0x38 0xe5 0x6c 0xeb
                  0xde 0xf4 0x1f 0xa0 0x8f 0xde 0x0b 0xe2
                  0x23 0x3e 0x2e 0xd6 0xcb 0xf6 0x22 0x0d
                  0x54 0x88 0xf3 0x7e 0x86 0xd0 0xb7 0x7d
                  0x9d 0x75 0x8a 0xa9 0xa0 0x73 0xb6 0xee
                  0x9a 0x6b 0x80 0xb9 0xd7 0x87 0x65 0x3c
                  0x89 0x3b 0x90 0xd2 0x0a 0xf1 0xcb 0xe8
                  0x4b 0x73 0xbd 0xe2 0x05 0x75 0x35 0x8c
                  0xc0 0xa0 0x38 0x26 0x10 0xac 0x07 0xf3
                  0xae 0x58 0xa0 0x8e 0x64 0x8c 0x06 0x76
                  0x15 0x24 0xe5 0xeb 0x9a 0x56 0x97 0x91
                  0x99 0xa4 0x18 0xd9 0xd8 0x8a 0xaa 0xcb
                  0x73 0x07 0xd0 0xc5 0x96 0x79 0x95 0xa1
                  0xf3 0xec 0x21 0xf0 0xae 0xa5 0x8e 0x46
                  0xb8 0xdd 0x09 0x0b 0xbc 0x6f 0x4c 0xfe
                  0xa8 0x91 0xfc 0xdb 0x10 0x71 0x3a 0x16
                  0x5f 0xd2 0xf1 0x49 0x3c 0x24 0x58 0x8f
                  0xc5 0x82 0xc3 0x6c 0xf5 0xe9 0x16 0x08
                  0xb3 0x35 0x30 0x38 0x08 0x43 0x27 0x25
                  0x08 0x61 0x36 0xb8 0x83 0x04 0xac 0xae
                  0x94 0x8d 0x27 0x31 0x61 0x6b 0x11 0x1d
                  0xa2 0x0f 0x48 0xd9 0x0e 0x5c 0x27 0x3f
                  0x3c 0x19 0xa2 0x38 0xe8 0x90 0x32 0x50
                  0xd7 0xdd 0x4d 0x9d 0xf5 0x2d 0xa5 0xec
                  0xed 0xf6 0x04 0x51 0x91 0xf3 0xe8 0xc8
    ############################################################
    #### End of Image  #########################################
    dumpinfo has run successfully
    

    分区信息是:

    flash_primary:
      base_address: 0x08000000
      size: 64K
      # Bootloader
      mcuboot:
        address: 0x08000000
        size: 24K
      # Application slot 0
      image_0:
        address: 0x08006000
        size: 20K
      # Upgrade slot
      image_1:
        address: 0x0800b000
        size: 20K
    

    启动时候串口输出:

    *** Booting MCUboot v2.1.0-rc1-271-gc8470fb145f8 ***
    *** Using Zephyr OS build v4.1.0-1989-g1f9f4c8cee02 ***
    MCUBoot
    I: Starting bootloader
    D: Non-optimal sector distribution, slot0 has 18 usable sectors (20 assigned) but slot1 has 20 assigned
    I: Prim
    ary image: magic=good, swap_type=0x1, copy_done=0x3, image_ok=0x1
    I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
    I: Boot source: primary slot
    I: Image index: 0, Swap type: none
    I: Bootloader chainload address offset: 0x6000
    I: Image version: v0.0.0
    I: Jumping to the first image slot�*
    

    可以看到它 mcuboot 正确识别到了 image ,但是在跳转到 image 的那一刻就挂掉了。

    大家指点指点。

    第 1 条附言  ·  24 天前
    最后弃用了 platform.io ,改成直接用 west 编译,他会自动处理 slot 固件偏移的问题,暂时绕过了吧。
    6 条回复    2025-04-14 09:17:56 +08:00
    zzz22333
        1
    zzz22333  
       25 天前
    接个调试器吧,有调试器方便很多。
    msmmbl
        2
    msmmbl  
    OP
       25 天前
    @zzz22333 #1 嗯,我还没弄明白这种多分区的怎么用调试器调试
    zzz22333
        3
    zzz22333  
       24 天前
    @msmmbl #2 多分区没啥一样。 主要检查几个点:
    1. 是否关闭相关中断;
    2. 新的 app 的 sp 和 pc 值是否正确;
    3. cache 相关。
    4. 对齐相关的

    这个问题我之前也遇到过,但是忘记怎么解决的了,大致就是以上几个原因之一。
    msmmbl
        4
    msmmbl  
    OP
       24 天前
    @zzz22333 好的,多谢,Jlink 搞起
    msmmbl
        5
    msmmbl  
    OP
       18 天前 via Android
    @zzz22333 查到了,是 main 函数的栈分配少了。

    题外话:我现在很确性 stm32f030c8t6 不适合 mcuboot 或者 zephyr ,总 64kb 的 flash 空间,关闭 mucboot 所有的日志功能,才能把占用压到 16KB 。剩余 48Kb ,分为两个 slot ,一个 23KB ,一个 25KB ,去掉固件 2KB 的头,只有 21KB 用于开发。然后开了 UART Log GPIO LED 库直接占用达到了到接近 20kb ,真正用于写功能真的很少了,线程开多了还会爆只有 8KB 的 ram 。毫无开发乐趣。
    zzz22333
        6
    zzz22333  
       18 天前
    @msmmbl #5 mcuboot 有 2 个 solt ,肯定会多吃内存的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2182 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 07:46 · PVG 15:46 · LAX 00:46 · JFK 03:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.