blob: fc453bd53973050195bd6351a796c6ba23d5acdc [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
7#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01009#include <dm.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010010#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010013#include <malloc.h>
14#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010016#include <fdt_support.h>
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +010017#include <video.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010018#include <linux/libfdt.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010019#include <linux/string.h>
20#include <linux/ctype.h>
21#include <errno.h>
22#include <linux/list.h>
23
Zhang Ning9c1d9c52022-02-01 08:33:37 +080024#ifdef CONFIG_DM_RNG
Zhang Ning9c1d9c52022-02-01 08:33:37 +080025#include <rng.h>
26#endif
27
Patrice Chotard17e88042019-11-25 09:07:37 +010028#include <splash.h>
29#include <asm/io.h>
30
31#include "menu.h"
32#include "cli.h"
33
34#include "pxe_utils.h"
35
Ben Wolsieffer6273f132019-11-28 00:07:08 -050036#define MAX_TFTP_PATH_LEN 512
Patrice Chotard17e88042019-11-25 09:07:37 +010037
Simon Glassa9401b92021-10-14 12:48:08 -060038int pxe_get_file_size(ulong *sizep)
39{
40 const char *val;
41
42 val = from_env("filesize");
43 if (!val)
44 return -ENOENT;
45
46 if (strict_strtoul(val, 16, sizep) < 0)
47 return -EINVAL;
48
49 return 0;
50}
51
Simon Glass00302442021-10-14 12:48:01 -060052/**
53 * format_mac_pxe() - obtain a MAC address in the PXE format
54 *
55 * This produces a MAC-address string in the format for the current ethernet
56 * device:
57 *
58 * 01-aa-bb-cc-dd-ee-ff
59 *
60 * where aa-ff is the MAC address in hex
61 *
62 * @outbuf: Buffer to write string to
63 * @outbuf_len: length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010064 * Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
Simon Glass00302442021-10-14 12:48:01 -060065 * current ethernet device
66 */
Patrice Chotard17e88042019-11-25 09:07:37 +010067int format_mac_pxe(char *outbuf, size_t outbuf_len)
68{
69 uchar ethaddr[6];
70
71 if (outbuf_len < 21) {
72 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Simon Glass00302442021-10-14 12:48:01 -060073 return -ENOSPC;
Patrice Chotard17e88042019-11-25 09:07:37 +010074 }
75
76 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
77 return -ENOENT;
78
79 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
80 ethaddr[0], ethaddr[1], ethaddr[2],
81 ethaddr[3], ethaddr[4], ethaddr[5]);
82
83 return 1;
84}
85
Simon Glass00302442021-10-14 12:48:01 -060086/**
Simon Glass00302442021-10-14 12:48:01 -060087 * get_relfile() - read a file relative to the PXE file
88 *
Patrice Chotard17e88042019-11-25 09:07:37 +010089 * As in pxelinux, paths to files referenced from files we retrieve are
90 * relative to the location of bootfile. get_relfile takes such a path and
91 * joins it with the bootfile path to get the full path to the target file. If
92 * the bootfile path is NULL, we use file_path as is.
93 *
Simon Glass00302442021-10-14 12:48:01 -060094 * @ctx: PXE context
95 * @file_path: File path to read (relative to the PXE file)
96 * @file_addr: Address to load file to
Simon Glassa9401b92021-10-14 12:48:08 -060097 * @filesizep: If not NULL, returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -060098 * Returns 1 for success, or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +010099 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600100static int get_relfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600101 unsigned long file_addr, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100102{
103 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100104 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100105 char addr_buf[18];
Simon Glassa9401b92021-10-14 12:48:08 -0600106 ulong size;
107 int ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100108
Simon Glass5e3e39a2021-10-14 12:48:05 -0600109 if (file_path[0] == '/' && ctx->allow_abs_path)
110 *relfile = '\0';
111 else
112 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
Patrice Chotard17e88042019-11-25 09:07:37 +0100113
Simon Glass5e3e39a2021-10-14 12:48:05 -0600114 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard17e88042019-11-25 09:07:37 +0100115
116 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100117 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100118
119 return -ENAMETOOLONG;
120 }
121
122 strcat(relfile, file_path);
123
124 printf("Retrieving file: %s\n", relfile);
125
126 sprintf(addr_buf, "%lx", file_addr);
127
Simon Glassa9401b92021-10-14 12:48:08 -0600128 ret = ctx->getfile(ctx, relfile, addr_buf, &size);
129 if (ret < 0)
130 return log_msg_ret("get", ret);
131 if (filesizep)
132 *filesizep = size;
133
134 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100135}
136
Simon Glass00302442021-10-14 12:48:01 -0600137/**
138 * get_pxe_file() - read a file
139 *
140 * The file is read and nul-terminated
141 *
142 * @ctx: PXE context
143 * @file_path: File path to read (relative to the PXE file)
144 * @file_addr: Address to load file to
145 * Returns 1 for success, or < 0 on error
146 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600147int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600148 ulong file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100149{
Simon Glassa9401b92021-10-14 12:48:08 -0600150 ulong size;
Patrice Chotard17e88042019-11-25 09:07:37 +0100151 int err;
152 char *buf;
153
Simon Glassa9401b92021-10-14 12:48:08 -0600154 err = get_relfile(ctx, file_path, file_addr, &size);
Patrice Chotard17e88042019-11-25 09:07:37 +0100155 if (err < 0)
156 return err;
157
Simon Glassa9401b92021-10-14 12:48:08 -0600158 buf = map_sysmem(file_addr + size, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +0100159 *buf = '\0';
160 unmap_sysmem(buf);
161
162 return 1;
163}
164
165#define PXELINUX_DIR "pxelinux.cfg/"
166
Simon Glass00302442021-10-14 12:48:01 -0600167/**
168 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
169 *
170 * @ctx: PXE context
171 * @file: Filename to process (relative to pxelinux.cfg/)
172 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
173 * or other value < 0 on other error
174 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600175int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100176 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100177{
178 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100179 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100180
181 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
182 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100183 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100184 return -ENAMETOOLONG;
185 }
186
187 sprintf(path, PXELINUX_DIR "%s", file);
188
Simon Glassb0d08db2021-10-14 12:47:56 -0600189 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100190}
191
Simon Glass00302442021-10-14 12:48:01 -0600192/**
193 * get_relfile_envaddr() - read a file to an address in an env var
194 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100195 * Wrapper to make it easier to store the file at file_path in the location
196 * specified by envaddr_name. file_path will be joined to the bootfile path,
197 * if any is specified.
198 *
Simon Glass00302442021-10-14 12:48:01 -0600199 * @ctx: PXE context
200 * @file_path: File path to read (relative to the PXE file)
201 * @envaddr_name: Name of environment variable which contains the address to
202 * load to
Simon Glassa9401b92021-10-14 12:48:08 -0600203 * @filesizep: Returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -0600204 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
205 * environment variable, -EINVAL if its format is not valid hex, or other
206 * value < 0 on other error
Patrice Chotard17e88042019-11-25 09:07:37 +0100207 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600208static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600209 const char *envaddr_name, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100210{
211 unsigned long file_addr;
212 char *envaddr;
213
214 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100215 if (!envaddr)
216 return -ENOENT;
217
218 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
219 return -EINVAL;
220
Simon Glassa9401b92021-10-14 12:48:08 -0600221 return get_relfile(ctx, file_path, file_addr, filesizep);
Patrice Chotard17e88042019-11-25 09:07:37 +0100222}
223
Simon Glass00302442021-10-14 12:48:01 -0600224/**
225 * label_create() - crate a new PXE label
226 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100227 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
228 * result must be free()'d to reclaim the memory.
229 *
Simon Glass00302442021-10-14 12:48:01 -0600230 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100231 */
232static struct pxe_label *label_create(void)
233{
234 struct pxe_label *label;
235
236 label = malloc(sizeof(struct pxe_label));
Patrice Chotard17e88042019-11-25 09:07:37 +0100237 if (!label)
238 return NULL;
239
240 memset(label, 0, sizeof(struct pxe_label));
241
242 return label;
243}
244
Simon Glass00302442021-10-14 12:48:01 -0600245/**
246 * label_destroy() - free the memory used by a pxe_label
247 *
248 * This frees @label itself as well as memory used by its name,
249 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
250 * they're non-NULL.
Patrice Chotard17e88042019-11-25 09:07:37 +0100251 *
252 * So - be sure to only use dynamically allocated memory for the members of
253 * the pxe_label struct, unless you want to clean it up first. These are
254 * currently only created by the pxe file parsing code.
Simon Glass00302442021-10-14 12:48:01 -0600255 *
256 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100257 */
258static void label_destroy(struct pxe_label *label)
259{
Simon Glass764d0c02021-10-14 12:48:02 -0600260 free(label->name);
261 free(label->kernel);
262 free(label->config);
263 free(label->append);
264 free(label->initrd);
265 free(label->fdt);
266 free(label->fdtdir);
267 free(label->fdtoverlays);
Patrice Chotard17e88042019-11-25 09:07:37 +0100268 free(label);
269}
270
Simon Glass00302442021-10-14 12:48:01 -0600271/**
272 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100273 *
274 * This is passed as a callback to the menu code for displaying each
275 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600276 *
277 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-11-25 09:07:37 +0100278 */
279static void label_print(void *data)
280{
281 struct pxe_label *label = data;
282 const char *c = label->menu ? label->menu : label->name;
283
284 printf("%s:\t%s\n", label->num, c);
285}
286
Simon Glass00302442021-10-14 12:48:01 -0600287/**
288 * label_localboot() - Boot a label that specified 'localboot'
289 *
290 * This requires that the 'localcmd' environment variable is defined. Its
291 * contents will be executed as U-Boot commands. If the label specified an
292 * 'append' line, its contents will be used to overwrite the contents of the
293 * 'bootargs' environment variable prior to running 'localcmd'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100294 *
Simon Glass00302442021-10-14 12:48:01 -0600295 * @label: Label to process
296 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100297 */
298static int label_localboot(struct pxe_label *label)
299{
300 char *localcmd;
301
302 localcmd = from_env("localcmd");
Patrice Chotard17e88042019-11-25 09:07:37 +0100303 if (!localcmd)
304 return -ENOENT;
305
306 if (label->append) {
307 char bootargs[CONFIG_SYS_CBSIZE];
308
Simon Glassc7b03e82020-11-05 10:33:47 -0700309 cli_simple_process_macros(label->append, bootargs,
310 sizeof(bootargs));
Patrice Chotard17e88042019-11-25 09:07:37 +0100311 env_set("bootargs", bootargs);
312 }
313
314 debug("running: %s\n", localcmd);
315
316 return run_command_list(localcmd, strlen(localcmd), 0);
317}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100318
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800319/*
320 * label_boot_kaslrseed generate kaslrseed from hw rng
321 */
322
323static void label_boot_kaslrseed(void)
324{
325#ifdef CONFIG_DM_RNG
326 ulong fdt_addr;
327 struct fdt_header *working_fdt;
328 size_t n = 0x8;
329 struct udevice *dev;
330 u64 *buf;
331 int nodeoffset;
332 int err;
333
334 /* Get the main fdt and map it */
335 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
336 working_fdt = map_sysmem(fdt_addr, 0);
337 err = fdt_check_header(working_fdt);
338 if (err)
339 return;
340
341 /* add extra size for holding kaslr-seed */
342 /* err is new fdt size, 0 or negtive */
343 err = fdt_shrink_to_minimum(working_fdt, 512);
344 if (err <= 0)
345 return;
346
347 if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
348 printf("No RNG device\n");
349 return;
350 }
351
352 nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
353 if (nodeoffset < 0) {
354 printf("Reading chosen node failed\n");
355 return;
356 }
357
358 buf = malloc(n);
359 if (!buf) {
360 printf("Out of memory\n");
361 return;
362 }
363
364 if (dm_rng_read(dev, buf, n)) {
365 printf("Reading RNG failed\n");
366 goto err;
367 }
368
369 err = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
370 if (err < 0) {
371 printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(err));
372 goto err;
373 }
374err:
375 free(buf);
376#endif
377 return;
378}
379
Simon Glass00302442021-10-14 12:48:01 -0600380/**
381 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200382 * or 'devicetree-overlay'
Simon Glass00302442021-10-14 12:48:01 -0600383 *
384 * @ctx: PXE context
385 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100386 */
387#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600388static void label_boot_fdtoverlay(struct pxe_context *ctx,
389 struct pxe_label *label)
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100390{
391 char *fdtoverlay = label->fdtoverlays;
392 struct fdt_header *working_fdt;
393 char *fdtoverlay_addr_env;
394 ulong fdtoverlay_addr;
395 ulong fdt_addr;
396 int err;
397
398 /* Get the main fdt and map it */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600399 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100400 working_fdt = map_sysmem(fdt_addr, 0);
401 err = fdt_check_header(working_fdt);
402 if (err)
403 return;
404
405 /* Get the specific overlay loading address */
406 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
407 if (!fdtoverlay_addr_env) {
408 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
409 return;
410 }
411
Simon Glass3ff49ec2021-07-24 09:03:29 -0600412 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100413
414 /* Cycle over the overlay files and apply them in order */
415 do {
416 struct fdt_header *blob;
417 char *overlayfile;
418 char *end;
419 int len;
420
421 /* Drop leading spaces */
422 while (*fdtoverlay == ' ')
423 ++fdtoverlay;
424
425 /* Copy a single filename if multiple provided */
426 end = strstr(fdtoverlay, " ");
427 if (end) {
428 len = (int)(end - fdtoverlay);
429 overlayfile = malloc(len + 1);
430 strncpy(overlayfile, fdtoverlay, len);
431 overlayfile[len] = '\0';
432 } else
433 overlayfile = fdtoverlay;
434
435 if (!strlen(overlayfile))
436 goto skip_overlay;
437
438 /* Load overlay file */
Simon Glassa9401b92021-10-14 12:48:08 -0600439 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
440 NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100441 if (err < 0) {
442 printf("Failed loading overlay %s\n", overlayfile);
443 goto skip_overlay;
444 }
445
446 /* Resize main fdt */
447 fdt_shrink_to_minimum(working_fdt, 8192);
448
449 blob = map_sysmem(fdtoverlay_addr, 0);
450 err = fdt_check_header(blob);
451 if (err) {
452 printf("Invalid overlay %s, skipping\n",
453 overlayfile);
454 goto skip_overlay;
455 }
456
457 err = fdt_overlay_apply_verbose(working_fdt, blob);
458 if (err) {
459 printf("Failed to apply overlay %s, skipping\n",
460 overlayfile);
461 goto skip_overlay;
462 }
463
464skip_overlay:
465 if (end)
466 free(overlayfile);
467 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
468}
469#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100470
Simon Glass00302442021-10-14 12:48:01 -0600471/**
472 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard17e88042019-11-25 09:07:37 +0100473 *
474 * If we can't boot for any reason, we return. A successful boot never
475 * returns.
476 *
477 * The kernel will be stored in the location given by the 'kernel_addr_r'
478 * environment variable.
479 *
480 * If the label specifies an initrd file, it will be stored in the location
481 * given by the 'ramdisk_addr_r' environment variable.
482 *
483 * If the label specifies an 'append' line, its contents will overwrite that
484 * of the 'bootargs' environment variable.
Simon Glass00302442021-10-14 12:48:01 -0600485 *
486 * @ctx: PXE context
487 * @label: Label to process
488 * Returns does not return on success, otherwise returns 0 if a localboot
489 * label was processed, or 1 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100490 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600491static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +0100492{
493 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700494 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Libc14b932021-10-20 00:18:15 -0700495 char *kernel_addr = NULL;
496 char *initrd_addr_str = NULL;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700497 char initrd_filesize[10];
Zhaofeng Libc14b932021-10-20 00:18:15 -0700498 char initrd_str[28];
Patrice Chotard17e88042019-11-25 09:07:37 +0100499 char mac_str[29] = "";
500 char ip_str[68] = "";
501 char *fit_addr = NULL;
502 int bootm_argc = 2;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700503 int zboot_argc = 3;
Patrice Chotard17e88042019-11-25 09:07:37 +0100504 int len = 0;
Zhaofeng Libc14b932021-10-20 00:18:15 -0700505 ulong kernel_addr_r;
Patrice Chotard17e88042019-11-25 09:07:37 +0100506 void *buf;
507
508 label_print(label);
509
510 label->attempted = 1;
511
512 if (label->localboot) {
513 if (label->localboot_val >= 0)
514 label_localboot(label);
515 return 0;
516 }
517
Patrice Chotard6233de42019-11-25 09:07:39 +0100518 if (!label->kernel) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100519 printf("No kernel given, skipping %s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100520 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100521 return 1;
522 }
523
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200524 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
525 NULL) < 0) {
526 printf("Skipping %s for failure retrieving kernel\n",
527 label->name);
528 return 1;
529 }
530
531 kernel_addr = env_get("kernel_addr_r");
532 /* for FIT, append the configuration identifier */
533 if (label->config) {
534 int len = strlen(kernel_addr) + strlen(label->config) + 1;
535
536 fit_addr = malloc(len);
537 if (!fit_addr) {
538 printf("malloc fail (FIT address)\n");
539 return 1;
540 }
541 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
542 kernel_addr = fit_addr;
543 }
544
Patrice Chotard17e88042019-11-25 09:07:37 +0100545 if (label->initrd) {
Simon Glassa9401b92021-10-14 12:48:08 -0600546 ulong size;
547
548 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
549 &size) < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100550 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100551 label->name);
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200552 goto cleanup;
Patrice Chotard17e88042019-11-25 09:07:37 +0100553 }
554
Zhaofeng Libc14b932021-10-20 00:18:15 -0700555 initrd_addr_str = env_get("ramdisk_addr_r");
Heinrich Schuchardta8d6aed2021-11-15 19:26:51 +0100556 size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx",
557 initrd_addr_str, size);
558 if (size >= sizeof(initrd_str))
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200559 goto cleanup;
Patrice Chotard17e88042019-11-25 09:07:37 +0100560 }
561
Patrice Chotard17e88042019-11-25 09:07:37 +0100562 if (label->ipappend & 0x1) {
563 sprintf(ip_str, " ip=%s:%s:%s:%s",
564 env_get("ipaddr"), env_get("serverip"),
565 env_get("gatewayip"), env_get("netmask"));
566 }
567
Kory Maincentcaabd242021-02-02 16:42:28 +0100568 if (IS_ENABLED(CONFIG_CMD_NET)) {
569 if (label->ipappend & 0x2) {
570 int err;
Patrice Chotard6233de42019-11-25 09:07:39 +0100571
Kory Maincentcaabd242021-02-02 16:42:28 +0100572 strcpy(mac_str, " BOOTIF=");
573 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
574 if (err < 0)
575 mac_str[0] = '\0';
576 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100577 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100578
579 if ((label->ipappend & 0x3) || label->append) {
580 char bootargs[CONFIG_SYS_CBSIZE] = "";
581 char finalbootargs[CONFIG_SYS_CBSIZE];
582
583 if (strlen(label->append ?: "") +
584 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
585 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
586 strlen(label->append ?: ""),
587 strlen(ip_str), strlen(mac_str),
588 sizeof(bootargs));
Patrick Delaunay6b5cb362022-10-28 11:01:18 +0200589 goto cleanup;
Patrice Chotard17e88042019-11-25 09:07:37 +0100590 }
Patrice Chotard6233de42019-11-25 09:07:39 +0100591
592 if (label->append)
593 strncpy(bootargs, label->append, sizeof(bootargs));
594
595 strcat(bootargs, ip_str);
596 strcat(bootargs, mac_str);
597
Simon Glassc7b03e82020-11-05 10:33:47 -0700598 cli_simple_process_macros(bootargs, finalbootargs,
599 sizeof(finalbootargs));
Patrice Chotard6233de42019-11-25 09:07:39 +0100600 env_set("bootargs", finalbootargs);
601 printf("append: %s\n", finalbootargs);
Patrice Chotard17e88042019-11-25 09:07:37 +0100602 }
603
Patrice Chotard17e88042019-11-25 09:07:37 +0100604 /*
605 * fdt usage is optional:
Anton Leontievfeb7db82019-09-03 10:52:24 +0300606 * It handles the following scenarios.
Patrice Chotard17e88042019-11-25 09:07:37 +0100607 *
Anton Leontievfeb7db82019-09-03 10:52:24 +0300608 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
609 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
610 * bootm, and adjust argc appropriately.
611 *
612 * If retrieve fails and no exact fdt blob is specified in pxe file with
613 * "fdt" label, try Scenario 2.
Patrice Chotard17e88042019-11-25 09:07:37 +0100614 *
615 * Scenario 2: If there is an fdt_addr specified, pass it along to
616 * bootm, and adjust argc appropriately.
617 *
Peter Hoyesdb2f3872021-10-14 09:40:04 +0100618 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
619 * bootm, and adjust argc appropriately.
620 *
621 * Scenario 4: fdt blob is not available.
Patrice Chotard17e88042019-11-25 09:07:37 +0100622 */
623 bootm_argv[3] = env_get("fdt_addr_r");
624
625 /* if fdt label is defined then get fdt from server */
626 if (bootm_argv[3]) {
627 char *fdtfile = NULL;
628 char *fdtfilefree = NULL;
629
630 if (label->fdt) {
631 fdtfile = label->fdt;
632 } else if (label->fdtdir) {
633 char *f1, *f2, *f3, *f4, *slash;
634
635 f1 = env_get("fdtfile");
636 if (f1) {
637 f2 = "";
638 f3 = "";
639 f4 = "";
640 } else {
641 /*
642 * For complex cases where this code doesn't
643 * generate the correct filename, the board
644 * code should set $fdtfile during early boot,
645 * or the boot scripts should set $fdtfile
646 * before invoking "pxe" or "sysboot".
647 */
648 f1 = env_get("soc");
649 f2 = "-";
650 f3 = env_get("board");
651 f4 = ".dtb";
Dimitri John Ledkov5ee984a2021-04-21 12:42:01 +0100652 if (!f1) {
653 f1 = "";
654 f2 = "";
655 }
656 if (!f3) {
657 f2 = "";
658 f3 = "";
659 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100660 }
661
662 len = strlen(label->fdtdir);
663 if (!len)
664 slash = "./";
665 else if (label->fdtdir[len - 1] != '/')
666 slash = "/";
667 else
668 slash = "";
669
670 len = strlen(label->fdtdir) + strlen(slash) +
671 strlen(f1) + strlen(f2) + strlen(f3) +
672 strlen(f4) + 1;
673 fdtfilefree = malloc(len);
674 if (!fdtfilefree) {
675 printf("malloc fail (FDT filename)\n");
676 goto cleanup;
677 }
678
679 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
680 label->fdtdir, slash, f1, f2, f3, f4);
681 fdtfile = fdtfilefree;
682 }
683
684 if (fdtfile) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600685 int err = get_relfile_envaddr(ctx, fdtfile,
Simon Glassa9401b92021-10-14 12:48:08 -0600686 "fdt_addr_r", NULL);
Patrice Chotard6233de42019-11-25 09:07:39 +0100687
Patrice Chotard17e88042019-11-25 09:07:37 +0100688 free(fdtfilefree);
689 if (err < 0) {
Anton Leontievfeb7db82019-09-03 10:52:24 +0300690 bootm_argv[3] = NULL;
691
692 if (label->fdt) {
693 printf("Skipping %s for failure retrieving FDT\n",
694 label->name);
695 goto cleanup;
696 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100697 }
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100698
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800699 if (label->kaslrseed)
700 label_boot_kaslrseed();
701
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100702#ifdef CONFIG_OF_LIBFDT_OVERLAY
703 if (label->fdtoverlays)
Simon Glassb0d08db2021-10-14 12:47:56 -0600704 label_boot_fdtoverlay(ctx, label);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100705#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100706 } else {
707 bootm_argv[3] = NULL;
708 }
709 }
710
Zhaofeng Libc14b932021-10-20 00:18:15 -0700711 bootm_argv[1] = kernel_addr;
712 zboot_argv[1] = kernel_addr;
713
714 if (initrd_addr_str) {
715 bootm_argv[2] = initrd_str;
716 bootm_argc = 3;
717
718 zboot_argv[3] = initrd_addr_str;
719 zboot_argv[4] = initrd_filesize;
720 zboot_argc = 5;
721 }
722
Patrice Chotard17e88042019-11-25 09:07:37 +0100723 if (!bootm_argv[3])
724 bootm_argv[3] = env_get("fdt_addr");
725
Peter Hoyesdb2f3872021-10-14 09:40:04 +0100726 if (!bootm_argv[3])
727 bootm_argv[3] = env_get("fdtcontroladdr");
728
Patrice Chotard17e88042019-11-25 09:07:37 +0100729 if (bootm_argv[3]) {
730 if (!bootm_argv[2])
731 bootm_argv[2] = "-";
732 bootm_argc = 4;
733 }
734
Zhaofeng Libc14b932021-10-20 00:18:15 -0700735 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
736 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard17e88042019-11-25 09:07:37 +0100737 /* Try bootm for legacy and FIT format image */
John Keeping9b60a172022-07-28 11:19:15 +0100738 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID &&
739 IS_ENABLED(CONFIG_CMD_BOOTM))
Simon Glassb0d08db2021-10-14 12:47:56 -0600740 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100741 /* Try booting an AArch64 Linux kernel image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100742 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassb0d08db2021-10-14 12:47:56 -0600743 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100744 /* Try booting a Image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100745 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassb0d08db2021-10-14 12:47:56 -0600746 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincentb2187d02021-02-02 16:42:29 +0100747 /* Try booting an x86_64 Linux kernel image */
748 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassb0d08db2021-10-14 12:47:56 -0600749 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentcaabd242021-02-02 16:42:28 +0100750
Patrice Chotard17e88042019-11-25 09:07:37 +0100751 unmap_sysmem(buf);
752
753cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600754 free(fit_addr);
755
Patrice Chotard17e88042019-11-25 09:07:37 +0100756 return 1;
757}
758
Simon Glass00302442021-10-14 12:48:01 -0600759/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100760enum token_type {
761 T_EOL,
762 T_STRING,
763 T_EOF,
764 T_MENU,
765 T_TITLE,
766 T_TIMEOUT,
767 T_LABEL,
768 T_KERNEL,
769 T_LINUX,
770 T_APPEND,
771 T_INITRD,
772 T_LOCALBOOT,
773 T_DEFAULT,
774 T_PROMPT,
775 T_INCLUDE,
776 T_FDT,
777 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100778 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100779 T_ONTIMEOUT,
780 T_IPAPPEND,
781 T_BACKGROUND,
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800782 T_KASLRSEED,
Patrice Chotard17e88042019-11-25 09:07:37 +0100783 T_INVALID
784};
785
Simon Glass00302442021-10-14 12:48:01 -0600786/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100787struct token {
788 char *val;
789 enum token_type type;
790};
791
Simon Glass00302442021-10-14 12:48:01 -0600792/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100793static const struct token keywords[] = {
794 {"menu", T_MENU},
795 {"title", T_TITLE},
796 {"timeout", T_TIMEOUT},
797 {"default", T_DEFAULT},
798 {"prompt", T_PROMPT},
799 {"label", T_LABEL},
800 {"kernel", T_KERNEL},
801 {"linux", T_LINUX},
802 {"localboot", T_LOCALBOOT},
803 {"append", T_APPEND},
804 {"initrd", T_INITRD},
805 {"include", T_INCLUDE},
806 {"devicetree", T_FDT},
807 {"fdt", T_FDT},
808 {"devicetreedir", T_FDTDIR},
809 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100810 {"fdtoverlays", T_FDTOVERLAYS},
Edoardo Tomelleri6acf1b92022-09-21 15:26:33 +0200811 {"devicetree-overlay", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100812 {"ontimeout", T_ONTIMEOUT,},
813 {"ipappend", T_IPAPPEND,},
814 {"background", T_BACKGROUND,},
Zhang Ning9c1d9c52022-02-01 08:33:37 +0800815 {"kaslrseed", T_KASLRSEED,},
Patrice Chotard17e88042019-11-25 09:07:37 +0100816 {NULL, T_INVALID}
817};
818
Simon Glass00302442021-10-14 12:48:01 -0600819/**
820 * enum lex_state - lexer state
821 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100822 * Since pxe(linux) files don't have a token to identify the start of a
823 * literal, we have to keep track of when we're in a state where a literal is
824 * expected vs when we're in a state a keyword is expected.
825 */
826enum lex_state {
827 L_NORMAL = 0,
828 L_KEYWORD,
829 L_SLITERAL
830};
831
Simon Glass00302442021-10-14 12:48:01 -0600832/**
833 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100834 *
Simon Glass00302442021-10-14 12:48:01 -0600835 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100836 *
837 * Characters from *p are copied into t-val until a character equal to
838 * delim is found, or a NUL byte is reached. If delim has the special value of
839 * ' ', any whitespace character will be used as a delimiter.
840 *
841 * If lower is unequal to 0, uppercase characters will be converted to
842 * lowercase in the result. This is useful to make keywords case
843 * insensitive.
844 *
845 * The location of *p is updated to point to the first character after the end
846 * of the token - the ending delimiter.
847 *
Simon Glass00302442021-10-14 12:48:01 -0600848 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
849 * it.
850 *
851 * @p: Points to a pointer to the current position in the input being processed.
852 * Updated to point at the first character after the current token
853 * @t: Pointers to a token to fill in
854 * @delim: Delimiter character to look for, either newline or space
855 * @lower: true to convert the string to lower case when storing
856 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100857 */
858static char *get_string(char **p, struct token *t, char delim, int lower)
859{
860 char *b, *e;
861 size_t len, i;
862
863 /*
864 * b and e both start at the beginning of the input stream.
865 *
866 * e is incremented until we find the ending delimiter, or a NUL byte
867 * is reached. Then, we take e - b to find the length of the token.
868 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100869 b = *p;
870 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100871 while (*e) {
872 if ((delim == ' ' && isspace(*e)) || delim == *e)
873 break;
874 e++;
875 }
876
877 len = e - b;
878
879 /*
880 * Allocate memory to hold the string, and copy it in, converting
881 * characters to lowercase if lower is != 0.
882 */
883 t->val = malloc(len + 1);
884 if (!t->val)
885 return NULL;
886
887 for (i = 0; i < len; i++, b++) {
888 if (lower)
889 t->val[i] = tolower(*b);
890 else
891 t->val[i] = *b;
892 }
893
894 t->val[len] = '\0';
895
Simon Glass764d0c02021-10-14 12:48:02 -0600896 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100897 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100898 t->type = T_STRING;
899
900 return t->val;
901}
902
Simon Glass00302442021-10-14 12:48:01 -0600903/**
904 * get_keyword() - Populate a keyword token with a type and value
905 *
906 * Updates the ->type field based on the keyword string in @val
907 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100908 */
909static void get_keyword(struct token *t)
910{
911 int i;
912
913 for (i = 0; keywords[i].val; i++) {
914 if (!strcmp(t->val, keywords[i].val)) {
915 t->type = keywords[i].type;
916 break;
917 }
918 }
919}
920
Simon Glass00302442021-10-14 12:48:01 -0600921/**
922 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100923 *
Simon Glass00302442021-10-14 12:48:01 -0600924 * We have to keep track of which state we're in to know if we're looking to get
925 * a string literal or a keyword.
926 *
927 * @p: Points to a pointer to the current position in the input being processed.
928 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100929 */
930static void get_token(char **p, struct token *t, enum lex_state state)
931{
932 char *c = *p;
933
934 t->type = T_INVALID;
935
936 /* eat non EOL whitespace */
937 while (isblank(*c))
938 c++;
939
940 /*
941 * eat comments. note that string literals can't begin with #, but
942 * can contain a # after their first character.
943 */
944 if (*c == '#') {
945 while (*c && *c != '\n')
946 c++;
947 }
948
949 if (*c == '\n') {
950 t->type = T_EOL;
951 c++;
952 } else if (*c == '\0') {
953 t->type = T_EOF;
954 c++;
955 } else if (state == L_SLITERAL) {
956 get_string(&c, t, '\n', 0);
957 } else if (state == L_KEYWORD) {
958 /*
959 * when we expect a keyword, we first get the next string
960 * token delimited by whitespace, and then check if it
961 * matches a keyword in our keyword list. if it does, it's
962 * converted to a keyword token of the appropriate type, and
963 * if not, it remains a string token.
964 */
965 get_string(&c, t, ' ', 1);
966 get_keyword(t);
967 }
968
969 *p = c;
970}
971
Simon Glass00302442021-10-14 12:48:01 -0600972/**
973 * eol_or_eof() - Find end of line
974 *
975 * Increment *c until we get to the end of the current line, or EOF
976 *
977 * @c: Points to a pointer to the current position in the input being processed.
978 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100979 */
980static void eol_or_eof(char **c)
981{
982 while (**c && **c != '\n')
983 (*c)++;
984}
985
986/*
987 * All of these parse_* functions share some common behavior.
988 *
989 * They finish with *c pointing after the token they parse, and return 1 on
990 * success, or < 0 on error.
991 */
992
993/*
994 * Parse a string literal and store a pointer it at *dst. String literals
995 * terminate at the end of the line.
996 */
997static int parse_sliteral(char **c, char **dst)
998{
999 struct token t;
1000 char *s = *c;
1001
1002 get_token(c, &t, L_SLITERAL);
1003
1004 if (t.type != T_STRING) {
1005 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1006 return -EINVAL;
1007 }
1008
1009 *dst = t.val;
1010
1011 return 1;
1012}
1013
1014/*
1015 * Parse a base 10 (unsigned) integer and store it at *dst.
1016 */
1017static int parse_integer(char **c, int *dst)
1018{
1019 struct token t;
1020 char *s = *c;
1021
1022 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001023 if (t.type != T_STRING) {
1024 printf("Expected string: %.*s\n", (int)(*c - s), s);
1025 return -EINVAL;
1026 }
1027
1028 *dst = simple_strtol(t.val, NULL, 10);
1029
1030 free(t.val);
1031
1032 return 1;
1033}
1034
Simon Glassb0d08db2021-10-14 12:47:56 -06001035static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001036 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001037
1038/*
1039 * Parse an include statement, and retrieve and parse the file it mentions.
1040 *
1041 * base should point to a location where it's safe to store the file, and
1042 * nest_level should indicate how many nested includes have occurred. For this
1043 * include, nest_level has already been incremented and doesn't need to be
1044 * incremented here.
1045 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001046static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001047 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001048{
1049 char *include_path;
1050 char *s = *c;
1051 int err;
1052 char *buf;
1053 int ret;
1054
1055 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001056 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001057 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001058 return err;
1059 }
1060
Simon Glassb0d08db2021-10-14 12:47:56 -06001061 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001062 if (err < 0) {
1063 printf("Couldn't retrieve %s\n", include_path);
1064 return err;
1065 }
1066
1067 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001068 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001069 unmap_sysmem(buf);
1070
1071 return ret;
1072}
1073
1074/*
1075 * Parse lines that begin with 'menu'.
1076 *
1077 * base and nest are provided to handle the 'menu include' case.
1078 *
1079 * base should point to a location where it's safe to store the included file.
1080 *
1081 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1082 * a file it includes, 3 when parsing a file included by that file, and so on.
1083 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001084static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001085 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001086{
1087 struct token t;
1088 char *s = *c;
1089 int err = 0;
1090
1091 get_token(c, &t, L_KEYWORD);
1092
1093 switch (t.type) {
1094 case T_TITLE:
1095 err = parse_sliteral(c, &cfg->title);
1096
1097 break;
1098
1099 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001100 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001101 break;
1102
1103 case T_BACKGROUND:
1104 err = parse_sliteral(c, &cfg->bmp);
1105 break;
1106
1107 default:
1108 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001109 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001110 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001111 if (err < 0)
1112 return err;
1113
1114 eol_or_eof(c);
1115
1116 return 1;
1117}
1118
1119/*
1120 * Handles parsing a 'menu line' when we're parsing a label.
1121 */
1122static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001123 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001124{
1125 struct token t;
1126 char *s;
1127
1128 s = *c;
1129
1130 get_token(c, &t, L_KEYWORD);
1131
1132 switch (t.type) {
1133 case T_DEFAULT:
1134 if (!cfg->default_label)
1135 cfg->default_label = strdup(label->name);
1136
1137 if (!cfg->default_label)
1138 return -ENOMEM;
1139
1140 break;
1141 case T_LABEL:
1142 parse_sliteral(c, &label->menu);
1143 break;
1144 default:
1145 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001146 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001147 }
1148
1149 eol_or_eof(c);
1150
1151 return 0;
1152}
1153
1154/*
1155 * Handles parsing a 'kernel' label.
1156 * expecting "filename" or "<fit_filename>#cfg"
1157 */
1158static int parse_label_kernel(char **c, struct pxe_label *label)
1159{
1160 char *s;
1161 int err;
1162
1163 err = parse_sliteral(c, &label->kernel);
1164 if (err < 0)
1165 return err;
1166
1167 s = strstr(label->kernel, "#");
1168 if (!s)
1169 return 1;
1170
1171 label->config = malloc(strlen(s) + 1);
1172 if (!label->config)
1173 return -ENOMEM;
1174
1175 strcpy(label->config, s);
1176 *s = 0;
1177
1178 return 1;
1179}
1180
1181/*
1182 * Parses a label and adds it to the list of labels for a menu.
1183 *
1184 * A label ends when we either get to the end of a file, or
1185 * get some input we otherwise don't have a handler defined
1186 * for.
1187 *
1188 */
1189static int parse_label(char **c, struct pxe_menu *cfg)
1190{
1191 struct token t;
1192 int len;
1193 char *s = *c;
1194 struct pxe_label *label;
1195 int err;
1196
1197 label = label_create();
1198 if (!label)
1199 return -ENOMEM;
1200
1201 err = parse_sliteral(c, &label->name);
1202 if (err < 0) {
1203 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1204 label_destroy(label);
1205 return -EINVAL;
1206 }
1207
1208 list_add_tail(&label->list, &cfg->labels);
1209
1210 while (1) {
1211 s = *c;
1212 get_token(c, &t, L_KEYWORD);
1213
1214 err = 0;
1215 switch (t.type) {
1216 case T_MENU:
1217 err = parse_label_menu(c, cfg, label);
1218 break;
1219
1220 case T_KERNEL:
1221 case T_LINUX:
1222 err = parse_label_kernel(c, label);
1223 break;
1224
1225 case T_APPEND:
1226 err = parse_sliteral(c, &label->append);
1227 if (label->initrd)
1228 break;
1229 s = strstr(label->append, "initrd=");
1230 if (!s)
1231 break;
1232 s += 7;
1233 len = (int)(strchr(s, ' ') - s);
1234 label->initrd = malloc(len + 1);
1235 strncpy(label->initrd, s, len);
1236 label->initrd[len] = '\0';
1237
1238 break;
1239
1240 case T_INITRD:
1241 if (!label->initrd)
1242 err = parse_sliteral(c, &label->initrd);
1243 break;
1244
1245 case T_FDT:
1246 if (!label->fdt)
1247 err = parse_sliteral(c, &label->fdt);
1248 break;
1249
1250 case T_FDTDIR:
1251 if (!label->fdtdir)
1252 err = parse_sliteral(c, &label->fdtdir);
1253 break;
1254
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001255 case T_FDTOVERLAYS:
1256 if (!label->fdtoverlays)
1257 err = parse_sliteral(c, &label->fdtoverlays);
1258 break;
1259
Patrice Chotard17e88042019-11-25 09:07:37 +01001260 case T_LOCALBOOT:
1261 label->localboot = 1;
1262 err = parse_integer(c, &label->localboot_val);
1263 break;
1264
1265 case T_IPAPPEND:
1266 err = parse_integer(c, &label->ipappend);
1267 break;
1268
Zhang Ning9c1d9c52022-02-01 08:33:37 +08001269 case T_KASLRSEED:
1270 label->kaslrseed = 1;
1271 break;
1272
Patrice Chotard17e88042019-11-25 09:07:37 +01001273 case T_EOL:
1274 break;
1275
1276 default:
1277 /*
1278 * put the token back! we don't want it - it's the end
1279 * of a label and whatever token this is, it's
1280 * something for the menu level context to handle.
1281 */
1282 *c = s;
1283 return 1;
1284 }
1285
1286 if (err < 0)
1287 return err;
1288 }
1289}
1290
1291/*
1292 * This 16 comes from the limit pxelinux imposes on nested includes.
1293 *
1294 * There is no reason at all we couldn't do more, but some limit helps prevent
1295 * infinite (until crash occurs) recursion if a file tries to include itself.
1296 */
1297#define MAX_NEST_LEVEL 16
1298
1299/*
1300 * Entry point for parsing a menu file. nest_level indicates how many times
1301 * we've nested in includes. It will be 1 for the top level menu file.
1302 *
1303 * Returns 1 on success, < 0 on error.
1304 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001305static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001306 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001307{
1308 struct token t;
1309 char *s, *b, *label_name;
1310 int err;
1311
1312 b = p;
1313
1314 if (nest_level > MAX_NEST_LEVEL) {
1315 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1316 return -EMLINK;
1317 }
1318
1319 while (1) {
1320 s = p;
1321
1322 get_token(&p, &t, L_KEYWORD);
1323
1324 err = 0;
1325 switch (t.type) {
1326 case T_MENU:
1327 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001328 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001329 base + ALIGN(strlen(b) + 1, 4),
1330 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001331 break;
1332
1333 case T_TIMEOUT:
1334 err = parse_integer(&p, &cfg->timeout);
1335 break;
1336
1337 case T_LABEL:
1338 err = parse_label(&p, cfg);
1339 break;
1340
1341 case T_DEFAULT:
1342 case T_ONTIMEOUT:
1343 err = parse_sliteral(&p, &label_name);
1344
1345 if (label_name) {
1346 if (cfg->default_label)
1347 free(cfg->default_label);
1348
1349 cfg->default_label = label_name;
1350 }
1351
1352 break;
1353
1354 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001355 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001356 base + ALIGN(strlen(b), 4), cfg,
1357 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001358 break;
1359
1360 case T_PROMPT:
1361 eol_or_eof(&p);
1362 break;
1363
1364 case T_EOL:
1365 break;
1366
1367 case T_EOF:
1368 return 1;
1369
1370 default:
1371 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001372 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001373 eol_or_eof(&p);
1374 }
1375
1376 if (err < 0)
1377 return err;
1378 }
1379}
1380
1381/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001382 */
1383void destroy_pxe_menu(struct pxe_menu *cfg)
1384{
1385 struct list_head *pos, *n;
1386 struct pxe_label *label;
1387
Simon Glass764d0c02021-10-14 12:48:02 -06001388 free(cfg->title);
1389 free(cfg->default_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001390
1391 list_for_each_safe(pos, n, &cfg->labels) {
1392 label = list_entry(pos, struct pxe_label, list);
1393
1394 label_destroy(label);
1395 }
1396
1397 free(cfg);
1398}
1399
Simon Glassb0d08db2021-10-14 12:47:56 -06001400struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001401{
1402 struct pxe_menu *cfg;
1403 char *buf;
1404 int r;
1405
1406 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001407 if (!cfg)
1408 return NULL;
1409
1410 memset(cfg, 0, sizeof(struct pxe_menu));
1411
1412 INIT_LIST_HEAD(&cfg->labels);
1413
1414 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001415 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001416 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001417 if (r < 0) {
1418 destroy_pxe_menu(cfg);
1419 return NULL;
1420 }
1421
1422 return cfg;
1423}
1424
1425/*
1426 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1427 * menu code.
1428 */
1429static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1430{
1431 struct pxe_label *label;
1432 struct list_head *pos;
1433 struct menu *m;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001434 char *label_override;
Patrice Chotard17e88042019-11-25 09:07:37 +01001435 int err;
1436 int i = 1;
1437 char *default_num = NULL;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001438 char *override_num = NULL;
Patrice Chotard17e88042019-11-25 09:07:37 +01001439
1440 /*
1441 * Create a menu and add items for all the labels.
1442 */
1443 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -07001444 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001445 if (!m)
1446 return NULL;
1447
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001448 label_override = env_get("pxe_label_override");
1449
Patrice Chotard17e88042019-11-25 09:07:37 +01001450 list_for_each(pos, &cfg->labels) {
1451 label = list_entry(pos, struct pxe_label, list);
1452
1453 sprintf(label->num, "%d", i++);
1454 if (menu_item_add(m, label->num, label) != 1) {
1455 menu_destroy(m);
1456 return NULL;
1457 }
1458 if (cfg->default_label &&
1459 (strcmp(label->name, cfg->default_label) == 0))
1460 default_num = label->num;
Amjad Ouled-Ameur1d564a92021-11-13 14:09:20 +01001461 if (label_override && !strcmp(label->name, label_override))
1462 override_num = label->num;
1463 }
1464
1465
1466 if (label_override) {
1467 if (override_num)
1468 default_num = override_num;
1469 else
1470 printf("Missing override pxe label: %s\n",
1471 label_override);
Patrice Chotard17e88042019-11-25 09:07:37 +01001472 }
1473
1474 /*
1475 * After we've created items for each label in the menu, set the
1476 * menu's default label if one was specified.
1477 */
1478 if (default_num) {
1479 err = menu_default_set(m, default_num);
1480 if (err != 1) {
1481 if (err != -ENOENT) {
1482 menu_destroy(m);
1483 return NULL;
1484 }
1485
1486 printf("Missing default: %s\n", cfg->default_label);
1487 }
1488 }
1489
1490 return m;
1491}
1492
1493/*
1494 * Try to boot any labels we have yet to attempt to boot.
1495 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001496static void boot_unattempted_labels(struct pxe_context *ctx,
1497 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001498{
1499 struct list_head *pos;
1500 struct pxe_label *label;
1501
1502 list_for_each(pos, &cfg->labels) {
1503 label = list_entry(pos, struct pxe_label, list);
1504
1505 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001506 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001507 }
1508}
1509
Simon Glassb0d08db2021-10-14 12:47:56 -06001510void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001511{
1512 void *choice;
1513 struct menu *m;
1514 int err;
1515
Kory Maincentcaabd242021-02-02 16:42:28 +01001516 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1517 /* display BMP if available */
1518 if (cfg->bmp) {
Simon Glassa9401b92021-10-14 12:48:08 -06001519 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
Simon Glass52cb5042022-10-18 07:46:31 -06001520#if defined(CONFIG_VIDEO)
Patrick Delaunay6ec8cb92022-03-22 17:08:43 +01001521 struct udevice *dev;
1522
1523 err = uclass_first_device_err(UCLASS_VIDEO, &dev);
1524 if (!err)
1525 video_clear(dev);
1526#endif
Kory Maincentcaabd242021-02-02 16:42:28 +01001527 bmp_display(image_load_addr,
1528 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1529 } else {
1530 printf("Skipping background bmp %s for failure\n",
1531 cfg->bmp);
1532 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001533 }
1534 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001535
1536 m = pxe_menu_to_menu(cfg);
1537 if (!m)
1538 return;
1539
1540 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001541 menu_destroy(m);
1542
1543 /*
1544 * err == 1 means we got a choice back from menu_get_choice.
1545 *
1546 * err == -ENOENT if the menu was setup to select the default but no
1547 * default was set. in that case, we should continue trying to boot
1548 * labels that haven't been attempted yet.
1549 *
1550 * otherwise, the user interrupted or there was some other error and
1551 * we give up.
1552 */
1553
1554 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001555 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001556 if (!err)
1557 return;
1558 } else if (err != -ENOENT) {
1559 return;
1560 }
1561
Simon Glassb0d08db2021-10-14 12:47:56 -06001562 boot_unattempted_labels(ctx, cfg);
1563}
1564
Simon Glasse719fe02021-10-14 12:48:04 -06001565int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1566 pxe_getfile_func getfile, void *userdata,
1567 bool allow_abs_path, const char *bootfile)
Simon Glassb0d08db2021-10-14 12:47:56 -06001568{
Simon Glasse719fe02021-10-14 12:48:04 -06001569 const char *last_slash;
1570 size_t path_len = 0;
1571
1572 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001573 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001574 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001575 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001576 ctx->allow_abs_path = allow_abs_path;
Simon Glasse719fe02021-10-14 12:48:04 -06001577
1578 /* figure out the boot directory, if there is one */
1579 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1580 return -ENOSPC;
1581 ctx->bootdir = strdup(bootfile ? bootfile : "");
1582 if (!ctx->bootdir)
1583 return -ENOMEM;
1584
1585 if (bootfile) {
1586 last_slash = strrchr(bootfile, '/');
1587 if (last_slash)
1588 path_len = (last_slash - bootfile) + 1;
1589 }
1590 ctx->bootdir[path_len] = '\0';
1591
1592 return 0;
1593}
1594
1595void pxe_destroy_ctx(struct pxe_context *ctx)
1596{
1597 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001598}
Simon Glass791bbfe2021-10-14 12:48:03 -06001599
1600int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1601{
1602 struct pxe_menu *cfg;
1603
1604 cfg = parse_pxefile(ctx, pxefile_addr_r);
1605 if (!cfg) {
1606 printf("Error parsing config file\n");
1607 return 1;
1608 }
1609
1610 if (prompt)
1611 cfg->prompt = 1;
1612
1613 handle_pxe_menu(ctx, cfg);
1614
1615 destroy_pxe_menu(cfg);
1616
1617 return 0;
1618}