xref: /openbmc/u-boot/test/py/tests/test_mmc_rd.py (revision 25fde1c0)
1# SPDX-License-Identifier: GPL-2.0
2# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3
4# Test U-Boot's "mmc read" command. The test reads data from the eMMC or SD
5# card, and validates the no errors occurred, and that the expected data was
6# read if the test configuration contains a CRC of the expected data.
7
8import pytest
9import u_boot_utils
10
11"""
12This test relies on boardenv_* to containing configuration values to define
13which MMC devices should be tested. For example:
14
15env__mmc_rd_configs = (
16    {
17        'fixture_id': 'emmc-boot0',
18        'is_emmc': True,
19        'devid': 0,
20        'partid': 1,
21        'sector': 0x10,
22        'count': 1,
23    },
24    {
25        'fixture_id': 'emmc-boot1',
26        'is_emmc': True,
27        'devid': 0,
28        'partid': 2,
29        'sector': 0x10,
30        'count': 1,
31    },
32    {
33        'fixture_id': 'emmc-data',
34        'is_emmc': True,
35        'devid': 0,
36        'partid': 0,
37        'sector': 0x10,
38        'count': 0x1000,
39    },
40    {
41        'fixture_id': 'sd-mbr',
42        'is_emmc': False,
43        'devid': 1,
44        'partid': None,
45        'sector': 0,
46        'count': 1,
47        'crc32': '8f6ecf0d',
48    },
49    {
50        'fixture_id': 'sd-large',
51        'is_emmc': False,
52        'devid': 1,
53        'partid': None,
54        'sector': 0x10,
55        'count': 0x1000,
56    },
57)
58"""
59
60@pytest.mark.buildconfigspec('cmd_mmc')
61def test_mmc_rd(u_boot_console, env__mmc_rd_config):
62    """Test the "mmc read" command.
63
64    Args:
65        u_boot_console: A U-Boot console connection.
66        env__mmc_rd_config: The single MMC configuration on which
67            to run the test. See the file-level comment above for details
68            of the format.
69
70    Returns:
71        Nothing.
72    """
73
74    is_emmc = env__mmc_rd_config['is_emmc']
75    devid = env__mmc_rd_config['devid']
76    partid = env__mmc_rd_config.get('partid', 0)
77    sector = env__mmc_rd_config.get('sector', 0)
78    count_sectors = env__mmc_rd_config.get('count', 1)
79    expected_crc32 = env__mmc_rd_config.get('crc32', None)
80
81    count_bytes = count_sectors * 512
82    bcfg = u_boot_console.config.buildconfig
83    has_cmd_memory = bcfg.get('config_cmd_memory', 'n') == 'y'
84    has_cmd_crc32 = bcfg.get('config_cmd_crc32', 'n') == 'y'
85    ram_base = u_boot_utils.find_ram_base(u_boot_console)
86    addr = '0x%08x' % ram_base
87
88    # Select MMC device
89    cmd = 'mmc dev %d' % devid
90    if is_emmc:
91        cmd += ' %d' % partid
92    response = u_boot_console.run_command(cmd)
93    assert 'no card present' not in response
94    if is_emmc:
95        partid_response = '(part %d)' % partid
96    else:
97        partid_response = ''
98    good_response = 'mmc%d%s is current device' % (devid, partid_response)
99    assert good_response in response
100
101    # Clear target RAM
102    if expected_crc32:
103        if has_cmd_memory and has_cmd_crc32:
104            cmd = 'mw.b %s 0 0x%x' % (addr, count_bytes)
105            u_boot_console.run_command(cmd)
106
107            cmd = 'crc32 %s 0x%x' % (addr, count_bytes)
108            response = u_boot_console.run_command(cmd)
109            assert expected_crc32 not in response
110        else:
111            u_boot_console.log.warning(
112                'CONFIG_CMD_MEMORY or CONFIG_CMD_CRC32 != y: Skipping RAM clear')
113
114    # Read data
115    cmd = 'mmc read %s %x %x' % (addr, sector, count_sectors)
116    response = u_boot_console.run_command(cmd)
117    good_response = 'MMC read: dev # %d, block # %d, count %d ... %d blocks read: OK' % (
118        devid, sector, count_sectors, count_sectors)
119    assert good_response in response
120
121    # Check target RAM
122    if expected_crc32:
123        if has_cmd_crc32:
124            cmd = 'crc32 %s 0x%x' % (addr, count_bytes)
125            response = u_boot_console.run_command(cmd)
126            assert expected_crc32 in response
127        else:
128            u_boot_console.log.warning('CONFIG_CMD_CRC32 != y: Skipping check')
129