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