Repository navigation
Expand file tree
/
Copy pathcapsule.sh
More file actions
executable file
·969 lines (808 loc) · 29.2 KB
/
Copy pathcapsule.sh
File metadata and controls
executable file
·969 lines (808 loc) · 29.2 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
#!/bin/bash
set -e
edk_workspace=payloads/external/edk2/workspace
edk_basetools=${edk_workspace}/Dasharo/BaseTools
edk_tools=${edk_basetools}/BinWrappers/PosixLike
edk_scripts=${edk_basetools}/Scripts
# gets overwritten with gencap/GenerateCapsule if it exists and is executable,
# that directory is created by `box` subcommand and allows not depending on
# EDK2's repository for resigning
generate_capsule=${edk_tools}/GenerateCapsule
function die() {
echo error: "$@" 1>&2
exit 1
}
function confirm() {
local msg=$1
read -r -n1 -p "$msg [y/N] "
echo
if [ "$REPLY" != y ]; then
die "operation cancelled by the user"
fi
}
function print_banner() {
local msg=$1
echo
echo ========== "${msg^^*}" ==========
echo
}
function info() {
echo info: "$@"
}
function print_usage() {
echo "Usage: $(basename "$0") subcommand [subcommand-args...]"
echo
echo 'Subcommands:'
echo ' box export standalone GenerateCapsule out of EDK2'
echo ' help print this message'
echo ' keygen use OpenSSL to auto-generate test keys suitable for signing'
echo ' positional argument: directory-path'
echo ' make build a capsule, options:'
echo ' -k certs-dir (root.pub.pem, sub.pub.pem and sign.p12) AND/OR'
echo ' -t/-o/-s root/subroot/signing-certificate-file'
echo ' [-b] (include battery check DXE in the capsule)'
echo ' [-y] (overwrite destination without prompting)'
echo ' [-e ec-rom-file] (make an EC firmware capsule)'
echo ' [-c cap-file] (destination file name, generated if omitted)'
echo ' (later options override earlier ones)'
echo ' resign resign an existing capsule with a different key'
echo ' -k certs-dir (root.pub.pem, sub.pub.pem and sign.p12) AND/OR'
echo ' -t/-o/-s root/subroot/signing-certificate-file'
echo ' (later options override earlier ones)'
echo ' positional arguments: input-capsule output-capsule'
echo ' create_cabinet create a fwupd cabinet (.cab) from a capsule'
echo ' -e (make a cabinet for EC firmware)'
echo ' positional argument: capsule-file'
echo ' upload_lvfs upload a cabinet (.cab) to LVFS, options'
echo ' [-c credentials-file] (defaults to ~/.config/dasharo-credentials/lvfs)'
echo ' [-u lvfs-base-url] (defaults to $LVFS_URL)'
echo ' [-e email] (defaults to $LVFS_EMAIL)'
echo ' [-t token] (defaults to $LVFS_TOKEN)'
echo ' positional argument:'
echo ' cabinet-file (optional if exactly one .cab in current dir)'
}
function help_subcommand() {
print_usage
}
function source_coreboot_config() {
local config_file=${1-.config}
if [ ! -f "$config_file" ]; then
die "config file '$config_file' doesn't exist"
fi
local line
while read -r line; do
if ! eval "$line"; then
die "failed to source '.config'"
fi
done <<< "$(sed 's/\$(\([^)]\+\))/${\1}/g' $config_file)"
}
function require_capsule_support() {
if [ "$CONFIG_DRIVERS_EFI_UPDATE_CAPSULES" != y ]; then
die "current board configuration lacks support of update capsules"
fi
}
function keygen_subcommand() {
local dir=$1
if [ $# -ne 1 ]; then
echo "Usage: $(basename "$0") keygen keys-dir"
exit 1
fi
if [ -e "$dir" ]; then
confirm "OK to remove '$dir'?"
rm -r "$dir"
fi
mkdir "$dir"
cd "$dir"
# this is needed to make `openssl req` work non-interactively
cat > "openssl.cnf" << 'EOF'
.include /etc/ssl/openssl.cnf
[ CA_default ]
dir = ./test-ca
certs = $dir/certs
crl_dir = $dir/crl
database = $dir/index.txt
new_certs_dir = $dir/newcerts
certificate = $dir/cacert.pem
serial = $dir/serial
crlnumber = $dir/crlnumber
crl = $dir/crl.pem
private_key = $dir/private/cakey.pem
[ req_root ]
prompt = no
distinguished_name = req_root_dn
x509_extensions = v3_ca
string_mask = utf8only
[ req_root_dn ]
countryName = XX
stateOrProvinceName = Province
organizationName = Org
commonName = root
[ req_sub ]
prompt = no
distinguished_name = req_sub_dn
x509_extensions = v3_ca
string_mask = utf8only
[ req_sub_dn ]
countryName = XX
stateOrProvinceName = Province
organizationName = Org
commonName = sub
[ req_sign ]
prompt = no
distinguished_name = req_sign_dn
x509_extensions = v3_ca
string_mask = utf8only
[ req_sign_dn ]
countryName = XX
stateOrProvinceName = Province
organizationName = Org
commonName = sign
EOF
print_banner 'Making root certificate'
# make root certificate
openssl genrsa -out root.p8e 2048
openssl req -config openssl.cnf -section req_root -new -x509 -days 3650 -key root.p8e -out root.pub.pem
# dump certificate information like `openssl ca` does for completeness
openssl x509 -in root.pub.pem -text -certopt no_sigdump,no_pubkey -nocert
# create a CA
mkdir -p test-ca/newcerts
touch test-ca/index.txt
echo 01 > test-ca/serial
openssl x509 -in root.pub.pem -out root.cer -outform DER
python "${OLDPWD}/${edk_scripts}/BinToPcd.py" \
-p gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr \
-i root.cer -x -o CapsuleRootKey.inc
print_banner 'Making subroot certificate'
# make subroot certificate
openssl genrsa -out sub.p8e 2048
openssl req -config openssl.cnf -section req_sub -new -key sub.p8e -out sub.csr
yes | openssl ca -config openssl.cnf -extensions v3_ca -in sub.csr -days 3650 -cert root.pub.pem -keyfile root.p8e -notext -out sub.pub.pem
print_banner 'Making signing certificate'
# make signing certificate
openssl genrsa -out sign.p8e 2048
openssl req -config openssl.cnf -section req_sign -new -key sign.p8e -out sign.csr
yes | openssl ca -config openssl.cnf -in sign.csr -days 3650 -cert sub.pub.pem -keyfile sub.p8e -notext -out sign.crt
# create binary PKCS12 (certificate + private key) from signing certificate
openssl pkcs12 -export -passout pass: -inkey sign.p8e -in sign.crt -out sign.pfx
# now convert binary PKCS12 into PEM PKCS12 with no password
openssl pkcs12 -in sign.pfx -passin pass: -nodes -out sign.p12
print_banner 'Usage examples'
echo "Installing root certificate (before build):"
echo " cp $dir/CapsuleRootKey.inc ${edk_workspace}/Dasharo/DasharoPayloadPkg/"
echo
echo "Signing a capsule (after build):"
echo " $0 make -k $dir"
}
function check_cert() {
local name=$1
local path=$2
if [ -z "$path" ]; then
die "$name certificate wasn't provided"
fi
if [ ! -f "$path" ]; then
die "can't read $name certificate at '$path'"
fi
}
function check_generate_capsule() {
if [ -x gencap/GenerateCapsule ]; then
info "found 'gencap/GenerateCapsule'"
generate_capsule=gencap/GenerateCapsule
fi
if [ ! -x "$generate_capsule" ]; then
die "'${generate_capsule}' can't be executed"
fi
}
# This function assumes .config has been sourced.
function build_capsule() {
local payload=$1
local guid=$2
local version=$3
local lsv=$4
local -n drivers=$5
local -n certs=$6
local cap_file=$7
local v2_capsule=$8
local cap_flags=( --capflag PersistAcrossReset )
# Capsules on AMD boards do not survive resets
if [ "$CONFIG_EDK2_CAPSULE_DOES_NOT_SURVIVE_RESET" = y ]; then
cap_flags=()
fi
local json_file
json_file=$(mktemp --tmpdir --suffix -cap.json XXXXXXXX)
trap "$(printf 'rm -f -- %q %q' "$json_file" "$cap_file.inner")" EXIT
local opt_root_cert=${certs[root]}
local opt_sub_cert=${certs[sub]}
local opt_sign_cert=${certs[sign]}
if [ "$v2_capsule" = yes ]; then
# The inner capsule is always signed with the test key. Not signing it
# at all doesn't work because FmpDxe doesn't accept unsigned payloads at
# least due to Image->AuthInfo.Hdr.wRevision check in
# AuthenticateFmpImage().
opt_root_cert=${edk_basetools}/Source/Python/Pkcs7Sign/TestRoot.pub.pem
opt_sub_cert=${edk_basetools}/Source/Python/Pkcs7Sign/TestSub.pub.pem
opt_sign_cert=${edk_basetools}/Source/Python/Pkcs7Sign/TestCert.pem
fi
cat > "$json_file" << EOF
{
"EmbeddedDrivers": [
$(printf ' { "Driver": "%s" },\n' "${drivers[@]}" | sed '$s/,$//')
],
"Payloads": [
{
"Payload": "${payload}",
"Guid": "${guid}",
"FwVersion": "${version}",
"LowestSupportedVersion": "${lsv}",
"OpenSslSignerPrivateCertFile": "${opt_sign_cert}",
"OpenSslOtherPublicCertFile": "${opt_sub_cert}",
"OpenSslTrustedPublicCertFile": "${opt_root_cert}"
}
]
}
EOF
if [ "$v2_capsule" = yes ]; then
# The capsule created above is the inner capsule. Make it and then
# update JSON file to point at it as a payload.
if ! "$generate_capsule" --encode \
"${cap_flags[@]}" \
--json-file "$json_file" \
--output "$cap_file.inner"; then
die "GenerateCapsule failed"
fi
# The outer capsule is signed with the key passed by the user.
cat > "$json_file" << EOF
{
"EmbeddedDrivers": [],
"Payloads": [
{
"Payload": "$cap_file.inner",
"Guid": "${guid}",
"FwVersion": "${version}",
"LowestSupportedVersion": "${lsv}",
"OpenSslSignerPrivateCertFile": "${certs[sign]}",
"OpenSslOtherPublicCertFile": "${certs[sub]}",
"OpenSslTrustedPublicCertFile": "${certs[root]}"
}
]
}
EOF
fi
# Linux doesn't support InitiateReset flag, omitting it to rely on manual
# warm reset
if ! "$generate_capsule" --encode \
"${cap_flags[@]}" \
--json-file "$json_file" \
--output "$cap_file"; then
die "GenerateCapsule failed"
fi
}
# Prints version of an EC ROM as it appears in the file or dies.
function extract_raw_ec_version() {
local ec_rom_file=$1
local ver
ver=$(strings "$ec_rom_file" | sed -n '/^76EC_VERSION=./s/.*=//p' | head -1)
if [ -z "$ver" ]; then
die "Failed to extract raw 76EC_VERSION from '$ec_rom_file'"
fi
echo "$ver"
}
# Prints version of an EC ROM derived from the file or dies.
# The implementation of version conversion must be kept in sync with
# parse_ec_version() in src/drivers/efi/info.c
function extract_ec_version() {
local ec_rom_file=$1
local ver
ver=$(strings "$ec_rom_file" | \
sed -n '/^76EC_VERSION=./s/.*=\([0-9]\{4\}\(-[0-9][0-9]\)\{2\}\).*/\1/p' | \
head -1)
if [ -z "$ver" ]; then
die "Failed to extract 76EC_VERSION from '$ec_rom_file'"
fi
local y=${ver::4}
local m=${ver:5:2}
local d=${ver:8:2}
echo $(( ((10#$y & 0xffff) << 16) | ((10#$m & 0xff) << 8) | (10#$d & 0xff) ))
}
function make_subcommand() {
if [ ! -f .config ]; then
die "no '.config' file in current directory"
fi
if [ ! -f build/coreboot.rom ]; then
die "no 'build/coreboot.rom'; the firmware wasn't built?"
fi
if [ ! build/coreboot.rom -nt .config ]; then
die "'build/coreboot.rom' is not newer than .config'; need a re-build?"
fi
check_generate_capsule
source_coreboot_config
require_capsule_support
# Option names for key files match terminology of GenerateCapsule which,
# conveniently, has words starting with different letters:
# * t - trusted
# * o - other
# * s - signer
#
# -k can be combined with -t/-o/-s to override files, but order matters
local -A cap_certs
local include_battery_check overwrite_output ec_rom_file cap_file
while getopts "t:o:s:k:be:yc:" OPTION; do
case $OPTION in
t) cap_certs[root]="$OPTARG" ;;
o) cap_certs[sub]="$OPTARG" ;;
s) cap_certs[sign]="$OPTARG" ;;
k) cap_certs[root]="$OPTARG/root.pub.pem"
cap_certs[sub]="$OPTARG/sub.pub.pem"
cap_certs[sign]="$OPTARG/sign.p12" ;;
b) include_battery_check=1 ;;
y) overwrite_output=1 ;;
e) ec_rom_file="$OPTARG" ;;
c) cap_file="$OPTARG" ;;
*) exit 1 ;;
esac
done
check_cert root "${cap_certs[root]}"
check_cert sub "${cap_certs[sub]}"
check_cert sign "${cap_certs[sign]}"
# Assuming a coreboot capsule at first.
local rom_file=build/coreboot.rom
local guid=$CONFIG_DRIVERS_EFI_MAIN_FW_GUID
local splash_guid=E1CBE3CC-3D32-44CF-8DB9-2A78BA16F2F6
local version=$CONFIG_DRIVERS_EFI_MAIN_FW_VERSION
local lsv=$CONFIG_DRIVERS_EFI_MAIN_FW_LSV
if [ -n "$ec_rom_file" ]; then
if [ -z "$CONFIG_DRIVERS_EFI_EC_FW_GUID" ]; then
die '-e is passed but CONFIG_DRIVERS_EFI_EC_FW_GUID is empty'
fi
assert_file_exists "$ec_rom_file"
local ec_rom_version
ec_rom_version=$(extract_ec_version "$ec_rom_file")
local size
size=$(stat --printf %s "$ec_rom_file")
if [ "$size" -ne $(( $CONFIG_DRIVERS_EFI_EC_FW_SIZE )) ]; then
die "$(printf "'%s' is 0x%x bytes in size instead of 0x%x" \
"$ec_rom_file" \
"$size" \
"$CONFIG_DRIVERS_EFI_EC_FW_SIZE")"
fi
rom_file=$ec_rom_file
guid=$CONFIG_DRIVERS_EFI_EC_FW_GUID
splash_guid=8C675702-A60D-462C-B950-12F00FFDFFF3
version=$ec_rom_version
lsv=$CONFIG_DRIVERS_EFI_EC_FW_LSV
fi
if [ -z "$cap_file" ]; then
cap_file=${CONFIG_MAINBOARD_DIR//[\/-]/_}
if [[ ${CONFIG_MAINBOARD_PART_NUMBER} =~ DDR4 ]]; then
cap_file+=_ddr4
fi
if [ -n "$ec_rom_file" ]; then
cap_file+=_ec
fi
cap_file+=_${CONFIG_LOCALVERSION}
cap_file+=.cap
fi
if [ "$overwrite_output" != 1 ] && [ -e "$cap_file" ]; then
confirm "Overwrite already existing '$cap_file'?"
fi
local build_type
if [ "$CONFIG_EDK2_RELEASE" = y ]; then
build_type=RELEASE
else
build_type=DEBUG
fi
local toolchain=GCC
if [ -f "${edk_workspace}/../tools_def.txt" ]; then
# Must be older coreboot that defined its own toolchain.
toolchain=COREBOOT
fi
local build_dir=${edk_workspace}/Build/DasharoPayloadPkgX64/${build_type}_${toolchain}/X64
local embedded_drivers=()
# Ensure the charger check driver module comes first.
if [ "$include_battery_check" = 1 ]; then
embedded_drivers+=( "${build_dir}/CapsuleChargerCheckDxe.efi" )
fi
# EDK2's build system drops GUIDs when there is only one module of the same
# name, hence the checks.
if [ -f "${build_dir}/CapsuleSplashDxe.efi" ]; then
embedded_drivers+=( "${build_dir}/CapsuleSplashDxe.efi" )
else
# Can't use the same GUID more than once and can't easily derive it
# either, so CapsuleSplashDxe has hard-coded GUIDs for main/EC firmware
# and one of them is selected above.
embedded_drivers+=( "${build_dir}/CapsuleSplashDxe_${splash_guid}.efi" )
fi
if [ -f "${build_dir}/FmpDxe.efi" ]; then
embedded_drivers+=( "${build_dir}/FmpDxe.efi" )
else
embedded_drivers+=( "${build_dir}/FmpDxe_${guid}.efi" )
fi
# The format of a capsule must match the firmware that processes it. The
# V1->V2 transition keeps only the main firmware capsule in V1 format, so
# that the old firmware can apply it. An EC capsule is applied after the
# main firmware update, by V2 firmware, so it is always V2.
local v2_capsule=no
if [ "${CONFIG_EDK2_CAPSULES_V2:-n}" = y ]; then
if [ "${CONFIG_EDK2_CAPSULES_V2_TRANSITION:-n}" != y ] ||
[ -n "$ec_rom_file" ]; then
v2_capsule=yes
fi
fi
build_capsule "$rom_file" "$guid" "$version" "$lsv" \
embedded_drivers cap_certs \
"$cap_file" "$v2_capsule"
echo "Created the capsule at '$cap_file'"
}
function assert_file_exists() {
local path=$1
if [ ! -f "$path" ]; then
die "File '$path' not found"
fi
}
function assert_file_is_a_capsule() {
local path=$1
local fmp_guid_bytes_hex=edd5cb6d2de8444cbda17194199ad92a
if [ "$(xxd -l 16 -ps "$path")" != "$fmp_guid_bytes_hex" ]; then
die "'$path' is not an FMP capsule file"
fi
}
function assert_command_exists() {
local cmd=$1
if ! command -v "$cmd" >/dev/null 2>&1; then
die "'$cmd' not found in PATH"
fi
}
function decode_capsule() {
local tmp_dir=$1
local capsule=$2
local -n result=$3
"$generate_capsule" --decode "$capsule" \
--output "$tmp_dir/decoded"
local json_file="$tmp_dir/decoded.json"
result["fw_version"]=$(jq -r '.Payloads[0].FwVersion' "$json_file")
result["guid"]=$(jq -r '.Payloads[0].Guid' "$json_file")
result["lowest_supported_version"]=$(jq -r '.Payloads[0].LowestSupportedVersion' "$json_file")
result["payload"]=$(jq -r '.Payloads[0].Payload' "$json_file")
local drivers
drivers=$(ls -v "$tmp_dir"/decoded.EmbeddedDriver* 2>/dev/null | while read -r f; do
printf ' { "Driver": "%s" },\n' "$f"
done | sed '$s/,$//')
result["drivers"]="$drivers"
result["flags"]=$("$generate_capsule" --dump-info "$capsule" |
sed -nE '/CAPSULE_FLAGS_/{s/.*CAPSULE_FLAGS_//; s/_?([A-Z])([A-Z]*)/\1\L\2/g; p}')
}
function encode_capsule() {
local tmp_dir=$1
local cap_out=$2
local -n json_data=$3
cat > "$tmp_dir/cap.json" <<EOF
{
"EmbeddedDrivers": [
${json_data["drivers"]}
],
"Payloads": [
{
"Payload": "${json_data["payload"]}",
"Guid": "${json_data["guid"]}",
"FwVersion": "${json_data["fw_version"]}",
"LowestSupportedVersion": "${json_data["lowest_supported_version"]}",
"OpenSslSignerPrivateCertFile": "${json_data["sign_cert"]}",
"OpenSslOtherPublicCertFile": "${json_data["sub_cert"]}",
"OpenSslTrustedPublicCertFile": "${json_data["root_cert"]}"
}
]
}
EOF
local cap_flags
mapfile -t cap_flags < <(echo "${json_data["flags"]}" | xargs -r printf '--capflag\n%s\n')
"$generate_capsule" --encode \
"${cap_flags[@]}" \
--json-file "$tmp_dir/cap.json" \
--output "$cap_out"
}
function resign_subcommand() {
check_generate_capsule
local root_cert sub_cert sign_cert
OPTIND=1
while getopts "t:o:s:k:" OPTION; do
case $OPTION in
t) root_cert="$OPTARG" ;;
o) sub_cert="$OPTARG" ;;
s) sign_cert="$OPTARG" ;;
k) root_cert="$OPTARG/root.pub.pem"
sub_cert="$OPTARG/sub.pub.pem"
sign_cert="$OPTARG/sign.p12" ;;
*) exit 1 ;;
esac
done
shift $((OPTIND - 1))
if [ $# -ne 2 ]; then
die "Incorrect number of positional parameters: $# (expected: 2)"
fi
local in_capsule=$1
local out_capsule=$2
assert_file_exists "$in_capsule"
assert_file_is_a_capsule "$in_capsule"
if [ -e "$out_capsule" ]; then
confirm "Overwrite already existing '$out_capsule'?"
fi
assert_command_exists jq
local tmp_dir
tmp_dir=$(mktemp --tmpdir --directory --suffix -cap XXXXXXXX)
trap "$(printf 'rm -r -- %q' "$tmp_dir")" EXIT
check_cert root "$root_cert"
check_cert sub "$sub_cert"
check_cert sign "$sign_cert"
local -A metadata
decode_capsule "$tmp_dir" "$in_capsule" metadata
metadata[sign_cert]=$sign_cert
metadata[sub_cert]=$sub_cert
metadata[root_cert]=$root_cert
encode_capsule "$tmp_dir" "$out_capsule" metadata
}
# Prints a 32-bit firmware version (0xMMmmpprr) as major.minor.patch, which
# is how fwupd shows the system firmware version with the dell-bios-msb format
# set in the system firmware metadata. The rc byte is left out: fwupd compares
# a "-rcN" suffix as greater than no suffix, so a requirement on
# "1.0.2-rc3" would fail even on that firmware.
function format_fw_version() {
local fw_version=$(( $1 ))
printf '%d.%d.%d\n' \
$(( (fw_version >> 24) & 0xff )) \
$(( (fw_version >> 16) & 0xff )) \
$(( (fw_version >> 8) & 0xff ))
}
function create_cabinet_subcommand() {
local ec_fw
OPTIND=1
while getopts "e" OPTION; do
case $OPTION in
e) ec_fw="yes" ;;
*) exit 1 ;;
esac
done
shift $((OPTIND - 1))
if [ $# -ne 1 ]; then
die "Incorrect number of positional parameters: $# (expected: 1)"
fi
local capsule=$1
local cabinet="${capsule%.cap}.cab"
if [ -z "$capsule" ]; then
die "No input capsule specified"
fi
assert_file_exists "$capsule"
assert_file_is_a_capsule "$capsule"
assert_command_exists fwupdtool
source_coreboot_config
require_capsule_support
local date vendor version
date=$(stat -c %w "$capsule" 2>/dev/null | cut -d ' ' -f 1)
if [ -z "$date" ] || [ "$date" = "-" ]; then
date=$(stat -c %y "$capsule" 2>/dev/null | cut -d ' ' -f 1)
fi
vendor=$(grep -e "CONFIG_VENDOR_.*=y" .config | cut -d '=' -f 1 | cut -d '_' -f 3- | awk '{ print tolower($0) }')
version=$(echo "$CONFIG_LOCALVERSION" | tr -d 'v' | cut -d '-' -f 1)
local archive_dir
archive_dir=$(mktemp --tmpdir -d XXXXXXXX)
trap 'rm -rf -- "$archive_dir"' EXIT
local fw_guid=$CONFIG_DRIVERS_EFI_MAIN_FW_GUID
local fw_kind=system
local name_suffix=
local category=X-System
local version_formats=( quad dell-bios-msb )
local tag=$CONFIG_LOCALVERSION
local requires=
if [ "$ec_fw" = yes ]; then
if [ -z "$CONFIG_DRIVERS_EFI_EC_FW_GUID" ]; then
die '-e is passed but CONFIG_DRIVERS_EFI_EC_FW_GUID is empty'
fi
fw_guid=$CONFIG_DRIVERS_EFI_EC_FW_GUID
fw_kind=EC
name_suffix=' EC'
category=X-EmbeddedController
version_formats=( hex )
# A V2 EC capsule can only be applied by V2 firmware, so require a V2
# system firmware to be installed first. In a transitional build,
# this build's system firmware is the first V2 one. Otherwise, the
# lowest supported version is.
if [ "${CONFIG_EDK2_CAPSULES_V2:-n}" = y ]; then
local min_fw_version=$CONFIG_DRIVERS_EFI_MAIN_FW_LSV
if [ "${CONFIG_EDK2_CAPSULES_V2_TRANSITION:-n}" = y ]; then
min_fw_version=$CONFIG_DRIVERS_EFI_MAIN_FW_VERSION
fi
requires=$(printf ' <requires>\n <firmware compare="ge" version="%s">%s</firmware>\n </requires>' \
"$(format_fw_version "$min_fw_version")" \
"$CONFIG_DRIVERS_EFI_MAIN_FW_GUID")
fi
# extract[_raw]_ec_version() work on capsules too.
tag=$(extract_raw_ec_version "$capsule")
version=$(extract_ec_version "$capsule")
fi
local version_formats_str
version_formats_str=$(printf ' <value key="LVFS::VersionFormat">%s</value>\n' "${version_formats[@]}")
local id=com.${vendor}.${CONFIG_MAINBOARD_SMBIOS_PRODUCT_NAME}.${CONFIG_MAINBOARD_VERSION}.${fw_kind,,*}.firmware
id=${id// /_}
id=${id////_}
cat > "${archive_dir}/firmware.metainfo.xml" << EOF
<?xml version='1.0' encoding='utf-8'?>
<component type="firmware">
<id>${id}</id>
<name>${CONFIG_MAINBOARD_SMBIOS_PRODUCT_NAME}${name_suffix}</name>
<summary>${CONFIG_MAINBOARD_SMBIOS_PRODUCT_NAME} ${CONFIG_MAINBOARD_VERSION} ${fw_kind} firmware</summary>
<description>
<p>Dasharo ${CONFIG_MAINBOARD_SMBIOS_PRODUCT_NAME} ${CONFIG_MAINBOARD_VERSION} ${fw_kind} firmware</p>
</description>
<provides>
<firmware type="flashed">${fw_guid}</firmware>
</provides>
${requires}
<url type="homepage">https://docs.dasharo.com/</url>
<metadata_license>CC0-1.0</metadata_license>
<project_license>LicenseRef-proprietary</project_license>
<categories>
<category>${category}</category>
</categories>
<custom>
${version_formats_str}
<value key="LVFS::UpdateProtocol">org.uefi.capsule</value>
</custom>
<releases>
<release version="${version}" date="${date}" tag="${tag}" urgency="high">
<checksum filename="firmware.bin" target="content"/>
</release>
</releases>
</component>
EOF
cp "$capsule" "${archive_dir}/firmware.bin"
pushd "$archive_dir" >/dev/null
fwupdtool build-cabinet update.cab firmware.bin firmware.metainfo.xml
popd >/dev/null
mv "${archive_dir}/update.cab" "${cabinet}"
echo "File ${cabinet} created"
}
function box_subcommand() {
local src=${edk_basetools}/Source/Python
local dst=gencap
local keys=${src}/Pkcs7Sign
if [ -e "$dst" ]; then
confirm "Overwrite already existing '$dst'?"
rm -r "${dst}"
fi
info "using '${src}'"
info "constructing a standalone version in '${dst}'"
mkdir -p "${dst}"/{Common,keys}
cp "${src}/Capsule/GenerateCapsule.py" "${dst}"
cp -r "${src}/Common/Edk2" "${dst}/Common"
cp -r "${src}/Common/Uefi" "${dst}/Common"
info "using keys from '${keys}'"
cp "$keys"/TestRoot.pub.pem "${dst}/keys/root.pub.pem"
cp "$keys"/TestSub.pub.pem "${dst}/keys/sub.pub.pem"
cp "$keys"/TestCert.pem "${dst}/keys/sign.crt"
cat > "${dst}/GenerateCapsule" <<'EOF'
#!/usr/bin/env bash
python=python
if command -v python3 >/dev/null; then
python=python3
elif command -v python2 >/dev/null; then
python=python2
fi
dir=$(dirname "${BASH_SOURCE:-$0}")
exec "${python}" "${dir}/GenerateCapsule.py" "$@"
EOF
chmod +x "${dst}/GenerateCapsule"
print_banner 'Help'
echo "Location of capsule signing keys:"
echo " ${dst}/keys/"
echo
echo "Usage examples:"
echo " ${dst}/GenerateCapsule --help"
echo " ${dst}/GenerateCapsule --output decoded --decode coreboot.cap"
}
function upload_lvfs_subcommand() {
local default_creds_file creds_file opt_creds_file opt_url opt_email opt_token
local env_email env_token env_url
local email token base_url cabinet
local url response status body curl_rc
default_creds_file="${XDG_CONFIG_HOME:-$HOME/.config}/dasharo-credentials/lvfs"
creds_file="$default_creds_file"
opt_creds_file=""
opt_url=""
opt_email=""
opt_token=""
OPTIND=1
while getopts "c:u:e:t:" OPTION; do
case $OPTION in
c) opt_creds_file="$OPTARG" ;;
u) opt_url="$OPTARG" ;;
e) opt_email="$OPTARG" ;;
t) opt_token="$OPTARG" ;;
*) exit 1 ;;
esac
done
shift $((OPTIND - 1))
cabinet="$1"
if [ -n "$opt_creds_file" ]; then
creds_file="$opt_creds_file"
fi
env_email="${LVFS_EMAIL-}"
env_token="${LVFS_TOKEN-}"
env_url="${LVFS_URL-}"
if [ -r "$creds_file" ]; then
# shellcheck disable=SC1090
. "$creds_file"
fi
if [ -n "$env_email" ]; then
LVFS_EMAIL="$env_email"
fi
if [ -n "$env_token" ]; then
LVFS_TOKEN="$env_token"
fi
if [ -n "$env_url" ]; then
LVFS_URL="$env_url"
fi
email="${LVFS_EMAIL-}"
token="${LVFS_TOKEN-}"
base_url="${LVFS_URL-https://fwupd.org}"
if [ -n "$opt_email" ]; then
email="$opt_email"
fi
if [ -n "$opt_token" ]; then
token="$opt_token"
fi
if [ -n "$opt_url" ]; then
base_url="$opt_url"
fi
if [ -z "$cabinet" ]; then
set -- ./*.cab
if [ ! -e "$1" ]; then
die "No cabinet specified and no .cab found in current directory"
fi
if [ $# -ne 1 ]; then
die "Multiple .cab files found in current directory, specify the cabinet path explicitly"
fi
cabinet="$1"
fi
assert_file_exists "$cabinet"
if [ -z "$email" ]; then
die "LVFS email is not set. Put LVFS_EMAIL into '$creds_file' or pass -e"
fi
if [ -z "$token" ]; then
die "LVFS token is not set. Put LVFS_TOKEN into '$creds_file' or pass -t"
fi
assert_command_exists curl
url="${base_url%/}/lvfs/upload/token"
set +e
response=$(curl -sS -X POST \
--connect-timeout 10 --max-time 300 \
--retry 3 --retry-delay 2 --retry-connrefused \
-F "file=@${cabinet}" \
--user "${email}:${token}" \
-w "\n%{http_code}" \
"$url")
curl_rc=$?
set -e
if [ "$curl_rc" -ne 0 ]; then
die "curl failed with exit code $curl_rc"
fi
status="${response##*$'\n'}"
body="${response%$'\n'*}"
if [ "$status" -lt 200 ] || [ "$status" -ge 300 ]; then
echo "$body" 1>&2
die "LVFS upload failed with HTTP status $status"
fi
echo "$body"
}
if [ $# -eq 0 ]; then
print_usage
exit 1
fi
subcommand=$1
shift
case "$subcommand" in
box|help|keygen|make|resign|create_cabinet|upload_lvfs)
"$subcommand"_subcommand "$@" ;;
*)
echo "Unexpected subcommand: $subcommand"
echo
print_usage
exit 1 ;;
esac