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