2022-04-25 05:31:13 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
2023-05-10 22:34:46 +00:00
|
|
|
* Bootmethod for extlinux boot from a block device
|
2022-04-25 05:31:13 +00:00
|
|
|
*
|
|
|
|
* Copyright 2021 Google LLC
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_CATEGORY UCLASS_BOOTSTD
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <bootdev.h>
|
|
|
|
#include <bootflow.h>
|
|
|
|
#include <bootmeth.h>
|
|
|
|
#include <bootstd.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <dm.h>
|
2023-05-10 22:34:46 +00:00
|
|
|
#include <extlinux.h>
|
2022-04-25 05:31:13 +00:00
|
|
|
#include <fs.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <mapmem.h>
|
|
|
|
#include <mmc.h>
|
|
|
|
#include <pxe_utils.h>
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
|
2022-07-30 21:52:19 +00:00
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_SANDBOX)) {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = snprintf(buf, maxsize, "OK");
|
|
|
|
|
|
|
|
return len + 1 < maxsize ? 0 : -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
|
|
|
|
char *file_addr, ulong *sizep)
|
2022-04-25 05:31:13 +00:00
|
|
|
{
|
2023-05-10 22:34:46 +00:00
|
|
|
struct extlinux_info *info = ctx->userdata;
|
2022-04-25 05:31:13 +00:00
|
|
|
ulong addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
addr = simple_strtoul(file_addr, NULL, 16);
|
|
|
|
|
|
|
|
/* Allow up to 1GB */
|
|
|
|
*sizep = 1 << 30;
|
|
|
|
ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
|
|
|
|
sizep);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("read", ret);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
|
2022-04-25 05:31:13 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* This only works on block devices */
|
2023-01-17 17:47:54 +00:00
|
|
|
ret = bootflow_iter_check_blk(iter);
|
2022-04-25 05:31:13 +00:00
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("blk", ret);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-06 14:52:33 +00:00
|
|
|
/**
|
2023-05-10 22:34:46 +00:00
|
|
|
* extlinux_fill_info() - Decode the extlinux file to find out its info
|
2023-01-06 14:52:33 +00:00
|
|
|
*
|
|
|
|
* @bflow: Bootflow to process
|
|
|
|
* @return 0 if OK, -ve on error
|
|
|
|
*/
|
2023-05-10 22:34:46 +00:00
|
|
|
static int extlinux_fill_info(struct bootflow *bflow)
|
2023-01-06 14:52:33 +00:00
|
|
|
{
|
|
|
|
struct membuff mb;
|
|
|
|
char line[200];
|
|
|
|
char *data;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
log_debug("parsing bflow file size %x\n", bflow->size);
|
|
|
|
membuff_init(&mb, bflow->buf, bflow->size);
|
|
|
|
membuff_putraw(&mb, bflow->size, true, &data);
|
2024-01-05 07:22:10 +00:00
|
|
|
while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) {
|
2023-01-06 14:52:33 +00:00
|
|
|
char *tok, *p = line;
|
|
|
|
|
|
|
|
tok = strsep(&p, " ");
|
|
|
|
if (p) {
|
|
|
|
if (!strcmp("label", tok)) {
|
|
|
|
bflow->os_name = strdup(p);
|
|
|
|
if (!bflow->os_name)
|
|
|
|
return log_msg_ret("os", -ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
|
2022-04-25 05:31:13 +00:00
|
|
|
{
|
|
|
|
struct blk_desc *desc;
|
|
|
|
const char *const *prefixes;
|
|
|
|
struct udevice *bootstd;
|
|
|
|
const char *prefix;
|
|
|
|
loff_t size;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("std", ret);
|
|
|
|
|
|
|
|
/* If a block device, we require a partition table */
|
|
|
|
if (bflow->blk && !bflow->part)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
prefixes = bootstd_get_prefixes(bootstd);
|
|
|
|
i = 0;
|
|
|
|
desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL;
|
|
|
|
do {
|
|
|
|
prefix = prefixes ? prefixes[i] : NULL;
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
|
2022-04-25 05:31:13 +00:00
|
|
|
} while (ret && prefixes && prefixes[++i]);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("try", ret);
|
|
|
|
size = bflow->size;
|
|
|
|
|
|
|
|
ret = bootmeth_alloc_file(bflow, 0x10000, 1);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("read", ret);
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
ret = extlinux_fill_info(bflow);
|
2023-01-06 14:52:33 +00:00
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("inf", ret);
|
|
|
|
|
2022-04-25 05:31:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
|
2022-04-25 05:31:13 +00:00
|
|
|
{
|
|
|
|
struct cmd_tbl cmdtp = {}; /* dummy */
|
|
|
|
struct pxe_context ctx;
|
2023-05-10 22:34:46 +00:00
|
|
|
struct extlinux_info info;
|
2022-04-25 05:31:13 +00:00
|
|
|
ulong addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
addr = map_to_sysmem(bflow->buf);
|
|
|
|
info.dev = dev;
|
|
|
|
info.bflow = bflow;
|
2023-05-10 22:34:46 +00:00
|
|
|
ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
|
2023-06-09 14:59:01 +00:00
|
|
|
bflow->fname, false);
|
2022-04-25 05:31:13 +00:00
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("ctx", -EINVAL);
|
|
|
|
|
|
|
|
ret = pxe_process(&ctx, addr, false);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("bread", -EINVAL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static int extlinux_bootmeth_bind(struct udevice *dev)
|
2022-04-25 05:31:13 +00:00
|
|
|
{
|
|
|
|
struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
|
|
|
|
|
|
|
|
plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
|
2023-05-10 22:34:46 +00:00
|
|
|
"Extlinux boot from a block device" : "extlinux";
|
2022-04-25 05:31:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static struct bootmeth_ops extlinux_bootmeth_ops = {
|
|
|
|
.get_state_desc = extlinux_get_state_desc,
|
|
|
|
.check = extlinux_check,
|
|
|
|
.read_bootflow = extlinux_read_bootflow,
|
2022-04-25 05:31:13 +00:00
|
|
|
.read_file = bootmeth_common_read_file,
|
2023-05-10 22:34:46 +00:00
|
|
|
.boot = extlinux_boot,
|
2022-04-25 05:31:13 +00:00
|
|
|
};
|
|
|
|
|
2023-05-10 22:34:46 +00:00
|
|
|
static const struct udevice_id extlinux_bootmeth_ids[] = {
|
|
|
|
{ .compatible = "u-boot,extlinux" },
|
2022-04-25 05:31:13 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2023-08-19 22:49:35 +00:00
|
|
|
/* Put an number before 'extlinux' to provide a default ordering */
|
|
|
|
U_BOOT_DRIVER(bootmeth_1extlinux) = {
|
2023-05-10 22:34:46 +00:00
|
|
|
.name = "bootmeth_extlinux",
|
2022-04-25 05:31:13 +00:00
|
|
|
.id = UCLASS_BOOTMETH,
|
2023-05-10 22:34:46 +00:00
|
|
|
.of_match = extlinux_bootmeth_ids,
|
|
|
|
.ops = &extlinux_bootmeth_ops,
|
|
|
|
.bind = extlinux_bootmeth_bind,
|
2022-04-25 05:31:13 +00:00
|
|
|
};
|