boot1.c revision 90699
1/*
2 * Copyright (c) 1998 Robert Nordier
3 * All rights reserved.
4 * Copyright (c) 2001 Robert Drehmel
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms are freely
8 * permitted provided that the above copyright notice and this
9 * paragraph and the following disclaimer are duplicated in all
10 * such forms.
11 *
12 * This software is provided "AS IS" and without any express or
13 * implied warranties, including, without limitation, the implied
14 * warranties of merchantability and fitness for a particular
15 * purpose.
16 *
17 * $FreeBSD: head/sys/boot/sparc64/boot1/boot1.c 90699 2002-02-15 12:49:20Z robert $
18 */
19#include <sys/param.h>
20#include <sys/reboot.h>
21#include <sys/diskslice.h>
22#include <sys/disklabel.h>
23#include <sys/dirent.h>
24#include <machine/elf.h>
25#include <machine/stdarg.h>
26
27#include <ufs/ffs/fs.h>
28#include <ufs/ufs/dinode.h>
29
30#include <a.out.h>
31
32#define RBX_ASKNAME	0x0	/* -a */
33#define RBX_SINGLE	0x1	/* -s */
34#define RBX_DFLTROOT	0x5	/* -r */
35#define RBX_KDB 	0x6	/* -d */
36#define RBX_CONFIG	0xa	/* -c */
37#define RBX_VERBOSE	0xb	/* -v */
38#define RBX_CDROM	0xd	/* -C */
39#define RBX_GDB 	0xf	/* -g */
40
41#define RBX_MASK	0x2000ffff
42
43#define PATH_CONFIG	"/boot.config"
44#define PATH_LOADER	"/boot/loader"
45#define PATH_KERNEL	"/kernel"
46
47#define ARGS		0x900
48#define NOPT		11
49#define BSIZEMAX	8192
50#define NDEV		5
51
52#define TYPE_AD		0
53#define TYPE_WD		1
54#define TYPE_WFD 	2
55#define TYPE_FD		3
56#define TYPE_DA		4
57
58/*
59 * This structure will be refined along with the addition of a bootpath
60 * parsing routine when it is necessary to cope with bootpaths that are
61 * not in the exact <devpath>@<controller>,<disk>:<partition> format and
62 * for which we need to evaluate the disklabel ourselves.
63 */
64struct disk {
65	int meta;
66};
67struct disk dsk;
68
69extern uint32_t _end;
70
71static const char optstr[NOPT] = "aCcgrsv";
72static const unsigned char flags[NOPT] = {
73    RBX_ASKNAME,
74    RBX_CDROM,
75    RBX_CONFIG,
76    RBX_GDB,
77    RBX_DFLTROOT,
78    RBX_SINGLE,
79    RBX_VERBOSE
80};
81
82static char cmd[512];		/* command to parse */
83static char bname[1024];	/* name of the binary to load */
84static uint32_t opts;
85static int ls;
86static uint32_t fs_off;
87
88int main(void);
89void exit(int);
90static void load(const char *);
91static int parse(char *);
92static ino_t lookup(const char *);
93static int xfsread(ino_t, void *, size_t);
94static ssize_t fsread(ino_t, void *, size_t);
95static int dskread(void *, u_int64_t, int);
96static int printf(const char *, ...);
97static int putchar(int);
98static int keyhit(unsigned int);
99static int getc(void);
100
101static void *memcpy(void *, const void *, size_t);
102static void *memset(void *, int, size_t);
103static void *malloc(size_t);
104
105/*
106 * Open Firmware interface functions
107 */
108typedef u_int64_t	ofwcell_t;
109typedef int32_t		ofwh_t;
110typedef u_int32_t	u_ofwh_t;
111typedef int (*ofwfp_t)(ofwcell_t []);
112ofwfp_t ofw;			/* the prom Open Firmware entry */
113
114void ofw_init(int, int, int, int, ofwfp_t);
115ofwh_t ofw_finddevice(const char *);
116ofwh_t ofw_open(const char *);
117int ofw_getprop(ofwh_t, const char *, void *, size_t);
118int ofw_read(ofwh_t, void *, size_t);
119int ofw_write(ofwh_t, const void *, size_t);
120int ofw_seek(ofwh_t, u_int64_t);
121
122ofwh_t bootdevh;
123ofwh_t stdinh, stdouth;
124char bootpath[64];
125
126/*
127 * This has to stay here, as the PROM seems to ignore the
128 * entry point specified in the a.out header.  (or elftoaout is broken)
129 */
130
131void
132ofw_init(int d, int d1, int d2, int d3, ofwfp_t ofwaddr)
133{
134	ofwh_t chosenh;
135
136	ofw = ofwaddr;
137
138	chosenh = ofw_finddevice("/chosen");
139	ofw_getprop(chosenh, "stdin", &stdinh, sizeof(stdinh));
140	ofw_getprop(chosenh, "stdout", &stdouth, sizeof(stdouth));
141	ofw_getprop(chosenh, "bootpath", bootpath, sizeof(bootpath));
142
143	if ((bootdevh = ofw_open(bootpath)) == -1) {
144		printf("Could not open boot device.\n");
145	}
146
147	main();
148	d = d1 = d2 = d3;	/* make GCC happy */
149}
150
151ofwh_t
152ofw_finddevice(const char *name)
153{
154    ofwcell_t args[] = {
155	(ofwcell_t)"finddevice",
156	1,
157	1,
158	(ofwcell_t)name,
159	0
160    };
161    if ((*ofw)(args)) {
162	printf("ofw_finddevice: name=\"%s\"\n", name);
163	return 1;
164    }
165    return args[4];
166}
167
168int
169ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
170{
171    ofwcell_t args[] = {
172	(ofwcell_t)"getprop",
173	4,
174	1,
175	(u_ofwh_t)ofwh,
176	(ofwcell_t)name,
177	(ofwcell_t)buf,
178	len,
179	0
180    };
181    if ((*ofw)(args)) {
182	printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n", ofwh, buf, len);
183	return 1;
184    }
185    return 0;
186}
187
188ofwh_t
189ofw_open(const char *path)
190{
191    ofwcell_t args[] = {
192	(ofwcell_t)"open",
193	1,
194	1,
195	(ofwcell_t)path,
196	0
197    };
198    if ((*ofw)(args)) {
199	printf("ofw_open: path=\"%s\"\n", path);
200	return -1;
201    }
202    return args[4];
203}
204
205int
206ofw_close(ofwh_t devh)
207{
208    ofwcell_t args[] = {
209	(ofwcell_t)"close",
210	1,
211	0,
212	(u_ofwh_t)devh
213    };
214    if ((*ofw)(args)) {
215	printf("ofw_close: devh=0x%x\n", devh);
216	return 1;
217    }
218    return 0;
219}
220
221int
222ofw_read(ofwh_t devh, void *buf, size_t len)
223{
224    ofwcell_t args[] = {
225	(ofwcell_t)"read",
226	4,
227	1,
228	(u_ofwh_t)devh,
229	(ofwcell_t)buf,
230	len,
231	0
232    };
233    if ((*ofw)(args)) {
234	printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
235	return 1;
236    }
237    return 0;
238}
239
240int
241ofw_write(ofwh_t devh, const void *buf, size_t len)
242{
243    ofwcell_t args[] = {
244	(ofwcell_t)"write",
245	3,
246	1,
247	(u_ofwh_t)devh,
248	(ofwcell_t)buf,
249	len,
250	0
251    };
252    if ((*ofw)(args)) {
253	printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
254	return 1;
255    }
256    return 0;
257}
258
259int
260ofw_seek(ofwh_t devh, u_int64_t off)
261{
262    ofwcell_t args[] = {
263	(ofwcell_t)"seek",
264	4,
265	1,
266	(u_ofwh_t)devh,
267	off >> 32,
268	off & ((1UL << 32) - 1),
269	0
270    };
271    if ((*ofw)(args)) {
272	printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
273	return 1;
274    }
275    return 0;
276}
277
278static void
279readfile(const char *fname, void *buf, size_t size)
280{
281    ino_t ino;
282
283    if ((ino = lookup(fname)))
284	fsread(ino, buf, size);
285}
286
287static int
288strcmp(const char *s1, const char *s2)
289{
290    for (; *s1 == *s2 && *s1; s1++, s2++);
291    return (u_char)*s1 - (u_char)*s2;
292}
293
294static void *
295memset(void *dst, int val, size_t len)
296{
297	void * const ret = dst;
298	while (len) {
299		*((char *)dst)++ = val;
300		len--;
301	}
302	return ret;
303}
304
305static int
306fsfind(const char *name, ino_t * ino)
307{
308    char buf[DEV_BSIZE];
309    struct dirent *d;
310    char *s;
311    ssize_t n;
312
313    fs_off = 0;
314    while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
315	for (s = buf; s < buf + DEV_BSIZE;) {
316	    d = (void *)s;
317	    if (ls)
318		printf("%s ", d->d_name);
319	    else if (!strcmp(name, d->d_name)) {
320		*ino = d->d_fileno;
321		return d->d_type;
322	    }
323	    s += d->d_reclen;
324	}
325    if (n != -1 && ls)
326	putchar('\n');
327    return 0;
328}
329
330static int
331getchar(void)
332{
333    int c;
334
335    c = getc();
336    if (c == '\r')
337	c = '\n';
338    return c;
339}
340
341static void
342getstr(char *str, int size)
343{
344    char *s;
345    int c;
346
347    s = str;
348    do {
349	switch (c = getchar()) {
350	case 0:
351	    break;
352	case '\b':
353	case '\177':
354	    if (s > str) {
355		s--;
356		putchar('\b');
357		putchar(' ');
358	    } else
359		c = 0;
360	    break;
361	case '\n':
362	    *s = 0;
363	    break;
364	default:
365	    if (s - str < size - 1)
366		*s++ = c;
367	}
368	if (c)
369	    putchar(c);
370    } while (c != '\n');
371}
372
373static void
374putc(int c)
375{
376	char d = c;
377	ofw_write(stdouth, &d, 1);
378}
379
380int main(void)
381{
382    readfile(PATH_CONFIG, cmd, sizeof(cmd));
383    if (cmd[0] != '\0') {
384	printf("%s: %s", PATH_CONFIG, cmd);
385	if (parse(cmd))
386	    cmd[0] = '\0';
387    }
388    if (bname[0] == '\0')
389	memcpy(bname, PATH_LOADER, sizeof(PATH_LOADER));
390
391    printf(" \n>> FreeBSD/sparc64 boot block\n"
392	"   Boot path:   %s\n"
393	"   Boot loader: %s\n", bootpath, PATH_LOADER);
394    load(bname);
395    return 1;
396}
397
398static void
399load(const char *fname)
400{
401    Elf64_Ehdr eh;
402    Elf64_Phdr ep[2];
403    Elf64_Shdr es[2];
404    caddr_t p;
405    ino_t ino;
406    vm_offset_t entry;
407    int i, j;
408
409    if ((ino = lookup(fname)) == 0) {
410	if (!ls)
411	    printf("File %s not found\n", fname);
412	return;
413    }
414    if (xfsread(ino, &eh, sizeof(eh)))
415	return;
416    if (!IS_ELF(eh)) {
417	printf("Not an ELF file\n");
418	return;
419    }
420    fs_off = eh.e_phoff;
421    for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
422	if (xfsread(ino, ep + j, sizeof(ep[0])))
423	    return;
424	if (ep[j].p_type == PT_LOAD)
425	    j++;
426    }
427    for (i = 0; i < j; i++) {
428	p = (caddr_t)ep[i].p_vaddr;
429	fs_off = ep[i].p_offset;
430	if (xfsread(ino, p, ep[i].p_filesz))
431	    return;
432	/*
433	 * Assume the second program header table entry
434	 * to contain data and bss.  Clear out the .bss section.
435	 */
436	if (i == 1)
437	    memset(p + ep[i].p_filesz, 0, ep[i].p_memsz - ep[i].p_filesz);
438    }
439    p += roundup2(ep[1].p_memsz, PAGE_SIZE);
440    if (eh.e_shnum == eh.e_shstrndx + 3) {
441	fs_off = eh.e_shoff + sizeof(es[0]) * (eh.e_shstrndx + 1);
442	if (xfsread(ino, &es, sizeof(es)))
443	    return;
444	for (i = 0; i < 2; i++) {
445	    memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
446	    p += sizeof(es[i].sh_size);
447	    fs_off = es[i].sh_offset;
448	    if (xfsread(ino, p, es[i].sh_size))
449		return;
450	    p += es[i].sh_size;
451	}
452    }
453    entry = eh.e_entry;
454    ofw_close(bootdevh);
455    (*(void (*)(int, int, int, int, ofwfp_t))entry)(0, 0, 0, 0, ofw);
456}
457
458static int
459parse(char *arg)
460{
461    char *p;
462    int c, i;
463
464    while ((c = *arg++)) {
465	if (c == ' ' || c == '\t' || c == '\n')
466	    continue;
467	for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
468	if (*p)
469	    *p++ = 0;
470	if (c == '-') {
471	    while ((c = *arg++)) {
472		for (i = 0; c != optstr[i]; i++)
473		    if (i == NOPT - 1)
474			return -1;
475		opts ^= 1 << flags[i];
476	    }
477	}
478	arg = p;
479    }
480    return 0;
481}
482
483static ino_t
484lookup(const char *path)
485{
486    char name[MAXNAMLEN + 1];
487    const char *s;
488    ino_t ino;
489    ssize_t n;
490    int dt;
491
492    ino = ROOTINO;
493    dt = DT_DIR;
494    for (;;) {
495	if (*path == '/')
496	    path++;
497	if (!*path)
498	    break;
499	for (s = path; *s && *s != '/'; s++);
500	if ((n = s - path) > MAXNAMLEN)
501	    return 0;
502	ls = *path == '?' && n == 1 && !*s;
503	memcpy(name, path, n);
504	name[n] = 0;
505	if ((dt = fsfind(name, &ino)) <= 0)
506	    break;
507	path = s;
508    }
509    return dt == DT_REG ? ino : 0;
510}
511
512static int
513xfsread(ino_t inode, void *buf, size_t nbyte)
514{
515    if (fsread(inode, buf, nbyte) != (ssize_t)nbyte) {
516	printf("Invalid %s\n", "format");
517	return -1;
518    }
519    return 0;
520}
521
522static ssize_t
523fsread(ino_t inode, void *buf, size_t nbyte)
524{
525    static struct fs fs;
526    static struct dinode din;
527    static char *blkbuf;
528    static ufs_daddr_t *indbuf;
529    static ino_t inomap;
530    static ufs_daddr_t blkmap, indmap;
531    static unsigned int fsblks;
532    char *s;
533    ufs_daddr_t lbn, addr;
534    size_t n, nb, off;
535
536    if (!dsk.meta) {
537	if (!blkbuf)
538	    blkbuf = malloc(BSIZEMAX);
539	inomap = 0;
540	if (dskread(blkbuf, SBOFF / DEV_BSIZE, SBSIZE / DEV_BSIZE))
541	    return -1;
542	memcpy(&fs, blkbuf, sizeof(fs));
543	if (fs.fs_magic != FS_MAGIC) {
544	    printf("Not ufs\n");
545	    return -1;
546	}
547	fsblks = fs.fs_bsize >> DEV_BSHIFT;
548	dsk.meta++;
549    }
550    if (!inode)
551	return 0;
552    if (inomap != inode) {
553	if (dskread(blkbuf, fsbtodb(&fs, ino_to_fsba(&fs, inode)),
554		    fsblks))
555	    return -1;
556	din = ((struct dinode *)blkbuf)[inode % INOPB(&fs)];
557	inomap = inode;
558	fs_off = 0;
559	blkmap = indmap = 0;
560    }
561    s = buf;
562    if (nbyte > (n = din.di_size - fs_off))
563	nbyte = n;
564    nb = nbyte;
565    while (nb) {
566	lbn = lblkno(&fs, fs_off);
567	if (lbn < NDADDR)
568	    addr = din.di_db[lbn];
569	else {
570	    if (indmap != din.di_ib[0]) {
571		if (!indbuf)
572		    indbuf = malloc(BSIZEMAX);
573		if (dskread(indbuf, fsbtodb(&fs, din.di_ib[0]),
574			    fsblks))
575		    return -1;
576		indmap = din.di_ib[0];
577	    }
578	    addr = indbuf[(lbn - NDADDR) % NINDIR(&fs)];
579	}
580	n = dblksize(&fs, &din, lbn);
581	if (blkmap != addr) {
582	    if (dskread(blkbuf, fsbtodb(&fs, addr), n >> DEV_BSHIFT))
583		return -1;
584	    blkmap = addr;
585	}
586	off = blkoff(&fs, fs_off);
587	n -= off;
588	if (n > nb)
589	    n = nb;
590	memcpy(s, blkbuf + off, n);
591	s += n;
592	fs_off += n;
593	nb -= n;
594    }
595    return nbyte;
596}
597
598static int
599dskread(void *buf, u_int64_t lba, int nblk)
600{
601    /*
602     * The OpenFirmware should open the correct partition for us.
603     * That means, if we read from offset zero on an open instance handle,
604     * we should read from offset zero of that partition.
605     */
606    ofw_seek(bootdevh, lba * 512);
607    ofw_read(bootdevh, buf, nblk * DEV_BSIZE);
608    return 0;
609}
610
611static int
612printf(const char *fmt,...)
613{
614    static const char digits[16] = "0123456789abcdef";
615    va_list ap;
616    char buf[10];
617    char *s;
618    unsigned long int r, u;
619    int c;
620
621    va_start(ap, fmt);
622    while ((c = *fmt++)) {
623	if (c == '%') {
624	    c = *fmt++;
625	    switch (c) {
626	    case 'c':
627		putchar(va_arg(ap, int));
628		continue;
629	    case 's':
630		for (s = va_arg(ap, char *); *s; s++)
631		    putchar(*s);
632		continue;
633	    case 'p':
634		if (c == 'p') {
635		    putchar('0');
636		    putchar('x');
637		}
638	    case 'u':
639	    case 'x':
640		r = c == 'u' ? 10U : 16U;
641		u = c == 'p' ? va_arg(ap, unsigned long) :
642		    va_arg(ap, unsigned int);
643		s = buf;
644		do
645		    *s++ = digits[u % r];
646		while (u /= r);
647		while (--s >= buf)
648		    putchar(*s);
649		continue;
650	    }
651	}
652	putchar(c);
653    }
654    va_end(ap);
655    return 0;
656}
657
658static int
659putchar(int c)
660{
661    if (c == '\n')
662	putc('\r');
663    putc(c);
664    return c;
665}
666
667static void *
668memcpy(void *dst, const void *src, size_t size)
669{
670    const char *s;
671    char *d;
672
673    for (d = dst, s = src; size; size--)
674	*d++ = *s++;
675    return dst;
676}
677
678static void *
679malloc(size_t size)
680{
681    static vm_offset_t next = 0x10000;
682    void *p;
683
684    if (size & 0xf)
685	size = (size + 0xf) & ~0xf;
686    p = (void *)next;
687    next += size;
688    return p;
689}
690
691static int
692keyhit(unsigned int ticks)
693{
694	/* XXX */
695	return 0;
696	ticks = ticks;		/* make GCC happy */
697}
698
699static int
700getc(void)
701{
702	char c;
703	ofw_read(stdinh, &c, 1);
704	return c;
705}
706