1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for distro boot (syslinux boot from a block device)
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
9 #define LOG_CATEGORY UCLASS_BOOTSTD
23 #include <pxe_utils.h>
25 static int distro_get_state_desc(struct udevice *dev, char *buf, int maxsize)
27 if (IS_ENABLED(CONFIG_SANDBOX)) {
30 len = snprintf(buf, maxsize, "OK");
32 return len + 1 < maxsize ? 0 : -ENOSPC;
38 static int distro_getfile(struct pxe_context *ctx, const char *file_path,
39 char *file_addr, ulong *sizep)
41 struct distro_info *info = ctx->userdata;
45 addr = simple_strtoul(file_addr, NULL, 16);
49 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
52 return log_msg_ret("read", ret);
57 static int distro_check(struct udevice *dev, struct bootflow_iter *iter)
61 /* This only works on block devices */
62 ret = bootflow_iter_uses_blk_dev(iter);
64 return log_msg_ret("blk", ret);
69 static int distro_read_bootflow(struct udevice *dev, struct bootflow *bflow)
71 struct blk_desc *desc;
72 const char *const *prefixes;
73 struct udevice *bootstd;
78 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
80 return log_msg_ret("std", ret);
82 /* If a block device, we require a partition table */
83 if (bflow->blk && !bflow->part)
86 prefixes = bootstd_get_prefixes(bootstd);
88 desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL;
90 prefix = prefixes ? prefixes[i] : NULL;
92 ret = bootmeth_try_file(bflow, desc, prefix, DISTRO_FNAME);
93 } while (ret && prefixes && prefixes[++i]);
95 return log_msg_ret("try", ret);
98 ret = bootmeth_alloc_file(bflow, 0x10000, 1);
100 return log_msg_ret("read", ret);
105 static int distro_boot(struct udevice *dev, struct bootflow *bflow)
107 struct cmd_tbl cmdtp = {}; /* dummy */
108 struct pxe_context ctx;
109 struct distro_info info;
113 addr = map_to_sysmem(bflow->buf);
116 ret = pxe_setup_ctx(&ctx, &cmdtp, distro_getfile, &info, true,
119 return log_msg_ret("ctx", -EINVAL);
121 ret = pxe_process(&ctx, addr, false);
123 return log_msg_ret("bread", -EINVAL);
128 static int distro_bootmeth_bind(struct udevice *dev)
130 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
132 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
133 "Syslinux boot from a block device" : "syslinux";
138 static struct bootmeth_ops distro_bootmeth_ops = {
139 .get_state_desc = distro_get_state_desc,
140 .check = distro_check,
141 .read_bootflow = distro_read_bootflow,
142 .read_file = bootmeth_common_read_file,
146 static const struct udevice_id distro_bootmeth_ids[] = {
147 { .compatible = "u-boot,distro-syslinux" },
151 U_BOOT_DRIVER(bootmeth_distro) = {
152 .name = "bootmeth_distro",
153 .id = UCLASS_BOOTMETH,
154 .of_match = distro_bootmeth_ids,
155 .ops = &distro_bootmeth_ops,
156 .bind = distro_bootmeth_bind,