xref: /OK3568_Linux_fs/u-boot/cmd/pxe.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2010-2011 Calxeda, Inc.
3*4882a593Smuzhiyun  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <command.h>
10*4882a593Smuzhiyun #include <malloc.h>
11*4882a593Smuzhiyun #include <mapmem.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun #include <linux/ctype.h>
14*4882a593Smuzhiyun #include <errno.h>
15*4882a593Smuzhiyun #include <linux/list.h>
16*4882a593Smuzhiyun #include <fs.h>
17*4882a593Smuzhiyun #include <asm/io.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "menu.h"
20*4882a593Smuzhiyun #include "cli.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define MAX_TFTP_PATH_LEN 127
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun const char *pxe_default_paths[] = {
25*4882a593Smuzhiyun #ifdef CONFIG_SYS_SOC
26*4882a593Smuzhiyun 	"default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
27*4882a593Smuzhiyun #endif
28*4882a593Smuzhiyun 	"default-" CONFIG_SYS_ARCH,
29*4882a593Smuzhiyun 	"default",
30*4882a593Smuzhiyun 	NULL
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static bool is_pxe;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * Like env_get, but prints an error if envvar isn't defined in the
37*4882a593Smuzhiyun  * environment.  It always returns what env_get does, so it can be used in
38*4882a593Smuzhiyun  * place of env_get without changing error handling otherwise.
39*4882a593Smuzhiyun  */
from_env(const char * envvar)40*4882a593Smuzhiyun static char *from_env(const char *envvar)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	char *ret;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	ret = env_get(envvar);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	if (!ret)
47*4882a593Smuzhiyun 		printf("missing environment variable: %s\n", envvar);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return ret;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #ifdef CONFIG_CMD_NET
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * Convert an ethaddr from the environment to the format used by pxelinux
55*4882a593Smuzhiyun  * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
56*4882a593Smuzhiyun  * the beginning of the ethernet address to indicate a hardware type of
57*4882a593Smuzhiyun  * Ethernet. Also converts uppercase hex characters into lowercase, to match
58*4882a593Smuzhiyun  * pxelinux's behavior.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
61*4882a593Smuzhiyun  * environment, or some other value < 0 on error.
62*4882a593Smuzhiyun  */
format_mac_pxe(char * outbuf,size_t outbuf_len)63*4882a593Smuzhiyun static int format_mac_pxe(char *outbuf, size_t outbuf_len)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	uchar ethaddr[6];
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if (outbuf_len < 21) {
68*4882a593Smuzhiyun 		printf("outbuf is too small (%zd < 21)\n", outbuf_len);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 		return -EINVAL;
71*4882a593Smuzhiyun 	}
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
74*4882a593Smuzhiyun 		return -ENOENT;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
77*4882a593Smuzhiyun 		ethaddr[0], ethaddr[1], ethaddr[2],
78*4882a593Smuzhiyun 		ethaddr[3], ethaddr[4], ethaddr[5]);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	return 1;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * Returns the directory the file specified in the bootfile env variable is
86*4882a593Smuzhiyun  * in. If bootfile isn't defined in the environment, return NULL, which should
87*4882a593Smuzhiyun  * be interpreted as "don't prepend anything to paths".
88*4882a593Smuzhiyun  */
get_bootfile_path(const char * file_path,char * bootfile_path,size_t bootfile_path_size)89*4882a593Smuzhiyun static int get_bootfile_path(const char *file_path, char *bootfile_path,
90*4882a593Smuzhiyun 			     size_t bootfile_path_size)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	char *bootfile, *last_slash;
93*4882a593Smuzhiyun 	size_t path_len = 0;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* Only syslinux allows absolute paths */
96*4882a593Smuzhiyun 	if (file_path[0] == '/' && !is_pxe)
97*4882a593Smuzhiyun 		goto ret;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	bootfile = from_env("bootfile");
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (!bootfile)
102*4882a593Smuzhiyun 		goto ret;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	last_slash = strrchr(bootfile, '/');
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	if (last_slash == NULL)
107*4882a593Smuzhiyun 		goto ret;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	path_len = (last_slash - bootfile) + 1;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (bootfile_path_size < path_len) {
112*4882a593Smuzhiyun 		printf("bootfile_path too small. (%zd < %zd)\n",
113*4882a593Smuzhiyun 				bootfile_path_size, path_len);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		return -1;
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	strncpy(bootfile_path, bootfile, path_len);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun  ret:
121*4882a593Smuzhiyun 	bootfile_path[path_len] = '\0';
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	return 1;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #ifdef CONFIG_CMD_NET
do_get_tftp(cmd_tbl_t * cmdtp,const char * file_path,char * file_addr)129*4882a593Smuzhiyun static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	tftp_argv[1] = file_addr;
134*4882a593Smuzhiyun 	tftp_argv[2] = (void *)file_path;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	if (do_tftpb(cmdtp, 0, 3, tftp_argv))
137*4882a593Smuzhiyun 		return -ENOENT;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return 1;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun #endif
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun static char *fs_argv[5];
144*4882a593Smuzhiyun 
do_get_ext2(cmd_tbl_t * cmdtp,const char * file_path,char * file_addr)145*4882a593Smuzhiyun static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun #ifdef CONFIG_CMD_EXT2
148*4882a593Smuzhiyun 	fs_argv[0] = "ext2load";
149*4882a593Smuzhiyun 	fs_argv[3] = file_addr;
150*4882a593Smuzhiyun 	fs_argv[4] = (void *)file_path;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (!do_ext2load(cmdtp, 0, 5, fs_argv))
153*4882a593Smuzhiyun 		return 1;
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun 	return -ENOENT;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
do_get_fat(cmd_tbl_t * cmdtp,const char * file_path,char * file_addr)158*4882a593Smuzhiyun static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun #ifdef CONFIG_CMD_FAT
161*4882a593Smuzhiyun 	fs_argv[0] = "fatload";
162*4882a593Smuzhiyun 	fs_argv[3] = file_addr;
163*4882a593Smuzhiyun 	fs_argv[4] = (void *)file_path;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
166*4882a593Smuzhiyun 		return 1;
167*4882a593Smuzhiyun #endif
168*4882a593Smuzhiyun 	return -ENOENT;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
do_get_any(cmd_tbl_t * cmdtp,const char * file_path,char * file_addr)171*4882a593Smuzhiyun static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun #ifdef CONFIG_CMD_FS_GENERIC
174*4882a593Smuzhiyun 	fs_argv[0] = "load";
175*4882a593Smuzhiyun 	fs_argv[3] = file_addr;
176*4882a593Smuzhiyun 	fs_argv[4] = (void *)file_path;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
179*4882a593Smuzhiyun 		return 1;
180*4882a593Smuzhiyun #endif
181*4882a593Smuzhiyun 	return -ENOENT;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun  * As in pxelinux, paths to files referenced from files we retrieve are
186*4882a593Smuzhiyun  * relative to the location of bootfile. get_relfile takes such a path and
187*4882a593Smuzhiyun  * joins it with the bootfile path to get the full path to the target file. If
188*4882a593Smuzhiyun  * the bootfile path is NULL, we use file_path as is.
189*4882a593Smuzhiyun  *
190*4882a593Smuzhiyun  * Returns 1 for success, or < 0 on error.
191*4882a593Smuzhiyun  */
get_relfile(cmd_tbl_t * cmdtp,const char * file_path,unsigned long file_addr)192*4882a593Smuzhiyun static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
193*4882a593Smuzhiyun 	unsigned long file_addr)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	size_t path_len;
196*4882a593Smuzhiyun 	char relfile[MAX_TFTP_PATH_LEN+1];
197*4882a593Smuzhiyun 	char addr_buf[18];
198*4882a593Smuzhiyun 	int err;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	err = get_bootfile_path(file_path, relfile, sizeof(relfile));
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (err < 0)
203*4882a593Smuzhiyun 		return err;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	path_len = strlen(file_path);
206*4882a593Smuzhiyun 	path_len += strlen(relfile);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	if (path_len > MAX_TFTP_PATH_LEN) {
209*4882a593Smuzhiyun 		printf("Base path too long (%s%s)\n",
210*4882a593Smuzhiyun 					relfile,
211*4882a593Smuzhiyun 					file_path);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		return -ENAMETOOLONG;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	strcat(relfile, file_path);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	printf("Retrieving file: %s\n", relfile);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	sprintf(addr_buf, "%lx", file_addr);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	return do_getfile(cmdtp, relfile, addr_buf);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun /*
226*4882a593Smuzhiyun  * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
227*4882a593Smuzhiyun  * 'bootfile' was specified in the environment, the path to bootfile will be
228*4882a593Smuzhiyun  * prepended to 'file_path' and the resulting path will be used.
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * Returns 1 on success, or < 0 for error.
231*4882a593Smuzhiyun  */
get_pxe_file(cmd_tbl_t * cmdtp,const char * file_path,unsigned long file_addr)232*4882a593Smuzhiyun static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
233*4882a593Smuzhiyun 	unsigned long file_addr)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	unsigned long config_file_size;
236*4882a593Smuzhiyun 	char *tftp_filesize;
237*4882a593Smuzhiyun 	int err;
238*4882a593Smuzhiyun 	char *buf;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	err = get_relfile(cmdtp, file_path, file_addr);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	if (err < 0)
243*4882a593Smuzhiyun 		return err;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/*
246*4882a593Smuzhiyun 	 * the file comes without a NUL byte at the end, so find out its size
247*4882a593Smuzhiyun 	 * and add the NUL byte.
248*4882a593Smuzhiyun 	 */
249*4882a593Smuzhiyun 	tftp_filesize = from_env("filesize");
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	if (!tftp_filesize)
252*4882a593Smuzhiyun 		return -ENOENT;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
255*4882a593Smuzhiyun 		return -EINVAL;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	buf = map_sysmem(file_addr + config_file_size, 1);
258*4882a593Smuzhiyun 	*buf = '\0';
259*4882a593Smuzhiyun 	unmap_sysmem(buf);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	return 1;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun #ifdef CONFIG_CMD_NET
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun #define PXELINUX_DIR "pxelinux.cfg/"
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun  * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
270*4882a593Smuzhiyun  * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
271*4882a593Smuzhiyun  * from the bootfile path, as described above.
272*4882a593Smuzhiyun  *
273*4882a593Smuzhiyun  * Returns 1 on success or < 0 on error.
274*4882a593Smuzhiyun  */
get_pxelinux_path(cmd_tbl_t * cmdtp,const char * file,unsigned long pxefile_addr_r)275*4882a593Smuzhiyun static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
276*4882a593Smuzhiyun 	unsigned long pxefile_addr_r)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	size_t base_len = strlen(PXELINUX_DIR);
279*4882a593Smuzhiyun 	char path[MAX_TFTP_PATH_LEN+1];
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
282*4882a593Smuzhiyun 		printf("path (%s%s) too long, skipping\n",
283*4882a593Smuzhiyun 				PXELINUX_DIR, file);
284*4882a593Smuzhiyun 		return -ENAMETOOLONG;
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	sprintf(path, PXELINUX_DIR "%s", file);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	return get_pxe_file(cmdtp, path, pxefile_addr_r);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun /*
293*4882a593Smuzhiyun  * Looks for a pxe file with a name based on the pxeuuid environment variable.
294*4882a593Smuzhiyun  *
295*4882a593Smuzhiyun  * Returns 1 on success or < 0 on error.
296*4882a593Smuzhiyun  */
pxe_uuid_path(cmd_tbl_t * cmdtp,unsigned long pxefile_addr_r)297*4882a593Smuzhiyun static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	char *uuid_str;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	uuid_str = from_env("pxeuuid");
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (!uuid_str)
304*4882a593Smuzhiyun 		return -ENOENT;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun /*
310*4882a593Smuzhiyun  * Looks for a pxe file with a name based on the 'ethaddr' environment
311*4882a593Smuzhiyun  * variable.
312*4882a593Smuzhiyun  *
313*4882a593Smuzhiyun  * Returns 1 on success or < 0 on error.
314*4882a593Smuzhiyun  */
pxe_mac_path(cmd_tbl_t * cmdtp,unsigned long pxefile_addr_r)315*4882a593Smuzhiyun static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	char mac_str[21];
318*4882a593Smuzhiyun 	int err;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	err = format_mac_pxe(mac_str, sizeof(mac_str));
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (err < 0)
323*4882a593Smuzhiyun 		return err;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /*
329*4882a593Smuzhiyun  * Looks for pxe files with names based on our IP address. See pxelinux
330*4882a593Smuzhiyun  * documentation for details on what these file names look like.  We match
331*4882a593Smuzhiyun  * that exactly.
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * Returns 1 on success or < 0 on error.
334*4882a593Smuzhiyun  */
pxe_ipaddr_paths(cmd_tbl_t * cmdtp,unsigned long pxefile_addr_r)335*4882a593Smuzhiyun static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	char ip_addr[9];
338*4882a593Smuzhiyun 	int mask_pos, err;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	for (mask_pos = 7; mask_pos >= 0;  mask_pos--) {
343*4882a593Smuzhiyun 		err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 		if (err > 0)
346*4882a593Smuzhiyun 			return err;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 		ip_addr[mask_pos] = '\0';
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	return -ENOENT;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun /*
355*4882a593Smuzhiyun  * Entry point for the 'pxe get' command.
356*4882a593Smuzhiyun  * This Follows pxelinux's rules to download a config file from a tftp server.
357*4882a593Smuzhiyun  * The file is stored at the location given by the pxefile_addr_r environment
358*4882a593Smuzhiyun  * variable, which must be set.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * UUID comes from pxeuuid env variable, if defined
361*4882a593Smuzhiyun  * MAC addr comes from ethaddr env variable, if defined
362*4882a593Smuzhiyun  * IP
363*4882a593Smuzhiyun  *
364*4882a593Smuzhiyun  * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
365*4882a593Smuzhiyun  *
366*4882a593Smuzhiyun  * Returns 0 on success or 1 on error.
367*4882a593Smuzhiyun  */
368*4882a593Smuzhiyun static int
do_pxe_get(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])369*4882a593Smuzhiyun do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	char *pxefile_addr_str;
372*4882a593Smuzhiyun 	unsigned long pxefile_addr_r;
373*4882a593Smuzhiyun 	int err, i = 0;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	do_getfile = do_get_tftp;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	if (argc != 1)
378*4882a593Smuzhiyun 		return CMD_RET_USAGE;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	pxefile_addr_str = from_env("pxefile_addr_r");
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	if (!pxefile_addr_str)
383*4882a593Smuzhiyun 		return 1;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	err = strict_strtoul(pxefile_addr_str, 16,
386*4882a593Smuzhiyun 				(unsigned long *)&pxefile_addr_r);
387*4882a593Smuzhiyun 	if (err < 0)
388*4882a593Smuzhiyun 		return 1;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	/*
391*4882a593Smuzhiyun 	 * Keep trying paths until we successfully get a file we're looking
392*4882a593Smuzhiyun 	 * for.
393*4882a593Smuzhiyun 	 */
394*4882a593Smuzhiyun 	if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
395*4882a593Smuzhiyun 	    pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
396*4882a593Smuzhiyun 	    pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
397*4882a593Smuzhiyun 		printf("Config file found\n");
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 		return 0;
400*4882a593Smuzhiyun 	}
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	while (pxe_default_paths[i]) {
403*4882a593Smuzhiyun 		if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
404*4882a593Smuzhiyun 				      pxefile_addr_r) > 0) {
405*4882a593Smuzhiyun 			printf("Config file found\n");
406*4882a593Smuzhiyun 			return 0;
407*4882a593Smuzhiyun 		}
408*4882a593Smuzhiyun 		i++;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	printf("Config file not found\n");
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	return 1;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun #endif
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun /*
418*4882a593Smuzhiyun  * Wrapper to make it easier to store the file at file_path in the location
419*4882a593Smuzhiyun  * specified by envaddr_name. file_path will be joined to the bootfile path,
420*4882a593Smuzhiyun  * if any is specified.
421*4882a593Smuzhiyun  *
422*4882a593Smuzhiyun  * Returns 1 on success or < 0 on error.
423*4882a593Smuzhiyun  */
get_relfile_envaddr(cmd_tbl_t * cmdtp,const char * file_path,const char * envaddr_name)424*4882a593Smuzhiyun static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	unsigned long file_addr;
427*4882a593Smuzhiyun 	char *envaddr;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	envaddr = from_env(envaddr_name);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	if (!envaddr)
432*4882a593Smuzhiyun 		return -ENOENT;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	if (strict_strtoul(envaddr, 16, &file_addr) < 0)
435*4882a593Smuzhiyun 		return -EINVAL;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	return get_relfile(cmdtp, file_path, file_addr);
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /*
441*4882a593Smuzhiyun  * A note on the pxe file parser.
442*4882a593Smuzhiyun  *
443*4882a593Smuzhiyun  * We're parsing files that use syslinux grammar, which has a few quirks.
444*4882a593Smuzhiyun  * String literals must be recognized based on context - there is no
445*4882a593Smuzhiyun  * quoting or escaping support. There's also nothing to explicitly indicate
446*4882a593Smuzhiyun  * when a label section completes. We deal with that by ending a label
447*4882a593Smuzhiyun  * section whenever we see a line that doesn't include.
448*4882a593Smuzhiyun  *
449*4882a593Smuzhiyun  * As with the syslinux family, this same file format could be reused in the
450*4882a593Smuzhiyun  * future for non pxe purposes. The only action it takes during parsing that
451*4882a593Smuzhiyun  * would throw this off is handling of include files. It assumes we're using
452*4882a593Smuzhiyun  * pxe, and does a tftp download of a file listed as an include file in the
453*4882a593Smuzhiyun  * middle of the parsing operation. That could be handled by refactoring it to
454*4882a593Smuzhiyun  * take a 'include file getter' function.
455*4882a593Smuzhiyun  */
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun /*
458*4882a593Smuzhiyun  * Describes a single label given in a pxe file.
459*4882a593Smuzhiyun  *
460*4882a593Smuzhiyun  * Create these with the 'label_create' function given below.
461*4882a593Smuzhiyun  *
462*4882a593Smuzhiyun  * name - the name of the menu as given on the 'menu label' line.
463*4882a593Smuzhiyun  * kernel - the path to the kernel file to use for this label.
464*4882a593Smuzhiyun  * append - kernel command line to use when booting this label
465*4882a593Smuzhiyun  * initrd - path to the initrd to use for this label.
466*4882a593Smuzhiyun  * attempted - 0 if we haven't tried to boot this label, 1 if we have.
467*4882a593Smuzhiyun  * localboot - 1 if this label specified 'localboot', 0 otherwise.
468*4882a593Smuzhiyun  * list - lets these form a list, which a pxe_menu struct will hold.
469*4882a593Smuzhiyun  */
470*4882a593Smuzhiyun struct pxe_label {
471*4882a593Smuzhiyun 	char num[4];
472*4882a593Smuzhiyun 	char *name;
473*4882a593Smuzhiyun 	char *menu;
474*4882a593Smuzhiyun 	char *kernel;
475*4882a593Smuzhiyun 	char *append;
476*4882a593Smuzhiyun 	char *initrd;
477*4882a593Smuzhiyun 	char *fdt;
478*4882a593Smuzhiyun 	char *fdtdir;
479*4882a593Smuzhiyun 	int ipappend;
480*4882a593Smuzhiyun 	int attempted;
481*4882a593Smuzhiyun 	int localboot;
482*4882a593Smuzhiyun 	int localboot_val;
483*4882a593Smuzhiyun 	struct list_head list;
484*4882a593Smuzhiyun };
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun /*
487*4882a593Smuzhiyun  * Describes a pxe menu as given via pxe files.
488*4882a593Smuzhiyun  *
489*4882a593Smuzhiyun  * title - the name of the menu as given by a 'menu title' line.
490*4882a593Smuzhiyun  * default_label - the name of the default label, if any.
491*4882a593Smuzhiyun  * timeout - time in tenths of a second to wait for a user key-press before
492*4882a593Smuzhiyun  *           booting the default label.
493*4882a593Smuzhiyun  * prompt - if 0, don't prompt for a choice unless the timeout period is
494*4882a593Smuzhiyun  *          interrupted.  If 1, always prompt for a choice regardless of
495*4882a593Smuzhiyun  *          timeout.
496*4882a593Smuzhiyun  * labels - a list of labels defined for the menu.
497*4882a593Smuzhiyun  */
498*4882a593Smuzhiyun struct pxe_menu {
499*4882a593Smuzhiyun 	char *title;
500*4882a593Smuzhiyun 	char *default_label;
501*4882a593Smuzhiyun 	int timeout;
502*4882a593Smuzhiyun 	int prompt;
503*4882a593Smuzhiyun 	struct list_head labels;
504*4882a593Smuzhiyun };
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun /*
507*4882a593Smuzhiyun  * Allocates memory for and initializes a pxe_label. This uses malloc, so the
508*4882a593Smuzhiyun  * result must be free()'d to reclaim the memory.
509*4882a593Smuzhiyun  *
510*4882a593Smuzhiyun  * Returns NULL if malloc fails.
511*4882a593Smuzhiyun  */
label_create(void)512*4882a593Smuzhiyun static struct pxe_label *label_create(void)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	struct pxe_label *label;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	label = malloc(sizeof(struct pxe_label));
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	if (!label)
519*4882a593Smuzhiyun 		return NULL;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	memset(label, 0, sizeof(struct pxe_label));
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	return label;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun  * Free the memory used by a pxe_label, including that used by its name,
528*4882a593Smuzhiyun  * kernel, append and initrd members, if they're non NULL.
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  * So - be sure to only use dynamically allocated memory for the members of
531*4882a593Smuzhiyun  * the pxe_label struct, unless you want to clean it up first. These are
532*4882a593Smuzhiyun  * currently only created by the pxe file parsing code.
533*4882a593Smuzhiyun  */
label_destroy(struct pxe_label * label)534*4882a593Smuzhiyun static void label_destroy(struct pxe_label *label)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	if (label->name)
537*4882a593Smuzhiyun 		free(label->name);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	if (label->kernel)
540*4882a593Smuzhiyun 		free(label->kernel);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	if (label->append)
543*4882a593Smuzhiyun 		free(label->append);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (label->initrd)
546*4882a593Smuzhiyun 		free(label->initrd);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	if (label->fdt)
549*4882a593Smuzhiyun 		free(label->fdt);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	if (label->fdtdir)
552*4882a593Smuzhiyun 		free(label->fdtdir);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	free(label);
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /*
558*4882a593Smuzhiyun  * Print a label and its string members if they're defined.
559*4882a593Smuzhiyun  *
560*4882a593Smuzhiyun  * This is passed as a callback to the menu code for displaying each
561*4882a593Smuzhiyun  * menu entry.
562*4882a593Smuzhiyun  */
label_print(void * data)563*4882a593Smuzhiyun static void label_print(void *data)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun 	struct pxe_label *label = data;
566*4882a593Smuzhiyun 	const char *c = label->menu ? label->menu : label->name;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	printf("%s:\t%s\n", label->num, c);
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun /*
572*4882a593Smuzhiyun  * Boot a label that specified 'localboot'. This requires that the 'localcmd'
573*4882a593Smuzhiyun  * environment variable is defined. Its contents will be executed as U-Boot
574*4882a593Smuzhiyun  * command.  If the label specified an 'append' line, its contents will be
575*4882a593Smuzhiyun  * used to overwrite the contents of the 'bootargs' environment variable prior
576*4882a593Smuzhiyun  * to running 'localcmd'.
577*4882a593Smuzhiyun  *
578*4882a593Smuzhiyun  * Returns 1 on success or < 0 on error.
579*4882a593Smuzhiyun  */
label_localboot(struct pxe_label * label)580*4882a593Smuzhiyun static int label_localboot(struct pxe_label *label)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	char *localcmd;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	localcmd = from_env("localcmd");
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	if (!localcmd)
587*4882a593Smuzhiyun 		return -ENOENT;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	if (label->append) {
590*4882a593Smuzhiyun 		char bootargs[CONFIG_SYS_CBSIZE];
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 		cli_simple_process_macros(label->append, bootargs);
593*4882a593Smuzhiyun 		env_set("bootargs", bootargs);
594*4882a593Smuzhiyun 	}
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	debug("running: %s\n", localcmd);
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	return run_command_list(localcmd, strlen(localcmd), 0);
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun  * Boot according to the contents of a pxe_label.
603*4882a593Smuzhiyun  *
604*4882a593Smuzhiyun  * If we can't boot for any reason, we return.  A successful boot never
605*4882a593Smuzhiyun  * returns.
606*4882a593Smuzhiyun  *
607*4882a593Smuzhiyun  * The kernel will be stored in the location given by the 'kernel_addr_r'
608*4882a593Smuzhiyun  * environment variable.
609*4882a593Smuzhiyun  *
610*4882a593Smuzhiyun  * If the label specifies an initrd file, it will be stored in the location
611*4882a593Smuzhiyun  * given by the 'ramdisk_addr_r' environment variable.
612*4882a593Smuzhiyun  *
613*4882a593Smuzhiyun  * If the label specifies an 'append' line, its contents will overwrite that
614*4882a593Smuzhiyun  * of the 'bootargs' environment variable.
615*4882a593Smuzhiyun  */
label_boot(cmd_tbl_t * cmdtp,struct pxe_label * label)616*4882a593Smuzhiyun static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
619*4882a593Smuzhiyun 	char initrd_str[28];
620*4882a593Smuzhiyun 	char mac_str[29] = "";
621*4882a593Smuzhiyun 	char ip_str[68] = "";
622*4882a593Smuzhiyun 	int bootm_argc = 2;
623*4882a593Smuzhiyun 	int len = 0;
624*4882a593Smuzhiyun 	ulong kernel_addr;
625*4882a593Smuzhiyun 	void *buf;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	label_print(label);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	label->attempted = 1;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	if (label->localboot) {
632*4882a593Smuzhiyun 		if (label->localboot_val >= 0)
633*4882a593Smuzhiyun 			label_localboot(label);
634*4882a593Smuzhiyun 		return 0;
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	if (label->kernel == NULL) {
638*4882a593Smuzhiyun 		printf("No kernel given, skipping %s\n",
639*4882a593Smuzhiyun 				label->name);
640*4882a593Smuzhiyun 		return 1;
641*4882a593Smuzhiyun 	}
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	if (label->initrd) {
644*4882a593Smuzhiyun 		if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
645*4882a593Smuzhiyun 			printf("Skipping %s for failure retrieving initrd\n",
646*4882a593Smuzhiyun 					label->name);
647*4882a593Smuzhiyun 			return 1;
648*4882a593Smuzhiyun 		}
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 		bootm_argv[2] = initrd_str;
651*4882a593Smuzhiyun 		strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
652*4882a593Smuzhiyun 		strcat(bootm_argv[2], ":");
653*4882a593Smuzhiyun 		strncat(bootm_argv[2], env_get("filesize"), 9);
654*4882a593Smuzhiyun 	}
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
657*4882a593Smuzhiyun 		printf("Skipping %s for failure retrieving kernel\n",
658*4882a593Smuzhiyun 				label->name);
659*4882a593Smuzhiyun 		return 1;
660*4882a593Smuzhiyun 	}
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	if (label->ipappend & 0x1) {
663*4882a593Smuzhiyun 		sprintf(ip_str, " ip=%s:%s:%s:%s",
664*4882a593Smuzhiyun 			env_get("ipaddr"), env_get("serverip"),
665*4882a593Smuzhiyun 			env_get("gatewayip"), env_get("netmask"));
666*4882a593Smuzhiyun 	}
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun #ifdef CONFIG_CMD_NET
669*4882a593Smuzhiyun 	if (label->ipappend & 0x2) {
670*4882a593Smuzhiyun 		int err;
671*4882a593Smuzhiyun 		strcpy(mac_str, " BOOTIF=");
672*4882a593Smuzhiyun 		err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
673*4882a593Smuzhiyun 		if (err < 0)
674*4882a593Smuzhiyun 			mac_str[0] = '\0';
675*4882a593Smuzhiyun 	}
676*4882a593Smuzhiyun #endif
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	if ((label->ipappend & 0x3) || label->append) {
679*4882a593Smuzhiyun 		char bootargs[CONFIG_SYS_CBSIZE] = "";
680*4882a593Smuzhiyun 		char finalbootargs[CONFIG_SYS_CBSIZE];
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 		if (strlen(label->append ?: "") +
683*4882a593Smuzhiyun 		    strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
684*4882a593Smuzhiyun 			printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
685*4882a593Smuzhiyun 			       strlen(label->append ?: ""),
686*4882a593Smuzhiyun 			       strlen(ip_str), strlen(mac_str),
687*4882a593Smuzhiyun 			       sizeof(bootargs));
688*4882a593Smuzhiyun 			return 1;
689*4882a593Smuzhiyun 		} else {
690*4882a593Smuzhiyun 			if (label->append)
691*4882a593Smuzhiyun 				strncpy(bootargs, label->append,
692*4882a593Smuzhiyun 					sizeof(bootargs));
693*4882a593Smuzhiyun 			strcat(bootargs, ip_str);
694*4882a593Smuzhiyun 			strcat(bootargs, mac_str);
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 			cli_simple_process_macros(bootargs, finalbootargs);
697*4882a593Smuzhiyun 			env_set("bootargs", finalbootargs);
698*4882a593Smuzhiyun 			printf("append: %s\n", finalbootargs);
699*4882a593Smuzhiyun 		}
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	bootm_argv[1] = env_get("kernel_addr_r");
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	/*
705*4882a593Smuzhiyun 	 * fdt usage is optional:
706*4882a593Smuzhiyun 	 * It handles the following scenarios. All scenarios are exclusive
707*4882a593Smuzhiyun 	 *
708*4882a593Smuzhiyun 	 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
709*4882a593Smuzhiyun 	 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
710*4882a593Smuzhiyun 	 * and adjust argc appropriately.
711*4882a593Smuzhiyun 	 *
712*4882a593Smuzhiyun 	 * Scenario 2: If there is an fdt_addr specified, pass it along to
713*4882a593Smuzhiyun 	 * bootm, and adjust argc appropriately.
714*4882a593Smuzhiyun 	 *
715*4882a593Smuzhiyun 	 * Scenario 3: fdt blob is not available.
716*4882a593Smuzhiyun 	 */
717*4882a593Smuzhiyun 	bootm_argv[3] = env_get("fdt_addr_r");
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	/* if fdt label is defined then get fdt from server */
720*4882a593Smuzhiyun 	if (bootm_argv[3]) {
721*4882a593Smuzhiyun 		char *fdtfile = NULL;
722*4882a593Smuzhiyun 		char *fdtfilefree = NULL;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 		if (label->fdt) {
725*4882a593Smuzhiyun 			fdtfile = label->fdt;
726*4882a593Smuzhiyun 		} else if (label->fdtdir) {
727*4882a593Smuzhiyun 			char *f1, *f2, *f3, *f4, *slash;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 			f1 = env_get("fdtfile");
730*4882a593Smuzhiyun 			if (f1) {
731*4882a593Smuzhiyun 				f2 = "";
732*4882a593Smuzhiyun 				f3 = "";
733*4882a593Smuzhiyun 				f4 = "";
734*4882a593Smuzhiyun 			} else {
735*4882a593Smuzhiyun 				/*
736*4882a593Smuzhiyun 				 * For complex cases where this code doesn't
737*4882a593Smuzhiyun 				 * generate the correct filename, the board
738*4882a593Smuzhiyun 				 * code should set $fdtfile during early boot,
739*4882a593Smuzhiyun 				 * or the boot scripts should set $fdtfile
740*4882a593Smuzhiyun 				 * before invoking "pxe" or "sysboot".
741*4882a593Smuzhiyun 				 */
742*4882a593Smuzhiyun 				f1 = env_get("soc");
743*4882a593Smuzhiyun 				f2 = "-";
744*4882a593Smuzhiyun 				f3 = env_get("board");
745*4882a593Smuzhiyun 				f4 = ".dtb";
746*4882a593Smuzhiyun 			}
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 			len = strlen(label->fdtdir);
749*4882a593Smuzhiyun 			if (!len)
750*4882a593Smuzhiyun 				slash = "./";
751*4882a593Smuzhiyun 			else if (label->fdtdir[len - 1] != '/')
752*4882a593Smuzhiyun 				slash = "/";
753*4882a593Smuzhiyun 			else
754*4882a593Smuzhiyun 				slash = "";
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 			len = strlen(label->fdtdir) + strlen(slash) +
757*4882a593Smuzhiyun 				strlen(f1) + strlen(f2) + strlen(f3) +
758*4882a593Smuzhiyun 				strlen(f4) + 1;
759*4882a593Smuzhiyun 			fdtfilefree = malloc(len);
760*4882a593Smuzhiyun 			if (!fdtfilefree) {
761*4882a593Smuzhiyun 				printf("malloc fail (FDT filename)\n");
762*4882a593Smuzhiyun 				return 1;
763*4882a593Smuzhiyun 			}
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 			snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
766*4882a593Smuzhiyun 				 label->fdtdir, slash, f1, f2, f3, f4);
767*4882a593Smuzhiyun 			fdtfile = fdtfilefree;
768*4882a593Smuzhiyun 		}
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 		if (fdtfile) {
771*4882a593Smuzhiyun 			int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
772*4882a593Smuzhiyun 			free(fdtfilefree);
773*4882a593Smuzhiyun 			if (err < 0) {
774*4882a593Smuzhiyun 				printf("Skipping %s for failure retrieving fdt\n",
775*4882a593Smuzhiyun 						label->name);
776*4882a593Smuzhiyun 				return 1;
777*4882a593Smuzhiyun 			}
778*4882a593Smuzhiyun 		} else {
779*4882a593Smuzhiyun 			bootm_argv[3] = NULL;
780*4882a593Smuzhiyun 		}
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	if (!bootm_argv[3])
784*4882a593Smuzhiyun 		bootm_argv[3] = env_get("fdt_addr");
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	if (bootm_argv[3]) {
787*4882a593Smuzhiyun 		if (!bootm_argv[2])
788*4882a593Smuzhiyun 			bootm_argv[2] = "-";
789*4882a593Smuzhiyun 		bootm_argc = 4;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
793*4882a593Smuzhiyun 	buf = map_sysmem(kernel_addr, 0);
794*4882a593Smuzhiyun 	/* Try bootm for legacy and FIT format image */
795*4882a593Smuzhiyun 	if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
796*4882a593Smuzhiyun 		do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
797*4882a593Smuzhiyun #ifdef CONFIG_CMD_BOOTI
798*4882a593Smuzhiyun 	/* Try booting an AArch64 Linux kernel image */
799*4882a593Smuzhiyun 	else
800*4882a593Smuzhiyun 		do_booti(cmdtp, 0, bootm_argc, bootm_argv);
801*4882a593Smuzhiyun #elif defined(CONFIG_CMD_BOOTZ)
802*4882a593Smuzhiyun 	/* Try booting a Image */
803*4882a593Smuzhiyun 	else
804*4882a593Smuzhiyun 		do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
805*4882a593Smuzhiyun #endif
806*4882a593Smuzhiyun 	unmap_sysmem(buf);
807*4882a593Smuzhiyun 	return 1;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun /*
811*4882a593Smuzhiyun  * Tokens for the pxe file parser.
812*4882a593Smuzhiyun  */
813*4882a593Smuzhiyun enum token_type {
814*4882a593Smuzhiyun 	T_EOL,
815*4882a593Smuzhiyun 	T_STRING,
816*4882a593Smuzhiyun 	T_EOF,
817*4882a593Smuzhiyun 	T_MENU,
818*4882a593Smuzhiyun 	T_TITLE,
819*4882a593Smuzhiyun 	T_TIMEOUT,
820*4882a593Smuzhiyun 	T_LABEL,
821*4882a593Smuzhiyun 	T_KERNEL,
822*4882a593Smuzhiyun 	T_LINUX,
823*4882a593Smuzhiyun 	T_APPEND,
824*4882a593Smuzhiyun 	T_INITRD,
825*4882a593Smuzhiyun 	T_LOCALBOOT,
826*4882a593Smuzhiyun 	T_DEFAULT,
827*4882a593Smuzhiyun 	T_PROMPT,
828*4882a593Smuzhiyun 	T_INCLUDE,
829*4882a593Smuzhiyun 	T_FDT,
830*4882a593Smuzhiyun 	T_FDTDIR,
831*4882a593Smuzhiyun 	T_ONTIMEOUT,
832*4882a593Smuzhiyun 	T_IPAPPEND,
833*4882a593Smuzhiyun 	T_INVALID
834*4882a593Smuzhiyun };
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun /*
837*4882a593Smuzhiyun  * A token - given by a value and a type.
838*4882a593Smuzhiyun  */
839*4882a593Smuzhiyun struct token {
840*4882a593Smuzhiyun 	char *val;
841*4882a593Smuzhiyun 	enum token_type type;
842*4882a593Smuzhiyun };
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun /*
845*4882a593Smuzhiyun  * Keywords recognized.
846*4882a593Smuzhiyun  */
847*4882a593Smuzhiyun static const struct token keywords[] = {
848*4882a593Smuzhiyun 	{"menu", T_MENU},
849*4882a593Smuzhiyun 	{"title", T_TITLE},
850*4882a593Smuzhiyun 	{"timeout", T_TIMEOUT},
851*4882a593Smuzhiyun 	{"default", T_DEFAULT},
852*4882a593Smuzhiyun 	{"prompt", T_PROMPT},
853*4882a593Smuzhiyun 	{"label", T_LABEL},
854*4882a593Smuzhiyun 	{"kernel", T_KERNEL},
855*4882a593Smuzhiyun 	{"linux", T_LINUX},
856*4882a593Smuzhiyun 	{"localboot", T_LOCALBOOT},
857*4882a593Smuzhiyun 	{"append", T_APPEND},
858*4882a593Smuzhiyun 	{"initrd", T_INITRD},
859*4882a593Smuzhiyun 	{"include", T_INCLUDE},
860*4882a593Smuzhiyun 	{"devicetree", T_FDT},
861*4882a593Smuzhiyun 	{"fdt", T_FDT},
862*4882a593Smuzhiyun 	{"devicetreedir", T_FDTDIR},
863*4882a593Smuzhiyun 	{"fdtdir", T_FDTDIR},
864*4882a593Smuzhiyun 	{"ontimeout", T_ONTIMEOUT,},
865*4882a593Smuzhiyun 	{"ipappend", T_IPAPPEND,},
866*4882a593Smuzhiyun 	{NULL, T_INVALID}
867*4882a593Smuzhiyun };
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun /*
870*4882a593Smuzhiyun  * Since pxe(linux) files don't have a token to identify the start of a
871*4882a593Smuzhiyun  * literal, we have to keep track of when we're in a state where a literal is
872*4882a593Smuzhiyun  * expected vs when we're in a state a keyword is expected.
873*4882a593Smuzhiyun  */
874*4882a593Smuzhiyun enum lex_state {
875*4882a593Smuzhiyun 	L_NORMAL = 0,
876*4882a593Smuzhiyun 	L_KEYWORD,
877*4882a593Smuzhiyun 	L_SLITERAL
878*4882a593Smuzhiyun };
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun /*
881*4882a593Smuzhiyun  * get_string retrieves a string from *p and stores it as a token in
882*4882a593Smuzhiyun  * *t.
883*4882a593Smuzhiyun  *
884*4882a593Smuzhiyun  * get_string used for scanning both string literals and keywords.
885*4882a593Smuzhiyun  *
886*4882a593Smuzhiyun  * Characters from *p are copied into t-val until a character equal to
887*4882a593Smuzhiyun  * delim is found, or a NUL byte is reached. If delim has the special value of
888*4882a593Smuzhiyun  * ' ', any whitespace character will be used as a delimiter.
889*4882a593Smuzhiyun  *
890*4882a593Smuzhiyun  * If lower is unequal to 0, uppercase characters will be converted to
891*4882a593Smuzhiyun  * lowercase in the result. This is useful to make keywords case
892*4882a593Smuzhiyun  * insensitive.
893*4882a593Smuzhiyun  *
894*4882a593Smuzhiyun  * The location of *p is updated to point to the first character after the end
895*4882a593Smuzhiyun  * of the token - the ending delimiter.
896*4882a593Smuzhiyun  *
897*4882a593Smuzhiyun  * On success, the new value of t->val is returned. Memory for t->val is
898*4882a593Smuzhiyun  * allocated using malloc and must be free()'d to reclaim it.  If insufficient
899*4882a593Smuzhiyun  * memory is available, NULL is returned.
900*4882a593Smuzhiyun  */
get_string(char ** p,struct token * t,char delim,int lower)901*4882a593Smuzhiyun static char *get_string(char **p, struct token *t, char delim, int lower)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun 	char *b, *e;
904*4882a593Smuzhiyun 	size_t len, i;
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	/*
907*4882a593Smuzhiyun 	 * b and e both start at the beginning of the input stream.
908*4882a593Smuzhiyun 	 *
909*4882a593Smuzhiyun 	 * e is incremented until we find the ending delimiter, or a NUL byte
910*4882a593Smuzhiyun 	 * is reached. Then, we take e - b to find the length of the token.
911*4882a593Smuzhiyun 	 */
912*4882a593Smuzhiyun 	b = e = *p;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	while (*e) {
915*4882a593Smuzhiyun 		if ((delim == ' ' && isspace(*e)) || delim == *e)
916*4882a593Smuzhiyun 			break;
917*4882a593Smuzhiyun 		e++;
918*4882a593Smuzhiyun 	}
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	len = e - b;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	/*
923*4882a593Smuzhiyun 	 * Allocate memory to hold the string, and copy it in, converting
924*4882a593Smuzhiyun 	 * characters to lowercase if lower is != 0.
925*4882a593Smuzhiyun 	 */
926*4882a593Smuzhiyun 	t->val = malloc(len + 1);
927*4882a593Smuzhiyun 	if (!t->val)
928*4882a593Smuzhiyun 		return NULL;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	for (i = 0; i < len; i++, b++) {
931*4882a593Smuzhiyun 		if (lower)
932*4882a593Smuzhiyun 			t->val[i] = tolower(*b);
933*4882a593Smuzhiyun 		else
934*4882a593Smuzhiyun 			t->val[i] = *b;
935*4882a593Smuzhiyun 	}
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	t->val[len] = '\0';
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	/*
940*4882a593Smuzhiyun 	 * Update *p so the caller knows where to continue scanning.
941*4882a593Smuzhiyun 	 */
942*4882a593Smuzhiyun 	*p = e;
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	t->type = T_STRING;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	return t->val;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun /*
950*4882a593Smuzhiyun  * Populate a keyword token with a type and value.
951*4882a593Smuzhiyun  */
get_keyword(struct token * t)952*4882a593Smuzhiyun static void get_keyword(struct token *t)
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun 	int i;
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	for (i = 0; keywords[i].val; i++) {
957*4882a593Smuzhiyun 		if (!strcmp(t->val, keywords[i].val)) {
958*4882a593Smuzhiyun 			t->type = keywords[i].type;
959*4882a593Smuzhiyun 			break;
960*4882a593Smuzhiyun 		}
961*4882a593Smuzhiyun 	}
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun /*
965*4882a593Smuzhiyun  * Get the next token.  We have to keep track of which state we're in to know
966*4882a593Smuzhiyun  * if we're looking to get a string literal or a keyword.
967*4882a593Smuzhiyun  *
968*4882a593Smuzhiyun  * *p is updated to point at the first character after the current token.
969*4882a593Smuzhiyun  */
get_token(char ** p,struct token * t,enum lex_state state)970*4882a593Smuzhiyun static void get_token(char **p, struct token *t, enum lex_state state)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun 	char *c = *p;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	t->type = T_INVALID;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	/* eat non EOL whitespace */
977*4882a593Smuzhiyun 	while (isblank(*c))
978*4882a593Smuzhiyun 		c++;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	/*
981*4882a593Smuzhiyun 	 * eat comments. note that string literals can't begin with #, but
982*4882a593Smuzhiyun 	 * can contain a # after their first character.
983*4882a593Smuzhiyun 	 */
984*4882a593Smuzhiyun 	if (*c == '#') {
985*4882a593Smuzhiyun 		while (*c && *c != '\n')
986*4882a593Smuzhiyun 			c++;
987*4882a593Smuzhiyun 	}
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	if (*c == '\n') {
990*4882a593Smuzhiyun 		t->type = T_EOL;
991*4882a593Smuzhiyun 		c++;
992*4882a593Smuzhiyun 	} else if (*c == '\0') {
993*4882a593Smuzhiyun 		t->type = T_EOF;
994*4882a593Smuzhiyun 		c++;
995*4882a593Smuzhiyun 	} else if (state == L_SLITERAL) {
996*4882a593Smuzhiyun 		get_string(&c, t, '\n', 0);
997*4882a593Smuzhiyun 	} else if (state == L_KEYWORD) {
998*4882a593Smuzhiyun 		/*
999*4882a593Smuzhiyun 		 * when we expect a keyword, we first get the next string
1000*4882a593Smuzhiyun 		 * token delimited by whitespace, and then check if it
1001*4882a593Smuzhiyun 		 * matches a keyword in our keyword list. if it does, it's
1002*4882a593Smuzhiyun 		 * converted to a keyword token of the appropriate type, and
1003*4882a593Smuzhiyun 		 * if not, it remains a string token.
1004*4882a593Smuzhiyun 		 */
1005*4882a593Smuzhiyun 		get_string(&c, t, ' ', 1);
1006*4882a593Smuzhiyun 		get_keyword(t);
1007*4882a593Smuzhiyun 	}
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	*p = c;
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun /*
1013*4882a593Smuzhiyun  * Increment *c until we get to the end of the current line, or EOF.
1014*4882a593Smuzhiyun  */
eol_or_eof(char ** c)1015*4882a593Smuzhiyun static void eol_or_eof(char **c)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun 	while (**c && **c != '\n')
1018*4882a593Smuzhiyun 		(*c)++;
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun /*
1022*4882a593Smuzhiyun  * All of these parse_* functions share some common behavior.
1023*4882a593Smuzhiyun  *
1024*4882a593Smuzhiyun  * They finish with *c pointing after the token they parse, and return 1 on
1025*4882a593Smuzhiyun  * success, or < 0 on error.
1026*4882a593Smuzhiyun  */
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun /*
1029*4882a593Smuzhiyun  * Parse a string literal and store a pointer it at *dst. String literals
1030*4882a593Smuzhiyun  * terminate at the end of the line.
1031*4882a593Smuzhiyun  */
parse_sliteral(char ** c,char ** dst)1032*4882a593Smuzhiyun static int parse_sliteral(char **c, char **dst)
1033*4882a593Smuzhiyun {
1034*4882a593Smuzhiyun 	struct token t;
1035*4882a593Smuzhiyun 	char *s = *c;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	get_token(c, &t, L_SLITERAL);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	if (t.type != T_STRING) {
1040*4882a593Smuzhiyun 		printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1041*4882a593Smuzhiyun 		return -EINVAL;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	*dst = t.val;
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	return 1;
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun /*
1050*4882a593Smuzhiyun  * Parse a base 10 (unsigned) integer and store it at *dst.
1051*4882a593Smuzhiyun  */
parse_integer(char ** c,int * dst)1052*4882a593Smuzhiyun static int parse_integer(char **c, int *dst)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun 	struct token t;
1055*4882a593Smuzhiyun 	char *s = *c;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	get_token(c, &t, L_SLITERAL);
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	if (t.type != T_STRING) {
1060*4882a593Smuzhiyun 		printf("Expected string: %.*s\n", (int)(*c - s), s);
1061*4882a593Smuzhiyun 		return -EINVAL;
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	*dst = simple_strtol(t.val, NULL, 10);
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	free(t.val);
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	return 1;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1072*4882a593Smuzhiyun 	struct pxe_menu *cfg, int nest_level);
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun /*
1075*4882a593Smuzhiyun  * Parse an include statement, and retrieve and parse the file it mentions.
1076*4882a593Smuzhiyun  *
1077*4882a593Smuzhiyun  * base should point to a location where it's safe to store the file, and
1078*4882a593Smuzhiyun  * nest_level should indicate how many nested includes have occurred. For this
1079*4882a593Smuzhiyun  * include, nest_level has already been incremented and doesn't need to be
1080*4882a593Smuzhiyun  * incremented here.
1081*4882a593Smuzhiyun  */
handle_include(cmd_tbl_t * cmdtp,char ** c,unsigned long base,struct pxe_menu * cfg,int nest_level)1082*4882a593Smuzhiyun static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
1083*4882a593Smuzhiyun 				struct pxe_menu *cfg, int nest_level)
1084*4882a593Smuzhiyun {
1085*4882a593Smuzhiyun 	char *include_path;
1086*4882a593Smuzhiyun 	char *s = *c;
1087*4882a593Smuzhiyun 	int err;
1088*4882a593Smuzhiyun 	char *buf;
1089*4882a593Smuzhiyun 	int ret;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	err = parse_sliteral(c, &include_path);
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	if (err < 0) {
1094*4882a593Smuzhiyun 		printf("Expected include path: %.*s\n",
1095*4882a593Smuzhiyun 				 (int)(*c - s), s);
1096*4882a593Smuzhiyun 		return err;
1097*4882a593Smuzhiyun 	}
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	err = get_pxe_file(cmdtp, include_path, base);
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	if (err < 0) {
1102*4882a593Smuzhiyun 		printf("Couldn't retrieve %s\n", include_path);
1103*4882a593Smuzhiyun 		return err;
1104*4882a593Smuzhiyun 	}
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	buf = map_sysmem(base, 0);
1107*4882a593Smuzhiyun 	ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
1108*4882a593Smuzhiyun 	unmap_sysmem(buf);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	return ret;
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun /*
1114*4882a593Smuzhiyun  * Parse lines that begin with 'menu'.
1115*4882a593Smuzhiyun  *
1116*4882a593Smuzhiyun  * base and nest are provided to handle the 'menu include' case.
1117*4882a593Smuzhiyun  *
1118*4882a593Smuzhiyun  * base should point to a location where it's safe to store the included file.
1119*4882a593Smuzhiyun  *
1120*4882a593Smuzhiyun  * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1121*4882a593Smuzhiyun  * a file it includes, 3 when parsing a file included by that file, and so on.
1122*4882a593Smuzhiyun  */
parse_menu(cmd_tbl_t * cmdtp,char ** c,struct pxe_menu * cfg,unsigned long base,int nest_level)1123*4882a593Smuzhiyun static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
1124*4882a593Smuzhiyun 				unsigned long base, int nest_level)
1125*4882a593Smuzhiyun {
1126*4882a593Smuzhiyun 	struct token t;
1127*4882a593Smuzhiyun 	char *s = *c;
1128*4882a593Smuzhiyun 	int err = 0;
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	get_token(c, &t, L_KEYWORD);
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	switch (t.type) {
1133*4882a593Smuzhiyun 	case T_TITLE:
1134*4882a593Smuzhiyun 		err = parse_sliteral(c, &cfg->title);
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 		break;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	case T_INCLUDE:
1139*4882a593Smuzhiyun 		err = handle_include(cmdtp, c, base, cfg,
1140*4882a593Smuzhiyun 						nest_level + 1);
1141*4882a593Smuzhiyun 		break;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	default:
1144*4882a593Smuzhiyun 		printf("Ignoring malformed menu command: %.*s\n",
1145*4882a593Smuzhiyun 				(int)(*c - s), s);
1146*4882a593Smuzhiyun 	}
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	if (err < 0)
1149*4882a593Smuzhiyun 		return err;
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	eol_or_eof(c);
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	return 1;
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun /*
1157*4882a593Smuzhiyun  * Handles parsing a 'menu line' when we're parsing a label.
1158*4882a593Smuzhiyun  */
parse_label_menu(char ** c,struct pxe_menu * cfg,struct pxe_label * label)1159*4882a593Smuzhiyun static int parse_label_menu(char **c, struct pxe_menu *cfg,
1160*4882a593Smuzhiyun 				struct pxe_label *label)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun 	struct token t;
1163*4882a593Smuzhiyun 	char *s;
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	s = *c;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	get_token(c, &t, L_KEYWORD);
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	switch (t.type) {
1170*4882a593Smuzhiyun 	case T_DEFAULT:
1171*4882a593Smuzhiyun 		if (!cfg->default_label)
1172*4882a593Smuzhiyun 			cfg->default_label = strdup(label->name);
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 		if (!cfg->default_label)
1175*4882a593Smuzhiyun 			return -ENOMEM;
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 		break;
1178*4882a593Smuzhiyun 	case T_LABEL:
1179*4882a593Smuzhiyun 		parse_sliteral(c, &label->menu);
1180*4882a593Smuzhiyun 		break;
1181*4882a593Smuzhiyun 	default:
1182*4882a593Smuzhiyun 		printf("Ignoring malformed menu command: %.*s\n",
1183*4882a593Smuzhiyun 				(int)(*c - s), s);
1184*4882a593Smuzhiyun 	}
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	eol_or_eof(c);
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	return 0;
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun /*
1192*4882a593Smuzhiyun  * Parses a label and adds it to the list of labels for a menu.
1193*4882a593Smuzhiyun  *
1194*4882a593Smuzhiyun  * A label ends when we either get to the end of a file, or
1195*4882a593Smuzhiyun  * get some input we otherwise don't have a handler defined
1196*4882a593Smuzhiyun  * for.
1197*4882a593Smuzhiyun  *
1198*4882a593Smuzhiyun  */
parse_label(char ** c,struct pxe_menu * cfg)1199*4882a593Smuzhiyun static int parse_label(char **c, struct pxe_menu *cfg)
1200*4882a593Smuzhiyun {
1201*4882a593Smuzhiyun 	struct token t;
1202*4882a593Smuzhiyun 	int len;
1203*4882a593Smuzhiyun 	char *s = *c;
1204*4882a593Smuzhiyun 	struct pxe_label *label;
1205*4882a593Smuzhiyun 	int err;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	label = label_create();
1208*4882a593Smuzhiyun 	if (!label)
1209*4882a593Smuzhiyun 		return -ENOMEM;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	err = parse_sliteral(c, &label->name);
1212*4882a593Smuzhiyun 	if (err < 0) {
1213*4882a593Smuzhiyun 		printf("Expected label name: %.*s\n", (int)(*c - s), s);
1214*4882a593Smuzhiyun 		label_destroy(label);
1215*4882a593Smuzhiyun 		return -EINVAL;
1216*4882a593Smuzhiyun 	}
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	list_add_tail(&label->list, &cfg->labels);
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	while (1) {
1221*4882a593Smuzhiyun 		s = *c;
1222*4882a593Smuzhiyun 		get_token(c, &t, L_KEYWORD);
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 		err = 0;
1225*4882a593Smuzhiyun 		switch (t.type) {
1226*4882a593Smuzhiyun 		case T_MENU:
1227*4882a593Smuzhiyun 			err = parse_label_menu(c, cfg, label);
1228*4882a593Smuzhiyun 			break;
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 		case T_KERNEL:
1231*4882a593Smuzhiyun 		case T_LINUX:
1232*4882a593Smuzhiyun 			err = parse_sliteral(c, &label->kernel);
1233*4882a593Smuzhiyun 			break;
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun 		case T_APPEND:
1236*4882a593Smuzhiyun 			err = parse_sliteral(c, &label->append);
1237*4882a593Smuzhiyun 			if (label->initrd)
1238*4882a593Smuzhiyun 				break;
1239*4882a593Smuzhiyun 			s = strstr(label->append, "initrd=");
1240*4882a593Smuzhiyun 			if (!s)
1241*4882a593Smuzhiyun 				break;
1242*4882a593Smuzhiyun 			s += 7;
1243*4882a593Smuzhiyun 			len = (int)(strchr(s, ' ') - s);
1244*4882a593Smuzhiyun 			label->initrd = malloc(len + 1);
1245*4882a593Smuzhiyun 			strncpy(label->initrd, s, len);
1246*4882a593Smuzhiyun 			label->initrd[len] = '\0';
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 			break;
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 		case T_INITRD:
1251*4882a593Smuzhiyun 			if (!label->initrd)
1252*4882a593Smuzhiyun 				err = parse_sliteral(c, &label->initrd);
1253*4882a593Smuzhiyun 			break;
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 		case T_FDT:
1256*4882a593Smuzhiyun 			if (!label->fdt)
1257*4882a593Smuzhiyun 				err = parse_sliteral(c, &label->fdt);
1258*4882a593Smuzhiyun 			break;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 		case T_FDTDIR:
1261*4882a593Smuzhiyun 			if (!label->fdtdir)
1262*4882a593Smuzhiyun 				err = parse_sliteral(c, &label->fdtdir);
1263*4882a593Smuzhiyun 			break;
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 		case T_LOCALBOOT:
1266*4882a593Smuzhiyun 			label->localboot = 1;
1267*4882a593Smuzhiyun 			err = parse_integer(c, &label->localboot_val);
1268*4882a593Smuzhiyun 			break;
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 		case T_IPAPPEND:
1271*4882a593Smuzhiyun 			err = parse_integer(c, &label->ipappend);
1272*4882a593Smuzhiyun 			break;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 		case T_EOL:
1275*4882a593Smuzhiyun 			break;
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 		default:
1278*4882a593Smuzhiyun 			/*
1279*4882a593Smuzhiyun 			 * put the token back! we don't want it - it's the end
1280*4882a593Smuzhiyun 			 * of a label and whatever token this is, it's
1281*4882a593Smuzhiyun 			 * something for the menu level context to handle.
1282*4882a593Smuzhiyun 			 */
1283*4882a593Smuzhiyun 			*c = s;
1284*4882a593Smuzhiyun 			return 1;
1285*4882a593Smuzhiyun 		}
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 		if (err < 0)
1288*4882a593Smuzhiyun 			return err;
1289*4882a593Smuzhiyun 	}
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun /*
1293*4882a593Smuzhiyun  * This 16 comes from the limit pxelinux imposes on nested includes.
1294*4882a593Smuzhiyun  *
1295*4882a593Smuzhiyun  * There is no reason at all we couldn't do more, but some limit helps prevent
1296*4882a593Smuzhiyun  * infinite (until crash occurs) recursion if a file tries to include itself.
1297*4882a593Smuzhiyun  */
1298*4882a593Smuzhiyun #define MAX_NEST_LEVEL 16
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun /*
1301*4882a593Smuzhiyun  * Entry point for parsing a menu file. nest_level indicates how many times
1302*4882a593Smuzhiyun  * we've nested in includes.  It will be 1 for the top level menu file.
1303*4882a593Smuzhiyun  *
1304*4882a593Smuzhiyun  * Returns 1 on success, < 0 on error.
1305*4882a593Smuzhiyun  */
parse_pxefile_top(cmd_tbl_t * cmdtp,char * p,unsigned long base,struct pxe_menu * cfg,int nest_level)1306*4882a593Smuzhiyun static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1307*4882a593Smuzhiyun 				struct pxe_menu *cfg, int nest_level)
1308*4882a593Smuzhiyun {
1309*4882a593Smuzhiyun 	struct token t;
1310*4882a593Smuzhiyun 	char *s, *b, *label_name;
1311*4882a593Smuzhiyun 	int err;
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	b = p;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	if (nest_level > MAX_NEST_LEVEL) {
1316*4882a593Smuzhiyun 		printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1317*4882a593Smuzhiyun 		return -EMLINK;
1318*4882a593Smuzhiyun 	}
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	while (1) {
1321*4882a593Smuzhiyun 		s = p;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 		get_token(&p, &t, L_KEYWORD);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 		err = 0;
1326*4882a593Smuzhiyun 		switch (t.type) {
1327*4882a593Smuzhiyun 		case T_MENU:
1328*4882a593Smuzhiyun 			cfg->prompt = 1;
1329*4882a593Smuzhiyun 			err = parse_menu(cmdtp, &p, cfg,
1330*4882a593Smuzhiyun 				base + ALIGN(strlen(b) + 1, 4),
1331*4882a593Smuzhiyun 				nest_level);
1332*4882a593Smuzhiyun 			break;
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 		case T_TIMEOUT:
1335*4882a593Smuzhiyun 			err = parse_integer(&p, &cfg->timeout);
1336*4882a593Smuzhiyun 			break;
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 		case T_LABEL:
1339*4882a593Smuzhiyun 			err = parse_label(&p, cfg);
1340*4882a593Smuzhiyun 			break;
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 		case T_DEFAULT:
1343*4882a593Smuzhiyun 		case T_ONTIMEOUT:
1344*4882a593Smuzhiyun 			err = parse_sliteral(&p, &label_name);
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 			if (label_name) {
1347*4882a593Smuzhiyun 				if (cfg->default_label)
1348*4882a593Smuzhiyun 					free(cfg->default_label);
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 				cfg->default_label = label_name;
1351*4882a593Smuzhiyun 			}
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 			break;
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 		case T_INCLUDE:
1356*4882a593Smuzhiyun 			err = handle_include(cmdtp, &p,
1357*4882a593Smuzhiyun 				base + ALIGN(strlen(b), 4), cfg,
1358*4882a593Smuzhiyun 				nest_level + 1);
1359*4882a593Smuzhiyun 			break;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 		case T_PROMPT:
1362*4882a593Smuzhiyun 			eol_or_eof(&p);
1363*4882a593Smuzhiyun 			break;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 		case T_EOL:
1366*4882a593Smuzhiyun 			break;
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 		case T_EOF:
1369*4882a593Smuzhiyun 			return 1;
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun 		default:
1372*4882a593Smuzhiyun 			printf("Ignoring unknown command: %.*s\n",
1373*4882a593Smuzhiyun 							(int)(p - s), s);
1374*4882a593Smuzhiyun 			eol_or_eof(&p);
1375*4882a593Smuzhiyun 		}
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 		if (err < 0)
1378*4882a593Smuzhiyun 			return err;
1379*4882a593Smuzhiyun 	}
1380*4882a593Smuzhiyun }
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun /*
1383*4882a593Smuzhiyun  * Free the memory used by a pxe_menu and its labels.
1384*4882a593Smuzhiyun  */
destroy_pxe_menu(struct pxe_menu * cfg)1385*4882a593Smuzhiyun static void destroy_pxe_menu(struct pxe_menu *cfg)
1386*4882a593Smuzhiyun {
1387*4882a593Smuzhiyun 	struct list_head *pos, *n;
1388*4882a593Smuzhiyun 	struct pxe_label *label;
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 	if (cfg->title)
1391*4882a593Smuzhiyun 		free(cfg->title);
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	if (cfg->default_label)
1394*4882a593Smuzhiyun 		free(cfg->default_label);
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	list_for_each_safe(pos, n, &cfg->labels) {
1397*4882a593Smuzhiyun 		label = list_entry(pos, struct pxe_label, list);
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 		label_destroy(label);
1400*4882a593Smuzhiyun 	}
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 	free(cfg);
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun /*
1406*4882a593Smuzhiyun  * Entry point for parsing a pxe file. This is only used for the top level
1407*4882a593Smuzhiyun  * file.
1408*4882a593Smuzhiyun  *
1409*4882a593Smuzhiyun  * Returns NULL if there is an error, otherwise, returns a pointer to a
1410*4882a593Smuzhiyun  * pxe_menu struct populated with the results of parsing the pxe file (and any
1411*4882a593Smuzhiyun  * files it includes). The resulting pxe_menu struct can be free()'d by using
1412*4882a593Smuzhiyun  * the destroy_pxe_menu() function.
1413*4882a593Smuzhiyun  */
parse_pxefile(cmd_tbl_t * cmdtp,unsigned long menucfg)1414*4882a593Smuzhiyun static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
1415*4882a593Smuzhiyun {
1416*4882a593Smuzhiyun 	struct pxe_menu *cfg;
1417*4882a593Smuzhiyun 	char *buf;
1418*4882a593Smuzhiyun 	int r;
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	cfg = malloc(sizeof(struct pxe_menu));
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	if (!cfg)
1423*4882a593Smuzhiyun 		return NULL;
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	memset(cfg, 0, sizeof(struct pxe_menu));
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cfg->labels);
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	buf = map_sysmem(menucfg, 0);
1430*4882a593Smuzhiyun 	r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1431*4882a593Smuzhiyun 	unmap_sysmem(buf);
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	if (r < 0) {
1434*4882a593Smuzhiyun 		destroy_pxe_menu(cfg);
1435*4882a593Smuzhiyun 		return NULL;
1436*4882a593Smuzhiyun 	}
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	return cfg;
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun /*
1442*4882a593Smuzhiyun  * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1443*4882a593Smuzhiyun  * menu code.
1444*4882a593Smuzhiyun  */
pxe_menu_to_menu(struct pxe_menu * cfg)1445*4882a593Smuzhiyun static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1446*4882a593Smuzhiyun {
1447*4882a593Smuzhiyun 	struct pxe_label *label;
1448*4882a593Smuzhiyun 	struct list_head *pos;
1449*4882a593Smuzhiyun 	struct menu *m;
1450*4882a593Smuzhiyun 	int err;
1451*4882a593Smuzhiyun 	int i = 1;
1452*4882a593Smuzhiyun 	char *default_num = NULL;
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 	/*
1455*4882a593Smuzhiyun 	 * Create a menu and add items for all the labels.
1456*4882a593Smuzhiyun 	 */
1457*4882a593Smuzhiyun 	m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1458*4882a593Smuzhiyun 			NULL, NULL);
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 	if (!m)
1461*4882a593Smuzhiyun 		return NULL;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	list_for_each(pos, &cfg->labels) {
1464*4882a593Smuzhiyun 		label = list_entry(pos, struct pxe_label, list);
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun 		sprintf(label->num, "%d", i++);
1467*4882a593Smuzhiyun 		if (menu_item_add(m, label->num, label) != 1) {
1468*4882a593Smuzhiyun 			menu_destroy(m);
1469*4882a593Smuzhiyun 			return NULL;
1470*4882a593Smuzhiyun 		}
1471*4882a593Smuzhiyun 		if (cfg->default_label &&
1472*4882a593Smuzhiyun 		    (strcmp(label->name, cfg->default_label) == 0))
1473*4882a593Smuzhiyun 			default_num = label->num;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	}
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	/*
1478*4882a593Smuzhiyun 	 * After we've created items for each label in the menu, set the
1479*4882a593Smuzhiyun 	 * menu's default label if one was specified.
1480*4882a593Smuzhiyun 	 */
1481*4882a593Smuzhiyun 	if (default_num) {
1482*4882a593Smuzhiyun 		err = menu_default_set(m, default_num);
1483*4882a593Smuzhiyun 		if (err != 1) {
1484*4882a593Smuzhiyun 			if (err != -ENOENT) {
1485*4882a593Smuzhiyun 				menu_destroy(m);
1486*4882a593Smuzhiyun 				return NULL;
1487*4882a593Smuzhiyun 			}
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 			printf("Missing default: %s\n", cfg->default_label);
1490*4882a593Smuzhiyun 		}
1491*4882a593Smuzhiyun 	}
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	return m;
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun /*
1497*4882a593Smuzhiyun  * Try to boot any labels we have yet to attempt to boot.
1498*4882a593Smuzhiyun  */
boot_unattempted_labels(cmd_tbl_t * cmdtp,struct pxe_menu * cfg)1499*4882a593Smuzhiyun static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1500*4882a593Smuzhiyun {
1501*4882a593Smuzhiyun 	struct list_head *pos;
1502*4882a593Smuzhiyun 	struct pxe_label *label;
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	list_for_each(pos, &cfg->labels) {
1505*4882a593Smuzhiyun 		label = list_entry(pos, struct pxe_label, list);
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 		if (!label->attempted)
1508*4882a593Smuzhiyun 			label_boot(cmdtp, label);
1509*4882a593Smuzhiyun 	}
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun /*
1513*4882a593Smuzhiyun  * Boot the system as prescribed by a pxe_menu.
1514*4882a593Smuzhiyun  *
1515*4882a593Smuzhiyun  * Use the menu system to either get the user's choice or the default, based
1516*4882a593Smuzhiyun  * on config or user input.  If there is no default or user's choice,
1517*4882a593Smuzhiyun  * attempted to boot labels in the order they were given in pxe files.
1518*4882a593Smuzhiyun  * If the default or user's choice fails to boot, attempt to boot other
1519*4882a593Smuzhiyun  * labels in the order they were given in pxe files.
1520*4882a593Smuzhiyun  *
1521*4882a593Smuzhiyun  * If this function returns, there weren't any labels that successfully
1522*4882a593Smuzhiyun  * booted, or the user interrupted the menu selection via ctrl+c.
1523*4882a593Smuzhiyun  */
handle_pxe_menu(cmd_tbl_t * cmdtp,struct pxe_menu * cfg)1524*4882a593Smuzhiyun static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1525*4882a593Smuzhiyun {
1526*4882a593Smuzhiyun 	void *choice;
1527*4882a593Smuzhiyun 	struct menu *m;
1528*4882a593Smuzhiyun 	int err;
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	m = pxe_menu_to_menu(cfg);
1531*4882a593Smuzhiyun 	if (!m)
1532*4882a593Smuzhiyun 		return;
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	err = menu_get_choice(m, &choice);
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 	menu_destroy(m);
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	/*
1539*4882a593Smuzhiyun 	 * err == 1 means we got a choice back from menu_get_choice.
1540*4882a593Smuzhiyun 	 *
1541*4882a593Smuzhiyun 	 * err == -ENOENT if the menu was setup to select the default but no
1542*4882a593Smuzhiyun 	 * default was set. in that case, we should continue trying to boot
1543*4882a593Smuzhiyun 	 * labels that haven't been attempted yet.
1544*4882a593Smuzhiyun 	 *
1545*4882a593Smuzhiyun 	 * otherwise, the user interrupted or there was some other error and
1546*4882a593Smuzhiyun 	 * we give up.
1547*4882a593Smuzhiyun 	 */
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun 	if (err == 1) {
1550*4882a593Smuzhiyun 		err = label_boot(cmdtp, choice);
1551*4882a593Smuzhiyun 		if (!err)
1552*4882a593Smuzhiyun 			return;
1553*4882a593Smuzhiyun 	} else if (err != -ENOENT) {
1554*4882a593Smuzhiyun 		return;
1555*4882a593Smuzhiyun 	}
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun 	boot_unattempted_labels(cmdtp, cfg);
1558*4882a593Smuzhiyun }
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun #ifdef CONFIG_CMD_NET
1561*4882a593Smuzhiyun /*
1562*4882a593Smuzhiyun  * Boots a system using a pxe file
1563*4882a593Smuzhiyun  *
1564*4882a593Smuzhiyun  * Returns 0 on success, 1 on error.
1565*4882a593Smuzhiyun  */
1566*4882a593Smuzhiyun static int
do_pxe_boot(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1567*4882a593Smuzhiyun do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1568*4882a593Smuzhiyun {
1569*4882a593Smuzhiyun 	unsigned long pxefile_addr_r;
1570*4882a593Smuzhiyun 	struct pxe_menu *cfg;
1571*4882a593Smuzhiyun 	char *pxefile_addr_str;
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	do_getfile = do_get_tftp;
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	if (argc == 1) {
1576*4882a593Smuzhiyun 		pxefile_addr_str = from_env("pxefile_addr_r");
1577*4882a593Smuzhiyun 		if (!pxefile_addr_str)
1578*4882a593Smuzhiyun 			return 1;
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	} else if (argc == 2) {
1581*4882a593Smuzhiyun 		pxefile_addr_str = argv[1];
1582*4882a593Smuzhiyun 	} else {
1583*4882a593Smuzhiyun 		return CMD_RET_USAGE;
1584*4882a593Smuzhiyun 	}
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 	if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1587*4882a593Smuzhiyun 		printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1588*4882a593Smuzhiyun 		return 1;
1589*4882a593Smuzhiyun 	}
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	if (cfg == NULL) {
1594*4882a593Smuzhiyun 		printf("Error parsing config file\n");
1595*4882a593Smuzhiyun 		return 1;
1596*4882a593Smuzhiyun 	}
1597*4882a593Smuzhiyun 
1598*4882a593Smuzhiyun 	handle_pxe_menu(cmdtp, cfg);
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 	destroy_pxe_menu(cfg);
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	return 0;
1605*4882a593Smuzhiyun }
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun static cmd_tbl_t cmd_pxe_sub[] = {
1608*4882a593Smuzhiyun 	U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1609*4882a593Smuzhiyun 	U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1610*4882a593Smuzhiyun };
1611*4882a593Smuzhiyun 
do_pxe(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1612*4882a593Smuzhiyun static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1613*4882a593Smuzhiyun {
1614*4882a593Smuzhiyun 	cmd_tbl_t *cp;
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 	if (argc < 2)
1617*4882a593Smuzhiyun 		return CMD_RET_USAGE;
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	is_pxe = true;
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 	/* drop initial "pxe" arg */
1622*4882a593Smuzhiyun 	argc--;
1623*4882a593Smuzhiyun 	argv++;
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 	cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	if (cp)
1628*4882a593Smuzhiyun 		return cp->cmd(cmdtp, flag, argc, argv);
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	return CMD_RET_USAGE;
1631*4882a593Smuzhiyun }
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun U_BOOT_CMD(
1634*4882a593Smuzhiyun 	pxe, 3, 1, do_pxe,
1635*4882a593Smuzhiyun 	"commands to get and boot from pxe files",
1636*4882a593Smuzhiyun 	"get - try to retrieve a pxe file using tftp\npxe "
1637*4882a593Smuzhiyun 	"boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1638*4882a593Smuzhiyun );
1639*4882a593Smuzhiyun #endif
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun /*
1642*4882a593Smuzhiyun  * Boots a system using a local disk syslinux/extlinux file
1643*4882a593Smuzhiyun  *
1644*4882a593Smuzhiyun  * Returns 0 on success, 1 on error.
1645*4882a593Smuzhiyun  */
do_sysboot(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1646*4882a593Smuzhiyun static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1647*4882a593Smuzhiyun {
1648*4882a593Smuzhiyun 	unsigned long pxefile_addr_r;
1649*4882a593Smuzhiyun 	struct pxe_menu *cfg;
1650*4882a593Smuzhiyun 	char *pxefile_addr_str;
1651*4882a593Smuzhiyun 	char *filename;
1652*4882a593Smuzhiyun 	int prompt = 0;
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	is_pxe = false;
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	if (argc > 1 && strstr(argv[1], "-p")) {
1657*4882a593Smuzhiyun 		prompt = 1;
1658*4882a593Smuzhiyun 		argc--;
1659*4882a593Smuzhiyun 		argv++;
1660*4882a593Smuzhiyun 	}
1661*4882a593Smuzhiyun 
1662*4882a593Smuzhiyun 	if (argc < 4)
1663*4882a593Smuzhiyun 		return cmd_usage(cmdtp);
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 	if (argc < 5) {
1666*4882a593Smuzhiyun 		pxefile_addr_str = from_env("pxefile_addr_r");
1667*4882a593Smuzhiyun 		if (!pxefile_addr_str)
1668*4882a593Smuzhiyun 			return 1;
1669*4882a593Smuzhiyun 	} else {
1670*4882a593Smuzhiyun 		pxefile_addr_str = argv[4];
1671*4882a593Smuzhiyun 	}
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	if (argc < 6)
1674*4882a593Smuzhiyun 		filename = env_get("bootfile");
1675*4882a593Smuzhiyun 	else {
1676*4882a593Smuzhiyun 		filename = argv[5];
1677*4882a593Smuzhiyun 		env_set("bootfile", filename);
1678*4882a593Smuzhiyun 	}
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	if (strstr(argv[3], "ext2"))
1681*4882a593Smuzhiyun 		do_getfile = do_get_ext2;
1682*4882a593Smuzhiyun 	else if (strstr(argv[3], "fat"))
1683*4882a593Smuzhiyun 		do_getfile = do_get_fat;
1684*4882a593Smuzhiyun 	else if (strstr(argv[3], "any"))
1685*4882a593Smuzhiyun 		do_getfile = do_get_any;
1686*4882a593Smuzhiyun 	else {
1687*4882a593Smuzhiyun 		printf("Invalid filesystem: %s\n", argv[3]);
1688*4882a593Smuzhiyun 		return 1;
1689*4882a593Smuzhiyun 	}
1690*4882a593Smuzhiyun 	fs_argv[1] = argv[1];
1691*4882a593Smuzhiyun 	fs_argv[2] = argv[2];
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 	if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1694*4882a593Smuzhiyun 		printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1695*4882a593Smuzhiyun 		return 1;
1696*4882a593Smuzhiyun 	}
1697*4882a593Smuzhiyun 
1698*4882a593Smuzhiyun 	if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
1699*4882a593Smuzhiyun 		printf("Error reading config file\n");
1700*4882a593Smuzhiyun 		return 1;
1701*4882a593Smuzhiyun 	}
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 	cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun 	if (cfg == NULL) {
1706*4882a593Smuzhiyun 		printf("Error parsing config file\n");
1707*4882a593Smuzhiyun 		return 1;
1708*4882a593Smuzhiyun 	}
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun 	if (prompt)
1711*4882a593Smuzhiyun 		cfg->prompt = 1;
1712*4882a593Smuzhiyun 
1713*4882a593Smuzhiyun 	handle_pxe_menu(cmdtp, cfg);
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 	destroy_pxe_menu(cfg);
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 	return 0;
1718*4882a593Smuzhiyun }
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun U_BOOT_CMD(
1721*4882a593Smuzhiyun 	sysboot, 7, 1, do_sysboot,
1722*4882a593Smuzhiyun 	"command to get and boot from syslinux files",
1723*4882a593Smuzhiyun 	"[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1724*4882a593Smuzhiyun 	"    - load and parse syslinux menu file 'filename' from ext2, fat\n"
1725*4882a593Smuzhiyun 	"      or any filesystem on 'dev' on 'interface' to address 'addr'"
1726*4882a593Smuzhiyun );
1727