-
Notifications
You must be signed in to change notification settings - Fork 28
/
ota.c
459 lines (367 loc) · 12.2 KB
/
ota.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "ota.h"
#include "util.h"
#include "config.h"
#include "rboot-interface.h"
#include "display.h"
#include "sdk.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
static app_action_t flash_all_finish(string_t *dst,
SpiFlashOpResult result, unsigned int sector,
const char *tag, const char *action)
{
const char *cause = (const char *)0;
if(result == SPI_FLASH_RESULT_OK)
cause = (const char *)0;
else
if(result == SPI_FLASH_RESULT_ERR)
cause = "error";
else
if(result == SPI_FLASH_RESULT_TIMEOUT)
cause = "timeout";
else
cause = "unknown";
if(cause)
{
string_clear(dst);
string_format(dst, "ERROR: %s: %s flash %s at 0x%x\n",
tag, action, cause, sector);
return(app_action_error);
}
return(app_action_normal);
}
static app_action_t flash_read(unsigned int sector, void *dst, const char *tag, string_t *str_dst)
{
SpiFlashOpResult result;
result = spi_flash_read(sector * SPI_FLASH_SEC_SIZE, dst, SPI_FLASH_SEC_SIZE);
return(flash_all_finish(str_dst, result, sector, tag, "read"));
}
static app_action_t flash_write(unsigned int sector, const void *src, const char *tag, string_t *str_dst)
{
SpiFlashOpResult result;
result = spi_flash_write(sector * SPI_FLASH_SEC_SIZE, src, SPI_FLASH_SEC_SIZE);
return(flash_all_finish(str_dst, result, sector, tag, "write"));
}
static app_action_t flash_erase(unsigned int sector, const char *tag, string_t *str_dst)
{
SpiFlashOpResult result;
result = spi_flash_erase_sector(sector);
return(flash_all_finish(str_dst, result, sector, tag, "erase"));
}
app_action_t application_function_flash_info(app_params_t *parameters)
{
unsigned int ota_slots = 0;
unsigned int ota_slot = 0;
unsigned int ota_address_0 = 0;
unsigned int ota_address_1 = 0;
rboot_if_config_t config;
rboot_if_rtc_config_t rtc;
unsigned int x, y;
display_pixel_mode_t pixel_mode;
display_info_t info;
if(display_get_info(&info))
{
x = info.width;
y = info.height;
pixel_mode = info.pixel_mode;
}
else
{
x = 0;
y = 0;
pixel_mode = display_pixel_mode_none;
}
if(!rboot_if_read_config(&config))
{
string_append(parameters->dst, "ERROR rboot config invalid\n");
return(app_action_error);
}
ota_slots = config.slot_count;
ota_slot = config.slot_current;
ota_address_0 = config.slots[0];
ota_address_1 = config.slots[1];
if(rboot_if_read_rtc_ram(&rtc))
ota_slot = rtc.last_slot;
string_format(parameters->dst, "OK flash function available, "
"slots: %u, "
"current: %u, "
"sectors: [ %u, %u ], "
"display: %ux%upx@%u\n",
ota_slots,
ota_slot,
ota_address_0 / SPI_FLASH_SEC_SIZE, ota_address_1 / SPI_FLASH_SEC_SIZE,
x, y, pixel_mode);
return(app_action_normal);
}
app_action_t application_function_flash_read(app_params_t *parameters)
{
unsigned int sector, pad_offset, oob_offset;
uint8_t *sector_dst;
app_action_t rv;
if(string_size(parameters->dst) < (int)(SPI_FLASH_SEC_SIZE + 64))
{
string_format(parameters->dst, "ERROR flash-read: flash sector buffer too small: %d\n", string_size(parameters->dst));
return(app_action_error);
}
if(parse_uint(1, parameters->src, §or, 0, ' ') != parse_ok)
{
string_append(parameters->dst, "ERROR flash-read: requesting sector required\n");
return(app_action_error);
}
string_format(parameters->dst, "OK flash-read: read sector %u\n", sector);
oob_offset = pad_offset = string_length(parameters->dst);
sector_dst = (uint8_t *)string_buffer_nonconst(parameters->dst) + pad_offset;
while((oob_offset % 4 != 0))
{
*sector_dst++ = '\0';
oob_offset++;
}
if((rv = flash_read(sector, sector_dst, "flash read", parameters->dst)) != app_action_normal)
return(rv);
string_setlength(parameters->dst, oob_offset + SPI_FLASH_SEC_SIZE);
parameters->dst_data_pad_offset = pad_offset;
parameters->dst_data_oob_offset = oob_offset;
return(app_action_normal);
}
app_action_t application_function_flash_write(app_params_t *parameters)
{
unsigned int mode;
unsigned int sector;
unsigned int word;
const unsigned int *wordptr;
int same;
int erase;
uint8_t *old;
const uint8_t *new;
app_action_t rv;
if(string_size(parameters->dst) < SPI_FLASH_SEC_SIZE)
{
string_format(parameters->dst, "ERROR flash-write: dst buffer too small: %d\n", string_size(parameters->dst));
return(app_action_error);
}
if(parse_uint(1, parameters->src, &mode, 0, ' ') != parse_ok)
{
string_append(parameters->dst, "ERROR flash-write: mode required (0 = simulate, 1 = write)\n");
return(app_action_error);
}
if(parse_uint(2, parameters->src, §or, 0, ' ') != parse_ok)
{
string_append(parameters->dst, "ERROR flash-write: sector required\n");
return(app_action_error);
}
if(string_length(parameters->src_oob) != SPI_FLASH_SEC_SIZE)
{
string_format(parameters->dst, "ERROR flash-write: flash sector data length mismatch: %d != %d\n", string_length(parameters->src_oob), SPI_FLASH_SEC_SIZE);
return(app_action_error);
}
old = (uint8_t *)string_buffer_nonconst(parameters->dst);
new = (const uint8_t *)string_buffer(parameters->src_oob);
if((rv = flash_read(sector, old, "flash_write", parameters->dst)) != app_action_normal)
return(rv);
same = !memory_compare(SPI_FLASH_SEC_SIZE, old, new);
erase = 0;
if(!same)
{
for(word = 0, wordptr = (const unsigned int *)(const void *)old; word < (SPI_FLASH_SEC_SIZE / 4); word++, wordptr++)
{
if(*wordptr != 0xffffffffUL)
{
erase = 1;
break;
}
}
if(mode == 1)
{
if(erase && ((rv = flash_erase(sector, "flash_write", parameters->dst)) != app_action_normal))
return(rv);
if((rv = flash_write(sector, new, "flash_write", parameters->dst)) != app_action_normal)
return(rv);
}
}
string_clear(parameters->dst);
string_format(parameters->dst, "OK flash-write: written mode %u, sector %u, same %d, erased %d\n", mode, sector, same, erase);
return(app_action_normal);
}
app_action_t application_function_flash_checksum(app_params_t *parameters)
{
app_action_t result;
unsigned int sector, sectors, current, done;
char *buffer;
SHA_CTX sha_context;
unsigned char sha_result[SHA_DIGEST_LENGTH];
string_new(, sha_string, SHA_DIGEST_LENGTH * 2 + 2);
if(string_size(parameters->dst) < SPI_FLASH_SEC_SIZE)
{
string_format(parameters->dst, "ERROR flash-verify: dst buffer too small: %d\n", string_size(parameters->dst));
return(app_action_error);
}
if(parse_uint(1, parameters->src, §or, 0, ' ') != parse_ok)
{
string_append(parameters->dst, "ERROR flash-checksum: start sector required\n");
return(app_action_error);
}
if(parse_uint(2, parameters->src, §ors, 0, ' ') != parse_ok)
{
string_append(parameters->dst, "ERROR flash-checksum: length (sectors) required\n");
return(app_action_error);
}
SHA1Init(&sha_context);
buffer = string_buffer_nonconst(parameters->dst);
for(current = sector, done = 0; done < sectors; current++, done++)
{
if((result = flash_read(current, buffer, "flash_checksum", parameters->dst)) != app_action_normal)
return(result);
SHA1Update(&sha_context, buffer, SPI_FLASH_SEC_SIZE);
}
SHA1Final(sha_result, &sha_context);
string_bin_to_hex(&sha_string, sha_result, SHA_DIGEST_LENGTH);
string_clear(parameters->dst);
string_format(parameters->dst, "OK flash-checksum: checksummed %u sectors from sector %u, checksum: ", done, sector);
string_append_string(parameters->dst, &sha_string);
string_append(parameters->dst, "\n");
return(app_action_normal);
}
app_action_t application_function_flash_bench(app_params_t *parameters)
{
unsigned int bytes, pad_offset, oob_offset;
uint8_t *sector_dst;
if(string_size(parameters->dst) < SPI_FLASH_SEC_SIZE)
{
string_format(parameters->dst, "ERROR flash-bench: dst buffer too small: %d\n", string_size(parameters->dst));
return(app_action_error);
}
if(parse_uint(1, parameters->src, &bytes, 0, ' ') != parse_ok)
{
string_append(parameters->dst, "ERROR flash-bench: length (bytes) required\n");
return(app_action_error);
}
if(bytes > SPI_FLASH_SEC_SIZE)
{
string_append(parameters->dst, "ERROR flash-bench: length should be <= 4096\n");
return(app_action_error);
}
string_format(parameters->dst, "OK flash-bench: sending %u bytes\n", bytes);
if(bytes > 0)
{
oob_offset = pad_offset = string_length(parameters->dst);
sector_dst = (uint8_t *)string_buffer_nonconst(parameters->dst) + pad_offset;
while((oob_offset % 4) != 0)
{
*sector_dst++ = '\0';
oob_offset++;
}
memset(sector_dst, 0xff, bytes);
string_setlength(parameters->dst, oob_offset + bytes);
}
else
oob_offset = pad_offset = 0;
parameters->dst_data_pad_offset = pad_offset;
parameters->dst_data_oob_offset = oob_offset;
return(app_action_normal);
}
app_action_t application_function_flash_select(app_params_t *parameters)
{
unsigned int permanent, slot;
rboot_if_config_t config;
rboot_if_rtc_config_t rtc;
if(!rboot_if_read_config(&config))
{
string_append(parameters->dst, "ERROR flash-select: rboot config invalid\n");
return(app_action_error);
}
if(parse_uint(1, parameters->src, &slot, 0, ' ') != parse_ok)
{
string_append(parameters->dst, "ERROR flash-select: slot required\n");
return(app_action_error);
}
if(slot >= config.slot_count)
{
string_format(parameters->dst, "ERROR flash-select: invalid slot, valid range = 0 - %d\n", config.slot_count - 1);
return(app_action_error);
}
if(((parse_uint(2, parameters->src, &permanent, 0, ' ') != parse_ok)) || ((permanent != 0) && (permanent != 1)))
{
string_format(parameters->dst, "ERROR flash-select: mode temporary (=0) or permanent (=1) required, supplied: %u\n", permanent);
return(app_action_error);
}
if(!rboot_if_read_rtc_ram(&rtc))
{
rtc.magic = rboot_if_rtc_magic;
rtc.next_mode = rboot_if_conf_mode_standard;
rtc.last_mode = rboot_if_conf_mode_standard;
rtc.last_slot = config.slot_current;
rtc.temporary_slot= slot;
if(!rboot_if_write_rtc_ram(&rtc))
{
string_append(parameters->dst, "ERROR: flash-select: RTC RAM config signature absent and can't create a new one\n");
return(app_action_error);
}
}
if(!rboot_if_read_rtc_ram(&rtc))
{
string_append(parameters->dst, "ERROR: flash-select: write initial data to RTC RAM failed\n");
return(app_action_error);
}
if(rboot_if_mapped_slot() != rtc.last_slot)
{
string_format(parameters->dst, "ERROR flash-select: current slot according to rboot RTC RAM info does not match flash memory map: %u vs. %u\n",
config.slot_current, rboot_if_mapped_slot());
return(app_action_error);
}
rtc.next_mode = permanent ? rboot_if_conf_mode_standard : rboot_if_conf_mode_temp_rom;
rtc.temporary_slot = slot;
if(!rboot_if_write_rtc_ram(&rtc))
{
string_append(parameters->dst, "ERROR: flash-select: write data to RTC RAM failed\n");
return(app_action_error);
}
if(!rboot_if_read_rtc_ram(&rtc))
{
string_append(parameters->dst, "ERROR: flash-select: verify RTC data failed\n");
return(app_action_error);
}
if(rtc.next_mode != (permanent ? rboot_if_conf_mode_standard : rboot_if_conf_mode_temp_rom))
{
string_format(parameters->dst, "ERROR: flash-select: RTC data invalid, next boot mode: %s\n", rboot_if_boot_mode(rtc.next_mode));
return(app_action_error);
}
if(rtc.temporary_slot != slot)
{
string_format(parameters->dst, "ERROR: flash-select: RTC data invalid, next boot slot: %x\n", rtc.temporary_slot);
return(app_action_error);
}
slot = rtc.temporary_slot;
if(permanent)
{
bool success;
if(!rboot_if_read_config(&config))
{
string_append(parameters->dst, "ERROR flash-select: rboot config invalid\n");
return(app_action_error);
}
config.slot_current = slot;
string_clear(parameters->dst);
success = rboot_if_write_config(&config);
string_clear(parameters->dst);
if(!success)
{
string_append(parameters->dst, "ERROR flash-select: update rboot config failed\n");
return(app_action_error);
}
if(!rboot_if_read_config(&config))
{
string_append(parameters->dst, "ERROR flash-select: rboot config invalid after update\n");
return(app_action_error);
}
if(config.slot_current != slot)
{
string_append(parameters->dst, "ERROR flash-select: slot not selected\n");
return(app_action_error);
}
slot = config.slot_current;
}
string_format(parameters->dst, "OK flash-select: slot %u selected, sector %u, permanent %u\n",
slot, config.slots[slot] / SPI_FLASH_SEC_SIZE, permanent);
return(app_action_normal);
}