blob: 1ba0cebbbe046a86796cb2132601ffa9d5a2b11c [file] [log] [blame]
Rasmus Villemoes7505d5b2024-01-03 11:47:07 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for memory 'cp' command
4 */
5
6#include <command.h>
7#include <console.h>
8#include <mapmem.h>
9#include <dm/test.h>
10#include <test/ut.h>
11
12#define BUF_SIZE 256
13
14/* Declare a new mem test */
15#define MEM_TEST(_name) UNIT_TEST(_name, 0, mem_test)
16
17struct param {
18 int d, s, count;
19};
20
21static int do_test(struct unit_test_state *uts,
22 const char *suffix, int d, int s, int count)
23{
24 const long addr = 0x1000;
25 u8 shadow[BUF_SIZE];
26 u8 *buf;
27 int i, w, bytes;
28
29 buf = map_sysmem(addr, BUF_SIZE);
30
31 /* Fill with distinct bytes. */
32 for (i = 0; i < BUF_SIZE; ++i)
33 buf[i] = shadow[i] = i;
34
35 /* Parameter sanity checking. */
36 w = cmd_get_data_size(suffix, 4);
37 ut_assert(w == 1 || w == 2 || w == 4 || (MEM_SUPPORT_64BIT_DATA && w == 8));
38
39 bytes = count * w;
40 ut_assert(d < BUF_SIZE);
41 ut_assert(d + bytes <= BUF_SIZE);
42 ut_assert(s < BUF_SIZE);
43 ut_assert(s + bytes <= BUF_SIZE);
44
45 /* This is exactly what we expect to happen to "buf" */
46 memmove(shadow + d, shadow + s, bytes);
47
48 run_commandf("cp%s 0x%lx 0x%lx 0x%x", suffix, addr + s, addr + d, count);
49
50 ut_asserteq(0, memcmp(buf, shadow, BUF_SIZE));
51
52 unmap_sysmem(buf);
53
54 return 0;
55}
56
57static int mem_test_cp_b(struct unit_test_state *uts)
58{
59 static const struct param tests[] = {
60 { 0, 128, 128 },
61 { 128, 0, 128 },
62 { 0, 16, 32 },
63 { 16, 0, 32 },
64 { 60, 100, 100 },
65 { 100, 60, 100 },
66 { 123, 54, 96 },
67 { 54, 123, 96 },
68 };
69 const struct param *p;
70 int ret, i;
71
72 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
73 p = &tests[i];
74 ret = do_test(uts, ".b", p->d, p->s, p->count);
75 if (ret)
76 return ret;
77 }
78
79 return 0;
80}
81MEM_TEST(mem_test_cp_b);
82
83static int mem_test_cp_w(struct unit_test_state *uts)
84{
85 static const struct param tests[] = {
86 { 0, 128, 64 },
87 { 128, 0, 64 },
88 { 0, 16, 16 },
89 { 16, 0, 16 },
90 { 60, 100, 50 },
91 { 100, 60, 50 },
92 { 123, 54, 48 },
93 { 54, 123, 48 },
94 };
95 const struct param *p;
96 int ret, i;
97
98 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
99 p = &tests[i];
100 ret = do_test(uts, ".w", p->d, p->s, p->count);
101 if (ret)
102 return ret;
103 }
104
105 return 0;
106}
107MEM_TEST(mem_test_cp_w);
108
109static int mem_test_cp_l(struct unit_test_state *uts)
110{
111 static const struct param tests[] = {
112 { 0, 128, 32 },
113 { 128, 0, 32 },
114 { 0, 16, 8 },
115 { 16, 0, 8 },
116 { 60, 100, 25 },
117 { 100, 60, 25 },
118 { 123, 54, 24 },
119 { 54, 123, 24 },
120 };
121 const struct param *p;
122 int ret, i;
123
124 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
125 p = &tests[i];
126 ret = do_test(uts, ".l", p->d, p->s, p->count);
127 if (ret)
128 return ret;
129 }
130
131 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
132 p = &tests[i];
133 ret = do_test(uts, "", p->d, p->s, p->count);
134 if (ret)
135 return ret;
136 }
137
138 return 0;
139}
140MEM_TEST(mem_test_cp_l);
141
142#if MEM_SUPPORT_64BIT_DATA
143static int mem_test_cp_q(struct unit_test_state *uts)
144{
145 static const struct param tests[] = {
146 { 0, 128, 16 },
147 { 128, 0, 16 },
148 { 0, 16, 8 },
149 { 16, 0, 8 },
150 { 60, 100, 15 },
151 { 100, 60, 15 },
152 { 123, 54, 12 },
153 { 54, 123, 12 },
154 };
155 const struct param *p;
156 int ret, i;
157
158 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
159 p = &tests[i];
160 ret = do_test(uts, ".q", p->d, p->s, p->count);
161 if (ret)
162 return ret;
163 }
164
165 return 0;
166}
167MEM_TEST(mem_test_cp_q);
168#endif