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