blob: c54cd12e717bd91fc69aee3ff635411147ec095b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass4f443042016-11-25 20:15:52 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass4f443042016-11-25 20:15:52 -07005# To run a single test, change to this directory, and:
6#
7# python -m unittest func_test.TestFunctional.testHelp
8
9from optparse import OptionParser
10import os
11import shutil
12import struct
13import sys
14import tempfile
15import unittest
16
17import binman
18import cmdline
19import command
20import control
Simon Glass19790632017-11-13 18:55:01 -070021import elf
Simon Glass99ed4a22017-05-27 07:38:30 -060022import fdt
Simon Glass4f443042016-11-25 20:15:52 -070023import fdt_util
24import tools
25import tout
26
27# Contents of test files, corresponding to different entry types
Simon Glass6b187df2017-11-12 21:52:27 -070028U_BOOT_DATA = '1234'
29U_BOOT_IMG_DATA = 'img'
Simon Glassf6898902017-11-13 18:54:59 -070030U_BOOT_SPL_DATA = '56780123456789abcde'
Simon Glass6b187df2017-11-12 21:52:27 -070031BLOB_DATA = '89'
32ME_DATA = '0abcd'
33VGA_DATA = 'vga'
34U_BOOT_DTB_DATA = 'udtb'
Simon Glass47419ea2017-11-13 18:54:55 -070035U_BOOT_SPL_DTB_DATA = 'spldtb'
Simon Glass6b187df2017-11-12 21:52:27 -070036X86_START16_DATA = 'start16'
37X86_START16_SPL_DATA = 'start16spl'
38U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
39U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
40FSP_DATA = 'fsp'
41CMC_DATA = 'cmc'
42VBT_DATA = 'vbt'
Simon Glassca4f4ff2017-11-12 21:52:28 -070043MRC_DATA = 'mrc'
Simon Glass4f443042016-11-25 20:15:52 -070044
45class TestFunctional(unittest.TestCase):
46 """Functional tests for binman
47
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
50 and are numbered.
51
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
54 debug problems.
55
56 In some cases a 'real' file must be used - these are also supplied in
57 the test/ diurectory.
58 """
59 @classmethod
60 def setUpClass(self):
Simon Glass4d5994f2017-11-12 21:52:20 -070061 global entry
62 import entry
63
Simon Glass4f443042016-11-25 20:15:52 -070064 # 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')
67
68 # Create a temporary directory for input files
69 self._indir = tempfile.mkdtemp(prefix='binmant.')
70
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)
Simon Glasse0ff8552016-11-25 20:15:53 -070076 TestFunctional._MakeInputFile('me.bin', ME_DATA)
77 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070078 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
Simon Glass47419ea2017-11-13 18:54:55 -070079 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -070080 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Simon Glass87722132017-11-12 21:52:26 -070081 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
82 X86_START16_SPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070083 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass6b187df2017-11-12 21:52:27 -070084 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
85 U_BOOT_SPL_NODTB_DATA)
Simon Glassda229092016-11-25 20:15:56 -070086 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
87 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Meng59ea8c22017-08-15 22:41:54 -070088 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassca4f4ff2017-11-12 21:52:28 -070089 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070090 self._output_setup = False
91
Simon Glasse0ff8552016-11-25 20:15:53 -070092 # 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())
95
96 # Intel flash descriptor file
97 with open(self.TestFile('descriptor.bin')) as fd:
98 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
99
Simon Glass4f443042016-11-25 20:15:52 -0700100 @classmethod
101 def tearDownClass(self):
102 """Remove the temporary input directory and its contents"""
103 if self._indir:
104 shutil.rmtree(self._indir)
105 self._indir = None
106
107 def setUp(self):
108 # Enable this to turn on debugging output
109 # tout.Init(tout.DEBUG)
110 command.test_result = None
111
112 def tearDown(self):
113 """Remove the temporary output directory"""
114 tools._FinaliseForTest()
115
116 def _RunBinman(self, *args, **kwargs):
117 """Run binman using the command line
118
119 Args:
120 Arguments to pass, as a list of strings
121 kwargs: Arguments to pass to Command.RunPipe()
122 """
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))
128 return result
129
130 def _DoBinman(self, *args):
131 """Run binman using directly (in the same process)
132
133 Args:
134 Arguments to pass, as a list of strings
135 Returns:
136 Return value (0 for success)
137 """
Simon Glass7fe91732017-11-13 18:55:00 -0700138 args = list(args)
139 if '-D' in sys.argv:
140 args = args + ['-D']
141 (options, args) = cmdline.ParseArgs(args)
Simon Glass4f443042016-11-25 20:15:52 -0700142 options.pager = 'binman-invalid-pager'
143 options.build_dir = self._indir
144
145 # For testing, you can force an increase in verbosity here
146 # options.verbosity = tout.DEBUG
147 return control.Binman(options, args)
148
Simon Glass53af22a2018-07-17 13:25:32 -0600149 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
150 entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700151 """Run binman with a given test file
152
153 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600154 fname: Device-tree source filename to use (e.g. 05_simple.dts)
155 debug: True to enable debugging output
Simon Glass3b0c3822018-06-01 09:38:20 -0600156 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600157 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600158 tree before packing it into the image
Simon Glass4f443042016-11-25 20:15:52 -0700159 """
Simon Glass7fe91732017-11-13 18:55:00 -0700160 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
161 if debug:
162 args.append('-D')
Simon Glass3b0c3822018-06-01 09:38:20 -0600163 if map:
164 args.append('-m')
Simon Glass16b8d6b2018-07-06 10:27:42 -0600165 if update_dtb:
166 args.append('-up')
Simon Glass53af22a2018-07-17 13:25:32 -0600167 if entry_args:
168 for arg, value in entry_args.iteritems():
169 args.append('-a%s=%s' % (arg, value))
Simon Glass7fe91732017-11-13 18:55:00 -0700170 return self._DoBinman(*args)
Simon Glass4f443042016-11-25 20:15:52 -0700171
172 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glasse0ff8552016-11-25 20:15:53 -0700173 """Set up a new test device-tree file
174
175 The given file is compiled and set up as the device tree to be used
176 for ths test.
177
178 Args:
179 fname: Filename of .dts file to read
Simon Glass7ae5f312018-06-01 09:38:19 -0600180 outfile: Output filename for compiled device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700181
182 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600183 Contents of device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700184 """
Simon Glass4f443042016-11-25 20:15:52 -0700185 if not self._output_setup:
186 tools.PrepareOutputDir(self._indir, True)
187 self._output_setup = True
188 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
189 with open(dtb) as fd:
190 data = fd.read()
191 TestFunctional._MakeInputFile(outfile, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700192 return data
Simon Glass4f443042016-11-25 20:15:52 -0700193
Simon Glass16b8d6b2018-07-06 10:27:42 -0600194 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass53af22a2018-07-17 13:25:32 -0600195 update_dtb=False, entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700196 """Run binman and return the resulting image
197
198 This runs binman with a given test file and then reads the resulting
199 output file. It is a shortcut function since most tests need to do
200 these steps.
201
202 Raises an assertion failure if binman returns a non-zero exit code.
203
204 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600205 fname: Device-tree source filename to use (e.g. 05_simple.dts)
Simon Glass4f443042016-11-25 20:15:52 -0700206 use_real_dtb: True to use the test file as the contents of
207 the u-boot-dtb entry. Normally this is not needed and the
208 test contents (the U_BOOT_DTB_DATA string) can be used.
209 But in some test we need the real contents.
Simon Glass3b0c3822018-06-01 09:38:20 -0600210 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600211 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600212 tree before packing it into the image
Simon Glasse0ff8552016-11-25 20:15:53 -0700213
214 Returns:
215 Tuple:
216 Resulting image contents
217 Device tree contents
Simon Glass3b0c3822018-06-01 09:38:20 -0600218 Map data showing contents of image (or None if none)
Simon Glassea6922e2018-07-17 13:25:27 -0600219 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass4f443042016-11-25 20:15:52 -0700220 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700221 dtb_data = None
Simon Glass4f443042016-11-25 20:15:52 -0700222 # Use the compiled test file as the u-boot-dtb input
223 if use_real_dtb:
Simon Glasse0ff8552016-11-25 20:15:53 -0700224 dtb_data = self._SetupDtb(fname)
Simon Glass4f443042016-11-25 20:15:52 -0700225
226 try:
Simon Glass53af22a2018-07-17 13:25:32 -0600227 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
228 entry_args=entry_args)
Simon Glass4f443042016-11-25 20:15:52 -0700229 self.assertEqual(0, retcode)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600230 out_dtb_fname = control.GetFdtPath('u-boot.dtb')
Simon Glass4f443042016-11-25 20:15:52 -0700231
232 # Find the (only) image, read it and return its contents
233 image = control.images['image']
Simon Glass16b8d6b2018-07-06 10:27:42 -0600234 image_fname = tools.GetOutputFilename('image.bin')
235 self.assertTrue(os.path.exists(image_fname))
Simon Glass3b0c3822018-06-01 09:38:20 -0600236 if map:
237 map_fname = tools.GetOutputFilename('image.map')
238 with open(map_fname) as fd:
239 map_data = fd.read()
240 else:
241 map_data = None
Simon Glass16b8d6b2018-07-06 10:27:42 -0600242 with open(image_fname) as fd:
243 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass4f443042016-11-25 20:15:52 -0700244 finally:
245 # Put the test file back
246 if use_real_dtb:
247 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
248
Simon Glasse0ff8552016-11-25 20:15:53 -0700249 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass7ae5f312018-06-01 09:38:19 -0600250 """Helper function which discards the device-tree binary
251
252 Args:
253 fname: Device-tree source filename to use (e.g. 05_simple.dts)
254 use_real_dtb: True to use the test file as the contents of
255 the u-boot-dtb entry. Normally this is not needed and the
256 test contents (the U_BOOT_DTB_DATA string) can be used.
257 But in some test we need the real contents.
Simon Glassea6922e2018-07-17 13:25:27 -0600258
259 Returns:
260 Resulting image contents
Simon Glass7ae5f312018-06-01 09:38:19 -0600261 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700262 return self._DoReadFileDtb(fname, use_real_dtb)[0]
263
Simon Glass4f443042016-11-25 20:15:52 -0700264 @classmethod
265 def _MakeInputFile(self, fname, contents):
266 """Create a new test input file, creating directories as needed
267
268 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600269 fname: Filename to create
Simon Glass4f443042016-11-25 20:15:52 -0700270 contents: File contents to write in to the file
271 Returns:
272 Full pathname of file created
273 """
274 pathname = os.path.join(self._indir, fname)
275 dirname = os.path.dirname(pathname)
276 if dirname and not os.path.exists(dirname):
277 os.makedirs(dirname)
278 with open(pathname, 'wb') as fd:
279 fd.write(contents)
280 return pathname
281
282 @classmethod
283 def TestFile(self, fname):
284 return os.path.join(self._binman_dir, 'test', fname)
285
286 def AssertInList(self, grep_list, target):
287 """Assert that at least one of a list of things is in a target
288
289 Args:
290 grep_list: List of strings to check
291 target: Target string
292 """
293 for grep in grep_list:
294 if grep in target:
295 return
296 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
297
298 def CheckNoGaps(self, entries):
299 """Check that all entries fit together without gaps
300
301 Args:
302 entries: List of entries to check
303 """
Simon Glass3ab95982018-08-01 15:22:37 -0600304 offset = 0
Simon Glass4f443042016-11-25 20:15:52 -0700305 for entry in entries.values():
Simon Glass3ab95982018-08-01 15:22:37 -0600306 self.assertEqual(offset, entry.offset)
307 offset += entry.size
Simon Glass4f443042016-11-25 20:15:52 -0700308
Simon Glasse0ff8552016-11-25 20:15:53 -0700309 def GetFdtLen(self, dtb):
Simon Glass7ae5f312018-06-01 09:38:19 -0600310 """Get the totalsize field from a device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700311
312 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600313 dtb: Device-tree binary contents
Simon Glasse0ff8552016-11-25 20:15:53 -0700314
315 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600316 Total size of device-tree binary, from the header
Simon Glasse0ff8552016-11-25 20:15:53 -0700317 """
318 return struct.unpack('>L', dtb[4:8])[0]
319
Simon Glass16b8d6b2018-07-06 10:27:42 -0600320 def _GetPropTree(self, dtb_data, node_names):
321 def AddNode(node, path):
322 if node.name != '/':
323 path += '/' + node.name
Simon Glass16b8d6b2018-07-06 10:27:42 -0600324 for subnode in node.subnodes:
325 for prop in subnode.props.values():
326 if prop.name in node_names:
327 prop_path = path + '/' + subnode.name + ':' + prop.name
328 tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
329 prop.value)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600330 AddNode(subnode, path)
331
332 tree = {}
333 dtb = fdt.Fdt(dtb_data)
334 dtb.Scan()
335 AddNode(dtb.GetRoot(), '')
336 return tree
337
Simon Glass4f443042016-11-25 20:15:52 -0700338 def testRun(self):
339 """Test a basic run with valid args"""
340 result = self._RunBinman('-h')
341
342 def testFullHelp(self):
343 """Test that the full help is displayed with -H"""
344 result = self._RunBinman('-H')
345 help_file = os.path.join(self._binman_dir, 'README')
Tom Rini3759df02018-01-16 15:29:50 -0500346 # Remove possible extraneous strings
347 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
348 gothelp = result.stdout.replace(extra, '')
349 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass4f443042016-11-25 20:15:52 -0700350 self.assertEqual(0, len(result.stderr))
351 self.assertEqual(0, result.return_code)
352
353 def testFullHelpInternal(self):
354 """Test that the full help is displayed with -H"""
355 try:
356 command.test_result = command.CommandResult()
357 result = self._DoBinman('-H')
358 help_file = os.path.join(self._binman_dir, 'README')
359 finally:
360 command.test_result = None
361
362 def testHelp(self):
363 """Test that the basic help is displayed with -h"""
364 result = self._RunBinman('-h')
365 self.assertTrue(len(result.stdout) > 200)
366 self.assertEqual(0, len(result.stderr))
367 self.assertEqual(0, result.return_code)
368
Simon Glass4f443042016-11-25 20:15:52 -0700369 def testBoard(self):
370 """Test that we can run it with a specific board"""
371 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
372 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
373 result = self._DoBinman('-b', 'sandbox')
374 self.assertEqual(0, result)
375
376 def testNeedBoard(self):
377 """Test that we get an error when no board ius supplied"""
378 with self.assertRaises(ValueError) as e:
379 result = self._DoBinman()
380 self.assertIn("Must provide a board to process (use -b <board>)",
381 str(e.exception))
382
383 def testMissingDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600384 """Test that an invalid device-tree file generates an error"""
Simon Glass4f443042016-11-25 20:15:52 -0700385 with self.assertRaises(Exception) as e:
386 self._RunBinman('-d', 'missing_file')
387 # We get one error from libfdt, and a different one from fdtget.
388 self.AssertInList(["Couldn't open blob from 'missing_file'",
389 'No such file or directory'], str(e.exception))
390
391 def testBrokenDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600392 """Test that an invalid device-tree source file generates an error
Simon Glass4f443042016-11-25 20:15:52 -0700393
394 Since this is a source file it should be compiled and the error
395 will come from the device-tree compiler (dtc).
396 """
397 with self.assertRaises(Exception) as e:
398 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
399 self.assertIn("FATAL ERROR: Unable to parse input tree",
400 str(e.exception))
401
402 def testMissingNode(self):
403 """Test that a device tree without a 'binman' node generates an error"""
404 with self.assertRaises(Exception) as e:
405 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
406 self.assertIn("does not have a 'binman' node", str(e.exception))
407
408 def testEmpty(self):
409 """Test that an empty binman node works OK (i.e. does nothing)"""
410 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
411 self.assertEqual(0, len(result.stderr))
412 self.assertEqual(0, result.return_code)
413
414 def testInvalidEntry(self):
415 """Test that an invalid entry is flagged"""
416 with self.assertRaises(Exception) as e:
417 result = self._RunBinman('-d',
418 self.TestFile('04_invalid_entry.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700419 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
420 "'/binman/not-a-valid-type'", str(e.exception))
421
422 def testSimple(self):
423 """Test a simple binman with a single file"""
424 data = self._DoReadFile('05_simple.dts')
425 self.assertEqual(U_BOOT_DATA, data)
426
Simon Glass7fe91732017-11-13 18:55:00 -0700427 def testSimpleDebug(self):
428 """Test a simple binman run with debugging enabled"""
429 data = self._DoTestFile('05_simple.dts', debug=True)
430
Simon Glass4f443042016-11-25 20:15:52 -0700431 def testDual(self):
432 """Test that we can handle creating two images
433
434 This also tests image padding.
435 """
436 retcode = self._DoTestFile('06_dual_image.dts')
437 self.assertEqual(0, retcode)
438
439 image = control.images['image1']
440 self.assertEqual(len(U_BOOT_DATA), image._size)
441 fname = tools.GetOutputFilename('image1.bin')
442 self.assertTrue(os.path.exists(fname))
443 with open(fname) as fd:
444 data = fd.read()
445 self.assertEqual(U_BOOT_DATA, data)
446
447 image = control.images['image2']
448 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
449 fname = tools.GetOutputFilename('image2.bin')
450 self.assertTrue(os.path.exists(fname))
451 with open(fname) as fd:
452 data = fd.read()
453 self.assertEqual(U_BOOT_DATA, data[3:7])
454 self.assertEqual(chr(0) * 3, data[:3])
455 self.assertEqual(chr(0) * 5, data[7:])
456
457 def testBadAlign(self):
458 """Test that an invalid alignment value is detected"""
459 with self.assertRaises(ValueError) as e:
460 self._DoTestFile('07_bad_align.dts')
461 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
462 "of two", str(e.exception))
463
464 def testPackSimple(self):
465 """Test that packing works as expected"""
466 retcode = self._DoTestFile('08_pack.dts')
467 self.assertEqual(0, retcode)
468 self.assertIn('image', control.images)
469 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600470 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700471 self.assertEqual(5, len(entries))
472
473 # First u-boot
474 self.assertIn('u-boot', entries)
475 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600476 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700477 self.assertEqual(len(U_BOOT_DATA), entry.size)
478
479 # Second u-boot, aligned to 16-byte boundary
480 self.assertIn('u-boot-align', entries)
481 entry = entries['u-boot-align']
Simon Glass3ab95982018-08-01 15:22:37 -0600482 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700483 self.assertEqual(len(U_BOOT_DATA), entry.size)
484
485 # Third u-boot, size 23 bytes
486 self.assertIn('u-boot-size', entries)
487 entry = entries['u-boot-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600488 self.assertEqual(20, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700489 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
490 self.assertEqual(23, entry.size)
491
492 # Fourth u-boot, placed immediate after the above
493 self.assertIn('u-boot-next', entries)
494 entry = entries['u-boot-next']
Simon Glass3ab95982018-08-01 15:22:37 -0600495 self.assertEqual(43, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700496 self.assertEqual(len(U_BOOT_DATA), entry.size)
497
Simon Glass3ab95982018-08-01 15:22:37 -0600498 # Fifth u-boot, placed at a fixed offset
Simon Glass4f443042016-11-25 20:15:52 -0700499 self.assertIn('u-boot-fixed', entries)
500 entry = entries['u-boot-fixed']
Simon Glass3ab95982018-08-01 15:22:37 -0600501 self.assertEqual(61, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700502 self.assertEqual(len(U_BOOT_DATA), entry.size)
503
504 self.assertEqual(65, image._size)
505
506 def testPackExtra(self):
507 """Test that extra packing feature works as expected"""
508 retcode = self._DoTestFile('09_pack_extra.dts')
509
510 self.assertEqual(0, retcode)
511 self.assertIn('image', control.images)
512 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600513 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700514 self.assertEqual(5, len(entries))
515
516 # First u-boot with padding before and after
517 self.assertIn('u-boot', entries)
518 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600519 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700520 self.assertEqual(3, entry.pad_before)
521 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
522
523 # Second u-boot has an aligned size, but it has no effect
524 self.assertIn('u-boot-align-size-nop', entries)
525 entry = entries['u-boot-align-size-nop']
Simon Glass3ab95982018-08-01 15:22:37 -0600526 self.assertEqual(12, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700527 self.assertEqual(4, entry.size)
528
529 # Third u-boot has an aligned size too
530 self.assertIn('u-boot-align-size', entries)
531 entry = entries['u-boot-align-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600532 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700533 self.assertEqual(32, entry.size)
534
535 # Fourth u-boot has an aligned end
536 self.assertIn('u-boot-align-end', entries)
537 entry = entries['u-boot-align-end']
Simon Glass3ab95982018-08-01 15:22:37 -0600538 self.assertEqual(48, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700539 self.assertEqual(16, entry.size)
540
541 # Fifth u-boot immediately afterwards
542 self.assertIn('u-boot-align-both', entries)
543 entry = entries['u-boot-align-both']
Simon Glass3ab95982018-08-01 15:22:37 -0600544 self.assertEqual(64, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700545 self.assertEqual(64, entry.size)
546
547 self.CheckNoGaps(entries)
548 self.assertEqual(128, image._size)
549
550 def testPackAlignPowerOf2(self):
551 """Test that invalid entry alignment is detected"""
552 with self.assertRaises(ValueError) as e:
553 self._DoTestFile('10_pack_align_power2.dts')
554 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
555 "of two", str(e.exception))
556
557 def testPackAlignSizePowerOf2(self):
558 """Test that invalid entry size alignment is detected"""
559 with self.assertRaises(ValueError) as e:
560 self._DoTestFile('11_pack_align_size_power2.dts')
561 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
562 "power of two", str(e.exception))
563
564 def testPackInvalidAlign(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600565 """Test detection of an offset that does not match its alignment"""
Simon Glass4f443042016-11-25 20:15:52 -0700566 with self.assertRaises(ValueError) as e:
567 self._DoTestFile('12_pack_inv_align.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600568 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700569 "align 0x4 (4)", str(e.exception))
570
571 def testPackInvalidSizeAlign(self):
572 """Test that invalid entry size alignment is detected"""
573 with self.assertRaises(ValueError) as e:
574 self._DoTestFile('13_pack_inv_size_align.dts')
575 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
576 "align-size 0x4 (4)", str(e.exception))
577
578 def testPackOverlap(self):
579 """Test that overlapping regions are detected"""
580 with self.assertRaises(ValueError) as e:
581 self._DoTestFile('14_pack_overlap.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600582 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700583 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
584 str(e.exception))
585
586 def testPackEntryOverflow(self):
587 """Test that entries that overflow their size are detected"""
588 with self.assertRaises(ValueError) as e:
589 self._DoTestFile('15_pack_overflow.dts')
590 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
591 "but entry size is 0x3 (3)", str(e.exception))
592
593 def testPackImageOverflow(self):
594 """Test that entries which overflow the image size are detected"""
595 with self.assertRaises(ValueError) as e:
596 self._DoTestFile('16_pack_image_overflow.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600597 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass4f443042016-11-25 20:15:52 -0700598 "size 0x3 (3)", str(e.exception))
599
600 def testPackImageSize(self):
601 """Test that the image size can be set"""
602 retcode = self._DoTestFile('17_pack_image_size.dts')
603 self.assertEqual(0, retcode)
604 self.assertIn('image', control.images)
605 image = control.images['image']
606 self.assertEqual(7, image._size)
607
608 def testPackImageSizeAlign(self):
609 """Test that image size alignemnt works as expected"""
610 retcode = self._DoTestFile('18_pack_image_align.dts')
611 self.assertEqual(0, retcode)
612 self.assertIn('image', control.images)
613 image = control.images['image']
614 self.assertEqual(16, image._size)
615
616 def testPackInvalidImageAlign(self):
617 """Test that invalid image alignment is detected"""
618 with self.assertRaises(ValueError) as e:
619 self._DoTestFile('19_pack_inv_image_align.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600620 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700621 "align-size 0x8 (8)", str(e.exception))
622
623 def testPackAlignPowerOf2(self):
624 """Test that invalid image alignment is detected"""
625 with self.assertRaises(ValueError) as e:
626 self._DoTestFile('20_pack_inv_image_align_power2.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600627 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass4f443042016-11-25 20:15:52 -0700628 "two", str(e.exception))
629
630 def testImagePadByte(self):
631 """Test that the image pad byte can be specified"""
Simon Glass19790632017-11-13 18:55:01 -0700632 with open(self.TestFile('bss_data')) as fd:
633 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass4f443042016-11-25 20:15:52 -0700634 data = self._DoReadFile('21_image_pad.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700635 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700636
637 def testImageName(self):
638 """Test that image files can be named"""
639 retcode = self._DoTestFile('22_image_name.dts')
640 self.assertEqual(0, retcode)
641 image = control.images['image1']
642 fname = tools.GetOutputFilename('test-name')
643 self.assertTrue(os.path.exists(fname))
644
645 image = control.images['image2']
646 fname = tools.GetOutputFilename('test-name.xx')
647 self.assertTrue(os.path.exists(fname))
648
649 def testBlobFilename(self):
650 """Test that generic blobs can be provided by filename"""
651 data = self._DoReadFile('23_blob.dts')
652 self.assertEqual(BLOB_DATA, data)
653
654 def testPackSorted(self):
655 """Test that entries can be sorted"""
656 data = self._DoReadFile('24_sorted.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700657 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
Simon Glass4f443042016-11-25 20:15:52 -0700658 U_BOOT_DATA, data)
659
Simon Glass3ab95982018-08-01 15:22:37 -0600660 def testPackZeroOffset(self):
661 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass4f443042016-11-25 20:15:52 -0700662 with self.assertRaises(ValueError) as e:
663 self._DoTestFile('25_pack_zero_size.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600664 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700665 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
666 str(e.exception))
667
668 def testPackUbootDtb(self):
669 """Test that a device tree can be added to U-Boot"""
670 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
671 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700672
673 def testPackX86RomNoSize(self):
674 """Test that the end-at-4gb property requires a size property"""
675 with self.assertRaises(ValueError) as e:
676 self._DoTestFile('27_pack_4gb_no_size.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600677 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glasse0ff8552016-11-25 20:15:53 -0700678 "using end-at-4gb", str(e.exception))
679
680 def testPackX86RomOutside(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600681 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glasse0ff8552016-11-25 20:15:53 -0700682 with self.assertRaises(ValueError) as e:
683 self._DoTestFile('28_pack_4gb_outside.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600684 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glass8f1da502018-06-01 09:38:12 -0600685 "the section starting at 0xffffffe0 (4294967264)",
Simon Glasse0ff8552016-11-25 20:15:53 -0700686 str(e.exception))
687
688 def testPackX86Rom(self):
689 """Test that a basic x86 ROM can be created"""
690 data = self._DoReadFile('29_x86-rom.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700691 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
692 chr(0) * 2, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700693
694 def testPackX86RomMeNoDesc(self):
695 """Test that an invalid Intel descriptor entry is detected"""
696 TestFunctional._MakeInputFile('descriptor.bin', '')
697 with self.assertRaises(ValueError) as e:
698 self._DoTestFile('31_x86-rom-me.dts')
699 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
700 "signature", str(e.exception))
701
702 def testPackX86RomBadDesc(self):
703 """Test that the Intel requires a descriptor entry"""
704 with self.assertRaises(ValueError) as e:
705 self._DoTestFile('30_x86-rom-me-no-desc.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600706 self.assertIn("Node '/binman/intel-me': No offset set with "
707 "offset-unset: should another entry provide this correct "
708 "offset?", str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700709
710 def testPackX86RomMe(self):
711 """Test that an x86 ROM with an ME region can be created"""
712 data = self._DoReadFile('31_x86-rom-me.dts')
713 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
714
715 def testPackVga(self):
716 """Test that an image with a VGA binary can be created"""
717 data = self._DoReadFile('32_intel-vga.dts')
718 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
719
720 def testPackStart16(self):
721 """Test that an image with an x86 start16 region can be created"""
722 data = self._DoReadFile('33_x86-start16.dts')
723 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
724
Simon Glass736bb0a2018-07-06 10:27:17 -0600725 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glassadc57012018-07-06 10:27:16 -0600726 """Handle running a test for insertion of microcode
727
728 Args:
729 dts_fname: Name of test .dts file
730 nodtb_data: Data that we expect in the first section
Simon Glass736bb0a2018-07-06 10:27:17 -0600731 ucode_second: True if the microsecond entry is second instead of
732 third
Simon Glassadc57012018-07-06 10:27:16 -0600733
734 Returns:
735 Tuple:
736 Contents of first region (U-Boot or SPL)
Simon Glass3ab95982018-08-01 15:22:37 -0600737 Offset and size components of microcode pointer, as inserted
Simon Glassadc57012018-07-06 10:27:16 -0600738 in the above (two 4-byte words)
739 """
Simon Glass6b187df2017-11-12 21:52:27 -0700740 data = self._DoReadFile(dts_fname, True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700741
742 # Now check the device tree has no microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600743 if ucode_second:
744 ucode_content = data[len(nodtb_data):]
745 ucode_pos = len(nodtb_data)
746 dtb_with_ucode = ucode_content[16:]
747 fdt_len = self.GetFdtLen(dtb_with_ucode)
748 else:
749 dtb_with_ucode = data[len(nodtb_data):]
750 fdt_len = self.GetFdtLen(dtb_with_ucode)
751 ucode_content = dtb_with_ucode[fdt_len:]
752 ucode_pos = len(nodtb_data) + fdt_len
Simon Glasse0ff8552016-11-25 20:15:53 -0700753 fname = tools.GetOutputFilename('test.dtb')
754 with open(fname, 'wb') as fd:
Simon Glassadc57012018-07-06 10:27:16 -0600755 fd.write(dtb_with_ucode)
Simon Glassec3f3782017-05-27 07:38:29 -0600756 dtb = fdt.FdtScan(fname)
757 ucode = dtb.GetNode('/microcode')
Simon Glasse0ff8552016-11-25 20:15:53 -0700758 self.assertTrue(ucode)
759 for node in ucode.subnodes:
760 self.assertFalse(node.props.get('data'))
761
Simon Glasse0ff8552016-11-25 20:15:53 -0700762 # Check that the microcode appears immediately after the Fdt
763 # This matches the concatenation of the data properties in
Simon Glass87722132017-11-12 21:52:26 -0700764 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glasse0ff8552016-11-25 20:15:53 -0700765 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
766 0x78235609)
Simon Glassadc57012018-07-06 10:27:16 -0600767 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glasse0ff8552016-11-25 20:15:53 -0700768
769 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600770 # expected offset and size
Simon Glasse0ff8552016-11-25 20:15:53 -0700771 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
772 len(ucode_data))
Simon Glass736bb0a2018-07-06 10:27:17 -0600773 u_boot = data[:len(nodtb_data)]
774 return u_boot, pos_and_size
Simon Glass6b187df2017-11-12 21:52:27 -0700775
776 def testPackUbootMicrocode(self):
777 """Test that x86 microcode can be handled correctly
778
779 We expect to see the following in the image, in order:
780 u-boot-nodtb.bin with a microcode pointer inserted at the correct
781 place
782 u-boot.dtb with the microcode removed
783 the microcode
784 """
785 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
786 U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700787 self.assertEqual('nodtb with microcode' + pos_and_size +
788 ' somewhere in here', first)
789
Simon Glass160a7662017-05-27 07:38:26 -0600790 def _RunPackUbootSingleMicrocode(self):
Simon Glasse0ff8552016-11-25 20:15:53 -0700791 """Test that x86 microcode can be handled correctly
792
793 We expect to see the following in the image, in order:
794 u-boot-nodtb.bin with a microcode pointer inserted at the correct
795 place
796 u-boot.dtb with the microcode
797 an empty microcode region
798 """
799 # We need the libfdt library to run this test since only that allows
800 # finding the offset of a property. This is required by
801 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glasse0ff8552016-11-25 20:15:53 -0700802 data = self._DoReadFile('35_x86_single_ucode.dts', True)
803
804 second = data[len(U_BOOT_NODTB_DATA):]
805
806 fdt_len = self.GetFdtLen(second)
807 third = second[fdt_len:]
808 second = second[:fdt_len]
809
Simon Glass160a7662017-05-27 07:38:26 -0600810 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
811 self.assertIn(ucode_data, second)
812 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700813
Simon Glass160a7662017-05-27 07:38:26 -0600814 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600815 # expected offset and size
Simon Glass160a7662017-05-27 07:38:26 -0600816 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
817 len(ucode_data))
818 first = data[:len(U_BOOT_NODTB_DATA)]
819 self.assertEqual('nodtb with microcode' + pos_and_size +
820 ' somewhere in here', first)
Simon Glassc49deb82016-11-25 20:15:54 -0700821
Simon Glass75db0862016-11-25 20:15:55 -0700822 def testPackUbootSingleMicrocode(self):
823 """Test that x86 microcode can be handled correctly with fdt_normal.
824 """
Simon Glass160a7662017-05-27 07:38:26 -0600825 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700826
Simon Glassc49deb82016-11-25 20:15:54 -0700827 def testUBootImg(self):
828 """Test that u-boot.img can be put in a file"""
829 data = self._DoReadFile('36_u_boot_img.dts')
830 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glass75db0862016-11-25 20:15:55 -0700831
832 def testNoMicrocode(self):
833 """Test that a missing microcode region is detected"""
834 with self.assertRaises(ValueError) as e:
835 self._DoReadFile('37_x86_no_ucode.dts', True)
836 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
837 "node found in ", str(e.exception))
838
839 def testMicrocodeWithoutNode(self):
840 """Test that a missing u-boot-dtb-with-ucode node is detected"""
841 with self.assertRaises(ValueError) as e:
842 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
843 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
844 "microcode region u-boot-dtb-with-ucode", str(e.exception))
845
846 def testMicrocodeWithoutNode2(self):
847 """Test that a missing u-boot-ucode node is detected"""
848 with self.assertRaises(ValueError) as e:
849 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
850 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
851 "microcode region u-boot-ucode", str(e.exception))
852
853 def testMicrocodeWithoutPtrInElf(self):
854 """Test that a U-Boot binary without the microcode symbol is detected"""
855 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glass75db0862016-11-25 20:15:55 -0700856 try:
857 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
858 TestFunctional._MakeInputFile('u-boot', fd.read())
859
860 with self.assertRaises(ValueError) as e:
Simon Glass160a7662017-05-27 07:38:26 -0600861 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700862 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
863 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
864
865 finally:
866 # Put the original file back
867 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
868 TestFunctional._MakeInputFile('u-boot', fd.read())
869
870 def testMicrocodeNotInImage(self):
871 """Test that microcode must be placed within the image"""
872 with self.assertRaises(ValueError) as e:
873 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
874 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
875 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glass25ac0e62018-06-01 09:38:14 -0600876 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glass75db0862016-11-25 20:15:55 -0700877
878 def testWithoutMicrocode(self):
879 """Test that we can cope with an image without microcode (e.g. qemu)"""
880 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
881 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glass16b8d6b2018-07-06 10:27:42 -0600882 data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -0700883
884 # Now check the device tree has no microcode
885 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
886 second = data[len(U_BOOT_NODTB_DATA):]
887
888 fdt_len = self.GetFdtLen(second)
889 self.assertEqual(dtb, second[:fdt_len])
890
891 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
892 third = data[used_len:]
893 self.assertEqual(chr(0) * (0x200 - used_len), third)
894
895 def testUnknownPosSize(self):
896 """Test that microcode must be placed within the image"""
897 with self.assertRaises(ValueError) as e:
898 self._DoReadFile('41_unknown_pos_size.dts', True)
Simon Glass3ab95982018-08-01 15:22:37 -0600899 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glass75db0862016-11-25 20:15:55 -0700900 "entry 'invalid-entry'", str(e.exception))
Simon Glassda229092016-11-25 20:15:56 -0700901
902 def testPackFsp(self):
903 """Test that an image with a FSP binary can be created"""
904 data = self._DoReadFile('42_intel-fsp.dts')
905 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
906
907 def testPackCmc(self):
Bin Meng59ea8c22017-08-15 22:41:54 -0700908 """Test that an image with a CMC binary can be created"""
Simon Glassda229092016-11-25 20:15:56 -0700909 data = self._DoReadFile('43_intel-cmc.dts')
910 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Meng59ea8c22017-08-15 22:41:54 -0700911
912 def testPackVbt(self):
913 """Test that an image with a VBT binary can be created"""
914 data = self._DoReadFile('46_intel-vbt.dts')
915 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glass9fc60b42017-11-12 21:52:22 -0700916
Simon Glass56509842017-11-12 21:52:25 -0700917 def testSplBssPad(self):
918 """Test that we can pad SPL's BSS with zeros"""
Simon Glass6b187df2017-11-12 21:52:27 -0700919 # ELF file with a '__bss_size' symbol
920 with open(self.TestFile('bss_data')) as fd:
921 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass56509842017-11-12 21:52:25 -0700922 data = self._DoReadFile('47_spl_bss_pad.dts')
923 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
924
Simon Glassb50e5612017-11-13 18:54:54 -0700925 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
926 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
927 with self.assertRaises(ValueError) as e:
928 data = self._DoReadFile('47_spl_bss_pad.dts')
929 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
930 str(e.exception))
931
Simon Glass87722132017-11-12 21:52:26 -0700932 def testPackStart16Spl(self):
933 """Test that an image with an x86 start16 region can be created"""
934 data = self._DoReadFile('48_x86-start16-spl.dts')
935 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
936
Simon Glass736bb0a2018-07-06 10:27:17 -0600937 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
938 """Helper function for microcode tests
Simon Glass6b187df2017-11-12 21:52:27 -0700939
940 We expect to see the following in the image, in order:
941 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
942 correct place
943 u-boot.dtb with the microcode removed
944 the microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600945
946 Args:
947 dts: Device tree file to use for test
948 ucode_second: True if the microsecond entry is second instead of
949 third
Simon Glass6b187df2017-11-12 21:52:27 -0700950 """
951 # ELF file with a '_dt_ucode_base_size' symbol
952 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
953 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass736bb0a2018-07-06 10:27:17 -0600954 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
955 ucode_second=ucode_second)
Simon Glass6b187df2017-11-12 21:52:27 -0700956 self.assertEqual('splnodtb with microc' + pos_and_size +
957 'ter somewhere in here', first)
958
Simon Glass736bb0a2018-07-06 10:27:17 -0600959 def testPackUbootSplMicrocode(self):
960 """Test that x86 microcode can be handled correctly in SPL"""
961 self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
962
963 def testPackUbootSplMicrocodeReorder(self):
964 """Test that order doesn't matter for microcode entries
965
966 This is the same as testPackUbootSplMicrocode but when we process the
967 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
968 entry, so we reply on binman to try later.
969 """
970 self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
971 ucode_second=True)
972
Simon Glassca4f4ff2017-11-12 21:52:28 -0700973 def testPackMrc(self):
974 """Test that an image with an MRC binary can be created"""
975 data = self._DoReadFile('50_intel_mrc.dts')
976 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
977
Simon Glass47419ea2017-11-13 18:54:55 -0700978 def testSplDtb(self):
979 """Test that an image with spl/u-boot-spl.dtb can be created"""
980 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
981 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
982
Simon Glass4e6fdbe2017-11-13 18:54:56 -0700983 def testSplNoDtb(self):
984 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
985 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
986 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
987
Simon Glass19790632017-11-13 18:55:01 -0700988 def testSymbols(self):
989 """Test binman can assign symbols embedded in U-Boot"""
990 elf_fname = self.TestFile('u_boot_binman_syms')
991 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
992 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glass3ab95982018-08-01 15:22:37 -0600993 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass19790632017-11-13 18:55:01 -0700994
995 with open(self.TestFile('u_boot_binman_syms')) as fd:
996 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
997 data = self._DoReadFile('53_symbols.dts')
998 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
999 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
1000 U_BOOT_DATA +
1001 sym_values + U_BOOT_SPL_DATA[16:])
1002 self.assertEqual(expected, data)
1003
Simon Glassdd57c132018-06-01 09:38:11 -06001004 def testPackUnitAddress(self):
1005 """Test that we support multiple binaries with the same name"""
1006 data = self._DoReadFile('54_unit_address.dts')
1007 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1008
Simon Glass18546952018-06-01 09:38:16 -06001009 def testSections(self):
1010 """Basic test of sections"""
1011 data = self._DoReadFile('55_sections.dts')
Simon Glass8122f392018-07-17 13:25:28 -06001012 expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
1013 U_BOOT_DATA + '&' * 4)
Simon Glass18546952018-06-01 09:38:16 -06001014 self.assertEqual(expected, data)
Simon Glass9fc60b42017-11-12 21:52:22 -07001015
Simon Glass3b0c3822018-06-01 09:38:20 -06001016 def testMap(self):
1017 """Tests outputting a map of the images"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001018 _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
Simon Glass3ab95982018-08-01 15:22:37 -06001019 self.assertEqual(''' Offset Size Name
Simon Glass8122f392018-07-17 13:25:28 -0600102000000000 00000028 main-section
1021 00000000 00000010 section@0
1022 00000000 00000004 u-boot
1023 00000010 00000010 section@1
1024 00000000 00000004 u-boot
1025 00000020 00000004 section@2
1026 00000000 00000004 u-boot
Simon Glass3b0c3822018-06-01 09:38:20 -06001027''', map_data)
1028
Simon Glassc8d48ef2018-06-01 09:38:21 -06001029 def testNamePrefix(self):
1030 """Tests that name prefixes are used"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001031 _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
Simon Glass3ab95982018-08-01 15:22:37 -06001032 self.assertEqual(''' Offset Size Name
Simon Glass8122f392018-07-17 13:25:28 -0600103300000000 00000028 main-section
1034 00000000 00000010 section@0
1035 00000000 00000004 ro-u-boot
1036 00000010 00000010 section@1
1037 00000000 00000004 rw-u-boot
Simon Glassc8d48ef2018-06-01 09:38:21 -06001038''', map_data)
1039
Simon Glass736bb0a2018-07-06 10:27:17 -06001040 def testUnknownContents(self):
1041 """Test that obtaining the contents works as expected"""
1042 with self.assertRaises(ValueError) as e:
1043 self._DoReadFile('57_unknown_contents.dts', True)
1044 self.assertIn("Section '/binman': Internal error: Could not complete "
1045 "processing of contents: remaining [<_testing.Entry__testing ",
1046 str(e.exception))
1047
Simon Glass5c890232018-07-06 10:27:19 -06001048 def testBadChangeSize(self):
1049 """Test that trying to change the size of an entry fails"""
1050 with self.assertRaises(ValueError) as e:
1051 self._DoReadFile('59_change_size.dts', True)
1052 self.assertIn("Node '/binman/_testing': Cannot update entry size from "
1053 '2 to 1', str(e.exception))
1054
Simon Glass16b8d6b2018-07-06 10:27:42 -06001055 def testUpdateFdt(self):
Simon Glass3ab95982018-08-01 15:22:37 -06001056 """Test that we can update the device tree with offset/size info"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001057 _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
1058 update_dtb=True)
Simon Glassdbf6be92018-08-01 15:22:42 -06001059 props = self._GetPropTree(out_dtb_fname, ['offset', 'size',
1060 'image-pos'])
Simon Glass16b8d6b2018-07-06 10:27:42 -06001061 with open('/tmp/x.dtb', 'wb') as outf:
1062 with open(out_dtb_fname) as inf:
1063 outf.write(inf.read())
1064 self.assertEqual({
Simon Glassdbf6be92018-08-01 15:22:42 -06001065 'image-pos': 0,
Simon Glass8122f392018-07-17 13:25:28 -06001066 'offset': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001067 '_testing:offset': 32,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001068 '_testing:size': 1,
Simon Glassdbf6be92018-08-01 15:22:42 -06001069 '_testing:image-pos': 32,
Simon Glass3ab95982018-08-01 15:22:37 -06001070 'section@0/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001071 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001072 'section@0/u-boot:image-pos': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001073 'section@0:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001074 'section@0:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001075 'section@0:image-pos': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001076
Simon Glass3ab95982018-08-01 15:22:37 -06001077 'section@1/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001078 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001079 'section@1/u-boot:image-pos': 16,
Simon Glass3ab95982018-08-01 15:22:37 -06001080 'section@1:offset': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001081 'section@1:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001082 'section@1:image-pos': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001083 'size': 40
1084 }, props)
1085
1086 def testUpdateFdtBad(self):
1087 """Test that we detect when ProcessFdt never completes"""
1088 with self.assertRaises(ValueError) as e:
1089 self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
1090 self.assertIn('Could not complete processing of Fdt: remaining '
1091 '[<_testing.Entry__testing', str(e.exception))
Simon Glass5c890232018-07-06 10:27:19 -06001092
Simon Glass53af22a2018-07-17 13:25:32 -06001093 def testEntryArgs(self):
1094 """Test passing arguments to entries from the command line"""
1095 entry_args = {
1096 'test-str-arg': 'test1',
1097 'test-int-arg': '456',
1098 }
1099 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1100 self.assertIn('image', control.images)
1101 entry = control.images['image'].GetEntries()['_testing']
1102 self.assertEqual('test0', entry.test_str_fdt)
1103 self.assertEqual('test1', entry.test_str_arg)
1104 self.assertEqual(123, entry.test_int_fdt)
1105 self.assertEqual(456, entry.test_int_arg)
1106
1107 def testEntryArgsMissing(self):
1108 """Test missing arguments and properties"""
1109 entry_args = {
1110 'test-int-arg': '456',
1111 }
1112 self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args)
1113 entry = control.images['image'].GetEntries()['_testing']
1114 self.assertEqual('test0', entry.test_str_fdt)
1115 self.assertEqual(None, entry.test_str_arg)
1116 self.assertEqual(None, entry.test_int_fdt)
1117 self.assertEqual(456, entry.test_int_arg)
1118
1119 def testEntryArgsRequired(self):
1120 """Test missing arguments and properties"""
1121 entry_args = {
1122 'test-int-arg': '456',
1123 }
1124 with self.assertRaises(ValueError) as e:
1125 self._DoReadFileDtb('64_entry_args_required.dts')
1126 self.assertIn("Node '/binman/_testing': Missing required "
1127 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1128 str(e.exception))
1129
1130 def testEntryArgsInvalidFormat(self):
1131 """Test that an invalid entry-argument format is detected"""
1132 args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value']
1133 with self.assertRaises(ValueError) as e:
1134 self._DoBinman(*args)
1135 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1136
1137 def testEntryArgsInvalidInteger(self):
1138 """Test that an invalid entry-argument integer is detected"""
1139 entry_args = {
1140 'test-int-arg': 'abc',
1141 }
1142 with self.assertRaises(ValueError) as e:
1143 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1144 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1145 "'test-int-arg' (value 'abc') to integer",
1146 str(e.exception))
1147
1148 def testEntryArgsInvalidDatatype(self):
1149 """Test that an invalid entry-argument datatype is detected
1150
1151 This test could be written in entry_test.py except that it needs
1152 access to control.entry_args, which seems more than that module should
1153 be able to see.
1154 """
1155 entry_args = {
1156 'test-bad-datatype-arg': '12',
1157 }
1158 with self.assertRaises(ValueError) as e:
1159 self._DoReadFileDtb('65_entry_args_unknown_datatype.dts',
1160 entry_args=entry_args)
1161 self.assertIn('GetArg() internal error: Unknown data type ',
1162 str(e.exception))
1163
1164
Simon Glass9fc60b42017-11-12 21:52:22 -07001165if __name__ == "__main__":
1166 unittest.main()