xref: /freebsd/stand/powerpc/ofw/main.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
3  * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/endian.h>
30 
31 #include <stand.h>
32 #include "openfirm.h"
33 #include "libofw.h"
34 #include "bootstrap.h"
35 
36 #include <machine/asm.h>
37 #include <machine/psl.h>
38 
39 struct arch_switch	archsw;		/* MI/MD interface boundary */
40 
41 extern char end[];
42 
43 uint32_t	acells, scells;
44 
45 static char bootargs[128];
46 
47 #define	HEAP_SIZE	0x800000
48 static char heap[HEAP_SIZE]; // In BSS, so uses no space
49 
50 #define OF_puts(fd, text) OF_write(fd, text, strlen(text))
51 
52 static __inline register_t
53 mfmsr(void)
54 {
55 	register_t value;
56 
57 	__asm __volatile ("mfmsr %0" : "=r"(value));
58 
59 	return (value);
60 }
61 
62 void
63 init_heap(void)
64 {
65 	bzero(heap, HEAP_SIZE);
66 
67 	setheap(heap, (void *)((uintptr_t)heap + HEAP_SIZE));
68 }
69 
70 uint64_t
71 memsize(void)
72 {
73 	phandle_t	memoryp;
74 	cell_t		reg[24];
75 	int		i, sz;
76 	uint64_t	memsz;
77 
78 	memsz = 0;
79 	memoryp = OF_instance_to_package(memory);
80 
81 	sz = OF_getencprop(memoryp, "reg", &reg[0], sizeof(reg));
82 	sz /= sizeof(reg[0]);
83 
84 	for (i = 0; i < sz; i += (acells + scells)) {
85 		if (scells > 1)
86 			memsz += (uint64_t)reg[i + acells] << 32;
87 		memsz += reg[i + acells + scells - 1];
88 	}
89 
90 	return (memsz);
91 }
92 
93 #ifdef CAS
94 extern int ppc64_cas(void);
95 
96 static int
97 ppc64_autoload(void)
98 {
99 	const char *cas;
100 
101 	if ((cas = getenv("cas")) && cas[0] == '1')
102 		if (ppc64_cas() != 0)
103 			return (-1);
104 	return (ofw_autoload());
105 }
106 #endif
107 
108 #if BYTE_ORDER == LITTLE_ENDIAN
109 /*
110  * In Little-endian, we cannot just branch to the client interface. Since
111  * the client interface is big endian, we have to rfid to it.
112  * Likewise, when execution resumes, we are in the wrong endianness so
113  * we must do a fixup before returning to the caller.
114  */
115 static int (*openfirmware_entry)(void *);
116 extern int openfirmware_trampoline(void *buf, int (*cb)(void *));
117 
118 /*
119  * Wrapper to pass the real entry point to our trampoline.
120  */
121 static int
122 openfirmware_docall(void *buf)
123 {
124 	return openfirmware_trampoline(buf, openfirmware_entry);
125 }
126 #endif
127 
128 int
129 main(int (*openfirm)(void *))
130 {
131 	phandle_t	root;
132 	int		i;
133 	char		bootpath[64];
134 	char		*ch;
135 	int		bargc;
136 	char		**bargv;
137 
138 	/*
139 	 * Initialise the Open Firmware routines by giving them the entry point.
140 	 */
141 #if BYTE_ORDER == LITTLE_ENDIAN
142 	/*
143 	 * Use a trampoline entry point for endian fixups.
144 	 */
145 	openfirmware_entry = openfirm;
146 	OF_init(openfirmware_docall);
147 #else
148 	OF_init(openfirm);
149 #endif
150 
151 	root = OF_finddevice("/");
152 
153 	scells = acells = 1;
154 	OF_getencprop(root, "#address-cells", &acells, sizeof(acells));
155 	OF_getencprop(root, "#size-cells", &scells, sizeof(scells));
156 
157 	/*
158 	 * Initialise the heap as early as possible.  Once this is done,
159 	 * alloc() is usable. The stack is buried inside us, so this is
160 	 * safe.
161 	 */
162 	init_heap();
163 
164 	/*
165          * Set up console.
166          */
167 	cons_probe();
168 
169 	archsw.arch_getdev = ofw_getdev;
170 	archsw.arch_copyin = ofw_copyin;
171 	archsw.arch_copyout = ofw_copyout;
172 	archsw.arch_readin = ofw_readin;
173 #ifdef CAS
174 	setenv("cas", "1", 0);
175 	archsw.arch_autoload = ppc64_autoload;
176 #else
177 	archsw.arch_autoload = ofw_autoload;
178 #endif
179 
180 	/* Set up currdev variable to have hooks in place. */
181 	env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset);
182 
183 	devinit();
184 
185 	printf("\n%s", bootprog_info);
186 	printf("Memory: %lldKB\n", memsize() / 1024);
187 
188 	OF_getprop(chosen, "bootpath", bootpath, 64);
189 	ch = strchr(bootpath, ':');
190 	*ch = '\0';
191 	printf("Booted from: %s\n", bootpath);
192 
193 	printf("\n");
194 
195 	/*
196 	 * Only parse the first bootarg if present. It should
197 	 * be simple to handle extra arguments
198 	 */
199 	OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
200 	bargc = 0;
201 	parse(&bargc, &bargv, bootargs);
202 	if (bargc == 1)
203 		env_setenv("currdev", EV_VOLATILE, bargv[0], gen_setcurrdev,
204 		    env_nounset);
205 	else
206 		env_setenv("currdev", EV_VOLATILE, bootpath,
207 			   gen_setcurrdev, env_nounset);
208 	env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset,
209 	    env_nounset);
210 	setenv("LINES", "24", 1);		/* optional */
211 
212 	/*
213 	 * On non-Apple hardware, where it works reliably, pass flattened
214 	 * device trees to the kernel by default instead of OF CI pointers.
215 	 * Apple hardware is the only virtual-mode OF implementation in
216 	 * existence, so far as I am aware, so use that as a flag.
217 	 */
218 	if (!(mfmsr() & PSL_DR))
219 		setenv("usefdt", "1", 1);
220 
221 	interact();				/* doesn't return */
222 
223 	OF_exit();
224 
225 	return 0;
226 }
227 
228 COMMAND_SET(halt, "halt", "halt the system", command_halt);
229 
230 static int
231 command_halt(int argc, char *argv[])
232 {
233 
234 	OF_exit();
235 	return (CMD_OK);
236 }
237 
238 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
239 
240 int
241 command_memmap(int argc, char **argv)
242 {
243 
244 	ofw_memmap(acells);
245 	return (CMD_OK);
246 }
247