pxe: Return the file size from the getfile() function
[platform/kernel/u-boot.git] / cmd / pxe.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2010-2011 Calxeda, Inc.
4  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <fs.h>
10 #include <net.h>
11
12 #include "pxe_utils.h"
13
14 #ifdef CONFIG_CMD_NET
15 const char *pxe_default_paths[] = {
16 #ifdef CONFIG_SYS_SOC
17 #ifdef CONFIG_SYS_BOARD
18         "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
19 #endif
20         "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
21 #endif
22         "default-" CONFIG_SYS_ARCH,
23         "default",
24         NULL
25 };
26
27 static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
28                        char *file_addr, ulong *sizep)
29 {
30         char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
31         int ret;
32
33         tftp_argv[1] = file_addr;
34         tftp_argv[2] = (void *)file_path;
35
36         if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
37                 return -ENOENT;
38         ret = pxe_get_file_size(sizep);
39         if (ret)
40                 return log_msg_ret("tftp", ret);
41         ctx->pxe_file_size = *sizep;
42
43         return 1;
44 }
45
46 /*
47  * Looks for a pxe file with a name based on the pxeuuid environment variable.
48  *
49  * Returns 1 on success or < 0 on error.
50  */
51 static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
52 {
53         char *uuid_str;
54
55         uuid_str = from_env("pxeuuid");
56
57         if (!uuid_str)
58                 return -ENOENT;
59
60         return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r);
61 }
62
63 /*
64  * Looks for a pxe file with a name based on the 'ethaddr' environment
65  * variable.
66  *
67  * Returns 1 on success or < 0 on error.
68  */
69 static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
70 {
71         char mac_str[21];
72         int err;
73
74         err = format_mac_pxe(mac_str, sizeof(mac_str));
75
76         if (err < 0)
77                 return err;
78
79         return get_pxelinux_path(ctx, mac_str, pxefile_addr_r);
80 }
81
82 /*
83  * Looks for pxe files with names based on our IP address. See pxelinux
84  * documentation for details on what these file names look like.  We match
85  * that exactly.
86  *
87  * Returns 1 on success or < 0 on error.
88  */
89 static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r)
90 {
91         char ip_addr[9];
92         int mask_pos, err;
93
94         sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
95
96         for (mask_pos = 7; mask_pos >= 0;  mask_pos--) {
97                 err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r);
98
99                 if (err > 0)
100                         return err;
101
102                 ip_addr[mask_pos] = '\0';
103         }
104
105         return -ENOENT;
106 }
107 /*
108  * Entry point for the 'pxe get' command.
109  * This Follows pxelinux's rules to download a config file from a tftp server.
110  * The file is stored at the location given by the pxefile_addr_r environment
111  * variable, which must be set.
112  *
113  * UUID comes from pxeuuid env variable, if defined
114  * MAC addr comes from ethaddr env variable, if defined
115  * IP
116  *
117  * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
118  *
119  * Returns 0 on success or 1 on error.
120  */
121 static int
122 do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
123 {
124         char *pxefile_addr_str;
125         unsigned long pxefile_addr_r;
126         struct pxe_context ctx;
127         int err, i = 0;
128
129         if (argc != 1)
130                 return CMD_RET_USAGE;
131
132         pxefile_addr_str = from_env("pxefile_addr_r");
133
134         if (!pxefile_addr_str)
135                 return 1;
136
137         err = strict_strtoul(pxefile_addr_str, 16,
138                              (unsigned long *)&pxefile_addr_r);
139         if (err < 0)
140                 return 1;
141
142         if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
143                           env_get("bootfile"))) {
144                 printf("Out of memory\n");
145                 return CMD_RET_FAILURE;
146         }
147         /*
148          * Keep trying paths until we successfully get a file we're looking
149          * for.
150          */
151         if (pxe_uuid_path(&ctx, pxefile_addr_r) > 0 ||
152             pxe_mac_path(&ctx, pxefile_addr_r) > 0 ||
153             pxe_ipaddr_paths(&ctx, pxefile_addr_r) > 0) {
154                 printf("Config file found\n");
155                 pxe_destroy_ctx(&ctx);
156
157                 return 0;
158         }
159
160         while (pxe_default_paths[i]) {
161                 if (get_pxelinux_path(&ctx, pxe_default_paths[i],
162                                       pxefile_addr_r) > 0) {
163                         printf("Config file found\n");
164                         pxe_destroy_ctx(&ctx);
165                         return 0;
166                 }
167                 i++;
168         }
169
170         printf("Config file not found\n");
171         pxe_destroy_ctx(&ctx);
172
173         return 1;
174 }
175
176 /*
177  * Boots a system using a pxe file
178  *
179  * Returns 0 on success, 1 on error.
180  */
181 static int
182 do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
183 {
184         unsigned long pxefile_addr_r;
185         char *pxefile_addr_str;
186         struct pxe_context ctx;
187         int ret;
188
189         if (argc == 1) {
190                 pxefile_addr_str = from_env("pxefile_addr_r");
191                 if (!pxefile_addr_str)
192                         return 1;
193
194         } else if (argc == 2) {
195                 pxefile_addr_str = argv[1];
196         } else {
197                 return CMD_RET_USAGE;
198         }
199
200         if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
201                 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
202                 return 1;
203         }
204
205         if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
206                           env_get("bootfile"))) {
207                 printf("Out of memory\n");
208                 return CMD_RET_FAILURE;
209         }
210         ret = pxe_process(&ctx, pxefile_addr_r, false);
211         pxe_destroy_ctx(&ctx);
212         if (ret)
213                 return CMD_RET_FAILURE;
214
215         copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
216
217         return 0;
218 }
219
220 static struct cmd_tbl cmd_pxe_sub[] = {
221         U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
222         U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
223 };
224
225 static void __maybe_unused pxe_reloc(void)
226 {
227         static int relocated_pxe;
228
229         if (!relocated_pxe) {
230                 fixup_cmdtable(cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
231                 relocated_pxe = 1;
232         }
233 }
234
235 static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
236 {
237         struct cmd_tbl *cp;
238
239 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
240         pxe_reloc();
241 #endif
242
243         if (argc < 2)
244                 return CMD_RET_USAGE;
245
246         /* drop initial "pxe" arg */
247         argc--;
248         argv++;
249
250         cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
251
252         if (cp)
253                 return cp->cmd(cmdtp, flag, argc, argv);
254
255         return CMD_RET_USAGE;
256 }
257
258 U_BOOT_CMD(pxe, 3, 1, do_pxe,
259            "commands to get and boot from pxe files",
260            "get - try to retrieve a pxe file using tftp\n"
261            "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
262 );
263 #endif