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