forked from joepasquariello/FlasherX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFXUtil.cpp
278 lines (249 loc) · 9.88 KB
/
FXUtil.cpp
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
//******************************************************************************
// FXUTIL.H -- FlasherX utility functions
//******************************************************************************
#include <Arduino.h>
extern "C" {
#include "FlashTxx.h" // TLC/T3x/T4x/TMM flash primitives
}
//******************************************************************************
// hex_info_t struct for hex record and hex file info
//******************************************************************************
typedef struct { //
char *data; // pointer to array allocated elsewhere
unsigned int addr; // address in intel hex record
unsigned int code; // intel hex record type (0=data, etc.)
unsigned int num; // number of data bytes in intel hex record
uint32_t base; // base address to be added to intel hex 16-bit addr
uint32_t min; // min address in hex file
uint32_t max; // max address in hex file
int eof; // set true on intel hex EOF (code = 1)
int lines; // number of hex records received
} hex_info_t;
//******************************************************************************
// hex_info_t struct for hex record and hex file info
//******************************************************************************
void read_ascii_line( Stream *serial, char *line, int maxbytes );
int parse_hex_line( const char *theline, char *bytes,
unsigned int *addr, unsigned int *num, unsigned int *code );
int process_hex_record( hex_info_t *hex );
void update_firmware( Stream *in, Stream *out,
uint32_t buffer_addr, uint32_t buffer_size );
//******************************************************************************
// update_firmware() read hex file and write new firmware to program flash
//******************************************************************************
void update_firmware( Stream *in, Stream *out,
uint32_t buffer_addr, uint32_t buffer_size )
{
static char line[96]; // buffer for hex lines
static char data[32] __attribute__ ((aligned (8))); // buffer for hex data
hex_info_t hex = { // intel hex info struct
data, 0, 0, 0, // data,addr,num,code
0, 0xFFFFFFFF, 0, // base,min,max,
0, 0 // eof,lines
};
out->printf( "reading hex lines...\n" );
// read and process intel hex lines until EOF or error
while (!hex.eof) {
read_ascii_line( in, line, sizeof(line) );
// reliability of transfer via USB is improved by this printf/flush
if (in == out && out == (Stream*)&Serial) {
out->printf( "%s\n", line );
out->flush();
}
if (parse_hex_line( (const char*)line, hex.data, &hex.addr, &hex.num, &hex.code ) == 0) {
out->printf( "abort - bad hex line %s\n", line );
return;
}
else if (process_hex_record( &hex ) != 0) { // error on bad hex code
out->printf( "abort - invalid hex code %d\n", hex.code );
return;
}
else if (hex.code == 0) { // if data record
uint32_t addr = buffer_addr + hex.base + hex.addr - FLASH_BASE_ADDR;
if (hex.max > (FLASH_BASE_ADDR + buffer_size)) {
out->printf( "abort - max address %08lX too large\n", hex.max );
return;
}
else if (!IN_FLASH(buffer_addr)) {
memcpy( (void*)addr, (void*)hex.data, hex.num );
}
else if (IN_FLASH(buffer_addr)) {
int error = flash_write_block( addr, hex.data, hex.num );
if (error) {
out->printf( "abort - error %02X in flash_write_block()\n", error );
return;
}
}
}
hex.lines++;
}
out->printf( "\nhex file: %1d lines %1lu bytes (%08lX - %08lX)\n",
hex.lines, hex.max-hex.min, hex.min, hex.max );
// check FSEC value in new code -- abort if incorrect
#if defined(KINETISK) || defined(KINETISL)
uint32_t value = *(uint32_t *)(0x40C + buffer_addr);
if (value == 0xfffff9de) {
out->printf( "new code contains correct FSEC value %08lX\n", value );
}
else {
out->printf( "abort - FSEC value %08lX should be FFFFF9DE\n", value );
return;
}
#endif
// check FLASH_ID in new code - abort if not found
if (check_flash_id( buffer_addr, hex.max - hex.min )) {
out->printf( "new code contains correct target ID %s\n", FLASH_ID );
}
else {
out->printf( "abort - new code missing string %s\n", FLASH_ID );
return;
}
// get user input to write to flash or abort
int user_lines = -1;
while (user_lines != hex.lines && user_lines != 0) {
out->printf( "enter %d to flash or 0 to abort\n", hex.lines );
// Changed the next line to read from "in", the same place image was read.
// This allows "out" to be used for debug output if needed.
read_ascii_line( in, line, sizeof(line) );
sscanf( line, "%d", &user_lines );
}
if (user_lines == 0) {
out->printf( "abort - user entered 0 lines\n" );
return;
}
else {
out->printf( "calling flash_move() to load new firmware...\n" );
out->flush();
}
// move new program from buffer to flash, free buffer, and reboot
flash_move( FLASH_BASE_ADDR, buffer_addr, hex.max-hex.min );
// should not return from flash_move(), but put REBOOT here as reminder
REBOOT;
}
//******************************************************************************
// read_ascii_line() read ascii characters until '\n', '\r', or max bytes
//******************************************************************************
void read_ascii_line( Stream *serial, char *line, int maxbytes )
{
int c=0, nchar=0;
while (serial->available()) {
c = serial->read();
if (c == '\n' || c == '\r')
continue;
else {
line[nchar++] = c;
break;
}
}
while (nchar < maxbytes && !(c == '\n' || c == '\r')) {
if (serial->available()) {
c = serial->read();
line[nchar++] = c;
}
}
line[nchar-1] = 0; // null-terminate
}
//******************************************************************************
// process_hex_record() process record and return okay (0) or error (1)
//******************************************************************************
int process_hex_record( hex_info_t *hex )
{
if (hex->code==0) { // data -- update min/max address so far
if (hex->base + hex->addr + hex->num > hex->max)
hex->max = hex->base + hex->addr + hex->num;
if (hex->base + hex->addr < hex->min)
hex->min = hex->base + hex->addr;
}
else if (hex->code==1) { // EOF (:flash command not received yet)
hex->eof = 1;
}
else if (hex->code==2) { // extended segment address (top 16 of 24-bit addr)
hex->base = ((hex->data[0] << 8) | hex->data[1]) << 4;
}
else if (hex->code==3) { // start segment address (80x86 real mode only)
return 1;
}
else if (hex->code==4) { // extended linear address (top 16 of 32-bit addr)
hex->base = ((hex->data[0] << 8) | hex->data[1]) << 16;
}
else if (hex->code==5) { // start linear address (32-bit big endian addr)
hex->base = (hex->data[0] << 24) | (hex->data[1] << 16)
| (hex->data[2] << 8) | (hex->data[3] << 0);
}
else {
return 1;
}
return 0;
}
//******************************************************************************
// Intel Hex record foramt:
//
// Start code: one character, ASCII colon ':'.
// Byte count: two hex digits, number of bytes (hex digit pairs) in data field.
// Address: four hex digits
// Record type: two hex digits, 00 to 05, defining the meaning of the data field.
// Data: n bytes of data represented by 2n hex digits.
// Checksum: two hex digits, computed value used to verify record has no errors.
//
// Examples:
// :10 9D30 00 711F0000AD38000005390000F5460000 35
// :04 9D40 00 01480000 D6
// :00 0000 01 FF
//******************************************************************************
/* Intel HEX read/write functions, Paul Stoffregen, [email protected] */
/* This code is in the public domain. Please retain my name and */
/* email address in distributed copies, and let me know about any bugs */
/* I, Paul Stoffregen, give no warranty, expressed or implied for */
/* this software and/or documentation provided, including, without */
/* limitation, warranty of merchantability and fitness for a */
/* particular purpose. */
// type modifications by Jon Zeeff
/* parses a line of intel hex code, stores the data in bytes[] */
/* and the beginning address in addr, and returns a 1 if the */
/* line was valid, or a 0 if an error occured. The variable */
/* num gets the number of bytes that were stored into bytes[] */
#include <stdio.h> // sscanf(), etc.
#include <string.h> // strlen(), etc.
int parse_hex_line( const char *theline, char *bytes,
unsigned int *addr, unsigned int *num, unsigned int *code )
{
unsigned sum, len, cksum;
const char *ptr;
int temp;
*num = 0;
delay(2); // Delay a short moment to allow for `theline` to be saved before accessing it... Don't believe this is necessary? Remove this line and see what happens...
if (theline[0] != ':')
return 0;
if (strlen (theline) < 11)
return 0;
ptr = theline + 1;
if (!sscanf (ptr, "%02x", &len))
return 0;
ptr += 2;
if (strlen (theline) < (11 + (len * 2)))
return 0;
if (!sscanf (ptr, "%04x", (unsigned int *)addr))
return 0;
ptr += 4;
/* Serial.printf("Line: length=%d Addr=%d\n", len, *addr); */
if (!sscanf (ptr, "%02x", code))
return 0;
ptr += 2;
sum = (len & 255) + ((*addr >> 8) & 255) + (*addr & 255) + (*code & 255);
while (*num != len)
{
if (!sscanf (ptr, "%02x", &temp))
return 0;
bytes[*num] = temp;
ptr += 2;
sum += bytes[*num] & 255;
(*num)++;
if (*num >= 256)
return 0;
}
if (!sscanf (ptr, "%02x", &cksum))
return 0;
if (((sum & 255) + (cksum & 255)) & 255)
return 0; /* checksum error */
return 1;
}