blob: 2bd572d64691db5446d17d7af683405c9ceada9e [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>
14
15#include "menu.h"
16
17#define MAX_TFTP_PATH_LEN 127
18
Rob Herring39f98552012-12-02 21:00:28 -060019const char *pxe_default_paths[] = {
Joe Hershberger58d9ff92013-06-24 17:21:04 -050020#ifdef CONFIG_SYS_SOC
Rob Herring39f98552012-12-02 21:00:28 -060021 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger58d9ff92013-06-24 17:21:04 -050022#endif
Rob Herring39f98552012-12-02 21:00:28 -060023 "default-" CONFIG_SYS_ARCH,
24 "default",
25 NULL
26};
27
Rob Herringe5a9a402013-10-18 13:04:42 -050028static bool is_pxe;
29
Jason Hobbs06283a62011-08-31 10:37:30 -050030/*
31 * Like getenv, but prints an error if envvar isn't defined in the
32 * environment. It always returns what getenv does, so it can be used in
33 * place of getenv without changing error handling otherwise.
34 */
Rob Herring23b71942012-12-02 21:00:21 -060035static char *from_env(const char *envvar)
Jason Hobbs06283a62011-08-31 10:37:30 -050036{
37 char *ret;
38
39 ret = getenv(envvar);
40
41 if (!ret)
42 printf("missing environment variable: %s\n", envvar);
43
44 return ret;
45}
46
47/*
48 * Convert an ethaddr from the environment to the format used by pxelinux
49 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
50 * the beginning of the ethernet address to indicate a hardware type of
51 * Ethernet. Also converts uppercase hex characters into lowercase, to match
52 * pxelinux's behavior.
53 *
54 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
55 * environment, or some other value < 0 on error.
56 */
57static int format_mac_pxe(char *outbuf, size_t outbuf_len)
58{
Rob Herringef034c92012-12-02 21:00:20 -060059 uchar ethaddr[6];
Jason Hobbs06283a62011-08-31 10:37:30 -050060
Rob Herringef034c92012-12-02 21:00:20 -060061 if (outbuf_len < 21) {
David Feng5cea95c2013-12-14 11:47:30 +080062 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs06283a62011-08-31 10:37:30 -050063
64 return -EINVAL;
65 }
66
Rob Herringef034c92012-12-02 21:00:20 -060067 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
68 ethaddr))
69 return -ENOENT;
Jason Hobbs06283a62011-08-31 10:37:30 -050070
Rob Herringef034c92012-12-02 21:00:20 -060071 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
72 ethaddr[0], ethaddr[1], ethaddr[2],
73 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs06283a62011-08-31 10:37:30 -050074
75 return 1;
76}
77
78/*
79 * Returns the directory the file specified in the bootfile env variable is
80 * in. If bootfile isn't defined in the environment, return NULL, which should
81 * be interpreted as "don't prepend anything to paths".
82 */
Rob Herring90ba7d72012-03-28 05:51:36 +000083static int get_bootfile_path(const char *file_path, char *bootfile_path,
84 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -050085{
86 char *bootfile, *last_slash;
Rob Herring90ba7d72012-03-28 05:51:36 +000087 size_t path_len = 0;
88
Rob Herringe5a9a402013-10-18 13:04:42 -050089 /* Only syslinux allows absolute paths */
90 if (file_path[0] == '/' && !is_pxe)
Rob Herring90ba7d72012-03-28 05:51:36 +000091 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050092
93 bootfile = from_env("bootfile");
94
Rob Herring90ba7d72012-03-28 05:51:36 +000095 if (!bootfile)
96 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050097
98 last_slash = strrchr(bootfile, '/');
99
Rob Herring90ba7d72012-03-28 05:51:36 +0000100 if (last_slash == NULL)
101 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500102
103 path_len = (last_slash - bootfile) + 1;
104
105 if (bootfile_path_size < path_len) {
David Feng5cea95c2013-12-14 11:47:30 +0800106 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs06283a62011-08-31 10:37:30 -0500107 bootfile_path_size, path_len);
108
109 return -1;
110 }
111
112 strncpy(bootfile_path, bootfile, path_len);
113
Rob Herring90ba7d72012-03-28 05:51:36 +0000114 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500115 bootfile_path[path_len] = '\0';
116
117 return 1;
118}
119
Steven Falco0e3f3f82013-10-07 09:51:48 -0400120static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herring669df7e2012-05-25 10:47:39 +0000121
Steven Falco0e3f3f82013-10-07 09:51:48 -0400122static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000123{
124 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
125
126 tftp_argv[1] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600127 tftp_argv[2] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000128
Steven Falco0e3f3f82013-10-07 09:51:48 -0400129 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000130 return -ENOENT;
131
132 return 1;
133}
134
135static char *fs_argv[5];
136
Steven Falco0e3f3f82013-10-07 09:51:48 -0400137static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000138{
139#ifdef CONFIG_CMD_EXT2
140 fs_argv[0] = "ext2load";
141 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600142 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000143
Steven Falco0e3f3f82013-10-07 09:51:48 -0400144 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000145 return 1;
146#endif
147 return -ENOENT;
148}
149
Steven Falco0e3f3f82013-10-07 09:51:48 -0400150static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000151{
152#ifdef CONFIG_CMD_FAT
153 fs_argv[0] = "fatload";
154 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600155 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000156
Steven Falco0e3f3f82013-10-07 09:51:48 -0400157 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000158 return 1;
159#endif
160 return -ENOENT;
161}
162
Jason Hobbs06283a62011-08-31 10:37:30 -0500163/*
164 * As in pxelinux, paths to files referenced from files we retrieve are
165 * relative to the location of bootfile. get_relfile takes such a path and
166 * joins it with the bootfile path to get the full path to the target file. If
167 * the bootfile path is NULL, we use file_path as is.
168 *
169 * Returns 1 for success, or < 0 on error.
170 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400171static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500172{
173 size_t path_len;
174 char relfile[MAX_TFTP_PATH_LEN+1];
175 char addr_buf[10];
Jason Hobbs06283a62011-08-31 10:37:30 -0500176 int err;
177
Rob Herring90ba7d72012-03-28 05:51:36 +0000178 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-08-31 10:37:30 -0500179
180 if (err < 0)
181 return err;
182
183 path_len = strlen(file_path);
184 path_len += strlen(relfile);
185
186 if (path_len > MAX_TFTP_PATH_LEN) {
187 printf("Base path too long (%s%s)\n",
188 relfile,
189 file_path);
190
191 return -ENAMETOOLONG;
192 }
193
194 strcat(relfile, file_path);
195
196 printf("Retrieving file: %s\n", relfile);
197
198 sprintf(addr_buf, "%p", file_addr);
199
Steven Falco0e3f3f82013-10-07 09:51:48 -0400200 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs06283a62011-08-31 10:37:30 -0500201}
202
203/*
204 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
205 * 'bootfile' was specified in the environment, the path to bootfile will be
206 * prepended to 'file_path' and the resulting path will be used.
207 *
208 * Returns 1 on success, or < 0 for error.
209 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400210static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500211{
212 unsigned long config_file_size;
213 char *tftp_filesize;
214 int err;
215
Steven Falco0e3f3f82013-10-07 09:51:48 -0400216 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500217
218 if (err < 0)
219 return err;
220
221 /*
222 * the file comes without a NUL byte at the end, so find out its size
223 * and add the NUL byte.
224 */
225 tftp_filesize = from_env("filesize");
226
227 if (!tftp_filesize)
228 return -ENOENT;
229
230 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
231 return -EINVAL;
232
233 *(char *)(file_addr + config_file_size) = '\0';
234
235 return 1;
236}
237
238#define PXELINUX_DIR "pxelinux.cfg/"
239
240/*
241 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
242 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
243 * from the bootfile path, as described above.
244 *
245 * Returns 1 on success or < 0 on error.
246 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400247static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500248{
249 size_t base_len = strlen(PXELINUX_DIR);
250 char path[MAX_TFTP_PATH_LEN+1];
251
252 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
253 printf("path (%s%s) too long, skipping\n",
254 PXELINUX_DIR, file);
255 return -ENAMETOOLONG;
256 }
257
258 sprintf(path, PXELINUX_DIR "%s", file);
259
Steven Falco0e3f3f82013-10-07 09:51:48 -0400260 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500261}
262
263/*
264 * Looks for a pxe file with a name based on the pxeuuid environment variable.
265 *
266 * Returns 1 on success or < 0 on error.
267 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400268static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500269{
270 char *uuid_str;
271
272 uuid_str = from_env("pxeuuid");
273
274 if (!uuid_str)
275 return -ENOENT;
276
Steven Falco0e3f3f82013-10-07 09:51:48 -0400277 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500278}
279
280/*
281 * Looks for a pxe file with a name based on the 'ethaddr' environment
282 * variable.
283 *
284 * Returns 1 on success or < 0 on error.
285 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400286static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500287{
288 char mac_str[21];
289 int err;
290
291 err = format_mac_pxe(mac_str, sizeof(mac_str));
292
293 if (err < 0)
294 return err;
295
Steven Falco0e3f3f82013-10-07 09:51:48 -0400296 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500297}
298
299/*
300 * Looks for pxe files with names based on our IP address. See pxelinux
301 * documentation for details on what these file names look like. We match
302 * that exactly.
303 *
304 * Returns 1 on success or < 0 on error.
305 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400306static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500307{
308 char ip_addr[9];
309 int mask_pos, err;
310
311 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
312
313 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400314 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500315
316 if (err > 0)
317 return err;
318
319 ip_addr[mask_pos] = '\0';
320 }
321
322 return -ENOENT;
323}
324
325/*
326 * Entry point for the 'pxe get' command.
327 * This Follows pxelinux's rules to download a config file from a tftp server.
328 * The file is stored at the location given by the pxefile_addr_r environment
329 * variable, which must be set.
330 *
331 * UUID comes from pxeuuid env variable, if defined
332 * MAC addr comes from ethaddr env variable, if defined
333 * IP
334 *
335 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
336 *
337 * Returns 0 on success or 1 on error.
338 */
339static int
340do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
341{
342 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000343 unsigned long pxefile_addr_r;
Rob Herring39f98552012-12-02 21:00:28 -0600344 int err, i = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500345
Rob Herring669df7e2012-05-25 10:47:39 +0000346 do_getfile = do_get_tftp;
347
Jason Hobbs06283a62011-08-31 10:37:30 -0500348 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000349 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500350
Jason Hobbs06283a62011-08-31 10:37:30 -0500351 pxefile_addr_str = from_env("pxefile_addr_r");
352
353 if (!pxefile_addr_str)
354 return 1;
355
356 err = strict_strtoul(pxefile_addr_str, 16,
357 (unsigned long *)&pxefile_addr_r);
358 if (err < 0)
359 return 1;
360
361 /*
362 * Keep trying paths until we successfully get a file we're looking
363 * for.
364 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400365 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
366 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
367 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500368 printf("Config file found\n");
369
370 return 0;
371 }
372
Rob Herring39f98552012-12-02 21:00:28 -0600373 while (pxe_default_paths[i]) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400374 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herring39f98552012-12-02 21:00:28 -0600375 (void *)pxefile_addr_r) > 0) {
376 printf("Config file found\n");
377 return 0;
378 }
379 i++;
380 }
381
Jason Hobbs06283a62011-08-31 10:37:30 -0500382 printf("Config file not found\n");
383
384 return 1;
385}
386
387/*
388 * Wrapper to make it easier to store the file at file_path in the location
389 * specified by envaddr_name. file_path will be joined to the bootfile path,
390 * if any is specified.
391 *
392 * Returns 1 on success or < 0 on error.
393 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400394static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs06283a62011-08-31 10:37:30 -0500395{
Jason Hobbs834c9382012-03-05 08:12:28 +0000396 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500397 char *envaddr;
398
399 envaddr = from_env(envaddr_name);
400
401 if (!envaddr)
402 return -ENOENT;
403
Jason Hobbs834c9382012-03-05 08:12:28 +0000404 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500405 return -EINVAL;
406
Steven Falco0e3f3f82013-10-07 09:51:48 -0400407 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500408}
409
410/*
411 * A note on the pxe file parser.
412 *
413 * We're parsing files that use syslinux grammar, which has a few quirks.
414 * String literals must be recognized based on context - there is no
415 * quoting or escaping support. There's also nothing to explicitly indicate
416 * when a label section completes. We deal with that by ending a label
417 * section whenever we see a line that doesn't include.
418 *
419 * As with the syslinux family, this same file format could be reused in the
420 * future for non pxe purposes. The only action it takes during parsing that
421 * would throw this off is handling of include files. It assumes we're using
422 * pxe, and does a tftp download of a file listed as an include file in the
423 * middle of the parsing operation. That could be handled by refactoring it to
424 * take a 'include file getter' function.
425 */
426
427/*
428 * Describes a single label given in a pxe file.
429 *
430 * Create these with the 'label_create' function given below.
431 *
432 * name - the name of the menu as given on the 'menu label' line.
433 * kernel - the path to the kernel file to use for this label.
434 * append - kernel command line to use when booting this label
435 * initrd - path to the initrd to use for this label.
436 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
437 * localboot - 1 if this label specified 'localboot', 0 otherwise.
438 * list - lets these form a list, which a pxe_menu struct will hold.
439 */
440struct pxe_label {
Rob Herring32d2ffe2012-12-02 21:00:26 -0600441 char num[4];
Jason Hobbs06283a62011-08-31 10:37:30 -0500442 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000443 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500444 char *kernel;
445 char *append;
446 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000447 char *fdt;
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700448 char *fdtdir;
Rob Herring98f64672012-12-02 21:00:29 -0600449 int ipappend;
Jason Hobbs06283a62011-08-31 10:37:30 -0500450 int attempted;
451 int localboot;
Rob Herring500f3042012-12-02 21:00:22 -0600452 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500453 struct list_head list;
454};
455
456/*
457 * Describes a pxe menu as given via pxe files.
458 *
459 * title - the name of the menu as given by a 'menu title' line.
460 * default_label - the name of the default label, if any.
461 * timeout - time in tenths of a second to wait for a user key-press before
462 * booting the default label.
463 * prompt - if 0, don't prompt for a choice unless the timeout period is
464 * interrupted. If 1, always prompt for a choice regardless of
465 * timeout.
466 * labels - a list of labels defined for the menu.
467 */
468struct pxe_menu {
469 char *title;
470 char *default_label;
471 int timeout;
472 int prompt;
473 struct list_head labels;
474};
475
476/*
477 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
478 * result must be free()'d to reclaim the memory.
479 *
480 * Returns NULL if malloc fails.
481 */
482static struct pxe_label *label_create(void)
483{
484 struct pxe_label *label;
485
486 label = malloc(sizeof(struct pxe_label));
487
488 if (!label)
489 return NULL;
490
491 memset(label, 0, sizeof(struct pxe_label));
492
493 return label;
494}
495
496/*
497 * Free the memory used by a pxe_label, including that used by its name,
498 * kernel, append and initrd members, if they're non NULL.
499 *
500 * So - be sure to only use dynamically allocated memory for the members of
501 * the pxe_label struct, unless you want to clean it up first. These are
502 * currently only created by the pxe file parsing code.
503 */
504static void label_destroy(struct pxe_label *label)
505{
506 if (label->name)
507 free(label->name);
508
509 if (label->kernel)
510 free(label->kernel);
511
512 if (label->append)
513 free(label->append);
514
515 if (label->initrd)
516 free(label->initrd);
517
Chander Kashyapa6559382012-09-06 19:36:31 +0000518 if (label->fdt)
519 free(label->fdt);
520
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700521 if (label->fdtdir)
522 free(label->fdtdir);
523
Jason Hobbs06283a62011-08-31 10:37:30 -0500524 free(label);
525}
526
527/*
528 * Print a label and its string members if they're defined.
529 *
530 * This is passed as a callback to the menu code for displaying each
531 * menu entry.
532 */
533static void label_print(void *data)
534{
535 struct pxe_label *label = data;
Rob Herring32d2ffe2012-12-02 21:00:26 -0600536 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500537
Rob Herring32d2ffe2012-12-02 21:00:26 -0600538 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500539}
540
541/*
542 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
543 * environment variable is defined. Its contents will be executed as U-boot
544 * command. If the label specified an 'append' line, its contents will be
545 * used to overwrite the contents of the 'bootargs' environment variable prior
546 * to running 'localcmd'.
547 *
548 * Returns 1 on success or < 0 on error.
549 */
550static int label_localboot(struct pxe_label *label)
551{
Simon Glassd51004a2012-03-30 21:30:55 +0000552 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500553
554 localcmd = from_env("localcmd");
555
556 if (!localcmd)
557 return -ENOENT;
558
Jason Hobbs06283a62011-08-31 10:37:30 -0500559 if (label->append)
560 setenv("bootargs", label->append);
561
Simon Glassd51004a2012-03-30 21:30:55 +0000562 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500563
Simon Glassd51004a2012-03-30 21:30:55 +0000564 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500565}
566
567/*
568 * Boot according to the contents of a pxe_label.
569 *
570 * If we can't boot for any reason, we return. A successful boot never
571 * returns.
572 *
573 * The kernel will be stored in the location given by the 'kernel_addr_r'
574 * environment variable.
575 *
576 * If the label specifies an initrd file, it will be stored in the location
577 * given by the 'ramdisk_addr_r' environment variable.
578 *
579 * If the label specifies an 'append' line, its contents will overwrite that
580 * of the 'bootargs' environment variable.
581 */
Tom Rinid7884e02013-09-24 09:05:08 -0400582static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500583{
584 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600585 char initrd_str[22];
Rob Herring98f64672012-12-02 21:00:29 -0600586 char mac_str[29] = "";
587 char ip_str[68] = "";
588 char *bootargs;
Jason Hobbs06283a62011-08-31 10:37:30 -0500589 int bootm_argc = 3;
Rob Herring98f64672012-12-02 21:00:29 -0600590 int len = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500591
592 label_print(label);
593
594 label->attempted = 1;
595
596 if (label->localboot) {
Rob Herring500f3042012-12-02 21:00:22 -0600597 if (label->localboot_val >= 0)
598 label_localboot(label);
599 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500600 }
601
602 if (label->kernel == NULL) {
603 printf("No kernel given, skipping %s\n",
604 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600605 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500606 }
607
608 if (label->initrd) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400609 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500610 printf("Skipping %s for failure retrieving initrd\n",
611 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600612 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500613 }
614
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600615 bootm_argv[2] = initrd_str;
616 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
617 strcat(bootm_argv[2], ":");
618 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500619 } else {
620 bootm_argv[2] = "-";
621 }
622
Steven Falco0e3f3f82013-10-07 09:51:48 -0400623 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500624 printf("Skipping %s for failure retrieving kernel\n",
625 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600626 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500627 }
628
Rob Herring98f64672012-12-02 21:00:29 -0600629 if (label->ipappend & 0x1) {
630 sprintf(ip_str, " ip=%s:%s:%s:%s",
631 getenv("ipaddr"), getenv("serverip"),
632 getenv("gatewayip"), getenv("netmask"));
633 len += strlen(ip_str);
634 }
635
636 if (label->ipappend & 0x2) {
637 int err;
638 strcpy(mac_str, " BOOTIF=");
639 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
640 if (err < 0)
641 mac_str[0] = '\0';
642 len += strlen(mac_str);
643 }
644
645 if (label->append)
646 len += strlen(label->append);
647
648 if (len) {
649 bootargs = malloc(len + 1);
650 if (!bootargs)
651 return 1;
652 bootargs[0] = '\0';
653 if (label->append)
654 strcpy(bootargs, label->append);
655 strcat(bootargs, ip_str);
656 strcat(bootargs, mac_str);
657
658 setenv("bootargs", bootargs);
659 printf("append: %s\n", bootargs);
660
661 free(bootargs);
Rob Herring32d2ffe2012-12-02 21:00:26 -0600662 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500663
664 bootm_argv[1] = getenv("kernel_addr_r");
665
666 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000667 * fdt usage is optional:
668 * It handles the following scenarios. All scenarios are exclusive
669 *
670 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
671 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
672 * and adjust argc appropriately.
673 *
674 * Scenario 2: If there is an fdt_addr specified, pass it along to
675 * bootm, and adjust argc appropriately.
676 *
677 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500678 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000679 bootm_argv[3] = getenv("fdt_addr_r");
680
681 /* if fdt label is defined then get fdt from server */
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700682 if (bootm_argv[3]) {
683 char *fdtfile = NULL;
684 char *fdtfilefree = NULL;
685
686 if (label->fdt) {
687 fdtfile = label->fdt;
688 } else if (label->fdtdir) {
689 fdtfile = getenv("fdtfile");
690 /*
691 * For complex cases, it might be worth calling a
692 * board- or SoC-provided function here to provide a
693 * better default:
694 *
695 * if (!fdtfile)
696 * fdtfile = gen_fdtfile();
697 *
698 * If this is added, be sure to keep the default below,
699 * or move it to the default weak implementation of
700 * gen_fdtfile().
701 */
702 if (!fdtfile) {
703 char *soc = getenv("soc");
704 char *board = getenv("board");
705 char *slash;
706
707 len = strlen(label->fdtdir);
708 if (!len)
709 slash = "./";
710 else if (label->fdtdir[len - 1] != '/')
711 slash = "/";
712 else
713 slash = "";
714
715 len = strlen(label->fdtdir) + strlen(slash) +
716 strlen(soc) + 1 + strlen(board) + 5;
717 fdtfilefree = malloc(len);
718 if (!fdtfilefree) {
719 printf("malloc fail (FDT filename)\n");
720 return 1;
721 }
722
723 snprintf(fdtfilefree, len, "%s%s%s-%s.dtb",
724 label->fdtdir, slash, soc, board);
725 fdtfile = fdtfilefree;
726 }
Chander Kashyapa6559382012-09-06 19:36:31 +0000727 }
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700728
729 if (fdtfile) {
730 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
731 free(fdtfilefree);
732 if (err < 0) {
733 printf("Skipping %s for failure retrieving fdt\n",
734 label->name);
735 return 1;
736 }
737 } else {
738 bootm_argv[3] = NULL;
739 }
740 }
741
742 if (!bootm_argv[3])
Chander Kashyapa6559382012-09-06 19:36:31 +0000743 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500744
745 if (bootm_argv[3])
746 bootm_argc = 4;
747
Tom Rinid7884e02013-09-24 09:05:08 -0400748 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600749
750#ifdef CONFIG_CMD_BOOTZ
751 /* Try booting a zImage if do_bootm returns */
Tom Rinid7884e02013-09-24 09:05:08 -0400752 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600753#endif
Rob Herring500f3042012-12-02 21:00:22 -0600754 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500755}
756
757/*
758 * Tokens for the pxe file parser.
759 */
760enum token_type {
761 T_EOL,
762 T_STRING,
763 T_EOF,
764 T_MENU,
765 T_TITLE,
766 T_TIMEOUT,
767 T_LABEL,
768 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000769 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500770 T_APPEND,
771 T_INITRD,
772 T_LOCALBOOT,
773 T_DEFAULT,
774 T_PROMPT,
775 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000776 T_FDT,
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700777 T_FDTDIR,
Rob Herring8577fec2012-12-02 21:00:27 -0600778 T_ONTIMEOUT,
Rob Herring98f64672012-12-02 21:00:29 -0600779 T_IPAPPEND,
Jason Hobbs06283a62011-08-31 10:37:30 -0500780 T_INVALID
781};
782
783/*
784 * A token - given by a value and a type.
785 */
786struct token {
787 char *val;
788 enum token_type type;
789};
790
791/*
792 * Keywords recognized.
793 */
794static const struct token keywords[] = {
795 {"menu", T_MENU},
796 {"title", T_TITLE},
797 {"timeout", T_TIMEOUT},
798 {"default", T_DEFAULT},
799 {"prompt", T_PROMPT},
800 {"label", T_LABEL},
801 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000802 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500803 {"localboot", T_LOCALBOOT},
804 {"append", T_APPEND},
805 {"initrd", T_INITRD},
806 {"include", T_INCLUDE},
Stephen Warrenf43c4012014-01-28 14:50:09 -0700807 {"devicetree", T_FDT},
Chander Kashyapa6559382012-09-06 19:36:31 +0000808 {"fdt", T_FDT},
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700809 {"devicetreedir", T_FDTDIR},
810 {"fdtdir", T_FDTDIR},
Rob Herring8577fec2012-12-02 21:00:27 -0600811 {"ontimeout", T_ONTIMEOUT,},
Rob Herring98f64672012-12-02 21:00:29 -0600812 {"ipappend", T_IPAPPEND,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500813 {NULL, T_INVALID}
814};
815
816/*
817 * Since pxe(linux) files don't have a token to identify the start of a
818 * literal, we have to keep track of when we're in a state where a literal is
819 * expected vs when we're in a state a keyword is expected.
820 */
821enum lex_state {
822 L_NORMAL = 0,
823 L_KEYWORD,
824 L_SLITERAL
825};
826
827/*
828 * get_string retrieves a string from *p and stores it as a token in
829 * *t.
830 *
831 * get_string used for scanning both string literals and keywords.
832 *
833 * Characters from *p are copied into t-val until a character equal to
834 * delim is found, or a NUL byte is reached. If delim has the special value of
835 * ' ', any whitespace character will be used as a delimiter.
836 *
837 * If lower is unequal to 0, uppercase characters will be converted to
838 * lowercase in the result. This is useful to make keywords case
839 * insensitive.
840 *
841 * The location of *p is updated to point to the first character after the end
842 * of the token - the ending delimiter.
843 *
844 * On success, the new value of t->val is returned. Memory for t->val is
845 * allocated using malloc and must be free()'d to reclaim it. If insufficient
846 * memory is available, NULL is returned.
847 */
848static char *get_string(char **p, struct token *t, char delim, int lower)
849{
850 char *b, *e;
851 size_t len, i;
852
853 /*
854 * b and e both start at the beginning of the input stream.
855 *
856 * e is incremented until we find the ending delimiter, or a NUL byte
857 * is reached. Then, we take e - b to find the length of the token.
858 */
859 b = e = *p;
860
861 while (*e) {
862 if ((delim == ' ' && isspace(*e)) || delim == *e)
863 break;
864 e++;
865 }
866
867 len = e - b;
868
869 /*
870 * Allocate memory to hold the string, and copy it in, converting
871 * characters to lowercase if lower is != 0.
872 */
873 t->val = malloc(len + 1);
874 if (!t->val)
875 return NULL;
876
877 for (i = 0; i < len; i++, b++) {
878 if (lower)
879 t->val[i] = tolower(*b);
880 else
881 t->val[i] = *b;
882 }
883
884 t->val[len] = '\0';
885
886 /*
887 * Update *p so the caller knows where to continue scanning.
888 */
889 *p = e;
890
891 t->type = T_STRING;
892
893 return t->val;
894}
895
896/*
897 * Populate a keyword token with a type and value.
898 */
899static void get_keyword(struct token *t)
900{
901 int i;
902
903 for (i = 0; keywords[i].val; i++) {
904 if (!strcmp(t->val, keywords[i].val)) {
905 t->type = keywords[i].type;
906 break;
907 }
908 }
909}
910
911/*
912 * Get the next token. We have to keep track of which state we're in to know
913 * if we're looking to get a string literal or a keyword.
914 *
915 * *p is updated to point at the first character after the current token.
916 */
917static void get_token(char **p, struct token *t, enum lex_state state)
918{
919 char *c = *p;
920
921 t->type = T_INVALID;
922
923 /* eat non EOL whitespace */
924 while (isblank(*c))
925 c++;
926
927 /*
928 * eat comments. note that string literals can't begin with #, but
929 * can contain a # after their first character.
930 */
931 if (*c == '#') {
932 while (*c && *c != '\n')
933 c++;
934 }
935
936 if (*c == '\n') {
937 t->type = T_EOL;
938 c++;
939 } else if (*c == '\0') {
940 t->type = T_EOF;
941 c++;
942 } else if (state == L_SLITERAL) {
943 get_string(&c, t, '\n', 0);
944 } else if (state == L_KEYWORD) {
945 /*
946 * when we expect a keyword, we first get the next string
947 * token delimited by whitespace, and then check if it
948 * matches a keyword in our keyword list. if it does, it's
949 * converted to a keyword token of the appropriate type, and
950 * if not, it remains a string token.
951 */
952 get_string(&c, t, ' ', 1);
953 get_keyword(t);
954 }
955
956 *p = c;
957}
958
959/*
960 * Increment *c until we get to the end of the current line, or EOF.
961 */
962static void eol_or_eof(char **c)
963{
964 while (**c && **c != '\n')
965 (*c)++;
966}
967
968/*
969 * All of these parse_* functions share some common behavior.
970 *
971 * They finish with *c pointing after the token they parse, and return 1 on
972 * success, or < 0 on error.
973 */
974
975/*
976 * Parse a string literal and store a pointer it at *dst. String literals
977 * terminate at the end of the line.
978 */
979static int parse_sliteral(char **c, char **dst)
980{
981 struct token t;
982 char *s = *c;
983
984 get_token(c, &t, L_SLITERAL);
985
986 if (t.type != T_STRING) {
987 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
988 return -EINVAL;
989 }
990
991 *dst = t.val;
992
993 return 1;
994}
995
996/*
997 * Parse a base 10 (unsigned) integer and store it at *dst.
998 */
999static int parse_integer(char **c, int *dst)
1000{
1001 struct token t;
1002 char *s = *c;
Jason Hobbs06283a62011-08-31 10:37:30 -05001003
1004 get_token(c, &t, L_SLITERAL);
1005
1006 if (t.type != T_STRING) {
1007 printf("Expected string: %.*s\n", (int)(*c - s), s);
1008 return -EINVAL;
1009 }
1010
Rob Herring500f3042012-12-02 21:00:22 -06001011 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -05001012
1013 free(t.val);
1014
1015 return 1;
1016}
1017
Steven Falco0e3f3f82013-10-07 09:51:48 -04001018static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001019
1020/*
1021 * Parse an include statement, and retrieve and parse the file it mentions.
1022 *
1023 * base should point to a location where it's safe to store the file, and
1024 * nest_level should indicate how many nested includes have occurred. For this
1025 * include, nest_level has already been incremented and doesn't need to be
1026 * incremented here.
1027 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001028static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs06283a62011-08-31 10:37:30 -05001029 struct pxe_menu *cfg, int nest_level)
1030{
1031 char *include_path;
1032 char *s = *c;
1033 int err;
1034
1035 err = parse_sliteral(c, &include_path);
1036
1037 if (err < 0) {
1038 printf("Expected include path: %.*s\n",
1039 (int)(*c - s), s);
1040 return err;
1041 }
1042
Steven Falco0e3f3f82013-10-07 09:51:48 -04001043 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs06283a62011-08-31 10:37:30 -05001044
1045 if (err < 0) {
1046 printf("Couldn't retrieve %s\n", include_path);
1047 return err;
1048 }
1049
Steven Falco0e3f3f82013-10-07 09:51:48 -04001050 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001051}
1052
1053/*
1054 * Parse lines that begin with 'menu'.
1055 *
1056 * b and nest are provided to handle the 'menu include' case.
1057 *
1058 * b should be the address where the file currently being parsed is stored.
1059 *
1060 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1061 * a file it includes, 3 when parsing a file included by that file, and so on.
1062 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001063static 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 -05001064{
1065 struct token t;
1066 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +00001067 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -05001068
1069 get_token(c, &t, L_KEYWORD);
1070
1071 switch (t.type) {
1072 case T_TITLE:
1073 err = parse_sliteral(c, &cfg->title);
1074
1075 break;
1076
1077 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001078 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs06283a62011-08-31 10:37:30 -05001079 nest_level + 1);
1080 break;
1081
1082 default:
1083 printf("Ignoring malformed menu command: %.*s\n",
1084 (int)(*c - s), s);
1085 }
1086
1087 if (err < 0)
1088 return err;
1089
1090 eol_or_eof(c);
1091
1092 return 1;
1093}
1094
1095/*
1096 * Handles parsing a 'menu line' when we're parsing a label.
1097 */
1098static int parse_label_menu(char **c, struct pxe_menu *cfg,
1099 struct pxe_label *label)
1100{
1101 struct token t;
1102 char *s;
1103
1104 s = *c;
1105
1106 get_token(c, &t, L_KEYWORD);
1107
1108 switch (t.type) {
1109 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001110 if (!cfg->default_label)
1111 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001112
1113 if (!cfg->default_label)
1114 return -ENOMEM;
1115
1116 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001117 case T_LABEL:
1118 parse_sliteral(c, &label->menu);
1119 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001120 default:
1121 printf("Ignoring malformed menu command: %.*s\n",
1122 (int)(*c - s), s);
1123 }
1124
1125 eol_or_eof(c);
1126
1127 return 0;
1128}
1129
1130/*
1131 * Parses a label and adds it to the list of labels for a menu.
1132 *
1133 * A label ends when we either get to the end of a file, or
1134 * get some input we otherwise don't have a handler defined
1135 * for.
1136 *
1137 */
1138static int parse_label(char **c, struct pxe_menu *cfg)
1139{
1140 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001141 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001142 char *s = *c;
1143 struct pxe_label *label;
1144 int err;
1145
1146 label = label_create();
1147 if (!label)
1148 return -ENOMEM;
1149
1150 err = parse_sliteral(c, &label->name);
1151 if (err < 0) {
1152 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1153 label_destroy(label);
1154 return -EINVAL;
1155 }
1156
1157 list_add_tail(&label->list, &cfg->labels);
1158
1159 while (1) {
1160 s = *c;
1161 get_token(c, &t, L_KEYWORD);
1162
1163 err = 0;
1164 switch (t.type) {
1165 case T_MENU:
1166 err = parse_label_menu(c, cfg, label);
1167 break;
1168
1169 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001170 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001171 err = parse_sliteral(c, &label->kernel);
1172 break;
1173
1174 case T_APPEND:
1175 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001176 if (label->initrd)
1177 break;
1178 s = strstr(label->append, "initrd=");
1179 if (!s)
1180 break;
1181 s += 7;
1182 len = (int)(strchr(s, ' ') - s);
1183 label->initrd = malloc(len + 1);
1184 strncpy(label->initrd, s, len);
1185 label->initrd[len] = '\0';
1186
Jason Hobbs06283a62011-08-31 10:37:30 -05001187 break;
1188
1189 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001190 if (!label->initrd)
1191 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001192 break;
1193
Chander Kashyapa6559382012-09-06 19:36:31 +00001194 case T_FDT:
1195 if (!label->fdt)
1196 err = parse_sliteral(c, &label->fdt);
1197 break;
1198
Stephen Warrenc61d94d2014-01-28 14:50:10 -07001199 case T_FDTDIR:
1200 if (!label->fdtdir)
1201 err = parse_sliteral(c, &label->fdtdir);
1202 break;
1203
Jason Hobbs06283a62011-08-31 10:37:30 -05001204 case T_LOCALBOOT:
Rob Herring500f3042012-12-02 21:00:22 -06001205 label->localboot = 1;
1206 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001207 break;
1208
Rob Herring98f64672012-12-02 21:00:29 -06001209 case T_IPAPPEND:
1210 err = parse_integer(c, &label->ipappend);
1211 break;
1212
Jason Hobbs06283a62011-08-31 10:37:30 -05001213 case T_EOL:
1214 break;
1215
1216 default:
1217 /*
1218 * put the token back! we don't want it - it's the end
1219 * of a label and whatever token this is, it's
1220 * something for the menu level context to handle.
1221 */
1222 *c = s;
1223 return 1;
1224 }
1225
1226 if (err < 0)
1227 return err;
1228 }
1229}
1230
1231/*
1232 * This 16 comes from the limit pxelinux imposes on nested includes.
1233 *
1234 * There is no reason at all we couldn't do more, but some limit helps prevent
1235 * infinite (until crash occurs) recursion if a file tries to include itself.
1236 */
1237#define MAX_NEST_LEVEL 16
1238
1239/*
1240 * Entry point for parsing a menu file. nest_level indicates how many times
1241 * we've nested in includes. It will be 1 for the top level menu file.
1242 *
1243 * Returns 1 on success, < 0 on error.
1244 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001245static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs06283a62011-08-31 10:37:30 -05001246{
1247 struct token t;
1248 char *s, *b, *label_name;
1249 int err;
1250
1251 b = p;
1252
1253 if (nest_level > MAX_NEST_LEVEL) {
1254 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1255 return -EMLINK;
1256 }
1257
1258 while (1) {
1259 s = p;
1260
1261 get_token(&p, &t, L_KEYWORD);
1262
1263 err = 0;
1264 switch (t.type) {
1265 case T_MENU:
Rob Herringe82eeb52012-12-02 21:00:25 -06001266 cfg->prompt = 1;
Steven Falco0e3f3f82013-10-07 09:51:48 -04001267 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001268 break;
1269
1270 case T_TIMEOUT:
1271 err = parse_integer(&p, &cfg->timeout);
1272 break;
1273
1274 case T_LABEL:
1275 err = parse_label(&p, cfg);
1276 break;
1277
1278 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001279 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001280 err = parse_sliteral(&p, &label_name);
1281
1282 if (label_name) {
1283 if (cfg->default_label)
1284 free(cfg->default_label);
1285
1286 cfg->default_label = label_name;
1287 }
1288
1289 break;
1290
Rob Herring1e085222012-05-25 10:43:16 +00001291 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001292 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring1e085222012-05-25 10:43:16 +00001293 nest_level + 1);
1294 break;
1295
Jason Hobbs06283a62011-08-31 10:37:30 -05001296 case T_PROMPT:
Rob Herringe82eeb52012-12-02 21:00:25 -06001297 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001298 break;
1299
1300 case T_EOL:
1301 break;
1302
1303 case T_EOF:
1304 return 1;
1305
1306 default:
1307 printf("Ignoring unknown command: %.*s\n",
1308 (int)(p - s), s);
1309 eol_or_eof(&p);
1310 }
1311
1312 if (err < 0)
1313 return err;
1314 }
1315}
1316
1317/*
1318 * Free the memory used by a pxe_menu and its labels.
1319 */
1320static void destroy_pxe_menu(struct pxe_menu *cfg)
1321{
1322 struct list_head *pos, *n;
1323 struct pxe_label *label;
1324
1325 if (cfg->title)
1326 free(cfg->title);
1327
1328 if (cfg->default_label)
1329 free(cfg->default_label);
1330
1331 list_for_each_safe(pos, n, &cfg->labels) {
1332 label = list_entry(pos, struct pxe_label, list);
1333
1334 label_destroy(label);
1335 }
1336
1337 free(cfg);
1338}
1339
1340/*
1341 * Entry point for parsing a pxe file. This is only used for the top level
1342 * file.
1343 *
1344 * Returns NULL if there is an error, otherwise, returns a pointer to a
1345 * pxe_menu struct populated with the results of parsing the pxe file (and any
1346 * files it includes). The resulting pxe_menu struct can be free()'d by using
1347 * the destroy_pxe_menu() function.
1348 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001349static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001350{
1351 struct pxe_menu *cfg;
1352
1353 cfg = malloc(sizeof(struct pxe_menu));
1354
1355 if (!cfg)
1356 return NULL;
1357
1358 memset(cfg, 0, sizeof(struct pxe_menu));
1359
1360 INIT_LIST_HEAD(&cfg->labels);
1361
Steven Falco0e3f3f82013-10-07 09:51:48 -04001362 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001363 destroy_pxe_menu(cfg);
1364 return NULL;
1365 }
1366
1367 return cfg;
1368}
1369
1370/*
1371 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1372 * menu code.
1373 */
1374static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1375{
1376 struct pxe_label *label;
1377 struct list_head *pos;
1378 struct menu *m;
1379 int err;
Rob Herring32d2ffe2012-12-02 21:00:26 -06001380 int i = 1;
1381 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001382
1383 /*
1384 * Create a menu and add items for all the labels.
1385 */
Pali Rohárfc9d64f2013-03-23 14:50:40 +00001386 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1387 NULL, NULL);
Jason Hobbs06283a62011-08-31 10:37:30 -05001388
1389 if (!m)
1390 return NULL;
1391
1392 list_for_each(pos, &cfg->labels) {
1393 label = list_entry(pos, struct pxe_label, list);
1394
Rob Herring32d2ffe2012-12-02 21:00:26 -06001395 sprintf(label->num, "%d", i++);
1396 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001397 menu_destroy(m);
1398 return NULL;
1399 }
Rob Herring32d2ffe2012-12-02 21:00:26 -06001400 if (cfg->default_label &&
Rob Herring8577fec2012-12-02 21:00:27 -06001401 (strcmp(label->name, cfg->default_label) == 0))
Rob Herring32d2ffe2012-12-02 21:00:26 -06001402 default_num = label->num;
1403
Jason Hobbs06283a62011-08-31 10:37:30 -05001404 }
1405
1406 /*
1407 * After we've created items for each label in the menu, set the
1408 * menu's default label if one was specified.
1409 */
Rob Herring32d2ffe2012-12-02 21:00:26 -06001410 if (default_num) {
1411 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001412 if (err != 1) {
1413 if (err != -ENOENT) {
1414 menu_destroy(m);
1415 return NULL;
1416 }
1417
1418 printf("Missing default: %s\n", cfg->default_label);
1419 }
1420 }
1421
1422 return m;
1423}
1424
1425/*
1426 * Try to boot any labels we have yet to attempt to boot.
1427 */
Tom Rinid7884e02013-09-24 09:05:08 -04001428static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001429{
1430 struct list_head *pos;
1431 struct pxe_label *label;
1432
1433 list_for_each(pos, &cfg->labels) {
1434 label = list_entry(pos, struct pxe_label, list);
1435
1436 if (!label->attempted)
Tom Rinid7884e02013-09-24 09:05:08 -04001437 label_boot(cmdtp, label);
Jason Hobbs06283a62011-08-31 10:37:30 -05001438 }
1439}
1440
1441/*
1442 * Boot the system as prescribed by a pxe_menu.
1443 *
1444 * Use the menu system to either get the user's choice or the default, based
1445 * on config or user input. If there is no default or user's choice,
1446 * attempted to boot labels in the order they were given in pxe files.
1447 * If the default or user's choice fails to boot, attempt to boot other
1448 * labels in the order they were given in pxe files.
1449 *
1450 * If this function returns, there weren't any labels that successfully
1451 * booted, or the user interrupted the menu selection via ctrl+c.
1452 */
Tom Rinid7884e02013-09-24 09:05:08 -04001453static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001454{
1455 void *choice;
1456 struct menu *m;
1457 int err;
1458
1459 m = pxe_menu_to_menu(cfg);
1460 if (!m)
1461 return;
1462
1463 err = menu_get_choice(m, &choice);
1464
1465 menu_destroy(m);
1466
Jason Hobbs6f40f272011-11-07 03:07:15 +00001467 /*
1468 * err == 1 means we got a choice back from menu_get_choice.
1469 *
1470 * err == -ENOENT if the menu was setup to select the default but no
1471 * default was set. in that case, we should continue trying to boot
1472 * labels that haven't been attempted yet.
1473 *
1474 * otherwise, the user interrupted or there was some other error and
1475 * we give up.
1476 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001477
Rob Herring500f3042012-12-02 21:00:22 -06001478 if (err == 1) {
Tom Rinid7884e02013-09-24 09:05:08 -04001479 err = label_boot(cmdtp, choice);
Rob Herring500f3042012-12-02 21:00:22 -06001480 if (!err)
1481 return;
1482 } else if (err != -ENOENT) {
Jason Hobbs6f40f272011-11-07 03:07:15 +00001483 return;
Rob Herring500f3042012-12-02 21:00:22 -06001484 }
Jason Hobbs06283a62011-08-31 10:37:30 -05001485
Tom Rinid7884e02013-09-24 09:05:08 -04001486 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001487}
1488
1489/*
1490 * Boots a system using a pxe file
1491 *
1492 * Returns 0 on success, 1 on error.
1493 */
1494static int
1495do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1496{
1497 unsigned long pxefile_addr_r;
1498 struct pxe_menu *cfg;
1499 char *pxefile_addr_str;
1500
Rob Herring669df7e2012-05-25 10:47:39 +00001501 do_getfile = do_get_tftp;
1502
Jason Hobbs06283a62011-08-31 10:37:30 -05001503 if (argc == 1) {
1504 pxefile_addr_str = from_env("pxefile_addr_r");
1505 if (!pxefile_addr_str)
1506 return 1;
1507
1508 } else if (argc == 2) {
1509 pxefile_addr_str = argv[1];
1510 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001511 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001512 }
1513
1514 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1515 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1516 return 1;
1517 }
1518
Steven Falco0e3f3f82013-10-07 09:51:48 -04001519 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs06283a62011-08-31 10:37:30 -05001520
1521 if (cfg == NULL) {
1522 printf("Error parsing config file\n");
1523 return 1;
1524 }
1525
Tom Rinid7884e02013-09-24 09:05:08 -04001526 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001527
1528 destroy_pxe_menu(cfg);
1529
1530 return 0;
1531}
1532
1533static cmd_tbl_t cmd_pxe_sub[] = {
1534 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1535 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1536};
1537
1538int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1539{
1540 cmd_tbl_t *cp;
1541
1542 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001543 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001544
Rob Herringe5a9a402013-10-18 13:04:42 -05001545 is_pxe = true;
1546
Jason Hobbs06283a62011-08-31 10:37:30 -05001547 /* drop initial "pxe" arg */
1548 argc--;
1549 argv++;
1550
1551 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1552
1553 if (cp)
1554 return cp->cmd(cmdtp, flag, argc, argv);
1555
Simon Glass4c12eeb2011-12-10 08:44:01 +00001556 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001557}
1558
1559U_BOOT_CMD(
1560 pxe, 3, 1, do_pxe,
1561 "commands to get and boot from pxe files",
1562 "get - try to retrieve a pxe file using tftp\npxe "
1563 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1564);
Rob Herring669df7e2012-05-25 10:47:39 +00001565
1566/*
1567 * Boots a system using a local disk syslinux/extlinux file
1568 *
1569 * Returns 0 on success, 1 on error.
1570 */
1571int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1572{
1573 unsigned long pxefile_addr_r;
1574 struct pxe_menu *cfg;
1575 char *pxefile_addr_str;
1576 char *filename;
1577 int prompt = 0;
1578
Rob Herringe5a9a402013-10-18 13:04:42 -05001579 is_pxe = false;
1580
Rob Herring669df7e2012-05-25 10:47:39 +00001581 if (strstr(argv[1], "-p")) {
1582 prompt = 1;
1583 argc--;
1584 argv++;
1585 }
1586
1587 if (argc < 4)
1588 return cmd_usage(cmdtp);
1589
1590 if (argc < 5) {
1591 pxefile_addr_str = from_env("pxefile_addr_r");
1592 if (!pxefile_addr_str)
1593 return 1;
1594 } else {
1595 pxefile_addr_str = argv[4];
1596 }
1597
1598 if (argc < 6)
1599 filename = getenv("bootfile");
1600 else {
1601 filename = argv[5];
1602 setenv("bootfile", filename);
1603 }
1604
1605 if (strstr(argv[3], "ext2"))
1606 do_getfile = do_get_ext2;
1607 else if (strstr(argv[3], "fat"))
1608 do_getfile = do_get_fat;
1609 else {
1610 printf("Invalid filesystem: %s\n", argv[3]);
1611 return 1;
1612 }
1613 fs_argv[1] = argv[1];
1614 fs_argv[2] = argv[2];
1615
1616 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1617 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1618 return 1;
1619 }
1620
Steven Falco0e3f3f82013-10-07 09:51:48 -04001621 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herring669df7e2012-05-25 10:47:39 +00001622 printf("Error reading config file\n");
1623 return 1;
1624 }
1625
Steven Falco0e3f3f82013-10-07 09:51:48 -04001626 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herring669df7e2012-05-25 10:47:39 +00001627
1628 if (cfg == NULL) {
1629 printf("Error parsing config file\n");
1630 return 1;
1631 }
1632
1633 if (prompt)
1634 cfg->prompt = 1;
1635
Tom Rinid7884e02013-09-24 09:05:08 -04001636 handle_pxe_menu(cmdtp, cfg);
Rob Herring669df7e2012-05-25 10:47:39 +00001637
1638 destroy_pxe_menu(cfg);
1639
1640 return 0;
1641}
1642
1643U_BOOT_CMD(
1644 sysboot, 7, 1, do_sysboot,
1645 "command to get and boot from syslinux files",
1646 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1647 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1648 " filesystem on 'dev' on 'interface' to address 'addr'"
1649);