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