xref: /titanic_54/usr/src/boot/sys/boot/ofw/common/main.c (revision c8951589222ca4706bf8bfc6df387c3d4b569ad4)
14a5d661aSToomas Soome /*-
24a5d661aSToomas Soome  * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
34a5d661aSToomas Soome  * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
44a5d661aSToomas Soome  * All rights reserved.
54a5d661aSToomas Soome  *
64a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
74a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
84a5d661aSToomas Soome  * are met:
94a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
104a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
114a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
124a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
134a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
144a5d661aSToomas Soome  *
154a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
164a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254a5d661aSToomas Soome  * SUCH DAMAGE.
264a5d661aSToomas Soome  */
274a5d661aSToomas Soome 
284a5d661aSToomas Soome #include <sys/cdefs.h>
294a5d661aSToomas Soome 
304a5d661aSToomas Soome #include <stand.h>
314a5d661aSToomas Soome #include "openfirm.h"
324a5d661aSToomas Soome #include "libofw.h"
334a5d661aSToomas Soome #include "bootstrap.h"
344a5d661aSToomas Soome 
354a5d661aSToomas Soome struct arch_switch	archsw;		/* MI/MD interface boundary */
364a5d661aSToomas Soome 
374a5d661aSToomas Soome extern char end[];
38*c8951589SToomas Soome extern char bootprog_info[];
394a5d661aSToomas Soome 
404a5d661aSToomas Soome u_int32_t	acells, scells;
414a5d661aSToomas Soome 
424a5d661aSToomas Soome static char bootargs[128];
434a5d661aSToomas Soome 
444a5d661aSToomas Soome #define	HEAP_SIZE	0x100000
454a5d661aSToomas Soome 
464a5d661aSToomas Soome #define OF_puts(fd, text) OF_write(fd, text, strlen(text))
474a5d661aSToomas Soome 
484a5d661aSToomas Soome void
init_heap(void)494a5d661aSToomas Soome init_heap(void)
504a5d661aSToomas Soome {
514a5d661aSToomas Soome 	void	*base;
524a5d661aSToomas Soome 	ihandle_t stdout;
534a5d661aSToomas Soome 
544a5d661aSToomas Soome 	if ((base = ofw_alloc_heap(HEAP_SIZE)) == (void *)0xffffffff) {
554a5d661aSToomas Soome 		OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
564a5d661aSToomas Soome 		OF_puts(stdout, "Heap memory claim failed!\n");
574a5d661aSToomas Soome 		OF_enter();
584a5d661aSToomas Soome 	}
594a5d661aSToomas Soome 
604a5d661aSToomas Soome 	setheap(base, (void *)((int)base + HEAP_SIZE));
614a5d661aSToomas Soome }
624a5d661aSToomas Soome 
634a5d661aSToomas Soome uint64_t
memsize(void)644a5d661aSToomas Soome memsize(void)
654a5d661aSToomas Soome {
664a5d661aSToomas Soome 	phandle_t	memoryp;
674a5d661aSToomas Soome 	cell_t		reg[24];
684a5d661aSToomas Soome 	int		i, sz;
694a5d661aSToomas Soome 	u_int64_t	memsz;
704a5d661aSToomas Soome 
714a5d661aSToomas Soome 	memsz = 0;
724a5d661aSToomas Soome 	memoryp = OF_instance_to_package(memory);
734a5d661aSToomas Soome 
744a5d661aSToomas Soome 	sz = OF_getprop(memoryp, "reg", &reg, sizeof(reg));
754a5d661aSToomas Soome 	sz /= sizeof(reg[0]);
764a5d661aSToomas Soome 
774a5d661aSToomas Soome 	for (i = 0; i < sz; i += (acells + scells)) {
784a5d661aSToomas Soome 		if (scells > 1)
794a5d661aSToomas Soome 			memsz += (uint64_t)reg[i + acells] << 32;
804a5d661aSToomas Soome 		memsz += reg[i + acells + scells - 1];
814a5d661aSToomas Soome 	}
824a5d661aSToomas Soome 
834a5d661aSToomas Soome 	return (memsz);
844a5d661aSToomas Soome }
854a5d661aSToomas Soome 
864a5d661aSToomas Soome int
main(int (* openfirm)(void *))874a5d661aSToomas Soome main(int (*openfirm)(void *))
884a5d661aSToomas Soome {
894a5d661aSToomas Soome 	phandle_t	root;
904a5d661aSToomas Soome 	int		i;
914a5d661aSToomas Soome 	char		bootpath[64];
924a5d661aSToomas Soome 	char		*ch;
934a5d661aSToomas Soome 	int		bargc;
944a5d661aSToomas Soome 	char		**bargv;
954a5d661aSToomas Soome 
964a5d661aSToomas Soome 	/*
979c4e5a08SToomas Soome 	 * Initialize the Open Firmware routines by giving them the entry point.
984a5d661aSToomas Soome 	 */
994a5d661aSToomas Soome 	OF_init(openfirm);
1004a5d661aSToomas Soome 
1014a5d661aSToomas Soome 	root = OF_finddevice("/");
1024a5d661aSToomas Soome 
1034a5d661aSToomas Soome 	scells = acells = 1;
1044a5d661aSToomas Soome 	OF_getprop(root, "#address-cells", &acells, sizeof(acells));
1054a5d661aSToomas Soome 	OF_getprop(root, "#size-cells", &scells, sizeof(scells));
1064a5d661aSToomas Soome 
1074a5d661aSToomas Soome 	/*
1084a5d661aSToomas Soome 	 * Initialise the heap as early as possible.  Once this is done,
1094a5d661aSToomas Soome 	 * alloc() is usable. The stack is buried inside us, so this is
1104a5d661aSToomas Soome 	 * safe.
1114a5d661aSToomas Soome 	 */
1124a5d661aSToomas Soome 	init_heap();
1134a5d661aSToomas Soome 
1144a5d661aSToomas Soome 	/*
1154a5d661aSToomas Soome          * Set up console.
1164a5d661aSToomas Soome          */
1174a5d661aSToomas Soome 	cons_probe();
1184a5d661aSToomas Soome 
1194a5d661aSToomas Soome 	/*
1204a5d661aSToomas Soome 	 * March through the device switch probing for things.
1214a5d661aSToomas Soome 	 */
1224a5d661aSToomas Soome 	for (i = 0; devsw[i] != NULL; i++)
1234a5d661aSToomas Soome 		if (devsw[i]->dv_init != NULL)
1244a5d661aSToomas Soome 			(devsw[i]->dv_init)();
1254a5d661aSToomas Soome 
126*c8951589SToomas Soome 	printf("\n%s", bootprog_info);
1274a5d661aSToomas Soome 	printf("Memory: %lldKB\n", memsize() / 1024);
1284a5d661aSToomas Soome 
1294a5d661aSToomas Soome 	OF_getprop(chosen, "bootpath", bootpath, 64);
1304a5d661aSToomas Soome 	ch = strchr(bootpath, ':');
1314a5d661aSToomas Soome 	*ch = '\0';
1324a5d661aSToomas Soome 	printf("Booted from: %s\n", bootpath);
1334a5d661aSToomas Soome 
1344a5d661aSToomas Soome 	printf("\n");
1354a5d661aSToomas Soome 
1364a5d661aSToomas Soome 	/*
1374a5d661aSToomas Soome 	 * Only parse the first bootarg if present. It should
1384a5d661aSToomas Soome 	 * be simple to handle extra arguments
1394a5d661aSToomas Soome 	 */
1404a5d661aSToomas Soome 	OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
1414a5d661aSToomas Soome 	bargc = 0;
1424a5d661aSToomas Soome 	parse(&bargc, &bargv, bootargs);
1434a5d661aSToomas Soome 	if (bargc == 1)
1444a5d661aSToomas Soome 		env_setenv("currdev", EV_VOLATILE, bargv[0], ofw_setcurrdev,
1454a5d661aSToomas Soome 		    env_nounset);
1464a5d661aSToomas Soome 	else
1474a5d661aSToomas Soome 		env_setenv("currdev", EV_VOLATILE, bootpath,
1484a5d661aSToomas Soome 			   ofw_setcurrdev, env_nounset);
1494a5d661aSToomas Soome 	env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset,
1504a5d661aSToomas Soome 	    env_nounset);
1514a5d661aSToomas Soome 	setenv("LINES", "24", 1);		/* optional */
1524a5d661aSToomas Soome 
1534a5d661aSToomas Soome 	archsw.arch_getdev = ofw_getdev;
1544a5d661aSToomas Soome 	archsw.arch_copyin = ofw_copyin;
1554a5d661aSToomas Soome 	archsw.arch_copyout = ofw_copyout;
1564a5d661aSToomas Soome 	archsw.arch_readin = ofw_readin;
1574a5d661aSToomas Soome 	archsw.arch_autoload = ofw_autoload;
1584a5d661aSToomas Soome 
1594a5d661aSToomas Soome 	interact(NULL);				/* doesn't return */
1604a5d661aSToomas Soome 
1614a5d661aSToomas Soome 	OF_exit();
1624a5d661aSToomas Soome 
1634a5d661aSToomas Soome 	return 0;
1644a5d661aSToomas Soome }
1654a5d661aSToomas Soome 
1664a5d661aSToomas Soome COMMAND_SET(halt, "halt", "halt the system", command_halt);
1674a5d661aSToomas Soome 
1684a5d661aSToomas Soome static int
command_halt(int argc,char * argv[])1694a5d661aSToomas Soome command_halt(int argc, char *argv[])
1704a5d661aSToomas Soome {
1714a5d661aSToomas Soome 
1724a5d661aSToomas Soome 	OF_exit();
1734a5d661aSToomas Soome 	return (CMD_OK);
1744a5d661aSToomas Soome }
1754a5d661aSToomas Soome 
1764a5d661aSToomas Soome COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
1774a5d661aSToomas Soome 
1784a5d661aSToomas Soome int
command_memmap(int argc,char ** argv)1794a5d661aSToomas Soome command_memmap(int argc, char **argv)
1804a5d661aSToomas Soome {
1814a5d661aSToomas Soome 
1824a5d661aSToomas Soome 	ofw_memmap(acells);
1834a5d661aSToomas Soome 	return (CMD_OK);
1844a5d661aSToomas Soome }
185