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