blob: f577377da6a0fb0a6ab1a500836b6e3dd16c7131 [file] [log] [blame]
Simon Glass2c7b0e42022-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 Glass2c7b0e42022-10-29 19:47:19 -06007#include <blk.h>
8#include <dm.h>
9#include <fs.h>
Simon Glass08709212023-08-24 13:55:38 -060010#include <os.h>
Simon Glass2c7b0e42022-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 Glass2c7b0e42022-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 Glass08709212023-08-24 13:55:38 -060025 char fname[256];
Simon Glass2c7b0e42022-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 Meng881e4752023-09-26 16:43:33 +080033 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass2c7b0e42022-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 Glass08709212023-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 Glass2c7b0e42022-10-29 19:47:19 -060045 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
46 ut_asserteq_ptr(chk, dev);
47
Simon Glass08709212023-08-24 13:55:38 -060048 ut_asserteq_str(fname, plat->filename);
49 ut_assert(fname != plat->filename);
Simon Glass2c7b0e42022-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}
Simon Glass1a92f832024-08-22 07:57:48 -060075DM_TEST(dm_test_host, UTF_SCAN_FDT);
Simon Glass2c7b0e42022-10-29 19:47:19 -060076
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 Glass08709212023-08-24 13:55:38 -060082 char fname[256];
Simon Glass2c7b0e42022-10-29 19:47:19 -060083
84 ut_asserteq(0, uclass_id_count(UCLASS_HOST));
Bin Meng881e4752023-09-26 16:43:33 +080085 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass2c7b0e42022-10-29 19:47:19 -060086
Simon Glass08709212023-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 Glass2c7b0e42022-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 Meng881e4752023-09-26 16:43:33 +080095 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass2c7b0e42022-10-29 19:47:19 -060096
Simon Glass08709212023-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 Glass2c7b0e42022-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}
Simon Glass1a92f832024-08-22 07:57:48 -0600109DM_TEST(dm_test_host_dup, UTF_SCAN_FDT);
Simon Glass2c7b0e42022-10-29 19:47:19 -0600110
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 Glass08709212023-08-24 13:55:38 -0600116 char fname[256];
Simon Glass2c7b0e42022-10-29 19:47:19 -0600117
Simon Glass2c7b0e42022-10-29 19:47:19 -0600118 /* first check 'host info' with binding */
119 ut_assertok(run_command("host info", 0));
Bin Mengc4373782023-09-26 16:43:36 +0800120 ut_assert_nextline("dev blocks blksz label path");
Simon Glass2c7b0e42022-10-29 19:47:19 -0600121 ut_assert_console_end();
122
Simon Glass08709212023-08-24 13:55:38 -0600123 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
124 ut_assertok(run_commandf("host bind -r test2 %s", fname));
Simon Glass2c7b0e42022-10-29 19:47:19 -0600125
126 /* Check the -r flag worked */
127 ut_assertok(uclass_first_device_err(UCLASS_HOST, &dev));
128 ut_assertok(blk_get_from_parent(dev, &blk));
129 desc = dev_get_uclass_plat(blk);
130 ut_asserteq(true, desc->removable);
131
132 ut_assertok(run_command("host info", 0));
Bin Mengc4373782023-09-26 16:43:36 +0800133 ut_assert_nextline("dev blocks blksz label path");
134 ut_assert_nextlinen(" 0 4096 512 test2");
Simon Glass2c7b0e42022-10-29 19:47:19 -0600135 ut_assert_console_end();
136
Simon Glass08709212023-08-24 13:55:38 -0600137 ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img"));
138 ut_assertok(run_commandf("host bind fat %s", fname));
Simon Glass2c7b0e42022-10-29 19:47:19 -0600139
Yuepeng Xing71cc0212022-12-02 14:23:07 +0800140 /* Check it is not removable (no '-r') */
Simon Glass2c7b0e42022-10-29 19:47:19 -0600141 ut_assertok(uclass_next_device_err(&dev));
142 ut_assertok(blk_get_from_parent(dev, &blk));
143 desc = dev_get_uclass_plat(blk);
144 ut_asserteq(false, desc->removable);
145
146 ut_assertok(run_command("host info", 0));
Bin Mengc4373782023-09-26 16:43:36 +0800147 ut_assert_nextline("dev blocks blksz label path");
148 ut_assert_nextlinen(" 0 4096 512 test2");
149 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass2c7b0e42022-10-29 19:47:19 -0600150 ut_assert_console_end();
151
152 ut_asserteq(1, run_command("host info test", 0));
153 ut_assert_nextline("No such device 'test'");
154 ut_assert_console_end();
155
156 ut_assertok(run_command("host info fat", 0));
Bin Mengc4373782023-09-26 16:43:36 +0800157 ut_assert_nextline("dev blocks blksz label path");
158 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass2c7b0e42022-10-29 19:47:19 -0600159 ut_assert_console_end();
160
161 /* check 'host dev' */
162 ut_asserteq(1, run_command("host dev", 0));
163 ut_assert_nextline("No current host device");
164 ut_assert_console_end();
165
166 ut_asserteq(1, run_command("host dev missing", 0));
167 ut_assert_nextline("No such device 'missing'");
168 ut_assert_console_end();
169
170 ut_assertok(run_command("host dev fat", 0));
171 ut_assert_console_end();
172
173 ut_assertok(run_command("host dev", 0));
174 ut_assert_nextline("Current host device: 1: fat");
175 ut_assert_console_end();
176
177 /* Try a numerical label */
178 ut_assertok(run_command("host dev 0", 0));
179 ut_assert_console_end();
180
181 ut_assertok(run_command("host dev", 0));
182 ut_assert_nextline("Current host device: 0: test2");
183 ut_assert_console_end();
184
185 /* Remove one of the bindings */
186 ut_assertok(run_commandf("host unbind test2"));
187
188 /* There should now be no current device */
189 ut_asserteq(1, run_command("host dev", 0));
190 ut_assert_nextline("No current host device");
191 ut_assert_console_end();
192
193 ut_assertok(run_command("host info", 0));
Bin Mengc4373782023-09-26 16:43:36 +0800194 ut_assert_nextline("dev blocks blksz label path");
195 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass2c7b0e42022-10-29 19:47:19 -0600196 ut_assert_console_end();
197
198 return 0;
199}
Simon Glassf54458b2024-08-22 07:57:59 -0600200DM_TEST(dm_test_cmd_host, UTF_SCAN_FDT | UTF_CONSOLE);