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