[PATCH v2 22/41] bootstd: Add the bootmeth uclass and helpers
Simon Glass
sjg at chromium.org
Sun Oct 24 01:26:16 CEST 2021
A bootmeth is a method of locating an operating system. For now, just
add the uclass itself. Drivers for particular bootmeths are added later.
If no bootmeths devices are included in the devicetree, create them
automatically. This avoids the need for boilerplate in the devicetree
files.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
(no changes since v1)
MAINTAINERS | 2 +
boot/Makefile | 1 +
boot/bootmeth-uclass.c | 59 ++++++++++++++++
boot/bootstd-uclass.c | 29 +++++++-
include/bootmeth.h | 156 +++++++++++++++++++++++++++++++++++++++++
include/dm/uclass-id.h | 1 +
6 files changed, 246 insertions(+), 2 deletions(-)
create mode 100644 boot/bootmeth-uclass.c
create mode 100644 include/bootmeth.h
diff --git a/MAINTAINERS b/MAINTAINERS
index d800a3b7acb..be11e3c2f2a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -636,10 +636,12 @@ F: tools/binman/
BOOTDEVICE
M: Simon Glass <sjg at chromium.org>
S: Maintained
+F: boot/bootmeth*.c
F: boot/bootdev*.c
F: boot/bootstd.c
F: include/bootdev*.h
F: include/bootflow.h
+F: include/bootmeth.h
F: include/bootstd.h
BTRFS
diff --git a/boot/Makefile b/boot/Makefile
index 35abfd37654..54ded9541e0 100644
--- a/boot/Makefile
+++ b/boot/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_ANDROID_AB) += android_ab.o
obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o image-android-dt.o
obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) += bootdev-uclass.o
+obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) += bootmeth-uclass.o
obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) += bootstd-uclass.o
obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += image-fdt.o
diff --git a/boot/bootmeth-uclass.c b/boot/bootmeth-uclass.c
new file mode 100644
index 00000000000..5df5fb55c31
--- /dev/null
+++ b/boot/bootmeth-uclass.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <bootmeth.h>
+#include <dm.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
+{
+ const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
+
+ if (!ops->check)
+ return 0;
+
+ return ops->check(dev, iter);
+}
+
+int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
+{
+ const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
+
+ if (!ops->read_bootflow)
+ return -ENOSYS;
+
+ return ops->read_bootflow(dev, bflow);
+}
+
+int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
+{
+ const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
+
+ if (!ops->boot)
+ return -ENOSYS;
+
+ return ops->boot(dev, bflow);
+}
+
+int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
+ const char *file_path, ulong addr, ulong *sizep)
+{
+ const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
+
+ if (!ops->read_file)
+ return -ENOSYS;
+
+ return ops->read_file(dev, bflow, file_path, addr, sizep);
+}
+
+UCLASS_DRIVER(bootmeth) = {
+ .id = UCLASS_BOOTMETH,
+ .name = "bootmeth",
+ .flags = DM_UC_FLAG_SEQ_ALIAS,
+ .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
+};
diff --git a/boot/bootstd-uclass.c b/boot/bootstd-uclass.c
index 04823e5ba03..f812fbd0ae8 100644
--- a/boot/bootstd-uclass.c
+++ b/boot/bootstd-uclass.c
@@ -102,8 +102,10 @@ static int bootstd_probe(struct udevice *dev)
/* For now, bind the boormethod device if none are found in the devicetree */
int dm_scan_other(bool pre_reloc_only)
{
- struct udevice *bootstd;
- int ret;
+ struct driver *drv = ll_entry_start(struct driver, driver);
+ const int n_ents = ll_entry_count(struct driver, driver);
+ struct udevice *dev, *bootstd;
+ int i, ret;
/* These are not needed before relocation */
if (!(gd->flags & GD_FLG_RELOC))
@@ -118,6 +120,29 @@ int dm_scan_other(bool pre_reloc_only)
return log_msg_ret("bootstd", ret);
}
+ /* If there are no bootmeth devices, create them */
+ uclass_find_first_device(UCLASS_BOOTMETH, &dev);
+ if (dev)
+ return 0;
+
+ for (i = 0; i < n_ents; i++, drv++) {
+ /*
+ * Disable EFI Manager for now as no one uses it so it is
+ * confusing
+ */
+ if (drv->id == UCLASS_BOOTMETH &&
+ strcmp("efi_mgr_bootmeth", drv->name)) {
+ const char *name = drv->name;
+
+ if (!strncmp("bootmeth_", name, 9))
+ name += 9;
+ ret = device_bind(bootstd, drv, name, 0, ofnode_null(),
+ &dev); if (ret)
+
+ return log_msg_ret("meth", ret);
+ }
+ }
+
return 0;
}
diff --git a/include/bootmeth.h b/include/bootmeth.h
new file mode 100644
index 00000000000..645fdc93add
--- /dev/null
+++ b/include/bootmeth.h
@@ -0,0 +1,156 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#ifndef __bootmeth_h
+#define __bootmeth_h
+
+struct bootflow;
+struct bootflow_iter;
+struct udevice;
+
+/**
+ * struct bootmeth_uc_plat - information the uclass keeps about each bootmeth
+ *
+ * @desc: A long description of the bootmeth
+ */
+struct bootmeth_uc_plat {
+ const char *desc;
+};
+
+/** struct bootmeth_ops - Operations for boot methods */
+struct bootmeth_ops {
+ /**
+ * check_supported() - check if a bootmeth supports this bootflow
+ *
+ * This is optional. If not provided, the bootdev is assumed to be
+ * supported
+ *
+ * The bootmeth can check the bootdev (e.g. to make sure it is a
+ * network device) or the partition information. The following fields
+ * in @iter are available:
+ *
+ * name, dev, state, part
+ * max_part may be set if part != 0 (i.e. there is a valid partition
+ * table). Otherwise max_part is 0
+ * method is available but is the same as @dev
+ * the partition has not yet been read, nor has the filesystem been
+ * checked
+ *
+ * It may update only the flags in @iter
+ *
+ * @dev: Bootmethod device to check against
+ * @iter: On entry, provides bootdev, hwpart, part
+ * @return 0 if OK, -ENOTSUPP if this bootdev is not supported
+ */
+ int (*check)(struct udevice *dev, struct bootflow_iter *iter);
+
+ /**
+ * read_bootflow() - read a bootflow for a device
+ *
+ * @dev: Bootmethod device to use
+ * @bflow: On entry, provides dev, hwpart, part and method.
+ * Returns updated bootflow if found
+ * @return 0 if OK, -ve on error
+ */
+ int (*read_bootflow)(struct udevice *dev, struct bootflow *bflow);
+
+ /**
+ * read_file() - read a file needed for a bootflow
+ *
+ * Read a file from the same place as the bootflow came from
+ *
+ * @dev: Bootmethod device to use
+ * @bflow: Bootflow providing info on where to read from
+ * @file_path: Path to file (may be absolute or relative)
+ * @addr: Address to load file
+ * @sizep: On entry provides the maximum permitted size; on exit
+ * returns the size of the file
+ * @return 0 if OK, -ENOSPC if the file is too large for @sizep, other
+ * -ve value if something else goes wrong
+ */
+ int (*read_file)(struct udevice *dev, struct bootflow *bflow,
+ const char *file_path, ulong addr, ulong *sizep);
+
+ /**
+ * boot() - boot a bootflow
+ *
+ * @dev: Bootmethod device to boot
+ * @bflow: Bootflow to boot
+ * @return does not return on success, since it should boot the
+ * Operating Systemn. Returns -EFAULT if that fails, -ENOTSUPP if
+ * trying method resulted in finding out that is not actually
+ * supported for this boot and should not be tried again unless
+ * something changes, other -ve on other error
+ */
+ int (*boot)(struct udevice *dev, struct bootflow *bflow);
+};
+
+#define bootmeth_get_ops(dev) ((struct bootmeth_ops *)(dev)->driver->ops)
+
+/**
+ * bootmeth_check() - check if a bootmeth supports this bootflow
+ *
+ * This is optional. If not provided, the bootdev is assumed to be
+ * supported
+ *
+ * The bootmeth can check the bootdev (e.g. to make sure it is a
+ * network device) or the partition information. The following fields
+ * in @iter are available:
+ *
+ * name, dev, state, part
+ * max_part may be set if part != 0 (i.e. there is a valid partition
+ * table). Otherwise max_part is 0
+ * method is available but is the same as @dev
+ * the partition has not yet been read, nor has the filesystem been
+ * checked
+ *
+ * It may update only the flags in @iter
+ *
+ * @dev: Bootmethod device to check against
+ * @iter: On entry, provides bootdev, hwpart, part
+ * @return 0 if OK, -ENOTSUPP if this bootdev is not supported
+ */
+int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter);
+
+/**
+ * bootmeth_read_bootflow() - set up a bootflow for a device
+ *
+ * @dev: Bootmethod device to check
+ * @bflow: On entry, provides dev, hwpart, part and method.
+ * Returns updated bootflow if found
+ * @return 0 if OK, -ve on error
+ */
+int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow);
+
+/**
+ * bootmeth_read_file() - read a file needed for a bootflow
+ *
+ * Read a file from the same place as the bootflow came from
+ *
+ * @dev: Bootmethod device to use
+ * @bflow: Bootflow providing info on where to read from
+ * @file_path: Path to file (may be absolute or relative)
+ * @addr: Address to load file
+ * @sizep: On entry provides the maximum permitted size; on exit
+ * returns the size of the file
+ * @return 0 if OK, -ENOSPC if the file is too large for @sizep, other
+ * -ve value if something else goes wrong
+ */
+int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
+ const char *file_path, ulong addr, ulong *sizep);
+
+/**
+ * bootmeth_boot() - boot a bootflow
+ *
+ * @dev: Bootmethod device to boot
+ * @bflow: Bootflow to boot
+ * @return does not return on success, since it should boot the
+ * Operating Systemn. Returns -EFAULT if that fails, other -ve on
+ * other error
+ */
+int bootmeth_boot(struct udevice *dev, struct bootflow *bflow);
+
+#endif
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 03912cb0db8..1f977672a37 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -39,6 +39,7 @@ enum uclass_id {
UCLASS_BLK, /* Block device */
UCLASS_BOOTCOUNT, /* Bootcount backing store */
UCLASS_BOOTDEV, /* Boot device for locating an OS to boot */
+ UCLASS_BOOTMETH, /* Bootmethod for booting an OS */
UCLASS_BOOTSTD, /* Standard boot driver */
UCLASS_BUTTON, /* Button */
UCLASS_CACHE, /* Cache controller */
--
2.33.0.1079.g6e70778dc9-goog
More information about the U-Boot
mailing list