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