1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for extlinux boot using PXE (network boot)
5 * Copyright 2021 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
23 #include <pxe_utils.h>
25 static int extlinux_pxe_getfile(struct pxe_context *ctx, const char *file_path,
26 char *file_addr, ulong *sizep)
28 struct extlinux_info *info = ctx->userdata;
32 addr = simple_strtoul(file_addr, NULL, 16);
36 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
39 return log_msg_ret("read", ret);
44 static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
48 /* This only works on network devices */
49 ret = bootflow_iter_check_net(iter);
51 return log_msg_ret("net", ret);
53 if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
54 return log_msg_ret("dhcp", -ENOTSUPP);
59 static int extlinux_pxe_read_bootflow(struct udevice *dev,
60 struct bootflow *bflow)
70 addr_str = env_get("pxefile_addr_r");
72 return log_msg_ret("pxeb", -EPERM);
73 addr = simple_strtoul(addr_str, NULL, 16);
75 log_debug("calling pxe_get()\n");
76 ret = pxe_get(addr, &bootdir, &size, false);
77 log_debug("pxe_get() returned %d\n", ret);
79 return log_msg_ret("pxeb", ret);
82 /* Use the directory of the dhcp bootdir as our subdir, if provided */
84 const char *last_slash;
87 last_slash = strrchr(bootdir, '/');
89 path_len = (last_slash - bootdir) + 1;
90 bflow->subdir = malloc(path_len + 1);
91 memcpy(bflow->subdir, bootdir, path_len);
92 bflow->subdir[path_len] = '\0';
95 snprintf(fname, sizeof(fname), "%s%s",
96 bflow->subdir ? bflow->subdir : "", EXTLINUX_FNAME);
98 bflow->fname = strdup(fname);
100 return log_msg_ret("name", -ENOMEM);
102 bflow->state = BOOTFLOWST_READY;
104 /* Allocate the buffer, including the \0 byte added by get_pxe_file() */
105 buf = malloc(size + 1);
107 return log_msg_ret("buf", -ENOMEM);
108 memcpy(buf, map_sysmem(addr, 0), size + 1);
114 static int extlinux_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
115 const char *file_path, ulong addr,
118 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
119 struct pxe_context *ctx = dev_get_priv(dev);
124 sprintf(file_addr, "%lx", addr);
125 tftp_argv[1] = file_addr;
126 tftp_argv[2] = (void *)file_path;
128 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
130 ret = pxe_get_file_size(&size);
132 return log_msg_ret("tftp", ret);
134 return log_msg_ret("spc", -ENOSPC);
140 static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow)
142 struct pxe_context *ctx = dev_get_priv(dev);
143 struct cmd_tbl cmdtp = {}; /* dummy */
144 struct extlinux_info info;
148 addr = map_to_sysmem(bflow->buf);
152 ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false,
153 bflow->subdir, false);
155 return log_msg_ret("ctx", -EINVAL);
157 ret = pxe_process(ctx, addr, false);
159 return log_msg_ret("bread", -EINVAL);
164 static int extlinux_bootmeth_pxe_bind(struct udevice *dev)
166 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
168 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
169 "PXE boot from a network device" : "PXE";
174 static struct bootmeth_ops extlinux_bootmeth_pxe_ops = {
175 .check = extlinux_pxe_check,
176 .read_bootflow = extlinux_pxe_read_bootflow,
177 .read_file = extlinux_pxe_read_file,
178 .boot = extlinux_pxe_boot,
181 static const struct udevice_id extlinux_bootmeth_pxe_ids[] = {
182 { .compatible = "u-boot,extlinux-pxe" },
186 U_BOOT_DRIVER(bootmeth_zpxe) = {
187 .name = "bootmeth_pxe",
188 .id = UCLASS_BOOTMETH,
189 .of_match = extlinux_bootmeth_pxe_ids,
190 .ops = &extlinux_bootmeth_pxe_ops,
191 .bind = extlinux_bootmeth_pxe_bind,
192 .priv_auto = sizeof(struct pxe_context),