xref: /openbsd/sys/arch/alpha/stand/boot/boot.c (revision 54451086)
1 /*	$OpenBSD: boot.c,v 1.23 2014/02/19 22:02:14 miod Exp $	*/
2 /*	$NetBSD: boot.c,v 1.10 1997/01/18 01:58:33 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Ralph Campbell.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
36  */
37 
38 #include <lib/libkern/libkern.h>
39 #include <lib/libsa/stand.h>
40 #include <lib/libsa/loadfile.h>
41 
42 #include <sys/param.h>
43 #include <sys/exec.h>
44 #include <sys/stat.h>
45 #define _KERNEL
46 #include <sys/fcntl.h>
47 #undef _KERNEL
48 
49 #include <machine/rpb.h>
50 #include <machine/prom.h>
51 #include <machine/autoconf.h>
52 
53 char boot_file[128];
54 char boot_flags[128];
55 
56 extern char bootprog_name[];
57 
58 struct bootinfo_v1 bootinfo_v1;
59 
60 paddr_t ptbr_save;
61 
62 int debug;
63 
64 char   rnddata[BOOTRANDOM_MAX];
65 
66 void
67 loadrandom(char *name, char *buf, size_t buflen)
68 {
69 	struct stat sb;
70 	int fd, i;
71 
72 	fd = open(name, O_RDONLY);
73 	if (fd == -1) {
74 		if (errno != EPERM)
75 			printf("cannot open %s: %s\n", name, strerror(errno));
76 		return;
77 	}
78 	if (fstat(fd, &sb) == -1 || sb.st_uid != 0 || !S_ISREG(sb.st_mode) ||
79 	    (sb.st_mode & (S_IWOTH|S_IROTH)))
80 		goto fail;
81 	(void) read(fd, buf, buflen);
82 fail:
83 	close(fd);
84 }
85 
86 int
87 main()
88 {
89 	char *name, **namep;
90 	u_int64_t entry;
91 	int rc;
92 	u_long marks[MARK_MAX];
93 #ifdef DEBUG
94 	struct rpb *r;
95 	struct mddt *mddtp;
96 	struct mddt_cluster *memc;
97 	int i;
98 #endif
99 
100 	/* Init prom callback vector. */
101 	init_prom_calls();
102 
103 	/* print a banner */
104 	printf("%s\n", bootprog_name);
105 
106 	/* switch to OSF pal code. */
107 	OSFpal();
108 
109 #ifdef DEBUG
110 	r = (struct rpb *)HWRPB_ADDR;
111 	mddtp = (struct mddt *)(HWRPB_ADDR + r->rpb_memdat_off);
112 	printf("%d memory clusters\n", mddtp->mddt_cluster_cnt);
113 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
114 		memc = &mddtp->mddt_clusters[i];
115 		printf("%d: (%d) %lx-%lx\n", i, memc->mddt_usage,
116 		    memc->mddt_pfn << PAGE_SHIFT,
117 		    (memc->mddt_pfn + memc->mddt_pg_cnt) << PAGE_SHIFT);
118 	}
119 #endif
120 
121 	loadrandom(BOOTRANDOM, rnddata, sizeof(rnddata));
122 
123 	prom_getenv(PROM_E_BOOTED_FILE, boot_file, sizeof(boot_file));
124 	prom_getenv(PROM_E_BOOTED_OSFLAGS, boot_flags, sizeof(boot_flags));
125 
126 	if (boot_file[0] != '\0') {
127 		(void)printf("Boot file: %s %s\n", boot_file, boot_flags);
128 		name = boot_file;
129 	} else
130 		name = "bsd";
131 
132 	(void)printf("Loading %s...\n", name);
133 	marks[MARK_START] = 0;
134 	rc = loadfile(name, marks, LOAD_KERNEL | COUNT_KERNEL);
135 	(void)printf("\n");
136 	if (rc != 0)
137 		goto fail;
138 
139 	/*
140 	 * Fill in the bootinfo for the kernel.
141 	 */
142 	bzero(&bootinfo_v1, sizeof(bootinfo_v1));
143 	bootinfo_v1.ssym = marks[MARK_SYM];
144 	bootinfo_v1.esym = marks[MARK_END];
145 	bcopy(name, bootinfo_v1.booted_kernel,
146 	    sizeof(bootinfo_v1.booted_kernel));
147 	bcopy(boot_flags, bootinfo_v1.boot_flags,
148 	    sizeof(bootinfo_v1.boot_flags));
149 	bootinfo_v1.hwrpb = (void *)HWRPB_ADDR;
150 	bootinfo_v1.hwrpbsize = ((struct rpb *)HWRPB_ADDR)->rpb_size;
151 	bootinfo_v1.cngetc = NULL;
152 	bootinfo_v1.cnputc = NULL;
153 	bootinfo_v1.cnpollc = NULL;
154 
155 	entry = marks[MARK_START];
156 	(*(void (*)(u_int64_t, u_int64_t, u_int64_t, void *, u_int64_t,
157 	    u_int64_t))entry)(0, ptbr_save, BOOTINFO_MAGIC, &bootinfo_v1, 1, 0);
158 
159 fail:
160 	halt();
161 }
162