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