blob: ca05a36b3136fbb6e6edb8a40b2b1bec6cfb3e5d [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>
Simon Glass00613bc2023-08-24 13:55:38 -060011#include <os.h>
Simon Glass499503e2022-10-29 19:47:19 -060012#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
Simon Glass499503e2022-10-29 19:47:19 -060019/* Basic test of host interface */
20static 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;
Simon Glass00613bc2023-08-24 13:55:38 -060026 char fname[256];
Simon Glass499503e2022-10-29 19:47:19 -060027 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);
Bin Meng8897fab2023-09-26 16:43:33 +080034 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass499503e2022-10-29 19:47:19 -060035
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
Simon Glass00613bc2023-08-24 13:55:38 -060042 /* 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));
Simon Glass499503e2022-10-29 19:47:19 -060046 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
47 ut_asserteq_ptr(chk, dev);
48
Simon Glass00613bc2023-08-24 13:55:38 -060049 ut_asserteq_str(fname, plat->filename);
50 ut_assert(fname != plat->filename);
Simon Glass499503e2022-10-29 19:47:19 -060051 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}
76DM_TEST(dm_test_host, UT_TESTF_SCAN_FDT);
77
78/* reusing the same label should work */
79static int dm_test_host_dup(struct unit_test_state *uts)
80{
81 static char label[] = "test";
82 struct udevice *dev, *chk;
Simon Glass00613bc2023-08-24 13:55:38 -060083 char fname[256];
Simon Glass499503e2022-10-29 19:47:19 -060084
85 ut_asserteq(0, uclass_id_count(UCLASS_HOST));
Bin Meng8897fab2023-09-26 16:43:33 +080086 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass499503e2022-10-29 19:47:19 -060087
Simon Glass00613bc2023-08-24 13:55:38 -060088 /* 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));
Simon Glass499503e2022-10-29 19:47:19 -060091 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) */
Bin Meng8897fab2023-09-26 16:43:33 +080096 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
Simon Glass499503e2022-10-29 19:47:19 -060097
Simon Glass00613bc2023-08-24 13:55:38 -060098 /* 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
Simon Glass499503e2022-10-29 19:47:19 -0600102 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}
110DM_TEST(dm_test_host_dup, UT_TESTF_SCAN_FDT);
111
112/* Basic test of 'host' command */
113static int dm_test_cmd_host(struct unit_test_state *uts)
114{
115 struct udevice *dev, *blk;
116 struct blk_desc *desc;
Simon Glass00613bc2023-08-24 13:55:38 -0600117 char fname[256];
Simon Glass499503e2022-10-29 19:47:19 -0600118
119 console_record_reset();
120
121 /* first check 'host info' with binding */
122 ut_assertok(run_command("host info", 0));
Bin Meng256f6da2023-09-26 16:43:36 +0800123 ut_assert_nextline("dev blocks blksz label path");
Simon Glass499503e2022-10-29 19:47:19 -0600124 ut_assert_console_end();
125
Simon Glass00613bc2023-08-24 13:55:38 -0600126 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
127 ut_assertok(run_commandf("host bind -r test2 %s", fname));
Simon Glass499503e2022-10-29 19:47:19 -0600128
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));
Bin Meng256f6da2023-09-26 16:43:36 +0800136 ut_assert_nextline("dev blocks blksz label path");
137 ut_assert_nextlinen(" 0 4096 512 test2");
Simon Glass499503e2022-10-29 19:47:19 -0600138 ut_assert_console_end();
139
Simon Glass00613bc2023-08-24 13:55:38 -0600140 ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img"));
141 ut_assertok(run_commandf("host bind fat %s", fname));
Simon Glass499503e2022-10-29 19:47:19 -0600142
Yuepeng Xing7943ae22022-12-02 14:23:07 +0800143 /* Check it is not removable (no '-r') */
Simon Glass499503e2022-10-29 19:47:19 -0600144 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));
Bin Meng256f6da2023-09-26 16:43:36 +0800150 ut_assert_nextline("dev blocks blksz label path");
151 ut_assert_nextlinen(" 0 4096 512 test2");
152 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass499503e2022-10-29 19:47:19 -0600153 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));
Bin Meng256f6da2023-09-26 16:43:36 +0800160 ut_assert_nextline("dev blocks blksz label path");
161 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass499503e2022-10-29 19:47:19 -0600162 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));
Bin Meng256f6da2023-09-26 16:43:36 +0800197 ut_assert_nextline("dev blocks blksz label path");
198 ut_assert_nextlinen(" 1 2048 512 fat");
Simon Glass499503e2022-10-29 19:47:19 -0600199 ut_assert_console_end();
200
201 return 0;
202}
203DM_TEST(dm_test_cmd_host, UT_TESTF_SCAN_FDT);