1 /* $NetBSD: boot.c,v 1.5 2006/07/22 18:15:06 tsutsui Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jonathan Stone, Michael Hitch and Simon Burge.
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) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)boot.c 8.1 (Berkeley) 6/10/93
66 #include <lib/libsa/stand.h>
67 #include <lib/libsa/loadfile.h>
68 #include <lib/libkern/libkern.h>
70 #include <sys/param.h>
72 #include <sys/exec_elf.h>
73 #include <sys/boot_flag.h>
75 #include <dev/arcbios/arcbios.h>
81 #define DPRINTF if (debug) printf
83 #define DPRINTF while (/*CONSTCOND*/0) printf
87 * We won't go overboard with gzip'd kernel names. After all we can
88 * still boot a gzip'd kernel called "netbsd.arc" - it doesn't need
91 * For arcane reasons, the first byte of the first element of this struct will
92 * contain a zero. We therefore start from one.
95 char *kernelnames
[] = {
107 static int debug
= 0;
108 static char **environment
;
110 /* Storage must be static. */
111 struct btinfo_symtab bi_syms
;
112 struct btinfo_bootpath bi_bpath
;
114 static char bootinfo
[BOOTINFO_SIZE
];
116 extern const struct arcbios_fv
*ARCBIOS
;
118 int main(int, char **);
119 static char *firmware_getenv(char *);
122 * This gets arguments from the ARCS monitor, calls ARCS routines to open
123 * and load the program to boot, then transfers execution to the new program.
125 * argv[0] will be the ARCS path to the bootloader (i.e.,
126 * "scsi(0)disk(2)rdisk(0)partition(1)\boot").
128 * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
132 main(int argc
, char **argv
)
134 const char *kernel
= NULL
;
135 const char *bootpath
= NULL
;
136 char bootfile
[PATH_MAX
];
137 void (*entry
)(int, char *[], u_int
, void *);
138 u_long marks
[MARK_MAX
];
145 printf("%s Bootstrap, Revision %s\n", bootprog_name
, bootprog_rev
);
146 printf("(%s, %s)\n", bootprog_maker
, bootprog_date
);
148 memset(marks
, 0, sizeof marks
);
150 /* initialise bootinfo structure early */
154 for (i
= 0; i
< argc
; i
++)
155 printf("argv[%d] = %s\n", i
, argv
[i
]);
158 /* Parse arguments, if present. */
159 while ((ch
= getopt(argc
, argv
, "v")) != -1) {
167 environment
= &argv
[1];
169 bootpath
= firmware_getenv("OSLoadPartition");
170 if (bootpath
== NULL
)
172 (*ARCBIOS
->GetEnvironmentVariable
)("OSLoadPartition");
174 if (bootpath
== NULL
) {
175 /* XXX need to actually do the fixup */
176 printf("OSLoadPartition is not specified.\n");
179 DPRINTF("bootpath = %s\n", bootpath
);
182 * Grab OSLoadFilename from ARCS.
185 kernel
= firmware_getenv("OSLoadFilename");
187 kernel
= (*ARCBIOS
->GetEnvironmentVariable
)("OSLoadFilename");
189 DPRINTF("kernel = %s\n", kernel
? kernel
: "<null>");
192 * The first arg is assumed to contain the name of the kernel to boot,
193 * if it a) does not start with a hyphen and b) does not contain
197 for (i
= 1; i
< argc
; i
++) {
198 if (((strchr(argv
[i
], '=')) == NULL
) && (argv
[i
][0] != '-')) {
204 if (kernel
!= NULL
) {
206 * if the name contains parenthesis, we assume that it
207 * contains the bootpath and ignore anything passed through
210 if (strchr(kernel
, '('))
211 win
= loadfile(kernel
, marks
, LOAD_KERNEL
);
213 strcpy(bootfile
, bootpath
);
214 strcat(bootfile
, kernel
);
215 win
= loadfile(bootfile
, marks
, LOAD_KERNEL
);
220 while (kernelnames
[i
] != NULL
) {
221 strcpy(bootfile
, bootpath
);
222 strcat(bootfile
, kernelnames
[i
]);
223 kernel
= kernelnames
[i
];
224 win
= loadfile(bootfile
, marks
, LOAD_KERNEL
);
233 printf("Boot failed! Halting...\n");
238 strlcpy(bi_bpath
.bootpath
, kernel
, BTINFO_BOOTPATH_LEN
);
239 bi_add(&bi_bpath
, BTINFO_BOOTPATH
, sizeof(bi_bpath
));
241 bi_syms
.nsym
= marks
[MARK_NSYM
];
242 bi_syms
.ssym
= marks
[MARK_SYM
];
243 bi_syms
.esym
= marks
[MARK_END
];
244 bi_add(&bi_syms
, BTINFO_SYMTAB
, sizeof(bi_syms
));
246 entry
= (void *)marks
[MARK_ENTRY
];
249 printf("Starting at %p\n\n", entry
);
250 printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks
[MARK_NSYM
],
251 marks
[MARK_SYM
], marks
[MARK_END
]);
253 (*entry
)(argc
, argv
, BOOTINFO_MAGIC
, bootinfo
);
255 printf("Kernel returned! Halting...\n");
260 firmware_getenv(char *envname
)
265 len
= strlen(envname
);
267 for (env
= environment
; env
[0]; env
++) {
268 if (strncasecmp(envname
, env
[0], len
) == 0 &&
269 env
[0][len
] == '=') {
270 return &env
[0][len
+ 1];