Skip to content
Snippets Groups Projects
pxe.c 37.3 KiB
Newer Older
Jason Hobbs's avatar
Jason Hobbs committed
/*
 * Copyright 2010-2011 Calxeda, Inc.
 * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
Jason Hobbs's avatar
Jason Hobbs committed
 *
 * SPDX-License-Identifier:	GPL-2.0+
Jason Hobbs's avatar
Jason Hobbs committed
 */
Jason Hobbs's avatar
Jason Hobbs committed
#include <common.h>
#include <command.h>
#include <malloc.h>
Jason Hobbs's avatar
Jason Hobbs committed
#include <linux/string.h>
#include <linux/ctype.h>
#include <errno.h>
#include <linux/list.h>
#include <asm/io.h>
Jason Hobbs's avatar
Jason Hobbs committed

#include "menu.h"
Jason Hobbs's avatar
Jason Hobbs committed

#define MAX_TFTP_PATH_LEN 127

const char *pxe_default_paths[] = {
#ifdef CONFIG_SYS_SOC
	"default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
	"default-" CONFIG_SYS_ARCH,
	"default",
	NULL
};

static bool is_pxe;

Jason Hobbs's avatar
Jason Hobbs committed
/*
 * Like getenv, but prints an error if envvar isn't defined in the
 * environment.  It always returns what getenv does, so it can be used in
 * place of getenv without changing error handling otherwise.
 */
static char *from_env(const char *envvar)
Jason Hobbs's avatar
Jason Hobbs committed
{
	char *ret;

	ret = getenv(envvar);

	if (!ret)
		printf("missing environment variable: %s\n", envvar);

	return ret;
}

#ifdef CONFIG_CMD_NET
Jason Hobbs's avatar
Jason Hobbs committed
/*
 * Convert an ethaddr from the environment to the format used by pxelinux
 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
 * the beginning of the ethernet address to indicate a hardware type of
 * Ethernet. Also converts uppercase hex characters into lowercase, to match
 * pxelinux's behavior.
 *
 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
 * environment, or some other value < 0 on error.
 */
static int format_mac_pxe(char *outbuf, size_t outbuf_len)
{
	uchar ethaddr[6];
Jason Hobbs's avatar
Jason Hobbs committed

	if (outbuf_len < 21) {
		printf("outbuf is too small (%zd < 21)\n", outbuf_len);
Jason Hobbs's avatar
Jason Hobbs committed

		return -EINVAL;
	}

	if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
					  ethaddr))
		return -ENOENT;
Jason Hobbs's avatar
Jason Hobbs committed

	sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
		ethaddr[0], ethaddr[1], ethaddr[2],
		ethaddr[3], ethaddr[4], ethaddr[5]);
Jason Hobbs's avatar
Jason Hobbs committed

	return 1;
}
Jason Hobbs's avatar
Jason Hobbs committed

/*
 * Returns the directory the file specified in the bootfile env variable is
 * in. If bootfile isn't defined in the environment, return NULL, which should
 * be interpreted as "don't prepend anything to paths".
 */
Rob Herring's avatar
Rob Herring committed
static int get_bootfile_path(const char *file_path, char *bootfile_path,
			     size_t bootfile_path_size)
Jason Hobbs's avatar
Jason Hobbs committed
{
	char *bootfile, *last_slash;
Rob Herring's avatar
Rob Herring committed
	size_t path_len = 0;

	/* Only syslinux allows absolute paths */
	if (file_path[0] == '/' && !is_pxe)
Rob Herring's avatar
Rob Herring committed
		goto ret;
Jason Hobbs's avatar
Jason Hobbs committed

	bootfile = from_env("bootfile");

Rob Herring's avatar
Rob Herring committed
	if (!bootfile)
		goto ret;
Jason Hobbs's avatar
Jason Hobbs committed

	last_slash = strrchr(bootfile, '/');

Rob Herring's avatar
Rob Herring committed
	if (last_slash == NULL)
		goto ret;
Jason Hobbs's avatar
Jason Hobbs committed

	path_len = (last_slash - bootfile) + 1;

	if (bootfile_path_size < path_len) {
		printf("bootfile_path too small. (%zd < %zd)\n",
Jason Hobbs's avatar
Jason Hobbs committed
				bootfile_path_size, path_len);

		return -1;
	}

	strncpy(bootfile_path, bootfile, path_len);

Rob Herring's avatar
Rob Herring committed
 ret:
Jason Hobbs's avatar
Jason Hobbs committed
	bootfile_path[path_len] = '\0';

	return 1;
}

static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
{
	char *tftp_argv[] = {"tftp", NULL, NULL, NULL};

	tftp_argv[1] = file_addr;
	tftp_argv[2] = (void *)file_path;
	if (do_tftpb(cmdtp, 0, 3, tftp_argv))
static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
{
#ifdef CONFIG_CMD_EXT2
	fs_argv[0] = "ext2load";
	fs_argv[3] = file_addr;
	fs_argv[4] = (void *)file_path;
	if (!do_ext2load(cmdtp, 0, 5, fs_argv))
static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
{
#ifdef CONFIG_CMD_FAT
	fs_argv[0] = "fatload";
	fs_argv[3] = file_addr;
	fs_argv[4] = (void *)file_path;
	if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
{
#ifdef CONFIG_CMD_FS_GENERIC
	fs_argv[0] = "load";
	fs_argv[3] = file_addr;
	fs_argv[4] = (void *)file_path;

	if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
		return 1;
#endif
	return -ENOENT;
}

Jason Hobbs's avatar
Jason Hobbs committed
/*
 * As in pxelinux, paths to files referenced from files we retrieve are
 * relative to the location of bootfile. get_relfile takes such a path and
 * joins it with the bootfile path to get the full path to the target file. If
 * the bootfile path is NULL, we use file_path as is.
 *
 * Returns 1 for success, or < 0 on error.
 */
static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
	unsigned long file_addr)
Jason Hobbs's avatar
Jason Hobbs committed
{
	size_t path_len;
	char relfile[MAX_TFTP_PATH_LEN+1];
Jason Hobbs's avatar
Jason Hobbs committed
	int err;

Rob Herring's avatar
Rob Herring committed
	err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs's avatar
Jason Hobbs committed

	if (err < 0)
		return err;

	path_len = strlen(file_path);
	path_len += strlen(relfile);

	if (path_len > MAX_TFTP_PATH_LEN) {
		printf("Base path too long (%s%s)\n",
					relfile,
					file_path);

		return -ENAMETOOLONG;
	}

	strcat(relfile, file_path);

	printf("Retrieving file: %s\n", relfile);

	sprintf(addr_buf, "%lx", file_addr);
Jason Hobbs's avatar
Jason Hobbs committed

	return do_getfile(cmdtp, relfile, addr_buf);
Jason Hobbs's avatar
Jason Hobbs committed
}

/*
 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
 * 'bootfile' was specified in the environment, the path to bootfile will be
 * prepended to 'file_path' and the resulting path will be used.
 *
 * Returns 1 on success, or < 0 for error.
 */
static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
	unsigned long file_addr)
Jason Hobbs's avatar
Jason Hobbs committed
{
	unsigned long config_file_size;
	char *tftp_filesize;
	int err;
Jason Hobbs's avatar
Jason Hobbs committed

	err = get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs's avatar
Jason Hobbs committed

	if (err < 0)
		return err;

	/*
	 * the file comes without a NUL byte at the end, so find out its size
	 * and add the NUL byte.
	 */
	tftp_filesize = from_env("filesize");

	if (!tftp_filesize)
		return -ENOENT;

	if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
		return -EINVAL;

	buf = map_sysmem(file_addr + config_file_size, 1);
	*buf = '\0';
	unmap_sysmem(buf);
Jason Hobbs's avatar
Jason Hobbs committed

	return 1;
}

Jason Hobbs's avatar
Jason Hobbs committed
#define PXELINUX_DIR "pxelinux.cfg/"

/*
 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
 * from the bootfile path, as described above.
 *
 * Returns 1 on success or < 0 on error.
 */
static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
	unsigned long pxefile_addr_r)
Jason Hobbs's avatar
Jason Hobbs committed
{
	size_t base_len = strlen(PXELINUX_DIR);
	char path[MAX_TFTP_PATH_LEN+1];

	if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
		printf("path (%s%s) too long, skipping\n",
				PXELINUX_DIR, file);
		return -ENAMETOOLONG;
	}

	sprintf(path, PXELINUX_DIR "%s", file);

	return get_pxe_file(cmdtp, path, pxefile_addr_r);
Jason Hobbs's avatar
Jason Hobbs committed
}

/*
 * Looks for a pxe file with a name based on the pxeuuid environment variable.
 *
 * Returns 1 on success or < 0 on error.
 */
static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs's avatar
Jason Hobbs committed
{
	char *uuid_str;

	uuid_str = from_env("pxeuuid");

	if (!uuid_str)
		return -ENOENT;

	return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
Jason Hobbs's avatar
Jason Hobbs committed
}

/*
 * Looks for a pxe file with a name based on the 'ethaddr' environment
 * variable.
 *
 * Returns 1 on success or < 0 on error.
 */
static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs's avatar
Jason Hobbs committed
{
	char mac_str[21];
	int err;

	err = format_mac_pxe(mac_str, sizeof(mac_str));

	if (err < 0)
		return err;

	return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
Jason Hobbs's avatar
Jason Hobbs committed
}

/*
 * Looks for pxe files with names based on our IP address. See pxelinux
 * documentation for details on what these file names look like.  We match
 * that exactly.
 *
 * Returns 1 on success or < 0 on error.
 */
static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
Jason Hobbs's avatar
Jason Hobbs committed
{
	char ip_addr[9];
	int mask_pos, err;

	sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
Jason Hobbs's avatar
Jason Hobbs committed

	for (mask_pos = 7; mask_pos >= 0;  mask_pos--) {
		err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
Jason Hobbs's avatar
Jason Hobbs committed

		if (err > 0)
			return err;

		ip_addr[mask_pos] = '\0';
	}

	return -ENOENT;
}

/*
 * Entry point for the 'pxe get' command.
 * This Follows pxelinux's rules to download a config file from a tftp server.
 * The file is stored at the location given by the pxefile_addr_r environment
 * variable, which must be set.
 *
 * UUID comes from pxeuuid env variable, if defined
 * MAC addr comes from ethaddr env variable, if defined
 * IP
 *
 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
 *
 * Returns 0 on success or 1 on error.
 */
static int
do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	char *pxefile_addr_str;
	unsigned long pxefile_addr_r;
Jason Hobbs's avatar
Jason Hobbs committed

	do_getfile = do_get_tftp;

Jason Hobbs's avatar
Jason Hobbs committed
	if (argc != 1)
Jason Hobbs's avatar
Jason Hobbs committed

	pxefile_addr_str = from_env("pxefile_addr_r");

	if (!pxefile_addr_str)
		return 1;

	err = strict_strtoul(pxefile_addr_str, 16,
				(unsigned long *)&pxefile_addr_r);
	if (err < 0)
		return 1;

	/*
	 * Keep trying paths until we successfully get a file we're looking
	 * for.
	 */
	if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
	    pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
	    pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
Jason Hobbs's avatar
Jason Hobbs committed
		printf("Config file found\n");

		return 0;
	}

	while (pxe_default_paths[i]) {
		if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
				      pxefile_addr_r) > 0) {
			printf("Config file found\n");
			return 0;
		}
		i++;
	}

Jason Hobbs's avatar
Jason Hobbs committed
	printf("Config file not found\n");

	return 1;
}
Jason Hobbs's avatar
Jason Hobbs committed

/*
 * Wrapper to make it easier to store the file at file_path in the location
 * specified by envaddr_name. file_path will be joined to the bootfile path,
 * if any is specified.
 *
 * Returns 1 on success or < 0 on error.
 */
static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
Jason Hobbs's avatar
Jason Hobbs committed
{
	unsigned long file_addr;
Jason Hobbs's avatar
Jason Hobbs committed
	char *envaddr;

	envaddr = from_env(envaddr_name);

	if (!envaddr)
		return -ENOENT;

	if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs's avatar
Jason Hobbs committed
		return -EINVAL;

	return get_relfile(cmdtp, file_path, file_addr);
Jason Hobbs's avatar
Jason Hobbs committed
}

/*
 * A note on the pxe file parser.
 *
 * We're parsing files that use syslinux grammar, which has a few quirks.
 * String literals must be recognized based on context - there is no
 * quoting or escaping support. There's also nothing to explicitly indicate
 * when a label section completes. We deal with that by ending a label
 * section whenever we see a line that doesn't include.
 *
 * As with the syslinux family, this same file format could be reused in the
 * future for non pxe purposes. The only action it takes during parsing that
 * would throw this off is handling of include files. It assumes we're using
 * pxe, and does a tftp download of a file listed as an include file in the
 * middle of the parsing operation. That could be handled by refactoring it to
 * take a 'include file getter' function.
 */

/*
 * Describes a single label given in a pxe file.
 *
 * Create these with the 'label_create' function given below.
 *
 * name - the name of the menu as given on the 'menu label' line.
 * kernel - the path to the kernel file to use for this label.
 * append - kernel command line to use when booting this label
 * initrd - path to the initrd to use for this label.
 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
 * localboot - 1 if this label specified 'localboot', 0 otherwise.
 * list - lets these form a list, which a pxe_menu struct will hold.
 */
struct pxe_label {
	char num[4];
Jason Hobbs's avatar
Jason Hobbs committed
	char *name;
	char *menu;
Jason Hobbs's avatar
Jason Hobbs committed
	char *kernel;
	char *append;
	char *initrd;
	char *fdtdir;
Rob Herring's avatar
Rob Herring committed
	int ipappend;
Jason Hobbs's avatar
Jason Hobbs committed
	int attempted;
	int localboot;
	int localboot_val;
Jason Hobbs's avatar
Jason Hobbs committed
	struct list_head list;
};

/*
 * Describes a pxe menu as given via pxe files.
 *
 * title - the name of the menu as given by a 'menu title' line.
 * default_label - the name of the default label, if any.
 * timeout - time in tenths of a second to wait for a user key-press before
 *           booting the default label.
 * prompt - if 0, don't prompt for a choice unless the timeout period is
 *          interrupted.  If 1, always prompt for a choice regardless of
 *          timeout.
 * labels - a list of labels defined for the menu.
 */
struct pxe_menu {
	char *title;
	char *default_label;
	int timeout;
	int prompt;
	struct list_head labels;
};

/*
 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
 * result must be free()'d to reclaim the memory.
 *
 * Returns NULL if malloc fails.
 */
static struct pxe_label *label_create(void)
{
	struct pxe_label *label;

	label = malloc(sizeof(struct pxe_label));

	if (!label)
		return NULL;

	memset(label, 0, sizeof(struct pxe_label));

	return label;
}

/*
 * Free the memory used by a pxe_label, including that used by its name,
 * kernel, append and initrd members, if they're non NULL.
 *
 * So - be sure to only use dynamically allocated memory for the members of
 * the pxe_label struct, unless you want to clean it up first. These are
 * currently only created by the pxe file parsing code.
 */
static void label_destroy(struct pxe_label *label)
{
	if (label->name)
		free(label->name);

	if (label->kernel)
		free(label->kernel);

	if (label->append)
		free(label->append);

	if (label->initrd)
		free(label->initrd);

	if (label->fdt)
		free(label->fdt);

	if (label->fdtdir)
		free(label->fdtdir);

Jason Hobbs's avatar
Jason Hobbs committed
	free(label);
}

/*
 * Print a label and its string members if they're defined.
 *
 * This is passed as a callback to the menu code for displaying each
 * menu entry.
 */
static void label_print(void *data)
{
	struct pxe_label *label = data;
	const char *c = label->menu ? label->menu : label->name;
Jason Hobbs's avatar
Jason Hobbs committed

	printf("%s:\t%s\n", label->num, c);
Jason Hobbs's avatar
Jason Hobbs committed
}

/*
 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
 * environment variable is defined. Its contents will be executed as U-Boot
Jason Hobbs's avatar
Jason Hobbs committed
 * command.  If the label specified an 'append' line, its contents will be
 * used to overwrite the contents of the 'bootargs' environment variable prior
 * to running 'localcmd'.
 *
 * Returns 1 on success or < 0 on error.
 */
static int label_localboot(struct pxe_label *label)
{
Jason Hobbs's avatar
Jason Hobbs committed

	localcmd = from_env("localcmd");

	if (!localcmd)
		return -ENOENT;

	if (label->append) {
		char bootargs[CONFIG_SYS_CBSIZE];

		cli_simple_process_macros(label->append, bootargs);
		setenv("bootargs", bootargs);
	}
Jason Hobbs's avatar
Jason Hobbs committed

	debug("running: %s\n", localcmd);
Jason Hobbs's avatar
Jason Hobbs committed

	return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs's avatar
Jason Hobbs committed
}

/*
 * Boot according to the contents of a pxe_label.
 *
 * If we can't boot for any reason, we return.  A successful boot never
 * returns.
 *
 * The kernel will be stored in the location given by the 'kernel_addr_r'
 * environment variable.
 *
 * If the label specifies an initrd file, it will be stored in the location
 * given by the 'ramdisk_addr_r' environment variable.
 *
 * If the label specifies an 'append' line, its contents will overwrite that
 * of the 'bootargs' environment variable.
 */
static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
Jason Hobbs's avatar
Jason Hobbs committed
{
	char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
	char initrd_str[22];
Rob Herring's avatar
Rob Herring committed
	char mac_str[29] = "";
	char ip_str[68] = "";
Jason Hobbs's avatar
Jason Hobbs committed
	int bootm_argc = 3;
Rob Herring's avatar
Rob Herring committed
	int len = 0;
	ulong kernel_addr;
	void *buf;
Jason Hobbs's avatar
Jason Hobbs committed

	label_print(label);

	label->attempted = 1;

	if (label->localboot) {
		if (label->localboot_val >= 0)
			label_localboot(label);
		return 0;
Jason Hobbs's avatar
Jason Hobbs committed
	}

	if (label->kernel == NULL) {
		printf("No kernel given, skipping %s\n",
				label->name);
Jason Hobbs's avatar
Jason Hobbs committed
	}

	if (label->initrd) {
		if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
Jason Hobbs's avatar
Jason Hobbs committed
			printf("Skipping %s for failure retrieving initrd\n",
					label->name);
Jason Hobbs's avatar
Jason Hobbs committed
		}

		bootm_argv[2] = initrd_str;
		strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
		strcat(bootm_argv[2], ":");
		strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs's avatar
Jason Hobbs committed
	} else {
		bootm_argv[2] = "-";
	}
Loading
Loading full blame...