blob: 585e6b29d7f8b470efe303ab5776a370194727c9 [file] [log] [blame]
Stephen Warrenf5d196d2016-01-22 12:30:14 -07001# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
2#
3# SPDX-License-Identifier: GPL-2.0
4
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.
9
10import os
11import os.path
12import pytest
13import u_boot_utils
14
Stephen Warrene8debf32016-01-26 13:41:30 -070015"""
Stephen Warrenf5d196d2016-01-22 12:30:14 -070016Note: This test relies on:
17
18a) boardenv_* to contain configuration values to define which USB ports are
19available for testing. Without this, this test will be automatically skipped.
20For example:
21
22env__usb_dev_ports = (
23 {
Stephen Warrend20e5e92016-01-26 15:26:04 -070024 "fixture_id": "micro_b",
Stephen Warrenf5d196d2016-01-22 12:30:14 -070025 "tgt_usb_ctlr": "0",
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",
30 },
31)
32
Lukasz Majewskif3a87f52016-04-20 10:57:08 +020033# Optional entries (required only when "alt_id_test_file" and
34# "alt_id_dummy_file" are specified).
35test_file_name = "/dfu_test.bin"
36dummy_file_name = "/dfu_dummy.bin"
37# Above files are used to generate proper "alt_info" entry
38"alt_info": "/%s ext4 0 2;/%s ext4 0 2" % (test_file_name, dummy_file_name),
39
Stephen Warrenf5d196d2016-01-22 12:30:14 -070040env__dfu_configs = (
41 # eMMC, partition 1
42 {
Stephen Warrend20e5e92016-01-26 15:26:04 -070043 "fixture_id": "emmc",
Stephen Warrenf5d196d2016-01-22 12:30:14 -070044 "alt_info": "/dfu_test.bin ext4 0 1;/dfu_dummy.bin ext4 0 1",
45 "cmd_params": "mmc 0",
Stephen Warren26db3a62016-01-28 13:14:16 -070046 # This value is optional.
47 # If present, it specified the set of transfer sizes tested.
48 # If missing, a default list of sizes will be used, which covers
49 # various useful corner cases.
50 # Manually specifying test sizes is useful if you wish to test 4 DFU
51 # configurations, but don't want to test every single transfer size
52 # on each, to avoid bloating the overall time taken by testing.
53 "test_sizes": (63, 64, 65),
Lukasz Majewski8eb37522016-04-18 17:01:15 +020054 # This value is optional.
55 # The name of the environment variable that the the dfu command reads
56 # alt info from. If unspecified, this defaults to dfu_alt_info, which is
57 # valid for most systems. Some systems use a different variable name.
58 # One example is the Odroid XU3, which automatically generates
59 # $dfu_alt_info, each time the dfu command is run, by concatenating
60 # $dfu_alt_boot and $dfu_alt_system.
61 "alt_info_env_name": "dfu_alt_system",
Lukasz Majewskif3a87f52016-04-20 10:57:08 +020062 # This value is optional.
63 # For boards which require the "test file" alt setting number other than
64 # default (0) it is possible to specify exact file name to be used as
65 # this parameter.
66 "alt_id_test_file": test_file_name,
67 # This value is optional.
68 # For boards which require the "dummy file" alt setting number other
69 # than default (1) it is possible to specify exact file name to be used
70 # as this parameter.
71 "alt_id_dummy_file": dummy_file_name,
Stephen Warrenf5d196d2016-01-22 12:30:14 -070072 },
73)
Stephen Warrend20e5e92016-01-26 15:26:04 -070074
Stephen Warrenf5d196d2016-01-22 12:30:14 -070075b) udev rules to set permissions on devices nodes, so that sudo is not
76required. For example:
77
78ACTION=="add", SUBSYSTEM=="block", SUBSYSTEMS=="usb", KERNELS=="3-13", MODE:="666"
79
80(You may wish to change the group ID instead of setting the permissions wide
81open. All that matters is that the user ID running the test can access the
82device.)
Stephen Warrene8debf32016-01-26 13:41:30 -070083"""
Stephen Warrenf5d196d2016-01-22 12:30:14 -070084
85# The set of file sizes to test. These values trigger various edge-cases such
86# as one less than, equal to, and one greater than typical USB max packet
87# sizes, and similar boundary conditions.
Stephen Warren26db3a62016-01-28 13:14:16 -070088test_sizes_default = (
Stephen Warrenf5d196d2016-01-22 12:30:14 -070089 64 - 1,
90 64,
91 64 + 1,
92 128 - 1,
93 128,
94 128 + 1,
95 960 - 1,
96 960,
97 960 + 1,
98 4096 - 1,
99 4096,
100 4096 + 1,
101 1024 * 1024 - 1,
102 1024 * 1024,
103 8 * 1024 * 1024,
104)
105
106first_usb_dev_port = None
107
108@pytest.mark.buildconfigspec('cmd_dfu')
109def test_dfu(u_boot_console, env__usb_dev_port, env__dfu_config):
Stephen Warrene8debf32016-01-26 13:41:30 -0700110 """Test the "dfu" command; the host system must be able to enumerate a USB
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700111 device when "dfu" is running, various DFU transfers are tested, and the
112 USB device must disappear when "dfu" is aborted.
113
114 Args:
115 u_boot_console: A U-Boot console connection.
116 env__usb_dev_port: The single USB device-mode port specification on
117 which to run the test. See the file-level comment above for
118 details of the format.
119 env__dfu_config: The single DFU (memory region) configuration on which
120 to run the test. See the file-level comment above for details
121 of the format.
122
123 Returns:
124 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700125 """
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700126
127 def start_dfu():
Stephen Warrene8debf32016-01-26 13:41:30 -0700128 """Start U-Boot's dfu shell command.
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700129
130 This also waits for the host-side USB enumeration process to complete.
131
132 Args:
133 None.
134
135 Returns:
136 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700137 """
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700138
Stephen Warrendaa69f52016-05-05 17:02:06 -0600139 u_boot_utils.wait_until_file_open_fails(
140 env__usb_dev_port['host_usb_dev_node'], True)
Stephen Warrenbe1df822016-01-26 10:59:43 -0700141 fh = u_boot_utils.attempt_to_open_file(
142 env__usb_dev_port['host_usb_dev_node'])
143 if fh:
144 fh.close()
145 raise Exception('USB device present before dfu command invoked')
146
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700147 u_boot_console.log.action(
148 'Starting long-running U-Boot dfu shell command')
149
Lukasz Majewski8eb37522016-04-18 17:01:15 +0200150 dfu_alt_info_env = env__dfu_config.get('alt_info_env_name', \
151 'dfu_alt_info')
152
153 cmd = 'setenv "%s" "%s"' % (dfu_alt_info_env,
154 env__dfu_config['alt_info'])
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700155 u_boot_console.run_command(cmd)
156
157 cmd = 'dfu 0 ' + env__dfu_config['cmd_params']
158 u_boot_console.run_command(cmd, wait_for_prompt=False)
159 u_boot_console.log.action('Waiting for DFU USB device to appear')
160 fh = u_boot_utils.wait_until_open_succeeds(
161 env__usb_dev_port['host_usb_dev_node'])
162 fh.close()
163
164 def stop_dfu(ignore_errors):
Stephen Warrene8debf32016-01-26 13:41:30 -0700165 """Stop U-Boot's dfu shell command from executing.
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700166
167 This also waits for the host-side USB de-enumeration process to
168 complete.
169
170 Args:
171 ignore_errors: Ignore any errors. This is useful if an error has
172 already been detected, and the code is performing best-effort
173 cleanup. In this case, we do not want to mask the original
174 error by "honoring" any new errors.
175
176 Returns:
177 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700178 """
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700179
180 try:
181 u_boot_console.log.action(
182 'Stopping long-running U-Boot dfu shell command')
183 u_boot_console.ctrlc()
184 u_boot_console.log.action(
185 'Waiting for DFU USB device to disappear')
186 u_boot_utils.wait_until_file_open_fails(
187 env__usb_dev_port['host_usb_dev_node'], ignore_errors)
188 except:
189 if not ignore_errors:
190 raise
191
192 def run_dfu_util(alt_setting, fn, up_dn_load_arg):
Stephen Warrene8debf32016-01-26 13:41:30 -0700193 """Invoke dfu-util on the host.
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700194
195 Args:
196 alt_setting: The DFU "alternate setting" identifier to interact
197 with.
198 fn: The host-side file name to transfer.
199 up_dn_load_arg: '-U' or '-D' depending on whether a DFU upload or
200 download operation should be performed.
201
202 Returns:
203 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700204 """
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700205
Lukasz Majewskif3a87f52016-04-20 10:57:08 +0200206 cmd = ['dfu-util', '-a', alt_setting, up_dn_load_arg, fn]
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700207 if 'host_usb_port_path' in env__usb_dev_port:
208 cmd += ['-p', env__usb_dev_port['host_usb_port_path']]
209 u_boot_utils.run_and_log(u_boot_console, cmd)
210 u_boot_console.wait_for('Ctrl+C to exit ...')
211
212 def dfu_write(alt_setting, fn):
Stephen Warrene8debf32016-01-26 13:41:30 -0700213 """Write a file to the target board using DFU.
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700214
215 Args:
216 alt_setting: The DFU "alternate setting" identifier to interact
217 with.
218 fn: The host-side file name to transfer.
219
220 Returns:
221 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700222 """
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700223
224 run_dfu_util(alt_setting, fn, '-D')
225
226 def dfu_read(alt_setting, fn):
Stephen Warrene8debf32016-01-26 13:41:30 -0700227 """Read a file from the target board using DFU.
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700228
229 Args:
230 alt_setting: The DFU "alternate setting" identifier to interact
231 with.
232 fn: The host-side file name to transfer.
233
234 Returns:
235 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700236 """
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700237
238 # dfu-util fails reads/uploads if the host file already exists
239 if os.path.exists(fn):
240 os.remove(fn)
241 run_dfu_util(alt_setting, fn, '-U')
242
243 def dfu_write_read_check(size):
Stephen Warrene8debf32016-01-26 13:41:30 -0700244 """Test DFU transfers of a specific size of data
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700245
246 This function first writes data to the board then reads it back and
247 compares the written and read back data. Measures are taken to avoid
248 certain types of false positives.
249
250 Args:
251 size: The data size to test.
252
253 Returns:
254 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700255 """
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700256
257 test_f = u_boot_utils.PersistentRandomFile(u_boot_console,
258 'dfu_%d.bin' % size, size)
259 readback_fn = u_boot_console.config.result_dir + '/dfu_readback.bin'
260
261 u_boot_console.log.action('Writing test data to DFU primary ' +
262 'altsetting')
Lukasz Majewskic6eb8992016-04-20 10:36:32 +0200263 dfu_write(alt_setting_test_file, test_f.abs_fn)
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700264
265 u_boot_console.log.action('Writing dummy data to DFU secondary ' +
266 'altsetting to clear DFU buffers')
Lukasz Majewskic6eb8992016-04-20 10:36:32 +0200267 dfu_write(alt_setting_dummy_file, dummy_f.abs_fn)
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700268
269 u_boot_console.log.action('Reading DFU primary altsetting for ' +
270 'comparison')
Lukasz Majewskic6eb8992016-04-20 10:36:32 +0200271 dfu_read(alt_setting_test_file, readback_fn)
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700272
273 u_boot_console.log.action('Comparing written and read data')
274 written_hash = test_f.content_hash
275 read_back_hash = u_boot_utils.md5sum_file(readback_fn, size)
276 assert(written_hash == read_back_hash)
277
278 # This test may be executed against multiple USB ports. The test takes a
279 # long time, so we don't want to do the whole thing each time. Instead,
280 # execute the full test on the first USB port, and perform a very limited
281 # test on other ports. In the limited case, we solely validate that the
282 # host PC can enumerate the U-Boot USB device.
283 global first_usb_dev_port
284 if not first_usb_dev_port:
285 first_usb_dev_port = env__usb_dev_port
286 if env__usb_dev_port == first_usb_dev_port:
Stephen Warren26db3a62016-01-28 13:14:16 -0700287 sizes = env__dfu_config.get('test_sizes', test_sizes_default)
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700288 else:
289 sizes = []
290
291 dummy_f = u_boot_utils.PersistentRandomFile(u_boot_console,
292 'dfu_dummy.bin', 1024)
293
Lukasz Majewskif3a87f52016-04-20 10:57:08 +0200294 alt_setting_test_file = env__dfu_config.get('alt_id_test_file', '0')
295 alt_setting_dummy_file = env__dfu_config.get('alt_id_dummy_file', '1')
296
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700297 ignore_cleanup_errors = True
298 try:
299 start_dfu()
300
301 u_boot_console.log.action(
302 'Overwriting DFU primary altsetting with dummy data')
Lukasz Majewskic6eb8992016-04-20 10:36:32 +0200303 dfu_write(alt_setting_test_file, dummy_f.abs_fn)
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700304
305 for size in sizes:
Stephen Warrena2ec5602016-01-26 13:41:31 -0700306 with u_boot_console.log.section('Data size %d' % size):
Stephen Warrenf5d196d2016-01-22 12:30:14 -0700307 dfu_write_read_check(size)
308 # Make the status of each sub-test obvious. If the test didn't
309 # pass, an exception was thrown so this code isn't executed.
310 u_boot_console.log.status_pass('OK')
311 ignore_cleanup_errors = False
312 finally:
313 stop_dfu(ignore_cleanup_errors)