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 | } |
| 75 | DM_TEST(dm_test_host, UT_TESTF_SCAN_FDT); |
| 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 | } |
| 109 | DM_TEST(dm_test_host_dup, UT_TESTF_SCAN_FDT); |
| 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 | |
| 118 | console_record_reset(); |
| 119 | |
| 120 | /* first check 'host info' with binding */ |
| 121 | ut_assertok(run_command("host info", 0)); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 122 | ut_assert_nextline("dev blocks blksz label path"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 123 | ut_assert_console_end(); |
| 124 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 125 | ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img")); |
| 126 | ut_assertok(run_commandf("host bind -r test2 %s", fname)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 127 | |
| 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 135 | ut_assert_nextline("dev blocks blksz label path"); |
| 136 | ut_assert_nextlinen(" 0 4096 512 test2"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 137 | ut_assert_console_end(); |
| 138 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 139 | ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img")); |
| 140 | ut_assertok(run_commandf("host bind fat %s", fname)); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 141 | |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 142 | /* Check it is not removable (no '-r') */ |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 143 | 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 149 | 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 Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 152 | 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 159 | ut_assert_nextline("dev blocks blksz label path"); |
| 160 | ut_assert_nextlinen(" 1 2048 512 fat"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 161 | 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 Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 196 | ut_assert_nextline("dev blocks blksz label path"); |
| 197 | ut_assert_nextlinen(" 1 2048 512 fat"); |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 198 | ut_assert_console_end(); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | DM_TEST(dm_test_cmd_host, UT_TESTF_SCAN_FDT); |