[4872160] | 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>
|
---|
[4646710] | 43 | #include "../../components.h"
|
---|
[4872160] | 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);
|
---|
[a35b458] | 62 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 66 |
|
---|
[4872160] | 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);
|
---|
[7e752b2] | 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);
|
---|
[a35b458] | 74 |
|
---|
[4872160] | 75 | size_t i;
|
---|
| 76 | for (i = 0; i < COMPONENTS; i++)
|
---|
[4646710] | 77 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", components[i].addr,
|
---|
| 78 | ofw_translate(components[i].addr), components[i].name,
|
---|
[4872160] | 79 | components[i].inflated, components[i].size);
|
---|
[a35b458] | 80 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 87 |
|
---|
[4872160] | 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;
|
---|
[a35b458] | 93 |
|
---|
[4872160] | 94 | str_cpy(bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].name,
|
---|
| 95 | BOOTINFO_TASK_NAME_BUFLEN, components[i].name);
|
---|
[a35b458] | 96 |
|
---|
[4872160] | 97 | bootinfo.taskmap.cnt++;
|
---|
| 98 | }
|
---|
[a35b458] | 99 |
|
---|
[4872160] | 100 | dest[i] = top;
|
---|
| 101 | top += components[i].inflated;
|
---|
| 102 | cnt++;
|
---|
| 103 | }
|
---|
[a35b458] | 104 |
|
---|
[e5ac4130] | 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 | }
|
---|
[a35b458] | 110 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 116 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 122 |
|
---|
[4872160] | 123 | uintptr_t balloc_start = ALIGN_UP(top, PAGE_SIZE);
|
---|
[3bacee1] | 124 | size_t pages = (balloc_start + ALIGN_UP(BALLOC_MAX_SIZE, PAGE_SIZE)) >>
|
---|
| 125 | PAGE_WIDTH;
|
---|
[4872160] | 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);
|
---|
[a35b458] | 131 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 135 |
|
---|
[4872160] | 136 | printf("\nInflating components ... ");
|
---|
[a35b458] | 137 |
|
---|
[4872160] | 138 | for (i = cnt; i > 0; i--) {
|
---|
| 139 | printf("%s ", components[i - 1].name);
|
---|
[a35b458] | 140 |
|
---|
[4646710] | 141 | int err = inflate(components[i - 1].addr, components[i - 1].size,
|
---|
[4872160] | 142 | inflate_base + dest[i - 1], components[i - 1].inflated);
|
---|
[a35b458] | 143 |
|
---|
[4872160] | 144 | if (err != EOK) {
|
---|
| 145 | printf("\n%s: Inflating error %d, halting.\n",
|
---|
| 146 | components[i - 1].name, err);
|
---|
| 147 | halt();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[a35b458] | 150 |
|
---|
[4872160] | 151 | printf(".\n");
|
---|
[a35b458] | 152 |
|
---|
[4872160] | 153 | printf("Setting up boot allocator ...\n");
|
---|
| 154 | balloc_init(&bootinfo.ballocs, balloc_base, PA2KA(balloc_start),
|
---|
| 155 | BALLOC_MAX_SIZE);
|
---|
[a35b458] | 156 |
|
---|
[4872160] | 157 | printf("Setting up screens ...\n");
|
---|
| 158 | ofw_setup_screens();
|
---|
[a35b458] | 159 |
|
---|
[4872160] | 160 | printf("Canonizing OpenFirmware device tree ...\n");
|
---|
| 161 | bootinfo.ofw_root = ofw_tree_build();
|
---|
[a35b458] | 162 |
|
---|
[4872160] | 163 | printf("Setting up translate table ...\n");
|
---|
| 164 | for (i = 0; i < pages; i++) {
|
---|
| 165 | uintptr_t off = i << PAGE_WIDTH;
|
---|
| 166 | void *phys;
|
---|
[a35b458] | 167 |
|
---|
[4872160] | 168 | if (off < balloc_start)
|
---|
| 169 | phys = ofw_translate(inflate_base + off);
|
---|
| 170 | else
|
---|
| 171 | phys = ofw_translate(balloc_base + off - balloc_start);
|
---|
[a35b458] | 172 |
|
---|
[4872160] | 173 | ((void **) transtable)[i] = phys;
|
---|
| 174 | }
|
---|
[a35b458] | 175 |
|
---|
[4872160] | 176 | printf("Booting the kernel...\n");
|
---|
| 177 | jump_to_kernel(bootinfo_pa, transtable_pa, pages, real_mode_pa);
|
---|
| 178 | }
|
---|