]> git.saurik.com Git - apple/xnu.git/blob - pexpert/i386/pe_init.c
fc29c1a6525034ef70f35e9a7c9b2529668ed5b0
[apple/xnu.git] / pexpert / i386 / pe_init.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * file: pe_init.c
30 * i386 platform expert initialization.
31 */
32 #include <sys/types.h>
33 #include <mach/vm_param.h>
34 #include <machine/machine_routines.h>
35 #include <pexpert/protos.h>
36 #include <pexpert/pexpert.h>
37 #include <pexpert/boot.h>
38 #include <pexpert/device_tree.h>
39 #include <pexpert/pe_images.h>
40 #include <kern/sched_prim.h>
41 #include <kern/debug.h>
42
43 #include "boot_images.h"
44
45 /* extern references */
46 extern void pe_identify_machine(void * args);
47
48 /* private globals */
49 PE_state_t PE_state;
50
51 /* Clock Frequency Info */
52 clock_frequency_info_t gPEClockFrequencyInfo;
53
54 void *gPEEFISystemTable;
55 void *gPEEFIRuntimeServices;
56
57 int PE_initialize_console( PE_Video * info, int op )
58 {
59 static int last_console = -1;
60
61 if (info) {
62 info->v_offset = 0;
63 info->v_length = 0;
64 info->v_display = GRAPHICS_MODE;
65 }
66
67 switch ( op ) {
68
69 case kPEDisableScreen:
70 initialize_screen(info, op);
71 kprintf("kPEDisableScreen %d\n", last_console);
72 if (!console_is_serial())
73 last_console = switch_to_serial_console();
74 break;
75
76 case kPEEnableScreen:
77 initialize_screen(info, op);
78 if (info) PE_state.video = *info;
79 kprintf("kPEEnableScreen %d\n", last_console);
80 if( last_console != -1)
81 switch_to_old_console( last_console);
82 break;
83
84 case kPEBaseAddressChange:
85 if (info) PE_state.video = *info;
86 /* fall thru */
87
88 default:
89 initialize_screen(info, op);
90 break;
91 }
92
93 return 0;
94 }
95
96 void PE_init_iokit(void)
97 {
98 enum { kMaxBootVar = 128 };
99
100 typedef struct {
101 char name[32];
102 unsigned long length;
103 unsigned long value[2];
104 } DriversPackageProp;
105
106 boolean_t bootClutInitialized = FALSE;
107 boolean_t norootInitialized = FALSE;
108 DTEntry entry;
109 unsigned int size;
110 uint32_t *map;
111 boot_progress_element *bootPict;
112
113 PE_init_kprintf(TRUE);
114 PE_init_printf(TRUE);
115
116 kprintf("Kernel boot args: '%s'\n", PE_boot_args());
117
118 /*
119 * Fetch the CLUT and the noroot image.
120 */
121
122 if( kSuccess == DTLookupEntry(NULL, "/chosen/memory-map", &entry)) {
123 if( kSuccess == DTGetProperty(entry, "BootCLUT", (void **) &map, &size)) {
124 if (sizeof(appleClut8) <= map[1]) {
125 bcopy( (void *)ml_static_ptovirt(map[0]), appleClut8, sizeof(appleClut8) );
126 bootClutInitialized = TRUE;
127 }
128 }
129
130 if( kSuccess == DTGetProperty(entry, "Pict-FailedBoot", (void **) &map, &size)) {
131 bootPict = (boot_progress_element *) ml_static_ptovirt(map[0]);
132 default_noroot.width = bootPict->width;
133 default_noroot.height = bootPict->height;
134 default_noroot.dx = 0;
135 default_noroot.dy = bootPict->yOffset;
136 default_noroot_data = &bootPict->data[0];
137 norootInitialized = TRUE;
138 }
139 }
140
141 if (!bootClutInitialized) {
142 bcopy( (void *) (uintptr_t) bootClut, (void *) appleClut8, sizeof(appleClut8) );
143 }
144
145 if (!norootInitialized) {
146 default_noroot.width = kFailedBootWidth;
147 default_noroot.height = kFailedBootHeight;
148 default_noroot.dx = 0;
149 default_noroot.dy = kFailedBootOffset;
150 default_noroot_data = failedBootPict;
151 }
152
153 /*
154 * Initialize the panic UI
155 */
156 panic_ui_initialize( (unsigned char *) appleClut8 );
157
158 /*
159 * Initialize the spinning wheel (progress indicator).
160 */
161 vc_progress_initialize( &default_progress, default_progress_data,
162 (unsigned char *) appleClut8 );
163
164 (void) StartIOKit( PE_state.deviceTreeHead, PE_state.bootArgs, gPEEFIRuntimeServices, NULL);
165 }
166
167 void PE_init_platform(boolean_t vm_initialized, void * _args)
168 {
169 boot_args *args = (boot_args *)_args;
170
171 if (PE_state.initialized == FALSE) {
172 PE_state.initialized = TRUE;
173
174 // New EFI-style
175 PE_state.bootArgs = _args;
176 PE_state.deviceTreeHead = (void *) ml_static_ptovirt(args->deviceTreeP);
177 PE_state.video.v_baseAddr = args->Video.v_baseAddr; // remains physical address
178 PE_state.video.v_rowBytes = args->Video.v_rowBytes;
179 PE_state.video.v_width = args->Video.v_width;
180 PE_state.video.v_height = args->Video.v_height;
181 PE_state.video.v_depth = args->Video.v_depth;
182 PE_state.video.v_display = args->Video.v_display;
183 strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
184 sizeof(PE_state.video.v_pixelFormat));
185 }
186
187 if (!vm_initialized) {
188
189 if (PE_state.deviceTreeHead) {
190 DTInit(PE_state.deviceTreeHead);
191 }
192
193 pe_identify_machine(args);
194 } else {
195 pe_init_debug();
196 }
197
198 }
199
200 void PE_create_console( void )
201 {
202 if ( PE_state.video.v_display == GRAPHICS_MODE )
203 PE_initialize_console( &PE_state.video, kPEGraphicsMode );
204 else
205 PE_initialize_console( &PE_state.video, kPETextMode );
206 }
207
208 int PE_current_console( PE_Video * info )
209 {
210 *info = PE_state.video;
211
212 return (0);
213 }
214
215 void PE_display_icon( __unused unsigned int flags, __unused const char * name )
216 {
217 if ( default_noroot_data )
218 vc_display_icon( &default_noroot, default_noroot_data );
219 }
220
221 boolean_t
222 PE_get_hotkey(__unused unsigned char key)
223 {
224 return (FALSE);
225 }
226
227 static timebase_callback_func gTimebaseCallback;
228
229 void PE_register_timebase_callback(timebase_callback_func callback)
230 {
231 gTimebaseCallback = callback;
232
233 PE_call_timebase_callback();
234 }
235
236 void PE_call_timebase_callback(void)
237 {
238 struct timebase_freq_t timebase_freq;
239 unsigned long num, den, cnt;
240
241 num = gPEClockFrequencyInfo.bus_clock_rate_num * gPEClockFrequencyInfo.bus_to_dec_rate_num;
242 den = gPEClockFrequencyInfo.bus_clock_rate_den * gPEClockFrequencyInfo.bus_to_dec_rate_den;
243
244 cnt = 2;
245 while (cnt <= den) {
246 if ((num % cnt) || (den % cnt)) {
247 cnt++;
248 continue;
249 }
250
251 num /= cnt;
252 den /= cnt;
253 }
254
255 timebase_freq.timebase_num = num;
256 timebase_freq.timebase_den = den;
257
258 if (gTimebaseCallback) gTimebaseCallback(&timebase_freq);
259 }
260
261 /*
262 * The default (non-functional) PE_poll_input handler.
263 */
264 static int
265 PE_stub_poll_input(__unused unsigned int options, char * c)
266 {
267 *c = 0xff;
268 return 1; /* 0 for success, 1 for unsupported */
269 }
270
271 /*
272 * Called by the kernel debugger to poll for keyboard input.
273 * Keyboard drivers may replace the default stub function
274 * with their polled-mode input function.
275 */
276 int (*PE_poll_input)(unsigned int options, char * c)
277 = PE_stub_poll_input;
278
279 boolean_t
280 PE_reboot_on_panic(void)
281 {
282 return FALSE;
283 }