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