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 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * All rights reserved. The CMU software License Agreement specifies␊ |
29 | * the terms and conditions for use and redistribution.␊ |
30 | */␊ |
31 | ␊ |
32 | /*␊ |
33 | * INTEL CORPORATION PROPRIETARY INFORMATION␊ |
34 | *␊ |
35 | * This software is supplied under the terms of a license agreement or ␊ |
36 | * nondisclosure agreement with Intel Corporation and may not be copied ␊ |
37 | * nor disclosed except in accordance with the terms of that agreement.␊ |
38 | *␊ |
39 | * Copyright 1988, 1989 by Intel Corporation␊ |
40 | */␊ |
41 | ␊ |
42 | /*␊ |
43 | * Copyright 1993 NeXT Computer, Inc.␊ |
44 | * All rights reserved.␊ |
45 | */␊ |
46 | ␊ |
47 | /*␊ |
48 | * Completely reworked by Sam Streeper (sam_s@NeXT.com)␊ |
49 | * Reworked again by Curtis Galloway (galloway@NeXT.com)␊ |
50 | */␊ |
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 "platform.h"␊ |
59 | #include "graphics.h"␊ |
60 | #ifndef OPTION_ROM␊ |
61 | #include "appleboot.h"␊ |
62 | #endif␊ |
63 | #include "modules.h"␊ |
64 | #include "xml.h"␊ |
65 | ␊ |
66 | #ifndef DEBUG_BOOT␊ |
67 | #define DEBUG_BOOT 0␊ |
68 | #endif␊ |
69 | ␊ |
70 | #if DEBUG_BOOT␊ |
71 | #define DBG(x...)␉␉printf(x)␊ |
72 | #else␊ |
73 | #define DBG(x...)␉␉␊ |
74 | #endif␊ |
75 | ␊ |
76 | ␊ |
77 | typedef struct platform_info {␊ |
78 | ␉char platformName[PLATFORM_NAME_LEN];␊ |
79 | ␉char rootPath[ROOT_PATH_LEN];␊ |
80 | } PlatformInfo;␊ |
81 | ␊ |
82 | ␊ |
83 | char *gboardproduct = NULL;␊ |
84 | char *gPlatformName = NULL;␊ |
85 | //char gRootPath[256];␊ |
86 | long gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
87 | bool gOverrideKernel;␊ |
88 | char gBootKernelCacheFile[512];␊ |
89 | char gMKextName[512];␊ |
90 | char gMacOSVersion[8];␊ |
91 | char *gRootDevice = NULL;␊ |
92 | bool uuidSet = false;␊ |
93 | #ifndef OPTION_ROM␊ |
94 | bool gEnableCDROMRescan;␊ |
95 | bool gScanSingleDrive;␊ |
96 | #endif␊ |
97 | ␊ |
98 | int bvCount = 0;␊ |
99 | //int␉menucount = 0;␊ |
100 | int gDeviceCount = 0; ␊ |
101 | ␊ |
102 | BVRef bvr;␊ |
103 | BVRef menuBVR;␊ |
104 | BVRef bvChain;␊ |
105 | ␊ |
106 | //static void selectBiosDevice(void);␊ |
107 | ␊ |
108 | static bool getOSVersion(char *str);␊ |
109 | static void getRootDevice();␊ |
110 | ␊ |
111 | #ifdef NBP_SUPPORT␊ |
112 | static bool gUnloadPXEOnExit = false;␊ |
113 | #endif␊ |
114 | ␊ |
115 | /*␊ |
116 | * How long to wait (in seconds) to load the␊ |
117 | * kernel after displaying the "boot:" prompt.␊ |
118 | */␊ |
119 | #define kBootErrorTimeout 5␊ |
120 | ␊ |
121 | /*␊ |
122 | * Default path to kernel cache file␊ |
123 | */␊ |
124 | #define kDefaultCachePath "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"␊ |
125 | ␊ |
126 | //==========================================================================␊ |
127 | // Zero the BSS.␊ |
128 | ␊ |
129 | static void zeroBSS(void)␊ |
130 | {␊ |
131 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
132 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
133 | ␉␊ |
134 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
135 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
136 | }␊ |
137 | ␊ |
138 | //==========================================================================␊ |
139 | // Malloc error function␊ |
140 | ␊ |
141 | #ifdef SAFE_MALLOC␊ |
142 | static inline void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
143 | {␊ |
144 | stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);␊ |
145 | }␊ |
146 | #else␊ |
147 | static inline void malloc_error(char *addr, size_t size)␊ |
148 | {␊ |
149 | printf("\nMemory allocation error (0x%x, 0x%x)\n", (unsigned)addr, (unsigned)size);␊ |
150 | asm volatile ("hlt");␊ |
151 | }␊ |
152 | #endif␊ |
153 | ␊ |
154 | //==========================================================================␊ |
155 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
156 | //␊ |
157 | void initialize_runtime(void)␊ |
158 | {␊ |
159 | ␉zeroBSS();␊ |
160 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
161 | }␊ |
162 | ␊ |
163 | //==========================================================================␊ |
164 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
165 | ␊ |
166 | static int ExecKernel(void *binary)␊ |
167 | {␊ |
168 | entry_t kernelEntry;␊ |
169 | int ret;␊ |
170 | ␉␊ |
171 | bootArgs->kaddr = bootArgs->ksize = 0;␊ |
172 | ␉␊ |
173 | ␉if(gMacOSVersion[3] <= '6')␊ |
174 | ␉{␊ |
175 | ␉␉bootArgs->Version = kBootArgsVersion1;␉␉␊ |
176 | ␉␉bootArgs->Revision = gMacOSVersion[3];␉␊ |
177 | ␉}␊ |
178 | ␉else ␊ |
179 | ␉{␉␉␊ |
180 | #if kBootArgsVersion > 1␊ |
181 | ␉␉␊ |
182 | ␉␉bootArgs->Version = kBootArgsVersion;␉␉␊ |
183 | ␉␉bootArgs->Revision = kBootArgsRevision;␊ |
184 | #else␊ |
185 | ␉␉bootArgs->Version = 2;␊ |
186 | bootArgs->Revision = 0;␊ |
187 | #endif␊ |
188 | ␉}␊ |
189 | ␉␊ |
190 | ␉execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL, NULL, NULL);␊ |
191 | ␊ |
192 | ret = DecodeKernel(binary,␊ |
193 | &kernelEntry,␊ |
194 | (char **) &bootArgs->kaddr,␊ |
195 | (int *)&bootArgs->ksize );␊ |
196 | ␊ |
197 | if ( ret != 0 )␊ |
198 | return ret;␊ |
199 | ␉␊ |
200 | // Reserve space for boot args for 10.7 only (for 10.6 and earlier, we will convert (to legacy) the structure and reserve kernel memory for it later.)␊ |
201 | ␉if(gMacOSVersion[3] == '7')␊ |
202 | reserveKernBootStruct();␊ |
203 | ␉␊ |
204 | // Load boot drivers from the specifed root path.␊ |
205 | ␉␊ |
206 | if (!gHaveKernelCache) {␊ |
207 | ␉␉LoadDrivers("/");␊ |
208 | }␊ |
209 | ␉␊ |
210 | #if TEXT_SPINNER␊ |
211 | ␉clearActivityIndicator();␊ |
212 | #endif␊ |
213 | ␉␊ |
214 | if (gErrors) {␊ |
215 | printf("Errors encountered while starting up the computer.\n");␊ |
216 | printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
217 | sleep(kBootErrorTimeout);␊ |
218 | }␊ |
219 | ␉␊ |
220 | ␉execute_hook("md0Ramdisk", NULL, NULL, NULL, NULL, NULL, NULL);␊ |
221 | ␊ |
222 | setupFakeEfi();␊ |
223 | ␉␉␊ |
224 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
225 | #ifdef NBP_SUPPORT␊ |
226 | // Cleanup the PXE base code.␊ |
227 | ␉␊ |
228 | if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {␊ |
229 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
230 | {␊ |
231 | ␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
232 | sleep(2);␊ |
233 | }␊ |
234 | }␊ |
235 | #endif ␊ |
236 | bool wait = false;␊ |
237 | ␉const char *strval = 0;␊ |
238 | ␉int dummysize /*= 0*/;␉␊ |
239 | ␉␊ |
240 | ␉getBoolForKey(kWaitForKeypressKey, &wait, &bootInfo->bootConfig);␊ |
241 | ␉␊ |
242 | ␉if (getValueForBootKey(bootArgs->CommandLine, "-wait", &strval, &dummysize))␊ |
243 | ␉{␊ |
244 | ␉␉wait = true;␊ |
245 | ␉␉␊ |
246 | ␉␉if (strval && ((strcmp(strval, "no") == 0) || (strcmp(strval, "No") == 0))) {␊ |
247 | ␉␉␉wait = false;␊ |
248 | ␉␉} ␉␉␊ |
249 | ␉}␊ |
250 | ␉␉␊ |
251 | ␉if (wait == true) {␊ |
252 | ␉␉pause();␊ |
253 | ␉}␊ |
254 | ␉␉␉␊ |
255 | ␉if ((execute_hook("GUI_ExecKernel", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)) // (bootArgs->Video.v_display == VGA_TEXT_MODE)␉␊ |
256 | ␉{␊ |
257 | #if UNUSED␊ |
258 | ␉␉setVideoMode( GRAPHICS_MODE, 0 );␊ |
259 | #else␊ |
260 | ␉␉setVideoMode( GRAPHICS_MODE );␊ |
261 | #endif␊ |
262 | ␉␉␊ |
263 | #ifndef OPTION_ROM␊ |
264 | ␉␉ ␊ |
265 | ␉␉if(!gVerboseMode)␊ |
266 | ␉␉{␉␉␉␊ |
267 | ␉␉␉drawColorRectangle(0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 0x01); ␊ |
268 | ␉␉␊ |
269 | ␉␉␉uint8_t *appleBootPict; ␊ |
270 | ␉␉␉uint16_t bootImageWidth = kAppleBootWidth; ␊ |
271 | ␉␉␉uint16_t bootImageHeight = kAppleBootHeight; ␊ |
272 | ␉␉␉uint8_t *bootImageData = NULL; ␉␉ ␊ |
273 | ␉␉␉uint16_t x, y;␊ |
274 | ␉␉␉␊ |
275 | ␉␉␉unsigned long screen_params[4] = {DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32, 0};␉// here we store the used screen resolution␊ |
276 | ␉␉␉// Prepare the data for the default Apple boot image. ␊ |
277 | ␉␉␉appleBootPict = (uint8_t *) decodeRLE(gAppleBootPictRLE, kAppleBootRLEBlocks, bootImageWidth * bootImageHeight); ␊ |
278 | ␉␉␉if (appleBootPict) { ␊ |
279 | ␉␉␉␉convertImage(bootImageWidth, bootImageHeight, appleBootPict, &bootImageData); ␊ |
280 | ␉␉␉␉if (bootImageData) {␉␊ |
281 | ␉␉␉␉␉x = (screen_params[0] - MIN(kAppleBootWidth, screen_params[0])) / 2; ␊ |
282 | ␉␉␉␉␉y = (screen_params[1] - MIN(kAppleBootHeight, screen_params[1])) / 2; ␊ |
283 | ␉␉␉␉␉drawDataRectangle(x, y, kAppleBootWidth, kAppleBootHeight, bootImageData);␊ |
284 | ␉␉␉␉␉free(bootImageData);␊ |
285 | ␉␉␉␉}␊ |
286 | ␉␉␉␉free(appleBootPict); ␊ |
287 | ␉␉␉}␊ |
288 | ␉␉␊ |
289 | ␉␉}␊ |
290 | #endif␊ |
291 | ␉}␊ |
292 | ␊ |
293 | finalizeEFIConfigTable();␊ |
294 | ␊ |
295 | ␉setupBooterLog();␊ |
296 | ␉␊ |
297 | finalizeBootStruct(); ␊ |
298 | ␊ |
299 | ␉␊ |
300 | ␉if (gMacOSVersion[3] <= '6') ␊ |
301 | ␉␉reserveKernLegacyBootStruct(); ␉␉␊ |
302 | ␉␊ |
303 | ␉␊ |
304 | ␉//uint8_t Pic1,Pic2;␊ |
305 | ␉␊ |
306 | ␉//Pic1 = inb(0x21); /* Save all interrupts Pic1 */␊ |
307 | ␉//Pic2 = inb(0xa1); /* Save all interrupts Pic2 */␊ |
308 | ␉␊ |
309 | ␉␊ |
310 | ␉␊ |
311 | ␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL, NULL, NULL);␉// Notify modules that the kernel is about to be started␊ |
312 | //#if UNUSED␊ |
313 | ␉turnOffFloppy();␊ |
314 | //#endif␊ |
315 | //#if BETA␊ |
316 | #include "smp.h"␊ |
317 | #include "apic.h"␊ |
318 | ␉IMPS_LAPIC_WRITE(LAPIC_LVT1, LAPIC_ICR_DM_NMI);␊ |
319 | //#endif␊ |
320 | ␉␊ |
321 | ␉if (gMacOSVersion[3] <= '6') {␊ |
322 | ␉␉␊ |
323 | ␉␉//outb(0x21, Pic1); /* Restore all interrupts Pic1 */␊ |
324 | ␉␉//outb(0xa1, Pic2); /* Restore all interrupts Pic2 */␊ |
325 | ␉␉␊ |
326 | ␉␉// Jump to kernel's entry point. There's no going back now. XXX LEGACY OS XXX␊ |
327 | ␉␉startprog( kernelEntry, bootArgsLegacy );␊ |
328 | ␉}␉␊ |
329 | ␉␉␉␊ |
330 | ␉outb(0x21, 0xff); /* Maskout all interrupts Pic1 */␊ |
331 | ␉outb(0xa1, 0xff); /* Maskout all interrupts Pic2 */␊ |
332 | ␉␉␊ |
333 | ␉// Jump to kernel's entry point. There's no going back now. XXX LION XXX␊ |
334 | startprog( kernelEntry, bootArgs );␊ |
335 | ␉␊ |
336 | // Not reached␊ |
337 | ␉␊ |
338 | return 0;␊ |
339 | }␊ |
340 | ␊ |
341 | //==========================================================================␊ |
342 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
343 | // before. Multiboot does its own runtime initialization, does some of its␊ |
344 | // own things, and then calls common_boot.␊ |
345 | void boot(int biosdev)␊ |
346 | {␊ |
347 | ␉initialize_runtime();␊ |
348 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
349 | ␉enableA20();␊ |
350 | ␉common_boot(biosdev);␊ |
351 | }␊ |
352 | ␊ |
353 | //==========================================================================␊ |
354 | // The 'main' function for the booter. Called by boot0 when booting␊ |
355 | // from a block device, or by the network booter.␊ |
356 | //␊ |
357 | // arguments:␊ |
358 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
359 | // that the booter was loaded from.␊ |
360 | //␊ |
361 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
362 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
363 | // next boot device on its list.␊ |
364 | void common_boot(int biosdev)␊ |
365 | {␊ |
366 | int status;␊ |
367 | char *bootFile;␊ |
368 | bool quiet;␊ |
369 | bool firstRun = true;␊ |
370 | bool instantMenu;␊ |
371 | #ifndef OPTION_ROM␊ |
372 | bool rescanPrompt;␊ |
373 | #endif␊ |
374 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
375 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
376 | ␉␊ |
377 | #ifdef NBP_SUPPORT␊ |
378 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
379 | // the base code will result in a hang or kernel panic.␊ |
380 | gUnloadPXEOnExit = true;␊ |
381 | #endif␊ |
382 | // Record the device that the booter was loaded from.␊ |
383 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
384 | ␉␊ |
385 | ␉␊ |
386 | ␉␊ |
387 | // Setup VGA text mode.␊ |
388 | // Not sure if it is safe to call setVideoMode() before the␊ |
389 | // config table has been loaded. Call video_mode() instead.␊ |
390 | #if DEBUG␊ |
391 | printf("before video_mode\n");␊ |
392 | #endif␊ |
393 | video_mode( 2 ); // 80x25 mono text mode.␊ |
394 | #if DEBUG␊ |
395 | printf("after video_mode\n");␊ |
396 | #endif␊ |
397 | #if !TEXT_SPINNER␊ |
398 | ␉printf("Starting Chameleon ...\n");␊ |
399 | #endif␊ |
400 | ␉␊ |
401 | ␉initBooterLog();␊ |
402 | ␉␊ |
403 | ␉// Initialize boot info structure.␊ |
404 | initKernBootStruct();␊ |
405 | ␉␊ |
406 | ␉␊ |
407 | // Scan and record the system's hardware information.␊ |
408 | scan_platform();␊ |
409 | ␉␊ |
410 | // First get info for boot volume.␊ |
411 | scanBootVolumes(gBIOSDev, 0);␊ |
412 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
413 | setBootGlobals(bvChain);␊ |
414 | ␉␊ |
415 | // Load boot.plist config file␊ |
416 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
417 | ␉␊ |
418 | ␉Platform->CPU.isServer = false; ␊ |
419 | getBoolForKey(kIsServer, &Platform->CPU.isServer, &bootInfo->bootConfig); // set this as soon as possible␊ |
420 | ␉␊ |
421 | if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {␊ |
422 | gBootMode |= kBootModeQuiet;␊ |
423 | }␊ |
424 | ␉␊ |
425 | // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
426 | if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {␊ |
427 | firstRun = false;␊ |
428 | }␊ |
429 | ␉␊ |
430 | #ifndef OPTION_ROM␊ |
431 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
432 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {␊ |
433 | gScanSingleDrive = true;␊ |
434 | }␊ |
435 | ␉␊ |
436 | ␉// Create a list of partitions on device(s).␊ |
437 | if (gScanSingleDrive)␊ |
438 | ␉{␊ |
439 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
440 | } ␊ |
441 | ␉else ␊ |
442 | #endif␊ |
443 | ␉{␊ |
444 | #if UNUSED␊ |
445 | scanDisks(gBIOSDev, &bvCount);␊ |
446 | #else␊ |
447 | scanDisks();␊ |
448 | #endif␊ |
449 | }␊ |
450 | ␉␊ |
451 | ␉// Create a separated bvr chain using the specified filters.␊ |
452 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
453 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
454 | ␉␊ |
455 | ␉// Intialize module system␊ |
456 | ␉EFI_STATUS sysinit = init_module_system();␊ |
457 | ␉if((sysinit == EFI_SUCCESS) || (sysinit == EFI_ALREADY_STARTED)/*should never happen*/ ) ␊ |
458 | ␉{␊ |
459 | ␉␉load_all_modules();␊ |
460 | ␉}␊ |
461 | ␉␉␉␊ |
462 | // Loading preboot ramdisk if exists.␊ |
463 | ␉execute_hook("loadPrebootRAMDisk", NULL, NULL, NULL, NULL, NULL, NULL);␉␉␊ |
464 | ␊ |
465 | #ifndef OPTION_ROM␊ |
466 | ␊ |
467 | // Disable rescan option by default␊ |
468 | gEnableCDROMRescan = false;␊ |
469 | ␉␊ |
470 | // Enable it with Rescan=y in system config␊ |
471 | if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {␊ |
472 | gEnableCDROMRescan = true;␊ |
473 | }␊ |
474 | ␉␊ |
475 | // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
476 | rescanPrompt = false;␊ |
477 | if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {␊ |
478 | gEnableCDROMRescan = promptForRescanOption();␊ |
479 | }␊ |
480 | #endif␉␊ |
481 | ␉␊ |
482 | #if DEBUG␊ |
483 | printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
484 | printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
485 | getc();␊ |
486 | #endif␊ |
487 | ␉␉␉␊ |
488 | setBootGlobals(bvChain);␊ |
489 | ␉␊ |
490 | ␉// Display the GUI␊ |
491 | ␉execute_hook("GUI_Display", NULL, NULL, NULL, NULL, NULL, NULL);␊ |
492 | ␊ |
493 | // Parse args, load and start kernel.␊ |
494 | while (1) {␊ |
495 | const char *val;␊ |
496 | int len;␊ |
497 | int trycache = 0;␊ |
498 | long flags, cachetime, kerneltime, exttime;␊ |
499 | #ifdef BOOT_HELPER_SUPPORT␉␉␊ |
500 | ␉␉long time;␊ |
501 | #endif␊ |
502 | int ret = -1;␊ |
503 | void *binary = (void *)kLoadAddr;␊ |
504 | ␉␉␊ |
505 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
506 | char bootFileSpec[512];␊ |
507 | ␉␉␊ |
508 | // Initialize globals.␊ |
509 | ␉␉␊ |
510 | sysConfigValid = false;␊ |
511 | gErrors = false;␊ |
512 | ␉␉␊ |
513 | status = getBootOptions(firstRun);␊ |
514 | firstRun = false;␊ |
515 | if (status == -1) continue;␊ |
516 | ␉␉␊ |
517 | status = processBootOptions();␊ |
518 | // Status==1 means to chainboot␊ |
519 | if ( status == 1 ) break;␊ |
520 | // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
521 | if ( status == -1 )␊ |
522 | {␊ |
523 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
524 | ␉␉␉if(gBootVolume == NULL)␊ |
525 | ␉␉␉{␊ |
526 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
527 | #ifndef OPTION_ROM␊ |
528 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
529 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
530 | #endif␊ |
531 | ␉␉␉␉␊ |
532 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
533 | ␉␉␉␉setBootGlobals(bvChain);␊ |
534 | ␉␉␉}␊ |
535 | ␉␉␉continue;␊ |
536 | }␊ |
537 | ␉␉␉␉␉␉␊ |
538 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
539 | ␉␉execute_hook("GUI_PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);␊ |
540 | ␉␉␊ |
541 | ␉␉// Find out which version mac os we're booting.␊ |
542 | ␉␉getOSVersion(gMacOSVersion);␊ |
543 | ␉␉␊ |
544 | ␉␉//if (platformCPUFeature(CPU_FEATURE_EM64T)) { ␊ |
545 | if (cpu_mode_is64bit()) {␊ |
546 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
547 | ␉␉} else {␊ |
548 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
549 | ␉␉}␊ |
550 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {␊ |
551 | ␉␉␉if (strncmp(val, "i386", 4) == 0) {␊ |
552 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
553 | ␉␉␉}␊ |
554 | ␉␉}␊ |
555 | ␉␉␊ |
556 | ␉␉getRootDevice();␊ |
557 | ␊ |
558 | ␉␉// Notify to all modules that we are attempting to boot␊ |
559 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL, NULL, NULL); ␊ |
560 | ␉␉␊ |
561 | ␉␉if (execute_hook("getProductNamePatched", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)␉␉␉␉␉␊ |
562 | ␉␉␉readSMBIOS(thePlatformName); // read smbios Platform Name␊ |
563 | ␉␉␊ |
564 | ␉␉␊ |
565 | ␉␉if (!getValueForBootKey(bootArgs->CommandLine, kIgnorePrelinkKern, &val, &len)) {␉␉␊ |
566 | ␉␉␊ |
567 | ␉␉␉if(gMacOSVersion[3] == '7'){␉␉␉␉␉␊ |
568 | ␉␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s", kDefaultCachePath);␊ |
569 | ␉␉␉␉}␊ |
570 | ␉␉␉␉else if(gMacOSVersion[3] <= '6')␊ |
571 | ␉␉␉␉{␉␉␉ ␊ |
572 | ␊ |
573 | ␉␉␉␉␉␉PlatformInfo *platformInfo = malloc(sizeof(PlatformInfo));␊ |
574 | ␉␉␉␉␉␉if (platformInfo) {␊ |
575 | ␉␉␉␉␉␉␉␊ |
576 | ␉␉␉␉␉␉␉bzero(platformInfo, sizeof(PlatformInfo));␊ |
577 | ␉␉␉␉␉␉␉␊ |
578 | ␉␉␉␉␉␉␉if (gPlatformName)␊ |
579 | ␉␉␉␉␉␉␉␉strlcpy(platformInfo->platformName,gPlatformName, sizeof(platformInfo->platformName)+1);␊ |
580 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␊ |
581 | ␉␉␉␉␉␉␉if (gRootDevice) {␊ |
582 | ␉␉␉␉␉␉␉␉char *rootPath_p = platformInfo->rootPath;␊ |
583 | int len = strlen(gRootDevice) + 1;␊ |
584 | if ((unsigned)len > sizeof(platformInfo->rootPath)) {␊ |
585 | len = sizeof(platformInfo->rootPath);␊ |
586 | }␊ |
587 | ␉␉␉␉␉␉␉␉memcpy(rootPath_p, gRootDevice,len);␊ |
588 | ␊ |
589 | ␉␉␉␉␉␉␉␉rootPath_p += len;␊ |
590 | ␊ |
591 | len = strlen(bootInfo->bootFile);␊ |
592 | ␊ |
593 | if ((unsigned)(rootPath_p - platformInfo->rootPath + len) >=␊ |
594 | sizeof(platformInfo->rootPath)) {␊ |
595 | ␊ |
596 | len = sizeof(platformInfo->rootPath) -␊ |
597 | (rootPath_p - platformInfo->rootPath);␊ |
598 | } ␊ |
599 | ␉␉␉␉␉␉␉␉memcpy(rootPath_p, bootInfo->bootFile, len);␉␉␉␉␉␉␉␊ |
600 | ␉␉␉␉␉␉␉␉␊ |
601 | ␉␉␉␉␉␉␉}␉␊ |
602 | ␉␉␉␉␉␉␉␊ |
603 | ␉␉␉␉␉␉␉if (!platformInfo->platformName[0] || !platformInfo->rootPath[0]) {␊ |
604 | ␉␉␉␉␉␉␉␉platformInfo->platformName[0] = platformInfo->rootPath[0] = 0;␊ |
605 | ␉␉␉␉␉␉␉}␊ |
606 | ␉␉␉␉␉␉␉//memcpy(gRootPath,platformInfo->rootPath, sizeof(platformInfo->rootPath));␊ |
607 | ␊ |
608 | ␉␉␉␉␉␉␉␊ |
609 | ␉␉␉␉␉␉␉bootInfo->adler32 = OSSwapHostToBigInt32(local_adler32((unsigned char *)platformInfo, sizeof(*platformInfo)));␊ |
610 | ␉␉␉␉␉␉␉␊ |
611 | ␉␉␉␉␉␉␉free(platformInfo);␉␊ |
612 | ␉␉␉␉␉␉}␊ |
613 | ␊ |
614 | ␉␉␉␉␉DBG("Adler32: %08lX\n",bootInfo->adler32);␊ |
615 | ␉␉␉␉␊ |
616 | ␉␉␉␉␉if (gMacOSVersion[3] < '6') {␊ |
617 | ␉␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s.%08lX", "/System/Library/Caches/com.apple.kernelcaches/kernelcache",bootInfo->adler32);␊ |
618 | ␉␉␉␉␉␉ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
619 | ␉␉␉␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
620 | ␉␉␉␉␉␉␉bootInfo->adler32 = 0;␊ |
621 | ␉␉␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s", "/System/Library/Caches/com.apple.kernelcaches/kernelcache"); ␊ |
622 | ␉␉␉␉␉␉}␉␉␉␉␉␉␊ |
623 | ␉␉␉␉␉} else␊ |
624 | ␉␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s_%s.%08lX", kDefaultCachePath, (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", bootInfo->adler32); //Snow Leopard␊ |
625 | ␉␉␉␉␉␉␉␉␊ |
626 | ␉␉␉␉␉␉␉␉␊ |
627 | ␉␉␉␉}␉␉␉␉␉␊ |
628 | ␊ |
629 | ␉␉}␊ |
630 | ␉␉␊ |
631 | ␉␉// Check for cache file.␊ |
632 | ␉␉trycache = (((gBootMode & kBootModeSafe) == 0) &&␊ |
633 | ␉␉␉␉␉!gOverrideKernel &&␊ |
634 | ␉␉␉␉␉(gBootFileType == kBlockDeviceType) &&␊ |
635 | ␉␉␉␉␉(gMKextName[0] == '\0') &&␊ |
636 | ␉␉␉␉␉(gBootKernelCacheFile[0] != '\0'));␊ |
637 | ␉␉␊ |
638 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
639 | ␉␉␊ |
640 | if (trycache ) do {␊ |
641 | ␉␉␉␊ |
642 | // if we haven't found the kernel yet, don't use the cache␊ |
643 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
644 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
645 | trycache = 0;␊ |
646 | ␉␉␉␉bootInfo->adler32 = 0;␊ |
647 | break;␊ |
648 | }␊ |
649 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
650 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
651 | || (cachetime < kerneltime)) {␊ |
652 | ␉␉␉␉trycache = 0;␊ |
653 | ␉␉␉␉bootInfo->adler32 = 0;␊ |
654 | ␉␉␉␉break;␉␉␉␉ ␊ |
655 | } ␊ |
656 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
657 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
658 | && (cachetime < exttime)) {␊ |
659 | trycache = 0;␊ |
660 | ␉␉␉␉bootInfo->adler32 = 0;␊ |
661 | break;␊ |
662 | }␊ |
663 | if (kerneltime > exttime) {␊ |
664 | exttime = kerneltime;␊ |
665 | }␊ |
666 | if (cachetime != (exttime + 1)) {␊ |
667 | trycache = 0;␊ |
668 | ␉␉␉␉bootInfo->adler32 = 0;␊ |
669 | break;␊ |
670 | }␊ |
671 | } while (0);␊ |
672 | ␉␉␊ |
673 | do {␊ |
674 | if (trycache) {␊ |
675 | bootFile = gBootKernelCacheFile;␊ |
676 | verbose("Loading kernel cache %s\n", bootFile);␊ |
677 | ␉␉␉␉if (gMacOSVersion[3] == '7') {␉␉␉␉␉␊ |
678 | ␉␉␉␉␉ret = LoadThinFatFile(bootFile, &binary);␊ |
679 | ␉␉␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
680 | ␉␉␉␉␉{␊ |
681 | ␉␉␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
682 | ␉␉␉␉␉␉ret = LoadThinFatFile(bootFile, &binary);␉␉␉␉␊ |
683 | ␉␉␉␉␉}␊ |
684 | ␉␉␉␉} else {␊ |
685 | ␉␉␉␉␉ret = LoadFile(bootFile);␊ |
686 | ␉␉␉␉␉binary = (void *)kLoadAddr;␊ |
687 | ␉␉␉␉}␊ |
688 | ␉␉␉␉␊ |
689 | if (ret >= 0) {␊ |
690 | break;␊ |
691 | }␊ |
692 | ␉␉␉␉␊ |
693 | }␊ |
694 | ␉␉␉bootInfo->adler32 = 0;␊ |
695 | bootFile = bootInfo->bootFile;␊ |
696 | #ifdef BOOT_HELPER_SUPPORT␊ |
697 | ␉␉␉␊ |
698 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
699 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
700 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
701 | ␉ ␉ if (ret == -1)␊ |
702 | ␉ ␉ {␊ |
703 | ␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
704 | ␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
705 | ␉␉␉␉if (ret == -1)␊ |
706 | ␉␉␉␉{␊ |
707 | ␉␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
708 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
709 | ␉␉␉␉␉if (ret == -1)␊ |
710 | ␉␉␉␉␉{␊ |
711 | ␉␉␉␉␉␉// Not found any alternate locations, using the original kernel image path.␊ |
712 | ␉␉␉␉␉␉strcpy(bootFileSpec, bootFile);␊ |
713 | ␉␉␉␉␉}␊ |
714 | ␉␉␉␉}␊ |
715 | }␊ |
716 | #else␊ |
717 | ␉␉␉strcpy(bootFileSpec, bootFile);␊ |
718 | #endif␊ |
719 | ␉␉␉␊ |
720 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
721 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
722 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
723 | {␊ |
724 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
725 | ␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
726 | }␊ |
727 | ␉␉␉␊ |
728 | } while (0);␊ |
729 | #if TEXT_SPINNER␊ |
730 | clearActivityIndicator();␊ |
731 | #endif␊ |
732 | ␉␉␊ |
733 | #if DEBUG␊ |
734 | printf("Pausing...");␊ |
735 | sleep(8);␊ |
736 | #endif␊ |
737 | ␉␉␊ |
738 | if (ret <= 0) {␊ |
739 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
740 | ␉␉␉␊ |
741 | ␉␉␉sleep(1);␊ |
742 | #ifdef NBP_SUPPORT␊ |
743 | if (gBootFileType == kNetworkDeviceType) {␊ |
744 | // Return control back to PXE. Don't unload PXE base code.␊ |
745 | gUnloadPXEOnExit = false;␊ |
746 | break;␊ |
747 | }␊ |
748 | #endif␊ |
749 | } else {␊ |
750 | /* Won't return if successful. */␊ |
751 | ret = ExecKernel(binary);␊ |
752 | }␊ |
753 | }␊ |
754 | ␊ |
755 | // chainboot␊ |
756 | if (status==1) {␊ |
757 | ␉␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
758 | #if UNUSED␊ |
759 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
760 | #else␊ |
761 | ␉␉␉setVideoMode(VGA_TEXT_MODE);␉// switch back to text mode␊ |
762 | #endif␊ |
763 | ␉␉}␊ |
764 | }␊ |
765 | #ifdef NBP_SUPPORT␊ |
766 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
767 | ␉␉nbpUnloadBaseCode();␊ |
768 | }␊ |
769 | #endif␊ |
770 | }␊ |
771 | ␊ |
772 | // Maximum config table value size␊ |
773 | #define VALUE_SIZE 2048␊ |
774 | static void getRootDevice()␊ |
775 | {␊ |
776 | ␉const char *val = 0;␊ |
777 | int cnt = 0;␊ |
778 | ␉␊ |
779 | ␉if (getValueForKey(kBootUUIDKey, &val, &cnt, &bootInfo->bootConfig)){␊ |
780 | ␉␉uuidSet = true;␉␉␊ |
781 | ␉␉␊ |
782 | } else {␊ |
783 | ␉␉if (getValueForBootKey(bootArgs->CommandLine, kRootDeviceKey, &val, &cnt)) {␊ |
784 | ␉␉␉if (*val == '*' && *(val + 1) != '/' && *(val + 1) != 'u') { ␊ |
785 | ␉␉␉␉val += 1; //skip the *␊ |
786 | ␉␉␉␉uuidSet = true;␉␉␉␉␊ |
787 | ␉␉␉␉␊ |
788 | ␉␉␉} else if (*val == '*' && *(val + 1) == 'u') {␊ |
789 | ␉␉␉␉␊ |
790 | ␉␉␉␉if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig)) ␊ |
791 | ␉␉␉␉␉uuidSet = true;␉␉␉␉␊ |
792 | ␉␉␉␉␊ |
793 | ␉␉␉} ␉␉␉␊ |
794 | ␉␉} else {␊ |
795 | #ifdef BOOT_HELPER_SUPPORT␊ |
796 | ␉␉␉//␊ |
797 | ␉␉␉// Try an alternate method for getting the root UUID on boot helper partitions.␊ |
798 | ␉␉␉//␊ |
799 | ␉␉␉if (gBootVolume->flags & kBVFlagBooter)␊ |
800 | ␉␉␉{␊ |
801 | ␉␉␉␉if((loadHelperConfig(&bootInfo->helperConfig) == 0)␊ |
802 | ␉␉␉␉ && getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig) )␊ |
803 | ␉␉␉␉{␊ |
804 | ␉␉␉␉␉getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig);␊ |
805 | ␉␉␉␉␉uuidSet = true;␊ |
806 | ␉␉␉␉␉goto out;␊ |
807 | ␉␉␉␉}␊ |
808 | ␉␉␉}␊ |
809 | #endif␊ |
810 | ␉␉␉␊ |
811 | ␉␉␉if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig)) {␊ |
812 | ␉␉␉␉extern int ArgCntRemaining;␊ |
813 | ␉␉␉␉uuidSet = false;␊ |
814 | ␉␉␉␉char * valueBuffer;␊ |
815 | ␉␉␉␉valueBuffer = malloc(VALUE_SIZE);␊ |
816 | ␉␉␉␉char * argP = bootArgs->CommandLine;␊ |
817 | ␉␉␉␉valueBuffer[0] = '*';␊ |
818 | ␉␉␉␉cnt++;␊ |
819 | ␉␉␉␉strlcpy(valueBuffer + 1, val, cnt);␉␉␉␉␊ |
820 | ␉␉␉␉if (!copyArgument( kRootDeviceKey, valueBuffer, cnt, &argP, &ArgCntRemaining))␊ |
821 | ␉␉␉␉{␊ |
822 | ␉␉␉␉␉free(valueBuffer);␊ |
823 | ␉␉␉␉␉printf("Error: boot arguments too long, unable to set root device !!");␊ |
824 | ␉␉␉␉␉getc();␊ |
825 | ␉␉␉␉␉return;␊ |
826 | ␉␉␉␉}␊ |
827 | ␉␉␉␉free(valueBuffer);␊ |
828 | ␉␉␉␉goto out;␊ |
829 | ␉␉␉}␊ |
830 | ␉␉␉␊ |
831 | ␉␉␉if (gBootVolume->fs_getuuid && gBootVolume->fs_getuuid (gBootVolume, bootInfo->uuidStr) == 0) {␊ |
832 | ␉␉␉␉verbose("Setting boot-uuid to: %s\n", bootInfo->uuidStr);␊ |
833 | ␉␉␉␉uuidSet = true;␊ |
834 | ␉␉␉␉gRootDevice = bootInfo->uuidStr;␊ |
835 | ␉␉␉␉return;␊ |
836 | ␉␉␉}␊ |
837 | ␉␉␊ |
838 | ␉␉}␉␊ |
839 | ␉}␊ |
840 | ␉␉␊ |
841 | out:␉␊ |
842 | ␉verbose("Setting %s to: %s\n", uuidSet ? kBootUUIDKey : "root device", (char* )val); ␊ |
843 | ␉gRootDevice = (char* )val;␊ |
844 | }␊ |
845 | ␊ |
846 | static bool getOSVersion(char *str)␊ |
847 | {␊ |
848 | ␉bool valid = false;␊ |
849 | ␉config_file_t systemVersion;␊ |
850 | ␉const char *val;␊ |
851 | ␉int len;␊ |
852 | ␉␊ |
853 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
854 | ␉{␊ |
855 | ␉␉valid = true;␊ |
856 | ␉}␊ |
857 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
858 | ␉{␊ |
859 | ␉␉valid = true;␊ |
860 | ␉}␊ |
861 | ␉␊ |
862 | ␉if (valid)␊ |
863 | ␉{␊ |
864 | ␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion))␊ |
865 | ␉␉{␊ |
866 | ␉␉␉// getValueForKey uses const char for val␊ |
867 | ␉␉␉// so copy it and trim␊ |
868 | ␉␉␉*str = '\0';␊ |
869 | ␉␉␉strncat(str, val, MIN(len, 4));␊ |
870 | ␉␉}␊ |
871 | ␉␉else␊ |
872 | ␉␉␉valid = false;␊ |
873 | ␉}␊ |
874 | ␉␊ |
875 | ␉return valid;␊ |
876 | }␊ |
877 | ␊ |
878 | unsigned long␊ |
879 | local_adler32( unsigned char * buffer, long length )␊ |
880 | {␊ |
881 | long cnt;␊ |
882 | unsigned long result, lowHalf, highHalf;␊ |
883 | ␊ |
884 | lowHalf = 1;␊ |
885 | highHalf = 0;␊ |
886 | ␉␊ |
887 | ␉for ( cnt = 0; cnt < length; cnt++ )␊ |
888 | {␊ |
889 | if ((cnt % 5000) == 0)␊ |
890 | {␊ |
891 | lowHalf %= 65521L;␊ |
892 | highHalf %= 65521L;␊ |
893 | }␊ |
894 | ␉␉␊ |
895 | lowHalf += buffer[cnt];␊ |
896 | highHalf += lowHalf;␊ |
897 | }␊ |
898 | ␉␊ |
899 | ␉lowHalf %= 65521L;␊ |
900 | ␉highHalf %= 65521L;␊ |
901 | ␉␊ |
902 | ␉result = (highHalf << 16) | lowHalf;␊ |
903 | ␉␊ |
904 | ␉return result;␊ |
905 | }␊ |
906 | |