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 |
Simon Glass | 162017b | 2022-01-09 20:13:57 -0700 | [diff] [blame] | 20 | import unittest.mock |
| 21 | import urllib.error |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 22 | |
Simon Glass | 4eae925 | 2022-01-09 20:13:50 -0700 | [diff] [blame] | 23 | from binman import bintool |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 24 | from binman import cbfs_util |
| 25 | from binman import cmdline |
| 26 | from binman import control |
| 27 | from binman import elf |
| 28 | from binman import elf_test |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 29 | from binman import fip_util |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 30 | from binman import fmap_util |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 31 | from binman import state |
| 32 | from dtoc import fdt |
| 33 | from dtoc import fdt_util |
| 34 | from binman.etype import fdtmap |
| 35 | from binman.etype import image_header |
Simon Glass | 90cd6f0 | 2020-08-05 13:27:47 -0600 | [diff] [blame] | 36 | from binman.image import Image |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 37 | from patman import command |
| 38 | from patman import test_util |
| 39 | from patman import tools |
| 40 | from patman import tout |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 41 | |
| 42 | # Contents of test files, corresponding to different entry types |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 43 | U_BOOT_DATA = b'1234' |
| 44 | U_BOOT_IMG_DATA = b'img' |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 45 | U_BOOT_SPL_DATA = b'56780123456789abcdefghijklm' |
| 46 | U_BOOT_TPL_DATA = b'tpl9876543210fedcbazywvuts' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 47 | BLOB_DATA = b'89' |
| 48 | ME_DATA = b'0abcd' |
| 49 | VGA_DATA = b'vga' |
| 50 | U_BOOT_DTB_DATA = b'udtb' |
| 51 | U_BOOT_SPL_DTB_DATA = b'spldtb' |
| 52 | U_BOOT_TPL_DTB_DATA = b'tpldtb' |
| 53 | X86_START16_DATA = b'start16' |
| 54 | X86_START16_SPL_DATA = b'start16spl' |
| 55 | X86_START16_TPL_DATA = b'start16tpl' |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 56 | X86_RESET16_DATA = b'reset16' |
| 57 | X86_RESET16_SPL_DATA = b'reset16spl' |
| 58 | X86_RESET16_TPL_DATA = b'reset16tpl' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 59 | PPC_MPC85XX_BR_DATA = b'ppcmpc85xxbr' |
| 60 | U_BOOT_NODTB_DATA = b'nodtb with microcode pointer somewhere in here' |
| 61 | U_BOOT_SPL_NODTB_DATA = b'splnodtb with microcode pointer somewhere in here' |
| 62 | U_BOOT_TPL_NODTB_DATA = b'tplnodtb with microcode pointer somewhere in here' |
Alper Nebi Yasak | 09fb061 | 2022-02-08 01:08:04 +0300 | [diff] [blame] | 63 | U_BOOT_EXP_DATA = U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA |
| 64 | U_BOOT_SPL_EXP_DATA = U_BOOT_SPL_NODTB_DATA + U_BOOT_SPL_DTB_DATA |
| 65 | U_BOOT_TPL_EXP_DATA = U_BOOT_TPL_NODTB_DATA + U_BOOT_TPL_DTB_DATA |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 66 | FSP_DATA = b'fsp' |
| 67 | CMC_DATA = b'cmc' |
| 68 | VBT_DATA = b'vbt' |
| 69 | MRC_DATA = b'mrc' |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 70 | TEXT_DATA = 'text' |
| 71 | TEXT_DATA2 = 'text2' |
| 72 | TEXT_DATA3 = 'text3' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 73 | CROS_EC_RW_DATA = b'ecrw' |
| 74 | GBB_DATA = b'gbbd' |
| 75 | BMPBLK_DATA = b'bmp' |
| 76 | VBLOCK_DATA = b'vblk' |
| 77 | FILES_DATA = (b"sorry I'm late\nOh, don't bother apologising, I'm " + |
| 78 | b"sorry you're alive\n") |
Simon Glass | ccec026 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 79 | COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data' |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 80 | COMPRESS_DATA_BIG = COMPRESS_DATA * 2 |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 81 | REFCODE_DATA = b'refcode' |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 82 | FSP_M_DATA = b'fsp_m' |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 83 | FSP_S_DATA = b'fsp_s' |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 84 | FSP_T_DATA = b'fsp_t' |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 85 | ATF_BL31_DATA = b'bl31' |
Roger Quadros | 5cdcea0 | 2022-02-19 20:50:04 +0200 | [diff] [blame] | 86 | TEE_OS_DATA = b'this is some tee OS data' |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 87 | ATF_BL2U_DATA = b'bl2u' |
Bin Meng | c0b1574 | 2021-05-10 20:23:33 +0800 | [diff] [blame] | 88 | OPENSBI_DATA = b'opensbi' |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 89 | SCP_DATA = b'scp' |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 90 | TEST_FDT1_DATA = b'fdt1' |
| 91 | TEST_FDT2_DATA = b'test-fdt2' |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 92 | ENV_DATA = b'var1=1\nvar2="2"' |
Philippe Reynes | ebe96cb | 2022-03-28 22:57:04 +0200 | [diff] [blame] | 93 | PRE_LOAD_MAGIC = b'UBSH' |
| 94 | PRE_LOAD_VERSION = 0x11223344.to_bytes(4, 'big') |
| 95 | PRE_LOAD_HDR_SIZE = 0x00001000.to_bytes(4, 'big') |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 96 | |
| 97 | # Subdirectory of the input dir to use to put test FDTs |
| 98 | TEST_FDT_SUBDIR = 'fdts' |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 99 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 100 | # The expected size for the device tree in some tests |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 101 | EXTRACT_DTB_SIZE = 0x3c9 |
| 102 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 103 | # Properties expected to be in the device tree when update_dtb is used |
| 104 | BASE_DTB_PROPS = ['offset', 'size', 'image-pos'] |
| 105 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 106 | # Extra properties expected to be in the device tree when allow-repack is used |
| 107 | REPACK_DTB_PROPS = ['orig-offset', 'orig-size'] |
| 108 | |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 109 | # Supported compression bintools |
| 110 | COMP_BINTOOLS = ['lz4', 'lzma_alone'] |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 111 | |
| 112 | class TestFunctional(unittest.TestCase): |
| 113 | """Functional tests for binman |
| 114 | |
| 115 | Most of these use a sample .dts file to build an image and then check |
| 116 | that it looks correct. The sample files are in the test/ subdirectory |
| 117 | and are numbered. |
| 118 | |
| 119 | For each entry type a very small test file is created using fixed |
| 120 | string contents. This makes it easy to test that things look right, and |
| 121 | debug problems. |
| 122 | |
| 123 | In some cases a 'real' file must be used - these are also supplied in |
| 124 | the test/ diurectory. |
| 125 | """ |
| 126 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 127 | def setUpClass(cls): |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 128 | global entry |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 129 | from binman import entry |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 130 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 131 | # Handle the case where argv[0] is 'python' |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 132 | cls._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 133 | cls._binman_pathname = os.path.join(cls._binman_dir, 'binman') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 134 | |
| 135 | # Create a temporary directory for input files |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 136 | cls._indir = tempfile.mkdtemp(prefix='binmant.') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 137 | |
| 138 | # Create some test files |
| 139 | TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) |
| 140 | TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) |
| 141 | TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 142 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 143 | TestFunctional._MakeInputFile('blobfile', BLOB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 144 | TestFunctional._MakeInputFile('me.bin', ME_DATA) |
| 145 | TestFunctional._MakeInputFile('vga.bin', VGA_DATA) |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 146 | cls._ResetDtbs() |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 147 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 148 | TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 149 | |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 150 | TestFunctional._MakeInputFile('u-boot-x86-start16.bin', X86_START16_DATA) |
| 151 | TestFunctional._MakeInputFile('spl/u-boot-x86-start16-spl.bin', |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 152 | X86_START16_SPL_DATA) |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 153 | TestFunctional._MakeInputFile('tpl/u-boot-x86-start16-tpl.bin', |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 154 | X86_START16_TPL_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 155 | |
| 156 | TestFunctional._MakeInputFile('u-boot-x86-reset16.bin', |
| 157 | X86_RESET16_DATA) |
| 158 | TestFunctional._MakeInputFile('spl/u-boot-x86-reset16-spl.bin', |
| 159 | X86_RESET16_SPL_DATA) |
| 160 | TestFunctional._MakeInputFile('tpl/u-boot-x86-reset16-tpl.bin', |
| 161 | X86_RESET16_TPL_DATA) |
| 162 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 163 | TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 164 | TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin', |
| 165 | U_BOOT_SPL_NODTB_DATA) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 166 | TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin', |
| 167 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 168 | TestFunctional._MakeInputFile('fsp.bin', FSP_DATA) |
| 169 | TestFunctional._MakeInputFile('cmc.bin', CMC_DATA) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 170 | TestFunctional._MakeInputFile('vbt.bin', VBT_DATA) |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 171 | TestFunctional._MakeInputFile('mrc.bin', MRC_DATA) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 172 | TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 173 | TestFunctional._MakeInputDir('devkeys') |
| 174 | TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA) |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 175 | TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 176 | TestFunctional._MakeInputFile('fsp_m.bin', FSP_M_DATA) |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 177 | TestFunctional._MakeInputFile('fsp_s.bin', FSP_S_DATA) |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 178 | TestFunctional._MakeInputFile('fsp_t.bin', FSP_T_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 179 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 180 | cls._elf_testdir = os.path.join(cls._indir, 'elftest') |
| 181 | elf_test.BuildElfTestFiles(cls._elf_testdir) |
| 182 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 183 | # ELF file with a '_dt_ucode_base_size' symbol |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 184 | TestFunctional._MakeInputFile('u-boot', |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 185 | tools.read_file(cls.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 186 | |
| 187 | # Intel flash descriptor file |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 188 | cls._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 189 | |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 190 | shutil.copytree(cls.TestFile('files'), |
| 191 | os.path.join(cls._indir, 'files')) |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 192 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 193 | TestFunctional._MakeInputFile('compress', COMPRESS_DATA) |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 194 | TestFunctional._MakeInputFile('compress_big', COMPRESS_DATA_BIG) |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 195 | TestFunctional._MakeInputFile('bl31.bin', ATF_BL31_DATA) |
Roger Quadros | 5cdcea0 | 2022-02-19 20:50:04 +0200 | [diff] [blame] | 196 | TestFunctional._MakeInputFile('tee-pager.bin', TEE_OS_DATA) |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 197 | TestFunctional._MakeInputFile('bl2u.bin', ATF_BL2U_DATA) |
Bin Meng | c0b1574 | 2021-05-10 20:23:33 +0800 | [diff] [blame] | 198 | TestFunctional._MakeInputFile('fw_dynamic.bin', OPENSBI_DATA) |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 199 | TestFunctional._MakeInputFile('scp.bin', SCP_DATA) |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 200 | |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 201 | # Add a few .dtb files for testing |
| 202 | TestFunctional._MakeInputFile('%s/test-fdt1.dtb' % TEST_FDT_SUBDIR, |
| 203 | TEST_FDT1_DATA) |
| 204 | TestFunctional._MakeInputFile('%s/test-fdt2.dtb' % TEST_FDT_SUBDIR, |
| 205 | TEST_FDT2_DATA) |
| 206 | |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 207 | TestFunctional._MakeInputFile('env.txt', ENV_DATA) |
| 208 | |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 209 | # ELF file with two sections in different parts of memory, used for both |
| 210 | # ATF and OP_TEE |
| 211 | TestFunctional._MakeInputFile('bl31.elf', |
| 212 | tools.read_file(cls.ElfTestFile('elf_sections'))) |
| 213 | TestFunctional._MakeInputFile('tee.elf', |
| 214 | tools.read_file(cls.ElfTestFile('elf_sections'))) |
| 215 | |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 216 | cls.comp_bintools = {} |
| 217 | for name in COMP_BINTOOLS: |
| 218 | cls.comp_bintools[name] = bintool.Bintool.create(name) |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 219 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 220 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 221 | def tearDownClass(cls): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 222 | """Remove the temporary input directory and its contents""" |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 223 | if cls.preserve_indir: |
| 224 | print('Preserving input dir: %s' % cls._indir) |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 225 | else: |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 226 | if cls._indir: |
| 227 | shutil.rmtree(cls._indir) |
| 228 | cls._indir = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 229 | |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 230 | @classmethod |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 231 | def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False, |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 232 | toolpath=None, verbosity=None): |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 233 | """Accept arguments controlling test execution |
| 234 | |
| 235 | Args: |
| 236 | preserve_indir: Preserve the shared input directory used by all |
| 237 | tests in this class. |
| 238 | preserve_outdir: Preserve the output directories used by tests. Each |
| 239 | test has its own, so this is normally only useful when running a |
| 240 | single test. |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 241 | toolpath: ist of paths to use for tools |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 242 | """ |
| 243 | cls.preserve_indir = preserve_indir |
| 244 | cls.preserve_outdirs = preserve_outdirs |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 245 | cls.toolpath = toolpath |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 246 | cls.verbosity = verbosity |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 247 | |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 248 | def _CheckBintool(self, bintool): |
| 249 | if not bintool.is_present(): |
| 250 | self.skipTest('%s not available' % bintool.name) |
| 251 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 252 | def _CheckLz4(self): |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 253 | bintool = self.comp_bintools['lz4'] |
| 254 | self._CheckBintool(bintool) |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 255 | |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 256 | def _CleanupOutputDir(self): |
| 257 | """Remove the temporary output directory""" |
| 258 | if self.preserve_outdirs: |
| 259 | print('Preserving output dir: %s' % tools.outdir) |
| 260 | else: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 261 | tools._finalise_for_test() |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 262 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 263 | def setUp(self): |
| 264 | # Enable this to turn on debugging output |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 265 | # tout.init(tout.DEBUG) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 266 | command.test_result = None |
| 267 | |
| 268 | def tearDown(self): |
| 269 | """Remove the temporary output directory""" |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 270 | self._CleanupOutputDir() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 271 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 272 | def _SetupImageInTmpdir(self): |
| 273 | """Set up the output image in a new temporary directory |
| 274 | |
| 275 | This is used when an image has been generated in the output directory, |
| 276 | but we want to run binman again. This will create a new output |
| 277 | directory and fail to delete the original one. |
| 278 | |
| 279 | This creates a new temporary directory, copies the image to it (with a |
| 280 | new name) and removes the old output directory. |
| 281 | |
| 282 | Returns: |
| 283 | Tuple: |
| 284 | Temporary directory to use |
| 285 | New image filename |
| 286 | """ |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 287 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 288 | tmpdir = tempfile.mkdtemp(prefix='binman.') |
| 289 | updated_fname = os.path.join(tmpdir, 'image-updated.bin') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 290 | tools.write_file(updated_fname, tools.read_file(image_fname)) |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 291 | self._CleanupOutputDir() |
| 292 | return tmpdir, updated_fname |
| 293 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 294 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 295 | def _ResetDtbs(cls): |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 296 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
| 297 | TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA) |
| 298 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA) |
| 299 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 300 | def _RunBinman(self, *args, **kwargs): |
| 301 | """Run binman using the command line |
| 302 | |
| 303 | Args: |
| 304 | Arguments to pass, as a list of strings |
| 305 | kwargs: Arguments to pass to Command.RunPipe() |
| 306 | """ |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 307 | result = command.run_pipe([[self._binman_pathname] + list(args)], |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 308 | capture=True, capture_stderr=True, raise_on_error=False) |
| 309 | if result.return_code and kwargs.get('raise_on_error', True): |
| 310 | raise Exception("Error running '%s': %s" % (' '.join(args), |
| 311 | result.stdout + result.stderr)) |
| 312 | return result |
| 313 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 314 | def _DoBinman(self, *argv): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 315 | """Run binman using directly (in the same process) |
| 316 | |
| 317 | Args: |
| 318 | Arguments to pass, as a list of strings |
| 319 | Returns: |
| 320 | Return value (0 for success) |
| 321 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 322 | argv = list(argv) |
| 323 | args = cmdline.ParseArgs(argv) |
| 324 | args.pager = 'binman-invalid-pager' |
| 325 | args.build_dir = self._indir |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 326 | |
| 327 | # For testing, you can force an increase in verbosity here |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 328 | # args.verbosity = tout.DEBUG |
| 329 | return control.Binman(args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 330 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 331 | def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False, |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 332 | entry_args=None, images=None, use_real_dtb=False, |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 333 | use_expanded=False, verbosity=None, allow_missing=False, |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 334 | allow_fake_blobs=False, extra_indirs=None, threads=None, |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 335 | test_section_timeout=False, update_fdt_in_elf=None, |
| 336 | force_missing_bintools=''): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 337 | """Run binman with a given test file |
| 338 | |
| 339 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 340 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 341 | debug: True to enable debugging output |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 342 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 343 | 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] | 344 | tree before packing it into the image |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 345 | entry_args: Dict of entry args to supply to binman |
| 346 | key: arg name |
| 347 | value: value of that arg |
| 348 | images: List of image names to build |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 349 | use_real_dtb: True to use the test file as the contents of |
| 350 | the u-boot-dtb entry. Normally this is not needed and the |
| 351 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 352 | But in some test we need the real contents. |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 353 | use_expanded: True to use expanded entries where available, e.g. |
| 354 | 'u-boot-expanded' instead of 'u-boot' |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 355 | verbosity: Verbosity level to use (0-3, None=don't set it) |
| 356 | allow_missing: Set the '--allow-missing' flag so that missing |
| 357 | external binaries just produce a warning instead of an error |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 358 | allow_fake_blobs: Set the '--fake-ext-blobs' flag |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 359 | extra_indirs: Extra input directories to add using -I |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 360 | threads: Number of threads to use (None for default, 0 for |
| 361 | single-threaded) |
Simon Glass | 9a79840 | 2021-11-03 21:09:17 -0600 | [diff] [blame] | 362 | test_section_timeout: True to force the first time to timeout, as |
| 363 | used in testThreadTimeout() |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 364 | update_fdt_in_elf: Value to pass with --update-fdt-in-elf=xxx |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 365 | force_missing_tools (str): comma-separated list of bintools to |
| 366 | regard as missing |
Simon Glass | 9a79840 | 2021-11-03 21:09:17 -0600 | [diff] [blame] | 367 | |
| 368 | Returns: |
| 369 | int return code, 0 on success |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 370 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 371 | args = [] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 372 | if debug: |
| 373 | args.append('-D') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 374 | if verbosity is not None: |
| 375 | args.append('-v%d' % verbosity) |
| 376 | elif self.verbosity: |
| 377 | args.append('-v%d' % self.verbosity) |
| 378 | if self.toolpath: |
| 379 | for path in self.toolpath: |
| 380 | args += ['--toolpath', path] |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 381 | if threads is not None: |
| 382 | args.append('-T%d' % threads) |
| 383 | if test_section_timeout: |
| 384 | args.append('--test-section-timeout') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 385 | args += ['build', '-p', '-I', self._indir, '-d', self.TestFile(fname)] |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 386 | if map: |
| 387 | args.append('-m') |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 388 | if update_dtb: |
Simon Glass | 38a411c | 2019-07-08 13:18:47 -0600 | [diff] [blame] | 389 | args.append('-u') |
Simon Glass | 3140201 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 390 | if not use_real_dtb: |
| 391 | args.append('--fake-dtb') |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 392 | if not use_expanded: |
| 393 | args.append('--no-expanded') |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 394 | if entry_args: |
Simon Glass | 5f3645b | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 395 | for arg, value in entry_args.items(): |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 396 | args.append('-a%s=%s' % (arg, value)) |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 397 | if allow_missing: |
| 398 | args.append('-M') |
Heiko Thiery | 6d45136 | 2022-01-06 11:49:41 +0100 | [diff] [blame] | 399 | if allow_fake_blobs: |
| 400 | args.append('--fake-ext-blobs') |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 401 | if force_missing_bintools: |
| 402 | args += ['--force-missing-bintools', force_missing_bintools] |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 403 | if update_fdt_in_elf: |
| 404 | args += ['--update-fdt-in-elf', update_fdt_in_elf] |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 405 | if images: |
| 406 | for image in images: |
| 407 | args += ['-i', image] |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 408 | if extra_indirs: |
| 409 | for indir in extra_indirs: |
| 410 | args += ['-I', indir] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 411 | return self._DoBinman(*args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 412 | |
| 413 | def _SetupDtb(self, fname, outfile='u-boot.dtb'): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 414 | """Set up a new test device-tree file |
| 415 | |
| 416 | The given file is compiled and set up as the device tree to be used |
| 417 | for ths test. |
| 418 | |
| 419 | Args: |
| 420 | fname: Filename of .dts file to read |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 421 | outfile: Output filename for compiled device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 422 | |
| 423 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 424 | Contents of device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 425 | """ |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 426 | tmpdir = tempfile.mkdtemp(prefix='binmant.') |
| 427 | dtb = fdt_util.EnsureCompiled(self.TestFile(fname), tmpdir) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 428 | with open(dtb, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 429 | data = fd.read() |
| 430 | TestFunctional._MakeInputFile(outfile, data) |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 431 | shutil.rmtree(tmpdir) |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 432 | return data |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 433 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 434 | def _GetDtbContentsForSplTpl(self, dtb_data, name): |
| 435 | """Create a version of the main DTB for SPL or SPL |
| 436 | |
| 437 | For testing we don't actually have different versions of the DTB. With |
| 438 | U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests |
| 439 | we don't normally have any unwanted nodes. |
| 440 | |
| 441 | We still want the DTBs for SPL and TPL to be different though, since |
| 442 | otherwise it is confusing to know which one we are looking at. So add |
| 443 | an 'spl' or 'tpl' property to the top-level node. |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 444 | |
| 445 | Args: |
| 446 | dtb_data: dtb data to modify (this should be a value devicetree) |
| 447 | name: Name of a new property to add |
| 448 | |
| 449 | Returns: |
| 450 | New dtb data with the property added |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 451 | """ |
| 452 | dtb = fdt.Fdt.FromData(dtb_data) |
| 453 | dtb.Scan() |
| 454 | dtb.GetNode('/binman').AddZeroProp(name) |
| 455 | dtb.Sync(auto_resize=True) |
| 456 | dtb.Pack() |
| 457 | return dtb.GetContents() |
| 458 | |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 459 | def _DoReadFileDtb(self, fname, use_real_dtb=False, use_expanded=False, |
| 460 | map=False, update_dtb=False, entry_args=None, |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 461 | reset_dtbs=True, extra_indirs=None, threads=None): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 462 | """Run binman and return the resulting image |
| 463 | |
| 464 | This runs binman with a given test file and then reads the resulting |
| 465 | output file. It is a shortcut function since most tests need to do |
| 466 | these steps. |
| 467 | |
| 468 | Raises an assertion failure if binman returns a non-zero exit code. |
| 469 | |
| 470 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 471 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 472 | use_real_dtb: True to use the test file as the contents of |
| 473 | the u-boot-dtb entry. Normally this is not needed and the |
| 474 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 475 | But in some test we need the real contents. |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 476 | use_expanded: True to use expanded entries where available, e.g. |
| 477 | 'u-boot-expanded' instead of 'u-boot' |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 478 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 479 | 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] | 480 | tree before packing it into the image |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 481 | entry_args: Dict of entry args to supply to binman |
| 482 | key: arg name |
| 483 | value: value of that arg |
| 484 | reset_dtbs: With use_real_dtb the test dtb is overwritten by this |
| 485 | function. If reset_dtbs is True, then the original test dtb |
| 486 | is written back before this function finishes |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 487 | extra_indirs: Extra input directories to add using -I |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 488 | threads: Number of threads to use (None for default, 0 for |
| 489 | single-threaded) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 490 | |
| 491 | Returns: |
| 492 | Tuple: |
| 493 | Resulting image contents |
| 494 | Device tree contents |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 495 | Map data showing contents of image (or None if none) |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 496 | Output device tree binary filename ('u-boot.dtb' path) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 497 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 498 | dtb_data = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 499 | # Use the compiled test file as the u-boot-dtb input |
| 500 | if use_real_dtb: |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 501 | dtb_data = self._SetupDtb(fname) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 502 | |
| 503 | # For testing purposes, make a copy of the DT for SPL and TPL. Add |
| 504 | # a node indicating which it is, so aid verification. |
| 505 | for name in ['spl', 'tpl']: |
| 506 | dtb_fname = '%s/u-boot-%s.dtb' % (name, name) |
| 507 | outfile = os.path.join(self._indir, dtb_fname) |
| 508 | TestFunctional._MakeInputFile(dtb_fname, |
| 509 | self._GetDtbContentsForSplTpl(dtb_data, name)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 510 | |
| 511 | try: |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 512 | retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb, |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 513 | entry_args=entry_args, use_real_dtb=use_real_dtb, |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 514 | use_expanded=use_expanded, extra_indirs=extra_indirs, |
| 515 | threads=threads) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 516 | self.assertEqual(0, retcode) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 517 | out_dtb_fname = tools.get_output_filename('u-boot.dtb.out') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 518 | |
| 519 | # Find the (only) image, read it and return its contents |
| 520 | image = control.images['image'] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 521 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 522 | self.assertTrue(os.path.exists(image_fname)) |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 523 | if map: |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 524 | map_fname = tools.get_output_filename('image.map') |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 525 | with open(map_fname) as fd: |
| 526 | map_data = fd.read() |
| 527 | else: |
| 528 | map_data = None |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 529 | with open(image_fname, 'rb') as fd: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 530 | return fd.read(), dtb_data, map_data, out_dtb_fname |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 531 | finally: |
| 532 | # Put the test file back |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 533 | if reset_dtbs and use_real_dtb: |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 534 | self._ResetDtbs() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 535 | |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 536 | def _DoReadFileRealDtb(self, fname): |
| 537 | """Run binman with a real .dtb file and return the resulting data |
| 538 | |
| 539 | Args: |
| 540 | fname: DT source filename to use (e.g. 082_fdt_update_all.dts) |
| 541 | |
| 542 | Returns: |
| 543 | Resulting image contents |
| 544 | """ |
| 545 | return self._DoReadFileDtb(fname, use_real_dtb=True, update_dtb=True)[0] |
| 546 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 547 | def _DoReadFile(self, fname, use_real_dtb=False): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 548 | """Helper function which discards the device-tree binary |
| 549 | |
| 550 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 551 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 552 | use_real_dtb: True to use the test file as the contents of |
| 553 | the u-boot-dtb entry. Normally this is not needed and the |
| 554 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 555 | But in some test we need the real contents. |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 556 | |
| 557 | Returns: |
| 558 | Resulting image contents |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 559 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 560 | return self._DoReadFileDtb(fname, use_real_dtb)[0] |
| 561 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 562 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 563 | def _MakeInputFile(cls, fname, contents): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 564 | """Create a new test input file, creating directories as needed |
| 565 | |
| 566 | Args: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 567 | fname: Filename to create |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 568 | contents: File contents to write in to the file |
| 569 | Returns: |
| 570 | Full pathname of file created |
| 571 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 572 | pathname = os.path.join(cls._indir, fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 573 | dirname = os.path.dirname(pathname) |
| 574 | if dirname and not os.path.exists(dirname): |
| 575 | os.makedirs(dirname) |
| 576 | with open(pathname, 'wb') as fd: |
| 577 | fd.write(contents) |
| 578 | return pathname |
| 579 | |
| 580 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 581 | def _MakeInputDir(cls, dirname): |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 582 | """Create a new test input directory, creating directories as needed |
| 583 | |
| 584 | Args: |
| 585 | dirname: Directory name to create |
| 586 | |
| 587 | Returns: |
| 588 | Full pathname of directory created |
| 589 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 590 | pathname = os.path.join(cls._indir, dirname) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 591 | if not os.path.exists(pathname): |
| 592 | os.makedirs(pathname) |
| 593 | return pathname |
| 594 | |
| 595 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 596 | def _SetupSplElf(cls, src_fname='bss_data'): |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 597 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 598 | |
| 599 | Args: |
| 600 | Filename of ELF file to use as SPL |
| 601 | """ |
Simon Glass | 93a806f | 2019-08-24 07:22:59 -0600 | [diff] [blame] | 602 | TestFunctional._MakeInputFile('spl/u-boot-spl', |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 603 | tools.read_file(cls.ElfTestFile(src_fname))) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 604 | |
| 605 | @classmethod |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 606 | def _SetupTplElf(cls, src_fname='bss_data'): |
| 607 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 608 | |
| 609 | Args: |
| 610 | Filename of ELF file to use as TPL |
| 611 | """ |
| 612 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 613 | tools.read_file(cls.ElfTestFile(src_fname))) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 614 | |
| 615 | @classmethod |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 616 | def _SetupDescriptor(cls): |
| 617 | with open(cls.TestFile('descriptor.bin'), 'rb') as fd: |
| 618 | TestFunctional._MakeInputFile('descriptor.bin', fd.read()) |
| 619 | |
| 620 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 621 | def TestFile(cls, fname): |
| 622 | return os.path.join(cls._binman_dir, 'test', fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 623 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 624 | @classmethod |
| 625 | def ElfTestFile(cls, fname): |
| 626 | return os.path.join(cls._elf_testdir, fname) |
| 627 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 628 | def AssertInList(self, grep_list, target): |
| 629 | """Assert that at least one of a list of things is in a target |
| 630 | |
| 631 | Args: |
| 632 | grep_list: List of strings to check |
| 633 | target: Target string |
| 634 | """ |
| 635 | for grep in grep_list: |
| 636 | if grep in target: |
| 637 | return |
Simon Glass | 848cdb5 | 2019-05-17 22:00:50 -0600 | [diff] [blame] | 638 | self.fail("Error: '%s' not found in '%s'" % (grep_list, target)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 639 | |
| 640 | def CheckNoGaps(self, entries): |
| 641 | """Check that all entries fit together without gaps |
| 642 | |
| 643 | Args: |
| 644 | entries: List of entries to check |
| 645 | """ |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 646 | offset = 0 |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 647 | for entry in entries.values(): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 648 | self.assertEqual(offset, entry.offset) |
| 649 | offset += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 650 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 651 | def GetFdtLen(self, dtb): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 652 | """Get the totalsize field from a device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 653 | |
| 654 | Args: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 655 | dtb: Device-tree binary contents |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 656 | |
| 657 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 658 | Total size of device-tree binary, from the header |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 659 | """ |
| 660 | return struct.unpack('>L', dtb[4:8])[0] |
| 661 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 662 | def _GetPropTree(self, dtb, prop_names, prefix='/binman/'): |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 663 | def AddNode(node, path): |
| 664 | if node.name != '/': |
| 665 | path += '/' + node.name |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 666 | for prop in node.props.values(): |
| 667 | if prop.name in prop_names: |
| 668 | prop_path = path + ':' + prop.name |
| 669 | tree[prop_path[len(prefix):]] = fdt_util.fdt32_to_cpu( |
| 670 | prop.value) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 671 | for subnode in node.subnodes: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 672 | AddNode(subnode, path) |
| 673 | |
| 674 | tree = {} |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 675 | AddNode(dtb.GetRoot(), '') |
| 676 | return tree |
| 677 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 678 | def testRun(self): |
| 679 | """Test a basic run with valid args""" |
| 680 | result = self._RunBinman('-h') |
| 681 | |
| 682 | def testFullHelp(self): |
| 683 | """Test that the full help is displayed with -H""" |
| 684 | result = self._RunBinman('-H') |
Simon Glass | 75ead66 | 2021-03-18 20:25:13 +1300 | [diff] [blame] | 685 | help_file = os.path.join(self._binman_dir, 'README.rst') |
Tom Rini | c3c0b6d | 2018-01-16 15:29:50 -0500 | [diff] [blame] | 686 | # Remove possible extraneous strings |
| 687 | extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n' |
| 688 | gothelp = result.stdout.replace(extra, '') |
| 689 | self.assertEqual(len(gothelp), os.path.getsize(help_file)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 690 | self.assertEqual(0, len(result.stderr)) |
| 691 | self.assertEqual(0, result.return_code) |
| 692 | |
| 693 | def testFullHelpInternal(self): |
| 694 | """Test that the full help is displayed with -H""" |
| 695 | try: |
| 696 | command.test_result = command.CommandResult() |
| 697 | result = self._DoBinman('-H') |
Simon Glass | 75ead66 | 2021-03-18 20:25:13 +1300 | [diff] [blame] | 698 | help_file = os.path.join(self._binman_dir, 'README.rst') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 699 | finally: |
| 700 | command.test_result = None |
| 701 | |
| 702 | def testHelp(self): |
| 703 | """Test that the basic help is displayed with -h""" |
| 704 | result = self._RunBinman('-h') |
| 705 | self.assertTrue(len(result.stdout) > 200) |
| 706 | self.assertEqual(0, len(result.stderr)) |
| 707 | self.assertEqual(0, result.return_code) |
| 708 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 709 | def testBoard(self): |
| 710 | """Test that we can run it with a specific board""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 711 | self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 712 | TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA) |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 713 | result = self._DoBinman('build', '-n', '-b', 'sandbox') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 714 | self.assertEqual(0, result) |
| 715 | |
| 716 | def testNeedBoard(self): |
| 717 | """Test that we get an error when no board ius supplied""" |
| 718 | with self.assertRaises(ValueError) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 719 | result = self._DoBinman('build') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 720 | self.assertIn("Must provide a board to process (use -b <board>)", |
| 721 | str(e.exception)) |
| 722 | |
| 723 | def testMissingDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 724 | """Test that an invalid device-tree file generates an error""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 725 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 726 | self._RunBinman('build', '-d', 'missing_file') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 727 | # We get one error from libfdt, and a different one from fdtget. |
| 728 | self.AssertInList(["Couldn't open blob from 'missing_file'", |
| 729 | 'No such file or directory'], str(e.exception)) |
| 730 | |
| 731 | def testBrokenDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 732 | """Test that an invalid device-tree source file generates an error |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 733 | |
| 734 | Since this is a source file it should be compiled and the error |
| 735 | will come from the device-tree compiler (dtc). |
| 736 | """ |
| 737 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 738 | self._RunBinman('build', '-d', self.TestFile('001_invalid.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 739 | self.assertIn("FATAL ERROR: Unable to parse input tree", |
| 740 | str(e.exception)) |
| 741 | |
| 742 | def testMissingNode(self): |
| 743 | """Test that a device tree without a 'binman' node generates an error""" |
| 744 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 745 | self._DoBinman('build', '-d', self.TestFile('002_missing_node.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 746 | self.assertIn("does not have a 'binman' node", str(e.exception)) |
| 747 | |
| 748 | def testEmpty(self): |
| 749 | """Test that an empty binman node works OK (i.e. does nothing)""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 750 | result = self._RunBinman('build', '-d', self.TestFile('003_empty.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 751 | self.assertEqual(0, len(result.stderr)) |
| 752 | self.assertEqual(0, result.return_code) |
| 753 | |
| 754 | def testInvalidEntry(self): |
| 755 | """Test that an invalid entry is flagged""" |
| 756 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 757 | result = self._RunBinman('build', '-d', |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 758 | self.TestFile('004_invalid_entry.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 759 | self.assertIn("Unknown entry type 'not-a-valid-type' in node " |
| 760 | "'/binman/not-a-valid-type'", str(e.exception)) |
| 761 | |
| 762 | def testSimple(self): |
| 763 | """Test a simple binman with a single file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 764 | data = self._DoReadFile('005_simple.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 765 | self.assertEqual(U_BOOT_DATA, data) |
| 766 | |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 767 | def testSimpleDebug(self): |
| 768 | """Test a simple binman run with debugging enabled""" |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 769 | self._DoTestFile('005_simple.dts', debug=True) |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 770 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 771 | def testDual(self): |
| 772 | """Test that we can handle creating two images |
| 773 | |
| 774 | This also tests image padding. |
| 775 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 776 | retcode = self._DoTestFile('006_dual_image.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 777 | self.assertEqual(0, retcode) |
| 778 | |
| 779 | image = control.images['image1'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 780 | self.assertEqual(len(U_BOOT_DATA), image.size) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 781 | fname = tools.get_output_filename('image1.bin') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 782 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 783 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 784 | data = fd.read() |
| 785 | self.assertEqual(U_BOOT_DATA, data) |
| 786 | |
| 787 | image = control.images['image2'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 788 | self.assertEqual(3 + len(U_BOOT_DATA) + 5, image.size) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 789 | fname = tools.get_output_filename('image2.bin') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 790 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 791 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 792 | data = fd.read() |
| 793 | self.assertEqual(U_BOOT_DATA, data[3:7]) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 794 | self.assertEqual(tools.get_bytes(0, 3), data[:3]) |
| 795 | self.assertEqual(tools.get_bytes(0, 5), data[7:]) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 796 | |
| 797 | def testBadAlign(self): |
| 798 | """Test that an invalid alignment value is detected""" |
| 799 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 800 | self._DoTestFile('007_bad_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 801 | self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " |
| 802 | "of two", str(e.exception)) |
| 803 | |
| 804 | def testPackSimple(self): |
| 805 | """Test that packing works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 806 | retcode = self._DoTestFile('008_pack.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 807 | self.assertEqual(0, retcode) |
| 808 | self.assertIn('image', control.images) |
| 809 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 810 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 811 | self.assertEqual(5, len(entries)) |
| 812 | |
| 813 | # First u-boot |
| 814 | self.assertIn('u-boot', entries) |
| 815 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 816 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 817 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 818 | |
| 819 | # Second u-boot, aligned to 16-byte boundary |
| 820 | self.assertIn('u-boot-align', entries) |
| 821 | entry = entries['u-boot-align'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 822 | self.assertEqual(16, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 823 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 824 | |
| 825 | # Third u-boot, size 23 bytes |
| 826 | self.assertIn('u-boot-size', entries) |
| 827 | entry = entries['u-boot-size'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 828 | self.assertEqual(20, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 829 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 830 | self.assertEqual(23, entry.size) |
| 831 | |
| 832 | # Fourth u-boot, placed immediate after the above |
| 833 | self.assertIn('u-boot-next', entries) |
| 834 | entry = entries['u-boot-next'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 835 | self.assertEqual(43, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 836 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 837 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 838 | # Fifth u-boot, placed at a fixed offset |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 839 | self.assertIn('u-boot-fixed', entries) |
| 840 | entry = entries['u-boot-fixed'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 841 | self.assertEqual(61, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 842 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 843 | |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 844 | self.assertEqual(65, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 845 | |
| 846 | def testPackExtra(self): |
| 847 | """Test that extra packing feature works as expected""" |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 848 | data, _, _, out_dtb_fname = self._DoReadFileDtb('009_pack_extra.dts', |
| 849 | update_dtb=True) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 850 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 851 | self.assertIn('image', control.images) |
| 852 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 853 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 854 | self.assertEqual(5, len(entries)) |
| 855 | |
| 856 | # First u-boot with padding before and after |
| 857 | self.assertIn('u-boot', entries) |
| 858 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 859 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 860 | self.assertEqual(3, entry.pad_before) |
| 861 | self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 862 | self.assertEqual(U_BOOT_DATA, entry.data) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 863 | self.assertEqual(tools.get_bytes(0, 3) + U_BOOT_DATA + |
| 864 | tools.get_bytes(0, 5), data[:entry.size]) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 865 | pos = entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 866 | |
| 867 | # Second u-boot has an aligned size, but it has no effect |
| 868 | self.assertIn('u-boot-align-size-nop', entries) |
| 869 | entry = entries['u-boot-align-size-nop'] |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 870 | self.assertEqual(pos, entry.offset) |
| 871 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 872 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 873 | self.assertEqual(U_BOOT_DATA, data[pos:pos + entry.size]) |
| 874 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 875 | |
| 876 | # Third u-boot has an aligned size too |
| 877 | self.assertIn('u-boot-align-size', entries) |
| 878 | entry = entries['u-boot-align-size'] |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 879 | self.assertEqual(pos, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 880 | self.assertEqual(32, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 881 | self.assertEqual(U_BOOT_DATA, entry.data) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 882 | self.assertEqual(U_BOOT_DATA + tools.get_bytes(0, 32 - len(U_BOOT_DATA)), |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 883 | data[pos:pos + entry.size]) |
| 884 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 885 | |
| 886 | # Fourth u-boot has an aligned end |
| 887 | self.assertIn('u-boot-align-end', entries) |
| 888 | entry = entries['u-boot-align-end'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 889 | self.assertEqual(48, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 890 | self.assertEqual(16, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 891 | self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)]) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 892 | self.assertEqual(U_BOOT_DATA + tools.get_bytes(0, 16 - len(U_BOOT_DATA)), |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 893 | data[pos:pos + entry.size]) |
| 894 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 895 | |
| 896 | # Fifth u-boot immediately afterwards |
| 897 | self.assertIn('u-boot-align-both', entries) |
| 898 | entry = entries['u-boot-align-both'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 899 | self.assertEqual(64, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 900 | self.assertEqual(64, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 901 | self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)]) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 902 | self.assertEqual(U_BOOT_DATA + tools.get_bytes(0, 64 - len(U_BOOT_DATA)), |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 903 | data[pos:pos + entry.size]) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 904 | |
| 905 | self.CheckNoGaps(entries) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 906 | self.assertEqual(128, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 907 | |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 908 | dtb = fdt.Fdt(out_dtb_fname) |
| 909 | dtb.Scan() |
| 910 | props = self._GetPropTree(dtb, ['size', 'offset', 'image-pos']) |
| 911 | expected = { |
| 912 | 'image-pos': 0, |
| 913 | 'offset': 0, |
| 914 | 'size': 128, |
| 915 | |
| 916 | 'u-boot:image-pos': 0, |
| 917 | 'u-boot:offset': 0, |
| 918 | 'u-boot:size': 3 + 5 + len(U_BOOT_DATA), |
| 919 | |
| 920 | 'u-boot-align-size-nop:image-pos': 12, |
| 921 | 'u-boot-align-size-nop:offset': 12, |
| 922 | 'u-boot-align-size-nop:size': 4, |
| 923 | |
| 924 | 'u-boot-align-size:image-pos': 16, |
| 925 | 'u-boot-align-size:offset': 16, |
| 926 | 'u-boot-align-size:size': 32, |
| 927 | |
| 928 | 'u-boot-align-end:image-pos': 48, |
| 929 | 'u-boot-align-end:offset': 48, |
| 930 | 'u-boot-align-end:size': 16, |
| 931 | |
| 932 | 'u-boot-align-both:image-pos': 64, |
| 933 | 'u-boot-align-both:offset': 64, |
| 934 | 'u-boot-align-both:size': 64, |
| 935 | } |
| 936 | self.assertEqual(expected, props) |
| 937 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 938 | def testPackAlignPowerOf2(self): |
| 939 | """Test that invalid entry alignment is detected""" |
| 940 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 941 | self._DoTestFile('010_pack_align_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 942 | self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " |
| 943 | "of two", str(e.exception)) |
| 944 | |
| 945 | def testPackAlignSizePowerOf2(self): |
| 946 | """Test that invalid entry size alignment is detected""" |
| 947 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 948 | self._DoTestFile('011_pack_align_size_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 949 | self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " |
| 950 | "power of two", str(e.exception)) |
| 951 | |
| 952 | def testPackInvalidAlign(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 953 | """Test detection of an offset that does not match its alignment""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 954 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 955 | self._DoTestFile('012_pack_inv_align.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 956 | self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 957 | "align 0x4 (4)", str(e.exception)) |
| 958 | |
| 959 | def testPackInvalidSizeAlign(self): |
| 960 | """Test that invalid entry size alignment is detected""" |
| 961 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 962 | self._DoTestFile('013_pack_inv_size_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 963 | self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " |
| 964 | "align-size 0x4 (4)", str(e.exception)) |
| 965 | |
| 966 | def testPackOverlap(self): |
| 967 | """Test that overlapping regions are detected""" |
| 968 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 969 | self._DoTestFile('014_pack_overlap.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 970 | self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 971 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 972 | str(e.exception)) |
| 973 | |
| 974 | def testPackEntryOverflow(self): |
| 975 | """Test that entries that overflow their size are detected""" |
| 976 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 977 | self._DoTestFile('015_pack_overflow.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 978 | self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " |
| 979 | "but entry size is 0x3 (3)", str(e.exception)) |
| 980 | |
| 981 | def testPackImageOverflow(self): |
| 982 | """Test that entries which overflow the image size are detected""" |
| 983 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 984 | self._DoTestFile('016_pack_image_overflow.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 985 | self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 986 | "size 0x3 (3)", str(e.exception)) |
| 987 | |
| 988 | def testPackImageSize(self): |
| 989 | """Test that the image size can be set""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 990 | retcode = self._DoTestFile('017_pack_image_size.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 991 | self.assertEqual(0, retcode) |
| 992 | self.assertIn('image', control.images) |
| 993 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 994 | self.assertEqual(7, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 995 | |
| 996 | def testPackImageSizeAlign(self): |
| 997 | """Test that image size alignemnt works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 998 | retcode = self._DoTestFile('018_pack_image_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 999 | self.assertEqual(0, retcode) |
| 1000 | self.assertIn('image', control.images) |
| 1001 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1002 | self.assertEqual(16, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1003 | |
| 1004 | def testPackInvalidImageAlign(self): |
| 1005 | """Test that invalid image alignment is detected""" |
| 1006 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1007 | self._DoTestFile('019_pack_inv_image_align.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 1008 | self.assertIn("Section '/binman': Size 0x7 (7) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1009 | "align-size 0x8 (8)", str(e.exception)) |
| 1010 | |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 1011 | def testPackAlignPowerOf2Inv(self): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1012 | """Test that invalid image alignment is detected""" |
| 1013 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1014 | self._DoTestFile('020_pack_inv_image_align_power2.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1015 | self.assertIn("Image '/binman': Alignment size 131 must be a power of " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1016 | "two", str(e.exception)) |
| 1017 | |
| 1018 | def testImagePadByte(self): |
| 1019 | """Test that the image pad byte can be specified""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1020 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1021 | data = self._DoReadFile('021_image_pad.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1022 | self.assertEqual(U_BOOT_SPL_DATA + tools.get_bytes(0xff, 1) + |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1023 | U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1024 | |
| 1025 | def testImageName(self): |
| 1026 | """Test that image files can be named""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1027 | retcode = self._DoTestFile('022_image_name.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1028 | self.assertEqual(0, retcode) |
| 1029 | image = control.images['image1'] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1030 | fname = tools.get_output_filename('test-name') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1031 | self.assertTrue(os.path.exists(fname)) |
| 1032 | |
| 1033 | image = control.images['image2'] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1034 | fname = tools.get_output_filename('test-name.xx') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1035 | self.assertTrue(os.path.exists(fname)) |
| 1036 | |
| 1037 | def testBlobFilename(self): |
| 1038 | """Test that generic blobs can be provided by filename""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1039 | data = self._DoReadFile('023_blob.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1040 | self.assertEqual(BLOB_DATA, data) |
| 1041 | |
| 1042 | def testPackSorted(self): |
| 1043 | """Test that entries can be sorted""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1044 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1045 | data = self._DoReadFile('024_sorted.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1046 | self.assertEqual(tools.get_bytes(0, 1) + U_BOOT_SPL_DATA + |
| 1047 | tools.get_bytes(0, 2) + U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1048 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1049 | def testPackZeroOffset(self): |
| 1050 | """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] | 1051 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1052 | self._DoTestFile('025_pack_zero_size.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1053 | self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1054 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 1055 | str(e.exception)) |
| 1056 | |
| 1057 | def testPackUbootDtb(self): |
| 1058 | """Test that a device tree can be added to U-Boot""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1059 | data = self._DoReadFile('026_pack_u_boot_dtb.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1060 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1061 | |
| 1062 | def testPackX86RomNoSize(self): |
| 1063 | """Test that the end-at-4gb property requires a size property""" |
| 1064 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1065 | self._DoTestFile('027_pack_4gb_no_size.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1066 | self.assertIn("Image '/binman': Section size must be provided when " |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1067 | "using end-at-4gb", str(e.exception)) |
| 1068 | |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 1069 | def test4gbAndSkipAtStartTogether(self): |
| 1070 | """Test that the end-at-4gb and skip-at-size property can't be used |
| 1071 | together""" |
| 1072 | with self.assertRaises(ValueError) as e: |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 1073 | self._DoTestFile('098_4gb_and_skip_at_start_together.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1074 | self.assertIn("Image '/binman': Provide either 'end-at-4gb' or " |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 1075 | "'skip-at-start'", str(e.exception)) |
| 1076 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1077 | def testPackX86RomOutside(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1078 | """Test that the end-at-4gb property checks for offset boundaries""" |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1079 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1080 | self._DoTestFile('028_pack_4gb_outside.dts') |
Simon Glass | d617986 | 2020-10-26 17:40:05 -0600 | [diff] [blame] | 1081 | self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) size 0x4 (4) " |
| 1082 | "is outside the section '/binman' starting at " |
| 1083 | '0xffffffe0 (4294967264) of size 0x20 (32)', |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1084 | str(e.exception)) |
| 1085 | |
| 1086 | def testPackX86Rom(self): |
| 1087 | """Test that a basic x86 ROM can be created""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1088 | self._SetupSplElf() |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1089 | data = self._DoReadFile('029_x86_rom.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1090 | self.assertEqual(U_BOOT_DATA + tools.get_bytes(0, 3) + U_BOOT_SPL_DATA + |
| 1091 | tools.get_bytes(0, 2), data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1092 | |
| 1093 | def testPackX86RomMeNoDesc(self): |
| 1094 | """Test that an invalid Intel descriptor entry is detected""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1095 | try: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 1096 | TestFunctional._MakeInputFile('descriptor-empty.bin', b'') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1097 | with self.assertRaises(ValueError) as e: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 1098 | self._DoTestFile('163_x86_rom_me_empty.dts') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1099 | self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature", |
| 1100 | str(e.exception)) |
| 1101 | finally: |
| 1102 | self._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1103 | |
| 1104 | def testPackX86RomBadDesc(self): |
| 1105 | """Test that the Intel requires a descriptor entry""" |
| 1106 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1107 | self._DoTestFile('030_x86_rom_me_no_desc.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1108 | self.assertIn("Node '/binman/intel-me': No offset set with " |
| 1109 | "offset-unset: should another entry provide this correct " |
| 1110 | "offset?", str(e.exception)) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1111 | |
| 1112 | def testPackX86RomMe(self): |
| 1113 | """Test that an x86 ROM with an ME region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1114 | data = self._DoReadFile('031_x86_rom_me.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1115 | expected_desc = tools.read_file(self.TestFile('descriptor.bin')) |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 1116 | if data[:0x1000] != expected_desc: |
| 1117 | self.fail('Expected descriptor binary at start of image') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1118 | self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) |
| 1119 | |
| 1120 | def testPackVga(self): |
| 1121 | """Test that an image with a VGA binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1122 | data = self._DoReadFile('032_intel_vga.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1123 | self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) |
| 1124 | |
| 1125 | def testPackStart16(self): |
| 1126 | """Test that an image with an x86 start16 region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1127 | data = self._DoReadFile('033_x86_start16.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1128 | self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) |
| 1129 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 1130 | def testPackPowerpcMpc85xxBootpgResetvec(self): |
| 1131 | """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be |
| 1132 | created""" |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 1133 | data = self._DoReadFile('150_powerpc_mpc85xx_bootpg_resetvec.dts') |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 1134 | self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)]) |
| 1135 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1136 | def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1137 | """Handle running a test for insertion of microcode |
| 1138 | |
| 1139 | Args: |
| 1140 | dts_fname: Name of test .dts file |
| 1141 | nodtb_data: Data that we expect in the first section |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1142 | ucode_second: True if the microsecond entry is second instead of |
| 1143 | third |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1144 | |
| 1145 | Returns: |
| 1146 | Tuple: |
| 1147 | Contents of first region (U-Boot or SPL) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1148 | Offset and size components of microcode pointer, as inserted |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1149 | in the above (two 4-byte words) |
| 1150 | """ |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1151 | data = self._DoReadFile(dts_fname, True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1152 | |
| 1153 | # Now check the device tree has no microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1154 | if ucode_second: |
| 1155 | ucode_content = data[len(nodtb_data):] |
| 1156 | ucode_pos = len(nodtb_data) |
| 1157 | dtb_with_ucode = ucode_content[16:] |
| 1158 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1159 | else: |
| 1160 | dtb_with_ucode = data[len(nodtb_data):] |
| 1161 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1162 | ucode_content = dtb_with_ucode[fdt_len:] |
| 1163 | ucode_pos = len(nodtb_data) + fdt_len |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1164 | fname = tools.get_output_filename('test.dtb') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1165 | with open(fname, 'wb') as fd: |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1166 | fd.write(dtb_with_ucode) |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 1167 | dtb = fdt.FdtScan(fname) |
| 1168 | ucode = dtb.GetNode('/microcode') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1169 | self.assertTrue(ucode) |
| 1170 | for node in ucode.subnodes: |
| 1171 | self.assertFalse(node.props.get('data')) |
| 1172 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1173 | # Check that the microcode appears immediately after the Fdt |
| 1174 | # This matches the concatenation of the data properties in |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1175 | # the /microcode/update@xxx nodes in 34_x86_ucode.dts. |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1176 | ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, |
| 1177 | 0x78235609) |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1178 | self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1179 | |
| 1180 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1181 | # expected offset and size |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1182 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1183 | len(ucode_data)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1184 | u_boot = data[:len(nodtb_data)] |
| 1185 | return u_boot, pos_and_size |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1186 | |
| 1187 | def testPackUbootMicrocode(self): |
| 1188 | """Test that x86 microcode can be handled correctly |
| 1189 | |
| 1190 | We expect to see the following in the image, in order: |
| 1191 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1192 | place |
| 1193 | u-boot.dtb with the microcode removed |
| 1194 | the microcode |
| 1195 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1196 | first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts', |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1197 | U_BOOT_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1198 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1199 | b' somewhere in here', first) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1200 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1201 | def _RunPackUbootSingleMicrocode(self): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1202 | """Test that x86 microcode can be handled correctly |
| 1203 | |
| 1204 | We expect to see the following in the image, in order: |
| 1205 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1206 | place |
| 1207 | u-boot.dtb with the microcode |
| 1208 | an empty microcode region |
| 1209 | """ |
| 1210 | # We need the libfdt library to run this test since only that allows |
| 1211 | # finding the offset of a property. This is required by |
| 1212 | # Entry_u_boot_dtb_with_ucode.ObtainContents(). |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1213 | data = self._DoReadFile('035_x86_single_ucode.dts', True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1214 | |
| 1215 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1216 | |
| 1217 | fdt_len = self.GetFdtLen(second) |
| 1218 | third = second[fdt_len:] |
| 1219 | second = second[:fdt_len] |
| 1220 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1221 | ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) |
| 1222 | self.assertIn(ucode_data, second) |
| 1223 | ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1224 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1225 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1226 | # expected offset and size |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1227 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1228 | len(ucode_data)) |
| 1229 | first = data[:len(U_BOOT_NODTB_DATA)] |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1230 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1231 | b' somewhere in here', first) |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1232 | |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1233 | def testPackUbootSingleMicrocode(self): |
| 1234 | """Test that x86 microcode can be handled correctly with fdt_normal. |
| 1235 | """ |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1236 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1237 | |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1238 | def testUBootImg(self): |
| 1239 | """Test that u-boot.img can be put in a file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1240 | data = self._DoReadFile('036_u_boot_img.dts') |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1241 | self.assertEqual(U_BOOT_IMG_DATA, data) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1242 | |
| 1243 | def testNoMicrocode(self): |
| 1244 | """Test that a missing microcode region is detected""" |
| 1245 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1246 | self._DoReadFile('037_x86_no_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1247 | self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode " |
| 1248 | "node found in ", str(e.exception)) |
| 1249 | |
| 1250 | def testMicrocodeWithoutNode(self): |
| 1251 | """Test that a missing u-boot-dtb-with-ucode node is detected""" |
| 1252 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1253 | self._DoReadFile('038_x86_ucode_missing_node.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1254 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1255 | "microcode region u-boot-dtb-with-ucode", str(e.exception)) |
| 1256 | |
| 1257 | def testMicrocodeWithoutNode2(self): |
| 1258 | """Test that a missing u-boot-ucode node is detected""" |
| 1259 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1260 | self._DoReadFile('039_x86_ucode_missing_node2.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1261 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1262 | "microcode region u-boot-ucode", str(e.exception)) |
| 1263 | |
| 1264 | def testMicrocodeWithoutPtrInElf(self): |
| 1265 | """Test that a U-Boot binary without the microcode symbol is detected""" |
| 1266 | # ELF file without a '_dt_ucode_base_size' symbol |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1267 | try: |
Simon Glass | faaaa16 | 2019-08-24 07:22:55 -0600 | [diff] [blame] | 1268 | TestFunctional._MakeInputFile('u-boot', |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1269 | tools.read_file(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1270 | |
| 1271 | with self.assertRaises(ValueError) as e: |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1272 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1273 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate " |
| 1274 | "_dt_ucode_base_size symbol in u-boot", str(e.exception)) |
| 1275 | |
| 1276 | finally: |
| 1277 | # Put the original file back |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 1278 | TestFunctional._MakeInputFile('u-boot', |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1279 | tools.read_file(self.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1280 | |
| 1281 | def testMicrocodeNotInImage(self): |
| 1282 | """Test that microcode must be placed within the image""" |
| 1283 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1284 | self._DoReadFile('040_x86_ucode_not_in_image.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1285 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode " |
| 1286 | "pointer _dt_ucode_base_size at fffffe14 is outside the " |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 1287 | "section ranging from 00000000 to 0000002e", str(e.exception)) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1288 | |
| 1289 | def testWithoutMicrocode(self): |
| 1290 | """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] | 1291 | TestFunctional._MakeInputFile('u-boot', |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1292 | tools.read_file(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1293 | data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1294 | |
| 1295 | # Now check the device tree has no microcode |
| 1296 | self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)]) |
| 1297 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1298 | |
| 1299 | fdt_len = self.GetFdtLen(second) |
| 1300 | self.assertEqual(dtb, second[:fdt_len]) |
| 1301 | |
| 1302 | used_len = len(U_BOOT_NODTB_DATA) + fdt_len |
| 1303 | third = data[used_len:] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1304 | self.assertEqual(tools.get_bytes(0, 0x200 - used_len), third) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1305 | |
| 1306 | def testUnknownPosSize(self): |
| 1307 | """Test that microcode must be placed within the image""" |
| 1308 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1309 | self._DoReadFile('041_unknown_pos_size.dts', True) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1310 | self.assertIn("Section '/binman': Unable to set offset/size for unknown " |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1311 | "entry 'invalid-entry'", str(e.exception)) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1312 | |
| 1313 | def testPackFsp(self): |
| 1314 | """Test that an image with a FSP binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1315 | data = self._DoReadFile('042_intel_fsp.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1316 | self.assertEqual(FSP_DATA, data[:len(FSP_DATA)]) |
| 1317 | |
| 1318 | def testPackCmc(self): |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1319 | """Test that an image with a CMC binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1320 | data = self._DoReadFile('043_intel_cmc.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1321 | self.assertEqual(CMC_DATA, data[:len(CMC_DATA)]) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1322 | |
| 1323 | def testPackVbt(self): |
| 1324 | """Test that an image with a VBT binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1325 | data = self._DoReadFile('046_intel_vbt.dts') |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1326 | self.assertEqual(VBT_DATA, data[:len(VBT_DATA)]) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1327 | |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1328 | def testSplBssPad(self): |
| 1329 | """Test that we can pad SPL's BSS with zeros""" |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1330 | # ELF file with a '__bss_size' symbol |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1331 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1332 | data = self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1333 | self.assertEqual(U_BOOT_SPL_DATA + tools.get_bytes(0, 10) + U_BOOT_DATA, |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1334 | data) |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1335 | |
Simon Glass | 04cda03 | 2018-10-01 21:12:42 -0600 | [diff] [blame] | 1336 | def testSplBssPadMissing(self): |
| 1337 | """Test that a missing symbol is detected""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1338 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1339 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1340 | self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1341 | self.assertIn('Expected __bss_size symbol in spl/u-boot-spl', |
| 1342 | str(e.exception)) |
| 1343 | |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1344 | def testPackStart16Spl(self): |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1345 | """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] | 1346 | data = self._DoReadFile('048_x86_start16_spl.dts') |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1347 | self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) |
| 1348 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1349 | def _PackUbootSplMicrocode(self, dts, ucode_second=False): |
| 1350 | """Helper function for microcode tests |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1351 | |
| 1352 | We expect to see the following in the image, in order: |
| 1353 | u-boot-spl-nodtb.bin with a microcode pointer inserted at the |
| 1354 | correct place |
| 1355 | u-boot.dtb with the microcode removed |
| 1356 | the microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1357 | |
| 1358 | Args: |
| 1359 | dts: Device tree file to use for test |
| 1360 | ucode_second: True if the microsecond entry is second instead of |
| 1361 | third |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1362 | """ |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1363 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1364 | first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, |
| 1365 | ucode_second=ucode_second) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1366 | self.assertEqual(b'splnodtb with microc' + pos_and_size + |
| 1367 | b'ter somewhere in here', first) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1368 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1369 | def testPackUbootSplMicrocode(self): |
| 1370 | """Test that x86 microcode can be handled correctly in SPL""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1371 | self._PackUbootSplMicrocode('049_x86_ucode_spl.dts') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1372 | |
| 1373 | def testPackUbootSplMicrocodeReorder(self): |
| 1374 | """Test that order doesn't matter for microcode entries |
| 1375 | |
| 1376 | This is the same as testPackUbootSplMicrocode but when we process the |
| 1377 | u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode |
| 1378 | entry, so we reply on binman to try later. |
| 1379 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1380 | self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts', |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1381 | ucode_second=True) |
| 1382 | |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1383 | def testPackMrc(self): |
| 1384 | """Test that an image with an MRC binary can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1385 | data = self._DoReadFile('050_intel_mrc.dts') |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1386 | self.assertEqual(MRC_DATA, data[:len(MRC_DATA)]) |
| 1387 | |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1388 | def testSplDtb(self): |
| 1389 | """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] | 1390 | data = self._DoReadFile('051_u_boot_spl_dtb.dts') |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1391 | self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)]) |
| 1392 | |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1393 | def testSplNoDtb(self): |
| 1394 | """Test that an image with spl/u-boot-spl-nodtb.bin can be created""" |
Simon Glass | 13089cc | 2021-04-25 08:39:32 +1200 | [diff] [blame] | 1395 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1396 | data = self._DoReadFile('052_u_boot_spl_nodtb.dts') |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1397 | self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)]) |
| 1398 | |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 1399 | def checkSymbols(self, dts, base_data, u_boot_offset, entry_args=None, |
| 1400 | use_expanded=False): |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1401 | """Check the image contains the expected symbol values |
| 1402 | |
| 1403 | Args: |
| 1404 | dts: Device tree file to use for test |
| 1405 | base_data: Data before and after 'u-boot' section |
| 1406 | u_boot_offset: Offset of 'u-boot' section in image |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 1407 | entry_args: Dict of entry args to supply to binman |
| 1408 | key: arg name |
| 1409 | value: value of that arg |
| 1410 | use_expanded: True to use expanded entries where available, e.g. |
| 1411 | 'u-boot-expanded' instead of 'u-boot' |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1412 | """ |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 1413 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1414 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 1415 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 1416 | self.assertEqual(syms['_binman_sym_magic'].address, addr) |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1417 | self.assertEqual(syms['_binman_u_boot_spl_any_prop_offset'].address, |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 1418 | addr + 4) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1419 | |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1420 | self._SetupSplElf('u_boot_binman_syms') |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 1421 | data = self._DoReadFileDtb(dts, entry_args=entry_args, |
| 1422 | use_expanded=use_expanded)[0] |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1423 | # The image should contain the symbols from u_boot_binman_syms.c |
| 1424 | # Note that image_pos is adjusted by the base address of the image, |
| 1425 | # which is 0x10 in our test image |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 1426 | sym_values = struct.pack('<LLQLL', elf.BINMAN_SYM_MAGIC_VALUE, |
| 1427 | 0x00, u_boot_offset + len(U_BOOT_DATA), |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1428 | 0x10 + u_boot_offset, 0x04) |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 1429 | expected = (sym_values + base_data[24:] + |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1430 | tools.get_bytes(0xff, 1) + U_BOOT_DATA + sym_values + |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 1431 | base_data[24:]) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1432 | self.assertEqual(expected, data) |
| 1433 | |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1434 | def testSymbols(self): |
| 1435 | """Test binman can assign symbols embedded in U-Boot""" |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 1436 | self.checkSymbols('053_symbols.dts', U_BOOT_SPL_DATA, 0x1c) |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1437 | |
| 1438 | def testSymbolsNoDtb(self): |
| 1439 | """Test binman can assign symbols embedded in U-Boot SPL""" |
Simon Glass | 3bbc993 | 2021-03-21 18:24:29 +1300 | [diff] [blame] | 1440 | self.checkSymbols('196_symbols_nodtb.dts', |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1441 | U_BOOT_SPL_NODTB_DATA + U_BOOT_SPL_DTB_DATA, |
| 1442 | 0x38) |
| 1443 | |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1444 | def testPackUnitAddress(self): |
| 1445 | """Test that we support multiple binaries with the same name""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1446 | data = self._DoReadFile('054_unit_address.dts') |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1447 | self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data) |
| 1448 | |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1449 | def testSections(self): |
| 1450 | """Basic test of sections""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1451 | data = self._DoReadFile('055_sections.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1452 | expected = (U_BOOT_DATA + tools.get_bytes(ord('!'), 12) + |
| 1453 | U_BOOT_DATA + tools.get_bytes(ord('a'), 12) + |
| 1454 | U_BOOT_DATA + tools.get_bytes(ord('&'), 4)) |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1455 | self.assertEqual(expected, data) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1456 | |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1457 | def testMap(self): |
| 1458 | """Tests outputting a map of the images""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1459 | _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1460 | self.assertEqual('''ImagePos Offset Size Name |
| 1461 | 00000000 00000000 00000028 main-section |
| 1462 | 00000000 00000000 00000010 section@0 |
| 1463 | 00000000 00000000 00000004 u-boot |
| 1464 | 00000010 00000010 00000010 section@1 |
| 1465 | 00000010 00000000 00000004 u-boot |
| 1466 | 00000020 00000020 00000004 section@2 |
| 1467 | 00000020 00000000 00000004 u-boot |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1468 | ''', map_data) |
| 1469 | |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1470 | def testNamePrefix(self): |
| 1471 | """Tests that name prefixes are used""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1472 | _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1473 | self.assertEqual('''ImagePos Offset Size Name |
| 1474 | 00000000 00000000 00000028 main-section |
| 1475 | 00000000 00000000 00000010 section@0 |
| 1476 | 00000000 00000000 00000004 ro-u-boot |
| 1477 | 00000010 00000010 00000010 section@1 |
| 1478 | 00000010 00000000 00000004 rw-u-boot |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1479 | ''', map_data) |
| 1480 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1481 | def testUnknownContents(self): |
| 1482 | """Test that obtaining the contents works as expected""" |
| 1483 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1484 | self._DoReadFile('057_unknown_contents.dts', True) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1485 | self.assertIn("Image '/binman': Internal error: Could not complete " |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1486 | "processing of contents: remaining [" |
| 1487 | "<binman.etype._testing.Entry__testing ", str(e.exception)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1488 | |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1489 | def testBadChangeSize(self): |
| 1490 | """Test that trying to change the size of an entry fails""" |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 1491 | try: |
| 1492 | state.SetAllowEntryExpansion(False) |
| 1493 | with self.assertRaises(ValueError) as e: |
| 1494 | self._DoReadFile('059_change_size.dts', True) |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1495 | 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] | 1496 | str(e.exception)) |
| 1497 | finally: |
| 1498 | state.SetAllowEntryExpansion(True) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1499 | |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1500 | def testUpdateFdt(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1501 | """Test that we can update the device tree with offset/size info""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1502 | _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts', |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1503 | update_dtb=True) |
Simon Glass | 5463a6a | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 1504 | dtb = fdt.Fdt(out_dtb_fname) |
| 1505 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1506 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1507 | self.assertEqual({ |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1508 | 'image-pos': 0, |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 1509 | 'offset': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1510 | '_testing:offset': 32, |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1511 | '_testing:size': 2, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1512 | '_testing:image-pos': 32, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1513 | 'section@0/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1514 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1515 | 'section@0/u-boot:image-pos': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1516 | 'section@0:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1517 | 'section@0:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1518 | 'section@0:image-pos': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1519 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1520 | 'section@1/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1521 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1522 | 'section@1/u-boot:image-pos': 16, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1523 | 'section@1:offset': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1524 | 'section@1:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1525 | 'section@1:image-pos': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1526 | 'size': 40 |
| 1527 | }, props) |
| 1528 | |
| 1529 | def testUpdateFdtBad(self): |
| 1530 | """Test that we detect when ProcessFdt never completes""" |
| 1531 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1532 | self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1533 | self.assertIn('Could not complete processing of Fdt: remaining ' |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1534 | '[<binman.etype._testing.Entry__testing', |
| 1535 | str(e.exception)) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1536 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1537 | def testEntryArgs(self): |
| 1538 | """Test passing arguments to entries from the command line""" |
| 1539 | entry_args = { |
| 1540 | 'test-str-arg': 'test1', |
| 1541 | 'test-int-arg': '456', |
| 1542 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1543 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1544 | self.assertIn('image', control.images) |
| 1545 | entry = control.images['image'].GetEntries()['_testing'] |
| 1546 | self.assertEqual('test0', entry.test_str_fdt) |
| 1547 | self.assertEqual('test1', entry.test_str_arg) |
| 1548 | self.assertEqual(123, entry.test_int_fdt) |
| 1549 | self.assertEqual(456, entry.test_int_arg) |
| 1550 | |
| 1551 | def testEntryArgsMissing(self): |
| 1552 | """Test missing arguments and properties""" |
| 1553 | entry_args = { |
| 1554 | 'test-int-arg': '456', |
| 1555 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1556 | self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1557 | entry = control.images['image'].GetEntries()['_testing'] |
| 1558 | self.assertEqual('test0', entry.test_str_fdt) |
| 1559 | self.assertEqual(None, entry.test_str_arg) |
| 1560 | self.assertEqual(None, entry.test_int_fdt) |
| 1561 | self.assertEqual(456, entry.test_int_arg) |
| 1562 | |
| 1563 | def testEntryArgsRequired(self): |
| 1564 | """Test missing arguments and properties""" |
| 1565 | entry_args = { |
| 1566 | 'test-int-arg': '456', |
| 1567 | } |
| 1568 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1569 | self._DoReadFileDtb('064_entry_args_required.dts') |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1570 | self.assertIn("Node '/binman/_testing': " |
| 1571 | 'Missing required properties/entry args: test-str-arg, ' |
| 1572 | 'test-int-fdt, test-int-arg', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1573 | str(e.exception)) |
| 1574 | |
| 1575 | def testEntryArgsInvalidFormat(self): |
| 1576 | """Test that an invalid entry-argument format is detected""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 1577 | args = ['build', '-d', self.TestFile('064_entry_args_required.dts'), |
| 1578 | '-ano-value'] |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1579 | with self.assertRaises(ValueError) as e: |
| 1580 | self._DoBinman(*args) |
| 1581 | self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception)) |
| 1582 | |
| 1583 | def testEntryArgsInvalidInteger(self): |
| 1584 | """Test that an invalid entry-argument integer is detected""" |
| 1585 | entry_args = { |
| 1586 | 'test-int-arg': 'abc', |
| 1587 | } |
| 1588 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1589 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1590 | self.assertIn("Node '/binman/_testing': Cannot convert entry arg " |
| 1591 | "'test-int-arg' (value 'abc') to integer", |
| 1592 | str(e.exception)) |
| 1593 | |
| 1594 | def testEntryArgsInvalidDatatype(self): |
| 1595 | """Test that an invalid entry-argument datatype is detected |
| 1596 | |
| 1597 | This test could be written in entry_test.py except that it needs |
| 1598 | access to control.entry_args, which seems more than that module should |
| 1599 | be able to see. |
| 1600 | """ |
| 1601 | entry_args = { |
| 1602 | 'test-bad-datatype-arg': '12', |
| 1603 | } |
| 1604 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1605 | self._DoReadFileDtb('065_entry_args_unknown_datatype.dts', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1606 | entry_args=entry_args) |
| 1607 | self.assertIn('GetArg() internal error: Unknown data type ', |
| 1608 | str(e.exception)) |
| 1609 | |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1610 | def testText(self): |
| 1611 | """Test for a text entry type""" |
| 1612 | entry_args = { |
| 1613 | 'test-id': TEXT_DATA, |
| 1614 | 'test-id2': TEXT_DATA2, |
| 1615 | 'test-id3': TEXT_DATA3, |
| 1616 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1617 | data, _, _, _ = self._DoReadFileDtb('066_text.dts', |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1618 | entry_args=entry_args) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1619 | expected = (tools.to_bytes(TEXT_DATA) + |
| 1620 | tools.get_bytes(0, 8 - len(TEXT_DATA)) + |
| 1621 | tools.to_bytes(TEXT_DATA2) + tools.to_bytes(TEXT_DATA3) + |
Simon Glass | 47f6a62 | 2019-07-08 13:18:40 -0600 | [diff] [blame] | 1622 | b'some text' + b'more text') |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1623 | self.assertEqual(expected, data) |
| 1624 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1625 | def testEntryDocs(self): |
| 1626 | """Test for creation of entry documentation""" |
| 1627 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1628 | control.WriteEntryDocs(control.GetEntryModules()) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1629 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 1630 | |
| 1631 | def testEntryDocsMissing(self): |
| 1632 | """Test handling of missing entry documentation""" |
| 1633 | with self.assertRaises(ValueError) as e: |
| 1634 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1635 | control.WriteEntryDocs(control.GetEntryModules(), 'u_boot') |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1636 | self.assertIn('Documentation is missing for modules: u_boot', |
| 1637 | str(e.exception)) |
| 1638 | |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1639 | def testFmap(self): |
| 1640 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1641 | data = self._DoReadFile('067_fmap.dts') |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1642 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1643 | expected = (U_BOOT_DATA + tools.get_bytes(ord('!'), 12) + |
| 1644 | U_BOOT_DATA + tools.get_bytes(ord('a'), 12)) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1645 | self.assertEqual(expected, data[:32]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1646 | self.assertEqual(b'__FMAP__', fhdr.signature) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1647 | self.assertEqual(1, fhdr.ver_major) |
| 1648 | self.assertEqual(0, fhdr.ver_minor) |
| 1649 | self.assertEqual(0, fhdr.base) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1650 | expect_size = fmap_util.FMAP_HEADER_LEN + fmap_util.FMAP_AREA_LEN * 5 |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1651 | self.assertEqual(16 + 16 + expect_size, fhdr.image_size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1652 | self.assertEqual(b'FMAP', fhdr.name) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1653 | self.assertEqual(5, fhdr.nareas) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1654 | fiter = iter(fentries) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1655 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1656 | fentry = next(fiter) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1657 | self.assertEqual(b'SECTION0', fentry.name) |
| 1658 | self.assertEqual(0, fentry.offset) |
| 1659 | self.assertEqual(16, fentry.size) |
| 1660 | self.assertEqual(0, fentry.flags) |
| 1661 | |
| 1662 | fentry = next(fiter) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1663 | self.assertEqual(b'RO_U_BOOT', fentry.name) |
| 1664 | self.assertEqual(0, fentry.offset) |
| 1665 | self.assertEqual(4, fentry.size) |
| 1666 | self.assertEqual(0, fentry.flags) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1667 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1668 | fentry = next(fiter) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1669 | self.assertEqual(b'SECTION1', fentry.name) |
| 1670 | self.assertEqual(16, fentry.offset) |
| 1671 | self.assertEqual(16, fentry.size) |
| 1672 | self.assertEqual(0, fentry.flags) |
| 1673 | |
| 1674 | fentry = next(fiter) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1675 | self.assertEqual(b'RW_U_BOOT', fentry.name) |
| 1676 | self.assertEqual(16, fentry.offset) |
| 1677 | self.assertEqual(4, fentry.size) |
| 1678 | self.assertEqual(0, fentry.flags) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1679 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1680 | fentry = next(fiter) |
| 1681 | self.assertEqual(b'FMAP', fentry.name) |
| 1682 | self.assertEqual(32, fentry.offset) |
| 1683 | self.assertEqual(expect_size, fentry.size) |
| 1684 | self.assertEqual(0, fentry.flags) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1685 | |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1686 | def testBlobNamedByArg(self): |
| 1687 | """Test we can add a blob with the filename coming from an entry arg""" |
| 1688 | entry_args = { |
| 1689 | 'cros-ec-rw-path': 'ecrw.bin', |
| 1690 | } |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1691 | self._DoReadFileDtb('068_blob_named_by_arg.dts', entry_args=entry_args) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1692 | |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1693 | def testFill(self): |
| 1694 | """Test for an fill entry type""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1695 | data = self._DoReadFile('069_fill.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1696 | expected = tools.get_bytes(0xff, 8) + tools.get_bytes(0, 8) |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1697 | self.assertEqual(expected, data) |
| 1698 | |
| 1699 | def testFillNoSize(self): |
| 1700 | """Test for an fill entry type with no size""" |
| 1701 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1702 | self._DoReadFile('070_fill_no_size.dts') |
Simon Glass | 0cf5bce | 2022-08-13 11:40:44 -0600 | [diff] [blame] | 1703 | self.assertIn("'fill' entry is missing properties: size", |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1704 | str(e.exception)) |
| 1705 | |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1706 | def _HandleGbbCommand(self, pipe_list): |
| 1707 | """Fake calls to the futility utility""" |
| 1708 | if pipe_list[0][0] == 'futility': |
| 1709 | fname = pipe_list[0][-1] |
| 1710 | # Append our GBB data to the file, which will happen every time the |
| 1711 | # futility command is called. |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1712 | with open(fname, 'ab') as fd: |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1713 | fd.write(GBB_DATA) |
| 1714 | return command.CommandResult() |
| 1715 | |
| 1716 | def testGbb(self): |
| 1717 | """Test for the Chromium OS Google Binary Block""" |
| 1718 | command.test_result = self._HandleGbbCommand |
| 1719 | entry_args = { |
| 1720 | 'keydir': 'devkeys', |
| 1721 | 'bmpblk': 'bmpblk.bin', |
| 1722 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1723 | data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1724 | |
| 1725 | # Since futility |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1726 | expected = (GBB_DATA + GBB_DATA + tools.get_bytes(0, 8) + |
| 1727 | tools.get_bytes(0, 0x2180 - 16)) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1728 | self.assertEqual(expected, data) |
| 1729 | |
| 1730 | def testGbbTooSmall(self): |
| 1731 | """Test for the Chromium OS Google Binary Block being large enough""" |
| 1732 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1733 | self._DoReadFileDtb('072_gbb_too_small.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1734 | self.assertIn("Node '/binman/gbb': GBB is too small", |
| 1735 | str(e.exception)) |
| 1736 | |
| 1737 | def testGbbNoSize(self): |
| 1738 | """Test for the Chromium OS Google Binary Block having a size""" |
| 1739 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1740 | self._DoReadFileDtb('073_gbb_no_size.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1741 | self.assertIn("Node '/binman/gbb': GBB must have a fixed size", |
| 1742 | str(e.exception)) |
| 1743 | |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 1744 | def testGbbMissing(self): |
| 1745 | """Test that binman still produces an image if futility is missing""" |
| 1746 | entry_args = { |
| 1747 | 'keydir': 'devkeys', |
| 1748 | } |
| 1749 | with test_util.capture_sys_output() as (_, stderr): |
| 1750 | self._DoTestFile('071_gbb.dts', force_missing_bintools='futility', |
| 1751 | entry_args=entry_args) |
| 1752 | err = stderr.getvalue() |
| 1753 | self.assertRegex(err, |
| 1754 | "Image 'main-section'.*missing bintools.*: futility") |
| 1755 | |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1756 | def _HandleVblockCommand(self, pipe_list): |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1757 | """Fake calls to the futility utility |
| 1758 | |
| 1759 | The expected pipe is: |
| 1760 | |
| 1761 | [('futility', 'vbutil_firmware', '--vblock', |
| 1762 | 'vblock.vblock', '--keyblock', 'devkeys/firmware.keyblock', |
| 1763 | '--signprivate', 'devkeys/firmware_data_key.vbprivk', |
| 1764 | '--version', '1', '--fv', 'input.vblock', '--kernelkey', |
| 1765 | 'devkeys/kernel_subkey.vbpubk', '--flags', '1')] |
| 1766 | |
| 1767 | This writes to the output file (here, 'vblock.vblock'). If |
| 1768 | self._hash_data is False, it writes VBLOCK_DATA, else it writes a hash |
| 1769 | of the input data (here, 'input.vblock'). |
| 1770 | """ |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1771 | if pipe_list[0][0] == 'futility': |
| 1772 | fname = pipe_list[0][3] |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 1773 | with open(fname, 'wb') as fd: |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1774 | if self._hash_data: |
| 1775 | infile = pipe_list[0][11] |
| 1776 | m = hashlib.sha256() |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1777 | data = tools.read_file(infile) |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1778 | m.update(data) |
| 1779 | fd.write(m.digest()) |
| 1780 | else: |
| 1781 | fd.write(VBLOCK_DATA) |
| 1782 | |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1783 | return command.CommandResult() |
| 1784 | |
| 1785 | def testVblock(self): |
| 1786 | """Test for the Chromium OS Verified Boot Block""" |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1787 | self._hash_data = False |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1788 | command.test_result = self._HandleVblockCommand |
| 1789 | entry_args = { |
| 1790 | 'keydir': 'devkeys', |
| 1791 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1792 | data, _, _, _ = self._DoReadFileDtb('074_vblock.dts', |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1793 | entry_args=entry_args) |
| 1794 | expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA |
| 1795 | self.assertEqual(expected, data) |
| 1796 | |
| 1797 | def testVblockNoContent(self): |
| 1798 | """Test we detect a vblock which has no content to sign""" |
| 1799 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1800 | self._DoReadFile('075_vblock_no_content.dts') |
Simon Glass | e191578 | 2021-03-21 18:24:31 +1300 | [diff] [blame] | 1801 | self.assertIn("Node '/binman/vblock': Collection must have a 'content' " |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1802 | 'property', str(e.exception)) |
| 1803 | |
| 1804 | def testVblockBadPhandle(self): |
| 1805 | """Test that we detect a vblock with an invalid phandle in contents""" |
| 1806 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1807 | self._DoReadFile('076_vblock_bad_phandle.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1808 | self.assertIn("Node '/binman/vblock': Cannot find node for phandle " |
| 1809 | '1000', str(e.exception)) |
| 1810 | |
| 1811 | def testVblockBadEntry(self): |
| 1812 | """Test that we detect an entry that points to a non-entry""" |
| 1813 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1814 | self._DoReadFile('077_vblock_bad_entry.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1815 | self.assertIn("Node '/binman/vblock': Cannot find entry for node " |
| 1816 | "'other'", str(e.exception)) |
| 1817 | |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1818 | def testVblockContent(self): |
| 1819 | """Test that the vblock signs the right data""" |
| 1820 | self._hash_data = True |
| 1821 | command.test_result = self._HandleVblockCommand |
| 1822 | entry_args = { |
| 1823 | 'keydir': 'devkeys', |
| 1824 | } |
| 1825 | data = self._DoReadFileDtb( |
| 1826 | '189_vblock_content.dts', use_real_dtb=True, update_dtb=True, |
| 1827 | entry_args=entry_args)[0] |
| 1828 | hashlen = 32 # SHA256 hash is 32 bytes |
| 1829 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 1830 | hashval = data[-hashlen:] |
| 1831 | dtb = data[len(U_BOOT_DATA):-hashlen] |
| 1832 | |
| 1833 | expected_data = U_BOOT_DATA + dtb |
| 1834 | |
| 1835 | # The hashval should be a hash of the dtb |
| 1836 | m = hashlib.sha256() |
| 1837 | m.update(expected_data) |
| 1838 | expected_hashval = m.digest() |
| 1839 | self.assertEqual(expected_hashval, hashval) |
| 1840 | |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 1841 | def testVblockMissing(self): |
| 1842 | """Test that binman still produces an image if futility is missing""" |
| 1843 | entry_args = { |
| 1844 | 'keydir': 'devkeys', |
| 1845 | } |
| 1846 | with test_util.capture_sys_output() as (_, stderr): |
| 1847 | self._DoTestFile('074_vblock.dts', |
| 1848 | force_missing_bintools='futility', |
| 1849 | entry_args=entry_args) |
| 1850 | err = stderr.getvalue() |
| 1851 | self.assertRegex(err, |
| 1852 | "Image 'main-section'.*missing bintools.*: futility") |
| 1853 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1854 | def testTpl(self): |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1855 | """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] | 1856 | # ELF file with a '__bss_size' symbol |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1857 | self._SetupTplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1858 | data = self._DoReadFile('078_u_boot_tpl.dts') |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1859 | self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data) |
| 1860 | |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1861 | def testUsesPos(self): |
| 1862 | """Test that the 'pos' property cannot be used anymore""" |
| 1863 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1864 | data = self._DoReadFile('079_uses_pos.dts') |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1865 | self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of " |
| 1866 | "'pos'", str(e.exception)) |
| 1867 | |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1868 | def testFillZero(self): |
| 1869 | """Test for an fill entry type with a size of 0""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1870 | data = self._DoReadFile('080_fill_empty.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1871 | self.assertEqual(tools.get_bytes(0, 16), data) |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1872 | |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1873 | def testTextMissing(self): |
| 1874 | """Test for a text entry type where there is no text""" |
| 1875 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1876 | self._DoReadFileDtb('066_text.dts',) |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1877 | self.assertIn("Node '/binman/text': No value provided for text label " |
| 1878 | "'test-id'", str(e.exception)) |
| 1879 | |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1880 | def testPackStart16Tpl(self): |
| 1881 | """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] | 1882 | data = self._DoReadFile('081_x86_start16_tpl.dts') |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1883 | self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)]) |
| 1884 | |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1885 | def testSelectImage(self): |
| 1886 | """Test that we can select which images to build""" |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1887 | expected = 'Skipping images: image1' |
| 1888 | |
| 1889 | # We should only get the expected message in verbose mode |
Simon Glass | 8a50b4a | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 1890 | for verbosity in (0, 2): |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1891 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1892 | retcode = self._DoTestFile('006_dual_image.dts', |
| 1893 | verbosity=verbosity, |
| 1894 | images=['image2']) |
| 1895 | self.assertEqual(0, retcode) |
| 1896 | if verbosity: |
| 1897 | self.assertIn(expected, stdout.getvalue()) |
| 1898 | else: |
| 1899 | self.assertNotIn(expected, stdout.getvalue()) |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1900 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1901 | self.assertFalse(os.path.exists(tools.get_output_filename('image1.bin'))) |
| 1902 | self.assertTrue(os.path.exists(tools.get_output_filename('image2.bin'))) |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 1903 | self._CleanupOutputDir() |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1904 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1905 | def testUpdateFdtAll(self): |
| 1906 | """Test that all device trees are updated with offset/size info""" |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 1907 | data = self._DoReadFileRealDtb('082_fdt_update_all.dts') |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1908 | |
| 1909 | base_expected = { |
| 1910 | 'section:image-pos': 0, |
| 1911 | 'u-boot-tpl-dtb:size': 513, |
| 1912 | 'u-boot-spl-dtb:size': 513, |
| 1913 | 'u-boot-spl-dtb:offset': 493, |
| 1914 | 'image-pos': 0, |
| 1915 | 'section/u-boot-dtb:image-pos': 0, |
| 1916 | 'u-boot-spl-dtb:image-pos': 493, |
| 1917 | 'section/u-boot-dtb:size': 493, |
| 1918 | 'u-boot-tpl-dtb:image-pos': 1006, |
| 1919 | 'section/u-boot-dtb:offset': 0, |
| 1920 | 'section:size': 493, |
| 1921 | 'offset': 0, |
| 1922 | 'section:offset': 0, |
| 1923 | 'u-boot-tpl-dtb:offset': 1006, |
| 1924 | 'size': 1519 |
| 1925 | } |
| 1926 | |
| 1927 | # We expect three device-tree files in the output, one after the other. |
| 1928 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 1929 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 1930 | # main U-Boot tree. All three should have the same postions and offset. |
| 1931 | start = 0 |
| 1932 | for item in ['', 'spl', 'tpl']: |
| 1933 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1934 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1935 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS + |
| 1936 | ['spl', 'tpl']) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1937 | expected = dict(base_expected) |
| 1938 | if item: |
| 1939 | expected[item] = 0 |
| 1940 | self.assertEqual(expected, props) |
| 1941 | start += dtb._fdt_obj.totalsize() |
| 1942 | |
| 1943 | def testUpdateFdtOutput(self): |
| 1944 | """Test that output DTB files are updated""" |
| 1945 | try: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1946 | data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts', |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1947 | use_real_dtb=True, update_dtb=True, reset_dtbs=False) |
| 1948 | |
| 1949 | # Unfortunately, compiling a source file always results in a file |
| 1950 | # called source.dtb (see fdt_util.EnsureCompiled()). The test |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1951 | # 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] | 1952 | # binman as a file called u-boot.dtb. To fix this, copy the file |
| 1953 | # over to the expected place. |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1954 | start = 0 |
| 1955 | for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out', |
| 1956 | 'tpl/u-boot-tpl.dtb.out']: |
| 1957 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1958 | size = dtb._fdt_obj.totalsize() |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 1959 | pathname = tools.get_output_filename(os.path.split(fname)[1]) |
| 1960 | outdata = tools.read_file(pathname) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1961 | name = os.path.split(fname)[0] |
| 1962 | |
| 1963 | if name: |
| 1964 | orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name) |
| 1965 | else: |
| 1966 | orig_indata = dtb_data |
| 1967 | self.assertNotEqual(outdata, orig_indata, |
| 1968 | "Expected output file '%s' be updated" % pathname) |
| 1969 | self.assertEqual(outdata, data[start:start + size], |
| 1970 | "Expected output file '%s' to match output image" % |
| 1971 | pathname) |
| 1972 | start += size |
| 1973 | finally: |
| 1974 | self._ResetDtbs() |
| 1975 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1976 | def _decompress(self, data): |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 1977 | bintool = self.comp_bintools['lz4'] |
| 1978 | return bintool.decompress(data) |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1979 | |
| 1980 | def testCompress(self): |
| 1981 | """Test compression of blobs""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1982 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1983 | data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts', |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1984 | use_real_dtb=True, update_dtb=True) |
| 1985 | dtb = fdt.Fdt(out_dtb_fname) |
| 1986 | dtb.Scan() |
| 1987 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 1988 | orig = self._decompress(data) |
| 1989 | self.assertEquals(COMPRESS_DATA, orig) |
Simon Glass | 789b3440 | 2020-10-26 17:40:15 -0600 | [diff] [blame] | 1990 | |
| 1991 | # Do a sanity check on various fields |
| 1992 | image = control.images['image'] |
| 1993 | entries = image.GetEntries() |
| 1994 | self.assertEqual(1, len(entries)) |
| 1995 | |
| 1996 | entry = entries['blob'] |
| 1997 | self.assertEqual(COMPRESS_DATA, entry.uncomp_data) |
| 1998 | self.assertEqual(len(COMPRESS_DATA), entry.uncomp_size) |
| 1999 | orig = self._decompress(entry.data) |
| 2000 | self.assertEqual(orig, entry.uncomp_data) |
| 2001 | |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 2002 | self.assertEqual(image.data, entry.data) |
| 2003 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 2004 | expected = { |
| 2005 | 'blob:uncomp-size': len(COMPRESS_DATA), |
| 2006 | 'blob:size': len(data), |
| 2007 | 'size': len(data), |
| 2008 | } |
| 2009 | self.assertEqual(expected, props) |
| 2010 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 2011 | def testFiles(self): |
| 2012 | """Test bringing in multiple files""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2013 | data = self._DoReadFile('084_files.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 2014 | self.assertEqual(FILES_DATA, data) |
| 2015 | |
| 2016 | def testFilesCompress(self): |
| 2017 | """Test bringing in multiple files and compressing them""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 2018 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2019 | data = self._DoReadFile('085_files_compress.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 2020 | |
| 2021 | image = control.images['image'] |
| 2022 | entries = image.GetEntries() |
| 2023 | files = entries['files'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2024 | entries = files._entries |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 2025 | |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2026 | orig = b'' |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 2027 | for i in range(1, 3): |
| 2028 | key = '%d.dat' % i |
| 2029 | start = entries[key].image_pos |
| 2030 | len = entries[key].size |
| 2031 | chunk = data[start:start + len] |
| 2032 | orig += self._decompress(chunk) |
| 2033 | |
| 2034 | self.assertEqual(FILES_DATA, orig) |
| 2035 | |
| 2036 | def testFilesMissing(self): |
| 2037 | """Test missing files""" |
| 2038 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2039 | data = self._DoReadFile('086_files_none.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 2040 | self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched " |
| 2041 | 'no files', str(e.exception)) |
| 2042 | |
| 2043 | def testFilesNoPattern(self): |
| 2044 | """Test missing files""" |
| 2045 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2046 | data = self._DoReadFile('087_files_no_pattern.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 2047 | self.assertIn("Node '/binman/files': Missing 'pattern' property", |
| 2048 | str(e.exception)) |
| 2049 | |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 2050 | def testExtendSize(self): |
| 2051 | """Test an extending entry""" |
| 2052 | data, _, map_data, _ = self._DoReadFileDtb('088_extend_size.dts', |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 2053 | map=True) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2054 | expect = (tools.get_bytes(ord('a'), 8) + U_BOOT_DATA + |
| 2055 | MRC_DATA + tools.get_bytes(ord('b'), 1) + U_BOOT_DATA + |
| 2056 | tools.get_bytes(ord('c'), 8) + U_BOOT_DATA + |
| 2057 | tools.get_bytes(ord('d'), 8)) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 2058 | self.assertEqual(expect, data) |
| 2059 | self.assertEqual('''ImagePos Offset Size Name |
| 2060 | 00000000 00000000 00000028 main-section |
| 2061 | 00000000 00000000 00000008 fill |
| 2062 | 00000008 00000008 00000004 u-boot |
| 2063 | 0000000c 0000000c 00000004 section |
| 2064 | 0000000c 00000000 00000003 intel-mrc |
| 2065 | 00000010 00000010 00000004 u-boot2 |
| 2066 | 00000014 00000014 0000000c section2 |
| 2067 | 00000014 00000000 00000008 fill |
| 2068 | 0000001c 00000008 00000004 u-boot |
| 2069 | 00000020 00000020 00000008 fill2 |
| 2070 | ''', map_data) |
| 2071 | |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 2072 | def testExtendSizeBad(self): |
| 2073 | """Test an extending entry which fails to provide contents""" |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2074 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2075 | with self.assertRaises(ValueError) as e: |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 2076 | self._DoReadFileDtb('089_extend_size_bad.dts', map=True) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 2077 | self.assertIn("Node '/binman/_testing': Cannot obtain contents when " |
| 2078 | 'expanding entry', str(e.exception)) |
| 2079 | |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2080 | def testHash(self): |
| 2081 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2082 | _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2083 | use_real_dtb=True, update_dtb=True) |
| 2084 | dtb = fdt.Fdt(out_dtb_fname) |
| 2085 | dtb.Scan() |
| 2086 | hash_node = dtb.GetNode('/binman/u-boot/hash').props['value'] |
| 2087 | m = hashlib.sha256() |
| 2088 | m.update(U_BOOT_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2089 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2090 | |
| 2091 | def testHashNoAlgo(self): |
| 2092 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2093 | self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2094 | self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for " |
| 2095 | 'hash node', str(e.exception)) |
| 2096 | |
| 2097 | def testHashBadAlgo(self): |
| 2098 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2099 | self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True) |
Simon Glass | 64af7c2 | 2022-02-08 10:59:44 -0700 | [diff] [blame] | 2100 | self.assertIn("Node '/binman/u-boot': Unknown hash algorithm 'invalid'", |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2101 | str(e.exception)) |
| 2102 | |
| 2103 | def testHashSection(self): |
| 2104 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2105 | _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2106 | use_real_dtb=True, update_dtb=True) |
| 2107 | dtb = fdt.Fdt(out_dtb_fname) |
| 2108 | dtb.Scan() |
| 2109 | hash_node = dtb.GetNode('/binman/section/hash').props['value'] |
| 2110 | m = hashlib.sha256() |
| 2111 | m.update(U_BOOT_DATA) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2112 | m.update(tools.get_bytes(ord('a'), 16)) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2113 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2114 | |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 2115 | def testPackUBootTplMicrocode(self): |
| 2116 | """Test that x86 microcode can be handled correctly in TPL |
| 2117 | |
| 2118 | We expect to see the following in the image, in order: |
| 2119 | u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct |
| 2120 | place |
| 2121 | u-boot-tpl.dtb with the microcode removed |
| 2122 | the microcode |
| 2123 | """ |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2124 | self._SetupTplElf('u_boot_ucode_ptr') |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2125 | first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts', |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 2126 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2127 | self.assertEqual(b'tplnodtb with microc' + pos_and_size + |
| 2128 | b'ter somewhere in here', first) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 2129 | |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2130 | def testFmapX86(self): |
| 2131 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2132 | data = self._DoReadFile('094_fmap_x86.dts') |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2133 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2134 | expected = U_BOOT_DATA + MRC_DATA + tools.get_bytes(ord('a'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2135 | self.assertEqual(expected, data[:32]) |
| 2136 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
| 2137 | |
| 2138 | self.assertEqual(0x100, fhdr.image_size) |
| 2139 | |
| 2140 | self.assertEqual(0, fentries[0].offset) |
| 2141 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2142 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2143 | |
| 2144 | self.assertEqual(4, fentries[1].offset) |
| 2145 | self.assertEqual(3, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2146 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2147 | |
| 2148 | self.assertEqual(32, fentries[2].offset) |
| 2149 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 2150 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2151 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2152 | |
| 2153 | def testFmapX86Section(self): |
| 2154 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2155 | data = self._DoReadFile('095_fmap_x86_section.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2156 | expected = U_BOOT_DATA + MRC_DATA + tools.get_bytes(ord('b'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2157 | self.assertEqual(expected, data[:32]) |
| 2158 | fhdr, fentries = fmap_util.DecodeFmap(data[36:]) |
| 2159 | |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 2160 | self.assertEqual(0x180, fhdr.image_size) |
| 2161 | expect_size = fmap_util.FMAP_HEADER_LEN + fmap_util.FMAP_AREA_LEN * 4 |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2162 | fiter = iter(fentries) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2163 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2164 | fentry = next(fiter) |
| 2165 | self.assertEqual(b'U_BOOT', fentry.name) |
| 2166 | self.assertEqual(0, fentry.offset) |
| 2167 | self.assertEqual(4, fentry.size) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2168 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2169 | fentry = next(fiter) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 2170 | self.assertEqual(b'SECTION', fentry.name) |
| 2171 | self.assertEqual(4, fentry.offset) |
| 2172 | self.assertEqual(0x20 + expect_size, fentry.size) |
| 2173 | |
| 2174 | fentry = next(fiter) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2175 | self.assertEqual(b'INTEL_MRC', fentry.name) |
| 2176 | self.assertEqual(4, fentry.offset) |
| 2177 | self.assertEqual(3, fentry.size) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2178 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2179 | fentry = next(fiter) |
| 2180 | self.assertEqual(b'FMAP', fentry.name) |
| 2181 | self.assertEqual(36, fentry.offset) |
| 2182 | self.assertEqual(expect_size, fentry.size) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2183 | |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2184 | def testElf(self): |
| 2185 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 2186 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2187 | self._SetupTplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 2188 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2189 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2190 | data = self._DoReadFile('096_elf.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2191 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 2192 | def testElfStrip(self): |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2193 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 2194 | self._SetupSplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 2195 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2196 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2197 | data = self._DoReadFile('097_elf_strip.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2198 | |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2199 | def testPackOverlapMap(self): |
| 2200 | """Test that overlapping regions are detected""" |
| 2201 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2202 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2203 | self._DoTestFile('014_pack_overlap.dts', map=True) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2204 | map_fname = tools.get_output_filename('image.map') |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2205 | self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname, |
| 2206 | stdout.getvalue()) |
| 2207 | |
| 2208 | # We should not get an inmage, but there should be a map file |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2209 | self.assertFalse(os.path.exists(tools.get_output_filename('image.bin'))) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2210 | self.assertTrue(os.path.exists(map_fname)) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2211 | map_data = tools.read_file(map_fname, binary=False) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2212 | self.assertEqual('''ImagePos Offset Size Name |
Simon Glass | d99850b | 2020-10-26 17:40:24 -0600 | [diff] [blame] | 2213 | <none> 00000000 00000008 main-section |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2214 | <none> 00000000 00000004 u-boot |
| 2215 | <none> 00000003 00000004 u-boot-align |
| 2216 | ''', map_data) |
| 2217 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 2218 | def testPackRefCode(self): |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 2219 | """Test that an image with an Intel Reference code binary works""" |
| 2220 | data = self._DoReadFile('100_intel_refcode.dts') |
| 2221 | self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)]) |
| 2222 | |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 2223 | def testSectionOffset(self): |
| 2224 | """Tests use of a section with an offset""" |
| 2225 | data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts', |
| 2226 | map=True) |
| 2227 | self.assertEqual('''ImagePos Offset Size Name |
| 2228 | 00000000 00000000 00000038 main-section |
| 2229 | 00000004 00000004 00000010 section@0 |
| 2230 | 00000004 00000000 00000004 u-boot |
| 2231 | 00000018 00000018 00000010 section@1 |
| 2232 | 00000018 00000000 00000004 u-boot |
| 2233 | 0000002c 0000002c 00000004 section@2 |
| 2234 | 0000002c 00000000 00000004 u-boot |
| 2235 | ''', map_data) |
| 2236 | self.assertEqual(data, |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2237 | tools.get_bytes(0x26, 4) + U_BOOT_DATA + |
| 2238 | tools.get_bytes(0x21, 12) + |
| 2239 | tools.get_bytes(0x26, 4) + U_BOOT_DATA + |
| 2240 | tools.get_bytes(0x61, 12) + |
| 2241 | tools.get_bytes(0x26, 4) + U_BOOT_DATA + |
| 2242 | tools.get_bytes(0x26, 8)) |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 2243 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 2244 | def testCbfsRaw(self): |
| 2245 | """Test base handling of a Coreboot Filesystem (CBFS) |
| 2246 | |
| 2247 | The exact contents of the CBFS is verified by similar tests in |
| 2248 | cbfs_util_test.py. The tests here merely check that the files added to |
| 2249 | the CBFS can be found in the final image. |
| 2250 | """ |
| 2251 | data = self._DoReadFile('102_cbfs_raw.dts') |
| 2252 | size = 0xb0 |
| 2253 | |
| 2254 | cbfs = cbfs_util.CbfsReader(data) |
| 2255 | self.assertEqual(size, cbfs.rom_size) |
| 2256 | |
| 2257 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2258 | cfile = cbfs.files['u-boot-dtb'] |
| 2259 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 2260 | |
| 2261 | def testCbfsArch(self): |
| 2262 | """Test on non-x86 architecture""" |
| 2263 | data = self._DoReadFile('103_cbfs_raw_ppc.dts') |
| 2264 | size = 0x100 |
| 2265 | |
| 2266 | cbfs = cbfs_util.CbfsReader(data) |
| 2267 | self.assertEqual(size, cbfs.rom_size) |
| 2268 | |
| 2269 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2270 | cfile = cbfs.files['u-boot-dtb'] |
| 2271 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 2272 | |
| 2273 | def testCbfsStage(self): |
| 2274 | """Tests handling of a Coreboot Filesystem (CBFS)""" |
| 2275 | if not elf.ELF_TOOLS: |
| 2276 | self.skipTest('Python elftools not available') |
| 2277 | elf_fname = os.path.join(self._indir, 'cbfs-stage.elf') |
| 2278 | elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA) |
| 2279 | size = 0xb0 |
| 2280 | |
| 2281 | data = self._DoReadFile('104_cbfs_stage.dts') |
| 2282 | cbfs = cbfs_util.CbfsReader(data) |
| 2283 | self.assertEqual(size, cbfs.rom_size) |
| 2284 | |
| 2285 | self.assertIn('u-boot', cbfs.files) |
| 2286 | cfile = cbfs.files['u-boot'] |
| 2287 | self.assertEqual(U_BOOT_DATA + U_BOOT_DTB_DATA, cfile.data) |
| 2288 | |
| 2289 | def testCbfsRawCompress(self): |
| 2290 | """Test handling of compressing raw files""" |
| 2291 | self._CheckLz4() |
| 2292 | data = self._DoReadFile('105_cbfs_raw_compress.dts') |
| 2293 | size = 0x140 |
| 2294 | |
| 2295 | cbfs = cbfs_util.CbfsReader(data) |
| 2296 | self.assertIn('u-boot', cbfs.files) |
| 2297 | cfile = cbfs.files['u-boot'] |
| 2298 | self.assertEqual(COMPRESS_DATA, cfile.data) |
| 2299 | |
| 2300 | def testCbfsBadArch(self): |
| 2301 | """Test handling of a bad architecture""" |
| 2302 | with self.assertRaises(ValueError) as e: |
| 2303 | self._DoReadFile('106_cbfs_bad_arch.dts') |
| 2304 | self.assertIn("Invalid architecture 'bad-arch'", str(e.exception)) |
| 2305 | |
| 2306 | def testCbfsNoSize(self): |
| 2307 | """Test handling of a missing size property""" |
| 2308 | with self.assertRaises(ValueError) as e: |
| 2309 | self._DoReadFile('107_cbfs_no_size.dts') |
| 2310 | self.assertIn('entry must have a size property', str(e.exception)) |
| 2311 | |
Simon Glass | 3e28f4f | 2021-11-23 11:03:54 -0700 | [diff] [blame] | 2312 | def testCbfsNoContents(self): |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 2313 | """Test handling of a CBFS entry which does not provide contentsy""" |
| 2314 | with self.assertRaises(ValueError) as e: |
| 2315 | self._DoReadFile('108_cbfs_no_contents.dts') |
| 2316 | self.assertIn('Could not complete processing of contents', |
| 2317 | str(e.exception)) |
| 2318 | |
| 2319 | def testCbfsBadCompress(self): |
| 2320 | """Test handling of a bad architecture""" |
| 2321 | with self.assertRaises(ValueError) as e: |
| 2322 | self._DoReadFile('109_cbfs_bad_compress.dts') |
| 2323 | self.assertIn("Invalid compression in 'u-boot': 'invalid-algo'", |
| 2324 | str(e.exception)) |
| 2325 | |
| 2326 | def testCbfsNamedEntries(self): |
| 2327 | """Test handling of named entries""" |
| 2328 | data = self._DoReadFile('110_cbfs_name.dts') |
| 2329 | |
| 2330 | cbfs = cbfs_util.CbfsReader(data) |
| 2331 | self.assertIn('FRED', cbfs.files) |
| 2332 | cfile1 = cbfs.files['FRED'] |
| 2333 | self.assertEqual(U_BOOT_DATA, cfile1.data) |
| 2334 | |
| 2335 | self.assertIn('hello', cbfs.files) |
| 2336 | cfile2 = cbfs.files['hello'] |
| 2337 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2338 | |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2339 | def _SetupIfwi(self, fname): |
| 2340 | """Set up to run an IFWI test |
| 2341 | |
| 2342 | Args: |
| 2343 | fname: Filename of input file to provide (fitimage.bin or ifwi.bin) |
| 2344 | """ |
| 2345 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2346 | self._SetupTplElf() |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2347 | |
| 2348 | # Intel Integrated Firmware Image (IFWI) file |
| 2349 | with gzip.open(self.TestFile('%s.gz' % fname), 'rb') as fd: |
| 2350 | data = fd.read() |
| 2351 | TestFunctional._MakeInputFile(fname,data) |
| 2352 | |
| 2353 | def _CheckIfwi(self, data): |
| 2354 | """Check that an image with an IFWI contains the correct output |
| 2355 | |
| 2356 | Args: |
| 2357 | data: Conents of output file |
| 2358 | """ |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2359 | expected_desc = tools.read_file(self.TestFile('descriptor.bin')) |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2360 | if data[:0x1000] != expected_desc: |
| 2361 | self.fail('Expected descriptor binary at start of image') |
| 2362 | |
| 2363 | # We expect to find the TPL wil in subpart IBBP entry IBBL |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2364 | image_fname = tools.get_output_filename('image.bin') |
| 2365 | tpl_fname = tools.get_output_filename('tpl.out') |
Simon Glass | 57c7a48 | 2022-01-09 20:14:01 -0700 | [diff] [blame] | 2366 | ifwitool = bintool.Bintool.create('ifwitool') |
| 2367 | ifwitool.extract(image_fname, 'IBBP', 'IBBL', tpl_fname) |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2368 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2369 | tpl_data = tools.read_file(tpl_fname) |
Simon Glass | f55bd69 | 2019-08-24 07:22:51 -0600 | [diff] [blame] | 2370 | 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] | 2371 | |
| 2372 | def testPackX86RomIfwi(self): |
| 2373 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 2374 | self._SetupIfwi('fitimage.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2375 | data = self._DoReadFile('111_x86_rom_ifwi.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2376 | self._CheckIfwi(data) |
| 2377 | |
| 2378 | def testPackX86RomIfwiNoDesc(self): |
| 2379 | """Test that an x86 ROM with IFWI can be created from an ifwi.bin file""" |
| 2380 | self._SetupIfwi('ifwi.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2381 | data = self._DoReadFile('112_x86_rom_ifwi_nodesc.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2382 | self._CheckIfwi(data) |
| 2383 | |
| 2384 | def testPackX86RomIfwiNoData(self): |
| 2385 | """Test that an x86 ROM with IFWI handles missing data""" |
| 2386 | self._SetupIfwi('ifwi.bin') |
| 2387 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2388 | data = self._DoReadFile('113_x86_rom_ifwi_nodata.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2389 | self.assertIn('Could not complete processing of contents', |
| 2390 | str(e.exception)) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 2391 | |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 2392 | def testIfwiMissing(self): |
| 2393 | """Test that binman still produces an image if ifwitool is missing""" |
| 2394 | self._SetupIfwi('fitimage.bin') |
| 2395 | with test_util.capture_sys_output() as (_, stderr): |
| 2396 | self._DoTestFile('111_x86_rom_ifwi.dts', |
| 2397 | force_missing_bintools='ifwitool') |
| 2398 | err = stderr.getvalue() |
| 2399 | self.assertRegex(err, |
| 2400 | "Image 'main-section'.*missing bintools.*: ifwitool") |
| 2401 | |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2402 | def testCbfsOffset(self): |
| 2403 | """Test a CBFS with files at particular offsets |
| 2404 | |
| 2405 | Like all CFBS tests, this is just checking the logic that calls |
| 2406 | cbfs_util. See cbfs_util_test for fully tests (e.g. test_cbfs_offset()). |
| 2407 | """ |
| 2408 | data = self._DoReadFile('114_cbfs_offset.dts') |
| 2409 | size = 0x200 |
| 2410 | |
| 2411 | cbfs = cbfs_util.CbfsReader(data) |
| 2412 | self.assertEqual(size, cbfs.rom_size) |
| 2413 | |
| 2414 | self.assertIn('u-boot', cbfs.files) |
| 2415 | cfile = cbfs.files['u-boot'] |
| 2416 | self.assertEqual(U_BOOT_DATA, cfile.data) |
| 2417 | self.assertEqual(0x40, cfile.cbfs_offset) |
| 2418 | |
| 2419 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2420 | cfile2 = cbfs.files['u-boot-dtb'] |
| 2421 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2422 | self.assertEqual(0x140, cfile2.cbfs_offset) |
| 2423 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2424 | def testFdtmap(self): |
| 2425 | """Test an FDT map can be inserted in the image""" |
| 2426 | data = self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2427 | fdtmap_data = data[len(U_BOOT_DATA):] |
| 2428 | magic = fdtmap_data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2429 | self.assertEqual(b'_FDTMAP_', magic) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2430 | self.assertEqual(tools.get_bytes(0, 8), fdtmap_data[8:16]) |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2431 | |
| 2432 | fdt_data = fdtmap_data[16:] |
| 2433 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2434 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2435 | props = self._GetPropTree(dtb, BASE_DTB_PROPS, prefix='/') |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2436 | self.assertEqual({ |
| 2437 | 'image-pos': 0, |
| 2438 | 'offset': 0, |
| 2439 | 'u-boot:offset': 0, |
| 2440 | 'u-boot:size': len(U_BOOT_DATA), |
| 2441 | 'u-boot:image-pos': 0, |
| 2442 | 'fdtmap:image-pos': 4, |
| 2443 | 'fdtmap:offset': 4, |
| 2444 | 'fdtmap:size': len(fdtmap_data), |
| 2445 | 'size': len(data), |
| 2446 | }, props) |
| 2447 | |
| 2448 | def testFdtmapNoMatch(self): |
| 2449 | """Check handling of an FDT map when the section cannot be found""" |
| 2450 | self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2451 | |
| 2452 | # Mangle the section name, which should cause a mismatch between the |
| 2453 | # correct FDT path and the one expected by the section |
| 2454 | image = control.images['image'] |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2455 | image._node.path += '-suffix' |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2456 | entries = image.GetEntries() |
| 2457 | fdtmap = entries['fdtmap'] |
| 2458 | with self.assertRaises(ValueError) as e: |
| 2459 | fdtmap._GetFdtmap() |
| 2460 | self.assertIn("Cannot locate node for path '/binman-suffix'", |
| 2461 | str(e.exception)) |
| 2462 | |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2463 | def testFdtmapHeader(self): |
| 2464 | """Test an FDT map and image header can be inserted in the image""" |
| 2465 | data = self.data = self._DoReadFileRealDtb('116_fdtmap_hdr.dts') |
| 2466 | fdtmap_pos = len(U_BOOT_DATA) |
| 2467 | fdtmap_data = data[fdtmap_pos:] |
| 2468 | fdt_data = fdtmap_data[16:] |
| 2469 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2470 | fdt_size = dtb.GetFdtObj().totalsize() |
| 2471 | hdr_data = data[-8:] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2472 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2473 | offset = struct.unpack('<I', hdr_data[4:])[0] & 0xffffffff |
| 2474 | self.assertEqual(fdtmap_pos - 0x400, offset - (1 << 32)) |
| 2475 | |
| 2476 | def testFdtmapHeaderStart(self): |
| 2477 | """Test an image header can be inserted at the image start""" |
| 2478 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
| 2479 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2480 | hdr_data = data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2481 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2482 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2483 | self.assertEqual(fdtmap_pos, offset) |
| 2484 | |
| 2485 | def testFdtmapHeaderPos(self): |
| 2486 | """Test an image header can be inserted at a chosen position""" |
| 2487 | data = self.data = self._DoReadFileRealDtb('118_fdtmap_hdr_pos.dts') |
| 2488 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2489 | hdr_data = data[0x80:0x88] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2490 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2491 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2492 | self.assertEqual(fdtmap_pos, offset) |
| 2493 | |
| 2494 | def testHeaderMissingFdtmap(self): |
| 2495 | """Test an image header requires an fdtmap""" |
| 2496 | with self.assertRaises(ValueError) as e: |
| 2497 | self.data = self._DoReadFileRealDtb('119_fdtmap_hdr_missing.dts') |
| 2498 | self.assertIn("'image_header' section must have an 'fdtmap' sibling", |
| 2499 | str(e.exception)) |
| 2500 | |
| 2501 | def testHeaderNoLocation(self): |
| 2502 | """Test an image header with a no specified location is detected""" |
| 2503 | with self.assertRaises(ValueError) as e: |
| 2504 | self.data = self._DoReadFileRealDtb('120_hdr_no_location.dts') |
| 2505 | self.assertIn("Invalid location 'None', expected 'start' or 'end'", |
| 2506 | str(e.exception)) |
| 2507 | |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2508 | def testEntryExpand(self): |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 2509 | """Test extending an entry after it is packed""" |
| 2510 | data = self._DoReadFile('121_entry_extend.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2511 | self.assertEqual(b'aaa', data[:3]) |
| 2512 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2513 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2514 | |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 2515 | def testEntryExtendBad(self): |
| 2516 | """Test extending an entry after it is packed, twice""" |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2517 | with self.assertRaises(ValueError) as e: |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 2518 | self._DoReadFile('122_entry_extend_twice.dts') |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 2519 | self.assertIn("Image '/binman': Entries changed size after packing", |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2520 | str(e.exception)) |
| 2521 | |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 2522 | def testEntryExtendSection(self): |
| 2523 | """Test extending an entry within a section after it is packed""" |
| 2524 | data = self._DoReadFile('123_entry_extend_section.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2525 | self.assertEqual(b'aaa', data[:3]) |
| 2526 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2527 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2528 | |
Simon Glass | 90d2968 | 2019-07-08 14:25:38 -0600 | [diff] [blame] | 2529 | def testCompressDtb(self): |
| 2530 | """Test that compress of device-tree files is supported""" |
| 2531 | self._CheckLz4() |
| 2532 | data = self.data = self._DoReadFileRealDtb('124_compress_dtb.dts') |
| 2533 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 2534 | comp_data = data[len(U_BOOT_DATA):] |
| 2535 | orig = self._decompress(comp_data) |
| 2536 | dtb = fdt.Fdt.FromData(orig) |
| 2537 | dtb.Scan() |
| 2538 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 2539 | expected = { |
| 2540 | 'u-boot:size': len(U_BOOT_DATA), |
| 2541 | 'u-boot-dtb:uncomp-size': len(orig), |
| 2542 | 'u-boot-dtb:size': len(comp_data), |
| 2543 | 'size': len(data), |
| 2544 | } |
| 2545 | self.assertEqual(expected, props) |
| 2546 | |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2547 | def testCbfsUpdateFdt(self): |
| 2548 | """Test that we can update the device tree with CBFS offset/size info""" |
| 2549 | self._CheckLz4() |
| 2550 | data, _, _, out_dtb_fname = self._DoReadFileDtb('125_cbfs_update.dts', |
| 2551 | update_dtb=True) |
| 2552 | dtb = fdt.Fdt(out_dtb_fname) |
| 2553 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2554 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + ['uncomp-size']) |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2555 | del props['cbfs/u-boot:size'] |
| 2556 | self.assertEqual({ |
| 2557 | 'offset': 0, |
| 2558 | 'size': len(data), |
| 2559 | 'image-pos': 0, |
| 2560 | 'cbfs:offset': 0, |
| 2561 | 'cbfs:size': len(data), |
| 2562 | 'cbfs:image-pos': 0, |
| 2563 | 'cbfs/u-boot:offset': 0x38, |
| 2564 | 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA), |
| 2565 | 'cbfs/u-boot:image-pos': 0x38, |
| 2566 | 'cbfs/u-boot-dtb:offset': 0xb8, |
| 2567 | 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA), |
| 2568 | 'cbfs/u-boot-dtb:image-pos': 0xb8, |
| 2569 | }, props) |
| 2570 | |
Simon Glass | 3c9b4f2 | 2019-07-08 14:25:42 -0600 | [diff] [blame] | 2571 | def testCbfsBadType(self): |
| 2572 | """Test an image header with a no specified location is detected""" |
| 2573 | with self.assertRaises(ValueError) as e: |
| 2574 | self._DoReadFile('126_cbfs_bad_type.dts') |
| 2575 | self.assertIn("Unknown cbfs-type 'badtype'", str(e.exception)) |
| 2576 | |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2577 | def testList(self): |
| 2578 | """Test listing the files in an image""" |
| 2579 | self._CheckLz4() |
| 2580 | data = self._DoReadFile('127_list.dts') |
| 2581 | image = control.images['image'] |
| 2582 | entries = image.BuildEntryList() |
| 2583 | self.assertEqual(7, len(entries)) |
| 2584 | |
| 2585 | ent = entries[0] |
| 2586 | self.assertEqual(0, ent.indent) |
| 2587 | self.assertEqual('main-section', ent.name) |
| 2588 | self.assertEqual('section', ent.etype) |
| 2589 | self.assertEqual(len(data), ent.size) |
| 2590 | self.assertEqual(0, ent.image_pos) |
| 2591 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2592 | self.assertEqual(0, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2593 | |
| 2594 | ent = entries[1] |
| 2595 | self.assertEqual(1, ent.indent) |
| 2596 | self.assertEqual('u-boot', ent.name) |
| 2597 | self.assertEqual('u-boot', ent.etype) |
| 2598 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2599 | self.assertEqual(0, ent.image_pos) |
| 2600 | self.assertEqual(None, ent.uncomp_size) |
| 2601 | self.assertEqual(0, ent.offset) |
| 2602 | |
| 2603 | ent = entries[2] |
| 2604 | self.assertEqual(1, ent.indent) |
| 2605 | self.assertEqual('section', ent.name) |
| 2606 | self.assertEqual('section', ent.etype) |
| 2607 | section_size = ent.size |
| 2608 | self.assertEqual(0x100, ent.image_pos) |
| 2609 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2610 | self.assertEqual(0x100, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2611 | |
| 2612 | ent = entries[3] |
| 2613 | self.assertEqual(2, ent.indent) |
| 2614 | self.assertEqual('cbfs', ent.name) |
| 2615 | self.assertEqual('cbfs', ent.etype) |
| 2616 | self.assertEqual(0x400, ent.size) |
| 2617 | self.assertEqual(0x100, ent.image_pos) |
| 2618 | self.assertEqual(None, ent.uncomp_size) |
| 2619 | self.assertEqual(0, ent.offset) |
| 2620 | |
| 2621 | ent = entries[4] |
| 2622 | self.assertEqual(3, ent.indent) |
| 2623 | self.assertEqual('u-boot', ent.name) |
| 2624 | self.assertEqual('u-boot', ent.etype) |
| 2625 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2626 | self.assertEqual(0x138, ent.image_pos) |
| 2627 | self.assertEqual(None, ent.uncomp_size) |
| 2628 | self.assertEqual(0x38, ent.offset) |
| 2629 | |
| 2630 | ent = entries[5] |
| 2631 | self.assertEqual(3, ent.indent) |
| 2632 | self.assertEqual('u-boot-dtb', ent.name) |
| 2633 | self.assertEqual('text', ent.etype) |
| 2634 | self.assertGreater(len(COMPRESS_DATA), ent.size) |
| 2635 | self.assertEqual(0x178, ent.image_pos) |
| 2636 | self.assertEqual(len(COMPRESS_DATA), ent.uncomp_size) |
| 2637 | self.assertEqual(0x78, ent.offset) |
| 2638 | |
| 2639 | ent = entries[6] |
| 2640 | self.assertEqual(2, ent.indent) |
| 2641 | self.assertEqual('u-boot-dtb', ent.name) |
| 2642 | self.assertEqual('u-boot-dtb', ent.etype) |
| 2643 | self.assertEqual(0x500, ent.image_pos) |
| 2644 | self.assertEqual(len(U_BOOT_DTB_DATA), ent.uncomp_size) |
| 2645 | dtb_size = ent.size |
| 2646 | # Compressing this data expands it since headers are added |
| 2647 | self.assertGreater(dtb_size, len(U_BOOT_DTB_DATA)) |
| 2648 | self.assertEqual(0x400, ent.offset) |
| 2649 | |
| 2650 | self.assertEqual(len(data), 0x100 + section_size) |
| 2651 | self.assertEqual(section_size, 0x400 + dtb_size) |
| 2652 | |
Simon Glass | 8d8bf4e | 2019-07-08 14:25:44 -0600 | [diff] [blame] | 2653 | def testFindFdtmap(self): |
| 2654 | """Test locating an FDT map in an image""" |
| 2655 | self._CheckLz4() |
| 2656 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
| 2657 | image = control.images['image'] |
| 2658 | entries = image.GetEntries() |
| 2659 | entry = entries['fdtmap'] |
| 2660 | self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data)) |
| 2661 | |
| 2662 | def testFindFdtmapMissing(self): |
| 2663 | """Test failing to locate an FDP map""" |
| 2664 | data = self._DoReadFile('005_simple.dts') |
| 2665 | self.assertEqual(None, fdtmap.LocateFdtmap(data)) |
| 2666 | |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2667 | def testFindImageHeader(self): |
| 2668 | """Test locating a image header""" |
| 2669 | self._CheckLz4() |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2670 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2671 | image = control.images['image'] |
| 2672 | entries = image.GetEntries() |
| 2673 | entry = entries['fdtmap'] |
| 2674 | # The header should point to the FDT map |
| 2675 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2676 | |
| 2677 | def testFindImageHeaderStart(self): |
| 2678 | """Test locating a image header located at the start of an image""" |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2679 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2680 | image = control.images['image'] |
| 2681 | entries = image.GetEntries() |
| 2682 | entry = entries['fdtmap'] |
| 2683 | # The header should point to the FDT map |
| 2684 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2685 | |
| 2686 | def testFindImageHeaderMissing(self): |
| 2687 | """Test failing to locate an image header""" |
| 2688 | data = self._DoReadFile('005_simple.dts') |
| 2689 | self.assertEqual(None, image_header.LocateHeaderOffset(data)) |
| 2690 | |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2691 | def testReadImage(self): |
| 2692 | """Test reading an image and accessing its FDT map""" |
| 2693 | self._CheckLz4() |
| 2694 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2695 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2696 | orig_image = control.images['image'] |
| 2697 | image = Image.FromFile(image_fname) |
| 2698 | self.assertEqual(orig_image.GetEntries().keys(), |
| 2699 | image.GetEntries().keys()) |
| 2700 | |
| 2701 | orig_entry = orig_image.GetEntries()['fdtmap'] |
| 2702 | entry = image.GetEntries()['fdtmap'] |
| 2703 | self.assertEquals(orig_entry.offset, entry.offset) |
| 2704 | self.assertEquals(orig_entry.size, entry.size) |
| 2705 | self.assertEquals(orig_entry.image_pos, entry.image_pos) |
| 2706 | |
| 2707 | def testReadImageNoHeader(self): |
| 2708 | """Test accessing an image's FDT map without an image header""" |
| 2709 | self._CheckLz4() |
| 2710 | data = self._DoReadFileRealDtb('129_decode_image_nohdr.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2711 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2712 | image = Image.FromFile(image_fname) |
| 2713 | self.assertTrue(isinstance(image, Image)) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2714 | self.assertEqual('image', image.image_name[-5:]) |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2715 | |
| 2716 | def testReadImageFail(self): |
| 2717 | """Test failing to read an image image's FDT map""" |
| 2718 | self._DoReadFile('005_simple.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2719 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2720 | with self.assertRaises(ValueError) as e: |
| 2721 | image = Image.FromFile(image_fname) |
| 2722 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2723 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2724 | def testListCmd(self): |
| 2725 | """Test listing the files in an image using an Fdtmap""" |
| 2726 | self._CheckLz4() |
| 2727 | data = self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2728 | |
| 2729 | # lz4 compression size differs depending on the version |
| 2730 | image = control.images['image'] |
| 2731 | entries = image.GetEntries() |
| 2732 | section_size = entries['section'].size |
| 2733 | fdt_size = entries['section'].GetEntries()['u-boot-dtb'].size |
| 2734 | fdtmap_offset = entries['fdtmap'].offset |
| 2735 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2736 | try: |
| 2737 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2738 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2739 | self._DoBinman('ls', '-i', updated_fname) |
| 2740 | finally: |
| 2741 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2742 | lines = stdout.getvalue().splitlines() |
| 2743 | expected = [ |
| 2744 | 'Name Image-pos Size Entry-type Offset Uncomp-size', |
| 2745 | '----------------------------------------------------------------------', |
| 2746 | 'main-section 0 c00 section 0', |
| 2747 | ' u-boot 0 4 u-boot 0', |
| 2748 | ' section 100 %x section 100' % section_size, |
| 2749 | ' cbfs 100 400 cbfs 0', |
| 2750 | ' u-boot 138 4 u-boot 38', |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2751 | ' u-boot-dtb 180 105 u-boot-dtb 80 3c9', |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2752 | ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size, |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2753 | ' fdtmap %x 3bd fdtmap %x' % |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2754 | (fdtmap_offset, fdtmap_offset), |
| 2755 | ' image-header bf8 8 image-header bf8', |
| 2756 | ] |
| 2757 | self.assertEqual(expected, lines) |
| 2758 | |
| 2759 | def testListCmdFail(self): |
| 2760 | """Test failing to list an image""" |
| 2761 | self._DoReadFile('005_simple.dts') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2762 | try: |
| 2763 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2764 | with self.assertRaises(ValueError) as e: |
| 2765 | self._DoBinman('ls', '-i', updated_fname) |
| 2766 | finally: |
| 2767 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2768 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
| 2769 | |
| 2770 | def _RunListCmd(self, paths, expected): |
| 2771 | """List out entries and check the result |
| 2772 | |
| 2773 | Args: |
| 2774 | paths: List of paths to pass to the list command |
| 2775 | expected: Expected list of filenames to be returned, in order |
| 2776 | """ |
| 2777 | self._CheckLz4() |
| 2778 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2779 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2780 | image = Image.FromFile(image_fname) |
| 2781 | lines = image.GetListEntries(paths)[1] |
| 2782 | files = [line[0].strip() for line in lines[1:]] |
| 2783 | self.assertEqual(expected, files) |
| 2784 | |
| 2785 | def testListCmdSection(self): |
| 2786 | """Test listing the files in a section""" |
| 2787 | self._RunListCmd(['section'], |
| 2788 | ['section', 'cbfs', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2789 | |
| 2790 | def testListCmdFile(self): |
| 2791 | """Test listing a particular file""" |
| 2792 | self._RunListCmd(['*u-boot-dtb'], ['u-boot-dtb', 'u-boot-dtb']) |
| 2793 | |
| 2794 | def testListCmdWildcard(self): |
| 2795 | """Test listing a wildcarded file""" |
| 2796 | self._RunListCmd(['*boot*'], |
| 2797 | ['u-boot', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2798 | |
| 2799 | def testListCmdWildcardMulti(self): |
| 2800 | """Test listing a wildcarded file""" |
| 2801 | self._RunListCmd(['*cb*', '*head*'], |
| 2802 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 2803 | |
| 2804 | def testListCmdEmpty(self): |
| 2805 | """Test listing a wildcarded file""" |
| 2806 | self._RunListCmd(['nothing'], []) |
| 2807 | |
| 2808 | def testListCmdPath(self): |
| 2809 | """Test listing the files in a sub-entry of a section""" |
| 2810 | self._RunListCmd(['section/cbfs'], ['cbfs', 'u-boot', 'u-boot-dtb']) |
| 2811 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2812 | def _RunExtractCmd(self, entry_name, decomp=True): |
| 2813 | """Extract an entry from an image |
| 2814 | |
| 2815 | Args: |
| 2816 | entry_name: Entry name to extract |
| 2817 | decomp: True to decompress the data if compressed, False to leave |
| 2818 | it in its raw uncompressed format |
| 2819 | |
| 2820 | Returns: |
| 2821 | data from entry |
| 2822 | """ |
| 2823 | self._CheckLz4() |
| 2824 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2825 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2826 | return control.ReadEntry(image_fname, entry_name, decomp) |
| 2827 | |
| 2828 | def testExtractSimple(self): |
| 2829 | """Test extracting a single file""" |
| 2830 | data = self._RunExtractCmd('u-boot') |
| 2831 | self.assertEqual(U_BOOT_DATA, data) |
| 2832 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2833 | def testExtractSection(self): |
| 2834 | """Test extracting the files in a section""" |
| 2835 | data = self._RunExtractCmd('section') |
| 2836 | cbfs_data = data[:0x400] |
| 2837 | cbfs = cbfs_util.CbfsReader(cbfs_data) |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2838 | self.assertEqual(['u-boot', 'u-boot-dtb', ''], list(cbfs.files.keys())) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2839 | dtb_data = data[0x400:] |
| 2840 | dtb = self._decompress(dtb_data) |
| 2841 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2842 | |
| 2843 | def testExtractCompressed(self): |
| 2844 | """Test extracting compressed data""" |
| 2845 | data = self._RunExtractCmd('section/u-boot-dtb') |
| 2846 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2847 | |
| 2848 | def testExtractRaw(self): |
| 2849 | """Test extracting compressed data without decompressing it""" |
| 2850 | data = self._RunExtractCmd('section/u-boot-dtb', decomp=False) |
| 2851 | dtb = self._decompress(data) |
| 2852 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2853 | |
| 2854 | def testExtractCbfs(self): |
| 2855 | """Test extracting CBFS data""" |
| 2856 | data = self._RunExtractCmd('section/cbfs/u-boot') |
| 2857 | self.assertEqual(U_BOOT_DATA, data) |
| 2858 | |
| 2859 | def testExtractCbfsCompressed(self): |
| 2860 | """Test extracting CBFS compressed data""" |
| 2861 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb') |
| 2862 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2863 | |
| 2864 | def testExtractCbfsRaw(self): |
| 2865 | """Test extracting CBFS compressed data without decompressing it""" |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 2866 | bintool = self.comp_bintools['lzma_alone'] |
| 2867 | self._CheckBintool(bintool) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2868 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb', decomp=False) |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 2869 | dtb = bintool.decompress(data) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2870 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2871 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2872 | def testExtractBadEntry(self): |
| 2873 | """Test extracting a bad section path""" |
| 2874 | with self.assertRaises(ValueError) as e: |
| 2875 | self._RunExtractCmd('section/does-not-exist') |
| 2876 | self.assertIn("Entry 'does-not-exist' not found in '/section'", |
| 2877 | str(e.exception)) |
| 2878 | |
| 2879 | def testExtractMissingFile(self): |
| 2880 | """Test extracting file that does not exist""" |
| 2881 | with self.assertRaises(IOError) as e: |
| 2882 | control.ReadEntry('missing-file', 'name') |
| 2883 | |
| 2884 | def testExtractBadFile(self): |
| 2885 | """Test extracting an invalid file""" |
| 2886 | fname = os.path.join(self._indir, 'badfile') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2887 | tools.write_file(fname, b'') |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2888 | with self.assertRaises(ValueError) as e: |
| 2889 | control.ReadEntry(fname, 'name') |
| 2890 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2891 | def testExtractCmd(self): |
| 2892 | """Test extracting a file fron an image on the command line""" |
| 2893 | self._CheckLz4() |
| 2894 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2895 | fname = os.path.join(self._indir, 'output.extact') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2896 | try: |
| 2897 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2898 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2899 | self._DoBinman('extract', '-i', updated_fname, 'u-boot', |
| 2900 | '-f', fname) |
| 2901 | finally: |
| 2902 | shutil.rmtree(tmpdir) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2903 | data = tools.read_file(fname) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2904 | self.assertEqual(U_BOOT_DATA, data) |
| 2905 | |
| 2906 | def testExtractOneEntry(self): |
| 2907 | """Test extracting a single entry fron an image """ |
| 2908 | self._CheckLz4() |
| 2909 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2910 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2911 | fname = os.path.join(self._indir, 'output.extact') |
| 2912 | control.ExtractEntries(image_fname, fname, None, ['u-boot']) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2913 | data = tools.read_file(fname) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2914 | self.assertEqual(U_BOOT_DATA, data) |
| 2915 | |
| 2916 | def _CheckExtractOutput(self, decomp): |
| 2917 | """Helper to test file output with and without decompression |
| 2918 | |
| 2919 | Args: |
| 2920 | decomp: True to decompress entry data, False to output it raw |
| 2921 | """ |
| 2922 | def _CheckPresent(entry_path, expect_data, expect_size=None): |
| 2923 | """Check and remove expected file |
| 2924 | |
| 2925 | This checks the data/size of a file and removes the file both from |
| 2926 | the outfiles set and from the output directory. Once all files are |
| 2927 | processed, both the set and directory should be empty. |
| 2928 | |
| 2929 | Args: |
| 2930 | entry_path: Entry path |
| 2931 | expect_data: Data to expect in file, or None to skip check |
| 2932 | expect_size: Size of data to expect in file, or None to skip |
| 2933 | """ |
| 2934 | path = os.path.join(outdir, entry_path) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2935 | data = tools.read_file(path) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2936 | os.remove(path) |
| 2937 | if expect_data: |
| 2938 | self.assertEqual(expect_data, data) |
| 2939 | elif expect_size: |
| 2940 | self.assertEqual(expect_size, len(data)) |
| 2941 | outfiles.remove(path) |
| 2942 | |
| 2943 | def _CheckDirPresent(name): |
| 2944 | """Remove expected directory |
| 2945 | |
| 2946 | This gives an error if the directory does not exist as expected |
| 2947 | |
| 2948 | Args: |
| 2949 | name: Name of directory to remove |
| 2950 | """ |
| 2951 | path = os.path.join(outdir, name) |
| 2952 | os.rmdir(path) |
| 2953 | |
| 2954 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2955 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2956 | outdir = os.path.join(self._indir, 'extract') |
| 2957 | einfos = control.ExtractEntries(image_fname, None, outdir, [], decomp) |
| 2958 | |
| 2959 | # Create a set of all file that were output (should be 9) |
| 2960 | outfiles = set() |
| 2961 | for root, dirs, files in os.walk(outdir): |
| 2962 | outfiles |= set([os.path.join(root, fname) for fname in files]) |
| 2963 | self.assertEqual(9, len(outfiles)) |
| 2964 | self.assertEqual(9, len(einfos)) |
| 2965 | |
| 2966 | image = control.images['image'] |
| 2967 | entries = image.GetEntries() |
| 2968 | |
| 2969 | # Check the 9 files in various ways |
| 2970 | section = entries['section'] |
| 2971 | section_entries = section.GetEntries() |
| 2972 | cbfs_entries = section_entries['cbfs'].GetEntries() |
| 2973 | _CheckPresent('u-boot', U_BOOT_DATA) |
| 2974 | _CheckPresent('section/cbfs/u-boot', U_BOOT_DATA) |
| 2975 | dtb_len = EXTRACT_DTB_SIZE |
| 2976 | if not decomp: |
| 2977 | dtb_len = cbfs_entries['u-boot-dtb'].size |
| 2978 | _CheckPresent('section/cbfs/u-boot-dtb', None, dtb_len) |
| 2979 | if not decomp: |
| 2980 | dtb_len = section_entries['u-boot-dtb'].size |
| 2981 | _CheckPresent('section/u-boot-dtb', None, dtb_len) |
| 2982 | |
| 2983 | fdtmap = entries['fdtmap'] |
| 2984 | _CheckPresent('fdtmap', fdtmap.data) |
| 2985 | hdr = entries['image-header'] |
| 2986 | _CheckPresent('image-header', hdr.data) |
| 2987 | |
| 2988 | _CheckPresent('section/root', section.data) |
| 2989 | cbfs = section_entries['cbfs'] |
| 2990 | _CheckPresent('section/cbfs/root', cbfs.data) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 2991 | data = tools.read_file(image_fname) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2992 | _CheckPresent('root', data) |
| 2993 | |
| 2994 | # There should be no files left. Remove all the directories to check. |
| 2995 | # If there are any files/dirs remaining, one of these checks will fail. |
| 2996 | self.assertEqual(0, len(outfiles)) |
| 2997 | _CheckDirPresent('section/cbfs') |
| 2998 | _CheckDirPresent('section') |
| 2999 | _CheckDirPresent('') |
| 3000 | self.assertFalse(os.path.exists(outdir)) |
| 3001 | |
| 3002 | def testExtractAllEntries(self): |
| 3003 | """Test extracting all entries""" |
| 3004 | self._CheckLz4() |
| 3005 | self._CheckExtractOutput(decomp=True) |
| 3006 | |
| 3007 | def testExtractAllEntriesRaw(self): |
| 3008 | """Test extracting all entries without decompressing them""" |
| 3009 | self._CheckLz4() |
| 3010 | self._CheckExtractOutput(decomp=False) |
| 3011 | |
| 3012 | def testExtractSelectedEntries(self): |
| 3013 | """Test extracting some entries""" |
| 3014 | self._CheckLz4() |
| 3015 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3016 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 3017 | outdir = os.path.join(self._indir, 'extract') |
| 3018 | einfos = control.ExtractEntries(image_fname, None, outdir, |
| 3019 | ['*cb*', '*head*']) |
| 3020 | |
| 3021 | # File output is tested by testExtractAllEntries(), so just check that |
| 3022 | # the expected entries are selected |
| 3023 | names = [einfo.name for einfo in einfos] |
| 3024 | self.assertEqual(names, |
| 3025 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 3026 | |
| 3027 | def testExtractNoEntryPaths(self): |
| 3028 | """Test extracting some entries""" |
| 3029 | self._CheckLz4() |
| 3030 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3031 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 3032 | with self.assertRaises(ValueError) as e: |
| 3033 | control.ExtractEntries(image_fname, 'fname', None, []) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 3034 | self.assertIn('Must specify an entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 3035 | str(e.exception)) |
| 3036 | |
| 3037 | def testExtractTooManyEntryPaths(self): |
| 3038 | """Test extracting some entries""" |
| 3039 | self._CheckLz4() |
| 3040 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3041 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 3042 | with self.assertRaises(ValueError) as e: |
| 3043 | control.ExtractEntries(image_fname, 'fname', None, ['a', 'b']) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 3044 | self.assertIn('Must specify exactly one entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 3045 | str(e.exception)) |
| 3046 | |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 3047 | def testPackAlignSection(self): |
| 3048 | """Test that sections can have alignment""" |
| 3049 | self._DoReadFile('131_pack_align_section.dts') |
| 3050 | |
| 3051 | self.assertIn('image', control.images) |
| 3052 | image = control.images['image'] |
| 3053 | entries = image.GetEntries() |
| 3054 | self.assertEqual(3, len(entries)) |
| 3055 | |
| 3056 | # First u-boot |
| 3057 | self.assertIn('u-boot', entries) |
| 3058 | entry = entries['u-boot'] |
| 3059 | self.assertEqual(0, entry.offset) |
| 3060 | self.assertEqual(0, entry.image_pos) |
| 3061 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 3062 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3063 | |
| 3064 | # Section0 |
| 3065 | self.assertIn('section0', entries) |
| 3066 | section0 = entries['section0'] |
| 3067 | self.assertEqual(0x10, section0.offset) |
| 3068 | self.assertEqual(0x10, section0.image_pos) |
| 3069 | self.assertEqual(len(U_BOOT_DATA), section0.size) |
| 3070 | |
| 3071 | # Second u-boot |
| 3072 | section_entries = section0.GetEntries() |
| 3073 | self.assertIn('u-boot', section_entries) |
| 3074 | entry = section_entries['u-boot'] |
| 3075 | self.assertEqual(0, entry.offset) |
| 3076 | self.assertEqual(0x10, entry.image_pos) |
| 3077 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 3078 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3079 | |
| 3080 | # Section1 |
| 3081 | self.assertIn('section1', entries) |
| 3082 | section1 = entries['section1'] |
| 3083 | self.assertEqual(0x14, section1.offset) |
| 3084 | self.assertEqual(0x14, section1.image_pos) |
| 3085 | self.assertEqual(0x20, section1.size) |
| 3086 | |
| 3087 | # Second u-boot |
| 3088 | section_entries = section1.GetEntries() |
| 3089 | self.assertIn('u-boot', section_entries) |
| 3090 | entry = section_entries['u-boot'] |
| 3091 | self.assertEqual(0, entry.offset) |
| 3092 | self.assertEqual(0x14, entry.image_pos) |
| 3093 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 3094 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3095 | |
| 3096 | # Section2 |
| 3097 | self.assertIn('section2', section_entries) |
| 3098 | section2 = section_entries['section2'] |
| 3099 | self.assertEqual(0x4, section2.offset) |
| 3100 | self.assertEqual(0x18, section2.image_pos) |
| 3101 | self.assertEqual(4, section2.size) |
| 3102 | |
| 3103 | # Third u-boot |
| 3104 | section_entries = section2.GetEntries() |
| 3105 | self.assertIn('u-boot', section_entries) |
| 3106 | entry = section_entries['u-boot'] |
| 3107 | self.assertEqual(0, entry.offset) |
| 3108 | self.assertEqual(0x18, entry.image_pos) |
| 3109 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 3110 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3111 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3112 | def _RunReplaceCmd(self, entry_name, data, decomp=True, allow_resize=True, |
| 3113 | dts='132_replace.dts'): |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3114 | """Replace an entry in an image |
| 3115 | |
| 3116 | This writes the entry data to update it, then opens the updated file and |
| 3117 | returns the value that it now finds there. |
| 3118 | |
| 3119 | Args: |
| 3120 | entry_name: Entry name to replace |
| 3121 | data: Data to replace it with |
| 3122 | decomp: True to compress the data if needed, False if data is |
| 3123 | already compressed so should be used as is |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3124 | allow_resize: True to allow entries to change size, False to raise |
| 3125 | an exception |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3126 | |
| 3127 | Returns: |
| 3128 | Tuple: |
| 3129 | data from entry |
| 3130 | data from fdtmap (excluding header) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3131 | Image object that was modified |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3132 | """ |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3133 | dtb_data = self._DoReadFileDtb(dts, use_real_dtb=True, |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3134 | update_dtb=True)[1] |
| 3135 | |
| 3136 | self.assertIn('image', control.images) |
| 3137 | image = control.images['image'] |
| 3138 | entries = image.GetEntries() |
| 3139 | orig_dtb_data = entries['u-boot-dtb'].data |
| 3140 | orig_fdtmap_data = entries['fdtmap'].data |
| 3141 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3142 | image_fname = tools.get_output_filename('image.bin') |
| 3143 | updated_fname = tools.get_output_filename('image-updated.bin') |
| 3144 | tools.write_file(updated_fname, tools.read_file(image_fname)) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3145 | image = control.WriteEntry(updated_fname, entry_name, data, decomp, |
| 3146 | allow_resize) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3147 | data = control.ReadEntry(updated_fname, entry_name, decomp) |
| 3148 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3149 | # The DT data should not change unless resized: |
| 3150 | if not allow_resize: |
| 3151 | new_dtb_data = entries['u-boot-dtb'].data |
| 3152 | self.assertEqual(new_dtb_data, orig_dtb_data) |
| 3153 | new_fdtmap_data = entries['fdtmap'].data |
| 3154 | self.assertEqual(new_fdtmap_data, orig_fdtmap_data) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3155 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3156 | return data, orig_fdtmap_data[fdtmap.FDTMAP_HDR_LEN:], image |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3157 | |
| 3158 | def testReplaceSimple(self): |
| 3159 | """Test replacing a single file""" |
| 3160 | expected = b'x' * len(U_BOOT_DATA) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3161 | data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', expected, |
| 3162 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3163 | self.assertEqual(expected, data) |
| 3164 | |
| 3165 | # Test that the state looks right. There should be an FDT for the fdtmap |
| 3166 | # that we jsut read back in, and it should match what we find in the |
| 3167 | # 'control' tables. Checking for an FDT that does not exist should |
| 3168 | # return None. |
| 3169 | path, fdtmap = state.GetFdtContents('fdtmap') |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3170 | self.assertIsNotNone(path) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3171 | self.assertEqual(expected_fdtmap, fdtmap) |
| 3172 | |
| 3173 | dtb = state.GetFdtForEtype('fdtmap') |
| 3174 | self.assertEqual(dtb.GetContents(), fdtmap) |
| 3175 | |
| 3176 | missing_path, missing_fdtmap = state.GetFdtContents('missing') |
| 3177 | self.assertIsNone(missing_path) |
| 3178 | self.assertIsNone(missing_fdtmap) |
| 3179 | |
| 3180 | missing_dtb = state.GetFdtForEtype('missing') |
| 3181 | self.assertIsNone(missing_dtb) |
| 3182 | |
| 3183 | self.assertEqual('/binman', state.fdt_path_prefix) |
| 3184 | |
| 3185 | def testReplaceResizeFail(self): |
| 3186 | """Test replacing a file by something larger""" |
| 3187 | expected = U_BOOT_DATA + b'x' |
| 3188 | with self.assertRaises(ValueError) as e: |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3189 | self._RunReplaceCmd('u-boot', expected, allow_resize=False, |
| 3190 | dts='139_replace_repack.dts') |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3191 | self.assertIn("Node '/u-boot': Entry data size does not match, but resize is disabled", |
| 3192 | str(e.exception)) |
| 3193 | |
| 3194 | def testReplaceMulti(self): |
| 3195 | """Test replacing entry data where multiple images are generated""" |
| 3196 | data = self._DoReadFileDtb('133_replace_multi.dts', use_real_dtb=True, |
| 3197 | update_dtb=True)[0] |
| 3198 | expected = b'x' * len(U_BOOT_DATA) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3199 | updated_fname = tools.get_output_filename('image-updated.bin') |
| 3200 | tools.write_file(updated_fname, data) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3201 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3202 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3203 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3204 | data = control.ReadEntry(updated_fname, entry_name) |
| 3205 | self.assertEqual(expected, data) |
| 3206 | |
| 3207 | # Check the state looks right. |
| 3208 | self.assertEqual('/binman/image', state.fdt_path_prefix) |
| 3209 | |
| 3210 | # Now check we can write the first image |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3211 | image_fname = tools.get_output_filename('first-image.bin') |
| 3212 | updated_fname = tools.get_output_filename('first-updated.bin') |
| 3213 | tools.write_file(updated_fname, tools.read_file(image_fname)) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3214 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3215 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3216 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3217 | data = control.ReadEntry(updated_fname, entry_name) |
| 3218 | self.assertEqual(expected, data) |
| 3219 | |
| 3220 | # Check the state looks right. |
| 3221 | self.assertEqual('/binman/first-image', state.fdt_path_prefix) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 3222 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 3223 | def testUpdateFdtAllRepack(self): |
| 3224 | """Test that all device trees are updated with offset/size info""" |
| 3225 | data = self._DoReadFileRealDtb('134_fdt_update_all_repack.dts') |
| 3226 | SECTION_SIZE = 0x300 |
| 3227 | DTB_SIZE = 602 |
| 3228 | FDTMAP_SIZE = 608 |
| 3229 | base_expected = { |
| 3230 | 'offset': 0, |
| 3231 | 'size': SECTION_SIZE + DTB_SIZE * 2 + FDTMAP_SIZE, |
| 3232 | 'image-pos': 0, |
| 3233 | 'section:offset': 0, |
| 3234 | 'section:size': SECTION_SIZE, |
| 3235 | 'section:image-pos': 0, |
| 3236 | 'section/u-boot-dtb:offset': 4, |
| 3237 | 'section/u-boot-dtb:size': 636, |
| 3238 | 'section/u-boot-dtb:image-pos': 4, |
| 3239 | 'u-boot-spl-dtb:offset': SECTION_SIZE, |
| 3240 | 'u-boot-spl-dtb:size': DTB_SIZE, |
| 3241 | 'u-boot-spl-dtb:image-pos': SECTION_SIZE, |
| 3242 | 'u-boot-tpl-dtb:offset': SECTION_SIZE + DTB_SIZE, |
| 3243 | 'u-boot-tpl-dtb:image-pos': SECTION_SIZE + DTB_SIZE, |
| 3244 | 'u-boot-tpl-dtb:size': DTB_SIZE, |
| 3245 | 'fdtmap:offset': SECTION_SIZE + DTB_SIZE * 2, |
| 3246 | 'fdtmap:size': FDTMAP_SIZE, |
| 3247 | 'fdtmap:image-pos': SECTION_SIZE + DTB_SIZE * 2, |
| 3248 | } |
| 3249 | main_expected = { |
| 3250 | 'section:orig-size': SECTION_SIZE, |
| 3251 | 'section/u-boot-dtb:orig-offset': 4, |
| 3252 | } |
| 3253 | |
| 3254 | # We expect three device-tree files in the output, with the first one |
| 3255 | # within a fixed-size section. |
| 3256 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 3257 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 3258 | # main U-Boot tree. All three should have the same positions and offset |
| 3259 | # except that the main tree should include the main_expected properties |
| 3260 | start = 4 |
| 3261 | for item in ['', 'spl', 'tpl', None]: |
| 3262 | if item is None: |
| 3263 | start += 16 # Move past fdtmap header |
| 3264 | dtb = fdt.Fdt.FromData(data[start:]) |
| 3265 | dtb.Scan() |
| 3266 | props = self._GetPropTree(dtb, |
| 3267 | BASE_DTB_PROPS + REPACK_DTB_PROPS + ['spl', 'tpl'], |
| 3268 | prefix='/' if item is None else '/binman/') |
| 3269 | expected = dict(base_expected) |
| 3270 | if item: |
| 3271 | expected[item] = 0 |
| 3272 | else: |
| 3273 | # Main DTB and fdtdec should include the 'orig-' properties |
| 3274 | expected.update(main_expected) |
| 3275 | # Helpful for debugging: |
| 3276 | #for prop in sorted(props): |
| 3277 | #print('prop %s %s %s' % (prop, props[prop], expected[prop])) |
| 3278 | self.assertEqual(expected, props) |
| 3279 | if item == '': |
| 3280 | start = SECTION_SIZE |
| 3281 | else: |
| 3282 | start += dtb._fdt_obj.totalsize() |
| 3283 | |
Simon Glass | 1145376 | 2019-07-20 12:23:55 -0600 | [diff] [blame] | 3284 | def testFdtmapHeaderMiddle(self): |
| 3285 | """Test an FDT map in the middle of an image when it should be at end""" |
| 3286 | with self.assertRaises(ValueError) as e: |
| 3287 | self._DoReadFileRealDtb('135_fdtmap_hdr_middle.dts') |
| 3288 | self.assertIn("Invalid sibling order 'middle' for image-header: Must be at 'end' to match location", |
| 3289 | str(e.exception)) |
| 3290 | |
| 3291 | def testFdtmapHeaderStartBad(self): |
| 3292 | """Test an FDT map in middle of an image when it should be at start""" |
| 3293 | with self.assertRaises(ValueError) as e: |
| 3294 | self._DoReadFileRealDtb('136_fdtmap_hdr_startbad.dts') |
| 3295 | self.assertIn("Invalid sibling order 'end' for image-header: Must be at 'start' to match location", |
| 3296 | str(e.exception)) |
| 3297 | |
| 3298 | def testFdtmapHeaderEndBad(self): |
| 3299 | """Test an FDT map at the start of an image when it should be at end""" |
| 3300 | with self.assertRaises(ValueError) as e: |
| 3301 | self._DoReadFileRealDtb('137_fdtmap_hdr_endbad.dts') |
| 3302 | self.assertIn("Invalid sibling order 'start' for image-header: Must be at 'end' to match location", |
| 3303 | str(e.exception)) |
| 3304 | |
| 3305 | def testFdtmapHeaderNoSize(self): |
| 3306 | """Test an image header at the end of an image with undefined size""" |
| 3307 | self._DoReadFileRealDtb('138_fdtmap_hdr_nosize.dts') |
| 3308 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3309 | def testReplaceResize(self): |
| 3310 | """Test replacing a single file in an entry with a larger file""" |
| 3311 | expected = U_BOOT_DATA + b'x' |
| 3312 | data, _, image = self._RunReplaceCmd('u-boot', expected, |
| 3313 | dts='139_replace_repack.dts') |
| 3314 | self.assertEqual(expected, data) |
| 3315 | |
| 3316 | entries = image.GetEntries() |
| 3317 | dtb_data = entries['u-boot-dtb'].data |
| 3318 | dtb = fdt.Fdt.FromData(dtb_data) |
| 3319 | dtb.Scan() |
| 3320 | |
| 3321 | # The u-boot section should now be larger in the dtb |
| 3322 | node = dtb.GetNode('/binman/u-boot') |
| 3323 | self.assertEqual(len(expected), fdt_util.GetInt(node, 'size')) |
| 3324 | |
| 3325 | # Same for the fdtmap |
| 3326 | fdata = entries['fdtmap'].data |
| 3327 | fdtb = fdt.Fdt.FromData(fdata[fdtmap.FDTMAP_HDR_LEN:]) |
| 3328 | fdtb.Scan() |
| 3329 | fnode = fdtb.GetNode('/u-boot') |
| 3330 | self.assertEqual(len(expected), fdt_util.GetInt(fnode, 'size')) |
| 3331 | |
| 3332 | def testReplaceResizeNoRepack(self): |
| 3333 | """Test replacing an entry with a larger file when not allowed""" |
| 3334 | expected = U_BOOT_DATA + b'x' |
| 3335 | with self.assertRaises(ValueError) as e: |
| 3336 | self._RunReplaceCmd('u-boot', expected) |
| 3337 | self.assertIn('Entry data size does not match, but allow-repack is not present for this image', |
| 3338 | str(e.exception)) |
| 3339 | |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 3340 | def testEntryShrink(self): |
| 3341 | """Test contracting an entry after it is packed""" |
| 3342 | try: |
| 3343 | state.SetAllowEntryContraction(True) |
| 3344 | data = self._DoReadFileDtb('140_entry_shrink.dts', |
| 3345 | update_dtb=True)[0] |
| 3346 | finally: |
| 3347 | state.SetAllowEntryContraction(False) |
| 3348 | self.assertEqual(b'a', data[:1]) |
| 3349 | self.assertEqual(U_BOOT_DATA, data[1:1 + len(U_BOOT_DATA)]) |
| 3350 | self.assertEqual(b'a', data[-1:]) |
| 3351 | |
| 3352 | def testEntryShrinkFail(self): |
| 3353 | """Test not being allowed to contract an entry after it is packed""" |
| 3354 | data = self._DoReadFileDtb('140_entry_shrink.dts', update_dtb=True)[0] |
| 3355 | |
| 3356 | # In this case there is a spare byte at the end of the data. The size of |
| 3357 | # the contents is only 1 byte but we still have the size before it |
| 3358 | # shrunk. |
| 3359 | self.assertEqual(b'a\0', data[:2]) |
| 3360 | self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)]) |
| 3361 | self.assertEqual(b'a\0', data[-2:]) |
| 3362 | |
Simon Glass | 70e3298 | 2019-07-20 12:24:01 -0600 | [diff] [blame] | 3363 | def testDescriptorOffset(self): |
| 3364 | """Test that the Intel descriptor is always placed at at the start""" |
| 3365 | data = self._DoReadFileDtb('141_descriptor_offset.dts') |
| 3366 | image = control.images['image'] |
| 3367 | entries = image.GetEntries() |
| 3368 | desc = entries['intel-descriptor'] |
| 3369 | self.assertEqual(0xff800000, desc.offset); |
| 3370 | self.assertEqual(0xff800000, desc.image_pos); |
| 3371 | |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 3372 | def testReplaceCbfs(self): |
| 3373 | """Test replacing a single file in CBFS without changing the size""" |
| 3374 | self._CheckLz4() |
| 3375 | expected = b'x' * len(U_BOOT_DATA) |
| 3376 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3377 | updated_fname = tools.get_output_filename('image-updated.bin') |
| 3378 | tools.write_file(updated_fname, data) |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 3379 | entry_name = 'section/cbfs/u-boot' |
| 3380 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3381 | allow_resize=True) |
| 3382 | data = control.ReadEntry(updated_fname, entry_name) |
| 3383 | self.assertEqual(expected, data) |
| 3384 | |
| 3385 | def testReplaceResizeCbfs(self): |
| 3386 | """Test replacing a single file in CBFS with one of a different size""" |
| 3387 | self._CheckLz4() |
| 3388 | expected = U_BOOT_DATA + b'x' |
| 3389 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3390 | updated_fname = tools.get_output_filename('image-updated.bin') |
| 3391 | tools.write_file(updated_fname, data) |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 3392 | entry_name = 'section/cbfs/u-boot' |
| 3393 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3394 | allow_resize=True) |
| 3395 | data = control.ReadEntry(updated_fname, entry_name) |
| 3396 | self.assertEqual(expected, data) |
| 3397 | |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3398 | def _SetupForReplace(self): |
| 3399 | """Set up some files to use to replace entries |
| 3400 | |
| 3401 | This generates an image, copies it to a new file, extracts all the files |
| 3402 | in it and updates some of them |
| 3403 | |
| 3404 | Returns: |
| 3405 | List |
| 3406 | Image filename |
| 3407 | Output directory |
| 3408 | Expected values for updated entries, each a string |
| 3409 | """ |
| 3410 | data = self._DoReadFileRealDtb('143_replace_all.dts') |
| 3411 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3412 | updated_fname = tools.get_output_filename('image-updated.bin') |
| 3413 | tools.write_file(updated_fname, data) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3414 | |
| 3415 | outdir = os.path.join(self._indir, 'extract') |
| 3416 | einfos = control.ExtractEntries(updated_fname, None, outdir, []) |
| 3417 | |
| 3418 | expected1 = b'x' + U_BOOT_DATA + b'y' |
| 3419 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3420 | tools.write_file(u_boot_fname1, expected1) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3421 | |
| 3422 | expected2 = b'a' + U_BOOT_DATA + b'b' |
| 3423 | u_boot_fname2 = os.path.join(outdir, 'u-boot2') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3424 | tools.write_file(u_boot_fname2, expected2) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3425 | |
| 3426 | expected_text = b'not the same text' |
| 3427 | text_fname = os.path.join(outdir, 'text') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3428 | tools.write_file(text_fname, expected_text) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3429 | |
| 3430 | dtb_fname = os.path.join(outdir, 'u-boot-dtb') |
| 3431 | dtb = fdt.FdtScan(dtb_fname) |
| 3432 | node = dtb.GetNode('/binman/text') |
| 3433 | node.AddString('my-property', 'the value') |
| 3434 | dtb.Sync(auto_resize=True) |
| 3435 | dtb.Flush() |
| 3436 | |
| 3437 | return updated_fname, outdir, expected1, expected2, expected_text |
| 3438 | |
| 3439 | def _CheckReplaceMultiple(self, entry_paths): |
| 3440 | """Handle replacing the contents of multiple entries |
| 3441 | |
| 3442 | Args: |
| 3443 | entry_paths: List of entry paths to replace |
| 3444 | |
| 3445 | Returns: |
| 3446 | List |
| 3447 | Dict of entries in the image: |
| 3448 | key: Entry name |
| 3449 | Value: Entry object |
| 3450 | Expected values for updated entries, each a string |
| 3451 | """ |
| 3452 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3453 | self._SetupForReplace()) |
| 3454 | control.ReplaceEntries(updated_fname, None, outdir, entry_paths) |
| 3455 | |
| 3456 | image = Image.FromFile(updated_fname) |
| 3457 | image.LoadData() |
| 3458 | return image.GetEntries(), expected1, expected2, expected_text |
| 3459 | |
| 3460 | def testReplaceAll(self): |
| 3461 | """Test replacing the contents of all entries""" |
| 3462 | entries, expected1, expected2, expected_text = ( |
| 3463 | self._CheckReplaceMultiple([])) |
| 3464 | data = entries['u-boot'].data |
| 3465 | self.assertEqual(expected1, data) |
| 3466 | |
| 3467 | data = entries['u-boot2'].data |
| 3468 | self.assertEqual(expected2, data) |
| 3469 | |
| 3470 | data = entries['text'].data |
| 3471 | self.assertEqual(expected_text, data) |
| 3472 | |
| 3473 | # Check that the device tree is updated |
| 3474 | data = entries['u-boot-dtb'].data |
| 3475 | dtb = fdt.Fdt.FromData(data) |
| 3476 | dtb.Scan() |
| 3477 | node = dtb.GetNode('/binman/text') |
| 3478 | self.assertEqual('the value', node.props['my-property'].value) |
| 3479 | |
| 3480 | def testReplaceSome(self): |
| 3481 | """Test replacing the contents of a few entries""" |
| 3482 | entries, expected1, expected2, expected_text = ( |
| 3483 | self._CheckReplaceMultiple(['u-boot2', 'text'])) |
| 3484 | |
| 3485 | # This one should not change |
| 3486 | data = entries['u-boot'].data |
| 3487 | self.assertEqual(U_BOOT_DATA, data) |
| 3488 | |
| 3489 | data = entries['u-boot2'].data |
| 3490 | self.assertEqual(expected2, data) |
| 3491 | |
| 3492 | data = entries['text'].data |
| 3493 | self.assertEqual(expected_text, data) |
| 3494 | |
| 3495 | def testReplaceCmd(self): |
| 3496 | """Test replacing a file fron an image on the command line""" |
| 3497 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3498 | |
| 3499 | try: |
| 3500 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3501 | |
| 3502 | fname = os.path.join(tmpdir, 'update-u-boot.bin') |
| 3503 | expected = b'x' * len(U_BOOT_DATA) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3504 | tools.write_file(fname, expected) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3505 | |
| 3506 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', '-f', fname) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3507 | data = tools.read_file(updated_fname) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3508 | self.assertEqual(expected, data[:len(expected)]) |
| 3509 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3510 | self.assertFalse(os.path.exists(map_fname)) |
| 3511 | finally: |
| 3512 | shutil.rmtree(tmpdir) |
| 3513 | |
| 3514 | def testReplaceCmdSome(self): |
| 3515 | """Test replacing some files fron an image on the command line""" |
| 3516 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3517 | self._SetupForReplace()) |
| 3518 | |
| 3519 | self._DoBinman('replace', '-i', updated_fname, '-I', outdir, |
| 3520 | 'u-boot2', 'text') |
| 3521 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3522 | tools.prepare_output_dir(None) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3523 | image = Image.FromFile(updated_fname) |
| 3524 | image.LoadData() |
| 3525 | entries = image.GetEntries() |
| 3526 | |
| 3527 | # This one should not change |
| 3528 | data = entries['u-boot'].data |
| 3529 | self.assertEqual(U_BOOT_DATA, data) |
| 3530 | |
| 3531 | data = entries['u-boot2'].data |
| 3532 | self.assertEqual(expected2, data) |
| 3533 | |
| 3534 | data = entries['text'].data |
| 3535 | self.assertEqual(expected_text, data) |
| 3536 | |
| 3537 | def testReplaceMissing(self): |
| 3538 | """Test replacing entries where the file is missing""" |
| 3539 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3540 | self._SetupForReplace()) |
| 3541 | |
| 3542 | # Remove one of the files, to generate a warning |
| 3543 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
| 3544 | os.remove(u_boot_fname1) |
| 3545 | |
| 3546 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3547 | control.ReplaceEntries(updated_fname, None, outdir, []) |
| 3548 | self.assertIn("Skipping entry '/u-boot' from missing file", |
Simon Glass | 6e02f7c | 2020-07-09 18:39:39 -0600 | [diff] [blame] | 3549 | stderr.getvalue()) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3550 | |
| 3551 | def testReplaceCmdMap(self): |
| 3552 | """Test replacing a file fron an image on the command line""" |
| 3553 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3554 | |
| 3555 | try: |
| 3556 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3557 | |
| 3558 | fname = os.path.join(self._indir, 'update-u-boot.bin') |
| 3559 | expected = b'x' * len(U_BOOT_DATA) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3560 | tools.write_file(fname, expected) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3561 | |
| 3562 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', |
| 3563 | '-f', fname, '-m') |
| 3564 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3565 | self.assertTrue(os.path.exists(map_fname)) |
| 3566 | finally: |
| 3567 | shutil.rmtree(tmpdir) |
| 3568 | |
| 3569 | def testReplaceNoEntryPaths(self): |
| 3570 | """Test replacing an entry without an entry path""" |
| 3571 | self._DoReadFileRealDtb('143_replace_all.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3572 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3573 | with self.assertRaises(ValueError) as e: |
| 3574 | control.ReplaceEntries(image_fname, 'fname', None, []) |
| 3575 | self.assertIn('Must specify an entry path to read with -f', |
| 3576 | str(e.exception)) |
| 3577 | |
| 3578 | def testReplaceTooManyEntryPaths(self): |
| 3579 | """Test extracting some entries""" |
| 3580 | self._DoReadFileRealDtb('143_replace_all.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3581 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3582 | with self.assertRaises(ValueError) as e: |
| 3583 | control.ReplaceEntries(image_fname, 'fname', None, ['a', 'b']) |
| 3584 | self.assertIn('Must specify exactly one entry path to write with -f', |
| 3585 | str(e.exception)) |
| 3586 | |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 3587 | def testPackReset16(self): |
| 3588 | """Test that an image with an x86 reset16 region can be created""" |
| 3589 | data = self._DoReadFile('144_x86_reset16.dts') |
| 3590 | self.assertEqual(X86_RESET16_DATA, data[:len(X86_RESET16_DATA)]) |
| 3591 | |
| 3592 | def testPackReset16Spl(self): |
| 3593 | """Test that an image with an x86 reset16-spl region can be created""" |
| 3594 | data = self._DoReadFile('145_x86_reset16_spl.dts') |
| 3595 | self.assertEqual(X86_RESET16_SPL_DATA, data[:len(X86_RESET16_SPL_DATA)]) |
| 3596 | |
| 3597 | def testPackReset16Tpl(self): |
| 3598 | """Test that an image with an x86 reset16-tpl region can be created""" |
| 3599 | data = self._DoReadFile('146_x86_reset16_tpl.dts') |
| 3600 | self.assertEqual(X86_RESET16_TPL_DATA, data[:len(X86_RESET16_TPL_DATA)]) |
| 3601 | |
Simon Glass | 232f90c | 2019-08-24 07:22:50 -0600 | [diff] [blame] | 3602 | def testPackIntelFit(self): |
| 3603 | """Test that an image with an Intel FIT and pointer can be created""" |
| 3604 | data = self._DoReadFile('147_intel_fit.dts') |
| 3605 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3606 | fit = data[16:32]; |
| 3607 | self.assertEqual(b'_FIT_ \x01\x00\x00\x00\x00\x01\x80}' , fit) |
| 3608 | ptr = struct.unpack('<i', data[0x40:0x44])[0] |
| 3609 | |
| 3610 | image = control.images['image'] |
| 3611 | entries = image.GetEntries() |
| 3612 | expected_ptr = entries['intel-fit'].image_pos - (1 << 32) |
| 3613 | self.assertEqual(expected_ptr, ptr) |
| 3614 | |
| 3615 | def testPackIntelFitMissing(self): |
| 3616 | """Test detection of a FIT pointer with not FIT region""" |
| 3617 | with self.assertRaises(ValueError) as e: |
| 3618 | self._DoReadFile('148_intel_fit_missing.dts') |
| 3619 | self.assertIn("'intel-fit-ptr' section must have an 'intel-fit' sibling", |
| 3620 | str(e.exception)) |
| 3621 | |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3622 | def _CheckSymbolsTplSection(self, dts, expected_vals): |
| 3623 | data = self._DoReadFile(dts) |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 3624 | sym_values = struct.pack('<LLQLL', elf.BINMAN_SYM_MAGIC_VALUE, *expected_vals) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3625 | upto1 = 4 + len(U_BOOT_SPL_DATA) |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 3626 | expected1 = tools.get_bytes(0xff, 4) + sym_values + U_BOOT_SPL_DATA[24:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3627 | self.assertEqual(expected1, data[:upto1]) |
| 3628 | |
| 3629 | upto2 = upto1 + 1 + len(U_BOOT_SPL_DATA) |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 3630 | expected2 = tools.get_bytes(0xff, 1) + sym_values + U_BOOT_SPL_DATA[24:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3631 | self.assertEqual(expected2, data[upto1:upto2]) |
| 3632 | |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 3633 | upto3 = 0x3c + len(U_BOOT_DATA) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3634 | expected3 = tools.get_bytes(0xff, 1) + U_BOOT_DATA |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3635 | self.assertEqual(expected3, data[upto2:upto3]) |
| 3636 | |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 3637 | expected4 = sym_values + U_BOOT_TPL_DATA[24:] |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3638 | self.assertEqual(expected4, data[upto3:upto3 + len(U_BOOT_TPL_DATA)]) |
| 3639 | |
| 3640 | def testSymbolsTplSection(self): |
| 3641 | """Test binman can assign symbols embedded in U-Boot TPL in a section""" |
| 3642 | self._SetupSplElf('u_boot_binman_syms') |
| 3643 | self._SetupTplElf('u_boot_binman_syms') |
| 3644 | self._CheckSymbolsTplSection('149_symbols_tpl.dts', |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 3645 | [0x04, 0x20, 0x10 + 0x3c, 0x04]) |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3646 | |
| 3647 | def testSymbolsTplSectionX86(self): |
| 3648 | """Test binman can assign symbols in a section with end-at-4gb""" |
| 3649 | self._SetupSplElf('u_boot_binman_syms_x86') |
| 3650 | self._SetupTplElf('u_boot_binman_syms_x86') |
| 3651 | self._CheckSymbolsTplSection('155_symbols_tpl_x86.dts', |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 3652 | [0xffffff04, 0xffffff20, 0xffffff3c, |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3653 | 0x04]) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3654 | |
Simon Glass | 98c5957 | 2019-08-24 07:23:03 -0600 | [diff] [blame] | 3655 | def testPackX86RomIfwiSectiom(self): |
| 3656 | """Test that a section can be placed in an IFWI region""" |
| 3657 | self._SetupIfwi('fitimage.bin') |
| 3658 | data = self._DoReadFile('151_x86_rom_ifwi_section.dts') |
| 3659 | self._CheckIfwi(data) |
| 3660 | |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3661 | def testPackFspM(self): |
| 3662 | """Test that an image with a FSP memory-init binary can be created""" |
| 3663 | data = self._DoReadFile('152_intel_fsp_m.dts') |
| 3664 | self.assertEqual(FSP_M_DATA, data[:len(FSP_M_DATA)]) |
| 3665 | |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 3666 | def testPackFspS(self): |
| 3667 | """Test that an image with a FSP silicon-init binary can be created""" |
| 3668 | data = self._DoReadFile('153_intel_fsp_s.dts') |
| 3669 | self.assertEqual(FSP_S_DATA, data[:len(FSP_S_DATA)]) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3670 | |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 3671 | def testPackFspT(self): |
| 3672 | """Test that an image with a FSP temp-ram-init binary can be created""" |
| 3673 | data = self._DoReadFile('154_intel_fsp_t.dts') |
| 3674 | self.assertEqual(FSP_T_DATA, data[:len(FSP_T_DATA)]) |
| 3675 | |
Simon Glass | 48f3aad | 2020-07-09 18:39:31 -0600 | [diff] [blame] | 3676 | def testMkimage(self): |
| 3677 | """Test using mkimage to build an image""" |
| 3678 | data = self._DoReadFile('156_mkimage.dts') |
| 3679 | |
| 3680 | # Just check that the data appears in the file somewhere |
| 3681 | self.assertIn(U_BOOT_SPL_DATA, data) |
| 3682 | |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 3683 | def testMkimageMissing(self): |
| 3684 | """Test that binman still produces an image if mkimage is missing""" |
| 3685 | with test_util.capture_sys_output() as (_, stderr): |
| 3686 | self._DoTestFile('156_mkimage.dts', |
| 3687 | force_missing_bintools='mkimage') |
| 3688 | err = stderr.getvalue() |
| 3689 | self.assertRegex(err, |
| 3690 | "Image 'main-section'.*missing bintools.*: mkimage") |
| 3691 | |
Simon Glass | 5e56018 | 2020-07-09 18:39:36 -0600 | [diff] [blame] | 3692 | def testExtblob(self): |
| 3693 | """Test an image with an external blob""" |
| 3694 | data = self._DoReadFile('157_blob_ext.dts') |
| 3695 | self.assertEqual(REFCODE_DATA, data) |
| 3696 | |
| 3697 | def testExtblobMissing(self): |
| 3698 | """Test an image with a missing external blob""" |
| 3699 | with self.assertRaises(ValueError) as e: |
| 3700 | self._DoReadFile('158_blob_ext_missing.dts') |
| 3701 | self.assertIn("Filename 'missing-file' not found in input path", |
| 3702 | str(e.exception)) |
| 3703 | |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3704 | def testExtblobMissingOk(self): |
| 3705 | """Test an image with an missing external blob that is allowed""" |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 3706 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3707 | self._DoTestFile('158_blob_ext_missing.dts', allow_missing=True) |
| 3708 | err = stderr.getvalue() |
| 3709 | self.assertRegex(err, "Image 'main-section'.*missing.*: blob-ext") |
| 3710 | |
| 3711 | def testExtblobMissingOkSect(self): |
| 3712 | """Test an image with an missing external blob that is allowed""" |
| 3713 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3714 | self._DoTestFile('159_blob_ext_missing_sect.dts', |
| 3715 | allow_missing=True) |
| 3716 | err = stderr.getvalue() |
| 3717 | self.assertRegex(err, "Image 'main-section'.*missing.*: " |
| 3718 | "blob-ext blob-ext2") |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3719 | |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3720 | def testPackX86RomMeMissingDesc(self): |
| 3721 | """Test that an missing Intel descriptor entry is allowed""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3722 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 3723 | self._DoTestFile('164_x86_rom_me_missing.dts', allow_missing=True) |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3724 | err = stderr.getvalue() |
| 3725 | self.assertRegex(err, |
| 3726 | "Image 'main-section'.*missing.*: intel-descriptor") |
| 3727 | |
| 3728 | def testPackX86RomMissingIfwi(self): |
| 3729 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 3730 | self._SetupIfwi('fitimage.bin') |
| 3731 | pathname = os.path.join(self._indir, 'fitimage.bin') |
| 3732 | os.remove(pathname) |
| 3733 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3734 | self._DoTestFile('111_x86_rom_ifwi.dts', allow_missing=True) |
| 3735 | err = stderr.getvalue() |
| 3736 | self.assertRegex(err, "Image 'main-section'.*missing.*: intel-ifwi") |
| 3737 | |
Simon Glass | 2a0fa98 | 2022-02-11 13:23:21 -0700 | [diff] [blame] | 3738 | def testPackOverlapZero(self): |
Simon Glass | d70829a | 2020-07-09 18:39:42 -0600 | [diff] [blame] | 3739 | """Test that zero-size overlapping regions are ignored""" |
| 3740 | self._DoTestFile('160_pack_overlap_zero.dts') |
| 3741 | |
Alper Nebi Yasak | 09fb061 | 2022-02-08 01:08:04 +0300 | [diff] [blame] | 3742 | def _CheckSimpleFitData(self, fit_data, kernel_data, fdt1_data): |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3743 | # The data should be inside the FIT |
| 3744 | dtb = fdt.Fdt.FromData(fit_data) |
| 3745 | dtb.Scan() |
| 3746 | fnode = dtb.GetNode('/images/kernel') |
| 3747 | self.assertIn('data', fnode.props) |
| 3748 | |
| 3749 | fname = os.path.join(self._indir, 'fit_data.fit') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3750 | tools.write_file(fname, fit_data) |
| 3751 | out = tools.run('dumpimage', '-l', fname) |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3752 | |
| 3753 | # Check a few features to make sure the plumbing works. We don't need |
| 3754 | # to test the operation of mkimage or dumpimage here. First convert the |
| 3755 | # output into a dict where the keys are the fields printed by dumpimage |
| 3756 | # and the values are a list of values for each field |
| 3757 | lines = out.splitlines() |
| 3758 | |
| 3759 | # Converts "Compression: gzip compressed" into two groups: |
| 3760 | # 'Compression' and 'gzip compressed' |
| 3761 | re_line = re.compile(r'^ *([^:]*)(?:: *(.*))?$') |
| 3762 | vals = collections.defaultdict(list) |
| 3763 | for line in lines: |
| 3764 | mat = re_line.match(line) |
| 3765 | vals[mat.group(1)].append(mat.group(2)) |
| 3766 | |
| 3767 | self.assertEquals('FIT description: test-desc', lines[0]) |
| 3768 | self.assertIn('Created:', lines[1]) |
| 3769 | self.assertIn('Image 0 (kernel)', vals) |
| 3770 | self.assertIn('Hash value', vals) |
| 3771 | data_sizes = vals.get('Data Size') |
| 3772 | self.assertIsNotNone(data_sizes) |
| 3773 | self.assertEqual(2, len(data_sizes)) |
| 3774 | # Format is "4 Bytes = 0.00 KiB = 0.00 MiB" so take the first word |
Alper Nebi Yasak | 09fb061 | 2022-02-08 01:08:04 +0300 | [diff] [blame] | 3775 | self.assertEqual(len(kernel_data), int(data_sizes[0].split()[0])) |
| 3776 | self.assertEqual(len(fdt1_data), int(data_sizes[1].split()[0])) |
| 3777 | |
Alper Nebi Yasak | 1a0ee0f | 2022-03-27 18:31:47 +0300 | [diff] [blame] | 3778 | # Check if entry listing correctly omits /images/ |
| 3779 | image = control.images['image'] |
| 3780 | fit_entry = image.GetEntries()['fit'] |
| 3781 | subentries = list(fit_entry.GetEntries().keys()) |
| 3782 | expected = ['kernel', 'fdt-1'] |
| 3783 | self.assertEqual(expected, subentries) |
| 3784 | |
Alper Nebi Yasak | 09fb061 | 2022-02-08 01:08:04 +0300 | [diff] [blame] | 3785 | def testSimpleFit(self): |
| 3786 | """Test an image with a FIT inside""" |
| 3787 | data = self._DoReadFile('161_fit.dts') |
| 3788 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3789 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3790 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3791 | |
| 3792 | self._CheckSimpleFitData(fit_data, U_BOOT_DATA, U_BOOT_SPL_DTB_DATA) |
| 3793 | |
| 3794 | def testSimpleFitExpandsSubentries(self): |
| 3795 | """Test that FIT images expand their subentries""" |
| 3796 | data = self._DoReadFileDtb('161_fit.dts', use_expanded=True)[0] |
| 3797 | self.assertEqual(U_BOOT_EXP_DATA, data[:len(U_BOOT_EXP_DATA)]) |
| 3798 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3799 | fit_data = data[len(U_BOOT_EXP_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3800 | |
| 3801 | self._CheckSimpleFitData(fit_data, U_BOOT_EXP_DATA, U_BOOT_SPL_DTB_DATA) |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3802 | |
Alper Nebi Yasak | ac873ed | 2022-02-08 01:08:08 +0300 | [diff] [blame] | 3803 | def testSimpleFitImagePos(self): |
| 3804 | """Test that we have correct image-pos for FIT subentries""" |
| 3805 | data, _, _, out_dtb_fname = self._DoReadFileDtb('161_fit.dts', |
| 3806 | update_dtb=True) |
| 3807 | dtb = fdt.Fdt(out_dtb_fname) |
| 3808 | dtb.Scan() |
| 3809 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
| 3810 | |
Simon Glass | b7bad18 | 2022-03-05 20:19:01 -0700 | [diff] [blame] | 3811 | self.maxDiff = None |
Alper Nebi Yasak | ac873ed | 2022-02-08 01:08:08 +0300 | [diff] [blame] | 3812 | self.assertEqual({ |
| 3813 | 'image-pos': 0, |
| 3814 | 'offset': 0, |
| 3815 | 'size': 1890, |
| 3816 | |
| 3817 | 'u-boot:image-pos': 0, |
| 3818 | 'u-boot:offset': 0, |
| 3819 | 'u-boot:size': 4, |
| 3820 | |
| 3821 | 'fit:image-pos': 4, |
| 3822 | 'fit:offset': 4, |
| 3823 | 'fit:size': 1840, |
| 3824 | |
Simon Glass | b7bad18 | 2022-03-05 20:19:01 -0700 | [diff] [blame] | 3825 | 'fit/images/kernel:image-pos': 304, |
| 3826 | 'fit/images/kernel:offset': 300, |
Alper Nebi Yasak | ac873ed | 2022-02-08 01:08:08 +0300 | [diff] [blame] | 3827 | 'fit/images/kernel:size': 4, |
| 3828 | |
Simon Glass | b7bad18 | 2022-03-05 20:19:01 -0700 | [diff] [blame] | 3829 | 'fit/images/kernel/u-boot:image-pos': 304, |
Alper Nebi Yasak | ac873ed | 2022-02-08 01:08:08 +0300 | [diff] [blame] | 3830 | 'fit/images/kernel/u-boot:offset': 0, |
| 3831 | 'fit/images/kernel/u-boot:size': 4, |
| 3832 | |
Simon Glass | b7bad18 | 2022-03-05 20:19:01 -0700 | [diff] [blame] | 3833 | 'fit/images/fdt-1:image-pos': 552, |
| 3834 | 'fit/images/fdt-1:offset': 548, |
Alper Nebi Yasak | ac873ed | 2022-02-08 01:08:08 +0300 | [diff] [blame] | 3835 | 'fit/images/fdt-1:size': 6, |
| 3836 | |
Simon Glass | b7bad18 | 2022-03-05 20:19:01 -0700 | [diff] [blame] | 3837 | 'fit/images/fdt-1/u-boot-spl-dtb:image-pos': 552, |
Alper Nebi Yasak | ac873ed | 2022-02-08 01:08:08 +0300 | [diff] [blame] | 3838 | 'fit/images/fdt-1/u-boot-spl-dtb:offset': 0, |
| 3839 | 'fit/images/fdt-1/u-boot-spl-dtb:size': 6, |
| 3840 | |
| 3841 | 'u-boot-nodtb:image-pos': 1844, |
| 3842 | 'u-boot-nodtb:offset': 1844, |
| 3843 | 'u-boot-nodtb:size': 46, |
| 3844 | }, props) |
| 3845 | |
| 3846 | # Actually check the data is where we think it is |
| 3847 | for node, expected in [ |
| 3848 | ("u-boot", U_BOOT_DATA), |
| 3849 | ("fit/images/kernel", U_BOOT_DATA), |
| 3850 | ("fit/images/kernel/u-boot", U_BOOT_DATA), |
| 3851 | ("fit/images/fdt-1", U_BOOT_SPL_DTB_DATA), |
| 3852 | ("fit/images/fdt-1/u-boot-spl-dtb", U_BOOT_SPL_DTB_DATA), |
| 3853 | ("u-boot-nodtb", U_BOOT_NODTB_DATA), |
| 3854 | ]: |
| 3855 | image_pos = props[f"{node}:image-pos"] |
| 3856 | size = props[f"{node}:size"] |
| 3857 | self.assertEqual(len(expected), size) |
| 3858 | self.assertEqual(expected, data[image_pos:image_pos+size]) |
| 3859 | |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3860 | def testFitExternal(self): |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 3861 | """Test an image with an FIT with external images""" |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3862 | data = self._DoReadFile('162_fit_external.dts') |
| 3863 | fit_data = data[len(U_BOOT_DATA):-2] # _testing is 2 bytes |
| 3864 | |
Simon Glass | 7932c88 | 2022-01-09 20:13:39 -0700 | [diff] [blame] | 3865 | # Size of the external-data region as set up by mkimage |
| 3866 | external_data_size = len(U_BOOT_DATA) + 2 |
| 3867 | expected_size = (len(U_BOOT_DATA) + 0x400 + |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3868 | tools.align(external_data_size, 4) + |
Simon Glass | 7932c88 | 2022-01-09 20:13:39 -0700 | [diff] [blame] | 3869 | len(U_BOOT_NODTB_DATA)) |
| 3870 | |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3871 | # The data should be outside the FIT |
| 3872 | dtb = fdt.Fdt.FromData(fit_data) |
| 3873 | dtb.Scan() |
| 3874 | fnode = dtb.GetNode('/images/kernel') |
| 3875 | self.assertNotIn('data', fnode.props) |
Simon Glass | 7932c88 | 2022-01-09 20:13:39 -0700 | [diff] [blame] | 3876 | self.assertEqual(len(U_BOOT_DATA), |
| 3877 | fdt_util.fdt32_to_cpu(fnode.props['data-size'].value)) |
| 3878 | fit_pos = 0x400; |
| 3879 | self.assertEqual( |
| 3880 | fit_pos, |
| 3881 | fdt_util.fdt32_to_cpu(fnode.props['data-position'].value)) |
| 3882 | |
| 3883 | self.assertEquals(expected_size, len(data)) |
| 3884 | actual_pos = len(U_BOOT_DATA) + fit_pos |
| 3885 | self.assertEqual(U_BOOT_DATA + b'aa', |
| 3886 | data[actual_pos:actual_pos + external_data_size]) |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 3887 | |
Alper Nebi Yasak | ac873ed | 2022-02-08 01:08:08 +0300 | [diff] [blame] | 3888 | def testFitExternalImagePos(self): |
| 3889 | """Test that we have correct image-pos for external FIT subentries""" |
| 3890 | data, _, _, out_dtb_fname = self._DoReadFileDtb('162_fit_external.dts', |
| 3891 | update_dtb=True) |
| 3892 | dtb = fdt.Fdt(out_dtb_fname) |
| 3893 | dtb.Scan() |
| 3894 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
| 3895 | |
| 3896 | self.assertEqual({ |
| 3897 | 'image-pos': 0, |
| 3898 | 'offset': 0, |
| 3899 | 'size': 1082, |
| 3900 | |
| 3901 | 'u-boot:image-pos': 0, |
| 3902 | 'u-boot:offset': 0, |
| 3903 | 'u-boot:size': 4, |
| 3904 | |
| 3905 | 'fit:size': 1032, |
| 3906 | 'fit:offset': 4, |
| 3907 | 'fit:image-pos': 4, |
| 3908 | |
| 3909 | 'fit/images/kernel:size': 4, |
| 3910 | 'fit/images/kernel:offset': 1024, |
| 3911 | 'fit/images/kernel:image-pos': 1028, |
| 3912 | |
| 3913 | 'fit/images/kernel/u-boot:size': 4, |
| 3914 | 'fit/images/kernel/u-boot:offset': 0, |
| 3915 | 'fit/images/kernel/u-boot:image-pos': 1028, |
| 3916 | |
| 3917 | 'fit/images/fdt-1:size': 2, |
| 3918 | 'fit/images/fdt-1:offset': 1028, |
| 3919 | 'fit/images/fdt-1:image-pos': 1032, |
| 3920 | |
| 3921 | 'fit/images/fdt-1/_testing:size': 2, |
| 3922 | 'fit/images/fdt-1/_testing:offset': 0, |
| 3923 | 'fit/images/fdt-1/_testing:image-pos': 1032, |
| 3924 | |
| 3925 | 'u-boot-nodtb:image-pos': 1036, |
| 3926 | 'u-boot-nodtb:offset': 1036, |
| 3927 | 'u-boot-nodtb:size': 46, |
| 3928 | }, props) |
| 3929 | |
| 3930 | # Actually check the data is where we think it is |
| 3931 | for node, expected in [ |
| 3932 | ("u-boot", U_BOOT_DATA), |
| 3933 | ("fit/images/kernel", U_BOOT_DATA), |
| 3934 | ("fit/images/kernel/u-boot", U_BOOT_DATA), |
| 3935 | ("fit/images/fdt-1", b'aa'), |
| 3936 | ("fit/images/fdt-1/_testing", b'aa'), |
| 3937 | ("u-boot-nodtb", U_BOOT_NODTB_DATA), |
| 3938 | ]: |
| 3939 | image_pos = props[f"{node}:image-pos"] |
| 3940 | size = props[f"{node}:size"] |
| 3941 | self.assertEqual(len(expected), size) |
| 3942 | self.assertEqual(expected, data[image_pos:image_pos+size]) |
| 3943 | |
Simon Glass | 66152ce | 2022-01-09 20:14:09 -0700 | [diff] [blame] | 3944 | def testFitMissing(self): |
| 3945 | """Test that binman still produces a FIT image if mkimage is missing""" |
| 3946 | with test_util.capture_sys_output() as (_, stderr): |
| 3947 | self._DoTestFile('162_fit_external.dts', |
| 3948 | force_missing_bintools='mkimage') |
| 3949 | err = stderr.getvalue() |
| 3950 | self.assertRegex(err, |
| 3951 | "Image 'main-section'.*missing bintools.*: mkimage") |
| 3952 | |
Alper Nebi Yasak | 6aae239 | 2020-08-31 12:58:18 +0300 | [diff] [blame] | 3953 | def testSectionIgnoreHashSignature(self): |
| 3954 | """Test that sections ignore hash, signature nodes for its data""" |
| 3955 | data = self._DoReadFile('165_section_ignore_hash_signature.dts') |
| 3956 | expected = (U_BOOT_DATA + U_BOOT_DATA) |
| 3957 | self.assertEqual(expected, data) |
| 3958 | |
Alper Nebi Yasak | c503060 | 2020-08-31 12:58:19 +0300 | [diff] [blame] | 3959 | def testPadInSections(self): |
| 3960 | """Test pad-before, pad-after for entries in sections""" |
Simon Glass | d12599d | 2020-10-26 17:40:09 -0600 | [diff] [blame] | 3961 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 3962 | '166_pad_in_sections.dts', update_dtb=True) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 3963 | expected = (U_BOOT_DATA + tools.get_bytes(ord('!'), 12) + |
| 3964 | U_BOOT_DATA + tools.get_bytes(ord('!'), 6) + |
Alper Nebi Yasak | c503060 | 2020-08-31 12:58:19 +0300 | [diff] [blame] | 3965 | U_BOOT_DATA) |
| 3966 | self.assertEqual(expected, data) |
| 3967 | |
Simon Glass | d12599d | 2020-10-26 17:40:09 -0600 | [diff] [blame] | 3968 | dtb = fdt.Fdt(out_dtb_fname) |
| 3969 | dtb.Scan() |
| 3970 | props = self._GetPropTree(dtb, ['size', 'image-pos', 'offset']) |
| 3971 | expected = { |
| 3972 | 'image-pos': 0, |
| 3973 | 'offset': 0, |
| 3974 | 'size': 12 + 6 + 3 * len(U_BOOT_DATA), |
| 3975 | |
| 3976 | 'section:image-pos': 0, |
| 3977 | 'section:offset': 0, |
| 3978 | 'section:size': 12 + 6 + 3 * len(U_BOOT_DATA), |
| 3979 | |
| 3980 | 'section/before:image-pos': 0, |
| 3981 | 'section/before:offset': 0, |
| 3982 | 'section/before:size': len(U_BOOT_DATA), |
| 3983 | |
| 3984 | 'section/u-boot:image-pos': 4, |
| 3985 | 'section/u-boot:offset': 4, |
| 3986 | 'section/u-boot:size': 12 + len(U_BOOT_DATA) + 6, |
| 3987 | |
| 3988 | 'section/after:image-pos': 26, |
| 3989 | 'section/after:offset': 26, |
| 3990 | 'section/after:size': len(U_BOOT_DATA), |
| 3991 | } |
| 3992 | self.assertEqual(expected, props) |
| 3993 | |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 3994 | def testFitImageSubentryAlignment(self): |
| 3995 | """Test relative alignability of FIT image subentries""" |
Alper Nebi Yasak | d455326 | 2022-02-08 01:08:07 +0300 | [diff] [blame] | 3996 | self._SetupSplElf() |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 3997 | entry_args = { |
| 3998 | 'test-id': TEXT_DATA, |
| 3999 | } |
| 4000 | data, _, _, _ = self._DoReadFileDtb('167_fit_image_subentry_alignment.dts', |
| 4001 | entry_args=entry_args) |
| 4002 | dtb = fdt.Fdt.FromData(data) |
| 4003 | dtb.Scan() |
| 4004 | |
| 4005 | node = dtb.GetNode('/images/kernel') |
| 4006 | data = dtb.GetProps(node)["data"].bytes |
| 4007 | align_pad = 0x10 - (len(U_BOOT_SPL_DATA) % 0x10) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4008 | expected = (tools.get_bytes(0, 0x20) + U_BOOT_SPL_DATA + |
| 4009 | tools.get_bytes(0, align_pad) + U_BOOT_DATA) |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 4010 | self.assertEqual(expected, data) |
| 4011 | |
| 4012 | node = dtb.GetNode('/images/fdt-1') |
| 4013 | data = dtb.GetProps(node)["data"].bytes |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4014 | expected = (U_BOOT_SPL_DTB_DATA + tools.get_bytes(0, 20) + |
| 4015 | tools.to_bytes(TEXT_DATA) + tools.get_bytes(0, 30) + |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 4016 | U_BOOT_DTB_DATA) |
| 4017 | self.assertEqual(expected, data) |
| 4018 | |
| 4019 | def testFitExtblobMissingOk(self): |
| 4020 | """Test a FIT with a missing external blob that is allowed""" |
| 4021 | with test_util.capture_sys_output() as (stdout, stderr): |
| 4022 | self._DoTestFile('168_fit_missing_blob.dts', |
| 4023 | allow_missing=True) |
| 4024 | err = stderr.getvalue() |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 4025 | self.assertRegex(err, "Image 'main-section'.*missing.*: atf-bl31") |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 4026 | |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 4027 | def testBlobNamedByArgMissing(self): |
| 4028 | """Test handling of a missing entry arg""" |
| 4029 | with self.assertRaises(ValueError) as e: |
| 4030 | self._DoReadFile('068_blob_named_by_arg.dts') |
| 4031 | self.assertIn("Missing required properties/entry args: cros-ec-rw-path", |
| 4032 | str(e.exception)) |
| 4033 | |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 4034 | def testPackBl31(self): |
| 4035 | """Test that an image with an ATF BL31 binary can be created""" |
| 4036 | data = self._DoReadFile('169_atf_bl31.dts') |
| 4037 | self.assertEqual(ATF_BL31_DATA, data[:len(ATF_BL31_DATA)]) |
| 4038 | |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 4039 | def testPackScp(self): |
| 4040 | """Test that an image with an SCP binary can be created""" |
| 4041 | data = self._DoReadFile('172_scp.dts') |
| 4042 | self.assertEqual(SCP_DATA, data[:len(SCP_DATA)]) |
| 4043 | |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 4044 | def testFitFdt(self): |
| 4045 | """Test an image with an FIT with multiple FDT images""" |
| 4046 | def _CheckFdt(seq, expected_data): |
| 4047 | """Check the FDT nodes |
| 4048 | |
| 4049 | Args: |
| 4050 | seq: Sequence number to check (0 or 1) |
| 4051 | expected_data: Expected contents of 'data' property |
| 4052 | """ |
| 4053 | name = 'fdt-%d' % seq |
| 4054 | fnode = dtb.GetNode('/images/%s' % name) |
| 4055 | self.assertIsNotNone(fnode) |
| 4056 | self.assertEqual({'description','type', 'compression', 'data'}, |
| 4057 | set(fnode.props.keys())) |
| 4058 | self.assertEqual(expected_data, fnode.props['data'].bytes) |
| 4059 | self.assertEqual('fdt-test-fdt%d.dtb' % seq, |
| 4060 | fnode.props['description'].value) |
Jan Kiszka | a1419df | 2022-02-28 17:06:20 +0100 | [diff] [blame] | 4061 | self.assertEqual(fnode.subnodes[0].name, 'hash') |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 4062 | |
| 4063 | def _CheckConfig(seq, expected_data): |
| 4064 | """Check the configuration nodes |
| 4065 | |
| 4066 | Args: |
| 4067 | seq: Sequence number to check (0 or 1) |
| 4068 | expected_data: Expected contents of 'data' property |
| 4069 | """ |
| 4070 | cnode = dtb.GetNode('/configurations') |
| 4071 | self.assertIn('default', cnode.props) |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 4072 | self.assertEqual('config-2', cnode.props['default'].value) |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 4073 | |
| 4074 | name = 'config-%d' % seq |
| 4075 | fnode = dtb.GetNode('/configurations/%s' % name) |
| 4076 | self.assertIsNotNone(fnode) |
| 4077 | self.assertEqual({'description','firmware', 'loadables', 'fdt'}, |
| 4078 | set(fnode.props.keys())) |
| 4079 | self.assertEqual('conf-test-fdt%d.dtb' % seq, |
| 4080 | fnode.props['description'].value) |
| 4081 | self.assertEqual('fdt-%d' % seq, fnode.props['fdt'].value) |
| 4082 | |
| 4083 | entry_args = { |
| 4084 | 'of-list': 'test-fdt1 test-fdt2', |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 4085 | 'default-dt': 'test-fdt2', |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 4086 | } |
| 4087 | data = self._DoReadFileDtb( |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 4088 | '170_fit_fdt.dts', |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 4089 | entry_args=entry_args, |
| 4090 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 4091 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 4092 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 4093 | |
| 4094 | dtb = fdt.Fdt.FromData(fit_data) |
| 4095 | dtb.Scan() |
| 4096 | fnode = dtb.GetNode('/images/kernel') |
| 4097 | self.assertIn('data', fnode.props) |
| 4098 | |
| 4099 | # Check all the properties in fdt-1 and fdt-2 |
| 4100 | _CheckFdt(1, TEST_FDT1_DATA) |
| 4101 | _CheckFdt(2, TEST_FDT2_DATA) |
| 4102 | |
| 4103 | # Check configurations |
| 4104 | _CheckConfig(1, TEST_FDT1_DATA) |
| 4105 | _CheckConfig(2, TEST_FDT2_DATA) |
| 4106 | |
| 4107 | def testFitFdtMissingList(self): |
| 4108 | """Test handling of a missing 'of-list' entry arg""" |
| 4109 | with self.assertRaises(ValueError) as e: |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 4110 | self._DoReadFile('170_fit_fdt.dts') |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 4111 | self.assertIn("Generator node requires 'of-list' entry argument", |
| 4112 | str(e.exception)) |
| 4113 | |
| 4114 | def testFitFdtEmptyList(self): |
| 4115 | """Test handling of an empty 'of-list' entry arg""" |
| 4116 | entry_args = { |
| 4117 | 'of-list': '', |
| 4118 | } |
| 4119 | data = self._DoReadFileDtb('170_fit_fdt.dts', entry_args=entry_args)[0] |
| 4120 | |
| 4121 | def testFitFdtMissingProp(self): |
| 4122 | """Test handling of a missing 'fit,fdt-list' property""" |
| 4123 | with self.assertRaises(ValueError) as e: |
| 4124 | self._DoReadFile('171_fit_fdt_missing_prop.dts') |
| 4125 | self.assertIn("Generator node requires 'fit,fdt-list' property", |
| 4126 | str(e.exception)) |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 4127 | |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 4128 | def testFitFdtMissing(self): |
| 4129 | """Test handling of a missing 'default-dt' entry arg""" |
| 4130 | entry_args = { |
| 4131 | 'of-list': 'test-fdt1 test-fdt2', |
| 4132 | } |
| 4133 | with self.assertRaises(ValueError) as e: |
| 4134 | self._DoReadFileDtb( |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 4135 | '170_fit_fdt.dts', |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 4136 | entry_args=entry_args, |
| 4137 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 4138 | self.assertIn("Generated 'default' node requires default-dt entry argument", |
| 4139 | str(e.exception)) |
| 4140 | |
| 4141 | def testFitFdtNotInList(self): |
| 4142 | """Test handling of a default-dt that is not in the of-list""" |
| 4143 | entry_args = { |
| 4144 | 'of-list': 'test-fdt1 test-fdt2', |
| 4145 | 'default-dt': 'test-fdt3', |
| 4146 | } |
| 4147 | with self.assertRaises(ValueError) as e: |
| 4148 | self._DoReadFileDtb( |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 4149 | '170_fit_fdt.dts', |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 4150 | entry_args=entry_args, |
| 4151 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 4152 | self.assertIn("default-dt entry argument 'test-fdt3' not found in fdt list: test-fdt1, test-fdt2", |
| 4153 | str(e.exception)) |
| 4154 | |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 4155 | def testFitExtblobMissingHelp(self): |
| 4156 | """Test display of help messages when an external blob is missing""" |
| 4157 | control.missing_blob_help = control._ReadMissingBlobHelp() |
| 4158 | control.missing_blob_help['wibble'] = 'Wibble test' |
| 4159 | control.missing_blob_help['another'] = 'Another test' |
| 4160 | with test_util.capture_sys_output() as (stdout, stderr): |
| 4161 | self._DoTestFile('168_fit_missing_blob.dts', |
| 4162 | allow_missing=True) |
| 4163 | err = stderr.getvalue() |
| 4164 | |
| 4165 | # We can get the tag from the name, the type or the missing-msg |
| 4166 | # property. Check all three. |
| 4167 | self.assertIn('You may need to build ARM Trusted', err) |
| 4168 | self.assertIn('Wibble test', err) |
| 4169 | self.assertIn('Another test', err) |
| 4170 | |
Simon Glass | 6f1f4d4 | 2020-09-06 10:35:32 -0600 | [diff] [blame] | 4171 | def testMissingBlob(self): |
| 4172 | """Test handling of a blob containing a missing file""" |
| 4173 | with self.assertRaises(ValueError) as e: |
| 4174 | self._DoTestFile('173_missing_blob.dts', allow_missing=True) |
| 4175 | self.assertIn("Filename 'missing' not found in input path", |
| 4176 | str(e.exception)) |
| 4177 | |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 4178 | def testEnvironment(self): |
| 4179 | """Test adding a U-Boot environment""" |
| 4180 | data = self._DoReadFile('174_env.dts') |
| 4181 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 4182 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 4183 | env = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 4184 | self.assertEqual(b'\x1b\x97\x22\x7c\x01var1=1\0var2="2"\0\0\xff\xff', |
| 4185 | env) |
| 4186 | |
| 4187 | def testEnvironmentNoSize(self): |
| 4188 | """Test that a missing 'size' property is detected""" |
| 4189 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8cdc08a | 2020-10-26 17:40:00 -0600 | [diff] [blame] | 4190 | self._DoTestFile('175_env_no_size.dts') |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 4191 | self.assertIn("'u-boot-env' entry must have a size property", |
| 4192 | str(e.exception)) |
| 4193 | |
| 4194 | def testEnvironmentTooSmall(self): |
| 4195 | """Test handling of an environment that does not fit""" |
| 4196 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8cdc08a | 2020-10-26 17:40:00 -0600 | [diff] [blame] | 4197 | self._DoTestFile('176_env_too_small.dts') |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 4198 | |
| 4199 | # checksum, start byte, environment with \0 terminator, final \0 |
| 4200 | need = 4 + 1 + len(ENV_DATA) + 1 + 1 |
| 4201 | short = need - 0x8 |
| 4202 | self.assertIn("too small to hold data (need %#x more bytes)" % short, |
| 4203 | str(e.exception)) |
| 4204 | |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 4205 | def testSkipAtStart(self): |
| 4206 | """Test handling of skip-at-start section""" |
| 4207 | data = self._DoReadFile('177_skip_at_start.dts') |
| 4208 | self.assertEqual(U_BOOT_DATA, data) |
| 4209 | |
| 4210 | image = control.images['image'] |
| 4211 | entries = image.GetEntries() |
| 4212 | section = entries['section'] |
| 4213 | self.assertEqual(0, section.offset) |
| 4214 | self.assertEqual(len(U_BOOT_DATA), section.size) |
| 4215 | self.assertEqual(U_BOOT_DATA, section.GetData()) |
| 4216 | |
| 4217 | entry = section.GetEntries()['u-boot'] |
| 4218 | self.assertEqual(16, entry.offset) |
| 4219 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4220 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4221 | |
| 4222 | def testSkipAtStartPad(self): |
| 4223 | """Test handling of skip-at-start section with padded entry""" |
| 4224 | data = self._DoReadFile('178_skip_at_start_pad.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4225 | before = tools.get_bytes(0, 8) |
| 4226 | after = tools.get_bytes(0, 4) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 4227 | all = before + U_BOOT_DATA + after |
| 4228 | self.assertEqual(all, data) |
| 4229 | |
| 4230 | image = control.images['image'] |
| 4231 | entries = image.GetEntries() |
| 4232 | section = entries['section'] |
| 4233 | self.assertEqual(0, section.offset) |
| 4234 | self.assertEqual(len(all), section.size) |
| 4235 | self.assertEqual(all, section.GetData()) |
| 4236 | |
| 4237 | entry = section.GetEntries()['u-boot'] |
| 4238 | self.assertEqual(16, entry.offset) |
| 4239 | self.assertEqual(len(all), entry.size) |
| 4240 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4241 | |
| 4242 | def testSkipAtStartSectionPad(self): |
| 4243 | """Test handling of skip-at-start section with padding""" |
| 4244 | data = self._DoReadFile('179_skip_at_start_section_pad.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4245 | before = tools.get_bytes(0, 8) |
| 4246 | after = tools.get_bytes(0, 4) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 4247 | all = before + U_BOOT_DATA + after |
Simon Glass | 510ef0f | 2020-10-26 17:40:13 -0600 | [diff] [blame] | 4248 | self.assertEqual(all, data) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 4249 | |
| 4250 | image = control.images['image'] |
| 4251 | entries = image.GetEntries() |
| 4252 | section = entries['section'] |
| 4253 | self.assertEqual(0, section.offset) |
| 4254 | self.assertEqual(len(all), section.size) |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 4255 | self.assertEqual(U_BOOT_DATA, section.data) |
Simon Glass | 510ef0f | 2020-10-26 17:40:13 -0600 | [diff] [blame] | 4256 | self.assertEqual(all, section.GetPaddedData()) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 4257 | |
| 4258 | entry = section.GetEntries()['u-boot'] |
| 4259 | self.assertEqual(16, entry.offset) |
| 4260 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4261 | self.assertEqual(U_BOOT_DATA, entry.data) |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 4262 | |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4263 | def testSectionPad(self): |
| 4264 | """Testing padding with sections""" |
| 4265 | data = self._DoReadFile('180_section_pad.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4266 | expected = (tools.get_bytes(ord('&'), 3) + |
| 4267 | tools.get_bytes(ord('!'), 5) + |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4268 | U_BOOT_DATA + |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4269 | tools.get_bytes(ord('!'), 1) + |
| 4270 | tools.get_bytes(ord('&'), 2)) |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4271 | self.assertEqual(expected, data) |
| 4272 | |
| 4273 | def testSectionAlign(self): |
| 4274 | """Testing alignment with sections""" |
| 4275 | data = self._DoReadFileDtb('181_section_align.dts', map=True)[0] |
| 4276 | expected = (b'\0' + # fill section |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4277 | tools.get_bytes(ord('&'), 1) + # padding to section align |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4278 | b'\0' + # fill section |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4279 | tools.get_bytes(ord('!'), 3) + # padding to u-boot align |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4280 | U_BOOT_DATA + |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4281 | tools.get_bytes(ord('!'), 4) + # padding to u-boot size |
| 4282 | tools.get_bytes(ord('!'), 4)) # padding to section size |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4283 | self.assertEqual(expected, data) |
| 4284 | |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 4285 | def testCompressImage(self): |
| 4286 | """Test compression of the entire image""" |
| 4287 | self._CheckLz4() |
| 4288 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4289 | '182_compress_image.dts', use_real_dtb=True, update_dtb=True) |
| 4290 | dtb = fdt.Fdt(out_dtb_fname) |
| 4291 | dtb.Scan() |
| 4292 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4293 | 'uncomp-size']) |
| 4294 | orig = self._decompress(data) |
| 4295 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4296 | |
| 4297 | # Do a sanity check on various fields |
| 4298 | image = control.images['image'] |
| 4299 | entries = image.GetEntries() |
| 4300 | self.assertEqual(2, len(entries)) |
| 4301 | |
| 4302 | entry = entries['blob'] |
| 4303 | self.assertEqual(COMPRESS_DATA, entry.data) |
| 4304 | self.assertEqual(len(COMPRESS_DATA), entry.size) |
| 4305 | |
| 4306 | entry = entries['u-boot'] |
| 4307 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4308 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4309 | |
| 4310 | self.assertEqual(len(data), image.size) |
| 4311 | self.assertEqual(COMPRESS_DATA + U_BOOT_DATA, image.uncomp_data) |
| 4312 | self.assertEqual(len(COMPRESS_DATA + U_BOOT_DATA), image.uncomp_size) |
| 4313 | orig = self._decompress(image.data) |
| 4314 | self.assertEqual(orig, image.uncomp_data) |
| 4315 | |
| 4316 | expected = { |
| 4317 | 'blob:offset': 0, |
| 4318 | 'blob:size': len(COMPRESS_DATA), |
| 4319 | 'u-boot:offset': len(COMPRESS_DATA), |
| 4320 | 'u-boot:size': len(U_BOOT_DATA), |
| 4321 | 'uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4322 | 'offset': 0, |
| 4323 | 'image-pos': 0, |
| 4324 | 'size': len(data), |
| 4325 | } |
| 4326 | self.assertEqual(expected, props) |
| 4327 | |
| 4328 | def testCompressImageLess(self): |
| 4329 | """Test compression where compression reduces the image size""" |
| 4330 | self._CheckLz4() |
| 4331 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4332 | '183_compress_image_less.dts', use_real_dtb=True, update_dtb=True) |
| 4333 | dtb = fdt.Fdt(out_dtb_fname) |
| 4334 | dtb.Scan() |
| 4335 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4336 | 'uncomp-size']) |
| 4337 | orig = self._decompress(data) |
| 4338 | |
| 4339 | self.assertEquals(COMPRESS_DATA + COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4340 | |
| 4341 | # Do a sanity check on various fields |
| 4342 | image = control.images['image'] |
| 4343 | entries = image.GetEntries() |
| 4344 | self.assertEqual(2, len(entries)) |
| 4345 | |
| 4346 | entry = entries['blob'] |
| 4347 | self.assertEqual(COMPRESS_DATA_BIG, entry.data) |
| 4348 | self.assertEqual(len(COMPRESS_DATA_BIG), entry.size) |
| 4349 | |
| 4350 | entry = entries['u-boot'] |
| 4351 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4352 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4353 | |
| 4354 | self.assertEqual(len(data), image.size) |
| 4355 | self.assertEqual(COMPRESS_DATA_BIG + U_BOOT_DATA, image.uncomp_data) |
| 4356 | self.assertEqual(len(COMPRESS_DATA_BIG + U_BOOT_DATA), |
| 4357 | image.uncomp_size) |
| 4358 | orig = self._decompress(image.data) |
| 4359 | self.assertEqual(orig, image.uncomp_data) |
| 4360 | |
| 4361 | expected = { |
| 4362 | 'blob:offset': 0, |
| 4363 | 'blob:size': len(COMPRESS_DATA_BIG), |
| 4364 | 'u-boot:offset': len(COMPRESS_DATA_BIG), |
| 4365 | 'u-boot:size': len(U_BOOT_DATA), |
| 4366 | 'uncomp-size': len(COMPRESS_DATA_BIG + U_BOOT_DATA), |
| 4367 | 'offset': 0, |
| 4368 | 'image-pos': 0, |
| 4369 | 'size': len(data), |
| 4370 | } |
| 4371 | self.assertEqual(expected, props) |
| 4372 | |
| 4373 | def testCompressSectionSize(self): |
| 4374 | """Test compression of a section with a fixed size""" |
| 4375 | self._CheckLz4() |
| 4376 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4377 | '184_compress_section_size.dts', use_real_dtb=True, update_dtb=True) |
| 4378 | dtb = fdt.Fdt(out_dtb_fname) |
| 4379 | dtb.Scan() |
| 4380 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4381 | 'uncomp-size']) |
| 4382 | orig = self._decompress(data) |
| 4383 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4384 | expected = { |
| 4385 | 'section/blob:offset': 0, |
| 4386 | 'section/blob:size': len(COMPRESS_DATA), |
| 4387 | 'section/u-boot:offset': len(COMPRESS_DATA), |
| 4388 | 'section/u-boot:size': len(U_BOOT_DATA), |
| 4389 | 'section:offset': 0, |
| 4390 | 'section:image-pos': 0, |
| 4391 | 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4392 | 'section:size': 0x30, |
| 4393 | 'offset': 0, |
| 4394 | 'image-pos': 0, |
| 4395 | 'size': 0x30, |
| 4396 | } |
| 4397 | self.assertEqual(expected, props) |
| 4398 | |
| 4399 | def testCompressSection(self): |
| 4400 | """Test compression of a section with no fixed size""" |
| 4401 | self._CheckLz4() |
| 4402 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4403 | '185_compress_section.dts', use_real_dtb=True, update_dtb=True) |
| 4404 | dtb = fdt.Fdt(out_dtb_fname) |
| 4405 | dtb.Scan() |
| 4406 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4407 | 'uncomp-size']) |
| 4408 | orig = self._decompress(data) |
| 4409 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4410 | expected = { |
| 4411 | 'section/blob:offset': 0, |
| 4412 | 'section/blob:size': len(COMPRESS_DATA), |
| 4413 | 'section/u-boot:offset': len(COMPRESS_DATA), |
| 4414 | 'section/u-boot:size': len(U_BOOT_DATA), |
| 4415 | 'section:offset': 0, |
| 4416 | 'section:image-pos': 0, |
| 4417 | 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4418 | 'section:size': len(data), |
| 4419 | 'offset': 0, |
| 4420 | 'image-pos': 0, |
| 4421 | 'size': len(data), |
| 4422 | } |
| 4423 | self.assertEqual(expected, props) |
| 4424 | |
| 4425 | def testCompressExtra(self): |
| 4426 | """Test compression of a section with no fixed size""" |
| 4427 | self._CheckLz4() |
| 4428 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4429 | '186_compress_extra.dts', use_real_dtb=True, update_dtb=True) |
| 4430 | dtb = fdt.Fdt(out_dtb_fname) |
| 4431 | dtb.Scan() |
| 4432 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4433 | 'uncomp-size']) |
| 4434 | |
| 4435 | base = data[len(U_BOOT_DATA):] |
| 4436 | self.assertEquals(U_BOOT_DATA, base[:len(U_BOOT_DATA)]) |
| 4437 | rest = base[len(U_BOOT_DATA):] |
| 4438 | |
| 4439 | # Check compressed data |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 4440 | bintool = self.comp_bintools['lz4'] |
| 4441 | expect1 = bintool.compress(COMPRESS_DATA + U_BOOT_DATA) |
Stefan Herbrechtsmeier | 86f1fc0 | 2022-08-19 16:25:23 +0200 | [diff] [blame] | 4442 | data1 = rest[:len(expect1)] |
| 4443 | section1 = self._decompress(data1) |
| 4444 | self.assertEquals(expect1, data1) |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 4445 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, section1) |
| 4446 | rest1 = rest[len(expect1):] |
| 4447 | |
Stefan Herbrechtsmeier | b4bfbce | 2022-08-19 16:25:28 +0200 | [diff] [blame] | 4448 | expect2 = bintool.compress(COMPRESS_DATA + COMPRESS_DATA) |
Stefan Herbrechtsmeier | 86f1fc0 | 2022-08-19 16:25:23 +0200 | [diff] [blame] | 4449 | data2 = rest1[:len(expect2)] |
| 4450 | section2 = self._decompress(data2) |
| 4451 | self.assertEquals(expect2, data2) |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 4452 | self.assertEquals(COMPRESS_DATA + COMPRESS_DATA, section2) |
| 4453 | rest2 = rest1[len(expect2):] |
| 4454 | |
| 4455 | expect_size = (len(U_BOOT_DATA) + len(U_BOOT_DATA) + len(expect1) + |
| 4456 | len(expect2) + len(U_BOOT_DATA)) |
| 4457 | #self.assertEquals(expect_size, len(data)) |
| 4458 | |
| 4459 | #self.assertEquals(U_BOOT_DATA, rest2) |
| 4460 | |
| 4461 | self.maxDiff = None |
| 4462 | expected = { |
| 4463 | 'u-boot:offset': 0, |
| 4464 | 'u-boot:image-pos': 0, |
| 4465 | 'u-boot:size': len(U_BOOT_DATA), |
| 4466 | |
| 4467 | 'base:offset': len(U_BOOT_DATA), |
| 4468 | 'base:image-pos': len(U_BOOT_DATA), |
| 4469 | 'base:size': len(data) - len(U_BOOT_DATA), |
| 4470 | 'base/u-boot:offset': 0, |
| 4471 | 'base/u-boot:image-pos': len(U_BOOT_DATA), |
| 4472 | 'base/u-boot:size': len(U_BOOT_DATA), |
| 4473 | 'base/u-boot2:offset': len(U_BOOT_DATA) + len(expect1) + |
| 4474 | len(expect2), |
| 4475 | 'base/u-boot2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1) + |
| 4476 | len(expect2), |
| 4477 | 'base/u-boot2:size': len(U_BOOT_DATA), |
| 4478 | |
| 4479 | 'base/section:offset': len(U_BOOT_DATA), |
| 4480 | 'base/section:image-pos': len(U_BOOT_DATA) * 2, |
| 4481 | 'base/section:size': len(expect1), |
| 4482 | 'base/section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4483 | 'base/section/blob:offset': 0, |
| 4484 | 'base/section/blob:size': len(COMPRESS_DATA), |
| 4485 | 'base/section/u-boot:offset': len(COMPRESS_DATA), |
| 4486 | 'base/section/u-boot:size': len(U_BOOT_DATA), |
| 4487 | |
| 4488 | 'base/section2:offset': len(U_BOOT_DATA) + len(expect1), |
| 4489 | 'base/section2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1), |
| 4490 | 'base/section2:size': len(expect2), |
| 4491 | 'base/section2:uncomp-size': len(COMPRESS_DATA + COMPRESS_DATA), |
| 4492 | 'base/section2/blob:offset': 0, |
| 4493 | 'base/section2/blob:size': len(COMPRESS_DATA), |
| 4494 | 'base/section2/blob2:offset': len(COMPRESS_DATA), |
| 4495 | 'base/section2/blob2:size': len(COMPRESS_DATA), |
| 4496 | |
| 4497 | 'offset': 0, |
| 4498 | 'image-pos': 0, |
| 4499 | 'size': len(data), |
| 4500 | } |
| 4501 | self.assertEqual(expected, props) |
| 4502 | |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 4503 | def testSymbolsSubsection(self): |
| 4504 | """Test binman can assign symbols from a subsection""" |
Alper Nebi Yasak | 9634dc9 | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 4505 | self.checkSymbols('187_symbols_sub.dts', U_BOOT_SPL_DATA, 0x1c) |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 4506 | |
Simon Glass | 3fb2540 | 2021-01-06 21:35:16 -0700 | [diff] [blame] | 4507 | def testReadImageEntryArg(self): |
| 4508 | """Test reading an image that would need an entry arg to generate""" |
| 4509 | entry_args = { |
| 4510 | 'cros-ec-rw-path': 'ecrw.bin', |
| 4511 | } |
| 4512 | data = self.data = self._DoReadFileDtb( |
| 4513 | '188_image_entryarg.dts',use_real_dtb=True, update_dtb=True, |
| 4514 | entry_args=entry_args) |
| 4515 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4516 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 3fb2540 | 2021-01-06 21:35:16 -0700 | [diff] [blame] | 4517 | orig_image = control.images['image'] |
| 4518 | |
| 4519 | # This should not generate an error about the missing 'cros-ec-rw-path' |
| 4520 | # since we are reading the image from a file. Compare with |
| 4521 | # testEntryArgsRequired() |
| 4522 | image = Image.FromFile(image_fname) |
| 4523 | self.assertEqual(orig_image.GetEntries().keys(), |
| 4524 | image.GetEntries().keys()) |
| 4525 | |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 4526 | def testFilesAlign(self): |
| 4527 | """Test alignment with files""" |
| 4528 | data = self._DoReadFile('190_files_align.dts') |
| 4529 | |
| 4530 | # The first string is 15 bytes so will align to 16 |
| 4531 | expect = FILES_DATA[:15] + b'\0' + FILES_DATA[15:] |
| 4532 | self.assertEqual(expect, data) |
| 4533 | |
Simon Glass | db84b56 | 2021-01-06 21:35:19 -0700 | [diff] [blame] | 4534 | def testReadImageSkip(self): |
| 4535 | """Test reading an image and accessing its FDT map""" |
| 4536 | data = self.data = self._DoReadFileRealDtb('191_read_image_skip.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4537 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | db84b56 | 2021-01-06 21:35:19 -0700 | [diff] [blame] | 4538 | orig_image = control.images['image'] |
| 4539 | image = Image.FromFile(image_fname) |
| 4540 | self.assertEqual(orig_image.GetEntries().keys(), |
| 4541 | image.GetEntries().keys()) |
| 4542 | |
| 4543 | orig_entry = orig_image.GetEntries()['fdtmap'] |
| 4544 | entry = image.GetEntries()['fdtmap'] |
| 4545 | self.assertEqual(orig_entry.offset, entry.offset) |
| 4546 | self.assertEqual(orig_entry.size, entry.size) |
| 4547 | self.assertEqual(16, entry.image_pos) |
| 4548 | |
| 4549 | u_boot = image.GetEntries()['section'].GetEntries()['u-boot'] |
| 4550 | |
| 4551 | self.assertEquals(U_BOOT_DATA, u_boot.ReadData()) |
| 4552 | |
Simon Glass | c98de97 | 2021-03-18 20:24:57 +1300 | [diff] [blame] | 4553 | def testTplNoDtb(self): |
| 4554 | """Test that an image with tpl/u-boot-tpl-nodtb.bin can be created""" |
Simon Glass | 13089cc | 2021-04-25 08:39:32 +1200 | [diff] [blame] | 4555 | self._SetupTplElf() |
Simon Glass | c98de97 | 2021-03-18 20:24:57 +1300 | [diff] [blame] | 4556 | data = self._DoReadFile('192_u_boot_tpl_nodtb.dts') |
| 4557 | self.assertEqual(U_BOOT_TPL_NODTB_DATA, |
| 4558 | data[:len(U_BOOT_TPL_NODTB_DATA)]) |
| 4559 | |
Simon Glass | 63f41d4 | 2021-03-18 20:24:58 +1300 | [diff] [blame] | 4560 | def testTplBssPad(self): |
| 4561 | """Test that we can pad TPL's BSS with zeros""" |
| 4562 | # ELF file with a '__bss_size' symbol |
| 4563 | self._SetupTplElf() |
| 4564 | data = self._DoReadFile('193_tpl_bss_pad.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4565 | self.assertEqual(U_BOOT_TPL_DATA + tools.get_bytes(0, 10) + U_BOOT_DATA, |
Simon Glass | 63f41d4 | 2021-03-18 20:24:58 +1300 | [diff] [blame] | 4566 | data) |
| 4567 | |
| 4568 | def testTplBssPadMissing(self): |
| 4569 | """Test that a missing symbol is detected""" |
| 4570 | self._SetupTplElf('u_boot_ucode_ptr') |
| 4571 | with self.assertRaises(ValueError) as e: |
| 4572 | self._DoReadFile('193_tpl_bss_pad.dts') |
| 4573 | self.assertIn('Expected __bss_size symbol in tpl/u-boot-tpl', |
| 4574 | str(e.exception)) |
| 4575 | |
Simon Glass | 718b529 | 2021-03-18 20:25:07 +1300 | [diff] [blame] | 4576 | def checkDtbSizes(self, data, pad_len, start): |
| 4577 | """Check the size arguments in a dtb embedded in an image |
| 4578 | |
| 4579 | Args: |
| 4580 | data: The image data |
| 4581 | pad_len: Length of the pad section in the image, in bytes |
| 4582 | start: Start offset of the devicetree to examine, within the image |
| 4583 | |
| 4584 | Returns: |
| 4585 | Size of the devicetree in bytes |
| 4586 | """ |
| 4587 | dtb_data = data[start:] |
| 4588 | dtb = fdt.Fdt.FromData(dtb_data) |
| 4589 | fdt_size = dtb.GetFdtObj().totalsize() |
| 4590 | dtb.Scan() |
| 4591 | props = self._GetPropTree(dtb, 'size') |
| 4592 | self.assertEqual({ |
| 4593 | 'size': len(data), |
| 4594 | 'u-boot-spl/u-boot-spl-bss-pad:size': pad_len, |
| 4595 | 'u-boot-spl/u-boot-spl-dtb:size': 801, |
| 4596 | 'u-boot-spl/u-boot-spl-nodtb:size': len(U_BOOT_SPL_NODTB_DATA), |
| 4597 | 'u-boot-spl:size': 860, |
| 4598 | 'u-boot-tpl:size': len(U_BOOT_TPL_DATA), |
| 4599 | 'u-boot/u-boot-dtb:size': 781, |
| 4600 | 'u-boot/u-boot-nodtb:size': len(U_BOOT_NODTB_DATA), |
| 4601 | 'u-boot:size': 827, |
| 4602 | }, props) |
| 4603 | return fdt_size |
| 4604 | |
| 4605 | def testExpanded(self): |
| 4606 | """Test that an expanded entry type is selected when needed""" |
| 4607 | self._SetupSplElf() |
| 4608 | self._SetupTplElf() |
| 4609 | |
| 4610 | # SPL has a devicetree, TPL does not |
| 4611 | entry_args = { |
| 4612 | 'spl-dtb': '1', |
| 4613 | 'spl-bss-pad': 'y', |
| 4614 | 'tpl-dtb': '', |
| 4615 | } |
| 4616 | self._DoReadFileDtb('194_fdt_incl.dts', use_expanded=True, |
| 4617 | entry_args=entry_args) |
| 4618 | image = control.images['image'] |
| 4619 | entries = image.GetEntries() |
| 4620 | self.assertEqual(3, len(entries)) |
| 4621 | |
| 4622 | # First, u-boot, which should be expanded into u-boot-nodtb and dtb |
| 4623 | self.assertIn('u-boot', entries) |
| 4624 | entry = entries['u-boot'] |
| 4625 | self.assertEqual('u-boot-expanded', entry.etype) |
| 4626 | subent = entry.GetEntries() |
| 4627 | self.assertEqual(2, len(subent)) |
| 4628 | self.assertIn('u-boot-nodtb', subent) |
| 4629 | self.assertIn('u-boot-dtb', subent) |
| 4630 | |
| 4631 | # Second, u-boot-spl, which should be expanded into three parts |
| 4632 | self.assertIn('u-boot-spl', entries) |
| 4633 | entry = entries['u-boot-spl'] |
| 4634 | self.assertEqual('u-boot-spl-expanded', entry.etype) |
| 4635 | subent = entry.GetEntries() |
| 4636 | self.assertEqual(3, len(subent)) |
| 4637 | self.assertIn('u-boot-spl-nodtb', subent) |
| 4638 | self.assertIn('u-boot-spl-bss-pad', subent) |
| 4639 | self.assertIn('u-boot-spl-dtb', subent) |
| 4640 | |
| 4641 | # Third, u-boot-tpl, which should be not be expanded, since TPL has no |
| 4642 | # devicetree |
| 4643 | self.assertIn('u-boot-tpl', entries) |
| 4644 | entry = entries['u-boot-tpl'] |
| 4645 | self.assertEqual('u-boot-tpl', entry.etype) |
| 4646 | self.assertEqual(None, entry.GetEntries()) |
| 4647 | |
| 4648 | def testExpandedTpl(self): |
| 4649 | """Test that an expanded entry type is selected for TPL when needed""" |
| 4650 | self._SetupTplElf() |
| 4651 | |
| 4652 | entry_args = { |
| 4653 | 'tpl-bss-pad': 'y', |
| 4654 | 'tpl-dtb': 'y', |
| 4655 | } |
| 4656 | self._DoReadFileDtb('195_fdt_incl_tpl.dts', use_expanded=True, |
| 4657 | entry_args=entry_args) |
| 4658 | image = control.images['image'] |
| 4659 | entries = image.GetEntries() |
| 4660 | self.assertEqual(1, len(entries)) |
| 4661 | |
| 4662 | # We only have u-boot-tpl, which be expanded |
| 4663 | self.assertIn('u-boot-tpl', entries) |
| 4664 | entry = entries['u-boot-tpl'] |
| 4665 | self.assertEqual('u-boot-tpl-expanded', entry.etype) |
| 4666 | subent = entry.GetEntries() |
| 4667 | self.assertEqual(3, len(subent)) |
| 4668 | self.assertIn('u-boot-tpl-nodtb', subent) |
| 4669 | self.assertIn('u-boot-tpl-bss-pad', subent) |
| 4670 | self.assertIn('u-boot-tpl-dtb', subent) |
| 4671 | |
| 4672 | def testExpandedNoPad(self): |
| 4673 | """Test an expanded entry without BSS pad enabled""" |
| 4674 | self._SetupSplElf() |
| 4675 | self._SetupTplElf() |
| 4676 | |
| 4677 | # SPL has a devicetree, TPL does not |
| 4678 | entry_args = { |
| 4679 | 'spl-dtb': 'something', |
| 4680 | 'spl-bss-pad': 'n', |
| 4681 | 'tpl-dtb': '', |
| 4682 | } |
| 4683 | self._DoReadFileDtb('194_fdt_incl.dts', use_expanded=True, |
| 4684 | entry_args=entry_args) |
| 4685 | image = control.images['image'] |
| 4686 | entries = image.GetEntries() |
| 4687 | |
| 4688 | # Just check u-boot-spl, which should be expanded into two parts |
| 4689 | self.assertIn('u-boot-spl', entries) |
| 4690 | entry = entries['u-boot-spl'] |
| 4691 | self.assertEqual('u-boot-spl-expanded', entry.etype) |
| 4692 | subent = entry.GetEntries() |
| 4693 | self.assertEqual(2, len(subent)) |
| 4694 | self.assertIn('u-boot-spl-nodtb', subent) |
| 4695 | self.assertIn('u-boot-spl-dtb', subent) |
| 4696 | |
| 4697 | def testExpandedTplNoPad(self): |
| 4698 | """Test that an expanded entry type with padding disabled in TPL""" |
| 4699 | self._SetupTplElf() |
| 4700 | |
| 4701 | entry_args = { |
| 4702 | 'tpl-bss-pad': '', |
| 4703 | 'tpl-dtb': 'y', |
| 4704 | } |
| 4705 | self._DoReadFileDtb('195_fdt_incl_tpl.dts', use_expanded=True, |
| 4706 | entry_args=entry_args) |
| 4707 | image = control.images['image'] |
| 4708 | entries = image.GetEntries() |
| 4709 | self.assertEqual(1, len(entries)) |
| 4710 | |
| 4711 | # We only have u-boot-tpl, which be expanded |
| 4712 | self.assertIn('u-boot-tpl', entries) |
| 4713 | entry = entries['u-boot-tpl'] |
| 4714 | self.assertEqual('u-boot-tpl-expanded', entry.etype) |
| 4715 | subent = entry.GetEntries() |
| 4716 | self.assertEqual(2, len(subent)) |
| 4717 | self.assertIn('u-boot-tpl-nodtb', subent) |
| 4718 | self.assertIn('u-boot-tpl-dtb', subent) |
| 4719 | |
| 4720 | def testFdtInclude(self): |
| 4721 | """Test that an Fdt is update within all binaries""" |
| 4722 | self._SetupSplElf() |
| 4723 | self._SetupTplElf() |
| 4724 | |
| 4725 | # SPL has a devicetree, TPL does not |
| 4726 | self.maxDiff = None |
| 4727 | entry_args = { |
| 4728 | 'spl-dtb': '1', |
| 4729 | 'spl-bss-pad': 'y', |
| 4730 | 'tpl-dtb': '', |
| 4731 | } |
| 4732 | # Build the image. It includes two separate devicetree binaries, each |
| 4733 | # with their own contents, but all contain the binman definition. |
| 4734 | data = self._DoReadFileDtb( |
| 4735 | '194_fdt_incl.dts', use_real_dtb=True, use_expanded=True, |
| 4736 | update_dtb=True, entry_args=entry_args)[0] |
| 4737 | pad_len = 10 |
| 4738 | |
| 4739 | # Check the U-Boot dtb |
| 4740 | start = len(U_BOOT_NODTB_DATA) |
| 4741 | fdt_size = self.checkDtbSizes(data, pad_len, start) |
| 4742 | |
| 4743 | # Now check SPL |
| 4744 | start += fdt_size + len(U_BOOT_SPL_NODTB_DATA) + pad_len |
| 4745 | fdt_size = self.checkDtbSizes(data, pad_len, start) |
| 4746 | |
| 4747 | # TPL has no devicetree |
| 4748 | start += fdt_size + len(U_BOOT_TPL_DATA) |
| 4749 | self.assertEqual(len(data), start) |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4750 | |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 4751 | def testSymbolsExpanded(self): |
| 4752 | """Test binman can assign symbols in expanded entries""" |
| 4753 | entry_args = { |
| 4754 | 'spl-dtb': '1', |
| 4755 | } |
| 4756 | self.checkSymbols('197_symbols_expand.dts', U_BOOT_SPL_NODTB_DATA + |
| 4757 | U_BOOT_SPL_DTB_DATA, 0x38, |
| 4758 | entry_args=entry_args, use_expanded=True) |
| 4759 | |
Simon Glass | e191578 | 2021-03-21 18:24:31 +1300 | [diff] [blame] | 4760 | def testCollection(self): |
| 4761 | """Test a collection""" |
| 4762 | data = self._DoReadFile('198_collection.dts') |
| 4763 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA + |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4764 | tools.get_bytes(0xff, 2) + U_BOOT_NODTB_DATA + |
| 4765 | tools.get_bytes(0xfe, 3) + U_BOOT_DTB_DATA, |
Simon Glass | e191578 | 2021-03-21 18:24:31 +1300 | [diff] [blame] | 4766 | data) |
| 4767 | |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 4768 | def testCollectionSection(self): |
| 4769 | """Test a collection where a section must be built first""" |
| 4770 | # Sections never have their contents when GetData() is called, but when |
Simon Glass | 7e3f89f | 2021-11-23 11:03:47 -0700 | [diff] [blame] | 4771 | # BuildSectionData() is called with required=True, a section will force |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 4772 | # building the contents, producing an error is anything is still |
| 4773 | # missing. |
| 4774 | data = self._DoReadFile('199_collection_section.dts') |
| 4775 | section = U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4776 | self.assertEqual(section + U_BOOT_DATA + tools.get_bytes(0xff, 2) + |
| 4777 | section + tools.get_bytes(0xfe, 3) + U_BOOT_DATA, |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 4778 | data) |
| 4779 | |
Simon Glass | f427c5f | 2021-03-21 18:24:33 +1300 | [diff] [blame] | 4780 | def testAlignDefault(self): |
| 4781 | """Test that default alignment works on sections""" |
| 4782 | data = self._DoReadFile('200_align_default.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4783 | expected = (U_BOOT_DATA + tools.get_bytes(0, 8 - len(U_BOOT_DATA)) + |
Simon Glass | f427c5f | 2021-03-21 18:24:33 +1300 | [diff] [blame] | 4784 | U_BOOT_DATA) |
| 4785 | # Special alignment for section |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4786 | expected += tools.get_bytes(0, 32 - len(expected)) |
Simon Glass | f427c5f | 2021-03-21 18:24:33 +1300 | [diff] [blame] | 4787 | # No alignment within the nested section |
| 4788 | expected += U_BOOT_DATA + U_BOOT_NODTB_DATA; |
| 4789 | # Now the final piece, which should be default-aligned |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4790 | expected += tools.get_bytes(0, 88 - len(expected)) + U_BOOT_NODTB_DATA |
Simon Glass | f427c5f | 2021-03-21 18:24:33 +1300 | [diff] [blame] | 4791 | self.assertEqual(expected, data) |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 4792 | |
Bin Meng | c0b1574 | 2021-05-10 20:23:33 +0800 | [diff] [blame] | 4793 | def testPackOpenSBI(self): |
| 4794 | """Test that an image with an OpenSBI binary can be created""" |
| 4795 | data = self._DoReadFile('201_opensbi.dts') |
| 4796 | self.assertEqual(OPENSBI_DATA, data[:len(OPENSBI_DATA)]) |
| 4797 | |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 4798 | def testSectionsSingleThread(self): |
| 4799 | """Test sections without multithreading""" |
| 4800 | data = self._DoReadFileDtb('055_sections.dts', threads=0)[0] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4801 | expected = (U_BOOT_DATA + tools.get_bytes(ord('!'), 12) + |
| 4802 | U_BOOT_DATA + tools.get_bytes(ord('a'), 12) + |
| 4803 | U_BOOT_DATA + tools.get_bytes(ord('&'), 4)) |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 4804 | self.assertEqual(expected, data) |
| 4805 | |
| 4806 | def testThreadTimeout(self): |
| 4807 | """Test handling a thread that takes too long""" |
| 4808 | with self.assertRaises(ValueError) as e: |
| 4809 | self._DoTestFile('202_section_timeout.dts', |
| 4810 | test_section_timeout=True) |
Simon Glass | 2d59d15 | 2021-10-18 12:13:15 -0600 | [diff] [blame] | 4811 | self.assertIn("Timed out obtaining contents", str(e.exception)) |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 4812 | |
Simon Glass | 748a1d4 | 2021-07-06 10:36:41 -0600 | [diff] [blame] | 4813 | def testTiming(self): |
| 4814 | """Test output of timing information""" |
| 4815 | data = self._DoReadFile('055_sections.dts') |
| 4816 | with test_util.capture_sys_output() as (stdout, stderr): |
| 4817 | state.TimingShow() |
| 4818 | self.assertIn('read:', stdout.getvalue()) |
| 4819 | self.assertIn('compress:', stdout.getvalue()) |
| 4820 | |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 4821 | def testUpdateFdtInElf(self): |
| 4822 | """Test that we can update the devicetree in an ELF file""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 4823 | if not elf.ELF_TOOLS: |
| 4824 | self.skipTest('Python elftools not available') |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 4825 | infile = elf_fname = self.ElfTestFile('u_boot_binman_embed') |
| 4826 | outfile = os.path.join(self._indir, 'u-boot.out') |
| 4827 | begin_sym = 'dtb_embed_begin' |
| 4828 | end_sym = 'dtb_embed_end' |
| 4829 | retcode = self._DoTestFile( |
| 4830 | '060_fdt_update.dts', update_dtb=True, |
| 4831 | update_fdt_in_elf=','.join([infile,outfile,begin_sym,end_sym])) |
| 4832 | self.assertEqual(0, retcode) |
| 4833 | |
| 4834 | # Check that the output file does in fact contact a dtb with the binman |
| 4835 | # definition in the correct place |
| 4836 | syms = elf.GetSymbolFileOffset(infile, |
| 4837 | ['dtb_embed_begin', 'dtb_embed_end']) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4838 | data = tools.read_file(outfile) |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 4839 | dtb_data = data[syms['dtb_embed_begin'].offset: |
| 4840 | syms['dtb_embed_end'].offset] |
| 4841 | |
| 4842 | dtb = fdt.Fdt.FromData(dtb_data) |
| 4843 | dtb.Scan() |
| 4844 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
| 4845 | self.assertEqual({ |
| 4846 | 'image-pos': 0, |
| 4847 | 'offset': 0, |
| 4848 | '_testing:offset': 32, |
| 4849 | '_testing:size': 2, |
| 4850 | '_testing:image-pos': 32, |
| 4851 | 'section@0/u-boot:offset': 0, |
| 4852 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
| 4853 | 'section@0/u-boot:image-pos': 0, |
| 4854 | 'section@0:offset': 0, |
| 4855 | 'section@0:size': 16, |
| 4856 | 'section@0:image-pos': 0, |
| 4857 | |
| 4858 | 'section@1/u-boot:offset': 0, |
| 4859 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
| 4860 | 'section@1/u-boot:image-pos': 16, |
| 4861 | 'section@1:offset': 16, |
| 4862 | 'section@1:size': 16, |
| 4863 | 'section@1:image-pos': 16, |
| 4864 | 'size': 40 |
| 4865 | }, props) |
| 4866 | |
| 4867 | def testUpdateFdtInElfInvalid(self): |
| 4868 | """Test that invalid args are detected with --update-fdt-in-elf""" |
| 4869 | with self.assertRaises(ValueError) as e: |
| 4870 | self._DoTestFile('060_fdt_update.dts', update_fdt_in_elf='fred') |
| 4871 | self.assertIn("Invalid args ['fred'] to --update-fdt-in-elf", |
| 4872 | str(e.exception)) |
| 4873 | |
| 4874 | def testUpdateFdtInElfNoSyms(self): |
| 4875 | """Test that missing symbols are detected with --update-fdt-in-elf""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 4876 | if not elf.ELF_TOOLS: |
| 4877 | self.skipTest('Python elftools not available') |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 4878 | infile = elf_fname = self.ElfTestFile('u_boot_binman_embed') |
| 4879 | outfile = '' |
| 4880 | begin_sym = 'wrong_begin' |
| 4881 | end_sym = 'wrong_end' |
| 4882 | with self.assertRaises(ValueError) as e: |
| 4883 | self._DoTestFile( |
| 4884 | '060_fdt_update.dts', |
| 4885 | update_fdt_in_elf=','.join([infile,outfile,begin_sym,end_sym])) |
| 4886 | self.assertIn("Expected two symbols 'wrong_begin' and 'wrong_end': got 0:", |
| 4887 | str(e.exception)) |
| 4888 | |
| 4889 | def testUpdateFdtInElfTooSmall(self): |
| 4890 | """Test that an over-large dtb is detected with --update-fdt-in-elf""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 4891 | if not elf.ELF_TOOLS: |
| 4892 | self.skipTest('Python elftools not available') |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 4893 | infile = elf_fname = self.ElfTestFile('u_boot_binman_embed_sm') |
| 4894 | outfile = os.path.join(self._indir, 'u-boot.out') |
| 4895 | begin_sym = 'dtb_embed_begin' |
| 4896 | end_sym = 'dtb_embed_end' |
| 4897 | with self.assertRaises(ValueError) as e: |
| 4898 | self._DoTestFile( |
| 4899 | '060_fdt_update.dts', update_dtb=True, |
| 4900 | update_fdt_in_elf=','.join([infile,outfile,begin_sym,end_sym])) |
| 4901 | self.assertRegex( |
| 4902 | str(e.exception), |
| 4903 | "Not enough space in '.*u_boot_binman_embed_sm' for data length.*") |
| 4904 | |
Simon Glass | 88e04da | 2021-11-23 11:03:42 -0700 | [diff] [blame] | 4905 | def testVersion(self): |
| 4906 | """Test we can get the binman version""" |
| 4907 | version = '(unreleased)' |
| 4908 | self.assertEqual(version, state.GetVersion(self._indir)) |
| 4909 | |
| 4910 | with self.assertRaises(SystemExit): |
| 4911 | with test_util.capture_sys_output() as (_, stderr): |
| 4912 | self._DoBinman('-V') |
| 4913 | self.assertEqual('Binman %s\n' % version, stderr.getvalue()) |
| 4914 | |
| 4915 | # Try running the tool too, just to be safe |
| 4916 | result = self._RunBinman('-V') |
| 4917 | self.assertEqual('Binman %s\n' % version, result.stderr) |
| 4918 | |
| 4919 | # Set up a version file to make sure that works |
| 4920 | version = 'v2025.01-rc2' |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4921 | tools.write_file(os.path.join(self._indir, 'version'), version, |
Simon Glass | 88e04da | 2021-11-23 11:03:42 -0700 | [diff] [blame] | 4922 | binary=False) |
| 4923 | self.assertEqual(version, state.GetVersion(self._indir)) |
| 4924 | |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 4925 | def testAltFormat(self): |
| 4926 | """Test that alternative formats can be used to extract""" |
| 4927 | self._DoReadFileRealDtb('213_fdtmap_alt_format.dts') |
| 4928 | |
| 4929 | try: |
| 4930 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 4931 | with test_util.capture_sys_output() as (stdout, _): |
| 4932 | self._DoBinman('extract', '-i', updated_fname, '-F', 'list') |
| 4933 | self.assertEqual( |
| 4934 | '''Flag (-F) Entry type Description |
| 4935 | fdt fdtmap Extract the devicetree blob from the fdtmap |
| 4936 | ''', |
| 4937 | stdout.getvalue()) |
| 4938 | |
| 4939 | dtb = os.path.join(tmpdir, 'fdt.dtb') |
| 4940 | self._DoBinman('extract', '-i', updated_fname, '-F', 'fdt', '-f', |
| 4941 | dtb, 'fdtmap') |
| 4942 | |
| 4943 | # Check that we can read it and it can be scanning, meaning it does |
| 4944 | # not have a 16-byte fdtmap header |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4945 | data = tools.read_file(dtb) |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 4946 | dtb = fdt.Fdt.FromData(data) |
| 4947 | dtb.Scan() |
| 4948 | |
| 4949 | # Now check u-boot which has no alt_format |
| 4950 | fname = os.path.join(tmpdir, 'fdt.dtb') |
| 4951 | self._DoBinman('extract', '-i', updated_fname, '-F', 'dummy', |
| 4952 | '-f', fname, 'u-boot') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 4953 | data = tools.read_file(fname) |
Simon Glass | 637958f | 2021-11-23 21:09:50 -0700 | [diff] [blame] | 4954 | self.assertEqual(U_BOOT_DATA, data) |
| 4955 | |
| 4956 | finally: |
| 4957 | shutil.rmtree(tmpdir) |
| 4958 | |
Simon Glass | 0b00ae6 | 2021-11-23 21:09:52 -0700 | [diff] [blame] | 4959 | def testExtblobList(self): |
| 4960 | """Test an image with an external blob list""" |
| 4961 | data = self._DoReadFile('215_blob_ext_list.dts') |
| 4962 | self.assertEqual(REFCODE_DATA + FSP_M_DATA, data) |
| 4963 | |
| 4964 | def testExtblobListMissing(self): |
| 4965 | """Test an image with a missing external blob""" |
| 4966 | with self.assertRaises(ValueError) as e: |
| 4967 | self._DoReadFile('216_blob_ext_list_missing.dts') |
| 4968 | self.assertIn("Filename 'missing-file' not found in input path", |
| 4969 | str(e.exception)) |
| 4970 | |
| 4971 | def testExtblobListMissingOk(self): |
| 4972 | """Test an image with an missing external blob that is allowed""" |
| 4973 | with test_util.capture_sys_output() as (stdout, stderr): |
| 4974 | self._DoTestFile('216_blob_ext_list_missing.dts', |
| 4975 | allow_missing=True) |
| 4976 | err = stderr.getvalue() |
| 4977 | self.assertRegex(err, "Image 'main-section'.*missing.*: blob-ext") |
| 4978 | |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 4979 | def testFip(self): |
| 4980 | """Basic test of generation of an ARM Firmware Image Package (FIP)""" |
| 4981 | data = self._DoReadFile('203_fip.dts') |
| 4982 | hdr, fents = fip_util.decode_fip(data) |
| 4983 | self.assertEqual(fip_util.HEADER_MAGIC, hdr.name) |
| 4984 | self.assertEqual(fip_util.HEADER_SERIAL, hdr.serial) |
| 4985 | self.assertEqual(0x123, hdr.flags) |
| 4986 | |
| 4987 | self.assertEqual(2, len(fents)) |
| 4988 | |
| 4989 | fent = fents[0] |
| 4990 | self.assertEqual( |
| 4991 | bytes([0x47, 0xd4, 0x08, 0x6d, 0x4c, 0xfe, 0x98, 0x46, |
| 4992 | 0x9b, 0x95, 0x29, 0x50, 0xcb, 0xbd, 0x5a, 0x0]), fent.uuid) |
| 4993 | self.assertEqual('soc-fw', fent.fip_type) |
| 4994 | self.assertEqual(0x88, fent.offset) |
| 4995 | self.assertEqual(len(ATF_BL31_DATA), fent.size) |
| 4996 | self.assertEqual(0x123456789abcdef, fent.flags) |
| 4997 | self.assertEqual(ATF_BL31_DATA, fent.data) |
| 4998 | self.assertEqual(True, fent.valid) |
| 4999 | |
| 5000 | fent = fents[1] |
| 5001 | self.assertEqual( |
| 5002 | bytes([0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44, |
| 5003 | 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10]), fent.uuid) |
| 5004 | self.assertEqual('scp-fwu-cfg', fent.fip_type) |
| 5005 | self.assertEqual(0x8c, fent.offset) |
| 5006 | self.assertEqual(len(ATF_BL31_DATA), fent.size) |
| 5007 | self.assertEqual(0, fent.flags) |
| 5008 | self.assertEqual(ATF_BL2U_DATA, fent.data) |
| 5009 | self.assertEqual(True, fent.valid) |
| 5010 | |
| 5011 | def testFipOther(self): |
| 5012 | """Basic FIP with something that isn't a external blob""" |
| 5013 | data = self._DoReadFile('204_fip_other.dts') |
| 5014 | hdr, fents = fip_util.decode_fip(data) |
| 5015 | |
| 5016 | self.assertEqual(2, len(fents)) |
| 5017 | fent = fents[1] |
| 5018 | self.assertEqual('rot-cert', fent.fip_type) |
| 5019 | self.assertEqual(b'aa', fent.data) |
| 5020 | |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5021 | def testFipNoType(self): |
| 5022 | """FIP with an entry of an unknown type""" |
| 5023 | with self.assertRaises(ValueError) as e: |
| 5024 | self._DoReadFile('205_fip_no_type.dts') |
| 5025 | self.assertIn("Must provide a fip-type (node name 'u-boot' is not a known FIP type)", |
| 5026 | str(e.exception)) |
| 5027 | |
| 5028 | def testFipUuid(self): |
| 5029 | """Basic FIP with a manual uuid""" |
| 5030 | data = self._DoReadFile('206_fip_uuid.dts') |
| 5031 | hdr, fents = fip_util.decode_fip(data) |
| 5032 | |
| 5033 | self.assertEqual(2, len(fents)) |
| 5034 | fent = fents[1] |
| 5035 | self.assertEqual(None, fent.fip_type) |
| 5036 | self.assertEqual( |
| 5037 | bytes([0xfc, 0x65, 0x13, 0x92, 0x4a, 0x5b, 0x11, 0xec, |
| 5038 | 0x94, 0x35, 0xff, 0x2d, 0x1c, 0xfc, 0x79, 0x9c]), |
| 5039 | fent.uuid) |
| 5040 | self.assertEqual(U_BOOT_DATA, fent.data) |
| 5041 | |
| 5042 | def testFipLs(self): |
| 5043 | """Test listing a FIP""" |
| 5044 | data = self._DoReadFileRealDtb('207_fip_ls.dts') |
| 5045 | hdr, fents = fip_util.decode_fip(data) |
| 5046 | |
| 5047 | try: |
| 5048 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 5049 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5050 | self._DoBinman('ls', '-i', updated_fname) |
| 5051 | finally: |
| 5052 | shutil.rmtree(tmpdir) |
| 5053 | lines = stdout.getvalue().splitlines() |
| 5054 | expected = [ |
| 5055 | 'Name Image-pos Size Entry-type Offset Uncomp-size', |
| 5056 | '----------------------------------------------------------------', |
| 5057 | 'main-section 0 2d3 section 0', |
| 5058 | ' atf-fip 0 90 atf-fip 0', |
| 5059 | ' soc-fw 88 4 blob-ext 88', |
| 5060 | ' u-boot 8c 4 u-boot 8c', |
| 5061 | ' fdtmap 90 243 fdtmap 90', |
| 5062 | ] |
| 5063 | self.assertEqual(expected, lines) |
| 5064 | |
| 5065 | image = control.images['image'] |
| 5066 | entries = image.GetEntries() |
| 5067 | fdtmap = entries['fdtmap'] |
| 5068 | |
| 5069 | fdtmap_data = data[fdtmap.image_pos:fdtmap.image_pos + fdtmap.size] |
| 5070 | magic = fdtmap_data[:8] |
| 5071 | self.assertEqual(b'_FDTMAP_', magic) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5072 | self.assertEqual(tools.get_bytes(0, 8), fdtmap_data[8:16]) |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5073 | |
| 5074 | fdt_data = fdtmap_data[16:] |
| 5075 | dtb = fdt.Fdt.FromData(fdt_data) |
| 5076 | dtb.Scan() |
| 5077 | props = self._GetPropTree(dtb, BASE_DTB_PROPS, prefix='/') |
| 5078 | self.assertEqual({ |
| 5079 | 'atf-fip/soc-fw:image-pos': 136, |
| 5080 | 'atf-fip/soc-fw:offset': 136, |
| 5081 | 'atf-fip/soc-fw:size': 4, |
| 5082 | 'atf-fip/u-boot:image-pos': 140, |
| 5083 | 'atf-fip/u-boot:offset': 140, |
| 5084 | 'atf-fip/u-boot:size': 4, |
| 5085 | 'atf-fip:image-pos': 0, |
| 5086 | 'atf-fip:offset': 0, |
| 5087 | 'atf-fip:size': 144, |
| 5088 | 'image-pos': 0, |
| 5089 | 'offset': 0, |
| 5090 | 'fdtmap:image-pos': fdtmap.image_pos, |
| 5091 | 'fdtmap:offset': fdtmap.offset, |
| 5092 | 'fdtmap:size': len(fdtmap_data), |
| 5093 | 'size': len(data), |
| 5094 | }, props) |
| 5095 | |
| 5096 | def testFipExtractOneEntry(self): |
| 5097 | """Test extracting a single entry fron an FIP""" |
| 5098 | self._DoReadFileRealDtb('207_fip_ls.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5099 | image_fname = tools.get_output_filename('image.bin') |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5100 | fname = os.path.join(self._indir, 'output.extact') |
| 5101 | control.ExtractEntries(image_fname, fname, None, ['atf-fip/u-boot']) |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5102 | data = tools.read_file(fname) |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5103 | self.assertEqual(U_BOOT_DATA, data) |
| 5104 | |
| 5105 | def testFipReplace(self): |
| 5106 | """Test replacing a single file in a FIP""" |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5107 | expected = U_BOOT_DATA + tools.get_bytes(0x78, 50) |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5108 | data = self._DoReadFileRealDtb('208_fip_replace.dts') |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5109 | updated_fname = tools.get_output_filename('image-updated.bin') |
| 5110 | tools.write_file(updated_fname, data) |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5111 | entry_name = 'atf-fip/u-boot' |
| 5112 | control.WriteEntry(updated_fname, entry_name, expected, |
| 5113 | allow_resize=True) |
| 5114 | actual = control.ReadEntry(updated_fname, entry_name) |
| 5115 | self.assertEqual(expected, actual) |
| 5116 | |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5117 | new_data = tools.read_file(updated_fname) |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5118 | hdr, fents = fip_util.decode_fip(new_data) |
| 5119 | |
| 5120 | self.assertEqual(2, len(fents)) |
| 5121 | |
| 5122 | # Check that the FIP entry is updated |
| 5123 | fent = fents[1] |
| 5124 | self.assertEqual(0x8c, fent.offset) |
| 5125 | self.assertEqual(len(expected), fent.size) |
| 5126 | self.assertEqual(0, fent.flags) |
| 5127 | self.assertEqual(expected, fent.data) |
| 5128 | self.assertEqual(True, fent.valid) |
| 5129 | |
| 5130 | def testFipMissing(self): |
| 5131 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5132 | self._DoTestFile('209_fip_missing.dts', allow_missing=True) |
| 5133 | err = stderr.getvalue() |
| 5134 | self.assertRegex(err, "Image 'main-section'.*missing.*: rmm-fw") |
| 5135 | |
| 5136 | def testFipSize(self): |
| 5137 | """Test a FIP with a size property""" |
| 5138 | data = self._DoReadFile('210_fip_size.dts') |
| 5139 | self.assertEqual(0x100 + len(U_BOOT_DATA), len(data)) |
| 5140 | hdr, fents = fip_util.decode_fip(data) |
| 5141 | self.assertEqual(fip_util.HEADER_MAGIC, hdr.name) |
| 5142 | self.assertEqual(fip_util.HEADER_SERIAL, hdr.serial) |
| 5143 | |
| 5144 | self.assertEqual(1, len(fents)) |
| 5145 | |
| 5146 | fent = fents[0] |
| 5147 | self.assertEqual('soc-fw', fent.fip_type) |
| 5148 | self.assertEqual(0x60, fent.offset) |
| 5149 | self.assertEqual(len(ATF_BL31_DATA), fent.size) |
| 5150 | self.assertEqual(ATF_BL31_DATA, fent.data) |
| 5151 | self.assertEqual(True, fent.valid) |
| 5152 | |
| 5153 | rest = data[0x60 + len(ATF_BL31_DATA):0x100] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5154 | self.assertEqual(tools.get_bytes(0xff, len(rest)), rest) |
Simon Glass | 3efb297 | 2021-11-23 21:08:59 -0700 | [diff] [blame] | 5155 | |
| 5156 | def testFipBadAlign(self): |
| 5157 | """Test that an invalid alignment value in a FIP is detected""" |
| 5158 | with self.assertRaises(ValueError) as e: |
| 5159 | self._DoTestFile('211_fip_bad_align.dts') |
| 5160 | self.assertIn( |
| 5161 | "Node \'/binman/atf-fip\': FIP alignment 31 must be a power of two", |
| 5162 | str(e.exception)) |
| 5163 | |
| 5164 | def testFipCollection(self): |
| 5165 | """Test using a FIP in a collection""" |
| 5166 | data = self._DoReadFile('212_fip_collection.dts') |
| 5167 | entry1 = control.images['image'].GetEntries()['collection'] |
| 5168 | data1 = data[:entry1.size] |
| 5169 | hdr1, fents2 = fip_util.decode_fip(data1) |
| 5170 | |
| 5171 | entry2 = control.images['image'].GetEntries()['atf-fip'] |
| 5172 | data2 = data[entry2.offset:entry2.offset + entry2.size] |
| 5173 | hdr1, fents2 = fip_util.decode_fip(data2) |
| 5174 | |
| 5175 | # The 'collection' entry should have U-Boot included at the end |
| 5176 | self.assertEqual(entry1.size - len(U_BOOT_DATA), entry2.size) |
| 5177 | self.assertEqual(data1, data2 + U_BOOT_DATA) |
| 5178 | self.assertEqual(U_BOOT_DATA, data1[-4:]) |
| 5179 | |
| 5180 | # There should be a U-Boot after the final FIP |
| 5181 | self.assertEqual(U_BOOT_DATA, data[-4:]) |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 5182 | |
Simon Glass | ccae686 | 2022-01-12 13:10:35 -0700 | [diff] [blame] | 5183 | def testFakeBlob(self): |
| 5184 | """Test handling of faking an external blob""" |
| 5185 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5186 | self._DoTestFile('217_fake_blob.dts', allow_missing=True, |
| 5187 | allow_fake_blobs=True) |
| 5188 | err = stderr.getvalue() |
| 5189 | self.assertRegex( |
| 5190 | err, |
| 5191 | "Image '.*' has faked external blobs and is non-functional: .*") |
Simon Glass | ccae686 | 2022-01-12 13:10:35 -0700 | [diff] [blame] | 5192 | |
Simon Glass | ceb5f91 | 2022-01-09 20:13:46 -0700 | [diff] [blame] | 5193 | def testExtblobListFaked(self): |
| 5194 | """Test an extblob with missing external blob that are faked""" |
| 5195 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5196 | self._DoTestFile('216_blob_ext_list_missing.dts', |
| 5197 | allow_fake_blobs=True) |
| 5198 | err = stderr.getvalue() |
| 5199 | self.assertRegex(err, "Image 'main-section'.*faked.*: blob-ext-list") |
| 5200 | |
Simon Glass | 162017b | 2022-01-09 20:13:57 -0700 | [diff] [blame] | 5201 | def testListBintools(self): |
| 5202 | args = ['tool', '--list'] |
| 5203 | with test_util.capture_sys_output() as (stdout, _): |
| 5204 | self._DoBinman(*args) |
| 5205 | out = stdout.getvalue().splitlines() |
| 5206 | self.assertTrue(len(out) >= 2) |
| 5207 | |
| 5208 | def testFetchBintools(self): |
| 5209 | def fail_download(url): |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5210 | """Take the tools.download() function by raising an exception""" |
Simon Glass | 162017b | 2022-01-09 20:13:57 -0700 | [diff] [blame] | 5211 | raise urllib.error.URLError('my error') |
| 5212 | |
| 5213 | args = ['tool'] |
| 5214 | with self.assertRaises(ValueError) as e: |
| 5215 | self._DoBinman(*args) |
| 5216 | self.assertIn("Invalid arguments to 'tool' subcommand", |
| 5217 | str(e.exception)) |
| 5218 | |
| 5219 | args = ['tool', '--fetch'] |
| 5220 | with self.assertRaises(ValueError) as e: |
| 5221 | self._DoBinman(*args) |
| 5222 | self.assertIn('Please specify bintools to fetch', str(e.exception)) |
| 5223 | |
| 5224 | args = ['tool', '--fetch', '_testing'] |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 5225 | with unittest.mock.patch.object(tools, 'download', |
Simon Glass | 162017b | 2022-01-09 20:13:57 -0700 | [diff] [blame] | 5226 | side_effect=fail_download): |
| 5227 | with test_util.capture_sys_output() as (stdout, _): |
| 5228 | self._DoBinman(*args) |
| 5229 | self.assertIn('failed to fetch with all methods', stdout.getvalue()) |
| 5230 | |
Simon Glass | 620c446 | 2022-01-09 20:14:11 -0700 | [diff] [blame] | 5231 | def testBintoolDocs(self): |
| 5232 | """Test for creation of bintool documentation""" |
| 5233 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5234 | control.write_bintool_docs(control.bintool.Bintool.get_tool_list()) |
| 5235 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 5236 | |
| 5237 | def testBintoolDocsMissing(self): |
| 5238 | """Test handling of missing bintool documentation""" |
| 5239 | with self.assertRaises(ValueError) as e: |
| 5240 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5241 | control.write_bintool_docs( |
| 5242 | control.bintool.Bintool.get_tool_list(), 'mkimage') |
| 5243 | self.assertIn('Documentation is missing for modules: mkimage', |
| 5244 | str(e.exception)) |
| 5245 | |
Jan Kiszka | 58c407f | 2022-01-28 20:37:53 +0100 | [diff] [blame] | 5246 | def testListWithGenNode(self): |
| 5247 | """Check handling of an FDT map when the section cannot be found""" |
| 5248 | entry_args = { |
| 5249 | 'of-list': 'test-fdt1 test-fdt2', |
| 5250 | } |
| 5251 | data = self._DoReadFileDtb( |
| 5252 | '219_fit_gennode.dts', |
| 5253 | entry_args=entry_args, |
| 5254 | use_real_dtb=True, |
| 5255 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)]) |
| 5256 | |
| 5257 | try: |
| 5258 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 5259 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5260 | self._RunBinman('ls', '-i', updated_fname) |
| 5261 | finally: |
| 5262 | shutil.rmtree(tmpdir) |
| 5263 | |
Alper Nebi Yasak | e706d87 | 2022-02-08 01:08:05 +0300 | [diff] [blame] | 5264 | def testFitSubentryUsesBintool(self): |
| 5265 | """Test that binman FIT subentries can use bintools""" |
| 5266 | command.test_result = self._HandleGbbCommand |
| 5267 | entry_args = { |
| 5268 | 'keydir': 'devkeys', |
| 5269 | 'bmpblk': 'bmpblk.bin', |
| 5270 | } |
| 5271 | data, _, _, _ = self._DoReadFileDtb('220_fit_subentry_bintool.dts', |
| 5272 | entry_args=entry_args) |
| 5273 | |
Alper Nebi Yasak | d455326 | 2022-02-08 01:08:07 +0300 | [diff] [blame] | 5274 | expected = (GBB_DATA + GBB_DATA + tools.get_bytes(0, 8) + |
| 5275 | tools.get_bytes(0, 0x2180 - 16)) |
Alper Nebi Yasak | e706d87 | 2022-02-08 01:08:05 +0300 | [diff] [blame] | 5276 | self.assertIn(expected, data) |
| 5277 | |
| 5278 | def testFitSubentryMissingBintool(self): |
| 5279 | """Test that binman reports missing bintools for FIT subentries""" |
| 5280 | entry_args = { |
| 5281 | 'keydir': 'devkeys', |
| 5282 | } |
| 5283 | with test_util.capture_sys_output() as (_, stderr): |
| 5284 | self._DoTestFile('220_fit_subentry_bintool.dts', |
| 5285 | force_missing_bintools='futility', entry_args=entry_args) |
| 5286 | err = stderr.getvalue() |
| 5287 | self.assertRegex(err, |
| 5288 | "Image 'main-section'.*missing bintools.*: futility") |
Simon Glass | ccae686 | 2022-01-12 13:10:35 -0700 | [diff] [blame] | 5289 | |
Alper Nebi Yasak | 1e4ffd8 | 2022-02-09 22:02:35 +0300 | [diff] [blame] | 5290 | def testFitSubentryHashSubnode(self): |
| 5291 | """Test an image with a FIT inside""" |
| 5292 | data, _, _, out_dtb_name = self._DoReadFileDtb( |
| 5293 | '221_fit_subentry_hash.dts', use_real_dtb=True, update_dtb=True) |
| 5294 | |
| 5295 | mkimage_dtb = fdt.Fdt.FromData(data) |
| 5296 | mkimage_dtb.Scan() |
| 5297 | binman_dtb = fdt.Fdt(out_dtb_name) |
| 5298 | binman_dtb.Scan() |
| 5299 | |
| 5300 | # Check that binman didn't add hash values |
| 5301 | fnode = binman_dtb.GetNode('/binman/fit/images/kernel/hash') |
| 5302 | self.assertNotIn('value', fnode.props) |
| 5303 | |
| 5304 | fnode = binman_dtb.GetNode('/binman/fit/images/fdt-1/hash') |
| 5305 | self.assertNotIn('value', fnode.props) |
| 5306 | |
| 5307 | # Check that mkimage added hash values |
| 5308 | fnode = mkimage_dtb.GetNode('/images/kernel/hash') |
| 5309 | self.assertIn('value', fnode.props) |
| 5310 | |
| 5311 | fnode = mkimage_dtb.GetNode('/images/fdt-1/hash') |
| 5312 | self.assertIn('value', fnode.props) |
| 5313 | |
Roger Quadros | 5cdcea0 | 2022-02-19 20:50:04 +0200 | [diff] [blame] | 5314 | def testPackTeeOs(self): |
| 5315 | """Test that an image with an TEE binary can be created""" |
| 5316 | data = self._DoReadFile('222_tee_os.dts') |
| 5317 | self.assertEqual(TEE_OS_DATA, data[:len(TEE_OS_DATA)]) |
| 5318 | |
Simon Glass | 912339f | 2022-02-08 11:50:03 -0700 | [diff] [blame] | 5319 | def testFitFdtOper(self): |
| 5320 | """Check handling of a specified FIT operation""" |
| 5321 | entry_args = { |
| 5322 | 'of-list': 'test-fdt1 test-fdt2', |
| 5323 | 'default-dt': 'test-fdt2', |
| 5324 | } |
| 5325 | self._DoReadFileDtb( |
| 5326 | '223_fit_fdt_oper.dts', |
| 5327 | entry_args=entry_args, |
| 5328 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 5329 | |
| 5330 | def testFitFdtBadOper(self): |
| 5331 | """Check handling of an FDT map when the section cannot be found""" |
| 5332 | with self.assertRaises(ValueError) as exc: |
| 5333 | self._DoReadFileDtb('224_fit_bad_oper.dts') |
Simon Glass | 05f71dc | 2022-03-05 20:19:09 -0700 | [diff] [blame] | 5334 | self.assertIn("Node '/binman/fit': subnode 'images/@fdt-SEQ': Unknown operation 'unknown'", |
Simon Glass | 912339f | 2022-02-08 11:50:03 -0700 | [diff] [blame] | 5335 | str(exc.exception)) |
| 5336 | |
Simon Glass | dd156a4 | 2022-03-05 20:18:59 -0700 | [diff] [blame] | 5337 | def test_uses_expand_size(self): |
| 5338 | """Test that the 'expand-size' property cannot be used anymore""" |
| 5339 | with self.assertRaises(ValueError) as e: |
| 5340 | data = self._DoReadFile('225_expand_size_bad.dts') |
| 5341 | self.assertIn( |
| 5342 | "Node '/binman/u-boot': Please use 'extend-size' instead of 'expand-size'", |
| 5343 | str(e.exception)) |
| 5344 | |
Simon Glass | fc5a168 | 2022-03-05 20:19:05 -0700 | [diff] [blame] | 5345 | def testMkimageMissingBlob(self): |
| 5346 | """Test using mkimage to build an image""" |
| 5347 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5348 | self._DoTestFile('229_mkimage_missing.dts', allow_missing=True, |
| 5349 | allow_fake_blobs=True) |
| 5350 | err = stderr.getvalue() |
| 5351 | self.assertRegex( |
| 5352 | err, |
| 5353 | "Image '.*' has faked external blobs and is non-functional: .*") |
| 5354 | |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5355 | def testFitSplitElf(self): |
| 5356 | """Test an image with an FIT with an split-elf operation""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 5357 | if not elf.ELF_TOOLS: |
| 5358 | self.skipTest('Python elftools not available') |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5359 | entry_args = { |
| 5360 | 'of-list': 'test-fdt1 test-fdt2', |
| 5361 | 'default-dt': 'test-fdt2', |
| 5362 | 'atf-bl31-path': 'bl31.elf', |
| 5363 | 'tee-os-path': 'tee.elf', |
| 5364 | } |
| 5365 | test_subdir = os.path.join(self._indir, TEST_FDT_SUBDIR) |
| 5366 | data = self._DoReadFileDtb( |
| 5367 | '226_fit_split_elf.dts', |
| 5368 | entry_args=entry_args, |
| 5369 | extra_indirs=[test_subdir])[0] |
| 5370 | |
| 5371 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 5372 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 5373 | |
| 5374 | base_keys = {'description', 'type', 'arch', 'os', 'compression', |
| 5375 | 'data', 'load'} |
| 5376 | dtb = fdt.Fdt.FromData(fit_data) |
| 5377 | dtb.Scan() |
| 5378 | |
| 5379 | elf_data = tools.read_file(os.path.join(self._indir, 'bl31.elf')) |
| 5380 | segments, entry = elf.read_loadable_segments(elf_data) |
| 5381 | |
| 5382 | # We assume there are two segments |
| 5383 | self.assertEquals(2, len(segments)) |
| 5384 | |
| 5385 | atf1 = dtb.GetNode('/images/atf-1') |
| 5386 | _, start, data = segments[0] |
| 5387 | self.assertEqual(base_keys | {'entry'}, atf1.props.keys()) |
| 5388 | self.assertEqual(entry, |
| 5389 | fdt_util.fdt32_to_cpu(atf1.props['entry'].value)) |
| 5390 | self.assertEqual(start, |
| 5391 | fdt_util.fdt32_to_cpu(atf1.props['load'].value)) |
| 5392 | self.assertEqual(data, atf1.props['data'].bytes) |
| 5393 | |
| 5394 | atf2 = dtb.GetNode('/images/atf-2') |
| 5395 | self.assertEqual(base_keys, atf2.props.keys()) |
| 5396 | _, start, data = segments[1] |
| 5397 | self.assertEqual(start, |
| 5398 | fdt_util.fdt32_to_cpu(atf2.props['load'].value)) |
| 5399 | self.assertEqual(data, atf2.props['data'].bytes) |
| 5400 | |
| 5401 | conf = dtb.GetNode('/configurations') |
| 5402 | self.assertEqual({'default'}, conf.props.keys()) |
| 5403 | |
| 5404 | for subnode in conf.subnodes: |
| 5405 | self.assertEqual({'description', 'fdt', 'loadables'}, |
| 5406 | subnode.props.keys()) |
| 5407 | self.assertEqual( |
| 5408 | ['atf-1', 'atf-2', 'tee-1', 'tee-2'], |
| 5409 | fdt_util.GetStringList(subnode, 'loadables')) |
| 5410 | |
| 5411 | def _check_bad_fit(self, dts): |
| 5412 | """Check a bad FIT |
| 5413 | |
| 5414 | This runs with the given dts and returns the assertion raised |
| 5415 | |
| 5416 | Args: |
| 5417 | dts (str): dts filename to use |
| 5418 | |
| 5419 | Returns: |
| 5420 | str: Assertion string raised |
| 5421 | """ |
| 5422 | entry_args = { |
| 5423 | 'of-list': 'test-fdt1 test-fdt2', |
| 5424 | 'default-dt': 'test-fdt2', |
| 5425 | 'atf-bl31-path': 'bl31.elf', |
| 5426 | 'tee-os-path': 'tee.elf', |
| 5427 | } |
| 5428 | test_subdir = os.path.join(self._indir, TEST_FDT_SUBDIR) |
| 5429 | with self.assertRaises(ValueError) as exc: |
| 5430 | self._DoReadFileDtb(dts, entry_args=entry_args, |
| 5431 | extra_indirs=[test_subdir])[0] |
| 5432 | return str(exc.exception) |
| 5433 | |
| 5434 | def testFitSplitElfBadElf(self): |
| 5435 | """Test a FIT split-elf operation with an invalid ELF file""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 5436 | if not elf.ELF_TOOLS: |
| 5437 | self.skipTest('Python elftools not available') |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5438 | TestFunctional._MakeInputFile('bad.elf', tools.get_bytes(100, 100)) |
| 5439 | entry_args = { |
| 5440 | 'of-list': 'test-fdt1 test-fdt2', |
| 5441 | 'default-dt': 'test-fdt2', |
| 5442 | 'atf-bl31-path': 'bad.elf', |
| 5443 | 'tee-os-path': 'tee.elf', |
| 5444 | } |
| 5445 | test_subdir = os.path.join(self._indir, TEST_FDT_SUBDIR) |
| 5446 | with self.assertRaises(ValueError) as exc: |
| 5447 | self._DoReadFileDtb( |
| 5448 | '226_fit_split_elf.dts', |
| 5449 | entry_args=entry_args, |
| 5450 | extra_indirs=[test_subdir])[0] |
| 5451 | self.assertIn( |
| 5452 | "Node '/binman/fit': subnode 'images/@atf-SEQ': Failed to read ELF file: Magic number does not match", |
| 5453 | str(exc.exception)) |
| 5454 | |
| 5455 | def testFitSplitElfBadDirective(self): |
| 5456 | """Test a FIT split-elf invalid fit,xxx directive in an image node""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 5457 | if not elf.ELF_TOOLS: |
| 5458 | self.skipTest('Python elftools not available') |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5459 | err = self._check_bad_fit('227_fit_bad_dir.dts') |
| 5460 | self.assertIn( |
| 5461 | "Node '/binman/fit': subnode 'images/@atf-SEQ': Unknown directive 'fit,something'", |
| 5462 | err) |
| 5463 | |
| 5464 | def testFitSplitElfBadDirectiveConfig(self): |
| 5465 | """Test a FIT split-elf with invalid fit,xxx directive in config""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 5466 | if not elf.ELF_TOOLS: |
| 5467 | self.skipTest('Python elftools not available') |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5468 | err = self._check_bad_fit('228_fit_bad_dir_config.dts') |
| 5469 | self.assertEqual( |
| 5470 | "Node '/binman/fit': subnode 'configurations/@config-SEQ': Unknown directive 'fit,config'", |
| 5471 | err) |
| 5472 | |
| 5473 | def checkFitSplitElf(self, **kwargs): |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 5474 | """Test an split-elf FIT with a missing ELF file |
| 5475 | |
| 5476 | Args: |
| 5477 | kwargs (dict of str): Arguments to pass to _DoTestFile() |
| 5478 | |
| 5479 | Returns: |
| 5480 | tuple: |
| 5481 | str: stdout result |
| 5482 | str: stderr result |
| 5483 | """ |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5484 | entry_args = { |
| 5485 | 'of-list': 'test-fdt1 test-fdt2', |
| 5486 | 'default-dt': 'test-fdt2', |
| 5487 | 'atf-bl31-path': 'bl31.elf', |
| 5488 | 'tee-os-path': 'missing.elf', |
| 5489 | } |
| 5490 | test_subdir = os.path.join(self._indir, TEST_FDT_SUBDIR) |
| 5491 | with test_util.capture_sys_output() as (stdout, stderr): |
| 5492 | self._DoTestFile( |
| 5493 | '226_fit_split_elf.dts', entry_args=entry_args, |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 5494 | extra_indirs=[test_subdir], verbosity=3, **kwargs) |
| 5495 | out = stdout.getvalue() |
| 5496 | err = stderr.getvalue() |
| 5497 | return out, err |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5498 | |
| 5499 | def testFitSplitElfMissing(self): |
| 5500 | """Test an split-elf FIT with a missing ELF file""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 5501 | if not elf.ELF_TOOLS: |
| 5502 | self.skipTest('Python elftools not available') |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 5503 | out, err = self.checkFitSplitElf(allow_missing=True) |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5504 | self.assertRegex( |
| 5505 | err, |
| 5506 | "Image '.*' is missing external blobs and is non-functional: .*") |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 5507 | self.assertNotRegex(out, '.*Faked blob.*') |
| 5508 | fname = tools.get_output_filename('binman-fake/missing.elf') |
| 5509 | self.assertFalse(os.path.exists(fname)) |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5510 | |
| 5511 | def testFitSplitElfFaked(self): |
| 5512 | """Test an split-elf FIT with faked ELF file""" |
Stefan Herbrechtsmeier | 732742e | 2022-08-19 16:25:18 +0200 | [diff] [blame] | 5513 | if not elf.ELF_TOOLS: |
| 5514 | self.skipTest('Python elftools not available') |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 5515 | out, err = self.checkFitSplitElf(allow_missing=True, allow_fake_blobs=True) |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5516 | self.assertRegex( |
| 5517 | err, |
| 5518 | "Image '.*' is missing external blobs and is non-functional: .*") |
Simon Glass | 7d3e407 | 2022-08-07 09:46:46 -0600 | [diff] [blame] | 5519 | self.assertRegex( |
| 5520 | out, |
| 5521 | "Entry '/binman/fit/images/@tee-SEQ/tee-os': Faked blob '.*binman-fake/missing.elf") |
| 5522 | fname = tools.get_output_filename('binman-fake/missing.elf') |
| 5523 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 5f42342 | 2022-03-05 20:19:12 -0700 | [diff] [blame] | 5524 | |
Philippe Reynes | ebe96cb | 2022-03-28 22:57:04 +0200 | [diff] [blame] | 5525 | def testPreLoad(self): |
| 5526 | """Test an image with a pre-load header""" |
| 5527 | entry_args = { |
| 5528 | 'pre-load-key-path': '.', |
| 5529 | } |
| 5530 | data, _, _, _ = self._DoReadFileDtb('225_pre_load.dts', |
| 5531 | entry_args=entry_args) |
| 5532 | self.assertEqual(PRE_LOAD_MAGIC, data[:len(PRE_LOAD_MAGIC)]) |
| 5533 | self.assertEqual(PRE_LOAD_VERSION, data[4:4 + len(PRE_LOAD_VERSION)]) |
| 5534 | self.assertEqual(PRE_LOAD_HDR_SIZE, data[8:8 + len(PRE_LOAD_HDR_SIZE)]) |
| 5535 | data = self._DoReadFile('225_pre_load.dts') |
| 5536 | self.assertEqual(PRE_LOAD_MAGIC, data[:len(PRE_LOAD_MAGIC)]) |
| 5537 | self.assertEqual(PRE_LOAD_VERSION, data[4:4 + len(PRE_LOAD_VERSION)]) |
| 5538 | self.assertEqual(PRE_LOAD_HDR_SIZE, data[8:8 + len(PRE_LOAD_HDR_SIZE)]) |
| 5539 | |
| 5540 | def testPreLoadPkcs(self): |
| 5541 | """Test an image with a pre-load header with padding pkcs""" |
| 5542 | data = self._DoReadFile('226_pre_load_pkcs.dts') |
| 5543 | self.assertEqual(PRE_LOAD_MAGIC, data[:len(PRE_LOAD_MAGIC)]) |
| 5544 | self.assertEqual(PRE_LOAD_VERSION, data[4:4 + len(PRE_LOAD_VERSION)]) |
| 5545 | self.assertEqual(PRE_LOAD_HDR_SIZE, data[8:8 + len(PRE_LOAD_HDR_SIZE)]) |
| 5546 | |
| 5547 | def testPreLoadPss(self): |
| 5548 | """Test an image with a pre-load header with padding pss""" |
| 5549 | data = self._DoReadFile('227_pre_load_pss.dts') |
| 5550 | self.assertEqual(PRE_LOAD_MAGIC, data[:len(PRE_LOAD_MAGIC)]) |
| 5551 | self.assertEqual(PRE_LOAD_VERSION, data[4:4 + len(PRE_LOAD_VERSION)]) |
| 5552 | self.assertEqual(PRE_LOAD_HDR_SIZE, data[8:8 + len(PRE_LOAD_HDR_SIZE)]) |
| 5553 | |
| 5554 | def testPreLoadInvalidPadding(self): |
| 5555 | """Test an image with a pre-load header with an invalid padding""" |
| 5556 | with self.assertRaises(ValueError) as e: |
| 5557 | data = self._DoReadFile('228_pre_load_invalid_padding.dts') |
| 5558 | |
| 5559 | def testPreLoadInvalidSha(self): |
| 5560 | """Test an image with a pre-load header with an invalid hash""" |
| 5561 | with self.assertRaises(ValueError) as e: |
| 5562 | data = self._DoReadFile('229_pre_load_invalid_sha.dts') |
| 5563 | |
| 5564 | def testPreLoadInvalidAlgo(self): |
| 5565 | """Test an image with a pre-load header with an invalid algo""" |
| 5566 | with self.assertRaises(ValueError) as e: |
| 5567 | data = self._DoReadFile('230_pre_load_invalid_algo.dts') |
| 5568 | |
| 5569 | def testPreLoadInvalidKey(self): |
| 5570 | """Test an image with a pre-load header with an invalid key""" |
| 5571 | with self.assertRaises(ValueError) as e: |
| 5572 | data = self._DoReadFile('231_pre_load_invalid_key.dts') |
Roger Quadros | 5cdcea0 | 2022-02-19 20:50:04 +0200 | [diff] [blame] | 5573 | |
Alper Nebi Yasak | 5cff63f | 2022-03-27 18:31:44 +0300 | [diff] [blame] | 5574 | def _CheckSafeUniqueNames(self, *images): |
| 5575 | """Check all entries of given images for unsafe unique names""" |
| 5576 | for image in images: |
| 5577 | entries = {} |
| 5578 | image._CollectEntries(entries, {}, image) |
| 5579 | for entry in entries.values(): |
| 5580 | uniq = entry.GetUniqueName() |
| 5581 | |
| 5582 | # Used as part of a filename, so must not be absolute paths. |
| 5583 | self.assertFalse(os.path.isabs(uniq)) |
| 5584 | |
| 5585 | def testSafeUniqueNames(self): |
| 5586 | """Test entry unique names are safe in single image configuration""" |
| 5587 | data = self._DoReadFileRealDtb('230_unique_names.dts') |
| 5588 | |
| 5589 | orig_image = control.images['image'] |
| 5590 | image_fname = tools.get_output_filename('image.bin') |
| 5591 | image = Image.FromFile(image_fname) |
| 5592 | |
| 5593 | self._CheckSafeUniqueNames(orig_image, image) |
| 5594 | |
| 5595 | def testSafeUniqueNamesMulti(self): |
| 5596 | """Test entry unique names are safe with multiple images""" |
| 5597 | data = self._DoReadFileRealDtb('231_unique_names_multi.dts') |
| 5598 | |
| 5599 | orig_image = control.images['image'] |
| 5600 | image_fname = tools.get_output_filename('image.bin') |
| 5601 | image = Image.FromFile(image_fname) |
| 5602 | |
| 5603 | self._CheckSafeUniqueNames(orig_image, image) |
| 5604 | |
Alper Nebi Yasak | e63ca5a | 2022-03-27 18:31:45 +0300 | [diff] [blame] | 5605 | def testReplaceCmdWithBintool(self): |
| 5606 | """Test replacing an entry that needs a bintool to pack""" |
| 5607 | data = self._DoReadFileRealDtb('232_replace_with_bintool.dts') |
| 5608 | expected = U_BOOT_DATA + b'aa' |
| 5609 | self.assertEqual(expected, data[:len(expected)]) |
| 5610 | |
| 5611 | try: |
| 5612 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 5613 | fname = os.path.join(tmpdir, 'update-testing.bin') |
| 5614 | tools.write_file(fname, b'zz') |
| 5615 | self._DoBinman('replace', '-i', updated_fname, |
| 5616 | '_testing', '-f', fname) |
| 5617 | |
| 5618 | data = tools.read_file(updated_fname) |
| 5619 | expected = U_BOOT_DATA + b'zz' |
| 5620 | self.assertEqual(expected, data[:len(expected)]) |
| 5621 | finally: |
| 5622 | shutil.rmtree(tmpdir) |
| 5623 | |
| 5624 | def testReplaceCmdOtherWithBintool(self): |
| 5625 | """Test replacing an entry when another needs a bintool to pack""" |
| 5626 | data = self._DoReadFileRealDtb('232_replace_with_bintool.dts') |
| 5627 | expected = U_BOOT_DATA + b'aa' |
| 5628 | self.assertEqual(expected, data[:len(expected)]) |
| 5629 | |
| 5630 | try: |
| 5631 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 5632 | fname = os.path.join(tmpdir, 'update-u-boot.bin') |
| 5633 | tools.write_file(fname, b'x' * len(U_BOOT_DATA)) |
| 5634 | self._DoBinman('replace', '-i', updated_fname, |
| 5635 | 'u-boot', '-f', fname) |
| 5636 | |
| 5637 | data = tools.read_file(updated_fname) |
| 5638 | expected = b'x' * len(U_BOOT_DATA) + b'aa' |
| 5639 | self.assertEqual(expected, data[:len(expected)]) |
| 5640 | finally: |
| 5641 | shutil.rmtree(tmpdir) |
| 5642 | |
Alper Nebi Yasak | 00c68f1 | 2022-03-27 18:31:46 +0300 | [diff] [blame] | 5643 | def testReplaceResizeNoRepackSameSize(self): |
| 5644 | """Test replacing entries with same-size data without repacking""" |
| 5645 | expected = b'x' * len(U_BOOT_DATA) |
| 5646 | data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', expected) |
| 5647 | self.assertEqual(expected, data) |
| 5648 | |
| 5649 | path, fdtmap = state.GetFdtContents('fdtmap') |
| 5650 | self.assertIsNotNone(path) |
| 5651 | self.assertEqual(expected_fdtmap, fdtmap) |
| 5652 | |
| 5653 | def testReplaceResizeNoRepackSmallerSize(self): |
| 5654 | """Test replacing entries with smaller-size data without repacking""" |
| 5655 | new_data = b'x' |
| 5656 | data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', new_data) |
| 5657 | expected = new_data.ljust(len(U_BOOT_DATA), b'\0') |
| 5658 | self.assertEqual(expected, data) |
| 5659 | |
| 5660 | path, fdtmap = state.GetFdtContents('fdtmap') |
| 5661 | self.assertIsNotNone(path) |
| 5662 | self.assertEqual(expected_fdtmap, fdtmap) |
| 5663 | |
Alper Nebi Yasak | 6cadc50 | 2022-03-27 18:31:48 +0300 | [diff] [blame] | 5664 | def testExtractFit(self): |
| 5665 | """Test extracting a FIT section""" |
| 5666 | self._DoReadFileRealDtb('233_fit_extract_replace.dts') |
| 5667 | image_fname = tools.get_output_filename('image.bin') |
| 5668 | |
| 5669 | fit_data = control.ReadEntry(image_fname, 'fit') |
| 5670 | fit = fdt.Fdt.FromData(fit_data) |
| 5671 | fit.Scan() |
| 5672 | |
| 5673 | # Check subentry data inside the extracted fit |
| 5674 | for node_path, expected in [ |
| 5675 | ('/images/kernel', U_BOOT_DATA), |
| 5676 | ('/images/fdt-1', U_BOOT_NODTB_DATA), |
| 5677 | ('/images/scr-1', COMPRESS_DATA), |
| 5678 | ]: |
| 5679 | node = fit.GetNode(node_path) |
| 5680 | data = fit.GetProps(node)['data'].bytes |
| 5681 | self.assertEqual(expected, data) |
| 5682 | |
| 5683 | def testExtractFitSubentries(self): |
| 5684 | """Test extracting FIT section subentries""" |
| 5685 | self._DoReadFileRealDtb('233_fit_extract_replace.dts') |
| 5686 | image_fname = tools.get_output_filename('image.bin') |
| 5687 | |
| 5688 | for entry_path, expected in [ |
| 5689 | ('fit/kernel', U_BOOT_DATA), |
| 5690 | ('fit/kernel/u-boot', U_BOOT_DATA), |
| 5691 | ('fit/fdt-1', U_BOOT_NODTB_DATA), |
| 5692 | ('fit/fdt-1/u-boot-nodtb', U_BOOT_NODTB_DATA), |
| 5693 | ('fit/scr-1', COMPRESS_DATA), |
| 5694 | ('fit/scr-1/blob', COMPRESS_DATA), |
| 5695 | ]: |
| 5696 | data = control.ReadEntry(image_fname, entry_path) |
| 5697 | self.assertEqual(expected, data) |
| 5698 | |
Alper Nebi Yasak | 4989264 | 2022-03-27 18:31:49 +0300 | [diff] [blame] | 5699 | def testReplaceFitSubentryLeafSameSize(self): |
| 5700 | """Test replacing a FIT leaf subentry with same-size data""" |
| 5701 | new_data = b'x' * len(U_BOOT_DATA) |
| 5702 | data, expected_fdtmap, _ = self._RunReplaceCmd( |
| 5703 | 'fit/kernel/u-boot', new_data, |
| 5704 | dts='233_fit_extract_replace.dts') |
| 5705 | self.assertEqual(new_data, data) |
| 5706 | |
| 5707 | path, fdtmap = state.GetFdtContents('fdtmap') |
| 5708 | self.assertIsNotNone(path) |
| 5709 | self.assertEqual(expected_fdtmap, fdtmap) |
| 5710 | |
| 5711 | def testReplaceFitSubentryLeafBiggerSize(self): |
| 5712 | """Test replacing a FIT leaf subentry with bigger-size data""" |
| 5713 | new_data = b'ub' * len(U_BOOT_NODTB_DATA) |
| 5714 | data, expected_fdtmap, _ = self._RunReplaceCmd( |
| 5715 | 'fit/fdt-1/u-boot-nodtb', new_data, |
| 5716 | dts='233_fit_extract_replace.dts') |
| 5717 | self.assertEqual(new_data, data) |
| 5718 | |
| 5719 | # Will be repacked, so fdtmap must change |
| 5720 | path, fdtmap = state.GetFdtContents('fdtmap') |
| 5721 | self.assertIsNotNone(path) |
| 5722 | self.assertNotEqual(expected_fdtmap, fdtmap) |
| 5723 | |
| 5724 | def testReplaceFitSubentryLeafSmallerSize(self): |
| 5725 | """Test replacing a FIT leaf subentry with smaller-size data""" |
| 5726 | new_data = b'x' |
| 5727 | expected = new_data.ljust(len(U_BOOT_NODTB_DATA), b'\0') |
| 5728 | data, expected_fdtmap, _ = self._RunReplaceCmd( |
| 5729 | 'fit/fdt-1/u-boot-nodtb', new_data, |
| 5730 | dts='233_fit_extract_replace.dts') |
| 5731 | self.assertEqual(expected, data) |
| 5732 | |
| 5733 | path, fdtmap = state.GetFdtContents('fdtmap') |
| 5734 | self.assertIsNotNone(path) |
| 5735 | self.assertEqual(expected_fdtmap, fdtmap) |
| 5736 | |
Alper Nebi Yasak | 1d44c8e | 2022-03-27 18:31:50 +0300 | [diff] [blame] | 5737 | def testReplaceSectionSimple(self): |
| 5738 | """Test replacing a simple section with arbitrary data""" |
| 5739 | new_data = b'w' * len(COMPRESS_DATA + U_BOOT_DATA) |
Simon Glass | c6b283f | 2022-08-13 11:40:46 -0600 | [diff] [blame] | 5740 | with self.assertRaises(ValueError) as exc: |
| 5741 | self._RunReplaceCmd('section', new_data, |
| 5742 | dts='234_replace_section_simple.dts') |
| 5743 | self.assertIn( |
| 5744 | "Node '/section': Replacing sections is not implemented yet", |
| 5745 | str(exc.exception)) |
Alper Nebi Yasak | 1d44c8e | 2022-03-27 18:31:50 +0300 | [diff] [blame] | 5746 | |
Simon Glass | 8fbca77 | 2022-08-13 11:40:48 -0600 | [diff] [blame] | 5747 | def testMkimageImagename(self): |
| 5748 | """Test using mkimage with -n holding the data too""" |
| 5749 | data = self._DoReadFile('235_mkimage_name.dts') |
| 5750 | |
| 5751 | # Check that the data appears in the file somewhere |
| 5752 | self.assertIn(U_BOOT_SPL_DATA, data) |
| 5753 | |
| 5754 | # Get struct image_header -> ih_name |
| 5755 | name = data[0x20:0x40] |
| 5756 | |
| 5757 | # Build the filename that we expect to be placed in there, by virtue of |
| 5758 | # the -n paraameter |
| 5759 | expect = os.path.join(tools.get_output_dir(), 'mkimage.mkimage') |
| 5760 | |
| 5761 | # Check that the image name is set to the temporary filename used |
| 5762 | self.assertEqual(expect.encode('utf-8')[:0x20], name) |
| 5763 | |
Simon Glass | b166975 | 2022-08-13 11:40:49 -0600 | [diff] [blame] | 5764 | def testMkimageImage(self): |
| 5765 | """Test using mkimage with -n holding the data too""" |
| 5766 | data = self._DoReadFile('236_mkimage_image.dts') |
| 5767 | |
| 5768 | # Check that the data appears in the file somewhere |
| 5769 | self.assertIn(U_BOOT_SPL_DATA, data) |
| 5770 | |
| 5771 | # Get struct image_header -> ih_name |
| 5772 | name = data[0x20:0x40] |
| 5773 | |
| 5774 | # Build the filename that we expect to be placed in there, by virtue of |
| 5775 | # the -n paraameter |
| 5776 | expect = os.path.join(tools.get_output_dir(), 'mkimage-n.mkimage') |
| 5777 | |
| 5778 | # Check that the image name is set to the temporary filename used |
| 5779 | self.assertEqual(expect.encode('utf-8')[:0x20], name) |
| 5780 | |
| 5781 | # Check the corect data is in the imagename file |
| 5782 | self.assertEqual(U_BOOT_DATA, tools.read_file(expect)) |
| 5783 | |
| 5784 | def testMkimageImageNoContent(self): |
| 5785 | """Test using mkimage with -n and no data""" |
| 5786 | with self.assertRaises(ValueError) as exc: |
| 5787 | self._DoReadFile('237_mkimage_image_no_content.dts') |
| 5788 | self.assertIn('Could not complete processing of contents', |
| 5789 | str(exc.exception)) |
| 5790 | |
| 5791 | def testMkimageImageBad(self): |
| 5792 | """Test using mkimage with imagename node and data-to-imagename""" |
| 5793 | with self.assertRaises(ValueError) as exc: |
| 5794 | self._DoReadFile('238_mkimage_image_bad.dts') |
| 5795 | self.assertIn('Cannot use both imagename node and data-to-imagename', |
| 5796 | str(exc.exception)) |
| 5797 | |
Simon Glass | bd5cd88 | 2022-08-13 11:40:50 -0600 | [diff] [blame] | 5798 | def testCollectionOther(self): |
| 5799 | """Test a collection where the data comes from another section""" |
| 5800 | data = self._DoReadFile('239_collection_other.dts') |
| 5801 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA + |
| 5802 | tools.get_bytes(0xff, 2) + U_BOOT_NODTB_DATA + |
| 5803 | tools.get_bytes(0xfe, 3) + U_BOOT_DTB_DATA, |
| 5804 | data) |
| 5805 | |
| 5806 | def testMkimageCollection(self): |
| 5807 | """Test using a collection referring to an entry in a mkimage entry""" |
| 5808 | data = self._DoReadFile('240_mkimage_coll.dts') |
| 5809 | expect = U_BOOT_SPL_DATA + U_BOOT_DATA |
| 5810 | self.assertEqual(expect, data[:len(expect)]) |
| 5811 | |
Stefan Herbrechtsmeier | 11121d3 | 2022-08-19 16:25:25 +0200 | [diff] [blame] | 5812 | def testCompressDtbPrependInvalid(self): |
| 5813 | """Test that invalid header is detected""" |
| 5814 | with self.assertRaises(ValueError) as e: |
| 5815 | self._DoReadFileDtb('235_compress_dtb_prepend_invalid.dts') |
| 5816 | self.assertIn("Node '/binman/u-boot-dtb': Invalid prepend in " |
| 5817 | "'u-boot-dtb': 'invalid'", str(e.exception)) |
| 5818 | |
| 5819 | def testCompressDtbPrependLength(self): |
| 5820 | """Test that compress with length header works as expected""" |
| 5821 | data = self._DoReadFileRealDtb('236_compress_dtb_prepend_length.dts') |
| 5822 | image = control.images['image'] |
| 5823 | entries = image.GetEntries() |
| 5824 | self.assertIn('u-boot-dtb', entries) |
| 5825 | u_boot_dtb = entries['u-boot-dtb'] |
| 5826 | self.assertIn('fdtmap', entries) |
| 5827 | fdtmap = entries['fdtmap'] |
| 5828 | |
| 5829 | image_fname = tools.get_output_filename('image.bin') |
| 5830 | orig = control.ReadEntry(image_fname, 'u-boot-dtb') |
| 5831 | dtb = fdt.Fdt.FromData(orig) |
| 5832 | dtb.Scan() |
| 5833 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 5834 | expected = { |
| 5835 | 'u-boot:size': len(U_BOOT_DATA), |
| 5836 | 'u-boot-dtb:uncomp-size': len(orig), |
| 5837 | 'u-boot-dtb:size': u_boot_dtb.size, |
| 5838 | 'fdtmap:size': fdtmap.size, |
| 5839 | 'size': len(data), |
| 5840 | } |
| 5841 | self.assertEqual(expected, props) |
| 5842 | |
| 5843 | # Check implementation |
| 5844 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 5845 | rest = data[len(U_BOOT_DATA):] |
| 5846 | comp_data_len = struct.unpack('<I', rest[:4])[0] |
| 5847 | comp_data = rest[4:4 + comp_data_len] |
| 5848 | orig2 = self._decompress(comp_data) |
| 5849 | self.assertEqual(orig, orig2) |
| 5850 | |
Stefan Herbrechtsmeier | a6e0b50 | 2022-08-19 16:25:30 +0200 | [diff] [blame^] | 5851 | def testInvalidCompress(self): |
| 5852 | """Test that invalid compress algorithm is detected""" |
| 5853 | with self.assertRaises(ValueError) as e: |
| 5854 | self._DoTestFile('237_compress_dtb_invalid.dts') |
| 5855 | self.assertIn("Unknown algorithm 'invalid'", str(e.exception)) |
| 5856 | |
Alper Nebi Yasak | e63ca5a | 2022-03-27 18:31:45 +0300 | [diff] [blame] | 5857 | |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 5858 | if __name__ == "__main__": |
| 5859 | unittest.main() |