2 * Copyright (c) 2008-2009 TAKAHASHI Yoshihiro
3 * Copyright (c) 1998 Robert Nordier
6 * Redistribution and use in source and binary forms are freely
7 * permitted provided that the above copyright notice and this
8 * paragraph and the following disclaimer are duplicated in all
11 * This software is provided "AS IS" and without any express or
12 * implied warranties, including, without limitation, the implied
13 * warranties of merchantability and fitness for a particular
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/param.h>
21 #include <sys/disklabel.h>
22 #include <sys/diskpc98.h>
23 #include <sys/dirent.h>
24 #include <sys/reboot.h>
26 #include <machine/bootinfo.h>
27 #include <machine/cpufunc.h>
28 #include <machine/elf.h>
42 #define SECOND 1 /* Circa that many ticks in a second. */
44 #define RBX_ASKNAME 0x0 /* -a */
45 #define RBX_SINGLE 0x1 /* -s */
46 /* 0x2 is reserved for log2(RB_NOSYNC). */
47 /* 0x3 is reserved for log2(RB_HALT). */
48 /* 0x4 is reserved for log2(RB_INITNAME). */
49 #define RBX_DFLTROOT 0x5 /* -r */
50 #define RBX_KDB 0x6 /* -d */
51 /* 0x7 is reserved for log2(RB_RDONLY). */
52 /* 0x8 is reserved for log2(RB_DUMP). */
53 /* 0x9 is reserved for log2(RB_MINIROOT). */
54 #define RBX_CONFIG 0xa /* -c */
55 #define RBX_VERBOSE 0xb /* -v */
56 #define RBX_SERIAL 0xc /* -h */
57 #define RBX_CDROM 0xd /* -C */
58 /* 0xe is reserved for log2(RB_POWEROFF). */
59 #define RBX_GDB 0xf /* -g */
60 #define RBX_MUTE 0x10 /* -m */
61 /* 0x11 is reserved for log2(RB_SELFTEST). */
62 /* 0x12 is reserved for boot programs. */
63 /* 0x13 is reserved for boot programs. */
64 #define RBX_PAUSE 0x14 /* -p */
65 #define RBX_QUIET 0x15 /* -q */
66 #define RBX_NOINTR 0x1c /* -n */
67 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
68 #define RBX_DUAL 0x1d /* -D */
69 /* 0x1f is reserved for log2(RB_BOOTINFO). */
71 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
72 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
73 OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
74 OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
75 OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
76 OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
77 OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
79 #define PATH_DOTCONFIG "/boot.config"
80 #define PATH_CONFIG "/boot/config"
81 #define PATH_BOOT3 "/boot/loader"
82 #define PATH_KERNEL "/boot/kernel/kernel"
95 #define OPT_SET(opt) (1 << (opt))
96 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
100 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
101 static const unsigned char flags[NOPT] = {
118 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
119 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
120 static const unsigned char dev_daua[NDEV] = {0x80, 0xa0, 0x90};
133 static char cmd[512], cmddup[512], knamebuf[1024];
134 static const char *kname;
135 static uint32_t opts;
136 static int comspeed = SIOSPD;
137 static struct bootinfo bootinfo;
138 static uint8_t ioctrl = IO_KEYBOARD;
141 static void load(void);
142 static int parse(void);
143 static int dskread(void *, unsigned, unsigned);
144 static void printf(const char *,...);
145 static void putchar(int);
146 static int drvread(void *, unsigned);
147 static int keyhit(unsigned);
148 static int xputc(int);
149 static int xgetc(int);
150 static inline int getc(int);
152 static void memcpy(void *, const void *, int);
154 memcpy(void *dst, const void *src, int len)
164 strcmp(const char *s1, const char *s2)
166 for (; *s1 == *s2 && *s1; s1++, s2++);
167 return (unsigned char)*s1 - (unsigned char)*s2;
170 #define UFS_SMALL_CGBASE
174 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
176 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
177 printf("Invalid %s\n", "format");
191 switch (c = xgetc(0)) {
206 if (s - cmd < sizeof(cmd) - 1)
217 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
218 v86.addr = PUTCORG; /* call to putc in boot1 */
228 if ((*(u_char *)PTOV(0x482) >> dsk.unit) & 0x01)
235 fix_sector_size(void)
239 p = (u_char *)PTOV(0x460 + dsk.unit * 4); /* SCSI equipment parameter */
241 if ((p[0] & 0x1f) == 7) { /* SCSI MO */
242 if (!(p[3] & 0x30)) { /* 256B / sector */
243 p[3] |= 0x10; /* forced set 512B / sector */
244 p[3 + 0xa1000] |= 0x10;
249 static inline uint32_t
253 if (dsk.disk == 0x30) { /* 1440KB FD */
254 /* 80 cylinders, 2 heads, 18 sectors */
255 return (80 << 16) | (2 << 8) | 18;
256 } else if (dsk.disk == 0x90) { /* 1200KB FD */
257 /* 80 cylinders, 2 heads, 15 sectors */
258 return (80 << 16) | (2 << 8) | 15;
259 } else if (dsk.disk == 0x80 || is_scsi_hd()) { /* IDE or SCSI HDD */
261 v86.eax = 0x8400 | dsk.daua;
263 return (v86.ecx << 16) | v86.edx;
267 fix_sector_size(); /* SCSI MO */
269 /* other SCSI devices */
270 return (65535 << 16) | (8 << 8) | 32;
280 dsk.head = (di >> 8) & 0xff;
287 bd_getbigeom(int bunit)
290 int unit = 0x80; /* IDE HDD */
293 while (unit < 0xa7) {
294 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
299 int media = ((unsigned *)PTOV(0x460))[unit & 0x0F] & 0x1F;
301 if (media == 7 && hds++ == bunit) /* SCSI MO */
302 return(0xFFFE0820); /* C:65535 H:8 S:32 */
304 if (++unit == 0x84) {
305 unit = 0xA0; /* SCSI HDD */
310 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
312 v86.eax = 0x8400 | unit;
315 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
316 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
323 struct pc98_partition *dp;
327 sec = dmadat->secbuf;
328 cyl = *(uint16_t *)PTOV(ARGS);
331 if (dsk.type == TYPE_FD)
332 return (WHOLE_DISK_SLICE);
333 if (drvread(sec, PC98_BBSECTOR))
334 return (WHOLE_DISK_SLICE); /* Read error */
335 dp = (void *)(sec + PC98_PARTOFF);
336 for (i = 0; i < PC98_NPARTS; i++) {
337 if (dp[i].dp_mid == DOSMID_386BSD) {
338 if (dp[i].dp_scyl <= cyl && cyl <= dp[i].dp_ecyl)
339 return (BASE_SLICE + i);
343 return (WHOLE_DISK_SLICE);
356 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
358 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
359 dsk.daua = *(uint8_t *)PTOV(0x584);
360 dsk.disk = dsk.daua & DRV_DISK;
361 dsk.unit = dsk.daua & DRV_UNIT;
362 if (dsk.disk == 0x80)
364 else if (dsk.disk == 0xa0)
366 else /* if (dsk.disk == 0x30 || dsk.disk == 0x90) */
368 dsk.slice = check_slice();
370 for (i = 0; i < N_BIOS_GEOM; i++)
371 bootinfo.bi_bios_geom[i] = bd_getbigeom(i);
373 bootinfo.bi_version = BOOTINFO_VERSION;
374 bootinfo.bi_size = sizeof(bootinfo);
376 /* Process configuration file */
380 if ((ino = lookup(PATH_CONFIG)) ||
381 (ino = lookup(PATH_DOTCONFIG))) {
382 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
387 memcpy(cmddup, cmd, sizeof(cmd));
390 if (!OPT_CHECK(RBX_QUIET))
391 printf("%s: %s", PATH_CONFIG, cmddup);
392 /* Do not process this command twice */
397 * Try to exec stage 3 boot loader. If interrupted by a keypress,
398 * or in case of failure, try to load a kernel directly instead.
403 if (autoboot && !keyhit(3*SECOND)) {
409 /* Present the user with the boot2 prompt. */
412 if (!autoboot || !OPT_CHECK(RBX_QUIET))
413 printf("\nFreeBSD/pc98 boot\n"
414 "Default: %u:%s(%u,%c)%s\n"
416 dsk.unit, dev_nm[dsk.type], dsk.unit,
417 'a' + dsk.part, kname);
418 if (ioctrl & IO_SERIAL)
420 if (!autoboot || keyhit(3*SECOND))
422 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
432 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
445 static Elf32_Phdr ep[2];
446 static Elf32_Shdr es[2];
452 if (!(ino = lookup(kname))) {
454 printf("No %s\n", kname);
457 if (xfsread(ino, &hdr, sizeof(hdr)))
460 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
461 addr = hdr.ex.a_entry & 0xffffff;
464 if (xfsread(ino, p, hdr.ex.a_text))
466 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
467 if (xfsread(ino, p, hdr.ex.a_data))
469 } else if (IS_ELF(hdr.eh)) {
470 fs_off = hdr.eh.e_phoff;
471 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
472 if (xfsread(ino, ep + j, sizeof(ep[0])))
474 if (ep[j].p_type == PT_LOAD)
477 for (i = 0; i < 2; i++) {
478 p = PTOV(ep[i].p_paddr & 0xffffff);
479 fs_off = ep[i].p_offset;
480 if (xfsread(ino, p, ep[i].p_filesz))
483 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
484 bootinfo.bi_symtab = VTOP(p);
485 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
486 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
487 (hdr.eh.e_shstrndx + 1);
488 if (xfsread(ino, &es, sizeof(es)))
490 for (i = 0; i < 2; i++) {
491 *(Elf32_Word *)p = es[i].sh_size;
492 p += sizeof(es[i].sh_size);
493 fs_off = es[i].sh_offset;
494 if (xfsread(ino, p, es[i].sh_size))
499 addr = hdr.eh.e_entry & 0xffffff;
500 bootinfo.bi_esymtab = VTOP(p);
502 printf("Invalid %s\n", "format");
506 bootinfo.bi_kernelname = VTOP(kname);
507 bootinfo.bi_bios_dev = dsk.daua;
508 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
509 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
510 0, 0, 0, VTOP(&bootinfo));
522 while ((c = *arg++)) {
523 if (c == ' ' || c == '\t' || c == '\n')
525 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
530 while ((c = *arg++)) {
532 if (*(uint8_t *)PTOV(0x481) & 0x48) {
535 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
538 printf("Keyboard: %s\n", cp);
540 } else if (c == 'S') {
542 while ((unsigned int)(i = *arg++ - '0') <= 9)
544 if (j > 0 && i == -'0') {
548 /* Fall through to error below ('S' not in optstr[]). */
550 for (i = 0; c != optstr[i]; i++)
553 opts ^= OPT_SET(flags[i]);
555 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
556 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
557 if (ioctrl & IO_SERIAL) {
558 if (sio_init(115200 / comspeed) != 0)
559 ioctrl &= ~IO_SERIAL;
562 for (q = arg--; *q && *q != '('; q++);
573 for (i = 0; arg[0] != dev_nm[i][0] ||
574 arg[1] != dev_nm[i][1]; i++)
579 dsk.unit = *arg - '0';
580 if (arg[1] != ',' || dsk.unit > 9)
583 dsk.slice = WHOLE_DISK_SLICE;
585 dsk.slice = *arg - '0' + 1;
586 if (dsk.slice > PC98_NPARTS + 1)
592 dsk.part = *arg - 'a';
598 dsk.disk = dev_daua[dsk.type];
599 dsk.daua = dsk.disk | dsk.unit;
602 if ((i = ep - arg)) {
603 if ((size_t)i >= sizeof(knamebuf))
605 memcpy(knamebuf, arg, i + 1);
615 dskread(void *buf, unsigned lba, unsigned nblk)
617 struct pc98_partition *dp;
625 sec = dmadat->secbuf;
627 if (dsk.type == TYPE_FD)
629 if (drvread(sec, PC98_BBSECTOR))
631 dp = (void *)(sec + PC98_PARTOFF);
633 if (sl < BASE_SLICE) {
634 for (i = 0; i < PC98_NPARTS; i++)
635 if (dp[i].dp_mid == DOSMID_386BSD) {
641 if (sl != WHOLE_DISK_SLICE) {
642 dp += sl - BASE_SLICE;
643 if (dp->dp_mid != DOSMID_386BSD) {
644 printf("Invalid %s\n", "slice");
647 dsk.start = dp->dp_scyl * dsk.head * dsk.sec +
648 dp->dp_shd * dsk.sec + dp->dp_ssect;
650 if (drvread(sec, dsk.start + LABELSECTOR))
652 d = (void *)(sec + LABELOFFSET);
653 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
654 if (dsk.part != RAW_PART) {
655 printf("Invalid %s\n", "label");
659 if (dsk.part >= d->d_npartitions ||
660 !d->d_partitions[dsk.part].p_size) {
661 printf("Invalid %s\n", "partition");
664 dsk.start += d->d_partitions[dsk.part].p_offset;
665 dsk.start -= d->d_partitions[RAW_PART].p_offset;
669 for (p = buf; nblk; p += 512, lba++, nblk--) {
670 if ((i = drvread(p, dsk.start + lba)))
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;
723 drvread(void *buf, unsigned lba)
725 static unsigned c = 0x2d5c7c2f;
726 unsigned bpc, x, cyl, head, sec;
728 bpc = dsk.sec * dsk.head;
734 if (!OPT_CHECK(RBX_QUIET))
735 printf("%c\b", c = c << 8 | c >> 24);
736 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
737 v86.addr = READORG; /* call to read in boot1 */
739 v86.edx = (head << 8) | sec;
742 v86.es = VTOPSEG(buf);
743 v86.ebp = VTOPOFF(buf);
746 if (V86_CY(v86.efl)) {
747 printf("error %u c/h/s %u/%u/%u lba %u\n", v86.eax >> 8 & 0xff,
748 cyl, head, sec, lba);
761 outb(0x5f, 0); /* about 600ns */
770 if (OPT_CHECK(RBX_NOINTR))
772 for (i = 0; i < sec * 1000; i++) {
783 if (ioctrl & IO_KEYBOARD)
785 if (ioctrl & IO_SERIAL)
797 return (v86.ebx >> 8) & 0x01;
799 return v86.eax & 0xff;
805 if (OPT_CHECK(RBX_NOINTR))
808 if (ioctrl & IO_KEYBOARD && getc(1))
809 return fn ? 1 : getc(0);
810 if (ioctrl & IO_SERIAL && sio_ischar())
811 return fn ? 1 : sio_getc();