blob: d449d0baf5e13af337e895f7bf8fb4273bfc0681 [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 Glassd3203bd2022-04-24 23:31:25 -060016import u_boot_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 Glassd157d3f2024-11-21 15:32:09 -070030def 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 Glassd2bc33ed2023-01-06 08:52:41 -060033
34 Args:
35 cons (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 Glass64c63252024-11-07 14:31:49 -070047 fname = os.path.join(cons.config.source_dir, f'{basename}{devnum}.img')
Richard Weinbergerd99fdc02024-11-21 15:32:10 -070048 mnt = os.path.join(cons.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
Richard Weinbergerd99fdc02024-11-21 15:32:10 -070055 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 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 Glass64c63252024-11-07 14:31:49 -070060def copy_prepared_image(cons, devnum, fname, basename='mmc'):
Simon Glassd2bc33ed2023-01-06 08:52:41 -060061 """Use a prepared image since we cannot create one
62
63 Args:
64 cons (ConsoleBase): Console touse
Simon Glass64c63252024-11-07 14:31:49 -070065 devnum (int): device number
Simon Glassd2bc33ed2023-01-06 08:52:41 -060066 fname (str): Filename of MMC image
Simon Glass64c63252024-11-07 14:31:49 -070067 basename (str): Base name to use in the filename, e.g. 'mmc'
Simon Glassd2bc33ed2023-01-06 08:52:41 -060068 """
69 infname = os.path.join(cons.config.source_dir,
Simon Glass64c63252024-11-07 14:31:49 -070070 f'test/py/tests/bootstd/{basename}{devnum}.img.xz')
Simon Glass08631802024-09-01 16:26:14 -060071 u_boot_utils.run_and_log(cons, ['sh', '-c', f'xz -dc {infname} >{fname}'])
Simon Glassd2bc33ed2023-01-06 08:52:41 -060072
73def 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 Glassd3203bd2022-04-24 23:31:25 -060080
Simon Glassd3203bd2022-04-24 23:31:25 -060081 complete = False
82 try:
Simon Glassd2bc33ed2023-01-06 08:52:41 -060083 script = '''# DO NOT EDIT THIS FILE
84#
85# Please edit /boot/armbianEnv.txt to set supported parameters
86#
87
88setenv load_addr "0x9000000"
89setenv overlay_error "false"
90# default values
91setenv rootdev "/dev/mmcblk%dp1"
92setenv verbosity "1"
93setenv console "both"
94setenv bootlogo "false"
95setenv rootfstype "ext4"
96setenv docker_optimizations "on"
97setenv earlycon "off"
98
99echo "Boot script loaded from ${devtype} ${devnum}"
100
101if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then
102 load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt
103 env import -t ${load_addr} ${filesize}
104fi
105
106if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi
107
108if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi
109if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi
110if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi
111if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi
112
113# get PARTUUID of first partition on SD/eMMC the boot script was loaded from
114if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi
115
116setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"
117
118if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
119
120load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
121load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image
122
123load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
124fdt addr ${fdt_addr_r}
125fdt resize 65536
126for overlay_file in ${overlays}; do
127 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then
128 echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo"
129 fdt apply ${load_addr} || setenv overlay_error "true"
130 fi
131done
132for overlay_file in ${user_overlays}; do
133 if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then
134 echo "Applying user provided DT overlay ${overlay_file}.dtbo"
135 fdt apply ${load_addr} || setenv overlay_error "true"
136 fi
137done
138if test "${overlay_error}" = "true"; then
139 echo "Error applying DT overlays, restoring original DT"
140 load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
141else
142 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then
143 echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)"
144 source ${load_addr}
145 fi
146 if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then
147 load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr
148 echo "Applying user provided fixup script (fixup.scr)"
149 source ${load_addr}
150 fi
151fi
152booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
153
154# Recompile with:
155# mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
Simon Glass08631802024-09-01 16:26:14 -0600156'''
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600157 bootdir = os.path.join(mnt, 'boot')
158 mkdir_cond(bootdir)
159 cmd_fname = os.path.join(bootdir, 'boot.cmd')
160 scr_fname = os.path.join(bootdir, 'boot.scr')
Simon Glass08631802024-09-01 16:26:14 -0600161 with open(cmd_fname, 'w', encoding='ascii') as outf:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600162 print(script, file=outf)
163
164 infname = os.path.join(cons.config.source_dir,
165 'test/py/tests/bootstd/armbian.bmp.xz')
166 bmp_file = os.path.join(bootdir, 'boot.bmp')
167 u_boot_utils.run_and_log(
168 cons,
169 ['sh', '-c', f'xz -dc {infname} >{bmp_file}'])
170
171 u_boot_utils.run_and_log(
172 cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}')
173
174 kernel = 'vmlinuz-5.15.63-rockchip64'
175 target = os.path.join(bootdir, kernel)
176 with open(target, 'wb') as outf:
177 print('kernel', outf)
178
179 symlink = os.path.join(bootdir, 'Image')
180 if os.path.exists(symlink):
181 os.remove(symlink)
182 u_boot_utils.run_and_log(
183 cons, f'echo here {kernel} {symlink}')
184 os.symlink(kernel, symlink)
185
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700186 fsfile = 'ext18M.img'
187 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
188 u_boot_utils.run_and_log(cons, f'mkfs.ext4 {fsfile} -d {mnt}')
189 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600190 complete = True
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600191 except ValueError as exc:
Simon Glass08631802024-09-01 16:26:14 -0600192 print(f'Falled to create image, failing back to prepared copy: {exc}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600193 finally:
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700194 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
195 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600196
197 if not complete:
198 copy_prepared_image(cons, mmc_dev, fname)
199
200def setup_bootflow_image(cons):
201 """Create a 20MB disk image with a single FAT partition"""
202 mmc_dev = 1
Simon Glassf5e2df02023-01-17 10:47:41 -0700203 fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600204
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600205 complete = False
206 try:
Simon Glassd3203bd2022-04-24 23:31:25 -0600207 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
208 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
209 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
210 script = '''# extlinux.conf generated by appliance-creator
211ui menu.c32
212menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
213menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
214menu hidden
215timeout 20
216totaltimeout 600
217
218label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
219 kernel /%s
220 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
221 fdtdir /%s/
222 initrd /%s''' % (vmlinux, dtbdir, initrd)
223 ext = os.path.join(mnt, 'extlinux')
224 mkdir_cond(ext)
225
Simon Glass08631802024-09-01 16:26:14 -0600226 conf = os.path.join(ext, 'extlinux.conf')
227 with open(conf, 'w', encoding='ascii') as fd:
Simon Glassd3203bd2022-04-24 23:31:25 -0600228 print(script, file=fd)
229
230 inf = os.path.join(cons.config.persistent_data_dir, 'inf')
231 with open(inf, 'wb') as fd:
232 fd.write(gzip.compress(b'vmlinux'))
Simon Glass08631802024-09-01 16:26:14 -0600233 u_boot_utils.run_and_log(
234 cons, f'mkimage -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600235
Simon Glass08631802024-09-01 16:26:14 -0600236 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
Simon Glassd3203bd2022-04-24 23:31:25 -0600237 print('initrd', file=fd)
238
239 mkdir_cond(os.path.join(mnt, dtbdir))
240
Simon Glass08631802024-09-01 16:26:14 -0600241 dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
Simon Glassd3203bd2022-04-24 23:31:25 -0600242 u_boot_utils.run_and_log(
Simon Glass08631802024-09-01 16:26:14 -0600243 cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700244
245 fsfile = 'vfat18M.img'
246 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
247 u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
248 u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/'])
249 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
Simon Glassd3203bd2022-04-24 23:31:25 -0600250 complete = True
251 except ValueError as exc:
Simon Glass08631802024-09-01 16:26:14 -0600252 print(f'Falled to create image, failing back to prepared copy: {exc}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600253 finally:
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700254 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
255 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600256 if not complete:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600257 copy_prepared_image(cons, mmc_dev, fname)
Simon Glassd3203bd2022-04-24 23:31:25 -0600258
Simon Glassfff928c2023-08-24 13:55:41 -0600259def setup_cros_image(cons):
260 """Create a 20MB disk image with ChromiumOS partitions"""
261 Partition = collections.namedtuple('part', 'start,size,name')
262 parts = {}
263 disk_data = None
264
265 def pack_kernel(cons, arch, kern, dummy):
266 """Pack a kernel containing some fake data
267
268 Args:
269 cons (ConsoleBase): Console to use
270 arch (str): Architecture to use ('x86' or 'arm')
271 kern (str): Filename containing kernel
272 dummy (str): Dummy filename to use for config and bootloader
273
274 Return:
275 bytes: Packed-kernel data
276 """
Simon Glass08631802024-09-01 16:26:14 -0600277 kern_part = os.path.join(cons.config.result_dir,
278 f'kern-part-{arch}.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600279 u_boot_utils.run_and_log(
280 cons,
281 f'futility vbutil_kernel --pack {kern_part} '
282 '--keyblock doc/chromium/files/devkeys/kernel.keyblock '
283 '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk '
284 f'--version 1 --config {dummy} --bootloader {dummy} '
285 f'--vmlinuz {kern}')
286
287 with open(kern_part, 'rb') as inf:
288 kern_part_data = inf.read()
289 return kern_part_data
290
291 def set_part_data(partnum, data):
292 """Set the contents of a disk partition
293
294 This updates disk_data by putting data in the right place
295
296 Args:
297 partnum (int): Partition number to set
298 data (bytes): Data for that partition
299 """
300 nonlocal disk_data
301
302 start = parts[partnum].start * sect_size
303 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
304
305 mmc_dev = 5
306 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600307 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Simon Glassfff928c2023-08-24 13:55:41 -0600308 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
309
310 uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
311 uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309'
312 uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
313 uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3'
314 uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e'
315 uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
316
317 ptr = 40
318
319 # Number of sectors in 1MB
320 sect_size = 512
321 sect_1mb = (1 << 20) // sect_size
322
323 required_parts = [
324 {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'},
325 {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'},
326 {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'},
327 {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
328 {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
329
330 {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'},
331 {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'},
332
333 {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'},
334 {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'},
335
336 {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'},
337 {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'},
338 {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'},
339 ]
340
341 for part in required_parts:
342 size_str = part['size']
343 if 'M' in size_str:
344 size = int(size_str[:-1]) * sect_1mb
345 else:
346 size = int(size_str)
347 u_boot_utils.run_and_log(
348 cons,
349 f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}")
350 ptr += size
351
352 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
353 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
Simon Glass08631802024-09-01 16:26:14 -0600354
355 # We expect something like this:
356 # 8239 2048 1 Basic data
357 # 45 2048 2 ChromeOS kernel
358 # 8238 1 3 ChromeOS rootfs
359 # 2093 2048 4 ChromeOS kernel
360 # 8237 1 5 ChromeOS rootfs
361 # 41 1 6 ChromeOS kernel
362 # 42 1 7 ChromeOS rootfs
363 # 4141 2048 8 Basic data
364 # 43 1 9 ChromeOS reserved
365 # 44 1 10 ChromeOS reserved
366 # 40 1 11 ChromeOS firmware
367 # 6189 2048 12 EFI System Partition
Simon Glassfff928c2023-08-24 13:55:41 -0600368
369 # Create a dict (indexed by partition number) containing the above info
370 for line in out.splitlines():
371 start, size, num, name = line.split(maxsplit=3)
372 parts[int(num)] = Partition(int(start), int(size), name)
373
374 dummy = os.path.join(cons.config.result_dir, 'dummy.txt')
375 with open(dummy, 'wb') as outf:
376 outf.write(b'dummy\n')
377
378 # For now we just use dummy kernels. This limits testing to just detecting
379 # a signed kernel. We could add support for the x86 data structures so that
380 # testing could cover getting the cmdline, setup.bin and other pieces.
381 kern = os.path.join(cons.config.result_dir, 'kern.bin')
382 with open(kern, 'wb') as outf:
383 outf.write(b'kernel\n')
384
385 with open(fname, 'rb') as inf:
386 disk_data = inf.read()
387
388 # put x86 kernel in partition 2 and arm one in partition 4
389 set_part_data(2, pack_kernel(cons, 'x86', kern, dummy))
390 set_part_data(4, pack_kernel(cons, 'arm', kern, dummy))
391
392 with open(fname, 'wb') as outf:
393 outf.write(disk_data)
394
395 return fname
396
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200397def setup_android_image(cons):
398 """Create a 20MB disk image with Android partitions"""
399 Partition = collections.namedtuple('part', 'start,size,name')
400 parts = {}
401 disk_data = None
402
403 def set_part_data(partnum, data):
404 """Set the contents of a disk partition
405
406 This updates disk_data by putting data in the right place
407
408 Args:
409 partnum (int): Partition number to set
410 data (bytes): Data for that partition
411 """
412 nonlocal disk_data
413
414 start = parts[partnum].start * sect_size
415 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
416
417 mmc_dev = 7
418 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600419 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200420 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
421
422 ptr = 40
423
424 # Number of sectors in 1MB
425 sect_size = 512
426 sect_1mb = (1 << 20) // sect_size
427
428 required_parts = [
429 {'num': 1, 'label':'misc', 'size': '1M'},
430 {'num': 2, 'label':'boot_a', 'size': '4M'},
431 {'num': 3, 'label':'boot_b', 'size': '4M'},
432 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
433 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
434 ]
435
436 for part in required_parts:
437 size_str = part['size']
438 if 'M' in size_str:
439 size = int(size_str[:-1]) * sect_1mb
440 else:
441 size = int(size_str)
442 u_boot_utils.run_and_log(
443 cons,
444 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
445 ptr += size
446
447 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
448 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
449
450 # Create a dict (indexed by partition number) containing the above info
451 for line in out.splitlines():
452 start, size, num, name = line.split(maxsplit=3)
453 parts[int(num)] = Partition(int(start), int(size), name)
454
455 with open(fname, 'rb') as inf:
456 disk_data = inf.read()
457
458 test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex)
459 boot_img = os.path.join(cons.config.result_dir, 'bootv4.img')
460 with open(boot_img, 'rb') as inf:
461 set_part_data(2, inf.read())
462
463 test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex)
464 vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img')
465 with open(vendor_boot_img, 'rb') as inf:
466 set_part_data(4, inf.read())
467
468 with open(fname, 'wb') as outf:
469 outf.write(disk_data)
470
Simon Glass08631802024-09-01 16:26:14 -0600471 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200472
473 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600474
Simon Glassb8c26552023-06-01 10:23:03 -0600475def setup_cedit_file(cons):
Simon Glass08631802024-09-01 16:26:14 -0600476 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glassb8c26552023-06-01 10:23:03 -0600477 infname = os.path.join(cons.config.source_dir,
478 'test/boot/files/expo_layout.dts')
Simon Glassb1263bc2023-08-14 16:40:28 -0600479 inhname = os.path.join(cons.config.source_dir,
480 'test/boot/files/expo_ids.h')
Simon Glassb8c26552023-06-01 10:23:03 -0600481 expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py')
482 outfname = 'cedit.dtb'
483 u_boot_utils.run_and_log(
Simon Glassb1263bc2023-08-14 16:40:28 -0600484 cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600485
Stephen Warren770fe172016-02-08 14:44:16 -0700486@pytest.mark.buildconfigspec('ut_dm')
487def test_ut_dm_init(u_boot_console):
488 """Initialize data for ut dm tests."""
489
490 fn = u_boot_console.config.source_dir + '/testflash.bin'
491 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400492 data = b'this is a test'
493 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700494 with open(fn, 'wb') as fh:
495 fh.write(data)
496
497 fn = u_boot_console.config.source_dir + '/spi.bin'
498 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400499 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700500 with open(fn, 'wb') as fh:
501 fh.write(data)
502
Simon Glass509f32e2022-09-21 16:21:47 +0200503 # Create a file with a single partition
504 fn = u_boot_console.config.source_dir + '/scsi.img'
505 if not os.path.exists(fn):
506 data = b'\x00' * (2 * 1024 * 1024)
507 with open(fn, 'wb') as fh:
508 fh.write(data)
509 u_boot_utils.run_and_log(
510 u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
511
Richard Weinberger41eca322024-11-21 15:32:07 -0700512 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', None)
513 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', None)
Simon Glass2c7b0e42022-10-29 19:47:19 -0600514
Alexander Gendin038cb022023-10-09 01:24:36 +0000515 mmc_dev = 6
516 fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img')
517 data = b'\x00' * (12 * 1024 * 1024)
518 with open(fn, 'wb') as fh:
519 fh.write(data)
520
Simon Glass64c63252024-11-07 14:31:49 -0700521
522def setup_efi_image(cons):
523 """Create a 20MB disk image with an EFI app on it"""
524 devnum = 1
525 basename = 'flash'
526 fname, mnt = setup_image(cons, devnum, 0xc, second_part=True,
527 basename=basename)
528
Simon Glass64c63252024-11-07 14:31:49 -0700529 complete = False
530 try:
Simon Glass64c63252024-11-07 14:31:49 -0700531 efi_dir = os.path.join(mnt, 'EFI')
532 mkdir_cond(efi_dir)
533 bootdir = os.path.join(efi_dir, 'BOOT')
534 mkdir_cond(bootdir)
535 efi_src = os.path.join(cons.config.build_dir,
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700536 'lib/efi_loader/testapp.efi')
Simon Glass64c63252024-11-07 14:31:49 -0700537 efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
538 with open(efi_src, 'rb') as inf:
539 with open(efi_dst, 'wb') as outf:
540 outf.write(inf.read())
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700541 fsfile = 'vfat18M.img'
542 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
543 u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
544 u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/'])
545 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
Simon Glass64c63252024-11-07 14:31:49 -0700546 complete = True
547 except ValueError as exc:
548 print(f'Falled to create image, failing back to prepared copy: {exc}')
549
550 finally:
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700551 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
552 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glass64c63252024-11-07 14:31:49 -0700553
554 if not complete:
555 copy_prepared_image(cons, devnum, fname, basename)
556
557
Simon Glassd3203bd2022-04-24 23:31:25 -0600558@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600559@pytest.mark.buildconfigspec('sandbox')
Simon Glassd3203bd2022-04-24 23:31:25 -0600560def test_ut_dm_init_bootstd(u_boot_console):
561 """Initialise data for bootflow tests"""
562
563 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600564 setup_bootmenu_image(u_boot_console)
Simon Glassb8c26552023-06-01 10:23:03 -0600565 setup_cedit_file(u_boot_console)
Simon Glassfff928c2023-08-24 13:55:41 -0600566 setup_cros_image(u_boot_console)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200567 setup_android_image(u_boot_console)
Simon Glass64c63252024-11-07 14:31:49 -0700568 setup_efi_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600569
570 # Restart so that the new mmc1.img is picked up
571 u_boot_console.restart_uboot()
572
573
Stephen Warren770fe172016-02-08 14:44:16 -0700574def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200575 """Execute a "ut" subtest.
576
577 The subtests are collected in function generate_ut_subtest() from linker
578 generated lists by applying a regular expression to the lines of file
579 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
580
581 Strict naming conventions have to be followed to match the regular
582 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
583 test suite foo that can be executed via command 'ut foo bar' and is
584 implemented in C function foo_test_bar().
585
586 Args:
587 u_boot_console (ConsoleBase): U-Boot console
588 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
589 execute command 'ut foo bar'
590 """
Stephen Warren770fe172016-02-08 14:44:16 -0700591
Francis Laniel91ec8702023-12-22 22:02:24 +0100592 if ut_subtest == 'hush hush_test_simple_dollar':
593 # ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
594 with u_boot_console.disable_check('unknown_command'):
595 output = u_boot_console.run_command('ut ' + ut_subtest)
Simon Glass08631802024-09-01 16:26:14 -0600596 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100597 else:
598 output = u_boot_console.run_command('ut ' + ut_subtest)
Stephen Warren770fe172016-02-08 14:44:16 -0700599 assert output.endswith('Failures: 0')