1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright 2020 Joyent, Inc. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/inttypes.h> 33 #include <sys/systm.h> 34 #include <sys/elf.h> 35 #include <sys/elf_notes.h> 36 37 #include <util/memcpy.h> 38 39 #include "dboot_xboot.h" 40 #include "dboot_elfload.h" 41 #include "dboot_printf.h" 42 43 static caddr_t elf_file = 0; 44 45 #define PGETBYTES(offset) ((void *)(elf_file + (offset))) 46 47 static void * 48 getehdr(void) 49 { 50 uchar_t *ident; 51 void *hdr = NULL; 52 53 ident = PGETBYTES(0); 54 if (ident == NULL) 55 dboot_panic("Cannot read kernel ELF header"); 56 57 if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 || 58 ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) 59 dboot_panic("not an ELF file!"); 60 61 if (ident[EI_CLASS] == ELFCLASS32) 62 hdr = PGETBYTES(0); 63 else if (ident[EI_CLASS] == ELFCLASS64) 64 hdr = PGETBYTES(0); 65 else 66 dboot_panic("Unknown ELF class"); 67 68 return (hdr); 69 } 70 71 72 /* 73 * parse the elf file for program information 74 */ 75 int 76 dboot_elfload64(uintptr_t file_image) 77 { 78 Elf64_Ehdr *eh; 79 Elf64_Phdr *phdr; 80 caddr_t allphdrs; 81 int i; 82 paddr_t src; 83 paddr_t dst; 84 85 elf_file = (caddr_t)file_image; 86 87 allphdrs = NULL; 88 89 eh = getehdr(); 90 if (eh == NULL) 91 dboot_panic("getehdr() failed"); 92 93 if (eh->e_type != ET_EXEC) 94 dboot_panic("not ET_EXEC, e_type = 0x%x", eh->e_type); 95 96 if (eh->e_phnum == 0 || eh->e_phoff == 0) 97 dboot_panic("no program headers"); 98 99 /* 100 * Get the program headers. 101 */ 102 allphdrs = PGETBYTES(eh->e_phoff); 103 if (allphdrs == NULL) 104 dboot_panic("Failed to get program headers e_phnum = %d", 105 eh->e_phnum); 106 107 /* 108 * Next look for interesting program headers. 109 */ 110 for (i = 0; i < eh->e_phnum; i++) { 111 /*LINTED [ELF program header alignment]*/ 112 phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i); 113 114 /* 115 * Dynamically-linked executable. 116 * Complain. 117 */ 118 if (phdr->p_type == PT_INTERP) { 119 dboot_printf("warning: PT_INTERP section\n"); 120 continue; 121 } 122 123 /* 124 * at this point we only care about PT_LOAD segments 125 */ 126 if (phdr->p_type != PT_LOAD) 127 continue; 128 129 if (phdr->p_flags == (PF_R | PF_W) && phdr->p_vaddr == 0) { 130 dboot_printf("warning: krtld reloc info?\n"); 131 continue; 132 } 133 134 /* 135 * If memory size is zero just ignore this header. 136 */ 137 if (phdr->p_memsz == 0) 138 continue; 139 140 /* 141 * If load address 1:1 then ignore this header. 142 */ 143 if (phdr->p_paddr == phdr->p_vaddr) { 144 if (prom_debug) 145 dboot_printf("Skipping PT_LOAD segment for " 146 "paddr = 0x%lx\n", (ulong_t)phdr->p_paddr); 147 continue; 148 } 149 150 /* 151 * copy the data to kernel area 152 */ 153 if (phdr->p_paddr != FOUR_MEG && phdr->p_paddr != 2 * FOUR_MEG) 154 dboot_panic("Bad paddr for kernel nucleus segment"); 155 src = (uintptr_t)PGETBYTES(phdr->p_offset); 156 dst = ktext_phys + phdr->p_paddr - FOUR_MEG; 157 if (prom_debug) 158 dboot_printf("copying %ld bytes from ELF offset 0x%lx " 159 "to physaddr 0x%lx (va=0x%lx)\n", 160 (ulong_t)phdr->p_filesz, (ulong_t)phdr->p_offset, 161 (ulong_t)dst, (ulong_t)phdr->p_vaddr); 162 (void) memcpy((void *)(uintptr_t)dst, 163 (void *)(uintptr_t)src, (size_t)phdr->p_filesz); 164 165 /* 166 * Clear space from oversized segments, bss. 167 * Note, the bss actual start is 168 * P2ROUNDUP(dst + phdr->p_filesz, shdr->sh_addralign), 169 * but we can wipe everything from phdr->p_filesz to 170 * phdr->p_memsz. 171 */ 172 if (phdr->p_filesz < phdr->p_memsz) { 173 if (prom_debug) { 174 dboot_printf("zeroing BSS %zu bytes from " 175 "physaddr 0x%" PRIx64 176 " (end=0x%" PRIx64 ")\n", 177 (size_t)(phdr->p_memsz - phdr->p_filesz), 178 dst + phdr->p_filesz, 179 dst + phdr->p_memsz - 1); 180 } 181 (void) memset((void *)(uintptr_t)(dst + phdr->p_filesz), 182 0, phdr->p_memsz - phdr->p_filesz); 183 } 184 } 185 186 /* 187 * Ignore the intepreter (or should we die if there is one??) 188 */ 189 return (0); 190 } 191