From 3c2e531cb8f19841cb734c18623832c73d16c26b Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:31 -0600 Subject: bootstd: Correct the name of the QEMU bootmeth This does not relate to sandbox. Correct the name. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/bootmeth_qfw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'boot') diff --git a/boot/bootmeth_qfw.c b/boot/bootmeth_qfw.c index ecd4b082fd2..8ebbc3ebcd5 100644 --- a/boot/bootmeth_qfw.c +++ b/boot/bootmeth_qfw.c @@ -76,7 +76,7 @@ static int qfw_bootmeth_bind(struct udevice *dev) { struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); - plat->desc = "Sandbox boot for testing"; + plat->desc = "QEMU boot using firmware interface"; return 0; } -- cgit v1.3.1 From 4de979f6646d05d13572ecd2b652f0358dfa5de6 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:32 -0600 Subject: bootstd: Use bootdev instead of bootdevice It seems better to call this a 'bootdev' since this is name used in the documentation. The older 'Bootdevice' name is no-longer used and may cause confusion with the 'bootdevice' environment variable. Update throughout to use bootdev. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/bootflow.c | 4 ++-- drivers/mmc/mmc_bootdev.c | 2 +- drivers/scsi/scsi_bootdev.c | 2 +- drivers/usb/host/usb_bootdev.c | 2 +- fs/sandbox/host_bootdev.c | 2 +- include/bootdev.h | 2 +- include/bootflow.h | 2 +- net/eth_bootdev.c | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) (limited to 'boot') diff --git a/boot/bootflow.c b/boot/bootflow.c index 8f2cb876bb4..487552fa28c 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -315,14 +315,14 @@ static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow) /* If we got a valid bootflow, return it */ if (!ret) { - log_debug("Bootdevice '%s' part %d method '%s': Found bootflow\n", + log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n", dev->name, iter->part, iter->method->name); return 0; } /* Unless there is nothing more to try, move to the next device */ else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) { - log_debug("Bootdevice '%s' part %d method '%s': Error %d\n", + log_debug("Bootdev '%s' part %d method '%s': Error %d\n", dev->name, iter->part, iter->method->name, ret); /* * For 'all' we return all bootflows, even diff --git a/drivers/mmc/mmc_bootdev.c b/drivers/mmc/mmc_bootdev.c index b57b8a62276..55ecead2ddf 100644 --- a/drivers/mmc/mmc_bootdev.c +++ b/drivers/mmc/mmc_bootdev.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Bootdevice for MMC + * Bootdev for MMC * * Copyright 2021 Google LLC * Written by Simon Glass diff --git a/drivers/scsi/scsi_bootdev.c b/drivers/scsi/scsi_bootdev.c index 991013fe1ef..218221fa306 100644 --- a/drivers/scsi/scsi_bootdev.c +++ b/drivers/scsi/scsi_bootdev.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Bootdevice for USB + * Bootdev for SCSI * * Copyright 2021 Google LLC * Written by Simon Glass diff --git a/drivers/usb/host/usb_bootdev.c b/drivers/usb/host/usb_bootdev.c index 06e8f61aa1c..7fa1c601dff 100644 --- a/drivers/usb/host/usb_bootdev.c +++ b/drivers/usb/host/usb_bootdev.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Bootdevice for USB + * Bootdev for USB * * Copyright 2021 Google LLC * Written by Simon Glass diff --git a/fs/sandbox/host_bootdev.c b/fs/sandbox/host_bootdev.c index 0d12ee4ef74..3ef53627608 100644 --- a/fs/sandbox/host_bootdev.c +++ b/fs/sandbox/host_bootdev.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Bootdevice for MMC + * Bootdev for sandbox host * * Copyright 2021 Google LLC * Written by Simon Glass diff --git a/include/bootdev.h b/include/bootdev.h index e72ef3650f7..1533adfe506 100644 --- a/include/bootdev.h +++ b/include/bootdev.h @@ -200,7 +200,7 @@ void bootdev_clear_bootflows(struct udevice *dev); * All fields in @bflow must be set up. Note that @bflow->dev is used to add the * bootflow to that device. * - * @dev: Bootdevice device to add to + * @dev: Bootdev device to add to * @bflow: Bootflow to add. Note that fields within bflow must be allocated * since this function takes over ownership of these. This functions makes * a copy of @bflow itself (without allocating its fields again), so the diff --git a/include/bootflow.h b/include/bootflow.h index f20f575030f..018d021b810 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -58,7 +58,7 @@ enum bootflow_flags_t { * * @bm_node: Points to siblings in the same bootdev * @glob_node: Points to siblings in the global list (all bootdev) - * @dev: Bootdevice device which produced this bootflow + * @dev: Bootdev device which produced this bootflow * @blk: Block device which contains this bootflow, NULL if this is a network * device or sandbox 'host' device * @part: Partition number (0 for whole device) diff --git a/net/eth_bootdev.c b/net/eth_bootdev.c index f7b4196f78d..869adf8cbbd 100644 --- a/net/eth_bootdev.c +++ b/net/eth_bootdev.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Bootdevice for ethernet (uses PXE) + * Bootdev for ethernet (uses PXE) * * Copyright 2021 Google LLC * Written by Simon Glass -- cgit v1.3.1 From 2270c3639ae355e298f245ec1c9ba9f560bc7e5c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:33 -0600 Subject: bootstd: Correct baudrate typo This is a copy error. Fix it. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/bootmeth-uclass.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'boot') diff --git a/boot/bootmeth-uclass.c b/boot/bootmeth-uclass.c index 701ee8ad1c8..eeded08dd42 100644 --- a/boot/bootmeth-uclass.c +++ b/boot/bootmeth-uclass.c @@ -395,7 +395,7 @@ int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow, /** * on_bootmeths() - Update the bootmeth order * - * This will check for a valid baudrate and only apply it if valid. + * This will check for a valid list of bootmeths and only apply it if valid. */ static int on_bootmeths(const char *name, const char *value, enum env_op op, int flags) -- cgit v1.3.1 From d42243fe21c8847cd5c6db4e11b2aee660448451 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:35 -0600 Subject: bootstd: Use the bootargs env var for changing the cmdline The "bootargs" environment variable is used to set the command-line arguments to pass to the OS. Use this same mechanism with bootstd as well. When the variable is updated, it is written to the current bootflow. When the current bootflow is updated, the environment variable is updated too. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/bootflow.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/bootflow.c | 6 ++++++ include/env_callback.h | 6 ++++-- 3 files changed, 68 insertions(+), 2 deletions(-) (limited to 'boot') diff --git a/boot/bootflow.c b/boot/bootflow.c index 487552fa28c..b820678d41a 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -552,3 +553,60 @@ int bootflow_iter_check_system(const struct bootflow_iter *iter) return -ENOTSUPP; } + +/** + * bootflow_cmdline_set() - Set the command line for a bootflow + * + * @value: New command-line string + * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory + */ +int bootflow_cmdline_set(struct bootflow *bflow, const char *value) +{ + char *cmdline = NULL; + + if (value) { + cmdline = strdup(value); + if (!cmdline) + return -ENOMEM; + } + + free(bflow->cmdline); + bflow->cmdline = cmdline; + + return 0; +} + +#ifdef CONFIG_BOOTSTD_FULL +/** + * on_bootargs() - Update the cmdline of a bootflow + */ +static int on_bootargs(const char *name, const char *value, enum env_op op, + int flags) +{ + struct bootstd_priv *std; + struct bootflow *bflow; + int ret; + + ret = bootstd_get_priv(&std); + if (ret) + return 0; + bflow = std->cur_bootflow; + if (!bflow) + return 0; + + switch (op) { + case env_op_create: + case env_op_overwrite: + ret = bootflow_cmdline_set(bflow, value); + if (ret && ret != ENOENT) + return 1; + return 0; + case env_op_delete: + bootflow_cmdline_set(bflow, NULL); + fallthrough; + default: + return 0; + } +} +U_BOOT_ENV_CALLBACK(bootargs, on_bootargs); +#endif diff --git a/cmd/bootflow.c b/cmd/bootflow.c index ddb168c7039..f33db7be5f2 100644 --- a/cmd/bootflow.c +++ b/cmd/bootflow.c @@ -288,6 +288,12 @@ static int do_bootflow_select(struct cmd_tbl *cmdtp, int flag, int argc, return CMD_RET_FAILURE; } std->cur_bootflow = found; + if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { + if (env_set("bootargs", found->cmdline)) { + printf("Cannot set bootargs\n"); + return CMD_RET_FAILURE; + } + } return 0; } diff --git a/include/env_callback.h b/include/env_callback.h index a9a14f2a84a..23bc650c162 100644 --- a/include/env_callback.h +++ b/include/env_callback.h @@ -60,8 +60,10 @@ #define NET6_CALLBACKS #endif -#ifdef CONFIG_BOOTSTD -#define BOOTSTD_CALLBACK "bootmeths:bootmeths," +#ifdef CONFIG_BOOTSTD_FULL +#define BOOTSTD_CALLBACK \ + "bootmeths:bootmeths," \ + "bootargs:bootargs," #else #define BOOTSTD_CALLBACK #endif -- cgit v1.3.1 From d07861cc7aa605a64c83bc7fbe1340bc31b2c2cf Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:38 -0600 Subject: bootstd: Add a function to update a command line The Linux command line consists of a number of words with optional values. At present U-Boot allows this to be changed using the bootargs environment variable. But this is quite painful, since the command line can be very long. Add a function which can adjust a single field in the command line, so that it is easier to make changes before booting. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/bootflow.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/bootflow.h | 40 +++++++++++ test/boot/bootflow.c | 154 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 385 insertions(+) (limited to 'boot') diff --git a/boot/bootflow.c b/boot/bootflow.c index b820678d41a..26f26aee38a 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -610,3 +610,194 @@ static int on_bootargs(const char *name, const char *value, enum env_op op, } U_BOOT_ENV_CALLBACK(bootargs, on_bootargs); #endif + +/** + * copy_in() - Copy a string into a cmdline buffer + * + * @buf: Buffer to copy into + * @end: End of buffer (pointer to char after the end) + * @arg: String to copy from + * @len: Number of chars to copy from @arg (note that this is not usually the + * sane as strlen(arg) since the string may contain following arguments) + * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value + * with no '=' sign + * Returns: Number of chars written to @buf + */ +static int copy_in(char *buf, char *end, const char *arg, int len, + const char *new_val) +{ + char *to = buf; + + /* copy the arg name */ + if (to + len >= end) + return -E2BIG; + memcpy(to, arg, len); + to += len; + + if (new_val == BOOTFLOWCL_EMPTY) { + /* no value */ + } else { + bool need_quote = strchr(new_val, ' '); + len = strlen(new_val); + + /* need space for value, equals sign and maybe two quotes */ + if (to + 1 + (need_quote ? 2 : 0) + len >= end) + return -E2BIG; + *to++ = '='; + if (need_quote) + *to++ = '"'; + memcpy(to, new_val, len); + to += len; + if (need_quote) + *to++ = '"'; + } + + return to - buf; +} + +int cmdline_set_arg(char *buf, int maxlen, const char *cmdline, + const char *set_arg, const char *new_val, int *posp) +{ + bool found_arg = false; + const char *from; + char *to, *end; + int set_arg_len; + char empty = '\0'; + int ret; + + from = cmdline ?: ∅ + + /* check if the value has quotes inside */ + if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"')) + return -EBADF; + + set_arg_len = strlen(set_arg); + for (to = buf, end = buf + maxlen; *from;) { + const char *val, *arg_end, *val_end, *p; + bool in_quote; + + if (to >= end) + return -E2BIG; + while (*from == ' ') + from++; + if (!*from) + break; + + /* find the end of this arg */ + val = NULL; + arg_end = NULL; + val_end = NULL; + in_quote = false; + for (p = from;; p++) { + if (in_quote) { + if (!*p) + return -EINVAL; + if (*p == '"') + in_quote = false; + continue; + } + if (*p == '=') { + arg_end = p; + val = p + 1; + } else if (*p == '"') { + in_quote = true; + } else if (!*p || *p == ' ') { + val_end = p; + if (!arg_end) + arg_end = p; + break; + } + } + /* + * At this point val_end points to the end of the value, or the + * last char after the arg name, if there is no label. + * arg_end is the char after the arg name + * val points to the value, or NULL if there is none + * char after the value. + * + * fred=1234 + * ^ ^^ ^ + * from || | + * / \ \ + * arg_end val val_end + */ + log_debug("from %s arg_end %ld val %ld val_end %ld\n", from, + (long)(arg_end - from), (long)(val - from), + (long)(val_end - from)); + + if (to != buf) { + if (to >= end) + return -E2BIG; + *to++ = ' '; + } + + /* if this is the target arg, update it */ + if (!strncmp(from, set_arg, arg_end - from)) { + if (!buf) { + bool has_quote = val_end[-1] == '"'; + + /* + * exclude any start/end quotes from + * calculations + */ + if (!val) + val = val_end; + *posp = val - cmdline + has_quote; + return val_end - val - 2 * has_quote; + } + found_arg = true; + if (!new_val) { + /* delete this arg */ + from = val_end + (*val_end == ' '); + log_debug("delete from: %s\n", from); + if (to != buf) + to--; /* drop the space we added */ + continue; + } + + ret = copy_in(to, end, from, arg_end - from, new_val); + if (ret < 0) + return ret; + to += ret; + + /* if not the target arg, copy it unchanged */ + } else if (to) { + int len; + + len = val_end - from; + if (to + len >= end) + return -E2BIG; + memcpy(to, from, len); + to += len; + } + from = val_end; + } + + /* If we didn't find the arg, add it */ + if (!found_arg) { + /* trying to delete something that is not there */ + if (!new_val || !buf) + return -ENOENT; + if (to >= end) + return -E2BIG; + + /* add a space to separate it from the previous arg */ + if (to != buf && to[-1] != ' ') + *to++ = ' '; + ret = copy_in(to, end, set_arg, set_arg_len, new_val); + log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf); + if (ret < 0) + return ret; + to += ret; + } + + /* delete any trailing space */ + if (to > buf && to[-1] == ' ') + to--; + + if (to >= end) + return -E2BIG; + *to++ = '\0'; + + return to - buf; +} diff --git a/include/bootflow.h b/include/bootflow.h index f263173c4da..8db875adf61 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -444,4 +444,44 @@ int bootflow_menu_apply_theme(struct expo *exp, ofnode node); int bootflow_menu_run(struct bootstd_priv *std, bool text_mode, struct bootflow **bflowp); +#define BOOTFLOWCL_EMPTY ((void *)1) + +/** + * cmdline_set_arg() - Update or read an argument in a cmdline string + * + * Handles updating a single arg in a cmdline string, returning it in a supplied + * buffer; also reading an arg from a cmdline string + * + * When updating, consecutive spaces are squashed as are spaces at the start and + * end. + * + * @buf: Working buffer to use (initial contents are ignored). Use NULL when + * reading + * @maxlen: Length of working buffer. Use 0 when reading + * @cmdline: Command line to update, in the form: + * + * fred mary= jane=123 john="has spaces" + * + * @set_arg: Argument to set or read (may or may not exist) + * @new_val: Value for the new argument. May not include quotes (") but may + * include embedded spaces, in which case it will be quoted when added to the + * command line. Use NULL to delete the argument from @cmdline, BOOTFLOWCL_EMPTY + * to set it to an empty value (no '=' sign after arg), "" to add an '=' sign + * but with an empty value. Use NULL when reading. + * @posp: Ignored when setting an argument; when getting an argument, returns + * the start position of its value in @cmdline, after the first quote, if any + * + * Return: + * For updating: + * length of new buffer (including \0 terminator) on success, -ENOENT if + * @new_val is NULL and @set_arg does not exist in @from, -EINVAL if a + * quoted arg-value in @from is not terminated with a quote, -EBADF if + * @new_val has spaces but does not start and end with quotes (or it has + * quotes in the middle of the string), -E2BIG if @maxlen is too small + * For reading: + * length of arg value (excluding quotes), -ENOENT if not found + */ +int cmdline_set_arg(char *buf, int maxlen, const char *cmdline, + const char *set_arg, const char *new_val, int *posp); + #endif diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index f7ebf9d7f27..ead71172e12 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -683,3 +683,157 @@ static int bootflow_menu_theme(struct unit_test_state *uts) return 0; } BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT); + +/** + * check_arg() - Check both the normal case and the buffer-overflow case + * + * @uts: Unit-test state + * @expect_ret: Expected return value (i.e. buffer length) + * @expect_str: String expected to be returned + * @buf: Buffer to use + * @from: Original cmdline to update + * @arg: Argument to update (e.g. "console") + * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present, + * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add + * it without any value ("initrd") + */ +static int check_arg(struct unit_test_state *uts, int expect_ret, + const char *expect_str, char *buf, const char *from, + const char *arg, const char *val) +{ + /* check for writing outside the reported bounds */ + buf[expect_ret] = '['; + ut_asserteq(expect_ret, + cmdline_set_arg(buf, expect_ret, from, arg, val, NULL)); + ut_asserteq_str(expect_str, buf); + ut_asserteq('[', buf[expect_ret]); + + /* do the test again but with one less byte in the buffer */ + ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg, + val, NULL)); + + return 0; +} + +/* Test of bootflow_cmdline_set_arg() */ +static int test_bootflow_cmdline_set(struct unit_test_state *uts) +{ + char buf[50]; + const int size = sizeof(buf); + + /* + * note that buffer-overflow tests are immediately each test case, just + * top keep the code together + */ + + /* add an arg that doesn't already exist, starting from empty */ + ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL, + NULL)); + + ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY)); + ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", "")); + ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred")); + + /* add an arg that doesn't already exist, starting from non-empty */ + ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me", + BOOTFLOWCL_EMPTY)); + ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me", + "")); + ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me", + "fred")); + + /* update an arg at the start */ + ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL)); + ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg", + BOOTFLOWCL_EMPTY)); + ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", "")); + ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1")); + ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg", + "1234")); + + /* update an arg at the end */ + ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg", + NULL)); + ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg", + BOOTFLOWCL_EMPTY)); + ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg", + "")); + ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg", + "1")); + ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123", + "arg", "1234")); + + /* update an arg in the middle */ + ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf, + "mary=abc arg=123 john=2", "arg", NULL)); + ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf, + "mary=abc arg=123 john=2", "arg", + BOOTFLOWCL_EMPTY)); + ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf, + "mary=abc arg=123 john=2", "arg", "")); + ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf, + "mary=abc arg=123 john=2", "arg", "1")); + ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf, + "mary=abc arg=123 john=2", "arg", "1234")); + + /* handle existing args with quotes */ + ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf, + "mary=\"abc\" arg=123 john", "arg", NULL)); + + /* handle existing args with quoted spaces */ + ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf, + "mary=\"abc def\" arg=123 john", "arg", NULL)); + + ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4", + buf, "mary=\"abc def\" arg=123 john", "def", + "4")); + + /* quote at the start */ + ut_asserteq(-EBADF, cmdline_set_arg(buf, size, + "mary=\"abc def\" arg=\"123 456\"", + "arg", "\"4 5 6", NULL)); + + /* quote at the end */ + ut_asserteq(-EBADF, cmdline_set_arg(buf, size, + "mary=\"abc def\" arg=\"123 456\"", + "arg", "4 5 6\"", NULL)); + + /* quote in the middle */ + ut_asserteq(-EBADF, cmdline_set_arg(buf, size, + "mary=\"abc def\" arg=\"123 456\"", + "arg", "\"4 \"5 6\"", NULL)); + + /* handle updating a quoted arg */ + ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf, + "mary=\"abc def\" arg=\"123 456\"", "arg", + "4 5 6")); + + /* changing a quoted arg to a non-quoted arg */ + ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf, + "mary=\"abc def\" arg=\"123 456\"", "arg", + "789")); + + /* changing a non-quoted arg to a quoted arg */ + ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf, + "mary=\"abc def\" arg=123", "arg", "456 789")); + + /* handling of spaces */ + ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123")); + ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123")); + ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg", + "123")); + ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ", + "arg", "123")); + ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf, + " john arg=123 mary ", "arg", "123")); + + /* unchanged arg */ + ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY)); + + /* arg which starts with the same name */ + ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf, + "mary=abc johnathon=2 john=1", "john", "3")); + + return 0; +} +BOOTSTD_TEST(test_bootflow_cmdline_set, 0); -- cgit v1.3.1 From 82c0938f1d3befdd7dd1a1bda3b0a02b219abb5d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:39 -0600 Subject: bootstd: Add support for updating elements of the cmdline Add a bootflow command to update the command line more easily. This allows changing a particular parameter rather than editing a very long strings. It is also easier to handle with scripting. The new 'bootflow cmdline' command allows getting and setting single parameters. Fix up the example output while we are here, since there are a few new items. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/bootflow.c | 53 ++++++++++++++++++++++ cmd/bootflow.c | 70 ++++++++++++++++++++++++++++- doc/usage/cmd/bootflow.rst | 22 ++++++++- include/bootflow.h | 42 ++++++++++++++++++ test/boot/bootflow.c | 108 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 293 insertions(+), 2 deletions(-) (limited to 'boot') diff --git a/boot/bootflow.c b/boot/bootflow.c index 26f26aee38a..8c649e8e66c 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -801,3 +801,56 @@ int cmdline_set_arg(char *buf, int maxlen, const char *cmdline, return to - buf; } + +int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg, + const char *new_val, bool set_env) +{ + char buf[2048]; + char *cmd = NULL; + int ret; + + ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg, + new_val, NULL); + if (ret < 0) + return ret; + + ret = bootflow_cmdline_set(bflow, buf); + if (*buf) { + cmd = strdup(buf); + if (!cmd) + return -ENOMEM; + } + free(bflow->cmdline); + bflow->cmdline = cmd; + + if (set_env) { + ret = env_set("bootargs", bflow->cmdline); + if (ret) + return ret; + } + + return 0; +} + +int cmdline_get_arg(const char *cmdline, const char *arg, int *posp) +{ + int ret; + + ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp); + + return ret; +} + +int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg, + const char **val) +{ + int ret; + int pos; + + ret = cmdline_get_arg(bflow->cmdline, arg, &pos); + if (ret < 0) + return ret; + *val = bflow->cmdline + pos; + + return ret; +} diff --git a/cmd/bootflow.c b/cmd/bootflow.c index bf30087c7c4..ab00e4a19e1 100644 --- a/cmd/bootflow.c +++ b/cmd/bootflow.c @@ -431,6 +431,72 @@ static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc, return 0; } + +static int do_bootflow_cmdline(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct bootstd_priv *std; + struct bootflow *bflow; + const char *op, *arg, *val = NULL; + int ret; + + if (argc < 3) + return CMD_RET_USAGE; + + ret = bootstd_get_priv(&std); + if (ret) + return CMD_RET_FAILURE; + + bflow = std->cur_bootflow; + if (!bflow) { + printf("No bootflow selected\n"); + return CMD_RET_FAILURE; + } + + op = argv[1]; + arg = argv[2]; + if (*op == 's') { + if (argc < 4) + return CMD_RET_USAGE; + val = argv[3]; + } + + switch (*op) { + case 'c': /* clear */ + val = ""; + fallthrough; + case 's': /* set */ + case 'd': /* delete */ + ret = bootflow_cmdline_set_arg(bflow, arg, val, true); + break; + case 'g': /* get */ + ret = bootflow_cmdline_get_arg(bflow, arg, &val); + if (ret >= 0) + printf("%.*s\n", ret, val); + break; + } + switch (ret) { + case -E2BIG: + printf("Argument too long\n"); + break; + case -ENOENT: + printf("Argument not found\n"); + break; + case -EINVAL: + printf("Mismatched quotes\n"); + break; + case -EBADF: + printf("Value must be quoted\n"); + break; + default: + if (ret < 0) + printf("Unknown error: %dE\n", ret); + } + if (ret < 0) + return CMD_RET_FAILURE; + + return 0; +} #endif /* CONFIG_CMD_BOOTFLOW_FULL */ #ifdef CONFIG_SYS_LONGHELP @@ -441,7 +507,8 @@ static char bootflow_help_text[] = "bootflow select [|] - select a bootflow\n" "bootflow info [-d] - show info on current bootflow (-d dump bootflow)\n" "bootflow boot - boot current bootflow (or first available if none selected)\n" - "bootflow menu [-t] - show a menu of available bootflows"; + "bootflow menu [-t] - show a menu of available bootflows\n" + "bootflow cmdline [set|get|clear|delete] [] - update cmdline"; #else "scan - boot first available bootflow\n"; #endif @@ -455,5 +522,6 @@ U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text, U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info), U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot), U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu), + U_BOOT_SUBCMD_MKENT(cmdline, 4, 1, do_bootflow_cmdline), #endif ); diff --git a/doc/usage/cmd/bootflow.rst b/doc/usage/cmd/bootflow.rst index 907d44ad9f9..07af789e670 100644 --- a/doc/usage/cmd/bootflow.rst +++ b/doc/usage/cmd/bootflow.rst @@ -13,7 +13,7 @@ Synopis bootflow select [] bootflow info [-d] bootflow boot - + bootflow cmdline [set|get|clear|delete] [] Description ----------- @@ -198,6 +198,26 @@ bootflow boot This boots the current bootflow. +bootflow cmdline +~~~~~~~~~~~~~~~~ + +Some bootmeths can obtain the OS command line since it is stored with the OS. +In that case, you can use `bootflow cmdline` to adjust this. The command line +is assumed to be in the format used by Linux, i.e. a space-separated set of +parameters with optional values, e.g. "noinitrd console=/dev/tty0". + +To change or add a parameter, use:: + + bootflow cmdline set + +To clear a parameter value to empty you can use "" for the value, or use:: + + bootflow cmdline clear + +To delete a parameter entirely, use:: + + bootflow cmdline delete + Example ------- diff --git a/include/bootflow.h b/include/bootflow.h index 8db875adf61..7a8595f3dfc 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -484,4 +484,46 @@ int bootflow_menu_run(struct bootstd_priv *std, bool text_mode, int cmdline_set_arg(char *buf, int maxlen, const char *cmdline, const char *set_arg, const char *new_val, int *posp); +/** + * bootflow_cmdline_set_arg() - Set a single argument for a bootflow + * + * Update the allocated cmdline and set the bootargs variable + * + * @bflow: Bootflow to update + * @arg: Argument to update (e.g. "console") + * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present, + * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add + * it without any value ("initrd") + * @set_env: true to set the "bootargs" environment variable too + * + * Return: 0 if OK, -ENOMEM if out of memory + */ +int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *arg, + const char *val, bool set_env); + +/** + * cmdline_get_arg() - Read an argument from a cmdline + * + * @cmdline: Command line to read, in the form: + * + * fred mary= jane=123 john="has spaces" + * @arg: Argument to read (may or may not exist) + * @posp: Returns position of argument (after any leading quote) if present + * Return: Length of argument value excluding quotes if found, -ENOENT if not + * found + */ +int cmdline_get_arg(const char *cmdline, const char *arg, int *posp); + +/** + * bootflow_cmdline_get_arg() - Read an argument from a cmdline + * + * @bootflow: Bootflow to read from + * @arg: Argument to read (may or may not exist) + * @valp: Returns a pointer to the argument (after any leading quote) if present + * Return: Length of argument value excluding quotes if found, -ENOENT if not + * found + */ +int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg, + const char **val); + #endif diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index ead71172e12..8a4e090e9bc 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -837,3 +837,111 @@ static int test_bootflow_cmdline_set(struct unit_test_state *uts) return 0; } BOOTSTD_TEST(test_bootflow_cmdline_set, 0); + +/* Test of bootflow_cmdline_set_arg() */ +static int bootflow_set_arg(struct unit_test_state *uts) +{ + struct bootflow s_bflow, *bflow = &s_bflow; + ulong mem_start; + + ut_assertok(env_set("bootargs", NULL)); + + mem_start = ut_check_delta(0); + + /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */ + bflow->cmdline = NULL; + ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false)); + ut_asserteq_str(bflow->cmdline, "fred=123"); + + ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false)); + ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\""); + + ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false)); + ut_asserteq_str(bflow->cmdline, "fred=123"); + ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false)); + ut_asserteq_ptr(bflow->cmdline, NULL); + + ut_asserteq(0, ut_check_delta(mem_start)); + + ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true)); + ut_asserteq_str("mary=here", env_get("bootargs")); + ut_assertok(env_set("bootargs", NULL)); + + return 0; +} +BOOTSTD_TEST(bootflow_set_arg, 0); + +/* Test of bootflow_cmdline_get_arg() */ +static int bootflow_cmdline_get(struct unit_test_state *uts) +{ + int pos; + + /* empty string */ + ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos)); + + /* arg with empty value */ + ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos)); + ut_asserteq(5, pos); + + /* arg with a value */ + ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos)); + ut_asserteq(5, pos); + + /* arg with a value */ + ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos)); + ut_asserteq(12, pos); + + /* arg with a value, after quoted arg */ + ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos)); + ut_asserteq(16, pos); + + /* arg in the middle */ + ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred", + &pos)); + ut_asserteq(15, pos); + + /* quoted arg */ + ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23", + "fred", &pos)); + ut_asserteq(17, pos); + + /* args starting with the same prefix */ + ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john", + &pos)); + ut_asserteq(26, pos); + + return 0; +} +BOOTSTD_TEST(bootflow_cmdline_get, 0); + +static int bootflow_cmdline(struct unit_test_state *uts) +{ + ut_assertok(run_command("bootflow scan mmc", 0)); + ut_assertok(run_command("bootflow sel 0", 0)); + console_record_reset_enable(); + + ut_asserteq(1, run_command("bootflow cmdline get fred", 0)); + ut_assert_nextline("Argument not found"); + ut_assert_console_end(); + + ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0)); + ut_asserteq(0, run_command("bootflow cmdline get fred", 0)); + ut_assert_nextline("123"); + + ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0)); + ut_asserteq(0, run_command("bootflow cmdline get mary", 0)); + ut_assert_nextline("abc"); + + ut_asserteq(0, run_command("bootflow cmdline delete fred", 0)); + ut_asserteq(1, run_command("bootflow cmdline get fred", 0)); + ut_assert_nextline("Argument not found"); + + ut_asserteq(0, run_command("bootflow cmdline clear mary", 0)); + ut_asserteq(0, run_command("bootflow cmdline get mary", 0)); + ut_assert_nextline_empty(); + + ut_assert_console_end(); + + return 0; +} +BOOTSTD_TEST(bootflow_cmdline, 0); -- cgit v1.3.1 From 33ebcb468109c40ef0dce262be00a4c161265b90 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:42 -0600 Subject: bootstd: Support automatically setting Linux parameters Some Linux parameters can be set automatically by U-Boot, if it knows the device being used. For example, since U-Boot knows the serial console being used, it can add parameters for earlycon and console. Add support for this. Note that this is an experimental feature and we will see how useful it turns out to be. It is very handy for ChromeOS, since otherwise it is very difficult to manually determine the UART address or port number, particularly in a script. Provide an example of how this is used with ChromeOS. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/bootflow.c | 33 ++++++++++++++++++++ cmd/bootflow.c | 5 +++- doc/usage/cmd/bootflow.rst | 75 +++++++++++++++++++++++++++++++++++++++++++++- include/bootflow.h | 12 ++++++++ 4 files changed, 123 insertions(+), 2 deletions(-) (limited to 'boot') diff --git a/boot/bootflow.c b/boot/bootflow.c index 8c649e8e66c..81b5829d5b3 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -854,3 +855,35 @@ int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg, return ret; } + +int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg) +{ + struct serial_device_info info; + char buf[50]; + int ret; + + ret = serial_getinfo(gd->cur_serial_dev, &info); + if (ret) + return ret; + + *buf = '\0'; + if (!strcmp("earlycon", arg)) { + snprintf(buf, sizeof(buf), + "uart8250,mmio32,%#lx,%dn8", info.addr, + info.baudrate); + } else if (!strcmp("console", arg)) { + snprintf(buf, sizeof(buf), + "ttyS0,%dn8", info.baudrate); + } + + if (!*buf) { + printf("Unknown param '%s\n", arg); + return -ENOENT; + } + + ret = bootflow_cmdline_set_arg(bflow, arg, buf, true); + if (ret) + return ret; + + return 0; +} diff --git a/cmd/bootflow.c b/cmd/bootflow.c index ab00e4a19e1..c0aa4f84fe8 100644 --- a/cmd/bootflow.c +++ b/cmd/bootflow.c @@ -474,6 +474,9 @@ static int do_bootflow_cmdline(struct cmd_tbl *cmdtp, int flag, int argc, if (ret >= 0) printf("%.*s\n", ret, val); break; + case 'a': /* auto */ + ret = bootflow_cmdline_auto(bflow, arg); + break; } switch (ret) { case -E2BIG: @@ -508,7 +511,7 @@ static char bootflow_help_text[] = "bootflow info [-d] - show info on current bootflow (-d dump bootflow)\n" "bootflow boot - boot current bootflow (or first available if none selected)\n" "bootflow menu [-t] - show a menu of available bootflows\n" - "bootflow cmdline [set|get|clear|delete] [] - update cmdline"; + "bootflow cmdline [set|get|clear|delete|auto] [] - update cmdline"; #else "scan - boot first available bootflow\n"; #endif diff --git a/doc/usage/cmd/bootflow.rst b/doc/usage/cmd/bootflow.rst index 07af789e670..a8af1f8f603 100644 --- a/doc/usage/cmd/bootflow.rst +++ b/doc/usage/cmd/bootflow.rst @@ -13,7 +13,7 @@ Synopis bootflow select [] bootflow info [-d] bootflow boot - bootflow cmdline [set|get|clear|delete] [] + bootflow cmdline [set|get|clear|delete|auto] [] Description ----------- @@ -218,6 +218,16 @@ To delete a parameter entirely, use:: bootflow cmdline delete +Automatic parameters are available in a very few cases. You can use these to +add parmeters where the value is known by U-Boot. For example:: + + bootflow cmdline auto earlycon + bootflow cmdline auto console + +can be used to set the early console (or console) to a suitable value so that +output appears on the serial port. This is only supported by the 16550 serial +driver so far. + Example ------- @@ -450,6 +460,69 @@ Here is am example using the -e flag to see all errors:: (21 bootflows, 2 valid) U-Boot> +Here is an example of booting ChromeOS, adjusting the console beforehand. Note that +the cmdline is word-wrapped here and some parts of the command line are elided:: + + => bootfl list + Showing all bootflows + Seq Method State Uclass Part Name Filename + --- ----------- ------ -------- ---- ------------------------ ---------------- + 0 cros ready nvme 0 5.10.153-20434-g98da1eb2c + 1 efi ready nvme c nvme#0.blk#1.bootdev.part efi/boot/bootia32.efi + 2 efi ready usb_mass_ 2 usb_mass_storage.lun0.boo efi/boot/bootia32.efi + --- ----------- ------ -------- ---- ------------------------ ---------------- + (3 bootflows, 3 valid) + => bootfl sel 0 + => bootfl inf + Name: 5.10.153-20434-g98da1eb2cf9d (chrome-bot@chromeos-release-builder-us-central1-b-x32-12-xijx) #1 SMP PREEMPT Tue Jan 24 19:38:23 PST 2023 + Device: nvme#0.blk#1.bootdev + Block dev: nvme#0.blk#1 + Method: cros + State: ready + Partition: 0 + Subdir: (none) + Filename: + Buffer: 737a1400 + Size: c47000 (12873728 bytes) + OS: ChromeOS + Cmdline: console= loglevel=7 init=/sbin/init cros_secure drm.trace=0x106 + root=/dev/dm-0 rootwait ro dm_verity.error_behavior=3 + dm_verity.max_bios=-1 dm_verity.dev_wait=1 + dm="1 vroot none ro 1,0 6348800 + verity payload=PARTUUID=799c935b-ae62-d143-8493-816fa936eef7/PARTNROFF=1 + hashtree=PARTUUID=799c935b-ae62-d143-8493-816fa936eef7/PARTNROFF=1 + hashstart=6348800 alg=sha256 + root_hexdigest=78cc462cd45aecbcd49ca476587b4dee59aa1b00ba5ece58e2c29ec9acd914ab + salt=8dec4dc80a75dd834a9b3175c674405e15b16a253fdfe05c79394ae5fd76f66a" + noinitrd vt.global_cursor_default=0 + kern_guid=799c935b-ae62-d143-8493-816fa936eef7 add_efi_memmap boot=local + noresume noswap i915.modeset=1 ramoops.ecc=1 tpm_tis.force=0 + intel_pmc_core.warn_on_s0ix_failures=1 i915.enable_guc=3 i915.enable_dc=4 + xdomain=0 swiotlb=65536 intel_iommu=on i915.enable_psr=1 + usb-storage.quirks=13fe:6500:u + X86 setup: 742e3400 + Logo: (none) + FDT: + Error: 0 + => bootflow cmdline auto earlycon + => bootflow cmd auto console + => print bootargs + bootargs=console=ttyS0,115200n8 loglevel=7 ... + usb-storage.quirks=13fe:6500:u earlycon=uart8250,mmio32,0xfe03e000,115200n8 + => bootflow cmd del console + => print bootargs + bootargs=loglevel=7 ... earlycon=uart8250,mmio32,0xfe03e000,115200n8 + => bootfl boot + ** Booting bootflow '5.10.153-20434-g98da1eb2cf9d (chrome-bot@chromeos-release-builder-us-central1-b-x32-12-xijx) #1 SMP PREEMPT Tue Jan 24 19:38:23 PST 2023' with cros + Kernel command line: "loglevel=7 ... earlycon=uart8250,mmio32,0xfe03e000,115200n8" + + Starting kernel ... + + [ 0.000000] Linux version 5.10.153-20434-g98da1eb2cf9d (chrome-bot@chromeos-release-builder-us-central1-b-x32-12-xijx) (Chromium OS 15.0_pre465103_p20220825-r4 clang version 15.0.0 (/var/tmp/portage/sys-devel/llvm-15.0_pre465103_p20220825-r4/work/llvm-15.0_pre465103_p20220825/clang db1978b67431ca3462ad8935bf662c15750b8252), LLD 15.0.0) #1 SMP PREEMPT Tue Jan 24 19:38:23 PST 2023 + [ 0.000000] Command line: loglevel=7 ... usb-storage.quirks=13fe:6500:u earlycon=uart8250,mmio32,0xfe03e000,115200n8 + [ 0.000000] x86/split lock detection: warning about user-space split_locks + + Return value ------------ diff --git a/include/bootflow.h b/include/bootflow.h index 7a8595f3dfc..4152577afb7 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -526,4 +526,16 @@ int cmdline_get_arg(const char *cmdline, const char *arg, int *posp); int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg, const char **val); +/** + * bootflow_cmdline_auto() - Automatically set a value for a known argument + * + * This handles a small number of known arguments, for Linux in particular. It + * adds suitable kernel parameters automatically, e.g. to enable the console. + * + * @bflow: Bootflow to update + * @arg: Name of argument to set (e.g. "earlycon" or "console") + * Return: 0 if OK -ve on error + */ +int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg); + #endif -- cgit v1.3.1 From c88d67d0211292dd5f259816b24c65da62ce3e30 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 12 Jul 2023 09:04:45 -0600 Subject: bootstd: Add a simple bootmeth for ChromiumOS It is possible to boot x86-based ChromeOS machines by parsing a table and locating the kernel and command line. Add a bootmeth for this. Signed-off-by: Simon Glass Reviewed-by: Bin Meng --- boot/Kconfig | 11 +++ boot/Makefile | 1 + boot/bootmeth_cros.c | 212 +++++++++++++++++++++++++++++++++++++++++++ configs/tools-only_defconfig | 1 + 4 files changed, 225 insertions(+) create mode 100644 boot/bootmeth_cros.c (limited to 'boot') diff --git a/boot/Kconfig b/boot/Kconfig index c8b8f36d835..b424265df8d 100644 --- a/boot/Kconfig +++ b/boot/Kconfig @@ -463,6 +463,17 @@ config BOOTMETH_GLOBAL EFI bootmgr, since they take full control over which bootdevs are selected to boot. +config BOOTMETH_CROS + bool "Bootdev support for Chromium OS" + depends on X86 || SANDBOX + default y + help + Enables support for booting Chromium OS using bootdevs. This uses the + kernel A slot and obtains the kernel command line from the parameters + provided there. + + Note that only x86 devices are supported at present. + config BOOTMETH_EXTLINUX bool "Bootdev support for extlinux boot" select PXE_UTILS diff --git a/boot/Makefile b/boot/Makefile index f828f870a37..10f01572237 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -27,6 +27,7 @@ obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) += bootstd-uclass.o obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_EXTLINUX) += bootmeth_extlinux.o obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_EXTLINUX_PXE) += bootmeth_pxe.o obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_EFILOADER) += bootmeth_efi.o +obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_CROS) += bootmeth_cros.o obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_SANDBOX) += bootmeth_sandbox.o obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_SCRIPT) += bootmeth_script.o ifdef CONFIG_$(SPL_TPL_)BOOTSTD_FULL diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c new file mode 100644 index 00000000000..aa19ae097f5 --- /dev/null +++ b/boot/bootmeth_cros.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Bootmethod for ChromiumOS + * + * Copyright 2023 Google LLC + * Written by Simon Glass + */ + +#define LOG_CATEGORY UCLASS_BOOTSTD + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_X86 +#include +#endif +#include + +enum { + /* Offsets in the kernel-partition header */ + KERN_START = 0x4f0, + KERN_SIZE = 0x518, + + SETUP_OFFSET = 0x1000, /* bytes before base */ + CMDLINE_OFFSET = 0x2000, /* bytes before base */ + OFFSET_BASE = 0x100000, /* assumed kernel load-address */ +}; + +static int cros_check(struct udevice *dev, struct bootflow_iter *iter) +{ + /* This only works on block and network devices */ + if (bootflow_iter_check_blk(iter)) + return log_msg_ret("blk", -ENOTSUPP); + + return 0; +} + +static int copy_cmdline(const char *from, const char *uuid, char **bufp) +{ + const int maxlen = 2048; + char buf[maxlen]; + char *cmd, *to, *end; + int len; + + /* Allow space for cmdline + UUID */ + len = strnlen(from, sizeof(buf)); + if (len >= maxlen) + return -E2BIG; + + log_debug("uuid %d %s\n", uuid ? (int)strlen(uuid) : 0, uuid); + for (to = buf, end = buf + maxlen - UUID_STR_LEN - 1; *from; from++) { + if (to >= end) + return -E2BIG; + if (from[0] == '%' && from[1] == 'U' && uuid && + strlen(uuid) == UUID_STR_LEN) { + strcpy(to, uuid); + to += UUID_STR_LEN; + from++; + } else { + *to++ = *from; + } + } + *to = '\0'; + len = to - buf; + cmd = strdup(buf); + if (!cmd) + return -ENOMEM; + free(*bufp); + *bufp = cmd; + + return 0; +} + +static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) +{ + struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); + ulong base, start, size, setup, cmdline, num_blks, kern_base; + struct disk_partition info; + const char *uuid = NULL; + void *buf, *hdr; + int ret; + + log_debug("starting, part=%d\n", bflow->part); + + /* We consider the whole disk, not any one partition */ + if (bflow->part) + return log_msg_ret("max", -ENOENT); + + /* Check partition 2 */ + ret = part_get_info(desc, 2, &info); + if (ret) + return log_msg_ret("part", ret); + + /* Make a buffer for the header information */ + num_blks = SZ_4K >> desc->log2blksz; + log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n", + bflow->blk->name, (ulong)info.start, num_blks); + hdr = memalign(SZ_1K, SZ_4K); + if (!hdr) + return log_msg_ret("hdr", -ENOMEM); + ret = blk_read(bflow->blk, info.start, num_blks, hdr); + if (ret != num_blks) + return log_msg_ret("inf", ret); + + if (memcmp("CHROMEOS", hdr, 8)) + return -ENOENT; + + log_info("Header at %lx\n", (ulong)map_to_sysmem(hdr)); + start = *(u32 *)(hdr + KERN_START); + size = ALIGN(*(u32 *)(hdr + KERN_SIZE), desc->blksz); + log_debug("Reading start %lx size %lx\n", start, size); + bflow->size = size; + + buf = memalign(SZ_1K, size); + if (!buf) + return log_msg_ret("buf", -ENOMEM); + num_blks = size >> desc->log2blksz; + log_debug("Reading data, blk=%s, start=%lx, blocks=%lx\n", + bflow->blk->name, (ulong)info.start, num_blks); + ret = blk_read(bflow->blk, (ulong)info.start + 0x80, num_blks, buf); + if (ret != num_blks) + return log_msg_ret("inf", ret); + base = map_to_sysmem(buf); + + setup = base + start - OFFSET_BASE - SETUP_OFFSET; + cmdline = base + start - OFFSET_BASE - CMDLINE_OFFSET; + kern_base = base + start - OFFSET_BASE + SZ_16K; + log_debug("base %lx setup %lx, cmdline %lx, kern_base %lx\n", base, + setup, cmdline, kern_base); + +#ifdef CONFIG_X86 + const char *version; + + version = zimage_get_kernel_version(map_sysmem(setup, 0), + map_sysmem(kern_base, 0)); + log_debug("version %s\n", version); + if (version) + bflow->name = strdup(version); +#endif + if (!bflow->name) + bflow->name = strdup("ChromeOS"); + if (!bflow->name) + return log_msg_ret("nam", -ENOMEM); + bflow->os_name = strdup("ChromeOS"); + if (!bflow->os_name) + return log_msg_ret("os", -ENOMEM); + +#if CONFIG_IS_ENABLED(PARTITION_UUIDS) + uuid = info.uuid; +#endif + ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline); + if (ret) + return log_msg_ret("cmd", ret); + + bflow->state = BOOTFLOWST_READY; + bflow->buf = buf; + bflow->x86_setup = map_sysmem(setup, 0); + + return 0; +} + +static int cros_read_file(struct udevice *dev, struct bootflow *bflow, + const char *file_path, ulong addr, ulong *sizep) +{ + return -ENOSYS; +} + +static int cros_boot(struct udevice *dev, struct bootflow *bflow) +{ +#ifdef CONFIG_X86 + zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0, + map_to_sysmem(bflow->x86_setup), + bflow->cmdline); +#endif + + return log_msg_ret("go", -EFAULT); +} + +static int cros_bootmeth_bind(struct udevice *dev) +{ + struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); + + plat->desc = "ChromiumOS boot"; + + return 0; +} + +static struct bootmeth_ops cros_bootmeth_ops = { + .check = cros_check, + .read_bootflow = cros_read_bootflow, + .read_file = cros_read_file, + .boot = cros_boot, +}; + +static const struct udevice_id cros_bootmeth_ids[] = { + { .compatible = "u-boot,cros" }, + { } +}; + +U_BOOT_DRIVER(bootmeth_cros) = { + .name = "bootmeth_cros", + .id = UCLASS_BOOTMETH, + .of_match = cros_bootmeth_ids, + .ops = &cros_bootmeth_ops, + .bind = cros_bootmeth_bind, +}; diff --git a/configs/tools-only_defconfig b/configs/tools-only_defconfig index 2bc3bd9e912..3f588ea69be 100644 --- a/configs/tools-only_defconfig +++ b/configs/tools-only_defconfig @@ -10,6 +10,7 @@ CONFIG_FIT=y CONFIG_TIMESTAMP=y CONFIG_FIT_SIGNATURE=y # CONFIG_BOOTSTD_FULL is not set +# CONFIG_BOOTMETH_CROS is not set # CONFIG_BOOTMETH_VBE is not set CONFIG_USE_BOOTCOMMAND=y CONFIG_BOOTCOMMAND="run distro_bootcmd" -- cgit v1.3.1