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