]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/boot/pc98/boot2/boot2.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 #include <machine/psl.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 IO_KEYBOARD     1
41 #define IO_SERIAL       2
42
43 #define SECOND          1       /* Circa that many ticks in a second. */
44
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). */
71
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))
79
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 #define V86_CY(x)       ((x) & PSL_C)
88 #define V86_ZR(x)       ((x) & PSL_Z)
89
90 #define DRV_DISK        0xf0
91 #define DRV_UNIT        0x0f
92
93 #define TYPE_AD         0
94 #define TYPE_DA         1
95 #define TYPE_FD         2
96
97 #define OPT_SET(opt)    (1 << (opt))
98 #define OPT_CHECK(opt)  ((opts) & OPT_SET(opt))
99
100 extern uint32_t _end;
101
102 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
103 static const unsigned char flags[NOPT] = {
104     RBX_DUAL,
105     RBX_SERIAL,
106     RBX_ASKNAME,
107     RBX_CDROM,
108     RBX_CONFIG,
109     RBX_KDB,
110     RBX_GDB,
111     RBX_MUTE,
112     RBX_NOINTR,
113     RBX_PAUSE,
114     RBX_QUIET,
115     RBX_DFLTROOT,
116     RBX_SINGLE,
117     RBX_VERBOSE
118 };
119
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};
123
124 static struct dsk {
125     unsigned daua;
126     unsigned type;
127     unsigned disk;
128     unsigned unit;
129     unsigned head;
130     unsigned sec;
131     unsigned slice;
132     unsigned part;
133     unsigned start;
134 } dsk;
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;
141
142 void exit(int);
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);
155
156 static void memcpy(void *, const void *, int);
157 static void
158 memcpy(void *dst, const void *src, int len)
159 {
160     const char *s = src;
161     char *d = dst;
162
163     while (len--)
164         *d++ = *s++;
165 }
166
167 static inline int
168 strcmp(const char *s1, const char *s2)
169 {
170     for (; *s1 == *s2 && *s1; s1++, s2++);
171     return (unsigned char)*s1 - (unsigned char)*s2;
172 }
173
174 #define UFS_SMALL_CGBASE
175 #include "ufsread.c"
176
177 static inline int
178 xfsread(ino_t inode, void *buf, size_t nbyte)
179 {
180     if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
181         printf("Invalid %s\n", "format");
182         return -1;
183     }
184     return 0;
185 }
186
187 static inline uint32_t
188 memsize(void)
189 {
190     u_char *p = (u_char *)PTOV(0);
191
192     return *(p + 0x401) * 128 * 1024 + *(u_int16_t *)(p + 0x594) * 1024 * 1024;
193 }
194
195 static inline void
196 getstr(void)
197 {
198     char *s;
199     int c;
200
201     s = cmd;
202     for (;;) {
203         switch (c = xgetc(0)) {
204         case 0:
205             break;
206         case '\177':
207         case '\b':
208             if (s > cmd) {
209                 s--;
210                 printf("\b \b");
211             }
212             break;
213         case '\n':
214         case '\r':
215             *s = 0;
216             return;
217         default:
218             if (s - cmd < sizeof(cmd) - 1)
219                 *s++ = c;
220             putchar(c);
221         }
222     }
223 }
224
225 static inline void
226 putc(int c)
227 {
228
229     v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
230     v86.addr = PUTCORG;         /* call to putc in boot1 */
231     v86.eax = c;
232     v86int();
233     v86.ctl = V86_FLAGS;
234 }
235
236 static inline int
237 is_scsi_hd(void)
238 {
239
240     if ((*(u_char *)PTOV(0x482) >> dsk.unit) & 0x01)
241         return 1;
242
243     return 0;
244 }
245
246 static inline void
247 fix_sector_size(void)
248 {
249     u_char *p;
250
251     p = (u_char *)PTOV(0x460 + dsk.unit * 4);   /* SCSI equipment parameter */
252
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;
257         }
258     }
259 }
260
261 static inline uint32_t
262 get_diskinfo(void)
263 {
264
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 */
272         v86.addr = 0x1b;
273         v86.eax = 0x8400 | dsk.daua;
274         v86int();
275         return (v86.ecx << 16) | v86.edx;
276     }
277
278     /* SCSI MO or CD */
279     fix_sector_size();  /* SCSI MO */
280
281     /* other SCSI devices */
282     return (65535 << 16) | (8 << 8) | 32;
283 }
284
285 static void
286 set_dsk(void)
287 {
288     uint32_t di;
289
290     di = get_diskinfo();
291
292     dsk.head = (di >> 8) & 0xff;
293     dsk.sec = di & 0xff;
294     dsk.start = 0;
295 }
296
297 #ifdef GET_BIOSGEOM
298 static uint32_t
299 bd_getbigeom(int bunit)
300 {
301     int hds = 0;
302     int unit = 0x80;            /* IDE HDD */
303     u_int addr = 0x55d;
304
305     while (unit < 0xa7) {
306         if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
307             if (hds++ == bunit)
308                 break;
309
310         if (unit >= 0xA0) {
311             int media = ((unsigned *)PTOV(0x460))[unit & 0x0F] & 0x1F;
312
313             if (media == 7 && hds++ == bunit)   /* SCSI MO */
314                 return(0xFFFE0820); /* C:65535 H:8 S:32 */
315         }
316         if (++unit == 0x84) {
317             unit = 0xA0;        /* SCSI HDD */
318             addr = 0x482;
319         }
320     }
321     if (unit == 0xa7)
322         return 0x4F020F;        /* 1200KB FD C:80 H:2 S:15 */
323     v86.addr = 0x1b;
324     v86.eax = 0x8400 | unit;
325     v86int();
326     if (v86.efl & 0x1)
327         return 0x4F020F;        /* 1200KB FD C:80 H:2 S:15 */
328     return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
329 }
330 #endif
331
332 static int
333 check_slice(void)
334 {
335     struct pc98_partition *dp;
336     char *sec;
337     unsigned i, cyl;
338
339     sec = dmadat->secbuf;
340     cyl = *(uint16_t *)PTOV(ARGS);
341     set_dsk();
342
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);
352         }
353     }
354
355     return (WHOLE_DISK_SLICE);
356 }
357
358 int
359 main(void)
360 {
361 #ifdef GET_BIOSGEOM
362     int i;
363 #endif
364     int autoboot;
365     ino_t ino;
366
367     dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
368     v86.ctl = V86_FLAGS;
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)
374         dsk.type = TYPE_AD;
375     else if (dsk.disk == 0xa0)
376         dsk.type = TYPE_DA;
377     else /* if (dsk.disk == 0x30 || dsk.disk == 0x90) */
378         dsk.type = TYPE_FD;
379     dsk.slice = check_slice();
380 #ifdef GET_BIOSGEOM
381     for (i = 0; i < N_BIOS_GEOM; i++)
382         bootinfo.bi_bios_geom[i] = bd_getbigeom(i);
383 #endif
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++;
389
390     /* Process configuration file */
391
392     autoboot = 1;
393
394     if ((ino = lookup(PATH_CONFIG)))
395         fsread(ino, cmd, sizeof(cmd));
396
397     if (*cmd) {
398         memcpy(cmddup, cmd, sizeof(cmd));
399         if (parse())
400             autoboot = 0;
401         if (!OPT_CHECK(RBX_QUIET))
402             printf("%s: %s", PATH_CONFIG, cmddup);
403         /* Do not process this command twice */
404         *cmd = 0;
405     }
406
407     /*
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.
410      */
411
412     if (autoboot && !*kname) {
413         memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
414         if (!keyhit(3*SECOND)) {
415             load();
416             memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
417         }
418     }
419
420     /* Present the user with the boot2 prompt. */
421
422     for (;;) {
423         if (!autoboot || !OPT_CHECK(RBX_QUIET))
424             printf("\nFreeBSD/pc98 boot\n"
425                    "Default: %u:%s(%u,%c)%s\n"
426                    "boot: ",
427                    dsk.unit, dev_nm[dsk.type], dsk.unit,
428                    'a' + dsk.part, kname);
429         if (ioctrl & IO_SERIAL)
430             sio_flush();
431         if (!autoboot || keyhit(5*SECOND))
432             getstr();
433         else if (!autoboot || !OPT_CHECK(RBX_QUIET))
434             putchar('\n');
435         autoboot = 0;
436         if (parse())
437             putchar('\a');
438         else
439             load();
440     }
441 }
442
443 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
444 void
445 exit(int x)
446 {
447 }
448
449 static void
450 load(void)
451 {
452     union {
453         struct exec ex;
454         Elf32_Ehdr eh;
455     } hdr;
456     static Elf32_Phdr ep[2];
457     static Elf32_Shdr es[2];
458     caddr_t p;
459     ino_t ino;
460     uint32_t addr, x;
461     int fmt, i, j;
462
463     if (!(ino = lookup(kname))) {
464         if (!ls)
465             printf("No %s\n", kname);
466         return;
467     }
468     if (xfsread(ino, &hdr, sizeof(hdr)))
469         return;
470     if (N_GETMAGIC(hdr.ex) == ZMAGIC)
471         fmt = 0;
472     else if (IS_ELF(hdr.eh))
473         fmt = 1;
474     else {
475         printf("Invalid %s\n", "format");
476         return;
477     }
478     if (fmt == 0) {
479         addr = hdr.ex.a_entry & 0xffffff;
480         p = PTOV(addr);
481         fs_off = PAGE_SIZE;
482         if (xfsread(ino, p, hdr.ex.a_text))
483             return;
484         p += roundup2(hdr.ex.a_text, PAGE_SIZE);
485         if (xfsread(ino, p, hdr.ex.a_data))
486             return;
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);
491         if (hdr.ex.a_syms) {
492             if (xfsread(ino, p, hdr.ex.a_syms))
493                 return;
494             p += hdr.ex.a_syms;
495             if (xfsread(ino, p, sizeof(int)))
496                 return;
497             x = *(uint32_t *)p;
498             p += sizeof(int);
499             x -= sizeof(int);
500             if (xfsread(ino, p, x))
501                 return;
502             p += x;
503         }
504     } else {
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])))
508                 return;
509             if (ep[j].p_type == PT_LOAD)
510                 j++;
511         }
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))
516                 return;
517         }
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)))
524                 return;
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))
530                     return;
531                 p += es[i].sh_size;
532             }
533         }
534         addr = hdr.eh.e_entry & 0xffffff;
535     }
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));
542 }
543
544 static int
545 parse()
546 {
547     char *arg = cmd;
548     char *ep, *p, *q;
549     const char *cp;
550     unsigned int drv;
551     int c, i, j;
552
553     while ((c = *arg++)) {
554         if (c == ' ' || c == '\t' || c == '\n')
555             continue;
556         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
557         ep = p;
558         if (*p)
559             *p++ = 0;
560         if (c == '-') {
561             while ((c = *arg++)) {
562                 if (c == 'P') {
563                     if (*(uint8_t *)PTOV(0x481) & 0x48) {
564                         cp = "yes";
565                     } else {
566                         opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
567                         cp = "no";
568                     }
569                     printf("Keyboard: %s\n", cp);
570                     continue;
571                 } else if (c == 'S') {
572                     j = 0;
573                     while ((unsigned int)(i = *arg++ - '0') <= 9)
574                         j = j * 10 + i;
575                     if (j > 0 && i == -'0') {
576                         comspeed = j;
577                         break;
578                     }
579                     /* Fall through to error below ('S' not in optstr[]). */
580                 }
581                 for (i = 0; c != optstr[i]; i++)
582                     if (i == NOPT - 1)
583                         return -1;
584                 opts ^= OPT_SET(flags[i]);
585             }
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);
590         } else {
591             for (q = arg--; *q && *q != '('; q++);
592             if (*q) {
593                 drv = -1;
594                 if (arg[1] == ':') {
595                     drv = *arg - '0';
596                     if (drv > 9)
597                         return (-1);
598                     arg += 2;
599                 }
600                 if (q - arg != 2)
601                     return -1;
602                 for (i = 0; arg[0] != dev_nm[i][0] ||
603                             arg[1] != dev_nm[i][1]; i++)
604                     if (i == NDEV - 1)
605                         return -1;
606                 dsk.type = i;
607                 arg += 3;
608                 dsk.unit = *arg - '0';
609                 if (arg[1] != ',' || dsk.unit > 9)
610                     return -1;
611                 arg += 2;
612                 dsk.slice = WHOLE_DISK_SLICE;
613                 if (arg[1] == ',') {
614                     dsk.slice = *arg - '0' + 1;
615                     if (dsk.slice > NDOSPART + 1)
616                         return -1;
617                     arg += 2;
618                 }
619                 if (arg[1] != ')')
620                     return -1;
621                 dsk.part = *arg - 'a';
622                 if (dsk.part > 7)
623                     return (-1);
624                 arg += 2;
625                 if (drv == -1)
626                     drv = dsk.unit;
627                 dsk.disk = dev_daua[dsk.type];
628                 dsk.daua = dsk.disk | dsk.unit;
629                 dsk_meta = 0;
630             }
631             if ((i = ep - arg)) {
632                 if ((size_t)i >= sizeof(kname))
633                     return -1;
634                 memcpy(kname, arg, i + 1);
635             }
636         }
637         arg = p;
638     }
639     return 0;
640 }
641
642 static int
643 dskread(void *buf, unsigned lba, unsigned nblk)
644 {
645     struct pc98_partition *dp;
646     struct disklabel *d;
647     char *sec;
648     unsigned sl, i;
649     u_char *p;
650
651     if (!dsk_meta) {
652         sec = dmadat->secbuf;
653         set_dsk();
654         if (dsk.type == TYPE_FD)
655             goto unsliced;
656         if (drvread(sec, DOSBBSECTOR + 1))
657             return -1;
658         dp = (void *)(sec + DOSPARTOFF);
659         sl = dsk.slice;
660         if (sl < BASE_SLICE) {
661             for (i = 0; i < NDOSPART; i++)
662                 if (dp[i].dp_mid == DOSMID_386BSD) {
663                     sl = BASE_SLICE + i;
664                     break;
665                 }
666             dsk.slice = sl;
667         }
668         if (sl != WHOLE_DISK_SLICE) {
669             dp += sl - BASE_SLICE;
670             if (dp->dp_mid != DOSMID_386BSD) {
671                 printf("Invalid %s\n", "slice");
672                 return -1;
673             }
674             dsk.start = dp->dp_scyl * dsk.head * dsk.sec +
675                 dp->dp_shd * dsk.sec + dp->dp_ssect;
676         }
677         if (drvread(sec, dsk.start + LABELSECTOR))
678                 return -1;
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");
683                 return -1;
684             }
685         } else {
686             if (dsk.part >= d->d_npartitions ||
687                 !d->d_partitions[dsk.part].p_size) {
688                 printf("Invalid %s\n", "partition");
689                 return -1;
690             }
691             dsk.start += d->d_partitions[dsk.part].p_offset;
692             dsk.start -= d->d_partitions[RAW_PART].p_offset;
693         }
694     unsliced: ;
695     }
696     for (p = buf; nblk; p += 512, lba++, nblk--) {
697         if ((i = drvread(p, dsk.start + lba)))
698             return i;
699     }
700     return 0;
701 }
702
703 static void
704 printf(const char *fmt,...)
705 {
706     va_list ap;
707     char buf[10];
708     char *s;
709     unsigned u;
710     int c;
711
712     va_start(ap, fmt);
713     while ((c = *fmt++)) {
714         if (c == '%') {
715             c = *fmt++;
716             switch (c) {
717             case 'c':
718                 putchar(va_arg(ap, int));
719                 continue;
720             case 's':
721                 for (s = va_arg(ap, char *); *s; s++)
722                     putchar(*s);
723                 continue;
724             case 'u':
725                 u = va_arg(ap, unsigned);
726                 s = buf;
727                 do
728                     *s++ = '0' + u % 10U;
729                 while (u /= 10U);
730                 while (--s >= buf)
731                     putchar(*s);
732                 continue;
733             }
734         }
735         putchar(c);
736     }
737     va_end(ap);
738     return;
739 }
740
741 static void
742 putchar(int c)
743 {
744     if (c == '\n')
745         xputc('\r');
746     xputc(c);
747 }
748
749 static int
750 drvread(void *buf, unsigned lba)
751 {
752     static unsigned c = 0x2d5c7c2f;
753     unsigned bpc, x, cyl, head, sec;
754
755     bpc = dsk.sec * dsk.head;
756     cyl = lba / bpc;
757     x = lba % bpc;
758     head = x / dsk.sec;
759     sec = x % dsk.sec;
760
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 */
765     v86.ecx = cyl;
766     v86.edx = (head << 8) | sec;
767     v86.edi = lba;
768     v86.ebx = 512;
769     v86.es = VTOPSEG(buf);
770     v86.ebp = VTOPOFF(buf);
771     v86int();
772     v86.ctl = V86_FLAGS;
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);
776         return -1;
777     }
778     return 0;
779 }
780
781 static inline void
782 delay(void)
783 {
784     int i;
785
786     i = 800;
787     do {
788         outb(0x5f, 0);  /* about 600ns */
789     } while (--i >= 0);
790 }
791
792 static int
793 keyhit(unsigned sec)
794 {
795     unsigned i;
796
797     if (OPT_CHECK(RBX_NOINTR))
798         return 0;
799     for (i = 0; i < sec * 1000; i++) {
800         if (xgetc(1))
801             return 1;
802         delay();
803     }
804     return 0;
805 }
806
807 static int
808 xputc(int c)
809 {
810     if (ioctrl & IO_KEYBOARD)
811         putc(c);
812     if (ioctrl & IO_SERIAL)
813         sio_putc(c);
814     return c;
815 }
816
817 static int
818 xgetc(int fn)
819 {
820     if (OPT_CHECK(RBX_NOINTR))
821         return 0;
822     for (;;) {
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();
827         if (fn)
828             return 0;
829     }
830 }
831
832 static int
833 getc(int fn)
834 {
835     v86.addr = 0x18;
836     v86.eax = fn << 8;
837     v86int();
838     if (fn)
839         return (v86.ebx >> 8) & 0x01;
840     else
841         return v86.eax & 0xff;
842 }