PX4 Firmware
PX4 Autopilot Software http://px4.io
bl_update.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012, 2013 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file bl_update.c
36  *
37  * STM32F4 & STM32F7 bootloader update tool.
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <px4_platform_common/log.h>
42 #include <px4_platform_common/module.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdbool.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <sys/stat.h>
51 
52 #include <arch/board/board.h>
53 
54 #include <nuttx/progmem.h>
55 
56 #if defined(CONFIG_ARCH_CHIP_STM32H7)
57 # define BL_FILE_SIZE_LIMIT 128*1024
58 # define STM_RAM_BASE STM32_AXISRAM_BASE
59 #else
60 # define BL_FILE_SIZE_LIMIT 16384
61 # define STM_RAM_BASE STM32_SRAM_BASE
62 #endif
63 
64 __EXPORT int bl_update_main(int argc, char *argv[]);
65 
66 #if defined (CONFIG_STM32_STM32F4XXX) || defined (CONFIG_ARCH_CHIP_STM32F7) || \
67  defined (CONFIG_ARCH_CHIP_STM32H7)
68 
69 static int setopt(void);
70 
71 static void print_usage(const char *reason)
72 {
73  if (reason) {
74  PX4_ERR("%s", reason);
75  }
76 
77  PRINT_MODULE_DESCRIPTION("Utility to flash the bootloader from a file");
78 
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)");
81 
82  PRINT_MODULE_USAGE_COMMAND_DESCR("<file>", "Bootloader bin file");
83 }
84 
85 #endif // defined (CONFIG_STM32_STM32F4XXX) || defined (CONFIG_ARCH_CHIP_STM32F7)
86 
87 
88 int
89 bl_update_main(int argc, char *argv[])
90 {
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");
94  return 1;
95 }
96 #else
97 
98  if (argc != 2)
99  {
100  print_usage("missing firmware filename or command");
101  return 1;
102  }
103 
104  if (!strcmp(argv[1], "setopt"))
105  {
106  return setopt();
107  }
108 
109  int fd = open(argv[1], O_RDONLY);
110 
111  if (fd < 0)
112  {
113  PX4_ERR("open %s failed", argv[1]);
114  return 1;
115  }
116 
117  struct stat s;
118 
119  if (stat(argv[1], &s) != 0)
120  {
121  PX4_ERR("stat %s failed", argv[1]);
122  close(fd);
123  return 1;
124  }
125 
126  /* sanity-check file size */
127  if (s.st_size > BL_FILE_SIZE_LIMIT)
128  {
129  PX4_ERR("%s: file too large (limit: %u, actual: %d)", argv[1], BL_FILE_SIZE_LIMIT, s.st_size);
130  close(fd);
131  return 1;
132  }
133 
134  uint8_t *buf = malloc(s.st_size);
135 
136  if (buf == NULL)
137  {
138  PX4_ERR("failed to allocate %u bytes for firmware buffer", s.st_size);
139  close(fd);
140  return 1;
141  }
142 
143  if (read(fd, buf, s.st_size) != s.st_size)
144  {
145  PX4_ERR("firmware read error");
146  close(fd);
147  free(buf);
148  return 1;
149  }
150 
151  close(fd);
152 
153  uint32_t *hdr = (uint32_t *)buf;
154 
155  if ((hdr[0] < STM_RAM_BASE) || /* stack not below RAM */
156  (hdr[0] > (STM_RAM_BASE + (128 * 1024))) || /* stack not above RAM */
157  (hdr[1] < PX4_FLASH_BASE) || /* entrypoint not below flash */
158  ((hdr[1] - PX4_FLASH_BASE) > BL_FILE_SIZE_LIMIT)) /* entrypoint not outside bootloader */
159  {
160  free(buf);
161  PX4_ERR("not a bootloader image");
162  return 1;
163  }
164 
165  PX4_INFO("image validated, erasing bootloader...");
166  px4_usleep(10000);
167 
168  /* prevent other tasks from running while we do this */
169  sched_lock();
170 
171  const size_t page = 0;
172  uint8_t *base = (uint8_t *) PX4_FLASH_BASE;
173 
174  ssize_t size = up_progmem_eraseblock(page);
175 
176  if (size != BL_FILE_SIZE_LIMIT)
177  {
178  PX4_ERR("erase error at %p", &base[size]);
179  }
180 
181  PX4_INFO("flashing...");
182 
183  /* now program the bootloader - speed is not critical so use x8 mode */
184 
185  size = up_progmem_write((size_t) base, buf, s.st_size);
186 
187  if (size != s.st_size)
188  {
189  PX4_ERR("program error at %p", &base[size]);
190  goto flash_end;
191  }
192 
193  /* re-lock the flash control register */
194 
195  stm32_flash_lock();
196 
197  PX4_INFO("verifying...");
198 
199  /* now run a verify pass */
200  for (int i = 0; i < s.st_size; i++)
201  {
202  if (base[i] != buf[i]) {
203  PX4_WARN("verify failed at %i - retry update, DO NOT reboot", i);
204  goto flash_end;
205  }
206  }
207 
208  PX4_INFO("bootloader update complete");
209 
210 flash_end:
211  /* unlock the scheduler */
212  sched_unlock();
213 
214  free(buf);
215  exit(0);
216 }
217 
218 static int
219 setopt(void)
220 {
221  volatile uint32_t *optcr = (volatile uint32_t *)0x40023c14;
222 
223  const uint16_t opt_mask = (3 << 2); /* BOR_LEV bitmask */
224  const uint16_t opt_bits = (0 << 2); /* BOR = 0, setting for 2.7-3.6V operation */
225 
226  if ((*optcr & opt_mask) == opt_bits) {
227  PX4_INFO("option bits are already set as required");
228  return 0;
229  }
230 
231  /* unlock the control register */
232  volatile uint32_t *optkeyr = (volatile uint32_t *)0x40023c08;
233  *optkeyr = 0x08192a3bU;
234  *optkeyr = 0x4c5d6e7fU;
235 
236  if (*optcr & 1) {
237  PX4_ERR("option control register unlock failed");
238  return 1;
239  }
240 
241  /* program the new option value */
242  *optcr = (*optcr & ~opt_mask) | opt_bits | (1 << 1);
243 
244  px4_usleep(1000);
245 
246  if ((*optcr & opt_mask) == opt_bits) {
247  PX4_INFO("option bits set");
248  return 0;
249  }
250 
251  PX4_ERR("option bits setting failed; readback 0x%04" PRIx32, *optcr);
252  return 1;
253 }
254 #endif // defined (CONFIG_STM32_STM32F4XXX) || defined (CONFIG_ARCH_CHIP_STM32F7)
#define BL_FILE_SIZE_LIMIT
Definition: bl_update.c:60
Definition: I2C.hpp:51
static void print_usage()
static void read(bootloader_app_shared_t *pshared)
__EXPORT int bl_update_main(int argc, char *argv[])
Definition: bl_update.c:89
int fd
Definition: dataman.cpp:146
#define STM_RAM_BASE
Definition: bl_update.c:61