blob: 5a8af2bbd0b111a5904abfbed010d7d43012f0c5 [file] [log] [blame]
Simon Glass83144612022-04-24 23:31:16 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Bootmethod for distro boot using PXE (network boot)
4 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEGORY UCLASS_BOOTSTD
10
11#include <common.h>
12#include <bootdev.h>
13#include <bootflow.h>
14#include <bootmeth.h>
15#include <command.h>
16#include <distro.h>
17#include <dm.h>
18#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
Dario Binacchi3c9c6d72022-08-26 15:15:41 +020026static int distro_pxe_getfile(struct pxe_context *ctx, const char *file_path,
27 char *file_addr, ulong *sizep)
Simon Glass83144612022-04-24 23:31:16 -060028{
29 struct distro_info *info = ctx->userdata;
30 ulong addr;
31 int ret;
32
33 addr = simple_strtoul(file_addr, NULL, 16);
34 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
35 sizep);
36 if (ret)
37 return log_msg_ret("read", ret);
38
39 return 0;
40}
41
42static int distro_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
43{
44 int ret;
45
46 /* This only works on network devices */
Simon Glass18c50402023-01-17 10:47:54 -070047 ret = bootflow_iter_check_net(iter);
Simon Glass83144612022-04-24 23:31:16 -060048 if (ret)
49 return log_msg_ret("net", ret);
50
Simon Glasse22fe922023-01-17 10:48:05 -070051 if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
52 return log_msg_ret("dhcp", -ENOTSUPP);
53
Simon Glass83144612022-04-24 23:31:16 -060054 return 0;
55}
56
57static int distro_pxe_read_bootflow(struct udevice *dev, struct bootflow *bflow)
58{
59 const char *addr_str;
60 char fname[200];
61 char *bootdir;
62 ulong addr;
63 ulong size;
64 char *buf;
65 int ret;
66
67 addr_str = env_get("pxefile_addr_r");
68 if (!addr_str)
69 return log_msg_ret("pxeb", -EPERM);
70 addr = simple_strtoul(addr_str, NULL, 16);
71
72 log_debug("calling pxe_get()\n");
Sean Edmondba802862023-04-11 10:48:47 -070073 ret = pxe_get(addr, &bootdir, &size, false);
Simon Glass83144612022-04-24 23:31:16 -060074 log_debug("pxe_get() returned %d\n", ret);
75 if (ret)
76 return log_msg_ret("pxeb", ret);
77 bflow->size = size;
78
79 /* Use the directory of the dhcp bootdir as our subdir, if provided */
80 if (bootdir) {
81 const char *last_slash;
82 int path_len;
83
84 last_slash = strrchr(bootdir, '/');
85 if (last_slash) {
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';
90 }
91 }
92 snprintf(fname, sizeof(fname), "%s%s",
93 bflow->subdir ? bflow->subdir : "", DISTRO_FNAME);
94
95 bflow->fname = strdup(fname);
96 if (!bflow->fname)
97 return log_msg_ret("name", -ENOMEM);
98
99 bflow->state = BOOTFLOWST_READY;
100
101 /* Allocate the buffer, including the \0 byte added by get_pxe_file() */
102 buf = malloc(size + 1);
103 if (!buf)
104 return log_msg_ret("buf", -ENOMEM);
105 memcpy(buf, map_sysmem(addr, 0), size + 1);
106 bflow->buf = buf;
107
108 return 0;
109}
110
111static int distro_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
112 const char *file_path, ulong addr, ulong *sizep)
113{
114 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
115 struct pxe_context *ctx = dev_get_priv(dev);
116 char file_addr[17];
117 ulong size;
118 int ret;
119
120 sprintf(file_addr, "%lx", addr);
121 tftp_argv[1] = file_addr;
122 tftp_argv[2] = (void *)file_path;
123
124 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
125 return -ENOENT;
126 ret = pxe_get_file_size(&size);
127 if (ret)
128 return log_msg_ret("tftp", ret);
129 if (size > *sizep)
130 return log_msg_ret("spc", -ENOSPC);
131 *sizep = size;
132
133 return 0;
134}
135
136static int distro_pxe_boot(struct udevice *dev, struct bootflow *bflow)
137{
138 struct pxe_context *ctx = dev_get_priv(dev);
139 struct cmd_tbl cmdtp = {}; /* dummy */
140 struct distro_info info;
141 ulong addr;
142 int ret;
143
144 addr = map_to_sysmem(bflow->buf);
145 info.dev = dev;
146 info.bflow = bflow;
147 info.cmdtp = &cmdtp;
Dario Binacchi3c9c6d72022-08-26 15:15:41 +0200148 ret = pxe_setup_ctx(ctx, &cmdtp, distro_pxe_getfile, &info, false,
Sean Edmondba802862023-04-11 10:48:47 -0700149 bflow->subdir, false);
Simon Glass83144612022-04-24 23:31:16 -0600150 if (ret)
151 return log_msg_ret("ctx", -EINVAL);
152
153 ret = pxe_process(ctx, addr, false);
154 if (ret)
155 return log_msg_ret("bread", -EINVAL);
156
157 return 0;
158}
159
160static int distro_bootmeth_pxe_bind(struct udevice *dev)
161{
162 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
163
164 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
165 "PXE boot from a network device" : "PXE";
166
167 return 0;
168}
169
170static 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,
175};
176
177static const struct udevice_id distro_bootmeth_pxe_ids[] = {
178 { .compatible = "u-boot,distro-pxe" },
179 { }
180};
181
182U_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),
189};