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