1 /* $NetBSD: boot.c,v 1.22 2008/04/28 20:23:27 martin Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
34 * Copyright (C) 1995, 1996 TooLs GmbH.
35 * All rights reserved.
37 * ELF support derived from NetBSD/alpha's boot loader, written
38 * by Christopher G. Demetriou.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by TooLs GmbH.
51 * 4. The name of TooLs GmbH may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
60 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
61 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
62 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
63 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 * First try for the boot code
70 * [promdev[{:|,}partition]]/[filename] [flags]
75 #include <sys/param.h>
76 #include <sys/boot_flag.h>
77 #include <sys/disklabel.h>
79 #include <lib/libsa/stand.h>
80 #include <lib/libsa/loadfile.h>
81 #include <lib/libkern/libkern.h>
86 extern void __syncicache(void *, size_t); /* in libkern */
90 # define DPRINTF printf
92 # define DPRINTF while (0) printf
95 char bootdev
[MAXBOOTPATHLEN
];
96 char bootfile
[MAXBOOTPATHLEN
];
100 static int ofw_version
= 0;
101 static const char *kernels
[] = { "/netbsd", "/netbsd.gz", "/netbsd.macppc", NULL
};
108 cp
= dev
+ strlen(dev
) - 1;
111 if (ofw_version
< 3) {
116 /* disk@0:5,boot -> disk@0:0 */
125 parseargs(char *str
, int *howtop
)
129 /* Allow user to drop back to the PROM. */
130 if (strcmp(str
, "exit") == 0)
138 for (cp
= str
; *cp
; cp
++)
146 BOOT_FLAG(*cp
++, *howtop
);
150 is_floppyboot(const char *path
, const char *defaultdev
)
152 char dev
[MAXBOOTPATHLEN
];
156 if (parsefilepath(path
, dev
, NULL
, NULL
)) {
157 if (dev
[0] == '\0' && defaultdev
!= NULL
)
158 strlcpy(dev
, defaultdev
, sizeof(dev
));
160 /* check properties */
161 handle
= OF_finddevice(dev
);
163 rv
= OF_getprop(handle
, "name", nam
, sizeof(nam
));
165 (strcmp(nam
, "swim3") == 0 ||
166 strcmp(nam
, "floppy") == 0))
170 /* also check devalias */
171 if (strcmp(dev
, "fd") == 0)
179 chain(boot_entry_t entry
, char *args
, void *ssym
, void *esym
)
187 * Stash pointer to end of symbol table after the argument
190 l
= strlen(args
) + 1;
191 memcpy(args
+ l
, &ssym
, sizeof(ssym
));
193 memcpy(args
+ l
, &esym
, sizeof(esym
));
195 l
+= sizeof(int); /* XXX */
197 OF_chain((void *) RELOC
, end
- (char *) RELOC
, entry
, args
, l
);
211 extern char bootprog_name
[], bootprog_rev
[],
212 bootprog_maker
[], bootprog_date
[];
213 int chosen
, options
, openprom
;
214 char bootline
[512]; /* Should check size? */
216 u_long marks
[MARK_MAX
];
221 printf(">> %s, Revision %s\n", bootprog_name
, bootprog_rev
);
222 printf(">> (%s, %s)\n", bootprog_maker
, bootprog_date
);
225 * Figure out what version of Open Firmware...
227 if ((openprom
= OF_finddevice("/openprom")) != -1) {
230 memset(model
, 0, sizeof model
);
231 OF_getprop(openprom
, "model", model
, sizeof model
);
232 for (cp
= model
; *cp
; cp
++)
233 if (*cp
>= '0' && *cp
<= '9') {
234 ofw_version
= *cp
- '0';
237 DPRINTF(">> Open Firmware version %d.x\n", ofw_version
);
241 * Get the boot arguments from Openfirmware
243 if ((chosen
= OF_finddevice("/chosen")) == -1 ||
244 OF_getprop(chosen
, "bootpath", bootdev
, sizeof bootdev
) < 0 ||
245 OF_getprop(chosen
, "bootargs", bootline
, sizeof bootline
) < 0) {
246 printf("Invalid Openfirmware environment\n");
251 * Some versions of Openfirmware sets bootpath to "".
252 * We use boot-device instead if it occurs.
254 if (bootdev
[0] == 0) {
255 printf("Cannot use bootpath\n");
256 if ((options
= OF_finddevice("/options")) == -1 ||
257 OF_getprop(options
, "boot-device", bootdev
,
258 sizeof bootdev
) < 0) {
259 printf("Invalid Openfirmware environment\n");
262 printf("Using boot-device instead\n");
266 parseargs(bootline
, &boothowto
);
267 DPRINTF("bootline=%s\n", bootline
);
272 if (boothowto
& RB_ASKNAME
) {
275 parseargs(bootline
, &boothowto
);
279 kernels
[0] = bootline
;
283 for (i
= 0; kernels
[i
]; i
++) {
284 floppyboot
= is_floppyboot(kernels
[i
], bootdev
);
286 DPRINTF("Trying %s%s\n", kernels
[i
],
287 floppyboot
? " (floppyboot)" : "");
289 loadflag
= LOAD_KERNEL
;
291 loadflag
&= ~LOAD_NOTE
;
293 marks
[MARK_START
] = 0;
294 if (loadfile(kernels
[i
], marks
, loadflag
) >= 0)
297 boothowto
|= RB_ASKNAME
;
302 OF_setprop(chosen
, "bootpath", opened_name
, strlen(opened_name
) + 1);
305 strcpy(bootline
, opened_name
);
306 cp
= bootline
+ strlen(bootline
);
310 if (boothowto
& RB_ASKNAME
)
312 if (boothowto
& RB_USERCONF
)
314 if (boothowto
& RB_SINGLE
)
316 if (boothowto
& RB_KDB
)
327 OF_setprop(chosen
, "bootargs", bootline
, strlen(bootline
) + 1);
330 entry
= marks
[MARK_ENTRY
];
331 ssym
= (void *)marks
[MARK_SYM
];
332 esym
= (void *)marks
[MARK_END
];
334 printf(" start=0x%x\n", entry
);
335 __syncicache((void *) entry
, (u_int
) ssym
- (u_int
) entry
);
336 chain((boot_entry_t
) entry
, bootline
, ssym
, esym
);
341 #ifdef HAVE_CHANGEDISK_HOOK
343 changedisk_hook(struct open_file
*of
)
345 struct of_dev
*op
= of
->f_devdata
;
348 OF_call_method("eject", op
->handle
, 0, 0);
356 OF_call_method("close", op
->handle
, 0, 0);
357 OF_call_method("open", op
->handle
, 0, 0);