blob: 75e9a2143cad02139ade6474fea692c700ee1cbe [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass57454f42016-11-25 20:15:52 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass57454f42016-11-25 20:15:52 -07005# To run a single test, change to this directory, and:
6#
7# python -m unittest func_test.TestFunctional.testHelp
8
9from optparse import OptionParser
10import os
11import shutil
12import struct
13import sys
14import tempfile
15import unittest
16
17import binman
18import cmdline
19import command
20import control
Simon Glass4ca8e042017-11-13 18:55:01 -070021import elf
Simon Glassa9440932017-05-27 07:38:30 -060022import fdt
Simon Glass57454f42016-11-25 20:15:52 -070023import fdt_util
Simon Glass704784b2018-07-17 13:25:38 -060024import fmap_util
Simon Glass969616c2018-07-17 13:25:36 -060025import test_util
Simon Glass29aa7362018-09-14 04:57:19 -060026import state
Simon Glass57454f42016-11-25 20:15:52 -070027import tools
28import tout
29
30# Contents of test files, corresponding to different entry types
Simon Glass3d274232017-11-12 21:52:27 -070031U_BOOT_DATA = '1234'
32U_BOOT_IMG_DATA = 'img'
Simon Glassd6051522017-11-13 18:54:59 -070033U_BOOT_SPL_DATA = '56780123456789abcde'
Simon Glass8425a1f2018-07-17 13:25:48 -060034U_BOOT_TPL_DATA = 'tpl'
Simon Glass3d274232017-11-12 21:52:27 -070035BLOB_DATA = '89'
36ME_DATA = '0abcd'
37VGA_DATA = 'vga'
38U_BOOT_DTB_DATA = 'udtb'
Simon Glass9aa6a6f2017-11-13 18:54:55 -070039U_BOOT_SPL_DTB_DATA = 'spldtb'
Simon Glass8425a1f2018-07-17 13:25:48 -060040U_BOOT_TPL_DTB_DATA = 'tpldtb'
Simon Glass3d274232017-11-12 21:52:27 -070041X86_START16_DATA = 'start16'
42X86_START16_SPL_DATA = 'start16spl'
Simon Glassed40e962018-09-14 04:57:10 -060043X86_START16_TPL_DATA = 'start16tpl'
Simon Glass3d274232017-11-12 21:52:27 -070044U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
45U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
46FSP_DATA = 'fsp'
47CMC_DATA = 'cmc'
48VBT_DATA = 'vbt'
Simon Glassa409c932017-11-12 21:52:28 -070049MRC_DATA = 'mrc'
Simon Glass2ca52032018-07-17 13:25:33 -060050TEXT_DATA = 'text'
51TEXT_DATA2 = 'text2'
52TEXT_DATA3 = 'text3'
Simon Glassdb168d42018-07-17 13:25:39 -060053CROS_EC_RW_DATA = 'ecrw'
Simon Glassc1ae83c2018-07-17 13:25:44 -060054GBB_DATA = 'gbbd'
55BMPBLK_DATA = 'bmp'
Simon Glass5c350162018-07-17 13:25:47 -060056VBLOCK_DATA = 'vblk'
Simon Glassdb168d42018-07-17 13:25:39 -060057
Simon Glass57454f42016-11-25 20:15:52 -070058
59class TestFunctional(unittest.TestCase):
60 """Functional tests for binman
61
62 Most of these use a sample .dts file to build an image and then check
63 that it looks correct. The sample files are in the test/ subdirectory
64 and are numbered.
65
66 For each entry type a very small test file is created using fixed
67 string contents. This makes it easy to test that things look right, and
68 debug problems.
69
70 In some cases a 'real' file must be used - these are also supplied in
71 the test/ diurectory.
72 """
73 @classmethod
74 def setUpClass(self):
Simon Glassb3393262017-11-12 21:52:20 -070075 global entry
76 import entry
77
Simon Glass57454f42016-11-25 20:15:52 -070078 # Handle the case where argv[0] is 'python'
79 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
80 self._binman_pathname = os.path.join(self._binman_dir, 'binman')
81
82 # Create a temporary directory for input files
83 self._indir = tempfile.mkdtemp(prefix='binmant.')
84
85 # Create some test files
86 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
87 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
88 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
Simon Glass8425a1f2018-07-17 13:25:48 -060089 TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA)
Simon Glass57454f42016-11-25 20:15:52 -070090 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
Simon Glass72232452016-11-25 20:15:53 -070091 TestFunctional._MakeInputFile('me.bin', ME_DATA)
92 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glass8425a1f2018-07-17 13:25:48 -060093 self._ResetDtbs()
Simon Glass72232452016-11-25 20:15:53 -070094 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Simon Glasse83679d2017-11-12 21:52:26 -070095 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
96 X86_START16_SPL_DATA)
Simon Glassed40e962018-09-14 04:57:10 -060097 TestFunctional._MakeInputFile('tpl/u-boot-x86-16bit-tpl.bin',
98 X86_START16_TPL_DATA)
Simon Glass57454f42016-11-25 20:15:52 -070099 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass3d274232017-11-12 21:52:27 -0700100 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
101 U_BOOT_SPL_NODTB_DATA)
Simon Glassb4176d42016-11-25 20:15:56 -0700102 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
103 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Mengd7bcdf52017-08-15 22:41:54 -0700104 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassa409c932017-11-12 21:52:28 -0700105 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glassdb168d42018-07-17 13:25:39 -0600106 TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
Simon Glassc1ae83c2018-07-17 13:25:44 -0600107 TestFunctional._MakeInputDir('devkeys')
108 TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
Simon Glass57454f42016-11-25 20:15:52 -0700109 self._output_setup = False
110
Simon Glass72232452016-11-25 20:15:53 -0700111 # ELF file with a '_dt_ucode_base_size' symbol
112 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
113 TestFunctional._MakeInputFile('u-boot', fd.read())
114
115 # Intel flash descriptor file
116 with open(self.TestFile('descriptor.bin')) as fd:
117 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
118
Simon Glass57454f42016-11-25 20:15:52 -0700119 @classmethod
120 def tearDownClass(self):
121 """Remove the temporary input directory and its contents"""
122 if self._indir:
123 shutil.rmtree(self._indir)
124 self._indir = None
125
126 def setUp(self):
127 # Enable this to turn on debugging output
128 # tout.Init(tout.DEBUG)
129 command.test_result = None
130
131 def tearDown(self):
132 """Remove the temporary output directory"""
133 tools._FinaliseForTest()
134
Simon Glass8425a1f2018-07-17 13:25:48 -0600135 @classmethod
136 def _ResetDtbs(self):
137 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
138 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
139 TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA)
140
Simon Glass57454f42016-11-25 20:15:52 -0700141 def _RunBinman(self, *args, **kwargs):
142 """Run binman using the command line
143
144 Args:
145 Arguments to pass, as a list of strings
146 kwargs: Arguments to pass to Command.RunPipe()
147 """
148 result = command.RunPipe([[self._binman_pathname] + list(args)],
149 capture=True, capture_stderr=True, raise_on_error=False)
150 if result.return_code and kwargs.get('raise_on_error', True):
151 raise Exception("Error running '%s': %s" % (' '.join(args),
152 result.stdout + result.stderr))
153 return result
154
155 def _DoBinman(self, *args):
156 """Run binman using directly (in the same process)
157
158 Args:
159 Arguments to pass, as a list of strings
160 Returns:
161 Return value (0 for success)
162 """
Simon Glass075a45c2017-11-13 18:55:00 -0700163 args = list(args)
164 if '-D' in sys.argv:
165 args = args + ['-D']
166 (options, args) = cmdline.ParseArgs(args)
Simon Glass57454f42016-11-25 20:15:52 -0700167 options.pager = 'binman-invalid-pager'
168 options.build_dir = self._indir
169
170 # For testing, you can force an increase in verbosity here
171 # options.verbosity = tout.DEBUG
172 return control.Binman(options, args)
173
Simon Glass91710b32018-07-17 13:25:32 -0600174 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
Simon Glass31402012018-09-14 04:57:23 -0600175 entry_args=None, images=None, use_real_dtb=False):
Simon Glass57454f42016-11-25 20:15:52 -0700176 """Run binman with a given test file
177
178 Args:
Simon Glass1e324002018-06-01 09:38:19 -0600179 fname: Device-tree source filename to use (e.g. 05_simple.dts)
180 debug: True to enable debugging output
Simon Glass30732662018-06-01 09:38:20 -0600181 map: True to output map files for the images
Simon Glasse8561af2018-08-01 15:22:37 -0600182 update_dtb: Update the offset and size of each entry in the device
Simon Glassa87014e2018-07-06 10:27:42 -0600183 tree before packing it into the image
Simon Glass3b376c32018-09-14 04:57:12 -0600184 entry_args: Dict of entry args to supply to binman
185 key: arg name
186 value: value of that arg
187 images: List of image names to build
Simon Glass57454f42016-11-25 20:15:52 -0700188 """
Simon Glass075a45c2017-11-13 18:55:00 -0700189 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
190 if debug:
191 args.append('-D')
Simon Glass30732662018-06-01 09:38:20 -0600192 if map:
193 args.append('-m')
Simon Glassa87014e2018-07-06 10:27:42 -0600194 if update_dtb:
195 args.append('-up')
Simon Glass31402012018-09-14 04:57:23 -0600196 if not use_real_dtb:
197 args.append('--fake-dtb')
Simon Glass91710b32018-07-17 13:25:32 -0600198 if entry_args:
199 for arg, value in entry_args.iteritems():
200 args.append('-a%s=%s' % (arg, value))
Simon Glass3b376c32018-09-14 04:57:12 -0600201 if images:
202 for image in images:
203 args += ['-i', image]
Simon Glass075a45c2017-11-13 18:55:00 -0700204 return self._DoBinman(*args)
Simon Glass57454f42016-11-25 20:15:52 -0700205
206 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glass72232452016-11-25 20:15:53 -0700207 """Set up a new test device-tree file
208
209 The given file is compiled and set up as the device tree to be used
210 for ths test.
211
212 Args:
213 fname: Filename of .dts file to read
Simon Glass1e324002018-06-01 09:38:19 -0600214 outfile: Output filename for compiled device-tree binary
Simon Glass72232452016-11-25 20:15:53 -0700215
216 Returns:
Simon Glass1e324002018-06-01 09:38:19 -0600217 Contents of device-tree binary
Simon Glass72232452016-11-25 20:15:53 -0700218 """
Simon Glass57454f42016-11-25 20:15:52 -0700219 if not self._output_setup:
220 tools.PrepareOutputDir(self._indir, True)
221 self._output_setup = True
222 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
223 with open(dtb) as fd:
224 data = fd.read()
225 TestFunctional._MakeInputFile(outfile, data)
Simon Glass72232452016-11-25 20:15:53 -0700226 return data
Simon Glass57454f42016-11-25 20:15:52 -0700227
Simon Glassa87014e2018-07-06 10:27:42 -0600228 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass91710b32018-07-17 13:25:32 -0600229 update_dtb=False, entry_args=None):
Simon Glass57454f42016-11-25 20:15:52 -0700230 """Run binman and return the resulting image
231
232 This runs binman with a given test file and then reads the resulting
233 output file. It is a shortcut function since most tests need to do
234 these steps.
235
236 Raises an assertion failure if binman returns a non-zero exit code.
237
238 Args:
Simon Glass1e324002018-06-01 09:38:19 -0600239 fname: Device-tree source filename to use (e.g. 05_simple.dts)
Simon Glass57454f42016-11-25 20:15:52 -0700240 use_real_dtb: True to use the test file as the contents of
241 the u-boot-dtb entry. Normally this is not needed and the
242 test contents (the U_BOOT_DTB_DATA string) can be used.
243 But in some test we need the real contents.
Simon Glass30732662018-06-01 09:38:20 -0600244 map: True to output map files for the images
Simon Glasse8561af2018-08-01 15:22:37 -0600245 update_dtb: Update the offset and size of each entry in the device
Simon Glassa87014e2018-07-06 10:27:42 -0600246 tree before packing it into the image
Simon Glass72232452016-11-25 20:15:53 -0700247
248 Returns:
249 Tuple:
250 Resulting image contents
251 Device tree contents
Simon Glass30732662018-06-01 09:38:20 -0600252 Map data showing contents of image (or None if none)
Simon Glassdef77b52018-07-17 13:25:27 -0600253 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass57454f42016-11-25 20:15:52 -0700254 """
Simon Glass72232452016-11-25 20:15:53 -0700255 dtb_data = None
Simon Glass57454f42016-11-25 20:15:52 -0700256 # Use the compiled test file as the u-boot-dtb input
257 if use_real_dtb:
Simon Glass72232452016-11-25 20:15:53 -0700258 dtb_data = self._SetupDtb(fname)
Simon Glass57454f42016-11-25 20:15:52 -0700259
260 try:
Simon Glass91710b32018-07-17 13:25:32 -0600261 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
262 entry_args=entry_args)
Simon Glass57454f42016-11-25 20:15:52 -0700263 self.assertEqual(0, retcode)
Simon Glass29aa7362018-09-14 04:57:19 -0600264 out_dtb_fname = state.GetFdtPath('u-boot.dtb')
Simon Glass57454f42016-11-25 20:15:52 -0700265
266 # Find the (only) image, read it and return its contents
267 image = control.images['image']
Simon Glassa87014e2018-07-06 10:27:42 -0600268 image_fname = tools.GetOutputFilename('image.bin')
269 self.assertTrue(os.path.exists(image_fname))
Simon Glass30732662018-06-01 09:38:20 -0600270 if map:
271 map_fname = tools.GetOutputFilename('image.map')
272 with open(map_fname) as fd:
273 map_data = fd.read()
274 else:
275 map_data = None
Simon Glassa87014e2018-07-06 10:27:42 -0600276 with open(image_fname) as fd:
277 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass57454f42016-11-25 20:15:52 -0700278 finally:
279 # Put the test file back
280 if use_real_dtb:
Simon Glass8425a1f2018-07-17 13:25:48 -0600281 self._ResetDtbs()
Simon Glass57454f42016-11-25 20:15:52 -0700282
Simon Glass72232452016-11-25 20:15:53 -0700283 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass1e324002018-06-01 09:38:19 -0600284 """Helper function which discards the device-tree binary
285
286 Args:
287 fname: Device-tree source filename to use (e.g. 05_simple.dts)
288 use_real_dtb: True to use the test file as the contents of
289 the u-boot-dtb entry. Normally this is not needed and the
290 test contents (the U_BOOT_DTB_DATA string) can be used.
291 But in some test we need the real contents.
Simon Glassdef77b52018-07-17 13:25:27 -0600292
293 Returns:
294 Resulting image contents
Simon Glass1e324002018-06-01 09:38:19 -0600295 """
Simon Glass72232452016-11-25 20:15:53 -0700296 return self._DoReadFileDtb(fname, use_real_dtb)[0]
297
Simon Glass57454f42016-11-25 20:15:52 -0700298 @classmethod
299 def _MakeInputFile(self, fname, contents):
300 """Create a new test input file, creating directories as needed
301
302 Args:
Simon Glasse8561af2018-08-01 15:22:37 -0600303 fname: Filename to create
Simon Glass57454f42016-11-25 20:15:52 -0700304 contents: File contents to write in to the file
305 Returns:
306 Full pathname of file created
307 """
308 pathname = os.path.join(self._indir, fname)
309 dirname = os.path.dirname(pathname)
310 if dirname and not os.path.exists(dirname):
311 os.makedirs(dirname)
312 with open(pathname, 'wb') as fd:
313 fd.write(contents)
314 return pathname
315
316 @classmethod
Simon Glassc1ae83c2018-07-17 13:25:44 -0600317 def _MakeInputDir(self, dirname):
318 """Create a new test input directory, creating directories as needed
319
320 Args:
321 dirname: Directory name to create
322
323 Returns:
324 Full pathname of directory created
325 """
326 pathname = os.path.join(self._indir, dirname)
327 if not os.path.exists(pathname):
328 os.makedirs(pathname)
329 return pathname
330
331 @classmethod
Simon Glass57454f42016-11-25 20:15:52 -0700332 def TestFile(self, fname):
333 return os.path.join(self._binman_dir, 'test', fname)
334
335 def AssertInList(self, grep_list, target):
336 """Assert that at least one of a list of things is in a target
337
338 Args:
339 grep_list: List of strings to check
340 target: Target string
341 """
342 for grep in grep_list:
343 if grep in target:
344 return
345 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
346
347 def CheckNoGaps(self, entries):
348 """Check that all entries fit together without gaps
349
350 Args:
351 entries: List of entries to check
352 """
Simon Glasse8561af2018-08-01 15:22:37 -0600353 offset = 0
Simon Glass57454f42016-11-25 20:15:52 -0700354 for entry in entries.values():
Simon Glasse8561af2018-08-01 15:22:37 -0600355 self.assertEqual(offset, entry.offset)
356 offset += entry.size
Simon Glass57454f42016-11-25 20:15:52 -0700357
Simon Glass72232452016-11-25 20:15:53 -0700358 def GetFdtLen(self, dtb):
Simon Glass1e324002018-06-01 09:38:19 -0600359 """Get the totalsize field from a device-tree binary
Simon Glass72232452016-11-25 20:15:53 -0700360
361 Args:
Simon Glass1e324002018-06-01 09:38:19 -0600362 dtb: Device-tree binary contents
Simon Glass72232452016-11-25 20:15:53 -0700363
364 Returns:
Simon Glass1e324002018-06-01 09:38:19 -0600365 Total size of device-tree binary, from the header
Simon Glass72232452016-11-25 20:15:53 -0700366 """
367 return struct.unpack('>L', dtb[4:8])[0]
368
Simon Glass5463a6a2018-07-17 13:25:52 -0600369 def _GetPropTree(self, dtb, prop_names):
Simon Glassa87014e2018-07-06 10:27:42 -0600370 def AddNode(node, path):
371 if node.name != '/':
372 path += '/' + node.name
Simon Glassa87014e2018-07-06 10:27:42 -0600373 for subnode in node.subnodes:
374 for prop in subnode.props.values():
Simon Glass5463a6a2018-07-17 13:25:52 -0600375 if prop.name in prop_names:
Simon Glassa87014e2018-07-06 10:27:42 -0600376 prop_path = path + '/' + subnode.name + ':' + prop.name
377 tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
378 prop.value)
Simon Glassa87014e2018-07-06 10:27:42 -0600379 AddNode(subnode, path)
380
381 tree = {}
Simon Glassa87014e2018-07-06 10:27:42 -0600382 AddNode(dtb.GetRoot(), '')
383 return tree
384
Simon Glass57454f42016-11-25 20:15:52 -0700385 def testRun(self):
386 """Test a basic run with valid args"""
387 result = self._RunBinman('-h')
388
389 def testFullHelp(self):
390 """Test that the full help is displayed with -H"""
391 result = self._RunBinman('-H')
392 help_file = os.path.join(self._binman_dir, 'README')
Tom Rinic3c0b6d2018-01-16 15:29:50 -0500393 # Remove possible extraneous strings
394 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
395 gothelp = result.stdout.replace(extra, '')
396 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass57454f42016-11-25 20:15:52 -0700397 self.assertEqual(0, len(result.stderr))
398 self.assertEqual(0, result.return_code)
399
400 def testFullHelpInternal(self):
401 """Test that the full help is displayed with -H"""
402 try:
403 command.test_result = command.CommandResult()
404 result = self._DoBinman('-H')
405 help_file = os.path.join(self._binman_dir, 'README')
406 finally:
407 command.test_result = None
408
409 def testHelp(self):
410 """Test that the basic help is displayed with -h"""
411 result = self._RunBinman('-h')
412 self.assertTrue(len(result.stdout) > 200)
413 self.assertEqual(0, len(result.stderr))
414 self.assertEqual(0, result.return_code)
415
Simon Glass57454f42016-11-25 20:15:52 -0700416 def testBoard(self):
417 """Test that we can run it with a specific board"""
418 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
419 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
420 result = self._DoBinman('-b', 'sandbox')
421 self.assertEqual(0, result)
422
423 def testNeedBoard(self):
424 """Test that we get an error when no board ius supplied"""
425 with self.assertRaises(ValueError) as e:
426 result = self._DoBinman()
427 self.assertIn("Must provide a board to process (use -b <board>)",
428 str(e.exception))
429
430 def testMissingDt(self):
Simon Glass1e324002018-06-01 09:38:19 -0600431 """Test that an invalid device-tree file generates an error"""
Simon Glass57454f42016-11-25 20:15:52 -0700432 with self.assertRaises(Exception) as e:
433 self._RunBinman('-d', 'missing_file')
434 # We get one error from libfdt, and a different one from fdtget.
435 self.AssertInList(["Couldn't open blob from 'missing_file'",
436 'No such file or directory'], str(e.exception))
437
438 def testBrokenDt(self):
Simon Glass1e324002018-06-01 09:38:19 -0600439 """Test that an invalid device-tree source file generates an error
Simon Glass57454f42016-11-25 20:15:52 -0700440
441 Since this is a source file it should be compiled and the error
442 will come from the device-tree compiler (dtc).
443 """
444 with self.assertRaises(Exception) as e:
445 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
446 self.assertIn("FATAL ERROR: Unable to parse input tree",
447 str(e.exception))
448
449 def testMissingNode(self):
450 """Test that a device tree without a 'binman' node generates an error"""
451 with self.assertRaises(Exception) as e:
452 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
453 self.assertIn("does not have a 'binman' node", str(e.exception))
454
455 def testEmpty(self):
456 """Test that an empty binman node works OK (i.e. does nothing)"""
457 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
458 self.assertEqual(0, len(result.stderr))
459 self.assertEqual(0, result.return_code)
460
461 def testInvalidEntry(self):
462 """Test that an invalid entry is flagged"""
463 with self.assertRaises(Exception) as e:
464 result = self._RunBinman('-d',
465 self.TestFile('04_invalid_entry.dts'))
Simon Glass57454f42016-11-25 20:15:52 -0700466 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
467 "'/binman/not-a-valid-type'", str(e.exception))
468
469 def testSimple(self):
470 """Test a simple binman with a single file"""
471 data = self._DoReadFile('05_simple.dts')
472 self.assertEqual(U_BOOT_DATA, data)
473
Simon Glass075a45c2017-11-13 18:55:00 -0700474 def testSimpleDebug(self):
475 """Test a simple binman run with debugging enabled"""
476 data = self._DoTestFile('05_simple.dts', debug=True)
477
Simon Glass57454f42016-11-25 20:15:52 -0700478 def testDual(self):
479 """Test that we can handle creating two images
480
481 This also tests image padding.
482 """
483 retcode = self._DoTestFile('06_dual_image.dts')
484 self.assertEqual(0, retcode)
485
486 image = control.images['image1']
487 self.assertEqual(len(U_BOOT_DATA), image._size)
488 fname = tools.GetOutputFilename('image1.bin')
489 self.assertTrue(os.path.exists(fname))
490 with open(fname) as fd:
491 data = fd.read()
492 self.assertEqual(U_BOOT_DATA, data)
493
494 image = control.images['image2']
495 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
496 fname = tools.GetOutputFilename('image2.bin')
497 self.assertTrue(os.path.exists(fname))
498 with open(fname) as fd:
499 data = fd.read()
500 self.assertEqual(U_BOOT_DATA, data[3:7])
501 self.assertEqual(chr(0) * 3, data[:3])
502 self.assertEqual(chr(0) * 5, data[7:])
503
504 def testBadAlign(self):
505 """Test that an invalid alignment value is detected"""
506 with self.assertRaises(ValueError) as e:
507 self._DoTestFile('07_bad_align.dts')
508 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
509 "of two", str(e.exception))
510
511 def testPackSimple(self):
512 """Test that packing works as expected"""
513 retcode = self._DoTestFile('08_pack.dts')
514 self.assertEqual(0, retcode)
515 self.assertIn('image', control.images)
516 image = control.images['image']
Simon Glasseca32212018-06-01 09:38:12 -0600517 entries = image.GetEntries()
Simon Glass57454f42016-11-25 20:15:52 -0700518 self.assertEqual(5, len(entries))
519
520 # First u-boot
521 self.assertIn('u-boot', entries)
522 entry = entries['u-boot']
Simon Glasse8561af2018-08-01 15:22:37 -0600523 self.assertEqual(0, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700524 self.assertEqual(len(U_BOOT_DATA), entry.size)
525
526 # Second u-boot, aligned to 16-byte boundary
527 self.assertIn('u-boot-align', entries)
528 entry = entries['u-boot-align']
Simon Glasse8561af2018-08-01 15:22:37 -0600529 self.assertEqual(16, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700530 self.assertEqual(len(U_BOOT_DATA), entry.size)
531
532 # Third u-boot, size 23 bytes
533 self.assertIn('u-boot-size', entries)
534 entry = entries['u-boot-size']
Simon Glasse8561af2018-08-01 15:22:37 -0600535 self.assertEqual(20, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700536 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
537 self.assertEqual(23, entry.size)
538
539 # Fourth u-boot, placed immediate after the above
540 self.assertIn('u-boot-next', entries)
541 entry = entries['u-boot-next']
Simon Glasse8561af2018-08-01 15:22:37 -0600542 self.assertEqual(43, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700543 self.assertEqual(len(U_BOOT_DATA), entry.size)
544
Simon Glasse8561af2018-08-01 15:22:37 -0600545 # Fifth u-boot, placed at a fixed offset
Simon Glass57454f42016-11-25 20:15:52 -0700546 self.assertIn('u-boot-fixed', entries)
547 entry = entries['u-boot-fixed']
Simon Glasse8561af2018-08-01 15:22:37 -0600548 self.assertEqual(61, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700549 self.assertEqual(len(U_BOOT_DATA), entry.size)
550
551 self.assertEqual(65, image._size)
552
553 def testPackExtra(self):
554 """Test that extra packing feature works as expected"""
555 retcode = self._DoTestFile('09_pack_extra.dts')
556
557 self.assertEqual(0, retcode)
558 self.assertIn('image', control.images)
559 image = control.images['image']
Simon Glasseca32212018-06-01 09:38:12 -0600560 entries = image.GetEntries()
Simon Glass57454f42016-11-25 20:15:52 -0700561 self.assertEqual(5, len(entries))
562
563 # First u-boot with padding before and after
564 self.assertIn('u-boot', entries)
565 entry = entries['u-boot']
Simon Glasse8561af2018-08-01 15:22:37 -0600566 self.assertEqual(0, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700567 self.assertEqual(3, entry.pad_before)
568 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
569
570 # Second u-boot has an aligned size, but it has no effect
571 self.assertIn('u-boot-align-size-nop', entries)
572 entry = entries['u-boot-align-size-nop']
Simon Glasse8561af2018-08-01 15:22:37 -0600573 self.assertEqual(12, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700574 self.assertEqual(4, entry.size)
575
576 # Third u-boot has an aligned size too
577 self.assertIn('u-boot-align-size', entries)
578 entry = entries['u-boot-align-size']
Simon Glasse8561af2018-08-01 15:22:37 -0600579 self.assertEqual(16, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700580 self.assertEqual(32, entry.size)
581
582 # Fourth u-boot has an aligned end
583 self.assertIn('u-boot-align-end', entries)
584 entry = entries['u-boot-align-end']
Simon Glasse8561af2018-08-01 15:22:37 -0600585 self.assertEqual(48, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700586 self.assertEqual(16, entry.size)
587
588 # Fifth u-boot immediately afterwards
589 self.assertIn('u-boot-align-both', entries)
590 entry = entries['u-boot-align-both']
Simon Glasse8561af2018-08-01 15:22:37 -0600591 self.assertEqual(64, entry.offset)
Simon Glass57454f42016-11-25 20:15:52 -0700592 self.assertEqual(64, entry.size)
593
594 self.CheckNoGaps(entries)
595 self.assertEqual(128, image._size)
596
597 def testPackAlignPowerOf2(self):
598 """Test that invalid entry alignment is detected"""
599 with self.assertRaises(ValueError) as e:
600 self._DoTestFile('10_pack_align_power2.dts')
601 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
602 "of two", str(e.exception))
603
604 def testPackAlignSizePowerOf2(self):
605 """Test that invalid entry size alignment is detected"""
606 with self.assertRaises(ValueError) as e:
607 self._DoTestFile('11_pack_align_size_power2.dts')
608 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
609 "power of two", str(e.exception))
610
611 def testPackInvalidAlign(self):
Simon Glasse8561af2018-08-01 15:22:37 -0600612 """Test detection of an offset that does not match its alignment"""
Simon Glass57454f42016-11-25 20:15:52 -0700613 with self.assertRaises(ValueError) as e:
614 self._DoTestFile('12_pack_inv_align.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600615 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass57454f42016-11-25 20:15:52 -0700616 "align 0x4 (4)", str(e.exception))
617
618 def testPackInvalidSizeAlign(self):
619 """Test that invalid entry size alignment is detected"""
620 with self.assertRaises(ValueError) as e:
621 self._DoTestFile('13_pack_inv_size_align.dts')
622 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
623 "align-size 0x4 (4)", str(e.exception))
624
625 def testPackOverlap(self):
626 """Test that overlapping regions are detected"""
627 with self.assertRaises(ValueError) as e:
628 self._DoTestFile('14_pack_overlap.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600629 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass57454f42016-11-25 20:15:52 -0700630 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
631 str(e.exception))
632
633 def testPackEntryOverflow(self):
634 """Test that entries that overflow their size are detected"""
635 with self.assertRaises(ValueError) as e:
636 self._DoTestFile('15_pack_overflow.dts')
637 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
638 "but entry size is 0x3 (3)", str(e.exception))
639
640 def testPackImageOverflow(self):
641 """Test that entries which overflow the image size are detected"""
642 with self.assertRaises(ValueError) as e:
643 self._DoTestFile('16_pack_image_overflow.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600644 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass57454f42016-11-25 20:15:52 -0700645 "size 0x3 (3)", str(e.exception))
646
647 def testPackImageSize(self):
648 """Test that the image size can be set"""
649 retcode = self._DoTestFile('17_pack_image_size.dts')
650 self.assertEqual(0, retcode)
651 self.assertIn('image', control.images)
652 image = control.images['image']
653 self.assertEqual(7, image._size)
654
655 def testPackImageSizeAlign(self):
656 """Test that image size alignemnt works as expected"""
657 retcode = self._DoTestFile('18_pack_image_align.dts')
658 self.assertEqual(0, retcode)
659 self.assertIn('image', control.images)
660 image = control.images['image']
661 self.assertEqual(16, image._size)
662
663 def testPackInvalidImageAlign(self):
664 """Test that invalid image alignment is detected"""
665 with self.assertRaises(ValueError) as e:
666 self._DoTestFile('19_pack_inv_image_align.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600667 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass57454f42016-11-25 20:15:52 -0700668 "align-size 0x8 (8)", str(e.exception))
669
670 def testPackAlignPowerOf2(self):
671 """Test that invalid image alignment is detected"""
672 with self.assertRaises(ValueError) as e:
673 self._DoTestFile('20_pack_inv_image_align_power2.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600674 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass57454f42016-11-25 20:15:52 -0700675 "two", str(e.exception))
676
677 def testImagePadByte(self):
678 """Test that the image pad byte can be specified"""
Simon Glass4ca8e042017-11-13 18:55:01 -0700679 with open(self.TestFile('bss_data')) as fd:
680 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass57454f42016-11-25 20:15:52 -0700681 data = self._DoReadFile('21_image_pad.dts')
Simon Glassd6051522017-11-13 18:54:59 -0700682 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
Simon Glass57454f42016-11-25 20:15:52 -0700683
684 def testImageName(self):
685 """Test that image files can be named"""
686 retcode = self._DoTestFile('22_image_name.dts')
687 self.assertEqual(0, retcode)
688 image = control.images['image1']
689 fname = tools.GetOutputFilename('test-name')
690 self.assertTrue(os.path.exists(fname))
691
692 image = control.images['image2']
693 fname = tools.GetOutputFilename('test-name.xx')
694 self.assertTrue(os.path.exists(fname))
695
696 def testBlobFilename(self):
697 """Test that generic blobs can be provided by filename"""
698 data = self._DoReadFile('23_blob.dts')
699 self.assertEqual(BLOB_DATA, data)
700
701 def testPackSorted(self):
702 """Test that entries can be sorted"""
703 data = self._DoReadFile('24_sorted.dts')
Simon Glassd6051522017-11-13 18:54:59 -0700704 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
Simon Glass57454f42016-11-25 20:15:52 -0700705 U_BOOT_DATA, data)
706
Simon Glasse8561af2018-08-01 15:22:37 -0600707 def testPackZeroOffset(self):
708 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass57454f42016-11-25 20:15:52 -0700709 with self.assertRaises(ValueError) as e:
710 self._DoTestFile('25_pack_zero_size.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600711 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass57454f42016-11-25 20:15:52 -0700712 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
713 str(e.exception))
714
715 def testPackUbootDtb(self):
716 """Test that a device tree can be added to U-Boot"""
717 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
718 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glass72232452016-11-25 20:15:53 -0700719
720 def testPackX86RomNoSize(self):
721 """Test that the end-at-4gb property requires a size property"""
722 with self.assertRaises(ValueError) as e:
723 self._DoTestFile('27_pack_4gb_no_size.dts')
Simon Glasseca32212018-06-01 09:38:12 -0600724 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glass72232452016-11-25 20:15:53 -0700725 "using end-at-4gb", str(e.exception))
726
727 def testPackX86RomOutside(self):
Simon Glasse8561af2018-08-01 15:22:37 -0600728 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glass72232452016-11-25 20:15:53 -0700729 with self.assertRaises(ValueError) as e:
730 self._DoTestFile('28_pack_4gb_outside.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600731 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glasseca32212018-06-01 09:38:12 -0600732 "the section starting at 0xffffffe0 (4294967264)",
Simon Glass72232452016-11-25 20:15:53 -0700733 str(e.exception))
734
735 def testPackX86Rom(self):
736 """Test that a basic x86 ROM can be created"""
737 data = self._DoReadFile('29_x86-rom.dts')
Simon Glassd6051522017-11-13 18:54:59 -0700738 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
739 chr(0) * 2, data)
Simon Glass72232452016-11-25 20:15:53 -0700740
741 def testPackX86RomMeNoDesc(self):
742 """Test that an invalid Intel descriptor entry is detected"""
743 TestFunctional._MakeInputFile('descriptor.bin', '')
744 with self.assertRaises(ValueError) as e:
745 self._DoTestFile('31_x86-rom-me.dts')
746 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
747 "signature", str(e.exception))
748
749 def testPackX86RomBadDesc(self):
750 """Test that the Intel requires a descriptor entry"""
751 with self.assertRaises(ValueError) as e:
752 self._DoTestFile('30_x86-rom-me-no-desc.dts')
Simon Glasse8561af2018-08-01 15:22:37 -0600753 self.assertIn("Node '/binman/intel-me': No offset set with "
754 "offset-unset: should another entry provide this correct "
755 "offset?", str(e.exception))
Simon Glass72232452016-11-25 20:15:53 -0700756
757 def testPackX86RomMe(self):
758 """Test that an x86 ROM with an ME region can be created"""
759 data = self._DoReadFile('31_x86-rom-me.dts')
760 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
761
762 def testPackVga(self):
763 """Test that an image with a VGA binary can be created"""
764 data = self._DoReadFile('32_intel-vga.dts')
765 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
766
767 def testPackStart16(self):
768 """Test that an image with an x86 start16 region can be created"""
769 data = self._DoReadFile('33_x86-start16.dts')
770 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
771
Simon Glass6ba679c2018-07-06 10:27:17 -0600772 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glass820af1d2018-07-06 10:27:16 -0600773 """Handle running a test for insertion of microcode
774
775 Args:
776 dts_fname: Name of test .dts file
777 nodtb_data: Data that we expect in the first section
Simon Glass6ba679c2018-07-06 10:27:17 -0600778 ucode_second: True if the microsecond entry is second instead of
779 third
Simon Glass820af1d2018-07-06 10:27:16 -0600780
781 Returns:
782 Tuple:
783 Contents of first region (U-Boot or SPL)
Simon Glasse8561af2018-08-01 15:22:37 -0600784 Offset and size components of microcode pointer, as inserted
Simon Glass820af1d2018-07-06 10:27:16 -0600785 in the above (two 4-byte words)
786 """
Simon Glass3d274232017-11-12 21:52:27 -0700787 data = self._DoReadFile(dts_fname, True)
Simon Glass72232452016-11-25 20:15:53 -0700788
789 # Now check the device tree has no microcode
Simon Glass6ba679c2018-07-06 10:27:17 -0600790 if ucode_second:
791 ucode_content = data[len(nodtb_data):]
792 ucode_pos = len(nodtb_data)
793 dtb_with_ucode = ucode_content[16:]
794 fdt_len = self.GetFdtLen(dtb_with_ucode)
795 else:
796 dtb_with_ucode = data[len(nodtb_data):]
797 fdt_len = self.GetFdtLen(dtb_with_ucode)
798 ucode_content = dtb_with_ucode[fdt_len:]
799 ucode_pos = len(nodtb_data) + fdt_len
Simon Glass72232452016-11-25 20:15:53 -0700800 fname = tools.GetOutputFilename('test.dtb')
801 with open(fname, 'wb') as fd:
Simon Glass820af1d2018-07-06 10:27:16 -0600802 fd.write(dtb_with_ucode)
Simon Glass22c92ca2017-05-27 07:38:29 -0600803 dtb = fdt.FdtScan(fname)
804 ucode = dtb.GetNode('/microcode')
Simon Glass72232452016-11-25 20:15:53 -0700805 self.assertTrue(ucode)
806 for node in ucode.subnodes:
807 self.assertFalse(node.props.get('data'))
808
Simon Glass72232452016-11-25 20:15:53 -0700809 # Check that the microcode appears immediately after the Fdt
810 # This matches the concatenation of the data properties in
Simon Glasse83679d2017-11-12 21:52:26 -0700811 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glass72232452016-11-25 20:15:53 -0700812 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
813 0x78235609)
Simon Glass820af1d2018-07-06 10:27:16 -0600814 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glass72232452016-11-25 20:15:53 -0700815
816 # Check that the microcode pointer was inserted. It should match the
Simon Glasse8561af2018-08-01 15:22:37 -0600817 # expected offset and size
Simon Glass72232452016-11-25 20:15:53 -0700818 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
819 len(ucode_data))
Simon Glass6ba679c2018-07-06 10:27:17 -0600820 u_boot = data[:len(nodtb_data)]
821 return u_boot, pos_and_size
Simon Glass3d274232017-11-12 21:52:27 -0700822
823 def testPackUbootMicrocode(self):
824 """Test that x86 microcode can be handled correctly
825
826 We expect to see the following in the image, in order:
827 u-boot-nodtb.bin with a microcode pointer inserted at the correct
828 place
829 u-boot.dtb with the microcode removed
830 the microcode
831 """
832 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
833 U_BOOT_NODTB_DATA)
Simon Glass72232452016-11-25 20:15:53 -0700834 self.assertEqual('nodtb with microcode' + pos_and_size +
835 ' somewhere in here', first)
836
Simon Glassbac25c82017-05-27 07:38:26 -0600837 def _RunPackUbootSingleMicrocode(self):
Simon Glass72232452016-11-25 20:15:53 -0700838 """Test that x86 microcode can be handled correctly
839
840 We expect to see the following in the image, in order:
841 u-boot-nodtb.bin with a microcode pointer inserted at the correct
842 place
843 u-boot.dtb with the microcode
844 an empty microcode region
845 """
846 # We need the libfdt library to run this test since only that allows
847 # finding the offset of a property. This is required by
848 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glass72232452016-11-25 20:15:53 -0700849 data = self._DoReadFile('35_x86_single_ucode.dts', True)
850
851 second = data[len(U_BOOT_NODTB_DATA):]
852
853 fdt_len = self.GetFdtLen(second)
854 third = second[fdt_len:]
855 second = second[:fdt_len]
856
Simon Glassbac25c82017-05-27 07:38:26 -0600857 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
858 self.assertIn(ucode_data, second)
859 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glass72232452016-11-25 20:15:53 -0700860
Simon Glassbac25c82017-05-27 07:38:26 -0600861 # Check that the microcode pointer was inserted. It should match the
Simon Glasse8561af2018-08-01 15:22:37 -0600862 # expected offset and size
Simon Glassbac25c82017-05-27 07:38:26 -0600863 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
864 len(ucode_data))
865 first = data[:len(U_BOOT_NODTB_DATA)]
866 self.assertEqual('nodtb with microcode' + pos_and_size +
867 ' somewhere in here', first)
Simon Glass996021e2016-11-25 20:15:54 -0700868
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700869 def testPackUbootSingleMicrocode(self):
870 """Test that x86 microcode can be handled correctly with fdt_normal.
871 """
Simon Glassbac25c82017-05-27 07:38:26 -0600872 self._RunPackUbootSingleMicrocode()
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700873
Simon Glass996021e2016-11-25 20:15:54 -0700874 def testUBootImg(self):
875 """Test that u-boot.img can be put in a file"""
876 data = self._DoReadFile('36_u_boot_img.dts')
877 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700878
879 def testNoMicrocode(self):
880 """Test that a missing microcode region is detected"""
881 with self.assertRaises(ValueError) as e:
882 self._DoReadFile('37_x86_no_ucode.dts', True)
883 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
884 "node found in ", str(e.exception))
885
886 def testMicrocodeWithoutNode(self):
887 """Test that a missing u-boot-dtb-with-ucode node is detected"""
888 with self.assertRaises(ValueError) as e:
889 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
890 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
891 "microcode region u-boot-dtb-with-ucode", str(e.exception))
892
893 def testMicrocodeWithoutNode2(self):
894 """Test that a missing u-boot-ucode node is detected"""
895 with self.assertRaises(ValueError) as e:
896 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
897 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
898 "microcode region u-boot-ucode", str(e.exception))
899
900 def testMicrocodeWithoutPtrInElf(self):
901 """Test that a U-Boot binary without the microcode symbol is detected"""
902 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700903 try:
904 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
905 TestFunctional._MakeInputFile('u-boot', fd.read())
906
907 with self.assertRaises(ValueError) as e:
Simon Glassbac25c82017-05-27 07:38:26 -0600908 self._RunPackUbootSingleMicrocode()
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700909 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
910 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
911
912 finally:
913 # Put the original file back
914 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
915 TestFunctional._MakeInputFile('u-boot', fd.read())
916
917 def testMicrocodeNotInImage(self):
918 """Test that microcode must be placed within the image"""
919 with self.assertRaises(ValueError) as e:
920 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
921 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
922 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glassad5a7712018-06-01 09:38:14 -0600923 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700924
925 def testWithoutMicrocode(self):
926 """Test that we can cope with an image without microcode (e.g. qemu)"""
927 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
928 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glassa87014e2018-07-06 10:27:42 -0600929 data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700930
931 # Now check the device tree has no microcode
932 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
933 second = data[len(U_BOOT_NODTB_DATA):]
934
935 fdt_len = self.GetFdtLen(second)
936 self.assertEqual(dtb, second[:fdt_len])
937
938 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
939 third = data[used_len:]
940 self.assertEqual(chr(0) * (0x200 - used_len), third)
941
942 def testUnknownPosSize(self):
943 """Test that microcode must be placed within the image"""
944 with self.assertRaises(ValueError) as e:
945 self._DoReadFile('41_unknown_pos_size.dts', True)
Simon Glasse8561af2018-08-01 15:22:37 -0600946 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glassd2dfb5f2016-11-25 20:15:55 -0700947 "entry 'invalid-entry'", str(e.exception))
Simon Glassb4176d42016-11-25 20:15:56 -0700948
949 def testPackFsp(self):
950 """Test that an image with a FSP binary can be created"""
951 data = self._DoReadFile('42_intel-fsp.dts')
952 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
953
954 def testPackCmc(self):
Bin Mengd7bcdf52017-08-15 22:41:54 -0700955 """Test that an image with a CMC binary can be created"""
Simon Glassb4176d42016-11-25 20:15:56 -0700956 data = self._DoReadFile('43_intel-cmc.dts')
957 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Mengd7bcdf52017-08-15 22:41:54 -0700958
959 def testPackVbt(self):
960 """Test that an image with a VBT binary can be created"""
961 data = self._DoReadFile('46_intel-vbt.dts')
962 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glassac599912017-11-12 21:52:22 -0700963
Simon Glass7f94e832017-11-12 21:52:25 -0700964 def testSplBssPad(self):
965 """Test that we can pad SPL's BSS with zeros"""
Simon Glass3d274232017-11-12 21:52:27 -0700966 # ELF file with a '__bss_size' symbol
967 with open(self.TestFile('bss_data')) as fd:
968 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass7f94e832017-11-12 21:52:25 -0700969 data = self._DoReadFile('47_spl_bss_pad.dts')
970 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
971
Simon Glass24ad3652017-11-13 18:54:54 -0700972 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
973 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
974 with self.assertRaises(ValueError) as e:
975 data = self._DoReadFile('47_spl_bss_pad.dts')
976 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
977 str(e.exception))
978
Simon Glasse83679d2017-11-12 21:52:26 -0700979 def testPackStart16Spl(self):
Simon Glassed40e962018-09-14 04:57:10 -0600980 """Test that an image with an x86 start16 SPL region can be created"""
Simon Glasse83679d2017-11-12 21:52:26 -0700981 data = self._DoReadFile('48_x86-start16-spl.dts')
982 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
983
Simon Glass6ba679c2018-07-06 10:27:17 -0600984 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
985 """Helper function for microcode tests
Simon Glass3d274232017-11-12 21:52:27 -0700986
987 We expect to see the following in the image, in order:
988 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
989 correct place
990 u-boot.dtb with the microcode removed
991 the microcode
Simon Glass6ba679c2018-07-06 10:27:17 -0600992
993 Args:
994 dts: Device tree file to use for test
995 ucode_second: True if the microsecond entry is second instead of
996 third
Simon Glass3d274232017-11-12 21:52:27 -0700997 """
998 # ELF file with a '_dt_ucode_base_size' symbol
999 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
1000 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass6ba679c2018-07-06 10:27:17 -06001001 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
1002 ucode_second=ucode_second)
Simon Glass3d274232017-11-12 21:52:27 -07001003 self.assertEqual('splnodtb with microc' + pos_and_size +
1004 'ter somewhere in here', first)
1005
Simon Glass6ba679c2018-07-06 10:27:17 -06001006 def testPackUbootSplMicrocode(self):
1007 """Test that x86 microcode can be handled correctly in SPL"""
1008 self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
1009
1010 def testPackUbootSplMicrocodeReorder(self):
1011 """Test that order doesn't matter for microcode entries
1012
1013 This is the same as testPackUbootSplMicrocode but when we process the
1014 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
1015 entry, so we reply on binman to try later.
1016 """
1017 self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
1018 ucode_second=True)
1019
Simon Glassa409c932017-11-12 21:52:28 -07001020 def testPackMrc(self):
1021 """Test that an image with an MRC binary can be created"""
1022 data = self._DoReadFile('50_intel_mrc.dts')
1023 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
1024
Simon Glass9aa6a6f2017-11-13 18:54:55 -07001025 def testSplDtb(self):
1026 """Test that an image with spl/u-boot-spl.dtb can be created"""
1027 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
1028 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
1029
Simon Glass0a6da312017-11-13 18:54:56 -07001030 def testSplNoDtb(self):
1031 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
1032 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
1033 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
1034
Simon Glass4ca8e042017-11-13 18:55:01 -07001035 def testSymbols(self):
1036 """Test binman can assign symbols embedded in U-Boot"""
1037 elf_fname = self.TestFile('u_boot_binman_syms')
1038 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
1039 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glasse8561af2018-08-01 15:22:37 -06001040 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass4ca8e042017-11-13 18:55:01 -07001041
1042 with open(self.TestFile('u_boot_binman_syms')) as fd:
1043 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
1044 data = self._DoReadFile('53_symbols.dts')
1045 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
1046 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
1047 U_BOOT_DATA +
1048 sym_values + U_BOOT_SPL_DATA[16:])
1049 self.assertEqual(expected, data)
1050
Simon Glasse76a3e62018-06-01 09:38:11 -06001051 def testPackUnitAddress(self):
1052 """Test that we support multiple binaries with the same name"""
1053 data = self._DoReadFile('54_unit_address.dts')
1054 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1055
Simon Glassa91e1152018-06-01 09:38:16 -06001056 def testSections(self):
1057 """Basic test of sections"""
1058 data = self._DoReadFile('55_sections.dts')
Simon Glass3a9a2b82018-07-17 13:25:28 -06001059 expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
1060 U_BOOT_DATA + '&' * 4)
Simon Glassa91e1152018-06-01 09:38:16 -06001061 self.assertEqual(expected, data)
Simon Glassac599912017-11-12 21:52:22 -07001062
Simon Glass30732662018-06-01 09:38:20 -06001063 def testMap(self):
1064 """Tests outputting a map of the images"""
Simon Glassa87014e2018-07-06 10:27:42 -06001065 _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
Simon Glass7eca7922018-07-17 13:25:49 -06001066 self.assertEqual('''ImagePos Offset Size Name
106700000000 00000000 00000028 main-section
106800000000 00000000 00000010 section@0
106900000000 00000000 00000004 u-boot
107000000010 00000010 00000010 section@1
107100000010 00000000 00000004 u-boot
107200000020 00000020 00000004 section@2
107300000020 00000000 00000004 u-boot
Simon Glass30732662018-06-01 09:38:20 -06001074''', map_data)
1075
Simon Glass3b78d532018-06-01 09:38:21 -06001076 def testNamePrefix(self):
1077 """Tests that name prefixes are used"""
Simon Glassa87014e2018-07-06 10:27:42 -06001078 _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
Simon Glass7eca7922018-07-17 13:25:49 -06001079 self.assertEqual('''ImagePos Offset Size Name
108000000000 00000000 00000028 main-section
108100000000 00000000 00000010 section@0
108200000000 00000000 00000004 ro-u-boot
108300000010 00000010 00000010 section@1
108400000010 00000000 00000004 rw-u-boot
Simon Glass3b78d532018-06-01 09:38:21 -06001085''', map_data)
1086
Simon Glass6ba679c2018-07-06 10:27:17 -06001087 def testUnknownContents(self):
1088 """Test that obtaining the contents works as expected"""
1089 with self.assertRaises(ValueError) as e:
1090 self._DoReadFile('57_unknown_contents.dts', True)
1091 self.assertIn("Section '/binman': Internal error: Could not complete "
1092 "processing of contents: remaining [<_testing.Entry__testing ",
1093 str(e.exception))
1094
Simon Glass2e1169f2018-07-06 10:27:19 -06001095 def testBadChangeSize(self):
1096 """Test that trying to change the size of an entry fails"""
1097 with self.assertRaises(ValueError) as e:
1098 self._DoReadFile('59_change_size.dts', True)
1099 self.assertIn("Node '/binman/_testing': Cannot update entry size from "
1100 '2 to 1', str(e.exception))
1101
Simon Glassa87014e2018-07-06 10:27:42 -06001102 def testUpdateFdt(self):
Simon Glasse8561af2018-08-01 15:22:37 -06001103 """Test that we can update the device tree with offset/size info"""
Simon Glassa87014e2018-07-06 10:27:42 -06001104 _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
1105 update_dtb=True)
Simon Glass5463a6a2018-07-17 13:25:52 -06001106 dtb = fdt.Fdt(out_dtb_fname)
1107 dtb.Scan()
1108 props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos'])
Simon Glassa87014e2018-07-06 10:27:42 -06001109 self.assertEqual({
Simon Glass9dcc8612018-08-01 15:22:42 -06001110 'image-pos': 0,
Simon Glass3a9a2b82018-07-17 13:25:28 -06001111 'offset': 0,
Simon Glasse8561af2018-08-01 15:22:37 -06001112 '_testing:offset': 32,
Simon Glassa87014e2018-07-06 10:27:42 -06001113 '_testing:size': 1,
Simon Glass9dcc8612018-08-01 15:22:42 -06001114 '_testing:image-pos': 32,
Simon Glasse8561af2018-08-01 15:22:37 -06001115 'section@0/u-boot:offset': 0,
Simon Glassa87014e2018-07-06 10:27:42 -06001116 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glass9dcc8612018-08-01 15:22:42 -06001117 'section@0/u-boot:image-pos': 0,
Simon Glasse8561af2018-08-01 15:22:37 -06001118 'section@0:offset': 0,
Simon Glassa87014e2018-07-06 10:27:42 -06001119 'section@0:size': 16,
Simon Glass9dcc8612018-08-01 15:22:42 -06001120 'section@0:image-pos': 0,
Simon Glassa87014e2018-07-06 10:27:42 -06001121
Simon Glasse8561af2018-08-01 15:22:37 -06001122 'section@1/u-boot:offset': 0,
Simon Glassa87014e2018-07-06 10:27:42 -06001123 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glass9dcc8612018-08-01 15:22:42 -06001124 'section@1/u-boot:image-pos': 16,
Simon Glasse8561af2018-08-01 15:22:37 -06001125 'section@1:offset': 16,
Simon Glassa87014e2018-07-06 10:27:42 -06001126 'section@1:size': 16,
Simon Glass9dcc8612018-08-01 15:22:42 -06001127 'section@1:image-pos': 16,
Simon Glassa87014e2018-07-06 10:27:42 -06001128 'size': 40
1129 }, props)
1130
1131 def testUpdateFdtBad(self):
1132 """Test that we detect when ProcessFdt never completes"""
1133 with self.assertRaises(ValueError) as e:
1134 self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
1135 self.assertIn('Could not complete processing of Fdt: remaining '
1136 '[<_testing.Entry__testing', str(e.exception))
Simon Glass2e1169f2018-07-06 10:27:19 -06001137
Simon Glass91710b32018-07-17 13:25:32 -06001138 def testEntryArgs(self):
1139 """Test passing arguments to entries from the command line"""
1140 entry_args = {
1141 'test-str-arg': 'test1',
1142 'test-int-arg': '456',
1143 }
1144 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1145 self.assertIn('image', control.images)
1146 entry = control.images['image'].GetEntries()['_testing']
1147 self.assertEqual('test0', entry.test_str_fdt)
1148 self.assertEqual('test1', entry.test_str_arg)
1149 self.assertEqual(123, entry.test_int_fdt)
1150 self.assertEqual(456, entry.test_int_arg)
1151
1152 def testEntryArgsMissing(self):
1153 """Test missing arguments and properties"""
1154 entry_args = {
1155 'test-int-arg': '456',
1156 }
1157 self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args)
1158 entry = control.images['image'].GetEntries()['_testing']
1159 self.assertEqual('test0', entry.test_str_fdt)
1160 self.assertEqual(None, entry.test_str_arg)
1161 self.assertEqual(None, entry.test_int_fdt)
1162 self.assertEqual(456, entry.test_int_arg)
1163
1164 def testEntryArgsRequired(self):
1165 """Test missing arguments and properties"""
1166 entry_args = {
1167 'test-int-arg': '456',
1168 }
1169 with self.assertRaises(ValueError) as e:
1170 self._DoReadFileDtb('64_entry_args_required.dts')
1171 self.assertIn("Node '/binman/_testing': Missing required "
1172 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1173 str(e.exception))
1174
1175 def testEntryArgsInvalidFormat(self):
1176 """Test that an invalid entry-argument format is detected"""
1177 args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value']
1178 with self.assertRaises(ValueError) as e:
1179 self._DoBinman(*args)
1180 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1181
1182 def testEntryArgsInvalidInteger(self):
1183 """Test that an invalid entry-argument integer is detected"""
1184 entry_args = {
1185 'test-int-arg': 'abc',
1186 }
1187 with self.assertRaises(ValueError) as e:
1188 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1189 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1190 "'test-int-arg' (value 'abc') to integer",
1191 str(e.exception))
1192
1193 def testEntryArgsInvalidDatatype(self):
1194 """Test that an invalid entry-argument datatype is detected
1195
1196 This test could be written in entry_test.py except that it needs
1197 access to control.entry_args, which seems more than that module should
1198 be able to see.
1199 """
1200 entry_args = {
1201 'test-bad-datatype-arg': '12',
1202 }
1203 with self.assertRaises(ValueError) as e:
1204 self._DoReadFileDtb('65_entry_args_unknown_datatype.dts',
1205 entry_args=entry_args)
1206 self.assertIn('GetArg() internal error: Unknown data type ',
1207 str(e.exception))
1208
Simon Glass2ca52032018-07-17 13:25:33 -06001209 def testText(self):
1210 """Test for a text entry type"""
1211 entry_args = {
1212 'test-id': TEXT_DATA,
1213 'test-id2': TEXT_DATA2,
1214 'test-id3': TEXT_DATA3,
1215 }
1216 data, _, _, _ = self._DoReadFileDtb('66_text.dts',
1217 entry_args=entry_args)
1218 expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 +
1219 TEXT_DATA3 + 'some text')
1220 self.assertEqual(expected, data)
1221
Simon Glass969616c2018-07-17 13:25:36 -06001222 def testEntryDocs(self):
1223 """Test for creation of entry documentation"""
1224 with test_util.capture_sys_output() as (stdout, stderr):
1225 control.WriteEntryDocs(binman.GetEntryModules())
1226 self.assertTrue(len(stdout.getvalue()) > 0)
1227
1228 def testEntryDocsMissing(self):
1229 """Test handling of missing entry documentation"""
1230 with self.assertRaises(ValueError) as e:
1231 with test_util.capture_sys_output() as (stdout, stderr):
1232 control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
1233 self.assertIn('Documentation is missing for modules: u_boot',
1234 str(e.exception))
1235
Simon Glass704784b2018-07-17 13:25:38 -06001236 def testFmap(self):
1237 """Basic test of generation of a flashrom fmap"""
1238 data = self._DoReadFile('67_fmap.dts')
1239 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
1240 expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12
1241 self.assertEqual(expected, data[:32])
1242 self.assertEqual('__FMAP__', fhdr.signature)
1243 self.assertEqual(1, fhdr.ver_major)
1244 self.assertEqual(0, fhdr.ver_minor)
1245 self.assertEqual(0, fhdr.base)
1246 self.assertEqual(16 + 16 +
1247 fmap_util.FMAP_HEADER_LEN +
1248 fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size)
1249 self.assertEqual('FMAP', fhdr.name)
1250 self.assertEqual(3, fhdr.nareas)
1251 for fentry in fentries:
1252 self.assertEqual(0, fentry.flags)
1253
1254 self.assertEqual(0, fentries[0].offset)
1255 self.assertEqual(4, fentries[0].size)
1256 self.assertEqual('RO_U_BOOT', fentries[0].name)
1257
1258 self.assertEqual(16, fentries[1].offset)
1259 self.assertEqual(4, fentries[1].size)
1260 self.assertEqual('RW_U_BOOT', fentries[1].name)
1261
1262 self.assertEqual(32, fentries[2].offset)
1263 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1264 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
1265 self.assertEqual('FMAP', fentries[2].name)
1266
Simon Glassdb168d42018-07-17 13:25:39 -06001267 def testBlobNamedByArg(self):
1268 """Test we can add a blob with the filename coming from an entry arg"""
1269 entry_args = {
1270 'cros-ec-rw-path': 'ecrw.bin',
1271 }
1272 data, _, _, _ = self._DoReadFileDtb('68_blob_named_by_arg.dts',
1273 entry_args=entry_args)
1274
Simon Glass53f53992018-07-17 13:25:40 -06001275 def testFill(self):
1276 """Test for an fill entry type"""
1277 data = self._DoReadFile('69_fill.dts')
1278 expected = 8 * chr(0xff) + 8 * chr(0)
1279 self.assertEqual(expected, data)
1280
1281 def testFillNoSize(self):
1282 """Test for an fill entry type with no size"""
1283 with self.assertRaises(ValueError) as e:
1284 self._DoReadFile('70_fill_no_size.dts')
1285 self.assertIn("'fill' entry must have a size property",
1286 str(e.exception))
1287
Simon Glassc1ae83c2018-07-17 13:25:44 -06001288 def _HandleGbbCommand(self, pipe_list):
1289 """Fake calls to the futility utility"""
1290 if pipe_list[0][0] == 'futility':
1291 fname = pipe_list[0][-1]
1292 # Append our GBB data to the file, which will happen every time the
1293 # futility command is called.
1294 with open(fname, 'a') as fd:
1295 fd.write(GBB_DATA)
1296 return command.CommandResult()
1297
1298 def testGbb(self):
1299 """Test for the Chromium OS Google Binary Block"""
1300 command.test_result = self._HandleGbbCommand
1301 entry_args = {
1302 'keydir': 'devkeys',
1303 'bmpblk': 'bmpblk.bin',
1304 }
1305 data, _, _, _ = self._DoReadFileDtb('71_gbb.dts', entry_args=entry_args)
1306
1307 # Since futility
1308 expected = GBB_DATA + GBB_DATA + 8 * chr(0) + (0x2180 - 16) * chr(0)
1309 self.assertEqual(expected, data)
1310
1311 def testGbbTooSmall(self):
1312 """Test for the Chromium OS Google Binary Block being large enough"""
1313 with self.assertRaises(ValueError) as e:
1314 self._DoReadFileDtb('72_gbb_too_small.dts')
1315 self.assertIn("Node '/binman/gbb': GBB is too small",
1316 str(e.exception))
1317
1318 def testGbbNoSize(self):
1319 """Test for the Chromium OS Google Binary Block having a size"""
1320 with self.assertRaises(ValueError) as e:
1321 self._DoReadFileDtb('73_gbb_no_size.dts')
1322 self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
1323 str(e.exception))
1324
Simon Glass5c350162018-07-17 13:25:47 -06001325 def _HandleVblockCommand(self, pipe_list):
1326 """Fake calls to the futility utility"""
1327 if pipe_list[0][0] == 'futility':
1328 fname = pipe_list[0][3]
Simon Glass639505b2018-09-14 04:57:11 -06001329 with open(fname, 'wb') as fd:
Simon Glass5c350162018-07-17 13:25:47 -06001330 fd.write(VBLOCK_DATA)
1331 return command.CommandResult()
1332
1333 def testVblock(self):
1334 """Test for the Chromium OS Verified Boot Block"""
1335 command.test_result = self._HandleVblockCommand
1336 entry_args = {
1337 'keydir': 'devkeys',
1338 }
1339 data, _, _, _ = self._DoReadFileDtb('74_vblock.dts',
1340 entry_args=entry_args)
1341 expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA
1342 self.assertEqual(expected, data)
1343
1344 def testVblockNoContent(self):
1345 """Test we detect a vblock which has no content to sign"""
1346 with self.assertRaises(ValueError) as e:
1347 self._DoReadFile('75_vblock_no_content.dts')
1348 self.assertIn("Node '/binman/vblock': Vblock must have a 'content' "
1349 'property', str(e.exception))
1350
1351 def testVblockBadPhandle(self):
1352 """Test that we detect a vblock with an invalid phandle in contents"""
1353 with self.assertRaises(ValueError) as e:
1354 self._DoReadFile('76_vblock_bad_phandle.dts')
1355 self.assertIn("Node '/binman/vblock': Cannot find node for phandle "
1356 '1000', str(e.exception))
1357
1358 def testVblockBadEntry(self):
1359 """Test that we detect an entry that points to a non-entry"""
1360 with self.assertRaises(ValueError) as e:
1361 self._DoReadFile('77_vblock_bad_entry.dts')
1362 self.assertIn("Node '/binman/vblock': Cannot find entry for node "
1363 "'other'", str(e.exception))
1364
Simon Glass8425a1f2018-07-17 13:25:48 -06001365 def testTpl(self):
1366 """Test that an image with TPL and ots device tree can be created"""
1367 # ELF file with a '__bss_size' symbol
1368 with open(self.TestFile('bss_data')) as fd:
1369 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
1370 data = self._DoReadFile('78_u_boot_tpl.dts')
1371 self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
1372
Simon Glass24b97442018-07-17 13:25:51 -06001373 def testUsesPos(self):
1374 """Test that the 'pos' property cannot be used anymore"""
1375 with self.assertRaises(ValueError) as e:
1376 data = self._DoReadFile('79_uses_pos.dts')
1377 self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of "
1378 "'pos'", str(e.exception))
1379
Simon Glass274bf092018-09-14 04:57:08 -06001380 def testFillZero(self):
1381 """Test for an fill entry type with a size of 0"""
1382 data = self._DoReadFile('80_fill_empty.dts')
1383 self.assertEqual(chr(0) * 16, data)
1384
Simon Glass267de432018-09-14 04:57:09 -06001385 def testTextMissing(self):
1386 """Test for a text entry type where there is no text"""
1387 with self.assertRaises(ValueError) as e:
1388 self._DoReadFileDtb('66_text.dts',)
1389 self.assertIn("Node '/binman/text': No value provided for text label "
1390 "'test-id'", str(e.exception))
1391
Simon Glassed40e962018-09-14 04:57:10 -06001392 def testPackStart16Tpl(self):
1393 """Test that an image with an x86 start16 TPL region can be created"""
1394 data = self._DoReadFile('81_x86-start16-tpl.dts')
1395 self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)])
1396
Simon Glass3b376c32018-09-14 04:57:12 -06001397 def testSelectImage(self):
1398 """Test that we can select which images to build"""
1399 with test_util.capture_sys_output() as (stdout, stderr):
1400 retcode = self._DoTestFile('06_dual_image.dts', images=['image2'])
1401 self.assertEqual(0, retcode)
1402 self.assertIn('Skipping images: image1', stdout.getvalue())
1403
1404 self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin')))
1405 self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin')))
1406
Simon Glass91710b32018-07-17 13:25:32 -06001407
Simon Glassac599912017-11-12 21:52:22 -07001408if __name__ == "__main__":
1409 unittest.main()