blob: 346eb61a627ed8c52add3d255765f71fb7092c83 [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
714 do_bootm(NULL, 0, bootm_argc, bootm_argv);
Rob Herring43ee87a2012-12-03 13:17:21 -0600715
716#ifdef CONFIG_CMD_BOOTZ
717 /* Try booting a zImage if do_bootm returns */
718 do_bootz(NULL, 0, bootm_argc, bootm_argv);
Rob Herring5af27692012-03-26 13:16:59 -0500719#endif
Rob Herringc27d5602012-04-12 13:55:20 -0500720 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500721}
722
723/*
724 * Tokens for the pxe file parser.
725 */
726enum token_type {
727 T_EOL,
728 T_STRING,
729 T_EOF,
730 T_MENU,
731 T_TITLE,
732 T_TIMEOUT,
733 T_LABEL,
734 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000735 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500736 T_APPEND,
737 T_INITRD,
738 T_LOCALBOOT,
739 T_DEFAULT,
740 T_PROMPT,
741 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000742 T_FDT,
Rob Herringdf6be7e2012-05-02 18:57:48 -0500743 T_ONTIMEOUT,
Rob Herringdfba6102012-09-17 21:54:05 -0500744 T_IPAPPEND,
Jason Hobbs06283a62011-08-31 10:37:30 -0500745 T_INVALID
746};
747
748/*
749 * A token - given by a value and a type.
750 */
751struct token {
752 char *val;
753 enum token_type type;
754};
755
756/*
757 * Keywords recognized.
758 */
759static const struct token keywords[] = {
760 {"menu", T_MENU},
761 {"title", T_TITLE},
762 {"timeout", T_TIMEOUT},
763 {"default", T_DEFAULT},
764 {"prompt", T_PROMPT},
765 {"label", T_LABEL},
766 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000767 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500768 {"localboot", T_LOCALBOOT},
769 {"append", T_APPEND},
770 {"initrd", T_INITRD},
771 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000772 {"fdt", T_FDT},
Rob Herringdf6be7e2012-05-02 18:57:48 -0500773 {"ontimeout", T_ONTIMEOUT,},
Rob Herringdfba6102012-09-17 21:54:05 -0500774 {"ipappend", T_IPAPPEND,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500775 {NULL, T_INVALID}
776};
777
778/*
779 * Since pxe(linux) files don't have a token to identify the start of a
780 * literal, we have to keep track of when we're in a state where a literal is
781 * expected vs when we're in a state a keyword is expected.
782 */
783enum lex_state {
784 L_NORMAL = 0,
785 L_KEYWORD,
786 L_SLITERAL
787};
788
789/*
790 * get_string retrieves a string from *p and stores it as a token in
791 * *t.
792 *
793 * get_string used for scanning both string literals and keywords.
794 *
795 * Characters from *p are copied into t-val until a character equal to
796 * delim is found, or a NUL byte is reached. If delim has the special value of
797 * ' ', any whitespace character will be used as a delimiter.
798 *
799 * If lower is unequal to 0, uppercase characters will be converted to
800 * lowercase in the result. This is useful to make keywords case
801 * insensitive.
802 *
803 * The location of *p is updated to point to the first character after the end
804 * of the token - the ending delimiter.
805 *
806 * On success, the new value of t->val is returned. Memory for t->val is
807 * allocated using malloc and must be free()'d to reclaim it. If insufficient
808 * memory is available, NULL is returned.
809 */
810static char *get_string(char **p, struct token *t, char delim, int lower)
811{
812 char *b, *e;
813 size_t len, i;
814
815 /*
816 * b and e both start at the beginning of the input stream.
817 *
818 * e is incremented until we find the ending delimiter, or a NUL byte
819 * is reached. Then, we take e - b to find the length of the token.
820 */
821 b = e = *p;
822
823 while (*e) {
824 if ((delim == ' ' && isspace(*e)) || delim == *e)
825 break;
826 e++;
827 }
828
829 len = e - b;
830
831 /*
832 * Allocate memory to hold the string, and copy it in, converting
833 * characters to lowercase if lower is != 0.
834 */
835 t->val = malloc(len + 1);
836 if (!t->val)
837 return NULL;
838
839 for (i = 0; i < len; i++, b++) {
840 if (lower)
841 t->val[i] = tolower(*b);
842 else
843 t->val[i] = *b;
844 }
845
846 t->val[len] = '\0';
847
848 /*
849 * Update *p so the caller knows where to continue scanning.
850 */
851 *p = e;
852
853 t->type = T_STRING;
854
855 return t->val;
856}
857
858/*
859 * Populate a keyword token with a type and value.
860 */
861static void get_keyword(struct token *t)
862{
863 int i;
864
865 for (i = 0; keywords[i].val; i++) {
866 if (!strcmp(t->val, keywords[i].val)) {
867 t->type = keywords[i].type;
868 break;
869 }
870 }
871}
872
873/*
874 * Get the next token. We have to keep track of which state we're in to know
875 * if we're looking to get a string literal or a keyword.
876 *
877 * *p is updated to point at the first character after the current token.
878 */
879static void get_token(char **p, struct token *t, enum lex_state state)
880{
881 char *c = *p;
882
883 t->type = T_INVALID;
884
885 /* eat non EOL whitespace */
886 while (isblank(*c))
887 c++;
888
889 /*
890 * eat comments. note that string literals can't begin with #, but
891 * can contain a # after their first character.
892 */
893 if (*c == '#') {
894 while (*c && *c != '\n')
895 c++;
896 }
897
898 if (*c == '\n') {
899 t->type = T_EOL;
900 c++;
901 } else if (*c == '\0') {
902 t->type = T_EOF;
903 c++;
904 } else if (state == L_SLITERAL) {
905 get_string(&c, t, '\n', 0);
906 } else if (state == L_KEYWORD) {
907 /*
908 * when we expect a keyword, we first get the next string
909 * token delimited by whitespace, and then check if it
910 * matches a keyword in our keyword list. if it does, it's
911 * converted to a keyword token of the appropriate type, and
912 * if not, it remains a string token.
913 */
914 get_string(&c, t, ' ', 1);
915 get_keyword(t);
916 }
917
918 *p = c;
919}
920
921/*
922 * Increment *c until we get to the end of the current line, or EOF.
923 */
924static void eol_or_eof(char **c)
925{
926 while (**c && **c != '\n')
927 (*c)++;
928}
929
930/*
931 * All of these parse_* functions share some common behavior.
932 *
933 * They finish with *c pointing after the token they parse, and return 1 on
934 * success, or < 0 on error.
935 */
936
937/*
938 * Parse a string literal and store a pointer it at *dst. String literals
939 * terminate at the end of the line.
940 */
941static int parse_sliteral(char **c, char **dst)
942{
943 struct token t;
944 char *s = *c;
945
946 get_token(c, &t, L_SLITERAL);
947
948 if (t.type != T_STRING) {
949 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
950 return -EINVAL;
951 }
952
953 *dst = t.val;
954
955 return 1;
956}
957
958/*
959 * Parse a base 10 (unsigned) integer and store it at *dst.
960 */
961static int parse_integer(char **c, int *dst)
962{
963 struct token t;
964 char *s = *c;
965 unsigned long temp;
966
967 get_token(c, &t, L_SLITERAL);
968
969 if (t.type != T_STRING) {
970 printf("Expected string: %.*s\n", (int)(*c - s), s);
971 return -EINVAL;
972 }
973
Rob Herringc27d5602012-04-12 13:55:20 -0500974 *dst = simple_strtol(t.val, &temp, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -0500975
976 free(t.val);
977
978 return 1;
979}
980
981static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
982
983/*
984 * Parse an include statement, and retrieve and parse the file it mentions.
985 *
986 * base should point to a location where it's safe to store the file, and
987 * nest_level should indicate how many nested includes have occurred. For this
988 * include, nest_level has already been incremented and doesn't need to be
989 * incremented here.
990 */
991static int handle_include(char **c, char *base,
992 struct pxe_menu *cfg, int nest_level)
993{
994 char *include_path;
995 char *s = *c;
996 int err;
997
998 err = parse_sliteral(c, &include_path);
999
1000 if (err < 0) {
1001 printf("Expected include path: %.*s\n",
1002 (int)(*c - s), s);
1003 return err;
1004 }
1005
1006 err = get_pxe_file(include_path, base);
1007
1008 if (err < 0) {
1009 printf("Couldn't retrieve %s\n", include_path);
1010 return err;
1011 }
1012
1013 return parse_pxefile_top(base, cfg, nest_level);
1014}
1015
1016/*
1017 * Parse lines that begin with 'menu'.
1018 *
1019 * b and nest are provided to handle the 'menu include' case.
1020 *
1021 * b should be the address where the file currently being parsed is stored.
1022 *
1023 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1024 * a file it includes, 3 when parsing a file included by that file, and so on.
1025 */
1026static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
1027{
1028 struct token t;
1029 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +00001030 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -05001031
1032 get_token(c, &t, L_KEYWORD);
1033
1034 switch (t.type) {
1035 case T_TITLE:
1036 err = parse_sliteral(c, &cfg->title);
1037
1038 break;
1039
1040 case T_INCLUDE:
1041 err = handle_include(c, b + strlen(b) + 1, cfg,
1042 nest_level + 1);
1043 break;
1044
1045 default:
1046 printf("Ignoring malformed menu command: %.*s\n",
1047 (int)(*c - s), s);
1048 }
1049
1050 if (err < 0)
1051 return err;
1052
1053 eol_or_eof(c);
1054
1055 return 1;
1056}
1057
1058/*
1059 * Handles parsing a 'menu line' when we're parsing a label.
1060 */
1061static int parse_label_menu(char **c, struct pxe_menu *cfg,
1062 struct pxe_label *label)
1063{
1064 struct token t;
1065 char *s;
1066
1067 s = *c;
1068
1069 get_token(c, &t, L_KEYWORD);
1070
1071 switch (t.type) {
1072 case T_DEFAULT:
Rob Herringdf6be7e2012-05-02 18:57:48 -05001073 if (!cfg->default_label)
1074 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001075
1076 if (!cfg->default_label)
1077 return -ENOMEM;
1078
1079 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001080 case T_LABEL:
1081 parse_sliteral(c, &label->menu);
1082 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001083 default:
1084 printf("Ignoring malformed menu command: %.*s\n",
1085 (int)(*c - s), s);
1086 }
1087
1088 eol_or_eof(c);
1089
1090 return 0;
1091}
1092
1093/*
1094 * Parses a label and adds it to the list of labels for a menu.
1095 *
1096 * A label ends when we either get to the end of a file, or
1097 * get some input we otherwise don't have a handler defined
1098 * for.
1099 *
1100 */
1101static int parse_label(char **c, struct pxe_menu *cfg)
1102{
1103 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001104 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001105 char *s = *c;
1106 struct pxe_label *label;
1107 int err;
Rob Herringc27d5602012-04-12 13:55:20 -05001108 int localboot;
Jason Hobbs06283a62011-08-31 10:37:30 -05001109
1110 label = label_create();
1111 if (!label)
1112 return -ENOMEM;
1113
1114 err = parse_sliteral(c, &label->name);
1115 if (err < 0) {
1116 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1117 label_destroy(label);
1118 return -EINVAL;
1119 }
1120
1121 list_add_tail(&label->list, &cfg->labels);
1122
1123 while (1) {
1124 s = *c;
1125 get_token(c, &t, L_KEYWORD);
1126
1127 err = 0;
1128 switch (t.type) {
1129 case T_MENU:
1130 err = parse_label_menu(c, cfg, label);
1131 break;
1132
1133 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001134 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001135 err = parse_sliteral(c, &label->kernel);
1136 break;
1137
1138 case T_APPEND:
1139 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001140 if (label->initrd)
1141 break;
1142 s = strstr(label->append, "initrd=");
1143 if (!s)
1144 break;
1145 s += 7;
1146 len = (int)(strchr(s, ' ') - s);
1147 label->initrd = malloc(len + 1);
1148 strncpy(label->initrd, s, len);
1149 label->initrd[len] = '\0';
1150
Jason Hobbs06283a62011-08-31 10:37:30 -05001151 break;
1152
1153 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001154 if (!label->initrd)
1155 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001156 break;
1157
Chander Kashyapa6559382012-09-06 19:36:31 +00001158 case T_FDT:
1159 if (!label->fdt)
1160 err = parse_sliteral(c, &label->fdt);
1161 break;
1162
Jason Hobbs06283a62011-08-31 10:37:30 -05001163 case T_LOCALBOOT:
Rob Herringc27d5602012-04-12 13:55:20 -05001164 label->localboot = 1;
1165 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001166 break;
1167
Rob Herringdfba6102012-09-17 21:54:05 -05001168 case T_IPAPPEND:
1169 err = parse_integer(c, &label->ipappend);
1170 break;
1171
Jason Hobbs06283a62011-08-31 10:37:30 -05001172 case T_EOL:
1173 break;
1174
1175 default:
1176 /*
1177 * put the token back! we don't want it - it's the end
1178 * of a label and whatever token this is, it's
1179 * something for the menu level context to handle.
1180 */
1181 *c = s;
1182 return 1;
1183 }
1184
1185 if (err < 0)
1186 return err;
1187 }
1188}
1189
1190/*
1191 * This 16 comes from the limit pxelinux imposes on nested includes.
1192 *
1193 * There is no reason at all we couldn't do more, but some limit helps prevent
1194 * infinite (until crash occurs) recursion if a file tries to include itself.
1195 */
1196#define MAX_NEST_LEVEL 16
1197
1198/*
1199 * Entry point for parsing a menu file. nest_level indicates how many times
1200 * we've nested in includes. It will be 1 for the top level menu file.
1201 *
1202 * Returns 1 on success, < 0 on error.
1203 */
1204static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1205{
1206 struct token t;
1207 char *s, *b, *label_name;
1208 int err;
1209
1210 b = p;
1211
1212 if (nest_level > MAX_NEST_LEVEL) {
1213 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1214 return -EMLINK;
1215 }
1216
1217 while (1) {
1218 s = p;
1219
1220 get_token(&p, &t, L_KEYWORD);
1221
1222 err = 0;
1223 switch (t.type) {
1224 case T_MENU:
Rob Herring2619f512012-04-12 13:33:43 -05001225 cfg->prompt = 1;
Jason Hobbs06283a62011-08-31 10:37:30 -05001226 err = parse_menu(&p, cfg, b, nest_level);
1227 break;
1228
1229 case T_TIMEOUT:
1230 err = parse_integer(&p, &cfg->timeout);
1231 break;
1232
1233 case T_LABEL:
1234 err = parse_label(&p, cfg);
1235 break;
1236
1237 case T_DEFAULT:
Rob Herringdf6be7e2012-05-02 18:57:48 -05001238 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001239 err = parse_sliteral(&p, &label_name);
1240
1241 if (label_name) {
1242 if (cfg->default_label)
1243 free(cfg->default_label);
1244
1245 cfg->default_label = label_name;
1246 }
1247
1248 break;
1249
Rob Herring1e085222012-05-25 10:43:16 +00001250 case T_INCLUDE:
1251 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1252 nest_level + 1);
1253 break;
1254
Jason Hobbs06283a62011-08-31 10:37:30 -05001255 case T_PROMPT:
Rob Herring2619f512012-04-12 13:33:43 -05001256 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001257 break;
1258
1259 case T_EOL:
1260 break;
1261
1262 case T_EOF:
1263 return 1;
1264
1265 default:
1266 printf("Ignoring unknown command: %.*s\n",
1267 (int)(p - s), s);
1268 eol_or_eof(&p);
1269 }
1270
1271 if (err < 0)
1272 return err;
1273 }
1274}
1275
1276/*
1277 * Free the memory used by a pxe_menu and its labels.
1278 */
1279static void destroy_pxe_menu(struct pxe_menu *cfg)
1280{
1281 struct list_head *pos, *n;
1282 struct pxe_label *label;
1283
1284 if (cfg->title)
1285 free(cfg->title);
1286
1287 if (cfg->default_label)
1288 free(cfg->default_label);
1289
1290 list_for_each_safe(pos, n, &cfg->labels) {
1291 label = list_entry(pos, struct pxe_label, list);
1292
1293 label_destroy(label);
1294 }
1295
1296 free(cfg);
1297}
1298
1299/*
1300 * Entry point for parsing a pxe file. This is only used for the top level
1301 * file.
1302 *
1303 * Returns NULL if there is an error, otherwise, returns a pointer to a
1304 * pxe_menu struct populated with the results of parsing the pxe file (and any
1305 * files it includes). The resulting pxe_menu struct can be free()'d by using
1306 * the destroy_pxe_menu() function.
1307 */
1308static struct pxe_menu *parse_pxefile(char *menucfg)
1309{
1310 struct pxe_menu *cfg;
1311
1312 cfg = malloc(sizeof(struct pxe_menu));
1313
1314 if (!cfg)
1315 return NULL;
1316
1317 memset(cfg, 0, sizeof(struct pxe_menu));
1318
1319 INIT_LIST_HEAD(&cfg->labels);
1320
1321 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1322 destroy_pxe_menu(cfg);
1323 return NULL;
1324 }
1325
1326 return cfg;
1327}
1328
1329/*
1330 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1331 * menu code.
1332 */
1333static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1334{
1335 struct pxe_label *label;
1336 struct list_head *pos;
1337 struct menu *m;
1338 int err;
Rob Herring4b3efae2012-04-14 22:23:21 -05001339 int i = 1;
Rob Herring0731b7b2012-05-02 18:52:49 -05001340 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001341
1342 /*
1343 * Create a menu and add items for all the labels.
1344 */
1345 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1346
1347 if (!m)
1348 return NULL;
1349
1350 list_for_each(pos, &cfg->labels) {
1351 label = list_entry(pos, struct pxe_label, list);
1352
Rob Herring4b3efae2012-04-14 22:23:21 -05001353 sprintf(label->num, "%d", i++);
1354 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001355 menu_destroy(m);
1356 return NULL;
1357 }
Rob Herring0731b7b2012-05-02 18:52:49 -05001358 if (cfg->default_label &&
1359 (strcmp(label->name, cfg->default_label) == 0))
1360 default_num = label->num;
1361
Jason Hobbs06283a62011-08-31 10:37:30 -05001362 }
1363
1364 /*
1365 * After we've created items for each label in the menu, set the
1366 * menu's default label if one was specified.
1367 */
Rob Herring0731b7b2012-05-02 18:52:49 -05001368 if (default_num) {
1369 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001370 if (err != 1) {
1371 if (err != -ENOENT) {
1372 menu_destroy(m);
1373 return NULL;
1374 }
1375
1376 printf("Missing default: %s\n", cfg->default_label);
1377 }
1378 }
1379
1380 return m;
1381}
1382
1383/*
1384 * Try to boot any labels we have yet to attempt to boot.
1385 */
1386static void boot_unattempted_labels(struct pxe_menu *cfg)
1387{
1388 struct list_head *pos;
1389 struct pxe_label *label;
1390
1391 list_for_each(pos, &cfg->labels) {
1392 label = list_entry(pos, struct pxe_label, list);
1393
1394 if (!label->attempted)
1395 label_boot(label);
1396 }
1397}
1398
1399/*
1400 * Boot the system as prescribed by a pxe_menu.
1401 *
1402 * Use the menu system to either get the user's choice or the default, based
1403 * on config or user input. If there is no default or user's choice,
1404 * attempted to boot labels in the order they were given in pxe files.
1405 * If the default or user's choice fails to boot, attempt to boot other
1406 * labels in the order they were given in pxe files.
1407 *
1408 * If this function returns, there weren't any labels that successfully
1409 * booted, or the user interrupted the menu selection via ctrl+c.
1410 */
1411static void handle_pxe_menu(struct pxe_menu *cfg)
1412{
1413 void *choice;
1414 struct menu *m;
1415 int err;
1416
1417 m = pxe_menu_to_menu(cfg);
1418 if (!m)
1419 return;
1420
1421 err = menu_get_choice(m, &choice);
1422
1423 menu_destroy(m);
1424
Jason Hobbs6f40f272011-11-07 03:07:15 +00001425 /*
1426 * err == 1 means we got a choice back from menu_get_choice.
1427 *
1428 * err == -ENOENT if the menu was setup to select the default but no
1429 * default was set. in that case, we should continue trying to boot
1430 * labels that haven't been attempted yet.
1431 *
1432 * otherwise, the user interrupted or there was some other error and
1433 * we give up.
1434 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001435
Rob Herringc27d5602012-04-12 13:55:20 -05001436 if (err == 1) {
1437 err = label_boot(choice);
1438 if (!err)
1439 return;
1440 }
Jason Hobbs6f40f272011-11-07 03:07:15 +00001441 else if (err != -ENOENT)
1442 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001443
1444 boot_unattempted_labels(cfg);
1445}
1446
1447/*
1448 * Boots a system using a pxe file
1449 *
1450 * Returns 0 on success, 1 on error.
1451 */
1452static int
1453do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1454{
1455 unsigned long pxefile_addr_r;
1456 struct pxe_menu *cfg;
1457 char *pxefile_addr_str;
1458
Rob Herring669df7e2012-05-25 10:47:39 +00001459 do_getfile = do_get_tftp;
1460
Jason Hobbs06283a62011-08-31 10:37:30 -05001461 if (argc == 1) {
1462 pxefile_addr_str = from_env("pxefile_addr_r");
1463 if (!pxefile_addr_str)
1464 return 1;
1465
1466 } else if (argc == 2) {
1467 pxefile_addr_str = argv[1];
1468 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001469 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001470 }
1471
1472 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1473 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1474 return 1;
1475 }
1476
1477 cfg = parse_pxefile((char *)(pxefile_addr_r));
1478
1479 if (cfg == NULL) {
1480 printf("Error parsing config file\n");
1481 return 1;
1482 }
1483
1484 handle_pxe_menu(cfg);
1485
1486 destroy_pxe_menu(cfg);
1487
1488 return 0;
1489}
1490
1491static cmd_tbl_t cmd_pxe_sub[] = {
1492 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1493 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1494};
1495
1496int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1497{
1498 cmd_tbl_t *cp;
1499
1500 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001501 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001502
1503 /* drop initial "pxe" arg */
1504 argc--;
1505 argv++;
1506
1507 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1508
1509 if (cp)
1510 return cp->cmd(cmdtp, flag, argc, argv);
1511
Simon Glass4c12eeb2011-12-10 08:44:01 +00001512 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001513}
1514
1515U_BOOT_CMD(
1516 pxe, 3, 1, do_pxe,
1517 "commands to get and boot from pxe files",
1518 "get - try to retrieve a pxe file using tftp\npxe "
1519 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1520);
Rob Herring669df7e2012-05-25 10:47:39 +00001521
1522/*
1523 * Boots a system using a local disk syslinux/extlinux file
1524 *
1525 * Returns 0 on success, 1 on error.
1526 */
1527int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1528{
1529 unsigned long pxefile_addr_r;
1530 struct pxe_menu *cfg;
1531 char *pxefile_addr_str;
1532 char *filename;
1533 int prompt = 0;
1534
1535 if (strstr(argv[1], "-p")) {
1536 prompt = 1;
1537 argc--;
1538 argv++;
1539 }
1540
1541 if (argc < 4)
1542 return cmd_usage(cmdtp);
1543
1544 if (argc < 5) {
1545 pxefile_addr_str = from_env("pxefile_addr_r");
1546 if (!pxefile_addr_str)
1547 return 1;
1548 } else {
1549 pxefile_addr_str = argv[4];
1550 }
1551
1552 if (argc < 6)
1553 filename = getenv("bootfile");
1554 else {
1555 filename = argv[5];
1556 setenv("bootfile", filename);
1557 }
1558
1559 if (strstr(argv[3], "ext2"))
1560 do_getfile = do_get_ext2;
1561 else if (strstr(argv[3], "fat"))
1562 do_getfile = do_get_fat;
1563 else {
1564 printf("Invalid filesystem: %s\n", argv[3]);
1565 return 1;
1566 }
1567 fs_argv[1] = argv[1];
1568 fs_argv[2] = argv[2];
1569
1570 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1571 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1572 return 1;
1573 }
1574
1575 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1576 printf("Error reading config file\n");
1577 return 1;
1578 }
1579
1580 cfg = parse_pxefile((char *)(pxefile_addr_r));
1581
1582 if (cfg == NULL) {
1583 printf("Error parsing config file\n");
1584 return 1;
1585 }
1586
1587 if (prompt)
1588 cfg->prompt = 1;
1589
1590 handle_pxe_menu(cfg);
1591
1592 destroy_pxe_menu(cfg);
1593
1594 return 0;
1595}
1596
1597U_BOOT_CMD(
1598 sysboot, 7, 1, do_sysboot,
1599 "command to get and boot from syslinux files",
1600 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1601 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1602 " filesystem on 'dev' on 'interface' to address 'addr'"
1603);