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,
30 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
32 tftp_argv[1] = file_addr;
33 tftp_argv[2] = (void *)file_path;
35 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
42 * Looks for a pxe file with a name based on the pxeuuid environment variable.
44 * Returns 1 on success or < 0 on error.
46 static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
50 uuid_str = from_env("pxeuuid");
55 return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r);
59 * Looks for a pxe file with a name based on the 'ethaddr' environment
62 * Returns 1 on success or < 0 on error.
64 static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
69 err = format_mac_pxe(mac_str, sizeof(mac_str));
74 return get_pxelinux_path(ctx, mac_str, pxefile_addr_r);
78 * Looks for pxe files with names based on our IP address. See pxelinux
79 * documentation for details on what these file names look like. We match
82 * Returns 1 on success or < 0 on error.
84 static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r)
89 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
91 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
92 err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r);
97 ip_addr[mask_pos] = '\0';
103 * Entry point for the 'pxe get' command.
104 * This Follows pxelinux's rules to download a config file from a tftp server.
105 * The file is stored at the location given by the pxefile_addr_r environment
106 * variable, which must be set.
108 * UUID comes from pxeuuid env variable, if defined
109 * MAC addr comes from ethaddr env variable, if defined
112 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
114 * Returns 0 on success or 1 on error.
117 do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
119 char *pxefile_addr_str;
120 unsigned long pxefile_addr_r;
121 struct pxe_context ctx;
124 pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL);
127 return CMD_RET_USAGE;
129 pxefile_addr_str = from_env("pxefile_addr_r");
131 if (!pxefile_addr_str)
134 err = strict_strtoul(pxefile_addr_str, 16,
135 (unsigned long *)&pxefile_addr_r);
140 * Keep trying paths until we successfully get a file we're looking
143 if (pxe_uuid_path(&ctx, pxefile_addr_r) > 0 ||
144 pxe_mac_path(&ctx, pxefile_addr_r) > 0 ||
145 pxe_ipaddr_paths(&ctx, pxefile_addr_r) > 0) {
146 printf("Config file found\n");
151 while (pxe_default_paths[i]) {
152 if (get_pxelinux_path(&ctx, pxe_default_paths[i],
153 pxefile_addr_r) > 0) {
154 printf("Config file found\n");
160 printf("Config file not found\n");
166 * Boots a system using a pxe file
168 * Returns 0 on success, 1 on error.
171 do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
173 unsigned long pxefile_addr_r;
174 struct pxe_menu *cfg;
175 char *pxefile_addr_str;
176 struct pxe_context ctx;
178 pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL);
181 pxefile_addr_str = from_env("pxefile_addr_r");
182 if (!pxefile_addr_str)
185 } else if (argc == 2) {
186 pxefile_addr_str = argv[1];
188 return CMD_RET_USAGE;
191 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
192 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
196 cfg = parse_pxefile(&ctx, pxefile_addr_r);
199 printf("Error parsing config file\n");
203 handle_pxe_menu(&ctx, cfg);
205 destroy_pxe_menu(cfg);
207 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
212 static struct cmd_tbl cmd_pxe_sub[] = {
213 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
214 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
217 static void __maybe_unused pxe_reloc(void)
219 static int relocated_pxe;
221 if (!relocated_pxe) {
222 fixup_cmdtable(cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
227 static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
231 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
236 return CMD_RET_USAGE;
240 /* drop initial "pxe" arg */
244 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
247 return cp->cmd(cmdtp, flag, argc, argv);
249 return CMD_RET_USAGE;
252 U_BOOT_CMD(pxe, 3, 1, do_pxe,
253 "commands to get and boot from pxe files",
254 "get - try to retrieve a pxe file using tftp\n"
255 "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"