| 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>
|
|---|
| 32 | #include <arch/types.h>
|
|---|
| 33 | #include <arch/arch.h>
|
|---|
| 34 | #include <arch/asm.h>
|
|---|
| 35 | #include <arch/_components.h>
|
|---|
| 36 | #include <genarch/efi.h>
|
|---|
| 37 | #include <arch/sal.h>
|
|---|
| 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>
|
|---|
| 47 |
|
|---|
| 48 | #define DEFAULT_MEMORY_BASE 0x4000000ULL
|
|---|
| 49 | #define DEFAULT_MEMORY_SIZE 0x4000000ULL
|
|---|
| 50 | #define DEFAULT_LEGACY_IO_BASE 0x00000FFFFC000000ULL
|
|---|
| 51 | #define DEFAULT_LEGACY_IO_SIZE 0x4000000ULL
|
|---|
| 52 |
|
|---|
| 53 | #define DEFAULT_FREQ_SCALE 0x0000000100000001ULL /* 1/1 */
|
|---|
| 54 | #define DEFAULT_SYS_FREQ 100000000ULL /* 100MHz */
|
|---|
| 55 |
|
|---|
| 56 | #define MEMMAP_FREE_MEM 0
|
|---|
| 57 | #define MEMMAP_IO 1
|
|---|
| 58 | #define MEMMAP_IO_PORTS 2
|
|---|
| 59 |
|
|---|
| 60 | extern boot_param_t *bootpar;
|
|---|
| 61 |
|
|---|
| 62 | static bootinfo_t bootinfo;
|
|---|
| 63 |
|
|---|
| 64 | static void read_efi_memmap(void)
|
|---|
| 65 | {
|
|---|
| 66 | memmap_item_t *memmap = bootinfo.memmap;
|
|---|
| 67 | size_t items = 0;
|
|---|
| 68 |
|
|---|
| 69 | if (!bootpar) {
|
|---|
| 70 | /* Fake-up a memory map for simulators. */
|
|---|
| 71 | memmap[items].base = DEFAULT_MEMORY_BASE;
|
|---|
| 72 | memmap[items].size = DEFAULT_MEMORY_SIZE;
|
|---|
| 73 | memmap[items].type = MEMMAP_FREE_MEM;
|
|---|
| 74 | items++;
|
|---|
| 75 |
|
|---|
| 76 | memmap[items].base = DEFAULT_LEGACY_IO_BASE;
|
|---|
| 77 | memmap[items].size = DEFAULT_LEGACY_IO_SIZE;
|
|---|
| 78 | memmap[items].type = MEMMAP_IO_PORTS;
|
|---|
| 79 | items++;
|
|---|
| 80 | } else {
|
|---|
| 81 | char *cur, *mm_base = (char *) bootpar->efi_memmap;
|
|---|
| 82 | size_t mm_size = bootpar->efi_memmap_sz;
|
|---|
| 83 | size_t md_size = bootpar->efi_memdesc_sz;
|
|---|
| 84 |
|
|---|
| 85 | /*
|
|---|
| 86 | * Walk the EFI memory map using the V1 memory descriptor
|
|---|
| 87 | * format. The actual memory descriptor can use newer format,
|
|---|
| 88 | * but it must always be backwards compatible with the V1
|
|---|
| 89 | * format.
|
|---|
| 90 | */
|
|---|
| 91 | for (cur = mm_base;
|
|---|
| 92 | (cur < mm_base + (mm_size - md_size)) &&
|
|---|
| 93 | (items < MEMMAP_ITEMS);
|
|---|
| 94 | cur += md_size) {
|
|---|
| 95 | efi_v1_memdesc_t *md = (efi_v1_memdesc_t *) cur;
|
|---|
| 96 |
|
|---|
| 97 | switch ((efi_memory_type_t) md->type) {
|
|---|
| 98 | case EFI_CONVENTIONAL_MEMORY:
|
|---|
| 99 | memmap[items].type = MEMMAP_FREE_MEM;
|
|---|
| 100 | break;
|
|---|
| 101 | case EFI_MEMORY_MAPPED_IO:
|
|---|
| 102 | memmap[items].type = MEMMAP_IO;
|
|---|
| 103 | break;
|
|---|
| 104 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
|
|---|
| 105 | memmap[items].type = MEMMAP_IO_PORTS;
|
|---|
| 106 | break;
|
|---|
| 107 | default:
|
|---|
| 108 | continue;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | memmap[items].base = md->phys_start;
|
|---|
| 112 | memmap[items].size = md->pages * EFI_PAGE_SIZE;
|
|---|
| 113 | items++;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | bootinfo.memmap_items = items;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | static void read_sal_configuration(void)
|
|---|
| 121 | {
|
|---|
| 122 | if (bootpar && bootpar->efi_system_table) {
|
|---|
| 123 | /* TODO: read the real values from SAL */
|
|---|
| 124 | bootinfo.freq_scale = DEFAULT_FREQ_SCALE;
|
|---|
| 125 | bootinfo.sys_freq = DEFAULT_SYS_FREQ;
|
|---|
| 126 |
|
|---|
| 127 | efi_guid_t sal_guid = SAL_SYSTEM_TABLE_GUID;
|
|---|
| 128 | sal_system_table_header_t *sal_st;
|
|---|
| 129 |
|
|---|
| 130 | sal_st = efi_vendor_table_find(
|
|---|
| 131 | (efi_system_table_t *) bootpar->efi_system_table, sal_guid);
|
|---|
| 132 |
|
|---|
| 133 | sal_system_table_parse(sal_st);
|
|---|
| 134 | } else {
|
|---|
| 135 | /* Configure default values for simulators. */
|
|---|
| 136 | bootinfo.freq_scale = DEFAULT_FREQ_SCALE;
|
|---|
| 137 | bootinfo.sys_freq = DEFAULT_SYS_FREQ;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void bootstrap(void)
|
|---|
| 142 | {
|
|---|
| 143 | version_print();
|
|---|
| 144 |
|
|---|
| 145 | printf(" %p|%p: boot info structure\n", &bootinfo, &bootinfo);
|
|---|
| 146 | printf(" %p|%p: kernel entry point\n",
|
|---|
| 147 | (void *) KERNEL_ADDRESS, (void *) KERNEL_ADDRESS);
|
|---|
| 148 | printf(" %p|%p: loader entry point\n",
|
|---|
| 149 | (void *) LOADER_ADDRESS, (void *) LOADER_ADDRESS);
|
|---|
| 150 |
|
|---|
| 151 | size_t i;
|
|---|
| 152 | for (i = 0; i < COMPONENTS; i++)
|
|---|
| 153 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", components[i].start,
|
|---|
| 154 | components[i].start, components[i].name,
|
|---|
| 155 | components[i].inflated, components[i].size);
|
|---|
| 156 |
|
|---|
| 157 | void *dest[COMPONENTS];
|
|---|
| 158 | size_t top = KERNEL_ADDRESS;
|
|---|
| 159 | size_t cnt = 0;
|
|---|
| 160 | bootinfo.taskmap.cnt = 0;
|
|---|
| 161 | for (i = 0; i < min(COMPONENTS, TASKMAP_MAX_RECORDS); i++) {
|
|---|
| 162 | top = ALIGN_UP(top, PAGE_SIZE);
|
|---|
| 163 |
|
|---|
| 164 | if (i > 0) {
|
|---|
| 165 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].addr =
|
|---|
| 166 | (void *) top;
|
|---|
| 167 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].size =
|
|---|
| 168 | components[i].inflated;
|
|---|
| 169 |
|
|---|
| 170 | str_cpy(bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].name,
|
|---|
| 171 | BOOTINFO_TASK_NAME_BUFLEN, components[i].name);
|
|---|
| 172 |
|
|---|
| 173 | bootinfo.taskmap.cnt++;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | dest[i] = (void *) top;
|
|---|
| 177 | top += components[i].inflated;
|
|---|
| 178 | cnt++;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | printf("\nInflating components ... ");
|
|---|
| 182 |
|
|---|
| 183 | for (i = cnt; i > 0; i--) {
|
|---|
| 184 | printf("%s ", components[i - 1].name);
|
|---|
| 185 |
|
|---|
| 186 | int err = inflate(components[i - 1].start, components[i - 1].size,
|
|---|
| 187 | dest[i - 1], components[i - 1].inflated);
|
|---|
| 188 |
|
|---|
| 189 | if (err != EOK) {
|
|---|
| 190 | printf("\n%s: Inflating error %d, halting.\n",
|
|---|
| 191 | components[i - 1].name, err);
|
|---|
| 192 | halt();
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | printf(".\n");
|
|---|
| 197 |
|
|---|
| 198 | read_efi_memmap();
|
|---|
| 199 | read_sal_configuration();
|
|---|
| 200 |
|
|---|
| 201 | printf("Booting the kernel ...\n");
|
|---|
| 202 | jump_to_kernel(&bootinfo);
|
|---|
| 203 | }
|
|---|