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 u_boot_utils as util
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(cons, replace_pair, board, tmpdir, disable_migrate=True):
18    """Build an updated U-Boot with a slightly modified device tree
19
20    Args:
21        cons (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 = cons.config.source_dir
30    build_dir = cons.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    util.run_and_log(cons, ['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    util.run_and_log(cons, ['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 = util.run_and_log(
58        cons, ['./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(u_boot_console):
65    """Test sandbox with old boot phase tags like u-boot,dm-pre-proper"""
66    cons = u_boot_console
67
68    build_for_migrate(cons, ['bootph-some-ram', 'u-boot,dm-pre-proper'],
69                      'sandbox', TMPDIR1)
70
71    # It should fail to run, since the lcd device will not be bound before
72    # relocation. so won't get its frame-buffer memory
73    out = util.run_and_log(
74        cons, [os.path.join(TMPDIR1, 'u-boot'), '-D', '-c', 'help'],
75        ignore_errors=True)
76    assert "Video device 'lcd' cannot allocate frame buffer memory" in out
77
78
79@pytest.mark.slow
80@pytest.mark.boardspec('sandbox_spl')
81@pytest.mark.boardspec('spl_of_platdata_inst')
82@pytest.mark.boardspec('!sandbox_tpl')
83def test_of_no_migrate_spl(u_boot_console):
84    """Test sandbox with old boot phase tags like u-boot,dm-spl"""
85    cons = u_boot_console
86
87    out = build_for_migrate(cons, ['bootph-pre-ram', 'u-boot,dm-spl'],
88                            'sandbox_spl', TMPDIR2)
89
90    # It should fail to build, since the SPL DT will not include 'spl-test'
91    # node, among others
92    assert "undefined type ‘struct dtd_sandbox_spl_test’" in out
93
94
95@pytest.mark.slow
96@pytest.mark.boardspec('sandbox')
97def test_of_migrate(u_boot_console):
98    """Test sandbox shows a message when tags were migrated"""
99    cons = u_boot_console
100
101    build_for_migrate(cons, ['bootph-some-ram', 'u-boot,dm-pre-proper'],
102                      'sandbox', TMPDIR3, disable_migrate=False)
103
104    # It should show a migration message
105    out = util.run_and_log(
106        cons, [os.path.join(TMPDIR3, 'u-boot'), '-D', '-c', 'help'],
107        ignore_errors=True)
108    assert "Warning: Device tree includes old 'u-boot,dm-' tags" in out
109