boot2.c revision 226746
1/*-
2 * Copyright (c) 2008-2009 TAKAHASHI Yoshihiro
3 * Copyright (c) 1998 Robert Nordier
4 * All rights reserved.
5 *
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
9 * such forms.
10 *
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
14 * purpose.
15 */
16
17#include <sys/cdefs.h>
18__FBSDID("$FreeBSD: head/sys/boot/pc98/boot2/boot2.c 226746 2011-10-25 19:45:12Z jhb $");
19
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>
25
26#include <machine/bootinfo.h>
27#include <machine/cpufunc.h>
28#include <machine/elf.h>
29
30#include <stdarg.h>
31
32#include <a.out.h>
33
34#include <btxv86.h>
35
36#include "boot2.h"
37#include "lib.h"
38
39#define IO_KEYBOARD	1
40#define IO_SERIAL	2
41
42#define SECOND		1	/* Circa that many ticks in a second. */
43
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). */
70
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))
78
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"
83
84#define ARGS		0x900
85#define NOPT		14
86#define NDEV		3
87
88#define DRV_DISK	0xf0
89#define DRV_UNIT	0x0f
90
91#define TYPE_AD		0
92#define TYPE_DA		1
93#define TYPE_FD		2
94
95#define OPT_SET(opt)	(1 << (opt))
96#define OPT_CHECK(opt)	((opts) & OPT_SET(opt))
97
98extern uint32_t _end;
99
100static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
101static const unsigned char flags[NOPT] = {
102    RBX_DUAL,
103    RBX_SERIAL,
104    RBX_ASKNAME,
105    RBX_CDROM,
106    RBX_CONFIG,
107    RBX_KDB,
108    RBX_GDB,
109    RBX_MUTE,
110    RBX_NOINTR,
111    RBX_PAUSE,
112    RBX_QUIET,
113    RBX_DFLTROOT,
114    RBX_SINGLE,
115    RBX_VERBOSE
116};
117
118static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
119static const unsigned char dev_maj[NDEV] = {30, 4, 2};
120static const unsigned char dev_daua[NDEV] = {0x80, 0xa0, 0x90};
121
122static struct dsk {
123    unsigned daua;
124    unsigned type;
125    unsigned disk;
126    unsigned unit;
127    unsigned head;
128    unsigned sec;
129    unsigned slice;
130    unsigned part;
131    unsigned start;
132} dsk;
133static char cmd[512], cmddup[512];
134static const char *kname = NULL;
135static uint32_t opts;
136static int comspeed = SIOSPD;
137static struct bootinfo bootinfo;
138static uint8_t ioctrl = IO_KEYBOARD;
139
140void exit(int);
141static void load(void);
142static int parse(void);
143static int xfsread(ino_t, void *, size_t);
144static int dskread(void *, unsigned, unsigned);
145static void printf(const char *,...);
146static void putchar(int);
147static int drvread(void *, unsigned);
148static int keyhit(unsigned);
149static int xputc(int);
150static int xgetc(int);
151static inline int getc(int);
152
153static void memcpy(void *, const void *, int);
154static void
155memcpy(void *dst, const void *src, int len)
156{
157    const char *s = src;
158    char *d = dst;
159
160    while (len--)
161        *d++ = *s++;
162}
163
164static inline int
165strcmp(const char *s1, const char *s2)
166{
167    for (; *s1 == *s2 && *s1; s1++, s2++);
168    return (unsigned char)*s1 - (unsigned char)*s2;
169}
170
171#define	UFS_SMALL_CGBASE
172#include "ufsread.c"
173
174static inline int
175xfsread(ino_t inode, void *buf, size_t nbyte)
176{
177    if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
178	printf("Invalid %s\n", "format");
179	return -1;
180    }
181    return 0;
182}
183
184static inline void
185getstr(void)
186{
187    char *s;
188    int c;
189
190    s = cmd;
191    for (;;) {
192	switch (c = xgetc(0)) {
193	case 0:
194	    break;
195	case '\177':
196	case '\b':
197	    if (s > cmd) {
198		s--;
199		printf("\b \b");
200	    }
201	    break;
202	case '\n':
203	case '\r':
204	    *s = 0;
205	    return;
206	default:
207	    if (s - cmd < sizeof(cmd) - 1)
208		*s++ = c;
209	    putchar(c);
210	}
211    }
212}
213
214static inline void
215putc(int c)
216{
217
218    v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
219    v86.addr = PUTCORG;		/* call to putc in boot1 */
220    v86.eax = c;
221    v86int();
222    v86.ctl = V86_FLAGS;
223}
224
225static inline int
226is_scsi_hd(void)
227{
228
229    if ((*(u_char *)PTOV(0x482) >> dsk.unit) & 0x01)
230	return 1;
231
232    return 0;
233}
234
235static inline void
236fix_sector_size(void)
237{
238    u_char *p;
239
240    p = (u_char *)PTOV(0x460 + dsk.unit * 4);	/* SCSI equipment parameter */
241
242    if ((p[0] & 0x1f) == 7) {		/* SCSI MO */
243	if (!(p[3] & 0x30)) {		/* 256B / sector */
244	    p[3] |= 0x10;		/* forced set 512B / sector */
245	    p[3 + 0xa1000] |= 0x10;
246	}
247    }
248}
249
250static inline uint32_t
251get_diskinfo(void)
252{
253
254    if (dsk.disk == 0x30) {				/* 1440KB FD */
255	/* 80 cylinders, 2 heads, 18 sectors */
256	return (80 << 16) | (2 << 8) | 18;
257    } else if (dsk.disk == 0x90) {			/* 1200KB FD */
258	/* 80 cylinders, 2 heads, 15 sectors */
259	return (80 << 16) | (2 << 8) | 15;
260    } else if (dsk.disk == 0x80 || is_scsi_hd()) {	/* IDE or SCSI HDD */
261	v86.addr = 0x1b;
262	v86.eax = 0x8400 | dsk.daua;
263	v86int();
264	return (v86.ecx << 16) | v86.edx;
265    }
266
267    /* SCSI MO or CD */
268    fix_sector_size();	/* SCSI MO */
269
270    /* other SCSI devices */
271    return (65535 << 16) | (8 << 8) | 32;
272}
273
274static void
275set_dsk(void)
276{
277    uint32_t di;
278
279    di = get_diskinfo();
280
281    dsk.head = (di >> 8) & 0xff;
282    dsk.sec = di & 0xff;
283    dsk.start = 0;
284}
285
286#ifdef GET_BIOSGEOM
287static uint32_t
288bd_getbigeom(int bunit)
289{
290    int hds = 0;
291    int unit = 0x80;		/* IDE HDD */
292    u_int addr = 0x55d;
293
294    while (unit < 0xa7) {
295	if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
296	    if (hds++ == bunit)
297		break;
298
299	if (unit >= 0xA0) {
300	    int media = ((unsigned *)PTOV(0x460))[unit & 0x0F] & 0x1F;
301
302	    if (media == 7 && hds++ == bunit)	/* SCSI MO */
303		return(0xFFFE0820); /* C:65535 H:8 S:32 */
304	}
305	if (++unit == 0x84) {
306	    unit = 0xA0;	/* SCSI HDD */
307	    addr = 0x482;
308	}
309    }
310    if (unit == 0xa7)
311	return 0x4F020F;	/* 1200KB FD C:80 H:2 S:15 */
312    v86.addr = 0x1b;
313    v86.eax = 0x8400 | unit;
314    v86int();
315    if (v86.efl & 0x1)
316	return 0x4F020F;	/* 1200KB FD C:80 H:2 S:15 */
317    return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
318}
319#endif
320
321static int
322check_slice(void)
323{
324    struct pc98_partition *dp;
325    char *sec;
326    unsigned i, cyl;
327
328    sec = dmadat->secbuf;
329    cyl = *(uint16_t *)PTOV(ARGS);
330    set_dsk();
331
332    if (dsk.type == TYPE_FD)
333	return (WHOLE_DISK_SLICE);
334    if (drvread(sec, DOSBBSECTOR + 1))
335	return (WHOLE_DISK_SLICE);	/* Read error */
336    dp = (void *)(sec + DOSPARTOFF);
337    for (i = 0; i < NDOSPART; i++) {
338	if (dp[i].dp_mid == DOSMID_386BSD) {
339	    if (dp[i].dp_scyl <= cyl && cyl <= dp[i].dp_ecyl)
340		return (BASE_SLICE + i);
341	}
342    }
343
344    return (WHOLE_DISK_SLICE);
345}
346
347int
348main(void)
349{
350#ifdef GET_BIOSGEOM
351    int i;
352#endif
353    uint8_t autoboot;
354    ino_t ino;
355
356    dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
357    v86.ctl = V86_FLAGS;
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)
363        dsk.type = TYPE_AD;
364    else if (dsk.disk == 0xa0)
365        dsk.type = TYPE_DA;
366    else /* if (dsk.disk == 0x30 || dsk.disk == 0x90) */
367        dsk.type = TYPE_FD;
368    dsk.slice = check_slice();
369#ifdef GET_BIOSGEOM
370    for (i = 0; i < N_BIOS_GEOM; i++)
371	bootinfo.bi_bios_geom[i] = bd_getbigeom(i);
372#endif
373    bootinfo.bi_version = BOOTINFO_VERSION;
374    bootinfo.bi_size = sizeof(bootinfo);
375
376    /* Process configuration file */
377
378    autoboot = 1;
379
380    if ((ino = lookup(PATH_CONFIG)) ||
381        (ino = lookup(PATH_DOTCONFIG)))
382	fsread(ino, cmd, sizeof(cmd));
383
384    if (*cmd) {
385	memcpy(cmddup, cmd, sizeof(cmd));
386	if (parse())
387	    autoboot = 0;
388	if (!OPT_CHECK(RBX_QUIET))
389	    printf("%s: %s", PATH_CONFIG, cmddup);
390	/* Do not process this command twice */
391	*cmd = 0;
392    }
393
394    /*
395     * Try to exec stage 3 boot loader. If interrupted by a keypress,
396     * or in case of failure, try to load a kernel directly instead.
397     */
398
399    if (autoboot && !kname) {
400	kname = PATH_BOOT3;
401	if (!keyhit(3*SECOND)) {
402	    load();
403	    kname = PATH_KERNEL;
404	}
405    }
406
407    /* Present the user with the boot2 prompt. */
408
409    for (;;) {
410	if (!autoboot || !OPT_CHECK(RBX_QUIET))
411	    printf("\nFreeBSD/pc98 boot\n"
412		   "Default: %u:%s(%u,%c)%s\n"
413		   "boot: ",
414		   dsk.unit, dev_nm[dsk.type], dsk.unit,
415		   'a' + dsk.part, kname);
416	if (ioctrl & IO_SERIAL)
417	    sio_flush();
418	if (!autoboot || keyhit(3*SECOND))
419	    getstr();
420	else if (!autoboot || !OPT_CHECK(RBX_QUIET))
421	    putchar('\n');
422	autoboot = 0;
423	if (parse())
424	    putchar('\a');
425	else
426	    load();
427    }
428}
429
430/* XXX - Needed for btxld to link the boot2 binary; do not remove. */
431void
432exit(int x)
433{
434}
435
436static void
437load(void)
438{
439    union {
440	struct exec ex;
441	Elf32_Ehdr eh;
442    } hdr;
443    static Elf32_Phdr ep[2];
444    static Elf32_Shdr es[2];
445    caddr_t p;
446    ino_t ino;
447    uint32_t addr;
448    int i, j;
449
450    if (!(ino = lookup(kname))) {
451	if (!ls)
452	    printf("No %s\n", kname);
453	return;
454    }
455    if (xfsread(ino, &hdr, sizeof(hdr)))
456	return;
457
458    if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
459	addr = hdr.ex.a_entry & 0xffffff;
460	p = PTOV(addr);
461	fs_off = PAGE_SIZE;
462	if (xfsread(ino, p, hdr.ex.a_text))
463	    return;
464	p += roundup2(hdr.ex.a_text, PAGE_SIZE);
465	if (xfsread(ino, p, hdr.ex.a_data))
466	    return;
467    } else if (IS_ELF(hdr.eh)) {
468	fs_off = hdr.eh.e_phoff;
469	for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
470	    if (xfsread(ino, ep + j, sizeof(ep[0])))
471		return;
472	    if (ep[j].p_type == PT_LOAD)
473		j++;
474	}
475	for (i = 0; i < 2; i++) {
476	    p = PTOV(ep[i].p_paddr & 0xffffff);
477	    fs_off = ep[i].p_offset;
478	    if (xfsread(ino, p, ep[i].p_filesz))
479		return;
480	}
481	p += roundup2(ep[1].p_memsz, PAGE_SIZE);
482	bootinfo.bi_symtab = VTOP(p);
483	if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
484	    fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
485		(hdr.eh.e_shstrndx + 1);
486	    if (xfsread(ino, &es, sizeof(es)))
487		return;
488	    for (i = 0; i < 2; i++) {
489		*(Elf32_Word *)p = es[i].sh_size;
490		p += sizeof(es[i].sh_size);
491		fs_off = es[i].sh_offset;
492		if (xfsread(ino, p, es[i].sh_size))
493		    return;
494		p += es[i].sh_size;
495	    }
496	}
497	addr = hdr.eh.e_entry & 0xffffff;
498	bootinfo.bi_esymtab = VTOP(p);
499    } else {
500	printf("Invalid %s\n", "format");
501	return;
502    }
503
504    bootinfo.bi_kernelname = VTOP(kname);
505    bootinfo.bi_bios_dev = dsk.daua;
506    __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
507	   MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
508	   0, 0, 0, VTOP(&bootinfo));
509}
510
511static int
512parse()
513{
514    char *arg = cmd;
515    char *ep, *p, *q;
516    const char *cp;
517    unsigned int drv;
518    int c, i, j;
519
520    while ((c = *arg++)) {
521	if (c == ' ' || c == '\t' || c == '\n')
522	    continue;
523	for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
524	ep = p;
525	if (*p)
526	    *p++ = 0;
527	if (c == '-') {
528	    while ((c = *arg++)) {
529		if (c == 'P') {
530		    if (*(uint8_t *)PTOV(0x481) & 0x48) {
531			cp = "yes";
532		    } else {
533			opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
534			cp = "no";
535		    }
536		    printf("Keyboard: %s\n", cp);
537		    continue;
538		} else if (c == 'S') {
539		    j = 0;
540		    while ((unsigned int)(i = *arg++ - '0') <= 9)
541			j = j * 10 + i;
542		    if (j > 0 && i == -'0') {
543			comspeed = j;
544			break;
545		    }
546		    /* Fall through to error below ('S' not in optstr[]). */
547		}
548		for (i = 0; c != optstr[i]; i++)
549		    if (i == NOPT - 1)
550			return -1;
551		opts ^= OPT_SET(flags[i]);
552	    }
553	    ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
554		     OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
555	    if (ioctrl & IO_SERIAL)
556	        sio_init(115200 / comspeed);
557	} else {
558	    for (q = arg--; *q && *q != '('; q++);
559	    if (*q) {
560		drv = -1;
561		if (arg[1] == ':') {
562		    drv = *arg - '0';
563		    if (drv > 9)
564			return (-1);
565		    arg += 2;
566		}
567		if (q - arg != 2)
568		    return -1;
569		for (i = 0; arg[0] != dev_nm[i][0] ||
570			    arg[1] != dev_nm[i][1]; i++)
571		    if (i == NDEV - 1)
572			return -1;
573		dsk.type = i;
574		arg += 3;
575		dsk.unit = *arg - '0';
576		if (arg[1] != ',' || dsk.unit > 9)
577		    return -1;
578		arg += 2;
579		dsk.slice = WHOLE_DISK_SLICE;
580		if (arg[1] == ',') {
581		    dsk.slice = *arg - '0' + 1;
582		    if (dsk.slice > NDOSPART + 1)
583			return -1;
584		    arg += 2;
585		}
586		if (arg[1] != ')')
587		    return -1;
588		dsk.part = *arg - 'a';
589		if (dsk.part > 7)
590		    return (-1);
591		arg += 2;
592		if (drv == -1)
593		    drv = dsk.unit;
594		dsk.disk = dev_daua[dsk.type];
595		dsk.daua = dsk.disk | dsk.unit;
596		dsk_meta = 0;
597	    }
598            kname = arg;
599	}
600	arg = p;
601    }
602    return 0;
603}
604
605static int
606dskread(void *buf, unsigned lba, unsigned nblk)
607{
608    struct pc98_partition *dp;
609    struct disklabel *d;
610    char *sec;
611    unsigned sl, i;
612    u_char *p;
613
614    if (!dsk_meta) {
615	sec = dmadat->secbuf;
616	set_dsk();
617	if (dsk.type == TYPE_FD)
618	    goto unsliced;
619	if (drvread(sec, DOSBBSECTOR + 1))
620	    return -1;
621	dp = (void *)(sec + DOSPARTOFF);
622	sl = dsk.slice;
623	if (sl < BASE_SLICE) {
624	    for (i = 0; i < NDOSPART; i++)
625		if (dp[i].dp_mid == DOSMID_386BSD) {
626		    sl = BASE_SLICE + i;
627		    break;
628		}
629	    dsk.slice = sl;
630	}
631	if (sl != WHOLE_DISK_SLICE) {
632	    dp += sl - BASE_SLICE;
633	    if (dp->dp_mid != DOSMID_386BSD) {
634		printf("Invalid %s\n", "slice");
635		return -1;
636	    }
637	    dsk.start = dp->dp_scyl * dsk.head * dsk.sec +
638		dp->dp_shd * dsk.sec + dp->dp_ssect;
639	}
640	if (drvread(sec, dsk.start + LABELSECTOR))
641		return -1;
642	d = (void *)(sec + LABELOFFSET);
643	if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
644	    if (dsk.part != RAW_PART) {
645		printf("Invalid %s\n", "label");
646		return -1;
647	    }
648	} else {
649	    if (dsk.part >= d->d_npartitions ||
650		!d->d_partitions[dsk.part].p_size) {
651		printf("Invalid %s\n", "partition");
652		return -1;
653	    }
654	    dsk.start += d->d_partitions[dsk.part].p_offset;
655	    dsk.start -= d->d_partitions[RAW_PART].p_offset;
656	}
657    unsliced: ;
658    }
659    for (p = buf; nblk; p += 512, lba++, nblk--) {
660	if ((i = drvread(p, dsk.start + lba)))
661	    return i;
662    }
663    return 0;
664}
665
666static void
667printf(const char *fmt,...)
668{
669    va_list ap;
670    static char buf[10];
671    char *s;
672    unsigned u;
673    int c;
674
675    va_start(ap, fmt);
676    while ((c = *fmt++)) {
677	if (c == '%') {
678	    c = *fmt++;
679	    switch (c) {
680	    case 'c':
681		putchar(va_arg(ap, int));
682		continue;
683	    case 's':
684		for (s = va_arg(ap, char *); *s; s++)
685		    putchar(*s);
686		continue;
687	    case 'u':
688		u = va_arg(ap, unsigned);
689		s = buf;
690		do
691		    *s++ = '0' + u % 10U;
692		while (u /= 10U);
693		while (--s >= buf)
694		    putchar(*s);
695		continue;
696	    }
697	}
698	putchar(c);
699    }
700    va_end(ap);
701    return;
702}
703
704static void
705putchar(int c)
706{
707    if (c == '\n')
708	xputc('\r');
709    xputc(c);
710}
711
712static int
713drvread(void *buf, unsigned lba)
714{
715    static unsigned c = 0x2d5c7c2f;
716    unsigned bpc, x, cyl, head, sec;
717
718    bpc = dsk.sec * dsk.head;
719    cyl = lba / bpc;
720    x = lba % bpc;
721    head = x / dsk.sec;
722    sec = x % dsk.sec;
723
724    if (!OPT_CHECK(RBX_QUIET))
725	printf("%c\b", c = c << 8 | c >> 24);
726    v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
727    v86.addr = READORG;		/* call to read in boot1 */
728    v86.ecx = cyl;
729    v86.edx = (head << 8) | sec;
730    v86.edi = lba;
731    v86.ebx = 512;
732    v86.es = VTOPSEG(buf);
733    v86.ebp = VTOPOFF(buf);
734    v86int();
735    v86.ctl = V86_FLAGS;
736    if (V86_CY(v86.efl)) {
737	printf("error %u c/h/s %u/%u/%u lba %u\n", v86.eax >> 8 & 0xff,
738	       cyl, head, sec, lba);
739	return -1;
740    }
741    return 0;
742}
743
744static inline void
745delay(void)
746{
747    int i;
748
749    i = 800;
750    do {
751	outb(0x5f, 0);	/* about 600ns */
752    } while (--i >= 0);
753}
754
755static int
756keyhit(unsigned sec)
757{
758    unsigned i;
759
760    if (OPT_CHECK(RBX_NOINTR))
761	return 0;
762    for (i = 0; i < sec * 1000; i++) {
763	if (xgetc(1))
764	    return 1;
765	delay();
766    }
767    return 0;
768}
769
770static int
771xputc(int c)
772{
773    if (ioctrl & IO_KEYBOARD)
774	putc(c);
775    if (ioctrl & IO_SERIAL)
776	sio_putc(c);
777    return c;
778}
779
780static int
781getc(int fn)
782{
783    v86.addr = 0x18;
784    v86.eax = fn << 8;
785    v86int();
786    if (fn)
787	return (v86.ebx >> 8) & 0x01;
788    else
789	return v86.eax & 0xff;
790}
791
792static int
793xgetc(int fn)
794{
795    if (OPT_CHECK(RBX_NOINTR))
796	return 0;
797    for (;;) {
798	if (ioctrl & IO_KEYBOARD && getc(1))
799	    return fn ? 1 : getc(0);
800	if (ioctrl & IO_SERIAL && sio_ischar())
801	    return fn ? 1 : sio_getc();
802	if (fn)
803	    return 0;
804    }
805}
806