1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | *␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License").␉You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | *␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | *␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | ␊ |
25 | /*␊ |
26 | * Mach Operating System␊ |
27 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
28 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
29 | * All rights reserved.␉ The CMU software License Agreement specifies␊ |
30 | * the terms and conditions for use and redistribution.␊ |
31 | *␊ |
32 | * INTEL CORPORATION PROPRIETARY INFORMATION␊ |
33 | *␊ |
34 | * This software is supplied under the terms of a license agreement or␊ |
35 | * nondisclosure agreement with Intel Corporation and may not be copied␊ |
36 | * nor disclosed except in accordance with the terms of that agreement.␊ |
37 | *␊ |
38 | *␉Copyright 1988, 1989 by Intel Corporation␊ |
39 | *␊ |
40 | *␊ |
41 | * Copyright 1993 NeXT Computer, Inc.␊ |
42 | * All rights reserved.␊ |
43 | *␊ |
44 | * Completely reworked by Sam Streeper (sam_s@NeXT.com)␊ |
45 | * Reworked again by Curtis Galloway (galloway@NeXT.com)␊ |
46 | */␊ |
47 | ␊ |
48 | #include "boot.h"␊ |
49 | #include "bootstruct.h"␊ |
50 | #include "fake_efi.h"␊ |
51 | #include "sl.h"␊ |
52 | #include "libsa.h"␊ |
53 | #include "ramdisk.h"␊ |
54 | #include "gui.h"␊ |
55 | #include "platform.h"␊ |
56 | #include "modules.h"␊ |
57 | #include "device_tree.h"␊ |
58 | ␊ |
59 | #ifndef DEBUG_BOOT2␊ |
60 | ␉#define DEBUG_BOOT2 0␊ |
61 | #endif␊ |
62 | ␊ |
63 | #ifndef DEBUG_INTERRUPTS␊ |
64 | ␉#define DEBUG_INTERRUPTS 0␊ |
65 | #endif␊ |
66 | ␊ |
67 | #if DEBUG_BOOT2␊ |
68 | ␉#define DBG(x...)␉printf(x)␊ |
69 | #else␊ |
70 | ␉#define DBG(x...)␉msglog(x)␊ |
71 | #endif␊ |
72 | ␊ |
73 | /*␊ |
74 | * How long to wait (in seconds) to load the␊ |
75 | * kernel after displaying the "boot:" prompt.␊ |
76 | */␊ |
77 | #define kBootErrorTimeout 5␊ |
78 | ␊ |
79 | bool␉␉gOverrideKernel;␊ |
80 | bool␉␉gEnableCDROMRescan;␊ |
81 | bool␉␉gScanSingleDrive;␊ |
82 | bool␉␉useGUI;␊ |
83 | ␊ |
84 | #if DEBUG_INTERRUPTS␊ |
85 | ␉static int␉interruptsAvailable = 0;␊ |
86 | #endif␊ |
87 | ␊ |
88 | static bool␉gUnloadPXEOnExit = false;␊ |
89 | ␊ |
90 | static char␉gCacheNameAdler[64 + 256];␊ |
91 | char␉␉*gPlatformName = gCacheNameAdler;␊ |
92 | ␊ |
93 | char␉␉gRootDevice[ROOT_DEVICE_SIZE];␊ |
94 | char␉␉gMKextName[512];␊ |
95 | int␉␉bvCount = 0;␊ |
96 | int␉␉gDeviceCount = 0;␊ |
97 | //int␉␉menucount = 0;␊ |
98 | long␉␉gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
99 | ␊ |
100 | BVRef␉␉bvr;␊ |
101 | BVRef␉␉menuBVR;␊ |
102 | BVRef␉␉bvChain;␊ |
103 | ␊ |
104 | static unsigned long␉Adler32(unsigned char *buffer, long length);␊ |
105 | //static void␉␉␉selectBiosDevice(void);␊ |
106 | ␊ |
107 | /** options.c **/␊ |
108 | extern char *msgbuf;␊ |
109 | void showTextBuffer(char *buf, int size);␊ |
110 | ␊ |
111 | //==========================================================================␊ |
112 | // Zero the BSS.␊ |
113 | ␊ |
114 | static void zeroBSS(void)␊ |
115 | {␊ |
116 | ␉extern char bss_start __asm("section$start$__DATA$__bss");␊ |
117 | ␉extern char bss_end __asm("section$end$__DATA$__bss");␊ |
118 | ␉extern char common_start __asm("section$start$__DATA$__common");␊ |
119 | ␉extern char common_end __asm("section$end$__DATA$__common");␊ |
120 | ␊ |
121 | ␉bzero(&bss_start, (&bss_end - &bss_start));␊ |
122 | ␉bzero(&common_start, (&common_end - &common_start));␊ |
123 | }␊ |
124 | ␊ |
125 | //==========================================================================␊ |
126 | // Malloc error function␊ |
127 | ␊ |
128 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
129 | {␊ |
130 | ␉stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",␊ |
131 | ␉␉␉␉␉␉␉␉␉ (unsigned)addr, (unsigned)size, file, line);␊ |
132 | }␊ |
133 | ␊ |
134 | //==========================================================================␊ |
135 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
136 | //␊ |
137 | void initialize_runtime(void)␊ |
138 | {␊ |
139 | ␉zeroBSS();␊ |
140 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
141 | }␊ |
142 | ␊ |
143 | // =========================================================================␊ |
144 | ␊ |
145 | ␊ |
146 | ␊ |
147 | //==========================================================================␊ |
148 | // ExecKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
149 | ␊ |
150 | static int ExecKernel(void *binary)␊ |
151 | {␊ |
152 | ␉int␉␉␉ret;␊ |
153 | ␉entry_t␉␉kernelEntry;␊ |
154 | ␊ |
155 | ␉bootArgs->kaddr = bootArgs->ksize = 0;␊ |
156 | ␊ |
157 | // ===============================================================================␊ |
158 | ␊ |
159 | ␉gMacOSVersion[0] = 0;␊ |
160 | ␉// TODO identify sierra as macOS␊ |
161 | ␉verbose("Booting on %s %s (%s)\n", (MacOSVerCurrent < MacOSVer2Int("10.8")) ? "Mac OS X" : "OS X", gBootVolume->OSFullVer, gBootVolume->OSBuildVer );␊ |
162 | ␊ |
163 | ␉setupBooterArgs();␊ |
164 | ␊ |
165 | // ===============================================================================␊ |
166 | ␊ |
167 | ␉execute_hook("ExecKernel", (void *)binary, NULL, NULL, NULL);␊ |
168 | ␊ |
169 | ␉ret = DecodeKernel(binary,␊ |
170 | ␉␉␉␉␉ &kernelEntry,␊ |
171 | ␉␉␉␉␉ (char **) &bootArgs->kaddr,␊ |
172 | ␉␉␉␉␉ (int *)&bootArgs->ksize);␊ |
173 | ␊ |
174 | ␉if ( ret != 0 )␊ |
175 | ␉{␊ |
176 | ␉␉printf("Decoding kernel failed.\n");␊ |
177 | ␉␉pause();␊ |
178 | ␉␉return ret;␊ |
179 | ␉}␊ |
180 | ␊ |
181 | ␉// Reserve space for boot args␊ |
182 | ␉reserveKernBootStruct();␊ |
183 | ␊ |
184 | ␉// Notify modules that the kernel has been decoded␊ |
185 | ␉execute_hook("DecodedKernel", (void *)binary, (void *)bootArgs->kaddr, (void *)bootArgs->ksize, NULL);␊ |
186 | ␊ |
187 | ␉setupFakeEfi();␊ |
188 | ␊ |
189 | ␉// Load boot drivers from the specifed root path.␊ |
190 | ␉//if (!gHaveKernelCache)␊ |
191 | ␉{␊ |
192 | ␉␉LoadDrivers("/");␊ |
193 | ␉}␊ |
194 | ␊ |
195 | ␉execute_hook("DriversLoaded", (void *)binary, NULL, NULL, NULL);␊ |
196 | ␊ |
197 | ␉clearActivityIndicator();␊ |
198 | ␊ |
199 | ␉if (gErrors)␊ |
200 | ␉{␊ |
201 | ␉␉printf("Errors encountered while starting up the computer.\n");␊ |
202 | ␉␉printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
203 | ␉␉sleep(kBootErrorTimeout);␊ |
204 | ␉}␊ |
205 | ␊ |
206 | ␉md0Ramdisk();␊ |
207 | ␊ |
208 | ␉// Cleanup the PXE base code.␊ |
209 | ␊ |
210 | ␉if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )␊ |
211 | ␉{␊ |
212 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
213 | ␉␉{␊ |
214 | ␉␉␉printf("nbpUnloadBaseCode error %d\n", (int)ret);␊ |
215 | ␉␉␉sleep(2);␊ |
216 | ␉␉}␊ |
217 | ␉}␊ |
218 | ␊ |
219 | ␉bool dummyVal;␊ |
220 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal)␊ |
221 | ␉{␊ |
222 | ␉␉showTextBuffer(msgbuf, strlen(msgbuf));␊ |
223 | ␉}␊ |
224 | ␊ |
225 | ␉usb_loop();␊ |
226 | ␊ |
227 | #if DEBUG␊ |
228 | ␉if (interruptsAvailable) ShowInterruptCounters();␊ |
229 | #endif␊ |
230 | ␊ |
231 | ␉// If we were in text mode, switch to graphics mode.␊ |
232 | ␉// This will draw the boot graphics unless we are in␊ |
233 | ␉// verbose mode.␊ |
234 | ␉if (gVerboseMode)␊ |
235 | ␉{␊ |
236 | ␉␉setVideoMode(GRAPHICS_MODE, 0);␊ |
237 | ␉}␊ |
238 | ␉else␊ |
239 | ␉{␊ |
240 | ␉␉drawBootGraphics();␊ |
241 | ␉}␊ |
242 | ␊ |
243 | ␉DBG("Starting Darwin/%s [%s]\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64", gDarwinBuildVerStr);␊ |
244 | ␉DBG("Boot Args: %s\n", bootArgs->CommandLine);␊ |
245 | ␊ |
246 | ␉setupBooterLog();␊ |
247 | ␊ |
248 | ␉finalizeBootStruct();␊ |
249 | ␊ |
250 | ␉// Jump to kernel's entry point. There's no going back now.␊ |
251 | ␉if ( MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
252 | ␉{␊ |
253 | ␉␉// Notify modules that the kernel is about to be started␊ |
254 | ␉␉execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgsLegacy, NULL, NULL);␊ |
255 | ␊ |
256 | ␉␉startprog( kernelEntry, bootArgsLegacy );␊ |
257 | ␉}␊ |
258 | ␉else␊ |
259 | ␉{␊ |
260 | ␉␉// Notify modules that the kernel is about to be started␊ |
261 | ␉␉execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgs, NULL, NULL);␊ |
262 | ␊ |
263 | ␉␉// Masking out so that Lion doesn't doublefault␊ |
264 | ␉␉outb(0x21, 0xff);␉/* Maskout all interrupts Pic1 */␊ |
265 | ␉␉outb(0xa1, 0xff);␉/* Maskout all interrupts Pic2 */␊ |
266 | ␊ |
267 | ␉␉startprog( kernelEntry, bootArgs );␊ |
268 | ␉}␊ |
269 | ␊ |
270 | ␉// Not reached␊ |
271 | ␉__builtin_unreachable();␊ |
272 | }␊ |
273 | ␊ |
274 | //==========================================================================␊ |
275 | // LoadKernelCache - Try to load Kernel Cache.␊ |
276 | // return the length of the loaded cache file or -1 on error␊ |
277 | long LoadKernelCache(const char *cacheFile, void **binary)␊ |
278 | {␊ |
279 | ␉char␉␉kernelCacheFile[512];␊ |
280 | ␉char␉␉kernelCachePath[512];␊ |
281 | ␉long␉␉flags, ret=-1;␊ |
282 | ␉unsigned long␉adler32 = 0;␊ |
283 | ␉u_int32_t time, cachetime, kerneltime, exttime;␊ |
284 | ␊ |
285 | ␉if((gBootMode & kBootModeSafe) != 0)␊ |
286 | ␉{␊ |
287 | ␉␉DBG("Kernel Cache ignored.\n");␊ |
288 | ␉␉return -1;␊ |
289 | ␉}␊ |
290 | ␊ |
291 | ␉// Use specify kernel cache file if not empty␊ |
292 | ␉if (cacheFile[0] != 0)␊ |
293 | ␉{␊ |
294 | ␉␉strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));␊ |
295 | ␉␉verbose("Specified kernel cache file path: %s\n", cacheFile);␊ |
296 | ␉}␊ |
297 | ␉else␊ |
298 | ␉{␊ |
299 | ␉␉// Leopard prelink kernel cache file␊ |
300 | ␉␉if ( MacOSVerCurrent >= MacOSVer2Int("10.4") && MacOSVerCurrent <= MacOSVer2Int("10.5") ) // OSX is 10.4 or 10.5␊ |
301 | ␉␉{␊ |
302 | ␉␉␉// Reset cache name.␊ |
303 | ␉␉␉bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
304 | ␉␉␉snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
305 | ␉␉␉adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
306 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);␊ |
307 | ␉␉␉verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);␊ |
308 | ␊ |
309 | ␉␉}␊ |
310 | ␉␉// Snow Leopard prelink kernel cache file␊ |
311 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.6") && MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
312 | ␉␉{␊ |
313 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",␊ |
314 | ␉␉␉␉(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");␊ |
315 | ␊ |
316 | ␉␉␉int␉␉lnam = strlen(kernelCacheFile) + 9; //with adler32␊ |
317 | ␉␉␉char␉␉*name;␊ |
318 | ␉␉␉u_int32_t␉prev_time = 0;␊ |
319 | ␊ |
320 | ␉␉␉struct dirstuff *cacheDir = opendir(kDefaultCachePathSnow);␊ |
321 | ␊ |
322 | ␉␉␉/* TODO: handle error? */␊ |
323 | ␉␉␉if (cacheDir)␊ |
324 | ␉␉␉{␊ |
325 | ␉␉␉␉while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)␊ |
326 | ␉␉␉␉{␊ |
327 | ␉␉␉␉␉if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time␊ |
328 | ␉␉␉␉␉␉&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))␊ |
329 | ␉␉␉␉␉{␊ |
330 | ␉␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);␊ |
331 | ␉␉␉␉␉␉prev_time = time;␊ |
332 | ␉␉␉␉␉}␊ |
333 | ␉␉␉␉}␊ |
334 | ␉␉␉␉verbose("Kernel Cache file path (Mac OS X 10.6): %s\n", kernelCacheFile);␊ |
335 | ␉␉␉}␊ |
336 | ␉␉␉closedir(cacheDir);␊ |
337 | ␉␉}␊ |
338 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.10") )␊ |
339 | ␉␉{␊ |
340 | ␉␉␉// Lion, Mountain Lion and Mavericks prelink kernel cache file␊ |
341 | ␉␉␉// for 10.7 10.8 10.9␊ |
342 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);␊ |
343 | ␉␉␉verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);␊ |
344 | ␊ |
345 | ␉␉}␊ |
346 | ␉␉else␊ |
347 | ␉␉{␊ |
348 | ␉␉␉// Yosemite and El Capitan prelink kernel cache file␊ |
349 | ␉␉␉// for 10.10 10.11␊ |
350 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);␊ |
351 | ␉␉␉verbose("Kernel Cache file path (OS X 10.10 and newer): %s\n", kernelCacheFile);␊ |
352 | ␉␉}␊ |
353 | ␉}␊ |
354 | ␊ |
355 | ␉// Check if the kernel cache file exists␊ |
356 | ␉ret = -1;␊ |
357 | ␊ |
358 | ␉if (gBootVolume->flags & kBVFlagBooter)␊ |
359 | ␉{␊ |
360 | ␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);␊ |
361 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
362 | ␊ |
363 | ␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
364 | ␉␉{␊ |
365 | ␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);␊ |
366 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
367 | ␊ |
368 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
369 | ␉␉␉{␊ |
370 | ␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);␊ |
371 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
372 | ␊ |
373 | ␉␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
374 | ␉␉␉␉{␊ |
375 | ␉␉␉␉␉strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof(kernelCachePath) );␊ |
376 | ␉␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
377 | ␊ |
378 | ␉␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
379 | ␉␉␉␉␉{␊ |
380 | ␉␉␉␉␉␉ret = -1;␊ |
381 | ␉␉␉␉␉}␊ |
382 | ␉␉␉␉}␊ |
383 | ␉␉␉}␊ |
384 | ␉␉}␊ |
385 | ␉}␊ |
386 | ␊ |
387 | ␉// If not found, use the original kernel cache path.␊ |
388 | ␉if (ret == -1)␊ |
389 | ␉{␊ |
390 | ␉␉strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));␊ |
391 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
392 | ␊ |
393 | ␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
394 | ␉␉{␊ |
395 | ␉␉␉ret = -1;␊ |
396 | ␉␉}␊ |
397 | ␉}␊ |
398 | ␊ |
399 | ␉// Exit if kernel cache file wasn't found␊ |
400 | ␉if (ret == -1)␊ |
401 | ␉{␊ |
402 | ␉␉DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);␊ |
403 | ␉␉return -1;␊ |
404 | ␉}␊ |
405 | ␊ |
406 | ␉// Check if the kernel cache file is more recent (mtime)␊ |
407 | ␉// than the kernel file or the S/L/E directory␊ |
408 | ␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
409 | ␊ |
410 | ␉// Check if the kernel file is more recent than the cache file␊ |
411 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))␊ |
412 | ␉{␊ |
413 | ␉␉DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);␊ |
414 | ␉␉return -1;␊ |
415 | ␉}␊ |
416 | ␊ |
417 | ␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
418 | ␊ |
419 | ␉// Check if the S/L/E directory time is more recent than the cache file␊ |
420 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))␊ |
421 | ␉{␊ |
422 | ␉␉DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);␊ |
423 | ␉␉return -1;␊ |
424 | ␉}␊ |
425 | ␊ |
426 | ␉// Since the kernel cache file exists and is the most recent try to load it␊ |
427 | ␉DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);␊ |
428 | ␊ |
429 | ␉ret = LoadThinFatFile(kernelCachePath, binary);␊ |
430 | ␉return ret; // ret contain the length of the binary␊ |
431 | }␊ |
432 | ␊ |
433 | //==========================================================================␊ |
434 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
435 | // before. Multiboot does its own runtime initialization, does some of its␊ |
436 | // own things, and then calls common_boot.␊ |
437 | void boot(int biosdev)␊ |
438 | {␊ |
439 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
440 | ␉// Note: malloc_init(), called via initialize_runtime() writes␊ |
441 | ␉// memory >= 1Mb, so A20 must be enabled before calling it. - zenith432␊ |
442 | ␉zeroBSS();␊ |
443 | ␉enableA20();␊ |
444 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
445 | ␉common_boot(biosdev);␊ |
446 | }␊ |
447 | ␊ |
448 | //==========================================================================␊ |
449 | // The 'main' function for the booter. Called by boot0 when booting␊ |
450 | // from a block device, or by the network booter.␊ |
451 | //␊ |
452 | // arguments:␊ |
453 | //␉ biosdev - Value passed from boot1/NBP to specify the device␊ |
454 | //␉␉␉ that the booter was loaded from.␊ |
455 | //␊ |
456 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
457 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
458 | // next boot device on its list.␊ |
459 | void common_boot(int biosdev)␊ |
460 | {␊ |
461 | ␉bool␉ ␉␉quiet;␊ |
462 | ␉bool␉ ␉␉firstRun = true;␊ |
463 | ␉bool␉ ␉␉instantMenu;␊ |
464 | ␉bool␉ ␉␉rescanPrompt;␊ |
465 | ␉int␉␉␉status;␊ |
466 | ␉unsigned int␉␉allowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;␊ |
467 | ␉unsigned int␉␉denyBVFlags = kBVFlagEFISystem;␊ |
468 | ␊ |
469 | ␉// Set reminder to unload the PXE base code. Neglect to unload␊ |
470 | ␉// the base code will result in a hang or kernel panic.␊ |
471 | ␉gUnloadPXEOnExit = true;␊ |
472 | ␊ |
473 | ␉// Record the device that the booter was loaded from.␊ |
474 | ␉gBIOSDev = biosdev & kBIOSDevMask;␊ |
475 | ␊ |
476 | ␉// Initialize boot-log␊ |
477 | ␉initBooterLog();␊ |
478 | ␊ |
479 | #if DEBUG_INTERRUPTS␊ |
480 | ␉// Enable interrupts␊ |
481 | ␉interruptsAvailable = SetupInterrupts();␊ |
482 | ␉if (interruptsAvailable)␊ |
483 | ␉{␊ |
484 | ␉␉EnableInterrupts();␊ |
485 | ␉}␊ |
486 | #endif␊ |
487 | ␊ |
488 | ␉// Initialize boot info structure.␊ |
489 | ␉initKernBootStruct();␊ |
490 | ␊ |
491 | ␉// Setup VGA text mode.␊ |
492 | ␉// Not sure if it is safe to call setVideoMode() before the␊ |
493 | ␉// config table has been loaded. Call video_mode() instead.␊ |
494 | #if DEBUG␊ |
495 | ␉printf("before video_mode\n");␊ |
496 | #endif␊ |
497 | ␉video_mode( 2 ); // 80x25 mono text mode.␊ |
498 | #if DEBUG␊ |
499 | ␉printf("after video_mode\n");␊ |
500 | #endif␊ |
501 | ␊ |
502 | ␉// Scan and record the system's hardware information.␊ |
503 | ␉scan_platform();␊ |
504 | ␊ |
505 | ␉// First get info for boot volume.␊ |
506 | ␉scanBootVolumes(gBIOSDev, 0);␊ |
507 | ␉bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
508 | ␉setBootGlobals(bvChain);␊ |
509 | ␊ |
510 | ␉// Load boot.plist config file␊ |
511 | ␉status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);␊ |
512 | ␊ |
513 | ␉if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)␊ |
514 | ␉{␊ |
515 | ␉␉gBootMode |= kBootModeQuiet;␊ |
516 | ␉}␊ |
517 | ␊ |
518 | ␉// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
519 | ␉if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)␊ |
520 | ␉{␊ |
521 | ␉␉firstRun = false;␊ |
522 | ␉}␊ |
523 | ␊ |
524 | ␉// Loading preboot ramdisk if exists.␊ |
525 | ␉loadPrebootRAMDisk();␊ |
526 | ␊ |
527 | ␉// Disable rescan option by default␊ |
528 | ␉gEnableCDROMRescan = false;␊ |
529 | ␊ |
530 | ␉// Enable it with Rescan=y in system config␊ |
531 | ␉if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)␉&& gEnableCDROMRescan)␊ |
532 | ␉{␊ |
533 | ␉␉gEnableCDROMRescan = true;␊ |
534 | ␉}␊ |
535 | ␊ |
536 | ␉// Disable rescanPrompt option by default␊ |
537 | ␉rescanPrompt = false;␊ |
538 | ␊ |
539 | ␉// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
540 | ␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))␊ |
541 | ␉{␊ |
542 | ␉␉gEnableCDROMRescan = promptForRescanOption();␊ |
543 | ␉}␊ |
544 | ␊ |
545 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
546 | ␉if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)␊ |
547 | ␉{␊ |
548 | ␉␉gScanSingleDrive = true;␊ |
549 | ␉}␊ |
550 | ␊ |
551 | ␉// Create a list of partitions on device(s).␊ |
552 | ␉if (gScanSingleDrive)␊ |
553 | ␉{␊ |
554 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
555 | ␉}␊ |
556 | ␉else␊ |
557 | ␉{␊ |
558 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
559 | ␉}␊ |
560 | ␊ |
561 | ␉// Create a separated bvr chain using the specified filters.␊ |
562 | ␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
563 | ␊ |
564 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
565 | ␊ |
566 | ␉// Intialize module system␊ |
567 | ␉init_module_system();␊ |
568 | ␊ |
569 | #if DEBUG␊ |
570 | ␉printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
571 | ␉␉gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
572 | ␉printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
573 | ␉␉gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
574 | ␉getchar();␊ |
575 | #endif␊ |
576 | ␊ |
577 | ␉useGUI = true;␊ |
578 | ␉// Override useGUI default␊ |
579 | ␉getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);␊ |
580 | ␉if (useGUI && initGUI())␊ |
581 | ␉{␊ |
582 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
583 | ␉␉useGUI = false;␊ |
584 | ␉}␊ |
585 | ␊ |
586 | ␉setBootGlobals(bvChain);␊ |
587 | ␊ |
588 | ␉// Parse args, load and start kernel.␊ |
589 | ␉while (1)␊ |
590 | ␉{␊ |
591 | ␉␉bool␉␉tryresume, tryresumedefault, forceresume;␊ |
592 | ␉␉bool␉␉useKernelCache = true; // by default try to use the prelinked kernel␊ |
593 | ␉␉const char␉*val;␊ |
594 | ␉␉int␉␉␉len, ret = -1;␊ |
595 | ␉␉long␉␉flags;␊ |
596 | ␉␉u_int32_t␉sleeptime, time;␊ |
597 | ␉␉void␉␉*binary = (void *)kLoadAddr;␊ |
598 | ␊ |
599 | ␉␉char␉␉bootFile[sizeof(bootInfo->bootFile)];␊ |
600 | ␉␉char␉␉bootFilePath[512];␊ |
601 | ␉␉char␉␉kernelCacheFile[512];␊ |
602 | ␊ |
603 | ␉␉// Initialize globals.␊ |
604 | ␉␉sysConfigValid = false;␊ |
605 | ␉␉gErrors␉␉ = false;␊ |
606 | ␊ |
607 | ␉␉status = getBootOptions(firstRun);␊ |
608 | ␉␉firstRun = false;␊ |
609 | ␉␉if (status == -1) continue;␊ |
610 | ␊ |
611 | ␉␉status = processBootOptions();␊ |
612 | ␉␉// Status == 1 means to chainboot␊ |
613 | ␉␉if ( status ==␉1 ) break;␊ |
614 | ␉␉// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
615 | ␉␉if ( status == -1 )␊ |
616 | ␉␉{␊ |
617 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
618 | ␉␉␉if (gBootVolume == NULL)␊ |
619 | ␉␉␉{␊ |
620 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
621 | ␊ |
622 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
623 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
624 | ␊ |
625 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
626 | ␉␉␉␉setBootGlobals(bvChain);␊ |
627 | ␉␉␉␉setupDeviceList(&bootInfo->themeConfig);␊ |
628 | ␉␉␉}␊ |
629 | ␉␉␉continue;␊ |
630 | ␉␉}␊ |
631 | ␊ |
632 | ␉␉// Other status (e.g. 0) means that we should proceed with boot.␊ |
633 | ␊ |
634 | ␉␉// Turn off any GUI elements␊ |
635 | ␉␉if ( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
636 | ␉␉{␊ |
637 | ␉␉␉gui.devicelist.draw = false;␊ |
638 | ␉␉␉gui.bootprompt.draw = false;␊ |
639 | ␉␉␉gui.menu.draw = false;␊ |
640 | ␉␉␉gui.infobox.draw = false;␊ |
641 | ␉␉␉gui.logo.draw = false;␊ |
642 | ␉␉␉drawBackground();␊ |
643 | ␉␉␉updateVRAM();␊ |
644 | ␉␉}␊ |
645 | ␊ |
646 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T))␊ |
647 | ␉␉{␊ |
648 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
649 | ␉␉}␊ |
650 | ␉␉else␊ |
651 | ␉␉{␊ |
652 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
653 | ␉␉}␊ |
654 | ␊ |
655 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))␊ |
656 | ␉␉{␊ |
657 | ␉␉␉if (strncmp(val, "x86_64", sizeof("x86_64") ) == 0)␊ |
658 | ␉␉␉{␊ |
659 | ␉␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
660 | ␉␉␉}␊ |
661 | ␉␉␉else if (strncmp(val, "i386", sizeof("i386") ) == 0)␊ |
662 | ␉␉␉{␊ |
663 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
664 | ␉␉␉}␊ |
665 | ␉␉␉else␊ |
666 | ␉␉␉{␊ |
667 | ␉␉␉␉DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");␊ |
668 | ␉␉␉}␊ |
669 | ␉␉}␊ |
670 | ␊ |
671 | ␉␉if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig))␊ |
672 | ␉␉{␊ |
673 | ␉␉␉if (strncmp(val, "i386", sizeof("i386") ) == 0)␊ |
674 | ␉␉␉{␊ |
675 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
676 | ␉␉␉}␊ |
677 | ␉␉}␊ |
678 | ␊ |
679 | ␉␉// Notify modules that we are attempting to boot␊ |
680 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL);␊ |
681 | ␊ |
682 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))␊ |
683 | ␉␉{␊ |
684 | ␉␉␉tryresume = true;␊ |
685 | ␉␉␉tryresumedefault = true;␊ |
686 | ␉␉}␊ |
687 | ␉␉else␊ |
688 | ␉␉{␊ |
689 | ␉␉␉tryresumedefault = false;␊ |
690 | ␉␉}␊ |
691 | ␊ |
692 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig))␊ |
693 | ␉␉{␊ |
694 | ␉␉␉forceresume = false;␊ |
695 | ␉␉}␊ |
696 | ␊ |
697 | ␉␉if (forceresume)␊ |
698 | ␉␉{␊ |
699 | ␉␉␉tryresume = true;␊ |
700 | ␉␉␉tryresumedefault = false;␊ |
701 | ␉␉}␊ |
702 | ␊ |
703 | ␉␉while (tryresume)␊ |
704 | ␉␉{␊ |
705 | ␉␉␉const char *tmp;␊ |
706 | ␉␉␉BVRef bvr;␊ |
707 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))␊ |
708 | ␉␉␉␉val = "/private/var/vm/sleepimage";␊ |
709 | ␊ |
710 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
711 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
712 | ␊ |
713 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
714 | ␉␉␉␉break;␊ |
715 | ␊ |
716 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
717 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
718 | ␉␉␉␉break;␊ |
719 | ␊ |
720 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
721 | ␉␉␉␉break;␊ |
722 | ␊ |
723 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime))␊ |
724 | ␉␉␉{␊ |
725 | #if DEBUG␊ |
726 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",␊ |
727 | ␉␉␉␉␉␉bvr->modTime-sleeptime);␊ |
728 | #endif␊ |
729 | ␉␉␉␉break;␊ |
730 | ␉␉␉}␊ |
731 | ␊ |
732 | ␉␉␉HibernateBoot((char *)val);␊ |
733 | ␉␉␉break;␊ |
734 | ␉␉}␊ |
735 | ␊ |
736 | ␉␉getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);␊ |
737 | ␉␉if (useKernelCache)␊ |
738 | ␉␉{␊ |
739 | ␉␉␉do␊ |
740 | ␉␉␉{␊ |
741 | ␉␉␉␉// Determine the name of the Kernel Cache␊ |
742 | ␉␉␉␉if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))␊ |
743 | ␉␉␉␉{␊ |
744 | ␉␉␉␉␉if (val[0] == '\\')␊ |
745 | ␉␉␉␉␉{␊ |
746 | ␉␉␉␉␉␉len--;␊ |
747 | ␉␉␉␉␉␉val++;␊ |
748 | ␉␉␉␉␉}␊ |
749 | ␉␉␉␉␉/* FIXME: check len vs sizeof(kernelCacheFile) */␊ |
750 | ␉␉␉␉␉strlcpy(kernelCacheFile, val, len + 1);␊ |
751 | ␉␉␉␉}␊ |
752 | ␉␉␉␉else␊ |
753 | ␉␉␉␉{␊ |
754 | ␉␉␉␉␉kernelCacheFile[0] = 0; // Use default kernel cache file␊ |
755 | ␉␉␉␉}␊ |
756 | ␊ |
757 | ␉␉␉␉if (gOverrideKernel && kernelCacheFile[0] == 0)␊ |
758 | ␉␉␉␉{␊ |
759 | ␉␉␉␉␉DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);␊ |
760 | ␉␉␉␉␉useKernelCache = false;␊ |
761 | ␉␉␉␉␉break;␊ |
762 | ␉␉␉␉}␊ |
763 | ␊ |
764 | ␉␉␉␉if (gMKextName[0] != 0)␊ |
765 | ␉␉␉␉{␊ |
766 | ␉␉␉␉␉DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n", gMKextName);␊ |
767 | ␉␉␉␉␉useKernelCache = false;␊ |
768 | ␉␉␉␉␉break;␊ |
769 | ␉␉␉␉}␊ |
770 | ␊ |
771 | ␉␉␉␉if (gBootFileType != kBlockDeviceType)␊ |
772 | ␉␉␉␉{␊ |
773 | ␉␉␉␉␉useKernelCache = false;␊ |
774 | ␉␉␉␉}␊ |
775 | ␊ |
776 | ␉␉␉} while(0);␊ |
777 | ␉␉}␊ |
778 | ␉␉else␊ |
779 | ␉␉{␊ |
780 | ␉␉␉DBG("Kernel Cache using disabled by user.\n");␊ |
781 | ␉␉}␊ |
782 | ␊ |
783 | ␉␉do␊ |
784 | ␉␉{␊ |
785 | ␉␉␉if (useKernelCache)␊ |
786 | ␉␉␉{␊ |
787 | ␉␉␉␉ret = LoadKernelCache(kernelCacheFile, &binary);␊ |
788 | ␉␉␉␉if (ret >= 0)␊ |
789 | ␉␉␉␉{␊ |
790 | ␉␉␉␉␉break;␊ |
791 | ␉␉␉␉}␊ |
792 | ␉␉␉}␊ |
793 | ␊ |
794 | ␉␉␉bool bootFileWithDevice = false;␊ |
795 | ␉␉␉// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel␊ |
796 | ␉␉␉if (strncmp(bootInfo->bootFile, "bt(", sizeof("bt(") ) == 0 ||␊ |
797 | ␉␉␉␉strncmp(bootInfo->bootFile, "hd(", sizeof("hd(") ) == 0 ||␊ |
798 | ␉␉␉␉strncmp(bootInfo->bootFile, "rd(", sizeof("rd(") ) == 0)␊ |
799 | ␉␉␉{␊ |
800 | ␉␉␉␉bootFileWithDevice = true;␊ |
801 | ␉␉␉}␊ |
802 | ␊ |
803 | ␉␉␉// bootFile must start with a / if it not start with a device name␊ |
804 | ␉␉␉if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')␊ |
805 | ␉␉␉{␊ |
806 | ␉␉␉␉if ( MacOSVerCurrent < MacOSVer2Int("10.10") ) // Mavericks and older␊ |
807 | ␉␉␉␉{␊ |
808 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /␊ |
809 | ␉␉␉␉}␊ |
810 | ␉␉␉␉else␊ |
811 | ␉␉␉␉{␊ |
812 | ␉␉␉␉␉// Yosemite and newer␊ |
813 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite or El Capitan␊ |
814 | ␉␉␉␉}␊ |
815 | ␉␉␉}␊ |
816 | ␉␉␉else␊ |
817 | ␉␉␉{␊ |
818 | ␉␉␉␉strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));␊ |
819 | ␉␉␉}␊ |
820 | ␊ |
821 | ␉␉␉// Try to load kernel image from alternate locations on boot helper partitions.␊ |
822 | ␉␉␉ret = -1;␊ |
823 | ␉␉␉if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)␊ |
824 | ␉␉␉{␊ |
825 | ␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);␊ |
826 | ␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
827 | ␉␉␉␉if (ret == -1)␊ |
828 | ␉␉␉␉{␊ |
829 | ␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);␊ |
830 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
831 | ␉␉␉␉␉if (ret == -1)␊ |
832 | ␉␉␉␉␉{␊ |
833 | ␉␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);␊ |
834 | ␉␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
835 | ␉␉␉␉␉}␊ |
836 | ␉␉␉␉}␊ |
837 | ␉␉␉}␊ |
838 | ␉␉␉if (ret == -1)␊ |
839 | ␉␉␉{␊ |
840 | ␉␉␉␉// No alternate location found, using the original kernel image path.␊ |
841 | ␉␉␉␉strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));␊ |
842 | ␉␉␉}␊ |
843 | ␊ |
844 | ␉␉␉DBG("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);␊ |
845 | ␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
846 | ␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
847 | ␉␉␉{␊ |
848 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
849 | ␉␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
850 | ␉␉␉}␊ |
851 | ␉␉} while (0);␊ |
852 | ␊ |
853 | ␉␉clearActivityIndicator();␊ |
854 | ␊ |
855 | #if DEBUG␊ |
856 | ␉␉printf("Pausing...");␊ |
857 | ␉␉sleep(8);␊ |
858 | #endif␊ |
859 | ␊ |
860 | ␉␉if (ret <= 0)␊ |
861 | ␉␉{␊ |
862 | ␉␉␉printf("Can't find boot file: '%s'\n", bootFile);␊ |
863 | ␉␉␉sleep(1);␊ |
864 | ␊ |
865 | ␉␉␉if (gBootFileType == kNetworkDeviceType)␊ |
866 | ␉␉␉{␊ |
867 | ␉␉␉␉// Return control back to PXE. Don't unload PXE base code.␊ |
868 | ␉␉␉␉gUnloadPXEOnExit = false;␊ |
869 | ␉␉␉␉break;␊ |
870 | ␉␉␉}␊ |
871 | ␉␉␉pause();␊ |
872 | ␊ |
873 | ␉␉}␊ |
874 | ␉␉else␊ |
875 | ␉␉{␊ |
876 | ␉␉␉/* Won't return if successful. */␊ |
877 | ␉␉␉ret = ExecKernel(binary);␊ |
878 | ␉␉}␊ |
879 | ␉}␊ |
880 | ␊ |
881 | ␉// chainboot␊ |
882 | ␉if (status == 1)␊ |
883 | ␉{␊ |
884 | ␉␉// if we are already in graphics-mode,␊ |
885 | ␉␉if (getVideoMode() == GRAPHICS_MODE)␊ |
886 | ␉␉{␊ |
887 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.␊ |
888 | ␉␉}␊ |
889 | ␉}␊ |
890 | ␊ |
891 | ␉if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)␊ |
892 | ␉{␊ |
893 | ␉␉nbpUnloadBaseCode();␊ |
894 | ␉}␊ |
895 | ␊ |
896 | #if DEBUG_INTERRUPTS␊ |
897 | ␉if (interruptsAvailable)␊ |
898 | ␉{␊ |
899 | ␉␉DisableInterrupts();␊ |
900 | ␉}␊ |
901 | #endif␊ |
902 | ␊ |
903 | }␊ |
904 | ␊ |
905 | // =========================================================================␊ |
906 | //␊ |
907 | void setupBooterArgs()␊ |
908 | {␊ |
909 | ␉bool KPRebootOption␉= false;␊ |
910 | ␉bool HiDPIOption␉= false;␊ |
911 | ␉bool FlagBlackOption␉= false;␊ |
912 | ␊ |
913 | ␊ |
914 | ␉// OS X Mountain Lion 10.8␊ |
915 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.8") ) // Mountain Lion and Up!␊ |
916 | ␉{␊ |
917 | ␉␉// cparm␊ |
918 | ␉␉getBoolForKey(kRebootOnPanic, &KPRebootOption, &bootInfo->chameleonConfig);␊ |
919 | ␉␉if ( KPRebootOption )␊ |
920 | ␉␉{␊ |
921 | ␉␉␉bootArgs->flags |= kBootArgsFlagRebootOnPanic;␊ |
922 | ␉␉}␊ |
923 | ␊ |
924 | ␉␉// cparm␊ |
925 | ␉␉getBoolForKey(kEnableHiDPI, &HiDPIOption, &bootInfo->chameleonConfig);␊ |
926 | ␊ |
927 | ␉␉if ( HiDPIOption )␊ |
928 | ␉␉{␊ |
929 | ␉␉␉bootArgs->flags |= kBootArgsFlagHiDPI;␊ |
930 | ␉␉}␊ |
931 | ␉}␊ |
932 | ␊ |
933 | ␉// OS X Yosemite 10.10␊ |
934 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.10") ) // Yosemite and Up!␊ |
935 | ␉{␊ |
936 | ␉␉// Pike R. Alpha␊ |
937 | ␉␉getBoolForKey(kBlackMode, &FlagBlackOption, &bootInfo->chameleonConfig);␊ |
938 | ␉␉if ( FlagBlackOption )␊ |
939 | ␉␉{␊ |
940 | ␉␉␉// bootArgs->flags |= kBootArgsFlagBlack;␊ |
941 | ␉␉␉bootArgs->flags |= kBootArgsFlagBlackBg; // Micky1979␊ |
942 | ␉␉}␊ |
943 | ␉}␊ |
944 | ␊ |
945 | ␉// OS X El Capitan 10.11␊ |
946 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.11") ) // El Capitan and Up!␊ |
947 | ␉{␊ |
948 | ␉␉// ErmaC␊ |
949 | ␉␉verbose("\n");␊ |
950 | ␉␉int␉csrValue;␊ |
951 | ␊ |
952 | #if 0␊ |
953 | ␉␉/*␊ |
954 | ␉␉ * A special BootArgs flag "kBootArgsFlagCSRBoot"␊ |
955 | ␉␉ * is set in the Recovery or Installation environment.␊ |
956 | ␉␉ * This flag is kind of overkill by turning off all the protections␊ |
957 | ␉␉ */␊ |
958 | ␊ |
959 | ␉␉if (isRecoveryHD)␊ |
960 | ␉␉{␊ |
961 | ␉␉␉// SIP can be controlled with or without FileNVRAM.kext (Pike R. Alpha)␊ |
962 | ␉␉␉bootArgs->flags␉|=␉(kBootArgsFlagCSRActiveConfig + kBootArgsFlagCSRConfigMode + kBootArgsFlagCSRBoot);␊ |
963 | ␉␉}␊ |
964 | #endif␊ |
965 | ␊ |
966 | ␉␉bootArgs->flags␉␉|= kBootArgsFlagCSRActiveConfig;␊ |
967 | ␊ |
968 | ␊ |
969 | ␉␉// Set limit to 7bit␊ |
970 | ␉␉if ( getIntForKey(kCsrActiveConfig, &csrValue, &bootInfo->chameleonConfig) && (csrValue >= 0 && csrValue <= 127) )␊ |
971 | ␉␉{␊ |
972 | ␉␉␉bootArgs->csrActiveConfig␉= csrValue;␊ |
973 | ␊ |
974 | ␉␉}␊ |
975 | ␉␉else␊ |
976 | ␉␉{␊ |
977 | ␉␉␉// zenith432␊ |
978 | ␉␉␉bootArgs->csrActiveConfig␉= 0x67;␊ |
979 | ␊ |
980 | ␉␉}␊ |
981 | ␊ |
982 | // ===============================================================================␊ |
983 | ␉␉verbose("CsrActiveConfig set to 0x%x\n", bootArgs->csrActiveConfig);␊ |
984 | ␊ |
985 | // ===============================================================================␊ |
986 | ␊ |
987 | ␉␉bootArgs->csrCapabilities␉= CSR_VALID_FLAGS;␊ |
988 | ␉␉bootArgs->boot_SMC_plimit␉= 0;␊ |
989 | ␉}␊ |
990 | }␊ |
991 | ␊ |
992 | // =========================================================================␊ |
993 | ␊ |
994 | // =========================================================================␊ |
995 | ␊ |
996 | /*!␊ |
997 | ␉Selects a new BIOS device, taking care to update the global state appropriately.␊ |
998 | */␊ |
999 | /*␊ |
1000 | static void selectBiosDevice(void)␊ |
1001 | {␊ |
1002 | ␉struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
1003 | ␉CacheReset();␊ |
1004 | ␉diskFreeMap(oldMap);␊ |
1005 | ␉oldMap = NULL;␊ |
1006 | ␊ |
1007 | ␉int dev = selectAlternateBootDevice(gBIOSDev);␊ |
1008 | ␊ |
1009 | ␉BVRef bvchain = scanBootVolumes(dev, 0);␊ |
1010 | ␉BVRef bootVol = selectBootVolume(bvchain);␊ |
1011 | ␉gBootVolume = bootVol;␊ |
1012 | ␉setRootVolume(bootVol);␊ |
1013 | ␉gBIOSDev = dev;␊ |
1014 | }␊ |
1015 | */␊ |
1016 | ␊ |
1017 | // =========================================================================␊ |
1018 | bool checkOSVersion(const char *version)␊ |
1019 | {␊ |
1020 | ␉if ( '\0' != version[4] )␊ |
1021 | ␉{␊ |
1022 | ␉␉return !memcmp(&gMacOSVersion[0], &version[0], 5);␊ |
1023 | ␉}␊ |
1024 | ␉else␊ |
1025 | ␉{␊ |
1026 | ␉␉return !memcmp(&gMacOSVersion[0], &version[0], 4);␊ |
1027 | ␉}␊ |
1028 | }␊ |
1029 | ␊ |
1030 | uint32_t getMacOSVerCurrent()␊ |
1031 | {␊ |
1032 | ␉MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);␊ |
1033 | ␉return MacOSVerCurrent;␊ |
1034 | }␊ |
1035 | ␊ |
1036 | // =========================================================================␊ |
1037 | unsigned long Adler32(unsigned char *buf, long len)␊ |
1038 | {␊ |
1039 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
1040 | #define NMAX 5000␊ |
1041 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
1042 | ␊ |
1043 | #define DO1(buf, i)␉{s1 += buf[i]; s2 += s1;}␊ |
1044 | #define DO2(buf, i)␉DO1(buf, i); DO1(buf, i + 1);␊ |
1045 | #define DO4(buf, i)␉DO2(buf, i); DO2(buf, i + 2);␊ |
1046 | #define DO8(buf, i)␉DO4(buf, i); DO4(buf, i + 4);␊ |
1047 | #define DO16(buf)␉DO8(buf, 0); DO8(buf, 8);␊ |
1048 | ␊ |
1049 | ␉int k;␊ |
1050 | ␊ |
1051 | ␉unsigned long s1 = 1; // adler & 0xffff;␊ |
1052 | ␉unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
1053 | ␉unsigned long result;␊ |
1054 | ␊ |
1055 | ␉while (len > 0) {␊ |
1056 | ␉␉k = len < NMAX ? len : NMAX;␊ |
1057 | ␉␉len -= k;␊ |
1058 | ␉␉while (k >= 16)␊ |
1059 | ␉␉{␊ |
1060 | ␉␉␉DO16(buf);␊ |
1061 | ␉␉␉buf += 16;␊ |
1062 | ␉␉␉k -= 16;␊ |
1063 | ␉␉}␊ |
1064 | ␊ |
1065 | ␉␉if (k != 0)␊ |
1066 | ␉␉{␊ |
1067 | ␉␉␉do␊ |
1068 | ␉␉␉{␊ |
1069 | ␉␉␉␉s1 += *buf++;␊ |
1070 | ␉␉␉␉s2 += s1;␊ |
1071 | ␉␉␉} while (--k);␊ |
1072 | ␉␉}␊ |
1073 | ␊ |
1074 | ␉␉s1 %= BASE;␊ |
1075 | ␉␉s2 %= BASE;␊ |
1076 | ␉}␊ |
1077 | ␊ |
1078 | ␉result = (s2 << 16) | s1;␊ |
1079 | ␊ |
1080 | ␉return OSSwapHostToBigInt32(result);␊ |
1081 | }␊ |
1082 | |