aboutsummaryrefslogtreecommitdiff
path: root/test/dm/rkmtd.c
blob: 3dc9ca1add11a8b60de81cea70cd038d7db94ae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
/*
 * Test derived from:
 * /test/dm/host.c
 * Copyright 2022 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 *
 * Copyright (C) 2023 Johan Jonker <jbx6244@gmail.com>
 */

#include <blk.h>
#include <dm.h>
#include <fs.h>
#include <rkmtd.h>
#include <asm/test.h>
#include <dm/device-internal.h>
#include <dm/test.h>
#include <test/test.h>
#include <test/ut.h>

#define RW_BUF_SIZE	12 * 512

/* Basic test of the RKMTD interface */
static int dm_test_rkmtd(struct unit_test_state *uts)
{
	struct udevice *dev, *part, *chk, *blk;
	char write[RW_BUF_SIZE], read[RW_BUF_SIZE];
	static const char label[] = "test";
	struct rkmtd_dev *plat;
	struct blk_desc *desc;
	struct sector0 *sec0;
	int i;

	ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_RKMTD, &dev));
	ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));

	ut_assertok(rkmtd_create_device(label, &dev));

	/* Check that the plat data has been allocated */
	plat = dev_get_plat(dev);
	ut_asserteq_str("test", plat->label);
	ut_assert(label != plat->label);

	/* Attach RKMTD driver */
	ut_assertok(rkmtd_attach(dev));
	ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk));
	ut_asserteq_ptr(chk, dev);

	/* Get RKMTD block device */
	ut_assertok(blk_get_from_parent(dev, &blk));
	ut_assertok(device_probe(blk));

	/* There should be a GPT partition table in this device */
	ut_asserteq(0, uclass_first_device_err(UCLASS_PARTITION, &part));

	/* Write a boot block and verify that we get the same data back */
	desc = dev_get_uclass_plat(blk);
	ut_asserteq(true, desc->removable);
	ut_asserteq(LBA, desc->lba);

	memset(write, '\0', BLK_SIZE);

	for (i = BLK_SIZE; i < sizeof(write); i++)
		write[i] = i;

	sec0 = (struct sector0 *)write;
	sec0->magic = 0x0FF0AA55;
	sec0->rc4_flag = 0;
	sec0->boot_code1_offset = 4;
	sec0->boot_code2_offset = 4;
	sec0->flash_data_size = 4;
	sec0->flash_boot_size = 8;

	rkmtd_rc4(write, 512);
	ut_asserteq(RK_TAG, sec0->magic);

	ut_asserteq(12, blk_dwrite(desc, 64, 12, write));
	ut_asserteq(12, blk_dread(desc, 64, 12, read));
	ut_asserteq_mem(write, read, RW_BUF_SIZE);

	ut_assertok(rkmtd_detach(dev));

	ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
	ut_assertok(device_unbind(dev));

	return 0;
}
DM_TEST(dm_test_rkmtd, UT_TESTF_SCAN_FDT);

/* Reusing the same label should work */
static int dm_test_rkmtd_dup(struct unit_test_state *uts)
{
	static const char label[] = "test";
	struct udevice *dev, *chk;

	/* Create a RKMTD device with label "test" */
	ut_asserteq(0, uclass_id_count(UCLASS_RKMTD));
	ut_assertok(rkmtd_create_device(label, &dev));
	ut_assertok(rkmtd_attach(dev));
	ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk));
	ut_asserteq_ptr(chk, dev);
	ut_asserteq(1, uclass_id_count(UCLASS_RKMTD));

	/* Create another device with the same label (should remove old one) */
	ut_assertok(rkmtd_create_device(label, &dev));
	ut_assertok(rkmtd_attach(dev));
	ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk));
	ut_asserteq_ptr(chk, dev);

	/* Make sure there is still only one device */
	ut_asserteq(1, uclass_id_count(UCLASS_RKMTD));

	return 0;
}
DM_TEST(dm_test_rkmtd_dup, UT_TESTF_SCAN_FDT);

/* Basic test of the 'rkmtd' command */
static int dm_test_rkmtd_cmd(struct unit_test_state *uts)
{
	struct udevice *dev, *blk;
	struct blk_desc *desc;

	/* First check 'rkmtd info' with binding */
	ut_assertok(run_command("rkmtd info", 0));
	ut_assert_nextline("dev       blocks label          ");
	ut_assert_console_end();

	/* Bind device 1 */
	ut_assertok(run_commandf("rkmtd bind test1"));
	ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &dev));
	ut_assertok(blk_get_from_parent(dev, &blk));
	desc = dev_get_uclass_plat(blk);

	ut_assertok(run_command("rkmtd info", 0));
	ut_assert_nextline("dev       blocks label          ");
	ut_assert_nextline("  0          609 test1          ");
	ut_assert_console_end();

	/* Bind device 2 */
	ut_assertok(run_commandf("rkmtd bind test2"));
	ut_assertok(uclass_next_device_err(&dev));
	ut_assertok(blk_get_from_parent(dev, &blk));
	desc = dev_get_uclass_plat(blk);

	ut_assertok(run_command("rkmtd info", 0));
	ut_assert_nextline("dev       blocks label          ");
	ut_assert_nextline("  0          609 test1          ");
	ut_assert_nextline("  1          609 test2          ");
	ut_assert_console_end();

	ut_asserteq(1, run_command("rkmtd info test", 0));
	ut_assert_nextline("No such device 'test'");
	ut_assert_console_end();

	ut_assertok(run_command("rkmtd info test2", 0));
	ut_assert_nextline("dev       blocks label          ");
	ut_assert_nextline("  1          609 test2          ");
	ut_assert_console_end();

	/* Check 'rkmtd dev' */
	ut_asserteq(1, run_command("rkmtd dev", 0));
	ut_assert_nextline("No current rkmtd device");
	ut_assert_console_end();

	ut_asserteq(1, run_command("rkmtd dev missing", 0));
	ut_assert_nextline("No such device 'missing'");
	ut_assert_console_end();

	ut_assertok(run_command("rkmtd dev test2", 0));
	ut_assert_console_end();

	ut_assertok(run_command("rkmtd dev", 0));
	ut_assert_nextline("Current rkmtd device: 1: test2");
	ut_assert_console_end();

	/* Try a numerical label */
	ut_assertok(run_command("rkmtd dev 0", 0));
	ut_assert_console_end();

	ut_assertok(run_command("rkmtd dev", 0));
	ut_assert_nextline("Current rkmtd device: 0: test1");
	ut_assert_console_end();

	/* Remove one of the bindings */
	ut_assertok(run_commandf("rkmtd unbind test1"));

	/* There should now be no current device */
	ut_asserteq(1, run_command("rkmtd dev", 0));
	ut_assert_nextline("No current rkmtd device");
	ut_assert_console_end();

	ut_assertok(run_command("rkmtd info", 0));
	ut_assert_nextline("dev       blocks label          ");
	ut_assert_nextline("  1          609 test2          ");
	ut_assert_console_end();

	return 0;
}
DM_TEST(dm_test_rkmtd_cmd, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);