Merge from vendor branch LIBPCAP:
[dragonfly.git] / sys / boot / pc32 / boot2 / boot2.c
CommitLineData
cacaceec
MD
1/*
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
984263bc
MD
34 * Copyright (c) 1998 Robert Nordier
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms are freely
38 * permitted provided that the above copyright notice and this
39 * paragraph and the following disclaimer are duplicated in all
40 * such forms.
41 *
42 * This software is provided "AS IS" and without any express or
43 * implied warranties, including, without limitation, the implied
44 * warranties of merchantability and fitness for a particular
45 * purpose.
5ee58eed
MD
46 *
47 * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $
2b961883 48 * $DragonFly: src/sys/boot/pc32/boot2/boot2.c,v 1.16 2007/06/18 05:13:42 dillon Exp $
984263bc 49 */
984263bc 50#include <sys/param.h>
2b961883 51#include <sys/disklabel32.h>
5ee58eed
MD
52#include <sys/diskslice.h>
53#include <sys/diskmbr.h>
ba0cc1ab 54#include <sys/dtype.h>
984263bc
MD
55#include <sys/dirent.h>
56#include <machine/bootinfo.h>
57#include <machine/elf.h>
58
984263bc
MD
59#include <stdarg.h>
60
61#include <a.out.h>
62
63#include <btxv86.h>
64
65#include "boot2.h"
66#include "lib.h"
409cbc03 67#include "../bootasm.h"
984263bc 68
5ee58eed
MD
69#define SECOND 18 /* Circa that many ticks in a second. */
70
984263bc
MD
71#define RBX_ASKNAME 0x0 /* -a */
72#define RBX_SINGLE 0x1 /* -s */
73#define RBX_DFLTROOT 0x5 /* -r */
74#define RBX_KDB 0x6 /* -d */
75#define RBX_CONFIG 0xa /* -c */
76#define RBX_VERBOSE 0xb /* -v */
77#define RBX_SERIAL 0xc /* -h */
78#define RBX_CDROM 0xd /* -C */
79#define RBX_GDB 0xf /* -g */
5ee58eed
MD
80#define RBX_MUTE 0x10 /* -m */
81#define RBX_PAUSE 0x12 /* -p */
82#define RBX_NOINTR 0x1c /* -n */
badbfe61 83#define RBX_VIDEO 0x1d /* -V */
984263bc 84#define RBX_PROBEKBD 0x1e /* -P */
5ee58eed 85/* 0x1f is reserved for the historical RB_BOOTINFO option */
984263bc 86
38e10dff 87#define RBF_MUTE (1 << RBX_MUTE)
badbfe61
MD
88#define RBF_SERIAL (1 << RBX_SERIAL)
89#define RBF_VIDEO (1 << RBX_VIDEO)
90
50a1695f 91/* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
5ee58eed 92#define RBX_MASK 0x2005ffff
984263bc
MD
93
94#define PATH_CONFIG "/boot.config"
95#define PATH_BOOT3 "/boot/loader"
96#define PATH_KERNEL "/kernel"
97
5ee58eed 98#define NDEV 3
984263bc
MD
99#define MEM_BASE 0x12
100#define MEM_EXT 0x15
101#define V86_CY(x) ((x) & 1)
102#define V86_ZR(x) ((x) & 0x40)
103
104#define DRV_HARD 0x80
105#define DRV_MASK 0x7f
106
107#define TYPE_AD 0
5ee58eed
MD
108#define TYPE_DA 1
109#define TYPE_MAXHARD TYPE_DA
110#define TYPE_FD 2
984263bc 111
badbfe61
MD
112#define NOPT 12
113
4e06dda7
MD
114#define INVALID_S "Bad %s\n"
115
984263bc
MD
116extern uint32_t _end;
117
badbfe61 118static const char optstr[NOPT] = { "VhaCgmnPprsv" };
984263bc 119static const unsigned char flags[NOPT] = {
badbfe61 120 RBX_VIDEO,
984263bc
MD
121 RBX_SERIAL,
122 RBX_ASKNAME,
123 RBX_CDROM,
984263bc 124 RBX_GDB,
5ee58eed 125 RBX_MUTE,
984263bc
MD
126 RBX_NOINTR,
127 RBX_PROBEKBD,
5ee58eed 128 RBX_PAUSE,
984263bc
MD
129 RBX_DFLTROOT,
130 RBX_SINGLE,
131 RBX_VERBOSE
132};
133
5ee58eed
MD
134static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
135static const unsigned char dev_maj[NDEV] = {30, 4, 2};
984263bc
MD
136
137static struct dsk {
138 unsigned drive;
139 unsigned type;
140 unsigned unit;
141 unsigned slice;
142 unsigned part;
143 unsigned start;
144 int init;
984263bc
MD
145} dsk;
146static char cmd[512];
147static char kname[1024];
148static uint32_t opts;
149static struct bootinfo bootinfo;
984263bc
MD
150
151void exit(int);
5ee58eed
MD
152static void load(void);
153static int parse(void);
984263bc 154static int xfsread(ino_t, void *, size_t);
984263bc 155static int dskread(void *, unsigned, unsigned);
5ee58eed
MD
156static void printf(const char *,...);
157static void putchar(int);
158static uint32_t memsize(void);
984263bc
MD
159static int drvread(void *, unsigned, unsigned);
160static int keyhit(unsigned);
50a1695f 161static void xputc(int);
984263bc
MD
162static int xgetc(int);
163static int getc(int);
164
5ee58eed
MD
165static void
166memcpy(void *d, const void *s, int len)
984263bc 167{
5ee58eed
MD
168 char *dd = d;
169 const char *ss = s;
984263bc 170
5ee58eed
MD
171#if 0
172 if (dd < ss) {
173 while (--len >= 0)
174 *dd++ = *ss++;
175 } else {
176#endif
177 while (--len >= 0)
178 dd[len] = ss[len];
179#if 0
180 }
181#endif
984263bc
MD
182}
183
184static inline int
185strcmp(const char *s1, const char *s2)
186{
187 for (; *s1 == *s2 && *s1; s1++, s2++);
5ee58eed 188 return (unsigned char)*s1 - (unsigned char)*s2;
984263bc
MD
189}
190
5ee58eed
MD
191#include "ufsread.c"
192
193static int
194xfsread(ino_t inode, void *buf, size_t nbyte)
984263bc 195{
5ee58eed 196 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
4e06dda7 197 printf(INVALID_S, "format");
5ee58eed
MD
198 return -1;
199 }
984263bc
MD
200 return 0;
201}
202
5ee58eed
MD
203static inline uint32_t
204memsize(void)
984263bc 205{
5ee58eed
MD
206 v86.addr = MEM_EXT;
207 v86.eax = 0x8800;
208 v86int();
209 return v86.eax;
984263bc
MD
210}
211
212static inline void
5ee58eed 213getstr(void)
984263bc
MD
214{
215 char *s;
216 int c;
217
5ee58eed
MD
218 s = cmd;
219 for (;;) {
220 switch (c = xgetc(0)) {
984263bc
MD
221 case 0:
222 break;
984263bc 223 case '\177':
5ee58eed
MD
224 case '\b':
225 if (s > cmd) {
984263bc 226 s--;
5ee58eed
MD
227 printf("\b \b");
228 }
984263bc
MD
229 break;
230 case '\n':
5ee58eed 231 case '\r':
984263bc 232 *s = 0;
5ee58eed 233 return;
984263bc 234 default:
5ee58eed 235 if (s - cmd < sizeof(cmd) - 1)
984263bc 236 *s++ = c;
984263bc 237 putchar(c);
5ee58eed
MD
238 }
239 }
984263bc
MD
240}
241
242static inline void
243putc(int c)
244{
245 v86.addr = 0x10;
246 v86.eax = 0xe00 | (c & 0xff);
247 v86.ebx = 0x7;
248 v86int();
249}
250
251int
252main(void)
253{
5ee58eed
MD
254 int autoboot;
255 ino_t ino;
984263bc 256
5ee58eed 257 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
984263bc 258 v86.ctl = V86_FLAGS;
cacaceec 259 dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG);
984263bc
MD
260 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
261 dsk.unit = dsk.drive & DRV_MASK;
cacaceec 262 dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1;
984263bc
MD
263 bootinfo.bi_version = BOOTINFO_VERSION;
264 bootinfo.bi_size = sizeof(bootinfo);
265 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
5ee58eed 266 bootinfo.bi_extmem = memsize();
984263bc 267 bootinfo.bi_memsizes_valid++;
5ee58eed
MD
268
269 /* Process configuration file */
270
271 autoboot = 1;
272
273 if ((ino = lookup(PATH_CONFIG)))
274 fsread(ino, cmd, sizeof(cmd));
275
badbfe61 276 if (cmd[0]) {
984263bc 277 printf("%s: %s", PATH_CONFIG, cmd);
5ee58eed 278 if (parse())
984263bc 279 autoboot = 0;
5ee58eed 280 /* Do not process this command twice */
984263bc
MD
281 *cmd = 0;
282 }
5ee58eed 283
badbfe61 284 /*
38e10dff
MD
285 * Setup our (serial) console after processing the config file. If
286 * the initialization fails, don't try to use the serial port. This
287 * can happen if the serial port is unmaped (happens on new laptops a lot).
badbfe61 288 */
38e10dff 289 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
badbfe61 290 opts |= RBF_SERIAL|RBF_VIDEO;
38e10dff
MD
291 if (opts & RBF_SERIAL) {
292 if (sio_init())
293 opts = RBF_VIDEO;
294 }
badbfe61
MD
295
296
5ee58eed
MD
297 /*
298 * Try to exec stage 3 boot loader. If interrupted by a keypress,
299 * or in case of failure, try to load a kernel directly instead.
300 */
301
984263bc 302 if (autoboot && !*kname) {
5ee58eed
MD
303 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
304 if (!keyhit(3*SECOND)) {
305 load();
984263bc 306 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
5ee58eed 307 }
984263bc 308 }
5ee58eed
MD
309
310 /* Present the user with the boot2 prompt. */
311
984263bc 312 for (;;) {
38e10dff
MD
313 printf("\nDragonFly boot\n"
314 "%u:%s(%u,%c)%s: ",
984263bc
MD
315 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
316 'a' + dsk.part, kname);
5ee58eed
MD
317 if (!autoboot || keyhit(5*SECOND))
318 getstr();
984263bc
MD
319 else
320 putchar('\n');
321 autoboot = 0;
d561b492 322 if (parse())
5ee58eed 323 putchar('\a');
984263bc 324 else
5ee58eed 325 load();
984263bc
MD
326 }
327}
328
329/* XXX - Needed for btxld to link the boot2 binary; do not remove. */
330void
331exit(int x)
332{
333}
334
335static void
5ee58eed 336load(void)
984263bc
MD
337{
338 union {
339 struct exec ex;
340 Elf32_Ehdr eh;
341 } hdr;
342 Elf32_Phdr ep[2];
343 Elf32_Shdr es[2];
344 caddr_t p;
345 ino_t ino;
346 uint32_t addr, x;
347 int fmt, i, j;
348
5ee58eed 349 if (!(ino = lookup(kname))) {
984263bc 350 if (!ls)
5ee58eed 351 printf("No %s\n", kname);
984263bc
MD
352 return;
353 }
354 if (xfsread(ino, &hdr, sizeof(hdr)))
355 return;
356 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
357 fmt = 0;
358 else if (IS_ELF(hdr.eh))
359 fmt = 1;
360 else {
4e06dda7 361 printf(INVALID_S, "format");
984263bc
MD
362 return;
363 }
364 if (fmt == 0) {
365 addr = hdr.ex.a_entry & 0xffffff;
366 p = PTOV(addr);
367 fs_off = PAGE_SIZE;
368 if (xfsread(ino, p, hdr.ex.a_text))
369 return;
370 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
371 if (xfsread(ino, p, hdr.ex.a_data))
372 return;
373 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
374 bootinfo.bi_symtab = VTOP(p);
375 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
376 p += sizeof(hdr.ex.a_syms);
377 if (hdr.ex.a_syms) {
378 if (xfsread(ino, p, hdr.ex.a_syms))
379 return;
380 p += hdr.ex.a_syms;
381 if (xfsread(ino, p, sizeof(int)))
382 return;
383 x = *(uint32_t *)p;
384 p += sizeof(int);
385 x -= sizeof(int);
386 if (xfsread(ino, p, x))
387 return;
388 p += x;
389 }
390 } else {
391 fs_off = hdr.eh.e_phoff;
392 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
393 if (xfsread(ino, ep + j, sizeof(ep[0])))
394 return;
395 if (ep[j].p_type == PT_LOAD)
396 j++;
397 }
398 for (i = 0; i < 2; i++) {
399 p = PTOV(ep[i].p_paddr & 0xffffff);
400 fs_off = ep[i].p_offset;
401 if (xfsread(ino, p, ep[i].p_filesz))
402 return;
403 }
404 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
405 bootinfo.bi_symtab = VTOP(p);
406 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
407 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
408 (hdr.eh.e_shstrndx + 1);
409 if (xfsread(ino, &es, sizeof(es)))
410 return;
411 for (i = 0; i < 2; i++) {
412 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
413 p += sizeof(es[i].sh_size);
414 fs_off = es[i].sh_offset;
415 if (xfsread(ino, p, es[i].sh_size))
416 return;
417 p += es[i].sh_size;
418 }
419 }
420 addr = hdr.eh.e_entry & 0xffffff;
421 }
422 bootinfo.bi_esymtab = VTOP(p);
5ee58eed 423 bootinfo.bi_kernelname = VTOP(kname);
984263bc 424 bootinfo.bi_bios_dev = dsk.drive;
5ee58eed 425 __exec((caddr_t)addr, opts & RBX_MASK,
984263bc
MD
426 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
427 0, 0, 0, VTOP(&bootinfo));
428}
429
430static int
5ee58eed 431parse()
984263bc 432{
5ee58eed 433 char *arg = cmd;
984263bc 434 char *p, *q;
5ee58eed
MD
435 unsigned int drv;
436 int c, i;
984263bc
MD
437
438 while ((c = *arg++)) {
439 if (c == ' ' || c == '\t' || c == '\n')
440 continue;
badbfe61
MD
441 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
442 ;
984263bc
MD
443 if (*p)
444 *p++ = 0;
445 if (c == '-') {
446 while ((c = *arg++)) {
badbfe61
MD
447 for (i = NOPT - 1; i >= 0; --i) {
448 if (optstr[i] == c) {
449 opts ^= 1 << flags[i];
450 goto ok;
451 }
452 }
453 return(-1);
454 ok: ; /* ugly but save space */
984263bc 455 }
badbfe61 456 if (opts & (1 << RBX_PROBEKBD)) {
984263bc 457 i = *(uint8_t *)PTOV(0x496) & 0x10;
2d7f6790
MD
458 if (!i) {
459 printf("NO KB\n");
badbfe61 460 opts |= RBF_VIDEO | RBF_SERIAL;
2d7f6790 461 }
984263bc
MD
462 opts &= ~(1 << RBX_PROBEKBD);
463 }
984263bc
MD
464 } else {
465 for (q = arg--; *q && *q != '('; q++);
466 if (*q) {
467 drv = -1;
468 if (arg[1] == ':') {
984263bc 469 drv = *arg - '0';
5ee58eed
MD
470 if (drv > 9)
471 return (-1);
984263bc
MD
472 arg += 2;
473 }
474 if (q - arg != 2)
475 return -1;
476 for (i = 0; arg[0] != dev_nm[i][0] ||
477 arg[1] != dev_nm[i][1]; i++)
478 if (i == NDEV - 1)
479 return -1;
480 dsk.type = i;
481 arg += 3;
984263bc 482 dsk.unit = *arg - '0';
5ee58eed
MD
483 if (arg[1] != ',' || dsk.unit > 9)
484 return -1;
984263bc
MD
485 arg += 2;
486 dsk.slice = WHOLE_DISK_SLICE;
487 if (arg[1] == ',') {
5ee58eed
MD
488 dsk.slice = *arg - '0' + 1;
489 if (dsk.slice > NDOSPART)
984263bc 490 return -1;
984263bc
MD
491 arg += 2;
492 }
5ee58eed 493 if (arg[1] != ')')
984263bc
MD
494 return -1;
495 dsk.part = *arg - 'a';
5ee58eed
MD
496 if (dsk.part > 7)
497 return (-1);
984263bc
MD
498 arg += 2;
499 if (drv == -1)
500 drv = dsk.unit;
5ee58eed
MD
501 dsk.drive = (dsk.type <= TYPE_MAXHARD
502 ? DRV_HARD : 0) + drv;
503 dsk_meta = 0;
984263bc
MD
504 }
505 if ((i = p - arg - !*(p - 1))) {
5ee58eed 506 if ((size_t)i >= sizeof(kname))
984263bc
MD
507 return -1;
508 memcpy(kname, arg, i + 1);
509 }
510 }
511 arg = p;
512 }
513 return 0;
514}
515
984263bc
MD
516static int
517dskread(void *buf, unsigned lba, unsigned nblk)
518{
984263bc 519 struct dos_partition *dp;
2b961883 520 struct disklabel32 *d;
5ee58eed 521 char *sec;
984263bc
MD
522 unsigned sl, i;
523
5ee58eed
MD
524 if (!dsk_meta) {
525 sec = dmadat->secbuf;
984263bc
MD
526 dsk.start = 0;
527 if (drvread(sec, DOSBBSECTOR, 1))
528 return -1;
529 dp = (void *)(sec + DOSPARTOFF);
530 sl = dsk.slice;
531 if (sl < BASE_SLICE) {
532 for (i = 0; i < NDOSPART; i++)
533 if (dp[i].dp_typ == DOSPTYP_386BSD &&
534 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
535 sl = BASE_SLICE + i;
536 if (dp[i].dp_flag & 0x80 ||
537 dsk.slice == COMPATIBILITY_SLICE)
538 break;
539 }
540 if (dsk.slice == WHOLE_DISK_SLICE)
541 dsk.slice = sl;
542 }
543 if (sl != WHOLE_DISK_SLICE) {
544 if (sl != COMPATIBILITY_SLICE)
545 dp += sl - BASE_SLICE;
546 if (dp->dp_typ != DOSPTYP_386BSD) {
4e06dda7 547 printf(INVALID_S, "slice");
984263bc
MD
548 return -1;
549 }
550 dsk.start = dp->dp_start;
551 }
2b961883 552 if (drvread(sec, dsk.start + LABELSECTOR32, 1))
984263bc 553 return -1;
2b961883
MD
554 d = (void *)(sec + LABELOFFSET32);
555 if (d->d_magic != DISKMAGIC32 || d->d_magic2 != DISKMAGIC32) {
984263bc 556 if (dsk.part != RAW_PART) {
4e06dda7 557 printf(INVALID_S, "label");
984263bc
MD
558 return -1;
559 }
560 } else {
561 if (!dsk.init) {
562 if (d->d_type == DTYPE_SCSI)
563 dsk.type = TYPE_DA;
564 dsk.init++;
565 }
566 if (dsk.part >= d->d_npartitions ||
567 !d->d_partitions[dsk.part].p_size) {
4e06dda7 568 printf(INVALID_S, "partition");
984263bc
MD
569 return -1;
570 }
5ee58eed
MD
571 dsk.start += d->d_partitions[dsk.part].p_offset;
572 dsk.start -= d->d_partitions[RAW_PART].p_offset;
984263bc
MD
573 }
574 }
575 return drvread(buf, dsk.start + lba, nblk);
576}
577
5ee58eed 578static void
984263bc
MD
579printf(const char *fmt,...)
580{
5ee58eed 581 va_list ap;
984263bc
MD
582 char buf[10];
583 char *s;
5ee58eed 584 unsigned u;
984263bc
MD
585 int c;
586
5ee58eed 587 va_start(ap, fmt);
984263bc
MD
588 while ((c = *fmt++)) {
589 if (c == '%') {
590 c = *fmt++;
591 switch (c) {
592 case 'c':
5ee58eed 593 putchar(va_arg(ap, int));
984263bc
MD
594 continue;
595 case 's':
5ee58eed 596 for (s = va_arg(ap, char *); *s; s++)
984263bc
MD
597 putchar(*s);
598 continue;
599 case 'u':
5ee58eed 600 u = va_arg(ap, unsigned);
984263bc
MD
601 s = buf;
602 do
5ee58eed
MD
603 *s++ = '0' + u % 10U;
604 while (u /= 10U);
984263bc
MD
605 while (--s >= buf)
606 putchar(*s);
607 continue;
608 }
609 }
610 putchar(c);
611 }
5ee58eed
MD
612 va_end(ap);
613 return;
984263bc
MD
614}
615
5ee58eed 616static void
984263bc
MD
617putchar(int c)
618{
619 if (c == '\n')
620 xputc('\r');
5ee58eed 621 xputc(c);
984263bc
MD
622}
623
624static int
625drvread(void *buf, unsigned lba, unsigned nblk)
626{
50a1695f 627 static unsigned c = 0x2d5c7c2f; /* twiddle */
984263bc 628
50a1695f
MD
629 c = (c << 8) | (c >> 24);
630 xputc(c);
631 xputc('\b');
984263bc
MD
632 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
633 v86.addr = XREADORG; /* call to xread in boot1 */
634 v86.es = VTOPSEG(buf);
635 v86.eax = lba;
636 v86.ebx = VTOPOFF(buf);
637 v86.ecx = lba >> 16;
638 v86.edx = nblk << 8 | dsk.drive;
639 v86int();
640 v86.ctl = V86_FLAGS;
641 if (V86_CY(v86.efl)) {
5ee58eed 642 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
984263bc
MD
643 return -1;
644 }
645 return 0;
646}
647
648static int
649keyhit(unsigned ticks)
650{
651 uint32_t t0, t1;
652
653 if (opts & 1 << RBX_NOINTR)
654 return 0;
655 t0 = 0;
656 for (;;) {
657 if (xgetc(1))
658 return 1;
659 t1 = *(uint32_t *)PTOV(0x46c);
660 if (!t0)
661 t0 = t1;
662 if (t1 < t0 || t1 >= t0 + ticks)
663 return 0;
664 }
665}
666
50a1695f 667static void
984263bc
MD
668xputc(int c)
669{
badbfe61 670 if (opts & RBF_VIDEO)
984263bc 671 putc(c);
badbfe61 672 if (opts & RBF_SERIAL)
984263bc 673 sio_putc(c);
984263bc
MD
674}
675
676static int
677xgetc(int fn)
678{
679 if (opts & 1 << RBX_NOINTR)
680 return 0;
681 for (;;) {
badbfe61 682 if ((opts & RBF_VIDEO) && getc(1))
984263bc 683 return fn ? 1 : getc(0);
badbfe61 684 if ((opts & RBF_SERIAL) && sio_ischar())
984263bc
MD
685 return fn ? 1 : sio_getc();
686 if (fn)
687 return 0;
688 }
689}
690
691static int
692getc(int fn)
693{
694 v86.addr = 0x16;
695 v86.eax = fn << 8;
696 v86int();
697 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
698}