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