test_dfu.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0
  4. # Test U-Boot's "dfu" command. The test starts DFU in U-Boot, waits for USB
  5. # device enumeration on the host, executes dfu-util multiple times to test
  6. # various transfer sizes, many of which trigger USB driver edge cases, and
  7. # finally aborts the "dfu" command in U-Boot.
  8. import os
  9. import os.path
  10. import pytest
  11. import u_boot_utils
  12. """
  13. Note: This test relies on:
  14. a) boardenv_* to contain configuration values to define which USB ports are
  15. available for testing. Without this, this test will be automatically skipped.
  16. For example:
  17. env__usb_dev_ports = (
  18. {
  19. "fixture_id": "micro_b",
  20. "tgt_usb_ctlr": "0",
  21. "host_usb_dev_node": "/dev/usbdev-p2371-2180",
  22. # This parameter is optional /if/ you only have a single board
  23. # attached to your host at a time.
  24. "host_usb_port_path": "3-13",
  25. },
  26. )
  27. env__dfu_configs = (
  28. # eMMC, partition 1
  29. {
  30. "fixture_id": "emmc",
  31. "alt_info": "/dfu_test.bin ext4 0 1;/dfu_dummy.bin ext4 0 1",
  32. "cmd_params": "mmc 0",
  33. # This value is optional.
  34. # If present, it specified the set of transfer sizes tested.
  35. # If missing, a default list of sizes will be used, which covers
  36. # various useful corner cases.
  37. # Manually specifying test sizes is useful if you wish to test 4 DFU
  38. # configurations, but don't want to test every single transfer size
  39. # on each, to avoid bloating the overall time taken by testing.
  40. "test_sizes": (63, 64, 65),
  41. },
  42. )
  43. b) udev rules to set permissions on devices nodes, so that sudo is not
  44. required. For example:
  45. ACTION=="add", SUBSYSTEM=="block", SUBSYSTEMS=="usb", KERNELS=="3-13", MODE:="666"
  46. (You may wish to change the group ID instead of setting the permissions wide
  47. open. All that matters is that the user ID running the test can access the
  48. device.)
  49. """
  50. # The set of file sizes to test. These values trigger various edge-cases such
  51. # as one less than, equal to, and one greater than typical USB max packet
  52. # sizes, and similar boundary conditions.
  53. test_sizes_default = (
  54. 64 - 1,
  55. 64,
  56. 64 + 1,
  57. 128 - 1,
  58. 128,
  59. 128 + 1,
  60. 960 - 1,
  61. 960,
  62. 960 + 1,
  63. 4096 - 1,
  64. 4096,
  65. 4096 + 1,
  66. 1024 * 1024 - 1,
  67. 1024 * 1024,
  68. 8 * 1024 * 1024,
  69. )
  70. first_usb_dev_port = None
  71. @pytest.mark.buildconfigspec('cmd_dfu')
  72. def test_dfu(u_boot_console, env__usb_dev_port, env__dfu_config):
  73. """Test the "dfu" command; the host system must be able to enumerate a USB
  74. device when "dfu" is running, various DFU transfers are tested, and the
  75. USB device must disappear when "dfu" is aborted.
  76. Args:
  77. u_boot_console: A U-Boot console connection.
  78. env__usb_dev_port: The single USB device-mode port specification on
  79. which to run the test. See the file-level comment above for
  80. details of the format.
  81. env__dfu_config: The single DFU (memory region) configuration on which
  82. to run the test. See the file-level comment above for details
  83. of the format.
  84. Returns:
  85. Nothing.
  86. """
  87. def start_dfu():
  88. """Start U-Boot's dfu shell command.
  89. This also waits for the host-side USB enumeration process to complete.
  90. Args:
  91. None.
  92. Returns:
  93. Nothing.
  94. """
  95. fh = u_boot_utils.attempt_to_open_file(
  96. env__usb_dev_port['host_usb_dev_node'])
  97. if fh:
  98. fh.close()
  99. raise Exception('USB device present before dfu command invoked')
  100. u_boot_console.log.action(
  101. 'Starting long-running U-Boot dfu shell command')
  102. cmd = 'setenv dfu_alt_info "%s"' % env__dfu_config['alt_info']
  103. u_boot_console.run_command(cmd)
  104. cmd = 'dfu 0 ' + env__dfu_config['cmd_params']
  105. u_boot_console.run_command(cmd, wait_for_prompt=False)
  106. u_boot_console.log.action('Waiting for DFU USB device to appear')
  107. fh = u_boot_utils.wait_until_open_succeeds(
  108. env__usb_dev_port['host_usb_dev_node'])
  109. fh.close()
  110. def stop_dfu(ignore_errors):
  111. """Stop U-Boot's dfu shell command from executing.
  112. This also waits for the host-side USB de-enumeration process to
  113. complete.
  114. Args:
  115. ignore_errors: Ignore any errors. This is useful if an error has
  116. already been detected, and the code is performing best-effort
  117. cleanup. In this case, we do not want to mask the original
  118. error by "honoring" any new errors.
  119. Returns:
  120. Nothing.
  121. """
  122. try:
  123. u_boot_console.log.action(
  124. 'Stopping long-running U-Boot dfu shell command')
  125. u_boot_console.ctrlc()
  126. u_boot_console.log.action(
  127. 'Waiting for DFU USB device to disappear')
  128. u_boot_utils.wait_until_file_open_fails(
  129. env__usb_dev_port['host_usb_dev_node'], ignore_errors)
  130. except:
  131. if not ignore_errors:
  132. raise
  133. def run_dfu_util(alt_setting, fn, up_dn_load_arg):
  134. """Invoke dfu-util on the host.
  135. Args:
  136. alt_setting: The DFU "alternate setting" identifier to interact
  137. with.
  138. fn: The host-side file name to transfer.
  139. up_dn_load_arg: '-U' or '-D' depending on whether a DFU upload or
  140. download operation should be performed.
  141. Returns:
  142. Nothing.
  143. """
  144. cmd = ['dfu-util', '-a', str(alt_setting), up_dn_load_arg, fn]
  145. if 'host_usb_port_path' in env__usb_dev_port:
  146. cmd += ['-p', env__usb_dev_port['host_usb_port_path']]
  147. u_boot_utils.run_and_log(u_boot_console, cmd)
  148. u_boot_console.wait_for('Ctrl+C to exit ...')
  149. def dfu_write(alt_setting, fn):
  150. """Write a file to the target board using DFU.
  151. Args:
  152. alt_setting: The DFU "alternate setting" identifier to interact
  153. with.
  154. fn: The host-side file name to transfer.
  155. Returns:
  156. Nothing.
  157. """
  158. run_dfu_util(alt_setting, fn, '-D')
  159. def dfu_read(alt_setting, fn):
  160. """Read a file from the target board using DFU.
  161. Args:
  162. alt_setting: The DFU "alternate setting" identifier to interact
  163. with.
  164. fn: The host-side file name to transfer.
  165. Returns:
  166. Nothing.
  167. """
  168. # dfu-util fails reads/uploads if the host file already exists
  169. if os.path.exists(fn):
  170. os.remove(fn)
  171. run_dfu_util(alt_setting, fn, '-U')
  172. def dfu_write_read_check(size):
  173. """Test DFU transfers of a specific size of data
  174. This function first writes data to the board then reads it back and
  175. compares the written and read back data. Measures are taken to avoid
  176. certain types of false positives.
  177. Args:
  178. size: The data size to test.
  179. Returns:
  180. Nothing.
  181. """
  182. test_f = u_boot_utils.PersistentRandomFile(u_boot_console,
  183. 'dfu_%d.bin' % size, size)
  184. readback_fn = u_boot_console.config.result_dir + '/dfu_readback.bin'
  185. u_boot_console.log.action('Writing test data to DFU primary ' +
  186. 'altsetting')
  187. dfu_write(0, test_f.abs_fn)
  188. u_boot_console.log.action('Writing dummy data to DFU secondary ' +
  189. 'altsetting to clear DFU buffers')
  190. dfu_write(1, dummy_f.abs_fn)
  191. u_boot_console.log.action('Reading DFU primary altsetting for ' +
  192. 'comparison')
  193. dfu_read(0, readback_fn)
  194. u_boot_console.log.action('Comparing written and read data')
  195. written_hash = test_f.content_hash
  196. read_back_hash = u_boot_utils.md5sum_file(readback_fn, size)
  197. assert(written_hash == read_back_hash)
  198. # This test may be executed against multiple USB ports. The test takes a
  199. # long time, so we don't want to do the whole thing each time. Instead,
  200. # execute the full test on the first USB port, and perform a very limited
  201. # test on other ports. In the limited case, we solely validate that the
  202. # host PC can enumerate the U-Boot USB device.
  203. global first_usb_dev_port
  204. if not first_usb_dev_port:
  205. first_usb_dev_port = env__usb_dev_port
  206. if env__usb_dev_port == first_usb_dev_port:
  207. sizes = env__dfu_config.get('test_sizes', test_sizes_default)
  208. else:
  209. sizes = []
  210. dummy_f = u_boot_utils.PersistentRandomFile(u_boot_console,
  211. 'dfu_dummy.bin', 1024)
  212. ignore_cleanup_errors = True
  213. try:
  214. start_dfu()
  215. u_boot_console.log.action(
  216. 'Overwriting DFU primary altsetting with dummy data')
  217. dfu_write(0, dummy_f.abs_fn)
  218. for size in sizes:
  219. with u_boot_console.log.section('Data size %d' % size):
  220. dfu_write_read_check(size)
  221. # Make the status of each sub-test obvious. If the test didn't
  222. # pass, an exception was thrown so this code isn't executed.
  223. u_boot_console.log.status_pass('OK')
  224. ignore_cleanup_errors = False
  225. finally:
  226. stop_dfu(ignore_cleanup_errors)