test_efi_fit.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2019, Cristian Ciocaltea <cristian.ciocaltea@gmail.com>
  3. #
  4. # Work based on:
  5. # - test_net.py
  6. # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  7. # - test_fit.py
  8. # Copyright (c) 2013, Google Inc.
  9. #
  10. # Test launching UEFI binaries from FIT images.
  11. """
  12. Note: This test relies on boardenv_* containing configuration values to define
  13. which network environment is available for testing. Without this, the parts
  14. that rely on network will be automatically skipped.
  15. For example:
  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. # Boolean indicating whether the Ethernet device is attached to PCI, and hence
  21. # PCI enumeration needs to be performed prior to network tests.
  22. # This variable may be omitted if its value is False.
  23. env__net_uses_pci = True
  24. # True if a DHCP server is attached to the network, and should be tested.
  25. # If DHCP testing is not possible or desired, this variable may be omitted or
  26. # set to False.
  27. env__net_dhcp_server = True
  28. # A list of environment variables that should be set in order to configure a
  29. # static IP. If solely relying on DHCP, this variable may be omitted or set to
  30. # an empty list.
  31. env__net_static_env_vars = [
  32. ('ipaddr', '10.0.0.100'),
  33. ('netmask', '255.255.255.0'),
  34. ('serverip', '10.0.0.1'),
  35. ]
  36. # Details regarding a file that may be read from a TFTP server. This variable
  37. # may be omitted or set to None if TFTP testing is not possible or desired.
  38. # Additionally, when the 'size' is not available, the file will be generated
  39. # automatically in the TFTP root directory, as specified by the 'dn' field.
  40. env__efi_fit_tftp_file = {
  41. 'fn': 'test-efi-fit.img', # File path relative to TFTP root
  42. 'size': 3831, # File size
  43. 'crc32': '9fa3f79c', # Checksum using CRC-32 algorithm, optional
  44. 'addr': 0x40400000, # Loading address, integer, optional
  45. 'dn': 'tftp/root/dir', # TFTP root directory path, optional
  46. }
  47. """
  48. import os.path
  49. import pytest
  50. import u_boot_utils as util
  51. # Define the parametrized ITS data to be used for FIT images generation.
  52. ITS_DATA = '''
  53. /dts-v1/;
  54. / {
  55. description = "EFI image with FDT blob";
  56. #address-cells = <1>;
  57. images {
  58. efi {
  59. description = "Test EFI";
  60. data = /incbin/("%(efi-bin)s");
  61. type = "%(kernel-type)s";
  62. arch = "%(sys-arch)s";
  63. os = "efi";
  64. compression = "%(efi-comp)s";
  65. load = <0x0>;
  66. entry = <0x0>;
  67. };
  68. fdt {
  69. description = "Test FDT";
  70. data = /incbin/("%(fdt-bin)s");
  71. type = "flat_dt";
  72. arch = "%(sys-arch)s";
  73. compression = "%(fdt-comp)s";
  74. };
  75. };
  76. configurations {
  77. default = "config-efi-fdt";
  78. config-efi-fdt {
  79. description = "EFI FIT w/ FDT";
  80. kernel = "efi";
  81. fdt = "fdt";
  82. };
  83. config-efi-nofdt {
  84. description = "EFI FIT w/o FDT";
  85. kernel = "efi";
  86. };
  87. };
  88. };
  89. '''
  90. # Define the parametrized FDT data to be used for DTB images generation.
  91. FDT_DATA = '''
  92. /dts-v1/;
  93. / {
  94. #address-cells = <1>;
  95. #size-cells = <1>;
  96. model = "%(sys-arch)s %(fdt_type)s EFI FIT Boot Test";
  97. compatible = "%(sys-arch)s";
  98. reset@0 {
  99. compatible = "%(sys-arch)s,reset";
  100. reg = <0 4>;
  101. };
  102. };
  103. '''
  104. @pytest.mark.buildconfigspec('bootm_efi')
  105. @pytest.mark.buildconfigspec('cmd_bootefi_hello_compile')
  106. @pytest.mark.buildconfigspec('fit')
  107. @pytest.mark.notbuildconfigspec('generate_acpi_table')
  108. @pytest.mark.requiredtool('dtc')
  109. def test_efi_fit_launch(u_boot_console):
  110. """Test handling of UEFI binaries inside FIT images.
  111. The tests are trying to launch U-Boot's helloworld.efi embedded into
  112. FIT images, in uncompressed or gzip compressed format.
  113. Additionally, a sample FDT blob is created and embedded into the above
  114. mentioned FIT images, in uncompressed or gzip compressed format.
  115. For more details, see launch_efi().
  116. The following test cases are currently defined and enabled:
  117. - Launch uncompressed FIT EFI & internal FDT
  118. - Launch uncompressed FIT EFI & FIT FDT
  119. - Launch compressed FIT EFI & internal FDT
  120. - Launch compressed FIT EFI & FIT FDT
  121. """
  122. def net_pre_commands():
  123. """Execute any commands required to enable network hardware.
  124. These commands are provided by the boardenv_* file; see the comment
  125. at the beginning of this file.
  126. """
  127. init_usb = cons.config.env.get('env__net_uses_usb', False)
  128. if init_usb:
  129. cons.run_command('usb start')
  130. init_pci = cons.config.env.get('env__net_uses_pci', False)
  131. if init_pci:
  132. cons.run_command('pci enum')
  133. def net_dhcp():
  134. """Execute the dhcp command.
  135. The boardenv_* file may be used to enable/disable DHCP; see the
  136. comment at the beginning of this file.
  137. """
  138. has_dhcp = cons.config.buildconfig.get('config_cmd_dhcp', 'n') == 'y'
  139. if not has_dhcp:
  140. cons.log.warning('CONFIG_CMD_DHCP != y: Skipping DHCP network setup')
  141. return False
  142. test_dhcp = cons.config.env.get('env__net_dhcp_server', False)
  143. if not test_dhcp:
  144. cons.log.info('No DHCP server available')
  145. return False
  146. cons.run_command('setenv autoload no')
  147. output = cons.run_command('dhcp')
  148. assert 'DHCP client bound to address ' in output
  149. return True
  150. def net_setup_static():
  151. """Set up a static IP configuration.
  152. The configuration is provided by the boardenv_* file; see the comment at
  153. the beginning of this file.
  154. """
  155. has_dhcp = cons.config.buildconfig.get('config_cmd_dhcp', 'n') == 'y'
  156. if not has_dhcp:
  157. cons.log.warning('CONFIG_NET != y: Skipping static network setup')
  158. return False
  159. env_vars = cons.config.env.get('env__net_static_env_vars', None)
  160. if not env_vars:
  161. cons.log.info('No static network configuration is defined')
  162. return False
  163. for (var, val) in env_vars:
  164. cons.run_command('setenv %s %s' % (var, val))
  165. return True
  166. def make_fpath(file_name):
  167. """Compute the path of a given (temporary) file.
  168. Args:
  169. file_name: The name of a file within U-Boot build dir.
  170. Return:
  171. The computed file path.
  172. """
  173. return os.path.join(cons.config.build_dir, file_name)
  174. def make_efi(fname, comp):
  175. """Create an UEFI binary.
  176. This simply copies lib/efi_loader/helloworld.efi into U-Boot
  177. build dir and, optionally, compresses the file using gzip.
  178. Args:
  179. fname: The target file name within U-Boot build dir.
  180. comp: Flag to enable gzip compression.
  181. Return:
  182. The path of the created file.
  183. """
  184. bin_path = make_fpath(fname)
  185. util.run_and_log(cons,
  186. ['cp', make_fpath('lib/efi_loader/helloworld.efi'),
  187. bin_path])
  188. if comp:
  189. util.run_and_log(cons, ['gzip', '-f', bin_path])
  190. bin_path += '.gz'
  191. return bin_path
  192. def make_dtb(fdt_type, comp):
  193. """Create a sample DTB file.
  194. Creates a DTS file and compiles it to a DTB.
  195. Args:
  196. fdt_type: The type of the FDT, i.e. internal, user.
  197. comp: Flag to enable gzip compression.
  198. Return:
  199. The path of the created file.
  200. """
  201. # Generate resources referenced by FDT.
  202. fdt_params = {
  203. 'sys-arch': sys_arch,
  204. 'fdt_type': fdt_type,
  205. }
  206. # Generate a test FDT file.
  207. dts = make_fpath('test-efi-fit-%s.dts' % fdt_type)
  208. with open(dts, 'w') as file:
  209. file.write(FDT_DATA % fdt_params)
  210. # Build the test FDT.
  211. dtb = make_fpath('test-efi-fit-%s.dtb' % fdt_type)
  212. util.run_and_log(cons, ['dtc', '-I', 'dts', '-O', 'dtb', '-o', dtb, dts])
  213. if comp:
  214. util.run_and_log(cons, ['gzip', '-f', dtb])
  215. dtb += '.gz'
  216. return dtb
  217. def make_fit(comp):
  218. """Create a sample FIT image.
  219. Runs 'mkimage' to create a FIT image within U-Boot build dir.
  220. Args:
  221. comp: Enable gzip compression for the EFI binary and FDT blob.
  222. Return:
  223. The path of the created file.
  224. """
  225. # Generate resources referenced by ITS.
  226. its_params = {
  227. 'sys-arch': sys_arch,
  228. 'efi-bin': os.path.basename(make_efi('test-efi-fit-helloworld.efi', comp)),
  229. 'kernel-type': 'kernel' if comp else 'kernel_noload',
  230. 'efi-comp': 'gzip' if comp else 'none',
  231. 'fdt-bin': os.path.basename(make_dtb('user', comp)),
  232. 'fdt-comp': 'gzip' if comp else 'none',
  233. }
  234. # Generate a test ITS file.
  235. its_path = make_fpath('test-efi-fit-helloworld.its')
  236. with open(its_path, 'w') as file:
  237. file.write(ITS_DATA % its_params)
  238. # Build the test ITS.
  239. fit_path = make_fpath('test-efi-fit-helloworld.fit')
  240. util.run_and_log(
  241. cons, [make_fpath('tools/mkimage'), '-f', its_path, fit_path])
  242. return fit_path
  243. def load_fit_from_host(fit):
  244. """Load the FIT image using the 'host load' command and return its address.
  245. Args:
  246. fit: Dictionary describing the FIT image to load, see env__efi_fit_test_file
  247. in the comment at the beginning of this file.
  248. Return:
  249. The address where the file has been loaded.
  250. """
  251. addr = fit.get('addr', None)
  252. if not addr:
  253. addr = util.find_ram_base(cons)
  254. output = cons.run_command(
  255. 'host load hostfs - %x %s/%s' % (addr, fit['dn'], fit['fn']))
  256. expected_text = ' bytes read'
  257. size = fit.get('size', None)
  258. if size:
  259. expected_text = '%d' % size + expected_text
  260. assert expected_text in output
  261. return addr
  262. def load_fit_from_tftp(fit):
  263. """Load the FIT image using the tftpboot command and return its address.
  264. The file is downloaded from the TFTP server, its size and optionally its
  265. CRC32 are validated.
  266. Args:
  267. fit: Dictionary describing the FIT image to load, see env__efi_fit_tftp_file
  268. in the comment at the beginning of this file.
  269. Return:
  270. The address where the file has been loaded.
  271. """
  272. addr = fit.get('addr', None)
  273. if not addr:
  274. addr = util.find_ram_base(cons)
  275. file_name = fit['fn']
  276. output = cons.run_command('tftpboot %x %s' % (addr, file_name))
  277. expected_text = 'Bytes transferred = '
  278. size = fit.get('size', None)
  279. if size:
  280. expected_text += '%d' % size
  281. assert expected_text in output
  282. expected_crc = fit.get('crc32', None)
  283. if not expected_crc:
  284. return addr
  285. if cons.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  286. return addr
  287. output = cons.run_command('crc32 $fileaddr $filesize')
  288. assert expected_crc in output
  289. return addr
  290. def launch_efi(enable_fdt, enable_comp):
  291. """Launch U-Boot's helloworld.efi binary from a FIT image.
  292. An external image file can be downloaded from TFTP, when related
  293. details are provided by the boardenv_* file; see the comment at the
  294. beginning of this file.
  295. If the size of the TFTP file is not provided within env__efi_fit_tftp_file,
  296. the test image is generated automatically and placed in the TFTP root
  297. directory specified via the 'dn' field.
  298. When running the tests on Sandbox, the image file is loaded directly
  299. from the host filesystem.
  300. Once the load address is available on U-Boot console, the 'bootm'
  301. command is executed for either 'config-efi-fdt' or 'config-efi-nofdt'
  302. FIT configuration, depending on the value of the 'enable_fdt' function
  303. argument.
  304. Eventually the 'Hello, world' message is expected in the U-Boot console.
  305. Args:
  306. enable_fdt: Flag to enable using the FDT blob inside FIT image.
  307. enable_comp: Flag to enable GZIP compression on EFI and FDT
  308. generated content.
  309. """
  310. with cons.log.section('FDT=%s;COMP=%s' % (enable_fdt, enable_comp)):
  311. if is_sandbox:
  312. fit = {
  313. 'dn': cons.config.build_dir,
  314. }
  315. else:
  316. # Init networking.
  317. net_pre_commands()
  318. net_set_up = net_dhcp()
  319. net_set_up = net_setup_static() or net_set_up
  320. if not net_set_up:
  321. pytest.skip('Network not initialized')
  322. fit = cons.config.env.get('env__efi_fit_tftp_file', None)
  323. if not fit:
  324. pytest.skip('No env__efi_fit_tftp_file binary specified in environment')
  325. size = fit.get('size', None)
  326. if not size:
  327. if not fit.get('dn', None):
  328. pytest.skip('Neither "size", nor "dn" info provided in env__efi_fit_tftp_file')
  329. # Create test FIT image.
  330. fit_path = make_fit(enable_comp)
  331. fit['fn'] = os.path.basename(fit_path)
  332. fit['size'] = os.path.getsize(fit_path)
  333. # Copy image to TFTP root directory.
  334. if fit['dn'] != cons.config.build_dir:
  335. util.run_and_log(cons, ['mv', '-f', fit_path, '%s/' % fit['dn']])
  336. # Load FIT image.
  337. addr = load_fit_from_host(fit) if is_sandbox else load_fit_from_tftp(fit)
  338. # Select boot configuration.
  339. fit_config = 'config-efi-fdt' if enable_fdt else 'config-efi-nofdt'
  340. # Try booting.
  341. output = cons.run_command('bootm %x#%s' % (addr, fit_config))
  342. if enable_fdt:
  343. assert 'Booting using the fdt blob' in output
  344. assert 'Hello, world' in output
  345. assert '## Application failed' not in output
  346. cons.restart_uboot()
  347. cons = u_boot_console
  348. # Array slice removes leading/trailing quotes.
  349. sys_arch = cons.config.buildconfig.get('config_sys_arch', '"sandbox"')[1:-1]
  350. is_sandbox = sys_arch == 'sandbox'
  351. try:
  352. if is_sandbox:
  353. # Use our own device tree file, will be restored afterwards.
  354. control_dtb = make_dtb('internal', False)
  355. old_dtb = cons.config.dtb
  356. cons.config.dtb = control_dtb
  357. # Run tests
  358. # - fdt OFF, gzip OFF
  359. launch_efi(False, False)
  360. # - fdt ON, gzip OFF
  361. launch_efi(True, False)
  362. if is_sandbox:
  363. # - fdt OFF, gzip ON
  364. launch_efi(False, True)
  365. # - fdt ON, gzip ON
  366. launch_efi(True, True)
  367. finally:
  368. if is_sandbox:
  369. # Go back to the original U-Boot with the correct dtb.
  370. cons.config.dtb = old_dtb
  371. cons.restart_uboot()