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 $
50 #define AOUT_H_FORCE32
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>
63 #include <machine/psl.h>
78 #include "../bootasm.h"
80 #define SECOND 18 /* Circa that many ticks in a second. */
82 #define RBX_ASKNAME 0x0 /* -a */
83 #define RBX_SINGLE 0x1 /* -s */
84 #define RBX_DFLTROOT 0x5 /* -r */
85 #define RBX_KDB 0x6 /* -d */
86 #define RBX_CONFIG 0xa /* -c */
87 #define RBX_VERBOSE 0xb /* -v */
88 #define RBX_SERIAL 0xc /* -h */
89 #define RBX_CDROM 0xd /* -C */
90 #define RBX_GDB 0xf /* -g */
91 #define RBX_MUTE 0x10 /* -m */
92 #define RBX_PAUSE 0x12 /* -p */
93 #define RBX_NOINTR 0x1c /* -n */
94 #define RBX_VIDEO 0x1d /* -V */
95 #define RBX_PROBEKBD 0x1e /* -P */
96 /* 0x1f is reserved for the historical RB_BOOTINFO option */
98 #define RBF_MUTE (1 << RBX_MUTE)
99 #define RBF_SERIAL (1 << RBX_SERIAL)
100 #define RBF_VIDEO (1 << RBX_VIDEO)
101 #define RBF_NOINTR (1 << RBX_NOINTR)
102 #define RBF_PROBEKBD (1 << RBX_PROBEKBD)
104 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
105 #define RBX_MASK 0x2005ffff
107 #define PATH_CONFIG "/boot.config"
108 #define PATH_BOOT3 "/loader" /* /boot is dedicated */
109 #define PATH_BOOT3_ALT "/boot/loader" /* /boot in root */
110 #define PATH_KERNEL "/kernel"
114 #define MEM_BASE 0x12
116 #define V86_CY(x) ((x) & PSL_C)
117 #define V86_ZR(x) ((x) & PSL_Z)
119 #define DRV_HARD 0x80
120 #define DRV_MASK 0x7f
124 #define TYPE_MAXHARD TYPE_DA
127 #define INVALID_S "Bad %s\n"
129 extern uint32_t _end;
131 static const char optstr[NOPT] = { "VhaCgmnPprsv" };
132 static const unsigned char flags[NOPT] = {
147 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
148 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
160 static char cmd[512];
161 static const char *kname;
162 static uint32_t opts = RBF_VIDEO;
163 static struct bootinfo bootinfo;
166 * boot2 encapsulated ABI elements provided to *fsread.c
168 * NOTE: boot2_dmadat is extended by per-filesystem APIs
172 struct boot2_dmadat *boot2_dmadat;
175 static void load(void);
176 static int parse(void);
177 static int dskprobe(void);
178 static int xfsread(boot2_ino_t, void *, size_t);
179 static int drvread(void *, unsigned, unsigned);
180 static int keyhit(unsigned);
181 static int xputc(int);
182 static int xgetc(int);
183 static int getc(int);
186 memcpy(void *d, const void *s, int len)
196 strcmp(const char *s1, const char *s2)
198 for (; *s1 == *s2 && *s1; s1++, s2++)
200 return ((int)((unsigned char)*s1 - (unsigned char)*s2));
203 #if defined(UFS) && defined(HAMMERFS)
205 const struct boot2_fsapi *fsapi;
209 #define fsapi (&boot2_ufs_api)
211 #elif defined(HAMMERFS)
213 #define fsapi (&boot2_hammer_api)
218 xfsread(boot2_ino_t inode, void *buf, size_t nbyte)
220 if ((size_t)fsapi->fsread(inode, buf, nbyte) != nbyte) {
221 printf(INVALID_S, "format");
235 switch (c = xgetc(0)) {
250 if (s - cmd < sizeof(cmd) - 1)
261 v86.eax = 0xe00 | (c & 0xff);
274 (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
276 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
277 dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG);
278 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
279 dsk.unit = dsk.drive & DRV_MASK;
280 dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1;
281 bootinfo.bi_version = BOOTINFO_VERSION;
282 bootinfo.bi_size = sizeof(bootinfo);
287 * Probe the default disk and process the configuration file if
290 if (dskprobe() == 0) {
291 if ((ino = fsapi->fslookup(PATH_CONFIG)))
292 fsapi->fsread(ino, cmd, sizeof(cmd));
296 * Parse config file if present. parse() will re-probe if necessary.
299 printf("%s: %s", PATH_CONFIG, cmd);
302 /* Do not process this command twice */
307 * Setup our (serial) console after processing the config file. If
308 * the initialization fails, don't try to use the serial port. This
309 * can happen if the serial port is unmaped (happens on new laptops a lot).
311 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
312 opts |= RBF_SERIAL|RBF_VIDEO;
313 if (opts & RBF_SERIAL) {
320 * Try to exec stage 3 boot loader. If interrupted by a keypress,
321 * or in case of failure, try to load a kernel directly instead.
323 * We have to try boot /boot/loader and /loader to support booting
324 * from a /boot partition instead of a root partition.
326 if (autoboot && !kname) {
328 if (!keyhit(3*SECOND)) {
330 kname = PATH_BOOT3_ALT;
336 /* Present the user with the boot2 prompt. */
339 printf("\nDragonFly boot\n"
341 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
342 'a' + dsk.part, kname);
343 if (!autoboot || keyhit(5*SECOND))
355 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
368 static Elf32_Phdr ep[2];
369 static Elf32_Shdr es[2];
375 if (!(ino = fsapi->fslookup(kname))) {
377 printf("No %s\n", kname);
380 if (xfsread(ino, &hdr, sizeof(hdr)))
383 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
384 addr = hdr.ex.a_entry & 0xffffff;
387 if (xfsread(ino, p, hdr.ex.a_text))
389 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
390 if (xfsread(ino, p, hdr.ex.a_data))
392 } else if (IS_ELF(hdr.eh)) {
393 fs_off = hdr.eh.e_phoff;
394 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
395 if (xfsread(ino, ep + j, sizeof(ep[0])))
397 if (ep[j].p_type == PT_LOAD)
400 for (i = 0; i < 2; i++) {
401 p = PTOV(ep[i].p_paddr & 0xffffff);
402 fs_off = ep[i].p_offset;
403 if (xfsread(ino, p, ep[i].p_filesz))
406 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
407 bootinfo.bi_symtab = VTOP(p);
408 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
409 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
410 (hdr.eh.e_shstrndx + 1);
411 if (xfsread(ino, &es, sizeof(es)))
413 for (i = 0; i < 2; i++) {
414 *(Elf32_Word *)p = es[i].sh_size;
415 p += sizeof(es[i].sh_size);
416 fs_off = es[i].sh_offset;
417 if (xfsread(ino, p, es[i].sh_size))
422 addr = hdr.eh.e_entry & 0xffffff;
423 bootinfo.bi_esymtab = VTOP(p);
425 printf(INVALID_S, "format");
429 bootinfo.bi_kernelname = VTOP(kname);
430 bootinfo.bi_bios_dev = dsk.drive;
431 __exec((caddr_t)addr, opts & RBX_MASK,
432 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
433 0, 0, 0, VTOP(&bootinfo));
444 while ((c = *arg++)) {
445 if (c == ' ' || c == '\t' || c == '\n')
447 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
452 while ((c = *arg++)) {
453 for (i = NOPT - 1; i >= 0; --i) {
454 if (optstr[i] == c) {
455 opts ^= 1 << flags[i];
460 ok: ; /* ugly but save space */
462 if (opts & RBF_PROBEKBD) {
463 i = *(uint8_t *)PTOV(0x496) & 0x10;
466 opts |= RBF_VIDEO | RBF_SERIAL;
468 opts &= ~RBF_PROBEKBD;
471 for (q = arg--; *q && *q != '('; q++);
482 for (i = 0; arg[0] != dev_nm[i][0] ||
483 arg[1] != dev_nm[i][1]; i++)
488 dsk.unit = *arg - '0';
489 if (arg[1] != ',' || dsk.unit > 9)
492 dsk.slice = WHOLE_DISK_SLICE;
494 dsk.slice = *arg - '0' + 1;
495 if (dsk.slice > NDOSPART + 1)
501 dsk.part = *arg - 'a';
507 dsk.drive = (dsk.type <= TYPE_MAXHARD
508 ? DRV_HARD : 0) + drv;
520 struct dos_partition *dp;
522 struct disklabel64 *d;
524 struct disklabel32 *d;
533 sec = boot2_dmadat->secbuf;
535 if (drvread(sec, DOSBBSECTOR, 1))
537 dp = (void *)(sec + DOSPARTOFF);
539 if (sl < BASE_SLICE) {
540 for (i = 0; i < NDOSPART; i++)
541 if (dp[i].dp_typ == DOSPTYP_386BSD &&
542 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
544 if (dp[i].dp_flag & 0x80 ||
545 dsk.slice == COMPATIBILITY_SLICE)
548 if (dsk.slice == WHOLE_DISK_SLICE)
551 if (sl != WHOLE_DISK_SLICE) {
552 if (sl != COMPATIBILITY_SLICE)
553 dp += sl - BASE_SLICE;
554 if (dp->dp_typ != DOSPTYP_386BSD) {
555 printf(INVALID_S, "slice");
558 dsk.start = dp->dp_start;
562 * Probe label and partition table
565 if (drvread(sec, dsk.start, (sizeof(struct disklabel64) + 511) / 512))
568 if (d->d_magic != DISKMAGIC64) {
569 printf(INVALID_S, "label");
572 if (dsk.part >= d->d_npartitions || d->d_partitions[dsk.part].p_bsize == 0) {
573 printf(INVALID_S, "partition");
576 dsk.start += d->d_partitions[dsk.part].p_boffset / 512;
579 if (drvread(sec, dsk.start + LABELSECTOR32, 1))
581 d = (void *)(sec + LABELOFFSET32);
582 if (d->d_magic != DISKMAGIC32 || d->d_magic2 != DISKMAGIC32) {
583 if (dsk.part != RAW_PART) {
584 printf(INVALID_S, "label");
589 if (d->d_type == DTYPE_SCSI)
593 if (dsk.part >= d->d_npartitions ||
594 !d->d_partitions[dsk.part].p_size) {
595 printf(INVALID_S, "partition");
598 dsk.start += d->d_partitions[dsk.part].p_offset;
599 dsk.start -= d->d_partitions[RAW_PART].p_offset;
605 #if defined(UFS) && defined(HAMMERFS)
606 if (boot2_hammer_api.fsinit() == 0) {
607 fsapi = &boot2_hammer_api;
608 } else if (boot2_ufs_api.fsinit() == 0) {
609 fsapi = &boot2_ufs_api;
611 printf("fs probe failed\n");
612 fsapi = &boot2_ufs_api;
617 return fsapi->fsinit();
623 * Read from the probed disk. We have established the slice and partition
627 dskread(void *buf, unsigned lba, unsigned nblk)
629 return drvread(buf, dsk.start + lba, nblk);
633 * boot encapsulated ABI
636 printf(const char *fmt,...)
645 while ((c = *fmt++)) {
650 putchar(va_arg(ap, int));
653 for (s = va_arg(ap, char *); *s; s++)
657 u = va_arg(ap, unsigned);
660 *s++ = '0' + u % 10U;
674 * boot encapsulated ABI
685 * boot encapsulated ABI
688 drvread(void *buf, unsigned lba, unsigned nblk)
690 static unsigned c = 0x2d5c7c2f; /* twiddle */
692 c = (c << 8) | (c >> 24);
695 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
696 v86.addr = XREADORG; /* call to xread in boot1 */
697 v86.es = VTOPSEG(buf);
699 v86.ebx = VTOPOFF(buf);
701 v86.edx = nblk << 8 | dsk.drive;
704 if (V86_CY(v86.efl)) {
705 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
712 keyhit(unsigned ticks)
716 if (opts & RBF_NOINTR)
722 t1 = *(uint32_t *)PTOV(0x46c);
725 if ((uint32_t)(t1 - t0) >= ticks)
733 if (opts & RBF_VIDEO)
735 if (opts & RBF_SERIAL)
746 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
752 if (opts & RBF_NOINTR)
755 if ((opts & RBF_VIDEO) && getc(1))
756 return fn ? 1 : getc(0);
757 if ((opts & RBF_SERIAL) && sio_ischar())
758 return fn ? 1 : sio_getc();