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