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