I don't know how to use my types good
[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
24 #define logf(fmt, ...) printf("[LDR:%s]: " fmt, __FUNCTION__, ##__VA_ARGS__);
25
26 FATFS g_BootVolumeFs;
27
28 #define ROOT_VOLUME_PREFIX "0:"
29
30 extern "C" {
31 void bootLinux(int zero, int machineID, void* dtb, void* kernel);
32 }
33
34 struct LoaderImpl {
35 inline bool file_exists(const char* path) {
36 return f_stat(path, NULL) == FR_OK;
37 }
38
39 bool read_file(const char* path, uint8_t** dest) {
40 /* ensure file exists first */
41 if(!file_exists(path)) return false;
42
43 /* read entire file into buffer */
44 FIL fp;
45 f_open(&fp, path, FA_READ);
46
47 unsigned int len = f_size(&fp);
48 uint8_t* buffer = (uint8_t*) malloc(len);
49
50 printf("Buffer (%X) len %d\n", (unsigned int) buffer, len);
51
52 f_read(&fp, buffer, len, &len);
53 f_close(&fp);
54
55 *dest = buffer;
56
57 return true;
58 }
59
60 LoaderImpl() {
61 logf("Mounting boot partitiion ...\n");
62 FRESULT r = f_mount(&g_BootVolumeFs, ROOT_VOLUME_PREFIX, 1);
63 if (r != FR_OK) {
64 panic("failed to mount boot partition, error: %d", (int)r);
65 }
66 logf("Boot partition mounted!\n");
67
68 /* dump cmdline.txt for test */
69 uint8_t* arguments;
70
71 if(!read_file("cmdline.txt", &arguments)) {
72 panic("Error reading cmdline arguments");
73 }
74
75 printf("\n%s\n", arguments);
76
77 free(arguments);
78
79 /* read device tree blob */
80 uint8_t* dtb;
81
82 if(!read_file("rpi.dtb", &dtb)) {
83 panic("Error reading device tree blob");
84 }
85
86 printf("DTB loaded at %X\n", (unsigned int) dtb);
87
88 /* read the kernel */
89 uint8_t* zImage;
90
91 if(!read_file("zImage", &zImage)) {
92 panic("Error reading zImage");
93 }
94
95 printf("bzImage loaded at %X\n", (unsigned int) zImage);
96
97 printf("Jumping to the Linux kernel...\n");
98
99 /* this should never return */
100 bootLinux(0, 3139, dtb, zImage);
101 }
102 };
103
104 static LoaderImpl STATIC_APP g_Loader {};
This page took 0.084013 seconds and 5 git commands to generate.