test_sf.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2016, Xilinx Inc. Michal Simek
  3. # Copyright (c) 2017, Xiphos Systems Corp. All rights reserved.
  4. import re
  5. import pytest
  6. import random
  7. import u_boot_utils
  8. """
  9. Note: This test relies on boardenv_* containing configuration values to define
  10. which SPI Flash areas are available for testing. Without this, this test will
  11. be automatically skipped.
  12. For example:
  13. # A list of sections of Flash memory to be tested.
  14. env__sf_configs = (
  15. {
  16. # Where in SPI Flash should the test operate.
  17. 'offset': 0x00000000,
  18. # This value is optional.
  19. # If present, specifies the [[bus:]cs] argument used in `sf probe`
  20. # If missing, defaults to 0.
  21. 'id': '0:1',
  22. # This value is optional.
  23. # If set as a number, specifies the speed of the SPI Flash.
  24. # If set as an array of 2, specifies a range for a random speed.
  25. # If missing, defaults to 0.
  26. 'speed': 1000000,
  27. # This value is optional.
  28. # If present, specifies the size to use for read/write operations.
  29. # If missing, the SPI Flash page size is used as a default (based on
  30. # the `sf probe` output).
  31. 'len': 0x10000,
  32. # This value is optional.
  33. # If present, specifies if the test can write to Flash offset
  34. # If missing, defaults to False.
  35. 'writeable': False,
  36. # This value is optional.
  37. # If present, specifies the expected CRC32 value of the flash area.
  38. # If missing, extra check is ignored.
  39. 'crc32': 0xCAFECAFE,
  40. },
  41. )
  42. """
  43. def sf_prepare(u_boot_console, env__sf_config):
  44. """Check global state of the SPI Flash before running any test.
  45. Args:
  46. u_boot_console: A U-Boot console connection.
  47. env__sf_config: The single SPI Flash device configuration on which to
  48. run the tests.
  49. Returns:
  50. sf_params: a dictionary of SPI Flash parameters.
  51. """
  52. sf_params = {}
  53. sf_params['ram_base'] = u_boot_utils.find_ram_base(u_boot_console)
  54. probe_id = env__sf_config.get('id', 0)
  55. speed = env__sf_config.get('speed', 0)
  56. if isinstance(speed, int):
  57. sf_params['speed'] = speed
  58. else:
  59. assert len(speed) == 2, "If speed is a list, it must have 2 entries"
  60. sf_params['speed'] = random.randint(speed[0], speed[1])
  61. cmd = 'sf probe %d %d' % (probe_id, sf_params['speed'])
  62. output = u_boot_console.run_command(cmd)
  63. assert 'SF: Detected' in output, 'No Flash device available'
  64. m = re.search('page size (.+?) Bytes', output)
  65. assert m, 'SPI Flash page size not recognized'
  66. sf_params['page_size'] = int(m.group(1))
  67. m = re.search('erase size (.+?) KiB', output)
  68. assert m, 'SPI Flash erase size not recognized'
  69. sf_params['erase_size'] = int(m.group(1))
  70. sf_params['erase_size'] *= 1024
  71. m = re.search('total (.+?) MiB', output)
  72. assert m, 'SPI Flash total size not recognized'
  73. sf_params['total_size'] = int(m.group(1))
  74. sf_params['total_size'] *= 1024 * 1024
  75. assert 'offset' in env__sf_config, \
  76. '\'offset\' is required for this test.'
  77. sf_params['len'] = env__sf_config.get('len', sf_params['erase_size'])
  78. assert not env__sf_config['offset'] % sf_params['erase_size'], \
  79. 'offset not multiple of erase size.'
  80. assert not sf_params['len'] % sf_params['erase_size'], \
  81. 'erase length not multiple of erase size.'
  82. assert not (env__sf_config.get('writeable', False) and
  83. 'crc32' in env__sf_config), \
  84. 'Cannot check crc32 on writeable sections'
  85. return sf_params
  86. def sf_read(u_boot_console, env__sf_config, sf_params):
  87. """Helper function used to read and compute the CRC32 value of a section of
  88. SPI Flash memory.
  89. Args:
  90. u_boot_console: A U-Boot console connection.
  91. env__sf_config: The single SPI Flash device configuration on which to
  92. run the tests.
  93. sf_params: SPI Flash parameters.
  94. Returns:
  95. CRC32 value of SPI Flash section
  96. """
  97. addr = sf_params['ram_base']
  98. offset = env__sf_config['offset']
  99. count = sf_params['len']
  100. pattern = random.randint(0, 0xFF)
  101. crc_expected = env__sf_config.get('crc32', None)
  102. cmd = 'mw.b %08x %02x %x' % (addr, pattern, count)
  103. u_boot_console.run_command(cmd)
  104. crc_pattern = u_boot_utils.crc32(u_boot_console, addr, count)
  105. if crc_expected:
  106. assert crc_pattern != crc_expected
  107. cmd = 'sf read %08x %08x %x' % (addr, offset, count)
  108. response = u_boot_console.run_command(cmd)
  109. assert 'Read: OK' in response, 'Read operation failed'
  110. crc_readback = u_boot_utils.crc32(u_boot_console, addr, count)
  111. assert crc_pattern != crc_readback, 'sf read did not update RAM content.'
  112. if crc_expected:
  113. assert crc_readback == crc_expected
  114. return crc_readback
  115. def sf_update(u_boot_console, env__sf_config, sf_params):
  116. """Helper function used to update a section of SPI Flash memory.
  117. Args:
  118. u_boot_console: A U-Boot console connection.
  119. env__sf_config: The single SPI Flash device configuration on which to
  120. run the tests.
  121. Returns:
  122. CRC32 value of SPI Flash section
  123. """
  124. addr = sf_params['ram_base']
  125. offset = env__sf_config['offset']
  126. count = sf_params['len']
  127. pattern = int(random.random() * 0xFF)
  128. cmd = 'mw.b %08x %02x %x' % (addr, pattern, count)
  129. u_boot_console.run_command(cmd)
  130. crc_pattern = u_boot_utils.crc32(u_boot_console, addr, count)
  131. cmd = 'sf update %08x %08x %x' % (addr, offset, count)
  132. u_boot_console.run_command(cmd)
  133. crc_readback = sf_read(u_boot_console, env__sf_config, sf_params)
  134. assert crc_readback == crc_pattern
  135. @pytest.mark.buildconfigspec('cmd_sf')
  136. @pytest.mark.buildconfigspec('cmd_crc32')
  137. @pytest.mark.buildconfigspec('cmd_memory')
  138. def test_sf_read(u_boot_console, env__sf_config):
  139. sf_params = sf_prepare(u_boot_console, env__sf_config)
  140. sf_read(u_boot_console, env__sf_config, sf_params)
  141. @pytest.mark.buildconfigspec('cmd_sf')
  142. @pytest.mark.buildconfigspec('cmd_crc32')
  143. @pytest.mark.buildconfigspec('cmd_memory')
  144. def test_sf_read_twice(u_boot_console, env__sf_config):
  145. sf_params = sf_prepare(u_boot_console, env__sf_config)
  146. crc1 = sf_read(u_boot_console, env__sf_config, sf_params)
  147. sf_params['ram_base'] += 0x100
  148. crc2 = sf_read(u_boot_console, env__sf_config, sf_params)
  149. assert crc1 == crc2, 'CRC32 of two successive read operation do not match'
  150. @pytest.mark.buildconfigspec('cmd_sf')
  151. @pytest.mark.buildconfigspec('cmd_crc32')
  152. @pytest.mark.buildconfigspec('cmd_memory')
  153. def test_sf_erase(u_boot_console, env__sf_config):
  154. if not env__sf_config.get('writeable', False):
  155. pytest.skip('Flash config is tagged as not writeable')
  156. sf_params = sf_prepare(u_boot_console, env__sf_config)
  157. addr = sf_params['ram_base']
  158. offset = env__sf_config['offset']
  159. count = sf_params['len']
  160. cmd = 'sf erase %08x %x' % (offset, count)
  161. output = u_boot_console.run_command(cmd)
  162. assert 'Erased: OK' in output, 'Erase operation failed'
  163. cmd = 'mw.b %08x ff %x' % (addr, count)
  164. u_boot_console.run_command(cmd)
  165. crc_ffs = u_boot_utils.crc32(u_boot_console, addr, count)
  166. crc_read = sf_read(u_boot_console, env__sf_config, sf_params)
  167. assert crc_ffs == crc_read, 'Unexpected CRC32 after erase operation.'
  168. @pytest.mark.buildconfigspec('cmd_sf')
  169. @pytest.mark.buildconfigspec('cmd_crc32')
  170. @pytest.mark.buildconfigspec('cmd_memory')
  171. def test_sf_update(u_boot_console, env__sf_config):
  172. if not env__sf_config.get('writeable', False):
  173. pytest.skip('Flash config is tagged as not writeable')
  174. sf_params = sf_prepare(u_boot_console, env__sf_config)
  175. sf_update(u_boot_console, env__sf_config, sf_params)