blob: 9f1c14d2da142af7af718d16b5855be44c46d312 [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;
Rob Herringdfba6102012-09-17 21:54:05 -0500472 int ipappend;
Jason Hobbs06283a62011-08-31 10:37:30 -0500473 int attempted;
474 int localboot;
Rob Herringc27d5602012-04-12 13:55:20 -0500475 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500476 struct list_head list;
477};
478
479/*
480 * Describes a pxe menu as given via pxe files.
481 *
482 * title - the name of the menu as given by a 'menu title' line.
483 * default_label - the name of the default label, if any.
484 * timeout - time in tenths of a second to wait for a user key-press before
485 * booting the default label.
486 * prompt - if 0, don't prompt for a choice unless the timeout period is
487 * interrupted. If 1, always prompt for a choice regardless of
488 * timeout.
489 * labels - a list of labels defined for the menu.
490 */
491struct pxe_menu {
492 char *title;
493 char *default_label;
494 int timeout;
495 int prompt;
496 struct list_head labels;
497};
498
499/*
500 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
501 * result must be free()'d to reclaim the memory.
502 *
503 * Returns NULL if malloc fails.
504 */
505static struct pxe_label *label_create(void)
506{
507 struct pxe_label *label;
508
509 label = malloc(sizeof(struct pxe_label));
510
511 if (!label)
512 return NULL;
513
514 memset(label, 0, sizeof(struct pxe_label));
515
516 return label;
517}
518
519/*
520 * Free the memory used by a pxe_label, including that used by its name,
521 * kernel, append and initrd members, if they're non NULL.
522 *
523 * So - be sure to only use dynamically allocated memory for the members of
524 * the pxe_label struct, unless you want to clean it up first. These are
525 * currently only created by the pxe file parsing code.
526 */
527static void label_destroy(struct pxe_label *label)
528{
529 if (label->name)
530 free(label->name);
531
532 if (label->kernel)
533 free(label->kernel);
534
535 if (label->append)
536 free(label->append);
537
538 if (label->initrd)
539 free(label->initrd);
540
Chander Kashyapa6559382012-09-06 19:36:31 +0000541 if (label->fdt)
542 free(label->fdt);
543
Jason Hobbs06283a62011-08-31 10:37:30 -0500544 free(label);
545}
546
547/*
548 * Print a label and its string members if they're defined.
549 *
550 * This is passed as a callback to the menu code for displaying each
551 * menu entry.
552 */
553static void label_print(void *data)
554{
555 struct pxe_label *label = data;
Rob Herring4b3efae2012-04-14 22:23:21 -0500556 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500557
Rob Herring4b3efae2012-04-14 22:23:21 -0500558 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500559}
560
561/*
562 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
563 * environment variable is defined. Its contents will be executed as U-boot
564 * command. If the label specified an 'append' line, its contents will be
565 * used to overwrite the contents of the 'bootargs' environment variable prior
566 * to running 'localcmd'.
567 *
568 * Returns 1 on success or < 0 on error.
569 */
570static int label_localboot(struct pxe_label *label)
571{
Simon Glassd51004a2012-03-30 21:30:55 +0000572 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500573
574 localcmd = from_env("localcmd");
575
576 if (!localcmd)
577 return -ENOENT;
578
Jason Hobbs06283a62011-08-31 10:37:30 -0500579 if (label->append)
580 setenv("bootargs", label->append);
581
Simon Glassd51004a2012-03-30 21:30:55 +0000582 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500583
Simon Glassd51004a2012-03-30 21:30:55 +0000584 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500585}
586
587/*
588 * Boot according to the contents of a pxe_label.
589 *
590 * If we can't boot for any reason, we return. A successful boot never
591 * returns.
592 *
593 * The kernel will be stored in the location given by the 'kernel_addr_r'
594 * environment variable.
595 *
596 * If the label specifies an initrd file, it will be stored in the location
597 * given by the 'ramdisk_addr_r' environment variable.
598 *
599 * If the label specifies an 'append' line, its contents will overwrite that
600 * of the 'bootargs' environment variable.
601 */
Rob Herringc27d5602012-04-12 13:55:20 -0500602static int label_boot(struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500603{
604 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herring5af27692012-03-26 13:16:59 -0500605 char initrd_str[22];
Rob Herringdfba6102012-09-17 21:54:05 -0500606 char mac_str[29] = "";
607 char ip_str[68] = "";
608 char *bootargs;
Jason Hobbs06283a62011-08-31 10:37:30 -0500609 int bootm_argc = 3;
Rob Herringdfba6102012-09-17 21:54:05 -0500610 int len = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500611
612 label_print(label);
613
614 label->attempted = 1;
615
616 if (label->localboot) {
Rob Herringc27d5602012-04-12 13:55:20 -0500617 if (label->localboot_val >= 0)
618 label_localboot(label);
619 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500620 }
621
622 if (label->kernel == NULL) {
623 printf("No kernel given, skipping %s\n",
624 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500625 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500626 }
627
628 if (label->initrd) {
629 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
630 printf("Skipping %s for failure retrieving initrd\n",
631 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500632 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500633 }
634
Rob Herring5af27692012-03-26 13:16:59 -0500635 bootm_argv[2] = initrd_str;
636 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
637 strcat(bootm_argv[2], ":");
638 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500639 } else {
640 bootm_argv[2] = "-";
641 }
642
643 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
644 printf("Skipping %s for failure retrieving kernel\n",
645 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500646 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500647 }
648
Rob Herringdfba6102012-09-17 21:54:05 -0500649 if (label->ipappend & 0x1) {
650 sprintf(ip_str, " ip=%s:%s:%s:%s",
651 getenv("ipaddr"), getenv("serverip"),
652 getenv("gatewayip"), getenv("netmask"));
653 len += strlen(ip_str);
654 }
655
656 if (label->ipappend & 0x2) {
657 int err;
658 strcpy(mac_str, " BOOTIF=");
659 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
660 if (err < 0)
661 mac_str[0] = '\0';
662 len += strlen(mac_str);
663 }
664
665 if (label->append)
666 len += strlen(label->append);
667
668 if (len) {
Rob Herring89599d42012-09-20 20:45:48 -0500669 bootargs = malloc(len + 1);
Rob Herringdfba6102012-09-17 21:54:05 -0500670 if (!bootargs)
671 return 1;
672 bootargs[0] ='\0';
673 if (label->append)
674 strcpy(bootargs, label->append);
675 strcat(bootargs, ip_str);
676 strcat(bootargs, mac_str);
677
678 setenv("bootargs", bootargs);
679 printf("append: %s\n", bootargs);
680
681 free(bootargs);
Rob Herring4b3efae2012-04-14 22:23:21 -0500682 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500683
684 bootm_argv[1] = getenv("kernel_addr_r");
685
686 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000687 * fdt usage is optional:
688 * It handles the following scenarios. All scenarios are exclusive
689 *
690 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
691 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
692 * and adjust argc appropriately.
693 *
694 * Scenario 2: If there is an fdt_addr specified, pass it along to
695 * bootm, and adjust argc appropriately.
696 *
697 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500698 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000699 bootm_argv[3] = getenv("fdt_addr_r");
700
701 /* if fdt label is defined then get fdt from server */
702 if (bootm_argv[3] && label->fdt) {
703 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
704 printf("Skipping %s for failure retrieving fdt\n",
705 label->name);
706 return;
707 }
708 } else
709 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500710
711 if (bootm_argv[3])
712 bootm_argc = 4;
713
Rob Herring5af27692012-03-26 13:16:59 -0500714#ifdef CONFIG_CMD_BOOTZ
715 do_bootz(NULL, 0, bootm_argc, bootm_argv);
716#else
Jason Hobbs06283a62011-08-31 10:37:30 -0500717 do_bootm(NULL, 0, bootm_argc, bootm_argv);
Rob Herring5af27692012-03-26 13:16:59 -0500718#endif
Rob Herringc27d5602012-04-12 13:55:20 -0500719 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500720}
721
722/*
723 * Tokens for the pxe file parser.
724 */
725enum token_type {
726 T_EOL,
727 T_STRING,
728 T_EOF,
729 T_MENU,
730 T_TITLE,
731 T_TIMEOUT,
732 T_LABEL,
733 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000734 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500735 T_APPEND,
736 T_INITRD,
737 T_LOCALBOOT,
738 T_DEFAULT,
739 T_PROMPT,
740 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000741 T_FDT,
Rob Herringdf6be7e2012-05-02 18:57:48 -0500742 T_ONTIMEOUT,
Rob Herringdfba6102012-09-17 21:54:05 -0500743 T_IPAPPEND,
Jason Hobbs06283a62011-08-31 10:37:30 -0500744 T_INVALID
745};
746
747/*
748 * A token - given by a value and a type.
749 */
750struct token {
751 char *val;
752 enum token_type type;
753};
754
755/*
756 * Keywords recognized.
757 */
758static const struct token keywords[] = {
759 {"menu", T_MENU},
760 {"title", T_TITLE},
761 {"timeout", T_TIMEOUT},
762 {"default", T_DEFAULT},
763 {"prompt", T_PROMPT},
764 {"label", T_LABEL},
765 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000766 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500767 {"localboot", T_LOCALBOOT},
768 {"append", T_APPEND},
769 {"initrd", T_INITRD},
770 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000771 {"fdt", T_FDT},
Rob Herringdf6be7e2012-05-02 18:57:48 -0500772 {"ontimeout", T_ONTIMEOUT,},
Rob Herringdfba6102012-09-17 21:54:05 -0500773 {"ipappend", T_IPAPPEND,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500774 {NULL, T_INVALID}
775};
776
777/*
778 * Since pxe(linux) files don't have a token to identify the start of a
779 * literal, we have to keep track of when we're in a state where a literal is
780 * expected vs when we're in a state a keyword is expected.
781 */
782enum lex_state {
783 L_NORMAL = 0,
784 L_KEYWORD,
785 L_SLITERAL
786};
787
788/*
789 * get_string retrieves a string from *p and stores it as a token in
790 * *t.
791 *
792 * get_string used for scanning both string literals and keywords.
793 *
794 * Characters from *p are copied into t-val until a character equal to
795 * delim is found, or a NUL byte is reached. If delim has the special value of
796 * ' ', any whitespace character will be used as a delimiter.
797 *
798 * If lower is unequal to 0, uppercase characters will be converted to
799 * lowercase in the result. This is useful to make keywords case
800 * insensitive.
801 *
802 * The location of *p is updated to point to the first character after the end
803 * of the token - the ending delimiter.
804 *
805 * On success, the new value of t->val is returned. Memory for t->val is
806 * allocated using malloc and must be free()'d to reclaim it. If insufficient
807 * memory is available, NULL is returned.
808 */
809static char *get_string(char **p, struct token *t, char delim, int lower)
810{
811 char *b, *e;
812 size_t len, i;
813
814 /*
815 * b and e both start at the beginning of the input stream.
816 *
817 * e is incremented until we find the ending delimiter, or a NUL byte
818 * is reached. Then, we take e - b to find the length of the token.
819 */
820 b = e = *p;
821
822 while (*e) {
823 if ((delim == ' ' && isspace(*e)) || delim == *e)
824 break;
825 e++;
826 }
827
828 len = e - b;
829
830 /*
831 * Allocate memory to hold the string, and copy it in, converting
832 * characters to lowercase if lower is != 0.
833 */
834 t->val = malloc(len + 1);
835 if (!t->val)
836 return NULL;
837
838 for (i = 0; i < len; i++, b++) {
839 if (lower)
840 t->val[i] = tolower(*b);
841 else
842 t->val[i] = *b;
843 }
844
845 t->val[len] = '\0';
846
847 /*
848 * Update *p so the caller knows where to continue scanning.
849 */
850 *p = e;
851
852 t->type = T_STRING;
853
854 return t->val;
855}
856
857/*
858 * Populate a keyword token with a type and value.
859 */
860static void get_keyword(struct token *t)
861{
862 int i;
863
864 for (i = 0; keywords[i].val; i++) {
865 if (!strcmp(t->val, keywords[i].val)) {
866 t->type = keywords[i].type;
867 break;
868 }
869 }
870}
871
872/*
873 * Get the next token. We have to keep track of which state we're in to know
874 * if we're looking to get a string literal or a keyword.
875 *
876 * *p is updated to point at the first character after the current token.
877 */
878static void get_token(char **p, struct token *t, enum lex_state state)
879{
880 char *c = *p;
881
882 t->type = T_INVALID;
883
884 /* eat non EOL whitespace */
885 while (isblank(*c))
886 c++;
887
888 /*
889 * eat comments. note that string literals can't begin with #, but
890 * can contain a # after their first character.
891 */
892 if (*c == '#') {
893 while (*c && *c != '\n')
894 c++;
895 }
896
897 if (*c == '\n') {
898 t->type = T_EOL;
899 c++;
900 } else if (*c == '\0') {
901 t->type = T_EOF;
902 c++;
903 } else if (state == L_SLITERAL) {
904 get_string(&c, t, '\n', 0);
905 } else if (state == L_KEYWORD) {
906 /*
907 * when we expect a keyword, we first get the next string
908 * token delimited by whitespace, and then check if it
909 * matches a keyword in our keyword list. if it does, it's
910 * converted to a keyword token of the appropriate type, and
911 * if not, it remains a string token.
912 */
913 get_string(&c, t, ' ', 1);
914 get_keyword(t);
915 }
916
917 *p = c;
918}
919
920/*
921 * Increment *c until we get to the end of the current line, or EOF.
922 */
923static void eol_or_eof(char **c)
924{
925 while (**c && **c != '\n')
926 (*c)++;
927}
928
929/*
930 * All of these parse_* functions share some common behavior.
931 *
932 * They finish with *c pointing after the token they parse, and return 1 on
933 * success, or < 0 on error.
934 */
935
936/*
937 * Parse a string literal and store a pointer it at *dst. String literals
938 * terminate at the end of the line.
939 */
940static int parse_sliteral(char **c, char **dst)
941{
942 struct token t;
943 char *s = *c;
944
945 get_token(c, &t, L_SLITERAL);
946
947 if (t.type != T_STRING) {
948 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
949 return -EINVAL;
950 }
951
952 *dst = t.val;
953
954 return 1;
955}
956
957/*
958 * Parse a base 10 (unsigned) integer and store it at *dst.
959 */
960static int parse_integer(char **c, int *dst)
961{
962 struct token t;
963 char *s = *c;
964 unsigned long temp;
965
966 get_token(c, &t, L_SLITERAL);
967
968 if (t.type != T_STRING) {
969 printf("Expected string: %.*s\n", (int)(*c - s), s);
970 return -EINVAL;
971 }
972
Rob Herringc27d5602012-04-12 13:55:20 -0500973 *dst = simple_strtol(t.val, &temp, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -0500974
975 free(t.val);
976
977 return 1;
978}
979
980static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
981
982/*
983 * Parse an include statement, and retrieve and parse the file it mentions.
984 *
985 * base should point to a location where it's safe to store the file, and
986 * nest_level should indicate how many nested includes have occurred. For this
987 * include, nest_level has already been incremented and doesn't need to be
988 * incremented here.
989 */
990static int handle_include(char **c, char *base,
991 struct pxe_menu *cfg, int nest_level)
992{
993 char *include_path;
994 char *s = *c;
995 int err;
996
997 err = parse_sliteral(c, &include_path);
998
999 if (err < 0) {
1000 printf("Expected include path: %.*s\n",
1001 (int)(*c - s), s);
1002 return err;
1003 }
1004
1005 err = get_pxe_file(include_path, base);
1006
1007 if (err < 0) {
1008 printf("Couldn't retrieve %s\n", include_path);
1009 return err;
1010 }
1011
1012 return parse_pxefile_top(base, cfg, nest_level);
1013}
1014
1015/*
1016 * Parse lines that begin with 'menu'.
1017 *
1018 * b and nest are provided to handle the 'menu include' case.
1019 *
1020 * b should be the address where the file currently being parsed is stored.
1021 *
1022 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1023 * a file it includes, 3 when parsing a file included by that file, and so on.
1024 */
1025static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
1026{
1027 struct token t;
1028 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +00001029 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -05001030
1031 get_token(c, &t, L_KEYWORD);
1032
1033 switch (t.type) {
1034 case T_TITLE:
1035 err = parse_sliteral(c, &cfg->title);
1036
1037 break;
1038
1039 case T_INCLUDE:
1040 err = handle_include(c, b + strlen(b) + 1, cfg,
1041 nest_level + 1);
1042 break;
1043
1044 default:
1045 printf("Ignoring malformed menu command: %.*s\n",
1046 (int)(*c - s), s);
1047 }
1048
1049 if (err < 0)
1050 return err;
1051
1052 eol_or_eof(c);
1053
1054 return 1;
1055}
1056
1057/*
1058 * Handles parsing a 'menu line' when we're parsing a label.
1059 */
1060static int parse_label_menu(char **c, struct pxe_menu *cfg,
1061 struct pxe_label *label)
1062{
1063 struct token t;
1064 char *s;
1065
1066 s = *c;
1067
1068 get_token(c, &t, L_KEYWORD);
1069
1070 switch (t.type) {
1071 case T_DEFAULT:
Rob Herringdf6be7e2012-05-02 18:57:48 -05001072 if (!cfg->default_label)
1073 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001074
1075 if (!cfg->default_label)
1076 return -ENOMEM;
1077
1078 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001079 case T_LABEL:
1080 parse_sliteral(c, &label->menu);
1081 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001082 default:
1083 printf("Ignoring malformed menu command: %.*s\n",
1084 (int)(*c - s), s);
1085 }
1086
1087 eol_or_eof(c);
1088
1089 return 0;
1090}
1091
1092/*
1093 * Parses a label and adds it to the list of labels for a menu.
1094 *
1095 * A label ends when we either get to the end of a file, or
1096 * get some input we otherwise don't have a handler defined
1097 * for.
1098 *
1099 */
1100static int parse_label(char **c, struct pxe_menu *cfg)
1101{
1102 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001103 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001104 char *s = *c;
1105 struct pxe_label *label;
1106 int err;
Rob Herringc27d5602012-04-12 13:55:20 -05001107 int localboot;
Jason Hobbs06283a62011-08-31 10:37:30 -05001108
1109 label = label_create();
1110 if (!label)
1111 return -ENOMEM;
1112
1113 err = parse_sliteral(c, &label->name);
1114 if (err < 0) {
1115 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1116 label_destroy(label);
1117 return -EINVAL;
1118 }
1119
1120 list_add_tail(&label->list, &cfg->labels);
1121
1122 while (1) {
1123 s = *c;
1124 get_token(c, &t, L_KEYWORD);
1125
1126 err = 0;
1127 switch (t.type) {
1128 case T_MENU:
1129 err = parse_label_menu(c, cfg, label);
1130 break;
1131
1132 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001133 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001134 err = parse_sliteral(c, &label->kernel);
1135 break;
1136
1137 case T_APPEND:
1138 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001139 if (label->initrd)
1140 break;
1141 s = strstr(label->append, "initrd=");
1142 if (!s)
1143 break;
1144 s += 7;
1145 len = (int)(strchr(s, ' ') - s);
1146 label->initrd = malloc(len + 1);
1147 strncpy(label->initrd, s, len);
1148 label->initrd[len] = '\0';
1149
Jason Hobbs06283a62011-08-31 10:37:30 -05001150 break;
1151
1152 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001153 if (!label->initrd)
1154 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001155 break;
1156
Chander Kashyapa6559382012-09-06 19:36:31 +00001157 case T_FDT:
1158 if (!label->fdt)
1159 err = parse_sliteral(c, &label->fdt);
1160 break;
1161
Jason Hobbs06283a62011-08-31 10:37:30 -05001162 case T_LOCALBOOT:
Rob Herringc27d5602012-04-12 13:55:20 -05001163 label->localboot = 1;
1164 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001165 break;
1166
Rob Herringdfba6102012-09-17 21:54:05 -05001167 case T_IPAPPEND:
1168 err = parse_integer(c, &label->ipappend);
1169 break;
1170
Jason Hobbs06283a62011-08-31 10:37:30 -05001171 case T_EOL:
1172 break;
1173
1174 default:
1175 /*
1176 * put the token back! we don't want it - it's the end
1177 * of a label and whatever token this is, it's
1178 * something for the menu level context to handle.
1179 */
1180 *c = s;
1181 return 1;
1182 }
1183
1184 if (err < 0)
1185 return err;
1186 }
1187}
1188
1189/*
1190 * This 16 comes from the limit pxelinux imposes on nested includes.
1191 *
1192 * There is no reason at all we couldn't do more, but some limit helps prevent
1193 * infinite (until crash occurs) recursion if a file tries to include itself.
1194 */
1195#define MAX_NEST_LEVEL 16
1196
1197/*
1198 * Entry point for parsing a menu file. nest_level indicates how many times
1199 * we've nested in includes. It will be 1 for the top level menu file.
1200 *
1201 * Returns 1 on success, < 0 on error.
1202 */
1203static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1204{
1205 struct token t;
1206 char *s, *b, *label_name;
1207 int err;
1208
1209 b = p;
1210
1211 if (nest_level > MAX_NEST_LEVEL) {
1212 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1213 return -EMLINK;
1214 }
1215
1216 while (1) {
1217 s = p;
1218
1219 get_token(&p, &t, L_KEYWORD);
1220
1221 err = 0;
1222 switch (t.type) {
1223 case T_MENU:
Rob Herring2619f512012-04-12 13:33:43 -05001224 cfg->prompt = 1;
Jason Hobbs06283a62011-08-31 10:37:30 -05001225 err = parse_menu(&p, cfg, b, nest_level);
1226 break;
1227
1228 case T_TIMEOUT:
1229 err = parse_integer(&p, &cfg->timeout);
1230 break;
1231
1232 case T_LABEL:
1233 err = parse_label(&p, cfg);
1234 break;
1235
1236 case T_DEFAULT:
Rob Herringdf6be7e2012-05-02 18:57:48 -05001237 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001238 err = parse_sliteral(&p, &label_name);
1239
1240 if (label_name) {
1241 if (cfg->default_label)
1242 free(cfg->default_label);
1243
1244 cfg->default_label = label_name;
1245 }
1246
1247 break;
1248
Rob Herring1e085222012-05-25 10:43:16 +00001249 case T_INCLUDE:
1250 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1251 nest_level + 1);
1252 break;
1253
Jason Hobbs06283a62011-08-31 10:37:30 -05001254 case T_PROMPT:
Rob Herring2619f512012-04-12 13:33:43 -05001255 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001256 break;
1257
1258 case T_EOL:
1259 break;
1260
1261 case T_EOF:
1262 return 1;
1263
1264 default:
1265 printf("Ignoring unknown command: %.*s\n",
1266 (int)(p - s), s);
1267 eol_or_eof(&p);
1268 }
1269
1270 if (err < 0)
1271 return err;
1272 }
1273}
1274
1275/*
1276 * Free the memory used by a pxe_menu and its labels.
1277 */
1278static void destroy_pxe_menu(struct pxe_menu *cfg)
1279{
1280 struct list_head *pos, *n;
1281 struct pxe_label *label;
1282
1283 if (cfg->title)
1284 free(cfg->title);
1285
1286 if (cfg->default_label)
1287 free(cfg->default_label);
1288
1289 list_for_each_safe(pos, n, &cfg->labels) {
1290 label = list_entry(pos, struct pxe_label, list);
1291
1292 label_destroy(label);
1293 }
1294
1295 free(cfg);
1296}
1297
1298/*
1299 * Entry point for parsing a pxe file. This is only used for the top level
1300 * file.
1301 *
1302 * Returns NULL if there is an error, otherwise, returns a pointer to a
1303 * pxe_menu struct populated with the results of parsing the pxe file (and any
1304 * files it includes). The resulting pxe_menu struct can be free()'d by using
1305 * the destroy_pxe_menu() function.
1306 */
1307static struct pxe_menu *parse_pxefile(char *menucfg)
1308{
1309 struct pxe_menu *cfg;
1310
1311 cfg = malloc(sizeof(struct pxe_menu));
1312
1313 if (!cfg)
1314 return NULL;
1315
1316 memset(cfg, 0, sizeof(struct pxe_menu));
1317
1318 INIT_LIST_HEAD(&cfg->labels);
1319
1320 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1321 destroy_pxe_menu(cfg);
1322 return NULL;
1323 }
1324
1325 return cfg;
1326}
1327
1328/*
1329 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1330 * menu code.
1331 */
1332static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1333{
1334 struct pxe_label *label;
1335 struct list_head *pos;
1336 struct menu *m;
1337 int err;
Rob Herring4b3efae2012-04-14 22:23:21 -05001338 int i = 1;
Rob Herring0731b7b2012-05-02 18:52:49 -05001339 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001340
1341 /*
1342 * Create a menu and add items for all the labels.
1343 */
1344 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1345
1346 if (!m)
1347 return NULL;
1348
1349 list_for_each(pos, &cfg->labels) {
1350 label = list_entry(pos, struct pxe_label, list);
1351
Rob Herring4b3efae2012-04-14 22:23:21 -05001352 sprintf(label->num, "%d", i++);
1353 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001354 menu_destroy(m);
1355 return NULL;
1356 }
Rob Herring0731b7b2012-05-02 18:52:49 -05001357 if (cfg->default_label &&
1358 (strcmp(label->name, cfg->default_label) == 0))
1359 default_num = label->num;
1360
Jason Hobbs06283a62011-08-31 10:37:30 -05001361 }
1362
1363 /*
1364 * After we've created items for each label in the menu, set the
1365 * menu's default label if one was specified.
1366 */
Rob Herring0731b7b2012-05-02 18:52:49 -05001367 if (default_num) {
1368 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001369 if (err != 1) {
1370 if (err != -ENOENT) {
1371 menu_destroy(m);
1372 return NULL;
1373 }
1374
1375 printf("Missing default: %s\n", cfg->default_label);
1376 }
1377 }
1378
1379 return m;
1380}
1381
1382/*
1383 * Try to boot any labels we have yet to attempt to boot.
1384 */
1385static void boot_unattempted_labels(struct pxe_menu *cfg)
1386{
1387 struct list_head *pos;
1388 struct pxe_label *label;
1389
1390 list_for_each(pos, &cfg->labels) {
1391 label = list_entry(pos, struct pxe_label, list);
1392
1393 if (!label->attempted)
1394 label_boot(label);
1395 }
1396}
1397
1398/*
1399 * Boot the system as prescribed by a pxe_menu.
1400 *
1401 * Use the menu system to either get the user's choice or the default, based
1402 * on config or user input. If there is no default or user's choice,
1403 * attempted to boot labels in the order they were given in pxe files.
1404 * If the default or user's choice fails to boot, attempt to boot other
1405 * labels in the order they were given in pxe files.
1406 *
1407 * If this function returns, there weren't any labels that successfully
1408 * booted, or the user interrupted the menu selection via ctrl+c.
1409 */
1410static void handle_pxe_menu(struct pxe_menu *cfg)
1411{
1412 void *choice;
1413 struct menu *m;
1414 int err;
1415
1416 m = pxe_menu_to_menu(cfg);
1417 if (!m)
1418 return;
1419
1420 err = menu_get_choice(m, &choice);
1421
1422 menu_destroy(m);
1423
Jason Hobbs6f40f272011-11-07 03:07:15 +00001424 /*
1425 * err == 1 means we got a choice back from menu_get_choice.
1426 *
1427 * err == -ENOENT if the menu was setup to select the default but no
1428 * default was set. in that case, we should continue trying to boot
1429 * labels that haven't been attempted yet.
1430 *
1431 * otherwise, the user interrupted or there was some other error and
1432 * we give up.
1433 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001434
Rob Herringc27d5602012-04-12 13:55:20 -05001435 if (err == 1) {
1436 err = label_boot(choice);
1437 if (!err)
1438 return;
1439 }
Jason Hobbs6f40f272011-11-07 03:07:15 +00001440 else if (err != -ENOENT)
1441 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001442
1443 boot_unattempted_labels(cfg);
1444}
1445
1446/*
1447 * Boots a system using a pxe file
1448 *
1449 * Returns 0 on success, 1 on error.
1450 */
1451static int
1452do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1453{
1454 unsigned long pxefile_addr_r;
1455 struct pxe_menu *cfg;
1456 char *pxefile_addr_str;
1457
Rob Herring669df7e2012-05-25 10:47:39 +00001458 do_getfile = do_get_tftp;
1459
Jason Hobbs06283a62011-08-31 10:37:30 -05001460 if (argc == 1) {
1461 pxefile_addr_str = from_env("pxefile_addr_r");
1462 if (!pxefile_addr_str)
1463 return 1;
1464
1465 } else if (argc == 2) {
1466 pxefile_addr_str = argv[1];
1467 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001468 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001469 }
1470
1471 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1472 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1473 return 1;
1474 }
1475
1476 cfg = parse_pxefile((char *)(pxefile_addr_r));
1477
1478 if (cfg == NULL) {
1479 printf("Error parsing config file\n");
1480 return 1;
1481 }
1482
1483 handle_pxe_menu(cfg);
1484
1485 destroy_pxe_menu(cfg);
1486
1487 return 0;
1488}
1489
1490static cmd_tbl_t cmd_pxe_sub[] = {
1491 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1492 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1493};
1494
1495int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1496{
1497 cmd_tbl_t *cp;
1498
1499 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001500 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001501
1502 /* drop initial "pxe" arg */
1503 argc--;
1504 argv++;
1505
1506 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1507
1508 if (cp)
1509 return cp->cmd(cmdtp, flag, argc, argv);
1510
Simon Glass4c12eeb2011-12-10 08:44:01 +00001511 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001512}
1513
1514U_BOOT_CMD(
1515 pxe, 3, 1, do_pxe,
1516 "commands to get and boot from pxe files",
1517 "get - try to retrieve a pxe file using tftp\npxe "
1518 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1519);
Rob Herring669df7e2012-05-25 10:47:39 +00001520
1521/*
1522 * Boots a system using a local disk syslinux/extlinux file
1523 *
1524 * Returns 0 on success, 1 on error.
1525 */
1526int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1527{
1528 unsigned long pxefile_addr_r;
1529 struct pxe_menu *cfg;
1530 char *pxefile_addr_str;
1531 char *filename;
1532 int prompt = 0;
1533
1534 if (strstr(argv[1], "-p")) {
1535 prompt = 1;
1536 argc--;
1537 argv++;
1538 }
1539
1540 if (argc < 4)
1541 return cmd_usage(cmdtp);
1542
1543 if (argc < 5) {
1544 pxefile_addr_str = from_env("pxefile_addr_r");
1545 if (!pxefile_addr_str)
1546 return 1;
1547 } else {
1548 pxefile_addr_str = argv[4];
1549 }
1550
1551 if (argc < 6)
1552 filename = getenv("bootfile");
1553 else {
1554 filename = argv[5];
1555 setenv("bootfile", filename);
1556 }
1557
1558 if (strstr(argv[3], "ext2"))
1559 do_getfile = do_get_ext2;
1560 else if (strstr(argv[3], "fat"))
1561 do_getfile = do_get_fat;
1562 else {
1563 printf("Invalid filesystem: %s\n", argv[3]);
1564 return 1;
1565 }
1566 fs_argv[1] = argv[1];
1567 fs_argv[2] = argv[2];
1568
1569 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1570 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1571 return 1;
1572 }
1573
1574 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1575 printf("Error reading config file\n");
1576 return 1;
1577 }
1578
1579 cfg = parse_pxefile((char *)(pxefile_addr_r));
1580
1581 if (cfg == NULL) {
1582 printf("Error parsing config file\n");
1583 return 1;
1584 }
1585
1586 if (prompt)
1587 cfg->prompt = 1;
1588
1589 handle_pxe_menu(cfg);
1590
1591 destroy_pxe_menu(cfg);
1592
1593 return 0;
1594}
1595
1596U_BOOT_CMD(
1597 sysboot, 7, 1, do_sysboot,
1598 "command to get and boot from syslinux files",
1599 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1600 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1601 " filesystem on 'dev' on 'interface' to address 'addr'"
1602);