blob: 768e50aba610ed6a60336597b6b85f0e8d3abd94 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0e3a5932011-08-31 10:37:30 -05002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wua1d715b2014-07-31 17:39:59 -07004 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs0e3a5932011-08-31 10:37:30 -05005 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006
Jason Hobbs0e3a5932011-08-31 10:37:30 -05007#include <common.h>
8#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06009#include <env.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050010#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050011#include <mapmem.h>
Patrice Chotardd5074912019-04-11 11:13:13 +020012#include <lcd.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050013#include <linux/string.h>
14#include <linux/ctype.h>
15#include <errno.h>
16#include <linux/list.h>
Dennis Gilmorecd90dc82014-02-04 05:25:46 -060017#include <fs.h>
Patrice Chotardd5074912019-04-11 11:13:13 +020018#include <splash.h>
Sjoerd Simons45af07c2015-04-13 22:54:25 +020019#include <asm/io.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050020
21#include "menu.h"
Hans de Goede55adc412014-08-06 09:37:39 +020022#include "cli.h"
Jason Hobbs0e3a5932011-08-31 10:37:30 -050023
24#define MAX_TFTP_PATH_LEN 127
25
Rob Herringe26e8d62012-12-02 21:00:28 -060026const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050027#ifdef CONFIG_SYS_SOC
Marek BehĂșn016ed132019-05-02 15:29:12 +020028#ifdef CONFIG_SYS_BOARD
29 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
30#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060031 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050032#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060033 "default-" CONFIG_SYS_ARCH,
34 "default",
35 NULL
36};
37
Rob Herring759456e2013-10-18 13:04:42 -050038static bool is_pxe;
39
Jason Hobbs0e3a5932011-08-31 10:37:30 -050040/*
Simon Glass64b723f2017-08-03 12:22:12 -060041 * Like env_get, but prints an error if envvar isn't defined in the
42 * environment. It always returns what env_get does, so it can be used in
43 * place of env_get without changing error handling otherwise.
Jason Hobbs0e3a5932011-08-31 10:37:30 -050044 */
Rob Herring824901c2012-12-02 21:00:21 -060045static char *from_env(const char *envvar)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050046{
47 char *ret;
48
Simon Glass64b723f2017-08-03 12:22:12 -060049 ret = env_get(envvar);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050050
51 if (!ret)
52 printf("missing environment variable: %s\n", envvar);
53
54 return ret;
55}
56
Stephen Warren857291b2014-02-05 20:49:20 -070057#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-08-31 10:37:30 -050058/*
59 * Convert an ethaddr from the environment to the format used by pxelinux
60 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
61 * the beginning of the ethernet address to indicate a hardware type of
62 * Ethernet. Also converts uppercase hex characters into lowercase, to match
63 * pxelinux's behavior.
64 *
65 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
66 * environment, or some other value < 0 on error.
67 */
68static int format_mac_pxe(char *outbuf, size_t outbuf_len)
69{
Rob Herringdd248b32012-12-02 21:00:20 -060070 uchar ethaddr[6];
Jason Hobbs0e3a5932011-08-31 10:37:30 -050071
Rob Herringdd248b32012-12-02 21:00:20 -060072 if (outbuf_len < 21) {
David Feng4ec50292013-12-14 11:47:30 +080073 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050074
75 return -EINVAL;
76 }
77
Simon Glass399a9ce2017-08-03 12:22:14 -060078 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
Rob Herringdd248b32012-12-02 21:00:20 -060079 return -ENOENT;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050080
Rob Herringdd248b32012-12-02 21:00:20 -060081 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
82 ethaddr[0], ethaddr[1], ethaddr[2],
83 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050084
85 return 1;
86}
Stephen Warren857291b2014-02-05 20:49:20 -070087#endif
Jason Hobbs0e3a5932011-08-31 10:37:30 -050088
89/*
90 * Returns the directory the file specified in the bootfile env variable is
91 * in. If bootfile isn't defined in the environment, return NULL, which should
92 * be interpreted as "don't prepend anything to paths".
93 */
Rob Herringdc410cd2012-03-28 05:51:36 +000094static int get_bootfile_path(const char *file_path, char *bootfile_path,
95 size_t bootfile_path_size)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050096{
97 char *bootfile, *last_slash;
Rob Herringdc410cd2012-03-28 05:51:36 +000098 size_t path_len = 0;
99
Rob Herring759456e2013-10-18 13:04:42 -0500100 /* Only syslinux allows absolute paths */
101 if (file_path[0] == '/' && !is_pxe)
Rob Herringdc410cd2012-03-28 05:51:36 +0000102 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500103
104 bootfile = from_env("bootfile");
105
Rob Herringdc410cd2012-03-28 05:51:36 +0000106 if (!bootfile)
107 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500108
109 last_slash = strrchr(bootfile, '/');
110
Rob Herringdc410cd2012-03-28 05:51:36 +0000111 if (last_slash == NULL)
112 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500113
114 path_len = (last_slash - bootfile) + 1;
115
116 if (bootfile_path_size < path_len) {
David Feng4ec50292013-12-14 11:47:30 +0800117 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500118 bootfile_path_size, path_len);
119
120 return -1;
121 }
122
123 strncpy(bootfile_path, bootfile, path_len);
124
Rob Herringdc410cd2012-03-28 05:51:36 +0000125 ret:
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500126 bootfile_path[path_len] = '\0';
127
128 return 1;
129}
130
Steven Falco7de0fa82013-10-07 09:51:48 -0400131static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herringeee675f2012-05-25 10:47:39 +0000132
Stephen Warren857291b2014-02-05 20:49:20 -0700133#ifdef CONFIG_CMD_NET
Steven Falco7de0fa82013-10-07 09:51:48 -0400134static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000135{
136 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
137
138 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600139 tftp_argv[2] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000140
Steven Falco7de0fa82013-10-07 09:51:48 -0400141 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000142 return -ENOENT;
143
144 return 1;
145}
Stephen Warren857291b2014-02-05 20:49:20 -0700146#endif
Rob Herringeee675f2012-05-25 10:47:39 +0000147
148static char *fs_argv[5];
149
Steven Falco7de0fa82013-10-07 09:51:48 -0400150static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000151{
152#ifdef CONFIG_CMD_EXT2
153 fs_argv[0] = "ext2load";
154 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600155 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000156
Steven Falco7de0fa82013-10-07 09:51:48 -0400157 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000158 return 1;
159#endif
160 return -ENOENT;
161}
162
Steven Falco7de0fa82013-10-07 09:51:48 -0400163static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000164{
165#ifdef CONFIG_CMD_FAT
166 fs_argv[0] = "fatload";
167 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600168 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000169
Steven Falco7de0fa82013-10-07 09:51:48 -0400170 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000171 return 1;
172#endif
173 return -ENOENT;
174}
175
Dennis Gilmorecd90dc82014-02-04 05:25:46 -0600176static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
177{
178#ifdef CONFIG_CMD_FS_GENERIC
179 fs_argv[0] = "load";
180 fs_argv[3] = file_addr;
181 fs_argv[4] = (void *)file_path;
182
183 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
184 return 1;
185#endif
186 return -ENOENT;
187}
188
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500189/*
190 * As in pxelinux, paths to files referenced from files we retrieve are
191 * relative to the location of bootfile. get_relfile takes such a path and
192 * joins it with the bootfile path to get the full path to the target file. If
193 * the bootfile path is NULL, we use file_path as is.
194 *
195 * Returns 1 for success, or < 0 on error.
196 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200197static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
198 unsigned long file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500199{
200 size_t path_len;
201 char relfile[MAX_TFTP_PATH_LEN+1];
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200202 char addr_buf[18];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500203 int err;
204
Rob Herringdc410cd2012-03-28 05:51:36 +0000205 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500206
207 if (err < 0)
208 return err;
209
210 path_len = strlen(file_path);
211 path_len += strlen(relfile);
212
213 if (path_len > MAX_TFTP_PATH_LEN) {
214 printf("Base path too long (%s%s)\n",
215 relfile,
216 file_path);
217
218 return -ENAMETOOLONG;
219 }
220
221 strcat(relfile, file_path);
222
223 printf("Retrieving file: %s\n", relfile);
224
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200225 sprintf(addr_buf, "%lx", file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500226
Steven Falco7de0fa82013-10-07 09:51:48 -0400227 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500228}
229
230/*
231 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
232 * 'bootfile' was specified in the environment, the path to bootfile will be
233 * prepended to 'file_path' and the resulting path will be used.
234 *
235 * Returns 1 on success, or < 0 for error.
236 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200237static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
238 unsigned long file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500239{
240 unsigned long config_file_size;
241 char *tftp_filesize;
242 int err;
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200243 char *buf;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500244
Steven Falco7de0fa82013-10-07 09:51:48 -0400245 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500246
247 if (err < 0)
248 return err;
249
250 /*
251 * the file comes without a NUL byte at the end, so find out its size
252 * and add the NUL byte.
253 */
254 tftp_filesize = from_env("filesize");
255
256 if (!tftp_filesize)
257 return -ENOENT;
258
259 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
260 return -EINVAL;
261
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200262 buf = map_sysmem(file_addr + config_file_size, 1);
263 *buf = '\0';
264 unmap_sysmem(buf);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500265
266 return 1;
267}
268
Stephen Warren857291b2014-02-05 20:49:20 -0700269#ifdef CONFIG_CMD_NET
270
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500271#define PXELINUX_DIR "pxelinux.cfg/"
272
273/*
274 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
275 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
276 * from the bootfile path, as described above.
277 *
278 * Returns 1 on success or < 0 on error.
279 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200280static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
281 unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500282{
283 size_t base_len = strlen(PXELINUX_DIR);
284 char path[MAX_TFTP_PATH_LEN+1];
285
286 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
287 printf("path (%s%s) too long, skipping\n",
288 PXELINUX_DIR, file);
289 return -ENAMETOOLONG;
290 }
291
292 sprintf(path, PXELINUX_DIR "%s", file);
293
Steven Falco7de0fa82013-10-07 09:51:48 -0400294 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500295}
296
297/*
298 * Looks for a pxe file with a name based on the pxeuuid environment variable.
299 *
300 * Returns 1 on success or < 0 on error.
301 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200302static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500303{
304 char *uuid_str;
305
306 uuid_str = from_env("pxeuuid");
307
308 if (!uuid_str)
309 return -ENOENT;
310
Steven Falco7de0fa82013-10-07 09:51:48 -0400311 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500312}
313
314/*
315 * Looks for a pxe file with a name based on the 'ethaddr' environment
316 * variable.
317 *
318 * Returns 1 on success or < 0 on error.
319 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200320static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500321{
322 char mac_str[21];
323 int err;
324
325 err = format_mac_pxe(mac_str, sizeof(mac_str));
326
327 if (err < 0)
328 return err;
329
Steven Falco7de0fa82013-10-07 09:51:48 -0400330 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500331}
332
333/*
334 * Looks for pxe files with names based on our IP address. See pxelinux
335 * documentation for details on what these file names look like. We match
336 * that exactly.
337 *
338 * Returns 1 on success or < 0 on error.
339 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200340static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500341{
342 char ip_addr[9];
343 int mask_pos, err;
344
Joe Hershberger5874dec2015-04-08 01:41:01 -0500345 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500346
347 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400348 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500349
350 if (err > 0)
351 return err;
352
353 ip_addr[mask_pos] = '\0';
354 }
355
356 return -ENOENT;
357}
358
359/*
360 * Entry point for the 'pxe get' command.
361 * This Follows pxelinux's rules to download a config file from a tftp server.
362 * The file is stored at the location given by the pxefile_addr_r environment
363 * variable, which must be set.
364 *
365 * UUID comes from pxeuuid env variable, if defined
366 * MAC addr comes from ethaddr env variable, if defined
367 * IP
368 *
369 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
370 *
371 * Returns 0 on success or 1 on error.
372 */
373static int
374do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
375{
376 char *pxefile_addr_str;
Jason Hobbs9135c792012-03-05 08:12:28 +0000377 unsigned long pxefile_addr_r;
Rob Herringe26e8d62012-12-02 21:00:28 -0600378 int err, i = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500379
Rob Herringeee675f2012-05-25 10:47:39 +0000380 do_getfile = do_get_tftp;
381
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500382 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000383 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500384
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500385 pxefile_addr_str = from_env("pxefile_addr_r");
386
387 if (!pxefile_addr_str)
388 return 1;
389
390 err = strict_strtoul(pxefile_addr_str, 16,
391 (unsigned long *)&pxefile_addr_r);
392 if (err < 0)
393 return 1;
394
395 /*
396 * Keep trying paths until we successfully get a file we're looking
397 * for.
398 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200399 if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
400 pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
401 pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500402 printf("Config file found\n");
403
404 return 0;
405 }
406
Rob Herringe26e8d62012-12-02 21:00:28 -0600407 while (pxe_default_paths[i]) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400408 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200409 pxefile_addr_r) > 0) {
Rob Herringe26e8d62012-12-02 21:00:28 -0600410 printf("Config file found\n");
411 return 0;
412 }
413 i++;
414 }
415
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500416 printf("Config file not found\n");
417
418 return 1;
419}
Stephen Warren857291b2014-02-05 20:49:20 -0700420#endif
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500421
422/*
423 * Wrapper to make it easier to store the file at file_path in the location
424 * specified by envaddr_name. file_path will be joined to the bootfile path,
425 * if any is specified.
426 *
427 * Returns 1 on success or < 0 on error.
428 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400429static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500430{
Jason Hobbs9135c792012-03-05 08:12:28 +0000431 unsigned long file_addr;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500432 char *envaddr;
433
434 envaddr = from_env(envaddr_name);
435
436 if (!envaddr)
437 return -ENOENT;
438
Jason Hobbs9135c792012-03-05 08:12:28 +0000439 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500440 return -EINVAL;
441
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200442 return get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500443}
444
445/*
446 * A note on the pxe file parser.
447 *
448 * We're parsing files that use syslinux grammar, which has a few quirks.
449 * String literals must be recognized based on context - there is no
450 * quoting or escaping support. There's also nothing to explicitly indicate
451 * when a label section completes. We deal with that by ending a label
452 * section whenever we see a line that doesn't include.
453 *
454 * As with the syslinux family, this same file format could be reused in the
455 * future for non pxe purposes. The only action it takes during parsing that
456 * would throw this off is handling of include files. It assumes we're using
457 * pxe, and does a tftp download of a file listed as an include file in the
458 * middle of the parsing operation. That could be handled by refactoring it to
459 * take a 'include file getter' function.
460 */
461
462/*
463 * Describes a single label given in a pxe file.
464 *
465 * Create these with the 'label_create' function given below.
466 *
467 * name - the name of the menu as given on the 'menu label' line.
468 * kernel - the path to the kernel file to use for this label.
469 * append - kernel command line to use when booting this label
470 * initrd - path to the initrd to use for this label.
471 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
472 * localboot - 1 if this label specified 'localboot', 0 otherwise.
473 * list - lets these form a list, which a pxe_menu struct will hold.
474 */
475struct pxe_label {
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600476 char num[4];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500477 char *name;
Rob Herringa6b74172012-03-28 05:51:34 +0000478 char *menu;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500479 char *kernel;
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +0200480 char *config;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500481 char *append;
482 char *initrd;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000483 char *fdt;
Stephen Warren90d09af2014-01-28 14:50:10 -0700484 char *fdtdir;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600485 int ipappend;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500486 int attempted;
487 int localboot;
Rob Herring58d7f432012-12-02 21:00:22 -0600488 int localboot_val;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500489 struct list_head list;
490};
491
492/*
493 * Describes a pxe menu as given via pxe files.
494 *
495 * title - the name of the menu as given by a 'menu title' line.
496 * default_label - the name of the default label, if any.
Patrice Chotardd5074912019-04-11 11:13:13 +0200497 * bmp - the bmp file name which is displayed in background
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500498 * timeout - time in tenths of a second to wait for a user key-press before
499 * booting the default label.
500 * prompt - if 0, don't prompt for a choice unless the timeout period is
501 * interrupted. If 1, always prompt for a choice regardless of
502 * timeout.
503 * labels - a list of labels defined for the menu.
504 */
505struct pxe_menu {
506 char *title;
507 char *default_label;
Patrice Chotardd5074912019-04-11 11:13:13 +0200508 char *bmp;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500509 int timeout;
510 int prompt;
511 struct list_head labels;
512};
513
514/*
515 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
516 * result must be free()'d to reclaim the memory.
517 *
518 * Returns NULL if malloc fails.
519 */
520static struct pxe_label *label_create(void)
521{
522 struct pxe_label *label;
523
524 label = malloc(sizeof(struct pxe_label));
525
526 if (!label)
527 return NULL;
528
529 memset(label, 0, sizeof(struct pxe_label));
530
531 return label;
532}
533
534/*
535 * Free the memory used by a pxe_label, including that used by its name,
536 * kernel, append and initrd members, if they're non NULL.
537 *
538 * So - be sure to only use dynamically allocated memory for the members of
539 * the pxe_label struct, unless you want to clean it up first. These are
540 * currently only created by the pxe file parsing code.
541 */
542static void label_destroy(struct pxe_label *label)
543{
544 if (label->name)
545 free(label->name);
546
547 if (label->kernel)
548 free(label->kernel);
549
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +0200550 if (label->config)
551 free(label->config);
552
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500553 if (label->append)
554 free(label->append);
555
556 if (label->initrd)
557 free(label->initrd);
558
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000559 if (label->fdt)
560 free(label->fdt);
561
Stephen Warren90d09af2014-01-28 14:50:10 -0700562 if (label->fdtdir)
563 free(label->fdtdir);
564
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500565 free(label);
566}
567
568/*
569 * Print a label and its string members if they're defined.
570 *
571 * This is passed as a callback to the menu code for displaying each
572 * menu entry.
573 */
574static void label_print(void *data)
575{
576 struct pxe_label *label = data;
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600577 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500578
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600579 printf("%s:\t%s\n", label->num, c);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500580}
581
582/*
583 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
Bin Meng75574052016-02-05 19:30:11 -0800584 * environment variable is defined. Its contents will be executed as U-Boot
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500585 * command. If the label specified an 'append' line, its contents will be
586 * used to overwrite the contents of the 'bootargs' environment variable prior
587 * to running 'localcmd'.
588 *
589 * Returns 1 on success or < 0 on error.
590 */
591static int label_localboot(struct pxe_label *label)
592{
Simon Glassa6642e02012-03-30 21:30:55 +0000593 char *localcmd;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500594
595 localcmd = from_env("localcmd");
596
597 if (!localcmd)
598 return -ENOENT;
599
Hans de Goede55adc412014-08-06 09:37:39 +0200600 if (label->append) {
601 char bootargs[CONFIG_SYS_CBSIZE];
602
603 cli_simple_process_macros(label->append, bootargs);
Simon Glass6a38e412017-08-03 12:22:09 -0600604 env_set("bootargs", bootargs);
Hans de Goede55adc412014-08-06 09:37:39 +0200605 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500606
Simon Glassa6642e02012-03-30 21:30:55 +0000607 debug("running: %s\n", localcmd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500608
Simon Glassa6642e02012-03-30 21:30:55 +0000609 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500610}
611
612/*
613 * Boot according to the contents of a pxe_label.
614 *
615 * If we can't boot for any reason, we return. A successful boot never
616 * returns.
617 *
618 * The kernel will be stored in the location given by the 'kernel_addr_r'
619 * environment variable.
620 *
621 * If the label specifies an initrd file, it will be stored in the location
622 * given by the 'ramdisk_addr_r' environment variable.
623 *
624 * If the label specifies an 'append' line, its contents will overwrite that
625 * of the 'bootargs' environment variable.
626 */
Tom Rini5b5b1762013-09-24 09:05:08 -0400627static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500628{
629 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Tom Rini920c4dd2017-09-26 20:44:32 -0400630 char initrd_str[28];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600631 char mac_str[29] = "";
632 char ip_str[68] = "";
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +0200633 char *fit_addr = NULL;
York Sun2566fd92016-09-01 16:28:21 +0800634 int bootm_argc = 2;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600635 int len = 0;
Bryan Wua1d715b2014-07-31 17:39:59 -0700636 ulong kernel_addr;
637 void *buf;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500638
639 label_print(label);
640
641 label->attempted = 1;
642
643 if (label->localboot) {
Rob Herring58d7f432012-12-02 21:00:22 -0600644 if (label->localboot_val >= 0)
645 label_localboot(label);
646 return 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500647 }
648
649 if (label->kernel == NULL) {
650 printf("No kernel given, skipping %s\n",
651 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600652 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500653 }
654
655 if (label->initrd) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400656 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500657 printf("Skipping %s for failure retrieving initrd\n",
658 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600659 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500660 }
661
Rob Herringc0356dc2012-12-03 13:17:21 -0600662 bootm_argv[2] = initrd_str;
Tom Rini920c4dd2017-09-26 20:44:32 -0400663 strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
Rob Herringc0356dc2012-12-03 13:17:21 -0600664 strcat(bootm_argv[2], ":");
Tom Rini920c4dd2017-09-26 20:44:32 -0400665 strncat(bootm_argv[2], env_get("filesize"), 9);
Abhishek Shah7ca4e2c2019-11-18 16:41:49 -0800666 bootm_argc = 3;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500667 }
668
Steven Falco7de0fa82013-10-07 09:51:48 -0400669 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500670 printf("Skipping %s for failure retrieving kernel\n",
671 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600672 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500673 }
674
Rob Herring5a3d8be2012-12-02 21:00:29 -0600675 if (label->ipappend & 0x1) {
676 sprintf(ip_str, " ip=%s:%s:%s:%s",
Simon Glass64b723f2017-08-03 12:22:12 -0600677 env_get("ipaddr"), env_get("serverip"),
678 env_get("gatewayip"), env_get("netmask"));
Rob Herring5a3d8be2012-12-02 21:00:29 -0600679 }
680
Stephen Warren857291b2014-02-05 20:49:20 -0700681#ifdef CONFIG_CMD_NET
Rob Herring5a3d8be2012-12-02 21:00:29 -0600682 if (label->ipappend & 0x2) {
683 int err;
684 strcpy(mac_str, " BOOTIF=");
685 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
686 if (err < 0)
687 mac_str[0] = '\0';
Rob Herring5a3d8be2012-12-02 21:00:29 -0600688 }
Stephen Warren857291b2014-02-05 20:49:20 -0700689#endif
Rob Herring5a3d8be2012-12-02 21:00:29 -0600690
Hans de Goede55adc412014-08-06 09:37:39 +0200691 if ((label->ipappend & 0x3) || label->append) {
692 char bootargs[CONFIG_SYS_CBSIZE] = "";
693 char finalbootargs[CONFIG_SYS_CBSIZE];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600694
Ian Campbell149efb42014-10-03 14:29:01 +0100695 if (strlen(label->append ?: "") +
696 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
697 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
698 strlen(label->append ?: ""),
699 strlen(ip_str), strlen(mac_str),
700 sizeof(bootargs));
701 return 1;
Tom Rini121f5072017-10-11 15:34:33 -0400702 } else {
703 if (label->append)
704 strncpy(bootargs, label->append,
705 sizeof(bootargs));
706 strcat(bootargs, ip_str);
707 strcat(bootargs, mac_str);
Rob Herring5a3d8be2012-12-02 21:00:29 -0600708
Tom Rini121f5072017-10-11 15:34:33 -0400709 cli_simple_process_macros(bootargs, finalbootargs);
710 env_set("bootargs", finalbootargs);
711 printf("append: %s\n", finalbootargs);
712 }
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600713 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500714
Simon Glass64b723f2017-08-03 12:22:12 -0600715 bootm_argv[1] = env_get("kernel_addr_r");
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +0200716 /* for FIT, append the configuration identifier */
717 if (label->config) {
718 int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
719
720 fit_addr = malloc(len);
721 if (!fit_addr) {
722 printf("malloc fail (FIT address)\n");
723 return 1;
724 }
725 snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
726 bootm_argv[1] = fit_addr;
727 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500728
729 /*
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000730 * fdt usage is optional:
731 * It handles the following scenarios. All scenarios are exclusive
732 *
733 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
734 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
735 * and adjust argc appropriately.
736 *
737 * Scenario 2: If there is an fdt_addr specified, pass it along to
738 * bootm, and adjust argc appropriately.
739 *
740 * Scenario 3: fdt blob is not available.
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500741 */
Simon Glass64b723f2017-08-03 12:22:12 -0600742 bootm_argv[3] = env_get("fdt_addr_r");
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000743
744 /* if fdt label is defined then get fdt from server */
Stephen Warren90d09af2014-01-28 14:50:10 -0700745 if (bootm_argv[3]) {
746 char *fdtfile = NULL;
747 char *fdtfilefree = NULL;
748
749 if (label->fdt) {
750 fdtfile = label->fdt;
751 } else if (label->fdtdir) {
Stephen Warrenc0592392014-02-12 14:30:04 -0700752 char *f1, *f2, *f3, *f4, *slash;
Stephen Warren90d09af2014-01-28 14:50:10 -0700753
Simon Glass64b723f2017-08-03 12:22:12 -0600754 f1 = env_get("fdtfile");
Stephen Warrenc0592392014-02-12 14:30:04 -0700755 if (f1) {
756 f2 = "";
757 f3 = "";
758 f4 = "";
759 } else {
760 /*
761 * For complex cases where this code doesn't
762 * generate the correct filename, the board
763 * code should set $fdtfile during early boot,
764 * or the boot scripts should set $fdtfile
765 * before invoking "pxe" or "sysboot".
766 */
Simon Glass64b723f2017-08-03 12:22:12 -0600767 f1 = env_get("soc");
Stephen Warrenc0592392014-02-12 14:30:04 -0700768 f2 = "-";
Simon Glass64b723f2017-08-03 12:22:12 -0600769 f3 = env_get("board");
Stephen Warrenc0592392014-02-12 14:30:04 -0700770 f4 = ".dtb";
771 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700772
Stephen Warrenc0592392014-02-12 14:30:04 -0700773 len = strlen(label->fdtdir);
774 if (!len)
775 slash = "./";
776 else if (label->fdtdir[len - 1] != '/')
777 slash = "/";
778 else
779 slash = "";
Stephen Warren90d09af2014-01-28 14:50:10 -0700780
Stephen Warrenc0592392014-02-12 14:30:04 -0700781 len = strlen(label->fdtdir) + strlen(slash) +
782 strlen(f1) + strlen(f2) + strlen(f3) +
783 strlen(f4) + 1;
784 fdtfilefree = malloc(len);
785 if (!fdtfilefree) {
786 printf("malloc fail (FDT filename)\n");
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +0200787 goto cleanup;
Stephen Warren90d09af2014-01-28 14:50:10 -0700788 }
Stephen Warrenc0592392014-02-12 14:30:04 -0700789
790 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
791 label->fdtdir, slash, f1, f2, f3, f4);
792 fdtfile = fdtfilefree;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000793 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700794
795 if (fdtfile) {
796 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
797 free(fdtfilefree);
798 if (err < 0) {
799 printf("Skipping %s for failure retrieving fdt\n",
800 label->name);
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +0200801 goto cleanup;
Stephen Warren90d09af2014-01-28 14:50:10 -0700802 }
803 } else {
804 bootm_argv[3] = NULL;
805 }
806 }
807
808 if (!bootm_argv[3])
Simon Glass64b723f2017-08-03 12:22:12 -0600809 bootm_argv[3] = env_get("fdt_addr");
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500810
York Sun2566fd92016-09-01 16:28:21 +0800811 if (bootm_argv[3]) {
812 if (!bootm_argv[2])
813 bootm_argv[2] = "-";
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500814 bootm_argc = 4;
York Sun2566fd92016-09-01 16:28:21 +0800815 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500816
Bryan Wua1d715b2014-07-31 17:39:59 -0700817 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
818 buf = map_sysmem(kernel_addr, 0);
819 /* Try bootm for legacy and FIT format image */
820 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
821 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Stephen Warren9c3c9312015-07-21 17:49:41 -0600822#ifdef CONFIG_CMD_BOOTI
823 /* Try booting an AArch64 Linux kernel image */
824 else
825 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
826#elif defined(CONFIG_CMD_BOOTZ)
827 /* Try booting a Image */
Bryan Wua1d715b2014-07-31 17:39:59 -0700828 else
829 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600830#endif
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200831 unmap_sysmem(buf);
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +0200832
833cleanup:
834 if (fit_addr)
835 free(fit_addr);
Rob Herring58d7f432012-12-02 21:00:22 -0600836 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500837}
838
839/*
840 * Tokens for the pxe file parser.
841 */
842enum token_type {
843 T_EOL,
844 T_STRING,
845 T_EOF,
846 T_MENU,
847 T_TITLE,
848 T_TIMEOUT,
849 T_LABEL,
850 T_KERNEL,
Rob Herringc93f74a2012-03-28 05:51:35 +0000851 T_LINUX,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500852 T_APPEND,
853 T_INITRD,
854 T_LOCALBOOT,
855 T_DEFAULT,
856 T_PROMPT,
857 T_INCLUDE,
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000858 T_FDT,
Stephen Warren90d09af2014-01-28 14:50:10 -0700859 T_FDTDIR,
Rob Herringac995832012-12-02 21:00:27 -0600860 T_ONTIMEOUT,
Rob Herring5a3d8be2012-12-02 21:00:29 -0600861 T_IPAPPEND,
Patrice Chotardd5074912019-04-11 11:13:13 +0200862 T_BACKGROUND,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500863 T_INVALID
864};
865
866/*
867 * A token - given by a value and a type.
868 */
869struct token {
870 char *val;
871 enum token_type type;
872};
873
874/*
875 * Keywords recognized.
876 */
877static const struct token keywords[] = {
878 {"menu", T_MENU},
879 {"title", T_TITLE},
880 {"timeout", T_TIMEOUT},
881 {"default", T_DEFAULT},
882 {"prompt", T_PROMPT},
883 {"label", T_LABEL},
884 {"kernel", T_KERNEL},
Rob Herringc93f74a2012-03-28 05:51:35 +0000885 {"linux", T_LINUX},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500886 {"localboot", T_LOCALBOOT},
887 {"append", T_APPEND},
888 {"initrd", T_INITRD},
889 {"include", T_INCLUDE},
Stephen Warren7b472af2014-01-28 14:50:09 -0700890 {"devicetree", T_FDT},
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000891 {"fdt", T_FDT},
Stephen Warren90d09af2014-01-28 14:50:10 -0700892 {"devicetreedir", T_FDTDIR},
893 {"fdtdir", T_FDTDIR},
Rob Herringac995832012-12-02 21:00:27 -0600894 {"ontimeout", T_ONTIMEOUT,},
Rob Herring5a3d8be2012-12-02 21:00:29 -0600895 {"ipappend", T_IPAPPEND,},
Patrice Chotardd5074912019-04-11 11:13:13 +0200896 {"background", T_BACKGROUND,},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500897 {NULL, T_INVALID}
898};
899
900/*
901 * Since pxe(linux) files don't have a token to identify the start of a
902 * literal, we have to keep track of when we're in a state where a literal is
903 * expected vs when we're in a state a keyword is expected.
904 */
905enum lex_state {
906 L_NORMAL = 0,
907 L_KEYWORD,
908 L_SLITERAL
909};
910
911/*
912 * get_string retrieves a string from *p and stores it as a token in
913 * *t.
914 *
915 * get_string used for scanning both string literals and keywords.
916 *
917 * Characters from *p are copied into t-val until a character equal to
918 * delim is found, or a NUL byte is reached. If delim has the special value of
919 * ' ', any whitespace character will be used as a delimiter.
920 *
921 * If lower is unequal to 0, uppercase characters will be converted to
922 * lowercase in the result. This is useful to make keywords case
923 * insensitive.
924 *
925 * The location of *p is updated to point to the first character after the end
926 * of the token - the ending delimiter.
927 *
928 * On success, the new value of t->val is returned. Memory for t->val is
929 * allocated using malloc and must be free()'d to reclaim it. If insufficient
930 * memory is available, NULL is returned.
931 */
932static char *get_string(char **p, struct token *t, char delim, int lower)
933{
934 char *b, *e;
935 size_t len, i;
936
937 /*
938 * b and e both start at the beginning of the input stream.
939 *
940 * e is incremented until we find the ending delimiter, or a NUL byte
941 * is reached. Then, we take e - b to find the length of the token.
942 */
943 b = e = *p;
944
945 while (*e) {
946 if ((delim == ' ' && isspace(*e)) || delim == *e)
947 break;
948 e++;
949 }
950
951 len = e - b;
952
953 /*
954 * Allocate memory to hold the string, and copy it in, converting
955 * characters to lowercase if lower is != 0.
956 */
957 t->val = malloc(len + 1);
958 if (!t->val)
959 return NULL;
960
961 for (i = 0; i < len; i++, b++) {
962 if (lower)
963 t->val[i] = tolower(*b);
964 else
965 t->val[i] = *b;
966 }
967
968 t->val[len] = '\0';
969
970 /*
971 * Update *p so the caller knows where to continue scanning.
972 */
973 *p = e;
974
975 t->type = T_STRING;
976
977 return t->val;
978}
979
980/*
981 * Populate a keyword token with a type and value.
982 */
983static void get_keyword(struct token *t)
984{
985 int i;
986
987 for (i = 0; keywords[i].val; i++) {
988 if (!strcmp(t->val, keywords[i].val)) {
989 t->type = keywords[i].type;
990 break;
991 }
992 }
993}
994
995/*
996 * Get the next token. We have to keep track of which state we're in to know
997 * if we're looking to get a string literal or a keyword.
998 *
999 * *p is updated to point at the first character after the current token.
1000 */
1001static void get_token(char **p, struct token *t, enum lex_state state)
1002{
1003 char *c = *p;
1004
1005 t->type = T_INVALID;
1006
1007 /* eat non EOL whitespace */
1008 while (isblank(*c))
1009 c++;
1010
1011 /*
1012 * eat comments. note that string literals can't begin with #, but
1013 * can contain a # after their first character.
1014 */
1015 if (*c == '#') {
1016 while (*c && *c != '\n')
1017 c++;
1018 }
1019
1020 if (*c == '\n') {
1021 t->type = T_EOL;
1022 c++;
1023 } else if (*c == '\0') {
1024 t->type = T_EOF;
1025 c++;
1026 } else if (state == L_SLITERAL) {
1027 get_string(&c, t, '\n', 0);
1028 } else if (state == L_KEYWORD) {
1029 /*
1030 * when we expect a keyword, we first get the next string
1031 * token delimited by whitespace, and then check if it
1032 * matches a keyword in our keyword list. if it does, it's
1033 * converted to a keyword token of the appropriate type, and
1034 * if not, it remains a string token.
1035 */
1036 get_string(&c, t, ' ', 1);
1037 get_keyword(t);
1038 }
1039
1040 *p = c;
1041}
1042
1043/*
1044 * Increment *c until we get to the end of the current line, or EOF.
1045 */
1046static void eol_or_eof(char **c)
1047{
1048 while (**c && **c != '\n')
1049 (*c)++;
1050}
1051
1052/*
1053 * All of these parse_* functions share some common behavior.
1054 *
1055 * They finish with *c pointing after the token they parse, and return 1 on
1056 * success, or < 0 on error.
1057 */
1058
1059/*
1060 * Parse a string literal and store a pointer it at *dst. String literals
1061 * terminate at the end of the line.
1062 */
1063static int parse_sliteral(char **c, char **dst)
1064{
1065 struct token t;
1066 char *s = *c;
1067
1068 get_token(c, &t, L_SLITERAL);
1069
1070 if (t.type != T_STRING) {
1071 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1072 return -EINVAL;
1073 }
1074
1075 *dst = t.val;
1076
1077 return 1;
1078}
1079
1080/*
1081 * Parse a base 10 (unsigned) integer and store it at *dst.
1082 */
1083static int parse_integer(char **c, int *dst)
1084{
1085 struct token t;
1086 char *s = *c;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001087
1088 get_token(c, &t, L_SLITERAL);
1089
1090 if (t.type != T_STRING) {
1091 printf("Expected string: %.*s\n", (int)(*c - s), s);
1092 return -EINVAL;
1093 }
1094
Rob Herring58d7f432012-12-02 21:00:22 -06001095 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001096
1097 free(t.val);
1098
1099 return 1;
1100}
1101
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001102static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1103 struct pxe_menu *cfg, int nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001104
1105/*
1106 * Parse an include statement, and retrieve and parse the file it mentions.
1107 *
1108 * base should point to a location where it's safe to store the file, and
1109 * nest_level should indicate how many nested includes have occurred. For this
1110 * include, nest_level has already been incremented and doesn't need to be
1111 * incremented here.
1112 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001113static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001114 struct pxe_menu *cfg, int nest_level)
1115{
1116 char *include_path;
1117 char *s = *c;
1118 int err;
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001119 char *buf;
1120 int ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001121
1122 err = parse_sliteral(c, &include_path);
1123
1124 if (err < 0) {
1125 printf("Expected include path: %.*s\n",
1126 (int)(*c - s), s);
1127 return err;
1128 }
1129
Steven Falco7de0fa82013-10-07 09:51:48 -04001130 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001131
1132 if (err < 0) {
1133 printf("Couldn't retrieve %s\n", include_path);
1134 return err;
1135 }
1136
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001137 buf = map_sysmem(base, 0);
1138 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
1139 unmap_sysmem(buf);
1140
1141 return ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001142}
1143
1144/*
1145 * Parse lines that begin with 'menu'.
1146 *
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001147 * base and nest are provided to handle the 'menu include' case.
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001148 *
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001149 * base should point to a location where it's safe to store the included file.
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001150 *
1151 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1152 * a file it includes, 3 when parsing a file included by that file, and so on.
1153 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001154static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
1155 unsigned long base, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001156{
1157 struct token t;
1158 char *s = *c;
Heiko Schocher9cd01dd2011-12-12 20:37:17 +00001159 int err = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001160
1161 get_token(c, &t, L_KEYWORD);
1162
1163 switch (t.type) {
1164 case T_TITLE:
1165 err = parse_sliteral(c, &cfg->title);
1166
1167 break;
1168
1169 case T_INCLUDE:
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001170 err = handle_include(cmdtp, c, base, cfg,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001171 nest_level + 1);
1172 break;
1173
Patrice Chotardd5074912019-04-11 11:13:13 +02001174 case T_BACKGROUND:
1175 err = parse_sliteral(c, &cfg->bmp);
1176 break;
1177
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001178 default:
1179 printf("Ignoring malformed menu command: %.*s\n",
1180 (int)(*c - s), s);
1181 }
1182
1183 if (err < 0)
1184 return err;
1185
1186 eol_or_eof(c);
1187
1188 return 1;
1189}
1190
1191/*
1192 * Handles parsing a 'menu line' when we're parsing a label.
1193 */
1194static int parse_label_menu(char **c, struct pxe_menu *cfg,
1195 struct pxe_label *label)
1196{
1197 struct token t;
1198 char *s;
1199
1200 s = *c;
1201
1202 get_token(c, &t, L_KEYWORD);
1203
1204 switch (t.type) {
1205 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001206 if (!cfg->default_label)
1207 cfg->default_label = strdup(label->name);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001208
1209 if (!cfg->default_label)
1210 return -ENOMEM;
1211
1212 break;
Rob Herringa6b74172012-03-28 05:51:34 +00001213 case T_LABEL:
1214 parse_sliteral(c, &label->menu);
1215 break;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001216 default:
1217 printf("Ignoring malformed menu command: %.*s\n",
1218 (int)(*c - s), s);
1219 }
1220
1221 eol_or_eof(c);
1222
1223 return 0;
1224}
1225
1226/*
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +02001227 * Handles parsing a 'kernel' label.
1228 * expecting "filename" or "<fit_filename>#cfg"
1229 */
1230static int parse_label_kernel(char **c, struct pxe_label *label)
1231{
1232 char *s;
1233 int err;
1234
1235 err = parse_sliteral(c, &label->kernel);
1236 if (err < 0)
1237 return err;
1238
1239 s = strstr(label->kernel, "#");
1240 if (!s)
1241 return 1;
1242
1243 label->config = malloc(strlen(s) + 1);
1244 if (!label->config)
1245 return -ENOMEM;
1246
1247 strcpy(label->config, s);
1248 *s = 0;
1249
1250 return 1;
1251}
1252
1253/*
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001254 * Parses a label and adds it to the list of labels for a menu.
1255 *
1256 * A label ends when we either get to the end of a file, or
1257 * get some input we otherwise don't have a handler defined
1258 * for.
1259 *
1260 */
1261static int parse_label(char **c, struct pxe_menu *cfg)
1262{
1263 struct token t;
Rob Herringd8fceec2012-03-28 05:51:37 +00001264 int len;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001265 char *s = *c;
1266 struct pxe_label *label;
1267 int err;
1268
1269 label = label_create();
1270 if (!label)
1271 return -ENOMEM;
1272
1273 err = parse_sliteral(c, &label->name);
1274 if (err < 0) {
1275 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1276 label_destroy(label);
1277 return -EINVAL;
1278 }
1279
1280 list_add_tail(&label->list, &cfg->labels);
1281
1282 while (1) {
1283 s = *c;
1284 get_token(c, &t, L_KEYWORD);
1285
1286 err = 0;
1287 switch (t.type) {
1288 case T_MENU:
1289 err = parse_label_menu(c, cfg, label);
1290 break;
1291
1292 case T_KERNEL:
Rob Herringc93f74a2012-03-28 05:51:35 +00001293 case T_LINUX:
Patrick Delaunay1d3ffee2018-10-02 10:54:48 +02001294 err = parse_label_kernel(c, label);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001295 break;
1296
1297 case T_APPEND:
1298 err = parse_sliteral(c, &label->append);
Rob Herringd8fceec2012-03-28 05:51:37 +00001299 if (label->initrd)
1300 break;
1301 s = strstr(label->append, "initrd=");
1302 if (!s)
1303 break;
1304 s += 7;
1305 len = (int)(strchr(s, ' ') - s);
1306 label->initrd = malloc(len + 1);
1307 strncpy(label->initrd, s, len);
1308 label->initrd[len] = '\0';
1309
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001310 break;
1311
1312 case T_INITRD:
Rob Herringd8fceec2012-03-28 05:51:37 +00001313 if (!label->initrd)
1314 err = parse_sliteral(c, &label->initrd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001315 break;
1316
Chander Kashyap1644a9d2012-09-06 19:36:31 +00001317 case T_FDT:
1318 if (!label->fdt)
1319 err = parse_sliteral(c, &label->fdt);
1320 break;
1321
Stephen Warren90d09af2014-01-28 14:50:10 -07001322 case T_FDTDIR:
1323 if (!label->fdtdir)
1324 err = parse_sliteral(c, &label->fdtdir);
1325 break;
1326
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001327 case T_LOCALBOOT:
Rob Herring58d7f432012-12-02 21:00:22 -06001328 label->localboot = 1;
1329 err = parse_integer(c, &label->localboot_val);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001330 break;
1331
Rob Herring5a3d8be2012-12-02 21:00:29 -06001332 case T_IPAPPEND:
1333 err = parse_integer(c, &label->ipappend);
1334 break;
1335
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001336 case T_EOL:
1337 break;
1338
1339 default:
1340 /*
1341 * put the token back! we don't want it - it's the end
1342 * of a label and whatever token this is, it's
1343 * something for the menu level context to handle.
1344 */
1345 *c = s;
1346 return 1;
1347 }
1348
1349 if (err < 0)
1350 return err;
1351 }
1352}
1353
1354/*
1355 * This 16 comes from the limit pxelinux imposes on nested includes.
1356 *
1357 * There is no reason at all we couldn't do more, but some limit helps prevent
1358 * infinite (until crash occurs) recursion if a file tries to include itself.
1359 */
1360#define MAX_NEST_LEVEL 16
1361
1362/*
1363 * Entry point for parsing a menu file. nest_level indicates how many times
1364 * we've nested in includes. It will be 1 for the top level menu file.
1365 *
1366 * Returns 1 on success, < 0 on error.
1367 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001368static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1369 struct pxe_menu *cfg, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001370{
1371 struct token t;
1372 char *s, *b, *label_name;
1373 int err;
1374
1375 b = p;
1376
1377 if (nest_level > MAX_NEST_LEVEL) {
1378 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1379 return -EMLINK;
1380 }
1381
1382 while (1) {
1383 s = p;
1384
1385 get_token(&p, &t, L_KEYWORD);
1386
1387 err = 0;
1388 switch (t.type) {
1389 case T_MENU:
Rob Herringf0898b92012-12-02 21:00:25 -06001390 cfg->prompt = 1;
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001391 err = parse_menu(cmdtp, &p, cfg,
1392 base + ALIGN(strlen(b) + 1, 4),
1393 nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001394 break;
1395
1396 case T_TIMEOUT:
1397 err = parse_integer(&p, &cfg->timeout);
1398 break;
1399
1400 case T_LABEL:
1401 err = parse_label(&p, cfg);
1402 break;
1403
1404 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001405 case T_ONTIMEOUT:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001406 err = parse_sliteral(&p, &label_name);
1407
1408 if (label_name) {
1409 if (cfg->default_label)
1410 free(cfg->default_label);
1411
1412 cfg->default_label = label_name;
1413 }
1414
1415 break;
1416
Rob Herring8e5abb02012-05-25 10:43:16 +00001417 case T_INCLUDE:
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001418 err = handle_include(cmdtp, &p,
1419 base + ALIGN(strlen(b), 4), cfg,
1420 nest_level + 1);
Rob Herring8e5abb02012-05-25 10:43:16 +00001421 break;
1422
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001423 case T_PROMPT:
Rob Herringf0898b92012-12-02 21:00:25 -06001424 eol_or_eof(&p);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001425 break;
1426
1427 case T_EOL:
1428 break;
1429
1430 case T_EOF:
1431 return 1;
1432
1433 default:
1434 printf("Ignoring unknown command: %.*s\n",
1435 (int)(p - s), s);
1436 eol_or_eof(&p);
1437 }
1438
1439 if (err < 0)
1440 return err;
1441 }
1442}
1443
1444/*
1445 * Free the memory used by a pxe_menu and its labels.
1446 */
1447static void destroy_pxe_menu(struct pxe_menu *cfg)
1448{
1449 struct list_head *pos, *n;
1450 struct pxe_label *label;
1451
1452 if (cfg->title)
1453 free(cfg->title);
1454
1455 if (cfg->default_label)
1456 free(cfg->default_label);
1457
1458 list_for_each_safe(pos, n, &cfg->labels) {
1459 label = list_entry(pos, struct pxe_label, list);
1460
1461 label_destroy(label);
1462 }
1463
1464 free(cfg);
1465}
1466
1467/*
1468 * Entry point for parsing a pxe file. This is only used for the top level
1469 * file.
1470 *
1471 * Returns NULL if there is an error, otherwise, returns a pointer to a
1472 * pxe_menu struct populated with the results of parsing the pxe file (and any
1473 * files it includes). The resulting pxe_menu struct can be free()'d by using
1474 * the destroy_pxe_menu() function.
1475 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001476static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001477{
1478 struct pxe_menu *cfg;
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001479 char *buf;
1480 int r;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001481
1482 cfg = malloc(sizeof(struct pxe_menu));
1483
1484 if (!cfg)
1485 return NULL;
1486
1487 memset(cfg, 0, sizeof(struct pxe_menu));
1488
1489 INIT_LIST_HEAD(&cfg->labels);
1490
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001491 buf = map_sysmem(menucfg, 0);
1492 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1493 unmap_sysmem(buf);
1494
1495 if (r < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001496 destroy_pxe_menu(cfg);
1497 return NULL;
1498 }
1499
1500 return cfg;
1501}
1502
1503/*
Bin Meng75574052016-02-05 19:30:11 -08001504 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001505 * menu code.
1506 */
1507static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1508{
1509 struct pxe_label *label;
1510 struct list_head *pos;
1511 struct menu *m;
1512 int err;
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001513 int i = 1;
1514 char *default_num = NULL;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001515
1516 /*
1517 * Create a menu and add items for all the labels.
1518 */
Masahiro Yamadac46d0fe2018-05-24 17:04:57 +09001519 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1520 cfg->prompt, label_print, NULL, NULL);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001521
1522 if (!m)
1523 return NULL;
1524
1525 list_for_each(pos, &cfg->labels) {
1526 label = list_entry(pos, struct pxe_label, list);
1527
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001528 sprintf(label->num, "%d", i++);
1529 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001530 menu_destroy(m);
1531 return NULL;
1532 }
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001533 if (cfg->default_label &&
Rob Herringac995832012-12-02 21:00:27 -06001534 (strcmp(label->name, cfg->default_label) == 0))
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001535 default_num = label->num;
1536
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001537 }
1538
1539 /*
1540 * After we've created items for each label in the menu, set the
1541 * menu's default label if one was specified.
1542 */
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001543 if (default_num) {
1544 err = menu_default_set(m, default_num);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001545 if (err != 1) {
1546 if (err != -ENOENT) {
1547 menu_destroy(m);
1548 return NULL;
1549 }
1550
1551 printf("Missing default: %s\n", cfg->default_label);
1552 }
1553 }
1554
1555 return m;
1556}
1557
1558/*
1559 * Try to boot any labels we have yet to attempt to boot.
1560 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001561static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001562{
1563 struct list_head *pos;
1564 struct pxe_label *label;
1565
1566 list_for_each(pos, &cfg->labels) {
1567 label = list_entry(pos, struct pxe_label, list);
1568
1569 if (!label->attempted)
Tom Rini5b5b1762013-09-24 09:05:08 -04001570 label_boot(cmdtp, label);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001571 }
1572}
1573
1574/*
1575 * Boot the system as prescribed by a pxe_menu.
1576 *
1577 * Use the menu system to either get the user's choice or the default, based
1578 * on config or user input. If there is no default or user's choice,
1579 * attempted to boot labels in the order they were given in pxe files.
1580 * If the default or user's choice fails to boot, attempt to boot other
1581 * labels in the order they were given in pxe files.
1582 *
1583 * If this function returns, there weren't any labels that successfully
1584 * booted, or the user interrupted the menu selection via ctrl+c.
1585 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001586static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001587{
1588 void *choice;
1589 struct menu *m;
1590 int err;
1591
Patrice Chotardd5074912019-04-11 11:13:13 +02001592#ifdef CONFIG_CMD_BMP
1593 /* display BMP if available */
1594 if (cfg->bmp) {
1595 if (get_relfile(cmdtp, cfg->bmp, load_addr)) {
1596 run_command("cls", 0);
1597 bmp_display(load_addr,
1598 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1599 } else {
1600 printf("Skipping background bmp %s for failure\n",
1601 cfg->bmp);
1602 }
1603 }
1604#endif
1605
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001606 m = pxe_menu_to_menu(cfg);
1607 if (!m)
1608 return;
1609
1610 err = menu_get_choice(m, &choice);
1611
1612 menu_destroy(m);
1613
Jason Hobbs083b7a62011-11-07 03:07:15 +00001614 /*
1615 * err == 1 means we got a choice back from menu_get_choice.
1616 *
1617 * err == -ENOENT if the menu was setup to select the default but no
1618 * default was set. in that case, we should continue trying to boot
1619 * labels that haven't been attempted yet.
1620 *
1621 * otherwise, the user interrupted or there was some other error and
1622 * we give up.
1623 */
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001624
Rob Herring58d7f432012-12-02 21:00:22 -06001625 if (err == 1) {
Tom Rini5b5b1762013-09-24 09:05:08 -04001626 err = label_boot(cmdtp, choice);
Rob Herring58d7f432012-12-02 21:00:22 -06001627 if (!err)
1628 return;
1629 } else if (err != -ENOENT) {
Jason Hobbs083b7a62011-11-07 03:07:15 +00001630 return;
Rob Herring58d7f432012-12-02 21:00:22 -06001631 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001632
Tom Rini5b5b1762013-09-24 09:05:08 -04001633 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001634}
1635
Stephen Warren857291b2014-02-05 20:49:20 -07001636#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001637/*
1638 * Boots a system using a pxe file
1639 *
1640 * Returns 0 on success, 1 on error.
1641 */
1642static int
1643do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1644{
1645 unsigned long pxefile_addr_r;
1646 struct pxe_menu *cfg;
1647 char *pxefile_addr_str;
1648
Rob Herringeee675f2012-05-25 10:47:39 +00001649 do_getfile = do_get_tftp;
1650
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001651 if (argc == 1) {
1652 pxefile_addr_str = from_env("pxefile_addr_r");
1653 if (!pxefile_addr_str)
1654 return 1;
1655
1656 } else if (argc == 2) {
1657 pxefile_addr_str = argv[1];
1658 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +00001659 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001660 }
1661
1662 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1663 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1664 return 1;
1665 }
1666
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001667 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001668
1669 if (cfg == NULL) {
1670 printf("Error parsing config file\n");
1671 return 1;
1672 }
1673
Tom Rini5b5b1762013-09-24 09:05:08 -04001674 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001675
1676 destroy_pxe_menu(cfg);
1677
Joe Hershberger290c8992015-04-08 01:41:02 -05001678 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
Stephen Warrenc24cbc22014-07-22 18:06:46 -06001679
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001680 return 0;
1681}
1682
1683static cmd_tbl_t cmd_pxe_sub[] = {
1684 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1685 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1686};
1687
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +02001688static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001689{
1690 cmd_tbl_t *cp;
1691
1692 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001693 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001694
Rob Herring759456e2013-10-18 13:04:42 -05001695 is_pxe = true;
1696
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001697 /* drop initial "pxe" arg */
1698 argc--;
1699 argv++;
1700
1701 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1702
1703 if (cp)
1704 return cp->cmd(cmdtp, flag, argc, argv);
1705
Simon Glassa06dfc72011-12-10 08:44:01 +00001706 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001707}
1708
1709U_BOOT_CMD(
1710 pxe, 3, 1, do_pxe,
1711 "commands to get and boot from pxe files",
1712 "get - try to retrieve a pxe file using tftp\npxe "
1713 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1714);
Stephen Warren857291b2014-02-05 20:49:20 -07001715#endif
Rob Herringeee675f2012-05-25 10:47:39 +00001716
1717/*
1718 * Boots a system using a local disk syslinux/extlinux file
1719 *
1720 * Returns 0 on success, 1 on error.
1721 */
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +02001722static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Rob Herringeee675f2012-05-25 10:47:39 +00001723{
1724 unsigned long pxefile_addr_r;
1725 struct pxe_menu *cfg;
1726 char *pxefile_addr_str;
1727 char *filename;
1728 int prompt = 0;
1729
Rob Herring759456e2013-10-18 13:04:42 -05001730 is_pxe = false;
1731
Tuomas Tynkkynen22591d02015-05-07 21:29:18 +03001732 if (argc > 1 && strstr(argv[1], "-p")) {
Rob Herringeee675f2012-05-25 10:47:39 +00001733 prompt = 1;
1734 argc--;
1735 argv++;
1736 }
1737
1738 if (argc < 4)
1739 return cmd_usage(cmdtp);
1740
1741 if (argc < 5) {
1742 pxefile_addr_str = from_env("pxefile_addr_r");
1743 if (!pxefile_addr_str)
1744 return 1;
1745 } else {
1746 pxefile_addr_str = argv[4];
1747 }
1748
1749 if (argc < 6)
Simon Glass64b723f2017-08-03 12:22:12 -06001750 filename = env_get("bootfile");
Rob Herringeee675f2012-05-25 10:47:39 +00001751 else {
1752 filename = argv[5];
Simon Glass6a38e412017-08-03 12:22:09 -06001753 env_set("bootfile", filename);
Rob Herringeee675f2012-05-25 10:47:39 +00001754 }
1755
1756 if (strstr(argv[3], "ext2"))
1757 do_getfile = do_get_ext2;
1758 else if (strstr(argv[3], "fat"))
1759 do_getfile = do_get_fat;
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001760 else if (strstr(argv[3], "any"))
1761 do_getfile = do_get_any;
Rob Herringeee675f2012-05-25 10:47:39 +00001762 else {
1763 printf("Invalid filesystem: %s\n", argv[3]);
1764 return 1;
1765 }
1766 fs_argv[1] = argv[1];
1767 fs_argv[2] = argv[2];
1768
1769 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1770 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1771 return 1;
1772 }
1773
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001774 if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
Rob Herringeee675f2012-05-25 10:47:39 +00001775 printf("Error reading config file\n");
1776 return 1;
1777 }
1778
Sjoerd Simons45af07c2015-04-13 22:54:25 +02001779 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
Rob Herringeee675f2012-05-25 10:47:39 +00001780
1781 if (cfg == NULL) {
1782 printf("Error parsing config file\n");
1783 return 1;
1784 }
1785
1786 if (prompt)
1787 cfg->prompt = 1;
1788
Tom Rini5b5b1762013-09-24 09:05:08 -04001789 handle_pxe_menu(cmdtp, cfg);
Rob Herringeee675f2012-05-25 10:47:39 +00001790
1791 destroy_pxe_menu(cfg);
1792
1793 return 0;
1794}
1795
1796U_BOOT_CMD(
1797 sysboot, 7, 1, do_sysboot,
1798 "command to get and boot from syslinux files",
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001799 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1800 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1801 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herringeee675f2012-05-25 10:47:39 +00001802);