Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 1 | // 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 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 8 | #include <blk.h> |
| 9 | #include <dm.h> |
| 10 | #include <fs.h> |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 11 | #include <os.h> |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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 | |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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; |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 26 | char fname[256]; |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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); |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 34 | ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 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)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 46 | ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk)); |
| 47 | ut_asserteq_ptr(chk, dev); |
| 48 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 49 | ut_asserteq_str(fname, plat->filename); |
| 50 | ut_assert(fname != plat->filename); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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; |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 83 | char fname[256]; |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 84 | |
| 85 | ut_asserteq(0, uclass_id_count(UCLASS_HOST)); |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 86 | ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 87 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 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)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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) */ |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 96 | ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 97 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 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 | |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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; |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 117 | char fname[256]; |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 118 | |
| 119 | console_record_reset(); |
| 120 | |
| 121 | /* first check 'host info' with binding */ |
| 122 | ut_assertok(run_command("host info", 0)); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 123 | ut_assert_nextline("dev blocks blksz label path"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 124 | ut_assert_console_end(); |
| 125 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 126 | ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img")); |
| 127 | ut_assertok(run_commandf("host bind -r test2 %s", fname)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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)); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 136 | ut_assert_nextline("dev blocks blksz label path"); |
| 137 | ut_assert_nextlinen(" 0 4096 512 test2"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 138 | ut_assert_console_end(); |
| 139 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 140 | ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img")); |
| 141 | ut_assertok(run_commandf("host bind fat %s", fname)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 142 | |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 143 | /* Check it is not removable (no '-r') */ |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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)); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 150 | 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 Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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)); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 160 | ut_assert_nextline("dev blocks blksz label path"); |
| 161 | ut_assert_nextlinen(" 1 2048 512 fat"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 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)); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 197 | ut_assert_nextline("dev blocks blksz label path"); |
| 198 | ut_assert_nextlinen(" 1 2048 512 fat"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 199 | ut_assert_console_end(); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | DM_TEST(dm_test_cmd_host, UT_TESTF_SCAN_FDT); |