blob: c65f1eaf5c2d7d3299940f5c051c2b7d53dd3c29 [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
Patrice Chotard2373cba2019-11-25 09:07:37 +010097/*
98 * As in pxelinux, paths to files referenced from files we retrieve are
99 * relative to the location of bootfile. get_relfile takes such a path and
100 * joins it with the bootfile path to get the full path to the target file. If
101 * the bootfile path is NULL, we use file_path as is.
102 *
103 * Returns 1 for success, or < 0 on error.
104 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600105static int get_relfile(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100106 unsigned long file_addr)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100107{
108 size_t path_len;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100109 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100110 char addr_buf[18];
111 int err;
112
113 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
114
115 if (err < 0)
116 return err;
117
118 path_len = strlen(file_path);
119 path_len += strlen(relfile);
120
121 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100122 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100123
124 return -ENAMETOOLONG;
125 }
126
127 strcat(relfile, file_path);
128
129 printf("Retrieving file: %s\n", relfile);
130
131 sprintf(addr_buf, "%lx", file_addr);
132
Simon Glassb1ead6b2021-10-14 12:47:57 -0600133 return ctx->getfile(ctx, relfile, addr_buf);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100134}
135
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600136int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100137 unsigned long file_addr)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100138{
139 unsigned long config_file_size;
140 char *tftp_filesize;
141 int err;
142 char *buf;
143
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600144 err = get_relfile(ctx, file_path, file_addr);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100145
146 if (err < 0)
147 return err;
148
149 /*
150 * the file comes without a NUL byte at the end, so find out its size
151 * and add the NUL byte.
152 */
153 tftp_filesize = from_env("filesize");
154
155 if (!tftp_filesize)
156 return -ENOENT;
157
158 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
159 return -EINVAL;
160
161 buf = map_sysmem(file_addr + config_file_size, 1);
162 *buf = '\0';
163 unmap_sysmem(buf);
164
165 return 1;
166}
167
168#define PXELINUX_DIR "pxelinux.cfg/"
169
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600170int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100171 unsigned long pxefile_addr_r)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100172{
173 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100174 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100175
176 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
177 printf("path (%s%s) too long, skipping\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100178 PXELINUX_DIR, file);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100179 return -ENAMETOOLONG;
180 }
181
182 sprintf(path, PXELINUX_DIR "%s", file);
183
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600184 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100185}
186
187/*
188 * Wrapper to make it easier to store the file at file_path in the location
189 * specified by envaddr_name. file_path will be joined to the bootfile path,
190 * if any is specified.
191 *
192 * Returns 1 on success or < 0 on error.
193 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600194static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100195 const char *envaddr_name)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100196{
197 unsigned long file_addr;
198 char *envaddr;
199
200 envaddr = from_env(envaddr_name);
201
202 if (!envaddr)
203 return -ENOENT;
204
205 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
206 return -EINVAL;
207
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600208 return get_relfile(ctx, file_path, file_addr);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100209}
210
211/*
212 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
213 * result must be free()'d to reclaim the memory.
214 *
215 * Returns NULL if malloc fails.
216 */
217static struct pxe_label *label_create(void)
218{
219 struct pxe_label *label;
220
221 label = malloc(sizeof(struct pxe_label));
222
223 if (!label)
224 return NULL;
225
226 memset(label, 0, sizeof(struct pxe_label));
227
228 return label;
229}
230
231/*
232 * Free the memory used by a pxe_label, including that used by its name,
233 * kernel, append and initrd members, if they're non NULL.
234 *
235 * So - be sure to only use dynamically allocated memory for the members of
236 * the pxe_label struct, unless you want to clean it up first. These are
237 * currently only created by the pxe file parsing code.
238 */
239static void label_destroy(struct pxe_label *label)
240{
241 if (label->name)
242 free(label->name);
243
244 if (label->kernel)
245 free(label->kernel);
246
247 if (label->config)
248 free(label->config);
249
250 if (label->append)
251 free(label->append);
252
253 if (label->initrd)
254 free(label->initrd);
255
256 if (label->fdt)
257 free(label->fdt);
258
259 if (label->fdtdir)
260 free(label->fdtdir);
261
Neil Armstrong69076df2021-01-20 09:54:53 +0100262 if (label->fdtoverlays)
263 free(label->fdtoverlays);
264
Patrice Chotard2373cba2019-11-25 09:07:37 +0100265 free(label);
266}
267
268/*
269 * Print a label and its string members if they're defined.
270 *
271 * This is passed as a callback to the menu code for displaying each
272 * menu entry.
273 */
274static void label_print(void *data)
275{
276 struct pxe_label *label = data;
277 const char *c = label->menu ? label->menu : label->name;
278
279 printf("%s:\t%s\n", label->num, c);
280}
281
282/*
283 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
284 * environment variable is defined. Its contents will be executed as U-Boot
285 * command. If the label specified an 'append' line, its contents will be
286 * used to overwrite the contents of the 'bootargs' environment variable prior
287 * to running 'localcmd'.
288 *
289 * Returns 1 on success or < 0 on error.
290 */
291static int label_localboot(struct pxe_label *label)
292{
293 char *localcmd;
294
295 localcmd = from_env("localcmd");
296
297 if (!localcmd)
298 return -ENOENT;
299
300 if (label->append) {
301 char bootargs[CONFIG_SYS_CBSIZE];
302
Simon Glass1a62d642020-11-05 10:33:47 -0700303 cli_simple_process_macros(label->append, bootargs,
304 sizeof(bootargs));
Patrice Chotard2373cba2019-11-25 09:07:37 +0100305 env_set("bootargs", bootargs);
306 }
307
308 debug("running: %s\n", localcmd);
309
310 return run_command_list(localcmd, strlen(localcmd), 0);
311}
312
313/*
Neil Armstrong69076df2021-01-20 09:54:53 +0100314 * Loads fdt overlays specified in 'fdtoverlays'.
315 */
316#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600317static void label_boot_fdtoverlay(struct pxe_context *ctx,
318 struct pxe_label *label)
Neil Armstrong69076df2021-01-20 09:54:53 +0100319{
320 char *fdtoverlay = label->fdtoverlays;
321 struct fdt_header *working_fdt;
322 char *fdtoverlay_addr_env;
323 ulong fdtoverlay_addr;
324 ulong fdt_addr;
325 int err;
326
327 /* Get the main fdt and map it */
Simon Glass7e5f4602021-07-24 09:03:29 -0600328 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100329 working_fdt = map_sysmem(fdt_addr, 0);
330 err = fdt_check_header(working_fdt);
331 if (err)
332 return;
333
334 /* Get the specific overlay loading address */
335 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
336 if (!fdtoverlay_addr_env) {
337 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
338 return;
339 }
340
Simon Glass7e5f4602021-07-24 09:03:29 -0600341 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100342
343 /* Cycle over the overlay files and apply them in order */
344 do {
345 struct fdt_header *blob;
346 char *overlayfile;
347 char *end;
348 int len;
349
350 /* Drop leading spaces */
351 while (*fdtoverlay == ' ')
352 ++fdtoverlay;
353
354 /* Copy a single filename if multiple provided */
355 end = strstr(fdtoverlay, " ");
356 if (end) {
357 len = (int)(end - fdtoverlay);
358 overlayfile = malloc(len + 1);
359 strncpy(overlayfile, fdtoverlay, len);
360 overlayfile[len] = '\0';
361 } else
362 overlayfile = fdtoverlay;
363
364 if (!strlen(overlayfile))
365 goto skip_overlay;
366
367 /* Load overlay file */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600368 err = get_relfile_envaddr(ctx, overlayfile,
Neil Armstrong69076df2021-01-20 09:54:53 +0100369 "fdtoverlay_addr_r");
370 if (err < 0) {
371 printf("Failed loading overlay %s\n", overlayfile);
372 goto skip_overlay;
373 }
374
375 /* Resize main fdt */
376 fdt_shrink_to_minimum(working_fdt, 8192);
377
378 blob = map_sysmem(fdtoverlay_addr, 0);
379 err = fdt_check_header(blob);
380 if (err) {
381 printf("Invalid overlay %s, skipping\n",
382 overlayfile);
383 goto skip_overlay;
384 }
385
386 err = fdt_overlay_apply_verbose(working_fdt, blob);
387 if (err) {
388 printf("Failed to apply overlay %s, skipping\n",
389 overlayfile);
390 goto skip_overlay;
391 }
392
393skip_overlay:
394 if (end)
395 free(overlayfile);
396 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
397}
398#endif
399
400/*
Patrice Chotard2373cba2019-11-25 09:07:37 +0100401 * Boot according to the contents of a pxe_label.
402 *
403 * If we can't boot for any reason, we return. A successful boot never
404 * returns.
405 *
406 * The kernel will be stored in the location given by the 'kernel_addr_r'
407 * environment variable.
408 *
409 * If the label specifies an initrd file, it will be stored in the location
410 * given by the 'ramdisk_addr_r' environment variable.
411 *
412 * If the label specifies an 'append' line, its contents will overwrite that
413 * of the 'bootargs' environment variable.
414 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600415static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100416{
417 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700418 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700419 char *kernel_addr = NULL;
420 char *initrd_addr_str = NULL;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700421 char initrd_filesize[10];
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700422 char initrd_str[28];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100423 char mac_str[29] = "";
424 char ip_str[68] = "";
425 char *fit_addr = NULL;
426 int bootm_argc = 2;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700427 int zboot_argc = 3;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100428 int len = 0;
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700429 ulong kernel_addr_r;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100430 void *buf;
431
432 label_print(label);
433
434 label->attempted = 1;
435
436 if (label->localboot) {
437 if (label->localboot_val >= 0)
438 label_localboot(label);
439 return 0;
440 }
441
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100442 if (!label->kernel) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100443 printf("No kernel given, skipping %s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100444 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100445 return 1;
446 }
447
448 if (label->initrd) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600449 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100450 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100451 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100452 return 1;
453 }
454
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700455 initrd_addr_str = env_get("ramdisk_addr_r");
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700456 strncpy(initrd_filesize, env_get("filesize"), 9);
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700457
458 strncpy(initrd_str, initrd_addr_str, 18);
459 strcat(initrd_str, ":");
460 strncat(initrd_str, initrd_filesize, 9);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100461 }
462
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600463 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100464 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100465 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100466 return 1;
467 }
468
469 if (label->ipappend & 0x1) {
470 sprintf(ip_str, " ip=%s:%s:%s:%s",
471 env_get("ipaddr"), env_get("serverip"),
472 env_get("gatewayip"), env_get("netmask"));
473 }
474
Kory Maincentff0287e2021-02-02 16:42:28 +0100475 if (IS_ENABLED(CONFIG_CMD_NET)) {
476 if (label->ipappend & 0x2) {
477 int err;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100478
Kory Maincentff0287e2021-02-02 16:42:28 +0100479 strcpy(mac_str, " BOOTIF=");
480 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
481 if (err < 0)
482 mac_str[0] = '\0';
483 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100484 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100485
486 if ((label->ipappend & 0x3) || label->append) {
487 char bootargs[CONFIG_SYS_CBSIZE] = "";
488 char finalbootargs[CONFIG_SYS_CBSIZE];
489
490 if (strlen(label->append ?: "") +
491 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
492 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
493 strlen(label->append ?: ""),
494 strlen(ip_str), strlen(mac_str),
495 sizeof(bootargs));
496 return 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100497 }
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100498
499 if (label->append)
500 strncpy(bootargs, label->append, sizeof(bootargs));
501
502 strcat(bootargs, ip_str);
503 strcat(bootargs, mac_str);
504
Simon Glass1a62d642020-11-05 10:33:47 -0700505 cli_simple_process_macros(bootargs, finalbootargs,
506 sizeof(finalbootargs));
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100507 env_set("bootargs", finalbootargs);
508 printf("append: %s\n", finalbootargs);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100509 }
510
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700511 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700512
Patrice Chotard2373cba2019-11-25 09:07:37 +0100513 /* for FIT, append the configuration identifier */
514 if (label->config) {
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700515 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100516
517 fit_addr = malloc(len);
518 if (!fit_addr) {
519 printf("malloc fail (FIT address)\n");
520 return 1;
521 }
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700522 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
523 kernel_addr = fit_addr;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100524 }
525
526 /*
527 * fdt usage is optional:
Anton Leontievdb366742019-09-03 10:52:24 +0300528 * It handles the following scenarios.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100529 *
Anton Leontievdb366742019-09-03 10:52:24 +0300530 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
531 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
532 * bootm, and adjust argc appropriately.
533 *
534 * If retrieve fails and no exact fdt blob is specified in pxe file with
535 * "fdt" label, try Scenario 2.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100536 *
537 * Scenario 2: If there is an fdt_addr specified, pass it along to
538 * bootm, and adjust argc appropriately.
539 *
540 * Scenario 3: fdt blob is not available.
541 */
542 bootm_argv[3] = env_get("fdt_addr_r");
543
544 /* if fdt label is defined then get fdt from server */
545 if (bootm_argv[3]) {
546 char *fdtfile = NULL;
547 char *fdtfilefree = NULL;
548
549 if (label->fdt) {
550 fdtfile = label->fdt;
551 } else if (label->fdtdir) {
552 char *f1, *f2, *f3, *f4, *slash;
553
554 f1 = env_get("fdtfile");
555 if (f1) {
556 f2 = "";
557 f3 = "";
558 f4 = "";
559 } else {
560 /*
561 * For complex cases where this code doesn't
562 * generate the correct filename, the board
563 * code should set $fdtfile during early boot,
564 * or the boot scripts should set $fdtfile
565 * before invoking "pxe" or "sysboot".
566 */
567 f1 = env_get("soc");
568 f2 = "-";
569 f3 = env_get("board");
570 f4 = ".dtb";
Dimitri John Ledkov75efe7d2021-04-21 12:42:01 +0100571 if (!f1) {
572 f1 = "";
573 f2 = "";
574 }
575 if (!f3) {
576 f2 = "";
577 f3 = "";
578 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100579 }
580
581 len = strlen(label->fdtdir);
582 if (!len)
583 slash = "./";
584 else if (label->fdtdir[len - 1] != '/')
585 slash = "/";
586 else
587 slash = "";
588
589 len = strlen(label->fdtdir) + strlen(slash) +
590 strlen(f1) + strlen(f2) + strlen(f3) +
591 strlen(f4) + 1;
592 fdtfilefree = malloc(len);
593 if (!fdtfilefree) {
594 printf("malloc fail (FDT filename)\n");
595 goto cleanup;
596 }
597
598 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
599 label->fdtdir, slash, f1, f2, f3, f4);
600 fdtfile = fdtfilefree;
601 }
602
603 if (fdtfile) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600604 int err = get_relfile_envaddr(ctx, fdtfile,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100605 "fdt_addr_r");
606
Patrice Chotard2373cba2019-11-25 09:07:37 +0100607 free(fdtfilefree);
608 if (err < 0) {
Anton Leontievdb366742019-09-03 10:52:24 +0300609 bootm_argv[3] = NULL;
610
611 if (label->fdt) {
612 printf("Skipping %s for failure retrieving FDT\n",
613 label->name);
614 goto cleanup;
615 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100616 }
Neil Armstrong69076df2021-01-20 09:54:53 +0100617
618#ifdef CONFIG_OF_LIBFDT_OVERLAY
619 if (label->fdtoverlays)
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600620 label_boot_fdtoverlay(ctx, label);
Neil Armstrong69076df2021-01-20 09:54:53 +0100621#endif
Patrice Chotard2373cba2019-11-25 09:07:37 +0100622 } else {
623 bootm_argv[3] = NULL;
624 }
625 }
626
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700627 bootm_argv[1] = kernel_addr;
628 zboot_argv[1] = kernel_addr;
629
630 if (initrd_addr_str) {
631 bootm_argv[2] = initrd_str;
632 bootm_argc = 3;
633
634 zboot_argv[3] = initrd_addr_str;
635 zboot_argv[4] = initrd_filesize;
636 zboot_argc = 5;
637 }
638
Patrice Chotard2373cba2019-11-25 09:07:37 +0100639 if (!bootm_argv[3])
640 bootm_argv[3] = env_get("fdt_addr");
641
642 if (bootm_argv[3]) {
643 if (!bootm_argv[2])
644 bootm_argv[2] = "-";
645 bootm_argc = 4;
646 }
647
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700648 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
649 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100650 /* Try bootm for legacy and FIT format image */
651 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600652 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100653 /* Try booting an AArch64 Linux kernel image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100654 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600655 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100656 /* Try booting a Image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100657 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600658 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincent18c25822021-02-02 16:42:29 +0100659 /* Try booting an x86_64 Linux kernel image */
660 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600661 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentff0287e2021-02-02 16:42:28 +0100662
Patrice Chotard2373cba2019-11-25 09:07:37 +0100663 unmap_sysmem(buf);
664
665cleanup:
666 if (fit_addr)
667 free(fit_addr);
668 return 1;
669}
670
671/*
672 * Tokens for the pxe file parser.
673 */
674enum token_type {
675 T_EOL,
676 T_STRING,
677 T_EOF,
678 T_MENU,
679 T_TITLE,
680 T_TIMEOUT,
681 T_LABEL,
682 T_KERNEL,
683 T_LINUX,
684 T_APPEND,
685 T_INITRD,
686 T_LOCALBOOT,
687 T_DEFAULT,
688 T_PROMPT,
689 T_INCLUDE,
690 T_FDT,
691 T_FDTDIR,
Neil Armstrong69076df2021-01-20 09:54:53 +0100692 T_FDTOVERLAYS,
Patrice Chotard2373cba2019-11-25 09:07:37 +0100693 T_ONTIMEOUT,
694 T_IPAPPEND,
695 T_BACKGROUND,
696 T_INVALID
697};
698
699/*
700 * A token - given by a value and a type.
701 */
702struct token {
703 char *val;
704 enum token_type type;
705};
706
707/*
708 * Keywords recognized.
709 */
710static const struct token keywords[] = {
711 {"menu", T_MENU},
712 {"title", T_TITLE},
713 {"timeout", T_TIMEOUT},
714 {"default", T_DEFAULT},
715 {"prompt", T_PROMPT},
716 {"label", T_LABEL},
717 {"kernel", T_KERNEL},
718 {"linux", T_LINUX},
719 {"localboot", T_LOCALBOOT},
720 {"append", T_APPEND},
721 {"initrd", T_INITRD},
722 {"include", T_INCLUDE},
723 {"devicetree", T_FDT},
724 {"fdt", T_FDT},
725 {"devicetreedir", T_FDTDIR},
726 {"fdtdir", T_FDTDIR},
Neil Armstrong69076df2021-01-20 09:54:53 +0100727 {"fdtoverlays", T_FDTOVERLAYS},
Patrice Chotard2373cba2019-11-25 09:07:37 +0100728 {"ontimeout", T_ONTIMEOUT,},
729 {"ipappend", T_IPAPPEND,},
730 {"background", T_BACKGROUND,},
731 {NULL, T_INVALID}
732};
733
734/*
735 * Since pxe(linux) files don't have a token to identify the start of a
736 * literal, we have to keep track of when we're in a state where a literal is
737 * expected vs when we're in a state a keyword is expected.
738 */
739enum lex_state {
740 L_NORMAL = 0,
741 L_KEYWORD,
742 L_SLITERAL
743};
744
745/*
746 * get_string retrieves a string from *p and stores it as a token in
747 * *t.
748 *
749 * get_string used for scanning both string literals and keywords.
750 *
751 * Characters from *p are copied into t-val until a character equal to
752 * delim is found, or a NUL byte is reached. If delim has the special value of
753 * ' ', any whitespace character will be used as a delimiter.
754 *
755 * If lower is unequal to 0, uppercase characters will be converted to
756 * lowercase in the result. This is useful to make keywords case
757 * insensitive.
758 *
759 * The location of *p is updated to point to the first character after the end
760 * of the token - the ending delimiter.
761 *
762 * On success, the new value of t->val is returned. Memory for t->val is
763 * allocated using malloc and must be free()'d to reclaim it. If insufficient
764 * memory is available, NULL is returned.
765 */
766static char *get_string(char **p, struct token *t, char delim, int lower)
767{
768 char *b, *e;
769 size_t len, i;
770
771 /*
772 * b and e both start at the beginning of the input stream.
773 *
774 * e is incremented until we find the ending delimiter, or a NUL byte
775 * is reached. Then, we take e - b to find the length of the token.
776 */
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100777 b = *p;
778 e = *p;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100779
780 while (*e) {
781 if ((delim == ' ' && isspace(*e)) || delim == *e)
782 break;
783 e++;
784 }
785
786 len = e - b;
787
788 /*
789 * Allocate memory to hold the string, and copy it in, converting
790 * characters to lowercase if lower is != 0.
791 */
792 t->val = malloc(len + 1);
793 if (!t->val)
794 return NULL;
795
796 for (i = 0; i < len; i++, b++) {
797 if (lower)
798 t->val[i] = tolower(*b);
799 else
800 t->val[i] = *b;
801 }
802
803 t->val[len] = '\0';
804
805 /*
806 * Update *p so the caller knows where to continue scanning.
807 */
808 *p = e;
809
810 t->type = T_STRING;
811
812 return t->val;
813}
814
815/*
816 * Populate a keyword token with a type and value.
817 */
818static void get_keyword(struct token *t)
819{
820 int i;
821
822 for (i = 0; keywords[i].val; i++) {
823 if (!strcmp(t->val, keywords[i].val)) {
824 t->type = keywords[i].type;
825 break;
826 }
827 }
828}
829
830/*
831 * Get the next token. We have to keep track of which state we're in to know
832 * if we're looking to get a string literal or a keyword.
833 *
834 * *p is updated to point at the first character after the current token.
835 */
836static void get_token(char **p, struct token *t, enum lex_state state)
837{
838 char *c = *p;
839
840 t->type = T_INVALID;
841
842 /* eat non EOL whitespace */
843 while (isblank(*c))
844 c++;
845
846 /*
847 * eat comments. note that string literals can't begin with #, but
848 * can contain a # after their first character.
849 */
850 if (*c == '#') {
851 while (*c && *c != '\n')
852 c++;
853 }
854
855 if (*c == '\n') {
856 t->type = T_EOL;
857 c++;
858 } else if (*c == '\0') {
859 t->type = T_EOF;
860 c++;
861 } else if (state == L_SLITERAL) {
862 get_string(&c, t, '\n', 0);
863 } else if (state == L_KEYWORD) {
864 /*
865 * when we expect a keyword, we first get the next string
866 * token delimited by whitespace, and then check if it
867 * matches a keyword in our keyword list. if it does, it's
868 * converted to a keyword token of the appropriate type, and
869 * if not, it remains a string token.
870 */
871 get_string(&c, t, ' ', 1);
872 get_keyword(t);
873 }
874
875 *p = c;
876}
877
878/*
879 * Increment *c until we get to the end of the current line, or EOF.
880 */
881static void eol_or_eof(char **c)
882{
883 while (**c && **c != '\n')
884 (*c)++;
885}
886
887/*
888 * All of these parse_* functions share some common behavior.
889 *
890 * They finish with *c pointing after the token they parse, and return 1 on
891 * success, or < 0 on error.
892 */
893
894/*
895 * Parse a string literal and store a pointer it at *dst. String literals
896 * terminate at the end of the line.
897 */
898static int parse_sliteral(char **c, char **dst)
899{
900 struct token t;
901 char *s = *c;
902
903 get_token(c, &t, L_SLITERAL);
904
905 if (t.type != T_STRING) {
906 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
907 return -EINVAL;
908 }
909
910 *dst = t.val;
911
912 return 1;
913}
914
915/*
916 * Parse a base 10 (unsigned) integer and store it at *dst.
917 */
918static int parse_integer(char **c, int *dst)
919{
920 struct token t;
921 char *s = *c;
922
923 get_token(c, &t, L_SLITERAL);
924
925 if (t.type != T_STRING) {
926 printf("Expected string: %.*s\n", (int)(*c - s), s);
927 return -EINVAL;
928 }
929
930 *dst = simple_strtol(t.val, NULL, 10);
931
932 free(t.val);
933
934 return 1;
935}
936
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600937static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100938 struct pxe_menu *cfg, int nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100939
940/*
941 * Parse an include statement, and retrieve and parse the file it mentions.
942 *
943 * base should point to a location where it's safe to store the file, and
944 * nest_level should indicate how many nested includes have occurred. For this
945 * include, nest_level has already been incremented and doesn't need to be
946 * incremented here.
947 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600948static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100949 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100950{
951 char *include_path;
952 char *s = *c;
953 int err;
954 char *buf;
955 int ret;
956
957 err = parse_sliteral(c, &include_path);
958
959 if (err < 0) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100960 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100961 return err;
962 }
963
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600964 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100965
966 if (err < 0) {
967 printf("Couldn't retrieve %s\n", include_path);
968 return err;
969 }
970
971 buf = map_sysmem(base, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600972 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100973 unmap_sysmem(buf);
974
975 return ret;
976}
977
978/*
979 * Parse lines that begin with 'menu'.
980 *
981 * base and nest are provided to handle the 'menu include' case.
982 *
983 * base should point to a location where it's safe to store the included file.
984 *
985 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
986 * a file it includes, 3 when parsing a file included by that file, and so on.
987 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600988static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100989 unsigned long base, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100990{
991 struct token t;
992 char *s = *c;
993 int err = 0;
994
995 get_token(c, &t, L_KEYWORD);
996
997 switch (t.type) {
998 case T_TITLE:
999 err = parse_sliteral(c, &cfg->title);
1000
1001 break;
1002
1003 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001004 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001005 break;
1006
1007 case T_BACKGROUND:
1008 err = parse_sliteral(c, &cfg->bmp);
1009 break;
1010
1011 default:
1012 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001013 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001014 }
1015
1016 if (err < 0)
1017 return err;
1018
1019 eol_or_eof(c);
1020
1021 return 1;
1022}
1023
1024/*
1025 * Handles parsing a 'menu line' when we're parsing a label.
1026 */
1027static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001028 struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001029{
1030 struct token t;
1031 char *s;
1032
1033 s = *c;
1034
1035 get_token(c, &t, L_KEYWORD);
1036
1037 switch (t.type) {
1038 case T_DEFAULT:
1039 if (!cfg->default_label)
1040 cfg->default_label = strdup(label->name);
1041
1042 if (!cfg->default_label)
1043 return -ENOMEM;
1044
1045 break;
1046 case T_LABEL:
1047 parse_sliteral(c, &label->menu);
1048 break;
1049 default:
1050 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001051 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001052 }
1053
1054 eol_or_eof(c);
1055
1056 return 0;
1057}
1058
1059/*
1060 * Handles parsing a 'kernel' label.
1061 * expecting "filename" or "<fit_filename>#cfg"
1062 */
1063static int parse_label_kernel(char **c, struct pxe_label *label)
1064{
1065 char *s;
1066 int err;
1067
1068 err = parse_sliteral(c, &label->kernel);
1069 if (err < 0)
1070 return err;
1071
1072 s = strstr(label->kernel, "#");
1073 if (!s)
1074 return 1;
1075
1076 label->config = malloc(strlen(s) + 1);
1077 if (!label->config)
1078 return -ENOMEM;
1079
1080 strcpy(label->config, s);
1081 *s = 0;
1082
1083 return 1;
1084}
1085
1086/*
1087 * Parses a label and adds it to the list of labels for a menu.
1088 *
1089 * A label ends when we either get to the end of a file, or
1090 * get some input we otherwise don't have a handler defined
1091 * for.
1092 *
1093 */
1094static int parse_label(char **c, struct pxe_menu *cfg)
1095{
1096 struct token t;
1097 int len;
1098 char *s = *c;
1099 struct pxe_label *label;
1100 int err;
1101
1102 label = label_create();
1103 if (!label)
1104 return -ENOMEM;
1105
1106 err = parse_sliteral(c, &label->name);
1107 if (err < 0) {
1108 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1109 label_destroy(label);
1110 return -EINVAL;
1111 }
1112
1113 list_add_tail(&label->list, &cfg->labels);
1114
1115 while (1) {
1116 s = *c;
1117 get_token(c, &t, L_KEYWORD);
1118
1119 err = 0;
1120 switch (t.type) {
1121 case T_MENU:
1122 err = parse_label_menu(c, cfg, label);
1123 break;
1124
1125 case T_KERNEL:
1126 case T_LINUX:
1127 err = parse_label_kernel(c, label);
1128 break;
1129
1130 case T_APPEND:
1131 err = parse_sliteral(c, &label->append);
1132 if (label->initrd)
1133 break;
1134 s = strstr(label->append, "initrd=");
1135 if (!s)
1136 break;
1137 s += 7;
1138 len = (int)(strchr(s, ' ') - s);
1139 label->initrd = malloc(len + 1);
1140 strncpy(label->initrd, s, len);
1141 label->initrd[len] = '\0';
1142
1143 break;
1144
1145 case T_INITRD:
1146 if (!label->initrd)
1147 err = parse_sliteral(c, &label->initrd);
1148 break;
1149
1150 case T_FDT:
1151 if (!label->fdt)
1152 err = parse_sliteral(c, &label->fdt);
1153 break;
1154
1155 case T_FDTDIR:
1156 if (!label->fdtdir)
1157 err = parse_sliteral(c, &label->fdtdir);
1158 break;
1159
Neil Armstrong69076df2021-01-20 09:54:53 +01001160 case T_FDTOVERLAYS:
1161 if (!label->fdtoverlays)
1162 err = parse_sliteral(c, &label->fdtoverlays);
1163 break;
1164
Patrice Chotard2373cba2019-11-25 09:07:37 +01001165 case T_LOCALBOOT:
1166 label->localboot = 1;
1167 err = parse_integer(c, &label->localboot_val);
1168 break;
1169
1170 case T_IPAPPEND:
1171 err = parse_integer(c, &label->ipappend);
1172 break;
1173
1174 case T_EOL:
1175 break;
1176
1177 default:
1178 /*
1179 * put the token back! we don't want it - it's the end
1180 * of a label and whatever token this is, it's
1181 * something for the menu level context to handle.
1182 */
1183 *c = s;
1184 return 1;
1185 }
1186
1187 if (err < 0)
1188 return err;
1189 }
1190}
1191
1192/*
1193 * This 16 comes from the limit pxelinux imposes on nested includes.
1194 *
1195 * There is no reason at all we couldn't do more, but some limit helps prevent
1196 * infinite (until crash occurs) recursion if a file tries to include itself.
1197 */
1198#define MAX_NEST_LEVEL 16
1199
1200/*
1201 * Entry point for parsing a menu file. nest_level indicates how many times
1202 * we've nested in includes. It will be 1 for the top level menu file.
1203 *
1204 * Returns 1 on success, < 0 on error.
1205 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001206static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001207 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001208{
1209 struct token t;
1210 char *s, *b, *label_name;
1211 int err;
1212
1213 b = p;
1214
1215 if (nest_level > MAX_NEST_LEVEL) {
1216 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1217 return -EMLINK;
1218 }
1219
1220 while (1) {
1221 s = p;
1222
1223 get_token(&p, &t, L_KEYWORD);
1224
1225 err = 0;
1226 switch (t.type) {
1227 case T_MENU:
1228 cfg->prompt = 1;
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001229 err = parse_menu(ctx, &p, cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001230 base + ALIGN(strlen(b) + 1, 4),
1231 nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001232 break;
1233
1234 case T_TIMEOUT:
1235 err = parse_integer(&p, &cfg->timeout);
1236 break;
1237
1238 case T_LABEL:
1239 err = parse_label(&p, cfg);
1240 break;
1241
1242 case T_DEFAULT:
1243 case T_ONTIMEOUT:
1244 err = parse_sliteral(&p, &label_name);
1245
1246 if (label_name) {
1247 if (cfg->default_label)
1248 free(cfg->default_label);
1249
1250 cfg->default_label = label_name;
1251 }
1252
1253 break;
1254
1255 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001256 err = handle_include(ctx, &p,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001257 base + ALIGN(strlen(b), 4), cfg,
1258 nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001259 break;
1260
1261 case T_PROMPT:
1262 eol_or_eof(&p);
1263 break;
1264
1265 case T_EOL:
1266 break;
1267
1268 case T_EOF:
1269 return 1;
1270
1271 default:
1272 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001273 (int)(p - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001274 eol_or_eof(&p);
1275 }
1276
1277 if (err < 0)
1278 return err;
1279 }
1280}
1281
1282/*
Patrice Chotard2373cba2019-11-25 09:07:37 +01001283 */
1284void destroy_pxe_menu(struct pxe_menu *cfg)
1285{
1286 struct list_head *pos, *n;
1287 struct pxe_label *label;
1288
1289 if (cfg->title)
1290 free(cfg->title);
1291
1292 if (cfg->default_label)
1293 free(cfg->default_label);
1294
1295 list_for_each_safe(pos, n, &cfg->labels) {
1296 label = list_entry(pos, struct pxe_label, list);
1297
1298 label_destroy(label);
1299 }
1300
1301 free(cfg);
1302}
1303
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001304struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001305{
1306 struct pxe_menu *cfg;
1307 char *buf;
1308 int r;
1309
1310 cfg = malloc(sizeof(struct pxe_menu));
1311
1312 if (!cfg)
1313 return NULL;
1314
1315 memset(cfg, 0, sizeof(struct pxe_menu));
1316
1317 INIT_LIST_HEAD(&cfg->labels);
1318
1319 buf = map_sysmem(menucfg, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001320 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001321 unmap_sysmem(buf);
1322
1323 if (r < 0) {
1324 destroy_pxe_menu(cfg);
1325 return NULL;
1326 }
1327
1328 return cfg;
1329}
1330
1331/*
1332 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1333 * menu code.
1334 */
1335static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1336{
1337 struct pxe_label *label;
1338 struct list_head *pos;
1339 struct menu *m;
1340 int err;
1341 int i = 1;
1342 char *default_num = NULL;
1343
1344 /*
1345 * Create a menu and add items for all the labels.
1346 */
1347 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -07001348 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001349
1350 if (!m)
1351 return NULL;
1352
1353 list_for_each(pos, &cfg->labels) {
1354 label = list_entry(pos, struct pxe_label, list);
1355
1356 sprintf(label->num, "%d", i++);
1357 if (menu_item_add(m, label->num, label) != 1) {
1358 menu_destroy(m);
1359 return NULL;
1360 }
1361 if (cfg->default_label &&
1362 (strcmp(label->name, cfg->default_label) == 0))
1363 default_num = label->num;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001364 }
1365
1366 /*
1367 * After we've created items for each label in the menu, set the
1368 * menu's default label if one was specified.
1369 */
1370 if (default_num) {
1371 err = menu_default_set(m, default_num);
1372 if (err != 1) {
1373 if (err != -ENOENT) {
1374 menu_destroy(m);
1375 return NULL;
1376 }
1377
1378 printf("Missing default: %s\n", cfg->default_label);
1379 }
1380 }
1381
1382 return m;
1383}
1384
1385/*
1386 * Try to boot any labels we have yet to attempt to boot.
1387 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001388static void boot_unattempted_labels(struct pxe_context *ctx,
1389 struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001390{
1391 struct list_head *pos;
1392 struct pxe_label *label;
1393
1394 list_for_each(pos, &cfg->labels) {
1395 label = list_entry(pos, struct pxe_label, list);
1396
1397 if (!label->attempted)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001398 label_boot(ctx, label);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001399 }
1400}
1401
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001402void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001403{
1404 void *choice;
1405 struct menu *m;
1406 int err;
1407
Kory Maincentff0287e2021-02-02 16:42:28 +01001408 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1409 /* display BMP if available */
1410 if (cfg->bmp) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001411 if (get_relfile(ctx, cfg->bmp, image_load_addr)) {
Kory Maincentff0287e2021-02-02 16:42:28 +01001412 if (CONFIG_IS_ENABLED(CMD_CLS))
1413 run_command("cls", 0);
1414 bmp_display(image_load_addr,
1415 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1416 } else {
1417 printf("Skipping background bmp %s for failure\n",
1418 cfg->bmp);
1419 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001420 }
1421 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001422
1423 m = pxe_menu_to_menu(cfg);
1424 if (!m)
1425 return;
1426
1427 err = menu_get_choice(m, &choice);
1428
1429 menu_destroy(m);
1430
1431 /*
1432 * err == 1 means we got a choice back from menu_get_choice.
1433 *
1434 * err == -ENOENT if the menu was setup to select the default but no
1435 * default was set. in that case, we should continue trying to boot
1436 * labels that haven't been attempted yet.
1437 *
1438 * otherwise, the user interrupted or there was some other error and
1439 * we give up.
1440 */
1441
1442 if (err == 1) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001443 err = label_boot(ctx, choice);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001444 if (!err)
1445 return;
1446 } else if (err != -ENOENT) {
1447 return;
1448 }
1449
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001450 boot_unattempted_labels(ctx, cfg);
1451}
1452
Simon Glassb1ead6b2021-10-14 12:47:57 -06001453void pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1454 pxe_getfile_func getfile)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001455{
1456 ctx->cmdtp = cmdtp;
Simon Glassb1ead6b2021-10-14 12:47:57 -06001457 ctx->getfile = getfile;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001458}