xref: /freebsd/stand/powerpc/ofw/main.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
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 __FBSDID("$FreeBSD$");
30 
31 #include <stand.h>
32 #include "openfirm.h"
33 #include "libofw.h"
34 #include "bootstrap.h"
35 
36 #include <machine/psl.h>
37 
38 struct arch_switch	archsw;		/* MI/MD interface boundary */
39 
40 extern char end[];
41 extern char bootprog_info[];
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 *)((int)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_getprop(memoryp, "reg", &reg, 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 int
94 main(int (*openfirm)(void *))
95 {
96 	phandle_t	root;
97 	int		i;
98 	char		bootpath[64];
99 	char		*ch;
100 	int		bargc;
101 	char		**bargv;
102 
103 	/*
104 	 * Initialise the Open Firmware routines by giving them the entry point.
105 	 */
106 	OF_init(openfirm);
107 
108 	root = OF_finddevice("/");
109 
110 	scells = acells = 1;
111 	OF_getprop(root, "#address-cells", &acells, sizeof(acells));
112 	OF_getprop(root, "#size-cells", &scells, sizeof(scells));
113 
114 	/*
115 	 * Initialise the heap as early as possible.  Once this is done,
116 	 * alloc() is usable. The stack is buried inside us, so this is
117 	 * safe.
118 	 */
119 	init_heap();
120 
121 	/*
122          * Set up console.
123          */
124 	cons_probe();
125 
126 	/*
127 	 * March through the device switch probing for things.
128 	 */
129 	for (i = 0; devsw[i] != NULL; i++)
130 		if (devsw[i]->dv_init != NULL)
131 			(devsw[i]->dv_init)();
132 
133 	printf("\n%s", bootprog_info);
134 	printf("Memory: %lldKB\n", memsize() / 1024);
135 
136 	OF_getprop(chosen, "bootpath", bootpath, 64);
137 	ch = strchr(bootpath, ':');
138 	*ch = '\0';
139 	printf("Booted from: %s\n", bootpath);
140 
141 	printf("\n");
142 
143 	/*
144 	 * Only parse the first bootarg if present. It should
145 	 * be simple to handle extra arguments
146 	 */
147 	OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
148 	bargc = 0;
149 	parse(&bargc, &bargv, bootargs);
150 	if (bargc == 1)
151 		env_setenv("currdev", EV_VOLATILE, bargv[0], ofw_setcurrdev,
152 		    env_nounset);
153 	else
154 		env_setenv("currdev", EV_VOLATILE, bootpath,
155 			   ofw_setcurrdev, env_nounset);
156 	env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset,
157 	    env_nounset);
158 	setenv("LINES", "24", 1);		/* optional */
159 
160 	/*
161 	 * On non-Apple hardware, where it works reliably, pass flattened
162 	 * device trees to the kernel by default instead of OF CI pointers.
163 	 * Apple hardware is the only virtual-mode OF implementation in
164 	 * existence, so far as I am aware, so use that as a flag.
165 	 */
166 	if (!(mfmsr() & PSL_DR))
167 		setenv("usefdt", "1", 1);
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 	archsw.arch_autoload = ofw_autoload;
174 
175 	interact();				/* doesn't return */
176 
177 	OF_exit();
178 
179 	return 0;
180 }
181 
182 COMMAND_SET(halt, "halt", "halt the system", command_halt);
183 
184 static int
185 command_halt(int argc, char *argv[])
186 {
187 
188 	OF_exit();
189 	return (CMD_OK);
190 }
191 
192 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
193 
194 int
195 command_memmap(int argc, char **argv)
196 {
197 
198 	ofw_memmap(acells);
199 	return (CMD_OK);
200 }
201