1 /* $OpenBSD: elf64_exec.c,v 1.12 2020/06/13 12:25:09 kn 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 extern int boothowto; 75 76 void syncicache(void *, int); 77 78 int 79 elf64_exec(int fd, Elf_Ehdr *elf, u_int64_t *entryp, void **ssymp, void **esymp){ 80 Elf_Shdr *shp; 81 Elf_Off off; 82 void *addr; 83 size_t size; 84 u_int align; 85 int i, first = 1; 86 int n; 87 struct openbsd_bootdata *obd; 88 #ifdef SOFTRAID 89 struct sr_boot_volume *bv; 90 #endif 91 92 /* 93 * Don't display load address for ELF; it's encoded in 94 * each section. 95 */ 96 for (i = 0; i < elf->e_phnum; i++) { 97 Elf_Phdr phdr; 98 size = lseek(fd, (size_t)(elf->e_phoff + sizeof(phdr) * i), 99 SEEK_SET); 100 if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) { 101 printf("read phdr: %s\n", strerror(errno)); 102 return (1); 103 } 104 105 if (phdr.p_type == PT_OPENBSD_BOOTDATA) { 106 memset((void *) (long)phdr.p_paddr, 0, phdr.p_filesz); 107 108 if (phdr.p_filesz < BOOTDATA_LEN_SOFTRAID) 109 continue; 110 111 /* 112 * Kernels up to and including OpenBSD 6.7 113 * check for an exact match if the length. 114 * Lie here to make sure we can still boot 115 * older kernels with softraid. 116 */ 117 obd = (struct openbsd_bootdata *)(long)phdr.p_paddr; 118 obd->version = BOOTDATA_VERSION; 119 obd->len = BOOTDATA_LEN_SOFTRAID; 120 121 #ifdef SOFTRAID 122 /* 123 * If booting from softraid we must pass additional 124 * information to the kernel: 125 * 1) The uuid of the softraid volume we booted from. 126 * 2) The maskkey for decryption, if applicable. 127 */ 128 if (bootdev_dip && bootdev_dip->sr_vol) { 129 bv = bootdev_dip->sr_vol; 130 memcpy(obd->sr_uuid, bv->sbv_uuid.sui_id, 131 sizeof(obd->sr_uuid)); 132 if (bv->sbv_maskkey) 133 memcpy(obd->sr_maskkey, bv->sbv_maskkey, 134 sizeof(obd->sr_maskkey)); 135 } 136 137 #endif 138 139 if (phdr.p_filesz < BOOTDATA_LEN_BOOTHOWTO) 140 continue; 141 142 obd->boothowto = boothowto; 143 continue; 144 } 145 146 if (phdr.p_type == PT_OPENBSD_RANDOMIZE) { 147 extern struct rc4_ctx randomctx; 148 149 rc4_getbytes(&randomctx, (void *)(long)phdr.p_paddr, 150 phdr.p_filesz); 151 } 152 153 if (phdr.p_type != PT_LOAD || 154 (phdr.p_flags & (PF_W|PF_X)) == 0) 155 continue; 156 157 /* Read in segment. */ 158 printf("%s%lu@0x%lx", first ? "" : "+", (u_long)phdr.p_filesz, 159 (u_long)phdr.p_vaddr); 160 (void)lseek(fd, (size_t)phdr.p_offset, SEEK_SET); 161 162 /* 163 * If the segment's VA is aligned on a 4MB boundary, align its 164 * request 4MB aligned physical memory. Otherwise use default 165 * alignment. Make sure BSS is extended to a 4MB boundary, too. 166 */ 167 align = phdr.p_align; 168 if ((phdr.p_vaddr & (4 * MB - 1)) == 0) 169 align = 4 * MB; 170 if (phdr.p_filesz < phdr.p_memsz) 171 phdr.p_memsz = roundup(phdr.p_memsz, 4 * MB); 172 phdr.p_memsz = roundup(phdr.p_memsz, PAGE_SIZE); 173 if (OF_claim((void *)(long)phdr.p_vaddr, phdr.p_memsz, align) == 174 (void *)-1) 175 panic("cannot claim memory"); 176 if (read(fd, (void *)(long)phdr.p_vaddr, phdr.p_filesz) != 177 phdr.p_filesz) { 178 printf("read segment: %s\n", strerror(errno)); 179 return (1); 180 } 181 syncicache((void *)(long)phdr.p_vaddr, phdr.p_filesz); 182 183 /* Zero BSS. */ 184 if (phdr.p_filesz < phdr.p_memsz) { 185 printf("+%lu@0x%lx", 186 (u_long)phdr.p_memsz - phdr.p_filesz, 187 (u_long)(phdr.p_vaddr + phdr.p_filesz)); 188 bzero((void *)(long)phdr.p_vaddr + phdr.p_filesz, 189 (size_t)phdr.p_memsz - phdr.p_filesz); 190 } 191 first = 0; 192 } 193 194 printf(" \n"); 195 196 /* 197 * Compute the size of the symbol table. 198 */ 199 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf_Shdr)); 200 shp = addr = alloc(elf->e_shnum * sizeof(Elf_Shdr)); 201 (void)lseek(fd, (off_t)elf->e_shoff, SEEK_SET); 202 if (read(fd, addr, (size_t)(elf->e_shnum * sizeof(Elf_Shdr))) != 203 elf->e_shnum * sizeof(Elf_Shdr)) { 204 printf("read section headers: %s\n", strerror(errno)); 205 return (1); 206 } 207 208 size_t shstrsz = shp[elf->e_shstrndx].sh_size; 209 char *shstr = alloc(shstrsz); 210 if (lseek(fd, (off_t)shp[elf->e_shstrndx].sh_offset, SEEK_SET) == -1) { 211 printf("lseek section header string table: %s\n", strerror(errno)); 212 return 1; 213 } 214 if (read(fd, shstr, shstrsz) != shstrsz) { 215 printf("read section header string table: %s\n", strerror(errno)); 216 return 1; 217 } 218 219 for (i = 0; i < elf->e_shnum; i++, shp++) { 220 if (shp->sh_type == SHT_NULL) 221 continue; 222 if (shp->sh_type != SHT_SYMTAB 223 && shp->sh_type != SHT_STRTAB 224 && strcmp(shstr + shp->sh_name, ELF_CTF)) { 225 shp->sh_offset = 0; 226 continue; 227 } 228 size += shp->sh_size; 229 } 230 shp = addr; 231 232 /* 233 * Reserve memory for the symbols. 234 */ 235 if ((addr = OF_claim(0, roundup(size, PAGE_SIZE), PAGE_SIZE)) == (void *)-1) 236 panic("no space for symbol table"); 237 238 /* 239 * Copy the headers. 240 */ 241 elf->e_phoff = 0; 242 elf->e_shoff = sizeof(Elf_Ehdr); 243 elf->e_phentsize = 0; 244 elf->e_phnum = 0; 245 bcopy(elf, addr, sizeof(Elf_Ehdr)); 246 bcopy(shp, addr + sizeof(Elf_Ehdr), elf->e_shnum * sizeof(Elf_Shdr)); 247 free(shp, elf->e_shnum * sizeof(Elf_Shdr)); 248 *ssymp = addr; 249 250 /* 251 * Now load the symbol sections themselves. 252 */ 253 shp = addr + sizeof(Elf_Ehdr); 254 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf_Shdr)); 255 size = ELF_ALIGN(size); 256 addr += size; 257 off = size; 258 for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) { 259 if (shp->sh_type == SHT_SYMTAB 260 || shp->sh_type == SHT_STRTAB 261 || !strcmp(shstr + shp->sh_name, ELF_CTF)) { 262 if (first) 263 printf("symbols @ 0x%lx ", (u_long)addr); 264 printf("%s%d", first ? "" : "+", (int)shp->sh_size); 265 (void)lseek(fd, shp->sh_offset, SEEK_SET); 266 if (read(fd, addr, shp->sh_size) != shp->sh_size) { 267 printf("read symbols: %s\n", strerror(errno)); 268 return (1); 269 } 270 addr += ELF_ALIGN(shp->sh_size); 271 shp->sh_offset = off; 272 shp->sh_flags |= SHF_ALLOC; 273 off += ELF_ALIGN(shp->sh_size); 274 first = 0; 275 } 276 } 277 *esymp = addr; 278 279 *entryp = elf->e_entry; 280 return (0); 281 } 282 283 #undef ELF_ALIGN 284