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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f8fb03b was 4099129, checked in by Petr Pavlu <setup@…>, 7 years ago

arm64: Use _Static_assert() for a compile-time check

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[84176f3]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 <memstr.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 */
102void putwchar(wchar_t ch)
103{
104 if (ch == '\n')
105 scons_sendb('\r');
106
107 if (ascii_check(ch))
108 scons_sendb((uint8_t) ch);
109 else
110 scons_sendb('?');
111}
112
113efi_status_t bootstrap(void *efi_handle_in,
114 efi_system_table_t *efi_system_table_in, void *load_address)
115{
116 efi_status_t status;
117 uint64_t current_el;
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 /* Validate the exception level. */
146 current_el = CurrentEL_read();
147 if (current_el != CURRENT_EL_EL1) {
148 printf("Error: Unexpected CurrentEL value %0#18" PRIx64 ".\n",
149 current_el);
150 status = EFI_UNSUPPORTED;
151 goto fail;
152 }
153
154 /* Obtain memory map. */
155 status = efi_get_memory_map(efi_system_table, &memmap_size,
156 (efi_v1_memdesc_t **) &memmap, &memmap_key, &memmap_descriptor_size,
157 &memmap_descriptor_version);
158 if (status != EFI_SUCCESS) {
159 printf("Error: Unable to obtain initial memory map, status "
160 "code: %" PRIx64 ".\n", status);
161 goto fail;
162 }
163
164 /* Find start of usable RAM. */
165 uint64_t memory_base = (uint64_t) -1;
166 for (sysarg_t i = 0; i < memmap_size / memmap_descriptor_size; i++) {
167 efi_v1_memdesc_t *desc = (void *) memmap +
168 (i * memmap_descriptor_size);
169 if (get_memtype(desc->type) != MEMTYPE_AVAILABLE ||
170 !(desc->attribute & EFI_MEMORY_WB))
171 continue;
172
173 if (desc->phys_start < memory_base)
174 memory_base = desc->phys_start;
175 }
176
177 /* Deallocate memory holding the map. */
178 efi_system_table->boot_services->free_pool((void *) memmap);
179 memmap = 0;
180
181 if (memory_base == (uint64_t) -1) {
182 printf("Error: Memory map does not contain any usable RAM.\n");
183 status = EFI_UNSUPPORTED;
184 goto fail;
185 }
186
187 /*
188 * Check that everything is aligned on a 4kB boundary and the kernel can
189 * be placed by the decompression code at a correct address.
190 */
191
192 /* Statically check PAGE_SIZE and BOOT_OFFSET. */
[4099129]193 _Static_assert(PAGE_SIZE == 4096, "PAGE_SIZE must be equal to 4096");
194 _Static_assert(IS_ALIGNED(BOOT_OFFSET, PAGE_SIZE),
195 "BOOT_OFFSET must be a multiple of PAGE_SIZE");
196
[84176f3]197 /*
198 * Dynamically check the memory base. The condition should be always
199 * true because UEFI guarantees each physical/virtual address in the
200 * memory map is aligned on a 4kB boundary.
201 */
202 if (!IS_ALIGNED(memory_base, PAGE_SIZE)) {
203 printf("Error: Start of usable RAM (%p) is not aligned on a "
204 "4kB boundary.\n", (void *) memory_base);
205 status = EFI_UNSUPPORTED;
206 goto fail;
207 }
208
209 /*
210 * Calculate where the components (including the kernel) will get
211 * placed.
212 */
213 uint64_t decompress_base = memory_base + BOOT_OFFSET;
214 printf(" %p|%p: kernel entry point\n", (void *) decompress_base,
215 (void *) decompress_base);
216
217 /*
218 * Allocate memory for the decompressed components and for the bootinfo.
219 */
220 uint64_t component_pages =
221 ALIGN_UP(payload_unpacked_size(), EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
222 uint64_t bootinfo_pages = ALIGN_UP(sizeof(*bootinfo), EFI_PAGE_SIZE) /
223 EFI_PAGE_SIZE;
224 alloc_pages = component_pages + bootinfo_pages;
225 alloc_addr = decompress_base;
226 status = efi_system_table->boot_services->allocate_pages(
227 EFI_ALLOCATE_ADDRESS, EFI_LOADER_CODE, alloc_pages, &alloc_addr);
228 if (status != EFI_SUCCESS) {
229 printf("Error: Unable to allocate memory for inflated "
230 "components and bootinfo, status code: %" PRIx64 ".\n",
231 status);
232 goto fail;
233 }
234
235 bootinfo = (void *) alloc_addr + component_pages * EFI_PAGE_SIZE;
236 printf(" %p|%p: boot info structure\n", bootinfo, bootinfo);
237 memset(bootinfo, 0, sizeof(*bootinfo));
238
239 /* Decompress the components. */
240 uint8_t *kernel_dest = (uint8_t *) alloc_addr;
241 uint8_t *ram_end = kernel_dest + component_pages * EFI_PAGE_SIZE;
242
243 extract_payload(&bootinfo->taskmap, kernel_dest, ram_end,
244 (uintptr_t) kernel_dest, ensure_visibility);
245
246 /* Get final memory map. */
247 status = efi_get_memory_map(efi_system_table, &memmap_size,
248 (efi_v1_memdesc_t **) &memmap, &memmap_key, &memmap_descriptor_size,
249 &memmap_descriptor_version);
250 if (status != EFI_SUCCESS) {
251 printf("Error: Unable to obtain final memory map, status code: "
252 "%" PRIx64 ".\n", status);
253 goto fail;
254 }
255
256 /* Convert the UEFI memory map to the bootinfo representation. */
257 size_t cnt = 0;
258 memtype_t current_type = MEMTYPE_UNUSABLE;
259 void *current_start = 0;
260 size_t current_size = 0;
261 sysarg_t memmap_items_count = memmap_size / memmap_descriptor_size;
262 for (sysarg_t i = 0; i < memmap_items_count; i++) {
263 efi_v1_memdesc_t *desc = (void *) memmap +
264 (i * memmap_descriptor_size);
265
266 /* Get type of the new area. */
267 memtype_t type;
268 if (!(desc->attribute & EFI_MEMORY_WB))
269 type = MEMTYPE_UNUSABLE;
270 else
271 type = get_memtype(desc->type);
272
273 /* Try to merge the new area with the previous one. */
274 if (type == current_type &&
275 (uint64_t)current_start + current_size == desc->phys_start) {
276 current_size += desc->pages * EFI_PAGE_SIZE;
277 if (i != memmap_items_count - 1)
278 continue;
279 }
280
281 /* Record the previous area. */
282 if (current_type != MEMTYPE_UNUSABLE) {
283 if (cnt >= MEMMAP_MAX_RECORDS) {
284 printf("Error: Too many usable memory "
285 "areas.\n");
286 status = EFI_UNSUPPORTED;
287 goto fail;
288 }
289 bootinfo->memmap.zones[cnt].type = current_type;
290 bootinfo->memmap.zones[cnt].start = current_start;
291 bootinfo->memmap.zones[cnt].size = current_size;
292 cnt++;
293 }
294
295 /* Remember the new area. */
296 current_type = type;
297 current_start = (void *) desc->phys_start;
298 current_size = desc->pages * EFI_PAGE_SIZE;
299 }
300 bootinfo->memmap.cnt = cnt;
301
302 uintptr_t entry = check_kernel_translated((void *) decompress_base,
303 BOOT_OFFSET);
304
305 printf("Booting the kernel...\n");
306
307 /* Exit boot services. This is a point of no return. */
308 efi_system_table->boot_services->exit_boot_services(efi_handle_in,
309 memmap_key);
310
311 entry = memory_base + KA2PA(entry);
312 jump_to_kernel((void *) entry, bootinfo);
313
314fail:
315 if (memmap != 0)
316 efi_system_table->boot_services->free_pool((void *) memmap);
317
318 if (alloc_addr != 0)
319 efi_system_table->boot_services->free_pages(alloc_addr,
320 alloc_pages);
321
322 return status;
323}
324
325/** @}
326 */
Note: See TracBrowser for help on using the repository browser.