xref: /freebsd/stand/efi/loader/arch/arm64/exec.c (revision 370e009188ba90c3290b1479aa06ec98b66e140a)
1 /*-
2  * Copyright (c) 2006 Marcel Moolenaar
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  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <stand.h>
31 #include <string.h>
32 
33 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <machine/elf.h>
36 
37 #include <bootstrap.h>
38 
39 #include <efi.h>
40 #include <efilib.h>
41 
42 #include "loader_efi.h"
43 #include "cache.h"
44 
45 #include "platform/acfreebsd.h"
46 #include "acconfig.h"
47 #define ACPI_SYSTEM_XFACE
48 #define ACPI_USE_SYSTEM_INTTYPES
49 #include "actypes.h"
50 #include "actbl.h"
51 
52 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
53 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
54 
55 static int elf64_exec(struct preloaded_file *amp);
56 static int elf64_obj_exec(struct preloaded_file *amp);
57 
58 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
59     bool exit_bs);
60 
61 static struct file_format arm64_elf = {
62 	elf64_loadfile,
63 	elf64_exec
64 };
65 
66 struct file_format *file_formats[] = {
67 	&arm64_elf,
68 	NULL
69 };
70 
71 static int
72 elf64_exec(struct preloaded_file *fp)
73 {
74 	vm_offset_t modulep, kernendp;
75 	vm_offset_t clean_addr;
76 	size_t clean_size;
77 	struct file_metadata *md;
78 	ACPI_TABLE_RSDP *rsdp;
79 	Elf_Ehdr *ehdr;
80 	char buf[24];
81 	int err, revision;
82 	void (*entry)(vm_offset_t);
83 
84 	/*
85 	 * Report the RSDP to the kernel. The old code used the 'hints' method
86 	 * to communite this to the kernel. However, while convenient, the
87 	 * 'hints' method is fragile and does not work when static hints are
88 	 * compiled into the kernel. Instead, move to setting different tunables
89 	 * that start with acpi. The old 'hints' can be removed before we branch
90 	 * for FreeBSD 15.
91 	 */
92 
93 	rsdp = efi_get_table(&acpi20_guid);
94 	if (rsdp == NULL) {
95 		rsdp = efi_get_table(&acpi_guid);
96 	}
97 	if (rsdp != NULL) {
98 		sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
99 		setenv("hint.acpi.0.rsdp", buf, 1);
100 		setenv("acpi.rsdp", buf, 1);
101 		revision = rsdp->Revision;
102 		if (revision == 0)
103 			revision = 1;
104 		sprintf(buf, "%d", revision);
105 		setenv("hint.acpi.0.revision", buf, 1);
106 		setenv("acpi.revision", buf, 1);
107 		strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
108 		buf[sizeof(rsdp->OemId)] = '\0';
109 		setenv("hint.acpi.0.oem", buf, 1);
110 		setenv("acpi.oem", buf, 1);
111 		sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
112 		setenv("hint.acpi.0.rsdt", buf, 1);
113 		setenv("acpi.rsdt", buf, 1);
114 		if (revision >= 2) {
115 			/* XXX extended checksum? */
116 			sprintf(buf, "0x%016llx",
117 			    (unsigned long long)rsdp->XsdtPhysicalAddress);
118 			setenv("hint.acpi.0.xsdt", buf, 1);
119 			setenv("acpi.xsdt", buf, 1);
120 			sprintf(buf, "%d", rsdp->Length);
121 			setenv("hint.acpi.0.xsdt_length", buf, 1);
122 			setenv("acpi.xsdt_length", buf, 1);
123 		}
124 	}
125 
126 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
127         	return(EFTYPE);
128 
129 	ehdr = (Elf_Ehdr *)&(md->md_data);
130 	entry = efi_translate(ehdr->e_entry);
131 
132 	efi_time_fini();
133 	err = bi_load(fp->f_args, &modulep, &kernendp, true);
134 	if (err != 0) {
135 		efi_time_init();
136 		return (err);
137 	}
138 
139 	dev_cleanup();
140 
141 	/* Clean D-cache under kernel area and invalidate whole I-cache */
142 	clean_addr = (vm_offset_t)efi_translate(fp->f_addr);
143 	clean_size = (vm_offset_t)efi_translate(kernendp) - clean_addr;
144 
145 	cpu_flush_dcache((void *)clean_addr, clean_size);
146 	cpu_inval_icache();
147 
148 	(*entry)(modulep);
149 	panic("exec returned");
150 }
151 
152 static int
153 elf64_obj_exec(struct preloaded_file *fp)
154 {
155 
156 	printf("%s called for preloaded file %p (=%s):\n", __func__, fp,
157 	    fp->f_name);
158 	return (ENOSYS);
159 }
160 
161