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 <arch/types.h>
|
---|
33 | #include <genarch/ofw.h>
|
---|
34 | #include <genarch/ofw_tree.h>
|
---|
35 | #include <halt.h>
|
---|
36 | #include <printf.h>
|
---|
37 | #include <mem.h>
|
---|
38 | #include <version.h>
|
---|
39 | #include <macros.h>
|
---|
40 | #include <align.h>
|
---|
41 | #include <str.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <payload.h>
|
---|
44 | #include <kernel.h>
|
---|
45 |
|
---|
46 | #define BALLOC_MAX_SIZE 131072
|
---|
47 |
|
---|
48 | static bootinfo_t bootinfo;
|
---|
49 |
|
---|
50 | static void check_overlap(const char *dest, void *phys, size_t pages)
|
---|
51 | {
|
---|
52 | if ((pages << PAGE_WIDTH) > (uintptr_t) phys) {
|
---|
53 | printf("Error: Image (%u pages) overlaps %s at %p, halting.\n",
|
---|
54 | pages, dest, phys);
|
---|
55 | halt();
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | void bootstrap(void)
|
---|
60 | {
|
---|
61 | version_print();
|
---|
62 | ofw_memmap(&bootinfo.memmap);
|
---|
63 |
|
---|
64 | void *bootinfo_pa = ofw_translate(&bootinfo);
|
---|
65 | void *real_mode_pa = ofw_translate(&real_mode);
|
---|
66 | void *loader_address_pa = ofw_translate((void *) LOADER_ADDRESS);
|
---|
67 |
|
---|
68 | printf("\nMemory statistics (total %llu MB)\n", bootinfo.memmap.total >> 20);
|
---|
69 | printf(" %p|%p: real mode trampoline\n", &real_mode, real_mode_pa);
|
---|
70 | printf(" %p|%p: boot info structure\n", &bootinfo, bootinfo_pa);
|
---|
71 | printf(" %p|%p: loader entry point\n",
|
---|
72 | (void *) LOADER_ADDRESS, loader_address_pa);
|
---|
73 |
|
---|
74 | size_t unpacked_size = payload_unpacked_size();
|
---|
75 | printf("Payload uncompressed size: %d bytes\n", unpacked_size);
|
---|
76 |
|
---|
77 | if (unpacked_size >= (size_t) loader_address_pa) {
|
---|
78 | printf("Inflated components overlap loader area.\n");
|
---|
79 | printf("The boot image is too large. Halting.\n");
|
---|
80 | halt();
|
---|
81 | }
|
---|
82 |
|
---|
83 | void *balloc_base;
|
---|
84 | void *balloc_base_pa;
|
---|
85 | ofw_alloc("boot allocator area", &balloc_base, &balloc_base_pa,
|
---|
86 | BALLOC_MAX_SIZE, loader_address_pa);
|
---|
87 | printf(" %p|%p: boot allocator area\n", balloc_base, balloc_base_pa);
|
---|
88 |
|
---|
89 | void *inflate_base;
|
---|
90 | void *inflate_base_pa;
|
---|
91 | ofw_alloc("inflate area", &inflate_base, &inflate_base_pa,
|
---|
92 | unpacked_size, loader_address_pa);
|
---|
93 | printf(" %p|%p: inflate area\n", inflate_base, inflate_base_pa);
|
---|
94 |
|
---|
95 | uintptr_t balloc_start = ALIGN_UP(unpacked_size, PAGE_SIZE);
|
---|
96 | size_t pages = (balloc_start + ALIGN_UP(BALLOC_MAX_SIZE, PAGE_SIZE)) >>
|
---|
97 | PAGE_WIDTH;
|
---|
98 |
|
---|
99 | printf(" Boot allocations area: %p - %p\n", (void *) balloc_start,
|
---|
100 | (void *) (pages << PAGE_WIDTH));
|
---|
101 |
|
---|
102 | if ((pages << PAGE_WIDTH) >= (uintptr_t) loader_address_pa) {
|
---|
103 | printf("Boot allocations overlap loader area.\n");
|
---|
104 | printf("The boot image is too large. Halting.\n");
|
---|
105 | halt();
|
---|
106 | }
|
---|
107 |
|
---|
108 | void *transtable;
|
---|
109 | void *transtable_pa;
|
---|
110 | ofw_alloc("translate table", &transtable, &transtable_pa,
|
---|
111 | pages * sizeof(void *), loader_address_pa);
|
---|
112 | printf(" %p|%p: translate table\n", transtable, transtable_pa);
|
---|
113 |
|
---|
114 | check_overlap("boot allocator area", balloc_base_pa, pages);
|
---|
115 | check_overlap("inflate area", inflate_base_pa, pages);
|
---|
116 | check_overlap("translate table", transtable_pa, pages);
|
---|
117 |
|
---|
118 | /* Inflate components. */
|
---|
119 | extract_payload(&bootinfo.taskmap, inflate_base,
|
---|
120 | inflate_base + unpacked_size, PA2KA(0), NULL);
|
---|
121 |
|
---|
122 | printf("Setting up boot allocator ...\n");
|
---|
123 | balloc_init(&bootinfo.ballocs, balloc_base, PA2KA(balloc_start),
|
---|
124 | BALLOC_MAX_SIZE);
|
---|
125 |
|
---|
126 | printf("Setting up screens ...\n");
|
---|
127 | ofw_setup_screens();
|
---|
128 |
|
---|
129 | printf("Canonizing OpenFirmware device tree ...\n");
|
---|
130 | bootinfo.ofw_root = ofw_tree_build();
|
---|
131 |
|
---|
132 | printf("Setting up translate table ...\n");
|
---|
133 | for (size_t i = 0; i < pages; i++) {
|
---|
134 | uintptr_t off = i << PAGE_WIDTH;
|
---|
135 | void *phys;
|
---|
136 |
|
---|
137 | if (off < balloc_start)
|
---|
138 | phys = ofw_translate(inflate_base + off);
|
---|
139 | else
|
---|
140 | phys = ofw_translate(balloc_base + off - balloc_start);
|
---|
141 |
|
---|
142 | ((void **) transtable)[i] = phys;
|
---|
143 | }
|
---|
144 |
|
---|
145 | uintptr_t entry = check_kernel_translated(inflate_base, 0);
|
---|
146 |
|
---|
147 | printf("Booting the kernel...\n");
|
---|
148 | jump_to_kernel(bootinfo_pa, transtable_pa, pages, real_mode_pa, entry);
|
---|
149 | }
|
---|