blob: 37306f370093127ef5b4b956bf98f910d327f78b [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) {
Simon Glass60acbbc2025-03-05 17:25:13 -0700615 if (!IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS) ||
616 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 }
619
620 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
621 buf = map_sysmem(kernel_addr_r, 0);
622
Simon Glassb3beb6f2025-03-05 17:25:12 -0700623 if (!bmi.conf_fdt && genimg_get_format(buf) != IMAGE_FORMAT_FIT) {
Simon Glass60acbbc2025-03-05 17:25:13 -0700624 if (!IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS) ||
625 strcmp("-", label->fdt))
Simon Glassb3beb6f2025-03-05 17:25:12 -0700626 bmi.conf_fdt = env_get("fdtcontroladdr");
Tom Rini793921e2024-04-18 08:29:35 -0600627 }
628
Tom Rini793921e2024-04-18 08:29:35 -0600629 /* Try bootm for legacy and FIT format image */
630 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
Simon Glass9b2c78c2024-06-19 06:34:50 -0600631 IS_ENABLED(CONFIG_CMD_BOOTM)) {
632 log_debug("using bootm\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700633 ret = bootm_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600634 /* Try booting an AArch64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600635 } else if (IS_ENABLED(CONFIG_CMD_BOOTI)) {
636 log_debug("using booti\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700637 ret = booti_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600638 /* Try booting a Image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600639 } else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) {
640 log_debug("using bootz\n");
Simon Glassb3beb6f2025-03-05 17:25:12 -0700641 ret = bootz_run(&bmi);
Tom Rini793921e2024-04-18 08:29:35 -0600642 /* Try booting an x86_64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600643 } else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) {
644 log_debug("using zboot\n");
Tom Rini793921e2024-04-18 08:29:35 -0600645 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Simon Glass9b2c78c2024-06-19 06:34:50 -0600646 }
Tom Rini793921e2024-04-18 08:29:35 -0600647
648 unmap_sysmem(buf);
Simon Glassb3beb6f2025-03-05 17:25:12 -0700649 if (ret)
650 return ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100651
Simon Glass827cd9c2025-03-05 17:25:04 -0700652 return 0;
653}
654
655/**
656 * label_boot() - Boot according to the contents of a pxe_label
657 *
658 * If we can't boot for any reason, we return. A successful boot never
659 * returns.
660 *
661 * The kernel will be stored in the location given by the 'kernel_addr_r'
662 * environment variable.
663 *
664 * If the label specifies an initrd file, it will be stored in the location
665 * given by the 'ramdisk_addr_r' environment variable.
666 *
667 * If the label specifies an 'append' line, its contents will overwrite that
668 * of the 'bootargs' environment variable.
669 *
670 * @ctx: PXE context
671 * @label: Label to process
672 * Returns does not return on success, otherwise returns 0 if a localboot
673 * label was processed, or 1 on error
674 */
675static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
676{
677 char *kernel_addr = NULL;
678 char *initrd_addr_str = NULL;
679 char initrd_filesize[10];
680 char initrd_str[28];
681 char mac_str[29] = "";
682 char ip_str[68] = "";
683 char *fit_addr = NULL;
684
685 label_print(label);
686
687 label->attempted = 1;
688
689 if (label->localboot) {
690 if (label->localboot_val >= 0)
691 label_localboot(label);
692 return 0;
693 }
694
695 if (!label->kernel) {
696 printf("No kernel given, skipping %s\n",
697 label->name);
698 return 1;
699 }
700
701 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
702 (enum bootflow_img_t)IH_TYPE_KERNEL, NULL)
703 < 0) {
704 printf("Skipping %s for failure retrieving kernel\n",
705 label->name);
706 return 1;
707 }
708
709 kernel_addr = env_get("kernel_addr_r");
710 /* for FIT, append the configuration identifier */
711 if (label->config) {
712 int len = strlen(kernel_addr) + strlen(label->config) + 1;
713
714 fit_addr = malloc(len);
715 if (!fit_addr) {
716 printf("malloc fail (FIT address)\n");
717 return 1;
718 }
719 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
720 kernel_addr = fit_addr;
721 }
722
723 /* For FIT, the label can be identical to kernel one */
724 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
725 initrd_addr_str = kernel_addr;
726 } else if (label->initrd) {
727 ulong size;
728 int ret;
729
730 ret = get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
731 (enum bootflow_img_t)IH_TYPE_RAMDISK,
732 &size);
733 if (ret < 0) {
734 printf("Skipping %s for failure retrieving initrd\n",
735 label->name);
736 goto cleanup;
737 }
738 strcpy(initrd_filesize, simple_xtoa(size));
739 initrd_addr_str = env_get("ramdisk_addr_r");
740 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
741 initrd_addr_str, size);
742 if (size >= sizeof(initrd_str))
743 goto cleanup;
744 }
745
746 if (label->ipappend & 0x1) {
747 sprintf(ip_str, " ip=%s:%s:%s:%s",
748 env_get("ipaddr"), env_get("serverip"),
749 env_get("gatewayip"), env_get("netmask"));
750 }
751
752 if (IS_ENABLED(CONFIG_CMD_NET)) {
753 if (label->ipappend & 0x2) {
754 int err;
755
756 strcpy(mac_str, " BOOTIF=");
757 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
758 if (err < 0)
759 mac_str[0] = '\0';
760 }
761 }
762
763 if ((label->ipappend & 0x3) || label->append) {
764 char bootargs[CONFIG_SYS_CBSIZE] = "";
765 char finalbootargs[CONFIG_SYS_CBSIZE];
766
767 if (strlen(label->append ?: "") +
768 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
769 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
770 strlen(label->append ?: ""),
771 strlen(ip_str), strlen(mac_str),
772 sizeof(bootargs));
773 goto cleanup;
774 }
775
776 if (label->append)
777 strlcpy(bootargs, label->append, sizeof(bootargs));
778
779 strcat(bootargs, ip_str);
780 strcat(bootargs, mac_str);
781
782 cli_simple_process_macros(bootargs, finalbootargs,
783 sizeof(finalbootargs));
784 env_set("bootargs", finalbootargs);
785 printf("append: %s\n", finalbootargs);
786 }
787
788 label_run_boot(ctx, label, kernel_addr, initrd_addr_str,
789 initrd_filesize, initrd_str);
790 /* ignore the error value since we are going to fail anyway */
791
Patrice Chotard17e88042019-11-25 09:07:37 +0100792cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600793 free(fit_addr);
794
Simon Glass827cd9c2025-03-05 17:25:04 -0700795 return 1; /* returning is always failure */
Patrice Chotard17e88042019-11-25 09:07:37 +0100796}
797
Simon Glass00302442021-10-14 12:48:01 -0600798/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100799enum token_type {
800 T_EOL,
801 T_STRING,
802 T_EOF,
803 T_MENU,
804 T_TITLE,
805 T_TIMEOUT,
806 T_LABEL,
807 T_KERNEL,
808 T_LINUX,
809 T_APPEND,
810 T_INITRD,
811 T_LOCALBOOT,
812 T_DEFAULT,
813 T_PROMPT,
814 T_INCLUDE,
815 T_FDT,
816 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100817 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100818 T_ONTIMEOUT,
819 T_IPAPPEND,
820 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800821 T_KASLRSEED,
Martyn Welch53dd5102024-10-09 14:15:38 +0100822 T_FALLBACK,
Patrice Chotard17e88042019-11-25 09:07:37 +0100823 T_INVALID
824};
825
Simon Glass00302442021-10-14 12:48:01 -0600826/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100827struct token {
828 char *val;
829 enum token_type type;
830};
831
Simon Glass00302442021-10-14 12:48:01 -0600832/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100833static const struct token keywords[] = {
834 {"menu", T_MENU},
835 {"title", T_TITLE},
836 {"timeout", T_TIMEOUT},
837 {"default", T_DEFAULT},
838 {"prompt", T_PROMPT},
839 {"label", T_LABEL},
840 {"kernel", T_KERNEL},
841 {"linux", T_LINUX},
842 {"localboot", T_LOCALBOOT},
843 {"append", T_APPEND},
844 {"initrd", T_INITRD},
845 {"include", T_INCLUDE},
846 {"devicetree", T_FDT},
847 {"fdt", T_FDT},
848 {"devicetreedir", T_FDTDIR},
849 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100850 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200851 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100852 {"ontimeout", T_ONTIMEOUT,},
853 {"ipappend", T_IPAPPEND,},
854 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800855 {"kaslrseed", T_KASLRSEED,},
Martyn Welch53dd5102024-10-09 14:15:38 +0100856 {"fallback", T_FALLBACK,},
Patrice Chotard17e88042019-11-25 09:07:37 +0100857 {NULL, T_INVALID}
858};
859
Simon Glass00302442021-10-14 12:48:01 -0600860/**
861 * enum lex_state - lexer state
862 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100863 * Since pxe(linux) files don't have a token to identify the start of a
864 * literal, we have to keep track of when we're in a state where a literal is
865 * expected vs when we're in a state a keyword is expected.
866 */
867enum lex_state {
868 L_NORMAL = 0,
869 L_KEYWORD,
870 L_SLITERAL
871};
872
Simon Glass00302442021-10-14 12:48:01 -0600873/**
874 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100875 *
Simon Glass00302442021-10-14 12:48:01 -0600876 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100877 *
878 * Characters from *p are copied into t-val until a character equal to
879 * delim is found, or a NUL byte is reached. If delim has the special value of
880 * ' ', any whitespace character will be used as a delimiter.
881 *
882 * If lower is unequal to 0, uppercase characters will be converted to
883 * lowercase in the result. This is useful to make keywords case
884 * insensitive.
885 *
886 * The location of *p is updated to point to the first character after the end
887 * of the token - the ending delimiter.
888 *
Simon Glass00302442021-10-14 12:48:01 -0600889 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
890 * it.
891 *
892 * @p: Points to a pointer to the current position in the input being processed.
893 * Updated to point at the first character after the current token
894 * @t: Pointers to a token to fill in
895 * @delim: Delimiter character to look for, either newline or space
896 * @lower: true to convert the string to lower case when storing
897 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100898 */
899static char *get_string(char **p, struct token *t, char delim, int lower)
900{
901 char *b, *e;
902 size_t len, i;
903
904 /*
905 * b and e both start at the beginning of the input stream.
906 *
907 * e is incremented until we find the ending delimiter, or a NUL byte
908 * is reached. Then, we take e - b to find the length of the token.
909 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100910 b = *p;
911 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100912 while (*e) {
913 if ((delim == ' ' && isspace(*e)) || delim == *e)
914 break;
915 e++;
916 }
917
918 len = e - b;
919
920 /*
921 * Allocate memory to hold the string, and copy it in, converting
922 * characters to lowercase if lower is != 0.
923 */
924 t->val = malloc(len + 1);
925 if (!t->val)
926 return NULL;
927
928 for (i = 0; i < len; i++, b++) {
929 if (lower)
930 t->val[i] = tolower(*b);
931 else
932 t->val[i] = *b;
933 }
934
935 t->val[len] = '\0';
936
Simon Glass764d0c02021-10-14 12:48:02 -0600937 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100938 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100939 t->type = T_STRING;
940
941 return t->val;
942}
943
Simon Glass00302442021-10-14 12:48:01 -0600944/**
945 * get_keyword() - Populate a keyword token with a type and value
946 *
947 * Updates the ->type field based on the keyword string in @val
948 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100949 */
950static void get_keyword(struct token *t)
951{
952 int i;
953
954 for (i = 0; keywords[i].val; i++) {
955 if (!strcmp(t->val, keywords[i].val)) {
956 t->type = keywords[i].type;
957 break;
958 }
959 }
960}
961
Simon Glass00302442021-10-14 12:48:01 -0600962/**
963 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100964 *
Simon Glass00302442021-10-14 12:48:01 -0600965 * We have to keep track of which state we're in to know if we're looking to get
966 * a string literal or a keyword.
967 *
968 * @p: Points to a pointer to the current position in the input being processed.
969 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100970 */
971static void get_token(char **p, struct token *t, enum lex_state state)
972{
973 char *c = *p;
974
975 t->type = T_INVALID;
976
977 /* eat non EOL whitespace */
978 while (isblank(*c))
979 c++;
980
981 /*
982 * eat comments. note that string literals can't begin with #, but
983 * can contain a # after their first character.
984 */
985 if (*c == '#') {
986 while (*c && *c != '\n')
987 c++;
988 }
989
990 if (*c == '\n') {
991 t->type = T_EOL;
992 c++;
993 } else if (*c == '\0') {
994 t->type = T_EOF;
995 c++;
996 } else if (state == L_SLITERAL) {
997 get_string(&c, t, '\n', 0);
998 } else if (state == L_KEYWORD) {
999 /*
1000 * when we expect a keyword, we first get the next string
1001 * token delimited by whitespace, and then check if it
1002 * matches a keyword in our keyword list. if it does, it's
1003 * converted to a keyword token of the appropriate type, and
1004 * if not, it remains a string token.
1005 */
1006 get_string(&c, t, ' ', 1);
1007 get_keyword(t);
1008 }
1009
1010 *p = c;
1011}
1012
Simon Glass00302442021-10-14 12:48:01 -06001013/**
1014 * eol_or_eof() - Find end of line
1015 *
1016 * Increment *c until we get to the end of the current line, or EOF
1017 *
1018 * @c: Points to a pointer to the current position in the input being processed.
1019 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +01001020 */
1021static void eol_or_eof(char **c)
1022{
1023 while (**c && **c != '\n')
1024 (*c)++;
1025}
1026
1027/*
1028 * All of these parse_* functions share some common behavior.
1029 *
1030 * They finish with *c pointing after the token they parse, and return 1 on
1031 * success, or < 0 on error.
1032 */
1033
1034/*
1035 * Parse a string literal and store a pointer it at *dst. String literals
1036 * terminate at the end of the line.
1037 */
1038static int parse_sliteral(char **c, char **dst)
1039{
1040 struct token t;
1041 char *s = *c;
1042
1043 get_token(c, &t, L_SLITERAL);
1044
1045 if (t.type != T_STRING) {
1046 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1047 return -EINVAL;
1048 }
1049
1050 *dst = t.val;
1051
1052 return 1;
1053}
1054
1055/*
1056 * Parse a base 10 (unsigned) integer and store it at *dst.
1057 */
1058static int parse_integer(char **c, int *dst)
1059{
1060 struct token t;
1061 char *s = *c;
1062
1063 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001064 if (t.type != T_STRING) {
1065 printf("Expected string: %.*s\n", (int)(*c - s), s);
1066 return -EINVAL;
1067 }
1068
1069 *dst = simple_strtol(t.val, NULL, 10);
1070
1071 free(t.val);
1072
1073 return 1;
1074}
1075
Simon Glassb0d08db2021-10-14 12:47:56 -06001076static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001077 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001078
1079/*
1080 * Parse an include statement, and retrieve and parse the file it mentions.
1081 *
1082 * base should point to a location where it's safe to store the file, and
1083 * nest_level should indicate how many nested includes have occurred. For this
1084 * include, nest_level has already been incremented and doesn't need to be
1085 * incremented here.
1086 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001087static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001088 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001089{
1090 char *include_path;
1091 char *s = *c;
1092 int err;
1093 char *buf;
1094 int ret;
1095
1096 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001097 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001098 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001099 return err;
1100 }
1101
Simon Glassb0d08db2021-10-14 12:47:56 -06001102 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001103 if (err < 0) {
1104 printf("Couldn't retrieve %s\n", include_path);
1105 return err;
1106 }
1107
1108 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001109 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001110 unmap_sysmem(buf);
1111
1112 return ret;
1113}
1114
1115/*
1116 * Parse lines that begin with 'menu'.
1117 *
1118 * base and nest are provided to handle the 'menu include' case.
1119 *
1120 * base should point to a location where it's safe to store the included file.
1121 *
1122 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1123 * a file it includes, 3 when parsing a file included by that file, and so on.
1124 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001125static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001126 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001127{
1128 struct token t;
1129 char *s = *c;
1130 int err = 0;
1131
1132 get_token(c, &t, L_KEYWORD);
1133
1134 switch (t.type) {
1135 case T_TITLE:
1136 err = parse_sliteral(c, &cfg->title);
1137
1138 break;
1139
1140 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001141 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001142 break;
1143
1144 case T_BACKGROUND:
1145 err = parse_sliteral(c, &cfg->bmp);
1146 break;
1147
1148 default:
1149 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001150 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001151 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001152 if (err < 0)
1153 return err;
1154
1155 eol_or_eof(c);
1156
1157 return 1;
1158}
1159
1160/*
1161 * Handles parsing a 'menu line' when we're parsing a label.
1162 */
1163static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001164 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001165{
1166 struct token t;
1167 char *s;
1168
1169 s = *c;
1170
1171 get_token(c, &t, L_KEYWORD);
1172
1173 switch (t.type) {
1174 case T_DEFAULT:
1175 if (!cfg->default_label)
1176 cfg->default_label = strdup(label->name);
1177
1178 if (!cfg->default_label)
1179 return -ENOMEM;
1180
1181 break;
1182 case T_LABEL:
1183 parse_sliteral(c, &label->menu);
1184 break;
1185 default:
1186 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001187 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001188 }
1189
1190 eol_or_eof(c);
1191
1192 return 0;
1193}
1194
1195/*
1196 * Handles parsing a 'kernel' label.
1197 * expecting "filename" or "<fit_filename>#cfg"
1198 */
1199static int parse_label_kernel(char **c, struct pxe_label *label)
1200{
1201 char *s;
1202 int err;
1203
1204 err = parse_sliteral(c, &label->kernel);
1205 if (err < 0)
1206 return err;
1207
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +02001208 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1209 label->kernel_label = strdup(label->kernel);
1210 if (!label->kernel_label)
1211 return -ENOMEM;
1212
Patrice Chotard17e88042019-11-25 09:07:37 +01001213 s = strstr(label->kernel, "#");
1214 if (!s)
1215 return 1;
1216
Patrick Delaunay9d464f82022-10-28 11:01:20 +02001217 label->config = strdup(s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001218 if (!label->config)
1219 return -ENOMEM;
1220
Patrice Chotard17e88042019-11-25 09:07:37 +01001221 *s = 0;
1222
1223 return 1;
1224}
1225
1226/*
1227 * Parses a label and adds it to the list of labels for a menu.
1228 *
1229 * A label ends when we either get to the end of a file, or
1230 * get some input we otherwise don't have a handler defined
1231 * for.
1232 *
1233 */
1234static int parse_label(char **c, struct pxe_menu *cfg)
1235{
1236 struct token t;
1237 int len;
1238 char *s = *c;
1239 struct pxe_label *label;
1240 int err;
1241
1242 label = label_create();
1243 if (!label)
1244 return -ENOMEM;
1245
1246 err = parse_sliteral(c, &label->name);
1247 if (err < 0) {
1248 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1249 label_destroy(label);
1250 return -EINVAL;
1251 }
1252
1253 list_add_tail(&label->list, &cfg->labels);
1254
1255 while (1) {
1256 s = *c;
1257 get_token(c, &t, L_KEYWORD);
1258
1259 err = 0;
1260 switch (t.type) {
1261 case T_MENU:
1262 err = parse_label_menu(c, cfg, label);
1263 break;
1264
1265 case T_KERNEL:
1266 case T_LINUX:
1267 err = parse_label_kernel(c, label);
1268 break;
1269
1270 case T_APPEND:
1271 err = parse_sliteral(c, &label->append);
1272 if (label->initrd)
1273 break;
1274 s = strstr(label->append, "initrd=");
1275 if (!s)
1276 break;
1277 s += 7;
1278 len = (int)(strchr(s, ' ') - s);
1279 label->initrd = malloc(len + 1);
1280 strncpy(label->initrd, s, len);
1281 label->initrd[len] = '\0';
1282
1283 break;
1284
1285 case T_INITRD:
1286 if (!label->initrd)
1287 err = parse_sliteral(c, &label->initrd);
1288 break;
1289
1290 case T_FDT:
1291 if (!label->fdt)
1292 err = parse_sliteral(c, &label->fdt);
1293 break;
1294
1295 case T_FDTDIR:
1296 if (!label->fdtdir)
1297 err = parse_sliteral(c, &label->fdtdir);
1298 break;
1299
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001300 case T_FDTOVERLAYS:
1301 if (!label->fdtoverlays)
1302 err = parse_sliteral(c, &label->fdtoverlays);
1303 break;
1304
Patrice Chotard17e88042019-11-25 09:07:37 +01001305 case T_LOCALBOOT:
1306 label->localboot = 1;
1307 err = parse_integer(c, &label->localboot_val);
1308 break;
1309
1310 case T_IPAPPEND:
1311 err = parse_integer(c, &label->ipappend);
1312 break;
1313
Zhang Ning9c1d9c52022-02-01 08:33:37 +08001314 case T_KASLRSEED:
1315 label->kaslrseed = 1;
1316 break;
1317
Patrice Chotard17e88042019-11-25 09:07:37 +01001318 case T_EOL:
1319 break;
1320
1321 default:
1322 /*
1323 * put the token back! we don't want it - it's the end
1324 * of a label and whatever token this is, it's
1325 * something for the menu level context to handle.
1326 */
1327 *c = s;
1328 return 1;
1329 }
1330
1331 if (err < 0)
1332 return err;
1333 }
1334}
1335
1336/*
1337 * This 16 comes from the limit pxelinux imposes on nested includes.
1338 *
1339 * There is no reason at all we couldn't do more, but some limit helps prevent
1340 * infinite (until crash occurs) recursion if a file tries to include itself.
1341 */
1342#define MAX_NEST_LEVEL 16
1343
1344/*
1345 * Entry point for parsing a menu file. nest_level indicates how many times
1346 * we've nested in includes. It will be 1 for the top level menu file.
1347 *
1348 * Returns 1 on success, < 0 on error.
1349 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001350static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001351 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001352{
1353 struct token t;
1354 char *s, *b, *label_name;
1355 int err;
1356
1357 b = p;
1358
1359 if (nest_level > MAX_NEST_LEVEL) {
1360 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1361 return -EMLINK;
1362 }
1363
1364 while (1) {
1365 s = p;
1366
1367 get_token(&p, &t, L_KEYWORD);
1368
1369 err = 0;
1370 switch (t.type) {
1371 case T_MENU:
1372 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001373 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001374 base + ALIGN(strlen(b) + 1, 4),
1375 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001376 break;
1377
1378 case T_TIMEOUT:
1379 err = parse_integer(&p, &cfg->timeout);
1380 break;
1381
1382 case T_LABEL:
1383 err = parse_label(&p, cfg);
1384 break;
1385
1386 case T_DEFAULT:
1387 case T_ONTIMEOUT:
1388 err = parse_sliteral(&p, &label_name);
1389
1390 if (label_name) {
1391 if (cfg->default_label)
1392 free(cfg->default_label);
1393
1394 cfg->default_label = label_name;
1395 }
1396
1397 break;
1398
Martyn Welch53dd5102024-10-09 14:15:38 +01001399 case T_FALLBACK:
1400 err = parse_sliteral(&p, &label_name);
1401
1402 if (label_name) {
1403 if (cfg->fallback_label)
1404 free(cfg->fallback_label);
1405
1406 cfg->fallback_label = label_name;
1407 }
1408
1409 break;
1410
Patrice Chotard17e88042019-11-25 09:07:37 +01001411 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001412 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001413 base + ALIGN(strlen(b), 4), cfg,
1414 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001415 break;
1416
1417 case T_PROMPT:
Manuel Traut20081242022-11-18 09:00:27 +01001418 err = parse_integer(&p, &cfg->prompt);
1419 // Do not fail if prompt configuration is undefined
1420 if (err < 0)
1421 eol_or_eof(&p);
Patrice Chotard17e88042019-11-25 09:07:37 +01001422 break;
1423
1424 case T_EOL:
1425 break;
1426
1427 case T_EOF:
1428 return 1;
1429
1430 default:
1431 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001432 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001433 eol_or_eof(&p);
1434 }
1435
1436 if (err < 0)
1437 return err;
1438 }
1439}
1440
1441/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001442 */
1443void destroy_pxe_menu(struct pxe_menu *cfg)
1444{
1445 struct list_head *pos, *n;
1446 struct pxe_label *label;
1447
Simon Glass764d0c02021-10-14 12:48:02 -06001448 free(cfg->title);
1449 free(cfg->default_label);
Martyn Welch53dd5102024-10-09 14:15:38 +01001450 free(cfg->fallback_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001451
1452 list_for_each_safe(pos, n, &cfg->labels) {
1453 label = list_entry(pos, struct pxe_label, list);
1454
1455 label_destroy(label);
1456 }
1457
1458 free(cfg);
1459}
1460
Simon Glassb0d08db2021-10-14 12:47:56 -06001461struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001462{
1463 struct pxe_menu *cfg;
1464 char *buf;
1465 int r;
1466
1467 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001468 if (!cfg)
1469 return NULL;
1470
1471 memset(cfg, 0, sizeof(struct pxe_menu));
1472
1473 INIT_LIST_HEAD(&cfg->labels);
1474
1475 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001476 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Martyn Welch2c47aac2024-10-09 14:15:39 +01001477
1478 if (ctx->use_fallback) {
1479 if (cfg->fallback_label) {
1480 printf("Setting use of fallback\n");
1481 cfg->default_label = cfg->fallback_label;
1482 } else {
1483 printf("Selected fallback option, but not set\n");
1484 }
1485 }
1486
Patrice Chotard17e88042019-11-25 09:07:37 +01001487 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001488 if (r < 0) {
1489 destroy_pxe_menu(cfg);
1490 return NULL;
1491 }
1492
1493 return cfg;
1494}
1495
1496/*
1497 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1498 * menu code.
1499 */
1500static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1501{
1502 struct pxe_label *label;
1503 struct list_head *pos;
1504 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001505 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001506 int err;
1507 int i = 1;
1508 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001509 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001510
1511 /*
1512 * Create a menu and add items for all the labels.
1513 */
1514 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
developerc2a1e9e2024-10-29 17:47:16 +08001515 cfg->prompt, NULL, label_print, NULL, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001516 if (!m)
1517 return NULL;
1518
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001519 label_override = env_get("pxe_label_override");
1520
Patrice Chotard17e88042019-11-25 09:07:37 +01001521 list_for_each(pos, &cfg->labels) {
1522 label = list_entry(pos, struct pxe_label, list);
1523
1524 sprintf(label->num, "%d", i++);
1525 if (menu_item_add(m, label->num, label) != 1) {
1526 menu_destroy(m);
1527 return NULL;
1528 }
1529 if (cfg->default_label &&
1530 (strcmp(label->name, cfg->default_label) == 0))
1531 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001532 if (label_override && !strcmp(label->name, label_override))
1533 override_num = label->num;
1534 }
1535
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001536 if (label_override) {
1537 if (override_num)
1538 default_num = override_num;
1539 else
1540 printf("Missing override pxe label: %s\n",
1541 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001542 }
1543
1544 /*
1545 * After we've created items for each label in the menu, set the
1546 * menu's default label if one was specified.
1547 */
1548 if (default_num) {
1549 err = menu_default_set(m, default_num);
1550 if (err != 1) {
1551 if (err != -ENOENT) {
1552 menu_destroy(m);
1553 return NULL;
1554 }
1555
1556 printf("Missing default: %s\n", cfg->default_label);
1557 }
1558 }
1559
1560 return m;
1561}
1562
1563/*
1564 * Try to boot any labels we have yet to attempt to boot.
1565 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001566static void boot_unattempted_labels(struct pxe_context *ctx,
1567 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001568{
1569 struct list_head *pos;
1570 struct pxe_label *label;
1571
1572 list_for_each(pos, &cfg->labels) {
1573 label = list_entry(pos, struct pxe_label, list);
1574
1575 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001576 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001577 }
1578}
1579
Simon Glassb0d08db2021-10-14 12:47:56 -06001580void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001581{
1582 void *choice;
1583 struct menu *m;
1584 int err;
1585
Kory Maincentcaabd242021-02-02 16:42:28 +01001586 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1587 /* display BMP if available */
1588 if (cfg->bmp) {
Simon Glassa686ba62024-11-15 16:19:19 -07001589 if (get_relfile(ctx, cfg->bmp, image_load_addr,
1590 BFI_LOGO, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001591#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001592 struct udevice *dev;
1593
1594 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1595 if (!err)
1596 video_clear(dev);
1597#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001598 bmp_display(image_load_addr,
1599 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1600 } else {
1601 printf("Skipping background bmp %s for failure\n",
1602 cfg->bmp);
1603 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001604 }
1605 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001606
1607 m = pxe_menu_to_menu(cfg);
1608 if (!m)
1609 return;
1610
1611 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001612 menu_destroy(m);
1613
1614 /*
1615 * err == 1 means we got a choice back from menu_get_choice.
1616 *
1617 * err == -ENOENT if the menu was setup to select the default but no
1618 * default was set. in that case, we should continue trying to boot
1619 * labels that haven't been attempted yet.
1620 *
1621 * otherwise, the user interrupted or there was some other error and
1622 * we give up.
1623 */
1624
1625 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001626 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001627 if (!err)
1628 return;
1629 } else if (err != -ENOENT) {
1630 return;
1631 }
1632
Simon Glassb0d08db2021-10-14 12:47:56 -06001633 boot_unattempted_labels(ctx, cfg);
1634}
1635
Simon Glasse719fe02021-10-14 12:48:04 -06001636int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1637 pxe_getfile_func getfile, void *userdata,
Martyn Welch2c47aac2024-10-09 14:15:39 +01001638 bool allow_abs_path, const char *bootfile, bool use_ipv6,
1639 bool use_fallback)
Simon Glassb0d08db2021-10-14 12:47:56 -06001640{
Simon Glasse719fe02021-10-14 12:48:04 -06001641 const char *last_slash;
1642 size_t path_len = 0;
1643
1644 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001645 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001646 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001647 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001648 ctx->allow_abs_path = allow_abs_path;
Sean Edmondba802862023-04-11 10:48:47 -07001649 ctx->use_ipv6 = use_ipv6;
Martyn Welch2c47aac2024-10-09 14:15:39 +01001650 ctx->use_fallback = use_fallback;
Simon Glasse719fe02021-10-14 12:48:04 -06001651
1652 /* figure out the boot directory, if there is one */
1653 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1654 return -ENOSPC;
1655 ctx->bootdir = strdup(bootfile ? bootfile : "");
1656 if (!ctx->bootdir)
1657 return -ENOMEM;
1658
1659 if (bootfile) {
1660 last_slash = strrchr(bootfile, '/');
1661 if (last_slash)
1662 path_len = (last_slash - bootfile) + 1;
1663 }
1664 ctx->bootdir[path_len] = '\0';
1665
1666 return 0;
1667}
1668
1669void pxe_destroy_ctx(struct pxe_context *ctx)
1670{
1671 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001672}
Simon Glass791bbfe2021-10-14 12:48:03 -06001673
1674int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1675{
1676 struct pxe_menu *cfg;
1677
1678 cfg = parse_pxefile(ctx, pxefile_addr_r);
1679 if (!cfg) {
1680 printf("Error parsing config file\n");
1681 return 1;
1682 }
1683
1684 if (prompt)
1685 cfg->prompt = 1;
1686
1687 handle_pxe_menu(ctx, cfg);
1688
1689 destroy_pxe_menu(cfg);
1690
1691 return 0;
1692}