blob: 9983c86a11125bfe58c6f67128ef798dc8ecaa1c [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
Patrice Chotard2373cba2019-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 Chotard8cb22a62019-11-25 09:07:39 +010077 if (!last_slash)
Patrice Chotard2373cba2019-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 Chotard8cb22a62019-11-25 09:07:39 +010084 bootfile_path_size, path_len);
Patrice Chotard2373cba2019-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 Glass09140112020-05-10 11:40:03 -060097int (*do_getfile)(struct cmd_tbl *cmdtp, const char *file_path,
98 char *file_addr);
Patrice Chotard2373cba2019-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 Glassfd3fa5c2021-10-14 12:47:56 -0600108static int get_relfile(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100109 unsigned long file_addr)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100110{
111 size_t path_len;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100112 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-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 Chotard8cb22a62019-11-25 09:07:39 +0100125 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard2373cba2019-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
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600136 return do_getfile(ctx->cmdtp, relfile, addr_buf);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100137}
138
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600139int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100140 unsigned long file_addr)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100141{
142 unsigned long config_file_size;
143 char *tftp_filesize;
144 int err;
145 char *buf;
146
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600147 err = get_relfile(ctx, file_path, file_addr);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100148
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 Glassfd3fa5c2021-10-14 12:47:56 -0600173int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100174 unsigned long pxefile_addr_r)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100175{
176 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100177 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-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 Chotard8cb22a62019-11-25 09:07:39 +0100181 PXELINUX_DIR, file);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100182 return -ENAMETOOLONG;
183 }
184
185 sprintf(path, PXELINUX_DIR "%s", file);
186
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600187 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100188}
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 Glassfd3fa5c2021-10-14 12:47:56 -0600197static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100198 const char *envaddr_name)
Patrice Chotard2373cba2019-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
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600211 return get_relfile(ctx, file_path, file_addr);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100212}
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 Armstrong69076df2021-01-20 09:54:53 +0100265 if (label->fdtoverlays)
266 free(label->fdtoverlays);
267
Patrice Chotard2373cba2019-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 Glass1a62d642020-11-05 10:33:47 -0700306 cli_simple_process_macros(label->append, bootargs,
307 sizeof(bootargs));
Patrice Chotard2373cba2019-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}
315
316/*
Neil Armstrong69076df2021-01-20 09:54:53 +0100317 * Loads fdt overlays specified in 'fdtoverlays'.
318 */
319#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600320static void label_boot_fdtoverlay(struct pxe_context *ctx,
321 struct pxe_label *label)
Neil Armstrong69076df2021-01-20 09:54:53 +0100322{
323 char *fdtoverlay = label->fdtoverlays;
324 struct fdt_header *working_fdt;
325 char *fdtoverlay_addr_env;
326 ulong fdtoverlay_addr;
327 ulong fdt_addr;
328 int err;
329
330 /* Get the main fdt and map it */
Simon Glass7e5f4602021-07-24 09:03:29 -0600331 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100332 working_fdt = map_sysmem(fdt_addr, 0);
333 err = fdt_check_header(working_fdt);
334 if (err)
335 return;
336
337 /* Get the specific overlay loading address */
338 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
339 if (!fdtoverlay_addr_env) {
340 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
341 return;
342 }
343
Simon Glass7e5f4602021-07-24 09:03:29 -0600344 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100345
346 /* Cycle over the overlay files and apply them in order */
347 do {
348 struct fdt_header *blob;
349 char *overlayfile;
350 char *end;
351 int len;
352
353 /* Drop leading spaces */
354 while (*fdtoverlay == ' ')
355 ++fdtoverlay;
356
357 /* Copy a single filename if multiple provided */
358 end = strstr(fdtoverlay, " ");
359 if (end) {
360 len = (int)(end - fdtoverlay);
361 overlayfile = malloc(len + 1);
362 strncpy(overlayfile, fdtoverlay, len);
363 overlayfile[len] = '\0';
364 } else
365 overlayfile = fdtoverlay;
366
367 if (!strlen(overlayfile))
368 goto skip_overlay;
369
370 /* Load overlay file */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600371 err = get_relfile_envaddr(ctx, overlayfile,
Neil Armstrong69076df2021-01-20 09:54:53 +0100372 "fdtoverlay_addr_r");
373 if (err < 0) {
374 printf("Failed loading overlay %s\n", overlayfile);
375 goto skip_overlay;
376 }
377
378 /* Resize main fdt */
379 fdt_shrink_to_minimum(working_fdt, 8192);
380
381 blob = map_sysmem(fdtoverlay_addr, 0);
382 err = fdt_check_header(blob);
383 if (err) {
384 printf("Invalid overlay %s, skipping\n",
385 overlayfile);
386 goto skip_overlay;
387 }
388
389 err = fdt_overlay_apply_verbose(working_fdt, blob);
390 if (err) {
391 printf("Failed to apply overlay %s, skipping\n",
392 overlayfile);
393 goto skip_overlay;
394 }
395
396skip_overlay:
397 if (end)
398 free(overlayfile);
399 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
400}
401#endif
402
403/*
Patrice Chotard2373cba2019-11-25 09:07:37 +0100404 * Boot according to the contents of a pxe_label.
405 *
406 * If we can't boot for any reason, we return. A successful boot never
407 * returns.
408 *
409 * The kernel will be stored in the location given by the 'kernel_addr_r'
410 * environment variable.
411 *
412 * If the label specifies an initrd file, it will be stored in the location
413 * given by the 'ramdisk_addr_r' environment variable.
414 *
415 * If the label specifies an 'append' line, its contents will overwrite that
416 * of the 'bootargs' environment variable.
417 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600418static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100419{
420 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700421 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700422 char *kernel_addr = NULL;
423 char *initrd_addr_str = NULL;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700424 char initrd_filesize[10];
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700425 char initrd_str[28];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100426 char mac_str[29] = "";
427 char ip_str[68] = "";
428 char *fit_addr = NULL;
429 int bootm_argc = 2;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700430 int zboot_argc = 3;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100431 int len = 0;
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700432 ulong kernel_addr_r;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100433 void *buf;
434
435 label_print(label);
436
437 label->attempted = 1;
438
439 if (label->localboot) {
440 if (label->localboot_val >= 0)
441 label_localboot(label);
442 return 0;
443 }
444
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100445 if (!label->kernel) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100446 printf("No kernel given, skipping %s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100447 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100448 return 1;
449 }
450
451 if (label->initrd) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600452 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100453 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100454 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100455 return 1;
456 }
457
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700458 initrd_addr_str = env_get("ramdisk_addr_r");
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700459 strncpy(initrd_filesize, env_get("filesize"), 9);
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700460
461 strncpy(initrd_str, initrd_addr_str, 18);
462 strcat(initrd_str, ":");
463 strncat(initrd_str, initrd_filesize, 9);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100464 }
465
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600466 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100467 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100468 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100469 return 1;
470 }
471
472 if (label->ipappend & 0x1) {
473 sprintf(ip_str, " ip=%s:%s:%s:%s",
474 env_get("ipaddr"), env_get("serverip"),
475 env_get("gatewayip"), env_get("netmask"));
476 }
477
Kory Maincentff0287e2021-02-02 16:42:28 +0100478 if (IS_ENABLED(CONFIG_CMD_NET)) {
479 if (label->ipappend & 0x2) {
480 int err;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100481
Kory Maincentff0287e2021-02-02 16:42:28 +0100482 strcpy(mac_str, " BOOTIF=");
483 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
484 if (err < 0)
485 mac_str[0] = '\0';
486 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100487 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100488
489 if ((label->ipappend & 0x3) || label->append) {
490 char bootargs[CONFIG_SYS_CBSIZE] = "";
491 char finalbootargs[CONFIG_SYS_CBSIZE];
492
493 if (strlen(label->append ?: "") +
494 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
495 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
496 strlen(label->append ?: ""),
497 strlen(ip_str), strlen(mac_str),
498 sizeof(bootargs));
499 return 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100500 }
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100501
502 if (label->append)
503 strncpy(bootargs, label->append, sizeof(bootargs));
504
505 strcat(bootargs, ip_str);
506 strcat(bootargs, mac_str);
507
Simon Glass1a62d642020-11-05 10:33:47 -0700508 cli_simple_process_macros(bootargs, finalbootargs,
509 sizeof(finalbootargs));
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100510 env_set("bootargs", finalbootargs);
511 printf("append: %s\n", finalbootargs);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100512 }
513
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700514 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700515
Patrice Chotard2373cba2019-11-25 09:07:37 +0100516 /* for FIT, append the configuration identifier */
517 if (label->config) {
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700518 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100519
520 fit_addr = malloc(len);
521 if (!fit_addr) {
522 printf("malloc fail (FIT address)\n");
523 return 1;
524 }
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700525 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
526 kernel_addr = fit_addr;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100527 }
528
529 /*
530 * fdt usage is optional:
Anton Leontievdb366742019-09-03 10:52:24 +0300531 * It handles the following scenarios.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100532 *
Anton Leontievdb366742019-09-03 10:52:24 +0300533 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
534 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
535 * bootm, and adjust argc appropriately.
536 *
537 * If retrieve fails and no exact fdt blob is specified in pxe file with
538 * "fdt" label, try Scenario 2.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100539 *
540 * Scenario 2: If there is an fdt_addr specified, pass it along to
541 * bootm, and adjust argc appropriately.
542 *
543 * Scenario 3: fdt blob is not available.
544 */
545 bootm_argv[3] = env_get("fdt_addr_r");
546
547 /* if fdt label is defined then get fdt from server */
548 if (bootm_argv[3]) {
549 char *fdtfile = NULL;
550 char *fdtfilefree = NULL;
551
552 if (label->fdt) {
553 fdtfile = label->fdt;
554 } else if (label->fdtdir) {
555 char *f1, *f2, *f3, *f4, *slash;
556
557 f1 = env_get("fdtfile");
558 if (f1) {
559 f2 = "";
560 f3 = "";
561 f4 = "";
562 } else {
563 /*
564 * For complex cases where this code doesn't
565 * generate the correct filename, the board
566 * code should set $fdtfile during early boot,
567 * or the boot scripts should set $fdtfile
568 * before invoking "pxe" or "sysboot".
569 */
570 f1 = env_get("soc");
571 f2 = "-";
572 f3 = env_get("board");
573 f4 = ".dtb";
Dimitri John Ledkov75efe7d2021-04-21 12:42:01 +0100574 if (!f1) {
575 f1 = "";
576 f2 = "";
577 }
578 if (!f3) {
579 f2 = "";
580 f3 = "";
581 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100582 }
583
584 len = strlen(label->fdtdir);
585 if (!len)
586 slash = "./";
587 else if (label->fdtdir[len - 1] != '/')
588 slash = "/";
589 else
590 slash = "";
591
592 len = strlen(label->fdtdir) + strlen(slash) +
593 strlen(f1) + strlen(f2) + strlen(f3) +
594 strlen(f4) + 1;
595 fdtfilefree = malloc(len);
596 if (!fdtfilefree) {
597 printf("malloc fail (FDT filename)\n");
598 goto cleanup;
599 }
600
601 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
602 label->fdtdir, slash, f1, f2, f3, f4);
603 fdtfile = fdtfilefree;
604 }
605
606 if (fdtfile) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600607 int err = get_relfile_envaddr(ctx, fdtfile,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100608 "fdt_addr_r");
609
Patrice Chotard2373cba2019-11-25 09:07:37 +0100610 free(fdtfilefree);
611 if (err < 0) {
Anton Leontievdb366742019-09-03 10:52:24 +0300612 bootm_argv[3] = NULL;
613
614 if (label->fdt) {
615 printf("Skipping %s for failure retrieving FDT\n",
616 label->name);
617 goto cleanup;
618 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100619 }
Neil Armstrong69076df2021-01-20 09:54:53 +0100620
621#ifdef CONFIG_OF_LIBFDT_OVERLAY
622 if (label->fdtoverlays)
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600623 label_boot_fdtoverlay(ctx, label);
Neil Armstrong69076df2021-01-20 09:54:53 +0100624#endif
Patrice Chotard2373cba2019-11-25 09:07:37 +0100625 } else {
626 bootm_argv[3] = NULL;
627 }
628 }
629
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700630 bootm_argv[1] = kernel_addr;
631 zboot_argv[1] = kernel_addr;
632
633 if (initrd_addr_str) {
634 bootm_argv[2] = initrd_str;
635 bootm_argc = 3;
636
637 zboot_argv[3] = initrd_addr_str;
638 zboot_argv[4] = initrd_filesize;
639 zboot_argc = 5;
640 }
641
Patrice Chotard2373cba2019-11-25 09:07:37 +0100642 if (!bootm_argv[3])
643 bootm_argv[3] = env_get("fdt_addr");
644
645 if (bootm_argv[3]) {
646 if (!bootm_argv[2])
647 bootm_argv[2] = "-";
648 bootm_argc = 4;
649 }
650
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700651 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
652 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100653 /* Try bootm for legacy and FIT format image */
654 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600655 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100656 /* Try booting an AArch64 Linux kernel image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100657 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600658 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100659 /* Try booting a Image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100660 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600661 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincent18c25822021-02-02 16:42:29 +0100662 /* Try booting an x86_64 Linux kernel image */
663 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600664 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentff0287e2021-02-02 16:42:28 +0100665
Patrice Chotard2373cba2019-11-25 09:07:37 +0100666 unmap_sysmem(buf);
667
668cleanup:
669 if (fit_addr)
670 free(fit_addr);
671 return 1;
672}
673
674/*
675 * Tokens for the pxe file parser.
676 */
677enum token_type {
678 T_EOL,
679 T_STRING,
680 T_EOF,
681 T_MENU,
682 T_TITLE,
683 T_TIMEOUT,
684 T_LABEL,
685 T_KERNEL,
686 T_LINUX,
687 T_APPEND,
688 T_INITRD,
689 T_LOCALBOOT,
690 T_DEFAULT,
691 T_PROMPT,
692 T_INCLUDE,
693 T_FDT,
694 T_FDTDIR,
Neil Armstrong69076df2021-01-20 09:54:53 +0100695 T_FDTOVERLAYS,
Patrice Chotard2373cba2019-11-25 09:07:37 +0100696 T_ONTIMEOUT,
697 T_IPAPPEND,
698 T_BACKGROUND,
699 T_INVALID
700};
701
702/*
703 * A token - given by a value and a type.
704 */
705struct token {
706 char *val;
707 enum token_type type;
708};
709
710/*
711 * Keywords recognized.
712 */
713static const struct token keywords[] = {
714 {"menu", T_MENU},
715 {"title", T_TITLE},
716 {"timeout", T_TIMEOUT},
717 {"default", T_DEFAULT},
718 {"prompt", T_PROMPT},
719 {"label", T_LABEL},
720 {"kernel", T_KERNEL},
721 {"linux", T_LINUX},
722 {"localboot", T_LOCALBOOT},
723 {"append", T_APPEND},
724 {"initrd", T_INITRD},
725 {"include", T_INCLUDE},
726 {"devicetree", T_FDT},
727 {"fdt", T_FDT},
728 {"devicetreedir", T_FDTDIR},
729 {"fdtdir", T_FDTDIR},
Neil Armstrong69076df2021-01-20 09:54:53 +0100730 {"fdtoverlays", T_FDTOVERLAYS},
Patrice Chotard2373cba2019-11-25 09:07:37 +0100731 {"ontimeout", T_ONTIMEOUT,},
732 {"ipappend", T_IPAPPEND,},
733 {"background", T_BACKGROUND,},
734 {NULL, T_INVALID}
735};
736
737/*
738 * Since pxe(linux) files don't have a token to identify the start of a
739 * literal, we have to keep track of when we're in a state where a literal is
740 * expected vs when we're in a state a keyword is expected.
741 */
742enum lex_state {
743 L_NORMAL = 0,
744 L_KEYWORD,
745 L_SLITERAL
746};
747
748/*
749 * get_string retrieves a string from *p and stores it as a token in
750 * *t.
751 *
752 * get_string used for scanning both string literals and keywords.
753 *
754 * Characters from *p are copied into t-val until a character equal to
755 * delim is found, or a NUL byte is reached. If delim has the special value of
756 * ' ', any whitespace character will be used as a delimiter.
757 *
758 * If lower is unequal to 0, uppercase characters will be converted to
759 * lowercase in the result. This is useful to make keywords case
760 * insensitive.
761 *
762 * The location of *p is updated to point to the first character after the end
763 * of the token - the ending delimiter.
764 *
765 * On success, the new value of t->val is returned. Memory for t->val is
766 * allocated using malloc and must be free()'d to reclaim it. If insufficient
767 * memory is available, NULL is returned.
768 */
769static char *get_string(char **p, struct token *t, char delim, int lower)
770{
771 char *b, *e;
772 size_t len, i;
773
774 /*
775 * b and e both start at the beginning of the input stream.
776 *
777 * e is incremented until we find the ending delimiter, or a NUL byte
778 * is reached. Then, we take e - b to find the length of the token.
779 */
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100780 b = *p;
781 e = *p;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100782
783 while (*e) {
784 if ((delim == ' ' && isspace(*e)) || delim == *e)
785 break;
786 e++;
787 }
788
789 len = e - b;
790
791 /*
792 * Allocate memory to hold the string, and copy it in, converting
793 * characters to lowercase if lower is != 0.
794 */
795 t->val = malloc(len + 1);
796 if (!t->val)
797 return NULL;
798
799 for (i = 0; i < len; i++, b++) {
800 if (lower)
801 t->val[i] = tolower(*b);
802 else
803 t->val[i] = *b;
804 }
805
806 t->val[len] = '\0';
807
808 /*
809 * Update *p so the caller knows where to continue scanning.
810 */
811 *p = e;
812
813 t->type = T_STRING;
814
815 return t->val;
816}
817
818/*
819 * Populate a keyword token with a type and value.
820 */
821static void get_keyword(struct token *t)
822{
823 int i;
824
825 for (i = 0; keywords[i].val; i++) {
826 if (!strcmp(t->val, keywords[i].val)) {
827 t->type = keywords[i].type;
828 break;
829 }
830 }
831}
832
833/*
834 * Get the next token. We have to keep track of which state we're in to know
835 * if we're looking to get a string literal or a keyword.
836 *
837 * *p is updated to point at the first character after the current token.
838 */
839static void get_token(char **p, struct token *t, enum lex_state state)
840{
841 char *c = *p;
842
843 t->type = T_INVALID;
844
845 /* eat non EOL whitespace */
846 while (isblank(*c))
847 c++;
848
849 /*
850 * eat comments. note that string literals can't begin with #, but
851 * can contain a # after their first character.
852 */
853 if (*c == '#') {
854 while (*c && *c != '\n')
855 c++;
856 }
857
858 if (*c == '\n') {
859 t->type = T_EOL;
860 c++;
861 } else if (*c == '\0') {
862 t->type = T_EOF;
863 c++;
864 } else if (state == L_SLITERAL) {
865 get_string(&c, t, '\n', 0);
866 } else if (state == L_KEYWORD) {
867 /*
868 * when we expect a keyword, we first get the next string
869 * token delimited by whitespace, and then check if it
870 * matches a keyword in our keyword list. if it does, it's
871 * converted to a keyword token of the appropriate type, and
872 * if not, it remains a string token.
873 */
874 get_string(&c, t, ' ', 1);
875 get_keyword(t);
876 }
877
878 *p = c;
879}
880
881/*
882 * Increment *c until we get to the end of the current line, or EOF.
883 */
884static void eol_or_eof(char **c)
885{
886 while (**c && **c != '\n')
887 (*c)++;
888}
889
890/*
891 * All of these parse_* functions share some common behavior.
892 *
893 * They finish with *c pointing after the token they parse, and return 1 on
894 * success, or < 0 on error.
895 */
896
897/*
898 * Parse a string literal and store a pointer it at *dst. String literals
899 * terminate at the end of the line.
900 */
901static int parse_sliteral(char **c, char **dst)
902{
903 struct token t;
904 char *s = *c;
905
906 get_token(c, &t, L_SLITERAL);
907
908 if (t.type != T_STRING) {
909 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
910 return -EINVAL;
911 }
912
913 *dst = t.val;
914
915 return 1;
916}
917
918/*
919 * Parse a base 10 (unsigned) integer and store it at *dst.
920 */
921static int parse_integer(char **c, int *dst)
922{
923 struct token t;
924 char *s = *c;
925
926 get_token(c, &t, L_SLITERAL);
927
928 if (t.type != T_STRING) {
929 printf("Expected string: %.*s\n", (int)(*c - s), s);
930 return -EINVAL;
931 }
932
933 *dst = simple_strtol(t.val, NULL, 10);
934
935 free(t.val);
936
937 return 1;
938}
939
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600940static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100941 struct pxe_menu *cfg, int nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100942
943/*
944 * Parse an include statement, and retrieve and parse the file it mentions.
945 *
946 * base should point to a location where it's safe to store the file, and
947 * nest_level should indicate how many nested includes have occurred. For this
948 * include, nest_level has already been incremented and doesn't need to be
949 * incremented here.
950 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600951static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100952 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100953{
954 char *include_path;
955 char *s = *c;
956 int err;
957 char *buf;
958 int ret;
959
960 err = parse_sliteral(c, &include_path);
961
962 if (err < 0) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100963 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100964 return err;
965 }
966
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600967 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100968
969 if (err < 0) {
970 printf("Couldn't retrieve %s\n", include_path);
971 return err;
972 }
973
974 buf = map_sysmem(base, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600975 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100976 unmap_sysmem(buf);
977
978 return ret;
979}
980
981/*
982 * Parse lines that begin with 'menu'.
983 *
984 * base and nest are provided to handle the 'menu include' case.
985 *
986 * base should point to a location where it's safe to store the included file.
987 *
988 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
989 * a file it includes, 3 when parsing a file included by that file, and so on.
990 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600991static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100992 unsigned long base, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100993{
994 struct token t;
995 char *s = *c;
996 int err = 0;
997
998 get_token(c, &t, L_KEYWORD);
999
1000 switch (t.type) {
1001 case T_TITLE:
1002 err = parse_sliteral(c, &cfg->title);
1003
1004 break;
1005
1006 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001007 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001008 break;
1009
1010 case T_BACKGROUND:
1011 err = parse_sliteral(c, &cfg->bmp);
1012 break;
1013
1014 default:
1015 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001016 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001017 }
1018
1019 if (err < 0)
1020 return err;
1021
1022 eol_or_eof(c);
1023
1024 return 1;
1025}
1026
1027/*
1028 * Handles parsing a 'menu line' when we're parsing a label.
1029 */
1030static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001031 struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001032{
1033 struct token t;
1034 char *s;
1035
1036 s = *c;
1037
1038 get_token(c, &t, L_KEYWORD);
1039
1040 switch (t.type) {
1041 case T_DEFAULT:
1042 if (!cfg->default_label)
1043 cfg->default_label = strdup(label->name);
1044
1045 if (!cfg->default_label)
1046 return -ENOMEM;
1047
1048 break;
1049 case T_LABEL:
1050 parse_sliteral(c, &label->menu);
1051 break;
1052 default:
1053 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001054 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001055 }
1056
1057 eol_or_eof(c);
1058
1059 return 0;
1060}
1061
1062/*
1063 * Handles parsing a 'kernel' label.
1064 * expecting "filename" or "<fit_filename>#cfg"
1065 */
1066static int parse_label_kernel(char **c, struct pxe_label *label)
1067{
1068 char *s;
1069 int err;
1070
1071 err = parse_sliteral(c, &label->kernel);
1072 if (err < 0)
1073 return err;
1074
1075 s = strstr(label->kernel, "#");
1076 if (!s)
1077 return 1;
1078
1079 label->config = malloc(strlen(s) + 1);
1080 if (!label->config)
1081 return -ENOMEM;
1082
1083 strcpy(label->config, s);
1084 *s = 0;
1085
1086 return 1;
1087}
1088
1089/*
1090 * Parses a label and adds it to the list of labels for a menu.
1091 *
1092 * A label ends when we either get to the end of a file, or
1093 * get some input we otherwise don't have a handler defined
1094 * for.
1095 *
1096 */
1097static int parse_label(char **c, struct pxe_menu *cfg)
1098{
1099 struct token t;
1100 int len;
1101 char *s = *c;
1102 struct pxe_label *label;
1103 int err;
1104
1105 label = label_create();
1106 if (!label)
1107 return -ENOMEM;
1108
1109 err = parse_sliteral(c, &label->name);
1110 if (err < 0) {
1111 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1112 label_destroy(label);
1113 return -EINVAL;
1114 }
1115
1116 list_add_tail(&label->list, &cfg->labels);
1117
1118 while (1) {
1119 s = *c;
1120 get_token(c, &t, L_KEYWORD);
1121
1122 err = 0;
1123 switch (t.type) {
1124 case T_MENU:
1125 err = parse_label_menu(c, cfg, label);
1126 break;
1127
1128 case T_KERNEL:
1129 case T_LINUX:
1130 err = parse_label_kernel(c, label);
1131 break;
1132
1133 case T_APPEND:
1134 err = parse_sliteral(c, &label->append);
1135 if (label->initrd)
1136 break;
1137 s = strstr(label->append, "initrd=");
1138 if (!s)
1139 break;
1140 s += 7;
1141 len = (int)(strchr(s, ' ') - s);
1142 label->initrd = malloc(len + 1);
1143 strncpy(label->initrd, s, len);
1144 label->initrd[len] = '\0';
1145
1146 break;
1147
1148 case T_INITRD:
1149 if (!label->initrd)
1150 err = parse_sliteral(c, &label->initrd);
1151 break;
1152
1153 case T_FDT:
1154 if (!label->fdt)
1155 err = parse_sliteral(c, &label->fdt);
1156 break;
1157
1158 case T_FDTDIR:
1159 if (!label->fdtdir)
1160 err = parse_sliteral(c, &label->fdtdir);
1161 break;
1162
Neil Armstrong69076df2021-01-20 09:54:53 +01001163 case T_FDTOVERLAYS:
1164 if (!label->fdtoverlays)
1165 err = parse_sliteral(c, &label->fdtoverlays);
1166 break;
1167
Patrice Chotard2373cba2019-11-25 09:07:37 +01001168 case T_LOCALBOOT:
1169 label->localboot = 1;
1170 err = parse_integer(c, &label->localboot_val);
1171 break;
1172
1173 case T_IPAPPEND:
1174 err = parse_integer(c, &label->ipappend);
1175 break;
1176
1177 case T_EOL:
1178 break;
1179
1180 default:
1181 /*
1182 * put the token back! we don't want it - it's the end
1183 * of a label and whatever token this is, it's
1184 * something for the menu level context to handle.
1185 */
1186 *c = s;
1187 return 1;
1188 }
1189
1190 if (err < 0)
1191 return err;
1192 }
1193}
1194
1195/*
1196 * This 16 comes from the limit pxelinux imposes on nested includes.
1197 *
1198 * There is no reason at all we couldn't do more, but some limit helps prevent
1199 * infinite (until crash occurs) recursion if a file tries to include itself.
1200 */
1201#define MAX_NEST_LEVEL 16
1202
1203/*
1204 * Entry point for parsing a menu file. nest_level indicates how many times
1205 * we've nested in includes. It will be 1 for the top level menu file.
1206 *
1207 * Returns 1 on success, < 0 on error.
1208 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001209static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001210 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001211{
1212 struct token t;
1213 char *s, *b, *label_name;
1214 int err;
1215
1216 b = p;
1217
1218 if (nest_level > MAX_NEST_LEVEL) {
1219 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1220 return -EMLINK;
1221 }
1222
1223 while (1) {
1224 s = p;
1225
1226 get_token(&p, &t, L_KEYWORD);
1227
1228 err = 0;
1229 switch (t.type) {
1230 case T_MENU:
1231 cfg->prompt = 1;
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001232 err = parse_menu(ctx, &p, cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001233 base + ALIGN(strlen(b) + 1, 4),
1234 nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001235 break;
1236
1237 case T_TIMEOUT:
1238 err = parse_integer(&p, &cfg->timeout);
1239 break;
1240
1241 case T_LABEL:
1242 err = parse_label(&p, cfg);
1243 break;
1244
1245 case T_DEFAULT:
1246 case T_ONTIMEOUT:
1247 err = parse_sliteral(&p, &label_name);
1248
1249 if (label_name) {
1250 if (cfg->default_label)
1251 free(cfg->default_label);
1252
1253 cfg->default_label = label_name;
1254 }
1255
1256 break;
1257
1258 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001259 err = handle_include(ctx, &p,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001260 base + ALIGN(strlen(b), 4), cfg,
1261 nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001262 break;
1263
1264 case T_PROMPT:
1265 eol_or_eof(&p);
1266 break;
1267
1268 case T_EOL:
1269 break;
1270
1271 case T_EOF:
1272 return 1;
1273
1274 default:
1275 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001276 (int)(p - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001277 eol_or_eof(&p);
1278 }
1279
1280 if (err < 0)
1281 return err;
1282 }
1283}
1284
1285/*
Patrice Chotard2373cba2019-11-25 09:07:37 +01001286 */
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 Glassfd3fa5c2021-10-14 12:47:56 -06001307struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard2373cba2019-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);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001323 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001324 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 Annapureddy5168d7a2020-03-18 11:38:42 -07001351 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard2373cba2019-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 Chotard2373cba2019-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 Glassfd3fa5c2021-10-14 12:47:56 -06001391static void boot_unattempted_labels(struct pxe_context *ctx,
1392 struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001393{
1394 struct list_head *pos;
1395 struct pxe_label *label;
1396
1397 list_for_each(pos, &cfg->labels) {
1398 label = list_entry(pos, struct pxe_label, list);
1399
1400 if (!label->attempted)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001401 label_boot(ctx, label);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001402 }
1403}
1404
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001405void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001406{
1407 void *choice;
1408 struct menu *m;
1409 int err;
1410
Kory Maincentff0287e2021-02-02 16:42:28 +01001411 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1412 /* display BMP if available */
1413 if (cfg->bmp) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001414 if (get_relfile(ctx, cfg->bmp, image_load_addr)) {
Kory Maincentff0287e2021-02-02 16:42:28 +01001415 if (CONFIG_IS_ENABLED(CMD_CLS))
1416 run_command("cls", 0);
1417 bmp_display(image_load_addr,
1418 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1419 } else {
1420 printf("Skipping background bmp %s for failure\n",
1421 cfg->bmp);
1422 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001423 }
1424 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001425
1426 m = pxe_menu_to_menu(cfg);
1427 if (!m)
1428 return;
1429
1430 err = menu_get_choice(m, &choice);
1431
1432 menu_destroy(m);
1433
1434 /*
1435 * err == 1 means we got a choice back from menu_get_choice.
1436 *
1437 * err == -ENOENT if the menu was setup to select the default but no
1438 * default was set. in that case, we should continue trying to boot
1439 * labels that haven't been attempted yet.
1440 *
1441 * otherwise, the user interrupted or there was some other error and
1442 * we give up.
1443 */
1444
1445 if (err == 1) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001446 err = label_boot(ctx, choice);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001447 if (!err)
1448 return;
1449 } else if (err != -ENOENT) {
1450 return;
1451 }
1452
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001453 boot_unattempted_labels(ctx, cfg);
1454}
1455
1456void pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp)
1457{
1458 ctx->cmdtp = cmdtp;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001459}