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