1/*	$NetBSD: boot.cpp,v 1.6 2005/12/11 12:17:28 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <hpcdefs.h>
33#include <hpcboot.h>
34#include <boot.h>
35
36#include <load.h>
37#include <load_elf.h>
38#include <load_coff.h>
39#undef DPRINTF // trash coff_machdep.h's DPRINTF
40
41#include <console.h>
42
43#include <file.h>
44
45#ifdef ARM
46#include <arm/arm_boot.h>
47#endif
48#ifdef MIPS
49#include <mips/mips_boot.h>
50#endif
51#ifdef SHx
52#include <sh3/sh_boot.h>
53#endif
54
55Boot *Boot::_instance = 0;
56
57Boot &
58Boot::Instance()
59{
60
61	if (_instance)
62		return  *_instance;
63
64	// register bootloader to menu system.
65	// (will be invoked by boot button)
66	struct HpcMenuInterface::boot_hook_args bha;
67	bha.func	= hpcboot;
68	bha.arg		= 0;
69	HPC_MENU.register_boot_hook(bha);
70
71#ifdef ARM
72	_instance = new ARMBoot();
73#endif
74#ifdef MIPS
75	_instance = new MIPSBoot();
76#endif
77#ifdef SHx
78	_instance = new SHBoot();
79#endif
80
81	memset(&_instance->args, 0, sizeof(struct BootSetupArgs));
82	_instance->args.consoleEnable = TRUE;
83
84	return *_instance;
85}
86
87void
88Boot::Destroy()
89{
90
91	if (_instance)
92		delete _instance;
93}
94
95BOOL
96Boot::setup()
97{
98	struct HpcMenuInterface::HpcMenuPreferences &pref = HPC_PREFERENCE;
99
100	args.console = pref.boot_serial ? CONSOLE_SERIAL : CONSOLE_LCD;
101
102	// file path.
103	TCHAR *dir = pref.dir_user_path;
104	if (wcsstr(dir, TEXT("http://")))
105		args.file = FILE_HTTP;
106	else
107		args.file = wcschr(dir, TEXT('/')) ? FILE_UFS : FILE_FAT;
108	wcscpy(args.fileRoot, dir);
109
110	// file name.
111	wcscpy(args.fileName, pref.kernel_user_file);
112	args.loadmfs = (pref.rootfs == 2);
113	if (args.loadmfs)
114		wcscpy(args.mfsName, pref.rootfs_file);
115
116	// debug options.
117	args.loaderDebug	= FALSE;
118	args.architectureDebug	= FALSE;
119	args.memorymanagerDebug	= FALSE;
120	args.fileDebug		= FALSE;
121
122	return TRUE;
123}
124
125Boot::Boot()
126{
127	_arch	= 0;
128	_mem	= 0;
129	_file	= 0;
130	_loader	= 0;
131	// set default console
132	_cons = Console::Instance();
133}
134
135Boot::~Boot()
136{
137
138	if (_file)
139		delete _file;
140	if (_loader)
141		delete _loader;
142}
143
144BOOL
145Boot::create()
146{
147
148	// Set this console (setuped by machine dependent part) as default.
149	Console::changeConsole(*_cons);
150
151	// File manager.
152	_file = new FileManager(_cons, args.file);
153	_file->setDebug() = args.fileDebug;
154
155	return TRUE;
156}
157
158BOOL
159Boot::attachLoader()
160{
161
162	switch (Loader::objectFormat(*_file)) {
163	case LOADER_ELF:
164		_loader = new ElfLoader(_cons, _mem);
165		break;
166	case LOADER_COFF:
167		_loader = new CoffLoader(_cons, _mem);
168		break;
169	default:
170		DPRINTF((TEXT("unknown file format.\n")));
171		return FALSE;
172	}
173
174	return TRUE;
175}
176