blob: ea0c43cd4fcc309a7a7495320307360fe1a3839a [file] [log] [blame]
Stephen Warren770fe172016-02-08 14:44:16 -07001# SPDX-License-Identifier: GPL-2.0
Simon Glass08631802024-09-01 16:26:14 -06002"""
3Unit-test runner
Stephen Warren770fe172016-02-08 14:44:16 -07004
Simon Glass08631802024-09-01 16:26:14 -06005Provides a test_ut() function which is used by conftest.py to run each unit
6test 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 Glassfff928c2023-08-24 13:55:41 -060010import collections
Simon Glassd3203bd2022-04-24 23:31:25 -060011import gzip
12import os
Stephen Warren770fe172016-02-08 14:44:16 -070013import os.path
14import pytest
15
Simon Glassfb916372025-02-09 09:07:15 -070016import utils
Tom Rini50ad0192023-12-09 14:52:46 -050017# pylint: disable=E0611
Simon Glass2c7b0e42022-10-29 19:47:19 -060018from tests import fs_helper
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +020019from test_android import test_abootimg
Simon Glassd3203bd2022-04-24 23:31:25 -060020
21def mkdir_cond(dirname):
22 """Create a directory if it doesn't already exist
23
24 Args:
Simon Glassd2bc33ed2023-01-06 08:52:41 -060025 dirname (str): Name of directory to create
Simon Glassd3203bd2022-04-24 23:31:25 -060026 """
27 if not os.path.exists(dirname):
28 os.mkdir(dirname)
29
Simon Glass32701112025-02-09 09:07:17 -070030def setup_image(ubman, devnum, part_type, img_size=20, second_part=False,
Simon Glassd157d3f2024-11-21 15:32:09 -070031 basename='mmc'):
32 """Create a disk image with a single partition
Simon Glassd2bc33ed2023-01-06 08:52:41 -060033
34 Args:
Simon Glass32701112025-02-09 09:07:17 -070035 ubman (ConsoleBase): Console to use
Simon Glass64c63252024-11-07 14:31:49 -070036 devnum (int): Device number to use, e.g. 1
Simon Glassd2bc33ed2023-01-06 08:52:41 -060037 part_type (int): Partition type, e.g. 0xc for FAT32
Simon Glassd157d3f2024-11-21 15:32:09 -070038 img_size (int): Image size in MiB
Simon Glassf5e2df02023-01-17 10:47:41 -070039 second_part (bool): True to contain a small second partition
Simon Glass64c63252024-11-07 14:31:49 -070040 basename (str): Base name to use in the filename, e.g. 'mmc'
Simon Glassd2bc33ed2023-01-06 08:52:41 -060041
42 Returns:
43 tuple:
44 str: Filename of MMC image
Richard Weinbergerd99fdc02024-11-21 15:32:10 -070045 str: Directory name of scratch directory
Simon Glassd2bc33ed2023-01-06 08:52:41 -060046 """
Simon Glass32701112025-02-09 09:07:17 -070047 fname = os.path.join(ubman.config.source_dir, f'{basename}{devnum}.img')
48 mnt = os.path.join(ubman.config.persistent_data_dir, 'scratch')
Simon Glassd3203bd2022-04-24 23:31:25 -060049 mkdir_cond(mnt)
50
Simon Glassd157d3f2024-11-21 15:32:09 -070051 spec = f'type={part_type:x}, size={img_size - 2}M, start=1M, bootable'
Simon Glassf5e2df02023-01-17 10:47:41 -070052 if second_part:
53 spec += '\ntype=c'
54
Simon Glass32701112025-02-09 09:07:17 -070055 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
56 utils.run_and_log(ubman, f'sfdisk {fname}',
Simon Glassf5e2df02023-01-17 10:47:41 -070057 stdin=spec.encode('utf-8'))
Simon Glassd2bc33ed2023-01-06 08:52:41 -060058 return fname, mnt
59
Simon Glass32701112025-02-09 09:07:17 -070060def setup_bootmenu_image(ubman):
Simon Glassd2bc33ed2023-01-06 08:52:41 -060061 """Create a 20MB disk image with a single ext4 partition
62
63 This is modelled on Armbian 22.08 Jammy
64 """
65 mmc_dev = 4
Simon Glass32701112025-02-09 09:07:17 -070066 fname, mnt = setup_image(ubman, mmc_dev, 0x83)
Simon Glassd3203bd2022-04-24 23:31:25 -060067
Simon Glassd77e8ae2024-11-21 15:32:11 -070068 script = '''# DO NOT EDIT THIS FILE
Simon Glassd2bc33ed2023-01-06 08:52:41 -060069#
70# Please edit /boot/armbianEnv.txt to set supported parameters
71#
72
73setenv load_addr "0x9000000"
74setenv overlay_error "false"
75# default values
76setenv rootdev "/dev/mmcblk%dp1"
77setenv verbosity "1"
78setenv console "both"
79setenv bootlogo "false"
80setenv rootfstype "ext4"
81setenv docker_optimizations "on"
82setenv earlycon "off"
83
84echo "Boot script loaded from ${devtype} ${devnum}"
85
86if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then
87 load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt
88 env import -t ${load_addr} ${filesize}
89fi
90
91if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi
92
93if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi
94if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi
95if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi
96if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi
97
98# get PARTUUID of first partition on SD/eMMC the boot script was loaded from
99if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi
100
101setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"
102
103if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
104
105load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
106load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image
107
108load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
109fdt addr ${fdt_addr_r}
110fdt resize 65536
111for overlay_file in ${overlays}; do
112 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then
113 echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo"
114 fdt apply ${load_addr} || setenv overlay_error "true"
115 fi
116done
117for overlay_file in ${user_overlays}; do
118 if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then
119 echo "Applying user provided DT overlay ${overlay_file}.dtbo"
120 fdt apply ${load_addr} || setenv overlay_error "true"
121 fi
122done
123if test "${overlay_error}" = "true"; then
124 echo "Error applying DT overlays, restoring original DT"
125 load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
126else
127 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then
128 echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)"
129 source ${load_addr}
130 fi
131 if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then
132 load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr
133 echo "Applying user provided fixup script (fixup.scr)"
134 source ${load_addr}
135 fi
136fi
137booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
138
139# Recompile with:
140# mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
Simon Glass08631802024-09-01 16:26:14 -0600141'''
Simon Glassd77e8ae2024-11-21 15:32:11 -0700142 bootdir = os.path.join(mnt, 'boot')
143 mkdir_cond(bootdir)
144 cmd_fname = os.path.join(bootdir, 'boot.cmd')
145 scr_fname = os.path.join(bootdir, 'boot.scr')
146 with open(cmd_fname, 'w', encoding='ascii') as outf:
147 print(script, file=outf)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600148
Simon Glass32701112025-02-09 09:07:17 -0700149 infname = os.path.join(ubman.config.source_dir,
Simon Glassd77e8ae2024-11-21 15:32:11 -0700150 'test/py/tests/bootstd/armbian.bmp.xz')
151 bmp_file = os.path.join(bootdir, 'boot.bmp')
Simon Glassfb916372025-02-09 09:07:15 -0700152 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700153 ubman,
Simon Glassd77e8ae2024-11-21 15:32:11 -0700154 ['sh', '-c', f'xz -dc {infname} >{bmp_file}'])
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600155
Simon Glass32701112025-02-09 09:07:17 -0700156 mkimage = ubman.config.build_dir + '/tools/mkimage'
Simon Glassfb916372025-02-09 09:07:15 -0700157 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700158 ubman, f'{mkimage} -C none -A arm -T script -d {cmd_fname} {scr_fname}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600159
Simon Glassd77e8ae2024-11-21 15:32:11 -0700160 kernel = 'vmlinuz-5.15.63-rockchip64'
161 target = os.path.join(bootdir, kernel)
162 with open(target, 'wb') as outf:
163 print('kernel', outf)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600164
Simon Glassd77e8ae2024-11-21 15:32:11 -0700165 symlink = os.path.join(bootdir, 'Image')
166 if os.path.exists(symlink):
167 os.remove(symlink)
Simon Glassfb916372025-02-09 09:07:15 -0700168 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700169 ubman, f'echo here {kernel} {symlink}')
Simon Glassd77e8ae2024-11-21 15:32:11 -0700170 os.symlink(kernel, symlink)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600171
Simon Glassd77e8ae2024-11-21 15:32:11 -0700172 fsfile = 'ext18M.img'
Simon Glass32701112025-02-09 09:07:17 -0700173 utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}')
174 utils.run_and_log(ubman, f'mkfs.ext4 {fsfile} -d {mnt}')
175 utils.run_and_log(ubman, f'dd if={fsfile} of={fname} bs=1M seek=1')
176 utils.run_and_log(ubman, f'rm -rf {mnt}')
177 utils.run_and_log(ubman, f'rm -f {fsfile}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600178
Simon Glass32701112025-02-09 09:07:17 -0700179def setup_bootflow_image(ubman):
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600180 """Create a 20MB disk image with a single FAT partition"""
181 mmc_dev = 1
Simon Glass32701112025-02-09 09:07:17 -0700182 fname, mnt = setup_image(ubman, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600183
Simon Glassd77e8ae2024-11-21 15:32:11 -0700184 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
185 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
186 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
187 script = '''# extlinux.conf generated by appliance-creator
Simon Glassd3203bd2022-04-24 23:31:25 -0600188ui menu.c32
189menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
190menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
191menu hidden
192timeout 20
193totaltimeout 600
194
195label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
196 kernel /%s
197 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
198 fdtdir /%s/
199 initrd /%s''' % (vmlinux, dtbdir, initrd)
Simon Glassd77e8ae2024-11-21 15:32:11 -0700200 ext = os.path.join(mnt, 'extlinux')
201 mkdir_cond(ext)
Simon Glassd3203bd2022-04-24 23:31:25 -0600202
Simon Glassd77e8ae2024-11-21 15:32:11 -0700203 conf = os.path.join(ext, 'extlinux.conf')
204 with open(conf, 'w', encoding='ascii') as fd:
205 print(script, file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600206
Simon Glass32701112025-02-09 09:07:17 -0700207 inf = os.path.join(ubman.config.persistent_data_dir, 'inf')
Simon Glassd77e8ae2024-11-21 15:32:11 -0700208 with open(inf, 'wb') as fd:
209 fd.write(gzip.compress(b'vmlinux'))
Simon Glass32701112025-02-09 09:07:17 -0700210 mkimage = ubman.config.build_dir + '/tools/mkimage'
Simon Glassfb916372025-02-09 09:07:15 -0700211 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700212 ubman, f'{mkimage} -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600213
Simon Glassd77e8ae2024-11-21 15:32:11 -0700214 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
215 print('initrd', file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600216
Simon Glassd77e8ae2024-11-21 15:32:11 -0700217 mkdir_cond(os.path.join(mnt, dtbdir))
Simon Glassd3203bd2022-04-24 23:31:25 -0600218
Simon Glassd77e8ae2024-11-21 15:32:11 -0700219 dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
Simon Glassfb916372025-02-09 09:07:15 -0700220 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700221 ubman, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
Simon Glassd3203bd2022-04-24 23:31:25 -0600222
Simon Glassd77e8ae2024-11-21 15:32:11 -0700223 fsfile = 'vfat18M.img'
Simon Glass32701112025-02-09 09:07:17 -0700224 utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}')
225 utils.run_and_log(ubman, f'mkfs.vfat {fsfile}')
226 utils.run_and_log(ubman, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/'])
227 utils.run_and_log(ubman, f'dd if={fsfile} of={fname} bs=1M seek=1')
228 utils.run_and_log(ubman, f'rm -rf {mnt}')
229 utils.run_and_log(ubman, f'rm -f {fsfile}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600230
Simon Glass32701112025-02-09 09:07:17 -0700231def setup_cros_image(ubman):
Simon Glassfff928c2023-08-24 13:55:41 -0600232 """Create a 20MB disk image with ChromiumOS partitions"""
233 Partition = collections.namedtuple('part', 'start,size,name')
234 parts = {}
235 disk_data = None
236
Simon Glass32701112025-02-09 09:07:17 -0700237 def pack_kernel(ubman, arch, kern, dummy):
Simon Glassfff928c2023-08-24 13:55:41 -0600238 """Pack a kernel containing some fake data
239
240 Args:
Simon Glass32701112025-02-09 09:07:17 -0700241 ubman (ConsoleBase): Console to use
Simon Glassfff928c2023-08-24 13:55:41 -0600242 arch (str): Architecture to use ('x86' or 'arm')
243 kern (str): Filename containing kernel
244 dummy (str): Dummy filename to use for config and bootloader
245
246 Return:
247 bytes: Packed-kernel data
248 """
Simon Glass32701112025-02-09 09:07:17 -0700249 kern_part = os.path.join(ubman.config.result_dir,
Simon Glass08631802024-09-01 16:26:14 -0600250 f'kern-part-{arch}.bin')
Simon Glassfb916372025-02-09 09:07:15 -0700251 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700252 ubman,
Simon Glassfff928c2023-08-24 13:55:41 -0600253 f'futility vbutil_kernel --pack {kern_part} '
254 '--keyblock doc/chromium/files/devkeys/kernel.keyblock '
255 '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk '
256 f'--version 1 --config {dummy} --bootloader {dummy} '
257 f'--vmlinuz {kern}')
258
259 with open(kern_part, 'rb') as inf:
260 kern_part_data = inf.read()
261 return kern_part_data
262
263 def set_part_data(partnum, data):
264 """Set the contents of a disk partition
265
266 This updates disk_data by putting data in the right place
267
268 Args:
269 partnum (int): Partition number to set
270 data (bytes): Data for that partition
271 """
272 nonlocal disk_data
273
274 start = parts[partnum].start * sect_size
275 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
276
277 mmc_dev = 5
Simon Glass32701112025-02-09 09:07:17 -0700278 fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
279 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
280 utils.run_and_log(ubman, f'cgpt create {fname}')
Simon Glassfff928c2023-08-24 13:55:41 -0600281
282 uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
283 uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309'
284 uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
285 uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3'
286 uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e'
287 uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
288
289 ptr = 40
290
291 # Number of sectors in 1MB
292 sect_size = 512
293 sect_1mb = (1 << 20) // sect_size
294
295 required_parts = [
296 {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'},
297 {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'},
298 {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'},
299 {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
300 {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
301
302 {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'},
303 {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'},
304
305 {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'},
306 {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'},
307
308 {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'},
309 {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'},
310 {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'},
311 ]
312
313 for part in required_parts:
314 size_str = part['size']
315 if 'M' in size_str:
316 size = int(size_str[:-1]) * sect_1mb
317 else:
318 size = int(size_str)
Simon Glassfb916372025-02-09 09:07:15 -0700319 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700320 ubman,
Simon Glassfff928c2023-08-24 13:55:41 -0600321 f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}")
322 ptr += size
323
Simon Glass32701112025-02-09 09:07:17 -0700324 utils.run_and_log(ubman, f'cgpt boot -p {fname}')
325 out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
Simon Glass08631802024-09-01 16:26:14 -0600326
327 # We expect something like this:
328 # 8239 2048 1 Basic data
329 # 45 2048 2 ChromeOS kernel
330 # 8238 1 3 ChromeOS rootfs
331 # 2093 2048 4 ChromeOS kernel
332 # 8237 1 5 ChromeOS rootfs
333 # 41 1 6 ChromeOS kernel
334 # 42 1 7 ChromeOS rootfs
335 # 4141 2048 8 Basic data
336 # 43 1 9 ChromeOS reserved
337 # 44 1 10 ChromeOS reserved
338 # 40 1 11 ChromeOS firmware
339 # 6189 2048 12 EFI System Partition
Simon Glassfff928c2023-08-24 13:55:41 -0600340
341 # Create a dict (indexed by partition number) containing the above info
342 for line in out.splitlines():
343 start, size, num, name = line.split(maxsplit=3)
344 parts[int(num)] = Partition(int(start), int(size), name)
345
Simon Glassc6a52e72024-11-15 16:19:23 -0700346 # Set up the kernel command-line
Simon Glass32701112025-02-09 09:07:17 -0700347 dummy = os.path.join(ubman.config.result_dir, 'dummy.txt')
Simon Glassfff928c2023-08-24 13:55:41 -0600348 with open(dummy, 'wb') as outf:
Simon Glassc6a52e72024-11-15 16:19:23 -0700349 outf.write(b'BOOT_IMAGE=/vmlinuz-5.15.0-121-generic root=/dev/nvme0n1p1 ro quiet splash vt.handoff=7')
Simon Glassfff928c2023-08-24 13:55:41 -0600350
351 # For now we just use dummy kernels. This limits testing to just detecting
352 # a signed kernel. We could add support for the x86 data structures so that
353 # testing could cover getting the cmdline, setup.bin and other pieces.
Simon Glass32701112025-02-09 09:07:17 -0700354 kern = os.path.join(ubman.config.result_dir, 'kern.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600355 with open(kern, 'wb') as outf:
356 outf.write(b'kernel\n')
357
358 with open(fname, 'rb') as inf:
359 disk_data = inf.read()
360
361 # put x86 kernel in partition 2 and arm one in partition 4
Simon Glass32701112025-02-09 09:07:17 -0700362 set_part_data(2, pack_kernel(ubman, 'x86', kern, dummy))
363 set_part_data(4, pack_kernel(ubman, 'arm', kern, dummy))
Simon Glassfff928c2023-08-24 13:55:41 -0600364
365 with open(fname, 'wb') as outf:
366 outf.write(disk_data)
367
368 return fname
369
Simon Glass32701112025-02-09 09:07:17 -0700370def setup_android_image(ubman):
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200371 """Create a 20MB disk image with Android partitions"""
372 Partition = collections.namedtuple('part', 'start,size,name')
373 parts = {}
374 disk_data = None
375
376 def set_part_data(partnum, data):
377 """Set the contents of a disk partition
378
379 This updates disk_data by putting data in the right place
380
381 Args:
382 partnum (int): Partition number to set
383 data (bytes): Data for that partition
384 """
385 nonlocal disk_data
386
387 start = parts[partnum].start * sect_size
388 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
389
390 mmc_dev = 7
Simon Glass32701112025-02-09 09:07:17 -0700391 fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
392 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
393 utils.run_and_log(ubman, f'cgpt create {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200394
395 ptr = 40
396
397 # Number of sectors in 1MB
398 sect_size = 512
399 sect_1mb = (1 << 20) // sect_size
400
401 required_parts = [
402 {'num': 1, 'label':'misc', 'size': '1M'},
403 {'num': 2, 'label':'boot_a', 'size': '4M'},
404 {'num': 3, 'label':'boot_b', 'size': '4M'},
405 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
406 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
407 ]
408
409 for part in required_parts:
410 size_str = part['size']
411 if 'M' in size_str:
412 size = int(size_str[:-1]) * sect_1mb
413 else:
414 size = int(size_str)
Simon Glassfb916372025-02-09 09:07:15 -0700415 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700416 ubman,
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200417 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
418 ptr += size
419
Simon Glass32701112025-02-09 09:07:17 -0700420 utils.run_and_log(ubman, f'cgpt boot -p {fname}')
421 out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200422
423 # Create a dict (indexed by partition number) containing the above info
424 for line in out.splitlines():
425 start, size, num, name = line.split(maxsplit=3)
426 parts[int(num)] = Partition(int(start), int(size), name)
427
428 with open(fname, 'rb') as inf:
429 disk_data = inf.read()
430
Simon Glass32701112025-02-09 09:07:17 -0700431 test_abootimg.AbootimgTestDiskImage(ubman, 'bootv4.img', test_abootimg.boot_img_hex)
432 boot_img = os.path.join(ubman.config.result_dir, 'bootv4.img')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200433 with open(boot_img, 'rb') as inf:
434 set_part_data(2, inf.read())
435
Simon Glass32701112025-02-09 09:07:17 -0700436 test_abootimg.AbootimgTestDiskImage(ubman, 'vendor_boot.img', test_abootimg.vboot_img_hex)
437 vendor_boot_img = os.path.join(ubman.config.result_dir, 'vendor_boot.img')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200438 with open(vendor_boot_img, 'rb') as inf:
439 set_part_data(4, inf.read())
440
441 with open(fname, 'wb') as outf:
442 outf.write(disk_data)
443
Simon Glass08631802024-09-01 16:26:14 -0600444 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200445
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100446 mmc_dev = 8
Simon Glass32701112025-02-09 09:07:17 -0700447 fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
448 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
449 utils.run_and_log(ubman, f'cgpt create {fname}')
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100450
451 ptr = 40
452
453 # Number of sectors in 1MB
454 sect_size = 512
455 sect_1mb = (1 << 20) // sect_size
456
457 required_parts = [
458 {'num': 1, 'label':'misc', 'size': '1M'},
459 {'num': 2, 'label':'boot_a', 'size': '4M'},
460 {'num': 3, 'label':'boot_b', 'size': '4M'},
461 ]
462
463 for part in required_parts:
464 size_str = part['size']
465 if 'M' in size_str:
466 size = int(size_str[:-1]) * sect_1mb
467 else:
468 size = int(size_str)
Simon Glassfb916372025-02-09 09:07:15 -0700469 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700470 ubman,
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100471 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
472 ptr += size
473
Simon Glass32701112025-02-09 09:07:17 -0700474 utils.run_and_log(ubman, f'cgpt boot -p {fname}')
475 out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100476
477 # Create a dict (indexed by partition number) containing the above info
478 for line in out.splitlines():
479 start, size, num, name = line.split(maxsplit=3)
480 parts[int(num)] = Partition(int(start), int(size), name)
481
482 with open(fname, 'rb') as inf:
483 disk_data = inf.read()
484
Simon Glass32701112025-02-09 09:07:17 -0700485 test_abootimg.AbootimgTestDiskImage(ubman, 'boot.img', test_abootimg.img_hex)
486 boot_img = os.path.join(ubman.config.result_dir, 'boot.img')
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100487 with open(boot_img, 'rb') as inf:
488 set_part_data(2, inf.read())
489
490 with open(fname, 'wb') as outf:
491 outf.write(disk_data)
492
493 print(f'wrote to {fname}')
494
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200495 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600496
Simon Glass32701112025-02-09 09:07:17 -0700497def setup_cedit_file(ubman):
Simon Glass08631802024-09-01 16:26:14 -0600498 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glass32701112025-02-09 09:07:17 -0700499 infname = os.path.join(ubman.config.source_dir,
Simon Glassb8c26552023-06-01 10:23:03 -0600500 'test/boot/files/expo_layout.dts')
Simon Glass32701112025-02-09 09:07:17 -0700501 inhname = os.path.join(ubman.config.source_dir,
Simon Glassb1263bc2023-08-14 16:40:28 -0600502 'test/boot/files/expo_ids.h')
Simon Glass32701112025-02-09 09:07:17 -0700503 expo_tool = os.path.join(ubman.config.source_dir, 'tools/expo.py')
Simon Glassb8c26552023-06-01 10:23:03 -0600504 outfname = 'cedit.dtb'
Simon Glassfb916372025-02-09 09:07:15 -0700505 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700506 ubman, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600507
Stephen Warren770fe172016-02-08 14:44:16 -0700508@pytest.mark.buildconfigspec('ut_dm')
Simon Glassddba5202025-02-09 09:07:14 -0700509def test_ut_dm_init(ubman):
Stephen Warren770fe172016-02-08 14:44:16 -0700510 """Initialize data for ut dm tests."""
511
Simon Glassddba5202025-02-09 09:07:14 -0700512 fn = ubman.config.source_dir + '/testflash.bin'
Stephen Warren770fe172016-02-08 14:44:16 -0700513 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400514 data = b'this is a test'
515 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700516 with open(fn, 'wb') as fh:
517 fh.write(data)
518
Simon Glassddba5202025-02-09 09:07:14 -0700519 fn = ubman.config.source_dir + '/spi.bin'
Stephen Warren770fe172016-02-08 14:44:16 -0700520 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400521 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700522 with open(fn, 'wb') as fh:
523 fh.write(data)
524
Simon Glass509f32e2022-09-21 16:21:47 +0200525 # Create a file with a single partition
Simon Glassddba5202025-02-09 09:07:14 -0700526 fn = ubman.config.source_dir + '/scsi.img'
Simon Glass509f32e2022-09-21 16:21:47 +0200527 if not os.path.exists(fn):
528 data = b'\x00' * (2 * 1024 * 1024)
529 with open(fn, 'wb') as fh:
530 fh.write(data)
Simon Glassfb916372025-02-09 09:07:15 -0700531 utils.run_and_log(
Simon Glassddba5202025-02-09 09:07:14 -0700532 ubman, f'sfdisk {fn}', stdin=b'type=83')
Simon Glass509f32e2022-09-21 16:21:47 +0200533
Simon Glassddba5202025-02-09 09:07:14 -0700534 fs_helper.mk_fs(ubman.config, 'ext2', 0x200000, '2MB', None)
535 fs_helper.mk_fs(ubman.config, 'fat32', 0x100000, '1MB', None)
Simon Glass2c7b0e42022-10-29 19:47:19 -0600536
Alexander Gendin038cb022023-10-09 01:24:36 +0000537 mmc_dev = 6
Simon Glassddba5202025-02-09 09:07:14 -0700538 fn = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
Alexander Gendin038cb022023-10-09 01:24:36 +0000539 data = b'\x00' * (12 * 1024 * 1024)
540 with open(fn, 'wb') as fh:
541 fh.write(data)
542
Simon Glass64c63252024-11-07 14:31:49 -0700543
Simon Glass32701112025-02-09 09:07:17 -0700544def setup_efi_image(ubman):
Simon Glass64c63252024-11-07 14:31:49 -0700545 """Create a 20MB disk image with an EFI app on it"""
546 devnum = 1
547 basename = 'flash'
Simon Glass32701112025-02-09 09:07:17 -0700548 fname, mnt = setup_image(ubman, devnum, 0xc, second_part=True,
Simon Glass64c63252024-11-07 14:31:49 -0700549 basename=basename)
550
Simon Glassd77e8ae2024-11-21 15:32:11 -0700551 efi_dir = os.path.join(mnt, 'EFI')
552 mkdir_cond(efi_dir)
553 bootdir = os.path.join(efi_dir, 'BOOT')
554 mkdir_cond(bootdir)
Simon Glass32701112025-02-09 09:07:17 -0700555 efi_src = os.path.join(ubman.config.build_dir,
Simon Glassd77e8ae2024-11-21 15:32:11 -0700556 'lib/efi_loader/testapp.efi')
557 efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
558 with open(efi_src, 'rb') as inf:
559 with open(efi_dst, 'wb') as outf:
560 outf.write(inf.read())
561 fsfile = 'vfat18M.img'
Simon Glass32701112025-02-09 09:07:17 -0700562 utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}')
563 utils.run_and_log(ubman, f'mkfs.vfat {fsfile}')
564 utils.run_and_log(ubman, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/'])
565 utils.run_and_log(ubman, f'dd if={fsfile} of={fname} bs=1M seek=1')
566 utils.run_and_log(ubman, f'rm -rf {mnt}')
567 utils.run_and_log(ubman, f'rm -f {fsfile}')
Simon Glass64c63252024-11-07 14:31:49 -0700568
Simon Glassd3203bd2022-04-24 23:31:25 -0600569@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600570@pytest.mark.buildconfigspec('sandbox')
Simon Glassddba5202025-02-09 09:07:14 -0700571def test_ut_dm_init_bootstd(ubman):
Simon Glassd3203bd2022-04-24 23:31:25 -0600572 """Initialise data for bootflow tests"""
573
Simon Glassddba5202025-02-09 09:07:14 -0700574 setup_bootflow_image(ubman)
575 setup_bootmenu_image(ubman)
576 setup_cedit_file(ubman)
577 setup_cros_image(ubman)
578 setup_android_image(ubman)
579 setup_efi_image(ubman)
Simon Glassd3203bd2022-04-24 23:31:25 -0600580
581 # Restart so that the new mmc1.img is picked up
Simon Glassddba5202025-02-09 09:07:14 -0700582 ubman.restart_uboot()
Simon Glassd3203bd2022-04-24 23:31:25 -0600583
584
Simon Glassddba5202025-02-09 09:07:14 -0700585def test_ut(ubman, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200586 """Execute a "ut" subtest.
587
588 The subtests are collected in function generate_ut_subtest() from linker
589 generated lists by applying a regular expression to the lines of file
590 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
591
592 Strict naming conventions have to be followed to match the regular
593 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
594 test suite foo that can be executed via command 'ut foo bar' and is
595 implemented in C function foo_test_bar().
596
597 Args:
Simon Glassddba5202025-02-09 09:07:14 -0700598 ubman (ConsoleBase): U-Boot console
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200599 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
600 execute command 'ut foo bar'
601 """
Stephen Warren770fe172016-02-08 14:44:16 -0700602
Francis Laniel91ec8702023-12-22 22:02:24 +0100603 if ut_subtest == 'hush hush_test_simple_dollar':
604 # ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
Simon Glassddba5202025-02-09 09:07:14 -0700605 with ubman.disable_check('unknown_command'):
606 output = ubman.run_command('ut ' + ut_subtest)
Simon Glass08631802024-09-01 16:26:14 -0600607 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100608 else:
Simon Glassddba5202025-02-09 09:07:14 -0700609 output = ubman.run_command('ut ' + ut_subtest)
Simon Glass1f271dc2025-01-20 14:26:01 -0700610 assert output.endswith('failures: 0')