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>
41 /* Define to 0 to omit serial support */
50 #define DO_KBD (ioctrl & IO_KEYBOARD)
51 #define DO_SIO (ioctrl & IO_SERIAL)
57 #define SECOND 1 /* Circa that many ticks in a second. */
72 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
73 static const unsigned char flags[NOPT] = {
90 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
91 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
92 static const unsigned char dev_daua[NDEV] = {0x80, 0xa0, 0x90};
105 static char cmd[512], cmddup[512], knamebuf[1024];
106 static const char *kname;
108 static struct bootinfo bootinfo;
110 static int comspeed = SIOSPD;
111 static uint8_t ioctrl = IO_KEYBOARD;
115 static void load(void);
116 static int parse(void);
117 static int dskread(void *, unsigned, unsigned);
118 static void printf(const char *,...);
119 static void putchar(int);
120 static int drvread(void *, unsigned);
121 static int keyhit(unsigned);
122 static int xputc(int);
123 static int xgetc(int);
124 static inline int getc(int);
126 static void memcpy(void *, const void *, int);
128 memcpy(void *dst, const void *src, int len)
138 strcmp(const char *s1, const char *s2)
140 for (; *s1 == *s2 && *s1; s1++, s2++);
141 return (unsigned char)*s1 - (unsigned char)*s2;
144 #define UFS_SMALL_CGBASE
148 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
150 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
151 printf("Invalid %s\n", "format");
165 switch (c = xgetc(0)) {
180 if (s - cmd < sizeof(cmd) - 1)
191 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
192 v86.addr = PUTCORG; /* call to putc in boot1 */
202 if ((*(u_char *)PTOV(0x482) >> dsk.unit) & 0x01)
209 fix_sector_size(void)
213 p = (u_char *)PTOV(0x460 + dsk.unit * 4); /* SCSI equipment parameter */
215 if ((p[0] & 0x1f) == 7) { /* SCSI MO */
216 if (!(p[3] & 0x30)) { /* 256B / sector */
217 p[3] |= 0x10; /* forced set 512B / sector */
218 p[3 + 0xa1000] |= 0x10;
223 static inline uint32_t
227 if (dsk.disk == 0x30) { /* 1440KB FD */
228 /* 80 cylinders, 2 heads, 18 sectors */
229 return (80 << 16) | (2 << 8) | 18;
230 } else if (dsk.disk == 0x90) { /* 1200KB FD */
231 /* 80 cylinders, 2 heads, 15 sectors */
232 return (80 << 16) | (2 << 8) | 15;
233 } else if (dsk.disk == 0x80 || is_scsi_hd()) { /* IDE or SCSI HDD */
235 v86.eax = 0x8400 | dsk.daua;
237 return (v86.ecx << 16) | v86.edx;
241 fix_sector_size(); /* SCSI MO */
243 /* other SCSI devices */
244 return (65535 << 16) | (8 << 8) | 32;
254 dsk.head = (di >> 8) & 0xff;
261 bd_getbigeom(int bunit)
264 int unit = 0x80; /* IDE HDD */
267 while (unit < 0xa7) {
268 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
273 int media = ((unsigned *)PTOV(0x460))[unit & 0x0F] & 0x1F;
275 if (media == 7 && hds++ == bunit) /* SCSI MO */
276 return(0xFFFE0820); /* C:65535 H:8 S:32 */
278 if (++unit == 0x84) {
279 unit = 0xA0; /* SCSI HDD */
284 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
286 v86.eax = 0x8400 | unit;
289 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
290 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
297 struct pc98_partition *dp;
301 sec = dmadat->secbuf;
302 cyl = *(uint16_t *)PTOV(ARGS);
305 if (dsk.type == TYPE_FD)
306 return (WHOLE_DISK_SLICE);
307 if (drvread(sec, PC98_BBSECTOR))
308 return (WHOLE_DISK_SLICE); /* Read error */
309 dp = (void *)(sec + PC98_PARTOFF);
310 for (i = 0; i < PC98_NPARTS; i++) {
311 if (dp[i].dp_mid == DOSMID_386BSD) {
312 if (dp[i].dp_scyl <= cyl && cyl <= dp[i].dp_ecyl)
313 return (BASE_SLICE + i);
317 return (WHOLE_DISK_SLICE);
330 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
332 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
333 dsk.daua = *(uint8_t *)PTOV(0x584);
334 dsk.disk = dsk.daua & DRV_DISK;
335 dsk.unit = dsk.daua & DRV_UNIT;
336 if (dsk.disk == 0x80)
338 else if (dsk.disk == 0xa0)
340 else /* if (dsk.disk == 0x30 || dsk.disk == 0x90) */
342 dsk.slice = check_slice();
344 for (i = 0; i < N_BIOS_GEOM; i++)
345 bootinfo.bi_bios_geom[i] = bd_getbigeom(i);
347 bootinfo.bi_version = BOOTINFO_VERSION;
348 bootinfo.bi_size = sizeof(bootinfo);
350 /* Process configuration file */
354 if ((ino = lookup(PATH_CONFIG)) ||
355 (ino = lookup(PATH_DOTCONFIG))) {
356 nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
361 memcpy(cmddup, cmd, sizeof(cmd));
364 if (!OPT_CHECK(RBX_QUIET))
365 printf("%s: %s", PATH_CONFIG, cmddup);
366 /* Do not process this command twice */
371 * Try to exec stage 3 boot loader. If interrupted by a keypress,
372 * or in case of failure, try to load a kernel directly instead.
377 if (autoboot && !keyhit(3*SECOND)) {
383 /* Present the user with the boot2 prompt. */
386 if (!autoboot || !OPT_CHECK(RBX_QUIET))
387 printf("\nFreeBSD/pc98 boot\n"
388 "Default: %u:%s(%u,%c)%s\n"
390 dsk.unit, dev_nm[dsk.type], dsk.unit,
391 'a' + dsk.part, kname);
394 if (!autoboot || keyhit(3*SECOND))
396 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
406 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
419 static Elf32_Phdr ep[2];
420 static Elf32_Shdr es[2];
426 if (!(ino = lookup(kname))) {
428 printf("No %s\n", kname);
431 if (xfsread(ino, &hdr, sizeof(hdr)))
434 if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
435 addr = hdr.ex.a_entry & 0xffffff;
438 if (xfsread(ino, p, hdr.ex.a_text))
440 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
441 if (xfsread(ino, p, hdr.ex.a_data))
443 } else if (IS_ELF(hdr.eh)) {
444 fs_off = hdr.eh.e_phoff;
445 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
446 if (xfsread(ino, ep + j, sizeof(ep[0])))
448 if (ep[j].p_type == PT_LOAD)
451 for (i = 0; i < 2; i++) {
452 p = PTOV(ep[i].p_paddr & 0xffffff);
453 fs_off = ep[i].p_offset;
454 if (xfsread(ino, p, ep[i].p_filesz))
457 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
458 bootinfo.bi_symtab = VTOP(p);
459 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
460 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
461 (hdr.eh.e_shstrndx + 1);
462 if (xfsread(ino, &es, sizeof(es)))
464 for (i = 0; i < 2; i++) {
465 *(Elf32_Word *)p = es[i].sh_size;
466 p += sizeof(es[i].sh_size);
467 fs_off = es[i].sh_offset;
468 if (xfsread(ino, p, es[i].sh_size))
473 addr = hdr.eh.e_entry & 0xffffff;
474 bootinfo.bi_esymtab = VTOP(p);
476 printf("Invalid %s\n", "format");
480 bootinfo.bi_kernelname = VTOP(kname);
481 bootinfo.bi_bios_dev = dsk.daua;
482 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
483 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
484 0, 0, 0, VTOP(&bootinfo));
496 while ((c = *arg++)) {
497 if (c == ' ' || c == '\t' || c == '\n')
499 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
504 while ((c = *arg++)) {
506 if (*(uint8_t *)PTOV(0x481) & 0x48) {
509 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
512 printf("Keyboard: %s\n", cp);
515 } else if (c == 'S') {
517 while ((unsigned int)(i = *arg++ - '0') <= 9)
519 if (j > 0 && i == -'0') {
523 /* Fall through to error below ('S' not in optstr[]). */
526 for (i = 0; c != optstr[i]; i++)
529 opts ^= OPT_SET(flags[i]);
532 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
533 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
535 if (sio_init(115200 / comspeed) != 0)
536 ioctrl &= ~IO_SERIAL;
540 for (q = arg--; *q && *q != '('; q++);
551 for (i = 0; arg[0] != dev_nm[i][0] ||
552 arg[1] != dev_nm[i][1]; i++)
557 dsk.unit = *arg - '0';
558 if (arg[1] != ',' || dsk.unit > 9)
561 dsk.slice = WHOLE_DISK_SLICE;
563 dsk.slice = *arg - '0' + 1;
564 if (dsk.slice > PC98_NPARTS + 1)
570 dsk.part = *arg - 'a';
576 dsk.disk = dev_daua[dsk.type];
577 dsk.daua = dsk.disk | dsk.unit;
580 if ((i = ep - arg)) {
581 if ((size_t)i >= sizeof(knamebuf))
583 memcpy(knamebuf, arg, i + 1);
593 dskread(void *buf, unsigned lba, unsigned nblk)
595 struct pc98_partition *dp;
603 sec = dmadat->secbuf;
605 if (dsk.type == TYPE_FD)
607 if (drvread(sec, PC98_BBSECTOR))
609 dp = (void *)(sec + PC98_PARTOFF);
611 if (sl < BASE_SLICE) {
612 for (i = 0; i < PC98_NPARTS; i++)
613 if (dp[i].dp_mid == DOSMID_386BSD) {
619 if (sl != WHOLE_DISK_SLICE) {
620 dp += sl - BASE_SLICE;
621 if (dp->dp_mid != DOSMID_386BSD) {
622 printf("Invalid %s\n", "slice");
625 dsk.start = dp->dp_scyl * dsk.head * dsk.sec +
626 dp->dp_shd * dsk.sec + dp->dp_ssect;
628 if (drvread(sec, dsk.start + LABELSECTOR))
630 d = (void *)(sec + LABELOFFSET);
631 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
632 if (dsk.part != RAW_PART) {
633 printf("Invalid %s\n", "label");
637 if (dsk.part >= d->d_npartitions ||
638 !d->d_partitions[dsk.part].p_size) {
639 printf("Invalid %s\n", "partition");
642 dsk.start += d->d_partitions[dsk.part].p_offset;
643 dsk.start -= d->d_partitions[RAW_PART].p_offset;
647 for (p = buf; nblk; p += 512, lba++, nblk--) {
648 if ((i = drvread(p, dsk.start + lba)))
655 printf(const char *fmt,...)
664 while ((c = *fmt++)) {
669 putchar(va_arg(ap, int));
672 for (s = va_arg(ap, char *); *s; s++)
676 u = va_arg(ap, unsigned);
679 *s++ = '0' + u % 10U;
701 drvread(void *buf, unsigned lba)
703 static unsigned c = 0x2d5c7c2f;
704 unsigned bpc, x, cyl, head, sec;
706 bpc = dsk.sec * dsk.head;
712 if (!OPT_CHECK(RBX_QUIET))
713 printf("%c\b", c = c << 8 | c >> 24);
714 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
715 v86.addr = READORG; /* call to read in boot1 */
717 v86.edx = (head << 8) | sec;
720 v86.es = VTOPSEG(buf);
721 v86.ebp = VTOPOFF(buf);
724 if (V86_CY(v86.efl)) {
725 printf("error %u c/h/s %u/%u/%u lba %u\n", v86.eax >> 8 & 0xff,
726 cyl, head, sec, lba);
739 outb(0x5f, 0); /* about 600ns */
748 if (OPT_CHECK(RBX_NOINTR))
750 for (i = 0; i < sec * 1000; i++) {
775 return (v86.ebx >> 8) & 0x01;
777 return v86.eax & 0xff;
783 if (OPT_CHECK(RBX_NOINTR))
786 if (DO_KBD && getc(1))
787 return fn ? 1 : getc(0);
788 if (DO_SIO && sio_ischar())
789 return fn ? 1 : sio_getc();