fixed build issues in Ubuntu 16.04
[rpi-open-firmware.git] / arm_chainloader / loader.cc
1 /*=============================================================================
2 Copyright (C) 2016-2017 Authors of rpi-open-firmware
3 All rights reserved.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 FILE DESCRIPTION
16 Second stage bootloader.
17
18 =============================================================================*/
19
20 #include <string.h>
21 #include <drivers/fatfs/ff.h>
22 #include <chainloader.h>
23 #include <drivers/mailbox.hpp>
24 #include <drivers/block_device.hpp>
25 #include <libfdt.h>
26 #include <memory_map.h>
27
28 #define logf(fmt, ...) printf("[LDR:%s]: " fmt, __FUNCTION__, ##__VA_ARGS__);
29
30 FATFS g_BootVolumeFs;
31
32 #define ROOT_VOLUME_PREFIX "0:"
33 #define DTB_LOAD_ADDRESS 0xF000000
34 #define KERNEL_LOAD_ADDRESS 0x2000000
35
36 typedef void (*linux_t)(uint32_t, uint32_t, void*);
37
38 static_assert((MEM_USABLE_START+0x800000) < KERNEL_LOAD_ADDRESS,
39 "memory layout would not allow for kernel to be loaded at KERNEL_LOAD_ADDRESS, please check memory_map.h");
40
41 struct LoaderImpl {
42 inline bool file_exists(const char* path) {
43 return f_stat(path, NULL) == FR_OK;
44 }
45
46 size_t read_file(const char* path, uint8_t*& dest, bool should_alloc = true) {
47 /* ensure file exists first */
48 if(!file_exists(path))
49 panic("attempted to read %s, but it does not exist", path);
50
51 /* read entire file into buffer */
52 FIL fp;
53 f_open(&fp, path, FA_READ);
54
55 unsigned int len = f_size(&fp);
56
57 if(should_alloc) {
58 /*
59 * since this can be used for strings, there's no harm in reserving an
60 * extra byte for the null terminator and appending it.
61 */
62 uint8_t* buffer = new uint8_t[len + 1];
63 dest = buffer;
64 buffer[len] = 0;
65 }
66
67 logf("%s: reading %d bytes to 0x%X (allocated=%d) ...\n", path, len, (unsigned int)dest, should_alloc);
68
69 f_read(&fp, dest, len, &len);
70 f_close(&fp);
71
72 return len;
73 }
74
75 uint8_t* load_fdt(const char* filename, uint8_t* cmdline) {
76 /* read device tree blob */
77 uint8_t* fdt = reinterpret_cast<uint8_t*>(DTB_LOAD_ADDRESS);
78 size_t sz = read_file(filename, fdt, false);
79 logf("FDT loaded at %X\n", (unsigned int) fdt);
80
81 void* v_fdt = reinterpret_cast<void*>(fdt);
82
83 int res;
84
85 if ((res = fdt_check_header(v_fdt)) != 0) {
86 panic("fdt blob invalid, fdt_check_header returned %d", res);
87 }
88
89 /* pass in command line args */
90 res = fdt_open_into(v_fdt, v_fdt, sz + 4096);
91
92 int node = fdt_path_offset(v_fdt, "/chosen");
93 if (node < 0)
94 panic("no chosen node in fdt");
95
96 res = fdt_setprop(v_fdt, node, "bootargs", cmdline, strlen((char*) cmdline) + 1);
97
98 /* pass in a memory map, skipping first meg for bootcode */
99 int memory = fdt_path_offset(v_fdt, "/memory");
100 if(memory < 0)
101 panic("no memory node in fdt");
102
103 /* start the memory map at 1M/16 and grow continuous for 256M
104 * TODO: does this disrupt I/O? */
105
106 char dtype[] = "memory";
107 uint8_t memmap[] = { 0x00, 0x00, 0x01, 0x00, 0x30, 0x00, 0x00, 0x00 };
108 res = fdt_setprop(v_fdt, memory, "reg", (void*) memmap, sizeof(memmap));
109
110 logf("(valid) fdt loaded at 0x%X\n", (unsigned int)fdt);
111
112 return fdt;
113 }
114
115 void teardown_hardware() {
116 BlockDevice* bd = get_sdhost_device();
117 if (bd)
118 bd->stop();
119 }
120
121 LoaderImpl() {
122 logf("Mounting boot partitiion ...\n");
123 FRESULT r = f_mount(&g_BootVolumeFs, ROOT_VOLUME_PREFIX, 1);
124 if (r != FR_OK) {
125 panic("failed to mount boot partition, error: %d", (int)r);
126 }
127 logf("Boot partition mounted!\n");
128
129 /* read the command-line null-terminated */
130 uint8_t* cmdline;
131 size_t cmdlen = read_file("cmdline.txt", cmdline);
132
133 logf("kernel cmdline: %s\n", cmdline);
134
135 /* load flat device tree */
136 uint8_t* fdt = load_fdt("rpi.dtb", cmdline);
137
138 /* once the fdt contains the cmdline, it is not needed */
139 delete[] cmdline;
140
141 /* read the kernel as a function pointer at fixed address */
142 uint8_t* zImage = reinterpret_cast<uint8_t*>(KERNEL_LOAD_ADDRESS);
143 linux_t kernel = reinterpret_cast<linux_t>(zImage);
144
145 size_t ksize = read_file("zImage", zImage, false);
146 logf("zImage loaded at 0x%X\n", (unsigned int)kernel);
147
148 /* flush the cache */
149 logf("Flushing....\n")
150 for (uint8_t* i = zImage; i < zImage + ksize; i += 32) {
151 __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,1" : : "r" (i) : "memory");
152 }
153
154 /* the eMMC card in particular needs to be reset */
155 teardown_hardware();
156
157 /* fire away -- this should never return */
158 logf("Jumping to the Linux kernel...\n");
159 kernel(0, ~0, fdt);
160 }
161 };
162
163 static LoaderImpl STATIC_APP g_Loader {};
This page took 0.088358 seconds and 4 git commands to generate.