summaryrefslogtreecommitdiff
path: root/test/py/tests/fit_util.py
blob: 79718d431a0d5f3dd59ae3158fba5918df357b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# SPDX-License-Identifier: GPL-2.0+
# Copyright 2022 Google LLC

"""Common utility functions for FIT tests"""

import os

import u_boot_utils as util

def make_fname(cons, basename):
    """Make a temporary filename

    Args:
        cons (ConsoleBase): u_boot_console to use
        basename (str): Base name of file to create (within temporary directory)
    Return:
        Temporary filename
    """

    return os.path.join(cons.config.build_dir, basename)

def make_its(cons, base_its, params, basename='test.its'):
    """Make a sample .its file with parameters embedded

    Args:
        cons (ConsoleBase): u_boot_console to use
        base_its (str): Template text for the .its file, typically containing
            %() references
        params (dict of str): Parameters to embed in the %() strings
        basename (str): base name to write to (will be placed in the temp dir)
    Returns:
        str: Filename of .its file created
    """
    its = make_fname(cons, basename)
    with open(its, 'w', encoding='utf-8') as outf:
        print(base_its % params, file=outf)
    return its

def make_fit(cons, mkimage, base_its, params, basename='test.fit', base_fdt=None):
    """Make a sample .fit file ready for loading

    This creates a .its script with the selected parameters and uses mkimage to
    turn this into a .fit image.

    Args:
        cons (ConsoleBase): u_boot_console to use
        mkimage (str): Filename of 'mkimage' utility
        base_its (str): Template text for the .its file, typically containing
            %() references
        params (dict of str): Parameters to embed in the %() strings
        basename (str): base name to write to (will be placed in the temp dir)
    Return:
        Filename of .fit file created
    """
    fit = make_fname(cons, basename)
    its = make_its(cons, base_its, params)
    util.run_and_log(cons, [mkimage, '-f', its, fit])
    if base_fdt:
        with open(make_fname(cons, 'u-boot.dts'), 'w') as fd:
            fd.write(base_fdt)
    return fit

def make_kernel(cons, basename, text):
    """Make a sample kernel with test data

    Args:
        cons (ConsoleBase): u_boot_console to use
        basename (str): base name to write to (will be placed in the temp dir)
        text (str): Contents of the kernel file (will be repeated 100 times)
    Returns:
        str: Full path and filename of the kernel it created
    """
    fname = make_fname(cons, basename)
    data = ''
    for i in range(100):
        data += f'this {text} {i} is unlikely to boot\n'
    with open(fname, 'w', encoding='utf-8') as outf:
        print(data, file=outf)
    return fname

def make_dtb(cons, base_fdt, basename):
    """Make a sample .dts file and compile it to a .dtb

    Returns:
        cons (ConsoleBase): u_boot_console to use
        Filename of .dtb file created
    """
    src = make_fname(cons, f'{basename}.dts')
    dtb = make_fname(cons, f'{basename}.dtb')
    with open(src, 'w', encoding='utf-8') as outf:
        outf.write(base_fdt)
    util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
    return dtb