1# SPDX-License-Identifier: GPL-2.0
2# Copyright 2023 Google LLC
3# Written by Simon Glass <sjg@chromium.org>
4
5"""Test handling of unmigrated u-boot,dm- tags"""
6
7import os
8import pytest
9
10import utils
11
12# This is needed for Azure, since the default '..' directory is not writeable
13TMPDIR1 = '/tmp/test_no_migrate'
14TMPDIR2 = '/tmp/test_no_migrate_spl'
15TMPDIR3 = '/tmp/test_migrate'
16
17def build_for_migrate(ubman, replace_pair, board, tmpdir, disable_migrate=True):
18    """Build an updated U-Boot with a slightly modified device tree
19
20    Args:
21        ubman (ConsoleBase): U-Boot console
22        replace_pair (tuple):
23            String to find
24            String to replace it with
25        board (str): Board to build
26        tmpdir (str): Temporary directory to use
27        disable_migrate (bool): True to disable CONFIG_OF_TAG_MIGRATE in build
28    """
29    srcdir = ubman.config.source_dir
30    build_dir = ubman.config.build_dir
31
32    # Get the source for the existing dts
33    dt_dir = os.path.join(build_dir, 'arch', 'sandbox', 'dts')
34    orig_fname = os.path.join(dt_dir, 'sandbox.dtb')
35    out_dts = os.path.join(dt_dir, 'sandbox_out.dts')
36    utils.run_and_log(ubman, ['dtc', orig_fname, '-I', 'dtb', '-O', 'dts',
37                             '-o', out_dts])
38
39    # Update it to use an old tag
40    with open(out_dts) as inf:
41        data = inf.read()
42    data = data.replace(*replace_pair)
43
44    dts_fname = os.path.join(dt_dir, 'sandbox_oldtag.dts')
45    with open(dts_fname, 'w') as outf:
46        print(data, file=outf)
47    dtb_fname = os.path.join(dt_dir, 'sandbox_oldtag.dtb')
48    utils.run_and_log(ubman, ['dtc', dts_fname, '-o', dtb_fname])
49
50    migrate = ['-a', '~CONFIG_OF_TAG_MIGRATE'] if disable_migrate else []
51
52    # Build sandbox with this new dtb, turning off OF_TAG_MIGRATE
53    env = dict(os.environ)
54    env['EXT_DTB'] = dtb_fname
55    env['DEVICE_TREE'] = 'sandbox_new'
56    env['NO_LTO'] = '1'  # Speed up build
57    out = utils.run_and_log(
58        ubman, ['./tools/buildman/buildman', '-m', '--board', board,
59               *migrate, '-w', '-o', tmpdir], ignore_errors=True, env=env)
60    return out
61
62@pytest.mark.slow
63@pytest.mark.boardspec('sandbox')
64def test_of_no_migrate(ubman):
65    """Test sandbox with old boot phase tags like u-boot,dm-pre-proper"""
66
67    build_for_migrate(ubman, ['bootph-some-ram', 'u-boot,dm-pre-proper'],
68                      'sandbox', TMPDIR1)
69
70    # It should fail to run, since the lcd device will not be bound before
71    # relocation. so won't get its frame-buffer memory
72    out = utils.run_and_log(
73        ubman, [os.path.join(TMPDIR1, 'u-boot'), '-D', '-c', 'help'],
74        ignore_errors=True)
75    assert "Video device 'lcd' cannot allocate frame buffer memory" in out
76
77
78@pytest.mark.slow
79@pytest.mark.boardspec('sandbox_spl')
80@pytest.mark.boardspec('spl_of_platdata_inst')
81@pytest.mark.boardspec('!sandbox_tpl')
82def test_of_no_migrate_spl(ubman):
83    """Test sandbox with old boot phase tags like u-boot,dm-spl"""
84
85    out = build_for_migrate(ubman, ['bootph-pre-ram', 'u-boot,dm-spl'],
86                            'sandbox_spl', TMPDIR2)
87
88    # It should fail to build, since the SPL DT will not include 'spl-test'
89    # node, among others
90    assert "undefined type ‘struct dtd_sandbox_spl_test’" in out
91
92
93@pytest.mark.slow
94@pytest.mark.boardspec('sandbox')
95def test_of_migrate(ubman):
96    """Test sandbox shows a message when tags were migrated"""
97
98    build_for_migrate(ubman, ['bootph-some-ram', 'u-boot,dm-pre-proper'],
99                      'sandbox', TMPDIR3, disable_migrate=False)
100
101    # It should show a migration message
102    out = utils.run_and_log(
103        ubman, [os.path.join(TMPDIR3, 'u-boot'), '-D', '-c', 'help'],
104        ignore_errors=True)
105    assert "Warning: Device tree includes old 'u-boot,dm-' tags" in out
106