test_efi_loader.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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',
  38. 'size': 5058624,
  39. 'crc32': 'c2244b26',
  40. }
  41. """
  42. net_set_up = False
  43. def test_efi_pre_commands(u_boot_console):
  44. """Execute any commands required to enable network hardware.
  45. These commands are provided by the boardenv_* file; see the comment at the
  46. beginning of this file.
  47. """
  48. init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
  49. if init_usb:
  50. u_boot_console.run_command('usb start')
  51. init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
  52. if init_pci:
  53. u_boot_console.run_command('pci enum')
  54. @pytest.mark.buildconfigspec('cmd_dhcp')
  55. def test_efi_dhcp(u_boot_console):
  56. """Test the dhcp command.
  57. The boardenv_* file may be used to enable/disable this test; see the
  58. comment at the beginning of this file.
  59. """
  60. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  61. if not test_dhcp:
  62. pytest.skip('No DHCP server available')
  63. u_boot_console.run_command('setenv autoload no')
  64. output = u_boot_console.run_command('dhcp')
  65. assert 'DHCP client bound to address ' in output
  66. global net_set_up
  67. net_set_up = True
  68. @pytest.mark.buildconfigspec('net')
  69. def test_efi_setup_static(u_boot_console):
  70. """Set up a static IP configuration.
  71. The configuration is provided by the boardenv_* file; see the comment at
  72. the beginning of this file.
  73. """
  74. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  75. if not env_vars:
  76. pytest.skip('No static network configuration is defined')
  77. for (var, val) in env_vars:
  78. u_boot_console.run_command('setenv %s %s' % (var, val))
  79. global net_set_up
  80. net_set_up = True
  81. def fetch_tftp_file(u_boot_console, env_conf):
  82. """Grab an env described file via TFTP and return its address
  83. A file as described by an env config <env_conf> is downloaded from the TFTP
  84. server. The address to that file is returned.
  85. """
  86. if not net_set_up:
  87. pytest.skip('Network not initialized')
  88. f = u_boot_console.config.env.get(env_conf, None)
  89. if not f:
  90. pytest.skip('No %s binary specified in environment' % env_conf)
  91. addr = f.get('addr', None)
  92. if not addr:
  93. addr = u_boot_utils.find_ram_base(u_boot_console)
  94. fn = f['fn']
  95. output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
  96. expected_text = 'Bytes transferred = '
  97. sz = f.get('size', None)
  98. if sz:
  99. expected_text += '%d' % sz
  100. assert expected_text in output
  101. expected_crc = f.get('crc32', None)
  102. if not expected_crc:
  103. return addr
  104. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  105. return addr
  106. output = u_boot_console.run_command('crc32 %x $filesize' % addr)
  107. assert expected_crc in output
  108. return addr
  109. @pytest.mark.buildconfigspec('cmd_bootefi_hello_compile')
  110. def test_efi_helloworld_net(u_boot_console):
  111. """Run the helloworld.efi binary via TFTP.
  112. The helloworld.efi file is downloaded from the TFTP server and gets
  113. executed.
  114. """
  115. addr = fetch_tftp_file(u_boot_console, 'env__efi_loader_helloworld_file')
  116. output = u_boot_console.run_command('bootefi %x' % addr)
  117. expected_text = 'Hello, world'
  118. assert expected_text in output
  119. expected_text = '## Application terminated, r = 0'
  120. assert expected_text in output
  121. @pytest.mark.buildconfigspec('cmd_bootefi_hello')
  122. def test_efi_helloworld_builtin(u_boot_console):
  123. """Run the builtin helloworld.efi binary.
  124. The helloworld.efi file is included in U-Boot, execute it using the
  125. special "bootefi hello" command.
  126. """
  127. output = u_boot_console.run_command('bootefi hello')
  128. expected_text = 'Hello, world'
  129. assert expected_text in output
  130. @pytest.mark.buildconfigspec('cmd_bootefi')
  131. def test_efi_grub_net(u_boot_console):
  132. """Run the grub.efi binary via TFTP.
  133. The grub.efi file is downloaded from the TFTP server and gets
  134. executed.
  135. """
  136. addr = fetch_tftp_file(u_boot_console, 'env__efi_loader_grub_file')
  137. u_boot_console.run_command('bootefi %x' % addr, wait_for_prompt=False)
  138. # Verify that we have an SMBIOS table
  139. check_smbios = u_boot_console.config.env.get('env__efi_loader_check_smbios', False)
  140. if check_smbios:
  141. u_boot_console.wait_for('grub>')
  142. output = u_boot_console.run_command('lsefisystab', wait_for_prompt=False, wait_for_echo=False)
  143. u_boot_console.wait_for('SMBIOS')
  144. # Then exit cleanly
  145. u_boot_console.wait_for('grub>')
  146. output = u_boot_console.run_command('exit', wait_for_prompt=False, wait_for_echo=False)
  147. u_boot_console.wait_for('r = 0')
  148. # And give us our U-Boot prompt back
  149. u_boot_console.run_command('')