2 * Copyright (c) 1998 Robert Nordier
5 * Redistribution and use in source and binary forms are freely
6 * permitted provided that the above copyright notice and this
7 * paragraph and the following disclaimer are duplicated in all
10 * This software is provided "AS IS" and without any express or
11 * implied warranties, including, without limitation, the implied
12 * warranties of merchantability and fitness for a particular
15 * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $
16 * $DragonFly: src/sys/boot/i386/boot2/Attic/boot2.c,v 1.7 2004/03/04 00:06:58 dillon Exp $
18 #include <sys/param.h>
19 #include <sys/disklabel.h>
20 #include <sys/diskslice.h>
21 #include <sys/diskmbr.h>
22 #include <sys/dirent.h>
23 #include <machine/bootinfo.h>
24 #include <machine/elf.h>
38 #define SECOND 18 /* Circa that many ticks in a second. */
40 #define RBX_ASKNAME 0x0 /* -a */
41 #define RBX_SINGLE 0x1 /* -s */
42 #define RBX_DFLTROOT 0x5 /* -r */
43 #define RBX_KDB 0x6 /* -d */
44 #define RBX_CONFIG 0xa /* -c */
45 #define RBX_VERBOSE 0xb /* -v */
46 #define RBX_SERIAL 0xc /* -h */
47 #define RBX_CDROM 0xd /* -C */
48 #define RBX_GDB 0xf /* -g */
49 #define RBX_MUTE 0x10 /* -m */
50 #define RBX_PAUSE 0x12 /* -p */
51 #define RBX_NOINTR 0x1c /* -n */
52 #define RBX_DUAL 0x1d /* -D */
53 #define RBX_PROBEKBD 0x1e /* -P */
54 /* 0x1f is reserved for the historical RB_BOOTINFO option */
56 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
57 #define RBX_MASK 0x2005ffff
59 #define PATH_CONFIG "/boot.config"
60 #define PATH_BOOT3 "/boot/loader"
61 #define PATH_KERNEL "/kernel"
68 #define V86_CY(x) ((x) & 1)
69 #define V86_ZR(x) ((x) & 0x40)
76 #define TYPE_MAXHARD TYPE_DA
81 static const char optstr[NOPT] = "DhaCgmnPprsv";
82 static const unsigned char flags[NOPT] = {
97 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
98 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
109 static char cmd[512];
110 static char kname[1024];
111 static uint32_t opts;
112 static struct bootinfo bootinfo;
113 static uint8_t ioctrl = IO_KEYBOARD;
116 static void load(void);
117 static int parse(void);
118 static int xfsread(ino_t, void *, size_t);
119 static int dskread(void *, unsigned, unsigned);
120 static void printf(const char *,...);
121 static void putchar(int);
122 static uint32_t memsize(void);
123 static int drvread(void *, unsigned, unsigned);
124 static int keyhit(unsigned);
125 static int xputc(int);
126 static int xgetc(int);
127 static int getc(int);
130 memcpy(void *d, const void *s, int len)
149 strcmp(const char *s1, const char *s2)
151 for (; *s1 == *s2 && *s1; s1++, s2++);
152 return (unsigned char)*s1 - (unsigned char)*s2;
158 xfsread(ino_t inode, void *buf, size_t nbyte)
160 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
161 printf("Invalid %s\n", "format");
167 static inline uint32_t
184 switch (c = xgetc(0)) {
199 if (s - cmd < sizeof(cmd) - 1)
210 v86.eax = 0xe00 | (c & 0xff);
221 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
223 dsk.drive = *(uint8_t *)PTOV(ARGS);
224 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
225 dsk.unit = dsk.drive & DRV_MASK;
226 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
227 bootinfo.bi_version = BOOTINFO_VERSION;
228 bootinfo.bi_size = sizeof(bootinfo);
229 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
230 bootinfo.bi_extmem = memsize();
231 bootinfo.bi_memsizes_valid++;
233 /* Process configuration file */
237 if ((ino = lookup(PATH_CONFIG)))
238 fsread(ino, cmd, sizeof(cmd));
241 printf("%s: %s", PATH_CONFIG, cmd);
244 /* Do not process this command twice */
249 * Try to exec stage 3 boot loader. If interrupted by a keypress,
250 * or in case of failure, try to load a kernel directly instead.
253 if (autoboot && !*kname) {
254 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
255 if (!keyhit(3*SECOND)) {
257 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
261 /* Present the user with the boot2 prompt. */
264 printf("\nDragonFly/i386 boot\n"
265 "Default: %u:%s(%u,%c)%s\n"
267 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
268 'a' + dsk.part, kname);
269 if (ioctrl & IO_SERIAL)
271 if (!autoboot || keyhit(5*SECOND))
283 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
303 if (!(ino = lookup(kname))) {
305 printf("No %s\n", kname);
308 if (xfsread(ino, &hdr, sizeof(hdr)))
310 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
312 else if (IS_ELF(hdr.eh))
315 printf("Invalid %s\n", "format");
319 addr = hdr.ex.a_entry & 0xffffff;
322 if (xfsread(ino, p, hdr.ex.a_text))
324 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
325 if (xfsread(ino, p, hdr.ex.a_data))
327 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
328 bootinfo.bi_symtab = VTOP(p);
329 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
330 p += sizeof(hdr.ex.a_syms);
332 if (xfsread(ino, p, hdr.ex.a_syms))
335 if (xfsread(ino, p, sizeof(int)))
340 if (xfsread(ino, p, x))
345 fs_off = hdr.eh.e_phoff;
346 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
347 if (xfsread(ino, ep + j, sizeof(ep[0])))
349 if (ep[j].p_type == PT_LOAD)
352 for (i = 0; i < 2; i++) {
353 p = PTOV(ep[i].p_paddr & 0xffffff);
354 fs_off = ep[i].p_offset;
355 if (xfsread(ino, p, ep[i].p_filesz))
358 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
359 bootinfo.bi_symtab = VTOP(p);
360 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
361 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
362 (hdr.eh.e_shstrndx + 1);
363 if (xfsread(ino, &es, sizeof(es)))
365 for (i = 0; i < 2; i++) {
366 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
367 p += sizeof(es[i].sh_size);
368 fs_off = es[i].sh_offset;
369 if (xfsread(ino, p, es[i].sh_size))
374 addr = hdr.eh.e_entry & 0xffffff;
376 bootinfo.bi_esymtab = VTOP(p);
377 bootinfo.bi_kernelname = VTOP(kname);
378 bootinfo.bi_bios_dev = dsk.drive;
379 __exec((caddr_t)addr, opts & RBX_MASK,
380 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
381 0, 0, 0, VTOP(&bootinfo));
392 while ((c = *arg++)) {
393 if (c == ' ' || c == '\t' || c == '\n')
395 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
399 while ((c = *arg++)) {
400 for (i = 0; c != optstr[i]; i++)
403 opts ^= 1 << flags[i];
405 if (opts & 1 << RBX_PROBEKBD) {
406 i = *(uint8_t *)PTOV(0x496) & 0x10;
409 opts |= 1 << RBX_DUAL | 1 << RBX_SERIAL;
411 opts &= ~(1 << RBX_PROBEKBD);
413 ioctrl = opts & 1 << RBX_DUAL ? (IO_SERIAL|IO_KEYBOARD) :
414 opts & 1 << RBX_SERIAL ? IO_SERIAL : IO_KEYBOARD;
415 if (ioctrl & IO_SERIAL)
418 for (q = arg--; *q && *q != '('; q++);
429 for (i = 0; arg[0] != dev_nm[i][0] ||
430 arg[1] != dev_nm[i][1]; i++)
435 dsk.unit = *arg - '0';
436 if (arg[1] != ',' || dsk.unit > 9)
439 dsk.slice = WHOLE_DISK_SLICE;
441 dsk.slice = *arg - '0' + 1;
442 if (dsk.slice > NDOSPART)
448 dsk.part = *arg - 'a';
454 dsk.drive = (dsk.type <= TYPE_MAXHARD
455 ? DRV_HARD : 0) + drv;
458 if ((i = p - arg - !*(p - 1))) {
459 if ((size_t)i >= sizeof(kname))
461 memcpy(kname, arg, i + 1);
470 dskread(void *buf, unsigned lba, unsigned nblk)
472 struct dos_partition *dp;
478 sec = dmadat->secbuf;
480 if (drvread(sec, DOSBBSECTOR, 1))
482 dp = (void *)(sec + DOSPARTOFF);
484 if (sl < BASE_SLICE) {
485 for (i = 0; i < NDOSPART; i++)
486 if (dp[i].dp_typ == DOSPTYP_386BSD &&
487 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
489 if (dp[i].dp_flag & 0x80 ||
490 dsk.slice == COMPATIBILITY_SLICE)
493 if (dsk.slice == WHOLE_DISK_SLICE)
496 if (sl != WHOLE_DISK_SLICE) {
497 if (sl != COMPATIBILITY_SLICE)
498 dp += sl - BASE_SLICE;
499 if (dp->dp_typ != DOSPTYP_386BSD) {
500 printf("Invalid %s\n", "slice");
503 dsk.start = dp->dp_start;
505 if (drvread(sec, dsk.start + LABELSECTOR, 1))
507 d = (void *)(sec + LABELOFFSET);
508 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
509 if (dsk.part != RAW_PART) {
510 printf("Invalid %s\n", "label");
515 if (d->d_type == DTYPE_SCSI)
519 if (dsk.part >= d->d_npartitions ||
520 !d->d_partitions[dsk.part].p_size) {
521 printf("Invalid %s\n", "partition");
524 dsk.start += d->d_partitions[dsk.part].p_offset;
525 dsk.start -= d->d_partitions[RAW_PART].p_offset;
528 return drvread(buf, dsk.start + lba, nblk);
532 printf(const char *fmt,...)
541 while ((c = *fmt++)) {
546 putchar(va_arg(ap, int));
549 for (s = va_arg(ap, char *); *s; s++)
553 u = va_arg(ap, unsigned);
556 *s++ = '0' + u % 10U;
578 drvread(void *buf, unsigned lba, unsigned nblk)
580 static unsigned c = 0x2d5c7c2f;
582 printf("%c\b", c = c << 8 | c >> 24);
583 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
584 v86.addr = XREADORG; /* call to xread in boot1 */
585 v86.es = VTOPSEG(buf);
587 v86.ebx = VTOPOFF(buf);
589 v86.edx = nblk << 8 | dsk.drive;
592 if (V86_CY(v86.efl)) {
593 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
600 keyhit(unsigned ticks)
604 if (opts & 1 << RBX_NOINTR)
610 t1 = *(uint32_t *)PTOV(0x46c);
613 if (t1 < t0 || t1 >= t0 + ticks)
621 if (ioctrl & IO_KEYBOARD)
623 if (ioctrl & IO_SERIAL)
631 if (opts & 1 << RBX_NOINTR)
634 if (ioctrl & IO_KEYBOARD && getc(1))
635 return fn ? 1 : getc(0);
636 if (ioctrl & IO_SERIAL && sio_ischar())
637 return fn ? 1 : sio_getc();
649 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);