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