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 $
20 #include <sys/param.h>
21 #include <sys/reboot.h>
22 #include <sys/diskslice.h>
23 #include <sys/disklabel.h>
24 #include <sys/dirent.h>
25 #include <machine/bootinfo.h>
26 #include <machine/elf.h>
28 #include <ufs/ffs/fs.h>
29 #include <ufs/ufs/dinode.h>
40 #define RBX_ASKNAME 0x0 /* -a */
41 #define RBX_SINGLE 0x1 /* -s */
42 #define RBX_DFLTROOT 0x5 /* -r */
43 #define RBX_KDB 0x6 /* -d */
44 #define RBX_CONFIG 0xa /* -c */
45 #define RBX_VERBOSE 0xb /* -v */
46 #define RBX_SERIAL 0xc /* -h */
47 #define RBX_CDROM 0xd /* -C */
48 #define RBX_GDB 0xf /* -g */
49 #define RBX_DUAL 0x1d /* -D */
50 #define RBX_PROBEKBD 0x1e /* -P */
51 #define RBX_NOINTR 0x1f /* -n */
53 #define RBX_MASK 0xffff
55 #define PATH_CONFIG "/boot.config"
56 #define PATH_BOOT3 "/boot/loader"
57 #define PATH_KERNEL "/kernel"
61 #define BSIZEMAX 16384
65 #define V86_CY(x) ((x) & 1)
66 #define V86_ZR(x) ((x) & 0x40)
79 static const char optstr[NOPT] = "DhaCcdgnPrsv";
80 static const unsigned char flags[NOPT] = {
95 static const char *const dev_nm[] = {"ad", "wd", " ", "fd", "da"};
96 static const unsigned dev_maj[] = {30, 0, 1, 2, 4};
108 static char cmd[512];
109 static char kname[1024];
110 static uint32_t opts;
111 static struct bootinfo bootinfo;
113 static uint32_t fs_off;
114 static uint8_t ioctrl = 0x1;
117 static void load(const char *);
118 static int parse(char *);
119 static ino_t lookup(const char *);
120 static int xfsread(ino_t, void *, size_t);
121 static ssize_t fsread(ino_t, void *, size_t);
122 static int dskread(void *, unsigned, unsigned);
123 static int printf(const char *,...);
124 static int putchar(int);
125 static void *memcpy(void *, const void *, size_t);
126 static void *malloc(size_t);
127 static uint32_t memsize(int);
128 static int drvread(void *, unsigned, unsigned);
129 static int keyhit(unsigned);
130 static int xputc(int);
131 static int xgetc(int);
132 static int getc(int);
135 readfile(const char *fname, void *buf, size_t size)
139 if ((ino = lookup(fname)))
140 fsread(ino, buf, size);
144 strcmp(const char *s1, const char *s2)
146 for (; *s1 == *s2 && *s1; s1++, s2++);
147 return (u_char)*s1 - (u_char)*s2;
151 fsfind(const char *name, ino_t * ino)
159 while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
160 for (s = buf; s < buf + DEV_BSIZE;) {
163 printf("%s ", d->d_name);
164 else if (!strcmp(name, d->d_name)) {
187 getstr(char *str, int size)
194 switch (c = getchar()) {
210 if (s - str < size - 1)
218 static inline uint32_t
223 v86.edx = DRV_HARD + drive;
227 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
228 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
235 v86.eax = 0xe00 | (c & 0xff);
246 dsk.drive = *(uint8_t *)PTOV(ARGS);
247 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
248 dsk.unit = dsk.drive & DRV_MASK;
249 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
250 bootinfo.bi_version = BOOTINFO_VERSION;
251 bootinfo.bi_size = sizeof(bootinfo);
252 bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */
253 bootinfo.bi_extmem = memsize(MEM_EXT);
254 bootinfo.bi_memsizes_valid++;
255 for (i = 0; i < N_BIOS_GEOM; i++)
256 bootinfo.bi_bios_geom[i] = drvinfo(i);
258 readfile(PATH_CONFIG, cmd, sizeof(cmd));
260 printf("%s: %s", PATH_CONFIG, cmd);
265 if (autoboot && !*kname) {
267 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
274 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
277 printf(" \n>> FreeBSD/i386 BOOT\n"
278 "Default: %u:%s(%u,%c)%s\n"
280 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
281 'a' + dsk.part, kname);
284 if (!autoboot || keyhit(0x5a))
285 getstr(cmd, sizeof(cmd));
296 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
303 load(const char *fname)
316 if (!(ino = lookup(fname))) {
318 printf("No %s\n", fname);
321 if (xfsread(ino, &hdr, sizeof(hdr)))
323 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
325 else if (IS_ELF(hdr.eh))
328 printf("Invalid %s\n", "format");
332 addr = hdr.ex.a_entry & 0xffffff;
335 if (xfsread(ino, p, hdr.ex.a_text))
337 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
338 if (xfsread(ino, p, hdr.ex.a_data))
340 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
341 bootinfo.bi_symtab = VTOP(p);
342 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
343 p += sizeof(hdr.ex.a_syms);
345 if (xfsread(ino, p, hdr.ex.a_syms))
348 if (xfsread(ino, p, sizeof(int)))
353 if (xfsread(ino, p, x))
358 fs_off = hdr.eh.e_phoff;
359 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
360 if (xfsread(ino, ep + j, sizeof(ep[0])))
362 if (ep[j].p_type == PT_LOAD)
365 for (i = 0; i < 2; i++) {
366 p = PTOV(ep[i].p_paddr & 0xffffff);
367 fs_off = ep[i].p_offset;
368 if (xfsread(ino, p, ep[i].p_filesz))
371 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
372 bootinfo.bi_symtab = VTOP(p);
373 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
374 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
375 (hdr.eh.e_shstrndx + 1);
376 if (xfsread(ino, &es, sizeof(es)))
378 for (i = 0; i < 2; i++) {
379 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
380 p += sizeof(es[i].sh_size);
381 fs_off = es[i].sh_offset;
382 if (xfsread(ino, p, es[i].sh_size))
387 addr = hdr.eh.e_entry & 0xffffff;
389 bootinfo.bi_esymtab = VTOP(p);
390 bootinfo.bi_kernelname = VTOP(fname);
391 bootinfo.bi_bios_dev = dsk.drive;
392 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
393 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
394 0, 0, 0, VTOP(&bootinfo));
403 while ((c = *arg++)) {
404 if (c == ' ' || c == '\t' || c == '\n')
406 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
410 while ((c = *arg++)) {
411 for (i = 0; c != optstr[i]; i++)
414 opts ^= 1 << flags[i];
416 if (opts & 1 << RBX_PROBEKBD) {
417 i = *(uint8_t *)PTOV(0x496) & 0x10;
418 printf("Keyboard: %s\n", i ? "yes" : "no");
420 opts |= 1 << RBX_DUAL | 1 << RBX_SERIAL;
421 opts &= ~(1 << RBX_PROBEKBD);
423 ioctrl = opts & 1 << RBX_DUAL ? 0x3 :
424 opts & 1 << RBX_SERIAL ? 0x2 : 0x1;
428 for (q = arg--; *q && *q != '('; q++);
432 if (*arg < '0' || *arg > '9')
439 for (i = 0; arg[0] != dev_nm[i][0] ||
440 arg[1] != dev_nm[i][1]; i++)
445 if (arg[1] != ',' || *arg < '0' || *arg > '9')
447 dsk.unit = *arg - '0';
449 dsk.slice = WHOLE_DISK_SLICE;
451 if (*arg < '0' || *arg > '0' + NDOSPART)
453 if ((dsk.slice = *arg - '0'))
457 if (arg[1] != ')' || *arg < 'a' || *arg > 'p')
459 dsk.part = *arg - 'a';
463 dsk.drive = (dsk.type == TYPE_WD ||
464 dsk.type == TYPE_AD ||
465 dsk.type == TYPE_DA ? DRV_HARD : 0) + drv;
469 if ((i = p - arg - !*(p - 1))) {
470 if (i >= sizeof(kname))
472 memcpy(kname, arg, i + 1);
481 lookup(const char *path)
483 char name[MAXNAMLEN + 1];
496 for (s = path; *s && *s != '/'; s++);
497 if ((n = s - path) > MAXNAMLEN)
499 ls = *path == '?' && n == 1 && !*s;
500 memcpy(name, path, n);
502 if ((dt = fsfind(name, &ino)) <= 0)
506 return dt == DT_REG ? ino : 0;
509 xfsread(ino_t inode, void *buf, size_t nbyte)
511 if (fsread(inode, buf, nbyte) != nbyte) {
512 printf("Invalid %s\n", "format");
519 fsread(ino_t inode, void *buf, size_t nbyte)
522 static struct dinode din;
524 static ufs_daddr_t *indbuf;
526 static ufs_daddr_t blkmap, indmap;
527 static unsigned fsblks;
529 ufs_daddr_t lbn, addr;
534 blkbuf = malloc(BSIZEMAX);
536 if (dskread(blkbuf, SBOFF / DEV_BSIZE, SBSIZE / DEV_BSIZE))
538 memcpy(&fs, blkbuf, sizeof(fs));
539 if (fs.fs_magic != FS_MAGIC) {
543 fsblks = fs.fs_bsize >> DEV_BSHIFT;
548 if (inomap != inode) {
549 if (dskread(blkbuf, fsbtodb(&fs, ino_to_fsba(&fs, inode)),
552 din = ((struct dinode *)blkbuf)[inode % INOPB(&fs)];
558 if (nbyte > (n = din.di_size - fs_off))
562 lbn = lblkno(&fs, fs_off);
564 addr = din.di_db[lbn];
566 if (indmap != din.di_ib[0]) {
568 indbuf = malloc(BSIZEMAX);
569 if (dskread(indbuf, fsbtodb(&fs, din.di_ib[0]),
572 indmap = din.di_ib[0];
574 addr = indbuf[(lbn - NDADDR) % NINDIR(&fs)];
576 n = dblksize(&fs, &din, lbn);
577 if (blkmap != addr) {
578 if (dskread(blkbuf, fsbtodb(&fs, addr), n >> DEV_BSHIFT))
582 off = blkoff(&fs, fs_off);
586 memcpy(s, blkbuf + off, n);
595 dskread(void *buf, unsigned lba, unsigned nblk)
598 struct dos_partition *dp;
604 sec = malloc(DEV_BSIZE);
606 if (drvread(sec, DOSBBSECTOR, 1))
608 dp = (void *)(sec + DOSPARTOFF);
610 if (sl < BASE_SLICE) {
611 for (i = 0; i < NDOSPART; i++)
612 if (dp[i].dp_typ == DOSPTYP_386BSD &&
613 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
615 if (dp[i].dp_flag & 0x80 ||
616 dsk.slice == COMPATIBILITY_SLICE)
619 if (dsk.slice == WHOLE_DISK_SLICE)
622 if (sl != WHOLE_DISK_SLICE) {
623 if (sl != COMPATIBILITY_SLICE)
624 dp += sl - BASE_SLICE;
625 if (dp->dp_typ != DOSPTYP_386BSD) {
626 printf("Invalid %s\n", "slice");
629 dsk.start = dp->dp_start;
631 if (drvread(sec, dsk.start + LABELSECTOR, 1))
633 d = (void *)(sec + LABELOFFSET);
634 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
635 if (dsk.part != RAW_PART) {
636 printf("Invalid %s\n", "label");
641 if (d->d_type == DTYPE_SCSI)
645 if (dsk.part >= d->d_npartitions ||
646 !d->d_partitions[dsk.part].p_size) {
647 printf("Invalid %s\n", "partition");
650 dsk.start = d->d_partitions[dsk.part].p_offset;
653 return drvread(buf, dsk.start + lba, nblk);
657 printf(const char *fmt,...)
659 static const char digits[16] = "0123456789abcdef";
667 while ((c = *fmt++)) {
672 putchar(va_arg(ap, int));
675 for (s = va_arg(ap, char *); *s; s++)
680 r = c == 'u' ? 10U : 16U;
681 u = va_arg(ap, unsigned);
684 *s++ = digits[u % r];
706 memcpy(void *dst, const void *src, size_t size)
711 for (d = dst, s = src; size; size--)
719 static uint32_t next;
723 next = roundup2(__base + _end, 0x10000) - __base;
739 drvread(void *buf, unsigned lba, unsigned nblk)
741 static unsigned c = 0x2d5c7c2f;
743 printf("%c\b", c = c << 8 | c >> 24);
744 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
745 v86.addr = XREADORG; /* call to xread in boot1 */
746 v86.es = VTOPSEG(buf);
748 v86.ebx = VTOPOFF(buf);
750 v86.edx = nblk << 8 | dsk.drive;
753 if (V86_CY(v86.efl)) {
754 printf("Disk error 0x%x (lba=0x%x)\n", v86.eax >> 8 & 0xff,
762 keyhit(unsigned ticks)
766 if (opts & 1 << RBX_NOINTR)
772 t1 = *(uint32_t *)PTOV(0x46c);
775 if (t1 < t0 || t1 >= t0 + ticks)
793 if (opts & 1 << RBX_NOINTR)
796 if (ioctrl & 0x1 && getc(1))
797 return fn ? 1 : getc(0);
798 if (ioctrl & 0x2 && sio_ischar())
799 return fn ? 1 : sio_getc();
811 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);