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