test_dfu.py 8.5 KB

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