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