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