xref: /freebsd/stand/efi/loader/arch/arm64/exec.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 #include <stand.h>
29 #include <string.h>
30 
31 #include <sys/param.h>
32 #include <sys/linker.h>
33 #include <machine/elf.h>
34 
35 #include <bootstrap.h>
36 
37 #include <efi.h>
38 #include <efilib.h>
39 
40 #include "loader_efi.h"
41 #include "cache.h"
42 
43 #include "platform/acfreebsd.h"
44 #include "acconfig.h"
45 #define ACPI_SYSTEM_XFACE
46 #define ACPI_USE_SYSTEM_INTTYPES
47 #include "actypes.h"
48 #include "actbl.h"
49 
50 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
51 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
52 
53 static int elf64_exec(struct preloaded_file *amp);
54 static int elf64_obj_exec(struct preloaded_file *amp);
55 
56 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
57     bool exit_bs);
58 
59 static struct file_format arm64_elf = {
60 	elf64_loadfile,
61 	elf64_exec
62 };
63 
64 struct file_format *file_formats[] = {
65 	&arm64_elf,
66 	NULL
67 };
68 
69 static int
70 elf64_exec(struct preloaded_file *fp)
71 {
72 	vm_offset_t modulep, kernendp;
73 	vm_offset_t clean_addr;
74 	size_t clean_size;
75 	struct file_metadata *md;
76 	ACPI_TABLE_RSDP *rsdp;
77 	Elf_Ehdr *ehdr;
78 	char buf[24];
79 	int err, revision;
80 	void (*entry)(vm_offset_t);
81 
82 	/*
83 	 * Report the RSDP to the kernel. The old code used the 'hints' method
84 	 * to communicate this to the kernel, but this is now considered legacy.
85 	 * Instead, move to setting different tunables that start with acpi.
86 	 * The old 'hints' can be removed before we branch for FreeBSD 15.
87 	 */
88 
89 	rsdp = efi_get_table(&acpi20_guid);
90 	if (rsdp == NULL) {
91 		rsdp = efi_get_table(&acpi_guid);
92 	}
93 	if (rsdp != NULL) {
94 		sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
95 		setenv("hint.acpi.0.rsdp", buf, 1);
96 		setenv("acpi.rsdp", buf, 1);
97 		revision = rsdp->Revision;
98 		if (revision == 0)
99 			revision = 1;
100 		sprintf(buf, "%d", revision);
101 		setenv("hint.acpi.0.revision", buf, 1);
102 		setenv("acpi.revision", buf, 1);
103 		strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
104 		buf[sizeof(rsdp->OemId)] = '\0';
105 		setenv("hint.acpi.0.oem", buf, 1);
106 		setenv("acpi.oem", buf, 1);
107 		sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
108 		setenv("hint.acpi.0.rsdt", buf, 1);
109 		setenv("acpi.rsdt", buf, 1);
110 		if (revision >= 2) {
111 			/* XXX extended checksum? */
112 			sprintf(buf, "0x%016llx",
113 			    (unsigned long long)rsdp->XsdtPhysicalAddress);
114 			setenv("hint.acpi.0.xsdt", buf, 1);
115 			setenv("acpi.xsdt", buf, 1);
116 			sprintf(buf, "%d", rsdp->Length);
117 			setenv("hint.acpi.0.xsdt_length", buf, 1);
118 			setenv("acpi.xsdt_length", buf, 1);
119 		}
120 	}
121 
122 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
123         	return(EFTYPE);
124 
125 	ehdr = (Elf_Ehdr *)&(md->md_data);
126 	entry = efi_translate(ehdr->e_entry);
127 
128 	efi_time_fini();
129 	err = bi_load(fp->f_args, &modulep, &kernendp, true);
130 	if (err != 0) {
131 		efi_time_init();
132 		return (err);
133 	}
134 
135 	dev_cleanup();
136 
137 	/* Clean D-cache under kernel area and invalidate whole I-cache */
138 	clean_addr = (vm_offset_t)efi_translate(fp->f_addr);
139 	clean_size = (vm_offset_t)efi_translate(kernendp) - clean_addr;
140 
141 	cpu_flush_dcache((void *)clean_addr, clean_size);
142 	cpu_inval_icache();
143 
144 	(*entry)(modulep);
145 	panic("exec returned");
146 }
147 
148 static int
149 elf64_obj_exec(struct preloaded_file *fp)
150 {
151 
152 	printf("%s called for preloaded file %p (=%s):\n", __func__, fp,
153 	    fp->f_name);
154 	return (ENOSYS);
155 }
156 
157