1 | /*
|
---|
2 | * Copyright (c) 2005 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <arch/main.h>
|
---|
30 | #include <arch/arch.h>
|
---|
31 | #include <arch/asm.h>
|
---|
32 | #include <genarch/ofw.h>
|
---|
33 | #include <genarch/ofw_tree.h>
|
---|
34 | #include <halt.h>
|
---|
35 | #include <printf.h>
|
---|
36 | #include <memstr.h>
|
---|
37 | #include <version.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <align.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <inflate.h>
|
---|
43 | #include "../../components.h"
|
---|
44 |
|
---|
45 | #define BALLOC_MAX_SIZE 131072
|
---|
46 |
|
---|
47 | static bootinfo_t bootinfo;
|
---|
48 |
|
---|
49 | static void check_overlap(const char *dest, void *phys, size_t pages)
|
---|
50 | {
|
---|
51 | if ((pages << PAGE_WIDTH) > (uintptr_t) phys) {
|
---|
52 | printf("Error: Image (%u pages) overlaps %s at %p, halting.\n",
|
---|
53 | pages, dest, phys);
|
---|
54 | halt();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | void bootstrap(void)
|
---|
59 | {
|
---|
60 | version_print();
|
---|
61 | ofw_memmap(&bootinfo.memmap);
|
---|
62 |
|
---|
63 | void *bootinfo_pa = ofw_translate(&bootinfo);
|
---|
64 | void *real_mode_pa = ofw_translate(&real_mode);
|
---|
65 | void *loader_address_pa = ofw_translate((void *) LOADER_ADDRESS);
|
---|
66 |
|
---|
67 | printf("\nMemory statistics (total %llu MB)\n", bootinfo.memmap.total >> 20);
|
---|
68 | printf(" %p|%p: real mode trampoline\n", &real_mode, real_mode_pa);
|
---|
69 | printf(" %p|%p: boot info structure\n", &bootinfo, bootinfo_pa);
|
---|
70 | printf(" %p|%p: kernel entry point\n",
|
---|
71 | (void *) PA2KA(BOOT_OFFSET), (void *) BOOT_OFFSET);
|
---|
72 | printf(" %p|%p: loader entry point\n",
|
---|
73 | (void *) LOADER_ADDRESS, loader_address_pa);
|
---|
74 |
|
---|
75 | size_t i;
|
---|
76 | for (i = 0; i < COMPONENTS; i++)
|
---|
77 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", components[i].addr,
|
---|
78 | ofw_translate(components[i].addr), components[i].name,
|
---|
79 | components[i].inflated, components[i].size);
|
---|
80 |
|
---|
81 | size_t dest[COMPONENTS];
|
---|
82 | size_t top = 0;
|
---|
83 | size_t cnt = 0;
|
---|
84 | bootinfo.taskmap.cnt = 0;
|
---|
85 | for (i = 0; i < min(COMPONENTS, TASKMAP_MAX_RECORDS); i++) {
|
---|
86 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
87 |
|
---|
88 | if (i > 0) {
|
---|
89 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].addr =
|
---|
90 | (void *) PA2KA(top);
|
---|
91 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].size =
|
---|
92 | components[i].inflated;
|
---|
93 |
|
---|
94 | str_cpy(bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].name,
|
---|
95 | BOOTINFO_TASK_NAME_BUFLEN, components[i].name);
|
---|
96 |
|
---|
97 | bootinfo.taskmap.cnt++;
|
---|
98 | }
|
---|
99 |
|
---|
100 | dest[i] = top;
|
---|
101 | top += components[i].inflated;
|
---|
102 | cnt++;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (top >= (size_t) loader_address_pa) {
|
---|
106 | printf("Inflated components overlap loader area.\n");
|
---|
107 | printf("The boot image is too large. Halting.\n");
|
---|
108 | halt();
|
---|
109 | }
|
---|
110 |
|
---|
111 | void *balloc_base;
|
---|
112 | void *balloc_base_pa;
|
---|
113 | ofw_alloc("boot allocator area", &balloc_base, &balloc_base_pa,
|
---|
114 | BALLOC_MAX_SIZE, loader_address_pa);
|
---|
115 | printf(" %p|%p: boot allocator area\n", balloc_base, balloc_base_pa);
|
---|
116 |
|
---|
117 | void *inflate_base;
|
---|
118 | void *inflate_base_pa;
|
---|
119 | ofw_alloc("inflate area", &inflate_base, &inflate_base_pa, top,
|
---|
120 | loader_address_pa);
|
---|
121 | printf(" %p|%p: inflate area\n", inflate_base, inflate_base_pa);
|
---|
122 |
|
---|
123 | uintptr_t balloc_start = ALIGN_UP(top, PAGE_SIZE);
|
---|
124 | size_t pages = (balloc_start + ALIGN_UP(BALLOC_MAX_SIZE, PAGE_SIZE)) >>
|
---|
125 | PAGE_WIDTH;
|
---|
126 | void *transtable;
|
---|
127 | void *transtable_pa;
|
---|
128 | ofw_alloc("translate table", &transtable, &transtable_pa,
|
---|
129 | pages * sizeof(void *), loader_address_pa);
|
---|
130 | printf(" %p|%p: translate table\n", transtable, transtable_pa);
|
---|
131 |
|
---|
132 | check_overlap("boot allocator area", balloc_base_pa, pages);
|
---|
133 | check_overlap("inflate area", inflate_base_pa, pages);
|
---|
134 | check_overlap("translate table", transtable_pa, pages);
|
---|
135 |
|
---|
136 | printf("\nInflating components ... ");
|
---|
137 |
|
---|
138 | for (i = cnt; i > 0; i--) {
|
---|
139 | printf("%s ", components[i - 1].name);
|
---|
140 |
|
---|
141 | int err = inflate(components[i - 1].addr, components[i - 1].size,
|
---|
142 | inflate_base + dest[i - 1], components[i - 1].inflated);
|
---|
143 |
|
---|
144 | if (err != EOK) {
|
---|
145 | printf("\n%s: Inflating error %d, halting.\n",
|
---|
146 | components[i - 1].name, err);
|
---|
147 | halt();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | printf(".\n");
|
---|
152 |
|
---|
153 | printf("Setting up boot allocator ...\n");
|
---|
154 | balloc_init(&bootinfo.ballocs, balloc_base, PA2KA(balloc_start),
|
---|
155 | BALLOC_MAX_SIZE);
|
---|
156 |
|
---|
157 | printf("Setting up screens ...\n");
|
---|
158 | ofw_setup_screens();
|
---|
159 |
|
---|
160 | printf("Canonizing OpenFirmware device tree ...\n");
|
---|
161 | bootinfo.ofw_root = ofw_tree_build();
|
---|
162 |
|
---|
163 | printf("Setting up translate table ...\n");
|
---|
164 | for (i = 0; i < pages; i++) {
|
---|
165 | uintptr_t off = i << PAGE_WIDTH;
|
---|
166 | void *phys;
|
---|
167 |
|
---|
168 | if (off < balloc_start)
|
---|
169 | phys = ofw_translate(inflate_base + off);
|
---|
170 | else
|
---|
171 | phys = ofw_translate(balloc_base + off - balloc_start);
|
---|
172 |
|
---|
173 | ((void **) transtable)[i] = phys;
|
---|
174 | }
|
---|
175 |
|
---|
176 | printf("Booting the kernel...\n");
|
---|
177 | jump_to_kernel(bootinfo_pa, transtable_pa, pages, real_mode_pa);
|
---|
178 | }
|
---|