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 | /* recovery or installer ? */␊ |
85 | bool isInstaller;␊ |
86 | bool isRecoveryHD;␊ |
87 | bool isMacOSXUpgrade;␊ |
88 | bool isOSXUpgrade;␊ |
89 | ␊ |
90 | #if DEBUG_INTERRUPTS␊ |
91 | ␉static int␉interruptsAvailable = 0;␊ |
92 | #endif␊ |
93 | ␊ |
94 | static bool␉gUnloadPXEOnExit = false;␊ |
95 | ␊ |
96 | static char␉gCacheNameAdler[64 + 256];␊ |
97 | char␉␉*gPlatformName = gCacheNameAdler;␊ |
98 | ␊ |
99 | char␉␉gRootDevice[ROOT_DEVICE_SIZE];␊ |
100 | char␉␉gMKextName[512];␊ |
101 | int␉␉bvCount = 0;␊ |
102 | int␉␉gDeviceCount = 0;␊ |
103 | //int␉␉menucount = 0;␊ |
104 | long␉␉gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
105 | ␊ |
106 | BVRef␉␉bvr;␊ |
107 | BVRef␉␉menuBVR;␊ |
108 | BVRef␉␉bvChain;␊ |
109 | ␊ |
110 | static unsigned long␉Adler32(unsigned char *buffer, long length);␊ |
111 | //static void␉␉␉selectBiosDevice(void);␊ |
112 | ␊ |
113 | /** options.c **/␊ |
114 | extern char *msgbuf;␊ |
115 | void showTextBuffer(char *buf, int size);␊ |
116 | ␊ |
117 | //==========================================================================␊ |
118 | // Zero the BSS.␊ |
119 | ␊ |
120 | static void zeroBSS(void)␊ |
121 | {␊ |
122 | ␉extern char bss_start __asm("section$start$__DATA$__bss");␊ |
123 | ␉extern char bss_end __asm("section$end$__DATA$__bss");␊ |
124 | ␉extern char common_start __asm("section$start$__DATA$__common");␊ |
125 | ␉extern char common_end __asm("section$end$__DATA$__common");␊ |
126 | ␊ |
127 | ␉bzero(&bss_start, (&bss_end - &bss_start));␊ |
128 | ␉bzero(&common_start, (&common_end - &common_start));␊ |
129 | }␊ |
130 | ␊ |
131 | //==========================================================================␊ |
132 | // Malloc error function␊ |
133 | ␊ |
134 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
135 | {␊ |
136 | ␉stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",␊ |
137 | ␉␉␉␉␉␉␉␉␉ (unsigned)addr, (unsigned)size, file, line);␊ |
138 | }␊ |
139 | ␊ |
140 | //==========================================================================␊ |
141 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
142 | //␊ |
143 | void initialize_runtime(void)␊ |
144 | {␊ |
145 | ␉zeroBSS();␊ |
146 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
147 | }␊ |
148 | ␊ |
149 | // =========================================================================␊ |
150 | ␊ |
151 | ␊ |
152 | ␊ |
153 | //==========================================================================␊ |
154 | // ExecKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
155 | ␊ |
156 | static int ExecKernel(void *binary)␊ |
157 | {␊ |
158 | ␉int␉␉␉ret;␊ |
159 | ␉entry_t␉␉kernelEntry;␊ |
160 | ␊ |
161 | ␉bootArgs->kaddr = bootArgs->ksize = 0;␊ |
162 | ␊ |
163 | // ===============================================================================␊ |
164 | ␊ |
165 | ␉gMacOSVersion[0] = 0;␊ |
166 | ␉// TODO identify sierra as macOS␊ |
167 | ␉verbose("Booting on %s %s (%s)\n", (MacOSVerCurrent < MacOSVer2Int("10.8")) ? "Mac OS X" : "OS X", gBootVolume->OSFullVer, gBootVolume->OSBuildVer );␊ |
168 | ␊ |
169 | ␉setupBooterArgs();␊ |
170 | ␊ |
171 | // ===============================================================================␊ |
172 | ␊ |
173 | ␉execute_hook("ExecKernel", (void *)binary, NULL, NULL, NULL);␊ |
174 | ␊ |
175 | ␉ret = DecodeKernel(binary,␊ |
176 | ␉␉␉␉␉ &kernelEntry,␊ |
177 | ␉␉␉␉␉ (char **) &bootArgs->kaddr,␊ |
178 | ␉␉␉␉␉ (int *)&bootArgs->ksize);␊ |
179 | ␊ |
180 | ␉if ( ret != 0 )␊ |
181 | ␉{␊ |
182 | ␉␉printf("Decoding kernel failed.\n");␊ |
183 | ␉␉pause();␊ |
184 | ␉␉return ret;␊ |
185 | ␉}␊ |
186 | ␊ |
187 | ␉// Reserve space for boot args␊ |
188 | ␉reserveKernBootStruct();␊ |
189 | ␊ |
190 | ␉// Notify modules that the kernel has been decoded␊ |
191 | ␉execute_hook("DecodedKernel", (void *)binary, (void *)bootArgs->kaddr, (void *)bootArgs->ksize, NULL);␊ |
192 | ␊ |
193 | ␉setupFakeEfi();␊ |
194 | ␊ |
195 | ␉// Load boot drivers from the specifed root path.␊ |
196 | ␉//if (!gHaveKernelCache)␊ |
197 | ␉{␊ |
198 | ␉␉LoadDrivers("/");␊ |
199 | ␉}␊ |
200 | ␊ |
201 | ␉execute_hook("DriversLoaded", (void *)binary, NULL, NULL, NULL);␊ |
202 | ␊ |
203 | ␉clearActivityIndicator();␊ |
204 | ␊ |
205 | ␉if (gErrors)␊ |
206 | ␉{␊ |
207 | ␉␉printf("Errors encountered while starting up the computer.\n");␊ |
208 | ␉␉printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
209 | ␉␉sleep(kBootErrorTimeout);␊ |
210 | ␉}␊ |
211 | ␊ |
212 | ␉md0Ramdisk();␊ |
213 | ␊ |
214 | ␉// Cleanup the PXE base code.␊ |
215 | ␊ |
216 | ␉if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )␊ |
217 | ␉{␊ |
218 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
219 | ␉␉{␊ |
220 | ␉␉␉printf("nbpUnloadBaseCode error %d\n", (int)ret);␊ |
221 | ␉␉␉sleep(2);␊ |
222 | ␉␉}␊ |
223 | ␉}␊ |
224 | ␊ |
225 | ␉bool dummyVal;␊ |
226 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal)␊ |
227 | ␉{␊ |
228 | ␉␉showTextBuffer(msgbuf, strlen(msgbuf));␊ |
229 | ␉}␊ |
230 | ␊ |
231 | ␉usb_loop();␊ |
232 | ␊ |
233 | #if DEBUG␊ |
234 | ␉if (interruptsAvailable) ShowInterruptCounters();␊ |
235 | #endif␊ |
236 | ␊ |
237 | ␉// If we were in text mode, switch to graphics mode.␊ |
238 | ␉// This will draw the boot graphics unless we are in␊ |
239 | ␉// verbose mode.␊ |
240 | ␉if (gVerboseMode)␊ |
241 | ␉{␊ |
242 | ␉␉setVideoMode(GRAPHICS_MODE, 0);␊ |
243 | ␉}␊ |
244 | ␉else␊ |
245 | ␉{␊ |
246 | ␉␉drawBootGraphics();␊ |
247 | ␉}␊ |
248 | ␊ |
249 | ␉DBG("Starting Darwin/%s [%s]\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64", gDarwinBuildVerStr);␊ |
250 | ␉DBG("Boot Args: %s\n", bootArgs->CommandLine);␊ |
251 | ␊ |
252 | ␉setupBooterLog();␊ |
253 | ␊ |
254 | ␉finalizeBootStruct();␊ |
255 | ␊ |
256 | ␉// Jump to kernel's entry point. There's no going back now.␊ |
257 | ␉if ( MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
258 | ␉{␊ |
259 | ␉␉// Notify modules that the kernel is about to be started␊ |
260 | ␉␉execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgsLegacy, NULL, NULL);␊ |
261 | ␊ |
262 | ␉␉startprog( kernelEntry, bootArgsLegacy );␊ |
263 | ␉}␊ |
264 | ␉else␊ |
265 | ␉{␊ |
266 | ␉␉// Notify modules that the kernel is about to be started␊ |
267 | ␉␉execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgs, NULL, NULL);␊ |
268 | ␊ |
269 | ␉␉// Masking out so that Lion doesn't doublefault␊ |
270 | ␉␉outb(0x21, 0xff);␉/* Maskout all interrupts Pic1 */␊ |
271 | ␉␉outb(0xa1, 0xff);␉/* Maskout all interrupts Pic2 */␊ |
272 | ␊ |
273 | ␉␉startprog( kernelEntry, bootArgs );␊ |
274 | ␉}␊ |
275 | ␊ |
276 | ␉// Not reached␊ |
277 | ␉__builtin_unreachable();␊ |
278 | }␊ |
279 | ␊ |
280 | //==========================================================================␊ |
281 | // LoadKernelCache - Try to load Kernel Cache.␊ |
282 | // return the length of the loaded cache file or -1 on error␊ |
283 | long LoadKernelCache(const char *cacheFile, void **binary)␊ |
284 | {␊ |
285 | ␉char␉␉kernelCacheFile[512];␊ |
286 | ␉char␉␉kernelCachePath[512];␊ |
287 | ␉long␉␉flags, ret=-1;␊ |
288 | ␉unsigned long␉adler32 = 0;␊ |
289 | ␉u_int32_t time, cachetime, kerneltime, exttime;␊ |
290 | ␊ |
291 | ␉if((gBootMode & kBootModeSafe) != 0)␊ |
292 | ␉{␊ |
293 | ␉␉DBG("Kernel Cache ignored.\n");␊ |
294 | ␉␉return -1;␊ |
295 | ␉}␊ |
296 | ␊ |
297 | ␉// Use specify kernel cache file if not empty␊ |
298 | ␉if (cacheFile[0] != 0)␊ |
299 | ␉{␊ |
300 | ␉␉strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));␊ |
301 | ␉␉verbose("Specified kernel cache file path: %s\n", cacheFile);␊ |
302 | ␉}␊ |
303 | ␉else␊ |
304 | ␉{␊ |
305 | ␉␉// Leopard prelink kernel cache file␊ |
306 | ␉␉if ( MacOSVerCurrent >= MacOSVer2Int("10.4") && MacOSVerCurrent <= MacOSVer2Int("10.5") ) // OSX is 10.4 or 10.5␊ |
307 | ␉␉{␊ |
308 | ␉␉␉// Reset cache name.␊ |
309 | ␉␉␉bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
310 | ␉␉␉snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
311 | ␉␉␉adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
312 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);␊ |
313 | ␉␉␉verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);␊ |
314 | ␊ |
315 | ␉␉}␊ |
316 | ␉␉// Snow Leopard prelink kernel cache file␊ |
317 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.6") && MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
318 | ␉␉{␊ |
319 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",␊ |
320 | ␉␉␉␉(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");␊ |
321 | ␊ |
322 | ␉␉␉int␉␉lnam = strlen(kernelCacheFile) + 9; //with adler32␊ |
323 | ␉␉␉char␉␉*name;␊ |
324 | ␉␉␉u_int32_t␉prev_time = 0;␊ |
325 | ␊ |
326 | ␉␉␉struct dirstuff *cacheDir = opendir(kDefaultCachePathSnow);␊ |
327 | ␊ |
328 | ␉␉␉/* TODO: handle error? */␊ |
329 | ␉␉␉if (cacheDir)␊ |
330 | ␉␉␉{␊ |
331 | ␉␉␉␉while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)␊ |
332 | ␉␉␉␉{␊ |
333 | ␉␉␉␉␉if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time␊ |
334 | ␉␉␉␉␉␉&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))␊ |
335 | ␉␉␉␉␉{␊ |
336 | ␉␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);␊ |
337 | ␉␉␉␉␉␉prev_time = time;␊ |
338 | ␉␉␉␉␉}␊ |
339 | ␉␉␉␉}␊ |
340 | ␉␉␉␉verbose("Kernel Cache file path (Mac OS X 10.6): %s\n", kernelCacheFile);␊ |
341 | ␉␉␉}␊ |
342 | ␉␉␉closedir(cacheDir);␊ |
343 | ␉␉}␊ |
344 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.9") )␊ |
345 | ␉␉{␊ |
346 | ␉␉␉// Lion, Mountain Lion␊ |
347 | ␉␉␉// for 10.7 10.8␊ |
348 | ␉␉␉if (isMacOSXUpgrade)␊ |
349 | ␉␉␉{␊ |
350 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/Mac OS X Install Data/");␊ |
351 | ␉␉␉}␊ |
352 | ␉␉␉else if (isInstaller)␊ |
353 | ␉␉␉{␊ |
354 | ␉␉␉␉if (MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.8") )␊ |
355 | ␉␉␉␉{␊ |
356 | ␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kLionInstallerDataFolder);␊ |
357 | ␉␉␉␉}␊ |
358 | ␉␉␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.8") && MacOSVerCurrent < MacOSVer2Int("10.9") )␊ |
359 | ␉␉␉␉{␊ |
360 | ␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kMLionInstallerDataFolder);␊ |
361 | ␉␉␉␉}␊ |
362 | ␊ |
363 | ␉␉␉}␊ |
364 | ␉␉␉else if (isRecoveryHD)␊ |
365 | ␉␉␉{␊ |
366 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);␊ |
367 | ␉␉␉}␊ |
368 | ␉␉␉else␊ |
369 | ␉␉␉{␊ |
370 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);␊ |
371 | ␉␉␉}␊ |
372 | ␊ |
373 | ␉␉␉verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);␊ |
374 | ␊ |
375 | ␉␉}␊ |
376 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.9") && MacOSVerCurrent < MacOSVer2Int("10.10") )␊ |
377 | ␉␉{␊ |
378 | ␉␉␉// Mavericks prelinked cache file␊ |
379 | ␉␉␉// for 10.9␊ |
380 | ␉␉␉if (isOSXUpgrade)␊ |
381 | ␉␉␉{␊ |
382 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");␊ |
383 | ␉␉␉}␊ |
384 | ␉␉␉else if (isInstaller)␊ |
385 | ␉␉␉{␊ |
386 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);␊ |
387 | ␉␉␉}␊ |
388 | ␉␉␉else if (isRecoveryHD)␊ |
389 | ␉␉␉{␊ |
390 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);␊ |
391 | ␉␉␉}␊ |
392 | ␉␉␉else␊ |
393 | ␉␉␉{␊ |
394 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);␊ |
395 | ␉␉␉}␊ |
396 | ␊ |
397 | ␉␉␉verbose("Kernel Cache file path (OS X 10.9): %s\n", kernelCacheFile);␊ |
398 | ␊ |
399 | ␉␉}␊ |
400 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.10") && MacOSVerCurrent < MacOSVer2Int("10.11") )␊ |
401 | ␉␉{␊ |
402 | ␉␉␉// Yosemite prelink kernel cache file␊ |
403 | ␉␉␉// for 10.10 and 10.11␊ |
404 | ␉␉␉if (isOSXUpgrade)␊ |
405 | ␉␉␉{␊ |
406 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");␊ |
407 | ␉␉␉}␊ |
408 | ␉␉␉else if (isInstaller)␊ |
409 | ␉␉␉{␊ |
410 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);␊ |
411 | ␉␉␉}␊ |
412 | ␉␉␉else if (isRecoveryHD)␊ |
413 | ␉␉␉{␊ |
414 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);␊ |
415 | ␉␉␉}␊ |
416 | ␉␉␉else␊ |
417 | ␉␉␉{␊ |
418 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);␊ |
419 | ␉␉␉}␊ |
420 | ␊ |
421 | ␉␉␉verbose("Kernel Cache file path (OS X 10.10): %s\n", kernelCacheFile);␊ |
422 | ␉␉}␊ |
423 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.11") )␊ |
424 | ␉␉{␊ |
425 | ␉␉␉// El Capitan on prelinked kernel cache file␊ |
426 | ␉␉␉// for 10.10 and 10.11␊ |
427 | ␉␉␉if (isOSXUpgrade)␊ |
428 | ␉␉␉{␊ |
429 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", "/OS X Install Data/");␊ |
430 | ␉␉␉}␊ |
431 | ␉␉␉else if (isInstaller)␊ |
432 | ␉␉␉{␊ |
433 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheInstallerNew);␊ |
434 | ␉␉␉}␊ |
435 | ␉␉␉else if (isRecoveryHD)␊ |
436 | ␉␉␉{␊ |
437 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheRecoveryHD);␊ |
438 | ␉␉␉}␊ |
439 | ␉␉␉else␊ |
440 | ␉␉␉{␊ |
441 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);␊ |
442 | ␉␉␉}␊ |
443 | ␊ |
444 | ␉␉␉verbose("Kernel Cache file path (OS X 10.11 and newer): %s\n", kernelCacheFile);␊ |
445 | ␉␉}␊ |
446 | ␉}␊ |
447 | ␊ |
448 | ␉// Check if the kernel cache file exists␊ |
449 | ␉ret = -1;␊ |
450 | ␊ |
451 | ␉if (gBootVolume->flags & kBVFlagBooter)␊ |
452 | ␉{␊ |
453 | ␉␉if (isRecoveryHD)␊ |
454 | ␉␉{␊ |
455 | ␉␉␉strncpy(kernelCachePath, "/com.apple.recovery.boot/prelinkedkernel", sizeof(kernelCachePath) );␊ |
456 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
457 | ␊ |
458 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
459 | ␉␉␉{␊ |
460 | ␉␉␉␉strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof(kernelCachePath) );␊ |
461 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
462 | ␊ |
463 | ␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
464 | ␉␉␉␉{␊ |
465 | ␉␉␉␉␉ret = -1;␊ |
466 | ␉␉␉␉}␊ |
467 | ␉␉␉}␊ |
468 | ␉␉}␊ |
469 | ␉␉else if (isInstaller)␊ |
470 | ␉␉{␊ |
471 | ␉␉␉strncpy(kernelCachePath, "/.IABootFiles/prelinkedkernel", sizeof(kernelCachePath) );␊ |
472 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
473 | ␊ |
474 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
475 | ␉␉␉{␊ |
476 | ␉␉␉␉strncpy(kernelCachePath, "/.IABootFiles/kernelcache", sizeof(kernelCachePath) );␊ |
477 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
478 | ␊ |
479 | ␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
480 | ␉␉␉␉{␊ |
481 | ␉␉␉␉␉ret = -1;␊ |
482 | ␉␉␉␉}␊ |
483 | ␉␉␉}␊ |
484 | ␉␉}␊ |
485 | ␉␉else if (isMacOSXUpgrade)␊ |
486 | ␉␉{␊ |
487 | ␉␉␉strncpy(kernelCachePath, "/Mac OS X Install Data/kernelcache", sizeof(kernelCachePath) );␊ |
488 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
489 | ␊ |
490 | ␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
491 | ␉␉␉{␊ |
492 | ␉␉␉␉ret = -1;␊ |
493 | ␉␉␉}␊ |
494 | ␉␉}␊ |
495 | ␉␉else if (isOSXUpgrade)␊ |
496 | ␉␉{␊ |
497 | ␉␉␉strncpy(kernelCachePath, "/OS X Install Data/prelinkedkernel", sizeof(kernelCachePath) );␊ |
498 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
499 | ␊ |
500 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
501 | ␉␉␉{␊ |
502 | ␉␉␉␉strncpy(kernelCachePath, "/OS X Install Data/kernelcache", sizeof(kernelCachePath) );␊ |
503 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
504 | ␊ |
505 | ␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
506 | ␉␉␉␉{␊ |
507 | ␉␉␉␉␉ret = -1;␊ |
508 | ␉␉␉␉}␊ |
509 | ␉␉␉}␊ |
510 | ␉␉}␊ |
511 | ␉␉else␊ |
512 | ␉␉{␊ |
513 | ␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);␊ |
514 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
515 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
516 | ␉␉␉{␊ |
517 | ␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);␊ |
518 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
519 | ␊ |
520 | ␉␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
521 | ␉␉␉␉{␊ |
522 | ␉␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);␊ |
523 | ␉␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
524 | ␊ |
525 | ␉␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
526 | ␉␉␉␉␉{␊ |
527 | ␉␉␉␉␉␉ret = -1;␊ |
528 | ␉␉␉␉␉}␊ |
529 | ␉␉␉␉}␊ |
530 | ␉␉␉}␊ |
531 | ␉␉}␊ |
532 | ␉}␊ |
533 | ␊ |
534 | ␉// If not found, use the original kernel cache path.␊ |
535 | ␉if (ret == -1)␊ |
536 | ␉{␊ |
537 | ␉␉strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));␊ |
538 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
539 | ␊ |
540 | ␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
541 | ␉␉{␊ |
542 | ␉␉␉ret = -1;␊ |
543 | ␉␉}␊ |
544 | ␉}␊ |
545 | ␊ |
546 | ␉// Exit if kernel cache file wasn't found␊ |
547 | ␉if (ret == -1)␊ |
548 | ␉{␊ |
549 | ␉␉DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);␊ |
550 | ␉␉return -1;␊ |
551 | ␉}␊ |
552 | ␊ |
553 | ␉if ( !isInstaller && !isRecoveryHD && !isMacOSXUpgrade && !isOSXUpgrade )␊ |
554 | ␉{␊ |
555 | ␉␉// Check if the kernel cache file is more recent (mtime)␊ |
556 | ␉␉// than the kernel file or the S/L/E directory␊ |
557 | ␉␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
558 | ␊ |
559 | ␉␉// Check if the kernel file is more recent than the cache file␊ |
560 | ␉␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))␊ |
561 | ␉␉{␊ |
562 | ␉␉␉DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);␊ |
563 | ␉␉␉return -1;␊ |
564 | ␉␉}␊ |
565 | ␊ |
566 | ␉␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
567 | ␊ |
568 | ␉␉// Check if the S/L/E directory time is more recent than the cache file␊ |
569 | ␉␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))␊ |
570 | ␉␉{␊ |
571 | ␉␉␉DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);␊ |
572 | ␉␉␉return -1;␊ |
573 | ␉␉}␊ |
574 | ␉}␊ |
575 | ␊ |
576 | ␉// Since the kernel cache file exists and is the most recent try to load it␊ |
577 | ␉DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);␊ |
578 | ␊ |
579 | ␉ret = LoadThinFatFile(kernelCachePath, binary);␊ |
580 | ␉return ret; // ret contain the length of the binary␊ |
581 | }␊ |
582 | ␊ |
583 | //==========================================================================␊ |
584 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
585 | // before. Multiboot does its own runtime initialization, does some of its␊ |
586 | // own things, and then calls common_boot.␊ |
587 | void boot(int biosdev)␊ |
588 | {␊ |
589 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
590 | ␉// Note: malloc_init(), called via initialize_runtime() writes␊ |
591 | ␉// memory >= 1Mb, so A20 must be enabled before calling it. - zenith432␊ |
592 | ␉zeroBSS();␊ |
593 | ␉enableA20();␊ |
594 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
595 | ␉common_boot(biosdev);␊ |
596 | }␊ |
597 | ␊ |
598 | //==========================================================================␊ |
599 | // The 'main' function for the booter. Called by boot0 when booting␊ |
600 | // from a block device, or by the network booter.␊ |
601 | //␊ |
602 | // arguments:␊ |
603 | //␉ biosdev - Value passed from boot1/NBP to specify the device␊ |
604 | //␉␉␉ that the booter was loaded from.␊ |
605 | //␊ |
606 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
607 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
608 | // next boot device on its list.␊ |
609 | void common_boot(int biosdev)␊ |
610 | {␊ |
611 | ␉bool␉ ␉␉quiet;␊ |
612 | ␉bool␉ ␉␉firstRun = true;␊ |
613 | ␉bool␉ ␉␉instantMenu;␊ |
614 | ␉bool␉ ␉␉rescanPrompt;␊ |
615 | ␉int␉␉␉status;␊ |
616 | ␉unsigned int␉␉allowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;␊ |
617 | ␉unsigned int␉␉denyBVFlags = kBVFlagEFISystem;␊ |
618 | ␊ |
619 | ␉// Set reminder to unload the PXE base code. Neglect to unload␊ |
620 | ␉// the base code will result in a hang or kernel panic.␊ |
621 | ␉gUnloadPXEOnExit = true;␊ |
622 | ␊ |
623 | ␉// Record the device that the booter was loaded from.␊ |
624 | ␉gBIOSDev = biosdev & kBIOSDevMask;␊ |
625 | ␊ |
626 | ␉// Initialize boot-log␊ |
627 | ␉initBooterLog();␊ |
628 | ␊ |
629 | #if DEBUG_INTERRUPTS␊ |
630 | ␉// Enable interrupts␊ |
631 | ␉interruptsAvailable = SetupInterrupts();␊ |
632 | ␉if (interruptsAvailable)␊ |
633 | ␉{␊ |
634 | ␉␉EnableInterrupts();␊ |
635 | ␉}␊ |
636 | #endif␊ |
637 | ␊ |
638 | ␉// Initialize boot info structure.␊ |
639 | ␉initKernBootStruct();␊ |
640 | ␊ |
641 | ␉// Setup VGA text mode.␊ |
642 | ␉// Not sure if it is safe to call setVideoMode() before the␊ |
643 | ␉// config table has been loaded. Call video_mode() instead.␊ |
644 | #if DEBUG␊ |
645 | ␉printf("before video_mode\n");␊ |
646 | #endif␊ |
647 | ␉video_mode( 2 ); // 80x25 mono text mode.␊ |
648 | #if DEBUG␊ |
649 | ␉printf("after video_mode\n");␊ |
650 | #endif␊ |
651 | ␊ |
652 | ␉// Scan and record the system's hardware information.␊ |
653 | ␉scan_platform();␊ |
654 | ␊ |
655 | ␉// First get info for boot volume.␊ |
656 | ␉scanBootVolumes(gBIOSDev, 0);␊ |
657 | ␉bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
658 | ␉setBootGlobals(bvChain);␊ |
659 | ␊ |
660 | ␉// Load boot.plist config file␊ |
661 | ␉status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);␊ |
662 | ␊ |
663 | ␉if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)␊ |
664 | ␉{␊ |
665 | ␉␉gBootMode |= kBootModeQuiet;␊ |
666 | ␉}␊ |
667 | ␊ |
668 | ␉// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
669 | ␉if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)␊ |
670 | ␉{␊ |
671 | ␉␉firstRun = false;␊ |
672 | ␉}␊ |
673 | ␊ |
674 | ␉// Loading preboot ramdisk if exists.␊ |
675 | ␉loadPrebootRAMDisk();␊ |
676 | ␊ |
677 | ␉// Disable rescan option by default␊ |
678 | ␉gEnableCDROMRescan = false;␊ |
679 | ␊ |
680 | ␉// Enable it with Rescan=y in system config␊ |
681 | ␉if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)␉&& gEnableCDROMRescan)␊ |
682 | ␉{␊ |
683 | ␉␉gEnableCDROMRescan = true;␊ |
684 | ␉}␊ |
685 | ␊ |
686 | ␉// Disable rescanPrompt option by default␊ |
687 | ␉rescanPrompt = false;␊ |
688 | ␊ |
689 | ␉// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
690 | ␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))␊ |
691 | ␉{␊ |
692 | ␉␉gEnableCDROMRescan = promptForRescanOption();␊ |
693 | ␉}␊ |
694 | ␊ |
695 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
696 | ␉if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)␊ |
697 | ␉{␊ |
698 | ␉␉gScanSingleDrive = true;␊ |
699 | ␉}␊ |
700 | ␊ |
701 | ␉// Create a list of partitions on device(s).␊ |
702 | ␉if (gScanSingleDrive)␊ |
703 | ␉{␊ |
704 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
705 | ␉}␊ |
706 | ␉else␊ |
707 | ␉{␊ |
708 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
709 | ␉}␊ |
710 | ␊ |
711 | ␉// Create a separated bvr chain using the specified filters.␊ |
712 | ␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
713 | ␊ |
714 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
715 | ␊ |
716 | ␉// Intialize module system␊ |
717 | ␉init_module_system();␊ |
718 | ␊ |
719 | #if DEBUG␊ |
720 | ␉printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
721 | ␉␉gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
722 | ␉printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
723 | ␉␉gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
724 | ␉getchar();␊ |
725 | #endif␊ |
726 | ␊ |
727 | ␉useGUI = true;␊ |
728 | ␉// Override useGUI default␊ |
729 | ␉getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);␊ |
730 | ␉if (useGUI && initGUI())␊ |
731 | ␉{␊ |
732 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
733 | ␉␉useGUI = false;␊ |
734 | ␉}␊ |
735 | ␊ |
736 | ␉setBootGlobals(bvChain);␊ |
737 | ␊ |
738 | ␉// Parse args, load and start kernel.␊ |
739 | ␉while (1)␊ |
740 | ␉{␊ |
741 | ␉␉bool␉␉tryresume, tryresumedefault, forceresume;␊ |
742 | ␉␉bool␉␉useKernelCache = true; // by default try to use the prelinked kernel␊ |
743 | ␉␉const char␉*val;␊ |
744 | ␉␉int␉␉␉len, ret = -1;␊ |
745 | ␉␉long␉␉flags;␊ |
746 | ␉␉u_int32_t␉sleeptime, time;␊ |
747 | ␉␉void␉␉*binary = (void *)kLoadAddr;␊ |
748 | ␊ |
749 | ␉␉char␉␉bootFile[sizeof(bootInfo->bootFile)];␊ |
750 | ␉␉char␉␉bootFilePath[512];␊ |
751 | ␉␉char␉␉kernelCacheFile[512];␊ |
752 | ␊ |
753 | ␉␉// Initialize globals.␊ |
754 | ␉␉sysConfigValid = false;␊ |
755 | ␉␉gErrors␉␉ = false;␊ |
756 | ␊ |
757 | ␉␉status = getBootOptions(firstRun);␊ |
758 | ␉␉firstRun = false;␊ |
759 | ␉␉if (status == -1) continue;␊ |
760 | ␊ |
761 | ␉␉status = processBootOptions();␊ |
762 | ␉␉// Status == 1 means to chainboot␊ |
763 | ␉␉if ( status ==␉1 ) break;␊ |
764 | ␉␉// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
765 | ␉␉if ( status == -1 )␊ |
766 | ␉␉{␊ |
767 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
768 | ␉␉␉if (gBootVolume == NULL)␊ |
769 | ␉␉␉{␊ |
770 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
771 | ␊ |
772 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
773 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
774 | ␊ |
775 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
776 | ␉␉␉␉setBootGlobals(bvChain);␊ |
777 | ␉␉␉␉setupDeviceList(&bootInfo->themeConfig);␊ |
778 | ␉␉␉}␊ |
779 | ␉␉␉continue;␊ |
780 | ␉␉}␊ |
781 | ␊ |
782 | ␉␉// Other status (e.g. 0) means that we should proceed with boot.␊ |
783 | ␊ |
784 | ␉␉// Turn off any GUI elements␊ |
785 | ␉␉if ( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
786 | ␉␉{␊ |
787 | ␉␉␉gui.devicelist.draw = false;␊ |
788 | ␉␉␉gui.bootprompt.draw = false;␊ |
789 | ␉␉␉gui.menu.draw = false;␊ |
790 | ␉␉␉gui.infobox.draw = false;␊ |
791 | ␉␉␉gui.logo.draw = false;␊ |
792 | ␉␉␉drawBackground();␊ |
793 | ␉␉␉updateVRAM();␊ |
794 | ␉␉}␊ |
795 | ␊ |
796 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T))␊ |
797 | ␉␉{␊ |
798 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
799 | ␉␉}␊ |
800 | ␉␉else␊ |
801 | ␉␉{␊ |
802 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
803 | ␉␉}␊ |
804 | ␊ |
805 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))␊ |
806 | ␉␉{␊ |
807 | ␉␉␉if (strncmp(val, "x86_64", sizeof("x86_64") ) == 0)␊ |
808 | ␉␉␉{␊ |
809 | ␉␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
810 | ␉␉␉}␊ |
811 | ␉␉␉else if (strncmp(val, "i386", sizeof("i386") ) == 0)␊ |
812 | ␉␉␉{␊ |
813 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
814 | ␉␉␉}␊ |
815 | ␉␉␉else␊ |
816 | ␉␉␉{␊ |
817 | ␉␉␉␉DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");␊ |
818 | ␉␉␉}␊ |
819 | ␉␉}␊ |
820 | ␊ |
821 | ␉␉if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig))␊ |
822 | ␉␉{␊ |
823 | ␉␉␉if (strncmp(val, "i386", sizeof("i386") ) == 0)␊ |
824 | ␉␉␉{␊ |
825 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
826 | ␉␉␉}␊ |
827 | ␉␉}␊ |
828 | ␊ |
829 | ␉␉// Notify modules that we are attempting to boot␊ |
830 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL);␊ |
831 | ␊ |
832 | ␉␉if (gBootVolume->OSisInstaller)␊ |
833 | ␉␉{␊ |
834 | ␉␉␉isInstaller = true;␊ |
835 | ␉␉}␊ |
836 | ␊ |
837 | ␉␉if (gBootVolume->OSisMacOSXUpgrade)␊ |
838 | ␉␉{␊ |
839 | ␉␉␉isMacOSXUpgrade = true;␊ |
840 | ␉␉}␊ |
841 | ␊ |
842 | ␉␉if (gBootVolume->OSisOSXUpgrade)␊ |
843 | ␉␉{␊ |
844 | ␉␉␉isOSXUpgrade = true;␊ |
845 | ␉␉}␊ |
846 | ␊ |
847 | ␉␉if (gBootVolume->OSisRecovery)␊ |
848 | ␉␉{␊ |
849 | ␉␉␉isRecoveryHD = true;␊ |
850 | ␉␉}␊ |
851 | ␊ |
852 | ␉␉if ( !isRecoveryHD && !isInstaller && !isMacOSXUpgrade && !isOSXUpgrade )␊ |
853 | ␉␉{␊ |
854 | ␉␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))␊ |
855 | ␉␉␉{␊ |
856 | ␉␉␉␉tryresume = true;␊ |
857 | ␉␉␉␉tryresumedefault = true;␊ |
858 | ␉␉␉}␊ |
859 | ␉␉␉else␊ |
860 | ␉␉␉{␊ |
861 | ␉␉␉␉tryresumedefault = false;␊ |
862 | ␉␉␉}␊ |
863 | ␊ |
864 | ␉␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig))␊ |
865 | ␉␉␉{␊ |
866 | ␉␉␉␉forceresume = false;␊ |
867 | ␉␉␉}␊ |
868 | ␊ |
869 | ␉␉␉if (forceresume)␊ |
870 | ␉␉␉{␊ |
871 | ␉␉␉␉tryresume = true;␊ |
872 | ␉␉␉␉tryresumedefault = false;␊ |
873 | ␉␉␉}␊ |
874 | ␊ |
875 | ␉␉␉while (tryresume)␊ |
876 | ␉␉␉{␊ |
877 | ␉␉␉␉const char *tmp;␊ |
878 | ␉␉␉␉BVRef bvr;␊ |
879 | ␉␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))␊ |
880 | ␉␉␉␉␉val = "/private/var/vm/sleepimage";␊ |
881 | ␊ |
882 | ␉␉␉␉// Do this first to be sure that root volume is mounted␊ |
883 | ␉␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
884 | ␊ |
885 | ␉␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
886 | ␉␉␉␉␉break;␊ |
887 | ␊ |
888 | ␉␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
889 | ␉␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
890 | ␉␉␉␉␉break;␊ |
891 | ␊ |
892 | ␉␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
893 | ␉␉␉␉␉break;␊ |
894 | ␊ |
895 | ␉␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime))␊ |
896 | ␉␉␉␉{␊ |
897 | #if DEBUG␊ |
898 | ␉␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",␊ |
899 | ␉␉␉␉␉␉␉bvr->modTime-sleeptime);␊ |
900 | #endif␊ |
901 | ␉␉␉␉␉break;␊ |
902 | ␉␉␉␉}␊ |
903 | ␊ |
904 | ␉␉␉␉HibernateBoot((char *)val);␊ |
905 | ␉␉␉␉break;␊ |
906 | ␉␉␉}␊ |
907 | ␉␉}␊ |
908 | ␊ |
909 | ␉␉if (!isRecoveryHD && !isInstaller && !isMacOSXUpgrade && !isOSXUpgrade )␊ |
910 | ␉␉{␊ |
911 | ␉␉␉getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);␊ |
912 | ␉␉}␊ |
913 | ␊ |
914 | ␉␉if (useKernelCache)␊ |
915 | ␉␉{␊ |
916 | ␉␉␉do␊ |
917 | ␉␉␉{␊ |
918 | ␉␉␉␉// Determine the name of the Kernel Cache␊ |
919 | ␉␉␉␉if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))␊ |
920 | ␉␉␉␉{␊ |
921 | ␉␉␉␉␉if (val[0] == '\\')␊ |
922 | ␉␉␉␉␉{␊ |
923 | ␉␉␉␉␉␉len--;␊ |
924 | ␉␉␉␉␉␉val++;␊ |
925 | ␉␉␉␉␉}␊ |
926 | ␉␉␉␉␉/* FIXME: check len vs sizeof(kernelCacheFile) */␊ |
927 | ␉␉␉␉␉strlcpy(kernelCacheFile, val, len + 1);␊ |
928 | ␉␉␉␉}␊ |
929 | ␉␉␉␉else␊ |
930 | ␉␉␉␉{␊ |
931 | ␉␉␉␉␉kernelCacheFile[0] = 0; // Use default kernel cache file␊ |
932 | ␉␉␉␉}␊ |
933 | ␊ |
934 | ␉␉␉␉if (gOverrideKernel && kernelCacheFile[0] == 0)␊ |
935 | ␉␉␉␉{␊ |
936 | ␉␉␉␉␉DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);␊ |
937 | ␉␉␉␉␉useKernelCache = false;␊ |
938 | ␉␉␉␉␉break;␊ |
939 | ␉␉␉␉}␊ |
940 | ␊ |
941 | ␉␉␉␉if (gMKextName[0] != 0)␊ |
942 | ␉␉␉␉{␊ |
943 | ␉␉␉␉␉DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n", gMKextName);␊ |
944 | ␉␉␉␉␉useKernelCache = false;␊ |
945 | ␉␉␉␉␉break;␊ |
946 | ␉␉␉␉}␊ |
947 | ␊ |
948 | ␉␉␉␉if (gBootFileType != kBlockDeviceType)␊ |
949 | ␉␉␉␉{␊ |
950 | ␉␉␉␉␉useKernelCache = false;␊ |
951 | ␉␉␉␉}␊ |
952 | ␊ |
953 | ␉␉␉} while(0);␊ |
954 | ␉␉}␊ |
955 | ␉␉else␊ |
956 | ␉␉{␊ |
957 | ␉␉␉DBG("Kernel Cache using disabled by user.\n");␊ |
958 | ␉␉}␊ |
959 | ␊ |
960 | ␉␉do␊ |
961 | ␉␉{␊ |
962 | ␉␉␉if (useKernelCache)␊ |
963 | ␉␉␉{␊ |
964 | ␉␉␉␉ret = LoadKernelCache(kernelCacheFile, &binary);␊ |
965 | ␉␉␉␉if (ret >= 0)␊ |
966 | ␉␉␉␉{␊ |
967 | ␉␉␉␉␉break;␊ |
968 | ␉␉␉␉}␊ |
969 | ␉␉␉}␊ |
970 | ␊ |
971 | ␉␉␉bool bootFileWithDevice = false;␊ |
972 | ␉␉␉// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel␊ |
973 | ␉␉␉if (strncmp(bootInfo->bootFile, "bt(", sizeof("bt(") ) == 0 ||␊ |
974 | ␉␉␉␉strncmp(bootInfo->bootFile, "hd(", sizeof("hd(") ) == 0 ||␊ |
975 | ␉␉␉␉strncmp(bootInfo->bootFile, "rd(", sizeof("rd(") ) == 0)␊ |
976 | ␉␉␉{␊ |
977 | ␉␉␉␉bootFileWithDevice = true;␊ |
978 | ␉␉␉}␊ |
979 | ␊ |
980 | ␉␉␉// bootFile must start with a / if it not start with a device name␊ |
981 | ␉␉␉if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')␊ |
982 | ␉␉␉{␊ |
983 | ␉␉␉␉if ( MacOSVerCurrent < MacOSVer2Int("10.10") ) // Mavericks and older␊ |
984 | ␉␉␉␉{␊ |
985 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /␊ |
986 | ␉␉␉␉}␊ |
987 | ␉␉␉␉else␊ |
988 | ␉␉␉␉{␊ |
989 | ␉␉␉␉␉// Yosemite and newer␊ |
990 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite or El Capitan␊ |
991 | ␉␉␉␉}␊ |
992 | ␉␉␉}␊ |
993 | ␉␉␉else␊ |
994 | ␉␉␉{␊ |
995 | ␉␉␉␉strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));␊ |
996 | ␉␉␉}␊ |
997 | ␊ |
998 | ␉␉␉// Try to load kernel image from alternate locations on boot helper partitions.␊ |
999 | ␉␉␉ret = -1;␊ |
1000 | ␉␉␉if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)␊ |
1001 | ␉␉␉{␊ |
1002 | ␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);␊ |
1003 | ␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
1004 | ␉␉␉␉if (ret == -1)␊ |
1005 | ␉␉␉␉{␊ |
1006 | ␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);␊ |
1007 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
1008 | ␉␉␉␉␉if (ret == -1)␊ |
1009 | ␉␉␉␉␉{␊ |
1010 | ␉␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);␊ |
1011 | ␉␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
1012 | ␉␉␉␉␉}␊ |
1013 | ␉␉␉␉}␊ |
1014 | ␉␉␉}␊ |
1015 | ␉␉␉if (ret == -1)␊ |
1016 | ␉␉␉{␊ |
1017 | ␉␉␉␉// No alternate location found, using the original kernel image path.␊ |
1018 | ␉␉␉␉strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));␊ |
1019 | ␉␉␉}␊ |
1020 | ␊ |
1021 | ␉␉␉DBG("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);␊ |
1022 | ␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
1023 | ␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
1024 | ␉␉␉{␊ |
1025 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
1026 | ␉␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
1027 | ␉␉␉}␊ |
1028 | ␉␉} while (0);␊ |
1029 | ␊ |
1030 | ␉␉clearActivityIndicator();␊ |
1031 | ␊ |
1032 | #if DEBUG␊ |
1033 | ␉␉printf("Pausing...");␊ |
1034 | ␉␉sleep(8);␊ |
1035 | #endif␊ |
1036 | ␊ |
1037 | ␉␉if (ret <= 0)␊ |
1038 | ␉␉{␊ |
1039 | ␉␉␉printf("Can't find boot file: '%s'\n", bootFile);␊ |
1040 | ␉␉␉sleep(1);␊ |
1041 | ␊ |
1042 | ␉␉␉if (gBootFileType == kNetworkDeviceType)␊ |
1043 | ␉␉␉{␊ |
1044 | ␉␉␉␉// Return control back to PXE. Don't unload PXE base code.␊ |
1045 | ␉␉␉␉gUnloadPXEOnExit = false;␊ |
1046 | ␉␉␉␉break;␊ |
1047 | ␉␉␉}␊ |
1048 | ␉␉␉pause();␊ |
1049 | ␊ |
1050 | ␉␉}␊ |
1051 | ␉␉else␊ |
1052 | ␉␉{␊ |
1053 | ␉␉␉/* Won't return if successful. */␊ |
1054 | ␉␉␉ret = ExecKernel(binary);␊ |
1055 | ␉␉}␊ |
1056 | ␉}␊ |
1057 | ␊ |
1058 | ␉// chainboot␊ |
1059 | ␉if (status == 1)␊ |
1060 | ␉{␊ |
1061 | ␉␉// if we are already in graphics-mode,␊ |
1062 | ␉␉if (getVideoMode() == GRAPHICS_MODE)␊ |
1063 | ␉␉{␊ |
1064 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.␊ |
1065 | ␉␉}␊ |
1066 | ␉}␊ |
1067 | ␊ |
1068 | ␉if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)␊ |
1069 | ␉{␊ |
1070 | ␉␉nbpUnloadBaseCode();␊ |
1071 | ␉}␊ |
1072 | ␊ |
1073 | #if DEBUG_INTERRUPTS␊ |
1074 | ␉if (interruptsAvailable)␊ |
1075 | ␉{␊ |
1076 | ␉␉DisableInterrupts();␊ |
1077 | ␉}␊ |
1078 | #endif␊ |
1079 | ␊ |
1080 | }␊ |
1081 | ␊ |
1082 | // =========================================================================␊ |
1083 | //␊ |
1084 | void setupBooterArgs()␊ |
1085 | {␊ |
1086 | ␉bool KPRebootOption␉= false;␊ |
1087 | ␉bool HiDPIOption␉= false;␊ |
1088 | ␉bool FlagBlackOption␉= false;␊ |
1089 | ␊ |
1090 | ␊ |
1091 | ␉// OS X Mountain Lion 10.8␊ |
1092 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.8") ) // Mountain Lion and Up!␊ |
1093 | ␉{␊ |
1094 | ␉␉// cparm␊ |
1095 | ␉␉getBoolForKey(kRebootOnPanic, &KPRebootOption, &bootInfo->chameleonConfig);␊ |
1096 | ␉␉if ( KPRebootOption )␊ |
1097 | ␉␉{␊ |
1098 | ␉␉␉bootArgs->flags |= kBootArgsFlagRebootOnPanic;␊ |
1099 | ␉␉}␊ |
1100 | ␊ |
1101 | ␉␉// cparm␊ |
1102 | ␉␉getBoolForKey(kEnableHiDPI, &HiDPIOption, &bootInfo->chameleonConfig);␊ |
1103 | ␊ |
1104 | ␉␉if ( HiDPIOption )␊ |
1105 | ␉␉{␊ |
1106 | ␉␉␉bootArgs->flags |= kBootArgsFlagHiDPI;␊ |
1107 | ␉␉}␊ |
1108 | ␉}␊ |
1109 | ␊ |
1110 | ␉// OS X Yosemite 10.10␊ |
1111 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.10") ) // Yosemite and Up!␊ |
1112 | ␉{␊ |
1113 | ␉␉// Pike R. Alpha␊ |
1114 | ␉␉getBoolForKey(kBlackMode, &FlagBlackOption, &bootInfo->chameleonConfig);␊ |
1115 | ␉␉if ( FlagBlackOption )␊ |
1116 | ␉␉{␊ |
1117 | ␉␉␉// bootArgs->flags |= kBootArgsFlagBlack;␊ |
1118 | ␉␉␉bootArgs->flags |= kBootArgsFlagBlackBg; // Micky1979␊ |
1119 | ␉␉}␊ |
1120 | ␉}␊ |
1121 | ␊ |
1122 | ␉// OS X El Capitan 10.11␊ |
1123 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.11") ) // El Capitan and Up!␊ |
1124 | ␉{␊ |
1125 | ␉␉// ErmaC␊ |
1126 | ␉␉verbose("\n");␊ |
1127 | ␉␉int␉csrValue;␊ |
1128 | ␊ |
1129 | ␉␉/*␊ |
1130 | ␉␉ * A special BootArgs flag "kBootArgsFlagCSRBoot"␊ |
1131 | ␉␉ * is set in the Recovery or Installation environment.␊ |
1132 | ␉␉ * This flag is kind of overkill by turning off all the protections␊ |
1133 | ␉␉ */␊ |
1134 | ␊ |
1135 | ␉␉if (isRecoveryHD || isInstaller || isOSXUpgrade || isMacOSXUpgrade)␊ |
1136 | ␉␉{␊ |
1137 | ␉␉␉// SIP can be controlled with or without FileNVRAM.kext (Pike R. Alpha)␊ |
1138 | ␉␉␉bootArgs->flags␉|=␉(kBootArgsFlagCSRActiveConfig + kBootArgsFlagCSRConfigMode + kBootArgsFlagCSRBoot);␊ |
1139 | ␉␉}␊ |
1140 | ␉␉else␊ |
1141 | ␉␉{␊ |
1142 | ␉␉␉bootArgs->flags␉␉|= kBootArgsFlagCSRActiveConfig;␊ |
1143 | ␉␉}␊ |
1144 | ␊ |
1145 | ␉␉// Set limit to 7bit␊ |
1146 | ␉␉if ( getIntForKey(kCsrActiveConfig, &csrValue, &bootInfo->chameleonConfig) && (csrValue >= 0 && csrValue <= 127) )␊ |
1147 | ␉␉{␊ |
1148 | ␉␉␉bootArgs->csrActiveConfig␉= csrValue;␊ |
1149 | ␉␉␉csrInfo(csrValue, 1);␊ |
1150 | ␉␉}␊ |
1151 | ␉␉else␊ |
1152 | ␉␉{␊ |
1153 | ␉␉␉// zenith432␊ |
1154 | ␉␉␉bootArgs->csrActiveConfig␉= 0x67;␊ |
1155 | ␉␉␉csrInfo(0x67, 0);␊ |
1156 | ␊ |
1157 | ␉␉}␊ |
1158 | ␊ |
1159 | ␊ |
1160 | // ===============================================================================␊ |
1161 | ␊ |
1162 | ␉␉bootArgs->csrCapabilities␉= CSR_VALID_FLAGS;␊ |
1163 | ␉␉bootArgs->boot_SMC_plimit␉= 0;␊ |
1164 | ␉}␊ |
1165 | }␊ |
1166 | ␊ |
1167 | // =========================================================================␊ |
1168 | // ErmaC␊ |
1169 | void csrInfo(int csrValue, bool custom)␊ |
1170 | {␊ |
1171 | ␉int mask = 0x20;␊ |
1172 | ␉verbose("System Integrity Protection status: %s ", (csrValue == 0) ? "enabled":"disabled");␊ |
1173 | ␉verbose("(%s Configuration).\nCsrActiveConfig = 0x%02x (", custom ? "Custom":"Default", csrValue);␊ |
1174 | ␊ |
1175 | ␉// Display integer number into binary using bitwise operator␊ |
1176 | ␉((csrValue & 0x20) == 0) ? verbose("0"): verbose("1");␊ |
1177 | ␉while (mask != 0)␊ |
1178 | ␉{␊ |
1179 | ␉␉( ((csrValue & mask) == 0) ? verbose("0"): verbose("1") );␊ |
1180 | ␉␉mask = mask >> 1;␊ |
1181 | ␉}␊ |
1182 | ␉verbose(")\n");␊ |
1183 | ␉if (csrValue != 0)␊ |
1184 | ␉{␊ |
1185 | ␉␉verbose("\nConfiguration:\n");␊ |
1186 | ␉␉verbose("Kext Signing: %s\n", ((csrValue & 0x01) == 0) ? "enabled":"disabled"); /* (1 << 0) Allow untrusted kexts */␊ |
1187 | ␉␉verbose("Filesystem Protections: %s\n", ((csrValue & 0x02) == 0) ? "enabled":"disabled"); /* (1 << 1) Allow unrestricted file system. */␊ |
1188 | ␉␉verbose("Task for PID: %s\n", ((csrValue & 0x04) == 0) ? "enabled":"disabled"); /* (1 << 2) */␊ |
1189 | ␉␉verbose("Debugging Restrictions: %s\n", ((csrValue & 0x08) == 0) ? "enabled":"disabled"); /* (1 << 3) */␊ |
1190 | ␉␉verbose("Apple Internal: %s\n", ((csrValue & 0x10) == 0) ? "enabled":"disabled"); /* (1 << 4) */␊ |
1191 | ␉␉verbose("DTrace Restrictions: %s\n", ((csrValue & 0x20) == 0) ? "enabled":"disabled"); /* (1 << 5) Allow unrestricted dtrace */␊ |
1192 | ␉␉verbose("NVRAM Protections: %s\n", ((csrValue & 0x40) == 0) ? "enabled":"disabled"); /* (1 << 6) Allow unrestricted NVRAM */␊ |
1193 | //␉␉verbose("DEVICE configuration: %s\n", ((csrValue & 0x80) == 0) ? "enabled":"disabled"); /* (1 << 7) Allow device configuration */␊ |
1194 | ␉}␊ |
1195 | ␉verbose("\n");␊ |
1196 | }␊ |
1197 | ␊ |
1198 | // =========================================================================␊ |
1199 | ␊ |
1200 | /*!␊ |
1201 | ␉Selects a new BIOS device, taking care to update the global state appropriately.␊ |
1202 | */␊ |
1203 | /*␊ |
1204 | static void selectBiosDevice(void)␊ |
1205 | {␊ |
1206 | ␉struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
1207 | ␉CacheReset();␊ |
1208 | ␉diskFreeMap(oldMap);␊ |
1209 | ␉oldMap = NULL;␊ |
1210 | ␊ |
1211 | ␉int dev = selectAlternateBootDevice(gBIOSDev);␊ |
1212 | ␊ |
1213 | ␉BVRef bvchain = scanBootVolumes(dev, 0);␊ |
1214 | ␉BVRef bootVol = selectBootVolume(bvchain);␊ |
1215 | ␉gBootVolume = bootVol;␊ |
1216 | ␉setRootVolume(bootVol);␊ |
1217 | ␉gBIOSDev = dev;␊ |
1218 | }␊ |
1219 | */␊ |
1220 | ␊ |
1221 | // =========================================================================␊ |
1222 | bool checkOSVersion(const char *version)␊ |
1223 | {␊ |
1224 | ␉if ( '\0' != version[4] )␊ |
1225 | ␉{␊ |
1226 | ␉␉return !memcmp(&gMacOSVersion[0], &version[0], 5);␊ |
1227 | ␉}␊ |
1228 | ␉else␊ |
1229 | ␉{␊ |
1230 | ␉␉return !memcmp(&gMacOSVersion[0], &version[0], 4);␊ |
1231 | ␉}␊ |
1232 | }␊ |
1233 | ␊ |
1234 | uint32_t getMacOSVerCurrent()␊ |
1235 | {␊ |
1236 | ␉MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);␊ |
1237 | ␉return MacOSVerCurrent;␊ |
1238 | }␊ |
1239 | ␊ |
1240 | // =========================================================================␊ |
1241 | unsigned long Adler32(unsigned char *buf, long len)␊ |
1242 | {␊ |
1243 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
1244 | #define NMAX 5000␊ |
1245 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
1246 | ␊ |
1247 | #define DO1(buf, i)␉{s1 += buf[i]; s2 += s1;}␊ |
1248 | #define DO2(buf, i)␉DO1(buf, i); DO1(buf, i + 1);␊ |
1249 | #define DO4(buf, i)␉DO2(buf, i); DO2(buf, i + 2);␊ |
1250 | #define DO8(buf, i)␉DO4(buf, i); DO4(buf, i + 4);␊ |
1251 | #define DO16(buf)␉DO8(buf, 0); DO8(buf, 8);␊ |
1252 | ␊ |
1253 | ␉int k;␊ |
1254 | ␊ |
1255 | ␉unsigned long s1 = 1; // adler & 0xffff;␊ |
1256 | ␉unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
1257 | ␉unsigned long result;␊ |
1258 | ␊ |
1259 | ␉while (len > 0) {␊ |
1260 | ␉␉k = len < NMAX ? len : NMAX;␊ |
1261 | ␉␉len -= k;␊ |
1262 | ␉␉while (k >= 16)␊ |
1263 | ␉␉{␊ |
1264 | ␉␉␉DO16(buf);␊ |
1265 | ␉␉␉buf += 16;␊ |
1266 | ␉␉␉k -= 16;␊ |
1267 | ␉␉}␊ |
1268 | ␊ |
1269 | ␉␉if (k != 0)␊ |
1270 | ␉␉{␊ |
1271 | ␉␉␉do␊ |
1272 | ␉␉␉{␊ |
1273 | ␉␉␉␉s1 += *buf++;␊ |
1274 | ␉␉␉␉s2 += s1;␊ |
1275 | ␉␉␉} while (--k);␊ |
1276 | ␉␉}␊ |
1277 | ␊ |
1278 | ␉␉s1 %= BASE;␊ |
1279 | ␉␉s2 %= BASE;␊ |
1280 | ␉}␊ |
1281 | ␊ |
1282 | ␉result = (s2 << 16) | s1;␊ |
1283 | ␊ |
1284 | ␉return OSSwapHostToBigInt32(result);␊ |
1285 | }␊ |
1286 | |