blob: 48fb7076fd1be762a191c7dcc005478cfc462059 [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/**
Simon Glass00302442021-10-14 12:48:01 -060068 * get_relfile() - read a file relative to the PXE file
69 *
Patrice Chotard17e88042019-11-25 09:07:37 +010070 * As in pxelinux, paths to files referenced from files we retrieve are
71 * relative to the location of bootfile. get_relfile takes such a path and
72 * joins it with the bootfile path to get the full path to the target file. If
73 * the bootfile path is NULL, we use file_path as is.
74 *
Simon Glass00302442021-10-14 12:48:01 -060075 * @ctx: PXE context
76 * @file_path: File path to read (relative to the PXE file)
77 * @file_addr: Address to load file to
78 * Returns 1 for success, or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +010079 */
Simon Glassb0d08db2021-10-14 12:47:56 -060080static int get_relfile(struct pxe_context *ctx, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +010081 unsigned long file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +010082{
83 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +010084 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +010085 char addr_buf[18];
Patrice Chotard17e88042019-11-25 09:07:37 +010086
Simon Glass5e3e39a2021-10-14 12:48:05 -060087 if (file_path[0] == '/' && ctx->allow_abs_path)
88 *relfile = '\0';
89 else
90 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
Patrice Chotard17e88042019-11-25 09:07:37 +010091
Simon Glass5e3e39a2021-10-14 12:48:05 -060092 path_len = strlen(file_path) + strlen(relfile);
Patrice Chotard17e88042019-11-25 09:07:37 +010093
94 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +010095 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +010096
97 return -ENAMETOOLONG;
98 }
99
100 strcat(relfile, file_path);
101
102 printf("Retrieving file: %s\n", relfile);
103
104 sprintf(addr_buf, "%lx", file_addr);
105
Simon Glass44a20ef2021-10-14 12:47:57 -0600106 return ctx->getfile(ctx, relfile, addr_buf);
Patrice Chotard17e88042019-11-25 09:07:37 +0100107}
108
Simon Glass00302442021-10-14 12:48:01 -0600109/**
110 * get_pxe_file() - read a file
111 *
112 * The file is read and nul-terminated
113 *
114 * @ctx: PXE context
115 * @file_path: File path to read (relative to the PXE file)
116 * @file_addr: Address to load file to
117 * Returns 1 for success, or < 0 on error
118 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600119int get_pxe_file(struct pxe_context *ctx, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100120 unsigned long file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100121{
122 unsigned long config_file_size;
123 char *tftp_filesize;
124 int err;
125 char *buf;
126
Simon Glassb0d08db2021-10-14 12:47:56 -0600127 err = get_relfile(ctx, file_path, file_addr);
Patrice Chotard17e88042019-11-25 09:07:37 +0100128 if (err < 0)
129 return err;
130
131 /*
132 * the file comes without a NUL byte at the end, so find out its size
133 * and add the NUL byte.
134 */
135 tftp_filesize = from_env("filesize");
Patrice Chotard17e88042019-11-25 09:07:37 +0100136 if (!tftp_filesize)
137 return -ENOENT;
138
139 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
140 return -EINVAL;
141
142 buf = map_sysmem(file_addr + config_file_size, 1);
143 *buf = '\0';
144 unmap_sysmem(buf);
145
146 return 1;
147}
148
149#define PXELINUX_DIR "pxelinux.cfg/"
150
Simon Glass00302442021-10-14 12:48:01 -0600151/**
152 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
153 *
154 * @ctx: PXE context
155 * @file: Filename to process (relative to pxelinux.cfg/)
156 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
157 * or other value < 0 on other error
158 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600159int get_pxelinux_path(struct pxe_context *ctx, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100160 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100161{
162 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100163 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100164
165 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
166 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100167 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100168 return -ENAMETOOLONG;
169 }
170
171 sprintf(path, PXELINUX_DIR "%s", file);
172
Simon Glassb0d08db2021-10-14 12:47:56 -0600173 return get_pxe_file(ctx, path, pxefile_addr_r);
Patrice Chotard17e88042019-11-25 09:07:37 +0100174}
175
Simon Glass00302442021-10-14 12:48:01 -0600176/**
177 * get_relfile_envaddr() - read a file to an address in an env var
178 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100179 * Wrapper to make it easier to store the file at file_path in the location
180 * specified by envaddr_name. file_path will be joined to the bootfile path,
181 * if any is specified.
182 *
Simon Glass00302442021-10-14 12:48:01 -0600183 * @ctx: PXE context
184 * @file_path: File path to read (relative to the PXE file)
185 * @envaddr_name: Name of environment variable which contains the address to
186 * load to
187 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
188 * environment variable, -EINVAL if its format is not valid hex, or other
189 * value < 0 on other error
Patrice Chotard17e88042019-11-25 09:07:37 +0100190 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600191static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100192 const char *envaddr_name)
Patrice Chotard17e88042019-11-25 09:07:37 +0100193{
194 unsigned long file_addr;
195 char *envaddr;
196
197 envaddr = from_env(envaddr_name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100198 if (!envaddr)
199 return -ENOENT;
200
201 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
202 return -EINVAL;
203
Simon Glassb0d08db2021-10-14 12:47:56 -0600204 return get_relfile(ctx, file_path, file_addr);
Patrice Chotard17e88042019-11-25 09:07:37 +0100205}
206
Simon Glass00302442021-10-14 12:48:01 -0600207/**
208 * label_create() - crate a new PXE label
209 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100210 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
211 * result must be free()'d to reclaim the memory.
212 *
Simon Glass00302442021-10-14 12:48:01 -0600213 * Returns a pointer to the label, or NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100214 */
215static struct pxe_label *label_create(void)
216{
217 struct pxe_label *label;
218
219 label = malloc(sizeof(struct pxe_label));
Patrice Chotard17e88042019-11-25 09:07:37 +0100220 if (!label)
221 return NULL;
222
223 memset(label, 0, sizeof(struct pxe_label));
224
225 return label;
226}
227
Simon Glass00302442021-10-14 12:48:01 -0600228/**
229 * label_destroy() - free the memory used by a pxe_label
230 *
231 * This frees @label itself as well as memory used by its name,
232 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
233 * they're non-NULL.
Patrice Chotard17e88042019-11-25 09:07:37 +0100234 *
235 * So - be sure to only use dynamically allocated memory for the members of
236 * the pxe_label struct, unless you want to clean it up first. These are
237 * currently only created by the pxe file parsing code.
Simon Glass00302442021-10-14 12:48:01 -0600238 *
239 * @label: Label to free
Patrice Chotard17e88042019-11-25 09:07:37 +0100240 */
241static void label_destroy(struct pxe_label *label)
242{
Simon Glass764d0c02021-10-14 12:48:02 -0600243 free(label->name);
244 free(label->kernel);
245 free(label->config);
246 free(label->append);
247 free(label->initrd);
248 free(label->fdt);
249 free(label->fdtdir);
250 free(label->fdtoverlays);
Patrice Chotard17e88042019-11-25 09:07:37 +0100251 free(label);
252}
253
Simon Glass00302442021-10-14 12:48:01 -0600254/**
255 * label_print() - Print a label and its string members if they're defined
Patrice Chotard17e88042019-11-25 09:07:37 +0100256 *
257 * This is passed as a callback to the menu code for displaying each
258 * menu entry.
Simon Glass00302442021-10-14 12:48:01 -0600259 *
260 * @data: Label to print (is cast to struct pxe_label *)
Patrice Chotard17e88042019-11-25 09:07:37 +0100261 */
262static void label_print(void *data)
263{
264 struct pxe_label *label = data;
265 const char *c = label->menu ? label->menu : label->name;
266
267 printf("%s:\t%s\n", label->num, c);
268}
269
Simon Glass00302442021-10-14 12:48:01 -0600270/**
271 * label_localboot() - Boot a label that specified 'localboot'
272 *
273 * This requires that the 'localcmd' environment variable is defined. Its
274 * contents will be executed as U-Boot commands. If the label specified an
275 * 'append' line, its contents will be used to overwrite the contents of the
276 * 'bootargs' environment variable prior to running 'localcmd'.
Patrice Chotard17e88042019-11-25 09:07:37 +0100277 *
Simon Glass00302442021-10-14 12:48:01 -0600278 * @label: Label to process
279 * Returns 1 on success or < 0 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100280 */
281static int label_localboot(struct pxe_label *label)
282{
283 char *localcmd;
284
285 localcmd = from_env("localcmd");
Patrice Chotard17e88042019-11-25 09:07:37 +0100286 if (!localcmd)
287 return -ENOENT;
288
289 if (label->append) {
290 char bootargs[CONFIG_SYS_CBSIZE];
291
Simon Glassc7b03e82020-11-05 10:33:47 -0700292 cli_simple_process_macros(label->append, bootargs,
293 sizeof(bootargs));
Patrice Chotard17e88042019-11-25 09:07:37 +0100294 env_set("bootargs", bootargs);
295 }
296
297 debug("running: %s\n", localcmd);
298
299 return run_command_list(localcmd, strlen(localcmd), 0);
300}
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100301
Simon Glass00302442021-10-14 12:48:01 -0600302/**
303 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
304 *
305 * @ctx: PXE context
306 * @label: Label to process
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100307 */
308#ifdef CONFIG_OF_LIBFDT_OVERLAY
Simon Glassb0d08db2021-10-14 12:47:56 -0600309static void label_boot_fdtoverlay(struct pxe_context *ctx,
310 struct pxe_label *label)
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100311{
312 char *fdtoverlay = label->fdtoverlays;
313 struct fdt_header *working_fdt;
314 char *fdtoverlay_addr_env;
315 ulong fdtoverlay_addr;
316 ulong fdt_addr;
317 int err;
318
319 /* Get the main fdt and map it */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600320 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100321 working_fdt = map_sysmem(fdt_addr, 0);
322 err = fdt_check_header(working_fdt);
323 if (err)
324 return;
325
326 /* Get the specific overlay loading address */
327 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
328 if (!fdtoverlay_addr_env) {
329 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
330 return;
331 }
332
Simon Glass3ff49ec2021-07-24 09:03:29 -0600333 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100334
335 /* Cycle over the overlay files and apply them in order */
336 do {
337 struct fdt_header *blob;
338 char *overlayfile;
339 char *end;
340 int len;
341
342 /* Drop leading spaces */
343 while (*fdtoverlay == ' ')
344 ++fdtoverlay;
345
346 /* Copy a single filename if multiple provided */
347 end = strstr(fdtoverlay, " ");
348 if (end) {
349 len = (int)(end - fdtoverlay);
350 overlayfile = malloc(len + 1);
351 strncpy(overlayfile, fdtoverlay, len);
352 overlayfile[len] = '\0';
353 } else
354 overlayfile = fdtoverlay;
355
356 if (!strlen(overlayfile))
357 goto skip_overlay;
358
359 /* Load overlay file */
Simon Glassb0d08db2021-10-14 12:47:56 -0600360 err = get_relfile_envaddr(ctx, overlayfile,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100361 "fdtoverlay_addr_r");
362 if (err < 0) {
363 printf("Failed loading overlay %s\n", overlayfile);
364 goto skip_overlay;
365 }
366
367 /* Resize main fdt */
368 fdt_shrink_to_minimum(working_fdt, 8192);
369
370 blob = map_sysmem(fdtoverlay_addr, 0);
371 err = fdt_check_header(blob);
372 if (err) {
373 printf("Invalid overlay %s, skipping\n",
374 overlayfile);
375 goto skip_overlay;
376 }
377
378 err = fdt_overlay_apply_verbose(working_fdt, blob);
379 if (err) {
380 printf("Failed to apply overlay %s, skipping\n",
381 overlayfile);
382 goto skip_overlay;
383 }
384
385skip_overlay:
386 if (end)
387 free(overlayfile);
388 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
389}
390#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100391
Simon Glass00302442021-10-14 12:48:01 -0600392/**
393 * label_boot() - Boot according to the contents of a pxe_label
Patrice Chotard17e88042019-11-25 09:07:37 +0100394 *
395 * If we can't boot for any reason, we return. A successful boot never
396 * returns.
397 *
398 * The kernel will be stored in the location given by the 'kernel_addr_r'
399 * environment variable.
400 *
401 * If the label specifies an initrd file, it will be stored in the location
402 * given by the 'ramdisk_addr_r' environment variable.
403 *
404 * If the label specifies an 'append' line, its contents will overwrite that
405 * of the 'bootargs' environment variable.
Simon Glass00302442021-10-14 12:48:01 -0600406 *
407 * @ctx: PXE context
408 * @label: Label to process
409 * Returns does not return on success, otherwise returns 0 if a localboot
410 * label was processed, or 1 on error
Patrice Chotard17e88042019-11-25 09:07:37 +0100411 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600412static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +0100413{
414 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700415 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
Zhaofeng Libc14b932021-10-20 00:18:15 -0700416 char *kernel_addr = NULL;
417 char *initrd_addr_str = NULL;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700418 char initrd_filesize[10];
Zhaofeng Libc14b932021-10-20 00:18:15 -0700419 char initrd_str[28];
Patrice Chotard17e88042019-11-25 09:07:37 +0100420 char mac_str[29] = "";
421 char ip_str[68] = "";
422 char *fit_addr = NULL;
423 int bootm_argc = 2;
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700424 int zboot_argc = 3;
Patrice Chotard17e88042019-11-25 09:07:37 +0100425 int len = 0;
Zhaofeng Libc14b932021-10-20 00:18:15 -0700426 ulong kernel_addr_r;
Patrice Chotard17e88042019-11-25 09:07:37 +0100427 void *buf;
428
429 label_print(label);
430
431 label->attempted = 1;
432
433 if (label->localboot) {
434 if (label->localboot_val >= 0)
435 label_localboot(label);
436 return 0;
437 }
438
Patrice Chotard6233de42019-11-25 09:07:39 +0100439 if (!label->kernel) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100440 printf("No kernel given, skipping %s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100441 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100442 return 1;
443 }
444
445 if (label->initrd) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600446 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100447 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100448 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100449 return 1;
450 }
451
Zhaofeng Libc14b932021-10-20 00:18:15 -0700452 initrd_addr_str = env_get("ramdisk_addr_r");
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700453 strncpy(initrd_filesize, env_get("filesize"), 9);
Zhaofeng Libc14b932021-10-20 00:18:15 -0700454
455 strncpy(initrd_str, initrd_addr_str, 18);
456 strcat(initrd_str, ":");
457 strncat(initrd_str, initrd_filesize, 9);
Patrice Chotard17e88042019-11-25 09:07:37 +0100458 }
459
Simon Glassb0d08db2021-10-14 12:47:56 -0600460 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100461 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100462 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100463 return 1;
464 }
465
466 if (label->ipappend & 0x1) {
467 sprintf(ip_str, " ip=%s:%s:%s:%s",
468 env_get("ipaddr"), env_get("serverip"),
469 env_get("gatewayip"), env_get("netmask"));
470 }
471
Kory Maincentcaabd242021-02-02 16:42:28 +0100472 if (IS_ENABLED(CONFIG_CMD_NET)) {
473 if (label->ipappend & 0x2) {
474 int err;
Patrice Chotard6233de42019-11-25 09:07:39 +0100475
Kory Maincentcaabd242021-02-02 16:42:28 +0100476 strcpy(mac_str, " BOOTIF=");
477 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
478 if (err < 0)
479 mac_str[0] = '\0';
480 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100481 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100482
483 if ((label->ipappend & 0x3) || label->append) {
484 char bootargs[CONFIG_SYS_CBSIZE] = "";
485 char finalbootargs[CONFIG_SYS_CBSIZE];
486
487 if (strlen(label->append ?: "") +
488 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
489 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
490 strlen(label->append ?: ""),
491 strlen(ip_str), strlen(mac_str),
492 sizeof(bootargs));
493 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100494 }
Patrice Chotard6233de42019-11-25 09:07:39 +0100495
496 if (label->append)
497 strncpy(bootargs, label->append, sizeof(bootargs));
498
499 strcat(bootargs, ip_str);
500 strcat(bootargs, mac_str);
501
Simon Glassc7b03e82020-11-05 10:33:47 -0700502 cli_simple_process_macros(bootargs, finalbootargs,
503 sizeof(finalbootargs));
Patrice Chotard6233de42019-11-25 09:07:39 +0100504 env_set("bootargs", finalbootargs);
505 printf("append: %s\n", finalbootargs);
Patrice Chotard17e88042019-11-25 09:07:37 +0100506 }
507
Zhaofeng Libc14b932021-10-20 00:18:15 -0700508 kernel_addr = env_get("kernel_addr_r");
Zhaofeng Lidbf84cc2021-10-20 00:18:14 -0700509
Patrice Chotard17e88042019-11-25 09:07:37 +0100510 /* for FIT, append the configuration identifier */
511 if (label->config) {
Zhaofeng Libc14b932021-10-20 00:18:15 -0700512 int len = strlen(kernel_addr) + strlen(label->config) + 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100513
514 fit_addr = malloc(len);
515 if (!fit_addr) {
516 printf("malloc fail (FIT address)\n");
517 return 1;
518 }
Zhaofeng Libc14b932021-10-20 00:18:15 -0700519 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
520 kernel_addr = fit_addr;
Patrice Chotard17e88042019-11-25 09:07:37 +0100521 }
522
523 /*
524 * fdt usage is optional:
Anton Leontievfeb7db82019-09-03 10:52:24 +0300525 * It handles the following scenarios.
Patrice Chotard17e88042019-11-25 09:07:37 +0100526 *
Anton Leontievfeb7db82019-09-03 10:52:24 +0300527 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
528 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
529 * bootm, and adjust argc appropriately.
530 *
531 * If retrieve fails and no exact fdt blob is specified in pxe file with
532 * "fdt" label, try Scenario 2.
Patrice Chotard17e88042019-11-25 09:07:37 +0100533 *
534 * Scenario 2: If there is an fdt_addr specified, pass it along to
535 * bootm, and adjust argc appropriately.
536 *
537 * Scenario 3: fdt blob is not available.
538 */
539 bootm_argv[3] = env_get("fdt_addr_r");
540
541 /* if fdt label is defined then get fdt from server */
542 if (bootm_argv[3]) {
543 char *fdtfile = NULL;
544 char *fdtfilefree = NULL;
545
546 if (label->fdt) {
547 fdtfile = label->fdt;
548 } else if (label->fdtdir) {
549 char *f1, *f2, *f3, *f4, *slash;
550
551 f1 = env_get("fdtfile");
552 if (f1) {
553 f2 = "";
554 f3 = "";
555 f4 = "";
556 } else {
557 /*
558 * For complex cases where this code doesn't
559 * generate the correct filename, the board
560 * code should set $fdtfile during early boot,
561 * or the boot scripts should set $fdtfile
562 * before invoking "pxe" or "sysboot".
563 */
564 f1 = env_get("soc");
565 f2 = "-";
566 f3 = env_get("board");
567 f4 = ".dtb";
Dimitri John Ledkov5ee984a2021-04-21 12:42:01 +0100568 if (!f1) {
569 f1 = "";
570 f2 = "";
571 }
572 if (!f3) {
573 f2 = "";
574 f3 = "";
575 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100576 }
577
578 len = strlen(label->fdtdir);
579 if (!len)
580 slash = "./";
581 else if (label->fdtdir[len - 1] != '/')
582 slash = "/";
583 else
584 slash = "";
585
586 len = strlen(label->fdtdir) + strlen(slash) +
587 strlen(f1) + strlen(f2) + strlen(f3) +
588 strlen(f4) + 1;
589 fdtfilefree = malloc(len);
590 if (!fdtfilefree) {
591 printf("malloc fail (FDT filename)\n");
592 goto cleanup;
593 }
594
595 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
596 label->fdtdir, slash, f1, f2, f3, f4);
597 fdtfile = fdtfilefree;
598 }
599
600 if (fdtfile) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600601 int err = get_relfile_envaddr(ctx, fdtfile,
Patrice Chotard6233de42019-11-25 09:07:39 +0100602 "fdt_addr_r");
603
Patrice Chotard17e88042019-11-25 09:07:37 +0100604 free(fdtfilefree);
605 if (err < 0) {
Anton Leontievfeb7db82019-09-03 10:52:24 +0300606 bootm_argv[3] = NULL;
607
608 if (label->fdt) {
609 printf("Skipping %s for failure retrieving FDT\n",
610 label->name);
611 goto cleanup;
612 }
Patrice Chotard17e88042019-11-25 09:07:37 +0100613 }
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100614
615#ifdef CONFIG_OF_LIBFDT_OVERLAY
616 if (label->fdtoverlays)
Simon Glassb0d08db2021-10-14 12:47:56 -0600617 label_boot_fdtoverlay(ctx, label);
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100618#endif
Patrice Chotard17e88042019-11-25 09:07:37 +0100619 } else {
620 bootm_argv[3] = NULL;
621 }
622 }
623
Zhaofeng Libc14b932021-10-20 00:18:15 -0700624 bootm_argv[1] = kernel_addr;
625 zboot_argv[1] = kernel_addr;
626
627 if (initrd_addr_str) {
628 bootm_argv[2] = initrd_str;
629 bootm_argc = 3;
630
631 zboot_argv[3] = initrd_addr_str;
632 zboot_argv[4] = initrd_filesize;
633 zboot_argc = 5;
634 }
635
Patrice Chotard17e88042019-11-25 09:07:37 +0100636 if (!bootm_argv[3])
637 bootm_argv[3] = env_get("fdt_addr");
638
639 if (bootm_argv[3]) {
640 if (!bootm_argv[2])
641 bootm_argv[2] = "-";
642 bootm_argc = 4;
643 }
644
Zhaofeng Libc14b932021-10-20 00:18:15 -0700645 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
646 buf = map_sysmem(kernel_addr_r, 0);
Patrice Chotard17e88042019-11-25 09:07:37 +0100647 /* Try bootm for legacy and FIT format image */
648 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
Simon Glassb0d08db2021-10-14 12:47:56 -0600649 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100650 /* Try booting an AArch64 Linux kernel image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100651 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
Simon Glassb0d08db2021-10-14 12:47:56 -0600652 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Patrice Chotard17e88042019-11-25 09:07:37 +0100653 /* Try booting a Image */
Kory Maincentcaabd242021-02-02 16:42:28 +0100654 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glassb0d08db2021-10-14 12:47:56 -0600655 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
Kory Maincentb2187d02021-02-02 16:42:29 +0100656 /* Try booting an x86_64 Linux kernel image */
657 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
Simon Glassb0d08db2021-10-14 12:47:56 -0600658 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
Kory Maincentcaabd242021-02-02 16:42:28 +0100659
Patrice Chotard17e88042019-11-25 09:07:37 +0100660 unmap_sysmem(buf);
661
662cleanup:
Simon Glass764d0c02021-10-14 12:48:02 -0600663 free(fit_addr);
664
Patrice Chotard17e88042019-11-25 09:07:37 +0100665 return 1;
666}
667
Simon Glass00302442021-10-14 12:48:01 -0600668/** enum token_type - Tokens for the pxe file parser */
Patrice Chotard17e88042019-11-25 09:07:37 +0100669enum token_type {
670 T_EOL,
671 T_STRING,
672 T_EOF,
673 T_MENU,
674 T_TITLE,
675 T_TIMEOUT,
676 T_LABEL,
677 T_KERNEL,
678 T_LINUX,
679 T_APPEND,
680 T_INITRD,
681 T_LOCALBOOT,
682 T_DEFAULT,
683 T_PROMPT,
684 T_INCLUDE,
685 T_FDT,
686 T_FDTDIR,
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100687 T_FDTOVERLAYS,
Patrice Chotard17e88042019-11-25 09:07:37 +0100688 T_ONTIMEOUT,
689 T_IPAPPEND,
690 T_BACKGROUND,
691 T_INVALID
692};
693
Simon Glass00302442021-10-14 12:48:01 -0600694/** struct token - token - given by a value and a type */
Patrice Chotard17e88042019-11-25 09:07:37 +0100695struct token {
696 char *val;
697 enum token_type type;
698};
699
Simon Glass00302442021-10-14 12:48:01 -0600700/* Keywords recognized */
Patrice Chotard17e88042019-11-25 09:07:37 +0100701static const struct token keywords[] = {
702 {"menu", T_MENU},
703 {"title", T_TITLE},
704 {"timeout", T_TIMEOUT},
705 {"default", T_DEFAULT},
706 {"prompt", T_PROMPT},
707 {"label", T_LABEL},
708 {"kernel", T_KERNEL},
709 {"linux", T_LINUX},
710 {"localboot", T_LOCALBOOT},
711 {"append", T_APPEND},
712 {"initrd", T_INITRD},
713 {"include", T_INCLUDE},
714 {"devicetree", T_FDT},
715 {"fdt", T_FDT},
716 {"devicetreedir", T_FDTDIR},
717 {"fdtdir", T_FDTDIR},
Neil Armstrongc77a1a32021-01-20 09:54:53 +0100718 {"fdtoverlays", T_FDTOVERLAYS},
Patrice Chotard17e88042019-11-25 09:07:37 +0100719 {"ontimeout", T_ONTIMEOUT,},
720 {"ipappend", T_IPAPPEND,},
721 {"background", T_BACKGROUND,},
722 {NULL, T_INVALID}
723};
724
Simon Glass00302442021-10-14 12:48:01 -0600725/**
726 * enum lex_state - lexer state
727 *
Patrice Chotard17e88042019-11-25 09:07:37 +0100728 * Since pxe(linux) files don't have a token to identify the start of a
729 * literal, we have to keep track of when we're in a state where a literal is
730 * expected vs when we're in a state a keyword is expected.
731 */
732enum lex_state {
733 L_NORMAL = 0,
734 L_KEYWORD,
735 L_SLITERAL
736};
737
Simon Glass00302442021-10-14 12:48:01 -0600738/**
739 * get_string() - retrieves a string from *p and stores it as a token in *t.
Patrice Chotard17e88042019-11-25 09:07:37 +0100740 *
Simon Glass00302442021-10-14 12:48:01 -0600741 * This is used for scanning both string literals and keywords.
Patrice Chotard17e88042019-11-25 09:07:37 +0100742 *
743 * Characters from *p are copied into t-val until a character equal to
744 * delim is found, or a NUL byte is reached. If delim has the special value of
745 * ' ', any whitespace character will be used as a delimiter.
746 *
747 * If lower is unequal to 0, uppercase characters will be converted to
748 * lowercase in the result. This is useful to make keywords case
749 * insensitive.
750 *
751 * The location of *p is updated to point to the first character after the end
752 * of the token - the ending delimiter.
753 *
Simon Glass00302442021-10-14 12:48:01 -0600754 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
755 * it.
756 *
757 * @p: Points to a pointer to the current position in the input being processed.
758 * Updated to point at the first character after the current token
759 * @t: Pointers to a token to fill in
760 * @delim: Delimiter character to look for, either newline or space
761 * @lower: true to convert the string to lower case when storing
762 * Returns the new value of t->val, on success, NULL if out of memory
Patrice Chotard17e88042019-11-25 09:07:37 +0100763 */
764static char *get_string(char **p, struct token *t, char delim, int lower)
765{
766 char *b, *e;
767 size_t len, i;
768
769 /*
770 * b and e both start at the beginning of the input stream.
771 *
772 * e is incremented until we find the ending delimiter, or a NUL byte
773 * is reached. Then, we take e - b to find the length of the token.
774 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100775 b = *p;
776 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100777 while (*e) {
778 if ((delim == ' ' && isspace(*e)) || delim == *e)
779 break;
780 e++;
781 }
782
783 len = e - b;
784
785 /*
786 * Allocate memory to hold the string, and copy it in, converting
787 * characters to lowercase if lower is != 0.
788 */
789 t->val = malloc(len + 1);
790 if (!t->val)
791 return NULL;
792
793 for (i = 0; i < len; i++, b++) {
794 if (lower)
795 t->val[i] = tolower(*b);
796 else
797 t->val[i] = *b;
798 }
799
800 t->val[len] = '\0';
801
Simon Glass764d0c02021-10-14 12:48:02 -0600802 /* Update *p so the caller knows where to continue scanning */
Patrice Chotard17e88042019-11-25 09:07:37 +0100803 *p = e;
Patrice Chotard17e88042019-11-25 09:07:37 +0100804 t->type = T_STRING;
805
806 return t->val;
807}
808
Simon Glass00302442021-10-14 12:48:01 -0600809/**
810 * get_keyword() - Populate a keyword token with a type and value
811 *
812 * Updates the ->type field based on the keyword string in @val
813 * @t: Token to populate
Patrice Chotard17e88042019-11-25 09:07:37 +0100814 */
815static void get_keyword(struct token *t)
816{
817 int i;
818
819 for (i = 0; keywords[i].val; i++) {
820 if (!strcmp(t->val, keywords[i].val)) {
821 t->type = keywords[i].type;
822 break;
823 }
824 }
825}
826
Simon Glass00302442021-10-14 12:48:01 -0600827/**
828 * get_token() - Get the next token
Patrice Chotard17e88042019-11-25 09:07:37 +0100829 *
Simon Glass00302442021-10-14 12:48:01 -0600830 * We have to keep track of which state we're in to know if we're looking to get
831 * a string literal or a keyword.
832 *
833 * @p: Points to a pointer to the current position in the input being processed.
834 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100835 */
836static void get_token(char **p, struct token *t, enum lex_state state)
837{
838 char *c = *p;
839
840 t->type = T_INVALID;
841
842 /* eat non EOL whitespace */
843 while (isblank(*c))
844 c++;
845
846 /*
847 * eat comments. note that string literals can't begin with #, but
848 * can contain a # after their first character.
849 */
850 if (*c == '#') {
851 while (*c && *c != '\n')
852 c++;
853 }
854
855 if (*c == '\n') {
856 t->type = T_EOL;
857 c++;
858 } else if (*c == '\0') {
859 t->type = T_EOF;
860 c++;
861 } else if (state == L_SLITERAL) {
862 get_string(&c, t, '\n', 0);
863 } else if (state == L_KEYWORD) {
864 /*
865 * when we expect a keyword, we first get the next string
866 * token delimited by whitespace, and then check if it
867 * matches a keyword in our keyword list. if it does, it's
868 * converted to a keyword token of the appropriate type, and
869 * if not, it remains a string token.
870 */
871 get_string(&c, t, ' ', 1);
872 get_keyword(t);
873 }
874
875 *p = c;
876}
877
Simon Glass00302442021-10-14 12:48:01 -0600878/**
879 * eol_or_eof() - Find end of line
880 *
881 * Increment *c until we get to the end of the current line, or EOF
882 *
883 * @c: Points to a pointer to the current position in the input being processed.
884 * Updated to point at the first character after the current token
Patrice Chotard17e88042019-11-25 09:07:37 +0100885 */
886static void eol_or_eof(char **c)
887{
888 while (**c && **c != '\n')
889 (*c)++;
890}
891
892/*
893 * All of these parse_* functions share some common behavior.
894 *
895 * They finish with *c pointing after the token they parse, and return 1 on
896 * success, or < 0 on error.
897 */
898
899/*
900 * Parse a string literal and store a pointer it at *dst. String literals
901 * terminate at the end of the line.
902 */
903static int parse_sliteral(char **c, char **dst)
904{
905 struct token t;
906 char *s = *c;
907
908 get_token(c, &t, L_SLITERAL);
909
910 if (t.type != T_STRING) {
911 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
912 return -EINVAL;
913 }
914
915 *dst = t.val;
916
917 return 1;
918}
919
920/*
921 * Parse a base 10 (unsigned) integer and store it at *dst.
922 */
923static int parse_integer(char **c, int *dst)
924{
925 struct token t;
926 char *s = *c;
927
928 get_token(c, &t, L_SLITERAL);
Patrice Chotard17e88042019-11-25 09:07:37 +0100929 if (t.type != T_STRING) {
930 printf("Expected string: %.*s\n", (int)(*c - s), s);
931 return -EINVAL;
932 }
933
934 *dst = simple_strtol(t.val, NULL, 10);
935
936 free(t.val);
937
938 return 1;
939}
940
Simon Glassb0d08db2021-10-14 12:47:56 -0600941static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100942 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +0100943
944/*
945 * Parse an include statement, and retrieve and parse the file it mentions.
946 *
947 * base should point to a location where it's safe to store the file, and
948 * nest_level should indicate how many nested includes have occurred. For this
949 * include, nest_level has already been incremented and doesn't need to be
950 * incremented here.
951 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600952static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100953 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +0100954{
955 char *include_path;
956 char *s = *c;
957 int err;
958 char *buf;
959 int ret;
960
961 err = parse_sliteral(c, &include_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100962 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100963 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +0100964 return err;
965 }
966
Simon Glassb0d08db2021-10-14 12:47:56 -0600967 err = get_pxe_file(ctx, include_path, base);
Patrice Chotard17e88042019-11-25 09:07:37 +0100968 if (err < 0) {
969 printf("Couldn't retrieve %s\n", include_path);
970 return err;
971 }
972
973 buf = map_sysmem(base, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -0600974 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +0100975 unmap_sysmem(buf);
976
977 return ret;
978}
979
980/*
981 * Parse lines that begin with 'menu'.
982 *
983 * base and nest are provided to handle the 'menu include' case.
984 *
985 * base should point to a location where it's safe to store the included file.
986 *
987 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
988 * a file it includes, 3 when parsing a file included by that file, and so on.
989 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600990static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +0100991 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +0100992{
993 struct token t;
994 char *s = *c;
995 int err = 0;
996
997 get_token(c, &t, L_KEYWORD);
998
999 switch (t.type) {
1000 case T_TITLE:
1001 err = parse_sliteral(c, &cfg->title);
1002
1003 break;
1004
1005 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001006 err = handle_include(ctx, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001007 break;
1008
1009 case T_BACKGROUND:
1010 err = parse_sliteral(c, &cfg->bmp);
1011 break;
1012
1013 default:
1014 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001015 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001016 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001017 if (err < 0)
1018 return err;
1019
1020 eol_or_eof(c);
1021
1022 return 1;
1023}
1024
1025/*
1026 * Handles parsing a 'menu line' when we're parsing a label.
1027 */
1028static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001029 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +01001030{
1031 struct token t;
1032 char *s;
1033
1034 s = *c;
1035
1036 get_token(c, &t, L_KEYWORD);
1037
1038 switch (t.type) {
1039 case T_DEFAULT:
1040 if (!cfg->default_label)
1041 cfg->default_label = strdup(label->name);
1042
1043 if (!cfg->default_label)
1044 return -ENOMEM;
1045
1046 break;
1047 case T_LABEL:
1048 parse_sliteral(c, &label->menu);
1049 break;
1050 default:
1051 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001052 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001053 }
1054
1055 eol_or_eof(c);
1056
1057 return 0;
1058}
1059
1060/*
1061 * Handles parsing a 'kernel' label.
1062 * expecting "filename" or "<fit_filename>#cfg"
1063 */
1064static int parse_label_kernel(char **c, struct pxe_label *label)
1065{
1066 char *s;
1067 int err;
1068
1069 err = parse_sliteral(c, &label->kernel);
1070 if (err < 0)
1071 return err;
1072
1073 s = strstr(label->kernel, "#");
1074 if (!s)
1075 return 1;
1076
1077 label->config = malloc(strlen(s) + 1);
1078 if (!label->config)
1079 return -ENOMEM;
1080
1081 strcpy(label->config, s);
1082 *s = 0;
1083
1084 return 1;
1085}
1086
1087/*
1088 * Parses a label and adds it to the list of labels for a menu.
1089 *
1090 * A label ends when we either get to the end of a file, or
1091 * get some input we otherwise don't have a handler defined
1092 * for.
1093 *
1094 */
1095static int parse_label(char **c, struct pxe_menu *cfg)
1096{
1097 struct token t;
1098 int len;
1099 char *s = *c;
1100 struct pxe_label *label;
1101 int err;
1102
1103 label = label_create();
1104 if (!label)
1105 return -ENOMEM;
1106
1107 err = parse_sliteral(c, &label->name);
1108 if (err < 0) {
1109 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1110 label_destroy(label);
1111 return -EINVAL;
1112 }
1113
1114 list_add_tail(&label->list, &cfg->labels);
1115
1116 while (1) {
1117 s = *c;
1118 get_token(c, &t, L_KEYWORD);
1119
1120 err = 0;
1121 switch (t.type) {
1122 case T_MENU:
1123 err = parse_label_menu(c, cfg, label);
1124 break;
1125
1126 case T_KERNEL:
1127 case T_LINUX:
1128 err = parse_label_kernel(c, label);
1129 break;
1130
1131 case T_APPEND:
1132 err = parse_sliteral(c, &label->append);
1133 if (label->initrd)
1134 break;
1135 s = strstr(label->append, "initrd=");
1136 if (!s)
1137 break;
1138 s += 7;
1139 len = (int)(strchr(s, ' ') - s);
1140 label->initrd = malloc(len + 1);
1141 strncpy(label->initrd, s, len);
1142 label->initrd[len] = '\0';
1143
1144 break;
1145
1146 case T_INITRD:
1147 if (!label->initrd)
1148 err = parse_sliteral(c, &label->initrd);
1149 break;
1150
1151 case T_FDT:
1152 if (!label->fdt)
1153 err = parse_sliteral(c, &label->fdt);
1154 break;
1155
1156 case T_FDTDIR:
1157 if (!label->fdtdir)
1158 err = parse_sliteral(c, &label->fdtdir);
1159 break;
1160
Neil Armstrongc77a1a32021-01-20 09:54:53 +01001161 case T_FDTOVERLAYS:
1162 if (!label->fdtoverlays)
1163 err = parse_sliteral(c, &label->fdtoverlays);
1164 break;
1165
Patrice Chotard17e88042019-11-25 09:07:37 +01001166 case T_LOCALBOOT:
1167 label->localboot = 1;
1168 err = parse_integer(c, &label->localboot_val);
1169 break;
1170
1171 case T_IPAPPEND:
1172 err = parse_integer(c, &label->ipappend);
1173 break;
1174
1175 case T_EOL:
1176 break;
1177
1178 default:
1179 /*
1180 * put the token back! we don't want it - it's the end
1181 * of a label and whatever token this is, it's
1182 * something for the menu level context to handle.
1183 */
1184 *c = s;
1185 return 1;
1186 }
1187
1188 if (err < 0)
1189 return err;
1190 }
1191}
1192
1193/*
1194 * This 16 comes from the limit pxelinux imposes on nested includes.
1195 *
1196 * There is no reason at all we couldn't do more, but some limit helps prevent
1197 * infinite (until crash occurs) recursion if a file tries to include itself.
1198 */
1199#define MAX_NEST_LEVEL 16
1200
1201/*
1202 * Entry point for parsing a menu file. nest_level indicates how many times
1203 * we've nested in includes. It will be 1 for the top level menu file.
1204 *
1205 * Returns 1 on success, < 0 on error.
1206 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001207static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001208 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001209{
1210 struct token t;
1211 char *s, *b, *label_name;
1212 int err;
1213
1214 b = p;
1215
1216 if (nest_level > MAX_NEST_LEVEL) {
1217 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1218 return -EMLINK;
1219 }
1220
1221 while (1) {
1222 s = p;
1223
1224 get_token(&p, &t, L_KEYWORD);
1225
1226 err = 0;
1227 switch (t.type) {
1228 case T_MENU:
1229 cfg->prompt = 1;
Simon Glassb0d08db2021-10-14 12:47:56 -06001230 err = parse_menu(ctx, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001231 base + ALIGN(strlen(b) + 1, 4),
1232 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001233 break;
1234
1235 case T_TIMEOUT:
1236 err = parse_integer(&p, &cfg->timeout);
1237 break;
1238
1239 case T_LABEL:
1240 err = parse_label(&p, cfg);
1241 break;
1242
1243 case T_DEFAULT:
1244 case T_ONTIMEOUT:
1245 err = parse_sliteral(&p, &label_name);
1246
1247 if (label_name) {
1248 if (cfg->default_label)
1249 free(cfg->default_label);
1250
1251 cfg->default_label = label_name;
1252 }
1253
1254 break;
1255
1256 case T_INCLUDE:
Simon Glassb0d08db2021-10-14 12:47:56 -06001257 err = handle_include(ctx, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001258 base + ALIGN(strlen(b), 4), cfg,
1259 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001260 break;
1261
1262 case T_PROMPT:
1263 eol_or_eof(&p);
1264 break;
1265
1266 case T_EOL:
1267 break;
1268
1269 case T_EOF:
1270 return 1;
1271
1272 default:
1273 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001274 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001275 eol_or_eof(&p);
1276 }
1277
1278 if (err < 0)
1279 return err;
1280 }
1281}
1282
1283/*
Patrice Chotard17e88042019-11-25 09:07:37 +01001284 */
1285void destroy_pxe_menu(struct pxe_menu *cfg)
1286{
1287 struct list_head *pos, *n;
1288 struct pxe_label *label;
1289
Simon Glass764d0c02021-10-14 12:48:02 -06001290 free(cfg->title);
1291 free(cfg->default_label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001292
1293 list_for_each_safe(pos, n, &cfg->labels) {
1294 label = list_entry(pos, struct pxe_label, list);
1295
1296 label_destroy(label);
1297 }
1298
1299 free(cfg);
1300}
1301
Simon Glassb0d08db2021-10-14 12:47:56 -06001302struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001303{
1304 struct pxe_menu *cfg;
1305 char *buf;
1306 int r;
1307
1308 cfg = malloc(sizeof(struct pxe_menu));
Patrice Chotard17e88042019-11-25 09:07:37 +01001309 if (!cfg)
1310 return NULL;
1311
1312 memset(cfg, 0, sizeof(struct pxe_menu));
1313
1314 INIT_LIST_HEAD(&cfg->labels);
1315
1316 buf = map_sysmem(menucfg, 0);
Simon Glassb0d08db2021-10-14 12:47:56 -06001317 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001318 unmap_sysmem(buf);
Patrice Chotard17e88042019-11-25 09:07:37 +01001319 if (r < 0) {
1320 destroy_pxe_menu(cfg);
1321 return NULL;
1322 }
1323
1324 return cfg;
1325}
1326
1327/*
1328 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1329 * menu code.
1330 */
1331static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1332{
1333 struct pxe_label *label;
1334 struct list_head *pos;
1335 struct menu *m;
1336 int err;
1337 int i = 1;
1338 char *default_num = NULL;
1339
1340 /*
1341 * Create a menu and add items for all the labels.
1342 */
1343 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -07001344 cfg->prompt, NULL, label_print, NULL, NULL);
Patrice Chotard17e88042019-11-25 09:07:37 +01001345 if (!m)
1346 return NULL;
1347
1348 list_for_each(pos, &cfg->labels) {
1349 label = list_entry(pos, struct pxe_label, list);
1350
1351 sprintf(label->num, "%d", i++);
1352 if (menu_item_add(m, label->num, label) != 1) {
1353 menu_destroy(m);
1354 return NULL;
1355 }
1356 if (cfg->default_label &&
1357 (strcmp(label->name, cfg->default_label) == 0))
1358 default_num = label->num;
Patrice Chotard17e88042019-11-25 09:07:37 +01001359 }
1360
1361 /*
1362 * After we've created items for each label in the menu, set the
1363 * menu's default label if one was specified.
1364 */
1365 if (default_num) {
1366 err = menu_default_set(m, default_num);
1367 if (err != 1) {
1368 if (err != -ENOENT) {
1369 menu_destroy(m);
1370 return NULL;
1371 }
1372
1373 printf("Missing default: %s\n", cfg->default_label);
1374 }
1375 }
1376
1377 return m;
1378}
1379
1380/*
1381 * Try to boot any labels we have yet to attempt to boot.
1382 */
Simon Glassb0d08db2021-10-14 12:47:56 -06001383static void boot_unattempted_labels(struct pxe_context *ctx,
1384 struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001385{
1386 struct list_head *pos;
1387 struct pxe_label *label;
1388
1389 list_for_each(pos, &cfg->labels) {
1390 label = list_entry(pos, struct pxe_label, list);
1391
1392 if (!label->attempted)
Simon Glassb0d08db2021-10-14 12:47:56 -06001393 label_boot(ctx, label);
Patrice Chotard17e88042019-11-25 09:07:37 +01001394 }
1395}
1396
Simon Glassb0d08db2021-10-14 12:47:56 -06001397void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
Patrice Chotard17e88042019-11-25 09:07:37 +01001398{
1399 void *choice;
1400 struct menu *m;
1401 int err;
1402
Kory Maincentcaabd242021-02-02 16:42:28 +01001403 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1404 /* display BMP if available */
1405 if (cfg->bmp) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001406 if (get_relfile(ctx, cfg->bmp, image_load_addr)) {
Kory Maincentcaabd242021-02-02 16:42:28 +01001407 if (CONFIG_IS_ENABLED(CMD_CLS))
1408 run_command("cls", 0);
1409 bmp_display(image_load_addr,
1410 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1411 } else {
1412 printf("Skipping background bmp %s for failure\n",
1413 cfg->bmp);
1414 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001415 }
1416 }
Patrice Chotard17e88042019-11-25 09:07:37 +01001417
1418 m = pxe_menu_to_menu(cfg);
1419 if (!m)
1420 return;
1421
1422 err = menu_get_choice(m, &choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001423 menu_destroy(m);
1424
1425 /*
1426 * err == 1 means we got a choice back from menu_get_choice.
1427 *
1428 * err == -ENOENT if the menu was setup to select the default but no
1429 * default was set. in that case, we should continue trying to boot
1430 * labels that haven't been attempted yet.
1431 *
1432 * otherwise, the user interrupted or there was some other error and
1433 * we give up.
1434 */
1435
1436 if (err == 1) {
Simon Glassb0d08db2021-10-14 12:47:56 -06001437 err = label_boot(ctx, choice);
Patrice Chotard17e88042019-11-25 09:07:37 +01001438 if (!err)
1439 return;
1440 } else if (err != -ENOENT) {
1441 return;
1442 }
1443
Simon Glassb0d08db2021-10-14 12:47:56 -06001444 boot_unattempted_labels(ctx, cfg);
1445}
1446
Simon Glasse719fe02021-10-14 12:48:04 -06001447int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1448 pxe_getfile_func getfile, void *userdata,
1449 bool allow_abs_path, const char *bootfile)
Simon Glassb0d08db2021-10-14 12:47:56 -06001450{
Simon Glasse719fe02021-10-14 12:48:04 -06001451 const char *last_slash;
1452 size_t path_len = 0;
1453
1454 memset(ctx, '\0', sizeof(*ctx));
Simon Glassb0d08db2021-10-14 12:47:56 -06001455 ctx->cmdtp = cmdtp;
Simon Glass44a20ef2021-10-14 12:47:57 -06001456 ctx->getfile = getfile;
Simon Glass121e1312021-10-14 12:47:58 -06001457 ctx->userdata = userdata;
Simon Glass3ae416a2021-10-14 12:47:59 -06001458 ctx->allow_abs_path = allow_abs_path;
Simon Glasse719fe02021-10-14 12:48:04 -06001459
1460 /* figure out the boot directory, if there is one */
1461 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1462 return -ENOSPC;
1463 ctx->bootdir = strdup(bootfile ? bootfile : "");
1464 if (!ctx->bootdir)
1465 return -ENOMEM;
1466
1467 if (bootfile) {
1468 last_slash = strrchr(bootfile, '/');
1469 if (last_slash)
1470 path_len = (last_slash - bootfile) + 1;
1471 }
1472 ctx->bootdir[path_len] = '\0';
1473
1474 return 0;
1475}
1476
1477void pxe_destroy_ctx(struct pxe_context *ctx)
1478{
1479 free(ctx->bootdir);
Patrice Chotard17e88042019-11-25 09:07:37 +01001480}
Simon Glass791bbfe2021-10-14 12:48:03 -06001481
1482int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1483{
1484 struct pxe_menu *cfg;
1485
1486 cfg = parse_pxefile(ctx, pxefile_addr_r);
1487 if (!cfg) {
1488 printf("Error parsing config file\n");
1489 return 1;
1490 }
1491
1492 if (prompt)
1493 cfg->prompt = 1;
1494
1495 handle_pxe_menu(ctx, cfg);
1496
1497 destroy_pxe_menu(cfg);
1498
1499 return 0;
1500}