]> git.saurik.com Git - apple/xnu.git/blob - pexpert/i386/pe_init.c
1ef6c7c7b3b21feb4fb15c29f4a9569ec3f8746f
[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 static boot_icon_element* norootIcon_lzss;
58 static const uint8_t* norootClut_lzss;
59
60 int PE_initialize_console( PE_Video * info, int op )
61 {
62 static int last_console = -1;
63
64 if (info) {
65 info->v_offset = 0;
66 info->v_length = 0;
67 info->v_display = GRAPHICS_MODE;
68 }
69
70 switch ( op ) {
71
72 case kPEDisableScreen:
73 initialize_screen(info, op);
74 kprintf("kPEDisableScreen %d\n", last_console);
75 if (!console_is_serial())
76 last_console = switch_to_serial_console();
77 break;
78
79 case kPEEnableScreen:
80 initialize_screen(info, op);
81 if (info) PE_state.video = *info;
82 kprintf("kPEEnableScreen %d\n", last_console);
83 if( last_console != -1)
84 switch_to_old_console( last_console);
85 break;
86
87 case kPEBaseAddressChange:
88 if (info) PE_state.video = *info;
89 /* fall thru */
90
91 default:
92 initialize_screen(info, op);
93 break;
94 }
95
96 return 0;
97 }
98
99 void PE_init_iokit(void)
100 {
101 enum { kMaxBootVar = 128 };
102
103 typedef struct {
104 char name[32];
105 unsigned long length;
106 unsigned long value[2];
107 } DriversPackageProp;
108
109 boolean_t bootClutInitialized = FALSE;
110 boolean_t noroot_rle_Initialized = FALSE;
111
112 DTEntry entry;
113 unsigned int size;
114 uint32_t *map;
115 boot_progress_element *bootPict;
116
117 norootIcon_lzss = NULL;
118 norootClut_lzss = NULL;
119
120 PE_init_kprintf(TRUE);
121 PE_init_printf(TRUE);
122
123 kprintf("Kernel boot args: '%s'\n", PE_boot_args());
124
125 /*
126 * Fetch the CLUT and the noroot image.
127 */
128
129 if( kSuccess == DTLookupEntry(NULL, "/chosen/memory-map", &entry)) {
130 if( kSuccess == DTGetProperty(entry, "BootCLUT", (void **) &map, &size)) {
131 if (sizeof(appleClut8) <= map[1]) {
132 bcopy( (void *)ml_static_ptovirt(map[0]), appleClut8, sizeof(appleClut8) );
133 bootClutInitialized = TRUE;
134 }
135 }
136
137 if( kSuccess == DTGetProperty(entry, "Pict-FailedBoot", (void **) &map, &size)) {
138 bootPict = (boot_progress_element *) ml_static_ptovirt(map[0]);
139 default_noroot.width = bootPict->width;
140 default_noroot.height = bootPict->height;
141 default_noroot.dx = 0;
142 default_noroot.dy = bootPict->yOffset;
143 default_noroot_data = &bootPict->data[0];
144 noroot_rle_Initialized = TRUE;
145 }
146
147 if( kSuccess == DTGetProperty(entry, "FailedCLUT", (void **) &map, &size)) {
148 norootClut_lzss = (uint8_t*) ml_static_ptovirt(map[0]);
149 }
150
151 if( kSuccess == DTGetProperty(entry, "FailedImage", (void **) &map, &size)) {
152 norootIcon_lzss = (boot_icon_element *) ml_static_ptovirt(map[0]);
153 if (norootClut_lzss == NULL) {
154 printf("ERROR: No FailedCLUT provided for noroot icon!\n");
155 }
156 }
157 }
158
159 if (!bootClutInitialized) {
160 bcopy( (void *) (uintptr_t) bootClut, (void *) appleClut8, sizeof(appleClut8) );
161 }
162
163 if (!noroot_rle_Initialized) {
164 default_noroot.width = kFailedBootWidth;
165 default_noroot.height = kFailedBootHeight;
166 default_noroot.dx = 0;
167 default_noroot.dy = kFailedBootOffset;
168 default_noroot_data = failedBootPict;
169 }
170
171 /*
172 * Initialize the panic UI
173 */
174 panic_ui_initialize( (unsigned char *) appleClut8 );
175
176 /*
177 * Initialize the spinning wheel (progress indicator).
178 */
179 vc_progress_initialize( &default_progress, default_progress_data1x, default_progress_data2x,
180 (unsigned char *) appleClut8 );
181
182 (void) StartIOKit( PE_state.deviceTreeHead, PE_state.bootArgs, gPEEFIRuntimeServices, NULL);
183 }
184
185 void PE_init_platform(boolean_t vm_initialized, void * _args)
186 {
187 boot_args *args = (boot_args *)_args;
188
189 if (PE_state.initialized == FALSE) {
190 PE_state.initialized = TRUE;
191
192 // New EFI-style
193 PE_state.bootArgs = _args;
194 PE_state.deviceTreeHead = (void *) ml_static_ptovirt(args->deviceTreeP);
195 PE_state.video.v_baseAddr = args->Video.v_baseAddr; // remains physical address
196 PE_state.video.v_rowBytes = args->Video.v_rowBytes;
197 PE_state.video.v_width = args->Video.v_width;
198 PE_state.video.v_height = args->Video.v_height;
199 PE_state.video.v_depth = args->Video.v_depth;
200 PE_state.video.v_display = args->Video.v_display;
201 PE_state.video.v_scale = (kBootArgsFlagHiDPI & args->flags) ? 2 : 1;
202 strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
203 sizeof(PE_state.video.v_pixelFormat));
204
205 #ifdef kBootArgsFlagHiDPI
206 if (args->flags & kBootArgsFlagHiDPI)
207 PE_state.video.v_scale = kPEScaleFactor2x;
208 else
209 PE_state.video.v_scale = kPEScaleFactor1x;
210 #else
211 PE_state.video.v_scale = kPEScaleFactor1x;
212 #endif
213 }
214
215 if (!vm_initialized) {
216
217 if (PE_state.deviceTreeHead) {
218 DTInit(PE_state.deviceTreeHead);
219 }
220
221 pe_identify_machine(args);
222 } else {
223 pe_init_debug();
224 }
225
226 }
227
228 void PE_create_console( void )
229 {
230 if ( PE_state.video.v_display == GRAPHICS_MODE )
231 PE_initialize_console( &PE_state.video, kPEGraphicsMode );
232 else
233 PE_initialize_console( &PE_state.video, kPETextMode );
234 }
235
236 int PE_current_console( PE_Video * info )
237 {
238 *info = PE_state.video;
239
240 return (0);
241 }
242
243 void PE_display_icon( __unused unsigned int flags, __unused const char * name )
244 {
245 if ( norootIcon_lzss && norootClut_lzss ) {
246 uint32_t width = norootIcon_lzss->width;
247 uint32_t height = norootIcon_lzss->height;
248 uint32_t x = ((PE_state.video.v_width - width) / 2);
249 uint32_t y = ((PE_state.video.v_height - height) / 2) + norootIcon_lzss->y_offset_from_center;
250
251 vc_display_lzss_icon(x, y, width, height,
252 &norootIcon_lzss->data[0],
253 norootIcon_lzss->data_size,
254 norootClut_lzss);
255 }
256 else if ( default_noroot_data ) {
257 vc_display_icon( &default_noroot, default_noroot_data );
258 } else {
259 printf("ERROR: No data found for noroot icon!\n");
260 }
261 }
262
263 boolean_t
264 PE_get_hotkey(__unused unsigned char key)
265 {
266 return (FALSE);
267 }
268
269 static timebase_callback_func gTimebaseCallback;
270
271 void PE_register_timebase_callback(timebase_callback_func callback)
272 {
273 gTimebaseCallback = callback;
274
275 PE_call_timebase_callback();
276 }
277
278 void PE_call_timebase_callback(void)
279 {
280 struct timebase_freq_t timebase_freq;
281 unsigned long num, den, cnt;
282
283 num = gPEClockFrequencyInfo.bus_clock_rate_num * gPEClockFrequencyInfo.bus_to_dec_rate_num;
284 den = gPEClockFrequencyInfo.bus_clock_rate_den * gPEClockFrequencyInfo.bus_to_dec_rate_den;
285
286 cnt = 2;
287 while (cnt <= den) {
288 if ((num % cnt) || (den % cnt)) {
289 cnt++;
290 continue;
291 }
292
293 num /= cnt;
294 den /= cnt;
295 }
296
297 timebase_freq.timebase_num = num;
298 timebase_freq.timebase_den = den;
299
300 if (gTimebaseCallback) gTimebaseCallback(&timebase_freq);
301 }
302
303 /*
304 * The default (non-functional) PE_poll_input handler.
305 */
306 static int
307 PE_stub_poll_input(__unused unsigned int options, char * c)
308 {
309 *c = 0xff;
310 return 1; /* 0 for success, 1 for unsupported */
311 }
312
313 /*
314 * Called by the kernel debugger to poll for keyboard input.
315 * Keyboard drivers may replace the default stub function
316 * with their polled-mode input function.
317 */
318 int (*PE_poll_input)(unsigned int options, char * c)
319 = PE_stub_poll_input;
320
321 boolean_t
322 PE_reboot_on_panic(void)
323 {
324 boot_args *args = (boot_args *)PE_state.bootArgs;
325
326 if (args->flags & kBootArgsFlagRebootOnPanic)
327 return TRUE;
328 else
329 return FALSE;
330 }