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