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