Chameleon

Chameleon Svn Source Tree

Root/branches/slice/i386/boot2/boot.c

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 "acpi_patcher.h"
57#include "sl.h"
58#include "libsa.h"
59#include "ramdisk.h"
60#include "platform.h"
61#include "graphics.h"
62#include "pci.h"
63
64#include "modules.h"
65
66#define DEBUG 0
67
68long gBootMode; /* defaults to 0 == kBootModeNormal */
69bool gOverrideKernel;
70static char gBootKernelCacheFile[512];
71static char gCacheNameAdler[64 + 256];
72char *gPlatformName = gCacheNameAdler;
73char gRootDevice[512];
74char gMKextName[512];
75char gMacOSVersion[8];
76void *gRootPCIDev;
77void *gPlatform;
78void *gBootOrder;
79void *gSMBIOS;
80int gDualLink;
81#ifndef OPTION_ROM
82bool gEnableCDROMRescan;
83bool gScanSingleDrive;
84#endif
85config_file_t systemVersion;// system.plist of booting partition
86
87
88int bvCount = 0;
89//intmenucount = 0;
90int gDeviceCount = 0;
91
92BVRef bvr;
93BVRef menuBVR;
94BVRef bvChain;
95
96//static void selectBiosDevice(void);
97//static unsigned long Adler32(unsigned char *buffer, long length);
98static bool getOSVersion(char *str);
99
100#ifndef OPTION_ROM
101static bool gUnloadPXEOnExit = false;
102#endif
103
104/*
105 * How long to wait (in seconds) to load the
106 * kernel after displaying the "boot:" prompt.
107 */
108#define kBootErrorTimeout 5
109
110/*
111 * Default path to kernel cache file
112 */
113 //Slice - first one for Leopard
114#define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/"
115#define kDefaultCachePathSnow "/System/Library/Caches/com.apple.kext.caches/Startup/"
116
117//==========================================================================
118// Zero the BSS.
119
120static void zeroBSS(void)
121{
122extern char _DATA__bss__begin, _DATA__bss__end;
123extern char _DATA__common__begin, _DATA__common__end;
124
125bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
126bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
127}
128
129//==========================================================================
130// Malloc error function
131
132static void malloc_error(char *addr, size_t size, const char *file, int line)
133{
134stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
135}
136
137//==========================================================================
138//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
139//
140void initialize_runtime(void)
141{
142zeroBSS();
143malloc_init(0, 0, 0, malloc_error);
144}
145
146//==========================================================================
147// execKernel - Load the kernel image (mach-o) and jump to its entry point.
148
149int ExecKernel(void *binary)
150{
151 entry_t kernelEntry;
152 int ret;
153
154 bootArgs->kaddr = bootArgs->ksize = 0;
155execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
156
157 ret = DecodeKernel(binary,
158 &kernelEntry,
159 (char **) &bootArgs->kaddr,
160 (int *)&bootArgs->ksize );
161
162 if ( ret != 0 )
163 return ret;
164 // Reserve space for boot args
165 reserveKernBootStruct();
166
167 // Load boot drivers from the specifed root path.
168execute_hook("DecodedKernel", (void*)binary, NULL, NULL, NULL);
169
170
171setupFakeEfi();
172
173 if (!gHaveKernelCache) {
174LoadDrivers("/");
175 }
176
177 clearActivityIndicator();
178
179 if (gErrors) {
180 msglog("Errors encountered while starting up the computer.\n");
181 // printf("Pausing %d seconds...\n", kBootErrorTimeout);
182 // sleep(kBootErrorTimeout);
183 }
184
185md0Ramdisk();
186
187 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "i386" : "x86_64");
188#ifndef OPTION_ROM
189 // Cleanup the PXE base code.
190
191 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
192if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
193 {
194 verbose("nbpUnloadBaseCode error %d\n", (int) ret);
195 sleep(2);
196 }
197 }
198#endif
199 bool dummyVal;
200if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal)
201{
202printf("Press any key to continue...");
203getc();
204}
205
206execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);// Notify modules that the kernel is about to be started
207
208if (bootArgs->Video.v_display == VGA_TEXT_MODE)
209{
210setVideoMode( GRAPHICS_MODE, 0 );
211// Draw gray screen. NOTE: no boot image, that's in the gui module
212#ifndef OPTION_ROM
213if(!gVerboseMode) drawColorRectangle(0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 0x01); //Slice -???
214#endif
215}
216
217
218setupBooterLog();
219
220 finalizeBootStruct();
221
222// Jump to kernel's entry point. There's no going back now.
223 startprog( kernelEntry, bootArgs );
224
225 // Not reached
226
227 return 0;
228}
229
230//==========================================================================
231// This is the entrypoint from real-mode which functions exactly as it did
232// before. Multiboot does its own runtime initialization, does some of its
233// own things, and then calls common_boot.
234void boot(int biosdev)
235{
236initialize_runtime();
237// Enable A20 gate before accessing memory above 1Mb.
238enableA20();
239common_boot(biosdev);
240}
241
242//==========================================================================
243// The 'main' function for the booter. Called by boot0 when booting
244// from a block device, or by the network booter.
245//
246// arguments:
247// biosdev - Value passed from boot1/NBP to specify the device
248// that the booter was loaded from.
249//
250// If biosdev is kBIOSDevNetwork, then this function will return if
251// booting was unsuccessful. This allows the PXE firmware to try the
252// next boot device on its list.
253void common_boot(int biosdev)
254{
255 int status;
256inttrycache;
257 char *bootFile;
258 unsigned long adler32;
259 bool quiet;
260 bool firstRun = true;
261 bool instantMenu;
262#ifndef OPTION_ROM
263 bool rescanPrompt;
264#endif
265 unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
266 unsigned int denyBVFlags = kBVFlagEFISystem;
267
268#ifndef OPTION_ROM
269 // Set reminder to unload the PXE base code. Neglect to unload
270 // the base code will result in a hang or kernel panic.
271 gUnloadPXEOnExit = true;
272#endif
273
274 // Record the device that the booter was loaded from.
275 gBIOSDev = biosdev & kBIOSDevMask; // 0xff
276
277 // Initialize boot info structure.
278 initKernBootStruct();
279
280initBooterLog();
281
282 // Setup VGA text mode.
283 // Not sure if it is safe to call setVideoMode() before the
284 // config table has been loaded. Call video_mode() instead.
285 video_mode( 2 ); // 80x25 mono text mode.
286
287 // Scan and record the system's hardware information.
288 scan_platform();
289
290 // First get info for boot volume.
291 scanBootVolumes(gBIOSDev, 0);
292 bvChain = getBVChainForBIOSDev(gBIOSDev);
293
294#if DEBUG
295 printf("get bvChain dev=%02x type=%02x part_no=%d\n", bvChain->biosdev, bvChain->type, bvChain->part_no); //dev=0x80 - flash-stick
296//getc();
297#endif
298
299 setBootGlobals(bvChain);
300
301 // Load boot.plist config file
302 status = loadSystemConfig(&bootInfo->bootConfig);
303
304 if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {
305 gBootMode |= kBootModeQuiet;
306 }
307
308 // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
309 if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {
310 firstRun = false;
311 }
312
313#ifndef OPTION_ROM
314// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
315 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {
316 gScanSingleDrive = true;
317 }
318
319// Create a list of partitions on device(s).
320 if (gScanSingleDrive)
321{
322scanBootVolumes(gBIOSDev, &bvCount);
323 }
324else
325#endif
326{
327scanDisks(gBIOSDev, &bvCount);
328 }
329
330// Create a separated bvr chain using the specified filters.
331 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
332gBootVolume = selectBootVolume(bvChain);
333
334#if DEBUG
335 printf("separated bvChain dev=%02x type=%02x part_no=%d\n", bvChain->biosdev, bvChain->type, bvChain->part_no); //dev=0x81 - HDD
336pause();
337#endif
338
339// Intialize module system
340if(init_module_system())
341{
342#if DEBUG
343printf("begin load_all_modules\n");
344//pause();
345#endif
346
347load_all_modules();
348}
349
350execute_hook("ModulesLoaded", NULL, NULL, NULL, NULL);
351#if DEBUG
352 printf("ModulesLoaded\n");
353//pause();
354#endif
355
356
357#ifndef OPTION_ROM
358 // Loading preboot ramdisk if exists.
359 loadPrebootRAMDisk();
360
361 // Disable rescan option by default
362 gEnableCDROMRescan = false;
363
364 // Enable it with Rescan=y in system config
365 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {
366 gEnableCDROMRescan = true;
367 }
368
369 // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
370 rescanPrompt = false;
371 if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {
372 gEnableCDROMRescan = promptForRescanOption();
373 }
374#endif
375
376#if DEBUG
377 printf(" Default: %x, ->gBootVolume: %x, ->part_no: %d ->flags: %x\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
378 printf(" bt(0,0): %x, ->gBIOSBootVolume: %x, ->part_no: %d ->flags: %x\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
379 pause();
380/* Results
381 Rescan found 1 HDD and bvCount=4
382 bvr: 836bc10, dev: 80, part: 4, flags: 4a, vis: 1
383 bvr: 836bce0, dev: 80, part: 3, flags: 4b, vis: 1
384 bvr: 836be90, dev: 80, part: 2, flags: 4, vis: 1
385 bvr: 836bf60, dev: 80, part: 1, flags: a, vis: 0
386 count: 3
387
388 foundPrimary and flags bvr1=a bvr2=4b
389 Default: 836bce0, ->biosdev: 80, ->part_no: 3 ->flags: 4b //booted partition
390 bt(0,0): 8262030, ->biosdev: 80, ->part_no: 3 ->flags: 4b //unknown pointer
391
392 */
393#endif
394
395
396
397 setBootGlobals(bvChain);
398
399 // Parse args, load and start kernel.
400 while (1) {
401 const char *val;
402 int len;
403 // int trycache;
404 long flags, cachetime, kerneltime, exttime, sleeptime, time;
405 int ret = -1;
406 void *binary = (void *)kLoadAddr;
407 bool tryresume;
408 bool tryresumedefault;
409 bool forceresume;
410
411 // additional variable for testing alternate kernel image locations on boot helper partitions.
412 char bootFileSpec[512];
413
414 // Initialize globals.
415
416 sysConfigValid = false;
417 gErrors = false;
418 trycache=1;
419
420 status = getBootOptions(firstRun);
421 firstRun = false;
422 if (status == -1) continue;
423
424 status = processBootOptions();
425 // Status==1 means to chainboot
426 if ( status == 1 ) break;
427 // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL
428 if ( status == -1 )
429 {
430// gBootVolume == NULL usually means the user hit escape.
431if(gBootVolume == NULL)
432{
433freeFilteredBVChain(bvChain);
434#ifndef OPTION_ROM
435if (gEnableCDROMRescan)
436rescanBIOSDevice(gBIOSDev);
437#endif
438
439bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
440setBootGlobals(bvChain);
441}
442#if DEBUG
443verbose("After rescan\n");
444verbose(" System: %x, ->biosdev: %x, ->part_no: %d ->flags: %x\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
445verbose(" Booter: %x, ->biosdev: %x, ->part_no: %d ->flags: %x\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
446// getc();
447#endif
448
449continue;
450 }
451
452
453// Find out which version mac os we're booting.
454getOSVersion(gMacOSVersion);
455
456if (platformCPUFeature(CPU_FEATURE_EM64T)) {
457archCpuType = CPU_TYPE_X86_64;
458} else {
459archCpuType = CPU_TYPE_I386;
460}
461if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {
462if (strncmp(val, "i386", 4) == 0) {
463archCpuType = CPU_TYPE_I386;
464}
465}
466
467// Notify modules that we are attempting to boot
468execute_hook("PreBoot", NULL, NULL, NULL, NULL);
469
470if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {
471tryresume = true;
472tryresumedefault = true;
473} else {
474tryresumedefault = false;
475}
476
477if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {
478forceresume = false;
479}
480
481if (forceresume) {
482tryresume = true;
483tryresumedefault = false;
484}
485
486while (tryresume) {
487const char *tmp;
488BVRef bvr;
489if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))
490val="/private/var/vm/sleepimage";
491
492// Do this first to be sure that root volume is mounted
493ret = GetFileInfo(0, val, &flags, &sleeptime);
494
495if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
496break;
497
498// Can't check if it was hibernation Wake=y is required
499if (bvr->modTime == 0 && tryresumedefault)
500break;
501
502if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
503break;
504
505if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
506printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);
507break;
508}
509
510HibernateBoot((char *)val);
511break;
512}
513
514 // Reset cache name.
515 bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
516
517 sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
518
519 adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
520
521 if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig) == true) {
522 strlcpy(gBootKernelCacheFile, val, len+1);
523 } else {
524if(gMacOSVersion[3] == '6') {
525sprintf(gBootKernelCacheFile, "kernelcache_%s", /*kDefaultCachePathSnow,*/ (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64"); //, adler32);
526msglog("search for kernelcache %s\n", gBootKernelCacheFile);
527int lnam = sizeof(gBootKernelCacheFile) + 9; //with adler32
528//Slice - TODO
529/*
530 - but the name is longer .adler32 and more...
531 kernelcache_i386.E102928C.qSs0
532 so will opendir and scan for some files
533*/
534char* name;
535long flagsC;
536long timeC;
537struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
538while(readdir(cacheDir, (const char**)&name, &flagsC, &timeC) >= 0)
539{
540if(strstr(name, gBootKernelCacheFile)) //?
541{
542if(name[lnam] == '.') continue;
543//char* tmp = malloc(strlen(name) + 1);
544sprintf(gBootKernelCacheFile, "%s%s", kDefaultCachePathSnow, name);
545verbose("find kernelcache=%s\n", gBootKernelCacheFile);
546break;
547}
548}
549//close(cacheDir);
550
551}
552else //if(gMacOSVersion[3] == '5')
553sprintf(gBootKernelCacheFile, "%skernelcache", kDefaultCachePath);
554 }
555
556 // Check for cache file.
557 trycache = (((gBootMode & kBootModeSafe) == 0) &&
558 !gOverrideKernel &&
559 (gBootFileType == kBlockDeviceType) &&
560 (gMKextName[0] == '\0') &&
561 (gBootKernelCacheFile[0] != '\0')); // &&
562//(gMacOSVersion[3] != '4'));
563verbose("Try cache %s %s\n", gBootKernelCacheFile, trycache?"YES":"NO");
564
565verbose("Loading Darwin %s\n", gMacOSVersion);
566
567 if (trycache) do {
568
569 // if we haven't found the kernel yet, don't use the cache
570 ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
571 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
572 trycache = 0;
573 break;
574 }
575 ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
576 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
577 || (cachetime < kerneltime)) {
578 trycache = 0;
579 verbose("Try cache failed\n");
580 break;
581 }
582 ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
583 if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
584 && (cachetime < exttime)) {
585 trycache = 0;
586 break;
587 }
588 if (kerneltime > exttime) {
589 exttime = kerneltime;
590 }
591 if (cachetime != (exttime + 1)) {
592 trycache = 0;
593 break;
594 }
595 } while (0);
596
597 do {
598 if (trycache) {
599 bootFile = gBootKernelCacheFile;
600 verbose("Loading kernel cache %s\n", bootFile);
601 ret = LoadFile(bootFile);
602 binary = (void *)kLoadAddr;
603 if (ret >= 0) {
604 break;
605 }
606 }
607 bootFile = bootInfo->bootFile;
608
609 // Try to load kernel image from alternate locations on boot helper partitions.
610 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
611 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
612 if (ret == -1)
613 {
614sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
615ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
616if (ret == -1)
617{
618sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
619ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
620if (ret == -1)
621{
622// Not found any alternate locations, using the original kernel image path.
623strcpy(bootFileSpec, bootFile);
624}
625}
626 }
627
628 verbose("Loading kernel %s\n", bootFileSpec);
629 ret = LoadThinFatFile(bootFileSpec, &binary);
630 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
631 {
632archCpuType = CPU_TYPE_I386;
633ret = LoadThinFatFile(bootFileSpec, &binary);
634 }
635
636 } while (0);
637
638 clearActivityIndicator();
639#if DEBUG
640 printf("Pausing...");
641 sleep(8);
642#endif
643
644 if (ret <= 0) {
645printf("Can't find %s\n", bootFile);
646
647sleep(1);
648#ifndef OPTION_ROM
649 if (gBootFileType == kNetworkDeviceType) {
650 // Return control back to PXE. Don't unload PXE base code.
651 gUnloadPXEOnExit = false;
652 break;
653 }
654#endif
655 } else {
656 /* Won't return if successful. */
657// Notify modules that ExecKernel is about to be called
658 ret = ExecKernel(binary);
659 }
660
661 } /* while(1) */
662
663 // chainboot
664 if (status==1) {
665if (getVideoMode() == GRAPHICS_MODE) {// if we are already in graphics-mode,
666setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
667}
668 }
669#ifndef OPTION_ROM
670 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
671nbpUnloadBaseCode();
672 }
673#endif
674}
675
676/*!
677 Selects a new BIOS device, taking care to update the global state appropriately.
678 */
679/*
680 static void selectBiosDevice(void)
681 {
682 struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
683 CacheReset();
684 diskFreeMap(oldMap);
685 oldMap = NULL;
686
687 int dev = selectAlternateBootDevice(gBIOSDev);
688
689 BVRef bvchain = scanBootVolumes(dev, 0);
690 BVRef bootVol = selectBootVolume(bvchain);
691 gBootVolume = bootVol;
692 setRootVolume(bootVol);
693 gBIOSDev = dev;
694 }
695 */
696
697static bool getOSVersion(char *str)
698{
699bool valid = false;
700//config_file_t systemVersion;
701const char *val;
702int len;
703msglog("Address loadConfigFile=%x bootInfo=%x\n", &loadConfigFile, &bootInfo);
704if (!loadConfigFile("/System/Library/CoreServices/SystemVersion.plist", &systemVersion))
705{
706valid = true;
707}
708else if (!loadConfigFile("/System/Library/CoreServices/ServerVersion.plist", &systemVersion))
709{
710valid = true;
711}
712
713if (valid)
714{
715if (getValueForKey(kProductVersion, &val, &len, &systemVersion))
716{
717// getValueForKey uses const char for val
718// so copy it and trim
719*str = '\0';
720strncat(str, val, MIN(len, 4));
721}
722else
723valid = false;
724}
725
726return valid;
727}
728
729#define BASE 65521L /* largest prime smaller than 65536 */
730#define NMAX 5000
731// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
732
733#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
734#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
735#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
736#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
737#define DO16(buf) DO8(buf,0); DO8(buf,8);
738/*
739unsigned long Adler32(unsigned char *buf, long len)
740{
741 unsigned long s1 = 1; // adler & 0xffff;
742 unsigned long s2 = 0; // (adler >> 16) & 0xffff;
743 unsigned long result;
744 int k;
745
746 while (len > 0) {
747 k = len < NMAX ? len : NMAX;
748 len -= k;
749 while (k >= 16) {
750 DO16(buf);
751 buf += 16;
752 k -= 16;
753 }
754 if (k != 0) do {
755 s1 += *buf++;
756 s2 += s1;
757 } while (--k);
758 s1 %= BASE;
759 s2 %= BASE;
760 }
761 result = (s2 << 16) | s1;
762 return OSSwapHostToBigInt32(result);
763}
764*/

Archive Download this file

Revision: 721