blob: fd1a09225e1f3838b026a21e469bc35496a52fb8 [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
Simon Glass9b2c78c2024-06-19 06:34:50 -06007#define LOG_CATEGORY LOGC_BOOT
8
Simon Glassa686ba62024-11-15 16:19:19 -07009#include <bootflow.h>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010011#include <dm.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010012#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070013#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010015#include <malloc.h>
16#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <net.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010018#include <fdt_support.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010019#include <video.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010020#include <linux/libfdt.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010021#include <linux/string.h>
22#include <linux/ctype.h>
23#include <errno.h>
24#include <linux/list.h>
25
Zhang Ning9c1d9c52022-02-01 08:33:37 +080026#include <rng.h>
Zhang Ning9c1d9c52022-02-01 08:33:37 +080027
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 Glassa686ba62024-11-15 16:19:19 -0700101 unsigned long file_addr, enum bootflow_img_t type,
102 ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100103{
104 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100105 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100106 char addr_buf[18];
Simon Glassa9401b92021-10-14 12:48:08 -0600107 ulong size;
108 int ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100109
Simon Glass5e3e39a2021-10-14 12:48:05 -0600110 if (file_path[0] == '/' && ctx->allow_abs_path)
111 *relfile = '\0';
112 else
113 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
Patrice Chotard17e88042019-11-25 09:07:37 +0100114
Simon Glass5e3e39a2021-10-14 12:48:05 -0600115 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard17e88042019-11-25 09:07:37 +0100116
117 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100118 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100119
120 return -ENAMETOOLONG;
121 }
122
123 strcat(relfile, file_path);
124
125 printf("Retrieving file: %s\n", relfile);
126
127 sprintf(addr_buf, "%lx", file_addr);
128
Simon Glassa686ba62024-11-15 16:19:19 -0700129 ret = ctx->getfile(ctx, relfile, addr_buf, type, &size);
Simon Glassa9401b92021-10-14 12:48:08 -0600130 if (ret < 0)
131 return log_msg_ret("get", ret);
132 if (filesizep)
133 *filesizep = size;
134
135 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100136}
137
Simon Glassb0d08db2021-10-14 12:47:56 -0600138int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600139 ulong file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100140{
Simon Glassa9401b92021-10-14 12:48:08 -0600141 ulong size;
Patrice Chotard17e88042019-11-25 09:07:37 +0100142 int err;
143 char *buf;
144
Simon Glassa686ba62024-11-15 16:19:19 -0700145 err = get_relfile(ctx, file_path, file_addr, BFI_EXTLINUX_CFG,
146 &size);
Patrice Chotard17e88042019-11-25 09:07:37 +0100147 if (err < 0)
148 return err;
149
Simon Glassa9401b92021-10-14 12:48:08 -0600150 buf = map_sysmem(file_addr + size, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +0100151 *buf = '\0';
152 unmap_sysmem(buf);
153
154 return 1;
155}
156
157#define PXELINUX_DIR "pxelinux.cfg/"
158
Simon Glass00302442021-10-14 12:48:01 -0600159/**
160 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
161 *
162 * @ctx: PXE context
163 * @file: Filename to process (relative to pxelinux.cfg/)
164 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
165 * or other value < 0 on other error
166 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600167int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100168 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100169{
170 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100171 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100172
173 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
174 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100175 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100176 return -ENAMETOOLONG;
177 }
178
179 sprintf(path, PXELINUX_DIR "%s", file);
180
Simon Glassb0d08db2021-10-14 12:47:56 -0600181 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100182}
183
Simon Glass00302442021-10-14 12:48:01 -0600184/**
185 * get_relfile_envaddr() - read a file to an address in an env var
186 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100187 * Wrapper to make it easier to store the file at file_path in the location
188 * specified by envaddr_name. file_path will be joined to the bootfile path,
189 * if any is specified.
190 *
Simon Glass00302442021-10-14 12:48:01 -0600191 * @ctx: PXE context
192 * @file_path: File path to read (relative to the PXE file)
193 * @envaddr_name: Name of environment variable which contains the address to
194 * load to
Simon Glassa686ba62024-11-15 16:19:19 -0700195 * @type: File type
Simon Glassa9401b92021-10-14 12:48:08 -0600196 * @filesizep: Returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -0600197 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
198 * environment variable, -EINVAL if its format is not valid hex, or other
199 * value < 0 on other error
Patrice Chotard17e88042019-11-25 09:07:37 +0100200 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600201static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Simon Glassa686ba62024-11-15 16:19:19 -0700202 const char *envaddr_name,
203 enum bootflow_img_t type, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100204{
205 unsigned long file_addr;
206 char *envaddr;
207
208 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100209 if (!envaddr)
210 return -ENOENT;
211
212 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
213 return -EINVAL;
214
Simon Glassa686ba62024-11-15 16:19:19 -0700215 return get_relfile(ctx, file_path, file_addr, type, filesizep);
Patrice Chotard17e88042019-11-25 09:07:37 +0100216}
217
Simon Glass00302442021-10-14 12:48:01 -0600218/**
219 * label_create() - crate a new PXE label
220 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100221 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
222 * result must be free()'d to reclaim the memory.
223 *
Simon Glass00302442021-10-14 12:48:01 -0600224 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100225 */
226static struct pxe_label *label_create(void)
227{
228 struct pxe_label *label;
229
230 label = malloc(sizeof(struct pxe_label));
Patrice Chotard17e88042019-11-25 09:07:37 +0100231 if (!label)
232 return NULL;
233
234 memset(label, 0, sizeof(struct pxe_label));
235
236 return label;
237}
238
Simon Glass00302442021-10-14 12:48:01 -0600239/**
240 * label_destroy() - free the memory used by a pxe_label
241 *
242 * This frees @label itself as well as memory used by its name,
243 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
244 * they're non-NULL.
Patrice Chotard17e88042019-11-25 09:07:37 +0100245 *
246 * So - be sure to only use dynamically allocated memory for the members of
247 * the pxe_label struct, unless you want to clean it up first. These are
248 * currently only created by the pxe file parsing code.
Simon Glass00302442021-10-14 12:48:01 -0600249 *
250 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100251 */
252static void label_destroy(struct pxe_label *label)
253{
Simon Glass764d0c02021-10-14 12:48:02 -0600254 free(label->name);
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +0200255 free(label->kernel_label);
Simon Glass764d0c02021-10-14 12:48:02 -0600256 free(label->kernel);
257 free(label->config);
258 free(label->append);
259 free(label->initrd);
260 free(label->fdt);
261 free(label->fdtdir);
262 free(label->fdtoverlays);
Patrice Chotard17e88042019-11-25 09:07:37 +0100263 free(label);
264}
265
Simon Glass00302442021-10-14 12:48:01 -0600266/**
267 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100268 *
269 * This is passed as a callback to the menu code for displaying each
270 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600271 *
272 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-11-25 09:07:37 +0100273 */
274static void label_print(void *data)
275{
276 struct pxe_label *label = data;
277 const char *c = label->menu ? label->menu : label->name;
278
279 printf("%s:\t%s\n", label->num, c);
280}
281
Simon Glass00302442021-10-14 12:48:01 -0600282/**
283 * label_localboot() - Boot a label that specified 'localboot'
284 *
285 * This requires that the 'localcmd' environment variable is defined. Its
286 * contents will be executed as U-Boot commands. If the label specified an
287 * 'append' line, its contents will be used to overwrite the contents of the
288 * 'bootargs' environment variable prior to running 'localcmd'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100289 *
Simon Glass00302442021-10-14 12:48:01 -0600290 * @label: Label to process
291 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100292 */
293static int label_localboot(struct pxe_label *label)
294{
295 char *localcmd;
296
297 localcmd = from_env("localcmd");
Patrice Chotard17e88042019-11-25 09:07:37 +0100298 if (!localcmd)
299 return -ENOENT;
300
301 if (label->append) {
302 char bootargs[CONFIG_SYS_CBSIZE];
303
Simon Glassc7b03e82020-11-05 10:33:47 -0700304 cli_simple_process_macros(label->append, bootargs,
305 sizeof(bootargs));
Patrice Chotard17e88042019-11-25 09:07:37 +0100306 env_set("bootargs", bootargs);
307 }
308
309 debug("running: %s\n", localcmd);
310
311 return run_command_list(localcmd, strlen(localcmd), 0);
312}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100313
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800314/*
315 * label_boot_kaslrseed generate kaslrseed from hw rng
316 */
317
318static void label_boot_kaslrseed(void)
319{
Marek Vasut0d871e72024-04-26 01:02:07 +0200320#if CONFIG_IS_ENABLED(DM_RNG)
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800321 ulong fdt_addr;
322 struct fdt_header *working_fdt;
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800323 int err;
324
325 /* Get the main fdt and map it */
326 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
327 working_fdt = map_sysmem(fdt_addr, 0);
328 err = fdt_check_header(working_fdt);
329 if (err)
330 return;
331
332 /* add extra size for holding kaslr-seed */
333 /* err is new fdt size, 0 or negtive */
334 err = fdt_shrink_to_minimum(working_fdt, 512);
335 if (err <= 0)
336 return;
337
Tim Harveyc75fab42024-06-18 14:06:08 -0700338 fdt_kaslrseed(working_fdt, true);
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800339#endif
340 return;
341}
342
Simon Glass00302442021-10-14 12:48:01 -0600343/**
344 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200345 * or 'devicetree-overlay'
Simon Glass00302442021-10-14 12:48:01 -0600346 *
347 * @ctx: PXE context
348 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100349 */
350#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600351static void label_boot_fdtoverlay(struct pxe_context *ctx,
352 struct pxe_label *label)
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100353{
354 char *fdtoverlay = label->fdtoverlays;
355 struct fdt_header *working_fdt;
356 char *fdtoverlay_addr_env;
357 ulong fdtoverlay_addr;
358 ulong fdt_addr;
359 int err;
360
361 /* Get the main fdt and map it */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600362 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100363 working_fdt = map_sysmem(fdt_addr, 0);
364 err = fdt_check_header(working_fdt);
365 if (err)
366 return;
367
368 /* Get the specific overlay loading address */
369 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
370 if (!fdtoverlay_addr_env) {
371 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
372 return;
373 }
374
Simon Glass3ff49ec2021-07-24 09:03:29 -0600375 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100376
377 /* Cycle over the overlay files and apply them in order */
378 do {
379 struct fdt_header *blob;
380 char *overlayfile;
381 char *end;
382 int len;
383
384 /* Drop leading spaces */
385 while (*fdtoverlay == ' ')
386 ++fdtoverlay;
387
388 /* Copy a single filename if multiple provided */
389 end = strstr(fdtoverlay, " ");
390 if (end) {
391 len = (int)(end - fdtoverlay);
392 overlayfile = malloc(len + 1);
393 strncpy(overlayfile, fdtoverlay, len);
394 overlayfile[len] = '\0';
395 } else
396 overlayfile = fdtoverlay;
397
398 if (!strlen(overlayfile))
399 goto skip_overlay;
400
401 /* Load overlay file */
Simon Glassa9401b92021-10-14 12:48:08 -0600402 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
Simon Glassa686ba62024-11-15 16:19:19 -0700403 (enum bootflow_img_t)IH_TYPE_FLATDT,
Simon Glassa9401b92021-10-14 12:48:08 -0600404 NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100405 if (err < 0) {
406 printf("Failed loading overlay %s\n", overlayfile);
407 goto skip_overlay;
408 }
409
410 /* Resize main fdt */
411 fdt_shrink_to_minimum(working_fdt, 8192);
412
413 blob = map_sysmem(fdtoverlay_addr, 0);
414 err = fdt_check_header(blob);
415 if (err) {
416 printf("Invalid overlay %s, skipping\n",
417 overlayfile);
418 goto skip_overlay;
419 }
420
421 err = fdt_overlay_apply_verbose(working_fdt, blob);
422 if (err) {
423 printf("Failed to apply overlay %s, skipping\n",
424 overlayfile);
425 goto skip_overlay;
426 }
427
428skip_overlay:
429 if (end)
430 free(overlayfile);
431 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
432}
433#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100434
Simon Glass00302442021-10-14 12:48:01 -0600435/**
Simon Glass827cd9c2025-03-05 17:25:04 -0700436 * label_run_boot() - Set up the FDT and call the appropriate bootm/z/i command
Simon Glass00302442021-10-14 12:48:01 -0600437 *
438 * @ctx: PXE context
439 * @label: Label to process
Simon Glass827cd9c2025-03-05 17:25:04 -0700440 * @kernel_addr: String containing kernel address (cannot be NULL)
441 * @initrd_addr_str: String containing initrd address (NULL if none)
442 * @initrd_filesize: String containing initrd size (only used if
443 * @initrd_addr_str)
444 * @initrd_str: initrd string to process (only used if @initrd_addr_str)
445 * Return: does not return on success, or returns 0 if the boot command
446 * returned, or -ve error value on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100447 */
Simon Glass827cd9c2025-03-05 17:25:04 -0700448static int label_run_boot(struct pxe_context *ctx, struct pxe_label *label,
449 char *kernel_addr, char *initrd_addr_str,
450 char *initrd_filesize, char *initrd_str)
Patrice Chotard17e88042019-11-25 09:07:37 +0100451{
Tom Rini793921e2024-04-18 08:29:35 -0600452 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
453 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Simon Glass827cd9c2025-03-05 17:25:04 -0700454 ulong kernel_addr_r;
Tom Rini793921e2024-04-18 08:29:35 -0600455 int bootm_argc = 2;
456 int zboot_argc = 3;
Tom Rini793921e2024-04-18 08:29:35 -0600457 void *buf;
Patrice Chotard17e88042019-11-25 09:07:37 +0100458
Tom Rini793921e2024-04-18 08:29:35 -0600459 /*
460 * fdt usage is optional:
461 * It handles the following scenarios.
462 *
463 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
464 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
465 * bootm, and adjust argc appropriately.
466 *
467 * If retrieve fails and no exact fdt blob is specified in pxe file with
468 * "fdt" label, try Scenario 2.
469 *
470 * Scenario 2: If there is an fdt_addr specified, pass it along to
471 * bootm, and adjust argc appropriately.
472 *
473 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
474 * bootm, and adjust argc appropriately, unless the image type is fitImage.
475 *
476 * Scenario 4: fdt blob is not available.
477 */
478 bootm_argv[3] = env_get("fdt_addr_r");
479
480 /* For FIT, the label can be identical to kernel one */
481 if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
482 bootm_argv[3] = kernel_addr;
483 /* if fdt label is defined then get fdt from server */
484 } else if (bootm_argv[3]) {
485 char *fdtfile = NULL;
486 char *fdtfilefree = NULL;
487
488 if (label->fdt) {
489 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
490 if (strcmp("-", label->fdt))
491 fdtfile = label->fdt;
492 } else {
493 fdtfile = label->fdt;
494 }
495 } else if (label->fdtdir) {
496 char *f1, *f2, *f3, *f4, *slash;
Simon Glass827cd9c2025-03-05 17:25:04 -0700497 int len;
Tom Rini793921e2024-04-18 08:29:35 -0600498
499 f1 = env_get("fdtfile");
500 if (f1) {
501 f2 = "";
502 f3 = "";
503 f4 = "";
504 } else {
505 /*
506 * For complex cases where this code doesn't
507 * generate the correct filename, the board
508 * code should set $fdtfile during early boot,
509 * or the boot scripts should set $fdtfile
510 * before invoking "pxe" or "sysboot".
511 */
512 f1 = env_get("soc");
513 f2 = "-";
514 f3 = env_get("board");
515 f4 = ".dtb";
516 if (!f1) {
517 f1 = "";
518 f2 = "";
519 }
520 if (!f3) {
521 f2 = "";
522 f3 = "";
523 }
524 }
525
526 len = strlen(label->fdtdir);
527 if (!len)
528 slash = "./";
529 else if (label->fdtdir[len - 1] != '/')
530 slash = "/";
531 else
532 slash = "";
533
534 len = strlen(label->fdtdir) + strlen(slash) +
535 strlen(f1) + strlen(f2) + strlen(f3) +
536 strlen(f4) + 1;
537 fdtfilefree = malloc(len);
538 if (!fdtfilefree) {
539 printf("malloc fail (FDT filename)\n");
Simon Glass827cd9c2025-03-05 17:25:04 -0700540 return -ENOMEM;
Tom Rini793921e2024-04-18 08:29:35 -0600541 }
542
543 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
544 label->fdtdir, slash, f1, f2, f3, f4);
545 fdtfile = fdtfilefree;
546 }
547
548 if (fdtfile) {
549 int err = get_relfile_envaddr(ctx, fdtfile,
Simon Glassa686ba62024-11-15 16:19:19 -0700550 "fdt_addr_r",
551 (enum bootflow_img_t)IH_TYPE_FLATDT, NULL);
Tom Rini793921e2024-04-18 08:29:35 -0600552
553 free(fdtfilefree);
554 if (err < 0) {
555 bootm_argv[3] = NULL;
556
557 if (label->fdt) {
558 printf("Skipping %s for failure retrieving FDT\n",
559 label->name);
Simon Glass827cd9c2025-03-05 17:25:04 -0700560 return -ENOENT;
Tom Rini793921e2024-04-18 08:29:35 -0600561 }
562
563 if (label->fdtdir) {
564 printf("Skipping fdtdir %s for failure retrieving dts\n",
565 label->fdtdir);
566 }
567 }
568
569 if (label->kaslrseed)
570 label_boot_kaslrseed();
571
572#ifdef CONFIG_OF_LIBFDT_OVERLAY
573 if (label->fdtoverlays)
574 label_boot_fdtoverlay(ctx, label);
575#endif
576 } else {
577 bootm_argv[3] = NULL;
578 }
579 }
580
581 bootm_argv[1] = kernel_addr;
582 zboot_argv[1] = kernel_addr;
583
584 if (initrd_addr_str) {
585 bootm_argv[2] = initrd_str;
586 bootm_argc = 3;
587
588 zboot_argv[3] = initrd_addr_str;
589 zboot_argv[4] = initrd_filesize;
590 zboot_argc = 5;
591 }
592
593 if (!bootm_argv[3]) {
594 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
595 if (strcmp("-", label->fdt))
596 bootm_argv[3] = env_get("fdt_addr");
597 } else {
598 bootm_argv[3] = env_get("fdt_addr");
599 }
600 }
601
602 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
603 buf = map_sysmem(kernel_addr_r, 0);
604
605 if (!bootm_argv[3] && genimg_get_format(buf) != IMAGE_FORMAT_FIT) {
606 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
607 if (strcmp("-", label->fdt))
608 bootm_argv[3] = env_get("fdtcontroladdr");
609 } else {
610 bootm_argv[3] = env_get("fdtcontroladdr");
611 }
612 }
613
614 if (bootm_argv[3]) {
615 if (!bootm_argv[2])
616 bootm_argv[2] = "-";
617 bootm_argc = 4;
618 }
619
620 /* Try bootm for legacy and FIT format image */
621 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
Simon Glass9b2c78c2024-06-19 06:34:50 -0600622 IS_ENABLED(CONFIG_CMD_BOOTM)) {
623 log_debug("using bootm\n");
Tom Rini793921e2024-04-18 08:29:35 -0600624 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
625 /* Try booting an AArch64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600626 } else if (IS_ENABLED(CONFIG_CMD_BOOTI)) {
627 log_debug("using booti\n");
Tom Rini793921e2024-04-18 08:29:35 -0600628 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
629 /* Try booting a Image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600630 } else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) {
631 log_debug("using bootz\n");
Tom Rini793921e2024-04-18 08:29:35 -0600632 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
633 /* Try booting an x86_64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600634 } else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) {
635 log_debug("using zboot\n");
Tom Rini793921e2024-04-18 08:29:35 -0600636 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Simon Glass9b2c78c2024-06-19 06:34:50 -0600637 }
Tom Rini793921e2024-04-18 08:29:35 -0600638
639 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +0100640
Simon Glass827cd9c2025-03-05 17:25:04 -0700641 return 0;
642}
643
644/**
645 * label_boot() - Boot according to the contents of a pxe_label
646 *
647 * If we can't boot for any reason, we return. A successful boot never
648 * returns.
649 *
650 * The kernel will be stored in the location given by the 'kernel_addr_r'
651 * environment variable.
652 *
653 * If the label specifies an initrd file, it will be stored in the location
654 * given by the 'ramdisk_addr_r' environment variable.
655 *
656 * If the label specifies an 'append' line, its contents will overwrite that
657 * of the 'bootargs' environment variable.
658 *
659 * @ctx: PXE context
660 * @label: Label to process
661 * Returns does not return on success, otherwise returns 0 if a localboot
662 * label was processed, or 1 on error
663 */
664static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
665{
666 char *kernel_addr = NULL;
667 char *initrd_addr_str = NULL;
668 char initrd_filesize[10];
669 char initrd_str[28];
670 char mac_str[29] = "";
671 char ip_str[68] = "";
672 char *fit_addr = NULL;
673
674 label_print(label);
675
676 label->attempted = 1;
677
678 if (label->localboot) {
679 if (label->localboot_val >= 0)
680 label_localboot(label);
681 return 0;
682 }
683
684 if (!label->kernel) {
685 printf("No kernel given, skipping %s\n",
686 label->name);
687 return 1;
688 }
689
690 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
691 (enum bootflow_img_t)IH_TYPE_KERNEL, NULL)
692 < 0) {
693 printf("Skipping %s for failure retrieving kernel\n",
694 label->name);
695 return 1;
696 }
697
698 kernel_addr = env_get("kernel_addr_r");
699 /* for FIT, append the configuration identifier */
700 if (label->config) {
701 int len = strlen(kernel_addr) + strlen(label->config) + 1;
702
703 fit_addr = malloc(len);
704 if (!fit_addr) {
705 printf("malloc fail (FIT address)\n");
706 return 1;
707 }
708 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
709 kernel_addr = fit_addr;
710 }
711
712 /* For FIT, the label can be identical to kernel one */
713 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
714 initrd_addr_str = kernel_addr;
715 } else if (label->initrd) {
716 ulong size;
717 int ret;
718
719 ret = get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
720 (enum bootflow_img_t)IH_TYPE_RAMDISK,
721 &size);
722 if (ret < 0) {
723 printf("Skipping %s for failure retrieving initrd\n",
724 label->name);
725 goto cleanup;
726 }
727 strcpy(initrd_filesize, simple_xtoa(size));
728 initrd_addr_str = env_get("ramdisk_addr_r");
729 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
730 initrd_addr_str, size);
731 if (size >= sizeof(initrd_str))
732 goto cleanup;
733 }
734
735 if (label->ipappend & 0x1) {
736 sprintf(ip_str, " ip=%s:%s:%s:%s",
737 env_get("ipaddr"), env_get("serverip"),
738 env_get("gatewayip"), env_get("netmask"));
739 }
740
741 if (IS_ENABLED(CONFIG_CMD_NET)) {
742 if (label->ipappend & 0x2) {
743 int err;
744
745 strcpy(mac_str, " BOOTIF=");
746 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
747 if (err < 0)
748 mac_str[0] = '\0';
749 }
750 }
751
752 if ((label->ipappend & 0x3) || label->append) {
753 char bootargs[CONFIG_SYS_CBSIZE] = "";
754 char finalbootargs[CONFIG_SYS_CBSIZE];
755
756 if (strlen(label->append ?: "") +
757 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
758 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
759 strlen(label->append ?: ""),
760 strlen(ip_str), strlen(mac_str),
761 sizeof(bootargs));
762 goto cleanup;
763 }
764
765 if (label->append)
766 strlcpy(bootargs, label->append, sizeof(bootargs));
767
768 strcat(bootargs, ip_str);
769 strcat(bootargs, mac_str);
770
771 cli_simple_process_macros(bootargs, finalbootargs,
772 sizeof(finalbootargs));
773 env_set("bootargs", finalbootargs);
774 printf("append: %s\n", finalbootargs);
775 }
776
777 label_run_boot(ctx, label, kernel_addr, initrd_addr_str,
778 initrd_filesize, initrd_str);
779 /* ignore the error value since we are going to fail anyway */
780
Patrice Chotard17e88042019-11-25 09:07:37 +0100781cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600782 free(fit_addr);
783
Simon Glass827cd9c2025-03-05 17:25:04 -0700784 return 1; /* returning is always failure */
Patrice Chotard17e88042019-11-25 09:07:37 +0100785}
786
Simon Glass00302442021-10-14 12:48:01 -0600787/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100788enum token_type {
789 T_EOL,
790 T_STRING,
791 T_EOF,
792 T_MENU,
793 T_TITLE,
794 T_TIMEOUT,
795 T_LABEL,
796 T_KERNEL,
797 T_LINUX,
798 T_APPEND,
799 T_INITRD,
800 T_LOCALBOOT,
801 T_DEFAULT,
802 T_PROMPT,
803 T_INCLUDE,
804 T_FDT,
805 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100806 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100807 T_ONTIMEOUT,
808 T_IPAPPEND,
809 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800810 T_KASLRSEED,
Martyn Welch53dd5102024-10-09 14:15:38 +0100811 T_FALLBACK,
Patrice Chotard17e88042019-11-25 09:07:37 +0100812 T_INVALID
813};
814
Simon Glass00302442021-10-14 12:48:01 -0600815/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100816struct token {
817 char *val;
818 enum token_type type;
819};
820
Simon Glass00302442021-10-14 12:48:01 -0600821/* Keywords recognized */
Patrice Chotard17e88042019-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 Armstrongc77a1a32021-01-20 09:54:53 +0100839 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200840 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100841 {"ontimeout", T_ONTIMEOUT,},
842 {"ipappend", T_IPAPPEND,},
843 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800844 {"kaslrseed", T_KASLRSEED,},
Martyn Welch53dd5102024-10-09 14:15:38 +0100845 {"fallback", T_FALLBACK,},
Patrice Chotard17e88042019-11-25 09:07:37 +0100846 {NULL, T_INVALID}
847};
848
Simon Glass00302442021-10-14 12:48:01 -0600849/**
850 * enum lex_state - lexer state
851 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100852 * Since pxe(linux) files don't have a token to identify the start of a
853 * literal, we have to keep track of when we're in a state where a literal is
854 * expected vs when we're in a state a keyword is expected.
855 */
856enum lex_state {
857 L_NORMAL = 0,
858 L_KEYWORD,
859 L_SLITERAL
860};
861
Simon Glass00302442021-10-14 12:48:01 -0600862/**
863 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100864 *
Simon Glass00302442021-10-14 12:48:01 -0600865 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100866 *
867 * Characters from *p are copied into t-val until a character equal to
868 * delim is found, or a NUL byte is reached. If delim has the special value of
869 * ' ', any whitespace character will be used as a delimiter.
870 *
871 * If lower is unequal to 0, uppercase characters will be converted to
872 * lowercase in the result. This is useful to make keywords case
873 * insensitive.
874 *
875 * The location of *p is updated to point to the first character after the end
876 * of the token - the ending delimiter.
877 *
Simon Glass00302442021-10-14 12:48:01 -0600878 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
879 * it.
880 *
881 * @p: Points to a pointer to the current position in the input being processed.
882 * Updated to point at the first character after the current token
883 * @t: Pointers to a token to fill in
884 * @delim: Delimiter character to look for, either newline or space
885 * @lower: true to convert the string to lower case when storing
886 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100887 */
888static char *get_string(char **p, struct token *t, char delim, int lower)
889{
890 char *b, *e;
891 size_t len, i;
892
893 /*
894 * b and e both start at the beginning of the input stream.
895 *
896 * e is incremented until we find the ending delimiter, or a NUL byte
897 * is reached. Then, we take e - b to find the length of the token.
898 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100899 b = *p;
900 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100901 while (*e) {
902 if ((delim == ' ' && isspace(*e)) || delim == *e)
903 break;
904 e++;
905 }
906
907 len = e - b;
908
909 /*
910 * Allocate memory to hold the string, and copy it in, converting
911 * characters to lowercase if lower is != 0.
912 */
913 t->val = malloc(len + 1);
914 if (!t->val)
915 return NULL;
916
917 for (i = 0; i < len; i++, b++) {
918 if (lower)
919 t->val[i] = tolower(*b);
920 else
921 t->val[i] = *b;
922 }
923
924 t->val[len] = '\0';
925
Simon Glass764d0c02021-10-14 12:48:02 -0600926 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100927 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100928 t->type = T_STRING;
929
930 return t->val;
931}
932
Simon Glass00302442021-10-14 12:48:01 -0600933/**
934 * get_keyword() - Populate a keyword token with a type and value
935 *
936 * Updates the ->type field based on the keyword string in @val
937 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100938 */
939static void get_keyword(struct token *t)
940{
941 int i;
942
943 for (i = 0; keywords[i].val; i++) {
944 if (!strcmp(t->val, keywords[i].val)) {
945 t->type = keywords[i].type;
946 break;
947 }
948 }
949}
950
Simon Glass00302442021-10-14 12:48:01 -0600951/**
952 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100953 *
Simon Glass00302442021-10-14 12:48:01 -0600954 * We have to keep track of which state we're in to know if we're looking to get
955 * a string literal or a keyword.
956 *
957 * @p: Points to a pointer to the current position in the input being processed.
958 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100959 */
960static void get_token(char **p, struct token *t, enum lex_state state)
961{
962 char *c = *p;
963
964 t->type = T_INVALID;
965
966 /* eat non EOL whitespace */
967 while (isblank(*c))
968 c++;
969
970 /*
971 * eat comments. note that string literals can't begin with #, but
972 * can contain a # after their first character.
973 */
974 if (*c == '#') {
975 while (*c && *c != '\n')
976 c++;
977 }
978
979 if (*c == '\n') {
980 t->type = T_EOL;
981 c++;
982 } else if (*c == '\0') {
983 t->type = T_EOF;
984 c++;
985 } else if (state == L_SLITERAL) {
986 get_string(&c, t, '\n', 0);
987 } else if (state == L_KEYWORD) {
988 /*
989 * when we expect a keyword, we first get the next string
990 * token delimited by whitespace, and then check if it
991 * matches a keyword in our keyword list. if it does, it's
992 * converted to a keyword token of the appropriate type, and
993 * if not, it remains a string token.
994 */
995 get_string(&c, t, ' ', 1);
996 get_keyword(t);
997 }
998
999 *p = c;
1000}
1001
Simon Glass00302442021-10-14 12:48:01 -06001002/**
1003 * eol_or_eof() - Find end of line
1004 *
1005 * Increment *c until we get to the end of the current line, or EOF
1006 *
1007 * @c: Points to a pointer to the current position in the input being processed.
1008 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +01001009 */
1010static void eol_or_eof(char **c)
1011{
1012 while (**c && **c != '\n')
1013 (*c)++;
1014}
1015
1016/*
1017 * All of these parse_* functions share some common behavior.
1018 *
1019 * They finish with *c pointing after the token they parse, and return 1 on
1020 * success, or < 0 on error.
1021 */
1022
1023/*
1024 * Parse a string literal and store a pointer it at *dst. String literals
1025 * terminate at the end of the line.
1026 */
1027static int parse_sliteral(char **c, char **dst)
1028{
1029 struct token t;
1030 char *s = *c;
1031
1032 get_token(c, &t, L_SLITERAL);
1033
1034 if (t.type != T_STRING) {
1035 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1036 return -EINVAL;
1037 }
1038
1039 *dst = t.val;
1040
1041 return 1;
1042}
1043
1044/*
1045 * Parse a base 10 (unsigned) integer and store it at *dst.
1046 */
1047static int parse_integer(char **c, int *dst)
1048{
1049 struct token t;
1050 char *s = *c;
1051
1052 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001053 if (t.type != T_STRING) {
1054 printf("Expected string: %.*s\n", (int)(*c - s), s);
1055 return -EINVAL;
1056 }
1057
1058 *dst = simple_strtol(t.val, NULL, 10);
1059
1060 free(t.val);
1061
1062 return 1;
1063}
1064
Simon Glassb0d08db2021-10-14 12:47:56 -06001065static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001066 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001067
1068/*
1069 * Parse an include statement, and retrieve and parse the file it mentions.
1070 *
1071 * base should point to a location where it's safe to store the file, and
1072 * nest_level should indicate how many nested includes have occurred. For this
1073 * include, nest_level has already been incremented and doesn't need to be
1074 * incremented here.
1075 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001076static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001077 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001078{
1079 char *include_path;
1080 char *s = *c;
1081 int err;
1082 char *buf;
1083 int ret;
1084
1085 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001086 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001087 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001088 return err;
1089 }
1090
Simon Glassb0d08db2021-10-14 12:47:56 -06001091 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001092 if (err < 0) {
1093 printf("Couldn't retrieve %s\n", include_path);
1094 return err;
1095 }
1096
1097 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001098 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001099 unmap_sysmem(buf);
1100
1101 return ret;
1102}
1103
1104/*
1105 * Parse lines that begin with 'menu'.
1106 *
1107 * base and nest are provided to handle the 'menu include' case.
1108 *
1109 * base should point to a location where it's safe to store the included file.
1110 *
1111 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1112 * a file it includes, 3 when parsing a file included by that file, and so on.
1113 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001114static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001115 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001116{
1117 struct token t;
1118 char *s = *c;
1119 int err = 0;
1120
1121 get_token(c, &t, L_KEYWORD);
1122
1123 switch (t.type) {
1124 case T_TITLE:
1125 err = parse_sliteral(c, &cfg->title);
1126
1127 break;
1128
1129 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001130 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001131 break;
1132
1133 case T_BACKGROUND:
1134 err = parse_sliteral(c, &cfg->bmp);
1135 break;
1136
1137 default:
1138 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001139 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001140 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001141 if (err < 0)
1142 return err;
1143
1144 eol_or_eof(c);
1145
1146 return 1;
1147}
1148
1149/*
1150 * Handles parsing a 'menu line' when we're parsing a label.
1151 */
1152static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001153 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001154{
1155 struct token t;
1156 char *s;
1157
1158 s = *c;
1159
1160 get_token(c, &t, L_KEYWORD);
1161
1162 switch (t.type) {
1163 case T_DEFAULT:
1164 if (!cfg->default_label)
1165 cfg->default_label = strdup(label->name);
1166
1167 if (!cfg->default_label)
1168 return -ENOMEM;
1169
1170 break;
1171 case T_LABEL:
1172 parse_sliteral(c, &label->menu);
1173 break;
1174 default:
1175 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001176 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001177 }
1178
1179 eol_or_eof(c);
1180
1181 return 0;
1182}
1183
1184/*
1185 * Handles parsing a 'kernel' label.
1186 * expecting "filename" or "<fit_filename>#cfg"
1187 */
1188static int parse_label_kernel(char **c, struct pxe_label *label)
1189{
1190 char *s;
1191 int err;
1192
1193 err = parse_sliteral(c, &label->kernel);
1194 if (err < 0)
1195 return err;
1196
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +02001197 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1198 label->kernel_label = strdup(label->kernel);
1199 if (!label->kernel_label)
1200 return -ENOMEM;
1201
Patrice Chotard17e88042019-11-25 09:07:37 +01001202 s = strstr(label->kernel, "#");
1203 if (!s)
1204 return 1;
1205
Patrick Delaunay9d464f82022-10-28 11:01:20 +02001206 label->config = strdup(s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001207 if (!label->config)
1208 return -ENOMEM;
1209
Patrice Chotard17e88042019-11-25 09:07:37 +01001210 *s = 0;
1211
1212 return 1;
1213}
1214
1215/*
1216 * Parses a label and adds it to the list of labels for a menu.
1217 *
1218 * A label ends when we either get to the end of a file, or
1219 * get some input we otherwise don't have a handler defined
1220 * for.
1221 *
1222 */
1223static int parse_label(char **c, struct pxe_menu *cfg)
1224{
1225 struct token t;
1226 int len;
1227 char *s = *c;
1228 struct pxe_label *label;
1229 int err;
1230
1231 label = label_create();
1232 if (!label)
1233 return -ENOMEM;
1234
1235 err = parse_sliteral(c, &label->name);
1236 if (err < 0) {
1237 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1238 label_destroy(label);
1239 return -EINVAL;
1240 }
1241
1242 list_add_tail(&label->list, &cfg->labels);
1243
1244 while (1) {
1245 s = *c;
1246 get_token(c, &t, L_KEYWORD);
1247
1248 err = 0;
1249 switch (t.type) {
1250 case T_MENU:
1251 err = parse_label_menu(c, cfg, label);
1252 break;
1253
1254 case T_KERNEL:
1255 case T_LINUX:
1256 err = parse_label_kernel(c, label);
1257 break;
1258
1259 case T_APPEND:
1260 err = parse_sliteral(c, &label->append);
1261 if (label->initrd)
1262 break;
1263 s = strstr(label->append, "initrd=");
1264 if (!s)
1265 break;
1266 s += 7;
1267 len = (int)(strchr(s, ' ') - s);
1268 label->initrd = malloc(len + 1);
1269 strncpy(label->initrd, s, len);
1270 label->initrd[len] = '\0';
1271
1272 break;
1273
1274 case T_INITRD:
1275 if (!label->initrd)
1276 err = parse_sliteral(c, &label->initrd);
1277 break;
1278
1279 case T_FDT:
1280 if (!label->fdt)
1281 err = parse_sliteral(c, &label->fdt);
1282 break;
1283
1284 case T_FDTDIR:
1285 if (!label->fdtdir)
1286 err = parse_sliteral(c, &label->fdtdir);
1287 break;
1288
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001289 case T_FDTOVERLAYS:
1290 if (!label->fdtoverlays)
1291 err = parse_sliteral(c, &label->fdtoverlays);
1292 break;
1293
Patrice Chotard17e88042019-11-25 09:07:37 +01001294 case T_LOCALBOOT:
1295 label->localboot = 1;
1296 err = parse_integer(c, &label->localboot_val);
1297 break;
1298
1299 case T_IPAPPEND:
1300 err = parse_integer(c, &label->ipappend);
1301 break;
1302
Zhang Ning9c1d9c52022-02-01 08:33:37 +08001303 case T_KASLRSEED:
1304 label->kaslrseed = 1;
1305 break;
1306
Patrice Chotard17e88042019-11-25 09:07:37 +01001307 case T_EOL:
1308 break;
1309
1310 default:
1311 /*
1312 * put the token back! we don't want it - it's the end
1313 * of a label and whatever token this is, it's
1314 * something for the menu level context to handle.
1315 */
1316 *c = s;
1317 return 1;
1318 }
1319
1320 if (err < 0)
1321 return err;
1322 }
1323}
1324
1325/*
1326 * This 16 comes from the limit pxelinux imposes on nested includes.
1327 *
1328 * There is no reason at all we couldn't do more, but some limit helps prevent
1329 * infinite (until crash occurs) recursion if a file tries to include itself.
1330 */
1331#define MAX_NEST_LEVEL 16
1332
1333/*
1334 * Entry point for parsing a menu file. nest_level indicates how many times
1335 * we've nested in includes. It will be 1 for the top level menu file.
1336 *
1337 * Returns 1 on success, < 0 on error.
1338 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001339static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001340 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001341{
1342 struct token t;
1343 char *s, *b, *label_name;
1344 int err;
1345
1346 b = p;
1347
1348 if (nest_level > MAX_NEST_LEVEL) {
1349 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1350 return -EMLINK;
1351 }
1352
1353 while (1) {
1354 s = p;
1355
1356 get_token(&p, &t, L_KEYWORD);
1357
1358 err = 0;
1359 switch (t.type) {
1360 case T_MENU:
1361 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001362 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001363 base + ALIGN(strlen(b) + 1, 4),
1364 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001365 break;
1366
1367 case T_TIMEOUT:
1368 err = parse_integer(&p, &cfg->timeout);
1369 break;
1370
1371 case T_LABEL:
1372 err = parse_label(&p, cfg);
1373 break;
1374
1375 case T_DEFAULT:
1376 case T_ONTIMEOUT:
1377 err = parse_sliteral(&p, &label_name);
1378
1379 if (label_name) {
1380 if (cfg->default_label)
1381 free(cfg->default_label);
1382
1383 cfg->default_label = label_name;
1384 }
1385
1386 break;
1387
Martyn Welch53dd5102024-10-09 14:15:38 +01001388 case T_FALLBACK:
1389 err = parse_sliteral(&p, &label_name);
1390
1391 if (label_name) {
1392 if (cfg->fallback_label)
1393 free(cfg->fallback_label);
1394
1395 cfg->fallback_label = label_name;
1396 }
1397
1398 break;
1399
Patrice Chotard17e88042019-11-25 09:07:37 +01001400 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001401 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001402 base + ALIGN(strlen(b), 4), cfg,
1403 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001404 break;
1405
1406 case T_PROMPT:
Manuel Traut20081242022-11-18 09:00:27 +01001407 err = parse_integer(&p, &cfg->prompt);
1408 // Do not fail if prompt configuration is undefined
1409 if (err < 0)
1410 eol_or_eof(&p);
Patrice Chotard17e88042019-11-25 09:07:37 +01001411 break;
1412
1413 case T_EOL:
1414 break;
1415
1416 case T_EOF:
1417 return 1;
1418
1419 default:
1420 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001421 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001422 eol_or_eof(&p);
1423 }
1424
1425 if (err < 0)
1426 return err;
1427 }
1428}
1429
1430/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001431 */
1432void destroy_pxe_menu(struct pxe_menu *cfg)
1433{
1434 struct list_head *pos, *n;
1435 struct pxe_label *label;
1436
Simon Glass764d0c02021-10-14 12:48:02 -06001437 free(cfg->title);
1438 free(cfg->default_label);
Martyn Welch53dd5102024-10-09 14:15:38 +01001439 free(cfg->fallback_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001440
1441 list_for_each_safe(pos, n, &cfg->labels) {
1442 label = list_entry(pos, struct pxe_label, list);
1443
1444 label_destroy(label);
1445 }
1446
1447 free(cfg);
1448}
1449
Simon Glassb0d08db2021-10-14 12:47:56 -06001450struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001451{
1452 struct pxe_menu *cfg;
1453 char *buf;
1454 int r;
1455
1456 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001457 if (!cfg)
1458 return NULL;
1459
1460 memset(cfg, 0, sizeof(struct pxe_menu));
1461
1462 INIT_LIST_HEAD(&cfg->labels);
1463
1464 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001465 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Martyn Welch2c47aac2024-10-09 14:15:39 +01001466
1467 if (ctx->use_fallback) {
1468 if (cfg->fallback_label) {
1469 printf("Setting use of fallback\n");
1470 cfg->default_label = cfg->fallback_label;
1471 } else {
1472 printf("Selected fallback option, but not set\n");
1473 }
1474 }
1475
Patrice Chotard17e88042019-11-25 09:07:37 +01001476 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001477 if (r < 0) {
1478 destroy_pxe_menu(cfg);
1479 return NULL;
1480 }
1481
1482 return cfg;
1483}
1484
1485/*
1486 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1487 * menu code.
1488 */
1489static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1490{
1491 struct pxe_label *label;
1492 struct list_head *pos;
1493 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001494 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001495 int err;
1496 int i = 1;
1497 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001498 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001499
1500 /*
1501 * Create a menu and add items for all the labels.
1502 */
1503 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
developerc2a1e9e2024-10-29 17:47:16 +08001504 cfg->prompt, NULL, label_print, NULL, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001505 if (!m)
1506 return NULL;
1507
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001508 label_override = env_get("pxe_label_override");
1509
Patrice Chotard17e88042019-11-25 09:07:37 +01001510 list_for_each(pos, &cfg->labels) {
1511 label = list_entry(pos, struct pxe_label, list);
1512
1513 sprintf(label->num, "%d", i++);
1514 if (menu_item_add(m, label->num, label) != 1) {
1515 menu_destroy(m);
1516 return NULL;
1517 }
1518 if (cfg->default_label &&
1519 (strcmp(label->name, cfg->default_label) == 0))
1520 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001521 if (label_override && !strcmp(label->name, label_override))
1522 override_num = label->num;
1523 }
1524
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001525 if (label_override) {
1526 if (override_num)
1527 default_num = override_num;
1528 else
1529 printf("Missing override pxe label: %s\n",
1530 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001531 }
1532
1533 /*
1534 * After we've created items for each label in the menu, set the
1535 * menu's default label if one was specified.
1536 */
1537 if (default_num) {
1538 err = menu_default_set(m, default_num);
1539 if (err != 1) {
1540 if (err != -ENOENT) {
1541 menu_destroy(m);
1542 return NULL;
1543 }
1544
1545 printf("Missing default: %s\n", cfg->default_label);
1546 }
1547 }
1548
1549 return m;
1550}
1551
1552/*
1553 * Try to boot any labels we have yet to attempt to boot.
1554 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001555static void boot_unattempted_labels(struct pxe_context *ctx,
1556 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001557{
1558 struct list_head *pos;
1559 struct pxe_label *label;
1560
1561 list_for_each(pos, &cfg->labels) {
1562 label = list_entry(pos, struct pxe_label, list);
1563
1564 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001565 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001566 }
1567}
1568
Simon Glassb0d08db2021-10-14 12:47:56 -06001569void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001570{
1571 void *choice;
1572 struct menu *m;
1573 int err;
1574
Kory Maincentcaabd242021-02-02 16:42:28 +01001575 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1576 /* display BMP if available */
1577 if (cfg->bmp) {
Simon Glassa686ba62024-11-15 16:19:19 -07001578 if (get_relfile(ctx, cfg->bmp, image_load_addr,
1579 BFI_LOGO, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001580#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001581 struct udevice *dev;
1582
1583 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1584 if (!err)
1585 video_clear(dev);
1586#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001587 bmp_display(image_load_addr,
1588 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1589 } else {
1590 printf("Skipping background bmp %s for failure\n",
1591 cfg->bmp);
1592 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001593 }
1594 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001595
1596 m = pxe_menu_to_menu(cfg);
1597 if (!m)
1598 return;
1599
1600 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001601 menu_destroy(m);
1602
1603 /*
1604 * err == 1 means we got a choice back from menu_get_choice.
1605 *
1606 * err == -ENOENT if the menu was setup to select the default but no
1607 * default was set. in that case, we should continue trying to boot
1608 * labels that haven't been attempted yet.
1609 *
1610 * otherwise, the user interrupted or there was some other error and
1611 * we give up.
1612 */
1613
1614 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001615 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001616 if (!err)
1617 return;
1618 } else if (err != -ENOENT) {
1619 return;
1620 }
1621
Simon Glassb0d08db2021-10-14 12:47:56 -06001622 boot_unattempted_labels(ctx, cfg);
1623}
1624
Simon Glasse719fe02021-10-14 12:48:04 -06001625int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1626 pxe_getfile_func getfile, void *userdata,
Martyn Welch2c47aac2024-10-09 14:15:39 +01001627 bool allow_abs_path, const char *bootfile, bool use_ipv6,
1628 bool use_fallback)
Simon Glassb0d08db2021-10-14 12:47:56 -06001629{
Simon Glasse719fe02021-10-14 12:48:04 -06001630 const char *last_slash;
1631 size_t path_len = 0;
1632
1633 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001634 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001635 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001636 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001637 ctx->allow_abs_path = allow_abs_path;
Sean Edmondba802862023-04-11 10:48:47 -07001638 ctx->use_ipv6 = use_ipv6;
Martyn Welch2c47aac2024-10-09 14:15:39 +01001639 ctx->use_fallback = use_fallback;
Simon Glasse719fe02021-10-14 12:48:04 -06001640
1641 /* figure out the boot directory, if there is one */
1642 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1643 return -ENOSPC;
1644 ctx->bootdir = strdup(bootfile ? bootfile : "");
1645 if (!ctx->bootdir)
1646 return -ENOMEM;
1647
1648 if (bootfile) {
1649 last_slash = strrchr(bootfile, '/');
1650 if (last_slash)
1651 path_len = (last_slash - bootfile) + 1;
1652 }
1653 ctx->bootdir[path_len] = '\0';
1654
1655 return 0;
1656}
1657
1658void pxe_destroy_ctx(struct pxe_context *ctx)
1659{
1660 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001661}
Simon Glass791bbfe2021-10-14 12:48:03 -06001662
1663int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1664{
1665 struct pxe_menu *cfg;
1666
1667 cfg = parse_pxefile(ctx, pxefile_addr_r);
1668 if (!cfg) {
1669 printf("Error parsing config file\n");
1670 return 1;
1671 }
1672
1673 if (prompt)
1674 cfg->prompt = 1;
1675
1676 handle_pxe_menu(ctx, cfg);
1677
1678 destroy_pxe_menu(cfg);
1679
1680 return 0;
1681}