1 | /*
|
---|
2 | * Copyright (c) 2016 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/ucb.h>
|
---|
33 | #include <arch/mm.h>
|
---|
34 | #include <version.h>
|
---|
35 | #include <stddef.h>
|
---|
36 | #include <printf.h>
|
---|
37 | #include <macros.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <align.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <halt.h>
|
---|
42 | #include <inflate.h>
|
---|
43 | #include "../../components.h"
|
---|
44 |
|
---|
45 | static bootinfo_t bootinfo;
|
---|
46 |
|
---|
47 | void bootstrap(void)
|
---|
48 | {
|
---|
49 | version_print();
|
---|
50 |
|
---|
51 | bootinfo.htif_frame = ((uintptr_t) &htif_page) >> PAGE_WIDTH;
|
---|
52 | bootinfo.pt_frame = ((uintptr_t) &pt_page) >> PAGE_WIDTH;
|
---|
53 |
|
---|
54 | bootinfo.ucbinfo.tohost =
|
---|
55 | (volatile uint64_t *) PA2KA((uintptr_t) &tohost);
|
---|
56 | bootinfo.ucbinfo.fromhost =
|
---|
57 | (volatile uint64_t *) PA2KA((uintptr_t) &fromhost);
|
---|
58 |
|
---|
59 | // FIXME TODO: read from device tree
|
---|
60 | bootinfo.physmem_start = PHYSMEM_START;
|
---|
61 | bootinfo.memmap.total = PHYSMEM_SIZE;
|
---|
62 | bootinfo.memmap.cnt = 1;
|
---|
63 | bootinfo.memmap.zones[0].start = (void *) PHYSMEM_START;
|
---|
64 | bootinfo.memmap.zones[0].size = PHYSMEM_SIZE;
|
---|
65 |
|
---|
66 | printf("\nMemory statistics (total %llu MB, starting at %p)\n\n",
|
---|
67 | bootinfo.memmap.total >> 20, (void *) bootinfo.physmem_start);
|
---|
68 | printf(" %p: boot info structure\n", &bootinfo);
|
---|
69 |
|
---|
70 | uintptr_t top = BOOT_OFFSET;
|
---|
71 |
|
---|
72 | for (size_t i = 0; i < COMPONENTS; i++) {
|
---|
73 | printf(" %p: %s image (%zu/%zu bytes)\n", components[i].addr,
|
---|
74 | components[i].name, components[i].inflated,
|
---|
75 | components[i].size);
|
---|
76 |
|
---|
77 | uintptr_t tail = (uintptr_t) components[i].addr +
|
---|
78 | components[i].size;
|
---|
79 | if (tail > top) {
|
---|
80 | printf("\n%s: Image too large to fit (%p >= %p), halting.\n",
|
---|
81 | components[i].name, (void *) tail, (void *) top);
|
---|
82 | halt();
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | printf(" %p: inflate area\n", (void *) top);
|
---|
87 |
|
---|
88 | void *kernel_entry = NULL;
|
---|
89 | void *dest[COMPONENTS];
|
---|
90 | size_t cnt = 0;
|
---|
91 | bootinfo.taskmap.cnt = 0;
|
---|
92 |
|
---|
93 | for (size_t i = 0; i < min(COMPONENTS, TASKMAP_MAX_RECORDS); i++) {
|
---|
94 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
95 |
|
---|
96 | if (i > 0) {
|
---|
97 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].addr =
|
---|
98 | (void *) PA2KA(top);
|
---|
99 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].size =
|
---|
100 | components[i].inflated;
|
---|
101 |
|
---|
102 | str_cpy(bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].name,
|
---|
103 | BOOTINFO_TASK_NAME_BUFLEN, components[i].name);
|
---|
104 |
|
---|
105 | bootinfo.taskmap.cnt++;
|
---|
106 | } else
|
---|
107 | kernel_entry = (void *) PA2KA(top);
|
---|
108 |
|
---|
109 | dest[i] = (void *) top;
|
---|
110 | top += components[i].inflated;
|
---|
111 | cnt++;
|
---|
112 | }
|
---|
113 |
|
---|
114 | printf(" %p: kernel entry point\n", kernel_entry);
|
---|
115 |
|
---|
116 | if (top >= bootinfo.physmem_start + bootinfo.memmap.total) {
|
---|
117 | printf("Not enough physical memory available.\n");
|
---|
118 | printf("The boot image is too large. Halting.\n");
|
---|
119 | halt();
|
---|
120 | }
|
---|
121 |
|
---|
122 | printf("\nInflating components ... ");
|
---|
123 |
|
---|
124 | for (size_t i = cnt; i > 0; i--) {
|
---|
125 | printf("%s ", components[i - 1].name);
|
---|
126 |
|
---|
127 | int err = inflate(components[i - 1].addr, components[i - 1].size,
|
---|
128 | dest[i - 1], components[i - 1].inflated);
|
---|
129 |
|
---|
130 | if (err != EOK) {
|
---|
131 | printf("\n%s: Inflating error %d, halting.\n",
|
---|
132 | components[i - 1].name, err);
|
---|
133 | halt();
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | printf(".\n");
|
---|
138 |
|
---|
139 | printf("Booting the kernel...\n");
|
---|
140 | jump_to_kernel(PA2KA(&bootinfo));
|
---|
141 | }
|
---|