Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 5 | # To run a single test, change to this directory, and: |
| 6 | # |
| 7 | # python -m unittest func_test.TestFunctional.testHelp |
| 8 | |
| 9 | from optparse import OptionParser |
| 10 | import os |
| 11 | import shutil |
| 12 | import struct |
| 13 | import sys |
| 14 | import tempfile |
| 15 | import unittest |
| 16 | |
| 17 | import binman |
| 18 | import cmdline |
| 19 | import command |
| 20 | import control |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 21 | import elf |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 22 | import fdt |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 23 | import fdt_util |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 24 | import fmap_util |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 25 | import test_util |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 26 | import tools |
| 27 | import tout |
| 28 | |
| 29 | # Contents of test files, corresponding to different entry types |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 30 | U_BOOT_DATA = '1234' |
| 31 | U_BOOT_IMG_DATA = 'img' |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 32 | U_BOOT_SPL_DATA = '56780123456789abcde' |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 33 | U_BOOT_TPL_DATA = 'tpl' |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 34 | BLOB_DATA = '89' |
| 35 | ME_DATA = '0abcd' |
| 36 | VGA_DATA = 'vga' |
| 37 | U_BOOT_DTB_DATA = 'udtb' |
Simon Glass | 47419ea | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 38 | U_BOOT_SPL_DTB_DATA = 'spldtb' |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 39 | U_BOOT_TPL_DTB_DATA = 'tpldtb' |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 40 | X86_START16_DATA = 'start16' |
| 41 | X86_START16_SPL_DATA = 'start16spl' |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 42 | X86_START16_TPL_DATA = 'start16tpl' |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 43 | U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here' |
| 44 | U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here' |
| 45 | FSP_DATA = 'fsp' |
| 46 | CMC_DATA = 'cmc' |
| 47 | VBT_DATA = 'vbt' |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 48 | MRC_DATA = 'mrc' |
Simon Glass | bb74837 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 49 | TEXT_DATA = 'text' |
| 50 | TEXT_DATA2 = 'text2' |
| 51 | TEXT_DATA3 = 'text3' |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 52 | CROS_EC_RW_DATA = 'ecrw' |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 53 | GBB_DATA = 'gbbd' |
| 54 | BMPBLK_DATA = 'bmp' |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 55 | VBLOCK_DATA = 'vblk' |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 56 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 57 | |
| 58 | class TestFunctional(unittest.TestCase): |
| 59 | """Functional tests for binman |
| 60 | |
| 61 | Most of these use a sample .dts file to build an image and then check |
| 62 | that it looks correct. The sample files are in the test/ subdirectory |
| 63 | and are numbered. |
| 64 | |
| 65 | For each entry type a very small test file is created using fixed |
| 66 | string contents. This makes it easy to test that things look right, and |
| 67 | debug problems. |
| 68 | |
| 69 | In some cases a 'real' file must be used - these are also supplied in |
| 70 | the test/ diurectory. |
| 71 | """ |
| 72 | @classmethod |
| 73 | def setUpClass(self): |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 74 | global entry |
| 75 | import entry |
| 76 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 77 | # Handle the case where argv[0] is 'python' |
| 78 | self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 79 | self._binman_pathname = os.path.join(self._binman_dir, 'binman') |
| 80 | |
| 81 | # Create a temporary directory for input files |
| 82 | self._indir = tempfile.mkdtemp(prefix='binmant.') |
| 83 | |
| 84 | # Create some test files |
| 85 | TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) |
| 86 | TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) |
| 87 | TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 88 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 89 | TestFunctional._MakeInputFile('blobfile', BLOB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 90 | TestFunctional._MakeInputFile('me.bin', ME_DATA) |
| 91 | TestFunctional._MakeInputFile('vga.bin', VGA_DATA) |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 92 | self._ResetDtbs() |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 93 | TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA) |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 94 | TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin', |
| 95 | X86_START16_SPL_DATA) |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 96 | TestFunctional._MakeInputFile('tpl/u-boot-x86-16bit-tpl.bin', |
| 97 | X86_START16_TPL_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 98 | TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 99 | TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin', |
| 100 | U_BOOT_SPL_NODTB_DATA) |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 101 | TestFunctional._MakeInputFile('fsp.bin', FSP_DATA) |
| 102 | TestFunctional._MakeInputFile('cmc.bin', CMC_DATA) |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 103 | TestFunctional._MakeInputFile('vbt.bin', VBT_DATA) |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 104 | TestFunctional._MakeInputFile('mrc.bin', MRC_DATA) |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 105 | TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA) |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 106 | TestFunctional._MakeInputDir('devkeys') |
| 107 | TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 108 | self._output_setup = False |
| 109 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 110 | # ELF file with a '_dt_ucode_base_size' symbol |
| 111 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 112 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 113 | |
| 114 | # Intel flash descriptor file |
| 115 | with open(self.TestFile('descriptor.bin')) as fd: |
| 116 | TestFunctional._MakeInputFile('descriptor.bin', fd.read()) |
| 117 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 118 | @classmethod |
| 119 | def tearDownClass(self): |
| 120 | """Remove the temporary input directory and its contents""" |
| 121 | if self._indir: |
| 122 | shutil.rmtree(self._indir) |
| 123 | self._indir = None |
| 124 | |
| 125 | def setUp(self): |
| 126 | # Enable this to turn on debugging output |
| 127 | # tout.Init(tout.DEBUG) |
| 128 | command.test_result = None |
| 129 | |
| 130 | def tearDown(self): |
| 131 | """Remove the temporary output directory""" |
| 132 | tools._FinaliseForTest() |
| 133 | |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 134 | @classmethod |
| 135 | def _ResetDtbs(self): |
| 136 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
| 137 | TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA) |
| 138 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA) |
| 139 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 140 | def _RunBinman(self, *args, **kwargs): |
| 141 | """Run binman using the command line |
| 142 | |
| 143 | Args: |
| 144 | Arguments to pass, as a list of strings |
| 145 | kwargs: Arguments to pass to Command.RunPipe() |
| 146 | """ |
| 147 | result = command.RunPipe([[self._binman_pathname] + list(args)], |
| 148 | capture=True, capture_stderr=True, raise_on_error=False) |
| 149 | if result.return_code and kwargs.get('raise_on_error', True): |
| 150 | raise Exception("Error running '%s': %s" % (' '.join(args), |
| 151 | result.stdout + result.stderr)) |
| 152 | return result |
| 153 | |
| 154 | def _DoBinman(self, *args): |
| 155 | """Run binman using directly (in the same process) |
| 156 | |
| 157 | Args: |
| 158 | Arguments to pass, as a list of strings |
| 159 | Returns: |
| 160 | Return value (0 for success) |
| 161 | """ |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 162 | args = list(args) |
| 163 | if '-D' in sys.argv: |
| 164 | args = args + ['-D'] |
| 165 | (options, args) = cmdline.ParseArgs(args) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 166 | options.pager = 'binman-invalid-pager' |
| 167 | options.build_dir = self._indir |
| 168 | |
| 169 | # For testing, you can force an increase in verbosity here |
| 170 | # options.verbosity = tout.DEBUG |
| 171 | return control.Binman(options, args) |
| 172 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 173 | def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False, |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 174 | entry_args=None, images=None): |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 175 | """Run binman with a given test file |
| 176 | |
| 177 | Args: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 178 | fname: Device-tree source filename to use (e.g. 05_simple.dts) |
| 179 | debug: True to enable debugging output |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 180 | map: True to output map files for the images |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 181 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 182 | tree before packing it into the image |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 183 | entry_args: Dict of entry args to supply to binman |
| 184 | key: arg name |
| 185 | value: value of that arg |
| 186 | images: List of image names to build |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 187 | """ |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 188 | args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)] |
| 189 | if debug: |
| 190 | args.append('-D') |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 191 | if map: |
| 192 | args.append('-m') |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 193 | if update_dtb: |
| 194 | args.append('-up') |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 195 | if entry_args: |
| 196 | for arg, value in entry_args.iteritems(): |
| 197 | args.append('-a%s=%s' % (arg, value)) |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 198 | if images: |
| 199 | for image in images: |
| 200 | args += ['-i', image] |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 201 | return self._DoBinman(*args) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 202 | |
| 203 | def _SetupDtb(self, fname, outfile='u-boot.dtb'): |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 204 | """Set up a new test device-tree file |
| 205 | |
| 206 | The given file is compiled and set up as the device tree to be used |
| 207 | for ths test. |
| 208 | |
| 209 | Args: |
| 210 | fname: Filename of .dts file to read |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 211 | outfile: Output filename for compiled device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 212 | |
| 213 | Returns: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 214 | Contents of device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 215 | """ |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 216 | if not self._output_setup: |
| 217 | tools.PrepareOutputDir(self._indir, True) |
| 218 | self._output_setup = True |
| 219 | dtb = fdt_util.EnsureCompiled(self.TestFile(fname)) |
| 220 | with open(dtb) as fd: |
| 221 | data = fd.read() |
| 222 | TestFunctional._MakeInputFile(outfile, data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 223 | return data |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 224 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 225 | def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False, |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 226 | update_dtb=False, entry_args=None): |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 227 | """Run binman and return the resulting image |
| 228 | |
| 229 | This runs binman with a given test file and then reads the resulting |
| 230 | output file. It is a shortcut function since most tests need to do |
| 231 | these steps. |
| 232 | |
| 233 | Raises an assertion failure if binman returns a non-zero exit code. |
| 234 | |
| 235 | Args: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 236 | fname: Device-tree source filename to use (e.g. 05_simple.dts) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 237 | use_real_dtb: True to use the test file as the contents of |
| 238 | the u-boot-dtb entry. Normally this is not needed and the |
| 239 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 240 | But in some test we need the real contents. |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 241 | map: True to output map files for the images |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 242 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 243 | tree before packing it into the image |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 244 | |
| 245 | Returns: |
| 246 | Tuple: |
| 247 | Resulting image contents |
| 248 | Device tree contents |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 249 | Map data showing contents of image (or None if none) |
Simon Glass | ea6922e | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 250 | Output device tree binary filename ('u-boot.dtb' path) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 251 | """ |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 252 | dtb_data = None |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 253 | # Use the compiled test file as the u-boot-dtb input |
| 254 | if use_real_dtb: |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 255 | dtb_data = self._SetupDtb(fname) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 256 | |
| 257 | try: |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 258 | retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb, |
| 259 | entry_args=entry_args) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 260 | self.assertEqual(0, retcode) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 261 | out_dtb_fname = control.GetFdtPath('u-boot.dtb') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 262 | |
| 263 | # Find the (only) image, read it and return its contents |
| 264 | image = control.images['image'] |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 265 | image_fname = tools.GetOutputFilename('image.bin') |
| 266 | self.assertTrue(os.path.exists(image_fname)) |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 267 | if map: |
| 268 | map_fname = tools.GetOutputFilename('image.map') |
| 269 | with open(map_fname) as fd: |
| 270 | map_data = fd.read() |
| 271 | else: |
| 272 | map_data = None |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 273 | with open(image_fname) as fd: |
| 274 | return fd.read(), dtb_data, map_data, out_dtb_fname |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 275 | finally: |
| 276 | # Put the test file back |
| 277 | if use_real_dtb: |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 278 | self._ResetDtbs() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 279 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 280 | def _DoReadFile(self, fname, use_real_dtb=False): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 281 | """Helper function which discards the device-tree binary |
| 282 | |
| 283 | Args: |
| 284 | fname: Device-tree source filename to use (e.g. 05_simple.dts) |
| 285 | use_real_dtb: True to use the test file as the contents of |
| 286 | the u-boot-dtb entry. Normally this is not needed and the |
| 287 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 288 | But in some test we need the real contents. |
Simon Glass | ea6922e | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 289 | |
| 290 | Returns: |
| 291 | Resulting image contents |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 292 | """ |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 293 | return self._DoReadFileDtb(fname, use_real_dtb)[0] |
| 294 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 295 | @classmethod |
| 296 | def _MakeInputFile(self, fname, contents): |
| 297 | """Create a new test input file, creating directories as needed |
| 298 | |
| 299 | Args: |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 300 | fname: Filename to create |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 301 | contents: File contents to write in to the file |
| 302 | Returns: |
| 303 | Full pathname of file created |
| 304 | """ |
| 305 | pathname = os.path.join(self._indir, fname) |
| 306 | dirname = os.path.dirname(pathname) |
| 307 | if dirname and not os.path.exists(dirname): |
| 308 | os.makedirs(dirname) |
| 309 | with open(pathname, 'wb') as fd: |
| 310 | fd.write(contents) |
| 311 | return pathname |
| 312 | |
| 313 | @classmethod |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 314 | def _MakeInputDir(self, dirname): |
| 315 | """Create a new test input directory, creating directories as needed |
| 316 | |
| 317 | Args: |
| 318 | dirname: Directory name to create |
| 319 | |
| 320 | Returns: |
| 321 | Full pathname of directory created |
| 322 | """ |
| 323 | pathname = os.path.join(self._indir, dirname) |
| 324 | if not os.path.exists(pathname): |
| 325 | os.makedirs(pathname) |
| 326 | return pathname |
| 327 | |
| 328 | @classmethod |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 329 | def TestFile(self, fname): |
| 330 | return os.path.join(self._binman_dir, 'test', fname) |
| 331 | |
| 332 | def AssertInList(self, grep_list, target): |
| 333 | """Assert that at least one of a list of things is in a target |
| 334 | |
| 335 | Args: |
| 336 | grep_list: List of strings to check |
| 337 | target: Target string |
| 338 | """ |
| 339 | for grep in grep_list: |
| 340 | if grep in target: |
| 341 | return |
| 342 | self.fail("Error: '%' not found in '%s'" % (grep_list, target)) |
| 343 | |
| 344 | def CheckNoGaps(self, entries): |
| 345 | """Check that all entries fit together without gaps |
| 346 | |
| 347 | Args: |
| 348 | entries: List of entries to check |
| 349 | """ |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 350 | offset = 0 |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 351 | for entry in entries.values(): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 352 | self.assertEqual(offset, entry.offset) |
| 353 | offset += entry.size |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 354 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 355 | def GetFdtLen(self, dtb): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 356 | """Get the totalsize field from a device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 357 | |
| 358 | Args: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 359 | dtb: Device-tree binary contents |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 360 | |
| 361 | Returns: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 362 | Total size of device-tree binary, from the header |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 363 | """ |
| 364 | return struct.unpack('>L', dtb[4:8])[0] |
| 365 | |
Simon Glass | cee02e6 | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 366 | def _GetPropTree(self, dtb, prop_names): |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 367 | def AddNode(node, path): |
| 368 | if node.name != '/': |
| 369 | path += '/' + node.name |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 370 | for subnode in node.subnodes: |
| 371 | for prop in subnode.props.values(): |
Simon Glass | cee02e6 | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 372 | if prop.name in prop_names: |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 373 | prop_path = path + '/' + subnode.name + ':' + prop.name |
| 374 | tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu( |
| 375 | prop.value) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 376 | AddNode(subnode, path) |
| 377 | |
| 378 | tree = {} |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 379 | AddNode(dtb.GetRoot(), '') |
| 380 | return tree |
| 381 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 382 | def testRun(self): |
| 383 | """Test a basic run with valid args""" |
| 384 | result = self._RunBinman('-h') |
| 385 | |
| 386 | def testFullHelp(self): |
| 387 | """Test that the full help is displayed with -H""" |
| 388 | result = self._RunBinman('-H') |
| 389 | help_file = os.path.join(self._binman_dir, 'README') |
Tom Rini | 3759df0 | 2018-01-16 15:29:50 -0500 | [diff] [blame] | 390 | # Remove possible extraneous strings |
| 391 | extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n' |
| 392 | gothelp = result.stdout.replace(extra, '') |
| 393 | self.assertEqual(len(gothelp), os.path.getsize(help_file)) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 394 | self.assertEqual(0, len(result.stderr)) |
| 395 | self.assertEqual(0, result.return_code) |
| 396 | |
| 397 | def testFullHelpInternal(self): |
| 398 | """Test that the full help is displayed with -H""" |
| 399 | try: |
| 400 | command.test_result = command.CommandResult() |
| 401 | result = self._DoBinman('-H') |
| 402 | help_file = os.path.join(self._binman_dir, 'README') |
| 403 | finally: |
| 404 | command.test_result = None |
| 405 | |
| 406 | def testHelp(self): |
| 407 | """Test that the basic help is displayed with -h""" |
| 408 | result = self._RunBinman('-h') |
| 409 | self.assertTrue(len(result.stdout) > 200) |
| 410 | self.assertEqual(0, len(result.stderr)) |
| 411 | self.assertEqual(0, result.return_code) |
| 412 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 413 | def testBoard(self): |
| 414 | """Test that we can run it with a specific board""" |
| 415 | self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb') |
| 416 | TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA) |
| 417 | result = self._DoBinman('-b', 'sandbox') |
| 418 | self.assertEqual(0, result) |
| 419 | |
| 420 | def testNeedBoard(self): |
| 421 | """Test that we get an error when no board ius supplied""" |
| 422 | with self.assertRaises(ValueError) as e: |
| 423 | result = self._DoBinman() |
| 424 | self.assertIn("Must provide a board to process (use -b <board>)", |
| 425 | str(e.exception)) |
| 426 | |
| 427 | def testMissingDt(self): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 428 | """Test that an invalid device-tree file generates an error""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 429 | with self.assertRaises(Exception) as e: |
| 430 | self._RunBinman('-d', 'missing_file') |
| 431 | # We get one error from libfdt, and a different one from fdtget. |
| 432 | self.AssertInList(["Couldn't open blob from 'missing_file'", |
| 433 | 'No such file or directory'], str(e.exception)) |
| 434 | |
| 435 | def testBrokenDt(self): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 436 | """Test that an invalid device-tree source file generates an error |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 437 | |
| 438 | Since this is a source file it should be compiled and the error |
| 439 | will come from the device-tree compiler (dtc). |
| 440 | """ |
| 441 | with self.assertRaises(Exception) as e: |
| 442 | self._RunBinman('-d', self.TestFile('01_invalid.dts')) |
| 443 | self.assertIn("FATAL ERROR: Unable to parse input tree", |
| 444 | str(e.exception)) |
| 445 | |
| 446 | def testMissingNode(self): |
| 447 | """Test that a device tree without a 'binman' node generates an error""" |
| 448 | with self.assertRaises(Exception) as e: |
| 449 | self._DoBinman('-d', self.TestFile('02_missing_node.dts')) |
| 450 | self.assertIn("does not have a 'binman' node", str(e.exception)) |
| 451 | |
| 452 | def testEmpty(self): |
| 453 | """Test that an empty binman node works OK (i.e. does nothing)""" |
| 454 | result = self._RunBinman('-d', self.TestFile('03_empty.dts')) |
| 455 | self.assertEqual(0, len(result.stderr)) |
| 456 | self.assertEqual(0, result.return_code) |
| 457 | |
| 458 | def testInvalidEntry(self): |
| 459 | """Test that an invalid entry is flagged""" |
| 460 | with self.assertRaises(Exception) as e: |
| 461 | result = self._RunBinman('-d', |
| 462 | self.TestFile('04_invalid_entry.dts')) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 463 | self.assertIn("Unknown entry type 'not-a-valid-type' in node " |
| 464 | "'/binman/not-a-valid-type'", str(e.exception)) |
| 465 | |
| 466 | def testSimple(self): |
| 467 | """Test a simple binman with a single file""" |
| 468 | data = self._DoReadFile('05_simple.dts') |
| 469 | self.assertEqual(U_BOOT_DATA, data) |
| 470 | |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 471 | def testSimpleDebug(self): |
| 472 | """Test a simple binman run with debugging enabled""" |
| 473 | data = self._DoTestFile('05_simple.dts', debug=True) |
| 474 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 475 | def testDual(self): |
| 476 | """Test that we can handle creating two images |
| 477 | |
| 478 | This also tests image padding. |
| 479 | """ |
| 480 | retcode = self._DoTestFile('06_dual_image.dts') |
| 481 | self.assertEqual(0, retcode) |
| 482 | |
| 483 | image = control.images['image1'] |
| 484 | self.assertEqual(len(U_BOOT_DATA), image._size) |
| 485 | fname = tools.GetOutputFilename('image1.bin') |
| 486 | self.assertTrue(os.path.exists(fname)) |
| 487 | with open(fname) as fd: |
| 488 | data = fd.read() |
| 489 | self.assertEqual(U_BOOT_DATA, data) |
| 490 | |
| 491 | image = control.images['image2'] |
| 492 | self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size) |
| 493 | fname = tools.GetOutputFilename('image2.bin') |
| 494 | self.assertTrue(os.path.exists(fname)) |
| 495 | with open(fname) as fd: |
| 496 | data = fd.read() |
| 497 | self.assertEqual(U_BOOT_DATA, data[3:7]) |
| 498 | self.assertEqual(chr(0) * 3, data[:3]) |
| 499 | self.assertEqual(chr(0) * 5, data[7:]) |
| 500 | |
| 501 | def testBadAlign(self): |
| 502 | """Test that an invalid alignment value is detected""" |
| 503 | with self.assertRaises(ValueError) as e: |
| 504 | self._DoTestFile('07_bad_align.dts') |
| 505 | self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " |
| 506 | "of two", str(e.exception)) |
| 507 | |
| 508 | def testPackSimple(self): |
| 509 | """Test that packing works as expected""" |
| 510 | retcode = self._DoTestFile('08_pack.dts') |
| 511 | self.assertEqual(0, retcode) |
| 512 | self.assertIn('image', control.images) |
| 513 | image = control.images['image'] |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 514 | entries = image.GetEntries() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 515 | self.assertEqual(5, len(entries)) |
| 516 | |
| 517 | # First u-boot |
| 518 | self.assertIn('u-boot', entries) |
| 519 | entry = entries['u-boot'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 520 | self.assertEqual(0, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 521 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 522 | |
| 523 | # Second u-boot, aligned to 16-byte boundary |
| 524 | self.assertIn('u-boot-align', entries) |
| 525 | entry = entries['u-boot-align'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 526 | self.assertEqual(16, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 527 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 528 | |
| 529 | # Third u-boot, size 23 bytes |
| 530 | self.assertIn('u-boot-size', entries) |
| 531 | entry = entries['u-boot-size'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 532 | self.assertEqual(20, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 533 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 534 | self.assertEqual(23, entry.size) |
| 535 | |
| 536 | # Fourth u-boot, placed immediate after the above |
| 537 | self.assertIn('u-boot-next', entries) |
| 538 | entry = entries['u-boot-next'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 539 | self.assertEqual(43, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 540 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 541 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 542 | # Fifth u-boot, placed at a fixed offset |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 543 | self.assertIn('u-boot-fixed', entries) |
| 544 | entry = entries['u-boot-fixed'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 545 | self.assertEqual(61, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 546 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 547 | |
| 548 | self.assertEqual(65, image._size) |
| 549 | |
| 550 | def testPackExtra(self): |
| 551 | """Test that extra packing feature works as expected""" |
| 552 | retcode = self._DoTestFile('09_pack_extra.dts') |
| 553 | |
| 554 | self.assertEqual(0, retcode) |
| 555 | self.assertIn('image', control.images) |
| 556 | image = control.images['image'] |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 557 | entries = image.GetEntries() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 558 | self.assertEqual(5, len(entries)) |
| 559 | |
| 560 | # First u-boot with padding before and after |
| 561 | self.assertIn('u-boot', entries) |
| 562 | entry = entries['u-boot'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 563 | self.assertEqual(0, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 564 | self.assertEqual(3, entry.pad_before) |
| 565 | self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) |
| 566 | |
| 567 | # Second u-boot has an aligned size, but it has no effect |
| 568 | self.assertIn('u-boot-align-size-nop', entries) |
| 569 | entry = entries['u-boot-align-size-nop'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 570 | self.assertEqual(12, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 571 | self.assertEqual(4, entry.size) |
| 572 | |
| 573 | # Third u-boot has an aligned size too |
| 574 | self.assertIn('u-boot-align-size', entries) |
| 575 | entry = entries['u-boot-align-size'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 576 | self.assertEqual(16, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 577 | self.assertEqual(32, entry.size) |
| 578 | |
| 579 | # Fourth u-boot has an aligned end |
| 580 | self.assertIn('u-boot-align-end', entries) |
| 581 | entry = entries['u-boot-align-end'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 582 | self.assertEqual(48, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 583 | self.assertEqual(16, entry.size) |
| 584 | |
| 585 | # Fifth u-boot immediately afterwards |
| 586 | self.assertIn('u-boot-align-both', entries) |
| 587 | entry = entries['u-boot-align-both'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 588 | self.assertEqual(64, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 589 | self.assertEqual(64, entry.size) |
| 590 | |
| 591 | self.CheckNoGaps(entries) |
| 592 | self.assertEqual(128, image._size) |
| 593 | |
| 594 | def testPackAlignPowerOf2(self): |
| 595 | """Test that invalid entry alignment is detected""" |
| 596 | with self.assertRaises(ValueError) as e: |
| 597 | self._DoTestFile('10_pack_align_power2.dts') |
| 598 | self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " |
| 599 | "of two", str(e.exception)) |
| 600 | |
| 601 | def testPackAlignSizePowerOf2(self): |
| 602 | """Test that invalid entry size alignment is detected""" |
| 603 | with self.assertRaises(ValueError) as e: |
| 604 | self._DoTestFile('11_pack_align_size_power2.dts') |
| 605 | self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " |
| 606 | "power of two", str(e.exception)) |
| 607 | |
| 608 | def testPackInvalidAlign(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 609 | """Test detection of an offset that does not match its alignment""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 610 | with self.assertRaises(ValueError) as e: |
| 611 | self._DoTestFile('12_pack_inv_align.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 612 | self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 613 | "align 0x4 (4)", str(e.exception)) |
| 614 | |
| 615 | def testPackInvalidSizeAlign(self): |
| 616 | """Test that invalid entry size alignment is detected""" |
| 617 | with self.assertRaises(ValueError) as e: |
| 618 | self._DoTestFile('13_pack_inv_size_align.dts') |
| 619 | self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " |
| 620 | "align-size 0x4 (4)", str(e.exception)) |
| 621 | |
| 622 | def testPackOverlap(self): |
| 623 | """Test that overlapping regions are detected""" |
| 624 | with self.assertRaises(ValueError) as e: |
| 625 | self._DoTestFile('14_pack_overlap.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 626 | self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 627 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 628 | str(e.exception)) |
| 629 | |
| 630 | def testPackEntryOverflow(self): |
| 631 | """Test that entries that overflow their size are detected""" |
| 632 | with self.assertRaises(ValueError) as e: |
| 633 | self._DoTestFile('15_pack_overflow.dts') |
| 634 | self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " |
| 635 | "but entry size is 0x3 (3)", str(e.exception)) |
| 636 | |
| 637 | def testPackImageOverflow(self): |
| 638 | """Test that entries which overflow the image size are detected""" |
| 639 | with self.assertRaises(ValueError) as e: |
| 640 | self._DoTestFile('16_pack_image_overflow.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 641 | self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 642 | "size 0x3 (3)", str(e.exception)) |
| 643 | |
| 644 | def testPackImageSize(self): |
| 645 | """Test that the image size can be set""" |
| 646 | retcode = self._DoTestFile('17_pack_image_size.dts') |
| 647 | self.assertEqual(0, retcode) |
| 648 | self.assertIn('image', control.images) |
| 649 | image = control.images['image'] |
| 650 | self.assertEqual(7, image._size) |
| 651 | |
| 652 | def testPackImageSizeAlign(self): |
| 653 | """Test that image size alignemnt works as expected""" |
| 654 | retcode = self._DoTestFile('18_pack_image_align.dts') |
| 655 | self.assertEqual(0, retcode) |
| 656 | self.assertIn('image', control.images) |
| 657 | image = control.images['image'] |
| 658 | self.assertEqual(16, image._size) |
| 659 | |
| 660 | def testPackInvalidImageAlign(self): |
| 661 | """Test that invalid image alignment is detected""" |
| 662 | with self.assertRaises(ValueError) as e: |
| 663 | self._DoTestFile('19_pack_inv_image_align.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 664 | self.assertIn("Section '/binman': Size 0x7 (7) does not match " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 665 | "align-size 0x8 (8)", str(e.exception)) |
| 666 | |
| 667 | def testPackAlignPowerOf2(self): |
| 668 | """Test that invalid image alignment is detected""" |
| 669 | with self.assertRaises(ValueError) as e: |
| 670 | self._DoTestFile('20_pack_inv_image_align_power2.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 671 | self.assertIn("Section '/binman': Alignment size 131 must be a power of " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 672 | "two", str(e.exception)) |
| 673 | |
| 674 | def testImagePadByte(self): |
| 675 | """Test that the image pad byte can be specified""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 676 | with open(self.TestFile('bss_data')) as fd: |
| 677 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 678 | data = self._DoReadFile('21_image_pad.dts') |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 679 | self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 680 | |
| 681 | def testImageName(self): |
| 682 | """Test that image files can be named""" |
| 683 | retcode = self._DoTestFile('22_image_name.dts') |
| 684 | self.assertEqual(0, retcode) |
| 685 | image = control.images['image1'] |
| 686 | fname = tools.GetOutputFilename('test-name') |
| 687 | self.assertTrue(os.path.exists(fname)) |
| 688 | |
| 689 | image = control.images['image2'] |
| 690 | fname = tools.GetOutputFilename('test-name.xx') |
| 691 | self.assertTrue(os.path.exists(fname)) |
| 692 | |
| 693 | def testBlobFilename(self): |
| 694 | """Test that generic blobs can be provided by filename""" |
| 695 | data = self._DoReadFile('23_blob.dts') |
| 696 | self.assertEqual(BLOB_DATA, data) |
| 697 | |
| 698 | def testPackSorted(self): |
| 699 | """Test that entries can be sorted""" |
| 700 | data = self._DoReadFile('24_sorted.dts') |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 701 | self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 + |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 702 | U_BOOT_DATA, data) |
| 703 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 704 | def testPackZeroOffset(self): |
| 705 | """Test that an entry at offset 0 is not given a new offset""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 706 | with self.assertRaises(ValueError) as e: |
| 707 | self._DoTestFile('25_pack_zero_size.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 708 | self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 709 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 710 | str(e.exception)) |
| 711 | |
| 712 | def testPackUbootDtb(self): |
| 713 | """Test that a device tree can be added to U-Boot""" |
| 714 | data = self._DoReadFile('26_pack_u_boot_dtb.dts') |
| 715 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 716 | |
| 717 | def testPackX86RomNoSize(self): |
| 718 | """Test that the end-at-4gb property requires a size property""" |
| 719 | with self.assertRaises(ValueError) as e: |
| 720 | self._DoTestFile('27_pack_4gb_no_size.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 721 | self.assertIn("Section '/binman': Section size must be provided when " |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 722 | "using end-at-4gb", str(e.exception)) |
| 723 | |
| 724 | def testPackX86RomOutside(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 725 | """Test that the end-at-4gb property checks for offset boundaries""" |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 726 | with self.assertRaises(ValueError) as e: |
| 727 | self._DoTestFile('28_pack_4gb_outside.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 728 | self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside " |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 729 | "the section starting at 0xffffffe0 (4294967264)", |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 730 | str(e.exception)) |
| 731 | |
| 732 | def testPackX86Rom(self): |
| 733 | """Test that a basic x86 ROM can be created""" |
| 734 | data = self._DoReadFile('29_x86-rom.dts') |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 735 | self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA + |
| 736 | chr(0) * 2, data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 737 | |
| 738 | def testPackX86RomMeNoDesc(self): |
| 739 | """Test that an invalid Intel descriptor entry is detected""" |
| 740 | TestFunctional._MakeInputFile('descriptor.bin', '') |
| 741 | with self.assertRaises(ValueError) as e: |
| 742 | self._DoTestFile('31_x86-rom-me.dts') |
| 743 | self.assertIn("Node '/binman/intel-descriptor': Cannot find FD " |
| 744 | "signature", str(e.exception)) |
| 745 | |
| 746 | def testPackX86RomBadDesc(self): |
| 747 | """Test that the Intel requires a descriptor entry""" |
| 748 | with self.assertRaises(ValueError) as e: |
| 749 | self._DoTestFile('30_x86-rom-me-no-desc.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 750 | self.assertIn("Node '/binman/intel-me': No offset set with " |
| 751 | "offset-unset: should another entry provide this correct " |
| 752 | "offset?", str(e.exception)) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 753 | |
| 754 | def testPackX86RomMe(self): |
| 755 | """Test that an x86 ROM with an ME region can be created""" |
| 756 | data = self._DoReadFile('31_x86-rom-me.dts') |
| 757 | self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) |
| 758 | |
| 759 | def testPackVga(self): |
| 760 | """Test that an image with a VGA binary can be created""" |
| 761 | data = self._DoReadFile('32_intel-vga.dts') |
| 762 | self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) |
| 763 | |
| 764 | def testPackStart16(self): |
| 765 | """Test that an image with an x86 start16 region can be created""" |
| 766 | data = self._DoReadFile('33_x86-start16.dts') |
| 767 | self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) |
| 768 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 769 | def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 770 | """Handle running a test for insertion of microcode |
| 771 | |
| 772 | Args: |
| 773 | dts_fname: Name of test .dts file |
| 774 | nodtb_data: Data that we expect in the first section |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 775 | ucode_second: True if the microsecond entry is second instead of |
| 776 | third |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 777 | |
| 778 | Returns: |
| 779 | Tuple: |
| 780 | Contents of first region (U-Boot or SPL) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 781 | Offset and size components of microcode pointer, as inserted |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 782 | in the above (two 4-byte words) |
| 783 | """ |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 784 | data = self._DoReadFile(dts_fname, True) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 785 | |
| 786 | # Now check the device tree has no microcode |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 787 | if ucode_second: |
| 788 | ucode_content = data[len(nodtb_data):] |
| 789 | ucode_pos = len(nodtb_data) |
| 790 | dtb_with_ucode = ucode_content[16:] |
| 791 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 792 | else: |
| 793 | dtb_with_ucode = data[len(nodtb_data):] |
| 794 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 795 | ucode_content = dtb_with_ucode[fdt_len:] |
| 796 | ucode_pos = len(nodtb_data) + fdt_len |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 797 | fname = tools.GetOutputFilename('test.dtb') |
| 798 | with open(fname, 'wb') as fd: |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 799 | fd.write(dtb_with_ucode) |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 800 | dtb = fdt.FdtScan(fname) |
| 801 | ucode = dtb.GetNode('/microcode') |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 802 | self.assertTrue(ucode) |
| 803 | for node in ucode.subnodes: |
| 804 | self.assertFalse(node.props.get('data')) |
| 805 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 806 | # Check that the microcode appears immediately after the Fdt |
| 807 | # This matches the concatenation of the data properties in |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 808 | # the /microcode/update@xxx nodes in 34_x86_ucode.dts. |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 809 | ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, |
| 810 | 0x78235609) |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 811 | self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 812 | |
| 813 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 814 | # expected offset and size |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 815 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 816 | len(ucode_data)) |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 817 | u_boot = data[:len(nodtb_data)] |
| 818 | return u_boot, pos_and_size |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 819 | |
| 820 | def testPackUbootMicrocode(self): |
| 821 | """Test that x86 microcode can be handled correctly |
| 822 | |
| 823 | We expect to see the following in the image, in order: |
| 824 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 825 | place |
| 826 | u-boot.dtb with the microcode removed |
| 827 | the microcode |
| 828 | """ |
| 829 | first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts', |
| 830 | U_BOOT_NODTB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 831 | self.assertEqual('nodtb with microcode' + pos_and_size + |
| 832 | ' somewhere in here', first) |
| 833 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 834 | def _RunPackUbootSingleMicrocode(self): |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 835 | """Test that x86 microcode can be handled correctly |
| 836 | |
| 837 | We expect to see the following in the image, in order: |
| 838 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 839 | place |
| 840 | u-boot.dtb with the microcode |
| 841 | an empty microcode region |
| 842 | """ |
| 843 | # We need the libfdt library to run this test since only that allows |
| 844 | # finding the offset of a property. This is required by |
| 845 | # Entry_u_boot_dtb_with_ucode.ObtainContents(). |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 846 | data = self._DoReadFile('35_x86_single_ucode.dts', True) |
| 847 | |
| 848 | second = data[len(U_BOOT_NODTB_DATA):] |
| 849 | |
| 850 | fdt_len = self.GetFdtLen(second) |
| 851 | third = second[fdt_len:] |
| 852 | second = second[:fdt_len] |
| 853 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 854 | ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) |
| 855 | self.assertIn(ucode_data, second) |
| 856 | ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 857 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 858 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 859 | # expected offset and size |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 860 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 861 | len(ucode_data)) |
| 862 | first = data[:len(U_BOOT_NODTB_DATA)] |
| 863 | self.assertEqual('nodtb with microcode' + pos_and_size + |
| 864 | ' somewhere in here', first) |
Simon Glass | c49deb8 | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 865 | |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 866 | def testPackUbootSingleMicrocode(self): |
| 867 | """Test that x86 microcode can be handled correctly with fdt_normal. |
| 868 | """ |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 869 | self._RunPackUbootSingleMicrocode() |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 870 | |
Simon Glass | c49deb8 | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 871 | def testUBootImg(self): |
| 872 | """Test that u-boot.img can be put in a file""" |
| 873 | data = self._DoReadFile('36_u_boot_img.dts') |
| 874 | self.assertEqual(U_BOOT_IMG_DATA, data) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 875 | |
| 876 | def testNoMicrocode(self): |
| 877 | """Test that a missing microcode region is detected""" |
| 878 | with self.assertRaises(ValueError) as e: |
| 879 | self._DoReadFile('37_x86_no_ucode.dts', True) |
| 880 | self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode " |
| 881 | "node found in ", str(e.exception)) |
| 882 | |
| 883 | def testMicrocodeWithoutNode(self): |
| 884 | """Test that a missing u-boot-dtb-with-ucode node is detected""" |
| 885 | with self.assertRaises(ValueError) as e: |
| 886 | self._DoReadFile('38_x86_ucode_missing_node.dts', True) |
| 887 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 888 | "microcode region u-boot-dtb-with-ucode", str(e.exception)) |
| 889 | |
| 890 | def testMicrocodeWithoutNode2(self): |
| 891 | """Test that a missing u-boot-ucode node is detected""" |
| 892 | with self.assertRaises(ValueError) as e: |
| 893 | self._DoReadFile('39_x86_ucode_missing_node2.dts', True) |
| 894 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 895 | "microcode region u-boot-ucode", str(e.exception)) |
| 896 | |
| 897 | def testMicrocodeWithoutPtrInElf(self): |
| 898 | """Test that a U-Boot binary without the microcode symbol is detected""" |
| 899 | # ELF file without a '_dt_ucode_base_size' symbol |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 900 | try: |
| 901 | with open(self.TestFile('u_boot_no_ucode_ptr')) as fd: |
| 902 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 903 | |
| 904 | with self.assertRaises(ValueError) as e: |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 905 | self._RunPackUbootSingleMicrocode() |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 906 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate " |
| 907 | "_dt_ucode_base_size symbol in u-boot", str(e.exception)) |
| 908 | |
| 909 | finally: |
| 910 | # Put the original file back |
| 911 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 912 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 913 | |
| 914 | def testMicrocodeNotInImage(self): |
| 915 | """Test that microcode must be placed within the image""" |
| 916 | with self.assertRaises(ValueError) as e: |
| 917 | self._DoReadFile('40_x86_ucode_not_in_image.dts', True) |
| 918 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode " |
| 919 | "pointer _dt_ucode_base_size at fffffe14 is outside the " |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 920 | "section ranging from 00000000 to 0000002e", str(e.exception)) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 921 | |
| 922 | def testWithoutMicrocode(self): |
| 923 | """Test that we can cope with an image without microcode (e.g. qemu)""" |
| 924 | with open(self.TestFile('u_boot_no_ucode_ptr')) as fd: |
| 925 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 926 | data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 927 | |
| 928 | # Now check the device tree has no microcode |
| 929 | self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)]) |
| 930 | second = data[len(U_BOOT_NODTB_DATA):] |
| 931 | |
| 932 | fdt_len = self.GetFdtLen(second) |
| 933 | self.assertEqual(dtb, second[:fdt_len]) |
| 934 | |
| 935 | used_len = len(U_BOOT_NODTB_DATA) + fdt_len |
| 936 | third = data[used_len:] |
| 937 | self.assertEqual(chr(0) * (0x200 - used_len), third) |
| 938 | |
| 939 | def testUnknownPosSize(self): |
| 940 | """Test that microcode must be placed within the image""" |
| 941 | with self.assertRaises(ValueError) as e: |
| 942 | self._DoReadFile('41_unknown_pos_size.dts', True) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 943 | self.assertIn("Section '/binman': Unable to set offset/size for unknown " |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 944 | "entry 'invalid-entry'", str(e.exception)) |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 945 | |
| 946 | def testPackFsp(self): |
| 947 | """Test that an image with a FSP binary can be created""" |
| 948 | data = self._DoReadFile('42_intel-fsp.dts') |
| 949 | self.assertEqual(FSP_DATA, data[:len(FSP_DATA)]) |
| 950 | |
| 951 | def testPackCmc(self): |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 952 | """Test that an image with a CMC binary can be created""" |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 953 | data = self._DoReadFile('43_intel-cmc.dts') |
| 954 | self.assertEqual(CMC_DATA, data[:len(CMC_DATA)]) |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 955 | |
| 956 | def testPackVbt(self): |
| 957 | """Test that an image with a VBT binary can be created""" |
| 958 | data = self._DoReadFile('46_intel-vbt.dts') |
| 959 | self.assertEqual(VBT_DATA, data[:len(VBT_DATA)]) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 960 | |
Simon Glass | 5650984 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 961 | def testSplBssPad(self): |
| 962 | """Test that we can pad SPL's BSS with zeros""" |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 963 | # ELF file with a '__bss_size' symbol |
| 964 | with open(self.TestFile('bss_data')) as fd: |
| 965 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
Simon Glass | 5650984 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 966 | data = self._DoReadFile('47_spl_bss_pad.dts') |
| 967 | self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data) |
| 968 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 969 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 970 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
| 971 | with self.assertRaises(ValueError) as e: |
| 972 | data = self._DoReadFile('47_spl_bss_pad.dts') |
| 973 | self.assertIn('Expected __bss_size symbol in spl/u-boot-spl', |
| 974 | str(e.exception)) |
| 975 | |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 976 | def testPackStart16Spl(self): |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 977 | """Test that an image with an x86 start16 SPL region can be created""" |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 978 | data = self._DoReadFile('48_x86-start16-spl.dts') |
| 979 | self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) |
| 980 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 981 | def _PackUbootSplMicrocode(self, dts, ucode_second=False): |
| 982 | """Helper function for microcode tests |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 983 | |
| 984 | We expect to see the following in the image, in order: |
| 985 | u-boot-spl-nodtb.bin with a microcode pointer inserted at the |
| 986 | correct place |
| 987 | u-boot.dtb with the microcode removed |
| 988 | the microcode |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 989 | |
| 990 | Args: |
| 991 | dts: Device tree file to use for test |
| 992 | ucode_second: True if the microsecond entry is second instead of |
| 993 | third |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 994 | """ |
| 995 | # ELF file with a '_dt_ucode_base_size' symbol |
| 996 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 997 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 998 | first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, |
| 999 | ucode_second=ucode_second) |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1000 | self.assertEqual('splnodtb with microc' + pos_and_size + |
| 1001 | 'ter somewhere in here', first) |
| 1002 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1003 | def testPackUbootSplMicrocode(self): |
| 1004 | """Test that x86 microcode can be handled correctly in SPL""" |
| 1005 | self._PackUbootSplMicrocode('49_x86_ucode_spl.dts') |
| 1006 | |
| 1007 | def testPackUbootSplMicrocodeReorder(self): |
| 1008 | """Test that order doesn't matter for microcode entries |
| 1009 | |
| 1010 | This is the same as testPackUbootSplMicrocode but when we process the |
| 1011 | u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode |
| 1012 | entry, so we reply on binman to try later. |
| 1013 | """ |
| 1014 | self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts', |
| 1015 | ucode_second=True) |
| 1016 | |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1017 | def testPackMrc(self): |
| 1018 | """Test that an image with an MRC binary can be created""" |
| 1019 | data = self._DoReadFile('50_intel_mrc.dts') |
| 1020 | self.assertEqual(MRC_DATA, data[:len(MRC_DATA)]) |
| 1021 | |
Simon Glass | 47419ea | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1022 | def testSplDtb(self): |
| 1023 | """Test that an image with spl/u-boot-spl.dtb can be created""" |
| 1024 | data = self._DoReadFile('51_u_boot_spl_dtb.dts') |
| 1025 | self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)]) |
| 1026 | |
Simon Glass | 4e6fdbe | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1027 | def testSplNoDtb(self): |
| 1028 | """Test that an image with spl/u-boot-spl-nodtb.bin can be created""" |
| 1029 | data = self._DoReadFile('52_u_boot_spl_nodtb.dts') |
| 1030 | self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)]) |
| 1031 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1032 | def testSymbols(self): |
| 1033 | """Test binman can assign symbols embedded in U-Boot""" |
| 1034 | elf_fname = self.TestFile('u_boot_binman_syms') |
| 1035 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 1036 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1037 | self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1038 | |
| 1039 | with open(self.TestFile('u_boot_binman_syms')) as fd: |
| 1040 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
| 1041 | data = self._DoReadFile('53_symbols.dts') |
| 1042 | sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20) |
| 1043 | expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) + |
| 1044 | U_BOOT_DATA + |
| 1045 | sym_values + U_BOOT_SPL_DATA[16:]) |
| 1046 | self.assertEqual(expected, data) |
| 1047 | |
Simon Glass | dd57c13 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1048 | def testPackUnitAddress(self): |
| 1049 | """Test that we support multiple binaries with the same name""" |
| 1050 | data = self._DoReadFile('54_unit_address.dts') |
| 1051 | self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data) |
| 1052 | |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1053 | def testSections(self): |
| 1054 | """Basic test of sections""" |
| 1055 | data = self._DoReadFile('55_sections.dts') |
Simon Glass | 8122f39 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 1056 | expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 + |
| 1057 | U_BOOT_DATA + '&' * 4) |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1058 | self.assertEqual(expected, data) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1059 | |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1060 | def testMap(self): |
| 1061 | """Tests outputting a map of the images""" |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1062 | _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True) |
Simon Glass | 1be70d2 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1063 | self.assertEqual('''ImagePos Offset Size Name |
| 1064 | 00000000 00000000 00000028 main-section |
| 1065 | 00000000 00000000 00000010 section@0 |
| 1066 | 00000000 00000000 00000004 u-boot |
| 1067 | 00000010 00000010 00000010 section@1 |
| 1068 | 00000010 00000000 00000004 u-boot |
| 1069 | 00000020 00000020 00000004 section@2 |
| 1070 | 00000020 00000000 00000004 u-boot |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1071 | ''', map_data) |
| 1072 | |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1073 | def testNamePrefix(self): |
| 1074 | """Tests that name prefixes are used""" |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1075 | _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True) |
Simon Glass | 1be70d2 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1076 | self.assertEqual('''ImagePos Offset Size Name |
| 1077 | 00000000 00000000 00000028 main-section |
| 1078 | 00000000 00000000 00000010 section@0 |
| 1079 | 00000000 00000000 00000004 ro-u-boot |
| 1080 | 00000010 00000010 00000010 section@1 |
| 1081 | 00000010 00000000 00000004 rw-u-boot |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1082 | ''', map_data) |
| 1083 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1084 | def testUnknownContents(self): |
| 1085 | """Test that obtaining the contents works as expected""" |
| 1086 | with self.assertRaises(ValueError) as e: |
| 1087 | self._DoReadFile('57_unknown_contents.dts', True) |
| 1088 | self.assertIn("Section '/binman': Internal error: Could not complete " |
| 1089 | "processing of contents: remaining [<_testing.Entry__testing ", |
| 1090 | str(e.exception)) |
| 1091 | |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1092 | def testBadChangeSize(self): |
| 1093 | """Test that trying to change the size of an entry fails""" |
| 1094 | with self.assertRaises(ValueError) as e: |
| 1095 | self._DoReadFile('59_change_size.dts', True) |
| 1096 | self.assertIn("Node '/binman/_testing': Cannot update entry size from " |
| 1097 | '2 to 1', str(e.exception)) |
| 1098 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1099 | def testUpdateFdt(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1100 | """Test that we can update the device tree with offset/size info""" |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1101 | _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts', |
| 1102 | update_dtb=True) |
Simon Glass | cee02e6 | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 1103 | dtb = fdt.Fdt(out_dtb_fname) |
| 1104 | dtb.Scan() |
| 1105 | props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos']) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1106 | self.assertEqual({ |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1107 | 'image-pos': 0, |
Simon Glass | 8122f39 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 1108 | 'offset': 0, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1109 | '_testing:offset': 32, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1110 | '_testing:size': 1, |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1111 | '_testing:image-pos': 32, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1112 | 'section@0/u-boot:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1113 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1114 | 'section@0/u-boot:image-pos': 0, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1115 | 'section@0:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1116 | 'section@0:size': 16, |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1117 | 'section@0:image-pos': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1118 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1119 | 'section@1/u-boot:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1120 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1121 | 'section@1/u-boot:image-pos': 16, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1122 | 'section@1:offset': 16, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1123 | 'section@1:size': 16, |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1124 | 'section@1:image-pos': 16, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1125 | 'size': 40 |
| 1126 | }, props) |
| 1127 | |
| 1128 | def testUpdateFdtBad(self): |
| 1129 | """Test that we detect when ProcessFdt never completes""" |
| 1130 | with self.assertRaises(ValueError) as e: |
| 1131 | self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True) |
| 1132 | self.assertIn('Could not complete processing of Fdt: remaining ' |
| 1133 | '[<_testing.Entry__testing', str(e.exception)) |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1134 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1135 | def testEntryArgs(self): |
| 1136 | """Test passing arguments to entries from the command line""" |
| 1137 | entry_args = { |
| 1138 | 'test-str-arg': 'test1', |
| 1139 | 'test-int-arg': '456', |
| 1140 | } |
| 1141 | self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args) |
| 1142 | self.assertIn('image', control.images) |
| 1143 | entry = control.images['image'].GetEntries()['_testing'] |
| 1144 | self.assertEqual('test0', entry.test_str_fdt) |
| 1145 | self.assertEqual('test1', entry.test_str_arg) |
| 1146 | self.assertEqual(123, entry.test_int_fdt) |
| 1147 | self.assertEqual(456, entry.test_int_arg) |
| 1148 | |
| 1149 | def testEntryArgsMissing(self): |
| 1150 | """Test missing arguments and properties""" |
| 1151 | entry_args = { |
| 1152 | 'test-int-arg': '456', |
| 1153 | } |
| 1154 | self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args) |
| 1155 | entry = control.images['image'].GetEntries()['_testing'] |
| 1156 | self.assertEqual('test0', entry.test_str_fdt) |
| 1157 | self.assertEqual(None, entry.test_str_arg) |
| 1158 | self.assertEqual(None, entry.test_int_fdt) |
| 1159 | self.assertEqual(456, entry.test_int_arg) |
| 1160 | |
| 1161 | def testEntryArgsRequired(self): |
| 1162 | """Test missing arguments and properties""" |
| 1163 | entry_args = { |
| 1164 | 'test-int-arg': '456', |
| 1165 | } |
| 1166 | with self.assertRaises(ValueError) as e: |
| 1167 | self._DoReadFileDtb('64_entry_args_required.dts') |
| 1168 | self.assertIn("Node '/binman/_testing': Missing required " |
| 1169 | 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg', |
| 1170 | str(e.exception)) |
| 1171 | |
| 1172 | def testEntryArgsInvalidFormat(self): |
| 1173 | """Test that an invalid entry-argument format is detected""" |
| 1174 | args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value'] |
| 1175 | with self.assertRaises(ValueError) as e: |
| 1176 | self._DoBinman(*args) |
| 1177 | self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception)) |
| 1178 | |
| 1179 | def testEntryArgsInvalidInteger(self): |
| 1180 | """Test that an invalid entry-argument integer is detected""" |
| 1181 | entry_args = { |
| 1182 | 'test-int-arg': 'abc', |
| 1183 | } |
| 1184 | with self.assertRaises(ValueError) as e: |
| 1185 | self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args) |
| 1186 | self.assertIn("Node '/binman/_testing': Cannot convert entry arg " |
| 1187 | "'test-int-arg' (value 'abc') to integer", |
| 1188 | str(e.exception)) |
| 1189 | |
| 1190 | def testEntryArgsInvalidDatatype(self): |
| 1191 | """Test that an invalid entry-argument datatype is detected |
| 1192 | |
| 1193 | This test could be written in entry_test.py except that it needs |
| 1194 | access to control.entry_args, which seems more than that module should |
| 1195 | be able to see. |
| 1196 | """ |
| 1197 | entry_args = { |
| 1198 | 'test-bad-datatype-arg': '12', |
| 1199 | } |
| 1200 | with self.assertRaises(ValueError) as e: |
| 1201 | self._DoReadFileDtb('65_entry_args_unknown_datatype.dts', |
| 1202 | entry_args=entry_args) |
| 1203 | self.assertIn('GetArg() internal error: Unknown data type ', |
| 1204 | str(e.exception)) |
| 1205 | |
Simon Glass | bb74837 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1206 | def testText(self): |
| 1207 | """Test for a text entry type""" |
| 1208 | entry_args = { |
| 1209 | 'test-id': TEXT_DATA, |
| 1210 | 'test-id2': TEXT_DATA2, |
| 1211 | 'test-id3': TEXT_DATA3, |
| 1212 | } |
| 1213 | data, _, _, _ = self._DoReadFileDtb('66_text.dts', |
| 1214 | entry_args=entry_args) |
| 1215 | expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 + |
| 1216 | TEXT_DATA3 + 'some text') |
| 1217 | self.assertEqual(expected, data) |
| 1218 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1219 | def testEntryDocs(self): |
| 1220 | """Test for creation of entry documentation""" |
| 1221 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1222 | control.WriteEntryDocs(binman.GetEntryModules()) |
| 1223 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 1224 | |
| 1225 | def testEntryDocsMissing(self): |
| 1226 | """Test handling of missing entry documentation""" |
| 1227 | with self.assertRaises(ValueError) as e: |
| 1228 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1229 | control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot') |
| 1230 | self.assertIn('Documentation is missing for modules: u_boot', |
| 1231 | str(e.exception)) |
| 1232 | |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1233 | def testFmap(self): |
| 1234 | """Basic test of generation of a flashrom fmap""" |
| 1235 | data = self._DoReadFile('67_fmap.dts') |
| 1236 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
| 1237 | expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 |
| 1238 | self.assertEqual(expected, data[:32]) |
| 1239 | self.assertEqual('__FMAP__', fhdr.signature) |
| 1240 | self.assertEqual(1, fhdr.ver_major) |
| 1241 | self.assertEqual(0, fhdr.ver_minor) |
| 1242 | self.assertEqual(0, fhdr.base) |
| 1243 | self.assertEqual(16 + 16 + |
| 1244 | fmap_util.FMAP_HEADER_LEN + |
| 1245 | fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size) |
| 1246 | self.assertEqual('FMAP', fhdr.name) |
| 1247 | self.assertEqual(3, fhdr.nareas) |
| 1248 | for fentry in fentries: |
| 1249 | self.assertEqual(0, fentry.flags) |
| 1250 | |
| 1251 | self.assertEqual(0, fentries[0].offset) |
| 1252 | self.assertEqual(4, fentries[0].size) |
| 1253 | self.assertEqual('RO_U_BOOT', fentries[0].name) |
| 1254 | |
| 1255 | self.assertEqual(16, fentries[1].offset) |
| 1256 | self.assertEqual(4, fentries[1].size) |
| 1257 | self.assertEqual('RW_U_BOOT', fentries[1].name) |
| 1258 | |
| 1259 | self.assertEqual(32, fentries[2].offset) |
| 1260 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1261 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
| 1262 | self.assertEqual('FMAP', fentries[2].name) |
| 1263 | |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1264 | def testBlobNamedByArg(self): |
| 1265 | """Test we can add a blob with the filename coming from an entry arg""" |
| 1266 | entry_args = { |
| 1267 | 'cros-ec-rw-path': 'ecrw.bin', |
| 1268 | } |
| 1269 | data, _, _, _ = self._DoReadFileDtb('68_blob_named_by_arg.dts', |
| 1270 | entry_args=entry_args) |
| 1271 | |
Simon Glass | 3af8e49 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1272 | def testFill(self): |
| 1273 | """Test for an fill entry type""" |
| 1274 | data = self._DoReadFile('69_fill.dts') |
| 1275 | expected = 8 * chr(0xff) + 8 * chr(0) |
| 1276 | self.assertEqual(expected, data) |
| 1277 | |
| 1278 | def testFillNoSize(self): |
| 1279 | """Test for an fill entry type with no size""" |
| 1280 | with self.assertRaises(ValueError) as e: |
| 1281 | self._DoReadFile('70_fill_no_size.dts') |
| 1282 | self.assertIn("'fill' entry must have a size property", |
| 1283 | str(e.exception)) |
| 1284 | |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1285 | def _HandleGbbCommand(self, pipe_list): |
| 1286 | """Fake calls to the futility utility""" |
| 1287 | if pipe_list[0][0] == 'futility': |
| 1288 | fname = pipe_list[0][-1] |
| 1289 | # Append our GBB data to the file, which will happen every time the |
| 1290 | # futility command is called. |
| 1291 | with open(fname, 'a') as fd: |
| 1292 | fd.write(GBB_DATA) |
| 1293 | return command.CommandResult() |
| 1294 | |
| 1295 | def testGbb(self): |
| 1296 | """Test for the Chromium OS Google Binary Block""" |
| 1297 | command.test_result = self._HandleGbbCommand |
| 1298 | entry_args = { |
| 1299 | 'keydir': 'devkeys', |
| 1300 | 'bmpblk': 'bmpblk.bin', |
| 1301 | } |
| 1302 | data, _, _, _ = self._DoReadFileDtb('71_gbb.dts', entry_args=entry_args) |
| 1303 | |
| 1304 | # Since futility |
| 1305 | expected = GBB_DATA + GBB_DATA + 8 * chr(0) + (0x2180 - 16) * chr(0) |
| 1306 | self.assertEqual(expected, data) |
| 1307 | |
| 1308 | def testGbbTooSmall(self): |
| 1309 | """Test for the Chromium OS Google Binary Block being large enough""" |
| 1310 | with self.assertRaises(ValueError) as e: |
| 1311 | self._DoReadFileDtb('72_gbb_too_small.dts') |
| 1312 | self.assertIn("Node '/binman/gbb': GBB is too small", |
| 1313 | str(e.exception)) |
| 1314 | |
| 1315 | def testGbbNoSize(self): |
| 1316 | """Test for the Chromium OS Google Binary Block having a size""" |
| 1317 | with self.assertRaises(ValueError) as e: |
| 1318 | self._DoReadFileDtb('73_gbb_no_size.dts') |
| 1319 | self.assertIn("Node '/binman/gbb': GBB must have a fixed size", |
| 1320 | str(e.exception)) |
| 1321 | |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1322 | def _HandleVblockCommand(self, pipe_list): |
| 1323 | """Fake calls to the futility utility""" |
| 1324 | if pipe_list[0][0] == 'futility': |
| 1325 | fname = pipe_list[0][3] |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 1326 | with open(fname, 'wb') as fd: |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1327 | fd.write(VBLOCK_DATA) |
| 1328 | return command.CommandResult() |
| 1329 | |
| 1330 | def testVblock(self): |
| 1331 | """Test for the Chromium OS Verified Boot Block""" |
| 1332 | command.test_result = self._HandleVblockCommand |
| 1333 | entry_args = { |
| 1334 | 'keydir': 'devkeys', |
| 1335 | } |
| 1336 | data, _, _, _ = self._DoReadFileDtb('74_vblock.dts', |
| 1337 | entry_args=entry_args) |
| 1338 | expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA |
| 1339 | self.assertEqual(expected, data) |
| 1340 | |
| 1341 | def testVblockNoContent(self): |
| 1342 | """Test we detect a vblock which has no content to sign""" |
| 1343 | with self.assertRaises(ValueError) as e: |
| 1344 | self._DoReadFile('75_vblock_no_content.dts') |
| 1345 | self.assertIn("Node '/binman/vblock': Vblock must have a 'content' " |
| 1346 | 'property', str(e.exception)) |
| 1347 | |
| 1348 | def testVblockBadPhandle(self): |
| 1349 | """Test that we detect a vblock with an invalid phandle in contents""" |
| 1350 | with self.assertRaises(ValueError) as e: |
| 1351 | self._DoReadFile('76_vblock_bad_phandle.dts') |
| 1352 | self.assertIn("Node '/binman/vblock': Cannot find node for phandle " |
| 1353 | '1000', str(e.exception)) |
| 1354 | |
| 1355 | def testVblockBadEntry(self): |
| 1356 | """Test that we detect an entry that points to a non-entry""" |
| 1357 | with self.assertRaises(ValueError) as e: |
| 1358 | self._DoReadFile('77_vblock_bad_entry.dts') |
| 1359 | self.assertIn("Node '/binman/vblock': Cannot find entry for node " |
| 1360 | "'other'", str(e.exception)) |
| 1361 | |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1362 | def testTpl(self): |
| 1363 | """Test that an image with TPL and ots device tree can be created""" |
| 1364 | # ELF file with a '__bss_size' symbol |
| 1365 | with open(self.TestFile('bss_data')) as fd: |
| 1366 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read()) |
| 1367 | data = self._DoReadFile('78_u_boot_tpl.dts') |
| 1368 | self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data) |
| 1369 | |
Simon Glass | 15a587c | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1370 | def testUsesPos(self): |
| 1371 | """Test that the 'pos' property cannot be used anymore""" |
| 1372 | with self.assertRaises(ValueError) as e: |
| 1373 | data = self._DoReadFile('79_uses_pos.dts') |
| 1374 | self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of " |
| 1375 | "'pos'", str(e.exception)) |
| 1376 | |
Simon Glass | d178eab | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1377 | def testFillZero(self): |
| 1378 | """Test for an fill entry type with a size of 0""" |
| 1379 | data = self._DoReadFile('80_fill_empty.dts') |
| 1380 | self.assertEqual(chr(0) * 16, data) |
| 1381 | |
Simon Glass | 0b48936 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1382 | def testTextMissing(self): |
| 1383 | """Test for a text entry type where there is no text""" |
| 1384 | with self.assertRaises(ValueError) as e: |
| 1385 | self._DoReadFileDtb('66_text.dts',) |
| 1386 | self.assertIn("Node '/binman/text': No value provided for text label " |
| 1387 | "'test-id'", str(e.exception)) |
| 1388 | |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1389 | def testPackStart16Tpl(self): |
| 1390 | """Test that an image with an x86 start16 TPL region can be created""" |
| 1391 | data = self._DoReadFile('81_x86-start16-tpl.dts') |
| 1392 | self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)]) |
| 1393 | |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1394 | def testSelectImage(self): |
| 1395 | """Test that we can select which images to build""" |
| 1396 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1397 | retcode = self._DoTestFile('06_dual_image.dts', images=['image2']) |
| 1398 | self.assertEqual(0, retcode) |
| 1399 | self.assertIn('Skipping images: image1', stdout.getvalue()) |
| 1400 | |
| 1401 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin'))) |
| 1402 | self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin'))) |
| 1403 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1404 | |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1405 | if __name__ == "__main__": |
| 1406 | unittest.main() |