Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Test for bootdev functions. All start with 'bootdev' |
| 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <bootdev.h> |
| 11 | #include <bootflow.h> |
Simon Glass | c8d3721 | 2022-07-30 15:52:34 -0600 | [diff] [blame] | 12 | #include <bootmeth.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 13 | #include <bootstd.h> |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 14 | #include <cli.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 16 | #include <expo.h> |
Simon Glass | 90b643d | 2022-07-30 15:52:36 -0600 | [diff] [blame] | 17 | #ifdef CONFIG_SANDBOX |
Simon Glass | 161e1e3 | 2022-07-30 15:52:22 -0600 | [diff] [blame] | 18 | #include <asm/test.h> |
Simon Glass | 90b643d | 2022-07-30 15:52:36 -0600 | [diff] [blame] | 19 | #endif |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 20 | #include <dm/device-internal.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 21 | #include <dm/lists.h> |
| 22 | #include <test/suites.h> |
| 23 | #include <test/ut.h> |
| 24 | #include "bootstd_common.h" |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 25 | #include "../../boot/bootflow_internal.h" |
| 26 | #include "../../boot/scene_internal.h" |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 27 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 30 | extern U_BOOT_DRIVER(bootmeth_cros); |
Simon Glass | 2f27e47 | 2023-08-19 16:49:35 -0600 | [diff] [blame] | 31 | extern U_BOOT_DRIVER(bootmeth_2script); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 32 | |
Simon Glass | 49ad1d8 | 2022-07-30 15:52:16 -0600 | [diff] [blame] | 33 | static int inject_response(struct unit_test_state *uts) |
| 34 | { |
| 35 | /* |
| 36 | * The image being booted presents a menu of options: |
| 37 | * |
| 38 | * Fedora-Workstation-armhfp-31-1.9 Boot Options. |
| 39 | * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) |
| 40 | * Enter choice: |
| 41 | * |
| 42 | * Provide input for this, to avoid waiting two seconds for a timeout. |
| 43 | */ |
| 44 | ut_asserteq(2, console_in_puts("1\n")); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 49 | /* Check 'bootflow scan/list' commands */ |
| 50 | static int bootflow_cmd(struct unit_test_state *uts) |
| 51 | { |
| 52 | console_record_reset_enable(); |
| 53 | ut_assertok(run_command("bootdev select 1", 0)); |
| 54 | ut_assert_console_end(); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 55 | ut_assertok(run_command("bootflow scan -lH", 0)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 56 | ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'"); |
| 57 | ut_assert_nextline("Seq Method State Uclass Part Name Filename"); |
| 58 | ut_assert_nextlinen("---"); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 59 | ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':"); |
| 60 | ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':"); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 61 | ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 62 | ut_assert_nextline("No more bootdevs"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 63 | ut_assert_nextlinen("---"); |
| 64 | ut_assert_nextline("(1 bootflow, 1 valid)"); |
| 65 | ut_assert_console_end(); |
| 66 | |
| 67 | ut_assertok(run_command("bootflow list", 0)); |
| 68 | ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'"); |
| 69 | ut_assert_nextline("Seq Method State Uclass Part Name Filename"); |
| 70 | ut_assert_nextlinen("---"); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 71 | ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 72 | ut_assert_nextlinen("---"); |
| 73 | ut_assert_nextline("(1 bootflow, 1 valid)"); |
| 74 | ut_assert_console_end(); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 79 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 80 | /* Check 'bootflow scan' with a label / seq */ |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 81 | static int bootflow_cmd_label(struct unit_test_state *uts) |
| 82 | { |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 83 | test_set_eth_enable(false); |
| 84 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 85 | console_record_reset_enable(); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 86 | ut_assertok(run_command("bootflow scan -lH mmc1", 0)); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 87 | ut_assert_nextline("Scanning for bootflows with label 'mmc1'"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 88 | ut_assert_skip_to_line("(1 bootflow, 1 valid)"); |
| 89 | ut_assert_console_end(); |
| 90 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 91 | ut_assertok(run_command("bootflow scan -lH 0", 0)); |
| 92 | ut_assert_nextline("Scanning for bootflows with label '0'"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 93 | ut_assert_skip_to_line("(0 bootflows, 0 valid)"); |
| 94 | ut_assert_console_end(); |
| 95 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 96 | /* |
| 97 | * with ethernet enabled we have 8 devices ahead of the mmc ones: |
| 98 | * |
| 99 | * ut_assertok(run_command("bootdev list", 0)); |
| 100 | * Seq Probed Status Uclass Name |
| 101 | * --- ------ ------ -------- ------------------ |
| 102 | * 0 [ + ] OK ethernet eth@10002000.bootdev |
| 103 | * 1 [ ] OK ethernet eth@10003000.bootdev |
| 104 | * 2 [ ] OK ethernet sbe5.bootdev |
| 105 | * 3 [ ] OK ethernet eth@10004000.bootdev |
| 106 | * 4 [ ] OK ethernet phy-test-eth.bootdev |
| 107 | * 5 [ ] OK ethernet dsa-test-eth.bootdev |
| 108 | * 6 [ ] OK ethernet dsa-test@0.bootdev |
| 109 | * 7 [ ] OK ethernet dsa-test@1.bootdev |
| 110 | * 8 [ ] OK mmc mmc2.bootdev |
| 111 | * 9 [ + ] OK mmc mmc1.bootdev |
| 112 | * a [ ] OK mmc mmc0.bootdev |
| 113 | */ |
| 114 | ut_assertok(run_command("bootflow scan -lH 9", 0)); |
| 115 | ut_assert_nextline("Scanning for bootflows with label '9'"); |
| 116 | ut_assert_skip_to_line("(1 bootflow, 1 valid)"); |
| 117 | |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 118 | ut_assertok(run_command("bootflow scan -lH 0", 0)); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 119 | ut_assert_nextline("Scanning for bootflows with label '0'"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 120 | ut_assert_skip_to_line("(0 bootflows, 0 valid)"); |
| 121 | ut_assert_console_end(); |
| 122 | |
| 123 | return 0; |
| 124 | } |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 125 | BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 126 | UT_TESTF_ETH_BOOTDEV); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 127 | |
| 128 | /* Check 'bootflow scan/list' commands using all bootdevs */ |
| 129 | static int bootflow_cmd_glob(struct unit_test_state *uts) |
| 130 | { |
| 131 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 132 | |
| 133 | console_record_reset_enable(); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 134 | ut_assertok(run_command("bootflow scan -lGH", 0)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 135 | ut_assert_nextline("Scanning for bootflows in all bootdevs"); |
| 136 | ut_assert_nextline("Seq Method State Uclass Part Name Filename"); |
| 137 | ut_assert_nextlinen("---"); |
| 138 | ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':"); |
| 139 | ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':"); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 140 | ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 141 | ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':"); |
| 142 | ut_assert_nextline("No more bootdevs"); |
| 143 | ut_assert_nextlinen("---"); |
| 144 | ut_assert_nextline("(1 bootflow, 1 valid)"); |
| 145 | ut_assert_console_end(); |
| 146 | |
| 147 | ut_assertok(run_command("bootflow list", 0)); |
| 148 | ut_assert_nextline("Showing all bootflows"); |
| 149 | ut_assert_nextline("Seq Method State Uclass Part Name Filename"); |
| 150 | ut_assert_nextlinen("---"); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 151 | ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 152 | ut_assert_nextlinen("---"); |
| 153 | ut_assert_nextline("(1 bootflow, 1 valid)"); |
| 154 | ut_assert_console_end(); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 159 | |
| 160 | /* Check 'bootflow scan -e' */ |
| 161 | static int bootflow_cmd_scan_e(struct unit_test_state *uts) |
| 162 | { |
| 163 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 164 | |
| 165 | console_record_reset_enable(); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 166 | ut_assertok(run_command("bootflow scan -aleGH", 0)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 167 | ut_assert_nextline("Scanning for bootflows in all bootdevs"); |
| 168 | ut_assert_nextline("Seq Method State Uclass Part Name Filename"); |
| 169 | ut_assert_nextlinen("---"); |
| 170 | ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':"); |
Simon Glass | d465ad5 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 171 | ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole "); |
Simon Glass | 9482fdf | 2023-05-10 16:34:26 -0600 | [diff] [blame] | 172 | ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported"); |
Simon Glass | d465ad5 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 173 | ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole "); |
Simon Glass | 9482fdf | 2023-05-10 16:34:26 -0600 | [diff] [blame] | 174 | ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 175 | |
| 176 | ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':"); |
Simon Glass | d465ad5 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 177 | ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole "); |
Simon Glass | 9482fdf | 2023-05-10 16:34:26 -0600 | [diff] [blame] | 178 | ut_assert_nextline(" ** No partition found, err=-2: No such file or directory"); |
Simon Glass | d465ad5 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 179 | ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole "); |
Simon Glass | 9482fdf | 2023-05-10 16:34:26 -0600 | [diff] [blame] | 180 | ut_assert_nextline(" ** No partition found, err=-2: No such file or directory"); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 181 | ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 182 | ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 efi/boot/bootsbox.efi"); |
| 183 | |
| 184 | ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':"); |
Simon Glass | d465ad5 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 185 | ut_assert_skip_to_line( |
| 186 | " 3f efi media mmc 0 mmc0.bootdev.whole "); |
Simon Glass | 9482fdf | 2023-05-10 16:34:26 -0600 | [diff] [blame] | 187 | ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 188 | ut_assert_nextline("No more bootdevs"); |
| 189 | ut_assert_nextlinen("---"); |
| 190 | ut_assert_nextline("(64 bootflows, 1 valid)"); |
| 191 | ut_assert_console_end(); |
| 192 | |
| 193 | ut_assertok(run_command("bootflow list", 0)); |
| 194 | ut_assert_nextline("Showing all bootflows"); |
| 195 | ut_assert_nextline("Seq Method State Uclass Part Name Filename"); |
| 196 | ut_assert_nextlinen("---"); |
Simon Glass | d465ad5 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 197 | ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole "); |
| 198 | ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole "); |
| 199 | ut_assert_skip_to_line( |
| 200 | " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
| 201 | ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole "); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 202 | ut_assert_nextlinen("---"); |
| 203 | ut_assert_nextline("(64 bootflows, 1 valid)"); |
| 204 | ut_assert_console_end(); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 209 | |
| 210 | /* Check 'bootflow info' */ |
| 211 | static int bootflow_cmd_info(struct unit_test_state *uts) |
| 212 | { |
| 213 | console_record_reset_enable(); |
| 214 | ut_assertok(run_command("bootdev select 1", 0)); |
| 215 | ut_assert_console_end(); |
| 216 | ut_assertok(run_command("bootflow scan", 0)); |
| 217 | ut_assert_console_end(); |
| 218 | ut_assertok(run_command("bootflow select 0", 0)); |
| 219 | ut_assert_console_end(); |
| 220 | ut_assertok(run_command("bootflow info", 0)); |
| 221 | ut_assert_nextline("Name: mmc1.bootdev.part_1"); |
| 222 | ut_assert_nextline("Device: mmc1.bootdev"); |
| 223 | ut_assert_nextline("Block dev: mmc1.blk"); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 224 | ut_assert_nextline("Method: extlinux"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 225 | ut_assert_nextline("State: ready"); |
| 226 | ut_assert_nextline("Partition: 1"); |
| 227 | ut_assert_nextline("Subdir: (none)"); |
| 228 | ut_assert_nextline("Filename: /extlinux/extlinux.conf"); |
| 229 | ut_assert_nextlinen("Buffer: "); |
| 230 | ut_assert_nextline("Size: 253 (595 bytes)"); |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 231 | ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)"); |
Simon Glass | 3392752 | 2023-07-12 09:04:34 -0600 | [diff] [blame] | 232 | ut_assert_nextline("Cmdline: (none)"); |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 233 | ut_assert_nextline("Logo: (none)"); |
Simon Glass | 7b8c634 | 2023-01-17 10:47:56 -0700 | [diff] [blame] | 234 | ut_assert_nextline("FDT: <NULL>"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 235 | ut_assert_nextline("Error: 0"); |
| 236 | ut_assert_console_end(); |
| 237 | |
| 238 | ut_assertok(run_command("bootflow info -d", 0)); |
| 239 | ut_assert_nextline("Name: mmc1.bootdev.part_1"); |
| 240 | ut_assert_skip_to_line("Error: 0"); |
| 241 | ut_assert_nextline("Contents:"); |
| 242 | ut_assert_nextline("%s", ""); |
| 243 | ut_assert_nextline("# extlinux.conf generated by appliance-creator"); |
| 244 | ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img"); |
| 245 | ut_assert_console_end(); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 250 | |
| 251 | /* Check 'bootflow scan -b' to boot the first available bootdev */ |
| 252 | static int bootflow_scan_boot(struct unit_test_state *uts) |
| 253 | { |
| 254 | console_record_reset_enable(); |
Simon Glass | 49ad1d8 | 2022-07-30 15:52:16 -0600 | [diff] [blame] | 255 | ut_assertok(inject_response(uts)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 256 | ut_assertok(run_command("bootflow scan -b", 0)); |
| 257 | ut_assert_nextline( |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 258 | "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 259 | ut_assert_nextline("Ignoring unknown command: ui"); |
| 260 | |
| 261 | /* |
| 262 | * We expect it to get through to boot although sandbox always returns |
| 263 | * -EFAULT as it cannot actually boot the kernel |
| 264 | */ |
| 265 | ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux"); |
| 266 | ut_assert_nextline("Boot failed (err=-14)"); |
| 267 | ut_assert_console_end(); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 272 | |
| 273 | /* Check iterating through available bootflows */ |
| 274 | static int bootflow_iter(struct unit_test_state *uts) |
| 275 | { |
| 276 | struct bootflow_iter iter; |
| 277 | struct bootflow bflow; |
| 278 | |
| 279 | bootstd_clear_glob(); |
| 280 | |
| 281 | /* The first device is mmc2.bootdev which has no media */ |
| 282 | ut_asserteq(-EPROTONOSUPPORT, |
Simon Glass | 5d3d44f | 2023-01-17 10:48:16 -0700 | [diff] [blame] | 283 | bootflow_scan_first(NULL, NULL, &iter, |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 284 | BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 285 | ut_asserteq(2, iter.num_methods); |
| 286 | ut_asserteq(0, iter.cur_method); |
| 287 | ut_asserteq(0, iter.part); |
| 288 | ut_asserteq(0, iter.max_part); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 289 | ut_asserteq_str("extlinux", iter.method->name); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 290 | ut_asserteq(0, bflow.err); |
| 291 | |
| 292 | /* |
Simon Glass | c8d3721 | 2022-07-30 15:52:34 -0600 | [diff] [blame] | 293 | * This shows MEDIA even though there is none, since in |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 294 | * bootdev_find_in_blk() we call part_get_info() which returns |
| 295 | * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would |
| 296 | * know. |
| 297 | */ |
| 298 | ut_asserteq(BOOTFLOWST_MEDIA, bflow.state); |
| 299 | |
| 300 | ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow)); |
| 301 | ut_asserteq(2, iter.num_methods); |
| 302 | ut_asserteq(1, iter.cur_method); |
| 303 | ut_asserteq(0, iter.part); |
| 304 | ut_asserteq(0, iter.max_part); |
| 305 | ut_asserteq_str("efi", iter.method->name); |
| 306 | ut_asserteq(0, bflow.err); |
| 307 | ut_asserteq(BOOTFLOWST_MEDIA, bflow.state); |
| 308 | bootflow_free(&bflow); |
| 309 | |
| 310 | /* The next device is mmc1.bootdev - at first we use the whole device */ |
| 311 | ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow)); |
| 312 | ut_asserteq(2, iter.num_methods); |
| 313 | ut_asserteq(0, iter.cur_method); |
| 314 | ut_asserteq(0, iter.part); |
| 315 | ut_asserteq(0x1e, iter.max_part); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 316 | ut_asserteq_str("extlinux", iter.method->name); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 317 | ut_asserteq(0, bflow.err); |
| 318 | ut_asserteq(BOOTFLOWST_MEDIA, bflow.state); |
| 319 | bootflow_free(&bflow); |
| 320 | |
| 321 | ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow)); |
| 322 | ut_asserteq(2, iter.num_methods); |
| 323 | ut_asserteq(1, iter.cur_method); |
| 324 | ut_asserteq(0, iter.part); |
| 325 | ut_asserteq(0x1e, iter.max_part); |
| 326 | ut_asserteq_str("efi", iter.method->name); |
| 327 | ut_asserteq(0, bflow.err); |
| 328 | ut_asserteq(BOOTFLOWST_MEDIA, bflow.state); |
| 329 | bootflow_free(&bflow); |
| 330 | |
| 331 | /* Then more to partition 1 where we find something */ |
| 332 | ut_assertok(bootflow_scan_next(&iter, &bflow)); |
| 333 | ut_asserteq(2, iter.num_methods); |
| 334 | ut_asserteq(0, iter.cur_method); |
| 335 | ut_asserteq(1, iter.part); |
| 336 | ut_asserteq(0x1e, iter.max_part); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 337 | ut_asserteq_str("extlinux", iter.method->name); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 338 | ut_asserteq(0, bflow.err); |
| 339 | ut_asserteq(BOOTFLOWST_READY, bflow.state); |
| 340 | bootflow_free(&bflow); |
| 341 | |
| 342 | ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow)); |
| 343 | ut_asserteq(2, iter.num_methods); |
| 344 | ut_asserteq(1, iter.cur_method); |
| 345 | ut_asserteq(1, iter.part); |
| 346 | ut_asserteq(0x1e, iter.max_part); |
| 347 | ut_asserteq_str("efi", iter.method->name); |
| 348 | ut_asserteq(0, bflow.err); |
| 349 | ut_asserteq(BOOTFLOWST_FS, bflow.state); |
| 350 | bootflow_free(&bflow); |
| 351 | |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 352 | /* Then more to partition 2 which exists but is not bootable */ |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 353 | ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 354 | ut_asserteq(2, iter.num_methods); |
| 355 | ut_asserteq(0, iter.cur_method); |
| 356 | ut_asserteq(2, iter.part); |
| 357 | ut_asserteq(0x1e, iter.max_part); |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 358 | ut_asserteq_str("extlinux", iter.method->name); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 359 | ut_asserteq(0, bflow.err); |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 360 | ut_asserteq(BOOTFLOWST_MEDIA, bflow.state); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 361 | bootflow_free(&bflow); |
| 362 | |
| 363 | bootflow_iter_uninit(&iter); |
| 364 | |
| 365 | ut_assert_console_end(); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 370 | |
Simon Glass | 90b643d | 2022-07-30 15:52:36 -0600 | [diff] [blame] | 371 | #if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL) |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 372 | /* Check using the system bootdev */ |
| 373 | static int bootflow_system(struct unit_test_state *uts) |
| 374 | { |
Simon Glass | b9ff648 | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 375 | struct udevice *bootstd, *dev; |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 376 | |
AKASHI Takahiro | e3e542d | 2024-01-17 13:39:42 +0900 | [diff] [blame] | 377 | if (!IS_ENABLED(CONFIG_EFI_BOOTMGR)) |
Simon Glass | c759944 | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 378 | return -EAGAIN; |
Simon Glass | b9ff648 | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 379 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 380 | ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_efi_mgr), |
| 381 | "efi_mgr", 0, ofnode_null(), &dev)); |
| 382 | ut_assertok(device_probe(dev)); |
Simon Glass | 161e1e3 | 2022-07-30 15:52:22 -0600 | [diff] [blame] | 383 | sandbox_set_fake_efi_mgr_dev(dev, true); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 384 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 385 | /* We should get a single 'bootmgr' method right at the end */ |
| 386 | bootstd_clear_glob(); |
| 387 | console_record_reset_enable(); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 388 | ut_assertok(run_command("bootflow scan -lH", 0)); |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 389 | ut_assert_skip_to_line( |
Simon Glass | d465ad5 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 390 | " 0 efi_mgr ready (none) 0 <NULL> "); |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 391 | ut_assert_skip_to_line("No more bootdevs"); |
Simon Glass | b9ff648 | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 392 | ut_assert_skip_to_line("(2 bootflows, 2 valid)"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 393 | ut_assert_console_end(); |
| 394 | |
| 395 | return 0; |
| 396 | } |
Simon Glass | 771a244 | 2022-07-30 15:52:28 -0600 | [diff] [blame] | 397 | BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA | |
| 398 | UT_TESTF_SCAN_FDT); |
Simon Glass | 90b643d | 2022-07-30 15:52:36 -0600 | [diff] [blame] | 399 | #endif |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 400 | |
| 401 | /* Check disabling a bootmethod if it requests it */ |
| 402 | static int bootflow_iter_disable(struct unit_test_state *uts) |
| 403 | { |
| 404 | struct udevice *bootstd, *dev; |
| 405 | struct bootflow_iter iter; |
| 406 | struct bootflow bflow; |
| 407 | int i; |
| 408 | |
| 409 | /* Add the EFI bootmgr driver */ |
| 410 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 411 | ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox", |
| 412 | &dev)); |
| 413 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 414 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 415 | |
| 416 | bootstd_clear_glob(); |
Simon Glass | 49ad1d8 | 2022-07-30 15:52:16 -0600 | [diff] [blame] | 417 | console_record_reset_enable(); |
| 418 | ut_assertok(inject_response(uts)); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 419 | ut_assertok(run_command("bootflow scan -lbH", 0)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 420 | |
| 421 | /* Try to boot the bootmgr flow, which will fail */ |
| 422 | console_record_reset_enable(); |
Simon Glass | 5d3d44f | 2023-01-17 10:48:16 -0700 | [diff] [blame] | 423 | ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 424 | ut_asserteq(3, iter.num_methods); |
| 425 | ut_asserteq_str("sandbox", iter.method->name); |
Simon Glass | 49ad1d8 | 2022-07-30 15:52:16 -0600 | [diff] [blame] | 426 | ut_assertok(inject_response(uts)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 427 | ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow)); |
| 428 | |
| 429 | ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried"); |
| 430 | ut_assert_console_end(); |
| 431 | |
| 432 | /* Check that the sandbox bootmeth has been removed */ |
| 433 | ut_asserteq(2, iter.num_methods); |
| 434 | for (i = 0; i < iter.num_methods; i++) |
| 435 | ut_assert(strcmp("sandbox", iter.method_order[i]->name)); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 440 | |
Simon Glass | c8d3721 | 2022-07-30 15:52:34 -0600 | [diff] [blame] | 441 | /* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */ |
| 442 | static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts) |
| 443 | { |
Simon Glass | 90b643d | 2022-07-30 15:52:36 -0600 | [diff] [blame] | 444 | if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) |
Simon Glass | c759944 | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 445 | return -EAGAIN; |
Simon Glass | 90b643d | 2022-07-30 15:52:36 -0600 | [diff] [blame] | 446 | |
Simon Glass | c8d3721 | 2022-07-30 15:52:34 -0600 | [diff] [blame] | 447 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 448 | |
| 449 | /* |
| 450 | * Make sure that the -G flag makes the scan fail, since this is not |
| 451 | * supported when an ordering is provided |
| 452 | */ |
| 453 | console_record_reset_enable(); |
| 454 | ut_assertok(bootmeth_set_order("efi firmware0")); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 455 | ut_assertok(run_command("bootflow scan -lGH", 0)); |
Simon Glass | c8d3721 | 2022-07-30 15:52:34 -0600 | [diff] [blame] | 456 | ut_assert_nextline("Scanning for bootflows in all bootdevs"); |
| 457 | ut_assert_nextline( |
| 458 | "Seq Method State Uclass Part Name Filename"); |
| 459 | ut_assert_nextlinen("---"); |
| 460 | ut_assert_nextlinen("---"); |
| 461 | ut_assert_nextline("(0 bootflows, 0 valid)"); |
| 462 | ut_assert_console_end(); |
| 463 | |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 464 | ut_assertok(run_command("bootflow scan -lH", 0)); |
Simon Glass | c8d3721 | 2022-07-30 15:52:34 -0600 | [diff] [blame] | 465 | ut_assert_nextline("Scanning for bootflows in all bootdevs"); |
| 466 | ut_assert_nextline( |
| 467 | "Seq Method State Uclass Part Name Filename"); |
| 468 | ut_assert_nextlinen("---"); |
| 469 | ut_assert_nextline("Scanning global bootmeth 'firmware0':"); |
| 470 | ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':"); |
| 471 | ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':"); |
| 472 | ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':"); |
| 473 | ut_assert_nextline("No more bootdevs"); |
| 474 | ut_assert_nextlinen("---"); |
| 475 | ut_assert_nextline("(0 bootflows, 0 valid)"); |
| 476 | ut_assert_console_end(); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 481 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 482 | /* Check 'bootflow boot' to boot a selected bootflow */ |
| 483 | static int bootflow_cmd_boot(struct unit_test_state *uts) |
| 484 | { |
| 485 | console_record_reset_enable(); |
| 486 | ut_assertok(run_command("bootdev select 1", 0)); |
| 487 | ut_assert_console_end(); |
| 488 | ut_assertok(run_command("bootflow scan", 0)); |
| 489 | ut_assert_console_end(); |
| 490 | ut_assertok(run_command("bootflow select 0", 0)); |
| 491 | ut_assert_console_end(); |
Simon Glass | 49ad1d8 | 2022-07-30 15:52:16 -0600 | [diff] [blame] | 492 | |
| 493 | ut_assertok(inject_response(uts)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 494 | ut_asserteq(1, run_command("bootflow boot", 0)); |
| 495 | ut_assert_nextline( |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 496 | "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux"); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 497 | ut_assert_nextline("Ignoring unknown command: ui"); |
| 498 | |
| 499 | /* |
| 500 | * We expect it to get through to boot although sandbox always returns |
| 501 | * -EFAULT as it cannot actually boot the kernel |
| 502 | */ |
| 503 | ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux"); |
| 504 | ut_assert_nextline("Boot failed (err=-14)"); |
| 505 | ut_assert_console_end(); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 510 | |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 511 | /** |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 512 | * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 513 | * |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 514 | * After calling this function, set std->bootdev_order to *@old_orderp to |
| 515 | * restore normal operation of bootstd (i.e. with the original bootdev order) |
| 516 | * |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 517 | * @uts: Unit test state |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 518 | * @mmc_dev: MMC device to use, e.g. "mmc4". Note that this must remain valid |
| 519 | * in the caller until |
| 520 | * @bind_cros: true to bind the ChromiumOS bootmeth |
| 521 | * @old_orderp: Returns the original bootdev order, which must be restored |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 522 | * Returns 0 on success, -ve on failure |
| 523 | */ |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 524 | static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev, |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 525 | bool bind_cros, const char ***old_orderp) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 526 | { |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 527 | static const char *order[] = {"mmc2", "mmc1", NULL, NULL}; |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 528 | struct udevice *dev, *bootstd; |
| 529 | struct bootstd_priv *std; |
| 530 | const char **old_order; |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 531 | ofnode root, node; |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 532 | |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 533 | order[2] = mmc_dev; |
| 534 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 535 | /* Enable the mmc4 node since we need a second bootflow */ |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 536 | root = oftree_root(oftree_default()); |
| 537 | node = ofnode_find_subnode(root, mmc_dev); |
| 538 | ut_assert(ofnode_valid(node)); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 539 | ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false)); |
| 540 | |
| 541 | /* Enable the script bootmeth too */ |
| 542 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
Simon Glass | 2f27e47 | 2023-08-19 16:49:35 -0600 | [diff] [blame] | 543 | ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script), |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 544 | "bootmeth_script", 0, ofnode_null(), &dev)); |
| 545 | |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 546 | /* Enable the cros bootmeth if needed */ |
| 547 | if (bind_cros) { |
| 548 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 549 | ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros), |
| 550 | "cros", 0, ofnode_null(), &dev)); |
| 551 | } |
| 552 | |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 553 | /* Change the order to include the device */ |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 554 | std = dev_get_priv(bootstd); |
| 555 | old_order = std->bootdev_order; |
| 556 | std->bootdev_order = order; |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 557 | *old_orderp = old_order; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros |
| 564 | * |
| 565 | * @uts: Unit test state |
| 566 | * @mmc_dev: MMC device to use, e.g. "mmc4" |
| 567 | * @bind_cros: true to bind the ChromiumOS bootmeth |
| 568 | * Returns 0 on success, -ve on failure |
| 569 | */ |
| 570 | static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev, |
| 571 | bool bind_cros) |
| 572 | { |
| 573 | struct bootstd_priv *std; |
| 574 | struct udevice *bootstd; |
| 575 | const char **old_order; |
| 576 | |
| 577 | ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order)); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 578 | |
| 579 | console_record_reset_enable(); |
| 580 | ut_assertok(run_command("bootflow scan", 0)); |
| 581 | ut_assert_console_end(); |
| 582 | |
| 583 | /* Restore the order used by the device tree */ |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 584 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 585 | std = dev_get_priv(bootstd); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 586 | std->bootdev_order = old_order; |
| 587 | |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 591 | /** |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 592 | * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 593 | * |
| 594 | * @uts: Unit test state |
| 595 | * Returns 0 on success, -ve on failure |
| 596 | */ |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 597 | static int scan_mmc4_bootdev(struct unit_test_state *uts) |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 598 | { |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 599 | ut_assertok(scan_mmc_bootdev(uts, "mmc4", false)); |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 604 | /* Check 'bootflow menu' to select a bootflow */ |
| 605 | static int bootflow_cmd_menu(struct unit_test_state *uts) |
| 606 | { |
Simon Glass | 6d5083b | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 607 | struct bootstd_priv *std; |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 608 | char prev[3]; |
| 609 | |
Simon Glass | 6d5083b | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 610 | /* get access to the current bootflow */ |
| 611 | ut_assertok(bootstd_get_priv(&std)); |
| 612 | |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 613 | ut_assertok(scan_mmc4_bootdev(uts)); |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 614 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 615 | /* Add keypresses to move to and select the second one in the list */ |
| 616 | prev[0] = CTL_CH('n'); |
| 617 | prev[1] = '\r'; |
| 618 | prev[2] = '\0'; |
| 619 | ut_asserteq(2, console_in_puts(prev)); |
| 620 | |
| 621 | ut_assertok(run_command("bootflow menu", 0)); |
| 622 | ut_assert_nextline("Selected: Armbian"); |
Simon Glass | 6d5083b | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 623 | ut_assertnonnull(std->cur_bootflow); |
| 624 | ut_assert_console_end(); |
| 625 | |
| 626 | /* Check not selecting anything */ |
| 627 | prev[0] = '\e'; |
| 628 | prev[1] = '\0'; |
| 629 | ut_asserteq(1, console_in_puts(prev)); |
| 630 | |
| 631 | ut_asserteq(1, run_command("bootflow menu", 0)); |
| 632 | ut_assertnull(std->cur_bootflow); |
| 633 | ut_assert_nextline("Nothing chosen"); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 634 | ut_assert_console_end(); |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 639 | |
Simon Glass | 9bf2786 | 2023-10-01 19:15:25 -0600 | [diff] [blame] | 640 | /* Check 'bootflow scan -m' to select a bootflow using a menu */ |
| 641 | static int bootflow_scan_menu(struct unit_test_state *uts) |
| 642 | { |
| 643 | struct bootstd_priv *std; |
| 644 | const char **old_order, **new_order; |
| 645 | char prev[3]; |
| 646 | |
| 647 | /* get access to the current bootflow */ |
| 648 | ut_assertok(bootstd_get_priv(&std)); |
| 649 | |
| 650 | ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order)); |
| 651 | |
| 652 | /* Add keypresses to move to and select the second one in the list */ |
| 653 | prev[0] = CTL_CH('n'); |
| 654 | prev[1] = '\r'; |
| 655 | prev[2] = '\0'; |
| 656 | ut_asserteq(2, console_in_puts(prev)); |
| 657 | |
| 658 | ut_assertok(run_command("bootflow scan -lm", 0)); |
| 659 | new_order = std->bootdev_order; |
| 660 | std->bootdev_order = old_order; |
| 661 | |
| 662 | ut_assert_skip_to_line("No more bootdevs"); |
| 663 | ut_assert_nextlinen("--"); |
| 664 | ut_assert_nextline("(2 bootflows, 2 valid)"); |
| 665 | |
| 666 | ut_assert_nextline("Selected: Armbian"); |
| 667 | ut_assertnonnull(std->cur_bootflow); |
| 668 | ut_assert_console_end(); |
| 669 | |
| 670 | /* Check not selecting anything */ |
| 671 | prev[0] = '\e'; |
| 672 | prev[1] = '\0'; |
| 673 | ut_asserteq(1, console_in_puts(prev)); |
| 674 | |
| 675 | std->bootdev_order = new_order; /* Blue Monday */ |
| 676 | ut_assertok(run_command("bootflow scan -lm", 0)); |
| 677 | std->bootdev_order = old_order; |
| 678 | |
| 679 | ut_assertnull(std->cur_bootflow); |
| 680 | ut_assert_skip_to_line("(2 bootflows, 2 valid)"); |
| 681 | ut_assert_nextline("Nothing chosen"); |
| 682 | ut_assert_console_end(); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | BOOTSTD_TEST(bootflow_scan_menu, |
| 687 | UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC); |
| 688 | |
| 689 | /* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */ |
| 690 | static int bootflow_scan_menu_boot(struct unit_test_state *uts) |
| 691 | { |
| 692 | struct bootstd_priv *std; |
| 693 | const char **old_order; |
| 694 | char prev[3]; |
| 695 | |
| 696 | /* get access to the current bootflow */ |
| 697 | ut_assertok(bootstd_get_priv(&std)); |
| 698 | |
| 699 | ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order)); |
| 700 | |
| 701 | /* Add keypresses to move to and select the second one in the list */ |
| 702 | prev[0] = CTL_CH('n'); |
| 703 | prev[1] = '\r'; |
| 704 | prev[2] = '\0'; |
| 705 | ut_asserteq(2, console_in_puts(prev)); |
| 706 | |
| 707 | ut_assertok(run_command("bootflow scan -lmb", 0)); |
| 708 | std->bootdev_order = old_order; |
| 709 | |
| 710 | ut_assert_skip_to_line("(2 bootflows, 2 valid)"); |
| 711 | |
| 712 | ut_assert_nextline("Selected: Armbian"); |
Francis Laniel | 5b64c45 | 2023-12-22 22:02:35 +0100 | [diff] [blame] | 713 | |
| 714 | if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { |
| 715 | /* |
| 716 | * With old hush, despite booti failing to boot, i.e. returning |
| 717 | * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as |
| 718 | * we are using bootmeth_script here, to return -EFAULT. |
| 719 | */ |
| 720 | ut_assert_skip_to_line("Boot failed (err=-14)"); |
| 721 | } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { |
| 722 | /* |
| 723 | * While with modern one, run_command() propagates CMD_RET_FAILURE returned |
| 724 | * by booti, so we get 1 here. |
| 725 | */ |
| 726 | ut_assert_skip_to_line("Boot failed (err=1)"); |
| 727 | } |
Simon Glass | 9bf2786 | 2023-10-01 19:15:25 -0600 | [diff] [blame] | 728 | ut_assertnonnull(std->cur_bootflow); |
| 729 | ut_assert_console_end(); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | BOOTSTD_TEST(bootflow_scan_menu_boot, |
| 734 | UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC); |
| 735 | |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 736 | /* Check searching for a single bootdev using the hunters */ |
| 737 | static int bootflow_cmd_hunt_single(struct unit_test_state *uts) |
| 738 | { |
| 739 | struct bootstd_priv *std; |
| 740 | |
| 741 | /* get access to the used hunters */ |
| 742 | ut_assertok(bootstd_get_priv(&std)); |
| 743 | |
| 744 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 745 | |
| 746 | console_record_reset_enable(); |
| 747 | ut_assertok(run_command("bootflow scan -l mmc1", 0)); |
| 748 | ut_assert_nextline("Scanning for bootflows with label 'mmc1'"); |
| 749 | ut_assert_skip_to_line("(1 bootflow, 1 valid)"); |
| 750 | ut_assert_console_end(); |
| 751 | |
| 752 | /* check that the hunter was used */ |
| 753 | ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used); |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 758 | |
| 759 | /* Check searching for a uclass label using the hunters */ |
| 760 | static int bootflow_cmd_hunt_label(struct unit_test_state *uts) |
| 761 | { |
| 762 | struct bootstd_priv *std; |
| 763 | |
| 764 | /* get access to the used hunters */ |
| 765 | ut_assertok(bootstd_get_priv(&std)); |
| 766 | |
| 767 | test_set_skip_delays(true); |
| 768 | test_set_eth_enable(false); |
| 769 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 770 | |
| 771 | console_record_reset_enable(); |
| 772 | ut_assertok(run_command("bootflow scan -l mmc", 0)); |
| 773 | |
| 774 | /* check that the hunter was used */ |
| 775 | ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used); |
| 776 | |
| 777 | /* check that we got the mmc1 bootflow */ |
| 778 | ut_assert_nextline("Scanning for bootflows with label 'mmc'"); |
| 779 | ut_assert_nextlinen("Seq"); |
| 780 | ut_assert_nextlinen("---"); |
| 781 | ut_assert_nextline("Hunting with: simple_bus"); |
| 782 | ut_assert_nextline("Found 2 extension board(s)."); |
| 783 | ut_assert_nextline("Hunting with: mmc"); |
| 784 | ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':"); |
| 785 | ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':"); |
| 786 | ut_assert_nextline( |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 787 | " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 788 | ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':"); |
| 789 | ut_assert_skip_to_line("(1 bootflow, 1 valid)"); |
| 790 | ut_assert_console_end(); |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 795 | |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 796 | /** |
| 797 | * check_font() - Check that the font size for an item matches expectations |
| 798 | * |
| 799 | * @uts: Unit test state |
| 800 | * @scn: Scene containing the text object |
| 801 | * @id: ID of the text object |
| 802 | * Returns 0 on success, -ve on failure |
| 803 | */ |
| 804 | static int check_font(struct unit_test_state *uts, struct scene *scn, uint id, |
| 805 | int font_size) |
| 806 | { |
| 807 | struct scene_obj_txt *txt; |
| 808 | |
| 809 | txt = scene_obj_find(scn, id, SCENEOBJT_TEXT); |
| 810 | ut_assertnonnull(txt); |
| 811 | |
| 812 | ut_asserteq(font_size, txt->font_size); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | /* Check themes work with a bootflow menu */ |
| 818 | static int bootflow_menu_theme(struct unit_test_state *uts) |
| 819 | { |
| 820 | const int font_size = 30; |
| 821 | struct scene *scn; |
| 822 | struct expo *exp; |
| 823 | ofnode node; |
| 824 | int i; |
| 825 | |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 826 | ut_assertok(scan_mmc4_bootdev(uts)); |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 827 | |
| 828 | ut_assertok(bootflow_menu_new(&exp)); |
| 829 | node = ofnode_path("/bootstd/theme"); |
| 830 | ut_assert(ofnode_valid(node)); |
| 831 | ut_assertok(bootflow_menu_apply_theme(exp, node)); |
| 832 | |
| 833 | scn = expo_lookup_scene_id(exp, MAIN); |
| 834 | ut_assertnonnull(scn); |
| 835 | |
| 836 | /* |
| 837 | * Check that the txt objects have the correct font size from the |
| 838 | * device tree node: bootstd/theme |
| 839 | * |
| 840 | * Check both menu items, since there are two bootflows |
| 841 | */ |
| 842 | ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size)); |
| 843 | ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size)); |
| 844 | for (i = 0; i < 2; i++) { |
| 845 | ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size)); |
| 846 | ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size)); |
| 847 | ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size)); |
| 848 | } |
| 849 | |
| 850 | expo_destroy(exp); |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | a08adca | 2023-07-12 09:04:38 -0600 | [diff] [blame] | 855 | |
| 856 | /** |
| 857 | * check_arg() - Check both the normal case and the buffer-overflow case |
| 858 | * |
| 859 | * @uts: Unit-test state |
| 860 | * @expect_ret: Expected return value (i.e. buffer length) |
| 861 | * @expect_str: String expected to be returned |
| 862 | * @buf: Buffer to use |
| 863 | * @from: Original cmdline to update |
| 864 | * @arg: Argument to update (e.g. "console") |
| 865 | * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present, |
| 866 | * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add |
| 867 | * it without any value ("initrd") |
| 868 | */ |
| 869 | static int check_arg(struct unit_test_state *uts, int expect_ret, |
| 870 | const char *expect_str, char *buf, const char *from, |
| 871 | const char *arg, const char *val) |
| 872 | { |
| 873 | /* check for writing outside the reported bounds */ |
| 874 | buf[expect_ret] = '['; |
| 875 | ut_asserteq(expect_ret, |
| 876 | cmdline_set_arg(buf, expect_ret, from, arg, val, NULL)); |
| 877 | ut_asserteq_str(expect_str, buf); |
| 878 | ut_asserteq('[', buf[expect_ret]); |
| 879 | |
| 880 | /* do the test again but with one less byte in the buffer */ |
| 881 | ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg, |
| 882 | val, NULL)); |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* Test of bootflow_cmdline_set_arg() */ |
| 888 | static int test_bootflow_cmdline_set(struct unit_test_state *uts) |
| 889 | { |
| 890 | char buf[50]; |
| 891 | const int size = sizeof(buf); |
| 892 | |
| 893 | /* |
| 894 | * note that buffer-overflow tests are immediately each test case, just |
| 895 | * top keep the code together |
| 896 | */ |
| 897 | |
| 898 | /* add an arg that doesn't already exist, starting from empty */ |
| 899 | ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL, |
| 900 | NULL)); |
| 901 | |
| 902 | ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY)); |
| 903 | ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", "")); |
| 904 | ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred")); |
| 905 | |
| 906 | /* add an arg that doesn't already exist, starting from non-empty */ |
| 907 | ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me", |
| 908 | BOOTFLOWCL_EMPTY)); |
| 909 | ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me", |
| 910 | "")); |
| 911 | ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me", |
| 912 | "fred")); |
| 913 | |
| 914 | /* update an arg at the start */ |
| 915 | ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL)); |
| 916 | ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg", |
| 917 | BOOTFLOWCL_EMPTY)); |
| 918 | ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", "")); |
| 919 | ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1")); |
| 920 | ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg", |
| 921 | "1234")); |
| 922 | |
| 923 | /* update an arg at the end */ |
| 924 | ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg", |
| 925 | NULL)); |
| 926 | ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg", |
| 927 | BOOTFLOWCL_EMPTY)); |
| 928 | ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg", |
| 929 | "")); |
| 930 | ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg", |
| 931 | "1")); |
| 932 | ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123", |
| 933 | "arg", "1234")); |
| 934 | |
| 935 | /* update an arg in the middle */ |
| 936 | ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf, |
| 937 | "mary=abc arg=123 john=2", "arg", NULL)); |
| 938 | ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf, |
| 939 | "mary=abc arg=123 john=2", "arg", |
| 940 | BOOTFLOWCL_EMPTY)); |
| 941 | ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf, |
| 942 | "mary=abc arg=123 john=2", "arg", "")); |
| 943 | ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf, |
| 944 | "mary=abc arg=123 john=2", "arg", "1")); |
| 945 | ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf, |
| 946 | "mary=abc arg=123 john=2", "arg", "1234")); |
| 947 | |
| 948 | /* handle existing args with quotes */ |
| 949 | ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf, |
| 950 | "mary=\"abc\" arg=123 john", "arg", NULL)); |
| 951 | |
| 952 | /* handle existing args with quoted spaces */ |
| 953 | ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf, |
| 954 | "mary=\"abc def\" arg=123 john", "arg", NULL)); |
| 955 | |
| 956 | ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4", |
| 957 | buf, "mary=\"abc def\" arg=123 john", "def", |
| 958 | "4")); |
| 959 | |
| 960 | /* quote at the start */ |
| 961 | ut_asserteq(-EBADF, cmdline_set_arg(buf, size, |
| 962 | "mary=\"abc def\" arg=\"123 456\"", |
| 963 | "arg", "\"4 5 6", NULL)); |
| 964 | |
| 965 | /* quote at the end */ |
| 966 | ut_asserteq(-EBADF, cmdline_set_arg(buf, size, |
| 967 | "mary=\"abc def\" arg=\"123 456\"", |
| 968 | "arg", "4 5 6\"", NULL)); |
| 969 | |
| 970 | /* quote in the middle */ |
| 971 | ut_asserteq(-EBADF, cmdline_set_arg(buf, size, |
| 972 | "mary=\"abc def\" arg=\"123 456\"", |
| 973 | "arg", "\"4 \"5 6\"", NULL)); |
| 974 | |
| 975 | /* handle updating a quoted arg */ |
| 976 | ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf, |
| 977 | "mary=\"abc def\" arg=\"123 456\"", "arg", |
| 978 | "4 5 6")); |
| 979 | |
| 980 | /* changing a quoted arg to a non-quoted arg */ |
| 981 | ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf, |
| 982 | "mary=\"abc def\" arg=\"123 456\"", "arg", |
| 983 | "789")); |
| 984 | |
| 985 | /* changing a non-quoted arg to a quoted arg */ |
| 986 | ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf, |
| 987 | "mary=\"abc def\" arg=123", "arg", "456 789")); |
| 988 | |
| 989 | /* handling of spaces */ |
| 990 | ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123")); |
| 991 | ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123")); |
| 992 | ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg", |
| 993 | "123")); |
| 994 | ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ", |
| 995 | "arg", "123")); |
| 996 | ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf, |
| 997 | " john arg=123 mary ", "arg", "123")); |
| 998 | |
| 999 | /* unchanged arg */ |
| 1000 | ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY)); |
| 1001 | |
| 1002 | /* arg which starts with the same name */ |
| 1003 | ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf, |
| 1004 | "mary=abc johnathon=2 john=1", "john", "3")); |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | BOOTSTD_TEST(test_bootflow_cmdline_set, 0); |
Simon Glass | 55a2da3 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 1009 | |
| 1010 | /* Test of bootflow_cmdline_set_arg() */ |
| 1011 | static int bootflow_set_arg(struct unit_test_state *uts) |
| 1012 | { |
| 1013 | struct bootflow s_bflow, *bflow = &s_bflow; |
| 1014 | ulong mem_start; |
| 1015 | |
| 1016 | ut_assertok(env_set("bootargs", NULL)); |
| 1017 | |
| 1018 | mem_start = ut_check_delta(0); |
| 1019 | |
| 1020 | /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */ |
| 1021 | bflow->cmdline = NULL; |
| 1022 | ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false)); |
| 1023 | ut_asserteq_str(bflow->cmdline, "fred=123"); |
| 1024 | |
| 1025 | ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false)); |
| 1026 | ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\""); |
| 1027 | |
| 1028 | ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false)); |
| 1029 | ut_asserteq_str(bflow->cmdline, "fred=123"); |
| 1030 | ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false)); |
| 1031 | ut_asserteq_ptr(bflow->cmdline, NULL); |
| 1032 | |
| 1033 | ut_asserteq(0, ut_check_delta(mem_start)); |
| 1034 | |
| 1035 | ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true)); |
| 1036 | ut_asserteq_str("mary=here", env_get("bootargs")); |
| 1037 | ut_assertok(env_set("bootargs", NULL)); |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | BOOTSTD_TEST(bootflow_set_arg, 0); |
| 1042 | |
| 1043 | /* Test of bootflow_cmdline_get_arg() */ |
| 1044 | static int bootflow_cmdline_get(struct unit_test_state *uts) |
| 1045 | { |
| 1046 | int pos; |
| 1047 | |
| 1048 | /* empty string */ |
| 1049 | ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos)); |
| 1050 | |
| 1051 | /* arg with empty value */ |
| 1052 | ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos)); |
| 1053 | ut_asserteq(5, pos); |
| 1054 | |
| 1055 | /* arg with a value */ |
| 1056 | ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos)); |
| 1057 | ut_asserteq(5, pos); |
| 1058 | |
| 1059 | /* arg with a value */ |
| 1060 | ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos)); |
| 1061 | ut_asserteq(12, pos); |
| 1062 | |
| 1063 | /* arg with a value, after quoted arg */ |
| 1064 | ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos)); |
| 1065 | ut_asserteq(16, pos); |
| 1066 | |
| 1067 | /* arg in the middle */ |
| 1068 | ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred", |
| 1069 | &pos)); |
| 1070 | ut_asserteq(15, pos); |
| 1071 | |
| 1072 | /* quoted arg */ |
| 1073 | ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23", |
| 1074 | "fred", &pos)); |
| 1075 | ut_asserteq(17, pos); |
| 1076 | |
| 1077 | /* args starting with the same prefix */ |
| 1078 | ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john", |
| 1079 | &pos)); |
| 1080 | ut_asserteq(26, pos); |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | BOOTSTD_TEST(bootflow_cmdline_get, 0); |
| 1085 | |
| 1086 | static int bootflow_cmdline(struct unit_test_state *uts) |
| 1087 | { |
| 1088 | ut_assertok(run_command("bootflow scan mmc", 0)); |
| 1089 | ut_assertok(run_command("bootflow sel 0", 0)); |
| 1090 | console_record_reset_enable(); |
| 1091 | |
| 1092 | ut_asserteq(1, run_command("bootflow cmdline get fred", 0)); |
| 1093 | ut_assert_nextline("Argument not found"); |
| 1094 | ut_assert_console_end(); |
| 1095 | |
| 1096 | ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0)); |
| 1097 | ut_asserteq(0, run_command("bootflow cmdline get fred", 0)); |
| 1098 | ut_assert_nextline("123"); |
| 1099 | |
| 1100 | ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0)); |
| 1101 | ut_asserteq(0, run_command("bootflow cmdline get mary", 0)); |
| 1102 | ut_assert_nextline("abc"); |
| 1103 | |
| 1104 | ut_asserteq(0, run_command("bootflow cmdline delete fred", 0)); |
| 1105 | ut_asserteq(1, run_command("bootflow cmdline get fred", 0)); |
| 1106 | ut_assert_nextline("Argument not found"); |
| 1107 | |
| 1108 | ut_asserteq(0, run_command("bootflow cmdline clear mary", 0)); |
| 1109 | ut_asserteq(0, run_command("bootflow cmdline get mary", 0)); |
| 1110 | ut_assert_nextline_empty(); |
| 1111 | |
Simon Glass | 7a164f8 | 2023-11-29 10:31:19 -0700 | [diff] [blame] | 1112 | ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0)); |
| 1113 | ut_asserteq(0, run_command("bootflow cmdline set mary", 0)); |
| 1114 | ut_assert_nextline_empty(); |
| 1115 | |
Simon Glass | 55a2da3 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 1116 | ut_assert_console_end(); |
| 1117 | |
| 1118 | return 0; |
| 1119 | } |
| 1120 | BOOTSTD_TEST(bootflow_cmdline, 0); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1121 | |
Simon Glass | a61057f | 2023-10-25 07:17:36 +1300 | [diff] [blame] | 1122 | /* test a few special changes to a long command line */ |
| 1123 | static int bootflow_cmdline_special(struct unit_test_state *uts) |
| 1124 | { |
| 1125 | char buf[500]; |
| 1126 | int pos; |
| 1127 | |
| 1128 | /* |
| 1129 | * check handling of an argument which has an embedded '=', as well as |
| 1130 | * handling of a argument which partially matches ("ro" and "root") |
| 1131 | */ |
| 1132 | ut_asserteq(32, cmdline_set_arg( |
| 1133 | buf, sizeof(buf), |
| 1134 | "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd", |
| 1135 | "root", NULL, &pos)); |
| 1136 | ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf); |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | BOOTSTD_TEST(bootflow_cmdline_special, 0); |
| 1141 | |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1142 | /* Test ChromiumOS bootmeth */ |
| 1143 | static int bootflow_cros(struct unit_test_state *uts) |
| 1144 | { |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 1145 | ut_assertok(scan_mmc_bootdev(uts, "mmc5", true)); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1146 | ut_assertok(run_command("bootflow list", 0)); |
| 1147 | |
| 1148 | ut_assert_nextlinen("Showing all"); |
| 1149 | ut_assert_nextlinen("Seq"); |
| 1150 | ut_assert_nextlinen("---"); |
| 1151 | ut_assert_nextlinen(" 0 extlinux"); |
Simon Glass | d7d3a97 | 2023-08-24 13:55:45 -0600 | [diff] [blame] | 1152 | ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 "); |
| 1153 | ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 "); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1154 | ut_assert_nextlinen("---"); |
Simon Glass | d7d3a97 | 2023-08-24 13:55:45 -0600 | [diff] [blame] | 1155 | ut_assert_skip_to_line("(3 bootflows, 3 valid)"); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1156 | |
| 1157 | ut_assert_console_end(); |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | BOOTSTD_TEST(bootflow_cros, 0); |