blob: 05c6bece2c18bd327f267ca994e1866b1b15db8d [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,
26 char *file_addr, ulong *sizep)
Simon Glass83144612022-04-24 23:31:16 -060027{
Simon Glassb71d7f72023-05-10 16:34:46 -060028 struct extlinux_info *info = ctx->userdata;
Simon Glass83144612022-04-24 23:31:16 -060029 ulong addr;
30 int ret;
31
32 addr = simple_strtoul(file_addr, NULL, 16);
Simon Glass1dee4eb2023-07-26 21:01:25 -060033
34 /* Allow up to 1GB */
35 *sizep = 1 << 30;
Simon Glass83144612022-04-24 23:31:16 -060036 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
37 sizep);
38 if (ret)
39 return log_msg_ret("read", ret);
40
41 return 0;
42}
43
Simon Glassb71d7f72023-05-10 16:34:46 -060044static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
Simon Glass83144612022-04-24 23:31:16 -060045{
46 int ret;
47
48 /* This only works on network devices */
Simon Glass18c50402023-01-17 10:47:54 -070049 ret = bootflow_iter_check_net(iter);
Simon Glass83144612022-04-24 23:31:16 -060050 if (ret)
51 return log_msg_ret("net", ret);
52
Simon Glasse22fe922023-01-17 10:48:05 -070053 if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
54 return log_msg_ret("dhcp", -ENOTSUPP);
55
Simon Glass83144612022-04-24 23:31:16 -060056 return 0;
57}
58
Simon Glassb71d7f72023-05-10 16:34:46 -060059static int extlinux_pxe_read_bootflow(struct udevice *dev,
60 struct bootflow *bflow)
Simon Glass83144612022-04-24 23:31:16 -060061{
62 const char *addr_str;
63 char fname[200];
64 char *bootdir;
65 ulong addr;
66 ulong size;
67 char *buf;
68 int ret;
69
70 addr_str = env_get("pxefile_addr_r");
71 if (!addr_str)
72 return log_msg_ret("pxeb", -EPERM);
73 addr = simple_strtoul(addr_str, NULL, 16);
74
75 log_debug("calling pxe_get()\n");
Sean Edmondba802862023-04-11 10:48:47 -070076 ret = pxe_get(addr, &bootdir, &size, false);
Simon Glass83144612022-04-24 23:31:16 -060077 log_debug("pxe_get() returned %d\n", ret);
78 if (ret)
79 return log_msg_ret("pxeb", ret);
80 bflow->size = size;
81
82 /* Use the directory of the dhcp bootdir as our subdir, if provided */
83 if (bootdir) {
84 const char *last_slash;
85 int path_len;
86
87 last_slash = strrchr(bootdir, '/');
88 if (last_slash) {
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';
93 }
94 }
95 snprintf(fname, sizeof(fname), "%s%s",
Simon Glassb71d7f72023-05-10 16:34:46 -060096 bflow->subdir ? bflow->subdir : "", EXTLINUX_FNAME);
Simon Glass83144612022-04-24 23:31:16 -060097
98 bflow->fname = strdup(fname);
99 if (!bflow->fname)
100 return log_msg_ret("name", -ENOMEM);
101
102 bflow->state = BOOTFLOWST_READY;
103
104 /* Allocate the buffer, including the \0 byte added by get_pxe_file() */
105 buf = malloc(size + 1);
106 if (!buf)
107 return log_msg_ret("buf", -ENOMEM);
108 memcpy(buf, map_sysmem(addr, 0), size + 1);
109 bflow->buf = buf;
110
111 return 0;
112}
113
Simon Glassb71d7f72023-05-10 16:34:46 -0600114static int extlinux_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
115 const char *file_path, ulong addr,
116 ulong *sizep)
Simon Glass83144612022-04-24 23:31:16 -0600117{
118 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
119 struct pxe_context *ctx = dev_get_priv(dev);
120 char file_addr[17];
121 ulong size;
122 int ret;
123
124 sprintf(file_addr, "%lx", addr);
125 tftp_argv[1] = file_addr;
126 tftp_argv[2] = (void *)file_path;
127
128 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
129 return -ENOENT;
130 ret = pxe_get_file_size(&size);
131 if (ret)
132 return log_msg_ret("tftp", ret);
133 if (size > *sizep)
134 return log_msg_ret("spc", -ENOSPC);
135 *sizep = size;
136
137 return 0;
138}
139
Simon Glassb71d7f72023-05-10 16:34:46 -0600140static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glass83144612022-04-24 23:31:16 -0600141{
142 struct pxe_context *ctx = dev_get_priv(dev);
143 struct cmd_tbl cmdtp = {}; /* dummy */
Simon Glassb71d7f72023-05-10 16:34:46 -0600144 struct extlinux_info info;
Simon Glass83144612022-04-24 23:31:16 -0600145 ulong addr;
146 int ret;
147
148 addr = map_to_sysmem(bflow->buf);
149 info.dev = dev;
150 info.bflow = bflow;
151 info.cmdtp = &cmdtp;
Simon Glassb71d7f72023-05-10 16:34:46 -0600152 ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false,
Martyn Welch2c47aac2024-10-09 14:15:39 +0100153 bflow->subdir, false, false);
Simon Glass83144612022-04-24 23:31:16 -0600154 if (ret)
155 return log_msg_ret("ctx", -EINVAL);
156
157 ret = pxe_process(ctx, addr, false);
158 if (ret)
159 return log_msg_ret("bread", -EINVAL);
160
161 return 0;
162}
163
Simon Glassb71d7f72023-05-10 16:34:46 -0600164static int extlinux_bootmeth_pxe_bind(struct udevice *dev)
Simon Glass83144612022-04-24 23:31:16 -0600165{
166 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
167
168 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
169 "PXE boot from a network device" : "PXE";
170
171 return 0;
172}
173
Simon Glassb71d7f72023-05-10 16:34:46 -0600174static 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,
Simon Glass83144612022-04-24 23:31:16 -0600179};
180
Simon Glassb71d7f72023-05-10 16:34:46 -0600181static const struct udevice_id extlinux_bootmeth_pxe_ids[] = {
182 { .compatible = "u-boot,extlinux-pxe" },
Simon Glass83144612022-04-24 23:31:16 -0600183 { }
184};
185
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200186U_BOOT_DRIVER(bootmeth_zpxe) = {
Simon Glass83144612022-04-24 23:31:16 -0600187 .name = "bootmeth_pxe",
188 .id = UCLASS_BOOTMETH,
Simon Glassb71d7f72023-05-10 16:34:46 -0600189 .of_match = extlinux_bootmeth_pxe_ids,
190 .ops = &extlinux_bootmeth_pxe_ops,
191 .bind = extlinux_bootmeth_pxe_bind,
Simon Glass83144612022-04-24 23:31:16 -0600192 .priv_auto = sizeof(struct pxe_context),
193};