xref: /freebsd/stand/powerpc/ofw/main.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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/endian.h>
29 
30 #include <stand.h>
31 #include "openfirm.h"
32 #include "libofw.h"
33 #include "bootstrap.h"
34 
35 #include <machine/asm.h>
36 #include <machine/psl.h>
37 
38 struct arch_switch	archsw;		/* MI/MD interface boundary */
39 
40 extern char end[];
41 
42 uint32_t	acells, scells;
43 
44 static char bootargs[128];
45 
46 #define	HEAP_SIZE	0x800000
47 static char heap[HEAP_SIZE]; // In BSS, so uses no space
48 
49 #define OF_puts(fd, text) OF_write(fd, text, strlen(text))
50 
51 static __inline register_t
52 mfmsr(void)
53 {
54 	register_t value;
55 
56 	__asm __volatile ("mfmsr %0" : "=r"(value));
57 
58 	return (value);
59 }
60 
61 void
62 init_heap(void)
63 {
64 	bzero(heap, HEAP_SIZE);
65 
66 	setheap(heap, (void *)((uintptr_t)heap + HEAP_SIZE));
67 }
68 
69 uint64_t
70 memsize(void)
71 {
72 	phandle_t	memoryp;
73 	cell_t		reg[24];
74 	int		i, sz;
75 	uint64_t	memsz;
76 
77 	memsz = 0;
78 	memoryp = OF_instance_to_package(memory);
79 
80 	sz = OF_getencprop(memoryp, "reg", &reg[0], sizeof(reg));
81 	sz /= sizeof(reg[0]);
82 
83 	for (i = 0; i < sz; i += (acells + scells)) {
84 		if (scells > 1)
85 			memsz += (uint64_t)reg[i + acells] << 32;
86 		memsz += reg[i + acells + scells - 1];
87 	}
88 
89 	return (memsz);
90 }
91 
92 #ifdef CAS
93 extern int ppc64_cas(void);
94 
95 static int
96 ppc64_autoload(void)
97 {
98 	const char *cas;
99 
100 	if ((cas = getenv("cas")) && cas[0] == '1')
101 		if (ppc64_cas() != 0)
102 			return (-1);
103 	return (ofw_autoload());
104 }
105 #endif
106 
107 #if BYTE_ORDER == LITTLE_ENDIAN
108 /*
109  * In Little-endian, we cannot just branch to the client interface. Since
110  * the client interface is big endian, we have to rfid to it.
111  * Likewise, when execution resumes, we are in the wrong endianness so
112  * we must do a fixup before returning to the caller.
113  */
114 static int (*openfirmware_entry)(void *);
115 extern int openfirmware_trampoline(void *buf, int (*cb)(void *));
116 
117 /*
118  * Wrapper to pass the real entry point to our trampoline.
119  */
120 static int
121 openfirmware_docall(void *buf)
122 {
123 	return openfirmware_trampoline(buf, openfirmware_entry);
124 }
125 #endif
126 
127 int
128 main(int (*openfirm)(void *))
129 {
130 	phandle_t	root;
131 	int		i;
132 	char		bootpath[64];
133 	char		*ch;
134 	int		bargc;
135 	char		**bargv;
136 
137 	/*
138 	 * Initialise the Open Firmware routines by giving them the entry point.
139 	 */
140 #if BYTE_ORDER == LITTLE_ENDIAN
141 	/*
142 	 * Use a trampoline entry point for endian fixups.
143 	 */
144 	openfirmware_entry = openfirm;
145 	OF_init(openfirmware_docall);
146 #else
147 	OF_init(openfirm);
148 #endif
149 
150 	root = OF_finddevice("/");
151 
152 	scells = acells = 1;
153 	OF_getencprop(root, "#address-cells", &acells, sizeof(acells));
154 	OF_getencprop(root, "#size-cells", &scells, sizeof(scells));
155 
156 	/*
157 	 * Initialise the heap as early as possible.  Once this is done,
158 	 * alloc() is usable. The stack is buried inside us, so this is
159 	 * safe.
160 	 */
161 	init_heap();
162 
163 	/*
164          * Set up console.
165          */
166 	cons_probe();
167 
168 	archsw.arch_getdev = ofw_getdev;
169 	archsw.arch_copyin = ofw_copyin;
170 	archsw.arch_copyout = ofw_copyout;
171 	archsw.arch_readin = ofw_readin;
172 #ifdef CAS
173 	setenv("cas", "1", 0);
174 	archsw.arch_autoload = ppc64_autoload;
175 #else
176 	archsw.arch_autoload = ofw_autoload;
177 #endif
178 
179 	/* Set up currdev variable to have hooks in place. */
180 	env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset);
181 
182 	devinit();
183 
184 	printf("\n%s", bootprog_info);
185 	printf("Memory: %lldKB\n", memsize() / 1024);
186 
187 	OF_getprop(chosen, "bootpath", bootpath, 64);
188 	ch = strchr(bootpath, ':');
189 	*ch = '\0';
190 	printf("Booted from: %s\n", bootpath);
191 
192 	printf("\n");
193 
194 	/*
195 	 * Only parse the first bootarg if present. It should
196 	 * be simple to handle extra arguments
197 	 */
198 	OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
199 	bargc = 0;
200 	parse(&bargc, &bargv, bootargs);
201 	if (bargc == 1)
202 		env_setenv("currdev", EV_VOLATILE, bargv[0], gen_setcurrdev,
203 		    env_nounset);
204 	else
205 		env_setenv("currdev", EV_VOLATILE, bootpath,
206 			   gen_setcurrdev, env_nounset);
207 	env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset,
208 	    env_nounset);
209 	setenv("LINES", "24", 1);		/* optional */
210 
211 	/*
212 	 * On non-Apple hardware, where it works reliably, pass flattened
213 	 * device trees to the kernel by default instead of OF CI pointers.
214 	 * Apple hardware is the only virtual-mode OF implementation in
215 	 * existence, so far as I am aware, so use that as a flag.
216 	 */
217 	if (!(mfmsr() & PSL_DR))
218 		setenv("usefdt", "1", 1);
219 
220 	interact();				/* doesn't return */
221 
222 	OF_exit();
223 
224 	return 0;
225 }
226 
227 COMMAND_SET(halt, "halt", "halt the system", command_halt);
228 
229 static int
230 command_halt(int argc, char *argv[])
231 {
232 
233 	OF_exit();
234 	return (CMD_OK);
235 }
236 
237 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
238 
239 int
240 command_memmap(int argc, char **argv)
241 {
242 
243 	ofw_memmap(acells);
244 	return (CMD_OK);
245 }
246