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