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