Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / boot / i386 / boot2 / boot2.c
1 /*
2  * Copyright (c) 1998 Robert Nordier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are freely
6  * permitted provided that the above copyright notice and this
7  * paragraph and the following disclaimer are duplicated in all
8  * such forms.
9  *
10  * This software is provided "AS IS" and without any express or
11  * implied warranties, including, without limitation, the implied
12  * warranties of merchantability and fitness for a particular
13  * purpose.
14  */
15
16 /*
17  * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.28.2.7 2002/10/10 15:53:24 iwasaki Exp $
18  */
19
20 #include <sys/param.h>
21 #include <sys/reboot.h>
22 #include <sys/diskslice.h>
23 #include <sys/disklabel.h>
24 #include <sys/dirent.h>
25 #include <machine/bootinfo.h>
26 #include <machine/elf.h>
27
28 #include <ufs/ffs/fs.h>
29 #include <ufs/ufs/dinode.h>
30
31 #include <stdarg.h>
32
33 #include <a.out.h>
34
35 #include <btxv86.h>
36
37 #include "boot2.h"
38 #include "lib.h"
39
40 #define RBX_ASKNAME     0x0     /* -a */
41 #define RBX_SINGLE      0x1     /* -s */
42 #define RBX_DFLTROOT    0x5     /* -r */
43 #define RBX_KDB         0x6     /* -d */
44 #define RBX_CONFIG      0xa     /* -c */
45 #define RBX_VERBOSE     0xb     /* -v */
46 #define RBX_SERIAL      0xc     /* -h */
47 #define RBX_CDROM       0xd     /* -C */
48 #define RBX_GDB         0xf     /* -g */
49 #define RBX_DUAL        0x1d    /* -D */
50 #define RBX_PROBEKBD    0x1e    /* -P */
51 #define RBX_NOINTR      0x1f    /* -n */
52
53 #define RBX_MASK        0xffff
54
55 #define PATH_CONFIG     "/boot.config"
56 #define PATH_BOOT3      "/boot/loader"
57 #define PATH_KERNEL     "/kernel"
58
59 #define ARGS            0x900
60 #define NOPT            12
61 #define BSIZEMAX        16384
62 #define NDEV            5
63 #define MEM_BASE        0x12
64 #define MEM_EXT         0x15
65 #define V86_CY(x)       ((x) & 1)
66 #define V86_ZR(x)       ((x) & 0x40)
67
68 #define DRV_HARD        0x80
69 #define DRV_MASK        0x7f
70
71 #define TYPE_AD         0
72 #define TYPE_WD         1
73 #define TYPE_WFD        2
74 #define TYPE_FD         3
75 #define TYPE_DA         4
76
77 extern uint32_t _end;
78
79 static const char optstr[NOPT] = "DhaCcdgnPrsv";
80 static const unsigned char flags[NOPT] = {
81     RBX_DUAL,
82     RBX_SERIAL,
83     RBX_ASKNAME,
84     RBX_CDROM,
85     RBX_CONFIG,
86     RBX_KDB,
87     RBX_GDB,
88     RBX_NOINTR,
89     RBX_PROBEKBD,
90     RBX_DFLTROOT,
91     RBX_SINGLE,
92     RBX_VERBOSE
93 };
94
95 static const char *const dev_nm[] = {"ad", "wd", "  ", "fd", "da"};
96 static const unsigned dev_maj[] = {30, 0, 1, 2, 4};
97
98 static struct dsk {
99     unsigned drive;
100     unsigned type;
101     unsigned unit;
102     unsigned slice;
103     unsigned part;
104     unsigned start;
105     int init;
106     int meta;
107 } dsk;
108 static char cmd[512];
109 static char kname[1024];
110 static uint32_t opts;
111 static struct bootinfo bootinfo;
112 static int ls;
113 static uint32_t fs_off;
114 static uint8_t ioctrl = 0x1;
115
116 void exit(int);
117 static void load(const char *);
118 static int parse(char *);
119 static ino_t lookup(const char *);
120 static int xfsread(ino_t, void *, size_t);
121 static ssize_t fsread(ino_t, void *, size_t);
122 static int dskread(void *, unsigned, unsigned);
123 static int printf(const char *,...);
124 static int putchar(int);
125 static void *memcpy(void *, const void *, size_t);
126 static void *malloc(size_t);
127 static uint32_t memsize(int);
128 static int drvread(void *, unsigned, unsigned);
129 static int keyhit(unsigned);
130 static int xputc(int);
131 static int xgetc(int);
132 static int getc(int);
133
134 static inline void
135 readfile(const char *fname, void *buf, size_t size)
136 {
137     ino_t ino;
138
139     if ((ino = lookup(fname)))
140         fsread(ino, buf, size);
141 }
142
143 static inline int
144 strcmp(const char *s1, const char *s2)
145 {
146     for (; *s1 == *s2 && *s1; s1++, s2++);
147     return (u_char)*s1 - (u_char)*s2;
148 }
149
150 static inline int
151 fsfind(const char *name, ino_t * ino)
152 {
153     char buf[DEV_BSIZE];
154     struct dirent *d;
155     char *s;
156     ssize_t n;
157
158     fs_off = 0;
159     while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
160         for (s = buf; s < buf + DEV_BSIZE;) {
161             d = (void *)s;
162             if (ls)
163                 printf("%s ", d->d_name);
164             else if (!strcmp(name, d->d_name)) {
165                 *ino = d->d_fileno;
166                 return d->d_type;
167             }
168             s += d->d_reclen;
169         }
170     if (n != -1 && ls)
171         putchar('\n');
172     return 0;
173 }
174
175 static inline int
176 getchar(void)
177 {
178     int c;
179
180     c = xgetc(0);
181     if (c == '\r')
182         c = '\n';
183     return c;
184 }
185
186 static inline void
187 getstr(char *str, int size)
188 {
189     char *s;
190     int c;
191
192     s = str;
193     do {
194         switch (c = getchar()) {
195         case 0:
196             break;
197         case '\b':
198         case '\177':
199             if (s > str) {
200                 s--;
201                 putchar('\b');
202                 putchar(' ');
203             } else
204                 c = 0;
205             break;
206         case '\n':
207             *s = 0;
208             break;
209         default:
210             if (s - str < size - 1)
211                 *s++ = c;
212         }
213         if (c)
214             putchar(c);
215     } while (c != '\n');
216 }
217
218 static inline uint32_t
219 drvinfo(int drive)
220 {
221     v86.addr = 0x13;
222     v86.eax = 0x800;
223     v86.edx = DRV_HARD + drive;
224     v86int();
225     if (V86_CY(v86.efl))
226         return 0x4f010f;
227     return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
228            (v86.edx & 0xff00) | (v86.ecx & 0x3f);
229 }
230
231 static inline void
232 putc(int c)
233 {
234     v86.addr = 0x10;
235     v86.eax = 0xe00 | (c & 0xff);
236     v86.ebx = 0x7;
237     v86int();
238 }
239
240 int
241 main(void)
242 {
243     int autoboot, i;
244
245     v86.ctl = V86_FLAGS;
246     dsk.drive = *(uint8_t *)PTOV(ARGS);
247     dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
248     dsk.unit = dsk.drive & DRV_MASK;
249     dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
250     bootinfo.bi_version = BOOTINFO_VERSION;
251     bootinfo.bi_size = sizeof(bootinfo);
252     bootinfo.bi_basemem = 0;    /* XXX will be filled by loader or kernel */
253     bootinfo.bi_extmem = memsize(MEM_EXT);
254     bootinfo.bi_memsizes_valid++;
255     for (i = 0; i < N_BIOS_GEOM; i++)
256         bootinfo.bi_bios_geom[i] = drvinfo(i);
257     autoboot = 2;
258     readfile(PATH_CONFIG, cmd, sizeof(cmd));
259     if (*cmd) {
260         printf("%s: %s", PATH_CONFIG, cmd);
261         if (parse(cmd))
262             autoboot = 0;
263         *cmd = 0;
264     }
265     if (autoboot && !*kname) {
266         if (autoboot == 2) {
267             memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
268             if (!keyhit(0x37)) {
269                 load(kname);
270                 autoboot = 1;
271             }
272         }
273         if (autoboot == 1)
274             memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
275     }
276     for (;;) {
277         printf(" \n>> FreeBSD/i386 BOOT\n"
278                "Default: %u:%s(%u,%c)%s\n"
279                "boot: ",
280                dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
281                'a' + dsk.part, kname);
282         if (ioctrl & 0x2)
283             sio_flush();
284         if (!autoboot || keyhit(0x5a))
285             getstr(cmd, sizeof(cmd));
286         else
287             putchar('\n');
288         autoboot = 0;
289         if (parse(cmd))
290             putchar('\a'); 
291         else
292             load(kname);
293     }
294 }
295
296 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
297 void
298 exit(int x)
299 {
300 }
301
302 static void
303 load(const char *fname)
304 {
305     union {
306         struct exec ex;
307         Elf32_Ehdr eh;
308     } hdr;
309     Elf32_Phdr ep[2];
310     Elf32_Shdr es[2];
311     caddr_t p;
312     ino_t ino;
313     uint32_t addr, x;
314     int fmt, i, j;
315
316     if (!(ino = lookup(fname))) {
317         if (!ls)
318             printf("No %s\n", fname);
319         return;
320     }
321     if (xfsread(ino, &hdr, sizeof(hdr)))
322         return;
323     if (N_GETMAGIC(hdr.ex) == ZMAGIC)
324         fmt = 0;
325     else if (IS_ELF(hdr.eh))
326         fmt = 1;
327     else {
328         printf("Invalid %s\n", "format");
329         return;
330     }
331     if (fmt == 0) {
332         addr = hdr.ex.a_entry & 0xffffff;
333         p = PTOV(addr);
334         fs_off = PAGE_SIZE;
335         if (xfsread(ino, p, hdr.ex.a_text))
336             return;
337         p += roundup2(hdr.ex.a_text, PAGE_SIZE);
338         if (xfsread(ino, p, hdr.ex.a_data))
339             return;
340         p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
341         bootinfo.bi_symtab = VTOP(p);
342         memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
343         p += sizeof(hdr.ex.a_syms);
344         if (hdr.ex.a_syms) {
345             if (xfsread(ino, p, hdr.ex.a_syms))
346                 return;
347             p += hdr.ex.a_syms;
348             if (xfsread(ino, p, sizeof(int)))
349                 return;
350             x = *(uint32_t *)p;
351             p += sizeof(int);
352             x -= sizeof(int);
353             if (xfsread(ino, p, x))
354                 return;
355             p += x;
356         }
357     } else {
358         fs_off = hdr.eh.e_phoff;
359         for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
360             if (xfsread(ino, ep + j, sizeof(ep[0])))
361                 return;
362             if (ep[j].p_type == PT_LOAD)
363                 j++;
364         }
365         for (i = 0; i < 2; i++) {
366             p = PTOV(ep[i].p_paddr & 0xffffff);
367             fs_off = ep[i].p_offset;
368             if (xfsread(ino, p, ep[i].p_filesz))
369                 return;
370         }
371         p += roundup2(ep[1].p_memsz, PAGE_SIZE);
372         bootinfo.bi_symtab = VTOP(p);
373         if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
374             fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
375                 (hdr.eh.e_shstrndx + 1);
376             if (xfsread(ino, &es, sizeof(es)))
377                 return;
378             for (i = 0; i < 2; i++) {
379                 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
380                 p += sizeof(es[i].sh_size);
381                 fs_off = es[i].sh_offset;
382                 if (xfsread(ino, p, es[i].sh_size))
383                     return;
384                 p += es[i].sh_size;
385             }
386         }
387         addr = hdr.eh.e_entry & 0xffffff;
388     }
389     bootinfo.bi_esymtab = VTOP(p);
390     bootinfo.bi_kernelname = VTOP(fname);
391     bootinfo.bi_bios_dev = dsk.drive;
392     __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
393            MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
394            0, 0, 0, VTOP(&bootinfo));
395 }
396
397 static int
398 parse(char *arg)
399 {
400     char *p, *q;
401     int drv, c, i;
402
403     while ((c = *arg++)) {
404         if (c == ' ' || c == '\t' || c == '\n')
405             continue;
406         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
407         if (*p)
408             *p++ = 0;
409         if (c == '-') {
410             while ((c = *arg++)) {
411                 for (i = 0; c != optstr[i]; i++)
412                     if (i == NOPT - 1)
413                         return -1;
414                 opts ^= 1 << flags[i];
415             }
416             if (opts & 1 << RBX_PROBEKBD) {
417                 i = *(uint8_t *)PTOV(0x496) & 0x10;
418                 printf("Keyboard: %s\n", i ? "yes" : "no");
419                 if (!i)
420                     opts |= 1 << RBX_DUAL | 1 << RBX_SERIAL;
421                 opts &= ~(1 << RBX_PROBEKBD);
422             }
423             ioctrl = opts & 1 << RBX_DUAL ? 0x3 :
424                      opts & 1 << RBX_SERIAL ? 0x2 : 0x1;
425             if (ioctrl & 0x2)
426                 sio_init();
427         } else {
428             for (q = arg--; *q && *q != '('; q++);
429             if (*q) {
430                 drv = -1;
431                 if (arg[1] == ':') {
432                     if (*arg < '0' || *arg > '9')
433                         return -1;
434                     drv = *arg - '0';
435                     arg += 2;
436                 }
437                 if (q - arg != 2)
438                     return -1;
439                 for (i = 0; arg[0] != dev_nm[i][0] ||
440                             arg[1] != dev_nm[i][1]; i++)
441                     if (i == NDEV - 1)
442                         return -1;
443                 dsk.type = i;
444                 arg += 3;
445                 if (arg[1] != ',' || *arg < '0' || *arg > '9')
446                     return -1;
447                 dsk.unit = *arg - '0';
448                 arg += 2;
449                 dsk.slice = WHOLE_DISK_SLICE;
450                 if (arg[1] == ',') {
451                     if (*arg < '0' || *arg > '0' + NDOSPART)
452                         return -1;
453                     if ((dsk.slice = *arg - '0'))
454                         dsk.slice++;
455                     arg += 2;
456                 }
457                 if (arg[1] != ')' || *arg < 'a' || *arg > 'p')
458                     return -1;
459                 dsk.part = *arg - 'a';
460                 arg += 2;
461                 if (drv == -1)
462                     drv = dsk.unit;
463                 dsk.drive = (dsk.type == TYPE_WD ||
464                              dsk.type == TYPE_AD ||
465                              dsk.type == TYPE_DA ? DRV_HARD : 0) + drv;
466                 dsk.meta = 0;
467                 fsread(0, NULL, 0);
468             }
469             if ((i = p - arg - !*(p - 1))) {
470                 if (i >= sizeof(kname))
471                     return -1;
472                 memcpy(kname, arg, i + 1);
473             }
474         }
475         arg = p;
476     }
477     return 0;
478 }
479
480 static ino_t
481 lookup(const char *path)
482 {
483     char name[MAXNAMLEN + 1];
484     const char *s;
485     ino_t ino;
486     ssize_t n;
487     int dt;
488
489     ino = ROOTINO;
490     dt = DT_DIR;
491     for (;;) {
492         if (*path == '/')
493             path++;
494         if (!*path)
495             break;
496         for (s = path; *s && *s != '/'; s++);
497         if ((n = s - path) > MAXNAMLEN)
498             return 0;
499         ls = *path == '?' && n == 1 && !*s;
500         memcpy(name, path, n);
501         name[n] = 0;
502         if ((dt = fsfind(name, &ino)) <= 0)
503             break;
504         path = s;
505     }
506     return dt == DT_REG ? ino : 0;
507 }
508 static int
509 xfsread(ino_t inode, void *buf, size_t nbyte)
510 {
511     if (fsread(inode, buf, nbyte) != nbyte) {
512         printf("Invalid %s\n", "format");
513         return -1;
514     }
515     return 0;
516 }
517
518 static ssize_t
519 fsread(ino_t inode, void *buf, size_t nbyte)
520 {
521     static struct fs fs;
522     static struct dinode din;
523     static char *blkbuf;
524     static ufs_daddr_t *indbuf;
525     static ino_t inomap;
526     static ufs_daddr_t blkmap, indmap;
527     static unsigned fsblks;
528     char *s;
529     ufs_daddr_t lbn, addr;
530     size_t n, nb, off;
531
532     if (!dsk.meta) {
533         if (!blkbuf)
534             blkbuf = malloc(BSIZEMAX);
535         inomap = 0;
536         if (dskread(blkbuf, SBOFF / DEV_BSIZE, SBSIZE / DEV_BSIZE))
537             return -1;
538         memcpy(&fs, blkbuf, sizeof(fs));
539         if (fs.fs_magic != FS_MAGIC) {
540             printf("Not ufs\n");
541             return -1;
542         }
543         fsblks = fs.fs_bsize >> DEV_BSHIFT;
544         dsk.meta++;
545     }
546     if (!inode)
547         return 0;
548     if (inomap != inode) {
549         if (dskread(blkbuf, fsbtodb(&fs, ino_to_fsba(&fs, inode)),
550                     fsblks))
551             return -1;
552         din = ((struct dinode *)blkbuf)[inode % INOPB(&fs)];
553         inomap = inode;
554         fs_off = 0;
555         blkmap = indmap = 0;
556     }
557     s = buf;
558     if (nbyte > (n = din.di_size - fs_off))
559         nbyte = n;
560     nb = nbyte;
561     while (nb) {
562         lbn = lblkno(&fs, fs_off);
563         if (lbn < NDADDR)
564             addr = din.di_db[lbn];
565         else {
566             if (indmap != din.di_ib[0]) {
567                 if (!indbuf)
568                     indbuf = malloc(BSIZEMAX);
569                 if (dskread(indbuf, fsbtodb(&fs, din.di_ib[0]),
570                             fsblks))
571                     return -1;
572                 indmap = din.di_ib[0];
573             }
574             addr = indbuf[(lbn - NDADDR) % NINDIR(&fs)];
575         }
576         n = dblksize(&fs, &din, lbn);
577         if (blkmap != addr) {
578             if (dskread(blkbuf, fsbtodb(&fs, addr), n >> DEV_BSHIFT))
579                 return -1;
580             blkmap = addr;
581         }
582         off = blkoff(&fs, fs_off);
583         n -= off;
584         if (n > nb)
585             n = nb;
586         memcpy(s, blkbuf + off, n);
587         s += n;
588         fs_off += n;
589         nb -= n;
590     }
591     return nbyte;
592 }
593
594 static int
595 dskread(void *buf, unsigned lba, unsigned nblk)
596 {
597     static char *sec;
598     struct dos_partition *dp;
599     struct disklabel *d;
600     unsigned sl, i;
601
602     if (!dsk.meta) {
603         if (!sec)
604             sec = malloc(DEV_BSIZE);
605         dsk.start = 0;
606         if (drvread(sec, DOSBBSECTOR, 1))
607             return -1;
608         dp = (void *)(sec + DOSPARTOFF);
609         sl = dsk.slice;
610         if (sl < BASE_SLICE) {
611             for (i = 0; i < NDOSPART; i++)
612                 if (dp[i].dp_typ == DOSPTYP_386BSD &&
613                     (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
614                     sl = BASE_SLICE + i;
615                     if (dp[i].dp_flag & 0x80 ||
616                         dsk.slice == COMPATIBILITY_SLICE)
617                         break;
618                 }
619             if (dsk.slice == WHOLE_DISK_SLICE)
620                 dsk.slice = sl;
621         }
622         if (sl != WHOLE_DISK_SLICE) {
623             if (sl != COMPATIBILITY_SLICE)
624                 dp += sl - BASE_SLICE;
625             if (dp->dp_typ != DOSPTYP_386BSD) {
626                 printf("Invalid %s\n", "slice");
627                 return -1;
628             }
629             dsk.start = dp->dp_start;
630         }
631         if (drvread(sec, dsk.start + LABELSECTOR, 1))
632                 return -1;
633         d = (void *)(sec + LABELOFFSET);
634         if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
635             if (dsk.part != RAW_PART) {
636                 printf("Invalid %s\n", "label");
637                 return -1;
638             }
639         } else {
640             if (!dsk.init) {
641                 if (d->d_type == DTYPE_SCSI)
642                     dsk.type = TYPE_DA;
643                 dsk.init++;
644             }
645             if (dsk.part >= d->d_npartitions ||
646                 !d->d_partitions[dsk.part].p_size) {
647                 printf("Invalid %s\n", "partition");
648                 return -1;
649             }
650             dsk.start = d->d_partitions[dsk.part].p_offset;
651         }
652     }
653     return drvread(buf, dsk.start + lba, nblk);
654 }
655
656 static int
657 printf(const char *fmt,...)
658 {
659     static const char digits[16] = "0123456789abcdef";
660     va_list ap;
661     char buf[10];
662     char *s;
663     unsigned r, u;
664     int c;
665
666     va_start(ap, fmt);
667     while ((c = *fmt++)) {
668         if (c == '%') {
669             c = *fmt++;
670             switch (c) {
671             case 'c':
672                 putchar(va_arg(ap, int));
673                 continue;
674             case 's':
675                 for (s = va_arg(ap, char *); *s; s++)
676                     putchar(*s);
677                 continue;
678             case 'u':
679             case 'x':
680                 r = c == 'u' ? 10U : 16U;
681                 u = va_arg(ap, unsigned);
682                 s = buf;
683                 do
684                     *s++ = digits[u % r];
685                 while (u /= r);
686                 while (--s >= buf)
687                     putchar(*s);
688                 continue;
689             }
690         }
691         putchar(c);
692     }
693     va_end(ap);
694     return 0;
695 }
696
697 static int
698 putchar(int c)
699 {
700     if (c == '\n')
701         xputc('\r');
702     return xputc(c);
703 }
704
705 static void *
706 memcpy(void *dst, const void *src, size_t size)
707 {
708     const char *s;
709     char *d;
710
711     for (d = dst, s = src; size; size--)
712         *d++ = *s++;
713     return dst;
714 }
715
716 static void *
717 malloc(size_t size)
718 {
719     static uint32_t next;
720     void *p;
721
722     if (!next)
723         next = roundup2(__base + _end, 0x10000) - __base;
724     p = (void *)next;
725     next += size;
726     return p;
727 }
728
729 static uint32_t
730 memsize(int type)
731 {
732     v86.addr = type;
733     v86.eax = 0x8800;
734     v86int();
735     return v86.eax;
736 }
737
738 static int
739 drvread(void *buf, unsigned lba, unsigned nblk)
740 {
741     static unsigned c = 0x2d5c7c2f;
742
743     printf("%c\b", c = c << 8 | c >> 24);
744     v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
745     v86.addr = XREADORG;                /* call to xread in boot1 */
746     v86.es = VTOPSEG(buf);
747     v86.eax = lba;
748     v86.ebx = VTOPOFF(buf);
749     v86.ecx = lba >> 16;
750     v86.edx = nblk << 8 | dsk.drive;
751     v86int();
752     v86.ctl = V86_FLAGS;
753     if (V86_CY(v86.efl)) {
754         printf("Disk error 0x%x (lba=0x%x)\n", v86.eax >> 8 & 0xff,
755                lba);
756         return -1;
757     }
758     return 0;
759 }
760
761 static int
762 keyhit(unsigned ticks)
763 {
764     uint32_t t0, t1;
765
766     if (opts & 1 << RBX_NOINTR)
767         return 0;
768     t0 = 0;
769     for (;;) {
770         if (xgetc(1))
771             return 1;
772         t1 = *(uint32_t *)PTOV(0x46c);
773         if (!t0)
774             t0 = t1;
775         if (t1 < t0 || t1 >= t0 + ticks)
776             return 0;
777     }
778 }
779
780 static int
781 xputc(int c)
782 {
783     if (ioctrl & 0x1)
784         putc(c);
785     if (ioctrl & 0x2)
786         sio_putc(c);
787     return c;
788 }
789
790 static int
791 xgetc(int fn)
792 {
793     if (opts & 1 << RBX_NOINTR)
794         return 0;
795     for (;;) {
796         if (ioctrl & 0x1 && getc(1))
797             return fn ? 1 : getc(0);
798         if (ioctrl & 0x2 && sio_ischar())
799             return fn ? 1 : sio_getc();
800         if (fn)
801             return 0;
802     }
803 }
804
805 static int
806 getc(int fn)
807 {
808     v86.addr = 0x16;
809     v86.eax = fn << 8;
810     v86int();
811     return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
812 }