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