2 * Copyright (c) 1998 Robert Nordier
5 * Redistribution and use in source and binary forms are freely
6 * permitted provided that the above copyright notice and this
7 * paragraph and the following disclaimer are duplicated in all
10 * This software is provided "AS IS" and without any express or
11 * implied warranties, including, without limitation, the implied
12 * warranties of merchantability and fitness for a particular
15 * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $
16 * $DragonFly: src/sys/boot/i386/boot2/Attic/boot2.c,v 1.10 2004/06/27 08:00:46 dillon Exp $
18 #include <sys/param.h>
19 #include <sys/disklabel.h>
20 #include <sys/diskslice.h>
21 #include <sys/diskmbr.h>
22 #include <sys/dirent.h>
23 #include <machine/bootinfo.h>
24 #include <machine/elf.h>
35 #define SECOND 18 /* Circa that many ticks in a second. */
37 #define RBX_ASKNAME 0x0 /* -a */
38 #define RBX_SINGLE 0x1 /* -s */
39 #define RBX_DFLTROOT 0x5 /* -r */
40 #define RBX_KDB 0x6 /* -d */
41 #define RBX_CONFIG 0xa /* -c */
42 #define RBX_VERBOSE 0xb /* -v */
43 #define RBX_SERIAL 0xc /* -h */
44 #define RBX_CDROM 0xd /* -C */
45 #define RBX_GDB 0xf /* -g */
46 #define RBX_MUTE 0x10 /* -m */
47 #define RBX_PAUSE 0x12 /* -p */
48 #define RBX_NOINTR 0x1c /* -n */
49 #define RBX_VIDEO 0x1d /* -V */
50 #define RBX_PROBEKBD 0x1e /* -P */
51 /* 0x1f is reserved for the historical RB_BOOTINFO option */
53 #define RBF_MUTE (1 << RBX_MUTE)
54 #define RBF_SERIAL (1 << RBX_SERIAL)
55 #define RBF_VIDEO (1 << RBX_VIDEO)
57 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
58 #define RBX_MASK 0x2005ffff
60 #define PATH_CONFIG "/boot.config"
61 #define PATH_BOOT3 "/boot/loader"
62 #define PATH_KERNEL "/kernel"
68 #define V86_CY(x) ((x) & 1)
69 #define V86_ZR(x) ((x) & 0x40)
76 #define TYPE_MAXHARD TYPE_DA
83 static const char optstr[NOPT] = { "VhaCgmnPprsv" };
84 static const unsigned char flags[NOPT] = {
99 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
100 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
111 static char cmd[512];
112 static char kname[1024];
113 static uint32_t opts;
114 static struct bootinfo bootinfo;
117 static void load(void);
118 static int parse(void);
119 static int xfsread(ino_t, void *, size_t);
120 static int dskread(void *, unsigned, unsigned);
121 static void printf(const char *,...);
122 static void putchar(int);
123 static uint32_t memsize(void);
124 static int drvread(void *, unsigned, unsigned);
125 static int keyhit(unsigned);
126 static void xputc(int);
127 static int xgetc(int);
128 static int getc(int);
131 memcpy(void *d, const void *s, int len)
150 strcmp(const char *s1, const char *s2)
152 for (; *s1 == *s2 && *s1; s1++, s2++);
153 return (unsigned char)*s1 - (unsigned char)*s2;
159 xfsread(ino_t inode, void *buf, size_t nbyte)
161 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
162 printf("Invalid %s\n", "format");
168 static inline uint32_t
185 switch (c = xgetc(0)) {
200 if (s - cmd < sizeof(cmd) - 1)
211 v86.eax = 0xe00 | (c & 0xff);
222 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
224 dsk.drive = *(uint8_t *)PTOV(ARGS);
225 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
226 dsk.unit = dsk.drive & DRV_MASK;
227 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
228 bootinfo.bi_version = BOOTINFO_VERSION;
229 bootinfo.bi_size = sizeof(bootinfo);
230 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
231 bootinfo.bi_extmem = memsize();
232 bootinfo.bi_memsizes_valid++;
234 /* Process configuration file */
238 if ((ino = lookup(PATH_CONFIG)))
239 fsread(ino, cmd, sizeof(cmd));
242 printf("%s: %s", PATH_CONFIG, cmd);
245 /* Do not process this command twice */
250 * Setup our (serial) console after processing the config file. If
251 * the initialization fails, don't try to use the serial port. This
252 * can happen if the serial port is unmaped (happens on new laptops a lot).
254 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
255 opts |= RBF_SERIAL|RBF_VIDEO;
256 if (opts & RBF_SERIAL) {
263 * Try to exec stage 3 boot loader. If interrupted by a keypress,
264 * or in case of failure, try to load a kernel directly instead.
267 if (autoboot && !*kname) {
268 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
269 if (!keyhit(3*SECOND)) {
271 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
275 /* Present the user with the boot2 prompt. */
278 printf("\nDragonFly boot\n"
280 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
281 'a' + dsk.part, kname);
282 if (!autoboot || keyhit(5*SECOND))
287 if (cmd[0] == 0 || parse())
294 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
314 if (!(ino = lookup(kname))) {
316 printf("No %s\n", kname);
319 if (xfsread(ino, &hdr, sizeof(hdr)))
321 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
323 else if (IS_ELF(hdr.eh))
326 printf("Invalid %s\n", "format");
330 addr = hdr.ex.a_entry & 0xffffff;
333 if (xfsread(ino, p, hdr.ex.a_text))
335 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
336 if (xfsread(ino, p, hdr.ex.a_data))
338 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
339 bootinfo.bi_symtab = VTOP(p);
340 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
341 p += sizeof(hdr.ex.a_syms);
343 if (xfsread(ino, p, hdr.ex.a_syms))
346 if (xfsread(ino, p, sizeof(int)))
351 if (xfsread(ino, p, x))
356 fs_off = hdr.eh.e_phoff;
357 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
358 if (xfsread(ino, ep + j, sizeof(ep[0])))
360 if (ep[j].p_type == PT_LOAD)
363 for (i = 0; i < 2; i++) {
364 p = PTOV(ep[i].p_paddr & 0xffffff);
365 fs_off = ep[i].p_offset;
366 if (xfsread(ino, p, ep[i].p_filesz))
369 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
370 bootinfo.bi_symtab = VTOP(p);
371 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
372 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
373 (hdr.eh.e_shstrndx + 1);
374 if (xfsread(ino, &es, sizeof(es)))
376 for (i = 0; i < 2; i++) {
377 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
378 p += sizeof(es[i].sh_size);
379 fs_off = es[i].sh_offset;
380 if (xfsread(ino, p, es[i].sh_size))
385 addr = hdr.eh.e_entry & 0xffffff;
387 bootinfo.bi_esymtab = VTOP(p);
388 bootinfo.bi_kernelname = VTOP(kname);
389 bootinfo.bi_bios_dev = dsk.drive;
390 __exec((caddr_t)addr, opts & RBX_MASK,
391 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
392 0, 0, 0, VTOP(&bootinfo));
403 while ((c = *arg++)) {
404 if (c == ' ' || c == '\t' || c == '\n')
406 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
411 while ((c = *arg++)) {
412 for (i = NOPT - 1; i >= 0; --i) {
413 if (optstr[i] == c) {
414 opts ^= 1 << flags[i];
419 ok: ; /* ugly but save space */
421 if (opts & (1 << RBX_PROBEKBD)) {
422 i = *(uint8_t *)PTOV(0x496) & 0x10;
425 opts |= RBF_VIDEO | RBF_SERIAL;
427 opts &= ~(1 << RBX_PROBEKBD);
430 for (q = arg--; *q && *q != '('; q++);
441 for (i = 0; arg[0] != dev_nm[i][0] ||
442 arg[1] != dev_nm[i][1]; i++)
447 dsk.unit = *arg - '0';
448 if (arg[1] != ',' || dsk.unit > 9)
451 dsk.slice = WHOLE_DISK_SLICE;
453 dsk.slice = *arg - '0' + 1;
454 if (dsk.slice > NDOSPART)
460 dsk.part = *arg - 'a';
466 dsk.drive = (dsk.type <= TYPE_MAXHARD
467 ? DRV_HARD : 0) + drv;
470 if ((i = p - arg - !*(p - 1))) {
471 if ((size_t)i >= sizeof(kname))
473 memcpy(kname, arg, i + 1);
482 dskread(void *buf, unsigned lba, unsigned nblk)
484 struct dos_partition *dp;
490 sec = dmadat->secbuf;
492 if (drvread(sec, DOSBBSECTOR, 1))
494 dp = (void *)(sec + DOSPARTOFF);
496 if (sl < BASE_SLICE) {
497 for (i = 0; i < NDOSPART; i++)
498 if (dp[i].dp_typ == DOSPTYP_386BSD &&
499 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
501 if (dp[i].dp_flag & 0x80 ||
502 dsk.slice == COMPATIBILITY_SLICE)
505 if (dsk.slice == WHOLE_DISK_SLICE)
508 if (sl != WHOLE_DISK_SLICE) {
509 if (sl != COMPATIBILITY_SLICE)
510 dp += sl - BASE_SLICE;
511 if (dp->dp_typ != DOSPTYP_386BSD) {
512 printf("Invalid %s\n", "slice");
515 dsk.start = dp->dp_start;
517 if (drvread(sec, dsk.start + LABELSECTOR, 1))
519 d = (void *)(sec + LABELOFFSET);
520 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
521 if (dsk.part != RAW_PART) {
522 printf("Invalid %s\n", "label");
527 if (d->d_type == DTYPE_SCSI)
531 if (dsk.part >= d->d_npartitions ||
532 !d->d_partitions[dsk.part].p_size) {
533 printf("Invalid %s\n", "partition");
536 dsk.start += d->d_partitions[dsk.part].p_offset;
537 dsk.start -= d->d_partitions[RAW_PART].p_offset;
540 return drvread(buf, dsk.start + lba, nblk);
544 printf(const char *fmt,...)
553 while ((c = *fmt++)) {
558 putchar(va_arg(ap, int));
561 for (s = va_arg(ap, char *); *s; s++)
565 u = va_arg(ap, unsigned);
568 *s++ = '0' + u % 10U;
590 drvread(void *buf, unsigned lba, unsigned nblk)
592 static unsigned c = 0x2d5c7c2f; /* twiddle */
594 c = (c << 8) | (c >> 24);
597 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
598 v86.addr = XREADORG; /* call to xread in boot1 */
599 v86.es = VTOPSEG(buf);
601 v86.ebx = VTOPOFF(buf);
603 v86.edx = nblk << 8 | dsk.drive;
606 if (V86_CY(v86.efl)) {
607 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
614 keyhit(unsigned ticks)
618 if (opts & 1 << RBX_NOINTR)
624 t1 = *(uint32_t *)PTOV(0x46c);
627 if (t1 < t0 || t1 >= t0 + ticks)
635 if (opts & RBF_VIDEO)
637 if (opts & RBF_SERIAL)
644 if (opts & 1 << RBX_NOINTR)
647 if ((opts & RBF_VIDEO) && getc(1))
648 return fn ? 1 : getc(0);
649 if ((opts & RBF_SERIAL) && sio_ischar())
650 return fn ? 1 : sio_getc();
662 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);