summaryrefslogtreecommitdiff
path: root/bootloader/bootloader.c
blob: b3d3cd4f864b46be86c8cb3f93b1fe2fbb9f3406 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define LOG_LEVEL 5

#include "lib.h"

struct PACKED payload {
  uint64_t entry;
  uint32_t size;
  uint32_t cksum;
  uint8_t data[];
};

extern const struct payload payload;

#define CLOCK 16000000
#define CKSUM 1

void kmain() {
  {
    TRACE("uart init\n");
    uart_init(115200);

    gpio_set_function(14, GPIO_UART);
    gpio_set_function(15, GPIO_UART);

    uartmux_configure(uartmux_signal_number(14), 0, UARTMUX_TX);
    uartmux_configure(uartmux_signal_number(15), 0, UARTMUX_RX);
  }
  INFO("bootloader init\n");
  {
    TRACE("psram init\n");
    psram_init();
    TRACE("psram check\n");
    volatile u32 *psram = (volatile u32 *)0x50000000;
    put32(psram, 0xdeadbeef);
    u32 x = get32(psram);
    DEBUG("read back 0x%x from psram\n", x);
    if (x != 0xdeadbeef) {
      ERROR("psram init failed!\n");
      while(1);
    }
    INFO("psram init success\n");
  }
  TRACE("timer init\n");
  timer_init(CLOCK);

  INFO("addr = 0x%lx\n", payload.entry);
  INFO("size = 0x%x\n", payload.size);
  INFO("cksum = 0x%x\n", payload.cksum);

  DEBUG("copying %d bytes of code to address %p\n", payload.size, payload.entry);
  u64 start = timer_read();
  memcpy((void *)payload.entry, payload.data, payload.size);
  u64 end = timer_read();
  u64 ms = (end - start) * 1000 / CLOCK;
  DEBUG("memcpy took %d ms\n", ms);

#if CKSUM
  start = timer_read();
  uint32_t cksum = crc32((void *)payload.entry, payload.size);
  end = timer_read();
  ms = (end - start) * 1000 / CLOCK;
  DEBUG("crc32 took %d ms\n", ms);
  if (payload.cksum != cksum) {
    ERROR("invalid checksum: got 0x%x, expected 0x%x\n", cksum, payload.cksum);
    ERROR("refusing to boot\n");
    while(1);
  } else {
    INFO("valid checksum: 0x%x\n", cksum);
  }
#else
#warning "Checksum verification disabled."
  WARN("not verifying checksum\n");
#endif

  TRACE("branching to code\n");
  void (*fn)(void) = (void (*)(void))payload.entry;
  fn();
  WARN("code returned to bootloader\n");
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback