blob: 6958fabfa34db5003598cf936619883680bf68b5 [file] [log] [blame]
Stephen Warren770fe172016-02-08 14:44:16 -07001# SPDX-License-Identifier: GPL-2.0
Tom Rini10e47792018-05-06 17:58:06 -04002# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
Stephen Warren770fe172016-02-08 14:44:16 -07003
Simon Glassd2bc33ed2023-01-06 08:52:41 -06004import getpass
Simon Glassd3203bd2022-04-24 23:31:25 -06005import gzip
6import os
Stephen Warren770fe172016-02-08 14:44:16 -07007import os.path
8import pytest
9
Simon Glassd3203bd2022-04-24 23:31:25 -060010import u_boot_utils
Simon Glass2c7b0e42022-10-29 19:47:19 -060011from tests import fs_helper
Simon Glassd3203bd2022-04-24 23:31:25 -060012
13def mkdir_cond(dirname):
14 """Create a directory if it doesn't already exist
15
16 Args:
Simon Glassd2bc33ed2023-01-06 08:52:41 -060017 dirname (str): Name of directory to create
Simon Glassd3203bd2022-04-24 23:31:25 -060018 """
19 if not os.path.exists(dirname):
20 os.mkdir(dirname)
21
Simon Glassd2bc33ed2023-01-06 08:52:41 -060022def setup_image(cons, mmc_dev, part_type):
23 """Create a 20MB disk image with a single partition
24
25 Args:
26 cons (ConsoleBase): Console to use
27 mmc_dev (int): MMC device number to use, e.g. 1
28 part_type (int): Partition type, e.g. 0xc for FAT32
29
30 Returns:
31 tuple:
32 str: Filename of MMC image
33 str: Directory name of 'mnt' directory
34 """
35 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glassd3203bd2022-04-24 23:31:25 -060036 mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
37 mkdir_cond(mnt)
38
39 u_boot_utils.run_and_log(cons, 'qemu-img create %s 20M' % fname)
40 u_boot_utils.run_and_log(cons, 'sudo sfdisk %s' % fname,
Simon Glassd2bc33ed2023-01-06 08:52:41 -060041 stdin=f'type={part_type:x}'.encode('utf-8'))
42 return fname, mnt
43
44def mount_image(cons, fname, mnt, fstype):
45 """Create a filesystem and mount it on partition 1
46
47 Args:
48 cons (ConsoleBase): Console to use
49 fname (str): Filename of MMC image
50 mnt (str): Directory name of 'mnt' directory
51 fstype (str): Filesystem type ('vfat' or 'ext4')
52
53 Returns:
54 str: Name of loop device used
55 """
56 out = u_boot_utils.run_and_log(cons, 'sudo losetup --show -f -P %s' % fname)
57 loop = out.strip()
58 part = f'{loop}p1'
59 u_boot_utils.run_and_log(cons, f'sudo mkfs.{fstype} {part}')
60 opts = ''
61 if fstype == 'vfat':
62 opts += ' -o uid={os.getuid()},gid={os.getgid()}'
63 u_boot_utils.run_and_log(cons, f'sudo mount -o loop {part} {mnt}{opts}')
64 u_boot_utils.run_and_log(cons, f'sudo chown {getpass.getuser()} {mnt}')
65 return loop
66
67def copy_prepared_image(cons, mmc_dev, fname):
68 """Use a prepared image since we cannot create one
69
70 Args:
71 cons (ConsoleBase): Console touse
72 mmc_dev (int): MMC device number
73 fname (str): Filename of MMC image
74 """
75 infname = os.path.join(cons.config.source_dir,
76 f'test/py/tests/bootstd/mmc{mmc_dev}.img.xz')
77 u_boot_utils.run_and_log(
78 cons,
79 ['sh', '-c', 'xz -dc %s >%s' % (infname, fname)])
80
81def setup_bootmenu_image(cons):
82 """Create a 20MB disk image with a single ext4 partition
83
84 This is modelled on Armbian 22.08 Jammy
85 """
86 mmc_dev = 4
87 fname, mnt = setup_image(cons, mmc_dev, 0x83)
Simon Glassd3203bd2022-04-24 23:31:25 -060088
89 loop = None
90 mounted = False
91 complete = False
92 try:
Simon Glassd2bc33ed2023-01-06 08:52:41 -060093 loop = mount_image(cons, fname, mnt, 'ext4')
94 mounted = True
95
96 vmlinux = 'Image'
97 initrd = 'uInitrd'
98 dtbdir = 'dtb'
99 script = '''# DO NOT EDIT THIS FILE
100#
101# Please edit /boot/armbianEnv.txt to set supported parameters
102#
103
104setenv load_addr "0x9000000"
105setenv overlay_error "false"
106# default values
107setenv rootdev "/dev/mmcblk%dp1"
108setenv verbosity "1"
109setenv console "both"
110setenv bootlogo "false"
111setenv rootfstype "ext4"
112setenv docker_optimizations "on"
113setenv earlycon "off"
114
115echo "Boot script loaded from ${devtype} ${devnum}"
116
117if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then
118 load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt
119 env import -t ${load_addr} ${filesize}
120fi
121
122if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi
123
124if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi
125if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi
126if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi
127if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi
128
129# get PARTUUID of first partition on SD/eMMC the boot script was loaded from
130if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi
131
132setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"
133
134if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
135
136load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
137load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image
138
139load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
140fdt addr ${fdt_addr_r}
141fdt resize 65536
142for overlay_file in ${overlays}; do
143 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then
144 echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo"
145 fdt apply ${load_addr} || setenv overlay_error "true"
146 fi
147done
148for overlay_file in ${user_overlays}; do
149 if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then
150 echo "Applying user provided DT overlay ${overlay_file}.dtbo"
151 fdt apply ${load_addr} || setenv overlay_error "true"
152 fi
153done
154if test "${overlay_error}" = "true"; then
155 echo "Error applying DT overlays, restoring original DT"
156 load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
157else
158 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then
159 echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)"
160 source ${load_addr}
161 fi
162 if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then
163 load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr
164 echo "Applying user provided fixup script (fixup.scr)"
165 source ${load_addr}
166 fi
167fi
168booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
169
170# Recompile with:
171# mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
172''' % (mmc_dev)
173 bootdir = os.path.join(mnt, 'boot')
174 mkdir_cond(bootdir)
175 cmd_fname = os.path.join(bootdir, 'boot.cmd')
176 scr_fname = os.path.join(bootdir, 'boot.scr')
177 with open(cmd_fname, 'w') as outf:
178 print(script, file=outf)
179
180 infname = os.path.join(cons.config.source_dir,
181 'test/py/tests/bootstd/armbian.bmp.xz')
182 bmp_file = os.path.join(bootdir, 'boot.bmp')
183 u_boot_utils.run_and_log(
184 cons,
185 ['sh', '-c', f'xz -dc {infname} >{bmp_file}'])
186
187 u_boot_utils.run_and_log(
188 cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}')
189
190 kernel = 'vmlinuz-5.15.63-rockchip64'
191 target = os.path.join(bootdir, kernel)
192 with open(target, 'wb') as outf:
193 print('kernel', outf)
194
195 symlink = os.path.join(bootdir, 'Image')
196 if os.path.exists(symlink):
197 os.remove(symlink)
198 u_boot_utils.run_and_log(
199 cons, f'echo here {kernel} {symlink}')
200 os.symlink(kernel, symlink)
201
Simon Glassd3203bd2022-04-24 23:31:25 -0600202 u_boot_utils.run_and_log(
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600203 cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}')
204 complete = True
205
206 except ValueError as exc:
207 print('Falled to create image, failing back to prepared copy: %s',
208 str(exc))
209 finally:
210 if mounted:
211 u_boot_utils.run_and_log(cons, 'sudo umount %s' % mnt)
212 if loop:
213 u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop)
214
215 if not complete:
216 copy_prepared_image(cons, mmc_dev, fname)
217
218def setup_bootflow_image(cons):
219 """Create a 20MB disk image with a single FAT partition"""
220 mmc_dev = 1
221 fname, mnt = setup_image(cons, mmc_dev, 0xc)
222
223 loop = None
224 mounted = False
225 complete = False
226 try:
227 loop = mount_image(cons, fname, mnt, 'vfat')
Simon Glassd3203bd2022-04-24 23:31:25 -0600228 mounted = True
229
230 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
231 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
232 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
233 script = '''# extlinux.conf generated by appliance-creator
234ui menu.c32
235menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
236menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
237menu hidden
238timeout 20
239totaltimeout 600
240
241label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
242 kernel /%s
243 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
244 fdtdir /%s/
245 initrd /%s''' % (vmlinux, dtbdir, initrd)
246 ext = os.path.join(mnt, 'extlinux')
247 mkdir_cond(ext)
248
249 with open(os.path.join(ext, 'extlinux.conf'), 'w') as fd:
250 print(script, file=fd)
251
252 inf = os.path.join(cons.config.persistent_data_dir, 'inf')
253 with open(inf, 'wb') as fd:
254 fd.write(gzip.compress(b'vmlinux'))
255 u_boot_utils.run_and_log(cons, 'mkimage -f auto -d %s %s' %
256 (inf, os.path.join(mnt, vmlinux)))
257
258 with open(os.path.join(mnt, initrd), 'w') as fd:
259 print('initrd', file=fd)
260
261 mkdir_cond(os.path.join(mnt, dtbdir))
262
263 dtb_file = os.path.join(mnt, '%s/sandbox.dtb' % dtbdir)
264 u_boot_utils.run_and_log(
265 cons, 'dtc -o %s' % dtb_file, stdin=b'/dts-v1/; / {};')
266 complete = True
267 except ValueError as exc:
268 print('Falled to create image, failing back to prepared copy: %s',
269 str(exc))
270 finally:
271 if mounted:
272 u_boot_utils.run_and_log(cons, 'sudo umount %s' % mnt)
273 if loop:
274 u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop)
275
276 if not complete:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600277 copy_prepared_image(cons, mmc_dev, fname)
Simon Glassd3203bd2022-04-24 23:31:25 -0600278
279
Stephen Warren770fe172016-02-08 14:44:16 -0700280@pytest.mark.buildconfigspec('ut_dm')
281def test_ut_dm_init(u_boot_console):
282 """Initialize data for ut dm tests."""
283
284 fn = u_boot_console.config.source_dir + '/testflash.bin'
285 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400286 data = b'this is a test'
287 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700288 with open(fn, 'wb') as fh:
289 fh.write(data)
290
291 fn = u_boot_console.config.source_dir + '/spi.bin'
292 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400293 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700294 with open(fn, 'wb') as fh:
295 fh.write(data)
296
Simon Glass509f32e2022-09-21 16:21:47 +0200297 # Create a file with a single partition
298 fn = u_boot_console.config.source_dir + '/scsi.img'
299 if not os.path.exists(fn):
300 data = b'\x00' * (2 * 1024 * 1024)
301 with open(fn, 'wb') as fh:
302 fh.write(data)
303 u_boot_utils.run_and_log(
304 u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
305
Simon Glass2c7b0e42022-10-29 19:47:19 -0600306 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB',
307 use_src_dir=True)
308 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB',
309 use_src_dir=True)
310
Simon Glassd3203bd2022-04-24 23:31:25 -0600311@pytest.mark.buildconfigspec('cmd_bootflow')
312def test_ut_dm_init_bootstd(u_boot_console):
313 """Initialise data for bootflow tests"""
314
315 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600316 setup_bootmenu_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600317
318 # Restart so that the new mmc1.img is picked up
319 u_boot_console.restart_uboot()
320
321
Stephen Warren770fe172016-02-08 14:44:16 -0700322def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200323 """Execute a "ut" subtest.
324
325 The subtests are collected in function generate_ut_subtest() from linker
326 generated lists by applying a regular expression to the lines of file
327 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
328
329 Strict naming conventions have to be followed to match the regular
330 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
331 test suite foo that can be executed via command 'ut foo bar' and is
332 implemented in C function foo_test_bar().
333
334 Args:
335 u_boot_console (ConsoleBase): U-Boot console
336 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
337 execute command 'ut foo bar'
338 """
Stephen Warren770fe172016-02-08 14:44:16 -0700339
340 output = u_boot_console.run_command('ut ' + ut_subtest)
341 assert output.endswith('Failures: 0')