Simon Glass | a753190 | 2022-10-11 09:47:17 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright 2022 Google LLC |
| 3 | |
| 4 | """Common utility functions for FIT tests""" |
| 5 | |
| 6 | import os |
| 7 | |
| 8 | import u_boot_utils as util |
| 9 | |
| 10 | def 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 | |
| 22 | def 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 | |
Simon Glass | ae0bf22 | 2022-10-11 09:47:20 -0600 | [diff] [blame] | 39 | def make_fit(cons, mkimage, base_its, params, basename='test.fit', base_fdt=None): |
Simon Glass | a753190 | 2022-10-11 09:47:17 -0600 | [diff] [blame] | 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]) |
Simon Glass | ae0bf22 | 2022-10-11 09:47:20 -0600 | [diff] [blame] | 58 | if base_fdt: |
| 59 | with open(make_fname(cons, 'u-boot.dts'), 'w') as fd: |
| 60 | fd.write(base_fdt) |
Simon Glass | a753190 | 2022-10-11 09:47:17 -0600 | [diff] [blame] | 61 | return fit |
| 62 | |
| 63 | def make_kernel(cons, basename, text): |
| 64 | """Make a sample kernel with test data |
| 65 | |
| 66 | Args: |
| 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) |
| 70 | Returns: |
| 71 | str: Full path and filename of the kernel it created |
| 72 | """ |
| 73 | fname = make_fname(cons, basename) |
| 74 | data = '' |
| 75 | for i in range(100): |
| 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) |
| 79 | return fname |
| 80 | |
| 81 | def make_dtb(cons, base_fdt, basename): |
| 82 | """Make a sample .dts file and compile it to a .dtb |
| 83 | |
| 84 | Returns: |
| 85 | cons (ConsoleBase): u_boot_console to use |
| 86 | Filename of .dtb file created |
| 87 | """ |
| 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: |
| 91 | outf.write(base_fdt) |
| 92 | util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb]) |
| 93 | return dtb |