blob: 6aabd1357c7fb8d07be4bd9a4db86a55b3bdec90 [file] [log] [blame]
Jason Hobbs06283a62011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs06283a62011-08-31 10:37:30 -05005 */
Wolfgang Denk1a459662013-07-08 09:37:19 +02006
Jason Hobbs06283a62011-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 Gilmore6d1a3e52014-02-04 05:25:46 -060014#include <fs.h>
Jason Hobbs06283a62011-08-31 10:37:30 -050015
16#include "menu.h"
17
18#define MAX_TFTP_PATH_LEN 127
19
Rob Herring39f98552012-12-02 21:00:28 -060020const char *pxe_default_paths[] = {
Joe Hershberger58d9ff92013-06-24 17:21:04 -050021#ifdef CONFIG_SYS_SOC
Rob Herring39f98552012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger58d9ff92013-06-24 17:21:04 -050023#endif
Rob Herring39f98552012-12-02 21:00:28 -060024 "default-" CONFIG_SYS_ARCH,
25 "default",
26 NULL
27};
28
Rob Herringe5a9a402013-10-18 13:04:42 -050029static bool is_pxe;
30
Jason Hobbs06283a62011-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 Herring23b71942012-12-02 21:00:21 -060036static char *from_env(const char *envvar)
Jason Hobbs06283a62011-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 Herringef034c92012-12-02 21:00:20 -060060 uchar ethaddr[6];
Jason Hobbs06283a62011-08-31 10:37:30 -050061
Rob Herringef034c92012-12-02 21:00:20 -060062 if (outbuf_len < 21) {
David Feng5cea95c2013-12-14 11:47:30 +080063 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs06283a62011-08-31 10:37:30 -050064
65 return -EINVAL;
66 }
67
Rob Herringef034c92012-12-02 21:00:20 -060068 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
69 ethaddr))
70 return -ENOENT;
Jason Hobbs06283a62011-08-31 10:37:30 -050071
Rob Herringef034c92012-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 Hobbs06283a62011-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 Herring90ba7d72012-03-28 05:51:36 +000084static int get_bootfile_path(const char *file_path, char *bootfile_path,
85 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -050086{
87 char *bootfile, *last_slash;
Rob Herring90ba7d72012-03-28 05:51:36 +000088 size_t path_len = 0;
89
Rob Herringe5a9a402013-10-18 13:04:42 -050090 /* Only syslinux allows absolute paths */
91 if (file_path[0] == '/' && !is_pxe)
Rob Herring90ba7d72012-03-28 05:51:36 +000092 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050093
94 bootfile = from_env("bootfile");
95
Rob Herring90ba7d72012-03-28 05:51:36 +000096 if (!bootfile)
97 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050098
99 last_slash = strrchr(bootfile, '/');
100
Rob Herring90ba7d72012-03-28 05:51:36 +0000101 if (last_slash == NULL)
102 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500103
104 path_len = (last_slash - bootfile) + 1;
105
106 if (bootfile_path_size < path_len) {
David Feng5cea95c2013-12-14 11:47:30 +0800107 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs06283a62011-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 Herring90ba7d72012-03-28 05:51:36 +0000115 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500116 bootfile_path[path_len] = '\0';
117
118 return 1;
119}
120
Steven Falco0e3f3f82013-10-07 09:51:48 -0400121static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herring669df7e2012-05-25 10:47:39 +0000122
Steven Falco0e3f3f82013-10-07 09:51:48 -0400123static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000124{
125 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
126
127 tftp_argv[1] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600128 tftp_argv[2] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000129
Steven Falco0e3f3f82013-10-07 09:51:48 -0400130 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000131 return -ENOENT;
132
133 return 1;
134}
135
136static char *fs_argv[5];
137
Steven Falco0e3f3f82013-10-07 09:51:48 -0400138static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000139{
140#ifdef CONFIG_CMD_EXT2
141 fs_argv[0] = "ext2load";
142 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600143 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000144
Steven Falco0e3f3f82013-10-07 09:51:48 -0400145 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000146 return 1;
147#endif
148 return -ENOENT;
149}
150
Steven Falco0e3f3f82013-10-07 09:51:48 -0400151static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000152{
153#ifdef CONFIG_CMD_FAT
154 fs_argv[0] = "fatload";
155 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600156 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000157
Steven Falco0e3f3f82013-10-07 09:51:48 -0400158 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000159 return 1;
160#endif
161 return -ENOENT;
162}
163
Dennis Gilmore6d1a3e52014-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 Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400185static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-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 Hobbs06283a62011-08-31 10:37:30 -0500190 int err;
191
Rob Herring90ba7d72012-03-28 05:51:36 +0000192 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400214 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400224static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500225{
226 unsigned long config_file_size;
227 char *tftp_filesize;
228 int err;
229
Steven Falco0e3f3f82013-10-07 09:51:48 -0400230 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400261static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400274 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400282static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400291 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400300static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400310 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400320static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400328 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs06283a62011-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 Hobbs834c9382012-03-05 08:12:28 +0000357 unsigned long pxefile_addr_r;
Rob Herring39f98552012-12-02 21:00:28 -0600358 int err, i = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500359
Rob Herring669df7e2012-05-25 10:47:39 +0000360 do_getfile = do_get_tftp;
361
Jason Hobbs06283a62011-08-31 10:37:30 -0500362 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000363 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500364
Jason Hobbs06283a62011-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 Falco0e3f3f82013-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 Hobbs06283a62011-08-31 10:37:30 -0500382 printf("Config file found\n");
383
384 return 0;
385 }
386
Rob Herring39f98552012-12-02 21:00:28 -0600387 while (pxe_default_paths[i]) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400388 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herring39f98552012-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 Hobbs06283a62011-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 Falco0e3f3f82013-10-07 09:51:48 -0400408static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs06283a62011-08-31 10:37:30 -0500409{
Jason Hobbs834c9382012-03-05 08:12:28 +0000410 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500411 char *envaddr;
412
413 envaddr = from_env(envaddr_name);
414
415 if (!envaddr)
416 return -ENOENT;
417
Jason Hobbs834c9382012-03-05 08:12:28 +0000418 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500419 return -EINVAL;
420
Steven Falco0e3f3f82013-10-07 09:51:48 -0400421 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs06283a62011-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 Herring32d2ffe2012-12-02 21:00:26 -0600455 char num[4];
Jason Hobbs06283a62011-08-31 10:37:30 -0500456 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000457 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500458 char *kernel;
459 char *append;
460 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000461 char *fdt;
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700462 char *fdtdir;
Rob Herring98f64672012-12-02 21:00:29 -0600463 int ipappend;
Jason Hobbs06283a62011-08-31 10:37:30 -0500464 int attempted;
465 int localboot;
Rob Herring500f3042012-12-02 21:00:22 -0600466 int localboot_val;
Jason Hobbs06283a62011-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 Kashyapa6559382012-09-06 19:36:31 +0000532 if (label->fdt)
533 free(label->fdt);
534
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700535 if (label->fdtdir)
536 free(label->fdtdir);
537
Jason Hobbs06283a62011-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 Herring32d2ffe2012-12-02 21:00:26 -0600550 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500551
Rob Herring32d2ffe2012-12-02 21:00:26 -0600552 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-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 Glassd51004a2012-03-30 21:30:55 +0000566 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500567
568 localcmd = from_env("localcmd");
569
570 if (!localcmd)
571 return -ENOENT;
572
Jason Hobbs06283a62011-08-31 10:37:30 -0500573 if (label->append)
574 setenv("bootargs", label->append);
575
Simon Glassd51004a2012-03-30 21:30:55 +0000576 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500577
Simon Glassd51004a2012-03-30 21:30:55 +0000578 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-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 Rinid7884e02013-09-24 09:05:08 -0400596static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500597{
598 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600599 char initrd_str[22];
Rob Herring98f64672012-12-02 21:00:29 -0600600 char mac_str[29] = "";
601 char ip_str[68] = "";
602 char *bootargs;
Jason Hobbs06283a62011-08-31 10:37:30 -0500603 int bootm_argc = 3;
Rob Herring98f64672012-12-02 21:00:29 -0600604 int len = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500605
606 label_print(label);
607
608 label->attempted = 1;
609
610 if (label->localboot) {
Rob Herring500f3042012-12-02 21:00:22 -0600611 if (label->localboot_val >= 0)
612 label_localboot(label);
613 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500614 }
615
616 if (label->kernel == NULL) {
617 printf("No kernel given, skipping %s\n",
618 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600619 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500620 }
621
622 if (label->initrd) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400623 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500624 printf("Skipping %s for failure retrieving initrd\n",
625 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600626 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500627 }
628
Rob Herringe6b6ccf2012-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 Hobbs06283a62011-08-31 10:37:30 -0500633 } else {
634 bootm_argv[2] = "-";
635 }
636
Steven Falco0e3f3f82013-10-07 09:51:48 -0400637 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500638 printf("Skipping %s for failure retrieving kernel\n",
639 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600640 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500641 }
642
Rob Herring98f64672012-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 Herring32d2ffe2012-12-02 21:00:26 -0600676 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500677
678 bootm_argv[1] = getenv("kernel_addr_r");
679
680 /*
Chander Kashyapa6559382012-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 Hobbs06283a62011-08-31 10:37:30 -0500692 */
Chander Kashyapa6559382012-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 Warrenc61d94d2014-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) {
Stephen Warrene22361a2014-02-12 14:30:04 -0700703 char *f1, *f2, *f3, *f4, *slash;
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700704
Stephen Warrene22361a2014-02-12 14:30:04 -0700705 f1 = getenv("fdtfile");
706 if (f1) {
707 f2 = "";
708 f3 = "";
709 f4 = "";
710 } else {
711 /*
712 * For complex cases where this code doesn't
713 * generate the correct filename, the board
714 * code should set $fdtfile during early boot,
715 * or the boot scripts should set $fdtfile
716 * before invoking "pxe" or "sysboot".
717 */
718 f1 = getenv("soc");
719 f2 = "-";
720 f3 = getenv("board");
721 f4 = ".dtb";
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700722 }
Stephen Warrene22361a2014-02-12 14:30:04 -0700723
724 len = strlen(label->fdtdir);
725 if (!len)
726 slash = "./";
727 else if (label->fdtdir[len - 1] != '/')
728 slash = "/";
729 else
730 slash = "";
731
732 len = strlen(label->fdtdir) + strlen(slash) +
733 strlen(f1) + strlen(f2) + strlen(f3) +
734 strlen(f4) + 1;
735 fdtfilefree = malloc(len);
736 if (!fdtfilefree) {
737 printf("malloc fail (FDT filename)\n");
738 return 1;
739 }
740
741 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
742 label->fdtdir, slash, f1, f2, f3, f4);
743 fdtfile = fdtfilefree;
Chander Kashyapa6559382012-09-06 19:36:31 +0000744 }
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700745
746 if (fdtfile) {
747 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
748 free(fdtfilefree);
749 if (err < 0) {
750 printf("Skipping %s for failure retrieving fdt\n",
751 label->name);
752 return 1;
753 }
754 } else {
755 bootm_argv[3] = NULL;
756 }
757 }
758
759 if (!bootm_argv[3])
Chander Kashyapa6559382012-09-06 19:36:31 +0000760 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500761
762 if (bootm_argv[3])
763 bootm_argc = 4;
764
Tom Rinid7884e02013-09-24 09:05:08 -0400765 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600766
767#ifdef CONFIG_CMD_BOOTZ
768 /* Try booting a zImage if do_bootm returns */
Tom Rinid7884e02013-09-24 09:05:08 -0400769 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600770#endif
Rob Herring500f3042012-12-02 21:00:22 -0600771 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500772}
773
774/*
775 * Tokens for the pxe file parser.
776 */
777enum token_type {
778 T_EOL,
779 T_STRING,
780 T_EOF,
781 T_MENU,
782 T_TITLE,
783 T_TIMEOUT,
784 T_LABEL,
785 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000786 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500787 T_APPEND,
788 T_INITRD,
789 T_LOCALBOOT,
790 T_DEFAULT,
791 T_PROMPT,
792 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000793 T_FDT,
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700794 T_FDTDIR,
Rob Herring8577fec2012-12-02 21:00:27 -0600795 T_ONTIMEOUT,
Rob Herring98f64672012-12-02 21:00:29 -0600796 T_IPAPPEND,
Jason Hobbs06283a62011-08-31 10:37:30 -0500797 T_INVALID
798};
799
800/*
801 * A token - given by a value and a type.
802 */
803struct token {
804 char *val;
805 enum token_type type;
806};
807
808/*
809 * Keywords recognized.
810 */
811static const struct token keywords[] = {
812 {"menu", T_MENU},
813 {"title", T_TITLE},
814 {"timeout", T_TIMEOUT},
815 {"default", T_DEFAULT},
816 {"prompt", T_PROMPT},
817 {"label", T_LABEL},
818 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000819 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500820 {"localboot", T_LOCALBOOT},
821 {"append", T_APPEND},
822 {"initrd", T_INITRD},
823 {"include", T_INCLUDE},
Stephen Warrenf43c4012014-01-28 14:50:09 -0700824 {"devicetree", T_FDT},
Chander Kashyapa6559382012-09-06 19:36:31 +0000825 {"fdt", T_FDT},
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700826 {"devicetreedir", T_FDTDIR},
827 {"fdtdir", T_FDTDIR},
Rob Herring8577fec2012-12-02 21:00:27 -0600828 {"ontimeout", T_ONTIMEOUT,},
Rob Herring98f64672012-12-02 21:00:29 -0600829 {"ipappend", T_IPAPPEND,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500830 {NULL, T_INVALID}
831};
832
833/*
834 * Since pxe(linux) files don't have a token to identify the start of a
835 * literal, we have to keep track of when we're in a state where a literal is
836 * expected vs when we're in a state a keyword is expected.
837 */
838enum lex_state {
839 L_NORMAL = 0,
840 L_KEYWORD,
841 L_SLITERAL
842};
843
844/*
845 * get_string retrieves a string from *p and stores it as a token in
846 * *t.
847 *
848 * get_string used for scanning both string literals and keywords.
849 *
850 * Characters from *p are copied into t-val until a character equal to
851 * delim is found, or a NUL byte is reached. If delim has the special value of
852 * ' ', any whitespace character will be used as a delimiter.
853 *
854 * If lower is unequal to 0, uppercase characters will be converted to
855 * lowercase in the result. This is useful to make keywords case
856 * insensitive.
857 *
858 * The location of *p is updated to point to the first character after the end
859 * of the token - the ending delimiter.
860 *
861 * On success, the new value of t->val is returned. Memory for t->val is
862 * allocated using malloc and must be free()'d to reclaim it. If insufficient
863 * memory is available, NULL is returned.
864 */
865static char *get_string(char **p, struct token *t, char delim, int lower)
866{
867 char *b, *e;
868 size_t len, i;
869
870 /*
871 * b and e both start at the beginning of the input stream.
872 *
873 * e is incremented until we find the ending delimiter, or a NUL byte
874 * is reached. Then, we take e - b to find the length of the token.
875 */
876 b = e = *p;
877
878 while (*e) {
879 if ((delim == ' ' && isspace(*e)) || delim == *e)
880 break;
881 e++;
882 }
883
884 len = e - b;
885
886 /*
887 * Allocate memory to hold the string, and copy it in, converting
888 * characters to lowercase if lower is != 0.
889 */
890 t->val = malloc(len + 1);
891 if (!t->val)
892 return NULL;
893
894 for (i = 0; i < len; i++, b++) {
895 if (lower)
896 t->val[i] = tolower(*b);
897 else
898 t->val[i] = *b;
899 }
900
901 t->val[len] = '\0';
902
903 /*
904 * Update *p so the caller knows where to continue scanning.
905 */
906 *p = e;
907
908 t->type = T_STRING;
909
910 return t->val;
911}
912
913/*
914 * Populate a keyword token with a type and value.
915 */
916static void get_keyword(struct token *t)
917{
918 int i;
919
920 for (i = 0; keywords[i].val; i++) {
921 if (!strcmp(t->val, keywords[i].val)) {
922 t->type = keywords[i].type;
923 break;
924 }
925 }
926}
927
928/*
929 * Get the next token. We have to keep track of which state we're in to know
930 * if we're looking to get a string literal or a keyword.
931 *
932 * *p is updated to point at the first character after the current token.
933 */
934static void get_token(char **p, struct token *t, enum lex_state state)
935{
936 char *c = *p;
937
938 t->type = T_INVALID;
939
940 /* eat non EOL whitespace */
941 while (isblank(*c))
942 c++;
943
944 /*
945 * eat comments. note that string literals can't begin with #, but
946 * can contain a # after their first character.
947 */
948 if (*c == '#') {
949 while (*c && *c != '\n')
950 c++;
951 }
952
953 if (*c == '\n') {
954 t->type = T_EOL;
955 c++;
956 } else if (*c == '\0') {
957 t->type = T_EOF;
958 c++;
959 } else if (state == L_SLITERAL) {
960 get_string(&c, t, '\n', 0);
961 } else if (state == L_KEYWORD) {
962 /*
963 * when we expect a keyword, we first get the next string
964 * token delimited by whitespace, and then check if it
965 * matches a keyword in our keyword list. if it does, it's
966 * converted to a keyword token of the appropriate type, and
967 * if not, it remains a string token.
968 */
969 get_string(&c, t, ' ', 1);
970 get_keyword(t);
971 }
972
973 *p = c;
974}
975
976/*
977 * Increment *c until we get to the end of the current line, or EOF.
978 */
979static void eol_or_eof(char **c)
980{
981 while (**c && **c != '\n')
982 (*c)++;
983}
984
985/*
986 * All of these parse_* functions share some common behavior.
987 *
988 * They finish with *c pointing after the token they parse, and return 1 on
989 * success, or < 0 on error.
990 */
991
992/*
993 * Parse a string literal and store a pointer it at *dst. String literals
994 * terminate at the end of the line.
995 */
996static int parse_sliteral(char **c, char **dst)
997{
998 struct token t;
999 char *s = *c;
1000
1001 get_token(c, &t, L_SLITERAL);
1002
1003 if (t.type != T_STRING) {
1004 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1005 return -EINVAL;
1006 }
1007
1008 *dst = t.val;
1009
1010 return 1;
1011}
1012
1013/*
1014 * Parse a base 10 (unsigned) integer and store it at *dst.
1015 */
1016static int parse_integer(char **c, int *dst)
1017{
1018 struct token t;
1019 char *s = *c;
Jason Hobbs06283a62011-08-31 10:37:30 -05001020
1021 get_token(c, &t, L_SLITERAL);
1022
1023 if (t.type != T_STRING) {
1024 printf("Expected string: %.*s\n", (int)(*c - s), s);
1025 return -EINVAL;
1026 }
1027
Rob Herring500f3042012-12-02 21:00:22 -06001028 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -05001029
1030 free(t.val);
1031
1032 return 1;
1033}
1034
Steven Falco0e3f3f82013-10-07 09:51:48 -04001035static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001036
1037/*
1038 * Parse an include statement, and retrieve and parse the file it mentions.
1039 *
1040 * base should point to a location where it's safe to store the file, and
1041 * nest_level should indicate how many nested includes have occurred. For this
1042 * include, nest_level has already been incremented and doesn't need to be
1043 * incremented here.
1044 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001045static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs06283a62011-08-31 10:37:30 -05001046 struct pxe_menu *cfg, int nest_level)
1047{
1048 char *include_path;
1049 char *s = *c;
1050 int err;
1051
1052 err = parse_sliteral(c, &include_path);
1053
1054 if (err < 0) {
1055 printf("Expected include path: %.*s\n",
1056 (int)(*c - s), s);
1057 return err;
1058 }
1059
Steven Falco0e3f3f82013-10-07 09:51:48 -04001060 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs06283a62011-08-31 10:37:30 -05001061
1062 if (err < 0) {
1063 printf("Couldn't retrieve %s\n", include_path);
1064 return err;
1065 }
1066
Steven Falco0e3f3f82013-10-07 09:51:48 -04001067 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001068}
1069
1070/*
1071 * Parse lines that begin with 'menu'.
1072 *
1073 * b and nest are provided to handle the 'menu include' case.
1074 *
1075 * b should be the address where the file currently being parsed is stored.
1076 *
1077 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1078 * a file it includes, 3 when parsing a file included by that file, and so on.
1079 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001080static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
Jason Hobbs06283a62011-08-31 10:37:30 -05001081{
1082 struct token t;
1083 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +00001084 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -05001085
1086 get_token(c, &t, L_KEYWORD);
1087
1088 switch (t.type) {
1089 case T_TITLE:
1090 err = parse_sliteral(c, &cfg->title);
1091
1092 break;
1093
1094 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001095 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs06283a62011-08-31 10:37:30 -05001096 nest_level + 1);
1097 break;
1098
1099 default:
1100 printf("Ignoring malformed menu command: %.*s\n",
1101 (int)(*c - s), s);
1102 }
1103
1104 if (err < 0)
1105 return err;
1106
1107 eol_or_eof(c);
1108
1109 return 1;
1110}
1111
1112/*
1113 * Handles parsing a 'menu line' when we're parsing a label.
1114 */
1115static int parse_label_menu(char **c, struct pxe_menu *cfg,
1116 struct pxe_label *label)
1117{
1118 struct token t;
1119 char *s;
1120
1121 s = *c;
1122
1123 get_token(c, &t, L_KEYWORD);
1124
1125 switch (t.type) {
1126 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001127 if (!cfg->default_label)
1128 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001129
1130 if (!cfg->default_label)
1131 return -ENOMEM;
1132
1133 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001134 case T_LABEL:
1135 parse_sliteral(c, &label->menu);
1136 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001137 default:
1138 printf("Ignoring malformed menu command: %.*s\n",
1139 (int)(*c - s), s);
1140 }
1141
1142 eol_or_eof(c);
1143
1144 return 0;
1145}
1146
1147/*
1148 * Parses a label and adds it to the list of labels for a menu.
1149 *
1150 * A label ends when we either get to the end of a file, or
1151 * get some input we otherwise don't have a handler defined
1152 * for.
1153 *
1154 */
1155static int parse_label(char **c, struct pxe_menu *cfg)
1156{
1157 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001158 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001159 char *s = *c;
1160 struct pxe_label *label;
1161 int err;
1162
1163 label = label_create();
1164 if (!label)
1165 return -ENOMEM;
1166
1167 err = parse_sliteral(c, &label->name);
1168 if (err < 0) {
1169 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1170 label_destroy(label);
1171 return -EINVAL;
1172 }
1173
1174 list_add_tail(&label->list, &cfg->labels);
1175
1176 while (1) {
1177 s = *c;
1178 get_token(c, &t, L_KEYWORD);
1179
1180 err = 0;
1181 switch (t.type) {
1182 case T_MENU:
1183 err = parse_label_menu(c, cfg, label);
1184 break;
1185
1186 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001187 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001188 err = parse_sliteral(c, &label->kernel);
1189 break;
1190
1191 case T_APPEND:
1192 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001193 if (label->initrd)
1194 break;
1195 s = strstr(label->append, "initrd=");
1196 if (!s)
1197 break;
1198 s += 7;
1199 len = (int)(strchr(s, ' ') - s);
1200 label->initrd = malloc(len + 1);
1201 strncpy(label->initrd, s, len);
1202 label->initrd[len] = '\0';
1203
Jason Hobbs06283a62011-08-31 10:37:30 -05001204 break;
1205
1206 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001207 if (!label->initrd)
1208 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001209 break;
1210
Chander Kashyapa6559382012-09-06 19:36:31 +00001211 case T_FDT:
1212 if (!label->fdt)
1213 err = parse_sliteral(c, &label->fdt);
1214 break;
1215
Stephen Warrenc61d94d2014-01-28 14:50:10 -07001216 case T_FDTDIR:
1217 if (!label->fdtdir)
1218 err = parse_sliteral(c, &label->fdtdir);
1219 break;
1220
Jason Hobbs06283a62011-08-31 10:37:30 -05001221 case T_LOCALBOOT:
Rob Herring500f3042012-12-02 21:00:22 -06001222 label->localboot = 1;
1223 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001224 break;
1225
Rob Herring98f64672012-12-02 21:00:29 -06001226 case T_IPAPPEND:
1227 err = parse_integer(c, &label->ipappend);
1228 break;
1229
Jason Hobbs06283a62011-08-31 10:37:30 -05001230 case T_EOL:
1231 break;
1232
1233 default:
1234 /*
1235 * put the token back! we don't want it - it's the end
1236 * of a label and whatever token this is, it's
1237 * something for the menu level context to handle.
1238 */
1239 *c = s;
1240 return 1;
1241 }
1242
1243 if (err < 0)
1244 return err;
1245 }
1246}
1247
1248/*
1249 * This 16 comes from the limit pxelinux imposes on nested includes.
1250 *
1251 * There is no reason at all we couldn't do more, but some limit helps prevent
1252 * infinite (until crash occurs) recursion if a file tries to include itself.
1253 */
1254#define MAX_NEST_LEVEL 16
1255
1256/*
1257 * Entry point for parsing a menu file. nest_level indicates how many times
1258 * we've nested in includes. It will be 1 for the top level menu file.
1259 *
1260 * Returns 1 on success, < 0 on error.
1261 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001262static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs06283a62011-08-31 10:37:30 -05001263{
1264 struct token t;
1265 char *s, *b, *label_name;
1266 int err;
1267
1268 b = p;
1269
1270 if (nest_level > MAX_NEST_LEVEL) {
1271 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1272 return -EMLINK;
1273 }
1274
1275 while (1) {
1276 s = p;
1277
1278 get_token(&p, &t, L_KEYWORD);
1279
1280 err = 0;
1281 switch (t.type) {
1282 case T_MENU:
Rob Herringe82eeb52012-12-02 21:00:25 -06001283 cfg->prompt = 1;
Steven Falco0e3f3f82013-10-07 09:51:48 -04001284 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001285 break;
1286
1287 case T_TIMEOUT:
1288 err = parse_integer(&p, &cfg->timeout);
1289 break;
1290
1291 case T_LABEL:
1292 err = parse_label(&p, cfg);
1293 break;
1294
1295 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001296 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001297 err = parse_sliteral(&p, &label_name);
1298
1299 if (label_name) {
1300 if (cfg->default_label)
1301 free(cfg->default_label);
1302
1303 cfg->default_label = label_name;
1304 }
1305
1306 break;
1307
Rob Herring1e085222012-05-25 10:43:16 +00001308 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001309 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring1e085222012-05-25 10:43:16 +00001310 nest_level + 1);
1311 break;
1312
Jason Hobbs06283a62011-08-31 10:37:30 -05001313 case T_PROMPT:
Rob Herringe82eeb52012-12-02 21:00:25 -06001314 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001315 break;
1316
1317 case T_EOL:
1318 break;
1319
1320 case T_EOF:
1321 return 1;
1322
1323 default:
1324 printf("Ignoring unknown command: %.*s\n",
1325 (int)(p - s), s);
1326 eol_or_eof(&p);
1327 }
1328
1329 if (err < 0)
1330 return err;
1331 }
1332}
1333
1334/*
1335 * Free the memory used by a pxe_menu and its labels.
1336 */
1337static void destroy_pxe_menu(struct pxe_menu *cfg)
1338{
1339 struct list_head *pos, *n;
1340 struct pxe_label *label;
1341
1342 if (cfg->title)
1343 free(cfg->title);
1344
1345 if (cfg->default_label)
1346 free(cfg->default_label);
1347
1348 list_for_each_safe(pos, n, &cfg->labels) {
1349 label = list_entry(pos, struct pxe_label, list);
1350
1351 label_destroy(label);
1352 }
1353
1354 free(cfg);
1355}
1356
1357/*
1358 * Entry point for parsing a pxe file. This is only used for the top level
1359 * file.
1360 *
1361 * Returns NULL if there is an error, otherwise, returns a pointer to a
1362 * pxe_menu struct populated with the results of parsing the pxe file (and any
1363 * files it includes). The resulting pxe_menu struct can be free()'d by using
1364 * the destroy_pxe_menu() function.
1365 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001366static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001367{
1368 struct pxe_menu *cfg;
1369
1370 cfg = malloc(sizeof(struct pxe_menu));
1371
1372 if (!cfg)
1373 return NULL;
1374
1375 memset(cfg, 0, sizeof(struct pxe_menu));
1376
1377 INIT_LIST_HEAD(&cfg->labels);
1378
Steven Falco0e3f3f82013-10-07 09:51:48 -04001379 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001380 destroy_pxe_menu(cfg);
1381 return NULL;
1382 }
1383
1384 return cfg;
1385}
1386
1387/*
1388 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1389 * menu code.
1390 */
1391static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1392{
1393 struct pxe_label *label;
1394 struct list_head *pos;
1395 struct menu *m;
1396 int err;
Rob Herring32d2ffe2012-12-02 21:00:26 -06001397 int i = 1;
1398 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001399
1400 /*
1401 * Create a menu and add items for all the labels.
1402 */
Pali Rohárfc9d64f2013-03-23 14:50:40 +00001403 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1404 NULL, NULL);
Jason Hobbs06283a62011-08-31 10:37:30 -05001405
1406 if (!m)
1407 return NULL;
1408
1409 list_for_each(pos, &cfg->labels) {
1410 label = list_entry(pos, struct pxe_label, list);
1411
Rob Herring32d2ffe2012-12-02 21:00:26 -06001412 sprintf(label->num, "%d", i++);
1413 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001414 menu_destroy(m);
1415 return NULL;
1416 }
Rob Herring32d2ffe2012-12-02 21:00:26 -06001417 if (cfg->default_label &&
Rob Herring8577fec2012-12-02 21:00:27 -06001418 (strcmp(label->name, cfg->default_label) == 0))
Rob Herring32d2ffe2012-12-02 21:00:26 -06001419 default_num = label->num;
1420
Jason Hobbs06283a62011-08-31 10:37:30 -05001421 }
1422
1423 /*
1424 * After we've created items for each label in the menu, set the
1425 * menu's default label if one was specified.
1426 */
Rob Herring32d2ffe2012-12-02 21:00:26 -06001427 if (default_num) {
1428 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001429 if (err != 1) {
1430 if (err != -ENOENT) {
1431 menu_destroy(m);
1432 return NULL;
1433 }
1434
1435 printf("Missing default: %s\n", cfg->default_label);
1436 }
1437 }
1438
1439 return m;
1440}
1441
1442/*
1443 * Try to boot any labels we have yet to attempt to boot.
1444 */
Tom Rinid7884e02013-09-24 09:05:08 -04001445static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001446{
1447 struct list_head *pos;
1448 struct pxe_label *label;
1449
1450 list_for_each(pos, &cfg->labels) {
1451 label = list_entry(pos, struct pxe_label, list);
1452
1453 if (!label->attempted)
Tom Rinid7884e02013-09-24 09:05:08 -04001454 label_boot(cmdtp, label);
Jason Hobbs06283a62011-08-31 10:37:30 -05001455 }
1456}
1457
1458/*
1459 * Boot the system as prescribed by a pxe_menu.
1460 *
1461 * Use the menu system to either get the user's choice or the default, based
1462 * on config or user input. If there is no default or user's choice,
1463 * attempted to boot labels in the order they were given in pxe files.
1464 * If the default or user's choice fails to boot, attempt to boot other
1465 * labels in the order they were given in pxe files.
1466 *
1467 * If this function returns, there weren't any labels that successfully
1468 * booted, or the user interrupted the menu selection via ctrl+c.
1469 */
Tom Rinid7884e02013-09-24 09:05:08 -04001470static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001471{
1472 void *choice;
1473 struct menu *m;
1474 int err;
1475
1476 m = pxe_menu_to_menu(cfg);
1477 if (!m)
1478 return;
1479
1480 err = menu_get_choice(m, &choice);
1481
1482 menu_destroy(m);
1483
Jason Hobbs6f40f272011-11-07 03:07:15 +00001484 /*
1485 * err == 1 means we got a choice back from menu_get_choice.
1486 *
1487 * err == -ENOENT if the menu was setup to select the default but no
1488 * default was set. in that case, we should continue trying to boot
1489 * labels that haven't been attempted yet.
1490 *
1491 * otherwise, the user interrupted or there was some other error and
1492 * we give up.
1493 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001494
Rob Herring500f3042012-12-02 21:00:22 -06001495 if (err == 1) {
Tom Rinid7884e02013-09-24 09:05:08 -04001496 err = label_boot(cmdtp, choice);
Rob Herring500f3042012-12-02 21:00:22 -06001497 if (!err)
1498 return;
1499 } else if (err != -ENOENT) {
Jason Hobbs6f40f272011-11-07 03:07:15 +00001500 return;
Rob Herring500f3042012-12-02 21:00:22 -06001501 }
Jason Hobbs06283a62011-08-31 10:37:30 -05001502
Tom Rinid7884e02013-09-24 09:05:08 -04001503 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001504}
1505
1506/*
1507 * Boots a system using a pxe file
1508 *
1509 * Returns 0 on success, 1 on error.
1510 */
1511static int
1512do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1513{
1514 unsigned long pxefile_addr_r;
1515 struct pxe_menu *cfg;
1516 char *pxefile_addr_str;
1517
Rob Herring669df7e2012-05-25 10:47:39 +00001518 do_getfile = do_get_tftp;
1519
Jason Hobbs06283a62011-08-31 10:37:30 -05001520 if (argc == 1) {
1521 pxefile_addr_str = from_env("pxefile_addr_r");
1522 if (!pxefile_addr_str)
1523 return 1;
1524
1525 } else if (argc == 2) {
1526 pxefile_addr_str = argv[1];
1527 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001528 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001529 }
1530
1531 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1532 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1533 return 1;
1534 }
1535
Steven Falco0e3f3f82013-10-07 09:51:48 -04001536 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs06283a62011-08-31 10:37:30 -05001537
1538 if (cfg == NULL) {
1539 printf("Error parsing config file\n");
1540 return 1;
1541 }
1542
Tom Rinid7884e02013-09-24 09:05:08 -04001543 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001544
1545 destroy_pxe_menu(cfg);
1546
1547 return 0;
1548}
1549
1550static cmd_tbl_t cmd_pxe_sub[] = {
1551 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1552 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1553};
1554
1555int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1556{
1557 cmd_tbl_t *cp;
1558
1559 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001560 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001561
Rob Herringe5a9a402013-10-18 13:04:42 -05001562 is_pxe = true;
1563
Jason Hobbs06283a62011-08-31 10:37:30 -05001564 /* drop initial "pxe" arg */
1565 argc--;
1566 argv++;
1567
1568 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1569
1570 if (cp)
1571 return cp->cmd(cmdtp, flag, argc, argv);
1572
Simon Glass4c12eeb2011-12-10 08:44:01 +00001573 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001574}
1575
1576U_BOOT_CMD(
1577 pxe, 3, 1, do_pxe,
1578 "commands to get and boot from pxe files",
1579 "get - try to retrieve a pxe file using tftp\npxe "
1580 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1581);
Rob Herring669df7e2012-05-25 10:47:39 +00001582
1583/*
1584 * Boots a system using a local disk syslinux/extlinux file
1585 *
1586 * Returns 0 on success, 1 on error.
1587 */
1588int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1589{
1590 unsigned long pxefile_addr_r;
1591 struct pxe_menu *cfg;
1592 char *pxefile_addr_str;
1593 char *filename;
1594 int prompt = 0;
1595
Rob Herringe5a9a402013-10-18 13:04:42 -05001596 is_pxe = false;
1597
Rob Herring669df7e2012-05-25 10:47:39 +00001598 if (strstr(argv[1], "-p")) {
1599 prompt = 1;
1600 argc--;
1601 argv++;
1602 }
1603
1604 if (argc < 4)
1605 return cmd_usage(cmdtp);
1606
1607 if (argc < 5) {
1608 pxefile_addr_str = from_env("pxefile_addr_r");
1609 if (!pxefile_addr_str)
1610 return 1;
1611 } else {
1612 pxefile_addr_str = argv[4];
1613 }
1614
1615 if (argc < 6)
1616 filename = getenv("bootfile");
1617 else {
1618 filename = argv[5];
1619 setenv("bootfile", filename);
1620 }
1621
1622 if (strstr(argv[3], "ext2"))
1623 do_getfile = do_get_ext2;
1624 else if (strstr(argv[3], "fat"))
1625 do_getfile = do_get_fat;
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001626 else if (strstr(argv[3], "any"))
1627 do_getfile = do_get_any;
Rob Herring669df7e2012-05-25 10:47:39 +00001628 else {
1629 printf("Invalid filesystem: %s\n", argv[3]);
1630 return 1;
1631 }
1632 fs_argv[1] = argv[1];
1633 fs_argv[2] = argv[2];
1634
1635 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1636 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1637 return 1;
1638 }
1639
Steven Falco0e3f3f82013-10-07 09:51:48 -04001640 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herring669df7e2012-05-25 10:47:39 +00001641 printf("Error reading config file\n");
1642 return 1;
1643 }
1644
Steven Falco0e3f3f82013-10-07 09:51:48 -04001645 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herring669df7e2012-05-25 10:47:39 +00001646
1647 if (cfg == NULL) {
1648 printf("Error parsing config file\n");
1649 return 1;
1650 }
1651
1652 if (prompt)
1653 cfg->prompt = 1;
1654
Tom Rinid7884e02013-09-24 09:05:08 -04001655 handle_pxe_menu(cmdtp, cfg);
Rob Herring669df7e2012-05-25 10:47:39 +00001656
1657 destroy_pxe_menu(cfg);
1658
1659 return 0;
1660}
1661
1662U_BOOT_CMD(
1663 sysboot, 7, 1, do_sysboot,
1664 "command to get and boot from syslinux files",
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001665 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1666 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1667 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herring669df7e2012-05-25 10:47:39 +00001668);