test_ums.py 8.1 KB

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