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