Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/*
 * file: pe_init.c
 *    i386 platform expert initialization.
 */

#include <console/video_console.h>
#include <sys/types.h>
#include <mach/vm_param.h>
#include <machine/machine_routines.h>
#include <pexpert/protos.h>
#include <pexpert/pexpert.h>
#include <pexpert/boot.h>
#include <pexpert/device_tree.h>
#include <pexpert/pe_images.h>
#include <kern/sched_prim.h>
#include <kern/debug.h>

#if CONFIG_CSR
#include <sys/csr.h>
#endif

#include "boot_images.h"

/* extern references */
extern void pe_identify_machine(void * args);
extern int kdb_printf(const char *format, ...) __printflike(1, 2);
/* private globals */
PE_state_t  PE_state;

/* Clock Frequency Info */
clock_frequency_info_t gPEClockFrequencyInfo;

void *gPEEFISystemTable;
void *gPEEFIRuntimeServices;

static boot_icon_element* norootIcon_lzss;
static const uint8_t*     norootClut_lzss;

int
PE_initialize_console( PE_Video * info, int op )
{
	static int   last_console = -1;

	if (info) {
		info->v_offset  = 0;
		info->v_length  = 0;
		info->v_display = GRAPHICS_MODE;
	}

	switch (op) {
	case kPEDisableScreen:
		initialize_screen(info, op);
		kprintf("kPEDisableScreen %d\n", last_console);
		if (!console_is_serial()) {
			last_console = switch_to_serial_console();
		}
		break;

	case kPEEnableScreen:
		initialize_screen(info, op);
		if (info) {
			PE_state.video = *info;
		}
		kprintf("kPEEnableScreen %d\n", last_console);
		if (last_console != -1) {
			switch_to_old_console( last_console);
		}
		break;

	case kPEBaseAddressChange:
		if (info) {
			PE_state.video = *info;
		}
		OS_FALLTHROUGH;

	default:
		initialize_screen(info, op);
		break;
	}

	return 0;
}

void
PE_init_iokit(void)
{
	enum { kMaxBootVar = 128 };

	boolean_t bootClutInitialized = FALSE;
	boolean_t noroot_rle_Initialized = FALSE;

	DTEntry             entry;
	unsigned int        size;
	uint32_t const      *map;
	boot_progress_element *bootPict;

	norootIcon_lzss = NULL;
	norootClut_lzss = NULL;

	PE_init_printf(TRUE);

	kprintf("Kernel boot args: '%s'\n", PE_boot_args());

	/*
	 * Fetch the CLUT and the noroot image.
	 */

	if (kSuccess == SecureDTLookupEntry(NULL, "/chosen/memory-map", &entry)) {
		if (kSuccess == SecureDTGetProperty(entry, "BootCLUT", (void const **) &map, &size)) {
			if (sizeof(appleClut8) <= map[1]) {
				bcopy((void *)ml_static_ptovirt(map[0]), appleClut8, sizeof(appleClut8));
				bootClutInitialized = TRUE;
			}
		}

		if (kSuccess == SecureDTGetProperty(entry, "Pict-FailedBoot", (void const **) &map, &size)) {
			bootPict = (boot_progress_element *) ml_static_ptovirt(map[0]);
			default_noroot.width  = bootPict->width;
			default_noroot.height = bootPict->height;
			default_noroot.dx     = 0;
			default_noroot.dy     = bootPict->yOffset;
			default_noroot_data   = &bootPict->data[0];
			noroot_rle_Initialized = TRUE;
		}

		if (kSuccess == SecureDTGetProperty(entry, "FailedCLUT", (void const **) &map, &size)) {
			norootClut_lzss = (uint8_t*) ml_static_ptovirt(map[0]);
		}

		if (kSuccess == SecureDTGetProperty(entry, "FailedImage", (void const **) &map, &size)) {
			norootIcon_lzss = (boot_icon_element *) ml_static_ptovirt(map[0]);
			if (norootClut_lzss == NULL) {
				printf("ERROR: No FailedCLUT provided for noroot icon!\n");
			}
		}
	}

	if (!bootClutInitialized) {
		bcopy((void *) (uintptr_t) bootClut, (void *) appleClut8, sizeof(appleClut8));
	}

	if (!noroot_rle_Initialized) {
		default_noroot.width  = kFailedBootWidth;
		default_noroot.height = kFailedBootHeight;
		default_noroot.dx     = 0;
		default_noroot.dy     = kFailedBootOffset;
		default_noroot_data   = failedBootPict;
	}

	/*
	 * Initialize the spinning wheel (progress indicator).
	 */
	vc_progress_initialize(&default_progress,
	    default_progress_data1x,
	    default_progress_data2x,
	    default_progress_data3x,
	    (unsigned char *) appleClut8);

	/*
	 * x86 only minimally enforces lockdown in hardware.  Additionally, some pre-lockdown functionality
	 * such as commpage initialization requires IOKit enumeration of CPUs, which is heavily entangled
	 * with the ACPI stack.  Therefore, we start the IOKit matching process immediately on x86.
	 */
	InitIOKit(PE_state.deviceTreeHead);
	StartIOKitMatching();
}

void
PE_lockdown_iokit(void)
{
	/* Ensure that at least the CPUs have been enumerated before moving forward. */
	ml_wait_max_cpus();
}

void
PE_init_platform(boolean_t vm_initialized, void * _args)
{
	boot_args *args = (boot_args *)_args;

	if (PE_state.initialized == FALSE) {
		PE_state.initialized        = TRUE;

		// New EFI-style
		PE_state.bootArgs           = _args;
		PE_state.deviceTreeHead     = (void *) ml_static_ptovirt(args->deviceTreeP);
		PE_state.deviceTreeSize     = args->deviceTreeLength;
		if (args->Video.v_baseAddr) {
			PE_state.video.v_baseAddr   = args->Video.v_baseAddr;// remains physical address
			PE_state.video.v_rowBytes   = args->Video.v_rowBytes;
			PE_state.video.v_depth      = args->Video.v_depth;
			PE_state.video.v_display    = args->Video.v_display;
			PE_state.video.v_rotate     = args->Video.v_rotate;

			/* EFI doesn't have a good way of describing rotation internally,
			 * so it flips width and height in portrait mode. We flip it back. */
			if (PE_state.video.v_rotate == kDataRotate90 ||
			    PE_state.video.v_rotate == kDataRotate270) {
				PE_state.video.v_width  = args->Video.v_height;
				PE_state.video.v_height = args->Video.v_width;
			} else {
				PE_state.video.v_width  = args->Video.v_width;
				PE_state.video.v_height = args->Video.v_height;
			}

			strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
			    sizeof(PE_state.video.v_pixelFormat));
		} else {
			PE_state.video.v_baseAddr   = args->VideoV1.v_baseAddr;// remains physical address
			PE_state.video.v_rowBytes   = args->VideoV1.v_rowBytes;
			PE_state.video.v_width      = args->VideoV1.v_width;
			PE_state.video.v_height     = args->VideoV1.v_height;
			PE_state.video.v_depth      = args->VideoV1.v_depth;
			PE_state.video.v_display    = args->VideoV1.v_display;
			PE_state.video.v_rotate     = kDataRotate0; /* no room for rotation info */
			strlcpy(PE_state.video.v_pixelFormat, "PPPPPPPP",
			    sizeof(PE_state.video.v_pixelFormat));
		}

#ifdef  kBootArgsFlagHiDPI
		if (args->flags & kBootArgsFlagHiDPI) {
			PE_state.video.v_scale = kPEScaleFactor2x;
		} else {
			PE_state.video.v_scale = kPEScaleFactor1x;
		}
#else
		PE_state.video.v_scale = kPEScaleFactor1x;
#endif
	}

	if (!vm_initialized) {
		if (PE_state.deviceTreeHead) {
			SecureDTInit(PE_state.deviceTreeHead, PE_state.deviceTreeSize);
		}

		pe_identify_machine(args);
		pe_init_debug();
	}
}

void
PE_create_console( void )
{
	if (PE_state.video.v_display == GRAPHICS_MODE) {
		PE_initialize_console( &PE_state.video, kPEGraphicsMode );
	} else {
		PE_initialize_console( &PE_state.video, kPETextMode );
	}
}

int
PE_current_console( PE_Video * info )
{
	*info = PE_state.video;

	return 0;
}

void
PE_display_icon( __unused unsigned int flags, __unused const char * name )
{
	if (norootIcon_lzss && norootClut_lzss) {
		uint32_t width  = norootIcon_lzss->width;
		uint32_t height = norootIcon_lzss->height;
		uint32_t x = ((PE_state.video.v_width  - width) / 2);
		uint32_t y = ((PE_state.video.v_height - height) / 2) + norootIcon_lzss->y_offset_from_center;

		vc_display_lzss_icon(x, y, width, height,
		    &norootIcon_lzss->data[0],
		    norootIcon_lzss->data_size,
		    norootClut_lzss);
	} else if (default_noroot_data) {
		vc_display_icon( &default_noroot, default_noroot_data );
	} else {
		printf("ERROR: No data found for noroot icon!\n");
	}
}

boolean_t
PE_get_hotkey(__unused unsigned char key)
{
	return FALSE;
}

static timebase_callback_func gTimebaseCallback;

void
PE_register_timebase_callback(timebase_callback_func callback)
{
	gTimebaseCallback = callback;

	PE_call_timebase_callback();
}

void
PE_call_timebase_callback(void)
{
	struct timebase_freq_t timebase_freq;
	unsigned long          num, den, cnt;

	num = gPEClockFrequencyInfo.bus_clock_rate_num * gPEClockFrequencyInfo.bus_to_dec_rate_num;
	den = gPEClockFrequencyInfo.bus_clock_rate_den * gPEClockFrequencyInfo.bus_to_dec_rate_den;

	cnt = 2;
	while (cnt <= den) {
		if ((num % cnt) || (den % cnt)) {
			cnt++;
			continue;
		}

		num /= cnt;
		den /= cnt;
	}

	timebase_freq.timebase_num = num;
	timebase_freq.timebase_den = den;

	if (gTimebaseCallback) {
		gTimebaseCallback(&timebase_freq);
	}
}

/*
 * The default (non-functional) PE_poll_input handler.
 */
int
PE_stub_poll_input(__unused unsigned int options, char * c)
{
	*c = 0xff;
	return 1; /* 0 for success, 1 for unsupported */
}

boolean_t
PE_reboot_on_panic(void)
{
	boot_args *args = (boot_args *)PE_state.bootArgs;

	if (args->flags & kBootArgsFlagRebootOnPanic) {
		return TRUE;
	} else {
		return FALSE;
	}
}

void
PE_sync_panic_buffers(void)
{
}

/* rdar://problem/21244753 */
uint32_t
PE_i_can_has_debugger(uint32_t *debug_flags)
{
#if DEVELOPMENT || DEBUG
	if (debug_flags) {
		assert(startup_phase >= STARTUP_SUB_TUNABLES);
	}
#endif

#if CONFIG_CSR
	if (csr_check(CSR_ALLOW_KERNEL_DEBUGGER) != 0) {
		if (debug_flags) {
			*debug_flags = 0;
		}
		return FALSE;
	}
#endif
	if (debug_flags) {
		*debug_flags = debug_boot_arg;
	}
	return TRUE;
}

uint32_t
PE_get_offset_into_panic_region(char *location)
{
	assert(panic_info != NULL);
	assert(location > (char *) panic_info);

	return (uint32_t) (location - (char *) panic_info);
}

void
PE_init_panicheader()
{
	bzero(panic_info, offsetof(struct macos_panic_header, mph_data));
	panic_info->mph_panic_log_offset = PE_get_offset_into_panic_region(debug_buf_base);

	panic_info->mph_magic = MACOS_PANIC_MAGIC;
	panic_info->mph_version = MACOS_PANIC_HEADER_CURRENT_VERSION;

	return;
}

/*
 * Tries to update the panic header to keep it consistent on nested panics.
 *
 * NOTE: The purpose of this function is NOT to detect/correct corruption in the panic region,
 *       it is to update the panic header to make it consistent when we nest panics.
 *
 * We try to avoid nested panics/asserts on x86 because they are difficult to debug, so log any
 * inconsistencies we find.
 */
void
PE_update_panicheader_nestedpanic()
{
	/* If the panic log offset is not set, re-init the panic header */
	if (panic_info->mph_panic_log_offset == 0) {
		PE_init_panicheader();
		panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;
		return;
	}

	panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_NESTED_PANIC;

	/* Usually indicative of corruption in the panic region */
	if (!(((panic_info->mph_stackshot_offset == 0) && (panic_info->mph_stackshot_len == 0)) ||
	    ((panic_info->mph_stackshot_offset != 0) && (panic_info->mph_stackshot_len != 0)))) {
		kdb_printf("panic_info contains invalid stackshot metadata: mph_stackshot_offset 0x%x mph_stackshot_len 0x%x\n",
		    panic_info->mph_stackshot_offset, panic_info->mph_stackshot_len);
	}

	/*
	 * macOS panic logs contain nested panic data, if we've already closed the panic log,
	 * begin the other log.
	 */
	if ((panic_info->mph_panic_log_len != 0) && (panic_info->mph_other_log_offset == 0)) {
		panic_info->mph_other_log_offset = PE_get_offset_into_panic_region(debug_buf_ptr);

		/* Usually indicative of corruption in the panic region */
		if (panic_info->mph_other_log_len != 0) {
			kdb_printf("panic_info contains invalid other log metadata (zero offset but non-zero length), length was 0x%x, zeroing value\n",
			    panic_info->mph_other_log_len);
			panic_info->mph_other_log_len = 0;
		}
	}

	return;
}