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 
29 #include <stand.h>
30 #include <string.h>
31 
32 #include <sys/param.h>
33 #include <sys/linker.h>
34 #include <machine/elf.h>
35 
36 #include <bootstrap.h>
37 
38 #include <efi.h>
39 #include <efilib.h>
40 
41 #include "loader_efi.h"
42 #include "cache.h"
43 
44 #include "platform/acfreebsd.h"
45 #include "acconfig.h"
46 #define ACPI_SYSTEM_XFACE
47 #define ACPI_USE_SYSTEM_INTTYPES
48 #include "actypes.h"
49 #include "actbl.h"
50 
51 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
52 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
53 
54 static int elf64_exec(struct preloaded_file *amp);
55 static int elf64_obj_exec(struct preloaded_file *amp);
56 
57 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
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
elf64_exec(struct preloaded_file * fp)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 	rsdp = efi_get_table(&acpi20_guid);
83 	if (rsdp == NULL) {
84 		rsdp = efi_get_table(&acpi_guid);
85 	}
86 	if (rsdp != NULL) {
87 		sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
88 		setenv("hint.acpi.0.rsdp", buf, 1);
89 		revision = rsdp->Revision;
90 		if (revision == 0)
91 			revision = 1;
92 		sprintf(buf, "%d", revision);
93 		setenv("hint.acpi.0.revision", buf, 1);
94 		strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
95 		buf[sizeof(rsdp->OemId)] = '\0';
96 		setenv("hint.acpi.0.oem", buf, 1);
97 		sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
98 		setenv("hint.acpi.0.rsdt", buf, 1);
99 		if (revision >= 2) {
100 			/* XXX extended checksum? */
101 			sprintf(buf, "0x%016llx",
102 			    (unsigned long long)rsdp->XsdtPhysicalAddress);
103 			setenv("hint.acpi.0.xsdt", buf, 1);
104 			sprintf(buf, "%d", rsdp->Length);
105 			setenv("hint.acpi.0.xsdt_length", buf, 1);
106 		}
107 	}
108 
109 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
110 		return (EFTYPE);
111 
112 	ehdr = (Elf_Ehdr *)&(md->md_data);
113 	entry = efi_translate(ehdr->e_entry);
114 
115 	efi_time_fini();
116 	err = bi_load(fp->f_args, &modulep, &kernendp);
117 	if (err != 0) {
118 		efi_time_init();
119 		return (err);
120 	}
121 
122 	dev_cleanup();
123 
124 	/* Clean D-cache under kernel area and invalidate whole I-cache */
125 	clean_addr = (vm_offset_t)efi_translate(fp->f_addr);
126 	clean_size = (vm_offset_t)efi_translate(kernendp) - clean_addr;
127 
128 	cpu_flush_dcache((void *)clean_addr, clean_size);
129 	cpu_inval_icache(NULL, 0);
130 
131 	(*entry)(modulep);
132 	panic("exec returned");
133 }
134 
135 static int
elf64_obj_exec(struct preloaded_file * fp)136 elf64_obj_exec(struct preloaded_file *fp)
137 {
138 
139 	printf("%s called for preloaded file %p (=%s):\n", __func__, fp,
140 	    fp->f_name);
141 	return (ENOSYS);
142 }
143 
144