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