test_mmc_rd.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  3. # Test U-Boot's "mmc read" command. The test reads data from the eMMC or SD
  4. # card, and validates the no errors occurred, and that the expected data was
  5. # read if the test configuration contains a CRC of the expected data.
  6. import pytest
  7. import time
  8. import u_boot_utils
  9. """
  10. This test relies on boardenv_* to containing configuration values to define
  11. which MMC devices should be tested. For example:
  12. # Configuration data for test_mmc_dev, test_mmc_rescan, test_mmc_info; defines
  13. # whole MMC devices that mmc dev/rescan/info commands may operate upon.
  14. env__mmc_dev_configs = (
  15. {
  16. 'fixture_id': 'emmc-boot0',
  17. 'is_emmc': True,
  18. 'devid': 0,
  19. 'partid': 1,
  20. 'info_device': ???,
  21. 'info_speed': ???,
  22. 'info_mode': ???,
  23. 'info_buswidth': ???.
  24. },
  25. {
  26. 'fixture_id': 'emmc-boot1',
  27. 'is_emmc': True,
  28. 'devid': 0,
  29. 'partid': 2,
  30. 'info_device': ???,
  31. 'info_speed': ???,
  32. 'info_mode': ???,
  33. 'info_buswidth': ???.
  34. },
  35. {
  36. 'fixture_id': 'emmc-data',
  37. 'is_emmc': True,
  38. 'devid': 0,
  39. 'partid': 0,
  40. 'info_device': ???,
  41. 'info_speed': ???,
  42. 'info_mode': ???,
  43. 'info_buswidth': ???.
  44. },
  45. {
  46. 'fixture_id': 'sd',
  47. 'is_emmc': False,
  48. 'devid': 1,
  49. 'partid': None,
  50. 'info_device': ???,
  51. 'info_speed': ???,
  52. 'info_mode': ???,
  53. 'info_buswidth': ???.
  54. },
  55. )
  56. # Configuration data for test_mmc_rd; defines regions of the MMC (entire
  57. # devices, or ranges of sectors) which can be read:
  58. env__mmc_rd_configs = (
  59. {
  60. 'fixture_id': 'emmc-boot0',
  61. 'is_emmc': True,
  62. 'devid': 0,
  63. 'partid': 1,
  64. 'sector': 0x10,
  65. 'count': 1,
  66. },
  67. {
  68. 'fixture_id': 'emmc-boot1',
  69. 'is_emmc': True,
  70. 'devid': 0,
  71. 'partid': 2,
  72. 'sector': 0x10,
  73. 'count': 1,
  74. },
  75. {
  76. 'fixture_id': 'emmc-data',
  77. 'is_emmc': True,
  78. 'devid': 0,
  79. 'partid': 0,
  80. 'sector': 0x10,
  81. 'count': 0x1000,
  82. },
  83. {
  84. 'fixture_id': 'sd-mbr',
  85. 'is_emmc': False,
  86. 'devid': 1,
  87. 'partid': None,
  88. 'sector': 0,
  89. 'count': 1,
  90. 'crc32': '8f6ecf0d',
  91. },
  92. {
  93. 'fixture_id': 'sd-large',
  94. 'is_emmc': False,
  95. 'devid': 1,
  96. 'partid': None,
  97. 'sector': 0x10,
  98. 'count': 0x1000,
  99. },
  100. )
  101. """
  102. def mmc_dev(u_boot_console, is_emmc, devid, partid):
  103. """Run the "mmc dev" command.
  104. Args:
  105. u_boot_console: A U-Boot console connection.
  106. is_emmc: Whether the device is eMMC
  107. devid: Device ID
  108. partid: Partition ID
  109. Returns:
  110. Nothing.
  111. """
  112. # Select MMC device
  113. cmd = 'mmc dev %d' % devid
  114. if is_emmc:
  115. cmd += ' %d' % partid
  116. response = u_boot_console.run_command(cmd)
  117. assert 'no card present' not in response
  118. if is_emmc:
  119. partid_response = '(part %d)' % partid
  120. else:
  121. partid_response = ''
  122. good_response = 'mmc%d%s is current device' % (devid, partid_response)
  123. assert good_response in response
  124. @pytest.mark.buildconfigspec('cmd_mmc')
  125. def test_mmc_dev(u_boot_console, env__mmc_dev_config):
  126. """Test the "mmc dev" command.
  127. Args:
  128. u_boot_console: A U-Boot console connection.
  129. env__mmc_dev_config: The single MMC configuration on which
  130. to run the test. See the file-level comment above for details
  131. of the format.
  132. Returns:
  133. Nothing.
  134. """
  135. is_emmc = env__mmc_dev_config['is_emmc']
  136. devid = env__mmc_dev_config['devid']
  137. partid = env__mmc_dev_config.get('partid', 0)
  138. # Select MMC device
  139. mmc_dev(u_boot_console, is_emmc, devid, partid)
  140. @pytest.mark.buildconfigspec('cmd_mmc')
  141. def test_mmc_rescan(u_boot_console, env__mmc_dev_config):
  142. """Test the "mmc rescan" command.
  143. Args:
  144. u_boot_console: A U-Boot console connection.
  145. env__mmc_dev_config: The single MMC configuration on which
  146. to run the test. See the file-level comment above for details
  147. of the format.
  148. Returns:
  149. Nothing.
  150. """
  151. is_emmc = env__mmc_dev_config['is_emmc']
  152. devid = env__mmc_dev_config['devid']
  153. partid = env__mmc_dev_config.get('partid', 0)
  154. # Select MMC device
  155. mmc_dev(u_boot_console, is_emmc, devid, partid)
  156. # Rescan MMC device
  157. cmd = 'mmc rescan'
  158. response = u_boot_console.run_command(cmd)
  159. assert 'no card present' not in response
  160. @pytest.mark.buildconfigspec('cmd_mmc')
  161. def test_mmc_info(u_boot_console, env__mmc_dev_config):
  162. """Test the "mmc info" command.
  163. Args:
  164. u_boot_console: A U-Boot console connection.
  165. env__mmc_dev_config: The single MMC configuration on which
  166. to run the test. See the file-level comment above for details
  167. of the format.
  168. Returns:
  169. Nothing.
  170. """
  171. is_emmc = env__mmc_dev_config['is_emmc']
  172. devid = env__mmc_dev_config['devid']
  173. partid = env__mmc_dev_config.get('partid', 0)
  174. info_device = env__mmc_dev_config['info_device']
  175. info_speed = env__mmc_dev_config['info_speed']
  176. info_mode = env__mmc_dev_config['info_mode']
  177. info_buswidth = env__mmc_dev_config['info_buswidth']
  178. # Select MMC device
  179. mmc_dev(u_boot_console, is_emmc, devid, partid)
  180. # Read MMC device information
  181. cmd = 'mmc info'
  182. response = u_boot_console.run_command(cmd)
  183. good_response = "Device: %s" % info_device
  184. assert good_response in response
  185. good_response = "Bus Speed: %s" % info_speed
  186. assert good_response in response
  187. good_response = "Mode: %s" % info_mode
  188. assert good_response in response
  189. good_response = "Bus Width: %s" % info_buswidth
  190. assert good_response in response
  191. @pytest.mark.buildconfigspec('cmd_mmc')
  192. def test_mmc_rd(u_boot_console, env__mmc_rd_config):
  193. """Test the "mmc read" command.
  194. Args:
  195. u_boot_console: A U-Boot console connection.
  196. env__mmc_rd_config: The single MMC configuration on which
  197. to run the test. See the file-level comment above for details
  198. of the format.
  199. Returns:
  200. Nothing.
  201. """
  202. is_emmc = env__mmc_rd_config['is_emmc']
  203. devid = env__mmc_rd_config['devid']
  204. partid = env__mmc_rd_config.get('partid', 0)
  205. sector = env__mmc_rd_config.get('sector', 0)
  206. count_sectors = env__mmc_rd_config.get('count', 1)
  207. expected_crc32 = env__mmc_rd_config.get('crc32', None)
  208. read_duration_max = env__mmc_rd_config.get('read_duration_max', 0)
  209. count_bytes = count_sectors * 512
  210. bcfg = u_boot_console.config.buildconfig
  211. has_cmd_memory = bcfg.get('config_cmd_memory', 'n') == 'y'
  212. has_cmd_crc32 = bcfg.get('config_cmd_crc32', 'n') == 'y'
  213. ram_base = u_boot_utils.find_ram_base(u_boot_console)
  214. addr = '0x%08x' % ram_base
  215. # Select MMC device
  216. mmc_dev(u_boot_console, is_emmc, devid, partid)
  217. # Clear target RAM
  218. if expected_crc32:
  219. if has_cmd_memory and has_cmd_crc32:
  220. cmd = 'mw.b %s 0 0x%x' % (addr, count_bytes)
  221. u_boot_console.run_command(cmd)
  222. cmd = 'crc32 %s 0x%x' % (addr, count_bytes)
  223. response = u_boot_console.run_command(cmd)
  224. assert expected_crc32 not in response
  225. else:
  226. u_boot_console.log.warning(
  227. 'CONFIG_CMD_MEMORY or CONFIG_CMD_CRC32 != y: Skipping RAM clear')
  228. # Read data
  229. cmd = 'mmc read %s %x %x' % (addr, sector, count_sectors)
  230. tstart = time.time()
  231. response = u_boot_console.run_command(cmd)
  232. tend = time.time()
  233. good_response = 'MMC read: dev # %d, block # %d, count %d ... %d blocks read: OK' % (
  234. devid, sector, count_sectors, count_sectors)
  235. assert good_response in response
  236. # Check target RAM
  237. if expected_crc32:
  238. if has_cmd_crc32:
  239. cmd = 'crc32 %s 0x%x' % (addr, count_bytes)
  240. response = u_boot_console.run_command(cmd)
  241. assert expected_crc32 in response
  242. else:
  243. u_boot_console.log.warning('CONFIG_CMD_CRC32 != y: Skipping check')
  244. # Check if the command did not take too long
  245. if read_duration_max:
  246. elapsed = tend - tstart
  247. u_boot_console.log.info('Reading %d bytes took %f seconds' %
  248. (count_bytes, elapsed))
  249. assert elapsed <= (read_duration_max - 0.01)