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>
29 #include <machine/psl.h>
43 #define SECOND 1 /* Circa that many ticks in a second. */
45 #define RBX_ASKNAME 0x0 /* -a */
46 #define RBX_SINGLE 0x1 /* -s */
47 /* 0x2 is reserved for log2(RB_NOSYNC). */
48 /* 0x3 is reserved for log2(RB_HALT). */
49 /* 0x4 is reserved for log2(RB_INITNAME). */
50 #define RBX_DFLTROOT 0x5 /* -r */
51 #define RBX_KDB 0x6 /* -d */
52 /* 0x7 is reserved for log2(RB_RDONLY). */
53 /* 0x8 is reserved for log2(RB_DUMP). */
54 /* 0x9 is reserved for log2(RB_MINIROOT). */
55 #define RBX_CONFIG 0xa /* -c */
56 #define RBX_VERBOSE 0xb /* -v */
57 #define RBX_SERIAL 0xc /* -h */
58 #define RBX_CDROM 0xd /* -C */
59 /* 0xe is reserved for log2(RB_POWEROFF). */
60 #define RBX_GDB 0xf /* -g */
61 #define RBX_MUTE 0x10 /* -m */
62 /* 0x11 is reserved for log2(RB_SELFTEST). */
63 /* 0x12 is reserved for boot programs. */
64 /* 0x13 is reserved for boot programs. */
65 #define RBX_PAUSE 0x14 /* -p */
66 #define RBX_QUIET 0x15 /* -q */
67 #define RBX_NOINTR 0x1c /* -n */
68 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
69 #define RBX_DUAL 0x1d /* -D */
70 /* 0x1f is reserved for log2(RB_BOOTINFO). */
72 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
73 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
74 OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
75 OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
76 OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
77 OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
78 OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
80 #define PATH_CONFIG "/boot.config"
81 #define PATH_BOOT3 "/boot/loader"
82 #define PATH_KERNEL "/boot/kernel/kernel"
87 #define V86_CY(x) ((x) & PSL_C)
88 #define V86_ZR(x) ((x) & PSL_Z)
97 #define OPT_SET(opt) (1 << (opt))
98 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
100 extern uint32_t _end;
102 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
103 static const unsigned char flags[NOPT] = {
120 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
121 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
122 static const unsigned char dev_daua[NDEV] = {0x80, 0xa0, 0x90};
135 static char cmd[512], cmddup[512];
136 static char kname[1024];
137 static uint32_t opts;
138 static int comspeed = SIOSPD;
139 static struct bootinfo bootinfo;
140 static uint8_t ioctrl = IO_KEYBOARD;
143 static void load(void);
144 static int parse(void);
145 static int xfsread(ino_t, void *, size_t);
146 static int dskread(void *, unsigned, unsigned);
147 static void printf(const char *,...);
148 static void putchar(int);
149 static uint32_t memsize(void);
150 static int drvread(void *, unsigned);
151 static int keyhit(unsigned);
152 static int xputc(int);
153 static int xgetc(int);
154 static int getc(int);
156 static void memcpy(void *, const void *, int);
158 memcpy(void *dst, const void *src, int len)
168 strcmp(const char *s1, const char *s2)
170 for (; *s1 == *s2 && *s1; s1++, s2++);
171 return (unsigned char)*s1 - (unsigned char)*s2;
174 #define UFS_SMALL_CGBASE
178 xfsread(ino_t inode, void *buf, size_t nbyte)
180 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
181 printf("Invalid %s\n", "format");
187 static inline uint32_t
190 u_char *p = (u_char *)PTOV(0);
192 return *(p + 0x401) * 128 * 1024 + *(u_int16_t *)(p + 0x594) * 1024 * 1024;
203 switch (c = xgetc(0)) {
218 if (s - cmd < sizeof(cmd) - 1)
229 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
230 v86.addr = PUTCORG; /* call to putc in boot1 */
240 if ((*(u_char *)PTOV(0x482) >> dsk.unit) & 0x01)
247 fix_sector_size(void)
251 p = (u_char *)PTOV(0x460 + dsk.unit * 4); /* SCSI equipment parameter */
253 if ((p[0] & 0x1f) == 7) { /* SCSI MO */
254 if (!(p[3] & 0x30)) { /* 256B / sector */
255 p[3] |= 0x10; /* forced set 512B / sector */
256 p[3 + 0xa1000] |= 0x10;
261 static inline uint32_t
265 if (dsk.disk == 0x30) { /* 1440KB FD */
266 /* 80 cylinders, 2 heads, 18 sectors */
267 return (80 << 16) | (2 << 8) | 18;
268 } else if (dsk.disk == 0x90) { /* 1200KB FD */
269 /* 80 cylinders, 2 heads, 15 sectors */
270 return (80 << 16) | (2 << 8) | 15;
271 } else if (dsk.disk == 0x80 || is_scsi_hd()) { /* IDE or SCSI HDD */
273 v86.eax = 0x8400 | dsk.daua;
275 return (v86.ecx << 16) | v86.edx;
279 fix_sector_size(); /* SCSI MO */
281 /* other SCSI devices */
282 return (65535 << 16) | (8 << 8) | 32;
292 dsk.head = (di >> 8) & 0xff;
299 bd_getbigeom(int bunit)
302 int unit = 0x80; /* IDE HDD */
305 while (unit < 0xa7) {
306 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
311 int media = ((unsigned *)PTOV(0x460))[unit & 0x0F] & 0x1F;
313 if (media == 7 && hds++ == bunit) /* SCSI MO */
314 return(0xFFFE0820); /* C:65535 H:8 S:32 */
316 if (++unit == 0x84) {
317 unit = 0xA0; /* SCSI HDD */
322 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
324 v86.eax = 0x8400 | unit;
327 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
328 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
335 struct pc98_partition *dp;
339 sec = dmadat->secbuf;
340 cyl = *(uint16_t *)PTOV(ARGS);
343 if (dsk.type == TYPE_FD)
344 return (WHOLE_DISK_SLICE);
345 if (drvread(sec, DOSBBSECTOR + 1))
346 return (WHOLE_DISK_SLICE); /* Read error */
347 dp = (void *)(sec + DOSPARTOFF);
348 for (i = 0; i < NDOSPART; i++) {
349 if (dp[i].dp_mid == DOSMID_386BSD) {
350 if (dp[i].dp_scyl <= cyl && cyl <= dp[i].dp_ecyl)
351 return (BASE_SLICE + i);
355 return (WHOLE_DISK_SLICE);
367 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
369 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
370 dsk.daua = *(uint8_t *)PTOV(0x584);
371 dsk.disk = dsk.daua & DRV_DISK;
372 dsk.unit = dsk.daua & DRV_UNIT;
373 if (dsk.disk == 0x80)
375 else if (dsk.disk == 0xa0)
377 else /* if (dsk.disk == 0x30 || dsk.disk == 0x90) */
379 dsk.slice = check_slice();
381 for (i = 0; i < N_BIOS_GEOM; i++)
382 bootinfo.bi_bios_geom[i] = bd_getbigeom(i);
384 bootinfo.bi_version = BOOTINFO_VERSION;
385 bootinfo.bi_size = sizeof(bootinfo);
386 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
387 bootinfo.bi_extmem = memsize();
388 bootinfo.bi_memsizes_valid++;
390 /* Process configuration file */
394 if ((ino = lookup(PATH_CONFIG)))
395 fsread(ino, cmd, sizeof(cmd));
398 memcpy(cmddup, cmd, sizeof(cmd));
401 if (!OPT_CHECK(RBX_QUIET))
402 printf("%s: %s", PATH_CONFIG, cmddup);
403 /* Do not process this command twice */
408 * Try to exec stage 3 boot loader. If interrupted by a keypress,
409 * or in case of failure, try to load a kernel directly instead.
412 if (autoboot && !*kname) {
413 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
414 if (!keyhit(3*SECOND)) {
416 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
420 /* Present the user with the boot2 prompt. */
423 if (!autoboot || !OPT_CHECK(RBX_QUIET))
424 printf("\nFreeBSD/pc98 boot\n"
425 "Default: %u:%s(%u,%c)%s\n"
427 dsk.unit, dev_nm[dsk.type], dsk.unit,
428 'a' + dsk.part, kname);
429 if (ioctrl & IO_SERIAL)
431 if (!autoboot || keyhit(5*SECOND))
433 else if (!autoboot || !OPT_CHECK(RBX_QUIET))
443 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
456 static Elf32_Phdr ep[2];
457 static Elf32_Shdr es[2];
463 if (!(ino = lookup(kname))) {
465 printf("No %s\n", kname);
468 if (xfsread(ino, &hdr, sizeof(hdr)))
470 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
472 else if (IS_ELF(hdr.eh))
475 printf("Invalid %s\n", "format");
479 addr = hdr.ex.a_entry & 0xffffff;
482 if (xfsread(ino, p, hdr.ex.a_text))
484 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
485 if (xfsread(ino, p, hdr.ex.a_data))
487 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
488 bootinfo.bi_symtab = VTOP(p);
489 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
490 p += sizeof(hdr.ex.a_syms);
492 if (xfsread(ino, p, hdr.ex.a_syms))
495 if (xfsread(ino, p, sizeof(int)))
500 if (xfsread(ino, p, x))
505 fs_off = hdr.eh.e_phoff;
506 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
507 if (xfsread(ino, ep + j, sizeof(ep[0])))
509 if (ep[j].p_type == PT_LOAD)
512 for (i = 0; i < 2; i++) {
513 p = PTOV(ep[i].p_paddr & 0xffffff);
514 fs_off = ep[i].p_offset;
515 if (xfsread(ino, p, ep[i].p_filesz))
518 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
519 bootinfo.bi_symtab = VTOP(p);
520 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
521 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
522 (hdr.eh.e_shstrndx + 1);
523 if (xfsread(ino, &es, sizeof(es)))
525 for (i = 0; i < 2; i++) {
526 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
527 p += sizeof(es[i].sh_size);
528 fs_off = es[i].sh_offset;
529 if (xfsread(ino, p, es[i].sh_size))
534 addr = hdr.eh.e_entry & 0xffffff;
536 bootinfo.bi_esymtab = VTOP(p);
537 bootinfo.bi_kernelname = VTOP(kname);
538 bootinfo.bi_bios_dev = dsk.daua;
539 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
540 MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
541 0, 0, 0, VTOP(&bootinfo));
553 while ((c = *arg++)) {
554 if (c == ' ' || c == '\t' || c == '\n')
556 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
561 while ((c = *arg++)) {
563 if (*(uint8_t *)PTOV(0x481) & 0x48) {
566 opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
569 printf("Keyboard: %s\n", cp);
571 } else if (c == 'S') {
573 while ((unsigned int)(i = *arg++ - '0') <= 9)
575 if (j > 0 && i == -'0') {
579 /* Fall through to error below ('S' not in optstr[]). */
581 for (i = 0; c != optstr[i]; i++)
584 opts ^= OPT_SET(flags[i]);
586 ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
587 OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
588 if (ioctrl & IO_SERIAL)
589 sio_init(115200 / comspeed);
591 for (q = arg--; *q && *q != '('; q++);
602 for (i = 0; arg[0] != dev_nm[i][0] ||
603 arg[1] != dev_nm[i][1]; i++)
608 dsk.unit = *arg - '0';
609 if (arg[1] != ',' || dsk.unit > 9)
612 dsk.slice = WHOLE_DISK_SLICE;
614 dsk.slice = *arg - '0' + 1;
615 if (dsk.slice > NDOSPART + 1)
621 dsk.part = *arg - 'a';
627 dsk.disk = dev_daua[dsk.type];
628 dsk.daua = dsk.disk | dsk.unit;
631 if ((i = ep - arg)) {
632 if ((size_t)i >= sizeof(kname))
634 memcpy(kname, arg, i + 1);
643 dskread(void *buf, unsigned lba, unsigned nblk)
645 struct pc98_partition *dp;
652 sec = dmadat->secbuf;
654 if (dsk.type == TYPE_FD)
656 if (drvread(sec, DOSBBSECTOR + 1))
658 dp = (void *)(sec + DOSPARTOFF);
660 if (sl < BASE_SLICE) {
661 for (i = 0; i < NDOSPART; i++)
662 if (dp[i].dp_mid == DOSMID_386BSD) {
668 if (sl != WHOLE_DISK_SLICE) {
669 dp += sl - BASE_SLICE;
670 if (dp->dp_mid != DOSMID_386BSD) {
671 printf("Invalid %s\n", "slice");
674 dsk.start = dp->dp_scyl * dsk.head * dsk.sec +
675 dp->dp_shd * dsk.sec + dp->dp_ssect;
677 if (drvread(sec, dsk.start + LABELSECTOR))
679 d = (void *)(sec + LABELOFFSET);
680 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
681 if (dsk.part != RAW_PART) {
682 printf("Invalid %s\n", "label");
686 if (dsk.part >= d->d_npartitions ||
687 !d->d_partitions[dsk.part].p_size) {
688 printf("Invalid %s\n", "partition");
691 dsk.start += d->d_partitions[dsk.part].p_offset;
692 dsk.start -= d->d_partitions[RAW_PART].p_offset;
696 for (p = buf; nblk; p += 512, lba++, nblk--) {
697 if ((i = drvread(p, dsk.start + lba)))
704 printf(const char *fmt,...)
713 while ((c = *fmt++)) {
718 putchar(va_arg(ap, int));
721 for (s = va_arg(ap, char *); *s; s++)
725 u = va_arg(ap, unsigned);
728 *s++ = '0' + u % 10U;
750 drvread(void *buf, unsigned lba)
752 static unsigned c = 0x2d5c7c2f;
753 unsigned bpc, x, cyl, head, sec;
755 bpc = dsk.sec * dsk.head;
761 if (!OPT_CHECK(RBX_QUIET))
762 printf("%c\b", c = c << 8 | c >> 24);
763 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
764 v86.addr = READORG; /* call to read in boot1 */
766 v86.edx = (head << 8) | sec;
769 v86.es = VTOPSEG(buf);
770 v86.ebp = VTOPOFF(buf);
773 if (V86_CY(v86.efl)) {
774 printf("error %u c/h/s %u/%u/%u lba %u\n", v86.eax >> 8 & 0xff,
775 cyl, head, sec, lba);
788 outb(0x5f, 0); /* about 600ns */
797 if (OPT_CHECK(RBX_NOINTR))
799 for (i = 0; i < sec * 1000; i++) {
810 if (ioctrl & IO_KEYBOARD)
812 if (ioctrl & IO_SERIAL)
820 if (OPT_CHECK(RBX_NOINTR))
823 if (ioctrl & IO_KEYBOARD && getc(1))
824 return fn ? 1 : getc(0);
825 if (ioctrl & IO_SERIAL && sio_ischar())
826 return fn ? 1 : sio_getc();
839 return (v86.ebx >> 8) & 0x01;
841 return v86.eax & 0xff;