xref: /openbsd/sys/arch/alpha/stand/boot/boot.c (revision e3d38566)
1*e3d38566Sderaadt /*	$OpenBSD: boot.c,v 1.30 2023/01/16 07:29:34 deraadt Exp $	*/
2dfe00690Smillert /*	$NetBSD: boot.c,v 1.10 1997/01/18 01:58:33 cgd Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1992, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * This code is derived from software contributed to Berkeley by
9df930be7Sderaadt  * Ralph Campbell.
10df930be7Sderaadt  *
11df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
12df930be7Sderaadt  * modification, are permitted provided that the following conditions
13df930be7Sderaadt  * are met:
14df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
15df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
16df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
17df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
18df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
1929295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
20df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
21df930be7Sderaadt  *    without specific prior written permission.
22df930be7Sderaadt  *
23df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df930be7Sderaadt  * SUCH DAMAGE.
34df930be7Sderaadt  *
35df930be7Sderaadt  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
36df930be7Sderaadt  */
37df930be7Sderaadt 
3834fbf6deSderaadt #include <lib/libkern/libkern.h>
391e699572Smickey #include <lib/libsa/stand.h>
400cfa65d9Smiod #include <lib/libsa/loadfile.h>
41f65494e6Sderaadt #include <lib/libsa/arc4.h>
42df930be7Sderaadt 
43df930be7Sderaadt #include <sys/param.h>
44df930be7Sderaadt #include <sys/exec.h>
45a250ccf5Smiod #include <sys/stat.h>
46a872426cSderaadt #include <sys/reboot.h>
47a250ccf5Smiod #define _KERNEL
48a250ccf5Smiod #include <sys/fcntl.h>
49a250ccf5Smiod #undef _KERNEL
50df930be7Sderaadt 
51777da4a6Sniklas #include <machine/rpb.h>
52df930be7Sderaadt #include <machine/prom.h>
53aed035abSart #include <machine/autoconf.h>
54df930be7Sderaadt 
55df930be7Sderaadt char boot_file[128];
56df930be7Sderaadt char boot_flags[128];
57df930be7Sderaadt 
58b1919fcbSderaadt extern char bootprog_name[];
59df930be7Sderaadt 
60aed035abSart struct bootinfo_v1 bootinfo_v1;
61aed035abSart 
6210b2813dSderaadt extern paddr_t ptbr_save;
63df930be7Sderaadt 
64dfe00690Smillert int debug;
65dfe00690Smillert 
66a250ccf5Smiod char   rnddata[BOOTRANDOM_MAX];
67f65494e6Sderaadt struct rc4_ctx randomctx;
68a250ccf5Smiod 
69a872426cSderaadt int
loadrandom(char * name,char * buf,size_t buflen)70a250ccf5Smiod loadrandom(char *name, char *buf, size_t buflen)
71a250ccf5Smiod {
72a250ccf5Smiod 	struct stat sb;
73a872426cSderaadt 	int fd, i, error = 0;
74a250ccf5Smiod 
75a250ccf5Smiod 	fd = open(name, O_RDONLY);
76a250ccf5Smiod 	if (fd == -1) {
7754451086Smiod 		if (errno != EPERM)
78a250ccf5Smiod 			printf("cannot open %s: %s\n", name, strerror(errno));
79a872426cSderaadt 		return -1;
80a250ccf5Smiod 	}
81a872426cSderaadt 	if (fstat(fd, &sb) == -1) {
82a872426cSderaadt 		error = -1;
83a872426cSderaadt 		goto done;
84a872426cSderaadt 	}
85a872426cSderaadt 	if (read(fd, buf, buflen) != buflen) {
86a872426cSderaadt 		error = -1;
87a872426cSderaadt 		goto done;
88a872426cSderaadt 	}
89e4a6744dSderaadt 	if (sb.st_mode & S_ISTXT) {
90e4a6744dSderaadt 		printf("NOTE: random seed is being reused.\n");
91e4a6744dSderaadt 		error = -1;
92e4a6744dSderaadt 		goto done;
93e4a6744dSderaadt 	}
94e4a6744dSderaadt 	fchmod(fd, sb.st_mode | S_ISTXT);
95a872426cSderaadt done:
96a250ccf5Smiod 	close(fd);
97a872426cSderaadt 	return (error);
98a250ccf5Smiod }
992afebeaeSderaadt 
100*e3d38566Sderaadt void init_prom_calls(void);
101*e3d38566Sderaadt void OSFpal(void);
102*e3d38566Sderaadt void halt(void);
103*e3d38566Sderaadt 
1046f295a11Sderaadt int
main()10550ce9ee0Sniklas main()
106df930be7Sderaadt {
107dfe00690Smillert 	char *name, **namep;
108df930be7Sderaadt 	u_int64_t entry;
109a872426cSderaadt 	int rc, boothowto = 0;
1102340cfa5Sderaadt 	uint64_t marks[MARK_MAX];
1110cfa65d9Smiod #ifdef DEBUG
1120cfa65d9Smiod 	struct rpb *r;
1130cfa65d9Smiod 	struct mddt *mddtp;
1140cfa65d9Smiod 	struct mddt_cluster *memc;
1150cfa65d9Smiod 	int i;
1160cfa65d9Smiod #endif
117df930be7Sderaadt 
118df930be7Sderaadt 	/* Init prom callback vector. */
119df930be7Sderaadt 	init_prom_calls();
120df930be7Sderaadt 
121df930be7Sderaadt 	/* print a banner */
122b1919fcbSderaadt 	printf("%s\n", bootprog_name);
123df930be7Sderaadt 
124df930be7Sderaadt 	/* switch to OSF pal code. */
125df930be7Sderaadt 	OSFpal();
126df930be7Sderaadt 
1270cfa65d9Smiod #ifdef DEBUG
1280cfa65d9Smiod 	r = (struct rpb *)HWRPB_ADDR;
1290cfa65d9Smiod 	mddtp = (struct mddt *)(HWRPB_ADDR + r->rpb_memdat_off);
1300cfa65d9Smiod 	printf("%d memory clusters\n", mddtp->mddt_cluster_cnt);
1310cfa65d9Smiod 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
1320cfa65d9Smiod 		memc = &mddtp->mddt_clusters[i];
1330cfa65d9Smiod 		printf("%d: (%d) %lx-%lx\n", i, memc->mddt_usage,
1340cfa65d9Smiod 		    memc->mddt_pfn << PAGE_SHIFT,
1350cfa65d9Smiod 		    (memc->mddt_pfn + memc->mddt_pg_cnt) << PAGE_SHIFT);
1360cfa65d9Smiod 	}
1370cfa65d9Smiod #endif
138a250ccf5Smiod 
139a872426cSderaadt 	if (loadrandom(BOOTRANDOM, rnddata, sizeof(rnddata)) == 0)
140a872426cSderaadt 		boothowto |= RB_GOODRANDOM;
141f65494e6Sderaadt 	rc4_keysetup(&randomctx, rnddata, sizeof rnddata);
142f65494e6Sderaadt 	rc4_skip(&randomctx, 1536);
143a250ccf5Smiod 
144df930be7Sderaadt 	prom_getenv(PROM_E_BOOTED_FILE, boot_file, sizeof(boot_file));
145df930be7Sderaadt 	prom_getenv(PROM_E_BOOTED_OSFLAGS, boot_flags, sizeof(boot_flags));
146df930be7Sderaadt 
1475dc7e580Sderaadt 	if (boot_file[0] != '\0') {
148eb45c5c9Sderaadt 		(void)printf("Boot file: %s %s\n", boot_file, boot_flags);
149b1919fcbSderaadt 		name = boot_file;
1505dc7e580Sderaadt 	} else
151b1919fcbSderaadt 		name = "bsd";
1520cfa65d9Smiod 
1530cfa65d9Smiod 	(void)printf("Loading %s...\n", name);
1540cfa65d9Smiod 	marks[MARK_START] = 0;
1550cfa65d9Smiod 	rc = loadfile(name, marks, LOAD_KERNEL | COUNT_KERNEL);
1560cfa65d9Smiod 	(void)printf("\n");
1570cfa65d9Smiod 	if (rc != 0)
158aed035abSart 		goto fail;
159df930be7Sderaadt 
160aed035abSart 	/*
161aed035abSart 	 * Fill in the bootinfo for the kernel.
162aed035abSart 	 */
163aed035abSart 	bzero(&bootinfo_v1, sizeof(bootinfo_v1));
1640cfa65d9Smiod 	bootinfo_v1.ssym = marks[MARK_SYM];
1650cfa65d9Smiod 	bootinfo_v1.esym = marks[MARK_END];
166aed035abSart 	bcopy(name, bootinfo_v1.booted_kernel,
167aed035abSart 	    sizeof(bootinfo_v1.booted_kernel));
168aed035abSart 	bcopy(boot_flags, bootinfo_v1.boot_flags,
169aed035abSart 	    sizeof(bootinfo_v1.boot_flags));
170aed035abSart 	bootinfo_v1.hwrpb = (void *)HWRPB_ADDR;
171aed035abSart 	bootinfo_v1.hwrpbsize = ((struct rpb *)HWRPB_ADDR)->rpb_size;
172aed035abSart 	bootinfo_v1.cngetc = NULL;
173aed035abSart 	bootinfo_v1.cnputc = NULL;
174aed035abSart 	bootinfo_v1.cnpollc = NULL;
175c2afdefeSderaadt 	bootinfo_v1.howto = boothowto;
176aed035abSart 
1770cfa65d9Smiod 	entry = marks[MARK_START];
178aed035abSart 	(*(void (*)(u_int64_t, u_int64_t, u_int64_t, void *, u_int64_t,
1790cfa65d9Smiod 	    u_int64_t))entry)(0, ptbr_save, BOOTINFO_MAGIC, &bootinfo_v1, 1, 0);
180aed035abSart 
181aed035abSart fail:
18250ce9ee0Sniklas 	halt();
183df930be7Sderaadt }
184