[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>
|
---|
[3f7fe9e] | 32 | #include <arch/types.h>
|
---|
[4872160] | 33 | #include <genarch/ofw.h>
|
---|
| 34 | #include <genarch/ofw_tree.h>
|
---|
| 35 | #include <halt.h>
|
---|
| 36 | #include <printf.h>
|
---|
[b169619] | 37 | #include <mem.h>
|
---|
[4872160] | 38 | #include <version.h>
|
---|
| 39 | #include <macros.h>
|
---|
| 40 | #include <align.h>
|
---|
| 41 | #include <str.h>
|
---|
| 42 | #include <errno.h>
|
---|
[63a045c] | 43 | #include <payload.h>
|
---|
[cfdeedc] | 44 | #include <kernel.h>
|
---|
[4872160] | 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);
|
---|
[a35b458] | 63 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 67 |
|
---|
[4872160] | 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);
|
---|
[7e752b2] | 71 | printf(" %p|%p: loader entry point\n",
|
---|
| 72 | (void *) LOADER_ADDRESS, loader_address_pa);
|
---|
[a35b458] | 73 |
|
---|
[dcc2c5d] | 74 | size_t unpacked_size = payload_unpacked_size();
|
---|
| 75 | printf("Payload uncompressed size: %d bytes\n", unpacked_size);
|
---|
[a35b458] | 76 |
|
---|
[dcc2c5d] | 77 | if (unpacked_size >= (size_t) loader_address_pa) {
|
---|
[e5ac4130] | 78 | printf("Inflated components overlap loader area.\n");
|
---|
| 79 | printf("The boot image is too large. Halting.\n");
|
---|
| 80 | halt();
|
---|
| 81 | }
|
---|
[a35b458] | 82 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 88 |
|
---|
[4872160] | 89 | void *inflate_base;
|
---|
| 90 | void *inflate_base_pa;
|
---|
[63a045c] | 91 | ofw_alloc("inflate area", &inflate_base, &inflate_base_pa,
|
---|
[dcc2c5d] | 92 | unpacked_size, loader_address_pa);
|
---|
[4872160] | 93 | printf(" %p|%p: inflate area\n", inflate_base, inflate_base_pa);
|
---|
[a35b458] | 94 |
|
---|
[dcc2c5d] | 95 | uintptr_t balloc_start = ALIGN_UP(unpacked_size, PAGE_SIZE);
|
---|
[3bacee1] | 96 | size_t pages = (balloc_start + ALIGN_UP(BALLOC_MAX_SIZE, PAGE_SIZE)) >>
|
---|
| 97 | PAGE_WIDTH;
|
---|
[d59718e] | 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 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 113 |
|
---|
[4872160] | 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);
|
---|
[a35b458] | 117 |
|
---|
[63a045c] | 118 | /* Inflate components. */
|
---|
| 119 | extract_payload(&bootinfo.taskmap, inflate_base,
|
---|
[dcc2c5d] | 120 | inflate_base + unpacked_size, PA2KA(0), NULL);
|
---|
[a35b458] | 121 |
|
---|
[4872160] | 122 | printf("Setting up boot allocator ...\n");
|
---|
| 123 | balloc_init(&bootinfo.ballocs, balloc_base, PA2KA(balloc_start),
|
---|
| 124 | BALLOC_MAX_SIZE);
|
---|
[a35b458] | 125 |
|
---|
[4872160] | 126 | printf("Setting up screens ...\n");
|
---|
| 127 | ofw_setup_screens();
|
---|
[a35b458] | 128 |
|
---|
[4872160] | 129 | printf("Canonizing OpenFirmware device tree ...\n");
|
---|
| 130 | bootinfo.ofw_root = ofw_tree_build();
|
---|
[a35b458] | 131 |
|
---|
[4872160] | 132 | printf("Setting up translate table ...\n");
|
---|
[63a045c] | 133 | for (size_t i = 0; i < pages; i++) {
|
---|
[4872160] | 134 | uintptr_t off = i << PAGE_WIDTH;
|
---|
| 135 | void *phys;
|
---|
[a35b458] | 136 |
|
---|
[4872160] | 137 | if (off < balloc_start)
|
---|
| 138 | phys = ofw_translate(inflate_base + off);
|
---|
| 139 | else
|
---|
| 140 | phys = ofw_translate(balloc_base + off - balloc_start);
|
---|
[a35b458] | 141 |
|
---|
[4872160] | 142 | ((void **) transtable)[i] = phys;
|
---|
| 143 | }
|
---|
[a35b458] | 144 |
|
---|
[cfdeedc] | 145 | uintptr_t entry = check_kernel_translated(inflate_base, 0);
|
---|
| 146 |
|
---|
[4872160] | 147 | printf("Booting the kernel...\n");
|
---|
[cfdeedc] | 148 | jump_to_kernel(bootinfo_pa, transtable_pa, pages, real_mode_pa, entry);
|
---|
[4872160] | 149 | }
|
---|