1 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3 # SPDX-License-Identifier: GPL-2.0
5 # Test U-Boot's "dfu" command. The test starts DFU in U-Boot, waits for USB
6 # device enumeration on the host, executes dfu-util multiple times to test
7 # various transfer sizes, many of which trigger USB driver edge cases, and
8 # finally aborts the "dfu" command in U-Boot.
16 Note: This test relies on:
18 a) boardenv_* to contain configuration values to define which USB ports are
19 available for testing. Without this, this test will be automatically skipped.
22 env__usb_dev_ports = (
24 "fixture_id": "micro_b",
26 "host_usb_dev_node": "/dev/usbdev-p2371-2180",
27 # This parameter is optional /if/ you only have a single board
28 # attached to your host at a time.
29 "host_usb_port_path": "3-13",
37 "alt_info": "/dfu_test.bin ext4 0 1;/dfu_dummy.bin ext4 0 1",
38 "cmd_params": "mmc 0",
39 # This value is optional.
40 # If present, it specified the set of transfer sizes tested.
41 # If missing, a default list of sizes will be used, which covers
42 # various useful corner cases.
43 # Manually specifying test sizes is useful if you wish to test 4 DFU
44 # configurations, but don't want to test every single transfer size
45 # on each, to avoid bloating the overall time taken by testing.
46 "test_sizes": (63, 64, 65),
50 b) udev rules to set permissions on devices nodes, so that sudo is not
51 required. For example:
53 ACTION=="add", SUBSYSTEM=="block", SUBSYSTEMS=="usb", KERNELS=="3-13", MODE:="666"
55 (You may wish to change the group ID instead of setting the permissions wide
56 open. All that matters is that the user ID running the test can access the
60 # The set of file sizes to test. These values trigger various edge-cases such
61 # as one less than, equal to, and one greater than typical USB max packet
62 # sizes, and similar boundary conditions.
63 test_sizes_default = (
81 first_usb_dev_port = None
83 @pytest.mark.buildconfigspec('cmd_dfu')
84 def test_dfu(u_boot_console, env__usb_dev_port, env__dfu_config):
85 """Test the "dfu" command; the host system must be able to enumerate a USB
86 device when "dfu" is running, various DFU transfers are tested, and the
87 USB device must disappear when "dfu" is aborted.
90 u_boot_console: A U-Boot console connection.
91 env__usb_dev_port: The single USB device-mode port specification on
92 which to run the test. See the file-level comment above for
93 details of the format.
94 env__dfu_config: The single DFU (memory region) configuration on which
95 to run the test. See the file-level comment above for details
102 # Default alt settings for test and dummy files
103 alt_setting_test_file = 0
104 alt_setting_dummy_file = 1
107 """Start U-Boot's dfu shell command.
109 This also waits for the host-side USB enumeration process to complete.
118 fh = u_boot_utils.attempt_to_open_file(
119 env__usb_dev_port['host_usb_dev_node'])
122 raise Exception('USB device present before dfu command invoked')
124 u_boot_console.log.action(
125 'Starting long-running U-Boot dfu shell command')
127 cmd = 'setenv dfu_alt_info "%s"' % env__dfu_config['alt_info']
128 u_boot_console.run_command(cmd)
130 cmd = 'dfu 0 ' + env__dfu_config['cmd_params']
131 u_boot_console.run_command(cmd, wait_for_prompt=False)
132 u_boot_console.log.action('Waiting for DFU USB device to appear')
133 fh = u_boot_utils.wait_until_open_succeeds(
134 env__usb_dev_port['host_usb_dev_node'])
137 def stop_dfu(ignore_errors):
138 """Stop U-Boot's dfu shell command from executing.
140 This also waits for the host-side USB de-enumeration process to
144 ignore_errors: Ignore any errors. This is useful if an error has
145 already been detected, and the code is performing best-effort
146 cleanup. In this case, we do not want to mask the original
147 error by "honoring" any new errors.
154 u_boot_console.log.action(
155 'Stopping long-running U-Boot dfu shell command')
156 u_boot_console.ctrlc()
157 u_boot_console.log.action(
158 'Waiting for DFU USB device to disappear')
159 u_boot_utils.wait_until_file_open_fails(
160 env__usb_dev_port['host_usb_dev_node'], ignore_errors)
162 if not ignore_errors:
165 def run_dfu_util(alt_setting, fn, up_dn_load_arg):
166 """Invoke dfu-util on the host.
169 alt_setting: The DFU "alternate setting" identifier to interact
171 fn: The host-side file name to transfer.
172 up_dn_load_arg: '-U' or '-D' depending on whether a DFU upload or
173 download operation should be performed.
179 cmd = ['dfu-util', '-a', str(alt_setting), up_dn_load_arg, fn]
180 if 'host_usb_port_path' in env__usb_dev_port:
181 cmd += ['-p', env__usb_dev_port['host_usb_port_path']]
182 u_boot_utils.run_and_log(u_boot_console, cmd)
183 u_boot_console.wait_for('Ctrl+C to exit ...')
185 def dfu_write(alt_setting, fn):
186 """Write a file to the target board using DFU.
189 alt_setting: The DFU "alternate setting" identifier to interact
191 fn: The host-side file name to transfer.
197 run_dfu_util(alt_setting, fn, '-D')
199 def dfu_read(alt_setting, fn):
200 """Read a file from the target board using DFU.
203 alt_setting: The DFU "alternate setting" identifier to interact
205 fn: The host-side file name to transfer.
211 # dfu-util fails reads/uploads if the host file already exists
212 if os.path.exists(fn):
214 run_dfu_util(alt_setting, fn, '-U')
216 def dfu_write_read_check(size):
217 """Test DFU transfers of a specific size of data
219 This function first writes data to the board then reads it back and
220 compares the written and read back data. Measures are taken to avoid
221 certain types of false positives.
224 size: The data size to test.
230 test_f = u_boot_utils.PersistentRandomFile(u_boot_console,
231 'dfu_%d.bin' % size, size)
232 readback_fn = u_boot_console.config.result_dir + '/dfu_readback.bin'
234 u_boot_console.log.action('Writing test data to DFU primary ' +
236 dfu_write(alt_setting_test_file, test_f.abs_fn)
238 u_boot_console.log.action('Writing dummy data to DFU secondary ' +
239 'altsetting to clear DFU buffers')
240 dfu_write(alt_setting_dummy_file, dummy_f.abs_fn)
242 u_boot_console.log.action('Reading DFU primary altsetting for ' +
244 dfu_read(alt_setting_test_file, readback_fn)
246 u_boot_console.log.action('Comparing written and read data')
247 written_hash = test_f.content_hash
248 read_back_hash = u_boot_utils.md5sum_file(readback_fn, size)
249 assert(written_hash == read_back_hash)
251 # This test may be executed against multiple USB ports. The test takes a
252 # long time, so we don't want to do the whole thing each time. Instead,
253 # execute the full test on the first USB port, and perform a very limited
254 # test on other ports. In the limited case, we solely validate that the
255 # host PC can enumerate the U-Boot USB device.
256 global first_usb_dev_port
257 if not first_usb_dev_port:
258 first_usb_dev_port = env__usb_dev_port
259 if env__usb_dev_port == first_usb_dev_port:
260 sizes = env__dfu_config.get('test_sizes', test_sizes_default)
264 dummy_f = u_boot_utils.PersistentRandomFile(u_boot_console,
265 'dfu_dummy.bin', 1024)
267 ignore_cleanup_errors = True
271 u_boot_console.log.action(
272 'Overwriting DFU primary altsetting with dummy data')
273 dfu_write(alt_setting_test_file, dummy_f.abs_fn)
276 with u_boot_console.log.section('Data size %d' % size):
277 dfu_write_read_check(size)
278 # Make the status of each sub-test obvious. If the test didn't
279 # pass, an exception was thrown so this code isn't executed.
280 u_boot_console.log.status_pass('OK')
281 ignore_cleanup_errors = False
283 stop_dfu(ignore_cleanup_errors)