blob: 079d22658184c1917a9e80f9d7057884dd82c366 [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
Jason Hobbs06283a62011-08-31 10:37:30 -050029/*
30 * Like getenv, but prints an error if envvar isn't defined in the
31 * environment. It always returns what getenv does, so it can be used in
32 * place of getenv without changing error handling otherwise.
33 */
Rob Herring23b71942012-12-02 21:00:21 -060034static char *from_env(const char *envvar)
Jason Hobbs06283a62011-08-31 10:37:30 -050035{
36 char *ret;
37
38 ret = getenv(envvar);
39
40 if (!ret)
41 printf("missing environment variable: %s\n", envvar);
42
43 return ret;
44}
45
46/*
47 * Convert an ethaddr from the environment to the format used by pxelinux
48 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
49 * the beginning of the ethernet address to indicate a hardware type of
50 * Ethernet. Also converts uppercase hex characters into lowercase, to match
51 * pxelinux's behavior.
52 *
53 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
54 * environment, or some other value < 0 on error.
55 */
56static int format_mac_pxe(char *outbuf, size_t outbuf_len)
57{
Rob Herringef034c92012-12-02 21:00:20 -060058 uchar ethaddr[6];
Jason Hobbs06283a62011-08-31 10:37:30 -050059
Rob Herringef034c92012-12-02 21:00:20 -060060 if (outbuf_len < 21) {
61 printf("outbuf is too small (%d < 21)\n", outbuf_len);
Jason Hobbs06283a62011-08-31 10:37:30 -050062
63 return -EINVAL;
64 }
65
Rob Herringef034c92012-12-02 21:00:20 -060066 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
67 ethaddr))
68 return -ENOENT;
Jason Hobbs06283a62011-08-31 10:37:30 -050069
Rob Herringef034c92012-12-02 21:00:20 -060070 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
71 ethaddr[0], ethaddr[1], ethaddr[2],
72 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs06283a62011-08-31 10:37:30 -050073
74 return 1;
75}
76
77/*
78 * Returns the directory the file specified in the bootfile env variable is
79 * in. If bootfile isn't defined in the environment, return NULL, which should
80 * be interpreted as "don't prepend anything to paths".
81 */
Rob Herring90ba7d72012-03-28 05:51:36 +000082static int get_bootfile_path(const char *file_path, char *bootfile_path,
83 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -050084{
85 char *bootfile, *last_slash;
Rob Herring90ba7d72012-03-28 05:51:36 +000086 size_t path_len = 0;
87
88 if (file_path[0] == '/')
89 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050090
91 bootfile = from_env("bootfile");
92
Rob Herring90ba7d72012-03-28 05:51:36 +000093 if (!bootfile)
94 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050095
96 last_slash = strrchr(bootfile, '/');
97
Rob Herring90ba7d72012-03-28 05:51:36 +000098 if (last_slash == NULL)
99 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500100
101 path_len = (last_slash - bootfile) + 1;
102
103 if (bootfile_path_size < path_len) {
104 printf("bootfile_path too small. (%d < %d)\n",
105 bootfile_path_size, path_len);
106
107 return -1;
108 }
109
110 strncpy(bootfile_path, bootfile, path_len);
111
Rob Herring90ba7d72012-03-28 05:51:36 +0000112 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500113 bootfile_path[path_len] = '\0';
114
115 return 1;
116}
117
Rob Herring23b71942012-12-02 21:00:21 -0600118static int (*do_getfile)(const char *file_path, char *file_addr);
Rob Herring669df7e2012-05-25 10:47:39 +0000119
Rob Herring23b71942012-12-02 21:00:21 -0600120static int do_get_tftp(const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000121{
122 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
123
124 tftp_argv[1] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600125 tftp_argv[2] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000126
127 if (do_tftpb(NULL, 0, 3, tftp_argv))
128 return -ENOENT;
129
130 return 1;
131}
132
133static char *fs_argv[5];
134
Rob Herring23b71942012-12-02 21:00:21 -0600135static int do_get_ext2(const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000136{
137#ifdef CONFIG_CMD_EXT2
138 fs_argv[0] = "ext2load";
139 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600140 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000141
142 if (!do_ext2load(NULL, 0, 5, fs_argv))
143 return 1;
144#endif
145 return -ENOENT;
146}
147
Rob Herring23b71942012-12-02 21:00:21 -0600148static int do_get_fat(const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000149{
150#ifdef CONFIG_CMD_FAT
151 fs_argv[0] = "fatload";
152 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600153 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000154
155 if (!do_fat_fsload(NULL, 0, 5, fs_argv))
156 return 1;
157#endif
158 return -ENOENT;
159}
160
Jason Hobbs06283a62011-08-31 10:37:30 -0500161/*
162 * As in pxelinux, paths to files referenced from files we retrieve are
163 * relative to the location of bootfile. get_relfile takes such a path and
164 * joins it with the bootfile path to get the full path to the target file. If
165 * the bootfile path is NULL, we use file_path as is.
166 *
167 * Returns 1 for success, or < 0 on error.
168 */
Rob Herring23b71942012-12-02 21:00:21 -0600169static int get_relfile(const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500170{
171 size_t path_len;
172 char relfile[MAX_TFTP_PATH_LEN+1];
173 char addr_buf[10];
Jason Hobbs06283a62011-08-31 10:37:30 -0500174 int err;
175
Rob Herring90ba7d72012-03-28 05:51:36 +0000176 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-08-31 10:37:30 -0500177
178 if (err < 0)
179 return err;
180
181 path_len = strlen(file_path);
182 path_len += strlen(relfile);
183
184 if (path_len > MAX_TFTP_PATH_LEN) {
185 printf("Base path too long (%s%s)\n",
186 relfile,
187 file_path);
188
189 return -ENAMETOOLONG;
190 }
191
192 strcat(relfile, file_path);
193
194 printf("Retrieving file: %s\n", relfile);
195
196 sprintf(addr_buf, "%p", file_addr);
197
Rob Herring669df7e2012-05-25 10:47:39 +0000198 return do_getfile(relfile, addr_buf);
Jason Hobbs06283a62011-08-31 10:37:30 -0500199}
200
201/*
202 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
203 * 'bootfile' was specified in the environment, the path to bootfile will be
204 * prepended to 'file_path' and the resulting path will be used.
205 *
206 * Returns 1 on success, or < 0 for error.
207 */
Rob Herring23b71942012-12-02 21:00:21 -0600208static int get_pxe_file(const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500209{
210 unsigned long config_file_size;
211 char *tftp_filesize;
212 int err;
213
214 err = get_relfile(file_path, file_addr);
215
216 if (err < 0)
217 return err;
218
219 /*
220 * the file comes without a NUL byte at the end, so find out its size
221 * and add the NUL byte.
222 */
223 tftp_filesize = from_env("filesize");
224
225 if (!tftp_filesize)
226 return -ENOENT;
227
228 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
229 return -EINVAL;
230
231 *(char *)(file_addr + config_file_size) = '\0';
232
233 return 1;
234}
235
236#define PXELINUX_DIR "pxelinux.cfg/"
237
238/*
239 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
240 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
241 * from the bootfile path, as described above.
242 *
243 * Returns 1 on success or < 0 on error.
244 */
Rob Herring23b71942012-12-02 21:00:21 -0600245static int get_pxelinux_path(const char *file, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500246{
247 size_t base_len = strlen(PXELINUX_DIR);
248 char path[MAX_TFTP_PATH_LEN+1];
249
250 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
251 printf("path (%s%s) too long, skipping\n",
252 PXELINUX_DIR, file);
253 return -ENAMETOOLONG;
254 }
255
256 sprintf(path, PXELINUX_DIR "%s", file);
257
258 return get_pxe_file(path, pxefile_addr_r);
259}
260
261/*
262 * Looks for a pxe file with a name based on the pxeuuid environment variable.
263 *
264 * Returns 1 on success or < 0 on error.
265 */
266static int pxe_uuid_path(void *pxefile_addr_r)
267{
268 char *uuid_str;
269
270 uuid_str = from_env("pxeuuid");
271
272 if (!uuid_str)
273 return -ENOENT;
274
275 return get_pxelinux_path(uuid_str, pxefile_addr_r);
276}
277
278/*
279 * Looks for a pxe file with a name based on the 'ethaddr' environment
280 * variable.
281 *
282 * Returns 1 on success or < 0 on error.
283 */
284static int pxe_mac_path(void *pxefile_addr_r)
285{
286 char mac_str[21];
287 int err;
288
289 err = format_mac_pxe(mac_str, sizeof(mac_str));
290
291 if (err < 0)
292 return err;
293
294 return get_pxelinux_path(mac_str, pxefile_addr_r);
295}
296
297/*
298 * Looks for pxe files with names based on our IP address. See pxelinux
299 * documentation for details on what these file names look like. We match
300 * that exactly.
301 *
302 * Returns 1 on success or < 0 on error.
303 */
304static int pxe_ipaddr_paths(void *pxefile_addr_r)
305{
306 char ip_addr[9];
307 int mask_pos, err;
308
309 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
310
311 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
312 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
313
314 if (err > 0)
315 return err;
316
317 ip_addr[mask_pos] = '\0';
318 }
319
320 return -ENOENT;
321}
322
323/*
324 * Entry point for the 'pxe get' command.
325 * This Follows pxelinux's rules to download a config file from a tftp server.
326 * The file is stored at the location given by the pxefile_addr_r environment
327 * variable, which must be set.
328 *
329 * UUID comes from pxeuuid env variable, if defined
330 * MAC addr comes from ethaddr env variable, if defined
331 * IP
332 *
333 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
334 *
335 * Returns 0 on success or 1 on error.
336 */
337static int
338do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
339{
340 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000341 unsigned long pxefile_addr_r;
Jason Hobbs06283a62011-08-31 10:37:30 -0500342 int err;
343
Rob Herring669df7e2012-05-25 10:47:39 +0000344 do_getfile = do_get_tftp;
345
Jason Hobbs06283a62011-08-31 10:37:30 -0500346 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000347 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500348
Jason Hobbs06283a62011-08-31 10:37:30 -0500349 pxefile_addr_str = from_env("pxefile_addr_r");
350
351 if (!pxefile_addr_str)
352 return 1;
353
354 err = strict_strtoul(pxefile_addr_str, 16,
355 (unsigned long *)&pxefile_addr_r);
356 if (err < 0)
357 return 1;
358
359 /*
360 * Keep trying paths until we successfully get a file we're looking
361 * for.
362 */
Jason Hobbs834c9382012-03-05 08:12:28 +0000363 if (pxe_uuid_path((void *)pxefile_addr_r) > 0
364 || pxe_mac_path((void *)pxefile_addr_r) > 0
365 || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0
366 || get_pxelinux_path("default", (void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500367
368 printf("Config file found\n");
369
370 return 0;
371 }
372
373 printf("Config file not found\n");
374
375 return 1;
376}
377
378/*
379 * Wrapper to make it easier to store the file at file_path in the location
380 * specified by envaddr_name. file_path will be joined to the bootfile path,
381 * if any is specified.
382 *
383 * Returns 1 on success or < 0 on error.
384 */
Rob Herring23b71942012-12-02 21:00:21 -0600385static int get_relfile_envaddr(const char *file_path, const char *envaddr_name)
Jason Hobbs06283a62011-08-31 10:37:30 -0500386{
Jason Hobbs834c9382012-03-05 08:12:28 +0000387 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500388 char *envaddr;
389
390 envaddr = from_env(envaddr_name);
391
392 if (!envaddr)
393 return -ENOENT;
394
Jason Hobbs834c9382012-03-05 08:12:28 +0000395 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500396 return -EINVAL;
397
Jason Hobbs834c9382012-03-05 08:12:28 +0000398 return get_relfile(file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500399}
400
401/*
402 * A note on the pxe file parser.
403 *
404 * We're parsing files that use syslinux grammar, which has a few quirks.
405 * String literals must be recognized based on context - there is no
406 * quoting or escaping support. There's also nothing to explicitly indicate
407 * when a label section completes. We deal with that by ending a label
408 * section whenever we see a line that doesn't include.
409 *
410 * As with the syslinux family, this same file format could be reused in the
411 * future for non pxe purposes. The only action it takes during parsing that
412 * would throw this off is handling of include files. It assumes we're using
413 * pxe, and does a tftp download of a file listed as an include file in the
414 * middle of the parsing operation. That could be handled by refactoring it to
415 * take a 'include file getter' function.
416 */
417
418/*
419 * Describes a single label given in a pxe file.
420 *
421 * Create these with the 'label_create' function given below.
422 *
423 * name - the name of the menu as given on the 'menu label' line.
424 * kernel - the path to the kernel file to use for this label.
425 * append - kernel command line to use when booting this label
426 * initrd - path to the initrd to use for this label.
427 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
428 * localboot - 1 if this label specified 'localboot', 0 otherwise.
429 * list - lets these form a list, which a pxe_menu struct will hold.
430 */
431struct pxe_label {
Rob Herring32d2ffe2012-12-02 21:00:26 -0600432 char num[4];
Jason Hobbs06283a62011-08-31 10:37:30 -0500433 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000434 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500435 char *kernel;
436 char *append;
437 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000438 char *fdt;
Jason Hobbs06283a62011-08-31 10:37:30 -0500439 int attempted;
440 int localboot;
Rob Herring500f3042012-12-02 21:00:22 -0600441 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500442 struct list_head list;
443};
444
445/*
446 * Describes a pxe menu as given via pxe files.
447 *
448 * title - the name of the menu as given by a 'menu title' line.
449 * default_label - the name of the default label, if any.
450 * timeout - time in tenths of a second to wait for a user key-press before
451 * booting the default label.
452 * prompt - if 0, don't prompt for a choice unless the timeout period is
453 * interrupted. If 1, always prompt for a choice regardless of
454 * timeout.
455 * labels - a list of labels defined for the menu.
456 */
457struct pxe_menu {
458 char *title;
459 char *default_label;
460 int timeout;
461 int prompt;
462 struct list_head labels;
463};
464
465/*
466 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
467 * result must be free()'d to reclaim the memory.
468 *
469 * Returns NULL if malloc fails.
470 */
471static struct pxe_label *label_create(void)
472{
473 struct pxe_label *label;
474
475 label = malloc(sizeof(struct pxe_label));
476
477 if (!label)
478 return NULL;
479
480 memset(label, 0, sizeof(struct pxe_label));
481
482 return label;
483}
484
485/*
486 * Free the memory used by a pxe_label, including that used by its name,
487 * kernel, append and initrd members, if they're non NULL.
488 *
489 * So - be sure to only use dynamically allocated memory for the members of
490 * the pxe_label struct, unless you want to clean it up first. These are
491 * currently only created by the pxe file parsing code.
492 */
493static void label_destroy(struct pxe_label *label)
494{
495 if (label->name)
496 free(label->name);
497
498 if (label->kernel)
499 free(label->kernel);
500
501 if (label->append)
502 free(label->append);
503
504 if (label->initrd)
505 free(label->initrd);
506
Chander Kashyapa6559382012-09-06 19:36:31 +0000507 if (label->fdt)
508 free(label->fdt);
509
Jason Hobbs06283a62011-08-31 10:37:30 -0500510 free(label);
511}
512
513/*
514 * Print a label and its string members if they're defined.
515 *
516 * This is passed as a callback to the menu code for displaying each
517 * menu entry.
518 */
519static void label_print(void *data)
520{
521 struct pxe_label *label = data;
Rob Herring32d2ffe2012-12-02 21:00:26 -0600522 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500523
Rob Herring32d2ffe2012-12-02 21:00:26 -0600524 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500525}
526
527/*
528 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
529 * environment variable is defined. Its contents will be executed as U-boot
530 * command. If the label specified an 'append' line, its contents will be
531 * used to overwrite the contents of the 'bootargs' environment variable prior
532 * to running 'localcmd'.
533 *
534 * Returns 1 on success or < 0 on error.
535 */
536static int label_localboot(struct pxe_label *label)
537{
Simon Glassd51004a2012-03-30 21:30:55 +0000538 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500539
540 localcmd = from_env("localcmd");
541
542 if (!localcmd)
543 return -ENOENT;
544
Jason Hobbs06283a62011-08-31 10:37:30 -0500545 if (label->append)
546 setenv("bootargs", label->append);
547
Simon Glassd51004a2012-03-30 21:30:55 +0000548 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500549
Simon Glassd51004a2012-03-30 21:30:55 +0000550 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500551}
552
553/*
554 * Boot according to the contents of a pxe_label.
555 *
556 * If we can't boot for any reason, we return. A successful boot never
557 * returns.
558 *
559 * The kernel will be stored in the location given by the 'kernel_addr_r'
560 * environment variable.
561 *
562 * If the label specifies an initrd file, it will be stored in the location
563 * given by the 'ramdisk_addr_r' environment variable.
564 *
565 * If the label specifies an 'append' line, its contents will overwrite that
566 * of the 'bootargs' environment variable.
567 */
Rob Herring500f3042012-12-02 21:00:22 -0600568static int label_boot(struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500569{
570 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600571 char initrd_str[22];
Jason Hobbs06283a62011-08-31 10:37:30 -0500572 int bootm_argc = 3;
573
574 label_print(label);
575
576 label->attempted = 1;
577
578 if (label->localboot) {
Rob Herring500f3042012-12-02 21:00:22 -0600579 if (label->localboot_val >= 0)
580 label_localboot(label);
581 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500582 }
583
584 if (label->kernel == NULL) {
585 printf("No kernel given, skipping %s\n",
586 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600587 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500588 }
589
590 if (label->initrd) {
591 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
592 printf("Skipping %s for failure retrieving initrd\n",
593 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600594 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500595 }
596
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600597 bootm_argv[2] = initrd_str;
598 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
599 strcat(bootm_argv[2], ":");
600 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500601 } else {
602 bootm_argv[2] = "-";
603 }
604
605 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
606 printf("Skipping %s for failure retrieving kernel\n",
607 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600608 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500609 }
610
Rob Herring32d2ffe2012-12-02 21:00:26 -0600611 if (label->append) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500612 setenv("bootargs", label->append);
Rob Herring32d2ffe2012-12-02 21:00:26 -0600613 printf("append: %s\n", label->append);
614 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500615
616 bootm_argv[1] = getenv("kernel_addr_r");
617
618 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000619 * fdt usage is optional:
620 * It handles the following scenarios. All scenarios are exclusive
621 *
622 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
623 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
624 * and adjust argc appropriately.
625 *
626 * Scenario 2: If there is an fdt_addr specified, pass it along to
627 * bootm, and adjust argc appropriately.
628 *
629 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500630 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000631 bootm_argv[3] = getenv("fdt_addr_r");
632
633 /* if fdt label is defined then get fdt from server */
634 if (bootm_argv[3] && label->fdt) {
635 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
636 printf("Skipping %s for failure retrieving fdt\n",
637 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600638 return 1;
Chander Kashyapa6559382012-09-06 19:36:31 +0000639 }
640 } else
641 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500642
643 if (bootm_argv[3])
644 bootm_argc = 4;
645
646 do_bootm(NULL, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600647
648#ifdef CONFIG_CMD_BOOTZ
649 /* Try booting a zImage if do_bootm returns */
650 do_bootz(NULL, 0, bootm_argc, bootm_argv);
651#endif
Rob Herring500f3042012-12-02 21:00:22 -0600652 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500653}
654
655/*
656 * Tokens for the pxe file parser.
657 */
658enum token_type {
659 T_EOL,
660 T_STRING,
661 T_EOF,
662 T_MENU,
663 T_TITLE,
664 T_TIMEOUT,
665 T_LABEL,
666 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000667 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500668 T_APPEND,
669 T_INITRD,
670 T_LOCALBOOT,
671 T_DEFAULT,
672 T_PROMPT,
673 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000674 T_FDT,
Jason Hobbs06283a62011-08-31 10:37:30 -0500675 T_INVALID
676};
677
678/*
679 * A token - given by a value and a type.
680 */
681struct token {
682 char *val;
683 enum token_type type;
684};
685
686/*
687 * Keywords recognized.
688 */
689static const struct token keywords[] = {
690 {"menu", T_MENU},
691 {"title", T_TITLE},
692 {"timeout", T_TIMEOUT},
693 {"default", T_DEFAULT},
694 {"prompt", T_PROMPT},
695 {"label", T_LABEL},
696 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000697 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500698 {"localboot", T_LOCALBOOT},
699 {"append", T_APPEND},
700 {"initrd", T_INITRD},
701 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000702 {"fdt", T_FDT},
Jason Hobbs06283a62011-08-31 10:37:30 -0500703 {NULL, T_INVALID}
704};
705
706/*
707 * Since pxe(linux) files don't have a token to identify the start of a
708 * literal, we have to keep track of when we're in a state where a literal is
709 * expected vs when we're in a state a keyword is expected.
710 */
711enum lex_state {
712 L_NORMAL = 0,
713 L_KEYWORD,
714 L_SLITERAL
715};
716
717/*
718 * get_string retrieves a string from *p and stores it as a token in
719 * *t.
720 *
721 * get_string used for scanning both string literals and keywords.
722 *
723 * Characters from *p are copied into t-val until a character equal to
724 * delim is found, or a NUL byte is reached. If delim has the special value of
725 * ' ', any whitespace character will be used as a delimiter.
726 *
727 * If lower is unequal to 0, uppercase characters will be converted to
728 * lowercase in the result. This is useful to make keywords case
729 * insensitive.
730 *
731 * The location of *p is updated to point to the first character after the end
732 * of the token - the ending delimiter.
733 *
734 * On success, the new value of t->val is returned. Memory for t->val is
735 * allocated using malloc and must be free()'d to reclaim it. If insufficient
736 * memory is available, NULL is returned.
737 */
738static char *get_string(char **p, struct token *t, char delim, int lower)
739{
740 char *b, *e;
741 size_t len, i;
742
743 /*
744 * b and e both start at the beginning of the input stream.
745 *
746 * e is incremented until we find the ending delimiter, or a NUL byte
747 * is reached. Then, we take e - b to find the length of the token.
748 */
749 b = e = *p;
750
751 while (*e) {
752 if ((delim == ' ' && isspace(*e)) || delim == *e)
753 break;
754 e++;
755 }
756
757 len = e - b;
758
759 /*
760 * Allocate memory to hold the string, and copy it in, converting
761 * characters to lowercase if lower is != 0.
762 */
763 t->val = malloc(len + 1);
764 if (!t->val)
765 return NULL;
766
767 for (i = 0; i < len; i++, b++) {
768 if (lower)
769 t->val[i] = tolower(*b);
770 else
771 t->val[i] = *b;
772 }
773
774 t->val[len] = '\0';
775
776 /*
777 * Update *p so the caller knows where to continue scanning.
778 */
779 *p = e;
780
781 t->type = T_STRING;
782
783 return t->val;
784}
785
786/*
787 * Populate a keyword token with a type and value.
788 */
789static void get_keyword(struct token *t)
790{
791 int i;
792
793 for (i = 0; keywords[i].val; i++) {
794 if (!strcmp(t->val, keywords[i].val)) {
795 t->type = keywords[i].type;
796 break;
797 }
798 }
799}
800
801/*
802 * Get the next token. We have to keep track of which state we're in to know
803 * if we're looking to get a string literal or a keyword.
804 *
805 * *p is updated to point at the first character after the current token.
806 */
807static void get_token(char **p, struct token *t, enum lex_state state)
808{
809 char *c = *p;
810
811 t->type = T_INVALID;
812
813 /* eat non EOL whitespace */
814 while (isblank(*c))
815 c++;
816
817 /*
818 * eat comments. note that string literals can't begin with #, but
819 * can contain a # after their first character.
820 */
821 if (*c == '#') {
822 while (*c && *c != '\n')
823 c++;
824 }
825
826 if (*c == '\n') {
827 t->type = T_EOL;
828 c++;
829 } else if (*c == '\0') {
830 t->type = T_EOF;
831 c++;
832 } else if (state == L_SLITERAL) {
833 get_string(&c, t, '\n', 0);
834 } else if (state == L_KEYWORD) {
835 /*
836 * when we expect a keyword, we first get the next string
837 * token delimited by whitespace, and then check if it
838 * matches a keyword in our keyword list. if it does, it's
839 * converted to a keyword token of the appropriate type, and
840 * if not, it remains a string token.
841 */
842 get_string(&c, t, ' ', 1);
843 get_keyword(t);
844 }
845
846 *p = c;
847}
848
849/*
850 * Increment *c until we get to the end of the current line, or EOF.
851 */
852static void eol_or_eof(char **c)
853{
854 while (**c && **c != '\n')
855 (*c)++;
856}
857
858/*
859 * All of these parse_* functions share some common behavior.
860 *
861 * They finish with *c pointing after the token they parse, and return 1 on
862 * success, or < 0 on error.
863 */
864
865/*
866 * Parse a string literal and store a pointer it at *dst. String literals
867 * terminate at the end of the line.
868 */
869static int parse_sliteral(char **c, char **dst)
870{
871 struct token t;
872 char *s = *c;
873
874 get_token(c, &t, L_SLITERAL);
875
876 if (t.type != T_STRING) {
877 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
878 return -EINVAL;
879 }
880
881 *dst = t.val;
882
883 return 1;
884}
885
886/*
887 * Parse a base 10 (unsigned) integer and store it at *dst.
888 */
889static int parse_integer(char **c, int *dst)
890{
891 struct token t;
892 char *s = *c;
Jason Hobbs06283a62011-08-31 10:37:30 -0500893
894 get_token(c, &t, L_SLITERAL);
895
896 if (t.type != T_STRING) {
897 printf("Expected string: %.*s\n", (int)(*c - s), s);
898 return -EINVAL;
899 }
900
Rob Herring500f3042012-12-02 21:00:22 -0600901 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -0500902
903 free(t.val);
904
905 return 1;
906}
907
908static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
909
910/*
911 * Parse an include statement, and retrieve and parse the file it mentions.
912 *
913 * base should point to a location where it's safe to store the file, and
914 * nest_level should indicate how many nested includes have occurred. For this
915 * include, nest_level has already been incremented and doesn't need to be
916 * incremented here.
917 */
918static int handle_include(char **c, char *base,
919 struct pxe_menu *cfg, int nest_level)
920{
921 char *include_path;
922 char *s = *c;
923 int err;
924
925 err = parse_sliteral(c, &include_path);
926
927 if (err < 0) {
928 printf("Expected include path: %.*s\n",
929 (int)(*c - s), s);
930 return err;
931 }
932
933 err = get_pxe_file(include_path, base);
934
935 if (err < 0) {
936 printf("Couldn't retrieve %s\n", include_path);
937 return err;
938 }
939
940 return parse_pxefile_top(base, cfg, nest_level);
941}
942
943/*
944 * Parse lines that begin with 'menu'.
945 *
946 * b and nest are provided to handle the 'menu include' case.
947 *
948 * b should be the address where the file currently being parsed is stored.
949 *
950 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
951 * a file it includes, 3 when parsing a file included by that file, and so on.
952 */
953static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
954{
955 struct token t;
956 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000957 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500958
959 get_token(c, &t, L_KEYWORD);
960
961 switch (t.type) {
962 case T_TITLE:
963 err = parse_sliteral(c, &cfg->title);
964
965 break;
966
967 case T_INCLUDE:
968 err = handle_include(c, b + strlen(b) + 1, cfg,
969 nest_level + 1);
970 break;
971
972 default:
973 printf("Ignoring malformed menu command: %.*s\n",
974 (int)(*c - s), s);
975 }
976
977 if (err < 0)
978 return err;
979
980 eol_or_eof(c);
981
982 return 1;
983}
984
985/*
986 * Handles parsing a 'menu line' when we're parsing a label.
987 */
988static int parse_label_menu(char **c, struct pxe_menu *cfg,
989 struct pxe_label *label)
990{
991 struct token t;
992 char *s;
993
994 s = *c;
995
996 get_token(c, &t, L_KEYWORD);
997
998 switch (t.type) {
999 case T_DEFAULT:
1000 if (cfg->default_label)
1001 free(cfg->default_label);
1002
1003 cfg->default_label = strdup(label->name);
1004
1005 if (!cfg->default_label)
1006 return -ENOMEM;
1007
1008 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001009 case T_LABEL:
1010 parse_sliteral(c, &label->menu);
1011 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001012 default:
1013 printf("Ignoring malformed menu command: %.*s\n",
1014 (int)(*c - s), s);
1015 }
1016
1017 eol_or_eof(c);
1018
1019 return 0;
1020}
1021
1022/*
1023 * Parses a label and adds it to the list of labels for a menu.
1024 *
1025 * A label ends when we either get to the end of a file, or
1026 * get some input we otherwise don't have a handler defined
1027 * for.
1028 *
1029 */
1030static int parse_label(char **c, struct pxe_menu *cfg)
1031{
1032 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001033 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001034 char *s = *c;
1035 struct pxe_label *label;
1036 int err;
1037
1038 label = label_create();
1039 if (!label)
1040 return -ENOMEM;
1041
1042 err = parse_sliteral(c, &label->name);
1043 if (err < 0) {
1044 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1045 label_destroy(label);
1046 return -EINVAL;
1047 }
1048
1049 list_add_tail(&label->list, &cfg->labels);
1050
1051 while (1) {
1052 s = *c;
1053 get_token(c, &t, L_KEYWORD);
1054
1055 err = 0;
1056 switch (t.type) {
1057 case T_MENU:
1058 err = parse_label_menu(c, cfg, label);
1059 break;
1060
1061 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001062 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001063 err = parse_sliteral(c, &label->kernel);
1064 break;
1065
1066 case T_APPEND:
1067 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001068 if (label->initrd)
1069 break;
1070 s = strstr(label->append, "initrd=");
1071 if (!s)
1072 break;
1073 s += 7;
1074 len = (int)(strchr(s, ' ') - s);
1075 label->initrd = malloc(len + 1);
1076 strncpy(label->initrd, s, len);
1077 label->initrd[len] = '\0';
1078
Jason Hobbs06283a62011-08-31 10:37:30 -05001079 break;
1080
1081 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001082 if (!label->initrd)
1083 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001084 break;
1085
Chander Kashyapa6559382012-09-06 19:36:31 +00001086 case T_FDT:
1087 if (!label->fdt)
1088 err = parse_sliteral(c, &label->fdt);
1089 break;
1090
Jason Hobbs06283a62011-08-31 10:37:30 -05001091 case T_LOCALBOOT:
Rob Herring500f3042012-12-02 21:00:22 -06001092 label->localboot = 1;
1093 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001094 break;
1095
1096 case T_EOL:
1097 break;
1098
1099 default:
1100 /*
1101 * put the token back! we don't want it - it's the end
1102 * of a label and whatever token this is, it's
1103 * something for the menu level context to handle.
1104 */
1105 *c = s;
1106 return 1;
1107 }
1108
1109 if (err < 0)
1110 return err;
1111 }
1112}
1113
1114/*
1115 * This 16 comes from the limit pxelinux imposes on nested includes.
1116 *
1117 * There is no reason at all we couldn't do more, but some limit helps prevent
1118 * infinite (until crash occurs) recursion if a file tries to include itself.
1119 */
1120#define MAX_NEST_LEVEL 16
1121
1122/*
1123 * Entry point for parsing a menu file. nest_level indicates how many times
1124 * we've nested in includes. It will be 1 for the top level menu file.
1125 *
1126 * Returns 1 on success, < 0 on error.
1127 */
1128static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1129{
1130 struct token t;
1131 char *s, *b, *label_name;
1132 int err;
1133
1134 b = p;
1135
1136 if (nest_level > MAX_NEST_LEVEL) {
1137 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1138 return -EMLINK;
1139 }
1140
1141 while (1) {
1142 s = p;
1143
1144 get_token(&p, &t, L_KEYWORD);
1145
1146 err = 0;
1147 switch (t.type) {
1148 case T_MENU:
Rob Herringe82eeb52012-12-02 21:00:25 -06001149 cfg->prompt = 1;
Jason Hobbs06283a62011-08-31 10:37:30 -05001150 err = parse_menu(&p, cfg, b, nest_level);
1151 break;
1152
1153 case T_TIMEOUT:
1154 err = parse_integer(&p, &cfg->timeout);
1155 break;
1156
1157 case T_LABEL:
1158 err = parse_label(&p, cfg);
1159 break;
1160
1161 case T_DEFAULT:
1162 err = parse_sliteral(&p, &label_name);
1163
1164 if (label_name) {
1165 if (cfg->default_label)
1166 free(cfg->default_label);
1167
1168 cfg->default_label = label_name;
1169 }
1170
1171 break;
1172
Rob Herring1e085222012-05-25 10:43:16 +00001173 case T_INCLUDE:
1174 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1175 nest_level + 1);
1176 break;
1177
Jason Hobbs06283a62011-08-31 10:37:30 -05001178 case T_PROMPT:
Rob Herringe82eeb52012-12-02 21:00:25 -06001179 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001180 break;
1181
1182 case T_EOL:
1183 break;
1184
1185 case T_EOF:
1186 return 1;
1187
1188 default:
1189 printf("Ignoring unknown command: %.*s\n",
1190 (int)(p - s), s);
1191 eol_or_eof(&p);
1192 }
1193
1194 if (err < 0)
1195 return err;
1196 }
1197}
1198
1199/*
1200 * Free the memory used by a pxe_menu and its labels.
1201 */
1202static void destroy_pxe_menu(struct pxe_menu *cfg)
1203{
1204 struct list_head *pos, *n;
1205 struct pxe_label *label;
1206
1207 if (cfg->title)
1208 free(cfg->title);
1209
1210 if (cfg->default_label)
1211 free(cfg->default_label);
1212
1213 list_for_each_safe(pos, n, &cfg->labels) {
1214 label = list_entry(pos, struct pxe_label, list);
1215
1216 label_destroy(label);
1217 }
1218
1219 free(cfg);
1220}
1221
1222/*
1223 * Entry point for parsing a pxe file. This is only used for the top level
1224 * file.
1225 *
1226 * Returns NULL if there is an error, otherwise, returns a pointer to a
1227 * pxe_menu struct populated with the results of parsing the pxe file (and any
1228 * files it includes). The resulting pxe_menu struct can be free()'d by using
1229 * the destroy_pxe_menu() function.
1230 */
1231static struct pxe_menu *parse_pxefile(char *menucfg)
1232{
1233 struct pxe_menu *cfg;
1234
1235 cfg = malloc(sizeof(struct pxe_menu));
1236
1237 if (!cfg)
1238 return NULL;
1239
1240 memset(cfg, 0, sizeof(struct pxe_menu));
1241
1242 INIT_LIST_HEAD(&cfg->labels);
1243
1244 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1245 destroy_pxe_menu(cfg);
1246 return NULL;
1247 }
1248
1249 return cfg;
1250}
1251
1252/*
1253 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1254 * menu code.
1255 */
1256static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1257{
1258 struct pxe_label *label;
1259 struct list_head *pos;
1260 struct menu *m;
1261 int err;
Rob Herring32d2ffe2012-12-02 21:00:26 -06001262 int i = 1;
1263 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001264
1265 /*
1266 * Create a menu and add items for all the labels.
1267 */
Pali Rohárfc9d64f2013-03-23 14:50:40 +00001268 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1269 NULL, NULL);
Jason Hobbs06283a62011-08-31 10:37:30 -05001270
1271 if (!m)
1272 return NULL;
1273
1274 list_for_each(pos, &cfg->labels) {
1275 label = list_entry(pos, struct pxe_label, list);
1276
Rob Herring32d2ffe2012-12-02 21:00:26 -06001277 sprintf(label->num, "%d", i++);
1278 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001279 menu_destroy(m);
1280 return NULL;
1281 }
Rob Herring32d2ffe2012-12-02 21:00:26 -06001282 if (cfg->default_label &&
1283 (strcmp(label->name, cfg->default_label) == 0))
1284 default_num = label->num;
1285
Jason Hobbs06283a62011-08-31 10:37:30 -05001286 }
1287
1288 /*
1289 * After we've created items for each label in the menu, set the
1290 * menu's default label if one was specified.
1291 */
Rob Herring32d2ffe2012-12-02 21:00:26 -06001292 if (default_num) {
1293 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001294 if (err != 1) {
1295 if (err != -ENOENT) {
1296 menu_destroy(m);
1297 return NULL;
1298 }
1299
1300 printf("Missing default: %s\n", cfg->default_label);
1301 }
1302 }
1303
1304 return m;
1305}
1306
1307/*
1308 * Try to boot any labels we have yet to attempt to boot.
1309 */
1310static void boot_unattempted_labels(struct pxe_menu *cfg)
1311{
1312 struct list_head *pos;
1313 struct pxe_label *label;
1314
1315 list_for_each(pos, &cfg->labels) {
1316 label = list_entry(pos, struct pxe_label, list);
1317
1318 if (!label->attempted)
1319 label_boot(label);
1320 }
1321}
1322
1323/*
1324 * Boot the system as prescribed by a pxe_menu.
1325 *
1326 * Use the menu system to either get the user's choice or the default, based
1327 * on config or user input. If there is no default or user's choice,
1328 * attempted to boot labels in the order they were given in pxe files.
1329 * If the default or user's choice fails to boot, attempt to boot other
1330 * labels in the order they were given in pxe files.
1331 *
1332 * If this function returns, there weren't any labels that successfully
1333 * booted, or the user interrupted the menu selection via ctrl+c.
1334 */
1335static void handle_pxe_menu(struct pxe_menu *cfg)
1336{
1337 void *choice;
1338 struct menu *m;
1339 int err;
1340
1341 m = pxe_menu_to_menu(cfg);
1342 if (!m)
1343 return;
1344
1345 err = menu_get_choice(m, &choice);
1346
1347 menu_destroy(m);
1348
Jason Hobbs6f40f272011-11-07 03:07:15 +00001349 /*
1350 * err == 1 means we got a choice back from menu_get_choice.
1351 *
1352 * err == -ENOENT if the menu was setup to select the default but no
1353 * default was set. in that case, we should continue trying to boot
1354 * labels that haven't been attempted yet.
1355 *
1356 * otherwise, the user interrupted or there was some other error and
1357 * we give up.
1358 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001359
Rob Herring500f3042012-12-02 21:00:22 -06001360 if (err == 1) {
1361 err = label_boot(choice);
1362 if (!err)
1363 return;
1364 } else if (err != -ENOENT) {
Jason Hobbs6f40f272011-11-07 03:07:15 +00001365 return;
Rob Herring500f3042012-12-02 21:00:22 -06001366 }
Jason Hobbs06283a62011-08-31 10:37:30 -05001367
1368 boot_unattempted_labels(cfg);
1369}
1370
1371/*
1372 * Boots a system using a pxe file
1373 *
1374 * Returns 0 on success, 1 on error.
1375 */
1376static int
1377do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1378{
1379 unsigned long pxefile_addr_r;
1380 struct pxe_menu *cfg;
1381 char *pxefile_addr_str;
1382
Rob Herring669df7e2012-05-25 10:47:39 +00001383 do_getfile = do_get_tftp;
1384
Jason Hobbs06283a62011-08-31 10:37:30 -05001385 if (argc == 1) {
1386 pxefile_addr_str = from_env("pxefile_addr_r");
1387 if (!pxefile_addr_str)
1388 return 1;
1389
1390 } else if (argc == 2) {
1391 pxefile_addr_str = argv[1];
1392 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001393 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001394 }
1395
1396 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1397 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1398 return 1;
1399 }
1400
1401 cfg = parse_pxefile((char *)(pxefile_addr_r));
1402
1403 if (cfg == NULL) {
1404 printf("Error parsing config file\n");
1405 return 1;
1406 }
1407
1408 handle_pxe_menu(cfg);
1409
1410 destroy_pxe_menu(cfg);
1411
1412 return 0;
1413}
1414
1415static cmd_tbl_t cmd_pxe_sub[] = {
1416 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1417 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1418};
1419
1420int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1421{
1422 cmd_tbl_t *cp;
1423
1424 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001425 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001426
1427 /* drop initial "pxe" arg */
1428 argc--;
1429 argv++;
1430
1431 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1432
1433 if (cp)
1434 return cp->cmd(cmdtp, flag, argc, argv);
1435
Simon Glass4c12eeb2011-12-10 08:44:01 +00001436 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001437}
1438
1439U_BOOT_CMD(
1440 pxe, 3, 1, do_pxe,
1441 "commands to get and boot from pxe files",
1442 "get - try to retrieve a pxe file using tftp\npxe "
1443 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1444);
Rob Herring669df7e2012-05-25 10:47:39 +00001445
1446/*
1447 * Boots a system using a local disk syslinux/extlinux file
1448 *
1449 * Returns 0 on success, 1 on error.
1450 */
1451int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1452{
1453 unsigned long pxefile_addr_r;
1454 struct pxe_menu *cfg;
1455 char *pxefile_addr_str;
1456 char *filename;
1457 int prompt = 0;
1458
1459 if (strstr(argv[1], "-p")) {
1460 prompt = 1;
1461 argc--;
1462 argv++;
1463 }
1464
1465 if (argc < 4)
1466 return cmd_usage(cmdtp);
1467
1468 if (argc < 5) {
1469 pxefile_addr_str = from_env("pxefile_addr_r");
1470 if (!pxefile_addr_str)
1471 return 1;
1472 } else {
1473 pxefile_addr_str = argv[4];
1474 }
1475
1476 if (argc < 6)
1477 filename = getenv("bootfile");
1478 else {
1479 filename = argv[5];
1480 setenv("bootfile", filename);
1481 }
1482
1483 if (strstr(argv[3], "ext2"))
1484 do_getfile = do_get_ext2;
1485 else if (strstr(argv[3], "fat"))
1486 do_getfile = do_get_fat;
1487 else {
1488 printf("Invalid filesystem: %s\n", argv[3]);
1489 return 1;
1490 }
1491 fs_argv[1] = argv[1];
1492 fs_argv[2] = argv[2];
1493
1494 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1495 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1496 return 1;
1497 }
1498
1499 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1500 printf("Error reading config file\n");
1501 return 1;
1502 }
1503
1504 cfg = parse_pxefile((char *)(pxefile_addr_r));
1505
1506 if (cfg == NULL) {
1507 printf("Error parsing config file\n");
1508 return 1;
1509 }
1510
1511 if (prompt)
1512 cfg->prompt = 1;
1513
1514 handle_pxe_menu(cfg);
1515
1516 destroy_pxe_menu(cfg);
1517
1518 return 0;
1519}
1520
1521U_BOOT_CMD(
1522 sysboot, 7, 1, do_sysboot,
1523 "command to get and boot from syslinux files",
1524 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1525 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1526 " filesystem on 'dev' on 'interface' to address 'addr'"
1527);