blob: 29e48db20416cab3ec6fd881a98f68b6ff061a59 [file] [log] [blame]
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
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>
9#include <malloc.h>
10#include <linux/string.h>
11#include <linux/ctype.h>
12#include <errno.h>
13#include <linux/list.h>
Dennis Gilmorecd90dc82014-02-04 05:25:46 -060014#include <fs.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050015
16#include "menu.h"
17
18#define MAX_TFTP_PATH_LEN 127
19
Rob Herringe26e8d62012-12-02 21:00:28 -060020const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050021#ifdef CONFIG_SYS_SOC
Rob Herringe26e8d62012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050023#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060024 "default-" CONFIG_SYS_ARCH,
25 "default",
26 NULL
27};
28
Rob Herring759456e2013-10-18 13:04:42 -050029static bool is_pxe;
30
Jason Hobbs0e3a5932011-08-31 10:37:30 -050031/*
32 * Like getenv, but prints an error if envvar isn't defined in the
33 * environment. It always returns what getenv does, so it can be used in
34 * place of getenv without changing error handling otherwise.
35 */
Rob Herring824901c2012-12-02 21:00:21 -060036static char *from_env(const char *envvar)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050037{
38 char *ret;
39
40 ret = getenv(envvar);
41
42 if (!ret)
43 printf("missing environment variable: %s\n", envvar);
44
45 return ret;
46}
47
48/*
49 * Convert an ethaddr from the environment to the format used by pxelinux
50 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
51 * the beginning of the ethernet address to indicate a hardware type of
52 * Ethernet. Also converts uppercase hex characters into lowercase, to match
53 * pxelinux's behavior.
54 *
55 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
56 * environment, or some other value < 0 on error.
57 */
58static int format_mac_pxe(char *outbuf, size_t outbuf_len)
59{
Rob Herringdd248b32012-12-02 21:00:20 -060060 uchar ethaddr[6];
Jason Hobbs0e3a5932011-08-31 10:37:30 -050061
Rob Herringdd248b32012-12-02 21:00:20 -060062 if (outbuf_len < 21) {
David Feng4ec50292013-12-14 11:47:30 +080063 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050064
65 return -EINVAL;
66 }
67
Rob Herringdd248b32012-12-02 21:00:20 -060068 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
69 ethaddr))
70 return -ENOENT;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050071
Rob Herringdd248b32012-12-02 21:00:20 -060072 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
73 ethaddr[0], ethaddr[1], ethaddr[2],
74 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050075
76 return 1;
77}
78
79/*
80 * Returns the directory the file specified in the bootfile env variable is
81 * in. If bootfile isn't defined in the environment, return NULL, which should
82 * be interpreted as "don't prepend anything to paths".
83 */
Rob Herringdc410cd2012-03-28 05:51:36 +000084static int get_bootfile_path(const char *file_path, char *bootfile_path,
85 size_t bootfile_path_size)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050086{
87 char *bootfile, *last_slash;
Rob Herringdc410cd2012-03-28 05:51:36 +000088 size_t path_len = 0;
89
Rob Herring759456e2013-10-18 13:04:42 -050090 /* Only syslinux allows absolute paths */
91 if (file_path[0] == '/' && !is_pxe)
Rob Herringdc410cd2012-03-28 05:51:36 +000092 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050093
94 bootfile = from_env("bootfile");
95
Rob Herringdc410cd2012-03-28 05:51:36 +000096 if (!bootfile)
97 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050098
99 last_slash = strrchr(bootfile, '/');
100
Rob Herringdc410cd2012-03-28 05:51:36 +0000101 if (last_slash == NULL)
102 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500103
104 path_len = (last_slash - bootfile) + 1;
105
106 if (bootfile_path_size < path_len) {
David Feng4ec50292013-12-14 11:47:30 +0800107 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500108 bootfile_path_size, path_len);
109
110 return -1;
111 }
112
113 strncpy(bootfile_path, bootfile, path_len);
114
Rob Herringdc410cd2012-03-28 05:51:36 +0000115 ret:
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500116 bootfile_path[path_len] = '\0';
117
118 return 1;
119}
120
Steven Falco7de0fa82013-10-07 09:51:48 -0400121static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herringeee675f2012-05-25 10:47:39 +0000122
Steven Falco7de0fa82013-10-07 09:51:48 -0400123static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000124{
125 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
126
127 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600128 tftp_argv[2] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000129
Steven Falco7de0fa82013-10-07 09:51:48 -0400130 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000131 return -ENOENT;
132
133 return 1;
134}
135
136static char *fs_argv[5];
137
Steven Falco7de0fa82013-10-07 09:51:48 -0400138static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000139{
140#ifdef CONFIG_CMD_EXT2
141 fs_argv[0] = "ext2load";
142 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600143 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000144
Steven Falco7de0fa82013-10-07 09:51:48 -0400145 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000146 return 1;
147#endif
148 return -ENOENT;
149}
150
Steven Falco7de0fa82013-10-07 09:51:48 -0400151static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000152{
153#ifdef CONFIG_CMD_FAT
154 fs_argv[0] = "fatload";
155 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600156 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000157
Steven Falco7de0fa82013-10-07 09:51:48 -0400158 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000159 return 1;
160#endif
161 return -ENOENT;
162}
163
Dennis Gilmorecd90dc82014-02-04 05:25:46 -0600164static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
165{
166#ifdef CONFIG_CMD_FS_GENERIC
167 fs_argv[0] = "load";
168 fs_argv[3] = file_addr;
169 fs_argv[4] = (void *)file_path;
170
171 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
172 return 1;
173#endif
174 return -ENOENT;
175}
176
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500177/*
178 * As in pxelinux, paths to files referenced from files we retrieve are
179 * relative to the location of bootfile. get_relfile takes such a path and
180 * joins it with the bootfile path to get the full path to the target file. If
181 * the bootfile path is NULL, we use file_path as is.
182 *
183 * Returns 1 for success, or < 0 on error.
184 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400185static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500186{
187 size_t path_len;
188 char relfile[MAX_TFTP_PATH_LEN+1];
189 char addr_buf[10];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500190 int err;
191
Rob Herringdc410cd2012-03-28 05:51:36 +0000192 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500193
194 if (err < 0)
195 return err;
196
197 path_len = strlen(file_path);
198 path_len += strlen(relfile);
199
200 if (path_len > MAX_TFTP_PATH_LEN) {
201 printf("Base path too long (%s%s)\n",
202 relfile,
203 file_path);
204
205 return -ENAMETOOLONG;
206 }
207
208 strcat(relfile, file_path);
209
210 printf("Retrieving file: %s\n", relfile);
211
212 sprintf(addr_buf, "%p", file_addr);
213
Steven Falco7de0fa82013-10-07 09:51:48 -0400214 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500215}
216
217/*
218 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
219 * 'bootfile' was specified in the environment, the path to bootfile will be
220 * prepended to 'file_path' and the resulting path will be used.
221 *
222 * Returns 1 on success, or < 0 for error.
223 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400224static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500225{
226 unsigned long config_file_size;
227 char *tftp_filesize;
228 int err;
229
Steven Falco7de0fa82013-10-07 09:51:48 -0400230 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500231
232 if (err < 0)
233 return err;
234
235 /*
236 * the file comes without a NUL byte at the end, so find out its size
237 * and add the NUL byte.
238 */
239 tftp_filesize = from_env("filesize");
240
241 if (!tftp_filesize)
242 return -ENOENT;
243
244 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
245 return -EINVAL;
246
247 *(char *)(file_addr + config_file_size) = '\0';
248
249 return 1;
250}
251
252#define PXELINUX_DIR "pxelinux.cfg/"
253
254/*
255 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
256 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
257 * from the bootfile path, as described above.
258 *
259 * Returns 1 on success or < 0 on error.
260 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400261static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500262{
263 size_t base_len = strlen(PXELINUX_DIR);
264 char path[MAX_TFTP_PATH_LEN+1];
265
266 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
267 printf("path (%s%s) too long, skipping\n",
268 PXELINUX_DIR, file);
269 return -ENAMETOOLONG;
270 }
271
272 sprintf(path, PXELINUX_DIR "%s", file);
273
Steven Falco7de0fa82013-10-07 09:51:48 -0400274 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500275}
276
277/*
278 * Looks for a pxe file with a name based on the pxeuuid environment variable.
279 *
280 * Returns 1 on success or < 0 on error.
281 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400282static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500283{
284 char *uuid_str;
285
286 uuid_str = from_env("pxeuuid");
287
288 if (!uuid_str)
289 return -ENOENT;
290
Steven Falco7de0fa82013-10-07 09:51:48 -0400291 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500292}
293
294/*
295 * Looks for a pxe file with a name based on the 'ethaddr' environment
296 * variable.
297 *
298 * Returns 1 on success or < 0 on error.
299 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400300static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500301{
302 char mac_str[21];
303 int err;
304
305 err = format_mac_pxe(mac_str, sizeof(mac_str));
306
307 if (err < 0)
308 return err;
309
Steven Falco7de0fa82013-10-07 09:51:48 -0400310 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500311}
312
313/*
314 * Looks for pxe files with names based on our IP address. See pxelinux
315 * documentation for details on what these file names look like. We match
316 * that exactly.
317 *
318 * Returns 1 on success or < 0 on error.
319 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400320static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500321{
322 char ip_addr[9];
323 int mask_pos, err;
324
325 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
326
327 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400328 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500329
330 if (err > 0)
331 return err;
332
333 ip_addr[mask_pos] = '\0';
334 }
335
336 return -ENOENT;
337}
338
339/*
340 * Entry point for the 'pxe get' command.
341 * This Follows pxelinux's rules to download a config file from a tftp server.
342 * The file is stored at the location given by the pxefile_addr_r environment
343 * variable, which must be set.
344 *
345 * UUID comes from pxeuuid env variable, if defined
346 * MAC addr comes from ethaddr env variable, if defined
347 * IP
348 *
349 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
350 *
351 * Returns 0 on success or 1 on error.
352 */
353static int
354do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
355{
356 char *pxefile_addr_str;
Jason Hobbs9135c792012-03-05 08:12:28 +0000357 unsigned long pxefile_addr_r;
Rob Herringe26e8d62012-12-02 21:00:28 -0600358 int err, i = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500359
Rob Herringeee675f2012-05-25 10:47:39 +0000360 do_getfile = do_get_tftp;
361
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500362 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000363 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500364
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500365 pxefile_addr_str = from_env("pxefile_addr_r");
366
367 if (!pxefile_addr_str)
368 return 1;
369
370 err = strict_strtoul(pxefile_addr_str, 16,
371 (unsigned long *)&pxefile_addr_r);
372 if (err < 0)
373 return 1;
374
375 /*
376 * Keep trying paths until we successfully get a file we're looking
377 * for.
378 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400379 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
380 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
381 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500382 printf("Config file found\n");
383
384 return 0;
385 }
386
Rob Herringe26e8d62012-12-02 21:00:28 -0600387 while (pxe_default_paths[i]) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400388 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herringe26e8d62012-12-02 21:00:28 -0600389 (void *)pxefile_addr_r) > 0) {
390 printf("Config file found\n");
391 return 0;
392 }
393 i++;
394 }
395
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500396 printf("Config file not found\n");
397
398 return 1;
399}
400
401/*
402 * Wrapper to make it easier to store the file at file_path in the location
403 * specified by envaddr_name. file_path will be joined to the bootfile path,
404 * if any is specified.
405 *
406 * Returns 1 on success or < 0 on error.
407 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400408static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500409{
Jason Hobbs9135c792012-03-05 08:12:28 +0000410 unsigned long file_addr;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500411 char *envaddr;
412
413 envaddr = from_env(envaddr_name);
414
415 if (!envaddr)
416 return -ENOENT;
417
Jason Hobbs9135c792012-03-05 08:12:28 +0000418 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500419 return -EINVAL;
420
Steven Falco7de0fa82013-10-07 09:51:48 -0400421 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500422}
423
424/*
425 * A note on the pxe file parser.
426 *
427 * We're parsing files that use syslinux grammar, which has a few quirks.
428 * String literals must be recognized based on context - there is no
429 * quoting or escaping support. There's also nothing to explicitly indicate
430 * when a label section completes. We deal with that by ending a label
431 * section whenever we see a line that doesn't include.
432 *
433 * As with the syslinux family, this same file format could be reused in the
434 * future for non pxe purposes. The only action it takes during parsing that
435 * would throw this off is handling of include files. It assumes we're using
436 * pxe, and does a tftp download of a file listed as an include file in the
437 * middle of the parsing operation. That could be handled by refactoring it to
438 * take a 'include file getter' function.
439 */
440
441/*
442 * Describes a single label given in a pxe file.
443 *
444 * Create these with the 'label_create' function given below.
445 *
446 * name - the name of the menu as given on the 'menu label' line.
447 * kernel - the path to the kernel file to use for this label.
448 * append - kernel command line to use when booting this label
449 * initrd - path to the initrd to use for this label.
450 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
451 * localboot - 1 if this label specified 'localboot', 0 otherwise.
452 * list - lets these form a list, which a pxe_menu struct will hold.
453 */
454struct pxe_label {
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600455 char num[4];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500456 char *name;
Rob Herringa6b74172012-03-28 05:51:34 +0000457 char *menu;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500458 char *kernel;
459 char *append;
460 char *initrd;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000461 char *fdt;
Stephen Warren90d09af2014-01-28 14:50:10 -0700462 char *fdtdir;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600463 int ipappend;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500464 int attempted;
465 int localboot;
Rob Herring58d7f432012-12-02 21:00:22 -0600466 int localboot_val;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500467 struct list_head list;
468};
469
470/*
471 * Describes a pxe menu as given via pxe files.
472 *
473 * title - the name of the menu as given by a 'menu title' line.
474 * default_label - the name of the default label, if any.
475 * timeout - time in tenths of a second to wait for a user key-press before
476 * booting the default label.
477 * prompt - if 0, don't prompt for a choice unless the timeout period is
478 * interrupted. If 1, always prompt for a choice regardless of
479 * timeout.
480 * labels - a list of labels defined for the menu.
481 */
482struct pxe_menu {
483 char *title;
484 char *default_label;
485 int timeout;
486 int prompt;
487 struct list_head labels;
488};
489
490/*
491 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
492 * result must be free()'d to reclaim the memory.
493 *
494 * Returns NULL if malloc fails.
495 */
496static struct pxe_label *label_create(void)
497{
498 struct pxe_label *label;
499
500 label = malloc(sizeof(struct pxe_label));
501
502 if (!label)
503 return NULL;
504
505 memset(label, 0, sizeof(struct pxe_label));
506
507 return label;
508}
509
510/*
511 * Free the memory used by a pxe_label, including that used by its name,
512 * kernel, append and initrd members, if they're non NULL.
513 *
514 * So - be sure to only use dynamically allocated memory for the members of
515 * the pxe_label struct, unless you want to clean it up first. These are
516 * currently only created by the pxe file parsing code.
517 */
518static void label_destroy(struct pxe_label *label)
519{
520 if (label->name)
521 free(label->name);
522
523 if (label->kernel)
524 free(label->kernel);
525
526 if (label->append)
527 free(label->append);
528
529 if (label->initrd)
530 free(label->initrd);
531
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000532 if (label->fdt)
533 free(label->fdt);
534
Stephen Warren90d09af2014-01-28 14:50:10 -0700535 if (label->fdtdir)
536 free(label->fdtdir);
537
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500538 free(label);
539}
540
541/*
542 * Print a label and its string members if they're defined.
543 *
544 * This is passed as a callback to the menu code for displaying each
545 * menu entry.
546 */
547static void label_print(void *data)
548{
549 struct pxe_label *label = data;
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600550 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500551
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600552 printf("%s:\t%s\n", label->num, c);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500553}
554
555/*
556 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
557 * environment variable is defined. Its contents will be executed as U-boot
558 * command. If the label specified an 'append' line, its contents will be
559 * used to overwrite the contents of the 'bootargs' environment variable prior
560 * to running 'localcmd'.
561 *
562 * Returns 1 on success or < 0 on error.
563 */
564static int label_localboot(struct pxe_label *label)
565{
Simon Glassa6642e02012-03-30 21:30:55 +0000566 char *localcmd;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500567
568 localcmd = from_env("localcmd");
569
570 if (!localcmd)
571 return -ENOENT;
572
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500573 if (label->append)
574 setenv("bootargs", label->append);
575
Simon Glassa6642e02012-03-30 21:30:55 +0000576 debug("running: %s\n", localcmd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500577
Simon Glassa6642e02012-03-30 21:30:55 +0000578 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500579}
580
581/*
582 * Boot according to the contents of a pxe_label.
583 *
584 * If we can't boot for any reason, we return. A successful boot never
585 * returns.
586 *
587 * The kernel will be stored in the location given by the 'kernel_addr_r'
588 * environment variable.
589 *
590 * If the label specifies an initrd file, it will be stored in the location
591 * given by the 'ramdisk_addr_r' environment variable.
592 *
593 * If the label specifies an 'append' line, its contents will overwrite that
594 * of the 'bootargs' environment variable.
595 */
Tom Rini5b5b1762013-09-24 09:05:08 -0400596static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500597{
598 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringc0356dc2012-12-03 13:17:21 -0600599 char initrd_str[22];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600600 char mac_str[29] = "";
601 char ip_str[68] = "";
602 char *bootargs;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500603 int bootm_argc = 3;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600604 int len = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500605
606 label_print(label);
607
608 label->attempted = 1;
609
610 if (label->localboot) {
Rob Herring58d7f432012-12-02 21:00:22 -0600611 if (label->localboot_val >= 0)
612 label_localboot(label);
613 return 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500614 }
615
616 if (label->kernel == NULL) {
617 printf("No kernel given, skipping %s\n",
618 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600619 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500620 }
621
622 if (label->initrd) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400623 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500624 printf("Skipping %s for failure retrieving initrd\n",
625 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600626 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500627 }
628
Rob Herringc0356dc2012-12-03 13:17:21 -0600629 bootm_argv[2] = initrd_str;
630 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
631 strcat(bootm_argv[2], ":");
632 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500633 } else {
634 bootm_argv[2] = "-";
635 }
636
Steven Falco7de0fa82013-10-07 09:51:48 -0400637 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500638 printf("Skipping %s for failure retrieving kernel\n",
639 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600640 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500641 }
642
Rob Herring5a3d8be2012-12-02 21:00:29 -0600643 if (label->ipappend & 0x1) {
644 sprintf(ip_str, " ip=%s:%s:%s:%s",
645 getenv("ipaddr"), getenv("serverip"),
646 getenv("gatewayip"), getenv("netmask"));
647 len += strlen(ip_str);
648 }
649
650 if (label->ipappend & 0x2) {
651 int err;
652 strcpy(mac_str, " BOOTIF=");
653 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
654 if (err < 0)
655 mac_str[0] = '\0';
656 len += strlen(mac_str);
657 }
658
659 if (label->append)
660 len += strlen(label->append);
661
662 if (len) {
663 bootargs = malloc(len + 1);
664 if (!bootargs)
665 return 1;
666 bootargs[0] = '\0';
667 if (label->append)
668 strcpy(bootargs, label->append);
669 strcat(bootargs, ip_str);
670 strcat(bootargs, mac_str);
671
672 setenv("bootargs", bootargs);
673 printf("append: %s\n", bootargs);
674
675 free(bootargs);
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600676 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500677
678 bootm_argv[1] = getenv("kernel_addr_r");
679
680 /*
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000681 * fdt usage is optional:
682 * It handles the following scenarios. All scenarios are exclusive
683 *
684 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
685 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
686 * and adjust argc appropriately.
687 *
688 * Scenario 2: If there is an fdt_addr specified, pass it along to
689 * bootm, and adjust argc appropriately.
690 *
691 * Scenario 3: fdt blob is not available.
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500692 */
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000693 bootm_argv[3] = getenv("fdt_addr_r");
694
695 /* if fdt label is defined then get fdt from server */
Stephen Warren90d09af2014-01-28 14:50:10 -0700696 if (bootm_argv[3]) {
697 char *fdtfile = NULL;
698 char *fdtfilefree = NULL;
699
700 if (label->fdt) {
701 fdtfile = label->fdt;
702 } else if (label->fdtdir) {
703 fdtfile = getenv("fdtfile");
704 /*
705 * For complex cases, it might be worth calling a
706 * board- or SoC-provided function here to provide a
707 * better default:
708 *
709 * if (!fdtfile)
710 * fdtfile = gen_fdtfile();
711 *
712 * If this is added, be sure to keep the default below,
713 * or move it to the default weak implementation of
714 * gen_fdtfile().
715 */
716 if (!fdtfile) {
717 char *soc = getenv("soc");
718 char *board = getenv("board");
719 char *slash;
720
721 len = strlen(label->fdtdir);
722 if (!len)
723 slash = "./";
724 else if (label->fdtdir[len - 1] != '/')
725 slash = "/";
726 else
727 slash = "";
728
729 len = strlen(label->fdtdir) + strlen(slash) +
730 strlen(soc) + 1 + strlen(board) + 5;
731 fdtfilefree = malloc(len);
732 if (!fdtfilefree) {
733 printf("malloc fail (FDT filename)\n");
734 return 1;
735 }
736
737 snprintf(fdtfilefree, len, "%s%s%s-%s.dtb",
738 label->fdtdir, slash, soc, board);
739 fdtfile = fdtfilefree;
740 }
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000741 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700742
743 if (fdtfile) {
744 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
745 free(fdtfilefree);
746 if (err < 0) {
747 printf("Skipping %s for failure retrieving fdt\n",
748 label->name);
749 return 1;
750 }
751 } else {
752 bootm_argv[3] = NULL;
753 }
754 }
755
756 if (!bootm_argv[3])
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000757 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500758
759 if (bootm_argv[3])
760 bootm_argc = 4;
761
Tom Rini5b5b1762013-09-24 09:05:08 -0400762 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600763
764#ifdef CONFIG_CMD_BOOTZ
765 /* Try booting a zImage if do_bootm returns */
Tom Rini5b5b1762013-09-24 09:05:08 -0400766 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600767#endif
Rob Herring58d7f432012-12-02 21:00:22 -0600768 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500769}
770
771/*
772 * Tokens for the pxe file parser.
773 */
774enum token_type {
775 T_EOL,
776 T_STRING,
777 T_EOF,
778 T_MENU,
779 T_TITLE,
780 T_TIMEOUT,
781 T_LABEL,
782 T_KERNEL,
Rob Herringc93f74a2012-03-28 05:51:35 +0000783 T_LINUX,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500784 T_APPEND,
785 T_INITRD,
786 T_LOCALBOOT,
787 T_DEFAULT,
788 T_PROMPT,
789 T_INCLUDE,
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000790 T_FDT,
Stephen Warren90d09af2014-01-28 14:50:10 -0700791 T_FDTDIR,
Rob Herringac995832012-12-02 21:00:27 -0600792 T_ONTIMEOUT,
Rob Herring5a3d8be2012-12-02 21:00:29 -0600793 T_IPAPPEND,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500794 T_INVALID
795};
796
797/*
798 * A token - given by a value and a type.
799 */
800struct token {
801 char *val;
802 enum token_type type;
803};
804
805/*
806 * Keywords recognized.
807 */
808static const struct token keywords[] = {
809 {"menu", T_MENU},
810 {"title", T_TITLE},
811 {"timeout", T_TIMEOUT},
812 {"default", T_DEFAULT},
813 {"prompt", T_PROMPT},
814 {"label", T_LABEL},
815 {"kernel", T_KERNEL},
Rob Herringc93f74a2012-03-28 05:51:35 +0000816 {"linux", T_LINUX},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500817 {"localboot", T_LOCALBOOT},
818 {"append", T_APPEND},
819 {"initrd", T_INITRD},
820 {"include", T_INCLUDE},
Stephen Warren7b472af2014-01-28 14:50:09 -0700821 {"devicetree", T_FDT},
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000822 {"fdt", T_FDT},
Stephen Warren90d09af2014-01-28 14:50:10 -0700823 {"devicetreedir", T_FDTDIR},
824 {"fdtdir", T_FDTDIR},
Rob Herringac995832012-12-02 21:00:27 -0600825 {"ontimeout", T_ONTIMEOUT,},
Rob Herring5a3d8be2012-12-02 21:00:29 -0600826 {"ipappend", T_IPAPPEND,},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500827 {NULL, T_INVALID}
828};
829
830/*
831 * Since pxe(linux) files don't have a token to identify the start of a
832 * literal, we have to keep track of when we're in a state where a literal is
833 * expected vs when we're in a state a keyword is expected.
834 */
835enum lex_state {
836 L_NORMAL = 0,
837 L_KEYWORD,
838 L_SLITERAL
839};
840
841/*
842 * get_string retrieves a string from *p and stores it as a token in
843 * *t.
844 *
845 * get_string used for scanning both string literals and keywords.
846 *
847 * Characters from *p are copied into t-val until a character equal to
848 * delim is found, or a NUL byte is reached. If delim has the special value of
849 * ' ', any whitespace character will be used as a delimiter.
850 *
851 * If lower is unequal to 0, uppercase characters will be converted to
852 * lowercase in the result. This is useful to make keywords case
853 * insensitive.
854 *
855 * The location of *p is updated to point to the first character after the end
856 * of the token - the ending delimiter.
857 *
858 * On success, the new value of t->val is returned. Memory for t->val is
859 * allocated using malloc and must be free()'d to reclaim it. If insufficient
860 * memory is available, NULL is returned.
861 */
862static char *get_string(char **p, struct token *t, char delim, int lower)
863{
864 char *b, *e;
865 size_t len, i;
866
867 /*
868 * b and e both start at the beginning of the input stream.
869 *
870 * e is incremented until we find the ending delimiter, or a NUL byte
871 * is reached. Then, we take e - b to find the length of the token.
872 */
873 b = e = *p;
874
875 while (*e) {
876 if ((delim == ' ' && isspace(*e)) || delim == *e)
877 break;
878 e++;
879 }
880
881 len = e - b;
882
883 /*
884 * Allocate memory to hold the string, and copy it in, converting
885 * characters to lowercase if lower is != 0.
886 */
887 t->val = malloc(len + 1);
888 if (!t->val)
889 return NULL;
890
891 for (i = 0; i < len; i++, b++) {
892 if (lower)
893 t->val[i] = tolower(*b);
894 else
895 t->val[i] = *b;
896 }
897
898 t->val[len] = '\0';
899
900 /*
901 * Update *p so the caller knows where to continue scanning.
902 */
903 *p = e;
904
905 t->type = T_STRING;
906
907 return t->val;
908}
909
910/*
911 * Populate a keyword token with a type and value.
912 */
913static void get_keyword(struct token *t)
914{
915 int i;
916
917 for (i = 0; keywords[i].val; i++) {
918 if (!strcmp(t->val, keywords[i].val)) {
919 t->type = keywords[i].type;
920 break;
921 }
922 }
923}
924
925/*
926 * Get the next token. We have to keep track of which state we're in to know
927 * if we're looking to get a string literal or a keyword.
928 *
929 * *p is updated to point at the first character after the current token.
930 */
931static void get_token(char **p, struct token *t, enum lex_state state)
932{
933 char *c = *p;
934
935 t->type = T_INVALID;
936
937 /* eat non EOL whitespace */
938 while (isblank(*c))
939 c++;
940
941 /*
942 * eat comments. note that string literals can't begin with #, but
943 * can contain a # after their first character.
944 */
945 if (*c == '#') {
946 while (*c && *c != '\n')
947 c++;
948 }
949
950 if (*c == '\n') {
951 t->type = T_EOL;
952 c++;
953 } else if (*c == '\0') {
954 t->type = T_EOF;
955 c++;
956 } else if (state == L_SLITERAL) {
957 get_string(&c, t, '\n', 0);
958 } else if (state == L_KEYWORD) {
959 /*
960 * when we expect a keyword, we first get the next string
961 * token delimited by whitespace, and then check if it
962 * matches a keyword in our keyword list. if it does, it's
963 * converted to a keyword token of the appropriate type, and
964 * if not, it remains a string token.
965 */
966 get_string(&c, t, ' ', 1);
967 get_keyword(t);
968 }
969
970 *p = c;
971}
972
973/*
974 * Increment *c until we get to the end of the current line, or EOF.
975 */
976static void eol_or_eof(char **c)
977{
978 while (**c && **c != '\n')
979 (*c)++;
980}
981
982/*
983 * All of these parse_* functions share some common behavior.
984 *
985 * They finish with *c pointing after the token they parse, and return 1 on
986 * success, or < 0 on error.
987 */
988
989/*
990 * Parse a string literal and store a pointer it at *dst. String literals
991 * terminate at the end of the line.
992 */
993static int parse_sliteral(char **c, char **dst)
994{
995 struct token t;
996 char *s = *c;
997
998 get_token(c, &t, L_SLITERAL);
999
1000 if (t.type != T_STRING) {
1001 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1002 return -EINVAL;
1003 }
1004
1005 *dst = t.val;
1006
1007 return 1;
1008}
1009
1010/*
1011 * Parse a base 10 (unsigned) integer and store it at *dst.
1012 */
1013static int parse_integer(char **c, int *dst)
1014{
1015 struct token t;
1016 char *s = *c;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001017
1018 get_token(c, &t, L_SLITERAL);
1019
1020 if (t.type != T_STRING) {
1021 printf("Expected string: %.*s\n", (int)(*c - s), s);
1022 return -EINVAL;
1023 }
1024
Rob Herring58d7f432012-12-02 21:00:22 -06001025 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001026
1027 free(t.val);
1028
1029 return 1;
1030}
1031
Steven Falco7de0fa82013-10-07 09:51:48 -04001032static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001033
1034/*
1035 * Parse an include statement, and retrieve and parse the file it mentions.
1036 *
1037 * base should point to a location where it's safe to store the file, and
1038 * nest_level should indicate how many nested includes have occurred. For this
1039 * include, nest_level has already been incremented and doesn't need to be
1040 * incremented here.
1041 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001042static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001043 struct pxe_menu *cfg, int nest_level)
1044{
1045 char *include_path;
1046 char *s = *c;
1047 int err;
1048
1049 err = parse_sliteral(c, &include_path);
1050
1051 if (err < 0) {
1052 printf("Expected include path: %.*s\n",
1053 (int)(*c - s), s);
1054 return err;
1055 }
1056
Steven Falco7de0fa82013-10-07 09:51:48 -04001057 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001058
1059 if (err < 0) {
1060 printf("Couldn't retrieve %s\n", include_path);
1061 return err;
1062 }
1063
Steven Falco7de0fa82013-10-07 09:51:48 -04001064 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001065}
1066
1067/*
1068 * Parse lines that begin with 'menu'.
1069 *
1070 * b and nest are provided to handle the 'menu include' case.
1071 *
1072 * b should be the address where the file currently being parsed is stored.
1073 *
1074 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1075 * a file it includes, 3 when parsing a file included by that file, and so on.
1076 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001077static 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 -05001078{
1079 struct token t;
1080 char *s = *c;
Heiko Schocher9cd01dd2011-12-12 20:37:17 +00001081 int err = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001082
1083 get_token(c, &t, L_KEYWORD);
1084
1085 switch (t.type) {
1086 case T_TITLE:
1087 err = parse_sliteral(c, &cfg->title);
1088
1089 break;
1090
1091 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001092 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001093 nest_level + 1);
1094 break;
1095
1096 default:
1097 printf("Ignoring malformed menu command: %.*s\n",
1098 (int)(*c - s), s);
1099 }
1100
1101 if (err < 0)
1102 return err;
1103
1104 eol_or_eof(c);
1105
1106 return 1;
1107}
1108
1109/*
1110 * Handles parsing a 'menu line' when we're parsing a label.
1111 */
1112static int parse_label_menu(char **c, struct pxe_menu *cfg,
1113 struct pxe_label *label)
1114{
1115 struct token t;
1116 char *s;
1117
1118 s = *c;
1119
1120 get_token(c, &t, L_KEYWORD);
1121
1122 switch (t.type) {
1123 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001124 if (!cfg->default_label)
1125 cfg->default_label = strdup(label->name);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001126
1127 if (!cfg->default_label)
1128 return -ENOMEM;
1129
1130 break;
Rob Herringa6b74172012-03-28 05:51:34 +00001131 case T_LABEL:
1132 parse_sliteral(c, &label->menu);
1133 break;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001134 default:
1135 printf("Ignoring malformed menu command: %.*s\n",
1136 (int)(*c - s), s);
1137 }
1138
1139 eol_or_eof(c);
1140
1141 return 0;
1142}
1143
1144/*
1145 * Parses a label and adds it to the list of labels for a menu.
1146 *
1147 * A label ends when we either get to the end of a file, or
1148 * get some input we otherwise don't have a handler defined
1149 * for.
1150 *
1151 */
1152static int parse_label(char **c, struct pxe_menu *cfg)
1153{
1154 struct token t;
Rob Herringd8fceec2012-03-28 05:51:37 +00001155 int len;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001156 char *s = *c;
1157 struct pxe_label *label;
1158 int err;
1159
1160 label = label_create();
1161 if (!label)
1162 return -ENOMEM;
1163
1164 err = parse_sliteral(c, &label->name);
1165 if (err < 0) {
1166 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1167 label_destroy(label);
1168 return -EINVAL;
1169 }
1170
1171 list_add_tail(&label->list, &cfg->labels);
1172
1173 while (1) {
1174 s = *c;
1175 get_token(c, &t, L_KEYWORD);
1176
1177 err = 0;
1178 switch (t.type) {
1179 case T_MENU:
1180 err = parse_label_menu(c, cfg, label);
1181 break;
1182
1183 case T_KERNEL:
Rob Herringc93f74a2012-03-28 05:51:35 +00001184 case T_LINUX:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001185 err = parse_sliteral(c, &label->kernel);
1186 break;
1187
1188 case T_APPEND:
1189 err = parse_sliteral(c, &label->append);
Rob Herringd8fceec2012-03-28 05:51:37 +00001190 if (label->initrd)
1191 break;
1192 s = strstr(label->append, "initrd=");
1193 if (!s)
1194 break;
1195 s += 7;
1196 len = (int)(strchr(s, ' ') - s);
1197 label->initrd = malloc(len + 1);
1198 strncpy(label->initrd, s, len);
1199 label->initrd[len] = '\0';
1200
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001201 break;
1202
1203 case T_INITRD:
Rob Herringd8fceec2012-03-28 05:51:37 +00001204 if (!label->initrd)
1205 err = parse_sliteral(c, &label->initrd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001206 break;
1207
Chander Kashyap1644a9d2012-09-06 19:36:31 +00001208 case T_FDT:
1209 if (!label->fdt)
1210 err = parse_sliteral(c, &label->fdt);
1211 break;
1212
Stephen Warren90d09af2014-01-28 14:50:10 -07001213 case T_FDTDIR:
1214 if (!label->fdtdir)
1215 err = parse_sliteral(c, &label->fdtdir);
1216 break;
1217
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001218 case T_LOCALBOOT:
Rob Herring58d7f432012-12-02 21:00:22 -06001219 label->localboot = 1;
1220 err = parse_integer(c, &label->localboot_val);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001221 break;
1222
Rob Herring5a3d8be2012-12-02 21:00:29 -06001223 case T_IPAPPEND:
1224 err = parse_integer(c, &label->ipappend);
1225 break;
1226
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001227 case T_EOL:
1228 break;
1229
1230 default:
1231 /*
1232 * put the token back! we don't want it - it's the end
1233 * of a label and whatever token this is, it's
1234 * something for the menu level context to handle.
1235 */
1236 *c = s;
1237 return 1;
1238 }
1239
1240 if (err < 0)
1241 return err;
1242 }
1243}
1244
1245/*
1246 * This 16 comes from the limit pxelinux imposes on nested includes.
1247 *
1248 * There is no reason at all we couldn't do more, but some limit helps prevent
1249 * infinite (until crash occurs) recursion if a file tries to include itself.
1250 */
1251#define MAX_NEST_LEVEL 16
1252
1253/*
1254 * Entry point for parsing a menu file. nest_level indicates how many times
1255 * we've nested in includes. It will be 1 for the top level menu file.
1256 *
1257 * Returns 1 on success, < 0 on error.
1258 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001259static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001260{
1261 struct token t;
1262 char *s, *b, *label_name;
1263 int err;
1264
1265 b = p;
1266
1267 if (nest_level > MAX_NEST_LEVEL) {
1268 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1269 return -EMLINK;
1270 }
1271
1272 while (1) {
1273 s = p;
1274
1275 get_token(&p, &t, L_KEYWORD);
1276
1277 err = 0;
1278 switch (t.type) {
1279 case T_MENU:
Rob Herringf0898b92012-12-02 21:00:25 -06001280 cfg->prompt = 1;
Steven Falco7de0fa82013-10-07 09:51:48 -04001281 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001282 break;
1283
1284 case T_TIMEOUT:
1285 err = parse_integer(&p, &cfg->timeout);
1286 break;
1287
1288 case T_LABEL:
1289 err = parse_label(&p, cfg);
1290 break;
1291
1292 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001293 case T_ONTIMEOUT:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001294 err = parse_sliteral(&p, &label_name);
1295
1296 if (label_name) {
1297 if (cfg->default_label)
1298 free(cfg->default_label);
1299
1300 cfg->default_label = label_name;
1301 }
1302
1303 break;
1304
Rob Herring8e5abb02012-05-25 10:43:16 +00001305 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001306 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring8e5abb02012-05-25 10:43:16 +00001307 nest_level + 1);
1308 break;
1309
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001310 case T_PROMPT:
Rob Herringf0898b92012-12-02 21:00:25 -06001311 eol_or_eof(&p);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001312 break;
1313
1314 case T_EOL:
1315 break;
1316
1317 case T_EOF:
1318 return 1;
1319
1320 default:
1321 printf("Ignoring unknown command: %.*s\n",
1322 (int)(p - s), s);
1323 eol_or_eof(&p);
1324 }
1325
1326 if (err < 0)
1327 return err;
1328 }
1329}
1330
1331/*
1332 * Free the memory used by a pxe_menu and its labels.
1333 */
1334static void destroy_pxe_menu(struct pxe_menu *cfg)
1335{
1336 struct list_head *pos, *n;
1337 struct pxe_label *label;
1338
1339 if (cfg->title)
1340 free(cfg->title);
1341
1342 if (cfg->default_label)
1343 free(cfg->default_label);
1344
1345 list_for_each_safe(pos, n, &cfg->labels) {
1346 label = list_entry(pos, struct pxe_label, list);
1347
1348 label_destroy(label);
1349 }
1350
1351 free(cfg);
1352}
1353
1354/*
1355 * Entry point for parsing a pxe file. This is only used for the top level
1356 * file.
1357 *
1358 * Returns NULL if there is an error, otherwise, returns a pointer to a
1359 * pxe_menu struct populated with the results of parsing the pxe file (and any
1360 * files it includes). The resulting pxe_menu struct can be free()'d by using
1361 * the destroy_pxe_menu() function.
1362 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001363static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001364{
1365 struct pxe_menu *cfg;
1366
1367 cfg = malloc(sizeof(struct pxe_menu));
1368
1369 if (!cfg)
1370 return NULL;
1371
1372 memset(cfg, 0, sizeof(struct pxe_menu));
1373
1374 INIT_LIST_HEAD(&cfg->labels);
1375
Steven Falco7de0fa82013-10-07 09:51:48 -04001376 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001377 destroy_pxe_menu(cfg);
1378 return NULL;
1379 }
1380
1381 return cfg;
1382}
1383
1384/*
1385 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1386 * menu code.
1387 */
1388static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1389{
1390 struct pxe_label *label;
1391 struct list_head *pos;
1392 struct menu *m;
1393 int err;
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001394 int i = 1;
1395 char *default_num = NULL;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001396
1397 /*
1398 * Create a menu and add items for all the labels.
1399 */
Pali Rohárd0d8d3b2013-03-23 14:50:40 +00001400 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1401 NULL, NULL);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001402
1403 if (!m)
1404 return NULL;
1405
1406 list_for_each(pos, &cfg->labels) {
1407 label = list_entry(pos, struct pxe_label, list);
1408
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001409 sprintf(label->num, "%d", i++);
1410 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001411 menu_destroy(m);
1412 return NULL;
1413 }
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001414 if (cfg->default_label &&
Rob Herringac995832012-12-02 21:00:27 -06001415 (strcmp(label->name, cfg->default_label) == 0))
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001416 default_num = label->num;
1417
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001418 }
1419
1420 /*
1421 * After we've created items for each label in the menu, set the
1422 * menu's default label if one was specified.
1423 */
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001424 if (default_num) {
1425 err = menu_default_set(m, default_num);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001426 if (err != 1) {
1427 if (err != -ENOENT) {
1428 menu_destroy(m);
1429 return NULL;
1430 }
1431
1432 printf("Missing default: %s\n", cfg->default_label);
1433 }
1434 }
1435
1436 return m;
1437}
1438
1439/*
1440 * Try to boot any labels we have yet to attempt to boot.
1441 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001442static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001443{
1444 struct list_head *pos;
1445 struct pxe_label *label;
1446
1447 list_for_each(pos, &cfg->labels) {
1448 label = list_entry(pos, struct pxe_label, list);
1449
1450 if (!label->attempted)
Tom Rini5b5b1762013-09-24 09:05:08 -04001451 label_boot(cmdtp, label);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001452 }
1453}
1454
1455/*
1456 * Boot the system as prescribed by a pxe_menu.
1457 *
1458 * Use the menu system to either get the user's choice or the default, based
1459 * on config or user input. If there is no default or user's choice,
1460 * attempted to boot labels in the order they were given in pxe files.
1461 * If the default or user's choice fails to boot, attempt to boot other
1462 * labels in the order they were given in pxe files.
1463 *
1464 * If this function returns, there weren't any labels that successfully
1465 * booted, or the user interrupted the menu selection via ctrl+c.
1466 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001467static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001468{
1469 void *choice;
1470 struct menu *m;
1471 int err;
1472
1473 m = pxe_menu_to_menu(cfg);
1474 if (!m)
1475 return;
1476
1477 err = menu_get_choice(m, &choice);
1478
1479 menu_destroy(m);
1480
Jason Hobbs083b7a62011-11-07 03:07:15 +00001481 /*
1482 * err == 1 means we got a choice back from menu_get_choice.
1483 *
1484 * err == -ENOENT if the menu was setup to select the default but no
1485 * default was set. in that case, we should continue trying to boot
1486 * labels that haven't been attempted yet.
1487 *
1488 * otherwise, the user interrupted or there was some other error and
1489 * we give up.
1490 */
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001491
Rob Herring58d7f432012-12-02 21:00:22 -06001492 if (err == 1) {
Tom Rini5b5b1762013-09-24 09:05:08 -04001493 err = label_boot(cmdtp, choice);
Rob Herring58d7f432012-12-02 21:00:22 -06001494 if (!err)
1495 return;
1496 } else if (err != -ENOENT) {
Jason Hobbs083b7a62011-11-07 03:07:15 +00001497 return;
Rob Herring58d7f432012-12-02 21:00:22 -06001498 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001499
Tom Rini5b5b1762013-09-24 09:05:08 -04001500 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001501}
1502
1503/*
1504 * Boots a system using a pxe file
1505 *
1506 * Returns 0 on success, 1 on error.
1507 */
1508static int
1509do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1510{
1511 unsigned long pxefile_addr_r;
1512 struct pxe_menu *cfg;
1513 char *pxefile_addr_str;
1514
Rob Herringeee675f2012-05-25 10:47:39 +00001515 do_getfile = do_get_tftp;
1516
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001517 if (argc == 1) {
1518 pxefile_addr_str = from_env("pxefile_addr_r");
1519 if (!pxefile_addr_str)
1520 return 1;
1521
1522 } else if (argc == 2) {
1523 pxefile_addr_str = argv[1];
1524 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +00001525 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001526 }
1527
1528 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1529 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1530 return 1;
1531 }
1532
Steven Falco7de0fa82013-10-07 09:51:48 -04001533 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001534
1535 if (cfg == NULL) {
1536 printf("Error parsing config file\n");
1537 return 1;
1538 }
1539
Tom Rini5b5b1762013-09-24 09:05:08 -04001540 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001541
1542 destroy_pxe_menu(cfg);
1543
1544 return 0;
1545}
1546
1547static cmd_tbl_t cmd_pxe_sub[] = {
1548 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1549 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1550};
1551
1552int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1553{
1554 cmd_tbl_t *cp;
1555
1556 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001557 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001558
Rob Herring759456e2013-10-18 13:04:42 -05001559 is_pxe = true;
1560
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001561 /* drop initial "pxe" arg */
1562 argc--;
1563 argv++;
1564
1565 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1566
1567 if (cp)
1568 return cp->cmd(cmdtp, flag, argc, argv);
1569
Simon Glassa06dfc72011-12-10 08:44:01 +00001570 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001571}
1572
1573U_BOOT_CMD(
1574 pxe, 3, 1, do_pxe,
1575 "commands to get and boot from pxe files",
1576 "get - try to retrieve a pxe file using tftp\npxe "
1577 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1578);
Rob Herringeee675f2012-05-25 10:47:39 +00001579
1580/*
1581 * Boots a system using a local disk syslinux/extlinux file
1582 *
1583 * Returns 0 on success, 1 on error.
1584 */
1585int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1586{
1587 unsigned long pxefile_addr_r;
1588 struct pxe_menu *cfg;
1589 char *pxefile_addr_str;
1590 char *filename;
1591 int prompt = 0;
1592
Rob Herring759456e2013-10-18 13:04:42 -05001593 is_pxe = false;
1594
Rob Herringeee675f2012-05-25 10:47:39 +00001595 if (strstr(argv[1], "-p")) {
1596 prompt = 1;
1597 argc--;
1598 argv++;
1599 }
1600
1601 if (argc < 4)
1602 return cmd_usage(cmdtp);
1603
1604 if (argc < 5) {
1605 pxefile_addr_str = from_env("pxefile_addr_r");
1606 if (!pxefile_addr_str)
1607 return 1;
1608 } else {
1609 pxefile_addr_str = argv[4];
1610 }
1611
1612 if (argc < 6)
1613 filename = getenv("bootfile");
1614 else {
1615 filename = argv[5];
1616 setenv("bootfile", filename);
1617 }
1618
1619 if (strstr(argv[3], "ext2"))
1620 do_getfile = do_get_ext2;
1621 else if (strstr(argv[3], "fat"))
1622 do_getfile = do_get_fat;
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001623 else if (strstr(argv[3], "any"))
1624 do_getfile = do_get_any;
Rob Herringeee675f2012-05-25 10:47:39 +00001625 else {
1626 printf("Invalid filesystem: %s\n", argv[3]);
1627 return 1;
1628 }
1629 fs_argv[1] = argv[1];
1630 fs_argv[2] = argv[2];
1631
1632 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1633 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1634 return 1;
1635 }
1636
Steven Falco7de0fa82013-10-07 09:51:48 -04001637 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herringeee675f2012-05-25 10:47:39 +00001638 printf("Error reading config file\n");
1639 return 1;
1640 }
1641
Steven Falco7de0fa82013-10-07 09:51:48 -04001642 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herringeee675f2012-05-25 10:47:39 +00001643
1644 if (cfg == NULL) {
1645 printf("Error parsing config file\n");
1646 return 1;
1647 }
1648
1649 if (prompt)
1650 cfg->prompt = 1;
1651
Tom Rini5b5b1762013-09-24 09:05:08 -04001652 handle_pxe_menu(cmdtp, cfg);
Rob Herringeee675f2012-05-25 10:47:39 +00001653
1654 destroy_pxe_menu(cfg);
1655
1656 return 0;
1657}
1658
1659U_BOOT_CMD(
1660 sysboot, 7, 1, do_sysboot,
1661 "command to get and boot from syslinux files",
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001662 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1663 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1664 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herringeee675f2012-05-25 10:47:39 +00001665);