blob: faa8d729b1513fb9f7d1c97456813c268ed5d96c [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>
Tom Rinic31301c2025-05-15 17:31:50 -060016#include <env.h>
Simon Glassb71d7f72023-05-10 16:34:46 -060017#include <extlinux.h>
Simon Glass83144612022-04-24 23:31:16 -060018#include <fs.h>
19#include <log.h>
20#include <malloc.h>
21#include <mapmem.h>
22#include <mmc.h>
23#include <net.h>
24#include <pxe_utils.h>
25
Simon Glassb71d7f72023-05-10 16:34:46 -060026static int extlinux_pxe_getfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa686ba62024-11-15 16:19:19 -070027 char *file_addr, enum bootflow_img_t type,
28 ulong *sizep)
Simon Glass83144612022-04-24 23:31:16 -060029{
Simon Glassb71d7f72023-05-10 16:34:46 -060030 struct extlinux_info *info = ctx->userdata;
Simon Glass83144612022-04-24 23:31:16 -060031 ulong addr;
32 int ret;
33
34 addr = simple_strtoul(file_addr, NULL, 16);
Simon Glass1dee4eb2023-07-26 21:01:25 -060035
36 /* Allow up to 1GB */
37 *sizep = 1 << 30;
Simon Glass83144612022-04-24 23:31:16 -060038 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
Simon Glassa686ba62024-11-15 16:19:19 -070039 type, sizep);
Simon Glass83144612022-04-24 23:31:16 -060040 if (ret)
41 return log_msg_ret("read", ret);
42
43 return 0;
44}
45
Simon Glassb71d7f72023-05-10 16:34:46 -060046static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
Simon Glass83144612022-04-24 23:31:16 -060047{
48 int ret;
49
50 /* This only works on network devices */
Simon Glass18c50402023-01-17 10:47:54 -070051 ret = bootflow_iter_check_net(iter);
Simon Glass83144612022-04-24 23:31:16 -060052 if (ret)
53 return log_msg_ret("net", ret);
54
Simon Glasse22fe922023-01-17 10:48:05 -070055 if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
56 return log_msg_ret("dhcp", -ENOTSUPP);
57
Simon Glass83144612022-04-24 23:31:16 -060058 return 0;
59}
60
Simon Glassb71d7f72023-05-10 16:34:46 -060061static int extlinux_pxe_read_bootflow(struct udevice *dev,
62 struct bootflow *bflow)
Simon Glass83144612022-04-24 23:31:16 -060063{
64 const char *addr_str;
65 char fname[200];
66 char *bootdir;
67 ulong addr;
68 ulong size;
69 char *buf;
70 int ret;
71
72 addr_str = env_get("pxefile_addr_r");
73 if (!addr_str)
74 return log_msg_ret("pxeb", -EPERM);
75 addr = simple_strtoul(addr_str, NULL, 16);
76
Heiko Stuebner33df5cd2025-04-02 23:50:25 +020077 ret = dhcp_run(addr, NULL, false);
78 if (ret)
79 return log_msg_ret("dhc", ret);
80
Simon Glass83144612022-04-24 23:31:16 -060081 log_debug("calling pxe_get()\n");
Sean Edmondba802862023-04-11 10:48:47 -070082 ret = pxe_get(addr, &bootdir, &size, false);
Simon Glass83144612022-04-24 23:31:16 -060083 log_debug("pxe_get() returned %d\n", ret);
84 if (ret)
85 return log_msg_ret("pxeb", ret);
86 bflow->size = size;
87
88 /* Use the directory of the dhcp bootdir as our subdir, if provided */
89 if (bootdir) {
90 const char *last_slash;
91 int path_len;
92
93 last_slash = strrchr(bootdir, '/');
94 if (last_slash) {
95 path_len = (last_slash - bootdir) + 1;
96 bflow->subdir = malloc(path_len + 1);
97 memcpy(bflow->subdir, bootdir, path_len);
98 bflow->subdir[path_len] = '\0';
99 }
100 }
101 snprintf(fname, sizeof(fname), "%s%s",
Simon Glassb71d7f72023-05-10 16:34:46 -0600102 bflow->subdir ? bflow->subdir : "", EXTLINUX_FNAME);
Simon Glass83144612022-04-24 23:31:16 -0600103
104 bflow->fname = strdup(fname);
105 if (!bflow->fname)
106 return log_msg_ret("name", -ENOMEM);
107
108 bflow->state = BOOTFLOWST_READY;
109
110 /* Allocate the buffer, including the \0 byte added by get_pxe_file() */
111 buf = malloc(size + 1);
112 if (!buf)
113 return log_msg_ret("buf", -ENOMEM);
114 memcpy(buf, map_sysmem(addr, 0), size + 1);
115 bflow->buf = buf;
116
117 return 0;
118}
119
Simon Glassb71d7f72023-05-10 16:34:46 -0600120static int extlinux_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
121 const char *file_path, ulong addr,
Simon Glassf39b5592024-11-15 16:19:17 -0700122 enum bootflow_img_t type, ulong *sizep)
Simon Glass83144612022-04-24 23:31:16 -0600123{
124 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
125 struct pxe_context *ctx = dev_get_priv(dev);
126 char file_addr[17];
127 ulong size;
128 int ret;
129
130 sprintf(file_addr, "%lx", addr);
131 tftp_argv[1] = file_addr;
132 tftp_argv[2] = (void *)file_path;
133
134 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
135 return -ENOENT;
136 ret = pxe_get_file_size(&size);
137 if (ret)
138 return log_msg_ret("tftp", ret);
139 if (size > *sizep)
140 return log_msg_ret("spc", -ENOSPC);
141 *sizep = size;
142
Simon Glass392eac42024-11-15 16:19:20 -0700143 if (!bootflow_img_add(bflow, file_path, type, addr, size))
144 return log_msg_ret("pxi", -ENOMEM);
145
Simon Glass83144612022-04-24 23:31:16 -0600146 return 0;
147}
148
Simon Glassb71d7f72023-05-10 16:34:46 -0600149static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass83144612022-04-24 23:31:16 -0600150{
151 struct pxe_context *ctx = dev_get_priv(dev);
152 struct cmd_tbl cmdtp = {}; /* dummy */
Simon Glassb71d7f72023-05-10 16:34:46 -0600153 struct extlinux_info info;
Simon Glass83144612022-04-24 23:31:16 -0600154 ulong addr;
155 int ret;
156
157 addr = map_to_sysmem(bflow->buf);
158 info.dev = dev;
159 info.bflow = bflow;
160 info.cmdtp = &cmdtp;
Simon Glassb71d7f72023-05-10 16:34:46 -0600161 ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false,
Martyn Welch2c47aac2024-10-09 14:15:39 +0100162 bflow->subdir, false, false);
Simon Glass83144612022-04-24 23:31:16 -0600163 if (ret)
164 return log_msg_ret("ctx", -EINVAL);
165
166 ret = pxe_process(ctx, addr, false);
167 if (ret)
168 return log_msg_ret("bread", -EINVAL);
169
170 return 0;
171}
172
Simon Glassb71d7f72023-05-10 16:34:46 -0600173static int extlinux_bootmeth_pxe_bind(struct udevice *dev)
Simon Glass83144612022-04-24 23:31:16 -0600174{
175 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
176
177 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
178 "PXE boot from a network device" : "PXE";
179
180 return 0;
181}
182
Simon Glassb71d7f72023-05-10 16:34:46 -0600183static struct bootmeth_ops extlinux_bootmeth_pxe_ops = {
184 .check = extlinux_pxe_check,
185 .read_bootflow = extlinux_pxe_read_bootflow,
186 .read_file = extlinux_pxe_read_file,
187 .boot = extlinux_pxe_boot,
Simon Glass83144612022-04-24 23:31:16 -0600188};
189
Simon Glassb71d7f72023-05-10 16:34:46 -0600190static const struct udevice_id extlinux_bootmeth_pxe_ids[] = {
191 { .compatible = "u-boot,extlinux-pxe" },
Simon Glass83144612022-04-24 23:31:16 -0600192 { }
193};
194
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200195U_BOOT_DRIVER(bootmeth_zpxe) = {
Simon Glass83144612022-04-24 23:31:16 -0600196 .name = "bootmeth_pxe",
197 .id = UCLASS_BOOTMETH,
Simon Glassb71d7f72023-05-10 16:34:46 -0600198 .of_match = extlinux_bootmeth_pxe_ids,
199 .ops = &extlinux_bootmeth_pxe_ops,
200 .bind = extlinux_bootmeth_pxe_bind,
Simon Glass83144612022-04-24 23:31:16 -0600201 .priv_auto = sizeof(struct pxe_context),
202};