blob: c8163392326555ad96e5bcc9b3234e1a407cb82e [file] [log] [blame]
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wua1d715b2014-07-31 17:39:59 -07003 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs0e3a5932011-08-31 10:37:30 -05004 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0e3a5932011-08-31 10:37:30 -05006 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007
Jason Hobbs0e3a5932011-08-31 10:37:30 -05008#include <common.h>
9#include <command.h>
10#include <malloc.h>
11#include <linux/string.h>
12#include <linux/ctype.h>
13#include <errno.h>
14#include <linux/list.h>
Dennis Gilmorecd90dc82014-02-04 05:25:46 -060015#include <fs.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050016
17#include "menu.h"
18
19#define MAX_TFTP_PATH_LEN 127
20
Rob Herringe26e8d62012-12-02 21:00:28 -060021const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050022#ifdef CONFIG_SYS_SOC
Rob Herringe26e8d62012-12-02 21:00:28 -060023 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050024#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060025 "default-" CONFIG_SYS_ARCH,
26 "default",
27 NULL
28};
29
Rob Herring759456e2013-10-18 13:04:42 -050030static bool is_pxe;
31
Jason Hobbs0e3a5932011-08-31 10:37:30 -050032/*
33 * Like getenv, but prints an error if envvar isn't defined in the
34 * environment. It always returns what getenv does, so it can be used in
35 * place of getenv without changing error handling otherwise.
36 */
Rob Herring824901c2012-12-02 21:00:21 -060037static char *from_env(const char *envvar)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050038{
39 char *ret;
40
41 ret = getenv(envvar);
42
43 if (!ret)
44 printf("missing environment variable: %s\n", envvar);
45
46 return ret;
47}
48
Stephen Warren857291b2014-02-05 20:49:20 -070049#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-08-31 10:37:30 -050050/*
51 * Convert an ethaddr from the environment to the format used by pxelinux
52 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
53 * the beginning of the ethernet address to indicate a hardware type of
54 * Ethernet. Also converts uppercase hex characters into lowercase, to match
55 * pxelinux's behavior.
56 *
57 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
58 * environment, or some other value < 0 on error.
59 */
60static int format_mac_pxe(char *outbuf, size_t outbuf_len)
61{
Rob Herringdd248b32012-12-02 21:00:20 -060062 uchar ethaddr[6];
Jason Hobbs0e3a5932011-08-31 10:37:30 -050063
Rob Herringdd248b32012-12-02 21:00:20 -060064 if (outbuf_len < 21) {
David Feng4ec50292013-12-14 11:47:30 +080065 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050066
67 return -EINVAL;
68 }
69
Rob Herringdd248b32012-12-02 21:00:20 -060070 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
71 ethaddr))
72 return -ENOENT;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050073
Rob Herringdd248b32012-12-02 21:00:20 -060074 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
75 ethaddr[0], ethaddr[1], ethaddr[2],
76 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050077
78 return 1;
79}
Stephen Warren857291b2014-02-05 20:49:20 -070080#endif
Jason Hobbs0e3a5932011-08-31 10:37:30 -050081
82/*
83 * Returns the directory the file specified in the bootfile env variable is
84 * in. If bootfile isn't defined in the environment, return NULL, which should
85 * be interpreted as "don't prepend anything to paths".
86 */
Rob Herringdc410cd2012-03-28 05:51:36 +000087static int get_bootfile_path(const char *file_path, char *bootfile_path,
88 size_t bootfile_path_size)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050089{
90 char *bootfile, *last_slash;
Rob Herringdc410cd2012-03-28 05:51:36 +000091 size_t path_len = 0;
92
Rob Herring759456e2013-10-18 13:04:42 -050093 /* Only syslinux allows absolute paths */
94 if (file_path[0] == '/' && !is_pxe)
Rob Herringdc410cd2012-03-28 05:51:36 +000095 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050096
97 bootfile = from_env("bootfile");
98
Rob Herringdc410cd2012-03-28 05:51:36 +000099 if (!bootfile)
100 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500101
102 last_slash = strrchr(bootfile, '/');
103
Rob Herringdc410cd2012-03-28 05:51:36 +0000104 if (last_slash == NULL)
105 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500106
107 path_len = (last_slash - bootfile) + 1;
108
109 if (bootfile_path_size < path_len) {
David Feng4ec50292013-12-14 11:47:30 +0800110 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500111 bootfile_path_size, path_len);
112
113 return -1;
114 }
115
116 strncpy(bootfile_path, bootfile, path_len);
117
Rob Herringdc410cd2012-03-28 05:51:36 +0000118 ret:
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500119 bootfile_path[path_len] = '\0';
120
121 return 1;
122}
123
Steven Falco7de0fa82013-10-07 09:51:48 -0400124static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herringeee675f2012-05-25 10:47:39 +0000125
Stephen Warren857291b2014-02-05 20:49:20 -0700126#ifdef CONFIG_CMD_NET
Steven Falco7de0fa82013-10-07 09:51:48 -0400127static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000128{
129 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
130
131 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600132 tftp_argv[2] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000133
Steven Falco7de0fa82013-10-07 09:51:48 -0400134 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000135 return -ENOENT;
136
137 return 1;
138}
Stephen Warren857291b2014-02-05 20:49:20 -0700139#endif
Rob Herringeee675f2012-05-25 10:47:39 +0000140
141static char *fs_argv[5];
142
Steven Falco7de0fa82013-10-07 09:51:48 -0400143static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000144{
145#ifdef CONFIG_CMD_EXT2
146 fs_argv[0] = "ext2load";
147 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600148 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000149
Steven Falco7de0fa82013-10-07 09:51:48 -0400150 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000151 return 1;
152#endif
153 return -ENOENT;
154}
155
Steven Falco7de0fa82013-10-07 09:51:48 -0400156static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000157{
158#ifdef CONFIG_CMD_FAT
159 fs_argv[0] = "fatload";
160 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600161 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000162
Steven Falco7de0fa82013-10-07 09:51:48 -0400163 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000164 return 1;
165#endif
166 return -ENOENT;
167}
168
Dennis Gilmorecd90dc82014-02-04 05:25:46 -0600169static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
170{
171#ifdef CONFIG_CMD_FS_GENERIC
172 fs_argv[0] = "load";
173 fs_argv[3] = file_addr;
174 fs_argv[4] = (void *)file_path;
175
176 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
177 return 1;
178#endif
179 return -ENOENT;
180}
181
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500182/*
183 * As in pxelinux, paths to files referenced from files we retrieve are
184 * relative to the location of bootfile. get_relfile takes such a path and
185 * joins it with the bootfile path to get the full path to the target file. If
186 * the bootfile path is NULL, we use file_path as is.
187 *
188 * Returns 1 for success, or < 0 on error.
189 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400190static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500191{
192 size_t path_len;
193 char relfile[MAX_TFTP_PATH_LEN+1];
194 char addr_buf[10];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500195 int err;
196
Rob Herringdc410cd2012-03-28 05:51:36 +0000197 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500198
199 if (err < 0)
200 return err;
201
202 path_len = strlen(file_path);
203 path_len += strlen(relfile);
204
205 if (path_len > MAX_TFTP_PATH_LEN) {
206 printf("Base path too long (%s%s)\n",
207 relfile,
208 file_path);
209
210 return -ENAMETOOLONG;
211 }
212
213 strcat(relfile, file_path);
214
215 printf("Retrieving file: %s\n", relfile);
216
217 sprintf(addr_buf, "%p", file_addr);
218
Steven Falco7de0fa82013-10-07 09:51:48 -0400219 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500220}
221
222/*
223 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
224 * 'bootfile' was specified in the environment, the path to bootfile will be
225 * prepended to 'file_path' and the resulting path will be used.
226 *
227 * Returns 1 on success, or < 0 for error.
228 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400229static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500230{
231 unsigned long config_file_size;
232 char *tftp_filesize;
233 int err;
234
Steven Falco7de0fa82013-10-07 09:51:48 -0400235 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500236
237 if (err < 0)
238 return err;
239
240 /*
241 * the file comes without a NUL byte at the end, so find out its size
242 * and add the NUL byte.
243 */
244 tftp_filesize = from_env("filesize");
245
246 if (!tftp_filesize)
247 return -ENOENT;
248
249 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
250 return -EINVAL;
251
252 *(char *)(file_addr + config_file_size) = '\0';
253
254 return 1;
255}
256
Stephen Warren857291b2014-02-05 20:49:20 -0700257#ifdef CONFIG_CMD_NET
258
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500259#define PXELINUX_DIR "pxelinux.cfg/"
260
261/*
262 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
263 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
264 * from the bootfile path, as described above.
265 *
266 * Returns 1 on success or < 0 on error.
267 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400268static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500269{
270 size_t base_len = strlen(PXELINUX_DIR);
271 char path[MAX_TFTP_PATH_LEN+1];
272
273 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
274 printf("path (%s%s) too long, skipping\n",
275 PXELINUX_DIR, file);
276 return -ENAMETOOLONG;
277 }
278
279 sprintf(path, PXELINUX_DIR "%s", file);
280
Steven Falco7de0fa82013-10-07 09:51:48 -0400281 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500282}
283
284/*
285 * Looks for a pxe file with a name based on the pxeuuid environment variable.
286 *
287 * Returns 1 on success or < 0 on error.
288 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400289static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500290{
291 char *uuid_str;
292
293 uuid_str = from_env("pxeuuid");
294
295 if (!uuid_str)
296 return -ENOENT;
297
Steven Falco7de0fa82013-10-07 09:51:48 -0400298 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500299}
300
301/*
302 * Looks for a pxe file with a name based on the 'ethaddr' environment
303 * variable.
304 *
305 * Returns 1 on success or < 0 on error.
306 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400307static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500308{
309 char mac_str[21];
310 int err;
311
312 err = format_mac_pxe(mac_str, sizeof(mac_str));
313
314 if (err < 0)
315 return err;
316
Steven Falco7de0fa82013-10-07 09:51:48 -0400317 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500318}
319
320/*
321 * Looks for pxe files with names based on our IP address. See pxelinux
322 * documentation for details on what these file names look like. We match
323 * that exactly.
324 *
325 * Returns 1 on success or < 0 on error.
326 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400327static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500328{
329 char ip_addr[9];
330 int mask_pos, err;
331
332 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
333
334 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400335 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500336
337 if (err > 0)
338 return err;
339
340 ip_addr[mask_pos] = '\0';
341 }
342
343 return -ENOENT;
344}
345
346/*
347 * Entry point for the 'pxe get' command.
348 * This Follows pxelinux's rules to download a config file from a tftp server.
349 * The file is stored at the location given by the pxefile_addr_r environment
350 * variable, which must be set.
351 *
352 * UUID comes from pxeuuid env variable, if defined
353 * MAC addr comes from ethaddr env variable, if defined
354 * IP
355 *
356 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
357 *
358 * Returns 0 on success or 1 on error.
359 */
360static int
361do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
362{
363 char *pxefile_addr_str;
Jason Hobbs9135c792012-03-05 08:12:28 +0000364 unsigned long pxefile_addr_r;
Rob Herringe26e8d62012-12-02 21:00:28 -0600365 int err, i = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500366
Rob Herringeee675f2012-05-25 10:47:39 +0000367 do_getfile = do_get_tftp;
368
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500369 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000370 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500371
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500372 pxefile_addr_str = from_env("pxefile_addr_r");
373
374 if (!pxefile_addr_str)
375 return 1;
376
377 err = strict_strtoul(pxefile_addr_str, 16,
378 (unsigned long *)&pxefile_addr_r);
379 if (err < 0)
380 return 1;
381
382 /*
383 * Keep trying paths until we successfully get a file we're looking
384 * for.
385 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400386 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
387 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
388 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500389 printf("Config file found\n");
390
391 return 0;
392 }
393
Rob Herringe26e8d62012-12-02 21:00:28 -0600394 while (pxe_default_paths[i]) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400395 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herringe26e8d62012-12-02 21:00:28 -0600396 (void *)pxefile_addr_r) > 0) {
397 printf("Config file found\n");
398 return 0;
399 }
400 i++;
401 }
402
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500403 printf("Config file not found\n");
404
405 return 1;
406}
Stephen Warren857291b2014-02-05 20:49:20 -0700407#endif
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500408
409/*
410 * Wrapper to make it easier to store the file at file_path in the location
411 * specified by envaddr_name. file_path will be joined to the bootfile path,
412 * if any is specified.
413 *
414 * Returns 1 on success or < 0 on error.
415 */
Steven Falco7de0fa82013-10-07 09:51:48 -0400416static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500417{
Jason Hobbs9135c792012-03-05 08:12:28 +0000418 unsigned long file_addr;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500419 char *envaddr;
420
421 envaddr = from_env(envaddr_name);
422
423 if (!envaddr)
424 return -ENOENT;
425
Jason Hobbs9135c792012-03-05 08:12:28 +0000426 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500427 return -EINVAL;
428
Steven Falco7de0fa82013-10-07 09:51:48 -0400429 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500430}
431
432/*
433 * A note on the pxe file parser.
434 *
435 * We're parsing files that use syslinux grammar, which has a few quirks.
436 * String literals must be recognized based on context - there is no
437 * quoting or escaping support. There's also nothing to explicitly indicate
438 * when a label section completes. We deal with that by ending a label
439 * section whenever we see a line that doesn't include.
440 *
441 * As with the syslinux family, this same file format could be reused in the
442 * future for non pxe purposes. The only action it takes during parsing that
443 * would throw this off is handling of include files. It assumes we're using
444 * pxe, and does a tftp download of a file listed as an include file in the
445 * middle of the parsing operation. That could be handled by refactoring it to
446 * take a 'include file getter' function.
447 */
448
449/*
450 * Describes a single label given in a pxe file.
451 *
452 * Create these with the 'label_create' function given below.
453 *
454 * name - the name of the menu as given on the 'menu label' line.
455 * kernel - the path to the kernel file to use for this label.
456 * append - kernel command line to use when booting this label
457 * initrd - path to the initrd to use for this label.
458 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
459 * localboot - 1 if this label specified 'localboot', 0 otherwise.
460 * list - lets these form a list, which a pxe_menu struct will hold.
461 */
462struct pxe_label {
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600463 char num[4];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500464 char *name;
Rob Herringa6b74172012-03-28 05:51:34 +0000465 char *menu;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500466 char *kernel;
467 char *append;
468 char *initrd;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000469 char *fdt;
Stephen Warren90d09af2014-01-28 14:50:10 -0700470 char *fdtdir;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600471 int ipappend;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500472 int attempted;
473 int localboot;
Rob Herring58d7f432012-12-02 21:00:22 -0600474 int localboot_val;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500475 struct list_head list;
476};
477
478/*
479 * Describes a pxe menu as given via pxe files.
480 *
481 * title - the name of the menu as given by a 'menu title' line.
482 * default_label - the name of the default label, if any.
483 * timeout - time in tenths of a second to wait for a user key-press before
484 * booting the default label.
485 * prompt - if 0, don't prompt for a choice unless the timeout period is
486 * interrupted. If 1, always prompt for a choice regardless of
487 * timeout.
488 * labels - a list of labels defined for the menu.
489 */
490struct pxe_menu {
491 char *title;
492 char *default_label;
493 int timeout;
494 int prompt;
495 struct list_head labels;
496};
497
498/*
499 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
500 * result must be free()'d to reclaim the memory.
501 *
502 * Returns NULL if malloc fails.
503 */
504static struct pxe_label *label_create(void)
505{
506 struct pxe_label *label;
507
508 label = malloc(sizeof(struct pxe_label));
509
510 if (!label)
511 return NULL;
512
513 memset(label, 0, sizeof(struct pxe_label));
514
515 return label;
516}
517
518/*
519 * Free the memory used by a pxe_label, including that used by its name,
520 * kernel, append and initrd members, if they're non NULL.
521 *
522 * So - be sure to only use dynamically allocated memory for the members of
523 * the pxe_label struct, unless you want to clean it up first. These are
524 * currently only created by the pxe file parsing code.
525 */
526static void label_destroy(struct pxe_label *label)
527{
528 if (label->name)
529 free(label->name);
530
531 if (label->kernel)
532 free(label->kernel);
533
534 if (label->append)
535 free(label->append);
536
537 if (label->initrd)
538 free(label->initrd);
539
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000540 if (label->fdt)
541 free(label->fdt);
542
Stephen Warren90d09af2014-01-28 14:50:10 -0700543 if (label->fdtdir)
544 free(label->fdtdir);
545
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500546 free(label);
547}
548
549/*
550 * Print a label and its string members if they're defined.
551 *
552 * This is passed as a callback to the menu code for displaying each
553 * menu entry.
554 */
555static void label_print(void *data)
556{
557 struct pxe_label *label = data;
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600558 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500559
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600560 printf("%s:\t%s\n", label->num, c);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500561}
562
563/*
564 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
565 * environment variable is defined. Its contents will be executed as U-boot
566 * command. If the label specified an 'append' line, its contents will be
567 * used to overwrite the contents of the 'bootargs' environment variable prior
568 * to running 'localcmd'.
569 *
570 * Returns 1 on success or < 0 on error.
571 */
572static int label_localboot(struct pxe_label *label)
573{
Simon Glassa6642e02012-03-30 21:30:55 +0000574 char *localcmd;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500575
576 localcmd = from_env("localcmd");
577
578 if (!localcmd)
579 return -ENOENT;
580
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500581 if (label->append)
582 setenv("bootargs", label->append);
583
Simon Glassa6642e02012-03-30 21:30:55 +0000584 debug("running: %s\n", localcmd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500585
Simon Glassa6642e02012-03-30 21:30:55 +0000586 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500587}
588
589/*
590 * Boot according to the contents of a pxe_label.
591 *
592 * If we can't boot for any reason, we return. A successful boot never
593 * returns.
594 *
595 * The kernel will be stored in the location given by the 'kernel_addr_r'
596 * environment variable.
597 *
598 * If the label specifies an initrd file, it will be stored in the location
599 * given by the 'ramdisk_addr_r' environment variable.
600 *
601 * If the label specifies an 'append' line, its contents will overwrite that
602 * of the 'bootargs' environment variable.
603 */
Tom Rini5b5b1762013-09-24 09:05:08 -0400604static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500605{
606 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringc0356dc2012-12-03 13:17:21 -0600607 char initrd_str[22];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600608 char mac_str[29] = "";
609 char ip_str[68] = "";
610 char *bootargs;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500611 int bootm_argc = 3;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600612 int len = 0;
Bryan Wua1d715b2014-07-31 17:39:59 -0700613 ulong kernel_addr;
614 void *buf;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500615
616 label_print(label);
617
618 label->attempted = 1;
619
620 if (label->localboot) {
Rob Herring58d7f432012-12-02 21:00:22 -0600621 if (label->localboot_val >= 0)
622 label_localboot(label);
623 return 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500624 }
625
626 if (label->kernel == NULL) {
627 printf("No kernel given, skipping %s\n",
628 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600629 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500630 }
631
632 if (label->initrd) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400633 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500634 printf("Skipping %s for failure retrieving initrd\n",
635 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600636 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500637 }
638
Rob Herringc0356dc2012-12-03 13:17:21 -0600639 bootm_argv[2] = initrd_str;
640 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
641 strcat(bootm_argv[2], ":");
642 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500643 } else {
644 bootm_argv[2] = "-";
645 }
646
Steven Falco7de0fa82013-10-07 09:51:48 -0400647 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500648 printf("Skipping %s for failure retrieving kernel\n",
649 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600650 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500651 }
652
Rob Herring5a3d8be2012-12-02 21:00:29 -0600653 if (label->ipappend & 0x1) {
654 sprintf(ip_str, " ip=%s:%s:%s:%s",
655 getenv("ipaddr"), getenv("serverip"),
656 getenv("gatewayip"), getenv("netmask"));
657 len += strlen(ip_str);
658 }
659
Stephen Warren857291b2014-02-05 20:49:20 -0700660#ifdef CONFIG_CMD_NET
Rob Herring5a3d8be2012-12-02 21:00:29 -0600661 if (label->ipappend & 0x2) {
662 int err;
663 strcpy(mac_str, " BOOTIF=");
664 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
665 if (err < 0)
666 mac_str[0] = '\0';
667 len += strlen(mac_str);
668 }
Stephen Warren857291b2014-02-05 20:49:20 -0700669#endif
Rob Herring5a3d8be2012-12-02 21:00:29 -0600670
671 if (label->append)
672 len += strlen(label->append);
673
674 if (len) {
675 bootargs = malloc(len + 1);
676 if (!bootargs)
677 return 1;
678 bootargs[0] = '\0';
679 if (label->append)
680 strcpy(bootargs, label->append);
681 strcat(bootargs, ip_str);
682 strcat(bootargs, mac_str);
683
684 setenv("bootargs", bootargs);
685 printf("append: %s\n", bootargs);
686
687 free(bootargs);
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600688 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500689
690 bootm_argv[1] = getenv("kernel_addr_r");
691
692 /*
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000693 * fdt usage is optional:
694 * It handles the following scenarios. All scenarios are exclusive
695 *
696 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
697 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
698 * and adjust argc appropriately.
699 *
700 * Scenario 2: If there is an fdt_addr specified, pass it along to
701 * bootm, and adjust argc appropriately.
702 *
703 * Scenario 3: fdt blob is not available.
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500704 */
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000705 bootm_argv[3] = getenv("fdt_addr_r");
706
707 /* if fdt label is defined then get fdt from server */
Stephen Warren90d09af2014-01-28 14:50:10 -0700708 if (bootm_argv[3]) {
709 char *fdtfile = NULL;
710 char *fdtfilefree = NULL;
711
712 if (label->fdt) {
713 fdtfile = label->fdt;
714 } else if (label->fdtdir) {
Stephen Warrenc0592392014-02-12 14:30:04 -0700715 char *f1, *f2, *f3, *f4, *slash;
Stephen Warren90d09af2014-01-28 14:50:10 -0700716
Stephen Warrenc0592392014-02-12 14:30:04 -0700717 f1 = getenv("fdtfile");
718 if (f1) {
719 f2 = "";
720 f3 = "";
721 f4 = "";
722 } else {
723 /*
724 * For complex cases where this code doesn't
725 * generate the correct filename, the board
726 * code should set $fdtfile during early boot,
727 * or the boot scripts should set $fdtfile
728 * before invoking "pxe" or "sysboot".
729 */
730 f1 = getenv("soc");
731 f2 = "-";
732 f3 = getenv("board");
733 f4 = ".dtb";
734 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700735
Stephen Warrenc0592392014-02-12 14:30:04 -0700736 len = strlen(label->fdtdir);
737 if (!len)
738 slash = "./";
739 else if (label->fdtdir[len - 1] != '/')
740 slash = "/";
741 else
742 slash = "";
Stephen Warren90d09af2014-01-28 14:50:10 -0700743
Stephen Warrenc0592392014-02-12 14:30:04 -0700744 len = strlen(label->fdtdir) + strlen(slash) +
745 strlen(f1) + strlen(f2) + strlen(f3) +
746 strlen(f4) + 1;
747 fdtfilefree = malloc(len);
748 if (!fdtfilefree) {
749 printf("malloc fail (FDT filename)\n");
750 return 1;
Stephen Warren90d09af2014-01-28 14:50:10 -0700751 }
Stephen Warrenc0592392014-02-12 14:30:04 -0700752
753 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
754 label->fdtdir, slash, f1, f2, f3, f4);
755 fdtfile = fdtfilefree;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000756 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700757
758 if (fdtfile) {
759 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
760 free(fdtfilefree);
761 if (err < 0) {
762 printf("Skipping %s for failure retrieving fdt\n",
763 label->name);
764 return 1;
765 }
766 } else {
767 bootm_argv[3] = NULL;
768 }
769 }
770
771 if (!bootm_argv[3])
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000772 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500773
774 if (bootm_argv[3])
775 bootm_argc = 4;
776
Bryan Wua1d715b2014-07-31 17:39:59 -0700777 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
778 buf = map_sysmem(kernel_addr, 0);
779 /* Try bootm for legacy and FIT format image */
780 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
781 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600782#ifdef CONFIG_CMD_BOOTZ
Bryan Wua1d715b2014-07-31 17:39:59 -0700783 /* Try booting a zImage */
784 else
785 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600786#endif
Rob Herring58d7f432012-12-02 21:00:22 -0600787 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500788}
789
790/*
791 * Tokens for the pxe file parser.
792 */
793enum token_type {
794 T_EOL,
795 T_STRING,
796 T_EOF,
797 T_MENU,
798 T_TITLE,
799 T_TIMEOUT,
800 T_LABEL,
801 T_KERNEL,
Rob Herringc93f74a2012-03-28 05:51:35 +0000802 T_LINUX,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500803 T_APPEND,
804 T_INITRD,
805 T_LOCALBOOT,
806 T_DEFAULT,
807 T_PROMPT,
808 T_INCLUDE,
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000809 T_FDT,
Stephen Warren90d09af2014-01-28 14:50:10 -0700810 T_FDTDIR,
Rob Herringac995832012-12-02 21:00:27 -0600811 T_ONTIMEOUT,
Rob Herring5a3d8be2012-12-02 21:00:29 -0600812 T_IPAPPEND,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500813 T_INVALID
814};
815
816/*
817 * A token - given by a value and a type.
818 */
819struct token {
820 char *val;
821 enum token_type type;
822};
823
824/*
825 * Keywords recognized.
826 */
827static const struct token keywords[] = {
828 {"menu", T_MENU},
829 {"title", T_TITLE},
830 {"timeout", T_TIMEOUT},
831 {"default", T_DEFAULT},
832 {"prompt", T_PROMPT},
833 {"label", T_LABEL},
834 {"kernel", T_KERNEL},
Rob Herringc93f74a2012-03-28 05:51:35 +0000835 {"linux", T_LINUX},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500836 {"localboot", T_LOCALBOOT},
837 {"append", T_APPEND},
838 {"initrd", T_INITRD},
839 {"include", T_INCLUDE},
Stephen Warren7b472af2014-01-28 14:50:09 -0700840 {"devicetree", T_FDT},
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000841 {"fdt", T_FDT},
Stephen Warren90d09af2014-01-28 14:50:10 -0700842 {"devicetreedir", T_FDTDIR},
843 {"fdtdir", T_FDTDIR},
Rob Herringac995832012-12-02 21:00:27 -0600844 {"ontimeout", T_ONTIMEOUT,},
Rob Herring5a3d8be2012-12-02 21:00:29 -0600845 {"ipappend", T_IPAPPEND,},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500846 {NULL, T_INVALID}
847};
848
849/*
850 * Since pxe(linux) files don't have a token to identify the start of a
851 * literal, we have to keep track of when we're in a state where a literal is
852 * expected vs when we're in a state a keyword is expected.
853 */
854enum lex_state {
855 L_NORMAL = 0,
856 L_KEYWORD,
857 L_SLITERAL
858};
859
860/*
861 * get_string retrieves a string from *p and stores it as a token in
862 * *t.
863 *
864 * get_string used for scanning both string literals and keywords.
865 *
866 * Characters from *p are copied into t-val until a character equal to
867 * delim is found, or a NUL byte is reached. If delim has the special value of
868 * ' ', any whitespace character will be used as a delimiter.
869 *
870 * If lower is unequal to 0, uppercase characters will be converted to
871 * lowercase in the result. This is useful to make keywords case
872 * insensitive.
873 *
874 * The location of *p is updated to point to the first character after the end
875 * of the token - the ending delimiter.
876 *
877 * On success, the new value of t->val is returned. Memory for t->val is
878 * allocated using malloc and must be free()'d to reclaim it. If insufficient
879 * memory is available, NULL is returned.
880 */
881static char *get_string(char **p, struct token *t, char delim, int lower)
882{
883 char *b, *e;
884 size_t len, i;
885
886 /*
887 * b and e both start at the beginning of the input stream.
888 *
889 * e is incremented until we find the ending delimiter, or a NUL byte
890 * is reached. Then, we take e - b to find the length of the token.
891 */
892 b = e = *p;
893
894 while (*e) {
895 if ((delim == ' ' && isspace(*e)) || delim == *e)
896 break;
897 e++;
898 }
899
900 len = e - b;
901
902 /*
903 * Allocate memory to hold the string, and copy it in, converting
904 * characters to lowercase if lower is != 0.
905 */
906 t->val = malloc(len + 1);
907 if (!t->val)
908 return NULL;
909
910 for (i = 0; i < len; i++, b++) {
911 if (lower)
912 t->val[i] = tolower(*b);
913 else
914 t->val[i] = *b;
915 }
916
917 t->val[len] = '\0';
918
919 /*
920 * Update *p so the caller knows where to continue scanning.
921 */
922 *p = e;
923
924 t->type = T_STRING;
925
926 return t->val;
927}
928
929/*
930 * Populate a keyword token with a type and value.
931 */
932static void get_keyword(struct token *t)
933{
934 int i;
935
936 for (i = 0; keywords[i].val; i++) {
937 if (!strcmp(t->val, keywords[i].val)) {
938 t->type = keywords[i].type;
939 break;
940 }
941 }
942}
943
944/*
945 * Get the next token. We have to keep track of which state we're in to know
946 * if we're looking to get a string literal or a keyword.
947 *
948 * *p is updated to point at the first character after the current token.
949 */
950static void get_token(char **p, struct token *t, enum lex_state state)
951{
952 char *c = *p;
953
954 t->type = T_INVALID;
955
956 /* eat non EOL whitespace */
957 while (isblank(*c))
958 c++;
959
960 /*
961 * eat comments. note that string literals can't begin with #, but
962 * can contain a # after their first character.
963 */
964 if (*c == '#') {
965 while (*c && *c != '\n')
966 c++;
967 }
968
969 if (*c == '\n') {
970 t->type = T_EOL;
971 c++;
972 } else if (*c == '\0') {
973 t->type = T_EOF;
974 c++;
975 } else if (state == L_SLITERAL) {
976 get_string(&c, t, '\n', 0);
977 } else if (state == L_KEYWORD) {
978 /*
979 * when we expect a keyword, we first get the next string
980 * token delimited by whitespace, and then check if it
981 * matches a keyword in our keyword list. if it does, it's
982 * converted to a keyword token of the appropriate type, and
983 * if not, it remains a string token.
984 */
985 get_string(&c, t, ' ', 1);
986 get_keyword(t);
987 }
988
989 *p = c;
990}
991
992/*
993 * Increment *c until we get to the end of the current line, or EOF.
994 */
995static void eol_or_eof(char **c)
996{
997 while (**c && **c != '\n')
998 (*c)++;
999}
1000
1001/*
1002 * All of these parse_* functions share some common behavior.
1003 *
1004 * They finish with *c pointing after the token they parse, and return 1 on
1005 * success, or < 0 on error.
1006 */
1007
1008/*
1009 * Parse a string literal and store a pointer it at *dst. String literals
1010 * terminate at the end of the line.
1011 */
1012static int parse_sliteral(char **c, char **dst)
1013{
1014 struct token t;
1015 char *s = *c;
1016
1017 get_token(c, &t, L_SLITERAL);
1018
1019 if (t.type != T_STRING) {
1020 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1021 return -EINVAL;
1022 }
1023
1024 *dst = t.val;
1025
1026 return 1;
1027}
1028
1029/*
1030 * Parse a base 10 (unsigned) integer and store it at *dst.
1031 */
1032static int parse_integer(char **c, int *dst)
1033{
1034 struct token t;
1035 char *s = *c;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001036
1037 get_token(c, &t, L_SLITERAL);
1038
1039 if (t.type != T_STRING) {
1040 printf("Expected string: %.*s\n", (int)(*c - s), s);
1041 return -EINVAL;
1042 }
1043
Rob Herring58d7f432012-12-02 21:00:22 -06001044 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001045
1046 free(t.val);
1047
1048 return 1;
1049}
1050
Steven Falco7de0fa82013-10-07 09:51:48 -04001051static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001052
1053/*
1054 * Parse an include statement, and retrieve and parse the file it mentions.
1055 *
1056 * base should point to a location where it's safe to store the file, and
1057 * nest_level should indicate how many nested includes have occurred. For this
1058 * include, nest_level has already been incremented and doesn't need to be
1059 * incremented here.
1060 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001061static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001062 struct pxe_menu *cfg, int nest_level)
1063{
1064 char *include_path;
1065 char *s = *c;
1066 int err;
1067
1068 err = parse_sliteral(c, &include_path);
1069
1070 if (err < 0) {
1071 printf("Expected include path: %.*s\n",
1072 (int)(*c - s), s);
1073 return err;
1074 }
1075
Steven Falco7de0fa82013-10-07 09:51:48 -04001076 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001077
1078 if (err < 0) {
1079 printf("Couldn't retrieve %s\n", include_path);
1080 return err;
1081 }
1082
Steven Falco7de0fa82013-10-07 09:51:48 -04001083 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001084}
1085
1086/*
1087 * Parse lines that begin with 'menu'.
1088 *
1089 * b and nest are provided to handle the 'menu include' case.
1090 *
1091 * b should be the address where the file currently being parsed is stored.
1092 *
1093 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1094 * a file it includes, 3 when parsing a file included by that file, and so on.
1095 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001096static 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 -05001097{
1098 struct token t;
1099 char *s = *c;
Heiko Schocher9cd01dd2011-12-12 20:37:17 +00001100 int err = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001101
1102 get_token(c, &t, L_KEYWORD);
1103
1104 switch (t.type) {
1105 case T_TITLE:
1106 err = parse_sliteral(c, &cfg->title);
1107
1108 break;
1109
1110 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001111 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001112 nest_level + 1);
1113 break;
1114
1115 default:
1116 printf("Ignoring malformed menu command: %.*s\n",
1117 (int)(*c - s), s);
1118 }
1119
1120 if (err < 0)
1121 return err;
1122
1123 eol_or_eof(c);
1124
1125 return 1;
1126}
1127
1128/*
1129 * Handles parsing a 'menu line' when we're parsing a label.
1130 */
1131static int parse_label_menu(char **c, struct pxe_menu *cfg,
1132 struct pxe_label *label)
1133{
1134 struct token t;
1135 char *s;
1136
1137 s = *c;
1138
1139 get_token(c, &t, L_KEYWORD);
1140
1141 switch (t.type) {
1142 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001143 if (!cfg->default_label)
1144 cfg->default_label = strdup(label->name);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001145
1146 if (!cfg->default_label)
1147 return -ENOMEM;
1148
1149 break;
Rob Herringa6b74172012-03-28 05:51:34 +00001150 case T_LABEL:
1151 parse_sliteral(c, &label->menu);
1152 break;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001153 default:
1154 printf("Ignoring malformed menu command: %.*s\n",
1155 (int)(*c - s), s);
1156 }
1157
1158 eol_or_eof(c);
1159
1160 return 0;
1161}
1162
1163/*
1164 * Parses a label and adds it to the list of labels for a menu.
1165 *
1166 * A label ends when we either get to the end of a file, or
1167 * get some input we otherwise don't have a handler defined
1168 * for.
1169 *
1170 */
1171static int parse_label(char **c, struct pxe_menu *cfg)
1172{
1173 struct token t;
Rob Herringd8fceec2012-03-28 05:51:37 +00001174 int len;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001175 char *s = *c;
1176 struct pxe_label *label;
1177 int err;
1178
1179 label = label_create();
1180 if (!label)
1181 return -ENOMEM;
1182
1183 err = parse_sliteral(c, &label->name);
1184 if (err < 0) {
1185 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1186 label_destroy(label);
1187 return -EINVAL;
1188 }
1189
1190 list_add_tail(&label->list, &cfg->labels);
1191
1192 while (1) {
1193 s = *c;
1194 get_token(c, &t, L_KEYWORD);
1195
1196 err = 0;
1197 switch (t.type) {
1198 case T_MENU:
1199 err = parse_label_menu(c, cfg, label);
1200 break;
1201
1202 case T_KERNEL:
Rob Herringc93f74a2012-03-28 05:51:35 +00001203 case T_LINUX:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001204 err = parse_sliteral(c, &label->kernel);
1205 break;
1206
1207 case T_APPEND:
1208 err = parse_sliteral(c, &label->append);
Rob Herringd8fceec2012-03-28 05:51:37 +00001209 if (label->initrd)
1210 break;
1211 s = strstr(label->append, "initrd=");
1212 if (!s)
1213 break;
1214 s += 7;
1215 len = (int)(strchr(s, ' ') - s);
1216 label->initrd = malloc(len + 1);
1217 strncpy(label->initrd, s, len);
1218 label->initrd[len] = '\0';
1219
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001220 break;
1221
1222 case T_INITRD:
Rob Herringd8fceec2012-03-28 05:51:37 +00001223 if (!label->initrd)
1224 err = parse_sliteral(c, &label->initrd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001225 break;
1226
Chander Kashyap1644a9d2012-09-06 19:36:31 +00001227 case T_FDT:
1228 if (!label->fdt)
1229 err = parse_sliteral(c, &label->fdt);
1230 break;
1231
Stephen Warren90d09af2014-01-28 14:50:10 -07001232 case T_FDTDIR:
1233 if (!label->fdtdir)
1234 err = parse_sliteral(c, &label->fdtdir);
1235 break;
1236
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001237 case T_LOCALBOOT:
Rob Herring58d7f432012-12-02 21:00:22 -06001238 label->localboot = 1;
1239 err = parse_integer(c, &label->localboot_val);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001240 break;
1241
Rob Herring5a3d8be2012-12-02 21:00:29 -06001242 case T_IPAPPEND:
1243 err = parse_integer(c, &label->ipappend);
1244 break;
1245
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001246 case T_EOL:
1247 break;
1248
1249 default:
1250 /*
1251 * put the token back! we don't want it - it's the end
1252 * of a label and whatever token this is, it's
1253 * something for the menu level context to handle.
1254 */
1255 *c = s;
1256 return 1;
1257 }
1258
1259 if (err < 0)
1260 return err;
1261 }
1262}
1263
1264/*
1265 * This 16 comes from the limit pxelinux imposes on nested includes.
1266 *
1267 * There is no reason at all we couldn't do more, but some limit helps prevent
1268 * infinite (until crash occurs) recursion if a file tries to include itself.
1269 */
1270#define MAX_NEST_LEVEL 16
1271
1272/*
1273 * Entry point for parsing a menu file. nest_level indicates how many times
1274 * we've nested in includes. It will be 1 for the top level menu file.
1275 *
1276 * Returns 1 on success, < 0 on error.
1277 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001278static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001279{
1280 struct token t;
1281 char *s, *b, *label_name;
1282 int err;
1283
1284 b = p;
1285
1286 if (nest_level > MAX_NEST_LEVEL) {
1287 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1288 return -EMLINK;
1289 }
1290
1291 while (1) {
1292 s = p;
1293
1294 get_token(&p, &t, L_KEYWORD);
1295
1296 err = 0;
1297 switch (t.type) {
1298 case T_MENU:
Rob Herringf0898b92012-12-02 21:00:25 -06001299 cfg->prompt = 1;
Steven Falco7de0fa82013-10-07 09:51:48 -04001300 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001301 break;
1302
1303 case T_TIMEOUT:
1304 err = parse_integer(&p, &cfg->timeout);
1305 break;
1306
1307 case T_LABEL:
1308 err = parse_label(&p, cfg);
1309 break;
1310
1311 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001312 case T_ONTIMEOUT:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001313 err = parse_sliteral(&p, &label_name);
1314
1315 if (label_name) {
1316 if (cfg->default_label)
1317 free(cfg->default_label);
1318
1319 cfg->default_label = label_name;
1320 }
1321
1322 break;
1323
Rob Herring8e5abb02012-05-25 10:43:16 +00001324 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001325 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring8e5abb02012-05-25 10:43:16 +00001326 nest_level + 1);
1327 break;
1328
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001329 case T_PROMPT:
Rob Herringf0898b92012-12-02 21:00:25 -06001330 eol_or_eof(&p);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001331 break;
1332
1333 case T_EOL:
1334 break;
1335
1336 case T_EOF:
1337 return 1;
1338
1339 default:
1340 printf("Ignoring unknown command: %.*s\n",
1341 (int)(p - s), s);
1342 eol_or_eof(&p);
1343 }
1344
1345 if (err < 0)
1346 return err;
1347 }
1348}
1349
1350/*
1351 * Free the memory used by a pxe_menu and its labels.
1352 */
1353static void destroy_pxe_menu(struct pxe_menu *cfg)
1354{
1355 struct list_head *pos, *n;
1356 struct pxe_label *label;
1357
1358 if (cfg->title)
1359 free(cfg->title);
1360
1361 if (cfg->default_label)
1362 free(cfg->default_label);
1363
1364 list_for_each_safe(pos, n, &cfg->labels) {
1365 label = list_entry(pos, struct pxe_label, list);
1366
1367 label_destroy(label);
1368 }
1369
1370 free(cfg);
1371}
1372
1373/*
1374 * Entry point for parsing a pxe file. This is only used for the top level
1375 * file.
1376 *
1377 * Returns NULL if there is an error, otherwise, returns a pointer to a
1378 * pxe_menu struct populated with the results of parsing the pxe file (and any
1379 * files it includes). The resulting pxe_menu struct can be free()'d by using
1380 * the destroy_pxe_menu() function.
1381 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001382static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001383{
1384 struct pxe_menu *cfg;
1385
1386 cfg = malloc(sizeof(struct pxe_menu));
1387
1388 if (!cfg)
1389 return NULL;
1390
1391 memset(cfg, 0, sizeof(struct pxe_menu));
1392
1393 INIT_LIST_HEAD(&cfg->labels);
1394
Steven Falco7de0fa82013-10-07 09:51:48 -04001395 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001396 destroy_pxe_menu(cfg);
1397 return NULL;
1398 }
1399
1400 return cfg;
1401}
1402
1403/*
1404 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1405 * menu code.
1406 */
1407static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1408{
1409 struct pxe_label *label;
1410 struct list_head *pos;
1411 struct menu *m;
1412 int err;
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001413 int i = 1;
1414 char *default_num = NULL;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001415
1416 /*
1417 * Create a menu and add items for all the labels.
1418 */
Pali Rohárd0d8d3b2013-03-23 14:50:40 +00001419 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1420 NULL, NULL);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001421
1422 if (!m)
1423 return NULL;
1424
1425 list_for_each(pos, &cfg->labels) {
1426 label = list_entry(pos, struct pxe_label, list);
1427
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001428 sprintf(label->num, "%d", i++);
1429 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001430 menu_destroy(m);
1431 return NULL;
1432 }
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001433 if (cfg->default_label &&
Rob Herringac995832012-12-02 21:00:27 -06001434 (strcmp(label->name, cfg->default_label) == 0))
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001435 default_num = label->num;
1436
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001437 }
1438
1439 /*
1440 * After we've created items for each label in the menu, set the
1441 * menu's default label if one was specified.
1442 */
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001443 if (default_num) {
1444 err = menu_default_set(m, default_num);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001445 if (err != 1) {
1446 if (err != -ENOENT) {
1447 menu_destroy(m);
1448 return NULL;
1449 }
1450
1451 printf("Missing default: %s\n", cfg->default_label);
1452 }
1453 }
1454
1455 return m;
1456}
1457
1458/*
1459 * Try to boot any labels we have yet to attempt to boot.
1460 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001461static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001462{
1463 struct list_head *pos;
1464 struct pxe_label *label;
1465
1466 list_for_each(pos, &cfg->labels) {
1467 label = list_entry(pos, struct pxe_label, list);
1468
1469 if (!label->attempted)
Tom Rini5b5b1762013-09-24 09:05:08 -04001470 label_boot(cmdtp, label);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001471 }
1472}
1473
1474/*
1475 * Boot the system as prescribed by a pxe_menu.
1476 *
1477 * Use the menu system to either get the user's choice or the default, based
1478 * on config or user input. If there is no default or user's choice,
1479 * attempted to boot labels in the order they were given in pxe files.
1480 * If the default or user's choice fails to boot, attempt to boot other
1481 * labels in the order they were given in pxe files.
1482 *
1483 * If this function returns, there weren't any labels that successfully
1484 * booted, or the user interrupted the menu selection via ctrl+c.
1485 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001486static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001487{
1488 void *choice;
1489 struct menu *m;
1490 int err;
1491
1492 m = pxe_menu_to_menu(cfg);
1493 if (!m)
1494 return;
1495
1496 err = menu_get_choice(m, &choice);
1497
1498 menu_destroy(m);
1499
Jason Hobbs083b7a62011-11-07 03:07:15 +00001500 /*
1501 * err == 1 means we got a choice back from menu_get_choice.
1502 *
1503 * err == -ENOENT if the menu was setup to select the default but no
1504 * default was set. in that case, we should continue trying to boot
1505 * labels that haven't been attempted yet.
1506 *
1507 * otherwise, the user interrupted or there was some other error and
1508 * we give up.
1509 */
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001510
Rob Herring58d7f432012-12-02 21:00:22 -06001511 if (err == 1) {
Tom Rini5b5b1762013-09-24 09:05:08 -04001512 err = label_boot(cmdtp, choice);
Rob Herring58d7f432012-12-02 21:00:22 -06001513 if (!err)
1514 return;
1515 } else if (err != -ENOENT) {
Jason Hobbs083b7a62011-11-07 03:07:15 +00001516 return;
Rob Herring58d7f432012-12-02 21:00:22 -06001517 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001518
Tom Rini5b5b1762013-09-24 09:05:08 -04001519 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001520}
1521
Stephen Warren857291b2014-02-05 20:49:20 -07001522#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001523/*
1524 * Boots a system using a pxe file
1525 *
1526 * Returns 0 on success, 1 on error.
1527 */
1528static int
1529do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1530{
1531 unsigned long pxefile_addr_r;
1532 struct pxe_menu *cfg;
1533 char *pxefile_addr_str;
1534
Rob Herringeee675f2012-05-25 10:47:39 +00001535 do_getfile = do_get_tftp;
1536
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001537 if (argc == 1) {
1538 pxefile_addr_str = from_env("pxefile_addr_r");
1539 if (!pxefile_addr_str)
1540 return 1;
1541
1542 } else if (argc == 2) {
1543 pxefile_addr_str = argv[1];
1544 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +00001545 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001546 }
1547
1548 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1549 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1550 return 1;
1551 }
1552
Steven Falco7de0fa82013-10-07 09:51:48 -04001553 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001554
1555 if (cfg == NULL) {
1556 printf("Error parsing config file\n");
1557 return 1;
1558 }
1559
Tom Rini5b5b1762013-09-24 09:05:08 -04001560 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001561
1562 destroy_pxe_menu(cfg);
1563
Stephen Warrenc24cbc22014-07-22 18:06:46 -06001564 copy_filename(BootFile, "", sizeof(BootFile));
1565
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001566 return 0;
1567}
1568
1569static cmd_tbl_t cmd_pxe_sub[] = {
1570 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1571 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1572};
1573
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +02001574static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001575{
1576 cmd_tbl_t *cp;
1577
1578 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001579 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001580
Rob Herring759456e2013-10-18 13:04:42 -05001581 is_pxe = true;
1582
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001583 /* drop initial "pxe" arg */
1584 argc--;
1585 argv++;
1586
1587 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1588
1589 if (cp)
1590 return cp->cmd(cmdtp, flag, argc, argv);
1591
Simon Glassa06dfc72011-12-10 08:44:01 +00001592 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001593}
1594
1595U_BOOT_CMD(
1596 pxe, 3, 1, do_pxe,
1597 "commands to get and boot from pxe files",
1598 "get - try to retrieve a pxe file using tftp\npxe "
1599 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1600);
Stephen Warren857291b2014-02-05 20:49:20 -07001601#endif
Rob Herringeee675f2012-05-25 10:47:39 +00001602
1603/*
1604 * Boots a system using a local disk syslinux/extlinux file
1605 *
1606 * Returns 0 on success, 1 on error.
1607 */
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +02001608static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Rob Herringeee675f2012-05-25 10:47:39 +00001609{
1610 unsigned long pxefile_addr_r;
1611 struct pxe_menu *cfg;
1612 char *pxefile_addr_str;
1613 char *filename;
1614 int prompt = 0;
1615
Rob Herring759456e2013-10-18 13:04:42 -05001616 is_pxe = false;
1617
Rob Herringeee675f2012-05-25 10:47:39 +00001618 if (strstr(argv[1], "-p")) {
1619 prompt = 1;
1620 argc--;
1621 argv++;
1622 }
1623
1624 if (argc < 4)
1625 return cmd_usage(cmdtp);
1626
1627 if (argc < 5) {
1628 pxefile_addr_str = from_env("pxefile_addr_r");
1629 if (!pxefile_addr_str)
1630 return 1;
1631 } else {
1632 pxefile_addr_str = argv[4];
1633 }
1634
1635 if (argc < 6)
1636 filename = getenv("bootfile");
1637 else {
1638 filename = argv[5];
1639 setenv("bootfile", filename);
1640 }
1641
1642 if (strstr(argv[3], "ext2"))
1643 do_getfile = do_get_ext2;
1644 else if (strstr(argv[3], "fat"))
1645 do_getfile = do_get_fat;
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001646 else if (strstr(argv[3], "any"))
1647 do_getfile = do_get_any;
Rob Herringeee675f2012-05-25 10:47:39 +00001648 else {
1649 printf("Invalid filesystem: %s\n", argv[3]);
1650 return 1;
1651 }
1652 fs_argv[1] = argv[1];
1653 fs_argv[2] = argv[2];
1654
1655 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1656 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1657 return 1;
1658 }
1659
Steven Falco7de0fa82013-10-07 09:51:48 -04001660 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herringeee675f2012-05-25 10:47:39 +00001661 printf("Error reading config file\n");
1662 return 1;
1663 }
1664
Steven Falco7de0fa82013-10-07 09:51:48 -04001665 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herringeee675f2012-05-25 10:47:39 +00001666
1667 if (cfg == NULL) {
1668 printf("Error parsing config file\n");
1669 return 1;
1670 }
1671
1672 if (prompt)
1673 cfg->prompt = 1;
1674
Tom Rini5b5b1762013-09-24 09:05:08 -04001675 handle_pxe_menu(cmdtp, cfg);
Rob Herringeee675f2012-05-25 10:47:39 +00001676
1677 destroy_pxe_menu(cfg);
1678
1679 return 0;
1680}
1681
1682U_BOOT_CMD(
1683 sysboot, 7, 1, do_sysboot,
1684 "command to get and boot from syslinux files",
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001685 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1686 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1687 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herringeee675f2012-05-25 10:47:39 +00001688);