Add more bits for native hammer boot support.
[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 $
7580e818 48 * $DragonFly: src/sys/boot/pc32/boot2/boot2.c,v 1.18 2008/09/13 11:45:45 corecode Exp $
984263bc 49 */
7580e818
SS
50#ifdef HAMMERFS
51#undef __BOOT2_HACK
52#endif
53
984263bc 54#include <sys/param.h>
6080181b
SS
55#ifdef DISKLABEL64
56#include <sys/disklabel64.h>
57#else
2b961883 58#include <sys/disklabel32.h>
6080181b 59#endif
5ee58eed
MD
60#include <sys/diskslice.h>
61#include <sys/diskmbr.h>
ba0cc1ab 62#include <sys/dtype.h>
984263bc
MD
63#include <sys/dirent.h>
64#include <machine/bootinfo.h>
65#include <machine/elf.h>
66
984263bc
MD
67#include <stdarg.h>
68
69#include <a.out.h>
70
71#include <btxv86.h>
72
6080181b
SS
73#ifdef DISKLABEL64
74#include "boot2_64.h"
75#else
76#include "boot2_32.h"
77#endif
78
984263bc 79#include "lib.h"
409cbc03 80#include "../bootasm.h"
984263bc 81
5ee58eed
MD
82#define SECOND 18 /* Circa that many ticks in a second. */
83
984263bc
MD
84#define RBX_ASKNAME 0x0 /* -a */
85#define RBX_SINGLE 0x1 /* -s */
86#define RBX_DFLTROOT 0x5 /* -r */
87#define RBX_KDB 0x6 /* -d */
88#define RBX_CONFIG 0xa /* -c */
89#define RBX_VERBOSE 0xb /* -v */
90#define RBX_SERIAL 0xc /* -h */
91#define RBX_CDROM 0xd /* -C */
92#define RBX_GDB 0xf /* -g */
5ee58eed
MD
93#define RBX_MUTE 0x10 /* -m */
94#define RBX_PAUSE 0x12 /* -p */
95#define RBX_NOINTR 0x1c /* -n */
badbfe61 96#define RBX_VIDEO 0x1d /* -V */
984263bc 97#define RBX_PROBEKBD 0x1e /* -P */
5ee58eed 98/* 0x1f is reserved for the historical RB_BOOTINFO option */
984263bc 99
38e10dff 100#define RBF_MUTE (1 << RBX_MUTE)
badbfe61
MD
101#define RBF_SERIAL (1 << RBX_SERIAL)
102#define RBF_VIDEO (1 << RBX_VIDEO)
103
50a1695f 104/* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
5ee58eed 105#define RBX_MASK 0x2005ffff
984263bc
MD
106
107#define PATH_CONFIG "/boot.config"
79dc5fc6
MD
108#define PATH_BOOT3 "/boot/loader" /* /boot in root */
109#define PATH_BOOT3_ALT "/loader" /* /boot partition */
984263bc
MD
110#define PATH_KERNEL "/kernel"
111
5ee58eed 112#define NDEV 3
984263bc
MD
113#define MEM_BASE 0x12
114#define MEM_EXT 0x15
115#define V86_CY(x) ((x) & 1)
116#define V86_ZR(x) ((x) & 0x40)
117
118#define DRV_HARD 0x80
119#define DRV_MASK 0x7f
120
121#define TYPE_AD 0
5ee58eed
MD
122#define TYPE_DA 1
123#define TYPE_MAXHARD TYPE_DA
124#define TYPE_FD 2
984263bc 125
badbfe61
MD
126#define NOPT 12
127
4e06dda7
MD
128#define INVALID_S "Bad %s\n"
129
984263bc
MD
130extern uint32_t _end;
131
badbfe61 132static const char optstr[NOPT] = { "VhaCgmnPprsv" };
984263bc 133static const unsigned char flags[NOPT] = {
badbfe61 134 RBX_VIDEO,
984263bc
MD
135 RBX_SERIAL,
136 RBX_ASKNAME,
137 RBX_CDROM,
984263bc 138 RBX_GDB,
5ee58eed 139 RBX_MUTE,
984263bc
MD
140 RBX_NOINTR,
141 RBX_PROBEKBD,
5ee58eed 142 RBX_PAUSE,
984263bc
MD
143 RBX_DFLTROOT,
144 RBX_SINGLE,
145 RBX_VERBOSE
146};
147
5ee58eed
MD
148static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
149static const unsigned char dev_maj[NDEV] = {30, 4, 2};
984263bc
MD
150
151static struct dsk {
152 unsigned drive;
153 unsigned type;
154 unsigned unit;
155 unsigned slice;
156 unsigned part;
157 unsigned start;
158 int init;
984263bc
MD
159} dsk;
160static char cmd[512];
161static char kname[1024];
162static uint32_t opts;
163static struct bootinfo bootinfo;
984263bc 164
7580e818
SS
165static int ls, dsk_meta;
166static uint32_t fs_off;
167
984263bc 168void exit(int);
5ee58eed
MD
169static void load(void);
170static int parse(void);
984263bc 171static int xfsread(ino_t, void *, size_t);
984263bc 172static int dskread(void *, unsigned, unsigned);
5ee58eed
MD
173static void printf(const char *,...);
174static void putchar(int);
175static uint32_t memsize(void);
984263bc
MD
176static int drvread(void *, unsigned, unsigned);
177static int keyhit(unsigned);
50a1695f 178static void xputc(int);
984263bc
MD
179static int xgetc(int);
180static int getc(int);
181
5ee58eed
MD
182static void
183memcpy(void *d, const void *s, int len)
984263bc 184{
5ee58eed
MD
185 char *dd = d;
186 const char *ss = s;
984263bc 187
5ee58eed
MD
188#if 0
189 if (dd < ss) {
190 while (--len >= 0)
191 *dd++ = *ss++;
192 } else {
193#endif
194 while (--len >= 0)
195 dd[len] = ss[len];
196#if 0
197 }
198#endif
984263bc
MD
199}
200
201static inline int
202strcmp(const char *s1, const char *s2)
203{
204 for (; *s1 == *s2 && *s1; s1++, s2++);
5ee58eed 205 return (unsigned char)*s1 - (unsigned char)*s2;
984263bc
MD
206}
207
7580e818
SS
208#if HAMMERFS
209#include "hammerread.c"
210#else
5ee58eed 211#include "ufsread.c"
7580e818 212#endif
5ee58eed
MD
213
214static int
215xfsread(ino_t inode, void *buf, size_t nbyte)
984263bc 216{
5ee58eed 217 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
4e06dda7 218 printf(INVALID_S, "format");
5ee58eed
MD
219 return -1;
220 }
984263bc
MD
221 return 0;
222}
223
5ee58eed
MD
224static inline uint32_t
225memsize(void)
984263bc 226{
5ee58eed
MD
227 v86.addr = MEM_EXT;
228 v86.eax = 0x8800;
229 v86int();
230 return v86.eax;
984263bc
MD
231}
232
233static inline void
5ee58eed 234getstr(void)
984263bc
MD
235{
236 char *s;
237 int c;
238
5ee58eed
MD
239 s = cmd;
240 for (;;) {
241 switch (c = xgetc(0)) {
984263bc
MD
242 case 0:
243 break;
984263bc 244 case '\177':
5ee58eed
MD
245 case '\b':
246 if (s > cmd) {
984263bc 247 s--;
5ee58eed
MD
248 printf("\b \b");
249 }
984263bc
MD
250 break;
251 case '\n':
5ee58eed 252 case '\r':
984263bc 253 *s = 0;
5ee58eed 254 return;
984263bc 255 default:
5ee58eed 256 if (s - cmd < sizeof(cmd) - 1)
984263bc 257 *s++ = c;
984263bc 258 putchar(c);
5ee58eed
MD
259 }
260 }
984263bc
MD
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{
5ee58eed
MD
275 int autoboot;
276 ino_t ino;
984263bc 277
5ee58eed 278 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
984263bc 279 v86.ctl = V86_FLAGS;
cacaceec 280 dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG);
984263bc
MD
281 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
282 dsk.unit = dsk.drive & DRV_MASK;
cacaceec 283 dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1;
984263bc
MD
284 bootinfo.bi_version = BOOTINFO_VERSION;
285 bootinfo.bi_size = sizeof(bootinfo);
286 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
5ee58eed 287 bootinfo.bi_extmem = memsize();
984263bc 288 bootinfo.bi_memsizes_valid++;
5ee58eed
MD
289
290 /* Process configuration file */
291
292 autoboot = 1;
293
294 if ((ino = lookup(PATH_CONFIG)))
295 fsread(ino, cmd, sizeof(cmd));
296
badbfe61 297 if (cmd[0]) {
984263bc 298 printf("%s: %s", PATH_CONFIG, cmd);
5ee58eed 299 if (parse())
984263bc 300 autoboot = 0;
5ee58eed 301 /* Do not process this command twice */
984263bc
MD
302 *cmd = 0;
303 }
5ee58eed
MD
304
305 /*
38e10dff
MD
306 * Setup our (serial) console after processing the config file. If
307 * the initialization fails, don't try to use the serial port. This
308 * can happen if the serial port is unmaped (happens on new laptops a lot).
badbfe61 309 */
38e10dff 310 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
badbfe61 311 opts |= RBF_SERIAL|RBF_VIDEO;
38e10dff
MD
312 if (opts & RBF_SERIAL) {
313 if (sio_init())
314 opts = RBF_VIDEO;
315 }
badbfe61
MD
316
317
318 /*
5ee58eed
MD
319 * Try to exec stage 3 boot loader. If interrupted by a keypress,
320 * or in case of failure, try to load a kernel directly instead.
79dc5fc6
MD
321 *
322 * We have to try boot /boot/loader and /loader to support booting
323 * from a /boot partition instead of a root partition.
5ee58eed 324 */
984263bc 325 if (autoboot && !*kname) {
5ee58eed
MD
326 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
327 if (!keyhit(3*SECOND)) {
328 load();
79dc5fc6
MD
329 memcpy(kname, PATH_BOOT3_ALT, sizeof(PATH_BOOT3_ALT));
330 load();
984263bc 331 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
5ee58eed 332 }
984263bc 333 }
5ee58eed
MD
334
335 /* Present the user with the boot2 prompt. */
336
984263bc 337 for (;;) {
38e10dff
MD
338 printf("\nDragonFly boot\n"
339 "%u:%s(%u,%c)%s: ",
984263bc
MD
340 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
341 'a' + dsk.part, kname);
5ee58eed
MD
342 if (!autoboot || keyhit(5*SECOND))
343 getstr();
984263bc
MD
344 else
345 putchar('\n');
346 autoboot = 0;
d561b492 347 if (parse())
5ee58eed 348 putchar('\a');
984263bc 349 else
5ee58eed 350 load();
984263bc
MD
351 }
352}
353
354/* XXX - Needed for btxld to link the boot2 binary; do not remove. */
355void
356exit(int x)
357{
358}
359
360static void
5ee58eed 361load(void)
984263bc
MD
362{
363 union {
364 struct exec ex;
365 Elf32_Ehdr eh;
366 } hdr;
367 Elf32_Phdr ep[2];
368 Elf32_Shdr es[2];
369 caddr_t p;
370 ino_t ino;
371 uint32_t addr, x;
372 int fmt, i, j;
373
5ee58eed 374 if (!(ino = lookup(kname))) {
984263bc 375 if (!ls)
5ee58eed 376 printf("No %s\n", kname);
984263bc
MD
377 return;
378 }
379 if (xfsread(ino, &hdr, sizeof(hdr)))
380 return;
381 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
382 fmt = 0;
383 else if (IS_ELF(hdr.eh))
384 fmt = 1;
385 else {
4e06dda7 386 printf(INVALID_S, "format");
984263bc
MD
387 return;
388 }
389 if (fmt == 0) {
390 addr = hdr.ex.a_entry & 0xffffff;
391 p = PTOV(addr);
392 fs_off = PAGE_SIZE;
393 if (xfsread(ino, p, hdr.ex.a_text))
394 return;
395 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
396 if (xfsread(ino, p, hdr.ex.a_data))
397 return;
398 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
399 bootinfo.bi_symtab = VTOP(p);
400 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
401 p += sizeof(hdr.ex.a_syms);
402 if (hdr.ex.a_syms) {
403 if (xfsread(ino, p, hdr.ex.a_syms))
404 return;
405 p += hdr.ex.a_syms;
406 if (xfsread(ino, p, sizeof(int)))
407 return;
408 x = *(uint32_t *)p;
409 p += sizeof(int);
410 x -= sizeof(int);
411 if (xfsread(ino, p, x))
412 return;
413 p += x;
414 }
415 } else {
416 fs_off = hdr.eh.e_phoff;
417 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
418 if (xfsread(ino, ep + j, sizeof(ep[0])))
419 return;
420 if (ep[j].p_type == PT_LOAD)
421 j++;
422 }
423 for (i = 0; i < 2; i++) {
424 p = PTOV(ep[i].p_paddr & 0xffffff);
425 fs_off = ep[i].p_offset;
426 if (xfsread(ino, p, ep[i].p_filesz))
427 return;
428 }
429 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
430 bootinfo.bi_symtab = VTOP(p);
431 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
432 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
433 (hdr.eh.e_shstrndx + 1);
434 if (xfsread(ino, &es, sizeof(es)))
435 return;
436 for (i = 0; i < 2; i++) {
437 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
438 p += sizeof(es[i].sh_size);
439 fs_off = es[i].sh_offset;
440 if (xfsread(ino, p, es[i].sh_size))
441 return;
442 p += es[i].sh_size;
443 }
444 }
445 addr = hdr.eh.e_entry & 0xffffff;
446 }
447 bootinfo.bi_esymtab = VTOP(p);
5ee58eed 448 bootinfo.bi_kernelname = VTOP(kname);
984263bc 449 bootinfo.bi_bios_dev = dsk.drive;
5ee58eed 450 __exec((caddr_t)addr, opts & RBX_MASK,
984263bc
MD
451 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
452 0, 0, 0, VTOP(&bootinfo));
453}
454
455static int
5ee58eed 456parse()
984263bc 457{
5ee58eed 458 char *arg = cmd;
984263bc 459 char *p, *q;
5ee58eed
MD
460 unsigned int drv;
461 int c, i;
984263bc
MD
462
463 while ((c = *arg++)) {
464 if (c == ' ' || c == '\t' || c == '\n')
465 continue;
badbfe61
MD
466 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
467 ;
984263bc
MD
468 if (*p)
469 *p++ = 0;
470 if (c == '-') {
471 while ((c = *arg++)) {
badbfe61
MD
472 for (i = NOPT - 1; i >= 0; --i) {
473 if (optstr[i] == c) {
474 opts ^= 1 << flags[i];
475 goto ok;
476 }
477 }
478 return(-1);
479 ok: ; /* ugly but save space */
984263bc 480 }
badbfe61 481 if (opts & (1 << RBX_PROBEKBD)) {
984263bc 482 i = *(uint8_t *)PTOV(0x496) & 0x10;
2d7f6790
MD
483 if (!i) {
484 printf("NO KB\n");
badbfe61 485 opts |= RBF_VIDEO | RBF_SERIAL;
2d7f6790 486 }
984263bc
MD
487 opts &= ~(1 << RBX_PROBEKBD);
488 }
984263bc
MD
489 } else {
490 for (q = arg--; *q && *q != '('; q++);
491 if (*q) {
492 drv = -1;
493 if (arg[1] == ':') {
984263bc 494 drv = *arg - '0';
5ee58eed
MD
495 if (drv > 9)
496 return (-1);
984263bc
MD
497 arg += 2;
498 }
499 if (q - arg != 2)
500 return -1;
501 for (i = 0; arg[0] != dev_nm[i][0] ||
502 arg[1] != dev_nm[i][1]; i++)
503 if (i == NDEV - 1)
504 return -1;
505 dsk.type = i;
506 arg += 3;
984263bc 507 dsk.unit = *arg - '0';
5ee58eed
MD
508 if (arg[1] != ',' || dsk.unit > 9)
509 return -1;
984263bc
MD
510 arg += 2;
511 dsk.slice = WHOLE_DISK_SLICE;
512 if (arg[1] == ',') {
5ee58eed
MD
513 dsk.slice = *arg - '0' + 1;
514 if (dsk.slice > NDOSPART)
984263bc 515 return -1;
984263bc
MD
516 arg += 2;
517 }
5ee58eed 518 if (arg[1] != ')')
984263bc
MD
519 return -1;
520 dsk.part = *arg - 'a';
5ee58eed
MD
521 if (dsk.part > 7)
522 return (-1);
984263bc
MD
523 arg += 2;
524 if (drv == -1)
525 drv = dsk.unit;
5ee58eed
MD
526 dsk.drive = (dsk.type <= TYPE_MAXHARD
527 ? DRV_HARD : 0) + drv;
528 dsk_meta = 0;
984263bc
MD
529 }
530 if ((i = p - arg - !*(p - 1))) {
5ee58eed 531 if ((size_t)i >= sizeof(kname))
984263bc
MD
532 return -1;
533 memcpy(kname, arg, i + 1);
534 }
535 }
536 arg = p;
537 }
538 return 0;
539}
540
984263bc
MD
541static int
542dskread(void *buf, unsigned lba, unsigned nblk)
543{
984263bc 544 struct dos_partition *dp;
6080181b
SS
545#ifdef DISKLABEL64
546 struct disklabel64 *d;
547#else
2b961883 548 struct disklabel32 *d;
6080181b 549#endif
5ee58eed 550 char *sec;
984263bc
MD
551 unsigned sl, i;
552
5ee58eed
MD
553 if (!dsk_meta) {
554 sec = dmadat->secbuf;
984263bc
MD
555 dsk.start = 0;
556 if (drvread(sec, DOSBBSECTOR, 1))
557 return -1;
558 dp = (void *)(sec + DOSPARTOFF);
559 sl = dsk.slice;
560 if (sl < BASE_SLICE) {
561 for (i = 0; i < NDOSPART; i++)
562 if (dp[i].dp_typ == DOSPTYP_386BSD &&
563 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
564 sl = BASE_SLICE + i;
565 if (dp[i].dp_flag & 0x80 ||
566 dsk.slice == COMPATIBILITY_SLICE)
567 break;
568 }
569 if (dsk.slice == WHOLE_DISK_SLICE)
570 dsk.slice = sl;
571 }
572 if (sl != WHOLE_DISK_SLICE) {
573 if (sl != COMPATIBILITY_SLICE)
574 dp += sl - BASE_SLICE;
575 if (dp->dp_typ != DOSPTYP_386BSD) {
4e06dda7 576 printf(INVALID_S, "slice");
984263bc
MD
577 return -1;
578 }
579 dsk.start = dp->dp_start;
580 }
6080181b
SS
581#ifdef DISKLABEL64
582 if (drvread(sec, dsk.start, (sizeof(struct disklabel64) + 511) / 512))
583 return -1;
584 d = (void *)sec;
585 if (d->d_magic != DISKMAGIC64) {
586 printf(INVALID_S, "label");
587 return -1;
588 } else {
589 if (dsk.part >= d->d_npartitions || d->d_partitions[dsk.part].p_bsize == 0) {
590 printf(INVALID_S, "partition");
591 return -1;
592 }
593 dsk.start += d->d_partitions[dsk.part].p_boffset / 512;
594 }
595#else
2b961883 596 if (drvread(sec, dsk.start + LABELSECTOR32, 1))
984263bc 597 return -1;
2b961883
MD
598 d = (void *)(sec + LABELOFFSET32);
599 if (d->d_magic != DISKMAGIC32 || d->d_magic2 != DISKMAGIC32) {
984263bc 600 if (dsk.part != RAW_PART) {
4e06dda7 601 printf(INVALID_S, "label");
984263bc
MD
602 return -1;
603 }
604 } else {
605 if (!dsk.init) {
606 if (d->d_type == DTYPE_SCSI)
607 dsk.type = TYPE_DA;
608 dsk.init++;
609 }
610 if (dsk.part >= d->d_npartitions ||
611 !d->d_partitions[dsk.part].p_size) {
4e06dda7 612 printf(INVALID_S, "partition");
984263bc
MD
613 return -1;
614 }
5ee58eed
MD
615 dsk.start += d->d_partitions[dsk.part].p_offset;
616 dsk.start -= d->d_partitions[RAW_PART].p_offset;
984263bc 617 }
6080181b 618#endif
984263bc
MD
619 }
620 return drvread(buf, dsk.start + lba, nblk);
621}
622
5ee58eed 623static void
984263bc
MD
624printf(const char *fmt,...)
625{
5ee58eed 626 va_list ap;
984263bc
MD
627 char buf[10];
628 char *s;
5ee58eed 629 unsigned u;
984263bc
MD
630 int c;
631
5ee58eed 632 va_start(ap, fmt);
984263bc
MD
633 while ((c = *fmt++)) {
634 if (c == '%') {
635 c = *fmt++;
636 switch (c) {
637 case 'c':
5ee58eed 638 putchar(va_arg(ap, int));
984263bc
MD
639 continue;
640 case 's':
5ee58eed 641 for (s = va_arg(ap, char *); *s; s++)
984263bc
MD
642 putchar(*s);
643 continue;
644 case 'u':
5ee58eed 645 u = va_arg(ap, unsigned);
984263bc
MD
646 s = buf;
647 do
5ee58eed
MD
648 *s++ = '0' + u % 10U;
649 while (u /= 10U);
984263bc
MD
650 while (--s >= buf)
651 putchar(*s);
652 continue;
653 }
654 }
655 putchar(c);
656 }
5ee58eed
MD
657 va_end(ap);
658 return;
984263bc
MD
659}
660
5ee58eed 661static void
984263bc
MD
662putchar(int c)
663{
664 if (c == '\n')
665 xputc('\r');
5ee58eed 666 xputc(c);
984263bc
MD
667}
668
669static int
670drvread(void *buf, unsigned lba, unsigned nblk)
671{
50a1695f 672 static unsigned c = 0x2d5c7c2f; /* twiddle */
984263bc 673
50a1695f
MD
674 c = (c << 8) | (c >> 24);
675 xputc(c);
676 xputc('\b');
984263bc
MD
677 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
678 v86.addr = XREADORG; /* call to xread in boot1 */
679 v86.es = VTOPSEG(buf);
680 v86.eax = lba;
681 v86.ebx = VTOPOFF(buf);
682 v86.ecx = lba >> 16;
683 v86.edx = nblk << 8 | dsk.drive;
684 v86int();
685 v86.ctl = V86_FLAGS;
686 if (V86_CY(v86.efl)) {
5ee58eed 687 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
984263bc
MD
688 return -1;
689 }
690 return 0;
691}
692
693static int
694keyhit(unsigned ticks)
695{
696 uint32_t t0, t1;
697
698 if (opts & 1 << RBX_NOINTR)
699 return 0;
700 t0 = 0;
701 for (;;) {
702 if (xgetc(1))
703 return 1;
704 t1 = *(uint32_t *)PTOV(0x46c);
705 if (!t0)
706 t0 = t1;
707 if (t1 < t0 || t1 >= t0 + ticks)
708 return 0;
709 }
710}
711
50a1695f 712static void
984263bc
MD
713xputc(int c)
714{
badbfe61 715 if (opts & RBF_VIDEO)
984263bc 716 putc(c);
badbfe61 717 if (opts & RBF_SERIAL)
984263bc 718 sio_putc(c);
984263bc
MD
719}
720
721static int
722xgetc(int fn)
723{
724 if (opts & 1 << RBX_NOINTR)
725 return 0;
726 for (;;) {
badbfe61 727 if ((opts & RBF_VIDEO) && getc(1))
984263bc 728 return fn ? 1 : getc(0);
badbfe61 729 if ((opts & RBF_SERIAL) && sio_ischar())
984263bc
MD
730 return fn ? 1 : sio_getc();
731 if (fn)
732 return 0;
733 }
734}
735
736static int
737getc(int fn)
738{
739 v86.addr = 0x16;
740 v86.eax = fn << 8;
741 v86int();
742 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
743}