-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathrpieepromab.c
More file actions
776 lines (659 loc) · 25.5 KB
/
rpieepromab.c
File metadata and controls
776 lines (659 loc) · 25.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include "rpieepromab.h"
#include <errno.h>
#define countof(x) ((int)(sizeof x/sizeof *x))
#define VCIO_IOC_MAGIC 100
#define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
#if 0
#define LOG_DEBUG(...) do { \
fprintf(stderr, __VA_ARGS__); \
} while (0)
#else
#define LOG_DEBUG(...) do { } while (0)
#endif
/* Mailbox channel for property interface */
#define MBOX_CHAN_PROPERTY 8
/* VideoCore mailbox error flag */
#define VC_MAILBOX_ERROR 0x80000000
/* AB EEPROM related mailbox tags */
typedef enum {
TAG_GET_EEPROM_PACKET = 0x00030096,
TAG_SET_EEPROM_PACKET = 0x00038096,
TAG_GET_EEPROM_UPDATE_STATUS = 0x00030097,
TAG_SET_EEPROM_UPDATE_STATUS = 0x00038097,
TAG_GET_EEPROM_PARTITION = 0x00030098,
TAG_SET_EEPROM_PARTITION = 0x00038098,
TAG_GET_EEPROM_AB_PARAMS = 0x00030099,
TAG_SET_EEPROM_AB_PARAMS = 0x00038099,
} RPI_EEPROM_AB_UPDATE_TAG;
/* Common header structure for firmware mailbox messages */
struct firmware_msg_header {
uint32_t buf_size;
uint32_t code;
uint32_t tag;
uint32_t tag_buf_size;
uint32_t tag_req_resp_size;
};
/* Standard message structure for firmware mailbox */
struct firmware_msg {
struct firmware_msg_header hdr;
uint32_t value[4]; /* Value buffer for request/response */
uint32_t end_tag;
};
/* Message structure to transfer EEPROM data to firmware mailbox */
struct firmware_update_packet_msg {
struct firmware_msg_header hdr;
union {
uint32_t address;
RPI_EEPROM_AB_ERROR error;
};
uint32_t length;
uint8_t data[RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE];
uint32_t end_tag;
};
/* Message structure to get the status of the EEPROM update from firmware mailbox */
struct firmware_update_get_status_msg {
struct firmware_msg_header hdr;
union {
RPI_EEPROM_AB_UPDATE_RC_STATUS status;
RPI_EEPROM_AB_ERROR error;
};
RPI_EEPROM_AB_ERROR firmware_error;
uint32_t spi_gpio_check;
uint32_t using_partitioning;
uint32_t end_tag;
};
/* Command codes */
typedef enum {
RPI_EEPROM_AB_UPDATE_CANCEL = 0,
RPI_EEPROM_AB_UPDATE_START_WRITE = 1, // Used by old beta firmware version 2025-02-11
RPI_EEPROM_AB_UPDATE_START_ERASE = 2,
} RPI_EEPROM_AB_UPDATE_COMMAND_CODE;
/* Message structure to send EEPROM update command to firmware mailbox */
struct firmware_update_command_msg {
struct firmware_msg_header hdr;
union {
RPI_EEPROM_AB_UPDATE_COMMAND_CODE command;
RPI_EEPROM_AB_ERROR error;
};
uint32_t end_tag;
};
struct firmware_update_get_eeprom_partition_msg {
struct firmware_msg_header hdr;
union {
RPI_EEPROM_AB_PARTITION committed_partition;
RPI_EEPROM_AB_ERROR error;
};
RPI_EEPROM_AB_PARTITION valid_partition;
uint8_t committed_partition_hash[32];
uint8_t valid_partition_hash[32];
uint32_t end_tag;
};
struct firmware_update_set_eeprom_partition_msg {
struct firmware_msg_header hdr;
union {
RPI_EEPROM_RELATIVE_PARTITION relative_partition;
RPI_EEPROM_AB_ERROR error;
};
uint8_t hash[32];
uint32_t end_tag;
};
/* Message structure to get all AB EEPROM parameters from firmware mailbox */
struct firmware_update_get_ab_params_msg {
struct firmware_msg_header hdr;
union {
RPI_EEPROM_AB_PARTITION partition;
RPI_EEPROM_AB_ERROR error;
};
uint32_t committed;
uint32_t tryboot;
RPI_EEPROM_AB_PARTITION partition_at_boot;
uint32_t committed_at_boot;
uint32_t end_tag;
};
/* Message structure to set individual AB EEPROM parameter to firmware mailbox */
struct firmware_update_set_ab_param_msg {
struct firmware_msg_header hdr;
union {
enum {
EEPROM_UPDATE_PARAM_COMMIT = 1,
EEPROM_UPDATE_PARAM_TRYBOOT,
} param;
RPI_EEPROM_AB_ERROR error;
};
union {
RPI_EEPROM_RELATIVE_PARTITION relative_partition_to_commit;
uint32_t tryboot_value;
} value;
uint32_t end_tag;
};
#define TAG_BUFFER_SIZE(S) (sizeof(S) - sizeof(struct firmware_msg_header) - sizeof(uint32_t))
static int mbox_open(void) {
int file_desc;
const char *devices[] = {"/dev/vcio_ab", "/dev/vcio"};
int i;
// open a char device file used for communicating with kernel mbox driver
// first try the more restrictive interface but fall back to full if unavailable
for (i = 0; i < countof(devices); i++)
{
file_desc = open(devices[i], 0);
if (file_desc >= 0)
return file_desc;
}
fprintf(stderr, "Failed to open %s: %s\n", devices[0], strerror(errno));
return -1;
}
static void mbox_close(int file_desc) {
close(file_desc);
}
static int mbox_property(int file_desc, void *msg) {
struct firmware_msg_header *hdr = (struct firmware_msg_header *)msg;
int rc = ioctl(file_desc, IOCTL_MBOX_PROPERTY, msg);
if (rc < 0)
fprintf(stderr, "ioctl_mbox_property failed: %d\n", rc);
LOG_DEBUG("msg.hdr.code: %08x\n", hdr->code);
LOG_DEBUG("msg.hdr.buf_size: %d\n", hdr->buf_size);
LOG_DEBUG("msg.hdr.tag: %d\n", hdr->tag);
LOG_DEBUG("msg.hdr.tag_buf_size: %d\n", hdr->tag_buf_size);
LOG_DEBUG("msg.hdr.tag_req_resp_size: %d\n", hdr->tag_req_resp_size);
if (!(hdr->code & VC_MAILBOX_ERROR) ||
!(hdr->tag_req_resp_size & VC_MAILBOX_ERROR))
return -1;
return 0;
}
/* Get the library version string */
const char *rpi_eeprom_ab_version(void) {
return LIBRPIEEPROMAB_VERSION;
}
/* Get the error string for the error code */
const char *rpi_eeprom_ab_update_strerror(RPI_EEPROM_AB_ERROR error) {
static char buf[40];
switch (error) {
case RPI_EEPROM_AB_ERROR_NO_ERROR:
return "Success";
case RPI_EEPROM_AB_ERROR_FAILED:
return "Unknown error. Please check you are running a firmware version that supports AB.";
case RPI_EEPROM_AB_ERROR_INVALID_PARTITION:
return "Invalid partition selected";
case RPI_EEPROM_AB_ERROR_HASH_MISMATCH:
return "Hash mismatch";
case RPI_EEPROM_AB_ERROR_BUSY:
return "Busy";
case RPI_EEPROM_AB_ERROR_UPDATE:
return "Update failed";
case RPI_EEPROM_AB_ERROR_UNCOMMITTED:
return "Unsafe to perform action from uncommitted partition";
case RPI_EEPROM_AB_ERROR_INVALID_ARG:
return "Invalid argument";
case RPI_EEPROM_AB_ERROR_LENGTH:
return "Length error";
case RPI_EEPROM_AB_ERROR_ERASE:
return "Erase failed";
case RPI_EEPROM_AB_ERROR_WRITE:
return "Write failed";
case RPI_EEPROM_AB_ERROR_ALREADY_COMMITTED:
return "Already committed";
case RPI_EEPROM_AB_ERROR_SPI_GPIO_ERROR:
return "SPI GPIO Error. Please enable AB Firmware in raspi-config.";
case RPI_EEPROM_AB_ERROR_NO_PARTITIONING:
return "AB Partitioning is not being used. Perform an AB update to enable AB partitioning.";
default:
snprintf(buf, sizeof(buf), "Unrecognised error: %d", error);
return buf;
}
}
/* Get the status string for the status code */
const char *rpi_eeprom_ab_update_strstatus(RPI_EEPROM_AB_UPDATE_RC_STATUS status) {
switch (status) {
case RPI_EEPROM_AB_UPDATE_RC_NO_UPDATE:
return "No update";
case RPI_EEPROM_AB_UPDATE_RC_CANCELED:
return "Canceled";
case RPI_EEPROM_AB_UPDATE_RC_BUSY:
return "Busy";
case RPI_EEPROM_AB_UPDATE_RC_SUCCSESS:
return "Success";
default:
return "Unrecognised status code";
}
}
/* Send a packet to the firmware mailbox to be written to the partition in EEPROM */
static RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_packet_write(uint32_t address,
const uint8_t *data, size_t data_len) {
int mb;
int rc;
struct firmware_update_packet_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_SET_EEPROM_PACKET;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_packet_msg);
msg.address = address;
msg.length = data_len;
memcpy(msg.data, data, data_len);
msg.end_tag = 0;
rc = mbox_property(mb, (struct firmware_msg *)&msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Read data from EEPROM through the firmware mailbox */
static RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_packet_read(uint32_t address,
uint8_t *data, size_t data_len) {
int mb;
int rc;
struct firmware_update_packet_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
if (!data || data_len > RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE) {
return RPI_EEPROM_AB_ERROR_LENGTH;
}
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_GET_EEPROM_PACKET;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_packet_msg);
msg.address = address;
msg.length = data_len;
msg.end_tag = 0;
rc = mbox_property(mb, &msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
memcpy(data, msg.data, data_len);
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Get the status of the EEPROM update from firmware mailbox */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_status(RPI_EEPROM_AB_UPDATE_RC_STATUS *status,
RPI_EEPROM_AB_ERROR *firmware_error, uint32_t *spi_gpio_check, uint32_t *using_partitioning) {
int mb;
int rc;
struct firmware_update_get_status_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_GET_EEPROM_UPDATE_STATUS;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_get_status_msg);
msg.end_tag = 0;
rc = mbox_property(mb, &msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
*status = msg.status;
*firmware_error = msg.firmware_error;
*spi_gpio_check = msg.spi_gpio_check;
*using_partitioning = msg.using_partitioning;
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_spi_check(uint32_t *spi_gpio_check) {
RPI_EEPROM_AB_UPDATE_RC_STATUS _status;
RPI_EEPROM_AB_ERROR _firmware_error;
uint32_t using_partitioning;
return rpi_eeprom_ab_update_get_status(&_status, &_firmware_error,
spi_gpio_check, &using_partitioning);
}
/* Send an update command to the firmware mailbox */
static RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_send_command(RPI_EEPROM_AB_UPDATE_COMMAND_CODE command) {
int mb;
int rc;
struct firmware_update_command_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_SET_EEPROM_UPDATE_STATUS;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_command_msg);
msg.command = command;
msg.end_tag = 0;
rc = mbox_property(mb, (struct firmware_msg *)&msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Cancel the current EEPROM update */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_cancel(void) {
return rpi_eeprom_ab_update_send_command(RPI_EEPROM_AB_UPDATE_CANCEL);
}
/* Add a journal entry to EEPROM to mark the AB partition as valid but not committed */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_set_partition(RPI_EEPROM_RELATIVE_PARTITION relative_partition, uint8_t *hash) {
int mb;
int rc;
struct firmware_update_set_eeprom_partition_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_SET_EEPROM_PARTITION;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_set_eeprom_partition_msg);
msg.relative_partition = relative_partition;
if (hash)
memcpy(msg.hash, hash, 32);
msg.end_tag = 0;
rc = mbox_property(mb, (struct firmware_msg *)&msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Commit the chosen AB partition */
static RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_commit_partition(RPI_EEPROM_RELATIVE_PARTITION relative_partition) {
int mb;
int rc;
struct firmware_update_set_ab_param_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_SET_EEPROM_AB_PARAMS;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_set_ab_param_msg);
msg.param = EEPROM_UPDATE_PARAM_COMMIT;
msg.value.relative_partition_to_commit = relative_partition;
msg.end_tag = 0;
rc = mbox_property(mb, (struct firmware_msg *)&msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Commit the current AB partition */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_commit_current_partition(void) {
return rpi_eeprom_ab_update_commit_partition(RPI_EEPROM_AB_CURRENT_PARTITION);
}
/* Force commit the opposite AB partition */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_force_commit_opposite(void) {
return rpi_eeprom_ab_update_commit_partition(RPI_EEPROM_AB_OPPOSITE_PARTITION);
}
/* Set the value of the tryboot flag */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_set_tryboot(uint32_t tryboot) {
int mb;
int rc;
struct firmware_update_set_ab_param_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_SET_EEPROM_AB_PARAMS;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_set_ab_param_msg);
msg.param = EEPROM_UPDATE_PARAM_TRYBOOT;
msg.value.tryboot_value = tryboot;
msg.end_tag = 0;
rc = mbox_property(mb, (struct firmware_msg *)&msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Get the AB EEPROM parameters from firmware mailbox */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_ab_params(RPI_EEPROM_AB_PARTITION *partition,
uint32_t *committed, uint32_t *tryboot, RPI_EEPROM_AB_PARTITION *partition_at_boot, uint32_t *committed_at_boot) {
int mb;
int rc;
struct firmware_update_get_ab_params_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_GET_EEPROM_AB_PARAMS;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_get_ab_params_msg);
msg.end_tag = 0;
rc = mbox_property(mb, (struct firmware_msg *)&msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
*partition = msg.partition;
*committed = msg.committed;
*tryboot = msg.tryboot;
*partition_at_boot = msg.partition_at_boot;
*committed_at_boot = msg.committed_at_boot;
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Get the current partition */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_current_partition(RPI_EEPROM_AB_PARTITION *partition) {
uint32_t _committed, _tryboot;
RPI_EEPROM_AB_PARTITION _partition_at_boot;
uint32_t _committed_at_boot;
return rpi_eeprom_ab_update_get_ab_params(partition, &_committed,
&_tryboot, &_partition_at_boot, &_committed_at_boot);
}
/* Get the committed flag for the current partition */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_current_committed(uint32_t *committed) {
uint32_t _tryboot, _committed_at_boot;
RPI_EEPROM_AB_PARTITION _partition_at_boot, _partition;
return rpi_eeprom_ab_update_get_ab_params(&_partition, committed,
&_tryboot, &_partition_at_boot, &_committed_at_boot);
}
/* Get the current value of the tryboot flag */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_current_tryboot(uint32_t *tryboot) {
uint32_t _committed, _committed_at_boot;
RPI_EEPROM_AB_PARTITION _partition_at_boot, _partition;
return rpi_eeprom_ab_update_get_ab_params(&_partition, &_committed,
tryboot, &_partition_at_boot, &_committed_at_boot);
}
/* Get the partition and committed flag from during boot */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_boot_partition(RPI_EEPROM_AB_PARTITION *partition_at_boot, uint32_t *committed_at_boot) {
uint32_t _tryboot, _committed;
RPI_EEPROM_AB_PARTITION _partition;
return rpi_eeprom_ab_update_get_ab_params(&_partition, &_committed,
&_tryboot, partition_at_boot, committed_at_boot);
}
/* Get the stored hash and valid flag for the given relative partition */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_update_get_eeprom_partition(RPI_EEPROM_AB_PARTITION *committed_partition,
RPI_EEPROM_AB_PARTITION *valid_partition, uint8_t *committed_partition_hash, uint8_t *valid_partition_hash) {
int mb;
int rc;
struct firmware_update_get_eeprom_partition_msg msg = {0};
mb = mbox_open();
if (mb < 0)
return RPI_EEPROM_AB_ERROR_FAILED;
msg.hdr.buf_size = sizeof(msg);
msg.hdr.tag = TAG_GET_EEPROM_PARTITION;
msg.hdr.tag_buf_size = TAG_BUFFER_SIZE(struct firmware_update_get_eeprom_partition_msg);
msg.end_tag = 0;
rc = mbox_property(mb, (struct firmware_msg *)&msg);
mbox_close(mb);
if (rc < 0) {
LOG_DEBUG("Mailbox property failed: %d\n", rc);
return RPI_EEPROM_AB_ERROR_FAILED;
}
if (msg.error & VC_MAILBOX_ERROR) {
return (RPI_EEPROM_AB_ERROR)(msg.error & ~VC_MAILBOX_ERROR);
}
*committed_partition = msg.committed_partition;
*valid_partition = msg.valid_partition;
memcpy(committed_partition_hash, msg.committed_partition_hash, 32);
memcpy(valid_partition_hash, msg.valid_partition_hash, 32);
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Send the update data to firmware mailbox and start the write to the EEPROM */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_write_eeprom_update(uint8_t *update_data, uint32_t update_len, int print_progress) {
RPI_EEPROM_AB_ERROR err;
uint8_t message[RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE];
uint32_t sent_len = 0;
int attempt;
int packet_length = RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE;
if (!update_data || update_len == 0 || update_len > RPI_EEPROM_AB_PARTITION_SIZE) {
LOG_DEBUG("Invalid update length\n");
return RPI_EEPROM_AB_ERROR_LENGTH;
}
// Start the erase of the EEPROM
err = rpi_eeprom_ab_update_send_command(RPI_EEPROM_AB_UPDATE_START_ERASE);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
if (err == RPI_EEPROM_AB_ERROR_INVALID_ARG) {
LOG_DEBUG("Invalid arg returned. Assume it's old beta firmware version 2025-02-11 and continue...\n");
} else {
LOG_DEBUG("Failed to start erase of EEPROM: %s\n",
rpi_eeprom_ab_update_strerror(err));
return err;
}
}
while (sent_len < update_len) {
if (sent_len + RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE > update_len) {
packet_length = update_len - sent_len;
}
memcpy(message, update_data + sent_len, packet_length);
for (attempt = 0; attempt < 7; attempt++) {
err = rpi_eeprom_ab_update_packet_write(sent_len, message,
packet_length);
if (print_progress) {
printf(".");
fflush(stdout);
}
if (err == RPI_EEPROM_AB_ERROR_NO_ERROR) {
break;
} else if (err == RPI_EEPROM_AB_ERROR_BUSY) {
LOG_DEBUG("EEPROM is busy, retrying...\n");
continue;
} else if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
LOG_DEBUG("Failed to send to EEPROM: %s\n",
rpi_eeprom_ab_update_strerror(err));
return err;
}
}
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
LOG_DEBUG("Failed to send to EEPROM: %s\n",
rpi_eeprom_ab_update_strerror(err));
return err;
}
sent_len += packet_length;
}
// For old beta firmware version 2026-02-11, start the write to the EEPROM
// Not required for later versions
err = rpi_eeprom_ab_update_send_command(RPI_EEPROM_AB_UPDATE_START_WRITE);
if (err == RPI_EEPROM_AB_ERROR_NO_ERROR) {
RPI_EEPROM_AB_UPDATE_RC_STATUS status;
RPI_EEPROM_AB_ERROR firmware_error;
uint32_t _spi_gpio_check;
uint32_t _using_partitioning;
// Wait for the write to complete
int delay = 0;
while (delay < 60) {
err = rpi_eeprom_ab_update_get_status(&status, &firmware_error,
&_spi_gpio_check, &_using_partitioning);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
LOG_DEBUG("Failed to get status: %s\n",
rpi_eeprom_ab_update_strerror(err));
return err;
}
if (status == RPI_EEPROM_AB_UPDATE_RC_SUCCSESS) {
break;
}
delay++;
if (print_progress) {
printf(".");
fflush(stdout);
}
sleep(1);
}
if ((delay >= 60) && (status != RPI_EEPROM_AB_UPDATE_RC_SUCCSESS)) {
printf("\nWaiting for write to EEPROM is taking too long. It will continue in the background.\n");
printf("You can check the status (Busy or Success) of the write with 'rpi-eeprom-ab update-status'\n");
return RPI_EEPROM_AB_ERROR_WRITE;
}
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Read the entire EEPROM and store the data in the buffer */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_read_entire_eeprom(uint8_t data[RPI_EEPROM_CAPACITY]) {
RPI_EEPROM_AB_ERROR err;
uint8_t packet[RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE];
int read_len = 0;
int packet_length = RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE;
while (read_len < RPI_EEPROM_CAPACITY) {
if (read_len + packet_length > RPI_EEPROM_CAPACITY) {
packet_length = RPI_EEPROM_CAPACITY - read_len;
}
err = rpi_eeprom_ab_update_packet_read(read_len, packet, packet_length);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR){
LOG_DEBUG("Failed to read from EEPROM: %s\n",
rpi_eeprom_ab_update_strerror(err));
return err;
}
memcpy(data + read_len, packet, packet_length);
read_len += packet_length;
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Read the partition from the EEPROM and store the data in the buffer */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_read_eeprom_partition(RPI_EEPROM_AB_PARTITION partition, uint8_t data[RPI_EEPROM_AB_PARTITION_SIZE]) {
RPI_EEPROM_AB_ERROR err;
uint8_t packet[RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE];
int start_addr = (partition == RPI_EEPROM_AB_PARTITION_A) ? RPI_EEPROM_A_PARTITION_START_ADDRESS : RPI_EEPROM_B_PARTITION_START_ADDRESS;
int read = 0;
int packet_length = RPI_EEPROM_AB_UPDATE_PACKET_MAX_SIZE;
while (read < RPI_EEPROM_AB_PARTITION_SIZE) {
if (read + packet_length > RPI_EEPROM_AB_PARTITION_SIZE) {
packet_length = RPI_EEPROM_AB_PARTITION_SIZE - read;
}
err = rpi_eeprom_ab_update_packet_read(start_addr + read, packet, packet_length);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR){
LOG_DEBUG("Failed to read from EEPROM: %s\n",
rpi_eeprom_ab_update_strerror(err));
return err;
}
memcpy(data + read, packet, packet_length);
read += packet_length;
}
return RPI_EEPROM_AB_ERROR_NO_ERROR;
}
/* Read the current partition from the EEPROM and store the data in the buffer */
RPI_EEPROM_AB_ERROR rpi_eeprom_ab_read_current_partition(uint8_t data[RPI_EEPROM_AB_PARTITION_SIZE]) {
RPI_EEPROM_AB_ERROR err;
RPI_EEPROM_AB_PARTITION partition;
RPI_EEPROM_AB_PARTITION _partition_at_boot;
uint32_t _committed_at_boot, _committed, _tryboot;
err = rpi_eeprom_ab_update_get_ab_params(&partition, &_committed, &_tryboot, &_partition_at_boot, &_committed_at_boot);
if (err != RPI_EEPROM_AB_ERROR_NO_ERROR) {
LOG_DEBUG("Failed to get AB parameters: %s\n", rpi_eeprom_ab_update_strerror(err));
return err;
}
return rpi_eeprom_ab_read_eeprom_partition(partition, data);
}