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 | #if DEBUG_BOOT2␊ |
64 | #define DBG(x...)␉printf(x)␊ |
65 | #else␊ |
66 | #define DBG(x...)␉msglog(x)␊ |
67 | #endif␊ |
68 | ␊ |
69 | /*␊ |
70 | * How long to wait (in seconds) to load the␊ |
71 | * kernel after displaying the "boot:" prompt.␊ |
72 | */␊ |
73 | #define kBootErrorTimeout 5␊ |
74 | ␊ |
75 | bool␉␉gOverrideKernel, gEnableCDROMRescan, gScanSingleDrive, useGUI;␊ |
76 | static bool␉gUnloadPXEOnExit = false;␊ |
77 | ␊ |
78 | static char␉gCacheNameAdler[64 + 256];␊ |
79 | char␉␉*gPlatformName = gCacheNameAdler;␊ |
80 | ␊ |
81 | char␉␉gRootDevice[ROOT_DEVICE_SIZE];␊ |
82 | char␉␉gMKextName[512];␊ |
83 | char␉␉gMacOSVersion[8];␊ |
84 | int␉␉bvCount = 0, gDeviceCount = 0;␊ |
85 | //int␉␉menucount = 0;␊ |
86 | long␉␉gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
87 | BVRef␉␉bvr, menuBVR, bvChain;␊ |
88 | ␊ |
89 | static unsigned long␉Adler32(unsigned char *buffer, long length);␊ |
90 | //static void␉␉␉selectBiosDevice(void);␊ |
91 | ␊ |
92 | /** options.c **/␊ |
93 | extern char* msgbuf;␊ |
94 | void showTextBuffer(char *buf, int size);␊ |
95 | ␊ |
96 | ␊ |
97 | //==========================================================================␊ |
98 | // Zero the BSS.␊ |
99 | ␊ |
100 | static void zeroBSS(void)␊ |
101 | {␊ |
102 | ␉extern char bss_start __asm("section$start$__DATA$__bss");␊ |
103 | ␉extern char bss_end __asm("section$end$__DATA$__bss");␊ |
104 | ␉extern char common_start __asm("section$start$__DATA$__common");␊ |
105 | ␉extern char common_end __asm("section$end$__DATA$__common");␊ |
106 | ␊ |
107 | ␉bzero(&bss_start, (&bss_end - &bss_start));␊ |
108 | ␉bzero(&common_start, (&common_end - &common_start));␊ |
109 | }␊ |
110 | ␊ |
111 | //==========================================================================␊ |
112 | // Malloc error function␊ |
113 | ␊ |
114 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
115 | {␊ |
116 | ␉stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",␊ |
117 | ␉␉␉␉␉␉␉␉␉ (unsigned)addr, (unsigned)size, file, line);␊ |
118 | }␊ |
119 | ␊ |
120 | //==========================================================================␊ |
121 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
122 | //␊ |
123 | void initialize_runtime(void)␊ |
124 | {␊ |
125 | ␉zeroBSS();␊ |
126 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
127 | }␊ |
128 | ␊ |
129 | //==========================================================================␊ |
130 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
131 | ␊ |
132 | static int ExecKernel(void *binary)␊ |
133 | {␊ |
134 | ␉int␉␉␉ret;␊ |
135 | ␉entry_t␉␉kernelEntry;␊ |
136 | ␊ |
137 | ␉bootArgs->kaddr = bootArgs->ksize = 0;␊ |
138 | ␉execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);␊ |
139 | ␊ |
140 | ␉ret = DecodeKernel(binary,␊ |
141 | ␉␉␉␉␉ &kernelEntry,␊ |
142 | ␉␉␉␉␉ (char **) &bootArgs->kaddr,␊ |
143 | ␉␉␉␉␉ (int *)&bootArgs->ksize );␊ |
144 | ␊ |
145 | ␉if ( ret != 0 )␊ |
146 | ␉{␊ |
147 | ␉␉return ret;␊ |
148 | ␉}␊ |
149 | ␊ |
150 | ␉// Reserve space for boot args␊ |
151 | ␉reserveKernBootStruct();␊ |
152 | ␊ |
153 | ␉// Notify modules that the kernel has been decoded␊ |
154 | ␉execute_hook("DecodedKernel", (void*)binary, (void*)bootArgs->kaddr, (void*)bootArgs->ksize, NULL);␊ |
155 | ␊ |
156 | ␉setupFakeEfi();␊ |
157 | ␊ |
158 | ␉// Load boot drivers from the specifed root path.␊ |
159 | ␉//if (!gHaveKernelCache)␊ |
160 | ␉{␊ |
161 | ␉␉LoadDrivers("/");␊ |
162 | ␉}␊ |
163 | ␊ |
164 | ␉execute_hook("DriversLoaded", (void*)binary, NULL, NULL, NULL);␊ |
165 | ␊ |
166 | ␉clearActivityIndicator();␊ |
167 | ␊ |
168 | ␉if (gErrors)␊ |
169 | ␉{␊ |
170 | ␉␉printf("Errors encountered while starting up the computer.\n");␊ |
171 | ␉␉printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
172 | ␉␉sleep(kBootErrorTimeout);␊ |
173 | ␉}␊ |
174 | ␊ |
175 | ␉md0Ramdisk();␊ |
176 | ␊ |
177 | ␉// Cleanup the PXE base code.␊ |
178 | ␊ |
179 | ␉if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )␊ |
180 | ␉{␊ |
181 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
182 | ␉␉{␊ |
183 | ␉␉␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
184 | ␉␉␉sleep(2);␊ |
185 | ␉␉}␊ |
186 | ␉}␊ |
187 | ␊ |
188 | ␉bool dummyVal;␊ |
189 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal)␊ |
190 | ␉{␊ |
191 | ␉␉showTextBuffer(msgbuf, strlen(msgbuf));␊ |
192 | ␉}␊ |
193 | ␊ |
194 | ␉usb_loop();␊ |
195 | ␊ |
196 | ␉// If we were in text mode, switch to graphics mode.␊ |
197 | ␉// This will draw the boot graphics unless we are in␊ |
198 | ␉// verbose mode.␊ |
199 | ␉if (gVerboseMode)␊ |
200 | ␉{␊ |
201 | ␉␉setVideoMode( GRAPHICS_MODE, 0 );␊ |
202 | ␉}␊ |
203 | ␉else␊ |
204 | ␉{␊ |
205 | ␉␉drawBootGraphics();␊ |
206 | ␉}␊ |
207 | ␊ |
208 | ␉DBG("Starting Darwin/%s [%s]\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64", gDarwinBuildVerStr);␊ |
209 | ␉DBG("Boot Args: %s\n", bootArgs->CommandLine);␊ |
210 | ␊ |
211 | ␉setupBooterLog();␊ |
212 | ␊ |
213 | ␉finalizeBootStruct();␊ |
214 | ␊ |
215 | ␉// Jump to kernel's entry point. There's no going back now.␊ |
216 | ␉if ( LION || MOUNTAIN_LION || MAVERICKS || YOSEMITE )␊ |
217 | ␉{␊ |
218 | ␉␉// Notify modules that the kernel is about to be started␊ |
219 | ␉␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);␊ |
220 | ␊ |
221 | ␉␉// Masking out so that Lion doesn't doublefault␊ |
222 | ␊ |
223 | ␉␉startprog( kernelEntry, bootArgs );␊ |
224 | ␉}␊ |
225 | ␉else␊ |
226 | ␉{␊ |
227 | ␉␉// Notify modules that the kernel is about to be started␊ |
228 | ␉␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);␊ |
229 | ␊ |
230 | ␉␉startprog( kernelEntry, bootArgsPreLion );␊ |
231 | ␉}␊ |
232 | ␊ |
233 | ␉// Not reached␊ |
234 | ␉return 0;␊ |
235 | }␊ |
236 | ␊ |
237 | ␊ |
238 | //==========================================================================␊ |
239 | // LoadKernelCache - Try to load Kernel Cache.␊ |
240 | // return the length of the loaded cache file or -1 on error␊ |
241 | long LoadKernelCache(const char* cacheFile, void **binary)␊ |
242 | {␊ |
243 | ␉char␉␉kernelCacheFile[512];␊ |
244 | ␉char␉␉kernelCachePath[512];␊ |
245 | ␉long␉␉flags, ret=-1;␊ |
246 | ␉unsigned long adler32;␊ |
247 | ␉u_int32_t time, cachetime, kerneltime, exttime;␊ |
248 | ␊ |
249 | ␉if((gBootMode & kBootModeSafe) != 0)␊ |
250 | ␉{␊ |
251 | ␉␉DBG("Kernel Cache ignored.\n");␊ |
252 | ␉␉return -1;␊ |
253 | ␉}␊ |
254 | ␊ |
255 | ␉// Use specify kernel cache file if not empty␊ |
256 | ␉if (cacheFile[0] != 0)␊ |
257 | ␉{␊ |
258 | ␉␉strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));␊ |
259 | ␉␉verbose("Specified kernel cache file path = %s\n", cacheFile);␊ |
260 | ␉}␊ |
261 | ␉else␊ |
262 | ␉{␊ |
263 | ␉␉// Lion, Mountain Lion, Mavericks, and Yosemite prelink kernel cache file␊ |
264 | ␉␉// for 10.7 10.8 10.9 10.10␊ |
265 | ␉␉if ( LION || MOUNTAIN_LION || MAVERICKS || YOSEMITE)␊ |
266 | ␉␉{␊ |
267 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);␊ |
268 | ␉␉␉verbose("10.7, 10.8, 10.9 & 10.10 kernel cache file path = %s\n", kernelCacheFile);␊ |
269 | ␉␉}␊ |
270 | ␉␉// Snow Leopard prelink kernel cache file␊ |
271 | ␉␉else if ( SNOW_LEOPARD )␊ |
272 | ␉␉{␊ |
273 | ␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",␊ |
274 | ␉␉␉␉(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");␊ |
275 | ␊ |
276 | ␉␉␉int␉lnam = strlen(kernelCacheFile) + 9; //with adler32␊ |
277 | ␉␉␉char *name;␊ |
278 | ␉␉␉u_int32_t prev_time = 0;␊ |
279 | ␊ |
280 | ␉␉␉struct␉dirstuff* cacheDir = opendir(kDefaultCachePathSnow);␊ |
281 | ␊ |
282 | ␉␉␉/* TODO: handle error? */␊ |
283 | ␉␉␉if (cacheDir)␊ |
284 | ␉␉␉{␊ |
285 | ␉␉␉␉while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)␊ |
286 | ␉␉␉␉{␊ |
287 | ␉␉␉␉␉if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time␊ |
288 | ␉␉␉␉␉␉&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))␊ |
289 | ␉␉␉␉␉{␊ |
290 | ␉␉␉␉␉␉snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);␊ |
291 | ␉␉␉␉␉␉prev_time = time;␊ |
292 | ␉␉␉␉␉}␊ |
293 | ␉␉␉␉}␊ |
294 | ␉␉␉␉verbose("Snow Leopard kernel cache file path = %s\n", kernelCacheFile);␊ |
295 | ␉␉␉}␊ |
296 | ␉␉␉closedir(cacheDir);␊ |
297 | ␉␉} else {␊ |
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 | ␊ |
307 | ␉// Check if the kernel cache file exists␊ |
308 | ␉ret = -1;␊ |
309 | ␊ |
310 | ␉// If boot from a boot helper partition check the kernel cache file on it␊ |
311 | ␉if (gBootVolume->flags & kBVFlagBooter)␊ |
312 | ␉{␊ |
313 | ␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);␊ |
314 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
315 | ␊ |
316 | ␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
317 | ␉␉{␊ |
318 | ␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);␊ |
319 | ␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
320 | ␊ |
321 | ␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
322 | ␉␉␉{␊ |
323 | ␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);␊ |
324 | ␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
325 | ␊ |
326 | ␉␉␉␉if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
327 | ␉␉␉␉{␊ |
328 | ␉␉␉␉␉snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.recovery.boot/kernelcache", kernelCacheFile);␊ |
329 | ␉␉␉␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
330 | ␊ |
331 | ␉␉␉␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
332 | ␉␉␉␉␉{␊ |
333 | ␉␉␉␉␉␉ret = -1;␊ |
334 | ␉␉␉␉␉}␊ |
335 | ␉␉␉␉}␊ |
336 | ␉␉␉}␊ |
337 | ␉␉}␊ |
338 | ␉}␊ |
339 | ␊ |
340 | ␉// If not found, use the original kernel cache path.␊ |
341 | ␉if (ret == -1)␊ |
342 | ␉{␊ |
343 | ␉␉strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));␊ |
344 | ␉␉ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);␊ |
345 | ␊ |
346 | ␉␉if ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
347 | ␉␉{␊ |
348 | ␉␉␉ret = -1;␊ |
349 | ␉␉}␊ |
350 | ␉}␊ |
351 | ␊ |
352 | ␉// Exit if kernel cache file wasn't found␊ |
353 | ␉if (ret == -1)␊ |
354 | ␉{␊ |
355 | ␉␉DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);␊ |
356 | ␉␉return -1;␊ |
357 | ␉}␊ |
358 | ␊ |
359 | ␉// Check if the kernel cache file is more recent (mtime)␊ |
360 | ␉// than the kernel file or the S/L/E directory␊ |
361 | ␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
362 | ␊ |
363 | ␉// Check if the kernel file is more recent than the cache file␊ |
364 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))␊ |
365 | ␉{␊ |
366 | ␉␉DBG("Kernel file (%s) is more recent than Kernel Cache (%s)! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);␊ |
367 | ␉␉return -1;␊ |
368 | ␉}␊ |
369 | ␊ |
370 | ␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
371 | ␊ |
372 | ␉// Check if the S/L/E directory time is more recent than the cache file␊ |
373 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))␊ |
374 | ␉{␊ |
375 | ␉␉DBG("Folder: '/System/Library/Extensions' is more recent than Kernel Cache file (%s)! Ignoring Kernel Cache.\n", kernelCacheFile);␊ |
376 | ␉␉return -1;␊ |
377 | ␉}␊ |
378 | ␊ |
379 | ␉// Since the kernel cache file exists and is the most recent try to load it␊ |
380 | ␉DBG("Loading kernel cache: '%s'\n", kernelCachePath);␊ |
381 | ␊ |
382 | ␉ret = LoadThinFatFile(kernelCachePath, binary);␊ |
383 | ␉return ret; // ret contain the length of the binary␊ |
384 | }␊ |
385 | ␊ |
386 | //==========================================================================␊ |
387 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
388 | // before. Multiboot does its own runtime initialization, does some of its␊ |
389 | // own things, and then calls common_boot.␊ |
390 | void boot(int biosdev)␊ |
391 | {␊ |
392 | ␉initialize_runtime();␊ |
393 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
394 | ␉enableA20();␊ |
395 | ␉common_boot(biosdev);␊ |
396 | }␊ |
397 | ␊ |
398 | //==========================================================================␊ |
399 | // The 'main' function for the booter. Called by boot0 when booting␊ |
400 | // from a block device, or by the network booter.␊ |
401 | //␊ |
402 | // arguments:␊ |
403 | //␉ biosdev - Value passed from boot1/NBP to specify the device␊ |
404 | //␉␉␉ that the booter was loaded from.␊ |
405 | //␊ |
406 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
407 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
408 | // next boot device on its list.␊ |
409 | void common_boot(int biosdev)␊ |
410 | {␊ |
411 | ␉bool␉ ␉␉quiet;␊ |
412 | ␉bool␉ ␉␉firstRun = true;␊ |
413 | ␉bool␉ ␉␉instantMenu;␊ |
414 | ␉bool␉ ␉␉rescanPrompt;␊ |
415 | ␉int␉␉␉␉status;␊ |
416 | ␉unsigned int␉allowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;␊ |
417 | ␉unsigned int␉denyBVFlags = kBVFlagEFISystem;␊ |
418 | ␊ |
419 | ␉// Set reminder to unload the PXE base code. Neglect to unload␊ |
420 | ␉// the base code will result in a hang or kernel panic.␊ |
421 | ␉gUnloadPXEOnExit = true;␊ |
422 | ␊ |
423 | ␉// Record the device that the booter was loaded from.␊ |
424 | ␉gBIOSDev = biosdev & kBIOSDevMask;␊ |
425 | ␊ |
426 | ␉// Initialize boot-log␊ |
427 | ␉initBooterLog();␊ |
428 | ␊ |
429 | ␉// Initialize boot info structure.␊ |
430 | ␉initKernBootStruct();␊ |
431 | ␊ |
432 | ␉// Setup VGA text mode.␊ |
433 | ␉// Not sure if it is safe to call setVideoMode() before the␊ |
434 | ␉// config table has been loaded. Call video_mode() instead.␊ |
435 | #if DEBUG␊ |
436 | ␉printf("before video_mode\n");␊ |
437 | #endif␊ |
438 | ␉video_mode( 2 ); // 80x25 mono text mode.␊ |
439 | #if DEBUG␊ |
440 | ␉printf("after video_mode\n");␊ |
441 | #endif␊ |
442 | ␊ |
443 | ␉// Scan and record the system's hardware information.␊ |
444 | ␉scan_platform();␊ |
445 | ␊ |
446 | ␉// First get info for boot volume.␊ |
447 | ␉scanBootVolumes(gBIOSDev, 0);␊ |
448 | ␉bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
449 | ␉setBootGlobals(bvChain);␊ |
450 | ␊ |
451 | ␉// Load boot.plist config file␊ |
452 | ␉status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);␊ |
453 | ␊ |
454 | ␉if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)␊ |
455 | ␉{␊ |
456 | ␉␉gBootMode |= kBootModeQuiet;␊ |
457 | ␉}␊ |
458 | ␊ |
459 | ␉// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
460 | ␉if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)␊ |
461 | ␉{␊ |
462 | ␉␉firstRun = false;␊ |
463 | ␉}␊ |
464 | ␊ |
465 | ␉// Loading preboot ramdisk if exists.␊ |
466 | ␉loadPrebootRAMDisk();␊ |
467 | ␊ |
468 | ␉// Disable rescan option by default␊ |
469 | ␉gEnableCDROMRescan = false;␊ |
470 | ␊ |
471 | ␉// Enable it with Rescan=y in system config␊ |
472 | ␉if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)␉&& gEnableCDROMRescan)␊ |
473 | ␉{␊ |
474 | ␉␉gEnableCDROMRescan = true;␊ |
475 | ␉}␊ |
476 | ␊ |
477 | ␉// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
478 | ␉rescanPrompt = false;␊ |
479 | ␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))␊ |
480 | ␉{␊ |
481 | ␉␉gEnableCDROMRescan = promptForRescanOption();␊ |
482 | ␉}␊ |
483 | ␊ |
484 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
485 | ␉if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)␊ |
486 | ␉{␊ |
487 | ␉␉gScanSingleDrive = true;␊ |
488 | ␉}␊ |
489 | ␊ |
490 | ␉// Create a list of partitions on device(s).␊ |
491 | ␉if (gScanSingleDrive)␊ |
492 | ␉{␊ |
493 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
494 | ␉}␊ |
495 | ␉else␊ |
496 | ␉{␊ |
497 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
498 | ␉}␊ |
499 | ␊ |
500 | ␉// Create a separated bvr chain using the specified filters.␊ |
501 | ␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
502 | ␊ |
503 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
504 | ␊ |
505 | ␉// Intialize module system␊ |
506 | ␉init_module_system();␊ |
507 | ␊ |
508 | #if DEBUG␊ |
509 | ␉printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",␊ |
510 | ␉␉␉ gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
511 | ␉printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",␊ |
512 | ␉␉␉ gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
513 | ␉getchar();␊ |
514 | #endif␊ |
515 | ␊ |
516 | ␉useGUI = true;␊ |
517 | ␉// Override useGUI default␊ |
518 | ␉getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);␊ |
519 | ␉if (useGUI && initGUI())␊ |
520 | ␉{␊ |
521 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
522 | ␉␉useGUI = false;␊ |
523 | ␉}␊ |
524 | ␊ |
525 | ␉setBootGlobals(bvChain);␊ |
526 | ␊ |
527 | ␉// Parse args, load and start kernel.␊ |
528 | ␉while (1)␊ |
529 | ␉{␊ |
530 | ␉␉bool␉␉tryresume, tryresumedefault, forceresume;␊ |
531 | ␉␉bool␉␉useKernelCache = true; // by default try to use the prelinked kernel␊ |
532 | ␉␉const char␉*val;␊ |
533 | ␉␉int␉␉␉len, ret = -1;␊ |
534 | ␉␉long␉␉flags;␊ |
535 | ␉␉u_int32_t␉sleeptime, time;␊ |
536 | ␉␉void␉␉*binary = (void *)kLoadAddr;␊ |
537 | ␊ |
538 | ␉␉char␉␉bootFile[sizeof(bootInfo->bootFile)];␊ |
539 | ␉␉char␉␉bootFilePath[512];␊ |
540 | ␉␉char␉␉kernelCacheFile[512];␊ |
541 | ␊ |
542 | ␉␉// Initialize globals.␊ |
543 | ␉␉sysConfigValid = false;␊ |
544 | ␉␉gErrors␉␉ = false;␊ |
545 | ␊ |
546 | ␉␉status = getBootOptions(firstRun);␊ |
547 | ␉␉firstRun = false;␊ |
548 | ␉␉if (status == -1) continue;␊ |
549 | ␊ |
550 | ␉␉status = processBootOptions();␊ |
551 | ␉␉// Status == 1 means to chainboot␊ |
552 | ␉␉if ( status ==␉1 ) break;␊ |
553 | ␉␉// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
554 | ␉␉if ( status == -1 )␊ |
555 | ␉␉{␊ |
556 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
557 | ␉␉␉if (gBootVolume == NULL)␊ |
558 | ␉␉␉{␊ |
559 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
560 | ␊ |
561 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
562 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
563 | ␊ |
564 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
565 | ␉␉␉␉setBootGlobals(bvChain);␊ |
566 | ␉␉␉␉setupDeviceList(&bootInfo->themeConfig);␊ |
567 | ␉␉␉}␊ |
568 | ␉␉␉continue;␊ |
569 | ␉␉}␊ |
570 | ␊ |
571 | ␉␉// Other status (e.g. 0) means that we should proceed with boot.␊ |
572 | ␊ |
573 | ␉␉// Turn off any GUI elements␊ |
574 | ␉␉if ( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
575 | ␉␉{␊ |
576 | ␉␉␉gui.devicelist.draw = false;␊ |
577 | ␉␉␉gui.bootprompt.draw = false;␊ |
578 | ␉␉␉gui.menu.draw = false;␊ |
579 | ␉␉␉gui.infobox.draw = false;␊ |
580 | ␉␉␉gui.logo.draw = false;␊ |
581 | ␉␉␉drawBackground();␊ |
582 | ␉␉␉updateVRAM();␊ |
583 | ␉␉}␊ |
584 | ␊ |
585 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T))␊ |
586 | ␉␉{␊ |
587 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
588 | ␉␉}␊ |
589 | ␉␉else␊ |
590 | ␉␉{␊ |
591 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
592 | ␉␉}␊ |
593 | ␊ |
594 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))␊ |
595 | ␉␉{␊ |
596 | ␉␉␉if (strncmp(val, "i386", 4) == 0)␊ |
597 | ␉␉␉{␊ |
598 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
599 | ␉␉␉}␊ |
600 | ␉␉␉else␊ |
601 | ␉␉␉{␊ |
602 | ␉␉␉␉DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");␊ |
603 | ␉␉␉}␊ |
604 | ␉␉}␊ |
605 | ␊ |
606 | ␉␉if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {␊ |
607 | ␉␉␉if (strncmp(val, "i386", 4) == 0)␊ |
608 | ␉␉␉{␊ |
609 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
610 | ␉␉␉}␊ |
611 | ␉␉}␊ |
612 | ␊ |
613 | ␉␉// Notify modules that we are attempting to boot␊ |
614 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL);␊ |
615 | ␊ |
616 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))␊ |
617 | ␉␉{␊ |
618 | ␉␉␉tryresume = true;␊ |
619 | ␉␉␉tryresumedefault = true;␊ |
620 | ␉␉}␊ |
621 | ␉␉else␊ |
622 | ␉␉{␊ |
623 | ␉␉␉tryresumedefault = false;␊ |
624 | ␉␉}␊ |
625 | ␊ |
626 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {␊ |
627 | ␉␉␉forceresume = false;␊ |
628 | ␉␉}␊ |
629 | ␊ |
630 | ␉␉if (forceresume)␊ |
631 | ␉␉{␊ |
632 | ␉␉␉tryresume = true;␊ |
633 | ␉␉␉tryresumedefault = false;␊ |
634 | ␉␉}␊ |
635 | ␊ |
636 | ␉␉while (tryresume)␊ |
637 | ␉␉{␊ |
638 | ␉␉␉const char *tmp;␊ |
639 | ␉␉␉BVRef bvr;␊ |
640 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))␊ |
641 | ␉␉␉␉val = "/private/var/vm/sleepimage";␊ |
642 | ␊ |
643 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
644 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
645 | ␊ |
646 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
647 | ␉␉␉␉break;␊ |
648 | ␊ |
649 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
650 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
651 | ␉␉␉␉break;␊ |
652 | ␊ |
653 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
654 | ␉␉␉␉break;␊ |
655 | ␊ |
656 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime))␊ |
657 | ␉␉␉{␊ |
658 | #if DEBUG␊ |
659 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",␊ |
660 | ␉␉␉␉␉␉bvr->modTime-sleeptime);␊ |
661 | #endif␊ |
662 | ␉␉␉␉break;␊ |
663 | ␉␉␉}␊ |
664 | ␊ |
665 | ␉␉␉HibernateBoot((char *)val);␊ |
666 | ␉␉␉break;␊ |
667 | ␉␉}␊ |
668 | ␊ |
669 | ␉␉getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);␊ |
670 | ␉␉if (useKernelCache) do {␊ |
671 | ␊ |
672 | ␉␉␉// Determine the name of the Kernel Cache␊ |
673 | ␉␉␉if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
674 | ␉␉␉␉if (val[0] == '\\')␊ |
675 | ␉␉␉␉{␊ |
676 | ␉␉␉␉␉len--;␊ |
677 | ␉␉␉␉␉val++;␊ |
678 | ␉␉␉␉}␊ |
679 | ␉␉␉␉/* FIXME: check len vs sizeof(kernelCacheFile) */␊ |
680 | ␉␉␉␉strlcpy(kernelCacheFile, val, len + 1);␊ |
681 | ␉␉␉}␊ |
682 | ␉␉␉else␊ |
683 | ␉␉␉{␊ |
684 | ␉␉␉␉kernelCacheFile[0] = 0; // Use default kernel cache file␊ |
685 | ␉␉␉}␊ |
686 | ␊ |
687 | ␉␉␉if (gOverrideKernel && kernelCacheFile[0] == 0)␊ |
688 | ␉␉␉{␊ |
689 | ␉␉␉␉DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);␊ |
690 | ␉␉␉␉useKernelCache = false;␊ |
691 | ␉␉␉␉break;␊ |
692 | ␉␉␉}␊ |
693 | ␉␉␉if (gMKextName[0] != 0)␊ |
694 | ␉␉␉{␊ |
695 | ␉␉␉␉DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n",␊ |
696 | ␉␉␉␉␉␉gMKextName);␊ |
697 | ␉␉␉␉useKernelCache = false;␊ |
698 | ␉␉␉␉break;␊ |
699 | ␉␉␉}␊ |
700 | ␉␉␉if (gBootFileType != kBlockDeviceType)␊ |
701 | ␉␉␉␉useKernelCache = false;␊ |
702 | ␊ |
703 | ␉␉} while(0);␊ |
704 | ␊ |
705 | ␉␉do {␊ |
706 | ␉␉␉if (useKernelCache)␊ |
707 | ␉␉␉{␊ |
708 | ␉␉␉␉ret = LoadKernelCache(kernelCacheFile, &binary);␊ |
709 | ␉␉␉␉if (ret >= 0)␊ |
710 | ␉␉␉␉{␊ |
711 | ␉␉␉␉␉break;␊ |
712 | ␉␉␉␉}␊ |
713 | ␉␉␉}␊ |
714 | ␊ |
715 | ␉␉␉bool bootFileWithDevice = false;␊ |
716 | ␉␉␉// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel␊ |
717 | ␉␉␉if (strncmp(bootInfo->bootFile,"bt(",3) == 0 ||␊ |
718 | ␉␉␉␉strncmp(bootInfo->bootFile,"hd(",3) == 0 ||␊ |
719 | ␉␉␉␉strncmp(bootInfo->bootFile,"rd(",3) == 0)␊ |
720 | ␉␉␉␉bootFileWithDevice = true;␊ |
721 | ␊ |
722 | ␉␉␉// bootFile must start with a / if it not start with a device name␊ |
723 | ␉␉␉if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')␊ |
724 | ␉␉␉{␊ |
725 | ␉␉␉␉if ( !YOSEMITE )␊ |
726 | ␉␉␉␉{␊ |
727 | ␉␉␉␉␉//printf(HEADER " (%s).\n", bootInfo->bootFile);␊ |
728 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /␊ |
729 | ␉␉␉␉␉//sleep(1);␊ |
730 | ␉␉␉␉}␊ |
731 | ␉␉␉␉else␊ |
732 | ␉␉␉␉{␊ |
733 | ␊ |
734 | ␉␉␉␉␉//printf(HEADER " (%s).\n", bootInfo->bootFile);␊ |
735 | ␉␉␉␉␉snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite␊ |
736 | ␉␉␉␉␉//sleep(1);␊ |
737 | ␉␉␉␉}␊ |
738 | ␉␉␉}␊ |
739 | ␉␉␉else␊ |
740 | ␉␉␉{␊ |
741 | ␉␉␉␉strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));␊ |
742 | ␉␉␉}␊ |
743 | ␊ |
744 | ␉␉␉// Try to load kernel image from alternate locations on boot helper partitions.␊ |
745 | ␉␉␉ret = -1;␊ |
746 | ␉␉␉if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)␊ |
747 | ␉␉␉{␊ |
748 | ␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);␊ |
749 | ␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
750 | ␉␉␉␉if (ret == -1)␊ |
751 | ␉␉␉␉{␊ |
752 | ␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);␊ |
753 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
754 | ␉␉␉␉␉if (ret == -1)␊ |
755 | ␉␉␉␉␉{␊ |
756 | ␉␉␉␉␉␉snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);␊ |
757 | ␉␉␉␉␉␉ret = GetFileInfo(NULL, bootFilePath, &flags, &time);␊ |
758 | ␉␉␉␉␉}␊ |
759 | ␉␉␉␉}␊ |
760 | ␉␉␉}␊ |
761 | ␉␉␉if (ret == -1)␊ |
762 | ␉␉␉{␊ |
763 | ␉␉␉␉// No alternate location found, using the original kernel image path.␊ |
764 | ␉␉␉␉strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));␊ |
765 | ␉␉␉}␊ |
766 | ␊ |
767 | ␉␉␉DBG("Loading kernel: '%s'\n", bootFilePath);␊ |
768 | ␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
769 | ␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
770 | ␉␉␉{␊ |
771 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
772 | ␉␉␉␉ret = LoadThinFatFile(bootFilePath, &binary);␊ |
773 | ␉␉␉}␊ |
774 | ␉␉} while (0);␊ |
775 | ␊ |
776 | ␉␉clearActivityIndicator();␊ |
777 | ␊ |
778 | #if DEBUG␊ |
779 | ␉␉printf("Pausing...");␊ |
780 | ␉␉sleep(8);␊ |
781 | #endif␊ |
782 | ␊ |
783 | ␉␉if (ret <= 0)␊ |
784 | ␉␉{␊ |
785 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
786 | ␉␉␉sleep(1);␊ |
787 | ␊ |
788 | ␉␉␉if (gBootFileType == kNetworkDeviceType)␊ |
789 | ␉␉␉{␊ |
790 | ␉␉␉␉// Return control back to PXE. Don't unload PXE base code.␊ |
791 | ␉␉␉␉gUnloadPXEOnExit = false;␊ |
792 | ␉␉␉␉break;␊ |
793 | ␉␉␉}␊ |
794 | ␉␉␉pause();␊ |
795 | ␊ |
796 | ␉␉}␊ |
797 | ␉␉else␊ |
798 | ␉␉{␊ |
799 | ␉␉␉/* Won't return if successful. */␊ |
800 | ␉␉␉ret = ExecKernel(binary);␊ |
801 | ␉␉}␊ |
802 | ␉}␊ |
803 | ␊ |
804 | ␉// chainboot␊ |
805 | ␉if (status == 1)␊ |
806 | ␉{␊ |
807 | ␉␉// if we are already in graphics-mode,␊ |
808 | ␉␉if (getVideoMode() == GRAPHICS_MODE)␊ |
809 | ␉␉{␊ |
810 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.␊ |
811 | ␉␉}␊ |
812 | ␉}␊ |
813 | ␊ |
814 | ␉if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)␊ |
815 | ␉{␊ |
816 | ␉␉nbpUnloadBaseCode();␊ |
817 | ␉}␊ |
818 | }␊ |
819 | ␊ |
820 | /*!␊ |
821 | ␉Selects a new BIOS device, taking care to update the global state appropriately.␊ |
822 | */␊ |
823 | /*␊ |
824 | static void selectBiosDevice(void)␊ |
825 | {␊ |
826 | ␉struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
827 | ␉CacheReset();␊ |
828 | ␉diskFreeMap(oldMap);␊ |
829 | ␉oldMap = NULL;␊ |
830 | ␊ |
831 | ␉int dev = selectAlternateBootDevice(gBIOSDev);␊ |
832 | ␊ |
833 | ␉BVRef bvchain = scanBootVolumes(dev, 0);␊ |
834 | ␉BVRef bootVol = selectBootVolume(bvchain);␊ |
835 | ␉gBootVolume = bootVol;␊ |
836 | ␉setRootVolume(bootVol);␊ |
837 | ␉gBIOSDev = dev;␊ |
838 | }␊ |
839 | */␊ |
840 | ␊ |
841 | bool checkOSVersion(const char * version)␊ |
842 | {␊ |
843 | ␉if ( (sizeof(version) > 4) && ('.' != version[4]) && ('\0' != version[4]) )␊ |
844 | ␉{␊ |
845 | ␉␉return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])␊ |
846 | ␉␉&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3])␊ |
847 | ␉␉&& (gMacOSVersion[4] == version[4]));␊ |
848 | ␉}␊ |
849 | ␉else␊ |
850 | ␉{␊ |
851 | ␉␉return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])␊ |
852 | ␉␉&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));␊ |
853 | ␉}␊ |
854 | }␊ |
855 | ␊ |
856 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
857 | #define NMAX 5000␊ |
858 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
859 | ␊ |
860 | #define DO1(buf, i)␉{s1 += buf[i]; s2 += s1;}␊ |
861 | #define DO2(buf, i)␉DO1(buf, i); DO1(buf, i + 1);␊ |
862 | #define DO4(buf, i)␉DO2(buf, i); DO2(buf, i + 2);␊ |
863 | #define DO8(buf, i)␉DO4(buf, i); DO4(buf, i + 4);␊ |
864 | #define DO16(buf)␉DO8(buf, 0); DO8(buf, 8);␊ |
865 | ␊ |
866 | unsigned long Adler32(unsigned char *buf, long len)␊ |
867 | {␊ |
868 | ␉unsigned long s1 = 1; // adler & 0xffff;␊ |
869 | ␉unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
870 | ␉unsigned long result;␊ |
871 | ␉int k;␊ |
872 | ␊ |
873 | ␉while (len > 0) {␊ |
874 | ␉␉k = len < NMAX ? len : NMAX;␊ |
875 | ␉␉len -= k;␊ |
876 | ␉␉while (k >= 16) {␊ |
877 | ␉␉␉DO16(buf);␊ |
878 | ␉␉␉buf += 16;␊ |
879 | ␉␉␉k -= 16;␊ |
880 | ␉␉}␊ |
881 | ␉␉if (k != 0) do {␊ |
882 | ␉␉␉s1 += *buf++;␊ |
883 | ␉␉␉s2 += s1;␊ |
884 | ␉␉} while (--k);␊ |
885 | ␉␉s1 %= BASE;␊ |
886 | ␉␉s2 %= BASE;␊ |
887 | ␉}␊ |
888 | ␉result = (s2 << 16) | s1;␊ |
889 | ␉return OSSwapHostToBigInt32(result);␊ |
890 | }␊ |
891 | |