blob: b79fcb6418c77725c2c620a396bd396601f5c50b [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
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 Chotard6233de42019-11-25 09:07:39 +010087 if (!last_slash)
Patrice Chotard17e88042019-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 Chotard6233de42019-11-25 09:07:39 +010094 bootfile_path_size, path_len);
Patrice Chotard17e88042019-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 Glassed38aef2020-05-10 11:40:03 -0600107int (*do_getfile)(struct cmd_tbl *cmdtp, const char *file_path,
108 char *file_addr);
Patrice Chotard17e88042019-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 Glassed38aef2020-05-10 11:40:03 -0600118static int get_relfile(struct cmd_tbl *cmdtp, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100119 unsigned long file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100120{
121 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100122 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-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 Chotard6233de42019-11-25 09:07:39 +0100135 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-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 Glassed38aef2020-05-10 11:40:03 -0600156int get_pxe_file(struct cmd_tbl *cmdtp, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100157 unsigned long file_addr)
Patrice Chotard17e88042019-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 Chotard17e88042019-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 Glassed38aef2020-05-10 11:40:03 -0600197int get_pxelinux_path(struct cmd_tbl *cmdtp, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100198 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100199{
200 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100201 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-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 Chotard6233de42019-11-25 09:07:39 +0100205 PXELINUX_DIR, file);
Patrice Chotard17e88042019-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 Glassed38aef2020-05-10 11:40:03 -0600221static int get_relfile_envaddr(struct cmd_tbl *cmdtp, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100222 const char *envaddr_name)
Patrice Chotard17e88042019-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 Armstrongc77a1a32021-01-20 09:54:53 +0100289 if (label->fdtoverlays)
290 free(label->fdtoverlays);
291
Patrice Chotard17e88042019-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 Glassc7b03e82020-11-05 10:33:47 -0700330 cli_simple_process_macros(label->append, bootargs,
331 sizeof(bootargs));
Patrice Chotard17e88042019-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}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100339
340/*
341 * 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 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600354 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100355 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
Simon Glass3ff49ec2021-07-24 09:03:29 -0600367 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100368
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
Patrice Chotard17e88042019-11-25 09:07:37 +0100425
426/*
427 * 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 Glassed38aef2020-05-10 11:40:03 -0600441static int label_boot(struct cmd_tbl *cmdtp, struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +0100442{
443 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700444 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Libc14b932021-10-20 00:18:15 -0700445 char *kernel_addr = NULL;
446 char *initrd_addr_str = NULL;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700447 char initrd_filesize[10];
Zhaofeng Libc14b932021-10-20 00:18:15 -0700448 char initrd_str[28];
Patrice Chotard17e88042019-11-25 09:07:37 +0100449 char mac_str[29] = "";
450 char ip_str[68] = "";
451 char *fit_addr = NULL;
452 int bootm_argc = 2;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700453 int zboot_argc = 3;
Patrice Chotard17e88042019-11-25 09:07:37 +0100454 int len = 0;
Zhaofeng Libc14b932021-10-20 00:18:15 -0700455 ulong kernel_addr_r;
Patrice Chotard17e88042019-11-25 09:07:37 +0100456 void *buf;
457
458 label_print(label);
459
460 label->attempted = 1;
461
462 if (label->localboot) {
463 if (label->localboot_val >= 0)
464 label_localboot(label);
465 return 0;
466 }
467
Patrice Chotard6233de42019-11-25 09:07:39 +0100468 if (!label->kernel) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100469 printf("No kernel given, skipping %s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100470 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100471 return 1;
472 }
473
474 if (label->initrd) {
475 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
476 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100477 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100478 return 1;
479 }
480
Zhaofeng Libc14b932021-10-20 00:18:15 -0700481 initrd_addr_str = env_get("ramdisk_addr_r");
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700482 strncpy(initrd_filesize, env_get("filesize"), 9);
Zhaofeng Libc14b932021-10-20 00:18:15 -0700483
484 strncpy(initrd_str, initrd_addr_str, 18);
485 strcat(initrd_str, ":");
486 strncat(initrd_str, initrd_filesize, 9);
Patrice Chotard17e88042019-11-25 09:07:37 +0100487 }
488
489 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
490 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100491 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100492 return 1;
493 }
494
495 if (label->ipappend & 0x1) {
496 sprintf(ip_str, " ip=%s:%s:%s:%s",
497 env_get("ipaddr"), env_get("serverip"),
498 env_get("gatewayip"), env_get("netmask"));
499 }
500
Kory Maincentcaabd242021-02-02 16:42:28 +0100501 if (IS_ENABLED(CONFIG_CMD_NET)) {
502 if (label->ipappend & 0x2) {
503 int err;
Patrice Chotard6233de42019-11-25 09:07:39 +0100504
Kory Maincentcaabd242021-02-02 16:42:28 +0100505 strcpy(mac_str, " BOOTIF=");
506 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
507 if (err < 0)
508 mac_str[0] = '\0';
509 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100510 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100511
512 if ((label->ipappend & 0x3) || label->append) {
513 char bootargs[CONFIG_SYS_CBSIZE] = "";
514 char finalbootargs[CONFIG_SYS_CBSIZE];
515
516 if (strlen(label->append ?: "") +
517 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
518 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
519 strlen(label->append ?: ""),
520 strlen(ip_str), strlen(mac_str),
521 sizeof(bootargs));
522 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100523 }
Patrice Chotard6233de42019-11-25 09:07:39 +0100524
525 if (label->append)
526 strncpy(bootargs, label->append, sizeof(bootargs));
527
528 strcat(bootargs, ip_str);
529 strcat(bootargs, mac_str);
530
Simon Glassc7b03e82020-11-05 10:33:47 -0700531 cli_simple_process_macros(bootargs, finalbootargs,
532 sizeof(finalbootargs));
Patrice Chotard6233de42019-11-25 09:07:39 +0100533 env_set("bootargs", finalbootargs);
534 printf("append: %s\n", finalbootargs);
Patrice Chotard17e88042019-11-25 09:07:37 +0100535 }
536
Zhaofeng Libc14b932021-10-20 00:18:15 -0700537 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700538
Patrice Chotard17e88042019-11-25 09:07:37 +0100539 /* for FIT, append the configuration identifier */
540 if (label->config) {
Zhaofeng Libc14b932021-10-20 00:18:15 -0700541 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100542
543 fit_addr = malloc(len);
544 if (!fit_addr) {
545 printf("malloc fail (FIT address)\n");
546 return 1;
547 }
Zhaofeng Libc14b932021-10-20 00:18:15 -0700548 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
549 kernel_addr = fit_addr;
Patrice Chotard17e88042019-11-25 09:07:37 +0100550 }
551
552 /*
553 * fdt usage is optional:
Anton Leontievfeb7db82019-09-03 10:52:24 +0300554 * It handles the following scenarios.
Patrice Chotard17e88042019-11-25 09:07:37 +0100555 *
Anton Leontievfeb7db82019-09-03 10:52:24 +0300556 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
557 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
558 * bootm, and adjust argc appropriately.
559 *
560 * If retrieve fails and no exact fdt blob is specified in pxe file with
561 * "fdt" label, try Scenario 2.
Patrice Chotard17e88042019-11-25 09:07:37 +0100562 *
563 * Scenario 2: If there is an fdt_addr specified, pass it along to
564 * bootm, and adjust argc appropriately.
565 *
566 * Scenario 3: fdt blob is not available.
567 */
568 bootm_argv[3] = env_get("fdt_addr_r");
569
570 /* if fdt label is defined then get fdt from server */
571 if (bootm_argv[3]) {
572 char *fdtfile = NULL;
573 char *fdtfilefree = NULL;
574
575 if (label->fdt) {
576 fdtfile = label->fdt;
577 } else if (label->fdtdir) {
578 char *f1, *f2, *f3, *f4, *slash;
579
580 f1 = env_get("fdtfile");
581 if (f1) {
582 f2 = "";
583 f3 = "";
584 f4 = "";
585 } else {
586 /*
587 * For complex cases where this code doesn't
588 * generate the correct filename, the board
589 * code should set $fdtfile during early boot,
590 * or the boot scripts should set $fdtfile
591 * before invoking "pxe" or "sysboot".
592 */
593 f1 = env_get("soc");
594 f2 = "-";
595 f3 = env_get("board");
596 f4 = ".dtb";
Dimitri John Ledkov5ee984a2021-04-21 12:42:01 +0100597 if (!f1) {
598 f1 = "";
599 f2 = "";
600 }
601 if (!f3) {
602 f2 = "";
603 f3 = "";
604 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100605 }
606
607 len = strlen(label->fdtdir);
608 if (!len)
609 slash = "./";
610 else if (label->fdtdir[len - 1] != '/')
611 slash = "/";
612 else
613 slash = "";
614
615 len = strlen(label->fdtdir) + strlen(slash) +
616 strlen(f1) + strlen(f2) + strlen(f3) +
617 strlen(f4) + 1;
618 fdtfilefree = malloc(len);
619 if (!fdtfilefree) {
620 printf("malloc fail (FDT filename)\n");
621 goto cleanup;
622 }
623
624 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
625 label->fdtdir, slash, f1, f2, f3, f4);
626 fdtfile = fdtfilefree;
627 }
628
629 if (fdtfile) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100630 int err = get_relfile_envaddr(cmdtp, fdtfile,
631 "fdt_addr_r");
632
Patrice Chotard17e88042019-11-25 09:07:37 +0100633 free(fdtfilefree);
634 if (err < 0) {
Anton Leontievfeb7db82019-09-03 10:52:24 +0300635 bootm_argv[3] = NULL;
636
637 if (label->fdt) {
638 printf("Skipping %s for failure retrieving FDT\n",
639 label->name);
640 goto cleanup;
641 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100642 }
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100643
644#ifdef CONFIG_OF_LIBFDT_OVERLAY
645 if (label->fdtoverlays)
646 label_boot_fdtoverlay(cmdtp, label);
647#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100648 } else {
649 bootm_argv[3] = NULL;
650 }
651 }
652
Zhaofeng Libc14b932021-10-20 00:18:15 -0700653 bootm_argv[1] = kernel_addr;
654 zboot_argv[1] = kernel_addr;
655
656 if (initrd_addr_str) {
657 bootm_argv[2] = initrd_str;
658 bootm_argc = 3;
659
660 zboot_argv[3] = initrd_addr_str;
661 zboot_argv[4] = initrd_filesize;
662 zboot_argc = 5;
663 }
664
Patrice Chotard17e88042019-11-25 09:07:37 +0100665 if (!bootm_argv[3])
666 bootm_argv[3] = env_get("fdt_addr");
667
668 if (bootm_argv[3]) {
669 if (!bootm_argv[2])
670 bootm_argv[2] = "-";
671 bootm_argc = 4;
672 }
673
Zhaofeng Libc14b932021-10-20 00:18:15 -0700674 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
675 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard17e88042019-11-25 09:07:37 +0100676 /* Try bootm for legacy and FIT format image */
677 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
678 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100679 /* Try booting an AArch64 Linux kernel image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100680 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Patrice Chotard17e88042019-11-25 09:07:37 +0100681 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100682 /* Try booting a Image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100683 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Patrice Chotard17e88042019-11-25 09:07:37 +0100684 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincentb2187d02021-02-02 16:42:29 +0100685 /* Try booting an x86_64 Linux kernel image */
686 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700687 do_zboot_parent(cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentcaabd242021-02-02 16:42:28 +0100688
Patrice Chotard17e88042019-11-25 09:07:37 +0100689 unmap_sysmem(buf);
690
691cleanup:
692 if (fit_addr)
693 free(fit_addr);
694 return 1;
695}
696
697/*
698 * Tokens for the pxe file parser.
699 */
700enum token_type {
701 T_EOL,
702 T_STRING,
703 T_EOF,
704 T_MENU,
705 T_TITLE,
706 T_TIMEOUT,
707 T_LABEL,
708 T_KERNEL,
709 T_LINUX,
710 T_APPEND,
711 T_INITRD,
712 T_LOCALBOOT,
713 T_DEFAULT,
714 T_PROMPT,
715 T_INCLUDE,
716 T_FDT,
717 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100718 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100719 T_ONTIMEOUT,
720 T_IPAPPEND,
721 T_BACKGROUND,
722 T_INVALID
723};
724
725/*
726 * A token - given by a value and a type.
727 */
728struct token {
729 char *val;
730 enum token_type type;
731};
732
733/*
734 * Keywords recognized.
735 */
736static const struct token keywords[] = {
737 {"menu", T_MENU},
738 {"title", T_TITLE},
739 {"timeout", T_TIMEOUT},
740 {"default", T_DEFAULT},
741 {"prompt", T_PROMPT},
742 {"label", T_LABEL},
743 {"kernel", T_KERNEL},
744 {"linux", T_LINUX},
745 {"localboot", T_LOCALBOOT},
746 {"append", T_APPEND},
747 {"initrd", T_INITRD},
748 {"include", T_INCLUDE},
749 {"devicetree", T_FDT},
750 {"fdt", T_FDT},
751 {"devicetreedir", T_FDTDIR},
752 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100753 {"fdtoverlays", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100754 {"ontimeout", T_ONTIMEOUT,},
755 {"ipappend", T_IPAPPEND,},
756 {"background", T_BACKGROUND,},
757 {NULL, T_INVALID}
758};
759
760/*
761 * Since pxe(linux) files don't have a token to identify the start of a
762 * literal, we have to keep track of when we're in a state where a literal is
763 * expected vs when we're in a state a keyword is expected.
764 */
765enum lex_state {
766 L_NORMAL = 0,
767 L_KEYWORD,
768 L_SLITERAL
769};
770
771/*
772 * get_string retrieves a string from *p and stores it as a token in
773 * *t.
774 *
775 * get_string used for scanning both string literals and keywords.
776 *
777 * Characters from *p are copied into t-val until a character equal to
778 * delim is found, or a NUL byte is reached. If delim has the special value of
779 * ' ', any whitespace character will be used as a delimiter.
780 *
781 * If lower is unequal to 0, uppercase characters will be converted to
782 * lowercase in the result. This is useful to make keywords case
783 * insensitive.
784 *
785 * The location of *p is updated to point to the first character after the end
786 * of the token - the ending delimiter.
787 *
788 * On success, the new value of t->val is returned. Memory for t->val is
789 * allocated using malloc and must be free()'d to reclaim it. If insufficient
790 * memory is available, NULL is returned.
791 */
792static char *get_string(char **p, struct token *t, char delim, int lower)
793{
794 char *b, *e;
795 size_t len, i;
796
797 /*
798 * b and e both start at the beginning of the input stream.
799 *
800 * e is incremented until we find the ending delimiter, or a NUL byte
801 * is reached. Then, we take e - b to find the length of the token.
802 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100803 b = *p;
804 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100805
806 while (*e) {
807 if ((delim == ' ' && isspace(*e)) || delim == *e)
808 break;
809 e++;
810 }
811
812 len = e - b;
813
814 /*
815 * Allocate memory to hold the string, and copy it in, converting
816 * characters to lowercase if lower is != 0.
817 */
818 t->val = malloc(len + 1);
819 if (!t->val)
820 return NULL;
821
822 for (i = 0; i < len; i++, b++) {
823 if (lower)
824 t->val[i] = tolower(*b);
825 else
826 t->val[i] = *b;
827 }
828
829 t->val[len] = '\0';
830
831 /*
832 * Update *p so the caller knows where to continue scanning.
833 */
834 *p = e;
835
836 t->type = T_STRING;
837
838 return t->val;
839}
840
841/*
842 * Populate a keyword token with a type and value.
843 */
844static void get_keyword(struct token *t)
845{
846 int i;
847
848 for (i = 0; keywords[i].val; i++) {
849 if (!strcmp(t->val, keywords[i].val)) {
850 t->type = keywords[i].type;
851 break;
852 }
853 }
854}
855
856/*
857 * Get the next token. We have to keep track of which state we're in to know
858 * if we're looking to get a string literal or a keyword.
859 *
860 * *p is updated to point at the first character after the current token.
861 */
862static void get_token(char **p, struct token *t, enum lex_state state)
863{
864 char *c = *p;
865
866 t->type = T_INVALID;
867
868 /* eat non EOL whitespace */
869 while (isblank(*c))
870 c++;
871
872 /*
873 * eat comments. note that string literals can't begin with #, but
874 * can contain a # after their first character.
875 */
876 if (*c == '#') {
877 while (*c && *c != '\n')
878 c++;
879 }
880
881 if (*c == '\n') {
882 t->type = T_EOL;
883 c++;
884 } else if (*c == '\0') {
885 t->type = T_EOF;
886 c++;
887 } else if (state == L_SLITERAL) {
888 get_string(&c, t, '\n', 0);
889 } else if (state == L_KEYWORD) {
890 /*
891 * when we expect a keyword, we first get the next string
892 * token delimited by whitespace, and then check if it
893 * matches a keyword in our keyword list. if it does, it's
894 * converted to a keyword token of the appropriate type, and
895 * if not, it remains a string token.
896 */
897 get_string(&c, t, ' ', 1);
898 get_keyword(t);
899 }
900
901 *p = c;
902}
903
904/*
905 * Increment *c until we get to the end of the current line, or EOF.
906 */
907static void eol_or_eof(char **c)
908{
909 while (**c && **c != '\n')
910 (*c)++;
911}
912
913/*
914 * All of these parse_* functions share some common behavior.
915 *
916 * They finish with *c pointing after the token they parse, and return 1 on
917 * success, or < 0 on error.
918 */
919
920/*
921 * Parse a string literal and store a pointer it at *dst. String literals
922 * terminate at the end of the line.
923 */
924static int parse_sliteral(char **c, char **dst)
925{
926 struct token t;
927 char *s = *c;
928
929 get_token(c, &t, L_SLITERAL);
930
931 if (t.type != T_STRING) {
932 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
933 return -EINVAL;
934 }
935
936 *dst = t.val;
937
938 return 1;
939}
940
941/*
942 * Parse a base 10 (unsigned) integer and store it at *dst.
943 */
944static int parse_integer(char **c, int *dst)
945{
946 struct token t;
947 char *s = *c;
948
949 get_token(c, &t, L_SLITERAL);
950
951 if (t.type != T_STRING) {
952 printf("Expected string: %.*s\n", (int)(*c - s), s);
953 return -EINVAL;
954 }
955
956 *dst = simple_strtol(t.val, NULL, 10);
957
958 free(t.val);
959
960 return 1;
961}
962
Simon Glassed38aef2020-05-10 11:40:03 -0600963static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100964 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +0100965
966/*
967 * Parse an include statement, and retrieve and parse the file it mentions.
968 *
969 * base should point to a location where it's safe to store the file, and
970 * nest_level should indicate how many nested includes have occurred. For this
971 * include, nest_level has already been incremented and doesn't need to be
972 * incremented here.
973 */
Simon Glassed38aef2020-05-10 11:40:03 -0600974static int handle_include(struct cmd_tbl *cmdtp, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100975 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +0100976{
977 char *include_path;
978 char *s = *c;
979 int err;
980 char *buf;
981 int ret;
982
983 err = parse_sliteral(c, &include_path);
984
985 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100986 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +0100987 return err;
988 }
989
990 err = get_pxe_file(cmdtp, include_path, base);
991
992 if (err < 0) {
993 printf("Couldn't retrieve %s\n", include_path);
994 return err;
995 }
996
997 buf = map_sysmem(base, 0);
998 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
999 unmap_sysmem(buf);
1000
1001 return ret;
1002}
1003
1004/*
1005 * Parse lines that begin with 'menu'.
1006 *
1007 * base and nest are provided to handle the 'menu include' case.
1008 *
1009 * base should point to a location where it's safe to store the included file.
1010 *
1011 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1012 * a file it includes, 3 when parsing a file included by that file, and so on.
1013 */
Simon Glassed38aef2020-05-10 11:40:03 -06001014static int parse_menu(struct cmd_tbl *cmdtp, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001015 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001016{
1017 struct token t;
1018 char *s = *c;
1019 int err = 0;
1020
1021 get_token(c, &t, L_KEYWORD);
1022
1023 switch (t.type) {
1024 case T_TITLE:
1025 err = parse_sliteral(c, &cfg->title);
1026
1027 break;
1028
1029 case T_INCLUDE:
Patrice Chotard6233de42019-11-25 09:07:39 +01001030 err = handle_include(cmdtp, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001031 break;
1032
1033 case T_BACKGROUND:
1034 err = parse_sliteral(c, &cfg->bmp);
1035 break;
1036
1037 default:
1038 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001039 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001040 }
1041
1042 if (err < 0)
1043 return err;
1044
1045 eol_or_eof(c);
1046
1047 return 1;
1048}
1049
1050/*
1051 * Handles parsing a 'menu line' when we're parsing a label.
1052 */
1053static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001054 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001055{
1056 struct token t;
1057 char *s;
1058
1059 s = *c;
1060
1061 get_token(c, &t, L_KEYWORD);
1062
1063 switch (t.type) {
1064 case T_DEFAULT:
1065 if (!cfg->default_label)
1066 cfg->default_label = strdup(label->name);
1067
1068 if (!cfg->default_label)
1069 return -ENOMEM;
1070
1071 break;
1072 case T_LABEL:
1073 parse_sliteral(c, &label->menu);
1074 break;
1075 default:
1076 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001077 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001078 }
1079
1080 eol_or_eof(c);
1081
1082 return 0;
1083}
1084
1085/*
1086 * Handles parsing a 'kernel' label.
1087 * expecting "filename" or "<fit_filename>#cfg"
1088 */
1089static int parse_label_kernel(char **c, struct pxe_label *label)
1090{
1091 char *s;
1092 int err;
1093
1094 err = parse_sliteral(c, &label->kernel);
1095 if (err < 0)
1096 return err;
1097
1098 s = strstr(label->kernel, "#");
1099 if (!s)
1100 return 1;
1101
1102 label->config = malloc(strlen(s) + 1);
1103 if (!label->config)
1104 return -ENOMEM;
1105
1106 strcpy(label->config, s);
1107 *s = 0;
1108
1109 return 1;
1110}
1111
1112/*
1113 * Parses a label and adds it to the list of labels for a menu.
1114 *
1115 * A label ends when we either get to the end of a file, or
1116 * get some input we otherwise don't have a handler defined
1117 * for.
1118 *
1119 */
1120static int parse_label(char **c, struct pxe_menu *cfg)
1121{
1122 struct token t;
1123 int len;
1124 char *s = *c;
1125 struct pxe_label *label;
1126 int err;
1127
1128 label = label_create();
1129 if (!label)
1130 return -ENOMEM;
1131
1132 err = parse_sliteral(c, &label->name);
1133 if (err < 0) {
1134 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1135 label_destroy(label);
1136 return -EINVAL;
1137 }
1138
1139 list_add_tail(&label->list, &cfg->labels);
1140
1141 while (1) {
1142 s = *c;
1143 get_token(c, &t, L_KEYWORD);
1144
1145 err = 0;
1146 switch (t.type) {
1147 case T_MENU:
1148 err = parse_label_menu(c, cfg, label);
1149 break;
1150
1151 case T_KERNEL:
1152 case T_LINUX:
1153 err = parse_label_kernel(c, label);
1154 break;
1155
1156 case T_APPEND:
1157 err = parse_sliteral(c, &label->append);
1158 if (label->initrd)
1159 break;
1160 s = strstr(label->append, "initrd=");
1161 if (!s)
1162 break;
1163 s += 7;
1164 len = (int)(strchr(s, ' ') - s);
1165 label->initrd = malloc(len + 1);
1166 strncpy(label->initrd, s, len);
1167 label->initrd[len] = '\0';
1168
1169 break;
1170
1171 case T_INITRD:
1172 if (!label->initrd)
1173 err = parse_sliteral(c, &label->initrd);
1174 break;
1175
1176 case T_FDT:
1177 if (!label->fdt)
1178 err = parse_sliteral(c, &label->fdt);
1179 break;
1180
1181 case T_FDTDIR:
1182 if (!label->fdtdir)
1183 err = parse_sliteral(c, &label->fdtdir);
1184 break;
1185
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001186 case T_FDTOVERLAYS:
1187 if (!label->fdtoverlays)
1188 err = parse_sliteral(c, &label->fdtoverlays);
1189 break;
1190
Patrice Chotard17e88042019-11-25 09:07:37 +01001191 case T_LOCALBOOT:
1192 label->localboot = 1;
1193 err = parse_integer(c, &label->localboot_val);
1194 break;
1195
1196 case T_IPAPPEND:
1197 err = parse_integer(c, &label->ipappend);
1198 break;
1199
1200 case T_EOL:
1201 break;
1202
1203 default:
1204 /*
1205 * put the token back! we don't want it - it's the end
1206 * of a label and whatever token this is, it's
1207 * something for the menu level context to handle.
1208 */
1209 *c = s;
1210 return 1;
1211 }
1212
1213 if (err < 0)
1214 return err;
1215 }
1216}
1217
1218/*
1219 * This 16 comes from the limit pxelinux imposes on nested includes.
1220 *
1221 * There is no reason at all we couldn't do more, but some limit helps prevent
1222 * infinite (until crash occurs) recursion if a file tries to include itself.
1223 */
1224#define MAX_NEST_LEVEL 16
1225
1226/*
1227 * Entry point for parsing a menu file. nest_level indicates how many times
1228 * we've nested in includes. It will be 1 for the top level menu file.
1229 *
1230 * Returns 1 on success, < 0 on error.
1231 */
Simon Glassed38aef2020-05-10 11:40:03 -06001232static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001233 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001234{
1235 struct token t;
1236 char *s, *b, *label_name;
1237 int err;
1238
1239 b = p;
1240
1241 if (nest_level > MAX_NEST_LEVEL) {
1242 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1243 return -EMLINK;
1244 }
1245
1246 while (1) {
1247 s = p;
1248
1249 get_token(&p, &t, L_KEYWORD);
1250
1251 err = 0;
1252 switch (t.type) {
1253 case T_MENU:
1254 cfg->prompt = 1;
1255 err = parse_menu(cmdtp, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001256 base + ALIGN(strlen(b) + 1, 4),
1257 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001258 break;
1259
1260 case T_TIMEOUT:
1261 err = parse_integer(&p, &cfg->timeout);
1262 break;
1263
1264 case T_LABEL:
1265 err = parse_label(&p, cfg);
1266 break;
1267
1268 case T_DEFAULT:
1269 case T_ONTIMEOUT:
1270 err = parse_sliteral(&p, &label_name);
1271
1272 if (label_name) {
1273 if (cfg->default_label)
1274 free(cfg->default_label);
1275
1276 cfg->default_label = label_name;
1277 }
1278
1279 break;
1280
1281 case T_INCLUDE:
1282 err = handle_include(cmdtp, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001283 base + ALIGN(strlen(b), 4), cfg,
1284 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001285 break;
1286
1287 case T_PROMPT:
1288 eol_or_eof(&p);
1289 break;
1290
1291 case T_EOL:
1292 break;
1293
1294 case T_EOF:
1295 return 1;
1296
1297 default:
1298 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001299 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001300 eol_or_eof(&p);
1301 }
1302
1303 if (err < 0)
1304 return err;
1305 }
1306}
1307
1308/*
1309 * Free the memory used by a pxe_menu and its labels.
1310 */
1311void destroy_pxe_menu(struct pxe_menu *cfg)
1312{
1313 struct list_head *pos, *n;
1314 struct pxe_label *label;
1315
1316 if (cfg->title)
1317 free(cfg->title);
1318
1319 if (cfg->default_label)
1320 free(cfg->default_label);
1321
1322 list_for_each_safe(pos, n, &cfg->labels) {
1323 label = list_entry(pos, struct pxe_label, list);
1324
1325 label_destroy(label);
1326 }
1327
1328 free(cfg);
1329}
1330
1331/*
1332 * Entry point for parsing a pxe file. This is only used for the top level
1333 * file.
1334 *
1335 * Returns NULL if there is an error, otherwise, returns a pointer to a
1336 * pxe_menu struct populated with the results of parsing the pxe file (and any
1337 * files it includes). The resulting pxe_menu struct can be free()'d by using
1338 * the destroy_pxe_menu() function.
1339 */
Simon Glassed38aef2020-05-10 11:40:03 -06001340struct pxe_menu *parse_pxefile(struct cmd_tbl *cmdtp, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001341{
1342 struct pxe_menu *cfg;
1343 char *buf;
1344 int r;
1345
1346 cfg = malloc(sizeof(struct pxe_menu));
1347
1348 if (!cfg)
1349 return NULL;
1350
1351 memset(cfg, 0, sizeof(struct pxe_menu));
1352
1353 INIT_LIST_HEAD(&cfg->labels);
1354
1355 buf = map_sysmem(menucfg, 0);
1356 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1357 unmap_sysmem(buf);
1358
1359 if (r < 0) {
1360 destroy_pxe_menu(cfg);
1361 return NULL;
1362 }
1363
1364 return cfg;
1365}
1366
1367/*
1368 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1369 * menu code.
1370 */
1371static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1372{
1373 struct pxe_label *label;
1374 struct list_head *pos;
1375 struct menu *m;
1376 int err;
1377 int i = 1;
1378 char *default_num = NULL;
1379
1380 /*
1381 * Create a menu and add items for all the labels.
1382 */
1383 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -07001384 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001385
1386 if (!m)
1387 return NULL;
1388
1389 list_for_each(pos, &cfg->labels) {
1390 label = list_entry(pos, struct pxe_label, list);
1391
1392 sprintf(label->num, "%d", i++);
1393 if (menu_item_add(m, label->num, label) != 1) {
1394 menu_destroy(m);
1395 return NULL;
1396 }
1397 if (cfg->default_label &&
1398 (strcmp(label->name, cfg->default_label) == 0))
1399 default_num = label->num;
Patrice Chotard17e88042019-11-25 09:07:37 +01001400 }
1401
1402 /*
1403 * After we've created items for each label in the menu, set the
1404 * menu's default label if one was specified.
1405 */
1406 if (default_num) {
1407 err = menu_default_set(m, default_num);
1408 if (err != 1) {
1409 if (err != -ENOENT) {
1410 menu_destroy(m);
1411 return NULL;
1412 }
1413
1414 printf("Missing default: %s\n", cfg->default_label);
1415 }
1416 }
1417
1418 return m;
1419}
1420
1421/*
1422 * Try to boot any labels we have yet to attempt to boot.
1423 */
Simon Glassed38aef2020-05-10 11:40:03 -06001424static void boot_unattempted_labels(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001425{
1426 struct list_head *pos;
1427 struct pxe_label *label;
1428
1429 list_for_each(pos, &cfg->labels) {
1430 label = list_entry(pos, struct pxe_label, list);
1431
1432 if (!label->attempted)
1433 label_boot(cmdtp, label);
1434 }
1435}
1436
1437/*
1438 * Boot the system as prescribed by a pxe_menu.
1439 *
1440 * Use the menu system to either get the user's choice or the default, based
1441 * on config or user input. If there is no default or user's choice,
1442 * attempted to boot labels in the order they were given in pxe files.
1443 * If the default or user's choice fails to boot, attempt to boot other
1444 * labels in the order they were given in pxe files.
1445 *
1446 * If this function returns, there weren't any labels that successfully
1447 * booted, or the user interrupted the menu selection via ctrl+c.
1448 */
Simon Glassed38aef2020-05-10 11:40:03 -06001449void handle_pxe_menu(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001450{
1451 void *choice;
1452 struct menu *m;
1453 int err;
1454
Kory Maincentcaabd242021-02-02 16:42:28 +01001455 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1456 /* display BMP if available */
1457 if (cfg->bmp) {
1458 if (get_relfile(cmdtp, cfg->bmp, image_load_addr)) {
1459 if (CONFIG_IS_ENABLED(CMD_CLS))
1460 run_command("cls", 0);
1461 bmp_display(image_load_addr,
1462 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1463 } else {
1464 printf("Skipping background bmp %s for failure\n",
1465 cfg->bmp);
1466 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001467 }
1468 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001469
1470 m = pxe_menu_to_menu(cfg);
1471 if (!m)
1472 return;
1473
1474 err = menu_get_choice(m, &choice);
1475
1476 menu_destroy(m);
1477
1478 /*
1479 * err == 1 means we got a choice back from menu_get_choice.
1480 *
1481 * err == -ENOENT if the menu was setup to select the default but no
1482 * default was set. in that case, we should continue trying to boot
1483 * labels that haven't been attempted yet.
1484 *
1485 * otherwise, the user interrupted or there was some other error and
1486 * we give up.
1487 */
1488
1489 if (err == 1) {
1490 err = label_boot(cmdtp, choice);
1491 if (!err)
1492 return;
1493 } else if (err != -ENOENT) {
1494 return;
1495 }
1496
1497 boot_unattempted_labels(cmdtp, cfg);
1498}