40 #include <px4_platform_common/px4_config.h> 41 #include <px4_platform_common/log.h> 42 #include <px4_platform_common/module.h> 52 #include <arch/board/board.h> 54 #include <nuttx/progmem.h> 56 #if defined(CONFIG_ARCH_CHIP_STM32H7) 57 # define BL_FILE_SIZE_LIMIT 128*1024 58 # define STM_RAM_BASE STM32_AXISRAM_BASE 60 # define BL_FILE_SIZE_LIMIT 16384 61 # define STM_RAM_BASE STM32_SRAM_BASE 66 #if defined (CONFIG_STM32_STM32F4XXX) || defined (CONFIG_ARCH_CHIP_STM32F7) || \ 67 defined (CONFIG_ARCH_CHIP_STM32H7) 69 static int setopt(
void);
74 PX4_ERR(
"%s", reason);
77 PRINT_MODULE_DESCRIPTION(
"Utility to flash the bootloader from a file");
79 PRINT_MODULE_USAGE_NAME_SIMPLE(
"bl_update",
"command");
80 PRINT_MODULE_USAGE_COMMAND_DESCR(
"setopt",
"Set option bits to unlock the FLASH (only needed if in locked state)");
82 PRINT_MODULE_USAGE_COMMAND_DESCR(
"<file>",
"Bootloader bin file");
85 #endif // defined (CONFIG_STM32_STM32F4XXX) || defined (CONFIG_ARCH_CHIP_STM32F7) 91 #if !(defined (CONFIG_STM32_STM32F4XXX) || defined (CONFIG_ARCH_CHIP_STM32F7) \ 92 || defined (CONFIG_ARCH_CHIP_STM32H7)) 93 PX4_ERR(
"Not supported on this HW");
100 print_usage(
"missing firmware filename or command");
104 if (!strcmp(argv[1],
"setopt"))
109 int fd = open(argv[1], O_RDONLY);
113 PX4_ERR(
"open %s failed", argv[1]);
119 if (stat(argv[1], &s) != 0)
121 PX4_ERR(
"stat %s failed", argv[1]);
129 PX4_ERR(
"%s: file too large (limit: %u, actual: %d)", argv[1],
BL_FILE_SIZE_LIMIT, s.st_size);
134 uint8_t *buf = malloc(s.st_size);
138 PX4_ERR(
"failed to allocate %u bytes for firmware buffer", s.st_size);
143 if (
read(fd, buf, s.st_size) != s.st_size)
145 PX4_ERR(
"firmware read error");
153 uint32_t *hdr = (uint32_t *)buf;
157 (hdr[1] < PX4_FLASH_BASE) ||
161 PX4_ERR(
"not a bootloader image");
165 PX4_INFO(
"image validated, erasing bootloader...");
171 const size_t page = 0;
172 uint8_t *base = (uint8_t *) PX4_FLASH_BASE;
174 ssize_t size = up_progmem_eraseblock(page);
178 PX4_ERR(
"erase error at %p", &base[size]);
181 PX4_INFO(
"flashing...");
185 size = up_progmem_write((
size_t) base, buf, s.st_size);
187 if (size != s.st_size)
189 PX4_ERR(
"program error at %p", &base[size]);
197 PX4_INFO(
"verifying...");
200 for (
int i = 0; i < s.st_size; i++)
202 if (base[i] != buf[i]) {
203 PX4_WARN(
"verify failed at %i - retry update, DO NOT reboot", i);
208 PX4_INFO(
"bootloader update complete");
221 volatile uint32_t *optcr = (
volatile uint32_t *)0x40023c14;
223 const uint16_t opt_mask = (3 << 2);
224 const uint16_t opt_bits = (0 << 2);
226 if ((*optcr & opt_mask) == opt_bits) {
227 PX4_INFO(
"option bits are already set as required");
232 volatile uint32_t *optkeyr = (
volatile uint32_t *)0x40023c08;
233 *optkeyr = 0x08192a3bU;
234 *optkeyr = 0x4c5d6e7fU;
237 PX4_ERR(
"option control register unlock failed");
242 *optcr = (*optcr & ~opt_mask) | opt_bits | (1 << 1);
246 if ((*optcr & opt_mask) == opt_bits) {
247 PX4_INFO(
"option bits set");
251 PX4_ERR(
"option bits setting failed; readback 0x%04" PRIx32, *optcr);
254 #endif // defined (CONFIG_STM32_STM32F4XXX) || defined (CONFIG_ARCH_CHIP_STM32F7) #define BL_FILE_SIZE_LIMIT
static void print_usage()
static void read(bootloader_app_shared_t *pshared)
__EXPORT int bl_update_main(int argc, char *argv[])