STM32 通用 Bootloader 恢复出厂固件 注意事项

参考STM32 通用 Bootloader:

在生成页面中选中了 恢复出厂固件引脚 功能后,即开启了出厂固件恢复功能。开发者可以将制作好的 app 固件烧录至 factory 分区中,在系统启动前按下恢复出厂固件引脚(可选择一个或者两个引脚作为固件恢复触发引脚)并保持 10S,即可从 factory 分区中恢复出厂固件到 app 分区中。

由于恢复出厂设置是直接copy,所以编译的出厂固件其实和下载固件配置相同。
bootloader配置如下:
在这里插入图片描述
修改fal_cfg.h如下:

/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-12-5      SummerGift   first version
 */

#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_

#include <rtthread.h>
#include <board.h>

#define RT_APP_PART_ADDR 0x8040000


extern const struct fal_flash_dev stm32_onchip_flash;

/* flash device table */
#define FAL_FLASH_DEV_TABLE                                          \
{                                                                    \
    &stm32_onchip_flash,                                             \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG

/* partition table */
#define FAL_PART_TABLE                                                                      \
{                                                                                           \
        {FAL_PART_MAGIC_WROD,    "bl",       "onchip_flash",       0*128* 1024 , 128 * 1024, 0},  \
        {FAL_PART_MAGIC_WROD,    "download", "onchip_flash",       1*128* 1024 , 128 * 1024, 0},  \
        {FAL_PART_MAGIC_WROD,    "app",      "onchip_flash",       2*128* 1024 , 128 * 1024, 0},  \
        {FAL_PART_MAGIC_WROD,    "factory",  "onchip_flash",       3*128* 1024 , 128 * 1024, 0},  \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */

修改link.sct如下:

; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************

LR_IROM1 0x08040000 0x00080000  {    ; load region size_region
  ER_IROM1 0x08040000 0x00080000  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
  }
  RW_IRAM1 0x20000000 0x00010000  {  ; RW data
   .ANY (+RW +ZI)
  }
}

编译生成bin文件。

此时如果直接使用st-link等工具烧录至0x8060000,会出现如下错误:

[E]Get firmware header occur CRC32(calc.crc: 43e482ba != hdr.info_crc32: 080409eb) error on 'factory' partition!

因为无论是factory还是download,在向app分区拷贝的时候,都会做一次解密算法(因为factory分区和download分区是可以放在片外spi flash的,如果不加密直接拷出来下载就能用了,而app分区在stm32内部,设置了读保护是不用担心的)。
所以需要将生成的文件用打包器加密,再重新用st-link烧录。
在这里插入图片描述
恢复出厂设置的日志如下:

[SFUD]Find a Winbond flash chip. Size is 16777216 bytes.
[SFUD]spi flash device is initialize success.

 __  ___     __   __   __  ___ 
|__)  |  __ |__) /  \ /  \  |  
|  \  |     |__) \__/ \__/  |  
2006 - 2019 Copyright by rt-thread team
                0.9.1 build Oct 13 2019
[D/FAL] (fal_flash_init:63) Flash device |             onchip_flash | addr: 0x08000000 | len: 0x00080000 | blk_size: 0x00000800 |initialized finish.
[D/FAL] (fal_flash_init:63) Flash device |                nor_flash | addr: 0x00000000 | len: 0x01000000 | blk_size: 0x00001000 |initialized finish.
[I/FAL] ==================== FAL partition table ====================
[I/FAL] | name     | flash_dev    |   offset   |    length  |
[I/FAL] -------------------------------------------------------------
[I/FAL] | app      | onchip_flash | 0x00040000 | 0x00020000 |
[I/FAL] | download | onchip_flash | 0x00020000 | 0x00020000 |
[I/FAL] | factory  | onchip_flash | 0x00060000 | 0x00020000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer (V0.4.0) initialize success.
[I/FAL] System initialization successful.
[I]RT-Thread OTA package(V0.2.1) initialize success.
[E]Get firmware header occur CRC32(calc.crc: 7b93c5c8 != hdr.info_crc32: ffffffff) error on 'download' partition!
[E]Get OTA download partition firmware header failed!
[E]Get firmware header occur CRC32(calc.crc: 7b93c5c8 != hdr.info_crc32: ffffffff) error on 'app' partition!
[I]Begin to execute the program on app partition.
[I/FAL] Find user firmware at app partition 0x08040000 successfully.
[I/FAL] Bootloader jumps to user firmware now.

 \ | /
- RT -     Thread Operating System
 / | \     4.0.0 build Oct 13 2019
 2006 - 2018 Copyright by rt-thread team
[D/FAL] (fal_flash_init:61) Flash device |             onchip_flash | addr: 0x08000000 | len: 0x00080000 | blk_size: 0x00000800 |initialized finish.
[I/FAL] ==================== FAL partition table ====================
[I/FAL] | name     | flash_dev    |   offset   |    length  |
[I/FAL] -------------------------------------------------------------
[I/FAL] | bl       | onchip_flash | 0x00000000 | 0x00020000 |
[I/FAL] | download | onchip_flash | 0x00020000 | 0x00020000 |
[I/FAL] | app      | onchip_flash | 0x00040000 | 0x00020000 |
[I/FAL] | factory  | onchip_flash | 0x00060000 | 0x00020000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer (V0.4.0) initialize success.
version 2.0.0
msh >



[SFUD]Find a Winbond flash chip. Size is 16777216 bytes.
[SFUD]spi flash device is initialize success.
[D/FAL] (fal_flash_init:63) Flash device |             onchip_flash | addr: 0x08000000 | len: 0x00080000 | blk_size: 0x00000800 |initialized finish.
[D/FAL] (fal_flash_init:63) Flash device |                nor_flash | addr: 0x00000000 | len: 0x01000000 | blk_size: 0x00001000 |initialized finish.
[I/FAL] ==================== FAL partition table ====================
[I/FAL] | name     | flash_dev    |   offset   |    length  |
[I/FAL] -------------------------------------------------------------
[I/FAL] | app      | onchip_flash | 0x00040000 | 0x00020000 |
[I/FAL] | download | onchip_flash | 0x00020000 | 0x00020000 |
[I/FAL] | factory  | onchip_flash | 0x00060000 | 0x00020000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer (V0.4.0) initialize success.
[I]RT-Thread OTA package(V0.2.1) initialize success.
[I]Verify 'factory' partition(fw ver: 2.0.0, timestamp: 1570940368) success.
[E]Get firmware header occur CRC32(calc.crc: 7b93c5c8 != hdr.info_crc32: ffffffff) error on 'app' partition!
[I]OTA firmware(app) upgrade startup.
[I]The partition 'app' is erasing.
[I]The partition 'app' erase success.
[I]OTA Write: [====================================================================================================] 100%
[I]Verify 'app' partition(fw ver: 2.0.0, timestamp: 1570940368) success.
[I/FAL] Find user firmware at app partition 0x08040000 successfully.
[I/FAL] Bootloader jumps to user firmware now.

 \ | /
- RT -     Thread Operating System
 / | \     4.0.0 build Oct 13 2019
 2006 - 2018 Copyright by rt-thread team
[D/FAL] (fal_flash_init:61) Flash device |             onchip_flash | addr: 0x08000000 | len: 0x00080000 | blk_size: 0x00000800 |initialized finish.
[I/FAL] ==================== FAL partition table ====================
[I/FAL] | name     | flash_dev    |   offset   |    length  |
[I/FAL] -------------------------------------------------------------
[I/FAL] | bl       | onchip_flash | 0x00000000 | 0x00020000 |
[I/FAL] | download | onchip_flash | 0x00020000 | 0x00020000 |
[I/FAL] | app      | onchip_flash | 0x00040000 | 0x00020000 |
[I/FAL] | factory  | onchip_flash | 0x00060000 | 0x00020000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer (V0.4.0) initialize success.
version 1.0.0
msh >

发布了323 篇原创文章 · 获赞 63 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/qq_27508477/article/details/102531607
今日推荐