aboutsummaryrefslogtreecommitdiff
path: root/setup.c
blob: 63ca054815c0a6b427b2302766075f52c518543c (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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "uart.h"
#include "io.h"
#include "demo_functionality.h"
#include "paging.h"
#include "atags.h"
// for POWER_OF_2() macro... perhaps the macro should be moved
#include "memory.h"

void setup(uint32_t r0, uint32_t machine_type,
	   struct atag_header *atags)
{
  uart_init();

  // When we attach screen session after loading kernel with socat
  // we miss kernel's greeting... So we'll make the kernel wait for
  // one char we're going to send from within screen
  //getchar();
  
  puts("Hello, kernel World!");

  prints("ARM machine type: 0x"); printhext(machine_type); puts("");

  uint32_t memory_size = 0;
  
  // value 3 introduced by stage1 code means no atags was found
  if (r0 == 3)
    puts("No ATAGS was found!");
  else
    {
      prints("ATAGS copied to 0x");
      printhex((uint32_t) atags); puts("");

      puts("__ ATAGS contents __");

      print_atags(atags);

      puts("__ end of ATAGS contents __");

      memory_size = find_memory_size(atags);
    }

  if (memory_size)
    {
      char *unit;
      uint32_t size_in_unit;

      if (memory_size % POWER_OF_2(10))
	{
	  unit = "B";
	  size_in_unit = memory_size;
	}
      else if (memory_size % POWER_OF_2(20))
	{
	  unit = "KB";
	  size_in_unit = memory_size / POWER_OF_2(10);
	}
      else if (memory_size % POWER_OF_2(30))
	{
	  unit = "MB";
	  size_in_unit = memory_size / POWER_OF_2(20);
	}
      else
	{
	  unit = "GB";
	  size_in_unit = memory_size / POWER_OF_2(30);
	}
      
      prints("memory available: ");
      printdect(size_in_unit); puts(unit);
    }
  else
    {
      // Most Pis have more, but qemu might give us little
      puts("Couldn't determine available memory - assuming 192MB");
      memory_size = 192 * POWER_OF_2(20);
    }

  // assume we need at least one section for PL0
  if (memory_size < PRIVILEGED_MEMORY_END + SECTION_SIZE)
    {
      puts("Not enough memory to continue");
      while (1);
    }
  
  // prints some info
  demo_paging_support();

  // prints some info
  demo_current_mode();

  setup_pager_structures(memory_size);
  
  // prints some info and sets upp translation table, turns on MMU
  setup_flat_map();

  // prints some info and sets up a section for PL0 code,
  // loads a blob there 
  demo_setup_PL0();

  // jumps to unprivileged code... never, ever, ever returns
  demo_go_unprivileged();
  
  while (1)
    {
      char c = getchar();

      if (c == '\r')
	putchar('\n');

      putchar(c);
    }
}