blob: f15b215c60891e9f10763a635067a9eb04579e6f [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 Glass11e36cc2018-07-17 13:25:38 -060024import fmap_util
Simon Glassfd8d1f72018-07-17 13:25:36 -060025import test_util
Simon Glass4f443042016-11-25 20:15:52 -070026import tools
27import tout
28
29# Contents of test files, corresponding to different entry types
Simon Glass6b187df2017-11-12 21:52:27 -070030U_BOOT_DATA = '1234'
31U_BOOT_IMG_DATA = 'img'
Simon Glassf6898902017-11-13 18:54:59 -070032U_BOOT_SPL_DATA = '56780123456789abcde'
Simon Glass6b187df2017-11-12 21:52:27 -070033BLOB_DATA = '89'
34ME_DATA = '0abcd'
35VGA_DATA = 'vga'
36U_BOOT_DTB_DATA = 'udtb'
Simon Glass47419ea2017-11-13 18:54:55 -070037U_BOOT_SPL_DTB_DATA = 'spldtb'
Simon Glass6b187df2017-11-12 21:52:27 -070038X86_START16_DATA = 'start16'
39X86_START16_SPL_DATA = 'start16spl'
40U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
41U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
42FSP_DATA = 'fsp'
43CMC_DATA = 'cmc'
44VBT_DATA = 'vbt'
Simon Glassca4f4ff2017-11-12 21:52:28 -070045MRC_DATA = 'mrc'
Simon Glassbb748372018-07-17 13:25:33 -060046TEXT_DATA = 'text'
47TEXT_DATA2 = 'text2'
48TEXT_DATA3 = 'text3'
Simon Glassec127af2018-07-17 13:25:39 -060049CROS_EC_RW_DATA = 'ecrw'
Simon Glass0ef87aa2018-07-17 13:25:44 -060050GBB_DATA = 'gbbd'
51BMPBLK_DATA = 'bmp'
Simon Glassec127af2018-07-17 13:25:39 -060052
Simon Glass4f443042016-11-25 20:15:52 -070053
54class TestFunctional(unittest.TestCase):
55 """Functional tests for binman
56
57 Most of these use a sample .dts file to build an image and then check
58 that it looks correct. The sample files are in the test/ subdirectory
59 and are numbered.
60
61 For each entry type a very small test file is created using fixed
62 string contents. This makes it easy to test that things look right, and
63 debug problems.
64
65 In some cases a 'real' file must be used - these are also supplied in
66 the test/ diurectory.
67 """
68 @classmethod
69 def setUpClass(self):
Simon Glass4d5994f2017-11-12 21:52:20 -070070 global entry
71 import entry
72
Simon Glass4f443042016-11-25 20:15:52 -070073 # Handle the case where argv[0] is 'python'
74 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
75 self._binman_pathname = os.path.join(self._binman_dir, 'binman')
76
77 # Create a temporary directory for input files
78 self._indir = tempfile.mkdtemp(prefix='binmant.')
79
80 # Create some test files
81 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
82 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
83 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
84 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -070085 TestFunctional._MakeInputFile('me.bin', ME_DATA)
86 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070087 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
Simon Glass47419ea2017-11-13 18:54:55 -070088 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -070089 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Simon Glass87722132017-11-12 21:52:26 -070090 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
91 X86_START16_SPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070092 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass6b187df2017-11-12 21:52:27 -070093 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
94 U_BOOT_SPL_NODTB_DATA)
Simon Glassda229092016-11-25 20:15:56 -070095 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
96 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Meng59ea8c22017-08-15 22:41:54 -070097 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassca4f4ff2017-11-12 21:52:28 -070098 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glassec127af2018-07-17 13:25:39 -060099 TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
Simon Glass0ef87aa2018-07-17 13:25:44 -0600100 TestFunctional._MakeInputDir('devkeys')
101 TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700102 self._output_setup = False
103
Simon Glasse0ff8552016-11-25 20:15:53 -0700104 # ELF file with a '_dt_ucode_base_size' symbol
105 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
106 TestFunctional._MakeInputFile('u-boot', fd.read())
107
108 # Intel flash descriptor file
109 with open(self.TestFile('descriptor.bin')) as fd:
110 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
111
Simon Glass4f443042016-11-25 20:15:52 -0700112 @classmethod
113 def tearDownClass(self):
114 """Remove the temporary input directory and its contents"""
115 if self._indir:
116 shutil.rmtree(self._indir)
117 self._indir = None
118
119 def setUp(self):
120 # Enable this to turn on debugging output
121 # tout.Init(tout.DEBUG)
122 command.test_result = None
123
124 def tearDown(self):
125 """Remove the temporary output directory"""
126 tools._FinaliseForTest()
127
128 def _RunBinman(self, *args, **kwargs):
129 """Run binman using the command line
130
131 Args:
132 Arguments to pass, as a list of strings
133 kwargs: Arguments to pass to Command.RunPipe()
134 """
135 result = command.RunPipe([[self._binman_pathname] + list(args)],
136 capture=True, capture_stderr=True, raise_on_error=False)
137 if result.return_code and kwargs.get('raise_on_error', True):
138 raise Exception("Error running '%s': %s" % (' '.join(args),
139 result.stdout + result.stderr))
140 return result
141
142 def _DoBinman(self, *args):
143 """Run binman using directly (in the same process)
144
145 Args:
146 Arguments to pass, as a list of strings
147 Returns:
148 Return value (0 for success)
149 """
Simon Glass7fe91732017-11-13 18:55:00 -0700150 args = list(args)
151 if '-D' in sys.argv:
152 args = args + ['-D']
153 (options, args) = cmdline.ParseArgs(args)
Simon Glass4f443042016-11-25 20:15:52 -0700154 options.pager = 'binman-invalid-pager'
155 options.build_dir = self._indir
156
157 # For testing, you can force an increase in verbosity here
158 # options.verbosity = tout.DEBUG
159 return control.Binman(options, args)
160
Simon Glass53af22a2018-07-17 13:25:32 -0600161 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
162 entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700163 """Run binman with a given test file
164
165 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600166 fname: Device-tree source filename to use (e.g. 05_simple.dts)
167 debug: True to enable debugging output
Simon Glass3b0c3822018-06-01 09:38:20 -0600168 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600169 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600170 tree before packing it into the image
Simon Glass4f443042016-11-25 20:15:52 -0700171 """
Simon Glass7fe91732017-11-13 18:55:00 -0700172 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
173 if debug:
174 args.append('-D')
Simon Glass3b0c3822018-06-01 09:38:20 -0600175 if map:
176 args.append('-m')
Simon Glass16b8d6b2018-07-06 10:27:42 -0600177 if update_dtb:
178 args.append('-up')
Simon Glass53af22a2018-07-17 13:25:32 -0600179 if entry_args:
180 for arg, value in entry_args.iteritems():
181 args.append('-a%s=%s' % (arg, value))
Simon Glass7fe91732017-11-13 18:55:00 -0700182 return self._DoBinman(*args)
Simon Glass4f443042016-11-25 20:15:52 -0700183
184 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glasse0ff8552016-11-25 20:15:53 -0700185 """Set up a new test device-tree file
186
187 The given file is compiled and set up as the device tree to be used
188 for ths test.
189
190 Args:
191 fname: Filename of .dts file to read
Simon Glass7ae5f312018-06-01 09:38:19 -0600192 outfile: Output filename for compiled device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700193
194 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600195 Contents of device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700196 """
Simon Glass4f443042016-11-25 20:15:52 -0700197 if not self._output_setup:
198 tools.PrepareOutputDir(self._indir, True)
199 self._output_setup = True
200 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
201 with open(dtb) as fd:
202 data = fd.read()
203 TestFunctional._MakeInputFile(outfile, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700204 return data
Simon Glass4f443042016-11-25 20:15:52 -0700205
Simon Glass16b8d6b2018-07-06 10:27:42 -0600206 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass53af22a2018-07-17 13:25:32 -0600207 update_dtb=False, entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700208 """Run binman and return the resulting image
209
210 This runs binman with a given test file and then reads the resulting
211 output file. It is a shortcut function since most tests need to do
212 these steps.
213
214 Raises an assertion failure if binman returns a non-zero exit code.
215
216 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600217 fname: Device-tree source filename to use (e.g. 05_simple.dts)
Simon Glass4f443042016-11-25 20:15:52 -0700218 use_real_dtb: True to use the test file as the contents of
219 the u-boot-dtb entry. Normally this is not needed and the
220 test contents (the U_BOOT_DTB_DATA string) can be used.
221 But in some test we need the real contents.
Simon Glass3b0c3822018-06-01 09:38:20 -0600222 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600223 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600224 tree before packing it into the image
Simon Glasse0ff8552016-11-25 20:15:53 -0700225
226 Returns:
227 Tuple:
228 Resulting image contents
229 Device tree contents
Simon Glass3b0c3822018-06-01 09:38:20 -0600230 Map data showing contents of image (or None if none)
Simon Glassea6922e2018-07-17 13:25:27 -0600231 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass4f443042016-11-25 20:15:52 -0700232 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700233 dtb_data = None
Simon Glass4f443042016-11-25 20:15:52 -0700234 # Use the compiled test file as the u-boot-dtb input
235 if use_real_dtb:
Simon Glasse0ff8552016-11-25 20:15:53 -0700236 dtb_data = self._SetupDtb(fname)
Simon Glass4f443042016-11-25 20:15:52 -0700237
238 try:
Simon Glass53af22a2018-07-17 13:25:32 -0600239 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
240 entry_args=entry_args)
Simon Glass4f443042016-11-25 20:15:52 -0700241 self.assertEqual(0, retcode)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600242 out_dtb_fname = control.GetFdtPath('u-boot.dtb')
Simon Glass4f443042016-11-25 20:15:52 -0700243
244 # Find the (only) image, read it and return its contents
245 image = control.images['image']
Simon Glass16b8d6b2018-07-06 10:27:42 -0600246 image_fname = tools.GetOutputFilename('image.bin')
247 self.assertTrue(os.path.exists(image_fname))
Simon Glass3b0c3822018-06-01 09:38:20 -0600248 if map:
249 map_fname = tools.GetOutputFilename('image.map')
250 with open(map_fname) as fd:
251 map_data = fd.read()
252 else:
253 map_data = None
Simon Glass16b8d6b2018-07-06 10:27:42 -0600254 with open(image_fname) as fd:
255 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass4f443042016-11-25 20:15:52 -0700256 finally:
257 # Put the test file back
258 if use_real_dtb:
259 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
260
Simon Glasse0ff8552016-11-25 20:15:53 -0700261 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass7ae5f312018-06-01 09:38:19 -0600262 """Helper function which discards the device-tree binary
263
264 Args:
265 fname: Device-tree source filename to use (e.g. 05_simple.dts)
266 use_real_dtb: True to use the test file as the contents of
267 the u-boot-dtb entry. Normally this is not needed and the
268 test contents (the U_BOOT_DTB_DATA string) can be used.
269 But in some test we need the real contents.
Simon Glassea6922e2018-07-17 13:25:27 -0600270
271 Returns:
272 Resulting image contents
Simon Glass7ae5f312018-06-01 09:38:19 -0600273 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700274 return self._DoReadFileDtb(fname, use_real_dtb)[0]
275
Simon Glass4f443042016-11-25 20:15:52 -0700276 @classmethod
277 def _MakeInputFile(self, fname, contents):
278 """Create a new test input file, creating directories as needed
279
280 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600281 fname: Filename to create
Simon Glass4f443042016-11-25 20:15:52 -0700282 contents: File contents to write in to the file
283 Returns:
284 Full pathname of file created
285 """
286 pathname = os.path.join(self._indir, fname)
287 dirname = os.path.dirname(pathname)
288 if dirname and not os.path.exists(dirname):
289 os.makedirs(dirname)
290 with open(pathname, 'wb') as fd:
291 fd.write(contents)
292 return pathname
293
294 @classmethod
Simon Glass0ef87aa2018-07-17 13:25:44 -0600295 def _MakeInputDir(self, dirname):
296 """Create a new test input directory, creating directories as needed
297
298 Args:
299 dirname: Directory name to create
300
301 Returns:
302 Full pathname of directory created
303 """
304 pathname = os.path.join(self._indir, dirname)
305 if not os.path.exists(pathname):
306 os.makedirs(pathname)
307 return pathname
308
309 @classmethod
Simon Glass4f443042016-11-25 20:15:52 -0700310 def TestFile(self, fname):
311 return os.path.join(self._binman_dir, 'test', fname)
312
313 def AssertInList(self, grep_list, target):
314 """Assert that at least one of a list of things is in a target
315
316 Args:
317 grep_list: List of strings to check
318 target: Target string
319 """
320 for grep in grep_list:
321 if grep in target:
322 return
323 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
324
325 def CheckNoGaps(self, entries):
326 """Check that all entries fit together without gaps
327
328 Args:
329 entries: List of entries to check
330 """
Simon Glass3ab95982018-08-01 15:22:37 -0600331 offset = 0
Simon Glass4f443042016-11-25 20:15:52 -0700332 for entry in entries.values():
Simon Glass3ab95982018-08-01 15:22:37 -0600333 self.assertEqual(offset, entry.offset)
334 offset += entry.size
Simon Glass4f443042016-11-25 20:15:52 -0700335
Simon Glasse0ff8552016-11-25 20:15:53 -0700336 def GetFdtLen(self, dtb):
Simon Glass7ae5f312018-06-01 09:38:19 -0600337 """Get the totalsize field from a device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700338
339 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600340 dtb: Device-tree binary contents
Simon Glasse0ff8552016-11-25 20:15:53 -0700341
342 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600343 Total size of device-tree binary, from the header
Simon Glasse0ff8552016-11-25 20:15:53 -0700344 """
345 return struct.unpack('>L', dtb[4:8])[0]
346
Simon Glass16b8d6b2018-07-06 10:27:42 -0600347 def _GetPropTree(self, dtb_data, node_names):
348 def AddNode(node, path):
349 if node.name != '/':
350 path += '/' + node.name
Simon Glass16b8d6b2018-07-06 10:27:42 -0600351 for subnode in node.subnodes:
352 for prop in subnode.props.values():
353 if prop.name in node_names:
354 prop_path = path + '/' + subnode.name + ':' + prop.name
355 tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
356 prop.value)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600357 AddNode(subnode, path)
358
359 tree = {}
360 dtb = fdt.Fdt(dtb_data)
361 dtb.Scan()
362 AddNode(dtb.GetRoot(), '')
363 return tree
364
Simon Glass4f443042016-11-25 20:15:52 -0700365 def testRun(self):
366 """Test a basic run with valid args"""
367 result = self._RunBinman('-h')
368
369 def testFullHelp(self):
370 """Test that the full help is displayed with -H"""
371 result = self._RunBinman('-H')
372 help_file = os.path.join(self._binman_dir, 'README')
Tom Rini3759df02018-01-16 15:29:50 -0500373 # Remove possible extraneous strings
374 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
375 gothelp = result.stdout.replace(extra, '')
376 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass4f443042016-11-25 20:15:52 -0700377 self.assertEqual(0, len(result.stderr))
378 self.assertEqual(0, result.return_code)
379
380 def testFullHelpInternal(self):
381 """Test that the full help is displayed with -H"""
382 try:
383 command.test_result = command.CommandResult()
384 result = self._DoBinman('-H')
385 help_file = os.path.join(self._binman_dir, 'README')
386 finally:
387 command.test_result = None
388
389 def testHelp(self):
390 """Test that the basic help is displayed with -h"""
391 result = self._RunBinman('-h')
392 self.assertTrue(len(result.stdout) > 200)
393 self.assertEqual(0, len(result.stderr))
394 self.assertEqual(0, result.return_code)
395
Simon Glass4f443042016-11-25 20:15:52 -0700396 def testBoard(self):
397 """Test that we can run it with a specific board"""
398 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
399 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
400 result = self._DoBinman('-b', 'sandbox')
401 self.assertEqual(0, result)
402
403 def testNeedBoard(self):
404 """Test that we get an error when no board ius supplied"""
405 with self.assertRaises(ValueError) as e:
406 result = self._DoBinman()
407 self.assertIn("Must provide a board to process (use -b <board>)",
408 str(e.exception))
409
410 def testMissingDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600411 """Test that an invalid device-tree file generates an error"""
Simon Glass4f443042016-11-25 20:15:52 -0700412 with self.assertRaises(Exception) as e:
413 self._RunBinman('-d', 'missing_file')
414 # We get one error from libfdt, and a different one from fdtget.
415 self.AssertInList(["Couldn't open blob from 'missing_file'",
416 'No such file or directory'], str(e.exception))
417
418 def testBrokenDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600419 """Test that an invalid device-tree source file generates an error
Simon Glass4f443042016-11-25 20:15:52 -0700420
421 Since this is a source file it should be compiled and the error
422 will come from the device-tree compiler (dtc).
423 """
424 with self.assertRaises(Exception) as e:
425 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
426 self.assertIn("FATAL ERROR: Unable to parse input tree",
427 str(e.exception))
428
429 def testMissingNode(self):
430 """Test that a device tree without a 'binman' node generates an error"""
431 with self.assertRaises(Exception) as e:
432 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
433 self.assertIn("does not have a 'binman' node", str(e.exception))
434
435 def testEmpty(self):
436 """Test that an empty binman node works OK (i.e. does nothing)"""
437 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
438 self.assertEqual(0, len(result.stderr))
439 self.assertEqual(0, result.return_code)
440
441 def testInvalidEntry(self):
442 """Test that an invalid entry is flagged"""
443 with self.assertRaises(Exception) as e:
444 result = self._RunBinman('-d',
445 self.TestFile('04_invalid_entry.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700446 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
447 "'/binman/not-a-valid-type'", str(e.exception))
448
449 def testSimple(self):
450 """Test a simple binman with a single file"""
451 data = self._DoReadFile('05_simple.dts')
452 self.assertEqual(U_BOOT_DATA, data)
453
Simon Glass7fe91732017-11-13 18:55:00 -0700454 def testSimpleDebug(self):
455 """Test a simple binman run with debugging enabled"""
456 data = self._DoTestFile('05_simple.dts', debug=True)
457
Simon Glass4f443042016-11-25 20:15:52 -0700458 def testDual(self):
459 """Test that we can handle creating two images
460
461 This also tests image padding.
462 """
463 retcode = self._DoTestFile('06_dual_image.dts')
464 self.assertEqual(0, retcode)
465
466 image = control.images['image1']
467 self.assertEqual(len(U_BOOT_DATA), image._size)
468 fname = tools.GetOutputFilename('image1.bin')
469 self.assertTrue(os.path.exists(fname))
470 with open(fname) as fd:
471 data = fd.read()
472 self.assertEqual(U_BOOT_DATA, data)
473
474 image = control.images['image2']
475 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
476 fname = tools.GetOutputFilename('image2.bin')
477 self.assertTrue(os.path.exists(fname))
478 with open(fname) as fd:
479 data = fd.read()
480 self.assertEqual(U_BOOT_DATA, data[3:7])
481 self.assertEqual(chr(0) * 3, data[:3])
482 self.assertEqual(chr(0) * 5, data[7:])
483
484 def testBadAlign(self):
485 """Test that an invalid alignment value is detected"""
486 with self.assertRaises(ValueError) as e:
487 self._DoTestFile('07_bad_align.dts')
488 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
489 "of two", str(e.exception))
490
491 def testPackSimple(self):
492 """Test that packing works as expected"""
493 retcode = self._DoTestFile('08_pack.dts')
494 self.assertEqual(0, retcode)
495 self.assertIn('image', control.images)
496 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600497 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700498 self.assertEqual(5, len(entries))
499
500 # First u-boot
501 self.assertIn('u-boot', entries)
502 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600503 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700504 self.assertEqual(len(U_BOOT_DATA), entry.size)
505
506 # Second u-boot, aligned to 16-byte boundary
507 self.assertIn('u-boot-align', entries)
508 entry = entries['u-boot-align']
Simon Glass3ab95982018-08-01 15:22:37 -0600509 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700510 self.assertEqual(len(U_BOOT_DATA), entry.size)
511
512 # Third u-boot, size 23 bytes
513 self.assertIn('u-boot-size', entries)
514 entry = entries['u-boot-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600515 self.assertEqual(20, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700516 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
517 self.assertEqual(23, entry.size)
518
519 # Fourth u-boot, placed immediate after the above
520 self.assertIn('u-boot-next', entries)
521 entry = entries['u-boot-next']
Simon Glass3ab95982018-08-01 15:22:37 -0600522 self.assertEqual(43, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700523 self.assertEqual(len(U_BOOT_DATA), entry.size)
524
Simon Glass3ab95982018-08-01 15:22:37 -0600525 # Fifth u-boot, placed at a fixed offset
Simon Glass4f443042016-11-25 20:15:52 -0700526 self.assertIn('u-boot-fixed', entries)
527 entry = entries['u-boot-fixed']
Simon Glass3ab95982018-08-01 15:22:37 -0600528 self.assertEqual(61, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700529 self.assertEqual(len(U_BOOT_DATA), entry.size)
530
531 self.assertEqual(65, image._size)
532
533 def testPackExtra(self):
534 """Test that extra packing feature works as expected"""
535 retcode = self._DoTestFile('09_pack_extra.dts')
536
537 self.assertEqual(0, retcode)
538 self.assertIn('image', control.images)
539 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600540 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700541 self.assertEqual(5, len(entries))
542
543 # First u-boot with padding before and after
544 self.assertIn('u-boot', entries)
545 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600546 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700547 self.assertEqual(3, entry.pad_before)
548 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
549
550 # Second u-boot has an aligned size, but it has no effect
551 self.assertIn('u-boot-align-size-nop', entries)
552 entry = entries['u-boot-align-size-nop']
Simon Glass3ab95982018-08-01 15:22:37 -0600553 self.assertEqual(12, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700554 self.assertEqual(4, entry.size)
555
556 # Third u-boot has an aligned size too
557 self.assertIn('u-boot-align-size', entries)
558 entry = entries['u-boot-align-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600559 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700560 self.assertEqual(32, entry.size)
561
562 # Fourth u-boot has an aligned end
563 self.assertIn('u-boot-align-end', entries)
564 entry = entries['u-boot-align-end']
Simon Glass3ab95982018-08-01 15:22:37 -0600565 self.assertEqual(48, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700566 self.assertEqual(16, entry.size)
567
568 # Fifth u-boot immediately afterwards
569 self.assertIn('u-boot-align-both', entries)
570 entry = entries['u-boot-align-both']
Simon Glass3ab95982018-08-01 15:22:37 -0600571 self.assertEqual(64, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700572 self.assertEqual(64, entry.size)
573
574 self.CheckNoGaps(entries)
575 self.assertEqual(128, image._size)
576
577 def testPackAlignPowerOf2(self):
578 """Test that invalid entry alignment is detected"""
579 with self.assertRaises(ValueError) as e:
580 self._DoTestFile('10_pack_align_power2.dts')
581 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
582 "of two", str(e.exception))
583
584 def testPackAlignSizePowerOf2(self):
585 """Test that invalid entry size alignment is detected"""
586 with self.assertRaises(ValueError) as e:
587 self._DoTestFile('11_pack_align_size_power2.dts')
588 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
589 "power of two", str(e.exception))
590
591 def testPackInvalidAlign(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600592 """Test detection of an offset that does not match its alignment"""
Simon Glass4f443042016-11-25 20:15:52 -0700593 with self.assertRaises(ValueError) as e:
594 self._DoTestFile('12_pack_inv_align.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600595 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700596 "align 0x4 (4)", str(e.exception))
597
598 def testPackInvalidSizeAlign(self):
599 """Test that invalid entry size alignment is detected"""
600 with self.assertRaises(ValueError) as e:
601 self._DoTestFile('13_pack_inv_size_align.dts')
602 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
603 "align-size 0x4 (4)", str(e.exception))
604
605 def testPackOverlap(self):
606 """Test that overlapping regions are detected"""
607 with self.assertRaises(ValueError) as e:
608 self._DoTestFile('14_pack_overlap.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600609 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700610 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
611 str(e.exception))
612
613 def testPackEntryOverflow(self):
614 """Test that entries that overflow their size are detected"""
615 with self.assertRaises(ValueError) as e:
616 self._DoTestFile('15_pack_overflow.dts')
617 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
618 "but entry size is 0x3 (3)", str(e.exception))
619
620 def testPackImageOverflow(self):
621 """Test that entries which overflow the image size are detected"""
622 with self.assertRaises(ValueError) as e:
623 self._DoTestFile('16_pack_image_overflow.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600624 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass4f443042016-11-25 20:15:52 -0700625 "size 0x3 (3)", str(e.exception))
626
627 def testPackImageSize(self):
628 """Test that the image size can be set"""
629 retcode = self._DoTestFile('17_pack_image_size.dts')
630 self.assertEqual(0, retcode)
631 self.assertIn('image', control.images)
632 image = control.images['image']
633 self.assertEqual(7, image._size)
634
635 def testPackImageSizeAlign(self):
636 """Test that image size alignemnt works as expected"""
637 retcode = self._DoTestFile('18_pack_image_align.dts')
638 self.assertEqual(0, retcode)
639 self.assertIn('image', control.images)
640 image = control.images['image']
641 self.assertEqual(16, image._size)
642
643 def testPackInvalidImageAlign(self):
644 """Test that invalid image alignment is detected"""
645 with self.assertRaises(ValueError) as e:
646 self._DoTestFile('19_pack_inv_image_align.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600647 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700648 "align-size 0x8 (8)", str(e.exception))
649
650 def testPackAlignPowerOf2(self):
651 """Test that invalid image alignment is detected"""
652 with self.assertRaises(ValueError) as e:
653 self._DoTestFile('20_pack_inv_image_align_power2.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600654 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass4f443042016-11-25 20:15:52 -0700655 "two", str(e.exception))
656
657 def testImagePadByte(self):
658 """Test that the image pad byte can be specified"""
Simon Glass19790632017-11-13 18:55:01 -0700659 with open(self.TestFile('bss_data')) as fd:
660 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass4f443042016-11-25 20:15:52 -0700661 data = self._DoReadFile('21_image_pad.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700662 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700663
664 def testImageName(self):
665 """Test that image files can be named"""
666 retcode = self._DoTestFile('22_image_name.dts')
667 self.assertEqual(0, retcode)
668 image = control.images['image1']
669 fname = tools.GetOutputFilename('test-name')
670 self.assertTrue(os.path.exists(fname))
671
672 image = control.images['image2']
673 fname = tools.GetOutputFilename('test-name.xx')
674 self.assertTrue(os.path.exists(fname))
675
676 def testBlobFilename(self):
677 """Test that generic blobs can be provided by filename"""
678 data = self._DoReadFile('23_blob.dts')
679 self.assertEqual(BLOB_DATA, data)
680
681 def testPackSorted(self):
682 """Test that entries can be sorted"""
683 data = self._DoReadFile('24_sorted.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700684 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
Simon Glass4f443042016-11-25 20:15:52 -0700685 U_BOOT_DATA, data)
686
Simon Glass3ab95982018-08-01 15:22:37 -0600687 def testPackZeroOffset(self):
688 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass4f443042016-11-25 20:15:52 -0700689 with self.assertRaises(ValueError) as e:
690 self._DoTestFile('25_pack_zero_size.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600691 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700692 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
693 str(e.exception))
694
695 def testPackUbootDtb(self):
696 """Test that a device tree can be added to U-Boot"""
697 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
698 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700699
700 def testPackX86RomNoSize(self):
701 """Test that the end-at-4gb property requires a size property"""
702 with self.assertRaises(ValueError) as e:
703 self._DoTestFile('27_pack_4gb_no_size.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600704 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glasse0ff8552016-11-25 20:15:53 -0700705 "using end-at-4gb", str(e.exception))
706
707 def testPackX86RomOutside(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600708 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glasse0ff8552016-11-25 20:15:53 -0700709 with self.assertRaises(ValueError) as e:
710 self._DoTestFile('28_pack_4gb_outside.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600711 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glass8f1da502018-06-01 09:38:12 -0600712 "the section starting at 0xffffffe0 (4294967264)",
Simon Glasse0ff8552016-11-25 20:15:53 -0700713 str(e.exception))
714
715 def testPackX86Rom(self):
716 """Test that a basic x86 ROM can be created"""
717 data = self._DoReadFile('29_x86-rom.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700718 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
719 chr(0) * 2, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700720
721 def testPackX86RomMeNoDesc(self):
722 """Test that an invalid Intel descriptor entry is detected"""
723 TestFunctional._MakeInputFile('descriptor.bin', '')
724 with self.assertRaises(ValueError) as e:
725 self._DoTestFile('31_x86-rom-me.dts')
726 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
727 "signature", str(e.exception))
728
729 def testPackX86RomBadDesc(self):
730 """Test that the Intel requires a descriptor entry"""
731 with self.assertRaises(ValueError) as e:
732 self._DoTestFile('30_x86-rom-me-no-desc.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600733 self.assertIn("Node '/binman/intel-me': No offset set with "
734 "offset-unset: should another entry provide this correct "
735 "offset?", str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700736
737 def testPackX86RomMe(self):
738 """Test that an x86 ROM with an ME region can be created"""
739 data = self._DoReadFile('31_x86-rom-me.dts')
740 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
741
742 def testPackVga(self):
743 """Test that an image with a VGA binary can be created"""
744 data = self._DoReadFile('32_intel-vga.dts')
745 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
746
747 def testPackStart16(self):
748 """Test that an image with an x86 start16 region can be created"""
749 data = self._DoReadFile('33_x86-start16.dts')
750 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
751
Simon Glass736bb0a2018-07-06 10:27:17 -0600752 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glassadc57012018-07-06 10:27:16 -0600753 """Handle running a test for insertion of microcode
754
755 Args:
756 dts_fname: Name of test .dts file
757 nodtb_data: Data that we expect in the first section
Simon Glass736bb0a2018-07-06 10:27:17 -0600758 ucode_second: True if the microsecond entry is second instead of
759 third
Simon Glassadc57012018-07-06 10:27:16 -0600760
761 Returns:
762 Tuple:
763 Contents of first region (U-Boot or SPL)
Simon Glass3ab95982018-08-01 15:22:37 -0600764 Offset and size components of microcode pointer, as inserted
Simon Glassadc57012018-07-06 10:27:16 -0600765 in the above (two 4-byte words)
766 """
Simon Glass6b187df2017-11-12 21:52:27 -0700767 data = self._DoReadFile(dts_fname, True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700768
769 # Now check the device tree has no microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600770 if ucode_second:
771 ucode_content = data[len(nodtb_data):]
772 ucode_pos = len(nodtb_data)
773 dtb_with_ucode = ucode_content[16:]
774 fdt_len = self.GetFdtLen(dtb_with_ucode)
775 else:
776 dtb_with_ucode = data[len(nodtb_data):]
777 fdt_len = self.GetFdtLen(dtb_with_ucode)
778 ucode_content = dtb_with_ucode[fdt_len:]
779 ucode_pos = len(nodtb_data) + fdt_len
Simon Glasse0ff8552016-11-25 20:15:53 -0700780 fname = tools.GetOutputFilename('test.dtb')
781 with open(fname, 'wb') as fd:
Simon Glassadc57012018-07-06 10:27:16 -0600782 fd.write(dtb_with_ucode)
Simon Glassec3f3782017-05-27 07:38:29 -0600783 dtb = fdt.FdtScan(fname)
784 ucode = dtb.GetNode('/microcode')
Simon Glasse0ff8552016-11-25 20:15:53 -0700785 self.assertTrue(ucode)
786 for node in ucode.subnodes:
787 self.assertFalse(node.props.get('data'))
788
Simon Glasse0ff8552016-11-25 20:15:53 -0700789 # Check that the microcode appears immediately after the Fdt
790 # This matches the concatenation of the data properties in
Simon Glass87722132017-11-12 21:52:26 -0700791 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glasse0ff8552016-11-25 20:15:53 -0700792 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
793 0x78235609)
Simon Glassadc57012018-07-06 10:27:16 -0600794 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glasse0ff8552016-11-25 20:15:53 -0700795
796 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600797 # expected offset and size
Simon Glasse0ff8552016-11-25 20:15:53 -0700798 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
799 len(ucode_data))
Simon Glass736bb0a2018-07-06 10:27:17 -0600800 u_boot = data[:len(nodtb_data)]
801 return u_boot, pos_and_size
Simon Glass6b187df2017-11-12 21:52:27 -0700802
803 def testPackUbootMicrocode(self):
804 """Test that x86 microcode can be handled correctly
805
806 We expect to see the following in the image, in order:
807 u-boot-nodtb.bin with a microcode pointer inserted at the correct
808 place
809 u-boot.dtb with the microcode removed
810 the microcode
811 """
812 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
813 U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700814 self.assertEqual('nodtb with microcode' + pos_and_size +
815 ' somewhere in here', first)
816
Simon Glass160a7662017-05-27 07:38:26 -0600817 def _RunPackUbootSingleMicrocode(self):
Simon Glasse0ff8552016-11-25 20:15:53 -0700818 """Test that x86 microcode can be handled correctly
819
820 We expect to see the following in the image, in order:
821 u-boot-nodtb.bin with a microcode pointer inserted at the correct
822 place
823 u-boot.dtb with the microcode
824 an empty microcode region
825 """
826 # We need the libfdt library to run this test since only that allows
827 # finding the offset of a property. This is required by
828 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glasse0ff8552016-11-25 20:15:53 -0700829 data = self._DoReadFile('35_x86_single_ucode.dts', True)
830
831 second = data[len(U_BOOT_NODTB_DATA):]
832
833 fdt_len = self.GetFdtLen(second)
834 third = second[fdt_len:]
835 second = second[:fdt_len]
836
Simon Glass160a7662017-05-27 07:38:26 -0600837 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
838 self.assertIn(ucode_data, second)
839 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700840
Simon Glass160a7662017-05-27 07:38:26 -0600841 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600842 # expected offset and size
Simon Glass160a7662017-05-27 07:38:26 -0600843 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
844 len(ucode_data))
845 first = data[:len(U_BOOT_NODTB_DATA)]
846 self.assertEqual('nodtb with microcode' + pos_and_size +
847 ' somewhere in here', first)
Simon Glassc49deb82016-11-25 20:15:54 -0700848
Simon Glass75db0862016-11-25 20:15:55 -0700849 def testPackUbootSingleMicrocode(self):
850 """Test that x86 microcode can be handled correctly with fdt_normal.
851 """
Simon Glass160a7662017-05-27 07:38:26 -0600852 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700853
Simon Glassc49deb82016-11-25 20:15:54 -0700854 def testUBootImg(self):
855 """Test that u-boot.img can be put in a file"""
856 data = self._DoReadFile('36_u_boot_img.dts')
857 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glass75db0862016-11-25 20:15:55 -0700858
859 def testNoMicrocode(self):
860 """Test that a missing microcode region is detected"""
861 with self.assertRaises(ValueError) as e:
862 self._DoReadFile('37_x86_no_ucode.dts', True)
863 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
864 "node found in ", str(e.exception))
865
866 def testMicrocodeWithoutNode(self):
867 """Test that a missing u-boot-dtb-with-ucode node is detected"""
868 with self.assertRaises(ValueError) as e:
869 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
870 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
871 "microcode region u-boot-dtb-with-ucode", str(e.exception))
872
873 def testMicrocodeWithoutNode2(self):
874 """Test that a missing u-boot-ucode node is detected"""
875 with self.assertRaises(ValueError) as e:
876 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
877 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
878 "microcode region u-boot-ucode", str(e.exception))
879
880 def testMicrocodeWithoutPtrInElf(self):
881 """Test that a U-Boot binary without the microcode symbol is detected"""
882 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glass75db0862016-11-25 20:15:55 -0700883 try:
884 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
885 TestFunctional._MakeInputFile('u-boot', fd.read())
886
887 with self.assertRaises(ValueError) as e:
Simon Glass160a7662017-05-27 07:38:26 -0600888 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700889 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
890 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
891
892 finally:
893 # Put the original file back
894 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
895 TestFunctional._MakeInputFile('u-boot', fd.read())
896
897 def testMicrocodeNotInImage(self):
898 """Test that microcode must be placed within the image"""
899 with self.assertRaises(ValueError) as e:
900 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
901 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
902 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glass25ac0e62018-06-01 09:38:14 -0600903 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glass75db0862016-11-25 20:15:55 -0700904
905 def testWithoutMicrocode(self):
906 """Test that we can cope with an image without microcode (e.g. qemu)"""
907 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
908 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glass16b8d6b2018-07-06 10:27:42 -0600909 data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -0700910
911 # Now check the device tree has no microcode
912 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
913 second = data[len(U_BOOT_NODTB_DATA):]
914
915 fdt_len = self.GetFdtLen(second)
916 self.assertEqual(dtb, second[:fdt_len])
917
918 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
919 third = data[used_len:]
920 self.assertEqual(chr(0) * (0x200 - used_len), third)
921
922 def testUnknownPosSize(self):
923 """Test that microcode must be placed within the image"""
924 with self.assertRaises(ValueError) as e:
925 self._DoReadFile('41_unknown_pos_size.dts', True)
Simon Glass3ab95982018-08-01 15:22:37 -0600926 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glass75db0862016-11-25 20:15:55 -0700927 "entry 'invalid-entry'", str(e.exception))
Simon Glassda229092016-11-25 20:15:56 -0700928
929 def testPackFsp(self):
930 """Test that an image with a FSP binary can be created"""
931 data = self._DoReadFile('42_intel-fsp.dts')
932 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
933
934 def testPackCmc(self):
Bin Meng59ea8c22017-08-15 22:41:54 -0700935 """Test that an image with a CMC binary can be created"""
Simon Glassda229092016-11-25 20:15:56 -0700936 data = self._DoReadFile('43_intel-cmc.dts')
937 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Meng59ea8c22017-08-15 22:41:54 -0700938
939 def testPackVbt(self):
940 """Test that an image with a VBT binary can be created"""
941 data = self._DoReadFile('46_intel-vbt.dts')
942 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glass9fc60b42017-11-12 21:52:22 -0700943
Simon Glass56509842017-11-12 21:52:25 -0700944 def testSplBssPad(self):
945 """Test that we can pad SPL's BSS with zeros"""
Simon Glass6b187df2017-11-12 21:52:27 -0700946 # ELF file with a '__bss_size' symbol
947 with open(self.TestFile('bss_data')) as fd:
948 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass56509842017-11-12 21:52:25 -0700949 data = self._DoReadFile('47_spl_bss_pad.dts')
950 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
951
Simon Glassb50e5612017-11-13 18:54:54 -0700952 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
953 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
954 with self.assertRaises(ValueError) as e:
955 data = self._DoReadFile('47_spl_bss_pad.dts')
956 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
957 str(e.exception))
958
Simon Glass87722132017-11-12 21:52:26 -0700959 def testPackStart16Spl(self):
960 """Test that an image with an x86 start16 region can be created"""
961 data = self._DoReadFile('48_x86-start16-spl.dts')
962 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
963
Simon Glass736bb0a2018-07-06 10:27:17 -0600964 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
965 """Helper function for microcode tests
Simon Glass6b187df2017-11-12 21:52:27 -0700966
967 We expect to see the following in the image, in order:
968 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
969 correct place
970 u-boot.dtb with the microcode removed
971 the microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600972
973 Args:
974 dts: Device tree file to use for test
975 ucode_second: True if the microsecond entry is second instead of
976 third
Simon Glass6b187df2017-11-12 21:52:27 -0700977 """
978 # ELF file with a '_dt_ucode_base_size' symbol
979 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
980 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass736bb0a2018-07-06 10:27:17 -0600981 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
982 ucode_second=ucode_second)
Simon Glass6b187df2017-11-12 21:52:27 -0700983 self.assertEqual('splnodtb with microc' + pos_and_size +
984 'ter somewhere in here', first)
985
Simon Glass736bb0a2018-07-06 10:27:17 -0600986 def testPackUbootSplMicrocode(self):
987 """Test that x86 microcode can be handled correctly in SPL"""
988 self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
989
990 def testPackUbootSplMicrocodeReorder(self):
991 """Test that order doesn't matter for microcode entries
992
993 This is the same as testPackUbootSplMicrocode but when we process the
994 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
995 entry, so we reply on binman to try later.
996 """
997 self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
998 ucode_second=True)
999
Simon Glassca4f4ff2017-11-12 21:52:28 -07001000 def testPackMrc(self):
1001 """Test that an image with an MRC binary can be created"""
1002 data = self._DoReadFile('50_intel_mrc.dts')
1003 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
1004
Simon Glass47419ea2017-11-13 18:54:55 -07001005 def testSplDtb(self):
1006 """Test that an image with spl/u-boot-spl.dtb can be created"""
1007 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
1008 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
1009
Simon Glass4e6fdbe2017-11-13 18:54:56 -07001010 def testSplNoDtb(self):
1011 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
1012 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
1013 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
1014
Simon Glass19790632017-11-13 18:55:01 -07001015 def testSymbols(self):
1016 """Test binman can assign symbols embedded in U-Boot"""
1017 elf_fname = self.TestFile('u_boot_binman_syms')
1018 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
1019 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glass3ab95982018-08-01 15:22:37 -06001020 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass19790632017-11-13 18:55:01 -07001021
1022 with open(self.TestFile('u_boot_binman_syms')) as fd:
1023 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
1024 data = self._DoReadFile('53_symbols.dts')
1025 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
1026 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
1027 U_BOOT_DATA +
1028 sym_values + U_BOOT_SPL_DATA[16:])
1029 self.assertEqual(expected, data)
1030
Simon Glassdd57c132018-06-01 09:38:11 -06001031 def testPackUnitAddress(self):
1032 """Test that we support multiple binaries with the same name"""
1033 data = self._DoReadFile('54_unit_address.dts')
1034 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1035
Simon Glass18546952018-06-01 09:38:16 -06001036 def testSections(self):
1037 """Basic test of sections"""
1038 data = self._DoReadFile('55_sections.dts')
Simon Glass8122f392018-07-17 13:25:28 -06001039 expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
1040 U_BOOT_DATA + '&' * 4)
Simon Glass18546952018-06-01 09:38:16 -06001041 self.assertEqual(expected, data)
Simon Glass9fc60b42017-11-12 21:52:22 -07001042
Simon Glass3b0c3822018-06-01 09:38:20 -06001043 def testMap(self):
1044 """Tests outputting a map of the images"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001045 _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
Simon Glass3ab95982018-08-01 15:22:37 -06001046 self.assertEqual(''' Offset Size Name
Simon Glass8122f392018-07-17 13:25:28 -0600104700000000 00000028 main-section
1048 00000000 00000010 section@0
1049 00000000 00000004 u-boot
1050 00000010 00000010 section@1
1051 00000000 00000004 u-boot
1052 00000020 00000004 section@2
1053 00000000 00000004 u-boot
Simon Glass3b0c3822018-06-01 09:38:20 -06001054''', map_data)
1055
Simon Glassc8d48ef2018-06-01 09:38:21 -06001056 def testNamePrefix(self):
1057 """Tests that name prefixes are used"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001058 _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
Simon Glass3ab95982018-08-01 15:22:37 -06001059 self.assertEqual(''' Offset Size Name
Simon Glass8122f392018-07-17 13:25:28 -0600106000000000 00000028 main-section
1061 00000000 00000010 section@0
1062 00000000 00000004 ro-u-boot
1063 00000010 00000010 section@1
1064 00000000 00000004 rw-u-boot
Simon Glassc8d48ef2018-06-01 09:38:21 -06001065''', map_data)
1066
Simon Glass736bb0a2018-07-06 10:27:17 -06001067 def testUnknownContents(self):
1068 """Test that obtaining the contents works as expected"""
1069 with self.assertRaises(ValueError) as e:
1070 self._DoReadFile('57_unknown_contents.dts', True)
1071 self.assertIn("Section '/binman': Internal error: Could not complete "
1072 "processing of contents: remaining [<_testing.Entry__testing ",
1073 str(e.exception))
1074
Simon Glass5c890232018-07-06 10:27:19 -06001075 def testBadChangeSize(self):
1076 """Test that trying to change the size of an entry fails"""
1077 with self.assertRaises(ValueError) as e:
1078 self._DoReadFile('59_change_size.dts', True)
1079 self.assertIn("Node '/binman/_testing': Cannot update entry size from "
1080 '2 to 1', str(e.exception))
1081
Simon Glass16b8d6b2018-07-06 10:27:42 -06001082 def testUpdateFdt(self):
Simon Glass3ab95982018-08-01 15:22:37 -06001083 """Test that we can update the device tree with offset/size info"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001084 _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
1085 update_dtb=True)
Simon Glassdbf6be92018-08-01 15:22:42 -06001086 props = self._GetPropTree(out_dtb_fname, ['offset', 'size',
1087 'image-pos'])
Simon Glass16b8d6b2018-07-06 10:27:42 -06001088 with open('/tmp/x.dtb', 'wb') as outf:
1089 with open(out_dtb_fname) as inf:
1090 outf.write(inf.read())
1091 self.assertEqual({
Simon Glassdbf6be92018-08-01 15:22:42 -06001092 'image-pos': 0,
Simon Glass8122f392018-07-17 13:25:28 -06001093 'offset': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001094 '_testing:offset': 32,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001095 '_testing:size': 1,
Simon Glassdbf6be92018-08-01 15:22:42 -06001096 '_testing:image-pos': 32,
Simon Glass3ab95982018-08-01 15:22:37 -06001097 'section@0/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001098 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001099 'section@0/u-boot:image-pos': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001100 'section@0:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001101 'section@0:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001102 'section@0:image-pos': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001103
Simon Glass3ab95982018-08-01 15:22:37 -06001104 'section@1/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001105 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001106 'section@1/u-boot:image-pos': 16,
Simon Glass3ab95982018-08-01 15:22:37 -06001107 'section@1:offset': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001108 'section@1:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001109 'section@1:image-pos': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001110 'size': 40
1111 }, props)
1112
1113 def testUpdateFdtBad(self):
1114 """Test that we detect when ProcessFdt never completes"""
1115 with self.assertRaises(ValueError) as e:
1116 self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
1117 self.assertIn('Could not complete processing of Fdt: remaining '
1118 '[<_testing.Entry__testing', str(e.exception))
Simon Glass5c890232018-07-06 10:27:19 -06001119
Simon Glass53af22a2018-07-17 13:25:32 -06001120 def testEntryArgs(self):
1121 """Test passing arguments to entries from the command line"""
1122 entry_args = {
1123 'test-str-arg': 'test1',
1124 'test-int-arg': '456',
1125 }
1126 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1127 self.assertIn('image', control.images)
1128 entry = control.images['image'].GetEntries()['_testing']
1129 self.assertEqual('test0', entry.test_str_fdt)
1130 self.assertEqual('test1', entry.test_str_arg)
1131 self.assertEqual(123, entry.test_int_fdt)
1132 self.assertEqual(456, entry.test_int_arg)
1133
1134 def testEntryArgsMissing(self):
1135 """Test missing arguments and properties"""
1136 entry_args = {
1137 'test-int-arg': '456',
1138 }
1139 self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args)
1140 entry = control.images['image'].GetEntries()['_testing']
1141 self.assertEqual('test0', entry.test_str_fdt)
1142 self.assertEqual(None, entry.test_str_arg)
1143 self.assertEqual(None, entry.test_int_fdt)
1144 self.assertEqual(456, entry.test_int_arg)
1145
1146 def testEntryArgsRequired(self):
1147 """Test missing arguments and properties"""
1148 entry_args = {
1149 'test-int-arg': '456',
1150 }
1151 with self.assertRaises(ValueError) as e:
1152 self._DoReadFileDtb('64_entry_args_required.dts')
1153 self.assertIn("Node '/binman/_testing': Missing required "
1154 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1155 str(e.exception))
1156
1157 def testEntryArgsInvalidFormat(self):
1158 """Test that an invalid entry-argument format is detected"""
1159 args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value']
1160 with self.assertRaises(ValueError) as e:
1161 self._DoBinman(*args)
1162 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1163
1164 def testEntryArgsInvalidInteger(self):
1165 """Test that an invalid entry-argument integer is detected"""
1166 entry_args = {
1167 'test-int-arg': 'abc',
1168 }
1169 with self.assertRaises(ValueError) as e:
1170 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1171 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1172 "'test-int-arg' (value 'abc') to integer",
1173 str(e.exception))
1174
1175 def testEntryArgsInvalidDatatype(self):
1176 """Test that an invalid entry-argument datatype is detected
1177
1178 This test could be written in entry_test.py except that it needs
1179 access to control.entry_args, which seems more than that module should
1180 be able to see.
1181 """
1182 entry_args = {
1183 'test-bad-datatype-arg': '12',
1184 }
1185 with self.assertRaises(ValueError) as e:
1186 self._DoReadFileDtb('65_entry_args_unknown_datatype.dts',
1187 entry_args=entry_args)
1188 self.assertIn('GetArg() internal error: Unknown data type ',
1189 str(e.exception))
1190
Simon Glassbb748372018-07-17 13:25:33 -06001191 def testText(self):
1192 """Test for a text entry type"""
1193 entry_args = {
1194 'test-id': TEXT_DATA,
1195 'test-id2': TEXT_DATA2,
1196 'test-id3': TEXT_DATA3,
1197 }
1198 data, _, _, _ = self._DoReadFileDtb('66_text.dts',
1199 entry_args=entry_args)
1200 expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 +
1201 TEXT_DATA3 + 'some text')
1202 self.assertEqual(expected, data)
1203
Simon Glassfd8d1f72018-07-17 13:25:36 -06001204 def testEntryDocs(self):
1205 """Test for creation of entry documentation"""
1206 with test_util.capture_sys_output() as (stdout, stderr):
1207 control.WriteEntryDocs(binman.GetEntryModules())
1208 self.assertTrue(len(stdout.getvalue()) > 0)
1209
1210 def testEntryDocsMissing(self):
1211 """Test handling of missing entry documentation"""
1212 with self.assertRaises(ValueError) as e:
1213 with test_util.capture_sys_output() as (stdout, stderr):
1214 control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
1215 self.assertIn('Documentation is missing for modules: u_boot',
1216 str(e.exception))
1217
Simon Glass11e36cc2018-07-17 13:25:38 -06001218 def testFmap(self):
1219 """Basic test of generation of a flashrom fmap"""
1220 data = self._DoReadFile('67_fmap.dts')
1221 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
1222 expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12
1223 self.assertEqual(expected, data[:32])
1224 self.assertEqual('__FMAP__', fhdr.signature)
1225 self.assertEqual(1, fhdr.ver_major)
1226 self.assertEqual(0, fhdr.ver_minor)
1227 self.assertEqual(0, fhdr.base)
1228 self.assertEqual(16 + 16 +
1229 fmap_util.FMAP_HEADER_LEN +
1230 fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size)
1231 self.assertEqual('FMAP', fhdr.name)
1232 self.assertEqual(3, fhdr.nareas)
1233 for fentry in fentries:
1234 self.assertEqual(0, fentry.flags)
1235
1236 self.assertEqual(0, fentries[0].offset)
1237 self.assertEqual(4, fentries[0].size)
1238 self.assertEqual('RO_U_BOOT', fentries[0].name)
1239
1240 self.assertEqual(16, fentries[1].offset)
1241 self.assertEqual(4, fentries[1].size)
1242 self.assertEqual('RW_U_BOOT', fentries[1].name)
1243
1244 self.assertEqual(32, fentries[2].offset)
1245 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1246 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
1247 self.assertEqual('FMAP', fentries[2].name)
1248
Simon Glassec127af2018-07-17 13:25:39 -06001249 def testBlobNamedByArg(self):
1250 """Test we can add a blob with the filename coming from an entry arg"""
1251 entry_args = {
1252 'cros-ec-rw-path': 'ecrw.bin',
1253 }
1254 data, _, _, _ = self._DoReadFileDtb('68_blob_named_by_arg.dts',
1255 entry_args=entry_args)
1256
Simon Glass3af8e492018-07-17 13:25:40 -06001257 def testFill(self):
1258 """Test for an fill entry type"""
1259 data = self._DoReadFile('69_fill.dts')
1260 expected = 8 * chr(0xff) + 8 * chr(0)
1261 self.assertEqual(expected, data)
1262
1263 def testFillNoSize(self):
1264 """Test for an fill entry type with no size"""
1265 with self.assertRaises(ValueError) as e:
1266 self._DoReadFile('70_fill_no_size.dts')
1267 self.assertIn("'fill' entry must have a size property",
1268 str(e.exception))
1269
Simon Glass0ef87aa2018-07-17 13:25:44 -06001270 def _HandleGbbCommand(self, pipe_list):
1271 """Fake calls to the futility utility"""
1272 if pipe_list[0][0] == 'futility':
1273 fname = pipe_list[0][-1]
1274 # Append our GBB data to the file, which will happen every time the
1275 # futility command is called.
1276 with open(fname, 'a') as fd:
1277 fd.write(GBB_DATA)
1278 return command.CommandResult()
1279
1280 def testGbb(self):
1281 """Test for the Chromium OS Google Binary Block"""
1282 command.test_result = self._HandleGbbCommand
1283 entry_args = {
1284 'keydir': 'devkeys',
1285 'bmpblk': 'bmpblk.bin',
1286 }
1287 data, _, _, _ = self._DoReadFileDtb('71_gbb.dts', entry_args=entry_args)
1288
1289 # Since futility
1290 expected = GBB_DATA + GBB_DATA + 8 * chr(0) + (0x2180 - 16) * chr(0)
1291 self.assertEqual(expected, data)
1292
1293 def testGbbTooSmall(self):
1294 """Test for the Chromium OS Google Binary Block being large enough"""
1295 with self.assertRaises(ValueError) as e:
1296 self._DoReadFileDtb('72_gbb_too_small.dts')
1297 self.assertIn("Node '/binman/gbb': GBB is too small",
1298 str(e.exception))
1299
1300 def testGbbNoSize(self):
1301 """Test for the Chromium OS Google Binary Block having a size"""
1302 with self.assertRaises(ValueError) as e:
1303 self._DoReadFileDtb('73_gbb_no_size.dts')
1304 self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
1305 str(e.exception))
1306
Simon Glass53af22a2018-07-17 13:25:32 -06001307
Simon Glass9fc60b42017-11-12 21:52:22 -07001308if __name__ == "__main__":
1309 unittest.main()