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 | |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 7 | #include <blk.h> |
| 8 | #include <dm.h> |
| 9 | #include <fs.h> |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 10 | #include <os.h> |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 11 | #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 Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 18 | /* Basic test of host interface */ |
| 19 | static 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 Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 25 | char fname[256]; |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 26 | 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 Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 33 | ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 34 | |
| 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 Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 41 | /* 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 Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 45 | ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk)); |
| 46 | ut_asserteq_ptr(chk, dev); |
| 47 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 48 | ut_asserteq_str(fname, plat->filename); |
| 49 | ut_assert(fname != plat->filename); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 50 | 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 Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 75 | DM_TEST(dm_test_host, UTF_SCAN_FDT); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 76 | |
| 77 | /* reusing the same label should work */ |
| 78 | static int dm_test_host_dup(struct unit_test_state *uts) |
| 79 | { |
| 80 | static char label[] = "test"; |
| 81 | struct udevice *dev, *chk; |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 82 | char fname[256]; |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 83 | |
| 84 | ut_asserteq(0, uclass_id_count(UCLASS_HOST)); |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 85 | ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 86 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 87 | /* 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 Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 90 | 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 Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 95 | ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 96 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 97 | /* 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 Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 101 | 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 Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 109 | DM_TEST(dm_test_host_dup, UTF_SCAN_FDT); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 110 | |
| 111 | /* Basic test of 'host' command */ |
| 112 | static int dm_test_cmd_host(struct unit_test_state *uts) |
| 113 | { |
| 114 | struct udevice *dev, *blk; |
| 115 | struct blk_desc *desc; |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 116 | char fname[256]; |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 117 | |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 118 | /* first check 'host info' with binding */ |
| 119 | ut_assertok(run_command("host info", 0)); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 120 | ut_assert_nextline("dev blocks blksz label path"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 121 | ut_assert_console_end(); |
| 122 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 123 | ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img")); |
| 124 | ut_assertok(run_commandf("host bind -r test2 %s", fname)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 125 | |
| 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 133 | ut_assert_nextline("dev blocks blksz label path"); |
| 134 | ut_assert_nextlinen(" 0 4096 512 test2"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 135 | ut_assert_console_end(); |
| 136 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 137 | ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img")); |
| 138 | ut_assertok(run_commandf("host bind fat %s", fname)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 139 | |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 140 | /* Check it is not removable (no '-r') */ |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 141 | 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 147 | 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 Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 150 | 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 157 | ut_assert_nextline("dev blocks blksz label path"); |
| 158 | ut_assert_nextlinen(" 1 2048 512 fat"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 159 | 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 194 | ut_assert_nextline("dev blocks blksz label path"); |
| 195 | ut_assert_nextlinen(" 1 2048 512 fat"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 196 | ut_assert_console_end(); |
| 197 | |
| 198 | return 0; |
| 199 | } |
Simon Glass | f54458b | 2024-08-22 07:57:59 -0600 | [diff] [blame] | 200 | DM_TEST(dm_test_cmd_host, UTF_SCAN_FDT | UTF_CONSOLE); |