]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_bootm.c
Improve the bootm command for CONFIG_OF_LIBFDT
[karo-tx-uboot.git] / common / cmd_bootm.c
index e8895c68e9055eaefbd2cb0c9bc8383db6ccbc05..32c29e55a37bc829d16226590d94f63adc934ef0 100644 (file)
 #include <environment.h>
 #include <asm/byteorder.h>
 
-#ifdef CONFIG_OF_FLAT_TREE
+#if defined(CONFIG_OF_LIBFDT)
+#include <fdt.h>
+#include <libfdt.h>
+#include <fdt_support.h>
+#endif
+#if defined(CONFIG_OF_FLAT_TREE)
 #include <ft_build.h>
 #endif
 
@@ -242,26 +247,26 @@ int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 
        len_ptr = (ulong *)data;
 
-#if defined(__PPC__)
-       if (hdr->ih_arch != IH_CPU_PPC)
-#elif defined(__ARM__)
+#if defined(__ARM__)
        if (hdr->ih_arch != IH_CPU_ARM)
+#elif defined(__avr32__)
+       if (hdr->ih_arch != IH_CPU_AVR32)
+#elif defined(__bfin__)
+       if (hdr->ih_arch != IH_CPU_BLACKFIN)
 #elif defined(__I386__)
        if (hdr->ih_arch != IH_CPU_I386)
-#elif defined(__mips__)
-       if (hdr->ih_arch != IH_CPU_MIPS)
-#elif defined(__nios__)
-       if (hdr->ih_arch != IH_CPU_NIOS)
 #elif defined(__M68K__)
        if (hdr->ih_arch != IH_CPU_M68K)
 #elif defined(__microblaze__)
        if (hdr->ih_arch != IH_CPU_MICROBLAZE)
+#elif defined(__mips__)
+       if (hdr->ih_arch != IH_CPU_MIPS)
+#elif defined(__nios__)
+       if (hdr->ih_arch != IH_CPU_NIOS)
 #elif defined(__nios2__)
        if (hdr->ih_arch != IH_CPU_NIOS2)
-#elif defined(__blackfin__)
-       if (hdr->ih_arch != IH_CPU_BLACKFIN)
-#elif defined(__avr32__)
-       if (hdr->ih_arch != IH_CPU_AVR32)
+#elif defined(__PPC__)
+       if (hdr->ih_arch != IH_CPU_PPC)
 #else
 # error Unknown CPU type
 #endif
@@ -467,7 +472,7 @@ U_BOOT_CMD(
        "[addr [arg ...]]\n    - boot application image stored in memory\n"
        "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
        "\t'arg' can be the address of an initrd image\n"
-#ifdef CONFIG_OF_FLAT_TREE
+#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
        "\tWhen booting a Linux kernel which requires a flat device-tree\n"
        "\ta third argument is required which is the address of the of the\n"
        "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
@@ -529,8 +534,9 @@ do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
        bd_t    *kbd;
        void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
        image_header_t *hdr = &header;
-#ifdef CONFIG_OF_FLAT_TREE
+#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
        char    *of_flat_tree = NULL;
+       ulong   of_data = 0;
 #endif
 
        if ((s = getenv ("initrd_high")) != NULL) {
@@ -621,7 +627,7 @@ do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
         * Check if there is an initrd image
         */
 
-#ifdef CONFIG_OF_FLAT_TREE
+#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
        /* Look for a '-' which indicates to ignore the ramdisk argument */
        if (argc >= 3 && strcmp(argv[2], "-") ==  0) {
                        debug ("Skipping initrd\n");
@@ -738,18 +744,18 @@ do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
                len = data = 0;
        }
 
-#ifdef CONFIG_OF_FLAT_TREE
+#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
        if(argc > 3) {
                of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
                hdr = (image_header_t *)of_flat_tree;
-
-               if  (*(ulong *)of_flat_tree == OF_DT_HEADER) {
+#if defined(CONFIG_OF_LIBFDT)
+               if (fdt_check_header(of_flat_tree) == 0) {
+#else
+               if (*(ulong *)of_flat_tree == OF_DT_HEADER) {
+#endif
 #ifndef CFG_NO_FLASH
-                       if (addr2info((ulong)of_flat_tree) != NULL) {
-                               printf ("Cannot modify flat device tree stored in flash\n" \
-                                       "Copy to memory before using the bootm command\n");
-                               return;
-                       }
+                       if (addr2info((ulong)of_flat_tree) != NULL)
+                               of_data = (ulong)of_flat_tree;
 #endif
                } else if (ntohl(hdr->ih_magic) == IH_MAGIC) {
                        printf("## Flat Device Tree Image at %08lX\n", hdr);
@@ -789,7 +795,11 @@ do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
                                printf("ERROR: uImage is not uncompressed\n");
                                return;
                        }
+#if defined(CONFIG_OF_LIBFDT)
+                       if (fdt_check_header(of_flat_tree + sizeof(image_header_t)) == 0) {
+#else
                        if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
+#endif
                                printf ("ERROR: uImage data is not a flat device tree\n");
                                return;
                        }
@@ -804,9 +814,45 @@ do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
                }
                printf ("   Booting using flat device tree at 0x%x\n",
                                of_flat_tree);
-       } else if(getenv("disable_of") == NULL) {
-               printf ("ERROR: bootm needs flat device tree as third argument\n");
-               return;
+       } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
+               u_long tail    = ntohl(len_ptr[0]) % 4;
+               int i;
+
+               /* skip kernel length, initrd length, and terminator */
+               of_data = (ulong)(&len_ptr[3]);
+               /* skip any additional image length fields */
+               for (i=2; len_ptr[i]; ++i)
+                       of_data += 4;
+               /* add kernel length, and align */
+               of_data += ntohl(len_ptr[0]);
+               if (tail) {
+                       of_data += 4 - tail;
+               }
+
+               /* add initrd length, and align */
+               tail = ntohl(len_ptr[1]) % 4;
+               of_data += ntohl(len_ptr[1]);
+               if (tail) {
+                       of_data += 4 - tail;
+               }
+
+#if defined(CONFIG_OF_LIBFDT)
+               if (fdt_check_header((void *)of_data) != 0) {
+#else
+               if (((struct boot_param_header *)of_data)->magic != OF_DT_HEADER) {
+#endif
+                       printf ("ERROR: image is not a flat device tree\n");
+                       return;
+               }
+
+#if defined(CONFIG_OF_LIBFDT)
+               if (be32_to_cpu(fdt_totalsize(of_data)) !=  ntohl(len_ptr[2])) {
+#else
+               if (((struct boot_param_header *)of_data)->totalsize != ntohl(len_ptr[2])) {
+#endif
+                       printf ("ERROR: flat device tree size does not agree with image\n");
+                       return;
+               }
        }
 #endif
        if (!data) {
@@ -883,46 +929,123 @@ do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
 
        SHOW_BOOT_PROGRESS (15);
 
-#ifndef CONFIG_OF_FLAT_TREE
-
 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
        unlock_ram_in_cache();
 #endif
 
+#if defined(CONFIG_OF_LIBFDT)
+       /* move of_flat_tree if needed */
+       if (of_data) {
+               int err;
+               ulong of_start, of_len;
+
+               of_len = be32_to_cpu(fdt_totalsize(of_data));
+               /* position on a 4K boundary before the initrd/kbd */
+               if (initrd_start)
+                       of_start = initrd_start - of_len;
+               else
+                       of_start  = (ulong)kbd - of_len;
+               of_start &= ~(4096 - 1);        /* align on page */
+               debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
+                       of_data, of_data + of_len - 1, of_len, of_len);
+
+               of_flat_tree = (char *)of_start;
+               printf ("   Loading Device Tree to %08lx, end %08lx ... ",
+                       of_start, of_start + of_len - 1);
+               err = fdt_open_into((void *)of_start, (void *)of_data, of_len);
+               if (err != 0) {
+                       printf ("libfdt: %s " __FILE__ " %d\n", fdt_strerror(err), __LINE__);
+               }
+               /*
+                * Add the chosen node if it doesn't exist, add the env and bd_t
+                * if the user wants it (the logic is in the subroutines).
+                */
+               if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
+                               printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
+                               return;
+               }
+#ifdef CONFIG_OF_HAS_UBOOT_ENV
+               if (fdt_env(of_flat_tree) < 0) {
+                               printf("Failed creating the /u-boot-env node, aborting.\n");
+                               return;
+               }
+#endif
+#ifdef CONFIG_OF_HAS_BD_T
+               if (fdt_bd_t(of_flat_tree) < 0) {
+                               printf("Failed creating the /bd_t node, aborting.\n");
+                               return;
+               }
+#endif
+       }
+#endif
+#if defined(CONFIG_OF_FLAT_TREE)
+       /* move of_flat_tree if needed */
+       if (of_data) {
+               ulong of_start, of_len;
+               of_len = ((struct boot_param_header *)of_data)->totalsize;
+               /* provide extra 8k pad */
+               if (initrd_start)
+                       of_start = initrd_start - of_len - 8192;
+               else
+                       of_start  = (ulong)kbd - of_len - 8192;
+               of_start &= ~(4096 - 1);        /* align on page */
+               debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
+                       of_data, of_data + of_len - 1, of_len, of_len);
+
+               of_flat_tree = (char *)of_start;
+               printf ("   Loading Device Tree to %08lx, end %08lx ... ",
+                       of_start, of_start + of_len - 1);
+               memmove ((void *)of_start, (void *)of_data, of_len);
+       }
+#endif
+
        /*
-        * Linux Kernel Parameters:
+        * Linux Kernel Parameters (passing board info data):
         *   r3: ptr to board info data
         *   r4: initrd_start or 0 if no initrd
         *   r5: initrd_end - unused if r4 is 0
         *   r6: Start of command line string
         *   r7: End   of command line string
         */
-       (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
-
-#else
-       ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
-       /* ft_dump_blob(of_flat_tree); */
-
-#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
-       unlock_ram_in_cache();
+#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
+       if (!of_flat_tree)      /* no device tree; boot old style */
 #endif
+               (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
+               /* does not return */
+
+#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
        /*
-        * Linux Kernel Parameters:
+        * Linux Kernel Parameters (passing device tree):
         *   r3: ptr to OF flat tree, followed by the board info data
         *   r4: physical pointer to the kernel itself
         *   r5: NULL
         *   r6: NULL
         *   r7: NULL
         */
-       if (getenv("disable_of") != NULL)
-               (*kernel) ((bd_t *)of_flat_tree, initrd_start, initrd_end,
-                       cmd_start, cmd_end);
-       else {
-               ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
-               /* ft_dump_blob(of_flat_tree); */
-               (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
+#if defined(CONFIG_OF_FLAT_TREE)
+       ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
+       /* ft_dump_blob(of_flat_tree); */
+#endif
+#if defined(CONFIG_OF_LIBFDT)
+       if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
+               printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
+               return;
+       }
+#ifdef CONFIG_OF_HAS_UBOOT_ENV
+       if (fdt_env(of_flat_tree) < 0) {
+               printf("Failed creating the /u-boot-env node, aborting.\n");
+               return;
        }
-               
+#endif
+#ifdef CONFIG_OF_HAS_BD_T
+       if (fdt_bd_t(of_flat_tree) < 0) {
+               printf("Failed creating the /bd_t node, aborting.\n");
+               return;
+       }
+#endif
+#endif /* if defined(CONFIG_OF_LIBFDT) */
+
+       (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
 #endif
 }
 #endif /* CONFIG_PPC */
@@ -1316,19 +1439,20 @@ print_type (image_header_t *hdr)
        case IH_CPU_ALPHA:      arch = "Alpha";                 break;
        case IH_CPU_ARM:        arch = "ARM";                   break;
        case IH_CPU_AVR32:      arch = "AVR32";                 break;
+       case IH_CPU_BLACKFIN:   arch = "Blackfin";              break;
        case IH_CPU_I386:       arch = "Intel x86";             break;
        case IH_CPU_IA64:       arch = "IA64";                  break;
-       case IH_CPU_MIPS:       arch = "MIPS";                  break;
+       case IH_CPU_M68K:       arch = "M68K";                  break;
+       case IH_CPU_MICROBLAZE: arch = "Microblaze";            break;
        case IH_CPU_MIPS64:     arch = "MIPS 64 Bit";           break;
+       case IH_CPU_MIPS:       arch = "MIPS";                  break;
+       case IH_CPU_NIOS2:      arch = "Nios-II";               break;
+       case IH_CPU_NIOS:       arch = "Nios";                  break;
        case IH_CPU_PPC:        arch = "PowerPC";               break;
        case IH_CPU_S390:       arch = "IBM S390";              break;
        case IH_CPU_SH:         arch = "SuperH";                break;
-       case IH_CPU_SPARC:      arch = "SPARC";                 break;
        case IH_CPU_SPARC64:    arch = "SPARC 64 Bit";          break;
-       case IH_CPU_M68K:       arch = "M68K";                  break;
-       case IH_CPU_MICROBLAZE: arch = "Microblaze";            break;
-       case IH_CPU_NIOS:       arch = "Nios";                  break;
-       case IH_CPU_NIOS2:      arch = "Nios-II";               break;
+       case IH_CPU_SPARC:      arch = "SPARC";                 break;
        default:                arch = "Unknown Architecture";  break;
        }