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