blob: 348332874b0105abc8055bafc84dfd12705dd0ec [file] [log] [blame]
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0e3a5932011-08-31 10:37:30 -05005 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006
Jason Hobbs0e3a5932011-08-31 10:37:30 -05007#include <common.h>
8#include <command.h>
9#include <malloc.h>
10#include <linux/string.h>
11#include <linux/ctype.h>
12#include <errno.h>
13#include <linux/list.h>
Dennis Gilmorecd90dc82014-02-04 05:25:46 -060014#include <fs.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050015
16#include "menu.h"
17
18#define MAX_TFTP_PATH_LEN 127
19
Rob Herringe26e8d62012-12-02 21:00:28 -060020const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050021#ifdef CONFIG_SYS_SOC
Rob Herringe26e8d62012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050023#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060024 "default-" CONFIG_SYS_ARCH,
25 "default",
26 NULL
27};
28
Rob Herring759456e2013-10-18 13:04:42 -050029static bool is_pxe;
30
Jason Hobbs0e3a5932011-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 Herring824901c2012-12-02 21:00:21 -060036static char *from_env(const char *envvar)
Jason Hobbs0e3a5932011-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 Warren857291b2014-02-05 20:49:20 -070048#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-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 Herringdd248b32012-12-02 21:00:20 -060061 uchar ethaddr[6];
Jason Hobbs0e3a5932011-08-31 10:37:30 -050062
Rob Herringdd248b32012-12-02 21:00:20 -060063 if (outbuf_len < 21) {
David Feng4ec50292013-12-14 11:47:30 +080064 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050065
66 return -EINVAL;
67 }
68
Rob Herringdd248b32012-12-02 21:00:20 -060069 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
70 ethaddr))
71 return -ENOENT;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050072
Rob Herringdd248b32012-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 Hobbs0e3a5932011-08-31 10:37:30 -050076
77 return 1;
78}
Stephen Warren857291b2014-02-05 20:49:20 -070079#endif
Jason Hobbs0e3a5932011-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 Herringdc410cd2012-03-28 05:51:36 +000086static int get_bootfile_path(const char *file_path, char *bootfile_path,
87 size_t bootfile_path_size)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050088{
89 char *bootfile, *last_slash;
Rob Herringdc410cd2012-03-28 05:51:36 +000090 size_t path_len = 0;
91
Rob Herring759456e2013-10-18 13:04:42 -050092 /* Only syslinux allows absolute paths */
93 if (file_path[0] == '/' && !is_pxe)
Rob Herringdc410cd2012-03-28 05:51:36 +000094 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -050095
96 bootfile = from_env("bootfile");
97
Rob Herringdc410cd2012-03-28 05:51:36 +000098 if (!bootfile)
99 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500100
101 last_slash = strrchr(bootfile, '/');
102
Rob Herringdc410cd2012-03-28 05:51:36 +0000103 if (last_slash == NULL)
104 goto ret;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500105
106 path_len = (last_slash - bootfile) + 1;
107
108 if (bootfile_path_size < path_len) {
David Feng4ec50292013-12-14 11:47:30 +0800109 printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs0e3a5932011-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 Herringdc410cd2012-03-28 05:51:36 +0000117 ret:
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500118 bootfile_path[path_len] = '\0';
119
120 return 1;
121}
122
Steven Falco7de0fa82013-10-07 09:51:48 -0400123static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
Rob Herringeee675f2012-05-25 10:47:39 +0000124
Stephen Warren857291b2014-02-05 20:49:20 -0700125#ifdef CONFIG_CMD_NET
Steven Falco7de0fa82013-10-07 09:51:48 -0400126static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000127{
128 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
129
130 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600131 tftp_argv[2] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000132
Steven Falco7de0fa82013-10-07 09:51:48 -0400133 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000134 return -ENOENT;
135
136 return 1;
137}
Stephen Warren857291b2014-02-05 20:49:20 -0700138#endif
Rob Herringeee675f2012-05-25 10:47:39 +0000139
140static char *fs_argv[5];
141
Steven Falco7de0fa82013-10-07 09:51:48 -0400142static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000143{
144#ifdef CONFIG_CMD_EXT2
145 fs_argv[0] = "ext2load";
146 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600147 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000148
Steven Falco7de0fa82013-10-07 09:51:48 -0400149 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000150 return 1;
151#endif
152 return -ENOENT;
153}
154
Steven Falco7de0fa82013-10-07 09:51:48 -0400155static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +0000156{
157#ifdef CONFIG_CMD_FAT
158 fs_argv[0] = "fatload";
159 fs_argv[3] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -0600160 fs_argv[4] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +0000161
Steven Falco7de0fa82013-10-07 09:51:48 -0400162 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
Rob Herringeee675f2012-05-25 10:47:39 +0000163 return 1;
164#endif
165 return -ENOENT;
166}
167
Dennis Gilmorecd90dc82014-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 Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400189static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-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 Hobbs0e3a5932011-08-31 10:37:30 -0500194 int err;
195
Rob Herringdc410cd2012-03-28 05:51:36 +0000196 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400218 return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400228static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500229{
230 unsigned long config_file_size;
231 char *tftp_filesize;
232 int err;
233
Steven Falco7de0fa82013-10-07 09:51:48 -0400234 err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs0e3a5932011-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 Warren857291b2014-02-05 20:49:20 -0700256#ifdef CONFIG_CMD_NET
257
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400267static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400280 return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400288static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400297 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400306static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400316 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400326static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400334 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-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 Hobbs9135c792012-03-05 08:12:28 +0000363 unsigned long pxefile_addr_r;
Rob Herringe26e8d62012-12-02 21:00:28 -0600364 int err, i = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500365
Rob Herringeee675f2012-05-25 10:47:39 +0000366 do_getfile = do_get_tftp;
367
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500368 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000369 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500370
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-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 Hobbs0e3a5932011-08-31 10:37:30 -0500388 printf("Config file found\n");
389
390 return 0;
391 }
392
Rob Herringe26e8d62012-12-02 21:00:28 -0600393 while (pxe_default_paths[i]) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400394 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Rob Herringe26e8d62012-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 Hobbs0e3a5932011-08-31 10:37:30 -0500402 printf("Config file not found\n");
403
404 return 1;
405}
Stephen Warren857291b2014-02-05 20:49:20 -0700406#endif
Jason Hobbs0e3a5932011-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 Falco7de0fa82013-10-07 09:51:48 -0400415static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500416{
Jason Hobbs9135c792012-03-05 08:12:28 +0000417 unsigned long file_addr;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500418 char *envaddr;
419
420 envaddr = from_env(envaddr_name);
421
422 if (!envaddr)
423 return -ENOENT;
424
Jason Hobbs9135c792012-03-05 08:12:28 +0000425 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500426 return -EINVAL;
427
Steven Falco7de0fa82013-10-07 09:51:48 -0400428 return get_relfile(cmdtp, file_path, (void *)file_addr);
Jason Hobbs0e3a5932011-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 Herringc9d1e3d2012-12-02 21:00:26 -0600462 char num[4];
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500463 char *name;
Rob Herringa6b74172012-03-28 05:51:34 +0000464 char *menu;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500465 char *kernel;
466 char *append;
467 char *initrd;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000468 char *fdt;
Stephen Warren90d09af2014-01-28 14:50:10 -0700469 char *fdtdir;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600470 int ipappend;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500471 int attempted;
472 int localboot;
Rob Herring58d7f432012-12-02 21:00:22 -0600473 int localboot_val;
Jason Hobbs0e3a5932011-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 Kashyap1644a9d2012-09-06 19:36:31 +0000539 if (label->fdt)
540 free(label->fdt);
541
Stephen Warren90d09af2014-01-28 14:50:10 -0700542 if (label->fdtdir)
543 free(label->fdtdir);
544
Jason Hobbs0e3a5932011-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 Herringc9d1e3d2012-12-02 21:00:26 -0600557 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500558
Rob Herringc9d1e3d2012-12-02 21:00:26 -0600559 printf("%s:\t%s\n", label->num, c);
Jason Hobbs0e3a5932011-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 Glassa6642e02012-03-30 21:30:55 +0000573 char *localcmd;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500574
575 localcmd = from_env("localcmd");
576
577 if (!localcmd)
578 return -ENOENT;
579
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500580 if (label->append)
581 setenv("bootargs", label->append);
582
Simon Glassa6642e02012-03-30 21:30:55 +0000583 debug("running: %s\n", localcmd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500584
Simon Glassa6642e02012-03-30 21:30:55 +0000585 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs0e3a5932011-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 Rini5b5b1762013-09-24 09:05:08 -0400603static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500604{
605 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herringc0356dc2012-12-03 13:17:21 -0600606 char initrd_str[22];
Rob Herring5a3d8be2012-12-02 21:00:29 -0600607 char mac_str[29] = "";
608 char ip_str[68] = "";
609 char *bootargs;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500610 int bootm_argc = 3;
Rob Herring5a3d8be2012-12-02 21:00:29 -0600611 int len = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500612
613 label_print(label);
614
615 label->attempted = 1;
616
617 if (label->localboot) {
Rob Herring58d7f432012-12-02 21:00:22 -0600618 if (label->localboot_val >= 0)
619 label_localboot(label);
620 return 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500621 }
622
623 if (label->kernel == NULL) {
624 printf("No kernel given, skipping %s\n",
625 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600626 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500627 }
628
629 if (label->initrd) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400630 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500631 printf("Skipping %s for failure retrieving initrd\n",
632 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600633 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500634 }
635
Rob Herringc0356dc2012-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 Hobbs0e3a5932011-08-31 10:37:30 -0500640 } else {
641 bootm_argv[2] = "-";
642 }
643
Steven Falco7de0fa82013-10-07 09:51:48 -0400644 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500645 printf("Skipping %s for failure retrieving kernel\n",
646 label->name);
Rob Herring58d7f432012-12-02 21:00:22 -0600647 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500648 }
649
Rob Herring5a3d8be2012-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 Warren857291b2014-02-05 20:49:20 -0700657#ifdef CONFIG_CMD_NET
Rob Herring5a3d8be2012-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 Warren857291b2014-02-05 20:49:20 -0700666#endif
Rob Herring5a3d8be2012-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 Herringc9d1e3d2012-12-02 21:00:26 -0600685 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500686
687 bootm_argv[1] = getenv("kernel_addr_r");
688
689 /*
Chander Kashyap1644a9d2012-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 Hobbs0e3a5932011-08-31 10:37:30 -0500701 */
Chander Kashyap1644a9d2012-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 Warren90d09af2014-01-28 14:50:10 -0700705 if (bootm_argv[3]) {
706 char *fdtfile = NULL;
707 char *fdtfilefree = NULL;
708
709 if (label->fdt) {
710 fdtfile = label->fdt;
711 } else if (label->fdtdir) {
Stephen Warrenc0592392014-02-12 14:30:04 -0700712 char *f1, *f2, *f3, *f4, *slash;
Stephen Warren90d09af2014-01-28 14:50:10 -0700713
Stephen Warrenc0592392014-02-12 14:30:04 -0700714 f1 = getenv("fdtfile");
715 if (f1) {
716 f2 = "";
717 f3 = "";
718 f4 = "";
719 } else {
720 /*
721 * For complex cases where this code doesn't
722 * generate the correct filename, the board
723 * code should set $fdtfile during early boot,
724 * or the boot scripts should set $fdtfile
725 * before invoking "pxe" or "sysboot".
726 */
727 f1 = getenv("soc");
728 f2 = "-";
729 f3 = getenv("board");
730 f4 = ".dtb";
731 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700732
Stephen Warrenc0592392014-02-12 14:30:04 -0700733 len = strlen(label->fdtdir);
734 if (!len)
735 slash = "./";
736 else if (label->fdtdir[len - 1] != '/')
737 slash = "/";
738 else
739 slash = "";
Stephen Warren90d09af2014-01-28 14:50:10 -0700740
Stephen Warrenc0592392014-02-12 14:30:04 -0700741 len = strlen(label->fdtdir) + strlen(slash) +
742 strlen(f1) + strlen(f2) + strlen(f3) +
743 strlen(f4) + 1;
744 fdtfilefree = malloc(len);
745 if (!fdtfilefree) {
746 printf("malloc fail (FDT filename)\n");
747 return 1;
Stephen Warren90d09af2014-01-28 14:50:10 -0700748 }
Stephen Warrenc0592392014-02-12 14:30:04 -0700749
750 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
751 label->fdtdir, slash, f1, f2, f3, f4);
752 fdtfile = fdtfilefree;
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000753 }
Stephen Warren90d09af2014-01-28 14:50:10 -0700754
755 if (fdtfile) {
756 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
757 free(fdtfilefree);
758 if (err < 0) {
759 printf("Skipping %s for failure retrieving fdt\n",
760 label->name);
761 return 1;
762 }
763 } else {
764 bootm_argv[3] = NULL;
765 }
766 }
767
768 if (!bootm_argv[3])
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000769 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500770
771 if (bootm_argv[3])
772 bootm_argc = 4;
773
Tom Rini5b5b1762013-09-24 09:05:08 -0400774 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600775
776#ifdef CONFIG_CMD_BOOTZ
777 /* Try booting a zImage if do_bootm returns */
Tom Rini5b5b1762013-09-24 09:05:08 -0400778 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Rob Herringc0356dc2012-12-03 13:17:21 -0600779#endif
Rob Herring58d7f432012-12-02 21:00:22 -0600780 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500781}
782
783/*
784 * Tokens for the pxe file parser.
785 */
786enum token_type {
787 T_EOL,
788 T_STRING,
789 T_EOF,
790 T_MENU,
791 T_TITLE,
792 T_TIMEOUT,
793 T_LABEL,
794 T_KERNEL,
Rob Herringc93f74a2012-03-28 05:51:35 +0000795 T_LINUX,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500796 T_APPEND,
797 T_INITRD,
798 T_LOCALBOOT,
799 T_DEFAULT,
800 T_PROMPT,
801 T_INCLUDE,
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000802 T_FDT,
Stephen Warren90d09af2014-01-28 14:50:10 -0700803 T_FDTDIR,
Rob Herringac995832012-12-02 21:00:27 -0600804 T_ONTIMEOUT,
Rob Herring5a3d8be2012-12-02 21:00:29 -0600805 T_IPAPPEND,
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500806 T_INVALID
807};
808
809/*
810 * A token - given by a value and a type.
811 */
812struct token {
813 char *val;
814 enum token_type type;
815};
816
817/*
818 * Keywords recognized.
819 */
820static const struct token keywords[] = {
821 {"menu", T_MENU},
822 {"title", T_TITLE},
823 {"timeout", T_TIMEOUT},
824 {"default", T_DEFAULT},
825 {"prompt", T_PROMPT},
826 {"label", T_LABEL},
827 {"kernel", T_KERNEL},
Rob Herringc93f74a2012-03-28 05:51:35 +0000828 {"linux", T_LINUX},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500829 {"localboot", T_LOCALBOOT},
830 {"append", T_APPEND},
831 {"initrd", T_INITRD},
832 {"include", T_INCLUDE},
Stephen Warren7b472af2014-01-28 14:50:09 -0700833 {"devicetree", T_FDT},
Chander Kashyap1644a9d2012-09-06 19:36:31 +0000834 {"fdt", T_FDT},
Stephen Warren90d09af2014-01-28 14:50:10 -0700835 {"devicetreedir", T_FDTDIR},
836 {"fdtdir", T_FDTDIR},
Rob Herringac995832012-12-02 21:00:27 -0600837 {"ontimeout", T_ONTIMEOUT,},
Rob Herring5a3d8be2012-12-02 21:00:29 -0600838 {"ipappend", T_IPAPPEND,},
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500839 {NULL, T_INVALID}
840};
841
842/*
843 * Since pxe(linux) files don't have a token to identify the start of a
844 * literal, we have to keep track of when we're in a state where a literal is
845 * expected vs when we're in a state a keyword is expected.
846 */
847enum lex_state {
848 L_NORMAL = 0,
849 L_KEYWORD,
850 L_SLITERAL
851};
852
853/*
854 * get_string retrieves a string from *p and stores it as a token in
855 * *t.
856 *
857 * get_string used for scanning both string literals and keywords.
858 *
859 * Characters from *p are copied into t-val until a character equal to
860 * delim is found, or a NUL byte is reached. If delim has the special value of
861 * ' ', any whitespace character will be used as a delimiter.
862 *
863 * If lower is unequal to 0, uppercase characters will be converted to
864 * lowercase in the result. This is useful to make keywords case
865 * insensitive.
866 *
867 * The location of *p is updated to point to the first character after the end
868 * of the token - the ending delimiter.
869 *
870 * On success, the new value of t->val is returned. Memory for t->val is
871 * allocated using malloc and must be free()'d to reclaim it. If insufficient
872 * memory is available, NULL is returned.
873 */
874static char *get_string(char **p, struct token *t, char delim, int lower)
875{
876 char *b, *e;
877 size_t len, i;
878
879 /*
880 * b and e both start at the beginning of the input stream.
881 *
882 * e is incremented until we find the ending delimiter, or a NUL byte
883 * is reached. Then, we take e - b to find the length of the token.
884 */
885 b = e = *p;
886
887 while (*e) {
888 if ((delim == ' ' && isspace(*e)) || delim == *e)
889 break;
890 e++;
891 }
892
893 len = e - b;
894
895 /*
896 * Allocate memory to hold the string, and copy it in, converting
897 * characters to lowercase if lower is != 0.
898 */
899 t->val = malloc(len + 1);
900 if (!t->val)
901 return NULL;
902
903 for (i = 0; i < len; i++, b++) {
904 if (lower)
905 t->val[i] = tolower(*b);
906 else
907 t->val[i] = *b;
908 }
909
910 t->val[len] = '\0';
911
912 /*
913 * Update *p so the caller knows where to continue scanning.
914 */
915 *p = e;
916
917 t->type = T_STRING;
918
919 return t->val;
920}
921
922/*
923 * Populate a keyword token with a type and value.
924 */
925static void get_keyword(struct token *t)
926{
927 int i;
928
929 for (i = 0; keywords[i].val; i++) {
930 if (!strcmp(t->val, keywords[i].val)) {
931 t->type = keywords[i].type;
932 break;
933 }
934 }
935}
936
937/*
938 * Get the next token. We have to keep track of which state we're in to know
939 * if we're looking to get a string literal or a keyword.
940 *
941 * *p is updated to point at the first character after the current token.
942 */
943static void get_token(char **p, struct token *t, enum lex_state state)
944{
945 char *c = *p;
946
947 t->type = T_INVALID;
948
949 /* eat non EOL whitespace */
950 while (isblank(*c))
951 c++;
952
953 /*
954 * eat comments. note that string literals can't begin with #, but
955 * can contain a # after their first character.
956 */
957 if (*c == '#') {
958 while (*c && *c != '\n')
959 c++;
960 }
961
962 if (*c == '\n') {
963 t->type = T_EOL;
964 c++;
965 } else if (*c == '\0') {
966 t->type = T_EOF;
967 c++;
968 } else if (state == L_SLITERAL) {
969 get_string(&c, t, '\n', 0);
970 } else if (state == L_KEYWORD) {
971 /*
972 * when we expect a keyword, we first get the next string
973 * token delimited by whitespace, and then check if it
974 * matches a keyword in our keyword list. if it does, it's
975 * converted to a keyword token of the appropriate type, and
976 * if not, it remains a string token.
977 */
978 get_string(&c, t, ' ', 1);
979 get_keyword(t);
980 }
981
982 *p = c;
983}
984
985/*
986 * Increment *c until we get to the end of the current line, or EOF.
987 */
988static void eol_or_eof(char **c)
989{
990 while (**c && **c != '\n')
991 (*c)++;
992}
993
994/*
995 * All of these parse_* functions share some common behavior.
996 *
997 * They finish with *c pointing after the token they parse, and return 1 on
998 * success, or < 0 on error.
999 */
1000
1001/*
1002 * Parse a string literal and store a pointer it at *dst. String literals
1003 * terminate at the end of the line.
1004 */
1005static int parse_sliteral(char **c, char **dst)
1006{
1007 struct token t;
1008 char *s = *c;
1009
1010 get_token(c, &t, L_SLITERAL);
1011
1012 if (t.type != T_STRING) {
1013 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1014 return -EINVAL;
1015 }
1016
1017 *dst = t.val;
1018
1019 return 1;
1020}
1021
1022/*
1023 * Parse a base 10 (unsigned) integer and store it at *dst.
1024 */
1025static int parse_integer(char **c, int *dst)
1026{
1027 struct token t;
1028 char *s = *c;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001029
1030 get_token(c, &t, L_SLITERAL);
1031
1032 if (t.type != T_STRING) {
1033 printf("Expected string: %.*s\n", (int)(*c - s), s);
1034 return -EINVAL;
1035 }
1036
Rob Herring58d7f432012-12-02 21:00:22 -06001037 *dst = simple_strtol(t.val, NULL, 10);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001038
1039 free(t.val);
1040
1041 return 1;
1042}
1043
Steven Falco7de0fa82013-10-07 09:51:48 -04001044static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001045
1046/*
1047 * Parse an include statement, and retrieve and parse the file it mentions.
1048 *
1049 * base should point to a location where it's safe to store the file, and
1050 * nest_level should indicate how many nested includes have occurred. For this
1051 * include, nest_level has already been incremented and doesn't need to be
1052 * incremented here.
1053 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001054static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001055 struct pxe_menu *cfg, int nest_level)
1056{
1057 char *include_path;
1058 char *s = *c;
1059 int err;
1060
1061 err = parse_sliteral(c, &include_path);
1062
1063 if (err < 0) {
1064 printf("Expected include path: %.*s\n",
1065 (int)(*c - s), s);
1066 return err;
1067 }
1068
Steven Falco7de0fa82013-10-07 09:51:48 -04001069 err = get_pxe_file(cmdtp, include_path, base);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001070
1071 if (err < 0) {
1072 printf("Couldn't retrieve %s\n", include_path);
1073 return err;
1074 }
1075
Steven Falco7de0fa82013-10-07 09:51:48 -04001076 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001077}
1078
1079/*
1080 * Parse lines that begin with 'menu'.
1081 *
1082 * b and nest are provided to handle the 'menu include' case.
1083 *
1084 * b should be the address where the file currently being parsed is stored.
1085 *
1086 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1087 * a file it includes, 3 when parsing a file included by that file, and so on.
1088 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001089static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001090{
1091 struct token t;
1092 char *s = *c;
Heiko Schocher9cd01dd2011-12-12 20:37:17 +00001093 int err = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001094
1095 get_token(c, &t, L_KEYWORD);
1096
1097 switch (t.type) {
1098 case T_TITLE:
1099 err = parse_sliteral(c, &cfg->title);
1100
1101 break;
1102
1103 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001104 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001105 nest_level + 1);
1106 break;
1107
1108 default:
1109 printf("Ignoring malformed menu command: %.*s\n",
1110 (int)(*c - s), s);
1111 }
1112
1113 if (err < 0)
1114 return err;
1115
1116 eol_or_eof(c);
1117
1118 return 1;
1119}
1120
1121/*
1122 * Handles parsing a 'menu line' when we're parsing a label.
1123 */
1124static int parse_label_menu(char **c, struct pxe_menu *cfg,
1125 struct pxe_label *label)
1126{
1127 struct token t;
1128 char *s;
1129
1130 s = *c;
1131
1132 get_token(c, &t, L_KEYWORD);
1133
1134 switch (t.type) {
1135 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001136 if (!cfg->default_label)
1137 cfg->default_label = strdup(label->name);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001138
1139 if (!cfg->default_label)
1140 return -ENOMEM;
1141
1142 break;
Rob Herringa6b74172012-03-28 05:51:34 +00001143 case T_LABEL:
1144 parse_sliteral(c, &label->menu);
1145 break;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001146 default:
1147 printf("Ignoring malformed menu command: %.*s\n",
1148 (int)(*c - s), s);
1149 }
1150
1151 eol_or_eof(c);
1152
1153 return 0;
1154}
1155
1156/*
1157 * Parses a label and adds it to the list of labels for a menu.
1158 *
1159 * A label ends when we either get to the end of a file, or
1160 * get some input we otherwise don't have a handler defined
1161 * for.
1162 *
1163 */
1164static int parse_label(char **c, struct pxe_menu *cfg)
1165{
1166 struct token t;
Rob Herringd8fceec2012-03-28 05:51:37 +00001167 int len;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001168 char *s = *c;
1169 struct pxe_label *label;
1170 int err;
1171
1172 label = label_create();
1173 if (!label)
1174 return -ENOMEM;
1175
1176 err = parse_sliteral(c, &label->name);
1177 if (err < 0) {
1178 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1179 label_destroy(label);
1180 return -EINVAL;
1181 }
1182
1183 list_add_tail(&label->list, &cfg->labels);
1184
1185 while (1) {
1186 s = *c;
1187 get_token(c, &t, L_KEYWORD);
1188
1189 err = 0;
1190 switch (t.type) {
1191 case T_MENU:
1192 err = parse_label_menu(c, cfg, label);
1193 break;
1194
1195 case T_KERNEL:
Rob Herringc93f74a2012-03-28 05:51:35 +00001196 case T_LINUX:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001197 err = parse_sliteral(c, &label->kernel);
1198 break;
1199
1200 case T_APPEND:
1201 err = parse_sliteral(c, &label->append);
Rob Herringd8fceec2012-03-28 05:51:37 +00001202 if (label->initrd)
1203 break;
1204 s = strstr(label->append, "initrd=");
1205 if (!s)
1206 break;
1207 s += 7;
1208 len = (int)(strchr(s, ' ') - s);
1209 label->initrd = malloc(len + 1);
1210 strncpy(label->initrd, s, len);
1211 label->initrd[len] = '\0';
1212
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001213 break;
1214
1215 case T_INITRD:
Rob Herringd8fceec2012-03-28 05:51:37 +00001216 if (!label->initrd)
1217 err = parse_sliteral(c, &label->initrd);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001218 break;
1219
Chander Kashyap1644a9d2012-09-06 19:36:31 +00001220 case T_FDT:
1221 if (!label->fdt)
1222 err = parse_sliteral(c, &label->fdt);
1223 break;
1224
Stephen Warren90d09af2014-01-28 14:50:10 -07001225 case T_FDTDIR:
1226 if (!label->fdtdir)
1227 err = parse_sliteral(c, &label->fdtdir);
1228 break;
1229
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001230 case T_LOCALBOOT:
Rob Herring58d7f432012-12-02 21:00:22 -06001231 label->localboot = 1;
1232 err = parse_integer(c, &label->localboot_val);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001233 break;
1234
Rob Herring5a3d8be2012-12-02 21:00:29 -06001235 case T_IPAPPEND:
1236 err = parse_integer(c, &label->ipappend);
1237 break;
1238
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001239 case T_EOL:
1240 break;
1241
1242 default:
1243 /*
1244 * put the token back! we don't want it - it's the end
1245 * of a label and whatever token this is, it's
1246 * something for the menu level context to handle.
1247 */
1248 *c = s;
1249 return 1;
1250 }
1251
1252 if (err < 0)
1253 return err;
1254 }
1255}
1256
1257/*
1258 * This 16 comes from the limit pxelinux imposes on nested includes.
1259 *
1260 * There is no reason at all we couldn't do more, but some limit helps prevent
1261 * infinite (until crash occurs) recursion if a file tries to include itself.
1262 */
1263#define MAX_NEST_LEVEL 16
1264
1265/*
1266 * Entry point for parsing a menu file. nest_level indicates how many times
1267 * we've nested in includes. It will be 1 for the top level menu file.
1268 *
1269 * Returns 1 on success, < 0 on error.
1270 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001271static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001272{
1273 struct token t;
1274 char *s, *b, *label_name;
1275 int err;
1276
1277 b = p;
1278
1279 if (nest_level > MAX_NEST_LEVEL) {
1280 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1281 return -EMLINK;
1282 }
1283
1284 while (1) {
1285 s = p;
1286
1287 get_token(&p, &t, L_KEYWORD);
1288
1289 err = 0;
1290 switch (t.type) {
1291 case T_MENU:
Rob Herringf0898b92012-12-02 21:00:25 -06001292 cfg->prompt = 1;
Steven Falco7de0fa82013-10-07 09:51:48 -04001293 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001294 break;
1295
1296 case T_TIMEOUT:
1297 err = parse_integer(&p, &cfg->timeout);
1298 break;
1299
1300 case T_LABEL:
1301 err = parse_label(&p, cfg);
1302 break;
1303
1304 case T_DEFAULT:
Rob Herringac995832012-12-02 21:00:27 -06001305 case T_ONTIMEOUT:
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001306 err = parse_sliteral(&p, &label_name);
1307
1308 if (label_name) {
1309 if (cfg->default_label)
1310 free(cfg->default_label);
1311
1312 cfg->default_label = label_name;
1313 }
1314
1315 break;
1316
Rob Herring8e5abb02012-05-25 10:43:16 +00001317 case T_INCLUDE:
Steven Falco7de0fa82013-10-07 09:51:48 -04001318 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
Rob Herring8e5abb02012-05-25 10:43:16 +00001319 nest_level + 1);
1320 break;
1321
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001322 case T_PROMPT:
Rob Herringf0898b92012-12-02 21:00:25 -06001323 eol_or_eof(&p);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001324 break;
1325
1326 case T_EOL:
1327 break;
1328
1329 case T_EOF:
1330 return 1;
1331
1332 default:
1333 printf("Ignoring unknown command: %.*s\n",
1334 (int)(p - s), s);
1335 eol_or_eof(&p);
1336 }
1337
1338 if (err < 0)
1339 return err;
1340 }
1341}
1342
1343/*
1344 * Free the memory used by a pxe_menu and its labels.
1345 */
1346static void destroy_pxe_menu(struct pxe_menu *cfg)
1347{
1348 struct list_head *pos, *n;
1349 struct pxe_label *label;
1350
1351 if (cfg->title)
1352 free(cfg->title);
1353
1354 if (cfg->default_label)
1355 free(cfg->default_label);
1356
1357 list_for_each_safe(pos, n, &cfg->labels) {
1358 label = list_entry(pos, struct pxe_label, list);
1359
1360 label_destroy(label);
1361 }
1362
1363 free(cfg);
1364}
1365
1366/*
1367 * Entry point for parsing a pxe file. This is only used for the top level
1368 * file.
1369 *
1370 * Returns NULL if there is an error, otherwise, returns a pointer to a
1371 * pxe_menu struct populated with the results of parsing the pxe file (and any
1372 * files it includes). The resulting pxe_menu struct can be free()'d by using
1373 * the destroy_pxe_menu() function.
1374 */
Steven Falco7de0fa82013-10-07 09:51:48 -04001375static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001376{
1377 struct pxe_menu *cfg;
1378
1379 cfg = malloc(sizeof(struct pxe_menu));
1380
1381 if (!cfg)
1382 return NULL;
1383
1384 memset(cfg, 0, sizeof(struct pxe_menu));
1385
1386 INIT_LIST_HEAD(&cfg->labels);
1387
Steven Falco7de0fa82013-10-07 09:51:48 -04001388 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001389 destroy_pxe_menu(cfg);
1390 return NULL;
1391 }
1392
1393 return cfg;
1394}
1395
1396/*
1397 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1398 * menu code.
1399 */
1400static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1401{
1402 struct pxe_label *label;
1403 struct list_head *pos;
1404 struct menu *m;
1405 int err;
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001406 int i = 1;
1407 char *default_num = NULL;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001408
1409 /*
1410 * Create a menu and add items for all the labels.
1411 */
Pali Rohárd0d8d3b2013-03-23 14:50:40 +00001412 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1413 NULL, NULL);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001414
1415 if (!m)
1416 return NULL;
1417
1418 list_for_each(pos, &cfg->labels) {
1419 label = list_entry(pos, struct pxe_label, list);
1420
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001421 sprintf(label->num, "%d", i++);
1422 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001423 menu_destroy(m);
1424 return NULL;
1425 }
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001426 if (cfg->default_label &&
Rob Herringac995832012-12-02 21:00:27 -06001427 (strcmp(label->name, cfg->default_label) == 0))
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001428 default_num = label->num;
1429
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001430 }
1431
1432 /*
1433 * After we've created items for each label in the menu, set the
1434 * menu's default label if one was specified.
1435 */
Rob Herringc9d1e3d2012-12-02 21:00:26 -06001436 if (default_num) {
1437 err = menu_default_set(m, default_num);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001438 if (err != 1) {
1439 if (err != -ENOENT) {
1440 menu_destroy(m);
1441 return NULL;
1442 }
1443
1444 printf("Missing default: %s\n", cfg->default_label);
1445 }
1446 }
1447
1448 return m;
1449}
1450
1451/*
1452 * Try to boot any labels we have yet to attempt to boot.
1453 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001454static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001455{
1456 struct list_head *pos;
1457 struct pxe_label *label;
1458
1459 list_for_each(pos, &cfg->labels) {
1460 label = list_entry(pos, struct pxe_label, list);
1461
1462 if (!label->attempted)
Tom Rini5b5b1762013-09-24 09:05:08 -04001463 label_boot(cmdtp, label);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001464 }
1465}
1466
1467/*
1468 * Boot the system as prescribed by a pxe_menu.
1469 *
1470 * Use the menu system to either get the user's choice or the default, based
1471 * on config or user input. If there is no default or user's choice,
1472 * attempted to boot labels in the order they were given in pxe files.
1473 * If the default or user's choice fails to boot, attempt to boot other
1474 * labels in the order they were given in pxe files.
1475 *
1476 * If this function returns, there weren't any labels that successfully
1477 * booted, or the user interrupted the menu selection via ctrl+c.
1478 */
Tom Rini5b5b1762013-09-24 09:05:08 -04001479static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001480{
1481 void *choice;
1482 struct menu *m;
1483 int err;
1484
1485 m = pxe_menu_to_menu(cfg);
1486 if (!m)
1487 return;
1488
1489 err = menu_get_choice(m, &choice);
1490
1491 menu_destroy(m);
1492
Jason Hobbs083b7a62011-11-07 03:07:15 +00001493 /*
1494 * err == 1 means we got a choice back from menu_get_choice.
1495 *
1496 * err == -ENOENT if the menu was setup to select the default but no
1497 * default was set. in that case, we should continue trying to boot
1498 * labels that haven't been attempted yet.
1499 *
1500 * otherwise, the user interrupted or there was some other error and
1501 * we give up.
1502 */
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001503
Rob Herring58d7f432012-12-02 21:00:22 -06001504 if (err == 1) {
Tom Rini5b5b1762013-09-24 09:05:08 -04001505 err = label_boot(cmdtp, choice);
Rob Herring58d7f432012-12-02 21:00:22 -06001506 if (!err)
1507 return;
1508 } else if (err != -ENOENT) {
Jason Hobbs083b7a62011-11-07 03:07:15 +00001509 return;
Rob Herring58d7f432012-12-02 21:00:22 -06001510 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001511
Tom Rini5b5b1762013-09-24 09:05:08 -04001512 boot_unattempted_labels(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001513}
1514
Stephen Warren857291b2014-02-05 20:49:20 -07001515#ifdef CONFIG_CMD_NET
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001516/*
1517 * Boots a system using a pxe file
1518 *
1519 * Returns 0 on success, 1 on error.
1520 */
1521static int
1522do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1523{
1524 unsigned long pxefile_addr_r;
1525 struct pxe_menu *cfg;
1526 char *pxefile_addr_str;
1527
Rob Herringeee675f2012-05-25 10:47:39 +00001528 do_getfile = do_get_tftp;
1529
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001530 if (argc == 1) {
1531 pxefile_addr_str = from_env("pxefile_addr_r");
1532 if (!pxefile_addr_str)
1533 return 1;
1534
1535 } else if (argc == 2) {
1536 pxefile_addr_str = argv[1];
1537 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +00001538 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001539 }
1540
1541 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1542 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1543 return 1;
1544 }
1545
Steven Falco7de0fa82013-10-07 09:51:48 -04001546 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001547
1548 if (cfg == NULL) {
1549 printf("Error parsing config file\n");
1550 return 1;
1551 }
1552
Tom Rini5b5b1762013-09-24 09:05:08 -04001553 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001554
1555 destroy_pxe_menu(cfg);
1556
1557 return 0;
1558}
1559
1560static cmd_tbl_t cmd_pxe_sub[] = {
1561 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1562 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1563};
1564
1565int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1566{
1567 cmd_tbl_t *cp;
1568
1569 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001570 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001571
Rob Herring759456e2013-10-18 13:04:42 -05001572 is_pxe = true;
1573
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001574 /* drop initial "pxe" arg */
1575 argc--;
1576 argv++;
1577
1578 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1579
1580 if (cp)
1581 return cp->cmd(cmdtp, flag, argc, argv);
1582
Simon Glassa06dfc72011-12-10 08:44:01 +00001583 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -05001584}
1585
1586U_BOOT_CMD(
1587 pxe, 3, 1, do_pxe,
1588 "commands to get and boot from pxe files",
1589 "get - try to retrieve a pxe file using tftp\npxe "
1590 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1591);
Stephen Warren857291b2014-02-05 20:49:20 -07001592#endif
Rob Herringeee675f2012-05-25 10:47:39 +00001593
1594/*
1595 * Boots a system using a local disk syslinux/extlinux file
1596 *
1597 * Returns 0 on success, 1 on error.
1598 */
1599int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1600{
1601 unsigned long pxefile_addr_r;
1602 struct pxe_menu *cfg;
1603 char *pxefile_addr_str;
1604 char *filename;
1605 int prompt = 0;
1606
Rob Herring759456e2013-10-18 13:04:42 -05001607 is_pxe = false;
1608
Rob Herringeee675f2012-05-25 10:47:39 +00001609 if (strstr(argv[1], "-p")) {
1610 prompt = 1;
1611 argc--;
1612 argv++;
1613 }
1614
1615 if (argc < 4)
1616 return cmd_usage(cmdtp);
1617
1618 if (argc < 5) {
1619 pxefile_addr_str = from_env("pxefile_addr_r");
1620 if (!pxefile_addr_str)
1621 return 1;
1622 } else {
1623 pxefile_addr_str = argv[4];
1624 }
1625
1626 if (argc < 6)
1627 filename = getenv("bootfile");
1628 else {
1629 filename = argv[5];
1630 setenv("bootfile", filename);
1631 }
1632
1633 if (strstr(argv[3], "ext2"))
1634 do_getfile = do_get_ext2;
1635 else if (strstr(argv[3], "fat"))
1636 do_getfile = do_get_fat;
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001637 else if (strstr(argv[3], "any"))
1638 do_getfile = do_get_any;
Rob Herringeee675f2012-05-25 10:47:39 +00001639 else {
1640 printf("Invalid filesystem: %s\n", argv[3]);
1641 return 1;
1642 }
1643 fs_argv[1] = argv[1];
1644 fs_argv[2] = argv[2];
1645
1646 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1647 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1648 return 1;
1649 }
1650
Steven Falco7de0fa82013-10-07 09:51:48 -04001651 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
Rob Herringeee675f2012-05-25 10:47:39 +00001652 printf("Error reading config file\n");
1653 return 1;
1654 }
1655
Steven Falco7de0fa82013-10-07 09:51:48 -04001656 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
Rob Herringeee675f2012-05-25 10:47:39 +00001657
1658 if (cfg == NULL) {
1659 printf("Error parsing config file\n");
1660 return 1;
1661 }
1662
1663 if (prompt)
1664 cfg->prompt = 1;
1665
Tom Rini5b5b1762013-09-24 09:05:08 -04001666 handle_pxe_menu(cmdtp, cfg);
Rob Herringeee675f2012-05-25 10:47:39 +00001667
1668 destroy_pxe_menu(cfg);
1669
1670 return 0;
1671}
1672
1673U_BOOT_CMD(
1674 sysboot, 7, 1, do_sysboot,
1675 "command to get and boot from syslinux files",
Dennis Gilmorecd90dc82014-02-04 05:25:46 -06001676 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1677 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1678 " or any filesystem on 'dev' on 'interface' to address 'addr'"
Rob Herringeee675f2012-05-25 10:47:39 +00001679);