Use programmatic boot so that it is possible to boot without CONFIG_CMDLINE enabled. Series-to: u-boot Cover-letter: emulation: Improve support for booting from QFW U-Boot supports booting Linux from QFW which means that the kernel and any initrd are provided on the QEMU command line instead of being found in boot media. This series improves this support in several ways: - Enhances bootstd to implement 'bootflow read', thus allowing the files to be loaded and inspected (with potential cmdline changes) before booting - Updates bootstd to use programmatic boot, so that it works even when CONFIG_CMDLINE is disabled - Expands build-qemu script to allow providing cmdline and root disk It also includes a rough script to time U-Boot when running QEMU with kvm, making use of the qemu-boot-time repo: https://github.com/stefano-garzarella/qemu-boot-time.git END Signed-off-by: Simon Glass <sjg@chromium.org>
209 lines
5.4 KiB
C
209 lines
5.4 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* Bootmethod for QEMU qfw
|
|
*
|
|
* Copyright 2023 Google LLC
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
*/
|
|
|
|
#define LOG_CATEGORY UCLASS_BOOTSTD
|
|
|
|
#include <abuf.h>
|
|
#include <command.h>
|
|
#include <bootdev.h>
|
|
#include <bootflow.h>
|
|
#include <bootm.h>
|
|
#include <bootmeth.h>
|
|
#include <env.h>
|
|
#include <mapmem.h>
|
|
#include <qfw.h>
|
|
#include <dm.h>
|
|
|
|
static int qfw_check(struct udevice *dev, struct bootflow_iter *iter)
|
|
{
|
|
const struct udevice *media = dev_get_parent(iter->dev);
|
|
enum uclass_id id = device_get_uclass_id(media);
|
|
|
|
log_debug("media=%s\n", media->name);
|
|
if (id == UCLASS_QFW)
|
|
return 0;
|
|
|
|
return -ENOTSUPP;
|
|
}
|
|
|
|
static int qfw_read_bootflow(struct udevice *dev, struct bootflow *bflow)
|
|
{
|
|
struct udevice *qfw_dev = dev_get_parent(bflow->dev);
|
|
ulong setup, kern, ramdisk, setup_addr;
|
|
size_t cmdline_size;
|
|
struct abuf cmdline;
|
|
int ret;
|
|
|
|
/* Get the size of each region */
|
|
ret = qemu_fwcfg_read_info(qfw_dev, &setup, &kern, &ramdisk, &cmdline,
|
|
&setup_addr);
|
|
if (ret)
|
|
return log_msg_ret("qri", ret);
|
|
bflow->cmdline = abuf_uninit_move(&cmdline, &cmdline_size);
|
|
|
|
bflow->name = strdup("qfw");
|
|
if (!bflow->name)
|
|
return log_msg_ret("name", -ENOMEM);
|
|
|
|
/*
|
|
* create images for each; only cmdline has the actual data; the others
|
|
* only have a size for now, since the data has yet not been read
|
|
*/
|
|
if (!bootflow_img_add(bflow, "setup",
|
|
(enum bootflow_img_t)IH_TYPE_X86_SETUP,
|
|
setup_addr, setup))
|
|
return log_msg_ret("cri", -ENOMEM);
|
|
if (!bootflow_img_add(bflow, "kernel",
|
|
(enum bootflow_img_t)IH_TYPE_KERNEL, 0, kern))
|
|
return log_msg_ret("qrk", -ENOMEM);
|
|
if (ramdisk && !bootflow_img_add(bflow, "ramdisk",
|
|
(enum bootflow_img_t)IH_TYPE_RAMDISK,
|
|
0, ramdisk))
|
|
return log_msg_ret("qrr", -ENOMEM);
|
|
if (!bootflow_img_add(bflow, "cmdline", BFI_CMDLINE,
|
|
map_to_sysmem(bflow->cmdline), cmdline_size))
|
|
return log_msg_ret("qrc", -ENOMEM);
|
|
bflow->state = BOOTFLOWST_READY;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int qfw_read_files(struct udevice *dev, struct bootflow *bflow,
|
|
bool re_read, const struct bootflow_img **simgp,
|
|
const struct bootflow_img **kimgp,
|
|
const struct bootflow_img **rimgp)
|
|
{
|
|
struct udevice *qfw_dev = dev_get_parent(bflow->dev);
|
|
struct bootflow_img *kimg, *rimg;
|
|
struct abuf setup, kern, ramdisk;
|
|
const struct bootflow_img *simg;
|
|
|
|
simg = bootflow_img_find(bflow, (enum bootflow_img_t)IH_TYPE_X86_SETUP);
|
|
kimg = bootflow_img_findw(bflow, (enum bootflow_img_t)IH_TYPE_KERNEL);
|
|
rimg = bootflow_img_findw(bflow, (enum bootflow_img_t)IH_TYPE_RAMDISK);
|
|
if (!kimg)
|
|
return log_msg_ret("qfs", -EINVAL);
|
|
|
|
/* read files only if not already read */
|
|
if (re_read || !kimg->addr) {
|
|
abuf_init_const_addr(&setup, simg ? simg->addr : 0,
|
|
simg ? simg->size : 0);
|
|
abuf_init_const_addr(&kern, env_get_hex("kernel_addr_r", 0),
|
|
kimg->size);
|
|
abuf_init_const_addr(&ramdisk, env_get_hex("ramdisk_addr_r", 0),
|
|
rimg ? rimg->size : 0);
|
|
|
|
qemu_fwcfg_read_files(qfw_dev, &setup, &kern, &ramdisk);
|
|
kimg->addr = abuf_addr(&kern);
|
|
if (rimg)
|
|
rimg->addr = abuf_addr(&ramdisk);
|
|
}
|
|
|
|
if (simgp)
|
|
*simgp = simg;
|
|
if (kimgp)
|
|
*kimgp = kimg;
|
|
if (rimgp)
|
|
*rimgp = rimg;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int qfw_read_file(struct udevice *dev, struct bootflow *bflow,
|
|
const char *file_path, ulong addr,
|
|
enum bootflow_img_t type, ulong *sizep)
|
|
{
|
|
return -ENOSYS;
|
|
}
|
|
|
|
#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
|
|
static int qfw_read_all(struct udevice *dev, struct bootflow *bflow)
|
|
{
|
|
struct bootflow_img *kimg;
|
|
int ret;
|
|
|
|
kimg = bootflow_img_findw(bflow, (enum bootflow_img_t)IH_TYPE_KERNEL);
|
|
if (!kimg)
|
|
return log_msg_ret("qra", -ENOENT);
|
|
|
|
ret = qfw_read_files(dev, bflow, true, NULL, NULL, NULL);
|
|
if (ret)
|
|
return log_msg_ret("qrA", ret);
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
static int qfw_boot(struct udevice *dev, struct bootflow *bflow)
|
|
{
|
|
const struct bootflow_img *simg, *kimg, *rimg;
|
|
char conf_fdt[20], conf_ramdisk[40], addr_img_str[20];
|
|
struct bootm_info bmi;
|
|
int ret;
|
|
|
|
/* read the files if not already done */
|
|
ret = qfw_read_files(dev, bflow, false, &simg, &kimg, &rimg);
|
|
if (!kimg)
|
|
return log_msg_ret("qkf", -EINVAL);
|
|
|
|
ret = booti_run(&bmi);
|
|
bootm_init(&bmi);
|
|
snprintf(conf_fdt, sizeof(conf_fdt), "%lx",
|
|
(ulong)map_to_sysmem(gd->fdt_blob));
|
|
snprintf(addr_img_str, sizeof(addr_img_str), "%lx", kimg->addr);
|
|
bmi.addr_img = addr_img_str;
|
|
snprintf(conf_ramdisk, sizeof(conf_ramdisk), "%lx:%lx", rimg->addr,
|
|
rimg->size);
|
|
bmi.conf_ramdisk = conf_ramdisk;
|
|
|
|
ret = -ENOENT;
|
|
if (IS_ENABLED(CONFIG_CMD_BOOTI))
|
|
ret = booti_run(&bmi);
|
|
if (ret && IS_ENABLED(CONFIG_CMD_BOOTZ))
|
|
ret = bootz_run(&bmi);
|
|
if (ret && IS_ENABLED(CONFIG_ZBOOT) && simg) {
|
|
ret = zboot_run_args(kimg->addr, kimg->size,
|
|
rimg->addr, rimg->size, simg->addr,
|
|
*bflow->cmdline ? bflow->cmdline : NULL);
|
|
}
|
|
|
|
return ret ? -EIO : 0;
|
|
}
|
|
|
|
static int qfw_bootmeth_bind(struct udevice *dev)
|
|
{
|
|
struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
|
|
|
|
plat->desc = "QEMU boot using firmware interface";
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct bootmeth_ops qfw_bootmeth_ops = {
|
|
.check = qfw_check,
|
|
.read_bootflow = qfw_read_bootflow,
|
|
.read_file = qfw_read_file,
|
|
#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
|
|
.read_all = qfw_read_all,
|
|
#endif
|
|
.boot = qfw_boot,
|
|
};
|
|
|
|
static const struct udevice_id qfw_bootmeth_ids[] = {
|
|
{ .compatible = "u-boot,qfw-bootmeth" },
|
|
{ }
|
|
};
|
|
|
|
U_BOOT_DRIVER(bootmeth_qfw) = {
|
|
.name = "bootmeth_qfw",
|
|
.id = UCLASS_BOOTMETH,
|
|
.of_match = qfw_bootmeth_ids,
|
|
.ops = &qfw_bootmeth_ops,
|
|
.bind = qfw_bootmeth_bind,
|
|
};
|