blob: c27ec354cc597ba808f8667943623adee23b75dd [file] [log] [blame]
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0e3a5932011-08-31 10:37:30 -05005 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006
Jason Hobbs0e3a5932011-08-31 10:37:30 -05007#include <common.h>
8#include <command.h>
9#include <malloc.h>
10#include <linux/string.h>
11#include <linux/ctype.h>
12#include <errno.h>
13#include <linux/list.h>
14
15#include "menu.h"
16
17#define MAX_TFTP_PATH_LEN 127
18
Rob Herringe26e8d62012-12-02 21:00:28 -060019const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050020#ifdef CONFIG_SYS_SOC
Rob Herringe26e8d62012-12-02 21:00:28 -060021 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050022#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060023 "default-" CONFIG_SYS_ARCH,
24 "default",
25 NULL
26};
27
Rob Herring759456e2013-10-18 13:04:42 -050028static bool is_pxe;
29
Jason Hobbs0e3a5932011-08-31 10:37:30 -050030/*
31 * Like getenv, but prints an error if envvar isn't defined in the
32 * environment. It always returns what getenv does, so it can be used in
33 * place of getenv without changing error handling otherwise.
34 */
Rob Herring824901c2012-12-02 21:00:21 -060035static char *from_env(const char *envvar)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050036{
37 char *ret;
38
39 ret = getenv(envvar);
40
41 if (!ret)
42 printf("missing environment variable: %s\n", envvar);
43
44 return ret;
45}
46
47/*
48 * Convert an ethaddr from the environment to the format used by pxelinux
49 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
50 * the beginning of the ethernet address to indicate a hardware type of
51 * Ethernet. Also converts uppercase hex characters into lowercase, to match
52 * pxelinux's behavior.
53 *
54 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
55 * environment, or some other value < 0 on error.
56 */
57static int format_mac_pxe(char *outbuf, size_t outbuf_len)
58{
Rob Herringdd248b32012-12-02 21:00:20 -060059 uchar ethaddr[6];
Jason Hobbs0e3a5932011-08-31 10:37:30 -050060
Rob Herringdd248b32012-12-02 21:00:20 -060061 if (outbuf_len < 21) {
David Feng4ec50292013-12-14 11:47:30 +080062 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050063
64 return -EINVAL;
65 }
66
Rob Herringdd248b32012-12-02 21:00:20 -060067 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
68 ethaddr))
69 return -ENOENT;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050070
Rob Herringdd248b32012-12-02 21:00:20 -060071 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
72 ethaddr[0], ethaddr[1], ethaddr[2],
73 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050074
75 return 1;
76}
77
78/*
79 * Returns the directory the file specified in the bootfile env variable is
80 * in. If bootfile isn't defined in the environment, return NULL, which should
81 * be interpreted as "don't prepend anything to paths".
82 */
Rob Herringdc410cd2012-03-28 05:51:36 +000083static int get_bootfile_path(const char *file_path, char *bootfile_path,
84 size_t bootfile_path_size)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050085{
86 char *bootfile, *last_slash;
Rob Herringdc410cd2012-03-28 05:51:36 +000087 size_t path_len = 0;
88
Rob Herring759456e2013-10-18 13:04:42 -050089 /* Only syslinux allows absolute paths */
90 if (file_path[0] == '/' && !is_pxe)
Rob Herringdc410cd2012-03-28 05:51:36 +000091 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050092
93 bootfile = from_env("bootfile");
94
Rob Herringdc410cd2012-03-28 05:51:36 +000095 if (!bootfile)
96 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050097
98 last_slash = strrchr(bootfile, '/');
99
Rob Herringdc410cd2012-03-28 05:51:36 +0000100 if (last_slash == NULL)
101 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500102
103 path_len = (last_slash - bootfile) + 1;
104
105 if (bootfile_path_size < path_len) {
David Feng4ec50292013-12-14 11:47:30 +0800106 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500107 bootfile_path_size, path_len);
108
109 return -1;
110 }
111
112 strncpy(bootfile_path, bootfile, path_len);
113
Rob Herringdc410cd2012-03-28 05:51:36 +0000114 ret:
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500115 bootfile_path[path_len] = '\0';
116
117 return 1;
118}
119
Steven Falco7de0fa82013-10-07 09:51:48 -0400120static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herringeee675f2012-05-25 10:47:39 +0000121
Steven Falco7de0fa82013-10-07 09:51:48 -0400122static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000123{
124 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
125
126 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600127 tftp_argv[2] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000128
Steven Falco7de0fa82013-10-07 09:51:48 -0400129 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000130 return -ENOENT;
131
132 return 1;
133}
134
135static char *fs_argv[5];
136
Steven Falco7de0fa82013-10-07 09:51:48 -0400137static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000138{
139#ifdef CONFIG_CMD_EXT2
140 fs_argv[0] = "ext2load";
141 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600142 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000143
Steven Falco7de0fa82013-10-07 09:51:48 -0400144 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000145 return 1;
146#endif
147 return -ENOENT;
148}
149
Steven Falco7de0fa82013-10-07 09:51:48 -0400150static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000151{
152#ifdef CONFIG_CMD_FAT
153 fs_argv[0] = "fatload";
154 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600155 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000156
Steven Falco7de0fa82013-10-07 09:51:48 -0400157 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000158 return 1;
159#endif
160 return -ENOENT;
161}
162
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500163/*
164 * As in pxelinux, paths to files referenced from files we retrieve are
165 * relative to the location of bootfile. get_relfile takes such a path and
166 * joins it with the bootfile path to get the full path to the target file. If
167 * the bootfile path is NULL, we use file_path as is.
168 *
169 * Returns 1 for success, or < 0 on error.
170 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400171static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500172{
173 size_t path_len;
174 char relfile[MAX_TFTP_PATH_LEN+1];
175 char addr_buf[10];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500176 int err;
177
Rob Herringdc410cd2012-03-28 05:51:36 +0000178 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500179
180 if (err < 0)
181 return err;
182
183 path_len = strlen(file_path);
184 path_len += strlen(relfile);
185
186 if (path_len > MAX_TFTP_PATH_LEN) {
187 printf("Base path too long (%s%s)\n",
188 relfile,
189 file_path);
190
191 return -ENAMETOOLONG;
192 }
193
194 strcat(relfile, file_path);
195
196 printf("Retrieving file: %s\n", relfile);
197
198 sprintf(addr_buf, "%p", file_addr);
199
Steven Falco7de0fa82013-10-07 09:51:48 -0400200 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500201}
202
203/*
204 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
205 * 'bootfile' was specified in the environment, the path to bootfile will be
206 * prepended to 'file_path' and the resulting path will be used.
207 *
208 * Returns 1 on success, or < 0 for error.
209 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400210static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500211{
212 unsigned long config_file_size;
213 char *tftp_filesize;
214 int err;
215
Steven Falco7de0fa82013-10-07 09:51:48 -0400216 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500217
218 if (err < 0)
219 return err;
220
221 /*
222 * the file comes without a NUL byte at the end, so find out its size
223 * and add the NUL byte.
224 */
225 tftp_filesize = from_env("filesize");
226
227 if (!tftp_filesize)
228 return -ENOENT;
229
230 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
231 return -EINVAL;
232
233 *(char *)(file_addr + config_file_size) = '\0';
234
235 return 1;
236}
237
238#define PXELINUX_DIR "pxelinux.cfg/"
239
240/*
241 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
242 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
243 * from the bootfile path, as described above.
244 *
245 * Returns 1 on success or < 0 on error.
246 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400247static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500248{
249 size_t base_len = strlen(PXELINUX_DIR);
250 char path[MAX_TFTP_PATH_LEN+1];
251
252 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
253 printf("path (%s%s) too long, skipping\n",
254 PXELINUX_DIR, file);
255 return -ENAMETOOLONG;
256 }
257
258 sprintf(path, PXELINUX_DIR "%s", file);
259
Steven Falco7de0fa82013-10-07 09:51:48 -0400260 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500261}
262
263/*
264 * Looks for a pxe file with a name based on the pxeuuid environment variable.
265 *
266 * Returns 1 on success or < 0 on error.
267 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400268static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500269{
270 char *uuid_str;
271
272 uuid_str = from_env("pxeuuid");
273
274 if (!uuid_str)
275 return -ENOENT;
276
Steven Falco7de0fa82013-10-07 09:51:48 -0400277 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500278}
279
280/*
281 * Looks for a pxe file with a name based on the 'ethaddr' environment
282 * variable.
283 *
284 * Returns 1 on success or < 0 on error.
285 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400286static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500287{
288 char mac_str[21];
289 int err;
290
291 err = format_mac_pxe(mac_str, sizeof(mac_str));
292
293 if (err < 0)
294 return err;
295
Steven Falco7de0fa82013-10-07 09:51:48 -0400296 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500297}
298
299/*
300 * Looks for pxe files with names based on our IP address. See pxelinux
301 * documentation for details on what these file names look like. We match
302 * that exactly.
303 *
304 * Returns 1 on success or < 0 on error.
305 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400306static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500307{
308 char ip_addr[9];
309 int mask_pos, err;
310
311 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
312
313 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400314 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500315
316 if (err > 0)
317 return err;
318
319 ip_addr[mask_pos] = '\0';
320 }
321
322 return -ENOENT;
323}
324
325/*
326 * Entry point for the 'pxe get' command.
327 * This Follows pxelinux's rules to download a config file from a tftp server.
328 * The file is stored at the location given by the pxefile_addr_r environment
329 * variable, which must be set.
330 *
331 * UUID comes from pxeuuid env variable, if defined
332 * MAC addr comes from ethaddr env variable, if defined
333 * IP
334 *
335 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
336 *
337 * Returns 0 on success or 1 on error.
338 */
339static int
340do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
341{
342 char *pxefile_addr_str;
Jason Hobbs9135c792012-03-05 08:12:28 +0000343 unsigned long pxefile_addr_r;
Rob Herringe26e8d62012-12-02 21:00:28 -0600344 int err, i = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500345
Rob Herringeee675f2012-05-25 10:47:39 +0000346 do_getfile = do_get_tftp;
347
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500348 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000349 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500350
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500351 pxefile_addr_str = from_env("pxefile_addr_r");
352
353 if (!pxefile_addr_str)
354 return 1;
355
356 err = strict_strtoul(pxefile_addr_str, 16,
357 (unsigned long *)&pxefile_addr_r);
358 if (err < 0)
359 return 1;
360
361 /*
362 * Keep trying paths until we successfully get a file we're looking
363 * for.
364 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400365 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
366 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
367 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500368 printf("Config file found\n");
369
370 return 0;
371 }
372
Rob Herringe26e8d62012-12-02 21:00:28 -0600373 while (pxe_default_paths[i]) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400374 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herringe26e8d62012-12-02 21:00:28 -0600375 (void *)pxefile_addr_r) > 0) {
376 printf("Config file found\n");
377 return 0;
378 }
379 i++;
380 }
381
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500382 printf("Config file not found\n");
383
384 return 1;
385}
386
387/*
388 * Wrapper to make it easier to store the file at file_path in the location
389 * specified by envaddr_name. file_path will be joined to the bootfile path,
390 * if any is specified.
391 *
392 * Returns 1 on success or < 0 on error.
393 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400394static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500395{
Jason Hobbs9135c792012-03-05 08:12:28 +0000396 unsigned long file_addr;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500397 char *envaddr;
398
399 envaddr = from_env(envaddr_name);
400
401 if (!envaddr)
402 return -ENOENT;
403
Jason Hobbs9135c792012-03-05 08:12:28 +0000404 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500405 return -EINVAL;
406
Steven Falco7de0fa82013-10-07 09:51:48 -0400407 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500408}
409
410/*
411 * A note on the pxe file parser.
412 *
413 * We're parsing files that use syslinux grammar, which has a few quirks.
414 * String literals must be recognized based on context - there is no
415 * quoting or escaping support. There's also nothing to explicitly indicate
416 * when a label section completes. We deal with that by ending a label
417 * section whenever we see a line that doesn't include.
418 *
419 * As with the syslinux family, this same file format could be reused in the
420 * future for non pxe purposes. The only action it takes during parsing that
421 * would throw this off is handling of include files. It assumes we're using
422 * pxe, and does a tftp download of a file listed as an include file in the
423 * middle of the parsing operation. That could be handled by refactoring it to
424 * take a 'include file getter' function.
425 */
426
427/*
428 * Describes a single label given in a pxe file.
429 *
430 * Create these with the 'label_create' function given below.
431 *
432 * name - the name of the menu as given on the 'menu label' line.
433 * kernel - the path to the kernel file to use for this label.
434 * append - kernel command line to use when booting this label
435 * initrd - path to the initrd to use for this label.
436 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
437 * localboot - 1 if this label specified 'localboot', 0 otherwise.
438 * list - lets these form a list, which a pxe_menu struct will hold.
439 */
440struct pxe_label {
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600441 char num[4];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500442 char *name;
Rob Herringa6b74172012-03-28 05:51:34 +0000443 char *menu;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500444 char *kernel;
445 char *append;
446 char *initrd;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000447 char *fdt;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600448 int ipappend;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500449 int attempted;
450 int localboot;
Rob Herring58d7f432012-12-02 21:00:22 -0600451 int localboot_val;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500452 struct list_head list;
453};
454
455/*
456 * Describes a pxe menu as given via pxe files.
457 *
458 * title - the name of the menu as given by a 'menu title' line.
459 * default_label - the name of the default label, if any.
460 * timeout - time in tenths of a second to wait for a user key-press before
461 * booting the default label.
462 * prompt - if 0, don't prompt for a choice unless the timeout period is
463 * interrupted. If 1, always prompt for a choice regardless of
464 * timeout.
465 * labels - a list of labels defined for the menu.
466 */
467struct pxe_menu {
468 char *title;
469 char *default_label;
470 int timeout;
471 int prompt;
472 struct list_head labels;
473};
474
475/*
476 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
477 * result must be free()'d to reclaim the memory.
478 *
479 * Returns NULL if malloc fails.
480 */
481static struct pxe_label *label_create(void)
482{
483 struct pxe_label *label;
484
485 label = malloc(sizeof(struct pxe_label));
486
487 if (!label)
488 return NULL;
489
490 memset(label, 0, sizeof(struct pxe_label));
491
492 return label;
493}
494
495/*
496 * Free the memory used by a pxe_label, including that used by its name,
497 * kernel, append and initrd members, if they're non NULL.
498 *
499 * So - be sure to only use dynamically allocated memory for the members of
500 * the pxe_label struct, unless you want to clean it up first. These are
501 * currently only created by the pxe file parsing code.
502 */
503static void label_destroy(struct pxe_label *label)
504{
505 if (label->name)
506 free(label->name);
507
508 if (label->kernel)
509 free(label->kernel);
510
511 if (label->append)
512 free(label->append);
513
514 if (label->initrd)
515 free(label->initrd);
516
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000517 if (label->fdt)
518 free(label->fdt);
519
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500520 free(label);
521}
522
523/*
524 * Print a label and its string members if they're defined.
525 *
526 * This is passed as a callback to the menu code for displaying each
527 * menu entry.
528 */
529static void label_print(void *data)
530{
531 struct pxe_label *label = data;
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600532 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500533
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600534 printf("%s:\t%s\n", label->num, c);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500535}
536
537/*
538 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
539 * environment variable is defined. Its contents will be executed as U-boot
540 * command. If the label specified an 'append' line, its contents will be
541 * used to overwrite the contents of the 'bootargs' environment variable prior
542 * to running 'localcmd'.
543 *
544 * Returns 1 on success or < 0 on error.
545 */
546static int label_localboot(struct pxe_label *label)
547{
Simon Glassa6642e02012-03-30 21:30:55 +0000548 char *localcmd;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500549
550 localcmd = from_env("localcmd");
551
552 if (!localcmd)
553 return -ENOENT;
554
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500555 if (label->append)
556 setenv("bootargs", label->append);
557
Simon Glassa6642e02012-03-30 21:30:55 +0000558 debug("running: %s\n", localcmd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500559
Simon Glassa6642e02012-03-30 21:30:55 +0000560 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500561}
562
563/*
564 * Boot according to the contents of a pxe_label.
565 *
566 * If we can't boot for any reason, we return. A successful boot never
567 * returns.
568 *
569 * The kernel will be stored in the location given by the 'kernel_addr_r'
570 * environment variable.
571 *
572 * If the label specifies an initrd file, it will be stored in the location
573 * given by the 'ramdisk_addr_r' environment variable.
574 *
575 * If the label specifies an 'append' line, its contents will overwrite that
576 * of the 'bootargs' environment variable.
577 */
Tom Rini5b5b1762013-09-24 09:05:08 -0400578static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500579{
580 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringc0356dc2012-12-03 13:17:21 -0600581 char initrd_str[22];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600582 char mac_str[29] = "";
583 char ip_str[68] = "";
584 char *bootargs;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500585 int bootm_argc = 3;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600586 int len = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500587
588 label_print(label);
589
590 label->attempted = 1;
591
592 if (label->localboot) {
Rob Herring58d7f432012-12-02 21:00:22 -0600593 if (label->localboot_val >= 0)
594 label_localboot(label);
595 return 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500596 }
597
598 if (label->kernel == NULL) {
599 printf("No kernel given, skipping %s\n",
600 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600601 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500602 }
603
604 if (label->initrd) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400605 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500606 printf("Skipping %s for failure retrieving initrd\n",
607 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600608 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500609 }
610
Rob Herringc0356dc2012-12-03 13:17:21 -0600611 bootm_argv[2] = initrd_str;
612 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
613 strcat(bootm_argv[2], ":");
614 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500615 } else {
616 bootm_argv[2] = "-";
617 }
618
Steven Falco7de0fa82013-10-07 09:51:48 -0400619 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500620 printf("Skipping %s for failure retrieving kernel\n",
621 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600622 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500623 }
624
Rob Herring5a3d8be2012-12-02 21:00:29 -0600625 if (label->ipappend & 0x1) {
626 sprintf(ip_str, " ip=%s:%s:%s:%s",
627 getenv("ipaddr"), getenv("serverip"),
628 getenv("gatewayip"), getenv("netmask"));
629 len += strlen(ip_str);
630 }
631
632 if (label->ipappend & 0x2) {
633 int err;
634 strcpy(mac_str, " BOOTIF=");
635 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
636 if (err < 0)
637 mac_str[0] = '\0';
638 len += strlen(mac_str);
639 }
640
641 if (label->append)
642 len += strlen(label->append);
643
644 if (len) {
645 bootargs = malloc(len + 1);
646 if (!bootargs)
647 return 1;
648 bootargs[0] = '\0';
649 if (label->append)
650 strcpy(bootargs, label->append);
651 strcat(bootargs, ip_str);
652 strcat(bootargs, mac_str);
653
654 setenv("bootargs", bootargs);
655 printf("append: %s\n", bootargs);
656
657 free(bootargs);
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600658 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500659
660 bootm_argv[1] = getenv("kernel_addr_r");
661
662 /*
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000663 * fdt usage is optional:
664 * It handles the following scenarios. All scenarios are exclusive
665 *
666 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
667 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
668 * and adjust argc appropriately.
669 *
670 * Scenario 2: If there is an fdt_addr specified, pass it along to
671 * bootm, and adjust argc appropriately.
672 *
673 * Scenario 3: fdt blob is not available.
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500674 */
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000675 bootm_argv[3] = getenv("fdt_addr_r");
676
677 /* if fdt label is defined then get fdt from server */
678 if (bootm_argv[3] && label->fdt) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400679 if (get_relfile_envaddr(cmdtp, label->fdt, "fdt_addr_r") < 0) {
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000680 printf("Skipping %s for failure retrieving fdt\n",
681 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600682 return 1;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000683 }
684 } else
685 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500686
687 if (bootm_argv[3])
688 bootm_argc = 4;
689
Tom Rini5b5b1762013-09-24 09:05:08 -0400690 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600691
692#ifdef CONFIG_CMD_BOOTZ
693 /* Try booting a zImage if do_bootm returns */
Tom Rini5b5b1762013-09-24 09:05:08 -0400694 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600695#endif
Rob Herring58d7f432012-12-02 21:00:22 -0600696 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500697}
698
699/*
700 * Tokens for the pxe file parser.
701 */
702enum token_type {
703 T_EOL,
704 T_STRING,
705 T_EOF,
706 T_MENU,
707 T_TITLE,
708 T_TIMEOUT,
709 T_LABEL,
710 T_KERNEL,
Rob Herringc93f74a2012-03-28 05:51:35 +0000711 T_LINUX,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500712 T_APPEND,
713 T_INITRD,
714 T_LOCALBOOT,
715 T_DEFAULT,
716 T_PROMPT,
717 T_INCLUDE,
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000718 T_FDT,
Rob Herringac995832012-12-02 21:00:27 -0600719 T_ONTIMEOUT,
Rob Herring5a3d8be2012-12-02 21:00:29 -0600720 T_IPAPPEND,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500721 T_INVALID
722};
723
724/*
725 * A token - given by a value and a type.
726 */
727struct token {
728 char *val;
729 enum token_type type;
730};
731
732/*
733 * Keywords recognized.
734 */
735static const struct token keywords[] = {
736 {"menu", T_MENU},
737 {"title", T_TITLE},
738 {"timeout", T_TIMEOUT},
739 {"default", T_DEFAULT},
740 {"prompt", T_PROMPT},
741 {"label", T_LABEL},
742 {"kernel", T_KERNEL},
Rob Herringc93f74a2012-03-28 05:51:35 +0000743 {"linux", T_LINUX},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500744 {"localboot", T_LOCALBOOT},
745 {"append", T_APPEND},
746 {"initrd", T_INITRD},
747 {"include", T_INCLUDE},
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000748 {"fdt", T_FDT},
Rob Herringac995832012-12-02 21:00:27 -0600749 {"ontimeout", T_ONTIMEOUT,},
Rob Herring5a3d8be2012-12-02 21:00:29 -0600750 {"ipappend", T_IPAPPEND,},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500751 {NULL, T_INVALID}
752};
753
754/*
755 * Since pxe(linux) files don't have a token to identify the start of a
756 * literal, we have to keep track of when we're in a state where a literal is
757 * expected vs when we're in a state a keyword is expected.
758 */
759enum lex_state {
760 L_NORMAL = 0,
761 L_KEYWORD,
762 L_SLITERAL
763};
764
765/*
766 * get_string retrieves a string from *p and stores it as a token in
767 * *t.
768 *
769 * get_string used for scanning both string literals and keywords.
770 *
771 * Characters from *p are copied into t-val until a character equal to
772 * delim is found, or a NUL byte is reached. If delim has the special value of
773 * ' ', any whitespace character will be used as a delimiter.
774 *
775 * If lower is unequal to 0, uppercase characters will be converted to
776 * lowercase in the result. This is useful to make keywords case
777 * insensitive.
778 *
779 * The location of *p is updated to point to the first character after the end
780 * of the token - the ending delimiter.
781 *
782 * On success, the new value of t->val is returned. Memory for t->val is
783 * allocated using malloc and must be free()'d to reclaim it. If insufficient
784 * memory is available, NULL is returned.
785 */
786static char *get_string(char **p, struct token *t, char delim, int lower)
787{
788 char *b, *e;
789 size_t len, i;
790
791 /*
792 * b and e both start at the beginning of the input stream.
793 *
794 * e is incremented until we find the ending delimiter, or a NUL byte
795 * is reached. Then, we take e - b to find the length of the token.
796 */
797 b = e = *p;
798
799 while (*e) {
800 if ((delim == ' ' && isspace(*e)) || delim == *e)
801 break;
802 e++;
803 }
804
805 len = e - b;
806
807 /*
808 * Allocate memory to hold the string, and copy it in, converting
809 * characters to lowercase if lower is != 0.
810 */
811 t->val = malloc(len + 1);
812 if (!t->val)
813 return NULL;
814
815 for (i = 0; i < len; i++, b++) {
816 if (lower)
817 t->val[i] = tolower(*b);
818 else
819 t->val[i] = *b;
820 }
821
822 t->val[len] = '\0';
823
824 /*
825 * Update *p so the caller knows where to continue scanning.
826 */
827 *p = e;
828
829 t->type = T_STRING;
830
831 return t->val;
832}
833
834/*
835 * Populate a keyword token with a type and value.
836 */
837static void get_keyword(struct token *t)
838{
839 int i;
840
841 for (i = 0; keywords[i].val; i++) {
842 if (!strcmp(t->val, keywords[i].val)) {
843 t->type = keywords[i].type;
844 break;
845 }
846 }
847}
848
849/*
850 * Get the next token. We have to keep track of which state we're in to know
851 * if we're looking to get a string literal or a keyword.
852 *
853 * *p is updated to point at the first character after the current token.
854 */
855static void get_token(char **p, struct token *t, enum lex_state state)
856{
857 char *c = *p;
858
859 t->type = T_INVALID;
860
861 /* eat non EOL whitespace */
862 while (isblank(*c))
863 c++;
864
865 /*
866 * eat comments. note that string literals can't begin with #, but
867 * can contain a # after their first character.
868 */
869 if (*c == '#') {
870 while (*c && *c != '\n')
871 c++;
872 }
873
874 if (*c == '\n') {
875 t->type = T_EOL;
876 c++;
877 } else if (*c == '\0') {
878 t->type = T_EOF;
879 c++;
880 } else if (state == L_SLITERAL) {
881 get_string(&c, t, '\n', 0);
882 } else if (state == L_KEYWORD) {
883 /*
884 * when we expect a keyword, we first get the next string
885 * token delimited by whitespace, and then check if it
886 * matches a keyword in our keyword list. if it does, it's
887 * converted to a keyword token of the appropriate type, and
888 * if not, it remains a string token.
889 */
890 get_string(&c, t, ' ', 1);
891 get_keyword(t);
892 }
893
894 *p = c;
895}
896
897/*
898 * Increment *c until we get to the end of the current line, or EOF.
899 */
900static void eol_or_eof(char **c)
901{
902 while (**c && **c != '\n')
903 (*c)++;
904}
905
906/*
907 * All of these parse_* functions share some common behavior.
908 *
909 * They finish with *c pointing after the token they parse, and return 1 on
910 * success, or < 0 on error.
911 */
912
913/*
914 * Parse a string literal and store a pointer it at *dst. String literals
915 * terminate at the end of the line.
916 */
917static int parse_sliteral(char **c, char **dst)
918{
919 struct token t;
920 char *s = *c;
921
922 get_token(c, &t, L_SLITERAL);
923
924 if (t.type != T_STRING) {
925 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
926 return -EINVAL;
927 }
928
929 *dst = t.val;
930
931 return 1;
932}
933
934/*
935 * Parse a base 10 (unsigned) integer and store it at *dst.
936 */
937static int parse_integer(char **c, int *dst)
938{
939 struct token t;
940 char *s = *c;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500941
942 get_token(c, &t, L_SLITERAL);
943
944 if (t.type != T_STRING) {
945 printf("Expected string: %.*s\n", (int)(*c - s), s);
946 return -EINVAL;
947 }
948
Rob Herring58d7f432012-12-02 21:00:22 -0600949 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500950
951 free(t.val);
952
953 return 1;
954}
955
Steven Falco7de0fa82013-10-07 09:51:48 -0400956static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500957
958/*
959 * Parse an include statement, and retrieve and parse the file it mentions.
960 *
961 * base should point to a location where it's safe to store the file, and
962 * nest_level should indicate how many nested includes have occurred. For this
963 * include, nest_level has already been incremented and doesn't need to be
964 * incremented here.
965 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400966static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500967 struct pxe_menu *cfg, int nest_level)
968{
969 char *include_path;
970 char *s = *c;
971 int err;
972
973 err = parse_sliteral(c, &include_path);
974
975 if (err < 0) {
976 printf("Expected include path: %.*s\n",
977 (int)(*c - s), s);
978 return err;
979 }
980
Steven Falco7de0fa82013-10-07 09:51:48 -0400981 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500982
983 if (err < 0) {
984 printf("Couldn't retrieve %s\n", include_path);
985 return err;
986 }
987
Steven Falco7de0fa82013-10-07 09:51:48 -0400988 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500989}
990
991/*
992 * Parse lines that begin with 'menu'.
993 *
994 * b and nest are provided to handle the 'menu include' case.
995 *
996 * b should be the address where the file currently being parsed is stored.
997 *
998 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
999 * a file it includes, 3 when parsing a file included by that file, and so on.
1000 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001001static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001002{
1003 struct token t;
1004 char *s = *c;
Heiko Schocher9cd01dd2011-12-12 20:37:17 +00001005 int err = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001006
1007 get_token(c, &t, L_KEYWORD);
1008
1009 switch (t.type) {
1010 case T_TITLE:
1011 err = parse_sliteral(c, &cfg->title);
1012
1013 break;
1014
1015 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001016 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001017 nest_level + 1);
1018 break;
1019
1020 default:
1021 printf("Ignoring malformed menu command: %.*s\n",
1022 (int)(*c - s), s);
1023 }
1024
1025 if (err < 0)
1026 return err;
1027
1028 eol_or_eof(c);
1029
1030 return 1;
1031}
1032
1033/*
1034 * Handles parsing a 'menu line' when we're parsing a label.
1035 */
1036static int parse_label_menu(char **c, struct pxe_menu *cfg,
1037 struct pxe_label *label)
1038{
1039 struct token t;
1040 char *s;
1041
1042 s = *c;
1043
1044 get_token(c, &t, L_KEYWORD);
1045
1046 switch (t.type) {
1047 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001048 if (!cfg->default_label)
1049 cfg->default_label = strdup(label->name);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001050
1051 if (!cfg->default_label)
1052 return -ENOMEM;
1053
1054 break;
Rob Herringa6b74172012-03-28 05:51:34 +00001055 case T_LABEL:
1056 parse_sliteral(c, &label->menu);
1057 break;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001058 default:
1059 printf("Ignoring malformed menu command: %.*s\n",
1060 (int)(*c - s), s);
1061 }
1062
1063 eol_or_eof(c);
1064
1065 return 0;
1066}
1067
1068/*
1069 * Parses a label and adds it to the list of labels for a menu.
1070 *
1071 * A label ends when we either get to the end of a file, or
1072 * get some input we otherwise don't have a handler defined
1073 * for.
1074 *
1075 */
1076static int parse_label(char **c, struct pxe_menu *cfg)
1077{
1078 struct token t;
Rob Herringd8fceec2012-03-28 05:51:37 +00001079 int len;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001080 char *s = *c;
1081 struct pxe_label *label;
1082 int err;
1083
1084 label = label_create();
1085 if (!label)
1086 return -ENOMEM;
1087
1088 err = parse_sliteral(c, &label->name);
1089 if (err < 0) {
1090 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1091 label_destroy(label);
1092 return -EINVAL;
1093 }
1094
1095 list_add_tail(&label->list, &cfg->labels);
1096
1097 while (1) {
1098 s = *c;
1099 get_token(c, &t, L_KEYWORD);
1100
1101 err = 0;
1102 switch (t.type) {
1103 case T_MENU:
1104 err = parse_label_menu(c, cfg, label);
1105 break;
1106
1107 case T_KERNEL:
Rob Herringc93f74a2012-03-28 05:51:35 +00001108 case T_LINUX:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001109 err = parse_sliteral(c, &label->kernel);
1110 break;
1111
1112 case T_APPEND:
1113 err = parse_sliteral(c, &label->append);
Rob Herringd8fceec2012-03-28 05:51:37 +00001114 if (label->initrd)
1115 break;
1116 s = strstr(label->append, "initrd=");
1117 if (!s)
1118 break;
1119 s += 7;
1120 len = (int)(strchr(s, ' ') - s);
1121 label->initrd = malloc(len + 1);
1122 strncpy(label->initrd, s, len);
1123 label->initrd[len] = '\0';
1124
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001125 break;
1126
1127 case T_INITRD:
Rob Herringd8fceec2012-03-28 05:51:37 +00001128 if (!label->initrd)
1129 err = parse_sliteral(c, &label->initrd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001130 break;
1131
Chander Kashyap1644a9d2012-09-06 19:36:31 +00001132 case T_FDT:
1133 if (!label->fdt)
1134 err = parse_sliteral(c, &label->fdt);
1135 break;
1136
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001137 case T_LOCALBOOT:
Rob Herring58d7f432012-12-02 21:00:22 -06001138 label->localboot = 1;
1139 err = parse_integer(c, &label->localboot_val);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001140 break;
1141
Rob Herring5a3d8be2012-12-02 21:00:29 -06001142 case T_IPAPPEND:
1143 err = parse_integer(c, &label->ipappend);
1144 break;
1145
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001146 case T_EOL:
1147 break;
1148
1149 default:
1150 /*
1151 * put the token back! we don't want it - it's the end
1152 * of a label and whatever token this is, it's
1153 * something for the menu level context to handle.
1154 */
1155 *c = s;
1156 return 1;
1157 }
1158
1159 if (err < 0)
1160 return err;
1161 }
1162}
1163
1164/*
1165 * This 16 comes from the limit pxelinux imposes on nested includes.
1166 *
1167 * There is no reason at all we couldn't do more, but some limit helps prevent
1168 * infinite (until crash occurs) recursion if a file tries to include itself.
1169 */
1170#define MAX_NEST_LEVEL 16
1171
1172/*
1173 * Entry point for parsing a menu file. nest_level indicates how many times
1174 * we've nested in includes. It will be 1 for the top level menu file.
1175 *
1176 * Returns 1 on success, < 0 on error.
1177 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001178static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001179{
1180 struct token t;
1181 char *s, *b, *label_name;
1182 int err;
1183
1184 b = p;
1185
1186 if (nest_level > MAX_NEST_LEVEL) {
1187 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1188 return -EMLINK;
1189 }
1190
1191 while (1) {
1192 s = p;
1193
1194 get_token(&p, &t, L_KEYWORD);
1195
1196 err = 0;
1197 switch (t.type) {
1198 case T_MENU:
Rob Herringf0898b92012-12-02 21:00:25 -06001199 cfg->prompt = 1;
Steven Falco7de0fa82013-10-07 09:51:48 -04001200 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001201 break;
1202
1203 case T_TIMEOUT:
1204 err = parse_integer(&p, &cfg->timeout);
1205 break;
1206
1207 case T_LABEL:
1208 err = parse_label(&p, cfg);
1209 break;
1210
1211 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001212 case T_ONTIMEOUT:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001213 err = parse_sliteral(&p, &label_name);
1214
1215 if (label_name) {
1216 if (cfg->default_label)
1217 free(cfg->default_label);
1218
1219 cfg->default_label = label_name;
1220 }
1221
1222 break;
1223
Rob Herring8e5abb02012-05-25 10:43:16 +00001224 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001225 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring8e5abb02012-05-25 10:43:16 +00001226 nest_level + 1);
1227 break;
1228
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001229 case T_PROMPT:
Rob Herringf0898b92012-12-02 21:00:25 -06001230 eol_or_eof(&p);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001231 break;
1232
1233 case T_EOL:
1234 break;
1235
1236 case T_EOF:
1237 return 1;
1238
1239 default:
1240 printf("Ignoring unknown command: %.*s\n",
1241 (int)(p - s), s);
1242 eol_or_eof(&p);
1243 }
1244
1245 if (err < 0)
1246 return err;
1247 }
1248}
1249
1250/*
1251 * Free the memory used by a pxe_menu and its labels.
1252 */
1253static void destroy_pxe_menu(struct pxe_menu *cfg)
1254{
1255 struct list_head *pos, *n;
1256 struct pxe_label *label;
1257
1258 if (cfg->title)
1259 free(cfg->title);
1260
1261 if (cfg->default_label)
1262 free(cfg->default_label);
1263
1264 list_for_each_safe(pos, n, &cfg->labels) {
1265 label = list_entry(pos, struct pxe_label, list);
1266
1267 label_destroy(label);
1268 }
1269
1270 free(cfg);
1271}
1272
1273/*
1274 * Entry point for parsing a pxe file. This is only used for the top level
1275 * file.
1276 *
1277 * Returns NULL if there is an error, otherwise, returns a pointer to a
1278 * pxe_menu struct populated with the results of parsing the pxe file (and any
1279 * files it includes). The resulting pxe_menu struct can be free()'d by using
1280 * the destroy_pxe_menu() function.
1281 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001282static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001283{
1284 struct pxe_menu *cfg;
1285
1286 cfg = malloc(sizeof(struct pxe_menu));
1287
1288 if (!cfg)
1289 return NULL;
1290
1291 memset(cfg, 0, sizeof(struct pxe_menu));
1292
1293 INIT_LIST_HEAD(&cfg->labels);
1294
Steven Falco7de0fa82013-10-07 09:51:48 -04001295 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001296 destroy_pxe_menu(cfg);
1297 return NULL;
1298 }
1299
1300 return cfg;
1301}
1302
1303/*
1304 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1305 * menu code.
1306 */
1307static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1308{
1309 struct pxe_label *label;
1310 struct list_head *pos;
1311 struct menu *m;
1312 int err;
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001313 int i = 1;
1314 char *default_num = NULL;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001315
1316 /*
1317 * Create a menu and add items for all the labels.
1318 */
Pali Rohárd0d8d3b2013-03-23 14:50:40 +00001319 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1320 NULL, NULL);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001321
1322 if (!m)
1323 return NULL;
1324
1325 list_for_each(pos, &cfg->labels) {
1326 label = list_entry(pos, struct pxe_label, list);
1327
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001328 sprintf(label->num, "%d", i++);
1329 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001330 menu_destroy(m);
1331 return NULL;
1332 }
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001333 if (cfg->default_label &&
Rob Herringac995832012-12-02 21:00:27 -06001334 (strcmp(label->name, cfg->default_label) == 0))
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001335 default_num = label->num;
1336
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001337 }
1338
1339 /*
1340 * After we've created items for each label in the menu, set the
1341 * menu's default label if one was specified.
1342 */
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001343 if (default_num) {
1344 err = menu_default_set(m, default_num);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001345 if (err != 1) {
1346 if (err != -ENOENT) {
1347 menu_destroy(m);
1348 return NULL;
1349 }
1350
1351 printf("Missing default: %s\n", cfg->default_label);
1352 }
1353 }
1354
1355 return m;
1356}
1357
1358/*
1359 * Try to boot any labels we have yet to attempt to boot.
1360 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001361static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001362{
1363 struct list_head *pos;
1364 struct pxe_label *label;
1365
1366 list_for_each(pos, &cfg->labels) {
1367 label = list_entry(pos, struct pxe_label, list);
1368
1369 if (!label->attempted)
Tom Rini5b5b1762013-09-24 09:05:08 -04001370 label_boot(cmdtp, label);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001371 }
1372}
1373
1374/*
1375 * Boot the system as prescribed by a pxe_menu.
1376 *
1377 * Use the menu system to either get the user's choice or the default, based
1378 * on config or user input. If there is no default or user's choice,
1379 * attempted to boot labels in the order they were given in pxe files.
1380 * If the default or user's choice fails to boot, attempt to boot other
1381 * labels in the order they were given in pxe files.
1382 *
1383 * If this function returns, there weren't any labels that successfully
1384 * booted, or the user interrupted the menu selection via ctrl+c.
1385 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001386static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001387{
1388 void *choice;
1389 struct menu *m;
1390 int err;
1391
1392 m = pxe_menu_to_menu(cfg);
1393 if (!m)
1394 return;
1395
1396 err = menu_get_choice(m, &choice);
1397
1398 menu_destroy(m);
1399
Jason Hobbs083b7a62011-11-07 03:07:15 +00001400 /*
1401 * err == 1 means we got a choice back from menu_get_choice.
1402 *
1403 * err == -ENOENT if the menu was setup to select the default but no
1404 * default was set. in that case, we should continue trying to boot
1405 * labels that haven't been attempted yet.
1406 *
1407 * otherwise, the user interrupted or there was some other error and
1408 * we give up.
1409 */
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001410
Rob Herring58d7f432012-12-02 21:00:22 -06001411 if (err == 1) {
Tom Rini5b5b1762013-09-24 09:05:08 -04001412 err = label_boot(cmdtp, choice);
Rob Herring58d7f432012-12-02 21:00:22 -06001413 if (!err)
1414 return;
1415 } else if (err != -ENOENT) {
Jason Hobbs083b7a62011-11-07 03:07:15 +00001416 return;
Rob Herring58d7f432012-12-02 21:00:22 -06001417 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001418
Tom Rini5b5b1762013-09-24 09:05:08 -04001419 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001420}
1421
1422/*
1423 * Boots a system using a pxe file
1424 *
1425 * Returns 0 on success, 1 on error.
1426 */
1427static int
1428do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1429{
1430 unsigned long pxefile_addr_r;
1431 struct pxe_menu *cfg;
1432 char *pxefile_addr_str;
1433
Rob Herringeee675f2012-05-25 10:47:39 +00001434 do_getfile = do_get_tftp;
1435
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001436 if (argc == 1) {
1437 pxefile_addr_str = from_env("pxefile_addr_r");
1438 if (!pxefile_addr_str)
1439 return 1;
1440
1441 } else if (argc == 2) {
1442 pxefile_addr_str = argv[1];
1443 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +00001444 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001445 }
1446
1447 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1448 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1449 return 1;
1450 }
1451
Steven Falco7de0fa82013-10-07 09:51:48 -04001452 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001453
1454 if (cfg == NULL) {
1455 printf("Error parsing config file\n");
1456 return 1;
1457 }
1458
Tom Rini5b5b1762013-09-24 09:05:08 -04001459 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001460
1461 destroy_pxe_menu(cfg);
1462
1463 return 0;
1464}
1465
1466static cmd_tbl_t cmd_pxe_sub[] = {
1467 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1468 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1469};
1470
1471int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1472{
1473 cmd_tbl_t *cp;
1474
1475 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001476 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001477
Rob Herring759456e2013-10-18 13:04:42 -05001478 is_pxe = true;
1479
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001480 /* drop initial "pxe" arg */
1481 argc--;
1482 argv++;
1483
1484 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1485
1486 if (cp)
1487 return cp->cmd(cmdtp, flag, argc, argv);
1488
Simon Glassa06dfc72011-12-10 08:44:01 +00001489 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001490}
1491
1492U_BOOT_CMD(
1493 pxe, 3, 1, do_pxe,
1494 "commands to get and boot from pxe files",
1495 "get - try to retrieve a pxe file using tftp\npxe "
1496 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1497);
Rob Herringeee675f2012-05-25 10:47:39 +00001498
1499/*
1500 * Boots a system using a local disk syslinux/extlinux file
1501 *
1502 * Returns 0 on success, 1 on error.
1503 */
1504int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1505{
1506 unsigned long pxefile_addr_r;
1507 struct pxe_menu *cfg;
1508 char *pxefile_addr_str;
1509 char *filename;
1510 int prompt = 0;
1511
Rob Herring759456e2013-10-18 13:04:42 -05001512 is_pxe = false;
1513
Rob Herringeee675f2012-05-25 10:47:39 +00001514 if (strstr(argv[1], "-p")) {
1515 prompt = 1;
1516 argc--;
1517 argv++;
1518 }
1519
1520 if (argc < 4)
1521 return cmd_usage(cmdtp);
1522
1523 if (argc < 5) {
1524 pxefile_addr_str = from_env("pxefile_addr_r");
1525 if (!pxefile_addr_str)
1526 return 1;
1527 } else {
1528 pxefile_addr_str = argv[4];
1529 }
1530
1531 if (argc < 6)
1532 filename = getenv("bootfile");
1533 else {
1534 filename = argv[5];
1535 setenv("bootfile", filename);
1536 }
1537
1538 if (strstr(argv[3], "ext2"))
1539 do_getfile = do_get_ext2;
1540 else if (strstr(argv[3], "fat"))
1541 do_getfile = do_get_fat;
1542 else {
1543 printf("Invalid filesystem: %s\n", argv[3]);
1544 return 1;
1545 }
1546 fs_argv[1] = argv[1];
1547 fs_argv[2] = argv[2];
1548
1549 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1550 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1551 return 1;
1552 }
1553
Steven Falco7de0fa82013-10-07 09:51:48 -04001554 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herringeee675f2012-05-25 10:47:39 +00001555 printf("Error reading config file\n");
1556 return 1;
1557 }
1558
Steven Falco7de0fa82013-10-07 09:51:48 -04001559 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herringeee675f2012-05-25 10:47:39 +00001560
1561 if (cfg == NULL) {
1562 printf("Error parsing config file\n");
1563 return 1;
1564 }
1565
1566 if (prompt)
1567 cfg->prompt = 1;
1568
Tom Rini5b5b1762013-09-24 09:05:08 -04001569 handle_pxe_menu(cmdtp, cfg);
Rob Herringeee675f2012-05-25 10:47:39 +00001570
1571 destroy_pxe_menu(cfg);
1572
1573 return 0;
1574}
1575
1576U_BOOT_CMD(
1577 sysboot, 7, 1, do_sysboot,
1578 "command to get and boot from syslinux files",
1579 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1580 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1581 " filesystem on 'dev' on 'interface' to address 'addr'"
1582);