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