1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Bootmethod for extlinux boot from a block device
*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#define LOG_CATEGORY UCLASS_BOOTSTD
#include <asm/cache.h>
#include <bootdev.h>
#include <bootflow.h>
#include <bootmeth.h>
#include <bootstd.h>
#include <command.h>
#include <dm.h>
#include <extlinux.h>
#include <fs.h>
#include <malloc.h>
#include <mapmem.h>
#include <mmc.h>
#include <pxe_utils.h>
struct extlinux_plat {
bool use_fallback;
};
enum extlinux_option_type {
EO_FALLBACK,
EO_INVALID
};
struct extlinux_option {
char *name;
enum extlinux_option_type option;
};
static const struct extlinux_option options[] = {
{"fallback", EO_FALLBACK},
{NULL, EO_INVALID}
};
static enum extlinux_option_type get_option(const char *option)
{
int i = 0;
while (options[i].name) {
if (!strcmp(options[i].name, option))
return options[i].option;
i++;
}
return EO_INVALID;
};
static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
{
if (IS_ENABLED(CONFIG_SANDBOX)) {
int len;
len = snprintf(buf, maxsize, "OK");
return len + 1 < maxsize ? 0 : -ENOSPC;
}
return 0;
}
static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
char *file_addr, ulong *sizep)
{
struct extlinux_info *info = ctx->userdata;
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;
}
static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
{
int ret;
/* This only works on block devices */
ret = bootflow_iter_check_blk(iter);
if (ret)
return log_msg_ret("blk", ret);
return 0;
}
/**
* extlinux_fill_info() - Decode the extlinux file to find out its info
*
* @bflow: Bootflow to process
* @return 0 if OK, -ve on error
*/
static int extlinux_fill_info(struct bootflow *bflow)
{
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);
while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) {
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;
}
static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
{
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;
ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
} while (ret && prefixes && prefixes[++i]);
if (ret)
return log_msg_ret("try", ret);
size = bflow->size;
ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN);
if (ret)
return log_msg_ret("read", ret);
ret = extlinux_fill_info(bflow);
if (ret)
return log_msg_ret("inf", ret);
return 0;
}
static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
{
struct cmd_tbl cmdtp = {}; /* dummy */
struct pxe_context ctx;
struct extlinux_info info;
struct extlinux_plat *plat;
ulong addr;
int ret;
addr = map_to_sysmem(bflow->buf);
info.dev = dev;
info.bflow = bflow;
plat = dev_get_plat(dev);
ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
bflow->fname, false, plat->use_fallback);
if (ret)
return log_msg_ret("ctx", -EINVAL);
ret = pxe_process(&ctx, addr, false);
if (ret)
return log_msg_ret("bread", -EINVAL);
return 0;
}
static int extlinux_set_property(struct udevice *dev, const char *property, const char *value)
{
struct extlinux_plat *plat;
static enum extlinux_option_type option;
plat = dev_get_plat(dev);
option = get_option(property);
if (option == EO_INVALID) {
printf("Invalid option\n");
return -EINVAL;
}
switch (option) {
case EO_FALLBACK:
if (!strcmp(value, "1")) {
plat->use_fallback = true;
} else if (!strcmp(value, "0")) {
plat->use_fallback = false;
} else {
printf("Unexpected value '%s'\n", value);
return -EINVAL;
}
break;
default:
printf("Unrecognised property '%s'\n", property);
return -EINVAL;
}
return 0;
}
static int extlinux_bootmeth_bind(struct udevice *dev)
{
struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
"Extlinux boot from a block device" : "extlinux";
return 0;
}
static struct bootmeth_ops extlinux_bootmeth_ops = {
.get_state_desc = extlinux_get_state_desc,
.check = extlinux_check,
.read_bootflow = extlinux_read_bootflow,
.read_file = bootmeth_common_read_file,
.boot = extlinux_boot,
.set_property = extlinux_set_property,
};
static const struct udevice_id extlinux_bootmeth_ids[] = {
{ .compatible = "u-boot,extlinux" },
{ }
};
/* Put a number before 'extlinux' to provide a default ordering */
U_BOOT_DRIVER(bootmeth_1extlinux) = {
.name = "bootmeth_extlinux",
.id = UCLASS_BOOTMETH,
.of_match = extlinux_bootmeth_ids,
.ops = &extlinux_bootmeth_ops,
.bind = extlinux_bootmeth_bind,
.plat_auto = sizeof(struct extlinux_plat)
};
|