forked from toxygen/stm32flash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32.c
374 lines (315 loc) · 11.4 KB
/
stm32.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
stm32flash - Open Source ST STM32 flash program for *nix
Copyright (C) 2010 Geoffrey McRae <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "stm32.h"
#include "utils.h"
#define STM32_ACK 0x79
#define STM32_NACK 0x1F
#define STM32_CMD_INIT 0x7F
#define STM32_CMD_GET 0x00 /* get the version and command supported */
#define STM32_CMD_EE 0x44 /* extended erase */
struct stm32_cmd {
uint8_t get;
uint8_t gvr;
uint8_t gid;
uint8_t rm;
uint8_t go;
uint8_t wm;
uint8_t er; /* this may be extended erase */
uint8_t wp;
uint8_t uw;
uint8_t rp;
uint8_t ur;
};
/* device table */
const stm32_dev_t devices[] = {
{0x412, "Low-density" , 0x20000200, 0x20002800, 0x08000000, 0x08008000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800},
{0x410, "Medium-density" , 0x20000200, 0x20005000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800},
{0x411, "STM32F2xx" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF},
{0x413, "STM32F4xx" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF},
{0x414, "High-density" , 0x20000200, 0x20010000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800},
{0x416, "Medium-density ULP", 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000},
{0x418, "Connectivity line" , 0x20001000, 0x20010000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFB000, 0x1FFFF800},
{0x420, "Medium-density VL" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800},
{0x428, "High-density VL" , 0x20000200, 0x20008000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800},
{0x430, "XL-density" , 0x20000800, 0x20018000, 0x08000000, 0x08100000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFE000, 0x1FFFF800},
{0x440, "STM32F0xx" , 0x20000800, 0x20002000, 0x08000000, 0x08010000, 4, 1024, 0x1FFFF800, 0x1FFFF80B, 0x1FFFEC00, 0x1FFFF800},
{0x0}
};
/* internal functions */
uint8_t stm32_gen_cs(const uint32_t v);
void stm32_send_byte(const stm32_t *stm, uint8_t byte);
uint8_t stm32_read_byte(const stm32_t *stm);
char stm32_send_command(const stm32_t *stm, const uint8_t cmd);
/* stm32 programs */
extern unsigned int stmreset_length;
extern unsigned char stmreset_binary[];
uint8_t stm32_gen_cs(const uint32_t v) {
return ((v & 0xFF000000) >> 24) ^
((v & 0x00FF0000) >> 16) ^
((v & 0x0000FF00) >> 8) ^
((v & 0x000000FF) >> 0);
}
void stm32_send_byte(const stm32_t *stm, uint8_t byte) {
serial_err_t err;
err = serial_write(stm->serial, &byte, 1);
if (err != SERIAL_ERR_OK) {
perror("send_byte");
assert(0);
}
}
uint8_t stm32_read_byte(const stm32_t *stm) {
uint8_t byte;
serial_err_t err;
err = serial_read(stm->serial, &byte, 1);
if (err != SERIAL_ERR_OK) {
perror("read_byte");
assert(0);
}
return byte;
}
char stm32_send_command(const stm32_t *stm, const uint8_t cmd) {
stm32_send_byte(stm, cmd);
stm32_send_byte(stm, cmd ^ 0xFF);
if (stm32_read_byte(stm) != STM32_ACK) {
fprintf(stderr, "Error sending command 0x%02x to device\n", cmd);
return 0;
}
return 1;
}
stm32_t* stm32_init(const serial_t *serial, const char init) {
uint8_t len;
stm32_t *stm;
stm = calloc(sizeof(stm32_t), 1);
stm->cmd = calloc(sizeof(stm32_cmd_t), 1);
stm->serial = serial;
if (init) {
stm32_send_byte(stm, STM32_CMD_INIT);
if (stm32_read_byte(stm) != STM32_ACK) {
stm32_close(stm);
fprintf(stderr, "Failed to get init ACK from device\n");
return NULL;
}
}
/* get the bootloader information */
if (!stm32_send_command(stm, STM32_CMD_GET)) return 0;
len = stm32_read_byte(stm) + 1;
stm->bl_version = stm32_read_byte(stm); --len;
stm->cmd->get = stm32_read_byte(stm); --len;
stm->cmd->gvr = stm32_read_byte(stm); --len;
stm->cmd->gid = stm32_read_byte(stm); --len;
stm->cmd->rm = stm32_read_byte(stm); --len;
stm->cmd->go = stm32_read_byte(stm); --len;
stm->cmd->wm = stm32_read_byte(stm); --len;
stm->cmd->er = stm32_read_byte(stm); --len;
stm->cmd->wp = stm32_read_byte(stm); --len;
stm->cmd->uw = stm32_read_byte(stm); --len;
stm->cmd->rp = stm32_read_byte(stm); --len;
stm->cmd->ur = stm32_read_byte(stm); --len;
if (len > 0) {
fprintf(stderr, "Seems this bootloader returns more then we understand in the GET command, we will skip the unknown bytes\n");
while(len-- > 0) stm32_read_byte(stm);
}
if (stm32_read_byte(stm) != STM32_ACK) {
stm32_close(stm);
return NULL;
}
/* get the version and read protection status */
if (!stm32_send_command(stm, stm->cmd->gvr)) {
stm32_close(stm);
return NULL;
}
stm->version = stm32_read_byte(stm);
stm->option1 = stm32_read_byte(stm);
stm->option2 = stm32_read_byte(stm);
if (stm32_read_byte(stm) != STM32_ACK) {
stm32_close(stm);
return NULL;
}
/* get the device ID */
if (!stm32_send_command(stm, stm->cmd->gid)) {
stm32_close(stm);
return NULL;
}
len = stm32_read_byte(stm) + 1;
if (len != 2) {
stm32_close(stm);
fprintf(stderr, "More then two bytes sent in the PID, unknown/unsupported device\n");
return NULL;
}
stm->pid = (stm32_read_byte(stm) << 8) | stm32_read_byte(stm);
if (stm32_read_byte(stm) != STM32_ACK) {
stm32_close(stm);
return NULL;
}
stm->dev = devices;
while(stm->dev->id != 0x00 && stm->dev->id != stm->pid)
++stm->dev;
if (!stm->dev->id) {
fprintf(stderr, "Unknown/unsupported device (Device ID: 0x%03x)\n", stm->pid);
stm32_close(stm);
return NULL;
}
return stm;
}
void stm32_close(stm32_t *stm) {
if (stm) free(stm->cmd);
free(stm);
}
char stm32_read_memory(const stm32_t *stm, uint32_t address, uint8_t data[], unsigned int len) {
uint8_t cs;
unsigned int i;
assert(len > 0 && len < 257);
/* must be 32bit aligned */
assert(address % 4 == 0);
address = be_u32 (address);
cs = stm32_gen_cs(address);
if (!stm32_send_command(stm, stm->cmd->rm)) return 0;
assert(serial_write(stm->serial, &address, 4) == SERIAL_ERR_OK);
stm32_send_byte(stm, cs);
if (stm32_read_byte(stm) != STM32_ACK) return 0;
i = len - 1;
stm32_send_byte(stm, i);
stm32_send_byte(stm, i ^ 0xFF);
if (stm32_read_byte(stm) != STM32_ACK) return 0;
assert(serial_read(stm->serial, data, len) == SERIAL_ERR_OK);
return 1;
}
char stm32_write_memory(const stm32_t *stm, uint32_t address, uint8_t data[], unsigned int len) {
uint8_t cs;
unsigned int i;
int c, extra;
assert(len > 0 && len < 257);
/* must be 32bit aligned */
assert(address % 4 == 0);
address = be_u32 (address);
cs = stm32_gen_cs(address);
/* send the address and checksum */
if (!stm32_send_command(stm, stm->cmd->wm)) return 0;
assert(serial_write(stm->serial, &address, 4) == SERIAL_ERR_OK);
stm32_send_byte(stm, cs);
if (stm32_read_byte(stm) != STM32_ACK) return 0;
/* setup the cs and send the length */
extra = len % 4;
cs = len - 1 + extra;
stm32_send_byte(stm, cs);
/* write the data and build the checksum */
for(i = 0; i < len; ++i)
cs ^= data[i];
assert(serial_write(stm->serial, data, len) == SERIAL_ERR_OK);
/* write the alignment padding */
for(c = 0; c < extra; ++c) {
stm32_send_byte(stm, 0xFF);
cs ^= 0xFF;
}
/* send the checksum */
stm32_send_byte(stm, cs);
return stm32_read_byte(stm) == STM32_ACK;
}
char stm32_wunprot_memory(const stm32_t *stm) {
if (!stm32_send_command(stm, stm->cmd->uw)) return 0;
if (!stm32_send_command(stm, 0x8C )) return 0;
return 1;
}
char stm32_erase_memory(const stm32_t *stm, uint8_t spage, uint8_t pages) {
if (!stm32_send_command(stm, stm->cmd->er)) {
fprintf(stderr, "Can't initiate chip erase!\n");
return 0;
}
/* The erase command reported by the bootloader is either 0x43 or 0x44 */
/* 0x44 is Extended Erase, a 2 byte based protocol and needs to be handled differently. */
if (stm->cmd->er == STM32_CMD_EE) {
/* Not all chips using Extended Erase support mass erase */
/* Currently known as not supporting mass erase is the Ultra Low Power STM32L15xx range */
/* So if someone has not overridden the default, but uses one of these chips, take it out of */
/* mass erase mode, so it will be done page by page. This maximum might not be correct either! */
if (stm->pid == 0x416 && pages == 0xFF) pages = 0xF8; /* works for the STM32L152RB with 128Kb flash */
if (pages == 0xFF) {
stm32_send_byte(stm, 0xFF);
stm32_send_byte(stm, 0xFF); // 0xFFFF the magic number for mass erase
stm32_send_byte(stm, 0x00); // 0x00 the XOR of those two bytes as a checksum
if (stm32_read_byte(stm) != STM32_ACK) {
fprintf(stderr, "Mass erase failed. Try specifying the number of pages to be erased.\n");
return 0;
}
return 1;
}
uint16_t pg_num;
uint8_t pg_byte;
uint8_t cs = 0;
stm32_send_byte(stm, pages >> 8); // Number of pages to be erased, two bytes, MSB first
stm32_send_byte(stm, pages & 0xFF);
for (pg_num = 0; pg_num <= pages; pg_num++) {
pg_byte = pg_num >> 8;
cs ^= pg_byte;
stm32_send_byte(stm, pg_byte);
pg_byte = pg_num & 0xFF;
cs ^= pg_byte;
stm32_send_byte(stm, pg_byte);
}
stm32_send_byte(stm, 0x00); // Ought to need to hand over a valid checksum here...but 0 seems to work!
if (stm32_read_byte(stm) != STM32_ACK) {
fprintf(stderr, "Page-by-page erase failed. Check the maximum pages your device supports.\n");
return 0;
}
return 1;
}
/* And now the regular erase (0x43) for all other chips */
if (pages == 0xFF) {
return stm32_send_command(stm, 0xFF);
} else {
uint8_t cs = 0;
uint8_t pg_num;
stm32_send_byte(stm, pages-1);
cs ^= (pages-1);
for (pg_num = spage; pg_num < (pages + spage); pg_num++) {
stm32_send_byte(stm, pg_num);
cs ^= pg_num;
}
stm32_send_byte(stm, cs);
return stm32_read_byte(stm) == STM32_ACK;
}
}
char stm32_go(const stm32_t *stm, uint32_t address) {
uint8_t cs;
address = be_u32 (address);
cs = stm32_gen_cs(address);
if (!stm32_send_command(stm, stm->cmd->go)) return 0;
serial_write(stm->serial, &address, 4);
serial_write(stm->serial, &cs , 1);
return stm32_read_byte(stm) == STM32_ACK;
}
char stm32_reset_device(const stm32_t *stm) {
/*
since the bootloader does not have a reset command, we
upload the stmreset program into ram and run it, which
resets the device for us
*/
uint32_t length = stmreset_length;
unsigned char* pos = stmreset_binary;
uint32_t address = stm->dev->ram_start;
while(length > 0) {
uint32_t w = length > 256 ? 256 : length;
if (!stm32_write_memory(stm, address, pos, w))
return 0;
address += w;
pos += w;
length -= w;
}
return stm32_go(stm, stm->dev->ram_start);
}