1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for distro boot using PXE (network boot)
5 * Copyright 2021 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
24 #include <pxe_utils.h>
26 static int distro_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_check_net(iter);
49 return log_msg_ret("net", ret);
51 if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
52 return log_msg_ret("dhcp", -ENOTSUPP);
57 static int distro_pxe_read_bootflow(struct udevice *dev, struct bootflow *bflow)
67 addr_str = env_get("pxefile_addr_r");
69 return log_msg_ret("pxeb", -EPERM);
70 addr = simple_strtoul(addr_str, NULL, 16);
72 log_debug("calling pxe_get()\n");
73 ret = pxe_get(addr, &bootdir, &size);
74 log_debug("pxe_get() returned %d\n", ret);
76 return log_msg_ret("pxeb", ret);
79 /* Use the directory of the dhcp bootdir as our subdir, if provided */
81 const char *last_slash;
84 last_slash = strrchr(bootdir, '/');
86 path_len = (last_slash - bootdir) + 1;
87 bflow->subdir = malloc(path_len + 1);
88 memcpy(bflow->subdir, bootdir, path_len);
89 bflow->subdir[path_len] = '\0';
92 snprintf(fname, sizeof(fname), "%s%s",
93 bflow->subdir ? bflow->subdir : "", DISTRO_FNAME);
95 bflow->fname = strdup(fname);
97 return log_msg_ret("name", -ENOMEM);
99 bflow->state = BOOTFLOWST_READY;
101 /* Allocate the buffer, including the \0 byte added by get_pxe_file() */
102 buf = malloc(size + 1);
104 return log_msg_ret("buf", -ENOMEM);
105 memcpy(buf, map_sysmem(addr, 0), size + 1);
111 static int distro_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
112 const char *file_path, ulong addr, ulong *sizep)
114 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
115 struct pxe_context *ctx = dev_get_priv(dev);
120 sprintf(file_addr, "%lx", addr);
121 tftp_argv[1] = file_addr;
122 tftp_argv[2] = (void *)file_path;
124 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
126 ret = pxe_get_file_size(&size);
128 return log_msg_ret("tftp", ret);
130 return log_msg_ret("spc", -ENOSPC);
136 static int distro_pxe_boot(struct udevice *dev, struct bootflow *bflow)
138 struct pxe_context *ctx = dev_get_priv(dev);
139 struct cmd_tbl cmdtp = {}; /* dummy */
140 struct distro_info info;
144 addr = map_to_sysmem(bflow->buf);
148 ret = pxe_setup_ctx(ctx, &cmdtp, distro_pxe_getfile, &info, false,
151 return log_msg_ret("ctx", -EINVAL);
153 ret = pxe_process(ctx, addr, false);
155 return log_msg_ret("bread", -EINVAL);
160 static int distro_bootmeth_pxe_bind(struct udevice *dev)
162 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
164 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
165 "PXE boot from a network device" : "PXE";
170 static struct bootmeth_ops distro_bootmeth_pxe_ops = {
171 .check = distro_pxe_check,
172 .read_bootflow = distro_pxe_read_bootflow,
173 .read_file = distro_pxe_read_file,
174 .boot = distro_pxe_boot,
177 static const struct udevice_id distro_bootmeth_pxe_ids[] = {
178 { .compatible = "u-boot,distro-pxe" },
182 U_BOOT_DRIVER(bootmeth_pxe) = {
183 .name = "bootmeth_pxe",
184 .id = UCLASS_BOOTMETH,
185 .of_match = distro_bootmeth_pxe_ids,
186 .ops = &distro_bootmeth_pxe_ops,
187 .bind = distro_bootmeth_pxe_bind,
188 .priv_auto = sizeof(struct pxe_context),