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