Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 2 | """ |
| 3 | Unit-test runner |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 4 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 5 | Provides a test_ut() function which is used by conftest.py to run each unit |
| 6 | test one at a time, as well setting up some files needed by the tests. |
| 7 | |
| 8 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
| 9 | """ |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 10 | import collections |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 11 | import gzip |
| 12 | import os |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 13 | import os.path |
| 14 | import pytest |
| 15 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 16 | import u_boot_utils |
Tom Rini | 50ad019 | 2023-12-09 14:52:46 -0500 | [diff] [blame] | 17 | # pylint: disable=E0611 |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 18 | from tests import fs_helper |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 19 | from test_android import test_abootimg |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 20 | |
| 21 | def mkdir_cond(dirname): |
| 22 | """Create a directory if it doesn't already exist |
| 23 | |
| 24 | Args: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 25 | dirname (str): Name of directory to create |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 26 | """ |
| 27 | if not os.path.exists(dirname): |
| 28 | os.mkdir(dirname) |
| 29 | |
Simon Glass | d157d3f | 2024-11-21 15:32:09 -0700 | [diff] [blame] | 30 | def setup_image(cons, devnum, part_type, img_size=20, second_part=False, |
| 31 | basename='mmc'): |
| 32 | """Create a disk image with a single partition |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 33 | |
| 34 | Args: |
| 35 | cons (ConsoleBase): Console to use |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 36 | devnum (int): Device number to use, e.g. 1 |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 37 | part_type (int): Partition type, e.g. 0xc for FAT32 |
Simon Glass | d157d3f | 2024-11-21 15:32:09 -0700 | [diff] [blame] | 38 | img_size (int): Image size in MiB |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 39 | second_part (bool): True to contain a small second partition |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 40 | basename (str): Base name to use in the filename, e.g. 'mmc' |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 41 | |
| 42 | Returns: |
| 43 | tuple: |
| 44 | str: Filename of MMC image |
Richard Weinberger | d99fdc0 | 2024-11-21 15:32:10 -0700 | [diff] [blame] | 45 | str: Directory name of scratch directory |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 46 | """ |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 47 | fname = os.path.join(cons.config.source_dir, f'{basename}{devnum}.img') |
Richard Weinberger | d99fdc0 | 2024-11-21 15:32:10 -0700 | [diff] [blame] | 48 | mnt = os.path.join(cons.config.persistent_data_dir, 'scratch') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 49 | mkdir_cond(mnt) |
| 50 | |
Simon Glass | d157d3f | 2024-11-21 15:32:09 -0700 | [diff] [blame] | 51 | spec = f'type={part_type:x}, size={img_size - 2}M, start=1M, bootable' |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 52 | if second_part: |
| 53 | spec += '\ntype=c' |
| 54 | |
Richard Weinberger | d99fdc0 | 2024-11-21 15:32:10 -0700 | [diff] [blame] | 55 | u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') |
| 56 | u_boot_utils.run_and_log(cons, f'sfdisk {fname}', |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 57 | stdin=spec.encode('utf-8')) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 58 | return fname, mnt |
| 59 | |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 60 | def copy_prepared_image(cons, devnum, fname, basename='mmc'): |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 61 | """Use a prepared image since we cannot create one |
| 62 | |
| 63 | Args: |
| 64 | cons (ConsoleBase): Console touse |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 65 | devnum (int): device number |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 66 | fname (str): Filename of MMC image |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 67 | basename (str): Base name to use in the filename, e.g. 'mmc' |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 68 | """ |
| 69 | infname = os.path.join(cons.config.source_dir, |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 70 | f'test/py/tests/bootstd/{basename}{devnum}.img.xz') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 71 | u_boot_utils.run_and_log(cons, ['sh', '-c', f'xz -dc {infname} >{fname}']) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 72 | |
| 73 | def setup_bootmenu_image(cons): |
| 74 | """Create a 20MB disk image with a single ext4 partition |
| 75 | |
| 76 | This is modelled on Armbian 22.08 Jammy |
| 77 | """ |
| 78 | mmc_dev = 4 |
| 79 | fname, mnt = setup_image(cons, mmc_dev, 0x83) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 80 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 81 | complete = False |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 82 | script = '''# DO NOT EDIT THIS FILE |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 83 | # |
| 84 | # Please edit /boot/armbianEnv.txt to set supported parameters |
| 85 | # |
| 86 | |
| 87 | setenv load_addr "0x9000000" |
| 88 | setenv overlay_error "false" |
| 89 | # default values |
| 90 | setenv rootdev "/dev/mmcblk%dp1" |
| 91 | setenv verbosity "1" |
| 92 | setenv console "both" |
| 93 | setenv bootlogo "false" |
| 94 | setenv rootfstype "ext4" |
| 95 | setenv docker_optimizations "on" |
| 96 | setenv earlycon "off" |
| 97 | |
| 98 | echo "Boot script loaded from ${devtype} ${devnum}" |
| 99 | |
| 100 | if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then |
| 101 | load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt |
| 102 | env import -t ${load_addr} ${filesize} |
| 103 | fi |
| 104 | |
| 105 | if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi |
| 106 | |
| 107 | if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi |
| 108 | if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi |
| 109 | if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi |
| 110 | if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi |
| 111 | |
| 112 | # get PARTUUID of first partition on SD/eMMC the boot script was loaded from |
| 113 | if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi |
| 114 | |
| 115 | setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}" |
| 116 | |
| 117 | if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi |
| 118 | |
| 119 | load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd |
| 120 | load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image |
| 121 | |
| 122 | load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile} |
| 123 | fdt addr ${fdt_addr_r} |
| 124 | fdt resize 65536 |
| 125 | for overlay_file in ${overlays}; do |
| 126 | if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then |
| 127 | echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo" |
| 128 | fdt apply ${load_addr} || setenv overlay_error "true" |
| 129 | fi |
| 130 | done |
| 131 | for overlay_file in ${user_overlays}; do |
| 132 | if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then |
| 133 | echo "Applying user provided DT overlay ${overlay_file}.dtbo" |
| 134 | fdt apply ${load_addr} || setenv overlay_error "true" |
| 135 | fi |
| 136 | done |
| 137 | if test "${overlay_error}" = "true"; then |
| 138 | echo "Error applying DT overlays, restoring original DT" |
| 139 | load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile} |
| 140 | else |
| 141 | if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then |
| 142 | echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)" |
| 143 | source ${load_addr} |
| 144 | fi |
| 145 | if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then |
| 146 | load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr |
| 147 | echo "Applying user provided fixup script (fixup.scr)" |
| 148 | source ${load_addr} |
| 149 | fi |
| 150 | fi |
| 151 | booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r} |
| 152 | |
| 153 | # Recompile with: |
| 154 | # mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 155 | ''' |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 156 | bootdir = os.path.join(mnt, 'boot') |
| 157 | mkdir_cond(bootdir) |
| 158 | cmd_fname = os.path.join(bootdir, 'boot.cmd') |
| 159 | scr_fname = os.path.join(bootdir, 'boot.scr') |
| 160 | with open(cmd_fname, 'w', encoding='ascii') as outf: |
| 161 | print(script, file=outf) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 162 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 163 | infname = os.path.join(cons.config.source_dir, |
| 164 | 'test/py/tests/bootstd/armbian.bmp.xz') |
| 165 | bmp_file = os.path.join(bootdir, 'boot.bmp') |
| 166 | u_boot_utils.run_and_log( |
| 167 | cons, |
| 168 | ['sh', '-c', f'xz -dc {infname} >{bmp_file}']) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 169 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 170 | u_boot_utils.run_and_log( |
| 171 | cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}') |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 172 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 173 | kernel = 'vmlinuz-5.15.63-rockchip64' |
| 174 | target = os.path.join(bootdir, kernel) |
| 175 | with open(target, 'wb') as outf: |
| 176 | print('kernel', outf) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 177 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 178 | symlink = os.path.join(bootdir, 'Image') |
| 179 | if os.path.exists(symlink): |
| 180 | os.remove(symlink) |
| 181 | u_boot_utils.run_and_log( |
| 182 | cons, f'echo here {kernel} {symlink}') |
| 183 | os.symlink(kernel, symlink) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 184 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 185 | fsfile = 'ext18M.img' |
| 186 | u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}') |
| 187 | u_boot_utils.run_and_log(cons, f'mkfs.ext4 {fsfile} -d {mnt}') |
| 188 | u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1') |
| 189 | complete = True |
| 190 | u_boot_utils.run_and_log(cons, f'rm -rf {mnt}') |
| 191 | u_boot_utils.run_and_log(cons, f'rm -f {fsfile}') |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 192 | |
| 193 | if not complete: |
| 194 | copy_prepared_image(cons, mmc_dev, fname) |
| 195 | |
| 196 | def setup_bootflow_image(cons): |
| 197 | """Create a 20MB disk image with a single FAT partition""" |
| 198 | mmc_dev = 1 |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 199 | fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 200 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 201 | complete = False |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 202 | vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl' |
| 203 | initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img' |
| 204 | dtbdir = 'dtb-5.3.7-301.fc31.armv7hl' |
| 205 | script = '''# extlinux.conf generated by appliance-creator |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 206 | ui menu.c32 |
| 207 | menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options. |
| 208 | menu title Fedora-Workstation-armhfp-31-1.9 Boot Options. |
| 209 | menu hidden |
| 210 | timeout 20 |
| 211 | totaltimeout 600 |
| 212 | |
| 213 | label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) |
| 214 | kernel /%s |
| 215 | append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB |
| 216 | fdtdir /%s/ |
| 217 | initrd /%s''' % (vmlinux, dtbdir, initrd) |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 218 | ext = os.path.join(mnt, 'extlinux') |
| 219 | mkdir_cond(ext) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 220 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 221 | conf = os.path.join(ext, 'extlinux.conf') |
| 222 | with open(conf, 'w', encoding='ascii') as fd: |
| 223 | print(script, file=fd) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 224 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 225 | inf = os.path.join(cons.config.persistent_data_dir, 'inf') |
| 226 | with open(inf, 'wb') as fd: |
| 227 | fd.write(gzip.compress(b'vmlinux')) |
| 228 | u_boot_utils.run_and_log( |
| 229 | cons, f'mkimage -f auto -d {inf} {os.path.join(mnt, vmlinux)}') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 230 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 231 | with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd: |
| 232 | print('initrd', file=fd) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 233 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 234 | mkdir_cond(os.path.join(mnt, dtbdir)) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 235 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 236 | dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb') |
| 237 | u_boot_utils.run_and_log( |
| 238 | cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};') |
Richard Weinberger | d99fdc0 | 2024-11-21 15:32:10 -0700 | [diff] [blame] | 239 | |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 240 | fsfile = 'vfat18M.img' |
| 241 | u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}') |
| 242 | u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}') |
| 243 | u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/']) |
| 244 | u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1') |
| 245 | complete = True |
| 246 | u_boot_utils.run_and_log(cons, f'rm -rf {mnt}') |
| 247 | u_boot_utils.run_and_log(cons, f'rm -f {fsfile}') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 248 | if not complete: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 249 | copy_prepared_image(cons, mmc_dev, fname) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 250 | |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 251 | def setup_cros_image(cons): |
| 252 | """Create a 20MB disk image with ChromiumOS partitions""" |
| 253 | Partition = collections.namedtuple('part', 'start,size,name') |
| 254 | parts = {} |
| 255 | disk_data = None |
| 256 | |
| 257 | def pack_kernel(cons, arch, kern, dummy): |
| 258 | """Pack a kernel containing some fake data |
| 259 | |
| 260 | Args: |
| 261 | cons (ConsoleBase): Console to use |
| 262 | arch (str): Architecture to use ('x86' or 'arm') |
| 263 | kern (str): Filename containing kernel |
| 264 | dummy (str): Dummy filename to use for config and bootloader |
| 265 | |
| 266 | Return: |
| 267 | bytes: Packed-kernel data |
| 268 | """ |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 269 | kern_part = os.path.join(cons.config.result_dir, |
| 270 | f'kern-part-{arch}.bin') |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 271 | u_boot_utils.run_and_log( |
| 272 | cons, |
| 273 | f'futility vbutil_kernel --pack {kern_part} ' |
| 274 | '--keyblock doc/chromium/files/devkeys/kernel.keyblock ' |
| 275 | '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk ' |
| 276 | f'--version 1 --config {dummy} --bootloader {dummy} ' |
| 277 | f'--vmlinuz {kern}') |
| 278 | |
| 279 | with open(kern_part, 'rb') as inf: |
| 280 | kern_part_data = inf.read() |
| 281 | return kern_part_data |
| 282 | |
| 283 | def set_part_data(partnum, data): |
| 284 | """Set the contents of a disk partition |
| 285 | |
| 286 | This updates disk_data by putting data in the right place |
| 287 | |
| 288 | Args: |
| 289 | partnum (int): Partition number to set |
| 290 | data (bytes): Data for that partition |
| 291 | """ |
| 292 | nonlocal disk_data |
| 293 | |
| 294 | start = parts[partnum].start * sect_size |
| 295 | disk_data = disk_data[:start] + data + disk_data[start + len(data):] |
| 296 | |
| 297 | mmc_dev = 5 |
| 298 | fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 299 | u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 300 | u_boot_utils.run_and_log(cons, f'cgpt create {fname}') |
| 301 | |
| 302 | uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7' |
| 303 | uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309' |
| 304 | uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec' |
| 305 | uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3' |
| 306 | uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e' |
| 307 | uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b' |
| 308 | |
| 309 | ptr = 40 |
| 310 | |
| 311 | # Number of sectors in 1MB |
| 312 | sect_size = 512 |
| 313 | sect_1mb = (1 << 20) // sect_size |
| 314 | |
| 315 | required_parts = [ |
| 316 | {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'}, |
| 317 | {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'}, |
| 318 | {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'}, |
| 319 | {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'}, |
| 320 | {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'}, |
| 321 | |
| 322 | {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'}, |
| 323 | {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'}, |
| 324 | |
| 325 | {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'}, |
| 326 | {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'}, |
| 327 | |
| 328 | {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'}, |
| 329 | {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'}, |
| 330 | {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'}, |
| 331 | ] |
| 332 | |
| 333 | for part in required_parts: |
| 334 | size_str = part['size'] |
| 335 | if 'M' in size_str: |
| 336 | size = int(size_str[:-1]) * sect_1mb |
| 337 | else: |
| 338 | size = int(size_str) |
| 339 | u_boot_utils.run_and_log( |
| 340 | cons, |
| 341 | f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}") |
| 342 | ptr += size |
| 343 | |
| 344 | u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') |
| 345 | out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 346 | |
| 347 | # We expect something like this: |
| 348 | # 8239 2048 1 Basic data |
| 349 | # 45 2048 2 ChromeOS kernel |
| 350 | # 8238 1 3 ChromeOS rootfs |
| 351 | # 2093 2048 4 ChromeOS kernel |
| 352 | # 8237 1 5 ChromeOS rootfs |
| 353 | # 41 1 6 ChromeOS kernel |
| 354 | # 42 1 7 ChromeOS rootfs |
| 355 | # 4141 2048 8 Basic data |
| 356 | # 43 1 9 ChromeOS reserved |
| 357 | # 44 1 10 ChromeOS reserved |
| 358 | # 40 1 11 ChromeOS firmware |
| 359 | # 6189 2048 12 EFI System Partition |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 360 | |
| 361 | # Create a dict (indexed by partition number) containing the above info |
| 362 | for line in out.splitlines(): |
| 363 | start, size, num, name = line.split(maxsplit=3) |
| 364 | parts[int(num)] = Partition(int(start), int(size), name) |
| 365 | |
| 366 | dummy = os.path.join(cons.config.result_dir, 'dummy.txt') |
| 367 | with open(dummy, 'wb') as outf: |
| 368 | outf.write(b'dummy\n') |
| 369 | |
| 370 | # For now we just use dummy kernels. This limits testing to just detecting |
| 371 | # a signed kernel. We could add support for the x86 data structures so that |
| 372 | # testing could cover getting the cmdline, setup.bin and other pieces. |
| 373 | kern = os.path.join(cons.config.result_dir, 'kern.bin') |
| 374 | with open(kern, 'wb') as outf: |
| 375 | outf.write(b'kernel\n') |
| 376 | |
| 377 | with open(fname, 'rb') as inf: |
| 378 | disk_data = inf.read() |
| 379 | |
| 380 | # put x86 kernel in partition 2 and arm one in partition 4 |
| 381 | set_part_data(2, pack_kernel(cons, 'x86', kern, dummy)) |
| 382 | set_part_data(4, pack_kernel(cons, 'arm', kern, dummy)) |
| 383 | |
| 384 | with open(fname, 'wb') as outf: |
| 385 | outf.write(disk_data) |
| 386 | |
| 387 | return fname |
| 388 | |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 389 | def setup_android_image(cons): |
| 390 | """Create a 20MB disk image with Android partitions""" |
| 391 | Partition = collections.namedtuple('part', 'start,size,name') |
| 392 | parts = {} |
| 393 | disk_data = None |
| 394 | |
| 395 | def set_part_data(partnum, data): |
| 396 | """Set the contents of a disk partition |
| 397 | |
| 398 | This updates disk_data by putting data in the right place |
| 399 | |
| 400 | Args: |
| 401 | partnum (int): Partition number to set |
| 402 | data (bytes): Data for that partition |
| 403 | """ |
| 404 | nonlocal disk_data |
| 405 | |
| 406 | start = parts[partnum].start * sect_size |
| 407 | disk_data = disk_data[:start] + data + disk_data[start + len(data):] |
| 408 | |
| 409 | mmc_dev = 7 |
| 410 | fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 411 | u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 412 | u_boot_utils.run_and_log(cons, f'cgpt create {fname}') |
| 413 | |
| 414 | ptr = 40 |
| 415 | |
| 416 | # Number of sectors in 1MB |
| 417 | sect_size = 512 |
| 418 | sect_1mb = (1 << 20) // sect_size |
| 419 | |
| 420 | required_parts = [ |
| 421 | {'num': 1, 'label':'misc', 'size': '1M'}, |
| 422 | {'num': 2, 'label':'boot_a', 'size': '4M'}, |
| 423 | {'num': 3, 'label':'boot_b', 'size': '4M'}, |
| 424 | {'num': 4, 'label':'vendor_boot_a', 'size': '4M'}, |
| 425 | {'num': 5, 'label':'vendor_boot_b', 'size': '4M'}, |
| 426 | ] |
| 427 | |
| 428 | for part in required_parts: |
| 429 | size_str = part['size'] |
| 430 | if 'M' in size_str: |
| 431 | size = int(size_str[:-1]) * sect_1mb |
| 432 | else: |
| 433 | size = int(size_str) |
| 434 | u_boot_utils.run_and_log( |
| 435 | cons, |
| 436 | f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}") |
| 437 | ptr += size |
| 438 | |
| 439 | u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') |
| 440 | out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') |
| 441 | |
| 442 | # Create a dict (indexed by partition number) containing the above info |
| 443 | for line in out.splitlines(): |
| 444 | start, size, num, name = line.split(maxsplit=3) |
| 445 | parts[int(num)] = Partition(int(start), int(size), name) |
| 446 | |
| 447 | with open(fname, 'rb') as inf: |
| 448 | disk_data = inf.read() |
| 449 | |
| 450 | test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex) |
| 451 | boot_img = os.path.join(cons.config.result_dir, 'bootv4.img') |
| 452 | with open(boot_img, 'rb') as inf: |
| 453 | set_part_data(2, inf.read()) |
| 454 | |
| 455 | test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex) |
| 456 | vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img') |
| 457 | with open(vendor_boot_img, 'rb') as inf: |
| 458 | set_part_data(4, inf.read()) |
| 459 | |
| 460 | with open(fname, 'wb') as outf: |
| 461 | outf.write(disk_data) |
| 462 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 463 | print(f'wrote to {fname}') |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 464 | |
| 465 | return fname |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 466 | |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 467 | def setup_cedit_file(cons): |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 468 | """Set up a .dtb file for use with testing expo and configuration editor""" |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 469 | infname = os.path.join(cons.config.source_dir, |
| 470 | 'test/boot/files/expo_layout.dts') |
Simon Glass | b1263bc | 2023-08-14 16:40:28 -0600 | [diff] [blame] | 471 | inhname = os.path.join(cons.config.source_dir, |
| 472 | 'test/boot/files/expo_ids.h') |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 473 | expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py') |
| 474 | outfname = 'cedit.dtb' |
| 475 | u_boot_utils.run_and_log( |
Simon Glass | b1263bc | 2023-08-14 16:40:28 -0600 | [diff] [blame] | 476 | cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}') |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 477 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 478 | @pytest.mark.buildconfigspec('ut_dm') |
| 479 | def test_ut_dm_init(u_boot_console): |
| 480 | """Initialize data for ut dm tests.""" |
| 481 | |
| 482 | fn = u_boot_console.config.source_dir + '/testflash.bin' |
| 483 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 484 | data = b'this is a test' |
| 485 | data += b'\x00' * ((4 * 1024 * 1024) - len(data)) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 486 | with open(fn, 'wb') as fh: |
| 487 | fh.write(data) |
| 488 | |
| 489 | fn = u_boot_console.config.source_dir + '/spi.bin' |
| 490 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 491 | data = b'\x00' * (2 * 1024 * 1024) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 492 | with open(fn, 'wb') as fh: |
| 493 | fh.write(data) |
| 494 | |
Simon Glass | 509f32e | 2022-09-21 16:21:47 +0200 | [diff] [blame] | 495 | # Create a file with a single partition |
| 496 | fn = u_boot_console.config.source_dir + '/scsi.img' |
| 497 | if not os.path.exists(fn): |
| 498 | data = b'\x00' * (2 * 1024 * 1024) |
| 499 | with open(fn, 'wb') as fh: |
| 500 | fh.write(data) |
| 501 | u_boot_utils.run_and_log( |
| 502 | u_boot_console, f'sfdisk {fn}', stdin=b'type=83') |
| 503 | |
Richard Weinberger | 41eca32 | 2024-11-21 15:32:07 -0700 | [diff] [blame] | 504 | fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', None) |
| 505 | fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', None) |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 506 | |
Alexander Gendin | 038cb02 | 2023-10-09 01:24:36 +0000 | [diff] [blame] | 507 | mmc_dev = 6 |
| 508 | fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img') |
| 509 | data = b'\x00' * (12 * 1024 * 1024) |
| 510 | with open(fn, 'wb') as fh: |
| 511 | fh.write(data) |
| 512 | |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 513 | |
| 514 | def setup_efi_image(cons): |
| 515 | """Create a 20MB disk image with an EFI app on it""" |
| 516 | devnum = 1 |
| 517 | basename = 'flash' |
| 518 | fname, mnt = setup_image(cons, devnum, 0xc, second_part=True, |
| 519 | basename=basename) |
| 520 | |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 521 | complete = False |
Simon Glass | d77e8ae | 2024-11-21 15:32:11 -0700 | [diff] [blame^] | 522 | efi_dir = os.path.join(mnt, 'EFI') |
| 523 | mkdir_cond(efi_dir) |
| 524 | bootdir = os.path.join(efi_dir, 'BOOT') |
| 525 | mkdir_cond(bootdir) |
| 526 | efi_src = os.path.join(cons.config.build_dir, |
| 527 | 'lib/efi_loader/testapp.efi') |
| 528 | efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI') |
| 529 | with open(efi_src, 'rb') as inf: |
| 530 | with open(efi_dst, 'wb') as outf: |
| 531 | outf.write(inf.read()) |
| 532 | fsfile = 'vfat18M.img' |
| 533 | u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}') |
| 534 | u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}') |
| 535 | u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/']) |
| 536 | u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1') |
| 537 | complete = True |
| 538 | u_boot_utils.run_and_log(cons, f'rm -rf {mnt}') |
| 539 | u_boot_utils.run_and_log(cons, f'rm -f {fsfile}') |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 540 | |
| 541 | if not complete: |
| 542 | copy_prepared_image(cons, devnum, fname, basename) |
| 543 | |
| 544 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 545 | @pytest.mark.buildconfigspec('cmd_bootflow') |
Simon Glass | 84712cd | 2024-06-23 14:30:27 -0600 | [diff] [blame] | 546 | @pytest.mark.buildconfigspec('sandbox') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 547 | def test_ut_dm_init_bootstd(u_boot_console): |
| 548 | """Initialise data for bootflow tests""" |
| 549 | |
| 550 | setup_bootflow_image(u_boot_console) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 551 | setup_bootmenu_image(u_boot_console) |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 552 | setup_cedit_file(u_boot_console) |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 553 | setup_cros_image(u_boot_console) |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 554 | setup_android_image(u_boot_console) |
Simon Glass | 64c6325 | 2024-11-07 14:31:49 -0700 | [diff] [blame] | 555 | setup_efi_image(u_boot_console) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 556 | |
| 557 | # Restart so that the new mmc1.img is picked up |
| 558 | u_boot_console.restart_uboot() |
| 559 | |
| 560 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 561 | def test_ut(u_boot_console, ut_subtest): |
Heinrich Schuchardt | df6c36c | 2020-05-06 18:26:07 +0200 | [diff] [blame] | 562 | """Execute a "ut" subtest. |
| 563 | |
| 564 | The subtests are collected in function generate_ut_subtest() from linker |
| 565 | generated lists by applying a regular expression to the lines of file |
| 566 | u-boot.sym. The list entries are created using the C macro UNIT_TEST(). |
| 567 | |
| 568 | Strict naming conventions have to be followed to match the regular |
| 569 | expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in |
| 570 | test suite foo that can be executed via command 'ut foo bar' and is |
| 571 | implemented in C function foo_test_bar(). |
| 572 | |
| 573 | Args: |
| 574 | u_boot_console (ConsoleBase): U-Boot console |
| 575 | ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to |
| 576 | execute command 'ut foo bar' |
| 577 | """ |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 578 | |
Francis Laniel | 91ec870 | 2023-12-22 22:02:24 +0100 | [diff] [blame] | 579 | if ut_subtest == 'hush hush_test_simple_dollar': |
| 580 | # ut hush hush_test_simple_dollar prints "Unknown command" on purpose. |
| 581 | with u_boot_console.disable_check('unknown_command'): |
| 582 | output = u_boot_console.run_command('ut ' + ut_subtest) |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 583 | assert 'Unknown command \'quux\' - try \'help\'' in output |
Francis Laniel | 91ec870 | 2023-12-22 22:02:24 +0100 | [diff] [blame] | 584 | else: |
| 585 | output = u_boot_console.run_command('ut ' + ut_subtest) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 586 | assert output.endswith('Failures: 0') |