blob: edd762e4d58f6f90867a9f936c21621f3c5c5f40 [file] [log] [blame]
Rasmus Villemoesceefc042023-03-02 09:12:25 +01001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Tests for read and write commands
4 */
5
Rasmus Villemoesceefc042023-03-02 09:12:25 +01006#include <dm/test.h>
7#include <mapmem.h>
8#include <part.h>
9#include <test/test.h>
10#include <test/ut.h>
11
12static int setup_partitions(struct unit_test_state *uts, struct blk_desc **mmc_dev_desc)
13{
14 char str_disk_guid[UUID_STR_LEN + 1];
15 struct disk_partition parts[2] = {
16 {
17 .start = 48, /* GPT data takes up the first 34 blocks or so */
18 .size = 4,
19 .name = "data",
20 },
21 {
22 .start = 52,
23 .size = 10,
24 .name = "log",
25 },
26 };
27
28 ut_asserteq(2, blk_get_device_by_str("mmc", "2", mmc_dev_desc));
29 if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
30 gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD);
31 gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD);
32 gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
33 }
34 ut_assertok(gpt_restore(*mmc_dev_desc, str_disk_guid, parts,
35 ARRAY_SIZE(parts)));
36 return 0;
37}
38
39/* Fill the write buffer with pseudo-random data, clear the read buffer. */
40static void init_buffers(char *rb, char *wb, size_t size, unsigned seed)
41{
42 memset(rb, 0, size);
43 while (size--) {
44 *wb++ = seed;
45 seed *= 43;
46 seed += 17 + size/4;
47 }
48}
49
50static int dm_test_read_write(struct unit_test_state *uts)
51{
52 struct blk_desc *dev_desc;
53 char wbuf[1024], rbuf[1024];
54 ulong wa, ra;
55
56#define INIT_BUFFERS() init_buffers(rbuf, wbuf, sizeof(rbuf), __LINE__)
57
58 ut_assertok(setup_partitions(uts, &dev_desc));
59
60 wa = map_to_sysmem(wbuf);
61 ra = map_to_sysmem(rbuf);
62
63 /* Simple test, write to/read from same partition. */
64 INIT_BUFFERS();
65 ut_assertok(run_commandf("write mmc 2:1 0x%lx 0 2", wa));
66 ut_assertok(run_commandf("read mmc 2:1 0x%lx 0 2", ra));
67 ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
68 ut_assertok(run_commandf("read mmc 2:1 0x%lx 1 1", ra));
69 ut_assertok(memcmp(&wbuf[512], rbuf, 512));
70
71 /* Use name for write, number for read. */
72 INIT_BUFFERS();
73 ut_assertok(run_commandf("write mmc 2#log 0x%lx 0 2", wa));
74 ut_assertok(run_commandf("read mmc 2:2 0x%lx 0 2", ra));
75 ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
76
77 /* Use full device for write, name for read. */
78 INIT_BUFFERS();
79 ut_assertok(run_commandf("write mmc 2:0 0x%lx 0x30 2", wa));
80 ut_assertok(run_commandf("read mmc 2#data 0x%lx 0 2", ra));
81 ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
82
83 /* Use name for write, full device for read */
84 INIT_BUFFERS();
85 ut_assertok(run_commandf("write mmc 2#log 0x%lx 1 2", wa));
86 ut_assertok(run_commandf("read mmc 2:0 0x%lx 0x35 2", ra));
87 ut_assertok(memcmp(wbuf, rbuf, sizeof(wbuf)));
88
89 /* Read/write outside partition bounds should be rejected upfront. */
90 console_record_reset_enable();
91 ut_asserteq(1, run_commandf("read mmc 2#data 0x%lx 3 2", ra));
92 ut_assert_nextlinen("read out of range");
93 ut_assert_console_end();
94
95 console_record_reset_enable();
96 ut_asserteq(1, run_commandf("write mmc 2#log 0x%lx 9 2", wa));
97 ut_assert_nextlinen("write out of range");
98 ut_assert_console_end();
99
100 return 0;
101}
102
103DM_TEST(dm_test_read_write, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);