blob: 9166c8f6b6e4fb5a1935452872f4000eda28ea57 [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 Glassd2bc33ed2023-01-06 08:52:41 -0600211 complete = True
212
213 except ValueError as exc:
Simon Glass08631802024-09-01 16:26:14 -0600214 print(f'Falled to create image, failing back to prepared copy: {exc}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600215 finally:
216 if mounted:
Simon Glass08631802024-09-01 16:26:14 -0600217 u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600218 if loop:
Simon Glass08631802024-09-01 16:26:14 -0600219 u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600220
221 if not complete:
222 copy_prepared_image(cons, mmc_dev, fname)
223
224def setup_bootflow_image(cons):
225 """Create a 20MB disk image with a single FAT partition"""
226 mmc_dev = 1
Simon Glassf5e2df02023-01-17 10:47:41 -0700227 fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600228
229 loop = None
230 mounted = False
231 complete = False
232 try:
233 loop = mount_image(cons, fname, mnt, 'vfat')
Simon Glassd3203bd2022-04-24 23:31:25 -0600234 mounted = True
235
236 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
237 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
238 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
239 script = '''# extlinux.conf generated by appliance-creator
240ui menu.c32
241menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
242menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
243menu hidden
244timeout 20
245totaltimeout 600
246
247label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
248 kernel /%s
249 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
250 fdtdir /%s/
251 initrd /%s''' % (vmlinux, dtbdir, initrd)
252 ext = os.path.join(mnt, 'extlinux')
253 mkdir_cond(ext)
254
Simon Glass08631802024-09-01 16:26:14 -0600255 conf = os.path.join(ext, 'extlinux.conf')
256 with open(conf, 'w', encoding='ascii') as fd:
Simon Glassd3203bd2022-04-24 23:31:25 -0600257 print(script, file=fd)
258
259 inf = os.path.join(cons.config.persistent_data_dir, 'inf')
260 with open(inf, 'wb') as fd:
261 fd.write(gzip.compress(b'vmlinux'))
Simon Glass08631802024-09-01 16:26:14 -0600262 u_boot_utils.run_and_log(
263 cons, f'mkimage -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600264
Simon Glass08631802024-09-01 16:26:14 -0600265 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
Simon Glassd3203bd2022-04-24 23:31:25 -0600266 print('initrd', file=fd)
267
268 mkdir_cond(os.path.join(mnt, dtbdir))
269
Simon Glass08631802024-09-01 16:26:14 -0600270 dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
Simon Glassd3203bd2022-04-24 23:31:25 -0600271 u_boot_utils.run_and_log(
Simon Glass08631802024-09-01 16:26:14 -0600272 cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
Simon Glassd3203bd2022-04-24 23:31:25 -0600273 complete = True
274 except ValueError as exc:
Simon Glass08631802024-09-01 16:26:14 -0600275 print(f'Falled to create image, failing back to prepared copy: {exc}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600276 finally:
277 if mounted:
Simon Glass08631802024-09-01 16:26:14 -0600278 u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600279 if loop:
Simon Glass08631802024-09-01 16:26:14 -0600280 u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600281
282 if not complete:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600283 copy_prepared_image(cons, mmc_dev, fname)
Simon Glassd3203bd2022-04-24 23:31:25 -0600284
285
Simon Glassfff928c2023-08-24 13:55:41 -0600286def setup_cros_image(cons):
287 """Create a 20MB disk image with ChromiumOS partitions"""
288 Partition = collections.namedtuple('part', 'start,size,name')
289 parts = {}
290 disk_data = None
291
292 def pack_kernel(cons, arch, kern, dummy):
293 """Pack a kernel containing some fake data
294
295 Args:
296 cons (ConsoleBase): Console to use
297 arch (str): Architecture to use ('x86' or 'arm')
298 kern (str): Filename containing kernel
299 dummy (str): Dummy filename to use for config and bootloader
300
301 Return:
302 bytes: Packed-kernel data
303 """
Simon Glass08631802024-09-01 16:26:14 -0600304 kern_part = os.path.join(cons.config.result_dir,
305 f'kern-part-{arch}.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600306 u_boot_utils.run_and_log(
307 cons,
308 f'futility vbutil_kernel --pack {kern_part} '
309 '--keyblock doc/chromium/files/devkeys/kernel.keyblock '
310 '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk '
311 f'--version 1 --config {dummy} --bootloader {dummy} '
312 f'--vmlinuz {kern}')
313
314 with open(kern_part, 'rb') as inf:
315 kern_part_data = inf.read()
316 return kern_part_data
317
318 def set_part_data(partnum, data):
319 """Set the contents of a disk partition
320
321 This updates disk_data by putting data in the right place
322
323 Args:
324 partnum (int): Partition number to set
325 data (bytes): Data for that partition
326 """
327 nonlocal disk_data
328
329 start = parts[partnum].start * sect_size
330 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
331
332 mmc_dev = 5
333 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600334 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Simon Glassfff928c2023-08-24 13:55:41 -0600335 #mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
336 #mkdir_cond(mnt)
337 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
338
339 uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
340 uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309'
341 uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
342 uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3'
343 uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e'
344 uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
345
346 ptr = 40
347
348 # Number of sectors in 1MB
349 sect_size = 512
350 sect_1mb = (1 << 20) // sect_size
351
352 required_parts = [
353 {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'},
354 {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'},
355 {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'},
356 {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
357 {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
358
359 {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'},
360 {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'},
361
362 {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'},
363 {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'},
364
365 {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'},
366 {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'},
367 {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'},
368 ]
369
370 for part in required_parts:
371 size_str = part['size']
372 if 'M' in size_str:
373 size = int(size_str[:-1]) * sect_1mb
374 else:
375 size = int(size_str)
376 u_boot_utils.run_and_log(
377 cons,
378 f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}")
379 ptr += size
380
381 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
382 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
Simon Glass08631802024-09-01 16:26:14 -0600383
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
Simon Glassfff928c2023-08-24 13:55:41 -0600397
398 # Create a dict (indexed by partition number) containing the above info
399 for line in out.splitlines():
400 start, size, num, name = line.split(maxsplit=3)
401 parts[int(num)] = Partition(int(start), int(size), name)
402
403 dummy = os.path.join(cons.config.result_dir, 'dummy.txt')
404 with open(dummy, 'wb') as outf:
405 outf.write(b'dummy\n')
406
407 # For now we just use dummy kernels. This limits testing to just detecting
408 # a signed kernel. We could add support for the x86 data structures so that
409 # testing could cover getting the cmdline, setup.bin and other pieces.
410 kern = os.path.join(cons.config.result_dir, 'kern.bin')
411 with open(kern, 'wb') as outf:
412 outf.write(b'kernel\n')
413
414 with open(fname, 'rb') as inf:
415 disk_data = inf.read()
416
417 # put x86 kernel in partition 2 and arm one in partition 4
418 set_part_data(2, pack_kernel(cons, 'x86', kern, dummy))
419 set_part_data(4, pack_kernel(cons, 'arm', kern, dummy))
420
421 with open(fname, 'wb') as outf:
422 outf.write(disk_data)
423
424 return fname
425
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200426def setup_android_image(cons):
427 """Create a 20MB disk image with Android partitions"""
428 Partition = collections.namedtuple('part', 'start,size,name')
429 parts = {}
430 disk_data = None
431
432 def set_part_data(partnum, data):
433 """Set the contents of a disk partition
434
435 This updates disk_data by putting data in the right place
436
437 Args:
438 partnum (int): Partition number to set
439 data (bytes): Data for that partition
440 """
441 nonlocal disk_data
442
443 start = parts[partnum].start * sect_size
444 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
445
446 mmc_dev = 7
447 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600448 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200449 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
450
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 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
462 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
463 ]
464
465 for part in required_parts:
466 size_str = part['size']
467 if 'M' in size_str:
468 size = int(size_str[:-1]) * sect_1mb
469 else:
470 size = int(size_str)
471 u_boot_utils.run_and_log(
472 cons,
473 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
474 ptr += size
475
476 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
477 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
478
479 # Create a dict (indexed by partition number) containing the above info
480 for line in out.splitlines():
481 start, size, num, name = line.split(maxsplit=3)
482 parts[int(num)] = Partition(int(start), int(size), name)
483
484 with open(fname, 'rb') as inf:
485 disk_data = inf.read()
486
487 test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex)
488 boot_img = os.path.join(cons.config.result_dir, 'bootv4.img')
489 with open(boot_img, 'rb') as inf:
490 set_part_data(2, inf.read())
491
492 test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex)
493 vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img')
494 with open(vendor_boot_img, 'rb') as inf:
495 set_part_data(4, inf.read())
496
497 with open(fname, 'wb') as outf:
498 outf.write(disk_data)
499
Simon Glass08631802024-09-01 16:26:14 -0600500 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200501
502 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600503
Simon Glassb8c26552023-06-01 10:23:03 -0600504def setup_cedit_file(cons):
Simon Glass08631802024-09-01 16:26:14 -0600505 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glassb8c26552023-06-01 10:23:03 -0600506 infname = os.path.join(cons.config.source_dir,
507 'test/boot/files/expo_layout.dts')
Simon Glassb1263bc2023-08-14 16:40:28 -0600508 inhname = os.path.join(cons.config.source_dir,
509 'test/boot/files/expo_ids.h')
Simon Glassb8c26552023-06-01 10:23:03 -0600510 expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py')
511 outfname = 'cedit.dtb'
512 u_boot_utils.run_and_log(
Simon Glassb1263bc2023-08-14 16:40:28 -0600513 cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600514
Stephen Warren770fe172016-02-08 14:44:16 -0700515@pytest.mark.buildconfigspec('ut_dm')
516def 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 Rini439ed3e2019-10-24 11:59:22 -0400521 data = b'this is a test'
522 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700523 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 Rini439ed3e2019-10-24 11:59:22 -0400528 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700529 with open(fn, 'wb') as fh:
530 fh.write(data)
531
Simon Glass509f32e2022-09-21 16:21:47 +0200532 # 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 Glass08709212023-08-24 13:55:38 -0600541 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB')
542 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB')
Simon Glass2c7b0e42022-10-29 19:47:19 -0600543
Alexander Gendin038cb022023-10-09 01:24:36 +0000544 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 Glassd3203bd2022-04-24 23:31:25 -0600550@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600551@pytest.mark.buildconfigspec('sandbox')
Simon Glassd3203bd2022-04-24 23:31:25 -0600552def test_ut_dm_init_bootstd(u_boot_console):
553 """Initialise data for bootflow tests"""
554
555 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600556 setup_bootmenu_image(u_boot_console)
Simon Glassb8c26552023-06-01 10:23:03 -0600557 setup_cedit_file(u_boot_console)
Simon Glassfff928c2023-08-24 13:55:41 -0600558 setup_cros_image(u_boot_console)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200559 setup_android_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600560
561 # Restart so that the new mmc1.img is picked up
562 u_boot_console.restart_uboot()
563
564
Stephen Warren770fe172016-02-08 14:44:16 -0700565def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200566 """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 Warren770fe172016-02-08 14:44:16 -0700582
Francis Laniel91ec8702023-12-22 22:02:24 +0100583 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)
Simon Glass08631802024-09-01 16:26:14 -0600587 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100588 else:
589 output = u_boot_console.run_command('ut ' + ut_subtest)
Stephen Warren770fe172016-02-08 14:44:16 -0700590 assert output.endswith('Failures: 0')