summaryrefslogtreecommitdiff
path: root/boot
diff options
context:
space:
mode:
authorTom Rini <[email protected]>2023-07-17 10:38:28 -0400
committerTom Rini <[email protected]>2023-07-17 10:38:28 -0400
commit13aa090b87a0fbdfe690011669b9fdb96bb1ccc7 (patch)
tree69af16bc8ecc4b6e8106a750e31e51d7ec078828 /boot
parentaa817dfcaf158dda71358d02181bf52c30dbe4c6 (diff)
parentb8956425d525c3c25fd218f252f89a5e44df6a9f (diff)
Merge https://source.denx.de/u-boot/custodians/u-boot-x86
- bootstd: Add a bootmeth for ChromiumOS on x86 - x86: Use qemu-x86_64 to boot EFI installers
Diffstat (limited to 'boot')
-rw-r--r--boot/Kconfig11
-rw-r--r--boot/Makefile1
-rw-r--r--boot/bootflow.c339
-rw-r--r--boot/bootmeth-uclass.c2
-rw-r--r--boot/bootmeth_cros.c212
-rw-r--r--boot/bootmeth_qfw.c2
6 files changed, 563 insertions, 4 deletions
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/bootflow.c b/boot/bootflow.c
index 8f2cb876bb4..81b5829d5b3 100644
--- a/boot/bootflow.c
+++ b/boot/bootflow.c
@@ -12,7 +12,9 @@
#include <bootmeth.h>
#include <bootstd.h>
#include <dm.h>
+#include <env_internal.h>
#include <malloc.h>
+#include <serial.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>
@@ -315,14 +317,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
@@ -552,3 +554,336 @@ 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
+
+/**
+ * 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 ?: &empty;
+
+ /* 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;
+}
+
+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;
+}
+
+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/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)
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 <[email protected]>
+ */
+
+#define LOG_CATEGORY UCLASS_BOOTSTD
+
+#include <common.h>
+#include <blk.h>
+#include <bootdev.h>
+#include <bootflow.h>
+#include <bootmeth.h>
+#include <dm.h>
+#include <malloc.h>
+#include <mapmem.h>
+#include <part.h>
+#ifdef CONFIG_X86
+#include <asm/zimage.h>
+#endif
+#include <linux/sizes.h>
+
+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/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;
}