blob: a7a84f26c1d56556ef8f4cb2ca539b3b419bacec [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>
Patrice Chotard17e88042019-11-25 09:07:37 +01009#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070010#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010012#include <malloc.h>
13#include <mapmem.h>
14#include <lcd.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Neil Armstrongc77a1a32021-01-20 09:54:53 +010016#include <fdt_support.h>
17#include <linux/libfdt.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010018#include <linux/string.h>
19#include <linux/ctype.h>
20#include <errno.h>
21#include <linux/list.h>
22
23#include <splash.h>
24#include <asm/io.h>
25
26#include "menu.h"
27#include "cli.h"
28
29#include "pxe_utils.h"
30
Ben Wolsieffer6273f132019-11-28 00:07:08 -050031#define MAX_TFTP_PATH_LEN 512
Patrice Chotard17e88042019-11-25 09:07:37 +010032
Simon Glassa9401b92021-10-14 12:48:08 -060033int pxe_get_file_size(ulong *sizep)
34{
35 const char *val;
36
37 val = from_env("filesize");
38 if (!val)
39 return -ENOENT;
40
41 if (strict_strtoul(val, 16, sizep) < 0)
42 return -EINVAL;
43
44 return 0;
45}
46
Simon Glass00302442021-10-14 12:48:01 -060047/**
48 * format_mac_pxe() - obtain a MAC address in the PXE format
49 *
50 * This produces a MAC-address string in the format for the current ethernet
51 * device:
52 *
53 * 01-aa-bb-cc-dd-ee-ff
54 *
55 * where aa-ff is the MAC address in hex
56 *
57 * @outbuf: Buffer to write string to
58 * @outbuf_len: length of buffer
59 * @return 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
60 * current ethernet device
61 */
Patrice Chotard17e88042019-11-25 09:07:37 +010062int format_mac_pxe(char *outbuf, size_t outbuf_len)
63{
64 uchar ethaddr[6];
65
66 if (outbuf_len < 21) {
67 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Simon Glass00302442021-10-14 12:48:01 -060068 return -ENOSPC;
Patrice Chotard17e88042019-11-25 09:07:37 +010069 }
70
71 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
72 return -ENOENT;
73
74 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
75 ethaddr[0], ethaddr[1], ethaddr[2],
76 ethaddr[3], ethaddr[4], ethaddr[5]);
77
78 return 1;
79}
80
Simon Glass00302442021-10-14 12:48:01 -060081/**
Simon Glass00302442021-10-14 12:48:01 -060082 * get_relfile() - read a file relative to the PXE file
83 *
Patrice Chotard17e88042019-11-25 09:07:37 +010084 * As in pxelinux, paths to files referenced from files we retrieve are
85 * relative to the location of bootfile. get_relfile takes such a path and
86 * joins it with the bootfile path to get the full path to the target file. If
87 * the bootfile path is NULL, we use file_path as is.
88 *
Simon Glass00302442021-10-14 12:48:01 -060089 * @ctx: PXE context
90 * @file_path: File path to read (relative to the PXE file)
91 * @file_addr: Address to load file to
Simon Glassa9401b92021-10-14 12:48:08 -060092 * @filesizep: If not NULL, returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -060093 * Returns 1 for success, or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +010094 */
Simon Glassb0d08db2021-10-14 12:47:56 -060095static int get_relfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -060096 unsigned long file_addr, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +010097{
98 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +010099 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100100 char addr_buf[18];
Simon Glassa9401b92021-10-14 12:48:08 -0600101 ulong size;
102 int ret;
Patrice Chotard17e88042019-11-25 09:07:37 +0100103
Simon Glass5e3e39a2021-10-14 12:48:05 -0600104 if (file_path[0] == '/' && ctx->allow_abs_path)
105 *relfile = '\0';
106 else
107 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
Patrice Chotard17e88042019-11-25 09:07:37 +0100108
Simon Glass5e3e39a2021-10-14 12:48:05 -0600109 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard17e88042019-11-25 09:07:37 +0100110
111 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100112 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100113
114 return -ENAMETOOLONG;
115 }
116
117 strcat(relfile, file_path);
118
119 printf("Retrieving file: %s\n", relfile);
120
121 sprintf(addr_buf, "%lx", file_addr);
122
Simon Glassa9401b92021-10-14 12:48:08 -0600123 ret = ctx->getfile(ctx, relfile, addr_buf, &size);
124 if (ret < 0)
125 return log_msg_ret("get", ret);
126 if (filesizep)
127 *filesizep = size;
128
129 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100130}
131
Simon Glass00302442021-10-14 12:48:01 -0600132/**
133 * get_pxe_file() - read a file
134 *
135 * The file is read and nul-terminated
136 *
137 * @ctx: PXE context
138 * @file_path: File path to read (relative to the PXE file)
139 * @file_addr: Address to load file to
140 * Returns 1 for success, or < 0 on error
141 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600142int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600143 ulong file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100144{
Simon Glassa9401b92021-10-14 12:48:08 -0600145 ulong size;
Patrice Chotard17e88042019-11-25 09:07:37 +0100146 int err;
147 char *buf;
148
Simon Glassa9401b92021-10-14 12:48:08 -0600149 err = get_relfile(ctx, file_path, file_addr, &size);
Patrice Chotard17e88042019-11-25 09:07:37 +0100150 if (err < 0)
151 return err;
152
Simon Glassa9401b92021-10-14 12:48:08 -0600153 buf = map_sysmem(file_addr + size, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +0100154 *buf = '\0';
155 unmap_sysmem(buf);
156
157 return 1;
158}
159
160#define PXELINUX_DIR "pxelinux.cfg/"
161
Simon Glass00302442021-10-14 12:48:01 -0600162/**
163 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
164 *
165 * @ctx: PXE context
166 * @file: Filename to process (relative to pxelinux.cfg/)
167 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
168 * or other value < 0 on other error
169 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600170int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100171 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100172{
173 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100174 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100175
176 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
177 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100178 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100179 return -ENAMETOOLONG;
180 }
181
182 sprintf(path, PXELINUX_DIR "%s", file);
183
Simon Glassb0d08db2021-10-14 12:47:56 -0600184 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100185}
186
Simon Glass00302442021-10-14 12:48:01 -0600187/**
188 * get_relfile_envaddr() - read a file to an address in an env var
189 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100190 * Wrapper to make it easier to store the file at file_path in the location
191 * specified by envaddr_name. file_path will be joined to the bootfile path,
192 * if any is specified.
193 *
Simon Glass00302442021-10-14 12:48:01 -0600194 * @ctx: PXE context
195 * @file_path: File path to read (relative to the PXE file)
196 * @envaddr_name: Name of environment variable which contains the address to
197 * load to
Simon Glassa9401b92021-10-14 12:48:08 -0600198 * @filesizep: Returns the file size in bytes
Simon Glass00302442021-10-14 12:48:01 -0600199 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
200 * environment variable, -EINVAL if its format is not valid hex, or other
201 * value < 0 on other error
Patrice Chotard17e88042019-11-25 09:07:37 +0100202 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600203static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -0600204 const char *envaddr_name, ulong *filesizep)
Patrice Chotard17e88042019-11-25 09:07:37 +0100205{
206 unsigned long file_addr;
207 char *envaddr;
208
209 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100210 if (!envaddr)
211 return -ENOENT;
212
213 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
214 return -EINVAL;
215
Simon Glassa9401b92021-10-14 12:48:08 -0600216 return get_relfile(ctx, file_path, file_addr, filesizep);
Patrice Chotard17e88042019-11-25 09:07:37 +0100217}
218
Simon Glass00302442021-10-14 12:48:01 -0600219/**
220 * label_create() - crate a new PXE label
221 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100222 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
223 * result must be free()'d to reclaim the memory.
224 *
Simon Glass00302442021-10-14 12:48:01 -0600225 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100226 */
227static struct pxe_label *label_create(void)
228{
229 struct pxe_label *label;
230
231 label = malloc(sizeof(struct pxe_label));
Patrice Chotard17e88042019-11-25 09:07:37 +0100232 if (!label)
233 return NULL;
234
235 memset(label, 0, sizeof(struct pxe_label));
236
237 return label;
238}
239
Simon Glass00302442021-10-14 12:48:01 -0600240/**
241 * label_destroy() - free the memory used by a pxe_label
242 *
243 * This frees @label itself as well as memory used by its name,
244 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
245 * they're non-NULL.
Patrice Chotard17e88042019-11-25 09:07:37 +0100246 *
247 * So - be sure to only use dynamically allocated memory for the members of
248 * the pxe_label struct, unless you want to clean it up first. These are
249 * currently only created by the pxe file parsing code.
Simon Glass00302442021-10-14 12:48:01 -0600250 *
251 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100252 */
253static void label_destroy(struct pxe_label *label)
254{
Simon Glass764d0c02021-10-14 12:48:02 -0600255 free(label->name);
256 free(label->kernel);
257 free(label->config);
258 free(label->append);
259 free(label->initrd);
260 free(label->fdt);
261 free(label->fdtdir);
262 free(label->fdtoverlays);
Patrice Chotard17e88042019-11-25 09:07:37 +0100263 free(label);
264}
265
Simon Glass00302442021-10-14 12:48:01 -0600266/**
267 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100268 *
269 * This is passed as a callback to the menu code for displaying each
270 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600271 *
272 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-11-25 09:07:37 +0100273 */
274static void label_print(void *data)
275{
276 struct pxe_label *label = data;
277 const char *c = label->menu ? label->menu : label->name;
278
279 printf("%s:\t%s\n", label->num, c);
280}
281
Simon Glass00302442021-10-14 12:48:01 -0600282/**
283 * label_localboot() - Boot a label that specified 'localboot'
284 *
285 * This requires that the 'localcmd' environment variable is defined. Its
286 * contents will be executed as U-Boot commands. If the label specified an
287 * 'append' line, its contents will be used to overwrite the contents of the
288 * 'bootargs' environment variable prior to running 'localcmd'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100289 *
Simon Glass00302442021-10-14 12:48:01 -0600290 * @label: Label to process
291 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100292 */
293static int label_localboot(struct pxe_label *label)
294{
295 char *localcmd;
296
297 localcmd = from_env("localcmd");
Patrice Chotard17e88042019-11-25 09:07:37 +0100298 if (!localcmd)
299 return -ENOENT;
300
301 if (label->append) {
302 char bootargs[CONFIG_SYS_CBSIZE];
303
Simon Glassc7b03e82020-11-05 10:33:47 -0700304 cli_simple_process_macros(label->append, bootargs,
305 sizeof(bootargs));
Patrice Chotard17e88042019-11-25 09:07:37 +0100306 env_set("bootargs", bootargs);
307 }
308
309 debug("running: %s\n", localcmd);
310
311 return run_command_list(localcmd, strlen(localcmd), 0);
312}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100313
Simon Glass00302442021-10-14 12:48:01 -0600314/**
315 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
316 *
317 * @ctx: PXE context
318 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100319 */
320#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600321static void label_boot_fdtoverlay(struct pxe_context *ctx,
322 struct pxe_label *label)
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100323{
324 char *fdtoverlay = label->fdtoverlays;
325 struct fdt_header *working_fdt;
326 char *fdtoverlay_addr_env;
327 ulong fdtoverlay_addr;
328 ulong fdt_addr;
329 int err;
330
331 /* Get the main fdt and map it */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600332 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100333 working_fdt = map_sysmem(fdt_addr, 0);
334 err = fdt_check_header(working_fdt);
335 if (err)
336 return;
337
338 /* Get the specific overlay loading address */
339 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
340 if (!fdtoverlay_addr_env) {
341 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
342 return;
343 }
344
Simon Glass3ff49ec2021-07-24 09:03:29 -0600345 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100346
347 /* Cycle over the overlay files and apply them in order */
348 do {
349 struct fdt_header *blob;
350 char *overlayfile;
351 char *end;
352 int len;
353
354 /* Drop leading spaces */
355 while (*fdtoverlay == ' ')
356 ++fdtoverlay;
357
358 /* Copy a single filename if multiple provided */
359 end = strstr(fdtoverlay, " ");
360 if (end) {
361 len = (int)(end - fdtoverlay);
362 overlayfile = malloc(len + 1);
363 strncpy(overlayfile, fdtoverlay, len);
364 overlayfile[len] = '\0';
365 } else
366 overlayfile = fdtoverlay;
367
368 if (!strlen(overlayfile))
369 goto skip_overlay;
370
371 /* Load overlay file */
Simon Glassa9401b92021-10-14 12:48:08 -0600372 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
373 NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100374 if (err < 0) {
375 printf("Failed loading overlay %s\n", overlayfile);
376 goto skip_overlay;
377 }
378
379 /* Resize main fdt */
380 fdt_shrink_to_minimum(working_fdt, 8192);
381
382 blob = map_sysmem(fdtoverlay_addr, 0);
383 err = fdt_check_header(blob);
384 if (err) {
385 printf("Invalid overlay %s, skipping\n",
386 overlayfile);
387 goto skip_overlay;
388 }
389
390 err = fdt_overlay_apply_verbose(working_fdt, blob);
391 if (err) {
392 printf("Failed to apply overlay %s, skipping\n",
393 overlayfile);
394 goto skip_overlay;
395 }
396
397skip_overlay:
398 if (end)
399 free(overlayfile);
400 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
401}
402#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100403
Simon Glass00302442021-10-14 12:48:01 -0600404/**
405 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard17e88042019-11-25 09:07:37 +0100406 *
407 * If we can't boot for any reason, we return. A successful boot never
408 * returns.
409 *
410 * The kernel will be stored in the location given by the 'kernel_addr_r'
411 * environment variable.
412 *
413 * If the label specifies an initrd file, it will be stored in the location
414 * given by the 'ramdisk_addr_r' environment variable.
415 *
416 * If the label specifies an 'append' line, its contents will overwrite that
417 * of the 'bootargs' environment variable.
Simon Glass00302442021-10-14 12:48:01 -0600418 *
419 * @ctx: PXE context
420 * @label: Label to process
421 * Returns does not return on success, otherwise returns 0 if a localboot
422 * label was processed, or 1 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100423 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600424static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +0100425{
426 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700427 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Libc14b932021-10-20 00:18:15 -0700428 char *kernel_addr = NULL;
429 char *initrd_addr_str = NULL;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700430 char initrd_filesize[10];
Zhaofeng Libc14b932021-10-20 00:18:15 -0700431 char initrd_str[28];
Patrice Chotard17e88042019-11-25 09:07:37 +0100432 char mac_str[29] = "";
433 char ip_str[68] = "";
434 char *fit_addr = NULL;
435 int bootm_argc = 2;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700436 int zboot_argc = 3;
Patrice Chotard17e88042019-11-25 09:07:37 +0100437 int len = 0;
Zhaofeng Libc14b932021-10-20 00:18:15 -0700438 ulong kernel_addr_r;
Patrice Chotard17e88042019-11-25 09:07:37 +0100439 void *buf;
440
441 label_print(label);
442
443 label->attempted = 1;
444
445 if (label->localboot) {
446 if (label->localboot_val >= 0)
447 label_localboot(label);
448 return 0;
449 }
450
Patrice Chotard6233de42019-11-25 09:07:39 +0100451 if (!label->kernel) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100452 printf("No kernel given, skipping %s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100453 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100454 return 1;
455 }
456
457 if (label->initrd) {
Simon Glassa9401b92021-10-14 12:48:08 -0600458 ulong size;
459
460 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
461 &size) < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100462 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100463 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100464 return 1;
465 }
466
Zhaofeng Libc14b932021-10-20 00:18:15 -0700467 initrd_addr_str = env_get("ramdisk_addr_r");
Simon Glassa9401b92021-10-14 12:48:08 -0600468 strcpy(initrd_filesize, simple_xtoa(size));
Zhaofeng Libc14b932021-10-20 00:18:15 -0700469
470 strncpy(initrd_str, initrd_addr_str, 18);
471 strcat(initrd_str, ":");
472 strncat(initrd_str, initrd_filesize, 9);
Patrice Chotard17e88042019-11-25 09:07:37 +0100473 }
474
Simon Glassa9401b92021-10-14 12:48:08 -0600475 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
476 NULL) < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100477 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100478 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100479 return 1;
480 }
481
482 if (label->ipappend & 0x1) {
483 sprintf(ip_str, " ip=%s:%s:%s:%s",
484 env_get("ipaddr"), env_get("serverip"),
485 env_get("gatewayip"), env_get("netmask"));
486 }
487
Kory Maincentcaabd242021-02-02 16:42:28 +0100488 if (IS_ENABLED(CONFIG_CMD_NET)) {
489 if (label->ipappend & 0x2) {
490 int err;
Patrice Chotard6233de42019-11-25 09:07:39 +0100491
Kory Maincentcaabd242021-02-02 16:42:28 +0100492 strcpy(mac_str, " BOOTIF=");
493 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
494 if (err < 0)
495 mac_str[0] = '\0';
496 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100497 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100498
499 if ((label->ipappend & 0x3) || label->append) {
500 char bootargs[CONFIG_SYS_CBSIZE] = "";
501 char finalbootargs[CONFIG_SYS_CBSIZE];
502
503 if (strlen(label->append ?: "") +
504 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
505 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
506 strlen(label->append ?: ""),
507 strlen(ip_str), strlen(mac_str),
508 sizeof(bootargs));
509 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100510 }
Patrice Chotard6233de42019-11-25 09:07:39 +0100511
512 if (label->append)
513 strncpy(bootargs, label->append, sizeof(bootargs));
514
515 strcat(bootargs, ip_str);
516 strcat(bootargs, mac_str);
517
Simon Glassc7b03e82020-11-05 10:33:47 -0700518 cli_simple_process_macros(bootargs, finalbootargs,
519 sizeof(finalbootargs));
Patrice Chotard6233de42019-11-25 09:07:39 +0100520 env_set("bootargs", finalbootargs);
521 printf("append: %s\n", finalbootargs);
Patrice Chotard17e88042019-11-25 09:07:37 +0100522 }
523
Zhaofeng Libc14b932021-10-20 00:18:15 -0700524 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700525
Patrice Chotard17e88042019-11-25 09:07:37 +0100526 /* for FIT, append the configuration identifier */
527 if (label->config) {
Zhaofeng Libc14b932021-10-20 00:18:15 -0700528 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100529
530 fit_addr = malloc(len);
531 if (!fit_addr) {
532 printf("malloc fail (FIT address)\n");
533 return 1;
534 }
Zhaofeng Libc14b932021-10-20 00:18:15 -0700535 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
536 kernel_addr = fit_addr;
Patrice Chotard17e88042019-11-25 09:07:37 +0100537 }
538
539 /*
540 * fdt usage is optional:
Anton Leontievfeb7db82019-09-03 10:52:24 +0300541 * It handles the following scenarios.
Patrice Chotard17e88042019-11-25 09:07:37 +0100542 *
Anton Leontievfeb7db82019-09-03 10:52:24 +0300543 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
544 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
545 * bootm, and adjust argc appropriately.
546 *
547 * If retrieve fails and no exact fdt blob is specified in pxe file with
548 * "fdt" label, try Scenario 2.
Patrice Chotard17e88042019-11-25 09:07:37 +0100549 *
550 * Scenario 2: If there is an fdt_addr specified, pass it along to
551 * bootm, and adjust argc appropriately.
552 *
553 * Scenario 3: fdt blob is not available.
554 */
555 bootm_argv[3] = env_get("fdt_addr_r");
556
557 /* if fdt label is defined then get fdt from server */
558 if (bootm_argv[3]) {
559 char *fdtfile = NULL;
560 char *fdtfilefree = NULL;
561
562 if (label->fdt) {
563 fdtfile = label->fdt;
564 } else if (label->fdtdir) {
565 char *f1, *f2, *f3, *f4, *slash;
566
567 f1 = env_get("fdtfile");
568 if (f1) {
569 f2 = "";
570 f3 = "";
571 f4 = "";
572 } else {
573 /*
574 * For complex cases where this code doesn't
575 * generate the correct filename, the board
576 * code should set $fdtfile during early boot,
577 * or the boot scripts should set $fdtfile
578 * before invoking "pxe" or "sysboot".
579 */
580 f1 = env_get("soc");
581 f2 = "-";
582 f3 = env_get("board");
583 f4 = ".dtb";
Dimitri John Ledkov5ee984a2021-04-21 12:42:01 +0100584 if (!f1) {
585 f1 = "";
586 f2 = "";
587 }
588 if (!f3) {
589 f2 = "";
590 f3 = "";
591 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100592 }
593
594 len = strlen(label->fdtdir);
595 if (!len)
596 slash = "./";
597 else if (label->fdtdir[len - 1] != '/')
598 slash = "/";
599 else
600 slash = "";
601
602 len = strlen(label->fdtdir) + strlen(slash) +
603 strlen(f1) + strlen(f2) + strlen(f3) +
604 strlen(f4) + 1;
605 fdtfilefree = malloc(len);
606 if (!fdtfilefree) {
607 printf("malloc fail (FDT filename)\n");
608 goto cleanup;
609 }
610
611 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
612 label->fdtdir, slash, f1, f2, f3, f4);
613 fdtfile = fdtfilefree;
614 }
615
616 if (fdtfile) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600617 int err = get_relfile_envaddr(ctx, fdtfile,
Simon Glassa9401b92021-10-14 12:48:08 -0600618 "fdt_addr_r", NULL);
Patrice Chotard6233de42019-11-25 09:07:39 +0100619
Patrice Chotard17e88042019-11-25 09:07:37 +0100620 free(fdtfilefree);
621 if (err < 0) {
Anton Leontievfeb7db82019-09-03 10:52:24 +0300622 bootm_argv[3] = NULL;
623
624 if (label->fdt) {
625 printf("Skipping %s for failure retrieving FDT\n",
626 label->name);
627 goto cleanup;
628 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100629 }
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100630
631#ifdef CONFIG_OF_LIBFDT_OVERLAY
632 if (label->fdtoverlays)
Simon Glassb0d08db2021-10-14 12:47:56 -0600633 label_boot_fdtoverlay(ctx, label);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100634#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100635 } else {
636 bootm_argv[3] = NULL;
637 }
638 }
639
Zhaofeng Libc14b932021-10-20 00:18:15 -0700640 bootm_argv[1] = kernel_addr;
641 zboot_argv[1] = kernel_addr;
642
643 if (initrd_addr_str) {
644 bootm_argv[2] = initrd_str;
645 bootm_argc = 3;
646
647 zboot_argv[3] = initrd_addr_str;
648 zboot_argv[4] = initrd_filesize;
649 zboot_argc = 5;
650 }
651
Patrice Chotard17e88042019-11-25 09:07:37 +0100652 if (!bootm_argv[3])
653 bootm_argv[3] = env_get("fdt_addr");
654
655 if (bootm_argv[3]) {
656 if (!bootm_argv[2])
657 bootm_argv[2] = "-";
658 bootm_argc = 4;
659 }
660
Zhaofeng Libc14b932021-10-20 00:18:15 -0700661 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
662 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard17e88042019-11-25 09:07:37 +0100663 /* Try bootm for legacy and FIT format image */
664 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
Simon Glassb0d08db2021-10-14 12:47:56 -0600665 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100666 /* Try booting an AArch64 Linux kernel image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100667 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassb0d08db2021-10-14 12:47:56 -0600668 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100669 /* Try booting a Image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100670 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassb0d08db2021-10-14 12:47:56 -0600671 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincentb2187d02021-02-02 16:42:29 +0100672 /* Try booting an x86_64 Linux kernel image */
673 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassb0d08db2021-10-14 12:47:56 -0600674 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentcaabd242021-02-02 16:42:28 +0100675
Patrice Chotard17e88042019-11-25 09:07:37 +0100676 unmap_sysmem(buf);
677
678cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600679 free(fit_addr);
680
Patrice Chotard17e88042019-11-25 09:07:37 +0100681 return 1;
682}
683
Simon Glass00302442021-10-14 12:48:01 -0600684/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100685enum token_type {
686 T_EOL,
687 T_STRING,
688 T_EOF,
689 T_MENU,
690 T_TITLE,
691 T_TIMEOUT,
692 T_LABEL,
693 T_KERNEL,
694 T_LINUX,
695 T_APPEND,
696 T_INITRD,
697 T_LOCALBOOT,
698 T_DEFAULT,
699 T_PROMPT,
700 T_INCLUDE,
701 T_FDT,
702 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100703 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100704 T_ONTIMEOUT,
705 T_IPAPPEND,
706 T_BACKGROUND,
707 T_INVALID
708};
709
Simon Glass00302442021-10-14 12:48:01 -0600710/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100711struct token {
712 char *val;
713 enum token_type type;
714};
715
Simon Glass00302442021-10-14 12:48:01 -0600716/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100717static const struct token keywords[] = {
718 {"menu", T_MENU},
719 {"title", T_TITLE},
720 {"timeout", T_TIMEOUT},
721 {"default", T_DEFAULT},
722 {"prompt", T_PROMPT},
723 {"label", T_LABEL},
724 {"kernel", T_KERNEL},
725 {"linux", T_LINUX},
726 {"localboot", T_LOCALBOOT},
727 {"append", T_APPEND},
728 {"initrd", T_INITRD},
729 {"include", T_INCLUDE},
730 {"devicetree", T_FDT},
731 {"fdt", T_FDT},
732 {"devicetreedir", T_FDTDIR},
733 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100734 {"fdtoverlays", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100735 {"ontimeout", T_ONTIMEOUT,},
736 {"ipappend", T_IPAPPEND,},
737 {"background", T_BACKGROUND,},
738 {NULL, T_INVALID}
739};
740
Simon Glass00302442021-10-14 12:48:01 -0600741/**
742 * enum lex_state - lexer state
743 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100744 * Since pxe(linux) files don't have a token to identify the start of a
745 * literal, we have to keep track of when we're in a state where a literal is
746 * expected vs when we're in a state a keyword is expected.
747 */
748enum lex_state {
749 L_NORMAL = 0,
750 L_KEYWORD,
751 L_SLITERAL
752};
753
Simon Glass00302442021-10-14 12:48:01 -0600754/**
755 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100756 *
Simon Glass00302442021-10-14 12:48:01 -0600757 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100758 *
759 * Characters from *p are copied into t-val until a character equal to
760 * delim is found, or a NUL byte is reached. If delim has the special value of
761 * ' ', any whitespace character will be used as a delimiter.
762 *
763 * If lower is unequal to 0, uppercase characters will be converted to
764 * lowercase in the result. This is useful to make keywords case
765 * insensitive.
766 *
767 * The location of *p is updated to point to the first character after the end
768 * of the token - the ending delimiter.
769 *
Simon Glass00302442021-10-14 12:48:01 -0600770 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
771 * it.
772 *
773 * @p: Points to a pointer to the current position in the input being processed.
774 * Updated to point at the first character after the current token
775 * @t: Pointers to a token to fill in
776 * @delim: Delimiter character to look for, either newline or space
777 * @lower: true to convert the string to lower case when storing
778 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100779 */
780static char *get_string(char **p, struct token *t, char delim, int lower)
781{
782 char *b, *e;
783 size_t len, i;
784
785 /*
786 * b and e both start at the beginning of the input stream.
787 *
788 * e is incremented until we find the ending delimiter, or a NUL byte
789 * is reached. Then, we take e - b to find the length of the token.
790 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100791 b = *p;
792 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100793 while (*e) {
794 if ((delim == ' ' && isspace(*e)) || delim == *e)
795 break;
796 e++;
797 }
798
799 len = e - b;
800
801 /*
802 * Allocate memory to hold the string, and copy it in, converting
803 * characters to lowercase if lower is != 0.
804 */
805 t->val = malloc(len + 1);
806 if (!t->val)
807 return NULL;
808
809 for (i = 0; i < len; i++, b++) {
810 if (lower)
811 t->val[i] = tolower(*b);
812 else
813 t->val[i] = *b;
814 }
815
816 t->val[len] = '\0';
817
Simon Glass764d0c02021-10-14 12:48:02 -0600818 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100819 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100820 t->type = T_STRING;
821
822 return t->val;
823}
824
Simon Glass00302442021-10-14 12:48:01 -0600825/**
826 * get_keyword() - Populate a keyword token with a type and value
827 *
828 * Updates the ->type field based on the keyword string in @val
829 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100830 */
831static void get_keyword(struct token *t)
832{
833 int i;
834
835 for (i = 0; keywords[i].val; i++) {
836 if (!strcmp(t->val, keywords[i].val)) {
837 t->type = keywords[i].type;
838 break;
839 }
840 }
841}
842
Simon Glass00302442021-10-14 12:48:01 -0600843/**
844 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100845 *
Simon Glass00302442021-10-14 12:48:01 -0600846 * We have to keep track of which state we're in to know if we're looking to get
847 * a string literal or a keyword.
848 *
849 * @p: Points to a pointer to the current position in the input being processed.
850 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100851 */
852static void get_token(char **p, struct token *t, enum lex_state state)
853{
854 char *c = *p;
855
856 t->type = T_INVALID;
857
858 /* eat non EOL whitespace */
859 while (isblank(*c))
860 c++;
861
862 /*
863 * eat comments. note that string literals can't begin with #, but
864 * can contain a # after their first character.
865 */
866 if (*c == '#') {
867 while (*c && *c != '\n')
868 c++;
869 }
870
871 if (*c == '\n') {
872 t->type = T_EOL;
873 c++;
874 } else if (*c == '\0') {
875 t->type = T_EOF;
876 c++;
877 } else if (state == L_SLITERAL) {
878 get_string(&c, t, '\n', 0);
879 } else if (state == L_KEYWORD) {
880 /*
881 * when we expect a keyword, we first get the next string
882 * token delimited by whitespace, and then check if it
883 * matches a keyword in our keyword list. if it does, it's
884 * converted to a keyword token of the appropriate type, and
885 * if not, it remains a string token.
886 */
887 get_string(&c, t, ' ', 1);
888 get_keyword(t);
889 }
890
891 *p = c;
892}
893
Simon Glass00302442021-10-14 12:48:01 -0600894/**
895 * eol_or_eof() - Find end of line
896 *
897 * Increment *c until we get to the end of the current line, or EOF
898 *
899 * @c: Points to a pointer to the current position in the input being processed.
900 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100901 */
902static void eol_or_eof(char **c)
903{
904 while (**c && **c != '\n')
905 (*c)++;
906}
907
908/*
909 * All of these parse_* functions share some common behavior.
910 *
911 * They finish with *c pointing after the token they parse, and return 1 on
912 * success, or < 0 on error.
913 */
914
915/*
916 * Parse a string literal and store a pointer it at *dst. String literals
917 * terminate at the end of the line.
918 */
919static int parse_sliteral(char **c, char **dst)
920{
921 struct token t;
922 char *s = *c;
923
924 get_token(c, &t, L_SLITERAL);
925
926 if (t.type != T_STRING) {
927 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
928 return -EINVAL;
929 }
930
931 *dst = t.val;
932
933 return 1;
934}
935
936/*
937 * Parse a base 10 (unsigned) integer and store it at *dst.
938 */
939static int parse_integer(char **c, int *dst)
940{
941 struct token t;
942 char *s = *c;
943
944 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +0100945 if (t.type != T_STRING) {
946 printf("Expected string: %.*s\n", (int)(*c - s), s);
947 return -EINVAL;
948 }
949
950 *dst = simple_strtol(t.val, NULL, 10);
951
952 free(t.val);
953
954 return 1;
955}
956
Simon Glassb0d08db2021-10-14 12:47:56 -0600957static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100958 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +0100959
960/*
961 * Parse an include statement, and retrieve and parse the file it mentions.
962 *
963 * base should point to a location where it's safe to store the file, and
964 * nest_level should indicate how many nested includes have occurred. For this
965 * include, nest_level has already been incremented and doesn't need to be
966 * incremented here.
967 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600968static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100969 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +0100970{
971 char *include_path;
972 char *s = *c;
973 int err;
974 char *buf;
975 int ret;
976
977 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100978 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100979 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +0100980 return err;
981 }
982
Simon Glassb0d08db2021-10-14 12:47:56 -0600983 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +0100984 if (err < 0) {
985 printf("Couldn't retrieve %s\n", include_path);
986 return err;
987 }
988
989 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -0600990 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +0100991 unmap_sysmem(buf);
992
993 return ret;
994}
995
996/*
997 * Parse lines that begin with 'menu'.
998 *
999 * base and nest are provided to handle the 'menu include' case.
1000 *
1001 * base should point to a location where it's safe to store the included file.
1002 *
1003 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1004 * a file it includes, 3 when parsing a file included by that file, and so on.
1005 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001006static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001007 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001008{
1009 struct token t;
1010 char *s = *c;
1011 int err = 0;
1012
1013 get_token(c, &t, L_KEYWORD);
1014
1015 switch (t.type) {
1016 case T_TITLE:
1017 err = parse_sliteral(c, &cfg->title);
1018
1019 break;
1020
1021 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001022 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001023 break;
1024
1025 case T_BACKGROUND:
1026 err = parse_sliteral(c, &cfg->bmp);
1027 break;
1028
1029 default:
1030 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001031 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001032 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001033 if (err < 0)
1034 return err;
1035
1036 eol_or_eof(c);
1037
1038 return 1;
1039}
1040
1041/*
1042 * Handles parsing a 'menu line' when we're parsing a label.
1043 */
1044static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001045 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001046{
1047 struct token t;
1048 char *s;
1049
1050 s = *c;
1051
1052 get_token(c, &t, L_KEYWORD);
1053
1054 switch (t.type) {
1055 case T_DEFAULT:
1056 if (!cfg->default_label)
1057 cfg->default_label = strdup(label->name);
1058
1059 if (!cfg->default_label)
1060 return -ENOMEM;
1061
1062 break;
1063 case T_LABEL:
1064 parse_sliteral(c, &label->menu);
1065 break;
1066 default:
1067 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001068 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001069 }
1070
1071 eol_or_eof(c);
1072
1073 return 0;
1074}
1075
1076/*
1077 * Handles parsing a 'kernel' label.
1078 * expecting "filename" or "<fit_filename>#cfg"
1079 */
1080static int parse_label_kernel(char **c, struct pxe_label *label)
1081{
1082 char *s;
1083 int err;
1084
1085 err = parse_sliteral(c, &label->kernel);
1086 if (err < 0)
1087 return err;
1088
1089 s = strstr(label->kernel, "#");
1090 if (!s)
1091 return 1;
1092
1093 label->config = malloc(strlen(s) + 1);
1094 if (!label->config)
1095 return -ENOMEM;
1096
1097 strcpy(label->config, s);
1098 *s = 0;
1099
1100 return 1;
1101}
1102
1103/*
1104 * Parses a label and adds it to the list of labels for a menu.
1105 *
1106 * A label ends when we either get to the end of a file, or
1107 * get some input we otherwise don't have a handler defined
1108 * for.
1109 *
1110 */
1111static int parse_label(char **c, struct pxe_menu *cfg)
1112{
1113 struct token t;
1114 int len;
1115 char *s = *c;
1116 struct pxe_label *label;
1117 int err;
1118
1119 label = label_create();
1120 if (!label)
1121 return -ENOMEM;
1122
1123 err = parse_sliteral(c, &label->name);
1124 if (err < 0) {
1125 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1126 label_destroy(label);
1127 return -EINVAL;
1128 }
1129
1130 list_add_tail(&label->list, &cfg->labels);
1131
1132 while (1) {
1133 s = *c;
1134 get_token(c, &t, L_KEYWORD);
1135
1136 err = 0;
1137 switch (t.type) {
1138 case T_MENU:
1139 err = parse_label_menu(c, cfg, label);
1140 break;
1141
1142 case T_KERNEL:
1143 case T_LINUX:
1144 err = parse_label_kernel(c, label);
1145 break;
1146
1147 case T_APPEND:
1148 err = parse_sliteral(c, &label->append);
1149 if (label->initrd)
1150 break;
1151 s = strstr(label->append, "initrd=");
1152 if (!s)
1153 break;
1154 s += 7;
1155 len = (int)(strchr(s, ' ') - s);
1156 label->initrd = malloc(len + 1);
1157 strncpy(label->initrd, s, len);
1158 label->initrd[len] = '\0';
1159
1160 break;
1161
1162 case T_INITRD:
1163 if (!label->initrd)
1164 err = parse_sliteral(c, &label->initrd);
1165 break;
1166
1167 case T_FDT:
1168 if (!label->fdt)
1169 err = parse_sliteral(c, &label->fdt);
1170 break;
1171
1172 case T_FDTDIR:
1173 if (!label->fdtdir)
1174 err = parse_sliteral(c, &label->fdtdir);
1175 break;
1176
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001177 case T_FDTOVERLAYS:
1178 if (!label->fdtoverlays)
1179 err = parse_sliteral(c, &label->fdtoverlays);
1180 break;
1181
Patrice Chotard17e88042019-11-25 09:07:37 +01001182 case T_LOCALBOOT:
1183 label->localboot = 1;
1184 err = parse_integer(c, &label->localboot_val);
1185 break;
1186
1187 case T_IPAPPEND:
1188 err = parse_integer(c, &label->ipappend);
1189 break;
1190
1191 case T_EOL:
1192 break;
1193
1194 default:
1195 /*
1196 * put the token back! we don't want it - it's the end
1197 * of a label and whatever token this is, it's
1198 * something for the menu level context to handle.
1199 */
1200 *c = s;
1201 return 1;
1202 }
1203
1204 if (err < 0)
1205 return err;
1206 }
1207}
1208
1209/*
1210 * This 16 comes from the limit pxelinux imposes on nested includes.
1211 *
1212 * There is no reason at all we couldn't do more, but some limit helps prevent
1213 * infinite (until crash occurs) recursion if a file tries to include itself.
1214 */
1215#define MAX_NEST_LEVEL 16
1216
1217/*
1218 * Entry point for parsing a menu file. nest_level indicates how many times
1219 * we've nested in includes. It will be 1 for the top level menu file.
1220 *
1221 * Returns 1 on success, < 0 on error.
1222 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001223static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001224 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001225{
1226 struct token t;
1227 char *s, *b, *label_name;
1228 int err;
1229
1230 b = p;
1231
1232 if (nest_level > MAX_NEST_LEVEL) {
1233 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1234 return -EMLINK;
1235 }
1236
1237 while (1) {
1238 s = p;
1239
1240 get_token(&p, &t, L_KEYWORD);
1241
1242 err = 0;
1243 switch (t.type) {
1244 case T_MENU:
1245 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001246 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001247 base + ALIGN(strlen(b) + 1, 4),
1248 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001249 break;
1250
1251 case T_TIMEOUT:
1252 err = parse_integer(&p, &cfg->timeout);
1253 break;
1254
1255 case T_LABEL:
1256 err = parse_label(&p, cfg);
1257 break;
1258
1259 case T_DEFAULT:
1260 case T_ONTIMEOUT:
1261 err = parse_sliteral(&p, &label_name);
1262
1263 if (label_name) {
1264 if (cfg->default_label)
1265 free(cfg->default_label);
1266
1267 cfg->default_label = label_name;
1268 }
1269
1270 break;
1271
1272 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001273 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001274 base + ALIGN(strlen(b), 4), cfg,
1275 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001276 break;
1277
1278 case T_PROMPT:
1279 eol_or_eof(&p);
1280 break;
1281
1282 case T_EOL:
1283 break;
1284
1285 case T_EOF:
1286 return 1;
1287
1288 default:
1289 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001290 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001291 eol_or_eof(&p);
1292 }
1293
1294 if (err < 0)
1295 return err;
1296 }
1297}
1298
1299/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001300 */
1301void destroy_pxe_menu(struct pxe_menu *cfg)
1302{
1303 struct list_head *pos, *n;
1304 struct pxe_label *label;
1305
Simon Glass764d0c02021-10-14 12:48:02 -06001306 free(cfg->title);
1307 free(cfg->default_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001308
1309 list_for_each_safe(pos, n, &cfg->labels) {
1310 label = list_entry(pos, struct pxe_label, list);
1311
1312 label_destroy(label);
1313 }
1314
1315 free(cfg);
1316}
1317
Simon Glassb0d08db2021-10-14 12:47:56 -06001318struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001319{
1320 struct pxe_menu *cfg;
1321 char *buf;
1322 int r;
1323
1324 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001325 if (!cfg)
1326 return NULL;
1327
1328 memset(cfg, 0, sizeof(struct pxe_menu));
1329
1330 INIT_LIST_HEAD(&cfg->labels);
1331
1332 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001333 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001334 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001335 if (r < 0) {
1336 destroy_pxe_menu(cfg);
1337 return NULL;
1338 }
1339
1340 return cfg;
1341}
1342
1343/*
1344 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1345 * menu code.
1346 */
1347static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1348{
1349 struct pxe_label *label;
1350 struct list_head *pos;
1351 struct menu *m;
1352 int err;
1353 int i = 1;
1354 char *default_num = NULL;
1355
1356 /*
1357 * Create a menu and add items for all the labels.
1358 */
1359 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -07001360 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001361 if (!m)
1362 return NULL;
1363
1364 list_for_each(pos, &cfg->labels) {
1365 label = list_entry(pos, struct pxe_label, list);
1366
1367 sprintf(label->num, "%d", i++);
1368 if (menu_item_add(m, label->num, label) != 1) {
1369 menu_destroy(m);
1370 return NULL;
1371 }
1372 if (cfg->default_label &&
1373 (strcmp(label->name, cfg->default_label) == 0))
1374 default_num = label->num;
Patrice Chotard17e88042019-11-25 09:07:37 +01001375 }
1376
1377 /*
1378 * After we've created items for each label in the menu, set the
1379 * menu's default label if one was specified.
1380 */
1381 if (default_num) {
1382 err = menu_default_set(m, default_num);
1383 if (err != 1) {
1384 if (err != -ENOENT) {
1385 menu_destroy(m);
1386 return NULL;
1387 }
1388
1389 printf("Missing default: %s\n", cfg->default_label);
1390 }
1391 }
1392
1393 return m;
1394}
1395
1396/*
1397 * Try to boot any labels we have yet to attempt to boot.
1398 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001399static void boot_unattempted_labels(struct pxe_context *ctx,
1400 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001401{
1402 struct list_head *pos;
1403 struct pxe_label *label;
1404
1405 list_for_each(pos, &cfg->labels) {
1406 label = list_entry(pos, struct pxe_label, list);
1407
1408 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001409 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001410 }
1411}
1412
Simon Glassb0d08db2021-10-14 12:47:56 -06001413void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001414{
1415 void *choice;
1416 struct menu *m;
1417 int err;
1418
Kory Maincentcaabd242021-02-02 16:42:28 +01001419 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1420 /* display BMP if available */
1421 if (cfg->bmp) {
Simon Glassa9401b92021-10-14 12:48:08 -06001422 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
Kory Maincentcaabd242021-02-02 16:42:28 +01001423 if (CONFIG_IS_ENABLED(CMD_CLS))
1424 run_command("cls", 0);
1425 bmp_display(image_load_addr,
1426 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1427 } else {
1428 printf("Skipping background bmp %s for failure\n",
1429 cfg->bmp);
1430 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001431 }
1432 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001433
1434 m = pxe_menu_to_menu(cfg);
1435 if (!m)
1436 return;
1437
1438 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001439 menu_destroy(m);
1440
1441 /*
1442 * err == 1 means we got a choice back from menu_get_choice.
1443 *
1444 * err == -ENOENT if the menu was setup to select the default but no
1445 * default was set. in that case, we should continue trying to boot
1446 * labels that haven't been attempted yet.
1447 *
1448 * otherwise, the user interrupted or there was some other error and
1449 * we give up.
1450 */
1451
1452 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001453 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001454 if (!err)
1455 return;
1456 } else if (err != -ENOENT) {
1457 return;
1458 }
1459
Simon Glassb0d08db2021-10-14 12:47:56 -06001460 boot_unattempted_labels(ctx, cfg);
1461}
1462
Simon Glasse719fe02021-10-14 12:48:04 -06001463int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1464 pxe_getfile_func getfile, void *userdata,
1465 bool allow_abs_path, const char *bootfile)
Simon Glassb0d08db2021-10-14 12:47:56 -06001466{
Simon Glasse719fe02021-10-14 12:48:04 -06001467 const char *last_slash;
1468 size_t path_len = 0;
1469
1470 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001471 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001472 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001473 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001474 ctx->allow_abs_path = allow_abs_path;
Simon Glasse719fe02021-10-14 12:48:04 -06001475
1476 /* figure out the boot directory, if there is one */
1477 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1478 return -ENOSPC;
1479 ctx->bootdir = strdup(bootfile ? bootfile : "");
1480 if (!ctx->bootdir)
1481 return -ENOMEM;
1482
1483 if (bootfile) {
1484 last_slash = strrchr(bootfile, '/');
1485 if (last_slash)
1486 path_len = (last_slash - bootfile) + 1;
1487 }
1488 ctx->bootdir[path_len] = '\0';
1489
1490 return 0;
1491}
1492
1493void pxe_destroy_ctx(struct pxe_context *ctx)
1494{
1495 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001496}
Simon Glass791bbfe2021-10-14 12:48:03 -06001497
1498int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1499{
1500 struct pxe_menu *cfg;
1501
1502 cfg = parse_pxefile(ctx, pxefile_addr_r);
1503 if (!cfg) {
1504 printf("Error parsing config file\n");
1505 return 1;
1506 }
1507
1508 if (prompt)
1509 cfg->prompt = 1;
1510
1511 handle_pxe_menu(ctx, cfg);
1512
1513 destroy_pxe_menu(cfg);
1514
1515 return 0;
1516}