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.14 2005/05/25 06:22:22 y0netan1 Exp $
50 #include <sys/param.h>
51 #include <sys/disklabel.h>
52 #include <sys/diskslice.h>
53 #include <sys/diskmbr.h>
54 #include <sys/dirent.h>
55 #include <machine/bootinfo.h>
56 #include <machine/elf.h>
66 #include "../bootasm.h"
68 #define SECOND 18 /* Circa that many ticks in a second. */
70 #define RBX_ASKNAME 0x0 /* -a */
71 #define RBX_SINGLE 0x1 /* -s */
72 #define RBX_DFLTROOT 0x5 /* -r */
73 #define RBX_KDB 0x6 /* -d */
74 #define RBX_CONFIG 0xa /* -c */
75 #define RBX_VERBOSE 0xb /* -v */
76 #define RBX_SERIAL 0xc /* -h */
77 #define RBX_CDROM 0xd /* -C */
78 #define RBX_GDB 0xf /* -g */
79 #define RBX_MUTE 0x10 /* -m */
80 #define RBX_PAUSE 0x12 /* -p */
81 #define RBX_NOINTR 0x1c /* -n */
82 #define RBX_VIDEO 0x1d /* -V */
83 #define RBX_PROBEKBD 0x1e /* -P */
84 /* 0x1f is reserved for the historical RB_BOOTINFO option */
86 #define RBF_MUTE (1 << RBX_MUTE)
87 #define RBF_SERIAL (1 << RBX_SERIAL)
88 #define RBF_VIDEO (1 << RBX_VIDEO)
90 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
91 #define RBX_MASK 0x2005ffff
93 #define PATH_CONFIG "/boot.config"
94 #define PATH_BOOT3 "/boot/loader"
95 #define PATH_KERNEL "/kernel"
100 #define V86_CY(x) ((x) & 1)
101 #define V86_ZR(x) ((x) & 0x40)
103 #define DRV_HARD 0x80
104 #define DRV_MASK 0x7f
108 #define TYPE_MAXHARD TYPE_DA
113 #define INVALID_S "Bad %s\n"
115 extern uint32_t _end;
117 static const char optstr[NOPT] = { "VhaCgmnPprsv" };
118 static const unsigned char flags[NOPT] = {
133 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
134 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
145 static char cmd[512];
146 static char kname[1024];
147 static uint32_t opts;
148 static struct bootinfo bootinfo;
151 static void load(void);
152 static int parse(void);
153 static int xfsread(ino_t, void *, size_t);
154 static int dskread(void *, unsigned, unsigned);
155 static void printf(const char *,...);
156 static void putchar(int);
157 static uint32_t memsize(void);
158 static int drvread(void *, unsigned, unsigned);
159 static int keyhit(unsigned);
160 static void xputc(int);
161 static int xgetc(int);
162 static int getc(int);
165 memcpy(void *d, const void *s, int len)
184 strcmp(const char *s1, const char *s2)
186 for (; *s1 == *s2 && *s1; s1++, s2++);
187 return (unsigned char)*s1 - (unsigned char)*s2;
193 xfsread(ino_t inode, void *buf, size_t nbyte)
195 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
196 printf(INVALID_S, "format");
202 static inline uint32_t
219 switch (c = xgetc(0)) {
234 if (s - cmd < sizeof(cmd) - 1)
245 v86.eax = 0xe00 | (c & 0xff);
256 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
258 dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG);
259 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
260 dsk.unit = dsk.drive & DRV_MASK;
261 dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1;
262 bootinfo.bi_version = BOOTINFO_VERSION;
263 bootinfo.bi_size = sizeof(bootinfo);
264 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
265 bootinfo.bi_extmem = memsize();
266 bootinfo.bi_memsizes_valid++;
268 /* Process configuration file */
272 if ((ino = lookup(PATH_CONFIG)))
273 fsread(ino, cmd, sizeof(cmd));
276 printf("%s: %s", PATH_CONFIG, cmd);
279 /* Do not process this command twice */
284 * Setup our (serial) console after processing the config file. If
285 * the initialization fails, don't try to use the serial port. This
286 * can happen if the serial port is unmaped (happens on new laptops a lot).
288 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
289 opts |= RBF_SERIAL|RBF_VIDEO;
290 if (opts & RBF_SERIAL) {
297 * Try to exec stage 3 boot loader. If interrupted by a keypress,
298 * or in case of failure, try to load a kernel directly instead.
301 if (autoboot && !*kname) {
302 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
303 if (!keyhit(3*SECOND)) {
305 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
309 /* Present the user with the boot2 prompt. */
312 printf("\nDragonFly boot\n"
314 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
315 'a' + dsk.part, kname);
316 if (!autoboot || keyhit(5*SECOND))
328 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
348 if (!(ino = lookup(kname))) {
350 printf("No %s\n", kname);
353 if (xfsread(ino, &hdr, sizeof(hdr)))
355 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
357 else if (IS_ELF(hdr.eh))
360 printf(INVALID_S, "format");
364 addr = hdr.ex.a_entry & 0xffffff;
367 if (xfsread(ino, p, hdr.ex.a_text))
369 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
370 if (xfsread(ino, p, hdr.ex.a_data))
372 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
373 bootinfo.bi_symtab = VTOP(p);
374 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
375 p += sizeof(hdr.ex.a_syms);
377 if (xfsread(ino, p, hdr.ex.a_syms))
380 if (xfsread(ino, p, sizeof(int)))
385 if (xfsread(ino, p, x))
390 fs_off = hdr.eh.e_phoff;
391 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
392 if (xfsread(ino, ep + j, sizeof(ep[0])))
394 if (ep[j].p_type == PT_LOAD)
397 for (i = 0; i < 2; i++) {
398 p = PTOV(ep[i].p_paddr & 0xffffff);
399 fs_off = ep[i].p_offset;
400 if (xfsread(ino, p, ep[i].p_filesz))
403 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
404 bootinfo.bi_symtab = VTOP(p);
405 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
406 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
407 (hdr.eh.e_shstrndx + 1);
408 if (xfsread(ino, &es, sizeof(es)))
410 for (i = 0; i < 2; i++) {
411 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
412 p += sizeof(es[i].sh_size);
413 fs_off = es[i].sh_offset;
414 if (xfsread(ino, p, es[i].sh_size))
419 addr = hdr.eh.e_entry & 0xffffff;
421 bootinfo.bi_esymtab = VTOP(p);
422 bootinfo.bi_kernelname = VTOP(kname);
423 bootinfo.bi_bios_dev = dsk.drive;
424 __exec((caddr_t)addr, opts & RBX_MASK,
425 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
426 0, 0, 0, VTOP(&bootinfo));
437 while ((c = *arg++)) {
438 if (c == ' ' || c == '\t' || c == '\n')
440 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
445 while ((c = *arg++)) {
446 for (i = NOPT - 1; i >= 0; --i) {
447 if (optstr[i] == c) {
448 opts ^= 1 << flags[i];
453 ok: ; /* ugly but save space */
455 if (opts & (1 << RBX_PROBEKBD)) {
456 i = *(uint8_t *)PTOV(0x496) & 0x10;
459 opts |= RBF_VIDEO | RBF_SERIAL;
461 opts &= ~(1 << RBX_PROBEKBD);
464 for (q = arg--; *q && *q != '('; q++);
475 for (i = 0; arg[0] != dev_nm[i][0] ||
476 arg[1] != dev_nm[i][1]; i++)
481 dsk.unit = *arg - '0';
482 if (arg[1] != ',' || dsk.unit > 9)
485 dsk.slice = WHOLE_DISK_SLICE;
487 dsk.slice = *arg - '0' + 1;
488 if (dsk.slice > NDOSPART)
494 dsk.part = *arg - 'a';
500 dsk.drive = (dsk.type <= TYPE_MAXHARD
501 ? DRV_HARD : 0) + drv;
504 if ((i = p - arg - !*(p - 1))) {
505 if ((size_t)i >= sizeof(kname))
507 memcpy(kname, arg, i + 1);
516 dskread(void *buf, unsigned lba, unsigned nblk)
518 struct dos_partition *dp;
524 sec = dmadat->secbuf;
526 if (drvread(sec, DOSBBSECTOR, 1))
528 dp = (void *)(sec + DOSPARTOFF);
530 if (sl < BASE_SLICE) {
531 for (i = 0; i < NDOSPART; i++)
532 if (dp[i].dp_typ == DOSPTYP_386BSD &&
533 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
535 if (dp[i].dp_flag & 0x80 ||
536 dsk.slice == COMPATIBILITY_SLICE)
539 if (dsk.slice == WHOLE_DISK_SLICE)
542 if (sl != WHOLE_DISK_SLICE) {
543 if (sl != COMPATIBILITY_SLICE)
544 dp += sl - BASE_SLICE;
545 if (dp->dp_typ != DOSPTYP_386BSD) {
546 printf(INVALID_S, "slice");
549 dsk.start = dp->dp_start;
551 if (drvread(sec, dsk.start + LABELSECTOR, 1))
553 d = (void *)(sec + LABELOFFSET);
554 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
555 if (dsk.part != RAW_PART) {
556 printf(INVALID_S, "label");
561 if (d->d_type == DTYPE_SCSI)
565 if (dsk.part >= d->d_npartitions ||
566 !d->d_partitions[dsk.part].p_size) {
567 printf(INVALID_S, "partition");
570 dsk.start += d->d_partitions[dsk.part].p_offset;
571 dsk.start -= d->d_partitions[RAW_PART].p_offset;
574 return drvread(buf, dsk.start + lba, nblk);
578 printf(const char *fmt,...)
587 while ((c = *fmt++)) {
592 putchar(va_arg(ap, int));
595 for (s = va_arg(ap, char *); *s; s++)
599 u = va_arg(ap, unsigned);
602 *s++ = '0' + u % 10U;
624 drvread(void *buf, unsigned lba, unsigned nblk)
626 static unsigned c = 0x2d5c7c2f; /* twiddle */
628 c = (c << 8) | (c >> 24);
631 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
632 v86.addr = XREADORG; /* call to xread in boot1 */
633 v86.es = VTOPSEG(buf);
635 v86.ebx = VTOPOFF(buf);
637 v86.edx = nblk << 8 | dsk.drive;
640 if (V86_CY(v86.efl)) {
641 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
648 keyhit(unsigned ticks)
652 if (opts & 1 << RBX_NOINTR)
658 t1 = *(uint32_t *)PTOV(0x46c);
661 if (t1 < t0 || t1 >= t0 + ticks)
669 if (opts & RBF_VIDEO)
671 if (opts & RBF_SERIAL)
678 if (opts & 1 << RBX_NOINTR)
681 if ((opts & RBF_VIDEO) && getc(1))
682 return fn ? 1 : getc(0);
683 if ((opts & RBF_SERIAL) && sio_ischar())
684 return fn ? 1 : sio_getc();
696 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);