-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathmain.c
More file actions
538 lines (475 loc) · 18.5 KB
/
main.c
File metadata and controls
538 lines (475 loc) · 18.5 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include "rpieepromab.h"
#define PARTITION_NUM_TO_STRING(P) (P == RPI_EEPROM_AB_PARTITION_A ? "A" : (P == RPI_EEPROM_AB_PARTITION_B ? "B" : "Unknown"))
/* Print the usage message */
static void usage(const char *progname, int exit_status) {
fprintf(stderr,
"Usage: %s <command> [args]\n"
"\n"
"This application provides a command line interface to update the Raspberry Pi\n"
"AB EEPROM partitions.\n"
"\n"
"Commands:\n"
" update <update.bin> Update the opposite partition of the EEPROM\n"
" with the contents of the file\n"
" read <out.bin> Read the current AB partition and write the\n"
" contents to a file\n"
" dump <out.bin> Read the entire EEPROM and write the\n"
" contents to a file\n"
" update-status Get the status of the EEPROM update and any\n"
" error codes\n"
" partition Get the current AB partition select of the\n"
" EEPROM\n"
" mark-partition-valid <hash> Mark the AB partition thats not committed as\n"
" valid if hash matches the calculated hash of\n"
" the partition\n"
" revert-to-committed <hash> Mark the committed AB partition as valid\n"
" again to stop a valid uncommitted partition\n"
" from being used by tryboot. Hash must match\n"
" the calculated hash of the committed partition\n"
" tryboot Get the current value of tryboot\n"
" tryboot <tryboot> Set the value of tryboot to 0 or 1\n"
" committed Get whether the current AB partition is\n"
" committed\n"
" commit Commit the current AB partition\n"
" force-commit-opposite Force commit the opposite partition\n"
" (use with caution)\n"
" partition-status Get the committed and valid partition\n"
" selections and their hashes\n"
" status-at-boot Get the partition used at boot and the\n"
" committed status at boot\n"
" help Show this help message\n"
" version Show version information\n",
progname);
exit(exit_status);
}
/* Convert a hex string to a binary array */
static int hex2bin(const char *hexstr, uint8_t *bin, size_t bin_len) {
size_t hex_len;
size_t i;
if (!hexstr || !bin) {
return -1;
}
hex_len = strlen(hexstr);
if (hex_len != bin_len * 2) {
return -1;
}
for (i = 0; i < bin_len; i++) {
if (sscanf(hexstr + i * 2, "%2hhx", &bin[i]) != 1) {
return -1;
}
}
return 0;
}
static int cmd_write_eeprom_update(int argc, char *argv[]) {
RPI_EEPROM_AB_ERROR err;
const char *update_filename = NULL;
FILE *f;
uint8_t update_data[RPI_EEPROM_AB_PARTITION_SIZE];
long file_size;
if (argc < 3) {
return -1;
}
update_filename = argv[2];
if (!update_filename) {
printf("Update file is required\n");
usage(argv[0], 1);
return -1;
}
f = fopen(update_filename, "rb");
if (!f) {
printf("Failed to read file: %s\n", update_filename);
return -1;
}
if (fseek(f, 0, SEEK_END) != 0) {
printf("Failed to seek to end of file: %s\n", update_filename);
return -1;
}
file_size = ftell(f);
if (file_size < 0) {
printf("Failed to get file size: %s\n", update_filename);
return -1;
}
printf("file_size: %ld\n", file_size);
if (file_size != RPI_EEPROM_AB_PARTITION_SIZE) {
printf("File size is not a valid AB update size: %ld\n", file_size);
return -1;
}
rewind(f);
if (fread(update_data, 1, file_size, f) != (size_t) file_size) {
printf("Failed to read file: %s\n", update_filename);
return -1;
}
fclose(f);
// Cancel any existing update
err = rpi_eeprom_ab_update_cancel();
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to cancel existing update: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
err = rpi_eeprom_ab_write_eeprom_update(update_data, file_size, 1);
printf("\n");
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
if (err == RPI_EEPROM_AB_ERROR_BUSY) {
printf("Failed to write update. EEPROM is busy.\n");
return -1;
} else if (err == RPI_EEPROM_AB_ERROR_UNCOMMITTED) {
printf("Failed to write update. Cannot write from an uncommitted partition.\n");
return -1;
}
printf("Failed to write update: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
RPI_EEPROM_AB_UPDATE_RC_STATUS status;
RPI_EEPROM_AB_ERROR firmware_error;
uint32_t _spi_gpio_check, _using_partitioning;
err = rpi_eeprom_ab_update_get_status(&status, &firmware_error,
&_spi_gpio_check, &_using_partitioning);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR){
printf("Failed to get EEPROM update status: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
} else {
if (status == RPI_EEPROM_AB_UPDATE_RC_SUCCSESS) {
printf("Write to EEPROM completed\n");
} else if (status == RPI_EEPROM_AB_UPDATE_RC_BUSY) {
printf("Write to EEPROM did not complete\n");
return -1;
} else {
printf("EEPROM update firmware error: %s\n",
rpi_eeprom_ab_update_strerror(firmware_error));
return -1;
}
}
return 0;
}
static int cmd_eeprom_dump(int argc, char *argv[]) {
RPI_EEPROM_AB_ERROR err;
const char *outfile = NULL;
FILE *f;
uint8_t data[RPI_EEPROM_CAPACITY];
if (argc < 3) {
return -1;
}
outfile = argv[2];
if (!outfile) {
usage(argv[0], 1);
return -1;
}
f = fopen(outfile, "wb");
if (!f) {
printf("Failed to open file: %s\n", outfile);
return -1;
}
err = rpi_eeprom_ab_read_entire_eeprom(data);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to read from EEPROM: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
if (fwrite(data, 1, RPI_EEPROM_CAPACITY, f) != RPI_EEPROM_CAPACITY) {
printf("Failed to write to file: %s\n", outfile);
return -1;
}
fclose(f);
printf("EEPROM dump completed\n");
return 0;
}
static int cmd_eeprom_read_partition(int argc, char *argv[]) {
RPI_EEPROM_AB_ERROR err;
const char *outfile = NULL;
FILE *f;
uint8_t data[RPI_EEPROM_AB_PARTITION_SIZE];
RPI_EEPROM_AB_PARTITION partition;
if (argc < 3) {
return -1;
}
outfile = argv[2];
if (!outfile) {
usage(argv[0], 1);
return -1;
}
f = fopen(outfile, "wb");
if (!f) {
printf("Failed to open file: %s\n", outfile);
return -1;
}
err = rpi_eeprom_ab_update_get_current_partition(&partition);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get AB partition: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
err = rpi_eeprom_ab_read_eeprom_partition(partition, data);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to read from EEPROM: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
if (fwrite(data, 1, RPI_EEPROM_AB_PARTITION_SIZE, f) != RPI_EEPROM_AB_PARTITION_SIZE) {
printf("Failed to write to file: %s\n", outfile);
return -1;
}
fclose(f);
printf("EEPROM partition read completed\n");
return 0;
}
int main(int argc, char *argv[]) {
RPI_EEPROM_AB_ERROR err;
if (argc < 2) {
usage(argv[0], 1);
}
if (strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "help") == 0) {
usage(argv[0], 0);
return 0;
} else if (strcmp(argv[1], "--version") == 0 ||
strcmp(argv[1], "-v") == 0 ||
strcmp(argv[1], "version") == 0) {
printf("rpi-eeprom-ab %s\n", VERSION_STRING);
printf("librpieepromab %s\n", rpi_eeprom_ab_version());
return 0;
} else if (strcmp(argv[1], "update") == 0) {
// Update the EEPROM partition with the contents of the file
if (argc < 3) {
usage(argv[0], 1);
return -1;
}
if (cmd_write_eeprom_update(argc, argv) < 0) {
return -1;
}
return 0;
} else if (strcmp(argv[1], "dump") == 0) {
// Dump the entire EEPROM to a file
if (argc < 3) {
usage(argv[0], 1);
return -1;
}
if (cmd_eeprom_dump(argc, argv) < 0) {
return -1;
}
return 0;
} else if (strcmp(argv[1], "read") == 0) {
// Read the current partition from the EEPROM and write the contents to a file
if (argc < 3) {
usage(argv[0], 1);
return -1;
}
if (cmd_eeprom_read_partition(argc, argv) < 0) {
return -1;
}
} else if (strcmp(argv[1], "update-status") == 0) {
// Get the status of the firmware update of the EEPROM
RPI_EEPROM_AB_UPDATE_RC_STATUS status;
RPI_EEPROM_AB_ERROR firmware_error;
uint32_t _spi_gpio_check, _using_partitioning;
err = rpi_eeprom_ab_update_get_status(&status, &firmware_error,
&_spi_gpio_check, &_using_partitioning);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get EEPROM update status: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("EEPROM update status: %s\n", rpi_eeprom_ab_update_strstatus(status));
if (firmware_error != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("EEPROM update firmware error: %s\n",
rpi_eeprom_ab_update_strerror(firmware_error));
}
return 0;
} else if (strcmp(argv[1], "spi-check") == 0) {
// Check if the firmware can access SPI EEPROM
uint32_t spi_gpio_check;
err = rpi_eeprom_ab_update_get_spi_check(&spi_gpio_check);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get SPI check: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
if (spi_gpio_check == 1) {
printf("SPI check: OK\n");
return 0;
} else {
printf("SPI check: Failed\n");
return -1;
}
} else if (strcmp(argv[1], "partition") == 0) {
RPI_EEPROM_AB_PARTITION partition;
if (argc == 2) {
err = rpi_eeprom_ab_update_get_current_partition(&partition);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get EEPROM AB partition: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("%s\n", PARTITION_NUM_TO_STRING(partition));
return 0;
} else {
usage(argv[0], 1);
return -1;
}
} else if (strcmp(argv[1], "mark-partition-valid") == 0) {
// Mark the uncommitted partition as valid
if (argc == 3) {
RPI_EEPROM_RELATIVE_PARTITION relative_partition;
uint8_t hash[32];
uint32_t committed;
err = rpi_eeprom_ab_update_get_current_committed(&committed);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
if (committed == 1) {
relative_partition = RPI_EEPROM_AB_OPPOSITE_PARTITION;
} else {
printf("Can't mark a partition as valid from an uncommitted partition.\n");
return -1;
}
// Convert the hex string to a uint8_t array
if (hex2bin(argv[2], hash, sizeof(hash)) != 0) {
printf("Invalid hash string\n");
return -1;
}
err = rpi_eeprom_ab_update_set_partition(relative_partition, hash);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to set EEPROM AB partition: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("Next EEPROM AB partition marked valid\n");
return 0;
} else {
usage(argv[0], 1);
return -1;
}
} else if (strcmp(argv[1], "revert-to-committed") == 0) {
// Mark the committed partition as the latest valid partition to
// overwrite any previous attempt to mark the uncommitted partition valid
if (argc == 3) {
RPI_EEPROM_RELATIVE_PARTITION relative_partition;
uint32_t committed;
err = rpi_eeprom_ab_update_get_current_committed(&committed);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
if (committed == 1) {
relative_partition = RPI_EEPROM_AB_CURRENT_PARTITION;
} else {
relative_partition = RPI_EEPROM_AB_OPPOSITE_PARTITION;
}
uint8_t hash[32];
// Convert the hex string to a uint8_t array
if (hex2bin(argv[2], hash, sizeof(hash)) != 0) {
printf("Invalid hash string\n");
return -1;
}
err = rpi_eeprom_ab_update_set_partition(relative_partition, hash);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to set EEPROM AB partition: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("Reverted to uncommitted valid partition\n");
return 0;
} else {
usage(argv[0], 1);
return -1;
}
} else if (strcmp(argv[1], "tryboot") == 0) {
uint32_t tryboot;
if (argc == 2) {
err = rpi_eeprom_ab_update_get_current_tryboot(&tryboot);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get EEPROM tryboot: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("%d\n", (int) tryboot);
return 0;
} else if (argc == 3) {
tryboot = atoi(argv[2]);
err = rpi_eeprom_ab_update_set_tryboot(tryboot);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to set EEPROM tryboot: %s\n", rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("EEPROM tryboot set to: %d\n", (int) tryboot);
return 0;
} else {
usage(argv[0], 1);
return -1;
}
} else if (strcmp(argv[1], "committed") == 0) {
uint32_t committed;
err = rpi_eeprom_ab_update_get_current_committed(&committed);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get EEPROM AB committed: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("%d\n", (int) committed);
return 0;
} else if (strcmp(argv[1], "commit") == 0) {
if (argc < 2) {
usage(argv[0], 1);
return -1;
}
err = rpi_eeprom_ab_update_commit_current_partition();
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to commit EEPROM update: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("Committed current EEPROM partition\n");
return 0;
} else if (strcmp(argv[1], "force-commit-opposite") == 0) {
err = rpi_eeprom_ab_update_force_commit_opposite();
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to commit EEPROM update: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("Force committed opposite EEPROM partition\n");
return 0;
} else if (strcmp(argv[1], "partition-status") == 0) {
RPI_EEPROM_AB_PARTITION committed_partition, valid_partition;
uint8_t committed_partition_hash[32], valid_partition_hash[32];
err = rpi_eeprom_ab_update_get_eeprom_partition(&committed_partition,
&valid_partition, committed_partition_hash, valid_partition_hash);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get partition status: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("EEPROM committed partition: %s\n", PARTITION_NUM_TO_STRING(committed_partition));
printf("EEPROM valid partition: %s\n", PARTITION_NUM_TO_STRING(valid_partition));
printf("EEPROM committed partition hash: ");
for (int i = 0; i < 32; i++) {
printf("%02x", committed_partition_hash[i]);
}
printf("\n");
printf("EEPROM valid partition hash: ");
for (int i = 0; i < 32; i++) {
printf("%02x", valid_partition_hash[i]);
}
printf("\n");
return 0;
} else if (strcmp(argv[1], "status-at-boot") == 0) {
RPI_EEPROM_AB_PARTITION partition_at_boot;
uint32_t committed_at_boot;
err = rpi_eeprom_ab_update_get_boot_partition(&partition_at_boot, &committed_at_boot);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
printf("Failed to get EEPROM status at boot: %s\n",
rpi_eeprom_ab_update_strerror(err));
return -1;
}
printf("EEPROM partition used at boot: %s\n", PARTITION_NUM_TO_STRING(partition_at_boot));
printf("EEPROM committed status at boot: %d\n", committed_at_boot);
return 0;
} else {
printf("Invalid command: %s\n", argv[1]);
usage(argv[0], 1);
return -1;
}
}