-
Notifications
You must be signed in to change notification settings - Fork 8
/
tegra-bootloader-update.c
1522 lines (1414 loc) · 43.2 KB
/
tegra-bootloader-update.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
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tegra-bootloader-update.c
*
* Tool for updating/initializing Tegra boot partitions
* using a BUP package.
*
* Copyright (c) 2019-2021, Matthew Madison
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <tegra-eeprom/cvm.h>
#include <zlib.h>
#include "bup.h"
#include "gpt.h"
#include "bct.h"
#include "smd.h"
#include "ver.h"
#include "util.h"
#include "config.h"
static struct option options[] = {
{ "initialize", no_argument, 0, 'i' },
{ "slot-suffix", required_argument, 0, 's' },
{ "dry-run", no_argument, 0, 'n' },
{ "needs-repartition", no_argument, 0, 'N' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 0 },
{ 0, 0, 0, 0 }
};
static const char *shortopts = ":ins:h";
static char *optarghelp[] = {
"--initialize ",
"--slot-suffix ",
"--dry-run ",
"--needs-repartition ",
"--help ",
"--version ",
};
static char *opthelp[] = {
"initialize the entire set of boot partitions",
"update only the redundant boot partitions with the specified suffix (with no SMD update)",
"do not perform any writes, just show what would be written",
"check if boot device needs repartitioning (T186/T194 only)",
"display this help text",
"display version information"
};
struct update_entry_s {
char partname[64];
gpt_entry_t *part;
char devname[PATH_MAX];
off_t bup_offset;
size_t length;
};
struct update_list_s {
unsigned int count;
const char **partnames;
};
#define MAX_ENTRIES 64
static struct update_entry_s redundant_entries[MAX_ENTRIES];
static struct update_entry_s nonredundant_entries[MAX_ENTRIES];
static unsigned int redundant_entry_count;
static unsigned int nonredundant_entry_count;
static size_t contentbuf_size;
static size_t slotbuf_size;
static uint8_t *contentbuf, *slotbuf, *zerobuf;
static int bct_updated;
static tegra_soctype_t soctype = TEGRA_SOCTYPE_INVALID;
static bool spiboot_platform;
static unsigned long bootdev_size;
/*
* For tegra210 platforms, these are the names of partitions
* to be updated, **in order**. Note that only the eMMC-based
* tegra210 platforms have redundant copies of most of the boot
* partitions, and that the naming of the redundant NVC partition
* is different between eMMC and SPIflash platforms.
*/
static const char *t210_emmc_partnames[] = {
"VER_b", "BCT", "NVC-1",
"PT-1", "TBC-1", "RP1-1", "EBT-1", "WB0-1", "BPF-1", "DTB-1", "TOS-1", "EKS-1", "LNX-1",
"BCT",
"BCT",
"PT", "TBC", "RP1", "EBT", "WB0", "BPF", "DTB", "TOS", "EKS", "LNX",
"NVC", "VER",
};
static const char *t210_spi_sd_partnames[] = {
"VER_b", "BCT", "NVC_R",
"BCT",
"BCT",
"PT", "TBC", "RP1", "EBT", "WB0", "BPF", "DTB", "TOS", "EKS", "LNX",
"NVC", "VER",
};
static const struct update_list_s update_list_t210_emmc = {
.count = sizeof(t210_emmc_partnames)/sizeof(t210_emmc_partnames[0]),
.partnames = t210_emmc_partnames,
};
static const struct update_list_s update_list_t210_spi_sd = {
.count = sizeof(t210_spi_sd_partnames)/sizeof(t210_spi_sd_partnames[0]),
.partnames = t210_spi_sd_partnames,
};
/*
* print_usage
*
* Does what it says, extracting option strings and
* help from the arrays defined above.
*
* Returns: nothing
*/
static void
print_usage (void)
{
int i;
printf("\nUsage:\n");
printf("\ttegra-bootloader-update <option> <bup-package-path>\n\n");
printf("Options:\n");
for (i = 0; i < sizeof(options)/sizeof(options[0]) && options[i].name != 0; i++) {
printf(" %s\t%c%c\t%s\n",
optarghelp[i],
(options[i].val == 0 ? ' ' : '-'),
(options[i].val == 0 ? ' ' : options[i].val),
opthelp[i]);
}
printf("\nArguments:\n");
printf(" <bup-package-path>\tpathname of bootloader update package\n");
} /* print_usage */
/*
* read_completely_at
*
* Utility function for seeking to a specific offset
* and reading a fixed number of bytes into a buffer,
* handling short reads.
*
* fd: file descriptor
* buf: pointer to read buffer
* bufsiz: number of bytes to read
* offset: offset from start of file/device
*
* Returns: number of bytes read, or
* -1 on error (errno set)
*
*/
static ssize_t
read_completely_at (int fd, void *buf, size_t bufsiz, off_t offset)
{
ssize_t n, total;
size_t remain;
if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
return -1;
for (remain = bufsiz, total = 0; remain > 0; total += n, remain -= n) {
n = read(fd, (uint8_t *) buf + total, remain);
if (n <= 0)
return -1;
}
return total;
} /* read_completely_at */
/*
* write_completely_at
*
* Utility function for seeking to a specific offset
* and writing a fixed number of bytes to a file or device,
* handling short writes.
*
* fd: file descriptor
* buf: pointer to data to be written
* bufsiz: number of bytes to write
* offset: offset from start of file/device
*
* Returns: number of bytes written, or
* -1 on error (errno set)
*
*/
static ssize_t
write_completely_at (int fd, void *buf, size_t bufsiz, off_t offset, size_t erase_size)
{
ssize_t n, total;
size_t remain;
if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
return -1;
if (erase_size != 0) {
for (remain = erase_size, total = 0; remain > 0; total += n, remain -= n) {
n = write(fd, (uint8_t *) zerobuf + total, remain);
if (n <= 0)
return -1;
}
fsync(fd);
if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
return -1;
}
for (remain = bufsiz, total = 0; remain > 0; total += n, remain -= n) {
n = write(fd, (uint8_t *) buf + total, remain);
if (n <= 0)
return -1;
}
return total;
} /* write_completely_at */
/*
* redundant_part_format
*
* Returns a printf-style format string for formatting
* the name of a redundant partition, handling the differences
* in naming conventions between different platform variants.
*
* partname: base name of partition
*
* Returns: character string pointer
*
*/
static const char *redundant_part_format(const char *partname)
{
if (soctype != TEGRA_SOCTYPE_210)
return "%s_b";
if (strcmp(partname, "NVC") == 0)
return (spiboot_platform ? "%s_R" : "%s-1");
if (strcmp(partname, "VER") == 0)
return "%s_b";
return "%s-1";
} /* redundant_part_format */
/*
* update_bct
*
* Special handling for writing the BCT. Content to be
* written expected to be present in contentbuf.
*
* For tegra186/tegra194 platforms only.
*
* A block is 16KiB or 32KiB and holds multiple slots;
* each slot is an even number of "pages" in size, where
* the page size is 512 bytes for eMMC devices and 2KiB for
* SPI flash.
* The Tegra bootrom can handle up to 63 blocks, but
* in practice, only block 0 slots 0 & 1, and block 1 slot 0
* are used.
*
* Write sequence is block 0/slot 1, then block 1/slot 0,
* then block 0/slot 0.
*
* bootfd: file descriptor for boot device
* curbct: pointer to buffer holding current BCT partition, previously read
* (NULL if initializing)
* newbct: pointer to new BCT to write (maybe)
* ent: pointer to entry from the update payload
*
* returns: 0 on success, -1 on error (errno not set)
*
*/
static int
update_bct (int bootfd, void *curbct, void *newbct, struct update_entry_s *ent)
{
unsigned int block_size = (spiboot_platform ? 32768 : 16384);
unsigned int page_size = (spiboot_platform ? 2048 : 512);
size_t bctslotsize;
int i;
if (soctype == TEGRA_SOCTYPE_210) {
printf("[INTERNAL ERROR]\n");
fprintf(stderr, "Internal error: incorrect BCT update function for t210\n");
return -1;
}
if (curbct != NULL) {
if ((soctype == TEGRA_SOCTYPE_186 && !bct_update_valid_t18x(curbct, newbct)) ||
(soctype == TEGRA_SOCTYPE_194 && !bct_update_valid_t19x(curbct, newbct))) {
printf("[FAIL]\n");
fprintf(stderr, "Error: validation check failed for BCT update\n");
return -1;
}
}
bctslotsize = page_size * ((ent->length + (page_size-1)) / page_size);
for (i = 0; i < 3; i++) {
off_t offset = 0;
switch (i) {
case 0:
offset = (off_t) bctslotsize;
break;
case 1:
offset = block_size;
break;
case 2:
offset = 0;
break;
}
if (curbct != NULL && memcmp(newbct, (uint8_t *)curbct + offset, ent->length) == 0)
printf("[offset=%lu,no update needed]...", (unsigned long) offset);
else {
printf("[offset=%lu]...", (unsigned long) offset);
if (write_completely_at(bootfd, newbct, ent->length, ent->part->first_lba * 512 + offset, bctslotsize) < 0) {
printf("[FAIL]\n");
perror("BCT");
return -1;
}
}
}
fsync(bootfd);
bct_updated = 1;
printf("[OK]\n");
return 0;
} /* update_bct */
/*
* update_bct_t210
*
* Handles BCT updates to t210 platforms.
*
* On t210, there are up to 64 copies of the BCT. Ordering is:
* Last entry, (other updates), middle entries, (other updates), first entry
*
* SPI flash platforms put two copies at block 0; MMC platforms put one.
* All other entries start at beginning of a block.
*
* The 'which' argument is:
* < 0 -> update last BCT entry
* = 0 -> update first BCT entry
* other -> update middle entries (number varies by platform)
*
* Caller must initialize 'which' to -1 before the first call, since
* the last BCT is always updated first. This function will update
* it with each call.
*
* Note that we use 0-based counts for the redundant partitions
* (BCT, BCT-1, BCT-2, ... BCT-63), as opposed to 1-based counts
* (BCT, BCT-2, BCT-3, ... BCT-64) used in the update script provided
* in the L4T BSP.
*
* bootfd: file descriptor for boot device
* curbct: pointer to buffer holding current BCT partition, previously read
* (NULL if initializing)
* newbct: pointer to new BCT to write (maybe)
* ent: pointer to entry from the update payload
* which: pointer to BCT update context, see above
*
* returns: 0 on success, -1 on error (errno not set)
*/
static int
update_bct_t210 (int bootfd, void *curbct, void *newbct, struct update_entry_s *ent, int *which)
{
unsigned int block_size = (spiboot_platform ? 32768 : 16384);
unsigned int page_size = (spiboot_platform ? 2048 : 512);
unsigned int bctcopies = (spiboot_platform ? 2 : 1);
unsigned int bctpartsize;
int bctcount, bctstart, bctend, bctidx;
char bctname[32];
static const char indent[] = " "; // length of 'Processing BCT... leader
const char *prefix;
if (soctype != TEGRA_SOCTYPE_210) {
printf("[INTERNAL ERROR]\n");
fprintf(stderr, "Internal error: incorrect BCT function for non-t210\n");
return -1;
}
if (which == NULL) {
printf("[INTERNAL ERROR]\n");
fprintf(stderr, "Internal error: no BCT selection context for t210 update\n");
return -1;
}
if (curbct != NULL && !bct_update_valid_t21x(curbct, newbct, &block_size, &page_size)) {
printf("[FAIL]\n");
fprintf(stderr, "Error: validation check failed for BCT update\n");
return -1;
}
if (ent->length % page_size != 0) {
printf("[FAIL]\n");
fprintf(stderr, "Error: BCT update payload not an even multiple of boot device page size\n");
return -1;
}
if (ent->length * bctcopies > block_size) {
printf("[FAIL]\n");
fprintf(stderr, "Error: %u BCT payload%s too large for boot device block size\n",
bctcopies, (bctcopies == 1 ? "" : "s"));
return -1;
}
bctpartsize = (ent->part->last_lba - ent->part->first_lba + 1) * 512;
bctcount = bctpartsize / block_size;
if (bctcount > 64)
bctcount = 64;
if (*which < 0) {
bctstart = bctend = bctcount - 1;
/* Write middle entries next */
*which = 1;
} else if (*which == 0) {
bctstart = bctend = 0;
/* End of the line, just reset back to last */
*which = -1;
} else {
bctstart = bctcount - 2;
bctend = 1;
/* Write first BCT next */
*which = 0;
}
prefix = "";
for (bctidx = bctstart; bctidx >= bctend; bctidx -= 1, prefix = indent) {
off_t offset = bctidx * block_size;
if (bctidx == 0)
strcpy(bctname, "BCT");
else
sprintf(bctname, "BCT-%u", bctidx);
if (curbct != NULL && memcmp(newbct, (uint8_t *)curbct + offset, ent->length) == 0) {
printf("%s%s: [no update needed]\n", prefix, bctname);
continue;
}
printf("%s%s: ", prefix, bctname);
fflush(stdout);
if (write_completely_at(bootfd, contentbuf, ent->length, ent->part->first_lba * 512 + offset, ent->length) < 0) {
printf("[FAIL]\n");
perror("BCT");
return -1;
}
if (bctidx == 0 && bctcopies == 2) {
offset += ent->length;
if (write_completely_at(bootfd, contentbuf, ent->length, ent->part->first_lba * 512 + offset, ent->length) < 0) {
printf("[FAIL]\n");
perror("BCT");
return -1;
}
}
printf("[OK]\n");
}
fsync(bootfd);
bct_updated = 1;
return 0;
} /* update_bct_t210 */
/*
* maybe_update_bootpart
*
* Update a boot partition if its current contents
* differ from the BUP content (which is in contentbuf).
*
* On systems that boot from eMMC, boot partitions may be
* located either in /dev/mmcblk0boot0 (called the "boot device")
* or /dev/mmcblk0boot1 (called the "GPT device").
*
* bootfd: file descriptor for boot device
* gptfd: file descriptor for second boot (aka "GPT") device
* ent: pointer to entry from update payload
* is_bct: 1 if this is a BCT update, 0 otherwise
* initialize: non-zero if initializing, 0 otherwise
* bctctx: 'which' context for BCT updates (for t210 platforms)
*
* Returns: 0 on success, -1 on error (errno not set)
*
*/
static int
maybe_update_bootpart (int bootfd, int gptfd, struct update_entry_s *ent,
int is_bct, int initialize, int *bctctx)
{
int fd;
size_t partsize = (ent->part->last_lba - ent->part->first_lba + 1) * 512;
off_t offset;
if (ent->length > partsize) {
printf("[FAIL]\n");
fprintf(stderr, "Error: BUP contents too large for boot partition\n");
return -1;
}
fd = bootfd;
offset = ent->part->first_lba * 512;
if (offset >= bootdev_size) {
if (gptfd < 0) {
printf("[FAIL]\n");
fprintf(stderr, "Partition %s starts past end of boot device\n", ent->partname);
return -1;
}
fd = gptfd;
offset -= bootdev_size;
}
if (read_completely_at(fd, slotbuf, partsize, offset) < 0) {
printf("[FAIL]\n");
perror(ent->partname);
return -1;
}
if (is_bct)
return (soctype == TEGRA_SOCTYPE_210
? update_bct_t210(bootfd, (initialize ? NULL : slotbuf), contentbuf, ent, bctctx)
: update_bct(bootfd, (initialize ? NULL : slotbuf), contentbuf, ent));
if (memcmp(contentbuf, slotbuf, ent->length) == 0) {
printf("[no update needed]\n");
return 0;
}
if (write_completely_at(fd, contentbuf, ent->length, offset, partsize) < 0) {
printf("[FAIL]\n");
perror(ent->partname);
return -1;
}
fsync(fd);
printf("[OK]\n");
return 0;
} /* maybe_update_bootpart */
/*
* process_entry
*
* Processes an entry from the update payload.
*
* bupctx: context pointer for the open BUP payload
* bootfd: file descriptor for the boot device
* gptfd: file descriptor for the "GPT" device
* ent: pointer to update payload entry to process
* dryrun: true for a dry run (no writes)
* initialize: non-zero if initializing (skip BCT comparison check)
* bctctx: pointer to 'which' contenxt for t210 BCT updates
*
* Returns: 0 on success, -1 on error (errno not set)
*
*/
static int
process_entry (bup_context_t *bupctx, int bootfd, int gptfd, struct update_entry_s *ent,
bool dryrun, int initialize, int *bctctx)
{
ssize_t total, n;
unsigned int erase_size;
int fd;
printf(" Processing %s... ", ent->partname);
fflush(stdout);
if (bup_setpos(bupctx, ent->bup_offset) == (off_t) -1) {
printf("[FAIL]\n");
fprintf(stderr, "could not set position for %s\n", ent->partname);
return -1;
}
for (total = 0; total < ent->length; total += n) {
n = bup_read(bupctx, contentbuf + total, contentbuf_size - total);
if (n <= 0) {
printf("[FAIL]\n");
fprintf(stderr, "error reading content for %s\n", ent->partname);
return -1;
}
}
if (dryrun) {
printf("[OK] (dry run)\n");
return 0;
}
if (ent->part != NULL)
return maybe_update_bootpart(bootfd, gptfd, ent, strcmp(ent->partname, "BCT") == 0, initialize, bctctx);
fd = open(ent->devname, O_RDWR);
if (fd < 0) {
printf("[FAIL]\n");
perror(ent->devname);
return -1;
}
erase_size = lseek(fd, 0, SEEK_END);
if (erase_size < 0) {
printf("[FAIL]\n");
perror(ent->devname);
close(fd);
return -1;
}
if (lseek(fd, 0, SEEK_SET) < 0) {
printf("[FAIL]\n");
perror(ent->devname);
close(fd);
return -1;
}
if (write_completely_at(fd, contentbuf, ent->length, 0, erase_size) < 0) {
printf("[FAIL]\n");
perror(ent->devname);
close(fd);
return -1;
}
fsync(fd);
close(fd);
printf("[OK]\n");
return 0;
} /* process_entry */
/*
* order_entries
*
* Sorts an entries list to ensure that we process
* mb2/mb2_b before BCT before mb1/mb1_b. For
* tegra186/tegra194 platforms only.
*
* orig: array of payload entries to process
* ordered: array of pointers to be filled by this function
* count: length of the arrays
*
* Returns: nothing
*/
static void
order_entries (struct update_entry_s *orig, struct update_entry_s **ordered, unsigned int count)
{
int mb1, mb1_b, bct, mb2, mb2_b;
unsigned int i, j;
mb1 = mb1_b = bct = mb2 = mb2_b = -1;
j = 0;
for (i = 0; i < count; i++) {
if (strcmp(orig[i].partname, "mb1") == 0)
mb1 = i;
else if (strcmp(orig[i].partname, "mb1_b") == 0)
mb1_b = i;
else if (strcmp(orig[i].partname, "mb2") == 0)
mb2 = i;
else if (strcmp(orig[i].partname, "mb2_b") == 0)
mb2_b = i;
else if (strcmp(orig[i].partname, "BCT") == 0)
bct = i;
else
ordered[j++] = &orig[i];
}
if (mb2 >= 0)
ordered[j++] = &orig[mb2];
if (mb2_b >= 0)
ordered[j++] = &orig[mb2_b];
if (bct >= 0)
ordered[j++] = &orig[bct];
if (mb1 >= 0)
ordered[j++] = &orig[mb1];
if (mb1_b >= 0)
ordered[j++] = &orig[mb1_b];
if (j != count)
fprintf(stderr, "Warning: ordered entry list mismatch\n");
} /* order_entries */
/*
* find_entry_by_name
*
* Returns a pointer to the update entry for a named partition.
*
* list: array of update entries
* count: size of the array
* name: name to locate in the array
*
* Returns: NULL on error, valid pointer otherwise
*/
static struct update_entry_s *
find_entry_by_name (struct update_entry_s *list, unsigned int count, const char *name)
{
unsigned int i;
for (i = 0; i < count; i += 1)
if (strcmp(list[i].partname, name) == 0)
return &list[i];
return NULL;
} /* find_entry_by_name */
/*
* order_entries_t210
*
* Builds an array of pointers to update entries for
* performing partition updates in the correct order
* on tegra210 systems.
*
* Note that on tegra210s (unlike tegra186/tegra194), the
* ordered list will be longer than the original list, since
* BCT updates are handled in multiple parts (last, middle, first),
* with each update pointing back to the same original entry.
*
* Entries that do not appear in the fixed-order list are
* appended to the end.
*
* orig: array of payload entries to process
* ordered: array of pointers to be filled by this function
* count: length of the arrays
*
* Returns: number of entries in the ordered list
*/
static unsigned int
order_entries_t210 (struct update_entry_s *orig, struct update_entry_s **ordered, unsigned int count)
{
const struct update_list_s *update_list = (spiboot_platform
? &update_list_t210_spi_sd
: &update_list_t210_emmc);
struct update_entry_s *ent;
unsigned int i, retcount;
bool used[128];
if (count > sizeof(used)/sizeof(used[0])) {
fprintf(stderr, "Internal error: update entry list too long\n");
return 0;
}
memset(used, 0, sizeof(used));
for (i = 0, retcount = 0; i < update_list->count; i++) {
ent = find_entry_by_name(orig, count, update_list->partnames[i]);
if (ent == NULL) {
/* EKS partitions are optional */
if (memcmp(update_list->partnames[i], "EKS", 3) == 0)
continue;
else {
fprintf(stderr, "Error: payload or partition not found for %s\n",
update_list->partnames[i]);
return 0;
}
}
ordered[retcount++] = ent;
used[ent-orig] = true;
}
for (i = 0; i < count; i++) {
if (!used[i])
ordered[retcount++] = &orig[i];
}
return retcount;
} /* order_entries_t210 */
/*
* nvc_partitions_match
*
* Checks (via CRC computation) that the NVC partition
* and its backup (NVC_R or NVC-1, depending) are identical.
*
* bootfd: fd to boot device
* gptfd: fd to gpt device
* nvc: array of two update entries - primary and backup NVC
*
* Returns:
* true: NVCs are present with identical contents
* false: either an NVC is missing or the contents do not match
*/
static bool
nvc_parts_match (int bootfd, int gptfd, struct update_entry_s *nvc[2])
{
int i, fd;
off_t offset;
size_t partsize;
uint32_t crc[2];
if (nvc[0] == NULL || nvc[0]->part == NULL || nvc[1] == NULL || nvc[1]->part == NULL)
return false;
for (i = 0; i < 2; i++) {
fd = bootfd;
offset = nvc[i]->part->first_lba * 512;
partsize = (nvc[i]->part->last_lba - nvc[i]->part->first_lba + 1) * 512;
if (offset >= bootdev_size) {
fd = gptfd;
offset -= bootdev_size;
}
if (read_completely_at(fd, slotbuf, partsize, offset) < 0)
return false;
crc[i] = crc32(0, slotbuf, partsize);
}
return crc[0] == crc[1];
} /* nvc_parts_match */
/*
* invalid_version_or_downgrade
*
* Performs checks on the current version info partitions vs.
* the information in the payload, and returns true if the
* update should not be performed because (a) the version partitions
* are corrupted, or (b) the payload was built from an older BSP
* version.
*
* Logic should be equivalent to that in the L4T updater script.
*
* bupctx: BUP context pointer
* bootfd: fd for the boot device
* gptfd: fd for the GPT device
* entry_list: list of BUP entries to be processed
* entry_count: number of entries in list
* force_initialize: don't check for downgrade or bad VER partitions
*
* Returns:
* true: cannot apply the update
* false: OK to apply the update
*/
static bool
invalid_version_or_downgrade (bup_context_t *bupctx, int bootfd, int gptfd,
struct update_entry_s *entry_list, size_t entry_count,
bool force_initialize)
{
struct update_entry_s *ver[2], *nvc[2];
struct ver_info_s verinfo[2], bup_verinfo;
off_t offset;
char ver_b_name[64], nvc_b_name[64];
ssize_t total, n;
unsigned int i;
int fd;
ver[0] = ver[1] = nvc[0] = nvc[1] = NULL;
sprintf(ver_b_name, redundant_part_format("VER"), "VER");
sprintf(nvc_b_name, redundant_part_format("NVC"), "NVC");
for (i = 0; i < entry_count; i += 1)
if (strcmp(entry_list[i].partname, "VER") == 0)
ver[0] = &entry_list[i];
else if (strcmp(entry_list[i].partname, "NVC") == 0)
nvc[0] = &entry_list[i];
else if (strcmp(entry_list[i].partname, ver_b_name) == 0)
ver[1] = &entry_list[i];
else if (strcmp(entry_list[i].partname, nvc_b_name) == 0)
nvc[1] = &entry_list[i];
/*
* Update payloads that do not update the boot chain do not contain
* a VER entry, and that's OK
*/
if (ver[0] == NULL)
return false;
/*
* Read the version info from the payload
*/
if (bup_setpos(bupctx, ver[0]->bup_offset) == (off_t) -1) {
fprintf(stderr, "Error: could not find version info in BUP payload\n");
return true;
}
for (total = 0; total < ver[0]->length; total += n) {
n = bup_read(bupctx, contentbuf + total, contentbuf_size - total);
if (n <= 0) {
fprintf(stderr, "Error reading version info from BUP payload");
return true;
}
}
if (ver_extract_info(contentbuf, ver[0]->length, &bup_verinfo) != 0) {
fprintf(stderr, "Error validating version info in BUP payload: %s\n",
strerror(errno));
return true;
}
for (i = 0; i < 2; i += 1) {
size_t partsize;
memset(&verinfo[i], 0, sizeof(verinfo[i]));
if (ver[i] == NULL)
continue;
if (ver[i]->part == NULL) {
fprintf(stderr, "Error locating %s partition\n", ver[i]->partname);
return true;
}
fd = bootfd;
offset = ver[i]->part->first_lba * 512;
if (offset >= bootdev_size) {
fd = gptfd;
offset -= bootdev_size;
}
partsize = (ver[i]->part->last_lba - ver[i]->part->first_lba + 1) * 512;
if (read_completely_at(fd, slotbuf, partsize, offset) < 0) {
fprintf(stderr, "Error reading %s partition: %s\n", ver[i]->partname, strerror(errno));
return true;
}
/*
* We don't check the return code here, since we can recover if
* just one is valid, in some cases
*/
ver_extract_info(slotbuf, partsize, &verinfo[i]);
}
/*
* If both version partitions match and have a non-zero version (thus are valid),
* check for a rollback - downgrading can brick the device, so don't allow it.
*/
if (verinfo[0].bsp_version == verinfo[1].bsp_version && verinfo[0].bsp_version != 0) {
if (verinfo[0].bsp_version > bup_verinfo.bsp_version) {
fprintf(stderr, "Error: current bootloader version is %u.%u.%u; cannot roll back to %u.%u.%u\n",
bsp_version_major(verinfo[0].bsp_version),
bsp_version_minor(verinfo[0].bsp_version),
bsp_version_maint(verinfo[0].bsp_version),
bsp_version_major(bup_verinfo.bsp_version),
bsp_version_minor(bup_verinfo.bsp_version),
bsp_version_maint(bup_verinfo.bsp_version));
return true;
}
/*
* Validate that the last update was completely applied by comparing the
* NVC partition against its redundant copy. If there's a mismatch, something
* went wrong and we cannot apply the update.
*/
if (verinfo[0].crc == verinfo[1].crc && !nvc_parts_match(bootfd, gptfd, nvc)) {
fprintf(stderr, "Error: NVC partition mismatch - reflash required\n");
return true;
}
/*
* This is OK - no further checks required
*/
return false;
}
/*
* If the VER_b partition is invalid, but the primary VER is valid, that's OK - just check for a rollback.
* Otherwise, if VER_b is valid, the update can be applied if the BUP version exactly matches VER_b's version.
* Otherwise, if none of the checks have worked so far, something is wrong.
*/
if (verinfo[1].bsp_version == 0 && verinfo[0].bsp_version != 0 &&
verinfo[0].bsp_version > bup_verinfo.bsp_version) {
if (force_initialize) {
fprintf(stderr, "Warning: downgrading bootloader from %u.%u.%u to %u.%u.%u\n",
bsp_version_major(verinfo[0].bsp_version),
bsp_version_minor(verinfo[0].bsp_version),
bsp_version_maint(verinfo[0].bsp_version),
bsp_version_major(bup_verinfo.bsp_version),
bsp_version_minor(bup_verinfo.bsp_version),
bsp_version_maint(bup_verinfo.bsp_version));
return false;
}
fprintf(stderr, "Error: current bootloader version is %u.%u.%u; cannot downgrade to %u.%u.%u\n",
bsp_version_major(verinfo[0].bsp_version),
bsp_version_minor(verinfo[0].bsp_version),
bsp_version_maint(verinfo[0].bsp_version),
bsp_version_major(bup_verinfo.bsp_version),
bsp_version_minor(bup_verinfo.bsp_version),
bsp_version_maint(bup_verinfo.bsp_version));
return true;
} else if (verinfo[1].bsp_version != 0 && verinfo[1].bsp_version != bup_verinfo.bsp_version) {
fprintf(stderr, "Error: previous update was incomplete; please update with version %u.%u.%u\n",
bsp_version_major(verinfo[1].bsp_version),
bsp_version_minor(verinfo[1].bsp_version),
bsp_version_maint(verinfo[0].bsp_version));
return true;
} else if (force_initialize) {
fprintf(stderr, "Warning: bootloader version partitions were corrupted\n");
return false;
} else {
fprintf(stderr, "Error: bootloader version partitions are corrupted; cannot apply update\n");
return true;
}
return false;
} /* invalid_version_or_downgrade */
/*
* find_largest_partition
*
* Locates the largest partition to be updated, for
* allocating the buffers used for holding and
* erasing partition contents.
*
* sizep: pointer to size_t to hold result
*
* Returns: 0 on success, -1 on error
*/
static int
find_largest_partition (size_t *sizep)
{
int i;
size_t largest = 0;
size_t partlen;
for (i = 0; i < redundant_entry_count; i++) {
struct update_entry_s *ent = &redundant_entries[i];
if (ent->part != NULL)
partlen = (ent->part->last_lba - ent->part->first_lba + 1) * 512;
else {
off_t offset;
int fd = open(ent->devname, O_RDONLY);
if (fd < 0)
goto error_depart;
offset = lseek(fd, 0, SEEK_END);
close(fd);
if (offset == (off_t) -1)
goto error_depart;
partlen = (size_t) offset;
}
if (partlen > largest)
largest = partlen;
}
for (i = 0; i < nonredundant_entry_count; i++) {
struct update_entry_s *ent = &nonredundant_entries[i];
if (ent->part != NULL)
partlen = (ent->part->last_lba - ent->part->first_lba + 1) * 512;