1e05bc4f9SNathan Whitehorn /*- 2e05bc4f9SNathan Whitehorn * Copyright (c) 2001 Benno Rice <benno@FreeBSD.org> 3e05bc4f9SNathan Whitehorn * All rights reserved. 4e05bc4f9SNathan Whitehorn * 5e05bc4f9SNathan Whitehorn * Redistribution and use in source and binary forms, with or without 6e05bc4f9SNathan Whitehorn * modification, are permitted provided that the following conditions 7e05bc4f9SNathan Whitehorn * are met: 8e05bc4f9SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright 9e05bc4f9SNathan Whitehorn * notice, this list of conditions and the following disclaimer. 10e05bc4f9SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright 11e05bc4f9SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the 12e05bc4f9SNathan Whitehorn * documentation and/or other materials provided with the distribution. 13e05bc4f9SNathan Whitehorn * 14e05bc4f9SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15e05bc4f9SNathan Whitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16e05bc4f9SNathan Whitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17e05bc4f9SNathan Whitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18e05bc4f9SNathan Whitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19e05bc4f9SNathan Whitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20e05bc4f9SNathan Whitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21e05bc4f9SNathan Whitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22e05bc4f9SNathan Whitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23e05bc4f9SNathan Whitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24e05bc4f9SNathan Whitehorn * SUCH DAMAGE. 25e05bc4f9SNathan Whitehorn */ 26e05bc4f9SNathan Whitehorn 27e05bc4f9SNathan Whitehorn #include <sys/cdefs.h> 28e05bc4f9SNathan Whitehorn __FBSDID("$FreeBSD$"); 29e05bc4f9SNathan Whitehorn 30e05bc4f9SNathan Whitehorn #define __ELF_WORD_SIZE 64 31e05bc4f9SNathan Whitehorn 32e05bc4f9SNathan Whitehorn #include <sys/param.h> 33e05bc4f9SNathan Whitehorn #include <sys/linker.h> 34e05bc4f9SNathan Whitehorn 35e05bc4f9SNathan Whitehorn #include <machine/metadata.h> 36e05bc4f9SNathan Whitehorn #include <machine/elf.h> 37e05bc4f9SNathan Whitehorn #include <machine/md_var.h> 38e05bc4f9SNathan Whitehorn 39e05bc4f9SNathan Whitehorn #include <stand.h> 40e05bc4f9SNathan Whitehorn 41e05bc4f9SNathan Whitehorn #include "bootstrap.h" 42e05bc4f9SNathan Whitehorn #include "libofw.h" 43e05bc4f9SNathan Whitehorn #include "openfirm.h" 44e05bc4f9SNathan Whitehorn 45e05bc4f9SNathan Whitehorn extern char end[]; 46e05bc4f9SNathan Whitehorn extern vm_offset_t reloc; /* From <arch>/conf.c */ 47e05bc4f9SNathan Whitehorn 48e05bc4f9SNathan Whitehorn int 4956e53cb8SWarner Losh ppc64_ofw_elf_loadfile(char *filename, uint64_t dest, 50e05bc4f9SNathan Whitehorn struct preloaded_file **result) 51e05bc4f9SNathan Whitehorn { 52e05bc4f9SNathan Whitehorn int r; 53e05bc4f9SNathan Whitehorn 54e05bc4f9SNathan Whitehorn r = __elfN(loadfile)(filename, dest, result); 55e05bc4f9SNathan Whitehorn if (r != 0) 56e05bc4f9SNathan Whitehorn return (r); 57e05bc4f9SNathan Whitehorn 58e05bc4f9SNathan Whitehorn /* 59e05bc4f9SNathan Whitehorn * No need to sync the icache for modules: this will 60e05bc4f9SNathan Whitehorn * be done by the kernel after relocation. 61e05bc4f9SNathan Whitehorn */ 62e05bc4f9SNathan Whitehorn if (!strcmp((*result)->f_type, "elf kernel")) 63e05bc4f9SNathan Whitehorn __syncicache((void *) (*result)->f_addr, (*result)->f_size); 64e05bc4f9SNathan Whitehorn return (0); 65e05bc4f9SNathan Whitehorn } 66e05bc4f9SNathan Whitehorn 67e05bc4f9SNathan Whitehorn int 68e05bc4f9SNathan Whitehorn ppc64_ofw_elf_exec(struct preloaded_file *fp) 69e05bc4f9SNathan Whitehorn { 70e05bc4f9SNathan Whitehorn struct file_metadata *fmp; 71e05bc4f9SNathan Whitehorn vm_offset_t mdp, dtbp; 72e05bc4f9SNathan Whitehorn Elf_Ehdr *e; 73e05bc4f9SNathan Whitehorn int error; 74e05bc4f9SNathan Whitehorn intptr_t entry; 75e05bc4f9SNathan Whitehorn 76e05bc4f9SNathan Whitehorn if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) { 77e05bc4f9SNathan Whitehorn return(EFTYPE); 78e05bc4f9SNathan Whitehorn } 79e05bc4f9SNathan Whitehorn e = (Elf_Ehdr *)&fmp->md_data; 80e05bc4f9SNathan Whitehorn 81e05bc4f9SNathan Whitehorn /* Handle function descriptor for ELFv1 kernels */ 82e05bc4f9SNathan Whitehorn if ((e->e_flags & 3) == 2) 83e05bc4f9SNathan Whitehorn entry = e->e_entry; 84e05bc4f9SNathan Whitehorn else 85e05bc4f9SNathan Whitehorn entry = *(uint64_t *)(intptr_t)e->e_entry; 86e05bc4f9SNathan Whitehorn 87e05bc4f9SNathan Whitehorn if ((error = md_load64(fp->f_args, &mdp, &dtbp)) != 0) 88e05bc4f9SNathan Whitehorn return (error); 89e05bc4f9SNathan Whitehorn 90*ff7449d6SLeandro Lupori printf("Kernel entry at 0x%x ...\n", entry); 91e05bc4f9SNathan Whitehorn 92e05bc4f9SNathan Whitehorn dev_cleanup(); 93e05bc4f9SNathan Whitehorn 94e05bc4f9SNathan Whitehorn if (dtbp != 0) { 95e05bc4f9SNathan Whitehorn OF_quiesce(); 96e05bc4f9SNathan Whitehorn ((int (*)(u_long, u_long, u_long, void *, u_long))entry)(dtbp, 97e05bc4f9SNathan Whitehorn 0, 0, (void *)mdp, 0xfb5d104d); 98e05bc4f9SNathan Whitehorn } else { 99e05bc4f9SNathan Whitehorn OF_chain((void *)reloc, end - (char *)reloc, (void *)entry, 100e05bc4f9SNathan Whitehorn (void *)mdp, 0xfb5d104d); 101e05bc4f9SNathan Whitehorn } 102e05bc4f9SNathan Whitehorn 103e05bc4f9SNathan Whitehorn panic("exec returned"); 104e05bc4f9SNathan Whitehorn } 105e05bc4f9SNathan Whitehorn 106e05bc4f9SNathan Whitehorn struct file_format ofw_elf64 = 107e05bc4f9SNathan Whitehorn { 108e05bc4f9SNathan Whitehorn ppc64_ofw_elf_loadfile, 109e05bc4f9SNathan Whitehorn ppc64_ofw_elf_exec 110e05bc4f9SNathan Whitehorn }; 111