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