blob: 6e5e0f99ea42a148d5f1512e2ec6b0d0ff0d9915 [file] [log] [blame]
Simon Glass83144612022-04-24 23:31:16 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glassb71d7f72023-05-10 16:34:46 -06003 * Bootmethod for extlinux boot using PXE (network boot)
Simon Glass83144612022-04-24 23:31:16 -06004 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEGORY UCLASS_BOOTSTD
10
Simon Glass83144612022-04-24 23:31:16 -060011#include <bootdev.h>
12#include <bootflow.h>
13#include <bootmeth.h>
14#include <command.h>
Simon Glass83144612022-04-24 23:31:16 -060015#include <dm.h>
Simon Glassb71d7f72023-05-10 16:34:46 -060016#include <extlinux.h>
Simon Glass83144612022-04-24 23:31:16 -060017#include <fs.h>
18#include <log.h>
19#include <malloc.h>
20#include <mapmem.h>
21#include <mmc.h>
22#include <net.h>
23#include <pxe_utils.h>
24
Simon Glassb71d7f72023-05-10 16:34:46 -060025static int extlinux_pxe_getfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa686ba62024-11-15 16:19:19 -070026 char *file_addr, enum bootflow_img_t type,
27 ulong *sizep)
Simon Glass83144612022-04-24 23:31:16 -060028{
Simon Glassb71d7f72023-05-10 16:34:46 -060029 struct extlinux_info *info = ctx->userdata;
Simon Glass83144612022-04-24 23:31:16 -060030 ulong addr;
31 int ret;
32
33 addr = simple_strtoul(file_addr, NULL, 16);
Simon Glass1dee4eb2023-07-26 21:01:25 -060034
35 /* Allow up to 1GB */
36 *sizep = 1 << 30;
Simon Glass83144612022-04-24 23:31:16 -060037 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
Simon Glassa686ba62024-11-15 16:19:19 -070038 type, sizep);
Simon Glass83144612022-04-24 23:31:16 -060039 if (ret)
40 return log_msg_ret("read", ret);
41
42 return 0;
43}
44
Simon Glassb71d7f72023-05-10 16:34:46 -060045static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
Simon Glass83144612022-04-24 23:31:16 -060046{
47 int ret;
48
49 /* This only works on network devices */
Simon Glass18c50402023-01-17 10:47:54 -070050 ret = bootflow_iter_check_net(iter);
Simon Glass83144612022-04-24 23:31:16 -060051 if (ret)
52 return log_msg_ret("net", ret);
53
Simon Glasse22fe922023-01-17 10:48:05 -070054 if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
55 return log_msg_ret("dhcp", -ENOTSUPP);
56
Simon Glass83144612022-04-24 23:31:16 -060057 return 0;
58}
59
Simon Glassb71d7f72023-05-10 16:34:46 -060060static int extlinux_pxe_read_bootflow(struct udevice *dev,
61 struct bootflow *bflow)
Simon Glass83144612022-04-24 23:31:16 -060062{
63 const char *addr_str;
64 char fname[200];
65 char *bootdir;
66 ulong addr;
67 ulong size;
68 char *buf;
69 int ret;
70
71 addr_str = env_get("pxefile_addr_r");
72 if (!addr_str)
73 return log_msg_ret("pxeb", -EPERM);
74 addr = simple_strtoul(addr_str, NULL, 16);
75
Heiko Stuebner33df5cd2025-04-02 23:50:25 +020076 ret = dhcp_run(addr, NULL, false);
77 if (ret)
78 return log_msg_ret("dhc", ret);
79
Simon Glass83144612022-04-24 23:31:16 -060080 log_debug("calling pxe_get()\n");
Sean Edmondba802862023-04-11 10:48:47 -070081 ret = pxe_get(addr, &bootdir, &size, false);
Simon Glass83144612022-04-24 23:31:16 -060082 log_debug("pxe_get() returned %d\n", ret);
83 if (ret)
84 return log_msg_ret("pxeb", ret);
85 bflow->size = size;
86
87 /* Use the directory of the dhcp bootdir as our subdir, if provided */
88 if (bootdir) {
89 const char *last_slash;
90 int path_len;
91
92 last_slash = strrchr(bootdir, '/');
93 if (last_slash) {
94 path_len = (last_slash - bootdir) + 1;
95 bflow->subdir = malloc(path_len + 1);
96 memcpy(bflow->subdir, bootdir, path_len);
97 bflow->subdir[path_len] = '\0';
98 }
99 }
100 snprintf(fname, sizeof(fname), "%s%s",
Simon Glassb71d7f72023-05-10 16:34:46 -0600101 bflow->subdir ? bflow->subdir : "", EXTLINUX_FNAME);
Simon Glass83144612022-04-24 23:31:16 -0600102
103 bflow->fname = strdup(fname);
104 if (!bflow->fname)
105 return log_msg_ret("name", -ENOMEM);
106
107 bflow->state = BOOTFLOWST_READY;
108
109 /* Allocate the buffer, including the \0 byte added by get_pxe_file() */
110 buf = malloc(size + 1);
111 if (!buf)
112 return log_msg_ret("buf", -ENOMEM);
113 memcpy(buf, map_sysmem(addr, 0), size + 1);
114 bflow->buf = buf;
115
116 return 0;
117}
118
Simon Glassb71d7f72023-05-10 16:34:46 -0600119static int extlinux_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
120 const char *file_path, ulong addr,
Simon Glassf39b5592024-11-15 16:19:17 -0700121 enum bootflow_img_t type, ulong *sizep)
Simon Glass83144612022-04-24 23:31:16 -0600122{
123 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
124 struct pxe_context *ctx = dev_get_priv(dev);
125 char file_addr[17];
126 ulong size;
127 int ret;
128
129 sprintf(file_addr, "%lx", addr);
130 tftp_argv[1] = file_addr;
131 tftp_argv[2] = (void *)file_path;
132
133 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
134 return -ENOENT;
135 ret = pxe_get_file_size(&size);
136 if (ret)
137 return log_msg_ret("tftp", ret);
138 if (size > *sizep)
139 return log_msg_ret("spc", -ENOSPC);
140 *sizep = size;
141
Simon Glass392eac42024-11-15 16:19:20 -0700142 if (!bootflow_img_add(bflow, file_path, type, addr, size))
143 return log_msg_ret("pxi", -ENOMEM);
144
Simon Glass83144612022-04-24 23:31:16 -0600145 return 0;
146}
147
Simon Glassb71d7f72023-05-10 16:34:46 -0600148static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass83144612022-04-24 23:31:16 -0600149{
150 struct pxe_context *ctx = dev_get_priv(dev);
151 struct cmd_tbl cmdtp = {}; /* dummy */
Simon Glassb71d7f72023-05-10 16:34:46 -0600152 struct extlinux_info info;
Simon Glass83144612022-04-24 23:31:16 -0600153 ulong addr;
154 int ret;
155
156 addr = map_to_sysmem(bflow->buf);
157 info.dev = dev;
158 info.bflow = bflow;
159 info.cmdtp = &cmdtp;
Simon Glassb71d7f72023-05-10 16:34:46 -0600160 ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false,
Martyn Welch2c47aac2024-10-09 14:15:39 +0100161 bflow->subdir, false, false);
Simon Glass83144612022-04-24 23:31:16 -0600162 if (ret)
163 return log_msg_ret("ctx", -EINVAL);
164
165 ret = pxe_process(ctx, addr, false);
166 if (ret)
167 return log_msg_ret("bread", -EINVAL);
168
169 return 0;
170}
171
Simon Glassb71d7f72023-05-10 16:34:46 -0600172static int extlinux_bootmeth_pxe_bind(struct udevice *dev)
Simon Glass83144612022-04-24 23:31:16 -0600173{
174 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
175
176 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
177 "PXE boot from a network device" : "PXE";
178
179 return 0;
180}
181
Simon Glassb71d7f72023-05-10 16:34:46 -0600182static struct bootmeth_ops extlinux_bootmeth_pxe_ops = {
183 .check = extlinux_pxe_check,
184 .read_bootflow = extlinux_pxe_read_bootflow,
185 .read_file = extlinux_pxe_read_file,
186 .boot = extlinux_pxe_boot,
Simon Glass83144612022-04-24 23:31:16 -0600187};
188
Simon Glassb71d7f72023-05-10 16:34:46 -0600189static const struct udevice_id extlinux_bootmeth_pxe_ids[] = {
190 { .compatible = "u-boot,extlinux-pxe" },
Simon Glass83144612022-04-24 23:31:16 -0600191 { }
192};
193
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200194U_BOOT_DRIVER(bootmeth_zpxe) = {
Simon Glass83144612022-04-24 23:31:16 -0600195 .name = "bootmeth_pxe",
196 .id = UCLASS_BOOTMETH,
Simon Glassb71d7f72023-05-10 16:34:46 -0600197 .of_match = extlinux_bootmeth_pxe_ids,
198 .ops = &extlinux_bootmeth_pxe_ops,
199 .bind = extlinux_bootmeth_pxe_bind,
Simon Glass83144612022-04-24 23:31:16 -0600200 .priv_auto = sizeof(struct pxe_context),
201};