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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
|
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#
from oeqa.selftest.case import OESelftestTestCase
from oeqa.utils.commands import runCmd, bitbake, get_bb_vars
import os
import re
import shlex
import logging
import pprint
class FitImageTestCase(OESelftestTestCase):
"""Test functions usable for testing kernel-fitimage.bbclass and uboot-sign.bbclass
A brief summary showing the structure of a test case:
self._test_fitimage()
# Generate a local.conf file and bitbake the bootloader or the kernel
self._bitbake_fit_image()
# Check if the its file contains the expected paths and attributes.
# The _get_req_* functions are implemented by more specific chield classes.
self._check_its_file()
req_its_paths = self._get_req_its_paths()
req_sigvalues_config = self._get_req_sigvalues_config()
req_sigvalues_image = self._get_req_sigvalues_image()
# Compare the its file against req_its_paths, req_sigvalues_config, req_sigvalues_image
# Call the dumpimage utiliy and check that it prints all the expected paths and attributes
# The _get_req_* functions are implemented by more specific chield classes.
self._check_fitimage()
self._get_req_sections()
# Compare the output of the dumpimage utility against
"""
MKIMAGE_HASH_LENGTHS = { 'sha256': 64, 'sha384': 96, 'sha512': 128 }
MKIMAGE_SIGNATURE_LENGTHS = { 'rsa2048': 512 }
def _gen_signing_key(self, bb_vars):
"""Generate a key pair and a singing certificate
Generate a UBOOT_SIGN_KEYNAME in the UBOOT_SIGN_KEYDIR similar to what
the FIT_GENERATE_KEYS feature does. However, having a static key is
probably a more realistic use case than generating a random key with
each clean build. So this needs to be tested as well.
The FIT_GENERATE_KEYS generates 2 keys: The UBOOT_SIGN_KEYNAME and the
UBOOT_SIGN_IMG_KEYNAME. The UBOOT_SIGN_IMG_KEYNAME is used by the
FIT_SIGN_INDIVIDUAL feature only. Testing if everything is working if
there is only one key available is important as well. Therefore this
function generates only the keys which are really needed, not just two.
"""
# Define some variables which are usually defined by the kernel-fitimage.bbclass.
# But for testing purpose check if the uboot-sign.bbclass is independent from
# the kernel-fitimage.bbclass
fit_sign_numbits = bb_vars['FIT_SIGN_NUMBITS'] or "2048"
fit_key_genrsa_args = bb_vars['FIT_KEY_GENRSA_ARGS'] or "-F4"
fit_key_req_args = bb_vars['FIT_KEY_REQ_ARGS'] or "-batch -new"
fit_key_sign_pkcs = bb_vars['FIT_KEY_SIGN_PKCS'] or "-x509"
uboot_sign_keydir = bb_vars['UBOOT_SIGN_KEYDIR']
sign_keys = [bb_vars['UBOOT_SIGN_KEYNAME']]
if bb_vars['FIT_SIGN_INDIVIDUAL'] == "1":
sign_keys.append(bb_vars['UBOOT_SIGN_IMG_KEYNAME'])
for sign_key in sign_keys:
sing_key_path = os.path.join(uboot_sign_keydir, sign_key)
if not os.path.isdir(uboot_sign_keydir):
os.makedirs(uboot_sign_keydir)
openssl_bindir = FitImageTestCase._setup_native('openssl-native')
openssl_path = os.path.join(openssl_bindir, 'openssl')
runCmd("%s genrsa %s -out %s.key %s" % (
openssl_path,
fit_key_genrsa_args,
sing_key_path,
fit_sign_numbits
))
runCmd("%s req %s %s -key %s.key -out %s.crt" % (
openssl_path,
fit_key_req_args,
fit_key_sign_pkcs,
sing_key_path,
sing_key_path
))
@staticmethod
def _gen_random_file(file_path, num_bytes=65536):
with open(file_path, 'wb') as file_out:
file_out.write(os.urandom(num_bytes))
@staticmethod
def _setup_native(native_recipe):
"""Build a native recipe and return the path to its bindir in RECIPE_SYSROOT_NATIVE"""
bitbake(native_recipe + " -c addto_recipe_sysroot")
vars = get_bb_vars(['RECIPE_SYSROOT_NATIVE', 'bindir'], native_recipe)
return os.path.join(vars['RECIPE_SYSROOT_NATIVE'], vars['bindir'])
def _verify_fit_image_signature(self, uboot_tools_bindir, fitimage_path, dtb_path, conf_name=None):
"""Verify the signature of a fit configuration
The fit_check_sign utility from u-boot-tools-native is called.
uboot-fit_check_sign -f fitImage -k $dtb_path -c conf-$dtb_name
dtb_path refers to a binary device tree containing the public key.
"""
fit_check_sign_path = os.path.join(uboot_tools_bindir, 'uboot-fit_check_sign')
cmd = '%s -f %s -k %s' % (fit_check_sign_path, fitimage_path, dtb_path)
if conf_name:
cmd += ' -c %s' % conf_name
result = runCmd(cmd)
self.logger.debug("%s\nreturned: %s\n%s", cmd, str(result.status), result.output)
self.assertIn("Signature check OK", result.output)
@staticmethod
def _find_string_in_bin_file(file_path, search_string):
"""find strings in a binary file
Shell equivalent: strings "$1" | grep "$2" | wc -l
return number of matches
"""
found_positions = 0
with open(file_path, 'rb') as file:
content = file.read().decode('ascii', errors='ignore')
found_positions = content.count(search_string)
return found_positions
@staticmethod
def _get_uboot_mkimage_sign_args(uboot_mkimage_sign_args):
"""Retrive the string passed via -c to the mkimage command
Example: If a build configutation defines
UBOOT_MKIMAGE_SIGN_ARGS = "-c 'a smart comment'"
this function returns "a smart comment"
"""
a_comment = None
if uboot_mkimage_sign_args:
mkimage_args = shlex.split(uboot_mkimage_sign_args)
try:
c_index = mkimage_args.index('-c')
a_comment = mkimage_args[c_index+1]
except ValueError:
pass
return a_comment
@staticmethod
def _get_dtb_files(bb_vars):
kernel_devicetree = bb_vars['KERNEL_DEVICETREE'] or ""
if kernel_devicetree:
return [os.path.basename(dtb) for dtb in kernel_devicetree.split()]
return []
def _is_req_dict_in_dict(self, found_dict, req_dict):
"""
Check if all key-value pairs in the required dictionary are present in the found dictionary.
This function recursively checks if the required dictionary (`req_dict`) is a subset of the found dictionary (`found_dict`).
It supports nested dictionaries, strings, lists, and sets as values.
Args:
found_dict (dict): The dictionary to search within.
req_dict (dict): The dictionary containing the required key-value pairs.
"""
for key, value in req_dict.items():
self.assertIn(key, found_dict)
if isinstance(value, dict):
self._is_req_dict_in_dict(found_dict[key], value)
elif isinstance(value, str):
self.assertIn(value, found_dict[key])
elif isinstance(value, list):
self.assertLessEqual(set(value), set(found_dict[key]))
elif isinstance(value, set):
self.assertLessEqual(value, found_dict[key])
else:
self.assertEqual(value, found_dict[key])
def _check_its_file(self, bb_vars, its_file_path):
"""Check if the its file contains the expected sections and fields"""
# print the its file for debugging
if logging.DEBUG >= self.logger.level:
with open(its_file_path) as its_file:
self.logger.debug("its file: %s" % its_file.read())
# Generate a list of expected paths in the its file
req_its_paths = self._get_req_its_paths(bb_vars)
self.logger.debug("req_its_paths:\n%s\n" % pprint.pformat(req_its_paths, indent=4))
# Generate a dict of expected configuration signature nodes
req_sigvalues_config = self._get_req_sigvalues_config(bb_vars)
self.logger.debug("req_sigvalues_config:\n%s\n" % pprint.pformat(req_sigvalues_config, indent=4))
# Generate a dict of expected image signature nodes
req_sigvalues_image = self._get_req_sigvalues_image(bb_vars)
self.logger.debug("req_sigvalues_image:\n%s\n" % pprint.pformat(req_sigvalues_image, indent=4))
# Parse the its file for paths and signatures
its_path = []
its_paths = []
linect = 0
sigs = {}
with open(its_file_path) as its_file:
for line in its_file:
linect += 1
line = line.strip()
if line.endswith('};'):
its_path.pop()
elif line.endswith('{'):
its_path.append(line[:-1].strip())
its_paths.append(its_path[:])
# kernel-fitimage uses signature-1, uboot-sign uses signature
elif its_path and (its_path[-1] == 'signature-1' or its_path[-1] == 'signature'):
itsdotpath = '.'.join(its_path)
if not itsdotpath in sigs:
sigs[itsdotpath] = {}
if not '=' in line or not line.endswith(';'):
self.fail('Unexpected formatting in %s sigs section line %d:%s' % (its_file_path, linect, line))
key, value = line.split('=', 1)
sigs[itsdotpath][key.rstrip()] = value.lstrip().rstrip(';')
# Check if all expected paths are found in the its file
self.logger.debug("itspaths:\n%s\n" % pprint.pformat(its_paths, indent=4))
for req_path in req_its_paths:
if not req_path in its_paths:
self.fail('Missing path in its file: %s (%s)' % (req_path, its_file_path))
# Check if all the expected singnature nodes (images and configurations) are found
self.logger.debug("sigs:\n%s\n" % pprint.pformat(sigs, indent=4))
if req_sigvalues_config or req_sigvalues_image:
for its_path, values in sigs.items():
if 'conf-' in its_path:
reqsigvalues = req_sigvalues_config
else:
reqsigvalues = req_sigvalues_image
for reqkey, reqvalue in reqsigvalues.items():
value = values.get(reqkey, None)
if value is None:
self.fail('Missing key "%s" in its file signature section %s (%s)' % (reqkey, its_path, its_file_path))
self.assertEqual(value, reqvalue)
# Generate a list of expected fields in the its file
req_its_fields = self._get_req_its_fields(bb_vars)
self.logger.debug("req_its_fields:\n%s\n" % pprint.pformat(req_its_fields, indent=4))
# Check if all expected fields are in the its file
if req_its_fields:
field_index = 0
field_index_last = len(req_its_fields) - 1
with open(its_file_path) as its_file:
for line in its_file:
if req_its_fields[field_index] in line:
if field_index < field_index_last:
field_index +=1
else:
break
self.assertEqual(field_index, field_index_last,
"Fields in Image Tree Source File %s did not match, error in finding %s"
% (its_file_path, req_its_fields[field_index]))
def _check_fitimage(self, bb_vars, fitimage_path, uboot_tools_bindir):
"""Run dumpimage on the final FIT image and parse the output into a dict"""
dumpimage_path = os.path.join(uboot_tools_bindir, 'dumpimage')
cmd = '%s -l %s' % (dumpimage_path, fitimage_path)
self.logger.debug("Analyzing output from dumpimage: %s" % cmd)
dumpimage_result = runCmd(cmd)
in_section = None
sections = {}
self.logger.debug("dumpimage output: %s" % dumpimage_result.output)
for line in dumpimage_result.output.splitlines():
# Find potentially hashed and signed sections
if line.startswith((' Configuration', ' Image')):
in_section = re.search(r'\((.*)\)', line).groups()[0]
# Key value lines start with two spaces otherwise the section ended
elif not line.startswith(" "):
in_section = None
# Handle key value lines of this section
elif in_section:
if not in_section in sections:
sections[in_section] = {}
try:
key, value = line.split(':', 1)
key = key.strip()
value = value.strip()
except ValueError as val_err:
self.logger.debug("dumpimage debug: %s = %s" % (key, line))
# Handle multiple entries as e.g. for Loadables as a list
if key and line.startswith(" "):
value = sections[in_section][key] + "," + line.strip()
else:
raise ValueError(f"Error processing line: '{line}'. Original error: {val_err}")
sections[in_section][key] = value
# Check if the requested dictionary is a subset of the parsed dictionary
req_sections, num_signatures = self._get_req_sections(bb_vars)
self.logger.debug("req_sections: \n%s\n" % pprint.pformat(req_sections, indent=4))
self.logger.debug("dumpimage sections: \n%s\n" % pprint.pformat(sections, indent=4))
self._is_req_dict_in_dict(sections, req_sections)
# Call the signing related checks if the function is provided by a inherited class
self._check_signing(bb_vars, sections, num_signatures, uboot_tools_bindir, fitimage_path)
def _get_req_its_paths(self, bb_vars):
self.logger.error("This function needs to be implemented")
return []
def _get_req_its_fields(self, bb_vars):
self.logger.error("This function needs to be implemented")
return []
def _get_req_sigvalues_config(self, bb_vars):
self.logger.error("This function needs to be implemented")
return {}
def _get_req_sigvalues_image(self, bb_vars):
self.logger.error("This function needs to be implemented")
return {}
def _get_req_sections(self, bb_vars):
self.logger.error("This function needs to be implemented")
return ({}, 0)
def _check_signing(self, bb_vars, sections, num_signatures, uboot_tools_bindir, fitimage_path):
"""Verify the signatures in the FIT image."""
self.fail("Function needs to be implemented by inheriting classes")
def _bitbake_fit_image(self, bb_vars):
"""Bitbake the FIT image and return the paths to the its file and the FIT image"""
self.fail("Function needs to be implemented by inheriting classes")
def _test_fitimage(self, bb_vars):
"""Check if the its file and the FIT image are created and signed correctly"""
fitimage_its_path, fitimage_path = self._bitbake_fit_image(bb_vars)
self.assertExists(fitimage_its_path, "%s image tree source doesn't exist" % (fitimage_its_path))
self.assertExists(fitimage_path, "%s FIT image doesn't exist" % (fitimage_path))
self.logger.debug("Checking its: %s" % fitimage_its_path)
self._check_its_file(bb_vars, fitimage_its_path)
# Setup u-boot-tools-native
uboot_tools_bindir = FitImageTestCase._setup_native('u-boot-tools-native')
# Verify the FIT image
self._check_fitimage(bb_vars, fitimage_path, uboot_tools_bindir)
class KernelFitImageTests(FitImageTestCase):
"""Test cases for the kernel-fitimage bbclass"""
def _fit_get_bb_vars(self, additional_vars=[]):
"""Retrieve BitBake variables specific to the test case.
Call the get_bb_vars function once and get all variables needed by the test case.
"""
internal_used = {
'DEPLOY_DIR_IMAGE',
'FIT_DESC',
'FIT_HASH_ALG',
'FIT_KERNEL_COMP_ALG',
'FIT_SIGN_ALG',
'FIT_SIGN_INDIVIDUAL',
'FIT_UBOOT_ENV',
'INITRAMFS_IMAGE_BUNDLE',
'INITRAMFS_IMAGE_NAME',
'INITRAMFS_IMAGE',
'KERNEL_DEVICETREE',
'KERNEL_FIT_LINK_NAME',
'MACHINE',
'UBOOT_ARCH',
'UBOOT_ENTRYPOINT',
'UBOOT_LOADADDRESS',
'UBOOT_MKIMAGE_KERNEL_TYPE',
'UBOOT_MKIMAGE_SIGN_ARGS',
'UBOOT_RD_ENTRYPOINT',
'UBOOT_RD_LOADADDRESS',
'UBOOT_SIGN_ENABLE',
'UBOOT_SIGN_IMG_KEYNAME',
'UBOOT_SIGN_KEYDIR',
'UBOOT_SIGN_KEYNAME',
}
bb_vars = get_bb_vars(list(internal_used | set(additional_vars)), "virtual/kernel")
return bb_vars
def _config_add_uboot_env(self, config):
"""Generate an u-boot environment
Create a boot.cmd file that is packed into the FIT image as a source-able text file.
Updates the configuration to include the boot.cmd file.
"""
fit_uenv_file = "boot.cmd"
test_files_dir = "test-files"
fit_uenv_path = os.path.join(self.builddir, test_files_dir, fit_uenv_file)
config += '# Add an u-boot script to the fitImage' + os.linesep
config += 'FIT_UBOOT_ENV = "%s"' % fit_uenv_file + os.linesep
config += 'FILESEXTRAPATHS:prepend := "${TOPDIR}/%s:"' % test_files_dir + os.linesep
config += 'SRC_URI:append:pn-linux-yocto = " file://${FIT_UBOOT_ENV}"' + os.linesep
if not os.path.isdir(test_files_dir):
os.makedirs(test_files_dir)
self.logger.debug("Writing to: %s" % fit_uenv_path)
with open(fit_uenv_path, "w") as f:
f.write('echo "hello world"')
return config
def _bitbake_fit_image(self, bb_vars):
"""Bitbake the kernel and return the paths to the its file and the FIT image"""
bitbake("virtual/kernel")
# Find the right its file and the final fitImage and check if both files are available
deploy_dir_image = bb_vars['DEPLOY_DIR_IMAGE']
initramfs_image = bb_vars['INITRAMFS_IMAGE']
initramfs_image_bundle = bb_vars['INITRAMFS_IMAGE_BUNDLE']
initramfs_image_name = bb_vars['INITRAMFS_IMAGE_NAME']
kernel_fit_link_name = bb_vars['KERNEL_FIT_LINK_NAME']
if not initramfs_image and initramfs_image_bundle != "1":
fitimage_its_name = "fitImage-its-%s" % kernel_fit_link_name
fitimage_name = "fitImage"
elif initramfs_image and initramfs_image_bundle != "1":
fitimage_its_name = "fitImage-its-%s-%s" % (initramfs_image_name, kernel_fit_link_name)
fitimage_name = "fitImage-%s-%s" % (initramfs_image_name, kernel_fit_link_name)
elif initramfs_image and initramfs_image_bundle == "1":
fitimage_its_name = "fitImage-its-%s-%s" % (initramfs_image_name, kernel_fit_link_name)
fitimage_name = "fitImage" # or fitImage-${KERNEL_IMAGE_LINK_NAME}${KERNEL_IMAGE_BIN_EXT}
else:
self.fail('Invalid configuration: INITRAMFS_IMAGE_BUNDLE = "1" and not INITRAMFS_IMAGE')
fitimage_its_path = os.path.realpath(os.path.join(deploy_dir_image, fitimage_its_name))
fitimage_path = os.path.realpath(os.path.join(deploy_dir_image, fitimage_name))
return (fitimage_its_path, fitimage_path)
def _get_req_its_paths(self, bb_vars):
"""Generate a list of expected paths in the its file
Example:
[
['/', 'images', 'kernel-1', 'hash-1'],
['/', 'images', 'kernel-1', 'signature-1'],
]
"""
dtb_files = FitImageTestCase._get_dtb_files(bb_vars)
fit_sign_individual = bb_vars['FIT_SIGN_INDIVIDUAL']
fit_uboot_env = bb_vars['FIT_UBOOT_ENV']
initramfs_image = bb_vars['INITRAMFS_IMAGE']
initramfs_image_bundle = bb_vars['INITRAMFS_IMAGE_BUNDLE']
uboot_sign_enable = bb_vars['UBOOT_SIGN_ENABLE']
# image nodes
images = [ 'kernel-1' ]
if dtb_files:
images += [ 'fdt-' + dtb for dtb in dtb_files ]
if fit_uboot_env:
images.append('bootscr-' + fit_uboot_env)
if bb_vars['MACHINE'] == "qemux86-64": # Not really the right if
images.append('setup-1')
if initramfs_image and initramfs_image_bundle != "1":
images.append('ramdisk-1')
# configuration nodes
if dtb_files:
configurations = [ 'conf-' + conf for conf in dtb_files ]
else:
configurations = [ 'conf-1' ]
# Create a list of paths for all image and configuration nodes
req_its_paths = []
for image in images:
req_its_paths.append(['/', 'images', image, 'hash-1'])
if uboot_sign_enable == "1" and fit_sign_individual == "1":
req_its_paths.append(['/', 'images', image, 'signature-1'])
for configuration in configurations:
req_its_paths.append(['/', 'configurations', configuration, 'hash-1'])
if uboot_sign_enable == "1":
req_its_paths.append(['/', 'configurations', configuration, 'signature-1'])
return req_its_paths
def _get_req_its_fields(self, bb_vars):
initramfs_image = bb_vars['INITRAMFS_IMAGE']
initramfs_image_bundle = bb_vars['INITRAMFS_IMAGE_BUNDLE']
uboot_rd_loadaddress = bb_vars['UBOOT_RD_LOADADDRESS']
uboot_rd_entrypoint = bb_vars['UBOOT_RD_ENTRYPOINT']
its_field_check = [
'description = "%s";' % bb_vars['FIT_DESC'],
'description = "Linux kernel";',
'data = /incbin/("linux.bin");',
'type = "' + str(bb_vars['UBOOT_MKIMAGE_KERNEL_TYPE']) + '";',
'arch = "' + str(bb_vars['UBOOT_ARCH']) + '";',
'os = "linux";',
# 'compression = "' + str(bb_vars['FIT_KERNEL_COMP_ALG']) + '";', defined based on files in TMPDIR, not ideal...
'load = <' + str(bb_vars['UBOOT_LOADADDRESS']) + '>;',
'entry = <' + str(bb_vars['UBOOT_ENTRYPOINT']) + '>;',
]
if initramfs_image and initramfs_image_bundle != "1":
its_field_check.append('type = "ramdisk";')
if uboot_rd_loadaddress:
its_field_check.append("load = <%s>;" % uboot_rd_loadaddress)
if uboot_rd_entrypoint:
its_field_check.append("entry = <%s>;" % uboot_rd_entrypoint)
its_field_check += [
# 'default = "conf-1";', needs more work
'kernel = "kernel-1";',
]
if initramfs_image and initramfs_image_bundle != "1":
its_field_check.append('ramdisk = "ramdisk-1";')
return its_field_check
def _get_req_sigvalues_config(self, bb_vars):
"""Generate a dictionary of expected configuration signature nodes"""
sign_images = '"kernel", "fdt"'
if bb_vars['INITRAMFS_IMAGE'] and bb_vars['INITRAMFS_IMAGE_BUNDLE'] != "1":
sign_images += ', "ramdisk"'
if bb_vars['FIT_UBOOT_ENV']:
sign_images += ', "bootscr"'
req_sigvalues_config = {
'algo': '"%s,%s"' % (bb_vars['FIT_HASH_ALG'], bb_vars['FIT_SIGN_ALG']),
'key-name-hint': '"%s"' % bb_vars['UBOOT_SIGN_KEYNAME'],
'sign-images': sign_images,
}
return req_sigvalues_config
def _get_req_sigvalues_image(self, bb_vars):
"""Generate a dictionary of expected image signature nodes"""
if bb_vars['FIT_SIGN_INDIVIDUAL'] != "1":
return {}
req_sigvalues_image = {
'algo': '"%s,%s"' % (bb_vars['FIT_HASH_ALG'], bb_vars['FIT_SIGN_ALG']),
'key-name-hint': '"%s"' % bb_vars['UBOOT_SIGN_IMG_KEYNAME'],
}
return req_sigvalues_image
def _get_req_sections(self, bb_vars):
"""Generate a dictionary of expected sections in the output of dumpimage"""
dtb_files = FitImageTestCase._get_dtb_files(bb_vars)
fit_hash_alg = bb_vars['FIT_HASH_ALG']
fit_sign_alg = bb_vars['FIT_SIGN_ALG']
fit_sign_individual = bb_vars['FIT_SIGN_INDIVIDUAL']
fit_uboot_env = bb_vars['FIT_UBOOT_ENV']
initramfs_image = bb_vars['INITRAMFS_IMAGE']
initramfs_image_bundle = bb_vars['INITRAMFS_IMAGE_BUNDLE']
uboot_sign_enable = bb_vars['UBOOT_SIGN_ENABLE']
uboot_sign_img_keyname = bb_vars['UBOOT_SIGN_IMG_KEYNAME']
uboot_sign_keyname = bb_vars['UBOOT_SIGN_KEYNAME']
num_signatures = 0
req_sections = {
"kernel-1": {
"Type": "Kernel Image",
"OS": "Linux",
"Load Address": bb_vars['UBOOT_LOADADDRESS'],
"Entry Point": bb_vars['UBOOT_ENTRYPOINT'],
}
}
# Create one section per DTB
for dtb in dtb_files:
req_sections['fdt-' + dtb] = {
"Type": "Flat Device Tree",
}
# Add a script section if there is a script
if fit_uboot_env:
req_sections['bootscr-' + fit_uboot_env] = { "Type": "Script" }
# Add the initramfs
if initramfs_image and initramfs_image_bundle != "1":
req_sections['ramdisk-1'] = {
"Type": "RAMDisk Image",
"Load Address": bb_vars['UBOOT_RD_LOADADDRESS'],
"Entry Point": bb_vars['UBOOT_RD_ENTRYPOINT']
}
# Create a configuration section for each DTB
if dtb_files:
for dtb in dtb_files:
req_sections['conf-' + dtb] = {
"Kernel": "kernel-1",
"FDT": 'fdt-' + dtb,
}
if initramfs_image and initramfs_image_bundle != "1":
req_sections['conf-' + dtb]['Init Ramdisk'] = "ramdisk-1"
else:
req_sections['conf-1'] = {
"Kernel": "kernel-1"
}
if initramfs_image and initramfs_image_bundle != "1":
req_sections['conf-1']['Init Ramdisk'] = "ramdisk-1"
# Add signing related properties if needed
if uboot_sign_enable == "1":
for section in req_sections:
req_sections[section]['Hash algo'] = fit_hash_alg
if section.startswith('conf-'):
req_sections[section]['Hash value'] = "unavailable"
req_sections[section]['Sign algo'] = "%s,%s:%s" % (fit_hash_alg, fit_sign_alg, uboot_sign_keyname)
num_signatures += 1
elif fit_sign_individual == "1":
req_sections[section]['Sign algo'] = "%s,%s:%s" % (fit_hash_alg, fit_sign_alg, uboot_sign_img_keyname)
num_signatures += 1
return (req_sections, num_signatures)
def _check_signing(self, bb_vars, sections, num_signatures, uboot_tools_bindir, fitimage_path):
"""Verify the signature nodes in the FIT image"""
if bb_vars['UBOOT_SIGN_ENABLE'] == "1":
self.logger.debug("Verifying signatures in the FIT image")
else:
self.logger.debug("FIT image is not signed. Signature verification is not needed.")
return
fit_hash_alg = bb_vars['FIT_HASH_ALG']
fit_sign_alg = bb_vars['FIT_SIGN_ALG']
uboot_sign_keyname = bb_vars['UBOOT_SIGN_KEYNAME']
uboot_sign_img_keyname = bb_vars['UBOOT_SIGN_IMG_KEYNAME']
deploy_dir_image = bb_vars['DEPLOY_DIR_IMAGE']
fit_sign_individual = bb_vars['FIT_SIGN_INDIVIDUAL']
fit_hash_alg_len = FitImageTestCase.MKIMAGE_HASH_LENGTHS[fit_hash_alg]
fit_sign_alg_len = FitImageTestCase.MKIMAGE_SIGNATURE_LENGTHS[fit_sign_alg]
for section, values in sections.items():
# Configuration nodes are always signed with UBOOT_SIGN_KEYNAME (if UBOOT_SIGN_ENABLE = "1")
if section.startswith("conf"):
sign_algo = values.get('Sign algo', None)
req_sign_algo = "%s,%s:%s" % (fit_hash_alg, fit_sign_alg, uboot_sign_keyname)
self.assertEqual(sign_algo, req_sign_algo, 'Signature algorithm for %s not expected value' % section)
sign_value = values.get('Sign value', None)
self.assertEqual(len(sign_value), fit_sign_alg_len, 'Signature value for section %s not expected length' % section)
dtb_path = os.path.join(deploy_dir_image, section.replace('conf-', ''))
self._verify_fit_image_signature(uboot_tools_bindir, fitimage_path, dtb_path, section)
else:
# Image nodes always need a hash which gets indirectly signed by the config signature
hash_algo = values.get('Hash algo', None)
self.assertEqual(hash_algo, fit_hash_alg)
hash_value = values.get('Hash value', None)
self.assertEqual(len(hash_value), fit_hash_alg_len, 'Hash value for section %s not expected length' % section)
# Optionally, if FIT_SIGN_INDIVIDUAL = 1 also the image nodes have a signature (which is redundant but possible)
if fit_sign_individual == "1":
sign_algo = values.get('Sign algo', None)
req_sign_algo = "%s,%s:%s" % (fit_hash_alg, fit_sign_alg, uboot_sign_img_keyname)
self.assertEqual(sign_algo, req_sign_algo, 'Signature algorithm for %s not expected value' % section)
sign_value = values.get('Sign value', None)
self.assertEqual(len(sign_value), fit_sign_alg_len, 'Signature value for section %s not expected length' % section)
# Search for the string passed to mkimage in each signed section of the FIT image.
# Looks like mkimage supports to add a comment but does not support to read it back.
a_comment = FitImageTestCase._get_uboot_mkimage_sign_args(bb_vars['UBOOT_MKIMAGE_SIGN_ARGS'])
self.logger.debug("a_comment: %s" % a_comment)
if a_comment:
found_comments = FitImageTestCase._find_string_in_bin_file(fitimage_path, a_comment)
self.assertEqual(found_comments, num_signatures, "Expected %d signed and commented (%s) sections in the fitImage." %
(num_signatures, a_comment))
def test_fit_image(self):
"""
Summary: Check if FIT image and Image Tree Source (its) are built
and the Image Tree Source has the correct fields.
Expected: 1. fitImage and fitImage-its can be built
2. The type, load address, entrypoint address and
default values of kernel and ramdisk are as expected
in the Image Tree Source. Not all the fields are tested,
only the key fields that wont vary between different
architectures.
Product: oe-core
Author: Usama Arif <usama.arif@arm.com>
"""
config = """
# Enable creation of fitImage
KERNEL_IMAGETYPE = "Image"
KERNEL_IMAGETYPES += " fitImage "
KERNEL_CLASSES = " kernel-fitimage "
# RAM disk variables including load address and entrypoint for kernel and RAM disk
IMAGE_FSTYPES += "cpio.gz"
INITRAMFS_IMAGE = "core-image-minimal"
# core-image-minimal is used as initramfs here, drop the rootfs suffix
IMAGE_NAME_SUFFIX:pn-core-image-minimal = ""
UBOOT_RD_LOADADDRESS = "0x88000000"
UBOOT_RD_ENTRYPOINT = "0x88000000"
UBOOT_LOADADDRESS = "0x80080000"
UBOOT_ENTRYPOINT = "0x80080000"
FIT_DESC = "A model description"
"""
self.write_config(config)
bb_vars = self._fit_get_bb_vars()
self._test_fitimage(bb_vars)
def test_sign_fit_image_configurations(self):
"""
Summary: Check if FIT image and Image Tree Source (its) are created
and the configuration nodes are signed correctly.
Expected: 1) its and FIT image are built successfully
2) Scanning the its file indicates signing is enabled
as requested by UBOOT_SIGN_ENABLE (using 1 key
generated by the test not via FIT_GENERATE_KEYS)
3) Dumping the FIT image indicates signature values
are present (only for the configuration nodes as
FIT_SIGN_INDIVIDUAL is disabled)
4) Verify the FIT image contains the comments passed via
UBOOT_MKIMAGE_SIGN_ARGS once per configuration node.
"""
# Generate a configuration section which gets included into the local.conf file
config = """
# Enable creation of fitImage
MACHINE = "beaglebone-yocto"
KERNEL_IMAGETYPES += " fitImage "
KERNEL_CLASSES = " kernel-fitimage "
UBOOT_SIGN_ENABLE = "1"
UBOOT_SIGN_KEYDIR = "${TOPDIR}/signing-keys"
UBOOT_SIGN_KEYNAME = "dev"
UBOOT_MKIMAGE_SIGN_ARGS = "-c 'a smart comment'"
"""
config = self._config_add_uboot_env(config)
self.write_config(config)
# Retrieve some variables from bitbake
bb_vars = self._fit_get_bb_vars([
'FIT_KEY_GENRSA_ARGS',
'FIT_KEY_REQ_ARGS',
'FIT_KEY_SIGN_PKCS',
'FIT_SIGN_NUMBITS',
'UBOOT_SIGN_KEYDIR',
])
# Do not use the random keys generated by FIT_GENERATE_KEYS.
# Using a static key is probably a more realistic scenario.
self._gen_signing_key(bb_vars)
self._test_fitimage(bb_vars)
def test_sign_fit_image_individual(self):
"""
Summary: Check if FIT image and Image Tree Source (its) are created
and all nodes are signed correctly.
Expected: 1) its and FIT image are built successfully
2) Scanning the its file indicates signing is enabled
as requested by UBOOT_SIGN_ENABLE (using 2 keys
generated via FIT_GENERATE_KEYS)
3) Dumping the FIT image indicates signature values
are present (including for images as enabled via
FIT_SIGN_INDIVIDUAL)
4) Verify the FIT image contains the comments passed via
UBOOT_MKIMAGE_SIGN_ARGS once per image and per
configuration node.
Note: This test is mostly for backward compatibility.
The recommended approach is to sign the configuration nodes
which include also the hashes of all the images. Signing
all the images individually is therefore redundant.
Product: oe-core
Author: Paul Eggleton <paul.eggleton@microsoft.com> based upon
work by Usama Arif <usama.arif@arm.com>
"""
# Generate a configuration section which gets included into the local.conf file
config = """
# Enable creation of fitImage
MACHINE = "beaglebone-yocto"
KERNEL_IMAGETYPES += " fitImage "
KERNEL_CLASSES = " kernel-fitimage "
UBOOT_SIGN_ENABLE = "1"
FIT_GENERATE_KEYS = "1"
UBOOT_SIGN_KEYDIR = "${TOPDIR}/signing-keys"
UBOOT_SIGN_IMG_KEYNAME = "img-oe-selftest"
UBOOT_SIGN_KEYNAME = "cfg-oe-selftest"
FIT_SIGN_INDIVIDUAL = "1"
UBOOT_MKIMAGE_SIGN_ARGS = "-c 'a smart comment'"
"""
config = self._config_add_uboot_env(config)
self.write_config(config)
bb_vars = self._fit_get_bb_vars()
self._test_fitimage(bb_vars)
def test_fit_image_sign_initramfs(self):
"""
Summary: Verifies the content of the initramfs node in the FIT Image Tree Source (its)
The FIT settings are set by the test case.
The machine used is beaglebone-yocto.
Expected: 1. The ITS is generated with initramfs support
2. All the fields in the kernel node are as expected (matching the
conf settings)
3. The kernel is included in all the available configurations and
its hash is included in the configuration signature
Product: oe-core
Author: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
"""
config = """
DISTRO="poky"
MACHINE = "beaglebone-yocto"
INITRAMFS_IMAGE = "core-image-minimal-initramfs"
INITRAMFS_SCRIPTS = ""
UBOOT_MACHINE = "am335x_evm_defconfig"
KERNEL_CLASSES = " kernel-fitimage "
KERNEL_IMAGETYPES = "fitImage"
UBOOT_SIGN_ENABLE = "1"
UBOOT_SIGN_KEYNAME = "beaglebonekey"
UBOOT_SIGN_KEYDIR ?= "${DEPLOY_DIR_IMAGE}"
UBOOT_DTB_BINARY = "u-boot.dtb"
UBOOT_ENTRYPOINT = "0x80000000"
UBOOT_LOADADDRESS = "0x80000000"
UBOOT_RD_LOADADDRESS = "0x88000000"
UBOOT_RD_ENTRYPOINT = "0x88000000"
UBOOT_DTB_LOADADDRESS = "0x82000000"
UBOOT_ARCH = "arm"
UBOOT_MKIMAGE_DTCOPTS = "-I dts -O dtb -p 2000"
UBOOT_MKIMAGE_KERNEL_TYPE = "kernel"
UBOOT_EXTLINUX = "0"
FIT_GENERATE_KEYS = "1"
KERNEL_IMAGETYPE_REPLACEMENT = "zImage"
FIT_KERNEL_COMP_ALG = "none"
FIT_HASH_ALG = "sha256"
"""
config = self._config_add_uboot_env(config)
self.write_config(config)
# Retrieve some variables from bitbake
bb_vars = self._fit_get_bb_vars([
'FIT_KEY_GENRSA_ARGS',
'FIT_KEY_REQ_ARGS',
'FIT_KEY_SIGN_PKCS',
'FIT_SIGN_NUMBITS',
'UBOOT_SIGN_KEYDIR',
])
# Do not use the random keys generated by FIT_GENERATE_KEYS.
# Using a static key is probably a more realistic scenario.
self._gen_signing_key(bb_vars)
self._test_fitimage(bb_vars)
def test_fit_image_sign_initramfs_bundle(self):
"""
Summary: Verifies the content of the initramfs bundle node in the FIT Image Tree Source (its)
The FIT settings are set by the test case.
The machine used is beaglebone-yocto.
Expected: 1. The ITS is generated with initramfs bundle support
2. All the fields in the kernel node are as expected (matching the
conf settings)
3. The kernel is included in all the available configurations and
its hash is included in the configuration signature
Product: oe-core
Author: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
"""
config = """
DISTRO="poky"
MACHINE = "beaglebone-yocto"
INITRAMFS_IMAGE_BUNDLE = "1"
INITRAMFS_IMAGE = "core-image-minimal-initramfs"
INITRAMFS_SCRIPTS = ""
UBOOT_MACHINE = "am335x_evm_defconfig"
KERNEL_CLASSES = " kernel-fitimage "
KERNEL_IMAGETYPES = "fitImage"
UBOOT_SIGN_ENABLE = "1"
UBOOT_SIGN_KEYNAME = "beaglebonekey"
UBOOT_SIGN_KEYDIR ?= "${DEPLOY_DIR_IMAGE}"
UBOOT_DTB_BINARY = "u-boot.dtb"
UBOOT_ENTRYPOINT = "0x80000000"
UBOOT_LOADADDRESS = "0x80000000"
UBOOT_DTB_LOADADDRESS = "0x82000000"
UBOOT_ARCH = "arm"
UBOOT_MKIMAGE_DTCOPTS = "-I dts -O dtb -p 2000"
UBOOT_MKIMAGE_KERNEL_TYPE = "kernel"
UBOOT_EXTLINUX = "0"
FIT_GENERATE_KEYS = "1"
KERNEL_IMAGETYPE_REPLACEMENT = "zImage"
FIT_KERNEL_COMP_ALG = "none"
FIT_HASH_ALG = "sha256"
"""
config = self._config_add_uboot_env(config)
self.write_config(config)
bb_vars = self._fit_get_bb_vars()
self._test_fitimage(bb_vars)
class UBootFitImageTests(FitImageTestCase):
"""Test cases for the uboot-sign bbclass"""
def _fit_get_bb_vars(self, additional_vars=[]):
"""Get bb_vars as needed by _test_sign_fit_image
Call the get_bb_vars function once and get all variables needed by the test case.
"""
internal_used = {
'DEPLOY_DIR_IMAGE',
'MACHINE',
'SPL_MKIMAGE_SIGN_ARGS',
'SPL_SIGN_ENABLE',
'SPL_SIGN_KEYNAME',
'UBOOT_ARCH',
'UBOOT_DTB_BINARY',
'UBOOT_FIT_ARM_TRUSTED_FIRMWARE_ENTRYPOINT',
'UBOOT_FIT_ARM_TRUSTED_FIRMWARE_LOADADDRESS',
'UBOOT_FIT_ARM_TRUSTED_FIRMWARE',
'UBOOT_FIT_CONF_USER_LOADABLES',
'UBOOT_FIT_DESC',
'UBOOT_FIT_HASH_ALG',
'UBOOT_FIT_SIGN_ALG',
'UBOOT_FIT_TEE_ENTRYPOINT',
'UBOOT_FIT_TEE_LOADADDRESS',
'UBOOT_FIT_TEE',
'UBOOT_FIT_UBOOT_ENTRYPOINT',
'UBOOT_FIT_UBOOT_LOADADDRESS',
'UBOOT_FIT_USER_SETTINGS',
'UBOOT_FITIMAGE_ENABLE',
'UBOOT_NODTB_BINARY',
'UBOOT_SIGN_IMG_KEYNAME',
}
bb_vars = get_bb_vars(list(internal_used | set(additional_vars)), "virtual/bootloader")
return bb_vars
def _bitbake_fit_image(self, bb_vars):
"""Bitbake the bootloader and return the paths to the its file and the FIT image"""
bitbake("virtual/bootloader")
deploy_dir_image = bb_vars['DEPLOY_DIR_IMAGE']
machine = bb_vars['MACHINE']
fitimage_its_path = os.path.join(deploy_dir_image, "u-boot-its-%s" % machine)
fitimage_path = os.path.join(deploy_dir_image, "u-boot-fitImage-%s" % machine)
return (fitimage_its_path, fitimage_path)
def _get_req_its_paths(self, bb_vars):
# image nodes
images = [ 'uboot', 'fdt', ]
if bb_vars['UBOOT_FIT_TEE'] == "1":
images.append('tee')
if bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE'] == "1":
images.append('atf')
# if bb_vars['UBOOT_FIT_USER_SETTINGS']:
# configuration nodes
configurations = [ 'conf']
# Create a list of paths for all image and configuration nodes
req_its_paths = []
for image in images:
req_its_paths.append(['/', 'images', image])
if bb_vars['SPL_SIGN_ENABLE'] == "1":
req_its_paths.append(['/', 'images', image, 'signature'])
for configuration in configurations:
req_its_paths.append(['/', 'configurations', configuration])
return req_its_paths
def _get_req_its_fields(self, bb_vars):
loadables = ["uboot"]
its_field_check = [
'description = "%s";' % bb_vars['UBOOT_FIT_DESC'],
'description = "U-Boot image";',
'data = /incbin/("%s");' % bb_vars['UBOOT_NODTB_BINARY'],
'type = "standalone";',
'os = "u-boot";',
'arch = "%s";' % bb_vars['UBOOT_ARCH'],
'compression = "none";',
'load = <%s>;' % bb_vars['UBOOT_FIT_UBOOT_LOADADDRESS'],
'entry = <%s>;' % bb_vars['UBOOT_FIT_UBOOT_ENTRYPOINT'],
'description = "U-Boot FDT";',
'data = /incbin/("%s");' % bb_vars['UBOOT_DTB_BINARY'],
'type = "flat_dt";',
'arch = "%s";' % bb_vars['UBOOT_ARCH'],
'compression = "none";',
]
if bb_vars['UBOOT_FIT_TEE'] == "1":
its_field_check += [
'description = "Trusted Execution Environment";',
'data = /incbin/("%s");' % bb_vars['UBOOT_FIT_TEE_IMAGE'],
'type = "tee";',
'arch = "%s";' % bb_vars['UBOOT_ARCH'],
'os = "tee";',
'load = <%s>;' % bb_vars['UBOOT_FIT_TEE_LOADADDRESS'],
'entry = <%s>;' % bb_vars['UBOOT_FIT_TEE_ENTRYPOINT'],
'compression = "none";',
]
loadables.insert(0, "tee")
if bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE'] == "1":
its_field_check += [
'description = "ARM Trusted Firmware";',
'data = /incbin/("%s");' % bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE_IMAGE'],
'type = "firmware";',
'arch = "%s";' % bb_vars['UBOOT_ARCH'],
'os = "arm-trusted-firmware";',
'load = <%s>;' % bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE_LOADADDRESS'],
'entry = <%s>;' % bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE_ENTRYPOINT'],
'compression = "none";',
]
loadables.insert(0, "atf")
its_field_check += [
'default = "conf";',
'description = "Boot with signed U-Boot FIT";',
'loadables = "%s";' % '", "'.join(loadables),
'fdt = "fdt";',
]
return its_field_check
def _get_req_sigvalues_config(self, bb_vars):
# COnfigurations are not signed by uboot-sign
return {}
def _get_req_sigvalues_image(self, bb_vars):
if bb_vars['SPL_SIGN_ENABLE'] != "1":
return {}
req_sigvalues_image = {
'algo': '"%s,%s"' % (bb_vars['UBOOT_FIT_HASH_ALG'], bb_vars['UBOOT_FIT_SIGN_ALG']),
'key-name-hint': '"%s"' % bb_vars['SPL_SIGN_KEYNAME'],
}
return req_sigvalues_image
def _get_req_sections(self, bb_vars):
"""Generate the expected output of dumpimage for beaglebone targets
The dict generated by this function is supposed to be compared against
the dict which is generated by the _dump_fitimage function.
"""
loadables = ['uboot']
req_sections = {
"uboot": {
"Type": "Standalone Program",
"Load Address": bb_vars['UBOOT_FIT_UBOOT_LOADADDRESS'],
"Entry Point": bb_vars['UBOOT_FIT_UBOOT_ENTRYPOINT'],
},
"fdt": {
"Type": "Flat Device Tree",
}
}
if bb_vars['UBOOT_FIT_TEE'] == "1":
loadables.insert(0, "tee")
req_sections['tee'] = {
"Type": "Trusted Execution Environment Image",
# "Load Address": bb_vars['UBOOT_FIT_TEE_LOADADDRESS'], not printed by mkimage?
# "Entry Point": bb_vars['UBOOT_FIT_TEE_ENTRYPOINT'], not printed by mkimage?
}
if bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE'] == "1":
loadables.insert(0, "atf")
req_sections['atf'] = {
"Type": "Firmware",
"Load Address": bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE_LOADADDRESS'],
# "Entry Point": bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE_ENTRYPOINT'], not printed by mkimage?
}
req_sections["conf"] = {
"Kernel": "unavailable",
"FDT": "fdt",
"Loadables": ','.join(loadables),
}
# Add signing related properties if needed
uboot_fit_hash_alg = bb_vars['UBOOT_FIT_HASH_ALG']
uboot_fit_sign_alg = bb_vars['UBOOT_FIT_SIGN_ALG']
spl_sign_enable = bb_vars['SPL_SIGN_ENABLE']
spl_sign_keyname = bb_vars['SPL_SIGN_KEYNAME']
num_signatures = 0
if spl_sign_enable == "1":
for section in req_sections:
if not section.startswith('conf'):
req_sections[section]['Sign algo'] = "%s,%s:%s" % \
(uboot_fit_hash_alg, uboot_fit_sign_alg, spl_sign_keyname)
num_signatures += 1
return (req_sections, num_signatures)
def _check_signing(self, bb_vars, sections, num_signatures, uboot_tools_bindir, fitimage_path):
if bb_vars['UBOOT_FITIMAGE_ENABLE'] == '1' and bb_vars['SPL_SIGN_ENABLE'] == "1":
self.logger.debug("Verifying signatures in the FIT image")
else:
self.logger.debug("FIT image is not signed. Signature verification is not needed.")
return
uboot_fit_hash_alg = bb_vars['UBOOT_FIT_HASH_ALG']
uboot_fit_sign_alg = bb_vars['UBOOT_FIT_SIGN_ALG']
spl_sign_keyname = bb_vars['SPL_SIGN_KEYNAME']
fit_sign_alg_len = FitImageTestCase.MKIMAGE_SIGNATURE_LENGTHS[uboot_fit_sign_alg]
for section, values in sections.items():
# Configuration nodes are always signed with UBOOT_SIGN_KEYNAME (if UBOOT_SIGN_ENABLE = "1")
if section.startswith("conf"):
# uboot-sign does not sign configuration nodes
pass
else:
# uboot-sign does not add hash nodes, only image signatures
sign_algo = values.get('Sign algo', None)
req_sign_algo = "%s,%s:%s" % (uboot_fit_hash_alg, uboot_fit_sign_alg, spl_sign_keyname)
self.assertEqual(sign_algo, req_sign_algo, 'Signature algorithm for %s not expected value' % section)
sign_value = values.get('Sign value', None)
self.assertEqual(len(sign_value), fit_sign_alg_len, 'Signature value for section %s not expected length' % section)
# Search for the string passed to mkimage in each signed section of the FIT image.
# Looks like mkimage supports to add a comment but does not support to read it back.
a_comment = FitImageTestCase._get_uboot_mkimage_sign_args(bb_vars['SPL_MKIMAGE_SIGN_ARGS'])
self.logger.debug("a_comment: %s" % a_comment)
if a_comment:
found_comments = FitImageTestCase._find_string_in_bin_file(fitimage_path, a_comment)
self.assertEqual(found_comments, num_signatures, "Expected %d signed and commented (%s) sections in the fitImage." %
(num_signatures, a_comment))
def test_uboot_fit_image(self):
"""
Summary: Check if Uboot FIT image and Image Tree Source
(its) are built and the Image Tree Source has the
correct fields.
Expected: 1. u-boot-fitImage and u-boot-its can be built
2. The type, load address, entrypoint address and
default values of U-boot image are correct in the
Image Tree Source. Not all the fields are tested,
only the key fields that wont vary between
different architectures.
Product: oe-core
Author: Klaus Heinrich Kiwi <klaus@linux.vnet.ibm.com>
based on work by Usama Arif <usama.arif@arm.com>
"""
config = """
# We need at least CONFIG_SPL_LOAD_FIT and CONFIG_SPL_OF_CONTROL set
MACHINE = "qemuarm"
UBOOT_MACHINE = "am57xx_evm_defconfig"
SPL_BINARY = "MLO"
# Enable creation of the U-Boot fitImage
UBOOT_FITIMAGE_ENABLE = "1"
# (U-boot) fitImage properties
UBOOT_LOADADDRESS = "0x80080000"
UBOOT_ENTRYPOINT = "0x80080000"
UBOOT_FIT_DESC = "A model description"
"""
self.write_config(config)
bb_vars = self._fit_get_bb_vars()
self._test_fitimage(bb_vars)
def test_sign_standalone_uboot_fit_image(self):
"""
Summary: Check if U-Boot FIT image and Image Tree Source (its) are
created and signed correctly for the scenario where only
the U-Boot proper fitImage is being created and signed.
Expected: 1) U-Boot its and FIT image are built successfully
2) Scanning the its file indicates signing is enabled
as requested by SPL_SIGN_ENABLE (using keys generated
via UBOOT_FIT_GENERATE_KEYS)
3) Dumping the FIT image indicates signature values
are present
4) Examination of the do_uboot_assemble_fitimage
runfile/logfile indicate that UBOOT_MKIMAGE, UBOOT_MKIMAGE_SIGN
and SPL_MKIMAGE_SIGN_ARGS are working as expected.
Product: oe-core
Author: Klaus Heinrich Kiwi <klaus@linux.vnet.ibm.com> based upon
work by Paul Eggleton <paul.eggleton@microsoft.com> and
Usama Arif <usama.arif@arm.com>
"""
config = """
# There's no U-boot defconfig with CONFIG_FIT_SIGNATURE yet, so we need at
# least CONFIG_SPL_LOAD_FIT and CONFIG_SPL_OF_CONTROL set
MACHINE = "qemuarm"
UBOOT_MACHINE = "am57xx_evm_defconfig"
SPL_BINARY = "MLO"
# Enable creation and signing of the U-Boot fitImage
UBOOT_FITIMAGE_ENABLE = "1"
SPL_SIGN_ENABLE = "1"
SPL_SIGN_KEYNAME = "spl-oe-selftest"
SPL_SIGN_KEYDIR = "${TOPDIR}/signing-keys"
UBOOT_DTB_BINARY = "u-boot.dtb"
UBOOT_ENTRYPOINT = "0x80000000"
UBOOT_LOADADDRESS = "0x80000000"
UBOOT_DTB_LOADADDRESS = "0x82000000"
UBOOT_ARCH = "arm"
SPL_MKIMAGE_DTCOPTS = "-I dts -O dtb -p 2000"
SPL_MKIMAGE_SIGN_ARGS = "-c 'a smart U-Boot comment'"
UBOOT_EXTLINUX = "0"
UBOOT_FIT_GENERATE_KEYS = "1"
UBOOT_FIT_HASH_ALG = "sha256"
"""
self.write_config(config)
bb_vars = self._fit_get_bb_vars()
self._test_fitimage(bb_vars)
def test_sign_cascaded_uboot_fit_image(self):
"""
Summary: Check if U-Boot FIT image and Image Tree Source (its) are
created and signed correctly for the scenario where both
U-Boot proper and Kernel fitImages are being created and
signed.
Expected: 1) U-Boot its and FIT image are built successfully
2) Scanning the its file indicates signing is enabled
as requested by SPL_SIGN_ENABLE (using keys generated
via UBOOT_FIT_GENERATE_KEYS)
3) Dumping the FIT image indicates signature values
are present
4) Examination of the do_uboot_assemble_fitimage
runfile/logfile indicate that UBOOT_MKIMAGE, UBOOT_MKIMAGE_SIGN
and SPL_MKIMAGE_SIGN_ARGS are working as expected.
Product: oe-core
Author: Klaus Heinrich Kiwi <klaus@linux.vnet.ibm.com> based upon
work by Paul Eggleton <paul.eggleton@microsoft.com> and
Usama Arif <usama.arif@arm.com>
"""
config = """
# There's no U-boot deconfig with CONFIG_FIT_SIGNATURE yet, so we need at
# least CONFIG_SPL_LOAD_FIT and CONFIG_SPL_OF_CONTROL set
MACHINE = "qemuarm"
UBOOT_MACHINE = "am57xx_evm_defconfig"
SPL_BINARY = "MLO"
# Enable creation and signing of the U-Boot fitImage
UBOOT_FITIMAGE_ENABLE = "1"
SPL_SIGN_ENABLE = "1"
SPL_SIGN_KEYNAME = "spl-cascaded-oe-selftest"
SPL_SIGN_KEYDIR = "${TOPDIR}/signing-keys"
UBOOT_DTB_BINARY = "u-boot.dtb"
UBOOT_ENTRYPOINT = "0x80000000"
UBOOT_LOADADDRESS = "0x80000000"
UBOOT_MKIMAGE_DTCOPTS = "-I dts -O dtb -p 2000"
UBOOT_MKIMAGE_SIGN_ARGS = "-c 'a smart cascaded U-Boot comment'"
UBOOT_DTB_LOADADDRESS = "0x82000000"
UBOOT_ARCH = "arm"
SPL_MKIMAGE_DTCOPTS = "-I dts -O dtb -p 2000"
SPL_MKIMAGE_SIGN_ARGS = "-c 'a smart cascaded U-Boot comment'"
UBOOT_EXTLINUX = "0"
UBOOT_FIT_GENERATE_KEYS = "1"
UBOOT_FIT_HASH_ALG = "sha256"
UBOOT_SIGN_ENABLE = "1"
FIT_GENERATE_KEYS = "1"
UBOOT_SIGN_KEYDIR = "${TOPDIR}/signing-keys"
UBOOT_SIGN_IMG_KEYNAME = "img-oe-selftest"
UBOOT_SIGN_KEYNAME = "cfg-oe-selftest"
FIT_SIGN_INDIVIDUAL = "1"
"""
self.write_config(config)
bb_vars = self._fit_get_bb_vars()
self._test_fitimage(bb_vars)
def test_uboot_atf_tee_fit_image(self):
"""
Summary: Check if U-boot FIT image and Image Tree Source
(its) are built and the Image Tree Source has the
correct fields.
Expected: 1. Create atf and tee dummy images
2. Both u-boot-fitImage and u-boot-its can be built
3. The os, load address, entrypoint address and
default values of U-boot, ATF and TEE images are
correct in the Image Tree Source. Not all the
fields are tested, only the key fields that wont
vary between different architectures.
Product: oe-core
Author: Jamin Lin <jamin_lin@aspeedtech.com>
"""
config = """
# We need at least CONFIG_SPL_LOAD_FIT and CONFIG_SPL_OF_CONTROL set
MACHINE = "qemuarm"
UBOOT_MACHINE = "am57xx_evm_defconfig"
SPL_BINARY = "MLO"
# Enable creation of the U-Boot fitImage
UBOOT_FITIMAGE_ENABLE = "1"
# (U-boot) fitImage properties
UBOOT_LOADADDRESS = "0x80080000"
UBOOT_ENTRYPOINT = "0x80080000"
UBOOT_FIT_DESC = "A model description"
# Enable creation of the TEE fitImage
UBOOT_FIT_TEE = "1"
# TEE fitImage properties
UBOOT_FIT_TEE_IMAGE = "${TOPDIR}/tee-dummy.bin"
UBOOT_FIT_TEE_LOADADDRESS = "0x80180000"
UBOOT_FIT_TEE_ENTRYPOINT = "0x80180000"
# Enable creation of the ATF fitImage
UBOOT_FIT_ARM_TRUSTED_FIRMWARE = "1"
# ATF fitImage properties
UBOOT_FIT_ARM_TRUSTED_FIRMWARE_IMAGE = "${TOPDIR}/atf-dummy.bin"
UBOOT_FIT_ARM_TRUSTED_FIRMWARE_LOADADDRESS = "0x80280000"
UBOOT_FIT_ARM_TRUSTED_FIRMWARE_ENTRYPOINT = "0x80280000"
"""
self.write_config(config)
bb_vars = self._fit_get_bb_vars([
'UBOOT_FIT_ARM_TRUSTED_FIRMWARE_IMAGE',
'UBOOT_FIT_TEE_IMAGE',
])
# Create an ATF dummy image
dummy_atf = os.path.join(self.builddir, bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE_IMAGE'])
FitImageTestCase._gen_random_file(dummy_atf)
# Create a TEE dummy image
dummy_tee = os.path.join(self.builddir, bb_vars['UBOOT_FIT_TEE_IMAGE'])
FitImageTestCase._gen_random_file(dummy_tee)
self._test_fitimage(bb_vars)
def test_sign_standalone_uboot_atf_tee_fit_image(self):
"""
Summary: Check if U-Boot FIT image and Image Tree Source (its) are
created and signed correctly for the scenario where only
the U-Boot proper fitImage is being created and signed.
Expected: 1. Create atf and tee dummy images
2. U-Boot its and FIT image are built successfully
3. Scanning the its file indicates signing is enabled
as requested by SPL_SIGN_ENABLE (using keys generated
via UBOOT_FIT_GENERATE_KEYS)
4. Dumping the FIT image indicates signature values
are present
5. Examination of the do_uboot_assemble_fitimage
runfile/logfile indicate that UBOOT_MKIMAGE, UBOOT_MKIMAGE_SIGN
and SPL_MKIMAGE_SIGN_ARGS are working as expected.
Product: oe-core
Author: Jamin Lin <jamin_lin@aspeedtech.com>
"""
config = """
# There's no U-boot deconfig with CONFIG_FIT_SIGNATURE yet, so we need at
# least CONFIG_SPL_LOAD_FIT and CONFIG_SPL_OF_CONTROL set
MACHINE = "qemuarm"
UBOOT_MACHINE = "am57xx_evm_defconfig"
SPL_BINARY = "MLO"
# Enable creation and signing of the U-Boot fitImage
UBOOT_FITIMAGE_ENABLE = "1"
SPL_SIGN_ENABLE = "1"
SPL_SIGN_KEYNAME = "spl-oe-selftest"
SPL_SIGN_KEYDIR = "${TOPDIR}/signing-keys"
UBOOT_DTB_BINARY = "u-boot.dtb"
UBOOT_ENTRYPOINT = "0x80000000"
UBOOT_LOADADDRESS = "0x80000000"
UBOOT_ARCH = "arm"
SPL_MKIMAGE_DTCOPTS = "-I dts -O dtb -p 2000"
SPL_MKIMAGE_SIGN_ARGS = "-c 'a smart U-Boot ATF TEE comment'"
UBOOT_EXTLINUX = "0"
UBOOT_FIT_GENERATE_KEYS = "1"
UBOOT_FIT_HASH_ALG = "sha256"
# Enable creation of the TEE fitImage
UBOOT_FIT_TEE = "1"
# TEE fitImage properties
UBOOT_FIT_TEE_IMAGE = "${TOPDIR}/tee-dummy.bin"
UBOOT_FIT_TEE_LOADADDRESS = "0x80180000"
UBOOT_FIT_TEE_ENTRYPOINT = "0x80180000"
# Enable creation of the ATF fitImage
UBOOT_FIT_ARM_TRUSTED_FIRMWARE = "1"
# ATF fitImage properties
UBOOT_FIT_ARM_TRUSTED_FIRMWARE_IMAGE = "${TOPDIR}/atf-dummy.bin"
UBOOT_FIT_ARM_TRUSTED_FIRMWARE_LOADADDRESS = "0x80280000"
UBOOT_FIT_ARM_TRUSTED_FIRMWARE_ENTRYPOINT = "0x80280000"
"""
self.write_config(config)
bb_vars = self._fit_get_bb_vars([
'UBOOT_FIT_ARM_TRUSTED_FIRMWARE_IMAGE',
'UBOOT_FIT_TEE_IMAGE',
])
# Create an ATF dummy image
dummy_atf = os.path.join(self.builddir, bb_vars['UBOOT_FIT_ARM_TRUSTED_FIRMWARE_IMAGE'])
FitImageTestCase._gen_random_file(dummy_atf)
# Create a TEE dummy image
dummy_tee = os.path.join(self.builddir, bb_vars['UBOOT_FIT_TEE_IMAGE'])
FitImageTestCase._gen_random_file(dummy_tee)
self._test_fitimage(bb_vars)
|