blob: d591798eda2d8514dbbdd4577c3baff64a434a05 [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
Simon Glass18109cc2021-10-14 12:48:01 -060033/**
34 * format_mac_pxe() - obtain a MAC address in the PXE format
35 *
36 * This produces a MAC-address string in the format for the current ethernet
37 * device:
38 *
39 * 01-aa-bb-cc-dd-ee-ff
40 *
41 * where aa-ff is the MAC address in hex
42 *
43 * @outbuf: Buffer to write string to
44 * @outbuf_len: length of buffer
45 * @return 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
46 * current ethernet device
47 */
Patrice Chotard2373cba2019-11-25 09:07:37 +010048int format_mac_pxe(char *outbuf, size_t outbuf_len)
49{
50 uchar ethaddr[6];
51
52 if (outbuf_len < 21) {
53 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
54
Simon Glass18109cc2021-10-14 12:48:01 -060055 return -ENOSPC;
Patrice Chotard2373cba2019-11-25 09:07:37 +010056 }
57
58 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
59 return -ENOENT;
60
61 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
62 ethaddr[0], ethaddr[1], ethaddr[2],
63 ethaddr[3], ethaddr[4], ethaddr[5]);
64
65 return 1;
66}
67
Simon Glass18109cc2021-10-14 12:48:01 -060068/**
69 * get_bootfile_path() - Figure out the path of a file to read
70 *
71 * Returns the directory the file specified in the 'bootfile' env variable is
Patrice Chotard2373cba2019-11-25 09:07:37 +010072 * in. If bootfile isn't defined in the environment, return NULL, which should
73 * be interpreted as "don't prepend anything to paths".
Simon Glass18109cc2021-10-14 12:48:01 -060074 *
75 * @file_path: File path to read (relative to the PXE file)
76 * @bootfile_path: Place to put the bootfile path
77 * @bootfile_path_size: Size of @bootfile_path in bytes
78 * @allow_abs_path: true to allow an absolute path (where @file_path starts with
79 * '/', false to return an empty path (and success) in that case
80 * Returns 1 for success, -ENOSPC if bootfile_path_size is to small to hold the
81 * resulting path
Patrice Chotard2373cba2019-11-25 09:07:37 +010082 */
83static int get_bootfile_path(const char *file_path, char *bootfile_path,
Simon Glass8018b9a2021-10-14 12:47:59 -060084 size_t bootfile_path_size, bool allow_abs_path)
Patrice Chotard2373cba2019-11-25 09:07:37 +010085{
86 char *bootfile, *last_slash;
87 size_t path_len = 0;
88
89 /* Only syslinux allows absolute paths */
Simon Glass8018b9a2021-10-14 12:47:59 -060090 if (file_path[0] == '/' && allow_abs_path)
Patrice Chotard2373cba2019-11-25 09:07:37 +010091 goto ret;
92
93 bootfile = from_env("bootfile");
94
95 if (!bootfile)
96 goto ret;
97
98 last_slash = strrchr(bootfile, '/');
99
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100100 if (!last_slash)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100101 goto ret;
102
103 path_len = (last_slash - bootfile) + 1;
104
105 if (bootfile_path_size < path_len) {
106 printf("bootfile_path too small. (%zd < %zd)\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100107 bootfile_path_size, path_len);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100108
Simon Glass18109cc2021-10-14 12:48:01 -0600109 return -ENOSPC;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100110 }
111
112 strncpy(bootfile_path, bootfile, path_len);
113
114 ret:
115 bootfile_path[path_len] = '\0';
116
117 return 1;
118}
119
Simon Glass18109cc2021-10-14 12:48:01 -0600120/**
121 * get_relfile() - read a file relative to the PXE file
122 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100123 * As in pxelinux, paths to files referenced from files we retrieve are
124 * relative to the location of bootfile. get_relfile takes such a path and
125 * joins it with the bootfile path to get the full path to the target file. If
126 * the bootfile path is NULL, we use file_path as is.
127 *
Simon Glass18109cc2021-10-14 12:48:01 -0600128 * @ctx: PXE context
129 * @file_path: File path to read (relative to the PXE file)
130 * @file_addr: Address to load file to
131 * Returns 1 for success, or < 0 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100132 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600133static int get_relfile(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100134 unsigned long file_addr)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100135{
136 size_t path_len;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100137 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100138 char addr_buf[18];
139 int err;
140
Simon Glass8018b9a2021-10-14 12:47:59 -0600141 err = get_bootfile_path(file_path, relfile, sizeof(relfile),
142 ctx->allow_abs_path);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100143
144 if (err < 0)
145 return err;
146
147 path_len = strlen(file_path);
148 path_len += strlen(relfile);
149
150 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100151 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100152
153 return -ENAMETOOLONG;
154 }
155
156 strcat(relfile, file_path);
157
158 printf("Retrieving file: %s\n", relfile);
159
160 sprintf(addr_buf, "%lx", file_addr);
161
Simon Glassb1ead6b2021-10-14 12:47:57 -0600162 return ctx->getfile(ctx, relfile, addr_buf);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100163}
164
Simon Glass18109cc2021-10-14 12:48:01 -0600165/**
166 * get_pxe_file() - read a file
167 *
168 * The file is read and nul-terminated
169 *
170 * @ctx: PXE context
171 * @file_path: File path to read (relative to the PXE file)
172 * @file_addr: Address to load file to
173 * Returns 1 for success, or < 0 on error
174 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600175int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100176 unsigned long file_addr)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100177{
178 unsigned long config_file_size;
179 char *tftp_filesize;
180 int err;
181 char *buf;
182
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600183 err = get_relfile(ctx, file_path, file_addr);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100184
185 if (err < 0)
186 return err;
187
188 /*
189 * the file comes without a NUL byte at the end, so find out its size
190 * and add the NUL byte.
191 */
192 tftp_filesize = from_env("filesize");
193
194 if (!tftp_filesize)
195 return -ENOENT;
196
197 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
198 return -EINVAL;
199
200 buf = map_sysmem(file_addr + config_file_size, 1);
201 *buf = '\0';
202 unmap_sysmem(buf);
203
204 return 1;
205}
206
207#define PXELINUX_DIR "pxelinux.cfg/"
208
Simon Glass18109cc2021-10-14 12:48:01 -0600209/**
210 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
211 *
212 * @ctx: PXE context
213 * @file: Filename to process (relative to pxelinux.cfg/)
214 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
215 * or other value < 0 on other error
216 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600217int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100218 unsigned long pxefile_addr_r)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100219{
220 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100221 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100222
223 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
224 printf("path (%s%s) too long, skipping\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100225 PXELINUX_DIR, file);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100226 return -ENAMETOOLONG;
227 }
228
229 sprintf(path, PXELINUX_DIR "%s", file);
230
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600231 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100232}
233
Simon Glass18109cc2021-10-14 12:48:01 -0600234/**
235 * get_relfile_envaddr() - read a file to an address in an env var
236 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100237 * Wrapper to make it easier to store the file at file_path in the location
238 * specified by envaddr_name. file_path will be joined to the bootfile path,
239 * if any is specified.
240 *
Simon Glass18109cc2021-10-14 12:48:01 -0600241 * @ctx: PXE context
242 * @file_path: File path to read (relative to the PXE file)
243 * @envaddr_name: Name of environment variable which contains the address to
244 * load to
245 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
246 * environment variable, -EINVAL if its format is not valid hex, or other
247 * value < 0 on other error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100248 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600249static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100250 const char *envaddr_name)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100251{
252 unsigned long file_addr;
253 char *envaddr;
254
255 envaddr = from_env(envaddr_name);
256
257 if (!envaddr)
258 return -ENOENT;
259
260 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
261 return -EINVAL;
262
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600263 return get_relfile(ctx, file_path, file_addr);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100264}
265
Simon Glass18109cc2021-10-14 12:48:01 -0600266/**
267 * label_create() - crate a new PXE label
268 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100269 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
270 * result must be free()'d to reclaim the memory.
271 *
Simon Glass18109cc2021-10-14 12:48:01 -0600272 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard2373cba2019-11-25 09:07:37 +0100273 */
274static struct pxe_label *label_create(void)
275{
276 struct pxe_label *label;
277
278 label = malloc(sizeof(struct pxe_label));
279
280 if (!label)
281 return NULL;
282
283 memset(label, 0, sizeof(struct pxe_label));
284
285 return label;
286}
287
Simon Glass18109cc2021-10-14 12:48:01 -0600288/**
289 * label_destroy() - free the memory used by a pxe_label
290 *
291 * This frees @label itself as well as memory used by its name,
292 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
293 * they're non-NULL.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100294 *
295 * So - be sure to only use dynamically allocated memory for the members of
296 * the pxe_label struct, unless you want to clean it up first. These are
297 * currently only created by the pxe file parsing code.
Simon Glass18109cc2021-10-14 12:48:01 -0600298 *
299 * @label: Label to free
Patrice Chotard2373cba2019-11-25 09:07:37 +0100300 */
301static void label_destroy(struct pxe_label *label)
302{
303 if (label->name)
304 free(label->name);
305
306 if (label->kernel)
307 free(label->kernel);
308
309 if (label->config)
310 free(label->config);
311
312 if (label->append)
313 free(label->append);
314
315 if (label->initrd)
316 free(label->initrd);
317
318 if (label->fdt)
319 free(label->fdt);
320
321 if (label->fdtdir)
322 free(label->fdtdir);
323
Neil Armstrong69076df2021-01-20 09:54:53 +0100324 if (label->fdtoverlays)
325 free(label->fdtoverlays);
326
Patrice Chotard2373cba2019-11-25 09:07:37 +0100327 free(label);
328}
329
Simon Glass18109cc2021-10-14 12:48:01 -0600330/**
331 * label_print() - Print a label and its string members if they're defined
Patrice Chotard2373cba2019-11-25 09:07:37 +0100332 *
333 * This is passed as a callback to the menu code for displaying each
334 * menu entry.
Simon Glass18109cc2021-10-14 12:48:01 -0600335 *
336 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100337 */
338static void label_print(void *data)
339{
340 struct pxe_label *label = data;
341 const char *c = label->menu ? label->menu : label->name;
342
343 printf("%s:\t%s\n", label->num, c);
344}
345
Simon Glass18109cc2021-10-14 12:48:01 -0600346/**
347 * label_localboot() - Boot a label that specified 'localboot'
Patrice Chotard2373cba2019-11-25 09:07:37 +0100348 *
Simon Glass18109cc2021-10-14 12:48:01 -0600349 * This requires that the 'localcmd' environment variable is defined. Its
350 * contents will be executed as U-Boot commands. If the label specified an
351 * 'append' line, its contents will be used to overwrite the contents of the
352 * 'bootargs' environment variable prior to running 'localcmd'.
353 *
354 * @label: Label to process
355 * Returns 1 on success or < 0 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100356 */
357static int label_localboot(struct pxe_label *label)
358{
359 char *localcmd;
360
361 localcmd = from_env("localcmd");
362
363 if (!localcmd)
364 return -ENOENT;
365
366 if (label->append) {
367 char bootargs[CONFIG_SYS_CBSIZE];
368
Simon Glass1a62d642020-11-05 10:33:47 -0700369 cli_simple_process_macros(label->append, bootargs,
370 sizeof(bootargs));
Patrice Chotard2373cba2019-11-25 09:07:37 +0100371 env_set("bootargs", bootargs);
372 }
373
374 debug("running: %s\n", localcmd);
375
376 return run_command_list(localcmd, strlen(localcmd), 0);
377}
378
Simon Glass18109cc2021-10-14 12:48:01 -0600379/**
380 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
381 *
382 * @ctx: PXE context
383 * @label: Label to process
Neil Armstrong69076df2021-01-20 09:54:53 +0100384 */
385#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600386static void label_boot_fdtoverlay(struct pxe_context *ctx,
387 struct pxe_label *label)
Neil Armstrong69076df2021-01-20 09:54:53 +0100388{
389 char *fdtoverlay = label->fdtoverlays;
390 struct fdt_header *working_fdt;
391 char *fdtoverlay_addr_env;
392 ulong fdtoverlay_addr;
393 ulong fdt_addr;
394 int err;
395
396 /* Get the main fdt and map it */
Simon Glass7e5f4602021-07-24 09:03:29 -0600397 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100398 working_fdt = map_sysmem(fdt_addr, 0);
399 err = fdt_check_header(working_fdt);
400 if (err)
401 return;
402
403 /* Get the specific overlay loading address */
404 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
405 if (!fdtoverlay_addr_env) {
406 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
407 return;
408 }
409
Simon Glass7e5f4602021-07-24 09:03:29 -0600410 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100411
412 /* Cycle over the overlay files and apply them in order */
413 do {
414 struct fdt_header *blob;
415 char *overlayfile;
416 char *end;
417 int len;
418
419 /* Drop leading spaces */
420 while (*fdtoverlay == ' ')
421 ++fdtoverlay;
422
423 /* Copy a single filename if multiple provided */
424 end = strstr(fdtoverlay, " ");
425 if (end) {
426 len = (int)(end - fdtoverlay);
427 overlayfile = malloc(len + 1);
428 strncpy(overlayfile, fdtoverlay, len);
429 overlayfile[len] = '\0';
430 } else
431 overlayfile = fdtoverlay;
432
433 if (!strlen(overlayfile))
434 goto skip_overlay;
435
436 /* Load overlay file */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600437 err = get_relfile_envaddr(ctx, overlayfile,
Neil Armstrong69076df2021-01-20 09:54:53 +0100438 "fdtoverlay_addr_r");
439 if (err < 0) {
440 printf("Failed loading overlay %s\n", overlayfile);
441 goto skip_overlay;
442 }
443
444 /* Resize main fdt */
445 fdt_shrink_to_minimum(working_fdt, 8192);
446
447 blob = map_sysmem(fdtoverlay_addr, 0);
448 err = fdt_check_header(blob);
449 if (err) {
450 printf("Invalid overlay %s, skipping\n",
451 overlayfile);
452 goto skip_overlay;
453 }
454
455 err = fdt_overlay_apply_verbose(working_fdt, blob);
456 if (err) {
457 printf("Failed to apply overlay %s, skipping\n",
458 overlayfile);
459 goto skip_overlay;
460 }
461
462skip_overlay:
463 if (end)
464 free(overlayfile);
465 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
466}
467#endif
468
Simon Glass18109cc2021-10-14 12:48:01 -0600469/**
470 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard2373cba2019-11-25 09:07:37 +0100471 *
472 * If we can't boot for any reason, we return. A successful boot never
473 * returns.
474 *
475 * The kernel will be stored in the location given by the 'kernel_addr_r'
476 * environment variable.
477 *
478 * If the label specifies an initrd file, it will be stored in the location
479 * given by the 'ramdisk_addr_r' environment variable.
480 *
481 * If the label specifies an 'append' line, its contents will overwrite that
482 * of the 'bootargs' environment variable.
Simon Glass18109cc2021-10-14 12:48:01 -0600483 *
484 * @ctx: PXE context
485 * @label: Label to process
486 * Returns does not return on success, otherwise returns 0 if a localboot
487 * label was processed, or 1 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100488 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600489static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100490{
491 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700492 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700493 char *kernel_addr = NULL;
494 char *initrd_addr_str = NULL;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700495 char initrd_filesize[10];
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700496 char initrd_str[28];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100497 char mac_str[29] = "";
498 char ip_str[68] = "";
499 char *fit_addr = NULL;
500 int bootm_argc = 2;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700501 int zboot_argc = 3;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100502 int len = 0;
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700503 ulong kernel_addr_r;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100504 void *buf;
505
506 label_print(label);
507
508 label->attempted = 1;
509
510 if (label->localboot) {
511 if (label->localboot_val >= 0)
512 label_localboot(label);
513 return 0;
514 }
515
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100516 if (!label->kernel) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100517 printf("No kernel given, skipping %s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100518 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100519 return 1;
520 }
521
522 if (label->initrd) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600523 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100524 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100525 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100526 return 1;
527 }
528
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700529 initrd_addr_str = env_get("ramdisk_addr_r");
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700530 strncpy(initrd_filesize, env_get("filesize"), 9);
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700531
532 strncpy(initrd_str, initrd_addr_str, 18);
533 strcat(initrd_str, ":");
534 strncat(initrd_str, initrd_filesize, 9);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100535 }
536
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600537 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100538 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100539 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100540 return 1;
541 }
542
543 if (label->ipappend & 0x1) {
544 sprintf(ip_str, " ip=%s:%s:%s:%s",
545 env_get("ipaddr"), env_get("serverip"),
546 env_get("gatewayip"), env_get("netmask"));
547 }
548
Kory Maincentff0287e2021-02-02 16:42:28 +0100549 if (IS_ENABLED(CONFIG_CMD_NET)) {
550 if (label->ipappend & 0x2) {
551 int err;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100552
Kory Maincentff0287e2021-02-02 16:42:28 +0100553 strcpy(mac_str, " BOOTIF=");
554 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
555 if (err < 0)
556 mac_str[0] = '\0';
557 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100558 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100559
560 if ((label->ipappend & 0x3) || label->append) {
561 char bootargs[CONFIG_SYS_CBSIZE] = "";
562 char finalbootargs[CONFIG_SYS_CBSIZE];
563
564 if (strlen(label->append ?: "") +
565 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
566 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
567 strlen(label->append ?: ""),
568 strlen(ip_str), strlen(mac_str),
569 sizeof(bootargs));
570 return 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100571 }
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100572
573 if (label->append)
574 strncpy(bootargs, label->append, sizeof(bootargs));
575
576 strcat(bootargs, ip_str);
577 strcat(bootargs, mac_str);
578
Simon Glass1a62d642020-11-05 10:33:47 -0700579 cli_simple_process_macros(bootargs, finalbootargs,
580 sizeof(finalbootargs));
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100581 env_set("bootargs", finalbootargs);
582 printf("append: %s\n", finalbootargs);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100583 }
584
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700585 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700586
Patrice Chotard2373cba2019-11-25 09:07:37 +0100587 /* for FIT, append the configuration identifier */
588 if (label->config) {
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700589 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100590
591 fit_addr = malloc(len);
592 if (!fit_addr) {
593 printf("malloc fail (FIT address)\n");
594 return 1;
595 }
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700596 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
597 kernel_addr = fit_addr;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100598 }
599
600 /*
601 * fdt usage is optional:
Anton Leontievdb366742019-09-03 10:52:24 +0300602 * It handles the following scenarios.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100603 *
Anton Leontievdb366742019-09-03 10:52:24 +0300604 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
605 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
606 * bootm, and adjust argc appropriately.
607 *
608 * If retrieve fails and no exact fdt blob is specified in pxe file with
609 * "fdt" label, try Scenario 2.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100610 *
611 * Scenario 2: If there is an fdt_addr specified, pass it along to
612 * bootm, and adjust argc appropriately.
613 *
614 * Scenario 3: fdt blob is not available.
615 */
616 bootm_argv[3] = env_get("fdt_addr_r");
617
618 /* if fdt label is defined then get fdt from server */
619 if (bootm_argv[3]) {
620 char *fdtfile = NULL;
621 char *fdtfilefree = NULL;
622
623 if (label->fdt) {
624 fdtfile = label->fdt;
625 } else if (label->fdtdir) {
626 char *f1, *f2, *f3, *f4, *slash;
627
628 f1 = env_get("fdtfile");
629 if (f1) {
630 f2 = "";
631 f3 = "";
632 f4 = "";
633 } else {
634 /*
635 * For complex cases where this code doesn't
636 * generate the correct filename, the board
637 * code should set $fdtfile during early boot,
638 * or the boot scripts should set $fdtfile
639 * before invoking "pxe" or "sysboot".
640 */
641 f1 = env_get("soc");
642 f2 = "-";
643 f3 = env_get("board");
644 f4 = ".dtb";
Dimitri John Ledkov75efe7d2021-04-21 12:42:01 +0100645 if (!f1) {
646 f1 = "";
647 f2 = "";
648 }
649 if (!f3) {
650 f2 = "";
651 f3 = "";
652 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100653 }
654
655 len = strlen(label->fdtdir);
656 if (!len)
657 slash = "./";
658 else if (label->fdtdir[len - 1] != '/')
659 slash = "/";
660 else
661 slash = "";
662
663 len = strlen(label->fdtdir) + strlen(slash) +
664 strlen(f1) + strlen(f2) + strlen(f3) +
665 strlen(f4) + 1;
666 fdtfilefree = malloc(len);
667 if (!fdtfilefree) {
668 printf("malloc fail (FDT filename)\n");
669 goto cleanup;
670 }
671
672 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
673 label->fdtdir, slash, f1, f2, f3, f4);
674 fdtfile = fdtfilefree;
675 }
676
677 if (fdtfile) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600678 int err = get_relfile_envaddr(ctx, fdtfile,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100679 "fdt_addr_r");
680
Patrice Chotard2373cba2019-11-25 09:07:37 +0100681 free(fdtfilefree);
682 if (err < 0) {
Anton Leontievdb366742019-09-03 10:52:24 +0300683 bootm_argv[3] = NULL;
684
685 if (label->fdt) {
686 printf("Skipping %s for failure retrieving FDT\n",
687 label->name);
688 goto cleanup;
689 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100690 }
Neil Armstrong69076df2021-01-20 09:54:53 +0100691
692#ifdef CONFIG_OF_LIBFDT_OVERLAY
693 if (label->fdtoverlays)
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600694 label_boot_fdtoverlay(ctx, label);
Neil Armstrong69076df2021-01-20 09:54:53 +0100695#endif
Patrice Chotard2373cba2019-11-25 09:07:37 +0100696 } else {
697 bootm_argv[3] = NULL;
698 }
699 }
700
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700701 bootm_argv[1] = kernel_addr;
702 zboot_argv[1] = kernel_addr;
703
704 if (initrd_addr_str) {
705 bootm_argv[2] = initrd_str;
706 bootm_argc = 3;
707
708 zboot_argv[3] = initrd_addr_str;
709 zboot_argv[4] = initrd_filesize;
710 zboot_argc = 5;
711 }
712
Patrice Chotard2373cba2019-11-25 09:07:37 +0100713 if (!bootm_argv[3])
714 bootm_argv[3] = env_get("fdt_addr");
715
716 if (bootm_argv[3]) {
717 if (!bootm_argv[2])
718 bootm_argv[2] = "-";
719 bootm_argc = 4;
720 }
721
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700722 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
723 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100724 /* Try bootm for legacy and FIT format image */
725 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600726 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100727 /* Try booting an AArch64 Linux kernel image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100728 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600729 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100730 /* Try booting a Image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100731 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600732 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincent18c25822021-02-02 16:42:29 +0100733 /* Try booting an x86_64 Linux kernel image */
734 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600735 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentff0287e2021-02-02 16:42:28 +0100736
Patrice Chotard2373cba2019-11-25 09:07:37 +0100737 unmap_sysmem(buf);
738
739cleanup:
740 if (fit_addr)
741 free(fit_addr);
742 return 1;
743}
744
Simon Glass18109cc2021-10-14 12:48:01 -0600745/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100746enum token_type {
747 T_EOL,
748 T_STRING,
749 T_EOF,
750 T_MENU,
751 T_TITLE,
752 T_TIMEOUT,
753 T_LABEL,
754 T_KERNEL,
755 T_LINUX,
756 T_APPEND,
757 T_INITRD,
758 T_LOCALBOOT,
759 T_DEFAULT,
760 T_PROMPT,
761 T_INCLUDE,
762 T_FDT,
763 T_FDTDIR,
Neil Armstrong69076df2021-01-20 09:54:53 +0100764 T_FDTOVERLAYS,
Patrice Chotard2373cba2019-11-25 09:07:37 +0100765 T_ONTIMEOUT,
766 T_IPAPPEND,
767 T_BACKGROUND,
768 T_INVALID
769};
770
Simon Glass18109cc2021-10-14 12:48:01 -0600771/** struct token - token - given by a value and a type */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100772struct token {
773 char *val;
774 enum token_type type;
775};
776
Simon Glass18109cc2021-10-14 12:48:01 -0600777/* Keywords recognized */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100778static const struct token keywords[] = {
779 {"menu", T_MENU},
780 {"title", T_TITLE},
781 {"timeout", T_TIMEOUT},
782 {"default", T_DEFAULT},
783 {"prompt", T_PROMPT},
784 {"label", T_LABEL},
785 {"kernel", T_KERNEL},
786 {"linux", T_LINUX},
787 {"localboot", T_LOCALBOOT},
788 {"append", T_APPEND},
789 {"initrd", T_INITRD},
790 {"include", T_INCLUDE},
791 {"devicetree", T_FDT},
792 {"fdt", T_FDT},
793 {"devicetreedir", T_FDTDIR},
794 {"fdtdir", T_FDTDIR},
Neil Armstrong69076df2021-01-20 09:54:53 +0100795 {"fdtoverlays", T_FDTOVERLAYS},
Patrice Chotard2373cba2019-11-25 09:07:37 +0100796 {"ontimeout", T_ONTIMEOUT,},
797 {"ipappend", T_IPAPPEND,},
798 {"background", T_BACKGROUND,},
799 {NULL, T_INVALID}
800};
801
Simon Glass18109cc2021-10-14 12:48:01 -0600802/**
803 * enum lex_state - lexer state
804 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100805 * Since pxe(linux) files don't have a token to identify the start of a
806 * literal, we have to keep track of when we're in a state where a literal is
807 * expected vs when we're in a state a keyword is expected.
808 */
809enum lex_state {
810 L_NORMAL = 0,
811 L_KEYWORD,
812 L_SLITERAL
813};
814
Simon Glass18109cc2021-10-14 12:48:01 -0600815/**
816 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100817 *
Simon Glass18109cc2021-10-14 12:48:01 -0600818 * This is used for scanning both string literals and keywords.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100819 *
820 * Characters from *p are copied into t-val until a character equal to
821 * delim is found, or a NUL byte is reached. If delim has the special value of
822 * ' ', any whitespace character will be used as a delimiter.
823 *
824 * If lower is unequal to 0, uppercase characters will be converted to
825 * lowercase in the result. This is useful to make keywords case
826 * insensitive.
827 *
828 * The location of *p is updated to point to the first character after the end
829 * of the token - the ending delimiter.
830 *
Simon Glass18109cc2021-10-14 12:48:01 -0600831 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
832 * it.
833 *
834 * @p: Points to a pointer to the current position in the input being processed.
835 * Updated to point at the first character after the current token
836 * @t: Pointers to a token to fill in
837 * @delim: Delimiter character to look for, either newline or space
838 * @lower: true to convert the string to lower case when storing
839 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard2373cba2019-11-25 09:07:37 +0100840 */
841static char *get_string(char **p, struct token *t, char delim, int lower)
842{
843 char *b, *e;
844 size_t len, i;
845
846 /*
847 * b and e both start at the beginning of the input stream.
848 *
849 * e is incremented until we find the ending delimiter, or a NUL byte
850 * is reached. Then, we take e - b to find the length of the token.
851 */
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100852 b = *p;
853 e = *p;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100854
855 while (*e) {
856 if ((delim == ' ' && isspace(*e)) || delim == *e)
857 break;
858 e++;
859 }
860
861 len = e - b;
862
863 /*
864 * Allocate memory to hold the string, and copy it in, converting
865 * characters to lowercase if lower is != 0.
866 */
867 t->val = malloc(len + 1);
868 if (!t->val)
869 return NULL;
870
871 for (i = 0; i < len; i++, b++) {
872 if (lower)
873 t->val[i] = tolower(*b);
874 else
875 t->val[i] = *b;
876 }
877
878 t->val[len] = '\0';
879
880 /*
881 * Update *p so the caller knows where to continue scanning.
882 */
883 *p = e;
884
885 t->type = T_STRING;
886
887 return t->val;
888}
889
Simon Glass18109cc2021-10-14 12:48:01 -0600890/**
891 * get_keyword() - Populate a keyword token with a type and value
892 *
893 * Updates the ->type field based on the keyword string in @val
894 * @t: Token to populate
Patrice Chotard2373cba2019-11-25 09:07:37 +0100895 */
896static void get_keyword(struct token *t)
897{
898 int i;
899
900 for (i = 0; keywords[i].val; i++) {
901 if (!strcmp(t->val, keywords[i].val)) {
902 t->type = keywords[i].type;
903 break;
904 }
905 }
906}
907
Simon Glass18109cc2021-10-14 12:48:01 -0600908/**
909 * get_token() - Get the next token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100910 *
Simon Glass18109cc2021-10-14 12:48:01 -0600911 * We have to keep track of which state we're in to know if we're looking to get
912 * a string literal or a keyword.
913 *
914 * @p: Points to a pointer to the current position in the input being processed.
915 * Updated to point at the first character after the current token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100916 */
917static void get_token(char **p, struct token *t, enum lex_state state)
918{
919 char *c = *p;
920
921 t->type = T_INVALID;
922
923 /* eat non EOL whitespace */
924 while (isblank(*c))
925 c++;
926
927 /*
928 * eat comments. note that string literals can't begin with #, but
929 * can contain a # after their first character.
930 */
931 if (*c == '#') {
932 while (*c && *c != '\n')
933 c++;
934 }
935
936 if (*c == '\n') {
937 t->type = T_EOL;
938 c++;
939 } else if (*c == '\0') {
940 t->type = T_EOF;
941 c++;
942 } else if (state == L_SLITERAL) {
943 get_string(&c, t, '\n', 0);
944 } else if (state == L_KEYWORD) {
945 /*
946 * when we expect a keyword, we first get the next string
947 * token delimited by whitespace, and then check if it
948 * matches a keyword in our keyword list. if it does, it's
949 * converted to a keyword token of the appropriate type, and
950 * if not, it remains a string token.
951 */
952 get_string(&c, t, ' ', 1);
953 get_keyword(t);
954 }
955
956 *p = c;
957}
958
Simon Glass18109cc2021-10-14 12:48:01 -0600959/**
960 * eol_or_eof() - Find end of line
961 *
962 * Increment *c until we get to the end of the current line, or EOF
963 *
964 * @c: Points to a pointer to the current position in the input being processed.
965 * Updated to point at the first character after the current token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100966 */
967static void eol_or_eof(char **c)
968{
969 while (**c && **c != '\n')
970 (*c)++;
971}
972
973/*
974 * All of these parse_* functions share some common behavior.
975 *
976 * They finish with *c pointing after the token they parse, and return 1 on
977 * success, or < 0 on error.
978 */
979
980/*
981 * Parse a string literal and store a pointer it at *dst. String literals
982 * terminate at the end of the line.
983 */
984static int parse_sliteral(char **c, char **dst)
985{
986 struct token t;
987 char *s = *c;
988
989 get_token(c, &t, L_SLITERAL);
990
991 if (t.type != T_STRING) {
992 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
993 return -EINVAL;
994 }
995
996 *dst = t.val;
997
998 return 1;
999}
1000
1001/*
1002 * Parse a base 10 (unsigned) integer and store it at *dst.
1003 */
1004static int parse_integer(char **c, int *dst)
1005{
1006 struct token t;
1007 char *s = *c;
1008
1009 get_token(c, &t, L_SLITERAL);
1010
1011 if (t.type != T_STRING) {
1012 printf("Expected string: %.*s\n", (int)(*c - s), s);
1013 return -EINVAL;
1014 }
1015
1016 *dst = simple_strtol(t.val, NULL, 10);
1017
1018 free(t.val);
1019
1020 return 1;
1021}
1022
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001023static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001024 struct pxe_menu *cfg, int nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001025
1026/*
1027 * Parse an include statement, and retrieve and parse the file it mentions.
1028 *
1029 * base should point to a location where it's safe to store the file, and
1030 * nest_level should indicate how many nested includes have occurred. For this
1031 * include, nest_level has already been incremented and doesn't need to be
1032 * incremented here.
1033 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001034static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001035 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001036{
1037 char *include_path;
1038 char *s = *c;
1039 int err;
1040 char *buf;
1041 int ret;
1042
1043 err = parse_sliteral(c, &include_path);
1044
1045 if (err < 0) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001046 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001047 return err;
1048 }
1049
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001050 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001051
1052 if (err < 0) {
1053 printf("Couldn't retrieve %s\n", include_path);
1054 return err;
1055 }
1056
1057 buf = map_sysmem(base, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001058 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001059 unmap_sysmem(buf);
1060
1061 return ret;
1062}
1063
1064/*
1065 * Parse lines that begin with 'menu'.
1066 *
1067 * base and nest are provided to handle the 'menu include' case.
1068 *
1069 * base should point to a location where it's safe to store the included file.
1070 *
1071 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1072 * a file it includes, 3 when parsing a file included by that file, and so on.
1073 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001074static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001075 unsigned long base, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001076{
1077 struct token t;
1078 char *s = *c;
1079 int err = 0;
1080
1081 get_token(c, &t, L_KEYWORD);
1082
1083 switch (t.type) {
1084 case T_TITLE:
1085 err = parse_sliteral(c, &cfg->title);
1086
1087 break;
1088
1089 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001090 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001091 break;
1092
1093 case T_BACKGROUND:
1094 err = parse_sliteral(c, &cfg->bmp);
1095 break;
1096
1097 default:
1098 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001099 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001100 }
1101
1102 if (err < 0)
1103 return err;
1104
1105 eol_or_eof(c);
1106
1107 return 1;
1108}
1109
1110/*
1111 * Handles parsing a 'menu line' when we're parsing a label.
1112 */
1113static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001114 struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001115{
1116 struct token t;
1117 char *s;
1118
1119 s = *c;
1120
1121 get_token(c, &t, L_KEYWORD);
1122
1123 switch (t.type) {
1124 case T_DEFAULT:
1125 if (!cfg->default_label)
1126 cfg->default_label = strdup(label->name);
1127
1128 if (!cfg->default_label)
1129 return -ENOMEM;
1130
1131 break;
1132 case T_LABEL:
1133 parse_sliteral(c, &label->menu);
1134 break;
1135 default:
1136 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001137 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001138 }
1139
1140 eol_or_eof(c);
1141
1142 return 0;
1143}
1144
1145/*
1146 * Handles parsing a 'kernel' label.
1147 * expecting "filename" or "<fit_filename>#cfg"
1148 */
1149static int parse_label_kernel(char **c, struct pxe_label *label)
1150{
1151 char *s;
1152 int err;
1153
1154 err = parse_sliteral(c, &label->kernel);
1155 if (err < 0)
1156 return err;
1157
1158 s = strstr(label->kernel, "#");
1159 if (!s)
1160 return 1;
1161
1162 label->config = malloc(strlen(s) + 1);
1163 if (!label->config)
1164 return -ENOMEM;
1165
1166 strcpy(label->config, s);
1167 *s = 0;
1168
1169 return 1;
1170}
1171
1172/*
1173 * Parses a label and adds it to the list of labels for a menu.
1174 *
1175 * A label ends when we either get to the end of a file, or
1176 * get some input we otherwise don't have a handler defined
1177 * for.
1178 *
1179 */
1180static int parse_label(char **c, struct pxe_menu *cfg)
1181{
1182 struct token t;
1183 int len;
1184 char *s = *c;
1185 struct pxe_label *label;
1186 int err;
1187
1188 label = label_create();
1189 if (!label)
1190 return -ENOMEM;
1191
1192 err = parse_sliteral(c, &label->name);
1193 if (err < 0) {
1194 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1195 label_destroy(label);
1196 return -EINVAL;
1197 }
1198
1199 list_add_tail(&label->list, &cfg->labels);
1200
1201 while (1) {
1202 s = *c;
1203 get_token(c, &t, L_KEYWORD);
1204
1205 err = 0;
1206 switch (t.type) {
1207 case T_MENU:
1208 err = parse_label_menu(c, cfg, label);
1209 break;
1210
1211 case T_KERNEL:
1212 case T_LINUX:
1213 err = parse_label_kernel(c, label);
1214 break;
1215
1216 case T_APPEND:
1217 err = parse_sliteral(c, &label->append);
1218 if (label->initrd)
1219 break;
1220 s = strstr(label->append, "initrd=");
1221 if (!s)
1222 break;
1223 s += 7;
1224 len = (int)(strchr(s, ' ') - s);
1225 label->initrd = malloc(len + 1);
1226 strncpy(label->initrd, s, len);
1227 label->initrd[len] = '\0';
1228
1229 break;
1230
1231 case T_INITRD:
1232 if (!label->initrd)
1233 err = parse_sliteral(c, &label->initrd);
1234 break;
1235
1236 case T_FDT:
1237 if (!label->fdt)
1238 err = parse_sliteral(c, &label->fdt);
1239 break;
1240
1241 case T_FDTDIR:
1242 if (!label->fdtdir)
1243 err = parse_sliteral(c, &label->fdtdir);
1244 break;
1245
Neil Armstrong69076df2021-01-20 09:54:53 +01001246 case T_FDTOVERLAYS:
1247 if (!label->fdtoverlays)
1248 err = parse_sliteral(c, &label->fdtoverlays);
1249 break;
1250
Patrice Chotard2373cba2019-11-25 09:07:37 +01001251 case T_LOCALBOOT:
1252 label->localboot = 1;
1253 err = parse_integer(c, &label->localboot_val);
1254 break;
1255
1256 case T_IPAPPEND:
1257 err = parse_integer(c, &label->ipappend);
1258 break;
1259
1260 case T_EOL:
1261 break;
1262
1263 default:
1264 /*
1265 * put the token back! we don't want it - it's the end
1266 * of a label and whatever token this is, it's
1267 * something for the menu level context to handle.
1268 */
1269 *c = s;
1270 return 1;
1271 }
1272
1273 if (err < 0)
1274 return err;
1275 }
1276}
1277
1278/*
1279 * This 16 comes from the limit pxelinux imposes on nested includes.
1280 *
1281 * There is no reason at all we couldn't do more, but some limit helps prevent
1282 * infinite (until crash occurs) recursion if a file tries to include itself.
1283 */
1284#define MAX_NEST_LEVEL 16
1285
1286/*
1287 * Entry point for parsing a menu file. nest_level indicates how many times
1288 * we've nested in includes. It will be 1 for the top level menu file.
1289 *
1290 * Returns 1 on success, < 0 on error.
1291 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001292static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001293 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001294{
1295 struct token t;
1296 char *s, *b, *label_name;
1297 int err;
1298
1299 b = p;
1300
1301 if (nest_level > MAX_NEST_LEVEL) {
1302 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1303 return -EMLINK;
1304 }
1305
1306 while (1) {
1307 s = p;
1308
1309 get_token(&p, &t, L_KEYWORD);
1310
1311 err = 0;
1312 switch (t.type) {
1313 case T_MENU:
1314 cfg->prompt = 1;
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001315 err = parse_menu(ctx, &p, cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001316 base + ALIGN(strlen(b) + 1, 4),
1317 nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001318 break;
1319
1320 case T_TIMEOUT:
1321 err = parse_integer(&p, &cfg->timeout);
1322 break;
1323
1324 case T_LABEL:
1325 err = parse_label(&p, cfg);
1326 break;
1327
1328 case T_DEFAULT:
1329 case T_ONTIMEOUT:
1330 err = parse_sliteral(&p, &label_name);
1331
1332 if (label_name) {
1333 if (cfg->default_label)
1334 free(cfg->default_label);
1335
1336 cfg->default_label = label_name;
1337 }
1338
1339 break;
1340
1341 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001342 err = handle_include(ctx, &p,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001343 base + ALIGN(strlen(b), 4), cfg,
1344 nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001345 break;
1346
1347 case T_PROMPT:
1348 eol_or_eof(&p);
1349 break;
1350
1351 case T_EOL:
1352 break;
1353
1354 case T_EOF:
1355 return 1;
1356
1357 default:
1358 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001359 (int)(p - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001360 eol_or_eof(&p);
1361 }
1362
1363 if (err < 0)
1364 return err;
1365 }
1366}
1367
1368/*
Patrice Chotard2373cba2019-11-25 09:07:37 +01001369 */
1370void destroy_pxe_menu(struct pxe_menu *cfg)
1371{
1372 struct list_head *pos, *n;
1373 struct pxe_label *label;
1374
1375 if (cfg->title)
1376 free(cfg->title);
1377
1378 if (cfg->default_label)
1379 free(cfg->default_label);
1380
1381 list_for_each_safe(pos, n, &cfg->labels) {
1382 label = list_entry(pos, struct pxe_label, list);
1383
1384 label_destroy(label);
1385 }
1386
1387 free(cfg);
1388}
1389
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001390struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001391{
1392 struct pxe_menu *cfg;
1393 char *buf;
1394 int r;
1395
1396 cfg = malloc(sizeof(struct pxe_menu));
1397
1398 if (!cfg)
1399 return NULL;
1400
1401 memset(cfg, 0, sizeof(struct pxe_menu));
1402
1403 INIT_LIST_HEAD(&cfg->labels);
1404
1405 buf = map_sysmem(menucfg, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001406 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001407 unmap_sysmem(buf);
1408
1409 if (r < 0) {
1410 destroy_pxe_menu(cfg);
1411 return NULL;
1412 }
1413
1414 return cfg;
1415}
1416
1417/*
1418 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1419 * menu code.
1420 */
1421static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1422{
1423 struct pxe_label *label;
1424 struct list_head *pos;
1425 struct menu *m;
1426 int err;
1427 int i = 1;
1428 char *default_num = NULL;
1429
1430 /*
1431 * Create a menu and add items for all the labels.
1432 */
1433 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -07001434 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001435
1436 if (!m)
1437 return NULL;
1438
1439 list_for_each(pos, &cfg->labels) {
1440 label = list_entry(pos, struct pxe_label, list);
1441
1442 sprintf(label->num, "%d", i++);
1443 if (menu_item_add(m, label->num, label) != 1) {
1444 menu_destroy(m);
1445 return NULL;
1446 }
1447 if (cfg->default_label &&
1448 (strcmp(label->name, cfg->default_label) == 0))
1449 default_num = label->num;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001450 }
1451
1452 /*
1453 * After we've created items for each label in the menu, set the
1454 * menu's default label if one was specified.
1455 */
1456 if (default_num) {
1457 err = menu_default_set(m, default_num);
1458 if (err != 1) {
1459 if (err != -ENOENT) {
1460 menu_destroy(m);
1461 return NULL;
1462 }
1463
1464 printf("Missing default: %s\n", cfg->default_label);
1465 }
1466 }
1467
1468 return m;
1469}
1470
1471/*
1472 * Try to boot any labels we have yet to attempt to boot.
1473 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001474static void boot_unattempted_labels(struct pxe_context *ctx,
1475 struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001476{
1477 struct list_head *pos;
1478 struct pxe_label *label;
1479
1480 list_for_each(pos, &cfg->labels) {
1481 label = list_entry(pos, struct pxe_label, list);
1482
1483 if (!label->attempted)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001484 label_boot(ctx, label);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001485 }
1486}
1487
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001488void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001489{
1490 void *choice;
1491 struct menu *m;
1492 int err;
1493
Kory Maincentff0287e2021-02-02 16:42:28 +01001494 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1495 /* display BMP if available */
1496 if (cfg->bmp) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001497 if (get_relfile(ctx, cfg->bmp, image_load_addr)) {
Kory Maincentff0287e2021-02-02 16:42:28 +01001498 if (CONFIG_IS_ENABLED(CMD_CLS))
1499 run_command("cls", 0);
1500 bmp_display(image_load_addr,
1501 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1502 } else {
1503 printf("Skipping background bmp %s for failure\n",
1504 cfg->bmp);
1505 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001506 }
1507 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001508
1509 m = pxe_menu_to_menu(cfg);
1510 if (!m)
1511 return;
1512
1513 err = menu_get_choice(m, &choice);
1514
1515 menu_destroy(m);
1516
1517 /*
1518 * err == 1 means we got a choice back from menu_get_choice.
1519 *
1520 * err == -ENOENT if the menu was setup to select the default but no
1521 * default was set. in that case, we should continue trying to boot
1522 * labels that haven't been attempted yet.
1523 *
1524 * otherwise, the user interrupted or there was some other error and
1525 * we give up.
1526 */
1527
1528 if (err == 1) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001529 err = label_boot(ctx, choice);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001530 if (!err)
1531 return;
1532 } else if (err != -ENOENT) {
1533 return;
1534 }
1535
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001536 boot_unattempted_labels(ctx, cfg);
1537}
1538
Simon Glassb1ead6b2021-10-14 12:47:57 -06001539void pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
Simon Glass8018b9a2021-10-14 12:47:59 -06001540 pxe_getfile_func getfile, void *userdata,
1541 bool allow_abs_path)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001542{
1543 ctx->cmdtp = cmdtp;
Simon Glassb1ead6b2021-10-14 12:47:57 -06001544 ctx->getfile = getfile;
Simon Glass4ad5d512021-10-14 12:47:58 -06001545 ctx->userdata = userdata;
Simon Glass8018b9a2021-10-14 12:47:59 -06001546 ctx->allow_abs_path = allow_abs_path;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001547}