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