xref: /openbsd/sys/arch/macppc/stand/main.c (revision 09467b48)
1 /*	$OpenBSD: main.c,v 1.11 2020/05/25 15:10:18 deraadt Exp $	*/
2 /*	$NetBSD: boot.c,v 1.1 1997/04/16 20:29:17 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Jason R. Thorpe.  All rights reserved.
6  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
7  * Copyright (C) 1995, 1996 TooLs GmbH.
8  * All rights reserved.
9  *
10  * ELF support derived from NetBSD/alpha's boot loader, written
11  * by Christopher G. Demetriou.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by TooLs GmbH.
24  * 4. The name of TooLs GmbH may not be used to endorse or promote products
25  *    derived from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
33  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * First try for the boot code
41  *
42  * Input syntax is:
43  *	[promdev[{:|,}partition]]/[filename] [flags]
44  */
45 
46 #define	ELFSIZE		32		/* We use 32-bit ELF. */
47 
48 #include <sys/param.h>
49 #include <sys/exec.h>
50 #include <sys/exec_elf.h>
51 #include <sys/reboot.h>
52 #include <sys/disklabel.h>
53 
54 #include <lib/libkern/libkern.h>
55 #include <lib/libsa/loadfile.h>
56 #include <stand/boot/cmd.h>
57 
58 #include <machine/cpu.h>
59 
60 #include <macppc/stand/ofdev.h>
61 #include <macppc/stand/openfirm.h>
62 
63 #include "libsa.h"
64 
65 char bootdev[128];
66 int boothowto;
67 int debug;
68 
69 void
70 get_alt_bootdev(char *, size_t, char *, size_t);
71 
72 static void
73 prom2boot(char *dev)
74 {
75 	char *cp, *lp = 0;
76 
77 	for (cp = dev; *cp; cp++)
78 		if (*cp == ':')
79 			lp = cp;
80 	if (!lp)
81 		lp = cp;
82 	*lp = 0;
83 }
84 
85 static void
86 chain(void (*entry)(), char *args, void *ssym, void *esym)
87 {
88 	extern char end[];
89 	int l;
90 #ifdef __notyet__
91 	int machine_tag;
92 #endif
93 
94 	freeall();
95 
96 	/*
97 	 * Stash pointer to end of symbol table after the argument
98 	 * strings.
99 	 */
100 	l = strlen(args) + 1;
101 	bcopy(&ssym, args + l, sizeof(ssym));
102 	l += sizeof(ssym);
103 	bcopy(&esym, args + l, sizeof(esym));
104 	l += sizeof(esym);
105 
106 #ifdef __notyet__
107 	/*
108 	 * Tell the kernel we're an OpenFirmware system.
109 	 */
110 	machine_tag = POWERPC_MACHINE_OPENFIRMWARE;
111 	bcopy(&machine_tag, args + l, sizeof(machine_tag));
112 	l += sizeof(machine_tag);
113 #endif
114 
115 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
116 	panic("chain");
117 }
118 
119 /*
120  * XXX This limits the maximum size of the (uncompressed) bsd.rd to a
121  * little under 11MB.
122  */
123 #define CLAIM_LIMIT	0x00c00000
124 
125 char bootline[512];
126 
127 extern char *kernelfile;
128 
129 int
130 main(void)
131 {
132 	int chosen;
133 
134 	/*
135 	 * Get the boot arguments from Openfirmware
136 	 */
137 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
138 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
139 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
140 		printf("Invalid Openfirmware environment\n");
141 		exit();
142 	}
143 	prom2boot(bootdev);
144 	get_alt_bootdev(bootdev, sizeof(bootdev), bootline, sizeof(bootline));
145 	if (bootline[0] != '\0')
146 		kernelfile = bootline;
147 
148 	OF_claim((void *)0x00100000, CLAIM_LIMIT, 0); /* XXX */
149 	boot(0);
150 	return 0;
151 }
152 
153 void
154 get_alt_bootdev(char *dev, size_t devsz, char *line, size_t linesz)
155 {
156 	char *p;
157 	int len;
158 	/*
159 	 * if the kernel image specified contains a ':' it is
160 	 * [device]:[kernel], so separate the two fields.
161 	 */
162 	p = strrchr(line, ':');
163 	if (p == NULL)
164 		return;
165 	/* user specified boot device for kernel */
166 	len = p - line + 1; /* str len plus nil */
167 	strlcpy(dev, line, len > devsz ? devsz : len);
168 
169 	strlcpy(line, p+1, linesz); /* rest of string after ':' */
170 }
171 
172 
173 void
174 devboot(dev_t dev, char *p)
175 {
176 	strlcpy(p, bootdev, BOOTDEVLEN);
177 }
178 
179 void
180 run_loadfile(uint64_t *marks, int howto)
181 {
182 	char bootline[512];		/* Should check size? */
183 	u_int32_t entry;
184 	char *cp;
185 	void *ssym, *esym;
186 
187 	strlcpy(bootline, opened_name, sizeof bootline);
188 	cp = bootline + strlen(bootline);
189 	*cp++ = ' ';
190         *cp = '-';
191         if (howto & RB_ASKNAME)
192                 *++cp = 'a';
193         if (howto & RB_CONFIG)
194                 *++cp = 'c';
195         if (howto & RB_SINGLE)
196                 *++cp = 's';
197         if (howto & RB_KDB)
198                 *++cp = 'd';
199         if (howto & RB_GOODRANDOM)
200                 *++cp = 'R';
201         if (*cp == '-')
202 		*--cp = 0;
203 	else
204 		*++cp = 0;
205 
206 	entry = marks[MARK_ENTRY];
207 	ssym = (void *)marks[MARK_SYM];
208 	esym = (void *)marks[MARK_END];
209 	{
210 		u_int32_t lastpage;
211 		lastpage = roundup(marks[MARK_END], PAGE_SIZE);
212 		OF_release((void*)lastpage, CLAIM_LIMIT - lastpage);
213 	}
214 
215 	chain((void *)entry, bootline, ssym, esym);
216 
217 	_rtt();
218 }
219 
220 int
221 cnspeed(dev_t dev, int sp)
222 {
223 	return CONSPEED;
224 }
225 
226 char ttyname_buf[8];
227 
228 char *
229 ttyname(int fd)
230 {
231         snprintf(ttyname_buf, sizeof ttyname_buf, "ofc0");
232 	return ttyname_buf;
233 }
234 
235 dev_t
236 ttydev(char *name)
237 {
238 	return makedev(0,0);
239 }
240