blob: 6ecd15ceadac0dc06fbec3faaf5fd9d50f774985 [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 Glass00302442021-10-14 12:48:01 -060033/**
34 * format_mac_pxe() - obtain a MAC address in the PXE format
35 *
36 * This produces a MAC-address string in the format for the current ethernet
37 * device:
38 *
39 * 01-aa-bb-cc-dd-ee-ff
40 *
41 * where aa-ff is the MAC address in hex
42 *
43 * @outbuf: Buffer to write string to
44 * @outbuf_len: length of buffer
45 * @return 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
46 * current ethernet device
47 */
Patrice Chotard17e88042019-11-25 09:07:37 +010048int format_mac_pxe(char *outbuf, size_t outbuf_len)
49{
50 uchar ethaddr[6];
51
52 if (outbuf_len < 21) {
53 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Simon Glass00302442021-10-14 12:48:01 -060054 return -ENOSPC;
Patrice Chotard17e88042019-11-25 09:07:37 +010055 }
56
57 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
58 return -ENOENT;
59
60 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
61 ethaddr[0], ethaddr[1], ethaddr[2],
62 ethaddr[3], ethaddr[4], ethaddr[5]);
63
64 return 1;
65}
66
Simon Glass00302442021-10-14 12:48:01 -060067/**
68 * get_bootfile_path() - Figure out the path of a file to read
69 *
70 * Returns the directory the file specified in the 'bootfile' env variable is
Patrice Chotard17e88042019-11-25 09:07:37 +010071 * in. If bootfile isn't defined in the environment, return NULL, which should
72 * be interpreted as "don't prepend anything to paths".
Simon Glass00302442021-10-14 12:48:01 -060073 *
74 * @file_path: File path to read (relative to the PXE file)
75 * @bootfile_path: Place to put the bootfile path
76 * @bootfile_path_size: Size of @bootfile_path in bytes
77 * @allow_abs_path: true to allow an absolute path (where @file_path starts with
78 * '/', false to return an empty path (and success) in that case
79 * Returns 1 for success, -ENOSPC if bootfile_path_size is to small to hold the
80 * resulting path
Patrice Chotard17e88042019-11-25 09:07:37 +010081 */
82static int get_bootfile_path(const char *file_path, char *bootfile_path,
Simon Glass3ae416a2021-10-14 12:47:59 -060083 size_t bootfile_path_size, bool allow_abs_path)
Patrice Chotard17e88042019-11-25 09:07:37 +010084{
85 char *bootfile, *last_slash;
86 size_t path_len = 0;
87
88 /* Only syslinux allows absolute paths */
Simon Glass3ae416a2021-10-14 12:47:59 -060089 if (file_path[0] == '/' && allow_abs_path)
Patrice Chotard17e88042019-11-25 09:07:37 +010090 goto ret;
91
92 bootfile = from_env("bootfile");
Patrice Chotard17e88042019-11-25 09:07:37 +010093 if (!bootfile)
94 goto ret;
95
96 last_slash = strrchr(bootfile, '/');
Patrice Chotard6233de42019-11-25 09:07:39 +010097 if (!last_slash)
Patrice Chotard17e88042019-11-25 09:07:37 +010098 goto ret;
99
100 path_len = (last_slash - bootfile) + 1;
101
102 if (bootfile_path_size < path_len) {
103 printf("bootfile_path too small. (%zd < %zd)\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100104 bootfile_path_size, path_len);
Patrice Chotard17e88042019-11-25 09:07:37 +0100105
Simon Glass00302442021-10-14 12:48:01 -0600106 return -ENOSPC;
Patrice Chotard17e88042019-11-25 09:07:37 +0100107 }
108
109 strncpy(bootfile_path, bootfile, path_len);
110
111 ret:
112 bootfile_path[path_len] = '\0';
113
114 return 1;
115}
116
Simon Glass00302442021-10-14 12:48:01 -0600117/**
118 * get_relfile() - read a file relative to the PXE file
119 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100120 * As in pxelinux, paths to files referenced from files we retrieve are
121 * relative to the location of bootfile. get_relfile takes such a path and
122 * joins it with the bootfile path to get the full path to the target file. If
123 * the bootfile path is NULL, we use file_path as is.
124 *
Simon Glass00302442021-10-14 12:48:01 -0600125 * @ctx: PXE context
126 * @file_path: File path to read (relative to the PXE file)
127 * @file_addr: Address to load file to
128 * Returns 1 for success, or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100129 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600130static int get_relfile(struct pxe_context *ctx, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100131 unsigned long file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100132{
133 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100134 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100135 char addr_buf[18];
136 int err;
137
Simon Glass3ae416a2021-10-14 12:47:59 -0600138 err = get_bootfile_path(file_path, relfile, sizeof(relfile),
139 ctx->allow_abs_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100140 if (err < 0)
141 return err;
142
143 path_len = strlen(file_path);
144 path_len += strlen(relfile);
145
146 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100147 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100148
149 return -ENAMETOOLONG;
150 }
151
152 strcat(relfile, file_path);
153
154 printf("Retrieving file: %s\n", relfile);
155
156 sprintf(addr_buf, "%lx", file_addr);
157
Simon Glass44a20ef2021-10-14 12:47:57 -0600158 return ctx->getfile(ctx, relfile, addr_buf);
Patrice Chotard17e88042019-11-25 09:07:37 +0100159}
160
Simon Glass00302442021-10-14 12:48:01 -0600161/**
162 * get_pxe_file() - read a file
163 *
164 * The file is read and nul-terminated
165 *
166 * @ctx: PXE context
167 * @file_path: File path to read (relative to the PXE file)
168 * @file_addr: Address to load file to
169 * Returns 1 for success, or < 0 on error
170 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600171int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100172 unsigned long file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100173{
174 unsigned long config_file_size;
175 char *tftp_filesize;
176 int err;
177 char *buf;
178
Simon Glassb0d08db2021-10-14 12:47:56 -0600179 err = get_relfile(ctx, file_path, file_addr);
Patrice Chotard17e88042019-11-25 09:07:37 +0100180 if (err < 0)
181 return err;
182
183 /*
184 * the file comes without a NUL byte at the end, so find out its size
185 * and add the NUL byte.
186 */
187 tftp_filesize = from_env("filesize");
Patrice Chotard17e88042019-11-25 09:07:37 +0100188 if (!tftp_filesize)
189 return -ENOENT;
190
191 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
192 return -EINVAL;
193
194 buf = map_sysmem(file_addr + config_file_size, 1);
195 *buf = '\0';
196 unmap_sysmem(buf);
197
198 return 1;
199}
200
201#define PXELINUX_DIR "pxelinux.cfg/"
202
Simon Glass00302442021-10-14 12:48:01 -0600203/**
204 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
205 *
206 * @ctx: PXE context
207 * @file: Filename to process (relative to pxelinux.cfg/)
208 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
209 * or other value < 0 on other error
210 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600211int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100212 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100213{
214 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100215 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100216
217 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
218 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100219 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100220 return -ENAMETOOLONG;
221 }
222
223 sprintf(path, PXELINUX_DIR "%s", file);
224
Simon Glassb0d08db2021-10-14 12:47:56 -0600225 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100226}
227
Simon Glass00302442021-10-14 12:48:01 -0600228/**
229 * get_relfile_envaddr() - read a file to an address in an env var
230 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100231 * Wrapper to make it easier to store the file at file_path in the location
232 * specified by envaddr_name. file_path will be joined to the bootfile path,
233 * if any is specified.
234 *
Simon Glass00302442021-10-14 12:48:01 -0600235 * @ctx: PXE context
236 * @file_path: File path to read (relative to the PXE file)
237 * @envaddr_name: Name of environment variable which contains the address to
238 * load to
239 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
240 * environment variable, -EINVAL if its format is not valid hex, or other
241 * value < 0 on other error
Patrice Chotard17e88042019-11-25 09:07:37 +0100242 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600243static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100244 const char *envaddr_name)
Patrice Chotard17e88042019-11-25 09:07:37 +0100245{
246 unsigned long file_addr;
247 char *envaddr;
248
249 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100250 if (!envaddr)
251 return -ENOENT;
252
253 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
254 return -EINVAL;
255
Simon Glassb0d08db2021-10-14 12:47:56 -0600256 return get_relfile(ctx, file_path, file_addr);
Patrice Chotard17e88042019-11-25 09:07:37 +0100257}
258
Simon Glass00302442021-10-14 12:48:01 -0600259/**
260 * label_create() - crate a new PXE label
261 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100262 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
263 * result must be free()'d to reclaim the memory.
264 *
Simon Glass00302442021-10-14 12:48:01 -0600265 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100266 */
267static struct pxe_label *label_create(void)
268{
269 struct pxe_label *label;
270
271 label = malloc(sizeof(struct pxe_label));
Patrice Chotard17e88042019-11-25 09:07:37 +0100272 if (!label)
273 return NULL;
274
275 memset(label, 0, sizeof(struct pxe_label));
276
277 return label;
278}
279
Simon Glass00302442021-10-14 12:48:01 -0600280/**
281 * label_destroy() - free the memory used by a pxe_label
282 *
283 * This frees @label itself as well as memory used by its name,
284 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
285 * they're non-NULL.
Patrice Chotard17e88042019-11-25 09:07:37 +0100286 *
287 * So - be sure to only use dynamically allocated memory for the members of
288 * the pxe_label struct, unless you want to clean it up first. These are
289 * currently only created by the pxe file parsing code.
Simon Glass00302442021-10-14 12:48:01 -0600290 *
291 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100292 */
293static void label_destroy(struct pxe_label *label)
294{
Simon Glass764d0c02021-10-14 12:48:02 -0600295 free(label->name);
296 free(label->kernel);
297 free(label->config);
298 free(label->append);
299 free(label->initrd);
300 free(label->fdt);
301 free(label->fdtdir);
302 free(label->fdtoverlays);
Patrice Chotard17e88042019-11-25 09:07:37 +0100303 free(label);
304}
305
Simon Glass00302442021-10-14 12:48:01 -0600306/**
307 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100308 *
309 * This is passed as a callback to the menu code for displaying each
310 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600311 *
312 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-11-25 09:07:37 +0100313 */
314static void label_print(void *data)
315{
316 struct pxe_label *label = data;
317 const char *c = label->menu ? label->menu : label->name;
318
319 printf("%s:\t%s\n", label->num, c);
320}
321
Simon Glass00302442021-10-14 12:48:01 -0600322/**
323 * label_localboot() - Boot a label that specified 'localboot'
324 *
325 * This requires that the 'localcmd' environment variable is defined. Its
326 * contents will be executed as U-Boot commands. If the label specified an
327 * 'append' line, its contents will be used to overwrite the contents of the
328 * 'bootargs' environment variable prior to running 'localcmd'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100329 *
Simon Glass00302442021-10-14 12:48:01 -0600330 * @label: Label to process
331 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100332 */
333static int label_localboot(struct pxe_label *label)
334{
335 char *localcmd;
336
337 localcmd = from_env("localcmd");
Patrice Chotard17e88042019-11-25 09:07:37 +0100338 if (!localcmd)
339 return -ENOENT;
340
341 if (label->append) {
342 char bootargs[CONFIG_SYS_CBSIZE];
343
Simon Glassc7b03e82020-11-05 10:33:47 -0700344 cli_simple_process_macros(label->append, bootargs,
345 sizeof(bootargs));
Patrice Chotard17e88042019-11-25 09:07:37 +0100346 env_set("bootargs", bootargs);
347 }
348
349 debug("running: %s\n", localcmd);
350
351 return run_command_list(localcmd, strlen(localcmd), 0);
352}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100353
Simon Glass00302442021-10-14 12:48:01 -0600354/**
355 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
356 *
357 * @ctx: PXE context
358 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100359 */
360#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600361static void label_boot_fdtoverlay(struct pxe_context *ctx,
362 struct pxe_label *label)
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100363{
364 char *fdtoverlay = label->fdtoverlays;
365 struct fdt_header *working_fdt;
366 char *fdtoverlay_addr_env;
367 ulong fdtoverlay_addr;
368 ulong fdt_addr;
369 int err;
370
371 /* Get the main fdt and map it */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600372 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100373 working_fdt = map_sysmem(fdt_addr, 0);
374 err = fdt_check_header(working_fdt);
375 if (err)
376 return;
377
378 /* Get the specific overlay loading address */
379 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
380 if (!fdtoverlay_addr_env) {
381 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
382 return;
383 }
384
Simon Glass3ff49ec2021-07-24 09:03:29 -0600385 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100386
387 /* Cycle over the overlay files and apply them in order */
388 do {
389 struct fdt_header *blob;
390 char *overlayfile;
391 char *end;
392 int len;
393
394 /* Drop leading spaces */
395 while (*fdtoverlay == ' ')
396 ++fdtoverlay;
397
398 /* Copy a single filename if multiple provided */
399 end = strstr(fdtoverlay, " ");
400 if (end) {
401 len = (int)(end - fdtoverlay);
402 overlayfile = malloc(len + 1);
403 strncpy(overlayfile, fdtoverlay, len);
404 overlayfile[len] = '\0';
405 } else
406 overlayfile = fdtoverlay;
407
408 if (!strlen(overlayfile))
409 goto skip_overlay;
410
411 /* Load overlay file */
Simon Glassb0d08db2021-10-14 12:47:56 -0600412 err = get_relfile_envaddr(ctx, overlayfile,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100413 "fdtoverlay_addr_r");
414 if (err < 0) {
415 printf("Failed loading overlay %s\n", overlayfile);
416 goto skip_overlay;
417 }
418
419 /* Resize main fdt */
420 fdt_shrink_to_minimum(working_fdt, 8192);
421
422 blob = map_sysmem(fdtoverlay_addr, 0);
423 err = fdt_check_header(blob);
424 if (err) {
425 printf("Invalid overlay %s, skipping\n",
426 overlayfile);
427 goto skip_overlay;
428 }
429
430 err = fdt_overlay_apply_verbose(working_fdt, blob);
431 if (err) {
432 printf("Failed to apply overlay %s, skipping\n",
433 overlayfile);
434 goto skip_overlay;
435 }
436
437skip_overlay:
438 if (end)
439 free(overlayfile);
440 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
441}
442#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100443
Simon Glass00302442021-10-14 12:48:01 -0600444/**
445 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard17e88042019-11-25 09:07:37 +0100446 *
447 * If we can't boot for any reason, we return. A successful boot never
448 * returns.
449 *
450 * The kernel will be stored in the location given by the 'kernel_addr_r'
451 * environment variable.
452 *
453 * If the label specifies an initrd file, it will be stored in the location
454 * given by the 'ramdisk_addr_r' environment variable.
455 *
456 * If the label specifies an 'append' line, its contents will overwrite that
457 * of the 'bootargs' environment variable.
Simon Glass00302442021-10-14 12:48:01 -0600458 *
459 * @ctx: PXE context
460 * @label: Label to process
461 * Returns does not return on success, otherwise returns 0 if a localboot
462 * label was processed, or 1 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100463 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600464static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +0100465{
466 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700467 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Libc14b932021-10-20 00:18:15 -0700468 char *kernel_addr = NULL;
469 char *initrd_addr_str = NULL;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700470 char initrd_filesize[10];
Zhaofeng Libc14b932021-10-20 00:18:15 -0700471 char initrd_str[28];
Patrice Chotard17e88042019-11-25 09:07:37 +0100472 char mac_str[29] = "";
473 char ip_str[68] = "";
474 char *fit_addr = NULL;
475 int bootm_argc = 2;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700476 int zboot_argc = 3;
Patrice Chotard17e88042019-11-25 09:07:37 +0100477 int len = 0;
Zhaofeng Libc14b932021-10-20 00:18:15 -0700478 ulong kernel_addr_r;
Patrice Chotard17e88042019-11-25 09:07:37 +0100479 void *buf;
480
481 label_print(label);
482
483 label->attempted = 1;
484
485 if (label->localboot) {
486 if (label->localboot_val >= 0)
487 label_localboot(label);
488 return 0;
489 }
490
Patrice Chotard6233de42019-11-25 09:07:39 +0100491 if (!label->kernel) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100492 printf("No kernel given, skipping %s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100493 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100494 return 1;
495 }
496
497 if (label->initrd) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600498 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100499 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100500 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100501 return 1;
502 }
503
Zhaofeng Libc14b932021-10-20 00:18:15 -0700504 initrd_addr_str = env_get("ramdisk_addr_r");
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700505 strncpy(initrd_filesize, env_get("filesize"), 9);
Zhaofeng Libc14b932021-10-20 00:18:15 -0700506
507 strncpy(initrd_str, initrd_addr_str, 18);
508 strcat(initrd_str, ":");
509 strncat(initrd_str, initrd_filesize, 9);
Patrice Chotard17e88042019-11-25 09:07:37 +0100510 }
511
Simon Glassb0d08db2021-10-14 12:47:56 -0600512 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100513 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100514 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100515 return 1;
516 }
517
518 if (label->ipappend & 0x1) {
519 sprintf(ip_str, " ip=%s:%s:%s:%s",
520 env_get("ipaddr"), env_get("serverip"),
521 env_get("gatewayip"), env_get("netmask"));
522 }
523
Kory Maincentcaabd242021-02-02 16:42:28 +0100524 if (IS_ENABLED(CONFIG_CMD_NET)) {
525 if (label->ipappend & 0x2) {
526 int err;
Patrice Chotard6233de42019-11-25 09:07:39 +0100527
Kory Maincentcaabd242021-02-02 16:42:28 +0100528 strcpy(mac_str, " BOOTIF=");
529 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
530 if (err < 0)
531 mac_str[0] = '\0';
532 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100533 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100534
535 if ((label->ipappend & 0x3) || label->append) {
536 char bootargs[CONFIG_SYS_CBSIZE] = "";
537 char finalbootargs[CONFIG_SYS_CBSIZE];
538
539 if (strlen(label->append ?: "") +
540 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
541 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
542 strlen(label->append ?: ""),
543 strlen(ip_str), strlen(mac_str),
544 sizeof(bootargs));
545 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100546 }
Patrice Chotard6233de42019-11-25 09:07:39 +0100547
548 if (label->append)
549 strncpy(bootargs, label->append, sizeof(bootargs));
550
551 strcat(bootargs, ip_str);
552 strcat(bootargs, mac_str);
553
Simon Glassc7b03e82020-11-05 10:33:47 -0700554 cli_simple_process_macros(bootargs, finalbootargs,
555 sizeof(finalbootargs));
Patrice Chotard6233de42019-11-25 09:07:39 +0100556 env_set("bootargs", finalbootargs);
557 printf("append: %s\n", finalbootargs);
Patrice Chotard17e88042019-11-25 09:07:37 +0100558 }
559
Zhaofeng Libc14b932021-10-20 00:18:15 -0700560 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700561
Patrice Chotard17e88042019-11-25 09:07:37 +0100562 /* for FIT, append the configuration identifier */
563 if (label->config) {
Zhaofeng Libc14b932021-10-20 00:18:15 -0700564 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100565
566 fit_addr = malloc(len);
567 if (!fit_addr) {
568 printf("malloc fail (FIT address)\n");
569 return 1;
570 }
Zhaofeng Libc14b932021-10-20 00:18:15 -0700571 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
572 kernel_addr = fit_addr;
Patrice Chotard17e88042019-11-25 09:07:37 +0100573 }
574
575 /*
576 * fdt usage is optional:
Anton Leontievfeb7db82019-09-03 10:52:24 +0300577 * It handles the following scenarios.
Patrice Chotard17e88042019-11-25 09:07:37 +0100578 *
Anton Leontievfeb7db82019-09-03 10:52:24 +0300579 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
580 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
581 * bootm, and adjust argc appropriately.
582 *
583 * If retrieve fails and no exact fdt blob is specified in pxe file with
584 * "fdt" label, try Scenario 2.
Patrice Chotard17e88042019-11-25 09:07:37 +0100585 *
586 * Scenario 2: If there is an fdt_addr specified, pass it along to
587 * bootm, and adjust argc appropriately.
588 *
589 * Scenario 3: fdt blob is not available.
590 */
591 bootm_argv[3] = env_get("fdt_addr_r");
592
593 /* if fdt label is defined then get fdt from server */
594 if (bootm_argv[3]) {
595 char *fdtfile = NULL;
596 char *fdtfilefree = NULL;
597
598 if (label->fdt) {
599 fdtfile = label->fdt;
600 } else if (label->fdtdir) {
601 char *f1, *f2, *f3, *f4, *slash;
602
603 f1 = env_get("fdtfile");
604 if (f1) {
605 f2 = "";
606 f3 = "";
607 f4 = "";
608 } else {
609 /*
610 * For complex cases where this code doesn't
611 * generate the correct filename, the board
612 * code should set $fdtfile during early boot,
613 * or the boot scripts should set $fdtfile
614 * before invoking "pxe" or "sysboot".
615 */
616 f1 = env_get("soc");
617 f2 = "-";
618 f3 = env_get("board");
619 f4 = ".dtb";
Dimitri John Ledkov5ee984a2021-04-21 12:42:01 +0100620 if (!f1) {
621 f1 = "";
622 f2 = "";
623 }
624 if (!f3) {
625 f2 = "";
626 f3 = "";
627 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100628 }
629
630 len = strlen(label->fdtdir);
631 if (!len)
632 slash = "./";
633 else if (label->fdtdir[len - 1] != '/')
634 slash = "/";
635 else
636 slash = "";
637
638 len = strlen(label->fdtdir) + strlen(slash) +
639 strlen(f1) + strlen(f2) + strlen(f3) +
640 strlen(f4) + 1;
641 fdtfilefree = malloc(len);
642 if (!fdtfilefree) {
643 printf("malloc fail (FDT filename)\n");
644 goto cleanup;
645 }
646
647 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
648 label->fdtdir, slash, f1, f2, f3, f4);
649 fdtfile = fdtfilefree;
650 }
651
652 if (fdtfile) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600653 int err = get_relfile_envaddr(ctx, fdtfile,
Patrice Chotard6233de42019-11-25 09:07:39 +0100654 "fdt_addr_r");
655
Patrice Chotard17e88042019-11-25 09:07:37 +0100656 free(fdtfilefree);
657 if (err < 0) {
Anton Leontievfeb7db82019-09-03 10:52:24 +0300658 bootm_argv[3] = NULL;
659
660 if (label->fdt) {
661 printf("Skipping %s for failure retrieving FDT\n",
662 label->name);
663 goto cleanup;
664 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100665 }
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100666
667#ifdef CONFIG_OF_LIBFDT_OVERLAY
668 if (label->fdtoverlays)
Simon Glassb0d08db2021-10-14 12:47:56 -0600669 label_boot_fdtoverlay(ctx, label);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100670#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100671 } else {
672 bootm_argv[3] = NULL;
673 }
674 }
675
Zhaofeng Libc14b932021-10-20 00:18:15 -0700676 bootm_argv[1] = kernel_addr;
677 zboot_argv[1] = kernel_addr;
678
679 if (initrd_addr_str) {
680 bootm_argv[2] = initrd_str;
681 bootm_argc = 3;
682
683 zboot_argv[3] = initrd_addr_str;
684 zboot_argv[4] = initrd_filesize;
685 zboot_argc = 5;
686 }
687
Patrice Chotard17e88042019-11-25 09:07:37 +0100688 if (!bootm_argv[3])
689 bootm_argv[3] = env_get("fdt_addr");
690
691 if (bootm_argv[3]) {
692 if (!bootm_argv[2])
693 bootm_argv[2] = "-";
694 bootm_argc = 4;
695 }
696
Zhaofeng Libc14b932021-10-20 00:18:15 -0700697 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
698 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard17e88042019-11-25 09:07:37 +0100699 /* Try bootm for legacy and FIT format image */
700 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
Simon Glassb0d08db2021-10-14 12:47:56 -0600701 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100702 /* Try booting an AArch64 Linux kernel image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100703 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassb0d08db2021-10-14 12:47:56 -0600704 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100705 /* Try booting a Image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100706 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassb0d08db2021-10-14 12:47:56 -0600707 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincentb2187d02021-02-02 16:42:29 +0100708 /* Try booting an x86_64 Linux kernel image */
709 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassb0d08db2021-10-14 12:47:56 -0600710 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentcaabd242021-02-02 16:42:28 +0100711
Patrice Chotard17e88042019-11-25 09:07:37 +0100712 unmap_sysmem(buf);
713
714cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600715 free(fit_addr);
716
Patrice Chotard17e88042019-11-25 09:07:37 +0100717 return 1;
718}
719
Simon Glass00302442021-10-14 12:48:01 -0600720/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100721enum token_type {
722 T_EOL,
723 T_STRING,
724 T_EOF,
725 T_MENU,
726 T_TITLE,
727 T_TIMEOUT,
728 T_LABEL,
729 T_KERNEL,
730 T_LINUX,
731 T_APPEND,
732 T_INITRD,
733 T_LOCALBOOT,
734 T_DEFAULT,
735 T_PROMPT,
736 T_INCLUDE,
737 T_FDT,
738 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100739 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100740 T_ONTIMEOUT,
741 T_IPAPPEND,
742 T_BACKGROUND,
743 T_INVALID
744};
745
Simon Glass00302442021-10-14 12:48:01 -0600746/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100747struct token {
748 char *val;
749 enum token_type type;
750};
751
Simon Glass00302442021-10-14 12:48:01 -0600752/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100753static const struct token keywords[] = {
754 {"menu", T_MENU},
755 {"title", T_TITLE},
756 {"timeout", T_TIMEOUT},
757 {"default", T_DEFAULT},
758 {"prompt", T_PROMPT},
759 {"label", T_LABEL},
760 {"kernel", T_KERNEL},
761 {"linux", T_LINUX},
762 {"localboot", T_LOCALBOOT},
763 {"append", T_APPEND},
764 {"initrd", T_INITRD},
765 {"include", T_INCLUDE},
766 {"devicetree", T_FDT},
767 {"fdt", T_FDT},
768 {"devicetreedir", T_FDTDIR},
769 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100770 {"fdtoverlays", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100771 {"ontimeout", T_ONTIMEOUT,},
772 {"ipappend", T_IPAPPEND,},
773 {"background", T_BACKGROUND,},
774 {NULL, T_INVALID}
775};
776
Simon Glass00302442021-10-14 12:48:01 -0600777/**
778 * enum lex_state - lexer state
779 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100780 * Since pxe(linux) files don't have a token to identify the start of a
781 * literal, we have to keep track of when we're in a state where a literal is
782 * expected vs when we're in a state a keyword is expected.
783 */
784enum lex_state {
785 L_NORMAL = 0,
786 L_KEYWORD,
787 L_SLITERAL
788};
789
Simon Glass00302442021-10-14 12:48:01 -0600790/**
791 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100792 *
Simon Glass00302442021-10-14 12:48:01 -0600793 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100794 *
795 * Characters from *p are copied into t-val until a character equal to
796 * delim is found, or a NUL byte is reached. If delim has the special value of
797 * ' ', any whitespace character will be used as a delimiter.
798 *
799 * If lower is unequal to 0, uppercase characters will be converted to
800 * lowercase in the result. This is useful to make keywords case
801 * insensitive.
802 *
803 * The location of *p is updated to point to the first character after the end
804 * of the token - the ending delimiter.
805 *
Simon Glass00302442021-10-14 12:48:01 -0600806 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
807 * it.
808 *
809 * @p: Points to a pointer to the current position in the input being processed.
810 * Updated to point at the first character after the current token
811 * @t: Pointers to a token to fill in
812 * @delim: Delimiter character to look for, either newline or space
813 * @lower: true to convert the string to lower case when storing
814 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100815 */
816static char *get_string(char **p, struct token *t, char delim, int lower)
817{
818 char *b, *e;
819 size_t len, i;
820
821 /*
822 * b and e both start at the beginning of the input stream.
823 *
824 * e is incremented until we find the ending delimiter, or a NUL byte
825 * is reached. Then, we take e - b to find the length of the token.
826 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100827 b = *p;
828 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100829 while (*e) {
830 if ((delim == ' ' && isspace(*e)) || delim == *e)
831 break;
832 e++;
833 }
834
835 len = e - b;
836
837 /*
838 * Allocate memory to hold the string, and copy it in, converting
839 * characters to lowercase if lower is != 0.
840 */
841 t->val = malloc(len + 1);
842 if (!t->val)
843 return NULL;
844
845 for (i = 0; i < len; i++, b++) {
846 if (lower)
847 t->val[i] = tolower(*b);
848 else
849 t->val[i] = *b;
850 }
851
852 t->val[len] = '\0';
853
Simon Glass764d0c02021-10-14 12:48:02 -0600854 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100855 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100856 t->type = T_STRING;
857
858 return t->val;
859}
860
Simon Glass00302442021-10-14 12:48:01 -0600861/**
862 * get_keyword() - Populate a keyword token with a type and value
863 *
864 * Updates the ->type field based on the keyword string in @val
865 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100866 */
867static void get_keyword(struct token *t)
868{
869 int i;
870
871 for (i = 0; keywords[i].val; i++) {
872 if (!strcmp(t->val, keywords[i].val)) {
873 t->type = keywords[i].type;
874 break;
875 }
876 }
877}
878
Simon Glass00302442021-10-14 12:48:01 -0600879/**
880 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100881 *
Simon Glass00302442021-10-14 12:48:01 -0600882 * We have to keep track of which state we're in to know if we're looking to get
883 * a string literal or a keyword.
884 *
885 * @p: Points to a pointer to the current position in the input being processed.
886 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100887 */
888static void get_token(char **p, struct token *t, enum lex_state state)
889{
890 char *c = *p;
891
892 t->type = T_INVALID;
893
894 /* eat non EOL whitespace */
895 while (isblank(*c))
896 c++;
897
898 /*
899 * eat comments. note that string literals can't begin with #, but
900 * can contain a # after their first character.
901 */
902 if (*c == '#') {
903 while (*c && *c != '\n')
904 c++;
905 }
906
907 if (*c == '\n') {
908 t->type = T_EOL;
909 c++;
910 } else if (*c == '\0') {
911 t->type = T_EOF;
912 c++;
913 } else if (state == L_SLITERAL) {
914 get_string(&c, t, '\n', 0);
915 } else if (state == L_KEYWORD) {
916 /*
917 * when we expect a keyword, we first get the next string
918 * token delimited by whitespace, and then check if it
919 * matches a keyword in our keyword list. if it does, it's
920 * converted to a keyword token of the appropriate type, and
921 * if not, it remains a string token.
922 */
923 get_string(&c, t, ' ', 1);
924 get_keyword(t);
925 }
926
927 *p = c;
928}
929
Simon Glass00302442021-10-14 12:48:01 -0600930/**
931 * eol_or_eof() - Find end of line
932 *
933 * Increment *c until we get to the end of the current line, or EOF
934 *
935 * @c: Points to a pointer to the current position in the input being processed.
936 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100937 */
938static void eol_or_eof(char **c)
939{
940 while (**c && **c != '\n')
941 (*c)++;
942}
943
944/*
945 * All of these parse_* functions share some common behavior.
946 *
947 * They finish with *c pointing after the token they parse, and return 1 on
948 * success, or < 0 on error.
949 */
950
951/*
952 * Parse a string literal and store a pointer it at *dst. String literals
953 * terminate at the end of the line.
954 */
955static int parse_sliteral(char **c, char **dst)
956{
957 struct token t;
958 char *s = *c;
959
960 get_token(c, &t, L_SLITERAL);
961
962 if (t.type != T_STRING) {
963 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
964 return -EINVAL;
965 }
966
967 *dst = t.val;
968
969 return 1;
970}
971
972/*
973 * Parse a base 10 (unsigned) integer and store it at *dst.
974 */
975static int parse_integer(char **c, int *dst)
976{
977 struct token t;
978 char *s = *c;
979
980 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +0100981 if (t.type != T_STRING) {
982 printf("Expected string: %.*s\n", (int)(*c - s), s);
983 return -EINVAL;
984 }
985
986 *dst = simple_strtol(t.val, NULL, 10);
987
988 free(t.val);
989
990 return 1;
991}
992
Simon Glassb0d08db2021-10-14 12:47:56 -0600993static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100994 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +0100995
996/*
997 * Parse an include statement, and retrieve and parse the file it mentions.
998 *
999 * base should point to a location where it's safe to store the file, and
1000 * nest_level should indicate how many nested includes have occurred. For this
1001 * include, nest_level has already been incremented and doesn't need to be
1002 * incremented here.
1003 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001004static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001005 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001006{
1007 char *include_path;
1008 char *s = *c;
1009 int err;
1010 char *buf;
1011 int ret;
1012
1013 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +01001014 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +01001015 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001016 return err;
1017 }
1018
Simon Glassb0d08db2021-10-14 12:47:56 -06001019 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +01001020 if (err < 0) {
1021 printf("Couldn't retrieve %s\n", include_path);
1022 return err;
1023 }
1024
1025 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001026 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001027 unmap_sysmem(buf);
1028
1029 return ret;
1030}
1031
1032/*
1033 * Parse lines that begin with 'menu'.
1034 *
1035 * base and nest are provided to handle the 'menu include' case.
1036 *
1037 * base should point to a location where it's safe to store the included file.
1038 *
1039 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1040 * a file it includes, 3 when parsing a file included by that file, and so on.
1041 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001042static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001043 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001044{
1045 struct token t;
1046 char *s = *c;
1047 int err = 0;
1048
1049 get_token(c, &t, L_KEYWORD);
1050
1051 switch (t.type) {
1052 case T_TITLE:
1053 err = parse_sliteral(c, &cfg->title);
1054
1055 break;
1056
1057 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001058 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001059 break;
1060
1061 case T_BACKGROUND:
1062 err = parse_sliteral(c, &cfg->bmp);
1063 break;
1064
1065 default:
1066 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001067 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001068 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001069 if (err < 0)
1070 return err;
1071
1072 eol_or_eof(c);
1073
1074 return 1;
1075}
1076
1077/*
1078 * Handles parsing a 'menu line' when we're parsing a label.
1079 */
1080static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001081 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001082{
1083 struct token t;
1084 char *s;
1085
1086 s = *c;
1087
1088 get_token(c, &t, L_KEYWORD);
1089
1090 switch (t.type) {
1091 case T_DEFAULT:
1092 if (!cfg->default_label)
1093 cfg->default_label = strdup(label->name);
1094
1095 if (!cfg->default_label)
1096 return -ENOMEM;
1097
1098 break;
1099 case T_LABEL:
1100 parse_sliteral(c, &label->menu);
1101 break;
1102 default:
1103 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001104 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001105 }
1106
1107 eol_or_eof(c);
1108
1109 return 0;
1110}
1111
1112/*
1113 * Handles parsing a 'kernel' label.
1114 * expecting "filename" or "<fit_filename>#cfg"
1115 */
1116static int parse_label_kernel(char **c, struct pxe_label *label)
1117{
1118 char *s;
1119 int err;
1120
1121 err = parse_sliteral(c, &label->kernel);
1122 if (err < 0)
1123 return err;
1124
1125 s = strstr(label->kernel, "#");
1126 if (!s)
1127 return 1;
1128
1129 label->config = malloc(strlen(s) + 1);
1130 if (!label->config)
1131 return -ENOMEM;
1132
1133 strcpy(label->config, s);
1134 *s = 0;
1135
1136 return 1;
1137}
1138
1139/*
1140 * Parses a label and adds it to the list of labels for a menu.
1141 *
1142 * A label ends when we either get to the end of a file, or
1143 * get some input we otherwise don't have a handler defined
1144 * for.
1145 *
1146 */
1147static int parse_label(char **c, struct pxe_menu *cfg)
1148{
1149 struct token t;
1150 int len;
1151 char *s = *c;
1152 struct pxe_label *label;
1153 int err;
1154
1155 label = label_create();
1156 if (!label)
1157 return -ENOMEM;
1158
1159 err = parse_sliteral(c, &label->name);
1160 if (err < 0) {
1161 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1162 label_destroy(label);
1163 return -EINVAL;
1164 }
1165
1166 list_add_tail(&label->list, &cfg->labels);
1167
1168 while (1) {
1169 s = *c;
1170 get_token(c, &t, L_KEYWORD);
1171
1172 err = 0;
1173 switch (t.type) {
1174 case T_MENU:
1175 err = parse_label_menu(c, cfg, label);
1176 break;
1177
1178 case T_KERNEL:
1179 case T_LINUX:
1180 err = parse_label_kernel(c, label);
1181 break;
1182
1183 case T_APPEND:
1184 err = parse_sliteral(c, &label->append);
1185 if (label->initrd)
1186 break;
1187 s = strstr(label->append, "initrd=");
1188 if (!s)
1189 break;
1190 s += 7;
1191 len = (int)(strchr(s, ' ') - s);
1192 label->initrd = malloc(len + 1);
1193 strncpy(label->initrd, s, len);
1194 label->initrd[len] = '\0';
1195
1196 break;
1197
1198 case T_INITRD:
1199 if (!label->initrd)
1200 err = parse_sliteral(c, &label->initrd);
1201 break;
1202
1203 case T_FDT:
1204 if (!label->fdt)
1205 err = parse_sliteral(c, &label->fdt);
1206 break;
1207
1208 case T_FDTDIR:
1209 if (!label->fdtdir)
1210 err = parse_sliteral(c, &label->fdtdir);
1211 break;
1212
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001213 case T_FDTOVERLAYS:
1214 if (!label->fdtoverlays)
1215 err = parse_sliteral(c, &label->fdtoverlays);
1216 break;
1217
Patrice Chotard17e88042019-11-25 09:07:37 +01001218 case T_LOCALBOOT:
1219 label->localboot = 1;
1220 err = parse_integer(c, &label->localboot_val);
1221 break;
1222
1223 case T_IPAPPEND:
1224 err = parse_integer(c, &label->ipappend);
1225 break;
1226
1227 case T_EOL:
1228 break;
1229
1230 default:
1231 /*
1232 * put the token back! we don't want it - it's the end
1233 * of a label and whatever token this is, it's
1234 * something for the menu level context to handle.
1235 */
1236 *c = s;
1237 return 1;
1238 }
1239
1240 if (err < 0)
1241 return err;
1242 }
1243}
1244
1245/*
1246 * This 16 comes from the limit pxelinux imposes on nested includes.
1247 *
1248 * There is no reason at all we couldn't do more, but some limit helps prevent
1249 * infinite (until crash occurs) recursion if a file tries to include itself.
1250 */
1251#define MAX_NEST_LEVEL 16
1252
1253/*
1254 * Entry point for parsing a menu file. nest_level indicates how many times
1255 * we've nested in includes. It will be 1 for the top level menu file.
1256 *
1257 * Returns 1 on success, < 0 on error.
1258 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001259static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001260 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001261{
1262 struct token t;
1263 char *s, *b, *label_name;
1264 int err;
1265
1266 b = p;
1267
1268 if (nest_level > MAX_NEST_LEVEL) {
1269 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1270 return -EMLINK;
1271 }
1272
1273 while (1) {
1274 s = p;
1275
1276 get_token(&p, &t, L_KEYWORD);
1277
1278 err = 0;
1279 switch (t.type) {
1280 case T_MENU:
1281 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001282 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001283 base + ALIGN(strlen(b) + 1, 4),
1284 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001285 break;
1286
1287 case T_TIMEOUT:
1288 err = parse_integer(&p, &cfg->timeout);
1289 break;
1290
1291 case T_LABEL:
1292 err = parse_label(&p, cfg);
1293 break;
1294
1295 case T_DEFAULT:
1296 case T_ONTIMEOUT:
1297 err = parse_sliteral(&p, &label_name);
1298
1299 if (label_name) {
1300 if (cfg->default_label)
1301 free(cfg->default_label);
1302
1303 cfg->default_label = label_name;
1304 }
1305
1306 break;
1307
1308 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001309 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001310 base + ALIGN(strlen(b), 4), cfg,
1311 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001312 break;
1313
1314 case T_PROMPT:
1315 eol_or_eof(&p);
1316 break;
1317
1318 case T_EOL:
1319 break;
1320
1321 case T_EOF:
1322 return 1;
1323
1324 default:
1325 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001326 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001327 eol_or_eof(&p);
1328 }
1329
1330 if (err < 0)
1331 return err;
1332 }
1333}
1334
1335/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001336 */
1337void destroy_pxe_menu(struct pxe_menu *cfg)
1338{
1339 struct list_head *pos, *n;
1340 struct pxe_label *label;
1341
Simon Glass764d0c02021-10-14 12:48:02 -06001342 free(cfg->title);
1343 free(cfg->default_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001344
1345 list_for_each_safe(pos, n, &cfg->labels) {
1346 label = list_entry(pos, struct pxe_label, list);
1347
1348 label_destroy(label);
1349 }
1350
1351 free(cfg);
1352}
1353
Simon Glassb0d08db2021-10-14 12:47:56 -06001354struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001355{
1356 struct pxe_menu *cfg;
1357 char *buf;
1358 int r;
1359
1360 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001361 if (!cfg)
1362 return NULL;
1363
1364 memset(cfg, 0, sizeof(struct pxe_menu));
1365
1366 INIT_LIST_HEAD(&cfg->labels);
1367
1368 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001369 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001370 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001371 if (r < 0) {
1372 destroy_pxe_menu(cfg);
1373 return NULL;
1374 }
1375
1376 return cfg;
1377}
1378
1379/*
1380 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1381 * menu code.
1382 */
1383static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1384{
1385 struct pxe_label *label;
1386 struct list_head *pos;
1387 struct menu *m;
1388 int err;
1389 int i = 1;
1390 char *default_num = NULL;
1391
1392 /*
1393 * Create a menu and add items for all the labels.
1394 */
1395 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -07001396 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001397 if (!m)
1398 return NULL;
1399
1400 list_for_each(pos, &cfg->labels) {
1401 label = list_entry(pos, struct pxe_label, list);
1402
1403 sprintf(label->num, "%d", i++);
1404 if (menu_item_add(m, label->num, label) != 1) {
1405 menu_destroy(m);
1406 return NULL;
1407 }
1408 if (cfg->default_label &&
1409 (strcmp(label->name, cfg->default_label) == 0))
1410 default_num = label->num;
Patrice Chotard17e88042019-11-25 09:07:37 +01001411 }
1412
1413 /*
1414 * After we've created items for each label in the menu, set the
1415 * menu's default label if one was specified.
1416 */
1417 if (default_num) {
1418 err = menu_default_set(m, default_num);
1419 if (err != 1) {
1420 if (err != -ENOENT) {
1421 menu_destroy(m);
1422 return NULL;
1423 }
1424
1425 printf("Missing default: %s\n", cfg->default_label);
1426 }
1427 }
1428
1429 return m;
1430}
1431
1432/*
1433 * Try to boot any labels we have yet to attempt to boot.
1434 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001435static void boot_unattempted_labels(struct pxe_context *ctx,
1436 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001437{
1438 struct list_head *pos;
1439 struct pxe_label *label;
1440
1441 list_for_each(pos, &cfg->labels) {
1442 label = list_entry(pos, struct pxe_label, list);
1443
1444 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001445 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001446 }
1447}
1448
Simon Glassb0d08db2021-10-14 12:47:56 -06001449void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001450{
1451 void *choice;
1452 struct menu *m;
1453 int err;
1454
Kory Maincentcaabd242021-02-02 16:42:28 +01001455 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1456 /* display BMP if available */
1457 if (cfg->bmp) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001458 if (get_relfile(ctx, cfg->bmp, image_load_addr)) {
Kory Maincentcaabd242021-02-02 16:42:28 +01001459 if (CONFIG_IS_ENABLED(CMD_CLS))
1460 run_command("cls", 0);
1461 bmp_display(image_load_addr,
1462 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1463 } else {
1464 printf("Skipping background bmp %s for failure\n",
1465 cfg->bmp);
1466 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001467 }
1468 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001469
1470 m = pxe_menu_to_menu(cfg);
1471 if (!m)
1472 return;
1473
1474 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001475 menu_destroy(m);
1476
1477 /*
1478 * err == 1 means we got a choice back from menu_get_choice.
1479 *
1480 * err == -ENOENT if the menu was setup to select the default but no
1481 * default was set. in that case, we should continue trying to boot
1482 * labels that haven't been attempted yet.
1483 *
1484 * otherwise, the user interrupted or there was some other error and
1485 * we give up.
1486 */
1487
1488 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001489 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001490 if (!err)
1491 return;
1492 } else if (err != -ENOENT) {
1493 return;
1494 }
1495
Simon Glassb0d08db2021-10-14 12:47:56 -06001496 boot_unattempted_labels(ctx, cfg);
1497}
1498
Simon Glass44a20ef2021-10-14 12:47:57 -06001499void pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
Simon Glass3ae416a2021-10-14 12:47:59 -06001500 pxe_getfile_func getfile, void *userdata,
1501 bool allow_abs_path)
Simon Glassb0d08db2021-10-14 12:47:56 -06001502{
1503 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001504 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001505 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001506 ctx->allow_abs_path = allow_abs_path;
Patrice Chotard17e88042019-11-25 09:07:37 +01001507}