[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 <arch/_components.h>
|
---|
| 33 | #include <genarch/ofw.h>
|
---|
| 34 | #include <genarch/ofw_tree.h>
|
---|
| 35 | #include <halt.h>
|
---|
| 36 | #include <printf.h>
|
---|
| 37 | #include <memstr.h>
|
---|
| 38 | #include <version.h>
|
---|
| 39 | #include <macros.h>
|
---|
| 40 | #include <align.h>
|
---|
| 41 | #include <str.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <inflate.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);
|
---|
[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);
|
---|
[4872160] | 74 |
|
---|
| 75 | size_t i;
|
---|
| 76 | for (i = 0; i < COMPONENTS; i++)
|
---|
[7e752b2] | 77 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", components[i].start,
|
---|
[4872160] | 78 | ofw_translate(components[i].start), 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 | void *balloc_base;
|
---|
| 106 | void *balloc_base_pa;
|
---|
| 107 | ofw_alloc("boot allocator area", &balloc_base, &balloc_base_pa,
|
---|
| 108 | BALLOC_MAX_SIZE, loader_address_pa);
|
---|
| 109 | printf(" %p|%p: boot allocator area\n", balloc_base, balloc_base_pa);
|
---|
| 110 |
|
---|
| 111 | void *inflate_base;
|
---|
| 112 | void *inflate_base_pa;
|
---|
| 113 | ofw_alloc("inflate area", &inflate_base, &inflate_base_pa, top,
|
---|
| 114 | loader_address_pa);
|
---|
| 115 | printf(" %p|%p: inflate area\n", inflate_base, inflate_base_pa);
|
---|
| 116 |
|
---|
| 117 | uintptr_t balloc_start = ALIGN_UP(top, PAGE_SIZE);
|
---|
| 118 | size_t pages = (balloc_start + ALIGN_UP(BALLOC_MAX_SIZE, PAGE_SIZE))
|
---|
| 119 | >> PAGE_WIDTH;
|
---|
| 120 | void *transtable;
|
---|
| 121 | void *transtable_pa;
|
---|
| 122 | ofw_alloc("translate table", &transtable, &transtable_pa,
|
---|
| 123 | pages * sizeof(void *), loader_address_pa);
|
---|
| 124 | printf(" %p|%p: translate table\n", transtable, transtable_pa);
|
---|
| 125 |
|
---|
| 126 | check_overlap("boot allocator area", balloc_base_pa, pages);
|
---|
| 127 | check_overlap("inflate area", inflate_base_pa, pages);
|
---|
| 128 | check_overlap("translate table", transtable_pa, pages);
|
---|
| 129 |
|
---|
| 130 | printf("\nInflating components ... ");
|
---|
| 131 |
|
---|
| 132 | for (i = cnt; i > 0; i--) {
|
---|
| 133 | printf("%s ", components[i - 1].name);
|
---|
| 134 |
|
---|
| 135 | int err = inflate(components[i - 1].start, components[i - 1].size,
|
---|
| 136 | inflate_base + dest[i - 1], components[i - 1].inflated);
|
---|
| 137 |
|
---|
| 138 | if (err != EOK) {
|
---|
| 139 | printf("\n%s: Inflating error %d, halting.\n",
|
---|
| 140 | components[i - 1].name, err);
|
---|
| 141 | halt();
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | printf(".\n");
|
---|
| 146 |
|
---|
| 147 | printf("Setting up boot allocator ...\n");
|
---|
| 148 | balloc_init(&bootinfo.ballocs, balloc_base, PA2KA(balloc_start),
|
---|
| 149 | BALLOC_MAX_SIZE);
|
---|
| 150 |
|
---|
| 151 | printf("Setting up screens ...\n");
|
---|
| 152 | ofw_setup_screens();
|
---|
| 153 |
|
---|
| 154 | printf("Canonizing OpenFirmware device tree ...\n");
|
---|
| 155 | bootinfo.ofw_root = ofw_tree_build();
|
---|
| 156 |
|
---|
| 157 | printf("Setting up translate table ...\n");
|
---|
| 158 | for (i = 0; i < pages; i++) {
|
---|
| 159 | uintptr_t off = i << PAGE_WIDTH;
|
---|
| 160 | void *phys;
|
---|
| 161 |
|
---|
| 162 | if (off < balloc_start)
|
---|
| 163 | phys = ofw_translate(inflate_base + off);
|
---|
| 164 | else
|
---|
| 165 | phys = ofw_translate(balloc_base + off - balloc_start);
|
---|
| 166 |
|
---|
| 167 | ((void **) transtable)[i] = phys;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | printf("Booting the kernel...\n");
|
---|
| 171 | jump_to_kernel(bootinfo_pa, transtable_pa, pages, real_mode_pa);
|
---|
| 172 | }
|
---|