Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 2 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
| 3 | # Copyright (c) 2016, Alexander Graf <agraf@suse.de> |
| 4 | # |
| 5 | # based on test_net.py. |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 6 | |
| 7 | # Test efi loader implementation |
| 8 | |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 9 | """ |
| 10 | Note: This test relies on boardenv_* containing configuration values to define |
Heinrich Schuchardt | 844b66a | 2017-10-18 18:13:18 +0200 | [diff] [blame] | 11 | which network environment is available for testing. Without this, the parts |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 12 | that rely on network will be automatically skipped. |
| 13 | |
| 14 | For example: |
| 15 | |
| 16 | # Boolean indicating whether the Ethernet device is attached to USB, and hence |
| 17 | # USB enumeration needs to be performed prior to network tests. |
| 18 | # This variable may be omitted if its value is False. |
| 19 | env__net_uses_usb = False |
| 20 | |
| 21 | # Boolean indicating whether the Ethernet device is attached to PCI, and hence |
| 22 | # PCI enumeration needs to be performed prior to network tests. |
| 23 | # This variable may be omitted if its value is False. |
| 24 | env__net_uses_pci = True |
| 25 | |
| 26 | # True if a DHCP server is attached to the network, and should be tested. |
| 27 | # If DHCP testing is not possible or desired, this variable may be omitted or |
| 28 | # set to False. |
| 29 | env__net_dhcp_server = True |
| 30 | |
| 31 | # A list of environment variables that should be set in order to configure a |
| 32 | # static IP. If solely relying on DHCP, this variable may be omitted or set to |
| 33 | # an empty list. |
| 34 | env__net_static_env_vars = [ |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 35 | ('ipaddr', '10.0.0.100'), |
| 36 | ('netmask', '255.255.255.0'), |
| 37 | ('serverip', '10.0.0.1'), |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 38 | ] |
| 39 | |
| 40 | # Details regarding a file that may be read from a TFTP server. This variable |
| 41 | # may be omitted or set to None if TFTP testing is not possible or desired. |
| 42 | env__efi_loader_helloworld_file = { |
Heinrich Schuchardt | 0e5f653 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 43 | 'fn': 'lib/efi_loader/helloworld.efi', # file name |
| 44 | 'size': 5058624, # file length in bytes |
| 45 | 'crc32': 'c2244b26', # CRC32 check sum |
| 46 | 'addr': 0x40400000, # load address |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 47 | } |
| 48 | """ |
| 49 | |
Heinrich Schuchardt | 9460c18 | 2021-11-22 08:24:08 +0100 | [diff] [blame] | 50 | import pytest |
| 51 | import u_boot_utils |
| 52 | |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 53 | net_set_up = False |
| 54 | |
| 55 | def test_efi_pre_commands(u_boot_console): |
| 56 | """Execute any commands required to enable network hardware. |
| 57 | |
| 58 | These commands are provided by the boardenv_* file; see the comment at the |
| 59 | beginning of this file. |
| 60 | """ |
| 61 | |
| 62 | init_usb = u_boot_console.config.env.get('env__net_uses_usb', False) |
| 63 | if init_usb: |
| 64 | u_boot_console.run_command('usb start') |
| 65 | |
| 66 | init_pci = u_boot_console.config.env.get('env__net_uses_pci', False) |
| 67 | if init_pci: |
| 68 | u_boot_console.run_command('pci enum') |
| 69 | |
| 70 | @pytest.mark.buildconfigspec('cmd_dhcp') |
Heinrich Schuchardt | 5ecb53a | 2020-07-13 12:22:23 +0200 | [diff] [blame] | 71 | def test_efi_setup_dhcp(u_boot_console): |
| 72 | """Set up the network using DHCP. |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 73 | |
| 74 | The boardenv_* file may be used to enable/disable this test; see the |
| 75 | comment at the beginning of this file. |
| 76 | """ |
| 77 | |
| 78 | test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False) |
| 79 | if not test_dhcp: |
Heinrich Schuchardt | 5ecb53a | 2020-07-13 12:22:23 +0200 | [diff] [blame] | 80 | env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None) |
| 81 | if not env_vars: |
| 82 | pytest.skip('No DHCP server available') |
Heinrich Schuchardt | 9460c18 | 2021-11-22 08:24:08 +0100 | [diff] [blame] | 83 | return |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 84 | |
| 85 | u_boot_console.run_command('setenv autoload no') |
| 86 | output = u_boot_console.run_command('dhcp') |
| 87 | assert 'DHCP client bound to address ' in output |
| 88 | |
| 89 | global net_set_up |
| 90 | net_set_up = True |
| 91 | |
| 92 | @pytest.mark.buildconfigspec('net') |
| 93 | def test_efi_setup_static(u_boot_console): |
Heinrich Schuchardt | 5ecb53a | 2020-07-13 12:22:23 +0200 | [diff] [blame] | 94 | """Set up the network using a static IP configuration. |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 95 | |
| 96 | The configuration is provided by the boardenv_* file; see the comment at |
| 97 | the beginning of this file. |
| 98 | """ |
| 99 | |
| 100 | env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None) |
| 101 | if not env_vars: |
Heinrich Schuchardt | 5ecb53a | 2020-07-13 12:22:23 +0200 | [diff] [blame] | 102 | test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False) |
| 103 | if not test_dhcp: |
| 104 | pytest.skip('No static network configuration is defined') |
| 105 | return None |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 106 | |
| 107 | for (var, val) in env_vars: |
| 108 | u_boot_console.run_command('setenv %s %s' % (var, val)) |
| 109 | |
| 110 | global net_set_up |
| 111 | net_set_up = True |
| 112 | |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 113 | def fetch_tftp_file(u_boot_console, env_conf): |
| 114 | """Grab an env described file via TFTP and return its address |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 115 | |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 116 | A file as described by an env config <env_conf> is downloaded from the TFTP |
| 117 | server. The address to that file is returned. |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 118 | """ |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 119 | if not net_set_up: |
| 120 | pytest.skip('Network not initialized') |
| 121 | |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 122 | f = u_boot_console.config.env.get(env_conf, None) |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 123 | if not f: |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 124 | pytest.skip('No %s binary specified in environment' % env_conf) |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 125 | |
| 126 | addr = f.get('addr', None) |
| 127 | if not addr: |
Quentin Schulz | dae1aa8 | 2018-07-09 19:16:27 +0200 | [diff] [blame] | 128 | addr = u_boot_utils.find_ram_base(u_boot_console) |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 129 | |
| 130 | fn = f['fn'] |
| 131 | output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn)) |
| 132 | expected_text = 'Bytes transferred = ' |
| 133 | sz = f.get('size', None) |
| 134 | if sz: |
| 135 | expected_text += '%d' % sz |
| 136 | assert expected_text in output |
| 137 | |
| 138 | expected_crc = f.get('crc32', None) |
| 139 | if not expected_crc: |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 140 | return addr |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 141 | |
| 142 | if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y': |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 143 | return addr |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 144 | |
| 145 | output = u_boot_console.run_command('crc32 %x $filesize' % addr) |
| 146 | assert expected_crc in output |
| 147 | |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 148 | return addr |
| 149 | |
Heinrich Schuchardt | f8ce185 | 2020-03-28 10:41:20 +0100 | [diff] [blame] | 150 | @pytest.mark.buildconfigspec('of_control') |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 151 | @pytest.mark.buildconfigspec('cmd_bootefi_hello_compile') |
| 152 | def test_efi_helloworld_net(u_boot_console): |
| 153 | """Run the helloworld.efi binary via TFTP. |
| 154 | |
Heinrich Schuchardt | f8ce185 | 2020-03-28 10:41:20 +0100 | [diff] [blame] | 155 | The helloworld.efi file is downloaded from the TFTP server and is executed |
| 156 | using the fallback device tree at $fdtcontroladdr. |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 157 | """ |
| 158 | |
| 159 | addr = fetch_tftp_file(u_boot_console, 'env__efi_loader_helloworld_file') |
| 160 | |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 161 | output = u_boot_console.run_command('bootefi %x' % addr) |
| 162 | expected_text = 'Hello, world' |
| 163 | assert expected_text in output |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 164 | expected_text = '## Application failed' |
| 165 | assert expected_text not in output |
Alexander Graf | 343cccb | 2016-11-17 18:31:05 +0100 | [diff] [blame] | 166 | |
| 167 | @pytest.mark.buildconfigspec('cmd_bootefi_hello') |
| 168 | def test_efi_helloworld_builtin(u_boot_console): |
| 169 | """Run the builtin helloworld.efi binary. |
| 170 | |
| 171 | The helloworld.efi file is included in U-Boot, execute it using the |
| 172 | special "bootefi hello" command. |
| 173 | """ |
| 174 | |
| 175 | output = u_boot_console.run_command('bootefi hello') |
| 176 | expected_text = 'Hello, world' |
| 177 | assert expected_text in output |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 178 | |
Heinrich Schuchardt | 783151e | 2020-03-30 20:27:42 +0200 | [diff] [blame] | 179 | @pytest.mark.buildconfigspec('of_control') |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 180 | @pytest.mark.buildconfigspec('cmd_bootefi') |
| 181 | def test_efi_grub_net(u_boot_console): |
| 182 | """Run the grub.efi binary via TFTP. |
| 183 | |
| 184 | The grub.efi file is downloaded from the TFTP server and gets |
| 185 | executed. |
| 186 | """ |
| 187 | |
| 188 | addr = fetch_tftp_file(u_boot_console, 'env__efi_loader_grub_file') |
| 189 | |
| 190 | u_boot_console.run_command('bootefi %x' % addr, wait_for_prompt=False) |
| 191 | |
| 192 | # Verify that we have an SMBIOS table |
| 193 | check_smbios = u_boot_console.config.env.get('env__efi_loader_check_smbios', False) |
| 194 | if check_smbios: |
| 195 | u_boot_console.wait_for('grub>') |
Heinrich Schuchardt | 9460c18 | 2021-11-22 08:24:08 +0100 | [diff] [blame] | 196 | u_boot_console.run_command('lsefisystab', wait_for_prompt=False, wait_for_echo=False) |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 197 | u_boot_console.wait_for('SMBIOS') |
| 198 | |
| 199 | # Then exit cleanly |
| 200 | u_boot_console.wait_for('grub>') |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 201 | u_boot_console.run_command('exit', wait_for_prompt=False, wait_for_echo=False) |
Heinrich Schuchardt | fdf52c7 | 2020-07-24 20:55:37 +0200 | [diff] [blame] | 202 | u_boot_console.wait_for(u_boot_console.prompt) |
Alexander Graf | d558629 | 2016-11-18 13:18:00 +0100 | [diff] [blame] | 203 | # And give us our U-Boot prompt back |
| 204 | u_boot_console.run_command('') |