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