blob: c606da9e96ba25dcdd9403499f63d172d1bfaab0 [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 Glassb3beb6f2025-03-05 17:25:12 -070010#include <bootm.h>
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010012#include <dm.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010013#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070014#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010016#include <malloc.h>
17#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <net.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010019#include <fdt_support.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010020#include <video.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010021#include <linux/libfdt.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010022#include <linux/string.h>
23#include <linux/ctype.h>
24#include <errno.h>
25#include <linux/list.h>
26
Zhang Ning9c1d9c52022-02-01 08:33:37 +080027#include <rng.h>
Zhang Ning9c1d9c52022-02-01 08:33:37 +080028
Patrice Chotard17e88042019-11-25 09:07:37 +010029#include <splash.h>
30#include <asm/io.h>
31
32#include "menu.h"
33#include "cli.h"
34
35#include "pxe_utils.h"
36
Ben Wolsieffer6273f132019-11-28 00:07:08 -050037#define MAX_TFTP_PATH_LEN 512
Patrice Chotard17e88042019-11-25 09:07:37 +010038
Simon Glassa9401b92021-10-14 12:48:08 -060039int pxe_get_file_size(ulong *sizep)
40{
41 const char *val;
42
43 val = from_env("filesize");
44 if (!val)
45 return -ENOENT;
46
47 if (strict_strtoul(val, 16, sizep) < 0)
48 return -EINVAL;
49
50 return 0;
51}
52
Simon Glass00302442021-10-14 12:48:01 -060053/**
54 * format_mac_pxe() - obtain a MAC address in the PXE format
55 *
56 * This produces a MAC-address string in the format for the current ethernet
57 * device:
58 *
59 * 01-aa-bb-cc-dd-ee-ff
60 *
61 * where aa-ff is the MAC address in hex
62 *
63 * @outbuf: Buffer to write string to
64 * @outbuf_len: length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010065 * Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
Simon Glass00302442021-10-14 12:48:01 -060066 * current ethernet device
67 */
Patrice Chotard17e88042019-11-25 09:07:37 +010068int format_mac_pxe(char *outbuf, size_t outbuf_len)
69{
70 uchar ethaddr[6];
71
72 if (outbuf_len < 21) {
73 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Simon Glass00302442021-10-14 12:48:01 -060074 return -ENOSPC;
Patrice Chotard17e88042019-11-25 09:07:37 +010075 }
76
77 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
78 return -ENOENT;
79
80 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
81 ethaddr[0], ethaddr[1], ethaddr[2],
82 ethaddr[3], ethaddr[4], ethaddr[5]);
83
84 return 1;
85}
86
Simon Glass00302442021-10-14 12:48:01 -060087/**
Simon Glass00302442021-10-14 12:48:01 -060088 * get_relfile() - read a file relative to the PXE file
89 *
Patrice Chotard17e88042019-11-25 09:07:37 +010090 * As in pxelinux, paths to files referenced from files we retrieve are
91 * relative to the location of bootfile. get_relfile takes such a path and
92 * joins it with the bootfile path to get the full path to the target file. If
93 * the bootfile path is NULL, we use file_path as is.
94 *
Simon Glass00302442021-10-14 12:48:01 -060095 * @ctx: PXE context
96 * @file_path: File path to read (relative to the PXE file)
97 * @file_addr: Address to load file to
Simon Glassa9401b92021-10-14 12:48:08 -060098 * @filesizep: If not NULL, returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -060099 * Returns 1 for success, or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100100 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600101static int get_relfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa686ba62024-11-15 16:19:19 -0700102 unsigned long file_addr, enum bootflow_img_t type,
103 ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100104{
105 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100106 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100107 char addr_buf[18];
Simon Glassa9401b92021-10-14 12:48:08 -0600108 ulong size;
109 int ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100110
Simon Glass5e3e39a2021-10-14 12:48:05 -0600111 if (file_path[0] == '/' && ctx->allow_abs_path)
112 *relfile = '\0';
113 else
114 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
Patrice Chotard17e88042019-11-25 09:07:37 +0100115
Simon Glass5e3e39a2021-10-14 12:48:05 -0600116 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard17e88042019-11-25 09:07:37 +0100117
118 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100119 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100120
121 return -ENAMETOOLONG;
122 }
123
124 strcat(relfile, file_path);
125
126 printf("Retrieving file: %s\n", relfile);
127
128 sprintf(addr_buf, "%lx", file_addr);
129
Simon Glassa686ba62024-11-15 16:19:19 -0700130 ret = ctx->getfile(ctx, relfile, addr_buf, type, &size);
Simon Glassa9401b92021-10-14 12:48:08 -0600131 if (ret < 0)
132 return log_msg_ret("get", ret);
133 if (filesizep)
134 *filesizep = size;
135
136 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100137}
138
Simon Glassb0d08db2021-10-14 12:47:56 -0600139int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600140 ulong file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100141{
Simon Glassa9401b92021-10-14 12:48:08 -0600142 ulong size;
Patrice Chotard17e88042019-11-25 09:07:37 +0100143 int err;
144 char *buf;
145
Simon Glassa686ba62024-11-15 16:19:19 -0700146 err = get_relfile(ctx, file_path, file_addr, BFI_EXTLINUX_CFG,
147 &size);
Patrice Chotard17e88042019-11-25 09:07:37 +0100148 if (err < 0)
149 return err;
150
Simon Glassa9401b92021-10-14 12:48:08 -0600151 buf = map_sysmem(file_addr + size, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +0100152 *buf = '\0';
153 unmap_sysmem(buf);
154
155 return 1;
156}
157
158#define PXELINUX_DIR "pxelinux.cfg/"
159
Simon Glass00302442021-10-14 12:48:01 -0600160/**
161 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
162 *
163 * @ctx: PXE context
164 * @file: Filename to process (relative to pxelinux.cfg/)
165 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
166 * or other value < 0 on other error
167 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600168int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100169 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100170{
171 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100172 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100173
174 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
175 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100176 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100177 return -ENAMETOOLONG;
178 }
179
180 sprintf(path, PXELINUX_DIR "%s", file);
181
Simon Glassb0d08db2021-10-14 12:47:56 -0600182 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100183}
184
Simon Glass00302442021-10-14 12:48:01 -0600185/**
186 * get_relfile_envaddr() - read a file to an address in an env var
187 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100188 * Wrapper to make it easier to store the file at file_path in the location
189 * specified by envaddr_name. file_path will be joined to the bootfile path,
190 * if any is specified.
191 *
Simon Glass00302442021-10-14 12:48:01 -0600192 * @ctx: PXE context
193 * @file_path: File path to read (relative to the PXE file)
194 * @envaddr_name: Name of environment variable which contains the address to
195 * load to
Simon Glassa686ba62024-11-15 16:19:19 -0700196 * @type: File type
Simon Glassa9401b92021-10-14 12:48:08 -0600197 * @filesizep: Returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -0600198 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
199 * environment variable, -EINVAL if its format is not valid hex, or other
200 * value < 0 on other error
Patrice Chotard17e88042019-11-25 09:07:37 +0100201 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600202static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Simon Glassa686ba62024-11-15 16:19:19 -0700203 const char *envaddr_name,
204 enum bootflow_img_t type, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100205{
206 unsigned long file_addr;
207 char *envaddr;
208
209 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100210 if (!envaddr)
211 return -ENOENT;
212
213 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
214 return -EINVAL;
215
Simon Glassa686ba62024-11-15 16:19:19 -0700216 return get_relfile(ctx, file_path, file_addr, type, filesizep);
Patrice Chotard17e88042019-11-25 09:07:37 +0100217}
218
Simon Glass00302442021-10-14 12:48:01 -0600219/**
220 * label_create() - crate a new PXE label
221 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100222 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
223 * result must be free()'d to reclaim the memory.
224 *
Simon Glass00302442021-10-14 12:48:01 -0600225 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100226 */
227static struct pxe_label *label_create(void)
228{
229 struct pxe_label *label;
230
231 label = malloc(sizeof(struct pxe_label));
Patrice Chotard17e88042019-11-25 09:07:37 +0100232 if (!label)
233 return NULL;
234
235 memset(label, 0, sizeof(struct pxe_label));
236
237 return label;
238}
239
Simon Glass00302442021-10-14 12:48:01 -0600240/**
241 * label_destroy() - free the memory used by a pxe_label
242 *
243 * This frees @label itself as well as memory used by its name,
244 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
245 * they're non-NULL.
Patrice Chotard17e88042019-11-25 09:07:37 +0100246 *
247 * So - be sure to only use dynamically allocated memory for the members of
248 * the pxe_label struct, unless you want to clean it up first. These are
249 * currently only created by the pxe file parsing code.
Simon Glass00302442021-10-14 12:48:01 -0600250 *
251 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100252 */
253static void label_destroy(struct pxe_label *label)
254{
Simon Glass764d0c02021-10-14 12:48:02 -0600255 free(label->name);
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +0200256 free(label->kernel_label);
Simon Glass764d0c02021-10-14 12:48:02 -0600257 free(label->kernel);
258 free(label->config);
259 free(label->append);
260 free(label->initrd);
261 free(label->fdt);
262 free(label->fdtdir);
263 free(label->fdtoverlays);
Patrice Chotard17e88042019-11-25 09:07:37 +0100264 free(label);
265}
266
Simon Glass00302442021-10-14 12:48:01 -0600267/**
268 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100269 *
270 * This is passed as a callback to the menu code for displaying each
271 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600272 *
273 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-11-25 09:07:37 +0100274 */
275static void label_print(void *data)
276{
277 struct pxe_label *label = data;
278 const char *c = label->menu ? label->menu : label->name;
279
280 printf("%s:\t%s\n", label->num, c);
281}
282
Simon Glass00302442021-10-14 12:48:01 -0600283/**
284 * label_localboot() - Boot a label that specified 'localboot'
285 *
286 * This requires that the 'localcmd' environment variable is defined. Its
287 * contents will be executed as U-Boot commands. If the label specified an
288 * 'append' line, its contents will be used to overwrite the contents of the
289 * 'bootargs' environment variable prior to running 'localcmd'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100290 *
Simon Glass00302442021-10-14 12:48:01 -0600291 * @label: Label to process
292 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100293 */
294static int label_localboot(struct pxe_label *label)
295{
296 char *localcmd;
297
298 localcmd = from_env("localcmd");
Patrice Chotard17e88042019-11-25 09:07:37 +0100299 if (!localcmd)
300 return -ENOENT;
301
302 if (label->append) {
303 char bootargs[CONFIG_SYS_CBSIZE];
304
Simon Glassc7b03e82020-11-05 10:33:47 -0700305 cli_simple_process_macros(label->append, bootargs,
306 sizeof(bootargs));
Patrice Chotard17e88042019-11-25 09:07:37 +0100307 env_set("bootargs", bootargs);
308 }
309
310 debug("running: %s\n", localcmd);
311
312 return run_command_list(localcmd, strlen(localcmd), 0);
313}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100314
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800315/*
316 * label_boot_kaslrseed generate kaslrseed from hw rng
317 */
318
319static void label_boot_kaslrseed(void)
320{
Marek Vasut0d871e72024-04-26 01:02:07 +0200321#if CONFIG_IS_ENABLED(DM_RNG)
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800322 ulong fdt_addr;
323 struct fdt_header *working_fdt;
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800324 int err;
325
326 /* Get the main fdt and map it */
327 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
328 working_fdt = map_sysmem(fdt_addr, 0);
329 err = fdt_check_header(working_fdt);
330 if (err)
331 return;
332
333 /* add extra size for holding kaslr-seed */
334 /* err is new fdt size, 0 or negtive */
335 err = fdt_shrink_to_minimum(working_fdt, 512);
336 if (err <= 0)
337 return;
338
Tim Harveyc75fab42024-06-18 14:06:08 -0700339 fdt_kaslrseed(working_fdt, true);
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800340#endif
341 return;
342}
343
Simon Glass00302442021-10-14 12:48:01 -0600344/**
345 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200346 * or 'devicetree-overlay'
Simon Glass00302442021-10-14 12:48:01 -0600347 *
348 * @ctx: PXE context
349 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100350 */
351#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600352static void label_boot_fdtoverlay(struct pxe_context *ctx,
353 struct pxe_label *label)
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100354{
355 char *fdtoverlay = label->fdtoverlays;
356 struct fdt_header *working_fdt;
357 char *fdtoverlay_addr_env;
358 ulong fdtoverlay_addr;
359 ulong fdt_addr;
360 int err;
361
362 /* Get the main fdt and map it */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600363 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100364 working_fdt = map_sysmem(fdt_addr, 0);
365 err = fdt_check_header(working_fdt);
366 if (err)
367 return;
368
369 /* Get the specific overlay loading address */
370 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
371 if (!fdtoverlay_addr_env) {
372 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
373 return;
374 }
375
Simon Glass3ff49ec2021-07-24 09:03:29 -0600376 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100377
378 /* Cycle over the overlay files and apply them in order */
379 do {
380 struct fdt_header *blob;
381 char *overlayfile;
382 char *end;
383 int len;
384
385 /* Drop leading spaces */
386 while (*fdtoverlay == ' ')
387 ++fdtoverlay;
388
389 /* Copy a single filename if multiple provided */
390 end = strstr(fdtoverlay, " ");
391 if (end) {
392 len = (int)(end - fdtoverlay);
393 overlayfile = malloc(len + 1);
394 strncpy(overlayfile, fdtoverlay, len);
395 overlayfile[len] = '\0';
396 } else
397 overlayfile = fdtoverlay;
398
399 if (!strlen(overlayfile))
400 goto skip_overlay;
401
402 /* Load overlay file */
Simon Glassa9401b92021-10-14 12:48:08 -0600403 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
Simon Glassa686ba62024-11-15 16:19:19 -0700404 (enum bootflow_img_t)IH_TYPE_FLATDT,
Simon Glassa9401b92021-10-14 12:48:08 -0600405 NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100406 if (err < 0) {
407 printf("Failed loading overlay %s\n", overlayfile);
408 goto skip_overlay;
409 }
410
411 /* Resize main fdt */
412 fdt_shrink_to_minimum(working_fdt, 8192);
413
414 blob = map_sysmem(fdtoverlay_addr, 0);
415 err = fdt_check_header(blob);
416 if (err) {
417 printf("Invalid overlay %s, skipping\n",
418 overlayfile);
419 goto skip_overlay;
420 }
421
422 err = fdt_overlay_apply_verbose(working_fdt, blob);
423 if (err) {
424 printf("Failed to apply overlay %s, skipping\n",
425 overlayfile);
426 goto skip_overlay;
427 }
428
429skip_overlay:
430 if (end)
431 free(overlayfile);
432 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
433}
434#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100435
Simon Glass09786df2025-03-05 17:25:05 -0700436/*
437 * label_process_fdt() - Process FDT for the label
Simon Glass00302442021-10-14 12:48:01 -0600438 *
439 * @ctx: PXE context
440 * @label: Label to process
Simon Glass09786df2025-03-05 17:25:05 -0700441 * @kernel_addr: String containing kernel address
Simon Glass08d0f4a2025-03-05 17:25:06 -0700442 * @fdt_argp: bootm argument to fill in, for FDT
Simon Glass09786df2025-03-05 17:25:05 -0700443 * Return: 0 if OK, -ENOMEM if out of memory, -ENOENT if FDT file could not be
444 * loaded
445 *
446 * fdt usage is optional:
447 * It handles the following scenarios.
448 *
449 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
450 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
451 * bootm, and adjust argc appropriately.
452 *
453 * If retrieve fails and no exact fdt blob is specified in pxe file with
454 * "fdt" label, try Scenario 2.
455 *
456 * Scenario 2: If there is an fdt_addr specified, pass it along to
457 * bootm, and adjust argc appropriately.
458 *
459 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
460 * bootm, and adjust argc appropriately, unless the image type is fitImage.
461 *
462 * Scenario 4: fdt blob is not available.
Patrice Chotard17e88042019-11-25 09:07:37 +0100463 */
Simon Glass09786df2025-03-05 17:25:05 -0700464static int label_process_fdt(struct pxe_context *ctx, struct pxe_label *label,
Simon Glassb3beb6f2025-03-05 17:25:12 -0700465 char *kernel_addr, const char **fdt_argp)
Patrice Chotard17e88042019-11-25 09:07:37 +0100466{
Tom Rini793921e2024-04-18 08:29:35 -0600467 /* For FIT, the label can be identical to kernel one */
468 if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
Simon Glass08d0f4a2025-03-05 17:25:06 -0700469 *fdt_argp = kernel_addr;
Tom Rini793921e2024-04-18 08:29:35 -0600470 /* if fdt label is defined then get fdt from server */
Simon Glass08d0f4a2025-03-05 17:25:06 -0700471 } else if (*fdt_argp) {
Tom Rini793921e2024-04-18 08:29:35 -0600472 char *fdtfile = NULL;
473 char *fdtfilefree = NULL;
474
475 if (label->fdt) {
476 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
477 if (strcmp("-", label->fdt))
478 fdtfile = label->fdt;
479 } else {
480 fdtfile = label->fdt;
481 }
482 } else if (label->fdtdir) {
483 char *f1, *f2, *f3, *f4, *slash;
Simon Glass827cd9c2025-03-05 17:25:04 -0700484 int len;
Tom Rini793921e2024-04-18 08:29:35 -0600485
486 f1 = env_get("fdtfile");
487 if (f1) {
488 f2 = "";
489 f3 = "";
490 f4 = "";
491 } else {
492 /*
493 * For complex cases where this code doesn't
494 * generate the correct filename, the board
495 * code should set $fdtfile during early boot,
496 * or the boot scripts should set $fdtfile
497 * before invoking "pxe" or "sysboot".
498 */
499 f1 = env_get("soc");
500 f2 = "-";
501 f3 = env_get("board");
502 f4 = ".dtb";
503 if (!f1) {
504 f1 = "";
505 f2 = "";
506 }
507 if (!f3) {
508 f2 = "";
509 f3 = "";
510 }
511 }
512
513 len = strlen(label->fdtdir);
514 if (!len)
515 slash = "./";
516 else if (label->fdtdir[len - 1] != '/')
517 slash = "/";
518 else
519 slash = "";
520
521 len = strlen(label->fdtdir) + strlen(slash) +
522 strlen(f1) + strlen(f2) + strlen(f3) +
523 strlen(f4) + 1;
524 fdtfilefree = malloc(len);
525 if (!fdtfilefree) {
526 printf("malloc fail (FDT filename)\n");
Simon Glass827cd9c2025-03-05 17:25:04 -0700527 return -ENOMEM;
Tom Rini793921e2024-04-18 08:29:35 -0600528 }
529
530 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
531 label->fdtdir, slash, f1, f2, f3, f4);
532 fdtfile = fdtfilefree;
533 }
534
535 if (fdtfile) {
536 int err = get_relfile_envaddr(ctx, fdtfile,
Simon Glassa686ba62024-11-15 16:19:19 -0700537 "fdt_addr_r",
538 (enum bootflow_img_t)IH_TYPE_FLATDT, NULL);
Tom Rini793921e2024-04-18 08:29:35 -0600539
540 free(fdtfilefree);
541 if (err < 0) {
Simon Glass08d0f4a2025-03-05 17:25:06 -0700542 *fdt_argp = NULL;
Tom Rini793921e2024-04-18 08:29:35 -0600543
544 if (label->fdt) {
545 printf("Skipping %s for failure retrieving FDT\n",
546 label->name);
Simon Glass827cd9c2025-03-05 17:25:04 -0700547 return -ENOENT;
Tom Rini793921e2024-04-18 08:29:35 -0600548 }
549
550 if (label->fdtdir) {
551 printf("Skipping fdtdir %s for failure retrieving dts\n",
552 label->fdtdir);
553 }
554 }
555
556 if (label->kaslrseed)
557 label_boot_kaslrseed();
558
559#ifdef CONFIG_OF_LIBFDT_OVERLAY
560 if (label->fdtoverlays)
561 label_boot_fdtoverlay(ctx, label);
562#endif
563 } else {
Simon Glass08d0f4a2025-03-05 17:25:06 -0700564 *fdt_argp = NULL;
Tom Rini793921e2024-04-18 08:29:35 -0600565 }
566 }
567
Simon Glass09786df2025-03-05 17:25:05 -0700568 return 0;
569}
570
571/**
572 * label_run_boot() - Set up the FDT and call the appropriate bootm/z/i command
573 *
574 * @ctx: PXE context
575 * @label: Label to process
576 * @kernel_addr: String containing kernel address (cannot be NULL)
577 * @initrd_addr_str: String containing initrd address (NULL if none)
578 * @initrd_filesize: String containing initrd size (only used if
579 * @initrd_addr_str)
580 * @initrd_str: initrd string to process (only used if @initrd_addr_str)
581 * Return: does not return on success, or returns 0 if the boot command
582 * returned, or -ve error value on error
583 */
584static int label_run_boot(struct pxe_context *ctx, struct pxe_label *label,
585 char *kernel_addr, char *initrd_addr_str,
586 char *initrd_filesize, char *initrd_str)
587{
Simon Glassb3beb6f2025-03-05 17:25:12 -0700588 struct bootm_info bmi;
Simon Glass09786df2025-03-05 17:25:05 -0700589 ulong kernel_addr_r;
Simon Glass09786df2025-03-05 17:25:05 -0700590 void *buf;
591 int ret;
592
Simon Glassb3beb6f2025-03-05 17:25:12 -0700593 bootm_init(&bmi);
Simon Glass09786df2025-03-05 17:25:05 -0700594
Simon Glassb3beb6f2025-03-05 17:25:12 -0700595 bmi.conf_fdt = env_get("fdt_addr_r");
596
597 ret = label_process_fdt(ctx, label, kernel_addr, &bmi.conf_fdt);
Simon Glass09786df2025-03-05 17:25:05 -0700598 if (ret)
599 return ret;
600
Simon Glassb3beb6f2025-03-05 17:25:12 -0700601 bmi.addr_img = kernel_addr;
Simon Glass92beb2432025-03-05 17:25:14 -0700602 bootm_x86_set(&bmi, bzimage_addr, hextoul(kernel_addr, NULL));
Tom Rini793921e2024-04-18 08:29:35 -0600603
604 if (initrd_addr_str) {
Simon Glassb3beb6f2025-03-05 17:25:12 -0700605 bmi.conf_ramdisk = initrd_str;
Simon Glass92beb2432025-03-05 17:25:14 -0700606 bootm_x86_set(&bmi, initrd_addr,
607 hextoul(initrd_addr_str, NULL));
608 bootm_x86_set(&bmi, initrd_size,
609 hextoul(initrd_filesize, NULL));
Tom Rini793921e2024-04-18 08:29:35 -0600610 }
611
Simon Glassb3beb6f2025-03-05 17:25:12 -0700612 if (!bmi.conf_fdt) {
Simon Glass60acbbc2025-03-05 17:25:13 -0700613 if (!IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS) ||
614 strcmp("-", label->fdt))
Simon Glassb3beb6f2025-03-05 17:25:12 -0700615 bmi.conf_fdt = env_get("fdt_addr");
Tom Rini793921e2024-04-18 08:29:35 -0600616 }
617
618 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
619 buf = map_sysmem(kernel_addr_r, 0);
620
Simon Glassb3beb6f2025-03-05 17:25:12 -0700621 if (!bmi.conf_fdt && genimg_get_format(buf) != IMAGE_FORMAT_FIT) {
Simon Glass60acbbc2025-03-05 17:25:13 -0700622 if (!IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS) ||
623 strcmp("-", label->fdt))
Simon Glassb3beb6f2025-03-05 17:25:12 -0700624 bmi.conf_fdt = env_get("fdtcontroladdr");
Tom Rini793921e2024-04-18 08:29:35 -0600625 }
626
Tom Rini793921e2024-04-18 08:29:35 -0600627 /* Try bootm for legacy and FIT format image */
628 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
Simon Glass9b2c78c2024-06-19 06:34:50 -0600629 IS_ENABLED(CONFIG_CMD_BOOTM)) {
630 log_debug("using bootm\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700631 ret = bootm_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600632 /* Try booting an AArch64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600633 } else if (IS_ENABLED(CONFIG_CMD_BOOTI)) {
634 log_debug("using booti\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700635 ret = booti_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600636 /* Try booting a Image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600637 } else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) {
638 log_debug("using bootz\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700639 ret = bootz_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600640 /* Try booting an x86_64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600641 } else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) {
642 log_debug("using zboot\n");
Simon Glass92beb2432025-03-05 17:25:14 -0700643 ret = zboot_run(&bmi);
Simon Glass9b2c78c2024-06-19 06:34:50 -0600644 }
Tom Rini793921e2024-04-18 08:29:35 -0600645
646 unmap_sysmem(buf);
Simon Glassb3beb6f2025-03-05 17:25:12 -0700647 if (ret)
648 return ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100649
Simon Glass827cd9c2025-03-05 17:25:04 -0700650 return 0;
651}
652
653/**
654 * label_boot() - Boot according to the contents of a pxe_label
655 *
656 * If we can't boot for any reason, we return. A successful boot never
657 * returns.
658 *
659 * The kernel will be stored in the location given by the 'kernel_addr_r'
660 * environment variable.
661 *
662 * If the label specifies an initrd file, it will be stored in the location
663 * given by the 'ramdisk_addr_r' environment variable.
664 *
665 * If the label specifies an 'append' line, its contents will overwrite that
666 * of the 'bootargs' environment variable.
667 *
668 * @ctx: PXE context
669 * @label: Label to process
670 * Returns does not return on success, otherwise returns 0 if a localboot
671 * label was processed, or 1 on error
672 */
673static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
674{
675 char *kernel_addr = NULL;
676 char *initrd_addr_str = NULL;
677 char initrd_filesize[10];
678 char initrd_str[28];
679 char mac_str[29] = "";
680 char ip_str[68] = "";
681 char *fit_addr = NULL;
682
683 label_print(label);
684
685 label->attempted = 1;
686
687 if (label->localboot) {
688 if (label->localboot_val >= 0)
689 label_localboot(label);
690 return 0;
691 }
692
693 if (!label->kernel) {
694 printf("No kernel given, skipping %s\n",
695 label->name);
696 return 1;
697 }
698
699 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
700 (enum bootflow_img_t)IH_TYPE_KERNEL, NULL)
701 < 0) {
702 printf("Skipping %s for failure retrieving kernel\n",
703 label->name);
704 return 1;
705 }
706
707 kernel_addr = env_get("kernel_addr_r");
708 /* for FIT, append the configuration identifier */
709 if (label->config) {
710 int len = strlen(kernel_addr) + strlen(label->config) + 1;
711
712 fit_addr = malloc(len);
713 if (!fit_addr) {
714 printf("malloc fail (FIT address)\n");
715 return 1;
716 }
717 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
718 kernel_addr = fit_addr;
719 }
720
721 /* For FIT, the label can be identical to kernel one */
722 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
723 initrd_addr_str = kernel_addr;
724 } else if (label->initrd) {
725 ulong size;
726 int ret;
727
728 ret = get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
729 (enum bootflow_img_t)IH_TYPE_RAMDISK,
730 &size);
731 if (ret < 0) {
732 printf("Skipping %s for failure retrieving initrd\n",
733 label->name);
734 goto cleanup;
735 }
736 strcpy(initrd_filesize, simple_xtoa(size));
737 initrd_addr_str = env_get("ramdisk_addr_r");
738 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
739 initrd_addr_str, size);
740 if (size >= sizeof(initrd_str))
741 goto cleanup;
742 }
743
744 if (label->ipappend & 0x1) {
745 sprintf(ip_str, " ip=%s:%s:%s:%s",
746 env_get("ipaddr"), env_get("serverip"),
747 env_get("gatewayip"), env_get("netmask"));
748 }
749
750 if (IS_ENABLED(CONFIG_CMD_NET)) {
751 if (label->ipappend & 0x2) {
752 int err;
753
754 strcpy(mac_str, " BOOTIF=");
755 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
756 if (err < 0)
757 mac_str[0] = '\0';
758 }
759 }
760
761 if ((label->ipappend & 0x3) || label->append) {
762 char bootargs[CONFIG_SYS_CBSIZE] = "";
763 char finalbootargs[CONFIG_SYS_CBSIZE];
764
765 if (strlen(label->append ?: "") +
766 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
767 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
768 strlen(label->append ?: ""),
769 strlen(ip_str), strlen(mac_str),
770 sizeof(bootargs));
771 goto cleanup;
772 }
773
774 if (label->append)
775 strlcpy(bootargs, label->append, sizeof(bootargs));
776
777 strcat(bootargs, ip_str);
778 strcat(bootargs, mac_str);
779
780 cli_simple_process_macros(bootargs, finalbootargs,
781 sizeof(finalbootargs));
782 env_set("bootargs", finalbootargs);
783 printf("append: %s\n", finalbootargs);
784 }
785
786 label_run_boot(ctx, label, kernel_addr, initrd_addr_str,
787 initrd_filesize, initrd_str);
788 /* ignore the error value since we are going to fail anyway */
789
Patrice Chotard17e88042019-11-25 09:07:37 +0100790cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600791 free(fit_addr);
792
Simon Glass827cd9c2025-03-05 17:25:04 -0700793 return 1; /* returning is always failure */
Patrice Chotard17e88042019-11-25 09:07:37 +0100794}
795
Simon Glass00302442021-10-14 12:48:01 -0600796/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100797enum token_type {
798 T_EOL,
799 T_STRING,
800 T_EOF,
801 T_MENU,
802 T_TITLE,
803 T_TIMEOUT,
804 T_LABEL,
805 T_KERNEL,
806 T_LINUX,
807 T_APPEND,
808 T_INITRD,
809 T_LOCALBOOT,
810 T_DEFAULT,
811 T_PROMPT,
812 T_INCLUDE,
813 T_FDT,
814 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100815 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100816 T_ONTIMEOUT,
817 T_IPAPPEND,
818 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800819 T_KASLRSEED,
Martyn Welch53dd5102024-10-09 14:15:38 +0100820 T_FALLBACK,
Patrice Chotard17e88042019-11-25 09:07:37 +0100821 T_INVALID
822};
823
Simon Glass00302442021-10-14 12:48:01 -0600824/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100825struct token {
826 char *val;
827 enum token_type type;
828};
829
Simon Glass00302442021-10-14 12:48:01 -0600830/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100831static const struct token keywords[] = {
832 {"menu", T_MENU},
833 {"title", T_TITLE},
834 {"timeout", T_TIMEOUT},
835 {"default", T_DEFAULT},
836 {"prompt", T_PROMPT},
837 {"label", T_LABEL},
838 {"kernel", T_KERNEL},
839 {"linux", T_LINUX},
840 {"localboot", T_LOCALBOOT},
841 {"append", T_APPEND},
842 {"initrd", T_INITRD},
843 {"include", T_INCLUDE},
844 {"devicetree", T_FDT},
845 {"fdt", T_FDT},
846 {"devicetreedir", T_FDTDIR},
847 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100848 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200849 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100850 {"ontimeout", T_ONTIMEOUT,},
851 {"ipappend", T_IPAPPEND,},
852 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800853 {"kaslrseed", T_KASLRSEED,},
Martyn Welch53dd5102024-10-09 14:15:38 +0100854 {"fallback", T_FALLBACK,},
Patrice Chotard17e88042019-11-25 09:07:37 +0100855 {NULL, T_INVALID}
856};
857
Simon Glass00302442021-10-14 12:48:01 -0600858/**
859 * enum lex_state - lexer state
860 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100861 * Since pxe(linux) files don't have a token to identify the start of a
862 * literal, we have to keep track of when we're in a state where a literal is
863 * expected vs when we're in a state a keyword is expected.
864 */
865enum lex_state {
866 L_NORMAL = 0,
867 L_KEYWORD,
868 L_SLITERAL
869};
870
Simon Glass00302442021-10-14 12:48:01 -0600871/**
872 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100873 *
Simon Glass00302442021-10-14 12:48:01 -0600874 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100875 *
876 * Characters from *p are copied into t-val until a character equal to
877 * delim is found, or a NUL byte is reached. If delim has the special value of
878 * ' ', any whitespace character will be used as a delimiter.
879 *
880 * If lower is unequal to 0, uppercase characters will be converted to
881 * lowercase in the result. This is useful to make keywords case
882 * insensitive.
883 *
884 * The location of *p is updated to point to the first character after the end
885 * of the token - the ending delimiter.
886 *
Simon Glass00302442021-10-14 12:48:01 -0600887 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
888 * it.
889 *
890 * @p: Points to a pointer to the current position in the input being processed.
891 * Updated to point at the first character after the current token
892 * @t: Pointers to a token to fill in
893 * @delim: Delimiter character to look for, either newline or space
894 * @lower: true to convert the string to lower case when storing
895 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100896 */
897static char *get_string(char **p, struct token *t, char delim, int lower)
898{
899 char *b, *e;
900 size_t len, i;
901
902 /*
903 * b and e both start at the beginning of the input stream.
904 *
905 * e is incremented until we find the ending delimiter, or a NUL byte
906 * is reached. Then, we take e - b to find the length of the token.
907 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100908 b = *p;
909 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100910 while (*e) {
911 if ((delim == ' ' && isspace(*e)) || delim == *e)
912 break;
913 e++;
914 }
915
916 len = e - b;
917
918 /*
919 * Allocate memory to hold the string, and copy it in, converting
920 * characters to lowercase if lower is != 0.
921 */
922 t->val = malloc(len + 1);
923 if (!t->val)
924 return NULL;
925
926 for (i = 0; i < len; i++, b++) {
927 if (lower)
928 t->val[i] = tolower(*b);
929 else
930 t->val[i] = *b;
931 }
932
933 t->val[len] = '\0';
934
Simon Glass764d0c02021-10-14 12:48:02 -0600935 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100936 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100937 t->type = T_STRING;
938
939 return t->val;
940}
941
Simon Glass00302442021-10-14 12:48:01 -0600942/**
943 * get_keyword() - Populate a keyword token with a type and value
944 *
945 * Updates the ->type field based on the keyword string in @val
946 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100947 */
948static void get_keyword(struct token *t)
949{
950 int i;
951
952 for (i = 0; keywords[i].val; i++) {
953 if (!strcmp(t->val, keywords[i].val)) {
954 t->type = keywords[i].type;
955 break;
956 }
957 }
958}
959
Simon Glass00302442021-10-14 12:48:01 -0600960/**
961 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100962 *
Simon Glass00302442021-10-14 12:48:01 -0600963 * We have to keep track of which state we're in to know if we're looking to get
964 * a string literal or a keyword.
965 *
966 * @p: Points to a pointer to the current position in the input being processed.
967 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100968 */
969static void get_token(char **p, struct token *t, enum lex_state state)
970{
971 char *c = *p;
972
973 t->type = T_INVALID;
974
975 /* eat non EOL whitespace */
976 while (isblank(*c))
977 c++;
978
979 /*
980 * eat comments. note that string literals can't begin with #, but
981 * can contain a # after their first character.
982 */
983 if (*c == '#') {
984 while (*c && *c != '\n')
985 c++;
986 }
987
988 if (*c == '\n') {
989 t->type = T_EOL;
990 c++;
991 } else if (*c == '\0') {
992 t->type = T_EOF;
993 c++;
994 } else if (state == L_SLITERAL) {
995 get_string(&c, t, '\n', 0);
996 } else if (state == L_KEYWORD) {
997 /*
998 * when we expect a keyword, we first get the next string
999 * token delimited by whitespace, and then check if it
1000 * matches a keyword in our keyword list. if it does, it's
1001 * converted to a keyword token of the appropriate type, and
1002 * if not, it remains a string token.
1003 */
1004 get_string(&c, t, ' ', 1);
1005 get_keyword(t);
1006 }
1007
1008 *p = c;
1009}
1010
Simon Glass00302442021-10-14 12:48:01 -06001011/**
1012 * eol_or_eof() - Find end of line
1013 *
1014 * Increment *c until we get to the end of the current line, or EOF
1015 *
1016 * @c: Points to a pointer to the current position in the input being processed.
1017 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +01001018 */
1019static void eol_or_eof(char **c)
1020{
1021 while (**c && **c != '\n')
1022 (*c)++;
1023}
1024
1025/*
1026 * All of these parse_* functions share some common behavior.
1027 *
1028 * They finish with *c pointing after the token they parse, and return 1 on
1029 * success, or < 0 on error.
1030 */
1031
1032/*
1033 * Parse a string literal and store a pointer it at *dst. String literals
1034 * terminate at the end of the line.
1035 */
1036static int parse_sliteral(char **c, char **dst)
1037{
1038 struct token t;
1039 char *s = *c;
1040
1041 get_token(c, &t, L_SLITERAL);
1042
1043 if (t.type != T_STRING) {
1044 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1045 return -EINVAL;
1046 }
1047
1048 *dst = t.val;
1049
1050 return 1;
1051}
1052
1053/*
1054 * Parse a base 10 (unsigned) integer and store it at *dst.
1055 */
1056static int parse_integer(char **c, int *dst)
1057{
1058 struct token t;
1059 char *s = *c;
1060
1061 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001062 if (t.type != T_STRING) {
1063 printf("Expected string: %.*s\n", (int)(*c - s), s);
1064 return -EINVAL;
1065 }
1066
1067 *dst = simple_strtol(t.val, NULL, 10);
1068
1069 free(t.val);
1070
1071 return 1;
1072}
1073
Simon Glassb0d08db2021-10-14 12:47:56 -06001074static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001075 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001076
1077/*
1078 * Parse an include statement, and retrieve and parse the file it mentions.
1079 *
1080 * base should point to a location where it's safe to store the file, and
1081 * nest_level should indicate how many nested includes have occurred. For this
1082 * include, nest_level has already been incremented and doesn't need to be
1083 * incremented here.
1084 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001085static int handle_include(struct pxe_context *ctx, char **c, unsigned long 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 char *include_path;
1089 char *s = *c;
1090 int err;
1091 char *buf;
1092 int ret;
1093
1094 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001095 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001096 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001097 return err;
1098 }
1099
Simon Glassb0d08db2021-10-14 12:47:56 -06001100 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001101 if (err < 0) {
1102 printf("Couldn't retrieve %s\n", include_path);
1103 return err;
1104 }
1105
1106 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001107 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001108 unmap_sysmem(buf);
1109
1110 return ret;
1111}
1112
1113/*
1114 * Parse lines that begin with 'menu'.
1115 *
1116 * base and nest are provided to handle the 'menu include' case.
1117 *
1118 * base should point to a location where it's safe to store the included file.
1119 *
1120 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1121 * a file it includes, 3 when parsing a file included by that file, and so on.
1122 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001123static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001124 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001125{
1126 struct token t;
1127 char *s = *c;
1128 int err = 0;
1129
1130 get_token(c, &t, L_KEYWORD);
1131
1132 switch (t.type) {
1133 case T_TITLE:
1134 err = parse_sliteral(c, &cfg->title);
1135
1136 break;
1137
1138 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001139 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001140 break;
1141
1142 case T_BACKGROUND:
1143 err = parse_sliteral(c, &cfg->bmp);
1144 break;
1145
1146 default:
1147 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001148 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001149 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001150 if (err < 0)
1151 return err;
1152
1153 eol_or_eof(c);
1154
1155 return 1;
1156}
1157
1158/*
1159 * Handles parsing a 'menu line' when we're parsing a label.
1160 */
1161static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001162 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001163{
1164 struct token t;
1165 char *s;
1166
1167 s = *c;
1168
1169 get_token(c, &t, L_KEYWORD);
1170
1171 switch (t.type) {
1172 case T_DEFAULT:
1173 if (!cfg->default_label)
1174 cfg->default_label = strdup(label->name);
1175
1176 if (!cfg->default_label)
1177 return -ENOMEM;
1178
1179 break;
1180 case T_LABEL:
1181 parse_sliteral(c, &label->menu);
1182 break;
1183 default:
1184 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001185 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001186 }
1187
1188 eol_or_eof(c);
1189
1190 return 0;
1191}
1192
1193/*
1194 * Handles parsing a 'kernel' label.
1195 * expecting "filename" or "<fit_filename>#cfg"
1196 */
1197static int parse_label_kernel(char **c, struct pxe_label *label)
1198{
1199 char *s;
1200 int err;
1201
1202 err = parse_sliteral(c, &label->kernel);
1203 if (err < 0)
1204 return err;
1205
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +02001206 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1207 label->kernel_label = strdup(label->kernel);
1208 if (!label->kernel_label)
1209 return -ENOMEM;
1210
Patrice Chotard17e88042019-11-25 09:07:37 +01001211 s = strstr(label->kernel, "#");
1212 if (!s)
1213 return 1;
1214
Patrick Delaunay9d464f82022-10-28 11:01:20 +02001215 label->config = strdup(s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001216 if (!label->config)
1217 return -ENOMEM;
1218
Patrice Chotard17e88042019-11-25 09:07:37 +01001219 *s = 0;
1220
1221 return 1;
1222}
1223
1224/*
1225 * Parses a label and adds it to the list of labels for a menu.
1226 *
1227 * A label ends when we either get to the end of a file, or
1228 * get some input we otherwise don't have a handler defined
1229 * for.
1230 *
1231 */
1232static int parse_label(char **c, struct pxe_menu *cfg)
1233{
1234 struct token t;
1235 int len;
1236 char *s = *c;
1237 struct pxe_label *label;
1238 int err;
1239
1240 label = label_create();
1241 if (!label)
1242 return -ENOMEM;
1243
1244 err = parse_sliteral(c, &label->name);
1245 if (err < 0) {
1246 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1247 label_destroy(label);
1248 return -EINVAL;
1249 }
1250
1251 list_add_tail(&label->list, &cfg->labels);
1252
1253 while (1) {
1254 s = *c;
1255 get_token(c, &t, L_KEYWORD);
1256
1257 err = 0;
1258 switch (t.type) {
1259 case T_MENU:
1260 err = parse_label_menu(c, cfg, label);
1261 break;
1262
1263 case T_KERNEL:
1264 case T_LINUX:
1265 err = parse_label_kernel(c, label);
1266 break;
1267
1268 case T_APPEND:
1269 err = parse_sliteral(c, &label->append);
1270 if (label->initrd)
1271 break;
1272 s = strstr(label->append, "initrd=");
1273 if (!s)
1274 break;
1275 s += 7;
1276 len = (int)(strchr(s, ' ') - s);
1277 label->initrd = malloc(len + 1);
1278 strncpy(label->initrd, s, len);
1279 label->initrd[len] = '\0';
1280
1281 break;
1282
1283 case T_INITRD:
1284 if (!label->initrd)
1285 err = parse_sliteral(c, &label->initrd);
1286 break;
1287
1288 case T_FDT:
1289 if (!label->fdt)
1290 err = parse_sliteral(c, &label->fdt);
1291 break;
1292
1293 case T_FDTDIR:
1294 if (!label->fdtdir)
1295 err = parse_sliteral(c, &label->fdtdir);
1296 break;
1297
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001298 case T_FDTOVERLAYS:
1299 if (!label->fdtoverlays)
1300 err = parse_sliteral(c, &label->fdtoverlays);
1301 break;
1302
Patrice Chotard17e88042019-11-25 09:07:37 +01001303 case T_LOCALBOOT:
1304 label->localboot = 1;
1305 err = parse_integer(c, &label->localboot_val);
1306 break;
1307
1308 case T_IPAPPEND:
1309 err = parse_integer(c, &label->ipappend);
1310 break;
1311
Zhang Ning9c1d9c52022-02-01 08:33:37 +08001312 case T_KASLRSEED:
1313 label->kaslrseed = 1;
1314 break;
1315
Patrice Chotard17e88042019-11-25 09:07:37 +01001316 case T_EOL:
1317 break;
1318
1319 default:
1320 /*
1321 * put the token back! we don't want it - it's the end
1322 * of a label and whatever token this is, it's
1323 * something for the menu level context to handle.
1324 */
1325 *c = s;
1326 return 1;
1327 }
1328
1329 if (err < 0)
1330 return err;
1331 }
1332}
1333
1334/*
1335 * This 16 comes from the limit pxelinux imposes on nested includes.
1336 *
1337 * There is no reason at all we couldn't do more, but some limit helps prevent
1338 * infinite (until crash occurs) recursion if a file tries to include itself.
1339 */
1340#define MAX_NEST_LEVEL 16
1341
1342/*
1343 * Entry point for parsing a menu file. nest_level indicates how many times
1344 * we've nested in includes. It will be 1 for the top level menu file.
1345 *
1346 * Returns 1 on success, < 0 on error.
1347 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001348static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001349 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001350{
1351 struct token t;
1352 char *s, *b, *label_name;
1353 int err;
1354
1355 b = p;
1356
1357 if (nest_level > MAX_NEST_LEVEL) {
1358 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1359 return -EMLINK;
1360 }
1361
1362 while (1) {
1363 s = p;
1364
1365 get_token(&p, &t, L_KEYWORD);
1366
1367 err = 0;
1368 switch (t.type) {
1369 case T_MENU:
1370 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001371 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001372 base + ALIGN(strlen(b) + 1, 4),
1373 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001374 break;
1375
1376 case T_TIMEOUT:
1377 err = parse_integer(&p, &cfg->timeout);
1378 break;
1379
1380 case T_LABEL:
1381 err = parse_label(&p, cfg);
1382 break;
1383
1384 case T_DEFAULT:
1385 case T_ONTIMEOUT:
1386 err = parse_sliteral(&p, &label_name);
1387
1388 if (label_name) {
1389 if (cfg->default_label)
1390 free(cfg->default_label);
1391
1392 cfg->default_label = label_name;
1393 }
1394
1395 break;
1396
Martyn Welch53dd5102024-10-09 14:15:38 +01001397 case T_FALLBACK:
1398 err = parse_sliteral(&p, &label_name);
1399
1400 if (label_name) {
1401 if (cfg->fallback_label)
1402 free(cfg->fallback_label);
1403
1404 cfg->fallback_label = label_name;
1405 }
1406
1407 break;
1408
Patrice Chotard17e88042019-11-25 09:07:37 +01001409 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001410 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001411 base + ALIGN(strlen(b), 4), cfg,
1412 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001413 break;
1414
1415 case T_PROMPT:
Manuel Traut20081242022-11-18 09:00:27 +01001416 err = parse_integer(&p, &cfg->prompt);
1417 // Do not fail if prompt configuration is undefined
1418 if (err < 0)
1419 eol_or_eof(&p);
Patrice Chotard17e88042019-11-25 09:07:37 +01001420 break;
1421
1422 case T_EOL:
1423 break;
1424
1425 case T_EOF:
1426 return 1;
1427
1428 default:
1429 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001430 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001431 eol_or_eof(&p);
1432 }
1433
1434 if (err < 0)
1435 return err;
1436 }
1437}
1438
1439/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001440 */
1441void destroy_pxe_menu(struct pxe_menu *cfg)
1442{
1443 struct list_head *pos, *n;
1444 struct pxe_label *label;
1445
Simon Glass764d0c02021-10-14 12:48:02 -06001446 free(cfg->title);
1447 free(cfg->default_label);
Martyn Welch53dd5102024-10-09 14:15:38 +01001448 free(cfg->fallback_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001449
1450 list_for_each_safe(pos, n, &cfg->labels) {
1451 label = list_entry(pos, struct pxe_label, list);
1452
1453 label_destroy(label);
1454 }
1455
1456 free(cfg);
1457}
1458
Simon Glassb0d08db2021-10-14 12:47:56 -06001459struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001460{
1461 struct pxe_menu *cfg;
1462 char *buf;
1463 int r;
1464
1465 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001466 if (!cfg)
1467 return NULL;
1468
1469 memset(cfg, 0, sizeof(struct pxe_menu));
1470
1471 INIT_LIST_HEAD(&cfg->labels);
1472
1473 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001474 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Martyn Welch2c47aac2024-10-09 14:15:39 +01001475
1476 if (ctx->use_fallback) {
1477 if (cfg->fallback_label) {
1478 printf("Setting use of fallback\n");
1479 cfg->default_label = cfg->fallback_label;
1480 } else {
1481 printf("Selected fallback option, but not set\n");
1482 }
1483 }
1484
Patrice Chotard17e88042019-11-25 09:07:37 +01001485 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001486 if (r < 0) {
1487 destroy_pxe_menu(cfg);
1488 return NULL;
1489 }
1490
1491 return cfg;
1492}
1493
1494/*
1495 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1496 * menu code.
1497 */
1498static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1499{
1500 struct pxe_label *label;
1501 struct list_head *pos;
1502 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001503 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001504 int err;
1505 int i = 1;
1506 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001507 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001508
1509 /*
1510 * Create a menu and add items for all the labels.
1511 */
1512 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
developerc2a1e9e2024-10-29 17:47:16 +08001513 cfg->prompt, NULL, label_print, NULL, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001514 if (!m)
1515 return NULL;
1516
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001517 label_override = env_get("pxe_label_override");
1518
Patrice Chotard17e88042019-11-25 09:07:37 +01001519 list_for_each(pos, &cfg->labels) {
1520 label = list_entry(pos, struct pxe_label, list);
1521
1522 sprintf(label->num, "%d", i++);
1523 if (menu_item_add(m, label->num, label) != 1) {
1524 menu_destroy(m);
1525 return NULL;
1526 }
1527 if (cfg->default_label &&
1528 (strcmp(label->name, cfg->default_label) == 0))
1529 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001530 if (label_override && !strcmp(label->name, label_override))
1531 override_num = label->num;
1532 }
1533
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001534 if (label_override) {
1535 if (override_num)
1536 default_num = override_num;
1537 else
1538 printf("Missing override pxe label: %s\n",
1539 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001540 }
1541
1542 /*
1543 * After we've created items for each label in the menu, set the
1544 * menu's default label if one was specified.
1545 */
1546 if (default_num) {
1547 err = menu_default_set(m, default_num);
1548 if (err != 1) {
1549 if (err != -ENOENT) {
1550 menu_destroy(m);
1551 return NULL;
1552 }
1553
1554 printf("Missing default: %s\n", cfg->default_label);
1555 }
1556 }
1557
1558 return m;
1559}
1560
1561/*
1562 * Try to boot any labels we have yet to attempt to boot.
1563 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001564static void boot_unattempted_labels(struct pxe_context *ctx,
1565 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001566{
1567 struct list_head *pos;
1568 struct pxe_label *label;
1569
1570 list_for_each(pos, &cfg->labels) {
1571 label = list_entry(pos, struct pxe_label, list);
1572
1573 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001574 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001575 }
1576}
1577
Simon Glassb0d08db2021-10-14 12:47:56 -06001578void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001579{
1580 void *choice;
1581 struct menu *m;
1582 int err;
1583
Kory Maincentcaabd242021-02-02 16:42:28 +01001584 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1585 /* display BMP if available */
1586 if (cfg->bmp) {
Simon Glassa686ba62024-11-15 16:19:19 -07001587 if (get_relfile(ctx, cfg->bmp, image_load_addr,
1588 BFI_LOGO, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001589#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001590 struct udevice *dev;
1591
1592 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1593 if (!err)
1594 video_clear(dev);
1595#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001596 bmp_display(image_load_addr,
1597 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1598 } else {
1599 printf("Skipping background bmp %s for failure\n",
1600 cfg->bmp);
1601 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001602 }
1603 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001604
1605 m = pxe_menu_to_menu(cfg);
1606 if (!m)
1607 return;
1608
1609 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001610 menu_destroy(m);
1611
1612 /*
1613 * err == 1 means we got a choice back from menu_get_choice.
1614 *
1615 * err == -ENOENT if the menu was setup to select the default but no
1616 * default was set. in that case, we should continue trying to boot
1617 * labels that haven't been attempted yet.
1618 *
1619 * otherwise, the user interrupted or there was some other error and
1620 * we give up.
1621 */
1622
1623 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001624 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001625 if (!err)
1626 return;
1627 } else if (err != -ENOENT) {
1628 return;
1629 }
1630
Simon Glassb0d08db2021-10-14 12:47:56 -06001631 boot_unattempted_labels(ctx, cfg);
1632}
1633
Simon Glasse719fe02021-10-14 12:48:04 -06001634int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1635 pxe_getfile_func getfile, void *userdata,
Martyn Welch2c47aac2024-10-09 14:15:39 +01001636 bool allow_abs_path, const char *bootfile, bool use_ipv6,
1637 bool use_fallback)
Simon Glassb0d08db2021-10-14 12:47:56 -06001638{
Simon Glasse719fe02021-10-14 12:48:04 -06001639 const char *last_slash;
1640 size_t path_len = 0;
1641
1642 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001643 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001644 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001645 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001646 ctx->allow_abs_path = allow_abs_path;
Sean Edmondba802862023-04-11 10:48:47 -07001647 ctx->use_ipv6 = use_ipv6;
Martyn Welch2c47aac2024-10-09 14:15:39 +01001648 ctx->use_fallback = use_fallback;
Simon Glasse719fe02021-10-14 12:48:04 -06001649
1650 /* figure out the boot directory, if there is one */
1651 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1652 return -ENOSPC;
1653 ctx->bootdir = strdup(bootfile ? bootfile : "");
1654 if (!ctx->bootdir)
1655 return -ENOMEM;
1656
1657 if (bootfile) {
1658 last_slash = strrchr(bootfile, '/');
1659 if (last_slash)
1660 path_len = (last_slash - bootfile) + 1;
1661 }
1662 ctx->bootdir[path_len] = '\0';
1663
1664 return 0;
1665}
1666
1667void pxe_destroy_ctx(struct pxe_context *ctx)
1668{
1669 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001670}
Simon Glass791bbfe2021-10-14 12:48:03 -06001671
1672int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1673{
1674 struct pxe_menu *cfg;
1675
1676 cfg = parse_pxefile(ctx, pxefile_addr_r);
1677 if (!cfg) {
1678 printf("Error parsing config file\n");
1679 return 1;
1680 }
1681
1682 if (prompt)
1683 cfg->prompt = 1;
1684
1685 handle_pxe_menu(ctx, cfg);
1686
1687 destroy_pxe_menu(cfg);
1688
1689 return 0;
1690}