]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - test/py/tests/test_ut.py
1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
11 from tests
import fs_helper
13 def mkdir_cond ( dirname
):
14 """Create a directory if it doesn't already exist
17 dirname (str): Name of directory to create
19 if not os
. path
. exists ( dirname
):
22 def setup_image ( cons
, mmc_dev
, part_type
, second_part
= False ):
23 """Create a 20MB disk image with a single partition
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 second_part (bool): True to contain a small second partition
33 str: Filename of MMC image
34 str: Directory name of 'mnt' directory
36 fname
= os
. path
. join ( cons
. config
. source_dir
, f
'mmc {mmc_dev} .img' )
37 mnt
= os
. path
. join ( cons
. config
. persistent_data_dir
, 'mnt' )
40 spec
= f
'type={part_type:x}, size=18M, bootable'
44 u_boot_utils
. run_and_log ( cons
, 'qemu-img create %s 20M' % fname
)
45 u_boot_utils
. run_and_log ( cons
, 'sudo sfdisk %s ' % fname
,
46 stdin
= spec
. encode ( 'utf-8' ))
49 def mount_image ( cons
, fname
, mnt
, fstype
):
50 """Create a filesystem and mount it on partition 1
53 cons (ConsoleBase): Console to use
54 fname (str): Filename of MMC image
55 mnt (str): Directory name of 'mnt' directory
56 fstype (str): Filesystem type ('vfat' or 'ext4')
59 str: Name of loop device used
61 out
= u_boot_utils
. run_and_log ( cons
, 'sudo losetup --show -f -P %s ' % fname
)
64 u_boot_utils
. run_and_log ( cons
, f
'sudo mkfs. {fstype} {part} ' )
67 opts
+= f
' -o uid={os.getuid()},gid={os.getgid()}'
68 u_boot_utils
. run_and_log ( cons
, f
'sudo mount -o loop {part} {mnt}{opts} ' )
69 u_boot_utils
. run_and_log ( cons
, f
'sudo chown {getpass.getuser()} {mnt} ' )
72 def copy_prepared_image ( cons
, mmc_dev
, fname
):
73 """Use a prepared image since we cannot create one
76 cons (ConsoleBase): Console touse
77 mmc_dev (int): MMC device number
78 fname (str): Filename of MMC image
80 infname
= os
. path
. join ( cons
. config
. source_dir
,
81 f
'test/py/tests/bootstd/mmc {mmc_dev} .img.xz' )
82 u_boot_utils
. run_and_log (
84 [ 'sh' , '-c' , 'xz -dc %s > %s ' % ( infname
, fname
)])
86 def setup_bootmenu_image ( cons
):
87 """Create a 20MB disk image with a single ext4 partition
89 This is modelled on Armbian 22.08 Jammy
92 fname
, mnt
= setup_image ( cons
, mmc_dev
, 0x83 )
98 loop
= mount_image ( cons
, fname
, mnt
, 'ext4' )
104 script
= '''# DO NOT EDIT THIS FILE
106 # Please edit /boot/armbianEnv.txt to set supported parameters
109 setenv load_addr "0x9000000"
110 setenv overlay_error "false"
112 setenv rootdev "/dev/mmcblk %d p1"
114 setenv console "both"
115 setenv bootlogo "false"
116 setenv rootfstype "ext4"
117 setenv docker_optimizations "on"
118 setenv earlycon "off"
120 echo "Boot script loaded from $ {devtype} $ {devnum} "
122 if test -e $ {devtype} $ {devnum} $ {prefix} armbianEnv.txt; then
123 load $ {devtype} $ {devnum} $ {load_addr} $ {prefix} armbianEnv.txt
124 env import -t $ {load_addr} $ {filesize}
127 if test "$ {logo} " = "disabled"; then setenv logo "logo.nologo"; fi
129 if test "$ {console} " = "display" || test "$ {console} " = "both"; then setenv consoleargs "console=tty1"; fi
130 if test "$ {console} " = "serial" || test "$ {console} " = "both"; then setenv consoleargs "console=ttyS2,1500000 $ {consoleargs} "; fi
131 if test "$ {earlycon} " = "on"; then setenv consoleargs "earlycon $ {consoleargs} "; fi
132 if test "$ {bootlogo} " = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian $ {consoleargs} "; fi
134 # get PARTUUID of first partition on SD/eMMC the boot script was loaded from
135 if test "$ {devtype} " = "mmc"; then part uuid mmc $ {devnum} :1 partuuid; fi
137 setenv bootargs "root=$ {rootdev} rootwait rootfstype=$ {rootfstype} $ {consoleargs} consoleblank=0 loglevel=$ {verbosity} ubootpart=$ {partuuid} usb-storage.quirks=$ {usbstoragequirks} $ {extraargs} $ {extraboardargs} "
139 if test "$ {docker_optimizations} " = "on"; then setenv bootargs "$ {bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
141 load $ {devtype} $ {devnum} $ {ramdisk_addr_r} $ {prefix} uInitrd
142 load $ {devtype} $ {devnum} $ {kernel_addr_r} $ {prefix} Image
144 load $ {devtype} $ {devnum} $ {fdt_addr_r} $ {prefix} dtb/$ {fdtfile}
145 fdt addr $ {fdt_addr_r}
147 for overlay_file in $ {overlays} ; do
148 if load $ {devtype} $ {devnum} $ {load_addr} $ {prefix} dtb/rockchip/overlay/$ {overlay_prefix} -$ {overlay_file} .dtbo; then
149 echo "Applying kernel provided DT overlay $ {overlay_prefix} -$ {overlay_file} .dtbo"
150 fdt apply $ {load_addr} || setenv overlay_error "true"
153 for overlay_file in $ {user_overlays} ; do
154 if load $ {devtype} $ {devnum} $ {load_addr} $ {prefix} overlay-user/$ {overlay_file} .dtbo; then
155 echo "Applying user provided DT overlay $ {overlay_file} .dtbo"
156 fdt apply $ {load_addr} || setenv overlay_error "true"
159 if test "$ {overlay_error} " = "true"; then
160 echo "Error applying DT overlays, restoring original DT"
161 load $ {devtype} $ {devnum} $ {fdt_addr_r} $ {prefix} dtb/$ {fdtfile}
163 if load $ {devtype} $ {devnum} $ {load_addr} $ {prefix} dtb/rockchip/overlay/$ {overlay_prefix} -fixup.scr; then
164 echo "Applying kernel provided DT fixup script ($ {overlay_prefix} -fixup.scr)"
167 if test -e $ {devtype} $ {devnum} $ {prefix} fixup.scr; then
168 load $ {devtype} $ {devnum} $ {load_addr} $ {prefix} fixup.scr
169 echo "Applying user provided fixup script (fixup.scr)"
173 booti $ {kernel_addr_r} $ {ramdisk_addr_r} $ {fdt_addr_r}
176 # mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
178 bootdir
= os
. path
. join ( mnt
, 'boot' )
180 cmd_fname
= os
. path
. join ( bootdir
, 'boot.cmd' )
181 scr_fname
= os
. path
. join ( bootdir
, 'boot.scr' )
182 with
open ( cmd_fname
, 'w' ) as outf
:
183 print ( script
, file = outf
)
185 infname
= os
. path
. join ( cons
. config
. source_dir
,
186 'test/py/tests/bootstd/armbian.bmp.xz' )
187 bmp_file
= os
. path
. join ( bootdir
, 'boot.bmp' )
188 u_boot_utils
. run_and_log (
190 [ 'sh' , '-c' , f
'xz -dc {infname} > {bmp_file} ' ])
192 u_boot_utils
. run_and_log (
193 cons
, f
'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname} ' )
195 kernel
= 'vmlinuz-5.15.63-rockchip64'
196 target
= os
. path
. join ( bootdir
, kernel
)
197 with
open ( target
, 'wb' ) as outf
:
198 print ( 'kernel' , outf
)
200 symlink
= os
. path
. join ( bootdir
, 'Image' )
201 if os
. path
. exists ( symlink
):
203 u_boot_utils
. run_and_log (
204 cons
, f
'echo here {kernel} {symlink} ' )
205 os
. symlink ( kernel
, symlink
)
207 u_boot_utils
. run_and_log (
208 cons
, f
'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname} ' )
211 except ValueError as exc
:
212 print ( 'Falled to create image, failing back to prepared copy: %s ' ,
216 u_boot_utils
. run_and_log ( cons
, 'sudo umount --lazy %s ' % mnt
)
218 u_boot_utils
. run_and_log ( cons
, 'sudo losetup -d %s ' % loop
)
221 copy_prepared_image ( cons
, mmc_dev
, fname
)
223 def setup_bootflow_image ( cons
):
224 """Create a 20MB disk image with a single FAT partition"""
226 fname
, mnt
= setup_image ( cons
, mmc_dev
, 0xc , second_part
= True )
232 loop
= mount_image ( cons
, fname
, mnt
, 'vfat' )
235 vmlinux
= 'vmlinuz-5.3.7-301.fc31.armv7hl'
236 initrd
= 'initramfs-5.3.7-301.fc31.armv7hl.img'
237 dtbdir
= 'dtb-5.3.7-301.fc31.armv7hl'
238 script
= '''# extlinux.conf generated by appliance-creator
240 menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
241 menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
246 label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
248 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
250 initrd / %s ''' % ( vmlinux
, dtbdir
, initrd
)
251 ext
= os
. path
. join ( mnt
, 'extlinux' )
254 with
open ( os
. path
. join ( ext
, 'extlinux.conf' ), 'w' ) as fd
:
255 print ( script
, file = fd
)
257 inf
= os
. path
. join ( cons
. config
. persistent_data_dir
, 'inf' )
258 with
open ( inf
, 'wb' ) as fd
:
259 fd
. write ( gzip
. compress ( b
'vmlinux' ))
260 u_boot_utils
. run_and_log ( cons
, 'mkimage -f auto -d %s %s ' %
261 ( inf
, os
. path
. join ( mnt
, vmlinux
)))
263 with
open ( os
. path
. join ( mnt
, initrd
), 'w' ) as fd
:
264 print ( 'initrd' , file = fd
)
266 mkdir_cond ( os
. path
. join ( mnt
, dtbdir
))
268 dtb_file
= os
. path
. join ( mnt
, ' %s /sandbox.dtb' % dtbdir
)
269 u_boot_utils
. run_and_log (
270 cons
, 'dtc -o %s ' % dtb_file
, stdin
= b
'/dts-v1/; / {};' )
272 except ValueError as exc
:
273 print ( 'Falled to create image, failing back to prepared copy: %s ' ,
277 u_boot_utils
. run_and_log ( cons
, 'sudo umount --lazy %s ' % mnt
)
279 u_boot_utils
. run_and_log ( cons
, 'sudo losetup -d %s ' % loop
)
282 copy_prepared_image ( cons
, mmc_dev
, fname
)
285 @pytest . mark
. buildconfigspec ( 'ut_dm' )
286 def test_ut_dm_init ( u_boot_console
):
287 """Initialize data for ut dm tests."""
289 fn
= u_boot_console
. config
. source_dir
+ '/testflash.bin'
290 if not os
. path
. exists ( fn
):
291 data
= b
'this is a test'
292 data
+= b
' \x00 ' * (( 4 * 1024 * 1024 ) - len ( data
))
293 with
open ( fn
, 'wb' ) as fh
:
296 fn
= u_boot_console
. config
. source_dir
+ '/spi.bin'
297 if not os
. path
. exists ( fn
):
298 data
= b
' \x00 ' * ( 2 * 1024 * 1024 )
299 with
open ( fn
, 'wb' ) as fh
:
302 # Create a file with a single partition
303 fn
= u_boot_console
. config
. source_dir
+ '/scsi.img'
304 if not os
. path
. exists ( fn
):
305 data
= b
' \x00 ' * ( 2 * 1024 * 1024 )
306 with
open ( fn
, 'wb' ) as fh
:
308 u_boot_utils
. run_and_log (
309 u_boot_console
, f
'sfdisk {fn} ' , stdin
= b
'type=83' )
311 fs_helper
. mk_fs ( u_boot_console
. config
, 'ext2' , 0x200000 , '2MB' ,
313 fs_helper
. mk_fs ( u_boot_console
. config
, 'fat32' , 0x100000 , '1MB' ,
316 @pytest . mark
. buildconfigspec ( 'cmd_bootflow' )
317 def test_ut_dm_init_bootstd ( u_boot_console
):
318 """Initialise data for bootflow tests"""
320 setup_bootflow_image ( u_boot_console
)
321 setup_bootmenu_image ( u_boot_console
)
323 # Restart so that the new mmc1.img is picked up
324 u_boot_console
. restart_uboot ()
327 def test_ut ( u_boot_console
, ut_subtest
):
328 """Execute a "ut" subtest.
330 The subtests are collected in function generate_ut_subtest() from linker
331 generated lists by applying a regular expression to the lines of file
332 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
334 Strict naming conventions have to be followed to match the regular
335 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
336 test suite foo that can be executed via command 'ut foo bar' and is
337 implemented in C function foo_test_bar().
340 u_boot_console (ConsoleBase): U-Boot console
341 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
342 execute command 'ut foo bar'
345 output
= u_boot_console
. run_command ( 'ut ' + ut_subtest
)
346 assert output
. endswith ( 'Failures: 0' )