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