xref: /netbsd-src/sys/arch/hpc/stand/hpcboot/boot.cpp (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin /*	$NetBSD: boot.cpp,v 1.7 2008/04/28 20:23:20 martin Exp $	*/
29173eae7Such 
39173eae7Such /*-
4643a469dSuch  * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
59173eae7Such  * All rights reserved.
69173eae7Such  *
79173eae7Such  * This code is derived from software contributed to The NetBSD Foundation
89173eae7Such  * by UCHIYAMA Yasushi.
99173eae7Such  *
109173eae7Such  * Redistribution and use in source and binary forms, with or without
119173eae7Such  * modification, are permitted provided that the following conditions
129173eae7Such  * are met:
139173eae7Such  * 1. Redistributions of source code must retain the above copyright
149173eae7Such  *    notice, this list of conditions and the following disclaimer.
159173eae7Such  * 2. Redistributions in binary form must reproduce the above copyright
169173eae7Such  *    notice, this list of conditions and the following disclaimer in the
179173eae7Such  *    documentation and/or other materials provided with the distribution.
189173eae7Such  *
199173eae7Such  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209173eae7Such  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219173eae7Such  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229173eae7Such  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239173eae7Such  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249173eae7Such  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259173eae7Such  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269173eae7Such  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279173eae7Such  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289173eae7Such  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299173eae7Such  * POSSIBILITY OF SUCH DAMAGE.
309173eae7Such  */
319173eae7Such 
329173eae7Such #include <hpcdefs.h>
339173eae7Such #include <hpcboot.h>
349173eae7Such #include <boot.h>
359173eae7Such 
369173eae7Such #include <load.h>
379173eae7Such #include <load_elf.h>
389173eae7Such #include <load_coff.h>
399173eae7Such #undef DPRINTF // trash coff_machdep.h's DPRINTF
409173eae7Such 
419173eae7Such #include <console.h>
429173eae7Such 
439173eae7Such #include <file.h>
449173eae7Such 
459173eae7Such #ifdef ARM
469173eae7Such #include <arm/arm_boot.h>
479173eae7Such #endif
489173eae7Such #ifdef MIPS
499173eae7Such #include <mips/mips_boot.h>
509173eae7Such #endif
519173eae7Such #ifdef SHx
529173eae7Such #include <sh3/sh_boot.h>
539173eae7Such #endif
549173eae7Such 
559173eae7Such Boot *Boot::_instance = 0;
569173eae7Such 
579173eae7Such Boot &
Instance()589173eae7Such Boot::Instance()
599173eae7Such {
6032b30275Such 
619173eae7Such 	if (_instance)
629173eae7Such 		return  *_instance;
639173eae7Such 
64bd926f64Such 	// register bootloader to menu system.
65bd926f64Such 	// (will be invoked by boot button)
669173eae7Such 	struct HpcMenuInterface::boot_hook_args bha;
679173eae7Such 	bha.func	= hpcboot;
689173eae7Such 	bha.arg		= 0;
69bd926f64Such 	HPC_MENU.register_boot_hook(bha);
709173eae7Such 
719173eae7Such #ifdef ARM
729173eae7Such 	_instance = new ARMBoot();
739173eae7Such #endif
749173eae7Such #ifdef MIPS
759173eae7Such 	_instance = new MIPSBoot();
769173eae7Such #endif
779173eae7Such #ifdef SHx
789173eae7Such 	_instance = new SHBoot();
799173eae7Such #endif
809173eae7Such 
819173eae7Such 	memset(&_instance->args, 0, sizeof(struct BootSetupArgs));
829173eae7Such 	_instance->args.consoleEnable = TRUE;
839173eae7Such 
849173eae7Such 	return *_instance;
859173eae7Such }
869173eae7Such 
879173eae7Such void
Destroy()889173eae7Such Boot::Destroy()
899173eae7Such {
9032b30275Such 
919173eae7Such 	if (_instance)
929173eae7Such 		delete _instance;
939173eae7Such }
949173eae7Such 
959173eae7Such BOOL
setup()96bd926f64Such Boot::setup()
979173eae7Such {
98bd926f64Such 	struct HpcMenuInterface::HpcMenuPreferences &pref = HPC_PREFERENCE;
99bd926f64Such 
1009173eae7Such 	args.console = pref.boot_serial ? CONSOLE_SERIAL : CONSOLE_LCD;
1019173eae7Such 
1029173eae7Such 	// file path.
1039173eae7Such 	TCHAR *dir = pref.dir_user_path;
1049173eae7Such 	if (wcsstr(dir, TEXT("http://")))
1059173eae7Such 		args.file = FILE_HTTP;
1069173eae7Such 	else
1079173eae7Such 		args.file = wcschr(dir, TEXT('/')) ? FILE_UFS : FILE_FAT;
1089173eae7Such 	wcscpy(args.fileRoot, dir);
1099173eae7Such 
1109173eae7Such 	// file name.
1119173eae7Such 	wcscpy(args.fileName, pref.kernel_user_file);
1129173eae7Such 	args.loadmfs = (pref.rootfs == 2);
1139173eae7Such 	if (args.loadmfs)
1149173eae7Such 		wcscpy(args.mfsName, pref.rootfs_file);
1159173eae7Such 
1169173eae7Such 	// debug options.
1179173eae7Such 	args.loaderDebug	= FALSE;
1189173eae7Such 	args.architectureDebug	= FALSE;
1199173eae7Such 	args.memorymanagerDebug	= FALSE;
1209173eae7Such 	args.fileDebug		= FALSE;
1219173eae7Such 
1229173eae7Such 	return TRUE;
1239173eae7Such }
1249173eae7Such 
Boot()1259173eae7Such Boot::Boot()
1269173eae7Such {
1279173eae7Such 	_arch	= 0;
1289173eae7Such 	_mem	= 0;
1299173eae7Such 	_file	= 0;
1309173eae7Such 	_loader	= 0;
1319173eae7Such 	// set default console
1329173eae7Such 	_cons = Console::Instance();
1339173eae7Such }
1349173eae7Such 
~Boot()1359173eae7Such Boot::~Boot()
1369173eae7Such {
13732b30275Such 
1389173eae7Such 	if (_file)
1399173eae7Such 		delete _file;
1409173eae7Such 	if (_loader)
1419173eae7Such 		delete _loader;
1429173eae7Such }
1439173eae7Such 
1449173eae7Such BOOL
create()1459173eae7Such Boot::create()
1469173eae7Such {
147643a469dSuch 
148643a469dSuch 	// Set this console (setuped by machine dependent part) as default.
149643a469dSuch 	Console::changeConsole(*_cons);
150643a469dSuch 
1519173eae7Such 	// File manager.
1529173eae7Such 	_file = new FileManager(_cons, args.file);
1539173eae7Such 	_file->setDebug() = args.fileDebug;
1549173eae7Such 
1559173eae7Such 	return TRUE;
1569173eae7Such }
1579173eae7Such 
1589173eae7Such BOOL
attachLoader()1599173eae7Such Boot::attachLoader()
1609173eae7Such {
16132b30275Such 
1629173eae7Such 	switch (Loader::objectFormat(*_file)) {
1639173eae7Such 	case LOADER_ELF:
1649173eae7Such 		_loader = new ElfLoader(_cons, _mem);
1659173eae7Such 		break;
1669173eae7Such 	case LOADER_COFF:
1679173eae7Such 		_loader = new CoffLoader(_cons, _mem);
1689173eae7Such 		break;
1699173eae7Such 	default:
1709173eae7Such 		DPRINTF((TEXT("unknown file format.\n")));
1719173eae7Such 		return FALSE;
1729173eae7Such 	}
1739173eae7Such 
1749173eae7Such 	return TRUE;
1759173eae7Such }
176