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 );␊ |
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 | ␉␉startprog( kernelEntry, bootArgs );␊ |
264 | ␉}␊ |
265 | ␊ |
266 | ␉// Not reached␊ |
267 | ␉__builtin_unreachable();␊ |
268 | }␊ |
269 | ␊ |
270 | //==========================================================================␊ |
271 | // LoadKernelCache - Try to load Kernel Cache.␊ |
272 | // return the length of the loaded cache file or -1 on error␊ |
273 | long LoadKernelCache(const char *cacheFile, void **binary)␊ |
274 | {␊ |
275 | ␉char␉␉kernelCacheFile[512];␊ |
276 | ␉char␉␉kernelCachePath[512];␊ |
277 | ␉long␉␉flags, ret=-1;␊ |
278 | ␉unsigned long␉adler32 = 0;␊ |
279 | ␉u_int32_t time, cachetime, kerneltime, exttime;␊ |
280 | ␊ |
281 | ␉if((gBootMode & kBootModeSafe) != 0)␊ |
282 | ␉{␊ |
283 | ␉␉DBG("Kernel Cache ignored.\n");␊ |
284 | ␉␉return -1;␊ |
285 | ␉}␊ |
286 | ␊ |
287 | ␉// Use specify kernel cache file if not empty␊ |
288 | ␉if (cacheFile[0] != 0)␊ |
289 | ␉{␊ |
290 | ␉␉strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));␊ |
291 | ␉␉verbose("Specified kernel cache file path: %s\n", cacheFile);␊ |
292 | ␉}␊ |
293 | ␉else␊ |
294 | ␉{␊ |
295 | ␉␉// Leopard prelink kernel cache file␊ |
296 | ␉␉if ( MacOSVerCurrent >= MacOSVer2Int("10.4") && MacOSVerCurrent <= MacOSVer2Int("10.5") ) // OSX is 10.4 or 10.5␊ |
297 | ␉␉{␊ |
298 | ␉␉␉// Reset cache name.␊ |
299 | ␉␉␉bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
300 | ␉␉␉snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
301 | ␉␉␉adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
302 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);␊ |
303 | ␉␉␉verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);␊ |
304 | ␊ |
305 | ␉␉}␊ |
306 | ␉␉// Snow Leopard prelink kernel cache file␊ |
307 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.6") && MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
308 | ␉␉{␊ |
309 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",␊ |
310 | ␉␉␉␉(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");␊ |
311 | ␊ |
312 | ␉␉␉int␉␉lnam = strlen(kernelCacheFile) + 9; //with adler32␊ |
313 | ␉␉␉char␉␉*name;␊ |
314 | ␉␉␉u_int32_t␉prev_time = 0;␊ |
315 | ␊ |
316 | ␉␉␉struct dirstuff *cacheDir = opendir(kDefaultCachePathSnow);␊ |
317 | ␊ |
318 | ␉␉␉/* TODO: handle error? */␊ |
319 | ␉␉␉if (cacheDir)␊ |
320 | ␉␉␉{␊ |
321 | ␉␉␉␉while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)␊ |
322 | ␉␉␉␉{␊ |
323 | ␉␉␉␉␉if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time␊ |
324 | ␉␉␉␉␉␉&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))␊ |
325 | ␉␉␉␉␉{␊ |
326 | ␉␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);␊ |
327 | ␉␉␉␉␉␉prev_time = time;␊ |
328 | ␉␉␉␉␉}␊ |
329 | ␉␉␉␉}␊ |
330 | ␉␉␉␉verbose("Kernel Cache file path (Mac OS X 10.6): %s\n", kernelCacheFile);␊ |
331 | ␉␉␉}␊ |
332 | ␉␉␉closedir(cacheDir);␊ |
333 | ␉␉}␊ |
334 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.10") )␊ |
335 | ␉␉{␊ |
336 | ␉␉␉// Lion, Mountain Lion and Mavericks prelink kernel cache file␊ |
337 | ␉␉␉// for 10.7 10.8 10.9␊ |
338 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);␊ |
339 | ␉␉␉verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);␊ |
340 | ␊ |
341 | ␉␉}␊ |
342 | ␉␉else␊ |
343 | ␉␉{␊ |
344 | ␉␉␉// Yosemite and El Capitan prelink kernel cache file␊ |
345 | ␉␉␉// for 10.10 10.11␊ |
346 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);␊ |
347 | ␉␉␉verbose("Kernel Cache file path (OS X 10.10 and newer): %s\n", kernelCacheFile);␊ |
348 | ␉␉}␊ |
349 | ␉}␊ |
350 | ␊ |
351 | ␉// Check if the kernel cache file exists␊ |
352 | ␉ret = -1;␊ |
353 | ␊ |
354 | ␉if (gBootVolume->flags & kBVFlagBooter)␊ |
355 | ␉{␊ |
356 | ␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);␊ |
357 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
358 | ␊ |
359 | ␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
360 | ␉␉{␊ |
361 | ␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);␊ |
362 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
363 | ␊ |
364 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
365 | ␉␉␉{␊ |
366 | ␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);␊ |
367 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
368 | ␊ |
369 | ␉␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
370 | ␉␉␉␉{␊ |
371 | ␉␉␉␉␉strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof(kernelCachePath) );␊ |
372 | ␉␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
373 | ␊ |
374 | ␉␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
375 | ␉␉␉␉␉{␊ |
376 | ␉␉␉␉␉␉ret = -1;␊ |
377 | ␉␉␉␉␉}␊ |
378 | ␉␉␉␉}␊ |
379 | ␉␉␉}␊ |
380 | ␉␉}␊ |
381 | ␉}␊ |
382 | ␊ |
383 | ␉// If not found, use the original kernel cache path.␊ |
384 | ␉if (ret == -1)␊ |
385 | ␉{␊ |
386 | ␉␉strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));␊ |
387 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
388 | ␊ |
389 | ␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
390 | ␉␉{␊ |
391 | ␉␉␉ret = -1;␊ |
392 | ␉␉}␊ |
393 | ␉}␊ |
394 | ␊ |
395 | ␉// Exit if kernel cache file wasn't found␊ |
396 | ␉if (ret == -1)␊ |
397 | ␉{␊ |
398 | ␉␉DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);␊ |
399 | ␉␉return -1;␊ |
400 | ␉}␊ |
401 | ␊ |
402 | ␉// Check if the kernel cache file is more recent (mtime)␊ |
403 | ␉// than the kernel file or the S/L/E directory␊ |
404 | ␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
405 | ␊ |
406 | ␉// Check if the kernel file is more recent than the cache file␊ |
407 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))␊ |
408 | ␉{␊ |
409 | ␉␉DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);␊ |
410 | ␉␉return -1;␊ |
411 | ␉}␊ |
412 | ␊ |
413 | ␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
414 | ␊ |
415 | ␉// Check if the S/L/E directory time is more recent than the cache file␊ |
416 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))␊ |
417 | ␉{␊ |
418 | ␉␉DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);␊ |
419 | ␉␉return -1;␊ |
420 | ␉}␊ |
421 | ␊ |
422 | ␉// Since the kernel cache file exists and is the most recent try to load it␊ |
423 | ␉DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);␊ |
424 | ␊ |
425 | ␉ret = LoadThinFatFile(kernelCachePath, binary);␊ |
426 | ␉return ret; // ret contain the length of the binary␊ |
427 | }␊ |
428 | ␊ |
429 | //==========================================================================␊ |
430 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
431 | // before. Multiboot does its own runtime initialization, does some of its␊ |
432 | // own things, and then calls common_boot.␊ |
433 | void boot(int biosdev)␊ |
434 | {␊ |
435 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
436 | ␉// Note: malloc_init(), called via initialize_runtime() writes␊ |
437 | ␉// memory >= 1Mb, so A20 must be enabled before calling it. - zenith432␊ |
438 | ␉zeroBSS();␊ |
439 | ␉enableA20();␊ |
440 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
441 | ␉common_boot(biosdev);␊ |
442 | }␊ |
443 | ␊ |
444 | //==========================================================================␊ |
445 | // The 'main' function for the booter. Called by boot0 when booting␊ |
446 | // from a block device, or by the network booter.␊ |
447 | //␊ |
448 | // arguments:␊ |
449 | //␉ biosdev - Value passed from boot1/NBP to specify the device␊ |
450 | //␉␉␉ that the booter was loaded from.␊ |
451 | //␊ |
452 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
453 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
454 | // next boot device on its list.␊ |
455 | void common_boot(int biosdev)␊ |
456 | {␊ |
457 | ␉bool␉ ␉␉quiet;␊ |
458 | ␉bool␉ ␉␉firstRun = true;␊ |
459 | ␉bool␉ ␉␉instantMenu;␊ |
460 | ␉bool␉ ␉␉rescanPrompt;␊ |
461 | ␉int␉␉␉status;␊ |
462 | ␉unsigned int␉␉allowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;␊ |
463 | ␉unsigned int␉␉denyBVFlags = kBVFlagEFISystem;␊ |
464 | ␊ |
465 | ␉// Set reminder to unload the PXE base code. Neglect to unload␊ |
466 | ␉// the base code will result in a hang or kernel panic.␊ |
467 | ␉gUnloadPXEOnExit = true;␊ |
468 | ␊ |
469 | ␉// Record the device that the booter was loaded from.␊ |
470 | ␉gBIOSDev = biosdev & kBIOSDevMask;␊ |
471 | ␊ |
472 | ␉// Initialize boot-log␊ |
473 | ␉initBooterLog();␊ |
474 | ␊ |
475 | #if DEBUG_INTERRUPTS␊ |
476 | ␉// Enable interrupts␊ |
477 | ␉interruptsAvailable = SetupInterrupts();␊ |
478 | ␉if (interruptsAvailable)␊ |
479 | ␉{␊ |
480 | ␉␉EnableInterrupts();␊ |
481 | ␉}␊ |
482 | #endif␊ |
483 | ␊ |
484 | ␉// Initialize boot info structure.␊ |
485 | ␉initKernBootStruct();␊ |
486 | ␊ |
487 | ␉// Setup VGA text mode.␊ |
488 | ␉// Not sure if it is safe to call setVideoMode() before the␊ |
489 | ␉// config table has been loaded. Call video_mode() instead.␊ |
490 | #if DEBUG␊ |
491 | ␉printf("before video_mode\n");␊ |
492 | #endif␊ |
493 | ␉video_mode( 2 ); // 80x25 mono text mode.␊ |
494 | #if DEBUG␊ |
495 | ␉printf("after video_mode\n");␊ |
496 | #endif␊ |
497 | ␊ |
498 | ␉// Scan and record the system's hardware information.␊ |
499 | ␉scan_platform();␊ |
500 | ␊ |
501 | ␉// First get info for boot volume.␊ |
502 | ␉scanBootVolumes(gBIOSDev, 0);␊ |
503 | ␉bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
504 | ␉setBootGlobals(bvChain);␊ |
505 | ␊ |
506 | ␉// Load boot.plist config file␊ |
507 | ␉status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);␊ |
508 | ␊ |
509 | ␉if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)␊ |
510 | ␉{␊ |
511 | ␉␉gBootMode |= kBootModeQuiet;␊ |
512 | ␉}␊ |
513 | ␊ |
514 | ␉// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
515 | ␉if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)␊ |
516 | ␉{␊ |
517 | ␉␉firstRun = false;␊ |
518 | ␉}␊ |
519 | ␊ |
520 | ␉// Loading preboot ramdisk if exists.␊ |
521 | ␉loadPrebootRAMDisk();␊ |
522 | ␊ |
523 | ␉// Disable rescan option by default␊ |
524 | ␉gEnableCDROMRescan = false;␊ |
525 | ␊ |
526 | ␉// Enable it with Rescan=y in system config␊ |
527 | ␉if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)␉&& gEnableCDROMRescan)␊ |
528 | ␉{␊ |
529 | ␉␉gEnableCDROMRescan = true;␊ |
530 | ␉}␊ |
531 | ␊ |
532 | ␉// Disable rescanPrompt option by default␊ |
533 | ␉rescanPrompt = false;␊ |
534 | ␊ |
535 | ␉// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
536 | ␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))␊ |
537 | ␉{␊ |
538 | ␉␉gEnableCDROMRescan = promptForRescanOption();␊ |
539 | ␉}␊ |
540 | ␊ |
541 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
542 | ␉if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)␊ |
543 | ␉{␊ |
544 | ␉␉gScanSingleDrive = true;␊ |
545 | ␉}␊ |
546 | ␊ |
547 | ␉// Create a list of partitions on device(s).␊ |
548 | ␉if (gScanSingleDrive)␊ |
549 | ␉{␊ |
550 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
551 | ␉}␊ |
552 | ␉else␊ |
553 | ␉{␊ |
554 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
555 | ␉}␊ |
556 | ␊ |
557 | ␉// Create a separated bvr chain using the specified filters.␊ |
558 | ␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
559 | ␊ |
560 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
561 | ␊ |
562 | ␉// Intialize module system␊ |
563 | ␉init_module_system();␊ |
564 | ␊ |
565 | #if DEBUG␊ |
566 | ␉printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
567 | ␉␉gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
568 | ␉printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
569 | ␉␉gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
570 | ␉getchar();␊ |
571 | #endif␊ |
572 | ␊ |
573 | ␉useGUI = true;␊ |
574 | ␉// Override useGUI default␊ |
575 | ␉getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);␊ |
576 | ␉if (useGUI && initGUI())␊ |
577 | ␉{␊ |
578 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
579 | ␉␉useGUI = false;␊ |
580 | ␉}␊ |
581 | ␊ |
582 | ␉setBootGlobals(bvChain);␊ |
583 | ␊ |
584 | ␉// Parse args, load and start kernel.␊ |
585 | ␉while (1)␊ |
586 | ␉{␊ |
587 | ␉␉bool␉␉tryresume, tryresumedefault, forceresume;␊ |
588 | ␉␉bool␉␉useKernelCache = true; // by default try to use the prelinked kernel␊ |
589 | ␉␉const char␉*val;␊ |
590 | ␉␉int␉␉␉len, ret = -1;␊ |
591 | ␉␉long␉␉flags;␊ |
592 | ␉␉u_int32_t␉sleeptime, time;␊ |
593 | ␉␉void␉␉*binary = (void *)kLoadAddr;␊ |
594 | ␊ |
595 | ␉␉char␉␉bootFile[sizeof(bootInfo->bootFile)];␊ |
596 | ␉␉char␉␉bootFilePath[512];␊ |
597 | ␉␉char␉␉kernelCacheFile[512];␊ |
598 | ␊ |
599 | ␉␉// Initialize globals.␊ |
600 | ␉␉sysConfigValid = false;␊ |
601 | ␉␉gErrors␉␉ = false;␊ |
602 | ␊ |
603 | ␉␉status = getBootOptions(firstRun);␊ |
604 | ␉␉firstRun = false;␊ |
605 | ␉␉if (status == -1) continue;␊ |
606 | ␊ |
607 | ␉␉status = processBootOptions();␊ |
608 | ␉␉// Status == 1 means to chainboot␊ |
609 | ␉␉if ( status ==␉1 ) break;␊ |
610 | ␉␉// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
611 | ␉␉if ( status == -1 )␊ |
612 | ␉␉{␊ |
613 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
614 | ␉␉␉if (gBootVolume == NULL)␊ |
615 | ␉␉␉{␊ |
616 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
617 | ␊ |
618 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
619 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
620 | ␊ |
621 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
622 | ␉␉␉␉setBootGlobals(bvChain);␊ |
623 | ␉␉␉␉setupDeviceList(&bootInfo->themeConfig);␊ |
624 | ␉␉␉}␊ |
625 | ␉␉␉continue;␊ |
626 | ␉␉}␊ |
627 | ␊ |
628 | ␉␉// Other status (e.g. 0) means that we should proceed with boot.␊ |
629 | ␊ |
630 | ␉␉// Turn off any GUI elements␊ |
631 | ␉␉if ( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
632 | ␉␉{␊ |
633 | ␉␉␉gui.devicelist.draw = false;␊ |
634 | ␉␉␉gui.bootprompt.draw = false;␊ |
635 | ␉␉␉gui.menu.draw = false;␊ |
636 | ␉␉␉gui.infobox.draw = false;␊ |
637 | ␉␉␉gui.logo.draw = false;␊ |
638 | ␉␉␉drawBackground();␊ |
639 | ␉␉␉updateVRAM();␊ |
640 | ␉␉}␊ |
641 | ␊ |
642 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T))␊ |
643 | ␉␉{␊ |
644 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
645 | ␉␉}␊ |
646 | ␉␉else␊ |
647 | ␉␉{␊ |
648 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
649 | ␉␉}␊ |
650 | ␊ |
651 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))␊ |
652 | ␉␉{␊ |
653 | ␉␉␉if (strncmp(val, "x86_64", sizeof("x86_64") ) == 0)␊ |
654 | ␉␉␉{␊ |
655 | ␉␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
656 | ␉␉␉}␊ |
657 | ␉␉␉else if (strncmp(val, "i386", sizeof("i386") ) == 0)␊ |
658 | ␉␉␉{␊ |
659 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
660 | ␉␉␉}␊ |
661 | ␉␉␉else␊ |
662 | ␉␉␉{␊ |
663 | ␉␉␉␉DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");␊ |
664 | ␉␉␉}␊ |
665 | ␉␉}␊ |
666 | ␊ |
667 | ␉␉if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig))␊ |
668 | ␉␉{␊ |
669 | ␉␉␉if (strncmp(val, "i386", sizeof("i386") ) == 0)␊ |
670 | ␉␉␉{␊ |
671 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
672 | ␉␉␉}␊ |
673 | ␉␉}␊ |
674 | ␊ |
675 | ␉␉// Notify modules that we are attempting to boot␊ |
676 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL);␊ |
677 | ␊ |
678 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))␊ |
679 | ␉␉{␊ |
680 | ␉␉␉tryresume = true;␊ |
681 | ␉␉␉tryresumedefault = true;␊ |
682 | ␉␉}␊ |
683 | ␉␉else␊ |
684 | ␉␉{␊ |
685 | ␉␉␉tryresumedefault = false;␊ |
686 | ␉␉}␊ |
687 | ␊ |
688 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig))␊ |
689 | ␉␉{␊ |
690 | ␉␉␉forceresume = false;␊ |
691 | ␉␉}␊ |
692 | ␊ |
693 | ␉␉if (forceresume)␊ |
694 | ␉␉{␊ |
695 | ␉␉␉tryresume = true;␊ |
696 | ␉␉␉tryresumedefault = false;␊ |
697 | ␉␉}␊ |
698 | ␊ |
699 | ␉␉while (tryresume)␊ |
700 | ␉␉{␊ |
701 | ␉␉␉const char *tmp;␊ |
702 | ␉␉␉BVRef bvr;␊ |
703 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))␊ |
704 | ␉␉␉␉val = "/private/var/vm/sleepimage";␊ |
705 | ␊ |
706 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
707 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
708 | ␊ |
709 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
710 | ␉␉␉␉break;␊ |
711 | ␊ |
712 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
713 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
714 | ␉␉␉␉break;␊ |
715 | ␊ |
716 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
717 | ␉␉␉␉break;␊ |
718 | ␊ |
719 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime))␊ |
720 | ␉␉␉{␊ |
721 | #if DEBUG␊ |
722 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",␊ |
723 | ␉␉␉␉␉␉bvr->modTime-sleeptime);␊ |
724 | #endif␊ |
725 | ␉␉␉␉break;␊ |
726 | ␉␉␉}␊ |
727 | ␊ |
728 | ␉␉␉HibernateBoot((char *)val);␊ |
729 | ␉␉␉break;␊ |
730 | ␉␉}␊ |
731 | ␊ |
732 | ␉␉getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);␊ |
733 | ␉␉if (useKernelCache)␊ |
734 | ␉␉{␊ |
735 | ␉␉␉do␊ |
736 | ␉␉␉{␊ |
737 | ␉␉␉␉// Determine the name of the Kernel Cache␊ |
738 | ␉␉␉␉if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))␊ |
739 | ␉␉␉␉{␊ |
740 | ␉␉␉␉␉if (val[0] == '\\')␊ |
741 | ␉␉␉␉␉{␊ |
742 | ␉␉␉␉␉␉len--;␊ |
743 | ␉␉␉␉␉␉val++;␊ |
744 | ␉␉␉␉␉}␊ |
745 | ␉␉␉␉␉/* FIXME: check len vs sizeof(kernelCacheFile) */␊ |
746 | ␉␉␉␉␉strlcpy(kernelCacheFile, val, len + 1);␊ |
747 | ␉␉␉␉}␊ |
748 | ␉␉␉␉else␊ |
749 | ␉␉␉␉{␊ |
750 | ␉␉␉␉␉kernelCacheFile[0] = 0; // Use default kernel cache file␊ |
751 | ␉␉␉␉}␊ |
752 | ␊ |
753 | ␉␉␉␉if (gOverrideKernel && kernelCacheFile[0] == 0)␊ |
754 | ␉␉␉␉{␊ |
755 | ␉␉␉␉␉DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);␊ |
756 | ␉␉␉␉␉useKernelCache = false;␊ |
757 | ␉␉␉␉␉break;␊ |
758 | ␉␉␉␉}␊ |
759 | ␊ |
760 | ␉␉␉␉if (gMKextName[0] != 0)␊ |
761 | ␉␉␉␉{␊ |
762 | ␉␉␉␉␉DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n", gMKextName);␊ |
763 | ␉␉␉␉␉useKernelCache = false;␊ |
764 | ␉␉␉␉␉break;␊ |
765 | ␉␉␉␉}␊ |
766 | ␊ |
767 | ␉␉␉␉if (gBootFileType != kBlockDeviceType)␊ |
768 | ␉␉␉␉{␊ |
769 | ␉␉␉␉␉useKernelCache = false;␊ |
770 | ␉␉␉␉}␊ |
771 | ␊ |
772 | ␉␉␉} while(0);␊ |
773 | ␉␉}␊ |
774 | ␉␉else␊ |
775 | ␉␉{␊ |
776 | ␉␉␉DBG("Kernel Cache using disabled by user.\n");␊ |
777 | ␉␉}␊ |
778 | ␊ |
779 | ␉␉do␊ |
780 | ␉␉{␊ |
781 | ␉␉␉if (useKernelCache)␊ |
782 | ␉␉␉{␊ |
783 | ␉␉␉␉ret = LoadKernelCache(kernelCacheFile, &binary);␊ |
784 | ␉␉␉␉if (ret >= 0)␊ |
785 | ␉␉␉␉{␊ |
786 | ␉␉␉␉␉break;␊ |
787 | ␉␉␉␉}␊ |
788 | ␉␉␉}␊ |
789 | ␊ |
790 | ␉␉␉bool bootFileWithDevice = false;␊ |
791 | ␉␉␉// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel␊ |
792 | ␉␉␉if (strncmp(bootInfo->bootFile, "bt(", sizeof("bt(") ) == 0 ||␊ |
793 | ␉␉␉␉strncmp(bootInfo->bootFile, "hd(", sizeof("hd(") ) == 0 ||␊ |
794 | ␉␉␉␉strncmp(bootInfo->bootFile, "rd(", sizeof("rd(") ) == 0)␊ |
795 | ␉␉␉{␊ |
796 | ␉␉␉␉bootFileWithDevice = true;␊ |
797 | ␉␉␉}␊ |
798 | ␊ |
799 | ␉␉␉// bootFile must start with a / if it not start with a device name␊ |
800 | ␉␉␉if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')␊ |
801 | ␉␉␉{␊ |
802 | ␉␉␉␉if ( MacOSVerCurrent < MacOSVer2Int("10.10") ) // Mavericks and older␊ |
803 | ␉␉␉␉{␊ |
804 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /␊ |
805 | ␉␉␉␉}␊ |
806 | ␉␉␉␉else␊ |
807 | ␉␉␉␉{␊ |
808 | ␉␉␉␉␉// Yosemite and newer␊ |
809 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite or El Capitan␊ |
810 | ␉␉␉␉}␊ |
811 | ␉␉␉}␊ |
812 | ␉␉␉else␊ |
813 | ␉␉␉{␊ |
814 | ␉␉␉␉strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));␊ |
815 | ␉␉␉}␊ |
816 | ␊ |
817 | ␉␉␉// Try to load kernel image from alternate locations on boot helper partitions.␊ |
818 | ␉␉␉ret = -1;␊ |
819 | ␉␉␉if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)␊ |
820 | ␉␉␉{␊ |
821 | ␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);␊ |
822 | ␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
823 | ␉␉␉␉if (ret == -1)␊ |
824 | ␉␉␉␉{␊ |
825 | ␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);␊ |
826 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
827 | ␉␉␉␉␉if (ret == -1)␊ |
828 | ␉␉␉␉␉{␊ |
829 | ␉␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);␊ |
830 | ␉␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
831 | ␉␉␉␉␉}␊ |
832 | ␉␉␉␉}␊ |
833 | ␉␉␉}␊ |
834 | ␉␉␉if (ret == -1)␊ |
835 | ␉␉␉{␊ |
836 | ␉␉␉␉// No alternate location found, using the original kernel image path.␊ |
837 | ␉␉␉␉strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));␊ |
838 | ␉␉␉}␊ |
839 | ␊ |
840 | ␉␉␉DBG("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);␊ |
841 | ␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
842 | ␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
843 | ␉␉␉{␊ |
844 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
845 | ␉␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
846 | ␉␉␉}␊ |
847 | ␉␉} while (0);␊ |
848 | ␊ |
849 | ␉␉clearActivityIndicator();␊ |
850 | ␊ |
851 | #if DEBUG␊ |
852 | ␉␉printf("Pausing...");␊ |
853 | ␉␉sleep(8);␊ |
854 | #endif␊ |
855 | ␊ |
856 | ␉␉if (ret <= 0)␊ |
857 | ␉␉{␊ |
858 | ␉␉␉printf("Can't find boot file: '%s'\n", bootFile);␊ |
859 | ␉␉␉sleep(1);␊ |
860 | ␊ |
861 | ␉␉␉if (gBootFileType == kNetworkDeviceType)␊ |
862 | ␉␉␉{␊ |
863 | ␉␉␉␉// Return control back to PXE. Don't unload PXE base code.␊ |
864 | ␉␉␉␉gUnloadPXEOnExit = false;␊ |
865 | ␉␉␉␉break;␊ |
866 | ␉␉␉}␊ |
867 | ␉␉␉pause();␊ |
868 | ␊ |
869 | ␉␉}␊ |
870 | ␉␉else␊ |
871 | ␉␉{␊ |
872 | ␉␉␉/* Won't return if successful. */␊ |
873 | ␉␉␉ret = ExecKernel(binary);␊ |
874 | ␉␉}␊ |
875 | ␉}␊ |
876 | ␊ |
877 | ␉// chainboot␊ |
878 | ␉if (status == 1)␊ |
879 | ␉{␊ |
880 | ␉␉// if we are already in graphics-mode,␊ |
881 | ␉␉if (getVideoMode() == GRAPHICS_MODE)␊ |
882 | ␉␉{␊ |
883 | ␉␉␉setVideoMode( VGA_TEXT_MODE ); // switch back to text mode.␊ |
884 | ␉␉}␊ |
885 | ␉}␊ |
886 | ␊ |
887 | ␉if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)␊ |
888 | ␉{␊ |
889 | ␉␉nbpUnloadBaseCode();␊ |
890 | ␉}␊ |
891 | ␊ |
892 | #if DEBUG_INTERRUPTS␊ |
893 | ␉if (interruptsAvailable)␊ |
894 | ␉{␊ |
895 | ␉␉DisableInterrupts();␊ |
896 | ␉}␊ |
897 | #endif␊ |
898 | ␊ |
899 | }␊ |
900 | ␊ |
901 | // =========================================================================␊ |
902 | //␊ |
903 | void setupBooterArgs()␊ |
904 | {␊ |
905 | ␉bool KPRebootOption␉= false;␊ |
906 | ␉bool HiDPIOption␉= false;␊ |
907 | ␉bool FlagBlackOption␉= false;␊ |
908 | ␊ |
909 | ␊ |
910 | ␉// OS X Mountain Lion 10.8␊ |
911 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.8") ) // Mountain Lion and Up!␊ |
912 | ␉{␊ |
913 | ␉␉// cparm␊ |
914 | ␉␉getBoolForKey(kRebootOnPanic, &KPRebootOption, &bootInfo->chameleonConfig);␊ |
915 | ␉␉if ( KPRebootOption )␊ |
916 | ␉␉{␊ |
917 | ␉␉␉bootArgs->flags |= kBootArgsFlagRebootOnPanic;␊ |
918 | ␉␉}␊ |
919 | ␊ |
920 | ␉␉// cparm␊ |
921 | ␉␉getBoolForKey(kEnableHiDPI, &HiDPIOption, &bootInfo->chameleonConfig);␊ |
922 | ␊ |
923 | ␉␉if ( HiDPIOption )␊ |
924 | ␉␉{␊ |
925 | ␉␉␉bootArgs->flags |= kBootArgsFlagHiDPI;␊ |
926 | ␉␉}␊ |
927 | ␉}␊ |
928 | ␊ |
929 | ␉// OS X Yosemite 10.10␊ |
930 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.10") ) // Yosemite and Up!␊ |
931 | ␉{␊ |
932 | ␉␉// Pike R. Alpha␊ |
933 | ␉␉getBoolForKey(kBlackMode, &FlagBlackOption, &bootInfo->chameleonConfig);␊ |
934 | ␉␉if ( FlagBlackOption )␊ |
935 | ␉␉{␊ |
936 | ␉␉␉// bootArgs->flags |= kBootArgsFlagBlack;␊ |
937 | ␉␉␉bootArgs->flags |= kBootArgsFlagBlackBg; // Micky1979␊ |
938 | ␉␉}␊ |
939 | ␉}␊ |
940 | ␊ |
941 | ␉// OS X El Capitan 10.11␊ |
942 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.11") ) // El Capitan and Up!␊ |
943 | ␉{␊ |
944 | ␉␉// ErmaC␊ |
945 | ␉␉verbose("\n");␊ |
946 | ␉␉int␉csrValue;␊ |
947 | ␊ |
948 | #if 0␊ |
949 | ␉␉/*␊ |
950 | ␉␉ * A special BootArgs flag "kBootArgsFlagCSRBoot"␊ |
951 | ␉␉ * is set in the Recovery or Installation environment.␊ |
952 | ␉␉ * This flag is kind of overkill by turning off all the protections␊ |
953 | ␉␉ */␊ |
954 | ␊ |
955 | ␉␉if (isRecoveryHD)␊ |
956 | ␉␉{␊ |
957 | ␉␉␉// SIP can be controlled with or without FileNVRAM.kext (Pike R. Alpha)␊ |
958 | ␉␉␉bootArgs->flags␉|=␉(kBootArgsFlagCSRActiveConfig + kBootArgsFlagCSRConfigMode + kBootArgsFlagCSRBoot);␊ |
959 | ␉␉}␊ |
960 | #endif␊ |
961 | ␊ |
962 | ␉␉bootArgs->flags␉␉|= kBootArgsFlagCSRActiveConfig;␊ |
963 | ␊ |
964 | ␊ |
965 | ␉␉// Set limit to 7bit␊ |
966 | ␉␉if ( getIntForKey(kCsrActiveConfig, &csrValue, &bootInfo->chameleonConfig) && (csrValue >= 0 && csrValue <= 127) )␊ |
967 | ␉␉{␊ |
968 | ␉␉␉bootArgs->csrActiveConfig␉= csrValue;␊ |
969 | ␊ |
970 | ␉␉}␊ |
971 | ␉␉else␊ |
972 | ␉␉{␊ |
973 | ␉␉␉// zenith432␊ |
974 | ␉␉␉bootArgs->csrActiveConfig␉= 0x67;␊ |
975 | ␊ |
976 | ␉␉}␊ |
977 | ␊ |
978 | // ===============================================================================␊ |
979 | ␉␉verbose("CsrActiveConfig set to 0x%x\n", bootArgs->csrActiveConfig);␊ |
980 | ␊ |
981 | // ===============================================================================␊ |
982 | ␊ |
983 | ␉␉bootArgs->csrCapabilities␉= CSR_VALID_FLAGS;␊ |
984 | ␉␉bootArgs->boot_SMC_plimit␉= 0;␊ |
985 | ␉}␊ |
986 | }␊ |
987 | ␊ |
988 | // =========================================================================␊ |
989 | ␊ |
990 | // =========================================================================␊ |
991 | ␊ |
992 | /*!␊ |
993 | ␉Selects a new BIOS device, taking care to update the global state appropriately.␊ |
994 | */␊ |
995 | /*␊ |
996 | static void selectBiosDevice(void)␊ |
997 | {␊ |
998 | ␉struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
999 | ␉CacheReset();␊ |
1000 | ␉diskFreeMap(oldMap);␊ |
1001 | ␉oldMap = NULL;␊ |
1002 | ␊ |
1003 | ␉int dev = selectAlternateBootDevice(gBIOSDev);␊ |
1004 | ␊ |
1005 | ␉BVRef bvchain = scanBootVolumes(dev, 0);␊ |
1006 | ␉BVRef bootVol = selectBootVolume(bvchain);␊ |
1007 | ␉gBootVolume = bootVol;␊ |
1008 | ␉setRootVolume(bootVol);␊ |
1009 | ␉gBIOSDev = dev;␊ |
1010 | }␊ |
1011 | */␊ |
1012 | ␊ |
1013 | // =========================================================================␊ |
1014 | bool checkOSVersion(const char *version)␊ |
1015 | {␊ |
1016 | ␉if ( '\0' != version[4] )␊ |
1017 | ␉{␊ |
1018 | ␉␉return !memcmp(&gMacOSVersion[0], &version[0], 5);␊ |
1019 | ␉}␊ |
1020 | ␉else␊ |
1021 | ␉{␊ |
1022 | ␉␉return !memcmp(&gMacOSVersion[0], &version[0], 4);␊ |
1023 | ␉}␊ |
1024 | }␊ |
1025 | ␊ |
1026 | uint32_t getMacOSVerCurrent()␊ |
1027 | {␊ |
1028 | ␉MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);␊ |
1029 | ␉return MacOSVerCurrent;␊ |
1030 | }␊ |
1031 | ␊ |
1032 | // =========================================================================␊ |
1033 | unsigned long Adler32(unsigned char *buf, long len)␊ |
1034 | {␊ |
1035 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
1036 | #define NMAX 5000␊ |
1037 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
1038 | ␊ |
1039 | #define DO1(buf, i)␉{s1 += buf[i]; s2 += s1;}␊ |
1040 | #define DO2(buf, i)␉DO1(buf, i); DO1(buf, i + 1);␊ |
1041 | #define DO4(buf, i)␉DO2(buf, i); DO2(buf, i + 2);␊ |
1042 | #define DO8(buf, i)␉DO4(buf, i); DO4(buf, i + 4);␊ |
1043 | #define DO16(buf)␉DO8(buf, 0); DO8(buf, 8);␊ |
1044 | ␊ |
1045 | ␉int k;␊ |
1046 | ␊ |
1047 | ␉unsigned long s1 = 1; // adler & 0xffff;␊ |
1048 | ␉unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
1049 | ␉unsigned long result;␊ |
1050 | ␊ |
1051 | ␉while (len > 0) {␊ |
1052 | ␉␉k = len < NMAX ? len : NMAX;␊ |
1053 | ␉␉len -= k;␊ |
1054 | ␉␉while (k >= 16)␊ |
1055 | ␉␉{␊ |
1056 | ␉␉␉DO16(buf);␊ |
1057 | ␉␉␉buf += 16;␊ |
1058 | ␉␉␉k -= 16;␊ |
1059 | ␉␉}␊ |
1060 | ␊ |
1061 | ␉␉if (k != 0)␊ |
1062 | ␉␉{␊ |
1063 | ␉␉␉do␊ |
1064 | ␉␉␉{␊ |
1065 | ␉␉␉␉s1 += *buf++;␊ |
1066 | ␉␉␉␉s2 += s1;␊ |
1067 | ␉␉␉} while (--k);␊ |
1068 | ␉␉}␊ |
1069 | ␊ |
1070 | ␉␉s1 %= BASE;␊ |
1071 | ␉␉s2 %= BASE;␊ |
1072 | ␉}␊ |
1073 | ␊ |
1074 | ␉result = (s2 << 16) | s1;␊ |
1075 | ␊ |
1076 | ␉return OSSwapHostToBigInt32(result);␊ |
1077 | }␊ |
1078 | |