blob: fcec4c43c3c1114cf1c370a40dd0025fd649acde [file] [log] [blame]
Simon Glass1b035c12022-10-11 09:47:17 -06001# SPDX-License-Identifier: GPL-2.0+
2# Copyright 2022 Google LLC
3
4"""Common utility functions for FIT tests"""
5
6import os
7
8import u_boot_utils as util
9
10def make_fname(cons, basename):
11 """Make a temporary filename
12
13 Args:
14 cons (ConsoleBase): u_boot_console to use
15 basename (str): Base name of file to create (within temporary directory)
16 Return:
17 Temporary filename
18 """
19
20 return os.path.join(cons.config.build_dir, basename)
21
22def make_its(cons, base_its, params, basename='test.its'):
23 """Make a sample .its file with parameters embedded
24
25 Args:
26 cons (ConsoleBase): u_boot_console to use
27 base_its (str): Template text for the .its file, typically containing
28 %() references
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)
31 Returns:
32 str: Filename of .its file created
33 """
34 its = make_fname(cons, basename)
35 with open(its, 'w', encoding='utf-8') as outf:
36 print(base_its % params, file=outf)
37 return its
38
39def make_fit(cons, mkimage, base_its, params, basename='test.fit'):
40 """Make a sample .fit file ready for loading
41
42 This creates a .its script with the selected parameters and uses mkimage to
43 turn this into a .fit image.
44
45 Args:
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
49 %() references
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)
52 Return:
53 Filename of .fit file created
54 """
55 fit = make_fname(cons, basename)
56 its = make_its(cons, base_its, params)
57 util.run_and_log(cons, [mkimage, '-f', its, fit])
58 return fit
59
60def make_kernel(cons, basename, text):
61 """Make a sample kernel with test data
62
63 Args:
64 cons (ConsoleBase): u_boot_console to use
65 basename (str): base name to write to (will be placed in the temp dir)
66 text (str): Contents of the kernel file (will be repeated 100 times)
67 Returns:
68 str: Full path and filename of the kernel it created
69 """
70 fname = make_fname(cons, basename)
71 data = ''
72 for i in range(100):
73 data += f'this {text} {i} is unlikely to boot\n'
74 with open(fname, 'w', encoding='utf-8') as outf:
75 print(data, file=outf)
76 return fname
77
78def make_dtb(cons, base_fdt, basename):
79 """Make a sample .dts file and compile it to a .dtb
80
81 Returns:
82 cons (ConsoleBase): u_boot_console to use
83 Filename of .dtb file created
84 """
85 src = make_fname(cons, f'{basename}.dts')
86 dtb = make_fname(cons, f'{basename}.dtb')
87 with open(src, 'w', encoding='utf-8') as outf:
88 outf.write(base_fdt)
89 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
90 return dtb