2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1998 Robert Nordier
35 * All rights reserved.
37 * Redistribution and use in source and binary forms are freely
38 * permitted provided that the above copyright notice and this
39 * paragraph and the following disclaimer are duplicated in all
42 * This software is provided "AS IS" and without any express or
43 * implied warranties, including, without limitation, the implied
44 * warranties of merchantability and fitness for a particular
47 * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $
48 * $DragonFly: src/sys/boot/pc32/boot2/boot2.c,v 1.17 2008/09/02 17:21:17 dillon Exp $
50 #include <sys/param.h>
51 #include <sys/disklabel32.h>
52 #include <sys/diskslice.h>
53 #include <sys/diskmbr.h>
54 #include <sys/dtype.h>
55 #include <sys/dirent.h>
56 #include <machine/bootinfo.h>
57 #include <machine/elf.h>
67 #include "../bootasm.h"
69 #define SECOND 18 /* Circa that many ticks in a second. */
71 #define RBX_ASKNAME 0x0 /* -a */
72 #define RBX_SINGLE 0x1 /* -s */
73 #define RBX_DFLTROOT 0x5 /* -r */
74 #define RBX_KDB 0x6 /* -d */
75 #define RBX_CONFIG 0xa /* -c */
76 #define RBX_VERBOSE 0xb /* -v */
77 #define RBX_SERIAL 0xc /* -h */
78 #define RBX_CDROM 0xd /* -C */
79 #define RBX_GDB 0xf /* -g */
80 #define RBX_MUTE 0x10 /* -m */
81 #define RBX_PAUSE 0x12 /* -p */
82 #define RBX_NOINTR 0x1c /* -n */
83 #define RBX_VIDEO 0x1d /* -V */
84 #define RBX_PROBEKBD 0x1e /* -P */
85 /* 0x1f is reserved for the historical RB_BOOTINFO option */
87 #define RBF_MUTE (1 << RBX_MUTE)
88 #define RBF_SERIAL (1 << RBX_SERIAL)
89 #define RBF_VIDEO (1 << RBX_VIDEO)
91 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
92 #define RBX_MASK 0x2005ffff
94 #define PATH_CONFIG "/boot.config"
95 #define PATH_BOOT3 "/boot/loader" /* /boot in root */
96 #define PATH_BOOT3_ALT "/loader" /* /boot partition */
97 #define PATH_KERNEL "/kernel"
100 #define MEM_BASE 0x12
102 #define V86_CY(x) ((x) & 1)
103 #define V86_ZR(x) ((x) & 0x40)
105 #define DRV_HARD 0x80
106 #define DRV_MASK 0x7f
110 #define TYPE_MAXHARD TYPE_DA
115 #define INVALID_S "Bad %s\n"
117 extern uint32_t _end;
119 static const char optstr[NOPT] = { "VhaCgmnPprsv" };
120 static const unsigned char flags[NOPT] = {
135 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
136 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
147 static char cmd[512];
148 static char kname[1024];
149 static uint32_t opts;
150 static struct bootinfo bootinfo;
153 static void load(void);
154 static int parse(void);
155 static int xfsread(ino_t, void *, size_t);
156 static int dskread(void *, unsigned, unsigned);
157 static void printf(const char *,...);
158 static void putchar(int);
159 static uint32_t memsize(void);
160 static int drvread(void *, unsigned, unsigned);
161 static int keyhit(unsigned);
162 static void xputc(int);
163 static int xgetc(int);
164 static int getc(int);
167 memcpy(void *d, const void *s, int len)
186 strcmp(const char *s1, const char *s2)
188 for (; *s1 == *s2 && *s1; s1++, s2++);
189 return (unsigned char)*s1 - (unsigned char)*s2;
195 xfsread(ino_t inode, void *buf, size_t nbyte)
197 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
198 printf(INVALID_S, "format");
204 static inline uint32_t
221 switch (c = xgetc(0)) {
236 if (s - cmd < sizeof(cmd) - 1)
247 v86.eax = 0xe00 | (c & 0xff);
258 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
260 dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG);
261 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
262 dsk.unit = dsk.drive & DRV_MASK;
263 dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1;
264 bootinfo.bi_version = BOOTINFO_VERSION;
265 bootinfo.bi_size = sizeof(bootinfo);
266 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
267 bootinfo.bi_extmem = memsize();
268 bootinfo.bi_memsizes_valid++;
270 /* Process configuration file */
274 if ((ino = lookup(PATH_CONFIG)))
275 fsread(ino, cmd, sizeof(cmd));
278 printf("%s: %s", PATH_CONFIG, cmd);
281 /* Do not process this command twice */
286 * Setup our (serial) console after processing the config file. If
287 * the initialization fails, don't try to use the serial port. This
288 * can happen if the serial port is unmaped (happens on new laptops a lot).
290 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
291 opts |= RBF_SERIAL|RBF_VIDEO;
292 if (opts & RBF_SERIAL) {
299 * Try to exec stage 3 boot loader. If interrupted by a keypress,
300 * or in case of failure, try to load a kernel directly instead.
302 * We have to try boot /boot/loader and /loader to support booting
303 * from a /boot partition instead of a root partition.
305 if (autoboot && !*kname) {
306 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
307 if (!keyhit(3*SECOND)) {
309 memcpy(kname, PATH_BOOT3_ALT, sizeof(PATH_BOOT3_ALT));
311 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
315 /* Present the user with the boot2 prompt. */
318 printf("\nDragonFly boot\n"
320 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
321 'a' + dsk.part, kname);
322 if (!autoboot || keyhit(5*SECOND))
334 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
354 if (!(ino = lookup(kname))) {
356 printf("No %s\n", kname);
359 if (xfsread(ino, &hdr, sizeof(hdr)))
361 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
363 else if (IS_ELF(hdr.eh))
366 printf(INVALID_S, "format");
370 addr = hdr.ex.a_entry & 0xffffff;
373 if (xfsread(ino, p, hdr.ex.a_text))
375 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
376 if (xfsread(ino, p, hdr.ex.a_data))
378 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
379 bootinfo.bi_symtab = VTOP(p);
380 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
381 p += sizeof(hdr.ex.a_syms);
383 if (xfsread(ino, p, hdr.ex.a_syms))
386 if (xfsread(ino, p, sizeof(int)))
391 if (xfsread(ino, p, x))
396 fs_off = hdr.eh.e_phoff;
397 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
398 if (xfsread(ino, ep + j, sizeof(ep[0])))
400 if (ep[j].p_type == PT_LOAD)
403 for (i = 0; i < 2; i++) {
404 p = PTOV(ep[i].p_paddr & 0xffffff);
405 fs_off = ep[i].p_offset;
406 if (xfsread(ino, p, ep[i].p_filesz))
409 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
410 bootinfo.bi_symtab = VTOP(p);
411 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
412 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
413 (hdr.eh.e_shstrndx + 1);
414 if (xfsread(ino, &es, sizeof(es)))
416 for (i = 0; i < 2; i++) {
417 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
418 p += sizeof(es[i].sh_size);
419 fs_off = es[i].sh_offset;
420 if (xfsread(ino, p, es[i].sh_size))
425 addr = hdr.eh.e_entry & 0xffffff;
427 bootinfo.bi_esymtab = VTOP(p);
428 bootinfo.bi_kernelname = VTOP(kname);
429 bootinfo.bi_bios_dev = dsk.drive;
430 __exec((caddr_t)addr, opts & RBX_MASK,
431 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
432 0, 0, 0, VTOP(&bootinfo));
443 while ((c = *arg++)) {
444 if (c == ' ' || c == '\t' || c == '\n')
446 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
451 while ((c = *arg++)) {
452 for (i = NOPT - 1; i >= 0; --i) {
453 if (optstr[i] == c) {
454 opts ^= 1 << flags[i];
459 ok: ; /* ugly but save space */
461 if (opts & (1 << RBX_PROBEKBD)) {
462 i = *(uint8_t *)PTOV(0x496) & 0x10;
465 opts |= RBF_VIDEO | RBF_SERIAL;
467 opts &= ~(1 << RBX_PROBEKBD);
470 for (q = arg--; *q && *q != '('; q++);
481 for (i = 0; arg[0] != dev_nm[i][0] ||
482 arg[1] != dev_nm[i][1]; i++)
487 dsk.unit = *arg - '0';
488 if (arg[1] != ',' || dsk.unit > 9)
491 dsk.slice = WHOLE_DISK_SLICE;
493 dsk.slice = *arg - '0' + 1;
494 if (dsk.slice > NDOSPART)
500 dsk.part = *arg - 'a';
506 dsk.drive = (dsk.type <= TYPE_MAXHARD
507 ? DRV_HARD : 0) + drv;
510 if ((i = p - arg - !*(p - 1))) {
511 if ((size_t)i >= sizeof(kname))
513 memcpy(kname, arg, i + 1);
522 dskread(void *buf, unsigned lba, unsigned nblk)
524 struct dos_partition *dp;
525 struct disklabel32 *d;
530 sec = dmadat->secbuf;
532 if (drvread(sec, DOSBBSECTOR, 1))
534 dp = (void *)(sec + DOSPARTOFF);
536 if (sl < BASE_SLICE) {
537 for (i = 0; i < NDOSPART; i++)
538 if (dp[i].dp_typ == DOSPTYP_386BSD &&
539 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
541 if (dp[i].dp_flag & 0x80 ||
542 dsk.slice == COMPATIBILITY_SLICE)
545 if (dsk.slice == WHOLE_DISK_SLICE)
548 if (sl != WHOLE_DISK_SLICE) {
549 if (sl != COMPATIBILITY_SLICE)
550 dp += sl - BASE_SLICE;
551 if (dp->dp_typ != DOSPTYP_386BSD) {
552 printf(INVALID_S, "slice");
555 dsk.start = dp->dp_start;
557 if (drvread(sec, dsk.start + LABELSECTOR32, 1))
559 d = (void *)(sec + LABELOFFSET32);
560 if (d->d_magic != DISKMAGIC32 || d->d_magic2 != DISKMAGIC32) {
561 if (dsk.part != RAW_PART) {
562 printf(INVALID_S, "label");
567 if (d->d_type == DTYPE_SCSI)
571 if (dsk.part >= d->d_npartitions ||
572 !d->d_partitions[dsk.part].p_size) {
573 printf(INVALID_S, "partition");
576 dsk.start += d->d_partitions[dsk.part].p_offset;
577 dsk.start -= d->d_partitions[RAW_PART].p_offset;
580 return drvread(buf, dsk.start + lba, nblk);
584 printf(const char *fmt,...)
593 while ((c = *fmt++)) {
598 putchar(va_arg(ap, int));
601 for (s = va_arg(ap, char *); *s; s++)
605 u = va_arg(ap, unsigned);
608 *s++ = '0' + u % 10U;
630 drvread(void *buf, unsigned lba, unsigned nblk)
632 static unsigned c = 0x2d5c7c2f; /* twiddle */
634 c = (c << 8) | (c >> 24);
637 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
638 v86.addr = XREADORG; /* call to xread in boot1 */
639 v86.es = VTOPSEG(buf);
641 v86.ebx = VTOPOFF(buf);
643 v86.edx = nblk << 8 | dsk.drive;
646 if (V86_CY(v86.efl)) {
647 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
654 keyhit(unsigned ticks)
658 if (opts & 1 << RBX_NOINTR)
664 t1 = *(uint32_t *)PTOV(0x46c);
667 if (t1 < t0 || t1 >= t0 + ticks)
675 if (opts & RBF_VIDEO)
677 if (opts & RBF_SERIAL)
684 if (opts & 1 << RBX_NOINTR)
687 if ((opts & RBF_VIDEO) && getc(1))
688 return fn ? 1 : getc(0);
689 if ((opts & RBF_SERIAL) && sio_ischar())
690 return fn ? 1 : sio_getc();
702 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);