Lower DTB load address to support older models with less RAM
[rpi-open-firmware.git] / arm_chainloader / loader.cc
index a4f6098..bf0afa4 100644 (file)
@@ -1,5 +1,5 @@
 /*=============================================================================
-Copyright (C) 2016 Kristina Brooks
+Copyright (C) 2016-2017 Authors of rpi-open-firmware
 All rights reserved.
 
 This program is free software; you can redistribute it and/or
@@ -20,6 +20,7 @@ Second stage bootloader.
 #include <drivers/fatfs/ff.h>
 #include <chainloader.h>
 #include <drivers/mailbox.hpp>
+#include <drivers/block_device.hpp>
 #include <libfdt.h>
 #include <memory_map.h>
 
@@ -28,63 +29,57 @@ Second stage bootloader.
 FATFS g_BootVolumeFs;
 
 #define ROOT_VOLUME_PREFIX "0:"
+#define DTB_LOAD_ADDRESS    0xF000000
 #define KERNEL_LOAD_ADDRESS 0x2000000
 
-extern "C" {
-       void boot_linux(int zero, int machineID, void* dtb, void* kernel);
-}
+typedef void (*linux_t)(uint32_t, uint32_t, void*);
 
 static_assert((MEM_USABLE_START+0x800000) < KERNEL_LOAD_ADDRESS,
-       "memory layout would not allow for kernel to be loaded at KERNEL_LOAD_ADDRESS, please check memory_map.h");
+              "memory layout would not allow for kernel to be loaded at KERNEL_LOAD_ADDRESS, please check memory_map.h");
 
 struct LoaderImpl {
        inline bool file_exists(const char* path) {
                return f_stat(path, NULL) == FR_OK;
        }
 
-       bool read_file(const char* path, uint8_t*& dest, size_t& size, bool should_alloc = true) {
+       size_t read_file(const char* path, uint8_t*& dest, bool should_alloc = true) {
                /* ensure file exists first */
                if(!file_exists(path))
-                       return false;
+                       panic("attempted to read %s, but it does not exist", path);
 
                /* read entire file into buffer */
-               FIL fp; 
+               FIL fp;
                f_open(&fp, path, FA_READ);
 
                unsigned int len = f_size(&fp);
 
                if(should_alloc) {
-                       uint8_t* buffer = new uint8_t[len];
+                       /*
+                        * since this can be used for strings, there's no harm in reserving an
+                        * extra byte for the null terminator and appending it.
+                        */
+                       uint8_t* buffer = new uint8_t[len + 1];
                        dest = buffer;
+                       buffer[len] = 0;
                }
 
                logf("%s: reading %d bytes to 0x%X (allocated=%d) ...\n", path, len, (unsigned int)dest, should_alloc);
 
-               size = len;
-
                f_read(&fp, dest, len, &len);
                f_close(&fp);
 
-               return true;
-       }
-
-       inline bool read_file(const char* path, uint8_t*& dest, bool should_alloc = true) {
-               size_t size;
-               return read_file(path, dest, size, should_alloc);
+               return len;
        }
 
-       void* load_fdt(const char* filename, uint8_t* cmdline) {
+       uint8_t* load_fdt(const char* filename, uint8_t* cmdline) {
                /* read device tree blob */
-               uint8_t* fdt = reinterpret_cast<uint8_t*>(MEM_USABLE_START);
-               size_t sz;
-
-               if(!read_file(filename, fdt, sz, false)) {
-                       panic("error reading fdt");
-               }
+               uint8_t* fdt = reinterpret_cast<uint8_t*>(DTB_LOAD_ADDRESS);
+               size_t sz = read_file(filename, fdt, false);
+               logf("FDT loaded at %X\n", (unsigned int) fdt);
 
-                void* v_fdt = reinterpret_cast<void*>(fdt);
+               void* v_fdt = reinterpret_cast<void*>(fdt);
 
-                int res;
+               int res;
 
                if ((res = fdt_check_header(v_fdt)) != 0) {
                        panic("fdt blob invalid, fdt_check_header returned %d", res);
@@ -92,21 +87,37 @@ struct LoaderImpl {
 
                /* pass in command line args */
                res = fdt_open_into(v_fdt, v_fdt, sz + 4096);
-               logf("fdt_open_into(): %d\n", res);
 
                int node = fdt_path_offset(v_fdt, "/chosen");
-               logf("/chosen node: %d\n", node);
                if (node < 0)
-                       return NULL;
+                       panic("no chosen node in fdt");
+
                res = fdt_setprop(v_fdt, node, "bootargs", cmdline, strlen((char*) cmdline) + 1);
-               logf("fdt_setprop(): %d\n", res);
 
-               logf("valid fdt loaded at 0x%X\n", (unsigned int)fdt);
+               /* pass in a memory map, skipping first meg for bootcode */
+               int memory = fdt_path_offset(v_fdt, "/memory");
+               if(memory < 0)
+                       panic("no memory node in fdt");
+
+               /* start the memory map at 1M/16 and grow continuous for 256M
+                * TODO: does this disrupt I/O? */
+
+               char dtype[] = "memory";
+               uint8_t memmap[] = { 0x00, 0x00, 0x01, 0x00, 0x30, 0x00, 0x00, 0x00 };
+               res = fdt_setprop(v_fdt, memory, "reg", (void*) memmap, sizeof(memmap));
+
+               logf("(valid) fdt loaded at 0x%X\n", (unsigned int)fdt);
 
-               return reinterpret_cast<void*>(fdt);
+               return fdt;
        }
 
-       LoaderImpl() {  
+       void teardown_hardware() {
+               BlockDevice* bd = get_sdhost_device();
+               if (bd)
+                       bd->stop();
+       }
+
+       LoaderImpl() {
                logf("Mounting boot partitiion ...\n");
                FRESULT r = f_mount(&g_BootVolumeFs, ROOT_VOLUME_PREFIX, 1);
                if (r != FR_OK) {
@@ -114,40 +125,37 @@ struct LoaderImpl {
                }
                logf("Boot partition mounted!\n");
 
-               size_t sz;
-
-               /* dump cmdline.txt for test */
+               /* read the command-line null-terminated */
                uint8_t* cmdline;
+               size_t cmdlen = read_file("cmdline.txt", cmdline);
 
-               if(!read_file("cmdline.txt", cmdline, sz)) {
-                       panic("error reading cmdline");
-               }
-
-               /* nul terminate it */
-               cmdline[sz - 1] = 0;
                logf("kernel cmdline: %s\n", cmdline);
-               
+
                /* load flat device tree */
-               void* fdt = load_fdt("rpi.dtb", cmdline);
-               if (!fdt) {
-                       panic("fdt pointer is null");
-               }
+               uint8_t* fdt = load_fdt("rpi.dtb", cmdline);
 
-               /* after load_fdt is done with it, we don't need it anymore */
+               /* once the fdt contains the cmdline, it is not needed */
                delete[] cmdline;
 
-               /* read the kernel -- necessarily at fixed address */
+               /* read the kernel as a function pointer at fixed address */
                uint8_t* zImage = reinterpret_cast<uint8_t*>(KERNEL_LOAD_ADDRESS);
+               linux_t kernel = reinterpret_cast<linux_t>(zImage);
 
-               if(!read_file("zImage", zImage, false)) {
-                       panic("error reading zImage");
+               size_t ksize = read_file("zImage", zImage, false);
+               logf("zImage loaded at 0x%X\n", (unsigned int)kernel);
+
+               /* flush the cache */
+               logf("Flushing....\n")
+               for (uint8_t* i = zImage; i < zImage + ksize; i += 32) {
+                       __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,1" : : "r" (i) : "memory");
                }
 
-               logf("zImage loaded at 0x%X\n", (unsigned int)zImage);
+               /* the eMMC card in particular needs to be reset */
+               teardown_hardware();
+
+               /* fire away -- this should never return */
                logf("Jumping to the Linux kernel...\n");
-               
-               /* this should never return */
-               boot_linux(0, ~0, fdt, zImage);
+               kernel(0, ~0, fdt);
        }
 };
 
This page took 0.032323 seconds and 4 git commands to generate.