1 /*        $NetBSD: boot.c,v 1.1 2011/03/03 05:59:37 kiyohara Exp $    */
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jonathan Stone, Michael Hitch, Simon Burge and Wayne Knowles.
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  *
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.
30  */
31 
32 /*
33  * Copyright (c) 1992, 1993
34  *        The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * Ralph Campbell.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
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.
50  *
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
61  * SUCH DAMAGE.
62  *
63  *        @(#)boot.c          8.1 (Berkeley) 6/10/93
64  */
65 
66 #include <lib/libsa/stand.h>
67 #include <lib/libsa/loadfile.h>
68 #include <lib/libkern/libkern.h>
69 
70 #include <sys/param.h>
71 #include <sys/boot_flag.h>
72 #include <sys/exec.h>
73 #include <sys/exec_elf.h>
74 
75 #include <machine/cpu.h>
76 
77 #include "boot.h"
78 #include "cons.h"
79 #include "common.h"
80 #include "bootinfo.h"
81 
82 char *kernelnames[] = {
83           "netbsd",
84           "netbsd.gz",
85           "onetbsd",
86           "onetbsd.gz",
87 
88           NULL
89 };
90 
91 extern u_long end;            /* Boot loader code end address */
92 void start(void);
93 
94 static int get_bsdbootname(char **, char **, int *);
95 static int parse_bootname(char *, int, char **, char **);
96 static void print_banner(void);
97 
98 int main(void);
99 
100 /*
101  * Extract NetBSD boot specification
102  */
103 static int
get_bsdbootname(char ** dev,char ** kern,int * howtop)104 get_bsdbootname(char **dev, char **kern, int *howtop)
105 {
106           int len;
107           char *ptr, *spec;
108           char c, namebuf[PATH_MAX];
109 
110           spec = NULL;
111           len = 0;
112 
113           memset(namebuf, 0, sizeof namebuf);
114           printf("Boot [" DEFBOOTDEV ":%s]: ", DEFKERNELNAME);
115 
116           if (tgets(namebuf) == -1)
117                     printf("\n");
118 
119           ptr = namebuf;
120           while ((c = *ptr) != '\0') {
121                     while (c == ' ')
122                               c = *++ptr;
123                     if (c == '\0')
124                               break;
125                     if (c == '-')
126                               while ((c = *++ptr) && c != ' ')
127                                         BOOT_FLAG(c, *howtop);
128                     else {
129                               spec = ptr;
130                               while ((c = *++ptr) && c != ' ')
131                                         ;
132                               if (c)
133                                         *ptr++ = '\0';
134                               len = strlen(spec);
135                     }
136           }
137 
138           if (len > 0)
139                     if (parse_bootname(spec, len, dev, kern))
140                               return 1;
141 
142           DPRINTF(("dev = %s, kern = %s\n",
143               *dev ? *dev : "<NULL>",
144               *kern ? *kern : "<NULL>"));
145 
146           return 0;
147 }
148 
149 static int
parse_bootname(char * spec,int len,char ** dev,char ** kern)150 parse_bootname(char *spec, int len, char **dev, char **kern)
151 {
152           char *bootname, *ptr;
153 
154           bootname = alloc(len + 1);
155           if (bootname == NULL)
156                     return 1;
157           memcpy(bootname, spec, len);
158           bootname[len] = '\0';
159 
160           if ((ptr = memchr(bootname, ':', len)) != NULL) {
161                     /* "wdXX:kernel" */
162                     *ptr = '\0';
163                     *dev = bootname;
164                     if (*++ptr)
165                               *kern = ptr;
166           } else
167                     /* "kernel" */
168                     *kern = bootname;
169           return 0;
170 }
171 
172 /*
173  * Print boot message.
174  */
175 static void
print_banner(void)176 print_banner(void)
177 {
178 
179           printf("\n");
180           printf(">> %s " NETBSD_VERS " Bootloader, Revision %s [@%p]\n",
181                               bootprog_name, bootprog_rev, (void*)&start);
182 }
183 
184 int
main(void)185 main(void)
186 {
187           char **namep, *dev, *kernel, *bi_addr;
188           char bootpath[PATH_MAX];
189           int win;
190           u_long marks[MARK_MAX];
191           void (*entry)(u_int, char *);
192 
193           struct btinfo_bootdev bi_bdev;
194           struct btinfo_bootpath bi_bpath;
195           struct btinfo_howto bi_howto;
196           int howto;
197 
198           /* Initialize boot info early */
199           dev = NULL;
200           kernel = NULL;
201           howto = 0x0;
202           bi_addr = bi_init();
203 
204 #if defined(SH4)
205           tmu_init();
206 #endif
207           cninit();
208 
209           print_banner();
210 
211           memset(marks, 0, sizeof(marks));
212           get_bsdbootname(&dev, &kernel, &howto);
213 
214           if (kernel != NULL) {
215                     DPRINTF(("kernel: %s\n", kernel));
216                     kernelnames[0] = kernel;
217                     kernelnames[1] = NULL;
218           } else {
219                     DPRINTF(("kernel: NULL\n"));
220           }
221 
222           win = 0;
223           DPRINTF(("Kernel names: %p\n", kernelnames));
224           for (namep = kernelnames, win = 0; (*namep != NULL) && !win; namep++) {
225                     kernel = *namep;
226 
227                     bootpath[0] = '\0';
228 
229                     strcpy(bootpath, dev ? dev : DEFBOOTDEV);
230                     strcat(bootpath, ":");
231                     strcat(bootpath, kernel);
232 
233                     printf("Loading: %s", bootpath);
234                     if (howto)
235                               printf(" (howto 0x%x)", howto);
236                     printf("\n");
237                     win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
238           }
239 
240           if (win) {
241                     if (dev != NULL)
242                               strncpy(bi_bdev.bootdev, dev, BTINFO_BOOTDEV_LEN);
243                     else
244                               strncpy(bi_bdev.bootdev, DEFBOOTDEV,
245                                   BTINFO_BOOTDEV_LEN);
246                     bi_add(&bi_bdev, BTINFO_BOOTDEV, sizeof(bi_bdev));
247 
248                     strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
249                     bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
250 
251                     bi_howto.bi_howto = howto;
252                     bi_add(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto));
253 
254                     entry = (void *)marks[MARK_ENTRY];
255 
256                     DPRINTF(("Bootinfo @ 0x%lx\n", (u_long)bi_addr));
257                     printf("Starting at 0x%lx\n\n", (u_long)entry);
258 
259                     delay(10000);       /* XXXX: Wait output to console. */
260 
261                     (*entry)(BOOTINFO_MAGIC, bi_addr);
262           }
263 
264           (void)printf("Boot failed! Rebooting...\n");
265           delay(20000);
266           return 0;
267 }
268 
269 void
_rtt(void)270 _rtt(void)
271 {
272 
273           /* XXXX */
274 }
275