]>
Commit | Line | Data |
---|---|---|
2373cba3 PC |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Copyright 2010-2011 Calxeda, Inc. | |
4 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. | |
5 | */ | |
6 | ||
7 | #include <common.h> | |
09140113 | 8 | #include <command.h> |
2373cba3 | 9 | #include <env.h> |
8e8ccfe1 | 10 | #include <image.h> |
f7ae49fc | 11 | #include <log.h> |
2373cba3 PC |
12 | #include <malloc.h> |
13 | #include <mapmem.h> | |
14 | #include <lcd.h> | |
90526e9f | 15 | #include <net.h> |
69076dff NA |
16 | #include <fdt_support.h> |
17 | #include <linux/libfdt.h> | |
2373cba3 PC |
18 | #include <linux/string.h> |
19 | #include <linux/ctype.h> | |
20 | #include <errno.h> | |
21 | #include <linux/list.h> | |
22 | ||
23 | #include <splash.h> | |
24 | #include <asm/io.h> | |
25 | ||
26 | #include "menu.h" | |
27 | #include "cli.h" | |
28 | ||
29 | #include "pxe_utils.h" | |
30 | ||
2c4e067d | 31 | #define MAX_TFTP_PATH_LEN 512 |
2373cba3 PC |
32 | |
33 | bool is_pxe; | |
34 | ||
2373cba3 PC |
35 | int format_mac_pxe(char *outbuf, size_t outbuf_len) |
36 | { | |
37 | uchar ethaddr[6]; | |
38 | ||
39 | if (outbuf_len < 21) { | |
40 | printf("outbuf is too small (%zd < 21)\n", outbuf_len); | |
41 | ||
42 | return -EINVAL; | |
43 | } | |
44 | ||
45 | if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr)) | |
46 | return -ENOENT; | |
47 | ||
48 | sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x", | |
49 | ethaddr[0], ethaddr[1], ethaddr[2], | |
50 | ethaddr[3], ethaddr[4], ethaddr[5]); | |
51 | ||
52 | return 1; | |
53 | } | |
54 | ||
55 | /* | |
56 | * Returns the directory the file specified in the bootfile env variable is | |
57 | * in. If bootfile isn't defined in the environment, return NULL, which should | |
58 | * be interpreted as "don't prepend anything to paths". | |
59 | */ | |
60 | static int get_bootfile_path(const char *file_path, char *bootfile_path, | |
61 | size_t bootfile_path_size) | |
62 | { | |
63 | char *bootfile, *last_slash; | |
64 | size_t path_len = 0; | |
65 | ||
66 | /* Only syslinux allows absolute paths */ | |
67 | if (file_path[0] == '/' && !is_pxe) | |
68 | goto ret; | |
69 | ||
70 | bootfile = from_env("bootfile"); | |
71 | ||
72 | if (!bootfile) | |
73 | goto ret; | |
74 | ||
75 | last_slash = strrchr(bootfile, '/'); | |
76 | ||
8cb22a66 | 77 | if (!last_slash) |
2373cba3 PC |
78 | goto ret; |
79 | ||
80 | path_len = (last_slash - bootfile) + 1; | |
81 | ||
82 | if (bootfile_path_size < path_len) { | |
83 | printf("bootfile_path too small. (%zd < %zd)\n", | |
8cb22a66 | 84 | bootfile_path_size, path_len); |
2373cba3 PC |
85 | |
86 | return -1; | |
87 | } | |
88 | ||
89 | strncpy(bootfile_path, bootfile, path_len); | |
90 | ||
91 | ret: | |
92 | bootfile_path[path_len] = '\0'; | |
93 | ||
94 | return 1; | |
95 | } | |
96 | ||
09140113 SG |
97 | int (*do_getfile)(struct cmd_tbl *cmdtp, const char *file_path, |
98 | char *file_addr); | |
2373cba3 PC |
99 | |
100 | /* | |
101 | * As in pxelinux, paths to files referenced from files we retrieve are | |
102 | * relative to the location of bootfile. get_relfile takes such a path and | |
103 | * joins it with the bootfile path to get the full path to the target file. If | |
104 | * the bootfile path is NULL, we use file_path as is. | |
105 | * | |
106 | * Returns 1 for success, or < 0 on error. | |
107 | */ | |
fd3fa5c3 | 108 | static int get_relfile(struct pxe_context *ctx, const char *file_path, |
8cb22a66 | 109 | unsigned long file_addr) |
2373cba3 PC |
110 | { |
111 | size_t path_len; | |
8cb22a66 | 112 | char relfile[MAX_TFTP_PATH_LEN + 1]; |
2373cba3 PC |
113 | char addr_buf[18]; |
114 | int err; | |
115 | ||
116 | err = get_bootfile_path(file_path, relfile, sizeof(relfile)); | |
117 | ||
118 | if (err < 0) | |
119 | return err; | |
120 | ||
121 | path_len = strlen(file_path); | |
122 | path_len += strlen(relfile); | |
123 | ||
124 | if (path_len > MAX_TFTP_PATH_LEN) { | |
8cb22a66 | 125 | printf("Base path too long (%s%s)\n", relfile, file_path); |
2373cba3 PC |
126 | |
127 | return -ENAMETOOLONG; | |
128 | } | |
129 | ||
130 | strcat(relfile, file_path); | |
131 | ||
132 | printf("Retrieving file: %s\n", relfile); | |
133 | ||
134 | sprintf(addr_buf, "%lx", file_addr); | |
135 | ||
fd3fa5c3 | 136 | return do_getfile(ctx->cmdtp, relfile, addr_buf); |
2373cba3 PC |
137 | } |
138 | ||
fd3fa5c3 | 139 | int get_pxe_file(struct pxe_context *ctx, const char *file_path, |
8cb22a66 | 140 | unsigned long file_addr) |
2373cba3 PC |
141 | { |
142 | unsigned long config_file_size; | |
143 | char *tftp_filesize; | |
144 | int err; | |
145 | char *buf; | |
146 | ||
fd3fa5c3 | 147 | err = get_relfile(ctx, file_path, file_addr); |
2373cba3 PC |
148 | |
149 | if (err < 0) | |
150 | return err; | |
151 | ||
152 | /* | |
153 | * the file comes without a NUL byte at the end, so find out its size | |
154 | * and add the NUL byte. | |
155 | */ | |
156 | tftp_filesize = from_env("filesize"); | |
157 | ||
158 | if (!tftp_filesize) | |
159 | return -ENOENT; | |
160 | ||
161 | if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0) | |
162 | return -EINVAL; | |
163 | ||
164 | buf = map_sysmem(file_addr + config_file_size, 1); | |
165 | *buf = '\0'; | |
166 | unmap_sysmem(buf); | |
167 | ||
168 | return 1; | |
169 | } | |
170 | ||
171 | #define PXELINUX_DIR "pxelinux.cfg/" | |
172 | ||
fd3fa5c3 | 173 | int get_pxelinux_path(struct pxe_context *ctx, const char *file, |
8cb22a66 | 174 | unsigned long pxefile_addr_r) |
2373cba3 PC |
175 | { |
176 | size_t base_len = strlen(PXELINUX_DIR); | |
8cb22a66 | 177 | char path[MAX_TFTP_PATH_LEN + 1]; |
2373cba3 PC |
178 | |
179 | if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) { | |
180 | printf("path (%s%s) too long, skipping\n", | |
8cb22a66 | 181 | PXELINUX_DIR, file); |
2373cba3 PC |
182 | return -ENAMETOOLONG; |
183 | } | |
184 | ||
185 | sprintf(path, PXELINUX_DIR "%s", file); | |
186 | ||
fd3fa5c3 | 187 | return get_pxe_file(ctx, path, pxefile_addr_r); |
2373cba3 PC |
188 | } |
189 | ||
190 | /* | |
191 | * Wrapper to make it easier to store the file at file_path in the location | |
192 | * specified by envaddr_name. file_path will be joined to the bootfile path, | |
193 | * if any is specified. | |
194 | * | |
195 | * Returns 1 on success or < 0 on error. | |
196 | */ | |
fd3fa5c3 | 197 | static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path, |
8cb22a66 | 198 | const char *envaddr_name) |
2373cba3 PC |
199 | { |
200 | unsigned long file_addr; | |
201 | char *envaddr; | |
202 | ||
203 | envaddr = from_env(envaddr_name); | |
204 | ||
205 | if (!envaddr) | |
206 | return -ENOENT; | |
207 | ||
208 | if (strict_strtoul(envaddr, 16, &file_addr) < 0) | |
209 | return -EINVAL; | |
210 | ||
fd3fa5c3 | 211 | return get_relfile(ctx, file_path, file_addr); |
2373cba3 PC |
212 | } |
213 | ||
214 | /* | |
215 | * Allocates memory for and initializes a pxe_label. This uses malloc, so the | |
216 | * result must be free()'d to reclaim the memory. | |
217 | * | |
218 | * Returns NULL if malloc fails. | |
219 | */ | |
220 | static struct pxe_label *label_create(void) | |
221 | { | |
222 | struct pxe_label *label; | |
223 | ||
224 | label = malloc(sizeof(struct pxe_label)); | |
225 | ||
226 | if (!label) | |
227 | return NULL; | |
228 | ||
229 | memset(label, 0, sizeof(struct pxe_label)); | |
230 | ||
231 | return label; | |
232 | } | |
233 | ||
234 | /* | |
235 | * Free the memory used by a pxe_label, including that used by its name, | |
236 | * kernel, append and initrd members, if they're non NULL. | |
237 | * | |
238 | * So - be sure to only use dynamically allocated memory for the members of | |
239 | * the pxe_label struct, unless you want to clean it up first. These are | |
240 | * currently only created by the pxe file parsing code. | |
241 | */ | |
242 | static void label_destroy(struct pxe_label *label) | |
243 | { | |
244 | if (label->name) | |
245 | free(label->name); | |
246 | ||
247 | if (label->kernel) | |
248 | free(label->kernel); | |
249 | ||
250 | if (label->config) | |
251 | free(label->config); | |
252 | ||
253 | if (label->append) | |
254 | free(label->append); | |
255 | ||
256 | if (label->initrd) | |
257 | free(label->initrd); | |
258 | ||
259 | if (label->fdt) | |
260 | free(label->fdt); | |
261 | ||
262 | if (label->fdtdir) | |
263 | free(label->fdtdir); | |
264 | ||
69076dff NA |
265 | if (label->fdtoverlays) |
266 | free(label->fdtoverlays); | |
267 | ||
2373cba3 PC |
268 | free(label); |
269 | } | |
270 | ||
271 | /* | |
272 | * Print a label and its string members if they're defined. | |
273 | * | |
274 | * This is passed as a callback to the menu code for displaying each | |
275 | * menu entry. | |
276 | */ | |
277 | static void label_print(void *data) | |
278 | { | |
279 | struct pxe_label *label = data; | |
280 | const char *c = label->menu ? label->menu : label->name; | |
281 | ||
282 | printf("%s:\t%s\n", label->num, c); | |
283 | } | |
284 | ||
285 | /* | |
286 | * Boot a label that specified 'localboot'. This requires that the 'localcmd' | |
287 | * environment variable is defined. Its contents will be executed as U-Boot | |
288 | * command. If the label specified an 'append' line, its contents will be | |
289 | * used to overwrite the contents of the 'bootargs' environment variable prior | |
290 | * to running 'localcmd'. | |
291 | * | |
292 | * Returns 1 on success or < 0 on error. | |
293 | */ | |
294 | static int label_localboot(struct pxe_label *label) | |
295 | { | |
296 | char *localcmd; | |
297 | ||
298 | localcmd = from_env("localcmd"); | |
299 | ||
300 | if (!localcmd) | |
301 | return -ENOENT; | |
302 | ||
303 | if (label->append) { | |
304 | char bootargs[CONFIG_SYS_CBSIZE]; | |
305 | ||
1a62d64c SG |
306 | cli_simple_process_macros(label->append, bootargs, |
307 | sizeof(bootargs)); | |
2373cba3 PC |
308 | env_set("bootargs", bootargs); |
309 | } | |
310 | ||
311 | debug("running: %s\n", localcmd); | |
312 | ||
313 | return run_command_list(localcmd, strlen(localcmd), 0); | |
314 | } | |
315 | ||
69076dff NA |
316 | /* |
317 | * Loads fdt overlays specified in 'fdtoverlays'. | |
318 | */ | |
319 | #ifdef CONFIG_OF_LIBFDT_OVERLAY | |
fd3fa5c3 SG |
320 | static void label_boot_fdtoverlay(struct pxe_context *ctx, |
321 | struct pxe_label *label) | |
69076dff NA |
322 | { |
323 | char *fdtoverlay = label->fdtoverlays; | |
324 | struct fdt_header *working_fdt; | |
325 | char *fdtoverlay_addr_env; | |
326 | ulong fdtoverlay_addr; | |
327 | ulong fdt_addr; | |
328 | int err; | |
329 | ||
330 | /* Get the main fdt and map it */ | |
7e5f460e | 331 | fdt_addr = hextoul(env_get("fdt_addr_r"), NULL); |
69076dff NA |
332 | working_fdt = map_sysmem(fdt_addr, 0); |
333 | err = fdt_check_header(working_fdt); | |
334 | if (err) | |
335 | return; | |
336 | ||
337 | /* Get the specific overlay loading address */ | |
338 | fdtoverlay_addr_env = env_get("fdtoverlay_addr_r"); | |
339 | if (!fdtoverlay_addr_env) { | |
340 | printf("Invalid fdtoverlay_addr_r for loading overlays\n"); | |
341 | return; | |
342 | } | |
343 | ||
7e5f460e | 344 | fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL); |
69076dff NA |
345 | |
346 | /* Cycle over the overlay files and apply them in order */ | |
347 | do { | |
348 | struct fdt_header *blob; | |
349 | char *overlayfile; | |
350 | char *end; | |
351 | int len; | |
352 | ||
353 | /* Drop leading spaces */ | |
354 | while (*fdtoverlay == ' ') | |
355 | ++fdtoverlay; | |
356 | ||
357 | /* Copy a single filename if multiple provided */ | |
358 | end = strstr(fdtoverlay, " "); | |
359 | if (end) { | |
360 | len = (int)(end - fdtoverlay); | |
361 | overlayfile = malloc(len + 1); | |
362 | strncpy(overlayfile, fdtoverlay, len); | |
363 | overlayfile[len] = '\0'; | |
364 | } else | |
365 | overlayfile = fdtoverlay; | |
366 | ||
367 | if (!strlen(overlayfile)) | |
368 | goto skip_overlay; | |
369 | ||
370 | /* Load overlay file */ | |
fd3fa5c3 | 371 | err = get_relfile_envaddr(ctx, overlayfile, |
69076dff NA |
372 | "fdtoverlay_addr_r"); |
373 | if (err < 0) { | |
374 | printf("Failed loading overlay %s\n", overlayfile); | |
375 | goto skip_overlay; | |
376 | } | |
377 | ||
378 | /* Resize main fdt */ | |
379 | fdt_shrink_to_minimum(working_fdt, 8192); | |
380 | ||
381 | blob = map_sysmem(fdtoverlay_addr, 0); | |
382 | err = fdt_check_header(blob); | |
383 | if (err) { | |
384 | printf("Invalid overlay %s, skipping\n", | |
385 | overlayfile); | |
386 | goto skip_overlay; | |
387 | } | |
388 | ||
389 | err = fdt_overlay_apply_verbose(working_fdt, blob); | |
390 | if (err) { | |
391 | printf("Failed to apply overlay %s, skipping\n", | |
392 | overlayfile); | |
393 | goto skip_overlay; | |
394 | } | |
395 | ||
396 | skip_overlay: | |
397 | if (end) | |
398 | free(overlayfile); | |
399 | } while ((fdtoverlay = strstr(fdtoverlay, " "))); | |
400 | } | |
401 | #endif | |
402 | ||
2373cba3 PC |
403 | /* |
404 | * Boot according to the contents of a pxe_label. | |
405 | * | |
406 | * If we can't boot for any reason, we return. A successful boot never | |
407 | * returns. | |
408 | * | |
409 | * The kernel will be stored in the location given by the 'kernel_addr_r' | |
410 | * environment variable. | |
411 | * | |
412 | * If the label specifies an initrd file, it will be stored in the location | |
413 | * given by the 'ramdisk_addr_r' environment variable. | |
414 | * | |
415 | * If the label specifies an 'append' line, its contents will overwrite that | |
416 | * of the 'bootargs' environment variable. | |
417 | */ | |
fd3fa5c3 | 418 | static int label_boot(struct pxe_context *ctx, struct pxe_label *label) |
2373cba3 PC |
419 | { |
420 | char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL }; | |
23f3e399 | 421 | char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL }; |
c97bd17b ZL |
422 | char *kernel_addr = NULL; |
423 | char *initrd_addr_str = NULL; | |
23f3e399 | 424 | char initrd_filesize[10]; |
c97bd17b | 425 | char initrd_str[28]; |
2373cba3 PC |
426 | char mac_str[29] = ""; |
427 | char ip_str[68] = ""; | |
428 | char *fit_addr = NULL; | |
429 | int bootm_argc = 2; | |
23f3e399 | 430 | int zboot_argc = 3; |
2373cba3 | 431 | int len = 0; |
c97bd17b | 432 | ulong kernel_addr_r; |
2373cba3 PC |
433 | void *buf; |
434 | ||
435 | label_print(label); | |
436 | ||
437 | label->attempted = 1; | |
438 | ||
439 | if (label->localboot) { | |
440 | if (label->localboot_val >= 0) | |
441 | label_localboot(label); | |
442 | return 0; | |
443 | } | |
444 | ||
8cb22a66 | 445 | if (!label->kernel) { |
2373cba3 | 446 | printf("No kernel given, skipping %s\n", |
8cb22a66 | 447 | label->name); |
2373cba3 PC |
448 | return 1; |
449 | } | |
450 | ||
451 | if (label->initrd) { | |
fd3fa5c3 | 452 | if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) { |
2373cba3 | 453 | printf("Skipping %s for failure retrieving initrd\n", |
8cb22a66 | 454 | label->name); |
2373cba3 PC |
455 | return 1; |
456 | } | |
457 | ||
c97bd17b | 458 | initrd_addr_str = env_get("ramdisk_addr_r"); |
23f3e399 | 459 | strncpy(initrd_filesize, env_get("filesize"), 9); |
c97bd17b ZL |
460 | |
461 | strncpy(initrd_str, initrd_addr_str, 18); | |
462 | strcat(initrd_str, ":"); | |
463 | strncat(initrd_str, initrd_filesize, 9); | |
2373cba3 PC |
464 | } |
465 | ||
fd3fa5c3 | 466 | if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) { |
2373cba3 | 467 | printf("Skipping %s for failure retrieving kernel\n", |
8cb22a66 | 468 | label->name); |
2373cba3 PC |
469 | return 1; |
470 | } | |
471 | ||
472 | if (label->ipappend & 0x1) { | |
473 | sprintf(ip_str, " ip=%s:%s:%s:%s", | |
474 | env_get("ipaddr"), env_get("serverip"), | |
475 | env_get("gatewayip"), env_get("netmask")); | |
476 | } | |
477 | ||
ff0287ec KM |
478 | if (IS_ENABLED(CONFIG_CMD_NET)) { |
479 | if (label->ipappend & 0x2) { | |
480 | int err; | |
481 | ||
482 | strcpy(mac_str, " BOOTIF="); | |
483 | err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8); | |
484 | if (err < 0) | |
485 | mac_str[0] = '\0'; | |
486 | } | |
2373cba3 | 487 | } |
2373cba3 PC |
488 | |
489 | if ((label->ipappend & 0x3) || label->append) { | |
490 | char bootargs[CONFIG_SYS_CBSIZE] = ""; | |
491 | char finalbootargs[CONFIG_SYS_CBSIZE]; | |
492 | ||
493 | if (strlen(label->append ?: "") + | |
494 | strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) { | |
495 | printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n", | |
496 | strlen(label->append ?: ""), | |
497 | strlen(ip_str), strlen(mac_str), | |
498 | sizeof(bootargs)); | |
499 | return 1; | |
2373cba3 | 500 | } |
8cb22a66 PC |
501 | |
502 | if (label->append) | |
503 | strncpy(bootargs, label->append, sizeof(bootargs)); | |
504 | ||
505 | strcat(bootargs, ip_str); | |
506 | strcat(bootargs, mac_str); | |
507 | ||
1a62d64c SG |
508 | cli_simple_process_macros(bootargs, finalbootargs, |
509 | sizeof(finalbootargs)); | |
8cb22a66 PC |
510 | env_set("bootargs", finalbootargs); |
511 | printf("append: %s\n", finalbootargs); | |
2373cba3 PC |
512 | } |
513 | ||
c97bd17b | 514 | kernel_addr = env_get("kernel_addr_r"); |
23f3e399 | 515 | |
2373cba3 PC |
516 | /* for FIT, append the configuration identifier */ |
517 | if (label->config) { | |
c97bd17b | 518 | int len = strlen(kernel_addr) + strlen(label->config) + 1; |
2373cba3 PC |
519 | |
520 | fit_addr = malloc(len); | |
521 | if (!fit_addr) { | |
522 | printf("malloc fail (FIT address)\n"); | |
523 | return 1; | |
524 | } | |
c97bd17b ZL |
525 | snprintf(fit_addr, len, "%s%s", kernel_addr, label->config); |
526 | kernel_addr = fit_addr; | |
2373cba3 PC |
527 | } |
528 | ||
529 | /* | |
530 | * fdt usage is optional: | |
db366741 | 531 | * It handles the following scenarios. |
2373cba3 | 532 | * |
db366741 AL |
533 | * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is |
534 | * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to | |
535 | * bootm, and adjust argc appropriately. | |
536 | * | |
537 | * If retrieve fails and no exact fdt blob is specified in pxe file with | |
538 | * "fdt" label, try Scenario 2. | |
2373cba3 PC |
539 | * |
540 | * Scenario 2: If there is an fdt_addr specified, pass it along to | |
541 | * bootm, and adjust argc appropriately. | |
542 | * | |
543 | * Scenario 3: fdt blob is not available. | |
544 | */ | |
545 | bootm_argv[3] = env_get("fdt_addr_r"); | |
546 | ||
547 | /* if fdt label is defined then get fdt from server */ | |
548 | if (bootm_argv[3]) { | |
549 | char *fdtfile = NULL; | |
550 | char *fdtfilefree = NULL; | |
551 | ||
552 | if (label->fdt) { | |
553 | fdtfile = label->fdt; | |
554 | } else if (label->fdtdir) { | |
555 | char *f1, *f2, *f3, *f4, *slash; | |
556 | ||
557 | f1 = env_get("fdtfile"); | |
558 | if (f1) { | |
559 | f2 = ""; | |
560 | f3 = ""; | |
561 | f4 = ""; | |
562 | } else { | |
563 | /* | |
564 | * For complex cases where this code doesn't | |
565 | * generate the correct filename, the board | |
566 | * code should set $fdtfile during early boot, | |
567 | * or the boot scripts should set $fdtfile | |
568 | * before invoking "pxe" or "sysboot". | |
569 | */ | |
570 | f1 = env_get("soc"); | |
571 | f2 = "-"; | |
572 | f3 = env_get("board"); | |
573 | f4 = ".dtb"; | |
75efe7dc DJL |
574 | if (!f1) { |
575 | f1 = ""; | |
576 | f2 = ""; | |
577 | } | |
578 | if (!f3) { | |
579 | f2 = ""; | |
580 | f3 = ""; | |
581 | } | |
2373cba3 PC |
582 | } |
583 | ||
584 | len = strlen(label->fdtdir); | |
585 | if (!len) | |
586 | slash = "./"; | |
587 | else if (label->fdtdir[len - 1] != '/') | |
588 | slash = "/"; | |
589 | else | |
590 | slash = ""; | |
591 | ||
592 | len = strlen(label->fdtdir) + strlen(slash) + | |
593 | strlen(f1) + strlen(f2) + strlen(f3) + | |
594 | strlen(f4) + 1; | |
595 | fdtfilefree = malloc(len); | |
596 | if (!fdtfilefree) { | |
597 | printf("malloc fail (FDT filename)\n"); | |
598 | goto cleanup; | |
599 | } | |
600 | ||
601 | snprintf(fdtfilefree, len, "%s%s%s%s%s%s", | |
602 | label->fdtdir, slash, f1, f2, f3, f4); | |
603 | fdtfile = fdtfilefree; | |
604 | } | |
605 | ||
606 | if (fdtfile) { | |
fd3fa5c3 | 607 | int err = get_relfile_envaddr(ctx, fdtfile, |
8cb22a66 PC |
608 | "fdt_addr_r"); |
609 | ||
2373cba3 PC |
610 | free(fdtfilefree); |
611 | if (err < 0) { | |
db366741 AL |
612 | bootm_argv[3] = NULL; |
613 | ||
614 | if (label->fdt) { | |
615 | printf("Skipping %s for failure retrieving FDT\n", | |
616 | label->name); | |
617 | goto cleanup; | |
618 | } | |
2373cba3 | 619 | } |
69076dff NA |
620 | |
621 | #ifdef CONFIG_OF_LIBFDT_OVERLAY | |
622 | if (label->fdtoverlays) | |
fd3fa5c3 | 623 | label_boot_fdtoverlay(ctx, label); |
69076dff | 624 | #endif |
2373cba3 PC |
625 | } else { |
626 | bootm_argv[3] = NULL; | |
627 | } | |
628 | } | |
629 | ||
c97bd17b ZL |
630 | bootm_argv[1] = kernel_addr; |
631 | zboot_argv[1] = kernel_addr; | |
632 | ||
633 | if (initrd_addr_str) { | |
634 | bootm_argv[2] = initrd_str; | |
635 | bootm_argc = 3; | |
636 | ||
637 | zboot_argv[3] = initrd_addr_str; | |
638 | zboot_argv[4] = initrd_filesize; | |
639 | zboot_argc = 5; | |
640 | } | |
641 | ||
2373cba3 PC |
642 | if (!bootm_argv[3]) |
643 | bootm_argv[3] = env_get("fdt_addr"); | |
644 | ||
645 | if (bootm_argv[3]) { | |
646 | if (!bootm_argv[2]) | |
647 | bootm_argv[2] = "-"; | |
648 | bootm_argc = 4; | |
649 | } | |
650 | ||
c97bd17b ZL |
651 | kernel_addr_r = genimg_get_kernel_addr(kernel_addr); |
652 | buf = map_sysmem(kernel_addr_r, 0); | |
2373cba3 PC |
653 | /* Try bootm for legacy and FIT format image */ |
654 | if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID) | |
fd3fa5c3 | 655 | do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
2373cba3 | 656 | /* Try booting an AArch64 Linux kernel image */ |
ff0287ec | 657 | else if (IS_ENABLED(CONFIG_CMD_BOOTI)) |
fd3fa5c3 | 658 | do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
2373cba3 | 659 | /* Try booting a Image */ |
ff0287ec | 660 | else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) |
fd3fa5c3 | 661 | do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
18c25821 KM |
662 | /* Try booting an x86_64 Linux kernel image */ |
663 | else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) | |
fd3fa5c3 | 664 | do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL); |
ff0287ec | 665 | |
2373cba3 PC |
666 | unmap_sysmem(buf); |
667 | ||
668 | cleanup: | |
669 | if (fit_addr) | |
670 | free(fit_addr); | |
671 | return 1; | |
672 | } | |
673 | ||
674 | /* | |
675 | * Tokens for the pxe file parser. | |
676 | */ | |
677 | enum token_type { | |
678 | T_EOL, | |
679 | T_STRING, | |
680 | T_EOF, | |
681 | T_MENU, | |
682 | T_TITLE, | |
683 | T_TIMEOUT, | |
684 | T_LABEL, | |
685 | T_KERNEL, | |
686 | T_LINUX, | |
687 | T_APPEND, | |
688 | T_INITRD, | |
689 | T_LOCALBOOT, | |
690 | T_DEFAULT, | |
691 | T_PROMPT, | |
692 | T_INCLUDE, | |
693 | T_FDT, | |
694 | T_FDTDIR, | |
69076dff | 695 | T_FDTOVERLAYS, |
2373cba3 PC |
696 | T_ONTIMEOUT, |
697 | T_IPAPPEND, | |
698 | T_BACKGROUND, | |
699 | T_INVALID | |
700 | }; | |
701 | ||
702 | /* | |
703 | * A token - given by a value and a type. | |
704 | */ | |
705 | struct token { | |
706 | char *val; | |
707 | enum token_type type; | |
708 | }; | |
709 | ||
710 | /* | |
711 | * Keywords recognized. | |
712 | */ | |
713 | static const struct token keywords[] = { | |
714 | {"menu", T_MENU}, | |
715 | {"title", T_TITLE}, | |
716 | {"timeout", T_TIMEOUT}, | |
717 | {"default", T_DEFAULT}, | |
718 | {"prompt", T_PROMPT}, | |
719 | {"label", T_LABEL}, | |
720 | {"kernel", T_KERNEL}, | |
721 | {"linux", T_LINUX}, | |
722 | {"localboot", T_LOCALBOOT}, | |
723 | {"append", T_APPEND}, | |
724 | {"initrd", T_INITRD}, | |
725 | {"include", T_INCLUDE}, | |
726 | {"devicetree", T_FDT}, | |
727 | {"fdt", T_FDT}, | |
728 | {"devicetreedir", T_FDTDIR}, | |
729 | {"fdtdir", T_FDTDIR}, | |
69076dff | 730 | {"fdtoverlays", T_FDTOVERLAYS}, |
2373cba3 PC |
731 | {"ontimeout", T_ONTIMEOUT,}, |
732 | {"ipappend", T_IPAPPEND,}, | |
733 | {"background", T_BACKGROUND,}, | |
734 | {NULL, T_INVALID} | |
735 | }; | |
736 | ||
737 | /* | |
738 | * Since pxe(linux) files don't have a token to identify the start of a | |
739 | * literal, we have to keep track of when we're in a state where a literal is | |
740 | * expected vs when we're in a state a keyword is expected. | |
741 | */ | |
742 | enum lex_state { | |
743 | L_NORMAL = 0, | |
744 | L_KEYWORD, | |
745 | L_SLITERAL | |
746 | }; | |
747 | ||
748 | /* | |
749 | * get_string retrieves a string from *p and stores it as a token in | |
750 | * *t. | |
751 | * | |
752 | * get_string used for scanning both string literals and keywords. | |
753 | * | |
754 | * Characters from *p are copied into t-val until a character equal to | |
755 | * delim is found, or a NUL byte is reached. If delim has the special value of | |
756 | * ' ', any whitespace character will be used as a delimiter. | |
757 | * | |
758 | * If lower is unequal to 0, uppercase characters will be converted to | |
759 | * lowercase in the result. This is useful to make keywords case | |
760 | * insensitive. | |
761 | * | |
762 | * The location of *p is updated to point to the first character after the end | |
763 | * of the token - the ending delimiter. | |
764 | * | |
765 | * On success, the new value of t->val is returned. Memory for t->val is | |
766 | * allocated using malloc and must be free()'d to reclaim it. If insufficient | |
767 | * memory is available, NULL is returned. | |
768 | */ | |
769 | static char *get_string(char **p, struct token *t, char delim, int lower) | |
770 | { | |
771 | char *b, *e; | |
772 | size_t len, i; | |
773 | ||
774 | /* | |
775 | * b and e both start at the beginning of the input stream. | |
776 | * | |
777 | * e is incremented until we find the ending delimiter, or a NUL byte | |
778 | * is reached. Then, we take e - b to find the length of the token. | |
779 | */ | |
8cb22a66 PC |
780 | b = *p; |
781 | e = *p; | |
2373cba3 PC |
782 | |
783 | while (*e) { | |
784 | if ((delim == ' ' && isspace(*e)) || delim == *e) | |
785 | break; | |
786 | e++; | |
787 | } | |
788 | ||
789 | len = e - b; | |
790 | ||
791 | /* | |
792 | * Allocate memory to hold the string, and copy it in, converting | |
793 | * characters to lowercase if lower is != 0. | |
794 | */ | |
795 | t->val = malloc(len + 1); | |
796 | if (!t->val) | |
797 | return NULL; | |
798 | ||
799 | for (i = 0; i < len; i++, b++) { | |
800 | if (lower) | |
801 | t->val[i] = tolower(*b); | |
802 | else | |
803 | t->val[i] = *b; | |
804 | } | |
805 | ||
806 | t->val[len] = '\0'; | |
807 | ||
808 | /* | |
809 | * Update *p so the caller knows where to continue scanning. | |
810 | */ | |
811 | *p = e; | |
812 | ||
813 | t->type = T_STRING; | |
814 | ||
815 | return t->val; | |
816 | } | |
817 | ||
818 | /* | |
819 | * Populate a keyword token with a type and value. | |
820 | */ | |
821 | static void get_keyword(struct token *t) | |
822 | { | |
823 | int i; | |
824 | ||
825 | for (i = 0; keywords[i].val; i++) { | |
826 | if (!strcmp(t->val, keywords[i].val)) { | |
827 | t->type = keywords[i].type; | |
828 | break; | |
829 | } | |
830 | } | |
831 | } | |
832 | ||
833 | /* | |
834 | * Get the next token. We have to keep track of which state we're in to know | |
835 | * if we're looking to get a string literal or a keyword. | |
836 | * | |
837 | * *p is updated to point at the first character after the current token. | |
838 | */ | |
839 | static void get_token(char **p, struct token *t, enum lex_state state) | |
840 | { | |
841 | char *c = *p; | |
842 | ||
843 | t->type = T_INVALID; | |
844 | ||
845 | /* eat non EOL whitespace */ | |
846 | while (isblank(*c)) | |
847 | c++; | |
848 | ||
849 | /* | |
850 | * eat comments. note that string literals can't begin with #, but | |
851 | * can contain a # after their first character. | |
852 | */ | |
853 | if (*c == '#') { | |
854 | while (*c && *c != '\n') | |
855 | c++; | |
856 | } | |
857 | ||
858 | if (*c == '\n') { | |
859 | t->type = T_EOL; | |
860 | c++; | |
861 | } else if (*c == '\0') { | |
862 | t->type = T_EOF; | |
863 | c++; | |
864 | } else if (state == L_SLITERAL) { | |
865 | get_string(&c, t, '\n', 0); | |
866 | } else if (state == L_KEYWORD) { | |
867 | /* | |
868 | * when we expect a keyword, we first get the next string | |
869 | * token delimited by whitespace, and then check if it | |
870 | * matches a keyword in our keyword list. if it does, it's | |
871 | * converted to a keyword token of the appropriate type, and | |
872 | * if not, it remains a string token. | |
873 | */ | |
874 | get_string(&c, t, ' ', 1); | |
875 | get_keyword(t); | |
876 | } | |
877 | ||
878 | *p = c; | |
879 | } | |
880 | ||
881 | /* | |
882 | * Increment *c until we get to the end of the current line, or EOF. | |
883 | */ | |
884 | static void eol_or_eof(char **c) | |
885 | { | |
886 | while (**c && **c != '\n') | |
887 | (*c)++; | |
888 | } | |
889 | ||
890 | /* | |
891 | * All of these parse_* functions share some common behavior. | |
892 | * | |
893 | * They finish with *c pointing after the token they parse, and return 1 on | |
894 | * success, or < 0 on error. | |
895 | */ | |
896 | ||
897 | /* | |
898 | * Parse a string literal and store a pointer it at *dst. String literals | |
899 | * terminate at the end of the line. | |
900 | */ | |
901 | static int parse_sliteral(char **c, char **dst) | |
902 | { | |
903 | struct token t; | |
904 | char *s = *c; | |
905 | ||
906 | get_token(c, &t, L_SLITERAL); | |
907 | ||
908 | if (t.type != T_STRING) { | |
909 | printf("Expected string literal: %.*s\n", (int)(*c - s), s); | |
910 | return -EINVAL; | |
911 | } | |
912 | ||
913 | *dst = t.val; | |
914 | ||
915 | return 1; | |
916 | } | |
917 | ||
918 | /* | |
919 | * Parse a base 10 (unsigned) integer and store it at *dst. | |
920 | */ | |
921 | static int parse_integer(char **c, int *dst) | |
922 | { | |
923 | struct token t; | |
924 | char *s = *c; | |
925 | ||
926 | get_token(c, &t, L_SLITERAL); | |
927 | ||
928 | if (t.type != T_STRING) { | |
929 | printf("Expected string: %.*s\n", (int)(*c - s), s); | |
930 | return -EINVAL; | |
931 | } | |
932 | ||
933 | *dst = simple_strtol(t.val, NULL, 10); | |
934 | ||
935 | free(t.val); | |
936 | ||
937 | return 1; | |
938 | } | |
939 | ||
fd3fa5c3 | 940 | static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base, |
8cb22a66 | 941 | struct pxe_menu *cfg, int nest_level); |
2373cba3 PC |
942 | |
943 | /* | |
944 | * Parse an include statement, and retrieve and parse the file it mentions. | |
945 | * | |
946 | * base should point to a location where it's safe to store the file, and | |
947 | * nest_level should indicate how many nested includes have occurred. For this | |
948 | * include, nest_level has already been incremented and doesn't need to be | |
949 | * incremented here. | |
950 | */ | |
fd3fa5c3 | 951 | static int handle_include(struct pxe_context *ctx, char **c, unsigned long base, |
8cb22a66 | 952 | struct pxe_menu *cfg, int nest_level) |
2373cba3 PC |
953 | { |
954 | char *include_path; | |
955 | char *s = *c; | |
956 | int err; | |
957 | char *buf; | |
958 | int ret; | |
959 | ||
960 | err = parse_sliteral(c, &include_path); | |
961 | ||
962 | if (err < 0) { | |
8cb22a66 | 963 | printf("Expected include path: %.*s\n", (int)(*c - s), s); |
2373cba3 PC |
964 | return err; |
965 | } | |
966 | ||
fd3fa5c3 | 967 | err = get_pxe_file(ctx, include_path, base); |
2373cba3 PC |
968 | |
969 | if (err < 0) { | |
970 | printf("Couldn't retrieve %s\n", include_path); | |
971 | return err; | |
972 | } | |
973 | ||
974 | buf = map_sysmem(base, 0); | |
fd3fa5c3 | 975 | ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level); |
2373cba3 PC |
976 | unmap_sysmem(buf); |
977 | ||
978 | return ret; | |
979 | } | |
980 | ||
981 | /* | |
982 | * Parse lines that begin with 'menu'. | |
983 | * | |
984 | * base and nest are provided to handle the 'menu include' case. | |
985 | * | |
986 | * base should point to a location where it's safe to store the included file. | |
987 | * | |
988 | * nest_level should be 1 when parsing the top level pxe file, 2 when parsing | |
989 | * a file it includes, 3 when parsing a file included by that file, and so on. | |
990 | */ | |
fd3fa5c3 | 991 | static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg, |
8cb22a66 | 992 | unsigned long base, int nest_level) |
2373cba3 PC |
993 | { |
994 | struct token t; | |
995 | char *s = *c; | |
996 | int err = 0; | |
997 | ||
998 | get_token(c, &t, L_KEYWORD); | |
999 | ||
1000 | switch (t.type) { | |
1001 | case T_TITLE: | |
1002 | err = parse_sliteral(c, &cfg->title); | |
1003 | ||
1004 | break; | |
1005 | ||
1006 | case T_INCLUDE: | |
fd3fa5c3 | 1007 | err = handle_include(ctx, c, base, cfg, nest_level + 1); |
2373cba3 PC |
1008 | break; |
1009 | ||
1010 | case T_BACKGROUND: | |
1011 | err = parse_sliteral(c, &cfg->bmp); | |
1012 | break; | |
1013 | ||
1014 | default: | |
1015 | printf("Ignoring malformed menu command: %.*s\n", | |
8cb22a66 | 1016 | (int)(*c - s), s); |
2373cba3 PC |
1017 | } |
1018 | ||
1019 | if (err < 0) | |
1020 | return err; | |
1021 | ||
1022 | eol_or_eof(c); | |
1023 | ||
1024 | return 1; | |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * Handles parsing a 'menu line' when we're parsing a label. | |
1029 | */ | |
1030 | static int parse_label_menu(char **c, struct pxe_menu *cfg, | |
8cb22a66 | 1031 | struct pxe_label *label) |
2373cba3 PC |
1032 | { |
1033 | struct token t; | |
1034 | char *s; | |
1035 | ||
1036 | s = *c; | |
1037 | ||
1038 | get_token(c, &t, L_KEYWORD); | |
1039 | ||
1040 | switch (t.type) { | |
1041 | case T_DEFAULT: | |
1042 | if (!cfg->default_label) | |
1043 | cfg->default_label = strdup(label->name); | |
1044 | ||
1045 | if (!cfg->default_label) | |
1046 | return -ENOMEM; | |
1047 | ||
1048 | break; | |
1049 | case T_LABEL: | |
1050 | parse_sliteral(c, &label->menu); | |
1051 | break; | |
1052 | default: | |
1053 | printf("Ignoring malformed menu command: %.*s\n", | |
8cb22a66 | 1054 | (int)(*c - s), s); |
2373cba3 PC |
1055 | } |
1056 | ||
1057 | eol_or_eof(c); | |
1058 | ||
1059 | return 0; | |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * Handles parsing a 'kernel' label. | |
1064 | * expecting "filename" or "<fit_filename>#cfg" | |
1065 | */ | |
1066 | static int parse_label_kernel(char **c, struct pxe_label *label) | |
1067 | { | |
1068 | char *s; | |
1069 | int err; | |
1070 | ||
1071 | err = parse_sliteral(c, &label->kernel); | |
1072 | if (err < 0) | |
1073 | return err; | |
1074 | ||
1075 | s = strstr(label->kernel, "#"); | |
1076 | if (!s) | |
1077 | return 1; | |
1078 | ||
1079 | label->config = malloc(strlen(s) + 1); | |
1080 | if (!label->config) | |
1081 | return -ENOMEM; | |
1082 | ||
1083 | strcpy(label->config, s); | |
1084 | *s = 0; | |
1085 | ||
1086 | return 1; | |
1087 | } | |
1088 | ||
1089 | /* | |
1090 | * Parses a label and adds it to the list of labels for a menu. | |
1091 | * | |
1092 | * A label ends when we either get to the end of a file, or | |
1093 | * get some input we otherwise don't have a handler defined | |
1094 | * for. | |
1095 | * | |
1096 | */ | |
1097 | static int parse_label(char **c, struct pxe_menu *cfg) | |
1098 | { | |
1099 | struct token t; | |
1100 | int len; | |
1101 | char *s = *c; | |
1102 | struct pxe_label *label; | |
1103 | int err; | |
1104 | ||
1105 | label = label_create(); | |
1106 | if (!label) | |
1107 | return -ENOMEM; | |
1108 | ||
1109 | err = parse_sliteral(c, &label->name); | |
1110 | if (err < 0) { | |
1111 | printf("Expected label name: %.*s\n", (int)(*c - s), s); | |
1112 | label_destroy(label); | |
1113 | return -EINVAL; | |
1114 | } | |
1115 | ||
1116 | list_add_tail(&label->list, &cfg->labels); | |
1117 | ||
1118 | while (1) { | |
1119 | s = *c; | |
1120 | get_token(c, &t, L_KEYWORD); | |
1121 | ||
1122 | err = 0; | |
1123 | switch (t.type) { | |
1124 | case T_MENU: | |
1125 | err = parse_label_menu(c, cfg, label); | |
1126 | break; | |
1127 | ||
1128 | case T_KERNEL: | |
1129 | case T_LINUX: | |
1130 | err = parse_label_kernel(c, label); | |
1131 | break; | |
1132 | ||
1133 | case T_APPEND: | |
1134 | err = parse_sliteral(c, &label->append); | |
1135 | if (label->initrd) | |
1136 | break; | |
1137 | s = strstr(label->append, "initrd="); | |
1138 | if (!s) | |
1139 | break; | |
1140 | s += 7; | |
1141 | len = (int)(strchr(s, ' ') - s); | |
1142 | label->initrd = malloc(len + 1); | |
1143 | strncpy(label->initrd, s, len); | |
1144 | label->initrd[len] = '\0'; | |
1145 | ||
1146 | break; | |
1147 | ||
1148 | case T_INITRD: | |
1149 | if (!label->initrd) | |
1150 | err = parse_sliteral(c, &label->initrd); | |
1151 | break; | |
1152 | ||
1153 | case T_FDT: | |
1154 | if (!label->fdt) | |
1155 | err = parse_sliteral(c, &label->fdt); | |
1156 | break; | |
1157 | ||
1158 | case T_FDTDIR: | |
1159 | if (!label->fdtdir) | |
1160 | err = parse_sliteral(c, &label->fdtdir); | |
1161 | break; | |
1162 | ||
69076dff NA |
1163 | case T_FDTOVERLAYS: |
1164 | if (!label->fdtoverlays) | |
1165 | err = parse_sliteral(c, &label->fdtoverlays); | |
1166 | break; | |
1167 | ||
2373cba3 PC |
1168 | case T_LOCALBOOT: |
1169 | label->localboot = 1; | |
1170 | err = parse_integer(c, &label->localboot_val); | |
1171 | break; | |
1172 | ||
1173 | case T_IPAPPEND: | |
1174 | err = parse_integer(c, &label->ipappend); | |
1175 | break; | |
1176 | ||
1177 | case T_EOL: | |
1178 | break; | |
1179 | ||
1180 | default: | |
1181 | /* | |
1182 | * put the token back! we don't want it - it's the end | |
1183 | * of a label and whatever token this is, it's | |
1184 | * something for the menu level context to handle. | |
1185 | */ | |
1186 | *c = s; | |
1187 | return 1; | |
1188 | } | |
1189 | ||
1190 | if (err < 0) | |
1191 | return err; | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | /* | |
1196 | * This 16 comes from the limit pxelinux imposes on nested includes. | |
1197 | * | |
1198 | * There is no reason at all we couldn't do more, but some limit helps prevent | |
1199 | * infinite (until crash occurs) recursion if a file tries to include itself. | |
1200 | */ | |
1201 | #define MAX_NEST_LEVEL 16 | |
1202 | ||
1203 | /* | |
1204 | * Entry point for parsing a menu file. nest_level indicates how many times | |
1205 | * we've nested in includes. It will be 1 for the top level menu file. | |
1206 | * | |
1207 | * Returns 1 on success, < 0 on error. | |
1208 | */ | |
fd3fa5c3 | 1209 | static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base, |
8cb22a66 | 1210 | struct pxe_menu *cfg, int nest_level) |
2373cba3 PC |
1211 | { |
1212 | struct token t; | |
1213 | char *s, *b, *label_name; | |
1214 | int err; | |
1215 | ||
1216 | b = p; | |
1217 | ||
1218 | if (nest_level > MAX_NEST_LEVEL) { | |
1219 | printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL); | |
1220 | return -EMLINK; | |
1221 | } | |
1222 | ||
1223 | while (1) { | |
1224 | s = p; | |
1225 | ||
1226 | get_token(&p, &t, L_KEYWORD); | |
1227 | ||
1228 | err = 0; | |
1229 | switch (t.type) { | |
1230 | case T_MENU: | |
1231 | cfg->prompt = 1; | |
fd3fa5c3 | 1232 | err = parse_menu(ctx, &p, cfg, |
8cb22a66 PC |
1233 | base + ALIGN(strlen(b) + 1, 4), |
1234 | nest_level); | |
2373cba3 PC |
1235 | break; |
1236 | ||
1237 | case T_TIMEOUT: | |
1238 | err = parse_integer(&p, &cfg->timeout); | |
1239 | break; | |
1240 | ||
1241 | case T_LABEL: | |
1242 | err = parse_label(&p, cfg); | |
1243 | break; | |
1244 | ||
1245 | case T_DEFAULT: | |
1246 | case T_ONTIMEOUT: | |
1247 | err = parse_sliteral(&p, &label_name); | |
1248 | ||
1249 | if (label_name) { | |
1250 | if (cfg->default_label) | |
1251 | free(cfg->default_label); | |
1252 | ||
1253 | cfg->default_label = label_name; | |
1254 | } | |
1255 | ||
1256 | break; | |
1257 | ||
1258 | case T_INCLUDE: | |
fd3fa5c3 | 1259 | err = handle_include(ctx, &p, |
8cb22a66 PC |
1260 | base + ALIGN(strlen(b), 4), cfg, |
1261 | nest_level + 1); | |
2373cba3 PC |
1262 | break; |
1263 | ||
1264 | case T_PROMPT: | |
1265 | eol_or_eof(&p); | |
1266 | break; | |
1267 | ||
1268 | case T_EOL: | |
1269 | break; | |
1270 | ||
1271 | case T_EOF: | |
1272 | return 1; | |
1273 | ||
1274 | default: | |
1275 | printf("Ignoring unknown command: %.*s\n", | |
8cb22a66 | 1276 | (int)(p - s), s); |
2373cba3 PC |
1277 | eol_or_eof(&p); |
1278 | } | |
1279 | ||
1280 | if (err < 0) | |
1281 | return err; | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | /* | |
2373cba3 PC |
1286 | */ |
1287 | void destroy_pxe_menu(struct pxe_menu *cfg) | |
1288 | { | |
1289 | struct list_head *pos, *n; | |
1290 | struct pxe_label *label; | |
1291 | ||
1292 | if (cfg->title) | |
1293 | free(cfg->title); | |
1294 | ||
1295 | if (cfg->default_label) | |
1296 | free(cfg->default_label); | |
1297 | ||
1298 | list_for_each_safe(pos, n, &cfg->labels) { | |
1299 | label = list_entry(pos, struct pxe_label, list); | |
1300 | ||
1301 | label_destroy(label); | |
1302 | } | |
1303 | ||
1304 | free(cfg); | |
1305 | } | |
1306 | ||
fd3fa5c3 | 1307 | struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg) |
2373cba3 PC |
1308 | { |
1309 | struct pxe_menu *cfg; | |
1310 | char *buf; | |
1311 | int r; | |
1312 | ||
1313 | cfg = malloc(sizeof(struct pxe_menu)); | |
1314 | ||
1315 | if (!cfg) | |
1316 | return NULL; | |
1317 | ||
1318 | memset(cfg, 0, sizeof(struct pxe_menu)); | |
1319 | ||
1320 | INIT_LIST_HEAD(&cfg->labels); | |
1321 | ||
1322 | buf = map_sysmem(menucfg, 0); | |
fd3fa5c3 | 1323 | r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1); |
2373cba3 PC |
1324 | unmap_sysmem(buf); |
1325 | ||
1326 | if (r < 0) { | |
1327 | destroy_pxe_menu(cfg); | |
1328 | return NULL; | |
1329 | } | |
1330 | ||
1331 | return cfg; | |
1332 | } | |
1333 | ||
1334 | /* | |
1335 | * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic | |
1336 | * menu code. | |
1337 | */ | |
1338 | static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg) | |
1339 | { | |
1340 | struct pxe_label *label; | |
1341 | struct list_head *pos; | |
1342 | struct menu *m; | |
1343 | int err; | |
1344 | int i = 1; | |
1345 | char *default_num = NULL; | |
1346 | ||
1347 | /* | |
1348 | * Create a menu and add items for all the labels. | |
1349 | */ | |
1350 | m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10), | |
5168d7a6 | 1351 | cfg->prompt, NULL, label_print, NULL, NULL); |
2373cba3 PC |
1352 | |
1353 | if (!m) | |
1354 | return NULL; | |
1355 | ||
1356 | list_for_each(pos, &cfg->labels) { | |
1357 | label = list_entry(pos, struct pxe_label, list); | |
1358 | ||
1359 | sprintf(label->num, "%d", i++); | |
1360 | if (menu_item_add(m, label->num, label) != 1) { | |
1361 | menu_destroy(m); | |
1362 | return NULL; | |
1363 | } | |
1364 | if (cfg->default_label && | |
1365 | (strcmp(label->name, cfg->default_label) == 0)) | |
1366 | default_num = label->num; | |
2373cba3 PC |
1367 | } |
1368 | ||
1369 | /* | |
1370 | * After we've created items for each label in the menu, set the | |
1371 | * menu's default label if one was specified. | |
1372 | */ | |
1373 | if (default_num) { | |
1374 | err = menu_default_set(m, default_num); | |
1375 | if (err != 1) { | |
1376 | if (err != -ENOENT) { | |
1377 | menu_destroy(m); | |
1378 | return NULL; | |
1379 | } | |
1380 | ||
1381 | printf("Missing default: %s\n", cfg->default_label); | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | return m; | |
1386 | } | |
1387 | ||
1388 | /* | |
1389 | * Try to boot any labels we have yet to attempt to boot. | |
1390 | */ | |
fd3fa5c3 SG |
1391 | static void boot_unattempted_labels(struct pxe_context *ctx, |
1392 | struct pxe_menu *cfg) | |
2373cba3 PC |
1393 | { |
1394 | struct list_head *pos; | |
1395 | struct pxe_label *label; | |
1396 | ||
1397 | list_for_each(pos, &cfg->labels) { | |
1398 | label = list_entry(pos, struct pxe_label, list); | |
1399 | ||
1400 | if (!label->attempted) | |
fd3fa5c3 | 1401 | label_boot(ctx, label); |
2373cba3 PC |
1402 | } |
1403 | } | |
1404 | ||
fd3fa5c3 | 1405 | void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg) |
2373cba3 PC |
1406 | { |
1407 | void *choice; | |
1408 | struct menu *m; | |
1409 | int err; | |
1410 | ||
ff0287ec KM |
1411 | if (IS_ENABLED(CONFIG_CMD_BMP)) { |
1412 | /* display BMP if available */ | |
1413 | if (cfg->bmp) { | |
fd3fa5c3 | 1414 | if (get_relfile(ctx, cfg->bmp, image_load_addr)) { |
ff0287ec KM |
1415 | if (CONFIG_IS_ENABLED(CMD_CLS)) |
1416 | run_command("cls", 0); | |
1417 | bmp_display(image_load_addr, | |
1418 | BMP_ALIGN_CENTER, BMP_ALIGN_CENTER); | |
1419 | } else { | |
1420 | printf("Skipping background bmp %s for failure\n", | |
1421 | cfg->bmp); | |
1422 | } | |
2373cba3 PC |
1423 | } |
1424 | } | |
2373cba3 PC |
1425 | |
1426 | m = pxe_menu_to_menu(cfg); | |
1427 | if (!m) | |
1428 | return; | |
1429 | ||
1430 | err = menu_get_choice(m, &choice); | |
1431 | ||
1432 | menu_destroy(m); | |
1433 | ||
1434 | /* | |
1435 | * err == 1 means we got a choice back from menu_get_choice. | |
1436 | * | |
1437 | * err == -ENOENT if the menu was setup to select the default but no | |
1438 | * default was set. in that case, we should continue trying to boot | |
1439 | * labels that haven't been attempted yet. | |
1440 | * | |
1441 | * otherwise, the user interrupted or there was some other error and | |
1442 | * we give up. | |
1443 | */ | |
1444 | ||
1445 | if (err == 1) { | |
fd3fa5c3 | 1446 | err = label_boot(ctx, choice); |
2373cba3 PC |
1447 | if (!err) |
1448 | return; | |
1449 | } else if (err != -ENOENT) { | |
1450 | return; | |
1451 | } | |
1452 | ||
fd3fa5c3 SG |
1453 | boot_unattempted_labels(ctx, cfg); |
1454 | } | |
1455 | ||
1456 | void pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp) | |
1457 | { | |
1458 | ctx->cmdtp = cmdtp; | |
2373cba3 | 1459 | } |