]> git.saurik.com Git - apple/xnu.git/blob - pexpert/i386/pe_init.c
xnu-6153.141.1.tar.gz
[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 <console/video_console.h>
34 #include <sys/types.h>
35 #include <mach/vm_param.h>
36 #include <machine/machine_routines.h>
37 #include <pexpert/protos.h>
38 #include <pexpert/pexpert.h>
39 #include <pexpert/boot.h>
40 #include <pexpert/device_tree.h>
41 #include <pexpert/pe_images.h>
42 #include <kern/sched_prim.h>
43 #include <kern/debug.h>
44
45 #if CONFIG_CSR
46 #include <sys/csr.h>
47 #endif
48
49 #include "boot_images.h"
50
51 /* extern references */
52 extern void pe_identify_machine(void * args);
53 extern int kdb_printf(const char *format, ...) __printflike(1, 2);
54 /* private globals */
55 PE_state_t PE_state;
56
57 /* Clock Frequency Info */
58 clock_frequency_info_t gPEClockFrequencyInfo;
59
60 void *gPEEFISystemTable;
61 void *gPEEFIRuntimeServices;
62
63 static boot_icon_element* norootIcon_lzss;
64 static const uint8_t* norootClut_lzss;
65
66 int
67 PE_initialize_console( PE_Video * info, int op )
68 {
69 static int last_console = -1;
70
71 if (info) {
72 info->v_offset = 0;
73 info->v_length = 0;
74 info->v_display = GRAPHICS_MODE;
75 }
76
77 switch (op) {
78 case kPEDisableScreen:
79 initialize_screen(info, op);
80 kprintf("kPEDisableScreen %d\n", last_console);
81 if (!console_is_serial()) {
82 last_console = switch_to_serial_console();
83 }
84 break;
85
86 case kPEEnableScreen:
87 initialize_screen(info, op);
88 if (info) {
89 PE_state.video = *info;
90 }
91 kprintf("kPEEnableScreen %d\n", last_console);
92 if (last_console != -1) {
93 switch_to_old_console( last_console);
94 }
95 break;
96
97 case kPEBaseAddressChange:
98 if (info) {
99 PE_state.video = *info;
100 }
101 /* fall thru */
102
103 default:
104 initialize_screen(info, op);
105 break;
106 }
107
108 return 0;
109 }
110
111 void
112 PE_init_iokit(void)
113 {
114 enum { kMaxBootVar = 128 };
115
116 boolean_t bootClutInitialized = FALSE;
117 boolean_t noroot_rle_Initialized = FALSE;
118
119 DTEntry entry;
120 unsigned int size;
121 uint32_t *map;
122 boot_progress_element *bootPict;
123
124 norootIcon_lzss = NULL;
125 norootClut_lzss = NULL;
126
127 PE_init_kprintf(TRUE);
128 PE_init_printf(TRUE);
129
130 kprintf("Kernel boot args: '%s'\n", PE_boot_args());
131
132 /*
133 * Fetch the CLUT and the noroot image.
134 */
135
136 if (kSuccess == DTLookupEntry(NULL, "/chosen/memory-map", &entry)) {
137 if (kSuccess == DTGetProperty(entry, "BootCLUT", (void **) &map, &size)) {
138 if (sizeof(appleClut8) <= map[1]) {
139 bcopy((void *)ml_static_ptovirt(map[0]), appleClut8, sizeof(appleClut8));
140 bootClutInitialized = TRUE;
141 }
142 }
143
144 if (kSuccess == DTGetProperty(entry, "Pict-FailedBoot", (void **) &map, &size)) {
145 bootPict = (boot_progress_element *) ml_static_ptovirt(map[0]);
146 default_noroot.width = bootPict->width;
147 default_noroot.height = bootPict->height;
148 default_noroot.dx = 0;
149 default_noroot.dy = bootPict->yOffset;
150 default_noroot_data = &bootPict->data[0];
151 noroot_rle_Initialized = TRUE;
152 }
153
154 if (kSuccess == DTGetProperty(entry, "FailedCLUT", (void **) &map, &size)) {
155 norootClut_lzss = (uint8_t*) ml_static_ptovirt(map[0]);
156 }
157
158 if (kSuccess == DTGetProperty(entry, "FailedImage", (void **) &map, &size)) {
159 norootIcon_lzss = (boot_icon_element *) ml_static_ptovirt(map[0]);
160 if (norootClut_lzss == NULL) {
161 printf("ERROR: No FailedCLUT provided for noroot icon!\n");
162 }
163 }
164 }
165
166 if (!bootClutInitialized) {
167 bcopy((void *) (uintptr_t) bootClut, (void *) appleClut8, sizeof(appleClut8));
168 }
169
170 if (!noroot_rle_Initialized) {
171 default_noroot.width = kFailedBootWidth;
172 default_noroot.height = kFailedBootHeight;
173 default_noroot.dx = 0;
174 default_noroot.dy = kFailedBootOffset;
175 default_noroot_data = failedBootPict;
176 }
177
178 /*
179 * Initialize the spinning wheel (progress indicator).
180 */
181 vc_progress_initialize(&default_progress,
182 default_progress_data1x,
183 default_progress_data2x,
184 default_progress_data3x,
185 (unsigned char *) appleClut8);
186
187 StartIOKit( PE_state.deviceTreeHead, PE_state.bootArgs, gPEEFIRuntimeServices, NULL);
188 }
189
190 void
191 PE_init_platform(boolean_t vm_initialized, void * _args)
192 {
193 boot_args *args = (boot_args *)_args;
194
195 if (PE_state.initialized == FALSE) {
196 PE_state.initialized = TRUE;
197
198 // New EFI-style
199 PE_state.bootArgs = _args;
200 PE_state.deviceTreeHead = (void *) ml_static_ptovirt(args->deviceTreeP);
201 if (args->Video.v_baseAddr) {
202 PE_state.video.v_baseAddr = args->Video.v_baseAddr;// remains physical address
203 PE_state.video.v_rowBytes = args->Video.v_rowBytes;
204 PE_state.video.v_depth = args->Video.v_depth;
205 PE_state.video.v_display = args->Video.v_display;
206 PE_state.video.v_rotate = args->Video.v_rotate;
207
208 /* EFI doesn't have a good way of describing rotation internally,
209 * so it flips width and height in portrait mode. We flip it back. */
210 if (PE_state.video.v_rotate == kDataRotate90 ||
211 PE_state.video.v_rotate == kDataRotate270) {
212 PE_state.video.v_width = args->Video.v_height;
213 PE_state.video.v_height = args->Video.v_width;
214 } else {
215 PE_state.video.v_width = args->Video.v_width;
216 PE_state.video.v_height = args->Video.v_height;
217 }
218
219 strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
220 sizeof(PE_state.video.v_pixelFormat));
221 } else {
222 PE_state.video.v_baseAddr = args->VideoV1.v_baseAddr;// remains physical address
223 PE_state.video.v_rowBytes = args->VideoV1.v_rowBytes;
224 PE_state.video.v_width = args->VideoV1.v_width;
225 PE_state.video.v_height = args->VideoV1.v_height;
226 PE_state.video.v_depth = args->VideoV1.v_depth;
227 PE_state.video.v_display = args->VideoV1.v_display;
228 PE_state.video.v_rotate = kDataRotate0; /* no room for rotation info */
229 strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
230 sizeof(PE_state.video.v_pixelFormat));
231 }
232
233 #ifdef kBootArgsFlagHiDPI
234 if (args->flags & kBootArgsFlagHiDPI) {
235 PE_state.video.v_scale = kPEScaleFactor2x;
236 } else {
237 PE_state.video.v_scale = kPEScaleFactor1x;
238 }
239 #else
240 PE_state.video.v_scale = kPEScaleFactor1x;
241 #endif
242 }
243
244 if (!vm_initialized) {
245 if (PE_state.deviceTreeHead) {
246 DTInit(PE_state.deviceTreeHead);
247 }
248
249 pe_identify_machine(args);
250 pe_init_debug();
251 }
252 }
253
254 void
255 PE_create_console( void )
256 {
257 if (PE_state.video.v_display == GRAPHICS_MODE) {
258 PE_initialize_console( &PE_state.video, kPEGraphicsMode );
259 } else {
260 PE_initialize_console( &PE_state.video, kPETextMode );
261 }
262 }
263
264 int
265 PE_current_console( PE_Video * info )
266 {
267 *info = PE_state.video;
268
269 return 0;
270 }
271
272 void
273 PE_display_icon( __unused unsigned int flags, __unused const char * name )
274 {
275 if (norootIcon_lzss && norootClut_lzss) {
276 uint32_t width = norootIcon_lzss->width;
277 uint32_t height = norootIcon_lzss->height;
278 uint32_t x = ((PE_state.video.v_width - width) / 2);
279 uint32_t y = ((PE_state.video.v_height - height) / 2) + norootIcon_lzss->y_offset_from_center;
280
281 vc_display_lzss_icon(x, y, width, height,
282 &norootIcon_lzss->data[0],
283 norootIcon_lzss->data_size,
284 norootClut_lzss);
285 } else if (default_noroot_data) {
286 vc_display_icon( &default_noroot, default_noroot_data );
287 } else {
288 printf("ERROR: No data found for noroot icon!\n");
289 }
290 }
291
292 boolean_t
293 PE_get_hotkey(__unused unsigned char key)
294 {
295 return FALSE;
296 }
297
298 static timebase_callback_func gTimebaseCallback;
299
300 void
301 PE_register_timebase_callback(timebase_callback_func callback)
302 {
303 gTimebaseCallback = callback;
304
305 PE_call_timebase_callback();
306 }
307
308 void
309 PE_call_timebase_callback(void)
310 {
311 struct timebase_freq_t timebase_freq;
312 unsigned long num, den, cnt;
313
314 num = gPEClockFrequencyInfo.bus_clock_rate_num * gPEClockFrequencyInfo.bus_to_dec_rate_num;
315 den = gPEClockFrequencyInfo.bus_clock_rate_den * gPEClockFrequencyInfo.bus_to_dec_rate_den;
316
317 cnt = 2;
318 while (cnt <= den) {
319 if ((num % cnt) || (den % cnt)) {
320 cnt++;
321 continue;
322 }
323
324 num /= cnt;
325 den /= cnt;
326 }
327
328 timebase_freq.timebase_num = num;
329 timebase_freq.timebase_den = den;
330
331 if (gTimebaseCallback) {
332 gTimebaseCallback(&timebase_freq);
333 }
334 }
335
336 /*
337 * The default (non-functional) PE_poll_input handler.
338 */
339 int
340 PE_stub_poll_input(__unused unsigned int options, char * c)
341 {
342 *c = 0xff;
343 return 1; /* 0 for success, 1 for unsupported */
344 }
345
346 boolean_t
347 PE_reboot_on_panic(void)
348 {
349 boot_args *args = (boot_args *)PE_state.bootArgs;
350
351 if (args->flags & kBootArgsFlagRebootOnPanic) {
352 return TRUE;
353 } else {
354 return FALSE;
355 }
356 }
357
358 void
359 PE_sync_panic_buffers(void)
360 {
361 }
362
363 /* rdar://problem/21244753 */
364 uint32_t
365 PE_i_can_has_debugger(uint32_t *debug_flags)
366 {
367 #if DEVELOPMENT || DEBUG
368 if (debug_flags) {
369 assert(debug_boot_arg_inited);
370 }
371 #endif
372
373 #if CONFIG_CSR
374 if (csr_check(CSR_ALLOW_KERNEL_DEBUGGER) != 0) {
375 if (debug_flags) {
376 *debug_flags = 0;
377 }
378 return FALSE;
379 }
380 #endif
381 if (debug_flags) {
382 *debug_flags = debug_boot_arg;
383 }
384 return TRUE;
385 }
386
387 uint32_t
388 PE_get_offset_into_panic_region(char *location)
389 {
390 assert(panic_info != NULL);
391 assert(location > (char *) panic_info);
392
393 return (uint32_t) (location - (char *) panic_info);
394 }
395
396 void
397 PE_init_panicheader()
398 {
399 bzero(panic_info, offsetof(struct macos_panic_header, mph_data));
400 panic_info->mph_panic_log_offset = PE_get_offset_into_panic_region(debug_buf_base);
401
402 panic_info->mph_magic = MACOS_PANIC_MAGIC;
403 panic_info->mph_version = MACOS_PANIC_HEADER_CURRENT_VERSION;
404
405 return;
406 }
407
408 /*
409 * Tries to update the panic header to keep it consistent on nested panics.
410 *
411 * NOTE: The purpose of this function is NOT to detect/correct corruption in the panic region,
412 * it is to update the panic header to make it consistent when we nest panics.
413 *
414 * We try to avoid nested panics/asserts on x86 because they are difficult to debug, so log any
415 * inconsistencies we find.
416 */
417 void
418 PE_update_panicheader_nestedpanic()
419 {
420 /* If the panic log offset is not set, re-init the panic header */
421 if (panic_info->mph_panic_log_offset == 0) {
422 PE_init_panicheader();
423 panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;
424 return;
425 }
426
427 panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;
428
429 /* Usually indicative of corruption in the panic region */
430 if (!(((panic_info->mph_stackshot_offset == 0) && (panic_info->mph_stackshot_len == 0)) ||
431 ((panic_info->mph_stackshot_offset != 0) && (panic_info->mph_stackshot_len != 0)))) {
432 kdb_printf("panic_info contains invalid stackshot metadata: mph_stackshot_offset 0x%x mph_stackshot_len 0x%x\n",
433 panic_info->mph_stackshot_offset, panic_info->mph_stackshot_len);
434 }
435
436 /*
437 * macOS panic logs contain nested panic data, if we've already closed the panic log,
438 * begin the other log.
439 */
440 if ((panic_info->mph_panic_log_len != 0) && (panic_info->mph_other_log_offset == 0)) {
441 panic_info->mph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
442
443 /* Usually indicative of corruption in the panic region */
444 if (panic_info->mph_other_log_len != 0) {
445 kdb_printf("panic_info contains invalid other log metadata (zero offset but non-zero length), length was 0x%x, zeroing value\n",
446 panic_info->mph_other_log_len);
447 panic_info->mph_other_log_len = 0;
448 }
449 }
450
451 return;
452 }