blob: d800ba1e9d8fd47221f35a30284421a9803d4735 [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
Simon Glassd5164a72019-07-08 13:18:49 -06009from __future__ import print_function
10
Simon Glasse0e5df92018-09-14 04:57:31 -060011import hashlib
Simon Glass4f443042016-11-25 20:15:52 -070012from optparse import OptionParser
13import os
14import shutil
15import struct
16import sys
17import tempfile
18import unittest
19
20import binman
Simon Glassac62fba2019-07-08 13:18:53 -060021import cbfs_util
Simon Glass4f443042016-11-25 20:15:52 -070022import cmdline
23import command
24import control
Simon Glass19790632017-11-13 18:55:01 -070025import elf
Simon Glass99ed4a22017-05-27 07:38:30 -060026import fdt
Simon Glasse1925fa2019-07-08 14:25:44 -060027from etype import fdtmap
Simon Glass4f443042016-11-25 20:15:52 -070028import fdt_util
Simon Glass11e36cc2018-07-17 13:25:38 -060029import fmap_util
Simon Glassfd8d1f72018-07-17 13:25:36 -060030import test_util
Simon Glassc5ac1382019-07-08 13:18:54 -060031import gzip
Simon Glassc55a50f2018-09-14 04:57:19 -060032import state
Simon Glass4f443042016-11-25 20:15:52 -070033import tools
34import tout
35
36# Contents of test files, corresponding to different entry types
Simon Glassc6c10e72019-05-17 22:00:46 -060037U_BOOT_DATA = b'1234'
38U_BOOT_IMG_DATA = b'img'
39U_BOOT_SPL_DATA = b'56780123456789abcde'
40U_BOOT_TPL_DATA = b'tpl'
41BLOB_DATA = b'89'
42ME_DATA = b'0abcd'
43VGA_DATA = b'vga'
44U_BOOT_DTB_DATA = b'udtb'
45U_BOOT_SPL_DTB_DATA = b'spldtb'
46U_BOOT_TPL_DTB_DATA = b'tpldtb'
47X86_START16_DATA = b'start16'
48X86_START16_SPL_DATA = b'start16spl'
49X86_START16_TPL_DATA = b'start16tpl'
50PPC_MPC85XX_BR_DATA = b'ppcmpc85xxbr'
51U_BOOT_NODTB_DATA = b'nodtb with microcode pointer somewhere in here'
52U_BOOT_SPL_NODTB_DATA = b'splnodtb with microcode pointer somewhere in here'
53U_BOOT_TPL_NODTB_DATA = b'tplnodtb with microcode pointer somewhere in here'
54FSP_DATA = b'fsp'
55CMC_DATA = b'cmc'
56VBT_DATA = b'vbt'
57MRC_DATA = b'mrc'
Simon Glassbb748372018-07-17 13:25:33 -060058TEXT_DATA = 'text'
59TEXT_DATA2 = 'text2'
60TEXT_DATA3 = 'text3'
Simon Glassc6c10e72019-05-17 22:00:46 -060061CROS_EC_RW_DATA = b'ecrw'
62GBB_DATA = b'gbbd'
63BMPBLK_DATA = b'bmp'
64VBLOCK_DATA = b'vblk'
65FILES_DATA = (b"sorry I'm late\nOh, don't bother apologising, I'm " +
66 b"sorry you're alive\n")
Simon Glassff5c7e32019-07-08 13:18:42 -060067COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data'
Simon Glassc6c10e72019-05-17 22:00:46 -060068REFCODE_DATA = b'refcode'
Simon Glassec127af2018-07-17 13:25:39 -060069
Simon Glass4f443042016-11-25 20:15:52 -070070
71class TestFunctional(unittest.TestCase):
72 """Functional tests for binman
73
74 Most of these use a sample .dts file to build an image and then check
75 that it looks correct. The sample files are in the test/ subdirectory
76 and are numbered.
77
78 For each entry type a very small test file is created using fixed
79 string contents. This makes it easy to test that things look right, and
80 debug problems.
81
82 In some cases a 'real' file must be used - these are also supplied in
83 the test/ diurectory.
84 """
85 @classmethod
86 def setUpClass(self):
Simon Glass4d5994f2017-11-12 21:52:20 -070087 global entry
88 import entry
89
Simon Glass4f443042016-11-25 20:15:52 -070090 # Handle the case where argv[0] is 'python'
91 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
92 self._binman_pathname = os.path.join(self._binman_dir, 'binman')
93
94 # Create a temporary directory for input files
95 self._indir = tempfile.mkdtemp(prefix='binmant.')
96
97 # Create some test files
98 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
99 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
100 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
Simon Glassb8ef5b62018-07-17 13:25:48 -0600101 TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700102 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700103 TestFunctional._MakeInputFile('me.bin', ME_DATA)
104 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glassb8ef5b62018-07-17 13:25:48 -0600105 self._ResetDtbs()
Simon Glasse0ff8552016-11-25 20:15:53 -0700106 TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
Jagdish Gediya9d368f32018-09-03 21:35:08 +0530107 TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA)
Simon Glass87722132017-11-12 21:52:26 -0700108 TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
109 X86_START16_SPL_DATA)
Simon Glass35b384c2018-09-14 04:57:10 -0600110 TestFunctional._MakeInputFile('tpl/u-boot-x86-16bit-tpl.bin',
111 X86_START16_TPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700112 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass6b187df2017-11-12 21:52:27 -0700113 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
114 U_BOOT_SPL_NODTB_DATA)
Simon Glassf0253632018-09-14 04:57:32 -0600115 TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin',
116 U_BOOT_TPL_NODTB_DATA)
Simon Glassda229092016-11-25 20:15:56 -0700117 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
118 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Meng59ea8c22017-08-15 22:41:54 -0700119 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassca4f4ff2017-11-12 21:52:28 -0700120 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glassec127af2018-07-17 13:25:39 -0600121 TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
Simon Glass0ef87aa2018-07-17 13:25:44 -0600122 TestFunctional._MakeInputDir('devkeys')
123 TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
Simon Glass3ae192c2018-10-01 12:22:31 -0600124 TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700125
Simon Glasse0ff8552016-11-25 20:15:53 -0700126 # ELF file with a '_dt_ucode_base_size' symbol
Simon Glass1d0ebf72019-05-14 15:53:42 -0600127 with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd:
Simon Glasse0ff8552016-11-25 20:15:53 -0700128 TestFunctional._MakeInputFile('u-boot', fd.read())
129
130 # Intel flash descriptor file
Simon Glass1d0ebf72019-05-14 15:53:42 -0600131 with open(self.TestFile('descriptor.bin'), 'rb') as fd:
Simon Glasse0ff8552016-11-25 20:15:53 -0700132 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
133
Simon Glass0a98b282018-09-14 04:57:28 -0600134 shutil.copytree(self.TestFile('files'),
135 os.path.join(self._indir, 'files'))
136
Simon Glass83d73c22018-09-14 04:57:26 -0600137 TestFunctional._MakeInputFile('compress', COMPRESS_DATA)
138
Simon Glassac62fba2019-07-08 13:18:53 -0600139 # Travis-CI may have an old lz4
140 self.have_lz4 = True
141 try:
142 tools.Run('lz4', '--no-frame-crc', '-c',
143 os.path.join(self._indir, 'u-boot.bin'))
144 except:
145 self.have_lz4 = False
146
Simon Glass4f443042016-11-25 20:15:52 -0700147 @classmethod
148 def tearDownClass(self):
149 """Remove the temporary input directory and its contents"""
Simon Glassd5164a72019-07-08 13:18:49 -0600150 if self.preserve_indir:
151 print('Preserving input dir: %s' % self._indir)
152 else:
153 if self._indir:
154 shutil.rmtree(self._indir)
Simon Glass4f443042016-11-25 20:15:52 -0700155 self._indir = None
156
Simon Glassd5164a72019-07-08 13:18:49 -0600157 @classmethod
Simon Glass8acce602019-07-08 13:18:50 -0600158 def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False,
Simon Glass53cd5d92019-07-08 14:25:29 -0600159 toolpath=None, verbosity=None):
Simon Glassd5164a72019-07-08 13:18:49 -0600160 """Accept arguments controlling test execution
161
162 Args:
163 preserve_indir: Preserve the shared input directory used by all
164 tests in this class.
165 preserve_outdir: Preserve the output directories used by tests. Each
166 test has its own, so this is normally only useful when running a
167 single test.
Simon Glass8acce602019-07-08 13:18:50 -0600168 toolpath: ist of paths to use for tools
Simon Glassd5164a72019-07-08 13:18:49 -0600169 """
170 cls.preserve_indir = preserve_indir
171 cls.preserve_outdirs = preserve_outdirs
Simon Glass8acce602019-07-08 13:18:50 -0600172 cls.toolpath = toolpath
Simon Glass53cd5d92019-07-08 14:25:29 -0600173 cls.verbosity = verbosity
Simon Glassd5164a72019-07-08 13:18:49 -0600174
Simon Glassac62fba2019-07-08 13:18:53 -0600175 def _CheckLz4(self):
176 if not self.have_lz4:
177 self.skipTest('lz4 --no-frame-crc not available')
178
Simon Glass4f443042016-11-25 20:15:52 -0700179 def setUp(self):
180 # Enable this to turn on debugging output
181 # tout.Init(tout.DEBUG)
182 command.test_result = None
183
184 def tearDown(self):
185 """Remove the temporary output directory"""
Simon Glassd5164a72019-07-08 13:18:49 -0600186 if self.preserve_outdirs:
187 print('Preserving output dir: %s' % tools.outdir)
188 else:
189 tools._FinaliseForTest()
Simon Glass4f443042016-11-25 20:15:52 -0700190
Simon Glassb8ef5b62018-07-17 13:25:48 -0600191 @classmethod
192 def _ResetDtbs(self):
193 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
194 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
195 TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA)
196
Simon Glass4f443042016-11-25 20:15:52 -0700197 def _RunBinman(self, *args, **kwargs):
198 """Run binman using the command line
199
200 Args:
201 Arguments to pass, as a list of strings
202 kwargs: Arguments to pass to Command.RunPipe()
203 """
204 result = command.RunPipe([[self._binman_pathname] + list(args)],
205 capture=True, capture_stderr=True, raise_on_error=False)
206 if result.return_code and kwargs.get('raise_on_error', True):
207 raise Exception("Error running '%s': %s" % (' '.join(args),
208 result.stdout + result.stderr))
209 return result
210
Simon Glass53cd5d92019-07-08 14:25:29 -0600211 def _DoBinman(self, *argv):
Simon Glass4f443042016-11-25 20:15:52 -0700212 """Run binman using directly (in the same process)
213
214 Args:
215 Arguments to pass, as a list of strings
216 Returns:
217 Return value (0 for success)
218 """
Simon Glass53cd5d92019-07-08 14:25:29 -0600219 argv = list(argv)
220 args = cmdline.ParseArgs(argv)
221 args.pager = 'binman-invalid-pager'
222 args.build_dir = self._indir
Simon Glass4f443042016-11-25 20:15:52 -0700223
224 # For testing, you can force an increase in verbosity here
Simon Glass53cd5d92019-07-08 14:25:29 -0600225 # args.verbosity = tout.DEBUG
226 return control.Binman(args)
Simon Glass4f443042016-11-25 20:15:52 -0700227
Simon Glass53af22a2018-07-17 13:25:32 -0600228 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
Simon Glasseb833d82019-04-25 21:58:34 -0600229 entry_args=None, images=None, use_real_dtb=False,
230 verbosity=None):
Simon Glass4f443042016-11-25 20:15:52 -0700231 """Run binman with a given test file
232
233 Args:
Simon Glass741f2d62018-10-01 12:22:30 -0600234 fname: Device-tree source filename to use (e.g. 005_simple.dts)
Simon Glass7ae5f312018-06-01 09:38:19 -0600235 debug: True to enable debugging output
Simon Glass3b0c3822018-06-01 09:38:20 -0600236 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600237 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600238 tree before packing it into the image
Simon Glass0bfa7b02018-09-14 04:57:12 -0600239 entry_args: Dict of entry args to supply to binman
240 key: arg name
241 value: value of that arg
242 images: List of image names to build
Simon Glass4f443042016-11-25 20:15:52 -0700243 """
Simon Glass53cd5d92019-07-08 14:25:29 -0600244 args = []
Simon Glass7fe91732017-11-13 18:55:00 -0700245 if debug:
246 args.append('-D')
Simon Glass53cd5d92019-07-08 14:25:29 -0600247 if verbosity is not None:
248 args.append('-v%d' % verbosity)
249 elif self.verbosity:
250 args.append('-v%d' % self.verbosity)
251 if self.toolpath:
252 for path in self.toolpath:
253 args += ['--toolpath', path]
254 args += ['build', '-p', '-I', self._indir, '-d', self.TestFile(fname)]
Simon Glass3b0c3822018-06-01 09:38:20 -0600255 if map:
256 args.append('-m')
Simon Glass16b8d6b2018-07-06 10:27:42 -0600257 if update_dtb:
Simon Glass2569e102019-07-08 13:18:47 -0600258 args.append('-u')
Simon Glass93d17412018-09-14 04:57:23 -0600259 if not use_real_dtb:
260 args.append('--fake-dtb')
Simon Glass53af22a2018-07-17 13:25:32 -0600261 if entry_args:
Simon Glass50979152019-05-14 15:53:41 -0600262 for arg, value in entry_args.items():
Simon Glass53af22a2018-07-17 13:25:32 -0600263 args.append('-a%s=%s' % (arg, value))
Simon Glass0bfa7b02018-09-14 04:57:12 -0600264 if images:
265 for image in images:
266 args += ['-i', image]
Simon Glass7fe91732017-11-13 18:55:00 -0700267 return self._DoBinman(*args)
Simon Glass4f443042016-11-25 20:15:52 -0700268
269 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glasse0ff8552016-11-25 20:15:53 -0700270 """Set up a new test device-tree file
271
272 The given file is compiled and set up as the device tree to be used
273 for ths test.
274
275 Args:
276 fname: Filename of .dts file to read
Simon Glass7ae5f312018-06-01 09:38:19 -0600277 outfile: Output filename for compiled device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700278
279 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600280 Contents of device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700281 """
Simon Glasse0e62752018-10-01 21:12:41 -0600282 tools.PrepareOutputDir(None)
Simon Glass4f443042016-11-25 20:15:52 -0700283 dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
Simon Glass1d0ebf72019-05-14 15:53:42 -0600284 with open(dtb, 'rb') as fd:
Simon Glass4f443042016-11-25 20:15:52 -0700285 data = fd.read()
286 TestFunctional._MakeInputFile(outfile, data)
Simon Glasse0e62752018-10-01 21:12:41 -0600287 tools.FinaliseOutputDir()
288 return data
Simon Glass4f443042016-11-25 20:15:52 -0700289
Simon Glass6ed45ba2018-09-14 04:57:24 -0600290 def _GetDtbContentsForSplTpl(self, dtb_data, name):
291 """Create a version of the main DTB for SPL or SPL
292
293 For testing we don't actually have different versions of the DTB. With
294 U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests
295 we don't normally have any unwanted nodes.
296
297 We still want the DTBs for SPL and TPL to be different though, since
298 otherwise it is confusing to know which one we are looking at. So add
299 an 'spl' or 'tpl' property to the top-level node.
300 """
301 dtb = fdt.Fdt.FromData(dtb_data)
302 dtb.Scan()
303 dtb.GetNode('/binman').AddZeroProp(name)
304 dtb.Sync(auto_resize=True)
305 dtb.Pack()
306 return dtb.GetContents()
307
Simon Glass16b8d6b2018-07-06 10:27:42 -0600308 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass6ed45ba2018-09-14 04:57:24 -0600309 update_dtb=False, entry_args=None, reset_dtbs=True):
Simon Glass4f443042016-11-25 20:15:52 -0700310 """Run binman and return the resulting image
311
312 This runs binman with a given test file and then reads the resulting
313 output file. It is a shortcut function since most tests need to do
314 these steps.
315
316 Raises an assertion failure if binman returns a non-zero exit code.
317
318 Args:
Simon Glass741f2d62018-10-01 12:22:30 -0600319 fname: Device-tree source filename to use (e.g. 005_simple.dts)
Simon Glass4f443042016-11-25 20:15:52 -0700320 use_real_dtb: True to use the test file as the contents of
321 the u-boot-dtb entry. Normally this is not needed and the
322 test contents (the U_BOOT_DTB_DATA string) can be used.
323 But in some test we need the real contents.
Simon Glass3b0c3822018-06-01 09:38:20 -0600324 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600325 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600326 tree before packing it into the image
Simon Glasse0ff8552016-11-25 20:15:53 -0700327
328 Returns:
329 Tuple:
330 Resulting image contents
331 Device tree contents
Simon Glass3b0c3822018-06-01 09:38:20 -0600332 Map data showing contents of image (or None if none)
Simon Glassea6922e2018-07-17 13:25:27 -0600333 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass4f443042016-11-25 20:15:52 -0700334 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700335 dtb_data = None
Simon Glass4f443042016-11-25 20:15:52 -0700336 # Use the compiled test file as the u-boot-dtb input
337 if use_real_dtb:
Simon Glasse0ff8552016-11-25 20:15:53 -0700338 dtb_data = self._SetupDtb(fname)
Simon Glass6ed45ba2018-09-14 04:57:24 -0600339
340 # For testing purposes, make a copy of the DT for SPL and TPL. Add
341 # a node indicating which it is, so aid verification.
342 for name in ['spl', 'tpl']:
343 dtb_fname = '%s/u-boot-%s.dtb' % (name, name)
344 outfile = os.path.join(self._indir, dtb_fname)
345 TestFunctional._MakeInputFile(dtb_fname,
346 self._GetDtbContentsForSplTpl(dtb_data, name))
Simon Glass4f443042016-11-25 20:15:52 -0700347
348 try:
Simon Glass53af22a2018-07-17 13:25:32 -0600349 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
Simon Glass6ed45ba2018-09-14 04:57:24 -0600350 entry_args=entry_args, use_real_dtb=use_real_dtb)
Simon Glass4f443042016-11-25 20:15:52 -0700351 self.assertEqual(0, retcode)
Simon Glass6ed45ba2018-09-14 04:57:24 -0600352 out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out')
Simon Glass4f443042016-11-25 20:15:52 -0700353
354 # Find the (only) image, read it and return its contents
355 image = control.images['image']
Simon Glass16b8d6b2018-07-06 10:27:42 -0600356 image_fname = tools.GetOutputFilename('image.bin')
357 self.assertTrue(os.path.exists(image_fname))
Simon Glass3b0c3822018-06-01 09:38:20 -0600358 if map:
359 map_fname = tools.GetOutputFilename('image.map')
360 with open(map_fname) as fd:
361 map_data = fd.read()
362 else:
363 map_data = None
Simon Glass1d0ebf72019-05-14 15:53:42 -0600364 with open(image_fname, 'rb') as fd:
Simon Glass16b8d6b2018-07-06 10:27:42 -0600365 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass4f443042016-11-25 20:15:52 -0700366 finally:
367 # Put the test file back
Simon Glass6ed45ba2018-09-14 04:57:24 -0600368 if reset_dtbs and use_real_dtb:
Simon Glassb8ef5b62018-07-17 13:25:48 -0600369 self._ResetDtbs()
Simon Glass4f443042016-11-25 20:15:52 -0700370
Simon Glass3c081312019-07-08 14:25:26 -0600371 def _DoReadFileRealDtb(self, fname):
372 """Run binman with a real .dtb file and return the resulting data
373
374 Args:
375 fname: DT source filename to use (e.g. 082_fdt_update_all.dts)
376
377 Returns:
378 Resulting image contents
379 """
380 return self._DoReadFileDtb(fname, use_real_dtb=True, update_dtb=True)[0]
381
Simon Glasse0ff8552016-11-25 20:15:53 -0700382 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass7ae5f312018-06-01 09:38:19 -0600383 """Helper function which discards the device-tree binary
384
385 Args:
Simon Glass741f2d62018-10-01 12:22:30 -0600386 fname: Device-tree source filename to use (e.g. 005_simple.dts)
Simon Glass7ae5f312018-06-01 09:38:19 -0600387 use_real_dtb: True to use the test file as the contents of
388 the u-boot-dtb entry. Normally this is not needed and the
389 test contents (the U_BOOT_DTB_DATA string) can be used.
390 But in some test we need the real contents.
Simon Glassea6922e2018-07-17 13:25:27 -0600391
392 Returns:
393 Resulting image contents
Simon Glass7ae5f312018-06-01 09:38:19 -0600394 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700395 return self._DoReadFileDtb(fname, use_real_dtb)[0]
396
Simon Glass4f443042016-11-25 20:15:52 -0700397 @classmethod
398 def _MakeInputFile(self, fname, contents):
399 """Create a new test input file, creating directories as needed
400
401 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600402 fname: Filename to create
Simon Glass4f443042016-11-25 20:15:52 -0700403 contents: File contents to write in to the file
404 Returns:
405 Full pathname of file created
406 """
407 pathname = os.path.join(self._indir, fname)
408 dirname = os.path.dirname(pathname)
409 if dirname and not os.path.exists(dirname):
410 os.makedirs(dirname)
411 with open(pathname, 'wb') as fd:
412 fd.write(contents)
413 return pathname
414
415 @classmethod
Simon Glass0ef87aa2018-07-17 13:25:44 -0600416 def _MakeInputDir(self, dirname):
417 """Create a new test input directory, creating directories as needed
418
419 Args:
420 dirname: Directory name to create
421
422 Returns:
423 Full pathname of directory created
424 """
425 pathname = os.path.join(self._indir, dirname)
426 if not os.path.exists(pathname):
427 os.makedirs(pathname)
428 return pathname
429
430 @classmethod
Simon Glass11ae93e2018-10-01 21:12:47 -0600431 def _SetupSplElf(self, src_fname='bss_data'):
432 """Set up an ELF file with a '_dt_ucode_base_size' symbol
433
434 Args:
435 Filename of ELF file to use as SPL
436 """
Simon Glass1d0ebf72019-05-14 15:53:42 -0600437 with open(self.TestFile(src_fname), 'rb') as fd:
Simon Glass11ae93e2018-10-01 21:12:47 -0600438 TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
439
440 @classmethod
Simon Glass4f443042016-11-25 20:15:52 -0700441 def TestFile(self, fname):
442 return os.path.join(self._binman_dir, 'test', fname)
443
444 def AssertInList(self, grep_list, target):
445 """Assert that at least one of a list of things is in a target
446
447 Args:
448 grep_list: List of strings to check
449 target: Target string
450 """
451 for grep in grep_list:
452 if grep in target:
453 return
Simon Glass1fc62de2019-05-17 22:00:50 -0600454 self.fail("Error: '%s' not found in '%s'" % (grep_list, target))
Simon Glass4f443042016-11-25 20:15:52 -0700455
456 def CheckNoGaps(self, entries):
457 """Check that all entries fit together without gaps
458
459 Args:
460 entries: List of entries to check
461 """
Simon Glass3ab95982018-08-01 15:22:37 -0600462 offset = 0
Simon Glass4f443042016-11-25 20:15:52 -0700463 for entry in entries.values():
Simon Glass3ab95982018-08-01 15:22:37 -0600464 self.assertEqual(offset, entry.offset)
465 offset += entry.size
Simon Glass4f443042016-11-25 20:15:52 -0700466
Simon Glasse0ff8552016-11-25 20:15:53 -0700467 def GetFdtLen(self, dtb):
Simon Glass7ae5f312018-06-01 09:38:19 -0600468 """Get the totalsize field from a device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700469
470 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600471 dtb: Device-tree binary contents
Simon Glasse0ff8552016-11-25 20:15:53 -0700472
473 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600474 Total size of device-tree binary, from the header
Simon Glasse0ff8552016-11-25 20:15:53 -0700475 """
476 return struct.unpack('>L', dtb[4:8])[0]
477
Simon Glass086cec92019-07-08 14:25:27 -0600478 def _GetPropTree(self, dtb, prop_names, prefix='/binman/'):
Simon Glass16b8d6b2018-07-06 10:27:42 -0600479 def AddNode(node, path):
480 if node.name != '/':
481 path += '/' + node.name
Simon Glass086cec92019-07-08 14:25:27 -0600482 for prop in node.props.values():
483 if prop.name in prop_names:
484 prop_path = path + ':' + prop.name
485 tree[prop_path[len(prefix):]] = fdt_util.fdt32_to_cpu(
486 prop.value)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600487 for subnode in node.subnodes:
Simon Glass16b8d6b2018-07-06 10:27:42 -0600488 AddNode(subnode, path)
489
490 tree = {}
Simon Glass16b8d6b2018-07-06 10:27:42 -0600491 AddNode(dtb.GetRoot(), '')
492 return tree
493
Simon Glass4f443042016-11-25 20:15:52 -0700494 def testRun(self):
495 """Test a basic run with valid args"""
496 result = self._RunBinman('-h')
497
498 def testFullHelp(self):
499 """Test that the full help is displayed with -H"""
500 result = self._RunBinman('-H')
501 help_file = os.path.join(self._binman_dir, 'README')
Tom Rini3759df02018-01-16 15:29:50 -0500502 # Remove possible extraneous strings
503 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
504 gothelp = result.stdout.replace(extra, '')
505 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass4f443042016-11-25 20:15:52 -0700506 self.assertEqual(0, len(result.stderr))
507 self.assertEqual(0, result.return_code)
508
509 def testFullHelpInternal(self):
510 """Test that the full help is displayed with -H"""
511 try:
512 command.test_result = command.CommandResult()
513 result = self._DoBinman('-H')
514 help_file = os.path.join(self._binman_dir, 'README')
515 finally:
516 command.test_result = None
517
518 def testHelp(self):
519 """Test that the basic help is displayed with -h"""
520 result = self._RunBinman('-h')
521 self.assertTrue(len(result.stdout) > 200)
522 self.assertEqual(0, len(result.stderr))
523 self.assertEqual(0, result.return_code)
524
Simon Glass4f443042016-11-25 20:15:52 -0700525 def testBoard(self):
526 """Test that we can run it with a specific board"""
Simon Glass741f2d62018-10-01 12:22:30 -0600527 self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb')
Simon Glass4f443042016-11-25 20:15:52 -0700528 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
Simon Glass53cd5d92019-07-08 14:25:29 -0600529 result = self._DoBinman('build', '-b', 'sandbox')
Simon Glass4f443042016-11-25 20:15:52 -0700530 self.assertEqual(0, result)
531
532 def testNeedBoard(self):
533 """Test that we get an error when no board ius supplied"""
534 with self.assertRaises(ValueError) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600535 result = self._DoBinman('build')
Simon Glass4f443042016-11-25 20:15:52 -0700536 self.assertIn("Must provide a board to process (use -b <board>)",
537 str(e.exception))
538
539 def testMissingDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600540 """Test that an invalid device-tree file generates an error"""
Simon Glass4f443042016-11-25 20:15:52 -0700541 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600542 self._RunBinman('build', '-d', 'missing_file')
Simon Glass4f443042016-11-25 20:15:52 -0700543 # We get one error from libfdt, and a different one from fdtget.
544 self.AssertInList(["Couldn't open blob from 'missing_file'",
545 'No such file or directory'], str(e.exception))
546
547 def testBrokenDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600548 """Test that an invalid device-tree source file generates an error
Simon Glass4f443042016-11-25 20:15:52 -0700549
550 Since this is a source file it should be compiled and the error
551 will come from the device-tree compiler (dtc).
552 """
553 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600554 self._RunBinman('build', '-d', self.TestFile('001_invalid.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700555 self.assertIn("FATAL ERROR: Unable to parse input tree",
556 str(e.exception))
557
558 def testMissingNode(self):
559 """Test that a device tree without a 'binman' node generates an error"""
560 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600561 self._DoBinman('build', '-d', self.TestFile('002_missing_node.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700562 self.assertIn("does not have a 'binman' node", str(e.exception))
563
564 def testEmpty(self):
565 """Test that an empty binman node works OK (i.e. does nothing)"""
Simon Glass53cd5d92019-07-08 14:25:29 -0600566 result = self._RunBinman('build', '-d', self.TestFile('003_empty.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700567 self.assertEqual(0, len(result.stderr))
568 self.assertEqual(0, result.return_code)
569
570 def testInvalidEntry(self):
571 """Test that an invalid entry is flagged"""
572 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600573 result = self._RunBinman('build', '-d',
Simon Glass741f2d62018-10-01 12:22:30 -0600574 self.TestFile('004_invalid_entry.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700575 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
576 "'/binman/not-a-valid-type'", str(e.exception))
577
578 def testSimple(self):
579 """Test a simple binman with a single file"""
Simon Glass741f2d62018-10-01 12:22:30 -0600580 data = self._DoReadFile('005_simple.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700581 self.assertEqual(U_BOOT_DATA, data)
582
Simon Glass7fe91732017-11-13 18:55:00 -0700583 def testSimpleDebug(self):
584 """Test a simple binman run with debugging enabled"""
Simon Glass741f2d62018-10-01 12:22:30 -0600585 data = self._DoTestFile('005_simple.dts', debug=True)
Simon Glass7fe91732017-11-13 18:55:00 -0700586
Simon Glass4f443042016-11-25 20:15:52 -0700587 def testDual(self):
588 """Test that we can handle creating two images
589
590 This also tests image padding.
591 """
Simon Glass741f2d62018-10-01 12:22:30 -0600592 retcode = self._DoTestFile('006_dual_image.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700593 self.assertEqual(0, retcode)
594
595 image = control.images['image1']
596 self.assertEqual(len(U_BOOT_DATA), image._size)
597 fname = tools.GetOutputFilename('image1.bin')
598 self.assertTrue(os.path.exists(fname))
Simon Glass1d0ebf72019-05-14 15:53:42 -0600599 with open(fname, 'rb') as fd:
Simon Glass4f443042016-11-25 20:15:52 -0700600 data = fd.read()
601 self.assertEqual(U_BOOT_DATA, data)
602
603 image = control.images['image2']
604 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
605 fname = tools.GetOutputFilename('image2.bin')
606 self.assertTrue(os.path.exists(fname))
Simon Glass1d0ebf72019-05-14 15:53:42 -0600607 with open(fname, 'rb') as fd:
Simon Glass4f443042016-11-25 20:15:52 -0700608 data = fd.read()
609 self.assertEqual(U_BOOT_DATA, data[3:7])
Simon Glasse6d85ff2019-05-14 15:53:47 -0600610 self.assertEqual(tools.GetBytes(0, 3), data[:3])
611 self.assertEqual(tools.GetBytes(0, 5), data[7:])
Simon Glass4f443042016-11-25 20:15:52 -0700612
613 def testBadAlign(self):
614 """Test that an invalid alignment value is detected"""
615 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600616 self._DoTestFile('007_bad_align.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700617 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
618 "of two", str(e.exception))
619
620 def testPackSimple(self):
621 """Test that packing works as expected"""
Simon Glass741f2d62018-10-01 12:22:30 -0600622 retcode = self._DoTestFile('008_pack.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700623 self.assertEqual(0, retcode)
624 self.assertIn('image', control.images)
625 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600626 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700627 self.assertEqual(5, len(entries))
628
629 # First u-boot
630 self.assertIn('u-boot', entries)
631 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600632 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700633 self.assertEqual(len(U_BOOT_DATA), entry.size)
634
635 # Second u-boot, aligned to 16-byte boundary
636 self.assertIn('u-boot-align', entries)
637 entry = entries['u-boot-align']
Simon Glass3ab95982018-08-01 15:22:37 -0600638 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700639 self.assertEqual(len(U_BOOT_DATA), entry.size)
640
641 # Third u-boot, size 23 bytes
642 self.assertIn('u-boot-size', entries)
643 entry = entries['u-boot-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600644 self.assertEqual(20, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700645 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
646 self.assertEqual(23, entry.size)
647
648 # Fourth u-boot, placed immediate after the above
649 self.assertIn('u-boot-next', entries)
650 entry = entries['u-boot-next']
Simon Glass3ab95982018-08-01 15:22:37 -0600651 self.assertEqual(43, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700652 self.assertEqual(len(U_BOOT_DATA), entry.size)
653
Simon Glass3ab95982018-08-01 15:22:37 -0600654 # Fifth u-boot, placed at a fixed offset
Simon Glass4f443042016-11-25 20:15:52 -0700655 self.assertIn('u-boot-fixed', entries)
656 entry = entries['u-boot-fixed']
Simon Glass3ab95982018-08-01 15:22:37 -0600657 self.assertEqual(61, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700658 self.assertEqual(len(U_BOOT_DATA), entry.size)
659
660 self.assertEqual(65, image._size)
661
662 def testPackExtra(self):
663 """Test that extra packing feature works as expected"""
Simon Glass741f2d62018-10-01 12:22:30 -0600664 retcode = self._DoTestFile('009_pack_extra.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700665
666 self.assertEqual(0, retcode)
667 self.assertIn('image', control.images)
668 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600669 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700670 self.assertEqual(5, len(entries))
671
672 # First u-boot with padding before and after
673 self.assertIn('u-boot', entries)
674 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600675 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700676 self.assertEqual(3, entry.pad_before)
677 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
678
679 # Second u-boot has an aligned size, but it has no effect
680 self.assertIn('u-boot-align-size-nop', entries)
681 entry = entries['u-boot-align-size-nop']
Simon Glass3ab95982018-08-01 15:22:37 -0600682 self.assertEqual(12, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700683 self.assertEqual(4, entry.size)
684
685 # Third u-boot has an aligned size too
686 self.assertIn('u-boot-align-size', entries)
687 entry = entries['u-boot-align-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600688 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700689 self.assertEqual(32, entry.size)
690
691 # Fourth u-boot has an aligned end
692 self.assertIn('u-boot-align-end', entries)
693 entry = entries['u-boot-align-end']
Simon Glass3ab95982018-08-01 15:22:37 -0600694 self.assertEqual(48, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700695 self.assertEqual(16, entry.size)
696
697 # Fifth u-boot immediately afterwards
698 self.assertIn('u-boot-align-both', entries)
699 entry = entries['u-boot-align-both']
Simon Glass3ab95982018-08-01 15:22:37 -0600700 self.assertEqual(64, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700701 self.assertEqual(64, entry.size)
702
703 self.CheckNoGaps(entries)
704 self.assertEqual(128, image._size)
705
706 def testPackAlignPowerOf2(self):
707 """Test that invalid entry alignment is detected"""
708 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600709 self._DoTestFile('010_pack_align_power2.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700710 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
711 "of two", str(e.exception))
712
713 def testPackAlignSizePowerOf2(self):
714 """Test that invalid entry size alignment is detected"""
715 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600716 self._DoTestFile('011_pack_align_size_power2.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700717 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
718 "power of two", str(e.exception))
719
720 def testPackInvalidAlign(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600721 """Test detection of an offset that does not match its alignment"""
Simon Glass4f443042016-11-25 20:15:52 -0700722 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600723 self._DoTestFile('012_pack_inv_align.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600724 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700725 "align 0x4 (4)", str(e.exception))
726
727 def testPackInvalidSizeAlign(self):
728 """Test that invalid entry size alignment is detected"""
729 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600730 self._DoTestFile('013_pack_inv_size_align.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700731 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
732 "align-size 0x4 (4)", str(e.exception))
733
734 def testPackOverlap(self):
735 """Test that overlapping regions are detected"""
736 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600737 self._DoTestFile('014_pack_overlap.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600738 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700739 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
740 str(e.exception))
741
742 def testPackEntryOverflow(self):
743 """Test that entries that overflow their size are detected"""
744 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600745 self._DoTestFile('015_pack_overflow.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700746 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
747 "but entry size is 0x3 (3)", str(e.exception))
748
749 def testPackImageOverflow(self):
750 """Test that entries which overflow the image size are detected"""
751 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600752 self._DoTestFile('016_pack_image_overflow.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600753 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass4f443042016-11-25 20:15:52 -0700754 "size 0x3 (3)", str(e.exception))
755
756 def testPackImageSize(self):
757 """Test that the image size can be set"""
Simon Glass741f2d62018-10-01 12:22:30 -0600758 retcode = self._DoTestFile('017_pack_image_size.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700759 self.assertEqual(0, retcode)
760 self.assertIn('image', control.images)
761 image = control.images['image']
762 self.assertEqual(7, image._size)
763
764 def testPackImageSizeAlign(self):
765 """Test that image size alignemnt works as expected"""
Simon Glass741f2d62018-10-01 12:22:30 -0600766 retcode = self._DoTestFile('018_pack_image_align.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700767 self.assertEqual(0, retcode)
768 self.assertIn('image', control.images)
769 image = control.images['image']
770 self.assertEqual(16, image._size)
771
772 def testPackInvalidImageAlign(self):
773 """Test that invalid image alignment is detected"""
774 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600775 self._DoTestFile('019_pack_inv_image_align.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600776 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700777 "align-size 0x8 (8)", str(e.exception))
778
779 def testPackAlignPowerOf2(self):
780 """Test that invalid image alignment is detected"""
781 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600782 self._DoTestFile('020_pack_inv_image_align_power2.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600783 self.assertIn("Section '/binman': Alignment size 131 must be a power of "
Simon Glass4f443042016-11-25 20:15:52 -0700784 "two", str(e.exception))
785
786 def testImagePadByte(self):
787 """Test that the image pad byte can be specified"""
Simon Glass11ae93e2018-10-01 21:12:47 -0600788 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -0600789 data = self._DoReadFile('021_image_pad.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -0600790 self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) +
791 U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700792
793 def testImageName(self):
794 """Test that image files can be named"""
Simon Glass741f2d62018-10-01 12:22:30 -0600795 retcode = self._DoTestFile('022_image_name.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700796 self.assertEqual(0, retcode)
797 image = control.images['image1']
798 fname = tools.GetOutputFilename('test-name')
799 self.assertTrue(os.path.exists(fname))
800
801 image = control.images['image2']
802 fname = tools.GetOutputFilename('test-name.xx')
803 self.assertTrue(os.path.exists(fname))
804
805 def testBlobFilename(self):
806 """Test that generic blobs can be provided by filename"""
Simon Glass741f2d62018-10-01 12:22:30 -0600807 data = self._DoReadFile('023_blob.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700808 self.assertEqual(BLOB_DATA, data)
809
810 def testPackSorted(self):
811 """Test that entries can be sorted"""
Simon Glass11ae93e2018-10-01 21:12:47 -0600812 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -0600813 data = self._DoReadFile('024_sorted.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -0600814 self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA +
815 tools.GetBytes(0, 2) + U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700816
Simon Glass3ab95982018-08-01 15:22:37 -0600817 def testPackZeroOffset(self):
818 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass4f443042016-11-25 20:15:52 -0700819 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600820 self._DoTestFile('025_pack_zero_size.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600821 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700822 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
823 str(e.exception))
824
825 def testPackUbootDtb(self):
826 """Test that a device tree can be added to U-Boot"""
Simon Glass741f2d62018-10-01 12:22:30 -0600827 data = self._DoReadFile('026_pack_u_boot_dtb.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700828 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700829
830 def testPackX86RomNoSize(self):
831 """Test that the end-at-4gb property requires a size property"""
832 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600833 self._DoTestFile('027_pack_4gb_no_size.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600834 self.assertIn("Section '/binman': Section size must be provided when "
Simon Glasse0ff8552016-11-25 20:15:53 -0700835 "using end-at-4gb", str(e.exception))
836
Jagdish Gediya94b57db2018-09-03 21:35:07 +0530837 def test4gbAndSkipAtStartTogether(self):
838 """Test that the end-at-4gb and skip-at-size property can't be used
839 together"""
840 with self.assertRaises(ValueError) as e:
841 self._DoTestFile('80_4gb_and_skip_at_start_together.dts')
842 self.assertIn("Section '/binman': Provide either 'end-at-4gb' or "
843 "'skip-at-start'", str(e.exception))
844
Simon Glasse0ff8552016-11-25 20:15:53 -0700845 def testPackX86RomOutside(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600846 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glasse0ff8552016-11-25 20:15:53 -0700847 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600848 self._DoTestFile('028_pack_4gb_outside.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600849 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glass8f1da502018-06-01 09:38:12 -0600850 "the section starting at 0xffffffe0 (4294967264)",
Simon Glasse0ff8552016-11-25 20:15:53 -0700851 str(e.exception))
852
853 def testPackX86Rom(self):
854 """Test that a basic x86 ROM can be created"""
Simon Glass11ae93e2018-10-01 21:12:47 -0600855 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -0600856 data = self._DoReadFile('029_x86-rom.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -0600857 self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 7) + U_BOOT_SPL_DATA +
858 tools.GetBytes(0, 2), data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700859
860 def testPackX86RomMeNoDesc(self):
861 """Test that an invalid Intel descriptor entry is detected"""
Simon Glassc6c10e72019-05-17 22:00:46 -0600862 TestFunctional._MakeInputFile('descriptor.bin', b'')
Simon Glasse0ff8552016-11-25 20:15:53 -0700863 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600864 self._DoTestFile('031_x86-rom-me.dts')
Simon Glass458be452019-07-08 13:18:32 -0600865 self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature",
866 str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700867
868 def testPackX86RomBadDesc(self):
869 """Test that the Intel requires a descriptor entry"""
870 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600871 self._DoTestFile('030_x86-rom-me-no-desc.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600872 self.assertIn("Node '/binman/intel-me': No offset set with "
873 "offset-unset: should another entry provide this correct "
874 "offset?", str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700875
876 def testPackX86RomMe(self):
877 """Test that an x86 ROM with an ME region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -0600878 data = self._DoReadFile('031_x86-rom-me.dts')
Simon Glassc5ac1382019-07-08 13:18:54 -0600879 expected_desc = tools.ReadFile(self.TestFile('descriptor.bin'))
880 if data[:0x1000] != expected_desc:
881 self.fail('Expected descriptor binary at start of image')
Simon Glasse0ff8552016-11-25 20:15:53 -0700882 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
883
884 def testPackVga(self):
885 """Test that an image with a VGA binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -0600886 data = self._DoReadFile('032_intel-vga.dts')
Simon Glasse0ff8552016-11-25 20:15:53 -0700887 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
888
889 def testPackStart16(self):
890 """Test that an image with an x86 start16 region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -0600891 data = self._DoReadFile('033_x86-start16.dts')
Simon Glasse0ff8552016-11-25 20:15:53 -0700892 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
893
Jagdish Gediya9d368f32018-09-03 21:35:08 +0530894 def testPackPowerpcMpc85xxBootpgResetvec(self):
895 """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be
896 created"""
897 data = self._DoReadFile('81_powerpc_mpc85xx_bootpg_resetvec.dts')
898 self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)])
899
Simon Glass736bb0a2018-07-06 10:27:17 -0600900 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glassadc57012018-07-06 10:27:16 -0600901 """Handle running a test for insertion of microcode
902
903 Args:
904 dts_fname: Name of test .dts file
905 nodtb_data: Data that we expect in the first section
Simon Glass736bb0a2018-07-06 10:27:17 -0600906 ucode_second: True if the microsecond entry is second instead of
907 third
Simon Glassadc57012018-07-06 10:27:16 -0600908
909 Returns:
910 Tuple:
911 Contents of first region (U-Boot or SPL)
Simon Glass3ab95982018-08-01 15:22:37 -0600912 Offset and size components of microcode pointer, as inserted
Simon Glassadc57012018-07-06 10:27:16 -0600913 in the above (two 4-byte words)
914 """
Simon Glass6b187df2017-11-12 21:52:27 -0700915 data = self._DoReadFile(dts_fname, True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700916
917 # Now check the device tree has no microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600918 if ucode_second:
919 ucode_content = data[len(nodtb_data):]
920 ucode_pos = len(nodtb_data)
921 dtb_with_ucode = ucode_content[16:]
922 fdt_len = self.GetFdtLen(dtb_with_ucode)
923 else:
924 dtb_with_ucode = data[len(nodtb_data):]
925 fdt_len = self.GetFdtLen(dtb_with_ucode)
926 ucode_content = dtb_with_ucode[fdt_len:]
927 ucode_pos = len(nodtb_data) + fdt_len
Simon Glasse0ff8552016-11-25 20:15:53 -0700928 fname = tools.GetOutputFilename('test.dtb')
929 with open(fname, 'wb') as fd:
Simon Glassadc57012018-07-06 10:27:16 -0600930 fd.write(dtb_with_ucode)
Simon Glassec3f3782017-05-27 07:38:29 -0600931 dtb = fdt.FdtScan(fname)
932 ucode = dtb.GetNode('/microcode')
Simon Glasse0ff8552016-11-25 20:15:53 -0700933 self.assertTrue(ucode)
934 for node in ucode.subnodes:
935 self.assertFalse(node.props.get('data'))
936
Simon Glasse0ff8552016-11-25 20:15:53 -0700937 # Check that the microcode appears immediately after the Fdt
938 # This matches the concatenation of the data properties in
Simon Glass87722132017-11-12 21:52:26 -0700939 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glasse0ff8552016-11-25 20:15:53 -0700940 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
941 0x78235609)
Simon Glassadc57012018-07-06 10:27:16 -0600942 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glasse0ff8552016-11-25 20:15:53 -0700943
944 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600945 # expected offset and size
Simon Glasse0ff8552016-11-25 20:15:53 -0700946 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
947 len(ucode_data))
Simon Glass736bb0a2018-07-06 10:27:17 -0600948 u_boot = data[:len(nodtb_data)]
949 return u_boot, pos_and_size
Simon Glass6b187df2017-11-12 21:52:27 -0700950
951 def testPackUbootMicrocode(self):
952 """Test that x86 microcode can be handled correctly
953
954 We expect to see the following in the image, in order:
955 u-boot-nodtb.bin with a microcode pointer inserted at the correct
956 place
957 u-boot.dtb with the microcode removed
958 the microcode
959 """
Simon Glass741f2d62018-10-01 12:22:30 -0600960 first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts',
Simon Glass6b187df2017-11-12 21:52:27 -0700961 U_BOOT_NODTB_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -0600962 self.assertEqual(b'nodtb with microcode' + pos_and_size +
963 b' somewhere in here', first)
Simon Glasse0ff8552016-11-25 20:15:53 -0700964
Simon Glass160a7662017-05-27 07:38:26 -0600965 def _RunPackUbootSingleMicrocode(self):
Simon Glasse0ff8552016-11-25 20:15:53 -0700966 """Test that x86 microcode can be handled correctly
967
968 We expect to see the following in the image, in order:
969 u-boot-nodtb.bin with a microcode pointer inserted at the correct
970 place
971 u-boot.dtb with the microcode
972 an empty microcode region
973 """
974 # We need the libfdt library to run this test since only that allows
975 # finding the offset of a property. This is required by
976 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glass741f2d62018-10-01 12:22:30 -0600977 data = self._DoReadFile('035_x86_single_ucode.dts', True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700978
979 second = data[len(U_BOOT_NODTB_DATA):]
980
981 fdt_len = self.GetFdtLen(second)
982 third = second[fdt_len:]
983 second = second[:fdt_len]
984
Simon Glass160a7662017-05-27 07:38:26 -0600985 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
986 self.assertIn(ucode_data, second)
987 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700988
Simon Glass160a7662017-05-27 07:38:26 -0600989 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -0600990 # expected offset and size
Simon Glass160a7662017-05-27 07:38:26 -0600991 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
992 len(ucode_data))
993 first = data[:len(U_BOOT_NODTB_DATA)]
Simon Glassc6c10e72019-05-17 22:00:46 -0600994 self.assertEqual(b'nodtb with microcode' + pos_and_size +
995 b' somewhere in here', first)
Simon Glassc49deb82016-11-25 20:15:54 -0700996
Simon Glass75db0862016-11-25 20:15:55 -0700997 def testPackUbootSingleMicrocode(self):
998 """Test that x86 microcode can be handled correctly with fdt_normal.
999 """
Simon Glass160a7662017-05-27 07:38:26 -06001000 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -07001001
Simon Glassc49deb82016-11-25 20:15:54 -07001002 def testUBootImg(self):
1003 """Test that u-boot.img can be put in a file"""
Simon Glass741f2d62018-10-01 12:22:30 -06001004 data = self._DoReadFile('036_u_boot_img.dts')
Simon Glassc49deb82016-11-25 20:15:54 -07001005 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glass75db0862016-11-25 20:15:55 -07001006
1007 def testNoMicrocode(self):
1008 """Test that a missing microcode region is detected"""
1009 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001010 self._DoReadFile('037_x86_no_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001011 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
1012 "node found in ", str(e.exception))
1013
1014 def testMicrocodeWithoutNode(self):
1015 """Test that a missing u-boot-dtb-with-ucode node is detected"""
1016 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001017 self._DoReadFile('038_x86_ucode_missing_node.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001018 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
1019 "microcode region u-boot-dtb-with-ucode", str(e.exception))
1020
1021 def testMicrocodeWithoutNode2(self):
1022 """Test that a missing u-boot-ucode node is detected"""
1023 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001024 self._DoReadFile('039_x86_ucode_missing_node2.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001025 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
1026 "microcode region u-boot-ucode", str(e.exception))
1027
1028 def testMicrocodeWithoutPtrInElf(self):
1029 """Test that a U-Boot binary without the microcode symbol is detected"""
1030 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glass75db0862016-11-25 20:15:55 -07001031 try:
Simon Glass1d0ebf72019-05-14 15:53:42 -06001032 with open(self.TestFile('u_boot_no_ucode_ptr'), 'rb') as fd:
Simon Glass75db0862016-11-25 20:15:55 -07001033 TestFunctional._MakeInputFile('u-boot', fd.read())
1034
1035 with self.assertRaises(ValueError) as e:
Simon Glass160a7662017-05-27 07:38:26 -06001036 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -07001037 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
1038 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
1039
1040 finally:
1041 # Put the original file back
Simon Glass1d0ebf72019-05-14 15:53:42 -06001042 with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd:
Simon Glass75db0862016-11-25 20:15:55 -07001043 TestFunctional._MakeInputFile('u-boot', fd.read())
1044
1045 def testMicrocodeNotInImage(self):
1046 """Test that microcode must be placed within the image"""
1047 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001048 self._DoReadFile('040_x86_ucode_not_in_image.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001049 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
1050 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glass25ac0e62018-06-01 09:38:14 -06001051 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glass75db0862016-11-25 20:15:55 -07001052
1053 def testWithoutMicrocode(self):
1054 """Test that we can cope with an image without microcode (e.g. qemu)"""
Simon Glass1d0ebf72019-05-14 15:53:42 -06001055 with open(self.TestFile('u_boot_no_ucode_ptr'), 'rb') as fd:
Simon Glass75db0862016-11-25 20:15:55 -07001056 TestFunctional._MakeInputFile('u-boot', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001057 data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001058
1059 # Now check the device tree has no microcode
1060 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
1061 second = data[len(U_BOOT_NODTB_DATA):]
1062
1063 fdt_len = self.GetFdtLen(second)
1064 self.assertEqual(dtb, second[:fdt_len])
1065
1066 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
1067 third = data[used_len:]
Simon Glasse6d85ff2019-05-14 15:53:47 -06001068 self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third)
Simon Glass75db0862016-11-25 20:15:55 -07001069
1070 def testUnknownPosSize(self):
1071 """Test that microcode must be placed within the image"""
1072 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001073 self._DoReadFile('041_unknown_pos_size.dts', True)
Simon Glass3ab95982018-08-01 15:22:37 -06001074 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glass75db0862016-11-25 20:15:55 -07001075 "entry 'invalid-entry'", str(e.exception))
Simon Glassda229092016-11-25 20:15:56 -07001076
1077 def testPackFsp(self):
1078 """Test that an image with a FSP binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001079 data = self._DoReadFile('042_intel-fsp.dts')
Simon Glassda229092016-11-25 20:15:56 -07001080 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
1081
1082 def testPackCmc(self):
Bin Meng59ea8c22017-08-15 22:41:54 -07001083 """Test that an image with a CMC binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001084 data = self._DoReadFile('043_intel-cmc.dts')
Simon Glassda229092016-11-25 20:15:56 -07001085 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Meng59ea8c22017-08-15 22:41:54 -07001086
1087 def testPackVbt(self):
1088 """Test that an image with a VBT binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001089 data = self._DoReadFile('046_intel-vbt.dts')
Bin Meng59ea8c22017-08-15 22:41:54 -07001090 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glass9fc60b42017-11-12 21:52:22 -07001091
Simon Glass56509842017-11-12 21:52:25 -07001092 def testSplBssPad(self):
1093 """Test that we can pad SPL's BSS with zeros"""
Simon Glass6b187df2017-11-12 21:52:27 -07001094 # ELF file with a '__bss_size' symbol
Simon Glass11ae93e2018-10-01 21:12:47 -06001095 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -06001096 data = self._DoReadFile('047_spl_bss_pad.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -06001097 self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA,
1098 data)
Simon Glass56509842017-11-12 21:52:25 -07001099
Simon Glass86af5112018-10-01 21:12:42 -06001100 def testSplBssPadMissing(self):
1101 """Test that a missing symbol is detected"""
Simon Glass11ae93e2018-10-01 21:12:47 -06001102 self._SetupSplElf('u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -07001103 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001104 self._DoReadFile('047_spl_bss_pad.dts')
Simon Glassb50e5612017-11-13 18:54:54 -07001105 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
1106 str(e.exception))
1107
Simon Glass87722132017-11-12 21:52:26 -07001108 def testPackStart16Spl(self):
Simon Glass35b384c2018-09-14 04:57:10 -06001109 """Test that an image with an x86 start16 SPL region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001110 data = self._DoReadFile('048_x86-start16-spl.dts')
Simon Glass87722132017-11-12 21:52:26 -07001111 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
1112
Simon Glass736bb0a2018-07-06 10:27:17 -06001113 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
1114 """Helper function for microcode tests
Simon Glass6b187df2017-11-12 21:52:27 -07001115
1116 We expect to see the following in the image, in order:
1117 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
1118 correct place
1119 u-boot.dtb with the microcode removed
1120 the microcode
Simon Glass736bb0a2018-07-06 10:27:17 -06001121
1122 Args:
1123 dts: Device tree file to use for test
1124 ucode_second: True if the microsecond entry is second instead of
1125 third
Simon Glass6b187df2017-11-12 21:52:27 -07001126 """
Simon Glass11ae93e2018-10-01 21:12:47 -06001127 self._SetupSplElf('u_boot_ucode_ptr')
Simon Glass736bb0a2018-07-06 10:27:17 -06001128 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
1129 ucode_second=ucode_second)
Simon Glassc6c10e72019-05-17 22:00:46 -06001130 self.assertEqual(b'splnodtb with microc' + pos_and_size +
1131 b'ter somewhere in here', first)
Simon Glass6b187df2017-11-12 21:52:27 -07001132
Simon Glass736bb0a2018-07-06 10:27:17 -06001133 def testPackUbootSplMicrocode(self):
1134 """Test that x86 microcode can be handled correctly in SPL"""
Simon Glass741f2d62018-10-01 12:22:30 -06001135 self._PackUbootSplMicrocode('049_x86_ucode_spl.dts')
Simon Glass736bb0a2018-07-06 10:27:17 -06001136
1137 def testPackUbootSplMicrocodeReorder(self):
1138 """Test that order doesn't matter for microcode entries
1139
1140 This is the same as testPackUbootSplMicrocode but when we process the
1141 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
1142 entry, so we reply on binman to try later.
1143 """
Simon Glass741f2d62018-10-01 12:22:30 -06001144 self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts',
Simon Glass736bb0a2018-07-06 10:27:17 -06001145 ucode_second=True)
1146
Simon Glassca4f4ff2017-11-12 21:52:28 -07001147 def testPackMrc(self):
1148 """Test that an image with an MRC binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001149 data = self._DoReadFile('050_intel_mrc.dts')
Simon Glassca4f4ff2017-11-12 21:52:28 -07001150 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
1151
Simon Glass47419ea2017-11-13 18:54:55 -07001152 def testSplDtb(self):
1153 """Test that an image with spl/u-boot-spl.dtb can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001154 data = self._DoReadFile('051_u_boot_spl_dtb.dts')
Simon Glass47419ea2017-11-13 18:54:55 -07001155 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
1156
Simon Glass4e6fdbe2017-11-13 18:54:56 -07001157 def testSplNoDtb(self):
1158 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001159 data = self._DoReadFile('052_u_boot_spl_nodtb.dts')
Simon Glass4e6fdbe2017-11-13 18:54:56 -07001160 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
1161
Simon Glass19790632017-11-13 18:55:01 -07001162 def testSymbols(self):
1163 """Test binman can assign symbols embedded in U-Boot"""
1164 elf_fname = self.TestFile('u_boot_binman_syms')
1165 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
1166 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glass3ab95982018-08-01 15:22:37 -06001167 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass19790632017-11-13 18:55:01 -07001168
Simon Glass11ae93e2018-10-01 21:12:47 -06001169 self._SetupSplElf('u_boot_binman_syms')
Simon Glass741f2d62018-10-01 12:22:30 -06001170 data = self._DoReadFile('053_symbols.dts')
Simon Glass19790632017-11-13 18:55:01 -07001171 sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
Simon Glasse6d85ff2019-05-14 15:53:47 -06001172 expected = (sym_values + U_BOOT_SPL_DATA[16:] +
1173 tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values +
1174 U_BOOT_SPL_DATA[16:])
Simon Glass19790632017-11-13 18:55:01 -07001175 self.assertEqual(expected, data)
1176
Simon Glassdd57c132018-06-01 09:38:11 -06001177 def testPackUnitAddress(self):
1178 """Test that we support multiple binaries with the same name"""
Simon Glass741f2d62018-10-01 12:22:30 -06001179 data = self._DoReadFile('054_unit_address.dts')
Simon Glassdd57c132018-06-01 09:38:11 -06001180 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1181
Simon Glass18546952018-06-01 09:38:16 -06001182 def testSections(self):
1183 """Basic test of sections"""
Simon Glass741f2d62018-10-01 12:22:30 -06001184 data = self._DoReadFile('055_sections.dts')
Simon Glassc6c10e72019-05-17 22:00:46 -06001185 expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
1186 U_BOOT_DATA + tools.GetBytes(ord('a'), 12) +
1187 U_BOOT_DATA + tools.GetBytes(ord('&'), 4))
Simon Glass18546952018-06-01 09:38:16 -06001188 self.assertEqual(expected, data)
Simon Glass9fc60b42017-11-12 21:52:22 -07001189
Simon Glass3b0c3822018-06-01 09:38:20 -06001190 def testMap(self):
1191 """Tests outputting a map of the images"""
Simon Glass741f2d62018-10-01 12:22:30 -06001192 _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001193 self.assertEqual('''ImagePos Offset Size Name
119400000000 00000000 00000028 main-section
119500000000 00000000 00000010 section@0
119600000000 00000000 00000004 u-boot
119700000010 00000010 00000010 section@1
119800000010 00000000 00000004 u-boot
119900000020 00000020 00000004 section@2
120000000020 00000000 00000004 u-boot
Simon Glass3b0c3822018-06-01 09:38:20 -06001201''', map_data)
1202
Simon Glassc8d48ef2018-06-01 09:38:21 -06001203 def testNamePrefix(self):
1204 """Tests that name prefixes are used"""
Simon Glass741f2d62018-10-01 12:22:30 -06001205 _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001206 self.assertEqual('''ImagePos Offset Size Name
120700000000 00000000 00000028 main-section
120800000000 00000000 00000010 section@0
120900000000 00000000 00000004 ro-u-boot
121000000010 00000010 00000010 section@1
121100000010 00000000 00000004 rw-u-boot
Simon Glassc8d48ef2018-06-01 09:38:21 -06001212''', map_data)
1213
Simon Glass736bb0a2018-07-06 10:27:17 -06001214 def testUnknownContents(self):
1215 """Test that obtaining the contents works as expected"""
1216 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001217 self._DoReadFile('057_unknown_contents.dts', True)
Simon Glass736bb0a2018-07-06 10:27:17 -06001218 self.assertIn("Section '/binman': Internal error: Could not complete "
1219 "processing of contents: remaining [<_testing.Entry__testing ",
1220 str(e.exception))
1221
Simon Glass5c890232018-07-06 10:27:19 -06001222 def testBadChangeSize(self):
1223 """Test that trying to change the size of an entry fails"""
Simon Glassc52c9e72019-07-08 14:25:37 -06001224 try:
1225 state.SetAllowEntryExpansion(False)
1226 with self.assertRaises(ValueError) as e:
1227 self._DoReadFile('059_change_size.dts', True)
1228 self.assertIn("Node '/binman/_testing': Cannot update entry size from 1 to 2",
1229 str(e.exception))
1230 finally:
1231 state.SetAllowEntryExpansion(True)
Simon Glass5c890232018-07-06 10:27:19 -06001232
Simon Glass16b8d6b2018-07-06 10:27:42 -06001233 def testUpdateFdt(self):
Simon Glass3ab95982018-08-01 15:22:37 -06001234 """Test that we can update the device tree with offset/size info"""
Simon Glass741f2d62018-10-01 12:22:30 -06001235 _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts',
Simon Glass16b8d6b2018-07-06 10:27:42 -06001236 update_dtb=True)
Simon Glasscee02e62018-07-17 13:25:52 -06001237 dtb = fdt.Fdt(out_dtb_fname)
1238 dtb.Scan()
1239 props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos'])
Simon Glass16b8d6b2018-07-06 10:27:42 -06001240 self.assertEqual({
Simon Glassdbf6be92018-08-01 15:22:42 -06001241 'image-pos': 0,
Simon Glass8122f392018-07-17 13:25:28 -06001242 'offset': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001243 '_testing:offset': 32,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001244 '_testing:size': 1,
Simon Glassdbf6be92018-08-01 15:22:42 -06001245 '_testing:image-pos': 32,
Simon Glass3ab95982018-08-01 15:22:37 -06001246 'section@0/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001247 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001248 'section@0/u-boot:image-pos': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001249 'section@0:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001250 'section@0:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001251 'section@0:image-pos': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001252
Simon Glass3ab95982018-08-01 15:22:37 -06001253 'section@1/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001254 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001255 'section@1/u-boot:image-pos': 16,
Simon Glass3ab95982018-08-01 15:22:37 -06001256 'section@1:offset': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001257 'section@1:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001258 'section@1:image-pos': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001259 'size': 40
1260 }, props)
1261
1262 def testUpdateFdtBad(self):
1263 """Test that we detect when ProcessFdt never completes"""
1264 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001265 self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True)
Simon Glass16b8d6b2018-07-06 10:27:42 -06001266 self.assertIn('Could not complete processing of Fdt: remaining '
1267 '[<_testing.Entry__testing', str(e.exception))
Simon Glass5c890232018-07-06 10:27:19 -06001268
Simon Glass53af22a2018-07-17 13:25:32 -06001269 def testEntryArgs(self):
1270 """Test passing arguments to entries from the command line"""
1271 entry_args = {
1272 'test-str-arg': 'test1',
1273 'test-int-arg': '456',
1274 }
Simon Glass741f2d62018-10-01 12:22:30 -06001275 self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args)
Simon Glass53af22a2018-07-17 13:25:32 -06001276 self.assertIn('image', control.images)
1277 entry = control.images['image'].GetEntries()['_testing']
1278 self.assertEqual('test0', entry.test_str_fdt)
1279 self.assertEqual('test1', entry.test_str_arg)
1280 self.assertEqual(123, entry.test_int_fdt)
1281 self.assertEqual(456, entry.test_int_arg)
1282
1283 def testEntryArgsMissing(self):
1284 """Test missing arguments and properties"""
1285 entry_args = {
1286 'test-int-arg': '456',
1287 }
Simon Glass741f2d62018-10-01 12:22:30 -06001288 self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args)
Simon Glass53af22a2018-07-17 13:25:32 -06001289 entry = control.images['image'].GetEntries()['_testing']
1290 self.assertEqual('test0', entry.test_str_fdt)
1291 self.assertEqual(None, entry.test_str_arg)
1292 self.assertEqual(None, entry.test_int_fdt)
1293 self.assertEqual(456, entry.test_int_arg)
1294
1295 def testEntryArgsRequired(self):
1296 """Test missing arguments and properties"""
1297 entry_args = {
1298 'test-int-arg': '456',
1299 }
1300 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001301 self._DoReadFileDtb('064_entry_args_required.dts')
Simon Glass53af22a2018-07-17 13:25:32 -06001302 self.assertIn("Node '/binman/_testing': Missing required "
1303 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1304 str(e.exception))
1305
1306 def testEntryArgsInvalidFormat(self):
1307 """Test that an invalid entry-argument format is detected"""
Simon Glass53cd5d92019-07-08 14:25:29 -06001308 args = ['build', '-d', self.TestFile('064_entry_args_required.dts'),
1309 '-ano-value']
Simon Glass53af22a2018-07-17 13:25:32 -06001310 with self.assertRaises(ValueError) as e:
1311 self._DoBinman(*args)
1312 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1313
1314 def testEntryArgsInvalidInteger(self):
1315 """Test that an invalid entry-argument integer is detected"""
1316 entry_args = {
1317 'test-int-arg': 'abc',
1318 }
1319 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001320 self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args)
Simon Glass53af22a2018-07-17 13:25:32 -06001321 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1322 "'test-int-arg' (value 'abc') to integer",
1323 str(e.exception))
1324
1325 def testEntryArgsInvalidDatatype(self):
1326 """Test that an invalid entry-argument datatype is detected
1327
1328 This test could be written in entry_test.py except that it needs
1329 access to control.entry_args, which seems more than that module should
1330 be able to see.
1331 """
1332 entry_args = {
1333 'test-bad-datatype-arg': '12',
1334 }
1335 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001336 self._DoReadFileDtb('065_entry_args_unknown_datatype.dts',
Simon Glass53af22a2018-07-17 13:25:32 -06001337 entry_args=entry_args)
1338 self.assertIn('GetArg() internal error: Unknown data type ',
1339 str(e.exception))
1340
Simon Glassbb748372018-07-17 13:25:33 -06001341 def testText(self):
1342 """Test for a text entry type"""
1343 entry_args = {
1344 'test-id': TEXT_DATA,
1345 'test-id2': TEXT_DATA2,
1346 'test-id3': TEXT_DATA3,
1347 }
Simon Glass741f2d62018-10-01 12:22:30 -06001348 data, _, _, _ = self._DoReadFileDtb('066_text.dts',
Simon Glassbb748372018-07-17 13:25:33 -06001349 entry_args=entry_args)
Simon Glassc6c10e72019-05-17 22:00:46 -06001350 expected = (tools.ToBytes(TEXT_DATA) +
1351 tools.GetBytes(0, 8 - len(TEXT_DATA)) +
1352 tools.ToBytes(TEXT_DATA2) + tools.ToBytes(TEXT_DATA3) +
Simon Glassaa88b502019-07-08 13:18:40 -06001353 b'some text' + b'more text')
Simon Glassbb748372018-07-17 13:25:33 -06001354 self.assertEqual(expected, data)
1355
Simon Glassfd8d1f72018-07-17 13:25:36 -06001356 def testEntryDocs(self):
1357 """Test for creation of entry documentation"""
1358 with test_util.capture_sys_output() as (stdout, stderr):
1359 control.WriteEntryDocs(binman.GetEntryModules())
1360 self.assertTrue(len(stdout.getvalue()) > 0)
1361
1362 def testEntryDocsMissing(self):
1363 """Test handling of missing entry documentation"""
1364 with self.assertRaises(ValueError) as e:
1365 with test_util.capture_sys_output() as (stdout, stderr):
1366 control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
1367 self.assertIn('Documentation is missing for modules: u_boot',
1368 str(e.exception))
1369
Simon Glass11e36cc2018-07-17 13:25:38 -06001370 def testFmap(self):
1371 """Basic test of generation of a flashrom fmap"""
Simon Glass741f2d62018-10-01 12:22:30 -06001372 data = self._DoReadFile('067_fmap.dts')
Simon Glass11e36cc2018-07-17 13:25:38 -06001373 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
Simon Glassc6c10e72019-05-17 22:00:46 -06001374 expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
1375 U_BOOT_DATA + tools.GetBytes(ord('a'), 12))
Simon Glass11e36cc2018-07-17 13:25:38 -06001376 self.assertEqual(expected, data[:32])
Simon Glassc6c10e72019-05-17 22:00:46 -06001377 self.assertEqual(b'__FMAP__', fhdr.signature)
Simon Glass11e36cc2018-07-17 13:25:38 -06001378 self.assertEqual(1, fhdr.ver_major)
1379 self.assertEqual(0, fhdr.ver_minor)
1380 self.assertEqual(0, fhdr.base)
1381 self.assertEqual(16 + 16 +
1382 fmap_util.FMAP_HEADER_LEN +
1383 fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001384 self.assertEqual(b'FMAP', fhdr.name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001385 self.assertEqual(3, fhdr.nareas)
1386 for fentry in fentries:
1387 self.assertEqual(0, fentry.flags)
1388
1389 self.assertEqual(0, fentries[0].offset)
1390 self.assertEqual(4, fentries[0].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001391 self.assertEqual(b'RO_U_BOOT', fentries[0].name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001392
1393 self.assertEqual(16, fentries[1].offset)
1394 self.assertEqual(4, fentries[1].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001395 self.assertEqual(b'RW_U_BOOT', fentries[1].name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001396
1397 self.assertEqual(32, fentries[2].offset)
1398 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1399 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001400 self.assertEqual(b'FMAP', fentries[2].name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001401
Simon Glassec127af2018-07-17 13:25:39 -06001402 def testBlobNamedByArg(self):
1403 """Test we can add a blob with the filename coming from an entry arg"""
1404 entry_args = {
1405 'cros-ec-rw-path': 'ecrw.bin',
1406 }
Simon Glass741f2d62018-10-01 12:22:30 -06001407 data, _, _, _ = self._DoReadFileDtb('068_blob_named_by_arg.dts',
Simon Glassec127af2018-07-17 13:25:39 -06001408 entry_args=entry_args)
1409
Simon Glass3af8e492018-07-17 13:25:40 -06001410 def testFill(self):
1411 """Test for an fill entry type"""
Simon Glass741f2d62018-10-01 12:22:30 -06001412 data = self._DoReadFile('069_fill.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -06001413 expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8)
Simon Glass3af8e492018-07-17 13:25:40 -06001414 self.assertEqual(expected, data)
1415
1416 def testFillNoSize(self):
1417 """Test for an fill entry type with no size"""
1418 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001419 self._DoReadFile('070_fill_no_size.dts')
Simon Glass3af8e492018-07-17 13:25:40 -06001420 self.assertIn("'fill' entry must have a size property",
1421 str(e.exception))
1422
Simon Glass0ef87aa2018-07-17 13:25:44 -06001423 def _HandleGbbCommand(self, pipe_list):
1424 """Fake calls to the futility utility"""
1425 if pipe_list[0][0] == 'futility':
1426 fname = pipe_list[0][-1]
1427 # Append our GBB data to the file, which will happen every time the
1428 # futility command is called.
Simon Glass1d0ebf72019-05-14 15:53:42 -06001429 with open(fname, 'ab') as fd:
Simon Glass0ef87aa2018-07-17 13:25:44 -06001430 fd.write(GBB_DATA)
1431 return command.CommandResult()
1432
1433 def testGbb(self):
1434 """Test for the Chromium OS Google Binary Block"""
1435 command.test_result = self._HandleGbbCommand
1436 entry_args = {
1437 'keydir': 'devkeys',
1438 'bmpblk': 'bmpblk.bin',
1439 }
Simon Glass741f2d62018-10-01 12:22:30 -06001440 data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args)
Simon Glass0ef87aa2018-07-17 13:25:44 -06001441
1442 # Since futility
Simon Glasse6d85ff2019-05-14 15:53:47 -06001443 expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) +
1444 tools.GetBytes(0, 0x2180 - 16))
Simon Glass0ef87aa2018-07-17 13:25:44 -06001445 self.assertEqual(expected, data)
1446
1447 def testGbbTooSmall(self):
1448 """Test for the Chromium OS Google Binary Block being large enough"""
1449 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001450 self._DoReadFileDtb('072_gbb_too_small.dts')
Simon Glass0ef87aa2018-07-17 13:25:44 -06001451 self.assertIn("Node '/binman/gbb': GBB is too small",
1452 str(e.exception))
1453
1454 def testGbbNoSize(self):
1455 """Test for the Chromium OS Google Binary Block having a size"""
1456 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001457 self._DoReadFileDtb('073_gbb_no_size.dts')
Simon Glass0ef87aa2018-07-17 13:25:44 -06001458 self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
1459 str(e.exception))
1460
Simon Glass24d0d3c2018-07-17 13:25:47 -06001461 def _HandleVblockCommand(self, pipe_list):
1462 """Fake calls to the futility utility"""
1463 if pipe_list[0][0] == 'futility':
1464 fname = pipe_list[0][3]
Simon Glassa326b492018-09-14 04:57:11 -06001465 with open(fname, 'wb') as fd:
Simon Glass24d0d3c2018-07-17 13:25:47 -06001466 fd.write(VBLOCK_DATA)
1467 return command.CommandResult()
1468
1469 def testVblock(self):
1470 """Test for the Chromium OS Verified Boot Block"""
1471 command.test_result = self._HandleVblockCommand
1472 entry_args = {
1473 'keydir': 'devkeys',
1474 }
Simon Glass741f2d62018-10-01 12:22:30 -06001475 data, _, _, _ = self._DoReadFileDtb('074_vblock.dts',
Simon Glass24d0d3c2018-07-17 13:25:47 -06001476 entry_args=entry_args)
1477 expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA
1478 self.assertEqual(expected, data)
1479
1480 def testVblockNoContent(self):
1481 """Test we detect a vblock which has no content to sign"""
1482 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001483 self._DoReadFile('075_vblock_no_content.dts')
Simon Glass24d0d3c2018-07-17 13:25:47 -06001484 self.assertIn("Node '/binman/vblock': Vblock must have a 'content' "
1485 'property', str(e.exception))
1486
1487 def testVblockBadPhandle(self):
1488 """Test that we detect a vblock with an invalid phandle in contents"""
1489 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001490 self._DoReadFile('076_vblock_bad_phandle.dts')
Simon Glass24d0d3c2018-07-17 13:25:47 -06001491 self.assertIn("Node '/binman/vblock': Cannot find node for phandle "
1492 '1000', str(e.exception))
1493
1494 def testVblockBadEntry(self):
1495 """Test that we detect an entry that points to a non-entry"""
1496 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001497 self._DoReadFile('077_vblock_bad_entry.dts')
Simon Glass24d0d3c2018-07-17 13:25:47 -06001498 self.assertIn("Node '/binman/vblock': Cannot find entry for node "
1499 "'other'", str(e.exception))
1500
Simon Glassb8ef5b62018-07-17 13:25:48 -06001501 def testTpl(self):
1502 """Test that an image with TPL and ots device tree can be created"""
1503 # ELF file with a '__bss_size' symbol
Simon Glass1d0ebf72019-05-14 15:53:42 -06001504 with open(self.TestFile('bss_data'), 'rb') as fd:
Simon Glassb8ef5b62018-07-17 13:25:48 -06001505 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001506 data = self._DoReadFile('078_u_boot_tpl.dts')
Simon Glassb8ef5b62018-07-17 13:25:48 -06001507 self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
1508
Simon Glass15a587c2018-07-17 13:25:51 -06001509 def testUsesPos(self):
1510 """Test that the 'pos' property cannot be used anymore"""
1511 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001512 data = self._DoReadFile('079_uses_pos.dts')
Simon Glass15a587c2018-07-17 13:25:51 -06001513 self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of "
1514 "'pos'", str(e.exception))
1515
Simon Glassd178eab2018-09-14 04:57:08 -06001516 def testFillZero(self):
1517 """Test for an fill entry type with a size of 0"""
Simon Glass741f2d62018-10-01 12:22:30 -06001518 data = self._DoReadFile('080_fill_empty.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -06001519 self.assertEqual(tools.GetBytes(0, 16), data)
Simon Glassd178eab2018-09-14 04:57:08 -06001520
Simon Glass0b489362018-09-14 04:57:09 -06001521 def testTextMissing(self):
1522 """Test for a text entry type where there is no text"""
1523 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001524 self._DoReadFileDtb('066_text.dts',)
Simon Glass0b489362018-09-14 04:57:09 -06001525 self.assertIn("Node '/binman/text': No value provided for text label "
1526 "'test-id'", str(e.exception))
1527
Simon Glass35b384c2018-09-14 04:57:10 -06001528 def testPackStart16Tpl(self):
1529 """Test that an image with an x86 start16 TPL region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001530 data = self._DoReadFile('081_x86-start16-tpl.dts')
Simon Glass35b384c2018-09-14 04:57:10 -06001531 self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)])
1532
Simon Glass0bfa7b02018-09-14 04:57:12 -06001533 def testSelectImage(self):
1534 """Test that we can select which images to build"""
Simon Glasseb833d82019-04-25 21:58:34 -06001535 expected = 'Skipping images: image1'
Simon Glass0bfa7b02018-09-14 04:57:12 -06001536
Simon Glasseb833d82019-04-25 21:58:34 -06001537 # We should only get the expected message in verbose mode
Simon Glassee0c9a72019-07-08 13:18:48 -06001538 for verbosity in (0, 2):
Simon Glasseb833d82019-04-25 21:58:34 -06001539 with test_util.capture_sys_output() as (stdout, stderr):
1540 retcode = self._DoTestFile('006_dual_image.dts',
1541 verbosity=verbosity,
1542 images=['image2'])
1543 self.assertEqual(0, retcode)
1544 if verbosity:
1545 self.assertIn(expected, stdout.getvalue())
1546 else:
1547 self.assertNotIn(expected, stdout.getvalue())
1548
1549 self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin')))
1550 self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin')))
Simon Glass0bfa7b02018-09-14 04:57:12 -06001551
Simon Glass6ed45ba2018-09-14 04:57:24 -06001552 def testUpdateFdtAll(self):
1553 """Test that all device trees are updated with offset/size info"""
Simon Glass3c081312019-07-08 14:25:26 -06001554 data = self._DoReadFileRealDtb('082_fdt_update_all.dts')
Simon Glass6ed45ba2018-09-14 04:57:24 -06001555
1556 base_expected = {
1557 'section:image-pos': 0,
1558 'u-boot-tpl-dtb:size': 513,
1559 'u-boot-spl-dtb:size': 513,
1560 'u-boot-spl-dtb:offset': 493,
1561 'image-pos': 0,
1562 'section/u-boot-dtb:image-pos': 0,
1563 'u-boot-spl-dtb:image-pos': 493,
1564 'section/u-boot-dtb:size': 493,
1565 'u-boot-tpl-dtb:image-pos': 1006,
1566 'section/u-boot-dtb:offset': 0,
1567 'section:size': 493,
1568 'offset': 0,
1569 'section:offset': 0,
1570 'u-boot-tpl-dtb:offset': 1006,
1571 'size': 1519
1572 }
1573
1574 # We expect three device-tree files in the output, one after the other.
1575 # Read them in sequence. We look for an 'spl' property in the SPL tree,
1576 # and 'tpl' in the TPL tree, to make sure they are distinct from the
1577 # main U-Boot tree. All three should have the same postions and offset.
1578 start = 0
1579 for item in ['', 'spl', 'tpl']:
1580 dtb = fdt.Fdt.FromData(data[start:])
1581 dtb.Scan()
1582 props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos',
1583 'spl', 'tpl'])
1584 expected = dict(base_expected)
1585 if item:
1586 expected[item] = 0
1587 self.assertEqual(expected, props)
1588 start += dtb._fdt_obj.totalsize()
1589
1590 def testUpdateFdtOutput(self):
1591 """Test that output DTB files are updated"""
1592 try:
Simon Glass741f2d62018-10-01 12:22:30 -06001593 data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts',
Simon Glass6ed45ba2018-09-14 04:57:24 -06001594 use_real_dtb=True, update_dtb=True, reset_dtbs=False)
1595
1596 # Unfortunately, compiling a source file always results in a file
1597 # called source.dtb (see fdt_util.EnsureCompiled()). The test
Simon Glass741f2d62018-10-01 12:22:30 -06001598 # source file (e.g. test/075_fdt_update_all.dts) thus does not enter
Simon Glass6ed45ba2018-09-14 04:57:24 -06001599 # binman as a file called u-boot.dtb. To fix this, copy the file
1600 # over to the expected place.
1601 #tools.WriteFile(os.path.join(self._indir, 'u-boot.dtb'),
1602 #tools.ReadFile(tools.GetOutputFilename('source.dtb')))
1603 start = 0
1604 for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out',
1605 'tpl/u-boot-tpl.dtb.out']:
1606 dtb = fdt.Fdt.FromData(data[start:])
1607 size = dtb._fdt_obj.totalsize()
1608 pathname = tools.GetOutputFilename(os.path.split(fname)[1])
1609 outdata = tools.ReadFile(pathname)
1610 name = os.path.split(fname)[0]
1611
1612 if name:
1613 orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name)
1614 else:
1615 orig_indata = dtb_data
1616 self.assertNotEqual(outdata, orig_indata,
1617 "Expected output file '%s' be updated" % pathname)
1618 self.assertEqual(outdata, data[start:start + size],
1619 "Expected output file '%s' to match output image" %
1620 pathname)
1621 start += size
1622 finally:
1623 self._ResetDtbs()
1624
Simon Glass83d73c22018-09-14 04:57:26 -06001625 def _decompress(self, data):
Simon Glassff5c7e32019-07-08 13:18:42 -06001626 return tools.Decompress(data, 'lz4')
Simon Glass83d73c22018-09-14 04:57:26 -06001627
1628 def testCompress(self):
1629 """Test compression of blobs"""
Simon Glassac62fba2019-07-08 13:18:53 -06001630 self._CheckLz4()
Simon Glass741f2d62018-10-01 12:22:30 -06001631 data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts',
Simon Glass83d73c22018-09-14 04:57:26 -06001632 use_real_dtb=True, update_dtb=True)
1633 dtb = fdt.Fdt(out_dtb_fname)
1634 dtb.Scan()
1635 props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
1636 orig = self._decompress(data)
1637 self.assertEquals(COMPRESS_DATA, orig)
1638 expected = {
1639 'blob:uncomp-size': len(COMPRESS_DATA),
1640 'blob:size': len(data),
1641 'size': len(data),
1642 }
1643 self.assertEqual(expected, props)
1644
Simon Glass0a98b282018-09-14 04:57:28 -06001645 def testFiles(self):
1646 """Test bringing in multiple files"""
Simon Glass741f2d62018-10-01 12:22:30 -06001647 data = self._DoReadFile('084_files.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001648 self.assertEqual(FILES_DATA, data)
1649
1650 def testFilesCompress(self):
1651 """Test bringing in multiple files and compressing them"""
Simon Glassac62fba2019-07-08 13:18:53 -06001652 self._CheckLz4()
Simon Glass741f2d62018-10-01 12:22:30 -06001653 data = self._DoReadFile('085_files_compress.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001654
1655 image = control.images['image']
1656 entries = image.GetEntries()
1657 files = entries['files']
1658 entries = files._section._entries
1659
Simon Glassc6c10e72019-05-17 22:00:46 -06001660 orig = b''
Simon Glass0a98b282018-09-14 04:57:28 -06001661 for i in range(1, 3):
1662 key = '%d.dat' % i
1663 start = entries[key].image_pos
1664 len = entries[key].size
1665 chunk = data[start:start + len]
1666 orig += self._decompress(chunk)
1667
1668 self.assertEqual(FILES_DATA, orig)
1669
1670 def testFilesMissing(self):
1671 """Test missing files"""
1672 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001673 data = self._DoReadFile('086_files_none.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001674 self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched "
1675 'no files', str(e.exception))
1676
1677 def testFilesNoPattern(self):
1678 """Test missing files"""
1679 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001680 data = self._DoReadFile('087_files_no_pattern.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001681 self.assertIn("Node '/binman/files': Missing 'pattern' property",
1682 str(e.exception))
1683
Simon Glassba64a0b2018-09-14 04:57:29 -06001684 def testExpandSize(self):
1685 """Test an expanding entry"""
Simon Glass741f2d62018-10-01 12:22:30 -06001686 data, _, map_data, _ = self._DoReadFileDtb('088_expand_size.dts',
Simon Glassba64a0b2018-09-14 04:57:29 -06001687 map=True)
Simon Glassc6c10e72019-05-17 22:00:46 -06001688 expect = (tools.GetBytes(ord('a'), 8) + U_BOOT_DATA +
1689 MRC_DATA + tools.GetBytes(ord('b'), 1) + U_BOOT_DATA +
1690 tools.GetBytes(ord('c'), 8) + U_BOOT_DATA +
1691 tools.GetBytes(ord('d'), 8))
Simon Glassba64a0b2018-09-14 04:57:29 -06001692 self.assertEqual(expect, data)
1693 self.assertEqual('''ImagePos Offset Size Name
169400000000 00000000 00000028 main-section
169500000000 00000000 00000008 fill
169600000008 00000008 00000004 u-boot
16970000000c 0000000c 00000004 section
16980000000c 00000000 00000003 intel-mrc
169900000010 00000010 00000004 u-boot2
170000000014 00000014 0000000c section2
170100000014 00000000 00000008 fill
17020000001c 00000008 00000004 u-boot
170300000020 00000020 00000008 fill2
1704''', map_data)
1705
1706 def testExpandSizeBad(self):
1707 """Test an expanding entry which fails to provide contents"""
Simon Glass163ed6c2018-09-14 04:57:36 -06001708 with test_util.capture_sys_output() as (stdout, stderr):
1709 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001710 self._DoReadFileDtb('089_expand_size_bad.dts', map=True)
Simon Glassba64a0b2018-09-14 04:57:29 -06001711 self.assertIn("Node '/binman/_testing': Cannot obtain contents when "
1712 'expanding entry', str(e.exception))
1713
Simon Glasse0e5df92018-09-14 04:57:31 -06001714 def testHash(self):
1715 """Test hashing of the contents of an entry"""
Simon Glass741f2d62018-10-01 12:22:30 -06001716 _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts',
Simon Glasse0e5df92018-09-14 04:57:31 -06001717 use_real_dtb=True, update_dtb=True)
1718 dtb = fdt.Fdt(out_dtb_fname)
1719 dtb.Scan()
1720 hash_node = dtb.GetNode('/binman/u-boot/hash').props['value']
1721 m = hashlib.sha256()
1722 m.update(U_BOOT_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -06001723 self.assertEqual(m.digest(), b''.join(hash_node.value))
Simon Glasse0e5df92018-09-14 04:57:31 -06001724
1725 def testHashNoAlgo(self):
1726 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001727 self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True)
Simon Glasse0e5df92018-09-14 04:57:31 -06001728 self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for "
1729 'hash node', str(e.exception))
1730
1731 def testHashBadAlgo(self):
1732 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001733 self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True)
Simon Glasse0e5df92018-09-14 04:57:31 -06001734 self.assertIn("Node '/binman/u-boot': Unknown hash algorithm",
1735 str(e.exception))
1736
1737 def testHashSection(self):
1738 """Test hashing of the contents of an entry"""
Simon Glass741f2d62018-10-01 12:22:30 -06001739 _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts',
Simon Glasse0e5df92018-09-14 04:57:31 -06001740 use_real_dtb=True, update_dtb=True)
1741 dtb = fdt.Fdt(out_dtb_fname)
1742 dtb.Scan()
1743 hash_node = dtb.GetNode('/binman/section/hash').props['value']
1744 m = hashlib.sha256()
1745 m.update(U_BOOT_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -06001746 m.update(tools.GetBytes(ord('a'), 16))
1747 self.assertEqual(m.digest(), b''.join(hash_node.value))
Simon Glasse0e5df92018-09-14 04:57:31 -06001748
Simon Glassf0253632018-09-14 04:57:32 -06001749 def testPackUBootTplMicrocode(self):
1750 """Test that x86 microcode can be handled correctly in TPL
1751
1752 We expect to see the following in the image, in order:
1753 u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct
1754 place
1755 u-boot-tpl.dtb with the microcode removed
1756 the microcode
1757 """
Simon Glass1d0ebf72019-05-14 15:53:42 -06001758 with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd:
Simon Glassf0253632018-09-14 04:57:32 -06001759 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001760 first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts',
Simon Glassf0253632018-09-14 04:57:32 -06001761 U_BOOT_TPL_NODTB_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -06001762 self.assertEqual(b'tplnodtb with microc' + pos_and_size +
1763 b'ter somewhere in here', first)
Simon Glassf0253632018-09-14 04:57:32 -06001764
Simon Glassf8f8df62018-09-14 04:57:34 -06001765 def testFmapX86(self):
1766 """Basic test of generation of a flashrom fmap"""
Simon Glass741f2d62018-10-01 12:22:30 -06001767 data = self._DoReadFile('094_fmap_x86.dts')
Simon Glassf8f8df62018-09-14 04:57:34 -06001768 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
Simon Glassc6c10e72019-05-17 22:00:46 -06001769 expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('a'), 32 - 7)
Simon Glassf8f8df62018-09-14 04:57:34 -06001770 self.assertEqual(expected, data[:32])
1771 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
1772
1773 self.assertEqual(0x100, fhdr.image_size)
1774
1775 self.assertEqual(0, fentries[0].offset)
1776 self.assertEqual(4, fentries[0].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001777 self.assertEqual(b'U_BOOT', fentries[0].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001778
1779 self.assertEqual(4, fentries[1].offset)
1780 self.assertEqual(3, fentries[1].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001781 self.assertEqual(b'INTEL_MRC', fentries[1].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001782
1783 self.assertEqual(32, fentries[2].offset)
1784 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1785 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001786 self.assertEqual(b'FMAP', fentries[2].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001787
1788 def testFmapX86Section(self):
1789 """Basic test of generation of a flashrom fmap"""
Simon Glass741f2d62018-10-01 12:22:30 -06001790 data = self._DoReadFile('095_fmap_x86_section.dts')
Simon Glassc6c10e72019-05-17 22:00:46 -06001791 expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('b'), 32 - 7)
Simon Glassf8f8df62018-09-14 04:57:34 -06001792 self.assertEqual(expected, data[:32])
1793 fhdr, fentries = fmap_util.DecodeFmap(data[36:])
1794
1795 self.assertEqual(0x100, fhdr.image_size)
1796
1797 self.assertEqual(0, fentries[0].offset)
1798 self.assertEqual(4, fentries[0].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001799 self.assertEqual(b'U_BOOT', fentries[0].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001800
1801 self.assertEqual(4, fentries[1].offset)
1802 self.assertEqual(3, fentries[1].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001803 self.assertEqual(b'INTEL_MRC', fentries[1].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001804
1805 self.assertEqual(36, fentries[2].offset)
1806 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1807 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001808 self.assertEqual(b'FMAP', fentries[2].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001809
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001810 def testElf(self):
1811 """Basic test of ELF entries"""
Simon Glass11ae93e2018-10-01 21:12:47 -06001812 self._SetupSplElf()
Simon Glass1d0ebf72019-05-14 15:53:42 -06001813 with open(self.TestFile('bss_data'), 'rb') as fd:
Simon Glass4c650252019-07-08 13:18:46 -06001814 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
1815 with open(self.TestFile('bss_data'), 'rb') as fd:
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001816 TestFunctional._MakeInputFile('-boot', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001817 data = self._DoReadFile('096_elf.dts')
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001818
Simon Glass093d1682019-07-08 13:18:25 -06001819 def testElfStrip(self):
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001820 """Basic test of ELF entries"""
Simon Glass11ae93e2018-10-01 21:12:47 -06001821 self._SetupSplElf()
Simon Glass1d0ebf72019-05-14 15:53:42 -06001822 with open(self.TestFile('bss_data'), 'rb') as fd:
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001823 TestFunctional._MakeInputFile('-boot', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001824 data = self._DoReadFile('097_elf_strip.dts')
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001825
Simon Glass163ed6c2018-09-14 04:57:36 -06001826 def testPackOverlapMap(self):
1827 """Test that overlapping regions are detected"""
1828 with test_util.capture_sys_output() as (stdout, stderr):
1829 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001830 self._DoTestFile('014_pack_overlap.dts', map=True)
Simon Glass163ed6c2018-09-14 04:57:36 -06001831 map_fname = tools.GetOutputFilename('image.map')
1832 self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname,
1833 stdout.getvalue())
1834
1835 # We should not get an inmage, but there should be a map file
1836 self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin')))
1837 self.assertTrue(os.path.exists(map_fname))
Simon Glasseb546ac2019-05-17 22:00:51 -06001838 map_data = tools.ReadFile(map_fname, binary=False)
Simon Glass163ed6c2018-09-14 04:57:36 -06001839 self.assertEqual('''ImagePos Offset Size Name
1840<none> 00000000 00000007 main-section
1841<none> 00000000 00000004 u-boot
1842<none> 00000003 00000004 u-boot-align
1843''', map_data)
1844
Simon Glass093d1682019-07-08 13:18:25 -06001845 def testPackRefCode(self):
Simon Glass3ae192c2018-10-01 12:22:31 -06001846 """Test that an image with an Intel Reference code binary works"""
1847 data = self._DoReadFile('100_intel_refcode.dts')
1848 self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)])
1849
Simon Glass9481c802019-04-25 21:58:39 -06001850 def testSectionOffset(self):
1851 """Tests use of a section with an offset"""
1852 data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts',
1853 map=True)
1854 self.assertEqual('''ImagePos Offset Size Name
185500000000 00000000 00000038 main-section
185600000004 00000004 00000010 section@0
185700000004 00000000 00000004 u-boot
185800000018 00000018 00000010 section@1
185900000018 00000000 00000004 u-boot
18600000002c 0000002c 00000004 section@2
18610000002c 00000000 00000004 u-boot
1862''', map_data)
1863 self.assertEqual(data,
Simon Glasse6d85ff2019-05-14 15:53:47 -06001864 tools.GetBytes(0x26, 4) + U_BOOT_DATA +
1865 tools.GetBytes(0x21, 12) +
1866 tools.GetBytes(0x26, 4) + U_BOOT_DATA +
1867 tools.GetBytes(0x61, 12) +
1868 tools.GetBytes(0x26, 4) + U_BOOT_DATA +
1869 tools.GetBytes(0x26, 8))
Simon Glass9481c802019-04-25 21:58:39 -06001870
Simon Glassac62fba2019-07-08 13:18:53 -06001871 def testCbfsRaw(self):
1872 """Test base handling of a Coreboot Filesystem (CBFS)
1873
1874 The exact contents of the CBFS is verified by similar tests in
1875 cbfs_util_test.py. The tests here merely check that the files added to
1876 the CBFS can be found in the final image.
1877 """
1878 data = self._DoReadFile('102_cbfs_raw.dts')
1879 size = 0xb0
1880
1881 cbfs = cbfs_util.CbfsReader(data)
1882 self.assertEqual(size, cbfs.rom_size)
1883
1884 self.assertIn('u-boot-dtb', cbfs.files)
1885 cfile = cbfs.files['u-boot-dtb']
1886 self.assertEqual(U_BOOT_DTB_DATA, cfile.data)
1887
1888 def testCbfsArch(self):
1889 """Test on non-x86 architecture"""
1890 data = self._DoReadFile('103_cbfs_raw_ppc.dts')
1891 size = 0x100
1892
1893 cbfs = cbfs_util.CbfsReader(data)
1894 self.assertEqual(size, cbfs.rom_size)
1895
1896 self.assertIn('u-boot-dtb', cbfs.files)
1897 cfile = cbfs.files['u-boot-dtb']
1898 self.assertEqual(U_BOOT_DTB_DATA, cfile.data)
1899
1900 def testCbfsStage(self):
1901 """Tests handling of a Coreboot Filesystem (CBFS)"""
1902 if not elf.ELF_TOOLS:
1903 self.skipTest('Python elftools not available')
1904 elf_fname = os.path.join(self._indir, 'cbfs-stage.elf')
1905 elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA)
1906 size = 0xb0
1907
1908 data = self._DoReadFile('104_cbfs_stage.dts')
1909 cbfs = cbfs_util.CbfsReader(data)
1910 self.assertEqual(size, cbfs.rom_size)
1911
1912 self.assertIn('u-boot', cbfs.files)
1913 cfile = cbfs.files['u-boot']
1914 self.assertEqual(U_BOOT_DATA + U_BOOT_DTB_DATA, cfile.data)
1915
1916 def testCbfsRawCompress(self):
1917 """Test handling of compressing raw files"""
1918 self._CheckLz4()
1919 data = self._DoReadFile('105_cbfs_raw_compress.dts')
1920 size = 0x140
1921
1922 cbfs = cbfs_util.CbfsReader(data)
1923 self.assertIn('u-boot', cbfs.files)
1924 cfile = cbfs.files['u-boot']
1925 self.assertEqual(COMPRESS_DATA, cfile.data)
1926
1927 def testCbfsBadArch(self):
1928 """Test handling of a bad architecture"""
1929 with self.assertRaises(ValueError) as e:
1930 self._DoReadFile('106_cbfs_bad_arch.dts')
1931 self.assertIn("Invalid architecture 'bad-arch'", str(e.exception))
1932
1933 def testCbfsNoSize(self):
1934 """Test handling of a missing size property"""
1935 with self.assertRaises(ValueError) as e:
1936 self._DoReadFile('107_cbfs_no_size.dts')
1937 self.assertIn('entry must have a size property', str(e.exception))
1938
1939 def testCbfsNoCOntents(self):
1940 """Test handling of a CBFS entry which does not provide contentsy"""
1941 with self.assertRaises(ValueError) as e:
1942 self._DoReadFile('108_cbfs_no_contents.dts')
1943 self.assertIn('Could not complete processing of contents',
1944 str(e.exception))
1945
1946 def testCbfsBadCompress(self):
1947 """Test handling of a bad architecture"""
1948 with self.assertRaises(ValueError) as e:
1949 self._DoReadFile('109_cbfs_bad_compress.dts')
1950 self.assertIn("Invalid compression in 'u-boot': 'invalid-algo'",
1951 str(e.exception))
1952
1953 def testCbfsNamedEntries(self):
1954 """Test handling of named entries"""
1955 data = self._DoReadFile('110_cbfs_name.dts')
1956
1957 cbfs = cbfs_util.CbfsReader(data)
1958 self.assertIn('FRED', cbfs.files)
1959 cfile1 = cbfs.files['FRED']
1960 self.assertEqual(U_BOOT_DATA, cfile1.data)
1961
1962 self.assertIn('hello', cbfs.files)
1963 cfile2 = cbfs.files['hello']
1964 self.assertEqual(U_BOOT_DTB_DATA, cfile2.data)
1965
Simon Glassc5ac1382019-07-08 13:18:54 -06001966 def _SetupIfwi(self, fname):
1967 """Set up to run an IFWI test
1968
1969 Args:
1970 fname: Filename of input file to provide (fitimage.bin or ifwi.bin)
1971 """
1972 self._SetupSplElf()
1973
1974 # Intel Integrated Firmware Image (IFWI) file
1975 with gzip.open(self.TestFile('%s.gz' % fname), 'rb') as fd:
1976 data = fd.read()
1977 TestFunctional._MakeInputFile(fname,data)
1978
1979 def _CheckIfwi(self, data):
1980 """Check that an image with an IFWI contains the correct output
1981
1982 Args:
1983 data: Conents of output file
1984 """
1985 expected_desc = tools.ReadFile(self.TestFile('descriptor.bin'))
1986 if data[:0x1000] != expected_desc:
1987 self.fail('Expected descriptor binary at start of image')
1988
1989 # We expect to find the TPL wil in subpart IBBP entry IBBL
1990 image_fname = tools.GetOutputFilename('image.bin')
1991 tpl_fname = tools.GetOutputFilename('tpl.out')
1992 tools.RunIfwiTool(image_fname, tools.CMD_EXTRACT, fname=tpl_fname,
1993 subpart='IBBP', entry_name='IBBL')
1994
1995 tpl_data = tools.ReadFile(tpl_fname)
1996 self.assertEqual(tpl_data[:len(U_BOOT_TPL_DATA)], U_BOOT_TPL_DATA)
1997
1998 def testPackX86RomIfwi(self):
1999 """Test that an x86 ROM with Integrated Firmware Image can be created"""
2000 self._SetupIfwi('fitimage.bin')
2001 data = self._DoReadFile('111_x86-rom-ifwi.dts')
2002 self._CheckIfwi(data)
2003
2004 def testPackX86RomIfwiNoDesc(self):
2005 """Test that an x86 ROM with IFWI can be created from an ifwi.bin file"""
2006 self._SetupIfwi('ifwi.bin')
2007 data = self._DoReadFile('112_x86-rom-ifwi-nodesc.dts')
2008 self._CheckIfwi(data)
2009
2010 def testPackX86RomIfwiNoData(self):
2011 """Test that an x86 ROM with IFWI handles missing data"""
2012 self._SetupIfwi('ifwi.bin')
2013 with self.assertRaises(ValueError) as e:
2014 data = self._DoReadFile('113_x86-rom-ifwi-nodata.dts')
2015 self.assertIn('Could not complete processing of contents',
2016 str(e.exception))
Simon Glass53af22a2018-07-17 13:25:32 -06002017
Simon Glasse073d4e2019-07-08 13:18:56 -06002018 def testCbfsOffset(self):
2019 """Test a CBFS with files at particular offsets
2020
2021 Like all CFBS tests, this is just checking the logic that calls
2022 cbfs_util. See cbfs_util_test for fully tests (e.g. test_cbfs_offset()).
2023 """
2024 data = self._DoReadFile('114_cbfs_offset.dts')
2025 size = 0x200
2026
2027 cbfs = cbfs_util.CbfsReader(data)
2028 self.assertEqual(size, cbfs.rom_size)
2029
2030 self.assertIn('u-boot', cbfs.files)
2031 cfile = cbfs.files['u-boot']
2032 self.assertEqual(U_BOOT_DATA, cfile.data)
2033 self.assertEqual(0x40, cfile.cbfs_offset)
2034
2035 self.assertIn('u-boot-dtb', cbfs.files)
2036 cfile2 = cbfs.files['u-boot-dtb']
2037 self.assertEqual(U_BOOT_DTB_DATA, cfile2.data)
2038 self.assertEqual(0x140, cfile2.cbfs_offset)
2039
Simon Glass086cec92019-07-08 14:25:27 -06002040 def testFdtmap(self):
2041 """Test an FDT map can be inserted in the image"""
2042 data = self.data = self._DoReadFileRealDtb('115_fdtmap.dts')
2043 fdtmap_data = data[len(U_BOOT_DATA):]
2044 magic = fdtmap_data[:8]
2045 self.assertEqual('_FDTMAP_', magic)
2046 self.assertEqual(tools.GetBytes(0, 8), fdtmap_data[8:16])
2047
2048 fdt_data = fdtmap_data[16:]
2049 dtb = fdt.Fdt.FromData(fdt_data)
2050 dtb.Scan()
2051 props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos'],
2052 prefix='/')
2053 self.assertEqual({
2054 'image-pos': 0,
2055 'offset': 0,
2056 'u-boot:offset': 0,
2057 'u-boot:size': len(U_BOOT_DATA),
2058 'u-boot:image-pos': 0,
2059 'fdtmap:image-pos': 4,
2060 'fdtmap:offset': 4,
2061 'fdtmap:size': len(fdtmap_data),
2062 'size': len(data),
2063 }, props)
2064
2065 def testFdtmapNoMatch(self):
2066 """Check handling of an FDT map when the section cannot be found"""
2067 self.data = self._DoReadFileRealDtb('115_fdtmap.dts')
2068
2069 # Mangle the section name, which should cause a mismatch between the
2070 # correct FDT path and the one expected by the section
2071 image = control.images['image']
Simon Glasscf228942019-07-08 14:25:28 -06002072 image._node.path += '-suffix'
Simon Glass086cec92019-07-08 14:25:27 -06002073 entries = image.GetEntries()
2074 fdtmap = entries['fdtmap']
2075 with self.assertRaises(ValueError) as e:
2076 fdtmap._GetFdtmap()
2077 self.assertIn("Cannot locate node for path '/binman-suffix'",
2078 str(e.exception))
2079
Simon Glasscf228942019-07-08 14:25:28 -06002080 def testFdtmapHeader(self):
2081 """Test an FDT map and image header can be inserted in the image"""
2082 data = self.data = self._DoReadFileRealDtb('116_fdtmap_hdr.dts')
2083 fdtmap_pos = len(U_BOOT_DATA)
2084 fdtmap_data = data[fdtmap_pos:]
2085 fdt_data = fdtmap_data[16:]
2086 dtb = fdt.Fdt.FromData(fdt_data)
2087 fdt_size = dtb.GetFdtObj().totalsize()
2088 hdr_data = data[-8:]
2089 self.assertEqual('BinM', hdr_data[:4])
2090 offset = struct.unpack('<I', hdr_data[4:])[0] & 0xffffffff
2091 self.assertEqual(fdtmap_pos - 0x400, offset - (1 << 32))
2092
2093 def testFdtmapHeaderStart(self):
2094 """Test an image header can be inserted at the image start"""
2095 data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts')
2096 fdtmap_pos = 0x100 + len(U_BOOT_DATA)
2097 hdr_data = data[:8]
2098 self.assertEqual('BinM', hdr_data[:4])
2099 offset = struct.unpack('<I', hdr_data[4:])[0]
2100 self.assertEqual(fdtmap_pos, offset)
2101
2102 def testFdtmapHeaderPos(self):
2103 """Test an image header can be inserted at a chosen position"""
2104 data = self.data = self._DoReadFileRealDtb('118_fdtmap_hdr_pos.dts')
2105 fdtmap_pos = 0x100 + len(U_BOOT_DATA)
2106 hdr_data = data[0x80:0x88]
2107 self.assertEqual('BinM', hdr_data[:4])
2108 offset = struct.unpack('<I', hdr_data[4:])[0]
2109 self.assertEqual(fdtmap_pos, offset)
2110
2111 def testHeaderMissingFdtmap(self):
2112 """Test an image header requires an fdtmap"""
2113 with self.assertRaises(ValueError) as e:
2114 self.data = self._DoReadFileRealDtb('119_fdtmap_hdr_missing.dts')
2115 self.assertIn("'image_header' section must have an 'fdtmap' sibling",
2116 str(e.exception))
2117
2118 def testHeaderNoLocation(self):
2119 """Test an image header with a no specified location is detected"""
2120 with self.assertRaises(ValueError) as e:
2121 self.data = self._DoReadFileRealDtb('120_hdr_no_location.dts')
2122 self.assertIn("Invalid location 'None', expected 'start' or 'end'",
2123 str(e.exception))
2124
Simon Glassc52c9e72019-07-08 14:25:37 -06002125 def testEntryExpand(self):
2126 """Test expanding an entry after it is packed"""
2127 data = self._DoReadFile('121_entry_expand.dts')
2128 self.assertEqual(b'aa', data[:2])
2129 self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)])
2130 self.assertEqual(b'aa', data[-2:])
2131
2132 def testEntryExpandBad(self):
2133 """Test expanding an entry after it is packed, twice"""
2134 with self.assertRaises(ValueError) as e:
2135 self._DoReadFile('122_entry_expand_twice.dts')
2136 self.assertIn("Image '/binman': Entries expanded after packing",
2137 str(e.exception))
2138
2139 def testEntryExpandSection(self):
2140 """Test expanding an entry within a section after it is packed"""
2141 data = self._DoReadFile('123_entry_expand_section.dts')
2142 self.assertEqual(b'aa', data[:2])
2143 self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)])
2144 self.assertEqual(b'aa', data[-2:])
2145
Simon Glass6c223fd2019-07-08 14:25:38 -06002146 def testCompressDtb(self):
2147 """Test that compress of device-tree files is supported"""
2148 self._CheckLz4()
2149 data = self.data = self._DoReadFileRealDtb('124_compress_dtb.dts')
2150 self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
2151 comp_data = data[len(U_BOOT_DATA):]
2152 orig = self._decompress(comp_data)
2153 dtb = fdt.Fdt.FromData(orig)
2154 dtb.Scan()
2155 props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
2156 expected = {
2157 'u-boot:size': len(U_BOOT_DATA),
2158 'u-boot-dtb:uncomp-size': len(orig),
2159 'u-boot-dtb:size': len(comp_data),
2160 'size': len(data),
2161 }
2162 self.assertEqual(expected, props)
2163
Simon Glass69f7cb32019-07-08 14:25:41 -06002164 def testCbfsUpdateFdt(self):
2165 """Test that we can update the device tree with CBFS offset/size info"""
2166 self._CheckLz4()
2167 data, _, _, out_dtb_fname = self._DoReadFileDtb('125_cbfs_update.dts',
2168 update_dtb=True)
2169 dtb = fdt.Fdt(out_dtb_fname)
2170 dtb.Scan()
2171 props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos',
2172 'uncomp-size'])
2173 del props['cbfs/u-boot:size']
2174 self.assertEqual({
2175 'offset': 0,
2176 'size': len(data),
2177 'image-pos': 0,
2178 'cbfs:offset': 0,
2179 'cbfs:size': len(data),
2180 'cbfs:image-pos': 0,
2181 'cbfs/u-boot:offset': 0x38,
2182 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA),
2183 'cbfs/u-boot:image-pos': 0x38,
2184 'cbfs/u-boot-dtb:offset': 0xb8,
2185 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA),
2186 'cbfs/u-boot-dtb:image-pos': 0xb8,
2187 }, props)
2188
Simon Glass8a1ad062019-07-08 14:25:42 -06002189 def testCbfsBadType(self):
2190 """Test an image header with a no specified location is detected"""
2191 with self.assertRaises(ValueError) as e:
2192 self._DoReadFile('126_cbfs_bad_type.dts')
2193 self.assertIn("Unknown cbfs-type 'badtype'", str(e.exception))
2194
Simon Glass41b8ba02019-07-08 14:25:43 -06002195 def testList(self):
2196 """Test listing the files in an image"""
2197 self._CheckLz4()
2198 data = self._DoReadFile('127_list.dts')
2199 image = control.images['image']
2200 entries = image.BuildEntryList()
2201 self.assertEqual(7, len(entries))
2202
2203 ent = entries[0]
2204 self.assertEqual(0, ent.indent)
2205 self.assertEqual('main-section', ent.name)
2206 self.assertEqual('section', ent.etype)
2207 self.assertEqual(len(data), ent.size)
2208 self.assertEqual(0, ent.image_pos)
2209 self.assertEqual(None, ent.uncomp_size)
2210 self.assertEqual(None, ent.offset)
2211
2212 ent = entries[1]
2213 self.assertEqual(1, ent.indent)
2214 self.assertEqual('u-boot', ent.name)
2215 self.assertEqual('u-boot', ent.etype)
2216 self.assertEqual(len(U_BOOT_DATA), ent.size)
2217 self.assertEqual(0, ent.image_pos)
2218 self.assertEqual(None, ent.uncomp_size)
2219 self.assertEqual(0, ent.offset)
2220
2221 ent = entries[2]
2222 self.assertEqual(1, ent.indent)
2223 self.assertEqual('section', ent.name)
2224 self.assertEqual('section', ent.etype)
2225 section_size = ent.size
2226 self.assertEqual(0x100, ent.image_pos)
2227 self.assertEqual(None, ent.uncomp_size)
2228 self.assertEqual(len(U_BOOT_DATA), ent.offset)
2229
2230 ent = entries[3]
2231 self.assertEqual(2, ent.indent)
2232 self.assertEqual('cbfs', ent.name)
2233 self.assertEqual('cbfs', ent.etype)
2234 self.assertEqual(0x400, ent.size)
2235 self.assertEqual(0x100, ent.image_pos)
2236 self.assertEqual(None, ent.uncomp_size)
2237 self.assertEqual(0, ent.offset)
2238
2239 ent = entries[4]
2240 self.assertEqual(3, ent.indent)
2241 self.assertEqual('u-boot', ent.name)
2242 self.assertEqual('u-boot', ent.etype)
2243 self.assertEqual(len(U_BOOT_DATA), ent.size)
2244 self.assertEqual(0x138, ent.image_pos)
2245 self.assertEqual(None, ent.uncomp_size)
2246 self.assertEqual(0x38, ent.offset)
2247
2248 ent = entries[5]
2249 self.assertEqual(3, ent.indent)
2250 self.assertEqual('u-boot-dtb', ent.name)
2251 self.assertEqual('text', ent.etype)
2252 self.assertGreater(len(COMPRESS_DATA), ent.size)
2253 self.assertEqual(0x178, ent.image_pos)
2254 self.assertEqual(len(COMPRESS_DATA), ent.uncomp_size)
2255 self.assertEqual(0x78, ent.offset)
2256
2257 ent = entries[6]
2258 self.assertEqual(2, ent.indent)
2259 self.assertEqual('u-boot-dtb', ent.name)
2260 self.assertEqual('u-boot-dtb', ent.etype)
2261 self.assertEqual(0x500, ent.image_pos)
2262 self.assertEqual(len(U_BOOT_DTB_DATA), ent.uncomp_size)
2263 dtb_size = ent.size
2264 # Compressing this data expands it since headers are added
2265 self.assertGreater(dtb_size, len(U_BOOT_DTB_DATA))
2266 self.assertEqual(0x400, ent.offset)
2267
2268 self.assertEqual(len(data), 0x100 + section_size)
2269 self.assertEqual(section_size, 0x400 + dtb_size)
2270
Simon Glasse1925fa2019-07-08 14:25:44 -06002271 def testFindFdtmap(self):
2272 """Test locating an FDT map in an image"""
2273 self._CheckLz4()
2274 data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
2275 image = control.images['image']
2276 entries = image.GetEntries()
2277 entry = entries['fdtmap']
2278 self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data))
2279
2280 def testFindFdtmapMissing(self):
2281 """Test failing to locate an FDP map"""
2282 data = self._DoReadFile('005_simple.dts')
2283 self.assertEqual(None, fdtmap.LocateFdtmap(data))
2284
Simon Glasse073d4e2019-07-08 13:18:56 -06002285
Simon Glass9fc60b42017-11-12 21:52:22 -07002286if __name__ == "__main__":
2287 unittest.main()