]>
Commit | Line | Data |
---|---|---|
06283a64 JH |
1 | /* |
2 | * Copyright 2010-2011 Calxeda, Inc. | |
1fb7d0e6 | 3 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
06283a64 | 4 | * |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
06283a64 | 6 | */ |
1a459660 | 7 | |
06283a64 JH |
8 | #include <common.h> |
9 | #include <command.h> | |
10 | #include <malloc.h> | |
11 | #include <linux/string.h> | |
12 | #include <linux/ctype.h> | |
13 | #include <errno.h> | |
14 | #include <linux/list.h> | |
6d1a3e5f | 15 | #include <fs.h> |
06283a64 JH |
16 | |
17 | #include "menu.h" | |
18 | ||
19 | #define MAX_TFTP_PATH_LEN 127 | |
20 | ||
39f98553 | 21 | const char *pxe_default_paths[] = { |
58d9ff93 | 22 | #ifdef CONFIG_SYS_SOC |
39f98553 | 23 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC, |
58d9ff93 | 24 | #endif |
39f98553 RH |
25 | "default-" CONFIG_SYS_ARCH, |
26 | "default", | |
27 | NULL | |
28 | }; | |
29 | ||
e5a9a407 RH |
30 | static bool is_pxe; |
31 | ||
06283a64 JH |
32 | /* |
33 | * Like getenv, but prints an error if envvar isn't defined in the | |
34 | * environment. It always returns what getenv does, so it can be used in | |
35 | * place of getenv without changing error handling otherwise. | |
36 | */ | |
23b7194e | 37 | static char *from_env(const char *envvar) |
06283a64 JH |
38 | { |
39 | char *ret; | |
40 | ||
41 | ret = getenv(envvar); | |
42 | ||
43 | if (!ret) | |
44 | printf("missing environment variable: %s\n", envvar); | |
45 | ||
46 | return ret; | |
47 | } | |
48 | ||
b81fdb04 | 49 | #ifdef CONFIG_CMD_NET |
06283a64 JH |
50 | /* |
51 | * Convert an ethaddr from the environment to the format used by pxelinux | |
52 | * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to | |
53 | * the beginning of the ethernet address to indicate a hardware type of | |
54 | * Ethernet. Also converts uppercase hex characters into lowercase, to match | |
55 | * pxelinux's behavior. | |
56 | * | |
57 | * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the | |
58 | * environment, or some other value < 0 on error. | |
59 | */ | |
60 | static int format_mac_pxe(char *outbuf, size_t outbuf_len) | |
61 | { | |
ef034c9d | 62 | uchar ethaddr[6]; |
06283a64 | 63 | |
ef034c9d | 64 | if (outbuf_len < 21) { |
5cea95cb | 65 | printf("outbuf is too small (%zd < 21)\n", outbuf_len); |
06283a64 JH |
66 | |
67 | return -EINVAL; | |
68 | } | |
69 | ||
ef034c9d RH |
70 | if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(), |
71 | ethaddr)) | |
72 | return -ENOENT; | |
06283a64 | 73 | |
ef034c9d RH |
74 | sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x", |
75 | ethaddr[0], ethaddr[1], ethaddr[2], | |
76 | ethaddr[3], ethaddr[4], ethaddr[5]); | |
06283a64 JH |
77 | |
78 | return 1; | |
79 | } | |
b81fdb04 | 80 | #endif |
06283a64 JH |
81 | |
82 | /* | |
83 | * Returns the directory the file specified in the bootfile env variable is | |
84 | * in. If bootfile isn't defined in the environment, return NULL, which should | |
85 | * be interpreted as "don't prepend anything to paths". | |
86 | */ | |
90ba7d7c RH |
87 | static int get_bootfile_path(const char *file_path, char *bootfile_path, |
88 | size_t bootfile_path_size) | |
06283a64 JH |
89 | { |
90 | char *bootfile, *last_slash; | |
90ba7d7c RH |
91 | size_t path_len = 0; |
92 | ||
e5a9a407 RH |
93 | /* Only syslinux allows absolute paths */ |
94 | if (file_path[0] == '/' && !is_pxe) | |
90ba7d7c | 95 | goto ret; |
06283a64 JH |
96 | |
97 | bootfile = from_env("bootfile"); | |
98 | ||
90ba7d7c RH |
99 | if (!bootfile) |
100 | goto ret; | |
06283a64 JH |
101 | |
102 | last_slash = strrchr(bootfile, '/'); | |
103 | ||
90ba7d7c RH |
104 | if (last_slash == NULL) |
105 | goto ret; | |
06283a64 JH |
106 | |
107 | path_len = (last_slash - bootfile) + 1; | |
108 | ||
109 | if (bootfile_path_size < path_len) { | |
5cea95cb | 110 | printf("bootfile_path too small. (%zd < %zd)\n", |
06283a64 JH |
111 | bootfile_path_size, path_len); |
112 | ||
113 | return -1; | |
114 | } | |
115 | ||
116 | strncpy(bootfile_path, bootfile, path_len); | |
117 | ||
90ba7d7c | 118 | ret: |
06283a64 JH |
119 | bootfile_path[path_len] = '\0'; |
120 | ||
121 | return 1; | |
122 | } | |
123 | ||
0e3f3f8a | 124 | static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr); |
669df7e4 | 125 | |
b81fdb04 | 126 | #ifdef CONFIG_CMD_NET |
0e3f3f8a | 127 | static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
669df7e4 RH |
128 | { |
129 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; | |
130 | ||
131 | tftp_argv[1] = file_addr; | |
23b7194e | 132 | tftp_argv[2] = (void *)file_path; |
669df7e4 | 133 | |
0e3f3f8a | 134 | if (do_tftpb(cmdtp, 0, 3, tftp_argv)) |
669df7e4 RH |
135 | return -ENOENT; |
136 | ||
137 | return 1; | |
138 | } | |
b81fdb04 | 139 | #endif |
669df7e4 RH |
140 | |
141 | static char *fs_argv[5]; | |
142 | ||
0e3f3f8a | 143 | static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
669df7e4 RH |
144 | { |
145 | #ifdef CONFIG_CMD_EXT2 | |
146 | fs_argv[0] = "ext2load"; | |
147 | fs_argv[3] = file_addr; | |
23b7194e | 148 | fs_argv[4] = (void *)file_path; |
669df7e4 | 149 | |
0e3f3f8a | 150 | if (!do_ext2load(cmdtp, 0, 5, fs_argv)) |
669df7e4 RH |
151 | return 1; |
152 | #endif | |
153 | return -ENOENT; | |
154 | } | |
155 | ||
0e3f3f8a | 156 | static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
669df7e4 RH |
157 | { |
158 | #ifdef CONFIG_CMD_FAT | |
159 | fs_argv[0] = "fatload"; | |
160 | fs_argv[3] = file_addr; | |
23b7194e | 161 | fs_argv[4] = (void *)file_path; |
669df7e4 | 162 | |
0e3f3f8a | 163 | if (!do_fat_fsload(cmdtp, 0, 5, fs_argv)) |
669df7e4 RH |
164 | return 1; |
165 | #endif | |
166 | return -ENOENT; | |
167 | } | |
168 | ||
6d1a3e5f DG |
169 | static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
170 | { | |
171 | #ifdef CONFIG_CMD_FS_GENERIC | |
172 | fs_argv[0] = "load"; | |
173 | fs_argv[3] = file_addr; | |
174 | fs_argv[4] = (void *)file_path; | |
175 | ||
176 | if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY)) | |
177 | return 1; | |
178 | #endif | |
179 | return -ENOENT; | |
180 | } | |
181 | ||
06283a64 JH |
182 | /* |
183 | * As in pxelinux, paths to files referenced from files we retrieve are | |
184 | * relative to the location of bootfile. get_relfile takes such a path and | |
185 | * joins it with the bootfile path to get the full path to the target file. If | |
186 | * the bootfile path is NULL, we use file_path as is. | |
187 | * | |
188 | * Returns 1 for success, or < 0 on error. | |
189 | */ | |
0e3f3f8a | 190 | static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr) |
06283a64 JH |
191 | { |
192 | size_t path_len; | |
193 | char relfile[MAX_TFTP_PATH_LEN+1]; | |
194 | char addr_buf[10]; | |
06283a64 JH |
195 | int err; |
196 | ||
90ba7d7c | 197 | err = get_bootfile_path(file_path, relfile, sizeof(relfile)); |
06283a64 JH |
198 | |
199 | if (err < 0) | |
200 | return err; | |
201 | ||
202 | path_len = strlen(file_path); | |
203 | path_len += strlen(relfile); | |
204 | ||
205 | if (path_len > MAX_TFTP_PATH_LEN) { | |
206 | printf("Base path too long (%s%s)\n", | |
207 | relfile, | |
208 | file_path); | |
209 | ||
210 | return -ENAMETOOLONG; | |
211 | } | |
212 | ||
213 | strcat(relfile, file_path); | |
214 | ||
215 | printf("Retrieving file: %s\n", relfile); | |
216 | ||
217 | sprintf(addr_buf, "%p", file_addr); | |
218 | ||
0e3f3f8a | 219 | return do_getfile(cmdtp, relfile, addr_buf); |
06283a64 JH |
220 | } |
221 | ||
222 | /* | |
223 | * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If | |
224 | * 'bootfile' was specified in the environment, the path to bootfile will be | |
225 | * prepended to 'file_path' and the resulting path will be used. | |
226 | * | |
227 | * Returns 1 on success, or < 0 for error. | |
228 | */ | |
0e3f3f8a | 229 | static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr) |
06283a64 JH |
230 | { |
231 | unsigned long config_file_size; | |
232 | char *tftp_filesize; | |
233 | int err; | |
234 | ||
0e3f3f8a | 235 | err = get_relfile(cmdtp, file_path, file_addr); |
06283a64 JH |
236 | |
237 | if (err < 0) | |
238 | return err; | |
239 | ||
240 | /* | |
241 | * the file comes without a NUL byte at the end, so find out its size | |
242 | * and add the NUL byte. | |
243 | */ | |
244 | tftp_filesize = from_env("filesize"); | |
245 | ||
246 | if (!tftp_filesize) | |
247 | return -ENOENT; | |
248 | ||
249 | if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0) | |
250 | return -EINVAL; | |
251 | ||
252 | *(char *)(file_addr + config_file_size) = '\0'; | |
253 | ||
254 | return 1; | |
255 | } | |
256 | ||
b81fdb04 SW |
257 | #ifdef CONFIG_CMD_NET |
258 | ||
06283a64 JH |
259 | #define PXELINUX_DIR "pxelinux.cfg/" |
260 | ||
261 | /* | |
262 | * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file | |
263 | * to do the hard work, the location of the 'pxelinux.cfg' folder is generated | |
264 | * from the bootfile path, as described above. | |
265 | * | |
266 | * Returns 1 on success or < 0 on error. | |
267 | */ | |
0e3f3f8a | 268 | static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r) |
06283a64 JH |
269 | { |
270 | size_t base_len = strlen(PXELINUX_DIR); | |
271 | char path[MAX_TFTP_PATH_LEN+1]; | |
272 | ||
273 | if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) { | |
274 | printf("path (%s%s) too long, skipping\n", | |
275 | PXELINUX_DIR, file); | |
276 | return -ENAMETOOLONG; | |
277 | } | |
278 | ||
279 | sprintf(path, PXELINUX_DIR "%s", file); | |
280 | ||
0e3f3f8a | 281 | return get_pxe_file(cmdtp, path, pxefile_addr_r); |
06283a64 JH |
282 | } |
283 | ||
284 | /* | |
285 | * Looks for a pxe file with a name based on the pxeuuid environment variable. | |
286 | * | |
287 | * Returns 1 on success or < 0 on error. | |
288 | */ | |
0e3f3f8a | 289 | static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r) |
06283a64 JH |
290 | { |
291 | char *uuid_str; | |
292 | ||
293 | uuid_str = from_env("pxeuuid"); | |
294 | ||
295 | if (!uuid_str) | |
296 | return -ENOENT; | |
297 | ||
0e3f3f8a | 298 | return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r); |
06283a64 JH |
299 | } |
300 | ||
301 | /* | |
302 | * Looks for a pxe file with a name based on the 'ethaddr' environment | |
303 | * variable. | |
304 | * | |
305 | * Returns 1 on success or < 0 on error. | |
306 | */ | |
0e3f3f8a | 307 | static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r) |
06283a64 JH |
308 | { |
309 | char mac_str[21]; | |
310 | int err; | |
311 | ||
312 | err = format_mac_pxe(mac_str, sizeof(mac_str)); | |
313 | ||
314 | if (err < 0) | |
315 | return err; | |
316 | ||
0e3f3f8a | 317 | return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r); |
06283a64 JH |
318 | } |
319 | ||
320 | /* | |
321 | * Looks for pxe files with names based on our IP address. See pxelinux | |
322 | * documentation for details on what these file names look like. We match | |
323 | * that exactly. | |
324 | * | |
325 | * Returns 1 on success or < 0 on error. | |
326 | */ | |
0e3f3f8a | 327 | static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r) |
06283a64 JH |
328 | { |
329 | char ip_addr[9]; | |
330 | int mask_pos, err; | |
331 | ||
332 | sprintf(ip_addr, "%08X", ntohl(NetOurIP)); | |
333 | ||
334 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { | |
0e3f3f8a | 335 | err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r); |
06283a64 JH |
336 | |
337 | if (err > 0) | |
338 | return err; | |
339 | ||
340 | ip_addr[mask_pos] = '\0'; | |
341 | } | |
342 | ||
343 | return -ENOENT; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Entry point for the 'pxe get' command. | |
348 | * This Follows pxelinux's rules to download a config file from a tftp server. | |
349 | * The file is stored at the location given by the pxefile_addr_r environment | |
350 | * variable, which must be set. | |
351 | * | |
352 | * UUID comes from pxeuuid env variable, if defined | |
353 | * MAC addr comes from ethaddr env variable, if defined | |
354 | * IP | |
355 | * | |
356 | * see http://syslinux.zytor.com/wiki/index.php/PXELINUX | |
357 | * | |
358 | * Returns 0 on success or 1 on error. | |
359 | */ | |
360 | static int | |
361 | do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
362 | { | |
363 | char *pxefile_addr_str; | |
834c9384 | 364 | unsigned long pxefile_addr_r; |
39f98553 | 365 | int err, i = 0; |
06283a64 | 366 | |
669df7e4 RH |
367 | do_getfile = do_get_tftp; |
368 | ||
06283a64 | 369 | if (argc != 1) |
4c12eeb8 | 370 | return CMD_RET_USAGE; |
06283a64 | 371 | |
06283a64 JH |
372 | pxefile_addr_str = from_env("pxefile_addr_r"); |
373 | ||
374 | if (!pxefile_addr_str) | |
375 | return 1; | |
376 | ||
377 | err = strict_strtoul(pxefile_addr_str, 16, | |
378 | (unsigned long *)&pxefile_addr_r); | |
379 | if (err < 0) | |
380 | return 1; | |
381 | ||
382 | /* | |
383 | * Keep trying paths until we successfully get a file we're looking | |
384 | * for. | |
385 | */ | |
0e3f3f8a SF |
386 | if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 || |
387 | pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 || | |
388 | pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) { | |
06283a64 JH |
389 | printf("Config file found\n"); |
390 | ||
391 | return 0; | |
392 | } | |
393 | ||
39f98553 | 394 | while (pxe_default_paths[i]) { |
0e3f3f8a | 395 | if (get_pxelinux_path(cmdtp, pxe_default_paths[i], |
39f98553 RH |
396 | (void *)pxefile_addr_r) > 0) { |
397 | printf("Config file found\n"); | |
398 | return 0; | |
399 | } | |
400 | i++; | |
401 | } | |
402 | ||
06283a64 JH |
403 | printf("Config file not found\n"); |
404 | ||
405 | return 1; | |
406 | } | |
b81fdb04 | 407 | #endif |
06283a64 JH |
408 | |
409 | /* | |
410 | * Wrapper to make it easier to store the file at file_path in the location | |
411 | * specified by envaddr_name. file_path will be joined to the bootfile path, | |
412 | * if any is specified. | |
413 | * | |
414 | * Returns 1 on success or < 0 on error. | |
415 | */ | |
0e3f3f8a | 416 | static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name) |
06283a64 | 417 | { |
834c9384 | 418 | unsigned long file_addr; |
06283a64 JH |
419 | char *envaddr; |
420 | ||
421 | envaddr = from_env(envaddr_name); | |
422 | ||
423 | if (!envaddr) | |
424 | return -ENOENT; | |
425 | ||
834c9384 | 426 | if (strict_strtoul(envaddr, 16, &file_addr) < 0) |
06283a64 JH |
427 | return -EINVAL; |
428 | ||
0e3f3f8a | 429 | return get_relfile(cmdtp, file_path, (void *)file_addr); |
06283a64 JH |
430 | } |
431 | ||
432 | /* | |
433 | * A note on the pxe file parser. | |
434 | * | |
435 | * We're parsing files that use syslinux grammar, which has a few quirks. | |
436 | * String literals must be recognized based on context - there is no | |
437 | * quoting or escaping support. There's also nothing to explicitly indicate | |
438 | * when a label section completes. We deal with that by ending a label | |
439 | * section whenever we see a line that doesn't include. | |
440 | * | |
441 | * As with the syslinux family, this same file format could be reused in the | |
442 | * future for non pxe purposes. The only action it takes during parsing that | |
443 | * would throw this off is handling of include files. It assumes we're using | |
444 | * pxe, and does a tftp download of a file listed as an include file in the | |
445 | * middle of the parsing operation. That could be handled by refactoring it to | |
446 | * take a 'include file getter' function. | |
447 | */ | |
448 | ||
449 | /* | |
450 | * Describes a single label given in a pxe file. | |
451 | * | |
452 | * Create these with the 'label_create' function given below. | |
453 | * | |
454 | * name - the name of the menu as given on the 'menu label' line. | |
455 | * kernel - the path to the kernel file to use for this label. | |
456 | * append - kernel command line to use when booting this label | |
457 | * initrd - path to the initrd to use for this label. | |
458 | * attempted - 0 if we haven't tried to boot this label, 1 if we have. | |
459 | * localboot - 1 if this label specified 'localboot', 0 otherwise. | |
460 | * list - lets these form a list, which a pxe_menu struct will hold. | |
461 | */ | |
462 | struct pxe_label { | |
32d2ffe7 | 463 | char num[4]; |
06283a64 | 464 | char *name; |
7815c4e8 | 465 | char *menu; |
06283a64 JH |
466 | char *kernel; |
467 | char *append; | |
468 | char *initrd; | |
a655938a | 469 | char *fdt; |
c61d94d8 | 470 | char *fdtdir; |
98f64676 | 471 | int ipappend; |
06283a64 JH |
472 | int attempted; |
473 | int localboot; | |
500f304b | 474 | int localboot_val; |
06283a64 JH |
475 | struct list_head list; |
476 | }; | |
477 | ||
478 | /* | |
479 | * Describes a pxe menu as given via pxe files. | |
480 | * | |
481 | * title - the name of the menu as given by a 'menu title' line. | |
482 | * default_label - the name of the default label, if any. | |
483 | * timeout - time in tenths of a second to wait for a user key-press before | |
484 | * booting the default label. | |
485 | * prompt - if 0, don't prompt for a choice unless the timeout period is | |
486 | * interrupted. If 1, always prompt for a choice regardless of | |
487 | * timeout. | |
488 | * labels - a list of labels defined for the menu. | |
489 | */ | |
490 | struct pxe_menu { | |
491 | char *title; | |
492 | char *default_label; | |
493 | int timeout; | |
494 | int prompt; | |
495 | struct list_head labels; | |
496 | }; | |
497 | ||
498 | /* | |
499 | * Allocates memory for and initializes a pxe_label. This uses malloc, so the | |
500 | * result must be free()'d to reclaim the memory. | |
501 | * | |
502 | * Returns NULL if malloc fails. | |
503 | */ | |
504 | static struct pxe_label *label_create(void) | |
505 | { | |
506 | struct pxe_label *label; | |
507 | ||
508 | label = malloc(sizeof(struct pxe_label)); | |
509 | ||
510 | if (!label) | |
511 | return NULL; | |
512 | ||
513 | memset(label, 0, sizeof(struct pxe_label)); | |
514 | ||
515 | return label; | |
516 | } | |
517 | ||
518 | /* | |
519 | * Free the memory used by a pxe_label, including that used by its name, | |
520 | * kernel, append and initrd members, if they're non NULL. | |
521 | * | |
522 | * So - be sure to only use dynamically allocated memory for the members of | |
523 | * the pxe_label struct, unless you want to clean it up first. These are | |
524 | * currently only created by the pxe file parsing code. | |
525 | */ | |
526 | static void label_destroy(struct pxe_label *label) | |
527 | { | |
528 | if (label->name) | |
529 | free(label->name); | |
530 | ||
531 | if (label->kernel) | |
532 | free(label->kernel); | |
533 | ||
534 | if (label->append) | |
535 | free(label->append); | |
536 | ||
537 | if (label->initrd) | |
538 | free(label->initrd); | |
539 | ||
a655938a CK |
540 | if (label->fdt) |
541 | free(label->fdt); | |
542 | ||
c61d94d8 SW |
543 | if (label->fdtdir) |
544 | free(label->fdtdir); | |
545 | ||
06283a64 JH |
546 | free(label); |
547 | } | |
548 | ||
549 | /* | |
550 | * Print a label and its string members if they're defined. | |
551 | * | |
552 | * This is passed as a callback to the menu code for displaying each | |
553 | * menu entry. | |
554 | */ | |
555 | static void label_print(void *data) | |
556 | { | |
557 | struct pxe_label *label = data; | |
32d2ffe7 | 558 | const char *c = label->menu ? label->menu : label->name; |
06283a64 | 559 | |
32d2ffe7 | 560 | printf("%s:\t%s\n", label->num, c); |
06283a64 JH |
561 | } |
562 | ||
563 | /* | |
564 | * Boot a label that specified 'localboot'. This requires that the 'localcmd' | |
565 | * environment variable is defined. Its contents will be executed as U-boot | |
566 | * command. If the label specified an 'append' line, its contents will be | |
567 | * used to overwrite the contents of the 'bootargs' environment variable prior | |
568 | * to running 'localcmd'. | |
569 | * | |
570 | * Returns 1 on success or < 0 on error. | |
571 | */ | |
572 | static int label_localboot(struct pxe_label *label) | |
573 | { | |
d51004a8 | 574 | char *localcmd; |
06283a64 JH |
575 | |
576 | localcmd = from_env("localcmd"); | |
577 | ||
578 | if (!localcmd) | |
579 | return -ENOENT; | |
580 | ||
06283a64 JH |
581 | if (label->append) |
582 | setenv("bootargs", label->append); | |
583 | ||
d51004a8 | 584 | debug("running: %s\n", localcmd); |
06283a64 | 585 | |
d51004a8 | 586 | return run_command_list(localcmd, strlen(localcmd), 0); |
06283a64 JH |
587 | } |
588 | ||
589 | /* | |
590 | * Boot according to the contents of a pxe_label. | |
591 | * | |
592 | * If we can't boot for any reason, we return. A successful boot never | |
593 | * returns. | |
594 | * | |
595 | * The kernel will be stored in the location given by the 'kernel_addr_r' | |
596 | * environment variable. | |
597 | * | |
598 | * If the label specifies an initrd file, it will be stored in the location | |
599 | * given by the 'ramdisk_addr_r' environment variable. | |
600 | * | |
601 | * If the label specifies an 'append' line, its contents will overwrite that | |
602 | * of the 'bootargs' environment variable. | |
603 | */ | |
d7884e04 | 604 | static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label) |
06283a64 JH |
605 | { |
606 | char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL }; | |
e6b6ccf2 | 607 | char initrd_str[22]; |
98f64676 RH |
608 | char mac_str[29] = ""; |
609 | char ip_str[68] = ""; | |
610 | char *bootargs; | |
06283a64 | 611 | int bootm_argc = 3; |
98f64676 | 612 | int len = 0; |
1fb7d0e6 BW |
613 | ulong kernel_addr; |
614 | void *buf; | |
06283a64 JH |
615 | |
616 | label_print(label); | |
617 | ||
618 | label->attempted = 1; | |
619 | ||
620 | if (label->localboot) { | |
500f304b RH |
621 | if (label->localboot_val >= 0) |
622 | label_localboot(label); | |
623 | return 0; | |
06283a64 JH |
624 | } |
625 | ||
626 | if (label->kernel == NULL) { | |
627 | printf("No kernel given, skipping %s\n", | |
628 | label->name); | |
500f304b | 629 | return 1; |
06283a64 JH |
630 | } |
631 | ||
632 | if (label->initrd) { | |
0e3f3f8a | 633 | if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) { |
06283a64 JH |
634 | printf("Skipping %s for failure retrieving initrd\n", |
635 | label->name); | |
500f304b | 636 | return 1; |
06283a64 JH |
637 | } |
638 | ||
e6b6ccf2 RH |
639 | bootm_argv[2] = initrd_str; |
640 | strcpy(bootm_argv[2], getenv("ramdisk_addr_r")); | |
641 | strcat(bootm_argv[2], ":"); | |
642 | strcat(bootm_argv[2], getenv("filesize")); | |
06283a64 JH |
643 | } else { |
644 | bootm_argv[2] = "-"; | |
645 | } | |
646 | ||
0e3f3f8a | 647 | if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) { |
06283a64 JH |
648 | printf("Skipping %s for failure retrieving kernel\n", |
649 | label->name); | |
500f304b | 650 | return 1; |
06283a64 JH |
651 | } |
652 | ||
98f64676 RH |
653 | if (label->ipappend & 0x1) { |
654 | sprintf(ip_str, " ip=%s:%s:%s:%s", | |
655 | getenv("ipaddr"), getenv("serverip"), | |
656 | getenv("gatewayip"), getenv("netmask")); | |
657 | len += strlen(ip_str); | |
658 | } | |
659 | ||
b81fdb04 | 660 | #ifdef CONFIG_CMD_NET |
98f64676 RH |
661 | if (label->ipappend & 0x2) { |
662 | int err; | |
663 | strcpy(mac_str, " BOOTIF="); | |
664 | err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8); | |
665 | if (err < 0) | |
666 | mac_str[0] = '\0'; | |
667 | len += strlen(mac_str); | |
668 | } | |
b81fdb04 | 669 | #endif |
98f64676 RH |
670 | |
671 | if (label->append) | |
672 | len += strlen(label->append); | |
673 | ||
674 | if (len) { | |
675 | bootargs = malloc(len + 1); | |
676 | if (!bootargs) | |
677 | return 1; | |
678 | bootargs[0] = '\0'; | |
679 | if (label->append) | |
680 | strcpy(bootargs, label->append); | |
681 | strcat(bootargs, ip_str); | |
682 | strcat(bootargs, mac_str); | |
683 | ||
684 | setenv("bootargs", bootargs); | |
685 | printf("append: %s\n", bootargs); | |
686 | ||
687 | free(bootargs); | |
32d2ffe7 | 688 | } |
06283a64 JH |
689 | |
690 | bootm_argv[1] = getenv("kernel_addr_r"); | |
691 | ||
692 | /* | |
a655938a CK |
693 | * fdt usage is optional: |
694 | * It handles the following scenarios. All scenarios are exclusive | |
695 | * | |
696 | * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in | |
697 | * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm, | |
698 | * and adjust argc appropriately. | |
699 | * | |
700 | * Scenario 2: If there is an fdt_addr specified, pass it along to | |
701 | * bootm, and adjust argc appropriately. | |
702 | * | |
703 | * Scenario 3: fdt blob is not available. | |
06283a64 | 704 | */ |
a655938a CK |
705 | bootm_argv[3] = getenv("fdt_addr_r"); |
706 | ||
707 | /* if fdt label is defined then get fdt from server */ | |
c61d94d8 SW |
708 | if (bootm_argv[3]) { |
709 | char *fdtfile = NULL; | |
710 | char *fdtfilefree = NULL; | |
711 | ||
712 | if (label->fdt) { | |
713 | fdtfile = label->fdt; | |
714 | } else if (label->fdtdir) { | |
e22361af SW |
715 | char *f1, *f2, *f3, *f4, *slash; |
716 | ||
717 | f1 = getenv("fdtfile"); | |
718 | if (f1) { | |
719 | f2 = ""; | |
720 | f3 = ""; | |
721 | f4 = ""; | |
722 | } else { | |
723 | /* | |
724 | * For complex cases where this code doesn't | |
725 | * generate the correct filename, the board | |
726 | * code should set $fdtfile during early boot, | |
727 | * or the boot scripts should set $fdtfile | |
728 | * before invoking "pxe" or "sysboot". | |
729 | */ | |
730 | f1 = getenv("soc"); | |
731 | f2 = "-"; | |
732 | f3 = getenv("board"); | |
733 | f4 = ".dtb"; | |
c61d94d8 | 734 | } |
e22361af SW |
735 | |
736 | len = strlen(label->fdtdir); | |
737 | if (!len) | |
738 | slash = "./"; | |
739 | else if (label->fdtdir[len - 1] != '/') | |
740 | slash = "/"; | |
741 | else | |
742 | slash = ""; | |
743 | ||
744 | len = strlen(label->fdtdir) + strlen(slash) + | |
745 | strlen(f1) + strlen(f2) + strlen(f3) + | |
746 | strlen(f4) + 1; | |
747 | fdtfilefree = malloc(len); | |
748 | if (!fdtfilefree) { | |
749 | printf("malloc fail (FDT filename)\n"); | |
750 | return 1; | |
751 | } | |
752 | ||
753 | snprintf(fdtfilefree, len, "%s%s%s%s%s%s", | |
754 | label->fdtdir, slash, f1, f2, f3, f4); | |
755 | fdtfile = fdtfilefree; | |
a655938a | 756 | } |
c61d94d8 SW |
757 | |
758 | if (fdtfile) { | |
759 | int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r"); | |
760 | free(fdtfilefree); | |
761 | if (err < 0) { | |
762 | printf("Skipping %s for failure retrieving fdt\n", | |
763 | label->name); | |
764 | return 1; | |
765 | } | |
766 | } else { | |
767 | bootm_argv[3] = NULL; | |
768 | } | |
769 | } | |
770 | ||
771 | if (!bootm_argv[3]) | |
a655938a | 772 | bootm_argv[3] = getenv("fdt_addr"); |
06283a64 JH |
773 | |
774 | if (bootm_argv[3]) | |
775 | bootm_argc = 4; | |
776 | ||
1fb7d0e6 BW |
777 | kernel_addr = genimg_get_kernel_addr(bootm_argv[1]); |
778 | buf = map_sysmem(kernel_addr, 0); | |
779 | /* Try bootm for legacy and FIT format image */ | |
780 | if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID) | |
781 | do_bootm(cmdtp, 0, bootm_argc, bootm_argv); | |
e6b6ccf2 | 782 | #ifdef CONFIG_CMD_BOOTZ |
1fb7d0e6 BW |
783 | /* Try booting a zImage */ |
784 | else | |
785 | do_bootz(cmdtp, 0, bootm_argc, bootm_argv); | |
e6b6ccf2 | 786 | #endif |
500f304b | 787 | return 1; |
06283a64 JH |
788 | } |
789 | ||
790 | /* | |
791 | * Tokens for the pxe file parser. | |
792 | */ | |
793 | enum token_type { | |
794 | T_EOL, | |
795 | T_STRING, | |
796 | T_EOF, | |
797 | T_MENU, | |
798 | T_TITLE, | |
799 | T_TIMEOUT, | |
800 | T_LABEL, | |
801 | T_KERNEL, | |
beb9f6c6 | 802 | T_LINUX, |
06283a64 JH |
803 | T_APPEND, |
804 | T_INITRD, | |
805 | T_LOCALBOOT, | |
806 | T_DEFAULT, | |
807 | T_PROMPT, | |
808 | T_INCLUDE, | |
a655938a | 809 | T_FDT, |
c61d94d8 | 810 | T_FDTDIR, |
8577fec9 | 811 | T_ONTIMEOUT, |
98f64676 | 812 | T_IPAPPEND, |
06283a64 JH |
813 | T_INVALID |
814 | }; | |
815 | ||
816 | /* | |
817 | * A token - given by a value and a type. | |
818 | */ | |
819 | struct token { | |
820 | char *val; | |
821 | enum token_type type; | |
822 | }; | |
823 | ||
824 | /* | |
825 | * Keywords recognized. | |
826 | */ | |
827 | static const struct token keywords[] = { | |
828 | {"menu", T_MENU}, | |
829 | {"title", T_TITLE}, | |
830 | {"timeout", T_TIMEOUT}, | |
831 | {"default", T_DEFAULT}, | |
832 | {"prompt", T_PROMPT}, | |
833 | {"label", T_LABEL}, | |
834 | {"kernel", T_KERNEL}, | |
beb9f6c6 | 835 | {"linux", T_LINUX}, |
06283a64 JH |
836 | {"localboot", T_LOCALBOOT}, |
837 | {"append", T_APPEND}, | |
838 | {"initrd", T_INITRD}, | |
839 | {"include", T_INCLUDE}, | |
f43c401b | 840 | {"devicetree", T_FDT}, |
a655938a | 841 | {"fdt", T_FDT}, |
c61d94d8 SW |
842 | {"devicetreedir", T_FDTDIR}, |
843 | {"fdtdir", T_FDTDIR}, | |
8577fec9 | 844 | {"ontimeout", T_ONTIMEOUT,}, |
98f64676 | 845 | {"ipappend", T_IPAPPEND,}, |
06283a64 JH |
846 | {NULL, T_INVALID} |
847 | }; | |
848 | ||
849 | /* | |
850 | * Since pxe(linux) files don't have a token to identify the start of a | |
851 | * literal, we have to keep track of when we're in a state where a literal is | |
852 | * expected vs when we're in a state a keyword is expected. | |
853 | */ | |
854 | enum lex_state { | |
855 | L_NORMAL = 0, | |
856 | L_KEYWORD, | |
857 | L_SLITERAL | |
858 | }; | |
859 | ||
860 | /* | |
861 | * get_string retrieves a string from *p and stores it as a token in | |
862 | * *t. | |
863 | * | |
864 | * get_string used for scanning both string literals and keywords. | |
865 | * | |
866 | * Characters from *p are copied into t-val until a character equal to | |
867 | * delim is found, or a NUL byte is reached. If delim has the special value of | |
868 | * ' ', any whitespace character will be used as a delimiter. | |
869 | * | |
870 | * If lower is unequal to 0, uppercase characters will be converted to | |
871 | * lowercase in the result. This is useful to make keywords case | |
872 | * insensitive. | |
873 | * | |
874 | * The location of *p is updated to point to the first character after the end | |
875 | * of the token - the ending delimiter. | |
876 | * | |
877 | * On success, the new value of t->val is returned. Memory for t->val is | |
878 | * allocated using malloc and must be free()'d to reclaim it. If insufficient | |
879 | * memory is available, NULL is returned. | |
880 | */ | |
881 | static char *get_string(char **p, struct token *t, char delim, int lower) | |
882 | { | |
883 | char *b, *e; | |
884 | size_t len, i; | |
885 | ||
886 | /* | |
887 | * b and e both start at the beginning of the input stream. | |
888 | * | |
889 | * e is incremented until we find the ending delimiter, or a NUL byte | |
890 | * is reached. Then, we take e - b to find the length of the token. | |
891 | */ | |
892 | b = e = *p; | |
893 | ||
894 | while (*e) { | |
895 | if ((delim == ' ' && isspace(*e)) || delim == *e) | |
896 | break; | |
897 | e++; | |
898 | } | |
899 | ||
900 | len = e - b; | |
901 | ||
902 | /* | |
903 | * Allocate memory to hold the string, and copy it in, converting | |
904 | * characters to lowercase if lower is != 0. | |
905 | */ | |
906 | t->val = malloc(len + 1); | |
907 | if (!t->val) | |
908 | return NULL; | |
909 | ||
910 | for (i = 0; i < len; i++, b++) { | |
911 | if (lower) | |
912 | t->val[i] = tolower(*b); | |
913 | else | |
914 | t->val[i] = *b; | |
915 | } | |
916 | ||
917 | t->val[len] = '\0'; | |
918 | ||
919 | /* | |
920 | * Update *p so the caller knows where to continue scanning. | |
921 | */ | |
922 | *p = e; | |
923 | ||
924 | t->type = T_STRING; | |
925 | ||
926 | return t->val; | |
927 | } | |
928 | ||
929 | /* | |
930 | * Populate a keyword token with a type and value. | |
931 | */ | |
932 | static void get_keyword(struct token *t) | |
933 | { | |
934 | int i; | |
935 | ||
936 | for (i = 0; keywords[i].val; i++) { | |
937 | if (!strcmp(t->val, keywords[i].val)) { | |
938 | t->type = keywords[i].type; | |
939 | break; | |
940 | } | |
941 | } | |
942 | } | |
943 | ||
944 | /* | |
945 | * Get the next token. We have to keep track of which state we're in to know | |
946 | * if we're looking to get a string literal or a keyword. | |
947 | * | |
948 | * *p is updated to point at the first character after the current token. | |
949 | */ | |
950 | static void get_token(char **p, struct token *t, enum lex_state state) | |
951 | { | |
952 | char *c = *p; | |
953 | ||
954 | t->type = T_INVALID; | |
955 | ||
956 | /* eat non EOL whitespace */ | |
957 | while (isblank(*c)) | |
958 | c++; | |
959 | ||
960 | /* | |
961 | * eat comments. note that string literals can't begin with #, but | |
962 | * can contain a # after their first character. | |
963 | */ | |
964 | if (*c == '#') { | |
965 | while (*c && *c != '\n') | |
966 | c++; | |
967 | } | |
968 | ||
969 | if (*c == '\n') { | |
970 | t->type = T_EOL; | |
971 | c++; | |
972 | } else if (*c == '\0') { | |
973 | t->type = T_EOF; | |
974 | c++; | |
975 | } else if (state == L_SLITERAL) { | |
976 | get_string(&c, t, '\n', 0); | |
977 | } else if (state == L_KEYWORD) { | |
978 | /* | |
979 | * when we expect a keyword, we first get the next string | |
980 | * token delimited by whitespace, and then check if it | |
981 | * matches a keyword in our keyword list. if it does, it's | |
982 | * converted to a keyword token of the appropriate type, and | |
983 | * if not, it remains a string token. | |
984 | */ | |
985 | get_string(&c, t, ' ', 1); | |
986 | get_keyword(t); | |
987 | } | |
988 | ||
989 | *p = c; | |
990 | } | |
991 | ||
992 | /* | |
993 | * Increment *c until we get to the end of the current line, or EOF. | |
994 | */ | |
995 | static void eol_or_eof(char **c) | |
996 | { | |
997 | while (**c && **c != '\n') | |
998 | (*c)++; | |
999 | } | |
1000 | ||
1001 | /* | |
1002 | * All of these parse_* functions share some common behavior. | |
1003 | * | |
1004 | * They finish with *c pointing after the token they parse, and return 1 on | |
1005 | * success, or < 0 on error. | |
1006 | */ | |
1007 | ||
1008 | /* | |
1009 | * Parse a string literal and store a pointer it at *dst. String literals | |
1010 | * terminate at the end of the line. | |
1011 | */ | |
1012 | static int parse_sliteral(char **c, char **dst) | |
1013 | { | |
1014 | struct token t; | |
1015 | char *s = *c; | |
1016 | ||
1017 | get_token(c, &t, L_SLITERAL); | |
1018 | ||
1019 | if (t.type != T_STRING) { | |
1020 | printf("Expected string literal: %.*s\n", (int)(*c - s), s); | |
1021 | return -EINVAL; | |
1022 | } | |
1023 | ||
1024 | *dst = t.val; | |
1025 | ||
1026 | return 1; | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * Parse a base 10 (unsigned) integer and store it at *dst. | |
1031 | */ | |
1032 | static int parse_integer(char **c, int *dst) | |
1033 | { | |
1034 | struct token t; | |
1035 | char *s = *c; | |
06283a64 JH |
1036 | |
1037 | get_token(c, &t, L_SLITERAL); | |
1038 | ||
1039 | if (t.type != T_STRING) { | |
1040 | printf("Expected string: %.*s\n", (int)(*c - s), s); | |
1041 | return -EINVAL; | |
1042 | } | |
1043 | ||
500f304b | 1044 | *dst = simple_strtol(t.val, NULL, 10); |
06283a64 JH |
1045 | |
1046 | free(t.val); | |
1047 | ||
1048 | return 1; | |
1049 | } | |
1050 | ||
0e3f3f8a | 1051 | static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level); |
06283a64 JH |
1052 | |
1053 | /* | |
1054 | * Parse an include statement, and retrieve and parse the file it mentions. | |
1055 | * | |
1056 | * base should point to a location where it's safe to store the file, and | |
1057 | * nest_level should indicate how many nested includes have occurred. For this | |
1058 | * include, nest_level has already been incremented and doesn't need to be | |
1059 | * incremented here. | |
1060 | */ | |
0e3f3f8a | 1061 | static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base, |
06283a64 JH |
1062 | struct pxe_menu *cfg, int nest_level) |
1063 | { | |
1064 | char *include_path; | |
1065 | char *s = *c; | |
1066 | int err; | |
1067 | ||
1068 | err = parse_sliteral(c, &include_path); | |
1069 | ||
1070 | if (err < 0) { | |
1071 | printf("Expected include path: %.*s\n", | |
1072 | (int)(*c - s), s); | |
1073 | return err; | |
1074 | } | |
1075 | ||
0e3f3f8a | 1076 | err = get_pxe_file(cmdtp, include_path, base); |
06283a64 JH |
1077 | |
1078 | if (err < 0) { | |
1079 | printf("Couldn't retrieve %s\n", include_path); | |
1080 | return err; | |
1081 | } | |
1082 | ||
0e3f3f8a | 1083 | return parse_pxefile_top(cmdtp, base, cfg, nest_level); |
06283a64 JH |
1084 | } |
1085 | ||
1086 | /* | |
1087 | * Parse lines that begin with 'menu'. | |
1088 | * | |
1089 | * b and nest are provided to handle the 'menu include' case. | |
1090 | * | |
1091 | * b should be the address where the file currently being parsed is stored. | |
1092 | * | |
1093 | * nest_level should be 1 when parsing the top level pxe file, 2 when parsing | |
1094 | * a file it includes, 3 when parsing a file included by that file, and so on. | |
1095 | */ | |
0e3f3f8a | 1096 | static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level) |
06283a64 JH |
1097 | { |
1098 | struct token t; | |
1099 | char *s = *c; | |
43d4a5e6 | 1100 | int err = 0; |
06283a64 JH |
1101 | |
1102 | get_token(c, &t, L_KEYWORD); | |
1103 | ||
1104 | switch (t.type) { | |
1105 | case T_TITLE: | |
1106 | err = parse_sliteral(c, &cfg->title); | |
1107 | ||
1108 | break; | |
1109 | ||
1110 | case T_INCLUDE: | |
0e3f3f8a | 1111 | err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg, |
06283a64 JH |
1112 | nest_level + 1); |
1113 | break; | |
1114 | ||
1115 | default: | |
1116 | printf("Ignoring malformed menu command: %.*s\n", | |
1117 | (int)(*c - s), s); | |
1118 | } | |
1119 | ||
1120 | if (err < 0) | |
1121 | return err; | |
1122 | ||
1123 | eol_or_eof(c); | |
1124 | ||
1125 | return 1; | |
1126 | } | |
1127 | ||
1128 | /* | |
1129 | * Handles parsing a 'menu line' when we're parsing a label. | |
1130 | */ | |
1131 | static int parse_label_menu(char **c, struct pxe_menu *cfg, | |
1132 | struct pxe_label *label) | |
1133 | { | |
1134 | struct token t; | |
1135 | char *s; | |
1136 | ||
1137 | s = *c; | |
1138 | ||
1139 | get_token(c, &t, L_KEYWORD); | |
1140 | ||
1141 | switch (t.type) { | |
1142 | case T_DEFAULT: | |
8577fec9 RH |
1143 | if (!cfg->default_label) |
1144 | cfg->default_label = strdup(label->name); | |
06283a64 JH |
1145 | |
1146 | if (!cfg->default_label) | |
1147 | return -ENOMEM; | |
1148 | ||
7815c4e8 RH |
1149 | break; |
1150 | case T_LABEL: | |
1151 | parse_sliteral(c, &label->menu); | |
06283a64 JH |
1152 | break; |
1153 | default: | |
1154 | printf("Ignoring malformed menu command: %.*s\n", | |
1155 | (int)(*c - s), s); | |
1156 | } | |
1157 | ||
1158 | eol_or_eof(c); | |
1159 | ||
1160 | return 0; | |
1161 | } | |
1162 | ||
1163 | /* | |
1164 | * Parses a label and adds it to the list of labels for a menu. | |
1165 | * | |
1166 | * A label ends when we either get to the end of a file, or | |
1167 | * get some input we otherwise don't have a handler defined | |
1168 | * for. | |
1169 | * | |
1170 | */ | |
1171 | static int parse_label(char **c, struct pxe_menu *cfg) | |
1172 | { | |
1173 | struct token t; | |
34bd23e4 | 1174 | int len; |
06283a64 JH |
1175 | char *s = *c; |
1176 | struct pxe_label *label; | |
1177 | int err; | |
1178 | ||
1179 | label = label_create(); | |
1180 | if (!label) | |
1181 | return -ENOMEM; | |
1182 | ||
1183 | err = parse_sliteral(c, &label->name); | |
1184 | if (err < 0) { | |
1185 | printf("Expected label name: %.*s\n", (int)(*c - s), s); | |
1186 | label_destroy(label); | |
1187 | return -EINVAL; | |
1188 | } | |
1189 | ||
1190 | list_add_tail(&label->list, &cfg->labels); | |
1191 | ||
1192 | while (1) { | |
1193 | s = *c; | |
1194 | get_token(c, &t, L_KEYWORD); | |
1195 | ||
1196 | err = 0; | |
1197 | switch (t.type) { | |
1198 | case T_MENU: | |
1199 | err = parse_label_menu(c, cfg, label); | |
1200 | break; | |
1201 | ||
1202 | case T_KERNEL: | |
beb9f6c6 | 1203 | case T_LINUX: |
06283a64 JH |
1204 | err = parse_sliteral(c, &label->kernel); |
1205 | break; | |
1206 | ||
1207 | case T_APPEND: | |
1208 | err = parse_sliteral(c, &label->append); | |
34bd23e4 RH |
1209 | if (label->initrd) |
1210 | break; | |
1211 | s = strstr(label->append, "initrd="); | |
1212 | if (!s) | |
1213 | break; | |
1214 | s += 7; | |
1215 | len = (int)(strchr(s, ' ') - s); | |
1216 | label->initrd = malloc(len + 1); | |
1217 | strncpy(label->initrd, s, len); | |
1218 | label->initrd[len] = '\0'; | |
1219 | ||
06283a64 JH |
1220 | break; |
1221 | ||
1222 | case T_INITRD: | |
34bd23e4 RH |
1223 | if (!label->initrd) |
1224 | err = parse_sliteral(c, &label->initrd); | |
06283a64 JH |
1225 | break; |
1226 | ||
a655938a CK |
1227 | case T_FDT: |
1228 | if (!label->fdt) | |
1229 | err = parse_sliteral(c, &label->fdt); | |
1230 | break; | |
1231 | ||
c61d94d8 SW |
1232 | case T_FDTDIR: |
1233 | if (!label->fdtdir) | |
1234 | err = parse_sliteral(c, &label->fdtdir); | |
1235 | break; | |
1236 | ||
06283a64 | 1237 | case T_LOCALBOOT: |
500f304b RH |
1238 | label->localboot = 1; |
1239 | err = parse_integer(c, &label->localboot_val); | |
06283a64 JH |
1240 | break; |
1241 | ||
98f64676 RH |
1242 | case T_IPAPPEND: |
1243 | err = parse_integer(c, &label->ipappend); | |
1244 | break; | |
1245 | ||
06283a64 JH |
1246 | case T_EOL: |
1247 | break; | |
1248 | ||
1249 | default: | |
1250 | /* | |
1251 | * put the token back! we don't want it - it's the end | |
1252 | * of a label and whatever token this is, it's | |
1253 | * something for the menu level context to handle. | |
1254 | */ | |
1255 | *c = s; | |
1256 | return 1; | |
1257 | } | |
1258 | ||
1259 | if (err < 0) | |
1260 | return err; | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | /* | |
1265 | * This 16 comes from the limit pxelinux imposes on nested includes. | |
1266 | * | |
1267 | * There is no reason at all we couldn't do more, but some limit helps prevent | |
1268 | * infinite (until crash occurs) recursion if a file tries to include itself. | |
1269 | */ | |
1270 | #define MAX_NEST_LEVEL 16 | |
1271 | ||
1272 | /* | |
1273 | * Entry point for parsing a menu file. nest_level indicates how many times | |
1274 | * we've nested in includes. It will be 1 for the top level menu file. | |
1275 | * | |
1276 | * Returns 1 on success, < 0 on error. | |
1277 | */ | |
0e3f3f8a | 1278 | static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level) |
06283a64 JH |
1279 | { |
1280 | struct token t; | |
1281 | char *s, *b, *label_name; | |
1282 | int err; | |
1283 | ||
1284 | b = p; | |
1285 | ||
1286 | if (nest_level > MAX_NEST_LEVEL) { | |
1287 | printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL); | |
1288 | return -EMLINK; | |
1289 | } | |
1290 | ||
1291 | while (1) { | |
1292 | s = p; | |
1293 | ||
1294 | get_token(&p, &t, L_KEYWORD); | |
1295 | ||
1296 | err = 0; | |
1297 | switch (t.type) { | |
1298 | case T_MENU: | |
e82eeb57 | 1299 | cfg->prompt = 1; |
0e3f3f8a | 1300 | err = parse_menu(cmdtp, &p, cfg, b, nest_level); |
06283a64 JH |
1301 | break; |
1302 | ||
1303 | case T_TIMEOUT: | |
1304 | err = parse_integer(&p, &cfg->timeout); | |
1305 | break; | |
1306 | ||
1307 | case T_LABEL: | |
1308 | err = parse_label(&p, cfg); | |
1309 | break; | |
1310 | ||
1311 | case T_DEFAULT: | |
8577fec9 | 1312 | case T_ONTIMEOUT: |
06283a64 JH |
1313 | err = parse_sliteral(&p, &label_name); |
1314 | ||
1315 | if (label_name) { | |
1316 | if (cfg->default_label) | |
1317 | free(cfg->default_label); | |
1318 | ||
1319 | cfg->default_label = label_name; | |
1320 | } | |
1321 | ||
1322 | break; | |
1323 | ||
1e085226 | 1324 | case T_INCLUDE: |
0e3f3f8a | 1325 | err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg, |
1e085226 RH |
1326 | nest_level + 1); |
1327 | break; | |
1328 | ||
06283a64 | 1329 | case T_PROMPT: |
e82eeb57 | 1330 | eol_or_eof(&p); |
06283a64 JH |
1331 | break; |
1332 | ||
1333 | case T_EOL: | |
1334 | break; | |
1335 | ||
1336 | case T_EOF: | |
1337 | return 1; | |
1338 | ||
1339 | default: | |
1340 | printf("Ignoring unknown command: %.*s\n", | |
1341 | (int)(p - s), s); | |
1342 | eol_or_eof(&p); | |
1343 | } | |
1344 | ||
1345 | if (err < 0) | |
1346 | return err; | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | /* | |
1351 | * Free the memory used by a pxe_menu and its labels. | |
1352 | */ | |
1353 | static void destroy_pxe_menu(struct pxe_menu *cfg) | |
1354 | { | |
1355 | struct list_head *pos, *n; | |
1356 | struct pxe_label *label; | |
1357 | ||
1358 | if (cfg->title) | |
1359 | free(cfg->title); | |
1360 | ||
1361 | if (cfg->default_label) | |
1362 | free(cfg->default_label); | |
1363 | ||
1364 | list_for_each_safe(pos, n, &cfg->labels) { | |
1365 | label = list_entry(pos, struct pxe_label, list); | |
1366 | ||
1367 | label_destroy(label); | |
1368 | } | |
1369 | ||
1370 | free(cfg); | |
1371 | } | |
1372 | ||
1373 | /* | |
1374 | * Entry point for parsing a pxe file. This is only used for the top level | |
1375 | * file. | |
1376 | * | |
1377 | * Returns NULL if there is an error, otherwise, returns a pointer to a | |
1378 | * pxe_menu struct populated with the results of parsing the pxe file (and any | |
1379 | * files it includes). The resulting pxe_menu struct can be free()'d by using | |
1380 | * the destroy_pxe_menu() function. | |
1381 | */ | |
0e3f3f8a | 1382 | static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg) |
06283a64 JH |
1383 | { |
1384 | struct pxe_menu *cfg; | |
1385 | ||
1386 | cfg = malloc(sizeof(struct pxe_menu)); | |
1387 | ||
1388 | if (!cfg) | |
1389 | return NULL; | |
1390 | ||
1391 | memset(cfg, 0, sizeof(struct pxe_menu)); | |
1392 | ||
1393 | INIT_LIST_HEAD(&cfg->labels); | |
1394 | ||
0e3f3f8a | 1395 | if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) { |
06283a64 JH |
1396 | destroy_pxe_menu(cfg); |
1397 | return NULL; | |
1398 | } | |
1399 | ||
1400 | return cfg; | |
1401 | } | |
1402 | ||
1403 | /* | |
1404 | * Converts a pxe_menu struct into a menu struct for use with U-boot's generic | |
1405 | * menu code. | |
1406 | */ | |
1407 | static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg) | |
1408 | { | |
1409 | struct pxe_label *label; | |
1410 | struct list_head *pos; | |
1411 | struct menu *m; | |
1412 | int err; | |
32d2ffe7 RH |
1413 | int i = 1; |
1414 | char *default_num = NULL; | |
06283a64 JH |
1415 | |
1416 | /* | |
1417 | * Create a menu and add items for all the labels. | |
1418 | */ | |
fc9d64ff PR |
1419 | m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print, |
1420 | NULL, NULL); | |
06283a64 JH |
1421 | |
1422 | if (!m) | |
1423 | return NULL; | |
1424 | ||
1425 | list_for_each(pos, &cfg->labels) { | |
1426 | label = list_entry(pos, struct pxe_label, list); | |
1427 | ||
32d2ffe7 RH |
1428 | sprintf(label->num, "%d", i++); |
1429 | if (menu_item_add(m, label->num, label) != 1) { | |
06283a64 JH |
1430 | menu_destroy(m); |
1431 | return NULL; | |
1432 | } | |
32d2ffe7 | 1433 | if (cfg->default_label && |
8577fec9 | 1434 | (strcmp(label->name, cfg->default_label) == 0)) |
32d2ffe7 RH |
1435 | default_num = label->num; |
1436 | ||
06283a64 JH |
1437 | } |
1438 | ||
1439 | /* | |
1440 | * After we've created items for each label in the menu, set the | |
1441 | * menu's default label if one was specified. | |
1442 | */ | |
32d2ffe7 RH |
1443 | if (default_num) { |
1444 | err = menu_default_set(m, default_num); | |
06283a64 JH |
1445 | if (err != 1) { |
1446 | if (err != -ENOENT) { | |
1447 | menu_destroy(m); | |
1448 | return NULL; | |
1449 | } | |
1450 | ||
1451 | printf("Missing default: %s\n", cfg->default_label); | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | return m; | |
1456 | } | |
1457 | ||
1458 | /* | |
1459 | * Try to boot any labels we have yet to attempt to boot. | |
1460 | */ | |
d7884e04 | 1461 | static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg) |
06283a64 JH |
1462 | { |
1463 | struct list_head *pos; | |
1464 | struct pxe_label *label; | |
1465 | ||
1466 | list_for_each(pos, &cfg->labels) { | |
1467 | label = list_entry(pos, struct pxe_label, list); | |
1468 | ||
1469 | if (!label->attempted) | |
d7884e04 | 1470 | label_boot(cmdtp, label); |
06283a64 JH |
1471 | } |
1472 | } | |
1473 | ||
1474 | /* | |
1475 | * Boot the system as prescribed by a pxe_menu. | |
1476 | * | |
1477 | * Use the menu system to either get the user's choice or the default, based | |
1478 | * on config or user input. If there is no default or user's choice, | |
1479 | * attempted to boot labels in the order they were given in pxe files. | |
1480 | * If the default or user's choice fails to boot, attempt to boot other | |
1481 | * labels in the order they were given in pxe files. | |
1482 | * | |
1483 | * If this function returns, there weren't any labels that successfully | |
1484 | * booted, or the user interrupted the menu selection via ctrl+c. | |
1485 | */ | |
d7884e04 | 1486 | static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg) |
06283a64 JH |
1487 | { |
1488 | void *choice; | |
1489 | struct menu *m; | |
1490 | int err; | |
1491 | ||
1492 | m = pxe_menu_to_menu(cfg); | |
1493 | if (!m) | |
1494 | return; | |
1495 | ||
1496 | err = menu_get_choice(m, &choice); | |
1497 | ||
1498 | menu_destroy(m); | |
1499 | ||
6f40f274 JH |
1500 | /* |
1501 | * err == 1 means we got a choice back from menu_get_choice. | |
1502 | * | |
1503 | * err == -ENOENT if the menu was setup to select the default but no | |
1504 | * default was set. in that case, we should continue trying to boot | |
1505 | * labels that haven't been attempted yet. | |
1506 | * | |
1507 | * otherwise, the user interrupted or there was some other error and | |
1508 | * we give up. | |
1509 | */ | |
06283a64 | 1510 | |
500f304b | 1511 | if (err == 1) { |
d7884e04 | 1512 | err = label_boot(cmdtp, choice); |
500f304b RH |
1513 | if (!err) |
1514 | return; | |
1515 | } else if (err != -ENOENT) { | |
6f40f274 | 1516 | return; |
500f304b | 1517 | } |
06283a64 | 1518 | |
d7884e04 | 1519 | boot_unattempted_labels(cmdtp, cfg); |
06283a64 JH |
1520 | } |
1521 | ||
b81fdb04 | 1522 | #ifdef CONFIG_CMD_NET |
06283a64 JH |
1523 | /* |
1524 | * Boots a system using a pxe file | |
1525 | * | |
1526 | * Returns 0 on success, 1 on error. | |
1527 | */ | |
1528 | static int | |
1529 | do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
1530 | { | |
1531 | unsigned long pxefile_addr_r; | |
1532 | struct pxe_menu *cfg; | |
1533 | char *pxefile_addr_str; | |
1534 | ||
669df7e4 RH |
1535 | do_getfile = do_get_tftp; |
1536 | ||
06283a64 JH |
1537 | if (argc == 1) { |
1538 | pxefile_addr_str = from_env("pxefile_addr_r"); | |
1539 | if (!pxefile_addr_str) | |
1540 | return 1; | |
1541 | ||
1542 | } else if (argc == 2) { | |
1543 | pxefile_addr_str = argv[1]; | |
1544 | } else { | |
4c12eeb8 | 1545 | return CMD_RET_USAGE; |
06283a64 JH |
1546 | } |
1547 | ||
1548 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { | |
1549 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); | |
1550 | return 1; | |
1551 | } | |
1552 | ||
0e3f3f8a | 1553 | cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r)); |
06283a64 JH |
1554 | |
1555 | if (cfg == NULL) { | |
1556 | printf("Error parsing config file\n"); | |
1557 | return 1; | |
1558 | } | |
1559 | ||
d7884e04 | 1560 | handle_pxe_menu(cmdtp, cfg); |
06283a64 JH |
1561 | |
1562 | destroy_pxe_menu(cfg); | |
1563 | ||
ded2e20e SW |
1564 | copy_filename(BootFile, "", sizeof(BootFile)); |
1565 | ||
06283a64 JH |
1566 | return 0; |
1567 | } | |
1568 | ||
1569 | static cmd_tbl_t cmd_pxe_sub[] = { | |
1570 | U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""), | |
1571 | U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "") | |
1572 | }; | |
1573 | ||
0e350f81 | 1574 | static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
06283a64 JH |
1575 | { |
1576 | cmd_tbl_t *cp; | |
1577 | ||
1578 | if (argc < 2) | |
4c12eeb8 | 1579 | return CMD_RET_USAGE; |
06283a64 | 1580 | |
e5a9a407 RH |
1581 | is_pxe = true; |
1582 | ||
06283a64 JH |
1583 | /* drop initial "pxe" arg */ |
1584 | argc--; | |
1585 | argv++; | |
1586 | ||
1587 | cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); | |
1588 | ||
1589 | if (cp) | |
1590 | return cp->cmd(cmdtp, flag, argc, argv); | |
1591 | ||
4c12eeb8 | 1592 | return CMD_RET_USAGE; |
06283a64 JH |
1593 | } |
1594 | ||
1595 | U_BOOT_CMD( | |
1596 | pxe, 3, 1, do_pxe, | |
1597 | "commands to get and boot from pxe files", | |
1598 | "get - try to retrieve a pxe file using tftp\npxe " | |
1599 | "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n" | |
1600 | ); | |
b81fdb04 | 1601 | #endif |
669df7e4 RH |
1602 | |
1603 | /* | |
1604 | * Boots a system using a local disk syslinux/extlinux file | |
1605 | * | |
1606 | * Returns 0 on success, 1 on error. | |
1607 | */ | |
0e350f81 | 1608 | static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
669df7e4 RH |
1609 | { |
1610 | unsigned long pxefile_addr_r; | |
1611 | struct pxe_menu *cfg; | |
1612 | char *pxefile_addr_str; | |
1613 | char *filename; | |
1614 | int prompt = 0; | |
1615 | ||
e5a9a407 RH |
1616 | is_pxe = false; |
1617 | ||
669df7e4 RH |
1618 | if (strstr(argv[1], "-p")) { |
1619 | prompt = 1; | |
1620 | argc--; | |
1621 | argv++; | |
1622 | } | |
1623 | ||
1624 | if (argc < 4) | |
1625 | return cmd_usage(cmdtp); | |
1626 | ||
1627 | if (argc < 5) { | |
1628 | pxefile_addr_str = from_env("pxefile_addr_r"); | |
1629 | if (!pxefile_addr_str) | |
1630 | return 1; | |
1631 | } else { | |
1632 | pxefile_addr_str = argv[4]; | |
1633 | } | |
1634 | ||
1635 | if (argc < 6) | |
1636 | filename = getenv("bootfile"); | |
1637 | else { | |
1638 | filename = argv[5]; | |
1639 | setenv("bootfile", filename); | |
1640 | } | |
1641 | ||
1642 | if (strstr(argv[3], "ext2")) | |
1643 | do_getfile = do_get_ext2; | |
1644 | else if (strstr(argv[3], "fat")) | |
1645 | do_getfile = do_get_fat; | |
6d1a3e5f DG |
1646 | else if (strstr(argv[3], "any")) |
1647 | do_getfile = do_get_any; | |
669df7e4 RH |
1648 | else { |
1649 | printf("Invalid filesystem: %s\n", argv[3]); | |
1650 | return 1; | |
1651 | } | |
1652 | fs_argv[1] = argv[1]; | |
1653 | fs_argv[2] = argv[2]; | |
1654 | ||
1655 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { | |
1656 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); | |
1657 | return 1; | |
1658 | } | |
1659 | ||
0e3f3f8a | 1660 | if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) { |
669df7e4 RH |
1661 | printf("Error reading config file\n"); |
1662 | return 1; | |
1663 | } | |
1664 | ||
0e3f3f8a | 1665 | cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r)); |
669df7e4 RH |
1666 | |
1667 | if (cfg == NULL) { | |
1668 | printf("Error parsing config file\n"); | |
1669 | return 1; | |
1670 | } | |
1671 | ||
1672 | if (prompt) | |
1673 | cfg->prompt = 1; | |
1674 | ||
d7884e04 | 1675 | handle_pxe_menu(cmdtp, cfg); |
669df7e4 RH |
1676 | |
1677 | destroy_pxe_menu(cfg); | |
1678 | ||
1679 | return 0; | |
1680 | } | |
1681 | ||
1682 | U_BOOT_CMD( | |
1683 | sysboot, 7, 1, do_sysboot, | |
1684 | "command to get and boot from syslinux files", | |
6d1a3e5f DG |
1685 | "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n" |
1686 | " - load and parse syslinux menu file 'filename' from ext2, fat\n" | |
1687 | " or any filesystem on 'dev' on 'interface' to address 'addr'" | |
669df7e4 | 1688 | ); |