blob: a14b4eb54c251798ae60f4f5bce2bc379e9cc14a [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 Glass09786df2025-03-05 17:25:05 -0700435/*
436 * label_process_fdt() - Process FDT for the label
Simon Glass00302442021-10-14 12:48:01 -0600437 *
438 * @ctx: PXE context
439 * @label: Label to process
Simon Glass09786df2025-03-05 17:25:05 -0700440 * @kernel_addr: String containing kernel address
441 * @bootm_argv: bootm arguments to fill in (this only adjusts @bootm_argv[3])
442 * Return: 0 if OK, -ENOMEM if out of memory, -ENOENT if FDT file could not be
443 * loaded
444 *
445 * fdt usage is optional:
446 * It handles the following scenarios.
447 *
448 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
449 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
450 * bootm, and adjust argc appropriately.
451 *
452 * If retrieve fails and no exact fdt blob is specified in pxe file with
453 * "fdt" label, try Scenario 2.
454 *
455 * Scenario 2: If there is an fdt_addr specified, pass it along to
456 * bootm, and adjust argc appropriately.
457 *
458 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
459 * bootm, and adjust argc appropriately, unless the image type is fitImage.
460 *
461 * Scenario 4: fdt blob is not available.
Patrice Chotard17e88042019-11-25 09:07:37 +0100462 */
Simon Glass09786df2025-03-05 17:25:05 -0700463static int label_process_fdt(struct pxe_context *ctx, struct pxe_label *label,
464 char *kernel_addr, char *bootm_argv[])
Patrice Chotard17e88042019-11-25 09:07:37 +0100465{
Tom Rini793921e2024-04-18 08:29:35 -0600466 /* For FIT, the label can be identical to kernel one */
467 if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
468 bootm_argv[3] = kernel_addr;
469 /* if fdt label is defined then get fdt from server */
470 } else if (bootm_argv[3]) {
471 char *fdtfile = NULL;
472 char *fdtfilefree = NULL;
473
474 if (label->fdt) {
475 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
476 if (strcmp("-", label->fdt))
477 fdtfile = label->fdt;
478 } else {
479 fdtfile = label->fdt;
480 }
481 } else if (label->fdtdir) {
482 char *f1, *f2, *f3, *f4, *slash;
Simon Glass827cd9c2025-03-05 17:25:04 -0700483 int len;
Tom Rini793921e2024-04-18 08:29:35 -0600484
485 f1 = env_get("fdtfile");
486 if (f1) {
487 f2 = "";
488 f3 = "";
489 f4 = "";
490 } else {
491 /*
492 * For complex cases where this code doesn't
493 * generate the correct filename, the board
494 * code should set $fdtfile during early boot,
495 * or the boot scripts should set $fdtfile
496 * before invoking "pxe" or "sysboot".
497 */
498 f1 = env_get("soc");
499 f2 = "-";
500 f3 = env_get("board");
501 f4 = ".dtb";
502 if (!f1) {
503 f1 = "";
504 f2 = "";
505 }
506 if (!f3) {
507 f2 = "";
508 f3 = "";
509 }
510 }
511
512 len = strlen(label->fdtdir);
513 if (!len)
514 slash = "./";
515 else if (label->fdtdir[len - 1] != '/')
516 slash = "/";
517 else
518 slash = "";
519
520 len = strlen(label->fdtdir) + strlen(slash) +
521 strlen(f1) + strlen(f2) + strlen(f3) +
522 strlen(f4) + 1;
523 fdtfilefree = malloc(len);
524 if (!fdtfilefree) {
525 printf("malloc fail (FDT filename)\n");
Simon Glass827cd9c2025-03-05 17:25:04 -0700526 return -ENOMEM;
Tom Rini793921e2024-04-18 08:29:35 -0600527 }
528
529 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
530 label->fdtdir, slash, f1, f2, f3, f4);
531 fdtfile = fdtfilefree;
532 }
533
534 if (fdtfile) {
535 int err = get_relfile_envaddr(ctx, fdtfile,
Simon Glassa686ba62024-11-15 16:19:19 -0700536 "fdt_addr_r",
537 (enum bootflow_img_t)IH_TYPE_FLATDT, NULL);
Tom Rini793921e2024-04-18 08:29:35 -0600538
539 free(fdtfilefree);
540 if (err < 0) {
541 bootm_argv[3] = NULL;
542
543 if (label->fdt) {
544 printf("Skipping %s for failure retrieving FDT\n",
545 label->name);
Simon Glass827cd9c2025-03-05 17:25:04 -0700546 return -ENOENT;
Tom Rini793921e2024-04-18 08:29:35 -0600547 }
548
549 if (label->fdtdir) {
550 printf("Skipping fdtdir %s for failure retrieving dts\n",
551 label->fdtdir);
552 }
553 }
554
555 if (label->kaslrseed)
556 label_boot_kaslrseed();
557
558#ifdef CONFIG_OF_LIBFDT_OVERLAY
559 if (label->fdtoverlays)
560 label_boot_fdtoverlay(ctx, label);
561#endif
562 } else {
563 bootm_argv[3] = NULL;
564 }
565 }
566
Simon Glass09786df2025-03-05 17:25:05 -0700567 return 0;
568}
569
570/**
571 * label_run_boot() - Set up the FDT and call the appropriate bootm/z/i command
572 *
573 * @ctx: PXE context
574 * @label: Label to process
575 * @kernel_addr: String containing kernel address (cannot be NULL)
576 * @initrd_addr_str: String containing initrd address (NULL if none)
577 * @initrd_filesize: String containing initrd size (only used if
578 * @initrd_addr_str)
579 * @initrd_str: initrd string to process (only used if @initrd_addr_str)
580 * Return: does not return on success, or returns 0 if the boot command
581 * returned, or -ve error value on error
582 */
583static int label_run_boot(struct pxe_context *ctx, struct pxe_label *label,
584 char *kernel_addr, char *initrd_addr_str,
585 char *initrd_filesize, char *initrd_str)
586{
587 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
588 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
589 ulong kernel_addr_r;
590 int bootm_argc = 2;
591 int zboot_argc = 3;
592 void *buf;
593 int ret;
594
595 bootm_argv[3] = env_get("fdt_addr_r");
596
597 ret = label_process_fdt(ctx, label, kernel_addr, bootm_argv);
598 if (ret)
599 return ret;
600
Tom Rini793921e2024-04-18 08:29:35 -0600601 bootm_argv[1] = kernel_addr;
602 zboot_argv[1] = kernel_addr;
603
604 if (initrd_addr_str) {
605 bootm_argv[2] = initrd_str;
606 bootm_argc = 3;
607
608 zboot_argv[3] = initrd_addr_str;
609 zboot_argv[4] = initrd_filesize;
610 zboot_argc = 5;
611 }
612
613 if (!bootm_argv[3]) {
614 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
615 if (strcmp("-", label->fdt))
616 bootm_argv[3] = env_get("fdt_addr");
617 } else {
618 bootm_argv[3] = env_get("fdt_addr");
619 }
620 }
621
622 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
623 buf = map_sysmem(kernel_addr_r, 0);
624
625 if (!bootm_argv[3] && genimg_get_format(buf) != IMAGE_FORMAT_FIT) {
626 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
627 if (strcmp("-", label->fdt))
628 bootm_argv[3] = env_get("fdtcontroladdr");
629 } else {
630 bootm_argv[3] = env_get("fdtcontroladdr");
631 }
632 }
633
634 if (bootm_argv[3]) {
635 if (!bootm_argv[2])
636 bootm_argv[2] = "-";
637 bootm_argc = 4;
638 }
639
640 /* Try bootm for legacy and FIT format image */
641 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
Simon Glass9b2c78c2024-06-19 06:34:50 -0600642 IS_ENABLED(CONFIG_CMD_BOOTM)) {
643 log_debug("using bootm\n");
Tom Rini793921e2024-04-18 08:29:35 -0600644 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
645 /* Try booting an AArch64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600646 } else if (IS_ENABLED(CONFIG_CMD_BOOTI)) {
647 log_debug("using booti\n");
Tom Rini793921e2024-04-18 08:29:35 -0600648 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
649 /* Try booting a Image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600650 } else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) {
651 log_debug("using bootz\n");
Tom Rini793921e2024-04-18 08:29:35 -0600652 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
653 /* Try booting an x86_64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600654 } else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) {
655 log_debug("using zboot\n");
Tom Rini793921e2024-04-18 08:29:35 -0600656 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Simon Glass9b2c78c2024-06-19 06:34:50 -0600657 }
Tom Rini793921e2024-04-18 08:29:35 -0600658
659 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +0100660
Simon Glass827cd9c2025-03-05 17:25:04 -0700661 return 0;
662}
663
664/**
665 * label_boot() - Boot according to the contents of a pxe_label
666 *
667 * If we can't boot for any reason, we return. A successful boot never
668 * returns.
669 *
670 * The kernel will be stored in the location given by the 'kernel_addr_r'
671 * environment variable.
672 *
673 * If the label specifies an initrd file, it will be stored in the location
674 * given by the 'ramdisk_addr_r' environment variable.
675 *
676 * If the label specifies an 'append' line, its contents will overwrite that
677 * of the 'bootargs' environment variable.
678 *
679 * @ctx: PXE context
680 * @label: Label to process
681 * Returns does not return on success, otherwise returns 0 if a localboot
682 * label was processed, or 1 on error
683 */
684static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
685{
686 char *kernel_addr = NULL;
687 char *initrd_addr_str = NULL;
688 char initrd_filesize[10];
689 char initrd_str[28];
690 char mac_str[29] = "";
691 char ip_str[68] = "";
692 char *fit_addr = NULL;
693
694 label_print(label);
695
696 label->attempted = 1;
697
698 if (label->localboot) {
699 if (label->localboot_val >= 0)
700 label_localboot(label);
701 return 0;
702 }
703
704 if (!label->kernel) {
705 printf("No kernel given, skipping %s\n",
706 label->name);
707 return 1;
708 }
709
710 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
711 (enum bootflow_img_t)IH_TYPE_KERNEL, NULL)
712 < 0) {
713 printf("Skipping %s for failure retrieving kernel\n",
714 label->name);
715 return 1;
716 }
717
718 kernel_addr = env_get("kernel_addr_r");
719 /* for FIT, append the configuration identifier */
720 if (label->config) {
721 int len = strlen(kernel_addr) + strlen(label->config) + 1;
722
723 fit_addr = malloc(len);
724 if (!fit_addr) {
725 printf("malloc fail (FIT address)\n");
726 return 1;
727 }
728 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
729 kernel_addr = fit_addr;
730 }
731
732 /* For FIT, the label can be identical to kernel one */
733 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
734 initrd_addr_str = kernel_addr;
735 } else if (label->initrd) {
736 ulong size;
737 int ret;
738
739 ret = get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
740 (enum bootflow_img_t)IH_TYPE_RAMDISK,
741 &size);
742 if (ret < 0) {
743 printf("Skipping %s for failure retrieving initrd\n",
744 label->name);
745 goto cleanup;
746 }
747 strcpy(initrd_filesize, simple_xtoa(size));
748 initrd_addr_str = env_get("ramdisk_addr_r");
749 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
750 initrd_addr_str, size);
751 if (size >= sizeof(initrd_str))
752 goto cleanup;
753 }
754
755 if (label->ipappend & 0x1) {
756 sprintf(ip_str, " ip=%s:%s:%s:%s",
757 env_get("ipaddr"), env_get("serverip"),
758 env_get("gatewayip"), env_get("netmask"));
759 }
760
761 if (IS_ENABLED(CONFIG_CMD_NET)) {
762 if (label->ipappend & 0x2) {
763 int err;
764
765 strcpy(mac_str, " BOOTIF=");
766 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
767 if (err < 0)
768 mac_str[0] = '\0';
769 }
770 }
771
772 if ((label->ipappend & 0x3) || label->append) {
773 char bootargs[CONFIG_SYS_CBSIZE] = "";
774 char finalbootargs[CONFIG_SYS_CBSIZE];
775
776 if (strlen(label->append ?: "") +
777 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
778 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
779 strlen(label->append ?: ""),
780 strlen(ip_str), strlen(mac_str),
781 sizeof(bootargs));
782 goto cleanup;
783 }
784
785 if (label->append)
786 strlcpy(bootargs, label->append, sizeof(bootargs));
787
788 strcat(bootargs, ip_str);
789 strcat(bootargs, mac_str);
790
791 cli_simple_process_macros(bootargs, finalbootargs,
792 sizeof(finalbootargs));
793 env_set("bootargs", finalbootargs);
794 printf("append: %s\n", finalbootargs);
795 }
796
797 label_run_boot(ctx, label, kernel_addr, initrd_addr_str,
798 initrd_filesize, initrd_str);
799 /* ignore the error value since we are going to fail anyway */
800
Patrice Chotard17e88042019-11-25 09:07:37 +0100801cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600802 free(fit_addr);
803
Simon Glass827cd9c2025-03-05 17:25:04 -0700804 return 1; /* returning is always failure */
Patrice Chotard17e88042019-11-25 09:07:37 +0100805}
806
Simon Glass00302442021-10-14 12:48:01 -0600807/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100808enum token_type {
809 T_EOL,
810 T_STRING,
811 T_EOF,
812 T_MENU,
813 T_TITLE,
814 T_TIMEOUT,
815 T_LABEL,
816 T_KERNEL,
817 T_LINUX,
818 T_APPEND,
819 T_INITRD,
820 T_LOCALBOOT,
821 T_DEFAULT,
822 T_PROMPT,
823 T_INCLUDE,
824 T_FDT,
825 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100826 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100827 T_ONTIMEOUT,
828 T_IPAPPEND,
829 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800830 T_KASLRSEED,
Martyn Welch53dd5102024-10-09 14:15:38 +0100831 T_FALLBACK,
Patrice Chotard17e88042019-11-25 09:07:37 +0100832 T_INVALID
833};
834
Simon Glass00302442021-10-14 12:48:01 -0600835/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100836struct token {
837 char *val;
838 enum token_type type;
839};
840
Simon Glass00302442021-10-14 12:48:01 -0600841/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100842static const struct token keywords[] = {
843 {"menu", T_MENU},
844 {"title", T_TITLE},
845 {"timeout", T_TIMEOUT},
846 {"default", T_DEFAULT},
847 {"prompt", T_PROMPT},
848 {"label", T_LABEL},
849 {"kernel", T_KERNEL},
850 {"linux", T_LINUX},
851 {"localboot", T_LOCALBOOT},
852 {"append", T_APPEND},
853 {"initrd", T_INITRD},
854 {"include", T_INCLUDE},
855 {"devicetree", T_FDT},
856 {"fdt", T_FDT},
857 {"devicetreedir", T_FDTDIR},
858 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100859 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200860 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100861 {"ontimeout", T_ONTIMEOUT,},
862 {"ipappend", T_IPAPPEND,},
863 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800864 {"kaslrseed", T_KASLRSEED,},
Martyn Welch53dd5102024-10-09 14:15:38 +0100865 {"fallback", T_FALLBACK,},
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
Martyn Welch53dd5102024-10-09 14:15:38 +01001408 case T_FALLBACK:
1409 err = parse_sliteral(&p, &label_name);
1410
1411 if (label_name) {
1412 if (cfg->fallback_label)
1413 free(cfg->fallback_label);
1414
1415 cfg->fallback_label = label_name;
1416 }
1417
1418 break;
1419
Patrice Chotard17e88042019-11-25 09:07:37 +01001420 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001421 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001422 base + ALIGN(strlen(b), 4), cfg,
1423 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001424 break;
1425
1426 case T_PROMPT:
Manuel Traut20081242022-11-18 09:00:27 +01001427 err = parse_integer(&p, &cfg->prompt);
1428 // Do not fail if prompt configuration is undefined
1429 if (err < 0)
1430 eol_or_eof(&p);
Patrice Chotard17e88042019-11-25 09:07:37 +01001431 break;
1432
1433 case T_EOL:
1434 break;
1435
1436 case T_EOF:
1437 return 1;
1438
1439 default:
1440 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001441 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001442 eol_or_eof(&p);
1443 }
1444
1445 if (err < 0)
1446 return err;
1447 }
1448}
1449
1450/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001451 */
1452void destroy_pxe_menu(struct pxe_menu *cfg)
1453{
1454 struct list_head *pos, *n;
1455 struct pxe_label *label;
1456
Simon Glass764d0c02021-10-14 12:48:02 -06001457 free(cfg->title);
1458 free(cfg->default_label);
Martyn Welch53dd5102024-10-09 14:15:38 +01001459 free(cfg->fallback_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001460
1461 list_for_each_safe(pos, n, &cfg->labels) {
1462 label = list_entry(pos, struct pxe_label, list);
1463
1464 label_destroy(label);
1465 }
1466
1467 free(cfg);
1468}
1469
Simon Glassb0d08db2021-10-14 12:47:56 -06001470struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001471{
1472 struct pxe_menu *cfg;
1473 char *buf;
1474 int r;
1475
1476 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001477 if (!cfg)
1478 return NULL;
1479
1480 memset(cfg, 0, sizeof(struct pxe_menu));
1481
1482 INIT_LIST_HEAD(&cfg->labels);
1483
1484 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001485 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Martyn Welch2c47aac2024-10-09 14:15:39 +01001486
1487 if (ctx->use_fallback) {
1488 if (cfg->fallback_label) {
1489 printf("Setting use of fallback\n");
1490 cfg->default_label = cfg->fallback_label;
1491 } else {
1492 printf("Selected fallback option, but not set\n");
1493 }
1494 }
1495
Patrice Chotard17e88042019-11-25 09:07:37 +01001496 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001497 if (r < 0) {
1498 destroy_pxe_menu(cfg);
1499 return NULL;
1500 }
1501
1502 return cfg;
1503}
1504
1505/*
1506 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1507 * menu code.
1508 */
1509static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1510{
1511 struct pxe_label *label;
1512 struct list_head *pos;
1513 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001514 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001515 int err;
1516 int i = 1;
1517 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001518 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001519
1520 /*
1521 * Create a menu and add items for all the labels.
1522 */
1523 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
developerc2a1e9e2024-10-29 17:47:16 +08001524 cfg->prompt, NULL, label_print, NULL, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001525 if (!m)
1526 return NULL;
1527
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001528 label_override = env_get("pxe_label_override");
1529
Patrice Chotard17e88042019-11-25 09:07:37 +01001530 list_for_each(pos, &cfg->labels) {
1531 label = list_entry(pos, struct pxe_label, list);
1532
1533 sprintf(label->num, "%d", i++);
1534 if (menu_item_add(m, label->num, label) != 1) {
1535 menu_destroy(m);
1536 return NULL;
1537 }
1538 if (cfg->default_label &&
1539 (strcmp(label->name, cfg->default_label) == 0))
1540 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001541 if (label_override && !strcmp(label->name, label_override))
1542 override_num = label->num;
1543 }
1544
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001545 if (label_override) {
1546 if (override_num)
1547 default_num = override_num;
1548 else
1549 printf("Missing override pxe label: %s\n",
1550 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001551 }
1552
1553 /*
1554 * After we've created items for each label in the menu, set the
1555 * menu's default label if one was specified.
1556 */
1557 if (default_num) {
1558 err = menu_default_set(m, default_num);
1559 if (err != 1) {
1560 if (err != -ENOENT) {
1561 menu_destroy(m);
1562 return NULL;
1563 }
1564
1565 printf("Missing default: %s\n", cfg->default_label);
1566 }
1567 }
1568
1569 return m;
1570}
1571
1572/*
1573 * Try to boot any labels we have yet to attempt to boot.
1574 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001575static void boot_unattempted_labels(struct pxe_context *ctx,
1576 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001577{
1578 struct list_head *pos;
1579 struct pxe_label *label;
1580
1581 list_for_each(pos, &cfg->labels) {
1582 label = list_entry(pos, struct pxe_label, list);
1583
1584 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001585 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001586 }
1587}
1588
Simon Glassb0d08db2021-10-14 12:47:56 -06001589void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001590{
1591 void *choice;
1592 struct menu *m;
1593 int err;
1594
Kory Maincentcaabd242021-02-02 16:42:28 +01001595 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1596 /* display BMP if available */
1597 if (cfg->bmp) {
Simon Glassa686ba62024-11-15 16:19:19 -07001598 if (get_relfile(ctx, cfg->bmp, image_load_addr,
1599 BFI_LOGO, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001600#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001601 struct udevice *dev;
1602
1603 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1604 if (!err)
1605 video_clear(dev);
1606#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001607 bmp_display(image_load_addr,
1608 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1609 } else {
1610 printf("Skipping background bmp %s for failure\n",
1611 cfg->bmp);
1612 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001613 }
1614 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001615
1616 m = pxe_menu_to_menu(cfg);
1617 if (!m)
1618 return;
1619
1620 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001621 menu_destroy(m);
1622
1623 /*
1624 * err == 1 means we got a choice back from menu_get_choice.
1625 *
1626 * err == -ENOENT if the menu was setup to select the default but no
1627 * default was set. in that case, we should continue trying to boot
1628 * labels that haven't been attempted yet.
1629 *
1630 * otherwise, the user interrupted or there was some other error and
1631 * we give up.
1632 */
1633
1634 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001635 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001636 if (!err)
1637 return;
1638 } else if (err != -ENOENT) {
1639 return;
1640 }
1641
Simon Glassb0d08db2021-10-14 12:47:56 -06001642 boot_unattempted_labels(ctx, cfg);
1643}
1644
Simon Glasse719fe02021-10-14 12:48:04 -06001645int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1646 pxe_getfile_func getfile, void *userdata,
Martyn Welch2c47aac2024-10-09 14:15:39 +01001647 bool allow_abs_path, const char *bootfile, bool use_ipv6,
1648 bool use_fallback)
Simon Glassb0d08db2021-10-14 12:47:56 -06001649{
Simon Glasse719fe02021-10-14 12:48:04 -06001650 const char *last_slash;
1651 size_t path_len = 0;
1652
1653 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001654 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001655 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001656 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001657 ctx->allow_abs_path = allow_abs_path;
Sean Edmondba802862023-04-11 10:48:47 -07001658 ctx->use_ipv6 = use_ipv6;
Martyn Welch2c47aac2024-10-09 14:15:39 +01001659 ctx->use_fallback = use_fallback;
Simon Glasse719fe02021-10-14 12:48:04 -06001660
1661 /* figure out the boot directory, if there is one */
1662 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1663 return -ENOSPC;
1664 ctx->bootdir = strdup(bootfile ? bootfile : "");
1665 if (!ctx->bootdir)
1666 return -ENOMEM;
1667
1668 if (bootfile) {
1669 last_slash = strrchr(bootfile, '/');
1670 if (last_slash)
1671 path_len = (last_slash - bootfile) + 1;
1672 }
1673 ctx->bootdir[path_len] = '\0';
1674
1675 return 0;
1676}
1677
1678void pxe_destroy_ctx(struct pxe_context *ctx)
1679{
1680 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001681}
Simon Glass791bbfe2021-10-14 12:48:03 -06001682
1683int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1684{
1685 struct pxe_menu *cfg;
1686
1687 cfg = parse_pxefile(ctx, pxefile_addr_r);
1688 if (!cfg) {
1689 printf("Error parsing config file\n");
1690 return 1;
1691 }
1692
1693 if (prompt)
1694 cfg->prompt = 1;
1695
1696 handle_pxe_menu(ctx, cfg);
1697
1698 destroy_pxe_menu(cfg);
1699
1700 return 0;
1701}