test_ums.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
  3. # Test U-Boot's "ums" command. The test starts UMS in U-Boot, waits for USB
  4. # device enumeration on the host, reads a small block of data from the UMS
  5. # block device, optionally mounts a partition and performs filesystem-based
  6. # read/write tests, and finally aborts the "ums" command in U-Boot.
  7. import os
  8. import os.path
  9. import pytest
  10. import re
  11. import time
  12. import u_boot_utils
  13. """
  14. Note: This test relies on:
  15. a) boardenv_* to contain configuration values to define which USB ports are
  16. available for testing. Without this, this test will be automatically skipped.
  17. For example:
  18. # Leave this list empty if you have no block_devs below with writable
  19. # partitions defined.
  20. env__mount_points = (
  21. '/mnt/ubtest-mnt-p2371-2180-na',
  22. )
  23. env__usb_dev_ports = (
  24. {
  25. 'fixture_id': 'micro_b',
  26. 'tgt_usb_ctlr': '0',
  27. 'host_ums_dev_node': '/dev/disk/by-path/pci-0000:00:14.0-usb-0:13:1.0-scsi-0:0:0:0',
  28. },
  29. )
  30. env__block_devs = (
  31. # eMMC; always present
  32. {
  33. 'fixture_id': 'emmc',
  34. 'type': 'mmc',
  35. 'id': '0',
  36. # The following two properties are optional.
  37. # If present, the partition will be mounted and a file written-to and
  38. # read-from it. If missing, only a simple block read test will be
  39. # performed.
  40. 'writable_fs_partition': 1,
  41. 'writable_fs_subdir': 'tmp/',
  42. },
  43. # SD card; present since I plugged one in
  44. {
  45. 'fixture_id': 'sd',
  46. 'type': 'mmc',
  47. 'id': '1'
  48. },
  49. )
  50. b) udev rules to set permissions on devices nodes, so that sudo is not
  51. required. For example:
  52. ACTION=="add", SUBSYSTEM=="block", SUBSYSTEMS=="usb", KERNELS=="3-13", MODE:="666"
  53. (You may wish to change the group ID instead of setting the permissions wide
  54. open. All that matters is that the user ID running the test can access the
  55. device.)
  56. c) /etc/fstab entries to allow the block device to be mounted without requiring
  57. root permissions. For example:
  58. /dev/disk/by-path/pci-0000:00:14.0-usb-0:13:1.0-scsi-0:0:0:0-part1 /mnt/ubtest-mnt-p2371-2180-na ext4 noauto,user,nosuid,nodev
  59. This entry is only needed if any block_devs above contain a
  60. writable_fs_partition value.
  61. """
  62. @pytest.mark.buildconfigspec('cmd_usb_mass_storage')
  63. def test_ums(u_boot_console, env__usb_dev_port, env__block_devs):
  64. """Test the "ums" command; the host system must be able to enumerate a UMS
  65. device when "ums" is running, block and optionally file I/O are tested,
  66. and this device must disappear when "ums" is aborted.
  67. Args:
  68. u_boot_console: A U-Boot console connection.
  69. env__usb_dev_port: The single USB device-mode port specification on
  70. which to run the test. See the file-level comment above for
  71. details of the format.
  72. env__block_devs: The list of block devices that the target U-Boot
  73. device has attached. See the file-level comment above for details
  74. of the format.
  75. Returns:
  76. Nothing.
  77. """
  78. have_writable_fs_partition = 'writable_fs_partition' in env__block_devs[0]
  79. if not have_writable_fs_partition:
  80. # If 'writable_fs_subdir' is missing, we'll skip all parts of the
  81. # testing which mount filesystems.
  82. u_boot_console.log.warning(
  83. 'boardenv missing "writable_fs_partition"; ' +
  84. 'UMS testing will be limited.')
  85. tgt_usb_ctlr = env__usb_dev_port['tgt_usb_ctlr']
  86. host_ums_dev_node = env__usb_dev_port['host_ums_dev_node']
  87. # We're interested in testing USB device mode on each port, not the cross-
  88. # product of that with each device. So, just pick the first entry in the
  89. # device list here. We'll test each block device somewhere else.
  90. tgt_dev_type = env__block_devs[0]['type']
  91. tgt_dev_id = env__block_devs[0]['id']
  92. if have_writable_fs_partition:
  93. mount_point = u_boot_console.config.env['env__mount_points'][0]
  94. mount_subdir = env__block_devs[0]['writable_fs_subdir']
  95. part_num = env__block_devs[0]['writable_fs_partition']
  96. host_ums_part_node = '%s-part%d' % (host_ums_dev_node, part_num)
  97. else:
  98. host_ums_part_node = host_ums_dev_node
  99. test_f = u_boot_utils.PersistentRandomFile(u_boot_console, 'ums.bin',
  100. 1024 * 1024);
  101. if have_writable_fs_partition:
  102. mounted_test_fn = mount_point + '/' + mount_subdir + test_f.fn
  103. def start_ums():
  104. """Start U-Boot's ums shell command.
  105. This also waits for the host-side USB enumeration process to complete.
  106. Args:
  107. None.
  108. Returns:
  109. Nothing.
  110. """
  111. u_boot_console.log.action(
  112. 'Starting long-running U-Boot ums shell command')
  113. cmd = 'ums %s %s %s' % (tgt_usb_ctlr, tgt_dev_type, tgt_dev_id)
  114. u_boot_console.run_command(cmd, wait_for_prompt=False)
  115. u_boot_console.wait_for(re.compile('UMS: LUN.*[\r\n]'))
  116. fh = u_boot_utils.wait_until_open_succeeds(host_ums_part_node)
  117. u_boot_console.log.action('Reading raw data from UMS device')
  118. fh.read(4096)
  119. fh.close()
  120. def mount():
  121. """Mount the block device that U-Boot exports.
  122. Args:
  123. None.
  124. Returns:
  125. Nothing.
  126. """
  127. u_boot_console.log.action('Mounting exported UMS device')
  128. cmd = ('/bin/mount', host_ums_part_node)
  129. u_boot_utils.run_and_log(u_boot_console, cmd)
  130. def umount(ignore_errors):
  131. """Unmount the block device that U-Boot exports.
  132. Args:
  133. ignore_errors: Ignore any errors. This is useful if an error has
  134. already been detected, and the code is performing best-effort
  135. cleanup. In this case, we do not want to mask the original
  136. error by "honoring" any new errors.
  137. Returns:
  138. Nothing.
  139. """
  140. u_boot_console.log.action('Unmounting UMS device')
  141. cmd = ('/bin/umount', host_ums_part_node)
  142. u_boot_utils.run_and_log(u_boot_console, cmd, ignore_errors)
  143. def stop_ums(ignore_errors):
  144. """Stop U-Boot's ums shell command from executing.
  145. This also waits for the host-side USB de-enumeration process to
  146. complete.
  147. Args:
  148. ignore_errors: Ignore any errors. This is useful if an error has
  149. already been detected, and the code is performing best-effort
  150. cleanup. In this case, we do not want to mask the original
  151. error by "honoring" any new errors.
  152. Returns:
  153. Nothing.
  154. """
  155. u_boot_console.log.action(
  156. 'Stopping long-running U-Boot ums shell command')
  157. u_boot_console.ctrlc()
  158. u_boot_utils.wait_until_file_open_fails(host_ums_part_node,
  159. ignore_errors)
  160. ignore_cleanup_errors = True
  161. try:
  162. start_ums()
  163. if not have_writable_fs_partition:
  164. # Skip filesystem-based testing if not configured
  165. return
  166. try:
  167. mount()
  168. u_boot_console.log.action('Writing test file via UMS')
  169. cmd = ('rm', '-f', mounted_test_fn)
  170. u_boot_utils.run_and_log(u_boot_console, cmd)
  171. if os.path.exists(mounted_test_fn):
  172. raise Exception('Could not rm target UMS test file')
  173. cmd = ('cp', test_f.abs_fn, mounted_test_fn)
  174. u_boot_utils.run_and_log(u_boot_console, cmd)
  175. ignore_cleanup_errors = False
  176. finally:
  177. umount(ignore_errors=ignore_cleanup_errors)
  178. finally:
  179. stop_ums(ignore_errors=ignore_cleanup_errors)
  180. ignore_cleanup_errors = True
  181. try:
  182. start_ums()
  183. try:
  184. mount()
  185. u_boot_console.log.action('Reading test file back via UMS')
  186. read_back_hash = u_boot_utils.md5sum_file(mounted_test_fn)
  187. cmd = ('rm', '-f', mounted_test_fn)
  188. u_boot_utils.run_and_log(u_boot_console, cmd)
  189. ignore_cleanup_errors = False
  190. finally:
  191. umount(ignore_errors=ignore_cleanup_errors)
  192. finally:
  193. stop_ums(ignore_errors=ignore_cleanup_errors)
  194. written_hash = test_f.content_hash
  195. assert(written_hash == read_back_hash)