blob: bab8b97672bb2b61380ebd7eeb391172d13a94e3 [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 Glassd3203bd2022-04-24 23:31:25 -06004import gzip
5import os
Stephen Warren770fe172016-02-08 14:44:16 -07006import os.path
7import pytest
8
Simon Glassd3203bd2022-04-24 23:31:25 -06009import u_boot_utils
Simon Glass2c7b0e42022-10-29 19:47:19 -060010from tests import fs_helper
Simon Glassd3203bd2022-04-24 23:31:25 -060011
12def mkdir_cond(dirname):
13 """Create a directory if it doesn't already exist
14
15 Args:
16 dirname: Name of directory to create
17 """
18 if not os.path.exists(dirname):
19 os.mkdir(dirname)
20
21def setup_bootflow_image(u_boot_console):
22 """Create a 20MB disk image with a single FAT partition"""
23 cons = u_boot_console
24 fname = os.path.join(cons.config.source_dir, 'mmc1.img')
25 mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
26 mkdir_cond(mnt)
27
28 u_boot_utils.run_and_log(cons, 'qemu-img create %s 20M' % fname)
29 u_boot_utils.run_and_log(cons, 'sudo sfdisk %s' % fname,
30 stdin=b'type=c')
31
32 loop = None
33 mounted = False
34 complete = False
35 try:
36 out = u_boot_utils.run_and_log(cons,
37 'sudo losetup --show -f -P %s' % fname)
38 loop = out.strip()
39 fatpart = '%sp1' % loop
40 u_boot_utils.run_and_log(cons, 'sudo mkfs.vfat %s' % fatpart)
41 u_boot_utils.run_and_log(
42 cons, 'sudo mount -o loop %s %s -o uid=%d,gid=%d' %
43 (fatpart, mnt, os.getuid(), os.getgid()))
44 mounted = True
45
46 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
47 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
48 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
49 script = '''# extlinux.conf generated by appliance-creator
50ui menu.c32
51menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
52menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
53menu hidden
54timeout 20
55totaltimeout 600
56
57label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
58 kernel /%s
59 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
60 fdtdir /%s/
61 initrd /%s''' % (vmlinux, dtbdir, initrd)
62 ext = os.path.join(mnt, 'extlinux')
63 mkdir_cond(ext)
64
65 with open(os.path.join(ext, 'extlinux.conf'), 'w') as fd:
66 print(script, file=fd)
67
68 inf = os.path.join(cons.config.persistent_data_dir, 'inf')
69 with open(inf, 'wb') as fd:
70 fd.write(gzip.compress(b'vmlinux'))
71 u_boot_utils.run_and_log(cons, 'mkimage -f auto -d %s %s' %
72 (inf, os.path.join(mnt, vmlinux)))
73
74 with open(os.path.join(mnt, initrd), 'w') as fd:
75 print('initrd', file=fd)
76
77 mkdir_cond(os.path.join(mnt, dtbdir))
78
79 dtb_file = os.path.join(mnt, '%s/sandbox.dtb' % dtbdir)
80 u_boot_utils.run_and_log(
81 cons, 'dtc -o %s' % dtb_file, stdin=b'/dts-v1/; / {};')
82 complete = True
83 except ValueError as exc:
84 print('Falled to create image, failing back to prepared copy: %s',
85 str(exc))
86 finally:
87 if mounted:
88 u_boot_utils.run_and_log(cons, 'sudo umount %s' % mnt)
89 if loop:
90 u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop)
91
92 if not complete:
93 # Use a prepared image since we cannot create one
94 infname = os.path.join(cons.config.source_dir,
95 'test/py/tests/bootstd/mmc1.img.xz')
96 u_boot_utils.run_and_log(
97 cons,
98 ['sh', '-c', 'xz -dc %s >%s' % (infname, fname)])
99
100
Stephen Warren770fe172016-02-08 14:44:16 -0700101@pytest.mark.buildconfigspec('ut_dm')
102def test_ut_dm_init(u_boot_console):
103 """Initialize data for ut dm tests."""
104
105 fn = u_boot_console.config.source_dir + '/testflash.bin'
106 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400107 data = b'this is a test'
108 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700109 with open(fn, 'wb') as fh:
110 fh.write(data)
111
112 fn = u_boot_console.config.source_dir + '/spi.bin'
113 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400114 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700115 with open(fn, 'wb') as fh:
116 fh.write(data)
117
Simon Glass509f32e2022-09-21 16:21:47 +0200118 # Create a file with a single partition
119 fn = u_boot_console.config.source_dir + '/scsi.img'
120 if not os.path.exists(fn):
121 data = b'\x00' * (2 * 1024 * 1024)
122 with open(fn, 'wb') as fh:
123 fh.write(data)
124 u_boot_utils.run_and_log(
125 u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
126
Simon Glass2c7b0e42022-10-29 19:47:19 -0600127 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB',
128 use_src_dir=True)
129 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB',
130 use_src_dir=True)
131
Simon Glassd3203bd2022-04-24 23:31:25 -0600132@pytest.mark.buildconfigspec('cmd_bootflow')
133def test_ut_dm_init_bootstd(u_boot_console):
134 """Initialise data for bootflow tests"""
135
136 setup_bootflow_image(u_boot_console)
137
138 # Restart so that the new mmc1.img is picked up
139 u_boot_console.restart_uboot()
140
141
Stephen Warren770fe172016-02-08 14:44:16 -0700142def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200143 """Execute a "ut" subtest.
144
145 The subtests are collected in function generate_ut_subtest() from linker
146 generated lists by applying a regular expression to the lines of file
147 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
148
149 Strict naming conventions have to be followed to match the regular
150 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
151 test suite foo that can be executed via command 'ut foo bar' and is
152 implemented in C function foo_test_bar().
153
154 Args:
155 u_boot_console (ConsoleBase): U-Boot console
156 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
157 execute command 'ut foo bar'
158 """
Stephen Warren770fe172016-02-08 14:44:16 -0700159
160 output = u_boot_console.run_command('ut ' + ut_subtest)
161 assert output.endswith('Failures: 0')