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