blob: 5cde5b6258ccee2e39c14874daa333323f39352c [file] [log] [blame]
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wua1d715b2014-07-31 17:39:59 -07003 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs0e3a5932011-08-31 10:37:30 -05004 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0e3a5932011-08-31 10:37:30 -05006 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007
Jason Hobbs0e3a5932011-08-31 10:37:30 -05008#include <common.h>
9#include <command.h>
10#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050011#include <mapmem.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050012#include <linux/string.h>
13#include <linux/ctype.h>
14#include <errno.h>
15#include <linux/list.h>
Dennis Gilmorecd90dc82014-02-04 05:25:46 -060016#include <fs.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050017
18#include "menu.h"
Hans de Goede55adc412014-08-06 09:37:39 +020019#include "cli.h"
Jason Hobbs0e3a5932011-08-31 10:37:30 -050020
21#define MAX_TFTP_PATH_LEN 127
22
Rob Herringe26e8d62012-12-02 21:00:28 -060023const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050024#ifdef CONFIG_SYS_SOC
Rob Herringe26e8d62012-12-02 21:00:28 -060025 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050026#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060027 "default-" CONFIG_SYS_ARCH,
28 "default",
29 NULL
30};
31
Rob Herring759456e2013-10-18 13:04:42 -050032static bool is_pxe;
33
Jason Hobbs0e3a5932011-08-31 10:37:30 -050034/*
35 * Like getenv, but prints an error if envvar isn't defined in the
36 * environment. It always returns what getenv does, so it can be used in
37 * place of getenv without changing error handling otherwise.
38 */
Rob Herring824901c2012-12-02 21:00:21 -060039static char *from_env(const char *envvar)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050040{
41 char *ret;
42
43 ret = getenv(envvar);
44
45 if (!ret)
46 printf("missing environment variable: %s\n", envvar);
47
48 return ret;
49}
50
Stephen Warren857291b2014-02-05 20:49:20 -070051#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-08-31 10:37:30 -050052/*
53 * Convert an ethaddr from the environment to the format used by pxelinux
54 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
55 * the beginning of the ethernet address to indicate a hardware type of
56 * Ethernet. Also converts uppercase hex characters into lowercase, to match
57 * pxelinux's behavior.
58 *
59 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
60 * environment, or some other value < 0 on error.
61 */
62static int format_mac_pxe(char *outbuf, size_t outbuf_len)
63{
Rob Herringdd248b32012-12-02 21:00:20 -060064 uchar ethaddr[6];
Jason Hobbs0e3a5932011-08-31 10:37:30 -050065
Rob Herringdd248b32012-12-02 21:00:20 -060066 if (outbuf_len < 21) {
David Feng4ec50292013-12-14 11:47:30 +080067 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050068
69 return -EINVAL;
70 }
71
Rob Herringdd248b32012-12-02 21:00:20 -060072 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
73 ethaddr))
74 return -ENOENT;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050075
Rob Herringdd248b32012-12-02 21:00:20 -060076 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
77 ethaddr[0], ethaddr[1], ethaddr[2],
78 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050079
80 return 1;
81}
Stephen Warren857291b2014-02-05 20:49:20 -070082#endif
Jason Hobbs0e3a5932011-08-31 10:37:30 -050083
84/*
85 * Returns the directory the file specified in the bootfile env variable is
86 * in. If bootfile isn't defined in the environment, return NULL, which should
87 * be interpreted as "don't prepend anything to paths".
88 */
Rob Herringdc410cd2012-03-28 05:51:36 +000089static int get_bootfile_path(const char *file_path, char *bootfile_path,
90 size_t bootfile_path_size)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050091{
92 char *bootfile, *last_slash;
Rob Herringdc410cd2012-03-28 05:51:36 +000093 size_t path_len = 0;
94
Rob Herring759456e2013-10-18 13:04:42 -050095 /* Only syslinux allows absolute paths */
96 if (file_path[0] == '/' && !is_pxe)
Rob Herringdc410cd2012-03-28 05:51:36 +000097 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050098
99 bootfile = from_env("bootfile");
100
Rob Herringdc410cd2012-03-28 05:51:36 +0000101 if (!bootfile)
102 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500103
104 last_slash = strrchr(bootfile, '/');
105
Rob Herringdc410cd2012-03-28 05:51:36 +0000106 if (last_slash == NULL)
107 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500108
109 path_len = (last_slash - bootfile) + 1;
110
111 if (bootfile_path_size < path_len) {
David Feng4ec50292013-12-14 11:47:30 +0800112 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500113 bootfile_path_size, path_len);
114
115 return -1;
116 }
117
118 strncpy(bootfile_path, bootfile, path_len);
119
Rob Herringdc410cd2012-03-28 05:51:36 +0000120 ret:
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500121 bootfile_path[path_len] = '\0';
122
123 return 1;
124}
125
Steven Falco7de0fa82013-10-07 09:51:48 -0400126static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herringeee675f2012-05-25 10:47:39 +0000127
Stephen Warren857291b2014-02-05 20:49:20 -0700128#ifdef CONFIG_CMD_NET
Steven Falco7de0fa82013-10-07 09:51:48 -0400129static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000130{
131 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
132
133 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600134 tftp_argv[2] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000135
Steven Falco7de0fa82013-10-07 09:51:48 -0400136 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000137 return -ENOENT;
138
139 return 1;
140}
Stephen Warren857291b2014-02-05 20:49:20 -0700141#endif
Rob Herringeee675f2012-05-25 10:47:39 +0000142
143static char *fs_argv[5];
144
Steven Falco7de0fa82013-10-07 09:51:48 -0400145static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000146{
147#ifdef CONFIG_CMD_EXT2
148 fs_argv[0] = "ext2load";
149 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600150 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000151
Steven Falco7de0fa82013-10-07 09:51:48 -0400152 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000153 return 1;
154#endif
155 return -ENOENT;
156}
157
Steven Falco7de0fa82013-10-07 09:51:48 -0400158static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000159{
160#ifdef CONFIG_CMD_FAT
161 fs_argv[0] = "fatload";
162 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600163 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000164
Steven Falco7de0fa82013-10-07 09:51:48 -0400165 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000166 return 1;
167#endif
168 return -ENOENT;
169}
170
Dennis Gilmorecd90dc82014-02-04 05:25:46 -0600171static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
172{
173#ifdef CONFIG_CMD_FS_GENERIC
174 fs_argv[0] = "load";
175 fs_argv[3] = file_addr;
176 fs_argv[4] = (void *)file_path;
177
178 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
179 return 1;
180#endif
181 return -ENOENT;
182}
183
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500184/*
185 * As in pxelinux, paths to files referenced from files we retrieve are
186 * relative to the location of bootfile. get_relfile takes such a path and
187 * joins it with the bootfile path to get the full path to the target file. If
188 * the bootfile path is NULL, we use file_path as is.
189 *
190 * Returns 1 for success, or < 0 on error.
191 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400192static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500193{
194 size_t path_len;
195 char relfile[MAX_TFTP_PATH_LEN+1];
196 char addr_buf[10];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500197 int err;
198
Rob Herringdc410cd2012-03-28 05:51:36 +0000199 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500200
201 if (err < 0)
202 return err;
203
204 path_len = strlen(file_path);
205 path_len += strlen(relfile);
206
207 if (path_len > MAX_TFTP_PATH_LEN) {
208 printf("Base path too long (%s%s)\n",
209 relfile,
210 file_path);
211
212 return -ENAMETOOLONG;
213 }
214
215 strcat(relfile, file_path);
216
217 printf("Retrieving file: %s\n", relfile);
218
219 sprintf(addr_buf, "%p", file_addr);
220
Steven Falco7de0fa82013-10-07 09:51:48 -0400221 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500222}
223
224/*
225 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
226 * 'bootfile' was specified in the environment, the path to bootfile will be
227 * prepended to 'file_path' and the resulting path will be used.
228 *
229 * Returns 1 on success, or < 0 for error.
230 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400231static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500232{
233 unsigned long config_file_size;
234 char *tftp_filesize;
235 int err;
236
Steven Falco7de0fa82013-10-07 09:51:48 -0400237 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500238
239 if (err < 0)
240 return err;
241
242 /*
243 * the file comes without a NUL byte at the end, so find out its size
244 * and add the NUL byte.
245 */
246 tftp_filesize = from_env("filesize");
247
248 if (!tftp_filesize)
249 return -ENOENT;
250
251 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
252 return -EINVAL;
253
254 *(char *)(file_addr + config_file_size) = '\0';
255
256 return 1;
257}
258
Stephen Warren857291b2014-02-05 20:49:20 -0700259#ifdef CONFIG_CMD_NET
260
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500261#define PXELINUX_DIR "pxelinux.cfg/"
262
263/*
264 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
265 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
266 * from the bootfile path, as described above.
267 *
268 * Returns 1 on success or < 0 on error.
269 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400270static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500271{
272 size_t base_len = strlen(PXELINUX_DIR);
273 char path[MAX_TFTP_PATH_LEN+1];
274
275 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
276 printf("path (%s%s) too long, skipping\n",
277 PXELINUX_DIR, file);
278 return -ENAMETOOLONG;
279 }
280
281 sprintf(path, PXELINUX_DIR "%s", file);
282
Steven Falco7de0fa82013-10-07 09:51:48 -0400283 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500284}
285
286/*
287 * Looks for a pxe file with a name based on the pxeuuid environment variable.
288 *
289 * Returns 1 on success or < 0 on error.
290 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400291static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500292{
293 char *uuid_str;
294
295 uuid_str = from_env("pxeuuid");
296
297 if (!uuid_str)
298 return -ENOENT;
299
Steven Falco7de0fa82013-10-07 09:51:48 -0400300 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500301}
302
303/*
304 * Looks for a pxe file with a name based on the 'ethaddr' environment
305 * variable.
306 *
307 * Returns 1 on success or < 0 on error.
308 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400309static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500310{
311 char mac_str[21];
312 int err;
313
314 err = format_mac_pxe(mac_str, sizeof(mac_str));
315
316 if (err < 0)
317 return err;
318
Steven Falco7de0fa82013-10-07 09:51:48 -0400319 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500320}
321
322/*
323 * Looks for pxe files with names based on our IP address. See pxelinux
324 * documentation for details on what these file names look like. We match
325 * that exactly.
326 *
327 * Returns 1 on success or < 0 on error.
328 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400329static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500330{
331 char ip_addr[9];
332 int mask_pos, err;
333
Joe Hershberger5874dec2015-04-08 01:41:01 -0500334 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500335
336 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400337 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500338
339 if (err > 0)
340 return err;
341
342 ip_addr[mask_pos] = '\0';
343 }
344
345 return -ENOENT;
346}
347
348/*
349 * Entry point for the 'pxe get' command.
350 * This Follows pxelinux's rules to download a config file from a tftp server.
351 * The file is stored at the location given by the pxefile_addr_r environment
352 * variable, which must be set.
353 *
354 * UUID comes from pxeuuid env variable, if defined
355 * MAC addr comes from ethaddr env variable, if defined
356 * IP
357 *
358 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
359 *
360 * Returns 0 on success or 1 on error.
361 */
362static int
363do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
364{
365 char *pxefile_addr_str;
Jason Hobbs9135c792012-03-05 08:12:28 +0000366 unsigned long pxefile_addr_r;
Rob Herringe26e8d62012-12-02 21:00:28 -0600367 int err, i = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500368
Rob Herringeee675f2012-05-25 10:47:39 +0000369 do_getfile = do_get_tftp;
370
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500371 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000372 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500373
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500374 pxefile_addr_str = from_env("pxefile_addr_r");
375
376 if (!pxefile_addr_str)
377 return 1;
378
379 err = strict_strtoul(pxefile_addr_str, 16,
380 (unsigned long *)&pxefile_addr_r);
381 if (err < 0)
382 return 1;
383
384 /*
385 * Keep trying paths until we successfully get a file we're looking
386 * for.
387 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400388 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
389 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
390 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500391 printf("Config file found\n");
392
393 return 0;
394 }
395
Rob Herringe26e8d62012-12-02 21:00:28 -0600396 while (pxe_default_paths[i]) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400397 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herringe26e8d62012-12-02 21:00:28 -0600398 (void *)pxefile_addr_r) > 0) {
399 printf("Config file found\n");
400 return 0;
401 }
402 i++;
403 }
404
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500405 printf("Config file not found\n");
406
407 return 1;
408}
Stephen Warren857291b2014-02-05 20:49:20 -0700409#endif
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500410
411/*
412 * Wrapper to make it easier to store the file at file_path in the location
413 * specified by envaddr_name. file_path will be joined to the bootfile path,
414 * if any is specified.
415 *
416 * Returns 1 on success or < 0 on error.
417 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400418static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500419{
Jason Hobbs9135c792012-03-05 08:12:28 +0000420 unsigned long file_addr;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500421 char *envaddr;
422
423 envaddr = from_env(envaddr_name);
424
425 if (!envaddr)
426 return -ENOENT;
427
Jason Hobbs9135c792012-03-05 08:12:28 +0000428 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500429 return -EINVAL;
430
Steven Falco7de0fa82013-10-07 09:51:48 -0400431 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500432}
433
434/*
435 * A note on the pxe file parser.
436 *
437 * We're parsing files that use syslinux grammar, which has a few quirks.
438 * String literals must be recognized based on context - there is no
439 * quoting or escaping support. There's also nothing to explicitly indicate
440 * when a label section completes. We deal with that by ending a label
441 * section whenever we see a line that doesn't include.
442 *
443 * As with the syslinux family, this same file format could be reused in the
444 * future for non pxe purposes. The only action it takes during parsing that
445 * would throw this off is handling of include files. It assumes we're using
446 * pxe, and does a tftp download of a file listed as an include file in the
447 * middle of the parsing operation. That could be handled by refactoring it to
448 * take a 'include file getter' function.
449 */
450
451/*
452 * Describes a single label given in a pxe file.
453 *
454 * Create these with the 'label_create' function given below.
455 *
456 * name - the name of the menu as given on the 'menu label' line.
457 * kernel - the path to the kernel file to use for this label.
458 * append - kernel command line to use when booting this label
459 * initrd - path to the initrd to use for this label.
460 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
461 * localboot - 1 if this label specified 'localboot', 0 otherwise.
462 * list - lets these form a list, which a pxe_menu struct will hold.
463 */
464struct pxe_label {
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600465 char num[4];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500466 char *name;
Rob Herringa6b74172012-03-28 05:51:34 +0000467 char *menu;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500468 char *kernel;
469 char *append;
470 char *initrd;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000471 char *fdt;
Stephen Warren90d09af2014-01-28 14:50:10 -0700472 char *fdtdir;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600473 int ipappend;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500474 int attempted;
475 int localboot;
Rob Herring58d7f432012-12-02 21:00:22 -0600476 int localboot_val;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500477 struct list_head list;
478};
479
480/*
481 * Describes a pxe menu as given via pxe files.
482 *
483 * title - the name of the menu as given by a 'menu title' line.
484 * default_label - the name of the default label, if any.
485 * timeout - time in tenths of a second to wait for a user key-press before
486 * booting the default label.
487 * prompt - if 0, don't prompt for a choice unless the timeout period is
488 * interrupted. If 1, always prompt for a choice regardless of
489 * timeout.
490 * labels - a list of labels defined for the menu.
491 */
492struct pxe_menu {
493 char *title;
494 char *default_label;
495 int timeout;
496 int prompt;
497 struct list_head labels;
498};
499
500/*
501 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
502 * result must be free()'d to reclaim the memory.
503 *
504 * Returns NULL if malloc fails.
505 */
506static struct pxe_label *label_create(void)
507{
508 struct pxe_label *label;
509
510 label = malloc(sizeof(struct pxe_label));
511
512 if (!label)
513 return NULL;
514
515 memset(label, 0, sizeof(struct pxe_label));
516
517 return label;
518}
519
520/*
521 * Free the memory used by a pxe_label, including that used by its name,
522 * kernel, append and initrd members, if they're non NULL.
523 *
524 * So - be sure to only use dynamically allocated memory for the members of
525 * the pxe_label struct, unless you want to clean it up first. These are
526 * currently only created by the pxe file parsing code.
527 */
528static void label_destroy(struct pxe_label *label)
529{
530 if (label->name)
531 free(label->name);
532
533 if (label->kernel)
534 free(label->kernel);
535
536 if (label->append)
537 free(label->append);
538
539 if (label->initrd)
540 free(label->initrd);
541
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000542 if (label->fdt)
543 free(label->fdt);
544
Stephen Warren90d09af2014-01-28 14:50:10 -0700545 if (label->fdtdir)
546 free(label->fdtdir);
547
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500548 free(label);
549}
550
551/*
552 * Print a label and its string members if they're defined.
553 *
554 * This is passed as a callback to the menu code for displaying each
555 * menu entry.
556 */
557static void label_print(void *data)
558{
559 struct pxe_label *label = data;
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600560 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500561
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600562 printf("%s:\t%s\n", label->num, c);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500563}
564
565/*
566 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
567 * environment variable is defined. Its contents will be executed as U-boot
568 * command. If the label specified an 'append' line, its contents will be
569 * used to overwrite the contents of the 'bootargs' environment variable prior
570 * to running 'localcmd'.
571 *
572 * Returns 1 on success or < 0 on error.
573 */
574static int label_localboot(struct pxe_label *label)
575{
Simon Glassa6642e02012-03-30 21:30:55 +0000576 char *localcmd;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500577
578 localcmd = from_env("localcmd");
579
580 if (!localcmd)
581 return -ENOENT;
582
Hans de Goede55adc412014-08-06 09:37:39 +0200583 if (label->append) {
584 char bootargs[CONFIG_SYS_CBSIZE];
585
586 cli_simple_process_macros(label->append, bootargs);
587 setenv("bootargs", bootargs);
588 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500589
Simon Glassa6642e02012-03-30 21:30:55 +0000590 debug("running: %s\n", localcmd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500591
Simon Glassa6642e02012-03-30 21:30:55 +0000592 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500593}
594
595/*
596 * Boot according to the contents of a pxe_label.
597 *
598 * If we can't boot for any reason, we return. A successful boot never
599 * returns.
600 *
601 * The kernel will be stored in the location given by the 'kernel_addr_r'
602 * environment variable.
603 *
604 * If the label specifies an initrd file, it will be stored in the location
605 * given by the 'ramdisk_addr_r' environment variable.
606 *
607 * If the label specifies an 'append' line, its contents will overwrite that
608 * of the 'bootargs' environment variable.
609 */
Tom Rini5b5b1762013-09-24 09:05:08 -0400610static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500611{
612 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringc0356dc2012-12-03 13:17:21 -0600613 char initrd_str[22];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600614 char mac_str[29] = "";
615 char ip_str[68] = "";
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500616 int bootm_argc = 3;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600617 int len = 0;
Bryan Wua1d715b2014-07-31 17:39:59 -0700618 ulong kernel_addr;
619 void *buf;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500620
621 label_print(label);
622
623 label->attempted = 1;
624
625 if (label->localboot) {
Rob Herring58d7f432012-12-02 21:00:22 -0600626 if (label->localboot_val >= 0)
627 label_localboot(label);
628 return 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500629 }
630
631 if (label->kernel == NULL) {
632 printf("No kernel given, skipping %s\n",
633 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600634 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500635 }
636
637 if (label->initrd) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400638 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500639 printf("Skipping %s for failure retrieving initrd\n",
640 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600641 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500642 }
643
Rob Herringc0356dc2012-12-03 13:17:21 -0600644 bootm_argv[2] = initrd_str;
645 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
646 strcat(bootm_argv[2], ":");
647 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500648 } else {
649 bootm_argv[2] = "-";
650 }
651
Steven Falco7de0fa82013-10-07 09:51:48 -0400652 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500653 printf("Skipping %s for failure retrieving kernel\n",
654 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600655 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500656 }
657
Rob Herring5a3d8be2012-12-02 21:00:29 -0600658 if (label->ipappend & 0x1) {
659 sprintf(ip_str, " ip=%s:%s:%s:%s",
660 getenv("ipaddr"), getenv("serverip"),
661 getenv("gatewayip"), getenv("netmask"));
Rob Herring5a3d8be2012-12-02 21:00:29 -0600662 }
663
Stephen Warren857291b2014-02-05 20:49:20 -0700664#ifdef CONFIG_CMD_NET
Rob Herring5a3d8be2012-12-02 21:00:29 -0600665 if (label->ipappend & 0x2) {
666 int err;
667 strcpy(mac_str, " BOOTIF=");
668 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
669 if (err < 0)
670 mac_str[0] = '\0';
Rob Herring5a3d8be2012-12-02 21:00:29 -0600671 }
Stephen Warren857291b2014-02-05 20:49:20 -0700672#endif
Rob Herring5a3d8be2012-12-02 21:00:29 -0600673
Hans de Goede55adc412014-08-06 09:37:39 +0200674 if ((label->ipappend & 0x3) || label->append) {
675 char bootargs[CONFIG_SYS_CBSIZE] = "";
676 char finalbootargs[CONFIG_SYS_CBSIZE];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600677
Ian Campbell149efb42014-10-03 14:29:01 +0100678 if (strlen(label->append ?: "") +
679 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
680 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
681 strlen(label->append ?: ""),
682 strlen(ip_str), strlen(mac_str),
683 sizeof(bootargs));
684 return 1;
685 }
686
Rob Herring5a3d8be2012-12-02 21:00:29 -0600687 if (label->append)
688 strcpy(bootargs, label->append);
689 strcat(bootargs, ip_str);
690 strcat(bootargs, mac_str);
691
Hans de Goede55adc412014-08-06 09:37:39 +0200692 cli_simple_process_macros(bootargs, finalbootargs);
693 setenv("bootargs", finalbootargs);
694 printf("append: %s\n", finalbootargs);
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600695 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500696
697 bootm_argv[1] = getenv("kernel_addr_r");
698
699 /*
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000700 * fdt usage is optional:
701 * It handles the following scenarios. All scenarios are exclusive
702 *
703 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
704 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
705 * and adjust argc appropriately.
706 *
707 * Scenario 2: If there is an fdt_addr specified, pass it along to
708 * bootm, and adjust argc appropriately.
709 *
710 * Scenario 3: fdt blob is not available.
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500711 */
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000712 bootm_argv[3] = getenv("fdt_addr_r");
713
714 /* if fdt label is defined then get fdt from server */
Stephen Warren90d09af2014-01-28 14:50:10 -0700715 if (bootm_argv[3]) {
716 char *fdtfile = NULL;
717 char *fdtfilefree = NULL;
718
719 if (label->fdt) {
720 fdtfile = label->fdt;
721 } else if (label->fdtdir) {
Stephen Warrenc0592392014-02-12 14:30:04 -0700722 char *f1, *f2, *f3, *f4, *slash;
Stephen Warren90d09af2014-01-28 14:50:10 -0700723
Stephen Warrenc0592392014-02-12 14:30:04 -0700724 f1 = getenv("fdtfile");
725 if (f1) {
726 f2 = "";
727 f3 = "";
728 f4 = "";
729 } else {
730 /*
731 * For complex cases where this code doesn't
732 * generate the correct filename, the board
733 * code should set $fdtfile during early boot,
734 * or the boot scripts should set $fdtfile
735 * before invoking "pxe" or "sysboot".
736 */
737 f1 = getenv("soc");
738 f2 = "-";
739 f3 = getenv("board");
740 f4 = ".dtb";
741 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700742
Stephen Warrenc0592392014-02-12 14:30:04 -0700743 len = strlen(label->fdtdir);
744 if (!len)
745 slash = "./";
746 else if (label->fdtdir[len - 1] != '/')
747 slash = "/";
748 else
749 slash = "";
Stephen Warren90d09af2014-01-28 14:50:10 -0700750
Stephen Warrenc0592392014-02-12 14:30:04 -0700751 len = strlen(label->fdtdir) + strlen(slash) +
752 strlen(f1) + strlen(f2) + strlen(f3) +
753 strlen(f4) + 1;
754 fdtfilefree = malloc(len);
755 if (!fdtfilefree) {
756 printf("malloc fail (FDT filename)\n");
757 return 1;
Stephen Warren90d09af2014-01-28 14:50:10 -0700758 }
Stephen Warrenc0592392014-02-12 14:30:04 -0700759
760 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
761 label->fdtdir, slash, f1, f2, f3, f4);
762 fdtfile = fdtfilefree;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000763 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700764
765 if (fdtfile) {
766 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
767 free(fdtfilefree);
768 if (err < 0) {
769 printf("Skipping %s for failure retrieving fdt\n",
770 label->name);
771 return 1;
772 }
773 } else {
774 bootm_argv[3] = NULL;
775 }
776 }
777
778 if (!bootm_argv[3])
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000779 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500780
781 if (bootm_argv[3])
782 bootm_argc = 4;
783
Bryan Wua1d715b2014-07-31 17:39:59 -0700784 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
785 buf = map_sysmem(kernel_addr, 0);
786 /* Try bootm for legacy and FIT format image */
787 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
788 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600789#ifdef CONFIG_CMD_BOOTZ
Bryan Wua1d715b2014-07-31 17:39:59 -0700790 /* Try booting a zImage */
791 else
792 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600793#endif
Rob Herring58d7f432012-12-02 21:00:22 -0600794 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500795}
796
797/*
798 * Tokens for the pxe file parser.
799 */
800enum token_type {
801 T_EOL,
802 T_STRING,
803 T_EOF,
804 T_MENU,
805 T_TITLE,
806 T_TIMEOUT,
807 T_LABEL,
808 T_KERNEL,
Rob Herringc93f74a2012-03-28 05:51:35 +0000809 T_LINUX,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500810 T_APPEND,
811 T_INITRD,
812 T_LOCALBOOT,
813 T_DEFAULT,
814 T_PROMPT,
815 T_INCLUDE,
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000816 T_FDT,
Stephen Warren90d09af2014-01-28 14:50:10 -0700817 T_FDTDIR,
Rob Herringac995832012-12-02 21:00:27 -0600818 T_ONTIMEOUT,
Rob Herring5a3d8be2012-12-02 21:00:29 -0600819 T_IPAPPEND,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500820 T_INVALID
821};
822
823/*
824 * A token - given by a value and a type.
825 */
826struct token {
827 char *val;
828 enum token_type type;
829};
830
831/*
832 * Keywords recognized.
833 */
834static const struct token keywords[] = {
835 {"menu", T_MENU},
836 {"title", T_TITLE},
837 {"timeout", T_TIMEOUT},
838 {"default", T_DEFAULT},
839 {"prompt", T_PROMPT},
840 {"label", T_LABEL},
841 {"kernel", T_KERNEL},
Rob Herringc93f74a2012-03-28 05:51:35 +0000842 {"linux", T_LINUX},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500843 {"localboot", T_LOCALBOOT},
844 {"append", T_APPEND},
845 {"initrd", T_INITRD},
846 {"include", T_INCLUDE},
Stephen Warren7b472af2014-01-28 14:50:09 -0700847 {"devicetree", T_FDT},
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000848 {"fdt", T_FDT},
Stephen Warren90d09af2014-01-28 14:50:10 -0700849 {"devicetreedir", T_FDTDIR},
850 {"fdtdir", T_FDTDIR},
Rob Herringac995832012-12-02 21:00:27 -0600851 {"ontimeout", T_ONTIMEOUT,},
Rob Herring5a3d8be2012-12-02 21:00:29 -0600852 {"ipappend", T_IPAPPEND,},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500853 {NULL, T_INVALID}
854};
855
856/*
857 * Since pxe(linux) files don't have a token to identify the start of a
858 * literal, we have to keep track of when we're in a state where a literal is
859 * expected vs when we're in a state a keyword is expected.
860 */
861enum lex_state {
862 L_NORMAL = 0,
863 L_KEYWORD,
864 L_SLITERAL
865};
866
867/*
868 * get_string retrieves a string from *p and stores it as a token in
869 * *t.
870 *
871 * get_string used for scanning both string literals and keywords.
872 *
873 * Characters from *p are copied into t-val until a character equal to
874 * delim is found, or a NUL byte is reached. If delim has the special value of
875 * ' ', any whitespace character will be used as a delimiter.
876 *
877 * If lower is unequal to 0, uppercase characters will be converted to
878 * lowercase in the result. This is useful to make keywords case
879 * insensitive.
880 *
881 * The location of *p is updated to point to the first character after the end
882 * of the token - the ending delimiter.
883 *
884 * On success, the new value of t->val is returned. Memory for t->val is
885 * allocated using malloc and must be free()'d to reclaim it. If insufficient
886 * memory is available, NULL is returned.
887 */
888static char *get_string(char **p, struct token *t, char delim, int lower)
889{
890 char *b, *e;
891 size_t len, i;
892
893 /*
894 * b and e both start at the beginning of the input stream.
895 *
896 * e is incremented until we find the ending delimiter, or a NUL byte
897 * is reached. Then, we take e - b to find the length of the token.
898 */
899 b = e = *p;
900
901 while (*e) {
902 if ((delim == ' ' && isspace(*e)) || delim == *e)
903 break;
904 e++;
905 }
906
907 len = e - b;
908
909 /*
910 * Allocate memory to hold the string, and copy it in, converting
911 * characters to lowercase if lower is != 0.
912 */
913 t->val = malloc(len + 1);
914 if (!t->val)
915 return NULL;
916
917 for (i = 0; i < len; i++, b++) {
918 if (lower)
919 t->val[i] = tolower(*b);
920 else
921 t->val[i] = *b;
922 }
923
924 t->val[len] = '\0';
925
926 /*
927 * Update *p so the caller knows where to continue scanning.
928 */
929 *p = e;
930
931 t->type = T_STRING;
932
933 return t->val;
934}
935
936/*
937 * Populate a keyword token with a type and value.
938 */
939static void get_keyword(struct token *t)
940{
941 int i;
942
943 for (i = 0; keywords[i].val; i++) {
944 if (!strcmp(t->val, keywords[i].val)) {
945 t->type = keywords[i].type;
946 break;
947 }
948 }
949}
950
951/*
952 * Get the next token. We have to keep track of which state we're in to know
953 * if we're looking to get a string literal or a keyword.
954 *
955 * *p is updated to point at the first character after the current token.
956 */
957static void get_token(char **p, struct token *t, enum lex_state state)
958{
959 char *c = *p;
960
961 t->type = T_INVALID;
962
963 /* eat non EOL whitespace */
964 while (isblank(*c))
965 c++;
966
967 /*
968 * eat comments. note that string literals can't begin with #, but
969 * can contain a # after their first character.
970 */
971 if (*c == '#') {
972 while (*c && *c != '\n')
973 c++;
974 }
975
976 if (*c == '\n') {
977 t->type = T_EOL;
978 c++;
979 } else if (*c == '\0') {
980 t->type = T_EOF;
981 c++;
982 } else if (state == L_SLITERAL) {
983 get_string(&c, t, '\n', 0);
984 } else if (state == L_KEYWORD) {
985 /*
986 * when we expect a keyword, we first get the next string
987 * token delimited by whitespace, and then check if it
988 * matches a keyword in our keyword list. if it does, it's
989 * converted to a keyword token of the appropriate type, and
990 * if not, it remains a string token.
991 */
992 get_string(&c, t, ' ', 1);
993 get_keyword(t);
994 }
995
996 *p = c;
997}
998
999/*
1000 * Increment *c until we get to the end of the current line, or EOF.
1001 */
1002static void eol_or_eof(char **c)
1003{
1004 while (**c && **c != '\n')
1005 (*c)++;
1006}
1007
1008/*
1009 * All of these parse_* functions share some common behavior.
1010 *
1011 * They finish with *c pointing after the token they parse, and return 1 on
1012 * success, or < 0 on error.
1013 */
1014
1015/*
1016 * Parse a string literal and store a pointer it at *dst. String literals
1017 * terminate at the end of the line.
1018 */
1019static int parse_sliteral(char **c, char **dst)
1020{
1021 struct token t;
1022 char *s = *c;
1023
1024 get_token(c, &t, L_SLITERAL);
1025
1026 if (t.type != T_STRING) {
1027 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1028 return -EINVAL;
1029 }
1030
1031 *dst = t.val;
1032
1033 return 1;
1034}
1035
1036/*
1037 * Parse a base 10 (unsigned) integer and store it at *dst.
1038 */
1039static int parse_integer(char **c, int *dst)
1040{
1041 struct token t;
1042 char *s = *c;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001043
1044 get_token(c, &t, L_SLITERAL);
1045
1046 if (t.type != T_STRING) {
1047 printf("Expected string: %.*s\n", (int)(*c - s), s);
1048 return -EINVAL;
1049 }
1050
Rob Herring58d7f432012-12-02 21:00:22 -06001051 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001052
1053 free(t.val);
1054
1055 return 1;
1056}
1057
Steven Falco7de0fa82013-10-07 09:51:48 -04001058static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001059
1060/*
1061 * Parse an include statement, and retrieve and parse the file it mentions.
1062 *
1063 * base should point to a location where it's safe to store the file, and
1064 * nest_level should indicate how many nested includes have occurred. For this
1065 * include, nest_level has already been incremented and doesn't need to be
1066 * incremented here.
1067 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001068static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001069 struct pxe_menu *cfg, int nest_level)
1070{
1071 char *include_path;
1072 char *s = *c;
1073 int err;
1074
1075 err = parse_sliteral(c, &include_path);
1076
1077 if (err < 0) {
1078 printf("Expected include path: %.*s\n",
1079 (int)(*c - s), s);
1080 return err;
1081 }
1082
Steven Falco7de0fa82013-10-07 09:51:48 -04001083 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001084
1085 if (err < 0) {
1086 printf("Couldn't retrieve %s\n", include_path);
1087 return err;
1088 }
1089
Steven Falco7de0fa82013-10-07 09:51:48 -04001090 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001091}
1092
1093/*
1094 * Parse lines that begin with 'menu'.
1095 *
1096 * b and nest are provided to handle the 'menu include' case.
1097 *
1098 * b should be the address where the file currently being parsed is stored.
1099 *
1100 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1101 * a file it includes, 3 when parsing a file included by that file, and so on.
1102 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001103static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001104{
1105 struct token t;
1106 char *s = *c;
Heiko Schocher9cd01dd2011-12-12 20:37:17 +00001107 int err = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001108
1109 get_token(c, &t, L_KEYWORD);
1110
1111 switch (t.type) {
1112 case T_TITLE:
1113 err = parse_sliteral(c, &cfg->title);
1114
1115 break;
1116
1117 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001118 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001119 nest_level + 1);
1120 break;
1121
1122 default:
1123 printf("Ignoring malformed menu command: %.*s\n",
1124 (int)(*c - s), s);
1125 }
1126
1127 if (err < 0)
1128 return err;
1129
1130 eol_or_eof(c);
1131
1132 return 1;
1133}
1134
1135/*
1136 * Handles parsing a 'menu line' when we're parsing a label.
1137 */
1138static int parse_label_menu(char **c, struct pxe_menu *cfg,
1139 struct pxe_label *label)
1140{
1141 struct token t;
1142 char *s;
1143
1144 s = *c;
1145
1146 get_token(c, &t, L_KEYWORD);
1147
1148 switch (t.type) {
1149 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001150 if (!cfg->default_label)
1151 cfg->default_label = strdup(label->name);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001152
1153 if (!cfg->default_label)
1154 return -ENOMEM;
1155
1156 break;
Rob Herringa6b74172012-03-28 05:51:34 +00001157 case T_LABEL:
1158 parse_sliteral(c, &label->menu);
1159 break;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001160 default:
1161 printf("Ignoring malformed menu command: %.*s\n",
1162 (int)(*c - s), s);
1163 }
1164
1165 eol_or_eof(c);
1166
1167 return 0;
1168}
1169
1170/*
1171 * Parses a label and adds it to the list of labels for a menu.
1172 *
1173 * A label ends when we either get to the end of a file, or
1174 * get some input we otherwise don't have a handler defined
1175 * for.
1176 *
1177 */
1178static int parse_label(char **c, struct pxe_menu *cfg)
1179{
1180 struct token t;
Rob Herringd8fceec2012-03-28 05:51:37 +00001181 int len;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001182 char *s = *c;
1183 struct pxe_label *label;
1184 int err;
1185
1186 label = label_create();
1187 if (!label)
1188 return -ENOMEM;
1189
1190 err = parse_sliteral(c, &label->name);
1191 if (err < 0) {
1192 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1193 label_destroy(label);
1194 return -EINVAL;
1195 }
1196
1197 list_add_tail(&label->list, &cfg->labels);
1198
1199 while (1) {
1200 s = *c;
1201 get_token(c, &t, L_KEYWORD);
1202
1203 err = 0;
1204 switch (t.type) {
1205 case T_MENU:
1206 err = parse_label_menu(c, cfg, label);
1207 break;
1208
1209 case T_KERNEL:
Rob Herringc93f74a2012-03-28 05:51:35 +00001210 case T_LINUX:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001211 err = parse_sliteral(c, &label->kernel);
1212 break;
1213
1214 case T_APPEND:
1215 err = parse_sliteral(c, &label->append);
Rob Herringd8fceec2012-03-28 05:51:37 +00001216 if (label->initrd)
1217 break;
1218 s = strstr(label->append, "initrd=");
1219 if (!s)
1220 break;
1221 s += 7;
1222 len = (int)(strchr(s, ' ') - s);
1223 label->initrd = malloc(len + 1);
1224 strncpy(label->initrd, s, len);
1225 label->initrd[len] = '\0';
1226
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001227 break;
1228
1229 case T_INITRD:
Rob Herringd8fceec2012-03-28 05:51:37 +00001230 if (!label->initrd)
1231 err = parse_sliteral(c, &label->initrd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001232 break;
1233
Chander Kashyap1644a9d2012-09-06 19:36:31 +00001234 case T_FDT:
1235 if (!label->fdt)
1236 err = parse_sliteral(c, &label->fdt);
1237 break;
1238
Stephen Warren90d09af2014-01-28 14:50:10 -07001239 case T_FDTDIR:
1240 if (!label->fdtdir)
1241 err = parse_sliteral(c, &label->fdtdir);
1242 break;
1243
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001244 case T_LOCALBOOT:
Rob Herring58d7f432012-12-02 21:00:22 -06001245 label->localboot = 1;
1246 err = parse_integer(c, &label->localboot_val);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001247 break;
1248
Rob Herring5a3d8be2012-12-02 21:00:29 -06001249 case T_IPAPPEND:
1250 err = parse_integer(c, &label->ipappend);
1251 break;
1252
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001253 case T_EOL:
1254 break;
1255
1256 default:
1257 /*
1258 * put the token back! we don't want it - it's the end
1259 * of a label and whatever token this is, it's
1260 * something for the menu level context to handle.
1261 */
1262 *c = s;
1263 return 1;
1264 }
1265
1266 if (err < 0)
1267 return err;
1268 }
1269}
1270
1271/*
1272 * This 16 comes from the limit pxelinux imposes on nested includes.
1273 *
1274 * There is no reason at all we couldn't do more, but some limit helps prevent
1275 * infinite (until crash occurs) recursion if a file tries to include itself.
1276 */
1277#define MAX_NEST_LEVEL 16
1278
1279/*
1280 * Entry point for parsing a menu file. nest_level indicates how many times
1281 * we've nested in includes. It will be 1 for the top level menu file.
1282 *
1283 * Returns 1 on success, < 0 on error.
1284 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001285static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001286{
1287 struct token t;
1288 char *s, *b, *label_name;
1289 int err;
1290
1291 b = p;
1292
1293 if (nest_level > MAX_NEST_LEVEL) {
1294 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1295 return -EMLINK;
1296 }
1297
1298 while (1) {
1299 s = p;
1300
1301 get_token(&p, &t, L_KEYWORD);
1302
1303 err = 0;
1304 switch (t.type) {
1305 case T_MENU:
Rob Herringf0898b92012-12-02 21:00:25 -06001306 cfg->prompt = 1;
Steven Falco7de0fa82013-10-07 09:51:48 -04001307 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001308 break;
1309
1310 case T_TIMEOUT:
1311 err = parse_integer(&p, &cfg->timeout);
1312 break;
1313
1314 case T_LABEL:
1315 err = parse_label(&p, cfg);
1316 break;
1317
1318 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001319 case T_ONTIMEOUT:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001320 err = parse_sliteral(&p, &label_name);
1321
1322 if (label_name) {
1323 if (cfg->default_label)
1324 free(cfg->default_label);
1325
1326 cfg->default_label = label_name;
1327 }
1328
1329 break;
1330
Rob Herring8e5abb02012-05-25 10:43:16 +00001331 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001332 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring8e5abb02012-05-25 10:43:16 +00001333 nest_level + 1);
1334 break;
1335
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001336 case T_PROMPT:
Rob Herringf0898b92012-12-02 21:00:25 -06001337 eol_or_eof(&p);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001338 break;
1339
1340 case T_EOL:
1341 break;
1342
1343 case T_EOF:
1344 return 1;
1345
1346 default:
1347 printf("Ignoring unknown command: %.*s\n",
1348 (int)(p - s), s);
1349 eol_or_eof(&p);
1350 }
1351
1352 if (err < 0)
1353 return err;
1354 }
1355}
1356
1357/*
1358 * Free the memory used by a pxe_menu and its labels.
1359 */
1360static void destroy_pxe_menu(struct pxe_menu *cfg)
1361{
1362 struct list_head *pos, *n;
1363 struct pxe_label *label;
1364
1365 if (cfg->title)
1366 free(cfg->title);
1367
1368 if (cfg->default_label)
1369 free(cfg->default_label);
1370
1371 list_for_each_safe(pos, n, &cfg->labels) {
1372 label = list_entry(pos, struct pxe_label, list);
1373
1374 label_destroy(label);
1375 }
1376
1377 free(cfg);
1378}
1379
1380/*
1381 * Entry point for parsing a pxe file. This is only used for the top level
1382 * file.
1383 *
1384 * Returns NULL if there is an error, otherwise, returns a pointer to a
1385 * pxe_menu struct populated with the results of parsing the pxe file (and any
1386 * files it includes). The resulting pxe_menu struct can be free()'d by using
1387 * the destroy_pxe_menu() function.
1388 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001389static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001390{
1391 struct pxe_menu *cfg;
1392
1393 cfg = malloc(sizeof(struct pxe_menu));
1394
1395 if (!cfg)
1396 return NULL;
1397
1398 memset(cfg, 0, sizeof(struct pxe_menu));
1399
1400 INIT_LIST_HEAD(&cfg->labels);
1401
Steven Falco7de0fa82013-10-07 09:51:48 -04001402 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001403 destroy_pxe_menu(cfg);
1404 return NULL;
1405 }
1406
1407 return cfg;
1408}
1409
1410/*
1411 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1412 * menu code.
1413 */
1414static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1415{
1416 struct pxe_label *label;
1417 struct list_head *pos;
1418 struct menu *m;
1419 int err;
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001420 int i = 1;
1421 char *default_num = NULL;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001422
1423 /*
1424 * Create a menu and add items for all the labels.
1425 */
Pali Rohárd0d8d3b2013-03-23 14:50:40 +00001426 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1427 NULL, NULL);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001428
1429 if (!m)
1430 return NULL;
1431
1432 list_for_each(pos, &cfg->labels) {
1433 label = list_entry(pos, struct pxe_label, list);
1434
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001435 sprintf(label->num, "%d", i++);
1436 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001437 menu_destroy(m);
1438 return NULL;
1439 }
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001440 if (cfg->default_label &&
Rob Herringac995832012-12-02 21:00:27 -06001441 (strcmp(label->name, cfg->default_label) == 0))
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001442 default_num = label->num;
1443
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001444 }
1445
1446 /*
1447 * After we've created items for each label in the menu, set the
1448 * menu's default label if one was specified.
1449 */
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001450 if (default_num) {
1451 err = menu_default_set(m, default_num);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001452 if (err != 1) {
1453 if (err != -ENOENT) {
1454 menu_destroy(m);
1455 return NULL;
1456 }
1457
1458 printf("Missing default: %s\n", cfg->default_label);
1459 }
1460 }
1461
1462 return m;
1463}
1464
1465/*
1466 * Try to boot any labels we have yet to attempt to boot.
1467 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001468static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001469{
1470 struct list_head *pos;
1471 struct pxe_label *label;
1472
1473 list_for_each(pos, &cfg->labels) {
1474 label = list_entry(pos, struct pxe_label, list);
1475
1476 if (!label->attempted)
Tom Rini5b5b1762013-09-24 09:05:08 -04001477 label_boot(cmdtp, label);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001478 }
1479}
1480
1481/*
1482 * Boot the system as prescribed by a pxe_menu.
1483 *
1484 * Use the menu system to either get the user's choice or the default, based
1485 * on config or user input. If there is no default or user's choice,
1486 * attempted to boot labels in the order they were given in pxe files.
1487 * If the default or user's choice fails to boot, attempt to boot other
1488 * labels in the order they were given in pxe files.
1489 *
1490 * If this function returns, there weren't any labels that successfully
1491 * booted, or the user interrupted the menu selection via ctrl+c.
1492 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001493static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001494{
1495 void *choice;
1496 struct menu *m;
1497 int err;
1498
1499 m = pxe_menu_to_menu(cfg);
1500 if (!m)
1501 return;
1502
1503 err = menu_get_choice(m, &choice);
1504
1505 menu_destroy(m);
1506
Jason Hobbs083b7a62011-11-07 03:07:15 +00001507 /*
1508 * err == 1 means we got a choice back from menu_get_choice.
1509 *
1510 * err == -ENOENT if the menu was setup to select the default but no
1511 * default was set. in that case, we should continue trying to boot
1512 * labels that haven't been attempted yet.
1513 *
1514 * otherwise, the user interrupted or there was some other error and
1515 * we give up.
1516 */
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001517
Rob Herring58d7f432012-12-02 21:00:22 -06001518 if (err == 1) {
Tom Rini5b5b1762013-09-24 09:05:08 -04001519 err = label_boot(cmdtp, choice);
Rob Herring58d7f432012-12-02 21:00:22 -06001520 if (!err)
1521 return;
1522 } else if (err != -ENOENT) {
Jason Hobbs083b7a62011-11-07 03:07:15 +00001523 return;
Rob Herring58d7f432012-12-02 21:00:22 -06001524 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001525
Tom Rini5b5b1762013-09-24 09:05:08 -04001526 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001527}
1528
Stephen Warren857291b2014-02-05 20:49:20 -07001529#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001530/*
1531 * Boots a system using a pxe file
1532 *
1533 * Returns 0 on success, 1 on error.
1534 */
1535static int
1536do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1537{
1538 unsigned long pxefile_addr_r;
1539 struct pxe_menu *cfg;
1540 char *pxefile_addr_str;
1541
Rob Herringeee675f2012-05-25 10:47:39 +00001542 do_getfile = do_get_tftp;
1543
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001544 if (argc == 1) {
1545 pxefile_addr_str = from_env("pxefile_addr_r");
1546 if (!pxefile_addr_str)
1547 return 1;
1548
1549 } else if (argc == 2) {
1550 pxefile_addr_str = argv[1];
1551 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +00001552 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001553 }
1554
1555 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1556 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1557 return 1;
1558 }
1559
Steven Falco7de0fa82013-10-07 09:51:48 -04001560 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001561
1562 if (cfg == NULL) {
1563 printf("Error parsing config file\n");
1564 return 1;
1565 }
1566
Tom Rini5b5b1762013-09-24 09:05:08 -04001567 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001568
1569 destroy_pxe_menu(cfg);
1570
Joe Hershberger290c8992015-04-08 01:41:02 -05001571 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
Stephen Warrenc24cbc22014-07-22 18:06:46 -06001572
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001573 return 0;
1574}
1575
1576static cmd_tbl_t cmd_pxe_sub[] = {
1577 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1578 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1579};
1580
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +02001581static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001582{
1583 cmd_tbl_t *cp;
1584
1585 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001586 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001587
Rob Herring759456e2013-10-18 13:04:42 -05001588 is_pxe = true;
1589
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001590 /* drop initial "pxe" arg */
1591 argc--;
1592 argv++;
1593
1594 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1595
1596 if (cp)
1597 return cp->cmd(cmdtp, flag, argc, argv);
1598
Simon Glassa06dfc72011-12-10 08:44:01 +00001599 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001600}
1601
1602U_BOOT_CMD(
1603 pxe, 3, 1, do_pxe,
1604 "commands to get and boot from pxe files",
1605 "get - try to retrieve a pxe file using tftp\npxe "
1606 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1607);
Stephen Warren857291b2014-02-05 20:49:20 -07001608#endif
Rob Herringeee675f2012-05-25 10:47:39 +00001609
1610/*
1611 * Boots a system using a local disk syslinux/extlinux file
1612 *
1613 * Returns 0 on success, 1 on error.
1614 */
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +02001615static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Rob Herringeee675f2012-05-25 10:47:39 +00001616{
1617 unsigned long pxefile_addr_r;
1618 struct pxe_menu *cfg;
1619 char *pxefile_addr_str;
1620 char *filename;
1621 int prompt = 0;
1622
Rob Herring759456e2013-10-18 13:04:42 -05001623 is_pxe = false;
1624
Rob Herringeee675f2012-05-25 10:47:39 +00001625 if (strstr(argv[1], "-p")) {
1626 prompt = 1;
1627 argc--;
1628 argv++;
1629 }
1630
1631 if (argc < 4)
1632 return cmd_usage(cmdtp);
1633
1634 if (argc < 5) {
1635 pxefile_addr_str = from_env("pxefile_addr_r");
1636 if (!pxefile_addr_str)
1637 return 1;
1638 } else {
1639 pxefile_addr_str = argv[4];
1640 }
1641
1642 if (argc < 6)
1643 filename = getenv("bootfile");
1644 else {
1645 filename = argv[5];
1646 setenv("bootfile", filename);
1647 }
1648
1649 if (strstr(argv[3], "ext2"))
1650 do_getfile = do_get_ext2;
1651 else if (strstr(argv[3], "fat"))
1652 do_getfile = do_get_fat;
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001653 else if (strstr(argv[3], "any"))
1654 do_getfile = do_get_any;
Rob Herringeee675f2012-05-25 10:47:39 +00001655 else {
1656 printf("Invalid filesystem: %s\n", argv[3]);
1657 return 1;
1658 }
1659 fs_argv[1] = argv[1];
1660 fs_argv[2] = argv[2];
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
Steven Falco7de0fa82013-10-07 09:51:48 -04001667 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herringeee675f2012-05-25 10:47:39 +00001668 printf("Error reading config file\n");
1669 return 1;
1670 }
1671
Steven Falco7de0fa82013-10-07 09:51:48 -04001672 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herringeee675f2012-05-25 10:47:39 +00001673
1674 if (cfg == NULL) {
1675 printf("Error parsing config file\n");
1676 return 1;
1677 }
1678
1679 if (prompt)
1680 cfg->prompt = 1;
1681
Tom Rini5b5b1762013-09-24 09:05:08 -04001682 handle_pxe_menu(cmdtp, cfg);
Rob Herringeee675f2012-05-25 10:47:39 +00001683
1684 destroy_pxe_menu(cfg);
1685
1686 return 0;
1687}
1688
1689U_BOOT_CMD(
1690 sysboot, 7, 1, do_sysboot,
1691 "command to get and boot from syslinux files",
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001692 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1693 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1694 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herringeee675f2012-05-25 10:47:39 +00001695);