test_efi_loader.py 6.2 KB

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