2 * Copyright (c) 1998 Robert Nordier
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
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
17 * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.28.2.7 2002/10/10 15:53:24 iwasaki Exp $
18 * $DragonFly: src/sys/boot/i386/boot2/Attic/boot2.c,v 1.3 2003/08/07 21:16:44 dillon Exp $
21 #include <sys/param.h>
22 #include <sys/reboot.h>
23 #include <sys/diskslice.h>
24 #include <sys/disklabel.h>
25 #include <sys/dirent.h>
26 #include <machine/bootinfo.h>
27 #include <machine/elf.h>
29 #include <vfs/ufs/fs.h>
30 #include <vfs/ufs/dinode.h>
41 #define RBX_ASKNAME 0x0 /* -a */
42 #define RBX_SINGLE 0x1 /* -s */
43 #define RBX_DFLTROOT 0x5 /* -r */
44 #define RBX_KDB 0x6 /* -d */
45 #define RBX_CONFIG 0xa /* -c */
46 #define RBX_VERBOSE 0xb /* -v */
47 #define RBX_SERIAL 0xc /* -h */
48 #define RBX_CDROM 0xd /* -C */
49 #define RBX_GDB 0xf /* -g */
50 #define RBX_DUAL 0x1d /* -D */
51 #define RBX_PROBEKBD 0x1e /* -P */
52 #define RBX_NOINTR 0x1f /* -n */
54 #define RBX_MASK 0xffff
56 #define PATH_CONFIG "/boot.config"
57 #define PATH_BOOT3 "/boot/loader"
58 #define PATH_KERNEL "/kernel"
62 #define BSIZEMAX 16384
66 #define V86_CY(x) ((x) & 1)
67 #define V86_ZR(x) ((x) & 0x40)
80 static const char optstr[NOPT] = "DhaCcdgnPrsv";
81 static const unsigned char flags[NOPT] = {
96 static const char *const dev_nm[] = {"ad", "wd", " ", "fd", "da"};
97 static const unsigned dev_maj[] = {30, 0, 1, 2, 4};
109 static char cmd[512];
110 static char kname[1024];
111 static uint32_t opts;
112 static struct bootinfo bootinfo;
114 static uint32_t fs_off;
115 static uint8_t ioctrl = 0x1;
118 static void load(const char *);
119 static int parse(char *);
120 static ino_t lookup(const char *);
121 static int xfsread(ino_t, void *, size_t);
122 static ssize_t fsread(ino_t, void *, size_t);
123 static int dskread(void *, unsigned, unsigned);
124 static int printf(const char *,...);
125 static int putchar(int);
126 static void *memcpy(void *, const void *, size_t);
127 static void *malloc(size_t);
128 static uint32_t memsize(int);
129 static int drvread(void *, unsigned, unsigned);
130 static int keyhit(unsigned);
131 static int xputc(int);
132 static int xgetc(int);
133 static int getc(int);
136 readfile(const char *fname, void *buf, size_t size)
140 if ((ino = lookup(fname)))
141 fsread(ino, buf, size);
145 strcmp(const char *s1, const char *s2)
147 for (; *s1 == *s2 && *s1; s1++, s2++);
148 return (u_char)*s1 - (u_char)*s2;
152 fsfind(const char *name, ino_t * ino)
160 while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
161 for (s = buf; s < buf + DEV_BSIZE;) {
164 printf("%s ", d->d_name);
165 else if (!strcmp(name, d->d_name)) {
188 getstr(char *str, int size)
195 switch (c = getchar()) {
211 if (s - str < size - 1)
219 static inline uint32_t
224 v86.edx = DRV_HARD + drive;
228 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
229 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
236 v86.eax = 0xe00 | (c & 0xff);
247 dsk.drive = *(uint8_t *)PTOV(ARGS);
248 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
249 dsk.unit = dsk.drive & DRV_MASK;
250 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
251 bootinfo.bi_version = BOOTINFO_VERSION;
252 bootinfo.bi_size = sizeof(bootinfo);
253 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
254 bootinfo.bi_extmem = memsize(MEM_EXT);
255 bootinfo.bi_memsizes_valid++;
256 for (i = 0; i < N_BIOS_GEOM; i++)
257 bootinfo.bi_bios_geom[i] = drvinfo(i);
259 readfile(PATH_CONFIG, cmd, sizeof(cmd));
261 printf("%s: %s", PATH_CONFIG, cmd);
266 if (autoboot && !*kname) {
268 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
275 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
278 printf(" \n>> FreeBSD/i386 BOOT\n"
279 "Default: %u:%s(%u,%c)%s\n"
281 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
282 'a' + dsk.part, kname);
285 if (!autoboot || keyhit(0x5a))
286 getstr(cmd, sizeof(cmd));
297 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
304 load(const char *fname)
317 if (!(ino = lookup(fname))) {
319 printf("No %s\n", fname);
322 if (xfsread(ino, &hdr, sizeof(hdr)))
324 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
326 else if (IS_ELF(hdr.eh))
329 printf("Invalid %s\n", "format");
333 addr = hdr.ex.a_entry & 0xffffff;
336 if (xfsread(ino, p, hdr.ex.a_text))
338 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
339 if (xfsread(ino, p, hdr.ex.a_data))
341 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
342 bootinfo.bi_symtab = VTOP(p);
343 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
344 p += sizeof(hdr.ex.a_syms);
346 if (xfsread(ino, p, hdr.ex.a_syms))
349 if (xfsread(ino, p, sizeof(int)))
354 if (xfsread(ino, p, x))
359 fs_off = hdr.eh.e_phoff;
360 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
361 if (xfsread(ino, ep + j, sizeof(ep[0])))
363 if (ep[j].p_type == PT_LOAD)
366 for (i = 0; i < 2; i++) {
367 p = PTOV(ep[i].p_paddr & 0xffffff);
368 fs_off = ep[i].p_offset;
369 if (xfsread(ino, p, ep[i].p_filesz))
372 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
373 bootinfo.bi_symtab = VTOP(p);
374 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
375 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
376 (hdr.eh.e_shstrndx + 1);
377 if (xfsread(ino, &es, sizeof(es)))
379 for (i = 0; i < 2; i++) {
380 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
381 p += sizeof(es[i].sh_size);
382 fs_off = es[i].sh_offset;
383 if (xfsread(ino, p, es[i].sh_size))
388 addr = hdr.eh.e_entry & 0xffffff;
390 bootinfo.bi_esymtab = VTOP(p);
391 bootinfo.bi_kernelname = VTOP(fname);
392 bootinfo.bi_bios_dev = dsk.drive;
393 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
394 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
395 0, 0, 0, VTOP(&bootinfo));
404 while ((c = *arg++)) {
405 if (c == ' ' || c == '\t' || c == '\n')
407 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
411 while ((c = *arg++)) {
412 for (i = 0; c != optstr[i]; i++)
415 opts ^= 1 << flags[i];
417 if (opts & 1 << RBX_PROBEKBD) {
418 i = *(uint8_t *)PTOV(0x496) & 0x10;
419 printf("Keyboard: %s\n", i ? "yes" : "no");
421 opts |= 1 << RBX_DUAL | 1 << RBX_SERIAL;
422 opts &= ~(1 << RBX_PROBEKBD);
424 ioctrl = opts & 1 << RBX_DUAL ? 0x3 :
425 opts & 1 << RBX_SERIAL ? 0x2 : 0x1;
429 for (q = arg--; *q && *q != '('; q++);
433 if (*arg < '0' || *arg > '9')
440 for (i = 0; arg[0] != dev_nm[i][0] ||
441 arg[1] != dev_nm[i][1]; i++)
446 if (arg[1] != ',' || *arg < '0' || *arg > '9')
448 dsk.unit = *arg - '0';
450 dsk.slice = WHOLE_DISK_SLICE;
452 if (*arg < '0' || *arg > '0' + NDOSPART)
454 if ((dsk.slice = *arg - '0'))
458 if (arg[1] != ')' || *arg < 'a' || *arg > 'p')
460 dsk.part = *arg - 'a';
464 dsk.drive = (dsk.type == TYPE_WD ||
465 dsk.type == TYPE_AD ||
466 dsk.type == TYPE_DA ? DRV_HARD : 0) + drv;
470 if ((i = p - arg - !*(p - 1))) {
471 if (i >= sizeof(kname))
473 memcpy(kname, arg, i + 1);
482 lookup(const char *path)
484 char name[MAXNAMLEN + 1];
497 for (s = path; *s && *s != '/'; s++);
498 if ((n = s - path) > MAXNAMLEN)
500 ls = *path == '?' && n == 1 && !*s;
501 memcpy(name, path, n);
503 if ((dt = fsfind(name, &ino)) <= 0)
507 return dt == DT_REG ? ino : 0;
510 xfsread(ino_t inode, void *buf, size_t nbyte)
512 if (fsread(inode, buf, nbyte) != nbyte) {
513 printf("Invalid %s\n", "format");
520 fsread(ino_t inode, void *buf, size_t nbyte)
523 static struct dinode din;
525 static ufs_daddr_t *indbuf;
527 static ufs_daddr_t blkmap, indmap;
528 static unsigned fsblks;
530 ufs_daddr_t lbn, addr;
535 blkbuf = malloc(BSIZEMAX);
537 if (dskread(blkbuf, SBOFF / DEV_BSIZE, SBSIZE / DEV_BSIZE))
539 memcpy(&fs, blkbuf, sizeof(fs));
540 if (fs.fs_magic != FS_MAGIC) {
544 fsblks = fs.fs_bsize >> DEV_BSHIFT;
549 if (inomap != inode) {
550 if (dskread(blkbuf, fsbtodb(&fs, ino_to_fsba(&fs, inode)),
553 din = ((struct dinode *)blkbuf)[inode % INOPB(&fs)];
559 if (nbyte > (n = din.di_size - fs_off))
563 lbn = lblkno(&fs, fs_off);
565 addr = din.di_db[lbn];
567 if (indmap != din.di_ib[0]) {
569 indbuf = malloc(BSIZEMAX);
570 if (dskread(indbuf, fsbtodb(&fs, din.di_ib[0]),
573 indmap = din.di_ib[0];
575 addr = indbuf[(lbn - NDADDR) % NINDIR(&fs)];
577 n = dblksize(&fs, &din, lbn);
578 if (blkmap != addr) {
579 if (dskread(blkbuf, fsbtodb(&fs, addr), n >> DEV_BSHIFT))
583 off = blkoff(&fs, fs_off);
587 memcpy(s, blkbuf + off, n);
596 dskread(void *buf, unsigned lba, unsigned nblk)
599 struct dos_partition *dp;
605 sec = malloc(DEV_BSIZE);
607 if (drvread(sec, DOSBBSECTOR, 1))
609 dp = (void *)(sec + DOSPARTOFF);
611 if (sl < BASE_SLICE) {
612 for (i = 0; i < NDOSPART; i++)
613 if (dp[i].dp_typ == DOSPTYP_386BSD &&
614 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
616 if (dp[i].dp_flag & 0x80 ||
617 dsk.slice == COMPATIBILITY_SLICE)
620 if (dsk.slice == WHOLE_DISK_SLICE)
623 if (sl != WHOLE_DISK_SLICE) {
624 if (sl != COMPATIBILITY_SLICE)
625 dp += sl - BASE_SLICE;
626 if (dp->dp_typ != DOSPTYP_386BSD) {
627 printf("Invalid %s\n", "slice");
630 dsk.start = dp->dp_start;
632 if (drvread(sec, dsk.start + LABELSECTOR, 1))
634 d = (void *)(sec + LABELOFFSET);
635 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
636 if (dsk.part != RAW_PART) {
637 printf("Invalid %s\n", "label");
642 if (d->d_type == DTYPE_SCSI)
646 if (dsk.part >= d->d_npartitions ||
647 !d->d_partitions[dsk.part].p_size) {
648 printf("Invalid %s\n", "partition");
651 dsk.start = d->d_partitions[dsk.part].p_offset;
654 return drvread(buf, dsk.start + lba, nblk);
658 printf(const char *fmt,...)
660 static const char digits[16] = "0123456789abcdef";
668 while ((c = *fmt++)) {
673 putchar(va_arg(ap, int));
676 for (s = va_arg(ap, char *); *s; s++)
681 r = c == 'u' ? 10U : 16U;
682 u = va_arg(ap, unsigned);
685 *s++ = digits[u % r];
707 memcpy(void *dst, const void *src, size_t size)
712 for (d = dst, s = src; size; size--)
720 static uint32_t next;
724 next = roundup2(__base + _end, 0x10000) - __base;
740 drvread(void *buf, unsigned lba, unsigned nblk)
742 static unsigned c = 0x2d5c7c2f;
744 printf("%c\b", c = c << 8 | c >> 24);
745 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
746 v86.addr = XREADORG; /* call to xread in boot1 */
747 v86.es = VTOPSEG(buf);
749 v86.ebx = VTOPOFF(buf);
751 v86.edx = nblk << 8 | dsk.drive;
754 if (V86_CY(v86.efl)) {
755 printf("Disk error 0x%x (lba=0x%x)\n", v86.eax >> 8 & 0xff,
763 keyhit(unsigned ticks)
767 if (opts & 1 << RBX_NOINTR)
773 t1 = *(uint32_t *)PTOV(0x46c);
776 if (t1 < t0 || t1 >= t0 + ticks)
794 if (opts & 1 << RBX_NOINTR)
797 if (ioctrl & 0x1 && getc(1))
798 return fn ? 1 : getc(0);
799 if (ioctrl & 0x2 && sio_ischar())
800 return fn ? 1 : sio_getc();
812 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);