aboutsummaryrefslogtreecommitdiff
path: root/loader_stage2.c
diff options
context:
space:
mode:
authorWojtek Kosior <kwojtus@protonmail.com>2019-10-11 11:59:59 +0200
committerWojtek Kosior <kwojtus@protonmail.com>2019-10-11 11:59:59 +0200
commit23e6ba8ef9f9967e0c15c6245fd92cdd5f60fc55 (patch)
treee7c51792a025b1d9a71eba5aa0a85128515b43d4 /loader_stage2.c
parent50814e0cbd58590a7e367b604afa8c06271a273a (diff)
downloadrpi-MMU-example-23e6ba8ef9f9967e0c15c6245fd92cdd5f60fc55.tar.gz
rpi-MMU-example-23e6ba8ef9f9967e0c15c6245fd92cdd5f60fc55.zip
add initial bootloader work
Diffstat (limited to 'loader_stage2.c')
-rw-r--r--loader_stage2.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/loader_stage2.c b/loader_stage2.c
new file mode 100644
index 0000000..98ce571
--- /dev/null
+++ b/loader_stage2.c
@@ -0,0 +1,40 @@
+#include <stddef.h>
+#include <stdint.h>
+#include <uart.h>
+#include <global.h>
+
+void *const kernel_load_addr = ((void*) 0x8000);
+
+void __attribute__((section(".text.stage2main")))
+stage2(uint32_t r0, uint32_t r1, uint32_t atags)
+{
+ // Declare as unused
+ (void) r0;
+ (void) r1;
+ (void) atags;
+
+ uart_puts("hello stage2!\n\r");
+
+ // get kernel size via uart (little endian)
+ uint32_t b0, b1, b2, b3;
+
+ b0 = uart_getc();
+ b1 = uart_getc();
+ b2 = uart_getc();
+ b3 = uart_getc();
+
+ uint32_t kernel_size = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
+
+ // load kernel at kernel_load_addr
+ char *dst = kernel_load_addr, *end = dst + kernel_size;
+
+ while (dst < end)
+ *(dst++) = uart_getc();
+
+ // jump to kernel
+ // TODO also forward arguments (r0, r1, atags)
+ asm volatile("bx %0" :: "r" (kernel_load_addr) : "memory");
+}
+
+void *const _start = ((void*) stage2); // for linker script
+