]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/boot/pc98/boot2/boot2.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / boot / pc98 / boot2 / boot2.c
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$");
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
98 extern uint32_t _end;
99
100 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
101 static 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
118 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
119 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
120 static const unsigned char dev_daua[NDEV] = {0x80, 0xa0, 0x90};
121
122 static struct dsk {
123     unsigned daua;
124     unsigned type;
125     unsigned disk;
126     unsigned unit;
127     unsigned head;
128     unsigned sec;
129     uint8_t slice;
130     uint8_t part;
131     unsigned start;
132 } dsk;
133 static char cmd[512], cmddup[512], knamebuf[1024];
134 static const char *kname;
135 static uint32_t opts;
136 static int comspeed = SIOSPD;
137 static struct bootinfo bootinfo;
138 static uint8_t ioctrl = IO_KEYBOARD;
139
140 void exit(int);
141 static void load(void);
142 static int parse(void);
143 static int xfsread(ino_t, void *, size_t);
144 static int dskread(void *, unsigned, unsigned);
145 static void printf(const char *,...);
146 static void putchar(int);
147 static int drvread(void *, unsigned);
148 static int keyhit(unsigned);
149 static int xputc(int);
150 static int xgetc(int);
151 static inline int getc(int);
152
153 static void memcpy(void *, const void *, int);
154 static void
155 memcpy(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
164 static inline int
165 strcmp(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
174 static inline int
175 xfsread(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
184 static inline void
185 getstr(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
214 static inline void
215 putc(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
225 static inline int
226 is_scsi_hd(void)
227 {
228
229     if ((*(u_char *)PTOV(0x482) >> dsk.unit) & 0x01)
230         return 1;
231
232     return 0;
233 }
234
235 static inline void
236 fix_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
250 static inline uint32_t
251 get_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
274 static void
275 set_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
287 static uint32_t
288 bd_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
321 static int
322 check_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
347 int
348 main(void)
349 {
350 #ifdef GET_BIOSGEOM
351     int i;
352 #endif
353     uint8_t autoboot;
354     ino_t ino;
355     size_t nbyte;
356
357     dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
358     v86.ctl = V86_FLAGS;
359     v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
360     dsk.daua = *(uint8_t *)PTOV(0x584);
361     dsk.disk = dsk.daua & DRV_DISK;
362     dsk.unit = dsk.daua & DRV_UNIT;
363     if (dsk.disk == 0x80)
364         dsk.type = TYPE_AD;
365     else if (dsk.disk == 0xa0)
366         dsk.type = TYPE_DA;
367     else /* if (dsk.disk == 0x30 || dsk.disk == 0x90) */
368         dsk.type = TYPE_FD;
369     dsk.slice = check_slice();
370 #ifdef GET_BIOSGEOM
371     for (i = 0; i < N_BIOS_GEOM; i++)
372         bootinfo.bi_bios_geom[i] = bd_getbigeom(i);
373 #endif
374     bootinfo.bi_version = BOOTINFO_VERSION;
375     bootinfo.bi_size = sizeof(bootinfo);
376
377     /* Process configuration file */
378
379     autoboot = 1;
380
381     if ((ino = lookup(PATH_CONFIG)) ||
382         (ino = lookup(PATH_DOTCONFIG))) {
383         nbyte = fsread(ino, cmd, sizeof(cmd) - 1);
384         cmd[nbyte] = '\0';
385     }
386
387     if (*cmd) {
388         memcpy(cmddup, cmd, sizeof(cmd));
389         if (parse())
390             autoboot = 0;
391         if (!OPT_CHECK(RBX_QUIET))
392             printf("%s: %s", PATH_CONFIG, cmddup);
393         /* Do not process this command twice */
394         *cmd = 0;
395     }
396
397     /*
398      * Try to exec stage 3 boot loader. If interrupted by a keypress,
399      * or in case of failure, try to load a kernel directly instead.
400      */
401
402     if (!kname) {
403         kname = PATH_BOOT3;
404         if (autoboot && !keyhit(3*SECOND)) {
405             load();
406             kname = PATH_KERNEL;
407         }
408     }
409
410     /* Present the user with the boot2 prompt. */
411
412     for (;;) {
413         if (!autoboot || !OPT_CHECK(RBX_QUIET))
414             printf("\nFreeBSD/pc98 boot\n"
415                    "Default: %u:%s(%u,%c)%s\n"
416                    "boot: ",
417                    dsk.unit, dev_nm[dsk.type], dsk.unit,
418                    'a' + dsk.part, kname);
419         if (ioctrl & IO_SERIAL)
420             sio_flush();
421         if (!autoboot || keyhit(3*SECOND))
422             getstr();
423         else if (!autoboot || !OPT_CHECK(RBX_QUIET))
424             putchar('\n');
425         autoboot = 0;
426         if (parse())
427             putchar('\a');
428         else
429             load();
430     }
431 }
432
433 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
434 void
435 exit(int x)
436 {
437 }
438
439 static void
440 load(void)
441 {
442     union {
443         struct exec ex;
444         Elf32_Ehdr eh;
445     } hdr;
446     static Elf32_Phdr ep[2];
447     static Elf32_Shdr es[2];
448     caddr_t p;
449     ino_t ino;
450     uint32_t addr;
451     int i, j;
452
453     if (!(ino = lookup(kname))) {
454         if (!ls)
455             printf("No %s\n", kname);
456         return;
457     }
458     if (xfsread(ino, &hdr, sizeof(hdr)))
459         return;
460
461     if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
462         addr = hdr.ex.a_entry & 0xffffff;
463         p = PTOV(addr);
464         fs_off = PAGE_SIZE;
465         if (xfsread(ino, p, hdr.ex.a_text))
466             return;
467         p += roundup2(hdr.ex.a_text, PAGE_SIZE);
468         if (xfsread(ino, p, hdr.ex.a_data))
469             return;
470     } else if (IS_ELF(hdr.eh)) {
471         fs_off = hdr.eh.e_phoff;
472         for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
473             if (xfsread(ino, ep + j, sizeof(ep[0])))
474                 return;
475             if (ep[j].p_type == PT_LOAD)
476                 j++;
477         }
478         for (i = 0; i < 2; i++) {
479             p = PTOV(ep[i].p_paddr & 0xffffff);
480             fs_off = ep[i].p_offset;
481             if (xfsread(ino, p, ep[i].p_filesz))
482                 return;
483         }
484         p += roundup2(ep[1].p_memsz, PAGE_SIZE);
485         bootinfo.bi_symtab = VTOP(p);
486         if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
487             fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
488                 (hdr.eh.e_shstrndx + 1);
489             if (xfsread(ino, &es, sizeof(es)))
490                 return;
491             for (i = 0; i < 2; i++) {
492                 *(Elf32_Word *)p = es[i].sh_size;
493                 p += sizeof(es[i].sh_size);
494                 fs_off = es[i].sh_offset;
495                 if (xfsread(ino, p, es[i].sh_size))
496                     return;
497                 p += es[i].sh_size;
498             }
499         }
500         addr = hdr.eh.e_entry & 0xffffff;
501         bootinfo.bi_esymtab = VTOP(p);
502     } else {
503         printf("Invalid %s\n", "format");
504         return;
505     }
506
507     bootinfo.bi_kernelname = VTOP(kname);
508     bootinfo.bi_bios_dev = dsk.daua;
509     __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
510            MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
511            0, 0, 0, VTOP(&bootinfo));
512 }
513
514 static int
515 parse()
516 {
517     char *arg = cmd;
518     char *ep, *p, *q;
519     const char *cp;
520     unsigned int drv;
521     int c, i, j;
522
523     while ((c = *arg++)) {
524         if (c == ' ' || c == '\t' || c == '\n')
525             continue;
526         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
527         ep = p;
528         if (*p)
529             *p++ = 0;
530         if (c == '-') {
531             while ((c = *arg++)) {
532                 if (c == 'P') {
533                     if (*(uint8_t *)PTOV(0x481) & 0x48) {
534                         cp = "yes";
535                     } else {
536                         opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
537                         cp = "no";
538                     }
539                     printf("Keyboard: %s\n", cp);
540                     continue;
541                 } else if (c == 'S') {
542                     j = 0;
543                     while ((unsigned int)(i = *arg++ - '0') <= 9)
544                         j = j * 10 + i;
545                     if (j > 0 && i == -'0') {
546                         comspeed = j;
547                         break;
548                     }
549                     /* Fall through to error below ('S' not in optstr[]). */
550                 }
551                 for (i = 0; c != optstr[i]; i++)
552                     if (i == NOPT - 1)
553                         return -1;
554                 opts ^= OPT_SET(flags[i]);
555             }
556             ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
557                      OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
558             if (ioctrl & IO_SERIAL) {
559                 if (sio_init(115200 / comspeed) != 0)
560                     ioctrl &= ~IO_SERIAL;
561             }
562         } else {
563             for (q = arg--; *q && *q != '('; q++);
564             if (*q) {
565                 drv = -1;
566                 if (arg[1] == ':') {
567                     drv = *arg - '0';
568                     if (drv > 9)
569                         return (-1);
570                     arg += 2;
571                 }
572                 if (q - arg != 2)
573                     return -1;
574                 for (i = 0; arg[0] != dev_nm[i][0] ||
575                             arg[1] != dev_nm[i][1]; i++)
576                     if (i == NDEV - 1)
577                         return -1;
578                 dsk.type = i;
579                 arg += 3;
580                 dsk.unit = *arg - '0';
581                 if (arg[1] != ',' || dsk.unit > 9)
582                     return -1;
583                 arg += 2;
584                 dsk.slice = WHOLE_DISK_SLICE;
585                 if (arg[1] == ',') {
586                     dsk.slice = *arg - '0' + 1;
587                     if (dsk.slice > NDOSPART + 1)
588                         return -1;
589                     arg += 2;
590                 }
591                 if (arg[1] != ')')
592                     return -1;
593                 dsk.part = *arg - 'a';
594                 if (dsk.part > 7)
595                     return (-1);
596                 arg += 2;
597                 if (drv == -1)
598                     drv = dsk.unit;
599                 dsk.disk = dev_daua[dsk.type];
600                 dsk.daua = dsk.disk | dsk.unit;
601                 dsk_meta = 0;
602             }
603             if ((i = ep - arg)) {
604                 if ((size_t)i >= sizeof(knamebuf))
605                     return -1;
606                 memcpy(knamebuf, arg, i + 1);
607                 kname = knamebuf;
608             }
609         }
610         arg = p;
611     }
612     return 0;
613 }
614
615 static int
616 dskread(void *buf, unsigned lba, unsigned nblk)
617 {
618     struct pc98_partition *dp;
619     struct disklabel *d;
620     char *sec;
621     unsigned i;
622     uint8_t sl;
623     u_char *p;
624
625     if (!dsk_meta) {
626         sec = dmadat->secbuf;
627         set_dsk();
628         if (dsk.type == TYPE_FD)
629             goto unsliced;
630         if (drvread(sec, DOSBBSECTOR + 1))
631             return -1;
632         dp = (void *)(sec + DOSPARTOFF);
633         sl = dsk.slice;
634         if (sl < BASE_SLICE) {
635             for (i = 0; i < NDOSPART; i++)
636                 if (dp[i].dp_mid == DOSMID_386BSD) {
637                     sl = BASE_SLICE + i;
638                     break;
639                 }
640             dsk.slice = sl;
641         }
642         if (sl != WHOLE_DISK_SLICE) {
643             dp += sl - BASE_SLICE;
644             if (dp->dp_mid != DOSMID_386BSD) {
645                 printf("Invalid %s\n", "slice");
646                 return -1;
647             }
648             dsk.start = dp->dp_scyl * dsk.head * dsk.sec +
649                 dp->dp_shd * dsk.sec + dp->dp_ssect;
650         }
651         if (drvread(sec, dsk.start + LABELSECTOR))
652                 return -1;
653         d = (void *)(sec + LABELOFFSET);
654         if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
655             if (dsk.part != RAW_PART) {
656                 printf("Invalid %s\n", "label");
657                 return -1;
658             }
659         } else {
660             if (dsk.part >= d->d_npartitions ||
661                 !d->d_partitions[dsk.part].p_size) {
662                 printf("Invalid %s\n", "partition");
663                 return -1;
664             }
665             dsk.start += d->d_partitions[dsk.part].p_offset;
666             dsk.start -= d->d_partitions[RAW_PART].p_offset;
667         }
668     unsliced: ;
669     }
670     for (p = buf; nblk; p += 512, lba++, nblk--) {
671         if ((i = drvread(p, dsk.start + lba)))
672             return i;
673     }
674     return 0;
675 }
676
677 static void
678 printf(const char *fmt,...)
679 {
680     va_list ap;
681     static char buf[10];
682     char *s;
683     unsigned u;
684     int c;
685
686     va_start(ap, fmt);
687     while ((c = *fmt++)) {
688         if (c == '%') {
689             c = *fmt++;
690             switch (c) {
691             case 'c':
692                 putchar(va_arg(ap, int));
693                 continue;
694             case 's':
695                 for (s = va_arg(ap, char *); *s; s++)
696                     putchar(*s);
697                 continue;
698             case 'u':
699                 u = va_arg(ap, unsigned);
700                 s = buf;
701                 do
702                     *s++ = '0' + u % 10U;
703                 while (u /= 10U);
704                 while (--s >= buf)
705                     putchar(*s);
706                 continue;
707             }
708         }
709         putchar(c);
710     }
711     va_end(ap);
712     return;
713 }
714
715 static void
716 putchar(int c)
717 {
718     if (c == '\n')
719         xputc('\r');
720     xputc(c);
721 }
722
723 static int
724 drvread(void *buf, unsigned lba)
725 {
726     static unsigned c = 0x2d5c7c2f;
727     unsigned bpc, x, cyl, head, sec;
728
729     bpc = dsk.sec * dsk.head;
730     cyl = lba / bpc;
731     x = lba % bpc;
732     head = x / dsk.sec;
733     sec = x % dsk.sec;
734
735     if (!OPT_CHECK(RBX_QUIET))
736         printf("%c\b", c = c << 8 | c >> 24);
737     v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
738     v86.addr = READORG;         /* call to read in boot1 */
739     v86.ecx = cyl;
740     v86.edx = (head << 8) | sec;
741     v86.edi = lba;
742     v86.ebx = 512;
743     v86.es = VTOPSEG(buf);
744     v86.ebp = VTOPOFF(buf);
745     v86int();
746     v86.ctl = V86_FLAGS;
747     if (V86_CY(v86.efl)) {
748         printf("error %u c/h/s %u/%u/%u lba %u\n", v86.eax >> 8 & 0xff,
749                cyl, head, sec, lba);
750         return -1;
751     }
752     return 0;
753 }
754
755 static inline void
756 delay(void)
757 {
758     int i;
759
760     i = 800;
761     do {
762         outb(0x5f, 0);  /* about 600ns */
763     } while (--i >= 0);
764 }
765
766 static int
767 keyhit(unsigned sec)
768 {
769     unsigned i;
770
771     if (OPT_CHECK(RBX_NOINTR))
772         return 0;
773     for (i = 0; i < sec * 1000; i++) {
774         if (xgetc(1))
775             return 1;
776         delay();
777     }
778     return 0;
779 }
780
781 static int
782 xputc(int c)
783 {
784     if (ioctrl & IO_KEYBOARD)
785         putc(c);
786     if (ioctrl & IO_SERIAL)
787         sio_putc(c);
788     return c;
789 }
790
791 static int
792 getc(int fn)
793 {
794     v86.addr = 0x18;
795     v86.eax = fn << 8;
796     v86int();
797     if (fn)
798         return (v86.ebx >> 8) & 0x01;
799     else
800         return v86.eax & 0xff;
801 }
802
803 static int
804 xgetc(int fn)
805 {
806     if (OPT_CHECK(RBX_NOINTR))
807         return 0;
808     for (;;) {
809         if (ioctrl & IO_KEYBOARD && getc(1))
810             return fn ? 1 : getc(0);
811         if (ioctrl & IO_SERIAL && sio_ischar())
812             return fn ? 1 : sio_getc();
813         if (fn)
814             return 0;
815     }
816 }