xref: /freebsd/stand/efi/loader/arch/arm64/exec.c (revision 4b9d6057)
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 static int elf64_exec(struct preloaded_file *amp);
44 static int elf64_obj_exec(struct preloaded_file *amp);
45 
46 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
47     bool exit_bs);
48 
49 static struct file_format arm64_elf = {
50 	elf64_loadfile,
51 	elf64_exec
52 };
53 
54 struct file_format *file_formats[] = {
55 	&arm64_elf,
56 	NULL
57 };
58 
59 static int
60 elf64_exec(struct preloaded_file *fp)
61 {
62 	vm_offset_t modulep, kernendp;
63 	vm_offset_t clean_addr;
64 	size_t clean_size;
65 	struct file_metadata *md;
66 	Elf_Ehdr *ehdr;
67 	int err;
68 	void (*entry)(vm_offset_t);
69 
70 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
71         	return(EFTYPE);
72 
73 	ehdr = (Elf_Ehdr *)&(md->md_data);
74 	entry = efi_translate(ehdr->e_entry);
75 
76 	efi_time_fini();
77 	err = bi_load(fp->f_args, &modulep, &kernendp, true);
78 	if (err != 0) {
79 		efi_time_init();
80 		return (err);
81 	}
82 
83 	dev_cleanup();
84 
85 	/* Clean D-cache under kernel area and invalidate whole I-cache */
86 	clean_addr = (vm_offset_t)efi_translate(fp->f_addr);
87 	clean_size = (vm_offset_t)efi_translate(kernendp) - clean_addr;
88 
89 	cpu_flush_dcache((void *)clean_addr, clean_size);
90 	cpu_inval_icache();
91 
92 	(*entry)(modulep);
93 	panic("exec returned");
94 }
95 
96 static int
97 elf64_obj_exec(struct preloaded_file *fp)
98 {
99 
100 	printf("%s called for preloaded file %p (=%s):\n", __func__, fp,
101 	    fp->f_name);
102 	return (ENOSYS);
103 }
104 
105