1 /* $OpenBSD: elf64_exec.c,v 1.10 2019/10/29 02:55:52 deraadt Exp $ */ 2 /* $NetBSD: elfXX_exec.c,v 1.2 2001/08/15 20:08:15 eeh Exp $ */ 3 4 /* 5 * Copyright (c) 1998-2000 Eduardo Horvath. All rights reserved. 6 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved. 7 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 8 * Copyright (C) 1995, 1996 TooLs GmbH. 9 * All rights reserved. 10 * 11 * ELF support derived from NetBSD/alpha's boot loader, written 12 * by Christopher G. Demetriou. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by TooLs GmbH. 25 * 4. The name of TooLs GmbH may not be used to endorse or promote products 26 * derived from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 34 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 35 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 36 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * First try for the boot code 42 * 43 * Input syntax is: 44 * [promdev[{:|,}partition]]/[filename] [flags] 45 */ 46 47 #define ELFSIZE 64 48 #define MB (1024 * 1024) 49 50 #define ELF_ALIGN(x) (((x) + 7) & (~7)) 51 52 #include <lib/libsa/stand.h> 53 54 #include <sys/param.h> 55 #include <sys/exec_elf.h> 56 57 #include <machine/boot_flag.h> 58 59 #ifdef SOFTRAID 60 #include <sys/param.h> 61 #include <sys/queue.h> 62 #include <sys/disklabel.h> 63 #include <dev/biovar.h> 64 #include <dev/softraidvar.h> 65 66 #include "disk.h" 67 #include "softraid_sparc64.h" 68 #endif 69 70 #include <lib/libsa/arc4.h> 71 72 #include "openfirm.h" 73 74 void syncicache(void *, int); 75 76 int 77 elf64_exec(int fd, Elf_Ehdr *elf, u_int64_t *entryp, void **ssymp, void **esymp){ 78 Elf_Shdr *shp; 79 Elf_Off off; 80 void *addr; 81 size_t size; 82 u_int align; 83 int i, first = 1; 84 int n; 85 struct openbsd_bootdata *obd; 86 #ifdef SOFTRAID 87 struct sr_boot_volume *bv; 88 #endif 89 90 /* 91 * Don't display load address for ELF; it's encoded in 92 * each section. 93 */ 94 for (i = 0; i < elf->e_phnum; i++) { 95 Elf_Phdr phdr; 96 size = lseek(fd, (size_t)(elf->e_phoff + sizeof(phdr) * i), 97 SEEK_SET); 98 if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) { 99 printf("read phdr: %s\n", strerror(errno)); 100 return (1); 101 } 102 103 if (phdr.p_type == PT_OPENBSD_BOOTDATA) { 104 memset((void *) (long)phdr.p_paddr, 0, phdr.p_filesz); 105 106 if (phdr.p_filesz < sizeof(struct openbsd_bootdata)) 107 continue; 108 109 obd = (struct openbsd_bootdata *)(long)phdr.p_paddr; 110 obd->version = BOOTDATA_VERSION; 111 obd->len = sizeof(struct openbsd_bootdata); 112 #ifdef SOFTRAID 113 /* 114 * If booting from softraid we must pass additional 115 * information to the kernel: 116 * 1) The uuid of the softraid volume we booted from. 117 * 2) The maskkey for decryption, if applicable. 118 */ 119 if (bootdev_dip && bootdev_dip->sr_vol) { 120 bv = bootdev_dip->sr_vol; 121 memcpy(obd->sr_uuid, bv->sbv_uuid.sui_id, 122 sizeof(obd->sr_uuid)); 123 if (bv->sbv_maskkey) 124 memcpy(obd->sr_maskkey, bv->sbv_maskkey, 125 sizeof(obd->sr_maskkey)); 126 } 127 128 #endif 129 continue; 130 } 131 132 if (phdr.p_type == PT_OPENBSD_RANDOMIZE) { 133 extern struct rc4_ctx randomctx; 134 135 rc4_getbytes(&randomctx, (void *)(long)phdr.p_paddr, 136 phdr.p_filesz); 137 } 138 139 if (phdr.p_type != PT_LOAD || 140 (phdr.p_flags & (PF_W|PF_X)) == 0) 141 continue; 142 143 /* Read in segment. */ 144 printf("%s%lu@0x%lx", first ? "" : "+", (u_long)phdr.p_filesz, 145 (u_long)phdr.p_vaddr); 146 (void)lseek(fd, (size_t)phdr.p_offset, SEEK_SET); 147 148 /* 149 * If the segment's VA is aligned on a 4MB boundary, align its 150 * request 4MB aligned physical memory. Otherwise use default 151 * alignment. Make sure BSS is extended to a 4MB boundary, too. 152 */ 153 align = phdr.p_align; 154 if ((phdr.p_vaddr & (4 * MB - 1)) == 0) 155 align = 4 * MB; 156 if (phdr.p_filesz < phdr.p_memsz) 157 phdr.p_memsz = roundup(phdr.p_memsz, 4 * MB); 158 phdr.p_memsz = roundup(phdr.p_memsz, PAGE_SIZE); 159 if (OF_claim((void *)(long)phdr.p_vaddr, phdr.p_memsz, align) == 160 (void *)-1) 161 panic("cannot claim memory"); 162 if (read(fd, (void *)(long)phdr.p_vaddr, phdr.p_filesz) != 163 phdr.p_filesz) { 164 printf("read segment: %s\n", strerror(errno)); 165 return (1); 166 } 167 syncicache((void *)(long)phdr.p_vaddr, phdr.p_filesz); 168 169 /* Zero BSS. */ 170 if (phdr.p_filesz < phdr.p_memsz) { 171 printf("+%lu@0x%lx", 172 (u_long)phdr.p_memsz - phdr.p_filesz, 173 (u_long)(phdr.p_vaddr + phdr.p_filesz)); 174 bzero((void *)(long)phdr.p_vaddr + phdr.p_filesz, 175 (size_t)phdr.p_memsz - phdr.p_filesz); 176 } 177 first = 0; 178 } 179 180 printf(" \n"); 181 182 /* 183 * Compute the size of the symbol table. 184 */ 185 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf_Shdr)); 186 shp = addr = alloc(elf->e_shnum * sizeof(Elf_Shdr)); 187 (void)lseek(fd, (off_t)elf->e_shoff, SEEK_SET); 188 if (read(fd, addr, (size_t)(elf->e_shnum * sizeof(Elf_Shdr))) != 189 elf->e_shnum * sizeof(Elf_Shdr)) { 190 printf("read section headers: %s\n", strerror(errno)); 191 return (1); 192 } 193 for (i = 0; i < elf->e_shnum; i++, shp++) { 194 if (shp->sh_type == SHT_NULL) 195 continue; 196 if (shp->sh_type != SHT_SYMTAB 197 && shp->sh_type != SHT_STRTAB) { 198 shp->sh_offset = 0; 199 continue; 200 } 201 size += shp->sh_size; 202 } 203 shp = addr; 204 205 /* 206 * Reserve memory for the symbols. 207 */ 208 if ((addr = OF_claim(0, roundup(size, PAGE_SIZE), PAGE_SIZE)) == (void *)-1) 209 panic("no space for symbol table"); 210 211 /* 212 * Copy the headers. 213 */ 214 elf->e_phoff = 0; 215 elf->e_shoff = sizeof(Elf_Ehdr); 216 elf->e_phentsize = 0; 217 elf->e_phnum = 0; 218 bcopy(elf, addr, sizeof(Elf_Ehdr)); 219 bcopy(shp, addr + sizeof(Elf_Ehdr), elf->e_shnum * sizeof(Elf_Shdr)); 220 free(shp, elf->e_shnum * sizeof(Elf_Shdr)); 221 *ssymp = addr; 222 223 /* 224 * Now load the symbol sections themselves. 225 */ 226 shp = addr + sizeof(Elf_Ehdr); 227 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf_Shdr)); 228 size = ELF_ALIGN(size); 229 addr += size; 230 off = size; 231 for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) { 232 if (shp->sh_type == SHT_SYMTAB 233 || shp->sh_type == SHT_STRTAB) { 234 if (first) 235 printf("symbols @ 0x%lx ", (u_long)addr); 236 printf("%s%d", first ? "" : "+", (int)shp->sh_size); 237 (void)lseek(fd, shp->sh_offset, SEEK_SET); 238 if (read(fd, addr, shp->sh_size) != shp->sh_size) { 239 printf("read symbols: %s\n", strerror(errno)); 240 return (1); 241 } 242 addr += ELF_ALIGN(shp->sh_size); 243 shp->sh_offset = off; 244 off += ELF_ALIGN(shp->sh_size); 245 first = 0; 246 } 247 } 248 *esymp = addr; 249 250 *entryp = elf->e_entry; 251 return (0); 252 } 253 254 #undef ELF_ALIGN 255