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