1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright 2022 Google LLC
4 """Common utility functions for FIT tests"""
8 import u_boot_utils as util
10 def make_fname(cons, basename):
11 """Make a temporary filename
14 cons (ConsoleBase): u_boot_console to use
15 basename (str): Base name of file to create (within temporary directory)
20 return os.path.join(cons.config.build_dir, basename)
22 def make_its(cons, base_its, params, basename='test.its'):
23 """Make a sample .its file with parameters embedded
26 cons (ConsoleBase): u_boot_console to use
27 base_its (str): Template text for the .its file, typically containing
29 params (dict of str): Parameters to embed in the %() strings
30 basename (str): base name to write to (will be placed in the temp dir)
32 str: Filename of .its file created
34 its = make_fname(cons, basename)
35 with open(its, 'w', encoding='utf-8') as outf:
36 print(base_its % params, file=outf)
39 def make_fit(cons, mkimage, base_its, params, basename='test.fit', base_fdt=None):
40 """Make a sample .fit file ready for loading
42 This creates a .its script with the selected parameters and uses mkimage to
43 turn this into a .fit image.
46 cons (ConsoleBase): u_boot_console to use
47 mkimage (str): Filename of 'mkimage' utility
48 base_its (str): Template text for the .its file, typically containing
50 params (dict of str): Parameters to embed in the %() strings
51 basename (str): base name to write to (will be placed in the temp dir)
53 Filename of .fit file created
55 fit = make_fname(cons, basename)
56 its = make_its(cons, base_its, params)
57 util.run_and_log(cons, [mkimage, '-f', its, fit])
59 with open(make_fname(cons, 'u-boot.dts'), 'w') as fd:
63 def make_kernel(cons, basename, text):
64 """Make a sample kernel with test data
67 cons (ConsoleBase): u_boot_console to use
68 basename (str): base name to write to (will be placed in the temp dir)
69 text (str): Contents of the kernel file (will be repeated 100 times)
71 str: Full path and filename of the kernel it created
73 fname = make_fname(cons, basename)
76 data += f'this {text} {i} is unlikely to boot\n'
77 with open(fname, 'w', encoding='utf-8') as outf:
78 print(data, file=outf)
81 def make_dtb(cons, base_fdt, basename):
82 """Make a sample .dts file and compile it to a .dtb
85 cons (ConsoleBase): u_boot_console to use
86 Filename of .dtb file created
88 src = make_fname(cons, f'{basename}.dts')
89 dtb = make_fname(cons, f'{basename}.dtb')
90 with open(src, 'w', encoding='utf-8') as outf:
92 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])