boot.c revision 1.5.2.1 1 /* $NetBSD: boot.c,v 1.5.2.1 2000/11/20 20:18:44 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
41 * Copyright (C) 1995, 1996 TooLs GmbH.
42 * All rights reserved.
43 *
44 * ELF support derived from NetBSD/alpha's boot loader, written
45 * by Christopher G. Demetriou.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by TooLs GmbH.
58 * 4. The name of TooLs GmbH may not be used to endorse or promote products
59 * derived from this software without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
62 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
63 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
64 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
66 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
67 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
68 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
69 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
70 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 */
72
73 /*
74 * First try for the boot code
75 *
76 * Input syntax is:
77 * [promdev[{:|,}partition]]/[filename] [flags]
78 */
79
80 #define ELFSIZE 32 /* We use 32-bit ELF. */
81
82 #include <sys/param.h>
83 #include <sys/exec.h>
84 #include <sys/exec_elf.h>
85 #include <sys/reboot.h>
86 #include <sys/disklabel.h>
87 #include <sys/boot_flag.h>
88
89 #include <lib/libsa/stand.h>
90 #include <lib/libkern/libkern.h>
91
92 #include <machine/cpu.h>
93 #include <machine/machine_type.h>
94
95 #include "ofdev.h"
96 #include "openfirm.h"
97
98 char bootdev[128];
99 char bootfile[128];
100 int boothowto;
101 int debug;
102
103 #ifdef POWERPC_BOOT_ELF
104 int elf_exec __P((int, Elf_Ehdr *, u_int32_t *, void **));
105 #endif
106
107 #ifdef POWERPC_BOOT_AOUT
108 int aout_exec __P((int, struct exec *, u_int32_t *, void **));
109 #endif
110
111 static void
112 prom2boot(dev)
113 char *dev;
114 {
115 char *cp;
116
117 for (cp = dev; *cp != '\0'; cp++)
118 if (*cp == ':') {
119 *cp = '\0';
120 return;
121 }
122 }
123
124 static void
125 parseargs(str, howtop)
126 char *str;
127 int *howtop;
128 {
129 char *cp;
130
131 /* Allow user to drop back to the PROM. */
132 if (strcmp(str, "exit") == 0)
133 OF_exit();
134
135 *howtop = 0;
136 for (cp = str; *cp; cp++)
137 if (*cp == ' ' || *cp == '-')
138 break;
139 if (!*cp)
140 return;
141
142 *cp++ = '\0';
143 while (*cp)
144 BOOT_FLAG(*cp++, *howtop);
145 }
146
147 static void
148 chain(entry, args, esym)
149 void (*entry)();
150 char *args;
151 void *esym;
152 {
153 extern char end[];
154 int l, machine_tag;
155
156 freeall();
157
158 /*
159 * Stash pointer to end of symbol table after the argument
160 * strings.
161 */
162 l = strlen(args) + 1;
163 bcopy(&esym, args + l, sizeof(esym));
164 l += sizeof(esym);
165
166 /*
167 * Tell the kernel we're an OpenFirmware system.
168 */
169 machine_tag = POWERPC_MACHINE_OPENFIRMWARE;
170 bcopy(&machine_tag, args + l, sizeof(machine_tag));
171 l += sizeof(machine_tag);
172
173 OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
174 panic("chain");
175 }
176
177 int
178 loadfile(fd, args)
179 int fd;
180 char *args;
181 {
182 union {
183 #ifdef POWERPC_BOOT_AOUT
184 struct exec aout;
185 #endif
186 #ifdef POWERPC_BOOT_ELF
187 Elf_Ehdr elf;
188 #endif
189 } hdr;
190 int rval;
191 u_int32_t entry;
192 void *esym;
193
194 rval = 1;
195 esym = NULL;
196
197 /* Load the header. */
198 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
199 printf("read header: %s\n", strerror(errno));
200 goto err;
201 }
202
203 /* Determine file type, load kernel. */
204 #ifdef POWERPC_BOOT_AOUT
205 if (N_BADMAG(hdr.aout) == 0 && N_GETMID(hdr.aout) == MID_POWERPC) {
206 rval = aout_exec(fd, &hdr.aout, &entry, &esym);
207 } else
208 #endif
209 #ifdef POWERPC_BOOT_ELF
210 if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0 &&
211 hdr.elf.e_ident[EI_CLASS] == ELFCLASS) {
212 rval = elf_exec(fd, &hdr.elf, &entry, &esym);
213 } else
214 #endif
215 {
216 printf("unknown executable format\n");
217 }
218
219 if (rval)
220 goto err;
221
222 printf(" start=0x%x\n", entry);
223
224 close(fd);
225
226 /* XXX this should be replaced w/ a mountroothook. */
227 if (floppyboot) {
228 printf("Please insert root disk and press ENTER ");
229 getchar();
230 printf("\n");
231 }
232
233 chain((void *)entry, args, esym);
234 /* NOTREACHED */
235
236 err:
237 close(fd);
238 return (rval);
239 }
240
241 __dead void
242 _rtt()
243 {
244
245 OF_exit();
246 }
247
248 #ifdef POWERPC_BOOT_AOUT
249 int
250 aout_exec(fd, hdr, entryp, esymp)
251 int fd;
252 struct exec *hdr;
253 u_int32_t *entryp;
254 void **esymp;
255 {
256 void *addr;
257 int n, *paddr;
258
259 /* Display the load address (entry point) for a.out. */
260 printf("Booting %s @ 0x%lx\n", opened_name, hdr->a_entry);
261 addr = (void *)(hdr->a_entry);
262
263 /*
264 * Determine memory needed for kernel and allocate it from
265 * the firmware.
266 */
267 n = hdr->a_text + hdr->a_data + hdr->a_bss + hdr->a_syms + sizeof(int);
268 if ((paddr = OF_claim(addr, n, 0)) == (int *)-1)
269 panic("cannot claim memory");
270
271 /* Load text. */
272 lseek(fd, N_TXTOFF(*hdr), SEEK_SET);
273 printf("%lu", hdr->a_text);
274 if (read(fd, paddr, hdr->a_text) != hdr->a_text) {
275 printf("read text: %s\n", strerror(errno));
276 return (1);
277 }
278 __syncicache((void *)paddr, hdr->a_text);
279
280 /* Load data. */
281 printf("+%lu", hdr->a_data);
282 if (read(fd, (void *)paddr + hdr->a_text, hdr->a_data) != hdr->a_data) {
283 printf("read data: %s\n", strerror(errno));
284 return (1);
285 }
286
287 /* Zero BSS. */
288 printf("+%lu", hdr->a_bss);
289 bzero((void *)paddr + hdr->a_text + hdr->a_data, hdr->a_bss);
290
291 /* Symbols. */
292 *esymp = paddr;
293 paddr = (int *)((void *)paddr + hdr->a_text + hdr->a_data + hdr->a_bss);
294 *paddr++ = hdr->a_syms;
295 if (hdr->a_syms) {
296 printf(" [%lu", hdr->a_syms);
297 if (read(fd, paddr, hdr->a_syms) != hdr->a_syms) {
298 printf("read symbols: %s\n", strerror(errno));
299 return (1);
300 }
301 paddr = (int *)((void *)paddr + hdr->a_syms);
302 if (read(fd, &n, sizeof(int)) != sizeof(int)) {
303 printf("read symbols: %s\n", strerror(errno));
304 return (1);
305 }
306 if (OF_claim((void *)paddr, n + sizeof(int), 0) == (void *)-1)
307 panic("cannot claim memory");
308 *paddr++ = n;
309 if (read(fd, paddr, n - sizeof(int)) != n - sizeof(int)) {
310 printf("read symbols: %s\n", strerror(errno));
311 return (1);
312 }
313 printf("+%d]", n - sizeof(int));
314 *esymp = paddr + (n - sizeof(int));
315 }
316
317 *entryp = hdr->a_entry;
318 return (0);
319 }
320 #endif /* POWERPC_BOOT_AOUT */
321
322 #ifdef POWERPC_BOOT_ELF
323 int
324 elf_exec(fd, elf, entryp, esymp)
325 int fd;
326 Elf_Ehdr *elf;
327 u_int32_t *entryp;
328 void **esymp;
329 {
330 Elf32_Shdr *shp;
331 Elf32_Off off;
332 void *addr;
333 size_t size;
334 int i, first = 1;
335 int n;
336
337 /*
338 * Don't display load address for ELF; it's encoded in
339 * each section.
340 */
341 printf("Booting %s\n", opened_name);
342
343 for (i = 0; i < elf->e_phnum; i++) {
344 Elf_Phdr phdr;
345 (void)lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET);
346 if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
347 printf("read phdr: %s\n", strerror(errno));
348 return (1);
349 }
350 if (phdr.p_type != PT_LOAD ||
351 (phdr.p_flags & (PF_W|PF_X)) == 0)
352 continue;
353
354 /* Read in segment. */
355 printf("%s%lu@0x%lx", first ? "" : "+", phdr.p_filesz,
356 (u_long)phdr.p_vaddr);
357 (void)lseek(fd, phdr.p_offset, SEEK_SET);
358 if (OF_claim((void *)phdr.p_vaddr, phdr.p_memsz, 0) ==
359 (void *)-1)
360 panic("cannot claim memory");
361 if (read(fd, (void *)phdr.p_vaddr, phdr.p_filesz) !=
362 phdr.p_filesz) {
363 printf("read segment: %s\n", strerror(errno));
364 return (1);
365 }
366 __syncicache((void *)phdr.p_vaddr, phdr.p_filesz);
367
368 /* Zero BSS. */
369 if (phdr.p_filesz < phdr.p_memsz) {
370 printf("+%lu@0x%lx", phdr.p_memsz - phdr.p_filesz,
371 (u_long)(phdr.p_vaddr + phdr.p_filesz));
372 bzero((void *)phdr.p_vaddr + phdr.p_filesz,
373 phdr.p_memsz - phdr.p_filesz);
374 }
375 first = 0;
376 }
377
378 printf(" \n");
379
380 #if 0 /* I want to rethink this... --thorpej (at) netbsd.org */
381 /*
382 * Compute the size of the symbol table.
383 */
384 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
385 shp = addr = alloc(elf->e_shnum * sizeof(Elf32_Shdr));
386 (void)lseek(fd, elf->e_shoff, SEEK_SET);
387 if (read(fd, addr, elf->e_shnum * sizeof(Elf32_Shdr)) !=
388 elf->e_shnum * sizeof(Elf32_Shdr)) {
389 printf("read section headers: %s\n", strerror(errno));
390 return (1);
391 }
392 for (i = 0; i < elf->e_shnum; i++, shp++) {
393 if (shp->sh_type == SHT_NULL)
394 continue;
395 if (shp->sh_type != SHT_SYMTAB
396 && shp->sh_type != SHT_STRTAB) {
397 shp->sh_offset = 0;
398 shp->sh_type = SHT_NOBITS;
399 continue;
400 }
401 size += shp->sh_size;
402 }
403 shp = addr;
404
405 /*
406 * Reserve memory for the symbols.
407 */
408 if ((addr = OF_claim(0, size, NBPG)) == (void *)-1)
409 panic("no space for symbol table");
410
411 /*
412 * Copy the headers.
413 */
414 elf->e_phoff = 0;
415 elf->e_shoff = sizeof(Elf_Ehdr);
416 elf->e_phentsize = 0;
417 elf->e_phnum = 0;
418 bcopy(elf, addr, sizeof(Elf_Ehdr));
419 bcopy(shp, addr + sizeof(Elf_Ehdr), elf->e_shnum * sizeof(Elf32_Shdr));
420 free(shp, elf->e_shnum * sizeof(Elf32_Shdr));
421 *ssymp = addr;
422
423 /*
424 * Now load the symbol sections themselves.
425 */
426 shp = addr + sizeof(Elf_Ehdr);
427 addr += sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
428 off = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
429 for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
430 if (shp->sh_type == SHT_SYMTAB
431 || shp->sh_type == SHT_STRTAB) {
432 if (first)
433 printf("symbols @ 0x%lx ", (u_long)addr);
434 printf("%s%d", first ? "" : "+", shp->sh_size);
435 (void)lseek(fd, shp->sh_offset, SEEK_SET);
436 if (read(fd, addr, shp->sh_size) != shp->sh_size) {
437 printf("read symbols: %s\n", strerror(errno));
438 return (1);
439 }
440 addr += shp->sh_size;
441 shp->sh_offset = off;
442 off += shp->sh_size;
443 first = 0;
444 }
445 }
446 *esymp = addr;
447 #endif /* 0 */
448
449 *entryp = elf->e_entry;
450 return (0);
451 }
452 #endif /* POWERPC_BOOT_ELF */
453
454 void
455 main()
456 {
457 extern char bootprog_name[], bootprog_rev[],
458 bootprog_maker[], bootprog_date[];
459 int chosen;
460 char bootline[512]; /* Should check size? */
461 char *cp;
462 int fd;
463
464 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
465 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
466
467 /*
468 * Get the boot arguments from Openfirmware
469 */
470 if ((chosen = OF_finddevice("/chosen")) == -1 ||
471 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
472 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
473 printf("Invalid Openfirmware environment\n");
474 OF_exit();
475 }
476
477 prom2boot(bootdev);
478 parseargs(bootline, &boothowto);
479
480 for (;;) {
481 if (boothowto & RB_ASKNAME) {
482 printf("Boot: ");
483 gets(bootline);
484 parseargs(bootline, &boothowto);
485 }
486 if ((fd = open(bootline, 0)) >= 0)
487 break;
488 if (errno)
489 printf("open %s: %s\n", opened_name, strerror(errno));
490 boothowto |= RB_ASKNAME;
491 }
492 #ifdef __notyet__
493 OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
494 cp = bootline;
495 #else
496 strcpy(bootline, opened_name);
497 cp = bootline + strlen(bootline);
498 *cp++ = ' ';
499 #endif
500 *cp = '-';
501 if (boothowto & RB_ASKNAME)
502 *++cp = 'a';
503 if (boothowto & RB_SINGLE)
504 *++cp = 's';
505 if (boothowto & RB_KDB)
506 *++cp = 'd';
507 if (*cp == '-')
508 #ifdef __notyet__
509 *cp = 0;
510 #else
511 *--cp = 0;
512 #endif
513 else
514 *++cp = 0;
515 #ifdef __notyet__
516 OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
517 #endif
518 /* XXX void, for now */
519 (void)loadfile(fd, bootline);
520
521 OF_exit();
522 }
523