1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/****************************************************************************
*
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
* Author: Lorenz Meier <lm@inf.ethz.ch>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file bl_update.c
*
* STM32F4 bootloader update tool.
*/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <arch/board/board.h>
#include "systemlib/systemlib.h"
#include "systemlib/err.h"
__EXPORT int bl_update_main(int argc, char *argv[]);
static void setopt(void);
int
bl_update_main(int argc, char *argv[])
{
if (argc != 2)
errx(1, "missing firmware filename or command");
if (!strcmp(argv[1], "setopt"))
setopt();
int fd = open(argv[1], O_RDONLY);
if (fd < 0)
err(1, "open %s", argv[1]);
struct stat s;
if (stat(argv[1], &s) < 0)
err(1, "stat %s", argv[1]);
/* sanity-check file size */
if (s.st_size > 16384)
errx(1, "%s: file too large", argv[1]);
uint8_t *buf = malloc(s.st_size);
if (buf == NULL)
errx(1, "failed to allocate %u bytes for firmware buffer", s.st_size);
if (read(fd, buf, s.st_size) != s.st_size)
err(1, "firmware read error");
close(fd);
uint32_t *hdr = (uint32_t *)buf;
if ((hdr[0] < 0x20000000) || /* stack not below RAM */
(hdr[0] > (0x20000000 + (128 * 1024))) || /* stack not above RAM */
(hdr[1] < 0x08000000) || /* entrypoint not below flash */
((hdr[1] - 0x08000000) > 16384)) { /* entrypoint not outside bootloader */
free(buf);
errx(1, "not a bootloader image");
}
warnx("image validated, erasing bootloader...");
usleep(10000);
/* prevent other tasks from running while we do this */
sched_lock();
/* unlock the control register */
volatile uint32_t *keyr = (volatile uint32_t *)0x40023c04;
*keyr = 0x45670123U;
*keyr = 0xcdef89abU;
volatile uint32_t *sr = (volatile uint32_t *)0x40023c0c;
volatile uint32_t *cr = (volatile uint32_t *)0x40023c10;
volatile uint8_t *base = (volatile uint8_t *)0x08000000;
/* check the control register */
if (*cr & 0x80000000) {
warnx("WARNING: flash unlock failed, flash aborted");
goto flash_end;
}
/* erase the bootloader sector */
*cr = 0x2;
*cr = 0x10002;
/* wait for the operation to complete */
while (*sr & 0x1000) {
}
if (*sr & 0xf2) {
warnx("WARNING: erase error 0x%02x", *sr);
goto flash_end;
}
/* verify the erase */
for (int i = 0; i < s.st_size; i++) {
if (base[i] != 0xff) {
warnx("WARNING: erase failed at %d - retry update, DO NOT reboot", i);
goto flash_end;
}
}
warnx("flashing...");
/* now program the bootloader - speed is not critical so use x8 mode */
for (int i = 0; i < s.st_size; i++) {
/* program a byte */
*cr = 1;
base[i] = buf[i];
/* wait for the operation to complete */
while (*sr & 0x1000) {
}
if (*sr & 0xf2) {
warnx("WARNING: program error 0x%02x", *sr);
goto flash_end;
}
}
/* re-lock the flash control register */
*cr = 0x80000000;
warnx("verifying...");
/* now run a verify pass */
for (int i = 0; i < s.st_size; i++) {
if (base[i] != buf[i]) {
warnx("WARNING: verify failed at %u - retry update, DO NOT reboot", i);
goto flash_end;
}
}
warnx("bootloader update complete");
flash_end:
/* unlock the scheduler */
sched_unlock();
free(buf);
exit(0);
}
static void
setopt(void)
{
volatile uint32_t *optcr = (volatile uint32_t *)0x40023c14;
const uint16_t opt_mask = (3 << 2); /* BOR_LEV bitmask */
const uint16_t opt_bits = (0 << 2); /* BOR = 0, setting for 2.7-3.6V operation */
if ((*optcr & opt_mask) == opt_bits)
errx(0, "option bits are already set as required");
/* unlock the control register */
volatile uint32_t *optkeyr = (volatile uint32_t *)0x40023c08;
*optkeyr = 0x08192a3bU;
*optkeyr = 0x4c5d6e7fU;
if (*optcr & 1)
errx(1, "option control register unlock failed");
/* program the new option value */
*optcr = (*optcr & ~opt_mask) | opt_bits | (1 << 1);
usleep(1000);
if ((*optcr & opt_mask) == opt_bits)
errx(0, "option bits set");
errx(1, "option bits setting failed; readback 0x%04x", *optcr);
}
|