1 /* $OpenBSD: elf64_exec.c,v 1.18 2023/11/09 14:26:34 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/queue.h>
61 #include <sys/disklabel.h>
62 #include <dev/biovar.h>
63 #include <dev/softraidvar.h>
64
65 #include "disk.h"
66 #include "softraid_sparc64.h"
67 #endif
68
69 #include <lib/libsa/arc4.h>
70
71 #include "openfirm.h"
72
73 extern int boothowto;
74
75 void syncicache(void *, int);
76
77 int
elf64_exec(int fd,Elf_Ehdr * elf,u_int64_t * entryp,void ** ssymp,void ** esymp)78 elf64_exec(int fd, Elf_Ehdr *elf, u_int64_t *entryp, void **ssymp, void **esymp){
79 Elf_Shdr *shp;
80 Elf_Off off;
81 void *addr;
82 size_t size;
83 u_int align;
84 int i, first = 1;
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 < BOOTDATA_LEN_BOOTHOWTO)
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
113 #ifdef SOFTRAID
114 /*
115 * If booting from softraid we must pass additional
116 * information to the kernel:
117 * 1) The uuid of the softraid volume we booted from.
118 * 2) The maskkey for decryption, if applicable.
119 */
120 if (bootdev_dip && bootdev_dip->sr_vol) {
121 bv = bootdev_dip->sr_vol;
122 memcpy(obd->sr_uuid, bv->sbv_uuid.sui_id,
123 sizeof(obd->sr_uuid));
124 if (bv->sbv_maskkey)
125 memcpy(obd->sr_maskkey, bv->sbv_maskkey,
126 sizeof(obd->sr_maskkey));
127 }
128
129 #endif
130
131 if (phdr.p_filesz < sizeof(struct openbsd_bootdata))
132 continue;
133
134 obd->boothowto = boothowto;
135 continue;
136 }
137
138 if (phdr.p_type == PT_OPENBSD_RANDOMIZE) {
139 extern struct rc4_ctx randomctx;
140
141 rc4_getbytes(&randomctx, (void *)(long)phdr.p_paddr,
142 phdr.p_filesz);
143 }
144
145 if (phdr.p_type != PT_LOAD ||
146 (phdr.p_flags & (PF_W|PF_X)) == 0)
147 continue;
148
149 /* Read in segment. */
150 printf("%s%lu@0x%lx", first ? "" : "+", (u_long)phdr.p_filesz,
151 (u_long)phdr.p_vaddr);
152 (void)lseek(fd, (size_t)phdr.p_offset, SEEK_SET);
153
154 /*
155 * If the segment's VA is aligned on a 4MB boundary, align its
156 * request 4MB aligned physical memory. Otherwise use default
157 * alignment. Make sure BSS is extended to a 4MB boundary, too.
158 */
159 align = phdr.p_align;
160 if ((phdr.p_vaddr & (4 * MB - 1)) == 0)
161 align = 4 * MB;
162 if (phdr.p_filesz < phdr.p_memsz)
163 phdr.p_memsz = roundup(phdr.p_memsz, 4 * MB);
164 phdr.p_memsz = roundup(phdr.p_memsz, PAGE_SIZE);
165 if (OF_claim((void *)(long)phdr.p_vaddr, phdr.p_memsz, align) ==
166 (void *)-1)
167 panic("cannot claim memory");
168 if (read(fd, (void *)(long)phdr.p_vaddr, phdr.p_filesz) !=
169 phdr.p_filesz) {
170 printf("read segment: %s\n", strerror(errno));
171 return (1);
172 }
173 syncicache((void *)(long)phdr.p_vaddr, phdr.p_filesz);
174
175 /* Zero BSS. */
176 if (phdr.p_filesz < phdr.p_memsz) {
177 printf("+%lu@0x%lx",
178 (u_long)phdr.p_memsz - phdr.p_filesz,
179 (u_long)(phdr.p_vaddr + phdr.p_filesz));
180 bzero((void *)(long)phdr.p_vaddr + phdr.p_filesz,
181 (size_t)phdr.p_memsz - phdr.p_filesz);
182 }
183 first = 0;
184 }
185
186 printf(" \n");
187
188 /*
189 * Compute the size of the symbol table.
190 */
191 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf_Shdr));
192 shp = addr = alloc(elf->e_shnum * sizeof(Elf_Shdr));
193 (void)lseek(fd, (off_t)elf->e_shoff, SEEK_SET);
194 if (read(fd, addr, (size_t)(elf->e_shnum * sizeof(Elf_Shdr))) !=
195 elf->e_shnum * sizeof(Elf_Shdr)) {
196 printf("read section headers: %s\n", strerror(errno));
197 return (1);
198 }
199
200 size_t shstrsz = shp[elf->e_shstrndx].sh_size;
201 char *shstr = alloc(shstrsz);
202 if (lseek(fd, (off_t)shp[elf->e_shstrndx].sh_offset, SEEK_SET) == -1) {
203 printf("lseek section header string table: %s\n", strerror(errno));
204 return 1;
205 }
206 if (read(fd, shstr, shstrsz) != shstrsz) {
207 printf("read section header string table: %s\n", strerror(errno));
208 return 1;
209 }
210
211 for (i = 0; i < elf->e_shnum; i++, shp++) {
212 if (shp->sh_type == SHT_NULL)
213 continue;
214 if (shp->sh_type != SHT_SYMTAB
215 && shp->sh_type != SHT_STRTAB
216 && strcmp(shstr + shp->sh_name, ELF_CTF)) {
217 shp->sh_offset = 0;
218 continue;
219 }
220 size += shp->sh_size;
221 }
222 shp = addr;
223
224 /*
225 * Reserve memory for the symbols.
226 */
227 if ((addr = OF_claim(0, roundup(size, PAGE_SIZE), PAGE_SIZE)) == (void *)-1)
228 panic("no space for symbol table");
229
230 /*
231 * Copy the headers.
232 */
233 elf->e_phoff = 0;
234 elf->e_shoff = sizeof(Elf_Ehdr);
235 elf->e_phentsize = 0;
236 elf->e_phnum = 0;
237 bcopy(elf, addr, sizeof(Elf_Ehdr));
238 bcopy(shp, addr + sizeof(Elf_Ehdr), elf->e_shnum * sizeof(Elf_Shdr));
239 free(shp, elf->e_shnum * sizeof(Elf_Shdr));
240 *ssymp = addr;
241
242 /*
243 * Now load the symbol sections themselves.
244 */
245 shp = addr + sizeof(Elf_Ehdr);
246 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf_Shdr));
247 size = ELF_ALIGN(size);
248 addr += size;
249 off = size;
250 for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
251 if (shp->sh_type == SHT_SYMTAB
252 || shp->sh_type == SHT_STRTAB
253 || !strcmp(shstr + shp->sh_name, ELF_CTF)) {
254 if (first)
255 printf("symbols @ 0x%lx ", (u_long)addr);
256 printf("%s%d", first ? "" : "+", (int)shp->sh_size);
257 (void)lseek(fd, shp->sh_offset, SEEK_SET);
258 if (read(fd, addr, shp->sh_size) != shp->sh_size) {
259 printf("read symbols: %s\n", strerror(errno));
260 return (1);
261 }
262 addr += ELF_ALIGN(shp->sh_size);
263 shp->sh_offset = off;
264 shp->sh_flags |= SHF_ALLOC;
265 off += ELF_ALIGN(shp->sh_size);
266 first = 0;
267 }
268 }
269 *esymp = addr;
270
271 *entryp = elf->e_entry;
272 return (0);
273 }
274
275 #undef ELF_ALIGN
276