]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_pxe.c
gpio: remove gpiolib.c and define remaining functions as static inline in asm/gpio.h
[karo-tx-uboot.git] / common / cmd_pxe.c
index 79d3a061f537f6c1fe5a527b0d13bde3d7a2834e..7e32c95df3217bc575dd39be2a71fd3f6e0deea3 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2010-2011 Calxeda, Inc.
+ * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
  *
  * SPDX-License-Identifier:    GPL-2.0+
  */
 #include <linux/ctype.h>
 #include <errno.h>
 #include <linux/list.h>
+#include <fs.h>
 
 #include "menu.h"
+#include "cli.h"
 
 #define MAX_TFTP_PATH_LEN 127
 
@@ -25,6 +28,8 @@ const char *pxe_default_paths[] = {
        NULL
 };
 
+static bool is_pxe;
+
 /*
  * 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
@@ -42,6 +47,7 @@ static char *from_env(const char *envvar)
        return ret;
 }
 
+#ifdef CONFIG_CMD_NET
 /*
  * Convert an ethaddr from the environment to the format used by pxelinux
  * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
@@ -57,7 +63,7 @@ static int format_mac_pxe(char *outbuf, size_t outbuf_len)
        uchar ethaddr[6];
 
        if (outbuf_len < 21) {
-               printf("outbuf is too small (%d < 21)\n", outbuf_len);
+               printf("outbuf is too small (%zd < 21)\n", outbuf_len);
 
                return -EINVAL;
        }
@@ -72,6 +78,7 @@ static int format_mac_pxe(char *outbuf, size_t outbuf_len)
 
        return 1;
 }
+#endif
 
 /*
  * Returns the directory the file specified in the bootfile env variable is
@@ -84,7 +91,8 @@ static int get_bootfile_path(const char *file_path, char *bootfile_path,
        char *bootfile, *last_slash;
        size_t path_len = 0;
 
-       if (file_path[0] == '/')
+       /* Only syslinux allows absolute paths */
+       if (file_path[0] == '/' && !is_pxe)
                goto ret;
 
        bootfile = from_env("bootfile");
@@ -100,7 +108,7 @@ static int get_bootfile_path(const char *file_path, char *bootfile_path,
        path_len = (last_slash - bootfile) + 1;
 
        if (bootfile_path_size < path_len) {
-               printf("bootfile_path too small. (%d < %d)\n",
+               printf("bootfile_path too small. (%zd < %zd)\n",
                                bootfile_path_size, path_len);
 
                return -1;
@@ -116,6 +124,7 @@ static int get_bootfile_path(const char *file_path, char *bootfile_path,
 
 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
 
+#ifdef CONFIG_CMD_NET
 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
 {
        char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
@@ -128,6 +137,7 @@ static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
 
        return 1;
 }
+#endif
 
 static char *fs_argv[5];
 
@@ -157,6 +167,19 @@ static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
        return -ENOENT;
 }
 
+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;
+}
+
 /*
  * 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
@@ -232,6 +255,8 @@ static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr
        return 1;
 }
 
+#ifdef CONFIG_CMD_NET
+
 #define PXELINUX_DIR "pxelinux.cfg/"
 
 /*
@@ -380,6 +405,7 @@ do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
        return 1;
 }
+#endif
 
 /*
  * Wrapper to make it easier to store the file at file_path in the location
@@ -442,6 +468,7 @@ struct pxe_label {
        char *append;
        char *initrd;
        char *fdt;
+       char *fdtdir;
        int ipappend;
        int attempted;
        int localboot;
@@ -514,6 +541,9 @@ static void label_destroy(struct pxe_label *label)
        if (label->fdt)
                free(label->fdt);
 
+       if (label->fdtdir)
+               free(label->fdtdir);
+
        free(label);
 }
 
@@ -549,8 +579,12 @@ static int label_localboot(struct pxe_label *label)
        if (!localcmd)
                return -ENOENT;
 
-       if (label->append)
-               setenv("bootargs", label->append);
+       if (label->append) {
+               char bootargs[CONFIG_SYS_CBSIZE];
+
+               cli_simple_process_macros(label->append, bootargs);
+               setenv("bootargs", bootargs);
+       }
 
        debug("running: %s\n", localcmd);
 
@@ -578,9 +612,10 @@ static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
        char initrd_str[22];
        char mac_str[29] = "";
        char ip_str[68] = "";
-       char *bootargs;
        int bootm_argc = 3;
        int len = 0;
+       ulong kernel_addr;
+       void *buf;
 
        label_print(label);
 
@@ -623,35 +658,39 @@ static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
                sprintf(ip_str, " ip=%s:%s:%s:%s",
                        getenv("ipaddr"), getenv("serverip"),
                        getenv("gatewayip"), getenv("netmask"));
-               len += strlen(ip_str);
        }
 
+#ifdef CONFIG_CMD_NET
        if (label->ipappend & 0x2) {
                int err;
                strcpy(mac_str, " BOOTIF=");
                err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
                if (err < 0)
                        mac_str[0] = '\0';
-               len += strlen(mac_str);
        }
+#endif
 
-       if (label->append)
-               len += strlen(label->append);
+       if ((label->ipappend & 0x3) || label->append) {
+               char bootargs[CONFIG_SYS_CBSIZE] = "";
+               char finalbootargs[CONFIG_SYS_CBSIZE];
 
-       if (len) {
-               bootargs = malloc(len + 1);
-               if (!bootargs)
+               if (strlen(label->append ?: "") +
+                   strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
+                       printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
+                              strlen(label->append ?: ""),
+                              strlen(ip_str), strlen(mac_str),
+                              sizeof(bootargs));
                        return 1;
-               bootargs[0] = '\0';
+               }
+
                if (label->append)
                        strcpy(bootargs, label->append);
                strcat(bootargs, ip_str);
                strcat(bootargs, mac_str);
 
-               setenv("bootargs", bootargs);
-               printf("append: %s\n", bootargs);
-
-               free(bootargs);
+               cli_simple_process_macros(bootargs, finalbootargs);
+               setenv("bootargs", finalbootargs);
+               printf("append: %s\n", finalbootargs);
        }
 
        bootm_argv[1] = getenv("kernel_addr_r");
@@ -672,23 +711,84 @@ static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
        bootm_argv[3] = getenv("fdt_addr_r");
 
        /* if fdt label is defined then get fdt from server */
-       if (bootm_argv[3] && label->fdt) {
-               if (get_relfile_envaddr(cmdtp, label->fdt, "fdt_addr_r") < 0) {
-                       printf("Skipping %s for failure retrieving fdt\n",
-                                       label->name);
-                       return 1;
+       if (bootm_argv[3]) {
+               char *fdtfile = NULL;
+               char *fdtfilefree = NULL;
+
+               if (label->fdt) {
+                       fdtfile = label->fdt;
+               } else if (label->fdtdir) {
+                       char *f1, *f2, *f3, *f4, *slash;
+
+                       f1 = getenv("fdtfile");
+                       if (f1) {
+                               f2 = "";
+                               f3 = "";
+                               f4 = "";
+                       } else {
+                               /*
+                                * For complex cases where this code doesn't
+                                * generate the correct filename, the board
+                                * code should set $fdtfile during early boot,
+                                * or the boot scripts should set $fdtfile
+                                * before invoking "pxe" or "sysboot".
+                                */
+                               f1 = getenv("soc");
+                               f2 = "-";
+                               f3 = getenv("board");
+                               f4 = ".dtb";
+                       }
+
+                       len = strlen(label->fdtdir);
+                       if (!len)
+                               slash = "./";
+                       else if (label->fdtdir[len - 1] != '/')
+                               slash = "/";
+                       else
+                               slash = "";
+
+                       len = strlen(label->fdtdir) + strlen(slash) +
+                               strlen(f1) + strlen(f2) + strlen(f3) +
+                               strlen(f4) + 1;
+                       fdtfilefree = malloc(len);
+                       if (!fdtfilefree) {
+                               printf("malloc fail (FDT filename)\n");
+                               return 1;
+                       }
+
+                       snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
+                                label->fdtdir, slash, f1, f2, f3, f4);
+                       fdtfile = fdtfilefree;
                }
-       } else
+
+               if (fdtfile) {
+                       int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
+                       free(fdtfilefree);
+                       if (err < 0) {
+                               printf("Skipping %s for failure retrieving fdt\n",
+                                               label->name);
+                               return 1;
+                       }
+               } else {
+                       bootm_argv[3] = NULL;
+               }
+       }
+
+       if (!bootm_argv[3])
                bootm_argv[3] = getenv("fdt_addr");
 
        if (bootm_argv[3])
                bootm_argc = 4;
 
-       do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
-
+       kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
+       buf = map_sysmem(kernel_addr, 0);
+       /* Try bootm for legacy and FIT format image */
+       if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
+               do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
 #ifdef CONFIG_CMD_BOOTZ
-       /* Try booting a zImage if do_bootm returns */
-       do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
+       /* Try booting a zImage */
+       else
+               do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
 #endif
        return 1;
 }
@@ -713,6 +813,7 @@ enum token_type {
        T_PROMPT,
        T_INCLUDE,
        T_FDT,
+       T_FDTDIR,
        T_ONTIMEOUT,
        T_IPAPPEND,
        T_INVALID
@@ -742,7 +843,10 @@ static const struct token keywords[] = {
        {"append", T_APPEND},
        {"initrd", T_INITRD},
        {"include", T_INCLUDE},
+       {"devicetree", T_FDT},
        {"fdt", T_FDT},
+       {"devicetreedir", T_FDTDIR},
+       {"fdtdir", T_FDTDIR},
        {"ontimeout", T_ONTIMEOUT,},
        {"ipappend", T_IPAPPEND,},
        {NULL, T_INVALID}
@@ -1131,6 +1235,11 @@ static int parse_label(char **c, struct pxe_menu *cfg)
                                err = parse_sliteral(c, &label->fdt);
                        break;
 
+               case T_FDTDIR:
+                       if (!label->fdtdir)
+                               err = parse_sliteral(c, &label->fdtdir);
+                       break;
+
                case T_LOCALBOOT:
                        label->localboot = 1;
                        err = parse_integer(c, &label->localboot_val);
@@ -1416,6 +1525,7 @@ static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
        boot_unattempted_labels(cmdtp, cfg);
 }
 
+#ifdef CONFIG_CMD_NET
 /*
  * Boots a system using a pxe file
  *
@@ -1457,6 +1567,8 @@ do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
        destroy_pxe_menu(cfg);
 
+       copy_filename(BootFile, "", sizeof(BootFile));
+
        return 0;
 }
 
@@ -1465,13 +1577,15 @@ static cmd_tbl_t cmd_pxe_sub[] = {
        U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
 };
 
-int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        cmd_tbl_t *cp;
 
        if (argc < 2)
                return CMD_RET_USAGE;
 
+       is_pxe = true;
+
        /* drop initial "pxe" arg */
        argc--;
        argv++;
@@ -1490,13 +1604,14 @@ U_BOOT_CMD(
        "get - try to retrieve a pxe file using tftp\npxe "
        "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
 );
+#endif
 
 /*
  * Boots a system using a local disk syslinux/extlinux file
  *
  * Returns 0 on success, 1 on error.
  */
-int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        unsigned long pxefile_addr_r;
        struct pxe_menu *cfg;
@@ -1504,6 +1619,8 @@ int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        char *filename;
        int prompt = 0;
 
+       is_pxe = false;
+
        if (strstr(argv[1], "-p")) {
                prompt = 1;
                argc--;
@@ -1532,6 +1649,8 @@ int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                do_getfile = do_get_ext2;
        else if (strstr(argv[3], "fat"))
                do_getfile = do_get_fat;
+       else if (strstr(argv[3], "any"))
+               do_getfile = do_get_any;
        else {
                printf("Invalid filesystem: %s\n", argv[3]);
                return 1;
@@ -1569,7 +1688,7 @@ int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD(
        sysboot, 7, 1, do_sysboot,
        "command to get and boot from syslinux files",
-       "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
-       "    - load and parse syslinux menu file 'filename' from ext2 or fat\n"
-       "      filesystem on 'dev' on 'interface' to address 'addr'"
+       "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
+       "    - load and parse syslinux menu file 'filename' from ext2, fat\n"
+       "      or any filesystem on 'dev' on 'interface' to address 'addr'"
 );