1 | /*
|
---|
2 | * Copyright (c) 2007 Michal Kebrt
|
---|
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 | /** @addtogroup arm32boot
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief Bootstrap.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <arch/main.h>
|
---|
37 | #include <arch/asm.h>
|
---|
38 | #include <arch/mm.h>
|
---|
39 | #include <halt.h>
|
---|
40 | #include <printf.h>
|
---|
41 | #include <memstr.h>
|
---|
42 | #include <version.h>
|
---|
43 | #include <macros.h>
|
---|
44 | #include <align.h>
|
---|
45 | #include <stdbool.h>
|
---|
46 | #include <str.h>
|
---|
47 | #include <errno.h>
|
---|
48 | #include <inflate.h>
|
---|
49 | #include <arch/cp15.h>
|
---|
50 | #include "../../components.h"
|
---|
51 |
|
---|
52 | #define TOP2ADDR(top) (((void *) PA2KA(BOOT_OFFSET)) + (top))
|
---|
53 |
|
---|
54 | extern void *bdata_start;
|
---|
55 | extern void *bdata_end;
|
---|
56 |
|
---|
57 | static inline void clean_dcache_poc(void *address, size_t size)
|
---|
58 | {
|
---|
59 | const uintptr_t addr = (uintptr_t) address;
|
---|
60 |
|
---|
61 | #if !defined(PROCESSOR_ARCH_armv7_a)
|
---|
62 | bool sep;
|
---|
63 | if (MIDR_read() != CTR_read()) {
|
---|
64 | sep = (CTR_read() & CTR_SEP_FLAG) == CTR_SEP_FLAG;
|
---|
65 | } else {
|
---|
66 | printf("Unknown cache type.\n");
|
---|
67 | halt();
|
---|
68 | }
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | for (uintptr_t a = ALIGN_DOWN(addr, CP15_C7_MVA_ALIGN); a < addr + size;
|
---|
72 | a += CP15_C7_MVA_ALIGN) {
|
---|
73 | #if defined(PROCESSOR_ARCH_armv7_a)
|
---|
74 | DCCMVAC_write(a);
|
---|
75 | #else
|
---|
76 | if (sep)
|
---|
77 | DCCMVA_write(a);
|
---|
78 | else
|
---|
79 | CCMVA_write(a);
|
---|
80 | #endif
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | static bootinfo_t bootinfo;
|
---|
85 |
|
---|
86 | void bootstrap(void)
|
---|
87 | {
|
---|
88 | /* Enable MMU and caches */
|
---|
89 | mmu_start();
|
---|
90 | version_print();
|
---|
91 |
|
---|
92 | printf("Boot data: %p -> %p\n", &bdata_start, &bdata_end);
|
---|
93 | printf("\nMemory statistics\n");
|
---|
94 | printf(" %p|%p: bootstrap stack\n", &boot_stack, &boot_stack);
|
---|
95 | printf(" %p|%p: bootstrap page table\n", &boot_pt, &boot_pt);
|
---|
96 | printf(" %p|%p: boot info structure\n", &bootinfo, &bootinfo);
|
---|
97 | printf(" %p|%p: kernel entry point\n",
|
---|
98 | (void *) PA2KA(BOOT_OFFSET), (void *) BOOT_OFFSET);
|
---|
99 |
|
---|
100 | for (size_t i = 0; i < COMPONENTS; i++) {
|
---|
101 | printf(" %p|%p: %s image (%u/%u bytes)\n", components[i].addr,
|
---|
102 | components[i].addr, components[i].name, components[i].inflated,
|
---|
103 | components[i].size);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void *dest[COMPONENTS];
|
---|
107 | size_t top = 0;
|
---|
108 | size_t cnt = 0;
|
---|
109 | bootinfo.cnt = 0;
|
---|
110 | for (size_t i = 0; i < min(COMPONENTS, TASKMAP_MAX_RECORDS); i++) {
|
---|
111 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
112 |
|
---|
113 | if (i > 0) {
|
---|
114 | bootinfo.tasks[bootinfo.cnt].addr = TOP2ADDR(top);
|
---|
115 | bootinfo.tasks[bootinfo.cnt].size = components[i].inflated;
|
---|
116 |
|
---|
117 | str_cpy(bootinfo.tasks[bootinfo.cnt].name,
|
---|
118 | BOOTINFO_TASK_NAME_BUFLEN, components[i].name);
|
---|
119 |
|
---|
120 | bootinfo.cnt++;
|
---|
121 | }
|
---|
122 |
|
---|
123 | dest[i] = TOP2ADDR(top);
|
---|
124 | top += components[i].inflated;
|
---|
125 | cnt++;
|
---|
126 | }
|
---|
127 |
|
---|
128 | printf("\nInflating components ... ");
|
---|
129 |
|
---|
130 | for (size_t i = cnt; i > 0; i--) {
|
---|
131 | void *tail = components[i - 1].addr + components[i - 1].size;
|
---|
132 | if (tail >= dest[i - 1]) {
|
---|
133 | printf("\n%s: Image too large to fit (%p >= %p), halting.\n",
|
---|
134 | components[i].name, tail, dest[i - 1]);
|
---|
135 | halt();
|
---|
136 | }
|
---|
137 |
|
---|
138 | printf("%s ", components[i - 1].name);
|
---|
139 |
|
---|
140 | int err = inflate(components[i - 1].addr, components[i - 1].size,
|
---|
141 | dest[i - 1], components[i - 1].inflated);
|
---|
142 | if (err != EOK) {
|
---|
143 | printf("\n%s: Inflating error %d\n", components[i - 1].name, err);
|
---|
144 | halt();
|
---|
145 | }
|
---|
146 | /* Make sure data are in the memory, ICache will need them */
|
---|
147 | clean_dcache_poc(dest[i - 1], components[i - 1].inflated);
|
---|
148 | }
|
---|
149 |
|
---|
150 | printf(".\n");
|
---|
151 |
|
---|
152 | /* Flush PT too. We need this if we disable caches later */
|
---|
153 | clean_dcache_poc(boot_pt, PTL0_ENTRIES * PTL0_ENTRY_SIZE);
|
---|
154 |
|
---|
155 | printf("Booting the kernel...\n");
|
---|
156 | jump_to_kernel((void *) PA2KA(BOOT_OFFSET), &bootinfo);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** @}
|
---|
160 | */
|
---|