1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh * Copyright (c) 2006 Marcel Moolenaar
3ca987d46SWarner Losh * All rights reserved.
4ca987d46SWarner Losh *
5ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh * are met:
8ca987d46SWarner Losh *
9ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright
10ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer.
11ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright
12ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the
13ca987d46SWarner Losh * documentation and/or other materials provided with the distribution.
14ca987d46SWarner Losh *
15ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16ca987d46SWarner Losh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17ca987d46SWarner Losh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18ca987d46SWarner Losh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19ca987d46SWarner Losh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20ca987d46SWarner Losh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21ca987d46SWarner Losh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22ca987d46SWarner Losh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23ca987d46SWarner Losh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24ca987d46SWarner Losh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ca987d46SWarner Losh */
26ca987d46SWarner Losh
27ca987d46SWarner Losh #include <stand.h>
28ca987d46SWarner Losh #include <string.h>
29ca987d46SWarner Losh
30ca987d46SWarner Losh #include <sys/param.h>
31ca987d46SWarner Losh #include <sys/linker.h>
32ca987d46SWarner Losh #include <machine/elf.h>
33ca987d46SWarner Losh
34ca987d46SWarner Losh #include <bootstrap.h>
35ca987d46SWarner Losh
36ca987d46SWarner Losh #include <efi.h>
37ca987d46SWarner Losh #include <efilib.h>
38ca987d46SWarner Losh
39ca987d46SWarner Losh #include "loader_efi.h"
40ca987d46SWarner Losh #include "cache.h"
41ca987d46SWarner Losh
42ca987d46SWarner Losh static int elf64_exec(struct preloaded_file *amp);
43ca987d46SWarner Losh static int elf64_obj_exec(struct preloaded_file *amp);
44ca987d46SWarner Losh
45ed87efbeSRoger Pau Monné int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
46ed87efbeSRoger Pau Monné bool exit_bs);
47ca987d46SWarner Losh
48ca987d46SWarner Losh static struct file_format arm64_elf = {
49ca987d46SWarner Losh elf64_loadfile,
50ca987d46SWarner Losh elf64_exec
51ca987d46SWarner Losh };
52ca987d46SWarner Losh
53ca987d46SWarner Losh struct file_format *file_formats[] = {
54ca987d46SWarner Losh &arm64_elf,
55ca987d46SWarner Losh NULL
56ca987d46SWarner Losh };
57ca987d46SWarner Losh
58ca987d46SWarner Losh static int
elf64_exec(struct preloaded_file * fp)59ca987d46SWarner Losh elf64_exec(struct preloaded_file *fp)
60ca987d46SWarner Losh {
61ca987d46SWarner Losh vm_offset_t modulep, kernendp;
62ca987d46SWarner Losh vm_offset_t clean_addr;
63ca987d46SWarner Losh size_t clean_size;
64ca987d46SWarner Losh struct file_metadata *md;
65ca987d46SWarner Losh Elf_Ehdr *ehdr;
66*0b01d457SR. Christian McDonald int err;
67ca987d46SWarner Losh void (*entry)(vm_offset_t);
68ca987d46SWarner Losh
69ca987d46SWarner Losh if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
70ca987d46SWarner Losh return(EFTYPE);
71ca987d46SWarner Losh
72ca987d46SWarner Losh ehdr = (Elf_Ehdr *)&(md->md_data);
73ca987d46SWarner Losh entry = efi_translate(ehdr->e_entry);
74ca987d46SWarner Losh
75ca987d46SWarner Losh efi_time_fini();
76ed87efbeSRoger Pau Monné err = bi_load(fp->f_args, &modulep, &kernendp, true);
77ca987d46SWarner Losh if (err != 0) {
78ca987d46SWarner Losh efi_time_init();
79ca987d46SWarner Losh return (err);
80ca987d46SWarner Losh }
81ca987d46SWarner Losh
82ca987d46SWarner Losh dev_cleanup();
83ca987d46SWarner Losh
84ca987d46SWarner Losh /* Clean D-cache under kernel area and invalidate whole I-cache */
85ca987d46SWarner Losh clean_addr = (vm_offset_t)efi_translate(fp->f_addr);
86ca987d46SWarner Losh clean_size = (vm_offset_t)efi_translate(kernendp) - clean_addr;
87ca987d46SWarner Losh
88ca987d46SWarner Losh cpu_flush_dcache((void *)clean_addr, clean_size);
89c1381f07SAndrew Turner cpu_inval_icache();
90ca987d46SWarner Losh
91ca987d46SWarner Losh (*entry)(modulep);
92ca987d46SWarner Losh panic("exec returned");
93ca987d46SWarner Losh }
94ca987d46SWarner Losh
95ca987d46SWarner Losh static int
elf64_obj_exec(struct preloaded_file * fp)96ca987d46SWarner Losh elf64_obj_exec(struct preloaded_file *fp)
97ca987d46SWarner Losh {
98ca987d46SWarner Losh
99ca987d46SWarner Losh printf("%s called for preloaded file %p (=%s):\n", __func__, fp,
100ca987d46SWarner Losh fp->f_name);
101ca987d46SWarner Losh return (ENOSYS);
102ca987d46SWarner Losh }
103ca987d46SWarner Losh
104