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 .l_load = elf64_loadfile,
47 .l_exec = elf64_exec
48 };
49
50 struct file_format *file_formats[] = {
51 &arm64_elf,
52 NULL
53 };
54
55 static int
elf64_exec(struct preloaded_file * fp)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 /*
73 * we have to cleanup here because net_cleanup() doesn't work after
74 * we call ExitBootServices
75 */
76 dev_cleanup();
77
78 efi_time_fini();
79 err = bi_load(fp->f_args, &modulep, &kernendp, true);
80 if (err != 0) {
81 efi_time_init();
82 return (err);
83 }
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
elf64_obj_exec(struct preloaded_file * fp)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