Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 57454f4 | 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 | 57454f4 | 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 | |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 9 | import collections |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 10 | import gzip |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 11 | import hashlib |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 12 | from optparse import OptionParser |
| 13 | import os |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 14 | import re |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 15 | import shutil |
| 16 | import struct |
| 17 | import sys |
| 18 | import tempfile |
| 19 | import unittest |
| 20 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 21 | from binman import cbfs_util |
| 22 | from binman import cmdline |
| 23 | from binman import control |
| 24 | from binman import elf |
| 25 | from binman import elf_test |
| 26 | from binman import fmap_util |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 27 | from binman import state |
| 28 | from dtoc import fdt |
| 29 | from dtoc import fdt_util |
| 30 | from binman.etype import fdtmap |
| 31 | from binman.etype import image_header |
Simon Glass | 90cd6f0 | 2020-08-05 13:27:47 -0600 | [diff] [blame] | 32 | from binman.image import Image |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 33 | from patman import command |
| 34 | from patman import test_util |
| 35 | from patman import tools |
| 36 | from patman import tout |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 37 | |
| 38 | # Contents of test files, corresponding to different entry types |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 39 | U_BOOT_DATA = b'1234' |
| 40 | U_BOOT_IMG_DATA = b'img' |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 41 | U_BOOT_SPL_DATA = b'56780123456789abcdefghi' |
| 42 | U_BOOT_TPL_DATA = b'tpl9876543210fedcbazyw' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 43 | BLOB_DATA = b'89' |
| 44 | ME_DATA = b'0abcd' |
| 45 | VGA_DATA = b'vga' |
| 46 | U_BOOT_DTB_DATA = b'udtb' |
| 47 | U_BOOT_SPL_DTB_DATA = b'spldtb' |
| 48 | U_BOOT_TPL_DTB_DATA = b'tpldtb' |
| 49 | X86_START16_DATA = b'start16' |
| 50 | X86_START16_SPL_DATA = b'start16spl' |
| 51 | X86_START16_TPL_DATA = b'start16tpl' |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 52 | X86_RESET16_DATA = b'reset16' |
| 53 | X86_RESET16_SPL_DATA = b'reset16spl' |
| 54 | X86_RESET16_TPL_DATA = b'reset16tpl' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 55 | PPC_MPC85XX_BR_DATA = b'ppcmpc85xxbr' |
| 56 | U_BOOT_NODTB_DATA = b'nodtb with microcode pointer somewhere in here' |
| 57 | U_BOOT_SPL_NODTB_DATA = b'splnodtb with microcode pointer somewhere in here' |
| 58 | U_BOOT_TPL_NODTB_DATA = b'tplnodtb with microcode pointer somewhere in here' |
| 59 | FSP_DATA = b'fsp' |
| 60 | CMC_DATA = b'cmc' |
| 61 | VBT_DATA = b'vbt' |
| 62 | MRC_DATA = b'mrc' |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 63 | TEXT_DATA = 'text' |
| 64 | TEXT_DATA2 = 'text2' |
| 65 | TEXT_DATA3 = 'text3' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 66 | CROS_EC_RW_DATA = b'ecrw' |
| 67 | GBB_DATA = b'gbbd' |
| 68 | BMPBLK_DATA = b'bmp' |
| 69 | VBLOCK_DATA = b'vblk' |
| 70 | FILES_DATA = (b"sorry I'm late\nOh, don't bother apologising, I'm " + |
| 71 | b"sorry you're alive\n") |
Simon Glass | ccec026 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 72 | COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data' |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 73 | COMPRESS_DATA_BIG = COMPRESS_DATA * 2 |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 74 | REFCODE_DATA = b'refcode' |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 75 | FSP_M_DATA = b'fsp_m' |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 76 | FSP_S_DATA = b'fsp_s' |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 77 | FSP_T_DATA = b'fsp_t' |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 78 | ATF_BL31_DATA = b'bl31' |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 79 | SCP_DATA = b'scp' |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 80 | TEST_FDT1_DATA = b'fdt1' |
| 81 | TEST_FDT2_DATA = b'test-fdt2' |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 82 | ENV_DATA = b'var1=1\nvar2="2"' |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 83 | |
| 84 | # Subdirectory of the input dir to use to put test FDTs |
| 85 | TEST_FDT_SUBDIR = 'fdts' |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 86 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 87 | # The expected size for the device tree in some tests |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 88 | EXTRACT_DTB_SIZE = 0x3c9 |
| 89 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 90 | # Properties expected to be in the device tree when update_dtb is used |
| 91 | BASE_DTB_PROPS = ['offset', 'size', 'image-pos'] |
| 92 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 93 | # Extra properties expected to be in the device tree when allow-repack is used |
| 94 | REPACK_DTB_PROPS = ['orig-offset', 'orig-size'] |
| 95 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 96 | |
| 97 | class TestFunctional(unittest.TestCase): |
| 98 | """Functional tests for binman |
| 99 | |
| 100 | Most of these use a sample .dts file to build an image and then check |
| 101 | that it looks correct. The sample files are in the test/ subdirectory |
| 102 | and are numbered. |
| 103 | |
| 104 | For each entry type a very small test file is created using fixed |
| 105 | string contents. This makes it easy to test that things look right, and |
| 106 | debug problems. |
| 107 | |
| 108 | In some cases a 'real' file must be used - these are also supplied in |
| 109 | the test/ diurectory. |
| 110 | """ |
| 111 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 112 | def setUpClass(cls): |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 113 | global entry |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 114 | from binman import entry |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 115 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 116 | # Handle the case where argv[0] is 'python' |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 117 | cls._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 118 | cls._binman_pathname = os.path.join(cls._binman_dir, 'binman') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 119 | |
| 120 | # Create a temporary directory for input files |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 121 | cls._indir = tempfile.mkdtemp(prefix='binmant.') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 122 | |
| 123 | # Create some test files |
| 124 | TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) |
| 125 | TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) |
| 126 | TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 127 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 128 | TestFunctional._MakeInputFile('blobfile', BLOB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 129 | TestFunctional._MakeInputFile('me.bin', ME_DATA) |
| 130 | TestFunctional._MakeInputFile('vga.bin', VGA_DATA) |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 131 | cls._ResetDtbs() |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 132 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 133 | TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 134 | |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 135 | TestFunctional._MakeInputFile('u-boot-x86-start16.bin', X86_START16_DATA) |
| 136 | TestFunctional._MakeInputFile('spl/u-boot-x86-start16-spl.bin', |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 137 | X86_START16_SPL_DATA) |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 138 | TestFunctional._MakeInputFile('tpl/u-boot-x86-start16-tpl.bin', |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 139 | X86_START16_TPL_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 140 | |
| 141 | TestFunctional._MakeInputFile('u-boot-x86-reset16.bin', |
| 142 | X86_RESET16_DATA) |
| 143 | TestFunctional._MakeInputFile('spl/u-boot-x86-reset16-spl.bin', |
| 144 | X86_RESET16_SPL_DATA) |
| 145 | TestFunctional._MakeInputFile('tpl/u-boot-x86-reset16-tpl.bin', |
| 146 | X86_RESET16_TPL_DATA) |
| 147 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 148 | TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 149 | TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin', |
| 150 | U_BOOT_SPL_NODTB_DATA) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 151 | TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin', |
| 152 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 153 | TestFunctional._MakeInputFile('fsp.bin', FSP_DATA) |
| 154 | TestFunctional._MakeInputFile('cmc.bin', CMC_DATA) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 155 | TestFunctional._MakeInputFile('vbt.bin', VBT_DATA) |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 156 | TestFunctional._MakeInputFile('mrc.bin', MRC_DATA) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 157 | TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 158 | TestFunctional._MakeInputDir('devkeys') |
| 159 | TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA) |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 160 | TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 161 | TestFunctional._MakeInputFile('fsp_m.bin', FSP_M_DATA) |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 162 | TestFunctional._MakeInputFile('fsp_s.bin', FSP_S_DATA) |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 163 | TestFunctional._MakeInputFile('fsp_t.bin', FSP_T_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 164 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 165 | cls._elf_testdir = os.path.join(cls._indir, 'elftest') |
| 166 | elf_test.BuildElfTestFiles(cls._elf_testdir) |
| 167 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 168 | # ELF file with a '_dt_ucode_base_size' symbol |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 169 | TestFunctional._MakeInputFile('u-boot', |
| 170 | tools.ReadFile(cls.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 171 | |
| 172 | # Intel flash descriptor file |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 173 | cls._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 174 | |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 175 | shutil.copytree(cls.TestFile('files'), |
| 176 | os.path.join(cls._indir, 'files')) |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 177 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 178 | TestFunctional._MakeInputFile('compress', COMPRESS_DATA) |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 179 | TestFunctional._MakeInputFile('compress_big', COMPRESS_DATA_BIG) |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 180 | TestFunctional._MakeInputFile('bl31.bin', ATF_BL31_DATA) |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 181 | TestFunctional._MakeInputFile('scp.bin', SCP_DATA) |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 182 | |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 183 | # Add a few .dtb files for testing |
| 184 | TestFunctional._MakeInputFile('%s/test-fdt1.dtb' % TEST_FDT_SUBDIR, |
| 185 | TEST_FDT1_DATA) |
| 186 | TestFunctional._MakeInputFile('%s/test-fdt2.dtb' % TEST_FDT_SUBDIR, |
| 187 | TEST_FDT2_DATA) |
| 188 | |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 189 | TestFunctional._MakeInputFile('env.txt', ENV_DATA) |
| 190 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 191 | # Travis-CI may have an old lz4 |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 192 | cls.have_lz4 = True |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 193 | try: |
| 194 | tools.Run('lz4', '--no-frame-crc', '-c', |
Simon Glass | cc311ac | 2019-10-31 07:42:50 -0600 | [diff] [blame] | 195 | os.path.join(cls._indir, 'u-boot.bin'), binary=True) |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 196 | except: |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 197 | cls.have_lz4 = False |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 198 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 199 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 200 | def tearDownClass(cls): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 201 | """Remove the temporary input directory and its contents""" |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 202 | if cls.preserve_indir: |
| 203 | print('Preserving input dir: %s' % cls._indir) |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 204 | else: |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 205 | if cls._indir: |
| 206 | shutil.rmtree(cls._indir) |
| 207 | cls._indir = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 208 | |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 209 | @classmethod |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 210 | def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False, |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 211 | toolpath=None, verbosity=None): |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 212 | """Accept arguments controlling test execution |
| 213 | |
| 214 | Args: |
| 215 | preserve_indir: Preserve the shared input directory used by all |
| 216 | tests in this class. |
| 217 | preserve_outdir: Preserve the output directories used by tests. Each |
| 218 | test has its own, so this is normally only useful when running a |
| 219 | single test. |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 220 | toolpath: ist of paths to use for tools |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 221 | """ |
| 222 | cls.preserve_indir = preserve_indir |
| 223 | cls.preserve_outdirs = preserve_outdirs |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 224 | cls.toolpath = toolpath |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 225 | cls.verbosity = verbosity |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 226 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 227 | def _CheckLz4(self): |
| 228 | if not self.have_lz4: |
| 229 | self.skipTest('lz4 --no-frame-crc not available') |
| 230 | |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 231 | def _CleanupOutputDir(self): |
| 232 | """Remove the temporary output directory""" |
| 233 | if self.preserve_outdirs: |
| 234 | print('Preserving output dir: %s' % tools.outdir) |
| 235 | else: |
| 236 | tools._FinaliseForTest() |
| 237 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 238 | def setUp(self): |
| 239 | # Enable this to turn on debugging output |
| 240 | # tout.Init(tout.DEBUG) |
| 241 | command.test_result = None |
| 242 | |
| 243 | def tearDown(self): |
| 244 | """Remove the temporary output directory""" |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 245 | self._CleanupOutputDir() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 246 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 247 | def _SetupImageInTmpdir(self): |
| 248 | """Set up the output image in a new temporary directory |
| 249 | |
| 250 | This is used when an image has been generated in the output directory, |
| 251 | but we want to run binman again. This will create a new output |
| 252 | directory and fail to delete the original one. |
| 253 | |
| 254 | This creates a new temporary directory, copies the image to it (with a |
| 255 | new name) and removes the old output directory. |
| 256 | |
| 257 | Returns: |
| 258 | Tuple: |
| 259 | Temporary directory to use |
| 260 | New image filename |
| 261 | """ |
| 262 | image_fname = tools.GetOutputFilename('image.bin') |
| 263 | tmpdir = tempfile.mkdtemp(prefix='binman.') |
| 264 | updated_fname = os.path.join(tmpdir, 'image-updated.bin') |
| 265 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
| 266 | self._CleanupOutputDir() |
| 267 | return tmpdir, updated_fname |
| 268 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 269 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 270 | def _ResetDtbs(cls): |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 271 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
| 272 | TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA) |
| 273 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA) |
| 274 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 275 | def _RunBinman(self, *args, **kwargs): |
| 276 | """Run binman using the command line |
| 277 | |
| 278 | Args: |
| 279 | Arguments to pass, as a list of strings |
| 280 | kwargs: Arguments to pass to Command.RunPipe() |
| 281 | """ |
| 282 | result = command.RunPipe([[self._binman_pathname] + list(args)], |
| 283 | capture=True, capture_stderr=True, raise_on_error=False) |
| 284 | if result.return_code and kwargs.get('raise_on_error', True): |
| 285 | raise Exception("Error running '%s': %s" % (' '.join(args), |
| 286 | result.stdout + result.stderr)) |
| 287 | return result |
| 288 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 289 | def _DoBinman(self, *argv): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 290 | """Run binman using directly (in the same process) |
| 291 | |
| 292 | Args: |
| 293 | Arguments to pass, as a list of strings |
| 294 | Returns: |
| 295 | Return value (0 for success) |
| 296 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 297 | argv = list(argv) |
| 298 | args = cmdline.ParseArgs(argv) |
| 299 | args.pager = 'binman-invalid-pager' |
| 300 | args.build_dir = self._indir |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 301 | |
| 302 | # For testing, you can force an increase in verbosity here |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 303 | # args.verbosity = tout.DEBUG |
| 304 | return control.Binman(args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 305 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 306 | def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False, |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 307 | entry_args=None, images=None, use_real_dtb=False, |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 308 | verbosity=None, allow_missing=False, extra_indirs=None): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 309 | """Run binman with a given test file |
| 310 | |
| 311 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 312 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 313 | debug: True to enable debugging output |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 314 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 315 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 316 | tree before packing it into the image |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 317 | entry_args: Dict of entry args to supply to binman |
| 318 | key: arg name |
| 319 | value: value of that arg |
| 320 | images: List of image names to build |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 321 | use_real_dtb: True to use the test file as the contents of |
| 322 | the u-boot-dtb entry. Normally this is not needed and the |
| 323 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 324 | But in some test we need the real contents. |
| 325 | verbosity: Verbosity level to use (0-3, None=don't set it) |
| 326 | allow_missing: Set the '--allow-missing' flag so that missing |
| 327 | external binaries just produce a warning instead of an error |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 328 | extra_indirs: Extra input directories to add using -I |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 329 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 330 | args = [] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 331 | if debug: |
| 332 | args.append('-D') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 333 | if verbosity is not None: |
| 334 | args.append('-v%d' % verbosity) |
| 335 | elif self.verbosity: |
| 336 | args.append('-v%d' % self.verbosity) |
| 337 | if self.toolpath: |
| 338 | for path in self.toolpath: |
| 339 | args += ['--toolpath', path] |
| 340 | args += ['build', '-p', '-I', self._indir, '-d', self.TestFile(fname)] |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 341 | if map: |
| 342 | args.append('-m') |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 343 | if update_dtb: |
Simon Glass | 38a411c | 2019-07-08 13:18:47 -0600 | [diff] [blame] | 344 | args.append('-u') |
Simon Glass | 3140201 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 345 | if not use_real_dtb: |
| 346 | args.append('--fake-dtb') |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 347 | if entry_args: |
Simon Glass | 5f3645b | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 348 | for arg, value in entry_args.items(): |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 349 | args.append('-a%s=%s' % (arg, value)) |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 350 | if allow_missing: |
| 351 | args.append('-M') |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 352 | if images: |
| 353 | for image in images: |
| 354 | args += ['-i', image] |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 355 | if extra_indirs: |
| 356 | for indir in extra_indirs: |
| 357 | args += ['-I', indir] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 358 | return self._DoBinman(*args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 359 | |
| 360 | def _SetupDtb(self, fname, outfile='u-boot.dtb'): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 361 | """Set up a new test device-tree file |
| 362 | |
| 363 | The given file is compiled and set up as the device tree to be used |
| 364 | for ths test. |
| 365 | |
| 366 | Args: |
| 367 | fname: Filename of .dts file to read |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 368 | outfile: Output filename for compiled device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 369 | |
| 370 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 371 | Contents of device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 372 | """ |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 373 | tmpdir = tempfile.mkdtemp(prefix='binmant.') |
| 374 | dtb = fdt_util.EnsureCompiled(self.TestFile(fname), tmpdir) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 375 | with open(dtb, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 376 | data = fd.read() |
| 377 | TestFunctional._MakeInputFile(outfile, data) |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 378 | shutil.rmtree(tmpdir) |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 379 | return data |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 380 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 381 | def _GetDtbContentsForSplTpl(self, dtb_data, name): |
| 382 | """Create a version of the main DTB for SPL or SPL |
| 383 | |
| 384 | For testing we don't actually have different versions of the DTB. With |
| 385 | U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests |
| 386 | we don't normally have any unwanted nodes. |
| 387 | |
| 388 | We still want the DTBs for SPL and TPL to be different though, since |
| 389 | otherwise it is confusing to know which one we are looking at. So add |
| 390 | an 'spl' or 'tpl' property to the top-level node. |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 391 | |
| 392 | Args: |
| 393 | dtb_data: dtb data to modify (this should be a value devicetree) |
| 394 | name: Name of a new property to add |
| 395 | |
| 396 | Returns: |
| 397 | New dtb data with the property added |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 398 | """ |
| 399 | dtb = fdt.Fdt.FromData(dtb_data) |
| 400 | dtb.Scan() |
| 401 | dtb.GetNode('/binman').AddZeroProp(name) |
| 402 | dtb.Sync(auto_resize=True) |
| 403 | dtb.Pack() |
| 404 | return dtb.GetContents() |
| 405 | |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 406 | def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False, |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 407 | update_dtb=False, entry_args=None, reset_dtbs=True, |
| 408 | extra_indirs=None): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 409 | """Run binman and return the resulting image |
| 410 | |
| 411 | This runs binman with a given test file and then reads the resulting |
| 412 | output file. It is a shortcut function since most tests need to do |
| 413 | these steps. |
| 414 | |
| 415 | Raises an assertion failure if binman returns a non-zero exit code. |
| 416 | |
| 417 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 418 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 419 | use_real_dtb: True to use the test file as the contents of |
| 420 | the u-boot-dtb entry. Normally this is not needed and the |
| 421 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 422 | But in some test we need the real contents. |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 423 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 424 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 425 | tree before packing it into the image |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 426 | entry_args: Dict of entry args to supply to binman |
| 427 | key: arg name |
| 428 | value: value of that arg |
| 429 | reset_dtbs: With use_real_dtb the test dtb is overwritten by this |
| 430 | function. If reset_dtbs is True, then the original test dtb |
| 431 | is written back before this function finishes |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 432 | extra_indirs: Extra input directories to add using -I |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 433 | |
| 434 | Returns: |
| 435 | Tuple: |
| 436 | Resulting image contents |
| 437 | Device tree contents |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 438 | Map data showing contents of image (or None if none) |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 439 | Output device tree binary filename ('u-boot.dtb' path) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 440 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 441 | dtb_data = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 442 | # Use the compiled test file as the u-boot-dtb input |
| 443 | if use_real_dtb: |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 444 | dtb_data = self._SetupDtb(fname) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 445 | |
| 446 | # For testing purposes, make a copy of the DT for SPL and TPL. Add |
| 447 | # a node indicating which it is, so aid verification. |
| 448 | for name in ['spl', 'tpl']: |
| 449 | dtb_fname = '%s/u-boot-%s.dtb' % (name, name) |
| 450 | outfile = os.path.join(self._indir, dtb_fname) |
| 451 | TestFunctional._MakeInputFile(dtb_fname, |
| 452 | self._GetDtbContentsForSplTpl(dtb_data, name)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 453 | |
| 454 | try: |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 455 | retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb, |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 456 | entry_args=entry_args, use_real_dtb=use_real_dtb, |
| 457 | extra_indirs=extra_indirs) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 458 | self.assertEqual(0, retcode) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 459 | out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 460 | |
| 461 | # Find the (only) image, read it and return its contents |
| 462 | image = control.images['image'] |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 463 | image_fname = tools.GetOutputFilename('image.bin') |
| 464 | self.assertTrue(os.path.exists(image_fname)) |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 465 | if map: |
| 466 | map_fname = tools.GetOutputFilename('image.map') |
| 467 | with open(map_fname) as fd: |
| 468 | map_data = fd.read() |
| 469 | else: |
| 470 | map_data = None |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 471 | with open(image_fname, 'rb') as fd: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 472 | return fd.read(), dtb_data, map_data, out_dtb_fname |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 473 | finally: |
| 474 | # Put the test file back |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 475 | if reset_dtbs and use_real_dtb: |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 476 | self._ResetDtbs() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 477 | |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 478 | def _DoReadFileRealDtb(self, fname): |
| 479 | """Run binman with a real .dtb file and return the resulting data |
| 480 | |
| 481 | Args: |
| 482 | fname: DT source filename to use (e.g. 082_fdt_update_all.dts) |
| 483 | |
| 484 | Returns: |
| 485 | Resulting image contents |
| 486 | """ |
| 487 | return self._DoReadFileDtb(fname, use_real_dtb=True, update_dtb=True)[0] |
| 488 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 489 | def _DoReadFile(self, fname, use_real_dtb=False): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 490 | """Helper function which discards the device-tree binary |
| 491 | |
| 492 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 493 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 494 | use_real_dtb: True to use the test file as the contents of |
| 495 | the u-boot-dtb entry. Normally this is not needed and the |
| 496 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 497 | But in some test we need the real contents. |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 498 | |
| 499 | Returns: |
| 500 | Resulting image contents |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 501 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 502 | return self._DoReadFileDtb(fname, use_real_dtb)[0] |
| 503 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 504 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 505 | def _MakeInputFile(cls, fname, contents): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 506 | """Create a new test input file, creating directories as needed |
| 507 | |
| 508 | Args: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 509 | fname: Filename to create |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 510 | contents: File contents to write in to the file |
| 511 | Returns: |
| 512 | Full pathname of file created |
| 513 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 514 | pathname = os.path.join(cls._indir, fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 515 | dirname = os.path.dirname(pathname) |
| 516 | if dirname and not os.path.exists(dirname): |
| 517 | os.makedirs(dirname) |
| 518 | with open(pathname, 'wb') as fd: |
| 519 | fd.write(contents) |
| 520 | return pathname |
| 521 | |
| 522 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 523 | def _MakeInputDir(cls, dirname): |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 524 | """Create a new test input directory, creating directories as needed |
| 525 | |
| 526 | Args: |
| 527 | dirname: Directory name to create |
| 528 | |
| 529 | Returns: |
| 530 | Full pathname of directory created |
| 531 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 532 | pathname = os.path.join(cls._indir, dirname) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 533 | if not os.path.exists(pathname): |
| 534 | os.makedirs(pathname) |
| 535 | return pathname |
| 536 | |
| 537 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 538 | def _SetupSplElf(cls, src_fname='bss_data'): |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 539 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 540 | |
| 541 | Args: |
| 542 | Filename of ELF file to use as SPL |
| 543 | """ |
Simon Glass | 93a806f | 2019-08-24 07:22:59 -0600 | [diff] [blame] | 544 | TestFunctional._MakeInputFile('spl/u-boot-spl', |
| 545 | tools.ReadFile(cls.ElfTestFile(src_fname))) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 546 | |
| 547 | @classmethod |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 548 | def _SetupTplElf(cls, src_fname='bss_data'): |
| 549 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 550 | |
| 551 | Args: |
| 552 | Filename of ELF file to use as TPL |
| 553 | """ |
| 554 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', |
| 555 | tools.ReadFile(cls.ElfTestFile(src_fname))) |
| 556 | |
| 557 | @classmethod |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 558 | def _SetupDescriptor(cls): |
| 559 | with open(cls.TestFile('descriptor.bin'), 'rb') as fd: |
| 560 | TestFunctional._MakeInputFile('descriptor.bin', fd.read()) |
| 561 | |
| 562 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 563 | def TestFile(cls, fname): |
| 564 | return os.path.join(cls._binman_dir, 'test', fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 565 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 566 | @classmethod |
| 567 | def ElfTestFile(cls, fname): |
| 568 | return os.path.join(cls._elf_testdir, fname) |
| 569 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 570 | def AssertInList(self, grep_list, target): |
| 571 | """Assert that at least one of a list of things is in a target |
| 572 | |
| 573 | Args: |
| 574 | grep_list: List of strings to check |
| 575 | target: Target string |
| 576 | """ |
| 577 | for grep in grep_list: |
| 578 | if grep in target: |
| 579 | return |
Simon Glass | 848cdb5 | 2019-05-17 22:00:50 -0600 | [diff] [blame] | 580 | self.fail("Error: '%s' not found in '%s'" % (grep_list, target)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 581 | |
| 582 | def CheckNoGaps(self, entries): |
| 583 | """Check that all entries fit together without gaps |
| 584 | |
| 585 | Args: |
| 586 | entries: List of entries to check |
| 587 | """ |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 588 | offset = 0 |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 589 | for entry in entries.values(): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 590 | self.assertEqual(offset, entry.offset) |
| 591 | offset += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 592 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 593 | def GetFdtLen(self, dtb): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 594 | """Get the totalsize field from a device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 595 | |
| 596 | Args: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 597 | dtb: Device-tree binary contents |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 598 | |
| 599 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 600 | Total size of device-tree binary, from the header |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 601 | """ |
| 602 | return struct.unpack('>L', dtb[4:8])[0] |
| 603 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 604 | def _GetPropTree(self, dtb, prop_names, prefix='/binman/'): |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 605 | def AddNode(node, path): |
| 606 | if node.name != '/': |
| 607 | path += '/' + node.name |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 608 | for prop in node.props.values(): |
| 609 | if prop.name in prop_names: |
| 610 | prop_path = path + ':' + prop.name |
| 611 | tree[prop_path[len(prefix):]] = fdt_util.fdt32_to_cpu( |
| 612 | prop.value) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 613 | for subnode in node.subnodes: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 614 | AddNode(subnode, path) |
| 615 | |
| 616 | tree = {} |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 617 | AddNode(dtb.GetRoot(), '') |
| 618 | return tree |
| 619 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 620 | def testRun(self): |
| 621 | """Test a basic run with valid args""" |
| 622 | result = self._RunBinman('-h') |
| 623 | |
| 624 | def testFullHelp(self): |
| 625 | """Test that the full help is displayed with -H""" |
| 626 | result = self._RunBinman('-H') |
| 627 | help_file = os.path.join(self._binman_dir, 'README') |
Tom Rini | c3c0b6d | 2018-01-16 15:29:50 -0500 | [diff] [blame] | 628 | # Remove possible extraneous strings |
| 629 | extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n' |
| 630 | gothelp = result.stdout.replace(extra, '') |
| 631 | self.assertEqual(len(gothelp), os.path.getsize(help_file)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 632 | self.assertEqual(0, len(result.stderr)) |
| 633 | self.assertEqual(0, result.return_code) |
| 634 | |
| 635 | def testFullHelpInternal(self): |
| 636 | """Test that the full help is displayed with -H""" |
| 637 | try: |
| 638 | command.test_result = command.CommandResult() |
| 639 | result = self._DoBinman('-H') |
| 640 | help_file = os.path.join(self._binman_dir, 'README') |
| 641 | finally: |
| 642 | command.test_result = None |
| 643 | |
| 644 | def testHelp(self): |
| 645 | """Test that the basic help is displayed with -h""" |
| 646 | result = self._RunBinman('-h') |
| 647 | self.assertTrue(len(result.stdout) > 200) |
| 648 | self.assertEqual(0, len(result.stderr)) |
| 649 | self.assertEqual(0, result.return_code) |
| 650 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 651 | def testBoard(self): |
| 652 | """Test that we can run it with a specific board""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 653 | self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 654 | TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 655 | result = self._DoBinman('build', '-b', 'sandbox') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 656 | self.assertEqual(0, result) |
| 657 | |
| 658 | def testNeedBoard(self): |
| 659 | """Test that we get an error when no board ius supplied""" |
| 660 | with self.assertRaises(ValueError) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 661 | result = self._DoBinman('build') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 662 | self.assertIn("Must provide a board to process (use -b <board>)", |
| 663 | str(e.exception)) |
| 664 | |
| 665 | def testMissingDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 666 | """Test that an invalid device-tree file generates an error""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 667 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 668 | self._RunBinman('build', '-d', 'missing_file') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 669 | # We get one error from libfdt, and a different one from fdtget. |
| 670 | self.AssertInList(["Couldn't open blob from 'missing_file'", |
| 671 | 'No such file or directory'], str(e.exception)) |
| 672 | |
| 673 | def testBrokenDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 674 | """Test that an invalid device-tree source file generates an error |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 675 | |
| 676 | Since this is a source file it should be compiled and the error |
| 677 | will come from the device-tree compiler (dtc). |
| 678 | """ |
| 679 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 680 | self._RunBinman('build', '-d', self.TestFile('001_invalid.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 681 | self.assertIn("FATAL ERROR: Unable to parse input tree", |
| 682 | str(e.exception)) |
| 683 | |
| 684 | def testMissingNode(self): |
| 685 | """Test that a device tree without a 'binman' node generates an error""" |
| 686 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 687 | self._DoBinman('build', '-d', self.TestFile('002_missing_node.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 688 | self.assertIn("does not have a 'binman' node", str(e.exception)) |
| 689 | |
| 690 | def testEmpty(self): |
| 691 | """Test that an empty binman node works OK (i.e. does nothing)""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 692 | result = self._RunBinman('build', '-d', self.TestFile('003_empty.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 693 | self.assertEqual(0, len(result.stderr)) |
| 694 | self.assertEqual(0, result.return_code) |
| 695 | |
| 696 | def testInvalidEntry(self): |
| 697 | """Test that an invalid entry is flagged""" |
| 698 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 699 | result = self._RunBinman('build', '-d', |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 700 | self.TestFile('004_invalid_entry.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 701 | self.assertIn("Unknown entry type 'not-a-valid-type' in node " |
| 702 | "'/binman/not-a-valid-type'", str(e.exception)) |
| 703 | |
| 704 | def testSimple(self): |
| 705 | """Test a simple binman with a single file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 706 | data = self._DoReadFile('005_simple.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 707 | self.assertEqual(U_BOOT_DATA, data) |
| 708 | |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 709 | def testSimpleDebug(self): |
| 710 | """Test a simple binman run with debugging enabled""" |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 711 | self._DoTestFile('005_simple.dts', debug=True) |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 712 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 713 | def testDual(self): |
| 714 | """Test that we can handle creating two images |
| 715 | |
| 716 | This also tests image padding. |
| 717 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 718 | retcode = self._DoTestFile('006_dual_image.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 719 | self.assertEqual(0, retcode) |
| 720 | |
| 721 | image = control.images['image1'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 722 | self.assertEqual(len(U_BOOT_DATA), image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 723 | fname = tools.GetOutputFilename('image1.bin') |
| 724 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 725 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 726 | data = fd.read() |
| 727 | self.assertEqual(U_BOOT_DATA, data) |
| 728 | |
| 729 | image = control.images['image2'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 730 | self.assertEqual(3 + len(U_BOOT_DATA) + 5, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 731 | fname = tools.GetOutputFilename('image2.bin') |
| 732 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 733 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 734 | data = fd.read() |
| 735 | self.assertEqual(U_BOOT_DATA, data[3:7]) |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 736 | self.assertEqual(tools.GetBytes(0, 3), data[:3]) |
| 737 | self.assertEqual(tools.GetBytes(0, 5), data[7:]) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 738 | |
| 739 | def testBadAlign(self): |
| 740 | """Test that an invalid alignment value is detected""" |
| 741 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 742 | self._DoTestFile('007_bad_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 743 | self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " |
| 744 | "of two", str(e.exception)) |
| 745 | |
| 746 | def testPackSimple(self): |
| 747 | """Test that packing works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 748 | retcode = self._DoTestFile('008_pack.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 749 | self.assertEqual(0, retcode) |
| 750 | self.assertIn('image', control.images) |
| 751 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 752 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 753 | self.assertEqual(5, len(entries)) |
| 754 | |
| 755 | # First u-boot |
| 756 | self.assertIn('u-boot', entries) |
| 757 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 758 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 759 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 760 | |
| 761 | # Second u-boot, aligned to 16-byte boundary |
| 762 | self.assertIn('u-boot-align', entries) |
| 763 | entry = entries['u-boot-align'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 764 | self.assertEqual(16, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 765 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 766 | |
| 767 | # Third u-boot, size 23 bytes |
| 768 | self.assertIn('u-boot-size', entries) |
| 769 | entry = entries['u-boot-size'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 770 | self.assertEqual(20, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 771 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 772 | self.assertEqual(23, entry.size) |
| 773 | |
| 774 | # Fourth u-boot, placed immediate after the above |
| 775 | self.assertIn('u-boot-next', entries) |
| 776 | entry = entries['u-boot-next'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 777 | self.assertEqual(43, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 778 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 779 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 780 | # Fifth u-boot, placed at a fixed offset |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 781 | self.assertIn('u-boot-fixed', entries) |
| 782 | entry = entries['u-boot-fixed'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 783 | self.assertEqual(61, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 784 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 785 | |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 786 | self.assertEqual(65, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 787 | |
| 788 | def testPackExtra(self): |
| 789 | """Test that extra packing feature works as expected""" |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 790 | data, _, _, out_dtb_fname = self._DoReadFileDtb('009_pack_extra.dts', |
| 791 | update_dtb=True) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 792 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 793 | self.assertIn('image', control.images) |
| 794 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 795 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 796 | self.assertEqual(5, len(entries)) |
| 797 | |
| 798 | # First u-boot with padding before and after |
| 799 | self.assertIn('u-boot', entries) |
| 800 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 801 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 802 | self.assertEqual(3, entry.pad_before) |
| 803 | self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 804 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 805 | self.assertEqual(tools.GetBytes(0, 3) + U_BOOT_DATA + |
| 806 | tools.GetBytes(0, 5), data[:entry.size]) |
| 807 | pos = entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 808 | |
| 809 | # Second u-boot has an aligned size, but it has no effect |
| 810 | self.assertIn('u-boot-align-size-nop', entries) |
| 811 | entry = entries['u-boot-align-size-nop'] |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 812 | self.assertEqual(pos, entry.offset) |
| 813 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 814 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 815 | self.assertEqual(U_BOOT_DATA, data[pos:pos + entry.size]) |
| 816 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 817 | |
| 818 | # Third u-boot has an aligned size too |
| 819 | self.assertIn('u-boot-align-size', entries) |
| 820 | entry = entries['u-boot-align-size'] |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 821 | self.assertEqual(pos, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 822 | self.assertEqual(32, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 823 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 824 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 32 - len(U_BOOT_DATA)), |
| 825 | data[pos:pos + entry.size]) |
| 826 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 827 | |
| 828 | # Fourth u-boot has an aligned end |
| 829 | self.assertIn('u-boot-align-end', entries) |
| 830 | entry = entries['u-boot-align-end'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 831 | self.assertEqual(48, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 832 | self.assertEqual(16, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 833 | self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)]) |
| 834 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 16 - len(U_BOOT_DATA)), |
| 835 | data[pos:pos + entry.size]) |
| 836 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 837 | |
| 838 | # Fifth u-boot immediately afterwards |
| 839 | self.assertIn('u-boot-align-both', entries) |
| 840 | entry = entries['u-boot-align-both'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 841 | self.assertEqual(64, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 842 | self.assertEqual(64, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 843 | self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)]) |
| 844 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 64 - len(U_BOOT_DATA)), |
| 845 | data[pos:pos + entry.size]) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 846 | |
| 847 | self.CheckNoGaps(entries) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 848 | self.assertEqual(128, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 849 | |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 850 | dtb = fdt.Fdt(out_dtb_fname) |
| 851 | dtb.Scan() |
| 852 | props = self._GetPropTree(dtb, ['size', 'offset', 'image-pos']) |
| 853 | expected = { |
| 854 | 'image-pos': 0, |
| 855 | 'offset': 0, |
| 856 | 'size': 128, |
| 857 | |
| 858 | 'u-boot:image-pos': 0, |
| 859 | 'u-boot:offset': 0, |
| 860 | 'u-boot:size': 3 + 5 + len(U_BOOT_DATA), |
| 861 | |
| 862 | 'u-boot-align-size-nop:image-pos': 12, |
| 863 | 'u-boot-align-size-nop:offset': 12, |
| 864 | 'u-boot-align-size-nop:size': 4, |
| 865 | |
| 866 | 'u-boot-align-size:image-pos': 16, |
| 867 | 'u-boot-align-size:offset': 16, |
| 868 | 'u-boot-align-size:size': 32, |
| 869 | |
| 870 | 'u-boot-align-end:image-pos': 48, |
| 871 | 'u-boot-align-end:offset': 48, |
| 872 | 'u-boot-align-end:size': 16, |
| 873 | |
| 874 | 'u-boot-align-both:image-pos': 64, |
| 875 | 'u-boot-align-both:offset': 64, |
| 876 | 'u-boot-align-both:size': 64, |
| 877 | } |
| 878 | self.assertEqual(expected, props) |
| 879 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 880 | def testPackAlignPowerOf2(self): |
| 881 | """Test that invalid entry alignment is detected""" |
| 882 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 883 | self._DoTestFile('010_pack_align_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 884 | self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " |
| 885 | "of two", str(e.exception)) |
| 886 | |
| 887 | def testPackAlignSizePowerOf2(self): |
| 888 | """Test that invalid entry size alignment is detected""" |
| 889 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 890 | self._DoTestFile('011_pack_align_size_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 891 | self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " |
| 892 | "power of two", str(e.exception)) |
| 893 | |
| 894 | def testPackInvalidAlign(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 895 | """Test detection of an offset that does not match its alignment""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 896 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 897 | self._DoTestFile('012_pack_inv_align.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 898 | self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 899 | "align 0x4 (4)", str(e.exception)) |
| 900 | |
| 901 | def testPackInvalidSizeAlign(self): |
| 902 | """Test that invalid entry size alignment is detected""" |
| 903 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 904 | self._DoTestFile('013_pack_inv_size_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 905 | self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " |
| 906 | "align-size 0x4 (4)", str(e.exception)) |
| 907 | |
| 908 | def testPackOverlap(self): |
| 909 | """Test that overlapping regions are detected""" |
| 910 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 911 | self._DoTestFile('014_pack_overlap.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 912 | self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 913 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 914 | str(e.exception)) |
| 915 | |
| 916 | def testPackEntryOverflow(self): |
| 917 | """Test that entries that overflow their size are detected""" |
| 918 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 919 | self._DoTestFile('015_pack_overflow.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 920 | self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " |
| 921 | "but entry size is 0x3 (3)", str(e.exception)) |
| 922 | |
| 923 | def testPackImageOverflow(self): |
| 924 | """Test that entries which overflow the image size are detected""" |
| 925 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 926 | self._DoTestFile('016_pack_image_overflow.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 927 | self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 928 | "size 0x3 (3)", str(e.exception)) |
| 929 | |
| 930 | def testPackImageSize(self): |
| 931 | """Test that the image size can be set""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 932 | retcode = self._DoTestFile('017_pack_image_size.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 933 | self.assertEqual(0, retcode) |
| 934 | self.assertIn('image', control.images) |
| 935 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 936 | self.assertEqual(7, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 937 | |
| 938 | def testPackImageSizeAlign(self): |
| 939 | """Test that image size alignemnt works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 940 | retcode = self._DoTestFile('018_pack_image_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 941 | self.assertEqual(0, retcode) |
| 942 | self.assertIn('image', control.images) |
| 943 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 944 | self.assertEqual(16, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 945 | |
| 946 | def testPackInvalidImageAlign(self): |
| 947 | """Test that invalid image alignment is detected""" |
| 948 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 949 | self._DoTestFile('019_pack_inv_image_align.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 950 | self.assertIn("Section '/binman': Size 0x7 (7) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 951 | "align-size 0x8 (8)", str(e.exception)) |
| 952 | |
| 953 | def testPackAlignPowerOf2(self): |
| 954 | """Test that invalid image alignment is detected""" |
| 955 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 956 | self._DoTestFile('020_pack_inv_image_align_power2.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 957 | self.assertIn("Image '/binman': Alignment size 131 must be a power of " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 958 | "two", str(e.exception)) |
| 959 | |
| 960 | def testImagePadByte(self): |
| 961 | """Test that the image pad byte can be specified""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 962 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 963 | data = self._DoReadFile('021_image_pad.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 964 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) + |
| 965 | U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 966 | |
| 967 | def testImageName(self): |
| 968 | """Test that image files can be named""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 969 | retcode = self._DoTestFile('022_image_name.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 970 | self.assertEqual(0, retcode) |
| 971 | image = control.images['image1'] |
| 972 | fname = tools.GetOutputFilename('test-name') |
| 973 | self.assertTrue(os.path.exists(fname)) |
| 974 | |
| 975 | image = control.images['image2'] |
| 976 | fname = tools.GetOutputFilename('test-name.xx') |
| 977 | self.assertTrue(os.path.exists(fname)) |
| 978 | |
| 979 | def testBlobFilename(self): |
| 980 | """Test that generic blobs can be provided by filename""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 981 | data = self._DoReadFile('023_blob.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 982 | self.assertEqual(BLOB_DATA, data) |
| 983 | |
| 984 | def testPackSorted(self): |
| 985 | """Test that entries can be sorted""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 986 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 987 | data = self._DoReadFile('024_sorted.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 988 | self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA + |
| 989 | tools.GetBytes(0, 2) + U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 990 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 991 | def testPackZeroOffset(self): |
| 992 | """Test that an entry at offset 0 is not given a new offset""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 993 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 994 | self._DoTestFile('025_pack_zero_size.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 995 | self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 996 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 997 | str(e.exception)) |
| 998 | |
| 999 | def testPackUbootDtb(self): |
| 1000 | """Test that a device tree can be added to U-Boot""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1001 | data = self._DoReadFile('026_pack_u_boot_dtb.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1002 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1003 | |
| 1004 | def testPackX86RomNoSize(self): |
| 1005 | """Test that the end-at-4gb property requires a size property""" |
| 1006 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1007 | self._DoTestFile('027_pack_4gb_no_size.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1008 | self.assertIn("Image '/binman': Section size must be provided when " |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1009 | "using end-at-4gb", str(e.exception)) |
| 1010 | |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 1011 | def test4gbAndSkipAtStartTogether(self): |
| 1012 | """Test that the end-at-4gb and skip-at-size property can't be used |
| 1013 | together""" |
| 1014 | with self.assertRaises(ValueError) as e: |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 1015 | self._DoTestFile('098_4gb_and_skip_at_start_together.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1016 | self.assertIn("Image '/binman': Provide either 'end-at-4gb' or " |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 1017 | "'skip-at-start'", str(e.exception)) |
| 1018 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1019 | def testPackX86RomOutside(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1020 | """Test that the end-at-4gb property checks for offset boundaries""" |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1021 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1022 | self._DoTestFile('028_pack_4gb_outside.dts') |
Simon Glass | d617986 | 2020-10-26 17:40:05 -0600 | [diff] [blame] | 1023 | self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) size 0x4 (4) " |
| 1024 | "is outside the section '/binman' starting at " |
| 1025 | '0xffffffe0 (4294967264) of size 0x20 (32)', |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1026 | str(e.exception)) |
| 1027 | |
| 1028 | def testPackX86Rom(self): |
| 1029 | """Test that a basic x86 ROM can be created""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1030 | self._SetupSplElf() |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1031 | data = self._DoReadFile('029_x86_rom.dts') |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 1032 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 3) + U_BOOT_SPL_DATA + |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1033 | tools.GetBytes(0, 2), data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1034 | |
| 1035 | def testPackX86RomMeNoDesc(self): |
| 1036 | """Test that an invalid Intel descriptor entry is detected""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1037 | try: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 1038 | TestFunctional._MakeInputFile('descriptor-empty.bin', b'') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1039 | with self.assertRaises(ValueError) as e: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 1040 | self._DoTestFile('163_x86_rom_me_empty.dts') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1041 | self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature", |
| 1042 | str(e.exception)) |
| 1043 | finally: |
| 1044 | self._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1045 | |
| 1046 | def testPackX86RomBadDesc(self): |
| 1047 | """Test that the Intel requires a descriptor entry""" |
| 1048 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1049 | self._DoTestFile('030_x86_rom_me_no_desc.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1050 | self.assertIn("Node '/binman/intel-me': No offset set with " |
| 1051 | "offset-unset: should another entry provide this correct " |
| 1052 | "offset?", str(e.exception)) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1053 | |
| 1054 | def testPackX86RomMe(self): |
| 1055 | """Test that an x86 ROM with an ME region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1056 | data = self._DoReadFile('031_x86_rom_me.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 1057 | expected_desc = tools.ReadFile(self.TestFile('descriptor.bin')) |
| 1058 | if data[:0x1000] != expected_desc: |
| 1059 | self.fail('Expected descriptor binary at start of image') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1060 | self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) |
| 1061 | |
| 1062 | def testPackVga(self): |
| 1063 | """Test that an image with a VGA binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1064 | data = self._DoReadFile('032_intel_vga.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1065 | self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) |
| 1066 | |
| 1067 | def testPackStart16(self): |
| 1068 | """Test that an image with an x86 start16 region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1069 | data = self._DoReadFile('033_x86_start16.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1070 | self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) |
| 1071 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 1072 | def testPackPowerpcMpc85xxBootpgResetvec(self): |
| 1073 | """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be |
| 1074 | created""" |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 1075 | data = self._DoReadFile('150_powerpc_mpc85xx_bootpg_resetvec.dts') |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 1076 | self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)]) |
| 1077 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1078 | def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1079 | """Handle running a test for insertion of microcode |
| 1080 | |
| 1081 | Args: |
| 1082 | dts_fname: Name of test .dts file |
| 1083 | nodtb_data: Data that we expect in the first section |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1084 | ucode_second: True if the microsecond entry is second instead of |
| 1085 | third |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1086 | |
| 1087 | Returns: |
| 1088 | Tuple: |
| 1089 | Contents of first region (U-Boot or SPL) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1090 | Offset and size components of microcode pointer, as inserted |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1091 | in the above (two 4-byte words) |
| 1092 | """ |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1093 | data = self._DoReadFile(dts_fname, True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1094 | |
| 1095 | # Now check the device tree has no microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1096 | if ucode_second: |
| 1097 | ucode_content = data[len(nodtb_data):] |
| 1098 | ucode_pos = len(nodtb_data) |
| 1099 | dtb_with_ucode = ucode_content[16:] |
| 1100 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1101 | else: |
| 1102 | dtb_with_ucode = data[len(nodtb_data):] |
| 1103 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1104 | ucode_content = dtb_with_ucode[fdt_len:] |
| 1105 | ucode_pos = len(nodtb_data) + fdt_len |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1106 | fname = tools.GetOutputFilename('test.dtb') |
| 1107 | with open(fname, 'wb') as fd: |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1108 | fd.write(dtb_with_ucode) |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 1109 | dtb = fdt.FdtScan(fname) |
| 1110 | ucode = dtb.GetNode('/microcode') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1111 | self.assertTrue(ucode) |
| 1112 | for node in ucode.subnodes: |
| 1113 | self.assertFalse(node.props.get('data')) |
| 1114 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1115 | # Check that the microcode appears immediately after the Fdt |
| 1116 | # This matches the concatenation of the data properties in |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1117 | # the /microcode/update@xxx nodes in 34_x86_ucode.dts. |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1118 | ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, |
| 1119 | 0x78235609) |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1120 | self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1121 | |
| 1122 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1123 | # expected offset and size |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1124 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1125 | len(ucode_data)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1126 | u_boot = data[:len(nodtb_data)] |
| 1127 | return u_boot, pos_and_size |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1128 | |
| 1129 | def testPackUbootMicrocode(self): |
| 1130 | """Test that x86 microcode can be handled correctly |
| 1131 | |
| 1132 | We expect to see the following in the image, in order: |
| 1133 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1134 | place |
| 1135 | u-boot.dtb with the microcode removed |
| 1136 | the microcode |
| 1137 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1138 | first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts', |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1139 | U_BOOT_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1140 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1141 | b' somewhere in here', first) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1142 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1143 | def _RunPackUbootSingleMicrocode(self): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1144 | """Test that x86 microcode can be handled correctly |
| 1145 | |
| 1146 | We expect to see the following in the image, in order: |
| 1147 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1148 | place |
| 1149 | u-boot.dtb with the microcode |
| 1150 | an empty microcode region |
| 1151 | """ |
| 1152 | # We need the libfdt library to run this test since only that allows |
| 1153 | # finding the offset of a property. This is required by |
| 1154 | # Entry_u_boot_dtb_with_ucode.ObtainContents(). |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1155 | data = self._DoReadFile('035_x86_single_ucode.dts', True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1156 | |
| 1157 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1158 | |
| 1159 | fdt_len = self.GetFdtLen(second) |
| 1160 | third = second[fdt_len:] |
| 1161 | second = second[:fdt_len] |
| 1162 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1163 | ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) |
| 1164 | self.assertIn(ucode_data, second) |
| 1165 | ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1166 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1167 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1168 | # expected offset and size |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1169 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1170 | len(ucode_data)) |
| 1171 | first = data[:len(U_BOOT_NODTB_DATA)] |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1172 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1173 | b' somewhere in here', first) |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1174 | |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1175 | def testPackUbootSingleMicrocode(self): |
| 1176 | """Test that x86 microcode can be handled correctly with fdt_normal. |
| 1177 | """ |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1178 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1179 | |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1180 | def testUBootImg(self): |
| 1181 | """Test that u-boot.img can be put in a file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1182 | data = self._DoReadFile('036_u_boot_img.dts') |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1183 | self.assertEqual(U_BOOT_IMG_DATA, data) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1184 | |
| 1185 | def testNoMicrocode(self): |
| 1186 | """Test that a missing microcode region is detected""" |
| 1187 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1188 | self._DoReadFile('037_x86_no_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1189 | self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode " |
| 1190 | "node found in ", str(e.exception)) |
| 1191 | |
| 1192 | def testMicrocodeWithoutNode(self): |
| 1193 | """Test that a missing u-boot-dtb-with-ucode node is detected""" |
| 1194 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1195 | self._DoReadFile('038_x86_ucode_missing_node.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1196 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1197 | "microcode region u-boot-dtb-with-ucode", str(e.exception)) |
| 1198 | |
| 1199 | def testMicrocodeWithoutNode2(self): |
| 1200 | """Test that a missing u-boot-ucode node is detected""" |
| 1201 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1202 | self._DoReadFile('039_x86_ucode_missing_node2.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1203 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1204 | "microcode region u-boot-ucode", str(e.exception)) |
| 1205 | |
| 1206 | def testMicrocodeWithoutPtrInElf(self): |
| 1207 | """Test that a U-Boot binary without the microcode symbol is detected""" |
| 1208 | # ELF file without a '_dt_ucode_base_size' symbol |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1209 | try: |
Simon Glass | faaaa16 | 2019-08-24 07:22:55 -0600 | [diff] [blame] | 1210 | TestFunctional._MakeInputFile('u-boot', |
| 1211 | tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1212 | |
| 1213 | with self.assertRaises(ValueError) as e: |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1214 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1215 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate " |
| 1216 | "_dt_ucode_base_size symbol in u-boot", str(e.exception)) |
| 1217 | |
| 1218 | finally: |
| 1219 | # Put the original file back |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 1220 | TestFunctional._MakeInputFile('u-boot', |
| 1221 | tools.ReadFile(self.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1222 | |
| 1223 | def testMicrocodeNotInImage(self): |
| 1224 | """Test that microcode must be placed within the image""" |
| 1225 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1226 | self._DoReadFile('040_x86_ucode_not_in_image.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1227 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode " |
| 1228 | "pointer _dt_ucode_base_size at fffffe14 is outside the " |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 1229 | "section ranging from 00000000 to 0000002e", str(e.exception)) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1230 | |
| 1231 | def testWithoutMicrocode(self): |
| 1232 | """Test that we can cope with an image without microcode (e.g. qemu)""" |
Simon Glass | faaaa16 | 2019-08-24 07:22:55 -0600 | [diff] [blame] | 1233 | TestFunctional._MakeInputFile('u-boot', |
| 1234 | tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1235 | data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1236 | |
| 1237 | # Now check the device tree has no microcode |
| 1238 | self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)]) |
| 1239 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1240 | |
| 1241 | fdt_len = self.GetFdtLen(second) |
| 1242 | self.assertEqual(dtb, second[:fdt_len]) |
| 1243 | |
| 1244 | used_len = len(U_BOOT_NODTB_DATA) + fdt_len |
| 1245 | third = data[used_len:] |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1246 | self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1247 | |
| 1248 | def testUnknownPosSize(self): |
| 1249 | """Test that microcode must be placed within the image""" |
| 1250 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1251 | self._DoReadFile('041_unknown_pos_size.dts', True) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1252 | self.assertIn("Section '/binman': Unable to set offset/size for unknown " |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1253 | "entry 'invalid-entry'", str(e.exception)) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1254 | |
| 1255 | def testPackFsp(self): |
| 1256 | """Test that an image with a FSP binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1257 | data = self._DoReadFile('042_intel_fsp.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1258 | self.assertEqual(FSP_DATA, data[:len(FSP_DATA)]) |
| 1259 | |
| 1260 | def testPackCmc(self): |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1261 | """Test that an image with a CMC binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1262 | data = self._DoReadFile('043_intel_cmc.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1263 | self.assertEqual(CMC_DATA, data[:len(CMC_DATA)]) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1264 | |
| 1265 | def testPackVbt(self): |
| 1266 | """Test that an image with a VBT binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1267 | data = self._DoReadFile('046_intel_vbt.dts') |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1268 | self.assertEqual(VBT_DATA, data[:len(VBT_DATA)]) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1269 | |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1270 | def testSplBssPad(self): |
| 1271 | """Test that we can pad SPL's BSS with zeros""" |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1272 | # ELF file with a '__bss_size' symbol |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1273 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1274 | data = self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1275 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA, |
| 1276 | data) |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1277 | |
Simon Glass | 04cda03 | 2018-10-01 21:12:42 -0600 | [diff] [blame] | 1278 | def testSplBssPadMissing(self): |
| 1279 | """Test that a missing symbol is detected""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1280 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1281 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1282 | self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1283 | self.assertIn('Expected __bss_size symbol in spl/u-boot-spl', |
| 1284 | str(e.exception)) |
| 1285 | |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1286 | def testPackStart16Spl(self): |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1287 | """Test that an image with an x86 start16 SPL region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1288 | data = self._DoReadFile('048_x86_start16_spl.dts') |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1289 | self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) |
| 1290 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1291 | def _PackUbootSplMicrocode(self, dts, ucode_second=False): |
| 1292 | """Helper function for microcode tests |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1293 | |
| 1294 | We expect to see the following in the image, in order: |
| 1295 | u-boot-spl-nodtb.bin with a microcode pointer inserted at the |
| 1296 | correct place |
| 1297 | u-boot.dtb with the microcode removed |
| 1298 | the microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1299 | |
| 1300 | Args: |
| 1301 | dts: Device tree file to use for test |
| 1302 | ucode_second: True if the microsecond entry is second instead of |
| 1303 | third |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1304 | """ |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1305 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1306 | first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, |
| 1307 | ucode_second=ucode_second) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1308 | self.assertEqual(b'splnodtb with microc' + pos_and_size + |
| 1309 | b'ter somewhere in here', first) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1310 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1311 | def testPackUbootSplMicrocode(self): |
| 1312 | """Test that x86 microcode can be handled correctly in SPL""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1313 | self._PackUbootSplMicrocode('049_x86_ucode_spl.dts') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1314 | |
| 1315 | def testPackUbootSplMicrocodeReorder(self): |
| 1316 | """Test that order doesn't matter for microcode entries |
| 1317 | |
| 1318 | This is the same as testPackUbootSplMicrocode but when we process the |
| 1319 | u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode |
| 1320 | entry, so we reply on binman to try later. |
| 1321 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1322 | self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts', |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1323 | ucode_second=True) |
| 1324 | |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1325 | def testPackMrc(self): |
| 1326 | """Test that an image with an MRC binary can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1327 | data = self._DoReadFile('050_intel_mrc.dts') |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1328 | self.assertEqual(MRC_DATA, data[:len(MRC_DATA)]) |
| 1329 | |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1330 | def testSplDtb(self): |
| 1331 | """Test that an image with spl/u-boot-spl.dtb can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1332 | data = self._DoReadFile('051_u_boot_spl_dtb.dts') |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1333 | self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)]) |
| 1334 | |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1335 | def testSplNoDtb(self): |
| 1336 | """Test that an image with spl/u-boot-spl-nodtb.bin can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1337 | data = self._DoReadFile('052_u_boot_spl_nodtb.dts') |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1338 | self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)]) |
| 1339 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1340 | def testSymbols(self): |
| 1341 | """Test binman can assign symbols embedded in U-Boot""" |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 1342 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1343 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 1344 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1345 | self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1346 | |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1347 | self._SetupSplElf('u_boot_binman_syms') |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1348 | data = self._DoReadFile('053_symbols.dts') |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 1349 | sym_values = struct.pack('<LQLL', 0x00, 0x1c, 0x28, 0x04) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 1350 | expected = (sym_values + U_BOOT_SPL_DATA[20:] + |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1351 | tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values + |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 1352 | U_BOOT_SPL_DATA[20:]) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1353 | self.assertEqual(expected, data) |
| 1354 | |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1355 | def testPackUnitAddress(self): |
| 1356 | """Test that we support multiple binaries with the same name""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1357 | data = self._DoReadFile('054_unit_address.dts') |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1358 | self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data) |
| 1359 | |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1360 | def testSections(self): |
| 1361 | """Basic test of sections""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1362 | data = self._DoReadFile('055_sections.dts') |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1363 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1364 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12) + |
| 1365 | U_BOOT_DATA + tools.GetBytes(ord('&'), 4)) |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1366 | self.assertEqual(expected, data) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1367 | |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1368 | def testMap(self): |
| 1369 | """Tests outputting a map of the images""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1370 | _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1371 | self.assertEqual('''ImagePos Offset Size Name |
| 1372 | 00000000 00000000 00000028 main-section |
| 1373 | 00000000 00000000 00000010 section@0 |
| 1374 | 00000000 00000000 00000004 u-boot |
| 1375 | 00000010 00000010 00000010 section@1 |
| 1376 | 00000010 00000000 00000004 u-boot |
| 1377 | 00000020 00000020 00000004 section@2 |
| 1378 | 00000020 00000000 00000004 u-boot |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1379 | ''', map_data) |
| 1380 | |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1381 | def testNamePrefix(self): |
| 1382 | """Tests that name prefixes are used""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1383 | _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1384 | self.assertEqual('''ImagePos Offset Size Name |
| 1385 | 00000000 00000000 00000028 main-section |
| 1386 | 00000000 00000000 00000010 section@0 |
| 1387 | 00000000 00000000 00000004 ro-u-boot |
| 1388 | 00000010 00000010 00000010 section@1 |
| 1389 | 00000010 00000000 00000004 rw-u-boot |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1390 | ''', map_data) |
| 1391 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1392 | def testUnknownContents(self): |
| 1393 | """Test that obtaining the contents works as expected""" |
| 1394 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1395 | self._DoReadFile('057_unknown_contents.dts', True) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1396 | self.assertIn("Image '/binman': Internal error: Could not complete " |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1397 | "processing of contents: remaining [" |
| 1398 | "<binman.etype._testing.Entry__testing ", str(e.exception)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1399 | |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1400 | def testBadChangeSize(self): |
| 1401 | """Test that trying to change the size of an entry fails""" |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 1402 | try: |
| 1403 | state.SetAllowEntryExpansion(False) |
| 1404 | with self.assertRaises(ValueError) as e: |
| 1405 | self._DoReadFile('059_change_size.dts', True) |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1406 | self.assertIn("Node '/binman/_testing': Cannot update entry size from 2 to 3", |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 1407 | str(e.exception)) |
| 1408 | finally: |
| 1409 | state.SetAllowEntryExpansion(True) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1410 | |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1411 | def testUpdateFdt(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1412 | """Test that we can update the device tree with offset/size info""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1413 | _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts', |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1414 | update_dtb=True) |
Simon Glass | 5463a6a | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 1415 | dtb = fdt.Fdt(out_dtb_fname) |
| 1416 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1417 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1418 | self.assertEqual({ |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1419 | 'image-pos': 0, |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 1420 | 'offset': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1421 | '_testing:offset': 32, |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1422 | '_testing:size': 2, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1423 | '_testing:image-pos': 32, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1424 | 'section@0/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1425 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1426 | 'section@0/u-boot:image-pos': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1427 | 'section@0:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1428 | 'section@0:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1429 | 'section@0:image-pos': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1430 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1431 | 'section@1/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1432 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1433 | 'section@1/u-boot:image-pos': 16, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1434 | 'section@1:offset': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1435 | 'section@1:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1436 | 'section@1:image-pos': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1437 | 'size': 40 |
| 1438 | }, props) |
| 1439 | |
| 1440 | def testUpdateFdtBad(self): |
| 1441 | """Test that we detect when ProcessFdt never completes""" |
| 1442 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1443 | self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1444 | self.assertIn('Could not complete processing of Fdt: remaining ' |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1445 | '[<binman.etype._testing.Entry__testing', |
| 1446 | str(e.exception)) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1447 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1448 | def testEntryArgs(self): |
| 1449 | """Test passing arguments to entries from the command line""" |
| 1450 | entry_args = { |
| 1451 | 'test-str-arg': 'test1', |
| 1452 | 'test-int-arg': '456', |
| 1453 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1454 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1455 | self.assertIn('image', control.images) |
| 1456 | entry = control.images['image'].GetEntries()['_testing'] |
| 1457 | self.assertEqual('test0', entry.test_str_fdt) |
| 1458 | self.assertEqual('test1', entry.test_str_arg) |
| 1459 | self.assertEqual(123, entry.test_int_fdt) |
| 1460 | self.assertEqual(456, entry.test_int_arg) |
| 1461 | |
| 1462 | def testEntryArgsMissing(self): |
| 1463 | """Test missing arguments and properties""" |
| 1464 | entry_args = { |
| 1465 | 'test-int-arg': '456', |
| 1466 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1467 | self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1468 | entry = control.images['image'].GetEntries()['_testing'] |
| 1469 | self.assertEqual('test0', entry.test_str_fdt) |
| 1470 | self.assertEqual(None, entry.test_str_arg) |
| 1471 | self.assertEqual(None, entry.test_int_fdt) |
| 1472 | self.assertEqual(456, entry.test_int_arg) |
| 1473 | |
| 1474 | def testEntryArgsRequired(self): |
| 1475 | """Test missing arguments and properties""" |
| 1476 | entry_args = { |
| 1477 | 'test-int-arg': '456', |
| 1478 | } |
| 1479 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1480 | self._DoReadFileDtb('064_entry_args_required.dts') |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1481 | self.assertIn("Node '/binman/_testing': " |
| 1482 | 'Missing required properties/entry args: test-str-arg, ' |
| 1483 | 'test-int-fdt, test-int-arg', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1484 | str(e.exception)) |
| 1485 | |
| 1486 | def testEntryArgsInvalidFormat(self): |
| 1487 | """Test that an invalid entry-argument format is detected""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 1488 | args = ['build', '-d', self.TestFile('064_entry_args_required.dts'), |
| 1489 | '-ano-value'] |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1490 | with self.assertRaises(ValueError) as e: |
| 1491 | self._DoBinman(*args) |
| 1492 | self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception)) |
| 1493 | |
| 1494 | def testEntryArgsInvalidInteger(self): |
| 1495 | """Test that an invalid entry-argument integer is detected""" |
| 1496 | entry_args = { |
| 1497 | 'test-int-arg': 'abc', |
| 1498 | } |
| 1499 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1500 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1501 | self.assertIn("Node '/binman/_testing': Cannot convert entry arg " |
| 1502 | "'test-int-arg' (value 'abc') to integer", |
| 1503 | str(e.exception)) |
| 1504 | |
| 1505 | def testEntryArgsInvalidDatatype(self): |
| 1506 | """Test that an invalid entry-argument datatype is detected |
| 1507 | |
| 1508 | This test could be written in entry_test.py except that it needs |
| 1509 | access to control.entry_args, which seems more than that module should |
| 1510 | be able to see. |
| 1511 | """ |
| 1512 | entry_args = { |
| 1513 | 'test-bad-datatype-arg': '12', |
| 1514 | } |
| 1515 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1516 | self._DoReadFileDtb('065_entry_args_unknown_datatype.dts', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1517 | entry_args=entry_args) |
| 1518 | self.assertIn('GetArg() internal error: Unknown data type ', |
| 1519 | str(e.exception)) |
| 1520 | |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1521 | def testText(self): |
| 1522 | """Test for a text entry type""" |
| 1523 | entry_args = { |
| 1524 | 'test-id': TEXT_DATA, |
| 1525 | 'test-id2': TEXT_DATA2, |
| 1526 | 'test-id3': TEXT_DATA3, |
| 1527 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1528 | data, _, _, _ = self._DoReadFileDtb('066_text.dts', |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1529 | entry_args=entry_args) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1530 | expected = (tools.ToBytes(TEXT_DATA) + |
| 1531 | tools.GetBytes(0, 8 - len(TEXT_DATA)) + |
| 1532 | tools.ToBytes(TEXT_DATA2) + tools.ToBytes(TEXT_DATA3) + |
Simon Glass | 47f6a62 | 2019-07-08 13:18:40 -0600 | [diff] [blame] | 1533 | b'some text' + b'more text') |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1534 | self.assertEqual(expected, data) |
| 1535 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1536 | def testEntryDocs(self): |
| 1537 | """Test for creation of entry documentation""" |
| 1538 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1539 | control.WriteEntryDocs(control.GetEntryModules()) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1540 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 1541 | |
| 1542 | def testEntryDocsMissing(self): |
| 1543 | """Test handling of missing entry documentation""" |
| 1544 | with self.assertRaises(ValueError) as e: |
| 1545 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1546 | control.WriteEntryDocs(control.GetEntryModules(), 'u_boot') |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1547 | self.assertIn('Documentation is missing for modules: u_boot', |
| 1548 | str(e.exception)) |
| 1549 | |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1550 | def testFmap(self): |
| 1551 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1552 | data = self._DoReadFile('067_fmap.dts') |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1553 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1554 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1555 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12)) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1556 | self.assertEqual(expected, data[:32]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1557 | self.assertEqual(b'__FMAP__', fhdr.signature) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1558 | self.assertEqual(1, fhdr.ver_major) |
| 1559 | self.assertEqual(0, fhdr.ver_minor) |
| 1560 | self.assertEqual(0, fhdr.base) |
| 1561 | self.assertEqual(16 + 16 + |
| 1562 | fmap_util.FMAP_HEADER_LEN + |
| 1563 | fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1564 | self.assertEqual(b'FMAP', fhdr.name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1565 | self.assertEqual(3, fhdr.nareas) |
| 1566 | for fentry in fentries: |
| 1567 | self.assertEqual(0, fentry.flags) |
| 1568 | |
| 1569 | self.assertEqual(0, fentries[0].offset) |
| 1570 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1571 | self.assertEqual(b'RO_U_BOOT', fentries[0].name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1572 | |
| 1573 | self.assertEqual(16, fentries[1].offset) |
| 1574 | self.assertEqual(4, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1575 | self.assertEqual(b'RW_U_BOOT', fentries[1].name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1576 | |
| 1577 | self.assertEqual(32, fentries[2].offset) |
| 1578 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1579 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1580 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1581 | |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1582 | def testBlobNamedByArg(self): |
| 1583 | """Test we can add a blob with the filename coming from an entry arg""" |
| 1584 | entry_args = { |
| 1585 | 'cros-ec-rw-path': 'ecrw.bin', |
| 1586 | } |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1587 | self._DoReadFileDtb('068_blob_named_by_arg.dts', entry_args=entry_args) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1588 | |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1589 | def testFill(self): |
| 1590 | """Test for an fill entry type""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1591 | data = self._DoReadFile('069_fill.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1592 | expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8) |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1593 | self.assertEqual(expected, data) |
| 1594 | |
| 1595 | def testFillNoSize(self): |
| 1596 | """Test for an fill entry type with no size""" |
| 1597 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1598 | self._DoReadFile('070_fill_no_size.dts') |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1599 | self.assertIn("'fill' entry must have a size property", |
| 1600 | str(e.exception)) |
| 1601 | |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1602 | def _HandleGbbCommand(self, pipe_list): |
| 1603 | """Fake calls to the futility utility""" |
| 1604 | if pipe_list[0][0] == 'futility': |
| 1605 | fname = pipe_list[0][-1] |
| 1606 | # Append our GBB data to the file, which will happen every time the |
| 1607 | # futility command is called. |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1608 | with open(fname, 'ab') as fd: |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1609 | fd.write(GBB_DATA) |
| 1610 | return command.CommandResult() |
| 1611 | |
| 1612 | def testGbb(self): |
| 1613 | """Test for the Chromium OS Google Binary Block""" |
| 1614 | command.test_result = self._HandleGbbCommand |
| 1615 | entry_args = { |
| 1616 | 'keydir': 'devkeys', |
| 1617 | 'bmpblk': 'bmpblk.bin', |
| 1618 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1619 | data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1620 | |
| 1621 | # Since futility |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1622 | expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) + |
| 1623 | tools.GetBytes(0, 0x2180 - 16)) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1624 | self.assertEqual(expected, data) |
| 1625 | |
| 1626 | def testGbbTooSmall(self): |
| 1627 | """Test for the Chromium OS Google Binary Block being large enough""" |
| 1628 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1629 | self._DoReadFileDtb('072_gbb_too_small.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1630 | self.assertIn("Node '/binman/gbb': GBB is too small", |
| 1631 | str(e.exception)) |
| 1632 | |
| 1633 | def testGbbNoSize(self): |
| 1634 | """Test for the Chromium OS Google Binary Block having a size""" |
| 1635 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1636 | self._DoReadFileDtb('073_gbb_no_size.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1637 | self.assertIn("Node '/binman/gbb': GBB must have a fixed size", |
| 1638 | str(e.exception)) |
| 1639 | |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1640 | def _HandleVblockCommand(self, pipe_list): |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1641 | """Fake calls to the futility utility |
| 1642 | |
| 1643 | The expected pipe is: |
| 1644 | |
| 1645 | [('futility', 'vbutil_firmware', '--vblock', |
| 1646 | 'vblock.vblock', '--keyblock', 'devkeys/firmware.keyblock', |
| 1647 | '--signprivate', 'devkeys/firmware_data_key.vbprivk', |
| 1648 | '--version', '1', '--fv', 'input.vblock', '--kernelkey', |
| 1649 | 'devkeys/kernel_subkey.vbpubk', '--flags', '1')] |
| 1650 | |
| 1651 | This writes to the output file (here, 'vblock.vblock'). If |
| 1652 | self._hash_data is False, it writes VBLOCK_DATA, else it writes a hash |
| 1653 | of the input data (here, 'input.vblock'). |
| 1654 | """ |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1655 | if pipe_list[0][0] == 'futility': |
| 1656 | fname = pipe_list[0][3] |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 1657 | with open(fname, 'wb') as fd: |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1658 | if self._hash_data: |
| 1659 | infile = pipe_list[0][11] |
| 1660 | m = hashlib.sha256() |
| 1661 | data = tools.ReadFile(infile) |
| 1662 | m.update(data) |
| 1663 | fd.write(m.digest()) |
| 1664 | else: |
| 1665 | fd.write(VBLOCK_DATA) |
| 1666 | |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1667 | return command.CommandResult() |
| 1668 | |
| 1669 | def testVblock(self): |
| 1670 | """Test for the Chromium OS Verified Boot Block""" |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1671 | self._hash_data = False |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1672 | command.test_result = self._HandleVblockCommand |
| 1673 | entry_args = { |
| 1674 | 'keydir': 'devkeys', |
| 1675 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1676 | data, _, _, _ = self._DoReadFileDtb('074_vblock.dts', |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1677 | entry_args=entry_args) |
| 1678 | expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA |
| 1679 | self.assertEqual(expected, data) |
| 1680 | |
| 1681 | def testVblockNoContent(self): |
| 1682 | """Test we detect a vblock which has no content to sign""" |
| 1683 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1684 | self._DoReadFile('075_vblock_no_content.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1685 | self.assertIn("Node '/binman/vblock': Vblock must have a 'content' " |
| 1686 | 'property', str(e.exception)) |
| 1687 | |
| 1688 | def testVblockBadPhandle(self): |
| 1689 | """Test that we detect a vblock with an invalid phandle in contents""" |
| 1690 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1691 | self._DoReadFile('076_vblock_bad_phandle.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1692 | self.assertIn("Node '/binman/vblock': Cannot find node for phandle " |
| 1693 | '1000', str(e.exception)) |
| 1694 | |
| 1695 | def testVblockBadEntry(self): |
| 1696 | """Test that we detect an entry that points to a non-entry""" |
| 1697 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1698 | self._DoReadFile('077_vblock_bad_entry.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1699 | self.assertIn("Node '/binman/vblock': Cannot find entry for node " |
| 1700 | "'other'", str(e.exception)) |
| 1701 | |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1702 | def testVblockContent(self): |
| 1703 | """Test that the vblock signs the right data""" |
| 1704 | self._hash_data = True |
| 1705 | command.test_result = self._HandleVblockCommand |
| 1706 | entry_args = { |
| 1707 | 'keydir': 'devkeys', |
| 1708 | } |
| 1709 | data = self._DoReadFileDtb( |
| 1710 | '189_vblock_content.dts', use_real_dtb=True, update_dtb=True, |
| 1711 | entry_args=entry_args)[0] |
| 1712 | hashlen = 32 # SHA256 hash is 32 bytes |
| 1713 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 1714 | hashval = data[-hashlen:] |
| 1715 | dtb = data[len(U_BOOT_DATA):-hashlen] |
| 1716 | |
| 1717 | expected_data = U_BOOT_DATA + dtb |
| 1718 | |
| 1719 | # The hashval should be a hash of the dtb |
| 1720 | m = hashlib.sha256() |
| 1721 | m.update(expected_data) |
| 1722 | expected_hashval = m.digest() |
| 1723 | self.assertEqual(expected_hashval, hashval) |
| 1724 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1725 | def testTpl(self): |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1726 | """Test that an image with TPL and its device tree can be created""" |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1727 | # ELF file with a '__bss_size' symbol |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1728 | self._SetupTplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1729 | data = self._DoReadFile('078_u_boot_tpl.dts') |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1730 | self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data) |
| 1731 | |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1732 | def testUsesPos(self): |
| 1733 | """Test that the 'pos' property cannot be used anymore""" |
| 1734 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1735 | data = self._DoReadFile('079_uses_pos.dts') |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1736 | self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of " |
| 1737 | "'pos'", str(e.exception)) |
| 1738 | |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1739 | def testFillZero(self): |
| 1740 | """Test for an fill entry type with a size of 0""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1741 | data = self._DoReadFile('080_fill_empty.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1742 | self.assertEqual(tools.GetBytes(0, 16), data) |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1743 | |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1744 | def testTextMissing(self): |
| 1745 | """Test for a text entry type where there is no text""" |
| 1746 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1747 | self._DoReadFileDtb('066_text.dts',) |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1748 | self.assertIn("Node '/binman/text': No value provided for text label " |
| 1749 | "'test-id'", str(e.exception)) |
| 1750 | |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1751 | def testPackStart16Tpl(self): |
| 1752 | """Test that an image with an x86 start16 TPL region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1753 | data = self._DoReadFile('081_x86_start16_tpl.dts') |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1754 | self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)]) |
| 1755 | |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1756 | def testSelectImage(self): |
| 1757 | """Test that we can select which images to build""" |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1758 | expected = 'Skipping images: image1' |
| 1759 | |
| 1760 | # We should only get the expected message in verbose mode |
Simon Glass | 8a50b4a | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 1761 | for verbosity in (0, 2): |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1762 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1763 | retcode = self._DoTestFile('006_dual_image.dts', |
| 1764 | verbosity=verbosity, |
| 1765 | images=['image2']) |
| 1766 | self.assertEqual(0, retcode) |
| 1767 | if verbosity: |
| 1768 | self.assertIn(expected, stdout.getvalue()) |
| 1769 | else: |
| 1770 | self.assertNotIn(expected, stdout.getvalue()) |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1771 | |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1772 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin'))) |
| 1773 | self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin'))) |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 1774 | self._CleanupOutputDir() |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1775 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1776 | def testUpdateFdtAll(self): |
| 1777 | """Test that all device trees are updated with offset/size info""" |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 1778 | data = self._DoReadFileRealDtb('082_fdt_update_all.dts') |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1779 | |
| 1780 | base_expected = { |
| 1781 | 'section:image-pos': 0, |
| 1782 | 'u-boot-tpl-dtb:size': 513, |
| 1783 | 'u-boot-spl-dtb:size': 513, |
| 1784 | 'u-boot-spl-dtb:offset': 493, |
| 1785 | 'image-pos': 0, |
| 1786 | 'section/u-boot-dtb:image-pos': 0, |
| 1787 | 'u-boot-spl-dtb:image-pos': 493, |
| 1788 | 'section/u-boot-dtb:size': 493, |
| 1789 | 'u-boot-tpl-dtb:image-pos': 1006, |
| 1790 | 'section/u-boot-dtb:offset': 0, |
| 1791 | 'section:size': 493, |
| 1792 | 'offset': 0, |
| 1793 | 'section:offset': 0, |
| 1794 | 'u-boot-tpl-dtb:offset': 1006, |
| 1795 | 'size': 1519 |
| 1796 | } |
| 1797 | |
| 1798 | # We expect three device-tree files in the output, one after the other. |
| 1799 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 1800 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 1801 | # main U-Boot tree. All three should have the same postions and offset. |
| 1802 | start = 0 |
| 1803 | for item in ['', 'spl', 'tpl']: |
| 1804 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1805 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1806 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS + |
| 1807 | ['spl', 'tpl']) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1808 | expected = dict(base_expected) |
| 1809 | if item: |
| 1810 | expected[item] = 0 |
| 1811 | self.assertEqual(expected, props) |
| 1812 | start += dtb._fdt_obj.totalsize() |
| 1813 | |
| 1814 | def testUpdateFdtOutput(self): |
| 1815 | """Test that output DTB files are updated""" |
| 1816 | try: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1817 | data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts', |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1818 | use_real_dtb=True, update_dtb=True, reset_dtbs=False) |
| 1819 | |
| 1820 | # Unfortunately, compiling a source file always results in a file |
| 1821 | # called source.dtb (see fdt_util.EnsureCompiled()). The test |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1822 | # source file (e.g. test/075_fdt_update_all.dts) thus does not enter |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1823 | # binman as a file called u-boot.dtb. To fix this, copy the file |
| 1824 | # over to the expected place. |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1825 | start = 0 |
| 1826 | for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out', |
| 1827 | 'tpl/u-boot-tpl.dtb.out']: |
| 1828 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1829 | size = dtb._fdt_obj.totalsize() |
| 1830 | pathname = tools.GetOutputFilename(os.path.split(fname)[1]) |
| 1831 | outdata = tools.ReadFile(pathname) |
| 1832 | name = os.path.split(fname)[0] |
| 1833 | |
| 1834 | if name: |
| 1835 | orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name) |
| 1836 | else: |
| 1837 | orig_indata = dtb_data |
| 1838 | self.assertNotEqual(outdata, orig_indata, |
| 1839 | "Expected output file '%s' be updated" % pathname) |
| 1840 | self.assertEqual(outdata, data[start:start + size], |
| 1841 | "Expected output file '%s' to match output image" % |
| 1842 | pathname) |
| 1843 | start += size |
| 1844 | finally: |
| 1845 | self._ResetDtbs() |
| 1846 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1847 | def _decompress(self, data): |
Simon Glass | ccec026 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 1848 | return tools.Decompress(data, 'lz4') |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1849 | |
| 1850 | def testCompress(self): |
| 1851 | """Test compression of blobs""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1852 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1853 | data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts', |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1854 | use_real_dtb=True, update_dtb=True) |
| 1855 | dtb = fdt.Fdt(out_dtb_fname) |
| 1856 | dtb.Scan() |
| 1857 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 1858 | orig = self._decompress(data) |
| 1859 | self.assertEquals(COMPRESS_DATA, orig) |
Simon Glass | 789b3440 | 2020-10-26 17:40:15 -0600 | [diff] [blame] | 1860 | |
| 1861 | # Do a sanity check on various fields |
| 1862 | image = control.images['image'] |
| 1863 | entries = image.GetEntries() |
| 1864 | self.assertEqual(1, len(entries)) |
| 1865 | |
| 1866 | entry = entries['blob'] |
| 1867 | self.assertEqual(COMPRESS_DATA, entry.uncomp_data) |
| 1868 | self.assertEqual(len(COMPRESS_DATA), entry.uncomp_size) |
| 1869 | orig = self._decompress(entry.data) |
| 1870 | self.assertEqual(orig, entry.uncomp_data) |
| 1871 | |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 1872 | self.assertEqual(image.data, entry.data) |
| 1873 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1874 | expected = { |
| 1875 | 'blob:uncomp-size': len(COMPRESS_DATA), |
| 1876 | 'blob:size': len(data), |
| 1877 | 'size': len(data), |
| 1878 | } |
| 1879 | self.assertEqual(expected, props) |
| 1880 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1881 | def testFiles(self): |
| 1882 | """Test bringing in multiple files""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1883 | data = self._DoReadFile('084_files.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1884 | self.assertEqual(FILES_DATA, data) |
| 1885 | |
| 1886 | def testFilesCompress(self): |
| 1887 | """Test bringing in multiple files and compressing them""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1888 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1889 | data = self._DoReadFile('085_files_compress.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1890 | |
| 1891 | image = control.images['image'] |
| 1892 | entries = image.GetEntries() |
| 1893 | files = entries['files'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1894 | entries = files._entries |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1895 | |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1896 | orig = b'' |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1897 | for i in range(1, 3): |
| 1898 | key = '%d.dat' % i |
| 1899 | start = entries[key].image_pos |
| 1900 | len = entries[key].size |
| 1901 | chunk = data[start:start + len] |
| 1902 | orig += self._decompress(chunk) |
| 1903 | |
| 1904 | self.assertEqual(FILES_DATA, orig) |
| 1905 | |
| 1906 | def testFilesMissing(self): |
| 1907 | """Test missing files""" |
| 1908 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1909 | data = self._DoReadFile('086_files_none.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1910 | self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched " |
| 1911 | 'no files', str(e.exception)) |
| 1912 | |
| 1913 | def testFilesNoPattern(self): |
| 1914 | """Test missing files""" |
| 1915 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1916 | data = self._DoReadFile('087_files_no_pattern.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1917 | self.assertIn("Node '/binman/files': Missing 'pattern' property", |
| 1918 | str(e.exception)) |
| 1919 | |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1920 | def testExpandSize(self): |
| 1921 | """Test an expanding entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1922 | data, _, map_data, _ = self._DoReadFileDtb('088_expand_size.dts', |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1923 | map=True) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1924 | expect = (tools.GetBytes(ord('a'), 8) + U_BOOT_DATA + |
| 1925 | MRC_DATA + tools.GetBytes(ord('b'), 1) + U_BOOT_DATA + |
| 1926 | tools.GetBytes(ord('c'), 8) + U_BOOT_DATA + |
| 1927 | tools.GetBytes(ord('d'), 8)) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1928 | self.assertEqual(expect, data) |
| 1929 | self.assertEqual('''ImagePos Offset Size Name |
| 1930 | 00000000 00000000 00000028 main-section |
| 1931 | 00000000 00000000 00000008 fill |
| 1932 | 00000008 00000008 00000004 u-boot |
| 1933 | 0000000c 0000000c 00000004 section |
| 1934 | 0000000c 00000000 00000003 intel-mrc |
| 1935 | 00000010 00000010 00000004 u-boot2 |
| 1936 | 00000014 00000014 0000000c section2 |
| 1937 | 00000014 00000000 00000008 fill |
| 1938 | 0000001c 00000008 00000004 u-boot |
| 1939 | 00000020 00000020 00000008 fill2 |
| 1940 | ''', map_data) |
| 1941 | |
| 1942 | def testExpandSizeBad(self): |
| 1943 | """Test an expanding entry which fails to provide contents""" |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1944 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1945 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1946 | self._DoReadFileDtb('089_expand_size_bad.dts', map=True) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1947 | self.assertIn("Node '/binman/_testing': Cannot obtain contents when " |
| 1948 | 'expanding entry', str(e.exception)) |
| 1949 | |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1950 | def testHash(self): |
| 1951 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1952 | _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1953 | use_real_dtb=True, update_dtb=True) |
| 1954 | dtb = fdt.Fdt(out_dtb_fname) |
| 1955 | dtb.Scan() |
| 1956 | hash_node = dtb.GetNode('/binman/u-boot/hash').props['value'] |
| 1957 | m = hashlib.sha256() |
| 1958 | m.update(U_BOOT_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1959 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1960 | |
| 1961 | def testHashNoAlgo(self): |
| 1962 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1963 | self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1964 | self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for " |
| 1965 | 'hash node', str(e.exception)) |
| 1966 | |
| 1967 | def testHashBadAlgo(self): |
| 1968 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1969 | self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1970 | self.assertIn("Node '/binman/u-boot': Unknown hash algorithm", |
| 1971 | str(e.exception)) |
| 1972 | |
| 1973 | def testHashSection(self): |
| 1974 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1975 | _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1976 | use_real_dtb=True, update_dtb=True) |
| 1977 | dtb = fdt.Fdt(out_dtb_fname) |
| 1978 | dtb.Scan() |
| 1979 | hash_node = dtb.GetNode('/binman/section/hash').props['value'] |
| 1980 | m = hashlib.sha256() |
| 1981 | m.update(U_BOOT_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1982 | m.update(tools.GetBytes(ord('a'), 16)) |
| 1983 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1984 | |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1985 | def testPackUBootTplMicrocode(self): |
| 1986 | """Test that x86 microcode can be handled correctly in TPL |
| 1987 | |
| 1988 | We expect to see the following in the image, in order: |
| 1989 | u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct |
| 1990 | place |
| 1991 | u-boot-tpl.dtb with the microcode removed |
| 1992 | the microcode |
| 1993 | """ |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1994 | self._SetupTplElf('u_boot_ucode_ptr') |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1995 | first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts', |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1996 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1997 | self.assertEqual(b'tplnodtb with microc' + pos_and_size + |
| 1998 | b'ter somewhere in here', first) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1999 | |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2000 | def testFmapX86(self): |
| 2001 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2002 | data = self._DoReadFile('094_fmap_x86.dts') |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2003 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2004 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('a'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2005 | self.assertEqual(expected, data[:32]) |
| 2006 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
| 2007 | |
| 2008 | self.assertEqual(0x100, fhdr.image_size) |
| 2009 | |
| 2010 | self.assertEqual(0, fentries[0].offset) |
| 2011 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2012 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2013 | |
| 2014 | self.assertEqual(4, fentries[1].offset) |
| 2015 | self.assertEqual(3, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2016 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2017 | |
| 2018 | self.assertEqual(32, fentries[2].offset) |
| 2019 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 2020 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2021 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2022 | |
| 2023 | def testFmapX86Section(self): |
| 2024 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2025 | data = self._DoReadFile('095_fmap_x86_section.dts') |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2026 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('b'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2027 | self.assertEqual(expected, data[:32]) |
| 2028 | fhdr, fentries = fmap_util.DecodeFmap(data[36:]) |
| 2029 | |
| 2030 | self.assertEqual(0x100, fhdr.image_size) |
| 2031 | |
| 2032 | self.assertEqual(0, fentries[0].offset) |
| 2033 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2034 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2035 | |
| 2036 | self.assertEqual(4, fentries[1].offset) |
| 2037 | self.assertEqual(3, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2038 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2039 | |
| 2040 | self.assertEqual(36, fentries[2].offset) |
| 2041 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 2042 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2043 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2044 | |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2045 | def testElf(self): |
| 2046 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 2047 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2048 | self._SetupTplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 2049 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2050 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2051 | data = self._DoReadFile('096_elf.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2052 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 2053 | def testElfStrip(self): |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2054 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 2055 | self._SetupSplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 2056 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2057 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2058 | data = self._DoReadFile('097_elf_strip.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2059 | |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2060 | def testPackOverlapMap(self): |
| 2061 | """Test that overlapping regions are detected""" |
| 2062 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2063 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2064 | self._DoTestFile('014_pack_overlap.dts', map=True) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2065 | map_fname = tools.GetOutputFilename('image.map') |
| 2066 | self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname, |
| 2067 | stdout.getvalue()) |
| 2068 | |
| 2069 | # We should not get an inmage, but there should be a map file |
| 2070 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin'))) |
| 2071 | self.assertTrue(os.path.exists(map_fname)) |
Simon Glass | b377475 | 2019-05-17 22:00:51 -0600 | [diff] [blame] | 2072 | map_data = tools.ReadFile(map_fname, binary=False) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2073 | self.assertEqual('''ImagePos Offset Size Name |
Simon Glass | d99850b | 2020-10-26 17:40:24 -0600 | [diff] [blame] | 2074 | <none> 00000000 00000008 main-section |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2075 | <none> 00000000 00000004 u-boot |
| 2076 | <none> 00000003 00000004 u-boot-align |
| 2077 | ''', map_data) |
| 2078 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 2079 | def testPackRefCode(self): |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 2080 | """Test that an image with an Intel Reference code binary works""" |
| 2081 | data = self._DoReadFile('100_intel_refcode.dts') |
| 2082 | self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)]) |
| 2083 | |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 2084 | def testSectionOffset(self): |
| 2085 | """Tests use of a section with an offset""" |
| 2086 | data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts', |
| 2087 | map=True) |
| 2088 | self.assertEqual('''ImagePos Offset Size Name |
| 2089 | 00000000 00000000 00000038 main-section |
| 2090 | 00000004 00000004 00000010 section@0 |
| 2091 | 00000004 00000000 00000004 u-boot |
| 2092 | 00000018 00000018 00000010 section@1 |
| 2093 | 00000018 00000000 00000004 u-boot |
| 2094 | 0000002c 0000002c 00000004 section@2 |
| 2095 | 0000002c 00000000 00000004 u-boot |
| 2096 | ''', map_data) |
| 2097 | self.assertEqual(data, |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 2098 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 2099 | tools.GetBytes(0x21, 12) + |
| 2100 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 2101 | tools.GetBytes(0x61, 12) + |
| 2102 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 2103 | tools.GetBytes(0x26, 8)) |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 2104 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 2105 | def testCbfsRaw(self): |
| 2106 | """Test base handling of a Coreboot Filesystem (CBFS) |
| 2107 | |
| 2108 | The exact contents of the CBFS is verified by similar tests in |
| 2109 | cbfs_util_test.py. The tests here merely check that the files added to |
| 2110 | the CBFS can be found in the final image. |
| 2111 | """ |
| 2112 | data = self._DoReadFile('102_cbfs_raw.dts') |
| 2113 | size = 0xb0 |
| 2114 | |
| 2115 | cbfs = cbfs_util.CbfsReader(data) |
| 2116 | self.assertEqual(size, cbfs.rom_size) |
| 2117 | |
| 2118 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2119 | cfile = cbfs.files['u-boot-dtb'] |
| 2120 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 2121 | |
| 2122 | def testCbfsArch(self): |
| 2123 | """Test on non-x86 architecture""" |
| 2124 | data = self._DoReadFile('103_cbfs_raw_ppc.dts') |
| 2125 | size = 0x100 |
| 2126 | |
| 2127 | cbfs = cbfs_util.CbfsReader(data) |
| 2128 | self.assertEqual(size, cbfs.rom_size) |
| 2129 | |
| 2130 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2131 | cfile = cbfs.files['u-boot-dtb'] |
| 2132 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 2133 | |
| 2134 | def testCbfsStage(self): |
| 2135 | """Tests handling of a Coreboot Filesystem (CBFS)""" |
| 2136 | if not elf.ELF_TOOLS: |
| 2137 | self.skipTest('Python elftools not available') |
| 2138 | elf_fname = os.path.join(self._indir, 'cbfs-stage.elf') |
| 2139 | elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA) |
| 2140 | size = 0xb0 |
| 2141 | |
| 2142 | data = self._DoReadFile('104_cbfs_stage.dts') |
| 2143 | cbfs = cbfs_util.CbfsReader(data) |
| 2144 | self.assertEqual(size, cbfs.rom_size) |
| 2145 | |
| 2146 | self.assertIn('u-boot', cbfs.files) |
| 2147 | cfile = cbfs.files['u-boot'] |
| 2148 | self.assertEqual(U_BOOT_DATA + U_BOOT_DTB_DATA, cfile.data) |
| 2149 | |
| 2150 | def testCbfsRawCompress(self): |
| 2151 | """Test handling of compressing raw files""" |
| 2152 | self._CheckLz4() |
| 2153 | data = self._DoReadFile('105_cbfs_raw_compress.dts') |
| 2154 | size = 0x140 |
| 2155 | |
| 2156 | cbfs = cbfs_util.CbfsReader(data) |
| 2157 | self.assertIn('u-boot', cbfs.files) |
| 2158 | cfile = cbfs.files['u-boot'] |
| 2159 | self.assertEqual(COMPRESS_DATA, cfile.data) |
| 2160 | |
| 2161 | def testCbfsBadArch(self): |
| 2162 | """Test handling of a bad architecture""" |
| 2163 | with self.assertRaises(ValueError) as e: |
| 2164 | self._DoReadFile('106_cbfs_bad_arch.dts') |
| 2165 | self.assertIn("Invalid architecture 'bad-arch'", str(e.exception)) |
| 2166 | |
| 2167 | def testCbfsNoSize(self): |
| 2168 | """Test handling of a missing size property""" |
| 2169 | with self.assertRaises(ValueError) as e: |
| 2170 | self._DoReadFile('107_cbfs_no_size.dts') |
| 2171 | self.assertIn('entry must have a size property', str(e.exception)) |
| 2172 | |
| 2173 | def testCbfsNoCOntents(self): |
| 2174 | """Test handling of a CBFS entry which does not provide contentsy""" |
| 2175 | with self.assertRaises(ValueError) as e: |
| 2176 | self._DoReadFile('108_cbfs_no_contents.dts') |
| 2177 | self.assertIn('Could not complete processing of contents', |
| 2178 | str(e.exception)) |
| 2179 | |
| 2180 | def testCbfsBadCompress(self): |
| 2181 | """Test handling of a bad architecture""" |
| 2182 | with self.assertRaises(ValueError) as e: |
| 2183 | self._DoReadFile('109_cbfs_bad_compress.dts') |
| 2184 | self.assertIn("Invalid compression in 'u-boot': 'invalid-algo'", |
| 2185 | str(e.exception)) |
| 2186 | |
| 2187 | def testCbfsNamedEntries(self): |
| 2188 | """Test handling of named entries""" |
| 2189 | data = self._DoReadFile('110_cbfs_name.dts') |
| 2190 | |
| 2191 | cbfs = cbfs_util.CbfsReader(data) |
| 2192 | self.assertIn('FRED', cbfs.files) |
| 2193 | cfile1 = cbfs.files['FRED'] |
| 2194 | self.assertEqual(U_BOOT_DATA, cfile1.data) |
| 2195 | |
| 2196 | self.assertIn('hello', cbfs.files) |
| 2197 | cfile2 = cbfs.files['hello'] |
| 2198 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2199 | |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2200 | def _SetupIfwi(self, fname): |
| 2201 | """Set up to run an IFWI test |
| 2202 | |
| 2203 | Args: |
| 2204 | fname: Filename of input file to provide (fitimage.bin or ifwi.bin) |
| 2205 | """ |
| 2206 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2207 | self._SetupTplElf() |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2208 | |
| 2209 | # Intel Integrated Firmware Image (IFWI) file |
| 2210 | with gzip.open(self.TestFile('%s.gz' % fname), 'rb') as fd: |
| 2211 | data = fd.read() |
| 2212 | TestFunctional._MakeInputFile(fname,data) |
| 2213 | |
| 2214 | def _CheckIfwi(self, data): |
| 2215 | """Check that an image with an IFWI contains the correct output |
| 2216 | |
| 2217 | Args: |
| 2218 | data: Conents of output file |
| 2219 | """ |
| 2220 | expected_desc = tools.ReadFile(self.TestFile('descriptor.bin')) |
| 2221 | if data[:0x1000] != expected_desc: |
| 2222 | self.fail('Expected descriptor binary at start of image') |
| 2223 | |
| 2224 | # We expect to find the TPL wil in subpart IBBP entry IBBL |
| 2225 | image_fname = tools.GetOutputFilename('image.bin') |
| 2226 | tpl_fname = tools.GetOutputFilename('tpl.out') |
| 2227 | tools.RunIfwiTool(image_fname, tools.CMD_EXTRACT, fname=tpl_fname, |
| 2228 | subpart='IBBP', entry_name='IBBL') |
| 2229 | |
| 2230 | tpl_data = tools.ReadFile(tpl_fname) |
Simon Glass | f55bd69 | 2019-08-24 07:22:51 -0600 | [diff] [blame] | 2231 | self.assertEqual(U_BOOT_TPL_DATA, tpl_data[:len(U_BOOT_TPL_DATA)]) |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2232 | |
| 2233 | def testPackX86RomIfwi(self): |
| 2234 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 2235 | self._SetupIfwi('fitimage.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2236 | data = self._DoReadFile('111_x86_rom_ifwi.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2237 | self._CheckIfwi(data) |
| 2238 | |
| 2239 | def testPackX86RomIfwiNoDesc(self): |
| 2240 | """Test that an x86 ROM with IFWI can be created from an ifwi.bin file""" |
| 2241 | self._SetupIfwi('ifwi.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2242 | data = self._DoReadFile('112_x86_rom_ifwi_nodesc.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2243 | self._CheckIfwi(data) |
| 2244 | |
| 2245 | def testPackX86RomIfwiNoData(self): |
| 2246 | """Test that an x86 ROM with IFWI handles missing data""" |
| 2247 | self._SetupIfwi('ifwi.bin') |
| 2248 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2249 | data = self._DoReadFile('113_x86_rom_ifwi_nodata.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2250 | self.assertIn('Could not complete processing of contents', |
| 2251 | str(e.exception)) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 2252 | |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2253 | def testCbfsOffset(self): |
| 2254 | """Test a CBFS with files at particular offsets |
| 2255 | |
| 2256 | Like all CFBS tests, this is just checking the logic that calls |
| 2257 | cbfs_util. See cbfs_util_test for fully tests (e.g. test_cbfs_offset()). |
| 2258 | """ |
| 2259 | data = self._DoReadFile('114_cbfs_offset.dts') |
| 2260 | size = 0x200 |
| 2261 | |
| 2262 | cbfs = cbfs_util.CbfsReader(data) |
| 2263 | self.assertEqual(size, cbfs.rom_size) |
| 2264 | |
| 2265 | self.assertIn('u-boot', cbfs.files) |
| 2266 | cfile = cbfs.files['u-boot'] |
| 2267 | self.assertEqual(U_BOOT_DATA, cfile.data) |
| 2268 | self.assertEqual(0x40, cfile.cbfs_offset) |
| 2269 | |
| 2270 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2271 | cfile2 = cbfs.files['u-boot-dtb'] |
| 2272 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2273 | self.assertEqual(0x140, cfile2.cbfs_offset) |
| 2274 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2275 | def testFdtmap(self): |
| 2276 | """Test an FDT map can be inserted in the image""" |
| 2277 | data = self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2278 | fdtmap_data = data[len(U_BOOT_DATA):] |
| 2279 | magic = fdtmap_data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2280 | self.assertEqual(b'_FDTMAP_', magic) |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2281 | self.assertEqual(tools.GetBytes(0, 8), fdtmap_data[8:16]) |
| 2282 | |
| 2283 | fdt_data = fdtmap_data[16:] |
| 2284 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2285 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2286 | props = self._GetPropTree(dtb, BASE_DTB_PROPS, prefix='/') |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2287 | self.assertEqual({ |
| 2288 | 'image-pos': 0, |
| 2289 | 'offset': 0, |
| 2290 | 'u-boot:offset': 0, |
| 2291 | 'u-boot:size': len(U_BOOT_DATA), |
| 2292 | 'u-boot:image-pos': 0, |
| 2293 | 'fdtmap:image-pos': 4, |
| 2294 | 'fdtmap:offset': 4, |
| 2295 | 'fdtmap:size': len(fdtmap_data), |
| 2296 | 'size': len(data), |
| 2297 | }, props) |
| 2298 | |
| 2299 | def testFdtmapNoMatch(self): |
| 2300 | """Check handling of an FDT map when the section cannot be found""" |
| 2301 | self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2302 | |
| 2303 | # Mangle the section name, which should cause a mismatch between the |
| 2304 | # correct FDT path and the one expected by the section |
| 2305 | image = control.images['image'] |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2306 | image._node.path += '-suffix' |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2307 | entries = image.GetEntries() |
| 2308 | fdtmap = entries['fdtmap'] |
| 2309 | with self.assertRaises(ValueError) as e: |
| 2310 | fdtmap._GetFdtmap() |
| 2311 | self.assertIn("Cannot locate node for path '/binman-suffix'", |
| 2312 | str(e.exception)) |
| 2313 | |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2314 | def testFdtmapHeader(self): |
| 2315 | """Test an FDT map and image header can be inserted in the image""" |
| 2316 | data = self.data = self._DoReadFileRealDtb('116_fdtmap_hdr.dts') |
| 2317 | fdtmap_pos = len(U_BOOT_DATA) |
| 2318 | fdtmap_data = data[fdtmap_pos:] |
| 2319 | fdt_data = fdtmap_data[16:] |
| 2320 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2321 | fdt_size = dtb.GetFdtObj().totalsize() |
| 2322 | hdr_data = data[-8:] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2323 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2324 | offset = struct.unpack('<I', hdr_data[4:])[0] & 0xffffffff |
| 2325 | self.assertEqual(fdtmap_pos - 0x400, offset - (1 << 32)) |
| 2326 | |
| 2327 | def testFdtmapHeaderStart(self): |
| 2328 | """Test an image header can be inserted at the image start""" |
| 2329 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
| 2330 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2331 | hdr_data = data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2332 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2333 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2334 | self.assertEqual(fdtmap_pos, offset) |
| 2335 | |
| 2336 | def testFdtmapHeaderPos(self): |
| 2337 | """Test an image header can be inserted at a chosen position""" |
| 2338 | data = self.data = self._DoReadFileRealDtb('118_fdtmap_hdr_pos.dts') |
| 2339 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2340 | hdr_data = data[0x80:0x88] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2341 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2342 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2343 | self.assertEqual(fdtmap_pos, offset) |
| 2344 | |
| 2345 | def testHeaderMissingFdtmap(self): |
| 2346 | """Test an image header requires an fdtmap""" |
| 2347 | with self.assertRaises(ValueError) as e: |
| 2348 | self.data = self._DoReadFileRealDtb('119_fdtmap_hdr_missing.dts') |
| 2349 | self.assertIn("'image_header' section must have an 'fdtmap' sibling", |
| 2350 | str(e.exception)) |
| 2351 | |
| 2352 | def testHeaderNoLocation(self): |
| 2353 | """Test an image header with a no specified location is detected""" |
| 2354 | with self.assertRaises(ValueError) as e: |
| 2355 | self.data = self._DoReadFileRealDtb('120_hdr_no_location.dts') |
| 2356 | self.assertIn("Invalid location 'None', expected 'start' or 'end'", |
| 2357 | str(e.exception)) |
| 2358 | |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2359 | def testEntryExpand(self): |
| 2360 | """Test expanding an entry after it is packed""" |
| 2361 | data = self._DoReadFile('121_entry_expand.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2362 | self.assertEqual(b'aaa', data[:3]) |
| 2363 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2364 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2365 | |
| 2366 | def testEntryExpandBad(self): |
| 2367 | """Test expanding an entry after it is packed, twice""" |
| 2368 | with self.assertRaises(ValueError) as e: |
| 2369 | self._DoReadFile('122_entry_expand_twice.dts') |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 2370 | self.assertIn("Image '/binman': Entries changed size after packing", |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2371 | str(e.exception)) |
| 2372 | |
| 2373 | def testEntryExpandSection(self): |
| 2374 | """Test expanding an entry within a section after it is packed""" |
| 2375 | data = self._DoReadFile('123_entry_expand_section.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2376 | self.assertEqual(b'aaa', data[:3]) |
| 2377 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2378 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2379 | |
Simon Glass | 90d2968 | 2019-07-08 14:25:38 -0600 | [diff] [blame] | 2380 | def testCompressDtb(self): |
| 2381 | """Test that compress of device-tree files is supported""" |
| 2382 | self._CheckLz4() |
| 2383 | data = self.data = self._DoReadFileRealDtb('124_compress_dtb.dts') |
| 2384 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 2385 | comp_data = data[len(U_BOOT_DATA):] |
| 2386 | orig = self._decompress(comp_data) |
| 2387 | dtb = fdt.Fdt.FromData(orig) |
| 2388 | dtb.Scan() |
| 2389 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 2390 | expected = { |
| 2391 | 'u-boot:size': len(U_BOOT_DATA), |
| 2392 | 'u-boot-dtb:uncomp-size': len(orig), |
| 2393 | 'u-boot-dtb:size': len(comp_data), |
| 2394 | 'size': len(data), |
| 2395 | } |
| 2396 | self.assertEqual(expected, props) |
| 2397 | |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2398 | def testCbfsUpdateFdt(self): |
| 2399 | """Test that we can update the device tree with CBFS offset/size info""" |
| 2400 | self._CheckLz4() |
| 2401 | data, _, _, out_dtb_fname = self._DoReadFileDtb('125_cbfs_update.dts', |
| 2402 | update_dtb=True) |
| 2403 | dtb = fdt.Fdt(out_dtb_fname) |
| 2404 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2405 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + ['uncomp-size']) |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2406 | del props['cbfs/u-boot:size'] |
| 2407 | self.assertEqual({ |
| 2408 | 'offset': 0, |
| 2409 | 'size': len(data), |
| 2410 | 'image-pos': 0, |
| 2411 | 'cbfs:offset': 0, |
| 2412 | 'cbfs:size': len(data), |
| 2413 | 'cbfs:image-pos': 0, |
| 2414 | 'cbfs/u-boot:offset': 0x38, |
| 2415 | 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA), |
| 2416 | 'cbfs/u-boot:image-pos': 0x38, |
| 2417 | 'cbfs/u-boot-dtb:offset': 0xb8, |
| 2418 | 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA), |
| 2419 | 'cbfs/u-boot-dtb:image-pos': 0xb8, |
| 2420 | }, props) |
| 2421 | |
Simon Glass | 3c9b4f2 | 2019-07-08 14:25:42 -0600 | [diff] [blame] | 2422 | def testCbfsBadType(self): |
| 2423 | """Test an image header with a no specified location is detected""" |
| 2424 | with self.assertRaises(ValueError) as e: |
| 2425 | self._DoReadFile('126_cbfs_bad_type.dts') |
| 2426 | self.assertIn("Unknown cbfs-type 'badtype'", str(e.exception)) |
| 2427 | |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2428 | def testList(self): |
| 2429 | """Test listing the files in an image""" |
| 2430 | self._CheckLz4() |
| 2431 | data = self._DoReadFile('127_list.dts') |
| 2432 | image = control.images['image'] |
| 2433 | entries = image.BuildEntryList() |
| 2434 | self.assertEqual(7, len(entries)) |
| 2435 | |
| 2436 | ent = entries[0] |
| 2437 | self.assertEqual(0, ent.indent) |
| 2438 | self.assertEqual('main-section', ent.name) |
| 2439 | self.assertEqual('section', ent.etype) |
| 2440 | self.assertEqual(len(data), ent.size) |
| 2441 | self.assertEqual(0, ent.image_pos) |
| 2442 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2443 | self.assertEqual(0, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2444 | |
| 2445 | ent = entries[1] |
| 2446 | self.assertEqual(1, ent.indent) |
| 2447 | self.assertEqual('u-boot', ent.name) |
| 2448 | self.assertEqual('u-boot', ent.etype) |
| 2449 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2450 | self.assertEqual(0, ent.image_pos) |
| 2451 | self.assertEqual(None, ent.uncomp_size) |
| 2452 | self.assertEqual(0, ent.offset) |
| 2453 | |
| 2454 | ent = entries[2] |
| 2455 | self.assertEqual(1, ent.indent) |
| 2456 | self.assertEqual('section', ent.name) |
| 2457 | self.assertEqual('section', ent.etype) |
| 2458 | section_size = ent.size |
| 2459 | self.assertEqual(0x100, ent.image_pos) |
| 2460 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2461 | self.assertEqual(0x100, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2462 | |
| 2463 | ent = entries[3] |
| 2464 | self.assertEqual(2, ent.indent) |
| 2465 | self.assertEqual('cbfs', ent.name) |
| 2466 | self.assertEqual('cbfs', ent.etype) |
| 2467 | self.assertEqual(0x400, ent.size) |
| 2468 | self.assertEqual(0x100, ent.image_pos) |
| 2469 | self.assertEqual(None, ent.uncomp_size) |
| 2470 | self.assertEqual(0, ent.offset) |
| 2471 | |
| 2472 | ent = entries[4] |
| 2473 | self.assertEqual(3, ent.indent) |
| 2474 | self.assertEqual('u-boot', ent.name) |
| 2475 | self.assertEqual('u-boot', ent.etype) |
| 2476 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2477 | self.assertEqual(0x138, ent.image_pos) |
| 2478 | self.assertEqual(None, ent.uncomp_size) |
| 2479 | self.assertEqual(0x38, ent.offset) |
| 2480 | |
| 2481 | ent = entries[5] |
| 2482 | self.assertEqual(3, ent.indent) |
| 2483 | self.assertEqual('u-boot-dtb', ent.name) |
| 2484 | self.assertEqual('text', ent.etype) |
| 2485 | self.assertGreater(len(COMPRESS_DATA), ent.size) |
| 2486 | self.assertEqual(0x178, ent.image_pos) |
| 2487 | self.assertEqual(len(COMPRESS_DATA), ent.uncomp_size) |
| 2488 | self.assertEqual(0x78, ent.offset) |
| 2489 | |
| 2490 | ent = entries[6] |
| 2491 | self.assertEqual(2, ent.indent) |
| 2492 | self.assertEqual('u-boot-dtb', ent.name) |
| 2493 | self.assertEqual('u-boot-dtb', ent.etype) |
| 2494 | self.assertEqual(0x500, ent.image_pos) |
| 2495 | self.assertEqual(len(U_BOOT_DTB_DATA), ent.uncomp_size) |
| 2496 | dtb_size = ent.size |
| 2497 | # Compressing this data expands it since headers are added |
| 2498 | self.assertGreater(dtb_size, len(U_BOOT_DTB_DATA)) |
| 2499 | self.assertEqual(0x400, ent.offset) |
| 2500 | |
| 2501 | self.assertEqual(len(data), 0x100 + section_size) |
| 2502 | self.assertEqual(section_size, 0x400 + dtb_size) |
| 2503 | |
Simon Glass | 8d8bf4e | 2019-07-08 14:25:44 -0600 | [diff] [blame] | 2504 | def testFindFdtmap(self): |
| 2505 | """Test locating an FDT map in an image""" |
| 2506 | self._CheckLz4() |
| 2507 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
| 2508 | image = control.images['image'] |
| 2509 | entries = image.GetEntries() |
| 2510 | entry = entries['fdtmap'] |
| 2511 | self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data)) |
| 2512 | |
| 2513 | def testFindFdtmapMissing(self): |
| 2514 | """Test failing to locate an FDP map""" |
| 2515 | data = self._DoReadFile('005_simple.dts') |
| 2516 | self.assertEqual(None, fdtmap.LocateFdtmap(data)) |
| 2517 | |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2518 | def testFindImageHeader(self): |
| 2519 | """Test locating a image header""" |
| 2520 | self._CheckLz4() |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2521 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2522 | image = control.images['image'] |
| 2523 | entries = image.GetEntries() |
| 2524 | entry = entries['fdtmap'] |
| 2525 | # The header should point to the FDT map |
| 2526 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2527 | |
| 2528 | def testFindImageHeaderStart(self): |
| 2529 | """Test locating a image header located at the start of an image""" |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2530 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2531 | image = control.images['image'] |
| 2532 | entries = image.GetEntries() |
| 2533 | entry = entries['fdtmap'] |
| 2534 | # The header should point to the FDT map |
| 2535 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2536 | |
| 2537 | def testFindImageHeaderMissing(self): |
| 2538 | """Test failing to locate an image header""" |
| 2539 | data = self._DoReadFile('005_simple.dts') |
| 2540 | self.assertEqual(None, image_header.LocateHeaderOffset(data)) |
| 2541 | |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2542 | def testReadImage(self): |
| 2543 | """Test reading an image and accessing its FDT map""" |
| 2544 | self._CheckLz4() |
| 2545 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
| 2546 | image_fname = tools.GetOutputFilename('image.bin') |
| 2547 | orig_image = control.images['image'] |
| 2548 | image = Image.FromFile(image_fname) |
| 2549 | self.assertEqual(orig_image.GetEntries().keys(), |
| 2550 | image.GetEntries().keys()) |
| 2551 | |
| 2552 | orig_entry = orig_image.GetEntries()['fdtmap'] |
| 2553 | entry = image.GetEntries()['fdtmap'] |
| 2554 | self.assertEquals(orig_entry.offset, entry.offset) |
| 2555 | self.assertEquals(orig_entry.size, entry.size) |
| 2556 | self.assertEquals(orig_entry.image_pos, entry.image_pos) |
| 2557 | |
| 2558 | def testReadImageNoHeader(self): |
| 2559 | """Test accessing an image's FDT map without an image header""" |
| 2560 | self._CheckLz4() |
| 2561 | data = self._DoReadFileRealDtb('129_decode_image_nohdr.dts') |
| 2562 | image_fname = tools.GetOutputFilename('image.bin') |
| 2563 | image = Image.FromFile(image_fname) |
| 2564 | self.assertTrue(isinstance(image, Image)) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2565 | self.assertEqual('image', image.image_name[-5:]) |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2566 | |
| 2567 | def testReadImageFail(self): |
| 2568 | """Test failing to read an image image's FDT map""" |
| 2569 | self._DoReadFile('005_simple.dts') |
| 2570 | image_fname = tools.GetOutputFilename('image.bin') |
| 2571 | with self.assertRaises(ValueError) as e: |
| 2572 | image = Image.FromFile(image_fname) |
| 2573 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2574 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2575 | def testListCmd(self): |
| 2576 | """Test listing the files in an image using an Fdtmap""" |
| 2577 | self._CheckLz4() |
| 2578 | data = self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2579 | |
| 2580 | # lz4 compression size differs depending on the version |
| 2581 | image = control.images['image'] |
| 2582 | entries = image.GetEntries() |
| 2583 | section_size = entries['section'].size |
| 2584 | fdt_size = entries['section'].GetEntries()['u-boot-dtb'].size |
| 2585 | fdtmap_offset = entries['fdtmap'].offset |
| 2586 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2587 | try: |
| 2588 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2589 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2590 | self._DoBinman('ls', '-i', updated_fname) |
| 2591 | finally: |
| 2592 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2593 | lines = stdout.getvalue().splitlines() |
| 2594 | expected = [ |
| 2595 | 'Name Image-pos Size Entry-type Offset Uncomp-size', |
| 2596 | '----------------------------------------------------------------------', |
| 2597 | 'main-section 0 c00 section 0', |
| 2598 | ' u-boot 0 4 u-boot 0', |
| 2599 | ' section 100 %x section 100' % section_size, |
| 2600 | ' cbfs 100 400 cbfs 0', |
| 2601 | ' u-boot 138 4 u-boot 38', |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2602 | ' u-boot-dtb 180 105 u-boot-dtb 80 3c9', |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2603 | ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size, |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2604 | ' fdtmap %x 3bd fdtmap %x' % |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2605 | (fdtmap_offset, fdtmap_offset), |
| 2606 | ' image-header bf8 8 image-header bf8', |
| 2607 | ] |
| 2608 | self.assertEqual(expected, lines) |
| 2609 | |
| 2610 | def testListCmdFail(self): |
| 2611 | """Test failing to list an image""" |
| 2612 | self._DoReadFile('005_simple.dts') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2613 | try: |
| 2614 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2615 | with self.assertRaises(ValueError) as e: |
| 2616 | self._DoBinman('ls', '-i', updated_fname) |
| 2617 | finally: |
| 2618 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2619 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
| 2620 | |
| 2621 | def _RunListCmd(self, paths, expected): |
| 2622 | """List out entries and check the result |
| 2623 | |
| 2624 | Args: |
| 2625 | paths: List of paths to pass to the list command |
| 2626 | expected: Expected list of filenames to be returned, in order |
| 2627 | """ |
| 2628 | self._CheckLz4() |
| 2629 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2630 | image_fname = tools.GetOutputFilename('image.bin') |
| 2631 | image = Image.FromFile(image_fname) |
| 2632 | lines = image.GetListEntries(paths)[1] |
| 2633 | files = [line[0].strip() for line in lines[1:]] |
| 2634 | self.assertEqual(expected, files) |
| 2635 | |
| 2636 | def testListCmdSection(self): |
| 2637 | """Test listing the files in a section""" |
| 2638 | self._RunListCmd(['section'], |
| 2639 | ['section', 'cbfs', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2640 | |
| 2641 | def testListCmdFile(self): |
| 2642 | """Test listing a particular file""" |
| 2643 | self._RunListCmd(['*u-boot-dtb'], ['u-boot-dtb', 'u-boot-dtb']) |
| 2644 | |
| 2645 | def testListCmdWildcard(self): |
| 2646 | """Test listing a wildcarded file""" |
| 2647 | self._RunListCmd(['*boot*'], |
| 2648 | ['u-boot', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2649 | |
| 2650 | def testListCmdWildcardMulti(self): |
| 2651 | """Test listing a wildcarded file""" |
| 2652 | self._RunListCmd(['*cb*', '*head*'], |
| 2653 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 2654 | |
| 2655 | def testListCmdEmpty(self): |
| 2656 | """Test listing a wildcarded file""" |
| 2657 | self._RunListCmd(['nothing'], []) |
| 2658 | |
| 2659 | def testListCmdPath(self): |
| 2660 | """Test listing the files in a sub-entry of a section""" |
| 2661 | self._RunListCmd(['section/cbfs'], ['cbfs', 'u-boot', 'u-boot-dtb']) |
| 2662 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2663 | def _RunExtractCmd(self, entry_name, decomp=True): |
| 2664 | """Extract an entry from an image |
| 2665 | |
| 2666 | Args: |
| 2667 | entry_name: Entry name to extract |
| 2668 | decomp: True to decompress the data if compressed, False to leave |
| 2669 | it in its raw uncompressed format |
| 2670 | |
| 2671 | Returns: |
| 2672 | data from entry |
| 2673 | """ |
| 2674 | self._CheckLz4() |
| 2675 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2676 | image_fname = tools.GetOutputFilename('image.bin') |
| 2677 | return control.ReadEntry(image_fname, entry_name, decomp) |
| 2678 | |
| 2679 | def testExtractSimple(self): |
| 2680 | """Test extracting a single file""" |
| 2681 | data = self._RunExtractCmd('u-boot') |
| 2682 | self.assertEqual(U_BOOT_DATA, data) |
| 2683 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2684 | def testExtractSection(self): |
| 2685 | """Test extracting the files in a section""" |
| 2686 | data = self._RunExtractCmd('section') |
| 2687 | cbfs_data = data[:0x400] |
| 2688 | cbfs = cbfs_util.CbfsReader(cbfs_data) |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2689 | self.assertEqual(['u-boot', 'u-boot-dtb', ''], list(cbfs.files.keys())) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2690 | dtb_data = data[0x400:] |
| 2691 | dtb = self._decompress(dtb_data) |
| 2692 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2693 | |
| 2694 | def testExtractCompressed(self): |
| 2695 | """Test extracting compressed data""" |
| 2696 | data = self._RunExtractCmd('section/u-boot-dtb') |
| 2697 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2698 | |
| 2699 | def testExtractRaw(self): |
| 2700 | """Test extracting compressed data without decompressing it""" |
| 2701 | data = self._RunExtractCmd('section/u-boot-dtb', decomp=False) |
| 2702 | dtb = self._decompress(data) |
| 2703 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2704 | |
| 2705 | def testExtractCbfs(self): |
| 2706 | """Test extracting CBFS data""" |
| 2707 | data = self._RunExtractCmd('section/cbfs/u-boot') |
| 2708 | self.assertEqual(U_BOOT_DATA, data) |
| 2709 | |
| 2710 | def testExtractCbfsCompressed(self): |
| 2711 | """Test extracting CBFS compressed data""" |
| 2712 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb') |
| 2713 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2714 | |
| 2715 | def testExtractCbfsRaw(self): |
| 2716 | """Test extracting CBFS compressed data without decompressing it""" |
| 2717 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb', decomp=False) |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 2718 | dtb = tools.Decompress(data, 'lzma', with_header=False) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2719 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2720 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2721 | def testExtractBadEntry(self): |
| 2722 | """Test extracting a bad section path""" |
| 2723 | with self.assertRaises(ValueError) as e: |
| 2724 | self._RunExtractCmd('section/does-not-exist') |
| 2725 | self.assertIn("Entry 'does-not-exist' not found in '/section'", |
| 2726 | str(e.exception)) |
| 2727 | |
| 2728 | def testExtractMissingFile(self): |
| 2729 | """Test extracting file that does not exist""" |
| 2730 | with self.assertRaises(IOError) as e: |
| 2731 | control.ReadEntry('missing-file', 'name') |
| 2732 | |
| 2733 | def testExtractBadFile(self): |
| 2734 | """Test extracting an invalid file""" |
| 2735 | fname = os.path.join(self._indir, 'badfile') |
| 2736 | tools.WriteFile(fname, b'') |
| 2737 | with self.assertRaises(ValueError) as e: |
| 2738 | control.ReadEntry(fname, 'name') |
| 2739 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2740 | def testExtractCmd(self): |
| 2741 | """Test extracting a file fron an image on the command line""" |
| 2742 | self._CheckLz4() |
| 2743 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2744 | fname = os.path.join(self._indir, 'output.extact') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2745 | try: |
| 2746 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2747 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2748 | self._DoBinman('extract', '-i', updated_fname, 'u-boot', |
| 2749 | '-f', fname) |
| 2750 | finally: |
| 2751 | shutil.rmtree(tmpdir) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2752 | data = tools.ReadFile(fname) |
| 2753 | self.assertEqual(U_BOOT_DATA, data) |
| 2754 | |
| 2755 | def testExtractOneEntry(self): |
| 2756 | """Test extracting a single entry fron an image """ |
| 2757 | self._CheckLz4() |
| 2758 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2759 | image_fname = tools.GetOutputFilename('image.bin') |
| 2760 | fname = os.path.join(self._indir, 'output.extact') |
| 2761 | control.ExtractEntries(image_fname, fname, None, ['u-boot']) |
| 2762 | data = tools.ReadFile(fname) |
| 2763 | self.assertEqual(U_BOOT_DATA, data) |
| 2764 | |
| 2765 | def _CheckExtractOutput(self, decomp): |
| 2766 | """Helper to test file output with and without decompression |
| 2767 | |
| 2768 | Args: |
| 2769 | decomp: True to decompress entry data, False to output it raw |
| 2770 | """ |
| 2771 | def _CheckPresent(entry_path, expect_data, expect_size=None): |
| 2772 | """Check and remove expected file |
| 2773 | |
| 2774 | This checks the data/size of a file and removes the file both from |
| 2775 | the outfiles set and from the output directory. Once all files are |
| 2776 | processed, both the set and directory should be empty. |
| 2777 | |
| 2778 | Args: |
| 2779 | entry_path: Entry path |
| 2780 | expect_data: Data to expect in file, or None to skip check |
| 2781 | expect_size: Size of data to expect in file, or None to skip |
| 2782 | """ |
| 2783 | path = os.path.join(outdir, entry_path) |
| 2784 | data = tools.ReadFile(path) |
| 2785 | os.remove(path) |
| 2786 | if expect_data: |
| 2787 | self.assertEqual(expect_data, data) |
| 2788 | elif expect_size: |
| 2789 | self.assertEqual(expect_size, len(data)) |
| 2790 | outfiles.remove(path) |
| 2791 | |
| 2792 | def _CheckDirPresent(name): |
| 2793 | """Remove expected directory |
| 2794 | |
| 2795 | This gives an error if the directory does not exist as expected |
| 2796 | |
| 2797 | Args: |
| 2798 | name: Name of directory to remove |
| 2799 | """ |
| 2800 | path = os.path.join(outdir, name) |
| 2801 | os.rmdir(path) |
| 2802 | |
| 2803 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2804 | image_fname = tools.GetOutputFilename('image.bin') |
| 2805 | outdir = os.path.join(self._indir, 'extract') |
| 2806 | einfos = control.ExtractEntries(image_fname, None, outdir, [], decomp) |
| 2807 | |
| 2808 | # Create a set of all file that were output (should be 9) |
| 2809 | outfiles = set() |
| 2810 | for root, dirs, files in os.walk(outdir): |
| 2811 | outfiles |= set([os.path.join(root, fname) for fname in files]) |
| 2812 | self.assertEqual(9, len(outfiles)) |
| 2813 | self.assertEqual(9, len(einfos)) |
| 2814 | |
| 2815 | image = control.images['image'] |
| 2816 | entries = image.GetEntries() |
| 2817 | |
| 2818 | # Check the 9 files in various ways |
| 2819 | section = entries['section'] |
| 2820 | section_entries = section.GetEntries() |
| 2821 | cbfs_entries = section_entries['cbfs'].GetEntries() |
| 2822 | _CheckPresent('u-boot', U_BOOT_DATA) |
| 2823 | _CheckPresent('section/cbfs/u-boot', U_BOOT_DATA) |
| 2824 | dtb_len = EXTRACT_DTB_SIZE |
| 2825 | if not decomp: |
| 2826 | dtb_len = cbfs_entries['u-boot-dtb'].size |
| 2827 | _CheckPresent('section/cbfs/u-boot-dtb', None, dtb_len) |
| 2828 | if not decomp: |
| 2829 | dtb_len = section_entries['u-boot-dtb'].size |
| 2830 | _CheckPresent('section/u-boot-dtb', None, dtb_len) |
| 2831 | |
| 2832 | fdtmap = entries['fdtmap'] |
| 2833 | _CheckPresent('fdtmap', fdtmap.data) |
| 2834 | hdr = entries['image-header'] |
| 2835 | _CheckPresent('image-header', hdr.data) |
| 2836 | |
| 2837 | _CheckPresent('section/root', section.data) |
| 2838 | cbfs = section_entries['cbfs'] |
| 2839 | _CheckPresent('section/cbfs/root', cbfs.data) |
| 2840 | data = tools.ReadFile(image_fname) |
| 2841 | _CheckPresent('root', data) |
| 2842 | |
| 2843 | # There should be no files left. Remove all the directories to check. |
| 2844 | # If there are any files/dirs remaining, one of these checks will fail. |
| 2845 | self.assertEqual(0, len(outfiles)) |
| 2846 | _CheckDirPresent('section/cbfs') |
| 2847 | _CheckDirPresent('section') |
| 2848 | _CheckDirPresent('') |
| 2849 | self.assertFalse(os.path.exists(outdir)) |
| 2850 | |
| 2851 | def testExtractAllEntries(self): |
| 2852 | """Test extracting all entries""" |
| 2853 | self._CheckLz4() |
| 2854 | self._CheckExtractOutput(decomp=True) |
| 2855 | |
| 2856 | def testExtractAllEntriesRaw(self): |
| 2857 | """Test extracting all entries without decompressing them""" |
| 2858 | self._CheckLz4() |
| 2859 | self._CheckExtractOutput(decomp=False) |
| 2860 | |
| 2861 | def testExtractSelectedEntries(self): |
| 2862 | """Test extracting some entries""" |
| 2863 | self._CheckLz4() |
| 2864 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2865 | image_fname = tools.GetOutputFilename('image.bin') |
| 2866 | outdir = os.path.join(self._indir, 'extract') |
| 2867 | einfos = control.ExtractEntries(image_fname, None, outdir, |
| 2868 | ['*cb*', '*head*']) |
| 2869 | |
| 2870 | # File output is tested by testExtractAllEntries(), so just check that |
| 2871 | # the expected entries are selected |
| 2872 | names = [einfo.name for einfo in einfos] |
| 2873 | self.assertEqual(names, |
| 2874 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 2875 | |
| 2876 | def testExtractNoEntryPaths(self): |
| 2877 | """Test extracting some entries""" |
| 2878 | self._CheckLz4() |
| 2879 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2880 | image_fname = tools.GetOutputFilename('image.bin') |
| 2881 | with self.assertRaises(ValueError) as e: |
| 2882 | control.ExtractEntries(image_fname, 'fname', None, []) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 2883 | self.assertIn('Must specify an entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2884 | str(e.exception)) |
| 2885 | |
| 2886 | def testExtractTooManyEntryPaths(self): |
| 2887 | """Test extracting some entries""" |
| 2888 | self._CheckLz4() |
| 2889 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2890 | image_fname = tools.GetOutputFilename('image.bin') |
| 2891 | with self.assertRaises(ValueError) as e: |
| 2892 | control.ExtractEntries(image_fname, 'fname', None, ['a', 'b']) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 2893 | self.assertIn('Must specify exactly one entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2894 | str(e.exception)) |
| 2895 | |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 2896 | def testPackAlignSection(self): |
| 2897 | """Test that sections can have alignment""" |
| 2898 | self._DoReadFile('131_pack_align_section.dts') |
| 2899 | |
| 2900 | self.assertIn('image', control.images) |
| 2901 | image = control.images['image'] |
| 2902 | entries = image.GetEntries() |
| 2903 | self.assertEqual(3, len(entries)) |
| 2904 | |
| 2905 | # First u-boot |
| 2906 | self.assertIn('u-boot', entries) |
| 2907 | entry = entries['u-boot'] |
| 2908 | self.assertEqual(0, entry.offset) |
| 2909 | self.assertEqual(0, entry.image_pos) |
| 2910 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2911 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2912 | |
| 2913 | # Section0 |
| 2914 | self.assertIn('section0', entries) |
| 2915 | section0 = entries['section0'] |
| 2916 | self.assertEqual(0x10, section0.offset) |
| 2917 | self.assertEqual(0x10, section0.image_pos) |
| 2918 | self.assertEqual(len(U_BOOT_DATA), section0.size) |
| 2919 | |
| 2920 | # Second u-boot |
| 2921 | section_entries = section0.GetEntries() |
| 2922 | self.assertIn('u-boot', section_entries) |
| 2923 | entry = section_entries['u-boot'] |
| 2924 | self.assertEqual(0, entry.offset) |
| 2925 | self.assertEqual(0x10, entry.image_pos) |
| 2926 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2927 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2928 | |
| 2929 | # Section1 |
| 2930 | self.assertIn('section1', entries) |
| 2931 | section1 = entries['section1'] |
| 2932 | self.assertEqual(0x14, section1.offset) |
| 2933 | self.assertEqual(0x14, section1.image_pos) |
| 2934 | self.assertEqual(0x20, section1.size) |
| 2935 | |
| 2936 | # Second u-boot |
| 2937 | section_entries = section1.GetEntries() |
| 2938 | self.assertIn('u-boot', section_entries) |
| 2939 | entry = section_entries['u-boot'] |
| 2940 | self.assertEqual(0, entry.offset) |
| 2941 | self.assertEqual(0x14, entry.image_pos) |
| 2942 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2943 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2944 | |
| 2945 | # Section2 |
| 2946 | self.assertIn('section2', section_entries) |
| 2947 | section2 = section_entries['section2'] |
| 2948 | self.assertEqual(0x4, section2.offset) |
| 2949 | self.assertEqual(0x18, section2.image_pos) |
| 2950 | self.assertEqual(4, section2.size) |
| 2951 | |
| 2952 | # Third u-boot |
| 2953 | section_entries = section2.GetEntries() |
| 2954 | self.assertIn('u-boot', section_entries) |
| 2955 | entry = section_entries['u-boot'] |
| 2956 | self.assertEqual(0, entry.offset) |
| 2957 | self.assertEqual(0x18, entry.image_pos) |
| 2958 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2959 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2960 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2961 | def _RunReplaceCmd(self, entry_name, data, decomp=True, allow_resize=True, |
| 2962 | dts='132_replace.dts'): |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2963 | """Replace an entry in an image |
| 2964 | |
| 2965 | This writes the entry data to update it, then opens the updated file and |
| 2966 | returns the value that it now finds there. |
| 2967 | |
| 2968 | Args: |
| 2969 | entry_name: Entry name to replace |
| 2970 | data: Data to replace it with |
| 2971 | decomp: True to compress the data if needed, False if data is |
| 2972 | already compressed so should be used as is |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2973 | allow_resize: True to allow entries to change size, False to raise |
| 2974 | an exception |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2975 | |
| 2976 | Returns: |
| 2977 | Tuple: |
| 2978 | data from entry |
| 2979 | data from fdtmap (excluding header) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2980 | Image object that was modified |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2981 | """ |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2982 | dtb_data = self._DoReadFileDtb(dts, use_real_dtb=True, |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2983 | update_dtb=True)[1] |
| 2984 | |
| 2985 | self.assertIn('image', control.images) |
| 2986 | image = control.images['image'] |
| 2987 | entries = image.GetEntries() |
| 2988 | orig_dtb_data = entries['u-boot-dtb'].data |
| 2989 | orig_fdtmap_data = entries['fdtmap'].data |
| 2990 | |
| 2991 | image_fname = tools.GetOutputFilename('image.bin') |
| 2992 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 2993 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2994 | image = control.WriteEntry(updated_fname, entry_name, data, decomp, |
| 2995 | allow_resize) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2996 | data = control.ReadEntry(updated_fname, entry_name, decomp) |
| 2997 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2998 | # The DT data should not change unless resized: |
| 2999 | if not allow_resize: |
| 3000 | new_dtb_data = entries['u-boot-dtb'].data |
| 3001 | self.assertEqual(new_dtb_data, orig_dtb_data) |
| 3002 | new_fdtmap_data = entries['fdtmap'].data |
| 3003 | self.assertEqual(new_fdtmap_data, orig_fdtmap_data) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3004 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3005 | return data, orig_fdtmap_data[fdtmap.FDTMAP_HDR_LEN:], image |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3006 | |
| 3007 | def testReplaceSimple(self): |
| 3008 | """Test replacing a single file""" |
| 3009 | expected = b'x' * len(U_BOOT_DATA) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3010 | data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', expected, |
| 3011 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3012 | self.assertEqual(expected, data) |
| 3013 | |
| 3014 | # Test that the state looks right. There should be an FDT for the fdtmap |
| 3015 | # that we jsut read back in, and it should match what we find in the |
| 3016 | # 'control' tables. Checking for an FDT that does not exist should |
| 3017 | # return None. |
| 3018 | path, fdtmap = state.GetFdtContents('fdtmap') |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3019 | self.assertIsNotNone(path) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3020 | self.assertEqual(expected_fdtmap, fdtmap) |
| 3021 | |
| 3022 | dtb = state.GetFdtForEtype('fdtmap') |
| 3023 | self.assertEqual(dtb.GetContents(), fdtmap) |
| 3024 | |
| 3025 | missing_path, missing_fdtmap = state.GetFdtContents('missing') |
| 3026 | self.assertIsNone(missing_path) |
| 3027 | self.assertIsNone(missing_fdtmap) |
| 3028 | |
| 3029 | missing_dtb = state.GetFdtForEtype('missing') |
| 3030 | self.assertIsNone(missing_dtb) |
| 3031 | |
| 3032 | self.assertEqual('/binman', state.fdt_path_prefix) |
| 3033 | |
| 3034 | def testReplaceResizeFail(self): |
| 3035 | """Test replacing a file by something larger""" |
| 3036 | expected = U_BOOT_DATA + b'x' |
| 3037 | with self.assertRaises(ValueError) as e: |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3038 | self._RunReplaceCmd('u-boot', expected, allow_resize=False, |
| 3039 | dts='139_replace_repack.dts') |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3040 | self.assertIn("Node '/u-boot': Entry data size does not match, but resize is disabled", |
| 3041 | str(e.exception)) |
| 3042 | |
| 3043 | def testReplaceMulti(self): |
| 3044 | """Test replacing entry data where multiple images are generated""" |
| 3045 | data = self._DoReadFileDtb('133_replace_multi.dts', use_real_dtb=True, |
| 3046 | update_dtb=True)[0] |
| 3047 | expected = b'x' * len(U_BOOT_DATA) |
| 3048 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3049 | tools.WriteFile(updated_fname, data) |
| 3050 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3051 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3052 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3053 | data = control.ReadEntry(updated_fname, entry_name) |
| 3054 | self.assertEqual(expected, data) |
| 3055 | |
| 3056 | # Check the state looks right. |
| 3057 | self.assertEqual('/binman/image', state.fdt_path_prefix) |
| 3058 | |
| 3059 | # Now check we can write the first image |
| 3060 | image_fname = tools.GetOutputFilename('first-image.bin') |
| 3061 | updated_fname = tools.GetOutputFilename('first-updated.bin') |
| 3062 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
| 3063 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3064 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3065 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3066 | data = control.ReadEntry(updated_fname, entry_name) |
| 3067 | self.assertEqual(expected, data) |
| 3068 | |
| 3069 | # Check the state looks right. |
| 3070 | self.assertEqual('/binman/first-image', state.fdt_path_prefix) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 3071 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 3072 | def testUpdateFdtAllRepack(self): |
| 3073 | """Test that all device trees are updated with offset/size info""" |
| 3074 | data = self._DoReadFileRealDtb('134_fdt_update_all_repack.dts') |
| 3075 | SECTION_SIZE = 0x300 |
| 3076 | DTB_SIZE = 602 |
| 3077 | FDTMAP_SIZE = 608 |
| 3078 | base_expected = { |
| 3079 | 'offset': 0, |
| 3080 | 'size': SECTION_SIZE + DTB_SIZE * 2 + FDTMAP_SIZE, |
| 3081 | 'image-pos': 0, |
| 3082 | 'section:offset': 0, |
| 3083 | 'section:size': SECTION_SIZE, |
| 3084 | 'section:image-pos': 0, |
| 3085 | 'section/u-boot-dtb:offset': 4, |
| 3086 | 'section/u-boot-dtb:size': 636, |
| 3087 | 'section/u-boot-dtb:image-pos': 4, |
| 3088 | 'u-boot-spl-dtb:offset': SECTION_SIZE, |
| 3089 | 'u-boot-spl-dtb:size': DTB_SIZE, |
| 3090 | 'u-boot-spl-dtb:image-pos': SECTION_SIZE, |
| 3091 | 'u-boot-tpl-dtb:offset': SECTION_SIZE + DTB_SIZE, |
| 3092 | 'u-boot-tpl-dtb:image-pos': SECTION_SIZE + DTB_SIZE, |
| 3093 | 'u-boot-tpl-dtb:size': DTB_SIZE, |
| 3094 | 'fdtmap:offset': SECTION_SIZE + DTB_SIZE * 2, |
| 3095 | 'fdtmap:size': FDTMAP_SIZE, |
| 3096 | 'fdtmap:image-pos': SECTION_SIZE + DTB_SIZE * 2, |
| 3097 | } |
| 3098 | main_expected = { |
| 3099 | 'section:orig-size': SECTION_SIZE, |
| 3100 | 'section/u-boot-dtb:orig-offset': 4, |
| 3101 | } |
| 3102 | |
| 3103 | # We expect three device-tree files in the output, with the first one |
| 3104 | # within a fixed-size section. |
| 3105 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 3106 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 3107 | # main U-Boot tree. All three should have the same positions and offset |
| 3108 | # except that the main tree should include the main_expected properties |
| 3109 | start = 4 |
| 3110 | for item in ['', 'spl', 'tpl', None]: |
| 3111 | if item is None: |
| 3112 | start += 16 # Move past fdtmap header |
| 3113 | dtb = fdt.Fdt.FromData(data[start:]) |
| 3114 | dtb.Scan() |
| 3115 | props = self._GetPropTree(dtb, |
| 3116 | BASE_DTB_PROPS + REPACK_DTB_PROPS + ['spl', 'tpl'], |
| 3117 | prefix='/' if item is None else '/binman/') |
| 3118 | expected = dict(base_expected) |
| 3119 | if item: |
| 3120 | expected[item] = 0 |
| 3121 | else: |
| 3122 | # Main DTB and fdtdec should include the 'orig-' properties |
| 3123 | expected.update(main_expected) |
| 3124 | # Helpful for debugging: |
| 3125 | #for prop in sorted(props): |
| 3126 | #print('prop %s %s %s' % (prop, props[prop], expected[prop])) |
| 3127 | self.assertEqual(expected, props) |
| 3128 | if item == '': |
| 3129 | start = SECTION_SIZE |
| 3130 | else: |
| 3131 | start += dtb._fdt_obj.totalsize() |
| 3132 | |
Simon Glass | 1145376 | 2019-07-20 12:23:55 -0600 | [diff] [blame] | 3133 | def testFdtmapHeaderMiddle(self): |
| 3134 | """Test an FDT map in the middle of an image when it should be at end""" |
| 3135 | with self.assertRaises(ValueError) as e: |
| 3136 | self._DoReadFileRealDtb('135_fdtmap_hdr_middle.dts') |
| 3137 | self.assertIn("Invalid sibling order 'middle' for image-header: Must be at 'end' to match location", |
| 3138 | str(e.exception)) |
| 3139 | |
| 3140 | def testFdtmapHeaderStartBad(self): |
| 3141 | """Test an FDT map in middle of an image when it should be at start""" |
| 3142 | with self.assertRaises(ValueError) as e: |
| 3143 | self._DoReadFileRealDtb('136_fdtmap_hdr_startbad.dts') |
| 3144 | self.assertIn("Invalid sibling order 'end' for image-header: Must be at 'start' to match location", |
| 3145 | str(e.exception)) |
| 3146 | |
| 3147 | def testFdtmapHeaderEndBad(self): |
| 3148 | """Test an FDT map at the start of an image when it should be at end""" |
| 3149 | with self.assertRaises(ValueError) as e: |
| 3150 | self._DoReadFileRealDtb('137_fdtmap_hdr_endbad.dts') |
| 3151 | self.assertIn("Invalid sibling order 'start' for image-header: Must be at 'end' to match location", |
| 3152 | str(e.exception)) |
| 3153 | |
| 3154 | def testFdtmapHeaderNoSize(self): |
| 3155 | """Test an image header at the end of an image with undefined size""" |
| 3156 | self._DoReadFileRealDtb('138_fdtmap_hdr_nosize.dts') |
| 3157 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3158 | def testReplaceResize(self): |
| 3159 | """Test replacing a single file in an entry with a larger file""" |
| 3160 | expected = U_BOOT_DATA + b'x' |
| 3161 | data, _, image = self._RunReplaceCmd('u-boot', expected, |
| 3162 | dts='139_replace_repack.dts') |
| 3163 | self.assertEqual(expected, data) |
| 3164 | |
| 3165 | entries = image.GetEntries() |
| 3166 | dtb_data = entries['u-boot-dtb'].data |
| 3167 | dtb = fdt.Fdt.FromData(dtb_data) |
| 3168 | dtb.Scan() |
| 3169 | |
| 3170 | # The u-boot section should now be larger in the dtb |
| 3171 | node = dtb.GetNode('/binman/u-boot') |
| 3172 | self.assertEqual(len(expected), fdt_util.GetInt(node, 'size')) |
| 3173 | |
| 3174 | # Same for the fdtmap |
| 3175 | fdata = entries['fdtmap'].data |
| 3176 | fdtb = fdt.Fdt.FromData(fdata[fdtmap.FDTMAP_HDR_LEN:]) |
| 3177 | fdtb.Scan() |
| 3178 | fnode = fdtb.GetNode('/u-boot') |
| 3179 | self.assertEqual(len(expected), fdt_util.GetInt(fnode, 'size')) |
| 3180 | |
| 3181 | def testReplaceResizeNoRepack(self): |
| 3182 | """Test replacing an entry with a larger file when not allowed""" |
| 3183 | expected = U_BOOT_DATA + b'x' |
| 3184 | with self.assertRaises(ValueError) as e: |
| 3185 | self._RunReplaceCmd('u-boot', expected) |
| 3186 | self.assertIn('Entry data size does not match, but allow-repack is not present for this image', |
| 3187 | str(e.exception)) |
| 3188 | |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 3189 | def testEntryShrink(self): |
| 3190 | """Test contracting an entry after it is packed""" |
| 3191 | try: |
| 3192 | state.SetAllowEntryContraction(True) |
| 3193 | data = self._DoReadFileDtb('140_entry_shrink.dts', |
| 3194 | update_dtb=True)[0] |
| 3195 | finally: |
| 3196 | state.SetAllowEntryContraction(False) |
| 3197 | self.assertEqual(b'a', data[:1]) |
| 3198 | self.assertEqual(U_BOOT_DATA, data[1:1 + len(U_BOOT_DATA)]) |
| 3199 | self.assertEqual(b'a', data[-1:]) |
| 3200 | |
| 3201 | def testEntryShrinkFail(self): |
| 3202 | """Test not being allowed to contract an entry after it is packed""" |
| 3203 | data = self._DoReadFileDtb('140_entry_shrink.dts', update_dtb=True)[0] |
| 3204 | |
| 3205 | # In this case there is a spare byte at the end of the data. The size of |
| 3206 | # the contents is only 1 byte but we still have the size before it |
| 3207 | # shrunk. |
| 3208 | self.assertEqual(b'a\0', data[:2]) |
| 3209 | self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)]) |
| 3210 | self.assertEqual(b'a\0', data[-2:]) |
| 3211 | |
Simon Glass | 70e3298 | 2019-07-20 12:24:01 -0600 | [diff] [blame] | 3212 | def testDescriptorOffset(self): |
| 3213 | """Test that the Intel descriptor is always placed at at the start""" |
| 3214 | data = self._DoReadFileDtb('141_descriptor_offset.dts') |
| 3215 | image = control.images['image'] |
| 3216 | entries = image.GetEntries() |
| 3217 | desc = entries['intel-descriptor'] |
| 3218 | self.assertEqual(0xff800000, desc.offset); |
| 3219 | self.assertEqual(0xff800000, desc.image_pos); |
| 3220 | |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 3221 | def testReplaceCbfs(self): |
| 3222 | """Test replacing a single file in CBFS without changing the size""" |
| 3223 | self._CheckLz4() |
| 3224 | expected = b'x' * len(U_BOOT_DATA) |
| 3225 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
| 3226 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3227 | tools.WriteFile(updated_fname, data) |
| 3228 | entry_name = 'section/cbfs/u-boot' |
| 3229 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3230 | allow_resize=True) |
| 3231 | data = control.ReadEntry(updated_fname, entry_name) |
| 3232 | self.assertEqual(expected, data) |
| 3233 | |
| 3234 | def testReplaceResizeCbfs(self): |
| 3235 | """Test replacing a single file in CBFS with one of a different size""" |
| 3236 | self._CheckLz4() |
| 3237 | expected = U_BOOT_DATA + b'x' |
| 3238 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
| 3239 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3240 | tools.WriteFile(updated_fname, data) |
| 3241 | entry_name = 'section/cbfs/u-boot' |
| 3242 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3243 | allow_resize=True) |
| 3244 | data = control.ReadEntry(updated_fname, entry_name) |
| 3245 | self.assertEqual(expected, data) |
| 3246 | |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3247 | def _SetupForReplace(self): |
| 3248 | """Set up some files to use to replace entries |
| 3249 | |
| 3250 | This generates an image, copies it to a new file, extracts all the files |
| 3251 | in it and updates some of them |
| 3252 | |
| 3253 | Returns: |
| 3254 | List |
| 3255 | Image filename |
| 3256 | Output directory |
| 3257 | Expected values for updated entries, each a string |
| 3258 | """ |
| 3259 | data = self._DoReadFileRealDtb('143_replace_all.dts') |
| 3260 | |
| 3261 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3262 | tools.WriteFile(updated_fname, data) |
| 3263 | |
| 3264 | outdir = os.path.join(self._indir, 'extract') |
| 3265 | einfos = control.ExtractEntries(updated_fname, None, outdir, []) |
| 3266 | |
| 3267 | expected1 = b'x' + U_BOOT_DATA + b'y' |
| 3268 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
| 3269 | tools.WriteFile(u_boot_fname1, expected1) |
| 3270 | |
| 3271 | expected2 = b'a' + U_BOOT_DATA + b'b' |
| 3272 | u_boot_fname2 = os.path.join(outdir, 'u-boot2') |
| 3273 | tools.WriteFile(u_boot_fname2, expected2) |
| 3274 | |
| 3275 | expected_text = b'not the same text' |
| 3276 | text_fname = os.path.join(outdir, 'text') |
| 3277 | tools.WriteFile(text_fname, expected_text) |
| 3278 | |
| 3279 | dtb_fname = os.path.join(outdir, 'u-boot-dtb') |
| 3280 | dtb = fdt.FdtScan(dtb_fname) |
| 3281 | node = dtb.GetNode('/binman/text') |
| 3282 | node.AddString('my-property', 'the value') |
| 3283 | dtb.Sync(auto_resize=True) |
| 3284 | dtb.Flush() |
| 3285 | |
| 3286 | return updated_fname, outdir, expected1, expected2, expected_text |
| 3287 | |
| 3288 | def _CheckReplaceMultiple(self, entry_paths): |
| 3289 | """Handle replacing the contents of multiple entries |
| 3290 | |
| 3291 | Args: |
| 3292 | entry_paths: List of entry paths to replace |
| 3293 | |
| 3294 | Returns: |
| 3295 | List |
| 3296 | Dict of entries in the image: |
| 3297 | key: Entry name |
| 3298 | Value: Entry object |
| 3299 | Expected values for updated entries, each a string |
| 3300 | """ |
| 3301 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3302 | self._SetupForReplace()) |
| 3303 | control.ReplaceEntries(updated_fname, None, outdir, entry_paths) |
| 3304 | |
| 3305 | image = Image.FromFile(updated_fname) |
| 3306 | image.LoadData() |
| 3307 | return image.GetEntries(), expected1, expected2, expected_text |
| 3308 | |
| 3309 | def testReplaceAll(self): |
| 3310 | """Test replacing the contents of all entries""" |
| 3311 | entries, expected1, expected2, expected_text = ( |
| 3312 | self._CheckReplaceMultiple([])) |
| 3313 | data = entries['u-boot'].data |
| 3314 | self.assertEqual(expected1, data) |
| 3315 | |
| 3316 | data = entries['u-boot2'].data |
| 3317 | self.assertEqual(expected2, data) |
| 3318 | |
| 3319 | data = entries['text'].data |
| 3320 | self.assertEqual(expected_text, data) |
| 3321 | |
| 3322 | # Check that the device tree is updated |
| 3323 | data = entries['u-boot-dtb'].data |
| 3324 | dtb = fdt.Fdt.FromData(data) |
| 3325 | dtb.Scan() |
| 3326 | node = dtb.GetNode('/binman/text') |
| 3327 | self.assertEqual('the value', node.props['my-property'].value) |
| 3328 | |
| 3329 | def testReplaceSome(self): |
| 3330 | """Test replacing the contents of a few entries""" |
| 3331 | entries, expected1, expected2, expected_text = ( |
| 3332 | self._CheckReplaceMultiple(['u-boot2', 'text'])) |
| 3333 | |
| 3334 | # This one should not change |
| 3335 | data = entries['u-boot'].data |
| 3336 | self.assertEqual(U_BOOT_DATA, data) |
| 3337 | |
| 3338 | data = entries['u-boot2'].data |
| 3339 | self.assertEqual(expected2, data) |
| 3340 | |
| 3341 | data = entries['text'].data |
| 3342 | self.assertEqual(expected_text, data) |
| 3343 | |
| 3344 | def testReplaceCmd(self): |
| 3345 | """Test replacing a file fron an image on the command line""" |
| 3346 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3347 | |
| 3348 | try: |
| 3349 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3350 | |
| 3351 | fname = os.path.join(tmpdir, 'update-u-boot.bin') |
| 3352 | expected = b'x' * len(U_BOOT_DATA) |
| 3353 | tools.WriteFile(fname, expected) |
| 3354 | |
| 3355 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', '-f', fname) |
| 3356 | data = tools.ReadFile(updated_fname) |
| 3357 | self.assertEqual(expected, data[:len(expected)]) |
| 3358 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3359 | self.assertFalse(os.path.exists(map_fname)) |
| 3360 | finally: |
| 3361 | shutil.rmtree(tmpdir) |
| 3362 | |
| 3363 | def testReplaceCmdSome(self): |
| 3364 | """Test replacing some files fron an image on the command line""" |
| 3365 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3366 | self._SetupForReplace()) |
| 3367 | |
| 3368 | self._DoBinman('replace', '-i', updated_fname, '-I', outdir, |
| 3369 | 'u-boot2', 'text') |
| 3370 | |
| 3371 | tools.PrepareOutputDir(None) |
| 3372 | image = Image.FromFile(updated_fname) |
| 3373 | image.LoadData() |
| 3374 | entries = image.GetEntries() |
| 3375 | |
| 3376 | # This one should not change |
| 3377 | data = entries['u-boot'].data |
| 3378 | self.assertEqual(U_BOOT_DATA, data) |
| 3379 | |
| 3380 | data = entries['u-boot2'].data |
| 3381 | self.assertEqual(expected2, data) |
| 3382 | |
| 3383 | data = entries['text'].data |
| 3384 | self.assertEqual(expected_text, data) |
| 3385 | |
| 3386 | def testReplaceMissing(self): |
| 3387 | """Test replacing entries where the file is missing""" |
| 3388 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3389 | self._SetupForReplace()) |
| 3390 | |
| 3391 | # Remove one of the files, to generate a warning |
| 3392 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
| 3393 | os.remove(u_boot_fname1) |
| 3394 | |
| 3395 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3396 | control.ReplaceEntries(updated_fname, None, outdir, []) |
| 3397 | self.assertIn("Skipping entry '/u-boot' from missing file", |
Simon Glass | 6e02f7c | 2020-07-09 18:39:39 -0600 | [diff] [blame] | 3398 | stderr.getvalue()) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3399 | |
| 3400 | def testReplaceCmdMap(self): |
| 3401 | """Test replacing a file fron an image on the command line""" |
| 3402 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3403 | |
| 3404 | try: |
| 3405 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3406 | |
| 3407 | fname = os.path.join(self._indir, 'update-u-boot.bin') |
| 3408 | expected = b'x' * len(U_BOOT_DATA) |
| 3409 | tools.WriteFile(fname, expected) |
| 3410 | |
| 3411 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', |
| 3412 | '-f', fname, '-m') |
| 3413 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3414 | self.assertTrue(os.path.exists(map_fname)) |
| 3415 | finally: |
| 3416 | shutil.rmtree(tmpdir) |
| 3417 | |
| 3418 | def testReplaceNoEntryPaths(self): |
| 3419 | """Test replacing an entry without an entry path""" |
| 3420 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3421 | image_fname = tools.GetOutputFilename('image.bin') |
| 3422 | with self.assertRaises(ValueError) as e: |
| 3423 | control.ReplaceEntries(image_fname, 'fname', None, []) |
| 3424 | self.assertIn('Must specify an entry path to read with -f', |
| 3425 | str(e.exception)) |
| 3426 | |
| 3427 | def testReplaceTooManyEntryPaths(self): |
| 3428 | """Test extracting some entries""" |
| 3429 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3430 | image_fname = tools.GetOutputFilename('image.bin') |
| 3431 | with self.assertRaises(ValueError) as e: |
| 3432 | control.ReplaceEntries(image_fname, 'fname', None, ['a', 'b']) |
| 3433 | self.assertIn('Must specify exactly one entry path to write with -f', |
| 3434 | str(e.exception)) |
| 3435 | |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 3436 | def testPackReset16(self): |
| 3437 | """Test that an image with an x86 reset16 region can be created""" |
| 3438 | data = self._DoReadFile('144_x86_reset16.dts') |
| 3439 | self.assertEqual(X86_RESET16_DATA, data[:len(X86_RESET16_DATA)]) |
| 3440 | |
| 3441 | def testPackReset16Spl(self): |
| 3442 | """Test that an image with an x86 reset16-spl region can be created""" |
| 3443 | data = self._DoReadFile('145_x86_reset16_spl.dts') |
| 3444 | self.assertEqual(X86_RESET16_SPL_DATA, data[:len(X86_RESET16_SPL_DATA)]) |
| 3445 | |
| 3446 | def testPackReset16Tpl(self): |
| 3447 | """Test that an image with an x86 reset16-tpl region can be created""" |
| 3448 | data = self._DoReadFile('146_x86_reset16_tpl.dts') |
| 3449 | self.assertEqual(X86_RESET16_TPL_DATA, data[:len(X86_RESET16_TPL_DATA)]) |
| 3450 | |
Simon Glass | 232f90c | 2019-08-24 07:22:50 -0600 | [diff] [blame] | 3451 | def testPackIntelFit(self): |
| 3452 | """Test that an image with an Intel FIT and pointer can be created""" |
| 3453 | data = self._DoReadFile('147_intel_fit.dts') |
| 3454 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3455 | fit = data[16:32]; |
| 3456 | self.assertEqual(b'_FIT_ \x01\x00\x00\x00\x00\x01\x80}' , fit) |
| 3457 | ptr = struct.unpack('<i', data[0x40:0x44])[0] |
| 3458 | |
| 3459 | image = control.images['image'] |
| 3460 | entries = image.GetEntries() |
| 3461 | expected_ptr = entries['intel-fit'].image_pos - (1 << 32) |
| 3462 | self.assertEqual(expected_ptr, ptr) |
| 3463 | |
| 3464 | def testPackIntelFitMissing(self): |
| 3465 | """Test detection of a FIT pointer with not FIT region""" |
| 3466 | with self.assertRaises(ValueError) as e: |
| 3467 | self._DoReadFile('148_intel_fit_missing.dts') |
| 3468 | self.assertIn("'intel-fit-ptr' section must have an 'intel-fit' sibling", |
| 3469 | str(e.exception)) |
| 3470 | |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3471 | def _CheckSymbolsTplSection(self, dts, expected_vals): |
| 3472 | data = self._DoReadFile(dts) |
| 3473 | sym_values = struct.pack('<LQLL', *expected_vals) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3474 | upto1 = 4 + len(U_BOOT_SPL_DATA) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3475 | expected1 = tools.GetBytes(0xff, 4) + sym_values + U_BOOT_SPL_DATA[20:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3476 | self.assertEqual(expected1, data[:upto1]) |
| 3477 | |
| 3478 | upto2 = upto1 + 1 + len(U_BOOT_SPL_DATA) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3479 | expected2 = tools.GetBytes(0xff, 1) + sym_values + U_BOOT_SPL_DATA[20:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3480 | self.assertEqual(expected2, data[upto1:upto2]) |
| 3481 | |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 3482 | upto3 = 0x34 + len(U_BOOT_DATA) |
| 3483 | expected3 = tools.GetBytes(0xff, 1) + U_BOOT_DATA |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3484 | self.assertEqual(expected3, data[upto2:upto3]) |
| 3485 | |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3486 | expected4 = sym_values + U_BOOT_TPL_DATA[20:] |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3487 | self.assertEqual(expected4, data[upto3:upto3 + len(U_BOOT_TPL_DATA)]) |
| 3488 | |
| 3489 | def testSymbolsTplSection(self): |
| 3490 | """Test binman can assign symbols embedded in U-Boot TPL in a section""" |
| 3491 | self._SetupSplElf('u_boot_binman_syms') |
| 3492 | self._SetupTplElf('u_boot_binman_syms') |
| 3493 | self._CheckSymbolsTplSection('149_symbols_tpl.dts', |
| 3494 | [0x04, 0x1c, 0x10 + 0x34, 0x04]) |
| 3495 | |
| 3496 | def testSymbolsTplSectionX86(self): |
| 3497 | """Test binman can assign symbols in a section with end-at-4gb""" |
| 3498 | self._SetupSplElf('u_boot_binman_syms_x86') |
| 3499 | self._SetupTplElf('u_boot_binman_syms_x86') |
| 3500 | self._CheckSymbolsTplSection('155_symbols_tpl_x86.dts', |
| 3501 | [0xffffff04, 0xffffff1c, 0xffffff34, |
| 3502 | 0x04]) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3503 | |
Simon Glass | 98c5957 | 2019-08-24 07:23:03 -0600 | [diff] [blame] | 3504 | def testPackX86RomIfwiSectiom(self): |
| 3505 | """Test that a section can be placed in an IFWI region""" |
| 3506 | self._SetupIfwi('fitimage.bin') |
| 3507 | data = self._DoReadFile('151_x86_rom_ifwi_section.dts') |
| 3508 | self._CheckIfwi(data) |
| 3509 | |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3510 | def testPackFspM(self): |
| 3511 | """Test that an image with a FSP memory-init binary can be created""" |
| 3512 | data = self._DoReadFile('152_intel_fsp_m.dts') |
| 3513 | self.assertEqual(FSP_M_DATA, data[:len(FSP_M_DATA)]) |
| 3514 | |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 3515 | def testPackFspS(self): |
| 3516 | """Test that an image with a FSP silicon-init binary can be created""" |
| 3517 | data = self._DoReadFile('153_intel_fsp_s.dts') |
| 3518 | self.assertEqual(FSP_S_DATA, data[:len(FSP_S_DATA)]) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3519 | |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 3520 | def testPackFspT(self): |
| 3521 | """Test that an image with a FSP temp-ram-init binary can be created""" |
| 3522 | data = self._DoReadFile('154_intel_fsp_t.dts') |
| 3523 | self.assertEqual(FSP_T_DATA, data[:len(FSP_T_DATA)]) |
| 3524 | |
Simon Glass | 48f3aad | 2020-07-09 18:39:31 -0600 | [diff] [blame] | 3525 | def testMkimage(self): |
| 3526 | """Test using mkimage to build an image""" |
| 3527 | data = self._DoReadFile('156_mkimage.dts') |
| 3528 | |
| 3529 | # Just check that the data appears in the file somewhere |
| 3530 | self.assertIn(U_BOOT_SPL_DATA, data) |
| 3531 | |
Simon Glass | 5e56018 | 2020-07-09 18:39:36 -0600 | [diff] [blame] | 3532 | def testExtblob(self): |
| 3533 | """Test an image with an external blob""" |
| 3534 | data = self._DoReadFile('157_blob_ext.dts') |
| 3535 | self.assertEqual(REFCODE_DATA, data) |
| 3536 | |
| 3537 | def testExtblobMissing(self): |
| 3538 | """Test an image with a missing external blob""" |
| 3539 | with self.assertRaises(ValueError) as e: |
| 3540 | self._DoReadFile('158_blob_ext_missing.dts') |
| 3541 | self.assertIn("Filename 'missing-file' not found in input path", |
| 3542 | str(e.exception)) |
| 3543 | |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3544 | def testExtblobMissingOk(self): |
| 3545 | """Test an image with an missing external blob that is allowed""" |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 3546 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3547 | self._DoTestFile('158_blob_ext_missing.dts', allow_missing=True) |
| 3548 | err = stderr.getvalue() |
| 3549 | self.assertRegex(err, "Image 'main-section'.*missing.*: blob-ext") |
| 3550 | |
| 3551 | def testExtblobMissingOkSect(self): |
| 3552 | """Test an image with an missing external blob that is allowed""" |
| 3553 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3554 | self._DoTestFile('159_blob_ext_missing_sect.dts', |
| 3555 | allow_missing=True) |
| 3556 | err = stderr.getvalue() |
| 3557 | self.assertRegex(err, "Image 'main-section'.*missing.*: " |
| 3558 | "blob-ext blob-ext2") |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3559 | |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3560 | def testPackX86RomMeMissingDesc(self): |
| 3561 | """Test that an missing Intel descriptor entry is allowed""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3562 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 3563 | self._DoTestFile('164_x86_rom_me_missing.dts', allow_missing=True) |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3564 | err = stderr.getvalue() |
| 3565 | self.assertRegex(err, |
| 3566 | "Image 'main-section'.*missing.*: intel-descriptor") |
| 3567 | |
| 3568 | def testPackX86RomMissingIfwi(self): |
| 3569 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 3570 | self._SetupIfwi('fitimage.bin') |
| 3571 | pathname = os.path.join(self._indir, 'fitimage.bin') |
| 3572 | os.remove(pathname) |
| 3573 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3574 | self._DoTestFile('111_x86_rom_ifwi.dts', allow_missing=True) |
| 3575 | err = stderr.getvalue() |
| 3576 | self.assertRegex(err, "Image 'main-section'.*missing.*: intel-ifwi") |
| 3577 | |
Simon Glass | d70829a | 2020-07-09 18:39:42 -0600 | [diff] [blame] | 3578 | def testPackOverlap(self): |
| 3579 | """Test that zero-size overlapping regions are ignored""" |
| 3580 | self._DoTestFile('160_pack_overlap_zero.dts') |
| 3581 | |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3582 | def testSimpleFit(self): |
| 3583 | """Test an image with a FIT inside""" |
| 3584 | data = self._DoReadFile('161_fit.dts') |
| 3585 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3586 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3587 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3588 | |
| 3589 | # The data should be inside the FIT |
| 3590 | dtb = fdt.Fdt.FromData(fit_data) |
| 3591 | dtb.Scan() |
| 3592 | fnode = dtb.GetNode('/images/kernel') |
| 3593 | self.assertIn('data', fnode.props) |
| 3594 | |
| 3595 | fname = os.path.join(self._indir, 'fit_data.fit') |
| 3596 | tools.WriteFile(fname, fit_data) |
| 3597 | out = tools.Run('dumpimage', '-l', fname) |
| 3598 | |
| 3599 | # Check a few features to make sure the plumbing works. We don't need |
| 3600 | # to test the operation of mkimage or dumpimage here. First convert the |
| 3601 | # output into a dict where the keys are the fields printed by dumpimage |
| 3602 | # and the values are a list of values for each field |
| 3603 | lines = out.splitlines() |
| 3604 | |
| 3605 | # Converts "Compression: gzip compressed" into two groups: |
| 3606 | # 'Compression' and 'gzip compressed' |
| 3607 | re_line = re.compile(r'^ *([^:]*)(?:: *(.*))?$') |
| 3608 | vals = collections.defaultdict(list) |
| 3609 | for line in lines: |
| 3610 | mat = re_line.match(line) |
| 3611 | vals[mat.group(1)].append(mat.group(2)) |
| 3612 | |
| 3613 | self.assertEquals('FIT description: test-desc', lines[0]) |
| 3614 | self.assertIn('Created:', lines[1]) |
| 3615 | self.assertIn('Image 0 (kernel)', vals) |
| 3616 | self.assertIn('Hash value', vals) |
| 3617 | data_sizes = vals.get('Data Size') |
| 3618 | self.assertIsNotNone(data_sizes) |
| 3619 | self.assertEqual(2, len(data_sizes)) |
| 3620 | # Format is "4 Bytes = 0.00 KiB = 0.00 MiB" so take the first word |
| 3621 | self.assertEqual(len(U_BOOT_DATA), int(data_sizes[0].split()[0])) |
| 3622 | self.assertEqual(len(U_BOOT_SPL_DTB_DATA), int(data_sizes[1].split()[0])) |
| 3623 | |
| 3624 | def testFitExternal(self): |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 3625 | """Test an image with an FIT with external images""" |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3626 | data = self._DoReadFile('162_fit_external.dts') |
| 3627 | fit_data = data[len(U_BOOT_DATA):-2] # _testing is 2 bytes |
| 3628 | |
| 3629 | # The data should be outside the FIT |
| 3630 | dtb = fdt.Fdt.FromData(fit_data) |
| 3631 | dtb.Scan() |
| 3632 | fnode = dtb.GetNode('/images/kernel') |
| 3633 | self.assertNotIn('data', fnode.props) |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 3634 | |
Alper Nebi Yasak | 6aae239 | 2020-08-31 12:58:18 +0300 | [diff] [blame] | 3635 | def testSectionIgnoreHashSignature(self): |
| 3636 | """Test that sections ignore hash, signature nodes for its data""" |
| 3637 | data = self._DoReadFile('165_section_ignore_hash_signature.dts') |
| 3638 | expected = (U_BOOT_DATA + U_BOOT_DATA) |
| 3639 | self.assertEqual(expected, data) |
| 3640 | |
Alper Nebi Yasak | c503060 | 2020-08-31 12:58:19 +0300 | [diff] [blame] | 3641 | def testPadInSections(self): |
| 3642 | """Test pad-before, pad-after for entries in sections""" |
Simon Glass | d12599d | 2020-10-26 17:40:09 -0600 | [diff] [blame] | 3643 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 3644 | '166_pad_in_sections.dts', update_dtb=True) |
Alper Nebi Yasak | c503060 | 2020-08-31 12:58:19 +0300 | [diff] [blame] | 3645 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 3646 | U_BOOT_DATA + tools.GetBytes(ord('!'), 6) + |
| 3647 | U_BOOT_DATA) |
| 3648 | self.assertEqual(expected, data) |
| 3649 | |
Simon Glass | d12599d | 2020-10-26 17:40:09 -0600 | [diff] [blame] | 3650 | dtb = fdt.Fdt(out_dtb_fname) |
| 3651 | dtb.Scan() |
| 3652 | props = self._GetPropTree(dtb, ['size', 'image-pos', 'offset']) |
| 3653 | expected = { |
| 3654 | 'image-pos': 0, |
| 3655 | 'offset': 0, |
| 3656 | 'size': 12 + 6 + 3 * len(U_BOOT_DATA), |
| 3657 | |
| 3658 | 'section:image-pos': 0, |
| 3659 | 'section:offset': 0, |
| 3660 | 'section:size': 12 + 6 + 3 * len(U_BOOT_DATA), |
| 3661 | |
| 3662 | 'section/before:image-pos': 0, |
| 3663 | 'section/before:offset': 0, |
| 3664 | 'section/before:size': len(U_BOOT_DATA), |
| 3665 | |
| 3666 | 'section/u-boot:image-pos': 4, |
| 3667 | 'section/u-boot:offset': 4, |
| 3668 | 'section/u-boot:size': 12 + len(U_BOOT_DATA) + 6, |
| 3669 | |
| 3670 | 'section/after:image-pos': 26, |
| 3671 | 'section/after:offset': 26, |
| 3672 | 'section/after:size': len(U_BOOT_DATA), |
| 3673 | } |
| 3674 | self.assertEqual(expected, props) |
| 3675 | |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 3676 | def testFitImageSubentryAlignment(self): |
| 3677 | """Test relative alignability of FIT image subentries""" |
| 3678 | entry_args = { |
| 3679 | 'test-id': TEXT_DATA, |
| 3680 | } |
| 3681 | data, _, _, _ = self._DoReadFileDtb('167_fit_image_subentry_alignment.dts', |
| 3682 | entry_args=entry_args) |
| 3683 | dtb = fdt.Fdt.FromData(data) |
| 3684 | dtb.Scan() |
| 3685 | |
| 3686 | node = dtb.GetNode('/images/kernel') |
| 3687 | data = dtb.GetProps(node)["data"].bytes |
| 3688 | align_pad = 0x10 - (len(U_BOOT_SPL_DATA) % 0x10) |
| 3689 | expected = (tools.GetBytes(0, 0x20) + U_BOOT_SPL_DATA + |
| 3690 | tools.GetBytes(0, align_pad) + U_BOOT_DATA) |
| 3691 | self.assertEqual(expected, data) |
| 3692 | |
| 3693 | node = dtb.GetNode('/images/fdt-1') |
| 3694 | data = dtb.GetProps(node)["data"].bytes |
| 3695 | expected = (U_BOOT_SPL_DTB_DATA + tools.GetBytes(0, 20) + |
| 3696 | tools.ToBytes(TEXT_DATA) + tools.GetBytes(0, 30) + |
| 3697 | U_BOOT_DTB_DATA) |
| 3698 | self.assertEqual(expected, data) |
| 3699 | |
| 3700 | def testFitExtblobMissingOk(self): |
| 3701 | """Test a FIT with a missing external blob that is allowed""" |
| 3702 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3703 | self._DoTestFile('168_fit_missing_blob.dts', |
| 3704 | allow_missing=True) |
| 3705 | err = stderr.getvalue() |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 3706 | self.assertRegex(err, "Image 'main-section'.*missing.*: atf-bl31") |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 3707 | |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 3708 | def testBlobNamedByArgMissing(self): |
| 3709 | """Test handling of a missing entry arg""" |
| 3710 | with self.assertRaises(ValueError) as e: |
| 3711 | self._DoReadFile('068_blob_named_by_arg.dts') |
| 3712 | self.assertIn("Missing required properties/entry args: cros-ec-rw-path", |
| 3713 | str(e.exception)) |
| 3714 | |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 3715 | def testPackBl31(self): |
| 3716 | """Test that an image with an ATF BL31 binary can be created""" |
| 3717 | data = self._DoReadFile('169_atf_bl31.dts') |
| 3718 | self.assertEqual(ATF_BL31_DATA, data[:len(ATF_BL31_DATA)]) |
| 3719 | |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 3720 | def testPackScp(self): |
| 3721 | """Test that an image with an SCP binary can be created""" |
| 3722 | data = self._DoReadFile('172_scp.dts') |
| 3723 | self.assertEqual(SCP_DATA, data[:len(SCP_DATA)]) |
| 3724 | |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3725 | def testFitFdt(self): |
| 3726 | """Test an image with an FIT with multiple FDT images""" |
| 3727 | def _CheckFdt(seq, expected_data): |
| 3728 | """Check the FDT nodes |
| 3729 | |
| 3730 | Args: |
| 3731 | seq: Sequence number to check (0 or 1) |
| 3732 | expected_data: Expected contents of 'data' property |
| 3733 | """ |
| 3734 | name = 'fdt-%d' % seq |
| 3735 | fnode = dtb.GetNode('/images/%s' % name) |
| 3736 | self.assertIsNotNone(fnode) |
| 3737 | self.assertEqual({'description','type', 'compression', 'data'}, |
| 3738 | set(fnode.props.keys())) |
| 3739 | self.assertEqual(expected_data, fnode.props['data'].bytes) |
| 3740 | self.assertEqual('fdt-test-fdt%d.dtb' % seq, |
| 3741 | fnode.props['description'].value) |
| 3742 | |
| 3743 | def _CheckConfig(seq, expected_data): |
| 3744 | """Check the configuration nodes |
| 3745 | |
| 3746 | Args: |
| 3747 | seq: Sequence number to check (0 or 1) |
| 3748 | expected_data: Expected contents of 'data' property |
| 3749 | """ |
| 3750 | cnode = dtb.GetNode('/configurations') |
| 3751 | self.assertIn('default', cnode.props) |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3752 | self.assertEqual('config-2', cnode.props['default'].value) |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3753 | |
| 3754 | name = 'config-%d' % seq |
| 3755 | fnode = dtb.GetNode('/configurations/%s' % name) |
| 3756 | self.assertIsNotNone(fnode) |
| 3757 | self.assertEqual({'description','firmware', 'loadables', 'fdt'}, |
| 3758 | set(fnode.props.keys())) |
| 3759 | self.assertEqual('conf-test-fdt%d.dtb' % seq, |
| 3760 | fnode.props['description'].value) |
| 3761 | self.assertEqual('fdt-%d' % seq, fnode.props['fdt'].value) |
| 3762 | |
| 3763 | entry_args = { |
| 3764 | 'of-list': 'test-fdt1 test-fdt2', |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3765 | 'default-dt': 'test-fdt2', |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3766 | } |
| 3767 | data = self._DoReadFileDtb( |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3768 | '172_fit_fdt.dts', |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3769 | entry_args=entry_args, |
| 3770 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 3771 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3772 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3773 | |
| 3774 | dtb = fdt.Fdt.FromData(fit_data) |
| 3775 | dtb.Scan() |
| 3776 | fnode = dtb.GetNode('/images/kernel') |
| 3777 | self.assertIn('data', fnode.props) |
| 3778 | |
| 3779 | # Check all the properties in fdt-1 and fdt-2 |
| 3780 | _CheckFdt(1, TEST_FDT1_DATA) |
| 3781 | _CheckFdt(2, TEST_FDT2_DATA) |
| 3782 | |
| 3783 | # Check configurations |
| 3784 | _CheckConfig(1, TEST_FDT1_DATA) |
| 3785 | _CheckConfig(2, TEST_FDT2_DATA) |
| 3786 | |
| 3787 | def testFitFdtMissingList(self): |
| 3788 | """Test handling of a missing 'of-list' entry arg""" |
| 3789 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3790 | self._DoReadFile('172_fit_fdt.dts') |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3791 | self.assertIn("Generator node requires 'of-list' entry argument", |
| 3792 | str(e.exception)) |
| 3793 | |
| 3794 | def testFitFdtEmptyList(self): |
| 3795 | """Test handling of an empty 'of-list' entry arg""" |
| 3796 | entry_args = { |
| 3797 | 'of-list': '', |
| 3798 | } |
| 3799 | data = self._DoReadFileDtb('170_fit_fdt.dts', entry_args=entry_args)[0] |
| 3800 | |
| 3801 | def testFitFdtMissingProp(self): |
| 3802 | """Test handling of a missing 'fit,fdt-list' property""" |
| 3803 | with self.assertRaises(ValueError) as e: |
| 3804 | self._DoReadFile('171_fit_fdt_missing_prop.dts') |
| 3805 | self.assertIn("Generator node requires 'fit,fdt-list' property", |
| 3806 | str(e.exception)) |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 3807 | |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3808 | def testFitFdtEmptyList(self): |
| 3809 | """Test handling of an empty 'of-list' entry arg""" |
| 3810 | entry_args = { |
| 3811 | 'of-list': '', |
| 3812 | } |
| 3813 | data = self._DoReadFileDtb('172_fit_fdt.dts', entry_args=entry_args)[0] |
| 3814 | |
| 3815 | def testFitFdtMissing(self): |
| 3816 | """Test handling of a missing 'default-dt' entry arg""" |
| 3817 | entry_args = { |
| 3818 | 'of-list': 'test-fdt1 test-fdt2', |
| 3819 | } |
| 3820 | with self.assertRaises(ValueError) as e: |
| 3821 | self._DoReadFileDtb( |
| 3822 | '172_fit_fdt.dts', |
| 3823 | entry_args=entry_args, |
| 3824 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 3825 | self.assertIn("Generated 'default' node requires default-dt entry argument", |
| 3826 | str(e.exception)) |
| 3827 | |
| 3828 | def testFitFdtNotInList(self): |
| 3829 | """Test handling of a default-dt that is not in the of-list""" |
| 3830 | entry_args = { |
| 3831 | 'of-list': 'test-fdt1 test-fdt2', |
| 3832 | 'default-dt': 'test-fdt3', |
| 3833 | } |
| 3834 | with self.assertRaises(ValueError) as e: |
| 3835 | self._DoReadFileDtb( |
| 3836 | '172_fit_fdt.dts', |
| 3837 | entry_args=entry_args, |
| 3838 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 3839 | self.assertIn("default-dt entry argument 'test-fdt3' not found in fdt list: test-fdt1, test-fdt2", |
| 3840 | str(e.exception)) |
| 3841 | |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 3842 | def testFitExtblobMissingHelp(self): |
| 3843 | """Test display of help messages when an external blob is missing""" |
| 3844 | control.missing_blob_help = control._ReadMissingBlobHelp() |
| 3845 | control.missing_blob_help['wibble'] = 'Wibble test' |
| 3846 | control.missing_blob_help['another'] = 'Another test' |
| 3847 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3848 | self._DoTestFile('168_fit_missing_blob.dts', |
| 3849 | allow_missing=True) |
| 3850 | err = stderr.getvalue() |
| 3851 | |
| 3852 | # We can get the tag from the name, the type or the missing-msg |
| 3853 | # property. Check all three. |
| 3854 | self.assertIn('You may need to build ARM Trusted', err) |
| 3855 | self.assertIn('Wibble test', err) |
| 3856 | self.assertIn('Another test', err) |
| 3857 | |
Simon Glass | 6f1f4d4 | 2020-09-06 10:35:32 -0600 | [diff] [blame] | 3858 | def testMissingBlob(self): |
| 3859 | """Test handling of a blob containing a missing file""" |
| 3860 | with self.assertRaises(ValueError) as e: |
| 3861 | self._DoTestFile('173_missing_blob.dts', allow_missing=True) |
| 3862 | self.assertIn("Filename 'missing' not found in input path", |
| 3863 | str(e.exception)) |
| 3864 | |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 3865 | def testEnvironment(self): |
| 3866 | """Test adding a U-Boot environment""" |
| 3867 | data = self._DoReadFile('174_env.dts') |
| 3868 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3869 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3870 | env = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3871 | self.assertEqual(b'\x1b\x97\x22\x7c\x01var1=1\0var2="2"\0\0\xff\xff', |
| 3872 | env) |
| 3873 | |
| 3874 | def testEnvironmentNoSize(self): |
| 3875 | """Test that a missing 'size' property is detected""" |
| 3876 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8cdc08a | 2020-10-26 17:40:00 -0600 | [diff] [blame] | 3877 | self._DoTestFile('175_env_no_size.dts') |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 3878 | self.assertIn("'u-boot-env' entry must have a size property", |
| 3879 | str(e.exception)) |
| 3880 | |
| 3881 | def testEnvironmentTooSmall(self): |
| 3882 | """Test handling of an environment that does not fit""" |
| 3883 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8cdc08a | 2020-10-26 17:40:00 -0600 | [diff] [blame] | 3884 | self._DoTestFile('176_env_too_small.dts') |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 3885 | |
| 3886 | # checksum, start byte, environment with \0 terminator, final \0 |
| 3887 | need = 4 + 1 + len(ENV_DATA) + 1 + 1 |
| 3888 | short = need - 0x8 |
| 3889 | self.assertIn("too small to hold data (need %#x more bytes)" % short, |
| 3890 | str(e.exception)) |
| 3891 | |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 3892 | def testSkipAtStart(self): |
| 3893 | """Test handling of skip-at-start section""" |
| 3894 | data = self._DoReadFile('177_skip_at_start.dts') |
| 3895 | self.assertEqual(U_BOOT_DATA, data) |
| 3896 | |
| 3897 | image = control.images['image'] |
| 3898 | entries = image.GetEntries() |
| 3899 | section = entries['section'] |
| 3900 | self.assertEqual(0, section.offset) |
| 3901 | self.assertEqual(len(U_BOOT_DATA), section.size) |
| 3902 | self.assertEqual(U_BOOT_DATA, section.GetData()) |
| 3903 | |
| 3904 | entry = section.GetEntries()['u-boot'] |
| 3905 | self.assertEqual(16, entry.offset) |
| 3906 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3907 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 3908 | |
| 3909 | def testSkipAtStartPad(self): |
| 3910 | """Test handling of skip-at-start section with padded entry""" |
| 3911 | data = self._DoReadFile('178_skip_at_start_pad.dts') |
| 3912 | before = tools.GetBytes(0, 8) |
| 3913 | after = tools.GetBytes(0, 4) |
| 3914 | all = before + U_BOOT_DATA + after |
| 3915 | self.assertEqual(all, data) |
| 3916 | |
| 3917 | image = control.images['image'] |
| 3918 | entries = image.GetEntries() |
| 3919 | section = entries['section'] |
| 3920 | self.assertEqual(0, section.offset) |
| 3921 | self.assertEqual(len(all), section.size) |
| 3922 | self.assertEqual(all, section.GetData()) |
| 3923 | |
| 3924 | entry = section.GetEntries()['u-boot'] |
| 3925 | self.assertEqual(16, entry.offset) |
| 3926 | self.assertEqual(len(all), entry.size) |
| 3927 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 3928 | |
| 3929 | def testSkipAtStartSectionPad(self): |
| 3930 | """Test handling of skip-at-start section with padding""" |
| 3931 | data = self._DoReadFile('179_skip_at_start_section_pad.dts') |
| 3932 | before = tools.GetBytes(0, 8) |
| 3933 | after = tools.GetBytes(0, 4) |
| 3934 | all = before + U_BOOT_DATA + after |
Simon Glass | 510ef0f | 2020-10-26 17:40:13 -0600 | [diff] [blame] | 3935 | self.assertEqual(all, data) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 3936 | |
| 3937 | image = control.images['image'] |
| 3938 | entries = image.GetEntries() |
| 3939 | section = entries['section'] |
| 3940 | self.assertEqual(0, section.offset) |
| 3941 | self.assertEqual(len(all), section.size) |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 3942 | self.assertEqual(U_BOOT_DATA, section.data) |
Simon Glass | 510ef0f | 2020-10-26 17:40:13 -0600 | [diff] [blame] | 3943 | self.assertEqual(all, section.GetPaddedData()) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 3944 | |
| 3945 | entry = section.GetEntries()['u-boot'] |
| 3946 | self.assertEqual(16, entry.offset) |
| 3947 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3948 | self.assertEqual(U_BOOT_DATA, entry.data) |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 3949 | |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 3950 | def testSectionPad(self): |
| 3951 | """Testing padding with sections""" |
| 3952 | data = self._DoReadFile('180_section_pad.dts') |
| 3953 | expected = (tools.GetBytes(ord('&'), 3) + |
| 3954 | tools.GetBytes(ord('!'), 5) + |
| 3955 | U_BOOT_DATA + |
| 3956 | tools.GetBytes(ord('!'), 1) + |
| 3957 | tools.GetBytes(ord('&'), 2)) |
| 3958 | self.assertEqual(expected, data) |
| 3959 | |
| 3960 | def testSectionAlign(self): |
| 3961 | """Testing alignment with sections""" |
| 3962 | data = self._DoReadFileDtb('181_section_align.dts', map=True)[0] |
| 3963 | expected = (b'\0' + # fill section |
| 3964 | tools.GetBytes(ord('&'), 1) + # padding to section align |
| 3965 | b'\0' + # fill section |
| 3966 | tools.GetBytes(ord('!'), 3) + # padding to u-boot align |
| 3967 | U_BOOT_DATA + |
| 3968 | tools.GetBytes(ord('!'), 4) + # padding to u-boot size |
| 3969 | tools.GetBytes(ord('!'), 4)) # padding to section size |
| 3970 | self.assertEqual(expected, data) |
| 3971 | |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 3972 | def testCompressImage(self): |
| 3973 | """Test compression of the entire image""" |
| 3974 | self._CheckLz4() |
| 3975 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 3976 | '182_compress_image.dts', use_real_dtb=True, update_dtb=True) |
| 3977 | dtb = fdt.Fdt(out_dtb_fname) |
| 3978 | dtb.Scan() |
| 3979 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 3980 | 'uncomp-size']) |
| 3981 | orig = self._decompress(data) |
| 3982 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 3983 | |
| 3984 | # Do a sanity check on various fields |
| 3985 | image = control.images['image'] |
| 3986 | entries = image.GetEntries() |
| 3987 | self.assertEqual(2, len(entries)) |
| 3988 | |
| 3989 | entry = entries['blob'] |
| 3990 | self.assertEqual(COMPRESS_DATA, entry.data) |
| 3991 | self.assertEqual(len(COMPRESS_DATA), entry.size) |
| 3992 | |
| 3993 | entry = entries['u-boot'] |
| 3994 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 3995 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3996 | |
| 3997 | self.assertEqual(len(data), image.size) |
| 3998 | self.assertEqual(COMPRESS_DATA + U_BOOT_DATA, image.uncomp_data) |
| 3999 | self.assertEqual(len(COMPRESS_DATA + U_BOOT_DATA), image.uncomp_size) |
| 4000 | orig = self._decompress(image.data) |
| 4001 | self.assertEqual(orig, image.uncomp_data) |
| 4002 | |
| 4003 | expected = { |
| 4004 | 'blob:offset': 0, |
| 4005 | 'blob:size': len(COMPRESS_DATA), |
| 4006 | 'u-boot:offset': len(COMPRESS_DATA), |
| 4007 | 'u-boot:size': len(U_BOOT_DATA), |
| 4008 | 'uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4009 | 'offset': 0, |
| 4010 | 'image-pos': 0, |
| 4011 | 'size': len(data), |
| 4012 | } |
| 4013 | self.assertEqual(expected, props) |
| 4014 | |
| 4015 | def testCompressImageLess(self): |
| 4016 | """Test compression where compression reduces the image size""" |
| 4017 | self._CheckLz4() |
| 4018 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4019 | '183_compress_image_less.dts', use_real_dtb=True, update_dtb=True) |
| 4020 | dtb = fdt.Fdt(out_dtb_fname) |
| 4021 | dtb.Scan() |
| 4022 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4023 | 'uncomp-size']) |
| 4024 | orig = self._decompress(data) |
| 4025 | |
| 4026 | self.assertEquals(COMPRESS_DATA + COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4027 | |
| 4028 | # Do a sanity check on various fields |
| 4029 | image = control.images['image'] |
| 4030 | entries = image.GetEntries() |
| 4031 | self.assertEqual(2, len(entries)) |
| 4032 | |
| 4033 | entry = entries['blob'] |
| 4034 | self.assertEqual(COMPRESS_DATA_BIG, entry.data) |
| 4035 | self.assertEqual(len(COMPRESS_DATA_BIG), entry.size) |
| 4036 | |
| 4037 | entry = entries['u-boot'] |
| 4038 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4039 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4040 | |
| 4041 | self.assertEqual(len(data), image.size) |
| 4042 | self.assertEqual(COMPRESS_DATA_BIG + U_BOOT_DATA, image.uncomp_data) |
| 4043 | self.assertEqual(len(COMPRESS_DATA_BIG + U_BOOT_DATA), |
| 4044 | image.uncomp_size) |
| 4045 | orig = self._decompress(image.data) |
| 4046 | self.assertEqual(orig, image.uncomp_data) |
| 4047 | |
| 4048 | expected = { |
| 4049 | 'blob:offset': 0, |
| 4050 | 'blob:size': len(COMPRESS_DATA_BIG), |
| 4051 | 'u-boot:offset': len(COMPRESS_DATA_BIG), |
| 4052 | 'u-boot:size': len(U_BOOT_DATA), |
| 4053 | 'uncomp-size': len(COMPRESS_DATA_BIG + U_BOOT_DATA), |
| 4054 | 'offset': 0, |
| 4055 | 'image-pos': 0, |
| 4056 | 'size': len(data), |
| 4057 | } |
| 4058 | self.assertEqual(expected, props) |
| 4059 | |
| 4060 | def testCompressSectionSize(self): |
| 4061 | """Test compression of a section with a fixed size""" |
| 4062 | self._CheckLz4() |
| 4063 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4064 | '184_compress_section_size.dts', use_real_dtb=True, update_dtb=True) |
| 4065 | dtb = fdt.Fdt(out_dtb_fname) |
| 4066 | dtb.Scan() |
| 4067 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4068 | 'uncomp-size']) |
| 4069 | orig = self._decompress(data) |
| 4070 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4071 | expected = { |
| 4072 | 'section/blob:offset': 0, |
| 4073 | 'section/blob:size': len(COMPRESS_DATA), |
| 4074 | 'section/u-boot:offset': len(COMPRESS_DATA), |
| 4075 | 'section/u-boot:size': len(U_BOOT_DATA), |
| 4076 | 'section:offset': 0, |
| 4077 | 'section:image-pos': 0, |
| 4078 | 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4079 | 'section:size': 0x30, |
| 4080 | 'offset': 0, |
| 4081 | 'image-pos': 0, |
| 4082 | 'size': 0x30, |
| 4083 | } |
| 4084 | self.assertEqual(expected, props) |
| 4085 | |
| 4086 | def testCompressSection(self): |
| 4087 | """Test compression of a section with no fixed size""" |
| 4088 | self._CheckLz4() |
| 4089 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4090 | '185_compress_section.dts', use_real_dtb=True, update_dtb=True) |
| 4091 | dtb = fdt.Fdt(out_dtb_fname) |
| 4092 | dtb.Scan() |
| 4093 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4094 | 'uncomp-size']) |
| 4095 | orig = self._decompress(data) |
| 4096 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4097 | expected = { |
| 4098 | 'section/blob:offset': 0, |
| 4099 | 'section/blob:size': len(COMPRESS_DATA), |
| 4100 | 'section/u-boot:offset': len(COMPRESS_DATA), |
| 4101 | 'section/u-boot:size': len(U_BOOT_DATA), |
| 4102 | 'section:offset': 0, |
| 4103 | 'section:image-pos': 0, |
| 4104 | 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4105 | 'section:size': len(data), |
| 4106 | 'offset': 0, |
| 4107 | 'image-pos': 0, |
| 4108 | 'size': len(data), |
| 4109 | } |
| 4110 | self.assertEqual(expected, props) |
| 4111 | |
| 4112 | def testCompressExtra(self): |
| 4113 | """Test compression of a section with no fixed size""" |
| 4114 | self._CheckLz4() |
| 4115 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4116 | '186_compress_extra.dts', use_real_dtb=True, update_dtb=True) |
| 4117 | dtb = fdt.Fdt(out_dtb_fname) |
| 4118 | dtb.Scan() |
| 4119 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4120 | 'uncomp-size']) |
| 4121 | |
| 4122 | base = data[len(U_BOOT_DATA):] |
| 4123 | self.assertEquals(U_BOOT_DATA, base[:len(U_BOOT_DATA)]) |
| 4124 | rest = base[len(U_BOOT_DATA):] |
| 4125 | |
| 4126 | # Check compressed data |
| 4127 | section1 = self._decompress(rest) |
| 4128 | expect1 = tools.Compress(COMPRESS_DATA + U_BOOT_DATA, 'lz4') |
| 4129 | self.assertEquals(expect1, rest[:len(expect1)]) |
| 4130 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, section1) |
| 4131 | rest1 = rest[len(expect1):] |
| 4132 | |
| 4133 | section2 = self._decompress(rest1) |
| 4134 | expect2 = tools.Compress(COMPRESS_DATA + COMPRESS_DATA, 'lz4') |
| 4135 | self.assertEquals(expect2, rest1[:len(expect2)]) |
| 4136 | self.assertEquals(COMPRESS_DATA + COMPRESS_DATA, section2) |
| 4137 | rest2 = rest1[len(expect2):] |
| 4138 | |
| 4139 | expect_size = (len(U_BOOT_DATA) + len(U_BOOT_DATA) + len(expect1) + |
| 4140 | len(expect2) + len(U_BOOT_DATA)) |
| 4141 | #self.assertEquals(expect_size, len(data)) |
| 4142 | |
| 4143 | #self.assertEquals(U_BOOT_DATA, rest2) |
| 4144 | |
| 4145 | self.maxDiff = None |
| 4146 | expected = { |
| 4147 | 'u-boot:offset': 0, |
| 4148 | 'u-boot:image-pos': 0, |
| 4149 | 'u-boot:size': len(U_BOOT_DATA), |
| 4150 | |
| 4151 | 'base:offset': len(U_BOOT_DATA), |
| 4152 | 'base:image-pos': len(U_BOOT_DATA), |
| 4153 | 'base:size': len(data) - len(U_BOOT_DATA), |
| 4154 | 'base/u-boot:offset': 0, |
| 4155 | 'base/u-boot:image-pos': len(U_BOOT_DATA), |
| 4156 | 'base/u-boot:size': len(U_BOOT_DATA), |
| 4157 | 'base/u-boot2:offset': len(U_BOOT_DATA) + len(expect1) + |
| 4158 | len(expect2), |
| 4159 | 'base/u-boot2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1) + |
| 4160 | len(expect2), |
| 4161 | 'base/u-boot2:size': len(U_BOOT_DATA), |
| 4162 | |
| 4163 | 'base/section:offset': len(U_BOOT_DATA), |
| 4164 | 'base/section:image-pos': len(U_BOOT_DATA) * 2, |
| 4165 | 'base/section:size': len(expect1), |
| 4166 | 'base/section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4167 | 'base/section/blob:offset': 0, |
| 4168 | 'base/section/blob:size': len(COMPRESS_DATA), |
| 4169 | 'base/section/u-boot:offset': len(COMPRESS_DATA), |
| 4170 | 'base/section/u-boot:size': len(U_BOOT_DATA), |
| 4171 | |
| 4172 | 'base/section2:offset': len(U_BOOT_DATA) + len(expect1), |
| 4173 | 'base/section2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1), |
| 4174 | 'base/section2:size': len(expect2), |
| 4175 | 'base/section2:uncomp-size': len(COMPRESS_DATA + COMPRESS_DATA), |
| 4176 | 'base/section2/blob:offset': 0, |
| 4177 | 'base/section2/blob:size': len(COMPRESS_DATA), |
| 4178 | 'base/section2/blob2:offset': len(COMPRESS_DATA), |
| 4179 | 'base/section2/blob2:size': len(COMPRESS_DATA), |
| 4180 | |
| 4181 | 'offset': 0, |
| 4182 | 'image-pos': 0, |
| 4183 | 'size': len(data), |
| 4184 | } |
| 4185 | self.assertEqual(expected, props) |
| 4186 | |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 4187 | def testSymbolsSubsection(self): |
| 4188 | """Test binman can assign symbols from a subsection""" |
| 4189 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
| 4190 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 4191 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
| 4192 | self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr) |
| 4193 | |
| 4194 | self._SetupSplElf('u_boot_binman_syms') |
| 4195 | data = self._DoReadFile('187_symbols_sub.dts') |
| 4196 | sym_values = struct.pack('<LQLL', 0x00, 0x1c, 0x28, 0x04) |
| 4197 | expected = (sym_values + U_BOOT_SPL_DATA[20:] + |
| 4198 | tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values + |
| 4199 | U_BOOT_SPL_DATA[20:]) |
| 4200 | self.assertEqual(expected, data) |
| 4201 | |
Simon Glass | 3fb2540 | 2021-01-06 21:35:16 -0700 | [diff] [blame] | 4202 | def testReadImageEntryArg(self): |
| 4203 | """Test reading an image that would need an entry arg to generate""" |
| 4204 | entry_args = { |
| 4205 | 'cros-ec-rw-path': 'ecrw.bin', |
| 4206 | } |
| 4207 | data = self.data = self._DoReadFileDtb( |
| 4208 | '188_image_entryarg.dts',use_real_dtb=True, update_dtb=True, |
| 4209 | entry_args=entry_args) |
| 4210 | |
| 4211 | image_fname = tools.GetOutputFilename('image.bin') |
| 4212 | orig_image = control.images['image'] |
| 4213 | |
| 4214 | # This should not generate an error about the missing 'cros-ec-rw-path' |
| 4215 | # since we are reading the image from a file. Compare with |
| 4216 | # testEntryArgsRequired() |
| 4217 | image = Image.FromFile(image_fname) |
| 4218 | self.assertEqual(orig_image.GetEntries().keys(), |
| 4219 | image.GetEntries().keys()) |
| 4220 | |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 4221 | def testFilesAlign(self): |
| 4222 | """Test alignment with files""" |
| 4223 | data = self._DoReadFile('190_files_align.dts') |
| 4224 | |
| 4225 | # The first string is 15 bytes so will align to 16 |
| 4226 | expect = FILES_DATA[:15] + b'\0' + FILES_DATA[15:] |
| 4227 | self.assertEqual(expect, data) |
| 4228 | |
Simon Glass | db84b56 | 2021-01-06 21:35:19 -0700 | [diff] [blame] | 4229 | def testReadImageSkip(self): |
| 4230 | """Test reading an image and accessing its FDT map""" |
| 4231 | data = self.data = self._DoReadFileRealDtb('191_read_image_skip.dts') |
| 4232 | image_fname = tools.GetOutputFilename('image.bin') |
| 4233 | orig_image = control.images['image'] |
| 4234 | image = Image.FromFile(image_fname) |
| 4235 | self.assertEqual(orig_image.GetEntries().keys(), |
| 4236 | image.GetEntries().keys()) |
| 4237 | |
| 4238 | orig_entry = orig_image.GetEntries()['fdtmap'] |
| 4239 | entry = image.GetEntries()['fdtmap'] |
| 4240 | self.assertEqual(orig_entry.offset, entry.offset) |
| 4241 | self.assertEqual(orig_entry.size, entry.size) |
| 4242 | self.assertEqual(16, entry.image_pos) |
| 4243 | |
| 4244 | u_boot = image.GetEntries()['section'].GetEntries()['u-boot'] |
| 4245 | |
| 4246 | self.assertEquals(U_BOOT_DATA, u_boot.ReadData()) |
| 4247 | |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4248 | |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 4249 | if __name__ == "__main__": |
| 4250 | unittest.main() |