blob: 1efedc2ec30e3fb7c27e6d4af366fa38117edb6b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass57454f42016-11-25 20:15:52 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass57454f42016-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 Glass4ca8e042017-11-13 18:55:01 -070021import elf
Simon Glassa9440932017-05-27 07:38:30 -060022import fdt
Simon Glass57454f42016-11-25 20:15:52 -070023import fdt_util
24import tools
25import tout
26
27# Contents of test files, corresponding to different entry types
Simon Glass3d274232017-11-12 21:52:27 -070028U_BOOT_DATA = '1234'
29U_BOOT_IMG_DATA = 'img'
Simon Glassd6051522017-11-13 18:54:59 -070030U_BOOT_SPL_DATA = '56780123456789abcde'
Simon Glass3d274232017-11-12 21:52:27 -070031BLOB_DATA = '89'
32ME_DATA = '0abcd'
33VGA_DATA = 'vga'
34U_BOOT_DTB_DATA = 'udtb'
Simon Glass9aa6a6f2017-11-13 18:54:55 -070035U_BOOT_SPL_DTB_DATA = 'spldtb'
Simon Glass3d274232017-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 Glassa409c932017-11-12 21:52:28 -070043MRC_DATA = 'mrc'
Simon Glass57454f42016-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 Glassb3393262017-11-12 21:52:20 -070061 global entry
62 import entry
63
Simon Glass57454f42016-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 Glass72232452016-11-25 20:15:53 -070076 TestFunctional._MakeInputFile('me.bin', ME_DATA)
77 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glass57454f42016-11-25 20:15:52 -070078 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
Simon Glass9aa6a6f2017-11-13 18:54:55 -070079 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
Simon Glass72232452016-11-25 20:15:53 -070080 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Simon Glasse83679d2017-11-12 21:52:26 -070081 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
82 X86_START16_SPL_DATA)
Simon Glass57454f42016-11-25 20:15:52 -070083 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass3d274232017-11-12 21:52:27 -070084 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
85 U_BOOT_SPL_NODTB_DATA)
Simon Glassb4176d42016-11-25 20:15:56 -070086 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
87 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Mengd7bcdf52017-08-15 22:41:54 -070088 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassa409c932017-11-12 21:52:28 -070089 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glass57454f42016-11-25 20:15:52 -070090 self._output_setup = False
91
Simon Glass72232452016-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 Glass57454f42016-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 Glass075a45c2017-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 Glass57454f42016-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 Glassa87014e2018-07-06 10:27:42 -0600149 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False):
Simon Glass57454f42016-11-25 20:15:52 -0700150 """Run binman with a given test file
151
152 Args:
Simon Glass1e324002018-06-01 09:38:19 -0600153 fname: Device-tree source filename to use (e.g. 05_simple.dts)
154 debug: True to enable debugging output
Simon Glass30732662018-06-01 09:38:20 -0600155 map: True to output map files for the images
Simon Glasse8561af2018-08-01 15:22:37 -0600156 update_dtb: Update the offset and size of each entry in the device
Simon Glassa87014e2018-07-06 10:27:42 -0600157 tree before packing it into the image
Simon Glass57454f42016-11-25 20:15:52 -0700158 """
Simon Glass075a45c2017-11-13 18:55:00 -0700159 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
160 if debug:
161 args.append('-D')
Simon Glass30732662018-06-01 09:38:20 -0600162 if map:
163 args.append('-m')
Simon Glassa87014e2018-07-06 10:27:42 -0600164 if update_dtb:
165 args.append('-up')
Simon Glass075a45c2017-11-13 18:55:00 -0700166 return self._DoBinman(*args)
Simon Glass57454f42016-11-25 20:15:52 -0700167
168 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glass72232452016-11-25 20:15:53 -0700169 """Set up a new test device-tree file
170
171 The given file is compiled and set up as the device tree to be used
172 for ths test.
173
174 Args:
175 fname: Filename of .dts file to read
Simon Glass1e324002018-06-01 09:38:19 -0600176 outfile: Output filename for compiled device-tree binary
Simon Glass72232452016-11-25 20:15:53 -0700177
178 Returns:
Simon Glass1e324002018-06-01 09:38:19 -0600179 Contents of device-tree binary
Simon Glass72232452016-11-25 20:15:53 -0700180 """
Simon Glass57454f42016-11-25 20:15:52 -0700181 if not self._output_setup:
182 tools.PrepareOutputDir(self._indir, True)
183 self._output_setup = True
184 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
185 with open(dtb) as fd:
186 data = fd.read()
187 TestFunctional._MakeInputFile(outfile, data)
Simon Glass72232452016-11-25 20:15:53 -0700188 return data
Simon Glass57454f42016-11-25 20:15:52 -0700189
Simon Glassa87014e2018-07-06 10:27:42 -0600190 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
191 update_dtb=False):
Simon Glass57454f42016-11-25 20:15:52 -0700192 """Run binman and return the resulting image
193
194 This runs binman with a given test file and then reads the resulting
195 output file. It is a shortcut function since most tests need to do
196 these steps.
197
198 Raises an assertion failure if binman returns a non-zero exit code.
199
200 Args:
Simon Glass1e324002018-06-01 09:38:19 -0600201 fname: Device-tree source filename to use (e.g. 05_simple.dts)
Simon Glass57454f42016-11-25 20:15:52 -0700202 use_real_dtb: True to use the test file as the contents of
203 the u-boot-dtb entry. Normally this is not needed and the
204 test contents (the U_BOOT_DTB_DATA string) can be used.
205 But in some test we need the real contents.
Simon Glass30732662018-06-01 09:38:20 -0600206 map: True to output map files for the images
Simon Glasse8561af2018-08-01 15:22:37 -0600207 update_dtb: Update the offset and size of each entry in the device
Simon Glassa87014e2018-07-06 10:27:42 -0600208 tree before packing it into the image
Simon Glass72232452016-11-25 20:15:53 -0700209
210 Returns:
211 Tuple:
212 Resulting image contents
213 Device tree contents
Simon Glass30732662018-06-01 09:38:20 -0600214 Map data showing contents of image (or None if none)
Simon Glass57454f42016-11-25 20:15:52 -0700215 """
Simon Glass72232452016-11-25 20:15:53 -0700216 dtb_data = None
Simon Glass57454f42016-11-25 20:15:52 -0700217 # Use the compiled test file as the u-boot-dtb input
218 if use_real_dtb:
Simon Glass72232452016-11-25 20:15:53 -0700219 dtb_data = self._SetupDtb(fname)
Simon Glass57454f42016-11-25 20:15:52 -0700220
221 try:
Simon Glassa87014e2018-07-06 10:27:42 -0600222 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb)
Simon Glass57454f42016-11-25 20:15:52 -0700223 self.assertEqual(0, retcode)
Simon Glassa87014e2018-07-06 10:27:42 -0600224 out_dtb_fname = control.GetFdtPath('u-boot.dtb')
Simon Glass57454f42016-11-25 20:15:52 -0700225
226 # Find the (only) image, read it and return its contents
227 image = control.images['image']
Simon Glassa87014e2018-07-06 10:27:42 -0600228 image_fname = tools.GetOutputFilename('image.bin')
229 self.assertTrue(os.path.exists(image_fname))
Simon Glass30732662018-06-01 09:38:20 -0600230 if map:
231 map_fname = tools.GetOutputFilename('image.map')
232 with open(map_fname) as fd:
233 map_data = fd.read()
234 else:
235 map_data = None
Simon Glassa87014e2018-07-06 10:27:42 -0600236 with open(image_fname) as fd:
237 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass57454f42016-11-25 20:15:52 -0700238 finally:
239 # Put the test file back
240 if use_real_dtb:
241 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
242
Simon Glass72232452016-11-25 20:15:53 -0700243 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass1e324002018-06-01 09:38:19 -0600244 """Helper function which discards the device-tree binary
245
246 Args:
247 fname: Device-tree source filename to use (e.g. 05_simple.dts)
248 use_real_dtb: True to use the test file as the contents of
249 the u-boot-dtb entry. Normally this is not needed and the
250 test contents (the U_BOOT_DTB_DATA string) can be used.
251 But in some test we need the real contents.
252 """
Simon Glass72232452016-11-25 20:15:53 -0700253 return self._DoReadFileDtb(fname, use_real_dtb)[0]
254
Simon Glass57454f42016-11-25 20:15:52 -0700255 @classmethod
256 def _MakeInputFile(self, fname, contents):
257 """Create a new test input file, creating directories as needed
258
259 Args:
Simon Glasse8561af2018-08-01 15:22:37 -0600260 fname: Filename to create
Simon Glass57454f42016-11-25 20:15:52 -0700261 contents: File contents to write in to the file
262 Returns:
263 Full pathname of file created
264 """
265 pathname = os.path.join(self._indir, fname)
266 dirname = os.path.dirname(pathname)
267 if dirname and not os.path.exists(dirname):
268 os.makedirs(dirname)
269 with open(pathname, 'wb') as fd:
270 fd.write(contents)
271 return pathname
272
273 @classmethod
274 def TestFile(self, fname):
275 return os.path.join(self._binman_dir, 'test', fname)
276
277 def AssertInList(self, grep_list, target):
278 """Assert that at least one of a list of things is in a target
279
280 Args:
281 grep_list: List of strings to check
282 target: Target string
283 """
284 for grep in grep_list:
285 if grep in target:
286 return
287 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
288
289 def CheckNoGaps(self, entries):
290 """Check that all entries fit together without gaps
291
292 Args:
293 entries: List of entries to check
294 """
Simon Glasse8561af2018-08-01 15:22:37 -0600295 offset = 0
Simon Glass57454f42016-11-25 20:15:52 -0700296 for entry in entries.values():
Simon Glasse8561af2018-08-01 15:22:37 -0600297 self.assertEqual(offset, entry.offset)
298 offset += entry.size
Simon Glass57454f42016-11-25 20:15:52 -0700299
Simon Glass72232452016-11-25 20:15:53 -0700300 def GetFdtLen(self, dtb):
Simon Glass1e324002018-06-01 09:38:19 -0600301 """Get the totalsize field from a device-tree binary
Simon Glass72232452016-11-25 20:15:53 -0700302
303 Args:
Simon Glass1e324002018-06-01 09:38:19 -0600304 dtb: Device-tree binary contents
Simon Glass72232452016-11-25 20:15:53 -0700305
306 Returns:
Simon Glass1e324002018-06-01 09:38:19 -0600307 Total size of device-tree binary, from the header
Simon Glass72232452016-11-25 20:15:53 -0700308 """
309 return struct.unpack('>L', dtb[4:8])[0]
310
Simon Glassa87014e2018-07-06 10:27:42 -0600311 def _GetPropTree(self, dtb_data, node_names):
312 def AddNode(node, path):
313 if node.name != '/':
314 path += '/' + node.name
315 #print 'path', path
316 for subnode in node.subnodes:
317 for prop in subnode.props.values():
318 if prop.name in node_names:
319 prop_path = path + '/' + subnode.name + ':' + prop.name
320 tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
321 prop.value)
Simon Glassa87014e2018-07-06 10:27:42 -0600322 AddNode(subnode, path)
323
324 tree = {}
325 dtb = fdt.Fdt(dtb_data)
326 dtb.Scan()
327 AddNode(dtb.GetRoot(), '')
328 return tree
329
Simon Glass57454f42016-11-25 20:15:52 -0700330 def testRun(self):
331 """Test a basic run with valid args"""
332 result = self._RunBinman('-h')
333
334 def testFullHelp(self):
335 """Test that the full help is displayed with -H"""
336 result = self._RunBinman('-H')
337 help_file = os.path.join(self._binman_dir, 'README')
Tom Rinic3c0b6d2018-01-16 15:29:50 -0500338 # Remove possible extraneous strings
339 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
340 gothelp = result.stdout.replace(extra, '')
341 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass57454f42016-11-25 20:15:52 -0700342 self.assertEqual(0, len(result.stderr))
343 self.assertEqual(0, result.return_code)
344
345 def testFullHelpInternal(self):
346 """Test that the full help is displayed with -H"""
347 try:
348 command.test_result = command.CommandResult()
349 result = self._DoBinman('-H')
350 help_file = os.path.join(self._binman_dir, 'README')
351 finally:
352 command.test_result = None
353
354 def testHelp(self):
355 """Test that the basic help is displayed with -h"""
356 result = self._RunBinman('-h')
357 self.assertTrue(len(result.stdout) > 200)
358 self.assertEqual(0, len(result.stderr))
359 self.assertEqual(0, result.return_code)
360
Simon Glass57454f42016-11-25 20:15:52 -0700361 def testBoard(self):
362 """Test that we can run it with a specific board"""
363 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
364 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
365 result = self._DoBinman('-b', 'sandbox')
366 self.assertEqual(0, result)
367
368 def testNeedBoard(self):
369 """Test that we get an error when no board ius supplied"""
370 with self.assertRaises(ValueError) as e:
371 result = self._DoBinman()
372 self.assertIn("Must provide a board to process (use -b <board>)",
373 str(e.exception))
374
375 def testMissingDt(self):
Simon Glass1e324002018-06-01 09:38:19 -0600376 """Test that an invalid device-tree file generates an error"""
Simon Glass57454f42016-11-25 20:15:52 -0700377 with self.assertRaises(Exception) as e:
378 self._RunBinman('-d', 'missing_file')
379 # We get one error from libfdt, and a different one from fdtget.
380 self.AssertInList(["Couldn't open blob from 'missing_file'",
381 'No such file or directory'], str(e.exception))
382
383 def testBrokenDt(self):
Simon Glass1e324002018-06-01 09:38:19 -0600384 """Test that an invalid device-tree source file generates an error
Simon Glass57454f42016-11-25 20:15:52 -0700385
386 Since this is a source file it should be compiled and the error
387 will come from the device-tree compiler (dtc).
388 """
389 with self.assertRaises(Exception) as e:
390 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
391 self.assertIn("FATAL ERROR: Unable to parse input tree",
392 str(e.exception))
393
394 def testMissingNode(self):
395 """Test that a device tree without a 'binman' node generates an error"""
396 with self.assertRaises(Exception) as e:
397 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
398 self.assertIn("does not have a 'binman' node", str(e.exception))
399
400 def testEmpty(self):
401 """Test that an empty binman node works OK (i.e. does nothing)"""
402 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
403 self.assertEqual(0, len(result.stderr))
404 self.assertEqual(0, result.return_code)
405
406 def testInvalidEntry(self):
407 """Test that an invalid entry is flagged"""
408 with self.assertRaises(Exception) as e:
409 result = self._RunBinman('-d',
410 self.TestFile('04_invalid_entry.dts'))
411 #print e.exception
412 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
413 "'/binman/not-a-valid-type'", str(e.exception))
414
415 def testSimple(self):
416 """Test a simple binman with a single file"""
417 data = self._DoReadFile('05_simple.dts')
418 self.assertEqual(U_BOOT_DATA, data)
419
Simon Glass075a45c2017-11-13 18:55:00 -0700420 def testSimpleDebug(self):
421 """Test a simple binman run with debugging enabled"""
422 data = self._DoTestFile('05_simple.dts', debug=True)
423
Simon Glass57454f42016-11-25 20:15:52 -0700424 def testDual(self):
425 """Test that we can handle creating two images
426
427 This also tests image padding.
428 """
429 retcode = self._DoTestFile('06_dual_image.dts')
430 self.assertEqual(0, retcode)
431
432 image = control.images['image1']
433 self.assertEqual(len(U_BOOT_DATA), image._size)
434 fname = tools.GetOutputFilename('image1.bin')
435 self.assertTrue(os.path.exists(fname))
436 with open(fname) as fd:
437 data = fd.read()
438 self.assertEqual(U_BOOT_DATA, data)
439
440 image = control.images['image2']
441 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
442 fname = tools.GetOutputFilename('image2.bin')
443 self.assertTrue(os.path.exists(fname))
444 with open(fname) as fd:
445 data = fd.read()
446 self.assertEqual(U_BOOT_DATA, data[3:7])
447 self.assertEqual(chr(0) * 3, data[:3])
448 self.assertEqual(chr(0) * 5, data[7:])
449
450 def testBadAlign(self):
451 """Test that an invalid alignment value is detected"""
452 with self.assertRaises(ValueError) as e:
453 self._DoTestFile('07_bad_align.dts')
454 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
455 "of two", str(e.exception))
456
457 def testPackSimple(self):
458 """Test that packing works as expected"""
459 retcode = self._DoTestFile('08_pack.dts')
460 self.assertEqual(0, retcode)
461 self.assertIn('image', control.images)
462 image = control.images['image']
Simon Glasseca32212018-06-01 09:38:12 -0600463 entries = image.GetEntries()
Simon Glass57454f42016-11-25 20:15:52 -0700464 self.assertEqual(5, len(entries))
465
466 # First u-boot
467 self.assertIn('u-boot', entries)
468 entry = entries['u-boot']
Simon Glasse8561af2018-08-01 15:22:37 -0600469 self.assertEqual(0, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700470 self.assertEqual(len(U_BOOT_DATA), entry.size)
471
472 # Second u-boot, aligned to 16-byte boundary
473 self.assertIn('u-boot-align', entries)
474 entry = entries['u-boot-align']
Simon Glasse8561af2018-08-01 15:22:37 -0600475 self.assertEqual(16, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700476 self.assertEqual(len(U_BOOT_DATA), entry.size)
477
478 # Third u-boot, size 23 bytes
479 self.assertIn('u-boot-size', entries)
480 entry = entries['u-boot-size']
Simon Glasse8561af2018-08-01 15:22:37 -0600481 self.assertEqual(20, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700482 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
483 self.assertEqual(23, entry.size)
484
485 # Fourth u-boot, placed immediate after the above
486 self.assertIn('u-boot-next', entries)
487 entry = entries['u-boot-next']
Simon Glasse8561af2018-08-01 15:22:37 -0600488 self.assertEqual(43, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700489 self.assertEqual(len(U_BOOT_DATA), entry.size)
490
Simon Glasse8561af2018-08-01 15:22:37 -0600491 # Fifth u-boot, placed at a fixed offset
Simon Glass57454f42016-11-25 20:15:52 -0700492 self.assertIn('u-boot-fixed', entries)
493 entry = entries['u-boot-fixed']
Simon Glasse8561af2018-08-01 15:22:37 -0600494 self.assertEqual(61, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700495 self.assertEqual(len(U_BOOT_DATA), entry.size)
496
497 self.assertEqual(65, image._size)
498
499 def testPackExtra(self):
500 """Test that extra packing feature works as expected"""
501 retcode = self._DoTestFile('09_pack_extra.dts')
502
503 self.assertEqual(0, retcode)
504 self.assertIn('image', control.images)
505 image = control.images['image']
Simon Glasseca32212018-06-01 09:38:12 -0600506 entries = image.GetEntries()
Simon Glass57454f42016-11-25 20:15:52 -0700507 self.assertEqual(5, len(entries))
508
509 # First u-boot with padding before and after
510 self.assertIn('u-boot', entries)
511 entry = entries['u-boot']
Simon Glasse8561af2018-08-01 15:22:37 -0600512 self.assertEqual(0, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700513 self.assertEqual(3, entry.pad_before)
514 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
515
516 # Second u-boot has an aligned size, but it has no effect
517 self.assertIn('u-boot-align-size-nop', entries)
518 entry = entries['u-boot-align-size-nop']
Simon Glasse8561af2018-08-01 15:22:37 -0600519 self.assertEqual(12, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700520 self.assertEqual(4, entry.size)
521
522 # Third u-boot has an aligned size too
523 self.assertIn('u-boot-align-size', entries)
524 entry = entries['u-boot-align-size']
Simon Glasse8561af2018-08-01 15:22:37 -0600525 self.assertEqual(16, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700526 self.assertEqual(32, entry.size)
527
528 # Fourth u-boot has an aligned end
529 self.assertIn('u-boot-align-end', entries)
530 entry = entries['u-boot-align-end']
Simon Glasse8561af2018-08-01 15:22:37 -0600531 self.assertEqual(48, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700532 self.assertEqual(16, entry.size)
533
534 # Fifth u-boot immediately afterwards
535 self.assertIn('u-boot-align-both', entries)
536 entry = entries['u-boot-align-both']
Simon Glasse8561af2018-08-01 15:22:37 -0600537 self.assertEqual(64, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700538 self.assertEqual(64, entry.size)
539
540 self.CheckNoGaps(entries)
541 self.assertEqual(128, image._size)
542
543 def testPackAlignPowerOf2(self):
544 """Test that invalid entry alignment is detected"""
545 with self.assertRaises(ValueError) as e:
546 self._DoTestFile('10_pack_align_power2.dts')
547 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
548 "of two", str(e.exception))
549
550 def testPackAlignSizePowerOf2(self):
551 """Test that invalid entry size alignment is detected"""
552 with self.assertRaises(ValueError) as e:
553 self._DoTestFile('11_pack_align_size_power2.dts')
554 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
555 "power of two", str(e.exception))
556
557 def testPackInvalidAlign(self):
Simon Glasse8561af2018-08-01 15:22:37 -0600558 """Test detection of an offset that does not match its alignment"""
Simon Glass57454f42016-11-25 20:15:52 -0700559 with self.assertRaises(ValueError) as e:
560 self._DoTestFile('12_pack_inv_align.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600561 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass57454f42016-11-25 20:15:52 -0700562 "align 0x4 (4)", str(e.exception))
563
564 def testPackInvalidSizeAlign(self):
565 """Test that invalid entry size alignment is detected"""
566 with self.assertRaises(ValueError) as e:
567 self._DoTestFile('13_pack_inv_size_align.dts')
568 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
569 "align-size 0x4 (4)", str(e.exception))
570
571 def testPackOverlap(self):
572 """Test that overlapping regions are detected"""
573 with self.assertRaises(ValueError) as e:
574 self._DoTestFile('14_pack_overlap.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600575 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass57454f42016-11-25 20:15:52 -0700576 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
577 str(e.exception))
578
579 def testPackEntryOverflow(self):
580 """Test that entries that overflow their size are detected"""
581 with self.assertRaises(ValueError) as e:
582 self._DoTestFile('15_pack_overflow.dts')
583 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
584 "but entry size is 0x3 (3)", str(e.exception))
585
586 def testPackImageOverflow(self):
587 """Test that entries which overflow the image size are detected"""
588 with self.assertRaises(ValueError) as e:
589 self._DoTestFile('16_pack_image_overflow.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600590 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass57454f42016-11-25 20:15:52 -0700591 "size 0x3 (3)", str(e.exception))
592
593 def testPackImageSize(self):
594 """Test that the image size can be set"""
595 retcode = self._DoTestFile('17_pack_image_size.dts')
596 self.assertEqual(0, retcode)
597 self.assertIn('image', control.images)
598 image = control.images['image']
599 self.assertEqual(7, image._size)
600
601 def testPackImageSizeAlign(self):
602 """Test that image size alignemnt works as expected"""
603 retcode = self._DoTestFile('18_pack_image_align.dts')
604 self.assertEqual(0, retcode)
605 self.assertIn('image', control.images)
606 image = control.images['image']
607 self.assertEqual(16, image._size)
608
609 def testPackInvalidImageAlign(self):
610 """Test that invalid image alignment is detected"""
611 with self.assertRaises(ValueError) as e:
612 self._DoTestFile('19_pack_inv_image_align.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600613 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass57454f42016-11-25 20:15:52 -0700614 "align-size 0x8 (8)", str(e.exception))
615
616 def testPackAlignPowerOf2(self):
617 """Test that invalid image alignment is detected"""
618 with self.assertRaises(ValueError) as e:
619 self._DoTestFile('20_pack_inv_image_align_power2.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600620 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass57454f42016-11-25 20:15:52 -0700621 "two", str(e.exception))
622
623 def testImagePadByte(self):
624 """Test that the image pad byte can be specified"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700625 with open(self.TestFile('bss_data')) as fd:
626 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass57454f42016-11-25 20:15:52 -0700627 data = self._DoReadFile('21_image_pad.dts')
Simon Glassd6051522017-11-13 18:54:59 -0700628 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
Simon Glass57454f42016-11-25 20:15:52 -0700629
630 def testImageName(self):
631 """Test that image files can be named"""
632 retcode = self._DoTestFile('22_image_name.dts')
633 self.assertEqual(0, retcode)
634 image = control.images['image1']
635 fname = tools.GetOutputFilename('test-name')
636 self.assertTrue(os.path.exists(fname))
637
638 image = control.images['image2']
639 fname = tools.GetOutputFilename('test-name.xx')
640 self.assertTrue(os.path.exists(fname))
641
642 def testBlobFilename(self):
643 """Test that generic blobs can be provided by filename"""
644 data = self._DoReadFile('23_blob.dts')
645 self.assertEqual(BLOB_DATA, data)
646
647 def testPackSorted(self):
648 """Test that entries can be sorted"""
649 data = self._DoReadFile('24_sorted.dts')
Simon Glassd6051522017-11-13 18:54:59 -0700650 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
Simon Glass57454f42016-11-25 20:15:52 -0700651 U_BOOT_DATA, data)
652
Simon Glasse8561af2018-08-01 15:22:37 -0600653 def testPackZeroOffset(self):
654 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass57454f42016-11-25 20:15:52 -0700655 with self.assertRaises(ValueError) as e:
656 self._DoTestFile('25_pack_zero_size.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600657 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass57454f42016-11-25 20:15:52 -0700658 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
659 str(e.exception))
660
661 def testPackUbootDtb(self):
662 """Test that a device tree can be added to U-Boot"""
663 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
664 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glass72232452016-11-25 20:15:53 -0700665
666 def testPackX86RomNoSize(self):
667 """Test that the end-at-4gb property requires a size property"""
668 with self.assertRaises(ValueError) as e:
669 self._DoTestFile('27_pack_4gb_no_size.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600670 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glass72232452016-11-25 20:15:53 -0700671 "using end-at-4gb", str(e.exception))
672
673 def testPackX86RomOutside(self):
Simon Glasse8561af2018-08-01 15:22:37 -0600674 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glass72232452016-11-25 20:15:53 -0700675 with self.assertRaises(ValueError) as e:
676 self._DoTestFile('28_pack_4gb_outside.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600677 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glasseca32212018-06-01 09:38:12 -0600678 "the section starting at 0xffffffe0 (4294967264)",
Simon Glass72232452016-11-25 20:15:53 -0700679 str(e.exception))
680
681 def testPackX86Rom(self):
682 """Test that a basic x86 ROM can be created"""
683 data = self._DoReadFile('29_x86-rom.dts')
Simon Glassd6051522017-11-13 18:54:59 -0700684 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
685 chr(0) * 2, data)
Simon Glass72232452016-11-25 20:15:53 -0700686
687 def testPackX86RomMeNoDesc(self):
688 """Test that an invalid Intel descriptor entry is detected"""
689 TestFunctional._MakeInputFile('descriptor.bin', '')
690 with self.assertRaises(ValueError) as e:
691 self._DoTestFile('31_x86-rom-me.dts')
692 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
693 "signature", str(e.exception))
694
695 def testPackX86RomBadDesc(self):
696 """Test that the Intel requires a descriptor entry"""
697 with self.assertRaises(ValueError) as e:
698 self._DoTestFile('30_x86-rom-me-no-desc.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600699 self.assertIn("Node '/binman/intel-me': No offset set with "
700 "offset-unset: should another entry provide this correct "
701 "offset?", str(e.exception))
Simon Glass72232452016-11-25 20:15:53 -0700702
703 def testPackX86RomMe(self):
704 """Test that an x86 ROM with an ME region can be created"""
705 data = self._DoReadFile('31_x86-rom-me.dts')
706 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
707
708 def testPackVga(self):
709 """Test that an image with a VGA binary can be created"""
710 data = self._DoReadFile('32_intel-vga.dts')
711 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
712
713 def testPackStart16(self):
714 """Test that an image with an x86 start16 region can be created"""
715 data = self._DoReadFile('33_x86-start16.dts')
716 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
717
Simon Glass6ba679c2018-07-06 10:27:17 -0600718 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glass820af1d2018-07-06 10:27:16 -0600719 """Handle running a test for insertion of microcode
720
721 Args:
722 dts_fname: Name of test .dts file
723 nodtb_data: Data that we expect in the first section
Simon Glass6ba679c2018-07-06 10:27:17 -0600724 ucode_second: True if the microsecond entry is second instead of
725 third
Simon Glass820af1d2018-07-06 10:27:16 -0600726
727 Returns:
728 Tuple:
729 Contents of first region (U-Boot or SPL)
Simon Glasse8561af2018-08-01 15:22:37 -0600730 Offset and size components of microcode pointer, as inserted
Simon Glass820af1d2018-07-06 10:27:16 -0600731 in the above (two 4-byte words)
732 """
Simon Glass3d274232017-11-12 21:52:27 -0700733 data = self._DoReadFile(dts_fname, True)
Simon Glass72232452016-11-25 20:15:53 -0700734
735 # Now check the device tree has no microcode
Simon Glass6ba679c2018-07-06 10:27:17 -0600736 if ucode_second:
737 ucode_content = data[len(nodtb_data):]
738 ucode_pos = len(nodtb_data)
739 dtb_with_ucode = ucode_content[16:]
740 fdt_len = self.GetFdtLen(dtb_with_ucode)
741 else:
742 dtb_with_ucode = data[len(nodtb_data):]
743 fdt_len = self.GetFdtLen(dtb_with_ucode)
744 ucode_content = dtb_with_ucode[fdt_len:]
745 ucode_pos = len(nodtb_data) + fdt_len
Simon Glass72232452016-11-25 20:15:53 -0700746 fname = tools.GetOutputFilename('test.dtb')
747 with open(fname, 'wb') as fd:
Simon Glass820af1d2018-07-06 10:27:16 -0600748 fd.write(dtb_with_ucode)
Simon Glass22c92ca2017-05-27 07:38:29 -0600749 dtb = fdt.FdtScan(fname)
750 ucode = dtb.GetNode('/microcode')
Simon Glass72232452016-11-25 20:15:53 -0700751 self.assertTrue(ucode)
752 for node in ucode.subnodes:
753 self.assertFalse(node.props.get('data'))
754
Simon Glass72232452016-11-25 20:15:53 -0700755 # Check that the microcode appears immediately after the Fdt
756 # This matches the concatenation of the data properties in
Simon Glasse83679d2017-11-12 21:52:26 -0700757 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glass72232452016-11-25 20:15:53 -0700758 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
759 0x78235609)
Simon Glass820af1d2018-07-06 10:27:16 -0600760 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glass72232452016-11-25 20:15:53 -0700761
762 # Check that the microcode pointer was inserted. It should match the
Simon Glasse8561af2018-08-01 15:22:37 -0600763 # expected offset and size
Simon Glass72232452016-11-25 20:15:53 -0700764 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
765 len(ucode_data))
Simon Glass6ba679c2018-07-06 10:27:17 -0600766 u_boot = data[:len(nodtb_data)]
767 return u_boot, pos_and_size
Simon Glass3d274232017-11-12 21:52:27 -0700768
769 def testPackUbootMicrocode(self):
770 """Test that x86 microcode can be handled correctly
771
772 We expect to see the following in the image, in order:
773 u-boot-nodtb.bin with a microcode pointer inserted at the correct
774 place
775 u-boot.dtb with the microcode removed
776 the microcode
777 """
778 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
779 U_BOOT_NODTB_DATA)
Simon Glass72232452016-11-25 20:15:53 -0700780 self.assertEqual('nodtb with microcode' + pos_and_size +
781 ' somewhere in here', first)
782
Simon Glassbac25c82017-05-27 07:38:26 -0600783 def _RunPackUbootSingleMicrocode(self):
Simon Glass72232452016-11-25 20:15:53 -0700784 """Test that x86 microcode can be handled correctly
785
786 We expect to see the following in the image, in order:
787 u-boot-nodtb.bin with a microcode pointer inserted at the correct
788 place
789 u-boot.dtb with the microcode
790 an empty microcode region
791 """
792 # We need the libfdt library to run this test since only that allows
793 # finding the offset of a property. This is required by
794 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glass72232452016-11-25 20:15:53 -0700795 data = self._DoReadFile('35_x86_single_ucode.dts', True)
796
797 second = data[len(U_BOOT_NODTB_DATA):]
798
799 fdt_len = self.GetFdtLen(second)
800 third = second[fdt_len:]
801 second = second[:fdt_len]
802
Simon Glassbac25c82017-05-27 07:38:26 -0600803 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
804 self.assertIn(ucode_data, second)
805 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glass72232452016-11-25 20:15:53 -0700806
Simon Glassbac25c82017-05-27 07:38:26 -0600807 # Check that the microcode pointer was inserted. It should match the
Simon Glasse8561af2018-08-01 15:22:37 -0600808 # expected offset and size
Simon Glassbac25c82017-05-27 07:38:26 -0600809 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
810 len(ucode_data))
811 first = data[:len(U_BOOT_NODTB_DATA)]
812 self.assertEqual('nodtb with microcode' + pos_and_size +
813 ' somewhere in here', first)
Simon Glass996021e2016-11-25 20:15:54 -0700814
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700815 def testPackUbootSingleMicrocode(self):
816 """Test that x86 microcode can be handled correctly with fdt_normal.
817 """
Simon Glassbac25c82017-05-27 07:38:26 -0600818 self._RunPackUbootSingleMicrocode()
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700819
Simon Glass996021e2016-11-25 20:15:54 -0700820 def testUBootImg(self):
821 """Test that u-boot.img can be put in a file"""
822 data = self._DoReadFile('36_u_boot_img.dts')
823 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700824
825 def testNoMicrocode(self):
826 """Test that a missing microcode region is detected"""
827 with self.assertRaises(ValueError) as e:
828 self._DoReadFile('37_x86_no_ucode.dts', True)
829 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
830 "node found in ", str(e.exception))
831
832 def testMicrocodeWithoutNode(self):
833 """Test that a missing u-boot-dtb-with-ucode node is detected"""
834 with self.assertRaises(ValueError) as e:
835 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
836 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
837 "microcode region u-boot-dtb-with-ucode", str(e.exception))
838
839 def testMicrocodeWithoutNode2(self):
840 """Test that a missing u-boot-ucode node is detected"""
841 with self.assertRaises(ValueError) as e:
842 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
843 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
844 "microcode region u-boot-ucode", str(e.exception))
845
846 def testMicrocodeWithoutPtrInElf(self):
847 """Test that a U-Boot binary without the microcode symbol is detected"""
848 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700849 try:
850 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
851 TestFunctional._MakeInputFile('u-boot', fd.read())
852
853 with self.assertRaises(ValueError) as e:
Simon Glassbac25c82017-05-27 07:38:26 -0600854 self._RunPackUbootSingleMicrocode()
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700855 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
856 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
857
858 finally:
859 # Put the original file back
860 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
861 TestFunctional._MakeInputFile('u-boot', fd.read())
862
863 def testMicrocodeNotInImage(self):
864 """Test that microcode must be placed within the image"""
865 with self.assertRaises(ValueError) as e:
866 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
867 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
868 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glassad5a7712018-06-01 09:38:14 -0600869 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700870
871 def testWithoutMicrocode(self):
872 """Test that we can cope with an image without microcode (e.g. qemu)"""
873 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
874 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glassa87014e2018-07-06 10:27:42 -0600875 data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700876
877 # Now check the device tree has no microcode
878 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
879 second = data[len(U_BOOT_NODTB_DATA):]
880
881 fdt_len = self.GetFdtLen(second)
882 self.assertEqual(dtb, second[:fdt_len])
883
884 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
885 third = data[used_len:]
886 self.assertEqual(chr(0) * (0x200 - used_len), third)
887
888 def testUnknownPosSize(self):
889 """Test that microcode must be placed within the image"""
890 with self.assertRaises(ValueError) as e:
891 self._DoReadFile('41_unknown_pos_size.dts', True)
Simon Glasse8561af2018-08-01 15:22:37 -0600892 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700893 "entry 'invalid-entry'", str(e.exception))
Simon Glassb4176d42016-11-25 20:15:56 -0700894
895 def testPackFsp(self):
896 """Test that an image with a FSP binary can be created"""
897 data = self._DoReadFile('42_intel-fsp.dts')
898 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
899
900 def testPackCmc(self):
Bin Mengd7bcdf52017-08-15 22:41:54 -0700901 """Test that an image with a CMC binary can be created"""
Simon Glassb4176d42016-11-25 20:15:56 -0700902 data = self._DoReadFile('43_intel-cmc.dts')
903 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Mengd7bcdf52017-08-15 22:41:54 -0700904
905 def testPackVbt(self):
906 """Test that an image with a VBT binary can be created"""
907 data = self._DoReadFile('46_intel-vbt.dts')
908 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glassac599912017-11-12 21:52:22 -0700909
Simon Glass7f94e832017-11-12 21:52:25 -0700910 def testSplBssPad(self):
911 """Test that we can pad SPL's BSS with zeros"""
Simon Glass3d274232017-11-12 21:52:27 -0700912 # ELF file with a '__bss_size' symbol
913 with open(self.TestFile('bss_data')) as fd:
914 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass7f94e832017-11-12 21:52:25 -0700915 data = self._DoReadFile('47_spl_bss_pad.dts')
916 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
917
Simon Glass24ad3652017-11-13 18:54:54 -0700918 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
919 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
920 with self.assertRaises(ValueError) as e:
921 data = self._DoReadFile('47_spl_bss_pad.dts')
922 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
923 str(e.exception))
924
Simon Glasse83679d2017-11-12 21:52:26 -0700925 def testPackStart16Spl(self):
926 """Test that an image with an x86 start16 region can be created"""
927 data = self._DoReadFile('48_x86-start16-spl.dts')
928 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
929
Simon Glass6ba679c2018-07-06 10:27:17 -0600930 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
931 """Helper function for microcode tests
Simon Glass3d274232017-11-12 21:52:27 -0700932
933 We expect to see the following in the image, in order:
934 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
935 correct place
936 u-boot.dtb with the microcode removed
937 the microcode
Simon Glass6ba679c2018-07-06 10:27:17 -0600938
939 Args:
940 dts: Device tree file to use for test
941 ucode_second: True if the microsecond entry is second instead of
942 third
Simon Glass3d274232017-11-12 21:52:27 -0700943 """
944 # ELF file with a '_dt_ucode_base_size' symbol
945 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
946 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass6ba679c2018-07-06 10:27:17 -0600947 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
948 ucode_second=ucode_second)
Simon Glass3d274232017-11-12 21:52:27 -0700949 self.assertEqual('splnodtb with microc' + pos_and_size +
950 'ter somewhere in here', first)
951
Simon Glass6ba679c2018-07-06 10:27:17 -0600952 def testPackUbootSplMicrocode(self):
953 """Test that x86 microcode can be handled correctly in SPL"""
954 self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
955
956 def testPackUbootSplMicrocodeReorder(self):
957 """Test that order doesn't matter for microcode entries
958
959 This is the same as testPackUbootSplMicrocode but when we process the
960 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
961 entry, so we reply on binman to try later.
962 """
963 self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
964 ucode_second=True)
965
Simon Glassa409c932017-11-12 21:52:28 -0700966 def testPackMrc(self):
967 """Test that an image with an MRC binary can be created"""
968 data = self._DoReadFile('50_intel_mrc.dts')
969 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
970
Simon Glass9aa6a6f2017-11-13 18:54:55 -0700971 def testSplDtb(self):
972 """Test that an image with spl/u-boot-spl.dtb can be created"""
973 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
974 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
975
Simon Glass0a6da312017-11-13 18:54:56 -0700976 def testSplNoDtb(self):
977 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
978 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
979 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
980
Simon Glass4ca8e042017-11-13 18:55:01 -0700981 def testSymbols(self):
982 """Test binman can assign symbols embedded in U-Boot"""
983 elf_fname = self.TestFile('u_boot_binman_syms')
984 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
985 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glasse8561af2018-08-01 15:22:37 -0600986 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass4ca8e042017-11-13 18:55:01 -0700987
988 with open(self.TestFile('u_boot_binman_syms')) as fd:
989 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
990 data = self._DoReadFile('53_symbols.dts')
991 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
992 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
993 U_BOOT_DATA +
994 sym_values + U_BOOT_SPL_DATA[16:])
995 self.assertEqual(expected, data)
996
Simon Glasse76a3e62018-06-01 09:38:11 -0600997 def testPackUnitAddress(self):
998 """Test that we support multiple binaries with the same name"""
999 data = self._DoReadFile('54_unit_address.dts')
1000 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1001
Simon Glassa91e1152018-06-01 09:38:16 -06001002 def testSections(self):
1003 """Basic test of sections"""
1004 data = self._DoReadFile('55_sections.dts')
1005 expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 + '&' * 8
1006 self.assertEqual(expected, data)
Simon Glassac599912017-11-12 21:52:22 -07001007
Simon Glass30732662018-06-01 09:38:20 -06001008 def testMap(self):
1009 """Tests outputting a map of the images"""
Simon Glassa87014e2018-07-06 10:27:42 -06001010 _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
Simon Glasse8561af2018-08-01 15:22:37 -06001011 self.assertEqual(''' Offset Size Name
Simon Glass30732662018-06-01 09:38:20 -0600101200000000 00000010 section@0
1013 00000000 00000004 u-boot
101400000010 00000010 section@1
1015 00000000 00000004 u-boot
1016''', map_data)
1017
Simon Glass3b78d532018-06-01 09:38:21 -06001018 def testNamePrefix(self):
1019 """Tests that name prefixes are used"""
Simon Glassa87014e2018-07-06 10:27:42 -06001020 _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
Simon Glasse8561af2018-08-01 15:22:37 -06001021 self.assertEqual(''' Offset Size Name
Simon Glass3b78d532018-06-01 09:38:21 -0600102200000000 00000010 section@0
1023 00000000 00000004 ro-u-boot
102400000010 00000010 section@1
1025 00000000 00000004 rw-u-boot
1026''', map_data)
1027
Simon Glass6ba679c2018-07-06 10:27:17 -06001028 def testUnknownContents(self):
1029 """Test that obtaining the contents works as expected"""
1030 with self.assertRaises(ValueError) as e:
1031 self._DoReadFile('57_unknown_contents.dts', True)
1032 self.assertIn("Section '/binman': Internal error: Could not complete "
1033 "processing of contents: remaining [<_testing.Entry__testing ",
1034 str(e.exception))
1035
Simon Glass2e1169f2018-07-06 10:27:19 -06001036 def testBadChangeSize(self):
1037 """Test that trying to change the size of an entry fails"""
1038 with self.assertRaises(ValueError) as e:
1039 self._DoReadFile('59_change_size.dts', True)
1040 self.assertIn("Node '/binman/_testing': Cannot update entry size from "
1041 '2 to 1', str(e.exception))
1042
Simon Glassa87014e2018-07-06 10:27:42 -06001043 def testUpdateFdt(self):
Simon Glasse8561af2018-08-01 15:22:37 -06001044 """Test that we can update the device tree with offset/size info"""
Simon Glassa87014e2018-07-06 10:27:42 -06001045 _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
1046 update_dtb=True)
Simon Glasse8561af2018-08-01 15:22:37 -06001047 props = self._GetPropTree(out_dtb_fname, ['offset', 'size'])
Simon Glassa87014e2018-07-06 10:27:42 -06001048 with open('/tmp/x.dtb', 'wb') as outf:
1049 with open(out_dtb_fname) as inf:
1050 outf.write(inf.read())
1051 self.assertEqual({
Simon Glasse8561af2018-08-01 15:22:37 -06001052 '_testing:offset': 32,
Simon Glassa87014e2018-07-06 10:27:42 -06001053 '_testing:size': 1,
Simon Glasse8561af2018-08-01 15:22:37 -06001054 'section@0/u-boot:offset': 0,
Simon Glassa87014e2018-07-06 10:27:42 -06001055 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glasse8561af2018-08-01 15:22:37 -06001056 'section@0:offset': 0,
Simon Glassa87014e2018-07-06 10:27:42 -06001057 'section@0:size': 16,
1058
Simon Glasse8561af2018-08-01 15:22:37 -06001059 'section@1/u-boot:offset': 0,
Simon Glassa87014e2018-07-06 10:27:42 -06001060 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glasse8561af2018-08-01 15:22:37 -06001061 'section@1:offset': 16,
Simon Glassa87014e2018-07-06 10:27:42 -06001062 'section@1:size': 16,
1063 'size': 40
1064 }, props)
1065
1066 def testUpdateFdtBad(self):
1067 """Test that we detect when ProcessFdt never completes"""
1068 with self.assertRaises(ValueError) as e:
1069 self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
1070 self.assertIn('Could not complete processing of Fdt: remaining '
1071 '[<_testing.Entry__testing', str(e.exception))
Simon Glass2e1169f2018-07-06 10:27:19 -06001072
Simon Glassac599912017-11-12 21:52:22 -07001073if __name__ == "__main__":
1074 unittest.main()