blob: bce6728875a6c3de11edc6c07ca4b66244731005 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0e3a5932011-08-31 10:37:30 -05002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wua1d715b2014-07-31 17:39:59 -07004 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs0e3a5932011-08-31 10:37:30 -05005 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006
Jason Hobbs0e3a5932011-08-31 10:37:30 -05007#include <common.h>
8#include <command.h>
Simon Glass1b3f75f2019-12-28 10:44:44 -07009#include <fs.h>
Simon Glass6e51ee12019-12-28 10:44:43 -070010#include <net.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050011
Patrice Chotard17e88042019-11-25 09:07:37 +010012#include "pxe_utils.h"
Jason Hobbs0e3a5932011-08-31 10:37:30 -050013
Patrice Chotard17e88042019-11-25 09:07:37 +010014#ifdef CONFIG_CMD_NET
Rob Herringe26e8d62012-12-02 21:00:28 -060015const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050016#ifdef CONFIG_SYS_SOC
Marek BehĂșn016ed132019-05-02 15:29:12 +020017#ifdef CONFIG_SYS_BOARD
18 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
19#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060020 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050021#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH,
23 "default",
24 NULL
25};
26
Steven Falco7de0fa82013-10-07 09:51:48 -040027static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
Rob Herringeee675f2012-05-25 10:47:39 +000028{
29 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
30
31 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -060032 tftp_argv[2] = (void *)file_path;
Rob Herringeee675f2012-05-25 10:47:39 +000033
Steven Falco7de0fa82013-10-07 09:51:48 -040034 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +000035 return -ENOENT;
36
37 return 1;
38}
Stephen Warren857291b2014-02-05 20:49:20 -070039
Jason Hobbs0e3a5932011-08-31 10:37:30 -050040/*
41 * Looks for a pxe file with a name based on the pxeuuid environment variable.
42 *
43 * Returns 1 on success or < 0 on error.
44 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +020045static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050046{
47 char *uuid_str;
48
49 uuid_str = from_env("pxeuuid");
50
51 if (!uuid_str)
52 return -ENOENT;
53
Steven Falco7de0fa82013-10-07 09:51:48 -040054 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050055}
56
57/*
58 * Looks for a pxe file with a name based on the 'ethaddr' environment
59 * variable.
60 *
61 * Returns 1 on success or < 0 on error.
62 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +020063static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050064{
65 char mac_str[21];
66 int err;
67
68 err = format_mac_pxe(mac_str, sizeof(mac_str));
69
70 if (err < 0)
71 return err;
72
Steven Falco7de0fa82013-10-07 09:51:48 -040073 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050074}
75
76/*
77 * Looks for pxe files with names based on our IP address. See pxelinux
78 * documentation for details on what these file names look like. We match
79 * that exactly.
80 *
81 * Returns 1 on success or < 0 on error.
82 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +020083static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050084{
85 char ip_addr[9];
86 int mask_pos, err;
87
Joe Hershberger5874dec2015-04-08 01:41:01 -050088 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
Jason Hobbs0e3a5932011-08-31 10:37:30 -050089
90 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Steven Falco7de0fa82013-10-07 09:51:48 -040091 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050092
93 if (err > 0)
94 return err;
95
96 ip_addr[mask_pos] = '\0';
97 }
98
99 return -ENOENT;
100}
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500101/*
102 * Entry point for the 'pxe get' command.
103 * This Follows pxelinux's rules to download a config file from a tftp server.
104 * The file is stored at the location given by the pxefile_addr_r environment
105 * variable, which must be set.
106 *
107 * UUID comes from pxeuuid env variable, if defined
108 * MAC addr comes from ethaddr env variable, if defined
109 * IP
110 *
111 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
112 *
113 * Returns 0 on success or 1 on error.
114 */
115static int
116do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
117{
118 char *pxefile_addr_str;
Jason Hobbs9135c792012-03-05 08:12:28 +0000119 unsigned long pxefile_addr_r;
Rob Herringe26e8d62012-12-02 21:00:28 -0600120 int err, i = 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500121
Rob Herringeee675f2012-05-25 10:47:39 +0000122 do_getfile = do_get_tftp;
123
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500124 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000125 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500126
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500127 pxefile_addr_str = from_env("pxefile_addr_r");
128
129 if (!pxefile_addr_str)
130 return 1;
131
132 err = strict_strtoul(pxefile_addr_str, 16,
Patrice Chotard7f871652019-11-25 09:07:41 +0100133 (unsigned long *)&pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500134 if (err < 0)
135 return 1;
136
137 /*
138 * Keep trying paths until we successfully get a file we're looking
139 * for.
140 */
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200141 if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
142 pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
143 pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500144 printf("Config file found\n");
145
146 return 0;
147 }
148
Rob Herringe26e8d62012-12-02 21:00:28 -0600149 while (pxe_default_paths[i]) {
Steven Falco7de0fa82013-10-07 09:51:48 -0400150 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200151 pxefile_addr_r) > 0) {
Rob Herringe26e8d62012-12-02 21:00:28 -0600152 printf("Config file found\n");
153 return 0;
154 }
155 i++;
156 }
157
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500158 printf("Config file not found\n");
159
Rob Herring58d7f432012-12-02 21:00:22 -0600160 return 1;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500161}
162
163/*
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500164 * Boots a system using a pxe file
165 *
166 * Returns 0 on success, 1 on error.
167 */
168static int
169do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
170{
171 unsigned long pxefile_addr_r;
172 struct pxe_menu *cfg;
173 char *pxefile_addr_str;
174
Rob Herringeee675f2012-05-25 10:47:39 +0000175 do_getfile = do_get_tftp;
176
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500177 if (argc == 1) {
178 pxefile_addr_str = from_env("pxefile_addr_r");
179 if (!pxefile_addr_str)
180 return 1;
181
182 } else if (argc == 2) {
183 pxefile_addr_str = argv[1];
184 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +0000185 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500186 }
187
188 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
189 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
190 return 1;
191 }
192
Sjoerd Simons45af07c2015-04-13 22:54:25 +0200193 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500194
Patrice Chotard7f871652019-11-25 09:07:41 +0100195 if (!cfg) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500196 printf("Error parsing config file\n");
197 return 1;
198 }
199
Tom Rini5b5b1762013-09-24 09:05:08 -0400200 handle_pxe_menu(cmdtp, cfg);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500201
202 destroy_pxe_menu(cfg);
203
Joe Hershberger290c8992015-04-08 01:41:02 -0500204 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
Stephen Warrenc24cbc22014-07-22 18:06:46 -0600205
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500206 return 0;
207}
208
209static cmd_tbl_t cmd_pxe_sub[] = {
210 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
211 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
212};
213
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +0200214static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500215{
216 cmd_tbl_t *cp;
217
218 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000219 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500220
Rob Herring759456e2013-10-18 13:04:42 -0500221 is_pxe = true;
222
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500223 /* drop initial "pxe" arg */
224 argc--;
225 argv++;
226
227 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
228
229 if (cp)
230 return cp->cmd(cmdtp, flag, argc, argv);
231
Simon Glassa06dfc72011-12-10 08:44:01 +0000232 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500233}
234
Patrice Chotard7f871652019-11-25 09:07:41 +0100235U_BOOT_CMD(pxe, 3, 1, do_pxe,
236 "commands to get and boot from pxe files",
237 "get - try to retrieve a pxe file using tftp\n"
238 "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500239);
Stephen Warren857291b2014-02-05 20:49:20 -0700240#endif