]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
06283a64 JH |
2 | /* |
3 | * Copyright 2010-2011 Calxeda, Inc. | |
1fb7d0e6 | 4 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
06283a64 | 5 | */ |
1a459660 | 6 | |
06283a64 JH |
7 | #include <common.h> |
8 | #include <command.h> | |
14449982 | 9 | #include <fs.h> |
77f4e477 | 10 | #include <net.h> |
06283a64 | 11 | |
2373cba3 | 12 | #include "pxe_utils.h" |
06283a64 | 13 | |
2373cba3 | 14 | #ifdef CONFIG_CMD_NET |
39f98553 | 15 | const char *pxe_default_paths[] = { |
58d9ff93 | 16 | #ifdef CONFIG_SYS_SOC |
2455efa2 MB |
17 | #ifdef CONFIG_SYS_BOARD |
18 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD, | |
19 | #endif | |
39f98553 | 20 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC, |
58d9ff93 | 21 | #endif |
39f98553 RH |
22 | "default-" CONFIG_SYS_ARCH, |
23 | "default", | |
24 | NULL | |
25 | }; | |
26 | ||
09140113 SG |
27 | static int do_get_tftp(struct cmd_tbl *cmdtp, const char *file_path, |
28 | char *file_addr) | |
669df7e4 RH |
29 | { |
30 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; | |
31 | ||
32 | tftp_argv[1] = file_addr; | |
23b7194e | 33 | tftp_argv[2] = (void *)file_path; |
669df7e4 | 34 | |
0e3f3f8a | 35 | if (do_tftpb(cmdtp, 0, 3, tftp_argv)) |
669df7e4 RH |
36 | return -ENOENT; |
37 | ||
38 | return 1; | |
39 | } | |
b81fdb04 | 40 | |
06283a64 JH |
41 | /* |
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 | */ | |
09140113 | 46 | static int pxe_uuid_path(struct cmd_tbl *cmdtp, unsigned long pxefile_addr_r) |
06283a64 JH |
47 | { |
48 | char *uuid_str; | |
49 | ||
50 | uuid_str = from_env("pxeuuid"); | |
51 | ||
52 | if (!uuid_str) | |
53 | return -ENOENT; | |
54 | ||
0e3f3f8a | 55 | return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r); |
06283a64 JH |
56 | } |
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 | */ | |
09140113 | 64 | static int pxe_mac_path(struct cmd_tbl *cmdtp, unsigned long pxefile_addr_r) |
06283a64 JH |
65 | { |
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 | ||
0e3f3f8a | 74 | return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r); |
06283a64 JH |
75 | } |
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 | */ | |
09140113 | 84 | static int pxe_ipaddr_paths(struct cmd_tbl *cmdtp, unsigned long pxefile_addr_r) |
06283a64 JH |
85 | { |
86 | char ip_addr[9]; | |
87 | int mask_pos, err; | |
88 | ||
049a95a7 | 89 | sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr)); |
06283a64 JH |
90 | |
91 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { | |
0e3f3f8a | 92 | err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r); |
06283a64 JH |
93 | |
94 | if (err > 0) | |
95 | return err; | |
96 | ||
97 | ip_addr[mask_pos] = '\0'; | |
98 | } | |
99 | ||
100 | return -ENOENT; | |
101 | } | |
06283a64 JH |
102 | /* |
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 | */ | |
116 | static int | |
09140113 | 117 | do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
06283a64 JH |
118 | { |
119 | char *pxefile_addr_str; | |
834c9384 | 120 | unsigned long pxefile_addr_r; |
39f98553 | 121 | int err, i = 0; |
06283a64 | 122 | |
669df7e4 RH |
123 | do_getfile = do_get_tftp; |
124 | ||
06283a64 | 125 | if (argc != 1) |
4c12eeb8 | 126 | return CMD_RET_USAGE; |
06283a64 | 127 | |
06283a64 JH |
128 | pxefile_addr_str = from_env("pxefile_addr_r"); |
129 | ||
130 | if (!pxefile_addr_str) | |
131 | return 1; | |
132 | ||
133 | err = strict_strtoul(pxefile_addr_str, 16, | |
31839dc2 | 134 | (unsigned long *)&pxefile_addr_r); |
06283a64 JH |
135 | if (err < 0) |
136 | return 1; | |
137 | ||
138 | /* | |
139 | * Keep trying paths until we successfully get a file we're looking | |
140 | * for. | |
141 | */ | |
4a0bd102 SS |
142 | if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 || |
143 | pxe_mac_path(cmdtp, pxefile_addr_r) > 0 || | |
144 | pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) { | |
06283a64 JH |
145 | printf("Config file found\n"); |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
39f98553 | 150 | while (pxe_default_paths[i]) { |
0e3f3f8a | 151 | if (get_pxelinux_path(cmdtp, pxe_default_paths[i], |
4a0bd102 | 152 | pxefile_addr_r) > 0) { |
39f98553 RH |
153 | printf("Config file found\n"); |
154 | return 0; | |
155 | } | |
156 | i++; | |
157 | } | |
158 | ||
06283a64 JH |
159 | printf("Config file not found\n"); |
160 | ||
161 | return 1; | |
162 | } | |
06283a64 | 163 | |
06283a64 JH |
164 | /* |
165 | * Boots a system using a pxe file | |
166 | * | |
167 | * Returns 0 on success, 1 on error. | |
168 | */ | |
169 | static int | |
09140113 | 170 | do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
06283a64 JH |
171 | { |
172 | unsigned long pxefile_addr_r; | |
173 | struct pxe_menu *cfg; | |
174 | char *pxefile_addr_str; | |
175 | ||
669df7e4 RH |
176 | do_getfile = do_get_tftp; |
177 | ||
06283a64 JH |
178 | if (argc == 1) { |
179 | pxefile_addr_str = from_env("pxefile_addr_r"); | |
180 | if (!pxefile_addr_str) | |
181 | return 1; | |
182 | ||
183 | } else if (argc == 2) { | |
184 | pxefile_addr_str = argv[1]; | |
185 | } else { | |
4c12eeb8 | 186 | return CMD_RET_USAGE; |
06283a64 JH |
187 | } |
188 | ||
189 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { | |
190 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); | |
191 | return 1; | |
192 | } | |
193 | ||
4a0bd102 | 194 | cfg = parse_pxefile(cmdtp, pxefile_addr_r); |
06283a64 | 195 | |
31839dc2 | 196 | if (!cfg) { |
06283a64 JH |
197 | printf("Error parsing config file\n"); |
198 | return 1; | |
199 | } | |
200 | ||
d7884e04 | 201 | handle_pxe_menu(cmdtp, cfg); |
06283a64 JH |
202 | |
203 | destroy_pxe_menu(cfg); | |
204 | ||
1411157d | 205 | copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name)); |
ded2e20e | 206 | |
06283a64 JH |
207 | return 0; |
208 | } | |
209 | ||
09140113 | 210 | static struct cmd_tbl cmd_pxe_sub[] = { |
06283a64 JH |
211 | U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""), |
212 | U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "") | |
213 | }; | |
214 | ||
09140113 | 215 | static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
06283a64 | 216 | { |
09140113 | 217 | struct cmd_tbl *cp; |
06283a64 JH |
218 | |
219 | if (argc < 2) | |
4c12eeb8 | 220 | return CMD_RET_USAGE; |
06283a64 | 221 | |
e5a9a407 RH |
222 | is_pxe = true; |
223 | ||
06283a64 JH |
224 | /* drop initial "pxe" arg */ |
225 | argc--; | |
226 | argv++; | |
227 | ||
228 | cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); | |
229 | ||
230 | if (cp) | |
231 | return cp->cmd(cmdtp, flag, argc, argv); | |
232 | ||
4c12eeb8 | 233 | return CMD_RET_USAGE; |
06283a64 JH |
234 | } |
235 | ||
31839dc2 PC |
236 | U_BOOT_CMD(pxe, 3, 1, do_pxe, |
237 | "commands to get and boot from pxe files", | |
238 | "get - try to retrieve a pxe file using tftp\n" | |
239 | "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n" | |
06283a64 | 240 | ); |
b81fdb04 | 241 | #endif |