]> Git Repo - J-u-boot.git/blob - boot/bootmeth_pxe.c
Merge patch series "dwc3: gadget: properly fix cache operations"
[J-u-boot.git] / boot / bootmeth_pxe.c
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 <[email protected]>
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 <extlinux.h>
17 #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
25 static int extlinux_pxe_getfile(struct pxe_context *ctx, const char *file_path,
26                                 char *file_addr, ulong *sizep)
27 {
28         struct extlinux_info *info = ctx->userdata;
29         ulong addr;
30         int ret;
31
32         addr = simple_strtoul(file_addr, NULL, 16);
33
34         /* Allow up to 1GB */
35         *sizep = 1 << 30;
36         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
44 static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
45 {
46         int ret;
47
48         /* This only works on network devices */
49         ret = bootflow_iter_check_net(iter);
50         if (ret)
51                 return log_msg_ret("net", ret);
52
53         if (iter->method_flags & BOOTFLOW_METHF_DHCP_ONLY)
54                 return log_msg_ret("dhcp", -ENOTSUPP);
55
56         return 0;
57 }
58
59 static int extlinux_pxe_read_bootflow(struct udevice *dev,
60                                       struct bootflow *bflow)
61 {
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");
76         ret = pxe_get(addr, &bootdir, &size, false);
77         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",
96                  bflow->subdir ? bflow->subdir : "", EXTLINUX_FNAME);
97
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
114 static int extlinux_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
115                                   const char *file_path, ulong addr,
116                                   ulong *sizep)
117 {
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
140 static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow)
141 {
142         struct pxe_context *ctx = dev_get_priv(dev);
143         struct cmd_tbl cmdtp = {};      /* dummy */
144         struct extlinux_info info;
145         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;
152         ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false,
153                             bflow->subdir, false, false);
154         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
164 static int extlinux_bootmeth_pxe_bind(struct udevice *dev)
165 {
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
174 static 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,
179 };
180
181 static const struct udevice_id extlinux_bootmeth_pxe_ids[] = {
182         { .compatible = "u-boot,extlinux-pxe" },
183         { }
184 };
185
186 U_BOOT_DRIVER(bootmeth_zpxe) = {
187         .name           = "bootmeth_pxe",
188         .id             = UCLASS_BOOTMETH,
189         .of_match       = extlinux_bootmeth_pxe_ids,
190         .ops            = &extlinux_bootmeth_pxe_ops,
191         .bind           = extlinux_bootmeth_pxe_bind,
192         .priv_auto      = sizeof(struct pxe_context),
193 };
This page took 0.035925 seconds and 4 git commands to generate.