blob: a34f4410e07e18e7e7ae971ce4b7f44132d3de44 [file] [log] [blame]
Patrice Chotard17e88042019-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 Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01009#include <dm.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010010#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010013#include <malloc.h>
14#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010016#include <fdt_support.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010017#include <video.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010018#include <linux/libfdt.h>
Patrice Chotard17e88042019-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 Ning9c1d9c52022-02-01 08:33:37 +080024#ifdef CONFIG_DM_RNG
Zhang Ning9c1d9c52022-02-01 08:33:37 +080025#include <rng.h>
26#endif
27
Patrice Chotard17e88042019-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 Wolsieffer6273f132019-11-28 00:07:08 -050036#define MAX_TFTP_PATH_LEN 512
Patrice Chotard17e88042019-11-25 09:07:37 +010037
Simon Glassa9401b92021-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 Glass00302442021-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 Schuchardt47b4c022022-01-19 18:05:50 +010064 * Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
Simon Glass00302442021-10-14 12:48:01 -060065 * current ethernet device
66 */
Patrice Chotard17e88042019-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 Glass00302442021-10-14 12:48:01 -060073 return -ENOSPC;
Patrice Chotard17e88042019-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 Glass00302442021-10-14 12:48:01 -060086/**
Simon Glass00302442021-10-14 12:48:01 -060087 * get_relfile() - read a file relative to the PXE file
88 *
Patrice Chotard17e88042019-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 Glass00302442021-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 Glassa9401b92021-10-14 12:48:08 -060097 * @filesizep: If not NULL, returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -060098 * Returns 1 for success, or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +010099 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600100static int get_relfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600101 unsigned long file_addr, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100102{
103 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100104 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100105 char addr_buf[18];
Simon Glassa9401b92021-10-14 12:48:08 -0600106 ulong size;
107 int ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100108
Simon Glass5e3e39a2021-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 Chotard17e88042019-11-25 09:07:37 +0100113
Simon Glass5e3e39a2021-10-14 12:48:05 -0600114 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard17e88042019-11-25 09:07:37 +0100115
116 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100117 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-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 Glassa9401b92021-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 Chotard17e88042019-11-25 09:07:37 +0100135}
136
Simon Glass00302442021-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 Glassb0d08db2021-10-14 12:47:56 -0600147int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600148 ulong file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100149{
Simon Glassa9401b92021-10-14 12:48:08 -0600150 ulong size;
Patrice Chotard17e88042019-11-25 09:07:37 +0100151 int err;
152 char *buf;
153
Simon Glassa9401b92021-10-14 12:48:08 -0600154 err = get_relfile(ctx, file_path, file_addr, &size);
Patrice Chotard17e88042019-11-25 09:07:37 +0100155 if (err < 0)
156 return err;
157
Simon Glassa9401b92021-10-14 12:48:08 -0600158 buf = map_sysmem(file_addr + size, 1);
Patrice Chotard17e88042019-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 Glass00302442021-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 Glassb0d08db2021-10-14 12:47:56 -0600175int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100176 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100177{
178 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100179 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-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 Chotard6233de42019-11-25 09:07:39 +0100183 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100184 return -ENAMETOOLONG;
185 }
186
187 sprintf(path, PXELINUX_DIR "%s", file);
188
Simon Glassb0d08db2021-10-14 12:47:56 -0600189 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100190}
191
Simon Glass00302442021-10-14 12:48:01 -0600192/**
193 * get_relfile_envaddr() - read a file to an address in an env var
194 *
Patrice Chotard17e88042019-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 Glass00302442021-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 Glassa9401b92021-10-14 12:48:08 -0600203 * @filesizep: Returns the file size in bytes
Simon Glass00302442021-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 Chotard17e88042019-11-25 09:07:37 +0100207 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600208static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600209 const char *envaddr_name, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100210{
211 unsigned long file_addr;
212 char *envaddr;
213
214 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-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 Glassa9401b92021-10-14 12:48:08 -0600221 return get_relfile(ctx, file_path, file_addr, filesizep);
Patrice Chotard17e88042019-11-25 09:07:37 +0100222}
223
Simon Glass00302442021-10-14 12:48:01 -0600224/**
225 * label_create() - crate a new PXE label
226 *
Patrice Chotard17e88042019-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 Glass00302442021-10-14 12:48:01 -0600230 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-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 Chotard17e88042019-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 Glass00302442021-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 Chotard17e88042019-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 Glass00302442021-10-14 12:48:01 -0600255 *
256 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100257 */
258static void label_destroy(struct pxe_label *label)
259{
Simon Glass764d0c02021-10-14 12:48:02 -0600260 free(label->name);
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +0200261 free(label->kernel_label);
Simon Glass764d0c02021-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 Chotard17e88042019-11-25 09:07:37 +0100269 free(label);
270}
271
Simon Glass00302442021-10-14 12:48:01 -0600272/**
273 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100274 *
275 * This is passed as a callback to the menu code for displaying each
276 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600277 *
278 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-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 Glass00302442021-10-14 12:48:01 -0600288/**
289 * label_localboot() - Boot a label that specified 'localboot'
290 *
291 * 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'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100295 *
Simon Glass00302442021-10-14 12:48:01 -0600296 * @label: Label to process
297 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-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 Chotard17e88042019-11-25 09:07:37 +0100304 if (!localcmd)
305 return -ENOENT;
306
307 if (label->append) {
308 char bootargs[CONFIG_SYS_CBSIZE];
309
Simon Glassc7b03e82020-11-05 10:33:47 -0700310 cli_simple_process_macros(label->append, bootargs,
311 sizeof(bootargs));
Patrice Chotard17e88042019-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}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100319
Zhang Ning9c1d9c52022-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 Glass00302442021-10-14 12:48:01 -0600381/**
382 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200383 * or 'devicetree-overlay'
Simon Glass00302442021-10-14 12:48:01 -0600384 *
385 * @ctx: PXE context
386 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100387 */
388#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600389static void label_boot_fdtoverlay(struct pxe_context *ctx,
390 struct pxe_label *label)
Neil Armstrongc77a1a32021-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 Glass3ff49ec2021-07-24 09:03:29 -0600400 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-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 Glass3ff49ec2021-07-24 09:03:29 -0600413 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-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 Glassa9401b92021-10-14 12:48:08 -0600440 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
441 NULL);
Neil Armstrongc77a1a32021-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
Patrice Chotard17e88042019-11-25 09:07:37 +0100471
Simon Glass00302442021-10-14 12:48:01 -0600472/**
Simon Glassf7eded22023-12-14 21:19:11 -0700473 * calc_fdt_fname() - Figure out the filename to use for the FDT
474 *
475 * Determine the path to the FDT filename, based on the "fdtfile" environment
476 * variable. Use <soc>-<board>.dtb as a fallback
477 *
478 * @fdtdir: Directory to use for the FDT file
479 * Return: allocated filename (including directory), or NULL if out of memory
480 */
481static char *calc_fdt_fname(const char *fdtdir)
482{
483 char *fdtfile;
484 char *f1, *f2, *f3, *f4, *slash;
485 int len;
486
487 f1 = env_get("fdtfile");
488 if (f1) {
489 f2 = "";
490 f3 = "";
491 f4 = "";
492 } else {
493 /*
494 * For complex cases where this code doesn't generate the
495 * correct filename, the board code should set $fdtfile during
496 * early boot, or the boot scripts should set $fdtfile before
497 * invoking "pxe" or "sysboot".
498 */
499 f1 = env_get("soc");
500 f2 = "-";
501 f3 = env_get("board");
502 f4 = ".dtb";
503 if (!f1) {
504 f1 = "";
505 f2 = "";
506 }
507 if (!f3) {
508 f2 = "";
509 f3 = "";
510 }
511 }
512
513 len = strlen(fdtdir);
514 if (!len)
515 slash = "./";
516 else if (fdtdir[len - 1] != '/')
517 slash = "/";
518 else
519 slash = "";
520
521 len = strlen(fdtdir) + strlen(slash) + strlen(f1) + strlen(f2) +
522 strlen(f3) + strlen(f4) + 1;
523 fdtfile = malloc(len);
524 if (!fdtfile) {
525 printf("malloc fail (FDT filename)\n");
526 return NULL;
527 }
528
529 snprintf(fdtfile, len, "%s%s%s%s%s%s", fdtdir, slash, f1, f2, f3, f4);
530
531 return fdtfile;
532}
533
534/**
Simon Glass5df368c2023-12-14 21:19:09 -0700535 * label_run_boot() - Run the correct boot procedure
Patrice Chotard17e88042019-11-25 09:07:37 +0100536 *
Simon Glass5df368c2023-12-14 21:19:09 -0700537 * fdt usage is optional:
538 * It handles the following scenarios.
Patrice Chotard17e88042019-11-25 09:07:37 +0100539 *
Simon Glass5df368c2023-12-14 21:19:09 -0700540 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
541 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
542 * bootm, and adjust argc appropriately.
Patrice Chotard17e88042019-11-25 09:07:37 +0100543 *
Simon Glass5df368c2023-12-14 21:19:09 -0700544 * If retrieve fails and no exact fdt blob is specified in pxe file with
545 * "fdt" label, try Scenario 2.
Patrice Chotard17e88042019-11-25 09:07:37 +0100546 *
Simon Glass5df368c2023-12-14 21:19:09 -0700547 * Scenario 2: If there is an fdt_addr specified, pass it along to
548 * bootm, and adjust argc appropriately.
549 *
550 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
551 * bootm, and adjust argc appropriately, unless the image type is fitImage.
552 *
553 * Scenario 4: fdt blob is not available.
Simon Glass00302442021-10-14 12:48:01 -0600554 *
555 * @ctx: PXE context
556 * @label: Label to process
Simon Glass5df368c2023-12-14 21:19:09 -0700557 * @kernel_addr: string containing the kernel address / config
558 * @initrd_str: string containing the initrd address / size
559 * @initrd_addr_str: initrd address, or NULL if none
560 * @initrd_filesize: initrd size in bytes; only valid if initrd_addr_str is not
561 * NULL
Simon Glass00302442021-10-14 12:48:01 -0600562 * Returns does not return on success, otherwise returns 0 if a localboot
563 * label was processed, or 1 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100564 */
Simon Glass5df368c2023-12-14 21:19:09 -0700565static int label_run_boot(struct pxe_context *ctx, struct pxe_label *label,
566 char *kernel_addr, char *initrd_str,
567 char *initrd_addr_str, char *initrd_filesize)
Patrice Chotard17e88042019-11-25 09:07:37 +0100568{
569 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700570 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Simon Glassb5e0eb82023-12-14 21:19:10 -0700571 const char *fdt_addr;
Simon Glass5df368c2023-12-14 21:19:09 -0700572 ulong kernel_addr_r;
Patrice Chotard17e88042019-11-25 09:07:37 +0100573 int bootm_argc = 2;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700574 int zboot_argc = 3;
Patrice Chotard17e88042019-11-25 09:07:37 +0100575 void *buf;
576
Simon Glassb5e0eb82023-12-14 21:19:10 -0700577 fdt_addr = env_get("fdt_addr_r");
Patrice Chotard17e88042019-11-25 09:07:37 +0100578
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +0200579 /* For FIT, the label can be identical to kernel one */
580 if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
Simon Glassb5e0eb82023-12-14 21:19:10 -0700581 fdt_addr = kernel_addr;
Patrice Chotard17e88042019-11-25 09:07:37 +0100582 /* if fdt label is defined then get fdt from server */
Simon Glassb5e0eb82023-12-14 21:19:10 -0700583 } else if (fdt_addr) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100584 char *fdtfile = NULL;
585 char *fdtfilefree = NULL;
586
587 if (label->fdt) {
588 fdtfile = label->fdt;
589 } else if (label->fdtdir) {
Simon Glassf7eded22023-12-14 21:19:11 -0700590 fdtfilefree = calc_fdt_fname(label->fdtdir);
591 if (!fdtfilefree)
Simon Glass5df368c2023-12-14 21:19:09 -0700592 return -ENOMEM;
Patrice Chotard17e88042019-11-25 09:07:37 +0100593 fdtfile = fdtfilefree;
594 }
595
596 if (fdtfile) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600597 int err = get_relfile_envaddr(ctx, fdtfile,
Simon Glassa9401b92021-10-14 12:48:08 -0600598 "fdt_addr_r", NULL);
Patrice Chotard6233de42019-11-25 09:07:39 +0100599
Patrice Chotard17e88042019-11-25 09:07:37 +0100600 free(fdtfilefree);
601 if (err < 0) {
Simon Glassb5e0eb82023-12-14 21:19:10 -0700602 fdt_addr = NULL;
Anton Leontievfeb7db82019-09-03 10:52:24 +0300603
604 if (label->fdt) {
605 printf("Skipping %s for failure retrieving FDT\n",
606 label->name);
Simon Glass5df368c2023-12-14 21:19:09 -0700607 return -ENOENT;
Anton Leontievfeb7db82019-09-03 10:52:24 +0300608 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100609 }
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100610
Dan Carpenter18b01e02023-07-27 10:12:39 +0300611 if (label->kaslrseed)
612 label_boot_kaslrseed();
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800613
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100614#ifdef CONFIG_OF_LIBFDT_OVERLAY
615 if (label->fdtoverlays)
Simon Glassb0d08db2021-10-14 12:47:56 -0600616 label_boot_fdtoverlay(ctx, label);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100617#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100618 } else {
Simon Glassb5e0eb82023-12-14 21:19:10 -0700619 fdt_addr = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +0100620 }
621 }
622
Zhaofeng Libc14b932021-10-20 00:18:15 -0700623 bootm_argv[1] = kernel_addr;
624 zboot_argv[1] = kernel_addr;
625
626 if (initrd_addr_str) {
627 bootm_argv[2] = initrd_str;
628 bootm_argc = 3;
629
630 zboot_argv[3] = initrd_addr_str;
631 zboot_argv[4] = initrd_filesize;
632 zboot_argc = 5;
633 }
634
Simon Glassb5e0eb82023-12-14 21:19:10 -0700635 if (!fdt_addr)
636 fdt_addr = env_get("fdt_addr");
Patrice Chotard17e88042019-11-25 09:07:37 +0100637
Marek Vasutf10df772022-12-14 07:45:18 +0100638 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
639 buf = map_sysmem(kernel_addr_r, 0);
640
Simon Glassb5e0eb82023-12-14 21:19:10 -0700641 if (!fdt_addr && genimg_get_format(buf) != IMAGE_FORMAT_FIT)
642 fdt_addr = env_get("fdtcontroladdr");
Marek Vasutbe6e05b2022-12-17 18:41:13 +0100643
Simon Glassb5e0eb82023-12-14 21:19:10 -0700644 if (fdt_addr) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100645 if (!bootm_argv[2])
646 bootm_argv[2] = "-";
647 bootm_argc = 4;
648 }
Simon Glassb5e0eb82023-12-14 21:19:10 -0700649 bootm_argv[3] = (char *)fdt_addr;
Patrice Chotard17e88042019-11-25 09:07:37 +0100650
Patrice Chotard17e88042019-11-25 09:07:37 +0100651 /* Try bootm for legacy and FIT format image */
John Keeping9b60a172022-07-28 11:19:15 +0100652 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
653 IS_ENABLED(CONFIG_CMD_BOOTM))
Simon Glassb0d08db2021-10-14 12:47:56 -0600654 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100655 /* Try booting an AArch64 Linux kernel image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100656 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassb0d08db2021-10-14 12:47:56 -0600657 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100658 /* Try booting a Image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100659 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassb0d08db2021-10-14 12:47:56 -0600660 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincentb2187d02021-02-02 16:42:29 +0100661 /* Try booting an x86_64 Linux kernel image */
662 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassb0d08db2021-10-14 12:47:56 -0600663 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentcaabd242021-02-02 16:42:28 +0100664
Patrice Chotard17e88042019-11-25 09:07:37 +0100665 unmap_sysmem(buf);
666
Simon Glass5df368c2023-12-14 21:19:09 -0700667 return 0;
668}
669
670/**
671 * label_boot() - Boot according to the contents of a pxe_label
672 *
673 * If we can't boot for any reason, we return. A successful boot never
674 * returns.
675 *
676 * The kernel will be stored in the location given by the 'kernel_addr_r'
677 * environment variable.
678 *
679 * If the label specifies an initrd file, it will be stored in the location
680 * given by the 'ramdisk_addr_r' environment variable.
681 *
682 * If the label specifies an 'append' line, its contents will overwrite that
683 * of the 'bootargs' environment variable.
684 *
685 * @ctx: PXE context
686 * @label: Label to process
687 * Returns does not return on success, otherwise returns 0 if a localboot
688 * label was processed, or 1 on error
689 */
690static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
691{
692 char *kernel_addr = NULL;
693 char *initrd_addr_str = NULL;
694 char initrd_filesize[10];
695 char initrd_str[28];
696 char mac_str[29] = "";
697 char ip_str[68] = "";
698 char *fit_addr = NULL;
699 int ret;
700
701 label_print(label);
702
703 label->attempted = 1;
704
705 if (label->localboot) {
706 if (label->localboot_val >= 0)
707 label_localboot(label);
708 return 0;
709 }
710
711 if (!label->kernel) {
712 printf("No kernel given, skipping %s\n",
713 label->name);
714 return 1;
715 }
716
717 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
718 NULL) < 0) {
719 printf("Skipping %s for failure retrieving kernel\n",
720 label->name);
721 return 1;
722 }
723
724 kernel_addr = env_get("kernel_addr_r");
725 /* for FIT, append the configuration identifier */
726 if (label->config) {
727 int len = strlen(kernel_addr) + strlen(label->config) + 1;
728
729 fit_addr = malloc(len);
730 if (!fit_addr) {
731 printf("malloc fail (FIT address)\n");
732 return 1;
733 }
734 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
735 kernel_addr = fit_addr;
736 }
737
738 /* For FIT, the label can be identical to kernel one */
739 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
740 initrd_addr_str = kernel_addr;
741 } else if (label->initrd) {
742 ulong size;
743
744 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
745 &size) < 0) {
746 printf("Skipping %s for failure retrieving initrd\n",
747 label->name);
748 goto cleanup;
749 }
750 strcpy(initrd_filesize, simple_xtoa(size));
751 initrd_addr_str = env_get("ramdisk_addr_r");
752 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
753 initrd_addr_str, size);
754 if (size >= sizeof(initrd_str))
755 goto cleanup;
756 }
757
758 if (label->ipappend & 0x1) {
759 sprintf(ip_str, " ip=%s:%s:%s:%s",
760 env_get("ipaddr"), env_get("serverip"),
761 env_get("gatewayip"), env_get("netmask"));
762 }
763
764 if (IS_ENABLED(CONFIG_CMD_NET)) {
765 if (label->ipappend & 0x2) {
766 int err;
767
768 strcpy(mac_str, " BOOTIF=");
769 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
770 if (err < 0)
771 mac_str[0] = '\0';
772 }
773 }
774
775 if ((label->ipappend & 0x3) || label->append) {
776 char bootargs[CONFIG_SYS_CBSIZE] = "";
777 char finalbootargs[CONFIG_SYS_CBSIZE];
778
779 if (strlen(label->append ?: "") +
780 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
781 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
782 strlen(label->append ?: ""),
783 strlen(ip_str), strlen(mac_str),
784 sizeof(bootargs));
785 goto cleanup;
786 }
787
788 if (label->append)
789 strlcpy(bootargs, label->append, sizeof(bootargs));
790
791 strcat(bootargs, ip_str);
792 strcat(bootargs, mac_str);
793
794 cli_simple_process_macros(bootargs, finalbootargs,
795 sizeof(finalbootargs));
796 env_set("bootargs", finalbootargs);
797 printf("append: %s\n", finalbootargs);
798 }
799
800 ret = label_run_boot(ctx, label, kernel_addr, initrd_str,
801 initrd_addr_str, initrd_filesize);
802
Patrice Chotard17e88042019-11-25 09:07:37 +0100803cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600804 free(fit_addr);
805
Patrice Chotard17e88042019-11-25 09:07:37 +0100806 return 1;
807}
808
Simon Glass00302442021-10-14 12:48:01 -0600809/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100810enum token_type {
811 T_EOL,
812 T_STRING,
813 T_EOF,
814 T_MENU,
815 T_TITLE,
816 T_TIMEOUT,
817 T_LABEL,
818 T_KERNEL,
819 T_LINUX,
820 T_APPEND,
821 T_INITRD,
822 T_LOCALBOOT,
823 T_DEFAULT,
824 T_PROMPT,
825 T_INCLUDE,
826 T_FDT,
827 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100828 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100829 T_ONTIMEOUT,
830 T_IPAPPEND,
831 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800832 T_KASLRSEED,
Patrice Chotard17e88042019-11-25 09:07:37 +0100833 T_INVALID
834};
835
Simon Glass00302442021-10-14 12:48:01 -0600836/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100837struct token {
838 char *val;
839 enum token_type type;
840};
841
Simon Glass00302442021-10-14 12:48:01 -0600842/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100843static const struct token keywords[] = {
844 {"menu", T_MENU},
845 {"title", T_TITLE},
846 {"timeout", T_TIMEOUT},
847 {"default", T_DEFAULT},
848 {"prompt", T_PROMPT},
849 {"label", T_LABEL},
850 {"kernel", T_KERNEL},
851 {"linux", T_LINUX},
852 {"localboot", T_LOCALBOOT},
853 {"append", T_APPEND},
854 {"initrd", T_INITRD},
855 {"include", T_INCLUDE},
856 {"devicetree", T_FDT},
857 {"fdt", T_FDT},
858 {"devicetreedir", T_FDTDIR},
859 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100860 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200861 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100862 {"ontimeout", T_ONTIMEOUT,},
863 {"ipappend", T_IPAPPEND,},
864 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800865 {"kaslrseed", T_KASLRSEED,},
Patrice Chotard17e88042019-11-25 09:07:37 +0100866 {NULL, T_INVALID}
867};
868
Simon Glass00302442021-10-14 12:48:01 -0600869/**
870 * enum lex_state - lexer state
871 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100872 * Since pxe(linux) files don't have a token to identify the start of a
873 * literal, we have to keep track of when we're in a state where a literal is
874 * expected vs when we're in a state a keyword is expected.
875 */
876enum lex_state {
877 L_NORMAL = 0,
878 L_KEYWORD,
879 L_SLITERAL
880};
881
Simon Glass00302442021-10-14 12:48:01 -0600882/**
883 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100884 *
Simon Glass00302442021-10-14 12:48:01 -0600885 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100886 *
887 * Characters from *p are copied into t-val until a character equal to
888 * delim is found, or a NUL byte is reached. If delim has the special value of
889 * ' ', any whitespace character will be used as a delimiter.
890 *
891 * If lower is unequal to 0, uppercase characters will be converted to
892 * lowercase in the result. This is useful to make keywords case
893 * insensitive.
894 *
895 * The location of *p is updated to point to the first character after the end
896 * of the token - the ending delimiter.
897 *
Simon Glass00302442021-10-14 12:48:01 -0600898 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
899 * it.
900 *
901 * @p: Points to a pointer to the current position in the input being processed.
902 * Updated to point at the first character after the current token
903 * @t: Pointers to a token to fill in
904 * @delim: Delimiter character to look for, either newline or space
905 * @lower: true to convert the string to lower case when storing
906 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100907 */
908static char *get_string(char **p, struct token *t, char delim, int lower)
909{
910 char *b, *e;
911 size_t len, i;
912
913 /*
914 * b and e both start at the beginning of the input stream.
915 *
916 * e is incremented until we find the ending delimiter, or a NUL byte
917 * is reached. Then, we take e - b to find the length of the token.
918 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100919 b = *p;
920 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100921 while (*e) {
922 if ((delim == ' ' && isspace(*e)) || delim == *e)
923 break;
924 e++;
925 }
926
927 len = e - b;
928
929 /*
930 * Allocate memory to hold the string, and copy it in, converting
931 * characters to lowercase if lower is != 0.
932 */
933 t->val = malloc(len + 1);
934 if (!t->val)
935 return NULL;
936
937 for (i = 0; i < len; i++, b++) {
938 if (lower)
939 t->val[i] = tolower(*b);
940 else
941 t->val[i] = *b;
942 }
943
944 t->val[len] = '\0';
945
Simon Glass764d0c02021-10-14 12:48:02 -0600946 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100947 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100948 t->type = T_STRING;
949
950 return t->val;
951}
952
Simon Glass00302442021-10-14 12:48:01 -0600953/**
954 * get_keyword() - Populate a keyword token with a type and value
955 *
956 * Updates the ->type field based on the keyword string in @val
957 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100958 */
959static void get_keyword(struct token *t)
960{
961 int i;
962
963 for (i = 0; keywords[i].val; i++) {
964 if (!strcmp(t->val, keywords[i].val)) {
965 t->type = keywords[i].type;
966 break;
967 }
968 }
969}
970
Simon Glass00302442021-10-14 12:48:01 -0600971/**
972 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100973 *
Simon Glass00302442021-10-14 12:48:01 -0600974 * We have to keep track of which state we're in to know if we're looking to get
975 * a string literal or a keyword.
976 *
977 * @p: Points to a pointer to the current position in the input being processed.
978 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100979 */
980static void get_token(char **p, struct token *t, enum lex_state state)
981{
982 char *c = *p;
983
984 t->type = T_INVALID;
985
986 /* eat non EOL whitespace */
987 while (isblank(*c))
988 c++;
989
990 /*
991 * eat comments. note that string literals can't begin with #, but
992 * can contain a # after their first character.
993 */
994 if (*c == '#') {
995 while (*c && *c != '\n')
996 c++;
997 }
998
999 if (*c == '\n') {
1000 t->type = T_EOL;
1001 c++;
1002 } else if (*c == '\0') {
1003 t->type = T_EOF;
1004 c++;
1005 } else if (state == L_SLITERAL) {
1006 get_string(&c, t, '\n', 0);
1007 } else if (state == L_KEYWORD) {
1008 /*
1009 * when we expect a keyword, we first get the next string
1010 * token delimited by whitespace, and then check if it
1011 * matches a keyword in our keyword list. if it does, it's
1012 * converted to a keyword token of the appropriate type, and
1013 * if not, it remains a string token.
1014 */
1015 get_string(&c, t, ' ', 1);
1016 get_keyword(t);
1017 }
1018
1019 *p = c;
1020}
1021
Simon Glass00302442021-10-14 12:48:01 -06001022/**
1023 * eol_or_eof() - Find end of line
1024 *
1025 * Increment *c until we get to the end of the current line, or EOF
1026 *
1027 * @c: Points to a pointer to the current position in the input being processed.
1028 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +01001029 */
1030static void eol_or_eof(char **c)
1031{
1032 while (**c && **c != '\n')
1033 (*c)++;
1034}
1035
1036/*
1037 * All of these parse_* functions share some common behavior.
1038 *
1039 * They finish with *c pointing after the token they parse, and return 1 on
1040 * success, or < 0 on error.
1041 */
1042
1043/*
1044 * Parse a string literal and store a pointer it at *dst. String literals
1045 * terminate at the end of the line.
1046 */
1047static int parse_sliteral(char **c, char **dst)
1048{
1049 struct token t;
1050 char *s = *c;
1051
1052 get_token(c, &t, L_SLITERAL);
1053
1054 if (t.type != T_STRING) {
1055 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1056 return -EINVAL;
1057 }
1058
1059 *dst = t.val;
1060
1061 return 1;
1062}
1063
1064/*
1065 * Parse a base 10 (unsigned) integer and store it at *dst.
1066 */
1067static int parse_integer(char **c, int *dst)
1068{
1069 struct token t;
1070 char *s = *c;
1071
1072 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001073 if (t.type != T_STRING) {
1074 printf("Expected string: %.*s\n", (int)(*c - s), s);
1075 return -EINVAL;
1076 }
1077
1078 *dst = simple_strtol(t.val, NULL, 10);
1079
1080 free(t.val);
1081
1082 return 1;
1083}
1084
Simon Glassb0d08db2021-10-14 12:47:56 -06001085static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001086 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001087
1088/*
1089 * Parse an include statement, and retrieve and parse the file it mentions.
1090 *
1091 * base should point to a location where it's safe to store the file, and
1092 * nest_level should indicate how many nested includes have occurred. For this
1093 * include, nest_level has already been incremented and doesn't need to be
1094 * incremented here.
1095 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001096static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001097 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001098{
1099 char *include_path;
1100 char *s = *c;
1101 int err;
1102 char *buf;
1103 int ret;
1104
1105 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001106 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001107 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001108 return err;
1109 }
1110
Simon Glassb0d08db2021-10-14 12:47:56 -06001111 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001112 if (err < 0) {
1113 printf("Couldn't retrieve %s\n", include_path);
1114 return err;
1115 }
1116
1117 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001118 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001119 unmap_sysmem(buf);
1120
1121 return ret;
1122}
1123
1124/*
1125 * Parse lines that begin with 'menu'.
1126 *
1127 * base and nest are provided to handle the 'menu include' case.
1128 *
1129 * base should point to a location where it's safe to store the included file.
1130 *
1131 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1132 * a file it includes, 3 when parsing a file included by that file, and so on.
1133 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001134static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001135 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001136{
1137 struct token t;
1138 char *s = *c;
1139 int err = 0;
1140
1141 get_token(c, &t, L_KEYWORD);
1142
1143 switch (t.type) {
1144 case T_TITLE:
1145 err = parse_sliteral(c, &cfg->title);
1146
1147 break;
1148
1149 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001150 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001151 break;
1152
1153 case T_BACKGROUND:
1154 err = parse_sliteral(c, &cfg->bmp);
1155 break;
1156
1157 default:
1158 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001159 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001160 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001161 if (err < 0)
1162 return err;
1163
1164 eol_or_eof(c);
1165
1166 return 1;
1167}
1168
1169/*
1170 * Handles parsing a 'menu line' when we're parsing a label.
1171 */
1172static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001173 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001174{
1175 struct token t;
1176 char *s;
1177
1178 s = *c;
1179
1180 get_token(c, &t, L_KEYWORD);
1181
1182 switch (t.type) {
1183 case T_DEFAULT:
1184 if (!cfg->default_label)
1185 cfg->default_label = strdup(label->name);
1186
1187 if (!cfg->default_label)
1188 return -ENOMEM;
1189
1190 break;
1191 case T_LABEL:
1192 parse_sliteral(c, &label->menu);
1193 break;
1194 default:
1195 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001196 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001197 }
1198
1199 eol_or_eof(c);
1200
1201 return 0;
1202}
1203
1204/*
1205 * Handles parsing a 'kernel' label.
1206 * expecting "filename" or "<fit_filename>#cfg"
1207 */
1208static int parse_label_kernel(char **c, struct pxe_label *label)
1209{
1210 char *s;
1211 int err;
1212
1213 err = parse_sliteral(c, &label->kernel);
1214 if (err < 0)
1215 return err;
1216
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +02001217 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1218 label->kernel_label = strdup(label->kernel);
1219 if (!label->kernel_label)
1220 return -ENOMEM;
1221
Patrice Chotard17e88042019-11-25 09:07:37 +01001222 s = strstr(label->kernel, "#");
1223 if (!s)
1224 return 1;
1225
Patrick Delaunay9d464f82022-10-28 11:01:20 +02001226 label->config = strdup(s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001227 if (!label->config)
1228 return -ENOMEM;
1229
Patrice Chotard17e88042019-11-25 09:07:37 +01001230 *s = 0;
1231
1232 return 1;
1233}
1234
1235/*
1236 * Parses a label and adds it to the list of labels for a menu.
1237 *
1238 * A label ends when we either get to the end of a file, or
1239 * get some input we otherwise don't have a handler defined
1240 * for.
1241 *
1242 */
1243static int parse_label(char **c, struct pxe_menu *cfg)
1244{
1245 struct token t;
1246 int len;
1247 char *s = *c;
1248 struct pxe_label *label;
1249 int err;
1250
1251 label = label_create();
1252 if (!label)
1253 return -ENOMEM;
1254
1255 err = parse_sliteral(c, &label->name);
1256 if (err < 0) {
1257 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1258 label_destroy(label);
1259 return -EINVAL;
1260 }
1261
1262 list_add_tail(&label->list, &cfg->labels);
1263
1264 while (1) {
1265 s = *c;
1266 get_token(c, &t, L_KEYWORD);
1267
1268 err = 0;
1269 switch (t.type) {
1270 case T_MENU:
1271 err = parse_label_menu(c, cfg, label);
1272 break;
1273
1274 case T_KERNEL:
1275 case T_LINUX:
1276 err = parse_label_kernel(c, label);
1277 break;
1278
1279 case T_APPEND:
1280 err = parse_sliteral(c, &label->append);
1281 if (label->initrd)
1282 break;
1283 s = strstr(label->append, "initrd=");
1284 if (!s)
1285 break;
1286 s += 7;
1287 len = (int)(strchr(s, ' ') - s);
1288 label->initrd = malloc(len + 1);
1289 strncpy(label->initrd, s, len);
1290 label->initrd[len] = '\0';
1291
1292 break;
1293
1294 case T_INITRD:
1295 if (!label->initrd)
1296 err = parse_sliteral(c, &label->initrd);
1297 break;
1298
1299 case T_FDT:
1300 if (!label->fdt)
1301 err = parse_sliteral(c, &label->fdt);
1302 break;
1303
1304 case T_FDTDIR:
1305 if (!label->fdtdir)
1306 err = parse_sliteral(c, &label->fdtdir);
1307 break;
1308
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001309 case T_FDTOVERLAYS:
1310 if (!label->fdtoverlays)
1311 err = parse_sliteral(c, &label->fdtoverlays);
1312 break;
1313
Patrice Chotard17e88042019-11-25 09:07:37 +01001314 case T_LOCALBOOT:
1315 label->localboot = 1;
1316 err = parse_integer(c, &label->localboot_val);
1317 break;
1318
1319 case T_IPAPPEND:
1320 err = parse_integer(c, &label->ipappend);
1321 break;
1322
Zhang Ning9c1d9c52022-02-01 08:33:37 +08001323 case T_KASLRSEED:
1324 label->kaslrseed = 1;
1325 break;
1326
Patrice Chotard17e88042019-11-25 09:07:37 +01001327 case T_EOL:
1328 break;
1329
1330 default:
1331 /*
1332 * put the token back! we don't want it - it's the end
1333 * of a label and whatever token this is, it's
1334 * something for the menu level context to handle.
1335 */
1336 *c = s;
1337 return 1;
1338 }
1339
1340 if (err < 0)
1341 return err;
1342 }
1343}
1344
1345/*
1346 * This 16 comes from the limit pxelinux imposes on nested includes.
1347 *
1348 * There is no reason at all we couldn't do more, but some limit helps prevent
1349 * infinite (until crash occurs) recursion if a file tries to include itself.
1350 */
1351#define MAX_NEST_LEVEL 16
1352
1353/*
1354 * Entry point for parsing a menu file. nest_level indicates how many times
1355 * we've nested in includes. It will be 1 for the top level menu file.
1356 *
1357 * Returns 1 on success, < 0 on error.
1358 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001359static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001360 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001361{
1362 struct token t;
1363 char *s, *b, *label_name;
1364 int err;
1365
1366 b = p;
1367
1368 if (nest_level > MAX_NEST_LEVEL) {
1369 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1370 return -EMLINK;
1371 }
1372
1373 while (1) {
1374 s = p;
1375
1376 get_token(&p, &t, L_KEYWORD);
1377
1378 err = 0;
1379 switch (t.type) {
1380 case T_MENU:
1381 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001382 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001383 base + ALIGN(strlen(b) + 1, 4),
1384 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001385 break;
1386
1387 case T_TIMEOUT:
1388 err = parse_integer(&p, &cfg->timeout);
1389 break;
1390
1391 case T_LABEL:
1392 err = parse_label(&p, cfg);
1393 break;
1394
1395 case T_DEFAULT:
1396 case T_ONTIMEOUT:
1397 err = parse_sliteral(&p, &label_name);
1398
1399 if (label_name) {
1400 if (cfg->default_label)
1401 free(cfg->default_label);
1402
1403 cfg->default_label = label_name;
1404 }
1405
1406 break;
1407
1408 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001409 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001410 base + ALIGN(strlen(b), 4), cfg,
1411 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001412 break;
1413
1414 case T_PROMPT:
Manuel Traut20081242022-11-18 09:00:27 +01001415 err = parse_integer(&p, &cfg->prompt);
1416 // Do not fail if prompt configuration is undefined
1417 if (err < 0)
1418 eol_or_eof(&p);
Patrice Chotard17e88042019-11-25 09:07:37 +01001419 break;
1420
1421 case T_EOL:
1422 break;
1423
1424 case T_EOF:
1425 return 1;
1426
1427 default:
1428 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001429 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001430 eol_or_eof(&p);
1431 }
1432
1433 if (err < 0)
1434 return err;
1435 }
1436}
1437
1438/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001439 */
1440void destroy_pxe_menu(struct pxe_menu *cfg)
1441{
1442 struct list_head *pos, *n;
1443 struct pxe_label *label;
1444
Simon Glass764d0c02021-10-14 12:48:02 -06001445 free(cfg->title);
1446 free(cfg->default_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001447
1448 list_for_each_safe(pos, n, &cfg->labels) {
1449 label = list_entry(pos, struct pxe_label, list);
1450
1451 label_destroy(label);
1452 }
1453
1454 free(cfg);
1455}
1456
Simon Glassb0d08db2021-10-14 12:47:56 -06001457struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001458{
1459 struct pxe_menu *cfg;
1460 char *buf;
1461 int r;
1462
1463 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001464 if (!cfg)
1465 return NULL;
1466
1467 memset(cfg, 0, sizeof(struct pxe_menu));
1468
1469 INIT_LIST_HEAD(&cfg->labels);
1470
1471 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001472 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001473 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001474 if (r < 0) {
1475 destroy_pxe_menu(cfg);
1476 return NULL;
1477 }
1478
1479 return cfg;
1480}
1481
1482/*
1483 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1484 * menu code.
1485 */
1486static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1487{
1488 struct pxe_label *label;
1489 struct list_head *pos;
1490 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001491 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001492 int err;
1493 int i = 1;
1494 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001495 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001496
1497 /*
1498 * Create a menu and add items for all the labels.
1499 */
1500 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -07001501 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001502 if (!m)
1503 return NULL;
1504
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001505 label_override = env_get("pxe_label_override");
1506
Patrice Chotard17e88042019-11-25 09:07:37 +01001507 list_for_each(pos, &cfg->labels) {
1508 label = list_entry(pos, struct pxe_label, list);
1509
1510 sprintf(label->num, "%d", i++);
1511 if (menu_item_add(m, label->num, label) != 1) {
1512 menu_destroy(m);
1513 return NULL;
1514 }
1515 if (cfg->default_label &&
1516 (strcmp(label->name, cfg->default_label) == 0))
1517 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001518 if (label_override && !strcmp(label->name, label_override))
1519 override_num = label->num;
1520 }
1521
1522
1523 if (label_override) {
1524 if (override_num)
1525 default_num = override_num;
1526 else
1527 printf("Missing override pxe label: %s\n",
1528 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001529 }
1530
1531 /*
1532 * After we've created items for each label in the menu, set the
1533 * menu's default label if one was specified.
1534 */
1535 if (default_num) {
1536 err = menu_default_set(m, default_num);
1537 if (err != 1) {
1538 if (err != -ENOENT) {
1539 menu_destroy(m);
1540 return NULL;
1541 }
1542
1543 printf("Missing default: %s\n", cfg->default_label);
1544 }
1545 }
1546
1547 return m;
1548}
1549
1550/*
1551 * Try to boot any labels we have yet to attempt to boot.
1552 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001553static void boot_unattempted_labels(struct pxe_context *ctx,
1554 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001555{
1556 struct list_head *pos;
1557 struct pxe_label *label;
1558
1559 list_for_each(pos, &cfg->labels) {
1560 label = list_entry(pos, struct pxe_label, list);
1561
1562 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001563 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001564 }
1565}
1566
Simon Glassb0d08db2021-10-14 12:47:56 -06001567void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001568{
1569 void *choice;
1570 struct menu *m;
1571 int err;
1572
Kory Maincentcaabd242021-02-02 16:42:28 +01001573 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1574 /* display BMP if available */
1575 if (cfg->bmp) {
Simon Glassa9401b92021-10-14 12:48:08 -06001576 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001577#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001578 struct udevice *dev;
1579
1580 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1581 if (!err)
1582 video_clear(dev);
1583#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001584 bmp_display(image_load_addr,
1585 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1586 } else {
1587 printf("Skipping background bmp %s for failure\n",
1588 cfg->bmp);
1589 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001590 }
1591 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001592
1593 m = pxe_menu_to_menu(cfg);
1594 if (!m)
1595 return;
1596
1597 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001598 menu_destroy(m);
1599
1600 /*
1601 * err == 1 means we got a choice back from menu_get_choice.
1602 *
1603 * err == -ENOENT if the menu was setup to select the default but no
1604 * default was set. in that case, we should continue trying to boot
1605 * labels that haven't been attempted yet.
1606 *
1607 * otherwise, the user interrupted or there was some other error and
1608 * we give up.
1609 */
1610
1611 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001612 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001613 if (!err)
1614 return;
1615 } else if (err != -ENOENT) {
1616 return;
1617 }
1618
Simon Glassb0d08db2021-10-14 12:47:56 -06001619 boot_unattempted_labels(ctx, cfg);
1620}
1621
Simon Glasse719fe02021-10-14 12:48:04 -06001622int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1623 pxe_getfile_func getfile, void *userdata,
Sean Edmondba802862023-04-11 10:48:47 -07001624 bool allow_abs_path, const char *bootfile, bool use_ipv6)
Simon Glassb0d08db2021-10-14 12:47:56 -06001625{
Simon Glasse719fe02021-10-14 12:48:04 -06001626 const char *last_slash;
1627 size_t path_len = 0;
1628
1629 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001630 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001631 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001632 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001633 ctx->allow_abs_path = allow_abs_path;
Sean Edmondba802862023-04-11 10:48:47 -07001634 ctx->use_ipv6 = use_ipv6;
Simon Glasse719fe02021-10-14 12:48:04 -06001635
1636 /* figure out the boot directory, if there is one */
1637 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1638 return -ENOSPC;
1639 ctx->bootdir = strdup(bootfile ? bootfile : "");
1640 if (!ctx->bootdir)
1641 return -ENOMEM;
1642
1643 if (bootfile) {
1644 last_slash = strrchr(bootfile, '/');
1645 if (last_slash)
1646 path_len = (last_slash - bootfile) + 1;
1647 }
1648 ctx->bootdir[path_len] = '\0';
1649
1650 return 0;
1651}
1652
1653void pxe_destroy_ctx(struct pxe_context *ctx)
1654{
1655 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001656}
Simon Glass791bbfe2021-10-14 12:48:03 -06001657
1658int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1659{
1660 struct pxe_menu *cfg;
1661
1662 cfg = parse_pxefile(ctx, pxefile_addr_r);
1663 if (!cfg) {
1664 printf("Error parsing config file\n");
1665 return 1;
1666 }
1667
1668 if (prompt)
1669 cfg->prompt = 1;
1670
1671 handle_pxe_menu(ctx, cfg);
1672
1673 destroy_pxe_menu(cfg);
1674
1675 return 0;
1676}