xref: /titanic_54/usr/src/boot/sys/boot/ofw/common/main.c (revision f4a5523c06038fcb37de45a740ef4385ede131c8)
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 
30 #include <stand.h>
31 #include "openfirm.h"
32 #include "libofw.h"
33 #include "bootstrap.h"
34 
35 struct arch_switch	archsw;		/* MI/MD interface boundary */
36 
37 extern char end[];
38 extern char bootprog_info[];
39 
40 u_int32_t	acells, scells;
41 
42 static char bootargs[128];
43 
44 #define	HEAP_SIZE	0x100000
45 
46 #define OF_puts(fd, text) OF_write(fd, text, strlen(text))
47 
48 void
49 init_heap(void)
50 {
51 	void	*base;
52 	ihandle_t stdout;
53 
54 	if ((base = ofw_alloc_heap(HEAP_SIZE)) == (void *)0xffffffff) {
55 		OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
56 		OF_puts(stdout, "Heap memory claim failed!\n");
57 		OF_enter();
58 	}
59 
60 	setheap(base, (void *)((int)base + HEAP_SIZE));
61 }
62 
63 uint64_t
64 memsize(void)
65 {
66 	phandle_t	memoryp;
67 	cell_t		reg[24];
68 	int		i, sz;
69 	u_int64_t	memsz;
70 
71 	memsz = 0;
72 	memoryp = OF_instance_to_package(memory);
73 
74 	sz = OF_getprop(memoryp, "reg", &reg, sizeof(reg));
75 	sz /= sizeof(reg[0]);
76 
77 	for (i = 0; i < sz; i += (acells + scells)) {
78 		if (scells > 1)
79 			memsz += (uint64_t)reg[i + acells] << 32;
80 		memsz += reg[i + acells + scells - 1];
81 	}
82 
83 	return (memsz);
84 }
85 
86 int
87 main(int (*openfirm)(void *))
88 {
89 	phandle_t	root;
90 	int		i;
91 	char		bootpath[64];
92 	char		*ch;
93 	int		bargc;
94 	char		**bargv;
95 
96 	/*
97 	 * Initialize the Open Firmware routines by giving them the entry point.
98 	 */
99 	OF_init(openfirm);
100 
101 	root = OF_finddevice("/");
102 
103 	scells = acells = 1;
104 	OF_getprop(root, "#address-cells", &acells, sizeof(acells));
105 	OF_getprop(root, "#size-cells", &scells, sizeof(scells));
106 
107 	/*
108 	 * Initialise the heap as early as possible.  Once this is done,
109 	 * alloc() is usable. The stack is buried inside us, so this is
110 	 * safe.
111 	 */
112 	init_heap();
113 
114 	/*
115          * Set up console.
116          */
117 	cons_probe();
118 
119 	/*
120 	 * March through the device switch probing for things.
121 	 */
122 	for (i = 0; devsw[i] != NULL; i++)
123 		if (devsw[i]->dv_init != NULL)
124 			(devsw[i]->dv_init)();
125 
126 	printf("\n%s", bootprog_info);
127 	printf("Memory: %lldKB\n", memsize() / 1024);
128 
129 	OF_getprop(chosen, "bootpath", bootpath, 64);
130 	ch = strchr(bootpath, ':');
131 	*ch = '\0';
132 	printf("Booted from: %s\n", bootpath);
133 
134 	printf("\n");
135 
136 	/*
137 	 * Only parse the first bootarg if present. It should
138 	 * be simple to handle extra arguments
139 	 */
140 	OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
141 	bargc = 0;
142 	parse(&bargc, &bargv, bootargs);
143 	if (bargc == 1)
144 		env_setenv("currdev", EV_VOLATILE, bargv[0], ofw_setcurrdev,
145 		    env_nounset);
146 	else
147 		env_setenv("currdev", EV_VOLATILE, bootpath,
148 			   ofw_setcurrdev, env_nounset);
149 	env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset,
150 	    env_nounset);
151 	setenv("LINES", "24", 1);		/* optional */
152 
153 	archsw.arch_getdev = ofw_getdev;
154 	archsw.arch_copyin = ofw_copyin;
155 	archsw.arch_copyout = ofw_copyout;
156 	archsw.arch_readin = ofw_readin;
157 	archsw.arch_autoload = ofw_autoload;
158 
159 	interact(NULL);				/* doesn't return */
160 
161 	OF_exit();
162 
163 	return 0;
164 }
165 
166 COMMAND_SET(halt, "halt", "halt the system", command_halt);
167 
168 static int
169 command_halt(int argc, char *argv[])
170 {
171 
172 	OF_exit();
173 	return (CMD_OK);
174 }
175 
176 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
177 
178 int
179 command_memmap(int argc, char **argv)
180 {
181 
182 	ofw_memmap(acells);
183 	return (CMD_OK);
184 }
185