]> git.saurik.com Git - apple/xnu.git/blame - pexpert/i386/pe_init.c
xnu-4903.241.1.tar.gz
[apple/xnu.git] / pexpert / i386 / pe_init.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * file: pe_init.c
30 * i386 platform expert initialization.
31 */
fe8ab488 32
1c79356b
A
33#include <sys/types.h>
34#include <mach/vm_param.h>
b0d623f7 35#include <machine/machine_routines.h>
1c79356b
A
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>
9bccf70c 41#include <kern/sched_prim.h>
0c530ab8 42#include <kern/debug.h>
1c79356b 43
3e170ce0
A
44#if CONFIG_CSR
45#include <sys/csr.h>
46#endif
47
9bccf70c 48#include "boot_images.h"
1c79356b
A
49
50/* extern references */
51extern void pe_identify_machine(void * args);
cc8bc92a 52extern int kdb_printf(const char *format, ...) __printflike(1,2);
1c79356b 53/* private globals */
0b4e3aa0 54PE_state_t PE_state;
1c79356b
A
55
56/* Clock Frequency Info */
57clock_frequency_info_t gPEClockFrequencyInfo;
58
2d21ac55
A
59void *gPEEFISystemTable;
60void *gPEEFIRuntimeServices;
0c530ab8 61
7ddcb079
A
62static boot_icon_element* norootIcon_lzss;
63static const uint8_t* norootClut_lzss;
64
1c79356b
A
65int PE_initialize_console( PE_Video * info, int op )
66{
67 static int last_console = -1;
1c79356b 68
2d21ac55
A
69 if (info) {
70 info->v_offset = 0;
71 info->v_length = 0;
72 info->v_display = GRAPHICS_MODE;
1c79356b 73 }
1c79356b
A
74
75 switch ( op ) {
76
77 case kPEDisableScreen:
2d21ac55 78 initialize_screen(info, op);
1c79356b 79 kprintf("kPEDisableScreen %d\n", last_console);
0c530ab8
A
80 if (!console_is_serial())
81 last_console = switch_to_serial_console();
1c79356b
A
82 break;
83
84 case kPEEnableScreen:
2d21ac55 85 initialize_screen(info, op);
060df5ea 86 if (info) PE_state.video = *info;
1c79356b 87 kprintf("kPEEnableScreen %d\n", last_console);
1c79356b
A
88 if( last_console != -1)
89 switch_to_old_console( last_console);
1c79356b
A
90 break;
91
060df5ea
A
92 case kPEBaseAddressChange:
93 if (info) PE_state.video = *info;
94 /* fall thru */
95
1c79356b 96 default:
2d21ac55 97 initialize_screen(info, op);
1c79356b
A
98 break;
99 }
100
101 return 0;
102}
103
104void PE_init_iokit(void)
105{
91447636 106 enum { kMaxBootVar = 128 };
9bccf70c 107
0c530ab8 108 boolean_t bootClutInitialized = FALSE;
7ddcb079
A
109 boolean_t noroot_rle_Initialized = FALSE;
110
0c530ab8 111 DTEntry entry;
2d21ac55 112 unsigned int size;
b0d623f7 113 uint32_t *map;
2d21ac55 114 boot_progress_element *bootPict;
0c530ab8 115
7ddcb079
A
116 norootIcon_lzss = NULL;
117 norootClut_lzss = NULL;
118
55e303ae
A
119 PE_init_kprintf(TRUE);
120 PE_init_printf(TRUE);
121
0c530ab8
A
122 kprintf("Kernel boot args: '%s'\n", PE_boot_args());
123
0b4e3aa0 124 /*
0c530ab8 125 * Fetch the CLUT and the noroot image.
0b4e3aa0
A
126 */
127
2d21ac55 128 if( kSuccess == DTLookupEntry(NULL, "/chosen/memory-map", &entry)) {
7ddcb079
A
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 }
4452a7af 156 }
21362eb3 157
0c530ab8 158 if (!bootClutInitialized) {
7ddcb079 159 bcopy( (void *) (uintptr_t) bootClut, (void *) appleClut8, sizeof(appleClut8) );
0c530ab8 160 }
6601e61a 161
7ddcb079
A
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;
0c530ab8 168 }
55e303ae 169
9bccf70c
A
170 /*
171 * Initialize the spinning wheel (progress indicator).
172 */
fe8ab488
A
173 vc_progress_initialize(&default_progress,
174 default_progress_data1x,
175 default_progress_data2x,
176 default_progress_data3x,
177 (unsigned char *) appleClut8);
1c79356b 178
3e170ce0 179 StartIOKit( PE_state.deviceTreeHead, PE_state.bootArgs, gPEEFIRuntimeServices, NULL);
1c79356b
A
180}
181
0c530ab8 182void PE_init_platform(boolean_t vm_initialized, void * _args)
1c79356b 183{
0c530ab8 184 boot_args *args = (boot_args *)_args;
1c79356b 185
0c530ab8 186 if (PE_state.initialized == FALSE) {
1c79356b 187 PE_state.initialized = TRUE;
0c530ab8
A
188
189 // New EFI-style
190 PE_state.bootArgs = _args;
b0d623f7 191 PE_state.deviceTreeHead = (void *) ml_static_ptovirt(args->deviceTreeP);
39037602
A
192 if (args->Video.v_baseAddr) {
193 PE_state.video.v_baseAddr = args->Video.v_baseAddr; // remains physical address
194 PE_state.video.v_rowBytes = args->Video.v_rowBytes;
195 PE_state.video.v_width = args->Video.v_width;
196 PE_state.video.v_height = args->Video.v_height;
197 PE_state.video.v_depth = args->Video.v_depth;
198 PE_state.video.v_display = args->Video.v_display;
199 strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
200 sizeof(PE_state.video.v_pixelFormat));
201 } else {
202 PE_state.video.v_baseAddr = args->VideoV1.v_baseAddr; // remains physical address
203 PE_state.video.v_rowBytes = args->VideoV1.v_rowBytes;
204 PE_state.video.v_width = args->VideoV1.v_width;
205 PE_state.video.v_height = args->VideoV1.v_height;
206 PE_state.video.v_depth = args->VideoV1.v_depth;
207 PE_state.video.v_display = args->VideoV1.v_display;
208 strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
209 sizeof(PE_state.video.v_pixelFormat));
210 }
316670eb
A
211
212#ifdef kBootArgsFlagHiDPI
213 if (args->flags & kBootArgsFlagHiDPI)
214 PE_state.video.v_scale = kPEScaleFactor2x;
215 else
216 PE_state.video.v_scale = kPEScaleFactor1x;
217#else
218 PE_state.video.v_scale = kPEScaleFactor1x;
219#endif
0b4e3aa0 220 }
1c79356b 221
0c530ab8 222 if (!vm_initialized) {
6d2010ae 223
0c530ab8
A
224 if (PE_state.deviceTreeHead) {
225 DTInit(PE_state.deviceTreeHead);
b7266188 226 }
0c530ab8
A
227
228 pe_identify_machine(args);
0b4e3aa0 229 pe_init_debug();
55e303ae 230 }
b7266188 231
1c79356b
A
232}
233
234void PE_create_console( void )
235{
0c530ab8 236 if ( PE_state.video.v_display == GRAPHICS_MODE )
1c79356b
A
237 PE_initialize_console( &PE_state.video, kPEGraphicsMode );
238 else
239 PE_initialize_console( &PE_state.video, kPETextMode );
240}
241
242int PE_current_console( PE_Video * info )
243{
244 *info = PE_state.video;
245
1c79356b
A
246 return (0);
247}
248
91447636 249void PE_display_icon( __unused unsigned int flags, __unused const char * name )
1c79356b 250{
7ddcb079
A
251 if ( norootIcon_lzss && norootClut_lzss ) {
252 uint32_t width = norootIcon_lzss->width;
253 uint32_t height = norootIcon_lzss->height;
254 uint32_t x = ((PE_state.video.v_width - width) / 2);
255 uint32_t y = ((PE_state.video.v_height - height) / 2) + norootIcon_lzss->y_offset_from_center;
256
257 vc_display_lzss_icon(x, y, width, height,
258 &norootIcon_lzss->data[0],
259 norootIcon_lzss->data_size,
260 norootClut_lzss);
261 }
262 else if ( default_noroot_data ) {
9bccf70c 263 vc_display_icon( &default_noroot, default_noroot_data );
7ddcb079
A
264 } else {
265 printf("ERROR: No data found for noroot icon!\n");
266 }
1c79356b
A
267}
268
2d21ac55
A
269boolean_t
270PE_get_hotkey(__unused unsigned char key)
1c79356b
A
271{
272 return (FALSE);
273}
274
275static timebase_callback_func gTimebaseCallback;
276
277void PE_register_timebase_callback(timebase_callback_func callback)
278{
279 gTimebaseCallback = callback;
280
281 PE_call_timebase_callback();
282}
283
284void PE_call_timebase_callback(void)
285{
286 struct timebase_freq_t timebase_freq;
287 unsigned long num, den, cnt;
288
289 num = gPEClockFrequencyInfo.bus_clock_rate_num * gPEClockFrequencyInfo.bus_to_dec_rate_num;
290 den = gPEClockFrequencyInfo.bus_clock_rate_den * gPEClockFrequencyInfo.bus_to_dec_rate_den;
291
292 cnt = 2;
293 while (cnt <= den) {
294 if ((num % cnt) || (den % cnt)) {
295 cnt++;
296 continue;
297 }
298
299 num /= cnt;
300 den /= cnt;
301 }
302
303 timebase_freq.timebase_num = num;
304 timebase_freq.timebase_den = den;
305
306 if (gTimebaseCallback) gTimebaseCallback(&timebase_freq);
307}
308
0b4e3aa0
A
309/*
310 * The default (non-functional) PE_poll_input handler.
311 */
312static int
91447636 313PE_stub_poll_input(__unused unsigned int options, char * c)
0b4e3aa0
A
314{
315 *c = 0xff;
316 return 1; /* 0 for success, 1 for unsupported */
317}
318
319/*
320 * Called by the kernel debugger to poll for keyboard input.
321 * Keyboard drivers may replace the default stub function
322 * with their polled-mode input function.
323 */
324int (*PE_poll_input)(unsigned int options, char * c)
325 = PE_stub_poll_input;
55e303ae 326
b7266188
A
327boolean_t
328PE_reboot_on_panic(void)
329{
13f56ec4
A
330 boot_args *args = (boot_args *)PE_state.bootArgs;
331
332 if (args->flags & kBootArgsFlagRebootOnPanic)
333 return TRUE;
334 else
335 return FALSE;
b7266188 336}
3e170ce0 337
743345f9
A
338void
339PE_sync_panic_buffers(void)
340{
341}
342
3e170ce0
A
343/* rdar://problem/21244753 */
344uint32_t
345PE_i_can_has_debugger(uint32_t *debug_flags)
346{
cc8bc92a
A
347#if DEVELOPMENT || DEBUG
348 if (debug_flags) {
349 assert(debug_boot_arg_inited);
350 }
351#endif
352
3e170ce0 353#if CONFIG_CSR
5ba3f43e 354 if (csr_check(CSR_ALLOW_KERNEL_DEBUGGER) != 0) {
3e170ce0
A
355 if (debug_flags)
356 *debug_flags = 0;
357 return FALSE;
358 }
359#endif
360 if (debug_flags) {
361 *debug_flags = debug_boot_arg;
362 }
363 return TRUE;
364}
5ba3f43e
A
365
366uint32_t
367PE_get_offset_into_panic_region(char *location)
368{
369 assert(panic_info != NULL);
370 assert(location > (char *) panic_info);
371
cc8bc92a 372 return (uint32_t) (location - (char *) panic_info);
5ba3f43e
A
373}
374
375void
376PE_init_panicheader()
377{
378 bzero(panic_info, offsetof(struct macos_panic_header, mph_data));
379 panic_info->mph_panic_log_offset = PE_get_offset_into_panic_region(debug_buf_base);
380
381 panic_info->mph_magic = MACOS_PANIC_MAGIC;
382 panic_info->mph_version = MACOS_PANIC_HEADER_CURRENT_VERSION;
383
384 return;
385}
386
387/*
388 * Tries to update the panic header to keep it consistent on nested panics.
389 *
390 * NOTE: The purpose of this function is NOT to detect/correct corruption in the panic region,
391 * it is to update the panic header to make it consistent when we nest panics.
cc8bc92a
A
392 *
393 * We try to avoid nested panics/asserts on x86 because they are difficult to debug, so log any
394 * inconsistencies we find.
5ba3f43e
A
395 */
396void
397PE_update_panicheader_nestedpanic()
398{
399 /* If the panic log offset is not set, re-init the panic header */
400 if (panic_info->mph_panic_log_offset == 0) {
401 PE_init_panicheader();
402 panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;
403 return;
404 }
405
406 panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;
407
cc8bc92a
A
408 /* Usually indicative of corruption in the panic region */
409 if(!(((panic_info->mph_stackshot_offset == 0) && (panic_info->mph_stackshot_len == 0)) ||
410 ((panic_info->mph_stackshot_offset != 0) && (panic_info->mph_stackshot_len != 0)))) {
411 kdb_printf("panic_info contains invalid stackshot metadata: mph_stackshot_offset 0x%x mph_stackshot_len 0x%x\n",
412 panic_info->mph_stackshot_offset, panic_info->mph_stackshot_len);
413 }
414
415 /*
416 * macOS panic logs contain nested panic data, if we've already closed the panic log,
417 * begin the other log.
418 */
419 if ((panic_info->mph_panic_log_len != 0) && (panic_info->mph_other_log_offset == 0)) {
420 panic_info->mph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
421
422 /* Usually indicative of corruption in the panic region */
423 if (panic_info->mph_other_log_len != 0) {
424 kdb_printf("panic_info contains invalid other log metadata (zero offset but non-zero length), length was 0x%x, zeroing value\n",
425 panic_info->mph_other_log_len);
426 panic_info->mph_other_log_len = 0;
427 }
428 }
5ba3f43e
A
429
430 return;
431}