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