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