blob: 96205626750325320aafe2287fa38ab65e304e99 [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);
Patrick Delaunaya5dacef2022-10-28 11:01:19 +0200261 free(label->kernel_label);
Simon Glass929860b2021-10-14 12:48:02 -0600262 free(label->kernel);
263 free(label->config);
264 free(label->append);
265 free(label->initrd);
266 free(label->fdt);
267 free(label->fdtdir);
268 free(label->fdtoverlays);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100269 free(label);
270}
271
Simon Glass18109cc2021-10-14 12:48:01 -0600272/**
273 * label_print() - Print a label and its string members if they're defined
Patrice Chotard2373cba2019-11-25 09:07:37 +0100274 *
275 * This is passed as a callback to the menu code for displaying each
276 * menu entry.
Simon Glass18109cc2021-10-14 12:48:01 -0600277 *
278 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100279 */
280static void label_print(void *data)
281{
282 struct pxe_label *label = data;
283 const char *c = label->menu ? label->menu : label->name;
284
285 printf("%s:\t%s\n", label->num, c);
286}
287
Simon Glass18109cc2021-10-14 12:48:01 -0600288/**
289 * label_localboot() - Boot a label that specified 'localboot'
Patrice Chotard2373cba2019-11-25 09:07:37 +0100290 *
Simon Glass18109cc2021-10-14 12:48:01 -0600291 * This requires that the 'localcmd' environment variable is defined. Its
292 * contents will be executed as U-Boot commands. If the label specified an
293 * 'append' line, its contents will be used to overwrite the contents of the
294 * 'bootargs' environment variable prior to running 'localcmd'.
295 *
296 * @label: Label to process
297 * Returns 1 on success or < 0 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100298 */
299static int label_localboot(struct pxe_label *label)
300{
301 char *localcmd;
302
303 localcmd = from_env("localcmd");
Patrice Chotard2373cba2019-11-25 09:07:37 +0100304 if (!localcmd)
305 return -ENOENT;
306
307 if (label->append) {
308 char bootargs[CONFIG_SYS_CBSIZE];
309
Simon Glass1a62d642020-11-05 10:33:47 -0700310 cli_simple_process_macros(label->append, bootargs,
311 sizeof(bootargs));
Patrice Chotard2373cba2019-11-25 09:07:37 +0100312 env_set("bootargs", bootargs);
313 }
314
315 debug("running: %s\n", localcmd);
316
317 return run_command_list(localcmd, strlen(localcmd), 0);
318}
319
Zhang Ning02901462022-02-01 08:33:37 +0800320/*
321 * label_boot_kaslrseed generate kaslrseed from hw rng
322 */
323
324static void label_boot_kaslrseed(void)
325{
326#ifdef CONFIG_DM_RNG
327 ulong fdt_addr;
328 struct fdt_header *working_fdt;
329 size_t n = 0x8;
330 struct udevice *dev;
331 u64 *buf;
332 int nodeoffset;
333 int err;
334
335 /* Get the main fdt and map it */
336 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
337 working_fdt = map_sysmem(fdt_addr, 0);
338 err = fdt_check_header(working_fdt);
339 if (err)
340 return;
341
342 /* add extra size for holding kaslr-seed */
343 /* err is new fdt size, 0 or negtive */
344 err = fdt_shrink_to_minimum(working_fdt, 512);
345 if (err <= 0)
346 return;
347
348 if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
349 printf("No RNG device\n");
350 return;
351 }
352
353 nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
354 if (nodeoffset < 0) {
355 printf("Reading chosen node failed\n");
356 return;
357 }
358
359 buf = malloc(n);
360 if (!buf) {
361 printf("Out of memory\n");
362 return;
363 }
364
365 if (dm_rng_read(dev, buf, n)) {
366 printf("Reading RNG failed\n");
367 goto err;
368 }
369
370 err = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
371 if (err < 0) {
372 printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(err));
373 goto err;
374 }
375err:
376 free(buf);
377#endif
378 return;
379}
380
Simon Glass18109cc2021-10-14 12:48:01 -0600381/**
382 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
Edoardo Tomelleri35821a22022-09-21 15:26:33 +0200383 * or 'devicetree-overlay'
Simon Glass18109cc2021-10-14 12:48:01 -0600384 *
385 * @ctx: PXE context
386 * @label: Label to process
Neil Armstrong69076df2021-01-20 09:54:53 +0100387 */
388#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600389static void label_boot_fdtoverlay(struct pxe_context *ctx,
390 struct pxe_label *label)
Neil Armstrong69076df2021-01-20 09:54:53 +0100391{
392 char *fdtoverlay = label->fdtoverlays;
393 struct fdt_header *working_fdt;
394 char *fdtoverlay_addr_env;
395 ulong fdtoverlay_addr;
396 ulong fdt_addr;
397 int err;
398
399 /* Get the main fdt and map it */
Simon Glass7e5f4602021-07-24 09:03:29 -0600400 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100401 working_fdt = map_sysmem(fdt_addr, 0);
402 err = fdt_check_header(working_fdt);
403 if (err)
404 return;
405
406 /* Get the specific overlay loading address */
407 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
408 if (!fdtoverlay_addr_env) {
409 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
410 return;
411 }
412
Simon Glass7e5f4602021-07-24 09:03:29 -0600413 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100414
415 /* Cycle over the overlay files and apply them in order */
416 do {
417 struct fdt_header *blob;
418 char *overlayfile;
419 char *end;
420 int len;
421
422 /* Drop leading spaces */
423 while (*fdtoverlay == ' ')
424 ++fdtoverlay;
425
426 /* Copy a single filename if multiple provided */
427 end = strstr(fdtoverlay, " ");
428 if (end) {
429 len = (int)(end - fdtoverlay);
430 overlayfile = malloc(len + 1);
431 strncpy(overlayfile, fdtoverlay, len);
432 overlayfile[len] = '\0';
433 } else
434 overlayfile = fdtoverlay;
435
436 if (!strlen(overlayfile))
437 goto skip_overlay;
438
439 /* Load overlay file */
Simon Glass4d79e882021-10-14 12:48:08 -0600440 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
441 NULL);
Neil Armstrong69076df2021-01-20 09:54:53 +0100442 if (err < 0) {
443 printf("Failed loading overlay %s\n", overlayfile);
444 goto skip_overlay;
445 }
446
447 /* Resize main fdt */
448 fdt_shrink_to_minimum(working_fdt, 8192);
449
450 blob = map_sysmem(fdtoverlay_addr, 0);
451 err = fdt_check_header(blob);
452 if (err) {
453 printf("Invalid overlay %s, skipping\n",
454 overlayfile);
455 goto skip_overlay;
456 }
457
458 err = fdt_overlay_apply_verbose(working_fdt, blob);
459 if (err) {
460 printf("Failed to apply overlay %s, skipping\n",
461 overlayfile);
462 goto skip_overlay;
463 }
464
465skip_overlay:
466 if (end)
467 free(overlayfile);
468 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
469}
470#endif
471
Simon Glass18109cc2021-10-14 12:48:01 -0600472/**
473 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard2373cba2019-11-25 09:07:37 +0100474 *
475 * If we can't boot for any reason, we return. A successful boot never
476 * returns.
477 *
478 * The kernel will be stored in the location given by the 'kernel_addr_r'
479 * environment variable.
480 *
481 * If the label specifies an initrd file, it will be stored in the location
482 * given by the 'ramdisk_addr_r' environment variable.
483 *
484 * If the label specifies an 'append' line, its contents will overwrite that
485 * of the 'bootargs' environment variable.
Simon Glass18109cc2021-10-14 12:48:01 -0600486 *
487 * @ctx: PXE context
488 * @label: Label to process
489 * Returns does not return on success, otherwise returns 0 if a localboot
490 * label was processed, or 1 on error
Patrice Chotard2373cba2019-11-25 09:07:37 +0100491 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600492static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +0100493{
Tom Rinicdd20e32024-04-18 08:29:35 -0600494 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
495 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700496 char *kernel_addr = NULL;
497 char *initrd_addr_str = NULL;
Zhaofeng Li23f3e392021-10-20 00:18:14 -0700498 char initrd_filesize[10];
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700499 char initrd_str[28];
Patrice Chotard2373cba2019-11-25 09:07:37 +0100500 char mac_str[29] = "";
501 char ip_str[68] = "";
502 char *fit_addr = NULL;
Tom Rinicdd20e32024-04-18 08:29:35 -0600503 int bootm_argc = 2;
504 int zboot_argc = 3;
505 int len = 0;
506 ulong kernel_addr_r;
507 void *buf;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100508
509 label_print(label);
510
511 label->attempted = 1;
512
513 if (label->localboot) {
514 if (label->localboot_val >= 0)
515 label_localboot(label);
516 return 0;
517 }
518
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100519 if (!label->kernel) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100520 printf("No kernel given, skipping %s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100521 label->name);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100522 return 1;
523 }
524
Patrick Delaunayf723c272022-10-28 11:01:18 +0200525 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
526 NULL) < 0) {
527 printf("Skipping %s for failure retrieving kernel\n",
528 label->name);
529 return 1;
530 }
Simon Glass4d79e882021-10-14 12:48:08 -0600531
Patrick Delaunayf723c272022-10-28 11:01:18 +0200532 kernel_addr = env_get("kernel_addr_r");
533 /* for FIT, append the configuration identifier */
534 if (label->config) {
535 int len = strlen(kernel_addr) + strlen(label->config) + 1;
536
537 fit_addr = malloc(len);
538 if (!fit_addr) {
539 printf("malloc fail (FIT address)\n");
540 return 1;
541 }
542 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
543 kernel_addr = fit_addr;
544 }
545
Patrick Delaunaya5dacef2022-10-28 11:01:19 +0200546 /* For FIT, the label can be identical to kernel one */
547 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
Tom Rinicdd20e32024-04-18 08:29:35 -0600548 initrd_addr_str = kernel_addr;
Patrick Delaunaya5dacef2022-10-28 11:01:19 +0200549 } else if (label->initrd) {
Simon Glass4d79e882021-10-14 12:48:08 -0600550 ulong size;
Simon Glass4d79e882021-10-14 12:48:08 -0600551 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
552 &size) < 0) {
Patrice Chotard2373cba2019-11-25 09:07:37 +0100553 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100554 label->name);
Patrick Delaunayf723c272022-10-28 11:01:18 +0200555 goto cleanup;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100556 }
Thomas Mittelstaedt1a075d42023-05-04 13:42:55 +0000557 strcpy(initrd_filesize, simple_xtoa(size));
Zhaofeng Lic97bd172021-10-20 00:18:15 -0700558 initrd_addr_str = env_get("ramdisk_addr_r");
Heinrich Schuchardt085cbda2021-11-15 19:26:51 +0100559 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
560 initrd_addr_str, size);
561 if (size >= sizeof(initrd_str))
Patrick Delaunayf723c272022-10-28 11:01:18 +0200562 goto cleanup;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100563 }
564
565 if (label->ipappend & 0x1) {
566 sprintf(ip_str, " ip=%s:%s:%s:%s",
567 env_get("ipaddr"), env_get("serverip"),
568 env_get("gatewayip"), env_get("netmask"));
569 }
570
Kory Maincentff0287e2021-02-02 16:42:28 +0100571 if (IS_ENABLED(CONFIG_CMD_NET)) {
572 if (label->ipappend & 0x2) {
573 int err;
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100574
Kory Maincentff0287e2021-02-02 16:42:28 +0100575 strcpy(mac_str, " BOOTIF=");
576 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
577 if (err < 0)
578 mac_str[0] = '\0';
579 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100580 }
Patrice Chotard2373cba2019-11-25 09:07:37 +0100581
582 if ((label->ipappend & 0x3) || label->append) {
583 char bootargs[CONFIG_SYS_CBSIZE] = "";
584 char finalbootargs[CONFIG_SYS_CBSIZE];
585
586 if (strlen(label->append ?: "") +
587 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
588 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
589 strlen(label->append ?: ""),
590 strlen(ip_str), strlen(mac_str),
591 sizeof(bootargs));
Patrick Delaunayf723c272022-10-28 11:01:18 +0200592 goto cleanup;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100593 }
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100594
595 if (label->append)
Tom Rinicdd20e32024-04-18 08:29:35 -0600596 strncpy(bootargs, label->append, sizeof(bootargs));
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100597
598 strcat(bootargs, ip_str);
599 strcat(bootargs, mac_str);
600
Simon Glass1a62d642020-11-05 10:33:47 -0700601 cli_simple_process_macros(bootargs, finalbootargs,
602 sizeof(finalbootargs));
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100603 env_set("bootargs", finalbootargs);
604 printf("append: %s\n", finalbootargs);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100605 }
606
Tom Rinicdd20e32024-04-18 08:29:35 -0600607 /*
608 * fdt usage is optional:
609 * It handles the following scenarios.
610 *
611 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
612 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
613 * bootm, and adjust argc appropriately.
614 *
615 * If retrieve fails and no exact fdt blob is specified in pxe file with
616 * "fdt" label, try Scenario 2.
617 *
618 * Scenario 2: If there is an fdt_addr specified, pass it along to
619 * bootm, and adjust argc appropriately.
620 *
621 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
622 * bootm, and adjust argc appropriately, unless the image type is fitImage.
623 *
624 * Scenario 4: fdt blob is not available.
625 */
626 bootm_argv[3] = env_get("fdt_addr_r");
627
628 /* For FIT, the label can be identical to kernel one */
629 if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
630 bootm_argv[3] = kernel_addr;
631 /* if fdt label is defined then get fdt from server */
632 } else if (bootm_argv[3]) {
633 char *fdtfile = NULL;
634 char *fdtfilefree = NULL;
635
636 if (label->fdt) {
637 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
638 if (strcmp("-", label->fdt))
639 fdtfile = label->fdt;
640 } else {
641 fdtfile = label->fdt;
642 }
643 } else if (label->fdtdir) {
644 char *f1, *f2, *f3, *f4, *slash;
645
646 f1 = env_get("fdtfile");
647 if (f1) {
648 f2 = "";
649 f3 = "";
650 f4 = "";
651 } else {
652 /*
653 * For complex cases where this code doesn't
654 * generate the correct filename, the board
655 * code should set $fdtfile during early boot,
656 * or the boot scripts should set $fdtfile
657 * before invoking "pxe" or "sysboot".
658 */
659 f1 = env_get("soc");
660 f2 = "-";
661 f3 = env_get("board");
662 f4 = ".dtb";
663 if (!f1) {
664 f1 = "";
665 f2 = "";
666 }
667 if (!f3) {
668 f2 = "";
669 f3 = "";
670 }
671 }
672
673 len = strlen(label->fdtdir);
674 if (!len)
675 slash = "./";
676 else if (label->fdtdir[len - 1] != '/')
677 slash = "/";
678 else
679 slash = "";
680
681 len = strlen(label->fdtdir) + strlen(slash) +
682 strlen(f1) + strlen(f2) + strlen(f3) +
683 strlen(f4) + 1;
684 fdtfilefree = malloc(len);
685 if (!fdtfilefree) {
686 printf("malloc fail (FDT filename)\n");
687 goto cleanup;
688 }
689
690 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
691 label->fdtdir, slash, f1, f2, f3, f4);
692 fdtfile = fdtfilefree;
693 }
694
695 if (fdtfile) {
696 int err = get_relfile_envaddr(ctx, fdtfile,
697 "fdt_addr_r", NULL);
698
699 free(fdtfilefree);
700 if (err < 0) {
701 bootm_argv[3] = NULL;
702
703 if (label->fdt) {
704 printf("Skipping %s for failure retrieving FDT\n",
705 label->name);
706 goto cleanup;
707 }
708
709 if (label->fdtdir) {
710 printf("Skipping fdtdir %s for failure retrieving dts\n",
711 label->fdtdir);
712 }
713 }
714
715 if (label->kaslrseed)
716 label_boot_kaslrseed();
717
718#ifdef CONFIG_OF_LIBFDT_OVERLAY
719 if (label->fdtoverlays)
720 label_boot_fdtoverlay(ctx, label);
721#endif
722 } else {
723 bootm_argv[3] = NULL;
724 }
725 }
726
727 bootm_argv[1] = kernel_addr;
728 zboot_argv[1] = kernel_addr;
729
730 if (initrd_addr_str) {
731 bootm_argv[2] = initrd_str;
732 bootm_argc = 3;
733
734 zboot_argv[3] = initrd_addr_str;
735 zboot_argv[4] = initrd_filesize;
736 zboot_argc = 5;
737 }
738
739 if (!bootm_argv[3]) {
740 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
741 if (strcmp("-", label->fdt))
742 bootm_argv[3] = env_get("fdt_addr");
743 } else {
744 bootm_argv[3] = env_get("fdt_addr");
745 }
746 }
747
748 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
749 buf = map_sysmem(kernel_addr_r, 0);
750
751 if (!bootm_argv[3] && genimg_get_format(buf) != IMAGE_FORMAT_FIT) {
752 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
753 if (strcmp("-", label->fdt))
754 bootm_argv[3] = env_get("fdtcontroladdr");
755 } else {
756 bootm_argv[3] = env_get("fdtcontroladdr");
757 }
758 }
759
760 if (bootm_argv[3]) {
761 if (!bootm_argv[2])
762 bootm_argv[2] = "-";
763 bootm_argc = 4;
764 }
765
766 /* Try bootm for legacy and FIT format image */
767 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
768 IS_ENABLED(CONFIG_CMD_BOOTM))
769 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
770 /* Try booting an AArch64 Linux kernel image */
771 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
772 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
773 /* Try booting a Image */
774 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
775 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
776 /* Try booting an x86_64 Linux kernel image */
777 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
778 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
779
780 unmap_sysmem(buf);
Patrice Chotard2373cba2019-11-25 09:07:37 +0100781
782cleanup:
Simon Glass929860b2021-10-14 12:48:02 -0600783 free(fit_addr);
784
Patrice Chotard2373cba2019-11-25 09:07:37 +0100785 return 1;
786}
787
Simon Glass18109cc2021-10-14 12:48:01 -0600788/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100789enum token_type {
790 T_EOL,
791 T_STRING,
792 T_EOF,
793 T_MENU,
794 T_TITLE,
795 T_TIMEOUT,
796 T_LABEL,
797 T_KERNEL,
798 T_LINUX,
799 T_APPEND,
800 T_INITRD,
801 T_LOCALBOOT,
802 T_DEFAULT,
803 T_PROMPT,
804 T_INCLUDE,
805 T_FDT,
806 T_FDTDIR,
Neil Armstrong69076df2021-01-20 09:54:53 +0100807 T_FDTOVERLAYS,
Patrice Chotard2373cba2019-11-25 09:07:37 +0100808 T_ONTIMEOUT,
809 T_IPAPPEND,
810 T_BACKGROUND,
Zhang Ning02901462022-02-01 08:33:37 +0800811 T_KASLRSEED,
Patrice Chotard2373cba2019-11-25 09:07:37 +0100812 T_INVALID
813};
814
Simon Glass18109cc2021-10-14 12:48:01 -0600815/** struct token - token - given by a value and a type */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100816struct token {
817 char *val;
818 enum token_type type;
819};
820
Simon Glass18109cc2021-10-14 12:48:01 -0600821/* Keywords recognized */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100822static const struct token keywords[] = {
823 {"menu", T_MENU},
824 {"title", T_TITLE},
825 {"timeout", T_TIMEOUT},
826 {"default", T_DEFAULT},
827 {"prompt", T_PROMPT},
828 {"label", T_LABEL},
829 {"kernel", T_KERNEL},
830 {"linux", T_LINUX},
831 {"localboot", T_LOCALBOOT},
832 {"append", T_APPEND},
833 {"initrd", T_INITRD},
834 {"include", T_INCLUDE},
835 {"devicetree", T_FDT},
836 {"fdt", T_FDT},
837 {"devicetreedir", T_FDTDIR},
838 {"fdtdir", T_FDTDIR},
Neil Armstrong69076df2021-01-20 09:54:53 +0100839 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri35821a22022-09-21 15:26:33 +0200840 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard2373cba2019-11-25 09:07:37 +0100841 {"ontimeout", T_ONTIMEOUT,},
842 {"ipappend", T_IPAPPEND,},
843 {"background", T_BACKGROUND,},
Zhang Ning02901462022-02-01 08:33:37 +0800844 {"kaslrseed", T_KASLRSEED,},
Patrice Chotard2373cba2019-11-25 09:07:37 +0100845 {NULL, T_INVALID}
846};
847
Simon Glass18109cc2021-10-14 12:48:01 -0600848/**
849 * enum lex_state - lexer state
850 *
Patrice Chotard2373cba2019-11-25 09:07:37 +0100851 * Since pxe(linux) files don't have a token to identify the start of a
852 * literal, we have to keep track of when we're in a state where a literal is
853 * expected vs when we're in a state a keyword is expected.
854 */
855enum lex_state {
856 L_NORMAL = 0,
857 L_KEYWORD,
858 L_SLITERAL
859};
860
Simon Glass18109cc2021-10-14 12:48:01 -0600861/**
862 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100863 *
Simon Glass18109cc2021-10-14 12:48:01 -0600864 * This is used for scanning both string literals and keywords.
Patrice Chotard2373cba2019-11-25 09:07:37 +0100865 *
866 * Characters from *p are copied into t-val until a character equal to
867 * delim is found, or a NUL byte is reached. If delim has the special value of
868 * ' ', any whitespace character will be used as a delimiter.
869 *
870 * If lower is unequal to 0, uppercase characters will be converted to
871 * lowercase in the result. This is useful to make keywords case
872 * insensitive.
873 *
874 * The location of *p is updated to point to the first character after the end
875 * of the token - the ending delimiter.
876 *
Simon Glass18109cc2021-10-14 12:48:01 -0600877 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
878 * it.
879 *
880 * @p: Points to a pointer to the current position in the input being processed.
881 * Updated to point at the first character after the current token
882 * @t: Pointers to a token to fill in
883 * @delim: Delimiter character to look for, either newline or space
884 * @lower: true to convert the string to lower case when storing
885 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard2373cba2019-11-25 09:07:37 +0100886 */
887static char *get_string(char **p, struct token *t, char delim, int lower)
888{
889 char *b, *e;
890 size_t len, i;
891
892 /*
893 * b and e both start at the beginning of the input stream.
894 *
895 * e is incremented until we find the ending delimiter, or a NUL byte
896 * is reached. Then, we take e - b to find the length of the token.
897 */
Patrice Chotard8cb22a62019-11-25 09:07:39 +0100898 b = *p;
899 e = *p;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100900 while (*e) {
901 if ((delim == ' ' && isspace(*e)) || delim == *e)
902 break;
903 e++;
904 }
905
906 len = e - b;
907
908 /*
909 * Allocate memory to hold the string, and copy it in, converting
910 * characters to lowercase if lower is != 0.
911 */
912 t->val = malloc(len + 1);
913 if (!t->val)
914 return NULL;
915
916 for (i = 0; i < len; i++, b++) {
917 if (lower)
918 t->val[i] = tolower(*b);
919 else
920 t->val[i] = *b;
921 }
922
923 t->val[len] = '\0';
924
Simon Glass929860b2021-10-14 12:48:02 -0600925 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard2373cba2019-11-25 09:07:37 +0100926 *p = e;
Patrice Chotard2373cba2019-11-25 09:07:37 +0100927 t->type = T_STRING;
928
929 return t->val;
930}
931
Simon Glass18109cc2021-10-14 12:48:01 -0600932/**
933 * get_keyword() - Populate a keyword token with a type and value
934 *
935 * Updates the ->type field based on the keyword string in @val
936 * @t: Token to populate
Patrice Chotard2373cba2019-11-25 09:07:37 +0100937 */
938static void get_keyword(struct token *t)
939{
940 int i;
941
942 for (i = 0; keywords[i].val; i++) {
943 if (!strcmp(t->val, keywords[i].val)) {
944 t->type = keywords[i].type;
945 break;
946 }
947 }
948}
949
Simon Glass18109cc2021-10-14 12:48:01 -0600950/**
951 * get_token() - Get the next token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100952 *
Simon Glass18109cc2021-10-14 12:48:01 -0600953 * We have to keep track of which state we're in to know if we're looking to get
954 * a string literal or a keyword.
955 *
956 * @p: Points to a pointer to the current position in the input being processed.
957 * Updated to point at the first character after the current token
Patrice Chotard2373cba2019-11-25 09:07:37 +0100958 */
959static void get_token(char **p, struct token *t, enum lex_state state)
960{
961 char *c = *p;
962
963 t->type = T_INVALID;
964
965 /* eat non EOL whitespace */
966 while (isblank(*c))
967 c++;
968
969 /*
970 * eat comments. note that string literals can't begin with #, but
971 * can contain a # after their first character.
972 */
973 if (*c == '#') {
974 while (*c && *c != '\n')
975 c++;
976 }
977
978 if (*c == '\n') {
979 t->type = T_EOL;
980 c++;
981 } else if (*c == '\0') {
982 t->type = T_EOF;
983 c++;
984 } else if (state == L_SLITERAL) {
985 get_string(&c, t, '\n', 0);
986 } else if (state == L_KEYWORD) {
987 /*
988 * when we expect a keyword, we first get the next string
989 * token delimited by whitespace, and then check if it
990 * matches a keyword in our keyword list. if it does, it's
991 * converted to a keyword token of the appropriate type, and
992 * if not, it remains a string token.
993 */
994 get_string(&c, t, ' ', 1);
995 get_keyword(t);
996 }
997
998 *p = c;
999}
1000
Simon Glass18109cc2021-10-14 12:48:01 -06001001/**
1002 * eol_or_eof() - Find end of line
1003 *
1004 * Increment *c until we get to the end of the current line, or EOF
1005 *
1006 * @c: Points to a pointer to the current position in the input being processed.
1007 * Updated to point at the first character after the current token
Patrice Chotard2373cba2019-11-25 09:07:37 +01001008 */
1009static void eol_or_eof(char **c)
1010{
1011 while (**c && **c != '\n')
1012 (*c)++;
1013}
1014
1015/*
1016 * All of these parse_* functions share some common behavior.
1017 *
1018 * They finish with *c pointing after the token they parse, and return 1 on
1019 * success, or < 0 on error.
1020 */
1021
1022/*
1023 * Parse a string literal and store a pointer it at *dst. String literals
1024 * terminate at the end of the line.
1025 */
1026static int parse_sliteral(char **c, char **dst)
1027{
1028 struct token t;
1029 char *s = *c;
1030
1031 get_token(c, &t, L_SLITERAL);
1032
1033 if (t.type != T_STRING) {
1034 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1035 return -EINVAL;
1036 }
1037
1038 *dst = t.val;
1039
1040 return 1;
1041}
1042
1043/*
1044 * Parse a base 10 (unsigned) integer and store it at *dst.
1045 */
1046static int parse_integer(char **c, int *dst)
1047{
1048 struct token t;
1049 char *s = *c;
1050
1051 get_token(c, &t, L_SLITERAL);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001052 if (t.type != T_STRING) {
1053 printf("Expected string: %.*s\n", (int)(*c - s), s);
1054 return -EINVAL;
1055 }
1056
1057 *dst = simple_strtol(t.val, NULL, 10);
1058
1059 free(t.val);
1060
1061 return 1;
1062}
1063
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001064static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001065 struct pxe_menu *cfg, int nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001066
1067/*
1068 * Parse an include statement, and retrieve and parse the file it mentions.
1069 *
1070 * base should point to a location where it's safe to store the file, and
1071 * nest_level should indicate how many nested includes have occurred. For this
1072 * include, nest_level has already been incremented and doesn't need to be
1073 * incremented here.
1074 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001075static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001076 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001077{
1078 char *include_path;
1079 char *s = *c;
1080 int err;
1081 char *buf;
1082 int ret;
1083
1084 err = parse_sliteral(c, &include_path);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001085 if (err < 0) {
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001086 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001087 return err;
1088 }
1089
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001090 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001091 if (err < 0) {
1092 printf("Couldn't retrieve %s\n", include_path);
1093 return err;
1094 }
1095
1096 buf = map_sysmem(base, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001097 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001098 unmap_sysmem(buf);
1099
1100 return ret;
1101}
1102
1103/*
1104 * Parse lines that begin with 'menu'.
1105 *
1106 * base and nest are provided to handle the 'menu include' case.
1107 *
1108 * base should point to a location where it's safe to store the included file.
1109 *
1110 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1111 * a file it includes, 3 when parsing a file included by that file, and so on.
1112 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001113static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001114 unsigned long base, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001115{
1116 struct token t;
1117 char *s = *c;
1118 int err = 0;
1119
1120 get_token(c, &t, L_KEYWORD);
1121
1122 switch (t.type) {
1123 case T_TITLE:
1124 err = parse_sliteral(c, &cfg->title);
1125
1126 break;
1127
1128 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001129 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001130 break;
1131
1132 case T_BACKGROUND:
1133 err = parse_sliteral(c, &cfg->bmp);
1134 break;
1135
1136 default:
1137 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001138 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001139 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001140 if (err < 0)
1141 return err;
1142
1143 eol_or_eof(c);
1144
1145 return 1;
1146}
1147
1148/*
1149 * Handles parsing a 'menu line' when we're parsing a label.
1150 */
1151static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001152 struct pxe_label *label)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001153{
1154 struct token t;
1155 char *s;
1156
1157 s = *c;
1158
1159 get_token(c, &t, L_KEYWORD);
1160
1161 switch (t.type) {
1162 case T_DEFAULT:
1163 if (!cfg->default_label)
1164 cfg->default_label = strdup(label->name);
1165
1166 if (!cfg->default_label)
1167 return -ENOMEM;
1168
1169 break;
1170 case T_LABEL:
1171 parse_sliteral(c, &label->menu);
1172 break;
1173 default:
1174 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001175 (int)(*c - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001176 }
1177
1178 eol_or_eof(c);
1179
1180 return 0;
1181}
1182
1183/*
1184 * Handles parsing a 'kernel' label.
1185 * expecting "filename" or "<fit_filename>#cfg"
1186 */
1187static int parse_label_kernel(char **c, struct pxe_label *label)
1188{
1189 char *s;
1190 int err;
1191
1192 err = parse_sliteral(c, &label->kernel);
1193 if (err < 0)
1194 return err;
1195
Patrick Delaunaya5dacef2022-10-28 11:01:19 +02001196 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1197 label->kernel_label = strdup(label->kernel);
1198 if (!label->kernel_label)
1199 return -ENOMEM;
1200
Patrice Chotard2373cba2019-11-25 09:07:37 +01001201 s = strstr(label->kernel, "#");
1202 if (!s)
1203 return 1;
1204
Patrick Delaunay51c5c282022-10-28 11:01:20 +02001205 label->config = strdup(s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001206 if (!label->config)
1207 return -ENOMEM;
1208
Patrice Chotard2373cba2019-11-25 09:07:37 +01001209 *s = 0;
1210
1211 return 1;
1212}
1213
1214/*
1215 * Parses a label and adds it to the list of labels for a menu.
1216 *
1217 * A label ends when we either get to the end of a file, or
1218 * get some input we otherwise don't have a handler defined
1219 * for.
1220 *
1221 */
1222static int parse_label(char **c, struct pxe_menu *cfg)
1223{
1224 struct token t;
1225 int len;
1226 char *s = *c;
1227 struct pxe_label *label;
1228 int err;
1229
1230 label = label_create();
1231 if (!label)
1232 return -ENOMEM;
1233
1234 err = parse_sliteral(c, &label->name);
1235 if (err < 0) {
1236 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1237 label_destroy(label);
1238 return -EINVAL;
1239 }
1240
1241 list_add_tail(&label->list, &cfg->labels);
1242
1243 while (1) {
1244 s = *c;
1245 get_token(c, &t, L_KEYWORD);
1246
1247 err = 0;
1248 switch (t.type) {
1249 case T_MENU:
1250 err = parse_label_menu(c, cfg, label);
1251 break;
1252
1253 case T_KERNEL:
1254 case T_LINUX:
1255 err = parse_label_kernel(c, label);
1256 break;
1257
1258 case T_APPEND:
1259 err = parse_sliteral(c, &label->append);
1260 if (label->initrd)
1261 break;
1262 s = strstr(label->append, "initrd=");
1263 if (!s)
1264 break;
1265 s += 7;
1266 len = (int)(strchr(s, ' ') - s);
1267 label->initrd = malloc(len + 1);
1268 strncpy(label->initrd, s, len);
1269 label->initrd[len] = '\0';
1270
1271 break;
1272
1273 case T_INITRD:
1274 if (!label->initrd)
1275 err = parse_sliteral(c, &label->initrd);
1276 break;
1277
1278 case T_FDT:
1279 if (!label->fdt)
1280 err = parse_sliteral(c, &label->fdt);
1281 break;
1282
1283 case T_FDTDIR:
1284 if (!label->fdtdir)
1285 err = parse_sliteral(c, &label->fdtdir);
1286 break;
1287
Neil Armstrong69076df2021-01-20 09:54:53 +01001288 case T_FDTOVERLAYS:
1289 if (!label->fdtoverlays)
1290 err = parse_sliteral(c, &label->fdtoverlays);
1291 break;
1292
Patrice Chotard2373cba2019-11-25 09:07:37 +01001293 case T_LOCALBOOT:
1294 label->localboot = 1;
1295 err = parse_integer(c, &label->localboot_val);
1296 break;
1297
1298 case T_IPAPPEND:
1299 err = parse_integer(c, &label->ipappend);
1300 break;
1301
Zhang Ning02901462022-02-01 08:33:37 +08001302 case T_KASLRSEED:
1303 label->kaslrseed = 1;
1304 break;
1305
Patrice Chotard2373cba2019-11-25 09:07:37 +01001306 case T_EOL:
1307 break;
1308
1309 default:
1310 /*
1311 * put the token back! we don't want it - it's the end
1312 * of a label and whatever token this is, it's
1313 * something for the menu level context to handle.
1314 */
1315 *c = s;
1316 return 1;
1317 }
1318
1319 if (err < 0)
1320 return err;
1321 }
1322}
1323
1324/*
1325 * This 16 comes from the limit pxelinux imposes on nested includes.
1326 *
1327 * There is no reason at all we couldn't do more, but some limit helps prevent
1328 * infinite (until crash occurs) recursion if a file tries to include itself.
1329 */
1330#define MAX_NEST_LEVEL 16
1331
1332/*
1333 * Entry point for parsing a menu file. nest_level indicates how many times
1334 * we've nested in includes. It will be 1 for the top level menu file.
1335 *
1336 * Returns 1 on success, < 0 on error.
1337 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001338static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001339 struct pxe_menu *cfg, int nest_level)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001340{
1341 struct token t;
1342 char *s, *b, *label_name;
1343 int err;
1344
1345 b = p;
1346
1347 if (nest_level > MAX_NEST_LEVEL) {
1348 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1349 return -EMLINK;
1350 }
1351
1352 while (1) {
1353 s = p;
1354
1355 get_token(&p, &t, L_KEYWORD);
1356
1357 err = 0;
1358 switch (t.type) {
1359 case T_MENU:
1360 cfg->prompt = 1;
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001361 err = parse_menu(ctx, &p, cfg,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001362 base + ALIGN(strlen(b) + 1, 4),
1363 nest_level);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001364 break;
1365
1366 case T_TIMEOUT:
1367 err = parse_integer(&p, &cfg->timeout);
1368 break;
1369
1370 case T_LABEL:
1371 err = parse_label(&p, cfg);
1372 break;
1373
1374 case T_DEFAULT:
1375 case T_ONTIMEOUT:
1376 err = parse_sliteral(&p, &label_name);
1377
1378 if (label_name) {
1379 if (cfg->default_label)
1380 free(cfg->default_label);
1381
1382 cfg->default_label = label_name;
1383 }
1384
1385 break;
1386
1387 case T_INCLUDE:
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001388 err = handle_include(ctx, &p,
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001389 base + ALIGN(strlen(b), 4), cfg,
1390 nest_level + 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001391 break;
1392
1393 case T_PROMPT:
Manuel Traut739e8362022-11-18 09:00:27 +01001394 err = parse_integer(&p, &cfg->prompt);
1395 // Do not fail if prompt configuration is undefined
1396 if (err < 0)
1397 eol_or_eof(&p);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001398 break;
1399
1400 case T_EOL:
1401 break;
1402
1403 case T_EOF:
1404 return 1;
1405
1406 default:
1407 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard8cb22a62019-11-25 09:07:39 +01001408 (int)(p - s), s);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001409 eol_or_eof(&p);
1410 }
1411
1412 if (err < 0)
1413 return err;
1414 }
1415}
1416
1417/*
Patrice Chotard2373cba2019-11-25 09:07:37 +01001418 */
1419void destroy_pxe_menu(struct pxe_menu *cfg)
1420{
1421 struct list_head *pos, *n;
1422 struct pxe_label *label;
1423
Simon Glass929860b2021-10-14 12:48:02 -06001424 free(cfg->title);
1425 free(cfg->default_label);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001426
1427 list_for_each_safe(pos, n, &cfg->labels) {
1428 label = list_entry(pos, struct pxe_label, list);
1429
1430 label_destroy(label);
1431 }
1432
1433 free(cfg);
1434}
1435
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001436struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001437{
1438 struct pxe_menu *cfg;
1439 char *buf;
1440 int r;
1441
1442 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard2373cba2019-11-25 09:07:37 +01001443 if (!cfg)
1444 return NULL;
1445
1446 memset(cfg, 0, sizeof(struct pxe_menu));
1447
1448 INIT_LIST_HEAD(&cfg->labels);
1449
1450 buf = map_sysmem(menucfg, 0);
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001451 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001452 unmap_sysmem(buf);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001453 if (r < 0) {
1454 destroy_pxe_menu(cfg);
1455 return NULL;
1456 }
1457
1458 return cfg;
1459}
1460
1461/*
1462 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1463 * menu code.
1464 */
1465static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1466{
1467 struct pxe_label *label;
1468 struct list_head *pos;
1469 struct menu *m;
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001470 char *label_override;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001471 int err;
1472 int i = 1;
1473 char *default_num = NULL;
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001474 char *override_num = NULL;
Patrice Chotard2373cba2019-11-25 09:07:37 +01001475
1476 /*
1477 * Create a menu and add items for all the labels.
1478 */
1479 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -07001480 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001481 if (!m)
1482 return NULL;
1483
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001484 label_override = env_get("pxe_label_override");
1485
Patrice Chotard2373cba2019-11-25 09:07:37 +01001486 list_for_each(pos, &cfg->labels) {
1487 label = list_entry(pos, struct pxe_label, list);
1488
1489 sprintf(label->num, "%d", i++);
1490 if (menu_item_add(m, label->num, label) != 1) {
1491 menu_destroy(m);
1492 return NULL;
1493 }
1494 if (cfg->default_label &&
1495 (strcmp(label->name, cfg->default_label) == 0))
1496 default_num = label->num;
Amjad Ouled-Ameurc2969792021-11-13 14:09:20 +01001497 if (label_override && !strcmp(label->name, label_override))
1498 override_num = label->num;
1499 }
1500
1501
1502 if (label_override) {
1503 if (override_num)
1504 default_num = override_num;
1505 else
1506 printf("Missing override pxe label: %s\n",
1507 label_override);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001508 }
1509
1510 /*
1511 * After we've created items for each label in the menu, set the
1512 * menu's default label if one was specified.
1513 */
1514 if (default_num) {
1515 err = menu_default_set(m, default_num);
1516 if (err != 1) {
1517 if (err != -ENOENT) {
1518 menu_destroy(m);
1519 return NULL;
1520 }
1521
1522 printf("Missing default: %s\n", cfg->default_label);
1523 }
1524 }
1525
1526 return m;
1527}
1528
1529/*
1530 * Try to boot any labels we have yet to attempt to boot.
1531 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001532static void boot_unattempted_labels(struct pxe_context *ctx,
1533 struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001534{
1535 struct list_head *pos;
1536 struct pxe_label *label;
1537
1538 list_for_each(pos, &cfg->labels) {
1539 label = list_entry(pos, struct pxe_label, list);
1540
1541 if (!label->attempted)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001542 label_boot(ctx, label);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001543 }
1544}
1545
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001546void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard2373cba2019-11-25 09:07:37 +01001547{
1548 void *choice;
1549 struct menu *m;
1550 int err;
1551
Kory Maincentff0287e2021-02-02 16:42:28 +01001552 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1553 /* display BMP if available */
1554 if (cfg->bmp) {
Simon Glass4d79e882021-10-14 12:48:08 -06001555 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
Simon Glassb86986c2022-10-18 07:46:31 -06001556#if defined(CONFIG_VIDEO)
Patrick Delaunaye6fe02a2022-03-22 17:08:43 +01001557 struct udevice *dev;
1558
1559 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1560 if (!err)
1561 video_clear(dev);
1562#endif
Kory Maincentff0287e2021-02-02 16:42:28 +01001563 bmp_display(image_load_addr,
1564 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1565 } else {
1566 printf("Skipping background bmp %s for failure\n",
1567 cfg->bmp);
1568 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001569 }
1570 }
Patrice Chotard2373cba2019-11-25 09:07:37 +01001571
1572 m = pxe_menu_to_menu(cfg);
1573 if (!m)
1574 return;
1575
1576 err = menu_get_choice(m, &choice);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001577 menu_destroy(m);
1578
1579 /*
1580 * err == 1 means we got a choice back from menu_get_choice.
1581 *
1582 * err == -ENOENT if the menu was setup to select the default but no
1583 * default was set. in that case, we should continue trying to boot
1584 * labels that haven't been attempted yet.
1585 *
1586 * otherwise, the user interrupted or there was some other error and
1587 * we give up.
1588 */
1589
1590 if (err == 1) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001591 err = label_boot(ctx, choice);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001592 if (!err)
1593 return;
1594 } else if (err != -ENOENT) {
1595 return;
1596 }
1597
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001598 boot_unattempted_labels(ctx, cfg);
1599}
1600
Simon Glass12df8422021-10-14 12:48:04 -06001601int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1602 pxe_getfile_func getfile, void *userdata,
Sean Edmond7d018892023-04-11 10:48:47 -07001603 bool allow_abs_path, const char *bootfile, bool use_ipv6)
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001604{
Simon Glass12df8422021-10-14 12:48:04 -06001605 const char *last_slash;
1606 size_t path_len = 0;
1607
1608 memset(ctx, '\0', sizeof(*ctx));
Simon Glassfd3fa5c2021-10-14 12:47:56 -06001609 ctx->cmdtp = cmdtp;
Simon Glassb1ead6b2021-10-14 12:47:57 -06001610 ctx->getfile = getfile;
Simon Glass4ad5d512021-10-14 12:47:58 -06001611 ctx->userdata = userdata;
Simon Glass8018b9a2021-10-14 12:47:59 -06001612 ctx->allow_abs_path = allow_abs_path;
Sean Edmond7d018892023-04-11 10:48:47 -07001613 ctx->use_ipv6 = use_ipv6;
Simon Glass12df8422021-10-14 12:48:04 -06001614
1615 /* figure out the boot directory, if there is one */
1616 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1617 return -ENOSPC;
1618 ctx->bootdir = strdup(bootfile ? bootfile : "");
1619 if (!ctx->bootdir)
1620 return -ENOMEM;
1621
1622 if (bootfile) {
1623 last_slash = strrchr(bootfile, '/');
1624 if (last_slash)
1625 path_len = (last_slash - bootfile) + 1;
1626 }
1627 ctx->bootdir[path_len] = '\0';
1628
1629 return 0;
1630}
1631
1632void pxe_destroy_ctx(struct pxe_context *ctx)
1633{
1634 free(ctx->bootdir);
Patrice Chotard2373cba2019-11-25 09:07:37 +01001635}
Simon Glass9e62e7c2021-10-14 12:48:03 -06001636
1637int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1638{
1639 struct pxe_menu *cfg;
1640
1641 cfg = parse_pxefile(ctx, pxefile_addr_r);
1642 if (!cfg) {
1643 printf("Error parsing config file\n");
1644 return 1;
1645 }
1646
1647 if (prompt)
1648 cfg->prompt = 1;
1649
1650 handle_pxe_menu(ctx, cfg);
1651
1652 destroy_pxe_menu(cfg);
1653
1654 return 0;
1655}