blob: 36519a2496c11db7e69ca9f0b198588beb1ef968 [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'
42U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
43U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
44FSP_DATA = 'fsp'
45CMC_DATA = 'cmc'
46VBT_DATA = 'vbt'
Simon Glassca4f4ff2017-11-12 21:52:28 -070047MRC_DATA = 'mrc'
Simon Glassbb748372018-07-17 13:25:33 -060048TEXT_DATA = 'text'
49TEXT_DATA2 = 'text2'
50TEXT_DATA3 = 'text3'
Simon Glassec127af2018-07-17 13:25:39 -060051CROS_EC_RW_DATA = 'ecrw'
Simon Glass0ef87aa2018-07-17 13:25:44 -060052GBB_DATA = 'gbbd'
53BMPBLK_DATA = 'bmp'
Simon Glass24d0d3c2018-07-17 13:25:47 -060054VBLOCK_DATA = 'vblk'
Simon Glassec127af2018-07-17 13:25:39 -060055
Simon Glass4f443042016-11-25 20:15:52 -070056
57class TestFunctional(unittest.TestCase):
58 """Functional tests for binman
59
60 Most of these use a sample .dts file to build an image and then check
61 that it looks correct. The sample files are in the test/ subdirectory
62 and are numbered.
63
64 For each entry type a very small test file is created using fixed
65 string contents. This makes it easy to test that things look right, and
66 debug problems.
67
68 In some cases a 'real' file must be used - these are also supplied in
69 the test/ diurectory.
70 """
71 @classmethod
72 def setUpClass(self):
Simon Glass4d5994f2017-11-12 21:52:20 -070073 global entry
74 import entry
75
Simon Glass4f443042016-11-25 20:15:52 -070076 # Handle the case where argv[0] is 'python'
77 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
78 self._binman_pathname = os.path.join(self._binman_dir, 'binman')
79
80 # Create a temporary directory for input files
81 self._indir = tempfile.mkdtemp(prefix='binmant.')
82
83 # Create some test files
84 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
85 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
86 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
Simon Glassb8ef5b62018-07-17 13:25:48 -060087 TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070088 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -070089 TestFunctional._MakeInputFile('me.bin', ME_DATA)
90 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glassb8ef5b62018-07-17 13:25:48 -060091 self._ResetDtbs()
Simon Glasse0ff8552016-11-25 20:15:53 -070092 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Simon Glass87722132017-11-12 21:52:26 -070093 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
94 X86_START16_SPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -070095 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass6b187df2017-11-12 21:52:27 -070096 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
97 U_BOOT_SPL_NODTB_DATA)
Simon Glassda229092016-11-25 20:15:56 -070098 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
99 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Meng59ea8c22017-08-15 22:41:54 -0700100 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassca4f4ff2017-11-12 21:52:28 -0700101 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glassec127af2018-07-17 13:25:39 -0600102 TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
Simon Glass0ef87aa2018-07-17 13:25:44 -0600103 TestFunctional._MakeInputDir('devkeys')
104 TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700105 self._output_setup = False
106
Simon Glasse0ff8552016-11-25 20:15:53 -0700107 # ELF file with a '_dt_ucode_base_size' symbol
108 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
109 TestFunctional._MakeInputFile('u-boot', fd.read())
110
111 # Intel flash descriptor file
112 with open(self.TestFile('descriptor.bin')) as fd:
113 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
114
Simon Glass4f443042016-11-25 20:15:52 -0700115 @classmethod
116 def tearDownClass(self):
117 """Remove the temporary input directory and its contents"""
118 if self._indir:
119 shutil.rmtree(self._indir)
120 self._indir = None
121
122 def setUp(self):
123 # Enable this to turn on debugging output
124 # tout.Init(tout.DEBUG)
125 command.test_result = None
126
127 def tearDown(self):
128 """Remove the temporary output directory"""
129 tools._FinaliseForTest()
130
Simon Glassb8ef5b62018-07-17 13:25:48 -0600131 @classmethod
132 def _ResetDtbs(self):
133 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
134 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
135 TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA)
136
Simon Glass4f443042016-11-25 20:15:52 -0700137 def _RunBinman(self, *args, **kwargs):
138 """Run binman using the command line
139
140 Args:
141 Arguments to pass, as a list of strings
142 kwargs: Arguments to pass to Command.RunPipe()
143 """
144 result = command.RunPipe([[self._binman_pathname] + list(args)],
145 capture=True, capture_stderr=True, raise_on_error=False)
146 if result.return_code and kwargs.get('raise_on_error', True):
147 raise Exception("Error running '%s': %s" % (' '.join(args),
148 result.stdout + result.stderr))
149 return result
150
151 def _DoBinman(self, *args):
152 """Run binman using directly (in the same process)
153
154 Args:
155 Arguments to pass, as a list of strings
156 Returns:
157 Return value (0 for success)
158 """
Simon Glass7fe91732017-11-13 18:55:00 -0700159 args = list(args)
160 if '-D' in sys.argv:
161 args = args + ['-D']
162 (options, args) = cmdline.ParseArgs(args)
Simon Glass4f443042016-11-25 20:15:52 -0700163 options.pager = 'binman-invalid-pager'
164 options.build_dir = self._indir
165
166 # For testing, you can force an increase in verbosity here
167 # options.verbosity = tout.DEBUG
168 return control.Binman(options, args)
169
Simon Glass53af22a2018-07-17 13:25:32 -0600170 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
171 entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700172 """Run binman with a given test file
173
174 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600175 fname: Device-tree source filename to use (e.g. 05_simple.dts)
176 debug: True to enable debugging output
Simon Glass3b0c3822018-06-01 09:38:20 -0600177 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600178 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600179 tree before packing it into the image
Simon Glass4f443042016-11-25 20:15:52 -0700180 """
Simon Glass7fe91732017-11-13 18:55:00 -0700181 args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
182 if debug:
183 args.append('-D')
Simon Glass3b0c3822018-06-01 09:38:20 -0600184 if map:
185 args.append('-m')
Simon Glass16b8d6b2018-07-06 10:27:42 -0600186 if update_dtb:
187 args.append('-up')
Simon Glass53af22a2018-07-17 13:25:32 -0600188 if entry_args:
189 for arg, value in entry_args.iteritems():
190 args.append('-a%s=%s' % (arg, value))
Simon Glass7fe91732017-11-13 18:55:00 -0700191 return self._DoBinman(*args)
Simon Glass4f443042016-11-25 20:15:52 -0700192
193 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glasse0ff8552016-11-25 20:15:53 -0700194 """Set up a new test device-tree file
195
196 The given file is compiled and set up as the device tree to be used
197 for ths test.
198
199 Args:
200 fname: Filename of .dts file to read
Simon Glass7ae5f312018-06-01 09:38:19 -0600201 outfile: Output filename for compiled device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700202
203 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600204 Contents of device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700205 """
Simon Glass4f443042016-11-25 20:15:52 -0700206 if not self._output_setup:
207 tools.PrepareOutputDir(self._indir, True)
208 self._output_setup = True
209 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
210 with open(dtb) as fd:
211 data = fd.read()
212 TestFunctional._MakeInputFile(outfile, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700213 return data
Simon Glass4f443042016-11-25 20:15:52 -0700214
Simon Glass16b8d6b2018-07-06 10:27:42 -0600215 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass53af22a2018-07-17 13:25:32 -0600216 update_dtb=False, entry_args=None):
Simon Glass4f443042016-11-25 20:15:52 -0700217 """Run binman and return the resulting image
218
219 This runs binman with a given test file and then reads the resulting
220 output file. It is a shortcut function since most tests need to do
221 these steps.
222
223 Raises an assertion failure if binman returns a non-zero exit code.
224
225 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600226 fname: Device-tree source filename to use (e.g. 05_simple.dts)
Simon Glass4f443042016-11-25 20:15:52 -0700227 use_real_dtb: True to use the test file as the contents of
228 the u-boot-dtb entry. Normally this is not needed and the
229 test contents (the U_BOOT_DTB_DATA string) can be used.
230 But in some test we need the real contents.
Simon Glass3b0c3822018-06-01 09:38:20 -0600231 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600232 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600233 tree before packing it into the image
Simon Glasse0ff8552016-11-25 20:15:53 -0700234
235 Returns:
236 Tuple:
237 Resulting image contents
238 Device tree contents
Simon Glass3b0c3822018-06-01 09:38:20 -0600239 Map data showing contents of image (or None if none)
Simon Glassea6922e2018-07-17 13:25:27 -0600240 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass4f443042016-11-25 20:15:52 -0700241 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700242 dtb_data = None
Simon Glass4f443042016-11-25 20:15:52 -0700243 # Use the compiled test file as the u-boot-dtb input
244 if use_real_dtb:
Simon Glasse0ff8552016-11-25 20:15:53 -0700245 dtb_data = self._SetupDtb(fname)
Simon Glass4f443042016-11-25 20:15:52 -0700246
247 try:
Simon Glass53af22a2018-07-17 13:25:32 -0600248 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
249 entry_args=entry_args)
Simon Glass4f443042016-11-25 20:15:52 -0700250 self.assertEqual(0, retcode)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600251 out_dtb_fname = control.GetFdtPath('u-boot.dtb')
Simon Glass4f443042016-11-25 20:15:52 -0700252
253 # Find the (only) image, read it and return its contents
254 image = control.images['image']
Simon Glass16b8d6b2018-07-06 10:27:42 -0600255 image_fname = tools.GetOutputFilename('image.bin')
256 self.assertTrue(os.path.exists(image_fname))
Simon Glass3b0c3822018-06-01 09:38:20 -0600257 if map:
258 map_fname = tools.GetOutputFilename('image.map')
259 with open(map_fname) as fd:
260 map_data = fd.read()
261 else:
262 map_data = None
Simon Glass16b8d6b2018-07-06 10:27:42 -0600263 with open(image_fname) as fd:
264 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass4f443042016-11-25 20:15:52 -0700265 finally:
266 # Put the test file back
267 if use_real_dtb:
Simon Glassb8ef5b62018-07-17 13:25:48 -0600268 self._ResetDtbs()
Simon Glass4f443042016-11-25 20:15:52 -0700269
Simon Glasse0ff8552016-11-25 20:15:53 -0700270 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass7ae5f312018-06-01 09:38:19 -0600271 """Helper function which discards the device-tree binary
272
273 Args:
274 fname: Device-tree source filename to use (e.g. 05_simple.dts)
275 use_real_dtb: True to use the test file as the contents of
276 the u-boot-dtb entry. Normally this is not needed and the
277 test contents (the U_BOOT_DTB_DATA string) can be used.
278 But in some test we need the real contents.
Simon Glassea6922e2018-07-17 13:25:27 -0600279
280 Returns:
281 Resulting image contents
Simon Glass7ae5f312018-06-01 09:38:19 -0600282 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700283 return self._DoReadFileDtb(fname, use_real_dtb)[0]
284
Simon Glass4f443042016-11-25 20:15:52 -0700285 @classmethod
286 def _MakeInputFile(self, fname, contents):
287 """Create a new test input file, creating directories as needed
288
289 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600290 fname: Filename to create
Simon Glass4f443042016-11-25 20:15:52 -0700291 contents: File contents to write in to the file
292 Returns:
293 Full pathname of file created
294 """
295 pathname = os.path.join(self._indir, fname)
296 dirname = os.path.dirname(pathname)
297 if dirname and not os.path.exists(dirname):
298 os.makedirs(dirname)
299 with open(pathname, 'wb') as fd:
300 fd.write(contents)
301 return pathname
302
303 @classmethod
Simon Glass0ef87aa2018-07-17 13:25:44 -0600304 def _MakeInputDir(self, dirname):
305 """Create a new test input directory, creating directories as needed
306
307 Args:
308 dirname: Directory name to create
309
310 Returns:
311 Full pathname of directory created
312 """
313 pathname = os.path.join(self._indir, dirname)
314 if not os.path.exists(pathname):
315 os.makedirs(pathname)
316 return pathname
317
318 @classmethod
Simon Glass4f443042016-11-25 20:15:52 -0700319 def TestFile(self, fname):
320 return os.path.join(self._binman_dir, 'test', fname)
321
322 def AssertInList(self, grep_list, target):
323 """Assert that at least one of a list of things is in a target
324
325 Args:
326 grep_list: List of strings to check
327 target: Target string
328 """
329 for grep in grep_list:
330 if grep in target:
331 return
332 self.fail("Error: '%' not found in '%s'" % (grep_list, target))
333
334 def CheckNoGaps(self, entries):
335 """Check that all entries fit together without gaps
336
337 Args:
338 entries: List of entries to check
339 """
Simon Glass3ab95982018-08-01 15:22:37 -0600340 offset = 0
Simon Glass4f443042016-11-25 20:15:52 -0700341 for entry in entries.values():
Simon Glass3ab95982018-08-01 15:22:37 -0600342 self.assertEqual(offset, entry.offset)
343 offset += entry.size
Simon Glass4f443042016-11-25 20:15:52 -0700344
Simon Glasse0ff8552016-11-25 20:15:53 -0700345 def GetFdtLen(self, dtb):
Simon Glass7ae5f312018-06-01 09:38:19 -0600346 """Get the totalsize field from a device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700347
348 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600349 dtb: Device-tree binary contents
Simon Glasse0ff8552016-11-25 20:15:53 -0700350
351 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600352 Total size of device-tree binary, from the header
Simon Glasse0ff8552016-11-25 20:15:53 -0700353 """
354 return struct.unpack('>L', dtb[4:8])[0]
355
Simon Glasscee02e62018-07-17 13:25:52 -0600356 def _GetPropTree(self, dtb, prop_names):
Simon Glass16b8d6b2018-07-06 10:27:42 -0600357 def AddNode(node, path):
358 if node.name != '/':
359 path += '/' + node.name
Simon Glass16b8d6b2018-07-06 10:27:42 -0600360 for subnode in node.subnodes:
361 for prop in subnode.props.values():
Simon Glasscee02e62018-07-17 13:25:52 -0600362 if prop.name in prop_names:
Simon Glass16b8d6b2018-07-06 10:27:42 -0600363 prop_path = path + '/' + subnode.name + ':' + prop.name
364 tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
365 prop.value)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600366 AddNode(subnode, path)
367
368 tree = {}
Simon Glass16b8d6b2018-07-06 10:27:42 -0600369 AddNode(dtb.GetRoot(), '')
370 return tree
371
Simon Glass4f443042016-11-25 20:15:52 -0700372 def testRun(self):
373 """Test a basic run with valid args"""
374 result = self._RunBinman('-h')
375
376 def testFullHelp(self):
377 """Test that the full help is displayed with -H"""
378 result = self._RunBinman('-H')
379 help_file = os.path.join(self._binman_dir, 'README')
Tom Rini3759df02018-01-16 15:29:50 -0500380 # Remove possible extraneous strings
381 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
382 gothelp = result.stdout.replace(extra, '')
383 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass4f443042016-11-25 20:15:52 -0700384 self.assertEqual(0, len(result.stderr))
385 self.assertEqual(0, result.return_code)
386
387 def testFullHelpInternal(self):
388 """Test that the full help is displayed with -H"""
389 try:
390 command.test_result = command.CommandResult()
391 result = self._DoBinman('-H')
392 help_file = os.path.join(self._binman_dir, 'README')
393 finally:
394 command.test_result = None
395
396 def testHelp(self):
397 """Test that the basic help is displayed with -h"""
398 result = self._RunBinman('-h')
399 self.assertTrue(len(result.stdout) > 200)
400 self.assertEqual(0, len(result.stderr))
401 self.assertEqual(0, result.return_code)
402
Simon Glass4f443042016-11-25 20:15:52 -0700403 def testBoard(self):
404 """Test that we can run it with a specific board"""
405 self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
406 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
407 result = self._DoBinman('-b', 'sandbox')
408 self.assertEqual(0, result)
409
410 def testNeedBoard(self):
411 """Test that we get an error when no board ius supplied"""
412 with self.assertRaises(ValueError) as e:
413 result = self._DoBinman()
414 self.assertIn("Must provide a board to process (use -b <board>)",
415 str(e.exception))
416
417 def testMissingDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600418 """Test that an invalid device-tree file generates an error"""
Simon Glass4f443042016-11-25 20:15:52 -0700419 with self.assertRaises(Exception) as e:
420 self._RunBinman('-d', 'missing_file')
421 # We get one error from libfdt, and a different one from fdtget.
422 self.AssertInList(["Couldn't open blob from 'missing_file'",
423 'No such file or directory'], str(e.exception))
424
425 def testBrokenDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600426 """Test that an invalid device-tree source file generates an error
Simon Glass4f443042016-11-25 20:15:52 -0700427
428 Since this is a source file it should be compiled and the error
429 will come from the device-tree compiler (dtc).
430 """
431 with self.assertRaises(Exception) as e:
432 self._RunBinman('-d', self.TestFile('01_invalid.dts'))
433 self.assertIn("FATAL ERROR: Unable to parse input tree",
434 str(e.exception))
435
436 def testMissingNode(self):
437 """Test that a device tree without a 'binman' node generates an error"""
438 with self.assertRaises(Exception) as e:
439 self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
440 self.assertIn("does not have a 'binman' node", str(e.exception))
441
442 def testEmpty(self):
443 """Test that an empty binman node works OK (i.e. does nothing)"""
444 result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
445 self.assertEqual(0, len(result.stderr))
446 self.assertEqual(0, result.return_code)
447
448 def testInvalidEntry(self):
449 """Test that an invalid entry is flagged"""
450 with self.assertRaises(Exception) as e:
451 result = self._RunBinman('-d',
452 self.TestFile('04_invalid_entry.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700453 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
454 "'/binman/not-a-valid-type'", str(e.exception))
455
456 def testSimple(self):
457 """Test a simple binman with a single file"""
458 data = self._DoReadFile('05_simple.dts')
459 self.assertEqual(U_BOOT_DATA, data)
460
Simon Glass7fe91732017-11-13 18:55:00 -0700461 def testSimpleDebug(self):
462 """Test a simple binman run with debugging enabled"""
463 data = self._DoTestFile('05_simple.dts', debug=True)
464
Simon Glass4f443042016-11-25 20:15:52 -0700465 def testDual(self):
466 """Test that we can handle creating two images
467
468 This also tests image padding.
469 """
470 retcode = self._DoTestFile('06_dual_image.dts')
471 self.assertEqual(0, retcode)
472
473 image = control.images['image1']
474 self.assertEqual(len(U_BOOT_DATA), image._size)
475 fname = tools.GetOutputFilename('image1.bin')
476 self.assertTrue(os.path.exists(fname))
477 with open(fname) as fd:
478 data = fd.read()
479 self.assertEqual(U_BOOT_DATA, data)
480
481 image = control.images['image2']
482 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
483 fname = tools.GetOutputFilename('image2.bin')
484 self.assertTrue(os.path.exists(fname))
485 with open(fname) as fd:
486 data = fd.read()
487 self.assertEqual(U_BOOT_DATA, data[3:7])
488 self.assertEqual(chr(0) * 3, data[:3])
489 self.assertEqual(chr(0) * 5, data[7:])
490
491 def testBadAlign(self):
492 """Test that an invalid alignment value is detected"""
493 with self.assertRaises(ValueError) as e:
494 self._DoTestFile('07_bad_align.dts')
495 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
496 "of two", str(e.exception))
497
498 def testPackSimple(self):
499 """Test that packing works as expected"""
500 retcode = self._DoTestFile('08_pack.dts')
501 self.assertEqual(0, retcode)
502 self.assertIn('image', control.images)
503 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600504 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700505 self.assertEqual(5, len(entries))
506
507 # First u-boot
508 self.assertIn('u-boot', entries)
509 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600510 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700511 self.assertEqual(len(U_BOOT_DATA), entry.size)
512
513 # Second u-boot, aligned to 16-byte boundary
514 self.assertIn('u-boot-align', entries)
515 entry = entries['u-boot-align']
Simon Glass3ab95982018-08-01 15:22:37 -0600516 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700517 self.assertEqual(len(U_BOOT_DATA), entry.size)
518
519 # Third u-boot, size 23 bytes
520 self.assertIn('u-boot-size', entries)
521 entry = entries['u-boot-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600522 self.assertEqual(20, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700523 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
524 self.assertEqual(23, entry.size)
525
526 # Fourth u-boot, placed immediate after the above
527 self.assertIn('u-boot-next', entries)
528 entry = entries['u-boot-next']
Simon Glass3ab95982018-08-01 15:22:37 -0600529 self.assertEqual(43, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700530 self.assertEqual(len(U_BOOT_DATA), entry.size)
531
Simon Glass3ab95982018-08-01 15:22:37 -0600532 # Fifth u-boot, placed at a fixed offset
Simon Glass4f443042016-11-25 20:15:52 -0700533 self.assertIn('u-boot-fixed', entries)
534 entry = entries['u-boot-fixed']
Simon Glass3ab95982018-08-01 15:22:37 -0600535 self.assertEqual(61, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700536 self.assertEqual(len(U_BOOT_DATA), entry.size)
537
538 self.assertEqual(65, image._size)
539
540 def testPackExtra(self):
541 """Test that extra packing feature works as expected"""
542 retcode = self._DoTestFile('09_pack_extra.dts')
543
544 self.assertEqual(0, retcode)
545 self.assertIn('image', control.images)
546 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600547 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700548 self.assertEqual(5, len(entries))
549
550 # First u-boot with padding before and after
551 self.assertIn('u-boot', entries)
552 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600553 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700554 self.assertEqual(3, entry.pad_before)
555 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
556
557 # Second u-boot has an aligned size, but it has no effect
558 self.assertIn('u-boot-align-size-nop', entries)
559 entry = entries['u-boot-align-size-nop']
Simon Glass3ab95982018-08-01 15:22:37 -0600560 self.assertEqual(12, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700561 self.assertEqual(4, entry.size)
562
563 # Third u-boot has an aligned size too
564 self.assertIn('u-boot-align-size', entries)
565 entry = entries['u-boot-align-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600566 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700567 self.assertEqual(32, entry.size)
568
569 # Fourth u-boot has an aligned end
570 self.assertIn('u-boot-align-end', entries)
571 entry = entries['u-boot-align-end']
Simon Glass3ab95982018-08-01 15:22:37 -0600572 self.assertEqual(48, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700573 self.assertEqual(16, entry.size)
574
575 # Fifth u-boot immediately afterwards
576 self.assertIn('u-boot-align-both', entries)
577 entry = entries['u-boot-align-both']
Simon Glass3ab95982018-08-01 15:22:37 -0600578 self.assertEqual(64, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700579 self.assertEqual(64, entry.size)
580
581 self.CheckNoGaps(entries)
582 self.assertEqual(128, image._size)
583
584 def testPackAlignPowerOf2(self):
585 """Test that invalid entry alignment is detected"""
586 with self.assertRaises(ValueError) as e:
587 self._DoTestFile('10_pack_align_power2.dts')
588 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
589 "of two", str(e.exception))
590
591 def testPackAlignSizePowerOf2(self):
592 """Test that invalid entry size alignment is detected"""
593 with self.assertRaises(ValueError) as e:
594 self._DoTestFile('11_pack_align_size_power2.dts')
595 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
596 "power of two", str(e.exception))
597
598 def testPackInvalidAlign(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600599 """Test detection of an offset that does not match its alignment"""
Simon Glass4f443042016-11-25 20:15:52 -0700600 with self.assertRaises(ValueError) as e:
601 self._DoTestFile('12_pack_inv_align.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600602 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700603 "align 0x4 (4)", str(e.exception))
604
605 def testPackInvalidSizeAlign(self):
606 """Test that invalid entry size alignment is detected"""
607 with self.assertRaises(ValueError) as e:
608 self._DoTestFile('13_pack_inv_size_align.dts')
609 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
610 "align-size 0x4 (4)", str(e.exception))
611
612 def testPackOverlap(self):
613 """Test that overlapping regions are detected"""
614 with self.assertRaises(ValueError) as e:
615 self._DoTestFile('14_pack_overlap.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600616 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700617 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
618 str(e.exception))
619
620 def testPackEntryOverflow(self):
621 """Test that entries that overflow their size are detected"""
622 with self.assertRaises(ValueError) as e:
623 self._DoTestFile('15_pack_overflow.dts')
624 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
625 "but entry size is 0x3 (3)", str(e.exception))
626
627 def testPackImageOverflow(self):
628 """Test that entries which overflow the image size are detected"""
629 with self.assertRaises(ValueError) as e:
630 self._DoTestFile('16_pack_image_overflow.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600631 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass4f443042016-11-25 20:15:52 -0700632 "size 0x3 (3)", str(e.exception))
633
634 def testPackImageSize(self):
635 """Test that the image size can be set"""
636 retcode = self._DoTestFile('17_pack_image_size.dts')
637 self.assertEqual(0, retcode)
638 self.assertIn('image', control.images)
639 image = control.images['image']
640 self.assertEqual(7, image._size)
641
642 def testPackImageSizeAlign(self):
643 """Test that image size alignemnt works as expected"""
644 retcode = self._DoTestFile('18_pack_image_align.dts')
645 self.assertEqual(0, retcode)
646 self.assertIn('image', control.images)
647 image = control.images['image']
648 self.assertEqual(16, image._size)
649
650 def testPackInvalidImageAlign(self):
651 """Test that invalid image alignment is detected"""
652 with self.assertRaises(ValueError) as e:
653 self._DoTestFile('19_pack_inv_image_align.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600654 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700655 "align-size 0x8 (8)", str(e.exception))
656
657 def testPackAlignPowerOf2(self):
658 """Test that invalid image alignment is detected"""
659 with self.assertRaises(ValueError) as e:
660 self._DoTestFile('20_pack_inv_image_align_power2.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600661 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass4f443042016-11-25 20:15:52 -0700662 "two", str(e.exception))
663
664 def testImagePadByte(self):
665 """Test that the image pad byte can be specified"""
Simon Glass19790632017-11-13 18:55:01 -0700666 with open(self.TestFile('bss_data')) as fd:
667 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass4f443042016-11-25 20:15:52 -0700668 data = self._DoReadFile('21_image_pad.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700669 self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700670
671 def testImageName(self):
672 """Test that image files can be named"""
673 retcode = self._DoTestFile('22_image_name.dts')
674 self.assertEqual(0, retcode)
675 image = control.images['image1']
676 fname = tools.GetOutputFilename('test-name')
677 self.assertTrue(os.path.exists(fname))
678
679 image = control.images['image2']
680 fname = tools.GetOutputFilename('test-name.xx')
681 self.assertTrue(os.path.exists(fname))
682
683 def testBlobFilename(self):
684 """Test that generic blobs can be provided by filename"""
685 data = self._DoReadFile('23_blob.dts')
686 self.assertEqual(BLOB_DATA, data)
687
688 def testPackSorted(self):
689 """Test that entries can be sorted"""
690 data = self._DoReadFile('24_sorted.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700691 self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
Simon Glass4f443042016-11-25 20:15:52 -0700692 U_BOOT_DATA, data)
693
Simon Glass3ab95982018-08-01 15:22:37 -0600694 def testPackZeroOffset(self):
695 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass4f443042016-11-25 20:15:52 -0700696 with self.assertRaises(ValueError) as e:
697 self._DoTestFile('25_pack_zero_size.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600698 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700699 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
700 str(e.exception))
701
702 def testPackUbootDtb(self):
703 """Test that a device tree can be added to U-Boot"""
704 data = self._DoReadFile('26_pack_u_boot_dtb.dts')
705 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700706
707 def testPackX86RomNoSize(self):
708 """Test that the end-at-4gb property requires a size property"""
709 with self.assertRaises(ValueError) as e:
710 self._DoTestFile('27_pack_4gb_no_size.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600711 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glasse0ff8552016-11-25 20:15:53 -0700712 "using end-at-4gb", str(e.exception))
713
Jagdish Gediya94b57db2018-09-03 21:35:07 +0530714 def test4gbAndSkipAtStartTogether(self):
715 """Test that the end-at-4gb and skip-at-size property can't be used
716 together"""
717 with self.assertRaises(ValueError) as e:
718 self._DoTestFile('80_4gb_and_skip_at_start_together.dts')
719 self.assertIn("Section '/binman': Provide either 'end-at-4gb' or "
720 "'skip-at-start'", str(e.exception))
721
Simon Glasse0ff8552016-11-25 20:15:53 -0700722 def testPackX86RomOutside(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600723 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glasse0ff8552016-11-25 20:15:53 -0700724 with self.assertRaises(ValueError) as e:
725 self._DoTestFile('28_pack_4gb_outside.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600726 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glass8f1da502018-06-01 09:38:12 -0600727 "the section starting at 0xffffffe0 (4294967264)",
Simon Glasse0ff8552016-11-25 20:15:53 -0700728 str(e.exception))
729
730 def testPackX86Rom(self):
731 """Test that a basic x86 ROM can be created"""
732 data = self._DoReadFile('29_x86-rom.dts')
Simon Glassf6898902017-11-13 18:54:59 -0700733 self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
734 chr(0) * 2, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700735
736 def testPackX86RomMeNoDesc(self):
737 """Test that an invalid Intel descriptor entry is detected"""
738 TestFunctional._MakeInputFile('descriptor.bin', '')
739 with self.assertRaises(ValueError) as e:
740 self._DoTestFile('31_x86-rom-me.dts')
741 self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
742 "signature", str(e.exception))
743
744 def testPackX86RomBadDesc(self):
745 """Test that the Intel requires a descriptor entry"""
746 with self.assertRaises(ValueError) as e:
747 self._DoTestFile('30_x86-rom-me-no-desc.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600748 self.assertIn("Node '/binman/intel-me': No offset set with "
749 "offset-unset: should another entry provide this correct "
750 "offset?", str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700751
752 def testPackX86RomMe(self):
753 """Test that an x86 ROM with an ME region can be created"""
754 data = self._DoReadFile('31_x86-rom-me.dts')
755 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
756
757 def testPackVga(self):
758 """Test that an image with a VGA binary can be created"""
759 data = self._DoReadFile('32_intel-vga.dts')
760 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
761
762 def testPackStart16(self):
763 """Test that an image with an x86 start16 region can be created"""
764 data = self._DoReadFile('33_x86-start16.dts')
765 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
766
Simon Glass736bb0a2018-07-06 10:27:17 -0600767 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glassadc57012018-07-06 10:27:16 -0600768 """Handle running a test for insertion of microcode
769
770 Args:
771 dts_fname: Name of test .dts file
772 nodtb_data: Data that we expect in the first section
Simon Glass736bb0a2018-07-06 10:27:17 -0600773 ucode_second: True if the microsecond entry is second instead of
774 third
Simon Glassadc57012018-07-06 10:27:16 -0600775
776 Returns:
777 Tuple:
778 Contents of first region (U-Boot or SPL)
Simon Glass3ab95982018-08-01 15:22:37 -0600779 Offset and size components of microcode pointer, as inserted
Simon Glassadc57012018-07-06 10:27:16 -0600780 in the above (two 4-byte words)
781 """
Simon Glass6b187df2017-11-12 21:52:27 -0700782 data = self._DoReadFile(dts_fname, True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700783
784 # Now check the device tree has no microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600785 if ucode_second:
786 ucode_content = data[len(nodtb_data):]
787 ucode_pos = len(nodtb_data)
788 dtb_with_ucode = ucode_content[16:]
789 fdt_len = self.GetFdtLen(dtb_with_ucode)
790 else:
791 dtb_with_ucode = data[len(nodtb_data):]
792 fdt_len = self.GetFdtLen(dtb_with_ucode)
793 ucode_content = dtb_with_ucode[fdt_len:]
794 ucode_pos = len(nodtb_data) + fdt_len
Simon Glasse0ff8552016-11-25 20:15:53 -0700795 fname = tools.GetOutputFilename('test.dtb')
796 with open(fname, 'wb') as fd:
Simon Glassadc57012018-07-06 10:27:16 -0600797 fd.write(dtb_with_ucode)
Simon Glassec3f3782017-05-27 07:38:29 -0600798 dtb = fdt.FdtScan(fname)
799 ucode = dtb.GetNode('/microcode')
Simon Glasse0ff8552016-11-25 20:15:53 -0700800 self.assertTrue(ucode)
801 for node in ucode.subnodes:
802 self.assertFalse(node.props.get('data'))
803
Simon Glasse0ff8552016-11-25 20:15:53 -0700804 # Check that the microcode appears immediately after the Fdt
805 # This matches the concatenation of the data properties in
Simon Glass87722132017-11-12 21:52:26 -0700806 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glasse0ff8552016-11-25 20:15:53 -0700807 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
808 0x78235609)
Simon Glassadc57012018-07-06 10:27:16 -0600809 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glasse0ff8552016-11-25 20:15:53 -0700810
811 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600812 # expected offset and size
Simon Glasse0ff8552016-11-25 20:15:53 -0700813 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
814 len(ucode_data))
Simon Glass736bb0a2018-07-06 10:27:17 -0600815 u_boot = data[:len(nodtb_data)]
816 return u_boot, pos_and_size
Simon Glass6b187df2017-11-12 21:52:27 -0700817
818 def testPackUbootMicrocode(self):
819 """Test that x86 microcode can be handled correctly
820
821 We expect to see the following in the image, in order:
822 u-boot-nodtb.bin with a microcode pointer inserted at the correct
823 place
824 u-boot.dtb with the microcode removed
825 the microcode
826 """
827 first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
828 U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700829 self.assertEqual('nodtb with microcode' + pos_and_size +
830 ' somewhere in here', first)
831
Simon Glass160a7662017-05-27 07:38:26 -0600832 def _RunPackUbootSingleMicrocode(self):
Simon Glasse0ff8552016-11-25 20:15:53 -0700833 """Test that x86 microcode can be handled correctly
834
835 We expect to see the following in the image, in order:
836 u-boot-nodtb.bin with a microcode pointer inserted at the correct
837 place
838 u-boot.dtb with the microcode
839 an empty microcode region
840 """
841 # We need the libfdt library to run this test since only that allows
842 # finding the offset of a property. This is required by
843 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glasse0ff8552016-11-25 20:15:53 -0700844 data = self._DoReadFile('35_x86_single_ucode.dts', True)
845
846 second = data[len(U_BOOT_NODTB_DATA):]
847
848 fdt_len = self.GetFdtLen(second)
849 third = second[fdt_len:]
850 second = second[:fdt_len]
851
Simon Glass160a7662017-05-27 07:38:26 -0600852 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
853 self.assertIn(ucode_data, second)
854 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700855
Simon Glass160a7662017-05-27 07:38:26 -0600856 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600857 # expected offset and size
Simon Glass160a7662017-05-27 07:38:26 -0600858 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
859 len(ucode_data))
860 first = data[:len(U_BOOT_NODTB_DATA)]
861 self.assertEqual('nodtb with microcode' + pos_and_size +
862 ' somewhere in here', first)
Simon Glassc49deb82016-11-25 20:15:54 -0700863
Simon Glass75db0862016-11-25 20:15:55 -0700864 def testPackUbootSingleMicrocode(self):
865 """Test that x86 microcode can be handled correctly with fdt_normal.
866 """
Simon Glass160a7662017-05-27 07:38:26 -0600867 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700868
Simon Glassc49deb82016-11-25 20:15:54 -0700869 def testUBootImg(self):
870 """Test that u-boot.img can be put in a file"""
871 data = self._DoReadFile('36_u_boot_img.dts')
872 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glass75db0862016-11-25 20:15:55 -0700873
874 def testNoMicrocode(self):
875 """Test that a missing microcode region is detected"""
876 with self.assertRaises(ValueError) as e:
877 self._DoReadFile('37_x86_no_ucode.dts', True)
878 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
879 "node found in ", str(e.exception))
880
881 def testMicrocodeWithoutNode(self):
882 """Test that a missing u-boot-dtb-with-ucode node is detected"""
883 with self.assertRaises(ValueError) as e:
884 self._DoReadFile('38_x86_ucode_missing_node.dts', True)
885 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
886 "microcode region u-boot-dtb-with-ucode", str(e.exception))
887
888 def testMicrocodeWithoutNode2(self):
889 """Test that a missing u-boot-ucode node is detected"""
890 with self.assertRaises(ValueError) as e:
891 self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
892 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
893 "microcode region u-boot-ucode", str(e.exception))
894
895 def testMicrocodeWithoutPtrInElf(self):
896 """Test that a U-Boot binary without the microcode symbol is detected"""
897 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glass75db0862016-11-25 20:15:55 -0700898 try:
899 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
900 TestFunctional._MakeInputFile('u-boot', fd.read())
901
902 with self.assertRaises(ValueError) as e:
Simon Glass160a7662017-05-27 07:38:26 -0600903 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -0700904 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
905 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
906
907 finally:
908 # Put the original file back
909 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
910 TestFunctional._MakeInputFile('u-boot', fd.read())
911
912 def testMicrocodeNotInImage(self):
913 """Test that microcode must be placed within the image"""
914 with self.assertRaises(ValueError) as e:
915 self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
916 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
917 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glass25ac0e62018-06-01 09:38:14 -0600918 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glass75db0862016-11-25 20:15:55 -0700919
920 def testWithoutMicrocode(self):
921 """Test that we can cope with an image without microcode (e.g. qemu)"""
922 with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
923 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glass16b8d6b2018-07-06 10:27:42 -0600924 data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -0700925
926 # Now check the device tree has no microcode
927 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
928 second = data[len(U_BOOT_NODTB_DATA):]
929
930 fdt_len = self.GetFdtLen(second)
931 self.assertEqual(dtb, second[:fdt_len])
932
933 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
934 third = data[used_len:]
935 self.assertEqual(chr(0) * (0x200 - used_len), third)
936
937 def testUnknownPosSize(self):
938 """Test that microcode must be placed within the image"""
939 with self.assertRaises(ValueError) as e:
940 self._DoReadFile('41_unknown_pos_size.dts', True)
Simon Glass3ab95982018-08-01 15:22:37 -0600941 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glass75db0862016-11-25 20:15:55 -0700942 "entry 'invalid-entry'", str(e.exception))
Simon Glassda229092016-11-25 20:15:56 -0700943
944 def testPackFsp(self):
945 """Test that an image with a FSP binary can be created"""
946 data = self._DoReadFile('42_intel-fsp.dts')
947 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
948
949 def testPackCmc(self):
Bin Meng59ea8c22017-08-15 22:41:54 -0700950 """Test that an image with a CMC binary can be created"""
Simon Glassda229092016-11-25 20:15:56 -0700951 data = self._DoReadFile('43_intel-cmc.dts')
952 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Meng59ea8c22017-08-15 22:41:54 -0700953
954 def testPackVbt(self):
955 """Test that an image with a VBT binary can be created"""
956 data = self._DoReadFile('46_intel-vbt.dts')
957 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glass9fc60b42017-11-12 21:52:22 -0700958
Simon Glass56509842017-11-12 21:52:25 -0700959 def testSplBssPad(self):
960 """Test that we can pad SPL's BSS with zeros"""
Simon Glass6b187df2017-11-12 21:52:27 -0700961 # ELF file with a '__bss_size' symbol
962 with open(self.TestFile('bss_data')) as fd:
963 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass56509842017-11-12 21:52:25 -0700964 data = self._DoReadFile('47_spl_bss_pad.dts')
965 self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
966
Simon Glassb50e5612017-11-13 18:54:54 -0700967 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
968 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
969 with self.assertRaises(ValueError) as e:
970 data = self._DoReadFile('47_spl_bss_pad.dts')
971 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
972 str(e.exception))
973
Simon Glass87722132017-11-12 21:52:26 -0700974 def testPackStart16Spl(self):
975 """Test that an image with an x86 start16 region can be created"""
976 data = self._DoReadFile('48_x86-start16-spl.dts')
977 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
978
Simon Glass736bb0a2018-07-06 10:27:17 -0600979 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
980 """Helper function for microcode tests
Simon Glass6b187df2017-11-12 21:52:27 -0700981
982 We expect to see the following in the image, in order:
983 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
984 correct place
985 u-boot.dtb with the microcode removed
986 the microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600987
988 Args:
989 dts: Device tree file to use for test
990 ucode_second: True if the microsecond entry is second instead of
991 third
Simon Glass6b187df2017-11-12 21:52:27 -0700992 """
993 # ELF file with a '_dt_ucode_base_size' symbol
994 with open(self.TestFile('u_boot_ucode_ptr')) as fd:
995 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
Simon Glass736bb0a2018-07-06 10:27:17 -0600996 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
997 ucode_second=ucode_second)
Simon Glass6b187df2017-11-12 21:52:27 -0700998 self.assertEqual('splnodtb with microc' + pos_and_size +
999 'ter somewhere in here', first)
1000
Simon Glass736bb0a2018-07-06 10:27:17 -06001001 def testPackUbootSplMicrocode(self):
1002 """Test that x86 microcode can be handled correctly in SPL"""
1003 self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
1004
1005 def testPackUbootSplMicrocodeReorder(self):
1006 """Test that order doesn't matter for microcode entries
1007
1008 This is the same as testPackUbootSplMicrocode but when we process the
1009 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
1010 entry, so we reply on binman to try later.
1011 """
1012 self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
1013 ucode_second=True)
1014
Simon Glassca4f4ff2017-11-12 21:52:28 -07001015 def testPackMrc(self):
1016 """Test that an image with an MRC binary can be created"""
1017 data = self._DoReadFile('50_intel_mrc.dts')
1018 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
1019
Simon Glass47419ea2017-11-13 18:54:55 -07001020 def testSplDtb(self):
1021 """Test that an image with spl/u-boot-spl.dtb can be created"""
1022 data = self._DoReadFile('51_u_boot_spl_dtb.dts')
1023 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
1024
Simon Glass4e6fdbe2017-11-13 18:54:56 -07001025 def testSplNoDtb(self):
1026 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
1027 data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
1028 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
1029
Simon Glass19790632017-11-13 18:55:01 -07001030 def testSymbols(self):
1031 """Test binman can assign symbols embedded in U-Boot"""
1032 elf_fname = self.TestFile('u_boot_binman_syms')
1033 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
1034 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glass3ab95982018-08-01 15:22:37 -06001035 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass19790632017-11-13 18:55:01 -07001036
1037 with open(self.TestFile('u_boot_binman_syms')) as fd:
1038 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
1039 data = self._DoReadFile('53_symbols.dts')
1040 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
1041 expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
1042 U_BOOT_DATA +
1043 sym_values + U_BOOT_SPL_DATA[16:])
1044 self.assertEqual(expected, data)
1045
Simon Glassdd57c132018-06-01 09:38:11 -06001046 def testPackUnitAddress(self):
1047 """Test that we support multiple binaries with the same name"""
1048 data = self._DoReadFile('54_unit_address.dts')
1049 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1050
Simon Glass18546952018-06-01 09:38:16 -06001051 def testSections(self):
1052 """Basic test of sections"""
1053 data = self._DoReadFile('55_sections.dts')
Simon Glass8122f392018-07-17 13:25:28 -06001054 expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
1055 U_BOOT_DATA + '&' * 4)
Simon Glass18546952018-06-01 09:38:16 -06001056 self.assertEqual(expected, data)
Simon Glass9fc60b42017-11-12 21:52:22 -07001057
Simon Glass3b0c3822018-06-01 09:38:20 -06001058 def testMap(self):
1059 """Tests outputting a map of the images"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001060 _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001061 self.assertEqual('''ImagePos Offset Size Name
106200000000 00000000 00000028 main-section
106300000000 00000000 00000010 section@0
106400000000 00000000 00000004 u-boot
106500000010 00000010 00000010 section@1
106600000010 00000000 00000004 u-boot
106700000020 00000020 00000004 section@2
106800000020 00000000 00000004 u-boot
Simon Glass3b0c3822018-06-01 09:38:20 -06001069''', map_data)
1070
Simon Glassc8d48ef2018-06-01 09:38:21 -06001071 def testNamePrefix(self):
1072 """Tests that name prefixes are used"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001073 _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001074 self.assertEqual('''ImagePos Offset Size Name
107500000000 00000000 00000028 main-section
107600000000 00000000 00000010 section@0
107700000000 00000000 00000004 ro-u-boot
107800000010 00000010 00000010 section@1
107900000010 00000000 00000004 rw-u-boot
Simon Glassc8d48ef2018-06-01 09:38:21 -06001080''', map_data)
1081
Simon Glass736bb0a2018-07-06 10:27:17 -06001082 def testUnknownContents(self):
1083 """Test that obtaining the contents works as expected"""
1084 with self.assertRaises(ValueError) as e:
1085 self._DoReadFile('57_unknown_contents.dts', True)
1086 self.assertIn("Section '/binman': Internal error: Could not complete "
1087 "processing of contents: remaining [<_testing.Entry__testing ",
1088 str(e.exception))
1089
Simon Glass5c890232018-07-06 10:27:19 -06001090 def testBadChangeSize(self):
1091 """Test that trying to change the size of an entry fails"""
1092 with self.assertRaises(ValueError) as e:
1093 self._DoReadFile('59_change_size.dts', True)
1094 self.assertIn("Node '/binman/_testing': Cannot update entry size from "
1095 '2 to 1', str(e.exception))
1096
Simon Glass16b8d6b2018-07-06 10:27:42 -06001097 def testUpdateFdt(self):
Simon Glass3ab95982018-08-01 15:22:37 -06001098 """Test that we can update the device tree with offset/size info"""
Simon Glass16b8d6b2018-07-06 10:27:42 -06001099 _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
1100 update_dtb=True)
Simon Glasscee02e62018-07-17 13:25:52 -06001101 dtb = fdt.Fdt(out_dtb_fname)
1102 dtb.Scan()
1103 props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos'])
Simon Glass16b8d6b2018-07-06 10:27:42 -06001104 self.assertEqual({
Simon Glassdbf6be92018-08-01 15:22:42 -06001105 'image-pos': 0,
Simon Glass8122f392018-07-17 13:25:28 -06001106 'offset': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001107 '_testing:offset': 32,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001108 '_testing:size': 1,
Simon Glassdbf6be92018-08-01 15:22:42 -06001109 '_testing:image-pos': 32,
Simon Glass3ab95982018-08-01 15:22:37 -06001110 'section@0/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001111 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001112 'section@0/u-boot:image-pos': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001113 'section@0:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001114 'section@0:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001115 'section@0:image-pos': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001116
Simon Glass3ab95982018-08-01 15:22:37 -06001117 'section@1/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001118 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001119 'section@1/u-boot:image-pos': 16,
Simon Glass3ab95982018-08-01 15:22:37 -06001120 'section@1:offset': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001121 'section@1:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001122 'section@1:image-pos': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001123 'size': 40
1124 }, props)
1125
1126 def testUpdateFdtBad(self):
1127 """Test that we detect when ProcessFdt never completes"""
1128 with self.assertRaises(ValueError) as e:
1129 self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
1130 self.assertIn('Could not complete processing of Fdt: remaining '
1131 '[<_testing.Entry__testing', str(e.exception))
Simon Glass5c890232018-07-06 10:27:19 -06001132
Simon Glass53af22a2018-07-17 13:25:32 -06001133 def testEntryArgs(self):
1134 """Test passing arguments to entries from the command line"""
1135 entry_args = {
1136 'test-str-arg': 'test1',
1137 'test-int-arg': '456',
1138 }
1139 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1140 self.assertIn('image', control.images)
1141 entry = control.images['image'].GetEntries()['_testing']
1142 self.assertEqual('test0', entry.test_str_fdt)
1143 self.assertEqual('test1', entry.test_str_arg)
1144 self.assertEqual(123, entry.test_int_fdt)
1145 self.assertEqual(456, entry.test_int_arg)
1146
1147 def testEntryArgsMissing(self):
1148 """Test missing arguments and properties"""
1149 entry_args = {
1150 'test-int-arg': '456',
1151 }
1152 self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args)
1153 entry = control.images['image'].GetEntries()['_testing']
1154 self.assertEqual('test0', entry.test_str_fdt)
1155 self.assertEqual(None, entry.test_str_arg)
1156 self.assertEqual(None, entry.test_int_fdt)
1157 self.assertEqual(456, entry.test_int_arg)
1158
1159 def testEntryArgsRequired(self):
1160 """Test missing arguments and properties"""
1161 entry_args = {
1162 'test-int-arg': '456',
1163 }
1164 with self.assertRaises(ValueError) as e:
1165 self._DoReadFileDtb('64_entry_args_required.dts')
1166 self.assertIn("Node '/binman/_testing': Missing required "
1167 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1168 str(e.exception))
1169
1170 def testEntryArgsInvalidFormat(self):
1171 """Test that an invalid entry-argument format is detected"""
1172 args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value']
1173 with self.assertRaises(ValueError) as e:
1174 self._DoBinman(*args)
1175 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1176
1177 def testEntryArgsInvalidInteger(self):
1178 """Test that an invalid entry-argument integer is detected"""
1179 entry_args = {
1180 'test-int-arg': 'abc',
1181 }
1182 with self.assertRaises(ValueError) as e:
1183 self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
1184 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1185 "'test-int-arg' (value 'abc') to integer",
1186 str(e.exception))
1187
1188 def testEntryArgsInvalidDatatype(self):
1189 """Test that an invalid entry-argument datatype is detected
1190
1191 This test could be written in entry_test.py except that it needs
1192 access to control.entry_args, which seems more than that module should
1193 be able to see.
1194 """
1195 entry_args = {
1196 'test-bad-datatype-arg': '12',
1197 }
1198 with self.assertRaises(ValueError) as e:
1199 self._DoReadFileDtb('65_entry_args_unknown_datatype.dts',
1200 entry_args=entry_args)
1201 self.assertIn('GetArg() internal error: Unknown data type ',
1202 str(e.exception))
1203
Simon Glassbb748372018-07-17 13:25:33 -06001204 def testText(self):
1205 """Test for a text entry type"""
1206 entry_args = {
1207 'test-id': TEXT_DATA,
1208 'test-id2': TEXT_DATA2,
1209 'test-id3': TEXT_DATA3,
1210 }
1211 data, _, _, _ = self._DoReadFileDtb('66_text.dts',
1212 entry_args=entry_args)
1213 expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 +
1214 TEXT_DATA3 + 'some text')
1215 self.assertEqual(expected, data)
1216
Simon Glassfd8d1f72018-07-17 13:25:36 -06001217 def testEntryDocs(self):
1218 """Test for creation of entry documentation"""
1219 with test_util.capture_sys_output() as (stdout, stderr):
1220 control.WriteEntryDocs(binman.GetEntryModules())
1221 self.assertTrue(len(stdout.getvalue()) > 0)
1222
1223 def testEntryDocsMissing(self):
1224 """Test handling of missing entry documentation"""
1225 with self.assertRaises(ValueError) as e:
1226 with test_util.capture_sys_output() as (stdout, stderr):
1227 control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
1228 self.assertIn('Documentation is missing for modules: u_boot',
1229 str(e.exception))
1230
Simon Glass11e36cc2018-07-17 13:25:38 -06001231 def testFmap(self):
1232 """Basic test of generation of a flashrom fmap"""
1233 data = self._DoReadFile('67_fmap.dts')
1234 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
1235 expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12
1236 self.assertEqual(expected, data[:32])
1237 self.assertEqual('__FMAP__', fhdr.signature)
1238 self.assertEqual(1, fhdr.ver_major)
1239 self.assertEqual(0, fhdr.ver_minor)
1240 self.assertEqual(0, fhdr.base)
1241 self.assertEqual(16 + 16 +
1242 fmap_util.FMAP_HEADER_LEN +
1243 fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size)
1244 self.assertEqual('FMAP', fhdr.name)
1245 self.assertEqual(3, fhdr.nareas)
1246 for fentry in fentries:
1247 self.assertEqual(0, fentry.flags)
1248
1249 self.assertEqual(0, fentries[0].offset)
1250 self.assertEqual(4, fentries[0].size)
1251 self.assertEqual('RO_U_BOOT', fentries[0].name)
1252
1253 self.assertEqual(16, fentries[1].offset)
1254 self.assertEqual(4, fentries[1].size)
1255 self.assertEqual('RW_U_BOOT', fentries[1].name)
1256
1257 self.assertEqual(32, fentries[2].offset)
1258 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1259 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
1260 self.assertEqual('FMAP', fentries[2].name)
1261
Simon Glassec127af2018-07-17 13:25:39 -06001262 def testBlobNamedByArg(self):
1263 """Test we can add a blob with the filename coming from an entry arg"""
1264 entry_args = {
1265 'cros-ec-rw-path': 'ecrw.bin',
1266 }
1267 data, _, _, _ = self._DoReadFileDtb('68_blob_named_by_arg.dts',
1268 entry_args=entry_args)
1269
Simon Glass3af8e492018-07-17 13:25:40 -06001270 def testFill(self):
1271 """Test for an fill entry type"""
1272 data = self._DoReadFile('69_fill.dts')
1273 expected = 8 * chr(0xff) + 8 * chr(0)
1274 self.assertEqual(expected, data)
1275
1276 def testFillNoSize(self):
1277 """Test for an fill entry type with no size"""
1278 with self.assertRaises(ValueError) as e:
1279 self._DoReadFile('70_fill_no_size.dts')
1280 self.assertIn("'fill' entry must have a size property",
1281 str(e.exception))
1282
Simon Glass0ef87aa2018-07-17 13:25:44 -06001283 def _HandleGbbCommand(self, pipe_list):
1284 """Fake calls to the futility utility"""
1285 if pipe_list[0][0] == 'futility':
1286 fname = pipe_list[0][-1]
1287 # Append our GBB data to the file, which will happen every time the
1288 # futility command is called.
1289 with open(fname, 'a') as fd:
1290 fd.write(GBB_DATA)
1291 return command.CommandResult()
1292
1293 def testGbb(self):
1294 """Test for the Chromium OS Google Binary Block"""
1295 command.test_result = self._HandleGbbCommand
1296 entry_args = {
1297 'keydir': 'devkeys',
1298 'bmpblk': 'bmpblk.bin',
1299 }
1300 data, _, _, _ = self._DoReadFileDtb('71_gbb.dts', entry_args=entry_args)
1301
1302 # Since futility
1303 expected = GBB_DATA + GBB_DATA + 8 * chr(0) + (0x2180 - 16) * chr(0)
1304 self.assertEqual(expected, data)
1305
1306 def testGbbTooSmall(self):
1307 """Test for the Chromium OS Google Binary Block being large enough"""
1308 with self.assertRaises(ValueError) as e:
1309 self._DoReadFileDtb('72_gbb_too_small.dts')
1310 self.assertIn("Node '/binman/gbb': GBB is too small",
1311 str(e.exception))
1312
1313 def testGbbNoSize(self):
1314 """Test for the Chromium OS Google Binary Block having a size"""
1315 with self.assertRaises(ValueError) as e:
1316 self._DoReadFileDtb('73_gbb_no_size.dts')
1317 self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
1318 str(e.exception))
1319
Simon Glass24d0d3c2018-07-17 13:25:47 -06001320 def _HandleVblockCommand(self, pipe_list):
1321 """Fake calls to the futility utility"""
1322 if pipe_list[0][0] == 'futility':
1323 fname = pipe_list[0][3]
1324 with open(fname, 'w') as fd:
1325 fd.write(VBLOCK_DATA)
1326 return command.CommandResult()
1327
1328 def testVblock(self):
1329 """Test for the Chromium OS Verified Boot Block"""
1330 command.test_result = self._HandleVblockCommand
1331 entry_args = {
1332 'keydir': 'devkeys',
1333 }
1334 data, _, _, _ = self._DoReadFileDtb('74_vblock.dts',
1335 entry_args=entry_args)
1336 expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA
1337 self.assertEqual(expected, data)
1338
1339 def testVblockNoContent(self):
1340 """Test we detect a vblock which has no content to sign"""
1341 with self.assertRaises(ValueError) as e:
1342 self._DoReadFile('75_vblock_no_content.dts')
1343 self.assertIn("Node '/binman/vblock': Vblock must have a 'content' "
1344 'property', str(e.exception))
1345
1346 def testVblockBadPhandle(self):
1347 """Test that we detect a vblock with an invalid phandle in contents"""
1348 with self.assertRaises(ValueError) as e:
1349 self._DoReadFile('76_vblock_bad_phandle.dts')
1350 self.assertIn("Node '/binman/vblock': Cannot find node for phandle "
1351 '1000', str(e.exception))
1352
1353 def testVblockBadEntry(self):
1354 """Test that we detect an entry that points to a non-entry"""
1355 with self.assertRaises(ValueError) as e:
1356 self._DoReadFile('77_vblock_bad_entry.dts')
1357 self.assertIn("Node '/binman/vblock': Cannot find entry for node "
1358 "'other'", str(e.exception))
1359
Simon Glassb8ef5b62018-07-17 13:25:48 -06001360 def testTpl(self):
1361 """Test that an image with TPL and ots device tree can be created"""
1362 # ELF file with a '__bss_size' symbol
1363 with open(self.TestFile('bss_data')) as fd:
1364 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
1365 data = self._DoReadFile('78_u_boot_tpl.dts')
1366 self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
1367
Simon Glass15a587c2018-07-17 13:25:51 -06001368 def testUsesPos(self):
1369 """Test that the 'pos' property cannot be used anymore"""
1370 with self.assertRaises(ValueError) as e:
1371 data = self._DoReadFile('79_uses_pos.dts')
1372 self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of "
1373 "'pos'", str(e.exception))
1374
Simon Glass53af22a2018-07-17 13:25:32 -06001375
Simon Glass9fc60b42017-11-12 21:52:22 -07001376if __name__ == "__main__":
1377 unittest.main()