gptboot.c revision 107874
1/*
2 * Copyright (c) 1998 Robert Nordier
3 * All rights reserved.
4 *
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
8 * such forms.
9 *
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
13 * purpose.
14 */
15
16/*
17 * $FreeBSD: head/sys/boot/i386/gptboot/gptboot.c 107874 2002-12-14 19:09:37Z phk $
18 */
19
20#include <sys/param.h>
21#include <sys/reboot.h>
22#include <sys/diskslice.h>
23#include <sys/disklabel.h>
24#include <sys/diskmbr.h>
25#include <sys/dirent.h>
26#include <machine/bootinfo.h>
27#include <machine/elf.h>
28
29#include <stdarg.h>
30
31#include <a.out.h>
32
33#include <btxv86.h>
34
35#include "boot2.h"
36#include "lib.h"
37
38#define IO_KEYBOARD	1
39#define IO_SERIAL	2
40
41#define SECOND		18	/* Circa that many ticks in a second. */
42
43#define RBX_ASKNAME	0x0	/* -a */
44#define RBX_SINGLE	0x1	/* -s */
45#define RBX_DFLTROOT	0x5	/* -r */
46#define RBX_KDB 	0x6	/* -d */
47#define RBX_CONFIG	0xa	/* -c */
48#define RBX_VERBOSE	0xb	/* -v */
49#define RBX_SERIAL	0xc	/* -h */
50#define RBX_CDROM	0xd	/* -C */
51#define RBX_GDB 	0xf	/* -g */
52#define RBX_MUTE	0x10	/* -m */
53#define RBX_PAUSE	0x12	/* -p */
54#define RBX_DUAL	0x1d	/* -D */
55#define RBX_PROBEKBD	0x1e	/* -P */
56#define RBX_NOINTR	0x1f	/* -n */
57
58#define RBX_MASK	0x2005ffff
59
60#define PATH_CONFIG	"/boot.config"
61#define PATH_BOOT3	"/boot/loader"
62#define PATH_KERNEL	"/kernel"
63
64#define ARGS		0x900
65#define NOPT		14
66#define NDEV		5
67#define MEM_BASE	0x12
68#define MEM_EXT 	0x15
69#define V86_CY(x)	((x) & 1)
70#define V86_ZR(x)	((x) & 0x40)
71
72#define DRV_HARD	0x80
73#define DRV_MASK	0x7f
74
75#define TYPE_AD		0
76#define TYPE_DA		2
77#define TYPE_MAXHARD	TYPE_DA
78#define TYPE_FD		4
79
80extern uint32_t _end;
81
82static const char optstr[NOPT] = "DhaCcdgmnPprsv";
83static const unsigned char flags[NOPT] = {
84    RBX_DUAL,
85    RBX_SERIAL,
86    RBX_ASKNAME,
87    RBX_CDROM,
88    RBX_CONFIG,
89    RBX_KDB,
90    RBX_GDB,
91    RBX_MUTE,
92    RBX_NOINTR,
93    RBX_PROBEKBD,
94    RBX_PAUSE,
95    RBX_DFLTROOT,
96    RBX_SINGLE,
97    RBX_VERBOSE
98};
99
100static const char *const dev_nm[NDEV] = {"ad", "wd", "da", "  ", "fd"};
101static const unsigned char dev_maj[NDEV] = {30, 0, 4, 1, 2};
102
103static struct dsk {
104    unsigned drive;
105    unsigned type;
106    unsigned unit;
107    unsigned slice;
108    unsigned part;
109    unsigned start;
110    int init;
111} dsk;
112static char cmd[512];
113static char kname[1024];
114static uint32_t opts = RB_BOOTINFO;
115static struct bootinfo bootinfo;
116static uint8_t ioctrl = IO_KEYBOARD;
117
118void exit(int);
119static void load(const char *);
120static int parse(char *);
121static int xfsread(ino_t, void *, size_t);
122static int dskread(void *, unsigned, unsigned);
123static void printf(const char *,...);
124static void putchar(int);
125static uint32_t memsize(int);
126static int drvread(void *, unsigned, unsigned);
127static int keyhit(unsigned);
128static int xputc(int);
129static int xgetc(int);
130static int getc(int);
131
132#if 1
133#define memcpy __builtin_memcpy
134#else
135static void memcpy(char *, const char *, int);
136static void
137memcpy(char *dst, const char *src, int len)
138{
139    while (len--)
140	*dst++ = *src++;
141}
142#endif
143
144static inline int
145strcmp(const char *s1, const char *s2)
146{
147    for (; *s1 == *s2 && *s1; s1++, s2++);
148    return (u_char)*s1 - (u_char)*s2;
149}
150
151#include "ufsread.c"
152
153static int
154xfsread(ino_t inode, void *buf, size_t nbyte)
155{
156    if (fsread(inode, buf, nbyte) != nbyte) {
157	printf("Invalid %s\n", "format");
158	return -1;
159    }
160    return 0;
161}
162
163static inline void
164getstr(char *str, int size)
165{
166    char *s;
167    int c;
168
169    s = str;
170    for (;;) {
171	switch (c = xgetc(0)) {
172	case 0:
173	    break;
174	case '\177':
175	    c = '\b';
176	case '\b':
177	    if (s > str) {
178		s--;
179		putchar('\b');
180		putchar(' ');
181	    } else
182		c = 0;
183	    break;
184	case '\n':
185	case '\r':
186	    *s = 0;
187	    return;
188	default:
189	    if (s - str < size - 1)
190		*s++ = c;
191	}
192	if (c)
193	    putchar(c);
194    }
195}
196
197static inline void
198putc(int c)
199{
200    v86.addr = 0x10;
201    v86.eax = 0xe00 | (c & 0xff);
202    v86.ebx = 0x7;
203    v86int();
204}
205
206int
207main(void)
208{
209    int autoboot;
210    ino_t ino;
211
212    dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
213    v86.ctl = V86_FLAGS;
214    dsk.drive = *(uint8_t *)PTOV(ARGS);
215    dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
216    dsk.unit = dsk.drive & DRV_MASK;
217    dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
218    bootinfo.bi_version = BOOTINFO_VERSION;
219    bootinfo.bi_size = sizeof(bootinfo);
220    bootinfo.bi_basemem = 0;	/* XXX will be filled by loader or kernel */
221    bootinfo.bi_extmem = memsize(MEM_EXT);
222    bootinfo.bi_memsizes_valid++;
223
224    /* Process configuration file */
225
226    autoboot = 1;
227
228    if ((ino = lookup(PATH_CONFIG)))
229	fsread(ino, cmd, sizeof(cmd));
230
231    if (*cmd) {
232	printf("%s: %s", PATH_CONFIG, cmd);
233	if (parse(cmd))
234	    autoboot = 0;
235	/* Do not process this command twice */
236	*cmd = 0;
237    }
238
239    /*
240     * Try to exec stage 3 boot loader. If interrupted by a keypress,
241     * or in case of failure, try to load a kernel directly instead.
242     */
243
244    if (autoboot && !*kname) {
245	memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
246	if (!keyhit(3*SECOND)) {
247	    load(kname);
248	    memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
249	}
250    }
251
252    /* Present the user with the boot2 prompt. */
253
254    for (;;) {
255#ifdef UFS1_ONLY
256	printf(" \n>> FreeBSD/i386/UFS1 BOOT\n"
257#else
258	printf(" \n>> FreeBSD/i386/UFS[12] BOOT\n"
259#endif
260	       "Default: %u:%s(%u,%c)%s\n"
261	       "boot: ",
262	       dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
263	       'a' + dsk.part, kname);
264	if (ioctrl & IO_SERIAL)
265	    sio_flush();
266	if (!autoboot || keyhit(5*SECOND))
267	    getstr(cmd, sizeof(cmd));
268	else
269	    putchar('\n');
270	autoboot = 0;
271	if (parse(cmd))
272	    putchar('\a');
273	else
274	    load(kname);
275    }
276}
277
278/* XXX - Needed for btxld to link the boot2 binary; do not remove. */
279void
280exit(int x)
281{
282}
283
284static void
285load(const char *fname)
286{
287    union {
288	struct exec ex;
289	Elf32_Ehdr eh;
290    } hdr;
291    Elf32_Phdr ep[2];
292    Elf32_Shdr es[2];
293    caddr_t p;
294    ino_t ino;
295    uint32_t addr, x;
296    int fmt, i, j;
297
298    if (!(ino = lookup(fname))) {
299	if (!ls)
300	    printf("No %s\n", fname);
301	return;
302    }
303    if (xfsread(ino, &hdr, sizeof(hdr)))
304	return;
305    if (N_GETMAGIC(hdr.ex) == ZMAGIC)
306	fmt = 0;
307    else if (IS_ELF(hdr.eh))
308	fmt = 1;
309    else {
310	printf("Invalid %s\n", "format");
311	return;
312    }
313    if (fmt == 0) {
314	addr = hdr.ex.a_entry & 0xffffff;
315	p = PTOV(addr);
316	fs_off = PAGE_SIZE;
317	if (xfsread(ino, p, hdr.ex.a_text))
318	    return;
319	p += roundup2(hdr.ex.a_text, PAGE_SIZE);
320	if (xfsread(ino, p, hdr.ex.a_data))
321	    return;
322	p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
323	bootinfo.bi_symtab = VTOP(p);
324	memcpy(p, (char *)&hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
325	p += sizeof(hdr.ex.a_syms);
326	if (hdr.ex.a_syms) {
327	    if (xfsread(ino, p, hdr.ex.a_syms))
328		return;
329	    p += hdr.ex.a_syms;
330	    if (xfsread(ino, p, sizeof(int)))
331		return;
332	    x = *(uint32_t *)p;
333	    p += sizeof(int);
334	    x -= sizeof(int);
335	    if (xfsread(ino, p, x))
336		return;
337	    p += x;
338	}
339    } else {
340	fs_off = hdr.eh.e_phoff;
341	for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
342	    if (xfsread(ino, ep + j, sizeof(ep[0])))
343		return;
344	    if (ep[j].p_type == PT_LOAD)
345		j++;
346	}
347	for (i = 0; i < 2; i++) {
348	    p = PTOV(ep[i].p_paddr & 0xffffff);
349	    fs_off = ep[i].p_offset;
350	    if (xfsread(ino, p, ep[i].p_filesz))
351		return;
352	}
353	p += roundup2(ep[1].p_memsz, PAGE_SIZE);
354	bootinfo.bi_symtab = VTOP(p);
355	if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
356	    fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
357		(hdr.eh.e_shstrndx + 1);
358	    if (xfsread(ino, &es, sizeof(es)))
359		return;
360	    for (i = 0; i < 2; i++) {
361		memcpy(p, (char *)&es[i].sh_size, sizeof(es[i].sh_size));
362		p += sizeof(es[i].sh_size);
363		fs_off = es[i].sh_offset;
364		if (xfsread(ino, p, es[i].sh_size))
365		    return;
366		p += es[i].sh_size;
367	    }
368	}
369	addr = hdr.eh.e_entry & 0xffffff;
370    }
371    bootinfo.bi_esymtab = VTOP(p);
372    bootinfo.bi_kernelname = VTOP(fname);
373    bootinfo.bi_bios_dev = dsk.drive;
374    __exec((caddr_t)addr, opts & RBX_MASK,
375	   MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
376	   0, 0, 0, VTOP(&bootinfo));
377}
378
379static int
380parse(char *arg)
381{
382    char *p, *q;
383    int drv, c, i;
384
385    while ((c = *arg++)) {
386	if (c == ' ' || c == '\t' || c == '\n')
387	    continue;
388	for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
389	if (*p)
390	    *p++ = 0;
391	if (c == '-') {
392	    while ((c = *arg++)) {
393		for (i = 0; c != optstr[i]; i++)
394		    if (i == NOPT - 1)
395			return -1;
396		opts ^= 1 << flags[i];
397	    }
398	    if (opts & 1 << RBX_PROBEKBD) {
399		i = *(uint8_t *)PTOV(0x496) & 0x10;
400		/* printf("Keyboard: %s\n", i ? "yes" : "no"); */
401		if (!i)
402		    opts |= 1 << RBX_DUAL | 1 << RBX_SERIAL;
403		opts &= ~(1 << RBX_PROBEKBD);
404	    }
405	    ioctrl = opts & 1 << RBX_DUAL ? (IO_SERIAL|IO_KEYBOARD) :
406		     opts & 1 << RBX_SERIAL ? IO_SERIAL : IO_KEYBOARD;
407	    if (ioctrl & IO_SERIAL)
408	        sio_init();
409	} else {
410	    for (q = arg--; *q && *q != '('; q++);
411	    if (*q) {
412		drv = -1;
413		if (arg[1] == ':') {
414		    if (*arg < '0' || *arg > '9')
415			return -1;
416		    drv = *arg - '0';
417		    arg += 2;
418		}
419		if (q - arg != 2)
420		    return -1;
421		for (i = 0; arg[0] != dev_nm[i][0] ||
422			    arg[1] != dev_nm[i][1]; i++)
423		    if (i == NDEV - 1)
424			return -1;
425		dsk.type = i;
426		arg += 3;
427		if (arg[1] != ',' || *arg < '0' || *arg > '9')
428		    return -1;
429		dsk.unit = *arg - '0';
430		arg += 2;
431		dsk.slice = WHOLE_DISK_SLICE;
432		if (arg[1] == ',') {
433		    if (*arg < '0' || *arg > '0' + NDOSPART)
434			return -1;
435		    if ((dsk.slice = *arg - '0'))
436			dsk.slice++;
437		    arg += 2;
438		}
439		if (arg[1] != ')' || *arg < 'a' || *arg > 'p')
440		    return -1;
441		dsk.part = *arg - 'a';
442		arg += 2;
443		if (drv == -1)
444		    drv = dsk.unit;
445		dsk.drive = (dsk.type <= TYPE_MAXHARD
446			     ? DRV_HARD : 0) + drv;
447		dsk_meta = 0;
448		fsread(0, NULL, 0);
449	    }
450	    if ((i = p - arg - !*(p - 1))) {
451		if (i >= sizeof(kname))
452		    return -1;
453		memcpy(kname, arg, i + 1);
454	    }
455	}
456	arg = p;
457    }
458    return 0;
459}
460
461static int
462dskread(void *buf, unsigned lba, unsigned nblk)
463{
464    struct dos_partition *dp;
465    struct disklabel *d;
466    char *sec;
467    unsigned sl, i;
468
469    if (!dsk_meta) {
470	sec = dmadat->secbuf;
471	dsk.start = 0;
472	if (drvread(sec, DOSBBSECTOR, 1))
473	    return -1;
474	dp = (void *)(sec + DOSPARTOFF);
475	sl = dsk.slice;
476	if (sl < BASE_SLICE) {
477	    for (i = 0; i < NDOSPART; i++)
478		if (dp[i].dp_typ == DOSPTYP_386BSD &&
479		    (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
480		    sl = BASE_SLICE + i;
481		    if (dp[i].dp_flag & 0x80 ||
482			dsk.slice == COMPATIBILITY_SLICE)
483			break;
484		}
485	    if (dsk.slice == WHOLE_DISK_SLICE)
486		dsk.slice = sl;
487	}
488	if (sl != WHOLE_DISK_SLICE) {
489	    if (sl != COMPATIBILITY_SLICE)
490		dp += sl - BASE_SLICE;
491	    if (dp->dp_typ != DOSPTYP_386BSD) {
492		printf("Invalid %s\n", "slice");
493		return -1;
494	    }
495	    dsk.start = dp->dp_start;
496	}
497	if (drvread(sec, dsk.start + LABELSECTOR, 1))
498		return -1;
499	d = (void *)(sec + LABELOFFSET);
500	if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
501	    if (dsk.part != RAW_PART) {
502		printf("Invalid %s\n", "label");
503		return -1;
504	    }
505	} else {
506	    if (!dsk.init) {
507		if (d->d_type == DTYPE_SCSI)
508		    dsk.type = TYPE_DA;
509		dsk.init++;
510	    }
511	    if (dsk.part >= d->d_npartitions ||
512		!d->d_partitions[dsk.part].p_size) {
513		printf("Invalid %s\n", "partition");
514		return -1;
515	    }
516	    dsk.start += d->d_partitions[dsk.part].p_offset;
517	    dsk.start -= d->d_partitions[RAW_PART].p_offset;
518	}
519    }
520    return drvread(buf, dsk.start + lba, nblk);
521}
522
523static void
524printf(const char *fmt,...)
525{
526    static const char digits[16] = "0123456789abcdef";
527    va_list ap;
528    char buf[10];
529    char *s;
530    unsigned r, u;
531    int c;
532
533    va_start(ap, fmt);
534    while ((c = *fmt++)) {
535	if (c == '%') {
536	    c = *fmt++;
537	    switch (c) {
538	    case 'c':
539		putchar(va_arg(ap, int));
540		continue;
541	    case 's':
542		for (s = va_arg(ap, char *); *s; s++)
543		    putchar(*s);
544		continue;
545	    case 'u':
546	    case 'x':
547		r = c == 'u' ? 10U : 16U;
548		u = va_arg(ap, unsigned);
549		s = buf;
550		do
551		    *s++ = digits[u % r];
552		while (u /= r);
553		while (--s >= buf)
554		    putchar(*s);
555		continue;
556	    }
557	}
558	putchar(c);
559    }
560    va_end(ap);
561    return;
562}
563
564static void
565putchar(int c)
566{
567    if (c == '\n')
568	xputc('\r');
569    xputc(c);
570}
571
572static uint32_t
573memsize(int type)
574{
575    v86.addr = type;
576    v86.eax = 0x8800;
577    v86int();
578    return v86.eax;
579}
580
581static int
582drvread(void *buf, unsigned lba, unsigned nblk)
583{
584    static unsigned c = 0x2d5c7c2f;
585
586    printf("%c\b", c = c << 8 | c >> 24);
587    v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
588    v86.addr = XREADORG;		/* call to xread in boot1 */
589    v86.es = VTOPSEG(buf);
590    v86.eax = lba;
591    v86.ebx = VTOPOFF(buf);
592    v86.ecx = lba >> 16;
593    v86.edx = nblk << 8 | dsk.drive;
594    v86int();
595    v86.ctl = V86_FLAGS;
596    if (V86_CY(v86.efl)) {
597	printf("Disk error 0x%x lba 0x%x\n", v86.eax >> 8 & 0xff, lba);
598	return -1;
599    }
600    return 0;
601}
602
603static int
604keyhit(unsigned ticks)
605{
606    uint32_t t0, t1;
607
608    if (opts & 1 << RBX_NOINTR)
609	return 0;
610    t0 = 0;
611    for (;;) {
612	if (xgetc(1))
613	    return 1;
614	t1 = *(uint32_t *)PTOV(0x46c);
615	if (!t0)
616	    t0 = t1;
617	if (t1 < t0 || t1 >= t0 + ticks)
618	    return 0;
619    }
620}
621
622static int
623xputc(int c)
624{
625    if (ioctrl & IO_KEYBOARD)
626	putc(c);
627    if (ioctrl & IO_SERIAL)
628	sio_putc(c);
629    return c;
630}
631
632static int
633xgetc(int fn)
634{
635    if (opts & 1 << RBX_NOINTR)
636	return 0;
637    for (;;) {
638	if (ioctrl & IO_KEYBOARD && getc(1))
639	    return fn ? 1 : getc(0);
640	if (ioctrl & IO_SERIAL && sio_ischar())
641	    return fn ? 1 : sio_getc();
642	if (fn)
643	    return 0;
644    }
645}
646
647static int
648getc(int fn)
649{
650    v86.addr = 0x16;
651    v86.eax = fn << 8;
652    v86int();
653    return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
654}
655