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 $
54 #include <sys/param.h>
55 #include <sys/disklabel32.h>
56 #include <sys/diskslice.h>
57 #include <sys/diskmbr.h>
58 #include <sys/dtype.h>
59 #include <sys/dirent.h>
60 #include <machine/bootinfo.h>
61 #include <machine/elf.h>
71 #include "../bootasm.h"
73 #define SECOND 18 /* Circa that many ticks in a second. */
75 #define RBX_ASKNAME 0x0 /* -a */
76 #define RBX_SINGLE 0x1 /* -s */
77 #define RBX_DFLTROOT 0x5 /* -r */
78 #define RBX_KDB 0x6 /* -d */
79 #define RBX_CONFIG 0xa /* -c */
80 #define RBX_VERBOSE 0xb /* -v */
81 #define RBX_SERIAL 0xc /* -h */
82 #define RBX_CDROM 0xd /* -C */
83 #define RBX_GDB 0xf /* -g */
84 #define RBX_MUTE 0x10 /* -m */
85 #define RBX_PAUSE 0x12 /* -p */
86 #define RBX_NOINTR 0x1c /* -n */
87 #define RBX_VIDEO 0x1d /* -V */
88 #define RBX_PROBEKBD 0x1e /* -P */
89 /* 0x1f is reserved for the historical RB_BOOTINFO option */
91 #define RBF_MUTE (1 << RBX_MUTE)
92 #define RBF_SERIAL (1 << RBX_SERIAL)
93 #define RBF_VIDEO (1 << RBX_VIDEO)
95 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
96 #define RBX_MASK 0x2005ffff
98 #define PATH_CONFIG "/boot.config"
99 #define PATH_BOOT3 "/boot/loader" /* /boot in root */
100 #define PATH_BOOT3_ALT "/loader" /* /boot partition */
101 #define PATH_KERNEL "/kernel"
104 #define MEM_BASE 0x12
106 #define V86_CY(x) ((x) & 1)
107 #define V86_ZR(x) ((x) & 0x40)
109 #define DRV_HARD 0x80
110 #define DRV_MASK 0x7f
114 #define TYPE_MAXHARD TYPE_DA
119 #define INVALID_S "Bad %s\n"
121 extern uint32_t _end;
123 static const char optstr[NOPT] = { "VhaCgmnPprsv" };
124 static const unsigned char flags[NOPT] = {
139 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
140 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
151 static char cmd[512];
152 static char kname[1024];
153 static uint32_t opts;
154 static struct bootinfo bootinfo;
156 static int ls, dsk_meta;
157 static uint32_t fs_off;
160 static void load(void);
161 static int parse(void);
162 static int xfsread(ino_t, void *, size_t);
163 static int dskread(void *, unsigned, unsigned);
164 static void printf(const char *,...);
165 static void putchar(int);
166 static uint32_t memsize(void);
167 static int drvread(void *, unsigned, unsigned);
168 static int keyhit(unsigned);
169 static void xputc(int);
170 static int xgetc(int);
171 static int getc(int);
174 memcpy(void *d, const void *s, int len)
193 strcmp(const char *s1, const char *s2)
195 for (; *s1 == *s2 && *s1; s1++, s2++);
196 return (unsigned char)*s1 - (unsigned char)*s2;
200 #include "hammerread.c"
206 xfsread(ino_t inode, void *buf, size_t nbyte)
208 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
209 printf(INVALID_S, "format");
215 static inline uint32_t
232 switch (c = xgetc(0)) {
247 if (s - cmd < sizeof(cmd) - 1)
258 v86.eax = 0xe00 | (c & 0xff);
269 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
271 dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG);
272 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
273 dsk.unit = dsk.drive & DRV_MASK;
274 dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1;
275 bootinfo.bi_version = BOOTINFO_VERSION;
276 bootinfo.bi_size = sizeof(bootinfo);
277 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
278 bootinfo.bi_extmem = memsize();
279 bootinfo.bi_memsizes_valid++;
281 /* Process configuration file */
285 if ((ino = lookup(PATH_CONFIG)))
286 fsread(ino, cmd, sizeof(cmd));
289 printf("%s: %s", PATH_CONFIG, cmd);
292 /* Do not process this command twice */
297 * Setup our (serial) console after processing the config file. If
298 * the initialization fails, don't try to use the serial port. This
299 * can happen if the serial port is unmaped (happens on new laptops a lot).
301 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
302 opts |= RBF_SERIAL|RBF_VIDEO;
303 if (opts & RBF_SERIAL) {
310 * Try to exec stage 3 boot loader. If interrupted by a keypress,
311 * or in case of failure, try to load a kernel directly instead.
313 * We have to try boot /boot/loader and /loader to support booting
314 * from a /boot partition instead of a root partition.
316 if (autoboot && !*kname) {
317 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
318 if (!keyhit(3*SECOND)) {
320 memcpy(kname, PATH_BOOT3_ALT, sizeof(PATH_BOOT3_ALT));
322 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
326 /* Present the user with the boot2 prompt. */
329 printf("\nDragonFly boot\n"
331 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
332 'a' + dsk.part, kname);
333 if (!autoboot || keyhit(5*SECOND))
345 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
365 if (!(ino = lookup(kname))) {
367 printf("No %s\n", kname);
370 if (xfsread(ino, &hdr, sizeof(hdr)))
372 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
374 else if (IS_ELF(hdr.eh))
377 printf(INVALID_S, "format");
381 addr = hdr.ex.a_entry & 0xffffff;
384 if (xfsread(ino, p, hdr.ex.a_text))
386 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
387 if (xfsread(ino, p, hdr.ex.a_data))
389 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
390 bootinfo.bi_symtab = VTOP(p);
391 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
392 p += sizeof(hdr.ex.a_syms);
394 if (xfsread(ino, p, hdr.ex.a_syms))
397 if (xfsread(ino, p, sizeof(int)))
402 if (xfsread(ino, p, x))
407 fs_off = hdr.eh.e_phoff;
408 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
409 if (xfsread(ino, ep + j, sizeof(ep[0])))
411 if (ep[j].p_type == PT_LOAD)
414 for (i = 0; i < 2; i++) {
415 p = PTOV(ep[i].p_paddr & 0xffffff);
416 fs_off = ep[i].p_offset;
417 if (xfsread(ino, p, ep[i].p_filesz))
420 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
421 bootinfo.bi_symtab = VTOP(p);
422 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
423 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
424 (hdr.eh.e_shstrndx + 1);
425 if (xfsread(ino, &es, sizeof(es)))
427 for (i = 0; i < 2; i++) {
428 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
429 p += sizeof(es[i].sh_size);
430 fs_off = es[i].sh_offset;
431 if (xfsread(ino, p, es[i].sh_size))
436 addr = hdr.eh.e_entry & 0xffffff;
438 bootinfo.bi_esymtab = VTOP(p);
439 bootinfo.bi_kernelname = VTOP(kname);
440 bootinfo.bi_bios_dev = dsk.drive;
441 __exec((caddr_t)addr, opts & RBX_MASK,
442 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
443 0, 0, 0, VTOP(&bootinfo));
454 while ((c = *arg++)) {
455 if (c == ' ' || c == '\t' || c == '\n')
457 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
462 while ((c = *arg++)) {
463 for (i = NOPT - 1; i >= 0; --i) {
464 if (optstr[i] == c) {
465 opts ^= 1 << flags[i];
470 ok: ; /* ugly but save space */
472 if (opts & (1 << RBX_PROBEKBD)) {
473 i = *(uint8_t *)PTOV(0x496) & 0x10;
476 opts |= RBF_VIDEO | RBF_SERIAL;
478 opts &= ~(1 << RBX_PROBEKBD);
481 for (q = arg--; *q && *q != '('; q++);
492 for (i = 0; arg[0] != dev_nm[i][0] ||
493 arg[1] != dev_nm[i][1]; i++)
498 dsk.unit = *arg - '0';
499 if (arg[1] != ',' || dsk.unit > 9)
502 dsk.slice = WHOLE_DISK_SLICE;
504 dsk.slice = *arg - '0' + 1;
505 if (dsk.slice > NDOSPART)
511 dsk.part = *arg - 'a';
517 dsk.drive = (dsk.type <= TYPE_MAXHARD
518 ? DRV_HARD : 0) + drv;
521 if ((i = p - arg - !*(p - 1))) {
522 if ((size_t)i >= sizeof(kname))
524 memcpy(kname, arg, i + 1);
533 dskread(void *buf, unsigned lba, unsigned nblk)
535 struct dos_partition *dp;
536 struct disklabel32 *d;
541 sec = dmadat->secbuf;
543 if (drvread(sec, DOSBBSECTOR, 1))
545 dp = (void *)(sec + DOSPARTOFF);
547 if (sl < BASE_SLICE) {
548 for (i = 0; i < NDOSPART; i++)
549 if (dp[i].dp_typ == DOSPTYP_386BSD &&
550 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
552 if (dp[i].dp_flag & 0x80 ||
553 dsk.slice == COMPATIBILITY_SLICE)
556 if (dsk.slice == WHOLE_DISK_SLICE)
559 if (sl != WHOLE_DISK_SLICE) {
560 if (sl != COMPATIBILITY_SLICE)
561 dp += sl - BASE_SLICE;
562 if (dp->dp_typ != DOSPTYP_386BSD) {
563 printf(INVALID_S, "slice");
566 dsk.start = dp->dp_start;
568 if (drvread(sec, dsk.start + LABELSECTOR32, 1))
570 d = (void *)(sec + LABELOFFSET32);
571 if (d->d_magic != DISKMAGIC32 || d->d_magic2 != DISKMAGIC32) {
572 if (dsk.part != RAW_PART) {
573 printf(INVALID_S, "label");
578 if (d->d_type == DTYPE_SCSI)
582 if (dsk.part >= d->d_npartitions ||
583 !d->d_partitions[dsk.part].p_size) {
584 printf(INVALID_S, "partition");
587 dsk.start += d->d_partitions[dsk.part].p_offset;
588 dsk.start -= d->d_partitions[RAW_PART].p_offset;
591 return drvread(buf, dsk.start + lba, nblk);
595 printf(const char *fmt,...)
604 while ((c = *fmt++)) {
609 putchar(va_arg(ap, int));
612 for (s = va_arg(ap, char *); *s; s++)
616 u = va_arg(ap, unsigned);
619 *s++ = '0' + u % 10U;
641 drvread(void *buf, unsigned lba, unsigned nblk)
643 static unsigned c = 0x2d5c7c2f; /* twiddle */
645 c = (c << 8) | (c >> 24);
648 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
649 v86.addr = XREADORG; /* call to xread in boot1 */
650 v86.es = VTOPSEG(buf);
652 v86.ebx = VTOPOFF(buf);
654 v86.edx = nblk << 8 | dsk.drive;
657 if (V86_CY(v86.efl)) {
658 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
665 keyhit(unsigned ticks)
669 if (opts & 1 << RBX_NOINTR)
675 t1 = *(uint32_t *)PTOV(0x46c);
678 if (t1 < t0 || t1 >= t0 + ticks)
686 if (opts & RBF_VIDEO)
688 if (opts & RBF_SERIAL)
695 if (opts & 1 << RBX_NOINTR)
698 if ((opts & RBF_VIDEO) && getc(1))
699 return fn ? 1 : getc(0);
700 if ((opts & RBF_SERIAL) && sio_ischar())
701 return fn ? 1 : sio_getc();
713 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);