unified VC4 and ARM runtime code (including C++ runtime), updated some minor bits
[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 <drivers/fatfs/ff.h>
21 #include <chainloader.h>
22 #include <drivers/mailbox.hpp>
23 #include <drivers/block_device.hpp>
24 #include <libfdt.h>
25 #include <memory_map.h>
26
27 #define logf(fmt, ...) printf("[LDR:%s]: " fmt, __FUNCTION__, ##__VA_ARGS__);
28
29 FATFS g_BootVolumeFs;
30
31 #define ROOT_VOLUME_PREFIX "0:"
32 #define DTB_LOAD_ADDRESS 0x20000000
33 #define KERNEL_LOAD_ADDRESS 0x2000000
34
35 typedef void (*linux_t)(uint32_t, uint32_t, void*);
36
37 static_assert((MEM_USABLE_START+0x800000) < KERNEL_LOAD_ADDRESS,
38 "memory layout would not allow for kernel to be loaded at KERNEL_LOAD_ADDRESS, please check memory_map.h");
39
40 struct LoaderImpl {
41 inline bool file_exists(const char* path) {
42 return f_stat(path, NULL) == FR_OK;
43 }
44
45 size_t read_file(const char* path, uint8_t*& dest, bool should_alloc = true) {
46 /* ensure file exists first */
47 if(!file_exists(path))
48 panic("attempted to read %s, but it does not exist", path);
49
50 /* read entire file into buffer */
51 FIL fp;
52 f_open(&fp, path, FA_READ);
53
54 unsigned int len = f_size(&fp);
55
56 if(should_alloc) {
57 uint8_t* buffer = new uint8_t[len];
58 dest = buffer;
59 }
60
61 logf("%s: reading %d bytes to 0x%X (allocated=%d) ...\n", path, len, (unsigned int)dest, should_alloc);
62
63 f_read(&fp, dest, len, &len);
64 f_close(&fp);
65
66 return len;
67 }
68
69 uint8_t* load_fdt(const char* filename, uint8_t* cmdline) {
70 /* read device tree blob */
71 uint8_t* fdt = reinterpret_cast<uint8_t*>(DTB_LOAD_ADDRESS);
72 size_t sz = read_file(filename, fdt, false);
73 logf("FDT loaded at %X\n", (unsigned int) fdt);
74
75 void* v_fdt = reinterpret_cast<void*>(fdt);
76
77 int res;
78
79 if ((res = fdt_check_header(v_fdt)) != 0) {
80 panic("fdt blob invalid, fdt_check_header returned %d", res);
81 }
82
83 /* pass in command line args */
84 res = fdt_open_into(v_fdt, v_fdt, sz + 4096);
85
86 int node = fdt_path_offset(v_fdt, "/chosen");
87 if (node < 0)
88 panic("no chosen node in fdt");
89
90 res = fdt_setprop(v_fdt, node, "bootargs", cmdline, strlen((char*) cmdline) + 1);
91
92 /* pass in a memory map, skipping first meg for bootcode */
93 int memory = fdt_path_offset(v_fdt, "/memory");
94 if(memory < 0)
95 panic("no memory node in fdt");
96
97 /* start the memory map at 1M/16 and grow continuous for 256M
98 * TODO: does this disrupt I/O? */
99
100 char dtype[] = "memory";
101 uint8_t memmap[] = { 0x00, 0x00, 0x01, 0x00, 0x30, 0x00, 0x00, 0x00 };
102 res = fdt_setprop(v_fdt, memory, "reg", (void*) memmap, sizeof(memmap));
103
104 logf("(valid) fdt loaded at 0x%X\n", (unsigned int)fdt);
105
106 return fdt;
107 }
108
109 void teardown_hardware() {
110 BlockDevice* bd = get_sdhost_device();
111 if (bd)
112 bd->stop();
113 }
114
115 LoaderImpl() {
116 logf("Mounting boot partitiion ...\n");
117 FRESULT r = f_mount(&g_BootVolumeFs, ROOT_VOLUME_PREFIX, 1);
118 if (r != FR_OK) {
119 panic("failed to mount boot partition, error: %d", (int)r);
120 }
121 logf("Boot partition mounted!\n");
122
123 /* read the command-line null-terminated */
124 uint8_t* cmdline;
125 size_t cmdlen = read_file("cmdline.txt", cmdline);
126
127 cmdline[cmdlen - 1] = 0;
128 logf("kernel cmdline: %s\n", cmdline);
129
130 /* load flat device tree */
131 uint8_t* fdt = load_fdt("rpi.dtb", cmdline);
132
133 /* once the fdt contains the cmdline, it is not needed */
134 delete[] cmdline;
135
136 /* read the kernel as a function pointer at fixed address */
137 uint8_t* zImage = reinterpret_cast<uint8_t*>(KERNEL_LOAD_ADDRESS);
138 linux_t kernel = reinterpret_cast<linux_t>(zImage);
139
140 size_t ksize = read_file("zImage", zImage, false);
141 logf("zImage loaded at 0x%X\n", (unsigned int)kernel);
142
143 /* flush the cache */
144 logf("Flushing....\n")
145 for (uint8_t* i = zImage; i < zImage + ksize; i += 32) {
146 __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,1" : : "r" (i) : "memory");
147 }
148
149 /* the eMMC card in particular needs to be reset */
150 teardown_hardware();
151
152 /* fire away -- this shoukd never return */
153 logf("Jumping to the Linux kernel...\n");
154 kernel(0, ~0, fdt);
155 }
156 };
157
158 static LoaderImpl STATIC_APP g_Loader {};
This page took 0.103841 seconds and 4 git commands to generate.