Merge pull request #18 from vamposdecampos/fdt-bootargs
[rpi-open-firmware.git] / arm_chainloader / loader.cc
1 /*=============================================================================
2 Copyright (C) 2016 Kristina Brooks
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 <libfdt.h>
24 #include <memory_map.h>
25
26 #define logf(fmt, ...) printf("[LDR:%s]: " fmt, __FUNCTION__, ##__VA_ARGS__);
27
28 FATFS g_BootVolumeFs;
29
30 #define ROOT_VOLUME_PREFIX "0:"
31 #define KERNEL_LOAD_ADDRESS 0x2000000
32
33 extern "C" {
34 void boot_linux(int zero, int machineID, void* dtb, void* kernel);
35 }
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 bool read_file(const char* path, uint8_t*& dest, size_t& size, bool should_alloc = true) {
46 /* ensure file exists first */
47 if(!file_exists(path))
48 return false;
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 size = len;
64
65 f_read(&fp, dest, len, &len);
66 f_close(&fp);
67
68 return true;
69 }
70
71 inline bool read_file(const char* path, uint8_t*& dest, bool should_alloc = true) {
72 size_t size;
73 return read_file(path, dest, size, should_alloc);
74 }
75
76 void* load_fdt(const char* filename, uint8_t* cmdline) {
77 /* read device tree blob */
78 uint8_t* fdt = reinterpret_cast<uint8_t*>(MEM_USABLE_START);
79 size_t sz;
80
81 if(!read_file(filename, fdt, sz, false)) {
82 panic("error reading fdt");
83 }
84
85 void* v_fdt = reinterpret_cast<void*>(fdt);
86
87 int res;
88
89 if ((res = fdt_check_header(v_fdt)) != 0) {
90 panic("fdt blob invalid, fdt_check_header returned %d", res);
91 }
92
93 /* pass in command line args */
94 res = fdt_open_into(v_fdt, v_fdt, sz + 4096);
95 logf("fdt_open_into(): %d\n", res);
96
97 int node = fdt_path_offset(v_fdt, "/chosen");
98 logf("/chosen node: %d\n", node);
99 if (node < 0)
100 return NULL;
101 res = fdt_setprop(v_fdt, node, "bootargs", cmdline, strlen((char*) cmdline) + 1);
102 logf("fdt_setprop(): %d\n", res);
103
104 logf("valid fdt loaded at 0x%X\n", (unsigned int)fdt);
105
106 return reinterpret_cast<void*>(fdt);
107 }
108
109 LoaderImpl() {
110 logf("Mounting boot partitiion ...\n");
111 FRESULT r = f_mount(&g_BootVolumeFs, ROOT_VOLUME_PREFIX, 1);
112 if (r != FR_OK) {
113 panic("failed to mount boot partition, error: %d", (int)r);
114 }
115 logf("Boot partition mounted!\n");
116
117 size_t sz;
118
119 /* dump cmdline.txt for test */
120 uint8_t* cmdline;
121
122 if(!read_file("cmdline.txt", cmdline, sz)) {
123 panic("error reading cmdline");
124 }
125
126 /* nul terminate it */
127 cmdline[sz - 1] = 0;
128 logf("kernel cmdline: %s\n", cmdline);
129
130 /* load flat device tree */
131 void* fdt = load_fdt("rpi.dtb", cmdline);
132 if (!fdt) {
133 panic("fdt pointer is null");
134 }
135
136 /* after load_fdt is done with it, we don't need it anymore */
137 delete[] cmdline;
138
139 /* read the kernel -- necessarily at fixed address */
140 uint8_t* zImage = reinterpret_cast<uint8_t*>(KERNEL_LOAD_ADDRESS);
141
142 if(!read_file("zImage", zImage, false)) {
143 panic("error reading zImage");
144 }
145
146 logf("zImage loaded at 0x%X\n", (unsigned int)zImage);
147 logf("Jumping to the Linux kernel...\n");
148
149 /* this should never return */
150 boot_linux(0, ~0, fdt, zImage);
151 }
152 };
153
154 static LoaderImpl STATIC_APP g_Loader {};
This page took 0.086748 seconds and 5 git commands to generate.