Merge branch '2022-04-25-initial-implementation-of-stdboot'

To quote the author:
The bootflow feature provide a built-in way for U-Boot to automatically
boot an Operating System without custom scripting and other customisation.
This is called 'standard boot' since it provides a standard way for
U-Boot to boot a distro, without scripting.

It introduces the following concepts:

   - bootdev - a device which can hold a distro
   - bootmeth - a method to scan a bootdev to find bootflows (owned by
                U-Boot)
   - bootflow - a description of how to boot (owned by the distro)

This series provides an implementation of these, enabled to scan for
bootflows from MMC, USB and Ethernet. It supports the existing distro
boot as well as the EFI loader flow (bootefi/bootmgr). It works
similiarly to the existing script-based approach, but is native to
U-Boot.

With this we can boot on a Raspberry Pi 3 with just one command:

   bootflow scan -lb

which means to scan, listing (-l) each bootflow and trying to boot each
one (-b). The final patch shows this.

With a standard way to identify boot devices, booting become easier. It
also should be possible to support U-Boot scripts, for backwards
compatibility only.

...

The design is described in these two documents:

https://drive.google.com/file/d/1ggW0KJpUOR__vBkj3l61L2dav4ZkNC12/view?usp=sharing

https://drive.google.com/file/d/1kTrflO9vvGlKp-ZH_jlgb9TY3WYG6FF9/view?usp=sharing
This commit is contained in:
Tom Rini
2022-04-25 16:02:27 -04:00
86 changed files with 7188 additions and 110 deletions

View File

@@ -211,6 +211,45 @@ config CMD_BOOTM_PRE_LOAD
This stage allow to check or modify the image provided
to the bootm command.
config CMD_BOOTDEV
bool "bootdev"
depends on BOOTSTD
default y if BOOTSTD_FULL
help
Support listing available bootdevs (boot devices) which can provide an
OS to boot, as well as showing information about a particular one.
This command is not necessary for bootstd to work.
config CMD_BOOTFLOW
bool "bootflow"
depends on BOOTSTD
default y
help
Support scanning for bootflows available with the bootdevs. The
bootflows can optionally be booted.
config CMD_BOOTFLOW_FULL
bool "bootflow - extract subcommands"
depends on BOOTSTD_FULL
default y if BOOTSTD_FULL
help
Add the ability to list the available bootflows, select one and obtain
information about it.
This command is not necessary for bootstd to work.
config CMD_BOOTMETH
bool "bootmeth"
depends on BOOTSTD
default y if BOOTSTD_FULL
help
Support listing available bootmethds (methods used to boot an
Operating System), as well as selecting the order that the bootmeths
are used.
This command is not necessary for bootstd to work.
config BOOTM_EFI
bool "Support booting UEFI FIT images"
depends on CMD_BOOTEFI && CMD_BOOTM && FIT

View File

@@ -19,6 +19,9 @@ obj-$(CONFIG_CMD_AB_SELECT) += ab_select.o
obj-$(CONFIG_CMD_ADC) += adc.o
obj-$(CONFIG_CMD_ARMFLASH) += armflash.o
obj-$(CONFIG_HAVE_BLOCK_DEVICE) += blk_common.o
obj-$(CONFIG_CMD_BOOTDEV) += bootdev.o
obj-$(CONFIG_CMD_BOOTFLOW) += bootflow.o
obj-$(CONFIG_CMD_BOOTMETH) += bootmeth.o
obj-$(CONFIG_CMD_SOURCE) += source.o
obj-$(CONFIG_CMD_BCB) += bcb.o
obj-$(CONFIG_CMD_BDI) += bdinfo.o

120
cmd/bootdev.c Normal file
View File

@@ -0,0 +1,120 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* 'bootdev' command
*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <bootdev.h>
#include <bootflow.h>
#include <bootstd.h>
#include <command.h>
#include <dm.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>
static int bootdev_check_state(struct bootstd_priv **stdp)
{
struct bootstd_priv *std;
int ret;
ret = bootstd_get_priv(&std);
if (ret)
return ret;
if (!std->cur_bootdev) {
printf("Please use 'bootdev select' first\n");
return -ENOENT;
}
*stdp = std;
return 0;
}
static int do_bootdev_list(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
bool probe;
probe = argc >= 2 && !strcmp(argv[1], "-p");
bootdev_list(probe);
return 0;
}
static int do_bootdev_select(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *std;
struct udevice *dev;
int ret;
ret = bootstd_get_priv(&std);
if (ret)
return CMD_RET_FAILURE;
if (argc < 2) {
std->cur_bootdev = NULL;
return 0;
}
if (bootdev_find_by_any(argv[1], &dev))
return CMD_RET_FAILURE;
std->cur_bootdev = dev;
return 0;
}
static int do_bootdev_info(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *priv;
struct bootflow *bflow;
int ret, i, num_valid;
struct udevice *dev;
bool probe;
probe = argc >= 2 && !strcmp(argv[1], "-p");
ret = bootdev_check_state(&priv);
if (ret)
return CMD_RET_FAILURE;
dev = priv->cur_bootdev;
/* Count the number of bootflows, including how many are valid*/
num_valid = 0;
for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
!ret;
ret = bootdev_next_bootflow(&bflow), i++)
num_valid += bflow->state == BOOTFLOWST_READY;
/*
* Prove the device, if requested, otherwise assume that there is no
* error
*/
ret = 0;
if (probe)
ret = device_probe(dev);
printf("Name: %s\n", dev->name);
printf("Sequence: %d\n", dev_seq(dev));
printf("Status: %s\n", ret ? simple_itoa(ret) : device_active(dev) ?
"Probed" : "OK");
printf("Uclass: %s\n", dev_get_uclass_name(dev_get_parent(dev)));
printf("Bootflows: %d (%d valid)\n", i, num_valid);
return 0;
}
#ifdef CONFIG_SYS_LONGHELP
static char bootdev_help_text[] =
"list [-p] - list all available bootdevs (-p to probe)\n"
"bootdev select <bd> - select a bootdev by name | label | seq\n"
"bootdev info [-p] - show information about a bootdev (-p to probe)";
#endif
U_BOOT_CMD_WITH_SUBCMDS(bootdev, "Boot devices", bootdev_help_text,
U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootdev_list),
U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootdev_select),
U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootdev_info));

404
cmd/bootflow.c Normal file
View File

@@ -0,0 +1,404 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* 'bootflow' command
*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <bootdev.h>
#include <bootflow.h>
#include <bootstd.h>
#include <command.h>
#include <console.h>
#include <dm.h>
#include <mapmem.h>
/**
* report_bootflow_err() - Report where a bootflow failed
*
* When a bootflow does not make it to the 'loaded' state, something went wrong.
* Print a helpful message if there is an error
*
* @bflow: Bootflow to process
* @err: Error code (0 if none)
*/
static void report_bootflow_err(struct bootflow *bflow, int err)
{
if (!err)
return;
/* Indent out to 'Method' */
printf(" ** ");
switch (bflow->state) {
case BOOTFLOWST_BASE:
printf("No media/partition found");
break;
case BOOTFLOWST_MEDIA:
printf("No partition found");
break;
case BOOTFLOWST_PART:
printf("No filesystem found");
break;
case BOOTFLOWST_FS:
printf("File not found");
break;
case BOOTFLOWST_FILE:
printf("File cannot be loaded");
break;
case BOOTFLOWST_READY:
printf("Ready");
break;
case BOOTFLOWST_COUNT:
break;
}
printf(", err=%d\n", err);
}
/**
* show_bootflow() - Show the status of a bootflow
*
* @seq: Bootflow index
* @bflow: Bootflow to show
* @errors: True to show the error received, if any
*/
static void show_bootflow(int index, struct bootflow *bflow, bool errors)
{
printf("%3x %-11s %-6s %-9.9s %4x %-25.25s %s\n", index,
bflow->method->name, bootflow_state_get_name(bflow->state),
dev_get_uclass_name(dev_get_parent(bflow->dev)), bflow->part,
bflow->name, bflow->fname);
if (errors)
report_bootflow_err(bflow, bflow->err);
}
static void show_header(void)
{
printf("Seq Method State Uclass Part Name Filename\n");
printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
}
static void show_footer(int count, int num_valid)
{
printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
printf("(%d bootflow%s, %d valid)\n", count, count != 1 ? "s" : "",
num_valid);
}
static int do_bootflow_scan(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *std;
struct bootflow_iter iter;
struct udevice *dev;
struct bootflow bflow;
bool all = false, boot = false, errors = false, list = false;
int num_valid = 0;
bool has_args;
int ret, i;
int flags;
ret = bootstd_get_priv(&std);
if (ret)
return CMD_RET_FAILURE;
dev = std->cur_bootdev;
has_args = argc > 1 && *argv[1] == '-';
if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL)) {
if (has_args) {
all = strchr(argv[1], 'a');
boot = strchr(argv[1], 'b');
errors = strchr(argv[1], 'e');
list = strchr(argv[1], 'l');
argc--;
argv++;
}
if (argc > 1) {
const char *label = argv[1];
if (bootdev_find_by_any(label, &dev))
return CMD_RET_FAILURE;
}
} else {
if (has_args) {
printf("Flags not supported: enable CONFIG_BOOTFLOW_FULL\n");
return CMD_RET_USAGE;
}
boot = true;
}
std->cur_bootflow = NULL;
flags = 0;
if (list)
flags |= BOOTFLOWF_SHOW;
if (all)
flags |= BOOTFLOWF_ALL;
/*
* If we have a device, just scan for bootflows attached to that device
*/
if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL) && dev) {
if (list) {
printf("Scanning for bootflows in bootdev '%s'\n",
dev->name);
show_header();
}
bootdev_clear_bootflows(dev);
for (i = 0,
ret = bootflow_scan_bootdev(dev, &iter, flags, &bflow);
i < 1000 && ret != -ENODEV;
i++, ret = bootflow_scan_next(&iter, &bflow)) {
bflow.err = ret;
if (!ret)
num_valid++;
ret = bootdev_add_bootflow(&bflow);
if (ret) {
printf("Out of memory\n");
return CMD_RET_FAILURE;
}
if (list)
show_bootflow(i, &bflow, errors);
if (boot && !bflow.err)
bootflow_run_boot(&iter, &bflow);
}
} else {
if (list) {
printf("Scanning for bootflows in all bootdevs\n");
show_header();
}
bootstd_clear_glob();
for (i = 0,
ret = bootflow_scan_first(&iter, flags, &bflow);
i < 1000 && ret != -ENODEV;
i++, ret = bootflow_scan_next(&iter, &bflow)) {
bflow.err = ret;
if (!ret)
num_valid++;
ret = bootdev_add_bootflow(&bflow);
if (ret) {
printf("Out of memory\n");
return CMD_RET_FAILURE;
}
if (list)
show_bootflow(i, &bflow, errors);
if (boot && !bflow.err)
bootflow_run_boot(&iter, &bflow);
}
}
bootflow_iter_uninit(&iter);
if (list)
show_footer(i, num_valid);
return 0;
}
#ifdef CONFIG_CMD_BOOTFLOW_FULL
static int do_bootflow_list(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *std;
struct udevice *dev;
struct bootflow *bflow;
int num_valid = 0;
bool errors = false;
int ret, i;
if (argc > 1 && *argv[1] == '-')
errors = strchr(argv[1], 'e');
ret = bootstd_get_priv(&std);
if (ret)
return CMD_RET_FAILURE;
dev = std->cur_bootdev;
/* If we have a device, just list bootflows attached to that device */
if (dev) {
printf("Showing bootflows for bootdev '%s'\n", dev->name);
show_header();
for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
!ret;
ret = bootdev_next_bootflow(&bflow), i++) {
num_valid += bflow->state == BOOTFLOWST_READY;
show_bootflow(i, bflow, errors);
}
} else {
printf("Showing all bootflows\n");
show_header();
for (ret = bootflow_first_glob(&bflow), i = 0;
!ret;
ret = bootflow_next_glob(&bflow), i++) {
num_valid += bflow->state == BOOTFLOWST_READY;
show_bootflow(i, bflow, errors);
}
}
show_footer(i, num_valid);
return 0;
}
static int do_bootflow_select(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *std;
struct bootflow *bflow, *found;
struct udevice *dev;
const char *name;
char *endp;
int seq, i;
int ret;
ret = bootstd_get_priv(&std);
if (ret)
return CMD_RET_FAILURE;
;
if (argc < 2) {
std->cur_bootflow = NULL;
return 0;
}
dev = std->cur_bootdev;
name = argv[1];
seq = simple_strtol(name, &endp, 16);
found = NULL;
/*
* If we have a bootdev device, only allow selection of bootflows
* attached to that device
*/
if (dev) {
for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
!ret;
ret = bootdev_next_bootflow(&bflow), i++) {
if (*endp ? !strcmp(bflow->name, name) : i == seq) {
found = bflow;
break;
}
}
} else {
for (ret = bootflow_first_glob(&bflow), i = 0;
!ret;
ret = bootflow_next_glob(&bflow), i++) {
if (*endp ? !strcmp(bflow->name, name) : i == seq) {
found = bflow;
break;
}
}
}
if (!found) {
printf("Cannot find bootflow '%s' ", name);
if (dev)
printf("in bootdev '%s' ", dev->name);
printf("(err=%d)\n", ret);
return CMD_RET_FAILURE;
}
std->cur_bootflow = found;
return 0;
}
static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *std;
struct bootflow *bflow;
bool dump = false;
int ret;
if (argc > 1 && *argv[1] == '-')
dump = strchr(argv[1], 'd');
ret = bootstd_get_priv(&std);
if (ret)
return CMD_RET_FAILURE;
if (!std->cur_bootflow) {
printf("No bootflow selected\n");
return CMD_RET_FAILURE;
}
bflow = std->cur_bootflow;
printf("Name: %s\n", bflow->name);
printf("Device: %s\n", bflow->dev->name);
printf("Block dev: %s\n", bflow->blk ? bflow->blk->name : "(none)");
printf("Method: %s\n", bflow->method->name);
printf("State: %s\n", bootflow_state_get_name(bflow->state));
printf("Partition: %d\n", bflow->part);
printf("Subdir: %s\n", bflow->subdir ? bflow->subdir : "(none)");
printf("Filename: %s\n", bflow->fname);
printf("Buffer: %lx\n", (ulong)map_to_sysmem(bflow->buf));
printf("Size: %x (%d bytes)\n", bflow->size, bflow->size);
printf("Error: %d\n", bflow->err);
if (dump && bflow->buf) {
/* Set some sort of maximum on the size */
int size = min(bflow->size, 10 << 10);
int i;
printf("Contents:\n\n");
for (i = 0; i < size; i++) {
putc(bflow->buf[i]);
if (!(i % 128) && ctrlc()) {
printf("...interrupted\n");
break;
}
}
}
return 0;
}
static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *std;
struct bootflow *bflow;
int ret;
ret = bootstd_get_priv(&std);
if (ret)
return CMD_RET_FAILURE;
/*
* Require a current bootflow. Users can use 'bootflow scan -b' to
* automatically scan and boot, if needed.
*/
if (!std->cur_bootflow) {
printf("No bootflow selected\n");
return CMD_RET_FAILURE;
}
bflow = std->cur_bootflow;
ret = bootflow_run_boot(NULL, bflow);
if (ret)
return CMD_RET_FAILURE;
return 0;
}
#endif /* CONFIG_CMD_BOOTFLOW_FULL */
#ifdef CONFIG_SYS_LONGHELP
static char bootflow_help_text[] =
#ifdef CONFIG_CMD_BOOTFLOW_FULL
"scan [-abel] [bdev] - scan for valid bootflows (-l list, -a all, -e errors, -b boot)\n"
"bootflow list [-e] - list scanned bootflows (-e errors)\n"
"bootflow select [<num>|<name>] - 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)";
#else
"scan - boot first available bootflow\n";
#endif
#endif /* CONFIG_SYS_LONGHELP */
U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text,
U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_bootflow_scan),
#ifdef CONFIG_CMD_BOOTFLOW_FULL
U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list),
U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select),
U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info),
U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot)
#endif
);

113
cmd/bootmeth.c Normal file
View File

@@ -0,0 +1,113 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* 'bootmeth' command
*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <bootdev.h>
#include <bootmeth.h>
#include <bootstd.h>
#include <command.h>
#include <dm.h>
#include <malloc.h>
#include <dm/uclass-internal.h>
static int do_bootmeth_list(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct bootstd_priv *std;
struct udevice *dev;
bool use_order;
bool all = false;
int ret;
int i;
if (argc > 1 && *argv[1] == '-') {
all = strchr(argv[1], 'a');
argc--;
argv++;
}
ret = bootstd_get_priv(&std);
if (ret) {
printf("Cannot get bootstd (err=%d)\n", ret);
return CMD_RET_FAILURE;
}
printf("Order Seq Name Description\n");
printf("----- --- ------------------ ------------------\n");
/*
* Use the ordering if we have one, so long as we are not trying to list
* all bootmethds
*/
use_order = std->bootmeth_count && !all;
if (use_order)
dev = std->bootmeth_order[0];
else
ret = uclass_find_first_device(UCLASS_BOOTMETH, &dev);
for (i = 0; dev;) {
struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(dev);
int order = i;
/*
* With the -a flag we may list bootdevs that are not in the
* ordering. Find their place in the order
*/
if (all && std->bootmeth_count) {
int j;
/* Find the position of this bootmeth in the order */
order = -1;
for (j = 0; j < std->bootmeth_count; j++) {
if (std->bootmeth_order[j] == dev)
order = j;
}
}
if (order == -1)
printf("%5s", "-");
else
printf("%5x", order);
printf(" %3x %-19.19s %s\n", dev_seq(dev), dev->name,
ucp->desc);
i++;
if (use_order)
dev = std->bootmeth_order[i];
else
uclass_find_next_device(&dev);
}
printf("----- --- ------------------ ------------------\n");
printf("(%d bootmeth%s)\n", i, i != 1 ? "s" : "");
return 0;
}
static int do_bootmeth_order(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
int ret;
ret = bootmeth_set_order(argv[1]);
if (ret) {
printf("Failed (err=%d)\n", ret);
return CMD_RET_FAILURE;
}
env_set("bootmeths", argv[1]);
return 0;
}
#ifdef CONFIG_SYS_LONGHELP
static char bootmeth_help_text[] =
"list [-a] - list available bootmeths (-a all)\n"
"bootmeth order [<bd> ...] - select bootmeth order / subset to use";
#endif
U_BOOT_CMD_WITH_SUBCMDS(bootmeth, "Boot methods", bootmeth_help_text,
U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootmeth_list),
U_BOOT_SUBCMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_bootmeth_order));