test_efi_loader.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # SPDX-License-Identifier: GPL-2.0
  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.
  6. # Test efi loader implementation
  7. import pytest
  8. import u_boot_utils
  9. """
  10. Note: This test relies on boardenv_* containing configuration values to define
  11. which network environment is available for testing. Without this, the parts
  12. that rely on network will be automatically skipped.
  13. For example:
  14. # Boolean indicating whether the Ethernet device is attached to USB, and hence
  15. # USB enumeration needs to be performed prior to network tests.
  16. # This variable may be omitted if its value is False.
  17. env__net_uses_usb = False
  18. # Boolean indicating whether the Ethernet device is attached to PCI, and hence
  19. # PCI enumeration needs to be performed prior to network tests.
  20. # This variable may be omitted if its value is False.
  21. env__net_uses_pci = True
  22. # True if a DHCP server is attached to the network, and should be tested.
  23. # If DHCP testing is not possible or desired, this variable may be omitted or
  24. # set to False.
  25. env__net_dhcp_server = True
  26. # A list of environment variables that should be set in order to configure a
  27. # static IP. If solely relying on DHCP, this variable may be omitted or set to
  28. # an empty list.
  29. env__net_static_env_vars = [
  30. ('ipaddr', '10.0.0.100'),
  31. ('netmask', '255.255.255.0'),
  32. ('serverip', '10.0.0.1'),
  33. ]
  34. # Details regarding a file that may be read from a TFTP server. This variable
  35. # may be omitted or set to None if TFTP testing is not possible or desired.
  36. env__efi_loader_helloworld_file = {
  37. 'fn': 'lib/efi_loader/helloworld.efi', # file name
  38. 'size': 5058624, # file length in bytes
  39. 'crc32': 'c2244b26', # CRC32 check sum
  40. 'addr': 0x40400000, # load address
  41. }
  42. """
  43. net_set_up = False
  44. def test_efi_pre_commands(u_boot_console):
  45. """Execute any commands required to enable network hardware.
  46. These commands are provided by the boardenv_* file; see the comment at the
  47. beginning of this file.
  48. """
  49. init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
  50. if init_usb:
  51. u_boot_console.run_command('usb start')
  52. init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
  53. if init_pci:
  54. u_boot_console.run_command('pci enum')
  55. @pytest.mark.buildconfigspec('cmd_dhcp')
  56. def test_efi_setup_dhcp(u_boot_console):
  57. """Set up the network using DHCP.
  58. The boardenv_* file may be used to enable/disable this test; see the
  59. comment at the beginning of this file.
  60. """
  61. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  62. if not test_dhcp:
  63. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  64. if not env_vars:
  65. pytest.skip('No DHCP server available')
  66. return None
  67. u_boot_console.run_command('setenv autoload no')
  68. output = u_boot_console.run_command('dhcp')
  69. assert 'DHCP client bound to address ' in output
  70. global net_set_up
  71. net_set_up = True
  72. @pytest.mark.buildconfigspec('net')
  73. def test_efi_setup_static(u_boot_console):
  74. """Set up the network using a static IP configuration.
  75. The configuration is provided by the boardenv_* file; see the comment at
  76. the beginning of this file.
  77. """
  78. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  79. if not env_vars:
  80. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  81. if not test_dhcp:
  82. pytest.skip('No static network configuration is defined')
  83. return None
  84. for (var, val) in env_vars:
  85. u_boot_console.run_command('setenv %s %s' % (var, val))
  86. global net_set_up
  87. net_set_up = True
  88. def fetch_tftp_file(u_boot_console, env_conf):
  89. """Grab an env described file via TFTP and return its address
  90. A file as described by an env config <env_conf> is downloaded from the TFTP
  91. server. The address to that file is returned.
  92. """
  93. if not net_set_up:
  94. pytest.skip('Network not initialized')
  95. f = u_boot_console.config.env.get(env_conf, None)
  96. if not f:
  97. pytest.skip('No %s binary specified in environment' % env_conf)
  98. addr = f.get('addr', None)
  99. if not addr:
  100. addr = u_boot_utils.find_ram_base(u_boot_console)
  101. fn = f['fn']
  102. output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
  103. expected_text = 'Bytes transferred = '
  104. sz = f.get('size', None)
  105. if sz:
  106. expected_text += '%d' % sz
  107. assert expected_text in output
  108. expected_crc = f.get('crc32', None)
  109. if not expected_crc:
  110. return addr
  111. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  112. return addr
  113. output = u_boot_console.run_command('crc32 %x $filesize' % addr)
  114. assert expected_crc in output
  115. return addr
  116. @pytest.mark.buildconfigspec('of_control')
  117. @pytest.mark.buildconfigspec('cmd_bootefi_hello_compile')
  118. def test_efi_helloworld_net(u_boot_console):
  119. """Run the helloworld.efi binary via TFTP.
  120. The helloworld.efi file is downloaded from the TFTP server and is executed
  121. using the fallback device tree at $fdtcontroladdr.
  122. """
  123. addr = fetch_tftp_file(u_boot_console, 'env__efi_loader_helloworld_file')
  124. output = u_boot_console.run_command('bootefi %x' % addr)
  125. expected_text = 'Hello, world'
  126. assert expected_text in output
  127. expected_text = '## Application failed'
  128. assert expected_text not in output
  129. @pytest.mark.buildconfigspec('cmd_bootefi_hello')
  130. def test_efi_helloworld_builtin(u_boot_console):
  131. """Run the builtin helloworld.efi binary.
  132. The helloworld.efi file is included in U-Boot, execute it using the
  133. special "bootefi hello" command.
  134. """
  135. output = u_boot_console.run_command('bootefi hello')
  136. expected_text = 'Hello, world'
  137. assert expected_text in output
  138. @pytest.mark.buildconfigspec('of_control')
  139. @pytest.mark.buildconfigspec('cmd_bootefi')
  140. def test_efi_grub_net(u_boot_console):
  141. """Run the grub.efi binary via TFTP.
  142. The grub.efi file is downloaded from the TFTP server and gets
  143. executed.
  144. """
  145. addr = fetch_tftp_file(u_boot_console, 'env__efi_loader_grub_file')
  146. u_boot_console.run_command('bootefi %x' % addr, wait_for_prompt=False)
  147. # Verify that we have an SMBIOS table
  148. check_smbios = u_boot_console.config.env.get('env__efi_loader_check_smbios', False)
  149. if check_smbios:
  150. u_boot_console.wait_for('grub>')
  151. output = u_boot_console.run_command('lsefisystab', wait_for_prompt=False, wait_for_echo=False)
  152. u_boot_console.wait_for('SMBIOS')
  153. # Then exit cleanly
  154. u_boot_console.wait_for('grub>')
  155. u_boot_console.run_command('exit', wait_for_prompt=False, wait_for_echo=False)
  156. u_boot_console.wait_for(u_boot_console.prompt)
  157. # And give us our U-Boot prompt back
  158. u_boot_console.run_command('')