blob: 39aa1035e34da3b0cde6a1c7439d2c65d060b225 [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 Glassd2bc33ed2023-01-06 08:52:41 -060011import getpass
Simon Glassd3203bd2022-04-24 23:31:25 -060012import gzip
13import os
Stephen Warren770fe172016-02-08 14:44:16 -070014import os.path
15import pytest
16
Simon Glassd3203bd2022-04-24 23:31:25 -060017import u_boot_utils
Tom Rini50ad0192023-12-09 14:52:46 -050018# pylint: disable=E0611
Simon Glass2c7b0e42022-10-29 19:47:19 -060019from tests import fs_helper
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +020020from test_android import test_abootimg
Simon Glassd3203bd2022-04-24 23:31:25 -060021
22def mkdir_cond(dirname):
23 """Create a directory if it doesn't already exist
24
25 Args:
Simon Glassd2bc33ed2023-01-06 08:52:41 -060026 dirname (str): Name of directory to create
Simon Glassd3203bd2022-04-24 23:31:25 -060027 """
28 if not os.path.exists(dirname):
29 os.mkdir(dirname)
30
Simon Glassf5e2df02023-01-17 10:47:41 -070031def setup_image(cons, mmc_dev, part_type, second_part=False):
Simon Glassd2bc33ed2023-01-06 08:52:41 -060032 """Create a 20MB disk image with a single partition
33
34 Args:
35 cons (ConsoleBase): Console to use
36 mmc_dev (int): MMC device number to use, e.g. 1
37 part_type (int): Partition type, e.g. 0xc for FAT32
Simon Glassf5e2df02023-01-17 10:47:41 -070038 second_part (bool): True to contain a small second partition
Simon Glassd2bc33ed2023-01-06 08:52:41 -060039
40 Returns:
41 tuple:
42 str: Filename of MMC image
43 str: Directory name of 'mnt' directory
44 """
45 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glassd3203bd2022-04-24 23:31:25 -060046 mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
47 mkdir_cond(mnt)
48
Simon Glassf5e2df02023-01-17 10:47:41 -070049 spec = f'type={part_type:x}, size=18M, bootable'
50 if second_part:
51 spec += '\ntype=c'
52
Simon Glass08631802024-09-01 16:26:14 -060053 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
54 u_boot_utils.run_and_log(cons, f'sudo sfdisk {fname}',
Simon Glassf5e2df02023-01-17 10:47:41 -070055 stdin=spec.encode('utf-8'))
Simon Glassd2bc33ed2023-01-06 08:52:41 -060056 return fname, mnt
57
58def mount_image(cons, fname, mnt, fstype):
59 """Create a filesystem and mount it on partition 1
60
61 Args:
62 cons (ConsoleBase): Console to use
63 fname (str): Filename of MMC image
64 mnt (str): Directory name of 'mnt' directory
65 fstype (str): Filesystem type ('vfat' or 'ext4')
66
67 Returns:
68 str: Name of loop device used
69 """
Simon Glass08631802024-09-01 16:26:14 -060070 out = u_boot_utils.run_and_log(cons, f'sudo losetup --show -f -P {fname}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -060071 loop = out.strip()
72 part = f'{loop}p1'
73 u_boot_utils.run_and_log(cons, f'sudo mkfs.{fstype} {part}')
74 opts = ''
75 if fstype == 'vfat':
Simon Glass08631802024-09-01 16:26:14 -060076 opts += f' -o uid={os.getuid()},gid={os.getgid()}'
Simon Glassd2bc33ed2023-01-06 08:52:41 -060077 u_boot_utils.run_and_log(cons, f'sudo mount -o loop {part} {mnt}{opts}')
78 u_boot_utils.run_and_log(cons, f'sudo chown {getpass.getuser()} {mnt}')
79 return loop
80
81def copy_prepared_image(cons, mmc_dev, fname):
82 """Use a prepared image since we cannot create one
83
84 Args:
85 cons (ConsoleBase): Console touse
86 mmc_dev (int): MMC device number
87 fname (str): Filename of MMC image
88 """
89 infname = os.path.join(cons.config.source_dir,
90 f'test/py/tests/bootstd/mmc{mmc_dev}.img.xz')
Simon Glass08631802024-09-01 16:26:14 -060091 u_boot_utils.run_and_log(cons, ['sh', '-c', f'xz -dc {infname} >{fname}'])
Simon Glassd2bc33ed2023-01-06 08:52:41 -060092
93def setup_bootmenu_image(cons):
94 """Create a 20MB disk image with a single ext4 partition
95
96 This is modelled on Armbian 22.08 Jammy
97 """
98 mmc_dev = 4
99 fname, mnt = setup_image(cons, mmc_dev, 0x83)
Simon Glassd3203bd2022-04-24 23:31:25 -0600100
101 loop = None
102 mounted = False
103 complete = False
104 try:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600105 loop = mount_image(cons, fname, mnt, 'ext4')
106 mounted = True
107
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600108 script = '''# DO NOT EDIT THIS FILE
109#
110# Please edit /boot/armbianEnv.txt to set supported parameters
111#
112
113setenv load_addr "0x9000000"
114setenv overlay_error "false"
115# default values
116setenv rootdev "/dev/mmcblk%dp1"
117setenv verbosity "1"
118setenv console "both"
119setenv bootlogo "false"
120setenv rootfstype "ext4"
121setenv docker_optimizations "on"
122setenv earlycon "off"
123
124echo "Boot script loaded from ${devtype} ${devnum}"
125
126if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then
127 load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt
128 env import -t ${load_addr} ${filesize}
129fi
130
131if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi
132
133if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi
134if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi
135if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi
136if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi
137
138# get PARTUUID of first partition on SD/eMMC the boot script was loaded from
139if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi
140
141setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"
142
143if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
144
145load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
146load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image
147
148load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
149fdt addr ${fdt_addr_r}
150fdt resize 65536
151for overlay_file in ${overlays}; do
152 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then
153 echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo"
154 fdt apply ${load_addr} || setenv overlay_error "true"
155 fi
156done
157for overlay_file in ${user_overlays}; do
158 if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then
159 echo "Applying user provided DT overlay ${overlay_file}.dtbo"
160 fdt apply ${load_addr} || setenv overlay_error "true"
161 fi
162done
163if test "${overlay_error}" = "true"; then
164 echo "Error applying DT overlays, restoring original DT"
165 load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
166else
167 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then
168 echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)"
169 source ${load_addr}
170 fi
171 if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then
172 load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr
173 echo "Applying user provided fixup script (fixup.scr)"
174 source ${load_addr}
175 fi
176fi
177booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
178
179# Recompile with:
180# mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
Simon Glass08631802024-09-01 16:26:14 -0600181'''
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600182 bootdir = os.path.join(mnt, 'boot')
183 mkdir_cond(bootdir)
184 cmd_fname = os.path.join(bootdir, 'boot.cmd')
185 scr_fname = os.path.join(bootdir, 'boot.scr')
Simon Glass08631802024-09-01 16:26:14 -0600186 with open(cmd_fname, 'w', encoding='ascii') as outf:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600187 print(script, file=outf)
188
189 infname = os.path.join(cons.config.source_dir,
190 'test/py/tests/bootstd/armbian.bmp.xz')
191 bmp_file = os.path.join(bootdir, 'boot.bmp')
192 u_boot_utils.run_and_log(
193 cons,
194 ['sh', '-c', f'xz -dc {infname} >{bmp_file}'])
195
196 u_boot_utils.run_and_log(
197 cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}')
198
199 kernel = 'vmlinuz-5.15.63-rockchip64'
200 target = os.path.join(bootdir, kernel)
201 with open(target, 'wb') as outf:
202 print('kernel', outf)
203
204 symlink = os.path.join(bootdir, 'Image')
205 if os.path.exists(symlink):
206 os.remove(symlink)
207 u_boot_utils.run_and_log(
208 cons, f'echo here {kernel} {symlink}')
209 os.symlink(kernel, symlink)
210
Simon Glassd3203bd2022-04-24 23:31:25 -0600211 u_boot_utils.run_and_log(
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600212 cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}')
213 complete = True
214
215 except ValueError as exc:
Simon Glass08631802024-09-01 16:26:14 -0600216 print(f'Falled to create image, failing back to prepared copy: {exc}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600217 finally:
218 if mounted:
Simon Glass08631802024-09-01 16:26:14 -0600219 u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600220 if loop:
Simon Glass08631802024-09-01 16:26:14 -0600221 u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600222
223 if not complete:
224 copy_prepared_image(cons, mmc_dev, fname)
225
226def setup_bootflow_image(cons):
227 """Create a 20MB disk image with a single FAT partition"""
228 mmc_dev = 1
Simon Glassf5e2df02023-01-17 10:47:41 -0700229 fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600230
231 loop = None
232 mounted = False
233 complete = False
234 try:
235 loop = mount_image(cons, fname, mnt, 'vfat')
Simon Glassd3203bd2022-04-24 23:31:25 -0600236 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
242ui menu.c32
243menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
244menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
245menu hidden
246timeout 20
247totaltimeout 600
248
249label 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
Simon Glass08631802024-09-01 16:26:14 -0600257 conf = os.path.join(ext, 'extlinux.conf')
258 with open(conf, 'w', encoding='ascii') as fd:
Simon Glassd3203bd2022-04-24 23:31:25 -0600259 print(script, file=fd)
260
261 inf = os.path.join(cons.config.persistent_data_dir, 'inf')
262 with open(inf, 'wb') as fd:
263 fd.write(gzip.compress(b'vmlinux'))
Simon Glass08631802024-09-01 16:26:14 -0600264 u_boot_utils.run_and_log(
265 cons, f'mkimage -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600266
Simon Glass08631802024-09-01 16:26:14 -0600267 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
Simon Glassd3203bd2022-04-24 23:31:25 -0600268 print('initrd', file=fd)
269
270 mkdir_cond(os.path.join(mnt, dtbdir))
271
Simon Glass08631802024-09-01 16:26:14 -0600272 dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
Simon Glassd3203bd2022-04-24 23:31:25 -0600273 u_boot_utils.run_and_log(
Simon Glass08631802024-09-01 16:26:14 -0600274 cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
Simon Glassd3203bd2022-04-24 23:31:25 -0600275 complete = True
276 except ValueError as exc:
Simon Glass08631802024-09-01 16:26:14 -0600277 print(f'Falled to create image, failing back to prepared copy: {exc}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600278 finally:
279 if mounted:
Simon Glass08631802024-09-01 16:26:14 -0600280 u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600281 if loop:
Simon Glass08631802024-09-01 16:26:14 -0600282 u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600283
284 if not complete:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600285 copy_prepared_image(cons, mmc_dev, fname)
Simon Glassd3203bd2022-04-24 23:31:25 -0600286
287
Simon Glassfff928c2023-08-24 13:55:41 -0600288def 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 """
Simon Glass08631802024-09-01 16:26:14 -0600306 kern_part = os.path.join(cons.config.result_dir,
307 f'kern-part-{arch}.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600308 u_boot_utils.run_and_log(
309 cons,
310 f'futility vbutil_kernel --pack {kern_part} '
311 '--keyblock doc/chromium/files/devkeys/kernel.keyblock '
312 '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk '
313 f'--version 1 --config {dummy} --bootloader {dummy} '
314 f'--vmlinuz {kern}')
315
316 with open(kern_part, 'rb') as inf:
317 kern_part_data = inf.read()
318 return kern_part_data
319
320 def set_part_data(partnum, data):
321 """Set the contents of a disk partition
322
323 This updates disk_data by putting data in the right place
324
325 Args:
326 partnum (int): Partition number to set
327 data (bytes): Data for that partition
328 """
329 nonlocal disk_data
330
331 start = parts[partnum].start * sect_size
332 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
333
334 mmc_dev = 5
335 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600336 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Simon Glassfff928c2023-08-24 13:55:41 -0600337 #mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
338 #mkdir_cond(mnt)
339 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
340
341 uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
342 uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309'
343 uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
344 uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3'
345 uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e'
346 uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
347
348 ptr = 40
349
350 # Number of sectors in 1MB
351 sect_size = 512
352 sect_1mb = (1 << 20) // sect_size
353
354 required_parts = [
355 {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'},
356 {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'},
357 {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'},
358 {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
359 {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
360
361 {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'},
362 {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'},
363
364 {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'},
365 {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'},
366
367 {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'},
368 {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'},
369 {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'},
370 ]
371
372 for part in required_parts:
373 size_str = part['size']
374 if 'M' in size_str:
375 size = int(size_str[:-1]) * sect_1mb
376 else:
377 size = int(size_str)
378 u_boot_utils.run_and_log(
379 cons,
380 f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}")
381 ptr += size
382
383 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
384 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
Simon Glass08631802024-09-01 16:26:14 -0600385
386 # We expect something like this:
387 # 8239 2048 1 Basic data
388 # 45 2048 2 ChromeOS kernel
389 # 8238 1 3 ChromeOS rootfs
390 # 2093 2048 4 ChromeOS kernel
391 # 8237 1 5 ChromeOS rootfs
392 # 41 1 6 ChromeOS kernel
393 # 42 1 7 ChromeOS rootfs
394 # 4141 2048 8 Basic data
395 # 43 1 9 ChromeOS reserved
396 # 44 1 10 ChromeOS reserved
397 # 40 1 11 ChromeOS firmware
398 # 6189 2048 12 EFI System Partition
Simon Glassfff928c2023-08-24 13:55:41 -0600399
400 # Create a dict (indexed by partition number) containing the above info
401 for line in out.splitlines():
402 start, size, num, name = line.split(maxsplit=3)
403 parts[int(num)] = Partition(int(start), int(size), name)
404
405 dummy = os.path.join(cons.config.result_dir, 'dummy.txt')
406 with open(dummy, 'wb') as outf:
407 outf.write(b'dummy\n')
408
409 # For now we just use dummy kernels. This limits testing to just detecting
410 # a signed kernel. We could add support for the x86 data structures so that
411 # testing could cover getting the cmdline, setup.bin and other pieces.
412 kern = os.path.join(cons.config.result_dir, 'kern.bin')
413 with open(kern, 'wb') as outf:
414 outf.write(b'kernel\n')
415
416 with open(fname, 'rb') as inf:
417 disk_data = inf.read()
418
419 # put x86 kernel in partition 2 and arm one in partition 4
420 set_part_data(2, pack_kernel(cons, 'x86', kern, dummy))
421 set_part_data(4, pack_kernel(cons, 'arm', kern, dummy))
422
423 with open(fname, 'wb') as outf:
424 outf.write(disk_data)
425
426 return fname
427
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200428def setup_android_image(cons):
429 """Create a 20MB disk image with Android partitions"""
430 Partition = collections.namedtuple('part', 'start,size,name')
431 parts = {}
432 disk_data = None
433
434 def set_part_data(partnum, data):
435 """Set the contents of a disk partition
436
437 This updates disk_data by putting data in the right place
438
439 Args:
440 partnum (int): Partition number to set
441 data (bytes): Data for that partition
442 """
443 nonlocal disk_data
444
445 start = parts[partnum].start * sect_size
446 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
447
448 mmc_dev = 7
449 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600450 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200451 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
452
453 ptr = 40
454
455 # Number of sectors in 1MB
456 sect_size = 512
457 sect_1mb = (1 << 20) // sect_size
458
459 required_parts = [
460 {'num': 1, 'label':'misc', 'size': '1M'},
461 {'num': 2, 'label':'boot_a', 'size': '4M'},
462 {'num': 3, 'label':'boot_b', 'size': '4M'},
463 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
464 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
465 ]
466
467 for part in required_parts:
468 size_str = part['size']
469 if 'M' in size_str:
470 size = int(size_str[:-1]) * sect_1mb
471 else:
472 size = int(size_str)
473 u_boot_utils.run_and_log(
474 cons,
475 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
476 ptr += size
477
478 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
479 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
480
481 # Create a dict (indexed by partition number) containing the above info
482 for line in out.splitlines():
483 start, size, num, name = line.split(maxsplit=3)
484 parts[int(num)] = Partition(int(start), int(size), name)
485
486 with open(fname, 'rb') as inf:
487 disk_data = inf.read()
488
489 test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex)
490 boot_img = os.path.join(cons.config.result_dir, 'bootv4.img')
491 with open(boot_img, 'rb') as inf:
492 set_part_data(2, inf.read())
493
494 test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex)
495 vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img')
496 with open(vendor_boot_img, 'rb') as inf:
497 set_part_data(4, inf.read())
498
499 with open(fname, 'wb') as outf:
500 outf.write(disk_data)
501
Simon Glass08631802024-09-01 16:26:14 -0600502 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200503
504 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600505
Simon Glassb8c26552023-06-01 10:23:03 -0600506def setup_cedit_file(cons):
Simon Glass08631802024-09-01 16:26:14 -0600507 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glassb8c26552023-06-01 10:23:03 -0600508 infname = os.path.join(cons.config.source_dir,
509 'test/boot/files/expo_layout.dts')
Simon Glassb1263bc2023-08-14 16:40:28 -0600510 inhname = os.path.join(cons.config.source_dir,
511 'test/boot/files/expo_ids.h')
Simon Glassb8c26552023-06-01 10:23:03 -0600512 expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py')
513 outfname = 'cedit.dtb'
514 u_boot_utils.run_and_log(
Simon Glassb1263bc2023-08-14 16:40:28 -0600515 cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600516
Stephen Warren770fe172016-02-08 14:44:16 -0700517@pytest.mark.buildconfigspec('ut_dm')
518def test_ut_dm_init(u_boot_console):
519 """Initialize data for ut dm tests."""
520
521 fn = u_boot_console.config.source_dir + '/testflash.bin'
522 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400523 data = b'this is a test'
524 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700525 with open(fn, 'wb') as fh:
526 fh.write(data)
527
528 fn = u_boot_console.config.source_dir + '/spi.bin'
529 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400530 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700531 with open(fn, 'wb') as fh:
532 fh.write(data)
533
Simon Glass509f32e2022-09-21 16:21:47 +0200534 # Create a file with a single partition
535 fn = u_boot_console.config.source_dir + '/scsi.img'
536 if not os.path.exists(fn):
537 data = b'\x00' * (2 * 1024 * 1024)
538 with open(fn, 'wb') as fh:
539 fh.write(data)
540 u_boot_utils.run_and_log(
541 u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
542
Simon Glass08709212023-08-24 13:55:38 -0600543 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB')
544 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB')
Simon Glass2c7b0e42022-10-29 19:47:19 -0600545
Alexander Gendin038cb022023-10-09 01:24:36 +0000546 mmc_dev = 6
547 fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img')
548 data = b'\x00' * (12 * 1024 * 1024)
549 with open(fn, 'wb') as fh:
550 fh.write(data)
551
Simon Glassd3203bd2022-04-24 23:31:25 -0600552@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600553@pytest.mark.buildconfigspec('sandbox')
Simon Glassd3203bd2022-04-24 23:31:25 -0600554def test_ut_dm_init_bootstd(u_boot_console):
555 """Initialise data for bootflow tests"""
556
557 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600558 setup_bootmenu_image(u_boot_console)
Simon Glassb8c26552023-06-01 10:23:03 -0600559 setup_cedit_file(u_boot_console)
Simon Glassfff928c2023-08-24 13:55:41 -0600560 setup_cros_image(u_boot_console)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200561 setup_android_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600562
563 # Restart so that the new mmc1.img is picked up
564 u_boot_console.restart_uboot()
565
566
Stephen Warren770fe172016-02-08 14:44:16 -0700567def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200568 """Execute a "ut" subtest.
569
570 The subtests are collected in function generate_ut_subtest() from linker
571 generated lists by applying a regular expression to the lines of file
572 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
573
574 Strict naming conventions have to be followed to match the regular
575 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
576 test suite foo that can be executed via command 'ut foo bar' and is
577 implemented in C function foo_test_bar().
578
579 Args:
580 u_boot_console (ConsoleBase): U-Boot console
581 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
582 execute command 'ut foo bar'
583 """
Stephen Warren770fe172016-02-08 14:44:16 -0700584
Francis Laniel91ec8702023-12-22 22:02:24 +0100585 if ut_subtest == 'hush hush_test_simple_dollar':
586 # ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
587 with u_boot_console.disable_check('unknown_command'):
588 output = u_boot_console.run_command('ut ' + ut_subtest)
Simon Glass08631802024-09-01 16:26:14 -0600589 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100590 else:
591 output = u_boot_console.run_command('ut ' + ut_subtest)
Stephen Warren770fe172016-02-08 14:44:16 -0700592 assert output.endswith('Failures: 0')