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
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>
27 #include <ufs/ffs/fs.h>
28 #include <ufs/ufs/dinode.h>
39 #define RBX_ASKNAME 0x0 /* -a */
40 #define RBX_SINGLE 0x1 /* -s */
41 #define RBX_DFLTROOT 0x5 /* -r */
42 #define RBX_KDB 0x6 /* -d */
43 #define RBX_CONFIG 0xa /* -c */
44 #define RBX_VERBOSE 0xb /* -v */
45 #define RBX_SERIAL 0xc /* -h */
46 #define RBX_CDROM 0xd /* -C */
47 #define RBX_GDB 0xf /* -g */
48 #define RBX_DUAL 0x1d /* -D */
49 #define RBX_PROBEKBD 0x1e /* -P */
51 #define RBX_MASK 0xffff
53 #define PATH_CONFIG "/boot.config"
54 #define PATH_BOOT3 "/boot/loader"
55 #define PATH_KERNEL "/kernel"
63 #define V86_CY(x) ((x) & 1)
64 #define V86_ZR(x) ((x) & 0x40)
77 static const char optstr[NOPT] = "DhaCcdgPrsv";
78 static const unsigned char flags[NOPT] = {
92 static const char *const dev_nm[] = {"ad", "wd", " ", "fd", "da"};
93 static const unsigned dev_maj[] = {30, 0, 1, 2, 4};
105 static char cmd[512];
106 static char kname[1024];
107 static uint32_t opts;
108 static struct bootinfo bootinfo;
110 static uint32_t fs_off;
111 static uint8_t ioctrl = 0x1;
114 static void load(const char *);
115 static int parse(char *);
116 static void readfile(const char *, void *, size_t);
117 static ino_t lookup(const char *);
118 static int fsfind(const char *, ino_t *);
119 static int xfsread(ino_t, void *, size_t);
120 static ssize_t fsread(ino_t, void *, size_t);
121 static int dskread(void *, unsigned, unsigned);
122 static int printf(const char *,...);
123 static void getstr(char *, int);
124 static int putchar(int);
125 static int getchar(void);
126 static void *memcpy(void *, const void *, size_t);
127 static int strcmp(const char *, const char *);
128 static void *malloc(size_t);
129 static uint32_t memsize(int);
130 static uint32_t drvinfo(int);
131 static int drvread(void *, unsigned, unsigned);
132 static int keyhit(unsigned);
133 static int xputc(int);
134 static int xgetc(int);
135 static void putc(int);
136 static int getc(int);
144 dsk.drive = *(uint8_t *)PTOV(ARGS);
145 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
146 dsk.unit = dsk.drive & DRV_MASK;
147 dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
148 bootinfo.bi_version = BOOTINFO_VERSION;
149 bootinfo.bi_size = sizeof(bootinfo);
150 bootinfo.bi_basemem = memsize(MEM_BASE);
151 bootinfo.bi_extmem = memsize(MEM_EXT);
152 bootinfo.bi_memsizes_valid++;
153 for (i = 0; i < N_BIOS_GEOM; i++)
154 bootinfo.bi_bios_geom[i] = drvinfo(i);
156 readfile(PATH_CONFIG, cmd, sizeof(cmd));
158 printf("%s: %s", PATH_CONFIG, cmd);
163 if (autoboot && !*kname) {
165 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
172 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
175 printf(" \n>> FreeBSD/i386 BOOT\n"
176 "Default: %u:%s(%u,%c)%s\n"
178 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
179 'a' + dsk.part, kname);
182 if (!autoboot || keyhit(0x5a))
183 getstr(cmd, sizeof(cmd));
200 load(const char *fname)
213 if (!(ino = lookup(fname))) {
215 printf("No %s\n", fname);
218 if (xfsread(ino, &hdr, sizeof(hdr)))
220 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
222 else if (IS_ELF(hdr.eh))
225 printf("Invalid %s\n", "format");
229 addr = hdr.ex.a_entry & 0xffffff;
232 if (xfsread(ino, p, hdr.ex.a_text))
234 p += roundup2(hdr.ex.a_text, PAGE_SIZE);
235 if (xfsread(ino, p, hdr.ex.a_data))
237 p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
238 bootinfo.bi_symtab = VTOP(p);
239 memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
240 p += sizeof(hdr.ex.a_syms);
242 if (xfsread(ino, p, hdr.ex.a_syms))
245 if (xfsread(ino, p, sizeof(int)))
250 if (xfsread(ino, p, x))
255 fs_off = hdr.eh.e_phoff;
256 for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
257 if (xfsread(ino, ep + j, sizeof(ep[0])))
259 if (ep[j].p_type == PT_LOAD)
262 for (i = 0; i < 2; i++) {
263 p = PTOV(ep[i].p_paddr & 0xffffff);
264 fs_off = ep[i].p_offset;
265 if (xfsread(ino, p, ep[i].p_filesz))
268 p += roundup2(ep[1].p_memsz, PAGE_SIZE);
269 bootinfo.bi_symtab = VTOP(p);
270 if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
271 fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
272 (hdr.eh.e_shstrndx + 1);
273 if (xfsread(ino, &es, sizeof(es)))
275 for (i = 0; i < 2; i++) {
276 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
277 p += sizeof(es[i].sh_size);
278 fs_off = es[i].sh_offset;
279 if (xfsread(ino, p, es[i].sh_size))
284 addr = hdr.eh.e_entry & 0xffffff;
286 bootinfo.bi_esymtab = VTOP(p);
287 bootinfo.bi_kernelname = VTOP(fname);
288 bootinfo.bi_bios_dev = dsk.drive;
289 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
290 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
291 0, 0, 0, VTOP(&bootinfo));
300 while ((c = *arg++)) {
303 for (p = arg; *p && *p != '\n' && *p != ' '; p++);
307 while ((c = *arg++)) {
308 for (i = 0; c != optstr[i]; i++)
311 opts ^= 1 << flags[i];
313 if (opts & 1 << RBX_PROBEKBD) {
314 i = *(uint8_t *)PTOV(0x496) & 0x10;
315 printf("Keyboard: %s\n", i ? "yes" : "no");
317 opts |= 1 << RBX_DUAL | 1 << RBX_SERIAL;
318 opts &= ~(1 << RBX_PROBEKBD);
320 ioctrl = opts & 1 << RBX_DUAL ? 0x3 :
321 opts & 1 << RBX_SERIAL ? 0x2 : 0x1;
325 for (q = arg--; *q && *q != '('; q++);
329 if (*arg < '0' || *arg > '9')
336 for (i = 0; arg[0] != dev_nm[i][0] ||
337 arg[1] != dev_nm[i][1]; i++)
342 if (arg[1] != ',' || *arg < '0' || *arg > '9')
344 dsk.unit = *arg - '0';
346 dsk.slice = WHOLE_DISK_SLICE;
348 if (*arg < '0' || *arg > '0' + NDOSPART)
350 if ((dsk.slice = *arg - '0'))
354 if (arg[1] != ')' || *arg < 'a' || *arg > 'p')
356 dsk.part = *arg - 'a';
360 dsk.drive = (dsk.type == TYPE_WD ||
361 dsk.type == TYPE_AD ||
362 dsk.type == TYPE_DA ? DRV_HARD : 0) + drv;
366 if ((i = p - arg - !*(p - 1))) {
367 if (i >= sizeof(kname))
369 memcpy(kname, arg, i + 1);
378 readfile(const char *fname, void *buf, size_t size)
382 if ((ino = lookup(fname)))
383 fsread(ino, buf, size);
387 lookup(const char *path)
389 char name[MAXNAMLEN + 1];
402 for (s = path; *s && *s != '/'; s++);
403 if ((n = s - path) > MAXNAMLEN)
405 ls = *path == '?' && n == 1 && !*s;
406 memcpy(name, path, n);
408 if ((dt = fsfind(name, &ino)) <= 0)
412 return dt == DT_REG ? ino : 0;
416 fsfind(const char *name, ino_t * ino)
424 while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
425 for (s = buf; s < buf + DEV_BSIZE;) {
428 printf("%s ", d->d_name);
429 else if (!strcmp(name, d->d_name)) {
441 xfsread(ino_t inode, void *buf, size_t nbyte)
443 if (fsread(inode, buf, nbyte) != nbyte) {
444 printf("Invalid %s\n", "format");
451 fsread(ino_t inode, void *buf, size_t nbyte)
454 static struct dinode din;
456 static ufs_daddr_t *indbuf;
458 static ufs_daddr_t blkmap, indmap;
459 static unsigned fsblks;
461 ufs_daddr_t lbn, addr;
466 blkbuf = malloc(BSIZEMAX);
468 if (dskread(blkbuf, SBOFF / DEV_BSIZE, SBSIZE / DEV_BSIZE))
470 memcpy(&fs, blkbuf, sizeof(fs));
471 if (fs.fs_magic != FS_MAGIC) {
475 fsblks = fs.fs_bsize >> DEV_BSHIFT;
480 if (inomap != inode) {
481 if (dskread(blkbuf, fsbtodb(&fs, ino_to_fsba(&fs, inode)),
484 din = ((struct dinode *)blkbuf)[inode % INOPB(&fs)];
490 if (nbyte > (n = din.di_size - fs_off))
494 lbn = lblkno(&fs, fs_off);
496 addr = din.di_db[lbn];
498 if (indmap != din.di_ib[0]) {
500 indbuf = malloc(BSIZEMAX);
501 if (dskread(indbuf, fsbtodb(&fs, din.di_ib[0]),
504 indmap = din.di_ib[0];
506 addr = indbuf[(lbn - NDADDR) % NINDIR(&fs)];
508 n = dblksize(&fs, &din, lbn);
509 if (blkmap != addr) {
510 if (dskread(blkbuf, fsbtodb(&fs, addr), n >> DEV_BSHIFT))
514 off = blkoff(&fs, fs_off);
518 memcpy(s, blkbuf + off, n);
527 dskread(void *buf, unsigned lba, unsigned nblk)
530 struct dos_partition *dp;
536 sec = malloc(DEV_BSIZE);
538 if (drvread(sec, DOSBBSECTOR, 1))
540 dp = (void *)(sec + DOSPARTOFF);
542 if (sl < BASE_SLICE) {
543 for (i = 0; i < NDOSPART; i++)
544 if (dp[i].dp_typ == DOSPTYP_386BSD &&
545 (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
547 if (dp[i].dp_flag & 0x80 ||
548 dsk.slice == COMPATIBILITY_SLICE)
551 if (dsk.slice == WHOLE_DISK_SLICE)
554 if (sl != WHOLE_DISK_SLICE) {
555 if (sl != COMPATIBILITY_SLICE)
556 dp += sl - BASE_SLICE;
557 if (dp->dp_typ != DOSPTYP_386BSD) {
558 printf("Invalid %s\n", "slice");
561 dsk.start = dp->dp_start;
563 if (drvread(sec, dsk.start + LABELSECTOR, 1))
565 d = (void *)(sec + LABELOFFSET);
566 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
567 if (dsk.part != RAW_PART) {
568 printf("Invalid %s\n", "label");
573 if (d->d_type == DTYPE_SCSI)
577 if (dsk.part >= d->d_npartitions ||
578 !d->d_partitions[dsk.part].p_size) {
579 printf("Invalid %s\n", "partition");
582 dsk.start = d->d_partitions[dsk.part].p_offset;
585 return drvread(buf, dsk.start + lba, nblk);
589 printf(const char *fmt,...)
591 static const char digits[16] = "0123456789abcdef";
599 while ((c = *fmt++)) {
604 putchar(va_arg(ap, int));
607 for (s = va_arg(ap, char *); *s; s++)
612 r = c == 'u' ? 10U : 16U;
613 u = va_arg(ap, unsigned);
616 *s++ = digits[u % r];
630 getstr(char *str, int size)
637 switch (c = getchar()) {
653 if (s - str < size - 1)
681 memcpy(void *dst, const void *src, size_t size)
686 for (d = dst, s = src; size; size--)
692 strcmp(const char *s1, const char *s2)
694 for (; *s1 == *s2 && *s1; s1++, s2++);
695 return (u_char)*s1 - (u_char)*s2;
701 static uint32_t next;
705 next = roundup2(__base + _end, 0x10000) - __base;
725 v86.edx = DRV_HARD + drive;
729 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
730 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
734 drvread(void *buf, unsigned lba, unsigned nblk)
736 static unsigned c = 0x2d5c7c2f;
738 printf("%c\b", c = c << 8 | c >> 24);
739 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
740 v86.addr = 0x704; /* call to xread in boot1 */
741 v86.es = VTOPSEG(buf);
743 v86.ebx = VTOPOFF(buf);
745 v86.edx = nblk << 8 | dsk.drive;
748 if (V86_CY(v86.efl)) {
749 printf("Disk error 0x%x (lba=0x%x)\n", v86.eax >> 8 & 0xff,
757 keyhit(unsigned ticks)
765 t1 = *(uint32_t *)PTOV(0x46c);
768 if (t1 < t0 || t1 >= t0 + ticks)
787 if (ioctrl & 0x1 && getc(1))
788 return fn ? 1 : getc(0);
789 if (ioctrl & 0x2 && sio_ischar())
790 return fn ? 1 : sio_getc();
800 v86.eax = 0xe00 | (c & 0xff);
811 return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);