blob: bbb6ff203b617c9697ee87dbb850c775df4d8b3b [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 Glass09786df2025-03-05 17:25:05 -0700588 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Simon Glassb3beb6f2025-03-05 17:25:12 -0700589 struct bootm_info bmi;
Simon Glass09786df2025-03-05 17:25:05 -0700590 ulong kernel_addr_r;
Simon Glass09786df2025-03-05 17:25:05 -0700591 int zboot_argc = 3;
592 void *buf;
593 int ret;
594
Simon Glassb3beb6f2025-03-05 17:25:12 -0700595 bootm_init(&bmi);
Simon Glass09786df2025-03-05 17:25:05 -0700596
Simon Glassb3beb6f2025-03-05 17:25:12 -0700597 bmi.conf_fdt = env_get("fdt_addr_r");
598
599 ret = label_process_fdt(ctx, label, kernel_addr, &bmi.conf_fdt);
Simon Glass09786df2025-03-05 17:25:05 -0700600 if (ret)
601 return ret;
602
Simon Glassb3beb6f2025-03-05 17:25:12 -0700603 bmi.addr_img = kernel_addr;
Tom Rini793921e2024-04-18 08:29:35 -0600604 zboot_argv[1] = kernel_addr;
605
606 if (initrd_addr_str) {
Simon Glassb3beb6f2025-03-05 17:25:12 -0700607 bmi.conf_ramdisk = initrd_str;
Tom Rini793921e2024-04-18 08:29:35 -0600608
609 zboot_argv[3] = initrd_addr_str;
610 zboot_argv[4] = initrd_filesize;
611 zboot_argc = 5;
612 }
613
Simon Glassb3beb6f2025-03-05 17:25:12 -0700614 if (!bmi.conf_fdt) {
Tom Rini793921e2024-04-18 08:29:35 -0600615 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
616 if (strcmp("-", label->fdt))
Simon Glassb3beb6f2025-03-05 17:25:12 -0700617 bmi.conf_fdt = env_get("fdt_addr");
Tom Rini793921e2024-04-18 08:29:35 -0600618 } else {
Simon Glassb3beb6f2025-03-05 17:25:12 -0700619 bmi.conf_fdt = env_get("fdt_addr");
Tom Rini793921e2024-04-18 08:29:35 -0600620 }
621 }
622
623 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
624 buf = map_sysmem(kernel_addr_r, 0);
625
Simon Glassb3beb6f2025-03-05 17:25:12 -0700626 if (!bmi.conf_fdt && genimg_get_format(buf) != IMAGE_FORMAT_FIT) {
Tom Rini793921e2024-04-18 08:29:35 -0600627 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
628 if (strcmp("-", label->fdt))
Simon Glassb3beb6f2025-03-05 17:25:12 -0700629 bmi.conf_fdt = env_get("fdtcontroladdr");
Tom Rini793921e2024-04-18 08:29:35 -0600630 } else {
Simon Glassb3beb6f2025-03-05 17:25:12 -0700631 bmi.conf_fdt = env_get("fdtcontroladdr");
Tom Rini793921e2024-04-18 08:29:35 -0600632 }
633 }
634
Tom Rini793921e2024-04-18 08:29:35 -0600635 /* Try bootm for legacy and FIT format image */
636 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
Simon Glass9b2c78c2024-06-19 06:34:50 -0600637 IS_ENABLED(CONFIG_CMD_BOOTM)) {
638 log_debug("using bootm\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700639 ret = bootm_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600640 /* Try booting an AArch64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600641 } else if (IS_ENABLED(CONFIG_CMD_BOOTI)) {
642 log_debug("using booti\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700643 ret = booti_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600644 /* Try booting a Image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600645 } else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) {
646 log_debug("using bootz\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700647 ret = bootz_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600648 /* Try booting an x86_64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600649 } else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) {
650 log_debug("using zboot\n");
Tom Rini793921e2024-04-18 08:29:35 -0600651 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Simon Glass9b2c78c2024-06-19 06:34:50 -0600652 }
Tom Rini793921e2024-04-18 08:29:35 -0600653
654 unmap_sysmem(buf);
Simon Glassb3beb6f2025-03-05 17:25:12 -0700655 if (ret)
656 return ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100657
Simon Glass827cd9c2025-03-05 17:25:04 -0700658 return 0;
659}
660
661/**
662 * label_boot() - Boot according to the contents of a pxe_label
663 *
664 * If we can't boot for any reason, we return. A successful boot never
665 * returns.
666 *
667 * The kernel will be stored in the location given by the 'kernel_addr_r'
668 * environment variable.
669 *
670 * If the label specifies an initrd file, it will be stored in the location
671 * given by the 'ramdisk_addr_r' environment variable.
672 *
673 * If the label specifies an 'append' line, its contents will overwrite that
674 * of the 'bootargs' environment variable.
675 *
676 * @ctx: PXE context
677 * @label: Label to process
678 * Returns does not return on success, otherwise returns 0 if a localboot
679 * label was processed, or 1 on error
680 */
681static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
682{
683 char *kernel_addr = NULL;
684 char *initrd_addr_str = NULL;
685 char initrd_filesize[10];
686 char initrd_str[28];
687 char mac_str[29] = "";
688 char ip_str[68] = "";
689 char *fit_addr = NULL;
690
691 label_print(label);
692
693 label->attempted = 1;
694
695 if (label->localboot) {
696 if (label->localboot_val >= 0)
697 label_localboot(label);
698 return 0;
699 }
700
701 if (!label->kernel) {
702 printf("No kernel given, skipping %s\n",
703 label->name);
704 return 1;
705 }
706
707 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
708 (enum bootflow_img_t)IH_TYPE_KERNEL, NULL)
709 < 0) {
710 printf("Skipping %s for failure retrieving kernel\n",
711 label->name);
712 return 1;
713 }
714
715 kernel_addr = env_get("kernel_addr_r");
716 /* for FIT, append the configuration identifier */
717 if (label->config) {
718 int len = strlen(kernel_addr) + strlen(label->config) + 1;
719
720 fit_addr = malloc(len);
721 if (!fit_addr) {
722 printf("malloc fail (FIT address)\n");
723 return 1;
724 }
725 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
726 kernel_addr = fit_addr;
727 }
728
729 /* For FIT, the label can be identical to kernel one */
730 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
731 initrd_addr_str = kernel_addr;
732 } else if (label->initrd) {
733 ulong size;
734 int ret;
735
736 ret = get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
737 (enum bootflow_img_t)IH_TYPE_RAMDISK,
738 &size);
739 if (ret < 0) {
740 printf("Skipping %s for failure retrieving initrd\n",
741 label->name);
742 goto cleanup;
743 }
744 strcpy(initrd_filesize, simple_xtoa(size));
745 initrd_addr_str = env_get("ramdisk_addr_r");
746 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
747 initrd_addr_str, size);
748 if (size >= sizeof(initrd_str))
749 goto cleanup;
750 }
751
752 if (label->ipappend & 0x1) {
753 sprintf(ip_str, " ip=%s:%s:%s:%s",
754 env_get("ipaddr"), env_get("serverip"),
755 env_get("gatewayip"), env_get("netmask"));
756 }
757
758 if (IS_ENABLED(CONFIG_CMD_NET)) {
759 if (label->ipappend & 0x2) {
760 int err;
761
762 strcpy(mac_str, " BOOTIF=");
763 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
764 if (err < 0)
765 mac_str[0] = '\0';
766 }
767 }
768
769 if ((label->ipappend & 0x3) || label->append) {
770 char bootargs[CONFIG_SYS_CBSIZE] = "";
771 char finalbootargs[CONFIG_SYS_CBSIZE];
772
773 if (strlen(label->append ?: "") +
774 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
775 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
776 strlen(label->append ?: ""),
777 strlen(ip_str), strlen(mac_str),
778 sizeof(bootargs));
779 goto cleanup;
780 }
781
782 if (label->append)
783 strlcpy(bootargs, label->append, sizeof(bootargs));
784
785 strcat(bootargs, ip_str);
786 strcat(bootargs, mac_str);
787
788 cli_simple_process_macros(bootargs, finalbootargs,
789 sizeof(finalbootargs));
790 env_set("bootargs", finalbootargs);
791 printf("append: %s\n", finalbootargs);
792 }
793
794 label_run_boot(ctx, label, kernel_addr, initrd_addr_str,
795 initrd_filesize, initrd_str);
796 /* ignore the error value since we are going to fail anyway */
797
Patrice Chotard17e88042019-11-25 09:07:37 +0100798cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600799 free(fit_addr);
800
Simon Glass827cd9c2025-03-05 17:25:04 -0700801 return 1; /* returning is always failure */
Patrice Chotard17e88042019-11-25 09:07:37 +0100802}
803
Simon Glass00302442021-10-14 12:48:01 -0600804/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100805enum token_type {
806 T_EOL,
807 T_STRING,
808 T_EOF,
809 T_MENU,
810 T_TITLE,
811 T_TIMEOUT,
812 T_LABEL,
813 T_KERNEL,
814 T_LINUX,
815 T_APPEND,
816 T_INITRD,
817 T_LOCALBOOT,
818 T_DEFAULT,
819 T_PROMPT,
820 T_INCLUDE,
821 T_FDT,
822 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100823 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100824 T_ONTIMEOUT,
825 T_IPAPPEND,
826 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800827 T_KASLRSEED,
Martyn Welch53dd5102024-10-09 14:15:38 +0100828 T_FALLBACK,
Patrice Chotard17e88042019-11-25 09:07:37 +0100829 T_INVALID
830};
831
Simon Glass00302442021-10-14 12:48:01 -0600832/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100833struct token {
834 char *val;
835 enum token_type type;
836};
837
Simon Glass00302442021-10-14 12:48:01 -0600838/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100839static const struct token keywords[] = {
840 {"menu", T_MENU},
841 {"title", T_TITLE},
842 {"timeout", T_TIMEOUT},
843 {"default", T_DEFAULT},
844 {"prompt", T_PROMPT},
845 {"label", T_LABEL},
846 {"kernel", T_KERNEL},
847 {"linux", T_LINUX},
848 {"localboot", T_LOCALBOOT},
849 {"append", T_APPEND},
850 {"initrd", T_INITRD},
851 {"include", T_INCLUDE},
852 {"devicetree", T_FDT},
853 {"fdt", T_FDT},
854 {"devicetreedir", T_FDTDIR},
855 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100856 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200857 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100858 {"ontimeout", T_ONTIMEOUT,},
859 {"ipappend", T_IPAPPEND,},
860 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800861 {"kaslrseed", T_KASLRSEED,},
Martyn Welch53dd5102024-10-09 14:15:38 +0100862 {"fallback", T_FALLBACK,},
Patrice Chotard17e88042019-11-25 09:07:37 +0100863 {NULL, T_INVALID}
864};
865
Simon Glass00302442021-10-14 12:48:01 -0600866/**
867 * enum lex_state - lexer state
868 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100869 * Since pxe(linux) files don't have a token to identify the start of a
870 * literal, we have to keep track of when we're in a state where a literal is
871 * expected vs when we're in a state a keyword is expected.
872 */
873enum lex_state {
874 L_NORMAL = 0,
875 L_KEYWORD,
876 L_SLITERAL
877};
878
Simon Glass00302442021-10-14 12:48:01 -0600879/**
880 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100881 *
Simon Glass00302442021-10-14 12:48:01 -0600882 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100883 *
884 * Characters from *p are copied into t-val until a character equal to
885 * delim is found, or a NUL byte is reached. If delim has the special value of
886 * ' ', any whitespace character will be used as a delimiter.
887 *
888 * If lower is unequal to 0, uppercase characters will be converted to
889 * lowercase in the result. This is useful to make keywords case
890 * insensitive.
891 *
892 * The location of *p is updated to point to the first character after the end
893 * of the token - the ending delimiter.
894 *
Simon Glass00302442021-10-14 12:48:01 -0600895 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
896 * it.
897 *
898 * @p: Points to a pointer to the current position in the input being processed.
899 * Updated to point at the first character after the current token
900 * @t: Pointers to a token to fill in
901 * @delim: Delimiter character to look for, either newline or space
902 * @lower: true to convert the string to lower case when storing
903 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100904 */
905static char *get_string(char **p, struct token *t, char delim, int lower)
906{
907 char *b, *e;
908 size_t len, i;
909
910 /*
911 * b and e both start at the beginning of the input stream.
912 *
913 * e is incremented until we find the ending delimiter, or a NUL byte
914 * is reached. Then, we take e - b to find the length of the token.
915 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100916 b = *p;
917 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100918 while (*e) {
919 if ((delim == ' ' && isspace(*e)) || delim == *e)
920 break;
921 e++;
922 }
923
924 len = e - b;
925
926 /*
927 * Allocate memory to hold the string, and copy it in, converting
928 * characters to lowercase if lower is != 0.
929 */
930 t->val = malloc(len + 1);
931 if (!t->val)
932 return NULL;
933
934 for (i = 0; i < len; i++, b++) {
935 if (lower)
936 t->val[i] = tolower(*b);
937 else
938 t->val[i] = *b;
939 }
940
941 t->val[len] = '\0';
942
Simon Glass764d0c02021-10-14 12:48:02 -0600943 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100944 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100945 t->type = T_STRING;
946
947 return t->val;
948}
949
Simon Glass00302442021-10-14 12:48:01 -0600950/**
951 * get_keyword() - Populate a keyword token with a type and value
952 *
953 * Updates the ->type field based on the keyword string in @val
954 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100955 */
956static void get_keyword(struct token *t)
957{
958 int i;
959
960 for (i = 0; keywords[i].val; i++) {
961 if (!strcmp(t->val, keywords[i].val)) {
962 t->type = keywords[i].type;
963 break;
964 }
965 }
966}
967
Simon Glass00302442021-10-14 12:48:01 -0600968/**
969 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100970 *
Simon Glass00302442021-10-14 12:48:01 -0600971 * We have to keep track of which state we're in to know if we're looking to get
972 * a string literal or a keyword.
973 *
974 * @p: Points to a pointer to the current position in the input being processed.
975 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100976 */
977static void get_token(char **p, struct token *t, enum lex_state state)
978{
979 char *c = *p;
980
981 t->type = T_INVALID;
982
983 /* eat non EOL whitespace */
984 while (isblank(*c))
985 c++;
986
987 /*
988 * eat comments. note that string literals can't begin with #, but
989 * can contain a # after their first character.
990 */
991 if (*c == '#') {
992 while (*c && *c != '\n')
993 c++;
994 }
995
996 if (*c == '\n') {
997 t->type = T_EOL;
998 c++;
999 } else if (*c == '\0') {
1000 t->type = T_EOF;
1001 c++;
1002 } else if (state == L_SLITERAL) {
1003 get_string(&c, t, '\n', 0);
1004 } else if (state == L_KEYWORD) {
1005 /*
1006 * when we expect a keyword, we first get the next string
1007 * token delimited by whitespace, and then check if it
1008 * matches a keyword in our keyword list. if it does, it's
1009 * converted to a keyword token of the appropriate type, and
1010 * if not, it remains a string token.
1011 */
1012 get_string(&c, t, ' ', 1);
1013 get_keyword(t);
1014 }
1015
1016 *p = c;
1017}
1018
Simon Glass00302442021-10-14 12:48:01 -06001019/**
1020 * eol_or_eof() - Find end of line
1021 *
1022 * Increment *c until we get to the end of the current line, or EOF
1023 *
1024 * @c: Points to a pointer to the current position in the input being processed.
1025 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +01001026 */
1027static void eol_or_eof(char **c)
1028{
1029 while (**c && **c != '\n')
1030 (*c)++;
1031}
1032
1033/*
1034 * All of these parse_* functions share some common behavior.
1035 *
1036 * They finish with *c pointing after the token they parse, and return 1 on
1037 * success, or < 0 on error.
1038 */
1039
1040/*
1041 * Parse a string literal and store a pointer it at *dst. String literals
1042 * terminate at the end of the line.
1043 */
1044static int parse_sliteral(char **c, char **dst)
1045{
1046 struct token t;
1047 char *s = *c;
1048
1049 get_token(c, &t, L_SLITERAL);
1050
1051 if (t.type != T_STRING) {
1052 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1053 return -EINVAL;
1054 }
1055
1056 *dst = t.val;
1057
1058 return 1;
1059}
1060
1061/*
1062 * Parse a base 10 (unsigned) integer and store it at *dst.
1063 */
1064static int parse_integer(char **c, int *dst)
1065{
1066 struct token t;
1067 char *s = *c;
1068
1069 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001070 if (t.type != T_STRING) {
1071 printf("Expected string: %.*s\n", (int)(*c - s), s);
1072 return -EINVAL;
1073 }
1074
1075 *dst = simple_strtol(t.val, NULL, 10);
1076
1077 free(t.val);
1078
1079 return 1;
1080}
1081
Simon Glassb0d08db2021-10-14 12:47:56 -06001082static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001083 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001084
1085/*
1086 * Parse an include statement, and retrieve and parse the file it mentions.
1087 *
1088 * base should point to a location where it's safe to store the file, and
1089 * nest_level should indicate how many nested includes have occurred. For this
1090 * include, nest_level has already been incremented and doesn't need to be
1091 * incremented here.
1092 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001093static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001094 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001095{
1096 char *include_path;
1097 char *s = *c;
1098 int err;
1099 char *buf;
1100 int ret;
1101
1102 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001103 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001104 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001105 return err;
1106 }
1107
Simon Glassb0d08db2021-10-14 12:47:56 -06001108 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001109 if (err < 0) {
1110 printf("Couldn't retrieve %s\n", include_path);
1111 return err;
1112 }
1113
1114 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001115 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001116 unmap_sysmem(buf);
1117
1118 return ret;
1119}
1120
1121/*
1122 * Parse lines that begin with 'menu'.
1123 *
1124 * base and nest are provided to handle the 'menu include' case.
1125 *
1126 * base should point to a location where it's safe to store the included file.
1127 *
1128 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1129 * a file it includes, 3 when parsing a file included by that file, and so on.
1130 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001131static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001132 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001133{
1134 struct token t;
1135 char *s = *c;
1136 int err = 0;
1137
1138 get_token(c, &t, L_KEYWORD);
1139
1140 switch (t.type) {
1141 case T_TITLE:
1142 err = parse_sliteral(c, &cfg->title);
1143
1144 break;
1145
1146 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001147 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001148 break;
1149
1150 case T_BACKGROUND:
1151 err = parse_sliteral(c, &cfg->bmp);
1152 break;
1153
1154 default:
1155 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001156 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001157 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001158 if (err < 0)
1159 return err;
1160
1161 eol_or_eof(c);
1162
1163 return 1;
1164}
1165
1166/*
1167 * Handles parsing a 'menu line' when we're parsing a label.
1168 */
1169static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001170 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001171{
1172 struct token t;
1173 char *s;
1174
1175 s = *c;
1176
1177 get_token(c, &t, L_KEYWORD);
1178
1179 switch (t.type) {
1180 case T_DEFAULT:
1181 if (!cfg->default_label)
1182 cfg->default_label = strdup(label->name);
1183
1184 if (!cfg->default_label)
1185 return -ENOMEM;
1186
1187 break;
1188 case T_LABEL:
1189 parse_sliteral(c, &label->menu);
1190 break;
1191 default:
1192 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001193 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001194 }
1195
1196 eol_or_eof(c);
1197
1198 return 0;
1199}
1200
1201/*
1202 * Handles parsing a 'kernel' label.
1203 * expecting "filename" or "<fit_filename>#cfg"
1204 */
1205static int parse_label_kernel(char **c, struct pxe_label *label)
1206{
1207 char *s;
1208 int err;
1209
1210 err = parse_sliteral(c, &label->kernel);
1211 if (err < 0)
1212 return err;
1213
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +02001214 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1215 label->kernel_label = strdup(label->kernel);
1216 if (!label->kernel_label)
1217 return -ENOMEM;
1218
Patrice Chotard17e88042019-11-25 09:07:37 +01001219 s = strstr(label->kernel, "#");
1220 if (!s)
1221 return 1;
1222
Patrick Delaunay9d464f82022-10-28 11:01:20 +02001223 label->config = strdup(s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001224 if (!label->config)
1225 return -ENOMEM;
1226
Patrice Chotard17e88042019-11-25 09:07:37 +01001227 *s = 0;
1228
1229 return 1;
1230}
1231
1232/*
1233 * Parses a label and adds it to the list of labels for a menu.
1234 *
1235 * A label ends when we either get to the end of a file, or
1236 * get some input we otherwise don't have a handler defined
1237 * for.
1238 *
1239 */
1240static int parse_label(char **c, struct pxe_menu *cfg)
1241{
1242 struct token t;
1243 int len;
1244 char *s = *c;
1245 struct pxe_label *label;
1246 int err;
1247
1248 label = label_create();
1249 if (!label)
1250 return -ENOMEM;
1251
1252 err = parse_sliteral(c, &label->name);
1253 if (err < 0) {
1254 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1255 label_destroy(label);
1256 return -EINVAL;
1257 }
1258
1259 list_add_tail(&label->list, &cfg->labels);
1260
1261 while (1) {
1262 s = *c;
1263 get_token(c, &t, L_KEYWORD);
1264
1265 err = 0;
1266 switch (t.type) {
1267 case T_MENU:
1268 err = parse_label_menu(c, cfg, label);
1269 break;
1270
1271 case T_KERNEL:
1272 case T_LINUX:
1273 err = parse_label_kernel(c, label);
1274 break;
1275
1276 case T_APPEND:
1277 err = parse_sliteral(c, &label->append);
1278 if (label->initrd)
1279 break;
1280 s = strstr(label->append, "initrd=");
1281 if (!s)
1282 break;
1283 s += 7;
1284 len = (int)(strchr(s, ' ') - s);
1285 label->initrd = malloc(len + 1);
1286 strncpy(label->initrd, s, len);
1287 label->initrd[len] = '\0';
1288
1289 break;
1290
1291 case T_INITRD:
1292 if (!label->initrd)
1293 err = parse_sliteral(c, &label->initrd);
1294 break;
1295
1296 case T_FDT:
1297 if (!label->fdt)
1298 err = parse_sliteral(c, &label->fdt);
1299 break;
1300
1301 case T_FDTDIR:
1302 if (!label->fdtdir)
1303 err = parse_sliteral(c, &label->fdtdir);
1304 break;
1305
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001306 case T_FDTOVERLAYS:
1307 if (!label->fdtoverlays)
1308 err = parse_sliteral(c, &label->fdtoverlays);
1309 break;
1310
Patrice Chotard17e88042019-11-25 09:07:37 +01001311 case T_LOCALBOOT:
1312 label->localboot = 1;
1313 err = parse_integer(c, &label->localboot_val);
1314 break;
1315
1316 case T_IPAPPEND:
1317 err = parse_integer(c, &label->ipappend);
1318 break;
1319
Zhang Ning9c1d9c52022-02-01 08:33:37 +08001320 case T_KASLRSEED:
1321 label->kaslrseed = 1;
1322 break;
1323
Patrice Chotard17e88042019-11-25 09:07:37 +01001324 case T_EOL:
1325 break;
1326
1327 default:
1328 /*
1329 * put the token back! we don't want it - it's the end
1330 * of a label and whatever token this is, it's
1331 * something for the menu level context to handle.
1332 */
1333 *c = s;
1334 return 1;
1335 }
1336
1337 if (err < 0)
1338 return err;
1339 }
1340}
1341
1342/*
1343 * This 16 comes from the limit pxelinux imposes on nested includes.
1344 *
1345 * There is no reason at all we couldn't do more, but some limit helps prevent
1346 * infinite (until crash occurs) recursion if a file tries to include itself.
1347 */
1348#define MAX_NEST_LEVEL 16
1349
1350/*
1351 * Entry point for parsing a menu file. nest_level indicates how many times
1352 * we've nested in includes. It will be 1 for the top level menu file.
1353 *
1354 * Returns 1 on success, < 0 on error.
1355 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001356static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001357 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001358{
1359 struct token t;
1360 char *s, *b, *label_name;
1361 int err;
1362
1363 b = p;
1364
1365 if (nest_level > MAX_NEST_LEVEL) {
1366 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1367 return -EMLINK;
1368 }
1369
1370 while (1) {
1371 s = p;
1372
1373 get_token(&p, &t, L_KEYWORD);
1374
1375 err = 0;
1376 switch (t.type) {
1377 case T_MENU:
1378 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001379 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001380 base + ALIGN(strlen(b) + 1, 4),
1381 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001382 break;
1383
1384 case T_TIMEOUT:
1385 err = parse_integer(&p, &cfg->timeout);
1386 break;
1387
1388 case T_LABEL:
1389 err = parse_label(&p, cfg);
1390 break;
1391
1392 case T_DEFAULT:
1393 case T_ONTIMEOUT:
1394 err = parse_sliteral(&p, &label_name);
1395
1396 if (label_name) {
1397 if (cfg->default_label)
1398 free(cfg->default_label);
1399
1400 cfg->default_label = label_name;
1401 }
1402
1403 break;
1404
Martyn Welch53dd5102024-10-09 14:15:38 +01001405 case T_FALLBACK:
1406 err = parse_sliteral(&p, &label_name);
1407
1408 if (label_name) {
1409 if (cfg->fallback_label)
1410 free(cfg->fallback_label);
1411
1412 cfg->fallback_label = label_name;
1413 }
1414
1415 break;
1416
Patrice Chotard17e88042019-11-25 09:07:37 +01001417 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001418 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001419 base + ALIGN(strlen(b), 4), cfg,
1420 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001421 break;
1422
1423 case T_PROMPT:
Manuel Traut20081242022-11-18 09:00:27 +01001424 err = parse_integer(&p, &cfg->prompt);
1425 // Do not fail if prompt configuration is undefined
1426 if (err < 0)
1427 eol_or_eof(&p);
Patrice Chotard17e88042019-11-25 09:07:37 +01001428 break;
1429
1430 case T_EOL:
1431 break;
1432
1433 case T_EOF:
1434 return 1;
1435
1436 default:
1437 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001438 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001439 eol_or_eof(&p);
1440 }
1441
1442 if (err < 0)
1443 return err;
1444 }
1445}
1446
1447/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001448 */
1449void destroy_pxe_menu(struct pxe_menu *cfg)
1450{
1451 struct list_head *pos, *n;
1452 struct pxe_label *label;
1453
Simon Glass764d0c02021-10-14 12:48:02 -06001454 free(cfg->title);
1455 free(cfg->default_label);
Martyn Welch53dd5102024-10-09 14:15:38 +01001456 free(cfg->fallback_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001457
1458 list_for_each_safe(pos, n, &cfg->labels) {
1459 label = list_entry(pos, struct pxe_label, list);
1460
1461 label_destroy(label);
1462 }
1463
1464 free(cfg);
1465}
1466
Simon Glassb0d08db2021-10-14 12:47:56 -06001467struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001468{
1469 struct pxe_menu *cfg;
1470 char *buf;
1471 int r;
1472
1473 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001474 if (!cfg)
1475 return NULL;
1476
1477 memset(cfg, 0, sizeof(struct pxe_menu));
1478
1479 INIT_LIST_HEAD(&cfg->labels);
1480
1481 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001482 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Martyn Welch2c47aac2024-10-09 14:15:39 +01001483
1484 if (ctx->use_fallback) {
1485 if (cfg->fallback_label) {
1486 printf("Setting use of fallback\n");
1487 cfg->default_label = cfg->fallback_label;
1488 } else {
1489 printf("Selected fallback option, but not set\n");
1490 }
1491 }
1492
Patrice Chotard17e88042019-11-25 09:07:37 +01001493 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001494 if (r < 0) {
1495 destroy_pxe_menu(cfg);
1496 return NULL;
1497 }
1498
1499 return cfg;
1500}
1501
1502/*
1503 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1504 * menu code.
1505 */
1506static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1507{
1508 struct pxe_label *label;
1509 struct list_head *pos;
1510 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001511 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001512 int err;
1513 int i = 1;
1514 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001515 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001516
1517 /*
1518 * Create a menu and add items for all the labels.
1519 */
1520 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
developerc2a1e9e2024-10-29 17:47:16 +08001521 cfg->prompt, NULL, label_print, NULL, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001522 if (!m)
1523 return NULL;
1524
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001525 label_override = env_get("pxe_label_override");
1526
Patrice Chotard17e88042019-11-25 09:07:37 +01001527 list_for_each(pos, &cfg->labels) {
1528 label = list_entry(pos, struct pxe_label, list);
1529
1530 sprintf(label->num, "%d", i++);
1531 if (menu_item_add(m, label->num, label) != 1) {
1532 menu_destroy(m);
1533 return NULL;
1534 }
1535 if (cfg->default_label &&
1536 (strcmp(label->name, cfg->default_label) == 0))
1537 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001538 if (label_override && !strcmp(label->name, label_override))
1539 override_num = label->num;
1540 }
1541
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001542 if (label_override) {
1543 if (override_num)
1544 default_num = override_num;
1545 else
1546 printf("Missing override pxe label: %s\n",
1547 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001548 }
1549
1550 /*
1551 * After we've created items for each label in the menu, set the
1552 * menu's default label if one was specified.
1553 */
1554 if (default_num) {
1555 err = menu_default_set(m, default_num);
1556 if (err != 1) {
1557 if (err != -ENOENT) {
1558 menu_destroy(m);
1559 return NULL;
1560 }
1561
1562 printf("Missing default: %s\n", cfg->default_label);
1563 }
1564 }
1565
1566 return m;
1567}
1568
1569/*
1570 * Try to boot any labels we have yet to attempt to boot.
1571 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001572static void boot_unattempted_labels(struct pxe_context *ctx,
1573 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001574{
1575 struct list_head *pos;
1576 struct pxe_label *label;
1577
1578 list_for_each(pos, &cfg->labels) {
1579 label = list_entry(pos, struct pxe_label, list);
1580
1581 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001582 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001583 }
1584}
1585
Simon Glassb0d08db2021-10-14 12:47:56 -06001586void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001587{
1588 void *choice;
1589 struct menu *m;
1590 int err;
1591
Kory Maincentcaabd242021-02-02 16:42:28 +01001592 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1593 /* display BMP if available */
1594 if (cfg->bmp) {
Simon Glassa686ba62024-11-15 16:19:19 -07001595 if (get_relfile(ctx, cfg->bmp, image_load_addr,
1596 BFI_LOGO, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001597#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001598 struct udevice *dev;
1599
1600 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1601 if (!err)
1602 video_clear(dev);
1603#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001604 bmp_display(image_load_addr,
1605 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1606 } else {
1607 printf("Skipping background bmp %s for failure\n",
1608 cfg->bmp);
1609 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001610 }
1611 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001612
1613 m = pxe_menu_to_menu(cfg);
1614 if (!m)
1615 return;
1616
1617 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001618 menu_destroy(m);
1619
1620 /*
1621 * err == 1 means we got a choice back from menu_get_choice.
1622 *
1623 * err == -ENOENT if the menu was setup to select the default but no
1624 * default was set. in that case, we should continue trying to boot
1625 * labels that haven't been attempted yet.
1626 *
1627 * otherwise, the user interrupted or there was some other error and
1628 * we give up.
1629 */
1630
1631 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001632 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001633 if (!err)
1634 return;
1635 } else if (err != -ENOENT) {
1636 return;
1637 }
1638
Simon Glassb0d08db2021-10-14 12:47:56 -06001639 boot_unattempted_labels(ctx, cfg);
1640}
1641
Simon Glasse719fe02021-10-14 12:48:04 -06001642int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1643 pxe_getfile_func getfile, void *userdata,
Martyn Welch2c47aac2024-10-09 14:15:39 +01001644 bool allow_abs_path, const char *bootfile, bool use_ipv6,
1645 bool use_fallback)
Simon Glassb0d08db2021-10-14 12:47:56 -06001646{
Simon Glasse719fe02021-10-14 12:48:04 -06001647 const char *last_slash;
1648 size_t path_len = 0;
1649
1650 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001651 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001652 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001653 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001654 ctx->allow_abs_path = allow_abs_path;
Sean Edmondba802862023-04-11 10:48:47 -07001655 ctx->use_ipv6 = use_ipv6;
Martyn Welch2c47aac2024-10-09 14:15:39 +01001656 ctx->use_fallback = use_fallback;
Simon Glasse719fe02021-10-14 12:48:04 -06001657
1658 /* figure out the boot directory, if there is one */
1659 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1660 return -ENOSPC;
1661 ctx->bootdir = strdup(bootfile ? bootfile : "");
1662 if (!ctx->bootdir)
1663 return -ENOMEM;
1664
1665 if (bootfile) {
1666 last_slash = strrchr(bootfile, '/');
1667 if (last_slash)
1668 path_len = (last_slash - bootfile) + 1;
1669 }
1670 ctx->bootdir[path_len] = '\0';
1671
1672 return 0;
1673}
1674
1675void pxe_destroy_ctx(struct pxe_context *ctx)
1676{
1677 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001678}
Simon Glass791bbfe2021-10-14 12:48:03 -06001679
1680int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1681{
1682 struct pxe_menu *cfg;
1683
1684 cfg = parse_pxefile(ctx, pxefile_addr_r);
1685 if (!cfg) {
1686 printf("Error parsing config file\n");
1687 return 1;
1688 }
1689
1690 if (prompt)
1691 cfg->prompt = 1;
1692
1693 handle_pxe_menu(ctx, cfg);
1694
1695 destroy_pxe_menu(cfg);
1696
1697 return 0;
1698}