1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2019, Cristian Ciocaltea <cristian.ciocaltea@gmail.com>
6 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
8 # Copyright (c) 2013, Google Inc.
10 # Test launching UEFI binaries from FIT images.
13 Note: This test relies on boardenv_* containing configuration values to define
14 which network environment is available for testing. Without this, the parts
15 that rely on network will be automatically skipped.
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
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
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
32 env__net_dhcp_server = True
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
37 env__net_static_env_vars = [
38 ('ipaddr', '10.0.0.100'),
39 ('netmask', '255.255.255.0'),
40 ('serverip', '10.0.0.1'),
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 # Additionally, when the 'size' is not available, the file will be generated
46 # automatically in the TFTP root directory, as specified by the 'dn' field.
47 env__efi_fit_tftp_file = {
48 'fn': 'test-efi-fit.img', # File path relative to TFTP root
49 'size': 3831, # File size
50 'crc32': '9fa3f79c', # Checksum using CRC-32 algorithm, optional
51 'addr': 0x40400000, # Loading address, integer, optional
52 'dn': 'tftp/root/dir', # TFTP root directory path, optional
58 import u_boot_utils as util
60 # Define the parametrized ITS data to be used for FIT images generation.
65 description = "EFI image with FDT blob";
70 description = "Test EFI";
71 data = /incbin/("%(efi-bin)s");
72 type = "%(kernel-type)s";
73 arch = "%(sys-arch)s";
75 compression = "%(efi-comp)s";
80 description = "Test FDT";
81 data = /incbin/("%(fdt-bin)s");
83 arch = "%(sys-arch)s";
84 compression = "%(fdt-comp)s";
89 default = "config-efi-fdt";
91 description = "EFI FIT w/ FDT";
96 description = "EFI FIT w/o FDT";
103 # Define the parametrized FDT data to be used for DTB images generation.
108 #address-cells = <1>;
111 model = "%(sys-arch)s %(fdt_type)s EFI FIT Boot Test";
112 compatible = "%(sys-arch)s";
115 compatible = "%(sys-arch)s,reset";
121 @pytest.mark.buildconfigspec('bootm_efi')
122 @pytest.mark.buildconfigspec('cmd_bootefi_hello_compile')
123 @pytest.mark.buildconfigspec('fit')
124 @pytest.mark.notbuildconfigspec('generate_acpi_table')
125 @pytest.mark.requiredtool('dtc')
126 def test_efi_fit_launch(u_boot_console):
127 """Test handling of UEFI binaries inside FIT images.
129 The tests are trying to launch U-Boot's helloworld.efi embedded into
130 FIT images, in uncompressed or gzip compressed format.
132 Additionally, a sample FDT blob is created and embedded into the above
133 mentioned FIT images, in uncompressed or gzip compressed format.
135 For more details, see launch_efi().
137 The following test cases are currently defined and enabled:
138 - Launch uncompressed FIT EFI & internal FDT
139 - Launch uncompressed FIT EFI & FIT FDT
140 - Launch compressed FIT EFI & internal FDT
141 - Launch compressed FIT EFI & FIT FDT
144 def net_pre_commands():
145 """Execute any commands required to enable network hardware.
147 These commands are provided by the boardenv_* file; see the comment
148 at the beginning of this file.
151 init_usb = cons.config.env.get('env__net_uses_usb', False)
153 cons.run_command('usb start')
155 init_pci = cons.config.env.get('env__net_uses_pci', False)
157 cons.run_command('pci enum')
160 """Execute the dhcp command.
162 The boardenv_* file may be used to enable/disable DHCP; see the
163 comment at the beginning of this file.
166 has_dhcp = cons.config.buildconfig.get('config_cmd_dhcp', 'n') == 'y'
168 cons.log.warning('CONFIG_CMD_DHCP != y: Skipping DHCP network setup')
171 test_dhcp = cons.config.env.get('env__net_dhcp_server', False)
173 cons.log.info('No DHCP server available')
176 cons.run_command('setenv autoload no')
177 output = cons.run_command('dhcp')
178 assert 'DHCP client bound to address ' in output
181 def net_setup_static():
182 """Set up a static IP configuration.
184 The configuration is provided by the boardenv_* file; see the comment at
185 the beginning of this file.
188 has_dhcp = cons.config.buildconfig.get('config_cmd_dhcp', 'n') == 'y'
190 cons.log.warning('CONFIG_NET != y: Skipping static network setup')
193 env_vars = cons.config.env.get('env__net_static_env_vars', None)
195 cons.log.info('No static network configuration is defined')
198 for (var, val) in env_vars:
199 cons.run_command('setenv %s %s' % (var, val))
202 def make_fpath(file_name):
203 """Compute the path of a given (temporary) file.
206 file_name: The name of a file within U-Boot build dir.
208 The computed file path.
211 return os.path.join(cons.config.build_dir, file_name)
213 def make_efi(fname, comp):
214 """Create an UEFI binary.
216 This simply copies lib/efi_loader/helloworld.efi into U-Boot
217 build dir and, optionally, compresses the file using gzip.
220 fname: The target file name within U-Boot build dir.
221 comp: Flag to enable gzip compression.
223 The path of the created file.
226 bin_path = make_fpath(fname)
227 util.run_and_log(cons,
228 ['cp', make_fpath('lib/efi_loader/helloworld.efi'),
231 util.run_and_log(cons, ['gzip', '-f', bin_path])
235 def make_dtb(fdt_type, comp):
236 """Create a sample DTB file.
238 Creates a DTS file and compiles it to a DTB.
241 fdt_type: The type of the FDT, i.e. internal, user.
242 comp: Flag to enable gzip compression.
244 The path of the created file.
247 # Generate resources referenced by FDT.
249 'sys-arch': sys_arch,
250 'fdt_type': fdt_type,
253 # Generate a test FDT file.
254 dts = make_fpath('test-efi-fit-%s.dts' % fdt_type)
255 with open(dts, 'w') as file:
256 file.write(FDT_DATA % fdt_params)
258 # Build the test FDT.
259 dtb = make_fpath('test-efi-fit-%s.dtb' % fdt_type)
260 util.run_and_log(cons, ['dtc', '-I', 'dts', '-O', 'dtb', '-o', dtb, dts])
262 util.run_and_log(cons, ['gzip', '-f', dtb])
267 """Create a sample FIT image.
269 Runs 'mkimage' to create a FIT image within U-Boot build dir.
271 comp: Enable gzip compression for the EFI binary and FDT blob.
273 The path of the created file.
276 # Generate resources referenced by ITS.
278 'sys-arch': sys_arch,
279 'efi-bin': os.path.basename(make_efi('test-efi-fit-helloworld.efi', comp)),
280 'kernel-type': 'kernel' if comp else 'kernel_noload',
281 'efi-comp': 'gzip' if comp else 'none',
282 'fdt-bin': os.path.basename(make_dtb('user', comp)),
283 'fdt-comp': 'gzip' if comp else 'none',
286 # Generate a test ITS file.
287 its_path = make_fpath('test-efi-fit-helloworld.its')
288 with open(its_path, 'w') as file:
289 file.write(ITS_DATA % its_params)
291 # Build the test ITS.
292 fit_path = make_fpath('test-efi-fit-helloworld.fit')
294 cons, [make_fpath('tools/mkimage'), '-f', its_path, fit_path])
297 def load_fit_from_host(fit):
298 """Load the FIT image using the 'host load' command and return its address.
301 fit: Dictionary describing the FIT image to load, see env__efi_fit_test_file
302 in the comment at the beginning of this file.
304 The address where the file has been loaded.
307 addr = fit.get('addr', None)
309 addr = util.find_ram_base(cons)
311 output = cons.run_command(
312 'host load hostfs - %x %s/%s' % (addr, fit['dn'], fit['fn']))
313 expected_text = ' bytes read'
314 size = fit.get('size', None)
316 expected_text = '%d' % size + expected_text
317 assert expected_text in output
321 def load_fit_from_tftp(fit):
322 """Load the FIT image using the tftpboot command and return its address.
324 The file is downloaded from the TFTP server, its size and optionally its
328 fit: Dictionary describing the FIT image to load, see env__efi_fit_tftp_file
329 in the comment at the beginning of this file.
331 The address where the file has been loaded.
334 addr = fit.get('addr', None)
336 addr = util.find_ram_base(cons)
338 file_name = fit['fn']
339 output = cons.run_command('tftpboot %x %s' % (addr, file_name))
340 expected_text = 'Bytes transferred = '
341 size = fit.get('size', None)
343 expected_text += '%d' % size
344 assert expected_text in output
346 expected_crc = fit.get('crc32', None)
350 if cons.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
353 output = cons.run_command('crc32 $fileaddr $filesize')
354 assert expected_crc in output
358 def launch_efi(enable_fdt, enable_comp):
359 """Launch U-Boot's helloworld.efi binary from a FIT image.
361 An external image file can be downloaded from TFTP, when related
362 details are provided by the boardenv_* file; see the comment at the
363 beginning of this file.
365 If the size of the TFTP file is not provided within env__efi_fit_tftp_file,
366 the test image is generated automatically and placed in the TFTP root
367 directory specified via the 'dn' field.
369 When running the tests on Sandbox, the image file is loaded directly
370 from the host filesystem.
372 Once the load address is available on U-Boot console, the 'bootm'
373 command is executed for either 'config-efi-fdt' or 'config-efi-nofdt'
374 FIT configuration, depending on the value of the 'enable_fdt' function
377 Eventually the 'Hello, world' message is expected in the U-Boot console.
380 enable_fdt: Flag to enable using the FDT blob inside FIT image.
381 enable_comp: Flag to enable GZIP compression on EFI and FDT
385 with cons.log.section('FDT=%s;COMP=%s' % (enable_fdt, enable_comp)):
388 'dn': cons.config.build_dir,
393 net_set_up = net_dhcp()
394 net_set_up = net_setup_static() or net_set_up
396 pytest.skip('Network not initialized')
398 fit = cons.config.env.get('env__efi_fit_tftp_file', None)
400 pytest.skip('No env__efi_fit_tftp_file binary specified in environment')
402 size = fit.get('size', None)
404 if not fit.get('dn', None):
405 pytest.skip('Neither "size", nor "dn" info provided in env__efi_fit_tftp_file')
407 # Create test FIT image.
408 fit_path = make_fit(enable_comp)
409 fit['fn'] = os.path.basename(fit_path)
410 fit['size'] = os.path.getsize(fit_path)
412 # Copy image to TFTP root directory.
413 if fit['dn'] != cons.config.build_dir:
414 util.run_and_log(cons, ['mv', '-f', fit_path, '%s/' % fit['dn']])
417 addr = load_fit_from_host(fit) if is_sandbox else load_fit_from_tftp(fit)
419 # Select boot configuration.
420 fit_config = 'config-efi-fdt' if enable_fdt else 'config-efi-nofdt'
424 'bootm %x#%s' % (addr, fit_config), wait_for_prompt=False)
426 cons.wait_for('Booting using the fdt blob')
427 cons.wait_for('Hello, world')
428 cons.wait_for('## Application terminated, r = 0')
431 cons = u_boot_console
432 # Array slice removes leading/trailing quotes.
433 sys_arch = cons.config.buildconfig.get('config_sys_arch', '"sandbox"')[1:-1]
434 is_sandbox = sys_arch == 'sandbox'
438 # Use our own device tree file, will be restored afterwards.
439 control_dtb = make_dtb('internal', False)
440 old_dtb = cons.config.dtb
441 cons.config.dtb = control_dtb
444 # - fdt OFF, gzip OFF
445 launch_efi(False, False)
447 launch_efi(True, False)
451 launch_efi(False, True)
453 launch_efi(True, True)
457 # Go back to the original U-Boot with the correct dtb.
458 cons.config.dtb = old_dtb