[3c1dec0] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Martin Decky
|
---|
[3c1dec0] | 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 |
|
---|
[ce8725be] | 29 | #include <printf.h>
|
---|
[63cda71] | 30 | #include <ofw.h>
|
---|
| 31 | #include <align.h>
|
---|
[fa024ce] | 32 | #include <macros.h>
|
---|
[5e9de3a] | 33 | #include <string.h>
|
---|
[e731b0d] | 34 | #include "main.h"
|
---|
| 35 | #include "asm.h"
|
---|
| 36 | #include "_components.h"
|
---|
[63f5cd6] | 37 |
|
---|
[e731b0d] | 38 | static bootinfo_t bootinfo;
|
---|
| 39 | static component_t components[COMPONENTS];
|
---|
| 40 | static char *release = STRING(RELEASE);
|
---|
[22f851e] | 41 |
|
---|
| 42 | #ifdef REVISION
|
---|
[e731b0d] | 43 | static char *revision = ", revision " STRING(REVISION);
|
---|
[22f851e] | 44 | #else
|
---|
[e731b0d] | 45 | static char *revision = "";
|
---|
[22f851e] | 46 | #endif
|
---|
| 47 |
|
---|
| 48 | #ifdef TIMESTAMP
|
---|
[e731b0d] | 49 | static char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
|
---|
[22f851e] | 50 | #else
|
---|
[e731b0d] | 51 | static char *timestamp = "";
|
---|
[22f851e] | 52 | #endif
|
---|
| 53 |
|
---|
| 54 | /** Print version information. */
|
---|
| 55 | static void version_print(void)
|
---|
| 56 | {
|
---|
[e731b0d] | 57 | printf("HelenOS PPC32 Bootloader\nRelease %s%s%s\n"
|
---|
| 58 | "Copyright (c) 2006 HelenOS project\n\n",
|
---|
| 59 | release, revision, timestamp);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | static void check_align(const void *addr, const char *desc)
|
---|
| 63 | {
|
---|
| 64 | if ((uintptr_t) addr % PAGE_SIZE != 0) {
|
---|
| 65 | printf("Error: %s not on page boundary, halting.\n", desc);
|
---|
| 66 | halt();
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | static void check_overlap(const void *pa, const char *desc, const uintptr_t top)
|
---|
| 71 | {
|
---|
| 72 | if ((uintptr_t) pa + PAGE_SIZE < top) {
|
---|
| 73 | printf("Error: %s overlaps destination physical area\n", desc);
|
---|
| 74 | halt();
|
---|
| 75 | }
|
---|
[22f851e] | 76 | }
|
---|
[1f330de] | 77 |
|
---|
[3c1dec0] | 78 | void bootstrap(void)
|
---|
| 79 | {
|
---|
[22f851e] | 80 | version_print();
|
---|
[e19d667] | 81 | init_components(components);
|
---|
[01cb210] | 82 |
|
---|
[7dcde32] | 83 | if (!ofw_memmap(&bootinfo.memmap)) {
|
---|
[f817d3a] | 84 | printf("Error: Unable to get memory map, halting.\n");
|
---|
[38fe9d0] | 85 | halt();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | if (bootinfo.memmap.total == 0) {
|
---|
[f817d3a] | 89 | printf("Error: No memory detected, halting.\n");
|
---|
[1f330de] | 90 | halt();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[e731b0d] | 93 | check_align(&real_mode, "bootstrap trampoline");
|
---|
| 94 | check_align(trans, "translation table");
|
---|
| 95 | check_align(balloc_base, "boot allocations");
|
---|
[f817d3a] | 96 |
|
---|
[e731b0d] | 97 | unsigned int i;
|
---|
| 98 | for (i = 0; i < COMPONENTS; i++)
|
---|
| 99 | check_align(components[i].start, components[i].name);
|
---|
[7dcde32] | 100 |
|
---|
| 101 | void *bootinfo_pa = ofw_translate(&bootinfo);
|
---|
[e731b0d] | 102 | void *real_mode_pa = ofw_translate(&real_mode);
|
---|
| 103 | void *trans_pa = ofw_translate(trans);
|
---|
| 104 | void *balloc_base_pa = ofw_translate(balloc_base);
|
---|
[01cb210] | 105 |
|
---|
[e731b0d] | 106 | printf("Memory statistics (total %d MB)\n", bootinfo.memmap.total >> 20);
|
---|
[3eee37d] | 107 | printf(" %L: boot info structure (physical %L)\n", &bootinfo, bootinfo_pa);
|
---|
| 108 | printf(" %L: bootstrap trampoline (physical %L)\n", &real_mode, real_mode_pa);
|
---|
[e731b0d] | 109 | printf(" %L: translation table (physical %L)\n", trans, trans_pa);
|
---|
| 110 | printf(" %L: boot allocations (physical %L)\n", balloc_base, balloc_base_pa);
|
---|
[3eee37d] | 111 | for (i = 0; i < COMPONENTS; i++)
|
---|
| 112 | printf(" %L: %s image (size %d bytes)\n", components[i].start, components[i].name, components[i].size);
|
---|
[3c1dec0] | 113 |
|
---|
[e731b0d] | 114 | uintptr_t top = 0;
|
---|
[3eee37d] | 115 | for (i = 0; i < COMPONENTS; i++)
|
---|
| 116 | top += ALIGN_UP(components[i].size, PAGE_SIZE);
|
---|
[e731b0d] | 117 | top += ALIGN_UP(BALLOC_MAX_SIZE, PAGE_SIZE);
|
---|
| 118 |
|
---|
| 119 | if (top >= TRANS_SIZE * PAGE_SIZE) {
|
---|
| 120 | printf("Error: boot image is too large\n");
|
---|
| 121 | halt();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | check_overlap(bootinfo_pa, "boot info", top);
|
---|
| 125 | check_overlap(real_mode_pa, "bootstrap trampoline", top);
|
---|
| 126 | check_overlap(trans_pa, "translation table", top);
|
---|
[63f5cd6] | 127 |
|
---|
[3eee37d] | 128 | unsigned int pages = ALIGN_UP(KERNEL_SIZE, PAGE_SIZE) >> PAGE_WIDTH;
|
---|
[63f5cd6] | 129 |
|
---|
[3eee37d] | 130 | for (i = 0; i < pages; i++) {
|
---|
[63f5cd6] | 131 | void *pa = ofw_translate(KERNEL_START + (i << PAGE_WIDTH));
|
---|
[e731b0d] | 132 | check_overlap(pa, "kernel", top);
|
---|
| 133 | trans[i] = (uintptr_t) pa;
|
---|
[63f5cd6] | 134 | }
|
---|
| 135 |
|
---|
[3eee37d] | 136 | bootinfo.taskmap.count = 0;
|
---|
| 137 | for (i = 1; i < COMPONENTS; i++) {
|
---|
[e731b0d] | 138 | if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
|
---|
| 139 | printf("\nSkipping superfluous components.\n");
|
---|
| 140 | break;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[3eee37d] | 143 | unsigned int component_pages = ALIGN_UP(components[i].size, PAGE_SIZE) >> PAGE_WIDTH;
|
---|
| 144 | unsigned int j;
|
---|
| 145 |
|
---|
| 146 | for (j = 0; j < component_pages; j++) {
|
---|
| 147 | void *pa = ofw_translate(components[i].start + (j << PAGE_WIDTH));
|
---|
[e731b0d] | 148 | check_overlap(pa, components[i].name, top);
|
---|
| 149 | trans[pages + j] = (uintptr_t) pa;
|
---|
[3eee37d] | 150 | if (j == 0) {
|
---|
[e731b0d] | 151 |
|
---|
| 152 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr = (void *) PA2KA(pages << PAGE_WIDTH);
|
---|
[3eee37d] | 153 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].size = components[i].size;
|
---|
[5e9de3a] | 154 | strncpy(bootinfo.taskmap.tasks[bootinfo.taskmap.count].name,
|
---|
| 155 | components[i].name, BOOTINFO_TASK_NAME_BUFLEN);
|
---|
[e731b0d] | 156 |
|
---|
[3eee37d] | 157 | bootinfo.taskmap.count++;
|
---|
| 158 | }
|
---|
[63f5cd6] | 159 | }
|
---|
[3eee37d] | 160 |
|
---|
| 161 | pages += component_pages;
|
---|
[3c1dec0] | 162 | }
|
---|
| 163 |
|
---|
[e731b0d] | 164 | uintptr_t balloc_kernel_base = PA2KA(pages << PAGE_WIDTH);
|
---|
| 165 | unsigned int balloc_pages = ALIGN_UP(BALLOC_MAX_SIZE, PAGE_SIZE) >> PAGE_WIDTH;
|
---|
| 166 | for (i = 0; i < balloc_pages; i++) {
|
---|
| 167 | void *pa = ofw_translate(balloc_base + (i << PAGE_WIDTH));
|
---|
| 168 | check_overlap(pa, "boot allocations", top);
|
---|
| 169 | trans[pages + i] = (uintptr_t) pa;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | pages += balloc_pages;
|
---|
| 173 |
|
---|
| 174 | balloc_init(&bootinfo.ballocs, (uintptr_t) balloc_base, balloc_kernel_base);
|
---|
| 175 | printf("\nCanonizing OpenFirmware device tree...");
|
---|
| 176 | bootinfo.ofw_root = ofw_tree_build();
|
---|
| 177 | printf("done.\n");
|
---|
[f941347] | 178 |
|
---|
[fd375a8d] | 179 | ofw_setup_palette();
|
---|
| 180 |
|
---|
[7dcde32] | 181 | printf("\nBooting the kernel...\n");
|
---|
[e731b0d] | 182 | jump_to_kernel(bootinfo_pa, sizeof(bootinfo), trans_pa, pages << PAGE_WIDTH, real_mode_pa);
|
---|
[3c1dec0] | 183 | }
|
---|