]>
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 | ||
0e3f3f8a | 27 | static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr) |
669df7e4 RH |
28 | { |
29 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; | |
30 | ||
31 | tftp_argv[1] = file_addr; | |
23b7194e | 32 | tftp_argv[2] = (void *)file_path; |
669df7e4 | 33 | |
0e3f3f8a | 34 | if (do_tftpb(cmdtp, 0, 3, tftp_argv)) |
669df7e4 RH |
35 | return -ENOENT; |
36 | ||
37 | return 1; | |
38 | } | |
b81fdb04 | 39 | |
06283a64 JH |
40 | /* |
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 | */ | |
4a0bd102 | 45 | static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r) |
06283a64 JH |
46 | { |
47 | char *uuid_str; | |
48 | ||
49 | uuid_str = from_env("pxeuuid"); | |
50 | ||
51 | if (!uuid_str) | |
52 | return -ENOENT; | |
53 | ||
0e3f3f8a | 54 | return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r); |
06283a64 JH |
55 | } |
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 | */ | |
4a0bd102 | 63 | static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r) |
06283a64 JH |
64 | { |
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 | ||
0e3f3f8a | 73 | return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r); |
06283a64 JH |
74 | } |
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 | */ | |
4a0bd102 | 83 | static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r) |
06283a64 JH |
84 | { |
85 | char ip_addr[9]; | |
86 | int mask_pos, err; | |
87 | ||
049a95a7 | 88 | sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr)); |
06283a64 JH |
89 | |
90 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { | |
0e3f3f8a | 91 | err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r); |
06283a64 JH |
92 | |
93 | if (err > 0) | |
94 | return err; | |
95 | ||
96 | ip_addr[mask_pos] = '\0'; | |
97 | } | |
98 | ||
99 | return -ENOENT; | |
100 | } | |
06283a64 JH |
101 | /* |
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 | */ | |
115 | static int | |
116 | do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
117 | { | |
118 | char *pxefile_addr_str; | |
834c9384 | 119 | unsigned long pxefile_addr_r; |
39f98553 | 120 | int err, i = 0; |
06283a64 | 121 | |
669df7e4 RH |
122 | do_getfile = do_get_tftp; |
123 | ||
06283a64 | 124 | if (argc != 1) |
4c12eeb8 | 125 | return CMD_RET_USAGE; |
06283a64 | 126 | |
06283a64 JH |
127 | 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, | |
31839dc2 | 133 | (unsigned long *)&pxefile_addr_r); |
06283a64 JH |
134 | if (err < 0) |
135 | return 1; | |
136 | ||
137 | /* | |
138 | * Keep trying paths until we successfully get a file we're looking | |
139 | * for. | |
140 | */ | |
4a0bd102 SS |
141 | 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) { | |
06283a64 JH |
144 | printf("Config file found\n"); |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
39f98553 | 149 | while (pxe_default_paths[i]) { |
0e3f3f8a | 150 | if (get_pxelinux_path(cmdtp, pxe_default_paths[i], |
4a0bd102 | 151 | pxefile_addr_r) > 0) { |
39f98553 RH |
152 | printf("Config file found\n"); |
153 | return 0; | |
154 | } | |
155 | i++; | |
156 | } | |
157 | ||
06283a64 JH |
158 | printf("Config file not found\n"); |
159 | ||
160 | return 1; | |
161 | } | |
06283a64 | 162 | |
06283a64 JH |
163 | /* |
164 | * Boots a system using a pxe file | |
165 | * | |
166 | * Returns 0 on success, 1 on error. | |
167 | */ | |
168 | static int | |
169 | do_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 | ||
669df7e4 RH |
175 | do_getfile = do_get_tftp; |
176 | ||
06283a64 JH |
177 | 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 { | |
4c12eeb8 | 185 | return CMD_RET_USAGE; |
06283a64 JH |
186 | } |
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 | ||
4a0bd102 | 193 | cfg = parse_pxefile(cmdtp, pxefile_addr_r); |
06283a64 | 194 | |
31839dc2 | 195 | if (!cfg) { |
06283a64 JH |
196 | printf("Error parsing config file\n"); |
197 | return 1; | |
198 | } | |
199 | ||
d7884e04 | 200 | handle_pxe_menu(cmdtp, cfg); |
06283a64 JH |
201 | |
202 | destroy_pxe_menu(cfg); | |
203 | ||
1411157d | 204 | copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name)); |
ded2e20e | 205 | |
06283a64 JH |
206 | return 0; |
207 | } | |
208 | ||
209 | static 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 | ||
0e350f81 | 214 | static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
06283a64 JH |
215 | { |
216 | cmd_tbl_t *cp; | |
217 | ||
218 | if (argc < 2) | |
4c12eeb8 | 219 | return CMD_RET_USAGE; |
06283a64 | 220 | |
e5a9a407 RH |
221 | is_pxe = true; |
222 | ||
06283a64 JH |
223 | /* 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 | ||
4c12eeb8 | 232 | return CMD_RET_USAGE; |
06283a64 JH |
233 | } |
234 | ||
31839dc2 PC |
235 | U_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" | |
06283a64 | 239 | ); |
b81fdb04 | 240 | #endif |