blob: 0ab1e0aaa63fa13e8b60a78ea3b7fb1a9698b293 [file] [log] [blame]
Jason Hobbs06283a62011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wu1fb7d0e2014-07-31 17:39:59 -07003 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs06283a62011-08-31 10:37:30 -05004 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs06283a62011-08-31 10:37:30 -05006 */
Wolfgang Denk1a459662013-07-08 09:37:19 +02007
Jason Hobbs06283a62011-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 Gilmore6d1a3e52014-02-04 05:25:46 -060015#include <fs.h>
Jason Hobbs06283a62011-08-31 10:37:30 -050016
17#include "menu.h"
Hans de Goedeb1ba62d2014-08-06 09:37:39 +020018#include "cli.h"
Jason Hobbs06283a62011-08-31 10:37:30 -050019
20#define MAX_TFTP_PATH_LEN 127
21
Rob Herring39f98552012-12-02 21:00:28 -060022const char *pxe_default_paths[] = {
Joe Hershberger58d9ff92013-06-24 17:21:04 -050023#ifdef CONFIG_SYS_SOC
Rob Herring39f98552012-12-02 21:00:28 -060024 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger58d9ff92013-06-24 17:21:04 -050025#endif
Rob Herring39f98552012-12-02 21:00:28 -060026 "default-" CONFIG_SYS_ARCH,
27 "default",
28 NULL
29};
30
Rob Herringe5a9a402013-10-18 13:04:42 -050031static bool is_pxe;
32
Jason Hobbs06283a62011-08-31 10:37:30 -050033/*
34 * Like getenv, but prints an error if envvar isn't defined in the
35 * environment. It always returns what getenv does, so it can be used in
36 * place of getenv without changing error handling otherwise.
37 */
Rob Herring23b71942012-12-02 21:00:21 -060038static char *from_env(const char *envvar)
Jason Hobbs06283a62011-08-31 10:37:30 -050039{
40 char *ret;
41
42 ret = getenv(envvar);
43
44 if (!ret)
45 printf("missing environment variable: %s\n", envvar);
46
47 return ret;
48}
49
Stephen Warrenb81fdb02014-02-05 20:49:20 -070050#ifdef CONFIG_CMD_NET
Jason Hobbs06283a62011-08-31 10:37:30 -050051/*
52 * Convert an ethaddr from the environment to the format used by pxelinux
53 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
54 * the beginning of the ethernet address to indicate a hardware type of
55 * Ethernet. Also converts uppercase hex characters into lowercase, to match
56 * pxelinux's behavior.
57 *
58 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
59 * environment, or some other value < 0 on error.
60 */
61static int format_mac_pxe(char *outbuf, size_t outbuf_len)
62{
Rob Herringef034c92012-12-02 21:00:20 -060063 uchar ethaddr[6];
Jason Hobbs06283a62011-08-31 10:37:30 -050064
Rob Herringef034c92012-12-02 21:00:20 -060065 if (outbuf_len < 21) {
David Feng5cea95c2013-12-14 11:47:30 +080066 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs06283a62011-08-31 10:37:30 -050067
68 return -EINVAL;
69 }
70
Rob Herringef034c92012-12-02 21:00:20 -060071 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
72 ethaddr))
73 return -ENOENT;
Jason Hobbs06283a62011-08-31 10:37:30 -050074
Rob Herringef034c92012-12-02 21:00:20 -060075 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
76 ethaddr[0], ethaddr[1], ethaddr[2],
77 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs06283a62011-08-31 10:37:30 -050078
79 return 1;
80}
Stephen Warrenb81fdb02014-02-05 20:49:20 -070081#endif
Jason Hobbs06283a62011-08-31 10:37:30 -050082
83/*
84 * Returns the directory the file specified in the bootfile env variable is
85 * in. If bootfile isn't defined in the environment, return NULL, which should
86 * be interpreted as "don't prepend anything to paths".
87 */
Rob Herring90ba7d72012-03-28 05:51:36 +000088static int get_bootfile_path(const char *file_path, char *bootfile_path,
89 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -050090{
91 char *bootfile, *last_slash;
Rob Herring90ba7d72012-03-28 05:51:36 +000092 size_t path_len = 0;
93
Rob Herringe5a9a402013-10-18 13:04:42 -050094 /* Only syslinux allows absolute paths */
95 if (file_path[0] == '/' && !is_pxe)
Rob Herring90ba7d72012-03-28 05:51:36 +000096 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050097
98 bootfile = from_env("bootfile");
99
Rob Herring90ba7d72012-03-28 05:51:36 +0000100 if (!bootfile)
101 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500102
103 last_slash = strrchr(bootfile, '/');
104
Rob Herring90ba7d72012-03-28 05:51:36 +0000105 if (last_slash == NULL)
106 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500107
108 path_len = (last_slash - bootfile) + 1;
109
110 if (bootfile_path_size < path_len) {
David Feng5cea95c2013-12-14 11:47:30 +0800111 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs06283a62011-08-31 10:37:30 -0500112 bootfile_path_size, path_len);
113
114 return -1;
115 }
116
117 strncpy(bootfile_path, bootfile, path_len);
118
Rob Herring90ba7d72012-03-28 05:51:36 +0000119 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500120 bootfile_path[path_len] = '\0';
121
122 return 1;
123}
124
Steven Falco0e3f3f82013-10-07 09:51:48 -0400125static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herring669df7e2012-05-25 10:47:39 +0000126
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700127#ifdef CONFIG_CMD_NET
Steven Falco0e3f3f82013-10-07 09:51:48 -0400128static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000129{
130 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
131
132 tftp_argv[1] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600133 tftp_argv[2] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000134
Steven Falco0e3f3f82013-10-07 09:51:48 -0400135 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000136 return -ENOENT;
137
138 return 1;
139}
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700140#endif
Rob Herring669df7e2012-05-25 10:47:39 +0000141
142static char *fs_argv[5];
143
Steven Falco0e3f3f82013-10-07 09:51:48 -0400144static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000145{
146#ifdef CONFIG_CMD_EXT2
147 fs_argv[0] = "ext2load";
148 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600149 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000150
Steven Falco0e3f3f82013-10-07 09:51:48 -0400151 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000152 return 1;
153#endif
154 return -ENOENT;
155}
156
Steven Falco0e3f3f82013-10-07 09:51:48 -0400157static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000158{
159#ifdef CONFIG_CMD_FAT
160 fs_argv[0] = "fatload";
161 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600162 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000163
Steven Falco0e3f3f82013-10-07 09:51:48 -0400164 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000165 return 1;
166#endif
167 return -ENOENT;
168}
169
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -0600170static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
171{
172#ifdef CONFIG_CMD_FS_GENERIC
173 fs_argv[0] = "load";
174 fs_argv[3] = file_addr;
175 fs_argv[4] = (void *)file_path;
176
177 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
178 return 1;
179#endif
180 return -ENOENT;
181}
182
Jason Hobbs06283a62011-08-31 10:37:30 -0500183/*
184 * As in pxelinux, paths to files referenced from files we retrieve are
185 * relative to the location of bootfile. get_relfile takes such a path and
186 * joins it with the bootfile path to get the full path to the target file. If
187 * the bootfile path is NULL, we use file_path as is.
188 *
189 * Returns 1 for success, or < 0 on error.
190 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400191static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500192{
193 size_t path_len;
194 char relfile[MAX_TFTP_PATH_LEN+1];
195 char addr_buf[10];
Jason Hobbs06283a62011-08-31 10:37:30 -0500196 int err;
197
Rob Herring90ba7d72012-03-28 05:51:36 +0000198 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-08-31 10:37:30 -0500199
200 if (err < 0)
201 return err;
202
203 path_len = strlen(file_path);
204 path_len += strlen(relfile);
205
206 if (path_len > MAX_TFTP_PATH_LEN) {
207 printf("Base path too long (%s%s)\n",
208 relfile,
209 file_path);
210
211 return -ENAMETOOLONG;
212 }
213
214 strcat(relfile, file_path);
215
216 printf("Retrieving file: %s\n", relfile);
217
218 sprintf(addr_buf, "%p", file_addr);
219
Steven Falco0e3f3f82013-10-07 09:51:48 -0400220 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs06283a62011-08-31 10:37:30 -0500221}
222
223/*
224 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
225 * 'bootfile' was specified in the environment, the path to bootfile will be
226 * prepended to 'file_path' and the resulting path will be used.
227 *
228 * Returns 1 on success, or < 0 for error.
229 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400230static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500231{
232 unsigned long config_file_size;
233 char *tftp_filesize;
234 int err;
235
Steven Falco0e3f3f82013-10-07 09:51:48 -0400236 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500237
238 if (err < 0)
239 return err;
240
241 /*
242 * the file comes without a NUL byte at the end, so find out its size
243 * and add the NUL byte.
244 */
245 tftp_filesize = from_env("filesize");
246
247 if (!tftp_filesize)
248 return -ENOENT;
249
250 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
251 return -EINVAL;
252
253 *(char *)(file_addr + config_file_size) = '\0';
254
255 return 1;
256}
257
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700258#ifdef CONFIG_CMD_NET
259
Jason Hobbs06283a62011-08-31 10:37:30 -0500260#define PXELINUX_DIR "pxelinux.cfg/"
261
262/*
263 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
264 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
265 * from the bootfile path, as described above.
266 *
267 * Returns 1 on success or < 0 on error.
268 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400269static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500270{
271 size_t base_len = strlen(PXELINUX_DIR);
272 char path[MAX_TFTP_PATH_LEN+1];
273
274 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
275 printf("path (%s%s) too long, skipping\n",
276 PXELINUX_DIR, file);
277 return -ENAMETOOLONG;
278 }
279
280 sprintf(path, PXELINUX_DIR "%s", file);
281
Steven Falco0e3f3f82013-10-07 09:51:48 -0400282 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500283}
284
285/*
286 * Looks for a pxe file with a name based on the pxeuuid environment variable.
287 *
288 * Returns 1 on success or < 0 on error.
289 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400290static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500291{
292 char *uuid_str;
293
294 uuid_str = from_env("pxeuuid");
295
296 if (!uuid_str)
297 return -ENOENT;
298
Steven Falco0e3f3f82013-10-07 09:51:48 -0400299 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500300}
301
302/*
303 * Looks for a pxe file with a name based on the 'ethaddr' environment
304 * variable.
305 *
306 * Returns 1 on success or < 0 on error.
307 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400308static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500309{
310 char mac_str[21];
311 int err;
312
313 err = format_mac_pxe(mac_str, sizeof(mac_str));
314
315 if (err < 0)
316 return err;
317
Steven Falco0e3f3f82013-10-07 09:51:48 -0400318 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500319}
320
321/*
322 * Looks for pxe files with names based on our IP address. See pxelinux
323 * documentation for details on what these file names look like. We match
324 * that exactly.
325 *
326 * Returns 1 on success or < 0 on error.
327 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400328static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500329{
330 char ip_addr[9];
331 int mask_pos, err;
332
333 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
334
335 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400336 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500337
338 if (err > 0)
339 return err;
340
341 ip_addr[mask_pos] = '\0';
342 }
343
344 return -ENOENT;
345}
346
347/*
348 * Entry point for the 'pxe get' command.
349 * This Follows pxelinux's rules to download a config file from a tftp server.
350 * The file is stored at the location given by the pxefile_addr_r environment
351 * variable, which must be set.
352 *
353 * UUID comes from pxeuuid env variable, if defined
354 * MAC addr comes from ethaddr env variable, if defined
355 * IP
356 *
357 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
358 *
359 * Returns 0 on success or 1 on error.
360 */
361static int
362do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
363{
364 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000365 unsigned long pxefile_addr_r;
Rob Herring39f98552012-12-02 21:00:28 -0600366 int err, i = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500367
Rob Herring669df7e2012-05-25 10:47:39 +0000368 do_getfile = do_get_tftp;
369
Jason Hobbs06283a62011-08-31 10:37:30 -0500370 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000371 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500372
Jason Hobbs06283a62011-08-31 10:37:30 -0500373 pxefile_addr_str = from_env("pxefile_addr_r");
374
375 if (!pxefile_addr_str)
376 return 1;
377
378 err = strict_strtoul(pxefile_addr_str, 16,
379 (unsigned long *)&pxefile_addr_r);
380 if (err < 0)
381 return 1;
382
383 /*
384 * Keep trying paths until we successfully get a file we're looking
385 * for.
386 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400387 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
388 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
389 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500390 printf("Config file found\n");
391
392 return 0;
393 }
394
Rob Herring39f98552012-12-02 21:00:28 -0600395 while (pxe_default_paths[i]) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400396 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herring39f98552012-12-02 21:00:28 -0600397 (void *)pxefile_addr_r) > 0) {
398 printf("Config file found\n");
399 return 0;
400 }
401 i++;
402 }
403
Jason Hobbs06283a62011-08-31 10:37:30 -0500404 printf("Config file not found\n");
405
406 return 1;
407}
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700408#endif
Jason Hobbs06283a62011-08-31 10:37:30 -0500409
410/*
411 * Wrapper to make it easier to store the file at file_path in the location
412 * specified by envaddr_name. file_path will be joined to the bootfile path,
413 * if any is specified.
414 *
415 * Returns 1 on success or < 0 on error.
416 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400417static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs06283a62011-08-31 10:37:30 -0500418{
Jason Hobbs834c9382012-03-05 08:12:28 +0000419 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500420 char *envaddr;
421
422 envaddr = from_env(envaddr_name);
423
424 if (!envaddr)
425 return -ENOENT;
426
Jason Hobbs834c9382012-03-05 08:12:28 +0000427 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500428 return -EINVAL;
429
Steven Falco0e3f3f82013-10-07 09:51:48 -0400430 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500431}
432
433/*
434 * A note on the pxe file parser.
435 *
436 * We're parsing files that use syslinux grammar, which has a few quirks.
437 * String literals must be recognized based on context - there is no
438 * quoting or escaping support. There's also nothing to explicitly indicate
439 * when a label section completes. We deal with that by ending a label
440 * section whenever we see a line that doesn't include.
441 *
442 * As with the syslinux family, this same file format could be reused in the
443 * future for non pxe purposes. The only action it takes during parsing that
444 * would throw this off is handling of include files. It assumes we're using
445 * pxe, and does a tftp download of a file listed as an include file in the
446 * middle of the parsing operation. That could be handled by refactoring it to
447 * take a 'include file getter' function.
448 */
449
450/*
451 * Describes a single label given in a pxe file.
452 *
453 * Create these with the 'label_create' function given below.
454 *
455 * name - the name of the menu as given on the 'menu label' line.
456 * kernel - the path to the kernel file to use for this label.
457 * append - kernel command line to use when booting this label
458 * initrd - path to the initrd to use for this label.
459 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
460 * localboot - 1 if this label specified 'localboot', 0 otherwise.
461 * list - lets these form a list, which a pxe_menu struct will hold.
462 */
463struct pxe_label {
Rob Herring32d2ffe2012-12-02 21:00:26 -0600464 char num[4];
Jason Hobbs06283a62011-08-31 10:37:30 -0500465 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000466 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500467 char *kernel;
468 char *append;
469 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000470 char *fdt;
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700471 char *fdtdir;
Rob Herring98f64672012-12-02 21:00:29 -0600472 int ipappend;
Jason Hobbs06283a62011-08-31 10:37:30 -0500473 int attempted;
474 int localboot;
Rob Herring500f3042012-12-02 21:00:22 -0600475 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500476 struct list_head list;
477};
478
479/*
480 * Describes a pxe menu as given via pxe files.
481 *
482 * title - the name of the menu as given by a 'menu title' line.
483 * default_label - the name of the default label, if any.
484 * timeout - time in tenths of a second to wait for a user key-press before
485 * booting the default label.
486 * prompt - if 0, don't prompt for a choice unless the timeout period is
487 * interrupted. If 1, always prompt for a choice regardless of
488 * timeout.
489 * labels - a list of labels defined for the menu.
490 */
491struct pxe_menu {
492 char *title;
493 char *default_label;
494 int timeout;
495 int prompt;
496 struct list_head labels;
497};
498
499/*
500 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
501 * result must be free()'d to reclaim the memory.
502 *
503 * Returns NULL if malloc fails.
504 */
505static struct pxe_label *label_create(void)
506{
507 struct pxe_label *label;
508
509 label = malloc(sizeof(struct pxe_label));
510
511 if (!label)
512 return NULL;
513
514 memset(label, 0, sizeof(struct pxe_label));
515
516 return label;
517}
518
519/*
520 * Free the memory used by a pxe_label, including that used by its name,
521 * kernel, append and initrd members, if they're non NULL.
522 *
523 * So - be sure to only use dynamically allocated memory for the members of
524 * the pxe_label struct, unless you want to clean it up first. These are
525 * currently only created by the pxe file parsing code.
526 */
527static void label_destroy(struct pxe_label *label)
528{
529 if (label->name)
530 free(label->name);
531
532 if (label->kernel)
533 free(label->kernel);
534
535 if (label->append)
536 free(label->append);
537
538 if (label->initrd)
539 free(label->initrd);
540
Chander Kashyapa6559382012-09-06 19:36:31 +0000541 if (label->fdt)
542 free(label->fdt);
543
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700544 if (label->fdtdir)
545 free(label->fdtdir);
546
Jason Hobbs06283a62011-08-31 10:37:30 -0500547 free(label);
548}
549
550/*
551 * Print a label and its string members if they're defined.
552 *
553 * This is passed as a callback to the menu code for displaying each
554 * menu entry.
555 */
556static void label_print(void *data)
557{
558 struct pxe_label *label = data;
Rob Herring32d2ffe2012-12-02 21:00:26 -0600559 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500560
Rob Herring32d2ffe2012-12-02 21:00:26 -0600561 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500562}
563
564/*
565 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
566 * environment variable is defined. Its contents will be executed as U-boot
567 * command. If the label specified an 'append' line, its contents will be
568 * used to overwrite the contents of the 'bootargs' environment variable prior
569 * to running 'localcmd'.
570 *
571 * Returns 1 on success or < 0 on error.
572 */
573static int label_localboot(struct pxe_label *label)
574{
Simon Glassd51004a2012-03-30 21:30:55 +0000575 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500576
577 localcmd = from_env("localcmd");
578
579 if (!localcmd)
580 return -ENOENT;
581
Hans de Goedeb1ba62d2014-08-06 09:37:39 +0200582 if (label->append) {
583 char bootargs[CONFIG_SYS_CBSIZE];
584
585 cli_simple_process_macros(label->append, bootargs);
586 setenv("bootargs", bootargs);
587 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500588
Simon Glassd51004a2012-03-30 21:30:55 +0000589 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500590
Simon Glassd51004a2012-03-30 21:30:55 +0000591 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500592}
593
594/*
595 * Boot according to the contents of a pxe_label.
596 *
597 * If we can't boot for any reason, we return. A successful boot never
598 * returns.
599 *
600 * The kernel will be stored in the location given by the 'kernel_addr_r'
601 * environment variable.
602 *
603 * If the label specifies an initrd file, it will be stored in the location
604 * given by the 'ramdisk_addr_r' environment variable.
605 *
606 * If the label specifies an 'append' line, its contents will overwrite that
607 * of the 'bootargs' environment variable.
608 */
Tom Rinid7884e02013-09-24 09:05:08 -0400609static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500610{
611 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600612 char initrd_str[22];
Rob Herring98f64672012-12-02 21:00:29 -0600613 char mac_str[29] = "";
614 char ip_str[68] = "";
Jason Hobbs06283a62011-08-31 10:37:30 -0500615 int bootm_argc = 3;
Rob Herring98f64672012-12-02 21:00:29 -0600616 int len = 0;
Bryan Wu1fb7d0e2014-07-31 17:39:59 -0700617 ulong kernel_addr;
618 void *buf;
Jason Hobbs06283a62011-08-31 10:37:30 -0500619
620 label_print(label);
621
622 label->attempted = 1;
623
624 if (label->localboot) {
Rob Herring500f3042012-12-02 21:00:22 -0600625 if (label->localboot_val >= 0)
626 label_localboot(label);
627 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500628 }
629
630 if (label->kernel == NULL) {
631 printf("No kernel given, skipping %s\n",
632 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600633 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500634 }
635
636 if (label->initrd) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400637 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500638 printf("Skipping %s for failure retrieving initrd\n",
639 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600640 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500641 }
642
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600643 bootm_argv[2] = initrd_str;
644 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
645 strcat(bootm_argv[2], ":");
646 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500647 } else {
648 bootm_argv[2] = "-";
649 }
650
Steven Falco0e3f3f82013-10-07 09:51:48 -0400651 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500652 printf("Skipping %s for failure retrieving kernel\n",
653 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600654 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500655 }
656
Rob Herring98f64672012-12-02 21:00:29 -0600657 if (label->ipappend & 0x1) {
658 sprintf(ip_str, " ip=%s:%s:%s:%s",
659 getenv("ipaddr"), getenv("serverip"),
660 getenv("gatewayip"), getenv("netmask"));
Rob Herring98f64672012-12-02 21:00:29 -0600661 }
662
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700663#ifdef CONFIG_CMD_NET
Rob Herring98f64672012-12-02 21:00:29 -0600664 if (label->ipappend & 0x2) {
665 int err;
666 strcpy(mac_str, " BOOTIF=");
667 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
668 if (err < 0)
669 mac_str[0] = '\0';
Rob Herring98f64672012-12-02 21:00:29 -0600670 }
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700671#endif
Rob Herring98f64672012-12-02 21:00:29 -0600672
Hans de Goedeb1ba62d2014-08-06 09:37:39 +0200673 if ((label->ipappend & 0x3) || label->append) {
674 char bootargs[CONFIG_SYS_CBSIZE] = "";
675 char finalbootargs[CONFIG_SYS_CBSIZE];
Rob Herring98f64672012-12-02 21:00:29 -0600676
Rob Herring98f64672012-12-02 21:00:29 -0600677 if (label->append)
678 strcpy(bootargs, label->append);
679 strcat(bootargs, ip_str);
680 strcat(bootargs, mac_str);
681
Hans de Goedeb1ba62d2014-08-06 09:37:39 +0200682 cli_simple_process_macros(bootargs, finalbootargs);
683 setenv("bootargs", finalbootargs);
684 printf("append: %s\n", finalbootargs);
Rob Herring32d2ffe2012-12-02 21:00:26 -0600685 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500686
687 bootm_argv[1] = getenv("kernel_addr_r");
688
689 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000690 * fdt usage is optional:
691 * It handles the following scenarios. All scenarios are exclusive
692 *
693 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
694 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
695 * and adjust argc appropriately.
696 *
697 * Scenario 2: If there is an fdt_addr specified, pass it along to
698 * bootm, and adjust argc appropriately.
699 *
700 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500701 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000702 bootm_argv[3] = getenv("fdt_addr_r");
703
704 /* if fdt label is defined then get fdt from server */
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700705 if (bootm_argv[3]) {
706 char *fdtfile = NULL;
707 char *fdtfilefree = NULL;
708
709 if (label->fdt) {
710 fdtfile = label->fdt;
711 } else if (label->fdtdir) {
Stephen Warrene22361a2014-02-12 14:30:04 -0700712 char *f1, *f2, *f3, *f4, *slash;
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700713
Stephen Warrene22361a2014-02-12 14:30:04 -0700714 f1 = getenv("fdtfile");
715 if (f1) {
716 f2 = "";
717 f3 = "";
718 f4 = "";
719 } else {
720 /*
721 * For complex cases where this code doesn't
722 * generate the correct filename, the board
723 * code should set $fdtfile during early boot,
724 * or the boot scripts should set $fdtfile
725 * before invoking "pxe" or "sysboot".
726 */
727 f1 = getenv("soc");
728 f2 = "-";
729 f3 = getenv("board");
730 f4 = ".dtb";
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700731 }
Stephen Warrene22361a2014-02-12 14:30:04 -0700732
733 len = strlen(label->fdtdir);
734 if (!len)
735 slash = "./";
736 else if (label->fdtdir[len - 1] != '/')
737 slash = "/";
738 else
739 slash = "";
740
741 len = strlen(label->fdtdir) + strlen(slash) +
742 strlen(f1) + strlen(f2) + strlen(f3) +
743 strlen(f4) + 1;
744 fdtfilefree = malloc(len);
745 if (!fdtfilefree) {
746 printf("malloc fail (FDT filename)\n");
747 return 1;
748 }
749
750 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
751 label->fdtdir, slash, f1, f2, f3, f4);
752 fdtfile = fdtfilefree;
Chander Kashyapa6559382012-09-06 19:36:31 +0000753 }
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700754
755 if (fdtfile) {
756 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
757 free(fdtfilefree);
758 if (err < 0) {
759 printf("Skipping %s for failure retrieving fdt\n",
760 label->name);
761 return 1;
762 }
763 } else {
764 bootm_argv[3] = NULL;
765 }
766 }
767
768 if (!bootm_argv[3])
Chander Kashyapa6559382012-09-06 19:36:31 +0000769 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500770
771 if (bootm_argv[3])
772 bootm_argc = 4;
773
Bryan Wu1fb7d0e2014-07-31 17:39:59 -0700774 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
775 buf = map_sysmem(kernel_addr, 0);
776 /* Try bootm for legacy and FIT format image */
777 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
778 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600779#ifdef CONFIG_CMD_BOOTZ
Bryan Wu1fb7d0e2014-07-31 17:39:59 -0700780 /* Try booting a zImage */
781 else
782 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600783#endif
Rob Herring500f3042012-12-02 21:00:22 -0600784 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500785}
786
787/*
788 * Tokens for the pxe file parser.
789 */
790enum token_type {
791 T_EOL,
792 T_STRING,
793 T_EOF,
794 T_MENU,
795 T_TITLE,
796 T_TIMEOUT,
797 T_LABEL,
798 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000799 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500800 T_APPEND,
801 T_INITRD,
802 T_LOCALBOOT,
803 T_DEFAULT,
804 T_PROMPT,
805 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000806 T_FDT,
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700807 T_FDTDIR,
Rob Herring8577fec2012-12-02 21:00:27 -0600808 T_ONTIMEOUT,
Rob Herring98f64672012-12-02 21:00:29 -0600809 T_IPAPPEND,
Jason Hobbs06283a62011-08-31 10:37:30 -0500810 T_INVALID
811};
812
813/*
814 * A token - given by a value and a type.
815 */
816struct token {
817 char *val;
818 enum token_type type;
819};
820
821/*
822 * Keywords recognized.
823 */
824static const struct token keywords[] = {
825 {"menu", T_MENU},
826 {"title", T_TITLE},
827 {"timeout", T_TIMEOUT},
828 {"default", T_DEFAULT},
829 {"prompt", T_PROMPT},
830 {"label", T_LABEL},
831 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000832 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500833 {"localboot", T_LOCALBOOT},
834 {"append", T_APPEND},
835 {"initrd", T_INITRD},
836 {"include", T_INCLUDE},
Stephen Warrenf43c4012014-01-28 14:50:09 -0700837 {"devicetree", T_FDT},
Chander Kashyapa6559382012-09-06 19:36:31 +0000838 {"fdt", T_FDT},
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700839 {"devicetreedir", T_FDTDIR},
840 {"fdtdir", T_FDTDIR},
Rob Herring8577fec2012-12-02 21:00:27 -0600841 {"ontimeout", T_ONTIMEOUT,},
Rob Herring98f64672012-12-02 21:00:29 -0600842 {"ipappend", T_IPAPPEND,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500843 {NULL, T_INVALID}
844};
845
846/*
847 * Since pxe(linux) files don't have a token to identify the start of a
848 * literal, we have to keep track of when we're in a state where a literal is
849 * expected vs when we're in a state a keyword is expected.
850 */
851enum lex_state {
852 L_NORMAL = 0,
853 L_KEYWORD,
854 L_SLITERAL
855};
856
857/*
858 * get_string retrieves a string from *p and stores it as a token in
859 * *t.
860 *
861 * get_string used for scanning both string literals and keywords.
862 *
863 * Characters from *p are copied into t-val until a character equal to
864 * delim is found, or a NUL byte is reached. If delim has the special value of
865 * ' ', any whitespace character will be used as a delimiter.
866 *
867 * If lower is unequal to 0, uppercase characters will be converted to
868 * lowercase in the result. This is useful to make keywords case
869 * insensitive.
870 *
871 * The location of *p is updated to point to the first character after the end
872 * of the token - the ending delimiter.
873 *
874 * On success, the new value of t->val is returned. Memory for t->val is
875 * allocated using malloc and must be free()'d to reclaim it. If insufficient
876 * memory is available, NULL is returned.
877 */
878static char *get_string(char **p, struct token *t, char delim, int lower)
879{
880 char *b, *e;
881 size_t len, i;
882
883 /*
884 * b and e both start at the beginning of the input stream.
885 *
886 * e is incremented until we find the ending delimiter, or a NUL byte
887 * is reached. Then, we take e - b to find the length of the token.
888 */
889 b = e = *p;
890
891 while (*e) {
892 if ((delim == ' ' && isspace(*e)) || delim == *e)
893 break;
894 e++;
895 }
896
897 len = e - b;
898
899 /*
900 * Allocate memory to hold the string, and copy it in, converting
901 * characters to lowercase if lower is != 0.
902 */
903 t->val = malloc(len + 1);
904 if (!t->val)
905 return NULL;
906
907 for (i = 0; i < len; i++, b++) {
908 if (lower)
909 t->val[i] = tolower(*b);
910 else
911 t->val[i] = *b;
912 }
913
914 t->val[len] = '\0';
915
916 /*
917 * Update *p so the caller knows where to continue scanning.
918 */
919 *p = e;
920
921 t->type = T_STRING;
922
923 return t->val;
924}
925
926/*
927 * Populate a keyword token with a type and value.
928 */
929static void get_keyword(struct token *t)
930{
931 int i;
932
933 for (i = 0; keywords[i].val; i++) {
934 if (!strcmp(t->val, keywords[i].val)) {
935 t->type = keywords[i].type;
936 break;
937 }
938 }
939}
940
941/*
942 * Get the next token. We have to keep track of which state we're in to know
943 * if we're looking to get a string literal or a keyword.
944 *
945 * *p is updated to point at the first character after the current token.
946 */
947static void get_token(char **p, struct token *t, enum lex_state state)
948{
949 char *c = *p;
950
951 t->type = T_INVALID;
952
953 /* eat non EOL whitespace */
954 while (isblank(*c))
955 c++;
956
957 /*
958 * eat comments. note that string literals can't begin with #, but
959 * can contain a # after their first character.
960 */
961 if (*c == '#') {
962 while (*c && *c != '\n')
963 c++;
964 }
965
966 if (*c == '\n') {
967 t->type = T_EOL;
968 c++;
969 } else if (*c == '\0') {
970 t->type = T_EOF;
971 c++;
972 } else if (state == L_SLITERAL) {
973 get_string(&c, t, '\n', 0);
974 } else if (state == L_KEYWORD) {
975 /*
976 * when we expect a keyword, we first get the next string
977 * token delimited by whitespace, and then check if it
978 * matches a keyword in our keyword list. if it does, it's
979 * converted to a keyword token of the appropriate type, and
980 * if not, it remains a string token.
981 */
982 get_string(&c, t, ' ', 1);
983 get_keyword(t);
984 }
985
986 *p = c;
987}
988
989/*
990 * Increment *c until we get to the end of the current line, or EOF.
991 */
992static void eol_or_eof(char **c)
993{
994 while (**c && **c != '\n')
995 (*c)++;
996}
997
998/*
999 * All of these parse_* functions share some common behavior.
1000 *
1001 * They finish with *c pointing after the token they parse, and return 1 on
1002 * success, or < 0 on error.
1003 */
1004
1005/*
1006 * Parse a string literal and store a pointer it at *dst. String literals
1007 * terminate at the end of the line.
1008 */
1009static int parse_sliteral(char **c, char **dst)
1010{
1011 struct token t;
1012 char *s = *c;
1013
1014 get_token(c, &t, L_SLITERAL);
1015
1016 if (t.type != T_STRING) {
1017 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1018 return -EINVAL;
1019 }
1020
1021 *dst = t.val;
1022
1023 return 1;
1024}
1025
1026/*
1027 * Parse a base 10 (unsigned) integer and store it at *dst.
1028 */
1029static int parse_integer(char **c, int *dst)
1030{
1031 struct token t;
1032 char *s = *c;
Jason Hobbs06283a62011-08-31 10:37:30 -05001033
1034 get_token(c, &t, L_SLITERAL);
1035
1036 if (t.type != T_STRING) {
1037 printf("Expected string: %.*s\n", (int)(*c - s), s);
1038 return -EINVAL;
1039 }
1040
Rob Herring500f3042012-12-02 21:00:22 -06001041 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -05001042
1043 free(t.val);
1044
1045 return 1;
1046}
1047
Steven Falco0e3f3f82013-10-07 09:51:48 -04001048static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001049
1050/*
1051 * Parse an include statement, and retrieve and parse the file it mentions.
1052 *
1053 * base should point to a location where it's safe to store the file, and
1054 * nest_level should indicate how many nested includes have occurred. For this
1055 * include, nest_level has already been incremented and doesn't need to be
1056 * incremented here.
1057 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001058static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs06283a62011-08-31 10:37:30 -05001059 struct pxe_menu *cfg, int nest_level)
1060{
1061 char *include_path;
1062 char *s = *c;
1063 int err;
1064
1065 err = parse_sliteral(c, &include_path);
1066
1067 if (err < 0) {
1068 printf("Expected include path: %.*s\n",
1069 (int)(*c - s), s);
1070 return err;
1071 }
1072
Steven Falco0e3f3f82013-10-07 09:51:48 -04001073 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs06283a62011-08-31 10:37:30 -05001074
1075 if (err < 0) {
1076 printf("Couldn't retrieve %s\n", include_path);
1077 return err;
1078 }
1079
Steven Falco0e3f3f82013-10-07 09:51:48 -04001080 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001081}
1082
1083/*
1084 * Parse lines that begin with 'menu'.
1085 *
1086 * b and nest are provided to handle the 'menu include' case.
1087 *
1088 * b should be the address where the file currently being parsed is stored.
1089 *
1090 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1091 * a file it includes, 3 when parsing a file included by that file, and so on.
1092 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001093static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
Jason Hobbs06283a62011-08-31 10:37:30 -05001094{
1095 struct token t;
1096 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +00001097 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -05001098
1099 get_token(c, &t, L_KEYWORD);
1100
1101 switch (t.type) {
1102 case T_TITLE:
1103 err = parse_sliteral(c, &cfg->title);
1104
1105 break;
1106
1107 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001108 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs06283a62011-08-31 10:37:30 -05001109 nest_level + 1);
1110 break;
1111
1112 default:
1113 printf("Ignoring malformed menu command: %.*s\n",
1114 (int)(*c - s), s);
1115 }
1116
1117 if (err < 0)
1118 return err;
1119
1120 eol_or_eof(c);
1121
1122 return 1;
1123}
1124
1125/*
1126 * Handles parsing a 'menu line' when we're parsing a label.
1127 */
1128static int parse_label_menu(char **c, struct pxe_menu *cfg,
1129 struct pxe_label *label)
1130{
1131 struct token t;
1132 char *s;
1133
1134 s = *c;
1135
1136 get_token(c, &t, L_KEYWORD);
1137
1138 switch (t.type) {
1139 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001140 if (!cfg->default_label)
1141 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001142
1143 if (!cfg->default_label)
1144 return -ENOMEM;
1145
1146 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001147 case T_LABEL:
1148 parse_sliteral(c, &label->menu);
1149 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001150 default:
1151 printf("Ignoring malformed menu command: %.*s\n",
1152 (int)(*c - s), s);
1153 }
1154
1155 eol_or_eof(c);
1156
1157 return 0;
1158}
1159
1160/*
1161 * Parses a label and adds it to the list of labels for a menu.
1162 *
1163 * A label ends when we either get to the end of a file, or
1164 * get some input we otherwise don't have a handler defined
1165 * for.
1166 *
1167 */
1168static int parse_label(char **c, struct pxe_menu *cfg)
1169{
1170 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001171 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001172 char *s = *c;
1173 struct pxe_label *label;
1174 int err;
1175
1176 label = label_create();
1177 if (!label)
1178 return -ENOMEM;
1179
1180 err = parse_sliteral(c, &label->name);
1181 if (err < 0) {
1182 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1183 label_destroy(label);
1184 return -EINVAL;
1185 }
1186
1187 list_add_tail(&label->list, &cfg->labels);
1188
1189 while (1) {
1190 s = *c;
1191 get_token(c, &t, L_KEYWORD);
1192
1193 err = 0;
1194 switch (t.type) {
1195 case T_MENU:
1196 err = parse_label_menu(c, cfg, label);
1197 break;
1198
1199 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001200 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001201 err = parse_sliteral(c, &label->kernel);
1202 break;
1203
1204 case T_APPEND:
1205 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001206 if (label->initrd)
1207 break;
1208 s = strstr(label->append, "initrd=");
1209 if (!s)
1210 break;
1211 s += 7;
1212 len = (int)(strchr(s, ' ') - s);
1213 label->initrd = malloc(len + 1);
1214 strncpy(label->initrd, s, len);
1215 label->initrd[len] = '\0';
1216
Jason Hobbs06283a62011-08-31 10:37:30 -05001217 break;
1218
1219 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001220 if (!label->initrd)
1221 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001222 break;
1223
Chander Kashyapa6559382012-09-06 19:36:31 +00001224 case T_FDT:
1225 if (!label->fdt)
1226 err = parse_sliteral(c, &label->fdt);
1227 break;
1228
Stephen Warrenc61d94d2014-01-28 14:50:10 -07001229 case T_FDTDIR:
1230 if (!label->fdtdir)
1231 err = parse_sliteral(c, &label->fdtdir);
1232 break;
1233
Jason Hobbs06283a62011-08-31 10:37:30 -05001234 case T_LOCALBOOT:
Rob Herring500f3042012-12-02 21:00:22 -06001235 label->localboot = 1;
1236 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001237 break;
1238
Rob Herring98f64672012-12-02 21:00:29 -06001239 case T_IPAPPEND:
1240 err = parse_integer(c, &label->ipappend);
1241 break;
1242
Jason Hobbs06283a62011-08-31 10:37:30 -05001243 case T_EOL:
1244 break;
1245
1246 default:
1247 /*
1248 * put the token back! we don't want it - it's the end
1249 * of a label and whatever token this is, it's
1250 * something for the menu level context to handle.
1251 */
1252 *c = s;
1253 return 1;
1254 }
1255
1256 if (err < 0)
1257 return err;
1258 }
1259}
1260
1261/*
1262 * This 16 comes from the limit pxelinux imposes on nested includes.
1263 *
1264 * There is no reason at all we couldn't do more, but some limit helps prevent
1265 * infinite (until crash occurs) recursion if a file tries to include itself.
1266 */
1267#define MAX_NEST_LEVEL 16
1268
1269/*
1270 * Entry point for parsing a menu file. nest_level indicates how many times
1271 * we've nested in includes. It will be 1 for the top level menu file.
1272 *
1273 * Returns 1 on success, < 0 on error.
1274 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001275static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs06283a62011-08-31 10:37:30 -05001276{
1277 struct token t;
1278 char *s, *b, *label_name;
1279 int err;
1280
1281 b = p;
1282
1283 if (nest_level > MAX_NEST_LEVEL) {
1284 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1285 return -EMLINK;
1286 }
1287
1288 while (1) {
1289 s = p;
1290
1291 get_token(&p, &t, L_KEYWORD);
1292
1293 err = 0;
1294 switch (t.type) {
1295 case T_MENU:
Rob Herringe82eeb52012-12-02 21:00:25 -06001296 cfg->prompt = 1;
Steven Falco0e3f3f82013-10-07 09:51:48 -04001297 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001298 break;
1299
1300 case T_TIMEOUT:
1301 err = parse_integer(&p, &cfg->timeout);
1302 break;
1303
1304 case T_LABEL:
1305 err = parse_label(&p, cfg);
1306 break;
1307
1308 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001309 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001310 err = parse_sliteral(&p, &label_name);
1311
1312 if (label_name) {
1313 if (cfg->default_label)
1314 free(cfg->default_label);
1315
1316 cfg->default_label = label_name;
1317 }
1318
1319 break;
1320
Rob Herring1e085222012-05-25 10:43:16 +00001321 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001322 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring1e085222012-05-25 10:43:16 +00001323 nest_level + 1);
1324 break;
1325
Jason Hobbs06283a62011-08-31 10:37:30 -05001326 case T_PROMPT:
Rob Herringe82eeb52012-12-02 21:00:25 -06001327 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001328 break;
1329
1330 case T_EOL:
1331 break;
1332
1333 case T_EOF:
1334 return 1;
1335
1336 default:
1337 printf("Ignoring unknown command: %.*s\n",
1338 (int)(p - s), s);
1339 eol_or_eof(&p);
1340 }
1341
1342 if (err < 0)
1343 return err;
1344 }
1345}
1346
1347/*
1348 * Free the memory used by a pxe_menu and its labels.
1349 */
1350static void destroy_pxe_menu(struct pxe_menu *cfg)
1351{
1352 struct list_head *pos, *n;
1353 struct pxe_label *label;
1354
1355 if (cfg->title)
1356 free(cfg->title);
1357
1358 if (cfg->default_label)
1359 free(cfg->default_label);
1360
1361 list_for_each_safe(pos, n, &cfg->labels) {
1362 label = list_entry(pos, struct pxe_label, list);
1363
1364 label_destroy(label);
1365 }
1366
1367 free(cfg);
1368}
1369
1370/*
1371 * Entry point for parsing a pxe file. This is only used for the top level
1372 * file.
1373 *
1374 * Returns NULL if there is an error, otherwise, returns a pointer to a
1375 * pxe_menu struct populated with the results of parsing the pxe file (and any
1376 * files it includes). The resulting pxe_menu struct can be free()'d by using
1377 * the destroy_pxe_menu() function.
1378 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001379static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001380{
1381 struct pxe_menu *cfg;
1382
1383 cfg = malloc(sizeof(struct pxe_menu));
1384
1385 if (!cfg)
1386 return NULL;
1387
1388 memset(cfg, 0, sizeof(struct pxe_menu));
1389
1390 INIT_LIST_HEAD(&cfg->labels);
1391
Steven Falco0e3f3f82013-10-07 09:51:48 -04001392 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001393 destroy_pxe_menu(cfg);
1394 return NULL;
1395 }
1396
1397 return cfg;
1398}
1399
1400/*
1401 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1402 * menu code.
1403 */
1404static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1405{
1406 struct pxe_label *label;
1407 struct list_head *pos;
1408 struct menu *m;
1409 int err;
Rob Herring32d2ffe2012-12-02 21:00:26 -06001410 int i = 1;
1411 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001412
1413 /*
1414 * Create a menu and add items for all the labels.
1415 */
Pali Rohárfc9d64f2013-03-23 14:50:40 +00001416 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1417 NULL, NULL);
Jason Hobbs06283a62011-08-31 10:37:30 -05001418
1419 if (!m)
1420 return NULL;
1421
1422 list_for_each(pos, &cfg->labels) {
1423 label = list_entry(pos, struct pxe_label, list);
1424
Rob Herring32d2ffe2012-12-02 21:00:26 -06001425 sprintf(label->num, "%d", i++);
1426 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001427 menu_destroy(m);
1428 return NULL;
1429 }
Rob Herring32d2ffe2012-12-02 21:00:26 -06001430 if (cfg->default_label &&
Rob Herring8577fec2012-12-02 21:00:27 -06001431 (strcmp(label->name, cfg->default_label) == 0))
Rob Herring32d2ffe2012-12-02 21:00:26 -06001432 default_num = label->num;
1433
Jason Hobbs06283a62011-08-31 10:37:30 -05001434 }
1435
1436 /*
1437 * After we've created items for each label in the menu, set the
1438 * menu's default label if one was specified.
1439 */
Rob Herring32d2ffe2012-12-02 21:00:26 -06001440 if (default_num) {
1441 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001442 if (err != 1) {
1443 if (err != -ENOENT) {
1444 menu_destroy(m);
1445 return NULL;
1446 }
1447
1448 printf("Missing default: %s\n", cfg->default_label);
1449 }
1450 }
1451
1452 return m;
1453}
1454
1455/*
1456 * Try to boot any labels we have yet to attempt to boot.
1457 */
Tom Rinid7884e02013-09-24 09:05:08 -04001458static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001459{
1460 struct list_head *pos;
1461 struct pxe_label *label;
1462
1463 list_for_each(pos, &cfg->labels) {
1464 label = list_entry(pos, struct pxe_label, list);
1465
1466 if (!label->attempted)
Tom Rinid7884e02013-09-24 09:05:08 -04001467 label_boot(cmdtp, label);
Jason Hobbs06283a62011-08-31 10:37:30 -05001468 }
1469}
1470
1471/*
1472 * Boot the system as prescribed by a pxe_menu.
1473 *
1474 * Use the menu system to either get the user's choice or the default, based
1475 * on config or user input. If there is no default or user's choice,
1476 * attempted to boot labels in the order they were given in pxe files.
1477 * If the default or user's choice fails to boot, attempt to boot other
1478 * labels in the order they were given in pxe files.
1479 *
1480 * If this function returns, there weren't any labels that successfully
1481 * booted, or the user interrupted the menu selection via ctrl+c.
1482 */
Tom Rinid7884e02013-09-24 09:05:08 -04001483static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001484{
1485 void *choice;
1486 struct menu *m;
1487 int err;
1488
1489 m = pxe_menu_to_menu(cfg);
1490 if (!m)
1491 return;
1492
1493 err = menu_get_choice(m, &choice);
1494
1495 menu_destroy(m);
1496
Jason Hobbs6f40f272011-11-07 03:07:15 +00001497 /*
1498 * err == 1 means we got a choice back from menu_get_choice.
1499 *
1500 * err == -ENOENT if the menu was setup to select the default but no
1501 * default was set. in that case, we should continue trying to boot
1502 * labels that haven't been attempted yet.
1503 *
1504 * otherwise, the user interrupted or there was some other error and
1505 * we give up.
1506 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001507
Rob Herring500f3042012-12-02 21:00:22 -06001508 if (err == 1) {
Tom Rinid7884e02013-09-24 09:05:08 -04001509 err = label_boot(cmdtp, choice);
Rob Herring500f3042012-12-02 21:00:22 -06001510 if (!err)
1511 return;
1512 } else if (err != -ENOENT) {
Jason Hobbs6f40f272011-11-07 03:07:15 +00001513 return;
Rob Herring500f3042012-12-02 21:00:22 -06001514 }
Jason Hobbs06283a62011-08-31 10:37:30 -05001515
Tom Rinid7884e02013-09-24 09:05:08 -04001516 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001517}
1518
Stephen Warrenb81fdb02014-02-05 20:49:20 -07001519#ifdef CONFIG_CMD_NET
Jason Hobbs06283a62011-08-31 10:37:30 -05001520/*
1521 * Boots a system using a pxe file
1522 *
1523 * Returns 0 on success, 1 on error.
1524 */
1525static int
1526do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1527{
1528 unsigned long pxefile_addr_r;
1529 struct pxe_menu *cfg;
1530 char *pxefile_addr_str;
1531
Rob Herring669df7e2012-05-25 10:47:39 +00001532 do_getfile = do_get_tftp;
1533
Jason Hobbs06283a62011-08-31 10:37:30 -05001534 if (argc == 1) {
1535 pxefile_addr_str = from_env("pxefile_addr_r");
1536 if (!pxefile_addr_str)
1537 return 1;
1538
1539 } else if (argc == 2) {
1540 pxefile_addr_str = argv[1];
1541 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001542 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001543 }
1544
1545 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1546 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1547 return 1;
1548 }
1549
Steven Falco0e3f3f82013-10-07 09:51:48 -04001550 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs06283a62011-08-31 10:37:30 -05001551
1552 if (cfg == NULL) {
1553 printf("Error parsing config file\n");
1554 return 1;
1555 }
1556
Tom Rinid7884e02013-09-24 09:05:08 -04001557 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001558
1559 destroy_pxe_menu(cfg);
1560
Stephen Warrended2e202014-07-22 18:06:46 -06001561 copy_filename(BootFile, "", sizeof(BootFile));
1562
Jason Hobbs06283a62011-08-31 10:37:30 -05001563 return 0;
1564}
1565
1566static cmd_tbl_t cmd_pxe_sub[] = {
1567 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1568 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1569};
1570
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001571static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -05001572{
1573 cmd_tbl_t *cp;
1574
1575 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001576 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001577
Rob Herringe5a9a402013-10-18 13:04:42 -05001578 is_pxe = true;
1579
Jason Hobbs06283a62011-08-31 10:37:30 -05001580 /* drop initial "pxe" arg */
1581 argc--;
1582 argv++;
1583
1584 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1585
1586 if (cp)
1587 return cp->cmd(cmdtp, flag, argc, argv);
1588
Simon Glass4c12eeb2011-12-10 08:44:01 +00001589 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001590}
1591
1592U_BOOT_CMD(
1593 pxe, 3, 1, do_pxe,
1594 "commands to get and boot from pxe files",
1595 "get - try to retrieve a pxe file using tftp\npxe "
1596 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1597);
Stephen Warrenb81fdb02014-02-05 20:49:20 -07001598#endif
Rob Herring669df7e2012-05-25 10:47:39 +00001599
1600/*
1601 * Boots a system using a local disk syslinux/extlinux file
1602 *
1603 * Returns 0 on success, 1 on error.
1604 */
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001605static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Rob Herring669df7e2012-05-25 10:47:39 +00001606{
1607 unsigned long pxefile_addr_r;
1608 struct pxe_menu *cfg;
1609 char *pxefile_addr_str;
1610 char *filename;
1611 int prompt = 0;
1612
Rob Herringe5a9a402013-10-18 13:04:42 -05001613 is_pxe = false;
1614
Rob Herring669df7e2012-05-25 10:47:39 +00001615 if (strstr(argv[1], "-p")) {
1616 prompt = 1;
1617 argc--;
1618 argv++;
1619 }
1620
1621 if (argc < 4)
1622 return cmd_usage(cmdtp);
1623
1624 if (argc < 5) {
1625 pxefile_addr_str = from_env("pxefile_addr_r");
1626 if (!pxefile_addr_str)
1627 return 1;
1628 } else {
1629 pxefile_addr_str = argv[4];
1630 }
1631
1632 if (argc < 6)
1633 filename = getenv("bootfile");
1634 else {
1635 filename = argv[5];
1636 setenv("bootfile", filename);
1637 }
1638
1639 if (strstr(argv[3], "ext2"))
1640 do_getfile = do_get_ext2;
1641 else if (strstr(argv[3], "fat"))
1642 do_getfile = do_get_fat;
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001643 else if (strstr(argv[3], "any"))
1644 do_getfile = do_get_any;
Rob Herring669df7e2012-05-25 10:47:39 +00001645 else {
1646 printf("Invalid filesystem: %s\n", argv[3]);
1647 return 1;
1648 }
1649 fs_argv[1] = argv[1];
1650 fs_argv[2] = argv[2];
1651
1652 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1653 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1654 return 1;
1655 }
1656
Steven Falco0e3f3f82013-10-07 09:51:48 -04001657 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herring669df7e2012-05-25 10:47:39 +00001658 printf("Error reading config file\n");
1659 return 1;
1660 }
1661
Steven Falco0e3f3f82013-10-07 09:51:48 -04001662 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herring669df7e2012-05-25 10:47:39 +00001663
1664 if (cfg == NULL) {
1665 printf("Error parsing config file\n");
1666 return 1;
1667 }
1668
1669 if (prompt)
1670 cfg->prompt = 1;
1671
Tom Rinid7884e02013-09-24 09:05:08 -04001672 handle_pxe_menu(cmdtp, cfg);
Rob Herring669df7e2012-05-25 10:47:39 +00001673
1674 destroy_pxe_menu(cfg);
1675
1676 return 0;
1677}
1678
1679U_BOOT_CMD(
1680 sysboot, 7, 1, do_sysboot,
1681 "command to get and boot from syslinux files",
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001682 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1683 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1684 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herring669df7e2012-05-25 10:47:39 +00001685);