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