1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Bootmethod for extlinux 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 <bootdev.h>
12 #include <bootflow.h>
13 #include <bootmeth.h>
14 #include <command.h>
15 #include <dm.h>
16 #include <env.h>
17 #include <extlinux.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
extlinux_pxe_getfile(struct pxe_context * ctx,const char * file_path,char * file_addr,enum bootflow_img_t type,ulong * sizep)26 static int extlinux_pxe_getfile(struct pxe_context *ctx, const char *file_path,
27 char *file_addr, enum bootflow_img_t type,
28 ulong *sizep)
29 {
30 struct extlinux_info *info = ctx->userdata;
31 ulong addr;
32 int ret;
33
34 addr = simple_strtoul(file_addr, NULL, 16);
35
36 /* Allow up to 1GB */
37 *sizep = 1 << 30;
38 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
39 type, sizep);
40 if (ret)
41 return log_msg_ret("read", ret);
42
43 return 0;
44 }
45
extlinux_pxe_check(struct udevice * dev,struct bootflow_iter * iter)46 static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
47 {
48 int ret;
49
50 /* This only works on network devices */
51 ret = bootflow_iter_check_net(iter);
52 if (ret)
53 return log_msg_ret("net", ret);
54
55 if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
56 return log_msg_ret("dhcp", -ENOTSUPP);
57
58 return 0;
59 }
60
extlinux_pxe_read_bootflow(struct udevice * dev,struct bootflow * bflow)61 static int extlinux_pxe_read_bootflow(struct udevice *dev,
62 struct bootflow *bflow)
63 {
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
77 ret = dhcp_run(addr, NULL, false);
78 if (ret)
79 return log_msg_ret("dhc", ret);
80
81 log_debug("calling pxe_get()\n");
82 ret = pxe_get(addr, &bootdir, &size, false);
83 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",
102 bflow->subdir ? bflow->subdir : "", EXTLINUX_FNAME);
103
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
extlinux_pxe_read_file(struct udevice * dev,struct bootflow * bflow,const char * file_path,ulong addr,enum bootflow_img_t type,ulong * sizep)120 static int extlinux_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
121 const char *file_path, ulong addr,
122 enum bootflow_img_t type, ulong *sizep)
123 {
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
143 if (!bootflow_img_add(bflow, file_path, type, addr, size))
144 return log_msg_ret("pxi", -ENOMEM);
145
146 return 0;
147 }
148
extlinux_pxe_boot(struct udevice * dev,struct bootflow * bflow)149 static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow)
150 {
151 struct pxe_context *ctx = dev_get_priv(dev);
152 struct cmd_tbl cmdtp = {}; /* dummy */
153 struct extlinux_info info;
154 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;
161 ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false,
162 bflow->subdir, false, false);
163 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
extlinux_bootmeth_pxe_bind(struct udevice * dev)173 static int extlinux_bootmeth_pxe_bind(struct udevice *dev)
174 {
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
183 static 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,
188 };
189
190 static const struct udevice_id extlinux_bootmeth_pxe_ids[] = {
191 { .compatible = "u-boot,extlinux-pxe" },
192 { }
193 };
194
195 U_BOOT_DRIVER(bootmeth_zpxe) = {
196 .name = "bootmeth_pxe",
197 .id = UCLASS_BOOTMETH,
198 .of_match = extlinux_bootmeth_pxe_ids,
199 .ops = &extlinux_bootmeth_pxe_ops,
200 .bind = extlinux_bootmeth_pxe_bind,
201 .priv_auto = sizeof(struct pxe_context),
202 };
203