1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/* $NetBSD: boot.c,v 1.4 2015/12/13 18:24:50 christos Exp $ */
/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code was written by Alessandro Forin and Neil Pittman
* at Microsoft Research and contributed to The NetBSD Foundation
* by Microsoft Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <lib/libsa/stand.h>
#include <lib/libsa/loadfile.h>
#include <lib/libkern/libkern.h>
#include <sys/param.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>
#include "common.h"
#include "bootinfo.h"
#include "start.h"
/*
* We won't go overboard with gzip'd kernel names. After all we can
* still boot a gzip'd kernel called "netbsd.emips" - it doesn't need
* the .gz suffix.
*/
char *kernelnames[] = {
"netbsd", "netbsd.gz",
"netbsd.old",
"onetbsd",
"gennetbsd",
"nfsnetbsd",
NULL
};
void main (char *);
char *getboot(char *, char*);
static int devcanon(char *);
#define OPT_MAX PATH_MAX /* way overkill */
static int loadit(char *name, u_long *marks)
{
printf("Loading: %s\n", name);
memset(marks, 0, sizeof(*marks) * MARK_MAX);
return (loadfile(name, marks, LOAD_ALL));
}
/*
* The locore in start.S calls us with an 8KB stack carved after _end.
*
*/
void
main(char *stack_top)
{
int autoboot = 1, win;
char *name, **namep, *dev, *kernel;
char bootpath[PATH_MAX], options[OPT_MAX];
uint32_t entry;
u_long marks[MARK_MAX];
struct btinfo_symtab bi_syms;
struct btinfo_bootpath bi_bpath;
/* Init all peripherals, esp USART for printf and memory */
init_board();
/* On account of compression, we need a fairly large heap.
* To keep things simple, take one meg just below the 16 meg mark.
* That allows for a large kernel, and a 16MB configuration still works.
*/
setheap((void *)(0x81000000-(1024*1024)), (void *)0x81000000);
/* On the BEE3 and the Giano simulator, we need a sec between the serial-line download complete
* and switching the serial line to PuTTY as console. Get a char to pause.
* This delay is also the practice on PCs so.
*/
Delay(200000);
printf("Hit any char to boot..");
(void)GetChar();
/* print a banner */
printf("\n");
printf("NetBSD/emips " NETBSD_VERS " " BOOT_TYPE_NAME " Bootstrap, Revision %s\n",
bootprog_rev);
/* initialise bootinfo structure early */
bi_init(BOOTINFO_ADDR);
/* Default is to auto-boot from the first disk */
dev = "0/ace(0,0)/";
kernel = kernelnames[0];
options[0] = 0;
win = 0;
for (;!win;) {
strcpy(bootpath, dev);
strcat(bootpath, kernel);
name = getboot(bootpath,options);
if (name != NULL) {
win = (loadit(name, marks) == 0);
} else if (autoboot)
break;
autoboot = 0;
}
if (!win) {
for (namep = kernelnames, win = 0; *namep != NULL && !win;
namep++) {
kernel = *namep;
strcpy(bootpath, dev);
strcat(bootpath, kernel);
win = (loadit(bootpath, marks) == 0);
if (win) {
name = bootpath;
}
}
}
if (!win)
goto fail;
strncpy(bi_bpath.bootpath, name/*kernel?*/, BTINFO_BOOTPATH_LEN);
bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
entry = marks[MARK_ENTRY];
bi_syms.nsym = marks[MARK_NSYM];
bi_syms.ssym = marks[MARK_SYM];
bi_syms.esym = marks[MARK_END];
bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
printf("Starting at 0x%x\n\n", entry);
call_kernel(entry, name, options, BOOTINFO_MAGIC, bootinfo);
(void)printf("KERNEL RETURNED!\n");
fail:
(void)printf("Boot failed! Halting...\n");
}
static inline int
parse(char *cmd, char *kname, char *optarg)
{
char *arg = cmd;
char *ep, *p;
int c, i;
while ((c = *arg++)) {
/* skip leading blanks */
if (c == ' ' || c == '\t' || c == '\n')
continue;
/* find separator, or eol */
for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
ep = p;
/* trim if separator */
if (*p)
*p++ = 0;
/* token is either "-opts" or "kernelname" */
if (c == '-') {
/* no overflow because whole line same length as optarg anyways */
while ((c = *arg++)) {
*optarg++ = c;
}
*optarg = 0;
} else {
arg--;
if ((i = ep - arg)) {
if ((size_t)i >= PATH_MAX)
return -1;
memcpy(kname, arg, i + 1);
}
}
arg = p;
}
return 0;
}
/* String returned is zero-terminated and at most PATH_MAX chars */
static inline void
getstr(char *cmd, int c)
{
char *s;
s = cmd;
if (c == 0)
c = GetChar();
for (;;) {
switch (c) {
case 0:
break;
case '\177':
case '\b':
if (s > cmd) {
s--;
printf("\b \b");
}
break;
case '\n':
case '\r':
*s = 0;
return;
default:
if ((s - cmd) < (PATH_MAX - 1))
*s++ = c;
xputchar(c);
}
c = GetChar();
}
}
char *getboot(char *kname, char* optarg)
{
char c = 0;
char cmd[PATH_MAX];
printf("\nDefault: %s%s %s\nboot: ", (*optarg) ? "-" : "", optarg, kname);
if ((c = GetChar()) == -1)
return NULL;
cmd[0] = 0;
getstr(cmd,c);
xputchar('\n');
if (parse(cmd,kname,optarg))
xputchar('\a');
else if (devcanon(kname) == 0)
return kname;
return NULL;
}
/*
* Make bootpath canonical, provides defaults when missing
*/
static int
devcanon(char *fname)
{
int ctlr = 0, unit = 0, part = 0;
int c;
char device_name[20];
char file_name[PATH_MAX];
const char *cp;
char *ncp;
//printf("devcanon(%s)\n",fname);
cp = fname;
ncp = device_name;
/* expect a string like '0/ace(0,0)/netbsd' e.g. ctrl/name(unit,part)/file
* Defaults: ctrl=0, name='ace', unit=0, part=0, file=<none>
*/
/* get controller number */
if ((c = *cp) >= '0' && c <= '9') {
ctlr = c - '0';
c = *++cp;
if (c != '/')
return (ENXIO);
c = *++cp;
}
/* get device name */
while ((c = *cp) != '\0') {
if ((c == '(') || (c == '/')) {
cp++;
break;
}
if (ncp < device_name + sizeof(device_name) - 1)
*ncp++ = c;
cp++;
}
/* set default if missing */
if (ncp == device_name) {
strcpy(device_name,"ace");
ncp += 3;
}
/* get device number */
if ((c = *cp) >= '0' && c <= '9') {
unit = c - '0';
c = *++cp;
}
if (c == ',') {
/* get partition number */
if ((c = *++cp) >= '0' && c <= '9') {
part = c - '0';
c = *++cp;
}
}
if (c == ')')
c = *++cp;
if (c == '/')
cp++;
*ncp = '\0';
/* Copy kernel name before we overwrite, then do it */
strcpy(file_name, (*cp) ? cp : kernelnames[0]);
snprintf(fname, PATH_MAX, "%c/%s(%c,%c)/%s",
ctlr + '0', device_name, unit + '0', part + '0', file_name);
//printf("devcanon -> %s\n",fname);
return (0);
}
|