blob: 96528aa14c035e3f402bf14a77efa83a0b5c0fd5 [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>
Patrick Delaunaye6fe02a2022-03-22 17:08:43 +01009#include <dm.h>
Patrice Chotard2373cba2019-11-25 09:07:37 +010010#include <env.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070011#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Patrice Chotard2373cba2019-11-25 09:07:37 +010013#include <malloc.h>
14#include <mapmem.h>
Simon Glass90526e92020-05-10 11:39:56 -060015#include <net.h>
Neil Armstrong69076df2021-01-20 09:54:53 +010016#include <fdt_support.h>
Patrick Delaunaye6fe02a2022-03-22 17:08:43 +010017#include <video.h>
Neil Armstrong69076df2021-01-20 09:54:53 +010018#include <linux/libfdt.h>
Patrice Chotard2373cba2019-11-25 09:07:37 +010019#include <linux/string.h>
20#include <linux/ctype.h>
21#include <errno.h>
22#include <linux/list.h>
23
Zhang Ning02901462022-02-01 08:33:37 +080024#ifdef CONFIG_DM_RNG
Zhang Ning02901462022-02-01 08:33:37 +080025#include <rng.h>
26#endif
27
Patrice Chotard2373cba2019-11-25 09:07:37 +010028#include <splash.h>
29#include <asm/io.h>
30
31#include "menu.h"
32#include "cli.h"
33
34#include "pxe_utils.h"
35
Ben Wolsieffer2c4e0672019-11-28 00:07:08 -050036#define MAX_TFTP_PATH_LEN 512
Patrice Chotard2373cba2019-11-25 09:07:37 +010037
Simon Glass4d79e882021-10-14 12:48:08 -060038int pxe_get_file_size(ulong *sizep)
39{
40 const char *val;
41
42 val = from_env("filesize");
43 if (!val)
44 return -ENOENT;
45
46 if (strict_strtoul(val, 16, sizep) < 0)
47 return -EINVAL;
48
49 return 0;
50}
51
Simon Glass18109cc2021-10-14 12:48:01 -060052/**
53 * format_mac_pxe() - obtain a MAC address in the PXE format
54 *
55 * This produces a MAC-address string in the format for the current ethernet
56 * device:
57 *
58 * 01-aa-bb-cc-dd-ee-ff
59 *
60 * where aa-ff is the MAC address in hex
61 *
62 * @outbuf: Buffer to write string to
63 * @outbuf_len: length of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010064 * Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
Simon Glass18109cc2021-10-14 12:48:01 -060065 * current ethernet device
66 */
Patrice Chotard2373cba2019-11-25 09:07:37 +010067int format_mac_pxe(char *outbuf, size_t outbuf_len)
68{
69 uchar ethaddr[6];
70
71 if (outbuf_len < 21) {
72 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Simon Glass18109cc2021-10-14 12:48:01 -060073 return -ENOSPC;
Patrice Chotard2373cba2019-11-25 09:07:37 +010074 }
75
76 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
77 return -ENOENT;
78
79 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
80 ethaddr[0], ethaddr[1], ethaddr[2],
81 ethaddr[3], ethaddr[4], ethaddr[5]);
82
83 return 1;
84}
85
Simon Glass18109cc2021-10-14 12:48:01 -060086/**
Simon Glass18109cc2021-10-14 12:48:01 -060087 * get_relfile() - read a file relative to the PXE file
88 *
Patrice Chotard2373cba2019-11-25 09:07:37 +010089 * As in pxelinux, paths to files referenced from files we retrieve are
90 * relative to the location of bootfile. get_relfile takes such a path and
91 * joins it with the bootfile path to get the full path to the target file. If
92 * the bootfile path is NULL, we use file_path as is.
93 *
Simon Glass18109cc2021-10-14 12:48:01 -060094 * @ctx: PXE context
95 * @file_path: File path to read (relative to the PXE file)
96 * @file_addr: Address to load file to
Simon Glass4d79e882021-10-14 12:48:08 -060097 * @filesizep: If not NULL, returns the file size in bytes
Simon Glass18109cc2021-10-14 12:48:01 -060098 * Returns 1 for success, or < 0 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +010099 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600100static int get_relfile(struct pxe_context *ctx, const char *file_path,
Simon Glass4d79e882021-10-14 12:48:08 -0600101 unsigned long file_addr, ulong *filesizep)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100102{
103 size_t path_len;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100104 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100105 char addr_buf[18];
Simon Glass4d79e882021-10-14 12:48:08 -0600106 ulong size;
107 int ret;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100108
Simon Glass74b7a2b2021-10-14 12:48:05 -0600109 if (file_path[0] == '/' && ctx->allow_abs_path)
110 *relfile = '\0';
111 else
112 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100113
Simon Glass74b7a2b2021-10-14 12:48:05 -0600114 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100115
116 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100117 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100118
119 return -ENAMETOOLONG;
120 }
121
122 strcat(relfile, file_path);
123
124 printf("Retrieving file: %s\n", relfile);
125
126 sprintf(addr_buf, "%lx", file_addr);
127
Simon Glass4d79e882021-10-14 12:48:08 -0600128 ret = ctx->getfile(ctx, relfile, addr_buf, &size);
129 if (ret < 0)
130 return log_msg_ret("get", ret);
131 if (filesizep)
132 *filesizep = size;
133
134 return 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100135}
136
Simon Glass18109cc2021-10-14 12:48:01 -0600137/**
138 * get_pxe_file() - read a file
139 *
140 * The file is read and nul-terminated
141 *
142 * @ctx: PXE context
143 * @file_path: File path to read (relative to the PXE file)
144 * @file_addr: Address to load file to
145 * Returns 1 for success, or < 0 on error
146 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600147int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Simon Glass4d79e882021-10-14 12:48:08 -0600148 ulong file_addr)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100149{
Simon Glass4d79e882021-10-14 12:48:08 -0600150 ulong size;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100151 int err;
152 char *buf;
153
Simon Glass4d79e882021-10-14 12:48:08 -0600154 err = get_relfile(ctx, file_path, file_addr, &size);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100155 if (err < 0)
156 return err;
157
Simon Glass4d79e882021-10-14 12:48:08 -0600158 buf = map_sysmem(file_addr + size, 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100159 *buf = '\0';
160 unmap_sysmem(buf);
161
162 return 1;
163}
164
165#define PXELINUX_DIR "pxelinux.cfg/"
166
Simon Glass18109cc2021-10-14 12:48:01 -0600167/**
168 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
169 *
170 * @ctx: PXE context
171 * @file: Filename to process (relative to pxelinux.cfg/)
172 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
173 * or other value < 0 on other error
174 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600175int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100176 unsigned long pxefile_addr_r)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100177{
178 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100179 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100180
181 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
182 printf("path (%s%s) too long, skipping\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100183 PXELINUX_DIR, file);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100184 return -ENAMETOOLONG;
185 }
186
187 sprintf(path, PXELINUX_DIR "%s", file);
188
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600189 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100190}
191
Simon Glass18109cc2021-10-14 12:48:01 -0600192/**
193 * get_relfile_envaddr() - read a file to an address in an env var
194 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100195 * Wrapper to make it easier to store the file at file_path in the location
196 * specified by envaddr_name. file_path will be joined to the bootfile path,
197 * if any is specified.
198 *
Simon Glass18109cc2021-10-14 12:48:01 -0600199 * @ctx: PXE context
200 * @file_path: File path to read (relative to the PXE file)
201 * @envaddr_name: Name of environment variable which contains the address to
202 * load to
Simon Glass4d79e882021-10-14 12:48:08 -0600203 * @filesizep: Returns the file size in bytes
Simon Glass18109cc2021-10-14 12:48:01 -0600204 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
205 * environment variable, -EINVAL if its format is not valid hex, or other
206 * value < 0 on other error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100207 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600208static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Simon Glass4d79e882021-10-14 12:48:08 -0600209 const char *envaddr_name, ulong *filesizep)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100210{
211 unsigned long file_addr;
212 char *envaddr;
213
214 envaddr = from_env(envaddr_name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100215 if (!envaddr)
216 return -ENOENT;
217
218 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
219 return -EINVAL;
220
Simon Glass4d79e882021-10-14 12:48:08 -0600221 return get_relfile(ctx, file_path, file_addr, filesizep);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100222}
223
Simon Glass18109cc2021-10-14 12:48:01 -0600224/**
225 * label_create() - crate a new PXE label
226 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100227 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
228 * result must be free()'d to reclaim the memory.
229 *
Simon Glass18109cc2021-10-14 12:48:01 -0600230 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard2373cba2019-11-25 09:07:37 +0100231 */
232static struct pxe_label *label_create(void)
233{
234 struct pxe_label *label;
235
236 label = malloc(sizeof(struct pxe_label));
Patrice Chotard2373cba2019-11-25 09:07:37 +0100237 if (!label)
238 return NULL;
239
240 memset(label, 0, sizeof(struct pxe_label));
241
242 return label;
243}
244
Simon Glass18109cc2021-10-14 12:48:01 -0600245/**
246 * label_destroy() - free the memory used by a pxe_label
247 *
248 * This frees @label itself as well as memory used by its name,
249 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
250 * they're non-NULL.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100251 *
252 * So - be sure to only use dynamically allocated memory for the members of
253 * the pxe_label struct, unless you want to clean it up first. These are
254 * currently only created by the pxe file parsing code.
Simon Glass18109cc2021-10-14 12:48:01 -0600255 *
256 * @label: Label to free
Patrice Chotard2373cba2019-11-25 09:07:37 +0100257 */
258static void label_destroy(struct pxe_label *label)
259{
Simon Glass929860b2021-10-14 12:48:02 -0600260 free(label->name);
261 free(label->kernel);
262 free(label->config);
263 free(label->append);
264 free(label->initrd);
265 free(label->fdt);
266 free(label->fdtdir);
267 free(label->fdtoverlays);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100268 free(label);
269}
270
Simon Glass18109cc2021-10-14 12:48:01 -0600271/**
272 * label_print() - Print a label and its string members if they're defined
Patrice Chotard2373cba2019-11-25 09:07:37 +0100273 *
274 * This is passed as a callback to the menu code for displaying each
275 * menu entry.
Simon Glass18109cc2021-10-14 12:48:01 -0600276 *
277 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100278 */
279static void label_print(void *data)
280{
281 struct pxe_label *label = data;
282 const char *c = label->menu ? label->menu : label->name;
283
284 printf("%s:\t%s\n", label->num, c);
285}
286
Simon Glass18109cc2021-10-14 12:48:01 -0600287/**
288 * label_localboot() - Boot a label that specified 'localboot'
Patrice Chotard2373cba2019-11-25 09:07:37 +0100289 *
Simon Glass18109cc2021-10-14 12:48:01 -0600290 * This requires that the 'localcmd' environment variable is defined. Its
291 * contents will be executed as U-Boot commands. If the label specified an
292 * 'append' line, its contents will be used to overwrite the contents of the
293 * 'bootargs' environment variable prior to running 'localcmd'.
294 *
295 * @label: Label to process
296 * Returns 1 on success or < 0 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100297 */
298static int label_localboot(struct pxe_label *label)
299{
300 char *localcmd;
301
302 localcmd = from_env("localcmd");
Patrice Chotard2373cba2019-11-25 09:07:37 +0100303 if (!localcmd)
304 return -ENOENT;
305
306 if (label->append) {
307 char bootargs[CONFIG_SYS_CBSIZE];
308
Simon Glass1a62d642020-11-05 10:33:47 -0700309 cli_simple_process_macros(label->append, bootargs,
310 sizeof(bootargs));
Patrice Chotard2373cba2019-11-25 09:07:37 +0100311 env_set("bootargs", bootargs);
312 }
313
314 debug("running: %s\n", localcmd);
315
316 return run_command_list(localcmd, strlen(localcmd), 0);
317}
318
Zhang Ning02901462022-02-01 08:33:37 +0800319/*
320 * label_boot_kaslrseed generate kaslrseed from hw rng
321 */
322
323static void label_boot_kaslrseed(void)
324{
325#ifdef CONFIG_DM_RNG
326 ulong fdt_addr;
327 struct fdt_header *working_fdt;
328 size_t n = 0x8;
329 struct udevice *dev;
330 u64 *buf;
331 int nodeoffset;
332 int err;
333
334 /* Get the main fdt and map it */
335 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
336 working_fdt = map_sysmem(fdt_addr, 0);
337 err = fdt_check_header(working_fdt);
338 if (err)
339 return;
340
341 /* add extra size for holding kaslr-seed */
342 /* err is new fdt size, 0 or negtive */
343 err = fdt_shrink_to_minimum(working_fdt, 512);
344 if (err <= 0)
345 return;
346
347 if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
348 printf("No RNG device\n");
349 return;
350 }
351
352 nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
353 if (nodeoffset < 0) {
354 printf("Reading chosen node failed\n");
355 return;
356 }
357
358 buf = malloc(n);
359 if (!buf) {
360 printf("Out of memory\n");
361 return;
362 }
363
364 if (dm_rng_read(dev, buf, n)) {
365 printf("Reading RNG failed\n");
366 goto err;
367 }
368
369 err = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
370 if (err < 0) {
371 printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(err));
372 goto err;
373 }
374err:
375 free(buf);
376#endif
377 return;
378}
379
Simon Glass18109cc2021-10-14 12:48:01 -0600380/**
381 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
Edoardo Tomelleri35821a22022-09-21 15:26:33 +0200382 * or 'devicetree-overlay'
Simon Glass18109cc2021-10-14 12:48:01 -0600383 *
384 * @ctx: PXE context
385 * @label: Label to process
Neil Armstrong69076df2021-01-20 09:54:53 +0100386 */
387#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600388static void label_boot_fdtoverlay(struct pxe_context *ctx,
389 struct pxe_label *label)
Neil Armstrong69076df2021-01-20 09:54:53 +0100390{
391 char *fdtoverlay = label->fdtoverlays;
392 struct fdt_header *working_fdt;
393 char *fdtoverlay_addr_env;
394 ulong fdtoverlay_addr;
395 ulong fdt_addr;
396 int err;
397
398 /* Get the main fdt and map it */
Simon Glass7e5f4602021-07-24 09:03:29 -0600399 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100400 working_fdt = map_sysmem(fdt_addr, 0);
401 err = fdt_check_header(working_fdt);
402 if (err)
403 return;
404
405 /* Get the specific overlay loading address */
406 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
407 if (!fdtoverlay_addr_env) {
408 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
409 return;
410 }
411
Simon Glass7e5f4602021-07-24 09:03:29 -0600412 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100413
414 /* Cycle over the overlay files and apply them in order */
415 do {
416 struct fdt_header *blob;
417 char *overlayfile;
418 char *end;
419 int len;
420
421 /* Drop leading spaces */
422 while (*fdtoverlay == ' ')
423 ++fdtoverlay;
424
425 /* Copy a single filename if multiple provided */
426 end = strstr(fdtoverlay, " ");
427 if (end) {
428 len = (int)(end - fdtoverlay);
429 overlayfile = malloc(len + 1);
430 strncpy(overlayfile, fdtoverlay, len);
431 overlayfile[len] = '\0';
432 } else
433 overlayfile = fdtoverlay;
434
435 if (!strlen(overlayfile))
436 goto skip_overlay;
437
438 /* Load overlay file */
Simon Glass4d79e882021-10-14 12:48:08 -0600439 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
440 NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100441 if (err < 0) {
442 printf("Failed loading overlay %s\n", overlayfile);
443 goto skip_overlay;
444 }
445
446 /* Resize main fdt */
447 fdt_shrink_to_minimum(working_fdt, 8192);
448
449 blob = map_sysmem(fdtoverlay_addr, 0);
450 err = fdt_check_header(blob);
451 if (err) {
452 printf("Invalid overlay %s, skipping\n",
453 overlayfile);
454 goto skip_overlay;
455 }
456
457 err = fdt_overlay_apply_verbose(working_fdt, blob);
458 if (err) {
459 printf("Failed to apply overlay %s, skipping\n",
460 overlayfile);
461 goto skip_overlay;
462 }
463
464skip_overlay:
465 if (end)
466 free(overlayfile);
467 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
468}
469#endif
470
Simon Glass18109cc2021-10-14 12:48:01 -0600471/**
472 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard2373cba2019-11-25 09:07:37 +0100473 *
474 * If we can't boot for any reason, we return. A successful boot never
475 * returns.
476 *
477 * The kernel will be stored in the location given by the 'kernel_addr_r'
478 * environment variable.
479 *
480 * If the label specifies an initrd file, it will be stored in the location
481 * given by the 'ramdisk_addr_r' environment variable.
482 *
483 * If the label specifies an 'append' line, its contents will overwrite that
484 * of the 'bootargs' environment variable.
Simon Glass18109cc2021-10-14 12:48:01 -0600485 *
486 * @ctx: PXE context
487 * @label: Label to process
488 * Returns does not return on success, otherwise returns 0 if a localboot
489 * label was processed, or 1 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100490 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600491static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100492{
493 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700494 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700495 char *kernel_addr = NULL;
496 char *initrd_addr_str = NULL;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700497 char initrd_filesize[10];
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700498 char initrd_str[28];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100499 char mac_str[29] = "";
500 char ip_str[68] = "";
501 char *fit_addr = NULL;
502 int bootm_argc = 2;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700503 int zboot_argc = 3;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100504 int len = 0;
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700505 ulong kernel_addr_r;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100506 void *buf;
507
508 label_print(label);
509
510 label->attempted = 1;
511
512 if (label->localboot) {
513 if (label->localboot_val >= 0)
514 label_localboot(label);
515 return 0;
516 }
517
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100518 if (!label->kernel) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100519 printf("No kernel given, skipping %s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100520 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100521 return 1;
522 }
523
524 if (label->initrd) {
Simon Glass4d79e882021-10-14 12:48:08 -0600525 ulong size;
526
527 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
528 &size) < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100529 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100530 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100531 return 1;
532 }
533
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700534 initrd_addr_str = env_get("ramdisk_addr_r");
Heinrich Schuchardt085cbda2021-11-15 19:26:51 +0100535 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
536 initrd_addr_str, size);
537 if (size >= sizeof(initrd_str))
538 return 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100539 }
540
Simon Glass4d79e882021-10-14 12:48:08 -0600541 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
542 NULL) < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100543 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100544 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100545 return 1;
546 }
547
548 if (label->ipappend & 0x1) {
549 sprintf(ip_str, " ip=%s:%s:%s:%s",
550 env_get("ipaddr"), env_get("serverip"),
551 env_get("gatewayip"), env_get("netmask"));
552 }
553
Kory Maincentff0287e2021-02-02 16:42:28 +0100554 if (IS_ENABLED(CONFIG_CMD_NET)) {
555 if (label->ipappend & 0x2) {
556 int err;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100557
Kory Maincentff0287e2021-02-02 16:42:28 +0100558 strcpy(mac_str, " BOOTIF=");
559 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
560 if (err < 0)
561 mac_str[0] = '\0';
562 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100563 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100564
565 if ((label->ipappend & 0x3) || label->append) {
566 char bootargs[CONFIG_SYS_CBSIZE] = "";
567 char finalbootargs[CONFIG_SYS_CBSIZE];
568
569 if (strlen(label->append ?: "") +
570 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
571 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
572 strlen(label->append ?: ""),
573 strlen(ip_str), strlen(mac_str),
574 sizeof(bootargs));
575 return 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100576 }
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100577
578 if (label->append)
579 strncpy(bootargs, label->append, sizeof(bootargs));
580
581 strcat(bootargs, ip_str);
582 strcat(bootargs, mac_str);
583
Simon Glass1a62d642020-11-05 10:33:47 -0700584 cli_simple_process_macros(bootargs, finalbootargs,
585 sizeof(finalbootargs));
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100586 env_set("bootargs", finalbootargs);
587 printf("append: %s\n", finalbootargs);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100588 }
589
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700590 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700591
Patrice Chotard2373cba2019-11-25 09:07:37 +0100592 /* for FIT, append the configuration identifier */
593 if (label->config) {
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700594 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100595
596 fit_addr = malloc(len);
597 if (!fit_addr) {
598 printf("malloc fail (FIT address)\n");
599 return 1;
600 }
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700601 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
602 kernel_addr = fit_addr;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100603 }
604
605 /*
606 * fdt usage is optional:
Anton Leontievdb366742019-09-03 10:52:24 +0300607 * It handles the following scenarios.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100608 *
Anton Leontievdb366742019-09-03 10:52:24 +0300609 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
610 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
611 * bootm, and adjust argc appropriately.
612 *
613 * If retrieve fails and no exact fdt blob is specified in pxe file with
614 * "fdt" label, try Scenario 2.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100615 *
616 * Scenario 2: If there is an fdt_addr specified, pass it along to
617 * bootm, and adjust argc appropriately.
618 *
Tom Rinied625112022-12-13 09:26:25 -0500619 * Scenario 3: fdt blob is not available.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100620 */
621 bootm_argv[3] = env_get("fdt_addr_r");
622
623 /* if fdt label is defined then get fdt from server */
624 if (bootm_argv[3]) {
625 char *fdtfile = NULL;
626 char *fdtfilefree = NULL;
627
628 if (label->fdt) {
629 fdtfile = label->fdt;
630 } else if (label->fdtdir) {
631 char *f1, *f2, *f3, *f4, *slash;
632
633 f1 = env_get("fdtfile");
634 if (f1) {
635 f2 = "";
636 f3 = "";
637 f4 = "";
638 } else {
639 /*
640 * For complex cases where this code doesn't
641 * generate the correct filename, the board
642 * code should set $fdtfile during early boot,
643 * or the boot scripts should set $fdtfile
644 * before invoking "pxe" or "sysboot".
645 */
646 f1 = env_get("soc");
647 f2 = "-";
648 f3 = env_get("board");
649 f4 = ".dtb";
Dimitri John Ledkov75efe7d2021-04-21 12:42:01 +0100650 if (!f1) {
651 f1 = "";
652 f2 = "";
653 }
654 if (!f3) {
655 f2 = "";
656 f3 = "";
657 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100658 }
659
660 len = strlen(label->fdtdir);
661 if (!len)
662 slash = "./";
663 else if (label->fdtdir[len - 1] != '/')
664 slash = "/";
665 else
666 slash = "";
667
668 len = strlen(label->fdtdir) + strlen(slash) +
669 strlen(f1) + strlen(f2) + strlen(f3) +
670 strlen(f4) + 1;
671 fdtfilefree = malloc(len);
672 if (!fdtfilefree) {
673 printf("malloc fail (FDT filename)\n");
674 goto cleanup;
675 }
676
677 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
678 label->fdtdir, slash, f1, f2, f3, f4);
679 fdtfile = fdtfilefree;
680 }
681
682 if (fdtfile) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600683 int err = get_relfile_envaddr(ctx, fdtfile,
Simon Glass4d79e882021-10-14 12:48:08 -0600684 "fdt_addr_r", NULL);
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100685
Patrice Chotard2373cba2019-11-25 09:07:37 +0100686 free(fdtfilefree);
687 if (err < 0) {
Anton Leontievdb366742019-09-03 10:52:24 +0300688 bootm_argv[3] = NULL;
689
690 if (label->fdt) {
691 printf("Skipping %s for failure retrieving FDT\n",
692 label->name);
693 goto cleanup;
694 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100695 }
Neil Armstrong69076df2021-01-20 09:54:53 +0100696
Zhang Ning02901462022-02-01 08:33:37 +0800697 if (label->kaslrseed)
698 label_boot_kaslrseed();
699
Neil Armstrong69076df2021-01-20 09:54:53 +0100700#ifdef CONFIG_OF_LIBFDT_OVERLAY
701 if (label->fdtoverlays)
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600702 label_boot_fdtoverlay(ctx, label);
Neil Armstrong69076df2021-01-20 09:54:53 +0100703#endif
Patrice Chotard2373cba2019-11-25 09:07:37 +0100704 } else {
705 bootm_argv[3] = NULL;
706 }
707 }
708
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700709 bootm_argv[1] = kernel_addr;
710 zboot_argv[1] = kernel_addr;
711
712 if (initrd_addr_str) {
713 bootm_argv[2] = initrd_str;
714 bootm_argc = 3;
715
716 zboot_argv[3] = initrd_addr_str;
717 zboot_argv[4] = initrd_filesize;
718 zboot_argc = 5;
719 }
720
Patrice Chotard2373cba2019-11-25 09:07:37 +0100721 if (!bootm_argv[3])
722 bootm_argv[3] = env_get("fdt_addr");
723
724 if (bootm_argv[3]) {
725 if (!bootm_argv[2])
726 bootm_argv[2] = "-";
727 bootm_argc = 4;
728 }
729
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700730 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
731 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100732 /* Try bootm for legacy and FIT format image */
John Keepingbe43a352022-07-28 11:19:15 +0100733 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
734 IS_ENABLED(CONFIG_CMD_BOOTM))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600735 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100736 /* Try booting an AArch64 Linux kernel image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100737 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600738 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100739 /* Try booting a Image */
Kory Maincentff0287e2021-02-02 16:42:28 +0100740 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600741 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincent18c25822021-02-02 16:42:29 +0100742 /* Try booting an x86_64 Linux kernel image */
743 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600744 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentff0287e2021-02-02 16:42:28 +0100745
Patrice Chotard2373cba2019-11-25 09:07:37 +0100746 unmap_sysmem(buf);
747
748cleanup:
Simon Glass929860b2021-10-14 12:48:02 -0600749 free(fit_addr);
750
Patrice Chotard2373cba2019-11-25 09:07:37 +0100751 return 1;
752}
753
Simon Glass18109cc2021-10-14 12:48:01 -0600754/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100755enum token_type {
756 T_EOL,
757 T_STRING,
758 T_EOF,
759 T_MENU,
760 T_TITLE,
761 T_TIMEOUT,
762 T_LABEL,
763 T_KERNEL,
764 T_LINUX,
765 T_APPEND,
766 T_INITRD,
767 T_LOCALBOOT,
768 T_DEFAULT,
769 T_PROMPT,
770 T_INCLUDE,
771 T_FDT,
772 T_FDTDIR,
Neil Armstrong69076df2021-01-20 09:54:53 +0100773 T_FDTOVERLAYS,
Patrice Chotard2373cba2019-11-25 09:07:37 +0100774 T_ONTIMEOUT,
775 T_IPAPPEND,
776 T_BACKGROUND,
Zhang Ning02901462022-02-01 08:33:37 +0800777 T_KASLRSEED,
Patrice Chotard2373cba2019-11-25 09:07:37 +0100778 T_INVALID
779};
780
Simon Glass18109cc2021-10-14 12:48:01 -0600781/** struct token - token - given by a value and a type */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100782struct token {
783 char *val;
784 enum token_type type;
785};
786
Simon Glass18109cc2021-10-14 12:48:01 -0600787/* Keywords recognized */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100788static const struct token keywords[] = {
789 {"menu", T_MENU},
790 {"title", T_TITLE},
791 {"timeout", T_TIMEOUT},
792 {"default", T_DEFAULT},
793 {"prompt", T_PROMPT},
794 {"label", T_LABEL},
795 {"kernel", T_KERNEL},
796 {"linux", T_LINUX},
797 {"localboot", T_LOCALBOOT},
798 {"append", T_APPEND},
799 {"initrd", T_INITRD},
800 {"include", T_INCLUDE},
801 {"devicetree", T_FDT},
802 {"fdt", T_FDT},
803 {"devicetreedir", T_FDTDIR},
804 {"fdtdir", T_FDTDIR},
Neil Armstrong69076df2021-01-20 09:54:53 +0100805 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri35821a22022-09-21 15:26:33 +0200806 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard2373cba2019-11-25 09:07:37 +0100807 {"ontimeout", T_ONTIMEOUT,},
808 {"ipappend", T_IPAPPEND,},
809 {"background", T_BACKGROUND,},
Zhang Ning02901462022-02-01 08:33:37 +0800810 {"kaslrseed", T_KASLRSEED,},
Patrice Chotard2373cba2019-11-25 09:07:37 +0100811 {NULL, T_INVALID}
812};
813
Simon Glass18109cc2021-10-14 12:48:01 -0600814/**
815 * enum lex_state - lexer state
816 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100817 * Since pxe(linux) files don't have a token to identify the start of a
818 * literal, we have to keep track of when we're in a state where a literal is
819 * expected vs when we're in a state a keyword is expected.
820 */
821enum lex_state {
822 L_NORMAL = 0,
823 L_KEYWORD,
824 L_SLITERAL
825};
826
Simon Glass18109cc2021-10-14 12:48:01 -0600827/**
828 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100829 *
Simon Glass18109cc2021-10-14 12:48:01 -0600830 * This is used for scanning both string literals and keywords.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100831 *
832 * Characters from *p are copied into t-val until a character equal to
833 * delim is found, or a NUL byte is reached. If delim has the special value of
834 * ' ', any whitespace character will be used as a delimiter.
835 *
836 * If lower is unequal to 0, uppercase characters will be converted to
837 * lowercase in the result. This is useful to make keywords case
838 * insensitive.
839 *
840 * The location of *p is updated to point to the first character after the end
841 * of the token - the ending delimiter.
842 *
Simon Glass18109cc2021-10-14 12:48:01 -0600843 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
844 * it.
845 *
846 * @p: Points to a pointer to the current position in the input being processed.
847 * Updated to point at the first character after the current token
848 * @t: Pointers to a token to fill in
849 * @delim: Delimiter character to look for, either newline or space
850 * @lower: true to convert the string to lower case when storing
851 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard2373cba2019-11-25 09:07:37 +0100852 */
853static char *get_string(char **p, struct token *t, char delim, int lower)
854{
855 char *b, *e;
856 size_t len, i;
857
858 /*
859 * b and e both start at the beginning of the input stream.
860 *
861 * e is incremented until we find the ending delimiter, or a NUL byte
862 * is reached. Then, we take e - b to find the length of the token.
863 */
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100864 b = *p;
865 e = *p;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100866 while (*e) {
867 if ((delim == ' ' && isspace(*e)) || delim == *e)
868 break;
869 e++;
870 }
871
872 len = e - b;
873
874 /*
875 * Allocate memory to hold the string, and copy it in, converting
876 * characters to lowercase if lower is != 0.
877 */
878 t->val = malloc(len + 1);
879 if (!t->val)
880 return NULL;
881
882 for (i = 0; i < len; i++, b++) {
883 if (lower)
884 t->val[i] = tolower(*b);
885 else
886 t->val[i] = *b;
887 }
888
889 t->val[len] = '\0';
890
Simon Glass929860b2021-10-14 12:48:02 -0600891 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100892 *p = e;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100893 t->type = T_STRING;
894
895 return t->val;
896}
897
Simon Glass18109cc2021-10-14 12:48:01 -0600898/**
899 * get_keyword() - Populate a keyword token with a type and value
900 *
901 * Updates the ->type field based on the keyword string in @val
902 * @t: Token to populate
Patrice Chotard2373cba2019-11-25 09:07:37 +0100903 */
904static void get_keyword(struct token *t)
905{
906 int i;
907
908 for (i = 0; keywords[i].val; i++) {
909 if (!strcmp(t->val, keywords[i].val)) {
910 t->type = keywords[i].type;
911 break;
912 }
913 }
914}
915
Simon Glass18109cc2021-10-14 12:48:01 -0600916/**
917 * get_token() - Get the next token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100918 *
Simon Glass18109cc2021-10-14 12:48:01 -0600919 * We have to keep track of which state we're in to know if we're looking to get
920 * a string literal or a keyword.
921 *
922 * @p: Points to a pointer to the current position in the input being processed.
923 * Updated to point at the first character after the current token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100924 */
925static void get_token(char **p, struct token *t, enum lex_state state)
926{
927 char *c = *p;
928
929 t->type = T_INVALID;
930
931 /* eat non EOL whitespace */
932 while (isblank(*c))
933 c++;
934
935 /*
936 * eat comments. note that string literals can't begin with #, but
937 * can contain a # after their first character.
938 */
939 if (*c == '#') {
940 while (*c && *c != '\n')
941 c++;
942 }
943
944 if (*c == '\n') {
945 t->type = T_EOL;
946 c++;
947 } else if (*c == '\0') {
948 t->type = T_EOF;
949 c++;
950 } else if (state == L_SLITERAL) {
951 get_string(&c, t, '\n', 0);
952 } else if (state == L_KEYWORD) {
953 /*
954 * when we expect a keyword, we first get the next string
955 * token delimited by whitespace, and then check if it
956 * matches a keyword in our keyword list. if it does, it's
957 * converted to a keyword token of the appropriate type, and
958 * if not, it remains a string token.
959 */
960 get_string(&c, t, ' ', 1);
961 get_keyword(t);
962 }
963
964 *p = c;
965}
966
Simon Glass18109cc2021-10-14 12:48:01 -0600967/**
968 * eol_or_eof() - Find end of line
969 *
970 * Increment *c until we get to the end of the current line, or EOF
971 *
972 * @c: Points to a pointer to the current position in the input being processed.
973 * Updated to point at the first character after the current token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100974 */
975static void eol_or_eof(char **c)
976{
977 while (**c && **c != '\n')
978 (*c)++;
979}
980
981/*
982 * All of these parse_* functions share some common behavior.
983 *
984 * They finish with *c pointing after the token they parse, and return 1 on
985 * success, or < 0 on error.
986 */
987
988/*
989 * Parse a string literal and store a pointer it at *dst. String literals
990 * terminate at the end of the line.
991 */
992static int parse_sliteral(char **c, char **dst)
993{
994 struct token t;
995 char *s = *c;
996
997 get_token(c, &t, L_SLITERAL);
998
999 if (t.type != T_STRING) {
1000 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1001 return -EINVAL;
1002 }
1003
1004 *dst = t.val;
1005
1006 return 1;
1007}
1008
1009/*
1010 * Parse a base 10 (unsigned) integer and store it at *dst.
1011 */
1012static int parse_integer(char **c, int *dst)
1013{
1014 struct token t;
1015 char *s = *c;
1016
1017 get_token(c, &t, L_SLITERAL);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001018 if (t.type != T_STRING) {
1019 printf("Expected string: %.*s\n", (int)(*c - s), s);
1020 return -EINVAL;
1021 }
1022
1023 *dst = simple_strtol(t.val, NULL, 10);
1024
1025 free(t.val);
1026
1027 return 1;
1028}
1029
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001030static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001031 struct pxe_menu *cfg, int nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001032
1033/*
1034 * Parse an include statement, and retrieve and parse the file it mentions.
1035 *
1036 * base should point to a location where it's safe to store the file, and
1037 * nest_level should indicate how many nested includes have occurred. For this
1038 * include, nest_level has already been incremented and doesn't need to be
1039 * incremented here.
1040 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001041static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001042 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001043{
1044 char *include_path;
1045 char *s = *c;
1046 int err;
1047 char *buf;
1048 int ret;
1049
1050 err = parse_sliteral(c, &include_path);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001051 if (err < 0) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001052 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001053 return err;
1054 }
1055
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001056 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001057 if (err < 0) {
1058 printf("Couldn't retrieve %s\n", include_path);
1059 return err;
1060 }
1061
1062 buf = map_sysmem(base, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001063 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001064 unmap_sysmem(buf);
1065
1066 return ret;
1067}
1068
1069/*
1070 * Parse lines that begin with 'menu'.
1071 *
1072 * base and nest are provided to handle the 'menu include' case.
1073 *
1074 * base should point to a location where it's safe to store the included file.
1075 *
1076 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1077 * a file it includes, 3 when parsing a file included by that file, and so on.
1078 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001079static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001080 unsigned long base, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001081{
1082 struct token t;
1083 char *s = *c;
1084 int err = 0;
1085
1086 get_token(c, &t, L_KEYWORD);
1087
1088 switch (t.type) {
1089 case T_TITLE:
1090 err = parse_sliteral(c, &cfg->title);
1091
1092 break;
1093
1094 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001095 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001096 break;
1097
1098 case T_BACKGROUND:
1099 err = parse_sliteral(c, &cfg->bmp);
1100 break;
1101
1102 default:
1103 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001104 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001105 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001106 if (err < 0)
1107 return err;
1108
1109 eol_or_eof(c);
1110
1111 return 1;
1112}
1113
1114/*
1115 * Handles parsing a 'menu line' when we're parsing a label.
1116 */
1117static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001118 struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001119{
1120 struct token t;
1121 char *s;
1122
1123 s = *c;
1124
1125 get_token(c, &t, L_KEYWORD);
1126
1127 switch (t.type) {
1128 case T_DEFAULT:
1129 if (!cfg->default_label)
1130 cfg->default_label = strdup(label->name);
1131
1132 if (!cfg->default_label)
1133 return -ENOMEM;
1134
1135 break;
1136 case T_LABEL:
1137 parse_sliteral(c, &label->menu);
1138 break;
1139 default:
1140 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001141 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001142 }
1143
1144 eol_or_eof(c);
1145
1146 return 0;
1147}
1148
1149/*
1150 * Handles parsing a 'kernel' label.
1151 * expecting "filename" or "<fit_filename>#cfg"
1152 */
1153static int parse_label_kernel(char **c, struct pxe_label *label)
1154{
1155 char *s;
1156 int err;
1157
1158 err = parse_sliteral(c, &label->kernel);
1159 if (err < 0)
1160 return err;
1161
1162 s = strstr(label->kernel, "#");
1163 if (!s)
1164 return 1;
1165
1166 label->config = malloc(strlen(s) + 1);
1167 if (!label->config)
1168 return -ENOMEM;
1169
1170 strcpy(label->config, s);
1171 *s = 0;
1172
1173 return 1;
1174}
1175
1176/*
1177 * Parses a label and adds it to the list of labels for a menu.
1178 *
1179 * A label ends when we either get to the end of a file, or
1180 * get some input we otherwise don't have a handler defined
1181 * for.
1182 *
1183 */
1184static int parse_label(char **c, struct pxe_menu *cfg)
1185{
1186 struct token t;
1187 int len;
1188 char *s = *c;
1189 struct pxe_label *label;
1190 int err;
1191
1192 label = label_create();
1193 if (!label)
1194 return -ENOMEM;
1195
1196 err = parse_sliteral(c, &label->name);
1197 if (err < 0) {
1198 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1199 label_destroy(label);
1200 return -EINVAL;
1201 }
1202
1203 list_add_tail(&label->list, &cfg->labels);
1204
1205 while (1) {
1206 s = *c;
1207 get_token(c, &t, L_KEYWORD);
1208
1209 err = 0;
1210 switch (t.type) {
1211 case T_MENU:
1212 err = parse_label_menu(c, cfg, label);
1213 break;
1214
1215 case T_KERNEL:
1216 case T_LINUX:
1217 err = parse_label_kernel(c, label);
1218 break;
1219
1220 case T_APPEND:
1221 err = parse_sliteral(c, &label->append);
1222 if (label->initrd)
1223 break;
1224 s = strstr(label->append, "initrd=");
1225 if (!s)
1226 break;
1227 s += 7;
1228 len = (int)(strchr(s, ' ') - s);
1229 label->initrd = malloc(len + 1);
1230 strncpy(label->initrd, s, len);
1231 label->initrd[len] = '\0';
1232
1233 break;
1234
1235 case T_INITRD:
1236 if (!label->initrd)
1237 err = parse_sliteral(c, &label->initrd);
1238 break;
1239
1240 case T_FDT:
1241 if (!label->fdt)
1242 err = parse_sliteral(c, &label->fdt);
1243 break;
1244
1245 case T_FDTDIR:
1246 if (!label->fdtdir)
1247 err = parse_sliteral(c, &label->fdtdir);
1248 break;
1249
Neil Armstrong69076df2021-01-20 09:54:53 +01001250 case T_FDTOVERLAYS:
1251 if (!label->fdtoverlays)
1252 err = parse_sliteral(c, &label->fdtoverlays);
1253 break;
1254
Patrice Chotard2373cba2019-11-25 09:07:37 +01001255 case T_LOCALBOOT:
1256 label->localboot = 1;
1257 err = parse_integer(c, &label->localboot_val);
1258 break;
1259
1260 case T_IPAPPEND:
1261 err = parse_integer(c, &label->ipappend);
1262 break;
1263
Zhang Ning02901462022-02-01 08:33:37 +08001264 case T_KASLRSEED:
1265 label->kaslrseed = 1;
1266 break;
1267
Patrice Chotard2373cba2019-11-25 09:07:37 +01001268 case T_EOL:
1269 break;
1270
1271 default:
1272 /*
1273 * put the token back! we don't want it - it's the end
1274 * of a label and whatever token this is, it's
1275 * something for the menu level context to handle.
1276 */
1277 *c = s;
1278 return 1;
1279 }
1280
1281 if (err < 0)
1282 return err;
1283 }
1284}
1285
1286/*
1287 * This 16 comes from the limit pxelinux imposes on nested includes.
1288 *
1289 * There is no reason at all we couldn't do more, but some limit helps prevent
1290 * infinite (until crash occurs) recursion if a file tries to include itself.
1291 */
1292#define MAX_NEST_LEVEL 16
1293
1294/*
1295 * Entry point for parsing a menu file. nest_level indicates how many times
1296 * we've nested in includes. It will be 1 for the top level menu file.
1297 *
1298 * Returns 1 on success, < 0 on error.
1299 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001300static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001301 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001302{
1303 struct token t;
1304 char *s, *b, *label_name;
1305 int err;
1306
1307 b = p;
1308
1309 if (nest_level > MAX_NEST_LEVEL) {
1310 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1311 return -EMLINK;
1312 }
1313
1314 while (1) {
1315 s = p;
1316
1317 get_token(&p, &t, L_KEYWORD);
1318
1319 err = 0;
1320 switch (t.type) {
1321 case T_MENU:
1322 cfg->prompt = 1;
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001323 err = parse_menu(ctx, &p, cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001324 base + ALIGN(strlen(b) + 1, 4),
1325 nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001326 break;
1327
1328 case T_TIMEOUT:
1329 err = parse_integer(&p, &cfg->timeout);
1330 break;
1331
1332 case T_LABEL:
1333 err = parse_label(&p, cfg);
1334 break;
1335
1336 case T_DEFAULT:
1337 case T_ONTIMEOUT:
1338 err = parse_sliteral(&p, &label_name);
1339
1340 if (label_name) {
1341 if (cfg->default_label)
1342 free(cfg->default_label);
1343
1344 cfg->default_label = label_name;
1345 }
1346
1347 break;
1348
1349 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001350 err = handle_include(ctx, &p,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001351 base + ALIGN(strlen(b), 4), cfg,
1352 nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001353 break;
1354
1355 case T_PROMPT:
1356 eol_or_eof(&p);
1357 break;
1358
1359 case T_EOL:
1360 break;
1361
1362 case T_EOF:
1363 return 1;
1364
1365 default:
1366 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001367 (int)(p - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001368 eol_or_eof(&p);
1369 }
1370
1371 if (err < 0)
1372 return err;
1373 }
1374}
1375
1376/*
Patrice Chotard2373cba2019-11-25 09:07:37 +01001377 */
1378void destroy_pxe_menu(struct pxe_menu *cfg)
1379{
1380 struct list_head *pos, *n;
1381 struct pxe_label *label;
1382
Simon Glass929860b2021-10-14 12:48:02 -06001383 free(cfg->title);
1384 free(cfg->default_label);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001385
1386 list_for_each_safe(pos, n, &cfg->labels) {
1387 label = list_entry(pos, struct pxe_label, list);
1388
1389 label_destroy(label);
1390 }
1391
1392 free(cfg);
1393}
1394
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001395struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001396{
1397 struct pxe_menu *cfg;
1398 char *buf;
1399 int r;
1400
1401 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard2373cba2019-11-25 09:07:37 +01001402 if (!cfg)
1403 return NULL;
1404
1405 memset(cfg, 0, sizeof(struct pxe_menu));
1406
1407 INIT_LIST_HEAD(&cfg->labels);
1408
1409 buf = map_sysmem(menucfg, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001410 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001411 unmap_sysmem(buf);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001412 if (r < 0) {
1413 destroy_pxe_menu(cfg);
1414 return NULL;
1415 }
1416
1417 return cfg;
1418}
1419
1420/*
1421 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1422 * menu code.
1423 */
1424static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1425{
1426 struct pxe_label *label;
1427 struct list_head *pos;
1428 struct menu *m;
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001429 char *label_override;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001430 int err;
1431 int i = 1;
1432 char *default_num = NULL;
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001433 char *override_num = NULL;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001434
1435 /*
1436 * Create a menu and add items for all the labels.
1437 */
1438 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -07001439 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001440 if (!m)
1441 return NULL;
1442
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001443 label_override = env_get("pxe_label_override");
1444
Patrice Chotard2373cba2019-11-25 09:07:37 +01001445 list_for_each(pos, &cfg->labels) {
1446 label = list_entry(pos, struct pxe_label, list);
1447
1448 sprintf(label->num, "%d", i++);
1449 if (menu_item_add(m, label->num, label) != 1) {
1450 menu_destroy(m);
1451 return NULL;
1452 }
1453 if (cfg->default_label &&
1454 (strcmp(label->name, cfg->default_label) == 0))
1455 default_num = label->num;
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001456 if (label_override && !strcmp(label->name, label_override))
1457 override_num = label->num;
1458 }
1459
1460
1461 if (label_override) {
1462 if (override_num)
1463 default_num = override_num;
1464 else
1465 printf("Missing override pxe label: %s\n",
1466 label_override);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001467 }
1468
1469 /*
1470 * After we've created items for each label in the menu, set the
1471 * menu's default label if one was specified.
1472 */
1473 if (default_num) {
1474 err = menu_default_set(m, default_num);
1475 if (err != 1) {
1476 if (err != -ENOENT) {
1477 menu_destroy(m);
1478 return NULL;
1479 }
1480
1481 printf("Missing default: %s\n", cfg->default_label);
1482 }
1483 }
1484
1485 return m;
1486}
1487
1488/*
1489 * Try to boot any labels we have yet to attempt to boot.
1490 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001491static void boot_unattempted_labels(struct pxe_context *ctx,
1492 struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001493{
1494 struct list_head *pos;
1495 struct pxe_label *label;
1496
1497 list_for_each(pos, &cfg->labels) {
1498 label = list_entry(pos, struct pxe_label, list);
1499
1500 if (!label->attempted)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001501 label_boot(ctx, label);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001502 }
1503}
1504
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001505void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001506{
1507 void *choice;
1508 struct menu *m;
1509 int err;
1510
Kory Maincentff0287e2021-02-02 16:42:28 +01001511 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1512 /* display BMP if available */
1513 if (cfg->bmp) {
Simon Glass4d79e882021-10-14 12:48:08 -06001514 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
Simon Glassb86986c2022-10-18 07:46:31 -06001515#if defined(CONFIG_VIDEO)
Patrick Delaunaye6fe02a2022-03-22 17:08:43 +01001516 struct udevice *dev;
1517
1518 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1519 if (!err)
1520 video_clear(dev);
1521#endif
Kory Maincentff0287e2021-02-02 16:42:28 +01001522 bmp_display(image_load_addr,
1523 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1524 } else {
1525 printf("Skipping background bmp %s for failure\n",
1526 cfg->bmp);
1527 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001528 }
1529 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001530
1531 m = pxe_menu_to_menu(cfg);
1532 if (!m)
1533 return;
1534
1535 err = menu_get_choice(m, &choice);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001536 menu_destroy(m);
1537
1538 /*
1539 * err == 1 means we got a choice back from menu_get_choice.
1540 *
1541 * err == -ENOENT if the menu was setup to select the default but no
1542 * default was set. in that case, we should continue trying to boot
1543 * labels that haven't been attempted yet.
1544 *
1545 * otherwise, the user interrupted or there was some other error and
1546 * we give up.
1547 */
1548
1549 if (err == 1) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001550 err = label_boot(ctx, choice);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001551 if (!err)
1552 return;
1553 } else if (err != -ENOENT) {
1554 return;
1555 }
1556
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001557 boot_unattempted_labels(ctx, cfg);
1558}
1559
Simon Glass12df8422021-10-14 12:48:04 -06001560int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1561 pxe_getfile_func getfile, void *userdata,
1562 bool allow_abs_path, const char *bootfile)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001563{
Simon Glass12df8422021-10-14 12:48:04 -06001564 const char *last_slash;
1565 size_t path_len = 0;
1566
1567 memset(ctx, '\0', sizeof(*ctx));
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001568 ctx->cmdtp = cmdtp;
Simon Glassb1ead6b2021-10-14 12:47:57 -06001569 ctx->getfile = getfile;
Simon Glass4ad5d512021-10-14 12:47:58 -06001570 ctx->userdata = userdata;
Simon Glass8018b9a2021-10-14 12:47:59 -06001571 ctx->allow_abs_path = allow_abs_path;
Simon Glass12df8422021-10-14 12:48:04 -06001572
1573 /* figure out the boot directory, if there is one */
1574 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1575 return -ENOSPC;
1576 ctx->bootdir = strdup(bootfile ? bootfile : "");
1577 if (!ctx->bootdir)
1578 return -ENOMEM;
1579
1580 if (bootfile) {
1581 last_slash = strrchr(bootfile, '/');
1582 if (last_slash)
1583 path_len = (last_slash - bootfile) + 1;
1584 }
1585 ctx->bootdir[path_len] = '\0';
1586
1587 return 0;
1588}
1589
1590void pxe_destroy_ctx(struct pxe_context *ctx)
1591{
1592 free(ctx->bootdir);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001593}
Simon Glass9e62e7c2021-10-14 12:48:03 -06001594
1595int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1596{
1597 struct pxe_menu *cfg;
1598
1599 cfg = parse_pxefile(ctx, pxefile_addr_r);
1600 if (!cfg) {
1601 printf("Error parsing config file\n");
1602 return 1;
1603 }
1604
1605 if (prompt)
1606 cfg->prompt = 1;
1607
1608 handle_pxe_menu(ctx, cfg);
1609
1610 destroy_pxe_menu(cfg);
1611
1612 return 0;
1613}