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 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 9 | #include <bootdev.h> |
| 10 | #include <bootflow.h> |
Simon Glass | c8d3721 | 2022-07-30 15:52:34 -0600 | [diff] [blame] | 11 | #include <bootmeth.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 12 | #include <bootstd.h> |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 13 | #include <cli.h> |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Heinrich Schuchardt | 4226d9d | 2024-04-03 23:39:48 +0200 | [diff] [blame] | 15 | #include <efi_default_filename.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 | |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 30 | extern U_BOOT_DRIVER(bootmeth_android); |
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 |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 522 | * @bind_cros: true to bind the ChromiumOS and Android bootmeths |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 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, |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 527 | bool bind_cros_android, 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 */ |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 549 | if (IS_ENABLED(CONFIG_BOOTMETH_CROS) && bind_cros_android) { |
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 | |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 555 | /* Enable the android bootmeths if needed */ |
| 556 | if (IS_ENABLED(CONFIG_BOOTMETH_ANDROID) && bind_cros_android) { |
| 557 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 558 | ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_android), |
| 559 | "android", 0, ofnode_null(), &dev)); |
| 560 | } |
| 561 | |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 562 | /* Change the order to include the device */ |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 563 | std = dev_get_priv(bootstd); |
| 564 | old_order = std->bootdev_order; |
| 565 | std->bootdev_order = order; |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 566 | *old_orderp = old_order; |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros |
| 573 | * |
| 574 | * @uts: Unit test state |
| 575 | * @mmc_dev: MMC device to use, e.g. "mmc4" |
| 576 | * @bind_cros: true to bind the ChromiumOS bootmeth |
| 577 | * Returns 0 on success, -ve on failure |
| 578 | */ |
| 579 | static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev, |
| 580 | bool bind_cros) |
| 581 | { |
| 582 | struct bootstd_priv *std; |
| 583 | struct udevice *bootstd; |
| 584 | const char **old_order; |
| 585 | |
| 586 | ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order)); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 587 | |
| 588 | console_record_reset_enable(); |
| 589 | ut_assertok(run_command("bootflow scan", 0)); |
| 590 | ut_assert_console_end(); |
| 591 | |
| 592 | /* Restore the order used by the device tree */ |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 593 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 594 | std = dev_get_priv(bootstd); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 595 | std->bootdev_order = old_order; |
| 596 | |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 600 | /** |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 601 | * scan_mmc_android_bootdev() - Set up an mmc bootdev so we can access other |
| 602 | * distros. Android bootflow might print "ANDROID:*" while scanning |
| 603 | * |
| 604 | * @uts: Unit test state |
| 605 | * @mmc_dev: MMC device to use, e.g. "mmc4" |
| 606 | * Returns 0 on success, -ve on failure |
| 607 | */ |
| 608 | static int scan_mmc_android_bootdev(struct unit_test_state *uts, const char *mmc_dev) |
| 609 | { |
| 610 | struct bootstd_priv *std; |
| 611 | struct udevice *bootstd; |
| 612 | const char **old_order; |
| 613 | |
| 614 | ut_assertok(prep_mmc_bootdev(uts, mmc_dev, true, &old_order)); |
| 615 | |
| 616 | console_record_reset_enable(); |
| 617 | ut_assertok(run_command("bootflow scan", 0)); |
| 618 | /* Android bootflow might print one or two 'ANDROID:*' logs */ |
| 619 | ut_check_skipline(uts); |
| 620 | ut_check_skipline(uts); |
| 621 | ut_assert_console_end(); |
| 622 | |
| 623 | /* Restore the order used by the device tree */ |
| 624 | ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); |
| 625 | std = dev_get_priv(bootstd); |
| 626 | std->bootdev_order = old_order; |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /** |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 632 | * 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] | 633 | * |
| 634 | * @uts: Unit test state |
| 635 | * Returns 0 on success, -ve on failure |
| 636 | */ |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 637 | static int scan_mmc4_bootdev(struct unit_test_state *uts) |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 638 | { |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 639 | ut_assertok(scan_mmc_bootdev(uts, "mmc4", false)); |
Simon Glass | bb76a5c | 2023-08-24 13:55:40 -0600 | [diff] [blame] | 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 644 | /* Check 'bootflow menu' to select a bootflow */ |
| 645 | static int bootflow_cmd_menu(struct unit_test_state *uts) |
| 646 | { |
Simon Glass | 6d5083b | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 647 | struct bootstd_priv *std; |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 648 | char prev[3]; |
| 649 | |
Simon Glass | 6d5083b | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 650 | /* get access to the current bootflow */ |
| 651 | ut_assertok(bootstd_get_priv(&std)); |
| 652 | |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 653 | ut_assertok(scan_mmc4_bootdev(uts)); |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 654 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 655 | /* Add keypresses to move to and select the second one in the list */ |
| 656 | prev[0] = CTL_CH('n'); |
| 657 | prev[1] = '\r'; |
| 658 | prev[2] = '\0'; |
| 659 | ut_asserteq(2, console_in_puts(prev)); |
| 660 | |
| 661 | ut_assertok(run_command("bootflow menu", 0)); |
| 662 | ut_assert_nextline("Selected: Armbian"); |
Simon Glass | 6d5083b | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 663 | ut_assertnonnull(std->cur_bootflow); |
| 664 | ut_assert_console_end(); |
| 665 | |
| 666 | /* Check not selecting anything */ |
| 667 | prev[0] = '\e'; |
| 668 | prev[1] = '\0'; |
| 669 | ut_asserteq(1, console_in_puts(prev)); |
| 670 | |
| 671 | ut_asserteq(1, run_command("bootflow menu", 0)); |
| 672 | ut_assertnull(std->cur_bootflow); |
| 673 | ut_assert_nextline("Nothing chosen"); |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 674 | ut_assert_console_end(); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 679 | |
Simon Glass | 9bf2786 | 2023-10-01 19:15:25 -0600 | [diff] [blame] | 680 | /* Check 'bootflow scan -m' to select a bootflow using a menu */ |
| 681 | static int bootflow_scan_menu(struct unit_test_state *uts) |
| 682 | { |
| 683 | struct bootstd_priv *std; |
| 684 | const char **old_order, **new_order; |
| 685 | char prev[3]; |
| 686 | |
| 687 | /* get access to the current bootflow */ |
| 688 | ut_assertok(bootstd_get_priv(&std)); |
| 689 | |
| 690 | ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order)); |
| 691 | |
| 692 | /* Add keypresses to move to and select the second one in the list */ |
| 693 | prev[0] = CTL_CH('n'); |
| 694 | prev[1] = '\r'; |
| 695 | prev[2] = '\0'; |
| 696 | ut_asserteq(2, console_in_puts(prev)); |
| 697 | |
| 698 | ut_assertok(run_command("bootflow scan -lm", 0)); |
| 699 | new_order = std->bootdev_order; |
| 700 | std->bootdev_order = old_order; |
| 701 | |
| 702 | ut_assert_skip_to_line("No more bootdevs"); |
| 703 | ut_assert_nextlinen("--"); |
| 704 | ut_assert_nextline("(2 bootflows, 2 valid)"); |
| 705 | |
| 706 | ut_assert_nextline("Selected: Armbian"); |
| 707 | ut_assertnonnull(std->cur_bootflow); |
| 708 | ut_assert_console_end(); |
| 709 | |
| 710 | /* Check not selecting anything */ |
| 711 | prev[0] = '\e'; |
| 712 | prev[1] = '\0'; |
| 713 | ut_asserteq(1, console_in_puts(prev)); |
| 714 | |
| 715 | std->bootdev_order = new_order; /* Blue Monday */ |
| 716 | ut_assertok(run_command("bootflow scan -lm", 0)); |
| 717 | std->bootdev_order = old_order; |
| 718 | |
| 719 | ut_assertnull(std->cur_bootflow); |
| 720 | ut_assert_skip_to_line("(2 bootflows, 2 valid)"); |
| 721 | ut_assert_nextline("Nothing chosen"); |
| 722 | ut_assert_console_end(); |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | BOOTSTD_TEST(bootflow_scan_menu, |
| 727 | UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC); |
| 728 | |
| 729 | /* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */ |
| 730 | static int bootflow_scan_menu_boot(struct unit_test_state *uts) |
| 731 | { |
| 732 | struct bootstd_priv *std; |
| 733 | const char **old_order; |
| 734 | char prev[3]; |
| 735 | |
| 736 | /* get access to the current bootflow */ |
| 737 | ut_assertok(bootstd_get_priv(&std)); |
| 738 | |
| 739 | ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order)); |
| 740 | |
| 741 | /* Add keypresses to move to and select the second one in the list */ |
| 742 | prev[0] = CTL_CH('n'); |
| 743 | prev[1] = '\r'; |
| 744 | prev[2] = '\0'; |
| 745 | ut_asserteq(2, console_in_puts(prev)); |
| 746 | |
| 747 | ut_assertok(run_command("bootflow scan -lmb", 0)); |
| 748 | std->bootdev_order = old_order; |
| 749 | |
| 750 | ut_assert_skip_to_line("(2 bootflows, 2 valid)"); |
| 751 | |
| 752 | ut_assert_nextline("Selected: Armbian"); |
Francis Laniel | 5b64c45 | 2023-12-22 22:02:35 +0100 | [diff] [blame] | 753 | |
| 754 | if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { |
| 755 | /* |
| 756 | * With old hush, despite booti failing to boot, i.e. returning |
| 757 | * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as |
| 758 | * we are using bootmeth_script here, to return -EFAULT. |
| 759 | */ |
| 760 | ut_assert_skip_to_line("Boot failed (err=-14)"); |
| 761 | } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { |
| 762 | /* |
| 763 | * While with modern one, run_command() propagates CMD_RET_FAILURE returned |
| 764 | * by booti, so we get 1 here. |
| 765 | */ |
| 766 | ut_assert_skip_to_line("Boot failed (err=1)"); |
| 767 | } |
Simon Glass | 9bf2786 | 2023-10-01 19:15:25 -0600 | [diff] [blame] | 768 | ut_assertnonnull(std->cur_bootflow); |
| 769 | ut_assert_console_end(); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | BOOTSTD_TEST(bootflow_scan_menu_boot, |
| 774 | UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC); |
| 775 | |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 776 | /* Check searching for a single bootdev using the hunters */ |
| 777 | static int bootflow_cmd_hunt_single(struct unit_test_state *uts) |
| 778 | { |
| 779 | struct bootstd_priv *std; |
| 780 | |
| 781 | /* get access to the used hunters */ |
| 782 | ut_assertok(bootstd_get_priv(&std)); |
| 783 | |
| 784 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 785 | |
| 786 | console_record_reset_enable(); |
| 787 | ut_assertok(run_command("bootflow scan -l mmc1", 0)); |
| 788 | ut_assert_nextline("Scanning for bootflows with label 'mmc1'"); |
| 789 | ut_assert_skip_to_line("(1 bootflow, 1 valid)"); |
| 790 | ut_assert_console_end(); |
| 791 | |
| 792 | /* check that the hunter was used */ |
| 793 | ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used); |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 798 | |
| 799 | /* Check searching for a uclass label using the hunters */ |
| 800 | static int bootflow_cmd_hunt_label(struct unit_test_state *uts) |
| 801 | { |
| 802 | struct bootstd_priv *std; |
| 803 | |
| 804 | /* get access to the used hunters */ |
| 805 | ut_assertok(bootstd_get_priv(&std)); |
| 806 | |
| 807 | test_set_skip_delays(true); |
| 808 | test_set_eth_enable(false); |
| 809 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 810 | |
| 811 | console_record_reset_enable(); |
| 812 | ut_assertok(run_command("bootflow scan -l mmc", 0)); |
| 813 | |
| 814 | /* check that the hunter was used */ |
| 815 | ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used); |
| 816 | |
| 817 | /* check that we got the mmc1 bootflow */ |
| 818 | ut_assert_nextline("Scanning for bootflows with label 'mmc'"); |
| 819 | ut_assert_nextlinen("Seq"); |
| 820 | ut_assert_nextlinen("---"); |
| 821 | ut_assert_nextline("Hunting with: simple_bus"); |
| 822 | ut_assert_nextline("Found 2 extension board(s)."); |
| 823 | ut_assert_nextline("Hunting with: mmc"); |
| 824 | ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':"); |
| 825 | ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':"); |
| 826 | ut_assert_nextline( |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 827 | " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 828 | ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':"); |
| 829 | ut_assert_skip_to_line("(1 bootflow, 1 valid)"); |
| 830 | ut_assert_console_end(); |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 835 | |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 836 | /** |
| 837 | * check_font() - Check that the font size for an item matches expectations |
| 838 | * |
| 839 | * @uts: Unit test state |
| 840 | * @scn: Scene containing the text object |
| 841 | * @id: ID of the text object |
| 842 | * Returns 0 on success, -ve on failure |
| 843 | */ |
| 844 | static int check_font(struct unit_test_state *uts, struct scene *scn, uint id, |
| 845 | int font_size) |
| 846 | { |
| 847 | struct scene_obj_txt *txt; |
| 848 | |
| 849 | txt = scene_obj_find(scn, id, SCENEOBJT_TEXT); |
| 850 | ut_assertnonnull(txt); |
| 851 | |
| 852 | ut_asserteq(font_size, txt->font_size); |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | /* Check themes work with a bootflow menu */ |
| 858 | static int bootflow_menu_theme(struct unit_test_state *uts) |
| 859 | { |
| 860 | const int font_size = 30; |
| 861 | struct scene *scn; |
| 862 | struct expo *exp; |
| 863 | ofnode node; |
| 864 | int i; |
| 865 | |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 866 | ut_assertok(scan_mmc4_bootdev(uts)); |
Simon Glass | d92bcc4 | 2023-01-06 08:52:42 -0600 | [diff] [blame] | 867 | |
| 868 | ut_assertok(bootflow_menu_new(&exp)); |
| 869 | node = ofnode_path("/bootstd/theme"); |
| 870 | ut_assert(ofnode_valid(node)); |
| 871 | ut_assertok(bootflow_menu_apply_theme(exp, node)); |
| 872 | |
| 873 | scn = expo_lookup_scene_id(exp, MAIN); |
| 874 | ut_assertnonnull(scn); |
| 875 | |
| 876 | /* |
| 877 | * Check that the txt objects have the correct font size from the |
| 878 | * device tree node: bootstd/theme |
| 879 | * |
| 880 | * Check both menu items, since there are two bootflows |
| 881 | */ |
| 882 | ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size)); |
| 883 | ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size)); |
| 884 | for (i = 0; i < 2; i++) { |
| 885 | ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size)); |
| 886 | ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size)); |
| 887 | ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size)); |
| 888 | } |
| 889 | |
| 890 | expo_destroy(exp); |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | a08adca | 2023-07-12 09:04:38 -0600 | [diff] [blame] | 895 | |
| 896 | /** |
| 897 | * check_arg() - Check both the normal case and the buffer-overflow case |
| 898 | * |
| 899 | * @uts: Unit-test state |
| 900 | * @expect_ret: Expected return value (i.e. buffer length) |
| 901 | * @expect_str: String expected to be returned |
| 902 | * @buf: Buffer to use |
| 903 | * @from: Original cmdline to update |
| 904 | * @arg: Argument to update (e.g. "console") |
| 905 | * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present, |
| 906 | * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add |
| 907 | * it without any value ("initrd") |
| 908 | */ |
| 909 | static int check_arg(struct unit_test_state *uts, int expect_ret, |
| 910 | const char *expect_str, char *buf, const char *from, |
| 911 | const char *arg, const char *val) |
| 912 | { |
| 913 | /* check for writing outside the reported bounds */ |
| 914 | buf[expect_ret] = '['; |
| 915 | ut_asserteq(expect_ret, |
| 916 | cmdline_set_arg(buf, expect_ret, from, arg, val, NULL)); |
| 917 | ut_asserteq_str(expect_str, buf); |
| 918 | ut_asserteq('[', buf[expect_ret]); |
| 919 | |
| 920 | /* do the test again but with one less byte in the buffer */ |
| 921 | ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg, |
| 922 | val, NULL)); |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | /* Test of bootflow_cmdline_set_arg() */ |
| 928 | static int test_bootflow_cmdline_set(struct unit_test_state *uts) |
| 929 | { |
| 930 | char buf[50]; |
| 931 | const int size = sizeof(buf); |
| 932 | |
| 933 | /* |
| 934 | * note that buffer-overflow tests are immediately each test case, just |
| 935 | * top keep the code together |
| 936 | */ |
| 937 | |
| 938 | /* add an arg that doesn't already exist, starting from empty */ |
| 939 | ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL, |
| 940 | NULL)); |
| 941 | |
| 942 | ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY)); |
| 943 | ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", "")); |
| 944 | ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred")); |
| 945 | |
| 946 | /* add an arg that doesn't already exist, starting from non-empty */ |
| 947 | ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me", |
| 948 | BOOTFLOWCL_EMPTY)); |
| 949 | ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me", |
| 950 | "")); |
| 951 | ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me", |
| 952 | "fred")); |
| 953 | |
| 954 | /* update an arg at the start */ |
| 955 | ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL)); |
| 956 | ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg", |
| 957 | BOOTFLOWCL_EMPTY)); |
| 958 | ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", "")); |
| 959 | ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1")); |
| 960 | ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg", |
| 961 | "1234")); |
| 962 | |
| 963 | /* update an arg at the end */ |
| 964 | ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg", |
| 965 | NULL)); |
| 966 | ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg", |
| 967 | BOOTFLOWCL_EMPTY)); |
| 968 | ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg", |
| 969 | "")); |
| 970 | ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg", |
| 971 | "1")); |
| 972 | ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123", |
| 973 | "arg", "1234")); |
| 974 | |
| 975 | /* update an arg in the middle */ |
| 976 | ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf, |
| 977 | "mary=abc arg=123 john=2", "arg", NULL)); |
| 978 | ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf, |
| 979 | "mary=abc arg=123 john=2", "arg", |
| 980 | BOOTFLOWCL_EMPTY)); |
| 981 | ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf, |
| 982 | "mary=abc arg=123 john=2", "arg", "")); |
| 983 | ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf, |
| 984 | "mary=abc arg=123 john=2", "arg", "1")); |
| 985 | ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf, |
| 986 | "mary=abc arg=123 john=2", "arg", "1234")); |
| 987 | |
| 988 | /* handle existing args with quotes */ |
| 989 | ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf, |
| 990 | "mary=\"abc\" arg=123 john", "arg", NULL)); |
| 991 | |
| 992 | /* handle existing args with quoted spaces */ |
| 993 | ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf, |
| 994 | "mary=\"abc def\" arg=123 john", "arg", NULL)); |
| 995 | |
| 996 | ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4", |
| 997 | buf, "mary=\"abc def\" arg=123 john", "def", |
| 998 | "4")); |
| 999 | |
| 1000 | /* quote at the start */ |
| 1001 | ut_asserteq(-EBADF, cmdline_set_arg(buf, size, |
| 1002 | "mary=\"abc def\" arg=\"123 456\"", |
| 1003 | "arg", "\"4 5 6", NULL)); |
| 1004 | |
| 1005 | /* quote at the end */ |
| 1006 | ut_asserteq(-EBADF, cmdline_set_arg(buf, size, |
| 1007 | "mary=\"abc def\" arg=\"123 456\"", |
| 1008 | "arg", "4 5 6\"", NULL)); |
| 1009 | |
| 1010 | /* quote in the middle */ |
| 1011 | ut_asserteq(-EBADF, cmdline_set_arg(buf, size, |
| 1012 | "mary=\"abc def\" arg=\"123 456\"", |
| 1013 | "arg", "\"4 \"5 6\"", NULL)); |
| 1014 | |
| 1015 | /* handle updating a quoted arg */ |
| 1016 | ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf, |
| 1017 | "mary=\"abc def\" arg=\"123 456\"", "arg", |
| 1018 | "4 5 6")); |
| 1019 | |
| 1020 | /* changing a quoted arg to a non-quoted arg */ |
| 1021 | ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf, |
| 1022 | "mary=\"abc def\" arg=\"123 456\"", "arg", |
| 1023 | "789")); |
| 1024 | |
| 1025 | /* changing a non-quoted arg to a quoted arg */ |
| 1026 | ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf, |
| 1027 | "mary=\"abc def\" arg=123", "arg", "456 789")); |
| 1028 | |
| 1029 | /* handling of spaces */ |
| 1030 | ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123")); |
| 1031 | ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123")); |
| 1032 | ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg", |
| 1033 | "123")); |
| 1034 | ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ", |
| 1035 | "arg", "123")); |
| 1036 | ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf, |
| 1037 | " john arg=123 mary ", "arg", "123")); |
| 1038 | |
| 1039 | /* unchanged arg */ |
| 1040 | ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY)); |
| 1041 | |
| 1042 | /* arg which starts with the same name */ |
| 1043 | ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf, |
| 1044 | "mary=abc johnathon=2 john=1", "john", "3")); |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | BOOTSTD_TEST(test_bootflow_cmdline_set, 0); |
Simon Glass | 55a2da3 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 1049 | |
| 1050 | /* Test of bootflow_cmdline_set_arg() */ |
| 1051 | static int bootflow_set_arg(struct unit_test_state *uts) |
| 1052 | { |
| 1053 | struct bootflow s_bflow, *bflow = &s_bflow; |
| 1054 | ulong mem_start; |
| 1055 | |
| 1056 | ut_assertok(env_set("bootargs", NULL)); |
| 1057 | |
| 1058 | mem_start = ut_check_delta(0); |
| 1059 | |
| 1060 | /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */ |
| 1061 | bflow->cmdline = NULL; |
| 1062 | ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false)); |
| 1063 | ut_asserteq_str(bflow->cmdline, "fred=123"); |
| 1064 | |
| 1065 | ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false)); |
| 1066 | ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\""); |
| 1067 | |
| 1068 | ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false)); |
| 1069 | ut_asserteq_str(bflow->cmdline, "fred=123"); |
| 1070 | ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false)); |
| 1071 | ut_asserteq_ptr(bflow->cmdline, NULL); |
| 1072 | |
| 1073 | ut_asserteq(0, ut_check_delta(mem_start)); |
| 1074 | |
| 1075 | ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true)); |
| 1076 | ut_asserteq_str("mary=here", env_get("bootargs")); |
| 1077 | ut_assertok(env_set("bootargs", NULL)); |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | BOOTSTD_TEST(bootflow_set_arg, 0); |
| 1082 | |
| 1083 | /* Test of bootflow_cmdline_get_arg() */ |
| 1084 | static int bootflow_cmdline_get(struct unit_test_state *uts) |
| 1085 | { |
| 1086 | int pos; |
| 1087 | |
| 1088 | /* empty string */ |
| 1089 | ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos)); |
| 1090 | |
| 1091 | /* arg with empty value */ |
| 1092 | ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos)); |
| 1093 | ut_asserteq(5, pos); |
| 1094 | |
| 1095 | /* arg with a value */ |
| 1096 | ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos)); |
| 1097 | ut_asserteq(5, pos); |
| 1098 | |
| 1099 | /* arg with a value */ |
| 1100 | ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos)); |
| 1101 | ut_asserteq(12, pos); |
| 1102 | |
| 1103 | /* arg with a value, after quoted arg */ |
| 1104 | ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos)); |
| 1105 | ut_asserteq(16, pos); |
| 1106 | |
| 1107 | /* arg in the middle */ |
| 1108 | ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred", |
| 1109 | &pos)); |
| 1110 | ut_asserteq(15, pos); |
| 1111 | |
| 1112 | /* quoted arg */ |
| 1113 | ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23", |
| 1114 | "fred", &pos)); |
| 1115 | ut_asserteq(17, pos); |
| 1116 | |
| 1117 | /* args starting with the same prefix */ |
| 1118 | ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john", |
| 1119 | &pos)); |
| 1120 | ut_asserteq(26, pos); |
| 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | BOOTSTD_TEST(bootflow_cmdline_get, 0); |
| 1125 | |
| 1126 | static int bootflow_cmdline(struct unit_test_state *uts) |
| 1127 | { |
| 1128 | ut_assertok(run_command("bootflow scan mmc", 0)); |
| 1129 | ut_assertok(run_command("bootflow sel 0", 0)); |
| 1130 | console_record_reset_enable(); |
| 1131 | |
| 1132 | ut_asserteq(1, run_command("bootflow cmdline get fred", 0)); |
| 1133 | ut_assert_nextline("Argument not found"); |
| 1134 | ut_assert_console_end(); |
| 1135 | |
| 1136 | ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0)); |
| 1137 | ut_asserteq(0, run_command("bootflow cmdline get fred", 0)); |
| 1138 | ut_assert_nextline("123"); |
| 1139 | |
| 1140 | ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0)); |
| 1141 | ut_asserteq(0, run_command("bootflow cmdline get mary", 0)); |
| 1142 | ut_assert_nextline("abc"); |
| 1143 | |
| 1144 | ut_asserteq(0, run_command("bootflow cmdline delete fred", 0)); |
| 1145 | ut_asserteq(1, run_command("bootflow cmdline get fred", 0)); |
| 1146 | ut_assert_nextline("Argument not found"); |
| 1147 | |
| 1148 | ut_asserteq(0, run_command("bootflow cmdline clear mary", 0)); |
| 1149 | ut_asserteq(0, run_command("bootflow cmdline get mary", 0)); |
| 1150 | ut_assert_nextline_empty(); |
| 1151 | |
Simon Glass | 7a164f8 | 2023-11-29 10:31:19 -0700 | [diff] [blame] | 1152 | ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0)); |
| 1153 | ut_asserteq(0, run_command("bootflow cmdline set mary", 0)); |
| 1154 | ut_assert_nextline_empty(); |
| 1155 | |
Simon Glass | 55a2da3 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 1156 | ut_assert_console_end(); |
| 1157 | |
| 1158 | return 0; |
| 1159 | } |
| 1160 | BOOTSTD_TEST(bootflow_cmdline, 0); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1161 | |
Simon Glass | a61057f | 2023-10-25 07:17:36 +1300 | [diff] [blame] | 1162 | /* test a few special changes to a long command line */ |
| 1163 | static int bootflow_cmdline_special(struct unit_test_state *uts) |
| 1164 | { |
| 1165 | char buf[500]; |
| 1166 | int pos; |
| 1167 | |
| 1168 | /* |
| 1169 | * check handling of an argument which has an embedded '=', as well as |
| 1170 | * handling of a argument which partially matches ("ro" and "root") |
| 1171 | */ |
| 1172 | ut_asserteq(32, cmdline_set_arg( |
| 1173 | buf, sizeof(buf), |
| 1174 | "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd", |
| 1175 | "root", NULL, &pos)); |
| 1176 | ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf); |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | BOOTSTD_TEST(bootflow_cmdline_special, 0); |
| 1181 | |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1182 | /* Test ChromiumOS bootmeth */ |
| 1183 | static int bootflow_cros(struct unit_test_state *uts) |
| 1184 | { |
Simon Glass | 3ff8a9a | 2023-10-01 19:14:37 -0600 | [diff] [blame] | 1185 | ut_assertok(scan_mmc_bootdev(uts, "mmc5", true)); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1186 | ut_assertok(run_command("bootflow list", 0)); |
| 1187 | |
| 1188 | ut_assert_nextlinen("Showing all"); |
| 1189 | ut_assert_nextlinen("Seq"); |
| 1190 | ut_assert_nextlinen("---"); |
| 1191 | ut_assert_nextlinen(" 0 extlinux"); |
Simon Glass | d7d3a97 | 2023-08-24 13:55:45 -0600 | [diff] [blame] | 1192 | ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 "); |
| 1193 | ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 "); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1194 | ut_assert_nextlinen("---"); |
Simon Glass | d7d3a97 | 2023-08-24 13:55:45 -0600 | [diff] [blame] | 1195 | ut_assert_skip_to_line("(3 bootflows, 3 valid)"); |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 1196 | |
| 1197 | ut_assert_console_end(); |
| 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | BOOTSTD_TEST(bootflow_cros, 0); |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 1202 | |
| 1203 | /* Test Android bootmeth */ |
| 1204 | static int bootflow_android(struct unit_test_state *uts) |
| 1205 | { |
| 1206 | if (!IS_ENABLED(CONFIG_BOOTMETH_ANDROID)) |
| 1207 | return -EAGAIN; |
| 1208 | |
| 1209 | ut_assertok(scan_mmc_android_bootdev(uts, "mmc7")); |
| 1210 | ut_assertok(run_command("bootflow list", 0)); |
| 1211 | |
| 1212 | ut_assert_nextlinen("Showing all"); |
| 1213 | ut_assert_nextlinen("Seq"); |
| 1214 | ut_assert_nextlinen("---"); |
| 1215 | ut_assert_nextlinen(" 0 extlinux"); |
| 1216 | ut_assert_nextlinen(" 1 android ready mmc 0 mmc7.bootdev.whole "); |
| 1217 | ut_assert_nextlinen("---"); |
| 1218 | ut_assert_skip_to_line("(2 bootflows, 2 valid)"); |
| 1219 | |
| 1220 | ut_assert_console_end(); |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | BOOTSTD_TEST(bootflow_android, 0); |