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