[807102ca] | 1 | /*
|
---|
| 2 | * Copyright (c) 2005 Martin Decky
|
---|
| 3 | * Copyright (c) 2006 Jakub Jermar
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | #include <arch/main.h>
|
---|
[bbe4828] | 32 | #include <arch/types.h>
|
---|
[807102ca] | 33 | #include <arch/arch.h>
|
---|
| 34 | #include <arch/asm.h>
|
---|
[bbe4828] | 35 | #include <genarch/efi.h>
|
---|
[26fb118a] | 36 | #include <arch/sal.h>
|
---|
[699f3bc] | 37 | #include <arch/pal.h>
|
---|
[807102ca] | 38 | #include <halt.h>
|
---|
| 39 | #include <printf.h>
|
---|
| 40 | #include <memstr.h>
|
---|
| 41 | #include <version.h>
|
---|
| 42 | #include <macros.h>
|
---|
| 43 | #include <align.h>
|
---|
| 44 | #include <str.h>
|
---|
| 45 | #include <errno.h>
|
---|
| 46 | #include <inflate.h>
|
---|
[4646710] | 47 | #include "../../components.h"
|
---|
[807102ca] | 48 |
|
---|
| 49 | #define DEFAULT_MEMORY_BASE 0x4000000ULL
|
---|
[dbb3552] | 50 | #define DEFAULT_MEMORY_SIZE (256 * 1024 * 1024)
|
---|
[807102ca] | 51 | #define DEFAULT_LEGACY_IO_BASE 0x00000FFFFC000000ULL
|
---|
| 52 | #define DEFAULT_LEGACY_IO_SIZE 0x4000000ULL
|
---|
| 53 |
|
---|
| 54 | #define DEFAULT_FREQ_SCALE 0x0000000100000001ULL /* 1/1 */
|
---|
| 55 | #define DEFAULT_SYS_FREQ 100000000ULL /* 100MHz */
|
---|
| 56 |
|
---|
[bbe4828] | 57 | #define MEMMAP_FREE_MEM 0
|
---|
| 58 | #define MEMMAP_IO 1
|
---|
| 59 | #define MEMMAP_IO_PORTS 2
|
---|
| 60 |
|
---|
| 61 | extern boot_param_t *bootpar;
|
---|
[807102ca] | 62 |
|
---|
| 63 | static bootinfo_t bootinfo;
|
---|
| 64 |
|
---|
[bbe4828] | 65 | static void read_efi_memmap(void)
|
---|
| 66 | {
|
---|
| 67 | memmap_item_t *memmap = bootinfo.memmap;
|
---|
| 68 | size_t items = 0;
|
---|
[a35b458] | 69 |
|
---|
[bbe4828] | 70 | if (!bootpar) {
|
---|
| 71 | /* Fake-up a memory map for simulators. */
|
---|
| 72 | memmap[items].base = DEFAULT_MEMORY_BASE;
|
---|
| 73 | memmap[items].size = DEFAULT_MEMORY_SIZE;
|
---|
| 74 | memmap[items].type = MEMMAP_FREE_MEM;
|
---|
| 75 | items++;
|
---|
| 76 |
|
---|
| 77 | memmap[items].base = DEFAULT_LEGACY_IO_BASE;
|
---|
| 78 | memmap[items].size = DEFAULT_LEGACY_IO_SIZE;
|
---|
| 79 | memmap[items].type = MEMMAP_IO_PORTS;
|
---|
[4646710] | 80 | items++;
|
---|
[bbe4828] | 81 | } else {
|
---|
| 82 | char *cur, *mm_base = (char *) bootpar->efi_memmap;
|
---|
| 83 | size_t mm_size = bootpar->efi_memmap_sz;
|
---|
| 84 | size_t md_size = bootpar->efi_memdesc_sz;
|
---|
[a35b458] | 85 |
|
---|
[bbe4828] | 86 | /*
|
---|
| 87 | * Walk the EFI memory map using the V1 memory descriptor
|
---|
| 88 | * format. The actual memory descriptor can use newer format,
|
---|
| 89 | * but it must always be backwards compatible with the V1
|
---|
| 90 | * format.
|
---|
| 91 | */
|
---|
| 92 | for (cur = mm_base;
|
---|
| 93 | (cur < mm_base + (mm_size - md_size)) &&
|
---|
| 94 | (items < MEMMAP_ITEMS);
|
---|
| 95 | cur += md_size) {
|
---|
| 96 | efi_v1_memdesc_t *md = (efi_v1_memdesc_t *) cur;
|
---|
| 97 |
|
---|
| 98 | switch ((efi_memory_type_t) md->type) {
|
---|
| 99 | case EFI_CONVENTIONAL_MEMORY:
|
---|
| 100 | memmap[items].type = MEMMAP_FREE_MEM;
|
---|
| 101 | break;
|
---|
| 102 | case EFI_MEMORY_MAPPED_IO:
|
---|
| 103 | memmap[items].type = MEMMAP_IO;
|
---|
| 104 | break;
|
---|
| 105 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
|
---|
| 106 | memmap[items].type = MEMMAP_IO_PORTS;
|
---|
| 107 | break;
|
---|
| 108 | default:
|
---|
| 109 | continue;
|
---|
| 110 | }
|
---|
[a35b458] | 111 |
|
---|
[bbe4828] | 112 | memmap[items].base = md->phys_start;
|
---|
| 113 | memmap[items].size = md->pages * EFI_PAGE_SIZE;
|
---|
| 114 | items++;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
[a35b458] | 117 |
|
---|
[bbe4828] | 118 | bootinfo.memmap_items = items;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[577fe9b6] | 121 | static void read_pal_configuration(void)
|
---|
| 122 | {
|
---|
| 123 | if (bootpar) {
|
---|
[699f3bc] | 124 | bootinfo.freq_scale = pal_proc_freq_ratio();
|
---|
[577fe9b6] | 125 | } else {
|
---|
| 126 | /* Configure default values for simulators. */
|
---|
| 127 | bootinfo.freq_scale = DEFAULT_FREQ_SCALE;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[bbe4828] | 131 | static void read_sal_configuration(void)
|
---|
| 132 | {
|
---|
[26fb118a] | 133 | if (bootpar && bootpar->efi_system_table) {
|
---|
| 134 | efi_guid_t sal_guid = SAL_SYSTEM_TABLE_GUID;
|
---|
| 135 | sal_system_table_header_t *sal_st;
|
---|
[a35b458] | 136 |
|
---|
[26fb118a] | 137 | sal_st = efi_vendor_table_find(
|
---|
| 138 | (efi_system_table_t *) bootpar->efi_system_table, sal_guid);
|
---|
| 139 |
|
---|
| 140 | sal_system_table_parse(sal_st);
|
---|
[a35b458] | 141 |
|
---|
[577fe9b6] | 142 | bootinfo.sys_freq = sal_base_clock_frequency();
|
---|
[bbe4828] | 143 | } else {
|
---|
[26fb118a] | 144 | /* Configure default values for simulators. */
|
---|
[bbe4828] | 145 | bootinfo.sys_freq = DEFAULT_SYS_FREQ;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[807102ca] | 149 | void bootstrap(void)
|
---|
| 150 | {
|
---|
| 151 | version_print();
|
---|
[a35b458] | 152 |
|
---|
[807102ca] | 153 | printf(" %p|%p: boot info structure\n", &bootinfo, &bootinfo);
|
---|
[7e752b2] | 154 | printf(" %p|%p: kernel entry point\n",
|
---|
| 155 | (void *) KERNEL_ADDRESS, (void *) KERNEL_ADDRESS);
|
---|
| 156 | printf(" %p|%p: loader entry point\n",
|
---|
| 157 | (void *) LOADER_ADDRESS, (void *) LOADER_ADDRESS);
|
---|
[a35b458] | 158 |
|
---|
[807102ca] | 159 | size_t i;
|
---|
| 160 | for (i = 0; i < COMPONENTS; i++)
|
---|
[4646710] | 161 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", components[i].addr,
|
---|
| 162 | components[i].addr, components[i].name,
|
---|
[807102ca] | 163 | components[i].inflated, components[i].size);
|
---|
[a35b458] | 164 |
|
---|
[807102ca] | 165 | void *dest[COMPONENTS];
|
---|
| 166 | size_t top = KERNEL_ADDRESS;
|
---|
| 167 | size_t cnt = 0;
|
---|
| 168 | bootinfo.taskmap.cnt = 0;
|
---|
| 169 | for (i = 0; i < min(COMPONENTS, TASKMAP_MAX_RECORDS); i++) {
|
---|
| 170 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
[a35b458] | 171 |
|
---|
[807102ca] | 172 | if (i > 0) {
|
---|
| 173 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].addr =
|
---|
| 174 | (void *) top;
|
---|
| 175 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].size =
|
---|
| 176 | components[i].inflated;
|
---|
[a35b458] | 177 |
|
---|
[807102ca] | 178 | str_cpy(bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].name,
|
---|
| 179 | BOOTINFO_TASK_NAME_BUFLEN, components[i].name);
|
---|
[a35b458] | 180 |
|
---|
[807102ca] | 181 | bootinfo.taskmap.cnt++;
|
---|
| 182 | }
|
---|
[a35b458] | 183 |
|
---|
[807102ca] | 184 | dest[i] = (void *) top;
|
---|
| 185 | top += components[i].inflated;
|
---|
| 186 | cnt++;
|
---|
| 187 | }
|
---|
[a35b458] | 188 |
|
---|
[807102ca] | 189 | printf("\nInflating components ... ");
|
---|
[a35b458] | 190 |
|
---|
[bb1b44e] | 191 | /*
|
---|
| 192 | * We will use the next available address for a copy of each component to
|
---|
| 193 | * make sure that inflate() works with disjunctive memory regions.
|
---|
| 194 | */
|
---|
| 195 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
| 196 |
|
---|
[807102ca] | 197 | for (i = cnt; i > 0; i--) {
|
---|
| 198 | printf("%s ", components[i - 1].name);
|
---|
[a35b458] | 199 |
|
---|
[bb1b44e] | 200 | /*
|
---|
| 201 | * Copy the component to a location which is guaranteed not to
|
---|
| 202 | * overlap with the destination for inflate().
|
---|
| 203 | */
|
---|
[4646710] | 204 | memmove((void *) top, components[i - 1].addr, components[i - 1].size);
|
---|
[a35b458] | 205 |
|
---|
[bb1b44e] | 206 | int err = inflate((void *) top, components[i - 1].size,
|
---|
[807102ca] | 207 | dest[i - 1], components[i - 1].inflated);
|
---|
[a35b458] | 208 |
|
---|
[807102ca] | 209 | if (err != EOK) {
|
---|
| 210 | printf("\n%s: Inflating error %d, halting.\n",
|
---|
| 211 | components[i - 1].name, err);
|
---|
| 212 | halt();
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
[a35b458] | 215 |
|
---|
[807102ca] | 216 | printf(".\n");
|
---|
| 217 |
|
---|
[bbe4828] | 218 | read_efi_memmap();
|
---|
| 219 | read_sal_configuration();
|
---|
[577fe9b6] | 220 | read_pal_configuration();
|
---|
[a35b458] | 221 |
|
---|
[807102ca] | 222 | printf("Booting the kernel ...\n");
|
---|
| 223 | jump_to_kernel(&bootinfo);
|
---|
| 224 | }
|
---|