1 /* $OpenBSD: boot.c,v 1.30 2023/01/16 07:29:34 deraadt 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 #include <lib/libsa/arc4.h>
42
43 #include <sys/param.h>
44 #include <sys/exec.h>
45 #include <sys/stat.h>
46 #include <sys/reboot.h>
47 #define _KERNEL
48 #include <sys/fcntl.h>
49 #undef _KERNEL
50
51 #include <machine/rpb.h>
52 #include <machine/prom.h>
53 #include <machine/autoconf.h>
54
55 char boot_file[128];
56 char boot_flags[128];
57
58 extern char bootprog_name[];
59
60 struct bootinfo_v1 bootinfo_v1;
61
62 extern paddr_t ptbr_save;
63
64 int debug;
65
66 char rnddata[BOOTRANDOM_MAX];
67 struct rc4_ctx randomctx;
68
69 int
loadrandom(char * name,char * buf,size_t buflen)70 loadrandom(char *name, char *buf, size_t buflen)
71 {
72 struct stat sb;
73 int fd, i, error = 0;
74
75 fd = open(name, O_RDONLY);
76 if (fd == -1) {
77 if (errno != EPERM)
78 printf("cannot open %s: %s\n", name, strerror(errno));
79 return -1;
80 }
81 if (fstat(fd, &sb) == -1) {
82 error = -1;
83 goto done;
84 }
85 if (read(fd, buf, buflen) != buflen) {
86 error = -1;
87 goto done;
88 }
89 if (sb.st_mode & S_ISTXT) {
90 printf("NOTE: random seed is being reused.\n");
91 error = -1;
92 goto done;
93 }
94 fchmod(fd, sb.st_mode | S_ISTXT);
95 done:
96 close(fd);
97 return (error);
98 }
99
100 void init_prom_calls(void);
101 void OSFpal(void);
102 void halt(void);
103
104 int
main()105 main()
106 {
107 char *name, **namep;
108 u_int64_t entry;
109 int rc, boothowto = 0;
110 uint64_t marks[MARK_MAX];
111 #ifdef DEBUG
112 struct rpb *r;
113 struct mddt *mddtp;
114 struct mddt_cluster *memc;
115 int i;
116 #endif
117
118 /* Init prom callback vector. */
119 init_prom_calls();
120
121 /* print a banner */
122 printf("%s\n", bootprog_name);
123
124 /* switch to OSF pal code. */
125 OSFpal();
126
127 #ifdef DEBUG
128 r = (struct rpb *)HWRPB_ADDR;
129 mddtp = (struct mddt *)(HWRPB_ADDR + r->rpb_memdat_off);
130 printf("%d memory clusters\n", mddtp->mddt_cluster_cnt);
131 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
132 memc = &mddtp->mddt_clusters[i];
133 printf("%d: (%d) %lx-%lx\n", i, memc->mddt_usage,
134 memc->mddt_pfn << PAGE_SHIFT,
135 (memc->mddt_pfn + memc->mddt_pg_cnt) << PAGE_SHIFT);
136 }
137 #endif
138
139 if (loadrandom(BOOTRANDOM, rnddata, sizeof(rnddata)) == 0)
140 boothowto |= RB_GOODRANDOM;
141 rc4_keysetup(&randomctx, rnddata, sizeof rnddata);
142 rc4_skip(&randomctx, 1536);
143
144 prom_getenv(PROM_E_BOOTED_FILE, boot_file, sizeof(boot_file));
145 prom_getenv(PROM_E_BOOTED_OSFLAGS, boot_flags, sizeof(boot_flags));
146
147 if (boot_file[0] != '\0') {
148 (void)printf("Boot file: %s %s\n", boot_file, boot_flags);
149 name = boot_file;
150 } else
151 name = "bsd";
152
153 (void)printf("Loading %s...\n", name);
154 marks[MARK_START] = 0;
155 rc = loadfile(name, marks, LOAD_KERNEL | COUNT_KERNEL);
156 (void)printf("\n");
157 if (rc != 0)
158 goto fail;
159
160 /*
161 * Fill in the bootinfo for the kernel.
162 */
163 bzero(&bootinfo_v1, sizeof(bootinfo_v1));
164 bootinfo_v1.ssym = marks[MARK_SYM];
165 bootinfo_v1.esym = marks[MARK_END];
166 bcopy(name, bootinfo_v1.booted_kernel,
167 sizeof(bootinfo_v1.booted_kernel));
168 bcopy(boot_flags, bootinfo_v1.boot_flags,
169 sizeof(bootinfo_v1.boot_flags));
170 bootinfo_v1.hwrpb = (void *)HWRPB_ADDR;
171 bootinfo_v1.hwrpbsize = ((struct rpb *)HWRPB_ADDR)->rpb_size;
172 bootinfo_v1.cngetc = NULL;
173 bootinfo_v1.cnputc = NULL;
174 bootinfo_v1.cnpollc = NULL;
175 bootinfo_v1.howto = boothowto;
176
177 entry = marks[MARK_START];
178 (*(void (*)(u_int64_t, u_int64_t, u_int64_t, void *, u_int64_t,
179 u_int64_t))entry)(0, ptbr_save, BOOTINFO_MAGIC, &bootinfo_v1, 1, 0);
180
181 fail:
182 halt();
183 }
184