blob: 8c9c7f84d975092929b913e72899a4522ca2e948 [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 Glassed38aef2020-05-10 11:40:03 -06009#include <command.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010010#include <dm.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010011#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070012#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010014#include <malloc.h>
15#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <net.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010017#include <fdt_support.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010018#include <video.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010019#include <linux/libfdt.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010020#include <linux/string.h>
21#include <linux/ctype.h>
22#include <errno.h>
23#include <linux/list.h>
24
Zhang Ning9c1d9c52022-02-01 08:33:37 +080025#include <rng.h>
Zhang Ning9c1d9c52022-02-01 08:33:37 +080026
Patrice Chotard17e88042019-11-25 09:07:37 +010027#include <splash.h>
28#include <asm/io.h>
29
30#include "menu.h"
31#include "cli.h"
32
33#include "pxe_utils.h"
34
Ben Wolsieffer6273f132019-11-28 00:07:08 -050035#define MAX_TFTP_PATH_LEN 512
Patrice Chotard17e88042019-11-25 09:07:37 +010036
Simon Glassa9401b92021-10-14 12:48:08 -060037int pxe_get_file_size(ulong *sizep)
38{
39 const char *val;
40
41 val = from_env("filesize");
42 if (!val)
43 return -ENOENT;
44
45 if (strict_strtoul(val, 16, sizep) < 0)
46 return -EINVAL;
47
48 return 0;
49}
50
Simon Glass00302442021-10-14 12:48:01 -060051/**
52 * format_mac_pxe() - obtain a MAC address in the PXE format
53 *
54 * This produces a MAC-address string in the format for the current ethernet
55 * device:
56 *
57 * 01-aa-bb-cc-dd-ee-ff
58 *
59 * where aa-ff is the MAC address in hex
60 *
61 * @outbuf: Buffer to write string to
62 * @outbuf_len: length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010063 * Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
Simon Glass00302442021-10-14 12:48:01 -060064 * current ethernet device
65 */
Patrice Chotard17e88042019-11-25 09:07:37 +010066int format_mac_pxe(char *outbuf, size_t outbuf_len)
67{
68 uchar ethaddr[6];
69
70 if (outbuf_len < 21) {
71 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Simon Glass00302442021-10-14 12:48:01 -060072 return -ENOSPC;
Patrice Chotard17e88042019-11-25 09:07:37 +010073 }
74
75 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
76 return -ENOENT;
77
78 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
79 ethaddr[0], ethaddr[1], ethaddr[2],
80 ethaddr[3], ethaddr[4], ethaddr[5]);
81
82 return 1;
83}
84
Simon Glass00302442021-10-14 12:48:01 -060085/**
Simon Glass00302442021-10-14 12:48:01 -060086 * get_relfile() - read a file relative to the PXE file
87 *
Patrice Chotard17e88042019-11-25 09:07:37 +010088 * As in pxelinux, paths to files referenced from files we retrieve are
89 * relative to the location of bootfile. get_relfile takes such a path and
90 * joins it with the bootfile path to get the full path to the target file. If
91 * the bootfile path is NULL, we use file_path as is.
92 *
Simon Glass00302442021-10-14 12:48:01 -060093 * @ctx: PXE context
94 * @file_path: File path to read (relative to the PXE file)
95 * @file_addr: Address to load file to
Simon Glassa9401b92021-10-14 12:48:08 -060096 * @filesizep: If not NULL, returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -060097 * Returns 1 for success, or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +010098 */
Simon Glassb0d08db2021-10-14 12:47:56 -060099static int get_relfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600100 unsigned long file_addr, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100101{
102 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100103 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100104 char addr_buf[18];
Simon Glassa9401b92021-10-14 12:48:08 -0600105 ulong size;
106 int ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100107
Simon Glass5e3e39a2021-10-14 12:48:05 -0600108 if (file_path[0] == '/' && ctx->allow_abs_path)
109 *relfile = '\0';
110 else
111 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
Patrice Chotard17e88042019-11-25 09:07:37 +0100112
Simon Glass5e3e39a2021-10-14 12:48:05 -0600113 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard17e88042019-11-25 09:07:37 +0100114
115 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100116 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100117
118 return -ENAMETOOLONG;
119 }
120
121 strcat(relfile, file_path);
122
123 printf("Retrieving file: %s\n", relfile);
124
125 sprintf(addr_buf, "%lx", file_addr);
126
Simon Glassa9401b92021-10-14 12:48:08 -0600127 ret = ctx->getfile(ctx, relfile, addr_buf, &size);
128 if (ret < 0)
129 return log_msg_ret("get", ret);
130 if (filesizep)
131 *filesizep = size;
132
133 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100134}
135
Simon Glassb0d08db2021-10-14 12:47:56 -0600136int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600137 ulong file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100138{
Simon Glassa9401b92021-10-14 12:48:08 -0600139 ulong size;
Patrice Chotard17e88042019-11-25 09:07:37 +0100140 int err;
141 char *buf;
142
Simon Glassa9401b92021-10-14 12:48:08 -0600143 err = get_relfile(ctx, file_path, file_addr, &size);
Patrice Chotard17e88042019-11-25 09:07:37 +0100144 if (err < 0)
145 return err;
146
Simon Glassa9401b92021-10-14 12:48:08 -0600147 buf = map_sysmem(file_addr + size, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +0100148 *buf = '\0';
149 unmap_sysmem(buf);
150
151 return 1;
152}
153
154#define PXELINUX_DIR "pxelinux.cfg/"
155
Simon Glass00302442021-10-14 12:48:01 -0600156/**
157 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
158 *
159 * @ctx: PXE context
160 * @file: Filename to process (relative to pxelinux.cfg/)
161 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
162 * or other value < 0 on other error
163 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600164int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100165 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100166{
167 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100168 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100169
170 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
171 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100172 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100173 return -ENAMETOOLONG;
174 }
175
176 sprintf(path, PXELINUX_DIR "%s", file);
177
Simon Glassb0d08db2021-10-14 12:47:56 -0600178 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100179}
180
Simon Glass00302442021-10-14 12:48:01 -0600181/**
182 * get_relfile_envaddr() - read a file to an address in an env var
183 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100184 * Wrapper to make it easier to store the file at file_path in the location
185 * specified by envaddr_name. file_path will be joined to the bootfile path,
186 * if any is specified.
187 *
Simon Glass00302442021-10-14 12:48:01 -0600188 * @ctx: PXE context
189 * @file_path: File path to read (relative to the PXE file)
190 * @envaddr_name: Name of environment variable which contains the address to
191 * load to
Simon Glassa9401b92021-10-14 12:48:08 -0600192 * @filesizep: Returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -0600193 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
194 * environment variable, -EINVAL if its format is not valid hex, or other
195 * value < 0 on other error
Patrice Chotard17e88042019-11-25 09:07:37 +0100196 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600197static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600198 const char *envaddr_name, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100199{
200 unsigned long file_addr;
201 char *envaddr;
202
203 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100204 if (!envaddr)
205 return -ENOENT;
206
207 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
208 return -EINVAL;
209
Simon Glassa9401b92021-10-14 12:48:08 -0600210 return get_relfile(ctx, file_path, file_addr, filesizep);
Patrice Chotard17e88042019-11-25 09:07:37 +0100211}
212
Simon Glass00302442021-10-14 12:48:01 -0600213/**
214 * label_create() - crate a new PXE label
215 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100216 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
217 * result must be free()'d to reclaim the memory.
218 *
Simon Glass00302442021-10-14 12:48:01 -0600219 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100220 */
221static struct pxe_label *label_create(void)
222{
223 struct pxe_label *label;
224
225 label = malloc(sizeof(struct pxe_label));
Patrice Chotard17e88042019-11-25 09:07:37 +0100226 if (!label)
227 return NULL;
228
229 memset(label, 0, sizeof(struct pxe_label));
230
231 return label;
232}
233
Simon Glass00302442021-10-14 12:48:01 -0600234/**
235 * label_destroy() - free the memory used by a pxe_label
236 *
237 * This frees @label itself as well as memory used by its name,
238 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
239 * they're non-NULL.
Patrice Chotard17e88042019-11-25 09:07:37 +0100240 *
241 * So - be sure to only use dynamically allocated memory for the members of
242 * the pxe_label struct, unless you want to clean it up first. These are
243 * currently only created by the pxe file parsing code.
Simon Glass00302442021-10-14 12:48:01 -0600244 *
245 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100246 */
247static void label_destroy(struct pxe_label *label)
248{
Simon Glass764d0c02021-10-14 12:48:02 -0600249 free(label->name);
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +0200250 free(label->kernel_label);
Simon Glass764d0c02021-10-14 12:48:02 -0600251 free(label->kernel);
252 free(label->config);
253 free(label->append);
254 free(label->initrd);
255 free(label->fdt);
256 free(label->fdtdir);
257 free(label->fdtoverlays);
Patrice Chotard17e88042019-11-25 09:07:37 +0100258 free(label);
259}
260
Simon Glass00302442021-10-14 12:48:01 -0600261/**
262 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100263 *
264 * This is passed as a callback to the menu code for displaying each
265 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600266 *
267 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-11-25 09:07:37 +0100268 */
269static void label_print(void *data)
270{
271 struct pxe_label *label = data;
272 const char *c = label->menu ? label->menu : label->name;
273
274 printf("%s:\t%s\n", label->num, c);
275}
276
Simon Glass00302442021-10-14 12:48:01 -0600277/**
278 * label_localboot() - Boot a label that specified 'localboot'
279 *
280 * This requires that the 'localcmd' environment variable is defined. Its
281 * contents will be executed as U-Boot commands. If the label specified an
282 * 'append' line, its contents will be used to overwrite the contents of the
283 * 'bootargs' environment variable prior to running 'localcmd'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100284 *
Simon Glass00302442021-10-14 12:48:01 -0600285 * @label: Label to process
286 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100287 */
288static int label_localboot(struct pxe_label *label)
289{
290 char *localcmd;
291
292 localcmd = from_env("localcmd");
Patrice Chotard17e88042019-11-25 09:07:37 +0100293 if (!localcmd)
294 return -ENOENT;
295
296 if (label->append) {
297 char bootargs[CONFIG_SYS_CBSIZE];
298
Simon Glassc7b03e82020-11-05 10:33:47 -0700299 cli_simple_process_macros(label->append, bootargs,
300 sizeof(bootargs));
Patrice Chotard17e88042019-11-25 09:07:37 +0100301 env_set("bootargs", bootargs);
302 }
303
304 debug("running: %s\n", localcmd);
305
306 return run_command_list(localcmd, strlen(localcmd), 0);
307}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100308
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800309/*
310 * label_boot_kaslrseed generate kaslrseed from hw rng
311 */
312
313static void label_boot_kaslrseed(void)
314{
Marek Vasut0d871e72024-04-26 01:02:07 +0200315#if CONFIG_IS_ENABLED(DM_RNG)
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800316 ulong fdt_addr;
317 struct fdt_header *working_fdt;
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800318 int err;
319
320 /* Get the main fdt and map it */
321 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
322 working_fdt = map_sysmem(fdt_addr, 0);
323 err = fdt_check_header(working_fdt);
324 if (err)
325 return;
326
327 /* add extra size for holding kaslr-seed */
328 /* err is new fdt size, 0 or negtive */
329 err = fdt_shrink_to_minimum(working_fdt, 512);
330 if (err <= 0)
331 return;
332
Tim Harveyc75fab42024-06-18 14:06:08 -0700333 fdt_kaslrseed(working_fdt, true);
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800334#endif
335 return;
336}
337
Simon Glass00302442021-10-14 12:48:01 -0600338/**
339 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200340 * or 'devicetree-overlay'
Simon Glass00302442021-10-14 12:48:01 -0600341 *
342 * @ctx: PXE context
343 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100344 */
345#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600346static void label_boot_fdtoverlay(struct pxe_context *ctx,
347 struct pxe_label *label)
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100348{
349 char *fdtoverlay = label->fdtoverlays;
350 struct fdt_header *working_fdt;
351 char *fdtoverlay_addr_env;
352 ulong fdtoverlay_addr;
353 ulong fdt_addr;
354 int err;
355
356 /* Get the main fdt and map it */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600357 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100358 working_fdt = map_sysmem(fdt_addr, 0);
359 err = fdt_check_header(working_fdt);
360 if (err)
361 return;
362
363 /* Get the specific overlay loading address */
364 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
365 if (!fdtoverlay_addr_env) {
366 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
367 return;
368 }
369
Simon Glass3ff49ec2021-07-24 09:03:29 -0600370 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100371
372 /* Cycle over the overlay files and apply them in order */
373 do {
374 struct fdt_header *blob;
375 char *overlayfile;
376 char *end;
377 int len;
378
379 /* Drop leading spaces */
380 while (*fdtoverlay == ' ')
381 ++fdtoverlay;
382
383 /* Copy a single filename if multiple provided */
384 end = strstr(fdtoverlay, " ");
385 if (end) {
386 len = (int)(end - fdtoverlay);
387 overlayfile = malloc(len + 1);
388 strncpy(overlayfile, fdtoverlay, len);
389 overlayfile[len] = '\0';
390 } else
391 overlayfile = fdtoverlay;
392
393 if (!strlen(overlayfile))
394 goto skip_overlay;
395
396 /* Load overlay file */
Simon Glassa9401b92021-10-14 12:48:08 -0600397 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
398 NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100399 if (err < 0) {
400 printf("Failed loading overlay %s\n", overlayfile);
401 goto skip_overlay;
402 }
403
404 /* Resize main fdt */
405 fdt_shrink_to_minimum(working_fdt, 8192);
406
407 blob = map_sysmem(fdtoverlay_addr, 0);
408 err = fdt_check_header(blob);
409 if (err) {
410 printf("Invalid overlay %s, skipping\n",
411 overlayfile);
412 goto skip_overlay;
413 }
414
415 err = fdt_overlay_apply_verbose(working_fdt, blob);
416 if (err) {
417 printf("Failed to apply overlay %s, skipping\n",
418 overlayfile);
419 goto skip_overlay;
420 }
421
422skip_overlay:
423 if (end)
424 free(overlayfile);
425 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
426}
427#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100428
Simon Glass00302442021-10-14 12:48:01 -0600429/**
430 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard17e88042019-11-25 09:07:37 +0100431 *
432 * If we can't boot for any reason, we return. A successful boot never
433 * returns.
434 *
435 * The kernel will be stored in the location given by the 'kernel_addr_r'
436 * environment variable.
437 *
438 * If the label specifies an initrd file, it will be stored in the location
439 * given by the 'ramdisk_addr_r' environment variable.
440 *
441 * If the label specifies an 'append' line, its contents will overwrite that
442 * of the 'bootargs' environment variable.
Simon Glass00302442021-10-14 12:48:01 -0600443 *
444 * @ctx: PXE context
445 * @label: Label to process
446 * Returns does not return on success, otherwise returns 0 if a localboot
447 * label was processed, or 1 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100448 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600449static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +0100450{
Tom Rini793921e2024-04-18 08:29:35 -0600451 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
452 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Libc14b932021-10-20 00:18:15 -0700453 char *kernel_addr = NULL;
454 char *initrd_addr_str = NULL;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700455 char initrd_filesize[10];
Zhaofeng Libc14b932021-10-20 00:18:15 -0700456 char initrd_str[28];
Patrice Chotard17e88042019-11-25 09:07:37 +0100457 char mac_str[29] = "";
458 char ip_str[68] = "";
459 char *fit_addr = NULL;
Tom Rini793921e2024-04-18 08:29:35 -0600460 int bootm_argc = 2;
461 int zboot_argc = 3;
462 int len = 0;
463 ulong kernel_addr_r;
464 void *buf;
Patrice Chotard17e88042019-11-25 09:07:37 +0100465
466 label_print(label);
467
468 label->attempted = 1;
469
470 if (label->localboot) {
471 if (label->localboot_val >= 0)
472 label_localboot(label);
473 return 0;
474 }
475
Patrice Chotard6233de42019-11-25 09:07:39 +0100476 if (!label->kernel) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100477 printf("No kernel given, skipping %s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100478 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100479 return 1;
480 }
481
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200482 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
483 NULL) < 0) {
484 printf("Skipping %s for failure retrieving kernel\n",
485 label->name);
486 return 1;
487 }
Simon Glassa9401b92021-10-14 12:48:08 -0600488
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200489 kernel_addr = env_get("kernel_addr_r");
490 /* for FIT, append the configuration identifier */
491 if (label->config) {
492 int len = strlen(kernel_addr) + strlen(label->config) + 1;
493
494 fit_addr = malloc(len);
495 if (!fit_addr) {
496 printf("malloc fail (FIT address)\n");
497 return 1;
498 }
499 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
500 kernel_addr = fit_addr;
501 }
502
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +0200503 /* For FIT, the label can be identical to kernel one */
504 if (label->initrd && !strcmp(label->kernel_label, label->initrd)) {
Tom Rini793921e2024-04-18 08:29:35 -0600505 initrd_addr_str = kernel_addr;
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +0200506 } else if (label->initrd) {
Simon Glassa9401b92021-10-14 12:48:08 -0600507 ulong size;
Simon Glassa9401b92021-10-14 12:48:08 -0600508 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
509 &size) < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100510 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100511 label->name);
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200512 goto cleanup;
Patrice Chotard17e88042019-11-25 09:07:37 +0100513 }
Thomas Mittelstaedtca65d232023-05-04 13:42:55 +0000514 strcpy(initrd_filesize, simple_xtoa(size));
Zhaofeng Libc14b932021-10-20 00:18:15 -0700515 initrd_addr_str = env_get("ramdisk_addr_r");
Heinrich Schuchardta8d6aed2021-11-15 19:26:51 +0100516 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
517 initrd_addr_str, size);
518 if (size >= sizeof(initrd_str))
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200519 goto cleanup;
Patrice Chotard17e88042019-11-25 09:07:37 +0100520 }
521
522 if (label->ipappend & 0x1) {
523 sprintf(ip_str, " ip=%s:%s:%s:%s",
524 env_get("ipaddr"), env_get("serverip"),
525 env_get("gatewayip"), env_get("netmask"));
526 }
527
Kory Maincentcaabd242021-02-02 16:42:28 +0100528 if (IS_ENABLED(CONFIG_CMD_NET)) {
529 if (label->ipappend & 0x2) {
530 int err;
Patrice Chotard6233de42019-11-25 09:07:39 +0100531
Kory Maincentcaabd242021-02-02 16:42:28 +0100532 strcpy(mac_str, " BOOTIF=");
533 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
534 if (err < 0)
535 mac_str[0] = '\0';
536 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100537 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100538
539 if ((label->ipappend & 0x3) || label->append) {
540 char bootargs[CONFIG_SYS_CBSIZE] = "";
541 char finalbootargs[CONFIG_SYS_CBSIZE];
542
543 if (strlen(label->append ?: "") +
544 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
545 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
546 strlen(label->append ?: ""),
547 strlen(ip_str), strlen(mac_str),
548 sizeof(bootargs));
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200549 goto cleanup;
Patrice Chotard17e88042019-11-25 09:07:37 +0100550 }
Patrice Chotard6233de42019-11-25 09:07:39 +0100551
552 if (label->append)
Tom Rini793921e2024-04-18 08:29:35 -0600553 strncpy(bootargs, label->append, sizeof(bootargs));
Patrice Chotard6233de42019-11-25 09:07:39 +0100554
555 strcat(bootargs, ip_str);
556 strcat(bootargs, mac_str);
557
Simon Glassc7b03e82020-11-05 10:33:47 -0700558 cli_simple_process_macros(bootargs, finalbootargs,
559 sizeof(finalbootargs));
Patrice Chotard6233de42019-11-25 09:07:39 +0100560 env_set("bootargs", finalbootargs);
561 printf("append: %s\n", finalbootargs);
Patrice Chotard17e88042019-11-25 09:07:37 +0100562 }
563
Tom Rini793921e2024-04-18 08:29:35 -0600564 /*
565 * fdt usage is optional:
566 * It handles the following scenarios.
567 *
568 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
569 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
570 * bootm, and adjust argc appropriately.
571 *
572 * If retrieve fails and no exact fdt blob is specified in pxe file with
573 * "fdt" label, try Scenario 2.
574 *
575 * Scenario 2: If there is an fdt_addr specified, pass it along to
576 * bootm, and adjust argc appropriately.
577 *
578 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
579 * bootm, and adjust argc appropriately, unless the image type is fitImage.
580 *
581 * Scenario 4: fdt blob is not available.
582 */
583 bootm_argv[3] = env_get("fdt_addr_r");
584
585 /* For FIT, the label can be identical to kernel one */
586 if (label->fdt && !strcmp(label->kernel_label, label->fdt)) {
587 bootm_argv[3] = kernel_addr;
588 /* if fdt label is defined then get fdt from server */
589 } else if (bootm_argv[3]) {
590 char *fdtfile = NULL;
591 char *fdtfilefree = NULL;
592
593 if (label->fdt) {
594 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
595 if (strcmp("-", label->fdt))
596 fdtfile = label->fdt;
597 } else {
598 fdtfile = label->fdt;
599 }
600 } else if (label->fdtdir) {
601 char *f1, *f2, *f3, *f4, *slash;
602
603 f1 = env_get("fdtfile");
604 if (f1) {
605 f2 = "";
606 f3 = "";
607 f4 = "";
608 } else {
609 /*
610 * For complex cases where this code doesn't
611 * generate the correct filename, the board
612 * code should set $fdtfile during early boot,
613 * or the boot scripts should set $fdtfile
614 * before invoking "pxe" or "sysboot".
615 */
616 f1 = env_get("soc");
617 f2 = "-";
618 f3 = env_get("board");
619 f4 = ".dtb";
620 if (!f1) {
621 f1 = "";
622 f2 = "";
623 }
624 if (!f3) {
625 f2 = "";
626 f3 = "";
627 }
628 }
629
630 len = strlen(label->fdtdir);
631 if (!len)
632 slash = "./";
633 else if (label->fdtdir[len - 1] != '/')
634 slash = "/";
635 else
636 slash = "";
637
638 len = strlen(label->fdtdir) + strlen(slash) +
639 strlen(f1) + strlen(f2) + strlen(f3) +
640 strlen(f4) + 1;
641 fdtfilefree = malloc(len);
642 if (!fdtfilefree) {
643 printf("malloc fail (FDT filename)\n");
644 goto cleanup;
645 }
646
647 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
648 label->fdtdir, slash, f1, f2, f3, f4);
649 fdtfile = fdtfilefree;
650 }
651
652 if (fdtfile) {
653 int err = get_relfile_envaddr(ctx, fdtfile,
654 "fdt_addr_r", NULL);
655
656 free(fdtfilefree);
657 if (err < 0) {
658 bootm_argv[3] = NULL;
659
660 if (label->fdt) {
661 printf("Skipping %s for failure retrieving FDT\n",
662 label->name);
663 goto cleanup;
664 }
665
666 if (label->fdtdir) {
667 printf("Skipping fdtdir %s for failure retrieving dts\n",
668 label->fdtdir);
669 }
670 }
671
672 if (label->kaslrseed)
673 label_boot_kaslrseed();
674
675#ifdef CONFIG_OF_LIBFDT_OVERLAY
676 if (label->fdtoverlays)
677 label_boot_fdtoverlay(ctx, label);
678#endif
679 } else {
680 bootm_argv[3] = NULL;
681 }
682 }
683
684 bootm_argv[1] = kernel_addr;
685 zboot_argv[1] = kernel_addr;
686
687 if (initrd_addr_str) {
688 bootm_argv[2] = initrd_str;
689 bootm_argc = 3;
690
691 zboot_argv[3] = initrd_addr_str;
692 zboot_argv[4] = initrd_filesize;
693 zboot_argc = 5;
694 }
695
696 if (!bootm_argv[3]) {
697 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
698 if (strcmp("-", label->fdt))
699 bootm_argv[3] = env_get("fdt_addr");
700 } else {
701 bootm_argv[3] = env_get("fdt_addr");
702 }
703 }
704
705 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
706 buf = map_sysmem(kernel_addr_r, 0);
707
708 if (!bootm_argv[3] && genimg_get_format(buf) != IMAGE_FORMAT_FIT) {
709 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS)) {
710 if (strcmp("-", label->fdt))
711 bootm_argv[3] = env_get("fdtcontroladdr");
712 } else {
713 bootm_argv[3] = env_get("fdtcontroladdr");
714 }
715 }
716
717 if (bootm_argv[3]) {
718 if (!bootm_argv[2])
719 bootm_argv[2] = "-";
720 bootm_argc = 4;
721 }
722
723 /* Try bootm for legacy and FIT format image */
724 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
Simon Glass9b2c78c2024-06-19 06:34:50 -0600725 IS_ENABLED(CONFIG_CMD_BOOTM)) {
726 log_debug("using bootm\n");
Tom Rini793921e2024-04-18 08:29:35 -0600727 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
728 /* Try booting an AArch64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600729 } else if (IS_ENABLED(CONFIG_CMD_BOOTI)) {
730 log_debug("using booti\n");
Tom Rini793921e2024-04-18 08:29:35 -0600731 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
732 /* Try booting a Image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600733 } else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) {
734 log_debug("using bootz\n");
Tom Rini793921e2024-04-18 08:29:35 -0600735 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
736 /* Try booting an x86_64 Linux kernel image */
Simon Glass9b2c78c2024-06-19 06:34:50 -0600737 } else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) {
738 log_debug("using zboot\n");
Tom Rini793921e2024-04-18 08:29:35 -0600739 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Simon Glass9b2c78c2024-06-19 06:34:50 -0600740 }
Tom Rini793921e2024-04-18 08:29:35 -0600741
742 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +0100743
744cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600745 free(fit_addr);
746
Patrice Chotard17e88042019-11-25 09:07:37 +0100747 return 1;
748}
749
Simon Glass00302442021-10-14 12:48:01 -0600750/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100751enum token_type {
752 T_EOL,
753 T_STRING,
754 T_EOF,
755 T_MENU,
756 T_TITLE,
757 T_TIMEOUT,
758 T_LABEL,
759 T_KERNEL,
760 T_LINUX,
761 T_APPEND,
762 T_INITRD,
763 T_LOCALBOOT,
764 T_DEFAULT,
765 T_PROMPT,
766 T_INCLUDE,
767 T_FDT,
768 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100769 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100770 T_ONTIMEOUT,
771 T_IPAPPEND,
772 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800773 T_KASLRSEED,
Martyn Welch53dd5102024-10-09 14:15:38 +0100774 T_FALLBACK,
Patrice Chotard17e88042019-11-25 09:07:37 +0100775 T_INVALID
776};
777
Simon Glass00302442021-10-14 12:48:01 -0600778/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100779struct token {
780 char *val;
781 enum token_type type;
782};
783
Simon Glass00302442021-10-14 12:48:01 -0600784/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100785static const struct token keywords[] = {
786 {"menu", T_MENU},
787 {"title", T_TITLE},
788 {"timeout", T_TIMEOUT},
789 {"default", T_DEFAULT},
790 {"prompt", T_PROMPT},
791 {"label", T_LABEL},
792 {"kernel", T_KERNEL},
793 {"linux", T_LINUX},
794 {"localboot", T_LOCALBOOT},
795 {"append", T_APPEND},
796 {"initrd", T_INITRD},
797 {"include", T_INCLUDE},
798 {"devicetree", T_FDT},
799 {"fdt", T_FDT},
800 {"devicetreedir", T_FDTDIR},
801 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100802 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200803 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100804 {"ontimeout", T_ONTIMEOUT,},
805 {"ipappend", T_IPAPPEND,},
806 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800807 {"kaslrseed", T_KASLRSEED,},
Martyn Welch53dd5102024-10-09 14:15:38 +0100808 {"fallback", T_FALLBACK,},
Patrice Chotard17e88042019-11-25 09:07:37 +0100809 {NULL, T_INVALID}
810};
811
Simon Glass00302442021-10-14 12:48:01 -0600812/**
813 * enum lex_state - lexer state
814 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100815 * Since pxe(linux) files don't have a token to identify the start of a
816 * literal, we have to keep track of when we're in a state where a literal is
817 * expected vs when we're in a state a keyword is expected.
818 */
819enum lex_state {
820 L_NORMAL = 0,
821 L_KEYWORD,
822 L_SLITERAL
823};
824
Simon Glass00302442021-10-14 12:48:01 -0600825/**
826 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100827 *
Simon Glass00302442021-10-14 12:48:01 -0600828 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100829 *
830 * Characters from *p are copied into t-val until a character equal to
831 * delim is found, or a NUL byte is reached. If delim has the special value of
832 * ' ', any whitespace character will be used as a delimiter.
833 *
834 * If lower is unequal to 0, uppercase characters will be converted to
835 * lowercase in the result. This is useful to make keywords case
836 * insensitive.
837 *
838 * The location of *p is updated to point to the first character after the end
839 * of the token - the ending delimiter.
840 *
Simon Glass00302442021-10-14 12:48:01 -0600841 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
842 * it.
843 *
844 * @p: Points to a pointer to the current position in the input being processed.
845 * Updated to point at the first character after the current token
846 * @t: Pointers to a token to fill in
847 * @delim: Delimiter character to look for, either newline or space
848 * @lower: true to convert the string to lower case when storing
849 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100850 */
851static char *get_string(char **p, struct token *t, char delim, int lower)
852{
853 char *b, *e;
854 size_t len, i;
855
856 /*
857 * b and e both start at the beginning of the input stream.
858 *
859 * e is incremented until we find the ending delimiter, or a NUL byte
860 * is reached. Then, we take e - b to find the length of the token.
861 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100862 b = *p;
863 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100864 while (*e) {
865 if ((delim == ' ' && isspace(*e)) || delim == *e)
866 break;
867 e++;
868 }
869
870 len = e - b;
871
872 /*
873 * Allocate memory to hold the string, and copy it in, converting
874 * characters to lowercase if lower is != 0.
875 */
876 t->val = malloc(len + 1);
877 if (!t->val)
878 return NULL;
879
880 for (i = 0; i < len; i++, b++) {
881 if (lower)
882 t->val[i] = tolower(*b);
883 else
884 t->val[i] = *b;
885 }
886
887 t->val[len] = '\0';
888
Simon Glass764d0c02021-10-14 12:48:02 -0600889 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100890 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100891 t->type = T_STRING;
892
893 return t->val;
894}
895
Simon Glass00302442021-10-14 12:48:01 -0600896/**
897 * get_keyword() - Populate a keyword token with a type and value
898 *
899 * Updates the ->type field based on the keyword string in @val
900 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100901 */
902static void get_keyword(struct token *t)
903{
904 int i;
905
906 for (i = 0; keywords[i].val; i++) {
907 if (!strcmp(t->val, keywords[i].val)) {
908 t->type = keywords[i].type;
909 break;
910 }
911 }
912}
913
Simon Glass00302442021-10-14 12:48:01 -0600914/**
915 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100916 *
Simon Glass00302442021-10-14 12:48:01 -0600917 * We have to keep track of which state we're in to know if we're looking to get
918 * a string literal or a keyword.
919 *
920 * @p: Points to a pointer to the current position in the input being processed.
921 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100922 */
923static void get_token(char **p, struct token *t, enum lex_state state)
924{
925 char *c = *p;
926
927 t->type = T_INVALID;
928
929 /* eat non EOL whitespace */
930 while (isblank(*c))
931 c++;
932
933 /*
934 * eat comments. note that string literals can't begin with #, but
935 * can contain a # after their first character.
936 */
937 if (*c == '#') {
938 while (*c && *c != '\n')
939 c++;
940 }
941
942 if (*c == '\n') {
943 t->type = T_EOL;
944 c++;
945 } else if (*c == '\0') {
946 t->type = T_EOF;
947 c++;
948 } else if (state == L_SLITERAL) {
949 get_string(&c, t, '\n', 0);
950 } else if (state == L_KEYWORD) {
951 /*
952 * when we expect a keyword, we first get the next string
953 * token delimited by whitespace, and then check if it
954 * matches a keyword in our keyword list. if it does, it's
955 * converted to a keyword token of the appropriate type, and
956 * if not, it remains a string token.
957 */
958 get_string(&c, t, ' ', 1);
959 get_keyword(t);
960 }
961
962 *p = c;
963}
964
Simon Glass00302442021-10-14 12:48:01 -0600965/**
966 * eol_or_eof() - Find end of line
967 *
968 * Increment *c until we get to the end of the current line, or EOF
969 *
970 * @c: Points to a pointer to the current position in the input being processed.
971 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100972 */
973static void eol_or_eof(char **c)
974{
975 while (**c && **c != '\n')
976 (*c)++;
977}
978
979/*
980 * All of these parse_* functions share some common behavior.
981 *
982 * They finish with *c pointing after the token they parse, and return 1 on
983 * success, or < 0 on error.
984 */
985
986/*
987 * Parse a string literal and store a pointer it at *dst. String literals
988 * terminate at the end of the line.
989 */
990static int parse_sliteral(char **c, char **dst)
991{
992 struct token t;
993 char *s = *c;
994
995 get_token(c, &t, L_SLITERAL);
996
997 if (t.type != T_STRING) {
998 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
999 return -EINVAL;
1000 }
1001
1002 *dst = t.val;
1003
1004 return 1;
1005}
1006
1007/*
1008 * Parse a base 10 (unsigned) integer and store it at *dst.
1009 */
1010static int parse_integer(char **c, int *dst)
1011{
1012 struct token t;
1013 char *s = *c;
1014
1015 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001016 if (t.type != T_STRING) {
1017 printf("Expected string: %.*s\n", (int)(*c - s), s);
1018 return -EINVAL;
1019 }
1020
1021 *dst = simple_strtol(t.val, NULL, 10);
1022
1023 free(t.val);
1024
1025 return 1;
1026}
1027
Simon Glassb0d08db2021-10-14 12:47:56 -06001028static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001029 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001030
1031/*
1032 * Parse an include statement, and retrieve and parse the file it mentions.
1033 *
1034 * base should point to a location where it's safe to store the file, and
1035 * nest_level should indicate how many nested includes have occurred. For this
1036 * include, nest_level has already been incremented and doesn't need to be
1037 * incremented here.
1038 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001039static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001040 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001041{
1042 char *include_path;
1043 char *s = *c;
1044 int err;
1045 char *buf;
1046 int ret;
1047
1048 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001049 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001050 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001051 return err;
1052 }
1053
Simon Glassb0d08db2021-10-14 12:47:56 -06001054 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001055 if (err < 0) {
1056 printf("Couldn't retrieve %s\n", include_path);
1057 return err;
1058 }
1059
1060 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001061 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001062 unmap_sysmem(buf);
1063
1064 return ret;
1065}
1066
1067/*
1068 * Parse lines that begin with 'menu'.
1069 *
1070 * base and nest are provided to handle the 'menu include' case.
1071 *
1072 * base should point to a location where it's safe to store the included file.
1073 *
1074 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1075 * a file it includes, 3 when parsing a file included by that file, and so on.
1076 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001077static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001078 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001079{
1080 struct token t;
1081 char *s = *c;
1082 int err = 0;
1083
1084 get_token(c, &t, L_KEYWORD);
1085
1086 switch (t.type) {
1087 case T_TITLE:
1088 err = parse_sliteral(c, &cfg->title);
1089
1090 break;
1091
1092 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001093 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001094 break;
1095
1096 case T_BACKGROUND:
1097 err = parse_sliteral(c, &cfg->bmp);
1098 break;
1099
1100 default:
1101 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001102 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001103 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001104 if (err < 0)
1105 return err;
1106
1107 eol_or_eof(c);
1108
1109 return 1;
1110}
1111
1112/*
1113 * Handles parsing a 'menu line' when we're parsing a label.
1114 */
1115static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001116 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001117{
1118 struct token t;
1119 char *s;
1120
1121 s = *c;
1122
1123 get_token(c, &t, L_KEYWORD);
1124
1125 switch (t.type) {
1126 case T_DEFAULT:
1127 if (!cfg->default_label)
1128 cfg->default_label = strdup(label->name);
1129
1130 if (!cfg->default_label)
1131 return -ENOMEM;
1132
1133 break;
1134 case T_LABEL:
1135 parse_sliteral(c, &label->menu);
1136 break;
1137 default:
1138 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001139 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001140 }
1141
1142 eol_or_eof(c);
1143
1144 return 0;
1145}
1146
1147/*
1148 * Handles parsing a 'kernel' label.
1149 * expecting "filename" or "<fit_filename>#cfg"
1150 */
1151static int parse_label_kernel(char **c, struct pxe_label *label)
1152{
1153 char *s;
1154 int err;
1155
1156 err = parse_sliteral(c, &label->kernel);
1157 if (err < 0)
1158 return err;
1159
Patrick Delaunay41c7e4a2022-10-28 11:01:19 +02001160 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1161 label->kernel_label = strdup(label->kernel);
1162 if (!label->kernel_label)
1163 return -ENOMEM;
1164
Patrice Chotard17e88042019-11-25 09:07:37 +01001165 s = strstr(label->kernel, "#");
1166 if (!s)
1167 return 1;
1168
Patrick Delaunay9d464f82022-10-28 11:01:20 +02001169 label->config = strdup(s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001170 if (!label->config)
1171 return -ENOMEM;
1172
Patrice Chotard17e88042019-11-25 09:07:37 +01001173 *s = 0;
1174
1175 return 1;
1176}
1177
1178/*
1179 * Parses a label and adds it to the list of labels for a menu.
1180 *
1181 * A label ends when we either get to the end of a file, or
1182 * get some input we otherwise don't have a handler defined
1183 * for.
1184 *
1185 */
1186static int parse_label(char **c, struct pxe_menu *cfg)
1187{
1188 struct token t;
1189 int len;
1190 char *s = *c;
1191 struct pxe_label *label;
1192 int err;
1193
1194 label = label_create();
1195 if (!label)
1196 return -ENOMEM;
1197
1198 err = parse_sliteral(c, &label->name);
1199 if (err < 0) {
1200 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1201 label_destroy(label);
1202 return -EINVAL;
1203 }
1204
1205 list_add_tail(&label->list, &cfg->labels);
1206
1207 while (1) {
1208 s = *c;
1209 get_token(c, &t, L_KEYWORD);
1210
1211 err = 0;
1212 switch (t.type) {
1213 case T_MENU:
1214 err = parse_label_menu(c, cfg, label);
1215 break;
1216
1217 case T_KERNEL:
1218 case T_LINUX:
1219 err = parse_label_kernel(c, label);
1220 break;
1221
1222 case T_APPEND:
1223 err = parse_sliteral(c, &label->append);
1224 if (label->initrd)
1225 break;
1226 s = strstr(label->append, "initrd=");
1227 if (!s)
1228 break;
1229 s += 7;
1230 len = (int)(strchr(s, ' ') - s);
1231 label->initrd = malloc(len + 1);
1232 strncpy(label->initrd, s, len);
1233 label->initrd[len] = '\0';
1234
1235 break;
1236
1237 case T_INITRD:
1238 if (!label->initrd)
1239 err = parse_sliteral(c, &label->initrd);
1240 break;
1241
1242 case T_FDT:
1243 if (!label->fdt)
1244 err = parse_sliteral(c, &label->fdt);
1245 break;
1246
1247 case T_FDTDIR:
1248 if (!label->fdtdir)
1249 err = parse_sliteral(c, &label->fdtdir);
1250 break;
1251
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001252 case T_FDTOVERLAYS:
1253 if (!label->fdtoverlays)
1254 err = parse_sliteral(c, &label->fdtoverlays);
1255 break;
1256
Patrice Chotard17e88042019-11-25 09:07:37 +01001257 case T_LOCALBOOT:
1258 label->localboot = 1;
1259 err = parse_integer(c, &label->localboot_val);
1260 break;
1261
1262 case T_IPAPPEND:
1263 err = parse_integer(c, &label->ipappend);
1264 break;
1265
Zhang Ning9c1d9c52022-02-01 08:33:37 +08001266 case T_KASLRSEED:
1267 label->kaslrseed = 1;
1268 break;
1269
Patrice Chotard17e88042019-11-25 09:07:37 +01001270 case T_EOL:
1271 break;
1272
1273 default:
1274 /*
1275 * put the token back! we don't want it - it's the end
1276 * of a label and whatever token this is, it's
1277 * something for the menu level context to handle.
1278 */
1279 *c = s;
1280 return 1;
1281 }
1282
1283 if (err < 0)
1284 return err;
1285 }
1286}
1287
1288/*
1289 * This 16 comes from the limit pxelinux imposes on nested includes.
1290 *
1291 * There is no reason at all we couldn't do more, but some limit helps prevent
1292 * infinite (until crash occurs) recursion if a file tries to include itself.
1293 */
1294#define MAX_NEST_LEVEL 16
1295
1296/*
1297 * Entry point for parsing a menu file. nest_level indicates how many times
1298 * we've nested in includes. It will be 1 for the top level menu file.
1299 *
1300 * Returns 1 on success, < 0 on error.
1301 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001302static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001303 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001304{
1305 struct token t;
1306 char *s, *b, *label_name;
1307 int err;
1308
1309 b = p;
1310
1311 if (nest_level > MAX_NEST_LEVEL) {
1312 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1313 return -EMLINK;
1314 }
1315
1316 while (1) {
1317 s = p;
1318
1319 get_token(&p, &t, L_KEYWORD);
1320
1321 err = 0;
1322 switch (t.type) {
1323 case T_MENU:
1324 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001325 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001326 base + ALIGN(strlen(b) + 1, 4),
1327 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001328 break;
1329
1330 case T_TIMEOUT:
1331 err = parse_integer(&p, &cfg->timeout);
1332 break;
1333
1334 case T_LABEL:
1335 err = parse_label(&p, cfg);
1336 break;
1337
1338 case T_DEFAULT:
1339 case T_ONTIMEOUT:
1340 err = parse_sliteral(&p, &label_name);
1341
1342 if (label_name) {
1343 if (cfg->default_label)
1344 free(cfg->default_label);
1345
1346 cfg->default_label = label_name;
1347 }
1348
1349 break;
1350
Martyn Welch53dd5102024-10-09 14:15:38 +01001351 case T_FALLBACK:
1352 err = parse_sliteral(&p, &label_name);
1353
1354 if (label_name) {
1355 if (cfg->fallback_label)
1356 free(cfg->fallback_label);
1357
1358 cfg->fallback_label = label_name;
1359 }
1360
1361 break;
1362
Patrice Chotard17e88042019-11-25 09:07:37 +01001363 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001364 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001365 base + ALIGN(strlen(b), 4), cfg,
1366 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001367 break;
1368
1369 case T_PROMPT:
Manuel Traut20081242022-11-18 09:00:27 +01001370 err = parse_integer(&p, &cfg->prompt);
1371 // Do not fail if prompt configuration is undefined
1372 if (err < 0)
1373 eol_or_eof(&p);
Patrice Chotard17e88042019-11-25 09:07:37 +01001374 break;
1375
1376 case T_EOL:
1377 break;
1378
1379 case T_EOF:
1380 return 1;
1381
1382 default:
1383 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001384 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001385 eol_or_eof(&p);
1386 }
1387
1388 if (err < 0)
1389 return err;
1390 }
1391}
1392
1393/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001394 */
1395void destroy_pxe_menu(struct pxe_menu *cfg)
1396{
1397 struct list_head *pos, *n;
1398 struct pxe_label *label;
1399
Simon Glass764d0c02021-10-14 12:48:02 -06001400 free(cfg->title);
1401 free(cfg->default_label);
Martyn Welch53dd5102024-10-09 14:15:38 +01001402 free(cfg->fallback_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001403
1404 list_for_each_safe(pos, n, &cfg->labels) {
1405 label = list_entry(pos, struct pxe_label, list);
1406
1407 label_destroy(label);
1408 }
1409
1410 free(cfg);
1411}
1412
Simon Glassb0d08db2021-10-14 12:47:56 -06001413struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001414{
1415 struct pxe_menu *cfg;
1416 char *buf;
1417 int r;
1418
1419 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001420 if (!cfg)
1421 return NULL;
1422
1423 memset(cfg, 0, sizeof(struct pxe_menu));
1424
1425 INIT_LIST_HEAD(&cfg->labels);
1426
1427 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001428 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Martyn Welch2c47aac2024-10-09 14:15:39 +01001429
1430 if (ctx->use_fallback) {
1431 if (cfg->fallback_label) {
1432 printf("Setting use of fallback\n");
1433 cfg->default_label = cfg->fallback_label;
1434 } else {
1435 printf("Selected fallback option, but not set\n");
1436 }
1437 }
1438
Patrice Chotard17e88042019-11-25 09:07:37 +01001439 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001440 if (r < 0) {
1441 destroy_pxe_menu(cfg);
1442 return NULL;
1443 }
1444
1445 return cfg;
1446}
1447
1448/*
1449 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1450 * menu code.
1451 */
1452static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1453{
1454 struct pxe_label *label;
1455 struct list_head *pos;
1456 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001457 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001458 int err;
1459 int i = 1;
1460 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001461 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001462
1463 /*
1464 * Create a menu and add items for all the labels.
1465 */
1466 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
developerc2a1e9e2024-10-29 17:47:16 +08001467 cfg->prompt, NULL, label_print, NULL, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001468 if (!m)
1469 return NULL;
1470
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001471 label_override = env_get("pxe_label_override");
1472
Patrice Chotard17e88042019-11-25 09:07:37 +01001473 list_for_each(pos, &cfg->labels) {
1474 label = list_entry(pos, struct pxe_label, list);
1475
1476 sprintf(label->num, "%d", i++);
1477 if (menu_item_add(m, label->num, label) != 1) {
1478 menu_destroy(m);
1479 return NULL;
1480 }
1481 if (cfg->default_label &&
1482 (strcmp(label->name, cfg->default_label) == 0))
1483 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001484 if (label_override && !strcmp(label->name, label_override))
1485 override_num = label->num;
1486 }
1487
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001488 if (label_override) {
1489 if (override_num)
1490 default_num = override_num;
1491 else
1492 printf("Missing override pxe label: %s\n",
1493 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001494 }
1495
1496 /*
1497 * After we've created items for each label in the menu, set the
1498 * menu's default label if one was specified.
1499 */
1500 if (default_num) {
1501 err = menu_default_set(m, default_num);
1502 if (err != 1) {
1503 if (err != -ENOENT) {
1504 menu_destroy(m);
1505 return NULL;
1506 }
1507
1508 printf("Missing default: %s\n", cfg->default_label);
1509 }
1510 }
1511
1512 return m;
1513}
1514
1515/*
1516 * Try to boot any labels we have yet to attempt to boot.
1517 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001518static void boot_unattempted_labels(struct pxe_context *ctx,
1519 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001520{
1521 struct list_head *pos;
1522 struct pxe_label *label;
1523
1524 list_for_each(pos, &cfg->labels) {
1525 label = list_entry(pos, struct pxe_label, list);
1526
1527 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001528 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001529 }
1530}
1531
Simon Glassb0d08db2021-10-14 12:47:56 -06001532void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001533{
1534 void *choice;
1535 struct menu *m;
1536 int err;
1537
Kory Maincentcaabd242021-02-02 16:42:28 +01001538 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1539 /* display BMP if available */
1540 if (cfg->bmp) {
Simon Glassa9401b92021-10-14 12:48:08 -06001541 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001542#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001543 struct udevice *dev;
1544
1545 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1546 if (!err)
1547 video_clear(dev);
1548#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001549 bmp_display(image_load_addr,
1550 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1551 } else {
1552 printf("Skipping background bmp %s for failure\n",
1553 cfg->bmp);
1554 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001555 }
1556 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001557
1558 m = pxe_menu_to_menu(cfg);
1559 if (!m)
1560 return;
1561
1562 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001563 menu_destroy(m);
1564
1565 /*
1566 * err == 1 means we got a choice back from menu_get_choice.
1567 *
1568 * err == -ENOENT if the menu was setup to select the default but no
1569 * default was set. in that case, we should continue trying to boot
1570 * labels that haven't been attempted yet.
1571 *
1572 * otherwise, the user interrupted or there was some other error and
1573 * we give up.
1574 */
1575
1576 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001577 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001578 if (!err)
1579 return;
1580 } else if (err != -ENOENT) {
1581 return;
1582 }
1583
Simon Glassb0d08db2021-10-14 12:47:56 -06001584 boot_unattempted_labels(ctx, cfg);
1585}
1586
Simon Glasse719fe02021-10-14 12:48:04 -06001587int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1588 pxe_getfile_func getfile, void *userdata,
Martyn Welch2c47aac2024-10-09 14:15:39 +01001589 bool allow_abs_path, const char *bootfile, bool use_ipv6,
1590 bool use_fallback)
Simon Glassb0d08db2021-10-14 12:47:56 -06001591{
Simon Glasse719fe02021-10-14 12:48:04 -06001592 const char *last_slash;
1593 size_t path_len = 0;
1594
1595 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001596 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001597 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001598 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001599 ctx->allow_abs_path = allow_abs_path;
Sean Edmondba802862023-04-11 10:48:47 -07001600 ctx->use_ipv6 = use_ipv6;
Martyn Welch2c47aac2024-10-09 14:15:39 +01001601 ctx->use_fallback = use_fallback;
Simon Glasse719fe02021-10-14 12:48:04 -06001602
1603 /* figure out the boot directory, if there is one */
1604 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1605 return -ENOSPC;
1606 ctx->bootdir = strdup(bootfile ? bootfile : "");
1607 if (!ctx->bootdir)
1608 return -ENOMEM;
1609
1610 if (bootfile) {
1611 last_slash = strrchr(bootfile, '/');
1612 if (last_slash)
1613 path_len = (last_slash - bootfile) + 1;
1614 }
1615 ctx->bootdir[path_len] = '\0';
1616
1617 return 0;
1618}
1619
1620void pxe_destroy_ctx(struct pxe_context *ctx)
1621{
1622 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001623}
Simon Glass791bbfe2021-10-14 12:48:03 -06001624
1625int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1626{
1627 struct pxe_menu *cfg;
1628
1629 cfg = parse_pxefile(ctx, pxefile_addr_r);
1630 if (!cfg) {
1631 printf("Error parsing config file\n");
1632 return 1;
1633 }
1634
1635 if (prompt)
1636 cfg->prompt = 1;
1637
1638 handle_pxe_menu(ctx, cfg);
1639
1640 destroy_pxe_menu(cfg);
1641
1642 return 0;
1643}