blob: 7e32c95df3217bc575dd39be2a71fd3f6e0deea3 [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
Ian Campbell64a0c242014-10-03 14:29:01 +0100677 if (strlen(label->append ?: "") +
678 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
679 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
680 strlen(label->append ?: ""),
681 strlen(ip_str), strlen(mac_str),
682 sizeof(bootargs));
683 return 1;
684 }
685
Rob Herring98f64672012-12-02 21:00:29 -0600686 if (label->append)
687 strcpy(bootargs, label->append);
688 strcat(bootargs, ip_str);
689 strcat(bootargs, mac_str);
690
Hans de Goedeb1ba62d2014-08-06 09:37:39 +0200691 cli_simple_process_macros(bootargs, finalbootargs);
692 setenv("bootargs", finalbootargs);
693 printf("append: %s\n", finalbootargs);
Rob Herring32d2ffe2012-12-02 21:00:26 -0600694 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500695
696 bootm_argv[1] = getenv("kernel_addr_r");
697
698 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000699 * fdt usage is optional:
700 * It handles the following scenarios. All scenarios are exclusive
701 *
702 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
703 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
704 * and adjust argc appropriately.
705 *
706 * Scenario 2: If there is an fdt_addr specified, pass it along to
707 * bootm, and adjust argc appropriately.
708 *
709 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500710 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000711 bootm_argv[3] = getenv("fdt_addr_r");
712
713 /* if fdt label is defined then get fdt from server */
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700714 if (bootm_argv[3]) {
715 char *fdtfile = NULL;
716 char *fdtfilefree = NULL;
717
718 if (label->fdt) {
719 fdtfile = label->fdt;
720 } else if (label->fdtdir) {
Stephen Warrene22361a2014-02-12 14:30:04 -0700721 char *f1, *f2, *f3, *f4, *slash;
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700722
Stephen Warrene22361a2014-02-12 14:30:04 -0700723 f1 = getenv("fdtfile");
724 if (f1) {
725 f2 = "";
726 f3 = "";
727 f4 = "";
728 } else {
729 /*
730 * For complex cases where this code doesn't
731 * generate the correct filename, the board
732 * code should set $fdtfile during early boot,
733 * or the boot scripts should set $fdtfile
734 * before invoking "pxe" or "sysboot".
735 */
736 f1 = getenv("soc");
737 f2 = "-";
738 f3 = getenv("board");
739 f4 = ".dtb";
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700740 }
Stephen Warrene22361a2014-02-12 14:30:04 -0700741
742 len = strlen(label->fdtdir);
743 if (!len)
744 slash = "./";
745 else if (label->fdtdir[len - 1] != '/')
746 slash = "/";
747 else
748 slash = "";
749
750 len = strlen(label->fdtdir) + strlen(slash) +
751 strlen(f1) + strlen(f2) + strlen(f3) +
752 strlen(f4) + 1;
753 fdtfilefree = malloc(len);
754 if (!fdtfilefree) {
755 printf("malloc fail (FDT filename)\n");
756 return 1;
757 }
758
759 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
760 label->fdtdir, slash, f1, f2, f3, f4);
761 fdtfile = fdtfilefree;
Chander Kashyapa6559382012-09-06 19:36:31 +0000762 }
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700763
764 if (fdtfile) {
765 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
766 free(fdtfilefree);
767 if (err < 0) {
768 printf("Skipping %s for failure retrieving fdt\n",
769 label->name);
770 return 1;
771 }
772 } else {
773 bootm_argv[3] = NULL;
774 }
775 }
776
777 if (!bootm_argv[3])
Chander Kashyapa6559382012-09-06 19:36:31 +0000778 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500779
780 if (bootm_argv[3])
781 bootm_argc = 4;
782
Bryan Wu1fb7d0e2014-07-31 17:39:59 -0700783 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
784 buf = map_sysmem(kernel_addr, 0);
785 /* Try bootm for legacy and FIT format image */
786 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
787 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600788#ifdef CONFIG_CMD_BOOTZ
Bryan Wu1fb7d0e2014-07-31 17:39:59 -0700789 /* Try booting a zImage */
790 else
791 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600792#endif
Rob Herring500f3042012-12-02 21:00:22 -0600793 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500794}
795
796/*
797 * Tokens for the pxe file parser.
798 */
799enum token_type {
800 T_EOL,
801 T_STRING,
802 T_EOF,
803 T_MENU,
804 T_TITLE,
805 T_TIMEOUT,
806 T_LABEL,
807 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000808 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500809 T_APPEND,
810 T_INITRD,
811 T_LOCALBOOT,
812 T_DEFAULT,
813 T_PROMPT,
814 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000815 T_FDT,
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700816 T_FDTDIR,
Rob Herring8577fec2012-12-02 21:00:27 -0600817 T_ONTIMEOUT,
Rob Herring98f64672012-12-02 21:00:29 -0600818 T_IPAPPEND,
Jason Hobbs06283a62011-08-31 10:37:30 -0500819 T_INVALID
820};
821
822/*
823 * A token - given by a value and a type.
824 */
825struct token {
826 char *val;
827 enum token_type type;
828};
829
830/*
831 * Keywords recognized.
832 */
833static const struct token keywords[] = {
834 {"menu", T_MENU},
835 {"title", T_TITLE},
836 {"timeout", T_TIMEOUT},
837 {"default", T_DEFAULT},
838 {"prompt", T_PROMPT},
839 {"label", T_LABEL},
840 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000841 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500842 {"localboot", T_LOCALBOOT},
843 {"append", T_APPEND},
844 {"initrd", T_INITRD},
845 {"include", T_INCLUDE},
Stephen Warrenf43c4012014-01-28 14:50:09 -0700846 {"devicetree", T_FDT},
Chander Kashyapa6559382012-09-06 19:36:31 +0000847 {"fdt", T_FDT},
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700848 {"devicetreedir", T_FDTDIR},
849 {"fdtdir", T_FDTDIR},
Rob Herring8577fec2012-12-02 21:00:27 -0600850 {"ontimeout", T_ONTIMEOUT,},
Rob Herring98f64672012-12-02 21:00:29 -0600851 {"ipappend", T_IPAPPEND,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500852 {NULL, T_INVALID}
853};
854
855/*
856 * Since pxe(linux) files don't have a token to identify the start of a
857 * literal, we have to keep track of when we're in a state where a literal is
858 * expected vs when we're in a state a keyword is expected.
859 */
860enum lex_state {
861 L_NORMAL = 0,
862 L_KEYWORD,
863 L_SLITERAL
864};
865
866/*
867 * get_string retrieves a string from *p and stores it as a token in
868 * *t.
869 *
870 * get_string used for scanning both string literals and keywords.
871 *
872 * Characters from *p are copied into t-val until a character equal to
873 * delim is found, or a NUL byte is reached. If delim has the special value of
874 * ' ', any whitespace character will be used as a delimiter.
875 *
876 * If lower is unequal to 0, uppercase characters will be converted to
877 * lowercase in the result. This is useful to make keywords case
878 * insensitive.
879 *
880 * The location of *p is updated to point to the first character after the end
881 * of the token - the ending delimiter.
882 *
883 * On success, the new value of t->val is returned. Memory for t->val is
884 * allocated using malloc and must be free()'d to reclaim it. If insufficient
885 * memory is available, NULL is returned.
886 */
887static char *get_string(char **p, struct token *t, char delim, int lower)
888{
889 char *b, *e;
890 size_t len, i;
891
892 /*
893 * b and e both start at the beginning of the input stream.
894 *
895 * e is incremented until we find the ending delimiter, or a NUL byte
896 * is reached. Then, we take e - b to find the length of the token.
897 */
898 b = e = *p;
899
900 while (*e) {
901 if ((delim == ' ' && isspace(*e)) || delim == *e)
902 break;
903 e++;
904 }
905
906 len = e - b;
907
908 /*
909 * Allocate memory to hold the string, and copy it in, converting
910 * characters to lowercase if lower is != 0.
911 */
912 t->val = malloc(len + 1);
913 if (!t->val)
914 return NULL;
915
916 for (i = 0; i < len; i++, b++) {
917 if (lower)
918 t->val[i] = tolower(*b);
919 else
920 t->val[i] = *b;
921 }
922
923 t->val[len] = '\0';
924
925 /*
926 * Update *p so the caller knows where to continue scanning.
927 */
928 *p = e;
929
930 t->type = T_STRING;
931
932 return t->val;
933}
934
935/*
936 * Populate a keyword token with a type and value.
937 */
938static void get_keyword(struct token *t)
939{
940 int i;
941
942 for (i = 0; keywords[i].val; i++) {
943 if (!strcmp(t->val, keywords[i].val)) {
944 t->type = keywords[i].type;
945 break;
946 }
947 }
948}
949
950/*
951 * Get the next token. We have to keep track of which state we're in to know
952 * if we're looking to get a string literal or a keyword.
953 *
954 * *p is updated to point at the first character after the current token.
955 */
956static void get_token(char **p, struct token *t, enum lex_state state)
957{
958 char *c = *p;
959
960 t->type = T_INVALID;
961
962 /* eat non EOL whitespace */
963 while (isblank(*c))
964 c++;
965
966 /*
967 * eat comments. note that string literals can't begin with #, but
968 * can contain a # after their first character.
969 */
970 if (*c == '#') {
971 while (*c && *c != '\n')
972 c++;
973 }
974
975 if (*c == '\n') {
976 t->type = T_EOL;
977 c++;
978 } else if (*c == '\0') {
979 t->type = T_EOF;
980 c++;
981 } else if (state == L_SLITERAL) {
982 get_string(&c, t, '\n', 0);
983 } else if (state == L_KEYWORD) {
984 /*
985 * when we expect a keyword, we first get the next string
986 * token delimited by whitespace, and then check if it
987 * matches a keyword in our keyword list. if it does, it's
988 * converted to a keyword token of the appropriate type, and
989 * if not, it remains a string token.
990 */
991 get_string(&c, t, ' ', 1);
992 get_keyword(t);
993 }
994
995 *p = c;
996}
997
998/*
999 * Increment *c until we get to the end of the current line, or EOF.
1000 */
1001static void eol_or_eof(char **c)
1002{
1003 while (**c && **c != '\n')
1004 (*c)++;
1005}
1006
1007/*
1008 * All of these parse_* functions share some common behavior.
1009 *
1010 * They finish with *c pointing after the token they parse, and return 1 on
1011 * success, or < 0 on error.
1012 */
1013
1014/*
1015 * Parse a string literal and store a pointer it at *dst. String literals
1016 * terminate at the end of the line.
1017 */
1018static int parse_sliteral(char **c, char **dst)
1019{
1020 struct token t;
1021 char *s = *c;
1022
1023 get_token(c, &t, L_SLITERAL);
1024
1025 if (t.type != T_STRING) {
1026 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1027 return -EINVAL;
1028 }
1029
1030 *dst = t.val;
1031
1032 return 1;
1033}
1034
1035/*
1036 * Parse a base 10 (unsigned) integer and store it at *dst.
1037 */
1038static int parse_integer(char **c, int *dst)
1039{
1040 struct token t;
1041 char *s = *c;
Jason Hobbs06283a62011-08-31 10:37:30 -05001042
1043 get_token(c, &t, L_SLITERAL);
1044
1045 if (t.type != T_STRING) {
1046 printf("Expected string: %.*s\n", (int)(*c - s), s);
1047 return -EINVAL;
1048 }
1049
Rob Herring500f3042012-12-02 21:00:22 -06001050 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -05001051
1052 free(t.val);
1053
1054 return 1;
1055}
1056
Steven Falco0e3f3f82013-10-07 09:51:48 -04001057static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001058
1059/*
1060 * Parse an include statement, and retrieve and parse the file it mentions.
1061 *
1062 * base should point to a location where it's safe to store the file, and
1063 * nest_level should indicate how many nested includes have occurred. For this
1064 * include, nest_level has already been incremented and doesn't need to be
1065 * incremented here.
1066 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001067static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs06283a62011-08-31 10:37:30 -05001068 struct pxe_menu *cfg, int nest_level)
1069{
1070 char *include_path;
1071 char *s = *c;
1072 int err;
1073
1074 err = parse_sliteral(c, &include_path);
1075
1076 if (err < 0) {
1077 printf("Expected include path: %.*s\n",
1078 (int)(*c - s), s);
1079 return err;
1080 }
1081
Steven Falco0e3f3f82013-10-07 09:51:48 -04001082 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs06283a62011-08-31 10:37:30 -05001083
1084 if (err < 0) {
1085 printf("Couldn't retrieve %s\n", include_path);
1086 return err;
1087 }
1088
Steven Falco0e3f3f82013-10-07 09:51:48 -04001089 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001090}
1091
1092/*
1093 * Parse lines that begin with 'menu'.
1094 *
1095 * b and nest are provided to handle the 'menu include' case.
1096 *
1097 * b should be the address where the file currently being parsed is stored.
1098 *
1099 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1100 * a file it includes, 3 when parsing a file included by that file, and so on.
1101 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001102static 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 -05001103{
1104 struct token t;
1105 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +00001106 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -05001107
1108 get_token(c, &t, L_KEYWORD);
1109
1110 switch (t.type) {
1111 case T_TITLE:
1112 err = parse_sliteral(c, &cfg->title);
1113
1114 break;
1115
1116 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001117 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs06283a62011-08-31 10:37:30 -05001118 nest_level + 1);
1119 break;
1120
1121 default:
1122 printf("Ignoring malformed menu command: %.*s\n",
1123 (int)(*c - s), s);
1124 }
1125
1126 if (err < 0)
1127 return err;
1128
1129 eol_or_eof(c);
1130
1131 return 1;
1132}
1133
1134/*
1135 * Handles parsing a 'menu line' when we're parsing a label.
1136 */
1137static int parse_label_menu(char **c, struct pxe_menu *cfg,
1138 struct pxe_label *label)
1139{
1140 struct token t;
1141 char *s;
1142
1143 s = *c;
1144
1145 get_token(c, &t, L_KEYWORD);
1146
1147 switch (t.type) {
1148 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001149 if (!cfg->default_label)
1150 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001151
1152 if (!cfg->default_label)
1153 return -ENOMEM;
1154
1155 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001156 case T_LABEL:
1157 parse_sliteral(c, &label->menu);
1158 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001159 default:
1160 printf("Ignoring malformed menu command: %.*s\n",
1161 (int)(*c - s), s);
1162 }
1163
1164 eol_or_eof(c);
1165
1166 return 0;
1167}
1168
1169/*
1170 * Parses a label and adds it to the list of labels for a menu.
1171 *
1172 * A label ends when we either get to the end of a file, or
1173 * get some input we otherwise don't have a handler defined
1174 * for.
1175 *
1176 */
1177static int parse_label(char **c, struct pxe_menu *cfg)
1178{
1179 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001180 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001181 char *s = *c;
1182 struct pxe_label *label;
1183 int err;
1184
1185 label = label_create();
1186 if (!label)
1187 return -ENOMEM;
1188
1189 err = parse_sliteral(c, &label->name);
1190 if (err < 0) {
1191 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1192 label_destroy(label);
1193 return -EINVAL;
1194 }
1195
1196 list_add_tail(&label->list, &cfg->labels);
1197
1198 while (1) {
1199 s = *c;
1200 get_token(c, &t, L_KEYWORD);
1201
1202 err = 0;
1203 switch (t.type) {
1204 case T_MENU:
1205 err = parse_label_menu(c, cfg, label);
1206 break;
1207
1208 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001209 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001210 err = parse_sliteral(c, &label->kernel);
1211 break;
1212
1213 case T_APPEND:
1214 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001215 if (label->initrd)
1216 break;
1217 s = strstr(label->append, "initrd=");
1218 if (!s)
1219 break;
1220 s += 7;
1221 len = (int)(strchr(s, ' ') - s);
1222 label->initrd = malloc(len + 1);
1223 strncpy(label->initrd, s, len);
1224 label->initrd[len] = '\0';
1225
Jason Hobbs06283a62011-08-31 10:37:30 -05001226 break;
1227
1228 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001229 if (!label->initrd)
1230 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001231 break;
1232
Chander Kashyapa6559382012-09-06 19:36:31 +00001233 case T_FDT:
1234 if (!label->fdt)
1235 err = parse_sliteral(c, &label->fdt);
1236 break;
1237
Stephen Warrenc61d94d2014-01-28 14:50:10 -07001238 case T_FDTDIR:
1239 if (!label->fdtdir)
1240 err = parse_sliteral(c, &label->fdtdir);
1241 break;
1242
Jason Hobbs06283a62011-08-31 10:37:30 -05001243 case T_LOCALBOOT:
Rob Herring500f3042012-12-02 21:00:22 -06001244 label->localboot = 1;
1245 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001246 break;
1247
Rob Herring98f64672012-12-02 21:00:29 -06001248 case T_IPAPPEND:
1249 err = parse_integer(c, &label->ipappend);
1250 break;
1251
Jason Hobbs06283a62011-08-31 10:37:30 -05001252 case T_EOL:
1253 break;
1254
1255 default:
1256 /*
1257 * put the token back! we don't want it - it's the end
1258 * of a label and whatever token this is, it's
1259 * something for the menu level context to handle.
1260 */
1261 *c = s;
1262 return 1;
1263 }
1264
1265 if (err < 0)
1266 return err;
1267 }
1268}
1269
1270/*
1271 * This 16 comes from the limit pxelinux imposes on nested includes.
1272 *
1273 * There is no reason at all we couldn't do more, but some limit helps prevent
1274 * infinite (until crash occurs) recursion if a file tries to include itself.
1275 */
1276#define MAX_NEST_LEVEL 16
1277
1278/*
1279 * Entry point for parsing a menu file. nest_level indicates how many times
1280 * we've nested in includes. It will be 1 for the top level menu file.
1281 *
1282 * Returns 1 on success, < 0 on error.
1283 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001284static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs06283a62011-08-31 10:37:30 -05001285{
1286 struct token t;
1287 char *s, *b, *label_name;
1288 int err;
1289
1290 b = p;
1291
1292 if (nest_level > MAX_NEST_LEVEL) {
1293 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1294 return -EMLINK;
1295 }
1296
1297 while (1) {
1298 s = p;
1299
1300 get_token(&p, &t, L_KEYWORD);
1301
1302 err = 0;
1303 switch (t.type) {
1304 case T_MENU:
Rob Herringe82eeb52012-12-02 21:00:25 -06001305 cfg->prompt = 1;
Steven Falco0e3f3f82013-10-07 09:51:48 -04001306 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001307 break;
1308
1309 case T_TIMEOUT:
1310 err = parse_integer(&p, &cfg->timeout);
1311 break;
1312
1313 case T_LABEL:
1314 err = parse_label(&p, cfg);
1315 break;
1316
1317 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001318 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001319 err = parse_sliteral(&p, &label_name);
1320
1321 if (label_name) {
1322 if (cfg->default_label)
1323 free(cfg->default_label);
1324
1325 cfg->default_label = label_name;
1326 }
1327
1328 break;
1329
Rob Herring1e085222012-05-25 10:43:16 +00001330 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001331 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring1e085222012-05-25 10:43:16 +00001332 nest_level + 1);
1333 break;
1334
Jason Hobbs06283a62011-08-31 10:37:30 -05001335 case T_PROMPT:
Rob Herringe82eeb52012-12-02 21:00:25 -06001336 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001337 break;
1338
1339 case T_EOL:
1340 break;
1341
1342 case T_EOF:
1343 return 1;
1344
1345 default:
1346 printf("Ignoring unknown command: %.*s\n",
1347 (int)(p - s), s);
1348 eol_or_eof(&p);
1349 }
1350
1351 if (err < 0)
1352 return err;
1353 }
1354}
1355
1356/*
1357 * Free the memory used by a pxe_menu and its labels.
1358 */
1359static void destroy_pxe_menu(struct pxe_menu *cfg)
1360{
1361 struct list_head *pos, *n;
1362 struct pxe_label *label;
1363
1364 if (cfg->title)
1365 free(cfg->title);
1366
1367 if (cfg->default_label)
1368 free(cfg->default_label);
1369
1370 list_for_each_safe(pos, n, &cfg->labels) {
1371 label = list_entry(pos, struct pxe_label, list);
1372
1373 label_destroy(label);
1374 }
1375
1376 free(cfg);
1377}
1378
1379/*
1380 * Entry point for parsing a pxe file. This is only used for the top level
1381 * file.
1382 *
1383 * Returns NULL if there is an error, otherwise, returns a pointer to a
1384 * pxe_menu struct populated with the results of parsing the pxe file (and any
1385 * files it includes). The resulting pxe_menu struct can be free()'d by using
1386 * the destroy_pxe_menu() function.
1387 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001388static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001389{
1390 struct pxe_menu *cfg;
1391
1392 cfg = malloc(sizeof(struct pxe_menu));
1393
1394 if (!cfg)
1395 return NULL;
1396
1397 memset(cfg, 0, sizeof(struct pxe_menu));
1398
1399 INIT_LIST_HEAD(&cfg->labels);
1400
Steven Falco0e3f3f82013-10-07 09:51:48 -04001401 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001402 destroy_pxe_menu(cfg);
1403 return NULL;
1404 }
1405
1406 return cfg;
1407}
1408
1409/*
1410 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1411 * menu code.
1412 */
1413static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1414{
1415 struct pxe_label *label;
1416 struct list_head *pos;
1417 struct menu *m;
1418 int err;
Rob Herring32d2ffe2012-12-02 21:00:26 -06001419 int i = 1;
1420 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001421
1422 /*
1423 * Create a menu and add items for all the labels.
1424 */
Pali Rohárfc9d64f2013-03-23 14:50:40 +00001425 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1426 NULL, NULL);
Jason Hobbs06283a62011-08-31 10:37:30 -05001427
1428 if (!m)
1429 return NULL;
1430
1431 list_for_each(pos, &cfg->labels) {
1432 label = list_entry(pos, struct pxe_label, list);
1433
Rob Herring32d2ffe2012-12-02 21:00:26 -06001434 sprintf(label->num, "%d", i++);
1435 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001436 menu_destroy(m);
1437 return NULL;
1438 }
Rob Herring32d2ffe2012-12-02 21:00:26 -06001439 if (cfg->default_label &&
Rob Herring8577fec2012-12-02 21:00:27 -06001440 (strcmp(label->name, cfg->default_label) == 0))
Rob Herring32d2ffe2012-12-02 21:00:26 -06001441 default_num = label->num;
1442
Jason Hobbs06283a62011-08-31 10:37:30 -05001443 }
1444
1445 /*
1446 * After we've created items for each label in the menu, set the
1447 * menu's default label if one was specified.
1448 */
Rob Herring32d2ffe2012-12-02 21:00:26 -06001449 if (default_num) {
1450 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001451 if (err != 1) {
1452 if (err != -ENOENT) {
1453 menu_destroy(m);
1454 return NULL;
1455 }
1456
1457 printf("Missing default: %s\n", cfg->default_label);
1458 }
1459 }
1460
1461 return m;
1462}
1463
1464/*
1465 * Try to boot any labels we have yet to attempt to boot.
1466 */
Tom Rinid7884e02013-09-24 09:05:08 -04001467static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001468{
1469 struct list_head *pos;
1470 struct pxe_label *label;
1471
1472 list_for_each(pos, &cfg->labels) {
1473 label = list_entry(pos, struct pxe_label, list);
1474
1475 if (!label->attempted)
Tom Rinid7884e02013-09-24 09:05:08 -04001476 label_boot(cmdtp, label);
Jason Hobbs06283a62011-08-31 10:37:30 -05001477 }
1478}
1479
1480/*
1481 * Boot the system as prescribed by a pxe_menu.
1482 *
1483 * Use the menu system to either get the user's choice or the default, based
1484 * on config or user input. If there is no default or user's choice,
1485 * attempted to boot labels in the order they were given in pxe files.
1486 * If the default or user's choice fails to boot, attempt to boot other
1487 * labels in the order they were given in pxe files.
1488 *
1489 * If this function returns, there weren't any labels that successfully
1490 * booted, or the user interrupted the menu selection via ctrl+c.
1491 */
Tom Rinid7884e02013-09-24 09:05:08 -04001492static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001493{
1494 void *choice;
1495 struct menu *m;
1496 int err;
1497
1498 m = pxe_menu_to_menu(cfg);
1499 if (!m)
1500 return;
1501
1502 err = menu_get_choice(m, &choice);
1503
1504 menu_destroy(m);
1505
Jason Hobbs6f40f272011-11-07 03:07:15 +00001506 /*
1507 * err == 1 means we got a choice back from menu_get_choice.
1508 *
1509 * err == -ENOENT if the menu was setup to select the default but no
1510 * default was set. in that case, we should continue trying to boot
1511 * labels that haven't been attempted yet.
1512 *
1513 * otherwise, the user interrupted or there was some other error and
1514 * we give up.
1515 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001516
Rob Herring500f3042012-12-02 21:00:22 -06001517 if (err == 1) {
Tom Rinid7884e02013-09-24 09:05:08 -04001518 err = label_boot(cmdtp, choice);
Rob Herring500f3042012-12-02 21:00:22 -06001519 if (!err)
1520 return;
1521 } else if (err != -ENOENT) {
Jason Hobbs6f40f272011-11-07 03:07:15 +00001522 return;
Rob Herring500f3042012-12-02 21:00:22 -06001523 }
Jason Hobbs06283a62011-08-31 10:37:30 -05001524
Tom Rinid7884e02013-09-24 09:05:08 -04001525 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001526}
1527
Stephen Warrenb81fdb02014-02-05 20:49:20 -07001528#ifdef CONFIG_CMD_NET
Jason Hobbs06283a62011-08-31 10:37:30 -05001529/*
1530 * Boots a system using a pxe file
1531 *
1532 * Returns 0 on success, 1 on error.
1533 */
1534static int
1535do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1536{
1537 unsigned long pxefile_addr_r;
1538 struct pxe_menu *cfg;
1539 char *pxefile_addr_str;
1540
Rob Herring669df7e2012-05-25 10:47:39 +00001541 do_getfile = do_get_tftp;
1542
Jason Hobbs06283a62011-08-31 10:37:30 -05001543 if (argc == 1) {
1544 pxefile_addr_str = from_env("pxefile_addr_r");
1545 if (!pxefile_addr_str)
1546 return 1;
1547
1548 } else if (argc == 2) {
1549 pxefile_addr_str = argv[1];
1550 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001551 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001552 }
1553
1554 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1555 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1556 return 1;
1557 }
1558
Steven Falco0e3f3f82013-10-07 09:51:48 -04001559 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs06283a62011-08-31 10:37:30 -05001560
1561 if (cfg == NULL) {
1562 printf("Error parsing config file\n");
1563 return 1;
1564 }
1565
Tom Rinid7884e02013-09-24 09:05:08 -04001566 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001567
1568 destroy_pxe_menu(cfg);
1569
Stephen Warrended2e202014-07-22 18:06:46 -06001570 copy_filename(BootFile, "", sizeof(BootFile));
1571
Jason Hobbs06283a62011-08-31 10:37:30 -05001572 return 0;
1573}
1574
1575static cmd_tbl_t cmd_pxe_sub[] = {
1576 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1577 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1578};
1579
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001580static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -05001581{
1582 cmd_tbl_t *cp;
1583
1584 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001585 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001586
Rob Herringe5a9a402013-10-18 13:04:42 -05001587 is_pxe = true;
1588
Jason Hobbs06283a62011-08-31 10:37:30 -05001589 /* drop initial "pxe" arg */
1590 argc--;
1591 argv++;
1592
1593 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1594
1595 if (cp)
1596 return cp->cmd(cmdtp, flag, argc, argv);
1597
Simon Glass4c12eeb2011-12-10 08:44:01 +00001598 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001599}
1600
1601U_BOOT_CMD(
1602 pxe, 3, 1, do_pxe,
1603 "commands to get and boot from pxe files",
1604 "get - try to retrieve a pxe file using tftp\npxe "
1605 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1606);
Stephen Warrenb81fdb02014-02-05 20:49:20 -07001607#endif
Rob Herring669df7e2012-05-25 10:47:39 +00001608
1609/*
1610 * Boots a system using a local disk syslinux/extlinux file
1611 *
1612 * Returns 0 on success, 1 on error.
1613 */
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001614static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Rob Herring669df7e2012-05-25 10:47:39 +00001615{
1616 unsigned long pxefile_addr_r;
1617 struct pxe_menu *cfg;
1618 char *pxefile_addr_str;
1619 char *filename;
1620 int prompt = 0;
1621
Rob Herringe5a9a402013-10-18 13:04:42 -05001622 is_pxe = false;
1623
Rob Herring669df7e2012-05-25 10:47:39 +00001624 if (strstr(argv[1], "-p")) {
1625 prompt = 1;
1626 argc--;
1627 argv++;
1628 }
1629
1630 if (argc < 4)
1631 return cmd_usage(cmdtp);
1632
1633 if (argc < 5) {
1634 pxefile_addr_str = from_env("pxefile_addr_r");
1635 if (!pxefile_addr_str)
1636 return 1;
1637 } else {
1638 pxefile_addr_str = argv[4];
1639 }
1640
1641 if (argc < 6)
1642 filename = getenv("bootfile");
1643 else {
1644 filename = argv[5];
1645 setenv("bootfile", filename);
1646 }
1647
1648 if (strstr(argv[3], "ext2"))
1649 do_getfile = do_get_ext2;
1650 else if (strstr(argv[3], "fat"))
1651 do_getfile = do_get_fat;
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001652 else if (strstr(argv[3], "any"))
1653 do_getfile = do_get_any;
Rob Herring669df7e2012-05-25 10:47:39 +00001654 else {
1655 printf("Invalid filesystem: %s\n", argv[3]);
1656 return 1;
1657 }
1658 fs_argv[1] = argv[1];
1659 fs_argv[2] = argv[2];
1660
1661 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1662 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1663 return 1;
1664 }
1665
Steven Falco0e3f3f82013-10-07 09:51:48 -04001666 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herring669df7e2012-05-25 10:47:39 +00001667 printf("Error reading config file\n");
1668 return 1;
1669 }
1670
Steven Falco0e3f3f82013-10-07 09:51:48 -04001671 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herring669df7e2012-05-25 10:47:39 +00001672
1673 if (cfg == NULL) {
1674 printf("Error parsing config file\n");
1675 return 1;
1676 }
1677
1678 if (prompt)
1679 cfg->prompt = 1;
1680
Tom Rinid7884e02013-09-24 09:05:08 -04001681 handle_pxe_menu(cmdtp, cfg);
Rob Herring669df7e2012-05-25 10:47:39 +00001682
1683 destroy_pxe_menu(cfg);
1684
1685 return 0;
1686}
1687
1688U_BOOT_CMD(
1689 sysboot, 7, 1, do_sysboot,
1690 "command to get and boot from syslinux files",
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001691 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1692 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1693 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herring669df7e2012-05-25 10:47:39 +00001694);