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