blob: 1d774e28e54a09cb5f3295544fc6c3428d78840e [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 Glass53e22bf2019-08-24 07:22:53 -060026import elf_test
Simon Glass99ed4a22017-05-27 07:38:30 -060027import fdt
Simon Glasse1925fa2019-07-08 14:25:44 -060028from etype import fdtmap
Simon Glass2d260032019-07-08 14:25:45 -060029from etype import image_header
Simon Glass4f443042016-11-25 20:15:52 -070030import fdt_util
Simon Glass11e36cc2018-07-17 13:25:38 -060031import fmap_util
Simon Glassfd8d1f72018-07-17 13:25:36 -060032import test_util
Simon Glassc5ac1382019-07-08 13:18:54 -060033import gzip
Simon Glassffded752019-07-08 14:25:46 -060034from image import Image
Simon Glassc55a50f2018-09-14 04:57:19 -060035import state
Simon Glass4f443042016-11-25 20:15:52 -070036import tools
37import tout
38
39# Contents of test files, corresponding to different entry types
Simon Glassc6c10e72019-05-17 22:00:46 -060040U_BOOT_DATA = b'1234'
41U_BOOT_IMG_DATA = b'img'
42U_BOOT_SPL_DATA = b'56780123456789abcde'
43U_BOOT_TPL_DATA = b'tpl'
44BLOB_DATA = b'89'
45ME_DATA = b'0abcd'
46VGA_DATA = b'vga'
47U_BOOT_DTB_DATA = b'udtb'
48U_BOOT_SPL_DTB_DATA = b'spldtb'
49U_BOOT_TPL_DTB_DATA = b'tpldtb'
50X86_START16_DATA = b'start16'
51X86_START16_SPL_DATA = b'start16spl'
52X86_START16_TPL_DATA = b'start16tpl'
Simon Glass2250ee62019-08-24 07:22:48 -060053X86_RESET16_DATA = b'reset16'
54X86_RESET16_SPL_DATA = b'reset16spl'
55X86_RESET16_TPL_DATA = b'reset16tpl'
Simon Glassc6c10e72019-05-17 22:00:46 -060056PPC_MPC85XX_BR_DATA = b'ppcmpc85xxbr'
57U_BOOT_NODTB_DATA = b'nodtb with microcode pointer somewhere in here'
58U_BOOT_SPL_NODTB_DATA = b'splnodtb with microcode pointer somewhere in here'
59U_BOOT_TPL_NODTB_DATA = b'tplnodtb with microcode pointer somewhere in here'
60FSP_DATA = b'fsp'
61CMC_DATA = b'cmc'
62VBT_DATA = b'vbt'
63MRC_DATA = b'mrc'
Simon Glassbb748372018-07-17 13:25:33 -060064TEXT_DATA = 'text'
65TEXT_DATA2 = 'text2'
66TEXT_DATA3 = 'text3'
Simon Glassc6c10e72019-05-17 22:00:46 -060067CROS_EC_RW_DATA = b'ecrw'
68GBB_DATA = b'gbbd'
69BMPBLK_DATA = b'bmp'
70VBLOCK_DATA = b'vblk'
71FILES_DATA = (b"sorry I'm late\nOh, don't bother apologising, I'm " +
72 b"sorry you're alive\n")
Simon Glassff5c7e32019-07-08 13:18:42 -060073COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data'
Simon Glassc6c10e72019-05-17 22:00:46 -060074REFCODE_DATA = b'refcode'
Simon Glassec127af2018-07-17 13:25:39 -060075
Simon Glass6ccbfcd2019-07-20 12:23:47 -060076# The expected size for the device tree in some tests
Simon Glassf667e452019-07-08 14:25:50 -060077EXTRACT_DTB_SIZE = 0x3c9
78
Simon Glass6ccbfcd2019-07-20 12:23:47 -060079# Properties expected to be in the device tree when update_dtb is used
80BASE_DTB_PROPS = ['offset', 'size', 'image-pos']
81
Simon Glass12bb1a92019-07-20 12:23:51 -060082# Extra properties expected to be in the device tree when allow-repack is used
83REPACK_DTB_PROPS = ['orig-offset', 'orig-size']
84
Simon Glass4f443042016-11-25 20:15:52 -070085
86class TestFunctional(unittest.TestCase):
87 """Functional tests for binman
88
89 Most of these use a sample .dts file to build an image and then check
90 that it looks correct. The sample files are in the test/ subdirectory
91 and are numbered.
92
93 For each entry type a very small test file is created using fixed
94 string contents. This makes it easy to test that things look right, and
95 debug problems.
96
97 In some cases a 'real' file must be used - these are also supplied in
98 the test/ diurectory.
99 """
100 @classmethod
Simon Glassb986b3b2019-08-24 07:22:43 -0600101 def setUpClass(cls):
Simon Glass4d5994f2017-11-12 21:52:20 -0700102 global entry
103 import entry
104
Simon Glass4f443042016-11-25 20:15:52 -0700105 # Handle the case where argv[0] is 'python'
Simon Glassb986b3b2019-08-24 07:22:43 -0600106 cls._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
107 cls._binman_pathname = os.path.join(cls._binman_dir, 'binman')
Simon Glass4f443042016-11-25 20:15:52 -0700108
109 # Create a temporary directory for input files
Simon Glassb986b3b2019-08-24 07:22:43 -0600110 cls._indir = tempfile.mkdtemp(prefix='binmant.')
Simon Glass4f443042016-11-25 20:15:52 -0700111
112 # Create some test files
113 TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
114 TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
115 TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
Simon Glassb8ef5b62018-07-17 13:25:48 -0600116 TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700117 TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -0700118 TestFunctional._MakeInputFile('me.bin', ME_DATA)
119 TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
Simon Glassb986b3b2019-08-24 07:22:43 -0600120 cls._ResetDtbs()
Simon Glass2250ee62019-08-24 07:22:48 -0600121
Jagdish Gediya9d368f32018-09-03 21:35:08 +0530122 TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA)
Simon Glass2250ee62019-08-24 07:22:48 -0600123
Simon Glass5e239182019-08-24 07:22:49 -0600124 TestFunctional._MakeInputFile('u-boot-x86-start16.bin', X86_START16_DATA)
125 TestFunctional._MakeInputFile('spl/u-boot-x86-start16-spl.bin',
Simon Glass87722132017-11-12 21:52:26 -0700126 X86_START16_SPL_DATA)
Simon Glass5e239182019-08-24 07:22:49 -0600127 TestFunctional._MakeInputFile('tpl/u-boot-x86-start16-tpl.bin',
Simon Glass35b384c2018-09-14 04:57:10 -0600128 X86_START16_TPL_DATA)
Simon Glass2250ee62019-08-24 07:22:48 -0600129
130 TestFunctional._MakeInputFile('u-boot-x86-reset16.bin',
131 X86_RESET16_DATA)
132 TestFunctional._MakeInputFile('spl/u-boot-x86-reset16-spl.bin',
133 X86_RESET16_SPL_DATA)
134 TestFunctional._MakeInputFile('tpl/u-boot-x86-reset16-tpl.bin',
135 X86_RESET16_TPL_DATA)
136
Simon Glass4f443042016-11-25 20:15:52 -0700137 TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
Simon Glass6b187df2017-11-12 21:52:27 -0700138 TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
139 U_BOOT_SPL_NODTB_DATA)
Simon Glassf0253632018-09-14 04:57:32 -0600140 TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin',
141 U_BOOT_TPL_NODTB_DATA)
Simon Glassda229092016-11-25 20:15:56 -0700142 TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
143 TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
Bin Meng59ea8c22017-08-15 22:41:54 -0700144 TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
Simon Glassca4f4ff2017-11-12 21:52:28 -0700145 TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
Simon Glassec127af2018-07-17 13:25:39 -0600146 TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
Simon Glass0ef87aa2018-07-17 13:25:44 -0600147 TestFunctional._MakeInputDir('devkeys')
148 TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
Simon Glass3ae192c2018-10-01 12:22:31 -0600149 TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA)
Simon Glass4f443042016-11-25 20:15:52 -0700150
Simon Glass53e22bf2019-08-24 07:22:53 -0600151 cls._elf_testdir = os.path.join(cls._indir, 'elftest')
152 elf_test.BuildElfTestFiles(cls._elf_testdir)
153
Simon Glasse0ff8552016-11-25 20:15:53 -0700154 # ELF file with a '_dt_ucode_base_size' symbol
Simon Glassf514d8f2019-08-24 07:22:54 -0600155 TestFunctional._MakeInputFile('u-boot',
156 tools.ReadFile(cls.ElfTestFile('u_boot_ucode_ptr')))
Simon Glasse0ff8552016-11-25 20:15:53 -0700157
158 # Intel flash descriptor file
Simon Glassb986b3b2019-08-24 07:22:43 -0600159 with open(cls.TestFile('descriptor.bin'), 'rb') as fd:
Simon Glasse0ff8552016-11-25 20:15:53 -0700160 TestFunctional._MakeInputFile('descriptor.bin', fd.read())
161
Simon Glassb986b3b2019-08-24 07:22:43 -0600162 shutil.copytree(cls.TestFile('files'),
163 os.path.join(cls._indir, 'files'))
Simon Glass0a98b282018-09-14 04:57:28 -0600164
Simon Glass83d73c22018-09-14 04:57:26 -0600165 TestFunctional._MakeInputFile('compress', COMPRESS_DATA)
166
Simon Glassac62fba2019-07-08 13:18:53 -0600167 # Travis-CI may have an old lz4
Simon Glassb986b3b2019-08-24 07:22:43 -0600168 cls.have_lz4 = True
Simon Glassac62fba2019-07-08 13:18:53 -0600169 try:
170 tools.Run('lz4', '--no-frame-crc', '-c',
Simon Glassb986b3b2019-08-24 07:22:43 -0600171 os.path.join(cls._indir, 'u-boot.bin'))
Simon Glassac62fba2019-07-08 13:18:53 -0600172 except:
Simon Glassb986b3b2019-08-24 07:22:43 -0600173 cls.have_lz4 = False
Simon Glassac62fba2019-07-08 13:18:53 -0600174
Simon Glass4f443042016-11-25 20:15:52 -0700175 @classmethod
Simon Glassb986b3b2019-08-24 07:22:43 -0600176 def tearDownClass(cls):
Simon Glass4f443042016-11-25 20:15:52 -0700177 """Remove the temporary input directory and its contents"""
Simon Glassb986b3b2019-08-24 07:22:43 -0600178 if cls.preserve_indir:
179 print('Preserving input dir: %s' % cls._indir)
Simon Glassd5164a72019-07-08 13:18:49 -0600180 else:
Simon Glassb986b3b2019-08-24 07:22:43 -0600181 if cls._indir:
182 shutil.rmtree(cls._indir)
183 cls._indir = None
Simon Glass4f443042016-11-25 20:15:52 -0700184
Simon Glassd5164a72019-07-08 13:18:49 -0600185 @classmethod
Simon Glass8acce602019-07-08 13:18:50 -0600186 def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False,
Simon Glass53cd5d92019-07-08 14:25:29 -0600187 toolpath=None, verbosity=None):
Simon Glassd5164a72019-07-08 13:18:49 -0600188 """Accept arguments controlling test execution
189
190 Args:
191 preserve_indir: Preserve the shared input directory used by all
192 tests in this class.
193 preserve_outdir: Preserve the output directories used by tests. Each
194 test has its own, so this is normally only useful when running a
195 single test.
Simon Glass8acce602019-07-08 13:18:50 -0600196 toolpath: ist of paths to use for tools
Simon Glassd5164a72019-07-08 13:18:49 -0600197 """
198 cls.preserve_indir = preserve_indir
199 cls.preserve_outdirs = preserve_outdirs
Simon Glass8acce602019-07-08 13:18:50 -0600200 cls.toolpath = toolpath
Simon Glass53cd5d92019-07-08 14:25:29 -0600201 cls.verbosity = verbosity
Simon Glassd5164a72019-07-08 13:18:49 -0600202
Simon Glassac62fba2019-07-08 13:18:53 -0600203 def _CheckLz4(self):
204 if not self.have_lz4:
205 self.skipTest('lz4 --no-frame-crc not available')
206
Simon Glassbf574f12019-07-20 12:24:09 -0600207 def _CleanupOutputDir(self):
208 """Remove the temporary output directory"""
209 if self.preserve_outdirs:
210 print('Preserving output dir: %s' % tools.outdir)
211 else:
212 tools._FinaliseForTest()
213
Simon Glass4f443042016-11-25 20:15:52 -0700214 def setUp(self):
215 # Enable this to turn on debugging output
216 # tout.Init(tout.DEBUG)
217 command.test_result = None
218
219 def tearDown(self):
220 """Remove the temporary output directory"""
Simon Glassbf574f12019-07-20 12:24:09 -0600221 self._CleanupOutputDir()
Simon Glass4f443042016-11-25 20:15:52 -0700222
Simon Glassf86a7362019-07-20 12:24:10 -0600223 def _SetupImageInTmpdir(self):
224 """Set up the output image in a new temporary directory
225
226 This is used when an image has been generated in the output directory,
227 but we want to run binman again. This will create a new output
228 directory and fail to delete the original one.
229
230 This creates a new temporary directory, copies the image to it (with a
231 new name) and removes the old output directory.
232
233 Returns:
234 Tuple:
235 Temporary directory to use
236 New image filename
237 """
238 image_fname = tools.GetOutputFilename('image.bin')
239 tmpdir = tempfile.mkdtemp(prefix='binman.')
240 updated_fname = os.path.join(tmpdir, 'image-updated.bin')
241 tools.WriteFile(updated_fname, tools.ReadFile(image_fname))
242 self._CleanupOutputDir()
243 return tmpdir, updated_fname
244
Simon Glassb8ef5b62018-07-17 13:25:48 -0600245 @classmethod
Simon Glassb986b3b2019-08-24 07:22:43 -0600246 def _ResetDtbs(cls):
Simon Glassb8ef5b62018-07-17 13:25:48 -0600247 TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
248 TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
249 TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA)
250
Simon Glass4f443042016-11-25 20:15:52 -0700251 def _RunBinman(self, *args, **kwargs):
252 """Run binman using the command line
253
254 Args:
255 Arguments to pass, as a list of strings
256 kwargs: Arguments to pass to Command.RunPipe()
257 """
258 result = command.RunPipe([[self._binman_pathname] + list(args)],
259 capture=True, capture_stderr=True, raise_on_error=False)
260 if result.return_code and kwargs.get('raise_on_error', True):
261 raise Exception("Error running '%s': %s" % (' '.join(args),
262 result.stdout + result.stderr))
263 return result
264
Simon Glass53cd5d92019-07-08 14:25:29 -0600265 def _DoBinman(self, *argv):
Simon Glass4f443042016-11-25 20:15:52 -0700266 """Run binman using directly (in the same process)
267
268 Args:
269 Arguments to pass, as a list of strings
270 Returns:
271 Return value (0 for success)
272 """
Simon Glass53cd5d92019-07-08 14:25:29 -0600273 argv = list(argv)
274 args = cmdline.ParseArgs(argv)
275 args.pager = 'binman-invalid-pager'
276 args.build_dir = self._indir
Simon Glass4f443042016-11-25 20:15:52 -0700277
278 # For testing, you can force an increase in verbosity here
Simon Glass53cd5d92019-07-08 14:25:29 -0600279 # args.verbosity = tout.DEBUG
280 return control.Binman(args)
Simon Glass4f443042016-11-25 20:15:52 -0700281
Simon Glass53af22a2018-07-17 13:25:32 -0600282 def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
Simon Glasseb833d82019-04-25 21:58:34 -0600283 entry_args=None, images=None, use_real_dtb=False,
284 verbosity=None):
Simon Glass4f443042016-11-25 20:15:52 -0700285 """Run binman with a given test file
286
287 Args:
Simon Glass741f2d62018-10-01 12:22:30 -0600288 fname: Device-tree source filename to use (e.g. 005_simple.dts)
Simon Glass7ae5f312018-06-01 09:38:19 -0600289 debug: True to enable debugging output
Simon Glass3b0c3822018-06-01 09:38:20 -0600290 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600291 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600292 tree before packing it into the image
Simon Glass0bfa7b02018-09-14 04:57:12 -0600293 entry_args: Dict of entry args to supply to binman
294 key: arg name
295 value: value of that arg
296 images: List of image names to build
Simon Glass4f443042016-11-25 20:15:52 -0700297 """
Simon Glass53cd5d92019-07-08 14:25:29 -0600298 args = []
Simon Glass7fe91732017-11-13 18:55:00 -0700299 if debug:
300 args.append('-D')
Simon Glass53cd5d92019-07-08 14:25:29 -0600301 if verbosity is not None:
302 args.append('-v%d' % verbosity)
303 elif self.verbosity:
304 args.append('-v%d' % self.verbosity)
305 if self.toolpath:
306 for path in self.toolpath:
307 args += ['--toolpath', path]
308 args += ['build', '-p', '-I', self._indir, '-d', self.TestFile(fname)]
Simon Glass3b0c3822018-06-01 09:38:20 -0600309 if map:
310 args.append('-m')
Simon Glass16b8d6b2018-07-06 10:27:42 -0600311 if update_dtb:
Simon Glass2569e102019-07-08 13:18:47 -0600312 args.append('-u')
Simon Glass93d17412018-09-14 04:57:23 -0600313 if not use_real_dtb:
314 args.append('--fake-dtb')
Simon Glass53af22a2018-07-17 13:25:32 -0600315 if entry_args:
Simon Glass50979152019-05-14 15:53:41 -0600316 for arg, value in entry_args.items():
Simon Glass53af22a2018-07-17 13:25:32 -0600317 args.append('-a%s=%s' % (arg, value))
Simon Glass0bfa7b02018-09-14 04:57:12 -0600318 if images:
319 for image in images:
320 args += ['-i', image]
Simon Glass7fe91732017-11-13 18:55:00 -0700321 return self._DoBinman(*args)
Simon Glass4f443042016-11-25 20:15:52 -0700322
323 def _SetupDtb(self, fname, outfile='u-boot.dtb'):
Simon Glasse0ff8552016-11-25 20:15:53 -0700324 """Set up a new test device-tree file
325
326 The given file is compiled and set up as the device tree to be used
327 for ths test.
328
329 Args:
330 fname: Filename of .dts file to read
Simon Glass7ae5f312018-06-01 09:38:19 -0600331 outfile: Output filename for compiled device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700332
333 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600334 Contents of device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700335 """
Simon Glassa004f292019-07-20 12:23:49 -0600336 tmpdir = tempfile.mkdtemp(prefix='binmant.')
337 dtb = fdt_util.EnsureCompiled(self.TestFile(fname), tmpdir)
Simon Glass1d0ebf72019-05-14 15:53:42 -0600338 with open(dtb, 'rb') as fd:
Simon Glass4f443042016-11-25 20:15:52 -0700339 data = fd.read()
340 TestFunctional._MakeInputFile(outfile, data)
Simon Glassa004f292019-07-20 12:23:49 -0600341 shutil.rmtree(tmpdir)
Simon Glasse0e62752018-10-01 21:12:41 -0600342 return data
Simon Glass4f443042016-11-25 20:15:52 -0700343
Simon Glass6ed45ba2018-09-14 04:57:24 -0600344 def _GetDtbContentsForSplTpl(self, dtb_data, name):
345 """Create a version of the main DTB for SPL or SPL
346
347 For testing we don't actually have different versions of the DTB. With
348 U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests
349 we don't normally have any unwanted nodes.
350
351 We still want the DTBs for SPL and TPL to be different though, since
352 otherwise it is confusing to know which one we are looking at. So add
353 an 'spl' or 'tpl' property to the top-level node.
354 """
355 dtb = fdt.Fdt.FromData(dtb_data)
356 dtb.Scan()
357 dtb.GetNode('/binman').AddZeroProp(name)
358 dtb.Sync(auto_resize=True)
359 dtb.Pack()
360 return dtb.GetContents()
361
Simon Glass16b8d6b2018-07-06 10:27:42 -0600362 def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
Simon Glass6ed45ba2018-09-14 04:57:24 -0600363 update_dtb=False, entry_args=None, reset_dtbs=True):
Simon Glass4f443042016-11-25 20:15:52 -0700364 """Run binman and return the resulting image
365
366 This runs binman with a given test file and then reads the resulting
367 output file. It is a shortcut function since most tests need to do
368 these steps.
369
370 Raises an assertion failure if binman returns a non-zero exit code.
371
372 Args:
Simon Glass741f2d62018-10-01 12:22:30 -0600373 fname: Device-tree source filename to use (e.g. 005_simple.dts)
Simon Glass4f443042016-11-25 20:15:52 -0700374 use_real_dtb: True to use the test file as the contents of
375 the u-boot-dtb entry. Normally this is not needed and the
376 test contents (the U_BOOT_DTB_DATA string) can be used.
377 But in some test we need the real contents.
Simon Glass3b0c3822018-06-01 09:38:20 -0600378 map: True to output map files for the images
Simon Glass3ab95982018-08-01 15:22:37 -0600379 update_dtb: Update the offset and size of each entry in the device
Simon Glass16b8d6b2018-07-06 10:27:42 -0600380 tree before packing it into the image
Simon Glasse0ff8552016-11-25 20:15:53 -0700381
382 Returns:
383 Tuple:
384 Resulting image contents
385 Device tree contents
Simon Glass3b0c3822018-06-01 09:38:20 -0600386 Map data showing contents of image (or None if none)
Simon Glassea6922e2018-07-17 13:25:27 -0600387 Output device tree binary filename ('u-boot.dtb' path)
Simon Glass4f443042016-11-25 20:15:52 -0700388 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700389 dtb_data = None
Simon Glass4f443042016-11-25 20:15:52 -0700390 # Use the compiled test file as the u-boot-dtb input
391 if use_real_dtb:
Simon Glasse0ff8552016-11-25 20:15:53 -0700392 dtb_data = self._SetupDtb(fname)
Simon Glass6ed45ba2018-09-14 04:57:24 -0600393
394 # For testing purposes, make a copy of the DT for SPL and TPL. Add
395 # a node indicating which it is, so aid verification.
396 for name in ['spl', 'tpl']:
397 dtb_fname = '%s/u-boot-%s.dtb' % (name, name)
398 outfile = os.path.join(self._indir, dtb_fname)
399 TestFunctional._MakeInputFile(dtb_fname,
400 self._GetDtbContentsForSplTpl(dtb_data, name))
Simon Glass4f443042016-11-25 20:15:52 -0700401
402 try:
Simon Glass53af22a2018-07-17 13:25:32 -0600403 retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
Simon Glass6ed45ba2018-09-14 04:57:24 -0600404 entry_args=entry_args, use_real_dtb=use_real_dtb)
Simon Glass4f443042016-11-25 20:15:52 -0700405 self.assertEqual(0, retcode)
Simon Glass6ed45ba2018-09-14 04:57:24 -0600406 out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out')
Simon Glass4f443042016-11-25 20:15:52 -0700407
408 # Find the (only) image, read it and return its contents
409 image = control.images['image']
Simon Glass16b8d6b2018-07-06 10:27:42 -0600410 image_fname = tools.GetOutputFilename('image.bin')
411 self.assertTrue(os.path.exists(image_fname))
Simon Glass3b0c3822018-06-01 09:38:20 -0600412 if map:
413 map_fname = tools.GetOutputFilename('image.map')
414 with open(map_fname) as fd:
415 map_data = fd.read()
416 else:
417 map_data = None
Simon Glass1d0ebf72019-05-14 15:53:42 -0600418 with open(image_fname, 'rb') as fd:
Simon Glass16b8d6b2018-07-06 10:27:42 -0600419 return fd.read(), dtb_data, map_data, out_dtb_fname
Simon Glass4f443042016-11-25 20:15:52 -0700420 finally:
421 # Put the test file back
Simon Glass6ed45ba2018-09-14 04:57:24 -0600422 if reset_dtbs and use_real_dtb:
Simon Glassb8ef5b62018-07-17 13:25:48 -0600423 self._ResetDtbs()
Simon Glass4f443042016-11-25 20:15:52 -0700424
Simon Glass3c081312019-07-08 14:25:26 -0600425 def _DoReadFileRealDtb(self, fname):
426 """Run binman with a real .dtb file and return the resulting data
427
428 Args:
429 fname: DT source filename to use (e.g. 082_fdt_update_all.dts)
430
431 Returns:
432 Resulting image contents
433 """
434 return self._DoReadFileDtb(fname, use_real_dtb=True, update_dtb=True)[0]
435
Simon Glasse0ff8552016-11-25 20:15:53 -0700436 def _DoReadFile(self, fname, use_real_dtb=False):
Simon Glass7ae5f312018-06-01 09:38:19 -0600437 """Helper function which discards the device-tree binary
438
439 Args:
Simon Glass741f2d62018-10-01 12:22:30 -0600440 fname: Device-tree source filename to use (e.g. 005_simple.dts)
Simon Glass7ae5f312018-06-01 09:38:19 -0600441 use_real_dtb: True to use the test file as the contents of
442 the u-boot-dtb entry. Normally this is not needed and the
443 test contents (the U_BOOT_DTB_DATA string) can be used.
444 But in some test we need the real contents.
Simon Glassea6922e2018-07-17 13:25:27 -0600445
446 Returns:
447 Resulting image contents
Simon Glass7ae5f312018-06-01 09:38:19 -0600448 """
Simon Glasse0ff8552016-11-25 20:15:53 -0700449 return self._DoReadFileDtb(fname, use_real_dtb)[0]
450
Simon Glass4f443042016-11-25 20:15:52 -0700451 @classmethod
Simon Glassb986b3b2019-08-24 07:22:43 -0600452 def _MakeInputFile(cls, fname, contents):
Simon Glass4f443042016-11-25 20:15:52 -0700453 """Create a new test input file, creating directories as needed
454
455 Args:
Simon Glass3ab95982018-08-01 15:22:37 -0600456 fname: Filename to create
Simon Glass4f443042016-11-25 20:15:52 -0700457 contents: File contents to write in to the file
458 Returns:
459 Full pathname of file created
460 """
Simon Glassb986b3b2019-08-24 07:22:43 -0600461 pathname = os.path.join(cls._indir, fname)
Simon Glass4f443042016-11-25 20:15:52 -0700462 dirname = os.path.dirname(pathname)
463 if dirname and not os.path.exists(dirname):
464 os.makedirs(dirname)
465 with open(pathname, 'wb') as fd:
466 fd.write(contents)
467 return pathname
468
469 @classmethod
Simon Glassb986b3b2019-08-24 07:22:43 -0600470 def _MakeInputDir(cls, dirname):
Simon Glass0ef87aa2018-07-17 13:25:44 -0600471 """Create a new test input directory, creating directories as needed
472
473 Args:
474 dirname: Directory name to create
475
476 Returns:
477 Full pathname of directory created
478 """
Simon Glassb986b3b2019-08-24 07:22:43 -0600479 pathname = os.path.join(cls._indir, dirname)
Simon Glass0ef87aa2018-07-17 13:25:44 -0600480 if not os.path.exists(pathname):
481 os.makedirs(pathname)
482 return pathname
483
484 @classmethod
Simon Glassb986b3b2019-08-24 07:22:43 -0600485 def _SetupSplElf(cls, src_fname='bss_data'):
Simon Glass11ae93e2018-10-01 21:12:47 -0600486 """Set up an ELF file with a '_dt_ucode_base_size' symbol
487
488 Args:
489 Filename of ELF file to use as SPL
490 """
Simon Glassc9a0b272019-08-24 07:22:59 -0600491 TestFunctional._MakeInputFile('spl/u-boot-spl',
492 tools.ReadFile(cls.ElfTestFile(src_fname)))
Simon Glass11ae93e2018-10-01 21:12:47 -0600493
494 @classmethod
Simon Glassb986b3b2019-08-24 07:22:43 -0600495 def TestFile(cls, fname):
496 return os.path.join(cls._binman_dir, 'test', fname)
Simon Glass4f443042016-11-25 20:15:52 -0700497
Simon Glass53e22bf2019-08-24 07:22:53 -0600498 @classmethod
499 def ElfTestFile(cls, fname):
500 return os.path.join(cls._elf_testdir, fname)
501
Simon Glass4f443042016-11-25 20:15:52 -0700502 def AssertInList(self, grep_list, target):
503 """Assert that at least one of a list of things is in a target
504
505 Args:
506 grep_list: List of strings to check
507 target: Target string
508 """
509 for grep in grep_list:
510 if grep in target:
511 return
Simon Glass1fc62de2019-05-17 22:00:50 -0600512 self.fail("Error: '%s' not found in '%s'" % (grep_list, target))
Simon Glass4f443042016-11-25 20:15:52 -0700513
514 def CheckNoGaps(self, entries):
515 """Check that all entries fit together without gaps
516
517 Args:
518 entries: List of entries to check
519 """
Simon Glass3ab95982018-08-01 15:22:37 -0600520 offset = 0
Simon Glass4f443042016-11-25 20:15:52 -0700521 for entry in entries.values():
Simon Glass3ab95982018-08-01 15:22:37 -0600522 self.assertEqual(offset, entry.offset)
523 offset += entry.size
Simon Glass4f443042016-11-25 20:15:52 -0700524
Simon Glasse0ff8552016-11-25 20:15:53 -0700525 def GetFdtLen(self, dtb):
Simon Glass7ae5f312018-06-01 09:38:19 -0600526 """Get the totalsize field from a device-tree binary
Simon Glasse0ff8552016-11-25 20:15:53 -0700527
528 Args:
Simon Glass7ae5f312018-06-01 09:38:19 -0600529 dtb: Device-tree binary contents
Simon Glasse0ff8552016-11-25 20:15:53 -0700530
531 Returns:
Simon Glass7ae5f312018-06-01 09:38:19 -0600532 Total size of device-tree binary, from the header
Simon Glasse0ff8552016-11-25 20:15:53 -0700533 """
534 return struct.unpack('>L', dtb[4:8])[0]
535
Simon Glass086cec92019-07-08 14:25:27 -0600536 def _GetPropTree(self, dtb, prop_names, prefix='/binman/'):
Simon Glass16b8d6b2018-07-06 10:27:42 -0600537 def AddNode(node, path):
538 if node.name != '/':
539 path += '/' + node.name
Simon Glass086cec92019-07-08 14:25:27 -0600540 for prop in node.props.values():
541 if prop.name in prop_names:
542 prop_path = path + ':' + prop.name
543 tree[prop_path[len(prefix):]] = fdt_util.fdt32_to_cpu(
544 prop.value)
Simon Glass16b8d6b2018-07-06 10:27:42 -0600545 for subnode in node.subnodes:
Simon Glass16b8d6b2018-07-06 10:27:42 -0600546 AddNode(subnode, path)
547
548 tree = {}
Simon Glass16b8d6b2018-07-06 10:27:42 -0600549 AddNode(dtb.GetRoot(), '')
550 return tree
551
Simon Glass4f443042016-11-25 20:15:52 -0700552 def testRun(self):
553 """Test a basic run with valid args"""
554 result = self._RunBinman('-h')
555
556 def testFullHelp(self):
557 """Test that the full help is displayed with -H"""
558 result = self._RunBinman('-H')
559 help_file = os.path.join(self._binman_dir, 'README')
Tom Rini3759df02018-01-16 15:29:50 -0500560 # Remove possible extraneous strings
561 extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
562 gothelp = result.stdout.replace(extra, '')
563 self.assertEqual(len(gothelp), os.path.getsize(help_file))
Simon Glass4f443042016-11-25 20:15:52 -0700564 self.assertEqual(0, len(result.stderr))
565 self.assertEqual(0, result.return_code)
566
567 def testFullHelpInternal(self):
568 """Test that the full help is displayed with -H"""
569 try:
570 command.test_result = command.CommandResult()
571 result = self._DoBinman('-H')
572 help_file = os.path.join(self._binman_dir, 'README')
573 finally:
574 command.test_result = None
575
576 def testHelp(self):
577 """Test that the basic help is displayed with -h"""
578 result = self._RunBinman('-h')
579 self.assertTrue(len(result.stdout) > 200)
580 self.assertEqual(0, len(result.stderr))
581 self.assertEqual(0, result.return_code)
582
Simon Glass4f443042016-11-25 20:15:52 -0700583 def testBoard(self):
584 """Test that we can run it with a specific board"""
Simon Glass741f2d62018-10-01 12:22:30 -0600585 self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb')
Simon Glass4f443042016-11-25 20:15:52 -0700586 TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
Simon Glass53cd5d92019-07-08 14:25:29 -0600587 result = self._DoBinman('build', '-b', 'sandbox')
Simon Glass4f443042016-11-25 20:15:52 -0700588 self.assertEqual(0, result)
589
590 def testNeedBoard(self):
591 """Test that we get an error when no board ius supplied"""
592 with self.assertRaises(ValueError) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600593 result = self._DoBinman('build')
Simon Glass4f443042016-11-25 20:15:52 -0700594 self.assertIn("Must provide a board to process (use -b <board>)",
595 str(e.exception))
596
597 def testMissingDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600598 """Test that an invalid device-tree file generates an error"""
Simon Glass4f443042016-11-25 20:15:52 -0700599 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600600 self._RunBinman('build', '-d', 'missing_file')
Simon Glass4f443042016-11-25 20:15:52 -0700601 # We get one error from libfdt, and a different one from fdtget.
602 self.AssertInList(["Couldn't open blob from 'missing_file'",
603 'No such file or directory'], str(e.exception))
604
605 def testBrokenDt(self):
Simon Glass7ae5f312018-06-01 09:38:19 -0600606 """Test that an invalid device-tree source file generates an error
Simon Glass4f443042016-11-25 20:15:52 -0700607
608 Since this is a source file it should be compiled and the error
609 will come from the device-tree compiler (dtc).
610 """
611 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600612 self._RunBinman('build', '-d', self.TestFile('001_invalid.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700613 self.assertIn("FATAL ERROR: Unable to parse input tree",
614 str(e.exception))
615
616 def testMissingNode(self):
617 """Test that a device tree without a 'binman' node generates an error"""
618 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600619 self._DoBinman('build', '-d', self.TestFile('002_missing_node.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700620 self.assertIn("does not have a 'binman' node", str(e.exception))
621
622 def testEmpty(self):
623 """Test that an empty binman node works OK (i.e. does nothing)"""
Simon Glass53cd5d92019-07-08 14:25:29 -0600624 result = self._RunBinman('build', '-d', self.TestFile('003_empty.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700625 self.assertEqual(0, len(result.stderr))
626 self.assertEqual(0, result.return_code)
627
628 def testInvalidEntry(self):
629 """Test that an invalid entry is flagged"""
630 with self.assertRaises(Exception) as e:
Simon Glass53cd5d92019-07-08 14:25:29 -0600631 result = self._RunBinman('build', '-d',
Simon Glass741f2d62018-10-01 12:22:30 -0600632 self.TestFile('004_invalid_entry.dts'))
Simon Glass4f443042016-11-25 20:15:52 -0700633 self.assertIn("Unknown entry type 'not-a-valid-type' in node "
634 "'/binman/not-a-valid-type'", str(e.exception))
635
636 def testSimple(self):
637 """Test a simple binman with a single file"""
Simon Glass741f2d62018-10-01 12:22:30 -0600638 data = self._DoReadFile('005_simple.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700639 self.assertEqual(U_BOOT_DATA, data)
640
Simon Glass7fe91732017-11-13 18:55:00 -0700641 def testSimpleDebug(self):
642 """Test a simple binman run with debugging enabled"""
Simon Glasse2705fa2019-07-08 14:25:53 -0600643 self._DoTestFile('005_simple.dts', debug=True)
Simon Glass7fe91732017-11-13 18:55:00 -0700644
Simon Glass4f443042016-11-25 20:15:52 -0700645 def testDual(self):
646 """Test that we can handle creating two images
647
648 This also tests image padding.
649 """
Simon Glass741f2d62018-10-01 12:22:30 -0600650 retcode = self._DoTestFile('006_dual_image.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700651 self.assertEqual(0, retcode)
652
653 image = control.images['image1']
Simon Glass8beb11e2019-07-08 14:25:47 -0600654 self.assertEqual(len(U_BOOT_DATA), image.size)
Simon Glass4f443042016-11-25 20:15:52 -0700655 fname = tools.GetOutputFilename('image1.bin')
656 self.assertTrue(os.path.exists(fname))
Simon Glass1d0ebf72019-05-14 15:53:42 -0600657 with open(fname, 'rb') as fd:
Simon Glass4f443042016-11-25 20:15:52 -0700658 data = fd.read()
659 self.assertEqual(U_BOOT_DATA, data)
660
661 image = control.images['image2']
Simon Glass8beb11e2019-07-08 14:25:47 -0600662 self.assertEqual(3 + len(U_BOOT_DATA) + 5, image.size)
Simon Glass4f443042016-11-25 20:15:52 -0700663 fname = tools.GetOutputFilename('image2.bin')
664 self.assertTrue(os.path.exists(fname))
Simon Glass1d0ebf72019-05-14 15:53:42 -0600665 with open(fname, 'rb') as fd:
Simon Glass4f443042016-11-25 20:15:52 -0700666 data = fd.read()
667 self.assertEqual(U_BOOT_DATA, data[3:7])
Simon Glasse6d85ff2019-05-14 15:53:47 -0600668 self.assertEqual(tools.GetBytes(0, 3), data[:3])
669 self.assertEqual(tools.GetBytes(0, 5), data[7:])
Simon Glass4f443042016-11-25 20:15:52 -0700670
671 def testBadAlign(self):
672 """Test that an invalid alignment value is detected"""
673 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600674 self._DoTestFile('007_bad_align.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700675 self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
676 "of two", str(e.exception))
677
678 def testPackSimple(self):
679 """Test that packing works as expected"""
Simon Glass741f2d62018-10-01 12:22:30 -0600680 retcode = self._DoTestFile('008_pack.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700681 self.assertEqual(0, retcode)
682 self.assertIn('image', control.images)
683 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600684 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700685 self.assertEqual(5, len(entries))
686
687 # First u-boot
688 self.assertIn('u-boot', entries)
689 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600690 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700691 self.assertEqual(len(U_BOOT_DATA), entry.size)
692
693 # Second u-boot, aligned to 16-byte boundary
694 self.assertIn('u-boot-align', entries)
695 entry = entries['u-boot-align']
Simon Glass3ab95982018-08-01 15:22:37 -0600696 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700697 self.assertEqual(len(U_BOOT_DATA), entry.size)
698
699 # Third u-boot, size 23 bytes
700 self.assertIn('u-boot-size', entries)
701 entry = entries['u-boot-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600702 self.assertEqual(20, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700703 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
704 self.assertEqual(23, entry.size)
705
706 # Fourth u-boot, placed immediate after the above
707 self.assertIn('u-boot-next', entries)
708 entry = entries['u-boot-next']
Simon Glass3ab95982018-08-01 15:22:37 -0600709 self.assertEqual(43, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700710 self.assertEqual(len(U_BOOT_DATA), entry.size)
711
Simon Glass3ab95982018-08-01 15:22:37 -0600712 # Fifth u-boot, placed at a fixed offset
Simon Glass4f443042016-11-25 20:15:52 -0700713 self.assertIn('u-boot-fixed', entries)
714 entry = entries['u-boot-fixed']
Simon Glass3ab95982018-08-01 15:22:37 -0600715 self.assertEqual(61, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700716 self.assertEqual(len(U_BOOT_DATA), entry.size)
717
Simon Glass8beb11e2019-07-08 14:25:47 -0600718 self.assertEqual(65, image.size)
Simon Glass4f443042016-11-25 20:15:52 -0700719
720 def testPackExtra(self):
721 """Test that extra packing feature works as expected"""
Simon Glass741f2d62018-10-01 12:22:30 -0600722 retcode = self._DoTestFile('009_pack_extra.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700723
724 self.assertEqual(0, retcode)
725 self.assertIn('image', control.images)
726 image = control.images['image']
Simon Glass8f1da502018-06-01 09:38:12 -0600727 entries = image.GetEntries()
Simon Glass4f443042016-11-25 20:15:52 -0700728 self.assertEqual(5, len(entries))
729
730 # First u-boot with padding before and after
731 self.assertIn('u-boot', entries)
732 entry = entries['u-boot']
Simon Glass3ab95982018-08-01 15:22:37 -0600733 self.assertEqual(0, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700734 self.assertEqual(3, entry.pad_before)
735 self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
736
737 # Second u-boot has an aligned size, but it has no effect
738 self.assertIn('u-boot-align-size-nop', entries)
739 entry = entries['u-boot-align-size-nop']
Simon Glass3ab95982018-08-01 15:22:37 -0600740 self.assertEqual(12, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700741 self.assertEqual(4, entry.size)
742
743 # Third u-boot has an aligned size too
744 self.assertIn('u-boot-align-size', entries)
745 entry = entries['u-boot-align-size']
Simon Glass3ab95982018-08-01 15:22:37 -0600746 self.assertEqual(16, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700747 self.assertEqual(32, entry.size)
748
749 # Fourth u-boot has an aligned end
750 self.assertIn('u-boot-align-end', entries)
751 entry = entries['u-boot-align-end']
Simon Glass3ab95982018-08-01 15:22:37 -0600752 self.assertEqual(48, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700753 self.assertEqual(16, entry.size)
754
755 # Fifth u-boot immediately afterwards
756 self.assertIn('u-boot-align-both', entries)
757 entry = entries['u-boot-align-both']
Simon Glass3ab95982018-08-01 15:22:37 -0600758 self.assertEqual(64, entry.offset)
Simon Glass4f443042016-11-25 20:15:52 -0700759 self.assertEqual(64, entry.size)
760
761 self.CheckNoGaps(entries)
Simon Glass8beb11e2019-07-08 14:25:47 -0600762 self.assertEqual(128, image.size)
Simon Glass4f443042016-11-25 20:15:52 -0700763
764 def testPackAlignPowerOf2(self):
765 """Test that invalid entry alignment is detected"""
766 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600767 self._DoTestFile('010_pack_align_power2.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700768 self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
769 "of two", str(e.exception))
770
771 def testPackAlignSizePowerOf2(self):
772 """Test that invalid entry size alignment is detected"""
773 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600774 self._DoTestFile('011_pack_align_size_power2.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700775 self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
776 "power of two", str(e.exception))
777
778 def testPackInvalidAlign(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600779 """Test detection of an offset that does not match its alignment"""
Simon Glass4f443042016-11-25 20:15:52 -0700780 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600781 self._DoTestFile('012_pack_inv_align.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600782 self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700783 "align 0x4 (4)", str(e.exception))
784
785 def testPackInvalidSizeAlign(self):
786 """Test that invalid entry size alignment is detected"""
787 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600788 self._DoTestFile('013_pack_inv_size_align.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700789 self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
790 "align-size 0x4 (4)", str(e.exception))
791
792 def testPackOverlap(self):
793 """Test that overlapping regions are detected"""
794 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600795 self._DoTestFile('014_pack_overlap.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600796 self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700797 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
798 str(e.exception))
799
800 def testPackEntryOverflow(self):
801 """Test that entries that overflow their size are detected"""
802 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600803 self._DoTestFile('015_pack_overflow.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700804 self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
805 "but entry size is 0x3 (3)", str(e.exception))
806
807 def testPackImageOverflow(self):
808 """Test that entries which overflow the image size are detected"""
809 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600810 self._DoTestFile('016_pack_image_overflow.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600811 self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
Simon Glass4f443042016-11-25 20:15:52 -0700812 "size 0x3 (3)", str(e.exception))
813
814 def testPackImageSize(self):
815 """Test that the image size can be set"""
Simon Glass741f2d62018-10-01 12:22:30 -0600816 retcode = self._DoTestFile('017_pack_image_size.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700817 self.assertEqual(0, retcode)
818 self.assertIn('image', control.images)
819 image = control.images['image']
Simon Glass8beb11e2019-07-08 14:25:47 -0600820 self.assertEqual(7, image.size)
Simon Glass4f443042016-11-25 20:15:52 -0700821
822 def testPackImageSizeAlign(self):
823 """Test that image size alignemnt works as expected"""
Simon Glass741f2d62018-10-01 12:22:30 -0600824 retcode = self._DoTestFile('018_pack_image_align.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700825 self.assertEqual(0, retcode)
826 self.assertIn('image', control.images)
827 image = control.images['image']
Simon Glass8beb11e2019-07-08 14:25:47 -0600828 self.assertEqual(16, image.size)
Simon Glass4f443042016-11-25 20:15:52 -0700829
830 def testPackInvalidImageAlign(self):
831 """Test that invalid image alignment is detected"""
832 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600833 self._DoTestFile('019_pack_inv_image_align.dts')
Simon Glass8f1da502018-06-01 09:38:12 -0600834 self.assertIn("Section '/binman': Size 0x7 (7) does not match "
Simon Glass4f443042016-11-25 20:15:52 -0700835 "align-size 0x8 (8)", str(e.exception))
836
837 def testPackAlignPowerOf2(self):
838 """Test that invalid image alignment is detected"""
839 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600840 self._DoTestFile('020_pack_inv_image_align_power2.dts')
Simon Glass8beb11e2019-07-08 14:25:47 -0600841 self.assertIn("Image '/binman': Alignment size 131 must be a power of "
Simon Glass4f443042016-11-25 20:15:52 -0700842 "two", str(e.exception))
843
844 def testImagePadByte(self):
845 """Test that the image pad byte can be specified"""
Simon Glass11ae93e2018-10-01 21:12:47 -0600846 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -0600847 data = self._DoReadFile('021_image_pad.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -0600848 self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) +
849 U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700850
851 def testImageName(self):
852 """Test that image files can be named"""
Simon Glass741f2d62018-10-01 12:22:30 -0600853 retcode = self._DoTestFile('022_image_name.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700854 self.assertEqual(0, retcode)
855 image = control.images['image1']
856 fname = tools.GetOutputFilename('test-name')
857 self.assertTrue(os.path.exists(fname))
858
859 image = control.images['image2']
860 fname = tools.GetOutputFilename('test-name.xx')
861 self.assertTrue(os.path.exists(fname))
862
863 def testBlobFilename(self):
864 """Test that generic blobs can be provided by filename"""
Simon Glass741f2d62018-10-01 12:22:30 -0600865 data = self._DoReadFile('023_blob.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700866 self.assertEqual(BLOB_DATA, data)
867
868 def testPackSorted(self):
869 """Test that entries can be sorted"""
Simon Glass11ae93e2018-10-01 21:12:47 -0600870 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -0600871 data = self._DoReadFile('024_sorted.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -0600872 self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA +
873 tools.GetBytes(0, 2) + U_BOOT_DATA, data)
Simon Glass4f443042016-11-25 20:15:52 -0700874
Simon Glass3ab95982018-08-01 15:22:37 -0600875 def testPackZeroOffset(self):
876 """Test that an entry at offset 0 is not given a new offset"""
Simon Glass4f443042016-11-25 20:15:52 -0700877 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600878 self._DoTestFile('025_pack_zero_size.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600879 self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
Simon Glass4f443042016-11-25 20:15:52 -0700880 "with previous entry '/binman/u-boot' ending at 0x4 (4)",
881 str(e.exception))
882
883 def testPackUbootDtb(self):
884 """Test that a device tree can be added to U-Boot"""
Simon Glass741f2d62018-10-01 12:22:30 -0600885 data = self._DoReadFile('026_pack_u_boot_dtb.dts')
Simon Glass4f443042016-11-25 20:15:52 -0700886 self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700887
888 def testPackX86RomNoSize(self):
889 """Test that the end-at-4gb property requires a size property"""
890 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600891 self._DoTestFile('027_pack_4gb_no_size.dts')
Simon Glass8beb11e2019-07-08 14:25:47 -0600892 self.assertIn("Image '/binman': Section size must be provided when "
Simon Glasse0ff8552016-11-25 20:15:53 -0700893 "using end-at-4gb", str(e.exception))
894
Jagdish Gediya94b57db2018-09-03 21:35:07 +0530895 def test4gbAndSkipAtStartTogether(self):
896 """Test that the end-at-4gb and skip-at-size property can't be used
897 together"""
898 with self.assertRaises(ValueError) as e:
899 self._DoTestFile('80_4gb_and_skip_at_start_together.dts')
Simon Glass8beb11e2019-07-08 14:25:47 -0600900 self.assertIn("Image '/binman': Provide either 'end-at-4gb' or "
Jagdish Gediya94b57db2018-09-03 21:35:07 +0530901 "'skip-at-start'", str(e.exception))
902
Simon Glasse0ff8552016-11-25 20:15:53 -0700903 def testPackX86RomOutside(self):
Simon Glass3ab95982018-08-01 15:22:37 -0600904 """Test that the end-at-4gb property checks for offset boundaries"""
Simon Glasse0ff8552016-11-25 20:15:53 -0700905 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600906 self._DoTestFile('028_pack_4gb_outside.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600907 self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
Simon Glass8f1da502018-06-01 09:38:12 -0600908 "the section starting at 0xffffffe0 (4294967264)",
Simon Glasse0ff8552016-11-25 20:15:53 -0700909 str(e.exception))
910
911 def testPackX86Rom(self):
912 """Test that a basic x86 ROM can be created"""
Simon Glass11ae93e2018-10-01 21:12:47 -0600913 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -0600914 data = self._DoReadFile('029_x86-rom.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -0600915 self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 7) + U_BOOT_SPL_DATA +
916 tools.GetBytes(0, 2), data)
Simon Glasse0ff8552016-11-25 20:15:53 -0700917
918 def testPackX86RomMeNoDesc(self):
919 """Test that an invalid Intel descriptor entry is detected"""
Simon Glassc6c10e72019-05-17 22:00:46 -0600920 TestFunctional._MakeInputFile('descriptor.bin', b'')
Simon Glasse0ff8552016-11-25 20:15:53 -0700921 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600922 self._DoTestFile('031_x86-rom-me.dts')
Simon Glass458be452019-07-08 13:18:32 -0600923 self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature",
924 str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700925
926 def testPackX86RomBadDesc(self):
927 """Test that the Intel requires a descriptor entry"""
928 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -0600929 self._DoTestFile('030_x86-rom-me-no-desc.dts')
Simon Glass3ab95982018-08-01 15:22:37 -0600930 self.assertIn("Node '/binman/intel-me': No offset set with "
931 "offset-unset: should another entry provide this correct "
932 "offset?", str(e.exception))
Simon Glasse0ff8552016-11-25 20:15:53 -0700933
934 def testPackX86RomMe(self):
935 """Test that an x86 ROM with an ME region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -0600936 data = self._DoReadFile('031_x86-rom-me.dts')
Simon Glassc5ac1382019-07-08 13:18:54 -0600937 expected_desc = tools.ReadFile(self.TestFile('descriptor.bin'))
938 if data[:0x1000] != expected_desc:
939 self.fail('Expected descriptor binary at start of image')
Simon Glasse0ff8552016-11-25 20:15:53 -0700940 self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
941
942 def testPackVga(self):
943 """Test that an image with a VGA binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -0600944 data = self._DoReadFile('032_intel-vga.dts')
Simon Glasse0ff8552016-11-25 20:15:53 -0700945 self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
946
947 def testPackStart16(self):
948 """Test that an image with an x86 start16 region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -0600949 data = self._DoReadFile('033_x86-start16.dts')
Simon Glasse0ff8552016-11-25 20:15:53 -0700950 self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
951
Jagdish Gediya9d368f32018-09-03 21:35:08 +0530952 def testPackPowerpcMpc85xxBootpgResetvec(self):
953 """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be
954 created"""
955 data = self._DoReadFile('81_powerpc_mpc85xx_bootpg_resetvec.dts')
956 self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)])
957
Simon Glass736bb0a2018-07-06 10:27:17 -0600958 def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
Simon Glassadc57012018-07-06 10:27:16 -0600959 """Handle running a test for insertion of microcode
960
961 Args:
962 dts_fname: Name of test .dts file
963 nodtb_data: Data that we expect in the first section
Simon Glass736bb0a2018-07-06 10:27:17 -0600964 ucode_second: True if the microsecond entry is second instead of
965 third
Simon Glassadc57012018-07-06 10:27:16 -0600966
967 Returns:
968 Tuple:
969 Contents of first region (U-Boot or SPL)
Simon Glass3ab95982018-08-01 15:22:37 -0600970 Offset and size components of microcode pointer, as inserted
Simon Glassadc57012018-07-06 10:27:16 -0600971 in the above (two 4-byte words)
972 """
Simon Glass6b187df2017-11-12 21:52:27 -0700973 data = self._DoReadFile(dts_fname, True)
Simon Glasse0ff8552016-11-25 20:15:53 -0700974
975 # Now check the device tree has no microcode
Simon Glass736bb0a2018-07-06 10:27:17 -0600976 if ucode_second:
977 ucode_content = data[len(nodtb_data):]
978 ucode_pos = len(nodtb_data)
979 dtb_with_ucode = ucode_content[16:]
980 fdt_len = self.GetFdtLen(dtb_with_ucode)
981 else:
982 dtb_with_ucode = data[len(nodtb_data):]
983 fdt_len = self.GetFdtLen(dtb_with_ucode)
984 ucode_content = dtb_with_ucode[fdt_len:]
985 ucode_pos = len(nodtb_data) + fdt_len
Simon Glasse0ff8552016-11-25 20:15:53 -0700986 fname = tools.GetOutputFilename('test.dtb')
987 with open(fname, 'wb') as fd:
Simon Glassadc57012018-07-06 10:27:16 -0600988 fd.write(dtb_with_ucode)
Simon Glassec3f3782017-05-27 07:38:29 -0600989 dtb = fdt.FdtScan(fname)
990 ucode = dtb.GetNode('/microcode')
Simon Glasse0ff8552016-11-25 20:15:53 -0700991 self.assertTrue(ucode)
992 for node in ucode.subnodes:
993 self.assertFalse(node.props.get('data'))
994
Simon Glasse0ff8552016-11-25 20:15:53 -0700995 # Check that the microcode appears immediately after the Fdt
996 # This matches the concatenation of the data properties in
Simon Glass87722132017-11-12 21:52:26 -0700997 # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
Simon Glasse0ff8552016-11-25 20:15:53 -0700998 ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
999 0x78235609)
Simon Glassadc57012018-07-06 10:27:16 -06001000 self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
Simon Glasse0ff8552016-11-25 20:15:53 -07001001
1002 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -06001003 # expected offset and size
Simon Glasse0ff8552016-11-25 20:15:53 -07001004 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
1005 len(ucode_data))
Simon Glass736bb0a2018-07-06 10:27:17 -06001006 u_boot = data[:len(nodtb_data)]
1007 return u_boot, pos_and_size
Simon Glass6b187df2017-11-12 21:52:27 -07001008
1009 def testPackUbootMicrocode(self):
1010 """Test that x86 microcode can be handled correctly
1011
1012 We expect to see the following in the image, in order:
1013 u-boot-nodtb.bin with a microcode pointer inserted at the correct
1014 place
1015 u-boot.dtb with the microcode removed
1016 the microcode
1017 """
Simon Glass741f2d62018-10-01 12:22:30 -06001018 first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts',
Simon Glass6b187df2017-11-12 21:52:27 -07001019 U_BOOT_NODTB_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -06001020 self.assertEqual(b'nodtb with microcode' + pos_and_size +
1021 b' somewhere in here', first)
Simon Glasse0ff8552016-11-25 20:15:53 -07001022
Simon Glass160a7662017-05-27 07:38:26 -06001023 def _RunPackUbootSingleMicrocode(self):
Simon Glasse0ff8552016-11-25 20:15:53 -07001024 """Test that x86 microcode can be handled correctly
1025
1026 We expect to see the following in the image, in order:
1027 u-boot-nodtb.bin with a microcode pointer inserted at the correct
1028 place
1029 u-boot.dtb with the microcode
1030 an empty microcode region
1031 """
1032 # We need the libfdt library to run this test since only that allows
1033 # finding the offset of a property. This is required by
1034 # Entry_u_boot_dtb_with_ucode.ObtainContents().
Simon Glass741f2d62018-10-01 12:22:30 -06001035 data = self._DoReadFile('035_x86_single_ucode.dts', True)
Simon Glasse0ff8552016-11-25 20:15:53 -07001036
1037 second = data[len(U_BOOT_NODTB_DATA):]
1038
1039 fdt_len = self.GetFdtLen(second)
1040 third = second[fdt_len:]
1041 second = second[:fdt_len]
1042
Simon Glass160a7662017-05-27 07:38:26 -06001043 ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
1044 self.assertIn(ucode_data, second)
1045 ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
Simon Glasse0ff8552016-11-25 20:15:53 -07001046
Simon Glass160a7662017-05-27 07:38:26 -06001047 # Check that the microcode pointer was inserted. It should match the
Simon Glass3ab95982018-08-01 15:22:37 -06001048 # expected offset and size
Simon Glass160a7662017-05-27 07:38:26 -06001049 pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
1050 len(ucode_data))
1051 first = data[:len(U_BOOT_NODTB_DATA)]
Simon Glassc6c10e72019-05-17 22:00:46 -06001052 self.assertEqual(b'nodtb with microcode' + pos_and_size +
1053 b' somewhere in here', first)
Simon Glassc49deb82016-11-25 20:15:54 -07001054
Simon Glass75db0862016-11-25 20:15:55 -07001055 def testPackUbootSingleMicrocode(self):
1056 """Test that x86 microcode can be handled correctly with fdt_normal.
1057 """
Simon Glass160a7662017-05-27 07:38:26 -06001058 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -07001059
Simon Glassc49deb82016-11-25 20:15:54 -07001060 def testUBootImg(self):
1061 """Test that u-boot.img can be put in a file"""
Simon Glass741f2d62018-10-01 12:22:30 -06001062 data = self._DoReadFile('036_u_boot_img.dts')
Simon Glassc49deb82016-11-25 20:15:54 -07001063 self.assertEqual(U_BOOT_IMG_DATA, data)
Simon Glass75db0862016-11-25 20:15:55 -07001064
1065 def testNoMicrocode(self):
1066 """Test that a missing microcode region is detected"""
1067 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001068 self._DoReadFile('037_x86_no_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001069 self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
1070 "node found in ", str(e.exception))
1071
1072 def testMicrocodeWithoutNode(self):
1073 """Test that a missing u-boot-dtb-with-ucode node is detected"""
1074 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001075 self._DoReadFile('038_x86_ucode_missing_node.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001076 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
1077 "microcode region u-boot-dtb-with-ucode", str(e.exception))
1078
1079 def testMicrocodeWithoutNode2(self):
1080 """Test that a missing u-boot-ucode node is detected"""
1081 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001082 self._DoReadFile('039_x86_ucode_missing_node2.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001083 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
1084 "microcode region u-boot-ucode", str(e.exception))
1085
1086 def testMicrocodeWithoutPtrInElf(self):
1087 """Test that a U-Boot binary without the microcode symbol is detected"""
1088 # ELF file without a '_dt_ucode_base_size' symbol
Simon Glass75db0862016-11-25 20:15:55 -07001089 try:
Simon Glassbccd91d2019-08-24 07:22:55 -06001090 TestFunctional._MakeInputFile('u-boot',
1091 tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr')))
Simon Glass75db0862016-11-25 20:15:55 -07001092
1093 with self.assertRaises(ValueError) as e:
Simon Glass160a7662017-05-27 07:38:26 -06001094 self._RunPackUbootSingleMicrocode()
Simon Glass75db0862016-11-25 20:15:55 -07001095 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
1096 "_dt_ucode_base_size symbol in u-boot", str(e.exception))
1097
1098 finally:
1099 # Put the original file back
Simon Glassf514d8f2019-08-24 07:22:54 -06001100 TestFunctional._MakeInputFile('u-boot',
1101 tools.ReadFile(self.ElfTestFile('u_boot_ucode_ptr')))
Simon Glass75db0862016-11-25 20:15:55 -07001102
1103 def testMicrocodeNotInImage(self):
1104 """Test that microcode must be placed within the image"""
1105 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001106 self._DoReadFile('040_x86_ucode_not_in_image.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001107 self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
1108 "pointer _dt_ucode_base_size at fffffe14 is outside the "
Simon Glass25ac0e62018-06-01 09:38:14 -06001109 "section ranging from 00000000 to 0000002e", str(e.exception))
Simon Glass75db0862016-11-25 20:15:55 -07001110
1111 def testWithoutMicrocode(self):
1112 """Test that we can cope with an image without microcode (e.g. qemu)"""
Simon Glassbccd91d2019-08-24 07:22:55 -06001113 TestFunctional._MakeInputFile('u-boot',
1114 tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr')))
Simon Glass741f2d62018-10-01 12:22:30 -06001115 data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True)
Simon Glass75db0862016-11-25 20:15:55 -07001116
1117 # Now check the device tree has no microcode
1118 self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
1119 second = data[len(U_BOOT_NODTB_DATA):]
1120
1121 fdt_len = self.GetFdtLen(second)
1122 self.assertEqual(dtb, second[:fdt_len])
1123
1124 used_len = len(U_BOOT_NODTB_DATA) + fdt_len
1125 third = data[used_len:]
Simon Glasse6d85ff2019-05-14 15:53:47 -06001126 self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third)
Simon Glass75db0862016-11-25 20:15:55 -07001127
1128 def testUnknownPosSize(self):
1129 """Test that microcode must be placed within the image"""
1130 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001131 self._DoReadFile('041_unknown_pos_size.dts', True)
Simon Glass3ab95982018-08-01 15:22:37 -06001132 self.assertIn("Section '/binman': Unable to set offset/size for unknown "
Simon Glass75db0862016-11-25 20:15:55 -07001133 "entry 'invalid-entry'", str(e.exception))
Simon Glassda229092016-11-25 20:15:56 -07001134
1135 def testPackFsp(self):
1136 """Test that an image with a FSP binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001137 data = self._DoReadFile('042_intel-fsp.dts')
Simon Glassda229092016-11-25 20:15:56 -07001138 self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
1139
1140 def testPackCmc(self):
Bin Meng59ea8c22017-08-15 22:41:54 -07001141 """Test that an image with a CMC binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001142 data = self._DoReadFile('043_intel-cmc.dts')
Simon Glassda229092016-11-25 20:15:56 -07001143 self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
Bin Meng59ea8c22017-08-15 22:41:54 -07001144
1145 def testPackVbt(self):
1146 """Test that an image with a VBT binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001147 data = self._DoReadFile('046_intel-vbt.dts')
Bin Meng59ea8c22017-08-15 22:41:54 -07001148 self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
Simon Glass9fc60b42017-11-12 21:52:22 -07001149
Simon Glass56509842017-11-12 21:52:25 -07001150 def testSplBssPad(self):
1151 """Test that we can pad SPL's BSS with zeros"""
Simon Glass6b187df2017-11-12 21:52:27 -07001152 # ELF file with a '__bss_size' symbol
Simon Glass11ae93e2018-10-01 21:12:47 -06001153 self._SetupSplElf()
Simon Glass741f2d62018-10-01 12:22:30 -06001154 data = self._DoReadFile('047_spl_bss_pad.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -06001155 self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA,
1156 data)
Simon Glass56509842017-11-12 21:52:25 -07001157
Simon Glass86af5112018-10-01 21:12:42 -06001158 def testSplBssPadMissing(self):
1159 """Test that a missing symbol is detected"""
Simon Glass11ae93e2018-10-01 21:12:47 -06001160 self._SetupSplElf('u_boot_ucode_ptr')
Simon Glassb50e5612017-11-13 18:54:54 -07001161 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001162 self._DoReadFile('047_spl_bss_pad.dts')
Simon Glassb50e5612017-11-13 18:54:54 -07001163 self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
1164 str(e.exception))
1165
Simon Glass87722132017-11-12 21:52:26 -07001166 def testPackStart16Spl(self):
Simon Glass35b384c2018-09-14 04:57:10 -06001167 """Test that an image with an x86 start16 SPL region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001168 data = self._DoReadFile('048_x86-start16-spl.dts')
Simon Glass87722132017-11-12 21:52:26 -07001169 self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
1170
Simon Glass736bb0a2018-07-06 10:27:17 -06001171 def _PackUbootSplMicrocode(self, dts, ucode_second=False):
1172 """Helper function for microcode tests
Simon Glass6b187df2017-11-12 21:52:27 -07001173
1174 We expect to see the following in the image, in order:
1175 u-boot-spl-nodtb.bin with a microcode pointer inserted at the
1176 correct place
1177 u-boot.dtb with the microcode removed
1178 the microcode
Simon Glass736bb0a2018-07-06 10:27:17 -06001179
1180 Args:
1181 dts: Device tree file to use for test
1182 ucode_second: True if the microsecond entry is second instead of
1183 third
Simon Glass6b187df2017-11-12 21:52:27 -07001184 """
Simon Glass11ae93e2018-10-01 21:12:47 -06001185 self._SetupSplElf('u_boot_ucode_ptr')
Simon Glass736bb0a2018-07-06 10:27:17 -06001186 first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
1187 ucode_second=ucode_second)
Simon Glassc6c10e72019-05-17 22:00:46 -06001188 self.assertEqual(b'splnodtb with microc' + pos_and_size +
1189 b'ter somewhere in here', first)
Simon Glass6b187df2017-11-12 21:52:27 -07001190
Simon Glass736bb0a2018-07-06 10:27:17 -06001191 def testPackUbootSplMicrocode(self):
1192 """Test that x86 microcode can be handled correctly in SPL"""
Simon Glass741f2d62018-10-01 12:22:30 -06001193 self._PackUbootSplMicrocode('049_x86_ucode_spl.dts')
Simon Glass736bb0a2018-07-06 10:27:17 -06001194
1195 def testPackUbootSplMicrocodeReorder(self):
1196 """Test that order doesn't matter for microcode entries
1197
1198 This is the same as testPackUbootSplMicrocode but when we process the
1199 u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
1200 entry, so we reply on binman to try later.
1201 """
Simon Glass741f2d62018-10-01 12:22:30 -06001202 self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts',
Simon Glass736bb0a2018-07-06 10:27:17 -06001203 ucode_second=True)
1204
Simon Glassca4f4ff2017-11-12 21:52:28 -07001205 def testPackMrc(self):
1206 """Test that an image with an MRC binary can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001207 data = self._DoReadFile('050_intel_mrc.dts')
Simon Glassca4f4ff2017-11-12 21:52:28 -07001208 self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
1209
Simon Glass47419ea2017-11-13 18:54:55 -07001210 def testSplDtb(self):
1211 """Test that an image with spl/u-boot-spl.dtb can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001212 data = self._DoReadFile('051_u_boot_spl_dtb.dts')
Simon Glass47419ea2017-11-13 18:54:55 -07001213 self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
1214
Simon Glass4e6fdbe2017-11-13 18:54:56 -07001215 def testSplNoDtb(self):
1216 """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001217 data = self._DoReadFile('052_u_boot_spl_nodtb.dts')
Simon Glass4e6fdbe2017-11-13 18:54:56 -07001218 self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
1219
Simon Glass19790632017-11-13 18:55:01 -07001220 def testSymbols(self):
1221 """Test binman can assign symbols embedded in U-Boot"""
Simon Glass1542c8b2019-08-24 07:22:56 -06001222 elf_fname = self.ElfTestFile('u_boot_binman_syms')
Simon Glass19790632017-11-13 18:55:01 -07001223 syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
1224 addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
Simon Glass3ab95982018-08-01 15:22:37 -06001225 self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
Simon Glass19790632017-11-13 18:55:01 -07001226
Simon Glass11ae93e2018-10-01 21:12:47 -06001227 self._SetupSplElf('u_boot_binman_syms')
Simon Glass741f2d62018-10-01 12:22:30 -06001228 data = self._DoReadFile('053_symbols.dts')
Simon Glass1542c8b2019-08-24 07:22:56 -06001229 sym_values = struct.pack('<LQL', 0, 24, 20)
Simon Glasse6d85ff2019-05-14 15:53:47 -06001230 expected = (sym_values + U_BOOT_SPL_DATA[16:] +
1231 tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values +
1232 U_BOOT_SPL_DATA[16:])
Simon Glass19790632017-11-13 18:55:01 -07001233 self.assertEqual(expected, data)
1234
Simon Glassdd57c132018-06-01 09:38:11 -06001235 def testPackUnitAddress(self):
1236 """Test that we support multiple binaries with the same name"""
Simon Glass741f2d62018-10-01 12:22:30 -06001237 data = self._DoReadFile('054_unit_address.dts')
Simon Glassdd57c132018-06-01 09:38:11 -06001238 self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
1239
Simon Glass18546952018-06-01 09:38:16 -06001240 def testSections(self):
1241 """Basic test of sections"""
Simon Glass741f2d62018-10-01 12:22:30 -06001242 data = self._DoReadFile('055_sections.dts')
Simon Glassc6c10e72019-05-17 22:00:46 -06001243 expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
1244 U_BOOT_DATA + tools.GetBytes(ord('a'), 12) +
1245 U_BOOT_DATA + tools.GetBytes(ord('&'), 4))
Simon Glass18546952018-06-01 09:38:16 -06001246 self.assertEqual(expected, data)
Simon Glass9fc60b42017-11-12 21:52:22 -07001247
Simon Glass3b0c3822018-06-01 09:38:20 -06001248 def testMap(self):
1249 """Tests outputting a map of the images"""
Simon Glass741f2d62018-10-01 12:22:30 -06001250 _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001251 self.assertEqual('''ImagePos Offset Size Name
125200000000 00000000 00000028 main-section
125300000000 00000000 00000010 section@0
125400000000 00000000 00000004 u-boot
125500000010 00000010 00000010 section@1
125600000010 00000000 00000004 u-boot
125700000020 00000020 00000004 section@2
125800000020 00000000 00000004 u-boot
Simon Glass3b0c3822018-06-01 09:38:20 -06001259''', map_data)
1260
Simon Glassc8d48ef2018-06-01 09:38:21 -06001261 def testNamePrefix(self):
1262 """Tests that name prefixes are used"""
Simon Glass741f2d62018-10-01 12:22:30 -06001263 _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True)
Simon Glass1be70d22018-07-17 13:25:49 -06001264 self.assertEqual('''ImagePos Offset Size Name
126500000000 00000000 00000028 main-section
126600000000 00000000 00000010 section@0
126700000000 00000000 00000004 ro-u-boot
126800000010 00000010 00000010 section@1
126900000010 00000000 00000004 rw-u-boot
Simon Glassc8d48ef2018-06-01 09:38:21 -06001270''', map_data)
1271
Simon Glass736bb0a2018-07-06 10:27:17 -06001272 def testUnknownContents(self):
1273 """Test that obtaining the contents works as expected"""
1274 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001275 self._DoReadFile('057_unknown_contents.dts', True)
Simon Glass8beb11e2019-07-08 14:25:47 -06001276 self.assertIn("Image '/binman': Internal error: Could not complete "
Simon Glass736bb0a2018-07-06 10:27:17 -06001277 "processing of contents: remaining [<_testing.Entry__testing ",
1278 str(e.exception))
1279
Simon Glass5c890232018-07-06 10:27:19 -06001280 def testBadChangeSize(self):
1281 """Test that trying to change the size of an entry fails"""
Simon Glassc52c9e72019-07-08 14:25:37 -06001282 try:
1283 state.SetAllowEntryExpansion(False)
1284 with self.assertRaises(ValueError) as e:
1285 self._DoReadFile('059_change_size.dts', True)
Simon Glass79d3c582019-07-20 12:23:57 -06001286 self.assertIn("Node '/binman/_testing': Cannot update entry size from 2 to 3",
Simon Glassc52c9e72019-07-08 14:25:37 -06001287 str(e.exception))
1288 finally:
1289 state.SetAllowEntryExpansion(True)
Simon Glass5c890232018-07-06 10:27:19 -06001290
Simon Glass16b8d6b2018-07-06 10:27:42 -06001291 def testUpdateFdt(self):
Simon Glass3ab95982018-08-01 15:22:37 -06001292 """Test that we can update the device tree with offset/size info"""
Simon Glass741f2d62018-10-01 12:22:30 -06001293 _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts',
Simon Glass16b8d6b2018-07-06 10:27:42 -06001294 update_dtb=True)
Simon Glasscee02e62018-07-17 13:25:52 -06001295 dtb = fdt.Fdt(out_dtb_fname)
1296 dtb.Scan()
Simon Glass12bb1a92019-07-20 12:23:51 -06001297 props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS)
Simon Glass16b8d6b2018-07-06 10:27:42 -06001298 self.assertEqual({
Simon Glassdbf6be92018-08-01 15:22:42 -06001299 'image-pos': 0,
Simon Glass8122f392018-07-17 13:25:28 -06001300 'offset': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001301 '_testing:offset': 32,
Simon Glass79d3c582019-07-20 12:23:57 -06001302 '_testing:size': 2,
Simon Glassdbf6be92018-08-01 15:22:42 -06001303 '_testing:image-pos': 32,
Simon Glass3ab95982018-08-01 15:22:37 -06001304 'section@0/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001305 'section@0/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001306 'section@0/u-boot:image-pos': 0,
Simon Glass3ab95982018-08-01 15:22:37 -06001307 'section@0:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001308 'section@0:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001309 'section@0:image-pos': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001310
Simon Glass3ab95982018-08-01 15:22:37 -06001311 'section@1/u-boot:offset': 0,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001312 'section@1/u-boot:size': len(U_BOOT_DATA),
Simon Glassdbf6be92018-08-01 15:22:42 -06001313 'section@1/u-boot:image-pos': 16,
Simon Glass3ab95982018-08-01 15:22:37 -06001314 'section@1:offset': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001315 'section@1:size': 16,
Simon Glassdbf6be92018-08-01 15:22:42 -06001316 'section@1:image-pos': 16,
Simon Glass16b8d6b2018-07-06 10:27:42 -06001317 'size': 40
1318 }, props)
1319
1320 def testUpdateFdtBad(self):
1321 """Test that we detect when ProcessFdt never completes"""
1322 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001323 self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True)
Simon Glass16b8d6b2018-07-06 10:27:42 -06001324 self.assertIn('Could not complete processing of Fdt: remaining '
1325 '[<_testing.Entry__testing', str(e.exception))
Simon Glass5c890232018-07-06 10:27:19 -06001326
Simon Glass53af22a2018-07-17 13:25:32 -06001327 def testEntryArgs(self):
1328 """Test passing arguments to entries from the command line"""
1329 entry_args = {
1330 'test-str-arg': 'test1',
1331 'test-int-arg': '456',
1332 }
Simon Glass741f2d62018-10-01 12:22:30 -06001333 self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args)
Simon Glass53af22a2018-07-17 13:25:32 -06001334 self.assertIn('image', control.images)
1335 entry = control.images['image'].GetEntries()['_testing']
1336 self.assertEqual('test0', entry.test_str_fdt)
1337 self.assertEqual('test1', entry.test_str_arg)
1338 self.assertEqual(123, entry.test_int_fdt)
1339 self.assertEqual(456, entry.test_int_arg)
1340
1341 def testEntryArgsMissing(self):
1342 """Test missing arguments and properties"""
1343 entry_args = {
1344 'test-int-arg': '456',
1345 }
Simon Glass741f2d62018-10-01 12:22:30 -06001346 self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args)
Simon Glass53af22a2018-07-17 13:25:32 -06001347 entry = control.images['image'].GetEntries()['_testing']
1348 self.assertEqual('test0', entry.test_str_fdt)
1349 self.assertEqual(None, entry.test_str_arg)
1350 self.assertEqual(None, entry.test_int_fdt)
1351 self.assertEqual(456, entry.test_int_arg)
1352
1353 def testEntryArgsRequired(self):
1354 """Test missing arguments and properties"""
1355 entry_args = {
1356 'test-int-arg': '456',
1357 }
1358 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001359 self._DoReadFileDtb('064_entry_args_required.dts')
Simon Glass53af22a2018-07-17 13:25:32 -06001360 self.assertIn("Node '/binman/_testing': Missing required "
1361 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
1362 str(e.exception))
1363
1364 def testEntryArgsInvalidFormat(self):
1365 """Test that an invalid entry-argument format is detected"""
Simon Glass53cd5d92019-07-08 14:25:29 -06001366 args = ['build', '-d', self.TestFile('064_entry_args_required.dts'),
1367 '-ano-value']
Simon Glass53af22a2018-07-17 13:25:32 -06001368 with self.assertRaises(ValueError) as e:
1369 self._DoBinman(*args)
1370 self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
1371
1372 def testEntryArgsInvalidInteger(self):
1373 """Test that an invalid entry-argument integer is detected"""
1374 entry_args = {
1375 'test-int-arg': 'abc',
1376 }
1377 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001378 self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args)
Simon Glass53af22a2018-07-17 13:25:32 -06001379 self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
1380 "'test-int-arg' (value 'abc') to integer",
1381 str(e.exception))
1382
1383 def testEntryArgsInvalidDatatype(self):
1384 """Test that an invalid entry-argument datatype is detected
1385
1386 This test could be written in entry_test.py except that it needs
1387 access to control.entry_args, which seems more than that module should
1388 be able to see.
1389 """
1390 entry_args = {
1391 'test-bad-datatype-arg': '12',
1392 }
1393 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001394 self._DoReadFileDtb('065_entry_args_unknown_datatype.dts',
Simon Glass53af22a2018-07-17 13:25:32 -06001395 entry_args=entry_args)
1396 self.assertIn('GetArg() internal error: Unknown data type ',
1397 str(e.exception))
1398
Simon Glassbb748372018-07-17 13:25:33 -06001399 def testText(self):
1400 """Test for a text entry type"""
1401 entry_args = {
1402 'test-id': TEXT_DATA,
1403 'test-id2': TEXT_DATA2,
1404 'test-id3': TEXT_DATA3,
1405 }
Simon Glass741f2d62018-10-01 12:22:30 -06001406 data, _, _, _ = self._DoReadFileDtb('066_text.dts',
Simon Glassbb748372018-07-17 13:25:33 -06001407 entry_args=entry_args)
Simon Glassc6c10e72019-05-17 22:00:46 -06001408 expected = (tools.ToBytes(TEXT_DATA) +
1409 tools.GetBytes(0, 8 - len(TEXT_DATA)) +
1410 tools.ToBytes(TEXT_DATA2) + tools.ToBytes(TEXT_DATA3) +
Simon Glassaa88b502019-07-08 13:18:40 -06001411 b'some text' + b'more text')
Simon Glassbb748372018-07-17 13:25:33 -06001412 self.assertEqual(expected, data)
1413
Simon Glassfd8d1f72018-07-17 13:25:36 -06001414 def testEntryDocs(self):
1415 """Test for creation of entry documentation"""
1416 with test_util.capture_sys_output() as (stdout, stderr):
1417 control.WriteEntryDocs(binman.GetEntryModules())
1418 self.assertTrue(len(stdout.getvalue()) > 0)
1419
1420 def testEntryDocsMissing(self):
1421 """Test handling of missing entry documentation"""
1422 with self.assertRaises(ValueError) as e:
1423 with test_util.capture_sys_output() as (stdout, stderr):
1424 control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
1425 self.assertIn('Documentation is missing for modules: u_boot',
1426 str(e.exception))
1427
Simon Glass11e36cc2018-07-17 13:25:38 -06001428 def testFmap(self):
1429 """Basic test of generation of a flashrom fmap"""
Simon Glass741f2d62018-10-01 12:22:30 -06001430 data = self._DoReadFile('067_fmap.dts')
Simon Glass11e36cc2018-07-17 13:25:38 -06001431 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
Simon Glassc6c10e72019-05-17 22:00:46 -06001432 expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
1433 U_BOOT_DATA + tools.GetBytes(ord('a'), 12))
Simon Glass11e36cc2018-07-17 13:25:38 -06001434 self.assertEqual(expected, data[:32])
Simon Glassc6c10e72019-05-17 22:00:46 -06001435 self.assertEqual(b'__FMAP__', fhdr.signature)
Simon Glass11e36cc2018-07-17 13:25:38 -06001436 self.assertEqual(1, fhdr.ver_major)
1437 self.assertEqual(0, fhdr.ver_minor)
1438 self.assertEqual(0, fhdr.base)
1439 self.assertEqual(16 + 16 +
1440 fmap_util.FMAP_HEADER_LEN +
1441 fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001442 self.assertEqual(b'FMAP', fhdr.name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001443 self.assertEqual(3, fhdr.nareas)
1444 for fentry in fentries:
1445 self.assertEqual(0, fentry.flags)
1446
1447 self.assertEqual(0, fentries[0].offset)
1448 self.assertEqual(4, fentries[0].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001449 self.assertEqual(b'RO_U_BOOT', fentries[0].name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001450
1451 self.assertEqual(16, fentries[1].offset)
1452 self.assertEqual(4, fentries[1].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001453 self.assertEqual(b'RW_U_BOOT', fentries[1].name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001454
1455 self.assertEqual(32, fentries[2].offset)
1456 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1457 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001458 self.assertEqual(b'FMAP', fentries[2].name)
Simon Glass11e36cc2018-07-17 13:25:38 -06001459
Simon Glassec127af2018-07-17 13:25:39 -06001460 def testBlobNamedByArg(self):
1461 """Test we can add a blob with the filename coming from an entry arg"""
1462 entry_args = {
1463 'cros-ec-rw-path': 'ecrw.bin',
1464 }
Simon Glass741f2d62018-10-01 12:22:30 -06001465 data, _, _, _ = self._DoReadFileDtb('068_blob_named_by_arg.dts',
Simon Glassec127af2018-07-17 13:25:39 -06001466 entry_args=entry_args)
1467
Simon Glass3af8e492018-07-17 13:25:40 -06001468 def testFill(self):
1469 """Test for an fill entry type"""
Simon Glass741f2d62018-10-01 12:22:30 -06001470 data = self._DoReadFile('069_fill.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -06001471 expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8)
Simon Glass3af8e492018-07-17 13:25:40 -06001472 self.assertEqual(expected, data)
1473
1474 def testFillNoSize(self):
1475 """Test for an fill entry type with no size"""
1476 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001477 self._DoReadFile('070_fill_no_size.dts')
Simon Glass3af8e492018-07-17 13:25:40 -06001478 self.assertIn("'fill' entry must have a size property",
1479 str(e.exception))
1480
Simon Glass0ef87aa2018-07-17 13:25:44 -06001481 def _HandleGbbCommand(self, pipe_list):
1482 """Fake calls to the futility utility"""
1483 if pipe_list[0][0] == 'futility':
1484 fname = pipe_list[0][-1]
1485 # Append our GBB data to the file, which will happen every time the
1486 # futility command is called.
Simon Glass1d0ebf72019-05-14 15:53:42 -06001487 with open(fname, 'ab') as fd:
Simon Glass0ef87aa2018-07-17 13:25:44 -06001488 fd.write(GBB_DATA)
1489 return command.CommandResult()
1490
1491 def testGbb(self):
1492 """Test for the Chromium OS Google Binary Block"""
1493 command.test_result = self._HandleGbbCommand
1494 entry_args = {
1495 'keydir': 'devkeys',
1496 'bmpblk': 'bmpblk.bin',
1497 }
Simon Glass741f2d62018-10-01 12:22:30 -06001498 data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args)
Simon Glass0ef87aa2018-07-17 13:25:44 -06001499
1500 # Since futility
Simon Glasse6d85ff2019-05-14 15:53:47 -06001501 expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) +
1502 tools.GetBytes(0, 0x2180 - 16))
Simon Glass0ef87aa2018-07-17 13:25:44 -06001503 self.assertEqual(expected, data)
1504
1505 def testGbbTooSmall(self):
1506 """Test for the Chromium OS Google Binary Block being large enough"""
1507 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001508 self._DoReadFileDtb('072_gbb_too_small.dts')
Simon Glass0ef87aa2018-07-17 13:25:44 -06001509 self.assertIn("Node '/binman/gbb': GBB is too small",
1510 str(e.exception))
1511
1512 def testGbbNoSize(self):
1513 """Test for the Chromium OS Google Binary Block having a size"""
1514 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001515 self._DoReadFileDtb('073_gbb_no_size.dts')
Simon Glass0ef87aa2018-07-17 13:25:44 -06001516 self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
1517 str(e.exception))
1518
Simon Glass24d0d3c2018-07-17 13:25:47 -06001519 def _HandleVblockCommand(self, pipe_list):
1520 """Fake calls to the futility utility"""
1521 if pipe_list[0][0] == 'futility':
1522 fname = pipe_list[0][3]
Simon Glassa326b492018-09-14 04:57:11 -06001523 with open(fname, 'wb') as fd:
Simon Glass24d0d3c2018-07-17 13:25:47 -06001524 fd.write(VBLOCK_DATA)
1525 return command.CommandResult()
1526
1527 def testVblock(self):
1528 """Test for the Chromium OS Verified Boot Block"""
1529 command.test_result = self._HandleVblockCommand
1530 entry_args = {
1531 'keydir': 'devkeys',
1532 }
Simon Glass741f2d62018-10-01 12:22:30 -06001533 data, _, _, _ = self._DoReadFileDtb('074_vblock.dts',
Simon Glass24d0d3c2018-07-17 13:25:47 -06001534 entry_args=entry_args)
1535 expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA
1536 self.assertEqual(expected, data)
1537
1538 def testVblockNoContent(self):
1539 """Test we detect a vblock which has no content to sign"""
1540 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001541 self._DoReadFile('075_vblock_no_content.dts')
Simon Glass24d0d3c2018-07-17 13:25:47 -06001542 self.assertIn("Node '/binman/vblock': Vblock must have a 'content' "
1543 'property', str(e.exception))
1544
1545 def testVblockBadPhandle(self):
1546 """Test that we detect a vblock with an invalid phandle in contents"""
1547 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001548 self._DoReadFile('076_vblock_bad_phandle.dts')
Simon Glass24d0d3c2018-07-17 13:25:47 -06001549 self.assertIn("Node '/binman/vblock': Cannot find node for phandle "
1550 '1000', str(e.exception))
1551
1552 def testVblockBadEntry(self):
1553 """Test that we detect an entry that points to a non-entry"""
1554 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001555 self._DoReadFile('077_vblock_bad_entry.dts')
Simon Glass24d0d3c2018-07-17 13:25:47 -06001556 self.assertIn("Node '/binman/vblock': Cannot find entry for node "
1557 "'other'", str(e.exception))
1558
Simon Glassb8ef5b62018-07-17 13:25:48 -06001559 def testTpl(self):
1560 """Test that an image with TPL and ots device tree can be created"""
1561 # ELF file with a '__bss_size' symbol
Simon Glass53e22bf2019-08-24 07:22:53 -06001562 with open(self.ElfTestFile('bss_data'), 'rb') as fd:
Simon Glassb8ef5b62018-07-17 13:25:48 -06001563 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001564 data = self._DoReadFile('078_u_boot_tpl.dts')
Simon Glassb8ef5b62018-07-17 13:25:48 -06001565 self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
1566
Simon Glass15a587c2018-07-17 13:25:51 -06001567 def testUsesPos(self):
1568 """Test that the 'pos' property cannot be used anymore"""
1569 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001570 data = self._DoReadFile('079_uses_pos.dts')
Simon Glass15a587c2018-07-17 13:25:51 -06001571 self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of "
1572 "'pos'", str(e.exception))
1573
Simon Glassd178eab2018-09-14 04:57:08 -06001574 def testFillZero(self):
1575 """Test for an fill entry type with a size of 0"""
Simon Glass741f2d62018-10-01 12:22:30 -06001576 data = self._DoReadFile('080_fill_empty.dts')
Simon Glasse6d85ff2019-05-14 15:53:47 -06001577 self.assertEqual(tools.GetBytes(0, 16), data)
Simon Glassd178eab2018-09-14 04:57:08 -06001578
Simon Glass0b489362018-09-14 04:57:09 -06001579 def testTextMissing(self):
1580 """Test for a text entry type where there is no text"""
1581 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001582 self._DoReadFileDtb('066_text.dts',)
Simon Glass0b489362018-09-14 04:57:09 -06001583 self.assertIn("Node '/binman/text': No value provided for text label "
1584 "'test-id'", str(e.exception))
1585
Simon Glass35b384c2018-09-14 04:57:10 -06001586 def testPackStart16Tpl(self):
1587 """Test that an image with an x86 start16 TPL region can be created"""
Simon Glass741f2d62018-10-01 12:22:30 -06001588 data = self._DoReadFile('081_x86-start16-tpl.dts')
Simon Glass35b384c2018-09-14 04:57:10 -06001589 self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)])
1590
Simon Glass0bfa7b02018-09-14 04:57:12 -06001591 def testSelectImage(self):
1592 """Test that we can select which images to build"""
Simon Glasseb833d82019-04-25 21:58:34 -06001593 expected = 'Skipping images: image1'
Simon Glass0bfa7b02018-09-14 04:57:12 -06001594
Simon Glasseb833d82019-04-25 21:58:34 -06001595 # We should only get the expected message in verbose mode
Simon Glassee0c9a72019-07-08 13:18:48 -06001596 for verbosity in (0, 2):
Simon Glasseb833d82019-04-25 21:58:34 -06001597 with test_util.capture_sys_output() as (stdout, stderr):
1598 retcode = self._DoTestFile('006_dual_image.dts',
1599 verbosity=verbosity,
1600 images=['image2'])
1601 self.assertEqual(0, retcode)
1602 if verbosity:
1603 self.assertIn(expected, stdout.getvalue())
1604 else:
1605 self.assertNotIn(expected, stdout.getvalue())
1606
1607 self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin')))
1608 self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin')))
Simon Glassf86a7362019-07-20 12:24:10 -06001609 self._CleanupOutputDir()
Simon Glass0bfa7b02018-09-14 04:57:12 -06001610
Simon Glass6ed45ba2018-09-14 04:57:24 -06001611 def testUpdateFdtAll(self):
1612 """Test that all device trees are updated with offset/size info"""
Simon Glass3c081312019-07-08 14:25:26 -06001613 data = self._DoReadFileRealDtb('082_fdt_update_all.dts')
Simon Glass6ed45ba2018-09-14 04:57:24 -06001614
1615 base_expected = {
1616 'section:image-pos': 0,
1617 'u-boot-tpl-dtb:size': 513,
1618 'u-boot-spl-dtb:size': 513,
1619 'u-boot-spl-dtb:offset': 493,
1620 'image-pos': 0,
1621 'section/u-boot-dtb:image-pos': 0,
1622 'u-boot-spl-dtb:image-pos': 493,
1623 'section/u-boot-dtb:size': 493,
1624 'u-boot-tpl-dtb:image-pos': 1006,
1625 'section/u-boot-dtb:offset': 0,
1626 'section:size': 493,
1627 'offset': 0,
1628 'section:offset': 0,
1629 'u-boot-tpl-dtb:offset': 1006,
1630 'size': 1519
1631 }
1632
1633 # We expect three device-tree files in the output, one after the other.
1634 # Read them in sequence. We look for an 'spl' property in the SPL tree,
1635 # and 'tpl' in the TPL tree, to make sure they are distinct from the
1636 # main U-Boot tree. All three should have the same postions and offset.
1637 start = 0
1638 for item in ['', 'spl', 'tpl']:
1639 dtb = fdt.Fdt.FromData(data[start:])
1640 dtb.Scan()
Simon Glass12bb1a92019-07-20 12:23:51 -06001641 props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS +
1642 ['spl', 'tpl'])
Simon Glass6ed45ba2018-09-14 04:57:24 -06001643 expected = dict(base_expected)
1644 if item:
1645 expected[item] = 0
1646 self.assertEqual(expected, props)
1647 start += dtb._fdt_obj.totalsize()
1648
1649 def testUpdateFdtOutput(self):
1650 """Test that output DTB files are updated"""
1651 try:
Simon Glass741f2d62018-10-01 12:22:30 -06001652 data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts',
Simon Glass6ed45ba2018-09-14 04:57:24 -06001653 use_real_dtb=True, update_dtb=True, reset_dtbs=False)
1654
1655 # Unfortunately, compiling a source file always results in a file
1656 # called source.dtb (see fdt_util.EnsureCompiled()). The test
Simon Glass741f2d62018-10-01 12:22:30 -06001657 # source file (e.g. test/075_fdt_update_all.dts) thus does not enter
Simon Glass6ed45ba2018-09-14 04:57:24 -06001658 # binman as a file called u-boot.dtb. To fix this, copy the file
1659 # over to the expected place.
1660 #tools.WriteFile(os.path.join(self._indir, 'u-boot.dtb'),
1661 #tools.ReadFile(tools.GetOutputFilename('source.dtb')))
1662 start = 0
1663 for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out',
1664 'tpl/u-boot-tpl.dtb.out']:
1665 dtb = fdt.Fdt.FromData(data[start:])
1666 size = dtb._fdt_obj.totalsize()
1667 pathname = tools.GetOutputFilename(os.path.split(fname)[1])
1668 outdata = tools.ReadFile(pathname)
1669 name = os.path.split(fname)[0]
1670
1671 if name:
1672 orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name)
1673 else:
1674 orig_indata = dtb_data
1675 self.assertNotEqual(outdata, orig_indata,
1676 "Expected output file '%s' be updated" % pathname)
1677 self.assertEqual(outdata, data[start:start + size],
1678 "Expected output file '%s' to match output image" %
1679 pathname)
1680 start += size
1681 finally:
1682 self._ResetDtbs()
1683
Simon Glass83d73c22018-09-14 04:57:26 -06001684 def _decompress(self, data):
Simon Glassff5c7e32019-07-08 13:18:42 -06001685 return tools.Decompress(data, 'lz4')
Simon Glass83d73c22018-09-14 04:57:26 -06001686
1687 def testCompress(self):
1688 """Test compression of blobs"""
Simon Glassac62fba2019-07-08 13:18:53 -06001689 self._CheckLz4()
Simon Glass741f2d62018-10-01 12:22:30 -06001690 data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts',
Simon Glass83d73c22018-09-14 04:57:26 -06001691 use_real_dtb=True, update_dtb=True)
1692 dtb = fdt.Fdt(out_dtb_fname)
1693 dtb.Scan()
1694 props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
1695 orig = self._decompress(data)
1696 self.assertEquals(COMPRESS_DATA, orig)
1697 expected = {
1698 'blob:uncomp-size': len(COMPRESS_DATA),
1699 'blob:size': len(data),
1700 'size': len(data),
1701 }
1702 self.assertEqual(expected, props)
1703
Simon Glass0a98b282018-09-14 04:57:28 -06001704 def testFiles(self):
1705 """Test bringing in multiple files"""
Simon Glass741f2d62018-10-01 12:22:30 -06001706 data = self._DoReadFile('084_files.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001707 self.assertEqual(FILES_DATA, data)
1708
1709 def testFilesCompress(self):
1710 """Test bringing in multiple files and compressing them"""
Simon Glassac62fba2019-07-08 13:18:53 -06001711 self._CheckLz4()
Simon Glass741f2d62018-10-01 12:22:30 -06001712 data = self._DoReadFile('085_files_compress.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001713
1714 image = control.images['image']
1715 entries = image.GetEntries()
1716 files = entries['files']
Simon Glass8beb11e2019-07-08 14:25:47 -06001717 entries = files._entries
Simon Glass0a98b282018-09-14 04:57:28 -06001718
Simon Glassc6c10e72019-05-17 22:00:46 -06001719 orig = b''
Simon Glass0a98b282018-09-14 04:57:28 -06001720 for i in range(1, 3):
1721 key = '%d.dat' % i
1722 start = entries[key].image_pos
1723 len = entries[key].size
1724 chunk = data[start:start + len]
1725 orig += self._decompress(chunk)
1726
1727 self.assertEqual(FILES_DATA, orig)
1728
1729 def testFilesMissing(self):
1730 """Test missing files"""
1731 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001732 data = self._DoReadFile('086_files_none.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001733 self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched "
1734 'no files', str(e.exception))
1735
1736 def testFilesNoPattern(self):
1737 """Test missing files"""
1738 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001739 data = self._DoReadFile('087_files_no_pattern.dts')
Simon Glass0a98b282018-09-14 04:57:28 -06001740 self.assertIn("Node '/binman/files': Missing 'pattern' property",
1741 str(e.exception))
1742
Simon Glassba64a0b2018-09-14 04:57:29 -06001743 def testExpandSize(self):
1744 """Test an expanding entry"""
Simon Glass741f2d62018-10-01 12:22:30 -06001745 data, _, map_data, _ = self._DoReadFileDtb('088_expand_size.dts',
Simon Glassba64a0b2018-09-14 04:57:29 -06001746 map=True)
Simon Glassc6c10e72019-05-17 22:00:46 -06001747 expect = (tools.GetBytes(ord('a'), 8) + U_BOOT_DATA +
1748 MRC_DATA + tools.GetBytes(ord('b'), 1) + U_BOOT_DATA +
1749 tools.GetBytes(ord('c'), 8) + U_BOOT_DATA +
1750 tools.GetBytes(ord('d'), 8))
Simon Glassba64a0b2018-09-14 04:57:29 -06001751 self.assertEqual(expect, data)
1752 self.assertEqual('''ImagePos Offset Size Name
175300000000 00000000 00000028 main-section
175400000000 00000000 00000008 fill
175500000008 00000008 00000004 u-boot
17560000000c 0000000c 00000004 section
17570000000c 00000000 00000003 intel-mrc
175800000010 00000010 00000004 u-boot2
175900000014 00000014 0000000c section2
176000000014 00000000 00000008 fill
17610000001c 00000008 00000004 u-boot
176200000020 00000020 00000008 fill2
1763''', map_data)
1764
1765 def testExpandSizeBad(self):
1766 """Test an expanding entry which fails to provide contents"""
Simon Glass163ed6c2018-09-14 04:57:36 -06001767 with test_util.capture_sys_output() as (stdout, stderr):
1768 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001769 self._DoReadFileDtb('089_expand_size_bad.dts', map=True)
Simon Glassba64a0b2018-09-14 04:57:29 -06001770 self.assertIn("Node '/binman/_testing': Cannot obtain contents when "
1771 'expanding entry', str(e.exception))
1772
Simon Glasse0e5df92018-09-14 04:57:31 -06001773 def testHash(self):
1774 """Test hashing of the contents of an entry"""
Simon Glass741f2d62018-10-01 12:22:30 -06001775 _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts',
Simon Glasse0e5df92018-09-14 04:57:31 -06001776 use_real_dtb=True, update_dtb=True)
1777 dtb = fdt.Fdt(out_dtb_fname)
1778 dtb.Scan()
1779 hash_node = dtb.GetNode('/binman/u-boot/hash').props['value']
1780 m = hashlib.sha256()
1781 m.update(U_BOOT_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -06001782 self.assertEqual(m.digest(), b''.join(hash_node.value))
Simon Glasse0e5df92018-09-14 04:57:31 -06001783
1784 def testHashNoAlgo(self):
1785 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001786 self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True)
Simon Glasse0e5df92018-09-14 04:57:31 -06001787 self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for "
1788 'hash node', str(e.exception))
1789
1790 def testHashBadAlgo(self):
1791 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001792 self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True)
Simon Glasse0e5df92018-09-14 04:57:31 -06001793 self.assertIn("Node '/binman/u-boot': Unknown hash algorithm",
1794 str(e.exception))
1795
1796 def testHashSection(self):
1797 """Test hashing of the contents of an entry"""
Simon Glass741f2d62018-10-01 12:22:30 -06001798 _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts',
Simon Glasse0e5df92018-09-14 04:57:31 -06001799 use_real_dtb=True, update_dtb=True)
1800 dtb = fdt.Fdt(out_dtb_fname)
1801 dtb.Scan()
1802 hash_node = dtb.GetNode('/binman/section/hash').props['value']
1803 m = hashlib.sha256()
1804 m.update(U_BOOT_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -06001805 m.update(tools.GetBytes(ord('a'), 16))
1806 self.assertEqual(m.digest(), b''.join(hash_node.value))
Simon Glasse0e5df92018-09-14 04:57:31 -06001807
Simon Glassf0253632018-09-14 04:57:32 -06001808 def testPackUBootTplMicrocode(self):
1809 """Test that x86 microcode can be handled correctly in TPL
1810
1811 We expect to see the following in the image, in order:
1812 u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct
1813 place
1814 u-boot-tpl.dtb with the microcode removed
1815 the microcode
1816 """
Simon Glassf514d8f2019-08-24 07:22:54 -06001817 TestFunctional._MakeInputFile('tpl/u-boot-tpl',
1818 tools.ReadFile(self.ElfTestFile('u_boot_ucode_ptr')))
Simon Glass741f2d62018-10-01 12:22:30 -06001819 first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts',
Simon Glassf0253632018-09-14 04:57:32 -06001820 U_BOOT_TPL_NODTB_DATA)
Simon Glassc6c10e72019-05-17 22:00:46 -06001821 self.assertEqual(b'tplnodtb with microc' + pos_and_size +
1822 b'ter somewhere in here', first)
Simon Glassf0253632018-09-14 04:57:32 -06001823
Simon Glassf8f8df62018-09-14 04:57:34 -06001824 def testFmapX86(self):
1825 """Basic test of generation of a flashrom fmap"""
Simon Glass741f2d62018-10-01 12:22:30 -06001826 data = self._DoReadFile('094_fmap_x86.dts')
Simon Glassf8f8df62018-09-14 04:57:34 -06001827 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
Simon Glassc6c10e72019-05-17 22:00:46 -06001828 expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('a'), 32 - 7)
Simon Glassf8f8df62018-09-14 04:57:34 -06001829 self.assertEqual(expected, data[:32])
1830 fhdr, fentries = fmap_util.DecodeFmap(data[32:])
1831
1832 self.assertEqual(0x100, fhdr.image_size)
1833
1834 self.assertEqual(0, fentries[0].offset)
1835 self.assertEqual(4, fentries[0].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001836 self.assertEqual(b'U_BOOT', fentries[0].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001837
1838 self.assertEqual(4, fentries[1].offset)
1839 self.assertEqual(3, fentries[1].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001840 self.assertEqual(b'INTEL_MRC', fentries[1].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001841
1842 self.assertEqual(32, fentries[2].offset)
1843 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1844 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001845 self.assertEqual(b'FMAP', fentries[2].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001846
1847 def testFmapX86Section(self):
1848 """Basic test of generation of a flashrom fmap"""
Simon Glass741f2d62018-10-01 12:22:30 -06001849 data = self._DoReadFile('095_fmap_x86_section.dts')
Simon Glassc6c10e72019-05-17 22:00:46 -06001850 expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('b'), 32 - 7)
Simon Glassf8f8df62018-09-14 04:57:34 -06001851 self.assertEqual(expected, data[:32])
1852 fhdr, fentries = fmap_util.DecodeFmap(data[36:])
1853
1854 self.assertEqual(0x100, fhdr.image_size)
1855
1856 self.assertEqual(0, fentries[0].offset)
1857 self.assertEqual(4, fentries[0].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001858 self.assertEqual(b'U_BOOT', fentries[0].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001859
1860 self.assertEqual(4, fentries[1].offset)
1861 self.assertEqual(3, fentries[1].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001862 self.assertEqual(b'INTEL_MRC', fentries[1].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001863
1864 self.assertEqual(36, fentries[2].offset)
1865 self.assertEqual(fmap_util.FMAP_HEADER_LEN +
1866 fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
Simon Glassc6c10e72019-05-17 22:00:46 -06001867 self.assertEqual(b'FMAP', fentries[2].name)
Simon Glassf8f8df62018-09-14 04:57:34 -06001868
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001869 def testElf(self):
1870 """Basic test of ELF entries"""
Simon Glass11ae93e2018-10-01 21:12:47 -06001871 self._SetupSplElf()
Simon Glass53e22bf2019-08-24 07:22:53 -06001872 with open(self.ElfTestFile('bss_data'), 'rb') as fd:
Simon Glass4c650252019-07-08 13:18:46 -06001873 TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
Simon Glass53e22bf2019-08-24 07:22:53 -06001874 with open(self.ElfTestFile('bss_data'), 'rb') as fd:
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001875 TestFunctional._MakeInputFile('-boot', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001876 data = self._DoReadFile('096_elf.dts')
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001877
Simon Glass093d1682019-07-08 13:18:25 -06001878 def testElfStrip(self):
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001879 """Basic test of ELF entries"""
Simon Glass11ae93e2018-10-01 21:12:47 -06001880 self._SetupSplElf()
Simon Glass53e22bf2019-08-24 07:22:53 -06001881 with open(self.ElfTestFile('bss_data'), 'rb') as fd:
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001882 TestFunctional._MakeInputFile('-boot', fd.read())
Simon Glass741f2d62018-10-01 12:22:30 -06001883 data = self._DoReadFile('097_elf_strip.dts')
Simon Glassfe1ae3e2018-09-14 04:57:35 -06001884
Simon Glass163ed6c2018-09-14 04:57:36 -06001885 def testPackOverlapMap(self):
1886 """Test that overlapping regions are detected"""
1887 with test_util.capture_sys_output() as (stdout, stderr):
1888 with self.assertRaises(ValueError) as e:
Simon Glass741f2d62018-10-01 12:22:30 -06001889 self._DoTestFile('014_pack_overlap.dts', map=True)
Simon Glass163ed6c2018-09-14 04:57:36 -06001890 map_fname = tools.GetOutputFilename('image.map')
1891 self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname,
1892 stdout.getvalue())
1893
1894 # We should not get an inmage, but there should be a map file
1895 self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin')))
1896 self.assertTrue(os.path.exists(map_fname))
Simon Glasseb546ac2019-05-17 22:00:51 -06001897 map_data = tools.ReadFile(map_fname, binary=False)
Simon Glass163ed6c2018-09-14 04:57:36 -06001898 self.assertEqual('''ImagePos Offset Size Name
1899<none> 00000000 00000007 main-section
1900<none> 00000000 00000004 u-boot
1901<none> 00000003 00000004 u-boot-align
1902''', map_data)
1903
Simon Glass093d1682019-07-08 13:18:25 -06001904 def testPackRefCode(self):
Simon Glass3ae192c2018-10-01 12:22:31 -06001905 """Test that an image with an Intel Reference code binary works"""
1906 data = self._DoReadFile('100_intel_refcode.dts')
1907 self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)])
1908
Simon Glass9481c802019-04-25 21:58:39 -06001909 def testSectionOffset(self):
1910 """Tests use of a section with an offset"""
1911 data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts',
1912 map=True)
1913 self.assertEqual('''ImagePos Offset Size Name
191400000000 00000000 00000038 main-section
191500000004 00000004 00000010 section@0
191600000004 00000000 00000004 u-boot
191700000018 00000018 00000010 section@1
191800000018 00000000 00000004 u-boot
19190000002c 0000002c 00000004 section@2
19200000002c 00000000 00000004 u-boot
1921''', map_data)
1922 self.assertEqual(data,
Simon Glasse6d85ff2019-05-14 15:53:47 -06001923 tools.GetBytes(0x26, 4) + U_BOOT_DATA +
1924 tools.GetBytes(0x21, 12) +
1925 tools.GetBytes(0x26, 4) + U_BOOT_DATA +
1926 tools.GetBytes(0x61, 12) +
1927 tools.GetBytes(0x26, 4) + U_BOOT_DATA +
1928 tools.GetBytes(0x26, 8))
Simon Glass9481c802019-04-25 21:58:39 -06001929
Simon Glassac62fba2019-07-08 13:18:53 -06001930 def testCbfsRaw(self):
1931 """Test base handling of a Coreboot Filesystem (CBFS)
1932
1933 The exact contents of the CBFS is verified by similar tests in
1934 cbfs_util_test.py. The tests here merely check that the files added to
1935 the CBFS can be found in the final image.
1936 """
1937 data = self._DoReadFile('102_cbfs_raw.dts')
1938 size = 0xb0
1939
1940 cbfs = cbfs_util.CbfsReader(data)
1941 self.assertEqual(size, cbfs.rom_size)
1942
1943 self.assertIn('u-boot-dtb', cbfs.files)
1944 cfile = cbfs.files['u-boot-dtb']
1945 self.assertEqual(U_BOOT_DTB_DATA, cfile.data)
1946
1947 def testCbfsArch(self):
1948 """Test on non-x86 architecture"""
1949 data = self._DoReadFile('103_cbfs_raw_ppc.dts')
1950 size = 0x100
1951
1952 cbfs = cbfs_util.CbfsReader(data)
1953 self.assertEqual(size, cbfs.rom_size)
1954
1955 self.assertIn('u-boot-dtb', cbfs.files)
1956 cfile = cbfs.files['u-boot-dtb']
1957 self.assertEqual(U_BOOT_DTB_DATA, cfile.data)
1958
1959 def testCbfsStage(self):
1960 """Tests handling of a Coreboot Filesystem (CBFS)"""
1961 if not elf.ELF_TOOLS:
1962 self.skipTest('Python elftools not available')
1963 elf_fname = os.path.join(self._indir, 'cbfs-stage.elf')
1964 elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA)
1965 size = 0xb0
1966
1967 data = self._DoReadFile('104_cbfs_stage.dts')
1968 cbfs = cbfs_util.CbfsReader(data)
1969 self.assertEqual(size, cbfs.rom_size)
1970
1971 self.assertIn('u-boot', cbfs.files)
1972 cfile = cbfs.files['u-boot']
1973 self.assertEqual(U_BOOT_DATA + U_BOOT_DTB_DATA, cfile.data)
1974
1975 def testCbfsRawCompress(self):
1976 """Test handling of compressing raw files"""
1977 self._CheckLz4()
1978 data = self._DoReadFile('105_cbfs_raw_compress.dts')
1979 size = 0x140
1980
1981 cbfs = cbfs_util.CbfsReader(data)
1982 self.assertIn('u-boot', cbfs.files)
1983 cfile = cbfs.files['u-boot']
1984 self.assertEqual(COMPRESS_DATA, cfile.data)
1985
1986 def testCbfsBadArch(self):
1987 """Test handling of a bad architecture"""
1988 with self.assertRaises(ValueError) as e:
1989 self._DoReadFile('106_cbfs_bad_arch.dts')
1990 self.assertIn("Invalid architecture 'bad-arch'", str(e.exception))
1991
1992 def testCbfsNoSize(self):
1993 """Test handling of a missing size property"""
1994 with self.assertRaises(ValueError) as e:
1995 self._DoReadFile('107_cbfs_no_size.dts')
1996 self.assertIn('entry must have a size property', str(e.exception))
1997
1998 def testCbfsNoCOntents(self):
1999 """Test handling of a CBFS entry which does not provide contentsy"""
2000 with self.assertRaises(ValueError) as e:
2001 self._DoReadFile('108_cbfs_no_contents.dts')
2002 self.assertIn('Could not complete processing of contents',
2003 str(e.exception))
2004
2005 def testCbfsBadCompress(self):
2006 """Test handling of a bad architecture"""
2007 with self.assertRaises(ValueError) as e:
2008 self._DoReadFile('109_cbfs_bad_compress.dts')
2009 self.assertIn("Invalid compression in 'u-boot': 'invalid-algo'",
2010 str(e.exception))
2011
2012 def testCbfsNamedEntries(self):
2013 """Test handling of named entries"""
2014 data = self._DoReadFile('110_cbfs_name.dts')
2015
2016 cbfs = cbfs_util.CbfsReader(data)
2017 self.assertIn('FRED', cbfs.files)
2018 cfile1 = cbfs.files['FRED']
2019 self.assertEqual(U_BOOT_DATA, cfile1.data)
2020
2021 self.assertIn('hello', cbfs.files)
2022 cfile2 = cbfs.files['hello']
2023 self.assertEqual(U_BOOT_DTB_DATA, cfile2.data)
2024
Simon Glassc5ac1382019-07-08 13:18:54 -06002025 def _SetupIfwi(self, fname):
2026 """Set up to run an IFWI test
2027
2028 Args:
2029 fname: Filename of input file to provide (fitimage.bin or ifwi.bin)
2030 """
2031 self._SetupSplElf()
2032
2033 # Intel Integrated Firmware Image (IFWI) file
2034 with gzip.open(self.TestFile('%s.gz' % fname), 'rb') as fd:
2035 data = fd.read()
2036 TestFunctional._MakeInputFile(fname,data)
2037
2038 def _CheckIfwi(self, data):
2039 """Check that an image with an IFWI contains the correct output
2040
2041 Args:
2042 data: Conents of output file
2043 """
2044 expected_desc = tools.ReadFile(self.TestFile('descriptor.bin'))
2045 if data[:0x1000] != expected_desc:
2046 self.fail('Expected descriptor binary at start of image')
2047
2048 # We expect to find the TPL wil in subpart IBBP entry IBBL
2049 image_fname = tools.GetOutputFilename('image.bin')
2050 tpl_fname = tools.GetOutputFilename('tpl.out')
2051 tools.RunIfwiTool(image_fname, tools.CMD_EXTRACT, fname=tpl_fname,
2052 subpart='IBBP', entry_name='IBBL')
2053
2054 tpl_data = tools.ReadFile(tpl_fname)
Simon Glasse95be632019-08-24 07:22:51 -06002055 self.assertEqual(U_BOOT_TPL_DATA, tpl_data[:len(U_BOOT_TPL_DATA)])
Simon Glassc5ac1382019-07-08 13:18:54 -06002056
2057 def testPackX86RomIfwi(self):
2058 """Test that an x86 ROM with Integrated Firmware Image can be created"""
2059 self._SetupIfwi('fitimage.bin')
2060 data = self._DoReadFile('111_x86-rom-ifwi.dts')
2061 self._CheckIfwi(data)
2062
2063 def testPackX86RomIfwiNoDesc(self):
2064 """Test that an x86 ROM with IFWI can be created from an ifwi.bin file"""
2065 self._SetupIfwi('ifwi.bin')
2066 data = self._DoReadFile('112_x86-rom-ifwi-nodesc.dts')
2067 self._CheckIfwi(data)
2068
2069 def testPackX86RomIfwiNoData(self):
2070 """Test that an x86 ROM with IFWI handles missing data"""
2071 self._SetupIfwi('ifwi.bin')
2072 with self.assertRaises(ValueError) as e:
2073 data = self._DoReadFile('113_x86-rom-ifwi-nodata.dts')
2074 self.assertIn('Could not complete processing of contents',
2075 str(e.exception))
Simon Glass53af22a2018-07-17 13:25:32 -06002076
Simon Glasse073d4e2019-07-08 13:18:56 -06002077 def testCbfsOffset(self):
2078 """Test a CBFS with files at particular offsets
2079
2080 Like all CFBS tests, this is just checking the logic that calls
2081 cbfs_util. See cbfs_util_test for fully tests (e.g. test_cbfs_offset()).
2082 """
2083 data = self._DoReadFile('114_cbfs_offset.dts')
2084 size = 0x200
2085
2086 cbfs = cbfs_util.CbfsReader(data)
2087 self.assertEqual(size, cbfs.rom_size)
2088
2089 self.assertIn('u-boot', cbfs.files)
2090 cfile = cbfs.files['u-boot']
2091 self.assertEqual(U_BOOT_DATA, cfile.data)
2092 self.assertEqual(0x40, cfile.cbfs_offset)
2093
2094 self.assertIn('u-boot-dtb', cbfs.files)
2095 cfile2 = cbfs.files['u-boot-dtb']
2096 self.assertEqual(U_BOOT_DTB_DATA, cfile2.data)
2097 self.assertEqual(0x140, cfile2.cbfs_offset)
2098
Simon Glass086cec92019-07-08 14:25:27 -06002099 def testFdtmap(self):
2100 """Test an FDT map can be inserted in the image"""
2101 data = self.data = self._DoReadFileRealDtb('115_fdtmap.dts')
2102 fdtmap_data = data[len(U_BOOT_DATA):]
2103 magic = fdtmap_data[:8]
2104 self.assertEqual('_FDTMAP_', magic)
2105 self.assertEqual(tools.GetBytes(0, 8), fdtmap_data[8:16])
2106
2107 fdt_data = fdtmap_data[16:]
2108 dtb = fdt.Fdt.FromData(fdt_data)
2109 dtb.Scan()
Simon Glass6ccbfcd2019-07-20 12:23:47 -06002110 props = self._GetPropTree(dtb, BASE_DTB_PROPS, prefix='/')
Simon Glass086cec92019-07-08 14:25:27 -06002111 self.assertEqual({
2112 'image-pos': 0,
2113 'offset': 0,
2114 'u-boot:offset': 0,
2115 'u-boot:size': len(U_BOOT_DATA),
2116 'u-boot:image-pos': 0,
2117 'fdtmap:image-pos': 4,
2118 'fdtmap:offset': 4,
2119 'fdtmap:size': len(fdtmap_data),
2120 'size': len(data),
2121 }, props)
2122
2123 def testFdtmapNoMatch(self):
2124 """Check handling of an FDT map when the section cannot be found"""
2125 self.data = self._DoReadFileRealDtb('115_fdtmap.dts')
2126
2127 # Mangle the section name, which should cause a mismatch between the
2128 # correct FDT path and the one expected by the section
2129 image = control.images['image']
Simon Glasscf228942019-07-08 14:25:28 -06002130 image._node.path += '-suffix'
Simon Glass086cec92019-07-08 14:25:27 -06002131 entries = image.GetEntries()
2132 fdtmap = entries['fdtmap']
2133 with self.assertRaises(ValueError) as e:
2134 fdtmap._GetFdtmap()
2135 self.assertIn("Cannot locate node for path '/binman-suffix'",
2136 str(e.exception))
2137
Simon Glasscf228942019-07-08 14:25:28 -06002138 def testFdtmapHeader(self):
2139 """Test an FDT map and image header can be inserted in the image"""
2140 data = self.data = self._DoReadFileRealDtb('116_fdtmap_hdr.dts')
2141 fdtmap_pos = len(U_BOOT_DATA)
2142 fdtmap_data = data[fdtmap_pos:]
2143 fdt_data = fdtmap_data[16:]
2144 dtb = fdt.Fdt.FromData(fdt_data)
2145 fdt_size = dtb.GetFdtObj().totalsize()
2146 hdr_data = data[-8:]
2147 self.assertEqual('BinM', hdr_data[:4])
2148 offset = struct.unpack('<I', hdr_data[4:])[0] & 0xffffffff
2149 self.assertEqual(fdtmap_pos - 0x400, offset - (1 << 32))
2150
2151 def testFdtmapHeaderStart(self):
2152 """Test an image header can be inserted at the image start"""
2153 data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts')
2154 fdtmap_pos = 0x100 + len(U_BOOT_DATA)
2155 hdr_data = data[:8]
2156 self.assertEqual('BinM', hdr_data[:4])
2157 offset = struct.unpack('<I', hdr_data[4:])[0]
2158 self.assertEqual(fdtmap_pos, offset)
2159
2160 def testFdtmapHeaderPos(self):
2161 """Test an image header can be inserted at a chosen position"""
2162 data = self.data = self._DoReadFileRealDtb('118_fdtmap_hdr_pos.dts')
2163 fdtmap_pos = 0x100 + len(U_BOOT_DATA)
2164 hdr_data = data[0x80:0x88]
2165 self.assertEqual('BinM', hdr_data[:4])
2166 offset = struct.unpack('<I', hdr_data[4:])[0]
2167 self.assertEqual(fdtmap_pos, offset)
2168
2169 def testHeaderMissingFdtmap(self):
2170 """Test an image header requires an fdtmap"""
2171 with self.assertRaises(ValueError) as e:
2172 self.data = self._DoReadFileRealDtb('119_fdtmap_hdr_missing.dts')
2173 self.assertIn("'image_header' section must have an 'fdtmap' sibling",
2174 str(e.exception))
2175
2176 def testHeaderNoLocation(self):
2177 """Test an image header with a no specified location is detected"""
2178 with self.assertRaises(ValueError) as e:
2179 self.data = self._DoReadFileRealDtb('120_hdr_no_location.dts')
2180 self.assertIn("Invalid location 'None', expected 'start' or 'end'",
2181 str(e.exception))
2182
Simon Glassc52c9e72019-07-08 14:25:37 -06002183 def testEntryExpand(self):
2184 """Test expanding an entry after it is packed"""
2185 data = self._DoReadFile('121_entry_expand.dts')
Simon Glass79d3c582019-07-20 12:23:57 -06002186 self.assertEqual(b'aaa', data[:3])
2187 self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)])
2188 self.assertEqual(b'aaa', data[-3:])
Simon Glassc52c9e72019-07-08 14:25:37 -06002189
2190 def testEntryExpandBad(self):
2191 """Test expanding an entry after it is packed, twice"""
2192 with self.assertRaises(ValueError) as e:
2193 self._DoReadFile('122_entry_expand_twice.dts')
Simon Glass61ec04f2019-07-20 12:23:58 -06002194 self.assertIn("Image '/binman': Entries changed size after packing",
Simon Glassc52c9e72019-07-08 14:25:37 -06002195 str(e.exception))
2196
2197 def testEntryExpandSection(self):
2198 """Test expanding an entry within a section after it is packed"""
2199 data = self._DoReadFile('123_entry_expand_section.dts')
Simon Glass79d3c582019-07-20 12:23:57 -06002200 self.assertEqual(b'aaa', data[:3])
2201 self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)])
2202 self.assertEqual(b'aaa', data[-3:])
Simon Glassc52c9e72019-07-08 14:25:37 -06002203
Simon Glass6c223fd2019-07-08 14:25:38 -06002204 def testCompressDtb(self):
2205 """Test that compress of device-tree files is supported"""
2206 self._CheckLz4()
2207 data = self.data = self._DoReadFileRealDtb('124_compress_dtb.dts')
2208 self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
2209 comp_data = data[len(U_BOOT_DATA):]
2210 orig = self._decompress(comp_data)
2211 dtb = fdt.Fdt.FromData(orig)
2212 dtb.Scan()
2213 props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
2214 expected = {
2215 'u-boot:size': len(U_BOOT_DATA),
2216 'u-boot-dtb:uncomp-size': len(orig),
2217 'u-boot-dtb:size': len(comp_data),
2218 'size': len(data),
2219 }
2220 self.assertEqual(expected, props)
2221
Simon Glass69f7cb32019-07-08 14:25:41 -06002222 def testCbfsUpdateFdt(self):
2223 """Test that we can update the device tree with CBFS offset/size info"""
2224 self._CheckLz4()
2225 data, _, _, out_dtb_fname = self._DoReadFileDtb('125_cbfs_update.dts',
2226 update_dtb=True)
2227 dtb = fdt.Fdt(out_dtb_fname)
2228 dtb.Scan()
Simon Glass6ccbfcd2019-07-20 12:23:47 -06002229 props = self._GetPropTree(dtb, BASE_DTB_PROPS + ['uncomp-size'])
Simon Glass69f7cb32019-07-08 14:25:41 -06002230 del props['cbfs/u-boot:size']
2231 self.assertEqual({
2232 'offset': 0,
2233 'size': len(data),
2234 'image-pos': 0,
2235 'cbfs:offset': 0,
2236 'cbfs:size': len(data),
2237 'cbfs:image-pos': 0,
2238 'cbfs/u-boot:offset': 0x38,
2239 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA),
2240 'cbfs/u-boot:image-pos': 0x38,
2241 'cbfs/u-boot-dtb:offset': 0xb8,
2242 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA),
2243 'cbfs/u-boot-dtb:image-pos': 0xb8,
2244 }, props)
2245
Simon Glass8a1ad062019-07-08 14:25:42 -06002246 def testCbfsBadType(self):
2247 """Test an image header with a no specified location is detected"""
2248 with self.assertRaises(ValueError) as e:
2249 self._DoReadFile('126_cbfs_bad_type.dts')
2250 self.assertIn("Unknown cbfs-type 'badtype'", str(e.exception))
2251
Simon Glass41b8ba02019-07-08 14:25:43 -06002252 def testList(self):
2253 """Test listing the files in an image"""
2254 self._CheckLz4()
2255 data = self._DoReadFile('127_list.dts')
2256 image = control.images['image']
2257 entries = image.BuildEntryList()
2258 self.assertEqual(7, len(entries))
2259
2260 ent = entries[0]
2261 self.assertEqual(0, ent.indent)
2262 self.assertEqual('main-section', ent.name)
2263 self.assertEqual('section', ent.etype)
2264 self.assertEqual(len(data), ent.size)
2265 self.assertEqual(0, ent.image_pos)
2266 self.assertEqual(None, ent.uncomp_size)
Simon Glass8beb11e2019-07-08 14:25:47 -06002267 self.assertEqual(0, ent.offset)
Simon Glass41b8ba02019-07-08 14:25:43 -06002268
2269 ent = entries[1]
2270 self.assertEqual(1, ent.indent)
2271 self.assertEqual('u-boot', ent.name)
2272 self.assertEqual('u-boot', ent.etype)
2273 self.assertEqual(len(U_BOOT_DATA), ent.size)
2274 self.assertEqual(0, ent.image_pos)
2275 self.assertEqual(None, ent.uncomp_size)
2276 self.assertEqual(0, ent.offset)
2277
2278 ent = entries[2]
2279 self.assertEqual(1, ent.indent)
2280 self.assertEqual('section', ent.name)
2281 self.assertEqual('section', ent.etype)
2282 section_size = ent.size
2283 self.assertEqual(0x100, ent.image_pos)
2284 self.assertEqual(None, ent.uncomp_size)
Simon Glass8beb11e2019-07-08 14:25:47 -06002285 self.assertEqual(0x100, ent.offset)
Simon Glass41b8ba02019-07-08 14:25:43 -06002286
2287 ent = entries[3]
2288 self.assertEqual(2, ent.indent)
2289 self.assertEqual('cbfs', ent.name)
2290 self.assertEqual('cbfs', ent.etype)
2291 self.assertEqual(0x400, ent.size)
2292 self.assertEqual(0x100, ent.image_pos)
2293 self.assertEqual(None, ent.uncomp_size)
2294 self.assertEqual(0, ent.offset)
2295
2296 ent = entries[4]
2297 self.assertEqual(3, ent.indent)
2298 self.assertEqual('u-boot', ent.name)
2299 self.assertEqual('u-boot', ent.etype)
2300 self.assertEqual(len(U_BOOT_DATA), ent.size)
2301 self.assertEqual(0x138, ent.image_pos)
2302 self.assertEqual(None, ent.uncomp_size)
2303 self.assertEqual(0x38, ent.offset)
2304
2305 ent = entries[5]
2306 self.assertEqual(3, ent.indent)
2307 self.assertEqual('u-boot-dtb', ent.name)
2308 self.assertEqual('text', ent.etype)
2309 self.assertGreater(len(COMPRESS_DATA), ent.size)
2310 self.assertEqual(0x178, ent.image_pos)
2311 self.assertEqual(len(COMPRESS_DATA), ent.uncomp_size)
2312 self.assertEqual(0x78, ent.offset)
2313
2314 ent = entries[6]
2315 self.assertEqual(2, ent.indent)
2316 self.assertEqual('u-boot-dtb', ent.name)
2317 self.assertEqual('u-boot-dtb', ent.etype)
2318 self.assertEqual(0x500, ent.image_pos)
2319 self.assertEqual(len(U_BOOT_DTB_DATA), ent.uncomp_size)
2320 dtb_size = ent.size
2321 # Compressing this data expands it since headers are added
2322 self.assertGreater(dtb_size, len(U_BOOT_DTB_DATA))
2323 self.assertEqual(0x400, ent.offset)
2324
2325 self.assertEqual(len(data), 0x100 + section_size)
2326 self.assertEqual(section_size, 0x400 + dtb_size)
2327
Simon Glasse1925fa2019-07-08 14:25:44 -06002328 def testFindFdtmap(self):
2329 """Test locating an FDT map in an image"""
2330 self._CheckLz4()
2331 data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
2332 image = control.images['image']
2333 entries = image.GetEntries()
2334 entry = entries['fdtmap']
2335 self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data))
2336
2337 def testFindFdtmapMissing(self):
2338 """Test failing to locate an FDP map"""
2339 data = self._DoReadFile('005_simple.dts')
2340 self.assertEqual(None, fdtmap.LocateFdtmap(data))
2341
Simon Glass2d260032019-07-08 14:25:45 -06002342 def testFindImageHeader(self):
2343 """Test locating a image header"""
2344 self._CheckLz4()
Simon Glassffded752019-07-08 14:25:46 -06002345 data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
Simon Glass2d260032019-07-08 14:25:45 -06002346 image = control.images['image']
2347 entries = image.GetEntries()
2348 entry = entries['fdtmap']
2349 # The header should point to the FDT map
2350 self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data))
2351
2352 def testFindImageHeaderStart(self):
2353 """Test locating a image header located at the start of an image"""
Simon Glassffded752019-07-08 14:25:46 -06002354 data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts')
Simon Glass2d260032019-07-08 14:25:45 -06002355 image = control.images['image']
2356 entries = image.GetEntries()
2357 entry = entries['fdtmap']
2358 # The header should point to the FDT map
2359 self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data))
2360
2361 def testFindImageHeaderMissing(self):
2362 """Test failing to locate an image header"""
2363 data = self._DoReadFile('005_simple.dts')
2364 self.assertEqual(None, image_header.LocateHeaderOffset(data))
2365
Simon Glassffded752019-07-08 14:25:46 -06002366 def testReadImage(self):
2367 """Test reading an image and accessing its FDT map"""
2368 self._CheckLz4()
2369 data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
2370 image_fname = tools.GetOutputFilename('image.bin')
2371 orig_image = control.images['image']
2372 image = Image.FromFile(image_fname)
2373 self.assertEqual(orig_image.GetEntries().keys(),
2374 image.GetEntries().keys())
2375
2376 orig_entry = orig_image.GetEntries()['fdtmap']
2377 entry = image.GetEntries()['fdtmap']
2378 self.assertEquals(orig_entry.offset, entry.offset)
2379 self.assertEquals(orig_entry.size, entry.size)
2380 self.assertEquals(orig_entry.image_pos, entry.image_pos)
2381
2382 def testReadImageNoHeader(self):
2383 """Test accessing an image's FDT map without an image header"""
2384 self._CheckLz4()
2385 data = self._DoReadFileRealDtb('129_decode_image_nohdr.dts')
2386 image_fname = tools.GetOutputFilename('image.bin')
2387 image = Image.FromFile(image_fname)
2388 self.assertTrue(isinstance(image, Image))
Simon Glass10f9d002019-07-20 12:23:50 -06002389 self.assertEqual('image', image.image_name[-5:])
Simon Glassffded752019-07-08 14:25:46 -06002390
2391 def testReadImageFail(self):
2392 """Test failing to read an image image's FDT map"""
2393 self._DoReadFile('005_simple.dts')
2394 image_fname = tools.GetOutputFilename('image.bin')
2395 with self.assertRaises(ValueError) as e:
2396 image = Image.FromFile(image_fname)
2397 self.assertIn("Cannot find FDT map in image", str(e.exception))
Simon Glasse073d4e2019-07-08 13:18:56 -06002398
Simon Glass61f564d2019-07-08 14:25:48 -06002399 def testListCmd(self):
2400 """Test listing the files in an image using an Fdtmap"""
2401 self._CheckLz4()
2402 data = self._DoReadFileRealDtb('130_list_fdtmap.dts')
2403
2404 # lz4 compression size differs depending on the version
2405 image = control.images['image']
2406 entries = image.GetEntries()
2407 section_size = entries['section'].size
2408 fdt_size = entries['section'].GetEntries()['u-boot-dtb'].size
2409 fdtmap_offset = entries['fdtmap'].offset
2410
Simon Glassf86a7362019-07-20 12:24:10 -06002411 try:
2412 tmpdir, updated_fname = self._SetupImageInTmpdir()
2413 with test_util.capture_sys_output() as (stdout, stderr):
2414 self._DoBinman('ls', '-i', updated_fname)
2415 finally:
2416 shutil.rmtree(tmpdir)
Simon Glass61f564d2019-07-08 14:25:48 -06002417 lines = stdout.getvalue().splitlines()
2418 expected = [
2419'Name Image-pos Size Entry-type Offset Uncomp-size',
2420'----------------------------------------------------------------------',
2421'main-section 0 c00 section 0',
2422' u-boot 0 4 u-boot 0',
2423' section 100 %x section 100' % section_size,
2424' cbfs 100 400 cbfs 0',
2425' u-boot 138 4 u-boot 38',
2426' u-boot-dtb 180 10f u-boot-dtb 80 3c9',
2427' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size,
Simon Glass1411ac82019-07-20 12:23:44 -06002428' fdtmap %x 3b4 fdtmap %x' %
Simon Glass61f564d2019-07-08 14:25:48 -06002429 (fdtmap_offset, fdtmap_offset),
2430' image-header bf8 8 image-header bf8',
2431 ]
2432 self.assertEqual(expected, lines)
2433
2434 def testListCmdFail(self):
2435 """Test failing to list an image"""
2436 self._DoReadFile('005_simple.dts')
Simon Glassf86a7362019-07-20 12:24:10 -06002437 try:
2438 tmpdir, updated_fname = self._SetupImageInTmpdir()
2439 with self.assertRaises(ValueError) as e:
2440 self._DoBinman('ls', '-i', updated_fname)
2441 finally:
2442 shutil.rmtree(tmpdir)
Simon Glass61f564d2019-07-08 14:25:48 -06002443 self.assertIn("Cannot find FDT map in image", str(e.exception))
2444
2445 def _RunListCmd(self, paths, expected):
2446 """List out entries and check the result
2447
2448 Args:
2449 paths: List of paths to pass to the list command
2450 expected: Expected list of filenames to be returned, in order
2451 """
2452 self._CheckLz4()
2453 self._DoReadFileRealDtb('130_list_fdtmap.dts')
2454 image_fname = tools.GetOutputFilename('image.bin')
2455 image = Image.FromFile(image_fname)
2456 lines = image.GetListEntries(paths)[1]
2457 files = [line[0].strip() for line in lines[1:]]
2458 self.assertEqual(expected, files)
2459
2460 def testListCmdSection(self):
2461 """Test listing the files in a section"""
2462 self._RunListCmd(['section'],
2463 ['section', 'cbfs', 'u-boot', 'u-boot-dtb', 'u-boot-dtb'])
2464
2465 def testListCmdFile(self):
2466 """Test listing a particular file"""
2467 self._RunListCmd(['*u-boot-dtb'], ['u-boot-dtb', 'u-boot-dtb'])
2468
2469 def testListCmdWildcard(self):
2470 """Test listing a wildcarded file"""
2471 self._RunListCmd(['*boot*'],
2472 ['u-boot', 'u-boot', 'u-boot-dtb', 'u-boot-dtb'])
2473
2474 def testListCmdWildcardMulti(self):
2475 """Test listing a wildcarded file"""
2476 self._RunListCmd(['*cb*', '*head*'],
2477 ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header'])
2478
2479 def testListCmdEmpty(self):
2480 """Test listing a wildcarded file"""
2481 self._RunListCmd(['nothing'], [])
2482
2483 def testListCmdPath(self):
2484 """Test listing the files in a sub-entry of a section"""
2485 self._RunListCmd(['section/cbfs'], ['cbfs', 'u-boot', 'u-boot-dtb'])
2486
Simon Glassf667e452019-07-08 14:25:50 -06002487 def _RunExtractCmd(self, entry_name, decomp=True):
2488 """Extract an entry from an image
2489
2490 Args:
2491 entry_name: Entry name to extract
2492 decomp: True to decompress the data if compressed, False to leave
2493 it in its raw uncompressed format
2494
2495 Returns:
2496 data from entry
2497 """
2498 self._CheckLz4()
2499 self._DoReadFileRealDtb('130_list_fdtmap.dts')
2500 image_fname = tools.GetOutputFilename('image.bin')
2501 return control.ReadEntry(image_fname, entry_name, decomp)
2502
2503 def testExtractSimple(self):
2504 """Test extracting a single file"""
2505 data = self._RunExtractCmd('u-boot')
2506 self.assertEqual(U_BOOT_DATA, data)
2507
Simon Glass71ce0ba2019-07-08 14:25:52 -06002508 def testExtractSection(self):
2509 """Test extracting the files in a section"""
2510 data = self._RunExtractCmd('section')
2511 cbfs_data = data[:0x400]
2512 cbfs = cbfs_util.CbfsReader(cbfs_data)
2513 self.assertEqual(['u-boot', 'u-boot-dtb', ''], cbfs.files.keys())
2514 dtb_data = data[0x400:]
2515 dtb = self._decompress(dtb_data)
2516 self.assertEqual(EXTRACT_DTB_SIZE, len(dtb))
2517
2518 def testExtractCompressed(self):
2519 """Test extracting compressed data"""
2520 data = self._RunExtractCmd('section/u-boot-dtb')
2521 self.assertEqual(EXTRACT_DTB_SIZE, len(data))
2522
2523 def testExtractRaw(self):
2524 """Test extracting compressed data without decompressing it"""
2525 data = self._RunExtractCmd('section/u-boot-dtb', decomp=False)
2526 dtb = self._decompress(data)
2527 self.assertEqual(EXTRACT_DTB_SIZE, len(dtb))
2528
2529 def testExtractCbfs(self):
2530 """Test extracting CBFS data"""
2531 data = self._RunExtractCmd('section/cbfs/u-boot')
2532 self.assertEqual(U_BOOT_DATA, data)
2533
2534 def testExtractCbfsCompressed(self):
2535 """Test extracting CBFS compressed data"""
2536 data = self._RunExtractCmd('section/cbfs/u-boot-dtb')
2537 self.assertEqual(EXTRACT_DTB_SIZE, len(data))
2538
2539 def testExtractCbfsRaw(self):
2540 """Test extracting CBFS compressed data without decompressing it"""
2541 data = self._RunExtractCmd('section/cbfs/u-boot-dtb', decomp=False)
Simon Glasseb0f4a42019-07-20 12:24:06 -06002542 dtb = tools.Decompress(data, 'lzma', with_header=False)
Simon Glass71ce0ba2019-07-08 14:25:52 -06002543 self.assertEqual(EXTRACT_DTB_SIZE, len(dtb))
2544
Simon Glassf667e452019-07-08 14:25:50 -06002545 def testExtractBadEntry(self):
2546 """Test extracting a bad section path"""
2547 with self.assertRaises(ValueError) as e:
2548 self._RunExtractCmd('section/does-not-exist')
2549 self.assertIn("Entry 'does-not-exist' not found in '/section'",
2550 str(e.exception))
2551
2552 def testExtractMissingFile(self):
2553 """Test extracting file that does not exist"""
2554 with self.assertRaises(IOError) as e:
2555 control.ReadEntry('missing-file', 'name')
2556
2557 def testExtractBadFile(self):
2558 """Test extracting an invalid file"""
2559 fname = os.path.join(self._indir, 'badfile')
2560 tools.WriteFile(fname, b'')
2561 with self.assertRaises(ValueError) as e:
2562 control.ReadEntry(fname, 'name')
2563
Simon Glass71ce0ba2019-07-08 14:25:52 -06002564 def testExtractCmd(self):
2565 """Test extracting a file fron an image on the command line"""
2566 self._CheckLz4()
2567 self._DoReadFileRealDtb('130_list_fdtmap.dts')
Simon Glass71ce0ba2019-07-08 14:25:52 -06002568 fname = os.path.join(self._indir, 'output.extact')
Simon Glassf86a7362019-07-20 12:24:10 -06002569 try:
2570 tmpdir, updated_fname = self._SetupImageInTmpdir()
2571 with test_util.capture_sys_output() as (stdout, stderr):
2572 self._DoBinman('extract', '-i', updated_fname, 'u-boot',
2573 '-f', fname)
2574 finally:
2575 shutil.rmtree(tmpdir)
Simon Glass71ce0ba2019-07-08 14:25:52 -06002576 data = tools.ReadFile(fname)
2577 self.assertEqual(U_BOOT_DATA, data)
2578
2579 def testExtractOneEntry(self):
2580 """Test extracting a single entry fron an image """
2581 self._CheckLz4()
2582 self._DoReadFileRealDtb('130_list_fdtmap.dts')
2583 image_fname = tools.GetOutputFilename('image.bin')
2584 fname = os.path.join(self._indir, 'output.extact')
2585 control.ExtractEntries(image_fname, fname, None, ['u-boot'])
2586 data = tools.ReadFile(fname)
2587 self.assertEqual(U_BOOT_DATA, data)
2588
2589 def _CheckExtractOutput(self, decomp):
2590 """Helper to test file output with and without decompression
2591
2592 Args:
2593 decomp: True to decompress entry data, False to output it raw
2594 """
2595 def _CheckPresent(entry_path, expect_data, expect_size=None):
2596 """Check and remove expected file
2597
2598 This checks the data/size of a file and removes the file both from
2599 the outfiles set and from the output directory. Once all files are
2600 processed, both the set and directory should be empty.
2601
2602 Args:
2603 entry_path: Entry path
2604 expect_data: Data to expect in file, or None to skip check
2605 expect_size: Size of data to expect in file, or None to skip
2606 """
2607 path = os.path.join(outdir, entry_path)
2608 data = tools.ReadFile(path)
2609 os.remove(path)
2610 if expect_data:
2611 self.assertEqual(expect_data, data)
2612 elif expect_size:
2613 self.assertEqual(expect_size, len(data))
2614 outfiles.remove(path)
2615
2616 def _CheckDirPresent(name):
2617 """Remove expected directory
2618
2619 This gives an error if the directory does not exist as expected
2620
2621 Args:
2622 name: Name of directory to remove
2623 """
2624 path = os.path.join(outdir, name)
2625 os.rmdir(path)
2626
2627 self._DoReadFileRealDtb('130_list_fdtmap.dts')
2628 image_fname = tools.GetOutputFilename('image.bin')
2629 outdir = os.path.join(self._indir, 'extract')
2630 einfos = control.ExtractEntries(image_fname, None, outdir, [], decomp)
2631
2632 # Create a set of all file that were output (should be 9)
2633 outfiles = set()
2634 for root, dirs, files in os.walk(outdir):
2635 outfiles |= set([os.path.join(root, fname) for fname in files])
2636 self.assertEqual(9, len(outfiles))
2637 self.assertEqual(9, len(einfos))
2638
2639 image = control.images['image']
2640 entries = image.GetEntries()
2641
2642 # Check the 9 files in various ways
2643 section = entries['section']
2644 section_entries = section.GetEntries()
2645 cbfs_entries = section_entries['cbfs'].GetEntries()
2646 _CheckPresent('u-boot', U_BOOT_DATA)
2647 _CheckPresent('section/cbfs/u-boot', U_BOOT_DATA)
2648 dtb_len = EXTRACT_DTB_SIZE
2649 if not decomp:
2650 dtb_len = cbfs_entries['u-boot-dtb'].size
2651 _CheckPresent('section/cbfs/u-boot-dtb', None, dtb_len)
2652 if not decomp:
2653 dtb_len = section_entries['u-boot-dtb'].size
2654 _CheckPresent('section/u-boot-dtb', None, dtb_len)
2655
2656 fdtmap = entries['fdtmap']
2657 _CheckPresent('fdtmap', fdtmap.data)
2658 hdr = entries['image-header']
2659 _CheckPresent('image-header', hdr.data)
2660
2661 _CheckPresent('section/root', section.data)
2662 cbfs = section_entries['cbfs']
2663 _CheckPresent('section/cbfs/root', cbfs.data)
2664 data = tools.ReadFile(image_fname)
2665 _CheckPresent('root', data)
2666
2667 # There should be no files left. Remove all the directories to check.
2668 # If there are any files/dirs remaining, one of these checks will fail.
2669 self.assertEqual(0, len(outfiles))
2670 _CheckDirPresent('section/cbfs')
2671 _CheckDirPresent('section')
2672 _CheckDirPresent('')
2673 self.assertFalse(os.path.exists(outdir))
2674
2675 def testExtractAllEntries(self):
2676 """Test extracting all entries"""
2677 self._CheckLz4()
2678 self._CheckExtractOutput(decomp=True)
2679
2680 def testExtractAllEntriesRaw(self):
2681 """Test extracting all entries without decompressing them"""
2682 self._CheckLz4()
2683 self._CheckExtractOutput(decomp=False)
2684
2685 def testExtractSelectedEntries(self):
2686 """Test extracting some entries"""
2687 self._CheckLz4()
2688 self._DoReadFileRealDtb('130_list_fdtmap.dts')
2689 image_fname = tools.GetOutputFilename('image.bin')
2690 outdir = os.path.join(self._indir, 'extract')
2691 einfos = control.ExtractEntries(image_fname, None, outdir,
2692 ['*cb*', '*head*'])
2693
2694 # File output is tested by testExtractAllEntries(), so just check that
2695 # the expected entries are selected
2696 names = [einfo.name for einfo in einfos]
2697 self.assertEqual(names,
2698 ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header'])
2699
2700 def testExtractNoEntryPaths(self):
2701 """Test extracting some entries"""
2702 self._CheckLz4()
2703 self._DoReadFileRealDtb('130_list_fdtmap.dts')
2704 image_fname = tools.GetOutputFilename('image.bin')
2705 with self.assertRaises(ValueError) as e:
2706 control.ExtractEntries(image_fname, 'fname', None, [])
Simon Glassbb5edc12019-07-20 12:24:14 -06002707 self.assertIn('Must specify an entry path to write with -f',
Simon Glass71ce0ba2019-07-08 14:25:52 -06002708 str(e.exception))
2709
2710 def testExtractTooManyEntryPaths(self):
2711 """Test extracting some entries"""
2712 self._CheckLz4()
2713 self._DoReadFileRealDtb('130_list_fdtmap.dts')
2714 image_fname = tools.GetOutputFilename('image.bin')
2715 with self.assertRaises(ValueError) as e:
2716 control.ExtractEntries(image_fname, 'fname', None, ['a', 'b'])
Simon Glassbb5edc12019-07-20 12:24:14 -06002717 self.assertIn('Must specify exactly one entry path to write with -f',
Simon Glass71ce0ba2019-07-08 14:25:52 -06002718 str(e.exception))
2719
Simon Glasse2705fa2019-07-08 14:25:53 -06002720 def testPackAlignSection(self):
2721 """Test that sections can have alignment"""
2722 self._DoReadFile('131_pack_align_section.dts')
2723
2724 self.assertIn('image', control.images)
2725 image = control.images['image']
2726 entries = image.GetEntries()
2727 self.assertEqual(3, len(entries))
2728
2729 # First u-boot
2730 self.assertIn('u-boot', entries)
2731 entry = entries['u-boot']
2732 self.assertEqual(0, entry.offset)
2733 self.assertEqual(0, entry.image_pos)
2734 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
2735 self.assertEqual(len(U_BOOT_DATA), entry.size)
2736
2737 # Section0
2738 self.assertIn('section0', entries)
2739 section0 = entries['section0']
2740 self.assertEqual(0x10, section0.offset)
2741 self.assertEqual(0x10, section0.image_pos)
2742 self.assertEqual(len(U_BOOT_DATA), section0.size)
2743
2744 # Second u-boot
2745 section_entries = section0.GetEntries()
2746 self.assertIn('u-boot', section_entries)
2747 entry = section_entries['u-boot']
2748 self.assertEqual(0, entry.offset)
2749 self.assertEqual(0x10, entry.image_pos)
2750 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
2751 self.assertEqual(len(U_BOOT_DATA), entry.size)
2752
2753 # Section1
2754 self.assertIn('section1', entries)
2755 section1 = entries['section1']
2756 self.assertEqual(0x14, section1.offset)
2757 self.assertEqual(0x14, section1.image_pos)
2758 self.assertEqual(0x20, section1.size)
2759
2760 # Second u-boot
2761 section_entries = section1.GetEntries()
2762 self.assertIn('u-boot', section_entries)
2763 entry = section_entries['u-boot']
2764 self.assertEqual(0, entry.offset)
2765 self.assertEqual(0x14, entry.image_pos)
2766 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
2767 self.assertEqual(len(U_BOOT_DATA), entry.size)
2768
2769 # Section2
2770 self.assertIn('section2', section_entries)
2771 section2 = section_entries['section2']
2772 self.assertEqual(0x4, section2.offset)
2773 self.assertEqual(0x18, section2.image_pos)
2774 self.assertEqual(4, section2.size)
2775
2776 # Third u-boot
2777 section_entries = section2.GetEntries()
2778 self.assertIn('u-boot', section_entries)
2779 entry = section_entries['u-boot']
2780 self.assertEqual(0, entry.offset)
2781 self.assertEqual(0x18, entry.image_pos)
2782 self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
2783 self.assertEqual(len(U_BOOT_DATA), entry.size)
2784
Simon Glass51014aa2019-07-20 12:23:56 -06002785 def _RunReplaceCmd(self, entry_name, data, decomp=True, allow_resize=True,
2786 dts='132_replace.dts'):
Simon Glass10f9d002019-07-20 12:23:50 -06002787 """Replace an entry in an image
2788
2789 This writes the entry data to update it, then opens the updated file and
2790 returns the value that it now finds there.
2791
2792 Args:
2793 entry_name: Entry name to replace
2794 data: Data to replace it with
2795 decomp: True to compress the data if needed, False if data is
2796 already compressed so should be used as is
Simon Glass51014aa2019-07-20 12:23:56 -06002797 allow_resize: True to allow entries to change size, False to raise
2798 an exception
Simon Glass10f9d002019-07-20 12:23:50 -06002799
2800 Returns:
2801 Tuple:
2802 data from entry
2803 data from fdtmap (excluding header)
Simon Glass51014aa2019-07-20 12:23:56 -06002804 Image object that was modified
Simon Glass10f9d002019-07-20 12:23:50 -06002805 """
Simon Glass51014aa2019-07-20 12:23:56 -06002806 dtb_data = self._DoReadFileDtb(dts, use_real_dtb=True,
Simon Glass10f9d002019-07-20 12:23:50 -06002807 update_dtb=True)[1]
2808
2809 self.assertIn('image', control.images)
2810 image = control.images['image']
2811 entries = image.GetEntries()
2812 orig_dtb_data = entries['u-boot-dtb'].data
2813 orig_fdtmap_data = entries['fdtmap'].data
2814
2815 image_fname = tools.GetOutputFilename('image.bin')
2816 updated_fname = tools.GetOutputFilename('image-updated.bin')
2817 tools.WriteFile(updated_fname, tools.ReadFile(image_fname))
Simon Glass51014aa2019-07-20 12:23:56 -06002818 image = control.WriteEntry(updated_fname, entry_name, data, decomp,
2819 allow_resize)
Simon Glass10f9d002019-07-20 12:23:50 -06002820 data = control.ReadEntry(updated_fname, entry_name, decomp)
2821
Simon Glass51014aa2019-07-20 12:23:56 -06002822 # The DT data should not change unless resized:
2823 if not allow_resize:
2824 new_dtb_data = entries['u-boot-dtb'].data
2825 self.assertEqual(new_dtb_data, orig_dtb_data)
2826 new_fdtmap_data = entries['fdtmap'].data
2827 self.assertEqual(new_fdtmap_data, orig_fdtmap_data)
Simon Glass10f9d002019-07-20 12:23:50 -06002828
Simon Glass51014aa2019-07-20 12:23:56 -06002829 return data, orig_fdtmap_data[fdtmap.FDTMAP_HDR_LEN:], image
Simon Glass10f9d002019-07-20 12:23:50 -06002830
2831 def testReplaceSimple(self):
2832 """Test replacing a single file"""
2833 expected = b'x' * len(U_BOOT_DATA)
Simon Glass51014aa2019-07-20 12:23:56 -06002834 data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', expected,
2835 allow_resize=False)
Simon Glass10f9d002019-07-20 12:23:50 -06002836 self.assertEqual(expected, data)
2837
2838 # Test that the state looks right. There should be an FDT for the fdtmap
2839 # that we jsut read back in, and it should match what we find in the
2840 # 'control' tables. Checking for an FDT that does not exist should
2841 # return None.
2842 path, fdtmap = state.GetFdtContents('fdtmap')
Simon Glass51014aa2019-07-20 12:23:56 -06002843 self.assertIsNotNone(path)
Simon Glass10f9d002019-07-20 12:23:50 -06002844 self.assertEqual(expected_fdtmap, fdtmap)
2845
2846 dtb = state.GetFdtForEtype('fdtmap')
2847 self.assertEqual(dtb.GetContents(), fdtmap)
2848
2849 missing_path, missing_fdtmap = state.GetFdtContents('missing')
2850 self.assertIsNone(missing_path)
2851 self.assertIsNone(missing_fdtmap)
2852
2853 missing_dtb = state.GetFdtForEtype('missing')
2854 self.assertIsNone(missing_dtb)
2855
2856 self.assertEqual('/binman', state.fdt_path_prefix)
2857
2858 def testReplaceResizeFail(self):
2859 """Test replacing a file by something larger"""
2860 expected = U_BOOT_DATA + b'x'
2861 with self.assertRaises(ValueError) as e:
Simon Glass51014aa2019-07-20 12:23:56 -06002862 self._RunReplaceCmd('u-boot', expected, allow_resize=False,
2863 dts='139_replace_repack.dts')
Simon Glass10f9d002019-07-20 12:23:50 -06002864 self.assertIn("Node '/u-boot': Entry data size does not match, but resize is disabled",
2865 str(e.exception))
2866
2867 def testReplaceMulti(self):
2868 """Test replacing entry data where multiple images are generated"""
2869 data = self._DoReadFileDtb('133_replace_multi.dts', use_real_dtb=True,
2870 update_dtb=True)[0]
2871 expected = b'x' * len(U_BOOT_DATA)
2872 updated_fname = tools.GetOutputFilename('image-updated.bin')
2873 tools.WriteFile(updated_fname, data)
2874 entry_name = 'u-boot'
Simon Glass51014aa2019-07-20 12:23:56 -06002875 control.WriteEntry(updated_fname, entry_name, expected,
2876 allow_resize=False)
Simon Glass10f9d002019-07-20 12:23:50 -06002877 data = control.ReadEntry(updated_fname, entry_name)
2878 self.assertEqual(expected, data)
2879
2880 # Check the state looks right.
2881 self.assertEqual('/binman/image', state.fdt_path_prefix)
2882
2883 # Now check we can write the first image
2884 image_fname = tools.GetOutputFilename('first-image.bin')
2885 updated_fname = tools.GetOutputFilename('first-updated.bin')
2886 tools.WriteFile(updated_fname, tools.ReadFile(image_fname))
2887 entry_name = 'u-boot'
Simon Glass51014aa2019-07-20 12:23:56 -06002888 control.WriteEntry(updated_fname, entry_name, expected,
2889 allow_resize=False)
Simon Glass10f9d002019-07-20 12:23:50 -06002890 data = control.ReadEntry(updated_fname, entry_name)
2891 self.assertEqual(expected, data)
2892
2893 # Check the state looks right.
2894 self.assertEqual('/binman/first-image', state.fdt_path_prefix)
Simon Glass8beb11e2019-07-08 14:25:47 -06002895
Simon Glass12bb1a92019-07-20 12:23:51 -06002896 def testUpdateFdtAllRepack(self):
2897 """Test that all device trees are updated with offset/size info"""
2898 data = self._DoReadFileRealDtb('134_fdt_update_all_repack.dts')
2899 SECTION_SIZE = 0x300
2900 DTB_SIZE = 602
2901 FDTMAP_SIZE = 608
2902 base_expected = {
2903 'offset': 0,
2904 'size': SECTION_SIZE + DTB_SIZE * 2 + FDTMAP_SIZE,
2905 'image-pos': 0,
2906 'section:offset': 0,
2907 'section:size': SECTION_SIZE,
2908 'section:image-pos': 0,
2909 'section/u-boot-dtb:offset': 4,
2910 'section/u-boot-dtb:size': 636,
2911 'section/u-boot-dtb:image-pos': 4,
2912 'u-boot-spl-dtb:offset': SECTION_SIZE,
2913 'u-boot-spl-dtb:size': DTB_SIZE,
2914 'u-boot-spl-dtb:image-pos': SECTION_SIZE,
2915 'u-boot-tpl-dtb:offset': SECTION_SIZE + DTB_SIZE,
2916 'u-boot-tpl-dtb:image-pos': SECTION_SIZE + DTB_SIZE,
2917 'u-boot-tpl-dtb:size': DTB_SIZE,
2918 'fdtmap:offset': SECTION_SIZE + DTB_SIZE * 2,
2919 'fdtmap:size': FDTMAP_SIZE,
2920 'fdtmap:image-pos': SECTION_SIZE + DTB_SIZE * 2,
2921 }
2922 main_expected = {
2923 'section:orig-size': SECTION_SIZE,
2924 'section/u-boot-dtb:orig-offset': 4,
2925 }
2926
2927 # We expect three device-tree files in the output, with the first one
2928 # within a fixed-size section.
2929 # Read them in sequence. We look for an 'spl' property in the SPL tree,
2930 # and 'tpl' in the TPL tree, to make sure they are distinct from the
2931 # main U-Boot tree. All three should have the same positions and offset
2932 # except that the main tree should include the main_expected properties
2933 start = 4
2934 for item in ['', 'spl', 'tpl', None]:
2935 if item is None:
2936 start += 16 # Move past fdtmap header
2937 dtb = fdt.Fdt.FromData(data[start:])
2938 dtb.Scan()
2939 props = self._GetPropTree(dtb,
2940 BASE_DTB_PROPS + REPACK_DTB_PROPS + ['spl', 'tpl'],
2941 prefix='/' if item is None else '/binman/')
2942 expected = dict(base_expected)
2943 if item:
2944 expected[item] = 0
2945 else:
2946 # Main DTB and fdtdec should include the 'orig-' properties
2947 expected.update(main_expected)
2948 # Helpful for debugging:
2949 #for prop in sorted(props):
2950 #print('prop %s %s %s' % (prop, props[prop], expected[prop]))
2951 self.assertEqual(expected, props)
2952 if item == '':
2953 start = SECTION_SIZE
2954 else:
2955 start += dtb._fdt_obj.totalsize()
2956
Simon Glasseba1f0c2019-07-20 12:23:55 -06002957 def testFdtmapHeaderMiddle(self):
2958 """Test an FDT map in the middle of an image when it should be at end"""
2959 with self.assertRaises(ValueError) as e:
2960 self._DoReadFileRealDtb('135_fdtmap_hdr_middle.dts')
2961 self.assertIn("Invalid sibling order 'middle' for image-header: Must be at 'end' to match location",
2962 str(e.exception))
2963
2964 def testFdtmapHeaderStartBad(self):
2965 """Test an FDT map in middle of an image when it should be at start"""
2966 with self.assertRaises(ValueError) as e:
2967 self._DoReadFileRealDtb('136_fdtmap_hdr_startbad.dts')
2968 self.assertIn("Invalid sibling order 'end' for image-header: Must be at 'start' to match location",
2969 str(e.exception))
2970
2971 def testFdtmapHeaderEndBad(self):
2972 """Test an FDT map at the start of an image when it should be at end"""
2973 with self.assertRaises(ValueError) as e:
2974 self._DoReadFileRealDtb('137_fdtmap_hdr_endbad.dts')
2975 self.assertIn("Invalid sibling order 'start' for image-header: Must be at 'end' to match location",
2976 str(e.exception))
2977
2978 def testFdtmapHeaderNoSize(self):
2979 """Test an image header at the end of an image with undefined size"""
2980 self._DoReadFileRealDtb('138_fdtmap_hdr_nosize.dts')
2981
Simon Glass51014aa2019-07-20 12:23:56 -06002982 def testReplaceResize(self):
2983 """Test replacing a single file in an entry with a larger file"""
2984 expected = U_BOOT_DATA + b'x'
2985 data, _, image = self._RunReplaceCmd('u-boot', expected,
2986 dts='139_replace_repack.dts')
2987 self.assertEqual(expected, data)
2988
2989 entries = image.GetEntries()
2990 dtb_data = entries['u-boot-dtb'].data
2991 dtb = fdt.Fdt.FromData(dtb_data)
2992 dtb.Scan()
2993
2994 # The u-boot section should now be larger in the dtb
2995 node = dtb.GetNode('/binman/u-boot')
2996 self.assertEqual(len(expected), fdt_util.GetInt(node, 'size'))
2997
2998 # Same for the fdtmap
2999 fdata = entries['fdtmap'].data
3000 fdtb = fdt.Fdt.FromData(fdata[fdtmap.FDTMAP_HDR_LEN:])
3001 fdtb.Scan()
3002 fnode = fdtb.GetNode('/u-boot')
3003 self.assertEqual(len(expected), fdt_util.GetInt(fnode, 'size'))
3004
3005 def testReplaceResizeNoRepack(self):
3006 """Test replacing an entry with a larger file when not allowed"""
3007 expected = U_BOOT_DATA + b'x'
3008 with self.assertRaises(ValueError) as e:
3009 self._RunReplaceCmd('u-boot', expected)
3010 self.assertIn('Entry data size does not match, but allow-repack is not present for this image',
3011 str(e.exception))
3012
Simon Glass61ec04f2019-07-20 12:23:58 -06003013 def testEntryShrink(self):
3014 """Test contracting an entry after it is packed"""
3015 try:
3016 state.SetAllowEntryContraction(True)
3017 data = self._DoReadFileDtb('140_entry_shrink.dts',
3018 update_dtb=True)[0]
3019 finally:
3020 state.SetAllowEntryContraction(False)
3021 self.assertEqual(b'a', data[:1])
3022 self.assertEqual(U_BOOT_DATA, data[1:1 + len(U_BOOT_DATA)])
3023 self.assertEqual(b'a', data[-1:])
3024
3025 def testEntryShrinkFail(self):
3026 """Test not being allowed to contract an entry after it is packed"""
3027 data = self._DoReadFileDtb('140_entry_shrink.dts', update_dtb=True)[0]
3028
3029 # In this case there is a spare byte at the end of the data. The size of
3030 # the contents is only 1 byte but we still have the size before it
3031 # shrunk.
3032 self.assertEqual(b'a\0', data[:2])
3033 self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)])
3034 self.assertEqual(b'a\0', data[-2:])
3035
Simon Glass27145fd2019-07-20 12:24:01 -06003036 def testDescriptorOffset(self):
3037 """Test that the Intel descriptor is always placed at at the start"""
3038 data = self._DoReadFileDtb('141_descriptor_offset.dts')
3039 image = control.images['image']
3040 entries = image.GetEntries()
3041 desc = entries['intel-descriptor']
3042 self.assertEqual(0xff800000, desc.offset);
3043 self.assertEqual(0xff800000, desc.image_pos);
3044
Simon Glasseb0f4a42019-07-20 12:24:06 -06003045 def testReplaceCbfs(self):
3046 """Test replacing a single file in CBFS without changing the size"""
3047 self._CheckLz4()
3048 expected = b'x' * len(U_BOOT_DATA)
3049 data = self._DoReadFileRealDtb('142_replace_cbfs.dts')
3050 updated_fname = tools.GetOutputFilename('image-updated.bin')
3051 tools.WriteFile(updated_fname, data)
3052 entry_name = 'section/cbfs/u-boot'
3053 control.WriteEntry(updated_fname, entry_name, expected,
3054 allow_resize=True)
3055 data = control.ReadEntry(updated_fname, entry_name)
3056 self.assertEqual(expected, data)
3057
3058 def testReplaceResizeCbfs(self):
3059 """Test replacing a single file in CBFS with one of a different size"""
3060 self._CheckLz4()
3061 expected = U_BOOT_DATA + b'x'
3062 data = self._DoReadFileRealDtb('142_replace_cbfs.dts')
3063 updated_fname = tools.GetOutputFilename('image-updated.bin')
3064 tools.WriteFile(updated_fname, data)
3065 entry_name = 'section/cbfs/u-boot'
3066 control.WriteEntry(updated_fname, entry_name, expected,
3067 allow_resize=True)
3068 data = control.ReadEntry(updated_fname, entry_name)
3069 self.assertEqual(expected, data)
3070
Simon Glassa6cb9952019-07-20 12:24:15 -06003071 def _SetupForReplace(self):
3072 """Set up some files to use to replace entries
3073
3074 This generates an image, copies it to a new file, extracts all the files
3075 in it and updates some of them
3076
3077 Returns:
3078 List
3079 Image filename
3080 Output directory
3081 Expected values for updated entries, each a string
3082 """
3083 data = self._DoReadFileRealDtb('143_replace_all.dts')
3084
3085 updated_fname = tools.GetOutputFilename('image-updated.bin')
3086 tools.WriteFile(updated_fname, data)
3087
3088 outdir = os.path.join(self._indir, 'extract')
3089 einfos = control.ExtractEntries(updated_fname, None, outdir, [])
3090
3091 expected1 = b'x' + U_BOOT_DATA + b'y'
3092 u_boot_fname1 = os.path.join(outdir, 'u-boot')
3093 tools.WriteFile(u_boot_fname1, expected1)
3094
3095 expected2 = b'a' + U_BOOT_DATA + b'b'
3096 u_boot_fname2 = os.path.join(outdir, 'u-boot2')
3097 tools.WriteFile(u_boot_fname2, expected2)
3098
3099 expected_text = b'not the same text'
3100 text_fname = os.path.join(outdir, 'text')
3101 tools.WriteFile(text_fname, expected_text)
3102
3103 dtb_fname = os.path.join(outdir, 'u-boot-dtb')
3104 dtb = fdt.FdtScan(dtb_fname)
3105 node = dtb.GetNode('/binman/text')
3106 node.AddString('my-property', 'the value')
3107 dtb.Sync(auto_resize=True)
3108 dtb.Flush()
3109
3110 return updated_fname, outdir, expected1, expected2, expected_text
3111
3112 def _CheckReplaceMultiple(self, entry_paths):
3113 """Handle replacing the contents of multiple entries
3114
3115 Args:
3116 entry_paths: List of entry paths to replace
3117
3118 Returns:
3119 List
3120 Dict of entries in the image:
3121 key: Entry name
3122 Value: Entry object
3123 Expected values for updated entries, each a string
3124 """
3125 updated_fname, outdir, expected1, expected2, expected_text = (
3126 self._SetupForReplace())
3127 control.ReplaceEntries(updated_fname, None, outdir, entry_paths)
3128
3129 image = Image.FromFile(updated_fname)
3130 image.LoadData()
3131 return image.GetEntries(), expected1, expected2, expected_text
3132
3133 def testReplaceAll(self):
3134 """Test replacing the contents of all entries"""
3135 entries, expected1, expected2, expected_text = (
3136 self._CheckReplaceMultiple([]))
3137 data = entries['u-boot'].data
3138 self.assertEqual(expected1, data)
3139
3140 data = entries['u-boot2'].data
3141 self.assertEqual(expected2, data)
3142
3143 data = entries['text'].data
3144 self.assertEqual(expected_text, data)
3145
3146 # Check that the device tree is updated
3147 data = entries['u-boot-dtb'].data
3148 dtb = fdt.Fdt.FromData(data)
3149 dtb.Scan()
3150 node = dtb.GetNode('/binman/text')
3151 self.assertEqual('the value', node.props['my-property'].value)
3152
3153 def testReplaceSome(self):
3154 """Test replacing the contents of a few entries"""
3155 entries, expected1, expected2, expected_text = (
3156 self._CheckReplaceMultiple(['u-boot2', 'text']))
3157
3158 # This one should not change
3159 data = entries['u-boot'].data
3160 self.assertEqual(U_BOOT_DATA, data)
3161
3162 data = entries['u-boot2'].data
3163 self.assertEqual(expected2, data)
3164
3165 data = entries['text'].data
3166 self.assertEqual(expected_text, data)
3167
3168 def testReplaceCmd(self):
3169 """Test replacing a file fron an image on the command line"""
3170 self._DoReadFileRealDtb('143_replace_all.dts')
3171
3172 try:
3173 tmpdir, updated_fname = self._SetupImageInTmpdir()
3174
3175 fname = os.path.join(tmpdir, 'update-u-boot.bin')
3176 expected = b'x' * len(U_BOOT_DATA)
3177 tools.WriteFile(fname, expected)
3178
3179 self._DoBinman('replace', '-i', updated_fname, 'u-boot', '-f', fname)
3180 data = tools.ReadFile(updated_fname)
3181 self.assertEqual(expected, data[:len(expected)])
3182 map_fname = os.path.join(tmpdir, 'image-updated.map')
3183 self.assertFalse(os.path.exists(map_fname))
3184 finally:
3185 shutil.rmtree(tmpdir)
3186
3187 def testReplaceCmdSome(self):
3188 """Test replacing some files fron an image on the command line"""
3189 updated_fname, outdir, expected1, expected2, expected_text = (
3190 self._SetupForReplace())
3191
3192 self._DoBinman('replace', '-i', updated_fname, '-I', outdir,
3193 'u-boot2', 'text')
3194
3195 tools.PrepareOutputDir(None)
3196 image = Image.FromFile(updated_fname)
3197 image.LoadData()
3198 entries = image.GetEntries()
3199
3200 # This one should not change
3201 data = entries['u-boot'].data
3202 self.assertEqual(U_BOOT_DATA, data)
3203
3204 data = entries['u-boot2'].data
3205 self.assertEqual(expected2, data)
3206
3207 data = entries['text'].data
3208 self.assertEqual(expected_text, data)
3209
3210 def testReplaceMissing(self):
3211 """Test replacing entries where the file is missing"""
3212 updated_fname, outdir, expected1, expected2, expected_text = (
3213 self._SetupForReplace())
3214
3215 # Remove one of the files, to generate a warning
3216 u_boot_fname1 = os.path.join(outdir, 'u-boot')
3217 os.remove(u_boot_fname1)
3218
3219 with test_util.capture_sys_output() as (stdout, stderr):
3220 control.ReplaceEntries(updated_fname, None, outdir, [])
3221 self.assertIn("Skipping entry '/u-boot' from missing file",
3222 stdout.getvalue())
3223
3224 def testReplaceCmdMap(self):
3225 """Test replacing a file fron an image on the command line"""
3226 self._DoReadFileRealDtb('143_replace_all.dts')
3227
3228 try:
3229 tmpdir, updated_fname = self._SetupImageInTmpdir()
3230
3231 fname = os.path.join(self._indir, 'update-u-boot.bin')
3232 expected = b'x' * len(U_BOOT_DATA)
3233 tools.WriteFile(fname, expected)
3234
3235 self._DoBinman('replace', '-i', updated_fname, 'u-boot',
3236 '-f', fname, '-m')
3237 map_fname = os.path.join(tmpdir, 'image-updated.map')
3238 self.assertTrue(os.path.exists(map_fname))
3239 finally:
3240 shutil.rmtree(tmpdir)
3241
3242 def testReplaceNoEntryPaths(self):
3243 """Test replacing an entry without an entry path"""
3244 self._DoReadFileRealDtb('143_replace_all.dts')
3245 image_fname = tools.GetOutputFilename('image.bin')
3246 with self.assertRaises(ValueError) as e:
3247 control.ReplaceEntries(image_fname, 'fname', None, [])
3248 self.assertIn('Must specify an entry path to read with -f',
3249 str(e.exception))
3250
3251 def testReplaceTooManyEntryPaths(self):
3252 """Test extracting some entries"""
3253 self._DoReadFileRealDtb('143_replace_all.dts')
3254 image_fname = tools.GetOutputFilename('image.bin')
3255 with self.assertRaises(ValueError) as e:
3256 control.ReplaceEntries(image_fname, 'fname', None, ['a', 'b'])
3257 self.assertIn('Must specify exactly one entry path to write with -f',
3258 str(e.exception))
3259
Simon Glass2250ee62019-08-24 07:22:48 -06003260 def testPackReset16(self):
3261 """Test that an image with an x86 reset16 region can be created"""
3262 data = self._DoReadFile('144_x86_reset16.dts')
3263 self.assertEqual(X86_RESET16_DATA, data[:len(X86_RESET16_DATA)])
3264
3265 def testPackReset16Spl(self):
3266 """Test that an image with an x86 reset16-spl region can be created"""
3267 data = self._DoReadFile('145_x86_reset16_spl.dts')
3268 self.assertEqual(X86_RESET16_SPL_DATA, data[:len(X86_RESET16_SPL_DATA)])
3269
3270 def testPackReset16Tpl(self):
3271 """Test that an image with an x86 reset16-tpl region can be created"""
3272 data = self._DoReadFile('146_x86_reset16_tpl.dts')
3273 self.assertEqual(X86_RESET16_TPL_DATA, data[:len(X86_RESET16_TPL_DATA)])
3274
Simon Glass5af12072019-08-24 07:22:50 -06003275 def testPackIntelFit(self):
3276 """Test that an image with an Intel FIT and pointer can be created"""
3277 data = self._DoReadFile('147_intel_fit.dts')
3278 self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
3279 fit = data[16:32];
3280 self.assertEqual(b'_FIT_ \x01\x00\x00\x00\x00\x01\x80}' , fit)
3281 ptr = struct.unpack('<i', data[0x40:0x44])[0]
3282
3283 image = control.images['image']
3284 entries = image.GetEntries()
3285 expected_ptr = entries['intel-fit'].image_pos - (1 << 32)
3286 self.assertEqual(expected_ptr, ptr)
3287
3288 def testPackIntelFitMissing(self):
3289 """Test detection of a FIT pointer with not FIT region"""
3290 with self.assertRaises(ValueError) as e:
3291 self._DoReadFile('148_intel_fit_missing.dts')
3292 self.assertIn("'intel-fit-ptr' section must have an 'intel-fit' sibling",
3293 str(e.exception))
3294
Simon Glass12bb1a92019-07-20 12:23:51 -06003295
Simon Glass9fc60b42017-11-12 21:52:22 -07003296if __name__ == "__main__":
3297 unittest.main()