-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflash.sh
More file actions
executable file
·4540 lines (4227 loc) · 142 KB
/
flash.sh
File metadata and controls
executable file
·4540 lines (4227 loc) · 142 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
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
#!/bin/bash
# SPDX-FileCopyrightText: Copyright (c) 2011-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# flash.sh: Flash the target board.
# flash.sh performs the best in LDK release environment.
#
# Usage: Place the board in recovery mode and run:
#
# flash.sh [options] <target_board> <root_device>
#
# for more detail enter 'flash.sh -h'
#
# Examples:
# ./flash.sh <target_board> internal - boot <target_board> from on-board device (eMMC/SDCARD)
# ./flash.sh <target_board> external - boot <target_board> from external device
# ./flash.sh <target_board> mmcblk3p1 - boot <target_board> from eMMC
# ./flash.sh <target_board> mmcblk1p1 - boot <target_board> from SDCARD
# ./flash.sh <target_board> sda1 - boot <target_board> from USB device
# ./flash.sh <target_board> nvme0n1 - boot <target_board> from NVME storage device
# ./flash.sh -N <IPaddr>:/nfsroot <target_board> eth0 - boot <target_board> from NFS
# ./flash.sh -k LNX <target_board> mmcblk1p1 - update <target_board> kernel
# ./flash.sh -k EBT <target_board> mmcblk1p1 - update <target_board> bootloader
#
# Optional Environment Variables:
# BOARDID ---------------- Pass boardid to override EEPROM value
# BOARDREV --------------- Pass board_revision to override EEPROM value
# BOARDSKU --------------- Pass board_sku to override EEPROM value
# BOOTLOADER ------------- Bootloader binary to be flashed
# CFGFILE ---------------- Partition table configuration file to be used.
# CMDLINE ---------------- Target cmdline. See help for more information.
# DEVSECTSIZE ------------ Device Sector size. (default = 512Byte).
# DTBFILE ---------------- Device Tree file to be used.
# EMCFUSE_VALUE ---------- Value of emc fuse.
# FLASHAPP --------------- Flash application running in host machine.
# FLASHER ---------------- Flash server running in target machine.
# INITRD ----------------- Initrd image file to be flashed.
# KERNEL_IMAGE ----------- Linux kernel zImage file to be flashed.
# MTS -------------------- MTS file name such as mts_si.
# MTSPREBOOT ------------- MTS preboot file name such as mts_preboot_si.
# NFSARGS ---------------- Static Network assignments.
# <C-ipa>:<S-ipa>:<G-ipa>:<netmask>
# NFSROOT ---------------- NFSROOT i.e. <my IP addr>:/exported/rootfs_dir.
# NO_KERNEL_DTB ---------- Do not use kernel dtb.
# NO_RECOVERY_IMG -------- Do not create or re-create recovery.img
# NO_ROOTFS -------------- Do not create or re-create system.img
# ODMDATA ---------------- Odmdata to be used.
# ROOTFSSIZE ------------- Linux RootFS size (internal emmc/nand only).
# ROOTFS_DIR ------------- Linux RootFS directory name.
# SBKKEY ----------------- SBK key file to used to encrypt bootloader images.
# SPEFILE ---------------- SPE firmware file path such as bootloader/spe.bin.
# FAB -------------------- Target board's FAB ID.
# TEGRABOOT -------------- lowerlayer bootloader such as nvtboot.bin.
# WB0BOOT ---------------- Warmboot code such as nvtbootwb0.bin
#
INFODIVIDER="\
###############################################################################\
";
chkerr ()
{
if [ $? -ne 0 ]; then
if [ "$1" != "" ]; then
echo "$1";
else
echo "failed.";
fi;
exit 1;
fi;
if [ "$1" = "" ]; then
echo "done.";
fi;
}
pr_conf()
{
echo "target_board=${target_board}";
echo "target_rootdev=${target_rootdev}";
echo "rootdev_type=${rootdev_type}";
echo "rootfssize=${rootfssize}";
echo "odmdata=${odmdata}";
echo "flashapp=${flashapp}";
echo "flasher=${flasher}";
echo "bootloader=${bootloader}";
echo "tegraboot=${tegraboot}";
echo "wb0boot=${wb0boot}";
echo "mtspreboot=${mtspreboot}";
echo "mts=${mts}";
echo "bctfile=${bctfile}";
echo "cfgfile=${cfgfile}";
echo "kernel_fs=${kernel_fs}";
echo "kernel_image=${kernel_image}";
echo "dtbfile=${dtbfile}"
echo "rootfs_dir=${rootfs_dir}";
echo "nfsroot=${nfsroot}";
echo "nfsargs=${nfsargs}";
echo "kernelinitrd=${kernelinitrd}";
echo "cmdline=${cmdline}";
echo "boardid=${boardid}";
}
validateIP ()
{
local ip=$1;
local ret=1;
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=${IFS};
IFS='.';
ip=($ip);
IFS=${OIFS};
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && \
${ip[2]} -le 255 && ${ip[3]} -le 255 ]];
ret=$?;
fi;
if [ ${ret} -ne 0 ]; then
echo "Invalid IP address: $1";
exit 1;
fi;
}
netmasktbl=(\
"255.255.255.252" \
"255.255.255.248" \
"255.255.255.240" \
"255.255.255.224" \
"255.255.255.192" \
"255.255.255.128" \
"255.255.255.0" \
"255.255.254.0" \
"255.255.252.0" \
"255.255.248.0" \
"255.255.240.0" \
"255.255.224.0" \
"255.255.192.0" \
"255.255.128.0" \
"255.255.0.0" \
"255.254.0.0" \
"255.252.0.0" \
"255.248.0.0" \
"255.240.0.0" \
"255.224.0.0" \
"255.192.0.0" \
"255.128.0.0" \
"255.0.0.0" \
);
validateNETMASK ()
{
local i;
local nm=$1;
for (( i=0; i<${#netmasktbl[@]}; i++ )); do
if [ "${nm}" = ${netmasktbl[$i]} ]; then
return 0;
fi;
done;
echo "Error: Invalid netmask($1)";
exit 1;
}
validateNFSargs ()
{
local a=$2;
OIFS=${IFS};
IFS=':';
a=($a);
IFS=${OIFS};
if [ ${#a[@]} -ne 4 ]; then
echo "Error: Invalid nfsargs($2)";
exit 1;
fi;
validateIP ${a[0]};
if [ "${serverip}" = "" ]; then
validateIP ${a[1]};
fi;
validateIP ${a[2]};
validateNETMASK ${a[3]};
if [ "$1" != "" ]; then
eval "$1=$2";
fi;
return 0;
}
validateNFSroot ()
{
if [ "$2" = "" ]; then
return 1;
fi;
OIFS=${IFS};
IFS=':';
local var=$1;
local a=($2);
IFS=${OIFS};
if [ ${#a[@]} -ne 2 ]; then
echo "Error: Invalid nfsroot($2)";
exit 1;
fi;
validateIP ${a[0]};
if [[ "${a[1]}" != /* ]]; then
echo "Error: Invalid nfsroot($2)";
exit 1;
fi;
eval "${var}=$2";
return 0;
}
usage ()
{
state=$1;
retval=$2;
if [[ $state == allunknown ]]; then
echo -e "
Usage: sudo ./flash.sh [options] <target_board> <rootdev>
Where,
target board: Valid target board name.
rootdev: Proper root device.";
elif [[ $state == rootdevunknown ]]; then
echo -e "
Usage: sudo ./flash.sh [options] ${target_board} <rootdev>
Where,
rootdev for ${target_board}:
${ROOT_DEV}";
else
echo "
Usage: sudo ./flash.sh [options] ${target_board} ${target_rootdev}";
fi;
cat << EOF
options:
-c <cfgfile> ---------- Flash partition table config file.
-d <dtbfile> ---------- device tree file.
-f <flashapp> --------- Path to flash application (tegraflash.py)
-h -------------------- print this message.
-i <enc rfs key file>-- key for disk encryption support.
-k <partition id> ----- partition name or number specified in flash.cfg.
-m <mts preboot> ------ MTS preboot such as mts_preboot_si.
-n <nfs args> --------- Static nfs network assignments
<Client IP>:<Server IP>:<Gateway IP>:<Netmask>
-o <odmdata> ---------- ODM data.
-r -------------------- skip building and reuse existing system.img.
-t <tegraboot> -------- tegraboot binary such as nvtboot.bin
-u <PKC key file>------ PKC key used for odm fused board.
-v <SBK key file>------ Secure Boot Key (SBK) key used for ODM fused board.
-w <wb0boot> ---------- warm boot binary such as nvtbootwb0.bin
-x <tegraid> ---------- Tegra CHIPID.
-B <boardid> ---------- BoardId.
-C <cmdline> ---------- Kernel commandline arguments.
WARNING:
Each option in this kernel commandline gets
higher preference over the values set by
flash.sh. In case of NFS booting, this script
adds NFS booting related arguments, if -i option
is omitted.
-F <flasher> ---------- Flash server such as cboot.bin.
-G <file name> -------- Read partition and save image to file.
-I <initrd> ----------- initrd file. Null initrd is default.
-K <kernel> ----------- Kernel image file such as zImage or Image.
-L <bootloader> ------- Bootloader such as cboot.bin or u-boot-dtb.bin.
-M <mts boot> --------- MTS boot file such as mts_si.
-N <nfsroot> ---------- i.e. <my IP addr>:/my/exported/nfs/rootfs.
-R <rootfs dir> ------- Sample rootfs directory.
-S <size> ------------- Rootfs size in bytes. Valid only for internal
rootdev. KiB, MiB, GiB short hands are allowed,
for example, 1GiB means 1024 * 1024 * 1024 bytes.
-T <ext num sectors> ---The number of the sectors of the external storage device.
The default value is 119537664 if this option is not set.
-U <int num sectors> ---The number of the sectors of the internal storage device.
The default value is 119537664 if this option is not set.
-Z -------------------- Print configurations and then exit.
--no-flash ------------ perform all steps except physically flashing the board.
This will create a system.img.
--external-device------ Generate flash images for external devices
--sparseupdate--------- only flash partitions that have changed. Currently only support SPI flash memory
--no-systemimg -------- Do not create or re-create system.img.
--bup ----------------- Generate bootloader update payload(BUP).
--single-image-bup <part name> Generate specified single image BUP, this must work with --bup.
--bup-type <type> ----- Generate specific type bootloader update payload(BUP), such as bl or kernel.
--multi-spec----------- Enable support for building multi-spec BUP.
--clean-up------------- Clean up BUP buffer when multi-spec is enabled.
--usb-instance <id> --- Specify the USB instance to connect to;
<id> = USB port path (e.g. 3-14).
--no-root-check ------- Typical usage of this script require root permissions.
Pass this option to allow running the script as a
regular user, in which case only specific combinations
of command-line options will be functional.
--uefi-keys <keys_conf> Specify UEFI keys configuration file.
--rcm-boot ------------ Do RCM boot instead of physically flashing the board.
--sign ---------------- Sign images and store them under "bootloader/signed"
directory. The board will not be physically flashed.
--image --------------- Specify the image to be written into board.
--boot-chain-flash <c> Flash only a specific boot chain (ex. "A, "B", "all").
Defaults to "all", inputs are case insensitive.
Not suitable for production.
--boot-chain-select <c> Specify booting chain (ex. "A" or "B") after the board is flashed.
Defaults to "A", inputs are case insensitive.
--pv-crt -------------- The certificate for the key that is used to sign cpu_bootloader
--with-systemimg ------ Generate system images also when using -k option
--pv-enc <enc_key>----- The encryption key that is used to encrypt cpu_bootloader.
--uefi-enc <uefi_enc_key> Key file (0x19: 16-byte; 0x23: 32-byte) to encrypt UEFI payloads
--uda-dir-------------- Directory to store user data that will be encrypted in UDA partition.
--generic-passphrase -- Use generic passphrase for disk encryption.
--disable-random-iv --- Disable generation of random IV; IV will be 0.
EOF
exit $retval;
}
setdflt ()
{
local var="$1";
if [ "${!var}" = "" ]; then
eval "${var}=\"$2\"";
fi;
}
setval ()
{
local var="$1";
local val="$2";
if [ "${!val}" = "" ]; then
echo "Error: missing $val not defined.";
exit 1;
fi;
eval "${var}=\"${!val}\"";
}
mkfilesoft ()
{
local var="$1";
local varname="$1name";
eval "${var}=\"$2\"";
if [ "${!var}" = "" -o ! -f "${!var}" ]; then
if [ "$3" != "" -a -f "$3" ]; then
eval "${var}=\"$3\"";
fi;
fi;
if [ "${!var}" != "" ]; then
if [ ! -f "${!var}" ]; then
eval "${var}=\"\"";
eval "${varname}=\"\"";
return 1;
fi;
eval "${var}=\"$(readlink -f "${!var}")\"";
eval "${varname}=\"$(basename "${!var}")\"";
fi;
return 0;
}
mkfilepath ()
{
local var="$1";
local varname="$1name";
eval "${var}=\"$2\"";
setdflt "${var}" "$3";
if [ "${!var}" != "" ]; then
eval "${var}=\"$(readlink -f "${!var}")\"";
if [ ! -f "${!var}" ]; then
if [ -z "${FLASHLIGHT}" ]; then
echo "Error: missing $var (${!var}).";
usage allknown 1;
fi;
dd if=/dev/zero of="${!var}" bs=512 count=100;
fi;
eval "${varname}=\"$(basename "${!var}")\"";
fi;
}
mkdirpath ()
{
local var="$1";
eval "${var}=$2";
setdflt "$1" "$3";
if [ "${!var}" != "" ]; then
eval "${var}=\"$(readlink -f "${!var}")\"";
if [ ! -d "${!var}" ]; then
if [ -z "${FLASHLIGHT}" ]; then
echo "Error: missing $var (${!var}).";
usage allknown 1;
fi;
mkdir -p "${!var}";
fi;
fi;
}
getsize ()
{
local var="$1";
local val="$2";
if [[ ${!val} != *[!0-9]* ]]; then
eval "${var}=${!val}";
elif [[ (${!val} == *KiB) && (${!val} != *[!0-9]*KiB) ]]; then
eval "${var}=$(( ${!val%KiB} * 1024 ))";
elif [[ (${!val} == *MiB) && (${!val} != *[!0-9]*MiB) ]]; then
eval "${var}=$(( ${!val%MiB} * 1024 * 1024 ))";
elif [[ (${!val} == *GiB) && (${!val} != *[!0-9]*GiB) ]]; then
eval "${var}=$(( ${!val%GiB} * 1024 * 1024 * 1024))";
else
echo "Error: Invalid $1: ${!val}";
exit 1;
fi;
}
validatePartID ()
{
local idx=0;
declare -A cf;
while read aline; do
if [ "$aline" != "" ]; then
arr=( $(echo $aline | tr '=' ' ') );
if [ "${arr[1]}" == "name" ]; then
if [ "${arr[3]}" == "id" ]; then
cf[$idx,1]="${arr[2]}";
cf[$idx,0]="${arr[4]}";
else
cf[$idx,0]="${arr[2]}";
fi
idx=$((idx+1));
fi
fi;
done < $4;
if [ "${arr[3]}" == "id" ]; then
for ((i = 0; i < idx; i++)) do
if [ "\"$3\"" = "${cf[$i,0]}" -o \
"\"$3\"" = "${cf[$i,1]}" ]; then
eval "$1=${cf[$i,0]}";
eval "$2=${cf[$i,1]}";
return 0;
fi;
done;
echo "Error: invalid partition id ($3)";
exit 1;
else
return 0;
fi;
}
cp2local ()
{
local src=$1;
if [ "${!src}" = "" ]; then return 1; fi;
if [ ! -f "${!src}" ]; then return 1; fi;
if [ "$2" = "" ]; then return 1; fi;
if [ -f "$2" -a "${!src}" = "$2" ]; then
local sum1=$(sum "${!src}");
local sum2=$(sum "$2");
if [ "$sum1" = "$sum2" ]; then
echo "Existing ${src}($2) reused.";
return 0;
fi;
fi;
echo -n "copying ${src}(${!src})... ";
cp -f "${!src}" "$2";
chkerr;
return 0;
}
chsuffix ()
{
local var="$1";
local fname=$(basename "$2");
local OIFS=${IFS};
IFS='.';
na=($fname);
IFS=${OIFS};
eval "${var}=${na[0]}.${3}";
}
pad_file_aligned ()
{
local __file="$1";
local __alignment="$2";
local __padvalue="$3";
local __padstring="";
filesize=$(stat --format=%s "${__file}");
rem=$(( filesize % __alignment ));
if (( rem > 0 )); then
rem=$(( __alignment - rem ));
for ((i = 0 ; i < rem ; i++)); do
__padstring+=${__padvalue};
done;
echo -e -n "${__padstring}" >> "${__file}";
fi;
}
build_fsimg ()
{
local __localsysfile="$1";
local __fillpat="$2";
local __rootfssize="$3";
local __rootfs_type="$4";
local __rootfs_dir="$5";
local __cmdline="$6";
local __do_sign="$7";
local _extlinux_conf="extlinux/extlinux.conf"
echo "Making ${__localsysfile}... ";
local bcnt=$(( ${__rootfssize} / 512 ));
local bcntdiv=$(( ${__rootfssize} % 512 ));
if [ ${bcnt} -eq 0 -o ${bcntdiv} -ne 0 ]; then
echo "Error: ${__rootfs_type} file system size has to be 512 bytes allign.";
exit 1;
fi
if [ "${__rootfs_type}" != "FAT32" ] && [ ! -f "${__rootfs_dir}/boot/${_extlinux_conf}" ]; then
# In case the rootfs is originated from foreign source,
# just copy extlinux.conf to /boot to make the rest of
# script happy.
if [ "${FLASHLIGHT}" != "1" ]; then
echo "${__rootfs_dir}/boot/${_extlinux_conf} is not found, exiting...";
exit 1
fi;
mkdir -p "${__rootfs_dir}/boot/extlinux";
cp "${BL_DIR}/extlinux.conf" "${__rootfs_dir}/boot/extlinux";
fi
if [ "${__fillpat}" != "" -a "${__fillpat}" != "0" ]; then
local fc=$(printf '%d' ${__fillpat});
local fillc=$(printf \\\\$(printf '%02o' $fc));
< /dev/zero head -c ${__rootfssize} | tr '\000' ${fillc} > ${__localsysfile};
chkerr "making ${__localsysfile} with fillpattern($fillc}) failed.";
else
truncate --size ${__rootfssize} ${__localsysfile};
chkerr "making ${__localsysfile} with zero fillpattern failed.";
fi;
loop_dev="$(losetup --show -f "${__localsysfile}")";
chkerr "mapping ${__localsysfile} to loop device failed.";
if [ "${__rootfs_type}" = "FAT32" ]; then
mkfs.msdos -I -F 32 "${loop_dev}" > /dev/null 2>&1;
else
mkfs -t ${__rootfs_type} "${loop_dev}" > /dev/null 2>&1;
fi;
chkerr "formating ${__rootfs_type} filesystem on ${__localsysfile} failed.";
mkdir -p mnt; chkerr "make $4 mount point failed.";
mount "${loop_dev}" mnt; chkerr "mount ${__localsysfile} failed.";
mkdir -p mnt/boot/dtb; chkerr "make ${__localsysfile}/boot/dtb failed.";
cp -f "${kernel_fs}" mnt/boot;
chkerr "Copying ${kernel_fs} failed.";
_dtbfile=${__rootfs_dir}/boot/${dtbfilename};
if [ -f "${_dtbfile}" ]; then
cp -f "${_dtbfile}" "mnt/boot/dtb/${dtbfilename}";
chkerr "populating ${_dtbfile} to ${__localsysfile}/boot/dtb failed.";
# Update FDT line into extlinux.conf if DTB file is specified
update_fdt_line "${__rootfs_dir}/boot/${_extlinux_conf}" "${dtbfilename}"; chkerr;
fi;
if [ "${__rootfs_type}" = "FAT32" ]; then
touch -f mnt/boot/cmdline.txt > /dev/null 2>&1;
chkerr "Creating cmdline.txt failed.";
echo -n -e "${__cmdline}" >mnt/boot/cmdline.txt;
chkerr "Writing cmdline.txt failed.";
else
pushd mnt > /dev/null 2>&1;
echo -n -e "\tpopulating rootfs from ${__rootfs_dir} ... ";
(cd "${__rootfs_dir}"; tar --xattrs --xattrs-include=* -cpf - *) \
| tar --xattrs --xattrs-include=* -xpf - ;
chkerr "Failed. Your APPSIZE might be too small.";
# Populate extlinux.conf if "$cmdline" exists
if [ "${__cmdline}" != "" ]; then
# Add the "$cmdline" at the APPEND line if it does not exist.
echo -n -e "\tpopulating /boot/${_extlinux_conf} ... ";
rootfs_found=$(grep -cE "${__cmdline}" "./boot/${_extlinux_conf}");
if [ "${rootfs_found}" == "0" ];then
sed -i "/^[ \t]*APPEND/s|\$| ${__cmdline}|" "./boot/${_extlinux_conf}";
chkerr;
fi;
fi;
if [ "${__do_sign}" = "True" ]; then
local kernel_fs_basename;
kernel_fs_basename=$(basename "${kernel_fs}");
echo -n -e "\tgenerating signed file of ${kernel_fs_basename} ... ";
uefi_signimage "./boot/${kernel_fs_basename}" "${uefi_db_key}" "${uefi_db_cert}" "nosplit" "True"; chkerr;
echo -n -e "\tgenerating sig file of ${dtbfilename} ... ";
uefi_signimage "./boot/dtb/${dtbfilename}" "${uefi_db_key}" "${uefi_db_cert}" "split" "True"; chkerr;
echo -n -e "\tgenerating sig file of initrd ... ";
uefi_signimage "./boot/initrd" "${uefi_db_key}" "${uefi_db_cert}" "split" "True"; chkerr;
echo -n -e "\tgenerating sig file of extlinux.conf ... ";
# Signing tool will pad extlinux.conf with 0x80 to be 16-byte aligned.
# This pad byte of 0x80 may cause some utilities fail to read the entire
# extlinux.conf.
# So, pad extlinux.conf to 16-byte aligned with linefeed.
pad_file_aligned "./boot/${_extlinux_conf}" 16 "\x0a";
# Note: no encryption to extlinux.conf file
uefi_signimage "./boot/${_extlinux_conf}" "${uefi_db_key}" "${uefi_db_cert}" "split" "False"; chkerr;
fi
popd > /dev/null 2>&1;
fi;
echo -e -n "\tSync'ing ${__localsysfile} ... ";
sync; sync; sleep 5; # Give FileBrowser time to terminate gracefully.
echo "done.";
umount mnt > /dev/null 2>&1;
losetup -d "${loop_dev}" > /dev/null 2>&1;
rmdir mnt > /dev/null 2>&1;
if [ "${__fillpat}" != "" -a -x mksparse ]; then
echo -e -n "\tConverting RAW image to Sparse image... ";
mv -f ${__localsysfile} ${__localsysfile}.raw;
if [ "${BLBlockSize}" != "" ]; then
blblksizeoption="-b $BLBlockSize";
fi;
./mksparse ${blblksizeoption} --fillpattern=${__fillpat} ${__localsysfile}.raw ${__localsysfile}; chkerr;
fi;
echo "${__localsysfile} built successfully. ";
}
get_fuse_level ()
{
local rcmcmd;
local inst_args="";
local idval_1="";
local idval_2="";
local flval="";
local baval="None";
local flvar="$1";
local hivar="$2";
local bavar="$3";
local minor="";
if [ -f "${BL_DIR}/tegrarcm_v2" ]; then
rcmcmd="tegrarcm_v2";
elif [ -f "${BL_DIR}/tegrarcm" ]; then
rcmcmd="tegrarcm";
else
echo "Error: tegrarcm is missing.";
exit 1;
fi;
if [ "${CHIPID}" = "0x23" ]; then
extra_args="--new_session --chip 0x23";
fi;
if [ -n "${usb_instance}" ]; then
inst_args="--instance ${usb_instance}";
fi;
pushd "${BL_DIR}" > /dev/null 2>&1;
ECID=$(./${rcmcmd} ${extra_args} ${inst_args} --uid | grep BR_CID | cut -d' ' -f2);
echo "ECID is ${ECID}"
popd > /dev/null 2>&1;
if [ "${ECID}" != "" ]; then
idval_1="0x${ECID:3:2}";
eval "${hivar}=\"${idval_1}\"";
idval_2="0x${ECID:6:2}";
flval="${ECID:2:1}";
baval="";
if [ "${idval_2}" = "0x23" ]; then
local tmp_1;
flval="0x${ECID:2:1}";
flval=$(printf %x "$((flval & 0x8))");
tmp_1=$(printf %2.2x "$((idval_1 & 0xf0))");
flval="${flval}${tmp_1}";
case ${flval} in
000) flval="fuselevel_nofuse"; ;;
800) flval="fuselevel_production"; baval="NS"; ;;
# 810 - 3K RSA
# 820 - ECDSA P-256
# 830 - ECDSA P-512
# 840 - ED25519
# 850 - XMSS
810|820|830|840|850)
flval="fuselevel_production"; baval="PKC"; ;;
# 890 - SBK + 3K RSA
# 8a0 - SBK + ECDSA P-256
# 8b0 - SBK + ECDSA P-512
# 8c0 - SBK + ED25519
# 8d0 - SBK + XMSS
890|8a0|8b0|8c0|8d0)
flval="fuselevel_production"; baval="SBKPKC"; ;;
*) echo "Error: Invalid fuse configuration 0x${flval}";
exit 1;
esac;
SKIPUID="--skipuid";
hwchipid="${idval_2}";
if [ "${minor}" != "" ]; then
hwchipid="${hwchipid} ${minor}";
fi;
hwchiprev="${ECID:5:1}";
elif [ "${idval_1}" = "0x21" ] || [ "${idval_1}" = "0x12" ] || \
[ "${idval_1}" = "0x00" ] && [ "${idval_2}" = "0x21" ]; then
case ${flval} in
0|1|2) flval="fuselevel_nofuse"; ;;
3) flval="fuselevel_production"; ;;
4) flval="fuselevel_production"; baval="NS"; ;;
5) flval="fuselevel_production"; baval="SBK"; ;;
6) flval="fuselevel_production"; baval="PKC"; ;;
*) flval="fuselevel_unknown"; ;;
esac;
SKIPUID="--skipuid";
if [ "${idval_1}" = "0x00" ]; then
eval "${hivar}=\"${idval_2}\"";
fi;
elif [ "${idval_1}" = "0x80" ]; then
if [ "${idval_2}" = "0x19" ]; then
case ${flval} in
0|1|2) flval="fuselevel_nofuse"; ;;
8) flval="fuselevel_production"; baval="NS"; ;;
# 9: 2K RSA, a: 3K RSA
9|a) flval="fuselevel_production"; baval="PKC"; ;;
# d: 2K RSA + SBK, e: 3K RSA + SBK
d|e) flval="fuselevel_production"; baval="SBKPKC"; ;;
esac;
SKIPUID="--skipuid";
hwchipid="${idval_2}";
hwchiprev="${ECID:5:1}";
fi
else
case ${flval} in
0|1|2) flval="fuselevel_nofuse"; ;;
8|c) flval="fuselevel_production"; baval="NS"; ;;
9|d) flval="fuselevel_production"; baval="SBK"; ;;
a) flval="fuselevel_production"; baval="PKC"; ;;
e) flval="fuselevel_production"; baval="SBKPKC"; ;;
*) flval="fuselevel_unknown"; ;;
esac;
fi;
eval "${flvar}=\"${flval}\"";
eval "${bavar}=\"${baval}\"";
fi;
}
function get_full_path ()
{
local val="$1";
local result="$2";
local fullpath;
fullpath=$(readlink -f ${val}); # null if path is invalid
if [ "${fullpath}" == "" ]; then
echo "Invalid path/filename ${val}";
exit 1;
fi;
eval "${result}=${fullpath}";
}
#
# XXX: This EEPROM read shall be replaced with new FAB agnostic function.
#
get_board_version ()
{
local args="";
local __board_id=$1;
local __board_FAB=$2;
local __board_sku=$3;
local __board_revision=$4;
local __emcfuse_bin=$5
local command="dump eeprom boardinfo cvm.bin"
local boardid;
local boardFAB;
local boardsku;
local boardrevision;
if [ -n "${usb_instance}" ]; then
args+="--instance ${usb_instance} ";
fi;
if [ "${CHIPMAJOR}" != "" ]; then
args+="--chip \"${CHIPID} ${CHIPMAJOR}\" ";
else
args+="--chip \"${CHIPID}\" ";
fi;
args+="--applet \"${LDK_DIR}/${SOSFILE}\" ";
args+="${SKIPUID} ";
SKIPUID="";
if [ "${CHIPID}" = "0x19" ]; then
mkfilesoft soft_fuses "${TARGET_DIR}/BCT/${SOFT_FUSES}";
cp2local soft_fuses "${BL_DIR}/${soft_fusesname}";
args+="--soft_fuses ${soft_fusesname} "
args+="--bins \"mb2_applet ${MB2APPLET}\" ";
command+=";reboot recovery"
elif [ "${CHIPID}" = "0x23" ]; then
command=""
if [ "${emcfuse}" != "" ]; then
chsuffix emcfusebin "${emcfusename}" "bin";
cp2local emcfuse "${BL_DIR}/${emcfusename}";
command+="readfuses ${emcfusebin} ${emcfusename}; ";
fi;
cp2local emc_fuse_dev_params "${BL_DIR}/${emc_fuse_dev_paramsname}";
if [ "${fuselevel}" = "fuselevel_production" ]; then
sed -i "s/preprod_dev_sign = <1>/preprod_dev_sign = <0>/" "${BL_DIR}/${emc_fuse_dev_paramsname}";
fi
args+="--cfg ${readinfofilename} "
command+="dump eeprom cvm cvm.bin; dump try_custinfo custinfo_out.bin;"
command+=" reboot recovery"
args+="--dev_params ${emc_fuse_dev_paramsname} ";
cp2local device_config "${BL_DIR}/${device_configname}"
cp2local misc_config "${BL_DIR}/${misc_configname}"
if [ -f "${uphy_config}" ]; then
cp2local uphy_config "${BL_DIR}/${uphy_configname}";
args+="--device_config ${device_configname} --misc_config ${misc_configname} --uphy_config ${uphy_configname} "
else
args+="--device_config ${device_configname} --misc_config ${misc_configname} "
fi;
args+="--bins \"mb2_applet ${mb2appletname}\" ";
fi
args+="--cmd \"${command}\" ";
local cmd="./tegraflash.py ${args}";
pushd "${BL_DIR}" > /dev/null 2>&1;
if [ "${keyfile}" != "" ]; then
cmd+="--key \"${keyfile}\" ";
fi;
if [ "${sbk_keyfile}" != "" ]; then
cmd+="--encrypt_key \"${sbk_keyfile}\" ";
if [ "${disable_random_iv}" = "True" ]; then
echo "Disable random IV"
cmd+="--disable_random_iv ";
fi
fi;
if [ -f "rcm_state" ]; then
rm rcm_state;
fi;
echo "${cmd}";
eval "${cmd}";
chkerr "Reading board information failed.";
if [ "${SKIP_EEPROM_CHECK}" = "" ]; then
boardid=$(./chkbdinfo -i cvm.bin);
boardFAB=$(./chkbdinfo -f cvm.bin);
boardsku=$(./chkbdinfo -k cvm.bin);
boardrevision=$(./chkbdinfo -r cvm.bin);
chkerr "Parsing board information failed.";
fi;
if [ "${BOARDID}" != "" ]; then
boardid="${BOARDID}";
fi;
if [ "${FAB}" != "" ]; then
boardFAB="${FAB}";
fi;
if [ "${BOARDSKU}" != "" ]; then
boardsku="${BOARDSKU}";
fi;
if [ "${BOARDREV}" != "" ]; then
boardrevision="${BOARDREV}";
fi;
popd > /dev/null 2>&1;
eval ${__board_id}="${boardid}";
eval ${__board_FAB}="${boardFAB}";
eval ${__board_sku}="${boardsku}";
eval ${__board_revision}="${boardrevision}";
if [ "${CHIPID}" = "0x23" ]; then
eval "${__emcfuse_bin}"="${emcfusebin}";
custinfofilename="custinfo_out.bin"
fi
}
#
# EEPROM get board S/N .
#
boardinfo_trk ()
{
local boardinforom;
local boardpartnu;
if [[ -e "${LDK_DIR}/nv_internal_trk.sh" &&
-e "${BL_DIR}/chkbdinfo" &&
-e "${BL_DIR}/cvm.bin" ]]; then
pushd "${BL_DIR}" > /dev/null 2>&1;
boardinforom=$(./chkbdinfo -a cvm.bin);
boardpartnu=$(./chkbdinfo -p cvm.bin);
if [[ "${boardinforom}" != "" ]] && [[ "${boardpartnu}" != "" ]]; then
eval "PRODUCT_OUT=\"${LDK_DIR}\" \"${LDK_DIR}/nv_internal_trk.sh\" \"${boardinforom}\" \"${boardpartnu}\"";
fi
popd > /dev/null 2>&1;
fi
}
#
# XXX: Read chip specifc details
#
get_chip_info_details ()
{
local args="";
local __chip_SKU=$1;
local __chip_minor_revision_ID=$2;
local __bootrom_revision_ID=$3;
local __ramcode_ID=$4;
local chipSKU;
local chipminorrevisionID;
local bootromrevisionID;
local ramcodeID;
pushd "${BL_DIR}" > /dev/null 2>&1 || return;
chipSKU=$(./chkbdinfo -C chip_info.bin_bak);
chipminorrevisionID=$(./chkbdinfo -M chip_info.bin_bak);
bootromrevisionID=$(./chkbdinfo -O chip_info.bin_bak);
ramcodeID=$(./chkbdinfo -R chip_info.bin_bak);
chkerr "Parsing chip_info.bin information failed.";
popd > /dev/null 2>&1 || return;
eval "${__chip_SKU}"="${chipSKU}";
eval "${__chip_minor_revision_ID}"="${chipminorrevisionID}";
eval "${__bootrom_revision_ID}"="${bootromrevisionID}";
eval "${__ramcode_ID}"="${ramcodeID}";
}
#
# SoC Sanity Check:
#
chk_soc_sanity ()
{
local mach_dir="generic";
local socname="Unknown";
local opmode="Unknown";
local disk_enc="";
if [ "${hwchipid}" = "" ]; then
# Nothing to check against. Just let it go.
echo "Error: probing the target board failed.";
echo " Make sure the target board is connected through ";
echo " USB port and is in recovery mode.";
exit 1;
fi;
#
# Print Target Board Information:
# NOTE: The list of board listed here may or may not be
# supported by the version of BSP(Board Support Package)
# that provides this copy of the script. This lists all
# of the publicly available Jetson developer platforms.
#
case ${hwchipid} in
0x19) socname="Tegra 194" ;;
0x23) socname="Tegra 234" ;;
esac;
case ${fuselevel} in
fuselevel_nofuse) opmode="pre-production"; ;;
fuselevel_production) opmode="production"; ;;
esac;
if [ ${disk_enc_enable} -eq 1 ]; then
disk_enc="enabled";
else
disk_enc="disabled";
fi;
echo "# Target Board Information:";
echo -n "# Name: ${ext_target_board}, Board Family: ${target_board}, ";
echo "SoC: ${socname}, ";
echo "# OpMode: ${opmode}, Boot Authentication: ${bootauth}, ";
echo "# Disk encryption: ${disk_enc} ,";
echo "${INFODIVIDER}";
if [ "${CHIPID}" != "" -a "${CHIPID}" != "${hwchipid}" ]; then
echo -n "Error: The Actual SoC ID(${hwchipid}) ";
echo -n "mismatches intended ${ext_target_board} ";
echo "SoC ID(${CHIPID}).";
exit 1;
fi;
if [ "${target_board}" != "${mach_dir}" ] && [ "${target_board}" != "${GENERIC}" ]; then
echo -n "Error: The Actual board family (${mach_dir}) ";
echo -n "mismatches intended ${ext_target_board} ";
echo "board family(${target_board}).";
exit 1;
fi;
case ${bootauth} in
PKC)
if [ "${keyfile}" = "" ] || [ "${sbk_keyfile}" != "" ]; then
echo -n "Error: Either RSA key file is not provided or SBK key ";
echo "file is provided for PKC protected target board.";