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.18 2008/09/13 11:45:45 corecode Exp $
51 #include <sys/param.h>
53 #include <sys/disklabel64.h>
55 #include <sys/disklabel32.h>
57 #include <sys/diskslice.h>
58 #include <sys/diskmbr.h>
59 #include <sys/dtype.h>
60 #include <sys/dirent.h>
61 #include <machine/bootinfo.h>
62 #include <machine/elf.h>
77 #include "../bootasm.h"
79 #define SECOND 18 /* Circa that many ticks in a second. */
81 #define RBX_ASKNAME 0x0 /* -a */
82 #define RBX_SINGLE 0x1 /* -s */
83 #define RBX_DFLTROOT 0x5 /* -r */
84 #define RBX_KDB 0x6 /* -d */
85 #define RBX_CONFIG 0xa /* -c */
86 #define RBX_VERBOSE 0xb /* -v */
87 #define RBX_SERIAL 0xc /* -h */
88 #define RBX_CDROM 0xd /* -C */
89 #define RBX_GDB 0xf /* -g */
90 #define RBX_MUTE 0x10 /* -m */
91 #define RBX_PAUSE 0x12 /* -p */
92 #define RBX_NOINTR 0x1c /* -n */
93 #define RBX_VIDEO 0x1d /* -V */
94 #define RBX_PROBEKBD 0x1e /* -P */
95 /* 0x1f is reserved for the historical RB_BOOTINFO option */
97 #define RBF_MUTE (1 << RBX_MUTE)
98 #define RBF_SERIAL (1 << RBX_SERIAL)
99 #define RBF_VIDEO (1 << RBX_VIDEO)
101 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
102 #define RBX_MASK 0x2005ffff
104 #define PATH_CONFIG "/boot.config"
105 #define PATH_BOOT3 "/boot/loader" /* /boot in root */
106 #define PATH_BOOT3_ALT "/loader" /* /boot partition */
107 #define PATH_KERNEL "/kernel"
110 #define MEM_BASE 0x12
112 #define V86_CY(x) ((x) & 1)
113 #define V86_ZR(x) ((x) & 0x40)
115 #define DRV_HARD 0x80
116 #define DRV_MASK 0x7f
120 #define TYPE_MAXHARD TYPE_DA
125 #define INVALID_S "Bad %s\n"
127 extern uint32_t _end;
129 static const char optstr[NOPT] = { "VhaCgmnPprsv" };
130 static const unsigned char flags[NOPT] = {
145 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
146 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
158 static char cmd[512];
159 static char kname[1024];
160 static uint32_t opts = RBF_VIDEO;
161 static struct bootinfo bootinfo;
164 * boot2 encapsulated ABI elements provided to *fsread.c
166 * NOTE: boot2_dmadat is extended by per-filesystem APIs
170 struct boot2_dmadat *boot2_dmadat;
173 static void load(void);
174 static int parse(void);
175 static int dskprobe(void);
176 static int xfsread(boot2_ino_t, void *, size_t);
177 static uint32_t memsize(void);
178 static int drvread(void *, unsigned, unsigned);
179 static int keyhit(unsigned);
180 static void xputc(int);
181 static int xgetc(int);
182 static int getc(int);
185 memcpy(void *d, const void *s, int len)
204 strcmp(const char *s1, const char *s2)
206 for (; *s1 == *s2 && *s1; s1++, s2++)
208 return ((int)((unsigned char)*s1 - (unsigned char)*s2));
211 #if defined(UFS) && defined(HAMMERFS)
213 const struct boot2_fsapi *fsapi;
217 #define fsapi (&boot2_ufs_api)
219 #elif defined(HAMMERFS)
221 #define fsapi (&boot2_hammer_api)
226 xfsread(boot2_ino_t inode, void *buf, size_t nbyte)
228 if ((size_t)fsapi->fsread(inode, buf, nbyte) != nbyte) {
229 printf(INVALID_S, "format");
235 static inline uint32_t
252 switch (c = xgetc(0)) {
267 if (s - cmd < sizeof(cmd) - 1)
278 v86.eax = 0xe00 | (c & 0xff);
290 (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
292 dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG);
293 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
294 dsk.unit = dsk.drive & DRV_MASK;
295 dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1;
296 bootinfo.bi_version = BOOTINFO_VERSION;
297 bootinfo.bi_size = sizeof(bootinfo);
298 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
299 bootinfo.bi_extmem = memsize();
300 bootinfo.bi_memsizes_valid++;
305 * Probe the default disk and process the configuration file if
308 if (dskprobe() == 0) {
309 if ((ino = fsapi->fslookup(PATH_CONFIG)))
310 fsapi->fsread(ino, cmd, sizeof(cmd));
314 * Parse config file if present. parse() will re-probe if necessary.
317 printf("%s: %s", PATH_CONFIG, cmd);
320 /* Do not process this command twice */
325 * Setup our (serial) console after processing the config file. If
326 * the initialization fails, don't try to use the serial port. This
327 * can happen if the serial port is unmaped (happens on new laptops a lot).
329 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
330 opts |= RBF_SERIAL|RBF_VIDEO;
331 if (opts & RBF_SERIAL) {
338 * Try to exec stage 3 boot loader. If interrupted by a keypress,
339 * or in case of failure, try to load a kernel directly instead.
341 * We have to try boot /boot/loader and /loader to support booting
342 * from a /boot partition instead of a root partition.
344 if (autoboot && !*kname) {
345 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
346 if (!keyhit(3*SECOND)) {
348 memcpy(kname, PATH_BOOT3_ALT, sizeof(PATH_BOOT3_ALT));
350 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
354 /* Present the user with the boot2 prompt. */
357 printf("\nDragonFly boot\n"
359 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
360 'a' + dsk.part, kname);
361 if (!autoboot || keyhit(5*SECOND))
373 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
393 if (!(ino = fsapi->fslookup(kname))) {
395 printf("No %s\n", kname);
398 if (xfsread(ino, &hdr, sizeof(hdr)))
400 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
402 else if (IS_ELF(hdr.eh))
405 printf(INVALID_S, "format");
409 addr = hdr.ex.a_entry & 0xffffff;
412 if (xfsread(ino, p, hdr.ex.a_text))
414 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
415 if (xfsread(ino, p, hdr.ex.a_data))
417 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
418 bootinfo.bi_symtab = VTOP(p);
419 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
420 p += sizeof(hdr.ex.a_syms);
422 if (xfsread(ino, p, hdr.ex.a_syms))
425 if (xfsread(ino, p, sizeof(int)))
430 if (xfsread(ino, p, x))
435 fs_off = hdr.eh.e_phoff;
436 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
437 if (xfsread(ino, ep + j, sizeof(ep[0])))
439 if (ep[j].p_type == PT_LOAD)
442 for (i = 0; i < 2; i++) {
443 p = PTOV(ep[i].p_paddr & 0xffffff);
444 fs_off = ep[i].p_offset;
445 if (xfsread(ino, p, ep[i].p_filesz))
448 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
449 bootinfo.bi_symtab = VTOP(p);
450 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
451 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
452 (hdr.eh.e_shstrndx + 1);
453 if (xfsread(ino, &es, sizeof(es)))
455 for (i = 0; i < 2; i++) {
456 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
457 p += sizeof(es[i].sh_size);
458 fs_off = es[i].sh_offset;
459 if (xfsread(ino, p, es[i].sh_size))
464 addr = hdr.eh.e_entry & 0xffffff;
466 bootinfo.bi_esymtab = VTOP(p);
467 bootinfo.bi_kernelname = VTOP(kname);
468 bootinfo.bi_bios_dev = dsk.drive;
469 __exec((caddr_t)addr, opts & RBX_MASK,
470 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
471 0, 0, 0, VTOP(&bootinfo));
482 while ((c = *arg++)) {
483 if (c == ' ' || c == '\t' || c == '\n')
485 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
490 while ((c = *arg++)) {
491 for (i = NOPT - 1; i >= 0; --i) {
492 if (optstr[i] == c) {
493 opts ^= 1 << flags[i];
498 ok: ; /* ugly but save space */
500 if (opts & (1 << RBX_PROBEKBD)) {
501 i = *(uint8_t *)PTOV(0x496) & 0x10;
504 opts |= RBF_VIDEO | RBF_SERIAL;
506 opts &= ~(1 << RBX_PROBEKBD);
509 for (q = arg--; *q && *q != '('; q++);
520 for (i = 0; arg[0] != dev_nm[i][0] ||
521 arg[1] != dev_nm[i][1]; i++)
526 dsk.unit = *arg - '0';
527 if (arg[1] != ',' || dsk.unit > 9)
530 dsk.slice = WHOLE_DISK_SLICE;
532 dsk.slice = *arg - '0' + 1;
533 if (dsk.slice > NDOSPART)
539 dsk.part = *arg - 'a';
545 dsk.drive = (dsk.type <= TYPE_MAXHARD
546 ? DRV_HARD : 0) + drv;
548 if ((i = p - arg - !*(p - 1))) {
549 if ((size_t)i >= sizeof(kname))
551 memcpy(kname, arg, i + 1);
562 struct dos_partition *dp;
564 struct disklabel64 *d;
566 struct disklabel32 *d;
574 sec = boot2_dmadat->secbuf;
576 if (drvread(sec, DOSBBSECTOR, 1))
578 dp = (void *)(sec + DOSPARTOFF);
580 if (sl < BASE_SLICE) {
581 for (i = 0; i < NDOSPART; i++)
582 if (dp[i].dp_typ == DOSPTYP_386BSD &&
583 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
585 if (dp[i].dp_flag & 0x80 ||
586 dsk.slice == COMPATIBILITY_SLICE)
589 if (dsk.slice == WHOLE_DISK_SLICE)
592 if (sl != WHOLE_DISK_SLICE) {
593 if (sl != COMPATIBILITY_SLICE)
594 dp += sl - BASE_SLICE;
595 if (dp->dp_typ != DOSPTYP_386BSD) {
596 printf(INVALID_S, "slice");
599 dsk.start = dp->dp_start;
603 * Probe label and partition table
606 if (drvread(sec, dsk.start, (sizeof(struct disklabel64) + 511) / 512))
609 if (d->d_magic != DISKMAGIC64) {
610 printf(INVALID_S, "label");
613 if (dsk.part >= d->d_npartitions || d->d_partitions[dsk.part].p_bsize == 0) {
614 printf(INVALID_S, "partition");
617 dsk.start += d->d_partitions[dsk.part].p_boffset / 512;
620 if (drvread(sec, dsk.start + LABELSECTOR32, 1))
622 d = (void *)(sec + LABELOFFSET32);
623 if (d->d_magic != DISKMAGIC32 || d->d_magic2 != DISKMAGIC32) {
624 if (dsk.part != RAW_PART) {
625 printf(INVALID_S, "label");
630 if (d->d_type == DTYPE_SCSI)
634 if (dsk.part >= d->d_npartitions ||
635 !d->d_partitions[dsk.part].p_size) {
636 printf(INVALID_S, "partition");
639 dsk.start += d->d_partitions[dsk.part].p_offset;
640 dsk.start -= d->d_partitions[RAW_PART].p_offset;
646 #if defined(UFS) && defined(HAMMERFS)
647 if (boot2_hammer_api.fsinit() == 0) {
648 fsapi = &boot2_hammer_api;
649 } else if (boot2_ufs_api.fsinit() == 0) {
650 fsapi = &boot2_ufs_api;
652 printf("fs probe failed\n");
653 fsapi = &boot2_ufs_api;
658 return fsapi->fsinit();
664 * Read from the probed disk. We have established the slice and partition
668 dskread(void *buf, unsigned lba, unsigned nblk)
670 return drvread(buf, dsk.start + lba, nblk);
674 * boot encapsulated ABI
677 printf(const char *fmt,...)
686 while ((c = *fmt++)) {
691 putchar(va_arg(ap, int));
694 for (s = va_arg(ap, char *); *s; s++)
698 u = va_arg(ap, unsigned);
701 *s++ = '0' + u % 10U;
714 * boot encapsulated ABI
725 * boot encapsulated ABI
728 drvread(void *buf, unsigned lba, unsigned nblk)
730 static unsigned c = 0x2d5c7c2f; /* twiddle */
732 c = (c << 8) | (c >> 24);
735 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
736 v86.addr = XREADORG; /* call to xread in boot1 */
737 v86.es = VTOPSEG(buf);
739 v86.ebx = VTOPOFF(buf);
741 v86.edx = nblk << 8 | dsk.drive;
744 if (V86_CY(v86.efl)) {
745 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
752 keyhit(unsigned ticks)
756 if (opts & 1 << RBX_NOINTR)
762 t1 = *(uint32_t *)PTOV(0x46c);
765 if (t1 < t0 || t1 >= t0 + ticks)
773 if (opts & RBF_VIDEO)
775 if (opts & RBF_SERIAL)
782 if (opts & 1 << RBX_NOINTR)
785 if ((opts & RBF_VIDEO) && getc(1))
786 return fn ? 1 : getc(0);
787 if ((opts & RBF_SERIAL) && sio_ischar())
788 return fn ? 1 : sio_getc();
800 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);