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