blob: 290e9aebf16923197a320ffd9fa3f575cb1967be [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 Glassb8ef5b62018-07-17 13:25:48 -060033U_BOOT_TPL_DATA = 'tpl'
Simon Glass6b187df2017-11-12 21:52:27 -070034BLOB_DATA = '89'
35ME_DATA = '0abcd'
36VGA_DATA = 'vga'
37U_BOOT_DTB_DATA = 'udtb'
Simon Glass47419ea2017-11-13 18:54:55 -070038U_BOOT_SPL_DTB_DATA = 'spldtb'
Simon Glassb8ef5b62018-07-17 13:25:48 -060039U_BOOT_TPL_DTB_DATA = 'tpldtb'
Simon Glass6b187df2017-11-12 21:52:27 -070040X86_START16_DATA = 'start16'
41X86_START16_SPL_DATA = 'start16spl'
Simon Glass35b384c2018-09-14 04:57:10 -060042X86_START16_TPL_DATA = 'start16tpl'
Simon Glass6b187df2017-11-12 21:52:27 -070043U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
44U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
45FSP_DATA = 'fsp'
46CMC_DATA = 'cmc'
47VBT_DATA = 'vbt'
Simon Glassca4f4ff2017-11-12 21:52:28 -070048MRC_DATA = 'mrc'
Simon Glassbb748372018-07-17 13:25:33 -060049TEXT_DATA = 'text'
50TEXT_DATA2 = 'text2'
51TEXT_DATA3 = 'text3'
Simon Glassec127af2018-07-17 13:25:39 -060052CROS_EC_RW_DATA = 'ecrw'
Simon Glass0ef87aa2018-07-17 13:25:44 -060053GBB_DATA = 'gbbd'
54BMPBLK_DATA = 'bmp'
Simon Glass24d0d3c2018-07-17 13:25:47 -060055VBLOCK_DATA = 'vblk'
Simon Glassec127af2018-07-17 13:25:39 -060056
Simon Glass4f443042016-11-25 20:15:52 -070057
58class TestFunctional(unittest.TestCase):
59 """Functional tests for binman
60
61 Most of these use a sample .dts file to build an image and then check
62 that it looks correct. The sample files are in the test/ subdirectory
63 and are numbered.
64
65 For each entry type a very small test file is created using fixed
66 string contents. This makes it easy to test that things look right, and
67 debug problems.
68
69 In some cases a 'real' file must be used - these are also supplied in
70 the test/ diurectory.
71 """
72 @classmethod
73 def setUpClass(self):
Simon Glass4d5994f2017-11-12 21:52:20 -070074 global entry
75 import entry
76
Simon Glass4f443042016-11-25 20:15:52 -070077 # Handle the case where argv[0] is 'python'
78 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
79 self._binman_pathname = os.path.join(self._binman_dir, 'binman')
80
81 # Create a temporary directory for input files
82 self._indir = tempfile.mkdtemp(prefix='binmant.')
83
84 # Create some test files
85 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
86 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
87 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
Simon Glassb8ef5b62018-07-17 13:25:48 -060088 TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070089 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -070090 TestFunctional._MakeInputFile('me.bin', ME_DATA)
91 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glassb8ef5b62018-07-17 13:25:48 -060092 self._ResetDtbs()
Simon Glasse0ff8552016-11-25 20:15:53 -070093 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Simon Glass87722132017-11-12 21:52:26 -070094 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
95 X86_START16_SPL_DATA)
Simon Glass35b384c2018-09-14 04:57:10 -060096 TestFunctional._MakeInputFile('tpl/u-boot-x86-16bit-tpl.bin',
97 X86_START16_TPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070098 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass6b187df2017-11-12 21:52:27 -070099 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
100 U_BOOT_SPL_NODTB_DATA)
Simon Glassda229092016-11-25 20:15:56 -0700101 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
102 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Meng59ea8c22017-08-15 22:41:54 -0700103 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassca4f4ff2017-11-12 21:52:28 -0700104 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glassec127af2018-07-17 13:25:39 -0600105 TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
Simon Glass0ef87aa2018-07-17 13:25:44 -0600106 TestFunctional._MakeInputDir('devkeys')
107 TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700108 self._output_setup = False
109
Simon Glasse0ff8552016-11-25 20:15:53 -0700110 # ELF file with a '_dt_ucode_base_size' symbol
111 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
112 TestFunctional._MakeInputFile('u-boot', fd.read())
113
114 # Intel flash descriptor file
115 with open(self.TestFile('descriptor.bin')) as fd:
116 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
117
Simon Glass4f443042016-11-25 20:15:52 -0700118 @classmethod
119 def tearDownClass(self):
120 """Remove the temporary input directory and its contents"""
121 if self._indir:
122 shutil.rmtree(self._indir)
123 self._indir = None
124
125 def setUp(self):
126 # Enable this to turn on debugging output
127 # tout.Init(tout.DEBUG)
128 command.test_result = None
129
130 def tearDown(self):
131 """Remove the temporary output directory"""
132 tools._FinaliseForTest()
133
Simon Glassb8ef5b62018-07-17 13:25:48 -0600134 @classmethod
135 def _ResetDtbs(self):
136 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
137 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
138 TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA)
139
Simon Glass4f443042016-11-25 20:15:52 -0700140 def _RunBinman(self, *args, **kwargs):
141 """Run binman using the command line
142
143 Args:
144 Arguments to pass, as a list of strings
145 kwargs: Arguments to pass to Command.RunPipe()
146 """
147 result = command.RunPipe([[self._binman_pathname] + list(args)],
148 capture=True, capture_stderr=True, raise_on_error=False)
149 if result.return_code and kwargs.get('raise_on_error', True):
150 raise Exception("Error running '%s': %s" % (' '.join(args),
151 result.stdout + result.stderr))
152 return result
153
154 def _DoBinman(self, *args):
155 """Run binman using directly (in the same process)
156
157 Args:
158 Arguments to pass, as a list of strings
159 Returns:
160 Return value (0 for success)
161 """
Simon Glass7fe91732017-11-13 18:55:00 -0700162 args = list(args)
163 if '-D' in sys.argv:
164 args = args + ['-D']
165 (options, args) = cmdline.ParseArgs(args)
Simon Glass4f443042016-11-25 20:15:52 -0700166 options.pager = 'binman-invalid-pager'
167 options.build_dir = self._indir
168
169 # For testing, you can force an increase in verbosity here
170 # options.verbosity = tout.DEBUG
171 return control.Binman(options, args)
172
Simon Glass53af22a2018-07-17 13:25:32 -0600173 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
Simon Glass0bfa7b02018-09-14 04:57:12 -0600174 entry_args=None, images=None):
Simon Glass4f443042016-11-25 20:15:52 -0700175 """Run binman with a given test file
176
177 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600178 fname: Device-tree source filename to use (e.g. 05_simple.dts)
179 debug: True to enable debugging output
Simon Glass3b0c3822018-06-01 09:38:20 -0600180 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600181 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600182 tree before packing it into the image
Simon Glass0bfa7b02018-09-14 04:57:12 -0600183 entry_args: Dict of entry args to supply to binman
184 key: arg name
185 value: value of that arg
186 images: List of image names to build
Simon Glass4f443042016-11-25 20:15:52 -0700187 """
Simon Glass7fe91732017-11-13 18:55:00 -0700188 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
189 if debug:
190 args.append('-D')
Simon Glass3b0c3822018-06-01 09:38:20 -0600191 if map:
192 args.append('-m')
Simon Glass16b8d6b2018-07-06 10:27:42 -0600193 if update_dtb:
194 args.append('-up')
Simon Glass53af22a2018-07-17 13:25:32 -0600195 if entry_args:
196 for arg, value in entry_args.iteritems():
197 args.append('-a%s=%s' % (arg, value))
Simon Glass0bfa7b02018-09-14 04:57:12 -0600198 if images:
199 for image in images:
200 args += ['-i', image]
Simon Glass7fe91732017-11-13 18:55:00 -0700201 return self._DoBinman(*args)
Simon Glass4f443042016-11-25 20:15:52 -0700202
203 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glasse0ff8552016-11-25 20:15:53 -0700204 """Set up a new test device-tree file
205
206 The given file is compiled and set up as the device tree to be used
207 for ths test.
208
209 Args:
210 fname: Filename of .dts file to read
Simon Glass7ae5f312018-06-01 09:38:19 -0600211 outfile: Output filename for compiled device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700212
213 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600214 Contents of device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700215 """
Simon Glass4f443042016-11-25 20:15:52 -0700216 if not self._output_setup:
217 tools.PrepareOutputDir(self._indir, True)
218 self._output_setup = True
219 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
220 with open(dtb) as fd:
221 data = fd.read()
222 TestFunctional._MakeInputFile(outfile, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700223 return data
Simon Glass4f443042016-11-25 20:15:52 -0700224
Simon Glass16b8d6b2018-07-06 10:27:42 -0600225 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass53af22a2018-07-17 13:25:32 -0600226 update_dtb=False, entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700227 """Run binman and return the resulting image
228
229 This runs binman with a given test file and then reads the resulting
230 output file. It is a shortcut function since most tests need to do
231 these steps.
232
233 Raises an assertion failure if binman returns a non-zero exit code.
234
235 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600236 fname: Device-tree source filename to use (e.g. 05_simple.dts)
Simon Glass4f443042016-11-25 20:15:52 -0700237 use_real_dtb: True to use the test file as the contents of
238 the u-boot-dtb entry. Normally this is not needed and the
239 test contents (the U_BOOT_DTB_DATA string) can be used.
240 But in some test we need the real contents.
Simon Glass3b0c3822018-06-01 09:38:20 -0600241 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600242 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600243 tree before packing it into the image
Simon Glasse0ff8552016-11-25 20:15:53 -0700244
245 Returns:
246 Tuple:
247 Resulting image contents
248 Device tree contents
Simon Glass3b0c3822018-06-01 09:38:20 -0600249 Map data showing contents of image (or None if none)
Simon Glassea6922e2018-07-17 13:25:27 -0600250 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass4f443042016-11-25 20:15:52 -0700251 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700252 dtb_data = None
Simon Glass4f443042016-11-25 20:15:52 -0700253 # Use the compiled test file as the u-boot-dtb input
254 if use_real_dtb:
Simon Glasse0ff8552016-11-25 20:15:53 -0700255 dtb_data = self._SetupDtb(fname)
Simon Glass4f443042016-11-25 20:15:52 -0700256
257 try:
Simon Glass53af22a2018-07-17 13:25:32 -0600258 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
259 entry_args=entry_args)
Simon Glass4f443042016-11-25 20:15:52 -0700260 self.assertEqual(0, retcode)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600261 out_dtb_fname = control.GetFdtPath('u-boot.dtb')
Simon Glass4f443042016-11-25 20:15:52 -0700262
263 # Find the (only) image, read it and return its contents
264 image = control.images['image']
Simon Glass16b8d6b2018-07-06 10:27:42 -0600265 image_fname = tools.GetOutputFilename('image.bin')
266 self.assertTrue(os.path.exists(image_fname))
Simon Glass3b0c3822018-06-01 09:38:20 -0600267 if map:
268 map_fname = tools.GetOutputFilename('image.map')
269 with open(map_fname) as fd:
270 map_data = fd.read()
271 else:
272 map_data = None
Simon Glass16b8d6b2018-07-06 10:27:42 -0600273 with open(image_fname) as fd:
274 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass4f443042016-11-25 20:15:52 -0700275 finally:
276 # Put the test file back
277 if use_real_dtb:
Simon Glassb8ef5b62018-07-17 13:25:48 -0600278 self._ResetDtbs()
Simon Glass4f443042016-11-25 20:15:52 -0700279
Simon Glasse0ff8552016-11-25 20:15:53 -0700280 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass7ae5f312018-06-01 09:38:19 -0600281 """Helper function which discards the device-tree binary
282
283 Args:
284 fname: Device-tree source filename to use (e.g. 05_simple.dts)
285 use_real_dtb: True to use the test file as the contents of
286 the u-boot-dtb entry. Normally this is not needed and the
287 test contents (the U_BOOT_DTB_DATA string) can be used.
288 But in some test we need the real contents.
Simon Glassea6922e2018-07-17 13:25:27 -0600289
290 Returns:
291 Resulting image contents
Simon Glass7ae5f312018-06-01 09:38:19 -0600292 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700293 return self._DoReadFileDtb(fname, use_real_dtb)[0]
294
Simon Glass4f443042016-11-25 20:15:52 -0700295 @classmethod
296 def _MakeInputFile(self, fname, contents):
297 """Create a new test input file, creating directories as needed
298
299 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600300 fname: Filename to create
Simon Glass4f443042016-11-25 20:15:52 -0700301 contents: File contents to write in to the file
302 Returns:
303 Full pathname of file created
304 """
305 pathname = os.path.join(self._indir, fname)
306 dirname = os.path.dirname(pathname)
307 if dirname and not os.path.exists(dirname):
308 os.makedirs(dirname)
309 with open(pathname, 'wb') as fd:
310 fd.write(contents)
311 return pathname
312
313 @classmethod
Simon Glass0ef87aa2018-07-17 13:25:44 -0600314 def _MakeInputDir(self, dirname):
315 """Create a new test input directory, creating directories as needed
316
317 Args:
318 dirname: Directory name to create
319
320 Returns:
321 Full pathname of directory created
322 """
323 pathname = os.path.join(self._indir, dirname)
324 if not os.path.exists(pathname):
325 os.makedirs(pathname)
326 return pathname
327
328 @classmethod
Simon Glass4f443042016-11-25 20:15:52 -0700329 def TestFile(self, fname):
330 return os.path.join(self._binman_dir, 'test', fname)
331
332 def AssertInList(self, grep_list, target):
333 """Assert that at least one of a list of things is in a target
334
335 Args:
336 grep_list: List of strings to check
337 target: Target string
338 """
339 for grep in grep_list:
340 if grep in target:
341 return
342 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
343
344 def CheckNoGaps(self, entries):
345 """Check that all entries fit together without gaps
346
347 Args:
348 entries: List of entries to check
349 """
Simon Glass3ab95982018-08-01 15:22:37 -0600350 offset = 0
Simon Glass4f443042016-11-25 20:15:52 -0700351 for entry in entries.values():
Simon Glass3ab95982018-08-01 15:22:37 -0600352 self.assertEqual(offset, entry.offset)
353 offset += entry.size
Simon Glass4f443042016-11-25 20:15:52 -0700354
Simon Glasse0ff8552016-11-25 20:15:53 -0700355 def GetFdtLen(self, dtb):
Simon Glass7ae5f312018-06-01 09:38:19 -0600356 """Get the totalsize field from a device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700357
358 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600359 dtb: Device-tree binary contents
Simon Glasse0ff8552016-11-25 20:15:53 -0700360
361 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600362 Total size of device-tree binary, from the header
Simon Glasse0ff8552016-11-25 20:15:53 -0700363 """
364 return struct.unpack('>L', dtb[4:8])[0]
365
Simon Glasscee02e62018-07-17 13:25:52 -0600366 def _GetPropTree(self, dtb, prop_names):
Simon Glass16b8d6b2018-07-06 10:27:42 -0600367 def AddNode(node, path):
368 if node.name != '/':
369 path += '/' + node.name
Simon Glass16b8d6b2018-07-06 10:27:42 -0600370 for subnode in node.subnodes:
371 for prop in subnode.props.values():
Simon Glasscee02e62018-07-17 13:25:52 -0600372 if prop.name in prop_names:
Simon Glass16b8d6b2018-07-06 10:27:42 -0600373 prop_path = path + '/' + subnode.name + ':' + prop.name
374 tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
375 prop.value)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600376 AddNode(subnode, path)
377
378 tree = {}
Simon Glass16b8d6b2018-07-06 10:27:42 -0600379 AddNode(dtb.GetRoot(), '')
380 return tree
381
Simon Glass4f443042016-11-25 20:15:52 -0700382 def testRun(self):
383 """Test a basic run with valid args"""
384 result = self._RunBinman('-h')
385
386 def testFullHelp(self):
387 """Test that the full help is displayed with -H"""
388 result = self._RunBinman('-H')
389 help_file = os.path.join(self._binman_dir, 'README')
Tom Rini3759df02018-01-16 15:29:50 -0500390 # Remove possible extraneous strings
391 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
392 gothelp = result.stdout.replace(extra, '')
393 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass4f443042016-11-25 20:15:52 -0700394 self.assertEqual(0, len(result.stderr))
395 self.assertEqual(0, result.return_code)
396
397 def testFullHelpInternal(self):
398 """Test that the full help is displayed with -H"""
399 try:
400 command.test_result = command.CommandResult()
401 result = self._DoBinman('-H')
402 help_file = os.path.join(self._binman_dir, 'README')
403 finally:
404 command.test_result = None
405
406 def testHelp(self):
407 """Test that the basic help is displayed with -h"""
408 result = self._RunBinman('-h')
409 self.assertTrue(len(result.stdout) > 200)
410 self.assertEqual(0, len(result.stderr))
411 self.assertEqual(0, result.return_code)
412
Simon Glass4f443042016-11-25 20:15:52 -0700413 def testBoard(self):
414 """Test that we can run it with a specific board"""
415 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
416 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
417 result = self._DoBinman('-b', 'sandbox')
418 self.assertEqual(0, result)
419
420 def testNeedBoard(self):
421 """Test that we get an error when no board ius supplied"""
422 with self.assertRaises(ValueError) as e:
423 result = self._DoBinman()
424 self.assertIn("Must provide a board to process (use -b <board>)",
425 str(e.exception))
426
427 def testMissingDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600428 """Test that an invalid device-tree file generates an error"""
Simon Glass4f443042016-11-25 20:15:52 -0700429 with self.assertRaises(Exception) as e:
430 self._RunBinman('-d', 'missing_file')
431 # We get one error from libfdt, and a different one from fdtget.
432 self.AssertInList(["Couldn't open blob from 'missing_file'",
433 'No such file or directory'], str(e.exception))
434
435 def testBrokenDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600436 """Test that an invalid device-tree source file generates an error
Simon Glass4f443042016-11-25 20:15:52 -0700437
438 Since this is a source file it should be compiled and the error
439 will come from the device-tree compiler (dtc).
440 """
441 with self.assertRaises(Exception) as e:
442 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
443 self.assertIn("FATAL ERROR: Unable to parse input tree",
444 str(e.exception))
445
446 def testMissingNode(self):
447 """Test that a device tree without a 'binman' node generates an error"""
448 with self.assertRaises(Exception) as e:
449 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
450 self.assertIn("does not have a 'binman' node", str(e.exception))
451
452 def testEmpty(self):
453 """Test that an empty binman node works OK (i.e. does nothing)"""
454 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
455 self.assertEqual(0, len(result.stderr))
456 self.assertEqual(0, result.return_code)
457
458 def testInvalidEntry(self):
459 """Test that an invalid entry is flagged"""
460 with self.assertRaises(Exception) as e:
461 result = self._RunBinman('-d',
462 self.TestFile('04_invalid_entry.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700463 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
464 "'/binman/not-a-valid-type'", str(e.exception))
465
466 def testSimple(self):
467 """Test a simple binman with a single file"""
468 data = self._DoReadFile('05_simple.dts')
469 self.assertEqual(U_BOOT_DATA, data)
470
Simon Glass7fe91732017-11-13 18:55:00 -0700471 def testSimpleDebug(self):
472 """Test a simple binman run with debugging enabled"""
473 data = self._DoTestFile('05_simple.dts', debug=True)
474
Simon Glass4f443042016-11-25 20:15:52 -0700475 def testDual(self):
476 """Test that we can handle creating two images
477
478 This also tests image padding.
479 """
480 retcode = self._DoTestFile('06_dual_image.dts')
481 self.assertEqual(0, retcode)
482
483 image = control.images['image1']
484 self.assertEqual(len(U_BOOT_DATA), image._size)
485 fname = tools.GetOutputFilename('image1.bin')
486 self.assertTrue(os.path.exists(fname))
487 with open(fname) as fd:
488 data = fd.read()
489 self.assertEqual(U_BOOT_DATA, data)
490
491 image = control.images['image2']
492 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
493 fname = tools.GetOutputFilename('image2.bin')
494 self.assertTrue(os.path.exists(fname))
495 with open(fname) as fd:
496 data = fd.read()
497 self.assertEqual(U_BOOT_DATA, data[3:7])
498 self.assertEqual(chr(0) * 3, data[:3])
499 self.assertEqual(chr(0) * 5, data[7:])
500
501 def testBadAlign(self):
502 """Test that an invalid alignment value is detected"""
503 with self.assertRaises(ValueError) as e:
504 self._DoTestFile('07_bad_align.dts')
505 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
506 "of two", str(e.exception))
507
508 def testPackSimple(self):
509 """Test that packing works as expected"""
510 retcode = self._DoTestFile('08_pack.dts')
511 self.assertEqual(0, retcode)
512 self.assertIn('image', control.images)
513 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600514 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700515 self.assertEqual(5, len(entries))
516
517 # First u-boot
518 self.assertIn('u-boot', entries)
519 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600520 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700521 self.assertEqual(len(U_BOOT_DATA), entry.size)
522
523 # Second u-boot, aligned to 16-byte boundary
524 self.assertIn('u-boot-align', entries)
525 entry = entries['u-boot-align']
Simon Glass3ab95982018-08-01 15:22:37 -0600526 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700527 self.assertEqual(len(U_BOOT_DATA), entry.size)
528
529 # Third u-boot, size 23 bytes
530 self.assertIn('u-boot-size', entries)
531 entry = entries['u-boot-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600532 self.assertEqual(20, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700533 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
534 self.assertEqual(23, entry.size)
535
536 # Fourth u-boot, placed immediate after the above
537 self.assertIn('u-boot-next', entries)
538 entry = entries['u-boot-next']
Simon Glass3ab95982018-08-01 15:22:37 -0600539 self.assertEqual(43, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700540 self.assertEqual(len(U_BOOT_DATA), entry.size)
541
Simon Glass3ab95982018-08-01 15:22:37 -0600542 # Fifth u-boot, placed at a fixed offset
Simon Glass4f443042016-11-25 20:15:52 -0700543 self.assertIn('u-boot-fixed', entries)
544 entry = entries['u-boot-fixed']
Simon Glass3ab95982018-08-01 15:22:37 -0600545 self.assertEqual(61, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700546 self.assertEqual(len(U_BOOT_DATA), entry.size)
547
548 self.assertEqual(65, image._size)
549
550 def testPackExtra(self):
551 """Test that extra packing feature works as expected"""
552 retcode = self._DoTestFile('09_pack_extra.dts')
553
554 self.assertEqual(0, retcode)
555 self.assertIn('image', control.images)
556 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600557 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700558 self.assertEqual(5, len(entries))
559
560 # First u-boot with padding before and after
561 self.assertIn('u-boot', entries)
562 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600563 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700564 self.assertEqual(3, entry.pad_before)
565 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
566
567 # Second u-boot has an aligned size, but it has no effect
568 self.assertIn('u-boot-align-size-nop', entries)
569 entry = entries['u-boot-align-size-nop']
Simon Glass3ab95982018-08-01 15:22:37 -0600570 self.assertEqual(12, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700571 self.assertEqual(4, entry.size)
572
573 # Third u-boot has an aligned size too
574 self.assertIn('u-boot-align-size', entries)
575 entry = entries['u-boot-align-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600576 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700577 self.assertEqual(32, entry.size)
578
579 # Fourth u-boot has an aligned end
580 self.assertIn('u-boot-align-end', entries)
581 entry = entries['u-boot-align-end']
Simon Glass3ab95982018-08-01 15:22:37 -0600582 self.assertEqual(48, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700583 self.assertEqual(16, entry.size)
584
585 # Fifth u-boot immediately afterwards
586 self.assertIn('u-boot-align-both', entries)
587 entry = entries['u-boot-align-both']
Simon Glass3ab95982018-08-01 15:22:37 -0600588 self.assertEqual(64, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700589 self.assertEqual(64, entry.size)
590
591 self.CheckNoGaps(entries)
592 self.assertEqual(128, image._size)
593
594 def testPackAlignPowerOf2(self):
595 """Test that invalid entry alignment is detected"""
596 with self.assertRaises(ValueError) as e:
597 self._DoTestFile('10_pack_align_power2.dts')
598 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
599 "of two", str(e.exception))
600
601 def testPackAlignSizePowerOf2(self):
602 """Test that invalid entry size alignment is detected"""
603 with self.assertRaises(ValueError) as e:
604 self._DoTestFile('11_pack_align_size_power2.dts')
605 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
606 "power of two", str(e.exception))
607
608 def testPackInvalidAlign(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600609 """Test detection of an offset that does not match its alignment"""
Simon Glass4f443042016-11-25 20:15:52 -0700610 with self.assertRaises(ValueError) as e:
611 self._DoTestFile('12_pack_inv_align.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600612 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700613 "align 0x4 (4)", str(e.exception))
614
615 def testPackInvalidSizeAlign(self):
616 """Test that invalid entry size alignment is detected"""
617 with self.assertRaises(ValueError) as e:
618 self._DoTestFile('13_pack_inv_size_align.dts')
619 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
620 "align-size 0x4 (4)", str(e.exception))
621
622 def testPackOverlap(self):
623 """Test that overlapping regions are detected"""
624 with self.assertRaises(ValueError) as e:
625 self._DoTestFile('14_pack_overlap.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600626 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700627 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
628 str(e.exception))
629
630 def testPackEntryOverflow(self):
631 """Test that entries that overflow their size are detected"""
632 with self.assertRaises(ValueError) as e:
633 self._DoTestFile('15_pack_overflow.dts')
634 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
635 "but entry size is 0x3 (3)", str(e.exception))
636
637 def testPackImageOverflow(self):
638 """Test that entries which overflow the image size are detected"""
639 with self.assertRaises(ValueError) as e:
640 self._DoTestFile('16_pack_image_overflow.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600641 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass4f443042016-11-25 20:15:52 -0700642 "size 0x3 (3)", str(e.exception))
643
644 def testPackImageSize(self):
645 """Test that the image size can be set"""
646 retcode = self._DoTestFile('17_pack_image_size.dts')
647 self.assertEqual(0, retcode)
648 self.assertIn('image', control.images)
649 image = control.images['image']
650 self.assertEqual(7, image._size)
651
652 def testPackImageSizeAlign(self):
653 """Test that image size alignemnt works as expected"""
654 retcode = self._DoTestFile('18_pack_image_align.dts')
655 self.assertEqual(0, retcode)
656 self.assertIn('image', control.images)
657 image = control.images['image']
658 self.assertEqual(16, image._size)
659
660 def testPackInvalidImageAlign(self):
661 """Test that invalid image alignment is detected"""
662 with self.assertRaises(ValueError) as e:
663 self._DoTestFile('19_pack_inv_image_align.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600664 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700665 "align-size 0x8 (8)", str(e.exception))
666
667 def testPackAlignPowerOf2(self):
668 """Test that invalid image alignment is detected"""
669 with self.assertRaises(ValueError) as e:
670 self._DoTestFile('20_pack_inv_image_align_power2.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600671 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass4f443042016-11-25 20:15:52 -0700672 "two", str(e.exception))
673
674 def testImagePadByte(self):
675 """Test that the image pad byte can be specified"""
Simon Glass19790632017-11-13 18:55:01 -0700676 with open(self.TestFile('bss_data')) as fd:
677 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass4f443042016-11-25 20:15:52 -0700678 data = self._DoReadFile('21_image_pad.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700679 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700680
681 def testImageName(self):
682 """Test that image files can be named"""
683 retcode = self._DoTestFile('22_image_name.dts')
684 self.assertEqual(0, retcode)
685 image = control.images['image1']
686 fname = tools.GetOutputFilename('test-name')
687 self.assertTrue(os.path.exists(fname))
688
689 image = control.images['image2']
690 fname = tools.GetOutputFilename('test-name.xx')
691 self.assertTrue(os.path.exists(fname))
692
693 def testBlobFilename(self):
694 """Test that generic blobs can be provided by filename"""
695 data = self._DoReadFile('23_blob.dts')
696 self.assertEqual(BLOB_DATA, data)
697
698 def testPackSorted(self):
699 """Test that entries can be sorted"""
700 data = self._DoReadFile('24_sorted.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700701 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
Simon Glass4f443042016-11-25 20:15:52 -0700702 U_BOOT_DATA, data)
703
Simon Glass3ab95982018-08-01 15:22:37 -0600704 def testPackZeroOffset(self):
705 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass4f443042016-11-25 20:15:52 -0700706 with self.assertRaises(ValueError) as e:
707 self._DoTestFile('25_pack_zero_size.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600708 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700709 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
710 str(e.exception))
711
712 def testPackUbootDtb(self):
713 """Test that a device tree can be added to U-Boot"""
714 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
715 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700716
717 def testPackX86RomNoSize(self):
718 """Test that the end-at-4gb property requires a size property"""
719 with self.assertRaises(ValueError) as e:
720 self._DoTestFile('27_pack_4gb_no_size.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600721 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glasse0ff8552016-11-25 20:15:53 -0700722 "using end-at-4gb", str(e.exception))
723
724 def testPackX86RomOutside(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600725 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glasse0ff8552016-11-25 20:15:53 -0700726 with self.assertRaises(ValueError) as e:
727 self._DoTestFile('28_pack_4gb_outside.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600728 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glass8f1da502018-06-01 09:38:12 -0600729 "the section starting at 0xffffffe0 (4294967264)",
Simon Glasse0ff8552016-11-25 20:15:53 -0700730 str(e.exception))
731
732 def testPackX86Rom(self):
733 """Test that a basic x86 ROM can be created"""
734 data = self._DoReadFile('29_x86-rom.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700735 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
736 chr(0) * 2, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700737
738 def testPackX86RomMeNoDesc(self):
739 """Test that an invalid Intel descriptor entry is detected"""
740 TestFunctional._MakeInputFile('descriptor.bin', '')
741 with self.assertRaises(ValueError) as e:
742 self._DoTestFile('31_x86-rom-me.dts')
743 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
744 "signature", str(e.exception))
745
746 def testPackX86RomBadDesc(self):
747 """Test that the Intel requires a descriptor entry"""
748 with self.assertRaises(ValueError) as e:
749 self._DoTestFile('30_x86-rom-me-no-desc.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600750 self.assertIn("Node '/binman/intel-me': No offset set with "
751 "offset-unset: should another entry provide this correct "
752 "offset?", str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700753
754 def testPackX86RomMe(self):
755 """Test that an x86 ROM with an ME region can be created"""
756 data = self._DoReadFile('31_x86-rom-me.dts')
757 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
758
759 def testPackVga(self):
760 """Test that an image with a VGA binary can be created"""
761 data = self._DoReadFile('32_intel-vga.dts')
762 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
763
764 def testPackStart16(self):
765 """Test that an image with an x86 start16 region can be created"""
766 data = self._DoReadFile('33_x86-start16.dts')
767 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
768
Simon Glass736bb0a2018-07-06 10:27:17 -0600769 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glassadc57012018-07-06 10:27:16 -0600770 """Handle running a test for insertion of microcode
771
772 Args:
773 dts_fname: Name of test .dts file
774 nodtb_data: Data that we expect in the first section
Simon Glass736bb0a2018-07-06 10:27:17 -0600775 ucode_second: True if the microsecond entry is second instead of
776 third
Simon Glassadc57012018-07-06 10:27:16 -0600777
778 Returns:
779 Tuple:
780 Contents of first region (U-Boot or SPL)
Simon Glass3ab95982018-08-01 15:22:37 -0600781 Offset and size components of microcode pointer, as inserted
Simon Glassadc57012018-07-06 10:27:16 -0600782 in the above (two 4-byte words)
783 """
Simon Glass6b187df2017-11-12 21:52:27 -0700784 data = self._DoReadFile(dts_fname, True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700785
786 # Now check the device tree has no microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600787 if ucode_second:
788 ucode_content = data[len(nodtb_data):]
789 ucode_pos = len(nodtb_data)
790 dtb_with_ucode = ucode_content[16:]
791 fdt_len = self.GetFdtLen(dtb_with_ucode)
792 else:
793 dtb_with_ucode = data[len(nodtb_data):]
794 fdt_len = self.GetFdtLen(dtb_with_ucode)
795 ucode_content = dtb_with_ucode[fdt_len:]
796 ucode_pos = len(nodtb_data) + fdt_len
Simon Glasse0ff8552016-11-25 20:15:53 -0700797 fname = tools.GetOutputFilename('test.dtb')
798 with open(fname, 'wb') as fd:
Simon Glassadc57012018-07-06 10:27:16 -0600799 fd.write(dtb_with_ucode)
Simon Glassec3f3782017-05-27 07:38:29 -0600800 dtb = fdt.FdtScan(fname)
801 ucode = dtb.GetNode('/microcode')
Simon Glasse0ff8552016-11-25 20:15:53 -0700802 self.assertTrue(ucode)
803 for node in ucode.subnodes:
804 self.assertFalse(node.props.get('data'))
805
Simon Glasse0ff8552016-11-25 20:15:53 -0700806 # Check that the microcode appears immediately after the Fdt
807 # This matches the concatenation of the data properties in
Simon Glass87722132017-11-12 21:52:26 -0700808 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glasse0ff8552016-11-25 20:15:53 -0700809 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
810 0x78235609)
Simon Glassadc57012018-07-06 10:27:16 -0600811 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glasse0ff8552016-11-25 20:15:53 -0700812
813 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600814 # expected offset and size
Simon Glasse0ff8552016-11-25 20:15:53 -0700815 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
816 len(ucode_data))
Simon Glass736bb0a2018-07-06 10:27:17 -0600817 u_boot = data[:len(nodtb_data)]
818 return u_boot, pos_and_size
Simon Glass6b187df2017-11-12 21:52:27 -0700819
820 def testPackUbootMicrocode(self):
821 """Test that x86 microcode can be handled correctly
822
823 We expect to see the following in the image, in order:
824 u-boot-nodtb.bin with a microcode pointer inserted at the correct
825 place
826 u-boot.dtb with the microcode removed
827 the microcode
828 """
829 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
830 U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700831 self.assertEqual('nodtb with microcode' + pos_and_size +
832 ' somewhere in here', first)
833
Simon Glass160a7662017-05-27 07:38:26 -0600834 def _RunPackUbootSingleMicrocode(self):
Simon Glasse0ff8552016-11-25 20:15:53 -0700835 """Test that x86 microcode can be handled correctly
836
837 We expect to see the following in the image, in order:
838 u-boot-nodtb.bin with a microcode pointer inserted at the correct
839 place
840 u-boot.dtb with the microcode
841 an empty microcode region
842 """
843 # We need the libfdt library to run this test since only that allows
844 # finding the offset of a property. This is required by
845 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glasse0ff8552016-11-25 20:15:53 -0700846 data = self._DoReadFile('35_x86_single_ucode.dts', True)
847
848 second = data[len(U_BOOT_NODTB_DATA):]
849
850 fdt_len = self.GetFdtLen(second)
851 third = second[fdt_len:]
852 second = second[:fdt_len]
853
Simon Glass160a7662017-05-27 07:38:26 -0600854 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
855 self.assertIn(ucode_data, second)
856 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700857
Simon Glass160a7662017-05-27 07:38:26 -0600858 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600859 # expected offset and size
Simon Glass160a7662017-05-27 07:38:26 -0600860 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
861 len(ucode_data))
862 first = data[:len(U_BOOT_NODTB_DATA)]
863 self.assertEqual('nodtb with microcode' + pos_and_size +
864 ' somewhere in here', first)
Simon Glassc49deb82016-11-25 20:15:54 -0700865
Simon Glass75db0862016-11-25 20:15:55 -0700866 def testPackUbootSingleMicrocode(self):
867 """Test that x86 microcode can be handled correctly with fdt_normal.
868 """
Simon Glass160a7662017-05-27 07:38:26 -0600869 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700870
Simon Glassc49deb82016-11-25 20:15:54 -0700871 def testUBootImg(self):
872 """Test that u-boot.img can be put in a file"""
873 data = self._DoReadFile('36_u_boot_img.dts')
874 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glass75db0862016-11-25 20:15:55 -0700875
876 def testNoMicrocode(self):
877 """Test that a missing microcode region is detected"""
878 with self.assertRaises(ValueError) as e:
879 self._DoReadFile('37_x86_no_ucode.dts', True)
880 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
881 "node found in ", str(e.exception))
882
883 def testMicrocodeWithoutNode(self):
884 """Test that a missing u-boot-dtb-with-ucode node is detected"""
885 with self.assertRaises(ValueError) as e:
886 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
887 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
888 "microcode region u-boot-dtb-with-ucode", str(e.exception))
889
890 def testMicrocodeWithoutNode2(self):
891 """Test that a missing u-boot-ucode node is detected"""
892 with self.assertRaises(ValueError) as e:
893 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
894 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
895 "microcode region u-boot-ucode", str(e.exception))
896
897 def testMicrocodeWithoutPtrInElf(self):
898 """Test that a U-Boot binary without the microcode symbol is detected"""
899 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glass75db0862016-11-25 20:15:55 -0700900 try:
901 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
902 TestFunctional._MakeInputFile('u-boot', fd.read())
903
904 with self.assertRaises(ValueError) as e:
Simon Glass160a7662017-05-27 07:38:26 -0600905 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700906 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
907 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
908
909 finally:
910 # Put the original file back
911 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
912 TestFunctional._MakeInputFile('u-boot', fd.read())
913
914 def testMicrocodeNotInImage(self):
915 """Test that microcode must be placed within the image"""
916 with self.assertRaises(ValueError) as e:
917 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
918 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
919 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glass25ac0e62018-06-01 09:38:14 -0600920 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glass75db0862016-11-25 20:15:55 -0700921
922 def testWithoutMicrocode(self):
923 """Test that we can cope with an image without microcode (e.g. qemu)"""
924 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
925 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glass16b8d6b2018-07-06 10:27:42 -0600926 data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -0700927
928 # Now check the device tree has no microcode
929 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
930 second = data[len(U_BOOT_NODTB_DATA):]
931
932 fdt_len = self.GetFdtLen(second)
933 self.assertEqual(dtb, second[:fdt_len])
934
935 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
936 third = data[used_len:]
937 self.assertEqual(chr(0) * (0x200 - used_len), third)
938
939 def testUnknownPosSize(self):
940 """Test that microcode must be placed within the image"""
941 with self.assertRaises(ValueError) as e:
942 self._DoReadFile('41_unknown_pos_size.dts', True)
Simon Glass3ab95982018-08-01 15:22:37 -0600943 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glass75db0862016-11-25 20:15:55 -0700944 "entry 'invalid-entry'", str(e.exception))
Simon Glassda229092016-11-25 20:15:56 -0700945
946 def testPackFsp(self):
947 """Test that an image with a FSP binary can be created"""
948 data = self._DoReadFile('42_intel-fsp.dts')
949 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
950
951 def testPackCmc(self):
Bin Meng59ea8c22017-08-15 22:41:54 -0700952 """Test that an image with a CMC binary can be created"""
Simon Glassda229092016-11-25 20:15:56 -0700953 data = self._DoReadFile('43_intel-cmc.dts')
954 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Meng59ea8c22017-08-15 22:41:54 -0700955
956 def testPackVbt(self):
957 """Test that an image with a VBT binary can be created"""
958 data = self._DoReadFile('46_intel-vbt.dts')
959 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glass9fc60b42017-11-12 21:52:22 -0700960
Simon Glass56509842017-11-12 21:52:25 -0700961 def testSplBssPad(self):
962 """Test that we can pad SPL's BSS with zeros"""
Simon Glass6b187df2017-11-12 21:52:27 -0700963 # ELF file with a '__bss_size' symbol
964 with open(self.TestFile('bss_data')) as fd:
965 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass56509842017-11-12 21:52:25 -0700966 data = self._DoReadFile('47_spl_bss_pad.dts')
967 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
968
Simon Glassb50e5612017-11-13 18:54:54 -0700969 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
970 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
971 with self.assertRaises(ValueError) as e:
972 data = self._DoReadFile('47_spl_bss_pad.dts')
973 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
974 str(e.exception))
975
Simon Glass87722132017-11-12 21:52:26 -0700976 def testPackStart16Spl(self):
Simon Glass35b384c2018-09-14 04:57:10 -0600977 """Test that an image with an x86 start16 SPL region can be created"""
Simon Glass87722132017-11-12 21:52:26 -0700978 data = self._DoReadFile('48_x86-start16-spl.dts')
979 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
980
Simon Glass736bb0a2018-07-06 10:27:17 -0600981 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
982 """Helper function for microcode tests
Simon Glass6b187df2017-11-12 21:52:27 -0700983
984 We expect to see the following in the image, in order:
985 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
986 correct place
987 u-boot.dtb with the microcode removed
988 the microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600989
990 Args:
991 dts: Device tree file to use for test
992 ucode_second: True if the microsecond entry is second instead of
993 third
Simon Glass6b187df2017-11-12 21:52:27 -0700994 """
995 # ELF file with a '_dt_ucode_base_size' symbol
996 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
997 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass736bb0a2018-07-06 10:27:17 -0600998 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
999 ucode_second=ucode_second)
Simon Glass6b187df2017-11-12 21:52:27 -07001000 self.assertEqual('splnodtb with microc' + pos_and_size +
1001 'ter somewhere in here', first)
1002
Simon Glass736bb0a2018-07-06 10:27:17 -06001003 def testPackUbootSplMicrocode(self):
1004 """Test that x86 microcode can be handled correctly in SPL"""
1005 self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
1006
1007 def testPackUbootSplMicrocodeReorder(self):
1008 """Test that order doesn't matter for microcode entries
1009
1010 This is the same as testPackUbootSplMicrocode but when we process the
1011 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
1012 entry, so we reply on binman to try later.
1013 """
1014 self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
1015 ucode_second=True)
1016
Simon Glassca4f4ff2017-11-12 21:52:28 -07001017 def testPackMrc(self):
1018 """Test that an image with an MRC binary can be created"""
1019 data = self._DoReadFile('50_intel_mrc.dts')
1020 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
1021
Simon Glass47419ea2017-11-13 18:54:55 -07001022 def testSplDtb(self):
1023 """Test that an image with spl/u-boot-spl.dtb can be created"""
1024 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
1025 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
1026
Simon Glass4e6fdbe2017-11-13 18:54:56 -07001027 def testSplNoDtb(self):
1028 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
1029 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
1030 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
1031
Simon Glass19790632017-11-13 18:55:01 -07001032 def testSymbols(self):
1033 """Test binman can assign symbols embedded in U-Boot"""
1034 elf_fname = self.TestFile('u_boot_binman_syms')
1035 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
1036 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glass3ab95982018-08-01 15:22:37 -06001037 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass19790632017-11-13 18:55:01 -07001038
1039 with open(self.TestFile('u_boot_binman_syms')) as fd:
1040 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
1041 data = self._DoReadFile('53_symbols.dts')
1042 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
1043 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
1044 U_BOOT_DATA +
1045 sym_values + U_BOOT_SPL_DATA[16:])
1046 self.assertEqual(expected, data)
1047
Simon Glassdd57c132018-06-01 09:38:11 -06001048 def testPackUnitAddress(self):
1049 """Test that we support multiple binaries with the same name"""
1050 data = self._DoReadFile('54_unit_address.dts')
1051 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1052
Simon Glass18546952018-06-01 09:38:16 -06001053 def testSections(self):
1054 """Basic test of sections"""
1055 data = self._DoReadFile('55_sections.dts')
Simon Glass8122f392018-07-17 13:25:28 -06001056 expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
1057 U_BOOT_DATA + '&' * 4)
Simon Glass18546952018-06-01 09:38:16 -06001058 self.assertEqual(expected, data)
Simon Glass9fc60b42017-11-12 21:52:22 -07001059
Simon Glass3b0c3822018-06-01 09:38:20 -06001060 def testMap(self):
1061 """Tests outputting a map of the images"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001062 _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001063 self.assertEqual('''ImagePos Offset Size Name
106400000000 00000000 00000028 main-section
106500000000 00000000 00000010 section@0
106600000000 00000000 00000004 u-boot
106700000010 00000010 00000010 section@1
106800000010 00000000 00000004 u-boot
106900000020 00000020 00000004 section@2
107000000020 00000000 00000004 u-boot
Simon Glass3b0c3822018-06-01 09:38:20 -06001071''', map_data)
1072
Simon Glassc8d48ef2018-06-01 09:38:21 -06001073 def testNamePrefix(self):
1074 """Tests that name prefixes are used"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001075 _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001076 self.assertEqual('''ImagePos Offset Size Name
107700000000 00000000 00000028 main-section
107800000000 00000000 00000010 section@0
107900000000 00000000 00000004 ro-u-boot
108000000010 00000010 00000010 section@1
108100000010 00000000 00000004 rw-u-boot
Simon Glassc8d48ef2018-06-01 09:38:21 -06001082''', map_data)
1083
Simon Glass736bb0a2018-07-06 10:27:17 -06001084 def testUnknownContents(self):
1085 """Test that obtaining the contents works as expected"""
1086 with self.assertRaises(ValueError) as e:
1087 self._DoReadFile('57_unknown_contents.dts', True)
1088 self.assertIn("Section '/binman': Internal error: Could not complete "
1089 "processing of contents: remaining [<_testing.Entry__testing ",
1090 str(e.exception))
1091
Simon Glass5c890232018-07-06 10:27:19 -06001092 def testBadChangeSize(self):
1093 """Test that trying to change the size of an entry fails"""
1094 with self.assertRaises(ValueError) as e:
1095 self._DoReadFile('59_change_size.dts', True)
1096 self.assertIn("Node '/binman/_testing': Cannot update entry size from "
1097 '2 to 1', str(e.exception))
1098
Simon Glass16b8d6b2018-07-06 10:27:42 -06001099 def testUpdateFdt(self):
Simon Glass3ab95982018-08-01 15:22:37 -06001100 """Test that we can update the device tree with offset/size info"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001101 _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
1102 update_dtb=True)
Simon Glasscee02e62018-07-17 13:25:52 -06001103 dtb = fdt.Fdt(out_dtb_fname)
1104 dtb.Scan()
1105 props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos'])
Simon Glass16b8d6b2018-07-06 10:27:42 -06001106 self.assertEqual({
Simon Glassdbf6be92018-08-01 15:22:42 -06001107 'image-pos': 0,
Simon Glass8122f392018-07-17 13:25:28 -06001108 'offset': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001109 '_testing:offset': 32,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001110 '_testing:size': 1,
Simon Glassdbf6be92018-08-01 15:22:42 -06001111 '_testing:image-pos': 32,
Simon Glass3ab95982018-08-01 15:22:37 -06001112 'section@0/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001113 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001114 'section@0/u-boot:image-pos': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001115 'section@0:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001116 'section@0:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001117 'section@0:image-pos': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001118
Simon Glass3ab95982018-08-01 15:22:37 -06001119 'section@1/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001120 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001121 'section@1/u-boot:image-pos': 16,
Simon Glass3ab95982018-08-01 15:22:37 -06001122 'section@1:offset': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001123 'section@1:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001124 'section@1:image-pos': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001125 'size': 40
1126 }, props)
1127
1128 def testUpdateFdtBad(self):
1129 """Test that we detect when ProcessFdt never completes"""
1130 with self.assertRaises(ValueError) as e:
1131 self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
1132 self.assertIn('Could not complete processing of Fdt: remaining '
1133 '[<_testing.Entry__testing', str(e.exception))
Simon Glass5c890232018-07-06 10:27:19 -06001134
Simon Glass53af22a2018-07-17 13:25:32 -06001135 def testEntryArgs(self):
1136 """Test passing arguments to entries from the command line"""
1137 entry_args = {
1138 'test-str-arg': 'test1',
1139 'test-int-arg': '456',
1140 }
1141 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1142 self.assertIn('image', control.images)
1143 entry = control.images['image'].GetEntries()['_testing']
1144 self.assertEqual('test0', entry.test_str_fdt)
1145 self.assertEqual('test1', entry.test_str_arg)
1146 self.assertEqual(123, entry.test_int_fdt)
1147 self.assertEqual(456, entry.test_int_arg)
1148
1149 def testEntryArgsMissing(self):
1150 """Test missing arguments and properties"""
1151 entry_args = {
1152 'test-int-arg': '456',
1153 }
1154 self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args)
1155 entry = control.images['image'].GetEntries()['_testing']
1156 self.assertEqual('test0', entry.test_str_fdt)
1157 self.assertEqual(None, entry.test_str_arg)
1158 self.assertEqual(None, entry.test_int_fdt)
1159 self.assertEqual(456, entry.test_int_arg)
1160
1161 def testEntryArgsRequired(self):
1162 """Test missing arguments and properties"""
1163 entry_args = {
1164 'test-int-arg': '456',
1165 }
1166 with self.assertRaises(ValueError) as e:
1167 self._DoReadFileDtb('64_entry_args_required.dts')
1168 self.assertIn("Node '/binman/_testing': Missing required "
1169 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1170 str(e.exception))
1171
1172 def testEntryArgsInvalidFormat(self):
1173 """Test that an invalid entry-argument format is detected"""
1174 args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value']
1175 with self.assertRaises(ValueError) as e:
1176 self._DoBinman(*args)
1177 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1178
1179 def testEntryArgsInvalidInteger(self):
1180 """Test that an invalid entry-argument integer is detected"""
1181 entry_args = {
1182 'test-int-arg': 'abc',
1183 }
1184 with self.assertRaises(ValueError) as e:
1185 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1186 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1187 "'test-int-arg' (value 'abc') to integer",
1188 str(e.exception))
1189
1190 def testEntryArgsInvalidDatatype(self):
1191 """Test that an invalid entry-argument datatype is detected
1192
1193 This test could be written in entry_test.py except that it needs
1194 access to control.entry_args, which seems more than that module should
1195 be able to see.
1196 """
1197 entry_args = {
1198 'test-bad-datatype-arg': '12',
1199 }
1200 with self.assertRaises(ValueError) as e:
1201 self._DoReadFileDtb('65_entry_args_unknown_datatype.dts',
1202 entry_args=entry_args)
1203 self.assertIn('GetArg() internal error: Unknown data type ',
1204 str(e.exception))
1205
Simon Glassbb748372018-07-17 13:25:33 -06001206 def testText(self):
1207 """Test for a text entry type"""
1208 entry_args = {
1209 'test-id': TEXT_DATA,
1210 'test-id2': TEXT_DATA2,
1211 'test-id3': TEXT_DATA3,
1212 }
1213 data, _, _, _ = self._DoReadFileDtb('66_text.dts',
1214 entry_args=entry_args)
1215 expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 +
1216 TEXT_DATA3 + 'some text')
1217 self.assertEqual(expected, data)
1218
Simon Glassfd8d1f72018-07-17 13:25:36 -06001219 def testEntryDocs(self):
1220 """Test for creation of entry documentation"""
1221 with test_util.capture_sys_output() as (stdout, stderr):
1222 control.WriteEntryDocs(binman.GetEntryModules())
1223 self.assertTrue(len(stdout.getvalue()) > 0)
1224
1225 def testEntryDocsMissing(self):
1226 """Test handling of missing entry documentation"""
1227 with self.assertRaises(ValueError) as e:
1228 with test_util.capture_sys_output() as (stdout, stderr):
1229 control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
1230 self.assertIn('Documentation is missing for modules: u_boot',
1231 str(e.exception))
1232
Simon Glass11e36cc2018-07-17 13:25:38 -06001233 def testFmap(self):
1234 """Basic test of generation of a flashrom fmap"""
1235 data = self._DoReadFile('67_fmap.dts')
1236 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
1237 expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12
1238 self.assertEqual(expected, data[:32])
1239 self.assertEqual('__FMAP__', fhdr.signature)
1240 self.assertEqual(1, fhdr.ver_major)
1241 self.assertEqual(0, fhdr.ver_minor)
1242 self.assertEqual(0, fhdr.base)
1243 self.assertEqual(16 + 16 +
1244 fmap_util.FMAP_HEADER_LEN +
1245 fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size)
1246 self.assertEqual('FMAP', fhdr.name)
1247 self.assertEqual(3, fhdr.nareas)
1248 for fentry in fentries:
1249 self.assertEqual(0, fentry.flags)
1250
1251 self.assertEqual(0, fentries[0].offset)
1252 self.assertEqual(4, fentries[0].size)
1253 self.assertEqual('RO_U_BOOT', fentries[0].name)
1254
1255 self.assertEqual(16, fentries[1].offset)
1256 self.assertEqual(4, fentries[1].size)
1257 self.assertEqual('RW_U_BOOT', fentries[1].name)
1258
1259 self.assertEqual(32, fentries[2].offset)
1260 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1261 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
1262 self.assertEqual('FMAP', fentries[2].name)
1263
Simon Glassec127af2018-07-17 13:25:39 -06001264 def testBlobNamedByArg(self):
1265 """Test we can add a blob with the filename coming from an entry arg"""
1266 entry_args = {
1267 'cros-ec-rw-path': 'ecrw.bin',
1268 }
1269 data, _, _, _ = self._DoReadFileDtb('68_blob_named_by_arg.dts',
1270 entry_args=entry_args)
1271
Simon Glass3af8e492018-07-17 13:25:40 -06001272 def testFill(self):
1273 """Test for an fill entry type"""
1274 data = self._DoReadFile('69_fill.dts')
1275 expected = 8 * chr(0xff) + 8 * chr(0)
1276 self.assertEqual(expected, data)
1277
1278 def testFillNoSize(self):
1279 """Test for an fill entry type with no size"""
1280 with self.assertRaises(ValueError) as e:
1281 self._DoReadFile('70_fill_no_size.dts')
1282 self.assertIn("'fill' entry must have a size property",
1283 str(e.exception))
1284
Simon Glass0ef87aa2018-07-17 13:25:44 -06001285 def _HandleGbbCommand(self, pipe_list):
1286 """Fake calls to the futility utility"""
1287 if pipe_list[0][0] == 'futility':
1288 fname = pipe_list[0][-1]
1289 # Append our GBB data to the file, which will happen every time the
1290 # futility command is called.
1291 with open(fname, 'a') as fd:
1292 fd.write(GBB_DATA)
1293 return command.CommandResult()
1294
1295 def testGbb(self):
1296 """Test for the Chromium OS Google Binary Block"""
1297 command.test_result = self._HandleGbbCommand
1298 entry_args = {
1299 'keydir': 'devkeys',
1300 'bmpblk': 'bmpblk.bin',
1301 }
1302 data, _, _, _ = self._DoReadFileDtb('71_gbb.dts', entry_args=entry_args)
1303
1304 # Since futility
1305 expected = GBB_DATA + GBB_DATA + 8 * chr(0) + (0x2180 - 16) * chr(0)
1306 self.assertEqual(expected, data)
1307
1308 def testGbbTooSmall(self):
1309 """Test for the Chromium OS Google Binary Block being large enough"""
1310 with self.assertRaises(ValueError) as e:
1311 self._DoReadFileDtb('72_gbb_too_small.dts')
1312 self.assertIn("Node '/binman/gbb': GBB is too small",
1313 str(e.exception))
1314
1315 def testGbbNoSize(self):
1316 """Test for the Chromium OS Google Binary Block having a size"""
1317 with self.assertRaises(ValueError) as e:
1318 self._DoReadFileDtb('73_gbb_no_size.dts')
1319 self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
1320 str(e.exception))
1321
Simon Glass24d0d3c2018-07-17 13:25:47 -06001322 def _HandleVblockCommand(self, pipe_list):
1323 """Fake calls to the futility utility"""
1324 if pipe_list[0][0] == 'futility':
1325 fname = pipe_list[0][3]
Simon Glassa326b492018-09-14 04:57:11 -06001326 with open(fname, 'wb') as fd:
Simon Glass24d0d3c2018-07-17 13:25:47 -06001327 fd.write(VBLOCK_DATA)
1328 return command.CommandResult()
1329
1330 def testVblock(self):
1331 """Test for the Chromium OS Verified Boot Block"""
1332 command.test_result = self._HandleVblockCommand
1333 entry_args = {
1334 'keydir': 'devkeys',
1335 }
1336 data, _, _, _ = self._DoReadFileDtb('74_vblock.dts',
1337 entry_args=entry_args)
1338 expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA
1339 self.assertEqual(expected, data)
1340
1341 def testVblockNoContent(self):
1342 """Test we detect a vblock which has no content to sign"""
1343 with self.assertRaises(ValueError) as e:
1344 self._DoReadFile('75_vblock_no_content.dts')
1345 self.assertIn("Node '/binman/vblock': Vblock must have a 'content' "
1346 'property', str(e.exception))
1347
1348 def testVblockBadPhandle(self):
1349 """Test that we detect a vblock with an invalid phandle in contents"""
1350 with self.assertRaises(ValueError) as e:
1351 self._DoReadFile('76_vblock_bad_phandle.dts')
1352 self.assertIn("Node '/binman/vblock': Cannot find node for phandle "
1353 '1000', str(e.exception))
1354
1355 def testVblockBadEntry(self):
1356 """Test that we detect an entry that points to a non-entry"""
1357 with self.assertRaises(ValueError) as e:
1358 self._DoReadFile('77_vblock_bad_entry.dts')
1359 self.assertIn("Node '/binman/vblock': Cannot find entry for node "
1360 "'other'", str(e.exception))
1361
Simon Glassb8ef5b62018-07-17 13:25:48 -06001362 def testTpl(self):
1363 """Test that an image with TPL and ots device tree can be created"""
1364 # ELF file with a '__bss_size' symbol
1365 with open(self.TestFile('bss_data')) as fd:
1366 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
1367 data = self._DoReadFile('78_u_boot_tpl.dts')
1368 self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
1369
Simon Glass15a587c2018-07-17 13:25:51 -06001370 def testUsesPos(self):
1371 """Test that the 'pos' property cannot be used anymore"""
1372 with self.assertRaises(ValueError) as e:
1373 data = self._DoReadFile('79_uses_pos.dts')
1374 self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of "
1375 "'pos'", str(e.exception))
1376
Simon Glassd178eab2018-09-14 04:57:08 -06001377 def testFillZero(self):
1378 """Test for an fill entry type with a size of 0"""
1379 data = self._DoReadFile('80_fill_empty.dts')
1380 self.assertEqual(chr(0) * 16, data)
1381
Simon Glass0b489362018-09-14 04:57:09 -06001382 def testTextMissing(self):
1383 """Test for a text entry type where there is no text"""
1384 with self.assertRaises(ValueError) as e:
1385 self._DoReadFileDtb('66_text.dts',)
1386 self.assertIn("Node '/binman/text': No value provided for text label "
1387 "'test-id'", str(e.exception))
1388
Simon Glass35b384c2018-09-14 04:57:10 -06001389 def testPackStart16Tpl(self):
1390 """Test that an image with an x86 start16 TPL region can be created"""
1391 data = self._DoReadFile('81_x86-start16-tpl.dts')
1392 self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)])
1393
Simon Glass0bfa7b02018-09-14 04:57:12 -06001394 def testSelectImage(self):
1395 """Test that we can select which images to build"""
1396 with test_util.capture_sys_output() as (stdout, stderr):
1397 retcode = self._DoTestFile('06_dual_image.dts', images=['image2'])
1398 self.assertEqual(0, retcode)
1399 self.assertIn('Skipping images: image1', stdout.getvalue())
1400
1401 self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin')))
1402 self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin')))
1403
Simon Glass53af22a2018-07-17 13:25:32 -06001404
Simon Glass9fc60b42017-11-12 21:52:22 -07001405if __name__ == "__main__":
1406 unittest.main()