blob: a314275b103f5fb57829730ae85ae56881d1be3a [file] [log] [blame]
Jason Hobbs06283a62011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#include <common.h>
18#include <command.h>
19#include <malloc.h>
20#include <linux/string.h>
21#include <linux/ctype.h>
22#include <errno.h>
23#include <linux/list.h>
24
25#include "menu.h"
26
27#define MAX_TFTP_PATH_LEN 127
28
Rob Herringc34a9422012-09-12 16:35:23 -050029const char *pxe_default_paths[] = {
30 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
31 "default-" CONFIG_SYS_ARCH,
32 "default",
33 NULL
34};
35
Jason Hobbs06283a62011-08-31 10:37:30 -050036/*
37 * Like getenv, but prints an error if envvar isn't defined in the
38 * environment. It always returns what getenv does, so it can be used in
39 * place of getenv without changing error handling otherwise.
40 */
Rob Herring31770a32012-09-12 16:17:57 -050041static char *from_env(const char *envvar)
Jason Hobbs06283a62011-08-31 10:37:30 -050042{
43 char *ret;
44
45 ret = getenv(envvar);
46
47 if (!ret)
48 printf("missing environment variable: %s\n", envvar);
49
50 return ret;
51}
52
53/*
54 * Convert an ethaddr from the environment to the format used by pxelinux
55 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
56 * the beginning of the ethernet address to indicate a hardware type of
57 * Ethernet. Also converts uppercase hex characters into lowercase, to match
58 * pxelinux's behavior.
59 *
60 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
61 * environment, or some other value < 0 on error.
62 */
63static int format_mac_pxe(char *outbuf, size_t outbuf_len)
64{
65 size_t ethaddr_len;
66 char *p, *ethaddr;
67
68 ethaddr = from_env("ethaddr");
69
70 if (!ethaddr)
John Rigby5943fe42011-07-13 23:05:19 -060071 ethaddr = from_env("usbethaddr");
72
73 if (!ethaddr)
Jason Hobbs06283a62011-08-31 10:37:30 -050074 return -ENOENT;
75
76 ethaddr_len = strlen(ethaddr);
77
78 /*
79 * ethaddr_len + 4 gives room for "01-", ethaddr, and a NUL byte at
80 * the end.
81 */
82 if (outbuf_len < ethaddr_len + 4) {
83 printf("outbuf is too small (%d < %d)\n",
84 outbuf_len, ethaddr_len + 4);
85
86 return -EINVAL;
87 }
88
89 strcpy(outbuf, "01-");
90
91 for (p = outbuf + 3; *ethaddr; ethaddr++, p++) {
92 if (*ethaddr == ':')
93 *p = '-';
94 else
95 *p = tolower(*ethaddr);
96 }
97
98 *p = '\0';
99
100 return 1;
101}
102
103/*
104 * Returns the directory the file specified in the bootfile env variable is
105 * in. If bootfile isn't defined in the environment, return NULL, which should
106 * be interpreted as "don't prepend anything to paths".
107 */
Rob Herring90ba7d72012-03-28 05:51:36 +0000108static int get_bootfile_path(const char *file_path, char *bootfile_path,
109 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -0500110{
111 char *bootfile, *last_slash;
Rob Herring90ba7d72012-03-28 05:51:36 +0000112 size_t path_len = 0;
113
114 if (file_path[0] == '/')
115 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500116
117 bootfile = from_env("bootfile");
118
Rob Herring90ba7d72012-03-28 05:51:36 +0000119 if (!bootfile)
120 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500121
122 last_slash = strrchr(bootfile, '/');
123
Rob Herring90ba7d72012-03-28 05:51:36 +0000124 if (last_slash == NULL)
125 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500126
127 path_len = (last_slash - bootfile) + 1;
128
129 if (bootfile_path_size < path_len) {
130 printf("bootfile_path too small. (%d < %d)\n",
131 bootfile_path_size, path_len);
132
133 return -1;
134 }
135
136 strncpy(bootfile_path, bootfile, path_len);
137
Rob Herring90ba7d72012-03-28 05:51:36 +0000138 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500139 bootfile_path[path_len] = '\0';
140
141 return 1;
142}
143
Rob Herring31770a32012-09-12 16:17:57 -0500144static int (*do_getfile)(const char *file_path, char *file_addr);
Rob Herring669df7e2012-05-25 10:47:39 +0000145
Rob Herring31770a32012-09-12 16:17:57 -0500146static int do_get_tftp(const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000147{
148 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
149
150 tftp_argv[1] = file_addr;
Rob Herring31770a32012-09-12 16:17:57 -0500151 tftp_argv[2] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000152
153 if (do_tftpb(NULL, 0, 3, tftp_argv))
154 return -ENOENT;
155
156 return 1;
157}
158
159static char *fs_argv[5];
160
Rob Herring31770a32012-09-12 16:17:57 -0500161static int do_get_ext2(const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000162{
163#ifdef CONFIG_CMD_EXT2
164 fs_argv[0] = "ext2load";
165 fs_argv[3] = file_addr;
Rob Herring31770a32012-09-12 16:17:57 -0500166 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000167
168 if (!do_ext2load(NULL, 0, 5, fs_argv))
169 return 1;
170#endif
171 return -ENOENT;
172}
173
Rob Herring31770a32012-09-12 16:17:57 -0500174static int do_get_fat(const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000175{
176#ifdef CONFIG_CMD_FAT
177 fs_argv[0] = "fatload";
178 fs_argv[3] = file_addr;
Rob Herring31770a32012-09-12 16:17:57 -0500179 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000180
181 if (!do_fat_fsload(NULL, 0, 5, fs_argv))
182 return 1;
183#endif
184 return -ENOENT;
185}
186
Jason Hobbs06283a62011-08-31 10:37:30 -0500187/*
188 * As in pxelinux, paths to files referenced from files we retrieve are
189 * relative to the location of bootfile. get_relfile takes such a path and
190 * joins it with the bootfile path to get the full path to the target file. If
191 * the bootfile path is NULL, we use file_path as is.
192 *
193 * Returns 1 for success, or < 0 on error.
194 */
Rob Herring31770a32012-09-12 16:17:57 -0500195static int get_relfile(const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500196{
197 size_t path_len;
198 char relfile[MAX_TFTP_PATH_LEN+1];
199 char addr_buf[10];
Jason Hobbs06283a62011-08-31 10:37:30 -0500200 int err;
201
Rob Herring90ba7d72012-03-28 05:51:36 +0000202 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-08-31 10:37:30 -0500203
204 if (err < 0)
205 return err;
206
207 path_len = strlen(file_path);
208 path_len += strlen(relfile);
209
210 if (path_len > MAX_TFTP_PATH_LEN) {
211 printf("Base path too long (%s%s)\n",
212 relfile,
213 file_path);
214
215 return -ENAMETOOLONG;
216 }
217
218 strcat(relfile, file_path);
219
220 printf("Retrieving file: %s\n", relfile);
221
222 sprintf(addr_buf, "%p", file_addr);
223
Rob Herring669df7e2012-05-25 10:47:39 +0000224 return do_getfile(relfile, addr_buf);
Jason Hobbs06283a62011-08-31 10:37:30 -0500225}
226
227/*
228 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
229 * 'bootfile' was specified in the environment, the path to bootfile will be
230 * prepended to 'file_path' and the resulting path will be used.
231 *
232 * Returns 1 on success, or < 0 for error.
233 */
Rob Herring31770a32012-09-12 16:17:57 -0500234static int get_pxe_file(const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500235{
236 unsigned long config_file_size;
237 char *tftp_filesize;
238 int err;
239
240 err = get_relfile(file_path, file_addr);
241
242 if (err < 0)
243 return err;
244
245 /*
246 * the file comes without a NUL byte at the end, so find out its size
247 * and add the NUL byte.
248 */
249 tftp_filesize = from_env("filesize");
250
251 if (!tftp_filesize)
252 return -ENOENT;
253
254 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
255 return -EINVAL;
256
257 *(char *)(file_addr + config_file_size) = '\0';
258
259 return 1;
260}
261
262#define PXELINUX_DIR "pxelinux.cfg/"
263
264/*
265 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
266 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
267 * from the bootfile path, as described above.
268 *
269 * Returns 1 on success or < 0 on error.
270 */
Rob Herring31770a32012-09-12 16:17:57 -0500271static int get_pxelinux_path(const char *file, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500272{
273 size_t base_len = strlen(PXELINUX_DIR);
274 char path[MAX_TFTP_PATH_LEN+1];
275
276 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
277 printf("path (%s%s) too long, skipping\n",
278 PXELINUX_DIR, file);
279 return -ENAMETOOLONG;
280 }
281
282 sprintf(path, PXELINUX_DIR "%s", file);
283
284 return get_pxe_file(path, pxefile_addr_r);
285}
286
287/*
288 * Looks for a pxe file with a name based on the pxeuuid environment variable.
289 *
290 * Returns 1 on success or < 0 on error.
291 */
292static int pxe_uuid_path(void *pxefile_addr_r)
293{
294 char *uuid_str;
295
296 uuid_str = from_env("pxeuuid");
297
298 if (!uuid_str)
299 return -ENOENT;
300
301 return get_pxelinux_path(uuid_str, pxefile_addr_r);
302}
303
304/*
305 * Looks for a pxe file with a name based on the 'ethaddr' environment
306 * variable.
307 *
308 * Returns 1 on success or < 0 on error.
309 */
310static int pxe_mac_path(void *pxefile_addr_r)
311{
312 char mac_str[21];
313 int err;
314
315 err = format_mac_pxe(mac_str, sizeof(mac_str));
316
317 if (err < 0)
318 return err;
319
320 return get_pxelinux_path(mac_str, pxefile_addr_r);
321}
322
323/*
324 * Looks for pxe files with names based on our IP address. See pxelinux
325 * documentation for details on what these file names look like. We match
326 * that exactly.
327 *
328 * Returns 1 on success or < 0 on error.
329 */
330static int pxe_ipaddr_paths(void *pxefile_addr_r)
331{
332 char ip_addr[9];
333 int mask_pos, err;
334
335 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
336
337 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
338 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
339
340 if (err > 0)
341 return err;
342
343 ip_addr[mask_pos] = '\0';
344 }
345
346 return -ENOENT;
347}
348
349/*
350 * Entry point for the 'pxe get' command.
351 * This Follows pxelinux's rules to download a config file from a tftp server.
352 * The file is stored at the location given by the pxefile_addr_r environment
353 * variable, which must be set.
354 *
355 * UUID comes from pxeuuid env variable, if defined
356 * MAC addr comes from ethaddr env variable, if defined
357 * IP
358 *
359 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
360 *
361 * Returns 0 on success or 1 on error.
362 */
363static int
364do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
365{
366 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000367 unsigned long pxefile_addr_r;
Rob Herringc34a9422012-09-12 16:35:23 -0500368 int err, i = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500369
Rob Herring669df7e2012-05-25 10:47:39 +0000370 do_getfile = do_get_tftp;
371
Jason Hobbs06283a62011-08-31 10:37:30 -0500372 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000373 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500374
Jason Hobbs06283a62011-08-31 10:37:30 -0500375 pxefile_addr_str = from_env("pxefile_addr_r");
376
377 if (!pxefile_addr_str)
378 return 1;
379
380 err = strict_strtoul(pxefile_addr_str, 16,
381 (unsigned long *)&pxefile_addr_r);
382 if (err < 0)
383 return 1;
384
385 /*
386 * Keep trying paths until we successfully get a file we're looking
387 * for.
388 */
Jason Hobbs834c9382012-03-05 08:12:28 +0000389 if (pxe_uuid_path((void *)pxefile_addr_r) > 0
390 || pxe_mac_path((void *)pxefile_addr_r) > 0
Rob Herringc34a9422012-09-12 16:35:23 -0500391 || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500392
393 printf("Config file found\n");
394
395 return 0;
396 }
397
Rob Herringc34a9422012-09-12 16:35:23 -0500398 while (pxe_default_paths[i]) {
399 if (get_pxelinux_path(pxe_default_paths[i], (void *)pxefile_addr_r) > 0) {
400 printf("Config file found\n");
401 return 0;
402 }
403 i++;
404 }
405
Jason Hobbs06283a62011-08-31 10:37:30 -0500406 printf("Config file not found\n");
407
408 return 1;
409}
410
411/*
412 * Wrapper to make it easier to store the file at file_path in the location
413 * specified by envaddr_name. file_path will be joined to the bootfile path,
414 * if any is specified.
415 *
416 * Returns 1 on success or < 0 on error.
417 */
Rob Herring31770a32012-09-12 16:17:57 -0500418static int get_relfile_envaddr(const char *file_path, const char *envaddr_name)
Jason Hobbs06283a62011-08-31 10:37:30 -0500419{
Jason Hobbs834c9382012-03-05 08:12:28 +0000420 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500421 char *envaddr;
422
423 envaddr = from_env(envaddr_name);
424
425 if (!envaddr)
426 return -ENOENT;
427
Jason Hobbs834c9382012-03-05 08:12:28 +0000428 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500429 return -EINVAL;
430
Jason Hobbs834c9382012-03-05 08:12:28 +0000431 return get_relfile(file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500432}
433
434/*
435 * A note on the pxe file parser.
436 *
437 * We're parsing files that use syslinux grammar, which has a few quirks.
438 * String literals must be recognized based on context - there is no
439 * quoting or escaping support. There's also nothing to explicitly indicate
440 * when a label section completes. We deal with that by ending a label
441 * section whenever we see a line that doesn't include.
442 *
443 * As with the syslinux family, this same file format could be reused in the
444 * future for non pxe purposes. The only action it takes during parsing that
445 * would throw this off is handling of include files. It assumes we're using
446 * pxe, and does a tftp download of a file listed as an include file in the
447 * middle of the parsing operation. That could be handled by refactoring it to
448 * take a 'include file getter' function.
449 */
450
451/*
452 * Describes a single label given in a pxe file.
453 *
454 * Create these with the 'label_create' function given below.
455 *
456 * name - the name of the menu as given on the 'menu label' line.
457 * kernel - the path to the kernel file to use for this label.
458 * append - kernel command line to use when booting this label
459 * initrd - path to the initrd to use for this label.
460 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
461 * localboot - 1 if this label specified 'localboot', 0 otherwise.
462 * list - lets these form a list, which a pxe_menu struct will hold.
463 */
464struct pxe_label {
Rob Herring4b3efae2012-04-14 22:23:21 -0500465 char num[4];
Jason Hobbs06283a62011-08-31 10:37:30 -0500466 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000467 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500468 char *kernel;
469 char *append;
470 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000471 char *fdt;
Jason Hobbs06283a62011-08-31 10:37:30 -0500472 int attempted;
473 int localboot;
Rob Herringc27d5602012-04-12 13:55:20 -0500474 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500475 struct list_head list;
476};
477
478/*
479 * Describes a pxe menu as given via pxe files.
480 *
481 * title - the name of the menu as given by a 'menu title' line.
482 * default_label - the name of the default label, if any.
483 * timeout - time in tenths of a second to wait for a user key-press before
484 * booting the default label.
485 * prompt - if 0, don't prompt for a choice unless the timeout period is
486 * interrupted. If 1, always prompt for a choice regardless of
487 * timeout.
488 * labels - a list of labels defined for the menu.
489 */
490struct pxe_menu {
491 char *title;
492 char *default_label;
493 int timeout;
494 int prompt;
495 struct list_head labels;
496};
497
498/*
499 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
500 * result must be free()'d to reclaim the memory.
501 *
502 * Returns NULL if malloc fails.
503 */
504static struct pxe_label *label_create(void)
505{
506 struct pxe_label *label;
507
508 label = malloc(sizeof(struct pxe_label));
509
510 if (!label)
511 return NULL;
512
513 memset(label, 0, sizeof(struct pxe_label));
514
515 return label;
516}
517
518/*
519 * Free the memory used by a pxe_label, including that used by its name,
520 * kernel, append and initrd members, if they're non NULL.
521 *
522 * So - be sure to only use dynamically allocated memory for the members of
523 * the pxe_label struct, unless you want to clean it up first. These are
524 * currently only created by the pxe file parsing code.
525 */
526static void label_destroy(struct pxe_label *label)
527{
528 if (label->name)
529 free(label->name);
530
531 if (label->kernel)
532 free(label->kernel);
533
534 if (label->append)
535 free(label->append);
536
537 if (label->initrd)
538 free(label->initrd);
539
Chander Kashyapa6559382012-09-06 19:36:31 +0000540 if (label->fdt)
541 free(label->fdt);
542
Jason Hobbs06283a62011-08-31 10:37:30 -0500543 free(label);
544}
545
546/*
547 * Print a label and its string members if they're defined.
548 *
549 * This is passed as a callback to the menu code for displaying each
550 * menu entry.
551 */
552static void label_print(void *data)
553{
554 struct pxe_label *label = data;
Rob Herring4b3efae2012-04-14 22:23:21 -0500555 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500556
Rob Herring4b3efae2012-04-14 22:23:21 -0500557 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500558}
559
560/*
561 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
562 * environment variable is defined. Its contents will be executed as U-boot
563 * command. If the label specified an 'append' line, its contents will be
564 * used to overwrite the contents of the 'bootargs' environment variable prior
565 * to running 'localcmd'.
566 *
567 * Returns 1 on success or < 0 on error.
568 */
569static int label_localboot(struct pxe_label *label)
570{
Simon Glassd51004a2012-03-30 21:30:55 +0000571 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500572
573 localcmd = from_env("localcmd");
574
575 if (!localcmd)
576 return -ENOENT;
577
Jason Hobbs06283a62011-08-31 10:37:30 -0500578 if (label->append)
579 setenv("bootargs", label->append);
580
Simon Glassd51004a2012-03-30 21:30:55 +0000581 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500582
Simon Glassd51004a2012-03-30 21:30:55 +0000583 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500584}
585
586/*
587 * Boot according to the contents of a pxe_label.
588 *
589 * If we can't boot for any reason, we return. A successful boot never
590 * returns.
591 *
592 * The kernel will be stored in the location given by the 'kernel_addr_r'
593 * environment variable.
594 *
595 * If the label specifies an initrd file, it will be stored in the location
596 * given by the 'ramdisk_addr_r' environment variable.
597 *
598 * If the label specifies an 'append' line, its contents will overwrite that
599 * of the 'bootargs' environment variable.
600 */
Rob Herringc27d5602012-04-12 13:55:20 -0500601static int label_boot(struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500602{
603 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herring5af27692012-03-26 13:16:59 -0500604 char initrd_str[22];
Jason Hobbs06283a62011-08-31 10:37:30 -0500605 int bootm_argc = 3;
606
607 label_print(label);
608
609 label->attempted = 1;
610
611 if (label->localboot) {
Rob Herringc27d5602012-04-12 13:55:20 -0500612 if (label->localboot_val >= 0)
613 label_localboot(label);
614 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500615 }
616
617 if (label->kernel == NULL) {
618 printf("No kernel given, skipping %s\n",
619 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500620 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500621 }
622
623 if (label->initrd) {
624 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
625 printf("Skipping %s for failure retrieving initrd\n",
626 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500627 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500628 }
629
Rob Herring5af27692012-03-26 13:16:59 -0500630 bootm_argv[2] = initrd_str;
631 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
632 strcat(bootm_argv[2], ":");
633 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500634 } else {
635 bootm_argv[2] = "-";
636 }
637
638 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
639 printf("Skipping %s for failure retrieving kernel\n",
640 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500641 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500642 }
643
Rob Herring4b3efae2012-04-14 22:23:21 -0500644 if (label->append) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500645 setenv("bootargs", label->append);
Rob Herring4b3efae2012-04-14 22:23:21 -0500646 printf("append: %s\n", label->append);
647 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500648
649 bootm_argv[1] = getenv("kernel_addr_r");
650
651 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000652 * fdt usage is optional:
653 * It handles the following scenarios. All scenarios are exclusive
654 *
655 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
656 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
657 * and adjust argc appropriately.
658 *
659 * Scenario 2: If there is an fdt_addr specified, pass it along to
660 * bootm, and adjust argc appropriately.
661 *
662 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500663 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000664 bootm_argv[3] = getenv("fdt_addr_r");
665
666 /* if fdt label is defined then get fdt from server */
667 if (bootm_argv[3] && label->fdt) {
668 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
669 printf("Skipping %s for failure retrieving fdt\n",
670 label->name);
671 return;
672 }
673 } else
674 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500675
676 if (bootm_argv[3])
677 bootm_argc = 4;
678
Rob Herring5af27692012-03-26 13:16:59 -0500679#ifdef CONFIG_CMD_BOOTZ
680 do_bootz(NULL, 0, bootm_argc, bootm_argv);
681#else
Jason Hobbs06283a62011-08-31 10:37:30 -0500682 do_bootm(NULL, 0, bootm_argc, bootm_argv);
Rob Herring5af27692012-03-26 13:16:59 -0500683#endif
Rob Herringc27d5602012-04-12 13:55:20 -0500684 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500685}
686
687/*
688 * Tokens for the pxe file parser.
689 */
690enum token_type {
691 T_EOL,
692 T_STRING,
693 T_EOF,
694 T_MENU,
695 T_TITLE,
696 T_TIMEOUT,
697 T_LABEL,
698 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000699 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500700 T_APPEND,
701 T_INITRD,
702 T_LOCALBOOT,
703 T_DEFAULT,
704 T_PROMPT,
705 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000706 T_FDT,
Rob Herringdf6be7e2012-05-02 18:57:48 -0500707 T_ONTIMEOUT,
Jason Hobbs06283a62011-08-31 10:37:30 -0500708 T_INVALID
709};
710
711/*
712 * A token - given by a value and a type.
713 */
714struct token {
715 char *val;
716 enum token_type type;
717};
718
719/*
720 * Keywords recognized.
721 */
722static const struct token keywords[] = {
723 {"menu", T_MENU},
724 {"title", T_TITLE},
725 {"timeout", T_TIMEOUT},
726 {"default", T_DEFAULT},
727 {"prompt", T_PROMPT},
728 {"label", T_LABEL},
729 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000730 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500731 {"localboot", T_LOCALBOOT},
732 {"append", T_APPEND},
733 {"initrd", T_INITRD},
734 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000735 {"fdt", T_FDT},
Rob Herringdf6be7e2012-05-02 18:57:48 -0500736 {"ontimeout", T_ONTIMEOUT,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500737 {NULL, T_INVALID}
738};
739
740/*
741 * Since pxe(linux) files don't have a token to identify the start of a
742 * literal, we have to keep track of when we're in a state where a literal is
743 * expected vs when we're in a state a keyword is expected.
744 */
745enum lex_state {
746 L_NORMAL = 0,
747 L_KEYWORD,
748 L_SLITERAL
749};
750
751/*
752 * get_string retrieves a string from *p and stores it as a token in
753 * *t.
754 *
755 * get_string used for scanning both string literals and keywords.
756 *
757 * Characters from *p are copied into t-val until a character equal to
758 * delim is found, or a NUL byte is reached. If delim has the special value of
759 * ' ', any whitespace character will be used as a delimiter.
760 *
761 * If lower is unequal to 0, uppercase characters will be converted to
762 * lowercase in the result. This is useful to make keywords case
763 * insensitive.
764 *
765 * The location of *p is updated to point to the first character after the end
766 * of the token - the ending delimiter.
767 *
768 * On success, the new value of t->val is returned. Memory for t->val is
769 * allocated using malloc and must be free()'d to reclaim it. If insufficient
770 * memory is available, NULL is returned.
771 */
772static char *get_string(char **p, struct token *t, char delim, int lower)
773{
774 char *b, *e;
775 size_t len, i;
776
777 /*
778 * b and e both start at the beginning of the input stream.
779 *
780 * e is incremented until we find the ending delimiter, or a NUL byte
781 * is reached. Then, we take e - b to find the length of the token.
782 */
783 b = e = *p;
784
785 while (*e) {
786 if ((delim == ' ' && isspace(*e)) || delim == *e)
787 break;
788 e++;
789 }
790
791 len = e - b;
792
793 /*
794 * Allocate memory to hold the string, and copy it in, converting
795 * characters to lowercase if lower is != 0.
796 */
797 t->val = malloc(len + 1);
798 if (!t->val)
799 return NULL;
800
801 for (i = 0; i < len; i++, b++) {
802 if (lower)
803 t->val[i] = tolower(*b);
804 else
805 t->val[i] = *b;
806 }
807
808 t->val[len] = '\0';
809
810 /*
811 * Update *p so the caller knows where to continue scanning.
812 */
813 *p = e;
814
815 t->type = T_STRING;
816
817 return t->val;
818}
819
820/*
821 * Populate a keyword token with a type and value.
822 */
823static void get_keyword(struct token *t)
824{
825 int i;
826
827 for (i = 0; keywords[i].val; i++) {
828 if (!strcmp(t->val, keywords[i].val)) {
829 t->type = keywords[i].type;
830 break;
831 }
832 }
833}
834
835/*
836 * Get the next token. We have to keep track of which state we're in to know
837 * if we're looking to get a string literal or a keyword.
838 *
839 * *p is updated to point at the first character after the current token.
840 */
841static void get_token(char **p, struct token *t, enum lex_state state)
842{
843 char *c = *p;
844
845 t->type = T_INVALID;
846
847 /* eat non EOL whitespace */
848 while (isblank(*c))
849 c++;
850
851 /*
852 * eat comments. note that string literals can't begin with #, but
853 * can contain a # after their first character.
854 */
855 if (*c == '#') {
856 while (*c && *c != '\n')
857 c++;
858 }
859
860 if (*c == '\n') {
861 t->type = T_EOL;
862 c++;
863 } else if (*c == '\0') {
864 t->type = T_EOF;
865 c++;
866 } else if (state == L_SLITERAL) {
867 get_string(&c, t, '\n', 0);
868 } else if (state == L_KEYWORD) {
869 /*
870 * when we expect a keyword, we first get the next string
871 * token delimited by whitespace, and then check if it
872 * matches a keyword in our keyword list. if it does, it's
873 * converted to a keyword token of the appropriate type, and
874 * if not, it remains a string token.
875 */
876 get_string(&c, t, ' ', 1);
877 get_keyword(t);
878 }
879
880 *p = c;
881}
882
883/*
884 * Increment *c until we get to the end of the current line, or EOF.
885 */
886static void eol_or_eof(char **c)
887{
888 while (**c && **c != '\n')
889 (*c)++;
890}
891
892/*
893 * All of these parse_* functions share some common behavior.
894 *
895 * They finish with *c pointing after the token they parse, and return 1 on
896 * success, or < 0 on error.
897 */
898
899/*
900 * Parse a string literal and store a pointer it at *dst. String literals
901 * terminate at the end of the line.
902 */
903static int parse_sliteral(char **c, char **dst)
904{
905 struct token t;
906 char *s = *c;
907
908 get_token(c, &t, L_SLITERAL);
909
910 if (t.type != T_STRING) {
911 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
912 return -EINVAL;
913 }
914
915 *dst = t.val;
916
917 return 1;
918}
919
920/*
921 * Parse a base 10 (unsigned) integer and store it at *dst.
922 */
923static int parse_integer(char **c, int *dst)
924{
925 struct token t;
926 char *s = *c;
927 unsigned long temp;
928
929 get_token(c, &t, L_SLITERAL);
930
931 if (t.type != T_STRING) {
932 printf("Expected string: %.*s\n", (int)(*c - s), s);
933 return -EINVAL;
934 }
935
Rob Herringc27d5602012-04-12 13:55:20 -0500936 *dst = simple_strtol(t.val, &temp, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -0500937
938 free(t.val);
939
940 return 1;
941}
942
943static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
944
945/*
946 * Parse an include statement, and retrieve and parse the file it mentions.
947 *
948 * base should point to a location where it's safe to store the file, and
949 * nest_level should indicate how many nested includes have occurred. For this
950 * include, nest_level has already been incremented and doesn't need to be
951 * incremented here.
952 */
953static int handle_include(char **c, char *base,
954 struct pxe_menu *cfg, int nest_level)
955{
956 char *include_path;
957 char *s = *c;
958 int err;
959
960 err = parse_sliteral(c, &include_path);
961
962 if (err < 0) {
963 printf("Expected include path: %.*s\n",
964 (int)(*c - s), s);
965 return err;
966 }
967
968 err = get_pxe_file(include_path, base);
969
970 if (err < 0) {
971 printf("Couldn't retrieve %s\n", include_path);
972 return err;
973 }
974
975 return parse_pxefile_top(base, cfg, nest_level);
976}
977
978/*
979 * Parse lines that begin with 'menu'.
980 *
981 * b and nest are provided to handle the 'menu include' case.
982 *
983 * b should be the address where the file currently being parsed is stored.
984 *
985 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
986 * a file it includes, 3 when parsing a file included by that file, and so on.
987 */
988static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
989{
990 struct token t;
991 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000992 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500993
994 get_token(c, &t, L_KEYWORD);
995
996 switch (t.type) {
997 case T_TITLE:
998 err = parse_sliteral(c, &cfg->title);
999
1000 break;
1001
1002 case T_INCLUDE:
1003 err = handle_include(c, b + strlen(b) + 1, cfg,
1004 nest_level + 1);
1005 break;
1006
1007 default:
1008 printf("Ignoring malformed menu command: %.*s\n",
1009 (int)(*c - s), s);
1010 }
1011
1012 if (err < 0)
1013 return err;
1014
1015 eol_or_eof(c);
1016
1017 return 1;
1018}
1019
1020/*
1021 * Handles parsing a 'menu line' when we're parsing a label.
1022 */
1023static int parse_label_menu(char **c, struct pxe_menu *cfg,
1024 struct pxe_label *label)
1025{
1026 struct token t;
1027 char *s;
1028
1029 s = *c;
1030
1031 get_token(c, &t, L_KEYWORD);
1032
1033 switch (t.type) {
1034 case T_DEFAULT:
Rob Herringdf6be7e2012-05-02 18:57:48 -05001035 if (!cfg->default_label)
1036 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001037
1038 if (!cfg->default_label)
1039 return -ENOMEM;
1040
1041 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001042 case T_LABEL:
1043 parse_sliteral(c, &label->menu);
1044 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001045 default:
1046 printf("Ignoring malformed menu command: %.*s\n",
1047 (int)(*c - s), s);
1048 }
1049
1050 eol_or_eof(c);
1051
1052 return 0;
1053}
1054
1055/*
1056 * Parses a label and adds it to the list of labels for a menu.
1057 *
1058 * A label ends when we either get to the end of a file, or
1059 * get some input we otherwise don't have a handler defined
1060 * for.
1061 *
1062 */
1063static int parse_label(char **c, struct pxe_menu *cfg)
1064{
1065 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001066 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001067 char *s = *c;
1068 struct pxe_label *label;
1069 int err;
Rob Herringc27d5602012-04-12 13:55:20 -05001070 int localboot;
Jason Hobbs06283a62011-08-31 10:37:30 -05001071
1072 label = label_create();
1073 if (!label)
1074 return -ENOMEM;
1075
1076 err = parse_sliteral(c, &label->name);
1077 if (err < 0) {
1078 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1079 label_destroy(label);
1080 return -EINVAL;
1081 }
1082
1083 list_add_tail(&label->list, &cfg->labels);
1084
1085 while (1) {
1086 s = *c;
1087 get_token(c, &t, L_KEYWORD);
1088
1089 err = 0;
1090 switch (t.type) {
1091 case T_MENU:
1092 err = parse_label_menu(c, cfg, label);
1093 break;
1094
1095 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001096 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001097 err = parse_sliteral(c, &label->kernel);
1098 break;
1099
1100 case T_APPEND:
1101 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001102 if (label->initrd)
1103 break;
1104 s = strstr(label->append, "initrd=");
1105 if (!s)
1106 break;
1107 s += 7;
1108 len = (int)(strchr(s, ' ') - s);
1109 label->initrd = malloc(len + 1);
1110 strncpy(label->initrd, s, len);
1111 label->initrd[len] = '\0';
1112
Jason Hobbs06283a62011-08-31 10:37:30 -05001113 break;
1114
1115 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001116 if (!label->initrd)
1117 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001118 break;
1119
Chander Kashyapa6559382012-09-06 19:36:31 +00001120 case T_FDT:
1121 if (!label->fdt)
1122 err = parse_sliteral(c, &label->fdt);
1123 break;
1124
Jason Hobbs06283a62011-08-31 10:37:30 -05001125 case T_LOCALBOOT:
Rob Herringc27d5602012-04-12 13:55:20 -05001126 label->localboot = 1;
1127 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001128 break;
1129
1130 case T_EOL:
1131 break;
1132
1133 default:
1134 /*
1135 * put the token back! we don't want it - it's the end
1136 * of a label and whatever token this is, it's
1137 * something for the menu level context to handle.
1138 */
1139 *c = s;
1140 return 1;
1141 }
1142
1143 if (err < 0)
1144 return err;
1145 }
1146}
1147
1148/*
1149 * This 16 comes from the limit pxelinux imposes on nested includes.
1150 *
1151 * There is no reason at all we couldn't do more, but some limit helps prevent
1152 * infinite (until crash occurs) recursion if a file tries to include itself.
1153 */
1154#define MAX_NEST_LEVEL 16
1155
1156/*
1157 * Entry point for parsing a menu file. nest_level indicates how many times
1158 * we've nested in includes. It will be 1 for the top level menu file.
1159 *
1160 * Returns 1 on success, < 0 on error.
1161 */
1162static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1163{
1164 struct token t;
1165 char *s, *b, *label_name;
1166 int err;
1167
1168 b = p;
1169
1170 if (nest_level > MAX_NEST_LEVEL) {
1171 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1172 return -EMLINK;
1173 }
1174
1175 while (1) {
1176 s = p;
1177
1178 get_token(&p, &t, L_KEYWORD);
1179
1180 err = 0;
1181 switch (t.type) {
1182 case T_MENU:
Rob Herring2619f512012-04-12 13:33:43 -05001183 cfg->prompt = 1;
Jason Hobbs06283a62011-08-31 10:37:30 -05001184 err = parse_menu(&p, cfg, b, nest_level);
1185 break;
1186
1187 case T_TIMEOUT:
1188 err = parse_integer(&p, &cfg->timeout);
1189 break;
1190
1191 case T_LABEL:
1192 err = parse_label(&p, cfg);
1193 break;
1194
1195 case T_DEFAULT:
Rob Herringdf6be7e2012-05-02 18:57:48 -05001196 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001197 err = parse_sliteral(&p, &label_name);
1198
1199 if (label_name) {
1200 if (cfg->default_label)
1201 free(cfg->default_label);
1202
1203 cfg->default_label = label_name;
1204 }
1205
1206 break;
1207
Rob Herring1e085222012-05-25 10:43:16 +00001208 case T_INCLUDE:
1209 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1210 nest_level + 1);
1211 break;
1212
Jason Hobbs06283a62011-08-31 10:37:30 -05001213 case T_PROMPT:
Rob Herring2619f512012-04-12 13:33:43 -05001214 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001215 break;
1216
1217 case T_EOL:
1218 break;
1219
1220 case T_EOF:
1221 return 1;
1222
1223 default:
1224 printf("Ignoring unknown command: %.*s\n",
1225 (int)(p - s), s);
1226 eol_or_eof(&p);
1227 }
1228
1229 if (err < 0)
1230 return err;
1231 }
1232}
1233
1234/*
1235 * Free the memory used by a pxe_menu and its labels.
1236 */
1237static void destroy_pxe_menu(struct pxe_menu *cfg)
1238{
1239 struct list_head *pos, *n;
1240 struct pxe_label *label;
1241
1242 if (cfg->title)
1243 free(cfg->title);
1244
1245 if (cfg->default_label)
1246 free(cfg->default_label);
1247
1248 list_for_each_safe(pos, n, &cfg->labels) {
1249 label = list_entry(pos, struct pxe_label, list);
1250
1251 label_destroy(label);
1252 }
1253
1254 free(cfg);
1255}
1256
1257/*
1258 * Entry point for parsing a pxe file. This is only used for the top level
1259 * file.
1260 *
1261 * Returns NULL if there is an error, otherwise, returns a pointer to a
1262 * pxe_menu struct populated with the results of parsing the pxe file (and any
1263 * files it includes). The resulting pxe_menu struct can be free()'d by using
1264 * the destroy_pxe_menu() function.
1265 */
1266static struct pxe_menu *parse_pxefile(char *menucfg)
1267{
1268 struct pxe_menu *cfg;
1269
1270 cfg = malloc(sizeof(struct pxe_menu));
1271
1272 if (!cfg)
1273 return NULL;
1274
1275 memset(cfg, 0, sizeof(struct pxe_menu));
1276
1277 INIT_LIST_HEAD(&cfg->labels);
1278
1279 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1280 destroy_pxe_menu(cfg);
1281 return NULL;
1282 }
1283
1284 return cfg;
1285}
1286
1287/*
1288 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1289 * menu code.
1290 */
1291static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1292{
1293 struct pxe_label *label;
1294 struct list_head *pos;
1295 struct menu *m;
1296 int err;
Rob Herring4b3efae2012-04-14 22:23:21 -05001297 int i = 1;
Rob Herring0731b7b2012-05-02 18:52:49 -05001298 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001299
1300 /*
1301 * Create a menu and add items for all the labels.
1302 */
1303 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1304
1305 if (!m)
1306 return NULL;
1307
1308 list_for_each(pos, &cfg->labels) {
1309 label = list_entry(pos, struct pxe_label, list);
1310
Rob Herring4b3efae2012-04-14 22:23:21 -05001311 sprintf(label->num, "%d", i++);
1312 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001313 menu_destroy(m);
1314 return NULL;
1315 }
Rob Herring0731b7b2012-05-02 18:52:49 -05001316 if (cfg->default_label &&
1317 (strcmp(label->name, cfg->default_label) == 0))
1318 default_num = label->num;
1319
Jason Hobbs06283a62011-08-31 10:37:30 -05001320 }
1321
1322 /*
1323 * After we've created items for each label in the menu, set the
1324 * menu's default label if one was specified.
1325 */
Rob Herring0731b7b2012-05-02 18:52:49 -05001326 if (default_num) {
1327 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001328 if (err != 1) {
1329 if (err != -ENOENT) {
1330 menu_destroy(m);
1331 return NULL;
1332 }
1333
1334 printf("Missing default: %s\n", cfg->default_label);
1335 }
1336 }
1337
1338 return m;
1339}
1340
1341/*
1342 * Try to boot any labels we have yet to attempt to boot.
1343 */
1344static void boot_unattempted_labels(struct pxe_menu *cfg)
1345{
1346 struct list_head *pos;
1347 struct pxe_label *label;
1348
1349 list_for_each(pos, &cfg->labels) {
1350 label = list_entry(pos, struct pxe_label, list);
1351
1352 if (!label->attempted)
1353 label_boot(label);
1354 }
1355}
1356
1357/*
1358 * Boot the system as prescribed by a pxe_menu.
1359 *
1360 * Use the menu system to either get the user's choice or the default, based
1361 * on config or user input. If there is no default or user's choice,
1362 * attempted to boot labels in the order they were given in pxe files.
1363 * If the default or user's choice fails to boot, attempt to boot other
1364 * labels in the order they were given in pxe files.
1365 *
1366 * If this function returns, there weren't any labels that successfully
1367 * booted, or the user interrupted the menu selection via ctrl+c.
1368 */
1369static void handle_pxe_menu(struct pxe_menu *cfg)
1370{
1371 void *choice;
1372 struct menu *m;
1373 int err;
1374
1375 m = pxe_menu_to_menu(cfg);
1376 if (!m)
1377 return;
1378
1379 err = menu_get_choice(m, &choice);
1380
1381 menu_destroy(m);
1382
Jason Hobbs6f40f272011-11-07 03:07:15 +00001383 /*
1384 * err == 1 means we got a choice back from menu_get_choice.
1385 *
1386 * err == -ENOENT if the menu was setup to select the default but no
1387 * default was set. in that case, we should continue trying to boot
1388 * labels that haven't been attempted yet.
1389 *
1390 * otherwise, the user interrupted or there was some other error and
1391 * we give up.
1392 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001393
Rob Herringc27d5602012-04-12 13:55:20 -05001394 if (err == 1) {
1395 err = label_boot(choice);
1396 if (!err)
1397 return;
1398 }
Jason Hobbs6f40f272011-11-07 03:07:15 +00001399 else if (err != -ENOENT)
1400 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001401
1402 boot_unattempted_labels(cfg);
1403}
1404
1405/*
1406 * Boots a system using a pxe file
1407 *
1408 * Returns 0 on success, 1 on error.
1409 */
1410static int
1411do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1412{
1413 unsigned long pxefile_addr_r;
1414 struct pxe_menu *cfg;
1415 char *pxefile_addr_str;
1416
Rob Herring669df7e2012-05-25 10:47:39 +00001417 do_getfile = do_get_tftp;
1418
Jason Hobbs06283a62011-08-31 10:37:30 -05001419 if (argc == 1) {
1420 pxefile_addr_str = from_env("pxefile_addr_r");
1421 if (!pxefile_addr_str)
1422 return 1;
1423
1424 } else if (argc == 2) {
1425 pxefile_addr_str = argv[1];
1426 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001427 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001428 }
1429
1430 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1431 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1432 return 1;
1433 }
1434
1435 cfg = parse_pxefile((char *)(pxefile_addr_r));
1436
1437 if (cfg == NULL) {
1438 printf("Error parsing config file\n");
1439 return 1;
1440 }
1441
1442 handle_pxe_menu(cfg);
1443
1444 destroy_pxe_menu(cfg);
1445
1446 return 0;
1447}
1448
1449static cmd_tbl_t cmd_pxe_sub[] = {
1450 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1451 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1452};
1453
1454int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1455{
1456 cmd_tbl_t *cp;
1457
1458 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001459 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001460
1461 /* drop initial "pxe" arg */
1462 argc--;
1463 argv++;
1464
1465 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1466
1467 if (cp)
1468 return cp->cmd(cmdtp, flag, argc, argv);
1469
Simon Glass4c12eeb2011-12-10 08:44:01 +00001470 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001471}
1472
1473U_BOOT_CMD(
1474 pxe, 3, 1, do_pxe,
1475 "commands to get and boot from pxe files",
1476 "get - try to retrieve a pxe file using tftp\npxe "
1477 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1478);
Rob Herring669df7e2012-05-25 10:47:39 +00001479
1480/*
1481 * Boots a system using a local disk syslinux/extlinux file
1482 *
1483 * Returns 0 on success, 1 on error.
1484 */
1485int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1486{
1487 unsigned long pxefile_addr_r;
1488 struct pxe_menu *cfg;
1489 char *pxefile_addr_str;
1490 char *filename;
1491 int prompt = 0;
1492
1493 if (strstr(argv[1], "-p")) {
1494 prompt = 1;
1495 argc--;
1496 argv++;
1497 }
1498
1499 if (argc < 4)
1500 return cmd_usage(cmdtp);
1501
1502 if (argc < 5) {
1503 pxefile_addr_str = from_env("pxefile_addr_r");
1504 if (!pxefile_addr_str)
1505 return 1;
1506 } else {
1507 pxefile_addr_str = argv[4];
1508 }
1509
1510 if (argc < 6)
1511 filename = getenv("bootfile");
1512 else {
1513 filename = argv[5];
1514 setenv("bootfile", filename);
1515 }
1516
1517 if (strstr(argv[3], "ext2"))
1518 do_getfile = do_get_ext2;
1519 else if (strstr(argv[3], "fat"))
1520 do_getfile = do_get_fat;
1521 else {
1522 printf("Invalid filesystem: %s\n", argv[3]);
1523 return 1;
1524 }
1525 fs_argv[1] = argv[1];
1526 fs_argv[2] = argv[2];
1527
1528 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1529 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1530 return 1;
1531 }
1532
1533 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1534 printf("Error reading config file\n");
1535 return 1;
1536 }
1537
1538 cfg = parse_pxefile((char *)(pxefile_addr_r));
1539
1540 if (cfg == NULL) {
1541 printf("Error parsing config file\n");
1542 return 1;
1543 }
1544
1545 if (prompt)
1546 cfg->prompt = 1;
1547
1548 handle_pxe_menu(cfg);
1549
1550 destroy_pxe_menu(cfg);
1551
1552 return 0;
1553}
1554
1555U_BOOT_CMD(
1556 sysboot, 7, 1, do_sysboot,
1557 "command to get and boot from syslinux files",
1558 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1559 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1560 " filesystem on 'dev' on 'interface' to address 'addr'"
1561);