blob: 9c01805c72e548b55b7f5f0975b6dfff6da3f1f5 [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
Simon Glassfd8d1f72018-07-17 13:25:36 -060024import test_util
Simon Glass4f443042016-11-25 20:15:52 -070025import tools
26import tout
27
28# Contents of test files, corresponding to different entry types
Simon Glass6b187df2017-11-12 21:52:27 -070029U_BOOT_DATA = '1234'
30U_BOOT_IMG_DATA = 'img'
Simon Glassf6898902017-11-13 18:54:59 -070031U_BOOT_SPL_DATA = '56780123456789abcde'
Simon Glass6b187df2017-11-12 21:52:27 -070032BLOB_DATA = '89'
33ME_DATA = '0abcd'
34VGA_DATA = 'vga'
35U_BOOT_DTB_DATA = 'udtb'
Simon Glass47419ea2017-11-13 18:54:55 -070036U_BOOT_SPL_DTB_DATA = 'spldtb'
Simon Glass6b187df2017-11-12 21:52:27 -070037X86_START16_DATA = 'start16'
38X86_START16_SPL_DATA = 'start16spl'
39U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
40U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
41FSP_DATA = 'fsp'
42CMC_DATA = 'cmc'
43VBT_DATA = 'vbt'
Simon Glassca4f4ff2017-11-12 21:52:28 -070044MRC_DATA = 'mrc'
Simon Glassbb748372018-07-17 13:25:33 -060045TEXT_DATA = 'text'
46TEXT_DATA2 = 'text2'
47TEXT_DATA3 = 'text3'
Simon Glass4f443042016-11-25 20:15:52 -070048
49class TestFunctional(unittest.TestCase):
50 """Functional tests for binman
51
52 Most of these use a sample .dts file to build an image and then check
53 that it looks correct. The sample files are in the test/ subdirectory
54 and are numbered.
55
56 For each entry type a very small test file is created using fixed
57 string contents. This makes it easy to test that things look right, and
58 debug problems.
59
60 In some cases a 'real' file must be used - these are also supplied in
61 the test/ diurectory.
62 """
63 @classmethod
64 def setUpClass(self):
Simon Glass4d5994f2017-11-12 21:52:20 -070065 global entry
66 import entry
67
Simon Glass4f443042016-11-25 20:15:52 -070068 # Handle the case where argv[0] is 'python'
69 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
70 self._binman_pathname = os.path.join(self._binman_dir, 'binman')
71
72 # Create a temporary directory for input files
73 self._indir = tempfile.mkdtemp(prefix='binmant.')
74
75 # Create some test files
76 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
77 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
78 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
79 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -070080 TestFunctional._MakeInputFile('me.bin', ME_DATA)
81 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070082 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
Simon Glass47419ea2017-11-13 18:54:55 -070083 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -070084 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Simon Glass87722132017-11-12 21:52:26 -070085 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
86 X86_START16_SPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070087 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass6b187df2017-11-12 21:52:27 -070088 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
89 U_BOOT_SPL_NODTB_DATA)
Simon Glassda229092016-11-25 20:15:56 -070090 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
91 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Meng59ea8c22017-08-15 22:41:54 -070092 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassca4f4ff2017-11-12 21:52:28 -070093 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070094 self._output_setup = False
95
Simon Glasse0ff8552016-11-25 20:15:53 -070096 # ELF file with a '_dt_ucode_base_size' symbol
97 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
98 TestFunctional._MakeInputFile('u-boot', fd.read())
99
100 # Intel flash descriptor file
101 with open(self.TestFile('descriptor.bin')) as fd:
102 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
103
Simon Glass4f443042016-11-25 20:15:52 -0700104 @classmethod
105 def tearDownClass(self):
106 """Remove the temporary input directory and its contents"""
107 if self._indir:
108 shutil.rmtree(self._indir)
109 self._indir = None
110
111 def setUp(self):
112 # Enable this to turn on debugging output
113 # tout.Init(tout.DEBUG)
114 command.test_result = None
115
116 def tearDown(self):
117 """Remove the temporary output directory"""
118 tools._FinaliseForTest()
119
120 def _RunBinman(self, *args, **kwargs):
121 """Run binman using the command line
122
123 Args:
124 Arguments to pass, as a list of strings
125 kwargs: Arguments to pass to Command.RunPipe()
126 """
127 result = command.RunPipe([[self._binman_pathname] + list(args)],
128 capture=True, capture_stderr=True, raise_on_error=False)
129 if result.return_code and kwargs.get('raise_on_error', True):
130 raise Exception("Error running '%s': %s" % (' '.join(args),
131 result.stdout + result.stderr))
132 return result
133
134 def _DoBinman(self, *args):
135 """Run binman using directly (in the same process)
136
137 Args:
138 Arguments to pass, as a list of strings
139 Returns:
140 Return value (0 for success)
141 """
Simon Glass7fe91732017-11-13 18:55:00 -0700142 args = list(args)
143 if '-D' in sys.argv:
144 args = args + ['-D']
145 (options, args) = cmdline.ParseArgs(args)
Simon Glass4f443042016-11-25 20:15:52 -0700146 options.pager = 'binman-invalid-pager'
147 options.build_dir = self._indir
148
149 # For testing, you can force an increase in verbosity here
150 # options.verbosity = tout.DEBUG
151 return control.Binman(options, args)
152
Simon Glass53af22a2018-07-17 13:25:32 -0600153 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
154 entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700155 """Run binman with a given test file
156
157 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600158 fname: Device-tree source filename to use (e.g. 05_simple.dts)
159 debug: True to enable debugging output
Simon Glass3b0c3822018-06-01 09:38:20 -0600160 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600161 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600162 tree before packing it into the image
Simon Glass4f443042016-11-25 20:15:52 -0700163 """
Simon Glass7fe91732017-11-13 18:55:00 -0700164 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
165 if debug:
166 args.append('-D')
Simon Glass3b0c3822018-06-01 09:38:20 -0600167 if map:
168 args.append('-m')
Simon Glass16b8d6b2018-07-06 10:27:42 -0600169 if update_dtb:
170 args.append('-up')
Simon Glass53af22a2018-07-17 13:25:32 -0600171 if entry_args:
172 for arg, value in entry_args.iteritems():
173 args.append('-a%s=%s' % (arg, value))
Simon Glass7fe91732017-11-13 18:55:00 -0700174 return self._DoBinman(*args)
Simon Glass4f443042016-11-25 20:15:52 -0700175
176 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glasse0ff8552016-11-25 20:15:53 -0700177 """Set up a new test device-tree file
178
179 The given file is compiled and set up as the device tree to be used
180 for ths test.
181
182 Args:
183 fname: Filename of .dts file to read
Simon Glass7ae5f312018-06-01 09:38:19 -0600184 outfile: Output filename for compiled device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700185
186 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600187 Contents of device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700188 """
Simon Glass4f443042016-11-25 20:15:52 -0700189 if not self._output_setup:
190 tools.PrepareOutputDir(self._indir, True)
191 self._output_setup = True
192 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
193 with open(dtb) as fd:
194 data = fd.read()
195 TestFunctional._MakeInputFile(outfile, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700196 return data
Simon Glass4f443042016-11-25 20:15:52 -0700197
Simon Glass16b8d6b2018-07-06 10:27:42 -0600198 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass53af22a2018-07-17 13:25:32 -0600199 update_dtb=False, entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700200 """Run binman and return the resulting image
201
202 This runs binman with a given test file and then reads the resulting
203 output file. It is a shortcut function since most tests need to do
204 these steps.
205
206 Raises an assertion failure if binman returns a non-zero exit code.
207
208 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600209 fname: Device-tree source filename to use (e.g. 05_simple.dts)
Simon Glass4f443042016-11-25 20:15:52 -0700210 use_real_dtb: True to use the test file as the contents of
211 the u-boot-dtb entry. Normally this is not needed and the
212 test contents (the U_BOOT_DTB_DATA string) can be used.
213 But in some test we need the real contents.
Simon Glass3b0c3822018-06-01 09:38:20 -0600214 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600215 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600216 tree before packing it into the image
Simon Glasse0ff8552016-11-25 20:15:53 -0700217
218 Returns:
219 Tuple:
220 Resulting image contents
221 Device tree contents
Simon Glass3b0c3822018-06-01 09:38:20 -0600222 Map data showing contents of image (or None if none)
Simon Glassea6922e2018-07-17 13:25:27 -0600223 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass4f443042016-11-25 20:15:52 -0700224 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700225 dtb_data = None
Simon Glass4f443042016-11-25 20:15:52 -0700226 # Use the compiled test file as the u-boot-dtb input
227 if use_real_dtb:
Simon Glasse0ff8552016-11-25 20:15:53 -0700228 dtb_data = self._SetupDtb(fname)
Simon Glass4f443042016-11-25 20:15:52 -0700229
230 try:
Simon Glass53af22a2018-07-17 13:25:32 -0600231 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
232 entry_args=entry_args)
Simon Glass4f443042016-11-25 20:15:52 -0700233 self.assertEqual(0, retcode)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600234 out_dtb_fname = control.GetFdtPath('u-boot.dtb')
Simon Glass4f443042016-11-25 20:15:52 -0700235
236 # Find the (only) image, read it and return its contents
237 image = control.images['image']
Simon Glass16b8d6b2018-07-06 10:27:42 -0600238 image_fname = tools.GetOutputFilename('image.bin')
239 self.assertTrue(os.path.exists(image_fname))
Simon Glass3b0c3822018-06-01 09:38:20 -0600240 if map:
241 map_fname = tools.GetOutputFilename('image.map')
242 with open(map_fname) as fd:
243 map_data = fd.read()
244 else:
245 map_data = None
Simon Glass16b8d6b2018-07-06 10:27:42 -0600246 with open(image_fname) as fd:
247 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass4f443042016-11-25 20:15:52 -0700248 finally:
249 # Put the test file back
250 if use_real_dtb:
251 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
252
Simon Glasse0ff8552016-11-25 20:15:53 -0700253 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass7ae5f312018-06-01 09:38:19 -0600254 """Helper function which discards the device-tree binary
255
256 Args:
257 fname: Device-tree source filename to use (e.g. 05_simple.dts)
258 use_real_dtb: True to use the test file as the contents of
259 the u-boot-dtb entry. Normally this is not needed and the
260 test contents (the U_BOOT_DTB_DATA string) can be used.
261 But in some test we need the real contents.
Simon Glassea6922e2018-07-17 13:25:27 -0600262
263 Returns:
264 Resulting image contents
Simon Glass7ae5f312018-06-01 09:38:19 -0600265 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700266 return self._DoReadFileDtb(fname, use_real_dtb)[0]
267
Simon Glass4f443042016-11-25 20:15:52 -0700268 @classmethod
269 def _MakeInputFile(self, fname, contents):
270 """Create a new test input file, creating directories as needed
271
272 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600273 fname: Filename to create
Simon Glass4f443042016-11-25 20:15:52 -0700274 contents: File contents to write in to the file
275 Returns:
276 Full pathname of file created
277 """
278 pathname = os.path.join(self._indir, fname)
279 dirname = os.path.dirname(pathname)
280 if dirname and not os.path.exists(dirname):
281 os.makedirs(dirname)
282 with open(pathname, 'wb') as fd:
283 fd.write(contents)
284 return pathname
285
286 @classmethod
287 def TestFile(self, fname):
288 return os.path.join(self._binman_dir, 'test', fname)
289
290 def AssertInList(self, grep_list, target):
291 """Assert that at least one of a list of things is in a target
292
293 Args:
294 grep_list: List of strings to check
295 target: Target string
296 """
297 for grep in grep_list:
298 if grep in target:
299 return
300 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
301
302 def CheckNoGaps(self, entries):
303 """Check that all entries fit together without gaps
304
305 Args:
306 entries: List of entries to check
307 """
Simon Glass3ab95982018-08-01 15:22:37 -0600308 offset = 0
Simon Glass4f443042016-11-25 20:15:52 -0700309 for entry in entries.values():
Simon Glass3ab95982018-08-01 15:22:37 -0600310 self.assertEqual(offset, entry.offset)
311 offset += entry.size
Simon Glass4f443042016-11-25 20:15:52 -0700312
Simon Glasse0ff8552016-11-25 20:15:53 -0700313 def GetFdtLen(self, dtb):
Simon Glass7ae5f312018-06-01 09:38:19 -0600314 """Get the totalsize field from a device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700315
316 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600317 dtb: Device-tree binary contents
Simon Glasse0ff8552016-11-25 20:15:53 -0700318
319 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600320 Total size of device-tree binary, from the header
Simon Glasse0ff8552016-11-25 20:15:53 -0700321 """
322 return struct.unpack('>L', dtb[4:8])[0]
323
Simon Glass16b8d6b2018-07-06 10:27:42 -0600324 def _GetPropTree(self, dtb_data, node_names):
325 def AddNode(node, path):
326 if node.name != '/':
327 path += '/' + node.name
Simon Glass16b8d6b2018-07-06 10:27:42 -0600328 for subnode in node.subnodes:
329 for prop in subnode.props.values():
330 if prop.name in node_names:
331 prop_path = path + '/' + subnode.name + ':' + prop.name
332 tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
333 prop.value)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600334 AddNode(subnode, path)
335
336 tree = {}
337 dtb = fdt.Fdt(dtb_data)
338 dtb.Scan()
339 AddNode(dtb.GetRoot(), '')
340 return tree
341
Simon Glass4f443042016-11-25 20:15:52 -0700342 def testRun(self):
343 """Test a basic run with valid args"""
344 result = self._RunBinman('-h')
345
346 def testFullHelp(self):
347 """Test that the full help is displayed with -H"""
348 result = self._RunBinman('-H')
349 help_file = os.path.join(self._binman_dir, 'README')
Tom Rini3759df02018-01-16 15:29:50 -0500350 # Remove possible extraneous strings
351 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
352 gothelp = result.stdout.replace(extra, '')
353 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass4f443042016-11-25 20:15:52 -0700354 self.assertEqual(0, len(result.stderr))
355 self.assertEqual(0, result.return_code)
356
357 def testFullHelpInternal(self):
358 """Test that the full help is displayed with -H"""
359 try:
360 command.test_result = command.CommandResult()
361 result = self._DoBinman('-H')
362 help_file = os.path.join(self._binman_dir, 'README')
363 finally:
364 command.test_result = None
365
366 def testHelp(self):
367 """Test that the basic help is displayed with -h"""
368 result = self._RunBinman('-h')
369 self.assertTrue(len(result.stdout) > 200)
370 self.assertEqual(0, len(result.stderr))
371 self.assertEqual(0, result.return_code)
372
Simon Glass4f443042016-11-25 20:15:52 -0700373 def testBoard(self):
374 """Test that we can run it with a specific board"""
375 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
376 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
377 result = self._DoBinman('-b', 'sandbox')
378 self.assertEqual(0, result)
379
380 def testNeedBoard(self):
381 """Test that we get an error when no board ius supplied"""
382 with self.assertRaises(ValueError) as e:
383 result = self._DoBinman()
384 self.assertIn("Must provide a board to process (use -b <board>)",
385 str(e.exception))
386
387 def testMissingDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600388 """Test that an invalid device-tree file generates an error"""
Simon Glass4f443042016-11-25 20:15:52 -0700389 with self.assertRaises(Exception) as e:
390 self._RunBinman('-d', 'missing_file')
391 # We get one error from libfdt, and a different one from fdtget.
392 self.AssertInList(["Couldn't open blob from 'missing_file'",
393 'No such file or directory'], str(e.exception))
394
395 def testBrokenDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600396 """Test that an invalid device-tree source file generates an error
Simon Glass4f443042016-11-25 20:15:52 -0700397
398 Since this is a source file it should be compiled and the error
399 will come from the device-tree compiler (dtc).
400 """
401 with self.assertRaises(Exception) as e:
402 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
403 self.assertIn("FATAL ERROR: Unable to parse input tree",
404 str(e.exception))
405
406 def testMissingNode(self):
407 """Test that a device tree without a 'binman' node generates an error"""
408 with self.assertRaises(Exception) as e:
409 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
410 self.assertIn("does not have a 'binman' node", str(e.exception))
411
412 def testEmpty(self):
413 """Test that an empty binman node works OK (i.e. does nothing)"""
414 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
415 self.assertEqual(0, len(result.stderr))
416 self.assertEqual(0, result.return_code)
417
418 def testInvalidEntry(self):
419 """Test that an invalid entry is flagged"""
420 with self.assertRaises(Exception) as e:
421 result = self._RunBinman('-d',
422 self.TestFile('04_invalid_entry.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700423 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
424 "'/binman/not-a-valid-type'", str(e.exception))
425
426 def testSimple(self):
427 """Test a simple binman with a single file"""
428 data = self._DoReadFile('05_simple.dts')
429 self.assertEqual(U_BOOT_DATA, data)
430
Simon Glass7fe91732017-11-13 18:55:00 -0700431 def testSimpleDebug(self):
432 """Test a simple binman run with debugging enabled"""
433 data = self._DoTestFile('05_simple.dts', debug=True)
434
Simon Glass4f443042016-11-25 20:15:52 -0700435 def testDual(self):
436 """Test that we can handle creating two images
437
438 This also tests image padding.
439 """
440 retcode = self._DoTestFile('06_dual_image.dts')
441 self.assertEqual(0, retcode)
442
443 image = control.images['image1']
444 self.assertEqual(len(U_BOOT_DATA), image._size)
445 fname = tools.GetOutputFilename('image1.bin')
446 self.assertTrue(os.path.exists(fname))
447 with open(fname) as fd:
448 data = fd.read()
449 self.assertEqual(U_BOOT_DATA, data)
450
451 image = control.images['image2']
452 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
453 fname = tools.GetOutputFilename('image2.bin')
454 self.assertTrue(os.path.exists(fname))
455 with open(fname) as fd:
456 data = fd.read()
457 self.assertEqual(U_BOOT_DATA, data[3:7])
458 self.assertEqual(chr(0) * 3, data[:3])
459 self.assertEqual(chr(0) * 5, data[7:])
460
461 def testBadAlign(self):
462 """Test that an invalid alignment value is detected"""
463 with self.assertRaises(ValueError) as e:
464 self._DoTestFile('07_bad_align.dts')
465 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
466 "of two", str(e.exception))
467
468 def testPackSimple(self):
469 """Test that packing works as expected"""
470 retcode = self._DoTestFile('08_pack.dts')
471 self.assertEqual(0, retcode)
472 self.assertIn('image', control.images)
473 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600474 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700475 self.assertEqual(5, len(entries))
476
477 # First u-boot
478 self.assertIn('u-boot', entries)
479 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600480 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700481 self.assertEqual(len(U_BOOT_DATA), entry.size)
482
483 # Second u-boot, aligned to 16-byte boundary
484 self.assertIn('u-boot-align', entries)
485 entry = entries['u-boot-align']
Simon Glass3ab95982018-08-01 15:22:37 -0600486 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700487 self.assertEqual(len(U_BOOT_DATA), entry.size)
488
489 # Third u-boot, size 23 bytes
490 self.assertIn('u-boot-size', entries)
491 entry = entries['u-boot-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600492 self.assertEqual(20, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700493 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
494 self.assertEqual(23, entry.size)
495
496 # Fourth u-boot, placed immediate after the above
497 self.assertIn('u-boot-next', entries)
498 entry = entries['u-boot-next']
Simon Glass3ab95982018-08-01 15:22:37 -0600499 self.assertEqual(43, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700500 self.assertEqual(len(U_BOOT_DATA), entry.size)
501
Simon Glass3ab95982018-08-01 15:22:37 -0600502 # Fifth u-boot, placed at a fixed offset
Simon Glass4f443042016-11-25 20:15:52 -0700503 self.assertIn('u-boot-fixed', entries)
504 entry = entries['u-boot-fixed']
Simon Glass3ab95982018-08-01 15:22:37 -0600505 self.assertEqual(61, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700506 self.assertEqual(len(U_BOOT_DATA), entry.size)
507
508 self.assertEqual(65, image._size)
509
510 def testPackExtra(self):
511 """Test that extra packing feature works as expected"""
512 retcode = self._DoTestFile('09_pack_extra.dts')
513
514 self.assertEqual(0, retcode)
515 self.assertIn('image', control.images)
516 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600517 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700518 self.assertEqual(5, len(entries))
519
520 # First u-boot with padding before and after
521 self.assertIn('u-boot', entries)
522 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600523 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700524 self.assertEqual(3, entry.pad_before)
525 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
526
527 # Second u-boot has an aligned size, but it has no effect
528 self.assertIn('u-boot-align-size-nop', entries)
529 entry = entries['u-boot-align-size-nop']
Simon Glass3ab95982018-08-01 15:22:37 -0600530 self.assertEqual(12, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700531 self.assertEqual(4, entry.size)
532
533 # Third u-boot has an aligned size too
534 self.assertIn('u-boot-align-size', entries)
535 entry = entries['u-boot-align-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600536 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700537 self.assertEqual(32, entry.size)
538
539 # Fourth u-boot has an aligned end
540 self.assertIn('u-boot-align-end', entries)
541 entry = entries['u-boot-align-end']
Simon Glass3ab95982018-08-01 15:22:37 -0600542 self.assertEqual(48, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700543 self.assertEqual(16, entry.size)
544
545 # Fifth u-boot immediately afterwards
546 self.assertIn('u-boot-align-both', entries)
547 entry = entries['u-boot-align-both']
Simon Glass3ab95982018-08-01 15:22:37 -0600548 self.assertEqual(64, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700549 self.assertEqual(64, entry.size)
550
551 self.CheckNoGaps(entries)
552 self.assertEqual(128, image._size)
553
554 def testPackAlignPowerOf2(self):
555 """Test that invalid entry alignment is detected"""
556 with self.assertRaises(ValueError) as e:
557 self._DoTestFile('10_pack_align_power2.dts')
558 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
559 "of two", str(e.exception))
560
561 def testPackAlignSizePowerOf2(self):
562 """Test that invalid entry size alignment is detected"""
563 with self.assertRaises(ValueError) as e:
564 self._DoTestFile('11_pack_align_size_power2.dts')
565 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
566 "power of two", str(e.exception))
567
568 def testPackInvalidAlign(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600569 """Test detection of an offset that does not match its alignment"""
Simon Glass4f443042016-11-25 20:15:52 -0700570 with self.assertRaises(ValueError) as e:
571 self._DoTestFile('12_pack_inv_align.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600572 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700573 "align 0x4 (4)", str(e.exception))
574
575 def testPackInvalidSizeAlign(self):
576 """Test that invalid entry size alignment is detected"""
577 with self.assertRaises(ValueError) as e:
578 self._DoTestFile('13_pack_inv_size_align.dts')
579 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
580 "align-size 0x4 (4)", str(e.exception))
581
582 def testPackOverlap(self):
583 """Test that overlapping regions are detected"""
584 with self.assertRaises(ValueError) as e:
585 self._DoTestFile('14_pack_overlap.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600586 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700587 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
588 str(e.exception))
589
590 def testPackEntryOverflow(self):
591 """Test that entries that overflow their size are detected"""
592 with self.assertRaises(ValueError) as e:
593 self._DoTestFile('15_pack_overflow.dts')
594 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
595 "but entry size is 0x3 (3)", str(e.exception))
596
597 def testPackImageOverflow(self):
598 """Test that entries which overflow the image size are detected"""
599 with self.assertRaises(ValueError) as e:
600 self._DoTestFile('16_pack_image_overflow.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600601 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass4f443042016-11-25 20:15:52 -0700602 "size 0x3 (3)", str(e.exception))
603
604 def testPackImageSize(self):
605 """Test that the image size can be set"""
606 retcode = self._DoTestFile('17_pack_image_size.dts')
607 self.assertEqual(0, retcode)
608 self.assertIn('image', control.images)
609 image = control.images['image']
610 self.assertEqual(7, image._size)
611
612 def testPackImageSizeAlign(self):
613 """Test that image size alignemnt works as expected"""
614 retcode = self._DoTestFile('18_pack_image_align.dts')
615 self.assertEqual(0, retcode)
616 self.assertIn('image', control.images)
617 image = control.images['image']
618 self.assertEqual(16, image._size)
619
620 def testPackInvalidImageAlign(self):
621 """Test that invalid image alignment is detected"""
622 with self.assertRaises(ValueError) as e:
623 self._DoTestFile('19_pack_inv_image_align.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600624 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700625 "align-size 0x8 (8)", str(e.exception))
626
627 def testPackAlignPowerOf2(self):
628 """Test that invalid image alignment is detected"""
629 with self.assertRaises(ValueError) as e:
630 self._DoTestFile('20_pack_inv_image_align_power2.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600631 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass4f443042016-11-25 20:15:52 -0700632 "two", str(e.exception))
633
634 def testImagePadByte(self):
635 """Test that the image pad byte can be specified"""
Simon Glass19790632017-11-13 18:55:01 -0700636 with open(self.TestFile('bss_data')) as fd:
637 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass4f443042016-11-25 20:15:52 -0700638 data = self._DoReadFile('21_image_pad.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700639 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700640
641 def testImageName(self):
642 """Test that image files can be named"""
643 retcode = self._DoTestFile('22_image_name.dts')
644 self.assertEqual(0, retcode)
645 image = control.images['image1']
646 fname = tools.GetOutputFilename('test-name')
647 self.assertTrue(os.path.exists(fname))
648
649 image = control.images['image2']
650 fname = tools.GetOutputFilename('test-name.xx')
651 self.assertTrue(os.path.exists(fname))
652
653 def testBlobFilename(self):
654 """Test that generic blobs can be provided by filename"""
655 data = self._DoReadFile('23_blob.dts')
656 self.assertEqual(BLOB_DATA, data)
657
658 def testPackSorted(self):
659 """Test that entries can be sorted"""
660 data = self._DoReadFile('24_sorted.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700661 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
Simon Glass4f443042016-11-25 20:15:52 -0700662 U_BOOT_DATA, data)
663
Simon Glass3ab95982018-08-01 15:22:37 -0600664 def testPackZeroOffset(self):
665 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass4f443042016-11-25 20:15:52 -0700666 with self.assertRaises(ValueError) as e:
667 self._DoTestFile('25_pack_zero_size.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600668 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700669 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
670 str(e.exception))
671
672 def testPackUbootDtb(self):
673 """Test that a device tree can be added to U-Boot"""
674 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
675 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700676
677 def testPackX86RomNoSize(self):
678 """Test that the end-at-4gb property requires a size property"""
679 with self.assertRaises(ValueError) as e:
680 self._DoTestFile('27_pack_4gb_no_size.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600681 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glasse0ff8552016-11-25 20:15:53 -0700682 "using end-at-4gb", str(e.exception))
683
684 def testPackX86RomOutside(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600685 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glasse0ff8552016-11-25 20:15:53 -0700686 with self.assertRaises(ValueError) as e:
687 self._DoTestFile('28_pack_4gb_outside.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600688 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glass8f1da502018-06-01 09:38:12 -0600689 "the section starting at 0xffffffe0 (4294967264)",
Simon Glasse0ff8552016-11-25 20:15:53 -0700690 str(e.exception))
691
692 def testPackX86Rom(self):
693 """Test that a basic x86 ROM can be created"""
694 data = self._DoReadFile('29_x86-rom.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700695 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
696 chr(0) * 2, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700697
698 def testPackX86RomMeNoDesc(self):
699 """Test that an invalid Intel descriptor entry is detected"""
700 TestFunctional._MakeInputFile('descriptor.bin', '')
701 with self.assertRaises(ValueError) as e:
702 self._DoTestFile('31_x86-rom-me.dts')
703 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
704 "signature", str(e.exception))
705
706 def testPackX86RomBadDesc(self):
707 """Test that the Intel requires a descriptor entry"""
708 with self.assertRaises(ValueError) as e:
709 self._DoTestFile('30_x86-rom-me-no-desc.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600710 self.assertIn("Node '/binman/intel-me': No offset set with "
711 "offset-unset: should another entry provide this correct "
712 "offset?", str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700713
714 def testPackX86RomMe(self):
715 """Test that an x86 ROM with an ME region can be created"""
716 data = self._DoReadFile('31_x86-rom-me.dts')
717 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
718
719 def testPackVga(self):
720 """Test that an image with a VGA binary can be created"""
721 data = self._DoReadFile('32_intel-vga.dts')
722 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
723
724 def testPackStart16(self):
725 """Test that an image with an x86 start16 region can be created"""
726 data = self._DoReadFile('33_x86-start16.dts')
727 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
728
Simon Glass736bb0a2018-07-06 10:27:17 -0600729 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glassadc57012018-07-06 10:27:16 -0600730 """Handle running a test for insertion of microcode
731
732 Args:
733 dts_fname: Name of test .dts file
734 nodtb_data: Data that we expect in the first section
Simon Glass736bb0a2018-07-06 10:27:17 -0600735 ucode_second: True if the microsecond entry is second instead of
736 third
Simon Glassadc57012018-07-06 10:27:16 -0600737
738 Returns:
739 Tuple:
740 Contents of first region (U-Boot or SPL)
Simon Glass3ab95982018-08-01 15:22:37 -0600741 Offset and size components of microcode pointer, as inserted
Simon Glassadc57012018-07-06 10:27:16 -0600742 in the above (two 4-byte words)
743 """
Simon Glass6b187df2017-11-12 21:52:27 -0700744 data = self._DoReadFile(dts_fname, True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700745
746 # Now check the device tree has no microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600747 if ucode_second:
748 ucode_content = data[len(nodtb_data):]
749 ucode_pos = len(nodtb_data)
750 dtb_with_ucode = ucode_content[16:]
751 fdt_len = self.GetFdtLen(dtb_with_ucode)
752 else:
753 dtb_with_ucode = data[len(nodtb_data):]
754 fdt_len = self.GetFdtLen(dtb_with_ucode)
755 ucode_content = dtb_with_ucode[fdt_len:]
756 ucode_pos = len(nodtb_data) + fdt_len
Simon Glasse0ff8552016-11-25 20:15:53 -0700757 fname = tools.GetOutputFilename('test.dtb')
758 with open(fname, 'wb') as fd:
Simon Glassadc57012018-07-06 10:27:16 -0600759 fd.write(dtb_with_ucode)
Simon Glassec3f3782017-05-27 07:38:29 -0600760 dtb = fdt.FdtScan(fname)
761 ucode = dtb.GetNode('/microcode')
Simon Glasse0ff8552016-11-25 20:15:53 -0700762 self.assertTrue(ucode)
763 for node in ucode.subnodes:
764 self.assertFalse(node.props.get('data'))
765
Simon Glasse0ff8552016-11-25 20:15:53 -0700766 # Check that the microcode appears immediately after the Fdt
767 # This matches the concatenation of the data properties in
Simon Glass87722132017-11-12 21:52:26 -0700768 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glasse0ff8552016-11-25 20:15:53 -0700769 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
770 0x78235609)
Simon Glassadc57012018-07-06 10:27:16 -0600771 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glasse0ff8552016-11-25 20:15:53 -0700772
773 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600774 # expected offset and size
Simon Glasse0ff8552016-11-25 20:15:53 -0700775 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
776 len(ucode_data))
Simon Glass736bb0a2018-07-06 10:27:17 -0600777 u_boot = data[:len(nodtb_data)]
778 return u_boot, pos_and_size
Simon Glass6b187df2017-11-12 21:52:27 -0700779
780 def testPackUbootMicrocode(self):
781 """Test that x86 microcode can be handled correctly
782
783 We expect to see the following in the image, in order:
784 u-boot-nodtb.bin with a microcode pointer inserted at the correct
785 place
786 u-boot.dtb with the microcode removed
787 the microcode
788 """
789 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
790 U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700791 self.assertEqual('nodtb with microcode' + pos_and_size +
792 ' somewhere in here', first)
793
Simon Glass160a7662017-05-27 07:38:26 -0600794 def _RunPackUbootSingleMicrocode(self):
Simon Glasse0ff8552016-11-25 20:15:53 -0700795 """Test that x86 microcode can be handled correctly
796
797 We expect to see the following in the image, in order:
798 u-boot-nodtb.bin with a microcode pointer inserted at the correct
799 place
800 u-boot.dtb with the microcode
801 an empty microcode region
802 """
803 # We need the libfdt library to run this test since only that allows
804 # finding the offset of a property. This is required by
805 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glasse0ff8552016-11-25 20:15:53 -0700806 data = self._DoReadFile('35_x86_single_ucode.dts', True)
807
808 second = data[len(U_BOOT_NODTB_DATA):]
809
810 fdt_len = self.GetFdtLen(second)
811 third = second[fdt_len:]
812 second = second[:fdt_len]
813
Simon Glass160a7662017-05-27 07:38:26 -0600814 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
815 self.assertIn(ucode_data, second)
816 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700817
Simon Glass160a7662017-05-27 07:38:26 -0600818 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600819 # expected offset and size
Simon Glass160a7662017-05-27 07:38:26 -0600820 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
821 len(ucode_data))
822 first = data[:len(U_BOOT_NODTB_DATA)]
823 self.assertEqual('nodtb with microcode' + pos_and_size +
824 ' somewhere in here', first)
Simon Glassc49deb82016-11-25 20:15:54 -0700825
Simon Glass75db0862016-11-25 20:15:55 -0700826 def testPackUbootSingleMicrocode(self):
827 """Test that x86 microcode can be handled correctly with fdt_normal.
828 """
Simon Glass160a7662017-05-27 07:38:26 -0600829 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700830
Simon Glassc49deb82016-11-25 20:15:54 -0700831 def testUBootImg(self):
832 """Test that u-boot.img can be put in a file"""
833 data = self._DoReadFile('36_u_boot_img.dts')
834 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glass75db0862016-11-25 20:15:55 -0700835
836 def testNoMicrocode(self):
837 """Test that a missing microcode region is detected"""
838 with self.assertRaises(ValueError) as e:
839 self._DoReadFile('37_x86_no_ucode.dts', True)
840 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
841 "node found in ", str(e.exception))
842
843 def testMicrocodeWithoutNode(self):
844 """Test that a missing u-boot-dtb-with-ucode node is detected"""
845 with self.assertRaises(ValueError) as e:
846 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
847 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
848 "microcode region u-boot-dtb-with-ucode", str(e.exception))
849
850 def testMicrocodeWithoutNode2(self):
851 """Test that a missing u-boot-ucode node is detected"""
852 with self.assertRaises(ValueError) as e:
853 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
854 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
855 "microcode region u-boot-ucode", str(e.exception))
856
857 def testMicrocodeWithoutPtrInElf(self):
858 """Test that a U-Boot binary without the microcode symbol is detected"""
859 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glass75db0862016-11-25 20:15:55 -0700860 try:
861 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
862 TestFunctional._MakeInputFile('u-boot', fd.read())
863
864 with self.assertRaises(ValueError) as e:
Simon Glass160a7662017-05-27 07:38:26 -0600865 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700866 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
867 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
868
869 finally:
870 # Put the original file back
871 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
872 TestFunctional._MakeInputFile('u-boot', fd.read())
873
874 def testMicrocodeNotInImage(self):
875 """Test that microcode must be placed within the image"""
876 with self.assertRaises(ValueError) as e:
877 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
878 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
879 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glass25ac0e62018-06-01 09:38:14 -0600880 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glass75db0862016-11-25 20:15:55 -0700881
882 def testWithoutMicrocode(self):
883 """Test that we can cope with an image without microcode (e.g. qemu)"""
884 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
885 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glass16b8d6b2018-07-06 10:27:42 -0600886 data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -0700887
888 # Now check the device tree has no microcode
889 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
890 second = data[len(U_BOOT_NODTB_DATA):]
891
892 fdt_len = self.GetFdtLen(second)
893 self.assertEqual(dtb, second[:fdt_len])
894
895 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
896 third = data[used_len:]
897 self.assertEqual(chr(0) * (0x200 - used_len), third)
898
899 def testUnknownPosSize(self):
900 """Test that microcode must be placed within the image"""
901 with self.assertRaises(ValueError) as e:
902 self._DoReadFile('41_unknown_pos_size.dts', True)
Simon Glass3ab95982018-08-01 15:22:37 -0600903 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glass75db0862016-11-25 20:15:55 -0700904 "entry 'invalid-entry'", str(e.exception))
Simon Glassda229092016-11-25 20:15:56 -0700905
906 def testPackFsp(self):
907 """Test that an image with a FSP binary can be created"""
908 data = self._DoReadFile('42_intel-fsp.dts')
909 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
910
911 def testPackCmc(self):
Bin Meng59ea8c22017-08-15 22:41:54 -0700912 """Test that an image with a CMC binary can be created"""
Simon Glassda229092016-11-25 20:15:56 -0700913 data = self._DoReadFile('43_intel-cmc.dts')
914 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Meng59ea8c22017-08-15 22:41:54 -0700915
916 def testPackVbt(self):
917 """Test that an image with a VBT binary can be created"""
918 data = self._DoReadFile('46_intel-vbt.dts')
919 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glass9fc60b42017-11-12 21:52:22 -0700920
Simon Glass56509842017-11-12 21:52:25 -0700921 def testSplBssPad(self):
922 """Test that we can pad SPL's BSS with zeros"""
Simon Glass6b187df2017-11-12 21:52:27 -0700923 # ELF file with a '__bss_size' symbol
924 with open(self.TestFile('bss_data')) as fd:
925 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass56509842017-11-12 21:52:25 -0700926 data = self._DoReadFile('47_spl_bss_pad.dts')
927 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
928
Simon Glassb50e5612017-11-13 18:54:54 -0700929 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
930 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
931 with self.assertRaises(ValueError) as e:
932 data = self._DoReadFile('47_spl_bss_pad.dts')
933 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
934 str(e.exception))
935
Simon Glass87722132017-11-12 21:52:26 -0700936 def testPackStart16Spl(self):
937 """Test that an image with an x86 start16 region can be created"""
938 data = self._DoReadFile('48_x86-start16-spl.dts')
939 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
940
Simon Glass736bb0a2018-07-06 10:27:17 -0600941 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
942 """Helper function for microcode tests
Simon Glass6b187df2017-11-12 21:52:27 -0700943
944 We expect to see the following in the image, in order:
945 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
946 correct place
947 u-boot.dtb with the microcode removed
948 the microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600949
950 Args:
951 dts: Device tree file to use for test
952 ucode_second: True if the microsecond entry is second instead of
953 third
Simon Glass6b187df2017-11-12 21:52:27 -0700954 """
955 # ELF file with a '_dt_ucode_base_size' symbol
956 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
957 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass736bb0a2018-07-06 10:27:17 -0600958 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
959 ucode_second=ucode_second)
Simon Glass6b187df2017-11-12 21:52:27 -0700960 self.assertEqual('splnodtb with microc' + pos_and_size +
961 'ter somewhere in here', first)
962
Simon Glass736bb0a2018-07-06 10:27:17 -0600963 def testPackUbootSplMicrocode(self):
964 """Test that x86 microcode can be handled correctly in SPL"""
965 self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
966
967 def testPackUbootSplMicrocodeReorder(self):
968 """Test that order doesn't matter for microcode entries
969
970 This is the same as testPackUbootSplMicrocode but when we process the
971 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
972 entry, so we reply on binman to try later.
973 """
974 self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
975 ucode_second=True)
976
Simon Glassca4f4ff2017-11-12 21:52:28 -0700977 def testPackMrc(self):
978 """Test that an image with an MRC binary can be created"""
979 data = self._DoReadFile('50_intel_mrc.dts')
980 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
981
Simon Glass47419ea2017-11-13 18:54:55 -0700982 def testSplDtb(self):
983 """Test that an image with spl/u-boot-spl.dtb can be created"""
984 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
985 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
986
Simon Glass4e6fdbe2017-11-13 18:54:56 -0700987 def testSplNoDtb(self):
988 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
989 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
990 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
991
Simon Glass19790632017-11-13 18:55:01 -0700992 def testSymbols(self):
993 """Test binman can assign symbols embedded in U-Boot"""
994 elf_fname = self.TestFile('u_boot_binman_syms')
995 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
996 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glass3ab95982018-08-01 15:22:37 -0600997 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass19790632017-11-13 18:55:01 -0700998
999 with open(self.TestFile('u_boot_binman_syms')) as fd:
1000 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
1001 data = self._DoReadFile('53_symbols.dts')
1002 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
1003 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
1004 U_BOOT_DATA +
1005 sym_values + U_BOOT_SPL_DATA[16:])
1006 self.assertEqual(expected, data)
1007
Simon Glassdd57c132018-06-01 09:38:11 -06001008 def testPackUnitAddress(self):
1009 """Test that we support multiple binaries with the same name"""
1010 data = self._DoReadFile('54_unit_address.dts')
1011 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1012
Simon Glass18546952018-06-01 09:38:16 -06001013 def testSections(self):
1014 """Basic test of sections"""
1015 data = self._DoReadFile('55_sections.dts')
Simon Glass8122f392018-07-17 13:25:28 -06001016 expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
1017 U_BOOT_DATA + '&' * 4)
Simon Glass18546952018-06-01 09:38:16 -06001018 self.assertEqual(expected, data)
Simon Glass9fc60b42017-11-12 21:52:22 -07001019
Simon Glass3b0c3822018-06-01 09:38:20 -06001020 def testMap(self):
1021 """Tests outputting a map of the images"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001022 _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
Simon Glass3ab95982018-08-01 15:22:37 -06001023 self.assertEqual(''' Offset Size Name
Simon Glass8122f392018-07-17 13:25:28 -0600102400000000 00000028 main-section
1025 00000000 00000010 section@0
1026 00000000 00000004 u-boot
1027 00000010 00000010 section@1
1028 00000000 00000004 u-boot
1029 00000020 00000004 section@2
1030 00000000 00000004 u-boot
Simon Glass3b0c3822018-06-01 09:38:20 -06001031''', map_data)
1032
Simon Glassc8d48ef2018-06-01 09:38:21 -06001033 def testNamePrefix(self):
1034 """Tests that name prefixes are used"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001035 _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
Simon Glass3ab95982018-08-01 15:22:37 -06001036 self.assertEqual(''' Offset Size Name
Simon Glass8122f392018-07-17 13:25:28 -0600103700000000 00000028 main-section
1038 00000000 00000010 section@0
1039 00000000 00000004 ro-u-boot
1040 00000010 00000010 section@1
1041 00000000 00000004 rw-u-boot
Simon Glassc8d48ef2018-06-01 09:38:21 -06001042''', map_data)
1043
Simon Glass736bb0a2018-07-06 10:27:17 -06001044 def testUnknownContents(self):
1045 """Test that obtaining the contents works as expected"""
1046 with self.assertRaises(ValueError) as e:
1047 self._DoReadFile('57_unknown_contents.dts', True)
1048 self.assertIn("Section '/binman': Internal error: Could not complete "
1049 "processing of contents: remaining [<_testing.Entry__testing ",
1050 str(e.exception))
1051
Simon Glass5c890232018-07-06 10:27:19 -06001052 def testBadChangeSize(self):
1053 """Test that trying to change the size of an entry fails"""
1054 with self.assertRaises(ValueError) as e:
1055 self._DoReadFile('59_change_size.dts', True)
1056 self.assertIn("Node '/binman/_testing': Cannot update entry size from "
1057 '2 to 1', str(e.exception))
1058
Simon Glass16b8d6b2018-07-06 10:27:42 -06001059 def testUpdateFdt(self):
Simon Glass3ab95982018-08-01 15:22:37 -06001060 """Test that we can update the device tree with offset/size info"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001061 _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
1062 update_dtb=True)
Simon Glassdbf6be92018-08-01 15:22:42 -06001063 props = self._GetPropTree(out_dtb_fname, ['offset', 'size',
1064 'image-pos'])
Simon Glass16b8d6b2018-07-06 10:27:42 -06001065 with open('/tmp/x.dtb', 'wb') as outf:
1066 with open(out_dtb_fname) as inf:
1067 outf.write(inf.read())
1068 self.assertEqual({
Simon Glassdbf6be92018-08-01 15:22:42 -06001069 'image-pos': 0,
Simon Glass8122f392018-07-17 13:25:28 -06001070 'offset': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001071 '_testing:offset': 32,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001072 '_testing:size': 1,
Simon Glassdbf6be92018-08-01 15:22:42 -06001073 '_testing:image-pos': 32,
Simon Glass3ab95982018-08-01 15:22:37 -06001074 'section@0/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001075 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001076 'section@0/u-boot:image-pos': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001077 'section@0:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001078 'section@0:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001079 'section@0:image-pos': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001080
Simon Glass3ab95982018-08-01 15:22:37 -06001081 'section@1/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001082 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001083 'section@1/u-boot:image-pos': 16,
Simon Glass3ab95982018-08-01 15:22:37 -06001084 'section@1:offset': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001085 'section@1:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001086 'section@1:image-pos': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001087 'size': 40
1088 }, props)
1089
1090 def testUpdateFdtBad(self):
1091 """Test that we detect when ProcessFdt never completes"""
1092 with self.assertRaises(ValueError) as e:
1093 self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
1094 self.assertIn('Could not complete processing of Fdt: remaining '
1095 '[<_testing.Entry__testing', str(e.exception))
Simon Glass5c890232018-07-06 10:27:19 -06001096
Simon Glass53af22a2018-07-17 13:25:32 -06001097 def testEntryArgs(self):
1098 """Test passing arguments to entries from the command line"""
1099 entry_args = {
1100 'test-str-arg': 'test1',
1101 'test-int-arg': '456',
1102 }
1103 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1104 self.assertIn('image', control.images)
1105 entry = control.images['image'].GetEntries()['_testing']
1106 self.assertEqual('test0', entry.test_str_fdt)
1107 self.assertEqual('test1', entry.test_str_arg)
1108 self.assertEqual(123, entry.test_int_fdt)
1109 self.assertEqual(456, entry.test_int_arg)
1110
1111 def testEntryArgsMissing(self):
1112 """Test missing arguments and properties"""
1113 entry_args = {
1114 'test-int-arg': '456',
1115 }
1116 self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args)
1117 entry = control.images['image'].GetEntries()['_testing']
1118 self.assertEqual('test0', entry.test_str_fdt)
1119 self.assertEqual(None, entry.test_str_arg)
1120 self.assertEqual(None, entry.test_int_fdt)
1121 self.assertEqual(456, entry.test_int_arg)
1122
1123 def testEntryArgsRequired(self):
1124 """Test missing arguments and properties"""
1125 entry_args = {
1126 'test-int-arg': '456',
1127 }
1128 with self.assertRaises(ValueError) as e:
1129 self._DoReadFileDtb('64_entry_args_required.dts')
1130 self.assertIn("Node '/binman/_testing': Missing required "
1131 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1132 str(e.exception))
1133
1134 def testEntryArgsInvalidFormat(self):
1135 """Test that an invalid entry-argument format is detected"""
1136 args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value']
1137 with self.assertRaises(ValueError) as e:
1138 self._DoBinman(*args)
1139 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1140
1141 def testEntryArgsInvalidInteger(self):
1142 """Test that an invalid entry-argument integer is detected"""
1143 entry_args = {
1144 'test-int-arg': 'abc',
1145 }
1146 with self.assertRaises(ValueError) as e:
1147 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1148 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1149 "'test-int-arg' (value 'abc') to integer",
1150 str(e.exception))
1151
1152 def testEntryArgsInvalidDatatype(self):
1153 """Test that an invalid entry-argument datatype is detected
1154
1155 This test could be written in entry_test.py except that it needs
1156 access to control.entry_args, which seems more than that module should
1157 be able to see.
1158 """
1159 entry_args = {
1160 'test-bad-datatype-arg': '12',
1161 }
1162 with self.assertRaises(ValueError) as e:
1163 self._DoReadFileDtb('65_entry_args_unknown_datatype.dts',
1164 entry_args=entry_args)
1165 self.assertIn('GetArg() internal error: Unknown data type ',
1166 str(e.exception))
1167
Simon Glassbb748372018-07-17 13:25:33 -06001168 def testText(self):
1169 """Test for a text entry type"""
1170 entry_args = {
1171 'test-id': TEXT_DATA,
1172 'test-id2': TEXT_DATA2,
1173 'test-id3': TEXT_DATA3,
1174 }
1175 data, _, _, _ = self._DoReadFileDtb('66_text.dts',
1176 entry_args=entry_args)
1177 expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 +
1178 TEXT_DATA3 + 'some text')
1179 self.assertEqual(expected, data)
1180
Simon Glassfd8d1f72018-07-17 13:25:36 -06001181 def testEntryDocs(self):
1182 """Test for creation of entry documentation"""
1183 with test_util.capture_sys_output() as (stdout, stderr):
1184 control.WriteEntryDocs(binman.GetEntryModules())
1185 self.assertTrue(len(stdout.getvalue()) > 0)
1186
1187 def testEntryDocsMissing(self):
1188 """Test handling of missing entry documentation"""
1189 with self.assertRaises(ValueError) as e:
1190 with test_util.capture_sys_output() as (stdout, stderr):
1191 control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
1192 self.assertIn('Documentation is missing for modules: u_boot',
1193 str(e.exception))
1194
Simon Glass53af22a2018-07-17 13:25:32 -06001195
Simon Glass9fc60b42017-11-12 21:52:22 -07001196if __name__ == "__main__":
1197 unittest.main()