xref: /xnu-8796.121.2/pexpert/i386/pe_init.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
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 SECURITY_READ_ONLY_LATE(uint32_t) PE_srd_fused = 0;
57 
58 /* Clock Frequency Info */
59 clock_frequency_info_t gPEClockFrequencyInfo;
60 
61 void *gPEEFISystemTable;
62 void *gPEEFIRuntimeServices;
63 
64 static boot_icon_element* norootIcon_lzss;
65 static const uint8_t*     norootClut_lzss;
66 
67 int
PE_initialize_console(PE_Video * info,int op)68 PE_initialize_console( PE_Video * info, int op )
69 {
70 	static int   last_console = -1;
71 
72 	if (info) {
73 		info->v_offset  = 0;
74 		info->v_length  = 0;
75 		info->v_display = GRAPHICS_MODE;
76 	}
77 
78 	switch (op) {
79 	case kPEDisableScreen:
80 		initialize_screen(info, op);
81 		kprintf("kPEDisableScreen %d\n", last_console);
82 		if (!console_is_serial()) {
83 			last_console = switch_to_serial_console();
84 		}
85 		break;
86 
87 	case kPEEnableScreen:
88 		initialize_screen(info, op);
89 		if (info) {
90 			PE_state.video = *info;
91 		}
92 		kprintf("kPEEnableScreen %d\n", last_console);
93 		if (last_console != -1) {
94 			switch_to_old_console( last_console);
95 		}
96 		break;
97 
98 	case kPEBaseAddressChange:
99 		if (info) {
100 			PE_state.video = *info;
101 		}
102 		OS_FALLTHROUGH;
103 
104 	default:
105 		initialize_screen(info, op);
106 		break;
107 	}
108 
109 	return 0;
110 }
111 
112 void
PE_init_iokit(void)113 PE_init_iokit(void)
114 {
115 	enum { kMaxBootVar = 128 };
116 
117 	boolean_t bootClutInitialized = FALSE;
118 	boolean_t noroot_rle_Initialized = FALSE;
119 
120 	DTEntry             entry;
121 	unsigned int        size;
122 	uint32_t const      *map;
123 	boot_progress_element *bootPict;
124 
125 	norootIcon_lzss = NULL;
126 	norootClut_lzss = NULL;
127 
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 == SecureDTLookupEntry(NULL, "/chosen/memory-map", &entry)) {
137 		if (kSuccess == SecureDTGetProperty(entry, "BootCLUT", (void const **) &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 == SecureDTGetProperty(entry, "Pict-FailedBoot", (void const **) &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 == SecureDTGetProperty(entry, "FailedCLUT", (void const **) &map, &size)) {
155 			norootClut_lzss = (uint8_t*) ml_static_ptovirt(map[0]);
156 		}
157 
158 		if (kSuccess == SecureDTGetProperty(entry, "FailedImage", (void const **) &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 	/*
188 	 * x86 only minimally enforces lockdown in hardware.  Additionally, some pre-lockdown functionality
189 	 * such as commpage initialization requires IOKit enumeration of CPUs, which is heavily entangled
190 	 * with the ACPI stack.  Therefore, we start the IOKit matching process immediately on x86.
191 	 */
192 	InitIOKit(PE_state.deviceTreeHead);
193 	StartIOKitMatching();
194 }
195 
196 void
PE_lockdown_iokit(void)197 PE_lockdown_iokit(void)
198 {
199 	/* Ensure that at least the CPUs have been enumerated before moving forward. */
200 	ml_wait_max_cpus();
201 }
202 
203 void
PE_init_platform(boolean_t vm_initialized,void * _args)204 PE_init_platform(boolean_t vm_initialized, void * _args)
205 {
206 	boot_args *args = (boot_args *)_args;
207 
208 	if (PE_state.initialized == FALSE) {
209 		PE_state.initialized        = TRUE;
210 
211 		// New EFI-style
212 		PE_state.bootArgs           = _args;
213 		PE_state.deviceTreeHead     = (void *) ml_static_ptovirt(args->deviceTreeP);
214 		PE_state.deviceTreeSize     = args->deviceTreeLength;
215 		if (args->Video.v_baseAddr) {
216 			PE_state.video.v_baseAddr   = args->Video.v_baseAddr;// remains physical address
217 			PE_state.video.v_rowBytes   = args->Video.v_rowBytes;
218 			PE_state.video.v_depth      = args->Video.v_depth;
219 			PE_state.video.v_display    = args->Video.v_display;
220 			PE_state.video.v_rotate     = args->Video.v_rotate;
221 
222 			/* EFI doesn't have a good way of describing rotation internally,
223 			 * so it flips width and height in portrait mode. We flip it back. */
224 			if (PE_state.video.v_rotate == kDataRotate90 ||
225 			    PE_state.video.v_rotate == kDataRotate270) {
226 				PE_state.video.v_width  = args->Video.v_height;
227 				PE_state.video.v_height = args->Video.v_width;
228 			} else {
229 				PE_state.video.v_width  = args->Video.v_width;
230 				PE_state.video.v_height = args->Video.v_height;
231 			}
232 
233 			strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
234 			    sizeof(PE_state.video.v_pixelFormat));
235 		} else {
236 			PE_state.video.v_baseAddr   = args->VideoV1.v_baseAddr;// remains physical address
237 			PE_state.video.v_rowBytes   = args->VideoV1.v_rowBytes;
238 			PE_state.video.v_width      = args->VideoV1.v_width;
239 			PE_state.video.v_height     = args->VideoV1.v_height;
240 			PE_state.video.v_depth      = args->VideoV1.v_depth;
241 			PE_state.video.v_display    = args->VideoV1.v_display;
242 			PE_state.video.v_rotate     = kDataRotate0; /* no room for rotation info */
243 			strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
244 			    sizeof(PE_state.video.v_pixelFormat));
245 		}
246 
247 #ifdef  kBootArgsFlagHiDPI
248 		if (args->flags & kBootArgsFlagHiDPI) {
249 			PE_state.video.v_scale = kPEScaleFactor2x;
250 		} else {
251 			PE_state.video.v_scale = kPEScaleFactor1x;
252 		}
253 #else
254 		PE_state.video.v_scale = kPEScaleFactor1x;
255 #endif
256 	}
257 
258 	if (!vm_initialized) {
259 		if (PE_state.deviceTreeHead) {
260 			SecureDTInit(PE_state.deviceTreeHead, PE_state.deviceTreeSize);
261 		}
262 
263 		pe_identify_machine(args);
264 		pe_init_debug();
265 	}
266 }
267 
268 void
PE_create_console(void)269 PE_create_console( void )
270 {
271 	if (PE_state.video.v_display == GRAPHICS_MODE) {
272 		PE_initialize_console( &PE_state.video, kPEGraphicsMode );
273 	} else {
274 		PE_initialize_console( &PE_state.video, kPETextMode );
275 	}
276 }
277 
278 int
PE_current_console(PE_Video * info)279 PE_current_console( PE_Video * info )
280 {
281 	*info = PE_state.video;
282 
283 	return 0;
284 }
285 
286 void
PE_display_icon(__unused unsigned int flags,__unused const char * name)287 PE_display_icon( __unused unsigned int flags, __unused const char * name )
288 {
289 	if (norootIcon_lzss && norootClut_lzss) {
290 		uint32_t width  = norootIcon_lzss->width;
291 		uint32_t height = norootIcon_lzss->height;
292 		uint32_t x = ((PE_state.video.v_width  - width) / 2);
293 		uint32_t y = ((PE_state.video.v_height - height) / 2) + norootIcon_lzss->y_offset_from_center;
294 
295 		vc_display_lzss_icon(x, y, width, height,
296 		    &norootIcon_lzss->data[0],
297 		    norootIcon_lzss->data_size,
298 		    norootClut_lzss);
299 	} else if (default_noroot_data) {
300 		vc_display_icon( &default_noroot, default_noroot_data );
301 	} else {
302 		printf("ERROR: No data found for noroot icon!\n");
303 	}
304 }
305 
306 boolean_t
PE_get_hotkey(__unused unsigned char key)307 PE_get_hotkey(__unused unsigned char key)
308 {
309 	return FALSE;
310 }
311 
312 static timebase_callback_func gTimebaseCallback;
313 
314 void
PE_register_timebase_callback(timebase_callback_func callback)315 PE_register_timebase_callback(timebase_callback_func callback)
316 {
317 	gTimebaseCallback = callback;
318 
319 	PE_call_timebase_callback();
320 }
321 
322 void
PE_call_timebase_callback(void)323 PE_call_timebase_callback(void)
324 {
325 	struct timebase_freq_t timebase_freq;
326 	unsigned long          num, den, cnt;
327 
328 	num = gPEClockFrequencyInfo.bus_clock_rate_num * gPEClockFrequencyInfo.bus_to_dec_rate_num;
329 	den = gPEClockFrequencyInfo.bus_clock_rate_den * gPEClockFrequencyInfo.bus_to_dec_rate_den;
330 
331 	cnt = 2;
332 	while (cnt <= den) {
333 		if ((num % cnt) || (den % cnt)) {
334 			cnt++;
335 			continue;
336 		}
337 
338 		num /= cnt;
339 		den /= cnt;
340 	}
341 
342 	timebase_freq.timebase_num = num;
343 	timebase_freq.timebase_den = den;
344 
345 	if (gTimebaseCallback) {
346 		gTimebaseCallback(&timebase_freq);
347 	}
348 }
349 
350 /*
351  * The default (non-functional) PE_poll_input handler.
352  */
353 int
PE_stub_poll_input(__unused unsigned int options,char * c)354 PE_stub_poll_input(__unused unsigned int options, char * c)
355 {
356 	*c = 0xff;
357 	return 1; /* 0 for success, 1 for unsupported */
358 }
359 
360 boolean_t
PE_reboot_on_panic(void)361 PE_reboot_on_panic(void)
362 {
363 	boot_args *args = (boot_args *)PE_state.bootArgs;
364 
365 	if (args->flags & kBootArgsFlagRebootOnPanic) {
366 		return TRUE;
367 	} else {
368 		return FALSE;
369 	}
370 }
371 
372 void
PE_sync_panic_buffers(void)373 PE_sync_panic_buffers(void)
374 {
375 }
376 
377 /* rdar://problem/21244753 */
378 uint32_t
PE_i_can_has_debugger(uint32_t * debug_flags)379 PE_i_can_has_debugger(uint32_t *debug_flags)
380 {
381 #if DEVELOPMENT || DEBUG
382 	if (debug_flags) {
383 		assert(startup_phase >= STARTUP_SUB_TUNABLES);
384 	}
385 #endif
386 
387 #if CONFIG_CSR
388 	if (csr_check(CSR_ALLOW_KERNEL_DEBUGGER) != 0) {
389 		if (debug_flags) {
390 			*debug_flags = 0;
391 		}
392 		return FALSE;
393 	}
394 #endif
395 	if (debug_flags) {
396 		*debug_flags = debug_boot_arg;
397 	}
398 	return TRUE;
399 }
400 
401 uint32_t
PE_get_offset_into_panic_region(char * location)402 PE_get_offset_into_panic_region(char *location)
403 {
404 	assert(panic_info != NULL);
405 	assert(location > (char *) panic_info);
406 
407 	return (uint32_t) (location - (char *) panic_info);
408 }
409 
410 void
PE_init_panicheader()411 PE_init_panicheader()
412 {
413 	bzero(panic_info, offsetof(struct macos_panic_header, mph_data));
414 	panic_info->mph_panic_log_offset = PE_get_offset_into_panic_region(debug_buf_base);
415 
416 	panic_info->mph_magic = MACOS_PANIC_MAGIC;
417 	panic_info->mph_version = MACOS_PANIC_HEADER_CURRENT_VERSION;
418 
419 	return;
420 }
421 
422 /*
423  * Tries to update the panic header to keep it consistent on nested panics.
424  *
425  * NOTE: The purpose of this function is NOT to detect/correct corruption in the panic region,
426  *       it is to update the panic header to make it consistent when we nest panics.
427  *
428  * We try to avoid nested panics/asserts on x86 because they are difficult to debug, so log any
429  * inconsistencies we find.
430  */
431 void
PE_update_panicheader_nestedpanic()432 PE_update_panicheader_nestedpanic()
433 {
434 	/*
435 	 * if the panic header pointer is bogus (e.g. someone stomped on it) then bail.
436 	 */
437 	if (!panic_info) {
438 		/* if this happens in development then blow up bigly */
439 		assert(panic_info);
440 		return;
441 	}
442 
443 	/* If the panic log offset is not set, re-init the panic header */
444 	if (panic_info->mph_panic_log_offset == 0) {
445 		PE_init_panicheader();
446 		panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;
447 		return;
448 	}
449 
450 	panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;
451 
452 	/* Usually indicative of corruption in the panic region */
453 	if (!(((panic_info->mph_stackshot_offset == 0) && (panic_info->mph_stackshot_len == 0)) ||
454 	    ((panic_info->mph_stackshot_offset != 0) && (panic_info->mph_stackshot_len != 0)))) {
455 		kdb_printf("panic_info contains invalid stackshot metadata: mph_stackshot_offset 0x%x mph_stackshot_len 0x%x\n",
456 		    panic_info->mph_stackshot_offset, panic_info->mph_stackshot_len);
457 
458 		panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_INCOHERENT_PANICLOG;
459 	}
460 
461 	/*
462 	 * macOS panic logs contain nested panic data, if we've already closed the panic log,
463 	 * begin the other log.
464 	 */
465 	if ((panic_info->mph_panic_log_len != 0) && (panic_info->mph_other_log_offset == 0)) {
466 		panic_info->mph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);
467 
468 		/* Usually indicative of corruption in the panic region */
469 		if (panic_info->mph_other_log_len != 0) {
470 			kdb_printf("panic_info contains invalid other log metadata (zero offset but non-zero length), length was 0x%x, zeroing value\n",
471 			    panic_info->mph_other_log_len);
472 
473 			panic_info->mph_other_log_len = 0;
474 
475 			panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_INCOHERENT_PANICLOG;
476 		}
477 	}
478 
479 	return;
480 }
481 
482 vm_size_t
PE_init_socd_client(void)483 PE_init_socd_client(void)
484 {
485 	/* not implemented */
486 	return 0;
487 }
488 
489 void
PE_write_socd_client_buffer(__unused vm_offset_t offset,__unused const void * buff,__unused vm_size_t size)490 PE_write_socd_client_buffer(__unused vm_offset_t offset, __unused const void *buff, __unused vm_size_t size)
491 {
492 	/* not implemented */
493 }
494