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