The Pedigree Project  0.1
system/boot/ppc/main.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "Elf32.h"
21 #include "Vga.h"
22 #include "autogen.h"
23 #include "autogen_initrd.h"
24 #include "prom.h"
25 
26 extern int ByteSet(void *buf, int c, size_t len);
27 struct BootstrapStruct_t
28 {
29  int (*prom)(struct anon *);
30  uint32_t initrd_start;
31  uint32_t initrd_end;
32 
33  /* ELF information */
34  uint32_t num;
35  uint32_t size;
36  uint32_t addr;
37  uint32_t shndx;
38 };
39 
40 void writeChar(char c)
41 {
42  prom_putchar(c);
43 }
44 
45 void writeStr(const char *str)
46 {
47  char c;
48  while ((c = *str++))
49  writeChar(c);
50 }
51 
52 void writeHex(unsigned int n)
53 {
54  bool noZeroes = true;
55 
56  int i;
57  unsigned int tmp;
58  for (i = 28; i > 0; i -= 4)
59  {
60  tmp = (n >> i) & 0xF;
61  if (tmp == 0 && noZeroes)
62  {
63  continue;
64  }
65 
66  if (tmp >= 0xA)
67  {
68  noZeroes = false;
69  writeChar(tmp - 0xA + 'a');
70  }
71  else
72  {
73  noZeroes = false;
74  writeChar(tmp + '0');
75  }
76  }
77 
78  tmp = n & 0xF;
79  if (tmp >= 0xA)
80  {
81  writeChar(tmp - 0xA + 'a');
82  }
83  else
84  {
85  writeChar(tmp + '0');
86  }
87 }
88 extern void *prom_screen;
89 extern "C" void _start(unsigned long r3, unsigned long r4, unsigned long r5)
90 {
91  prom_init((prom_entry) r5);
92 
93  Elf32 elf("kernel");
94  elf.load((uint8_t *) file, 0);
95  elf.writeSections();
96  int (*main)(struct BootstrapStruct_t *) =
97  (int (*)(struct BootstrapStruct_t *)) elf.getEntryPoint();
98 
99  struct BootstrapStruct_t bs;
100 
101  ByteSet(&bs, 0, sizeof(bs));
102  bs.shndx = elf.m_pHeader->shstrndx;
103  bs.num = elf.m_pHeader->shnum;
104  bs.size = elf.m_pHeader->shentsize;
105  bs.addr = (unsigned int) elf.m_pSectionHeaders;
106  bs.initrd_start = (uint32_t) initrd;
107  bs.initrd_end = (uint32_t) initrd + initrd_size;
108  bs.prom = (int (*)(struct anon *)) r5;
109 
110  // For every section header, set .addr = .offset + m_pBuffer.
111  for (int i = 0; i < elf.m_pHeader->shnum; i++)
112  {
113  elf.m_pSectionHeaders[i].addr =
114  elf.m_pSectionHeaders[i].offset + (uint32_t) elf.m_pBuffer;
115  }
116 
117  // Cache flush.
118 
119  writeStr("About to jump to kernel - entry point 0x");
120  writeHex(elf.getEntryPoint());
121  writeStr("\n");
122 
123  // vga_init();
124 
125  main(&bs);
126  writeStr("Kernel exited!\n");
127  for (;;)
128  ;
129 }
Bootstrap structure passed to the kernel entry point.