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