1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for distro boot (syslinux boot from a block device)
5 * Copyright 2021 Google LLC
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_check_blk(iter);
64 return log_msg_ret("blk", ret);
70 * distro_fill_info() - Decode the extlinux file to find out distro info
72 * @bflow: Bootflow to process
73 * @return 0 if OK, -ve on error
75 static int distro_fill_info(struct bootflow *bflow)
82 log_debug("parsing bflow file size %x\n", bflow->size);
83 membuff_init(&mb, bflow->buf, bflow->size);
84 membuff_putraw(&mb, bflow->size, true, &data);
85 while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' '), len) {
88 tok = strsep(&p, " ");
90 if (!strcmp("label", tok)) {
91 bflow->os_name = strdup(p);
93 return log_msg_ret("os", -ENOMEM);
101 static int distro_read_bootflow(struct udevice *dev, struct bootflow *bflow)
103 struct blk_desc *desc;
104 const char *const *prefixes;
105 struct udevice *bootstd;
110 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
112 return log_msg_ret("std", ret);
114 /* If a block device, we require a partition table */
115 if (bflow->blk && !bflow->part)
118 prefixes = bootstd_get_prefixes(bootstd);
120 desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL;
122 prefix = prefixes ? prefixes[i] : NULL;
124 ret = bootmeth_try_file(bflow, desc, prefix, DISTRO_FNAME);
125 } while (ret && prefixes && prefixes[++i]);
127 return log_msg_ret("try", ret);
130 ret = bootmeth_alloc_file(bflow, 0x10000, 1);
132 return log_msg_ret("read", ret);
134 ret = distro_fill_info(bflow);
136 return log_msg_ret("inf", ret);
141 static int distro_boot(struct udevice *dev, struct bootflow *bflow)
143 struct cmd_tbl cmdtp = {}; /* dummy */
144 struct pxe_context ctx;
145 struct distro_info info;
149 addr = map_to_sysmem(bflow->buf);
152 ret = pxe_setup_ctx(&ctx, &cmdtp, distro_getfile, &info, true,
155 return log_msg_ret("ctx", -EINVAL);
157 ret = pxe_process(&ctx, addr, false);
159 return log_msg_ret("bread", -EINVAL);
164 static int distro_bootmeth_bind(struct udevice *dev)
166 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
168 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
169 "Syslinux boot from a block device" : "syslinux";
174 static struct bootmeth_ops distro_bootmeth_ops = {
175 .get_state_desc = distro_get_state_desc,
176 .check = distro_check,
177 .read_bootflow = distro_read_bootflow,
178 .read_file = bootmeth_common_read_file,
182 static const struct udevice_id distro_bootmeth_ids[] = {
183 { .compatible = "u-boot,distro-syslinux" },
187 U_BOOT_DRIVER(bootmeth_distro) = {
188 .name = "bootmeth_distro",
189 .id = UCLASS_BOOTMETH,
190 .of_match = distro_bootmeth_ids,
191 .ops = &distro_bootmeth_ops,
192 .bind = distro_bootmeth_bind,