blob: e514f8409cf6e42f6e287c8d307c2104971c53fe [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
Simon Glass499503e2022-10-29 19:47:19 -06007#include <blk.h>
8#include <dm.h>
9#include <fs.h>
Simon Glass00613bc2023-08-24 13:55:38 -060010#include <os.h>
Simon Glass499503e2022-10-29 19:47:19 -060011#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
Simon Glass499503e2022-10-29 19:47:19 -060018/* Basic test of host interface */
19static int dm_test_host(struct unit_test_state *uts)
20{
21 static char label[] = "test";
22 struct udevice *dev, *part, *chk, *blk;
23 struct host_sb_plat *plat;
24 struct blk_desc *desc;
Simon Glass00613bc2023-08-24 13:55:38 -060025 char fname[256];
Simon Glass499503e2022-10-29 19:47:19 -060026 ulong mem_start;
27 loff_t actwrite;
28
29 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_HOST, &dev));
30 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
31
32 mem_start = ut_check_delta(0);
Bin Meng8897fab2023-09-26 16:43:33 +080033 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass499503e2022-10-29 19:47:19 -060034
35 /* Check that the plat data has been allocated */
36 plat = dev_get_plat(dev);
37 ut_asserteq_str("test", plat->label);
38 ut_assert(label != plat->label);
39 ut_asserteq(0, plat->fd);
40
Simon Glass00613bc2023-08-24 13:55:38 -060041 /* Attach a file created in test_ut_dm_init */
42 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
43
44 ut_assertok(host_attach_file(dev, fname));
Simon Glass499503e2022-10-29 19:47:19 -060045 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
46 ut_asserteq_ptr(chk, dev);
47
Simon Glass00613bc2023-08-24 13:55:38 -060048 ut_asserteq_str(fname, plat->filename);
49 ut_assert(fname != plat->filename);
Simon Glass499503e2022-10-29 19:47:19 -060050 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;
Simon Glass00613bc2023-08-24 13:55:38 -060082 char fname[256];
Simon Glass499503e2022-10-29 19:47:19 -060083
84 ut_asserteq(0, uclass_id_count(UCLASS_HOST));
Bin Meng8897fab2023-09-26 16:43:33 +080085 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass499503e2022-10-29 19:47:19 -060086
Simon Glass00613bc2023-08-24 13:55:38 -060087 /* Attach a file created in test_ut_dm_init */
88 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
89 ut_assertok(host_attach_file(dev, fname));
Simon Glass499503e2022-10-29 19:47:19 -060090 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
91 ut_asserteq_ptr(chk, dev);
92 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
93
94 /* Create another device with the same label (should remove old one) */
Bin Meng8897fab2023-09-26 16:43:33 +080095 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass499503e2022-10-29 19:47:19 -060096
Simon Glass00613bc2023-08-24 13:55:38 -060097 /* Attach a different file created in test_ut_dm_init */
98 ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img"));
99 ut_assertok(host_attach_file(dev, fname));
100
Simon Glass499503e2022-10-29 19:47:19 -0600101 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
102 ut_asserteq_ptr(chk, dev);
103
104 /* Make sure there is still only one device */
105 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
106
107 return 0;
108}
109DM_TEST(dm_test_host_dup, UT_TESTF_SCAN_FDT);
110
111/* Basic test of 'host' command */
112static int dm_test_cmd_host(struct unit_test_state *uts)
113{
114 struct udevice *dev, *blk;
115 struct blk_desc *desc;
Simon Glass00613bc2023-08-24 13:55:38 -0600116 char fname[256];
Simon Glass499503e2022-10-29 19:47:19 -0600117
118 console_record_reset();
119
120 /* first check 'host info' with binding */
121 ut_assertok(run_command("host info", 0));
Bin Meng256f6da2023-09-26 16:43:36 +0800122 ut_assert_nextline("dev blocks blksz label path");
Simon Glass499503e2022-10-29 19:47:19 -0600123 ut_assert_console_end();
124
Simon Glass00613bc2023-08-24 13:55:38 -0600125 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
126 ut_assertok(run_commandf("host bind -r test2 %s", fname));
Simon Glass499503e2022-10-29 19:47:19 -0600127
128 /* Check the -r flag worked */
129 ut_assertok(uclass_first_device_err(UCLASS_HOST, &dev));
130 ut_assertok(blk_get_from_parent(dev, &blk));
131 desc = dev_get_uclass_plat(blk);
132 ut_asserteq(true, desc->removable);
133
134 ut_assertok(run_command("host info", 0));
Bin Meng256f6da2023-09-26 16:43:36 +0800135 ut_assert_nextline("dev blocks blksz label path");
136 ut_assert_nextlinen(" 0 4096 512 test2");
Simon Glass499503e2022-10-29 19:47:19 -0600137 ut_assert_console_end();
138
Simon Glass00613bc2023-08-24 13:55:38 -0600139 ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img"));
140 ut_assertok(run_commandf("host bind fat %s", fname));
Simon Glass499503e2022-10-29 19:47:19 -0600141
Yuepeng Xing7943ae22022-12-02 14:23:07 +0800142 /* Check it is not removable (no '-r') */
Simon Glass499503e2022-10-29 19:47:19 -0600143 ut_assertok(uclass_next_device_err(&dev));
144 ut_assertok(blk_get_from_parent(dev, &blk));
145 desc = dev_get_uclass_plat(blk);
146 ut_asserteq(false, desc->removable);
147
148 ut_assertok(run_command("host info", 0));
Bin Meng256f6da2023-09-26 16:43:36 +0800149 ut_assert_nextline("dev blocks blksz label path");
150 ut_assert_nextlinen(" 0 4096 512 test2");
151 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass499503e2022-10-29 19:47:19 -0600152 ut_assert_console_end();
153
154 ut_asserteq(1, run_command("host info test", 0));
155 ut_assert_nextline("No such device 'test'");
156 ut_assert_console_end();
157
158 ut_assertok(run_command("host info fat", 0));
Bin Meng256f6da2023-09-26 16:43:36 +0800159 ut_assert_nextline("dev blocks blksz label path");
160 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass499503e2022-10-29 19:47:19 -0600161 ut_assert_console_end();
162
163 /* check 'host dev' */
164 ut_asserteq(1, run_command("host dev", 0));
165 ut_assert_nextline("No current host device");
166 ut_assert_console_end();
167
168 ut_asserteq(1, run_command("host dev missing", 0));
169 ut_assert_nextline("No such device 'missing'");
170 ut_assert_console_end();
171
172 ut_assertok(run_command("host dev fat", 0));
173 ut_assert_console_end();
174
175 ut_assertok(run_command("host dev", 0));
176 ut_assert_nextline("Current host device: 1: fat");
177 ut_assert_console_end();
178
179 /* Try a numerical label */
180 ut_assertok(run_command("host dev 0", 0));
181 ut_assert_console_end();
182
183 ut_assertok(run_command("host dev", 0));
184 ut_assert_nextline("Current host device: 0: test2");
185 ut_assert_console_end();
186
187 /* Remove one of the bindings */
188 ut_assertok(run_commandf("host unbind test2"));
189
190 /* There should now be no current device */
191 ut_asserteq(1, run_command("host dev", 0));
192 ut_assert_nextline("No current host device");
193 ut_assert_console_end();
194
195 ut_assertok(run_command("host info", 0));
Bin Meng256f6da2023-09-26 16:43:36 +0800196 ut_assert_nextline("dev blocks blksz label path");
197 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass499503e2022-10-29 19:47:19 -0600198 ut_assert_console_end();
199
200 return 0;
201}
202DM_TEST(dm_test_cmd_host, UT_TESTF_SCAN_FDT);