blob: 70dbde3a6366f27640073f3c827187bae7bc6ffe [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs06283a62011-08-31 10:37:30 -05002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wu1fb7d0e2014-07-31 17:39:59 -07004 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs06283a62011-08-31 10:37:30 -05005 */
Wolfgang Denk1a459662013-07-08 09:37:19 +02006
Jason Hobbs06283a62011-08-31 10:37:30 -05007#include <common.h>
8#include <command.h>
Simon Glass14449982019-12-28 10:44:44 -07009#include <fs.h>
Simon Glass77f4e472019-12-28 10:44:43 -070010#include <net.h>
Jason Hobbs06283a62011-08-31 10:37:30 -050011
Patrice Chotard2373cba2019-11-25 09:07:37 +010012#include "pxe_utils.h"
Jason Hobbs06283a62011-08-31 10:37:30 -050013
Patrice Chotard2373cba2019-11-25 09:07:37 +010014#ifdef CONFIG_CMD_NET
Rob Herring39f98552012-12-02 21:00:28 -060015const char *pxe_default_paths[] = {
Joe Hershberger58d9ff92013-06-24 17:21:04 -050016#ifdef CONFIG_SYS_SOC
Marek BehĂșn2455efa2019-05-02 15:29:12 +020017#ifdef CONFIG_SYS_BOARD
18 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
19#endif
Rob Herring39f98552012-12-02 21:00:28 -060020 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger58d9ff92013-06-24 17:21:04 -050021#endif
Rob Herring39f98552012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH,
23 "default",
24 NULL
25};
26
Simon Glassb1ead6b2021-10-14 12:47:57 -060027static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
Simon Glass09140112020-05-10 11:40:03 -060028 char *file_addr)
Rob Herring669df7e2012-05-25 10:47:39 +000029{
30 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
31
32 tftp_argv[1] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -060033 tftp_argv[2] = (void *)file_path;
Rob Herring669df7e2012-05-25 10:47:39 +000034
Simon Glassb1ead6b2021-10-14 12:47:57 -060035 if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
Rob Herring669df7e2012-05-25 10:47:39 +000036 return -ENOENT;
37
38 return 1;
39}
Stephen Warrenb81fdb02014-02-05 20:49:20 -070040
Jason Hobbs06283a62011-08-31 10:37:30 -050041/*
42 * Looks for a pxe file with a name based on the pxeuuid environment variable.
43 *
44 * Returns 1 on success or < 0 on error.
45 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -060046static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -050047{
48 char *uuid_str;
49
50 uuid_str = from_env("pxeuuid");
51
52 if (!uuid_str)
53 return -ENOENT;
54
Simon Glassfd3fa5c2021-10-14 12:47:56 -060055 return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -050056}
57
58/*
59 * Looks for a pxe file with a name based on the 'ethaddr' environment
60 * variable.
61 *
62 * Returns 1 on success or < 0 on error.
63 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -060064static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -050065{
66 char mac_str[21];
67 int err;
68
69 err = format_mac_pxe(mac_str, sizeof(mac_str));
70
71 if (err < 0)
72 return err;
73
Simon Glassfd3fa5c2021-10-14 12:47:56 -060074 return get_pxelinux_path(ctx, mac_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -050075}
76
77/*
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
80 * that exactly.
81 *
82 * Returns 1 on success or < 0 on error.
83 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -060084static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -050085{
86 char ip_addr[9];
87 int mask_pos, err;
88
Joe Hershberger049a95a2015-04-08 01:41:01 -050089 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
Jason Hobbs06283a62011-08-31 10:37:30 -050090
91 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -060092 err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -050093
94 if (err > 0)
95 return err;
96
97 ip_addr[mask_pos] = '\0';
98 }
99
100 return -ENOENT;
101}
Jason Hobbs06283a62011-08-31 10:37:30 -0500102/*
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.
107 *
108 * UUID comes from pxeuuid env variable, if defined
109 * MAC addr comes from ethaddr env variable, if defined
110 * IP
111 *
112 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
113 *
114 * Returns 0 on success or 1 on error.
115 */
116static int
Simon Glass09140112020-05-10 11:40:03 -0600117do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -0500118{
119 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000120 unsigned long pxefile_addr_r;
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600121 struct pxe_context ctx;
Rob Herring39f98552012-12-02 21:00:28 -0600122 int err, i = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500123
Simon Glassb1ead6b2021-10-14 12:47:57 -0600124 pxe_setup_ctx(&ctx, cmdtp, do_get_tftp);
Rob Herring669df7e2012-05-25 10:47:39 +0000125
Jason Hobbs06283a62011-08-31 10:37:30 -0500126 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000127 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500128
Jason Hobbs06283a62011-08-31 10:37:30 -0500129 pxefile_addr_str = from_env("pxefile_addr_r");
130
131 if (!pxefile_addr_str)
132 return 1;
133
134 err = strict_strtoul(pxefile_addr_str, 16,
Patrice Chotard31839dc2019-11-25 09:07:41 +0100135 (unsigned long *)&pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500136 if (err < 0)
137 return 1;
138
139 /*
140 * Keep trying paths until we successfully get a file we're looking
141 * for.
142 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600143 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) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500146 printf("Config file found\n");
147
148 return 0;
149 }
150
Rob Herring39f98552012-12-02 21:00:28 -0600151 while (pxe_default_paths[i]) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600152 if (get_pxelinux_path(&ctx, pxe_default_paths[i],
Sjoerd Simons4a0bd102015-04-13 22:54:25 +0200153 pxefile_addr_r) > 0) {
Rob Herring39f98552012-12-02 21:00:28 -0600154 printf("Config file found\n");
155 return 0;
156 }
157 i++;
158 }
159
Jason Hobbs06283a62011-08-31 10:37:30 -0500160 printf("Config file not found\n");
161
162 return 1;
163}
Jason Hobbs06283a62011-08-31 10:37:30 -0500164
165/*
166 * Boots a system using a pxe file
167 *
168 * Returns 0 on success, 1 on error.
169 */
170static int
Simon Glass09140112020-05-10 11:40:03 -0600171do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -0500172{
173 unsigned long pxefile_addr_r;
174 struct pxe_menu *cfg;
175 char *pxefile_addr_str;
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600176 struct pxe_context ctx;
Jason Hobbs06283a62011-08-31 10:37:30 -0500177
Simon Glassb1ead6b2021-10-14 12:47:57 -0600178 pxe_setup_ctx(&ctx, cmdtp, do_get_tftp);
Rob Herring669df7e2012-05-25 10:47:39 +0000179
Jason Hobbs06283a62011-08-31 10:37:30 -0500180 if (argc == 1) {
181 pxefile_addr_str = from_env("pxefile_addr_r");
182 if (!pxefile_addr_str)
183 return 1;
184
185 } else if (argc == 2) {
186 pxefile_addr_str = argv[1];
187 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000188 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500189 }
190
191 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
192 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
193 return 1;
194 }
195
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600196 cfg = parse_pxefile(&ctx, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500197
Patrice Chotard31839dc2019-11-25 09:07:41 +0100198 if (!cfg) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500199 printf("Error parsing config file\n");
200 return 1;
201 }
202
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600203 handle_pxe_menu(&ctx, cfg);
Jason Hobbs06283a62011-08-31 10:37:30 -0500204
205 destroy_pxe_menu(cfg);
206
Joe Hershberger14111572015-04-08 01:41:02 -0500207 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
Stephen Warrended2e202014-07-22 18:06:46 -0600208
Jason Hobbs06283a62011-08-31 10:37:30 -0500209 return 0;
210}
211
Simon Glass09140112020-05-10 11:40:03 -0600212static struct cmd_tbl cmd_pxe_sub[] = {
Jason Hobbs06283a62011-08-31 10:37:30 -0500213 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
214 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
215};
216
T Karthik Reddy504d1792020-09-16 01:13:56 -0600217static void __maybe_unused pxe_reloc(void)
218{
219 static int relocated_pxe;
220
221 if (!relocated_pxe) {
222 fixup_cmdtable(cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
223 relocated_pxe = 1;
224 }
225}
226
Simon Glass09140112020-05-10 11:40:03 -0600227static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -0500228{
Simon Glass09140112020-05-10 11:40:03 -0600229 struct cmd_tbl *cp;
Jason Hobbs06283a62011-08-31 10:37:30 -0500230
T Karthik Reddy504d1792020-09-16 01:13:56 -0600231#if defined(CONFIG_NEEDS_MANUAL_RELOC)
232 pxe_reloc();
233#endif
234
Jason Hobbs06283a62011-08-31 10:37:30 -0500235 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000236 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500237
Rob Herringe5a9a402013-10-18 13:04:42 -0500238 is_pxe = true;
239
Jason Hobbs06283a62011-08-31 10:37:30 -0500240 /* drop initial "pxe" arg */
241 argc--;
242 argv++;
243
244 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
245
246 if (cp)
247 return cp->cmd(cmdtp, flag, argc, argv);
248
Simon Glass4c12eeb2011-12-10 08:44:01 +0000249 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500250}
251
Patrice Chotard31839dc2019-11-25 09:07:41 +0100252U_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"
Jason Hobbs06283a62011-08-31 10:37:30 -0500256);
Stephen Warrenb81fdb02014-02-05 20:49:20 -0700257#endif