Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.git] / sys / boot / pc98 / boot2 / boot.c
CommitLineData
984263bc
MD
1/*
2 * Mach Operating System
3 * Copyright (c) 1992, 1991 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 *
26 * from: Mach, [92/04/03 16:51:14 rvb]
984263bc 27 */
984263bc
MD
28/*
29 Copyright 1988, 1989, 1990, 1991, 1992
30 by Intel Corporation, Santa Clara, California.
31
32 All Rights Reserved
33
34Permission to use, copy, modify, and distribute this software and
35its documentation for any purpose and without fee is hereby
36granted, provided that the above copyright notice appears in all
37copies and that both the copyright notice and this permission notice
38appear in supporting documentation, and that the name of Intel
39not be used in advertising or publicity pertaining to distribution
40of the software without specific, written prior permission.
41
42INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
43INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
44IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
45CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
46LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
47NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
48WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5ee58eed 49
984263bc
MD
50*/
51
5ee58eed
MD
52/*
53 * $FreeBSD: src/sys/boot/pc98/boot2/boot.c,v 1.11 2003/09/08 09:11:20 obrien Exp $
54 * $DragonFly: src/sys/boot/pc98/boot2/Attic/boot.c,v 1.3 2003/11/10 06:08:38 dillon Exp $
55 */
56
984263bc
MD
57#include "boot.h"
58#include <a.out.h>
59#include <sys/reboot.h>
60#include <machine/bootinfo.h>
61
62#define ouraddr (BOOTSEG << 4) /* XXX */
63
64#define BOOT_CONFIG_SIZE 512
65#define BOOT_HELP_SIZE 2048
66#define KERNEL_CONFIG_SIZE 512
67#define NAMEBUF_LEN 1024 /* oversized to defend against gets() */
68
69static char boot_config[BOOT_CONFIG_SIZE];
70static char boot_help[BOOT_HELP_SIZE];
71char *name;
72static char kernel_config[KERNEL_CONFIG_SIZE];
73static char kernel_config_namebuf[NAMEBUF_LEN + sizeof "config"];
74static char linebuf[NAMEBUF_LEN];
75static char namebuf[NAMEBUF_LEN];
76static struct bootinfo bootinfo;
77int loadflags;
78
79static void getbootdev(char *ptr, int *howto);
80static void loadprog(void);
81static void readfile(char *path, char *buf, size_t nbytes);
82
83/* NORETURN */
84void
85boot(int drive)
86{
87 int ret;
88#ifdef PC98
89 int i;
90 unsigned char disk_equips;
91#endif
92
93 /* Pick up the story from the Bios on geometry of disks */
94
95#ifdef PC98
96 for(ret = 0; ret < 2; ret ++) {
97 if (*(unsigned char*)V(0xA155d) & (1 << ret)) {
98 bootinfo.bi_bios_geom[ret] = get_diskinfo(ret + 0x80);
99 }
100 }
101#else /* IBM-PC */
102 for(ret = 0; ret < N_BIOS_GEOM; ret ++)
103 bootinfo.bi_bios_geom[ret] = get_diskinfo(ret + 0x80);
104#endif /* PC98 */
105
106 bootinfo.bi_basemem = memsize(0);
107 bootinfo.bi_extmem = memsize(1);
108 bootinfo.bi_memsizes_valid = 1;
109
110 gateA20();
111
112#ifdef PC98
113 /* set machine type to PC98_SYSTEM_PARAMETER */
114 machine_check();
115#endif /* PC98 */
116
117 /*
118 * The default boot device is the first partition in the
119 * compatibility slice on the boot drive.
120 */
121 dosdev = drive;
122#ifdef PC98
123 maj = (drive&0x70) >> 3; /* a good first bet */
124 if (maj == 4) { /* da */
125 disk_equips = *(unsigned char *)V(0xA1482);
126 unit = 0;
127 for (i=0; i<(drive&0x0f); i++) {
128 int media = ((unsigned *)V(0xA1460))[i] & 0x1F;
129
130 if ((disk_equips >> i) & 1) /* HD */
131 unit++;
132 else if (media == 7) /* MO */
133 unit++;
134 }
135 } else {
136 unit = drive & 0x0f;
137 }
138#else /* IBM-PC */
139 maj = 2;
140 unit = drive & 0x7f;
141#ifdef dontneed
142 slice = 0;
143 part = 0;
144#endif
145 if (drive & 0x80) {
146 /* Hard drive. Adjust. */
147 maj = 0;
148#if BOOT_HD_BIAS > 0
149 if (unit >= BOOT_HD_BIAS) {
150 /*
151 * The drive is probably a SCSI drive with a unit
152 * number BOOT_HD_BIAS less than the BIOS drive
153 * number.
154 */
155 maj = 4;
156 unit -= BOOT_HD_BIAS;
157 }
158#endif
159 }
160#endif /* PC98 */
161 readfile("boot.config", boot_config, BOOT_CONFIG_SIZE);
162 name = "/boot/loader";
163 if (boot_config[0] != '\0') {
164 printf("boot.config: %s", boot_config);
165 getbootdev(boot_config, &loadflags);
166 if (openrd() != 0)
167 name = "kernel";
168 }
169loadstart:
170 /* print this all each time.. (saves space to do so) */
171 /* If we have looped, use the previous entries as defaults */
172 printf("\r \n>> FreeBSD BOOT @ 0x%x: %d/%d k of memory, %s%s console\n"
173 "Boot default: %d:%s(%d,%c)%s\n"
174 "%s\n"
175 "boot: ",
176 ouraddr, bootinfo.bi_basemem, bootinfo.bi_extmem,
177 (loadflags & RB_SERIAL) ? "serial" : "internal",
178 (loadflags & RB_DUAL) ? "/dual" : "",
179#ifdef PC98
180 dosdev & 0x0f, devs[maj], unit, 'a' + part,
181#else
182 dosdev & 0x7f, devs[maj], unit, 'a' + part,
183#endif
184 name ? name : "*specify_a_kernel_name*",
185 boot_help);
186
187 /*
188 * Ignore flags from previous attempted boot, if any.
189 * XXX this is now too strict. Settings given in boot.config should
190 * not be changed.
191 */
192 loadflags &= (RB_DUAL | RB_SERIAL);
193
194 /*
195 * Be paranoid and make doubly sure that the input buffer is empty.
196 */
197 if (loadflags & (RB_DUAL | RB_SERIAL))
198 init_serial();
199
200 if (!gets(linebuf))
201 putchar('\n');
202 else
203 getbootdev(linebuf, &loadflags);
204 if (name == NULL)
205 goto loadstart;
206 ret = openrd();
207 if (ret != 0) {
208 if (ret > 0)
209 printf("Can't find %s\n", name);
210 goto loadstart;
211 }
212/* if (inode.i_mode&IEXEC)
213 loadflags |= RB_KDB;
214*/
215 loadprog();
216 goto loadstart;
217}
218
219static void
220loadprog(void)
221{
222 struct exec head;
223 int startaddr;
224 int addr; /* physical address.. not directly useable */
225 int bootdev;
226 int i;
227 unsigned pad;
228 char *s, *t;
229
230 read((void *)&head, sizeof(head));
231 if ( N_BADMAG(head)) {
232 printf("Invalid format!\n");
233 return;
234 }
235
236 poff = N_TXTOFF(head);
237 /*if(poff==0)
238 poff = 32;*/
239
240 /*
241 * We assume that the entry address is the same as the lowest text
242 * address and that the kernel startup code handles relocation by
243 * this address rounded down to a multiple of 16M.
244 */
245 startaddr = head.a_entry & 0x00FFFFFF;
246 addr = startaddr;
247 printf("Booting %d:%s(%d,%c)%s @ 0x%x\n"
248#ifdef PC98
249 , dosdev & 0x0f
250#else
251 , dosdev & 0x7f
252#endif
253 , devs[maj]
254 , unit
255 , 'a'+part
256 , name
257 , addr);
258 if(addr < 0x00100000)
259 {
260 /*
261 * Bail out, instead of risking to damage the BIOS
262 * variables, the loader, or the adapter memory area.
263 * We don't support loading below 1 MB any more.
264 */
265 printf("Start address too low\n");
266 return;
267 }
268 printf("text=0x%x ", head.a_text);
269 /********************************************************/
270 /* LOAD THE TEXT SEGMENT */
271 /********************************************************/
272 xread((void *)addr, head.a_text);
273 addr += head.a_text;
274
275 /********************************************************/
276 /* Load the Initialised data after the text */
277 /********************************************************/
278 while (addr & PAGE_MASK)
279 *(char *)addr++ = 0;
280
281 printf("data=0x%x ", head.a_data);
282 xread((void *)addr, head.a_data);
283 addr += head.a_data;
284
285 /********************************************************/
286 /* Skip over the uninitialised data */
287 /* (but clear it) */
288 /********************************************************/
289 printf("bss=0x%x ", head.a_bss);
290
291/*
292 * XXX however, we should be checking that we don't load ... into
293 * nonexistent memory. A full symbol table is unlikely to fit on 4MB
294 * machines.
295 */
296 /* kzip & kernel will zero their own bss */
297 addr += head.a_bss;
298
299 /* Pad to a page boundary. */
300 pad = (unsigned)addr & PAGE_MASK;
301 if (pad != 0) {
302 pad = PAGE_SIZE - pad;
303 addr += pad;
304 }
305 bootinfo.bi_symtab = addr;
306
307 /********************************************************/
308 /* Copy the symbol table size */
309 /********************************************************/
310 pcpy(&head.a_syms, (void *)addr, sizeof(head.a_syms));
311 addr += sizeof(head.a_syms);
312
313 /********************************************************/
314 /* Load the symbol table */
315 /********************************************************/
316 printf("symbols=[+0x%x+0x%x+0x%x", pad, sizeof(head.a_syms),
317 head.a_syms);
318 xread((void *)addr, head.a_syms);
319 addr += head.a_syms;
320
321 /********************************************************/
322 /* Load the string table size */
323 /********************************************************/
324 read((void *)&i, sizeof(int));
325 pcpy(&i, (void *)addr, sizeof(int));
326 i -= sizeof(int);
327 addr += sizeof(int);
328
329 /********************************************************/
330 /* Load the string table */
331 /********************************************************/
332 printf("+0x%x+0x%x]\n", sizeof(int), i);
333 xread((void *)addr, i);
334 addr += i;
335
336 bootinfo.bi_esymtab = addr;
337
338 /*
339 * For backwards compatibility, use the previously-unused adaptor
340 * and controller bitfields to hold the slice number.
341 */
342 bootdev = MAKEBOOTDEV(maj, (slice >> 4), slice & 0xf, unit, part);
343
344 bootinfo.bi_version = BOOTINFO_VERSION;
345 bootinfo.bi_kernelname = (u_int32_t)(name + ouraddr);
346 bootinfo.bi_nfs_diskless = 0;
347 bootinfo.bi_size = sizeof(bootinfo);
348 bootinfo.bi_bios_dev = dosdev;
349
350 /*
351 * Load the kernel config file (if any). Its name is given by
352 * appending ".config" to the kernel name. Build the name inline
353 * because no str*() functions are available. The file has to be
354 * copied to &disklabel for userconfig. It can't be loaded there
355 * directly because the label is used late in readfile() in some
356 * unusual cases.
357 */
358 s = name;
359 t = kernel_config_namebuf;
360 do
361 ;
362 while ((*t++ = *s++) != '\0');
363 s = ".config";
364 --t;
365 do
366 ;
367 while ((*t++ = *s++) != '\0');
368 readfile(kernel_config_namebuf, kernel_config, KERNEL_CONFIG_SIZE);
369 pcpy(kernel_config, (char *)&disklabel + ouraddr, KERNEL_CONFIG_SIZE);
370
371 printf("total=0x%x entry point=0x%x\n", addr, startaddr);
372 startprog(startaddr, loadflags | RB_BOOTINFO, bootdev,
373 (unsigned)&bootinfo + ouraddr);
374}
375
376static void
377readfile(char *path, char *buf, size_t nbytes)
378{
379 int openstatus;
380
381 buf[0] = '\0';
382 name = path;
383 openstatus = openrd();
384 if (openstatus == 0) {
385 /* XXX no way to determine file size. */
386 read(buf, nbytes);
387 }
388 buf[nbytes - 1] = '\0';
389}
390
391static void
392getbootdev(char *ptr, int *howto)
393{
394 char c;
395 int f;
396 char *p;
397
398 /* Copy the flags to save some bytes. */
399 f = *howto;
400
401 c = *ptr;
402 for (;;) {
403nextarg:
404 while (c == ' ' || c == '\n')
405 c = *++ptr;
406 if (c == '-')
407 while ((c = *++ptr) != '\0') {
408 if (c == ' ' || c == '\n')
409 goto nextarg;
410 if (c == 'a')
411 f |= RB_ASKNAME;
412 if (c == 'C')
413 f |= RB_CDROM;
414 if (c == 'c')
415 f |= RB_CONFIG;
416 if (c == 'D')
417 f ^= RB_DUAL;
418 if (c == 'd')
419 f |= RB_KDB;
420 if (c == 'g')
421 f |= RB_GDB;
422 if (c == 'h')
423 f ^= RB_SERIAL;
424 if (c == 'P')
425 f |= RB_PROBEKBD;
426 if (c == 'r')
427 f |= RB_DFLTROOT;
428 if (c == 's')
429 f |= RB_SINGLE;
430 if (c == 'v')
431 f |= RB_VERBOSE;
432 }
433 if (c == '\0')
434 break;
435 p = name = namebuf;
436 while (c != '\0' && c != ' ' && c != '\n') {
437 *p++ = c;
438 c = *++ptr;
439 }
440 *p = '\0';
441 }
442 if (f & RB_PROBEKBD) {
443 if (probe_keyboard()) {
444 f |= RB_DUAL | RB_SERIAL;
445 printf("No keyboard found\n");
446 } else
447 printf("Keyboard found\n");
448 }
449 if (f & (RB_DUAL | RB_SERIAL))
450 init_serial();
451 *howto = f;
452}