1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
12 #include "pxe_utils.h"
15 const char *pxe_default_paths[] = {
17 #ifdef CONFIG_SYS_BOARD
18 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
20 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
22 "default-" CONFIG_SYS_ARCH,
27 static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
28 char *file_addr, ulong *sizep)
30 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
33 tftp_argv[1] = file_addr;
34 tftp_argv[2] = (void *)file_path;
36 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
38 ret = pxe_get_file_size(sizep);
40 return log_msg_ret("tftp", ret);
41 ctx->pxe_file_size = *sizep;
47 * Looks for a pxe file with a name based on the pxeuuid environment variable.
49 * Returns 1 on success or < 0 on error.
51 static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
55 uuid_str = from_env("pxeuuid");
60 return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r);
64 * Looks for a pxe file with a name based on the 'ethaddr' environment
67 * Returns 1 on success or < 0 on error.
69 static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
74 err = format_mac_pxe(mac_str, sizeof(mac_str));
79 return get_pxelinux_path(ctx, mac_str, pxefile_addr_r);
83 * Looks for pxe files with names based on our IP address. See pxelinux
84 * documentation for details on what these file names look like. We match
87 * Returns 1 on success or < 0 on error.
89 static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r)
94 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
96 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
97 err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r);
102 ip_addr[mask_pos] = '\0';
108 int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep)
110 struct cmd_tbl cmdtp[] = {}; /* dummy */
111 struct pxe_context ctx;
114 if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
115 env_get("bootfile")))
118 * Keep trying paths until we successfully get a file we're looking
121 if (pxe_uuid_path(&ctx, pxefile_addr_r) > 0 ||
122 pxe_mac_path(&ctx, pxefile_addr_r) > 0 ||
123 pxe_ipaddr_paths(&ctx, pxefile_addr_r) > 0)
127 while (pxe_default_paths[i]) {
128 if (get_pxelinux_path(&ctx, pxe_default_paths[i],
134 pxe_destroy_ctx(&ctx);
138 *bootdirp = env_get("bootfile");
141 * The PXE file size is returned but not the name. It is probably not
144 *sizep = ctx.pxe_file_size;
145 pxe_destroy_ctx(&ctx);
151 * Entry point for the 'pxe get' command.
152 * This Follows pxelinux's rules to download a config file from a tftp server.
153 * The file is stored at the location given by the pxefile_addr_r environment
154 * variable, which must be set.
156 * UUID comes from pxeuuid env variable, if defined
157 * MAC addr comes from ethaddr env variable, if defined
160 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
162 * Returns 0 on success or 1 on error.
165 do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
167 char *pxefile_addr_str;
168 ulong pxefile_addr_r;
174 return CMD_RET_USAGE;
176 pxefile_addr_str = from_env("pxefile_addr_r");
178 if (!pxefile_addr_str)
181 ret = strict_strtoul(pxefile_addr_str, 16,
182 (unsigned long *)&pxefile_addr_r);
186 ret = pxe_get(pxefile_addr_r, &fname, &size);
189 printf("Config file '%s' found\n", fname);
192 printf("Out of memory\n");
193 return CMD_RET_FAILURE;
195 printf("Config file not found\n");
196 return CMD_RET_FAILURE;
203 * Boots a system using a pxe file
205 * Returns 0 on success, 1 on error.
208 do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
210 unsigned long pxefile_addr_r;
211 char *pxefile_addr_str;
212 struct pxe_context ctx;
216 pxefile_addr_str = from_env("pxefile_addr_r");
217 if (!pxefile_addr_str)
220 } else if (argc == 2) {
221 pxefile_addr_str = argv[1];
223 return CMD_RET_USAGE;
226 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
227 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
231 if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
232 env_get("bootfile"))) {
233 printf("Out of memory\n");
234 return CMD_RET_FAILURE;
236 ret = pxe_process(&ctx, pxefile_addr_r, false);
237 pxe_destroy_ctx(&ctx);
239 return CMD_RET_FAILURE;
241 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
246 static struct cmd_tbl cmd_pxe_sub[] = {
247 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
248 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
251 static void __maybe_unused pxe_reloc(void)
253 static int relocated_pxe;
255 if (!relocated_pxe) {
256 fixup_cmdtable(cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
261 static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
265 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
270 return CMD_RET_USAGE;
272 /* drop initial "pxe" arg */
276 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
279 return cp->cmd(cmdtp, flag, argc, argv);
281 return CMD_RET_USAGE;
284 U_BOOT_CMD(pxe, 3, 1, do_pxe,
285 "commands to get and boot from pxe files",
286 "get - try to retrieve a pxe file using tftp\n"
287 "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"