aboutsummaryrefslogtreecommitdiff
path: root/loader_stage2.c
blob: e221dda126d52a39da497d4f725b40ce2dcf71ce (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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_init();

  // 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