1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
5 # To run a single test, change to this directory, and:
7 # python -m unittest func_test.TestFunctional.testHelp
9 from optparse import OptionParser
27 # Contents of test files, corresponding to different entry types
29 U_BOOT_IMG_DATA = 'img'
30 U_BOOT_SPL_DATA = '56780123456789abcde'
34 U_BOOT_DTB_DATA = 'udtb'
35 U_BOOT_SPL_DTB_DATA = 'spldtb'
36 X86_START16_DATA = 'start16'
37 X86_START16_SPL_DATA = 'start16spl'
38 U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
39 U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
45 class TestFunctional(unittest.TestCase):
46 """Functional tests for binman
48 Most of these use a sample .dts file to build an image and then check
49 that it looks correct. The sample files are in the test/ subdirectory
52 For each entry type a very small test file is created using fixed
53 string contents. This makes it easy to test that things look right, and
56 In some cases a 'real' file must be used - these are also supplied in
64 # Handle the case where argv[0] is 'python'
65 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
66 self._binman_pathname = os.path.join(self._binman_dir, 'binman')
68 # Create a temporary directory for input files
69 self._indir = tempfile.mkdtemp(prefix='binmant.')
71 # Create some test files
72 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
73 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
74 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
75 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
76 TestFunctional._MakeInputFile('me.bin', ME_DATA)
77 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
78 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
79 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
80 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
81 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
83 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
84 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
85 U_BOOT_SPL_NODTB_DATA)
86 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
87 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
88 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
89 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
90 self._output_setup = False
92 # ELF file with a '_dt_ucode_base_size' symbol
93 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
94 TestFunctional._MakeInputFile('u-boot', fd.read())
96 # Intel flash descriptor file
97 with open(self.TestFile('descriptor.bin')) as fd:
98 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
101 def tearDownClass(self):
102 """Remove the temporary input directory and its contents"""
104 shutil.rmtree(self._indir)
108 # Enable this to turn on debugging output
109 # tout.Init(tout.DEBUG)
110 command.test_result = None
113 """Remove the temporary output directory"""
114 tools._FinaliseForTest()
116 def _RunBinman(self, *args, **kwargs):
117 """Run binman using the command line
120 Arguments to pass, as a list of strings
121 kwargs: Arguments to pass to Command.RunPipe()
123 result = command.RunPipe([[self._binman_pathname] + list(args)],
124 capture=True, capture_stderr=True, raise_on_error=False)
125 if result.return_code and kwargs.get('raise_on_error', True):
126 raise Exception("Error running '%s': %s" % (' '.join(args),
127 result.stdout + result.stderr))
130 def _DoBinman(self, *args):
131 """Run binman using directly (in the same process)
134 Arguments to pass, as a list of strings
136 Return value (0 for success)
141 (options, args) = cmdline.ParseArgs(args)
142 options.pager = 'binman-invalid-pager'
143 options.build_dir = self._indir
145 # For testing, you can force an increase in verbosity here
146 # options.verbosity = tout.DEBUG
147 return control.Binman(options, args)
149 def _DoTestFile(self, fname, debug=False, map=False):
150 """Run binman with a given test file
153 fname: Device-tree source filename to use (e.g. 05_simple.dts)
154 debug: True to enable debugging output
155 map: True to output map files for the images
157 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
162 return self._DoBinman(*args)
164 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
165 """Set up a new test device-tree file
167 The given file is compiled and set up as the device tree to be used
171 fname: Filename of .dts file to read
172 outfile: Output filename for compiled device-tree binary
175 Contents of device-tree binary
177 if not self._output_setup:
178 tools.PrepareOutputDir(self._indir, True)
179 self._output_setup = True
180 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
181 with open(dtb) as fd:
183 TestFunctional._MakeInputFile(outfile, data)
186 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False):
187 """Run binman and return the resulting image
189 This runs binman with a given test file and then reads the resulting
190 output file. It is a shortcut function since most tests need to do
193 Raises an assertion failure if binman returns a non-zero exit code.
196 fname: Device-tree source filename to use (e.g. 05_simple.dts)
197 use_real_dtb: True to use the test file as the contents of
198 the u-boot-dtb entry. Normally this is not needed and the
199 test contents (the U_BOOT_DTB_DATA string) can be used.
200 But in some test we need the real contents.
201 map: True to output map files for the images
205 Resulting image contents
207 Map data showing contents of image (or None if none)
210 # Use the compiled test file as the u-boot-dtb input
212 dtb_data = self._SetupDtb(fname)
215 retcode = self._DoTestFile(fname, map=map)
216 self.assertEqual(0, retcode)
218 # Find the (only) image, read it and return its contents
219 image = control.images['image']
220 fname = tools.GetOutputFilename('image.bin')
221 self.assertTrue(os.path.exists(fname))
223 map_fname = tools.GetOutputFilename('image.map')
224 with open(map_fname) as fd:
228 with open(fname) as fd:
229 return fd.read(), dtb_data, map_data
231 # Put the test file back
233 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
235 def _DoReadFile(self, fname, use_real_dtb=False):
236 """Helper function which discards the device-tree binary
239 fname: Device-tree source filename to use (e.g. 05_simple.dts)
240 use_real_dtb: True to use the test file as the contents of
241 the u-boot-dtb entry. Normally this is not needed and the
242 test contents (the U_BOOT_DTB_DATA string) can be used.
243 But in some test we need the real contents.
245 return self._DoReadFileDtb(fname, use_real_dtb)[0]
248 def _MakeInputFile(self, fname, contents):
249 """Create a new test input file, creating directories as needed
252 fname: Filenaem to create
253 contents: File contents to write in to the file
255 Full pathname of file created
257 pathname = os.path.join(self._indir, fname)
258 dirname = os.path.dirname(pathname)
259 if dirname and not os.path.exists(dirname):
261 with open(pathname, 'wb') as fd:
266 def TestFile(self, fname):
267 return os.path.join(self._binman_dir, 'test', fname)
269 def AssertInList(self, grep_list, target):
270 """Assert that at least one of a list of things is in a target
273 grep_list: List of strings to check
274 target: Target string
276 for grep in grep_list:
279 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
281 def CheckNoGaps(self, entries):
282 """Check that all entries fit together without gaps
285 entries: List of entries to check
288 for entry in entries.values():
289 self.assertEqual(pos, entry.pos)
292 def GetFdtLen(self, dtb):
293 """Get the totalsize field from a device-tree binary
296 dtb: Device-tree binary contents
299 Total size of device-tree binary, from the header
301 return struct.unpack('>L', dtb[4:8])[0]
304 """Test a basic run with valid args"""
305 result = self._RunBinman('-h')
307 def testFullHelp(self):
308 """Test that the full help is displayed with -H"""
309 result = self._RunBinman('-H')
310 help_file = os.path.join(self._binman_dir, 'README')
311 # Remove possible extraneous strings
312 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
313 gothelp = result.stdout.replace(extra, '')
314 self.assertEqual(len(gothelp), os.path.getsize(help_file))
315 self.assertEqual(0, len(result.stderr))
316 self.assertEqual(0, result.return_code)
318 def testFullHelpInternal(self):
319 """Test that the full help is displayed with -H"""
321 command.test_result = command.CommandResult()
322 result = self._DoBinman('-H')
323 help_file = os.path.join(self._binman_dir, 'README')
325 command.test_result = None
328 """Test that the basic help is displayed with -h"""
329 result = self._RunBinman('-h')
330 self.assertTrue(len(result.stdout) > 200)
331 self.assertEqual(0, len(result.stderr))
332 self.assertEqual(0, result.return_code)
335 """Test that we can run it with a specific board"""
336 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
337 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
338 result = self._DoBinman('-b', 'sandbox')
339 self.assertEqual(0, result)
341 def testNeedBoard(self):
342 """Test that we get an error when no board ius supplied"""
343 with self.assertRaises(ValueError) as e:
344 result = self._DoBinman()
345 self.assertIn("Must provide a board to process (use -b <board>)",
348 def testMissingDt(self):
349 """Test that an invalid device-tree file generates an error"""
350 with self.assertRaises(Exception) as e:
351 self._RunBinman('-d', 'missing_file')
352 # We get one error from libfdt, and a different one from fdtget.
353 self.AssertInList(["Couldn't open blob from 'missing_file'",
354 'No such file or directory'], str(e.exception))
356 def testBrokenDt(self):
357 """Test that an invalid device-tree source file generates an error
359 Since this is a source file it should be compiled and the error
360 will come from the device-tree compiler (dtc).
362 with self.assertRaises(Exception) as e:
363 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
364 self.assertIn("FATAL ERROR: Unable to parse input tree",
367 def testMissingNode(self):
368 """Test that a device tree without a 'binman' node generates an error"""
369 with self.assertRaises(Exception) as e:
370 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
371 self.assertIn("does not have a 'binman' node", str(e.exception))
374 """Test that an empty binman node works OK (i.e. does nothing)"""
375 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
376 self.assertEqual(0, len(result.stderr))
377 self.assertEqual(0, result.return_code)
379 def testInvalidEntry(self):
380 """Test that an invalid entry is flagged"""
381 with self.assertRaises(Exception) as e:
382 result = self._RunBinman('-d',
383 self.TestFile('04_invalid_entry.dts'))
385 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
386 "'/binman/not-a-valid-type'", str(e.exception))
388 def testSimple(self):
389 """Test a simple binman with a single file"""
390 data = self._DoReadFile('05_simple.dts')
391 self.assertEqual(U_BOOT_DATA, data)
393 def testSimpleDebug(self):
394 """Test a simple binman run with debugging enabled"""
395 data = self._DoTestFile('05_simple.dts', debug=True)
398 """Test that we can handle creating two images
400 This also tests image padding.
402 retcode = self._DoTestFile('06_dual_image.dts')
403 self.assertEqual(0, retcode)
405 image = control.images['image1']
406 self.assertEqual(len(U_BOOT_DATA), image._size)
407 fname = tools.GetOutputFilename('image1.bin')
408 self.assertTrue(os.path.exists(fname))
409 with open(fname) as fd:
411 self.assertEqual(U_BOOT_DATA, data)
413 image = control.images['image2']
414 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
415 fname = tools.GetOutputFilename('image2.bin')
416 self.assertTrue(os.path.exists(fname))
417 with open(fname) as fd:
419 self.assertEqual(U_BOOT_DATA, data[3:7])
420 self.assertEqual(chr(0) * 3, data[:3])
421 self.assertEqual(chr(0) * 5, data[7:])
423 def testBadAlign(self):
424 """Test that an invalid alignment value is detected"""
425 with self.assertRaises(ValueError) as e:
426 self._DoTestFile('07_bad_align.dts')
427 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
428 "of two", str(e.exception))
430 def testPackSimple(self):
431 """Test that packing works as expected"""
432 retcode = self._DoTestFile('08_pack.dts')
433 self.assertEqual(0, retcode)
434 self.assertIn('image', control.images)
435 image = control.images['image']
436 entries = image.GetEntries()
437 self.assertEqual(5, len(entries))
440 self.assertIn('u-boot', entries)
441 entry = entries['u-boot']
442 self.assertEqual(0, entry.pos)
443 self.assertEqual(len(U_BOOT_DATA), entry.size)
445 # Second u-boot, aligned to 16-byte boundary
446 self.assertIn('u-boot-align', entries)
447 entry = entries['u-boot-align']
448 self.assertEqual(16, entry.pos)
449 self.assertEqual(len(U_BOOT_DATA), entry.size)
451 # Third u-boot, size 23 bytes
452 self.assertIn('u-boot-size', entries)
453 entry = entries['u-boot-size']
454 self.assertEqual(20, entry.pos)
455 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
456 self.assertEqual(23, entry.size)
458 # Fourth u-boot, placed immediate after the above
459 self.assertIn('u-boot-next', entries)
460 entry = entries['u-boot-next']
461 self.assertEqual(43, entry.pos)
462 self.assertEqual(len(U_BOOT_DATA), entry.size)
464 # Fifth u-boot, placed at a fixed position
465 self.assertIn('u-boot-fixed', entries)
466 entry = entries['u-boot-fixed']
467 self.assertEqual(61, entry.pos)
468 self.assertEqual(len(U_BOOT_DATA), entry.size)
470 self.assertEqual(65, image._size)
472 def testPackExtra(self):
473 """Test that extra packing feature works as expected"""
474 retcode = self._DoTestFile('09_pack_extra.dts')
476 self.assertEqual(0, retcode)
477 self.assertIn('image', control.images)
478 image = control.images['image']
479 entries = image.GetEntries()
480 self.assertEqual(5, len(entries))
482 # First u-boot with padding before and after
483 self.assertIn('u-boot', entries)
484 entry = entries['u-boot']
485 self.assertEqual(0, entry.pos)
486 self.assertEqual(3, entry.pad_before)
487 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
489 # Second u-boot has an aligned size, but it has no effect
490 self.assertIn('u-boot-align-size-nop', entries)
491 entry = entries['u-boot-align-size-nop']
492 self.assertEqual(12, entry.pos)
493 self.assertEqual(4, entry.size)
495 # Third u-boot has an aligned size too
496 self.assertIn('u-boot-align-size', entries)
497 entry = entries['u-boot-align-size']
498 self.assertEqual(16, entry.pos)
499 self.assertEqual(32, entry.size)
501 # Fourth u-boot has an aligned end
502 self.assertIn('u-boot-align-end', entries)
503 entry = entries['u-boot-align-end']
504 self.assertEqual(48, entry.pos)
505 self.assertEqual(16, entry.size)
507 # Fifth u-boot immediately afterwards
508 self.assertIn('u-boot-align-both', entries)
509 entry = entries['u-boot-align-both']
510 self.assertEqual(64, entry.pos)
511 self.assertEqual(64, entry.size)
513 self.CheckNoGaps(entries)
514 self.assertEqual(128, image._size)
516 def testPackAlignPowerOf2(self):
517 """Test that invalid entry alignment is detected"""
518 with self.assertRaises(ValueError) as e:
519 self._DoTestFile('10_pack_align_power2.dts')
520 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
521 "of two", str(e.exception))
523 def testPackAlignSizePowerOf2(self):
524 """Test that invalid entry size alignment is detected"""
525 with self.assertRaises(ValueError) as e:
526 self._DoTestFile('11_pack_align_size_power2.dts')
527 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
528 "power of two", str(e.exception))
530 def testPackInvalidAlign(self):
531 """Test detection of an position that does not match its alignment"""
532 with self.assertRaises(ValueError) as e:
533 self._DoTestFile('12_pack_inv_align.dts')
534 self.assertIn("Node '/binman/u-boot': Position 0x5 (5) does not match "
535 "align 0x4 (4)", str(e.exception))
537 def testPackInvalidSizeAlign(self):
538 """Test that invalid entry size alignment is detected"""
539 with self.assertRaises(ValueError) as e:
540 self._DoTestFile('13_pack_inv_size_align.dts')
541 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
542 "align-size 0x4 (4)", str(e.exception))
544 def testPackOverlap(self):
545 """Test that overlapping regions are detected"""
546 with self.assertRaises(ValueError) as e:
547 self._DoTestFile('14_pack_overlap.dts')
548 self.assertIn("Node '/binman/u-boot-align': Position 0x3 (3) overlaps "
549 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
552 def testPackEntryOverflow(self):
553 """Test that entries that overflow their size are detected"""
554 with self.assertRaises(ValueError) as e:
555 self._DoTestFile('15_pack_overflow.dts')
556 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
557 "but entry size is 0x3 (3)", str(e.exception))
559 def testPackImageOverflow(self):
560 """Test that entries which overflow the image size are detected"""
561 with self.assertRaises(ValueError) as e:
562 self._DoTestFile('16_pack_image_overflow.dts')
563 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
564 "size 0x3 (3)", str(e.exception))
566 def testPackImageSize(self):
567 """Test that the image size can be set"""
568 retcode = self._DoTestFile('17_pack_image_size.dts')
569 self.assertEqual(0, retcode)
570 self.assertIn('image', control.images)
571 image = control.images['image']
572 self.assertEqual(7, image._size)
574 def testPackImageSizeAlign(self):
575 """Test that image size alignemnt works as expected"""
576 retcode = self._DoTestFile('18_pack_image_align.dts')
577 self.assertEqual(0, retcode)
578 self.assertIn('image', control.images)
579 image = control.images['image']
580 self.assertEqual(16, image._size)
582 def testPackInvalidImageAlign(self):
583 """Test that invalid image alignment is detected"""
584 with self.assertRaises(ValueError) as e:
585 self._DoTestFile('19_pack_inv_image_align.dts')
586 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
587 "align-size 0x8 (8)", str(e.exception))
589 def testPackAlignPowerOf2(self):
590 """Test that invalid image alignment is detected"""
591 with self.assertRaises(ValueError) as e:
592 self._DoTestFile('20_pack_inv_image_align_power2.dts')
593 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
594 "two", str(e.exception))
596 def testImagePadByte(self):
597 """Test that the image pad byte can be specified"""
598 with open(self.TestFile('bss_data')) as fd:
599 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
600 data = self._DoReadFile('21_image_pad.dts')
601 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
603 def testImageName(self):
604 """Test that image files can be named"""
605 retcode = self._DoTestFile('22_image_name.dts')
606 self.assertEqual(0, retcode)
607 image = control.images['image1']
608 fname = tools.GetOutputFilename('test-name')
609 self.assertTrue(os.path.exists(fname))
611 image = control.images['image2']
612 fname = tools.GetOutputFilename('test-name.xx')
613 self.assertTrue(os.path.exists(fname))
615 def testBlobFilename(self):
616 """Test that generic blobs can be provided by filename"""
617 data = self._DoReadFile('23_blob.dts')
618 self.assertEqual(BLOB_DATA, data)
620 def testPackSorted(self):
621 """Test that entries can be sorted"""
622 data = self._DoReadFile('24_sorted.dts')
623 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
626 def testPackZeroPosition(self):
627 """Test that an entry at position 0 is not given a new position"""
628 with self.assertRaises(ValueError) as e:
629 self._DoTestFile('25_pack_zero_size.dts')
630 self.assertIn("Node '/binman/u-boot-spl': Position 0x0 (0) overlaps "
631 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
634 def testPackUbootDtb(self):
635 """Test that a device tree can be added to U-Boot"""
636 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
637 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
639 def testPackX86RomNoSize(self):
640 """Test that the end-at-4gb property requires a size property"""
641 with self.assertRaises(ValueError) as e:
642 self._DoTestFile('27_pack_4gb_no_size.dts')
643 self.assertIn("Section '/binman': Section size must be provided when "
644 "using end-at-4gb", str(e.exception))
646 def testPackX86RomOutside(self):
647 """Test that the end-at-4gb property checks for position boundaries"""
648 with self.assertRaises(ValueError) as e:
649 self._DoTestFile('28_pack_4gb_outside.dts')
650 self.assertIn("Node '/binman/u-boot': Position 0x0 (0) is outside "
651 "the section starting at 0xffffffe0 (4294967264)",
654 def testPackX86Rom(self):
655 """Test that a basic x86 ROM can be created"""
656 data = self._DoReadFile('29_x86-rom.dts')
657 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
660 def testPackX86RomMeNoDesc(self):
661 """Test that an invalid Intel descriptor entry is detected"""
662 TestFunctional._MakeInputFile('descriptor.bin', '')
663 with self.assertRaises(ValueError) as e:
664 self._DoTestFile('31_x86-rom-me.dts')
665 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
666 "signature", str(e.exception))
668 def testPackX86RomBadDesc(self):
669 """Test that the Intel requires a descriptor entry"""
670 with self.assertRaises(ValueError) as e:
671 self._DoTestFile('30_x86-rom-me-no-desc.dts')
672 self.assertIn("Node '/binman/intel-me': No position set with "
673 "pos-unset: should another entry provide this correct "
674 "position?", str(e.exception))
676 def testPackX86RomMe(self):
677 """Test that an x86 ROM with an ME region can be created"""
678 data = self._DoReadFile('31_x86-rom-me.dts')
679 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
681 def testPackVga(self):
682 """Test that an image with a VGA binary can be created"""
683 data = self._DoReadFile('32_intel-vga.dts')
684 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
686 def testPackStart16(self):
687 """Test that an image with an x86 start16 region can be created"""
688 data = self._DoReadFile('33_x86-start16.dts')
689 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
691 def _RunMicrocodeTest(self, dts_fname, nodtb_data):
692 data = self._DoReadFile(dts_fname, True)
694 # Now check the device tree has no microcode
695 second = data[len(nodtb_data):]
696 fname = tools.GetOutputFilename('test.dtb')
697 with open(fname, 'wb') as fd:
699 dtb = fdt.FdtScan(fname)
700 ucode = dtb.GetNode('/microcode')
701 self.assertTrue(ucode)
702 for node in ucode.subnodes:
703 self.assertFalse(node.props.get('data'))
705 fdt_len = self.GetFdtLen(second)
706 third = second[fdt_len:]
708 # Check that the microcode appears immediately after the Fdt
709 # This matches the concatenation of the data properties in
710 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
711 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
713 self.assertEqual(ucode_data, third[:len(ucode_data)])
714 ucode_pos = len(nodtb_data) + fdt_len
716 # Check that the microcode pointer was inserted. It should match the
717 # expected position and size
718 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
720 first = data[:len(nodtb_data)]
721 return first, pos_and_size
723 def testPackUbootMicrocode(self):
724 """Test that x86 microcode can be handled correctly
726 We expect to see the following in the image, in order:
727 u-boot-nodtb.bin with a microcode pointer inserted at the correct
729 u-boot.dtb with the microcode removed
732 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
734 self.assertEqual('nodtb with microcode' + pos_and_size +
735 ' somewhere in here', first)
737 def _RunPackUbootSingleMicrocode(self):
738 """Test that x86 microcode can be handled correctly
740 We expect to see the following in the image, in order:
741 u-boot-nodtb.bin with a microcode pointer inserted at the correct
743 u-boot.dtb with the microcode
744 an empty microcode region
746 # We need the libfdt library to run this test since only that allows
747 # finding the offset of a property. This is required by
748 # Entry_u_boot_dtb_with_ucode.ObtainContents().
749 data = self._DoReadFile('35_x86_single_ucode.dts', True)
751 second = data[len(U_BOOT_NODTB_DATA):]
753 fdt_len = self.GetFdtLen(second)
754 third = second[fdt_len:]
755 second = second[:fdt_len]
757 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
758 self.assertIn(ucode_data, second)
759 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
761 # Check that the microcode pointer was inserted. It should match the
762 # expected position and size
763 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
765 first = data[:len(U_BOOT_NODTB_DATA)]
766 self.assertEqual('nodtb with microcode' + pos_and_size +
767 ' somewhere in here', first)
769 def testPackUbootSingleMicrocode(self):
770 """Test that x86 microcode can be handled correctly with fdt_normal.
772 self._RunPackUbootSingleMicrocode()
774 def testUBootImg(self):
775 """Test that u-boot.img can be put in a file"""
776 data = self._DoReadFile('36_u_boot_img.dts')
777 self.assertEqual(U_BOOT_IMG_DATA, data)
779 def testNoMicrocode(self):
780 """Test that a missing microcode region is detected"""
781 with self.assertRaises(ValueError) as e:
782 self._DoReadFile('37_x86_no_ucode.dts', True)
783 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
784 "node found in ", str(e.exception))
786 def testMicrocodeWithoutNode(self):
787 """Test that a missing u-boot-dtb-with-ucode node is detected"""
788 with self.assertRaises(ValueError) as e:
789 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
790 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
791 "microcode region u-boot-dtb-with-ucode", str(e.exception))
793 def testMicrocodeWithoutNode2(self):
794 """Test that a missing u-boot-ucode node is detected"""
795 with self.assertRaises(ValueError) as e:
796 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
797 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
798 "microcode region u-boot-ucode", str(e.exception))
800 def testMicrocodeWithoutPtrInElf(self):
801 """Test that a U-Boot binary without the microcode symbol is detected"""
802 # ELF file without a '_dt_ucode_base_size' symbol
804 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
805 TestFunctional._MakeInputFile('u-boot', fd.read())
807 with self.assertRaises(ValueError) as e:
808 self._RunPackUbootSingleMicrocode()
809 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
810 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
813 # Put the original file back
814 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
815 TestFunctional._MakeInputFile('u-boot', fd.read())
817 def testMicrocodeNotInImage(self):
818 """Test that microcode must be placed within the image"""
819 with self.assertRaises(ValueError) as e:
820 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
821 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
822 "pointer _dt_ucode_base_size at fffffe14 is outside the "
823 "section ranging from 00000000 to 0000002e", str(e.exception))
825 def testWithoutMicrocode(self):
826 """Test that we can cope with an image without microcode (e.g. qemu)"""
827 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
828 TestFunctional._MakeInputFile('u-boot', fd.read())
829 data, dtb, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
831 # Now check the device tree has no microcode
832 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
833 second = data[len(U_BOOT_NODTB_DATA):]
835 fdt_len = self.GetFdtLen(second)
836 self.assertEqual(dtb, second[:fdt_len])
838 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
839 third = data[used_len:]
840 self.assertEqual(chr(0) * (0x200 - used_len), third)
842 def testUnknownPosSize(self):
843 """Test that microcode must be placed within the image"""
844 with self.assertRaises(ValueError) as e:
845 self._DoReadFile('41_unknown_pos_size.dts', True)
846 self.assertIn("Section '/binman': Unable to set pos/size for unknown "
847 "entry 'invalid-entry'", str(e.exception))
849 def testPackFsp(self):
850 """Test that an image with a FSP binary can be created"""
851 data = self._DoReadFile('42_intel-fsp.dts')
852 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
854 def testPackCmc(self):
855 """Test that an image with a CMC binary can be created"""
856 data = self._DoReadFile('43_intel-cmc.dts')
857 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
859 def testPackVbt(self):
860 """Test that an image with a VBT binary can be created"""
861 data = self._DoReadFile('46_intel-vbt.dts')
862 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
864 def testSplBssPad(self):
865 """Test that we can pad SPL's BSS with zeros"""
866 # ELF file with a '__bss_size' symbol
867 with open(self.TestFile('bss_data')) as fd:
868 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
869 data = self._DoReadFile('47_spl_bss_pad.dts')
870 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
872 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
873 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
874 with self.assertRaises(ValueError) as e:
875 data = self._DoReadFile('47_spl_bss_pad.dts')
876 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
879 def testPackStart16Spl(self):
880 """Test that an image with an x86 start16 region can be created"""
881 data = self._DoReadFile('48_x86-start16-spl.dts')
882 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
884 def testPackUbootSplMicrocode(self):
885 """Test that x86 microcode can be handled correctly in SPL
887 We expect to see the following in the image, in order:
888 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
890 u-boot.dtb with the microcode removed
893 # ELF file with a '_dt_ucode_base_size' symbol
894 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
895 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
896 first, pos_and_size = self._RunMicrocodeTest('49_x86_ucode_spl.dts',
897 U_BOOT_SPL_NODTB_DATA)
898 self.assertEqual('splnodtb with microc' + pos_and_size +
899 'ter somewhere in here', first)
901 def testPackMrc(self):
902 """Test that an image with an MRC binary can be created"""
903 data = self._DoReadFile('50_intel_mrc.dts')
904 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
906 def testSplDtb(self):
907 """Test that an image with spl/u-boot-spl.dtb can be created"""
908 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
909 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
911 def testSplNoDtb(self):
912 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
913 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
914 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
916 def testSymbols(self):
917 """Test binman can assign symbols embedded in U-Boot"""
918 elf_fname = self.TestFile('u_boot_binman_syms')
919 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
920 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
921 self.assertEqual(syms['_binman_u_boot_spl_prop_pos'].address, addr)
923 with open(self.TestFile('u_boot_binman_syms')) as fd:
924 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
925 data = self._DoReadFile('53_symbols.dts')
926 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
927 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
929 sym_values + U_BOOT_SPL_DATA[16:])
930 self.assertEqual(expected, data)
932 def testPackUnitAddress(self):
933 """Test that we support multiple binaries with the same name"""
934 data = self._DoReadFile('54_unit_address.dts')
935 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
937 def testSections(self):
938 """Basic test of sections"""
939 data = self._DoReadFile('55_sections.dts')
940 expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 + '&' * 8
941 self.assertEqual(expected, data)
944 """Tests outputting a map of the images"""
945 _, _, map_data = self._DoReadFileDtb('55_sections.dts', map=True)
946 self.assertEqual('''Position Size Name
947 00000000 00000010 section@0
948 00000000 00000004 u-boot
949 00000010 00000010 section@1
950 00000000 00000004 u-boot
953 def testNamePrefix(self):
954 """Tests that name prefixes are used"""
955 _, _, map_data = self._DoReadFileDtb('56_name_prefix.dts', map=True)
956 self.assertEqual('''Position Size Name
957 00000000 00000010 section@0
958 00000000 00000004 ro-u-boot
959 00000010 00000010 section@1
960 00000000 00000004 rw-u-boot
963 if __name__ == "__main__":