boot.c revision 1.5.2.2 1 /* $NetBSD: boot.c,v 1.5.2.2 2001/02/11 19:11:26 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 int n, *addr;
257
258 /* Display the load address (entry point) for a.out. */
259 printf("Booting %s @ 0x%lx\n", opened_name, hdr->a_entry);
260 addr = (void *)(hdr->a_entry);
261
262 /*
263 * Determine memory needed for kernel and allocate it from
264 * the firmware.
265 */
266 n = hdr->a_text + hdr->a_data + hdr->a_bss + hdr->a_syms + sizeof(int);
267
268 /* Load text. */
269 lseek(fd, N_TXTOFF(*hdr), SEEK_SET);
270 printf("%lu", hdr->a_text);
271 if (read(fd, addr, hdr->a_text) != hdr->a_text) {
272 printf("read text: %s\n", strerror(errno));
273 return (1);
274 }
275 __syncicache((void *)addr, hdr->a_text);
276
277 /* Load data. */
278 printf("+%lu", hdr->a_data);
279 if (read(fd, (char *)addr + hdr->a_text, hdr->a_data) != hdr->a_data) {
280 printf("read data: %s\n", strerror(errno));
281 return (1);
282 }
283
284 /* Zero BSS. */
285 printf("+%lu", hdr->a_bss);
286 bzero((char *)addr + hdr->a_text + hdr->a_data, hdr->a_bss);
287
288 /* Symbols. */
289 *esymp = addr;
290 addr = (int *)((char *)addr + hdr->a_text + hdr->a_data + hdr->a_bss);
291 *addr++ = hdr->a_syms;
292 if (hdr->a_syms) {
293 printf(" [%lu", hdr->a_syms);
294 if (read(fd, addr, hdr->a_syms) != hdr->a_syms) {
295 printf("read symbols: %s\n", strerror(errno));
296 return (1);
297 }
298 addr = (int *)((char *)addr + hdr->a_syms);
299 if (read(fd, &n, sizeof(int)) != sizeof(int)) {
300 printf("read symbols: %s\n", strerror(errno));
301 return (1);
302 }
303 *addr++ = n;
304 if (read(fd, addr, n - sizeof(int)) != n - sizeof(int)) {
305 printf("read symbols: %s\n", strerror(errno));
306 return (1);
307 }
308 printf("+%d]", n - sizeof(int));
309 *esymp = addr + (n - sizeof(int));
310 }
311
312 *entryp = hdr->a_entry;
313 return (0);
314 }
315 #endif /* POWERPC_BOOT_AOUT */
316
317 #ifdef POWERPC_BOOT_ELF
318 int
319 elf_exec(fd, elf, entryp, esymp)
320 int fd;
321 Elf_Ehdr *elf;
322 u_int32_t *entryp;
323 void **esymp;
324 {
325 Elf32_Shdr *shp;
326 Elf32_Off off;
327 void *addr;
328 size_t size;
329 int i, first = 1;
330 int n;
331
332 /*
333 * Don't display load address for ELF; it's encoded in
334 * each section.
335 */
336 printf("Booting %s\n", opened_name);
337
338 for (i = 0; i < elf->e_phnum; i++) {
339 Elf_Phdr phdr;
340 (void)lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET);
341 if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
342 printf("read phdr: %s\n", strerror(errno));
343 return (1);
344 }
345 if (phdr.p_type != PT_LOAD ||
346 (phdr.p_flags & (PF_W|PF_X)) == 0)
347 continue;
348
349 /* Read in segment. */
350 printf("%s%lu@0x%lx", first ? "" : "+", phdr.p_filesz,
351 (u_long)phdr.p_vaddr);
352 (void)lseek(fd, phdr.p_offset, SEEK_SET);
353 if (read(fd, (void *)phdr.p_vaddr, phdr.p_filesz) !=
354 phdr.p_filesz) {
355 printf("read segment: %s\n", strerror(errno));
356 return (1);
357 }
358 __syncicache((void *)phdr.p_vaddr, phdr.p_filesz);
359
360 /* Zero BSS. */
361 if (phdr.p_filesz < phdr.p_memsz) {
362 printf("+%lu@0x%lx", phdr.p_memsz - phdr.p_filesz,
363 (u_long)(phdr.p_vaddr + phdr.p_filesz));
364 bzero((void *)phdr.p_vaddr + phdr.p_filesz,
365 phdr.p_memsz - phdr.p_filesz);
366 }
367 first = 0;
368 }
369
370 printf(" \n");
371
372 #if 0 /* I want to rethink this... --thorpej (at) netbsd.org */
373 /*
374 * Compute the size of the symbol table.
375 */
376 size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
377 shp = addr = alloc(elf->e_shnum * sizeof(Elf32_Shdr));
378 (void)lseek(fd, elf->e_shoff, SEEK_SET);
379 if (read(fd, addr, elf->e_shnum * sizeof(Elf32_Shdr)) !=
380 elf->e_shnum * sizeof(Elf32_Shdr)) {
381 printf("read section headers: %s\n", strerror(errno));
382 return (1);
383 }
384 for (i = 0; i < elf->e_shnum; i++, shp++) {
385 if (shp->sh_type == SHT_NULL)
386 continue;
387 if (shp->sh_type != SHT_SYMTAB
388 && shp->sh_type != SHT_STRTAB) {
389 shp->sh_offset = 0;
390 shp->sh_type = SHT_NOBITS;
391 continue;
392 }
393 size += shp->sh_size;
394 }
395 shp = addr;
396
397 panic("no space for symbol table");
398
399 /*
400 * Copy the headers.
401 */
402 elf->e_phoff = 0;
403 elf->e_shoff = sizeof(Elf_Ehdr);
404 elf->e_phentsize = 0;
405 elf->e_phnum = 0;
406 bcopy(elf, addr, sizeof(Elf_Ehdr));
407 bcopy(shp, addr + sizeof(Elf_Ehdr), elf->e_shnum * sizeof(Elf32_Shdr));
408 free(shp, elf->e_shnum * sizeof(Elf32_Shdr));
409 *ssymp = addr;
410
411 /*
412 * Now load the symbol sections themselves.
413 */
414 shp = addr + sizeof(Elf_Ehdr);
415 addr += sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
416 off = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
417 for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
418 if (shp->sh_type == SHT_SYMTAB
419 || shp->sh_type == SHT_STRTAB) {
420 if (first)
421 printf("symbols @ 0x%lx ", (u_long)addr);
422 printf("%s%d", first ? "" : "+", shp->sh_size);
423 (void)lseek(fd, shp->sh_offset, SEEK_SET);
424 if (read(fd, addr, shp->sh_size) != shp->sh_size) {
425 printf("read symbols: %s\n", strerror(errno));
426 return (1);
427 }
428 addr += shp->sh_size;
429 shp->sh_offset = off;
430 off += shp->sh_size;
431 first = 0;
432 }
433 }
434 *esymp = addr;
435 #endif /* 0 */
436
437 *entryp = elf->e_entry;
438 return (0);
439 }
440 #endif /* POWERPC_BOOT_ELF */
441
442 void
443 main()
444 {
445 extern char bootprog_name[], bootprog_rev[],
446 bootprog_maker[], bootprog_date[];
447 int chosen;
448 char bootline[512]; /* Should check size? */
449 char *cp;
450 int fd;
451
452 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
453 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
454
455 /*
456 * Get the boot arguments from Openfirmware
457 */
458 if ((chosen = OF_finddevice("/chosen")) == -1 ||
459 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
460 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
461 printf("Invalid Openfirmware environment\n");
462 OF_exit();
463 }
464
465 prom2boot(bootdev);
466 parseargs(bootline, &boothowto);
467
468 for (;;) {
469 if (boothowto & RB_ASKNAME) {
470 printf("Boot: ");
471 gets(bootline);
472 parseargs(bootline, &boothowto);
473 }
474 if ((fd = open(bootline, 0)) >= 0)
475 break;
476 if (errno)
477 printf("open %s: %s\n", opened_name, strerror(errno));
478 boothowto |= RB_ASKNAME;
479 }
480 #ifdef __notyet__
481 OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
482 cp = bootline;
483 #else
484 strcpy(bootline, opened_name);
485 cp = bootline + strlen(bootline);
486 *cp++ = ' ';
487 #endif
488 *cp = '-';
489 if (boothowto & RB_ASKNAME)
490 *++cp = 'a';
491 if (boothowto & RB_SINGLE)
492 *++cp = 's';
493 if (boothowto & RB_KDB)
494 *++cp = 'd';
495 if (*cp == '-')
496 #ifdef __notyet__
497 *cp = 0;
498 #else
499 *--cp = 0;
500 #endif
501 else
502 *++cp = 0;
503 #ifdef __notyet__
504 OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
505 #endif
506 /* XXX void, for now */
507 (void)loadfile(fd, bootline);
508
509 OF_exit();
510 }
511