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