bootstd: Add the bootmeth uclass and helpers

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@chromium.org>
This commit is contained in:
Simon Glass 2022-04-24 23:31:08 -06:00 committed by Tom Rini
parent 201417d700
commit a950d31abe
7 changed files with 605 additions and 2 deletions

View file

@ -696,10 +696,12 @@ F: tools/binman/
BOOTDEVICE
M: Simon Glass <sjg@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

View file

@ -20,6 +20,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

333
boot/bootmeth-uclass.c Normal file
View file

@ -0,0 +1,333 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#define LOG_CATEGORY UCLASS_BOOTSTD
#include <common.h>
#include <blk.h>
#include <bootflow.h>
#include <bootmeth.h>
#include <bootstd.h>
#include <dm.h>
#include <env_internal.h>
#include <fs.h>
#include <malloc.h>
#include <mapmem.h>
#include <dm/uclass-internal.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);
}
/**
* bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
*
* This sets up the ordering information in @iter, based on the selected
* ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no
* ordering there, then all bootmethods are added
*
* @iter: Iterator to update with the order
* Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
* on other error
*/
int bootmeth_setup_iter_order(struct bootflow_iter *iter)
{
struct bootstd_priv *std;
struct udevice **order;
int count;
int ret;
ret = bootstd_get_priv(&std);
if (ret)
return ret;
/* Create an array large enough */
count = std->bootmeth_count ? std->bootmeth_count :
uclass_id_count(UCLASS_BOOTMETH);
if (!count)
return log_msg_ret("count", -ENOENT);
order = calloc(count, sizeof(struct udevice *));
if (!order)
return log_msg_ret("order", -ENOMEM);
/* If we have an ordering, copy it */
if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
memcpy(order, std->bootmeth_order,
count * sizeof(struct bootmeth *));
} else {
struct udevice *dev;
int i, upto;
/*
* Get a list of bootmethods, in seq order (i.e. using aliases).
* There may be gaps so try to count up high enough to find them
* all.
*/
for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, i,
&dev);
if (!ret)
order[upto++] = dev;
}
count = upto;
}
iter->method_order = order;
iter->num_methods = count;
iter->cur_method = 0;
return 0;
}
int bootmeth_set_order(const char *order_str)
{
struct bootstd_priv *std;
struct udevice **order;
int count, ret, i, len;
const char *s, *p;
ret = bootstd_get_priv(&std);
if (ret)
return ret;
if (!order_str) {
free(std->bootmeth_order);
std->bootmeth_order = NULL;
std->bootmeth_count = 0;
return 0;
}
/* Create an array large enough */
count = uclass_id_count(UCLASS_BOOTMETH);
if (!count)
return log_msg_ret("count", -ENOENT);
order = calloc(count + 1, sizeof(struct udevice *));
if (!order)
return log_msg_ret("order", -ENOMEM);
for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
struct udevice *dev;
p = strchrnul(s, ' ');
len = p - s;
ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
&dev);
if (ret) {
printf("Unknown bootmeth '%.*s'\n", len, s);
free(order);
return ret;
}
order[i] = dev;
}
order[i] = NULL;
free(std->bootmeth_order);
std->bootmeth_order = order;
std->bootmeth_count = i;
return 0;
}
/**
* setup_fs() - Set up read to read a file
*
* We must redo the setup before each filesystem operation. This function
* handles that, including setting the filesystem type if a block device is not
* being used
*
* @bflow: Information about file to try
* @desc: Block descriptor to read from (NULL if not a block device)
* Return: 0 if OK, -ve on error
*/
static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
{
int ret;
if (desc) {
ret = fs_set_blk_dev_with_part(desc, bflow->part);
if (ret)
return log_msg_ret("set", ret);
} else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
fs_set_type(bflow->fs_type);
}
return 0;
}
int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
const char *prefix, const char *fname)
{
char path[200];
loff_t size;
int ret, ret2;
snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
log_debug("trying: %s\n", path);
free(bflow->fname);
bflow->fname = strdup(path);
if (!bflow->fname)
return log_msg_ret("name", -ENOMEM);
if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
fs_set_type(bflow->fs_type);
ret = fs_size(path, &size);
log_debug(" %s - err=%d\n", path, ret);
/* Sadly FS closes the file after fs_size() so we must redo this */
ret2 = setup_fs(bflow, desc);
if (ret2)
return log_msg_ret("fs", ret2);
if (ret)
return log_msg_ret("size", ret);
bflow->size = size;
bflow->state = BOOTFLOWST_FILE;
return 0;
}
int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
{
loff_t bytes_read;
ulong addr;
char *buf;
uint size;
int ret;
size = bflow->size;
log_debug(" - script file size %x\n", size);
if (size > size_limit)
return log_msg_ret("chk", -E2BIG);
buf = memalign(align, size + 1);
if (!buf)
return log_msg_ret("buf", -ENOMEM);
addr = map_to_sysmem(buf);
ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
if (ret) {
free(buf);
return log_msg_ret("read", ret);
}
if (size != bytes_read)
return log_msg_ret("bread", -EINVAL);
buf[size] = '\0';
bflow->state = BOOTFLOWST_READY;
bflow->buf = buf;
return 0;
}
int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
const char *file_path, ulong addr, ulong *sizep)
{
struct blk_desc *desc = NULL;
loff_t len_read;
loff_t size;
int ret;
if (bflow->blk)
desc = dev_get_uclass_plat(bflow->blk);
ret = setup_fs(bflow, desc);
if (ret)
return log_msg_ret("fs", ret);
ret = fs_size(file_path, &size);
if (ret)
return log_msg_ret("size", ret);
if (size > *sizep)
return log_msg_ret("spc", -ENOSPC);
ret = setup_fs(bflow, desc);
if (ret)
return log_msg_ret("fs", ret);
ret = fs_read(file_path, addr, 0, 0, &len_read);
if (ret)
return ret;
*sizep = len_read;
return 0;
}
#ifdef CONFIG_BOOTSTD_FULL
/**
* on_bootmeths() - Update the bootmeth order
*
* This will check for a valid baudrate and only apply it if valid.
*/
static int on_bootmeths(const char *name, const char *value, enum env_op op,
int flags)
{
int ret;
switch (op) {
case env_op_create:
case env_op_overwrite:
ret = bootmeth_set_order(value);
if (ret)
return 1;
return 0;
case env_op_delete:
bootmeth_set_order(NULL);
fallthrough;
default:
return 0;
}
}
U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
#endif /* CONFIG_BOOTSTD_FULL */
UCLASS_DRIVER(bootmeth) = {
.id = UCLASS_BOOTMETH,
.name = "bootmeth",
.flags = DM_UC_FLAG_SEQ_ALIAS,
.per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
};

View file

@ -109,8 +109,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))
@ -125,6 +127,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;
}

234
include/bootmeth.h Normal file
View file

@ -0,0 +1,234 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#ifndef __bootmeth_h
#define __bootmeth_h
struct blk_desc;
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);
/**
* bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
*
* This sets up the ordering information in @iter, based on the selected
* ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no
* ordering there, then all bootmethods are added
*
* @iter: Iterator to update with the order
* Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
* on other error
*/
int bootmeth_setup_iter_order(struct bootflow_iter *iter);
/**
* bootmeth_set_order() - Set the bootmeth order
*
* This selects the ordering to use for bootmeths
*
* @order_str: String containing the ordering. This is a comma-separate list of
* bootmeth-device names, e.g. "syslinux,efi". If empty then a default ordering
* is used, based on the sequence number of devices (i.e. using aliases)
* Return: 0 if OK, -ENODEV if an unknown bootmeth is mentioned, -ENOMEM if
* out of memory, -ENOENT if there are no bootmeth devices
*/
int bootmeth_set_order(const char *order_str);
/**
* bootmeth_try_file() - See we can access a given file
*
* Check for a file with a given name. If found, the filename is allocated in
* @bflow
*
* Sets the state to BOOTFLOWST_FILE on success. It also calls
* fs_set_blk_dev_with_part() so that this does not need to be done by the
* caller before reading the file.
*
* @bflow: Information about file to try
* @desc: Block descriptor to read from
* @prefix: Filename prefix to prepend to @fname (NULL for none)
* @fname: Filename to read
* Return: 0 if OK, -ENOMEM if not enough memory to allocate bflow->fname,
* other -ve value on other error
*/
int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
const char *prefix, const char *fname);
/**
* bootmeth_alloc_file() - Allocate and read a bootflow file
*
* Allocates memory for a bootflow file and reads it in. Sets the state to
* BOOTFLOWST_READY on success
*
* Note that fs_set_blk_dev_with_part() must have been called previously.
*
* @bflow: Information about file to read
* @size_limit: Maximum file size to permit
* @align: Allocation alignment (1 for unaligned)
* Return: 0 if OK, -E2BIG if file is too large, -ENOMEM if out of memory,
* other -ve on other error
*/
int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align);
/**
* bootmeth_common_read_file() - Common handler for reading a file
*
* Reads a named file from the same location as the bootflow file.
*
* @dev: bootmeth device to read from
* @bflow: Bootflow information
* @file_path: Path to file
* @addr: Address to load file to
* @sizep: On entry, the maximum file size to accept, on exit the actual file
* size read
*/
int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
const char *file_path, ulong addr, ulong *sizep);
#endif

View file

@ -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 */

View file

@ -57,6 +57,12 @@
#define NET_CALLBACKS
#endif
#ifdef CONFIG_BOOTSTD
#define BOOTSTD_CALLBACK "bootmeths:bootmeths,"
#else
#define BOOTSTD_CALLBACK
#endif
/*
* This list of callback bindings is static, but may be overridden by defining
* a new association in the ".callbacks" environment variable.
@ -65,6 +71,7 @@
ENV_DOT_ESCAPE ENV_FLAGS_VAR ":flags," \
"baudrate:baudrate," \
NET_CALLBACKS \
BOOTSTD_CALLBACK \
"loadaddr:loadaddr," \
SILENT_CALLBACK \
SPLASHIMAGE_CALLBACK \