blob: 3d132681427e29ffc4109450b6b88a6a6498c0d7 [file] [log] [blame]
Jason Hobbs06283a62011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs06283a62011-08-31 10:37:30 -05005 */
Wolfgang Denk1a459662013-07-08 09:37:19 +02006
Jason Hobbs06283a62011-08-31 10:37:30 -05007#include <common.h>
8#include <command.h>
9#include <malloc.h>
10#include <linux/string.h>
11#include <linux/ctype.h>
12#include <errno.h>
13#include <linux/list.h>
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -060014#include <fs.h>
Jason Hobbs06283a62011-08-31 10:37:30 -050015
16#include "menu.h"
17
18#define MAX_TFTP_PATH_LEN 127
19
Rob Herring39f98552012-12-02 21:00:28 -060020const char *pxe_default_paths[] = {
Joe Hershberger58d9ff92013-06-24 17:21:04 -050021#ifdef CONFIG_SYS_SOC
Rob Herring39f98552012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger58d9ff92013-06-24 17:21:04 -050023#endif
Rob Herring39f98552012-12-02 21:00:28 -060024 "default-" CONFIG_SYS_ARCH,
25 "default",
26 NULL
27};
28
Rob Herringe5a9a402013-10-18 13:04:42 -050029static bool is_pxe;
30
Jason Hobbs06283a62011-08-31 10:37:30 -050031/*
32 * Like getenv, but prints an error if envvar isn't defined in the
33 * environment. It always returns what getenv does, so it can be used in
34 * place of getenv without changing error handling otherwise.
35 */
Rob Herring23b71942012-12-02 21:00:21 -060036static char *from_env(const char *envvar)
Jason Hobbs06283a62011-08-31 10:37:30 -050037{
38 char *ret;
39
40 ret = getenv(envvar);
41
42 if (!ret)
43 printf("missing environment variable: %s\n", envvar);
44
45 return ret;
46}
47
Stephen Warrenb81fdb02014-02-05 20:49:20 -070048#ifdef CONFIG_CMD_NET
Jason Hobbs06283a62011-08-31 10:37:30 -050049/*
50 * Convert an ethaddr from the environment to the format used by pxelinux
51 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
52 * the beginning of the ethernet address to indicate a hardware type of
53 * Ethernet. Also converts uppercase hex characters into lowercase, to match
54 * pxelinux's behavior.
55 *
56 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
57 * environment, or some other value < 0 on error.
58 */
59static int format_mac_pxe(char *outbuf, size_t outbuf_len)
60{
Rob Herringef034c92012-12-02 21:00:20 -060061 uchar ethaddr[6];
Jason Hobbs06283a62011-08-31 10:37:30 -050062
Rob Herringef034c92012-12-02 21:00:20 -060063 if (outbuf_len < 21) {
David Feng5cea95c2013-12-14 11:47:30 +080064 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs06283a62011-08-31 10:37:30 -050065
66 return -EINVAL;
67 }
68
Rob Herringef034c92012-12-02 21:00:20 -060069 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
70 ethaddr))
71 return -ENOENT;
Jason Hobbs06283a62011-08-31 10:37:30 -050072
Rob Herringef034c92012-12-02 21:00:20 -060073 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
74 ethaddr[0], ethaddr[1], ethaddr[2],
75 ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs06283a62011-08-31 10:37:30 -050076
77 return 1;
78}
Stephen Warrenb81fdb02014-02-05 20:49:20 -070079#endif
Jason Hobbs06283a62011-08-31 10:37:30 -050080
81/*
82 * Returns the directory the file specified in the bootfile env variable is
83 * in. If bootfile isn't defined in the environment, return NULL, which should
84 * be interpreted as "don't prepend anything to paths".
85 */
Rob Herring90ba7d72012-03-28 05:51:36 +000086static int get_bootfile_path(const char *file_path, char *bootfile_path,
87 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -050088{
89 char *bootfile, *last_slash;
Rob Herring90ba7d72012-03-28 05:51:36 +000090 size_t path_len = 0;
91
Rob Herringe5a9a402013-10-18 13:04:42 -050092 /* Only syslinux allows absolute paths */
93 if (file_path[0] == '/' && !is_pxe)
Rob Herring90ba7d72012-03-28 05:51:36 +000094 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -050095
96 bootfile = from_env("bootfile");
97
Rob Herring90ba7d72012-03-28 05:51:36 +000098 if (!bootfile)
99 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500100
101 last_slash = strrchr(bootfile, '/');
102
Rob Herring90ba7d72012-03-28 05:51:36 +0000103 if (last_slash == NULL)
104 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500105
106 path_len = (last_slash - bootfile) + 1;
107
108 if (bootfile_path_size < path_len) {
David Feng5cea95c2013-12-14 11:47:30 +0800109 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs06283a62011-08-31 10:37:30 -0500110 bootfile_path_size, path_len);
111
112 return -1;
113 }
114
115 strncpy(bootfile_path, bootfile, path_len);
116
Rob Herring90ba7d72012-03-28 05:51:36 +0000117 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500118 bootfile_path[path_len] = '\0';
119
120 return 1;
121}
122
Steven Falco0e3f3f82013-10-07 09:51:48 -0400123static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herring669df7e2012-05-25 10:47:39 +0000124
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700125#ifdef CONFIG_CMD_NET
Steven Falco0e3f3f82013-10-07 09:51:48 -0400126static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000127{
128 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
129
130 tftp_argv[1] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600131 tftp_argv[2] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000132
Steven Falco0e3f3f82013-10-07 09:51:48 -0400133 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000134 return -ENOENT;
135
136 return 1;
137}
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700138#endif
Rob Herring669df7e2012-05-25 10:47:39 +0000139
140static char *fs_argv[5];
141
Steven Falco0e3f3f82013-10-07 09:51:48 -0400142static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000143{
144#ifdef CONFIG_CMD_EXT2
145 fs_argv[0] = "ext2load";
146 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600147 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000148
Steven Falco0e3f3f82013-10-07 09:51:48 -0400149 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000150 return 1;
151#endif
152 return -ENOENT;
153}
154
Steven Falco0e3f3f82013-10-07 09:51:48 -0400155static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +0000156{
157#ifdef CONFIG_CMD_FAT
158 fs_argv[0] = "fatload";
159 fs_argv[3] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -0600160 fs_argv[4] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +0000161
Steven Falco0e3f3f82013-10-07 09:51:48 -0400162 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herring669df7e2012-05-25 10:47:39 +0000163 return 1;
164#endif
165 return -ENOENT;
166}
167
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -0600168static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
169{
170#ifdef CONFIG_CMD_FS_GENERIC
171 fs_argv[0] = "load";
172 fs_argv[3] = file_addr;
173 fs_argv[4] = (void *)file_path;
174
175 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
176 return 1;
177#endif
178 return -ENOENT;
179}
180
Jason Hobbs06283a62011-08-31 10:37:30 -0500181/*
182 * As in pxelinux, paths to files referenced from files we retrieve are
183 * relative to the location of bootfile. get_relfile takes such a path and
184 * joins it with the bootfile path to get the full path to the target file. If
185 * the bootfile path is NULL, we use file_path as is.
186 *
187 * Returns 1 for success, or < 0 on error.
188 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400189static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500190{
191 size_t path_len;
192 char relfile[MAX_TFTP_PATH_LEN+1];
193 char addr_buf[10];
Jason Hobbs06283a62011-08-31 10:37:30 -0500194 int err;
195
Rob Herring90ba7d72012-03-28 05:51:36 +0000196 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-08-31 10:37:30 -0500197
198 if (err < 0)
199 return err;
200
201 path_len = strlen(file_path);
202 path_len += strlen(relfile);
203
204 if (path_len > MAX_TFTP_PATH_LEN) {
205 printf("Base path too long (%s%s)\n",
206 relfile,
207 file_path);
208
209 return -ENAMETOOLONG;
210 }
211
212 strcat(relfile, file_path);
213
214 printf("Retrieving file: %s\n", relfile);
215
216 sprintf(addr_buf, "%p", file_addr);
217
Steven Falco0e3f3f82013-10-07 09:51:48 -0400218 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs06283a62011-08-31 10:37:30 -0500219}
220
221/*
222 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
223 * 'bootfile' was specified in the environment, the path to bootfile will be
224 * prepended to 'file_path' and the resulting path will be used.
225 *
226 * Returns 1 on success, or < 0 for error.
227 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400228static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs06283a62011-08-31 10:37:30 -0500229{
230 unsigned long config_file_size;
231 char *tftp_filesize;
232 int err;
233
Steven Falco0e3f3f82013-10-07 09:51:48 -0400234 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500235
236 if (err < 0)
237 return err;
238
239 /*
240 * the file comes without a NUL byte at the end, so find out its size
241 * and add the NUL byte.
242 */
243 tftp_filesize = from_env("filesize");
244
245 if (!tftp_filesize)
246 return -ENOENT;
247
248 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
249 return -EINVAL;
250
251 *(char *)(file_addr + config_file_size) = '\0';
252
253 return 1;
254}
255
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700256#ifdef CONFIG_CMD_NET
257
Jason Hobbs06283a62011-08-31 10:37:30 -0500258#define PXELINUX_DIR "pxelinux.cfg/"
259
260/*
261 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
262 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
263 * from the bootfile path, as described above.
264 *
265 * Returns 1 on success or < 0 on error.
266 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400267static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500268{
269 size_t base_len = strlen(PXELINUX_DIR);
270 char path[MAX_TFTP_PATH_LEN+1];
271
272 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
273 printf("path (%s%s) too long, skipping\n",
274 PXELINUX_DIR, file);
275 return -ENAMETOOLONG;
276 }
277
278 sprintf(path, PXELINUX_DIR "%s", file);
279
Steven Falco0e3f3f82013-10-07 09:51:48 -0400280 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500281}
282
283/*
284 * Looks for a pxe file with a name based on the pxeuuid environment variable.
285 *
286 * Returns 1 on success or < 0 on error.
287 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400288static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500289{
290 char *uuid_str;
291
292 uuid_str = from_env("pxeuuid");
293
294 if (!uuid_str)
295 return -ENOENT;
296
Steven Falco0e3f3f82013-10-07 09:51:48 -0400297 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500298}
299
300/*
301 * Looks for a pxe file with a name based on the 'ethaddr' environment
302 * variable.
303 *
304 * Returns 1 on success or < 0 on error.
305 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400306static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500307{
308 char mac_str[21];
309 int err;
310
311 err = format_mac_pxe(mac_str, sizeof(mac_str));
312
313 if (err < 0)
314 return err;
315
Steven Falco0e3f3f82013-10-07 09:51:48 -0400316 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500317}
318
319/*
320 * Looks for pxe files with names based on our IP address. See pxelinux
321 * documentation for details on what these file names look like. We match
322 * that exactly.
323 *
324 * Returns 1 on success or < 0 on error.
325 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400326static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500327{
328 char ip_addr[9];
329 int mask_pos, err;
330
331 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
332
333 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400334 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500335
336 if (err > 0)
337 return err;
338
339 ip_addr[mask_pos] = '\0';
340 }
341
342 return -ENOENT;
343}
344
345/*
346 * Entry point for the 'pxe get' command.
347 * This Follows pxelinux's rules to download a config file from a tftp server.
348 * The file is stored at the location given by the pxefile_addr_r environment
349 * variable, which must be set.
350 *
351 * UUID comes from pxeuuid env variable, if defined
352 * MAC addr comes from ethaddr env variable, if defined
353 * IP
354 *
355 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
356 *
357 * Returns 0 on success or 1 on error.
358 */
359static int
360do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
361{
362 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000363 unsigned long pxefile_addr_r;
Rob Herring39f98552012-12-02 21:00:28 -0600364 int err, i = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500365
Rob Herring669df7e2012-05-25 10:47:39 +0000366 do_getfile = do_get_tftp;
367
Jason Hobbs06283a62011-08-31 10:37:30 -0500368 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000369 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500370
Jason Hobbs06283a62011-08-31 10:37:30 -0500371 pxefile_addr_str = from_env("pxefile_addr_r");
372
373 if (!pxefile_addr_str)
374 return 1;
375
376 err = strict_strtoul(pxefile_addr_str, 16,
377 (unsigned long *)&pxefile_addr_r);
378 if (err < 0)
379 return 1;
380
381 /*
382 * Keep trying paths until we successfully get a file we're looking
383 * for.
384 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400385 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
386 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
387 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500388 printf("Config file found\n");
389
390 return 0;
391 }
392
Rob Herring39f98552012-12-02 21:00:28 -0600393 while (pxe_default_paths[i]) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400394 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herring39f98552012-12-02 21:00:28 -0600395 (void *)pxefile_addr_r) > 0) {
396 printf("Config file found\n");
397 return 0;
398 }
399 i++;
400 }
401
Jason Hobbs06283a62011-08-31 10:37:30 -0500402 printf("Config file not found\n");
403
404 return 1;
405}
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700406#endif
Jason Hobbs06283a62011-08-31 10:37:30 -0500407
408/*
409 * Wrapper to make it easier to store the file at file_path in the location
410 * specified by envaddr_name. file_path will be joined to the bootfile path,
411 * if any is specified.
412 *
413 * Returns 1 on success or < 0 on error.
414 */
Steven Falco0e3f3f82013-10-07 09:51:48 -0400415static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs06283a62011-08-31 10:37:30 -0500416{
Jason Hobbs834c9382012-03-05 08:12:28 +0000417 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500418 char *envaddr;
419
420 envaddr = from_env(envaddr_name);
421
422 if (!envaddr)
423 return -ENOENT;
424
Jason Hobbs834c9382012-03-05 08:12:28 +0000425 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500426 return -EINVAL;
427
Steven Falco0e3f3f82013-10-07 09:51:48 -0400428 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500429}
430
431/*
432 * A note on the pxe file parser.
433 *
434 * We're parsing files that use syslinux grammar, which has a few quirks.
435 * String literals must be recognized based on context - there is no
436 * quoting or escaping support. There's also nothing to explicitly indicate
437 * when a label section completes. We deal with that by ending a label
438 * section whenever we see a line that doesn't include.
439 *
440 * As with the syslinux family, this same file format could be reused in the
441 * future for non pxe purposes. The only action it takes during parsing that
442 * would throw this off is handling of include files. It assumes we're using
443 * pxe, and does a tftp download of a file listed as an include file in the
444 * middle of the parsing operation. That could be handled by refactoring it to
445 * take a 'include file getter' function.
446 */
447
448/*
449 * Describes a single label given in a pxe file.
450 *
451 * Create these with the 'label_create' function given below.
452 *
453 * name - the name of the menu as given on the 'menu label' line.
454 * kernel - the path to the kernel file to use for this label.
455 * append - kernel command line to use when booting this label
456 * initrd - path to the initrd to use for this label.
457 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
458 * localboot - 1 if this label specified 'localboot', 0 otherwise.
459 * list - lets these form a list, which a pxe_menu struct will hold.
460 */
461struct pxe_label {
Rob Herring32d2ffe2012-12-02 21:00:26 -0600462 char num[4];
Jason Hobbs06283a62011-08-31 10:37:30 -0500463 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000464 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500465 char *kernel;
466 char *append;
467 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000468 char *fdt;
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700469 char *fdtdir;
Rob Herring98f64672012-12-02 21:00:29 -0600470 int ipappend;
Jason Hobbs06283a62011-08-31 10:37:30 -0500471 int attempted;
472 int localboot;
Rob Herring500f3042012-12-02 21:00:22 -0600473 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500474 struct list_head list;
475};
476
477/*
478 * Describes a pxe menu as given via pxe files.
479 *
480 * title - the name of the menu as given by a 'menu title' line.
481 * default_label - the name of the default label, if any.
482 * timeout - time in tenths of a second to wait for a user key-press before
483 * booting the default label.
484 * prompt - if 0, don't prompt for a choice unless the timeout period is
485 * interrupted. If 1, always prompt for a choice regardless of
486 * timeout.
487 * labels - a list of labels defined for the menu.
488 */
489struct pxe_menu {
490 char *title;
491 char *default_label;
492 int timeout;
493 int prompt;
494 struct list_head labels;
495};
496
497/*
498 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
499 * result must be free()'d to reclaim the memory.
500 *
501 * Returns NULL if malloc fails.
502 */
503static struct pxe_label *label_create(void)
504{
505 struct pxe_label *label;
506
507 label = malloc(sizeof(struct pxe_label));
508
509 if (!label)
510 return NULL;
511
512 memset(label, 0, sizeof(struct pxe_label));
513
514 return label;
515}
516
517/*
518 * Free the memory used by a pxe_label, including that used by its name,
519 * kernel, append and initrd members, if they're non NULL.
520 *
521 * So - be sure to only use dynamically allocated memory for the members of
522 * the pxe_label struct, unless you want to clean it up first. These are
523 * currently only created by the pxe file parsing code.
524 */
525static void label_destroy(struct pxe_label *label)
526{
527 if (label->name)
528 free(label->name);
529
530 if (label->kernel)
531 free(label->kernel);
532
533 if (label->append)
534 free(label->append);
535
536 if (label->initrd)
537 free(label->initrd);
538
Chander Kashyapa6559382012-09-06 19:36:31 +0000539 if (label->fdt)
540 free(label->fdt);
541
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700542 if (label->fdtdir)
543 free(label->fdtdir);
544
Jason Hobbs06283a62011-08-31 10:37:30 -0500545 free(label);
546}
547
548/*
549 * Print a label and its string members if they're defined.
550 *
551 * This is passed as a callback to the menu code for displaying each
552 * menu entry.
553 */
554static void label_print(void *data)
555{
556 struct pxe_label *label = data;
Rob Herring32d2ffe2012-12-02 21:00:26 -0600557 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500558
Rob Herring32d2ffe2012-12-02 21:00:26 -0600559 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500560}
561
562/*
563 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
564 * environment variable is defined. Its contents will be executed as U-boot
565 * command. If the label specified an 'append' line, its contents will be
566 * used to overwrite the contents of the 'bootargs' environment variable prior
567 * to running 'localcmd'.
568 *
569 * Returns 1 on success or < 0 on error.
570 */
571static int label_localboot(struct pxe_label *label)
572{
Simon Glassd51004a2012-03-30 21:30:55 +0000573 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500574
575 localcmd = from_env("localcmd");
576
577 if (!localcmd)
578 return -ENOENT;
579
Jason Hobbs06283a62011-08-31 10:37:30 -0500580 if (label->append)
581 setenv("bootargs", label->append);
582
Simon Glassd51004a2012-03-30 21:30:55 +0000583 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500584
Simon Glassd51004a2012-03-30 21:30:55 +0000585 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500586}
587
588/*
589 * Boot according to the contents of a pxe_label.
590 *
591 * If we can't boot for any reason, we return. A successful boot never
592 * returns.
593 *
594 * The kernel will be stored in the location given by the 'kernel_addr_r'
595 * environment variable.
596 *
597 * If the label specifies an initrd file, it will be stored in the location
598 * given by the 'ramdisk_addr_r' environment variable.
599 *
600 * If the label specifies an 'append' line, its contents will overwrite that
601 * of the 'bootargs' environment variable.
602 */
Tom Rinid7884e02013-09-24 09:05:08 -0400603static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500604{
605 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600606 char initrd_str[22];
Rob Herring98f64672012-12-02 21:00:29 -0600607 char mac_str[29] = "";
608 char ip_str[68] = "";
609 char *bootargs;
Jason Hobbs06283a62011-08-31 10:37:30 -0500610 int bootm_argc = 3;
Rob Herring98f64672012-12-02 21:00:29 -0600611 int len = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500612
613 label_print(label);
614
615 label->attempted = 1;
616
617 if (label->localboot) {
Rob Herring500f3042012-12-02 21:00:22 -0600618 if (label->localboot_val >= 0)
619 label_localboot(label);
620 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500621 }
622
623 if (label->kernel == NULL) {
624 printf("No kernel given, skipping %s\n",
625 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600626 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500627 }
628
629 if (label->initrd) {
Steven Falco0e3f3f82013-10-07 09:51:48 -0400630 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500631 printf("Skipping %s for failure retrieving initrd\n",
632 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600633 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500634 }
635
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600636 bootm_argv[2] = initrd_str;
637 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
638 strcat(bootm_argv[2], ":");
639 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500640 } else {
641 bootm_argv[2] = "-";
642 }
643
Steven Falco0e3f3f82013-10-07 09:51:48 -0400644 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500645 printf("Skipping %s for failure retrieving kernel\n",
646 label->name);
Rob Herring500f3042012-12-02 21:00:22 -0600647 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500648 }
649
Rob Herring98f64672012-12-02 21:00:29 -0600650 if (label->ipappend & 0x1) {
651 sprintf(ip_str, " ip=%s:%s:%s:%s",
652 getenv("ipaddr"), getenv("serverip"),
653 getenv("gatewayip"), getenv("netmask"));
654 len += strlen(ip_str);
655 }
656
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700657#ifdef CONFIG_CMD_NET
Rob Herring98f64672012-12-02 21:00:29 -0600658 if (label->ipappend & 0x2) {
659 int err;
660 strcpy(mac_str, " BOOTIF=");
661 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
662 if (err < 0)
663 mac_str[0] = '\0';
664 len += strlen(mac_str);
665 }
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700666#endif
Rob Herring98f64672012-12-02 21:00:29 -0600667
668 if (label->append)
669 len += strlen(label->append);
670
671 if (len) {
672 bootargs = malloc(len + 1);
673 if (!bootargs)
674 return 1;
675 bootargs[0] = '\0';
676 if (label->append)
677 strcpy(bootargs, label->append);
678 strcat(bootargs, ip_str);
679 strcat(bootargs, mac_str);
680
681 setenv("bootargs", bootargs);
682 printf("append: %s\n", bootargs);
683
684 free(bootargs);
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) {
712 fdtfile = getenv("fdtfile");
713 /*
714 * For complex cases, it might be worth calling a
715 * board- or SoC-provided function here to provide a
716 * better default:
717 *
718 * if (!fdtfile)
719 * fdtfile = gen_fdtfile();
720 *
721 * If this is added, be sure to keep the default below,
722 * or move it to the default weak implementation of
723 * gen_fdtfile().
724 */
725 if (!fdtfile) {
726 char *soc = getenv("soc");
727 char *board = getenv("board");
728 char *slash;
729
730 len = strlen(label->fdtdir);
731 if (!len)
732 slash = "./";
733 else if (label->fdtdir[len - 1] != '/')
734 slash = "/";
735 else
736 slash = "";
737
738 len = strlen(label->fdtdir) + strlen(slash) +
739 strlen(soc) + 1 + strlen(board) + 5;
740 fdtfilefree = malloc(len);
741 if (!fdtfilefree) {
742 printf("malloc fail (FDT filename)\n");
743 return 1;
744 }
745
746 snprintf(fdtfilefree, len, "%s%s%s-%s.dtb",
747 label->fdtdir, slash, soc, board);
748 fdtfile = fdtfilefree;
749 }
Chander Kashyapa6559382012-09-06 19:36:31 +0000750 }
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700751
752 if (fdtfile) {
753 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
754 free(fdtfilefree);
755 if (err < 0) {
756 printf("Skipping %s for failure retrieving fdt\n",
757 label->name);
758 return 1;
759 }
760 } else {
761 bootm_argv[3] = NULL;
762 }
763 }
764
765 if (!bootm_argv[3])
Chander Kashyapa6559382012-09-06 19:36:31 +0000766 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500767
768 if (bootm_argv[3])
769 bootm_argc = 4;
770
Tom Rinid7884e02013-09-24 09:05:08 -0400771 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600772
773#ifdef CONFIG_CMD_BOOTZ
774 /* Try booting a zImage if do_bootm returns */
Tom Rinid7884e02013-09-24 09:05:08 -0400775 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringe6b6ccf2012-12-03 13:17:21 -0600776#endif
Rob Herring500f3042012-12-02 21:00:22 -0600777 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500778}
779
780/*
781 * Tokens for the pxe file parser.
782 */
783enum token_type {
784 T_EOL,
785 T_STRING,
786 T_EOF,
787 T_MENU,
788 T_TITLE,
789 T_TIMEOUT,
790 T_LABEL,
791 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000792 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500793 T_APPEND,
794 T_INITRD,
795 T_LOCALBOOT,
796 T_DEFAULT,
797 T_PROMPT,
798 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000799 T_FDT,
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700800 T_FDTDIR,
Rob Herring8577fec2012-12-02 21:00:27 -0600801 T_ONTIMEOUT,
Rob Herring98f64672012-12-02 21:00:29 -0600802 T_IPAPPEND,
Jason Hobbs06283a62011-08-31 10:37:30 -0500803 T_INVALID
804};
805
806/*
807 * A token - given by a value and a type.
808 */
809struct token {
810 char *val;
811 enum token_type type;
812};
813
814/*
815 * Keywords recognized.
816 */
817static const struct token keywords[] = {
818 {"menu", T_MENU},
819 {"title", T_TITLE},
820 {"timeout", T_TIMEOUT},
821 {"default", T_DEFAULT},
822 {"prompt", T_PROMPT},
823 {"label", T_LABEL},
824 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000825 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500826 {"localboot", T_LOCALBOOT},
827 {"append", T_APPEND},
828 {"initrd", T_INITRD},
829 {"include", T_INCLUDE},
Stephen Warrenf43c4012014-01-28 14:50:09 -0700830 {"devicetree", T_FDT},
Chander Kashyapa6559382012-09-06 19:36:31 +0000831 {"fdt", T_FDT},
Stephen Warrenc61d94d2014-01-28 14:50:10 -0700832 {"devicetreedir", T_FDTDIR},
833 {"fdtdir", T_FDTDIR},
Rob Herring8577fec2012-12-02 21:00:27 -0600834 {"ontimeout", T_ONTIMEOUT,},
Rob Herring98f64672012-12-02 21:00:29 -0600835 {"ipappend", T_IPAPPEND,},
Jason Hobbs06283a62011-08-31 10:37:30 -0500836 {NULL, T_INVALID}
837};
838
839/*
840 * Since pxe(linux) files don't have a token to identify the start of a
841 * literal, we have to keep track of when we're in a state where a literal is
842 * expected vs when we're in a state a keyword is expected.
843 */
844enum lex_state {
845 L_NORMAL = 0,
846 L_KEYWORD,
847 L_SLITERAL
848};
849
850/*
851 * get_string retrieves a string from *p and stores it as a token in
852 * *t.
853 *
854 * get_string used for scanning both string literals and keywords.
855 *
856 * Characters from *p are copied into t-val until a character equal to
857 * delim is found, or a NUL byte is reached. If delim has the special value of
858 * ' ', any whitespace character will be used as a delimiter.
859 *
860 * If lower is unequal to 0, uppercase characters will be converted to
861 * lowercase in the result. This is useful to make keywords case
862 * insensitive.
863 *
864 * The location of *p is updated to point to the first character after the end
865 * of the token - the ending delimiter.
866 *
867 * On success, the new value of t->val is returned. Memory for t->val is
868 * allocated using malloc and must be free()'d to reclaim it. If insufficient
869 * memory is available, NULL is returned.
870 */
871static char *get_string(char **p, struct token *t, char delim, int lower)
872{
873 char *b, *e;
874 size_t len, i;
875
876 /*
877 * b and e both start at the beginning of the input stream.
878 *
879 * e is incremented until we find the ending delimiter, or a NUL byte
880 * is reached. Then, we take e - b to find the length of the token.
881 */
882 b = e = *p;
883
884 while (*e) {
885 if ((delim == ' ' && isspace(*e)) || delim == *e)
886 break;
887 e++;
888 }
889
890 len = e - b;
891
892 /*
893 * Allocate memory to hold the string, and copy it in, converting
894 * characters to lowercase if lower is != 0.
895 */
896 t->val = malloc(len + 1);
897 if (!t->val)
898 return NULL;
899
900 for (i = 0; i < len; i++, b++) {
901 if (lower)
902 t->val[i] = tolower(*b);
903 else
904 t->val[i] = *b;
905 }
906
907 t->val[len] = '\0';
908
909 /*
910 * Update *p so the caller knows where to continue scanning.
911 */
912 *p = e;
913
914 t->type = T_STRING;
915
916 return t->val;
917}
918
919/*
920 * Populate a keyword token with a type and value.
921 */
922static void get_keyword(struct token *t)
923{
924 int i;
925
926 for (i = 0; keywords[i].val; i++) {
927 if (!strcmp(t->val, keywords[i].val)) {
928 t->type = keywords[i].type;
929 break;
930 }
931 }
932}
933
934/*
935 * Get the next token. We have to keep track of which state we're in to know
936 * if we're looking to get a string literal or a keyword.
937 *
938 * *p is updated to point at the first character after the current token.
939 */
940static void get_token(char **p, struct token *t, enum lex_state state)
941{
942 char *c = *p;
943
944 t->type = T_INVALID;
945
946 /* eat non EOL whitespace */
947 while (isblank(*c))
948 c++;
949
950 /*
951 * eat comments. note that string literals can't begin with #, but
952 * can contain a # after their first character.
953 */
954 if (*c == '#') {
955 while (*c && *c != '\n')
956 c++;
957 }
958
959 if (*c == '\n') {
960 t->type = T_EOL;
961 c++;
962 } else if (*c == '\0') {
963 t->type = T_EOF;
964 c++;
965 } else if (state == L_SLITERAL) {
966 get_string(&c, t, '\n', 0);
967 } else if (state == L_KEYWORD) {
968 /*
969 * when we expect a keyword, we first get the next string
970 * token delimited by whitespace, and then check if it
971 * matches a keyword in our keyword list. if it does, it's
972 * converted to a keyword token of the appropriate type, and
973 * if not, it remains a string token.
974 */
975 get_string(&c, t, ' ', 1);
976 get_keyword(t);
977 }
978
979 *p = c;
980}
981
982/*
983 * Increment *c until we get to the end of the current line, or EOF.
984 */
985static void eol_or_eof(char **c)
986{
987 while (**c && **c != '\n')
988 (*c)++;
989}
990
991/*
992 * All of these parse_* functions share some common behavior.
993 *
994 * They finish with *c pointing after the token they parse, and return 1 on
995 * success, or < 0 on error.
996 */
997
998/*
999 * Parse a string literal and store a pointer it at *dst. String literals
1000 * terminate at the end of the line.
1001 */
1002static int parse_sliteral(char **c, char **dst)
1003{
1004 struct token t;
1005 char *s = *c;
1006
1007 get_token(c, &t, L_SLITERAL);
1008
1009 if (t.type != T_STRING) {
1010 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1011 return -EINVAL;
1012 }
1013
1014 *dst = t.val;
1015
1016 return 1;
1017}
1018
1019/*
1020 * Parse a base 10 (unsigned) integer and store it at *dst.
1021 */
1022static int parse_integer(char **c, int *dst)
1023{
1024 struct token t;
1025 char *s = *c;
Jason Hobbs06283a62011-08-31 10:37:30 -05001026
1027 get_token(c, &t, L_SLITERAL);
1028
1029 if (t.type != T_STRING) {
1030 printf("Expected string: %.*s\n", (int)(*c - s), s);
1031 return -EINVAL;
1032 }
1033
Rob Herring500f3042012-12-02 21:00:22 -06001034 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -05001035
1036 free(t.val);
1037
1038 return 1;
1039}
1040
Steven Falco0e3f3f82013-10-07 09:51:48 -04001041static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001042
1043/*
1044 * Parse an include statement, and retrieve and parse the file it mentions.
1045 *
1046 * base should point to a location where it's safe to store the file, and
1047 * nest_level should indicate how many nested includes have occurred. For this
1048 * include, nest_level has already been incremented and doesn't need to be
1049 * incremented here.
1050 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001051static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs06283a62011-08-31 10:37:30 -05001052 struct pxe_menu *cfg, int nest_level)
1053{
1054 char *include_path;
1055 char *s = *c;
1056 int err;
1057
1058 err = parse_sliteral(c, &include_path);
1059
1060 if (err < 0) {
1061 printf("Expected include path: %.*s\n",
1062 (int)(*c - s), s);
1063 return err;
1064 }
1065
Steven Falco0e3f3f82013-10-07 09:51:48 -04001066 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs06283a62011-08-31 10:37:30 -05001067
1068 if (err < 0) {
1069 printf("Couldn't retrieve %s\n", include_path);
1070 return err;
1071 }
1072
Steven Falco0e3f3f82013-10-07 09:51:48 -04001073 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001074}
1075
1076/*
1077 * Parse lines that begin with 'menu'.
1078 *
1079 * b and nest are provided to handle the 'menu include' case.
1080 *
1081 * b should be the address where the file currently being parsed is stored.
1082 *
1083 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1084 * a file it includes, 3 when parsing a file included by that file, and so on.
1085 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001086static 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 -05001087{
1088 struct token t;
1089 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +00001090 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -05001091
1092 get_token(c, &t, L_KEYWORD);
1093
1094 switch (t.type) {
1095 case T_TITLE:
1096 err = parse_sliteral(c, &cfg->title);
1097
1098 break;
1099
1100 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001101 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs06283a62011-08-31 10:37:30 -05001102 nest_level + 1);
1103 break;
1104
1105 default:
1106 printf("Ignoring malformed menu command: %.*s\n",
1107 (int)(*c - s), s);
1108 }
1109
1110 if (err < 0)
1111 return err;
1112
1113 eol_or_eof(c);
1114
1115 return 1;
1116}
1117
1118/*
1119 * Handles parsing a 'menu line' when we're parsing a label.
1120 */
1121static int parse_label_menu(char **c, struct pxe_menu *cfg,
1122 struct pxe_label *label)
1123{
1124 struct token t;
1125 char *s;
1126
1127 s = *c;
1128
1129 get_token(c, &t, L_KEYWORD);
1130
1131 switch (t.type) {
1132 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001133 if (!cfg->default_label)
1134 cfg->default_label = strdup(label->name);
Jason Hobbs06283a62011-08-31 10:37:30 -05001135
1136 if (!cfg->default_label)
1137 return -ENOMEM;
1138
1139 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001140 case T_LABEL:
1141 parse_sliteral(c, &label->menu);
1142 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001143 default:
1144 printf("Ignoring malformed menu command: %.*s\n",
1145 (int)(*c - s), s);
1146 }
1147
1148 eol_or_eof(c);
1149
1150 return 0;
1151}
1152
1153/*
1154 * Parses a label and adds it to the list of labels for a menu.
1155 *
1156 * A label ends when we either get to the end of a file, or
1157 * get some input we otherwise don't have a handler defined
1158 * for.
1159 *
1160 */
1161static int parse_label(char **c, struct pxe_menu *cfg)
1162{
1163 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001164 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001165 char *s = *c;
1166 struct pxe_label *label;
1167 int err;
1168
1169 label = label_create();
1170 if (!label)
1171 return -ENOMEM;
1172
1173 err = parse_sliteral(c, &label->name);
1174 if (err < 0) {
1175 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1176 label_destroy(label);
1177 return -EINVAL;
1178 }
1179
1180 list_add_tail(&label->list, &cfg->labels);
1181
1182 while (1) {
1183 s = *c;
1184 get_token(c, &t, L_KEYWORD);
1185
1186 err = 0;
1187 switch (t.type) {
1188 case T_MENU:
1189 err = parse_label_menu(c, cfg, label);
1190 break;
1191
1192 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001193 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001194 err = parse_sliteral(c, &label->kernel);
1195 break;
1196
1197 case T_APPEND:
1198 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001199 if (label->initrd)
1200 break;
1201 s = strstr(label->append, "initrd=");
1202 if (!s)
1203 break;
1204 s += 7;
1205 len = (int)(strchr(s, ' ') - s);
1206 label->initrd = malloc(len + 1);
1207 strncpy(label->initrd, s, len);
1208 label->initrd[len] = '\0';
1209
Jason Hobbs06283a62011-08-31 10:37:30 -05001210 break;
1211
1212 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001213 if (!label->initrd)
1214 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001215 break;
1216
Chander Kashyapa6559382012-09-06 19:36:31 +00001217 case T_FDT:
1218 if (!label->fdt)
1219 err = parse_sliteral(c, &label->fdt);
1220 break;
1221
Stephen Warrenc61d94d2014-01-28 14:50:10 -07001222 case T_FDTDIR:
1223 if (!label->fdtdir)
1224 err = parse_sliteral(c, &label->fdtdir);
1225 break;
1226
Jason Hobbs06283a62011-08-31 10:37:30 -05001227 case T_LOCALBOOT:
Rob Herring500f3042012-12-02 21:00:22 -06001228 label->localboot = 1;
1229 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001230 break;
1231
Rob Herring98f64672012-12-02 21:00:29 -06001232 case T_IPAPPEND:
1233 err = parse_integer(c, &label->ipappend);
1234 break;
1235
Jason Hobbs06283a62011-08-31 10:37:30 -05001236 case T_EOL:
1237 break;
1238
1239 default:
1240 /*
1241 * put the token back! we don't want it - it's the end
1242 * of a label and whatever token this is, it's
1243 * something for the menu level context to handle.
1244 */
1245 *c = s;
1246 return 1;
1247 }
1248
1249 if (err < 0)
1250 return err;
1251 }
1252}
1253
1254/*
1255 * This 16 comes from the limit pxelinux imposes on nested includes.
1256 *
1257 * There is no reason at all we couldn't do more, but some limit helps prevent
1258 * infinite (until crash occurs) recursion if a file tries to include itself.
1259 */
1260#define MAX_NEST_LEVEL 16
1261
1262/*
1263 * Entry point for parsing a menu file. nest_level indicates how many times
1264 * we've nested in includes. It will be 1 for the top level menu file.
1265 *
1266 * Returns 1 on success, < 0 on error.
1267 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001268static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs06283a62011-08-31 10:37:30 -05001269{
1270 struct token t;
1271 char *s, *b, *label_name;
1272 int err;
1273
1274 b = p;
1275
1276 if (nest_level > MAX_NEST_LEVEL) {
1277 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1278 return -EMLINK;
1279 }
1280
1281 while (1) {
1282 s = p;
1283
1284 get_token(&p, &t, L_KEYWORD);
1285
1286 err = 0;
1287 switch (t.type) {
1288 case T_MENU:
Rob Herringe82eeb52012-12-02 21:00:25 -06001289 cfg->prompt = 1;
Steven Falco0e3f3f82013-10-07 09:51:48 -04001290 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs06283a62011-08-31 10:37:30 -05001291 break;
1292
1293 case T_TIMEOUT:
1294 err = parse_integer(&p, &cfg->timeout);
1295 break;
1296
1297 case T_LABEL:
1298 err = parse_label(&p, cfg);
1299 break;
1300
1301 case T_DEFAULT:
Rob Herring8577fec2012-12-02 21:00:27 -06001302 case T_ONTIMEOUT:
Jason Hobbs06283a62011-08-31 10:37:30 -05001303 err = parse_sliteral(&p, &label_name);
1304
1305 if (label_name) {
1306 if (cfg->default_label)
1307 free(cfg->default_label);
1308
1309 cfg->default_label = label_name;
1310 }
1311
1312 break;
1313
Rob Herring1e085222012-05-25 10:43:16 +00001314 case T_INCLUDE:
Steven Falco0e3f3f82013-10-07 09:51:48 -04001315 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring1e085222012-05-25 10:43:16 +00001316 nest_level + 1);
1317 break;
1318
Jason Hobbs06283a62011-08-31 10:37:30 -05001319 case T_PROMPT:
Rob Herringe82eeb52012-12-02 21:00:25 -06001320 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001321 break;
1322
1323 case T_EOL:
1324 break;
1325
1326 case T_EOF:
1327 return 1;
1328
1329 default:
1330 printf("Ignoring unknown command: %.*s\n",
1331 (int)(p - s), s);
1332 eol_or_eof(&p);
1333 }
1334
1335 if (err < 0)
1336 return err;
1337 }
1338}
1339
1340/*
1341 * Free the memory used by a pxe_menu and its labels.
1342 */
1343static void destroy_pxe_menu(struct pxe_menu *cfg)
1344{
1345 struct list_head *pos, *n;
1346 struct pxe_label *label;
1347
1348 if (cfg->title)
1349 free(cfg->title);
1350
1351 if (cfg->default_label)
1352 free(cfg->default_label);
1353
1354 list_for_each_safe(pos, n, &cfg->labels) {
1355 label = list_entry(pos, struct pxe_label, list);
1356
1357 label_destroy(label);
1358 }
1359
1360 free(cfg);
1361}
1362
1363/*
1364 * Entry point for parsing a pxe file. This is only used for the top level
1365 * file.
1366 *
1367 * Returns NULL if there is an error, otherwise, returns a pointer to a
1368 * pxe_menu struct populated with the results of parsing the pxe file (and any
1369 * files it includes). The resulting pxe_menu struct can be free()'d by using
1370 * the destroy_pxe_menu() function.
1371 */
Steven Falco0e3f3f82013-10-07 09:51:48 -04001372static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001373{
1374 struct pxe_menu *cfg;
1375
1376 cfg = malloc(sizeof(struct pxe_menu));
1377
1378 if (!cfg)
1379 return NULL;
1380
1381 memset(cfg, 0, sizeof(struct pxe_menu));
1382
1383 INIT_LIST_HEAD(&cfg->labels);
1384
Steven Falco0e3f3f82013-10-07 09:51:48 -04001385 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001386 destroy_pxe_menu(cfg);
1387 return NULL;
1388 }
1389
1390 return cfg;
1391}
1392
1393/*
1394 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1395 * menu code.
1396 */
1397static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1398{
1399 struct pxe_label *label;
1400 struct list_head *pos;
1401 struct menu *m;
1402 int err;
Rob Herring32d2ffe2012-12-02 21:00:26 -06001403 int i = 1;
1404 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001405
1406 /*
1407 * Create a menu and add items for all the labels.
1408 */
Pali Rohárfc9d64f2013-03-23 14:50:40 +00001409 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1410 NULL, NULL);
Jason Hobbs06283a62011-08-31 10:37:30 -05001411
1412 if (!m)
1413 return NULL;
1414
1415 list_for_each(pos, &cfg->labels) {
1416 label = list_entry(pos, struct pxe_label, list);
1417
Rob Herring32d2ffe2012-12-02 21:00:26 -06001418 sprintf(label->num, "%d", i++);
1419 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001420 menu_destroy(m);
1421 return NULL;
1422 }
Rob Herring32d2ffe2012-12-02 21:00:26 -06001423 if (cfg->default_label &&
Rob Herring8577fec2012-12-02 21:00:27 -06001424 (strcmp(label->name, cfg->default_label) == 0))
Rob Herring32d2ffe2012-12-02 21:00:26 -06001425 default_num = label->num;
1426
Jason Hobbs06283a62011-08-31 10:37:30 -05001427 }
1428
1429 /*
1430 * After we've created items for each label in the menu, set the
1431 * menu's default label if one was specified.
1432 */
Rob Herring32d2ffe2012-12-02 21:00:26 -06001433 if (default_num) {
1434 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001435 if (err != 1) {
1436 if (err != -ENOENT) {
1437 menu_destroy(m);
1438 return NULL;
1439 }
1440
1441 printf("Missing default: %s\n", cfg->default_label);
1442 }
1443 }
1444
1445 return m;
1446}
1447
1448/*
1449 * Try to boot any labels we have yet to attempt to boot.
1450 */
Tom Rinid7884e02013-09-24 09:05:08 -04001451static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001452{
1453 struct list_head *pos;
1454 struct pxe_label *label;
1455
1456 list_for_each(pos, &cfg->labels) {
1457 label = list_entry(pos, struct pxe_label, list);
1458
1459 if (!label->attempted)
Tom Rinid7884e02013-09-24 09:05:08 -04001460 label_boot(cmdtp, label);
Jason Hobbs06283a62011-08-31 10:37:30 -05001461 }
1462}
1463
1464/*
1465 * Boot the system as prescribed by a pxe_menu.
1466 *
1467 * Use the menu system to either get the user's choice or the default, based
1468 * on config or user input. If there is no default or user's choice,
1469 * attempted to boot labels in the order they were given in pxe files.
1470 * If the default or user's choice fails to boot, attempt to boot other
1471 * labels in the order they were given in pxe files.
1472 *
1473 * If this function returns, there weren't any labels that successfully
1474 * booted, or the user interrupted the menu selection via ctrl+c.
1475 */
Tom Rinid7884e02013-09-24 09:05:08 -04001476static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs06283a62011-08-31 10:37:30 -05001477{
1478 void *choice;
1479 struct menu *m;
1480 int err;
1481
1482 m = pxe_menu_to_menu(cfg);
1483 if (!m)
1484 return;
1485
1486 err = menu_get_choice(m, &choice);
1487
1488 menu_destroy(m);
1489
Jason Hobbs6f40f272011-11-07 03:07:15 +00001490 /*
1491 * err == 1 means we got a choice back from menu_get_choice.
1492 *
1493 * err == -ENOENT if the menu was setup to select the default but no
1494 * default was set. in that case, we should continue trying to boot
1495 * labels that haven't been attempted yet.
1496 *
1497 * otherwise, the user interrupted or there was some other error and
1498 * we give up.
1499 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001500
Rob Herring500f3042012-12-02 21:00:22 -06001501 if (err == 1) {
Tom Rinid7884e02013-09-24 09:05:08 -04001502 err = label_boot(cmdtp, choice);
Rob Herring500f3042012-12-02 21:00:22 -06001503 if (!err)
1504 return;
1505 } else if (err != -ENOENT) {
Jason Hobbs6f40f272011-11-07 03:07:15 +00001506 return;
Rob Herring500f3042012-12-02 21:00:22 -06001507 }
Jason Hobbs06283a62011-08-31 10:37:30 -05001508
Tom Rinid7884e02013-09-24 09:05:08 -04001509 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001510}
1511
Stephen Warrenb81fdb02014-02-05 20:49:20 -07001512#ifdef CONFIG_CMD_NET
Jason Hobbs06283a62011-08-31 10:37:30 -05001513/*
1514 * Boots a system using a pxe file
1515 *
1516 * Returns 0 on success, 1 on error.
1517 */
1518static int
1519do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1520{
1521 unsigned long pxefile_addr_r;
1522 struct pxe_menu *cfg;
1523 char *pxefile_addr_str;
1524
Rob Herring669df7e2012-05-25 10:47:39 +00001525 do_getfile = do_get_tftp;
1526
Jason Hobbs06283a62011-08-31 10:37:30 -05001527 if (argc == 1) {
1528 pxefile_addr_str = from_env("pxefile_addr_r");
1529 if (!pxefile_addr_str)
1530 return 1;
1531
1532 } else if (argc == 2) {
1533 pxefile_addr_str = argv[1];
1534 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001535 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001536 }
1537
1538 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1539 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1540 return 1;
1541 }
1542
Steven Falco0e3f3f82013-10-07 09:51:48 -04001543 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs06283a62011-08-31 10:37:30 -05001544
1545 if (cfg == NULL) {
1546 printf("Error parsing config file\n");
1547 return 1;
1548 }
1549
Tom Rinid7884e02013-09-24 09:05:08 -04001550 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -05001551
1552 destroy_pxe_menu(cfg);
1553
1554 return 0;
1555}
1556
1557static cmd_tbl_t cmd_pxe_sub[] = {
1558 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1559 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1560};
1561
1562int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1563{
1564 cmd_tbl_t *cp;
1565
1566 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001567 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001568
Rob Herringe5a9a402013-10-18 13:04:42 -05001569 is_pxe = true;
1570
Jason Hobbs06283a62011-08-31 10:37:30 -05001571 /* drop initial "pxe" arg */
1572 argc--;
1573 argv++;
1574
1575 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1576
1577 if (cp)
1578 return cp->cmd(cmdtp, flag, argc, argv);
1579
Simon Glass4c12eeb2011-12-10 08:44:01 +00001580 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001581}
1582
1583U_BOOT_CMD(
1584 pxe, 3, 1, do_pxe,
1585 "commands to get and boot from pxe files",
1586 "get - try to retrieve a pxe file using tftp\npxe "
1587 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1588);
Stephen Warrenb81fdb02014-02-05 20:49:20 -07001589#endif
Rob Herring669df7e2012-05-25 10:47:39 +00001590
1591/*
1592 * Boots a system using a local disk syslinux/extlinux file
1593 *
1594 * Returns 0 on success, 1 on error.
1595 */
1596int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1597{
1598 unsigned long pxefile_addr_r;
1599 struct pxe_menu *cfg;
1600 char *pxefile_addr_str;
1601 char *filename;
1602 int prompt = 0;
1603
Rob Herringe5a9a402013-10-18 13:04:42 -05001604 is_pxe = false;
1605
Rob Herring669df7e2012-05-25 10:47:39 +00001606 if (strstr(argv[1], "-p")) {
1607 prompt = 1;
1608 argc--;
1609 argv++;
1610 }
1611
1612 if (argc < 4)
1613 return cmd_usage(cmdtp);
1614
1615 if (argc < 5) {
1616 pxefile_addr_str = from_env("pxefile_addr_r");
1617 if (!pxefile_addr_str)
1618 return 1;
1619 } else {
1620 pxefile_addr_str = argv[4];
1621 }
1622
1623 if (argc < 6)
1624 filename = getenv("bootfile");
1625 else {
1626 filename = argv[5];
1627 setenv("bootfile", filename);
1628 }
1629
1630 if (strstr(argv[3], "ext2"))
1631 do_getfile = do_get_ext2;
1632 else if (strstr(argv[3], "fat"))
1633 do_getfile = do_get_fat;
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001634 else if (strstr(argv[3], "any"))
1635 do_getfile = do_get_any;
Rob Herring669df7e2012-05-25 10:47:39 +00001636 else {
1637 printf("Invalid filesystem: %s\n", argv[3]);
1638 return 1;
1639 }
1640 fs_argv[1] = argv[1];
1641 fs_argv[2] = argv[2];
1642
1643 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1644 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1645 return 1;
1646 }
1647
Steven Falco0e3f3f82013-10-07 09:51:48 -04001648 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herring669df7e2012-05-25 10:47:39 +00001649 printf("Error reading config file\n");
1650 return 1;
1651 }
1652
Steven Falco0e3f3f82013-10-07 09:51:48 -04001653 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herring669df7e2012-05-25 10:47:39 +00001654
1655 if (cfg == NULL) {
1656 printf("Error parsing config file\n");
1657 return 1;
1658 }
1659
1660 if (prompt)
1661 cfg->prompt = 1;
1662
Tom Rinid7884e02013-09-24 09:05:08 -04001663 handle_pxe_menu(cmdtp, cfg);
Rob Herring669df7e2012-05-25 10:47:39 +00001664
1665 destroy_pxe_menu(cfg);
1666
1667 return 0;
1668}
1669
1670U_BOOT_CMD(
1671 sysboot, 7, 1, do_sysboot,
1672 "command to get and boot from syslinux files",
Dennis Gilmore6d1a3e52014-02-04 05:25:46 -06001673 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1674 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1675 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herring669df7e2012-05-25 10:47:39 +00001676);