blob: 355ba7770afa4c10b1195fbb56ba9ea4452a0b5f [file] [log] [blame]
Simon Glass499503e2022-10-29 19:47:19 -06001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright 2022 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#include <common.h>
8#include <blk.h>
9#include <dm.h>
10#include <fs.h>
11#include <sandbox_host.h>
12#include <asm/test.h>
13#include <dm/device-internal.h>
14#include <dm/test.h>
15#include <test/test.h>
16#include <test/ut.h>
17
18static const char filename[] = "2MB.ext2.img";
19static const char filename2[] = "1MB.fat32.img";
20
21/* Basic test of host interface */
22static int dm_test_host(struct unit_test_state *uts)
23{
24 static char label[] = "test";
25 struct udevice *dev, *part, *chk, *blk;
26 struct host_sb_plat *plat;
27 struct blk_desc *desc;
28 ulong mem_start;
29 loff_t actwrite;
30
31 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_HOST, &dev));
32 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
33
34 mem_start = ut_check_delta(0);
35 ut_assertok(host_create_device(label, true, &dev));
36
37 /* Check that the plat data has been allocated */
38 plat = dev_get_plat(dev);
39 ut_asserteq_str("test", plat->label);
40 ut_assert(label != plat->label);
41 ut_asserteq(0, plat->fd);
42
43 /* Attach a file created in test_host.py */
44 ut_assertok(host_attach_file(dev, filename));
45 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
46 ut_asserteq_ptr(chk, dev);
47
48 ut_asserteq_str(filename, plat->filename);
49 ut_assert(filename != plat->filename);
50 ut_assert(plat->fd != 0);
51
52 /* Get the block device */
53 ut_assertok(blk_get_from_parent(dev, &blk));
54 ut_assertok(device_probe(blk));
55
56 /* There should be no partition table in this device */
57 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
58
59 /* Write to a file on the ext4 filesystem */
60 desc = dev_get_uclass_plat(blk);
61 ut_asserteq(true, desc->removable);
62 ut_assertok(fs_set_blk_dev_with_part(desc, 0));
63 ut_assertok(fs_write("/testing", 0, 0, 0x1000, &actwrite));
64
65 ut_assertok(host_detach_file(dev));
66 ut_asserteq(0, plat->fd);
67 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
68 ut_assertok(device_unbind(dev));
69
70 /* check there were no memory leaks */
71 ut_asserteq(0, ut_check_delta(mem_start));
72
73 return 0;
74}
75DM_TEST(dm_test_host, UT_TESTF_SCAN_FDT);
76
77/* reusing the same label should work */
78static int dm_test_host_dup(struct unit_test_state *uts)
79{
80 static char label[] = "test";
81 struct udevice *dev, *chk;
82
83 ut_asserteq(0, uclass_id_count(UCLASS_HOST));
84 ut_assertok(host_create_device(label, true, &dev));
85
86 /* Attach a file created in test_host.py */
87 ut_assertok(host_attach_file(dev, filename));
88 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
89 ut_asserteq_ptr(chk, dev);
90 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
91
92 /* Create another device with the same label (should remove old one) */
93 ut_assertok(host_create_device(label, true, &dev));
94
95 /* Attach a different file created in test_host.py */
96 ut_assertok(host_attach_file(dev, filename2));
97 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
98 ut_asserteq_ptr(chk, dev);
99
100 /* Make sure there is still only one device */
101 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
102
103 return 0;
104}
105DM_TEST(dm_test_host_dup, UT_TESTF_SCAN_FDT);
106
107/* Basic test of 'host' command */
108static int dm_test_cmd_host(struct unit_test_state *uts)
109{
110 struct udevice *dev, *blk;
111 struct blk_desc *desc;
112
113 console_record_reset();
114
115 /* first check 'host info' with binding */
116 ut_assertok(run_command("host info", 0));
117 ut_assert_nextline("dev blocks label path");
118 ut_assert_console_end();
119
120 ut_assertok(run_commandf("host bind -r test2 %s", filename));
121
122 /* Check the -r flag worked */
123 ut_assertok(uclass_first_device_err(UCLASS_HOST, &dev));
124 ut_assertok(blk_get_from_parent(dev, &blk));
125 desc = dev_get_uclass_plat(blk);
126 ut_asserteq(true, desc->removable);
127
128 ut_assertok(run_command("host info", 0));
129 ut_assert_nextline("dev blocks label path");
130 ut_assert_nextline(" 0 4096 test2 2MB.ext2.img");
131 ut_assert_console_end();
132
133 ut_assertok(run_commandf("host bind fat %s", filename2));
134
Yuepeng Xing7943ae22022-12-02 14:23:07 +0800135 /* Check it is not removable (no '-r') */
Simon Glass499503e2022-10-29 19:47:19 -0600136 ut_assertok(uclass_next_device_err(&dev));
137 ut_assertok(blk_get_from_parent(dev, &blk));
138 desc = dev_get_uclass_plat(blk);
139 ut_asserteq(false, desc->removable);
140
141 ut_assertok(run_command("host info", 0));
142 ut_assert_nextline("dev blocks label path");
143 ut_assert_nextline(" 0 4096 test2 2MB.ext2.img");
144 ut_assert_nextline(" 1 2048 fat 1MB.fat32.img");
145 ut_assert_console_end();
146
147 ut_asserteq(1, run_command("host info test", 0));
148 ut_assert_nextline("No such device 'test'");
149 ut_assert_console_end();
150
151 ut_assertok(run_command("host info fat", 0));
152 ut_assert_nextline("dev blocks label path");
153 ut_assert_nextline(" 1 2048 fat 1MB.fat32.img");
154 ut_assert_console_end();
155
156 /* check 'host dev' */
157 ut_asserteq(1, run_command("host dev", 0));
158 ut_assert_nextline("No current host device");
159 ut_assert_console_end();
160
161 ut_asserteq(1, run_command("host dev missing", 0));
162 ut_assert_nextline("No such device 'missing'");
163 ut_assert_console_end();
164
165 ut_assertok(run_command("host dev fat", 0));
166 ut_assert_console_end();
167
168 ut_assertok(run_command("host dev", 0));
169 ut_assert_nextline("Current host device: 1: fat");
170 ut_assert_console_end();
171
172 /* Try a numerical label */
173 ut_assertok(run_command("host dev 0", 0));
174 ut_assert_console_end();
175
176 ut_assertok(run_command("host dev", 0));
177 ut_assert_nextline("Current host device: 0: test2");
178 ut_assert_console_end();
179
180 /* Remove one of the bindings */
181 ut_assertok(run_commandf("host unbind test2"));
182
183 /* There should now be no current device */
184 ut_asserteq(1, run_command("host dev", 0));
185 ut_assert_nextline("No current host device");
186 ut_assert_console_end();
187
188 ut_assertok(run_command("host info", 0));
189 ut_assert_nextline("dev blocks label path");
190 ut_assert_nextline(" 1 2048 fat 1MB.fat32.img");
191 ut_assert_console_end();
192
193 return 0;
194}
195DM_TEST(dm_test_cmd_host, UT_TESTF_SCAN_FDT);