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