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>
39 /* Define to 0 to omit serial support */
48 #define DO_KBD (ioctrl & IO_KEYBOARD)
49 #define DO_SIO (ioctrl & IO_SERIAL)
55 #define SECOND 1 /* Circa that many ticks in a second. */
57 #define RBX_ASKNAME 0x0 /* -a */
58 #define RBX_SINGLE 0x1 /* -s */
59 /* 0x2 is reserved for log2(RB_NOSYNC). */
60 /* 0x3 is reserved for log2(RB_HALT). */
61 /* 0x4 is reserved for log2(RB_INITNAME). */
62 #define RBX_DFLTROOT 0x5 /* -r */
63 #define RBX_KDB 0x6 /* -d */
64 /* 0x7 is reserved for log2(RB_RDONLY). */
65 /* 0x8 is reserved for log2(RB_DUMP). */
66 /* 0x9 is reserved for log2(RB_MINIROOT). */
67 #define RBX_CONFIG 0xa /* -c */
68 #define RBX_VERBOSE 0xb /* -v */
69 #define RBX_SERIAL 0xc /* -h */
70 #define RBX_CDROM 0xd /* -C */
71 /* 0xe is reserved for log2(RB_POWEROFF). */
72 #define RBX_GDB 0xf /* -g */
73 #define RBX_MUTE 0x10 /* -m */
74 /* 0x11 is reserved for log2(RB_SELFTEST). */
75 /* 0x12 is reserved for boot programs. */
76 /* 0x13 is reserved for boot programs. */
77 #define RBX_PAUSE 0x14 /* -p */
78 #define RBX_QUIET 0x15 /* -q */
79 #define RBX_NOINTR 0x1c /* -n */
80 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
81 #define RBX_DUAL 0x1d /* -D */
82 /* 0x1f is reserved for log2(RB_BOOTINFO). */
84 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
85 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
86 OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
87 OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
88 OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
89 OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
90 OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
92 #define PATH_DOTCONFIG "/boot.config"
93 #define PATH_CONFIG "/boot/config"
94 #define PATH_BOOT3 "/boot/loader"
95 #define PATH_KERNEL "/boot/kernel/kernel"
101 #define DRV_DISK 0xf0
102 #define DRV_UNIT 0x0f
108 #define OPT_SET(opt) (1 << (opt))
109 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
111 extern uint32_t _end;
113 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
114 static const unsigned char flags[NOPT] = {
131 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
132 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
133 static const unsigned char dev_daua[NDEV] = {0x80, 0xa0, 0x90};
146 static char cmd[512], cmddup[512], knamebuf[1024];
147 static const char *kname;
148 static uint32_t opts;
149 static struct bootinfo bootinfo;
151 static int comspeed = SIOSPD;
152 static uint8_t ioctrl = IO_KEYBOARD;
156 static void load(void);
157 static int parse(void);
158 static int dskread(void *, unsigned, unsigned);
159 static void printf(const char *,...);
160 static void putchar(int);
161 static int drvread(void *, unsigned);
162 static int keyhit(unsigned);
163 static int xputc(int);
164 static int xgetc(int);
165 static inline int getc(int);
167 static void memcpy(void *, const void *, int);
169 memcpy(void *dst, const void *src, int len)
179 strcmp(const char *s1, const char *s2)
181 for (; *s1 == *s2 && *s1; s1++, s2++);
182 return (unsigned char)*s1 - (unsigned char)*s2;
185 #define UFS_SMALL_CGBASE
189 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
191 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
192 printf("Invalid %s\n", "format");
206 switch (c = xgetc(0)) {
221 if (s - cmd < sizeof(cmd) - 1)
232 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
233 v86.addr = PUTCORG; /* call to putc in boot1 */
243 if ((*(u_char *)PTOV(0x482) >> dsk.unit) & 0x01)
250 fix_sector_size(void)
254 p = (u_char *)PTOV(0x460 + dsk.unit * 4); /* SCSI equipment parameter */
256 if ((p[0] & 0x1f) == 7) { /* SCSI MO */
257 if (!(p[3] & 0x30)) { /* 256B / sector */
258 p[3] |= 0x10; /* forced set 512B / sector */
259 p[3 + 0xa1000] |= 0x10;
264 static inline uint32_t
268 if (dsk.disk == 0x30) { /* 1440KB FD */
269 /* 80 cylinders, 2 heads, 18 sectors */
270 return (80 << 16) | (2 << 8) | 18;
271 } else if (dsk.disk == 0x90) { /* 1200KB FD */
272 /* 80 cylinders, 2 heads, 15 sectors */
273 return (80 << 16) | (2 << 8) | 15;
274 } else if (dsk.disk == 0x80 || is_scsi_hd()) { /* IDE or SCSI HDD */
276 v86.eax = 0x8400 | dsk.daua;
278 return (v86.ecx << 16) | v86.edx;
282 fix_sector_size(); /* SCSI MO */
284 /* other SCSI devices */
285 return (65535 << 16) | (8 << 8) | 32;
295 dsk.head = (di >> 8) & 0xff;
302 bd_getbigeom(int bunit)
305 int unit = 0x80; /* IDE HDD */
308 while (unit < 0xa7) {
309 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
314 int media = ((unsigned *)PTOV(0x460))[unit & 0x0F] & 0x1F;
316 if (media == 7 && hds++ == bunit) /* SCSI MO */
317 return(0xFFFE0820); /* C:65535 H:8 S:32 */
319 if (++unit == 0x84) {
320 unit = 0xA0; /* SCSI HDD */
325 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
327 v86.eax = 0x8400 | unit;
330 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
331 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
338 struct pc98_partition *dp;
342 sec = dmadat->secbuf;
343 cyl = *(uint16_t *)PTOV(ARGS);
346 if (dsk.type == TYPE_FD)
347 return (WHOLE_DISK_SLICE);
348 if (drvread(sec, PC98_BBSECTOR))
349 return (WHOLE_DISK_SLICE); /* Read error */
350 dp = (void *)(sec + PC98_PARTOFF);
351 for (i = 0; i < PC98_NPARTS; i++) {
352 if (dp[i].dp_mid == DOSMID_386BSD) {
353 if (dp[i].dp_scyl <= cyl && cyl <= dp[i].dp_ecyl)
354 return (BASE_SLICE + i);
358 return (WHOLE_DISK_SLICE);
371 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
373 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
374 dsk.daua = *(uint8_t *)PTOV(0x584);
375 dsk.disk = dsk.daua & DRV_DISK;
376 dsk.unit = dsk.daua & DRV_UNIT;
377 if (dsk.disk == 0x80)
379 else if (dsk.disk == 0xa0)
381 else /* if (dsk.disk == 0x30 || dsk.disk == 0x90) */
383 dsk.slice = check_slice();
385 for (i = 0; i < N_BIOS_GEOM; i++)
386 bootinfo.bi_bios_geom[i] = bd_getbigeom(i);
388 bootinfo.bi_version = BOOTINFO_VERSION;
389 bootinfo.bi_size = sizeof(bootinfo);
391 /* Process configuration file */
395 if ((ino = lookup(PATH_CONFIG)) ||
396 (ino = lookup(PATH_DOTCONFIG))) {
397 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
402 memcpy(cmddup, cmd, sizeof(cmd));
405 if (!OPT_CHECK(RBX_QUIET))
406 printf("%s: %s", PATH_CONFIG, cmddup);
407 /* Do not process this command twice */
412 * Try to exec stage 3 boot loader. If interrupted by a keypress,
413 * or in case of failure, try to load a kernel directly instead.
418 if (autoboot && !keyhit(3*SECOND)) {
424 /* Present the user with the boot2 prompt. */
427 if (!autoboot || !OPT_CHECK(RBX_QUIET))
428 printf("\nFreeBSD/pc98 boot\n"
429 "Default: %u:%s(%u,%c)%s\n"
431 dsk.unit, dev_nm[dsk.type], dsk.unit,
432 'a' + dsk.part, kname);
435 if (!autoboot || keyhit(3*SECOND))
437 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
447 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
460 static Elf32_Phdr ep[2];
461 static Elf32_Shdr es[2];
467 if (!(ino = lookup(kname))) {
469 printf("No %s\n", kname);
472 if (xfsread(ino, &hdr, sizeof(hdr)))
475 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
476 addr = hdr.ex.a_entry & 0xffffff;
479 if (xfsread(ino, p, hdr.ex.a_text))
481 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
482 if (xfsread(ino, p, hdr.ex.a_data))
484 } else if (IS_ELF(hdr.eh)) {
485 fs_off = hdr.eh.e_phoff;
486 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
487 if (xfsread(ino, ep + j, sizeof(ep[0])))
489 if (ep[j].p_type == PT_LOAD)
492 for (i = 0; i < 2; i++) {
493 p = PTOV(ep[i].p_paddr & 0xffffff);
494 fs_off = ep[i].p_offset;
495 if (xfsread(ino, p, ep[i].p_filesz))
498 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
499 bootinfo.bi_symtab = VTOP(p);
500 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
501 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
502 (hdr.eh.e_shstrndx + 1);
503 if (xfsread(ino, &es, sizeof(es)))
505 for (i = 0; i < 2; i++) {
506 *(Elf32_Word *)p = es[i].sh_size;
507 p += sizeof(es[i].sh_size);
508 fs_off = es[i].sh_offset;
509 if (xfsread(ino, p, es[i].sh_size))
514 addr = hdr.eh.e_entry & 0xffffff;
515 bootinfo.bi_esymtab = VTOP(p);
517 printf("Invalid %s\n", "format");
521 bootinfo.bi_kernelname = VTOP(kname);
522 bootinfo.bi_bios_dev = dsk.daua;
523 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
524 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
525 0, 0, 0, VTOP(&bootinfo));
537 while ((c = *arg++)) {
538 if (c == ' ' || c == '\t' || c == '\n')
540 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
545 while ((c = *arg++)) {
547 if (*(uint8_t *)PTOV(0x481) & 0x48) {
550 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
553 printf("Keyboard: %s\n", cp);
556 } else if (c == 'S') {
558 while ((unsigned int)(i = *arg++ - '0') <= 9)
560 if (j > 0 && i == -'0') {
564 /* Fall through to error below ('S' not in optstr[]). */
567 for (i = 0; c != optstr[i]; i++)
570 opts ^= OPT_SET(flags[i]);
573 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
574 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
576 if (sio_init(115200 / comspeed) != 0)
577 ioctrl &= ~IO_SERIAL;
581 for (q = arg--; *q && *q != '('; q++);
592 for (i = 0; arg[0] != dev_nm[i][0] ||
593 arg[1] != dev_nm[i][1]; i++)
598 dsk.unit = *arg - '0';
599 if (arg[1] != ',' || dsk.unit > 9)
602 dsk.slice = WHOLE_DISK_SLICE;
604 dsk.slice = *arg - '0' + 1;
605 if (dsk.slice > PC98_NPARTS + 1)
611 dsk.part = *arg - 'a';
617 dsk.disk = dev_daua[dsk.type];
618 dsk.daua = dsk.disk | dsk.unit;
621 if ((i = ep - arg)) {
622 if ((size_t)i >= sizeof(knamebuf))
624 memcpy(knamebuf, arg, i + 1);
634 dskread(void *buf, unsigned lba, unsigned nblk)
636 struct pc98_partition *dp;
644 sec = dmadat->secbuf;
646 if (dsk.type == TYPE_FD)
648 if (drvread(sec, PC98_BBSECTOR))
650 dp = (void *)(sec + PC98_PARTOFF);
652 if (sl < BASE_SLICE) {
653 for (i = 0; i < PC98_NPARTS; i++)
654 if (dp[i].dp_mid == DOSMID_386BSD) {
660 if (sl != WHOLE_DISK_SLICE) {
661 dp += sl - BASE_SLICE;
662 if (dp->dp_mid != DOSMID_386BSD) {
663 printf("Invalid %s\n", "slice");
666 dsk.start = dp->dp_scyl * dsk.head * dsk.sec +
667 dp->dp_shd * dsk.sec + dp->dp_ssect;
669 if (drvread(sec, dsk.start + LABELSECTOR))
671 d = (void *)(sec + LABELOFFSET);
672 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
673 if (dsk.part != RAW_PART) {
674 printf("Invalid %s\n", "label");
678 if (dsk.part >= d->d_npartitions ||
679 !d->d_partitions[dsk.part].p_size) {
680 printf("Invalid %s\n", "partition");
683 dsk.start += d->d_partitions[dsk.part].p_offset;
684 dsk.start -= d->d_partitions[RAW_PART].p_offset;
688 for (p = buf; nblk; p += 512, lba++, nblk--) {
689 if ((i = drvread(p, dsk.start + lba)))
696 printf(const char *fmt,...)
705 while ((c = *fmt++)) {
710 putchar(va_arg(ap, int));
713 for (s = va_arg(ap, char *); *s; s++)
717 u = va_arg(ap, unsigned);
720 *s++ = '0' + u % 10U;
742 drvread(void *buf, unsigned lba)
744 static unsigned c = 0x2d5c7c2f;
745 unsigned bpc, x, cyl, head, sec;
747 bpc = dsk.sec * dsk.head;
753 if (!OPT_CHECK(RBX_QUIET))
754 printf("%c\b", c = c << 8 | c >> 24);
755 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
756 v86.addr = READORG; /* call to read in boot1 */
758 v86.edx = (head << 8) | sec;
761 v86.es = VTOPSEG(buf);
762 v86.ebp = VTOPOFF(buf);
765 if (V86_CY(v86.efl)) {
766 printf("error %u c/h/s %u/%u/%u lba %u\n", v86.eax >> 8 & 0xff,
767 cyl, head, sec, lba);
780 outb(0x5f, 0); /* about 600ns */
789 if (OPT_CHECK(RBX_NOINTR))
791 for (i = 0; i < sec * 1000; i++) {
816 return (v86.ebx >> 8) & 0x01;
818 return v86.eax & 0xff;
824 if (OPT_CHECK(RBX_NOINTR))
827 if (DO_KBD && getc(1))
828 return fn ? 1 : getc(0);
829 if (DO_SIO && sio_ischar())
830 return fn ? 1 : sio_getc();