Blame view

common/cmd_pxe.c 35.9 KB
06283a640   Jason Hobbs   Add pxe command
1
2
3
  /*
   * Copyright 2010-2011 Calxeda, Inc.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
4
   * SPDX-License-Identifier:	GPL-2.0+
06283a640   Jason Hobbs   Add pxe command
5
   */
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
6

06283a640   Jason Hobbs   Add pxe command
7
8
9
10
11
12
13
  #include <common.h>
  #include <command.h>
  #include <malloc.h>
  #include <linux/string.h>
  #include <linux/ctype.h>
  #include <errno.h>
  #include <linux/list.h>
6d1a3e5fa   Dennis Gilmore   cmd_pxe.c add any...
14
  #include <fs.h>
06283a640   Jason Hobbs   Add pxe command
15
16
17
18
  
  #include "menu.h"
  
  #define MAX_TFTP_PATH_LEN 127
39f985536   Rob Herring   pxe: add support ...
19
  const char *pxe_default_paths[] = {
58d9ff936   Joe Hershberger   net: Fix build re...
20
  #ifdef CONFIG_SYS_SOC
39f985536   Rob Herring   pxe: add support ...
21
  	"default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
58d9ff936   Joe Hershberger   net: Fix build re...
22
  #endif
39f985536   Rob Herring   pxe: add support ...
23
24
25
26
  	"default-" CONFIG_SYS_ARCH,
  	"default",
  	NULL
  };
e5a9a4076   Rob Herring   pxe: fix handling...
27
  static bool is_pxe;
06283a640   Jason Hobbs   Add pxe command
28
29
30
31
32
  /*
   * Like getenv, but prints an error if envvar isn't defined in the
   * environment.  It always returns what getenv does, so it can be used in
   * place of getenv without changing error handling otherwise.
   */
23b7194e6   Rob Herring   pxe: make string ...
33
  static char *from_env(const char *envvar)
06283a640   Jason Hobbs   Add pxe command
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  {
  	char *ret;
  
  	ret = getenv(envvar);
  
  	if (!ret)
  		printf("missing environment variable: %s
  ", envvar);
  
  	return ret;
  }
  
  /*
   * Convert an ethaddr from the environment to the format used by pxelinux
   * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
   * the beginning of the ethernet address to indicate a hardware type of
   * Ethernet. Also converts uppercase hex characters into lowercase, to match
   * pxelinux's behavior.
   *
   * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
   * environment, or some other value < 0 on error.
   */
  static int format_mac_pxe(char *outbuf, size_t outbuf_len)
  {
ef034c9d7   Rob Herring   pxe: Use ethact s...
58
  	uchar ethaddr[6];
06283a640   Jason Hobbs   Add pxe command
59

ef034c9d7   Rob Herring   pxe: Use ethact s...
60
  	if (outbuf_len < 21) {
5cea95cb5   David Feng   cmd_pxe: remove c...
61
62
  		printf("outbuf is too small (%zd < 21)
  ", outbuf_len);
06283a640   Jason Hobbs   Add pxe command
63
64
65
  
  		return -EINVAL;
  	}
ef034c9d7   Rob Herring   pxe: Use ethact s...
66
67
68
  	if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
  					  ethaddr))
  		return -ENOENT;
06283a640   Jason Hobbs   Add pxe command
69

ef034c9d7   Rob Herring   pxe: Use ethact s...
70
71
72
  	sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
  		ethaddr[0], ethaddr[1], ethaddr[2],
  		ethaddr[3], ethaddr[4], ethaddr[5]);
06283a640   Jason Hobbs   Add pxe command
73
74
75
76
77
78
79
80
81
  
  	return 1;
  }
  
  /*
   * Returns the directory the file specified in the bootfile env variable is
   * in. If bootfile isn't defined in the environment, return NULL, which should
   * be interpreted as "don't prepend anything to paths".
   */
90ba7d7c4   Rob Herring   pxe: support abso...
82
83
  static int get_bootfile_path(const char *file_path, char *bootfile_path,
  			     size_t bootfile_path_size)
06283a640   Jason Hobbs   Add pxe command
84
85
  {
  	char *bootfile, *last_slash;
90ba7d7c4   Rob Herring   pxe: support abso...
86
  	size_t path_len = 0;
e5a9a4076   Rob Herring   pxe: fix handling...
87
88
  	/* Only syslinux allows absolute paths */
  	if (file_path[0] == '/' && !is_pxe)
90ba7d7c4   Rob Herring   pxe: support abso...
89
  		goto ret;
06283a640   Jason Hobbs   Add pxe command
90
91
  
  	bootfile = from_env("bootfile");
90ba7d7c4   Rob Herring   pxe: support abso...
92
93
  	if (!bootfile)
  		goto ret;
06283a640   Jason Hobbs   Add pxe command
94
95
  
  	last_slash = strrchr(bootfile, '/');
90ba7d7c4   Rob Herring   pxe: support abso...
96
97
  	if (last_slash == NULL)
  		goto ret;
06283a640   Jason Hobbs   Add pxe command
98
99
100
101
  
  	path_len = (last_slash - bootfile) + 1;
  
  	if (bootfile_path_size < path_len) {
5cea95cb5   David Feng   cmd_pxe: remove c...
102
103
  		printf("bootfile_path too small. (%zd < %zd)
  ",
06283a640   Jason Hobbs   Add pxe command
104
105
106
107
108
109
  				bootfile_path_size, path_len);
  
  		return -1;
  	}
  
  	strncpy(bootfile_path, bootfile, path_len);
90ba7d7c4   Rob Herring   pxe: support abso...
110
   ret:
06283a640   Jason Hobbs   Add pxe command
111
112
113
114
  	bootfile_path[path_len] = '\0';
  
  	return 1;
  }
0e3f3f8a3   Steven Falco   Prevent null poin...
115
  static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
669df7e42   Rob Herring   pxe: add support ...
116

0e3f3f8a3   Steven Falco   Prevent null poin...
117
  static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
669df7e42   Rob Herring   pxe: add support ...
118
119
120
121
  {
  	char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
  
  	tftp_argv[1] = file_addr;
23b7194e6   Rob Herring   pxe: make string ...
122
  	tftp_argv[2] = (void *)file_path;
669df7e42   Rob Herring   pxe: add support ...
123

0e3f3f8a3   Steven Falco   Prevent null poin...
124
  	if (do_tftpb(cmdtp, 0, 3, tftp_argv))
669df7e42   Rob Herring   pxe: add support ...
125
126
127
128
129
130
  		return -ENOENT;
  
  	return 1;
  }
  
  static char *fs_argv[5];
0e3f3f8a3   Steven Falco   Prevent null poin...
131
  static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
669df7e42   Rob Herring   pxe: add support ...
132
133
134
135
  {
  #ifdef CONFIG_CMD_EXT2
  	fs_argv[0] = "ext2load";
  	fs_argv[3] = file_addr;
23b7194e6   Rob Herring   pxe: make string ...
136
  	fs_argv[4] = (void *)file_path;
669df7e42   Rob Herring   pxe: add support ...
137

0e3f3f8a3   Steven Falco   Prevent null poin...
138
  	if (!do_ext2load(cmdtp, 0, 5, fs_argv))
669df7e42   Rob Herring   pxe: add support ...
139
140
141
142
  		return 1;
  #endif
  	return -ENOENT;
  }
0e3f3f8a3   Steven Falco   Prevent null poin...
143
  static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
669df7e42   Rob Herring   pxe: add support ...
144
145
146
147
  {
  #ifdef CONFIG_CMD_FAT
  	fs_argv[0] = "fatload";
  	fs_argv[3] = file_addr;
23b7194e6   Rob Herring   pxe: make string ...
148
  	fs_argv[4] = (void *)file_path;
669df7e42   Rob Herring   pxe: add support ...
149

0e3f3f8a3   Steven Falco   Prevent null poin...
150
  	if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
669df7e42   Rob Herring   pxe: add support ...
151
152
153
154
  		return 1;
  #endif
  	return -ENOENT;
  }
6d1a3e5fa   Dennis Gilmore   cmd_pxe.c add any...
155
156
157
158
159
160
161
162
163
164
165
166
  static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
  {
  #ifdef CONFIG_CMD_FS_GENERIC
  	fs_argv[0] = "load";
  	fs_argv[3] = file_addr;
  	fs_argv[4] = (void *)file_path;
  
  	if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
  		return 1;
  #endif
  	return -ENOENT;
  }
06283a640   Jason Hobbs   Add pxe command
167
168
169
170
171
172
173
174
  /*
   * As in pxelinux, paths to files referenced from files we retrieve are
   * relative to the location of bootfile. get_relfile takes such a path and
   * joins it with the bootfile path to get the full path to the target file. If
   * the bootfile path is NULL, we use file_path as is.
   *
   * Returns 1 for success, or < 0 on error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
175
  static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
06283a640   Jason Hobbs   Add pxe command
176
177
178
179
  {
  	size_t path_len;
  	char relfile[MAX_TFTP_PATH_LEN+1];
  	char addr_buf[10];
06283a640   Jason Hobbs   Add pxe command
180
  	int err;
90ba7d7c4   Rob Herring   pxe: support abso...
181
  	err = get_bootfile_path(file_path, relfile, sizeof(relfile));
06283a640   Jason Hobbs   Add pxe command
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  
  	if (err < 0)
  		return err;
  
  	path_len = strlen(file_path);
  	path_len += strlen(relfile);
  
  	if (path_len > MAX_TFTP_PATH_LEN) {
  		printf("Base path too long (%s%s)
  ",
  					relfile,
  					file_path);
  
  		return -ENAMETOOLONG;
  	}
  
  	strcat(relfile, file_path);
  
  	printf("Retrieving file: %s
  ", relfile);
  
  	sprintf(addr_buf, "%p", file_addr);
0e3f3f8a3   Steven Falco   Prevent null poin...
204
  	return do_getfile(cmdtp, relfile, addr_buf);
06283a640   Jason Hobbs   Add pxe command
205
206
207
208
209
210
211
212
213
  }
  
  /*
   * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
   * 'bootfile' was specified in the environment, the path to bootfile will be
   * prepended to 'file_path' and the resulting path will be used.
   *
   * Returns 1 on success, or < 0 for error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
214
  static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
06283a640   Jason Hobbs   Add pxe command
215
216
217
218
  {
  	unsigned long config_file_size;
  	char *tftp_filesize;
  	int err;
0e3f3f8a3   Steven Falco   Prevent null poin...
219
  	err = get_relfile(cmdtp, file_path, file_addr);
06283a640   Jason Hobbs   Add pxe command
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  
  	if (err < 0)
  		return err;
  
  	/*
  	 * the file comes without a NUL byte at the end, so find out its size
  	 * and add the NUL byte.
  	 */
  	tftp_filesize = from_env("filesize");
  
  	if (!tftp_filesize)
  		return -ENOENT;
  
  	if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
  		return -EINVAL;
  
  	*(char *)(file_addr + config_file_size) = '\0';
  
  	return 1;
  }
  
  #define PXELINUX_DIR "pxelinux.cfg/"
  
  /*
   * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
   * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
   * from the bootfile path, as described above.
   *
   * Returns 1 on success or < 0 on error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
250
  static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
06283a640   Jason Hobbs   Add pxe command
251
252
253
254
255
256
257
258
259
260
261
262
  {
  	size_t base_len = strlen(PXELINUX_DIR);
  	char path[MAX_TFTP_PATH_LEN+1];
  
  	if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
  		printf("path (%s%s) too long, skipping
  ",
  				PXELINUX_DIR, file);
  		return -ENAMETOOLONG;
  	}
  
  	sprintf(path, PXELINUX_DIR "%s", file);
0e3f3f8a3   Steven Falco   Prevent null poin...
263
  	return get_pxe_file(cmdtp, path, pxefile_addr_r);
06283a640   Jason Hobbs   Add pxe command
264
265
266
267
268
269
270
  }
  
  /*
   * Looks for a pxe file with a name based on the pxeuuid environment variable.
   *
   * Returns 1 on success or < 0 on error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
271
  static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
06283a640   Jason Hobbs   Add pxe command
272
273
274
275
276
277
278
  {
  	char *uuid_str;
  
  	uuid_str = from_env("pxeuuid");
  
  	if (!uuid_str)
  		return -ENOENT;
0e3f3f8a3   Steven Falco   Prevent null poin...
279
  	return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
06283a640   Jason Hobbs   Add pxe command
280
281
282
283
284
285
286
287
  }
  
  /*
   * Looks for a pxe file with a name based on the 'ethaddr' environment
   * variable.
   *
   * Returns 1 on success or < 0 on error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
288
  static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
06283a640   Jason Hobbs   Add pxe command
289
290
291
292
293
294
295
296
  {
  	char mac_str[21];
  	int err;
  
  	err = format_mac_pxe(mac_str, sizeof(mac_str));
  
  	if (err < 0)
  		return err;
0e3f3f8a3   Steven Falco   Prevent null poin...
297
  	return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
06283a640   Jason Hobbs   Add pxe command
298
299
300
301
302
303
304
305
306
  }
  
  /*
   * Looks for pxe files with names based on our IP address. See pxelinux
   * documentation for details on what these file names look like.  We match
   * that exactly.
   *
   * Returns 1 on success or < 0 on error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
307
  static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
06283a640   Jason Hobbs   Add pxe command
308
309
310
311
312
313
314
  {
  	char ip_addr[9];
  	int mask_pos, err;
  
  	sprintf(ip_addr, "%08X", ntohl(NetOurIP));
  
  	for (mask_pos = 7; mask_pos >= 0;  mask_pos--) {
0e3f3f8a3   Steven Falco   Prevent null poin...
315
  		err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
06283a640   Jason Hobbs   Add pxe command
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  
  		if (err > 0)
  			return err;
  
  		ip_addr[mask_pos] = '\0';
  	}
  
  	return -ENOENT;
  }
  
  /*
   * Entry point for the 'pxe get' command.
   * This Follows pxelinux's rules to download a config file from a tftp server.
   * The file is stored at the location given by the pxefile_addr_r environment
   * variable, which must be set.
   *
   * UUID comes from pxeuuid env variable, if defined
   * MAC addr comes from ethaddr env variable, if defined
   * IP
   *
   * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
   *
   * Returns 0 on success or 1 on error.
   */
  static int
  do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	char *pxefile_addr_str;
834c9384a   Jason Hobbs   cmd_pxe.c: fix st...
344
  	unsigned long pxefile_addr_r;
39f985536   Rob Herring   pxe: add support ...
345
  	int err, i = 0;
06283a640   Jason Hobbs   Add pxe command
346

669df7e42   Rob Herring   pxe: add support ...
347
  	do_getfile = do_get_tftp;
06283a640   Jason Hobbs   Add pxe command
348
  	if (argc != 1)
4c12eeb8b   Simon Glass   Convert cmd_usage...
349
  		return CMD_RET_USAGE;
06283a640   Jason Hobbs   Add pxe command
350

06283a640   Jason Hobbs   Add pxe command
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  	pxefile_addr_str = from_env("pxefile_addr_r");
  
  	if (!pxefile_addr_str)
  		return 1;
  
  	err = strict_strtoul(pxefile_addr_str, 16,
  				(unsigned long *)&pxefile_addr_r);
  	if (err < 0)
  		return 1;
  
  	/*
  	 * Keep trying paths until we successfully get a file we're looking
  	 * for.
  	 */
0e3f3f8a3   Steven Falco   Prevent null poin...
365
366
367
  	if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
  	    pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
  	    pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
06283a640   Jason Hobbs   Add pxe command
368
369
370
371
372
  		printf("Config file found
  ");
  
  		return 0;
  	}
39f985536   Rob Herring   pxe: add support ...
373
  	while (pxe_default_paths[i]) {
0e3f3f8a3   Steven Falco   Prevent null poin...
374
  		if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
39f985536   Rob Herring   pxe: add support ...
375
376
377
378
379
380
381
  				      (void *)pxefile_addr_r) > 0) {
  			printf("Config file found
  ");
  			return 0;
  		}
  		i++;
  	}
06283a640   Jason Hobbs   Add pxe command
382
383
384
385
386
387
388
389
390
391
392
393
394
  	printf("Config file not found
  ");
  
  	return 1;
  }
  
  /*
   * Wrapper to make it easier to store the file at file_path in the location
   * specified by envaddr_name. file_path will be joined to the bootfile path,
   * if any is specified.
   *
   * Returns 1 on success or < 0 on error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
395
  static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
06283a640   Jason Hobbs   Add pxe command
396
  {
834c9384a   Jason Hobbs   cmd_pxe.c: fix st...
397
  	unsigned long file_addr;
06283a640   Jason Hobbs   Add pxe command
398
399
400
401
402
403
  	char *envaddr;
  
  	envaddr = from_env(envaddr_name);
  
  	if (!envaddr)
  		return -ENOENT;
834c9384a   Jason Hobbs   cmd_pxe.c: fix st...
404
  	if (strict_strtoul(envaddr, 16, &file_addr) < 0)
06283a640   Jason Hobbs   Add pxe command
405
  		return -EINVAL;
0e3f3f8a3   Steven Falco   Prevent null poin...
406
  	return get_relfile(cmdtp, file_path, (void *)file_addr);
06283a640   Jason Hobbs   Add pxe command
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  }
  
  /*
   * A note on the pxe file parser.
   *
   * We're parsing files that use syslinux grammar, which has a few quirks.
   * String literals must be recognized based on context - there is no
   * quoting or escaping support. There's also nothing to explicitly indicate
   * when a label section completes. We deal with that by ending a label
   * section whenever we see a line that doesn't include.
   *
   * As with the syslinux family, this same file format could be reused in the
   * future for non pxe purposes. The only action it takes during parsing that
   * would throw this off is handling of include files. It assumes we're using
   * pxe, and does a tftp download of a file listed as an include file in the
   * middle of the parsing operation. That could be handled by refactoring it to
   * take a 'include file getter' function.
   */
  
  /*
   * Describes a single label given in a pxe file.
   *
   * Create these with the 'label_create' function given below.
   *
   * name - the name of the menu as given on the 'menu label' line.
   * kernel - the path to the kernel file to use for this label.
   * append - kernel command line to use when booting this label
   * initrd - path to the initrd to use for this label.
   * attempted - 0 if we haven't tried to boot this label, 1 if we have.
   * localboot - 1 if this label specified 'localboot', 0 otherwise.
   * list - lets these form a list, which a pxe_menu struct will hold.
   */
  struct pxe_label {
32d2ffe73   Rob Herring   pxe: simplify men...
440
  	char num[4];
06283a640   Jason Hobbs   Add pxe command
441
  	char *name;
7815c4e89   Rob Herring   pxe: add support ...
442
  	char *menu;
06283a640   Jason Hobbs   Add pxe command
443
444
445
  	char *kernel;
  	char *append;
  	char *initrd;
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
446
  	char *fdt;
c61d94d86   Stephen Warren   pxe: implement fd...
447
  	char *fdtdir;
98f646764   Rob Herring   pxe: add ipappend...
448
  	int ipappend;
06283a640   Jason Hobbs   Add pxe command
449
450
  	int attempted;
  	int localboot;
500f304b6   Rob Herring   pxe: fix handling...
451
  	int localboot_val;
06283a640   Jason Hobbs   Add pxe command
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  	struct list_head list;
  };
  
  /*
   * Describes a pxe menu as given via pxe files.
   *
   * title - the name of the menu as given by a 'menu title' line.
   * default_label - the name of the default label, if any.
   * timeout - time in tenths of a second to wait for a user key-press before
   *           booting the default label.
   * prompt - if 0, don't prompt for a choice unless the timeout period is
   *          interrupted.  If 1, always prompt for a choice regardless of
   *          timeout.
   * labels - a list of labels defined for the menu.
   */
  struct pxe_menu {
  	char *title;
  	char *default_label;
  	int timeout;
  	int prompt;
  	struct list_head labels;
  };
  
  /*
   * Allocates memory for and initializes a pxe_label. This uses malloc, so the
   * result must be free()'d to reclaim the memory.
   *
   * Returns NULL if malloc fails.
   */
  static struct pxe_label *label_create(void)
  {
  	struct pxe_label *label;
  
  	label = malloc(sizeof(struct pxe_label));
  
  	if (!label)
  		return NULL;
  
  	memset(label, 0, sizeof(struct pxe_label));
  
  	return label;
  }
  
  /*
   * Free the memory used by a pxe_label, including that used by its name,
   * kernel, append and initrd members, if they're non NULL.
   *
   * So - be sure to only use dynamically allocated memory for the members of
   * the pxe_label struct, unless you want to clean it up first. These are
   * currently only created by the pxe file parsing code.
   */
  static void label_destroy(struct pxe_label *label)
  {
  	if (label->name)
  		free(label->name);
  
  	if (label->kernel)
  		free(label->kernel);
  
  	if (label->append)
  		free(label->append);
  
  	if (label->initrd)
  		free(label->initrd);
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
516
517
  	if (label->fdt)
  		free(label->fdt);
c61d94d86   Stephen Warren   pxe: implement fd...
518
519
  	if (label->fdtdir)
  		free(label->fdtdir);
06283a640   Jason Hobbs   Add pxe command
520
521
522
523
524
525
526
527
528
529
530
531
  	free(label);
  }
  
  /*
   * Print a label and its string members if they're defined.
   *
   * This is passed as a callback to the menu code for displaying each
   * menu entry.
   */
  static void label_print(void *data)
  {
  	struct pxe_label *label = data;
32d2ffe73   Rob Herring   pxe: simplify men...
532
  	const char *c = label->menu ? label->menu : label->name;
06283a640   Jason Hobbs   Add pxe command
533

32d2ffe73   Rob Herring   pxe: simplify men...
534
535
  	printf("%s:\t%s
  ", label->num, c);
06283a640   Jason Hobbs   Add pxe command
536
537
538
539
540
541
542
543
544
545
546
547
548
  }
  
  /*
   * Boot a label that specified 'localboot'. This requires that the 'localcmd'
   * environment variable is defined. Its contents will be executed as U-boot
   * command.  If the label specified an 'append' line, its contents will be
   * used to overwrite the contents of the 'bootargs' environment variable prior
   * to running 'localcmd'.
   *
   * Returns 1 on success or < 0 on error.
   */
  static int label_localboot(struct pxe_label *label)
  {
d51004a83   Simon Glass   Add run_command_l...
549
  	char *localcmd;
06283a640   Jason Hobbs   Add pxe command
550
551
552
553
554
  
  	localcmd = from_env("localcmd");
  
  	if (!localcmd)
  		return -ENOENT;
06283a640   Jason Hobbs   Add pxe command
555
556
  	if (label->append)
  		setenv("bootargs", label->append);
d51004a83   Simon Glass   Add run_command_l...
557
558
  	debug("running: %s
  ", localcmd);
06283a640   Jason Hobbs   Add pxe command
559

d51004a83   Simon Glass   Add run_command_l...
560
  	return run_command_list(localcmd, strlen(localcmd), 0);
06283a640   Jason Hobbs   Add pxe command
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  }
  
  /*
   * Boot according to the contents of a pxe_label.
   *
   * If we can't boot for any reason, we return.  A successful boot never
   * returns.
   *
   * The kernel will be stored in the location given by the 'kernel_addr_r'
   * environment variable.
   *
   * If the label specifies an initrd file, it will be stored in the location
   * given by the 'ramdisk_addr_r' environment variable.
   *
   * If the label specifies an 'append' line, its contents will overwrite that
   * of the 'bootargs' environment variable.
   */
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
578
  static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
06283a640   Jason Hobbs   Add pxe command
579
580
  {
  	char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
e6b6ccf20   Rob Herring   pxe: try bootz if...
581
  	char initrd_str[22];
98f646764   Rob Herring   pxe: add ipappend...
582
583
584
  	char mac_str[29] = "";
  	char ip_str[68] = "";
  	char *bootargs;
06283a640   Jason Hobbs   Add pxe command
585
  	int bootm_argc = 3;
98f646764   Rob Herring   pxe: add ipappend...
586
  	int len = 0;
06283a640   Jason Hobbs   Add pxe command
587
588
589
590
591
592
  
  	label_print(label);
  
  	label->attempted = 1;
  
  	if (label->localboot) {
500f304b6   Rob Herring   pxe: fix handling...
593
594
595
  		if (label->localboot_val >= 0)
  			label_localboot(label);
  		return 0;
06283a640   Jason Hobbs   Add pxe command
596
597
598
599
600
601
  	}
  
  	if (label->kernel == NULL) {
  		printf("No kernel given, skipping %s
  ",
  				label->name);
500f304b6   Rob Herring   pxe: fix handling...
602
  		return 1;
06283a640   Jason Hobbs   Add pxe command
603
604
605
  	}
  
  	if (label->initrd) {
0e3f3f8a3   Steven Falco   Prevent null poin...
606
  		if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
06283a640   Jason Hobbs   Add pxe command
607
608
609
  			printf("Skipping %s for failure retrieving initrd
  ",
  					label->name);
500f304b6   Rob Herring   pxe: fix handling...
610
  			return 1;
06283a640   Jason Hobbs   Add pxe command
611
  		}
e6b6ccf20   Rob Herring   pxe: try bootz if...
612
613
614
615
  		bootm_argv[2] = initrd_str;
  		strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
  		strcat(bootm_argv[2], ":");
  		strcat(bootm_argv[2], getenv("filesize"));
06283a640   Jason Hobbs   Add pxe command
616
617
618
  	} else {
  		bootm_argv[2] = "-";
  	}
0e3f3f8a3   Steven Falco   Prevent null poin...
619
  	if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
06283a640   Jason Hobbs   Add pxe command
620
621
622
  		printf("Skipping %s for failure retrieving kernel
  ",
  				label->name);
500f304b6   Rob Herring   pxe: fix handling...
623
  		return 1;
06283a640   Jason Hobbs   Add pxe command
624
  	}
98f646764   Rob Herring   pxe: add ipappend...
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  	if (label->ipappend & 0x1) {
  		sprintf(ip_str, " ip=%s:%s:%s:%s",
  			getenv("ipaddr"), getenv("serverip"),
  			getenv("gatewayip"), getenv("netmask"));
  		len += strlen(ip_str);
  	}
  
  	if (label->ipappend & 0x2) {
  		int err;
  		strcpy(mac_str, " BOOTIF=");
  		err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
  		if (err < 0)
  			mac_str[0] = '\0';
  		len += strlen(mac_str);
  	}
  
  	if (label->append)
  		len += strlen(label->append);
  
  	if (len) {
  		bootargs = malloc(len + 1);
  		if (!bootargs)
  			return 1;
  		bootargs[0] = '\0';
  		if (label->append)
  			strcpy(bootargs, label->append);
  		strcat(bootargs, ip_str);
  		strcat(bootargs, mac_str);
  
  		setenv("bootargs", bootargs);
  		printf("append: %s
  ", bootargs);
  
  		free(bootargs);
32d2ffe73   Rob Herring   pxe: simplify men...
659
  	}
06283a640   Jason Hobbs   Add pxe command
660
661
662
663
  
  	bootm_argv[1] = getenv("kernel_addr_r");
  
  	/*
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
664
665
666
667
668
669
670
671
672
673
674
  	 * fdt usage is optional:
  	 * It handles the following scenarios. All scenarios are exclusive
  	 *
  	 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
  	 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
  	 * and adjust argc appropriately.
  	 *
  	 * Scenario 2: If there is an fdt_addr specified, pass it along to
  	 * bootm, and adjust argc appropriately.
  	 *
  	 * Scenario 3: fdt blob is not available.
06283a640   Jason Hobbs   Add pxe command
675
  	 */
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
676
677
678
  	bootm_argv[3] = getenv("fdt_addr_r");
  
  	/* if fdt label is defined then get fdt from server */
c61d94d86   Stephen Warren   pxe: implement fd...
679
680
681
682
683
684
685
  	if (bootm_argv[3]) {
  		char *fdtfile = NULL;
  		char *fdtfilefree = NULL;
  
  		if (label->fdt) {
  			fdtfile = label->fdt;
  		} else if (label->fdtdir) {
e22361af0   Stephen Warren   pxe: prepend fdtd...
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
  			char *f1, *f2, *f3, *f4, *slash;
  
  			f1 = getenv("fdtfile");
  			if (f1) {
  				f2 = "";
  				f3 = "";
  				f4 = "";
  			} else {
  				/*
  				 * For complex cases where this code doesn't
  				 * generate the correct filename, the board
  				 * code should set $fdtfile during early boot,
  				 * or the boot scripts should set $fdtfile
  				 * before invoking "pxe" or "sysboot".
  				 */
  				f1 = getenv("soc");
  				f2 = "-";
  				f3 = getenv("board");
  				f4 = ".dtb";
c61d94d86   Stephen Warren   pxe: implement fd...
705
  			}
e22361af0   Stephen Warren   pxe: prepend fdtd...
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
  
  			len = strlen(label->fdtdir);
  			if (!len)
  				slash = "./";
  			else if (label->fdtdir[len - 1] != '/')
  				slash = "/";
  			else
  				slash = "";
  
  			len = strlen(label->fdtdir) + strlen(slash) +
  				strlen(f1) + strlen(f2) + strlen(f3) +
  				strlen(f4) + 1;
  			fdtfilefree = malloc(len);
  			if (!fdtfilefree) {
  				printf("malloc fail (FDT filename)
  ");
  				return 1;
  			}
  
  			snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
  				 label->fdtdir, slash, f1, f2, f3, f4);
  			fdtfile = fdtfilefree;
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
728
  		}
c61d94d86   Stephen Warren   pxe: implement fd...
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  
  		if (fdtfile) {
  			int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
  			free(fdtfilefree);
  			if (err < 0) {
  				printf("Skipping %s for failure retrieving fdt
  ",
  						label->name);
  				return 1;
  			}
  		} else {
  			bootm_argv[3] = NULL;
  		}
  	}
  
  	if (!bootm_argv[3])
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
745
  		bootm_argv[3] = getenv("fdt_addr");
06283a640   Jason Hobbs   Add pxe command
746
747
748
  
  	if (bootm_argv[3])
  		bootm_argc = 4;
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
749
  	do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
e6b6ccf20   Rob Herring   pxe: try bootz if...
750
751
752
  
  #ifdef CONFIG_CMD_BOOTZ
  	/* Try booting a zImage if do_bootm returns */
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
753
  	do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
e6b6ccf20   Rob Herring   pxe: try bootz if...
754
  #endif
500f304b6   Rob Herring   pxe: fix handling...
755
  	return 1;
06283a640   Jason Hobbs   Add pxe command
756
757
758
759
760
761
762
763
764
765
766
767
768
769
  }
  
  /*
   * Tokens for the pxe file parser.
   */
  enum token_type {
  	T_EOL,
  	T_STRING,
  	T_EOF,
  	T_MENU,
  	T_TITLE,
  	T_TIMEOUT,
  	T_LABEL,
  	T_KERNEL,
beb9f6c67   Rob Herring   pxe: support linu...
770
  	T_LINUX,
06283a640   Jason Hobbs   Add pxe command
771
772
773
774
775
776
  	T_APPEND,
  	T_INITRD,
  	T_LOCALBOOT,
  	T_DEFAULT,
  	T_PROMPT,
  	T_INCLUDE,
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
777
  	T_FDT,
c61d94d86   Stephen Warren   pxe: implement fd...
778
  	T_FDTDIR,
8577fec97   Rob Herring   pxe: add support ...
779
  	T_ONTIMEOUT,
98f646764   Rob Herring   pxe: add ipappend...
780
  	T_IPAPPEND,
06283a640   Jason Hobbs   Add pxe command
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
  	T_INVALID
  };
  
  /*
   * A token - given by a value and a type.
   */
  struct token {
  	char *val;
  	enum token_type type;
  };
  
  /*
   * Keywords recognized.
   */
  static const struct token keywords[] = {
  	{"menu", T_MENU},
  	{"title", T_TITLE},
  	{"timeout", T_TIMEOUT},
  	{"default", T_DEFAULT},
  	{"prompt", T_PROMPT},
  	{"label", T_LABEL},
  	{"kernel", T_KERNEL},
beb9f6c67   Rob Herring   pxe: support linu...
803
  	{"linux", T_LINUX},
06283a640   Jason Hobbs   Add pxe command
804
805
806
807
  	{"localboot", T_LOCALBOOT},
  	{"append", T_APPEND},
  	{"initrd", T_INITRD},
  	{"include", T_INCLUDE},
f43c401b7   Stephen Warren   pxe: support "dev...
808
  	{"devicetree", T_FDT},
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
809
  	{"fdt", T_FDT},
c61d94d86   Stephen Warren   pxe: implement fd...
810
811
  	{"devicetreedir", T_FDTDIR},
  	{"fdtdir", T_FDTDIR},
8577fec97   Rob Herring   pxe: add support ...
812
  	{"ontimeout", T_ONTIMEOUT,},
98f646764   Rob Herring   pxe: add ipappend...
813
  	{"ipappend", T_IPAPPEND,},
06283a640   Jason Hobbs   Add pxe command
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
  	{NULL, T_INVALID}
  };
  
  /*
   * Since pxe(linux) files don't have a token to identify the start of a
   * literal, we have to keep track of when we're in a state where a literal is
   * expected vs when we're in a state a keyword is expected.
   */
  enum lex_state {
  	L_NORMAL = 0,
  	L_KEYWORD,
  	L_SLITERAL
  };
  
  /*
   * get_string retrieves a string from *p and stores it as a token in
   * *t.
   *
   * get_string used for scanning both string literals and keywords.
   *
   * Characters from *p are copied into t-val until a character equal to
   * delim is found, or a NUL byte is reached. If delim has the special value of
   * ' ', any whitespace character will be used as a delimiter.
   *
   * If lower is unequal to 0, uppercase characters will be converted to
   * lowercase in the result. This is useful to make keywords case
   * insensitive.
   *
   * The location of *p is updated to point to the first character after the end
   * of the token - the ending delimiter.
   *
   * On success, the new value of t->val is returned. Memory for t->val is
   * allocated using malloc and must be free()'d to reclaim it.  If insufficient
   * memory is available, NULL is returned.
   */
  static char *get_string(char **p, struct token *t, char delim, int lower)
  {
  	char *b, *e;
  	size_t len, i;
  
  	/*
  	 * b and e both start at the beginning of the input stream.
  	 *
  	 * e is incremented until we find the ending delimiter, or a NUL byte
  	 * is reached. Then, we take e - b to find the length of the token.
  	 */
  	b = e = *p;
  
  	while (*e) {
  		if ((delim == ' ' && isspace(*e)) || delim == *e)
  			break;
  		e++;
  	}
  
  	len = e - b;
  
  	/*
  	 * Allocate memory to hold the string, and copy it in, converting
  	 * characters to lowercase if lower is != 0.
  	 */
  	t->val = malloc(len + 1);
  	if (!t->val)
  		return NULL;
  
  	for (i = 0; i < len; i++, b++) {
  		if (lower)
  			t->val[i] = tolower(*b);
  		else
  			t->val[i] = *b;
  	}
  
  	t->val[len] = '\0';
  
  	/*
  	 * Update *p so the caller knows where to continue scanning.
  	 */
  	*p = e;
  
  	t->type = T_STRING;
  
  	return t->val;
  }
  
  /*
   * Populate a keyword token with a type and value.
   */
  static void get_keyword(struct token *t)
  {
  	int i;
  
  	for (i = 0; keywords[i].val; i++) {
  		if (!strcmp(t->val, keywords[i].val)) {
  			t->type = keywords[i].type;
  			break;
  		}
  	}
  }
  
  /*
   * Get the next token.  We have to keep track of which state we're in to know
   * if we're looking to get a string literal or a keyword.
   *
   * *p is updated to point at the first character after the current token.
   */
  static void get_token(char **p, struct token *t, enum lex_state state)
  {
  	char *c = *p;
  
  	t->type = T_INVALID;
  
  	/* eat non EOL whitespace */
  	while (isblank(*c))
  		c++;
  
  	/*
  	 * eat comments. note that string literals can't begin with #, but
  	 * can contain a # after their first character.
  	 */
  	if (*c == '#') {
  		while (*c && *c != '
  ')
  			c++;
  	}
  
  	if (*c == '
  ') {
  		t->type = T_EOL;
  		c++;
  	} else if (*c == '\0') {
  		t->type = T_EOF;
  		c++;
  	} else if (state == L_SLITERAL) {
  		get_string(&c, t, '
  ', 0);
  	} else if (state == L_KEYWORD) {
  		/*
  		 * when we expect a keyword, we first get the next string
  		 * token delimited by whitespace, and then check if it
  		 * matches a keyword in our keyword list. if it does, it's
  		 * converted to a keyword token of the appropriate type, and
  		 * if not, it remains a string token.
  		 */
  		get_string(&c, t, ' ', 1);
  		get_keyword(t);
  	}
  
  	*p = c;
  }
  
  /*
   * Increment *c until we get to the end of the current line, or EOF.
   */
  static void eol_or_eof(char **c)
  {
  	while (**c && **c != '
  ')
  		(*c)++;
  }
  
  /*
   * All of these parse_* functions share some common behavior.
   *
   * They finish with *c pointing after the token they parse, and return 1 on
   * success, or < 0 on error.
   */
  
  /*
   * Parse a string literal and store a pointer it at *dst. String literals
   * terminate at the end of the line.
   */
  static int parse_sliteral(char **c, char **dst)
  {
  	struct token t;
  	char *s = *c;
  
  	get_token(c, &t, L_SLITERAL);
  
  	if (t.type != T_STRING) {
  		printf("Expected string literal: %.*s
  ", (int)(*c - s), s);
  		return -EINVAL;
  	}
  
  	*dst = t.val;
  
  	return 1;
  }
  
  /*
   * Parse a base 10 (unsigned) integer and store it at *dst.
   */
  static int parse_integer(char **c, int *dst)
  {
  	struct token t;
  	char *s = *c;
06283a640   Jason Hobbs   Add pxe command
1009
1010
1011
1012
1013
1014
1015
1016
  
  	get_token(c, &t, L_SLITERAL);
  
  	if (t.type != T_STRING) {
  		printf("Expected string: %.*s
  ", (int)(*c - s), s);
  		return -EINVAL;
  	}
500f304b6   Rob Herring   pxe: fix handling...
1017
  	*dst = simple_strtol(t.val, NULL, 10);
06283a640   Jason Hobbs   Add pxe command
1018
1019
1020
1021
1022
  
  	free(t.val);
  
  	return 1;
  }
0e3f3f8a3   Steven Falco   Prevent null poin...
1023
  static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
06283a640   Jason Hobbs   Add pxe command
1024
1025
1026
1027
1028
1029
1030
1031
1032
  
  /*
   * Parse an include statement, and retrieve and parse the file it mentions.
   *
   * base should point to a location where it's safe to store the file, and
   * nest_level should indicate how many nested includes have occurred. For this
   * include, nest_level has already been incremented and doesn't need to be
   * incremented here.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
1033
  static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
06283a640   Jason Hobbs   Add pxe command
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
  				struct pxe_menu *cfg, int nest_level)
  {
  	char *include_path;
  	char *s = *c;
  	int err;
  
  	err = parse_sliteral(c, &include_path);
  
  	if (err < 0) {
  		printf("Expected include path: %.*s
  ",
  				 (int)(*c - s), s);
  		return err;
  	}
0e3f3f8a3   Steven Falco   Prevent null poin...
1048
  	err = get_pxe_file(cmdtp, include_path, base);
06283a640   Jason Hobbs   Add pxe command
1049
1050
1051
1052
1053
1054
  
  	if (err < 0) {
  		printf("Couldn't retrieve %s
  ", include_path);
  		return err;
  	}
0e3f3f8a3   Steven Falco   Prevent null poin...
1055
  	return parse_pxefile_top(cmdtp, base, cfg, nest_level);
06283a640   Jason Hobbs   Add pxe command
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
  }
  
  /*
   * Parse lines that begin with 'menu'.
   *
   * b and nest are provided to handle the 'menu include' case.
   *
   * b should be the address where the file currently being parsed is stored.
   *
   * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
   * a file it includes, 3 when parsing a file included by that file, and so on.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
1068
  static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
06283a640   Jason Hobbs   Add pxe command
1069
1070
1071
  {
  	struct token t;
  	char *s = *c;
43d4a5e68   Heiko Schocher   common/cmd_pxe.c:...
1072
  	int err = 0;
06283a640   Jason Hobbs   Add pxe command
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
  
  	get_token(c, &t, L_KEYWORD);
  
  	switch (t.type) {
  	case T_TITLE:
  		err = parse_sliteral(c, &cfg->title);
  
  		break;
  
  	case T_INCLUDE:
0e3f3f8a3   Steven Falco   Prevent null poin...
1083
  		err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
06283a640   Jason Hobbs   Add pxe command
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  						nest_level + 1);
  		break;
  
  	default:
  		printf("Ignoring malformed menu command: %.*s
  ",
  				(int)(*c - s), s);
  	}
  
  	if (err < 0)
  		return err;
  
  	eol_or_eof(c);
  
  	return 1;
  }
  
  /*
   * Handles parsing a 'menu line' when we're parsing a label.
   */
  static int parse_label_menu(char **c, struct pxe_menu *cfg,
  				struct pxe_label *label)
  {
  	struct token t;
  	char *s;
  
  	s = *c;
  
  	get_token(c, &t, L_KEYWORD);
  
  	switch (t.type) {
  	case T_DEFAULT:
8577fec97   Rob Herring   pxe: add support ...
1116
1117
  		if (!cfg->default_label)
  			cfg->default_label = strdup(label->name);
06283a640   Jason Hobbs   Add pxe command
1118
1119
1120
1121
1122
  
  		if (!cfg->default_label)
  			return -ENOMEM;
  
  		break;
7815c4e89   Rob Herring   pxe: add support ...
1123
1124
1125
  	case T_LABEL:
  		parse_sliteral(c, &label->menu);
  		break;
06283a640   Jason Hobbs   Add pxe command
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
  	default:
  		printf("Ignoring malformed menu command: %.*s
  ",
  				(int)(*c - s), s);
  	}
  
  	eol_or_eof(c);
  
  	return 0;
  }
  
  /*
   * Parses a label and adds it to the list of labels for a menu.
   *
   * A label ends when we either get to the end of a file, or
   * get some input we otherwise don't have a handler defined
   * for.
   *
   */
  static int parse_label(char **c, struct pxe_menu *cfg)
  {
  	struct token t;
34bd23e42   Rob Herring   pxe: parse initrd...
1148
  	int len;
06283a640   Jason Hobbs   Add pxe command
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
  	char *s = *c;
  	struct pxe_label *label;
  	int err;
  
  	label = label_create();
  	if (!label)
  		return -ENOMEM;
  
  	err = parse_sliteral(c, &label->name);
  	if (err < 0) {
  		printf("Expected label name: %.*s
  ", (int)(*c - s), s);
  		label_destroy(label);
  		return -EINVAL;
  	}
  
  	list_add_tail(&label->list, &cfg->labels);
  
  	while (1) {
  		s = *c;
  		get_token(c, &t, L_KEYWORD);
  
  		err = 0;
  		switch (t.type) {
  		case T_MENU:
  			err = parse_label_menu(c, cfg, label);
  			break;
  
  		case T_KERNEL:
beb9f6c67   Rob Herring   pxe: support linu...
1178
  		case T_LINUX:
06283a640   Jason Hobbs   Add pxe command
1179
1180
1181
1182
1183
  			err = parse_sliteral(c, &label->kernel);
  			break;
  
  		case T_APPEND:
  			err = parse_sliteral(c, &label->append);
34bd23e42   Rob Herring   pxe: parse initrd...
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
  			if (label->initrd)
  				break;
  			s = strstr(label->append, "initrd=");
  			if (!s)
  				break;
  			s += 7;
  			len = (int)(strchr(s, ' ') - s);
  			label->initrd = malloc(len + 1);
  			strncpy(label->initrd, s, len);
  			label->initrd[len] = '\0';
06283a640   Jason Hobbs   Add pxe command
1194
1195
1196
  			break;
  
  		case T_INITRD:
34bd23e42   Rob Herring   pxe: parse initrd...
1197
1198
  			if (!label->initrd)
  				err = parse_sliteral(c, &label->initrd);
06283a640   Jason Hobbs   Add pxe command
1199
  			break;
a655938a9   Chander Kashyap   PXE: FDT: Add sup...
1200
1201
1202
1203
  		case T_FDT:
  			if (!label->fdt)
  				err = parse_sliteral(c, &label->fdt);
  			break;
c61d94d86   Stephen Warren   pxe: implement fd...
1204
1205
1206
1207
  		case T_FDTDIR:
  			if (!label->fdtdir)
  				err = parse_sliteral(c, &label->fdtdir);
  			break;
06283a640   Jason Hobbs   Add pxe command
1208
  		case T_LOCALBOOT:
500f304b6   Rob Herring   pxe: fix handling...
1209
1210
  			label->localboot = 1;
  			err = parse_integer(c, &label->localboot_val);
06283a640   Jason Hobbs   Add pxe command
1211
  			break;
98f646764   Rob Herring   pxe: add ipappend...
1212
1213
1214
  		case T_IPAPPEND:
  			err = parse_integer(c, &label->ipappend);
  			break;
06283a640   Jason Hobbs   Add pxe command
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
  		case T_EOL:
  			break;
  
  		default:
  			/*
  			 * put the token back! we don't want it - it's the end
  			 * of a label and whatever token this is, it's
  			 * something for the menu level context to handle.
  			 */
  			*c = s;
  			return 1;
  		}
  
  		if (err < 0)
  			return err;
  	}
  }
  
  /*
   * This 16 comes from the limit pxelinux imposes on nested includes.
   *
   * There is no reason at all we couldn't do more, but some limit helps prevent
   * infinite (until crash occurs) recursion if a file tries to include itself.
   */
  #define MAX_NEST_LEVEL 16
  
  /*
   * Entry point for parsing a menu file. nest_level indicates how many times
   * we've nested in includes.  It will be 1 for the top level menu file.
   *
   * Returns 1 on success, < 0 on error.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
1247
  static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
06283a640   Jason Hobbs   Add pxe command
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  {
  	struct token t;
  	char *s, *b, *label_name;
  	int err;
  
  	b = p;
  
  	if (nest_level > MAX_NEST_LEVEL) {
  		printf("Maximum nesting (%d) exceeded
  ", MAX_NEST_LEVEL);
  		return -EMLINK;
  	}
  
  	while (1) {
  		s = p;
  
  		get_token(&p, &t, L_KEYWORD);
  
  		err = 0;
  		switch (t.type) {
  		case T_MENU:
e82eeb570   Rob Herring   pxe: always displ...
1269
  			cfg->prompt = 1;
0e3f3f8a3   Steven Falco   Prevent null poin...
1270
  			err = parse_menu(cmdtp, &p, cfg, b, nest_level);
06283a640   Jason Hobbs   Add pxe command
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
  			break;
  
  		case T_TIMEOUT:
  			err = parse_integer(&p, &cfg->timeout);
  			break;
  
  		case T_LABEL:
  			err = parse_label(&p, cfg);
  			break;
  
  		case T_DEFAULT:
8577fec97   Rob Herring   pxe: add support ...
1282
  		case T_ONTIMEOUT:
06283a640   Jason Hobbs   Add pxe command
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
  			err = parse_sliteral(&p, &label_name);
  
  			if (label_name) {
  				if (cfg->default_label)
  					free(cfg->default_label);
  
  				cfg->default_label = label_name;
  			}
  
  			break;
1e0852269   Rob Herring   pxe: support incl...
1293
  		case T_INCLUDE:
0e3f3f8a3   Steven Falco   Prevent null poin...
1294
  			err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1e0852269   Rob Herring   pxe: support incl...
1295
1296
  							nest_level + 1);
  			break;
06283a640   Jason Hobbs   Add pxe command
1297
  		case T_PROMPT:
e82eeb570   Rob Herring   pxe: always displ...
1298
  			eol_or_eof(&p);
06283a640   Jason Hobbs   Add pxe command
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
  			break;
  
  		case T_EOL:
  			break;
  
  		case T_EOF:
  			return 1;
  
  		default:
  			printf("Ignoring unknown command: %.*s
  ",
  							(int)(p - s), s);
  			eol_or_eof(&p);
  		}
  
  		if (err < 0)
  			return err;
  	}
  }
  
  /*
   * Free the memory used by a pxe_menu and its labels.
   */
  static void destroy_pxe_menu(struct pxe_menu *cfg)
  {
  	struct list_head *pos, *n;
  	struct pxe_label *label;
  
  	if (cfg->title)
  		free(cfg->title);
  
  	if (cfg->default_label)
  		free(cfg->default_label);
  
  	list_for_each_safe(pos, n, &cfg->labels) {
  		label = list_entry(pos, struct pxe_label, list);
  
  		label_destroy(label);
  	}
  
  	free(cfg);
  }
  
  /*
   * Entry point for parsing a pxe file. This is only used for the top level
   * file.
   *
   * Returns NULL if there is an error, otherwise, returns a pointer to a
   * pxe_menu struct populated with the results of parsing the pxe file (and any
   * files it includes). The resulting pxe_menu struct can be free()'d by using
   * the destroy_pxe_menu() function.
   */
0e3f3f8a3   Steven Falco   Prevent null poin...
1351
  static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
06283a640   Jason Hobbs   Add pxe command
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
  {
  	struct pxe_menu *cfg;
  
  	cfg = malloc(sizeof(struct pxe_menu));
  
  	if (!cfg)
  		return NULL;
  
  	memset(cfg, 0, sizeof(struct pxe_menu));
  
  	INIT_LIST_HEAD(&cfg->labels);
0e3f3f8a3   Steven Falco   Prevent null poin...
1363
  	if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
06283a640   Jason Hobbs   Add pxe command
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
  		destroy_pxe_menu(cfg);
  		return NULL;
  	}
  
  	return cfg;
  }
  
  /*
   * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
   * menu code.
   */
  static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
  {
  	struct pxe_label *label;
  	struct list_head *pos;
  	struct menu *m;
  	int err;
32d2ffe73   Rob Herring   pxe: simplify men...
1381
1382
  	int i = 1;
  	char *default_num = NULL;
06283a640   Jason Hobbs   Add pxe command
1383
1384
1385
1386
  
  	/*
  	 * Create a menu and add items for all the labels.
  	 */
fc9d64ffc   Pali Rohár   menu: Add support...
1387
1388
  	m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
  			NULL, NULL);
06283a640   Jason Hobbs   Add pxe command
1389
1390
1391
1392
1393
1394
  
  	if (!m)
  		return NULL;
  
  	list_for_each(pos, &cfg->labels) {
  		label = list_entry(pos, struct pxe_label, list);
32d2ffe73   Rob Herring   pxe: simplify men...
1395
1396
  		sprintf(label->num, "%d", i++);
  		if (menu_item_add(m, label->num, label) != 1) {
06283a640   Jason Hobbs   Add pxe command
1397
1398
1399
  			menu_destroy(m);
  			return NULL;
  		}
32d2ffe73   Rob Herring   pxe: simplify men...
1400
  		if (cfg->default_label &&
8577fec97   Rob Herring   pxe: add support ...
1401
  		    (strcmp(label->name, cfg->default_label) == 0))
32d2ffe73   Rob Herring   pxe: simplify men...
1402
  			default_num = label->num;
06283a640   Jason Hobbs   Add pxe command
1403
1404
1405
1406
1407
1408
  	}
  
  	/*
  	 * After we've created items for each label in the menu, set the
  	 * menu's default label if one was specified.
  	 */
32d2ffe73   Rob Herring   pxe: simplify men...
1409
1410
  	if (default_num) {
  		err = menu_default_set(m, default_num);
06283a640   Jason Hobbs   Add pxe command
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
  		if (err != 1) {
  			if (err != -ENOENT) {
  				menu_destroy(m);
  				return NULL;
  			}
  
  			printf("Missing default: %s
  ", cfg->default_label);
  		}
  	}
  
  	return m;
  }
  
  /*
   * Try to boot any labels we have yet to attempt to boot.
   */
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
1428
  static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
06283a640   Jason Hobbs   Add pxe command
1429
1430
1431
1432
1433
1434
1435
1436
  {
  	struct list_head *pos;
  	struct pxe_label *label;
  
  	list_for_each(pos, &cfg->labels) {
  		label = list_entry(pos, struct pxe_label, list);
  
  		if (!label->attempted)
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
1437
  			label_boot(cmdtp, label);
06283a640   Jason Hobbs   Add pxe command
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
  	}
  }
  
  /*
   * Boot the system as prescribed by a pxe_menu.
   *
   * Use the menu system to either get the user's choice or the default, based
   * on config or user input.  If there is no default or user's choice,
   * attempted to boot labels in the order they were given in pxe files.
   * If the default or user's choice fails to boot, attempt to boot other
   * labels in the order they were given in pxe files.
   *
   * If this function returns, there weren't any labels that successfully
   * booted, or the user interrupted the menu selection via ctrl+c.
   */
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
1453
  static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
06283a640   Jason Hobbs   Add pxe command
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
  {
  	void *choice;
  	struct menu *m;
  	int err;
  
  	m = pxe_menu_to_menu(cfg);
  	if (!m)
  		return;
  
  	err = menu_get_choice(m, &choice);
  
  	menu_destroy(m);
6f40f2749   Jason Hobbs   pxe: make the fir...
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
  	/*
  	 * err == 1 means we got a choice back from menu_get_choice.
  	 *
  	 * err == -ENOENT if the menu was setup to select the default but no
  	 * default was set. in that case, we should continue trying to boot
  	 * labels that haven't been attempted yet.
  	 *
  	 * otherwise, the user interrupted or there was some other error and
  	 * we give up.
  	 */
06283a640   Jason Hobbs   Add pxe command
1476

500f304b6   Rob Herring   pxe: fix handling...
1477
  	if (err == 1) {
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
1478
  		err = label_boot(cmdtp, choice);
500f304b6   Rob Herring   pxe: fix handling...
1479
1480
1481
  		if (!err)
  			return;
  	} else if (err != -ENOENT) {
6f40f2749   Jason Hobbs   pxe: make the fir...
1482
  		return;
500f304b6   Rob Herring   pxe: fix handling...
1483
  	}
06283a640   Jason Hobbs   Add pxe command
1484

d7884e047   Tom Rini   cmd_pxe.c: Pass a...
1485
  	boot_unattempted_labels(cmdtp, cfg);
06283a640   Jason Hobbs   Add pxe command
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
  }
  
  /*
   * Boots a system using a pxe file
   *
   * Returns 0 on success, 1 on error.
   */
  static int
  do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	unsigned long pxefile_addr_r;
  	struct pxe_menu *cfg;
  	char *pxefile_addr_str;
669df7e42   Rob Herring   pxe: add support ...
1499
  	do_getfile = do_get_tftp;
06283a640   Jason Hobbs   Add pxe command
1500
1501
1502
1503
1504
1505
1506
1507
  	if (argc == 1) {
  		pxefile_addr_str = from_env("pxefile_addr_r");
  		if (!pxefile_addr_str)
  			return 1;
  
  	} else if (argc == 2) {
  		pxefile_addr_str = argv[1];
  	} else {
4c12eeb8b   Simon Glass   Convert cmd_usage...
1508
  		return CMD_RET_USAGE;
06283a640   Jason Hobbs   Add pxe command
1509
1510
1511
1512
1513
1514
1515
  	}
  
  	if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  		printf("Invalid pxefile address: %s
  ", pxefile_addr_str);
  		return 1;
  	}
0e3f3f8a3   Steven Falco   Prevent null poin...
1516
  	cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
06283a640   Jason Hobbs   Add pxe command
1517
1518
1519
1520
1521
1522
  
  	if (cfg == NULL) {
  		printf("Error parsing config file
  ");
  		return 1;
  	}
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
1523
  	handle_pxe_menu(cmdtp, cfg);
06283a640   Jason Hobbs   Add pxe command
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
  
  	destroy_pxe_menu(cfg);
  
  	return 0;
  }
  
  static cmd_tbl_t cmd_pxe_sub[] = {
  	U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
  	U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
  };
  
  int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	cmd_tbl_t *cp;
  
  	if (argc < 2)
4c12eeb8b   Simon Glass   Convert cmd_usage...
1540
  		return CMD_RET_USAGE;
06283a640   Jason Hobbs   Add pxe command
1541

e5a9a4076   Rob Herring   pxe: fix handling...
1542
  	is_pxe = true;
06283a640   Jason Hobbs   Add pxe command
1543
1544
1545
1546
1547
1548
1549
1550
  	/* drop initial "pxe" arg */
  	argc--;
  	argv++;
  
  	cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
  
  	if (cp)
  		return cp->cmd(cmdtp, flag, argc, argv);
4c12eeb8b   Simon Glass   Convert cmd_usage...
1551
  	return CMD_RET_USAGE;
06283a640   Jason Hobbs   Add pxe command
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
  }
  
  U_BOOT_CMD(
  	pxe, 3, 1, do_pxe,
  	"commands to get and boot from pxe files",
  	"get - try to retrieve a pxe file using tftp
  pxe "
  	"boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r
  "
  );
669df7e42   Rob Herring   pxe: add support ...
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
  
  /*
   * Boots a system using a local disk syslinux/extlinux file
   *
   * Returns 0 on success, 1 on error.
   */
  int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	unsigned long pxefile_addr_r;
  	struct pxe_menu *cfg;
  	char *pxefile_addr_str;
  	char *filename;
  	int prompt = 0;
e5a9a4076   Rob Herring   pxe: fix handling...
1575
  	is_pxe = false;
669df7e42   Rob Herring   pxe: add support ...
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
  	if (strstr(argv[1], "-p")) {
  		prompt = 1;
  		argc--;
  		argv++;
  	}
  
  	if (argc < 4)
  		return cmd_usage(cmdtp);
  
  	if (argc < 5) {
  		pxefile_addr_str = from_env("pxefile_addr_r");
  		if (!pxefile_addr_str)
  			return 1;
  	} else {
  		pxefile_addr_str = argv[4];
  	}
  
  	if (argc < 6)
  		filename = getenv("bootfile");
  	else {
  		filename = argv[5];
  		setenv("bootfile", filename);
  	}
  
  	if (strstr(argv[3], "ext2"))
  		do_getfile = do_get_ext2;
  	else if (strstr(argv[3], "fat"))
  		do_getfile = do_get_fat;
6d1a3e5fa   Dennis Gilmore   cmd_pxe.c add any...
1604
1605
  	else if (strstr(argv[3], "any"))
  		do_getfile = do_get_any;
669df7e42   Rob Herring   pxe: add support ...
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
  	else {
  		printf("Invalid filesystem: %s
  ", argv[3]);
  		return 1;
  	}
  	fs_argv[1] = argv[1];
  	fs_argv[2] = argv[2];
  
  	if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  		printf("Invalid pxefile address: %s
  ", pxefile_addr_str);
  		return 1;
  	}
0e3f3f8a3   Steven Falco   Prevent null poin...
1619
  	if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
669df7e42   Rob Herring   pxe: add support ...
1620
1621
1622
1623
  		printf("Error reading config file
  ");
  		return 1;
  	}
0e3f3f8a3   Steven Falco   Prevent null poin...
1624
  	cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
669df7e42   Rob Herring   pxe: add support ...
1625
1626
1627
1628
1629
1630
1631
1632
1633
  
  	if (cfg == NULL) {
  		printf("Error parsing config file
  ");
  		return 1;
  	}
  
  	if (prompt)
  		cfg->prompt = 1;
d7884e047   Tom Rini   cmd_pxe.c: Pass a...
1634
  	handle_pxe_menu(cmdtp, cfg);
669df7e42   Rob Herring   pxe: add support ...
1635
1636
1637
1638
1639
1640
1641
1642
1643
  
  	destroy_pxe_menu(cfg);
  
  	return 0;
  }
  
  U_BOOT_CMD(
  	sysboot, 7, 1, do_sysboot,
  	"command to get and boot from syslinux files",
6d1a3e5fa   Dennis Gilmore   cmd_pxe.c add any...
1644
1645
1646
1647
1648
  	"[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]
  "
  	"    - load and parse syslinux menu file 'filename' from ext2, fat
  "
  	"      or any filesystem on 'dev' on 'interface' to address 'addr'"
669df7e42   Rob Herring   pxe: add support ...
1649
  );