source: mainline/boot/arch/arm64/src/main.c

Last change on this file was b169619, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 20 months ago

Deduplicate mem functions

There are a number of functions which are copied between
kernel, libc, and potentially boot too. mem*() functions
are first such offenders. All this duplicate code will
be moved to directory 'common'.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * Copyright (c) 2015 Petr Pavlu
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 boot_arm64
30 * @{
31 */
32/** @file
33 * @brief Bootstrap.
34 */
35
36#include <stddef.h>
37#include <align.h>
38#include <arch/arch.h>
39#include <arch/asm.h>
40#include <arch/barrier.h>
41#include <arch/main.h>
42#include <arch/regutils.h>
43#include <arch/types.h>
44#include <errno.h>
45#include <inflate.h>
46#include <kernel.h>
47#include <macros.h>
48#include <mem.h>
49#include <payload.h>
50#include <printf.h>
51#include <putchar.h>
52#include <str.h>
53#include <version.h>
54
55static efi_system_table_t *efi_system_table;
56
57/** Translate given UEFI memory type to the bootinfo memory type.
58 *
59 * @param type UEFI memory type.
60 */
61static memtype_t get_memtype(uint32_t type)
62{
63 switch (type) {
64 case EFI_RESERVED:
65 case EFI_RUNTIME_SERVICES_CODE:
66 case EFI_RUNTIME_SERVICES_DATA:
67 case EFI_UNUSABLE_MEMORY:
68 case EFI_ACPI_MEMORY_NVS:
69 case EFI_MEMORY_MAPPED_IO:
70 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
71 case EFI_PAL_CODE:
72 return MEMTYPE_UNUSABLE;
73 case EFI_LOADER_CODE:
74 case EFI_LOADER_DATA:
75 case EFI_BOOT_SERVICES_CODE:
76 case EFI_BOOT_SERVICES_DATA:
77 case EFI_CONVENTIONAL_MEMORY:
78 case EFI_PERSISTENT_MEMORY:
79 return MEMTYPE_AVAILABLE;
80 case EFI_ACPI_RECLAIM_MEMORY:
81 return MEMTYPE_ACPI_RECLAIM;
82 }
83
84 return MEMTYPE_UNUSABLE;
85}
86
87/** Send a byte to the UEFI console output.
88 *
89 * @param byte Byte to send.
90 */
91static void scons_sendb(uint8_t byte)
92{
93 int16_t out[2] = { byte, '\0' };
94 efi_system_table->cons_out->output_string(efi_system_table->cons_out,
95 out);
96}
97
98/** Display a character.
99 *
100 * @param ch Character to display.
101 *
102 */
103void putuchar(char32_t ch)
104{
105 if (ch == '\n')
106 scons_sendb('\r');
107
108 if (ascii_check(ch))
109 scons_sendb((uint8_t) ch);
110 else
111 scons_sendb('?');
112}
113
114efi_status_t bootstrap(void *efi_handle_in,
115 efi_system_table_t *efi_system_table_in, void *load_address)
116{
117 efi_status_t status;
118 uint64_t memmap = 0;
119 sysarg_t memmap_size;
120 sysarg_t memmap_key;
121 sysarg_t memmap_descriptor_size;
122 uint32_t memmap_descriptor_version;
123 uint64_t alloc_addr = 0;
124 sysarg_t alloc_pages = 0;
125
126 /*
127 * Bootinfo structure is dynamically allocated in the ARM64 port. It is
128 * placed directly after the inflated components. This assures that if
129 * the kernel identity maps the first gigabyte of the main memory in the
130 * kernel/upper address space then it can access the bootinfo because
131 * the inflated components and bootinfo can always fit in this area.
132 */
133 bootinfo_t *bootinfo;
134
135 efi_system_table = efi_system_table_in;
136
137 version_print();
138
139 printf("Boot loader: %p -> %p\n", loader_start, loader_end);
140 printf("\nMemory statistics\n");
141 printf(" %p|%p: loader\n", load_address, load_address);
142 printf(" %p|%p: UEFI system table\n", efi_system_table_in,
143 efi_system_table_in);
144
145 /* Obtain memory map. */
146 status = efi_get_memory_map(efi_system_table, &memmap_size,
147 (efi_v1_memdesc_t **) &memmap, &memmap_key, &memmap_descriptor_size,
148 &memmap_descriptor_version);
149 if (status != EFI_SUCCESS) {
150 printf("Error: Unable to obtain initial memory map, status "
151 "code: %" PRIx64 ".\n", status);
152 goto fail;
153 }
154
155 /* Find start of usable RAM. */
156 uint64_t memory_base = (uint64_t) -1;
157 for (sysarg_t i = 0; i < memmap_size / memmap_descriptor_size; i++) {
158 efi_v1_memdesc_t *desc = (void *) memmap +
159 (i * memmap_descriptor_size);
160 if (get_memtype(desc->type) != MEMTYPE_AVAILABLE ||
161 !(desc->attribute & EFI_MEMORY_WB))
162 continue;
163
164 if (desc->phys_start < memory_base)
165 memory_base = desc->phys_start;
166 }
167
168 /* Deallocate memory holding the map. */
169 efi_system_table->boot_services->free_pool((void *) memmap);
170 memmap = 0;
171
172 if (memory_base == (uint64_t) -1) {
173 printf("Error: Memory map does not contain any usable RAM.\n");
174 status = EFI_UNSUPPORTED;
175 goto fail;
176 }
177
178 /*
179 * Check that everything is aligned on a 4 KiB boundary and the kernel can
180 * be placed by the decompression code at a correct address.
181 */
182
183 /* Statically check PAGE_SIZE and BOOT_OFFSET. */
184 _Static_assert(PAGE_SIZE == 4096, "PAGE_SIZE must be equal to 4096");
185 _Static_assert(IS_ALIGNED(BOOT_OFFSET, PAGE_SIZE),
186 "BOOT_OFFSET must be a multiple of PAGE_SIZE");
187
188 /*
189 * Dynamically check the memory base. The condition should be always
190 * true because UEFI guarantees each physical/virtual address in the
191 * memory map is aligned on a 4 KiB boundary.
192 */
193 if (!IS_ALIGNED(memory_base, PAGE_SIZE)) {
194 printf("Error: Start of usable RAM (%p) is not aligned on a "
195 "4 KiB boundary.\n", (void *) memory_base);
196 status = EFI_UNSUPPORTED;
197 goto fail;
198 }
199
200 /*
201 * Calculate where the components (including the kernel) will get
202 * placed.
203 */
204 uint64_t decompress_base = memory_base + BOOT_OFFSET;
205 printf(" %p|%p: kernel entry point\n", (void *) decompress_base,
206 (void *) decompress_base);
207
208 /*
209 * Allocate memory for the decompressed components and for the bootinfo.
210 */
211 uint64_t component_pages =
212 ALIGN_UP(payload_unpacked_size(), EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
213 uint64_t bootinfo_pages = ALIGN_UP(sizeof(*bootinfo), EFI_PAGE_SIZE) /
214 EFI_PAGE_SIZE;
215 alloc_pages = component_pages + bootinfo_pages;
216 alloc_addr = decompress_base;
217 status = efi_system_table->boot_services->allocate_pages(
218 EFI_ALLOCATE_ADDRESS, EFI_LOADER_CODE, alloc_pages, &alloc_addr);
219 if (status != EFI_SUCCESS) {
220 printf("Error: Unable to allocate memory for inflated "
221 "components and bootinfo, status code: %" PRIx64 ".\n",
222 status);
223 goto fail;
224 }
225
226 bootinfo = (void *) alloc_addr + component_pages * EFI_PAGE_SIZE;
227 printf(" %p|%p: boot info structure\n", bootinfo, bootinfo);
228 memset(bootinfo, 0, sizeof(*bootinfo));
229
230 /* Decompress the components. */
231 uint8_t *kernel_dest = (uint8_t *) alloc_addr;
232 uint8_t *ram_end = kernel_dest + component_pages * EFI_PAGE_SIZE;
233
234 extract_payload(&bootinfo->taskmap, kernel_dest, ram_end,
235 (uintptr_t) kernel_dest, smc_coherence);
236
237 /* Get final memory map. */
238 status = efi_get_memory_map(efi_system_table, &memmap_size,
239 (efi_v1_memdesc_t **) &memmap, &memmap_key, &memmap_descriptor_size,
240 &memmap_descriptor_version);
241 if (status != EFI_SUCCESS) {
242 printf("Error: Unable to obtain final memory map, status code: "
243 "%" PRIx64 ".\n", status);
244 goto fail;
245 }
246
247 /* Convert the UEFI memory map to the bootinfo representation. */
248 size_t cnt = 0;
249 memtype_t current_type = MEMTYPE_UNUSABLE;
250 void *current_start = 0;
251 size_t current_size = 0;
252 sysarg_t memmap_items_count = memmap_size / memmap_descriptor_size;
253 for (sysarg_t i = 0; i < memmap_items_count; i++) {
254 efi_v1_memdesc_t *desc = (void *) memmap +
255 (i * memmap_descriptor_size);
256
257 /* Get type of the new area. */
258 memtype_t type;
259 if (!(desc->attribute & EFI_MEMORY_WB))
260 type = MEMTYPE_UNUSABLE;
261 else
262 type = get_memtype(desc->type);
263
264 /* Try to merge the new area with the previous one. */
265 if (type == current_type &&
266 (uint64_t)current_start + current_size == desc->phys_start) {
267 current_size += desc->pages * EFI_PAGE_SIZE;
268 if (i != memmap_items_count - 1)
269 continue;
270 }
271
272 /* Record the previous area. */
273 if (current_type != MEMTYPE_UNUSABLE) {
274 if (cnt >= MEMMAP_MAX_RECORDS) {
275 printf("Error: Too many usable memory "
276 "areas.\n");
277 status = EFI_UNSUPPORTED;
278 goto fail;
279 }
280 bootinfo->memmap.zones[cnt].type = current_type;
281 bootinfo->memmap.zones[cnt].start = current_start;
282 bootinfo->memmap.zones[cnt].size = current_size;
283 cnt++;
284 }
285
286 /* Remember the new area. */
287 current_type = type;
288 current_start = (void *) desc->phys_start;
289 current_size = desc->pages * EFI_PAGE_SIZE;
290 }
291 bootinfo->memmap.cnt = cnt;
292
293 /* Flush the data cache containing bootinfo. */
294 dcache_flush(bootinfo, sizeof(*bootinfo));
295
296 uintptr_t entry = check_kernel_translated((void *) decompress_base,
297 BOOT_OFFSET);
298
299 printf("Booting the kernel...\n");
300
301 /* Exit boot services. This is a point of no return. */
302 efi_system_table->boot_services->exit_boot_services(efi_handle_in,
303 memmap_key);
304
305 entry = memory_base + KA2PA(entry);
306 jump_to_kernel((void *) entry, bootinfo);
307
308fail:
309 if (memmap != 0)
310 efi_system_table->boot_services->free_pool((void *) memmap);
311
312 if (alloc_addr != 0)
313 efi_system_table->boot_services->free_pages(alloc_addr,
314 alloc_pages);
315
316 return status;
317}
318
319/** @}
320 */
Note: See TracBrowser for help on using the repository browser.