blob: 3c3e8efa92f2f6a76b990b70d84e150b982f1c3a [file] [log] [blame]
Johan Jonker500439e2023-10-18 16:01:59 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Test derived from:
4 * /test/dm/host.c
5 * Copyright 2022 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 *
8 * Copyright (C) 2023 Johan Jonker <jbx6244@gmail.com>
9 */
10
11#include <common.h>
12#include <blk.h>
13#include <dm.h>
14#include <fs.h>
15#include <rkmtd.h>
16#include <asm/test.h>
17#include <dm/device-internal.h>
18#include <dm/test.h>
19#include <test/test.h>
20#include <test/ut.h>
21
22#define RW_BUF_SIZE 12 * 512
23
24/* Basic test of the RKMTD interface */
25static int dm_test_rkmtd(struct unit_test_state *uts)
26{
27 struct udevice *dev, *part, *chk, *blk;
28 char write[RW_BUF_SIZE], read[RW_BUF_SIZE];
29 static const char label[] = "test";
30 struct rkmtd_dev *plat;
31 struct blk_desc *desc;
32 struct sector0 *sec0;
33 int i;
34
35 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_RKMTD, &dev));
36 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
37
38 ut_assertok(rkmtd_create_device(label, &dev));
39
40 /* Check that the plat data has been allocated */
41 plat = dev_get_plat(dev);
42 ut_asserteq_str("test", plat->label);
43 ut_assert(label != plat->label);
44
45 /* Attach RKMTD driver */
46 ut_assertok(rkmtd_attach(dev));
47 ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk));
48 ut_asserteq_ptr(chk, dev);
49
50 /* Get RKMTD block device */
51 ut_assertok(blk_get_from_parent(dev, &blk));
52 ut_assertok(device_probe(blk));
53
54 /* There should be a GPT partition table in this device */
55 ut_asserteq(0, uclass_first_device_err(UCLASS_PARTITION, &part));
56
57 /* Write a boot block and verify that we get the same data back */
58 desc = dev_get_uclass_plat(blk);
59 ut_asserteq(true, desc->removable);
60 ut_asserteq(LBA, desc->lba);
61
62 memset(write, '\0', BLK_SIZE);
63
64 for (i = BLK_SIZE; i < sizeof(write); i++)
65 write[i] = i;
66
67 sec0 = (struct sector0 *)write;
68 sec0->magic = 0x0FF0AA55;
69 sec0->rc4_flag = 0;
70 sec0->boot_code1_offset = 4;
71 sec0->boot_code2_offset = 4;
72 sec0->flash_data_size = 4;
73 sec0->flash_boot_size = 8;
74
75 rkmtd_rc4(write, 512);
76 ut_asserteq(RK_TAG, sec0->magic);
77
78 ut_asserteq(12, blk_dwrite(desc, 64, 12, write));
79 ut_asserteq(12, blk_dread(desc, 64, 12, read));
80 ut_asserteq_mem(write, read, RW_BUF_SIZE);
81
82 ut_assertok(rkmtd_detach(dev));
83
84 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
85 ut_assertok(device_unbind(dev));
86
87 return 0;
88}
89DM_TEST(dm_test_rkmtd, UT_TESTF_SCAN_FDT);
90
91/* Reusing the same label should work */
92static int dm_test_rkmtd_dup(struct unit_test_state *uts)
93{
94 static const char label[] = "test";
95 struct udevice *dev, *chk;
96
97 /* Create a RKMTD device with label "test" */
98 ut_asserteq(0, uclass_id_count(UCLASS_RKMTD));
99 ut_assertok(rkmtd_create_device(label, &dev));
100 ut_assertok(rkmtd_attach(dev));
101 ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk));
102 ut_asserteq_ptr(chk, dev);
103 ut_asserteq(1, uclass_id_count(UCLASS_RKMTD));
104
105 /* Create another device with the same label (should remove old one) */
106 ut_assertok(rkmtd_create_device(label, &dev));
107 ut_assertok(rkmtd_attach(dev));
108 ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk));
109 ut_asserteq_ptr(chk, dev);
110
111 /* Make sure there is still only one device */
112 ut_asserteq(1, uclass_id_count(UCLASS_RKMTD));
113
114 return 0;
115}
116DM_TEST(dm_test_rkmtd_dup, UT_TESTF_SCAN_FDT);
117
118/* Basic test of the 'rkmtd' command */
119static int dm_test_rkmtd_cmd(struct unit_test_state *uts)
120{
121 struct udevice *dev, *blk;
122 struct blk_desc *desc;
123
124 /* First check 'rkmtd info' with binding */
125 ut_assertok(run_command("rkmtd info", 0));
126 ut_assert_nextline("dev blocks label ");
127 ut_assert_console_end();
128
129 /* Bind device 1 */
130 ut_assertok(run_commandf("rkmtd bind test1"));
131 ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &dev));
132 ut_assertok(blk_get_from_parent(dev, &blk));
133 desc = dev_get_uclass_plat(blk);
134
135 ut_assertok(run_command("rkmtd info", 0));
136 ut_assert_nextline("dev blocks label ");
137 ut_assert_nextline(" 0 609 test1 ");
138 ut_assert_console_end();
139
140 /* Bind device 2 */
141 ut_assertok(run_commandf("rkmtd bind test2"));
142 ut_assertok(uclass_next_device_err(&dev));
143 ut_assertok(blk_get_from_parent(dev, &blk));
144 desc = dev_get_uclass_plat(blk);
145
146 ut_assertok(run_command("rkmtd info", 0));
147 ut_assert_nextline("dev blocks label ");
148 ut_assert_nextline(" 0 609 test1 ");
149 ut_assert_nextline(" 1 609 test2 ");
150 ut_assert_console_end();
151
152 ut_asserteq(1, run_command("rkmtd info test", 0));
153 ut_assert_nextline("No such device 'test'");
154 ut_assert_console_end();
155
156 ut_assertok(run_command("rkmtd info test2", 0));
157 ut_assert_nextline("dev blocks label ");
158 ut_assert_nextline(" 1 609 test2 ");
159 ut_assert_console_end();
160
161 /* Check 'rkmtd dev' */
162 ut_asserteq(1, run_command("rkmtd dev", 0));
163 ut_assert_nextline("No current rkmtd device");
164 ut_assert_console_end();
165
166 ut_asserteq(1, run_command("rkmtd dev missing", 0));
167 ut_assert_nextline("No such device 'missing'");
168 ut_assert_console_end();
169
170 ut_assertok(run_command("rkmtd dev test2", 0));
171 ut_assert_console_end();
172
173 ut_assertok(run_command("rkmtd dev", 0));
174 ut_assert_nextline("Current rkmtd device: 1: test2");
175 ut_assert_console_end();
176
177 /* Try a numerical label */
178 ut_assertok(run_command("rkmtd dev 0", 0));
179 ut_assert_console_end();
180
181 ut_assertok(run_command("rkmtd dev", 0));
182 ut_assert_nextline("Current rkmtd device: 0: test1");
183 ut_assert_console_end();
184
185 /* Remove one of the bindings */
186 ut_assertok(run_commandf("rkmtd unbind test1"));
187
188 /* There should now be no current device */
189 ut_asserteq(1, run_command("rkmtd dev", 0));
190 ut_assert_nextline("No current rkmtd device");
191 ut_assert_console_end();
192
193 ut_assertok(run_command("rkmtd info", 0));
194 ut_assert_nextline("dev blocks label ");
195 ut_assert_nextline(" 1 609 test2 ");
196 ut_assert_console_end();
197
198 return 0;
199}
200DM_TEST(dm_test_rkmtd_cmd, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);