1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for distro boot using PXE (network boot)
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
9 #define LOG_CATEGORY UCLASS_BOOTSTD
24 #include <pxe_utils.h>
26 static int disto_pxe_getfile(struct pxe_context *ctx, const char *file_path,
27 char *file_addr, ulong *sizep)
29 struct distro_info *info = ctx->userdata;
33 addr = simple_strtoul(file_addr, NULL, 16);
34 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
37 return log_msg_ret("read", ret);
42 static int distro_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
46 /* This only works on network devices */
47 ret = bootflow_iter_uses_network(iter);
49 return log_msg_ret("net", ret);
54 static int distro_pxe_read_bootflow(struct udevice *dev, struct bootflow *bflow)
64 addr_str = env_get("pxefile_addr_r");
66 return log_msg_ret("pxeb", -EPERM);
67 addr = simple_strtoul(addr_str, NULL, 16);
69 log_debug("calling pxe_get()\n");
70 ret = pxe_get(addr, &bootdir, &size);
71 log_debug("pxe_get() returned %d\n", ret);
73 return log_msg_ret("pxeb", ret);
76 /* Use the directory of the dhcp bootdir as our subdir, if provided */
78 const char *last_slash;
81 last_slash = strrchr(bootdir, '/');
83 path_len = (last_slash - bootdir) + 1;
84 bflow->subdir = malloc(path_len + 1);
85 memcpy(bflow->subdir, bootdir, path_len);
86 bflow->subdir[path_len] = '\0';
89 snprintf(fname, sizeof(fname), "%s%s",
90 bflow->subdir ? bflow->subdir : "", DISTRO_FNAME);
92 bflow->fname = strdup(fname);
94 return log_msg_ret("name", -ENOMEM);
96 bflow->state = BOOTFLOWST_READY;
98 /* Allocate the buffer, including the \0 byte added by get_pxe_file() */
99 buf = malloc(size + 1);
101 return log_msg_ret("buf", -ENOMEM);
102 memcpy(buf, map_sysmem(addr, 0), size + 1);
108 static int distro_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
109 const char *file_path, ulong addr, ulong *sizep)
111 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
112 struct pxe_context *ctx = dev_get_priv(dev);
117 sprintf(file_addr, "%lx", addr);
118 tftp_argv[1] = file_addr;
119 tftp_argv[2] = (void *)file_path;
121 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
123 ret = pxe_get_file_size(&size);
125 return log_msg_ret("tftp", ret);
127 return log_msg_ret("spc", -ENOSPC);
133 static int distro_pxe_boot(struct udevice *dev, struct bootflow *bflow)
135 struct pxe_context *ctx = dev_get_priv(dev);
136 struct cmd_tbl cmdtp = {}; /* dummy */
137 struct distro_info info;
141 addr = map_to_sysmem(bflow->buf);
145 ret = pxe_setup_ctx(ctx, &cmdtp, disto_pxe_getfile, &info, false,
148 return log_msg_ret("ctx", -EINVAL);
150 ret = pxe_process(ctx, addr, false);
152 return log_msg_ret("bread", -EINVAL);
157 static int distro_bootmeth_pxe_bind(struct udevice *dev)
159 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
161 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
162 "PXE boot from a network device" : "PXE";
167 static struct bootmeth_ops distro_bootmeth_pxe_ops = {
168 .check = distro_pxe_check,
169 .read_bootflow = distro_pxe_read_bootflow,
170 .read_file = distro_pxe_read_file,
171 .boot = distro_pxe_boot,
174 static const struct udevice_id distro_bootmeth_pxe_ids[] = {
175 { .compatible = "u-boot,distro-pxe" },
179 U_BOOT_DRIVER(bootmeth_pxe) = {
180 .name = "bootmeth_pxe",
181 .id = UCLASS_BOOTMETH,
182 .of_match = distro_bootmeth_pxe_ids,
183 .ops = &distro_bootmeth_pxe_ops,
184 .bind = distro_bootmeth_pxe_bind,
185 .priv_auto = sizeof(struct pxe_context),