1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for extlinux boot from a block device
5 * Copyright 2021 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
22 #include <pxe_utils.h>
24 static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
26 if (IS_ENABLED(CONFIG_SANDBOX)) {
29 len = snprintf(buf, maxsize, "OK");
31 return len + 1 < maxsize ? 0 : -ENOSPC;
37 static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
38 char *file_addr, ulong *sizep)
40 struct extlinux_info *info = ctx->userdata;
44 addr = simple_strtoul(file_addr, NULL, 16);
48 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
51 return log_msg_ret("read", ret);
56 static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
60 /* This only works on block devices */
61 ret = bootflow_iter_check_blk(iter);
63 return log_msg_ret("blk", ret);
69 * extlinux_fill_info() - Decode the extlinux file to find out its info
71 * @bflow: Bootflow to process
72 * @return 0 if OK, -ve on error
74 static int extlinux_fill_info(struct bootflow *bflow)
81 log_debug("parsing bflow file size %x\n", bflow->size);
82 membuff_init(&mb, bflow->buf, bflow->size);
83 membuff_putraw(&mb, bflow->size, true, &data);
84 while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) {
87 tok = strsep(&p, " ");
89 if (!strcmp("label", tok)) {
90 bflow->os_name = strdup(p);
92 return log_msg_ret("os", -ENOMEM);
100 static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
102 struct blk_desc *desc;
103 const char *const *prefixes;
104 struct udevice *bootstd;
109 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
111 return log_msg_ret("std", ret);
113 /* If a block device, we require a partition table */
114 if (bflow->blk && !bflow->part)
117 prefixes = bootstd_get_prefixes(bootstd);
119 desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL;
121 prefix = prefixes ? prefixes[i] : NULL;
123 ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
124 } while (ret && prefixes && prefixes[++i]);
126 return log_msg_ret("try", ret);
129 ret = bootmeth_alloc_file(bflow, 0x10000, 1);
131 return log_msg_ret("read", ret);
133 ret = extlinux_fill_info(bflow);
135 return log_msg_ret("inf", ret);
140 static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
142 struct cmd_tbl cmdtp = {}; /* dummy */
143 struct pxe_context ctx;
144 struct extlinux_info info;
148 addr = map_to_sysmem(bflow->buf);
151 ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
152 bflow->fname, false);
154 return log_msg_ret("ctx", -EINVAL);
156 ret = pxe_process(&ctx, addr, false);
158 return log_msg_ret("bread", -EINVAL);
163 static int extlinux_bootmeth_bind(struct udevice *dev)
165 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
167 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
168 "Extlinux boot from a block device" : "extlinux";
173 static struct bootmeth_ops extlinux_bootmeth_ops = {
174 .get_state_desc = extlinux_get_state_desc,
175 .check = extlinux_check,
176 .read_bootflow = extlinux_read_bootflow,
177 .read_file = bootmeth_common_read_file,
178 .boot = extlinux_boot,
181 static const struct udevice_id extlinux_bootmeth_ids[] = {
182 { .compatible = "u-boot,extlinux" },
186 /* Put an number before 'extlinux' to provide a default ordering */
187 U_BOOT_DRIVER(bootmeth_1extlinux) = {
188 .name = "bootmeth_extlinux",
189 .id = UCLASS_BOOTMETH,
190 .of_match = extlinux_bootmeth_ids,
191 .ops = &extlinux_bootmeth_ops,
192 .bind = extlinux_bootmeth_bind,