1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | *␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License").␉You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | *␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | *␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | ␊ |
25 | /*␊ |
26 | * Mach Operating System␊ |
27 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
28 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
29 | * All rights reserved.␉ The CMU software License Agreement specifies␊ |
30 | * the terms and conditions for use and redistribution.␊ |
31 | *␊ |
32 | * INTEL CORPORATION PROPRIETARY INFORMATION␊ |
33 | *␊ |
34 | * This software is supplied under the terms of a license agreement or␊ |
35 | * nondisclosure agreement with Intel Corporation and may not be copied␊ |
36 | * nor disclosed except in accordance with the terms of that agreement.␊ |
37 | *␊ |
38 | *␉Copyright 1988, 1989 by Intel Corporation␊ |
39 | *␊ |
40 | *␊ |
41 | * Copyright 1993 NeXT Computer, Inc.␊ |
42 | * All rights reserved.␊ |
43 | *␊ |
44 | * Completely reworked by Sam Streeper (sam_s@NeXT.com)␊ |
45 | * Reworked again by Curtis Galloway (galloway@NeXT.com)␊ |
46 | */␊ |
47 | ␊ |
48 | #include "boot.h"␊ |
49 | #include "bootstruct.h"␊ |
50 | #include "fake_efi.h"␊ |
51 | #include "sl.h"␊ |
52 | #include "libsa.h"␊ |
53 | #include "ramdisk.h"␊ |
54 | #include "gui.h"␊ |
55 | #include "platform.h"␊ |
56 | #include "modules.h"␊ |
57 | #include "device_tree.h"␊ |
58 | ␊ |
59 | #ifndef DEBUG_BOOT2␊ |
60 | ␉#define DEBUG_BOOT2 0␊ |
61 | #endif␊ |
62 | ␊ |
63 | #ifndef DEBUG_INTERRUPTS␊ |
64 | ␉#define DEBUG_INTERRUPTS 0␊ |
65 | #endif␊ |
66 | ␊ |
67 | #if DEBUG_BOOT2␊ |
68 | ␉#define DBG(x...)␉printf(x)␊ |
69 | #else␊ |
70 | ␉#define DBG(x...)␉msglog(x)␊ |
71 | #endif␊ |
72 | ␊ |
73 | /*␊ |
74 | * How long to wait (in seconds) to load the␊ |
75 | * kernel after displaying the "boot:" prompt.␊ |
76 | */␊ |
77 | #define kBootErrorTimeout 5␊ |
78 | ␊ |
79 | bool␉␉gOverrideKernel;␊ |
80 | bool␉␉gEnableCDROMRescan;␊ |
81 | bool␉␉gScanSingleDrive;␊ |
82 | bool␉␉useGUI;␊ |
83 | ␊ |
84 | /* recovery or installer ? */␊ |
85 | bool isInstaller;␊ |
86 | bool isRecoveryHD;␊ |
87 | bool isMacOSXUpgrade;␊ |
88 | bool isOSXUpgrade;␊ |
89 | ␊ |
90 | #if DEBUG_INTERRUPTS␊ |
91 | ␉static int␉interruptsAvailable = 0;␊ |
92 | #endif␊ |
93 | ␊ |
94 | static bool␉gUnloadPXEOnExit = false;␊ |
95 | ␊ |
96 | static char␉gCacheNameAdler[64 + 256];␊ |
97 | char␉␉*gPlatformName = gCacheNameAdler;␊ |
98 | ␊ |
99 | char␉␉gRootDevice[ROOT_DEVICE_SIZE];␊ |
100 | char␉␉gMKextName[512];␊ |
101 | int␉␉bvCount = 0;␊ |
102 | int␉␉gDeviceCount = 0;␊ |
103 | //int␉␉menucount = 0;␊ |
104 | long␉␉gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
105 | ␊ |
106 | BVRef␉␉bvr;␊ |
107 | BVRef␉␉menuBVR;␊ |
108 | BVRef␉␉bvChain;␊ |
109 | ␊ |
110 | static unsigned long␉Adler32(unsigned char *buffer, long length);␊ |
111 | //static void␉␉␉selectBiosDevice(void);␊ |
112 | ␊ |
113 | /** options.c **/␊ |
114 | extern char *msgbuf;␊ |
115 | void showTextBuffer(char *buf, int size);␊ |
116 | ␊ |
117 | //==========================================================================␊ |
118 | // Zero the BSS.␊ |
119 | ␊ |
120 | static void zeroBSS(void)␊ |
121 | {␊ |
122 | ␉extern char bss_start __asm("section$start$__DATA$__bss");␊ |
123 | ␉extern char bss_end __asm("section$end$__DATA$__bss");␊ |
124 | ␉extern char common_start __asm("section$start$__DATA$__common");␊ |
125 | ␉extern char common_end __asm("section$end$__DATA$__common");␊ |
126 | ␊ |
127 | ␉bzero(&bss_start, (&bss_end - &bss_start));␊ |
128 | ␉bzero(&common_start, (&common_end - &common_start));␊ |
129 | }␊ |
130 | ␊ |
131 | //==========================================================================␊ |
132 | // Malloc error function␊ |
133 | ␊ |
134 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
135 | {␊ |
136 | ␉stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",␊ |
137 | ␉␉␉␉␉␉␉␉␉ (unsigned)addr, (unsigned)size, file, line);␊ |
138 | }␊ |
139 | ␊ |
140 | //==========================================================================␊ |
141 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
142 | //␊ |
143 | void initialize_runtime(void)␊ |
144 | {␊ |
145 | ␉zeroBSS();␊ |
146 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
147 | }␊ |
148 | ␊ |
149 | // =========================================================================␊ |
150 | ␊ |
151 | ␊ |
152 | ␊ |
153 | //==========================================================================␊ |
154 | // ExecKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
155 | ␊ |
156 | static int ExecKernel(void *binary)␊ |
157 | {␊ |
158 | ␉int␉␉␉ret;␊ |
159 | ␉entry_t␉␉kernelEntry;␊ |
160 | ␊ |
161 | ␉bootArgs->kaddr = bootArgs->ksize = 0;␊ |
162 | ␊ |
163 | // ===============================================================================␊ |
164 | ␊ |
165 | ␉gMacOSVersion[0] = 0;␊ |
166 | ␉// TODO identify sierra as macOS␊ |
167 | ␉verbose("Booting on %s %s (%s)\n", (MacOSVerCurrent < MacOSVer2Int("10.8")) ? "Mac OS X" : "OS X", gBootVolume->OSFullVer, gBootVolume->OSBuildVer );␊ |
168 | ␊ |
169 | ␉setupBooterArgs();␊ |
170 | ␊ |
171 | // ===============================================================================␊ |
172 | ␊ |
173 | ␉execute_hook("ExecKernel", (void *)binary, NULL, NULL, NULL);␊ |
174 | ␊ |
175 | ␉ret = DecodeKernel(binary,␊ |
176 | ␉␉␉␉␉ &kernelEntry,␊ |
177 | ␉␉␉␉␉ (char **) &bootArgs->kaddr,␊ |
178 | ␉␉␉␉␉ (int *)&bootArgs->ksize);␊ |
179 | ␊ |
180 | ␉if ( ret != 0 )␊ |
181 | ␉{␊ |
182 | ␉␉printf("Decoding kernel failed.\n");␊ |
183 | ␉␉pause();␊ |
184 | ␉␉return ret;␊ |
185 | ␉}␊ |
186 | ␊ |
187 | ␉// Reserve space for boot args␊ |
188 | ␉reserveKernBootStruct();␊ |
189 | ␊ |
190 | ␉// Notify modules that the kernel has been decoded␊ |
191 | ␉execute_hook("DecodedKernel", (void *)binary, (void *)bootArgs->kaddr, (void *)bootArgs->ksize, NULL);␊ |
192 | ␊ |
193 | ␉setupFakeEfi();␊ |
194 | ␊ |
195 | ␉// Load boot drivers from the specifed root path.␊ |
196 | ␉//if (!gHaveKernelCache)␊ |
197 | ␉{␊ |
198 | ␉␉LoadDrivers("/");␊ |
199 | ␉}␊ |
200 | ␊ |
201 | ␉execute_hook("DriversLoaded", (void *)binary, NULL, NULL, NULL);␊ |
202 | ␊ |
203 | ␉clearActivityIndicator();␊ |
204 | ␊ |
205 | ␉if (gErrors)␊ |
206 | ␉{␊ |
207 | ␉␉printf("Errors encountered while starting up the computer.\n");␊ |
208 | ␉␉printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
209 | ␉␉sleep(kBootErrorTimeout);␊ |
210 | ␉}␊ |
211 | ␊ |
212 | ␉md0Ramdisk();␊ |
213 | ␊ |
214 | ␉// Cleanup the PXE base code.␊ |
215 | ␊ |
216 | ␉if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )␊ |
217 | ␉{␊ |
218 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
219 | ␉␉{␊ |
220 | ␉␉␉printf("nbpUnloadBaseCode error %d\n", (int)ret);␊ |
221 | ␉␉␉sleep(2);␊ |
222 | ␉␉}␊ |
223 | ␉}␊ |
224 | ␊ |
225 | ␉bool dummyVal;␊ |
226 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal)␊ |
227 | ␉{␊ |
228 | ␉␉showTextBuffer(msgbuf, strlen(msgbuf));␊ |
229 | ␉}␊ |
230 | ␊ |
231 | ␉usb_loop();␊ |
232 | ␊ |
233 | #if DEBUG␊ |
234 | ␉if (interruptsAvailable) ShowInterruptCounters();␊ |
235 | #endif␊ |
236 | ␊ |
237 | ␉// If we were in text mode, switch to graphics mode.␊ |
238 | ␉// This will draw the boot graphics unless we are in␊ |
239 | ␉// verbose mode.␊ |
240 | ␉if (gVerboseMode)␊ |
241 | ␉{␊ |
242 | ␉␉setVideoMode( GRAPHICS_MODE );␊ |
243 | ␉}␊ |
244 | ␉else␊ |
245 | ␉{␊ |
246 | ␉␉drawBootGraphics();␊ |
247 | ␉}␊ |
248 | ␊ |
249 | ␉DBG("Starting Darwin/%s [%s]\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64", gDarwinBuildVerStr);␊ |
250 | ␉DBG("Boot Args: %s\n", bootArgs->CommandLine);␊ |
251 | ␊ |
252 | ␉setupBooterLog();␊ |
253 | ␊ |
254 | ␉finalizeBootStruct();␊ |
255 | ␊ |
256 | ␉// Jump to kernel's entry point. There's no going back now.␊ |
257 | ␉if ( MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
258 | ␉{␊ |
259 | ␉␉// Notify modules that the kernel is about to be started␊ |
260 | ␉␉execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgsLegacy, NULL, NULL);␊ |
261 | ␊ |
262 | ␉␉startprog( kernelEntry, bootArgsLegacy );␊ |
263 | ␉}␊ |
264 | ␉else␊ |
265 | ␉{␊ |
266 | ␉␉// Notify modules that the kernel is about to be started␊ |
267 | ␉␉execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgs, NULL, NULL);␊ |
268 | ␊ |
269 | ␉␉startprog( kernelEntry, bootArgs );␊ |
270 | ␉}␊ |
271 | ␊ |
272 | ␉// Not reached␊ |
273 | ␉__builtin_unreachable();␊ |
274 | }␊ |
275 | ␊ |
276 | //==========================================================================␊ |
277 | // LoadKernelCache - Try to load Kernel Cache.␊ |
278 | // return the length of the loaded cache file or -1 on error␊ |
279 | long LoadKernelCache(const char *cacheFile, void **binary)␊ |
280 | {␊ |
281 | ␉char␉␉kernelCacheFile[512];␊ |
282 | ␉char␉␉kernelCachePath[512];␊ |
283 | ␉long␉␉flags, ret=-1;␊ |
284 | ␉unsigned long␉adler32 = 0;␊ |
285 | ␉u_int32_t time, cachetime, kerneltime, exttime;␊ |
286 | ␊ |
287 | ␉if((gBootMode & kBootModeSafe) != 0)␊ |
288 | ␉{␊ |
289 | ␉␉DBG("Kernel Cache ignored.\n");␊ |
290 | ␉␉return -1;␊ |
291 | ␉}␊ |
292 | ␊ |
293 | ␉// Use specify kernel cache file if not empty␊ |
294 | ␉if (cacheFile[0] != 0)␊ |
295 | ␉{␊ |
296 | ␉␉strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));␊ |
297 | ␉␉verbose("Specified kernel cache file path: %s\n", cacheFile);␊ |
298 | ␉}␊ |
299 | ␉else␊ |
300 | ␉{␊ |
301 | ␉␉// Leopard prelink kernel cache file␊ |
302 | ␉␉if ( MacOSVerCurrent >= MacOSVer2Int("10.4") && MacOSVerCurrent <= MacOSVer2Int("10.5") ) // OSX is 10.4 or 10.5␊ |
303 | ␉␉{␊ |
304 | ␉␉␉// Reset cache name.␊ |
305 | ␉␉␉bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
306 | ␉␉␉snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
307 | ␉␉␉adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
308 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);␊ |
309 | ␉␉␉verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);␊ |
310 | ␊ |
311 | ␉␉}␊ |
312 | ␉␉// Snow Leopard prelink kernel cache file␊ |
313 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.6") && MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
314 | ␉␉{␊ |
315 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",␊ |
316 | ␉␉␉␉(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");␊ |
317 | ␊ |
318 | ␉␉␉int␉␉lnam = strlen(kernelCacheFile) + 9; //with adler32␊ |
319 | ␉␉␉char␉␉*name;␊ |
320 | ␉␉␉u_int32_t␉prev_time = 0;␊ |
321 | ␊ |
322 | ␉␉␉struct dirstuff *cacheDir = opendir(kDefaultCachePathSnow);␊ |
323 | ␊ |
324 | ␉␉␉/* TODO: handle error? */␊ |
325 | ␉␉␉if (cacheDir)␊ |
326 | ␉␉␉{␊ |
327 | ␉␉␉␉while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)␊ |
328 | ␉␉␉␉{␊ |
329 | ␉␉␉␉␉if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time␊ |
330 | ␉␉␉␉␉␉&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))␊ |
331 | ␉␉␉␉␉{␊ |
332 | ␉␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);␊ |
333 | ␉␉␉␉␉␉prev_time = time;␊ |
334 | ␉␉␉␉␉}␊ |
335 | ␉␉␉␉}␊ |
336 | ␉␉␉␉verbose("Kernel Cache file path (Mac OS X 10.6): %s\n", kernelCacheFile);␊ |
337 | ␉␉␉}␊ |
338 | ␉␉␉closedir(cacheDir);␊ |
339 | ␉␉}␊ |
340 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.9") )␊ |
341 | ␉␉{␊ |
342 | ␉␉␉// Lion, Mountain Lion␊ |
343 | ␉␉␉// for 10.7 10.8␊ |
344 | ␉␉␉if (isMacOSXUpgrade)␊ |
345 | ␉␉␉{␊ |
346 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/Mac OS X Install Data/");␊ |
347 | ␉␉␉}␊ |
348 | ␉␉␉else if (isInstaller)␊ |
349 | ␉␉␉{␊ |
350 | ␉␉␉␉if (MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.8") )␊ |
351 | ␉␉␉␉{␊ |
352 | ␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kLionInstallerDataFolder);␊ |
353 | ␉␉␉␉}␊ |
354 | ␉␉␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.8") && MacOSVerCurrent < MacOSVer2Int("10.9") )␊ |
355 | ␉␉␉␉{␊ |
356 | ␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kMLionInstallerDataFolder);␊ |
357 | ␉␉␉␉}␊ |
358 | ␊ |
359 | ␉␉␉}␊ |
360 | ␉␉␉else if (isRecoveryHD)␊ |
361 | ␉␉␉{␊ |
362 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);␊ |
363 | ␉␉␉}␊ |
364 | ␉␉␉else␊ |
365 | ␉␉␉{␊ |
366 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);␊ |
367 | ␉␉␉}␊ |
368 | ␊ |
369 | ␉␉␉verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);␊ |
370 | ␊ |
371 | ␉␉}␊ |
372 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.9") && MacOSVerCurrent < MacOSVer2Int("10.10") )␊ |
373 | ␉␉{␊ |
374 | ␉␉␉// Mavericks prelinked cache file␊ |
375 | ␉␉␉// for 10.9␊ |
376 | ␉␉␉if (isOSXUpgrade)␊ |
377 | ␉␉␉{␊ |
378 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");␊ |
379 | ␉␉␉}␊ |
380 | ␉␉␉else if (isInstaller)␊ |
381 | ␉␉␉{␊ |
382 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);␊ |
383 | ␉␉␉}␊ |
384 | ␉␉␉else if (isRecoveryHD)␊ |
385 | ␉␉␉{␊ |
386 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);␊ |
387 | ␉␉␉}␊ |
388 | ␉␉␉else␊ |
389 | ␉␉␉{␊ |
390 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);␊ |
391 | ␉␉␉}␊ |
392 | ␊ |
393 | ␉␉␉verbose("Kernel Cache file path (OS X 10.9): %s\n", kernelCacheFile);␊ |
394 | ␊ |
395 | ␉␉}␊ |
396 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.10") && MacOSVerCurrent < MacOSVer2Int("10.11") )␊ |
397 | ␉␉{␊ |
398 | ␉␉␉// Yosemite prelink kernel cache file␊ |
399 | ␉␉␉// for 10.10 and 10.11␊ |
400 | ␉␉␉if (isOSXUpgrade)␊ |
401 | ␉␉␉{␊ |
402 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");␊ |
403 | ␉␉␉}␊ |
404 | ␉␉␉else if (isInstaller)␊ |
405 | ␉␉␉{␊ |
406 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);␊ |
407 | ␉␉␉}␊ |
408 | ␉␉␉else if (isRecoveryHD)␊ |
409 | ␉␉␉{␊ |
410 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);␊ |
411 | ␉␉␉}␊ |
412 | ␉␉␉else␊ |
413 | ␉␉␉{␊ |
414 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);␊ |
415 | ␉␉␉}␊ |
416 | ␊ |
417 | ␉␉␉verbose("Kernel Cache file path (OS X 10.10): %s\n", kernelCacheFile);␊ |
418 | ␉␉}␊ |
419 | ␉␉else if ( MacOSVerCurrent >= MacOSVer2Int("10.11") )␊ |
420 | ␉␉{␊ |
421 | ␉␉␉// El Capitan on prelinked kernel cache file␊ |
422 | ␉␉␉// for 10.10 and 10.11␊ |
423 | ␉␉␉if (isOSXUpgrade)␊ |
424 | ␉␉␉{␊ |
425 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", "/OS X Install Data/");␊ |
426 | ␉␉␉}␊ |
427 | ␉␉␉else if (isInstaller)␊ |
428 | ␉␉␉{␊ |
429 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheInstallerNew);␊ |
430 | ␉␉␉}␊ |
431 | ␉␉␉else if (isRecoveryHD)␊ |
432 | ␉␉␉{␊ |
433 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheRecoveryHD);␊ |
434 | ␉␉␉}␊ |
435 | ␉␉␉else␊ |
436 | ␉␉␉{␊ |
437 | ␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);␊ |
438 | ␉␉␉}␊ |
439 | ␊ |
440 | ␉␉␉verbose("Kernel Cache file path (OS X 10.11 and newer): %s\n", kernelCacheFile);␊ |
441 | ␉␉}␊ |
442 | ␉}␊ |
443 | ␊ |
444 | ␉// Check if the kernel cache file exists␊ |
445 | ␉ret = -1;␊ |
446 | ␊ |
447 | ␉if (gBootVolume->flags & kBVFlagBooter)␊ |
448 | ␉{␊ |
449 | ␉␉if (isRecoveryHD)␊ |
450 | ␉␉{␊ |
451 | ␉␉␉strncpy(kernelCachePath, "/com.apple.recovery.boot/prelinkedkernel", sizeof(kernelCachePath) );␊ |
452 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
453 | ␊ |
454 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
455 | ␉␉␉{␊ |
456 | ␉␉␉␉strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof(kernelCachePath) );␊ |
457 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
458 | ␊ |
459 | ␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
460 | ␉␉␉␉{␊ |
461 | ␉␉␉␉␉ret = -1;␊ |
462 | ␉␉␉␉}␊ |
463 | ␉␉␉}␊ |
464 | ␉␉}␊ |
465 | ␉␉else if (isInstaller)␊ |
466 | ␉␉{␊ |
467 | ␉␉␉strncpy(kernelCachePath, "/.IABootFiles/prelinkedkernel", sizeof(kernelCachePath) );␊ |
468 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
469 | ␊ |
470 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
471 | ␉␉␉{␊ |
472 | ␉␉␉␉strncpy(kernelCachePath, "/.IABootFiles/kernelcache", sizeof(kernelCachePath) );␊ |
473 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
474 | ␊ |
475 | ␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
476 | ␉␉␉␉{␊ |
477 | ␉␉␉␉␉ret = -1;␊ |
478 | ␉␉␉␉}␊ |
479 | ␉␉␉}␊ |
480 | ␉␉}␊ |
481 | ␉␉else if (isMacOSXUpgrade)␊ |
482 | ␉␉{␊ |
483 | ␉␉␉strncpy(kernelCachePath, "/Mac OS X Install Data/kernelcache", sizeof(kernelCachePath) );␊ |
484 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
485 | ␊ |
486 | ␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
487 | ␉␉␉{␊ |
488 | ␉␉␉␉ret = -1;␊ |
489 | ␉␉␉}␊ |
490 | ␉␉}␊ |
491 | ␉␉else if (isOSXUpgrade)␊ |
492 | ␉␉{␊ |
493 | ␉␉␉strncpy(kernelCachePath, "/OS X Install Data/prelinkedkernel", sizeof(kernelCachePath) );␊ |
494 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
495 | ␊ |
496 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
497 | ␉␉␉{␊ |
498 | ␉␉␉␉strncpy(kernelCachePath, "/OS X Install Data/kernelcache", sizeof(kernelCachePath) );␊ |
499 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
500 | ␊ |
501 | ␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
502 | ␉␉␉␉{␊ |
503 | ␉␉␉␉␉ret = -1;␊ |
504 | ␉␉␉␉}␊ |
505 | ␉␉␉}␊ |
506 | ␉␉}␊ |
507 | ␉␉else␊ |
508 | ␉␉{␊ |
509 | ␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);␊ |
510 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
511 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
512 | ␉␉␉{␊ |
513 | ␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);␊ |
514 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
515 | ␊ |
516 | ␉␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
517 | ␉␉␉␉{␊ |
518 | ␉␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);␊ |
519 | ␉␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
520 | ␊ |
521 | ␉␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
522 | ␉␉␉␉␉{␊ |
523 | ␉␉␉␉␉␉ret = -1;␊ |
524 | ␉␉␉␉␉}␊ |
525 | ␉␉␉␉}␊ |
526 | ␉␉␉}␊ |
527 | ␉␉}␊ |
528 | ␉}␊ |
529 | ␊ |
530 | ␉// If not found, use the original kernel cache path.␊ |
531 | ␉if (ret == -1)␊ |
532 | ␉{␊ |
533 | ␉␉strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));␊ |
534 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
535 | ␊ |
536 | ␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
537 | ␉␉{␊ |
538 | ␉␉␉ret = -1;␊ |
539 | ␉␉}␊ |
540 | ␉}␊ |
541 | ␊ |
542 | ␉// Exit if kernel cache file wasn't found␊ |
543 | ␉if (ret == -1)␊ |
544 | ␉{␊ |
545 | ␉␉DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);␊ |
546 | ␉␉return -1;␊ |
547 | ␉}␊ |
548 | ␊ |
549 | ␉if ( !isInstaller && !isRecoveryHD && !isMacOSXUpgrade && !isOSXUpgrade )␊ |
550 | ␉{␊ |
551 | ␉␉// Check if the kernel cache file is more recent (mtime)␊ |
552 | ␉␉// than the kernel file or the S/L/E directory␊ |
553 | ␉␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
554 | ␊ |
555 | ␉␉// Check if the kernel file is more recent than the cache file␊ |
556 | ␉␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))␊ |
557 | ␉␉{␊ |
558 | ␉␉␉DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);␊ |
559 | ␉␉␉return -1;␊ |
560 | ␉␉}␊ |
561 | ␊ |
562 | ␉␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
563 | ␊ |
564 | ␉␉// Check if the S/L/E directory time is more recent than the cache file␊ |
565 | ␉␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))␊ |
566 | ␉␉{␊ |
567 | ␉␉␉DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);␊ |
568 | ␉␉␉return -1;␊ |
569 | ␉␉}␊ |
570 | ␉}␊ |
571 | ␊ |
572 | ␉// Since the kernel cache file exists and is the most recent try to load it␊ |
573 | ␉DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);␊ |
574 | ␊ |
575 | ␉ret = LoadThinFatFile(kernelCachePath, binary);␊ |
576 | ␉return ret; // ret contain the length of the binary␊ |
577 | }␊ |
578 | ␊ |
579 | //==========================================================================␊ |
580 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
581 | // before. Multiboot does its own runtime initialization, does some of its␊ |
582 | // own things, and then calls common_boot.␊ |
583 | void boot(int biosdev)␊ |
584 | {␊ |
585 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
586 | ␉// Note: malloc_init(), called via initialize_runtime() writes␊ |
587 | ␉// memory >= 1Mb, so A20 must be enabled before calling it. - zenith432␊ |
588 | ␉zeroBSS();␊ |
589 | ␉enableA20();␊ |
590 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
591 | ␉common_boot(biosdev);␊ |
592 | }␊ |
593 | ␊ |
594 | //==========================================================================␊ |
595 | // The 'main' function for the booter. Called by boot0 when booting␊ |
596 | // from a block device, or by the network booter.␊ |
597 | //␊ |
598 | // arguments:␊ |
599 | //␉ biosdev - Value passed from boot1/NBP to specify the device␊ |
600 | //␉␉␉ that the booter was loaded from.␊ |
601 | //␊ |
602 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
603 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
604 | // next boot device on its list.␊ |
605 | void common_boot(int biosdev)␊ |
606 | {␊ |
607 | ␉bool␉ ␉␉quiet;␊ |
608 | ␉bool␉ ␉␉firstRun = true;␊ |
609 | ␉bool␉ ␉␉instantMenu;␊ |
610 | ␉bool␉ ␉␉rescanPrompt;␊ |
611 | ␉int␉␉␉status;␊ |
612 | ␉unsigned int␉␉allowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;␊ |
613 | ␉unsigned int␉␉denyBVFlags = kBVFlagEFISystem;␊ |
614 | ␊ |
615 | ␉// Set reminder to unload the PXE base code. Neglect to unload␊ |
616 | ␉// the base code will result in a hang or kernel panic.␊ |
617 | ␉gUnloadPXEOnExit = true;␊ |
618 | ␊ |
619 | ␉// Record the device that the booter was loaded from.␊ |
620 | ␉gBIOSDev = biosdev & kBIOSDevMask;␊ |
621 | ␊ |
622 | ␉// Initialize boot-log␊ |
623 | ␉initBooterLog();␊ |
624 | ␊ |
625 | #if DEBUG_INTERRUPTS␊ |
626 | ␉// Enable interrupts␊ |
627 | ␉interruptsAvailable = SetupInterrupts();␊ |
628 | ␉if (interruptsAvailable)␊ |
629 | ␉{␊ |
630 | ␉␉EnableInterrupts();␊ |
631 | ␉}␊ |
632 | #endif␊ |
633 | ␊ |
634 | ␉// Initialize boot info structure.␊ |
635 | ␉initKernBootStruct();␊ |
636 | ␊ |
637 | ␉// Setup VGA text mode.␊ |
638 | ␉// Not sure if it is safe to call setVideoMode() before the␊ |
639 | ␉// config table has been loaded. Call video_mode() instead.␊ |
640 | #if DEBUG␊ |
641 | ␉printf("before video_mode\n");␊ |
642 | #endif␊ |
643 | ␉video_mode( 2 ); // 80x25 mono text mode.␊ |
644 | #if DEBUG␊ |
645 | ␉printf("after video_mode\n");␊ |
646 | #endif␊ |
647 | ␊ |
648 | ␉// Scan and record the system's hardware information.␊ |
649 | ␉scan_platform();␊ |
650 | ␊ |
651 | ␉// First get info for boot volume.␊ |
652 | ␉scanBootVolumes(gBIOSDev, 0);␊ |
653 | ␉bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
654 | ␉setBootGlobals(bvChain);␊ |
655 | ␊ |
656 | ␉// Load boot.plist config file␊ |
657 | ␉status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);␊ |
658 | ␊ |
659 | ␉if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)␊ |
660 | ␉{␊ |
661 | ␉␉gBootMode |= kBootModeQuiet;␊ |
662 | ␉}␊ |
663 | ␊ |
664 | ␉// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
665 | ␉if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)␊ |
666 | ␉{␊ |
667 | ␉␉firstRun = false;␊ |
668 | ␉}␊ |
669 | ␊ |
670 | ␉// Loading preboot ramdisk if exists.␊ |
671 | ␉loadPrebootRAMDisk();␊ |
672 | ␊ |
673 | ␉// Disable rescan option by default␊ |
674 | ␉gEnableCDROMRescan = false;␊ |
675 | ␊ |
676 | ␉// Enable it with Rescan=y in system config␊ |
677 | ␉if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)␉&& gEnableCDROMRescan)␊ |
678 | ␉{␊ |
679 | ␉␉gEnableCDROMRescan = true;␊ |
680 | ␉}␊ |
681 | ␊ |
682 | ␉// Disable rescanPrompt option by default␊ |
683 | ␉rescanPrompt = false;␊ |
684 | ␊ |
685 | ␉// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
686 | ␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))␊ |
687 | ␉{␊ |
688 | ␉␉gEnableCDROMRescan = promptForRescanOption();␊ |
689 | ␉}␊ |
690 | ␊ |
691 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
692 | ␉if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)␊ |
693 | ␉{␊ |
694 | ␉␉gScanSingleDrive = true;␊ |
695 | ␉}␊ |
696 | ␊ |
697 | ␉// Create a list of partitions on device(s).␊ |
698 | ␉if (gScanSingleDrive)␊ |
699 | ␉{␊ |
700 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
701 | ␉}␊ |
702 | ␉else␊ |
703 | ␉{␊ |
704 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
705 | ␉}␊ |
706 | ␊ |
707 | ␉// Create a separated bvr chain using the specified filters.␊ |
708 | ␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
709 | ␊ |
710 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
711 | ␊ |
712 | ␉// Intialize module system␊ |
713 | ␉init_module_system();␊ |
714 | ␊ |
715 | #if DEBUG␊ |
716 | ␉printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
717 | ␉␉gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
718 | ␉printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",␊ |
719 | ␉␉gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
720 | ␉getchar();␊ |
721 | #endif␊ |
722 | ␊ |
723 | ␉useGUI = true;␊ |
724 | ␉// Override useGUI default␊ |
725 | ␉getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);␊ |
726 | ␉if (useGUI && initGUI())␊ |
727 | ␉{␊ |
728 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
729 | ␉␉useGUI = false;␊ |
730 | ␉}␊ |
731 | ␊ |
732 | ␉setBootGlobals(bvChain);␊ |
733 | ␊ |
734 | ␉// Parse args, load and start kernel.␊ |
735 | ␉while (1)␊ |
736 | ␉{␊ |
737 | ␉␉bool␉␉tryresume, tryresumedefault, forceresume;␊ |
738 | ␉␉bool␉␉useKernelCache = true; // by default try to use the prelinked kernel␊ |
739 | ␉␉const char␉*val;␊ |
740 | ␉␉int␉␉␉len, ret = -1;␊ |
741 | ␉␉long␉␉flags;␊ |
742 | ␉␉u_int32_t␉sleeptime, time;␊ |
743 | ␉␉void␉␉*binary = (void *)kLoadAddr;␊ |
744 | ␊ |
745 | ␉␉char␉␉bootFile[sizeof(bootInfo->bootFile)];␊ |
746 | ␉␉char␉␉bootFilePath[512];␊ |
747 | ␉␉char␉␉kernelCacheFile[512];␊ |
748 | ␊ |
749 | ␉␉// Initialize globals.␊ |
750 | ␉␉sysConfigValid = false;␊ |
751 | ␉␉gErrors␉␉ = false;␊ |
752 | ␊ |
753 | ␉␉status = getBootOptions(firstRun);␊ |
754 | ␉␉firstRun = false;␊ |
755 | ␉␉if (status == -1) continue;␊ |
756 | ␊ |
757 | ␉␉status = processBootOptions();␊ |
758 | ␉␉// Status == 1 means to chainboot␊ |
759 | ␉␉if ( status ==␉1 ) break;␊ |
760 | ␉␉// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
761 | ␉␉if ( status == -1 )␊ |
762 | ␉␉{␊ |
763 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
764 | ␉␉␉if (gBootVolume == NULL)␊ |
765 | ␉␉␉{␊ |
766 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
767 | ␊ |
768 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
769 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
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 | |