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 | /*␊ |
64 | * How long to wait (in seconds) to load the␊ |
65 | * kernel after displaying the "boot:" prompt.␊ |
66 | */␊ |
67 | #define kBootErrorTimeout 5␊ |
68 | ␊ |
69 | bool␉␉gOverrideKernel, gEnableCDROMRescan, gScanSingleDrive, useGUI;␊ |
70 | static bool␉gUnloadPXEOnExit = false;␊ |
71 | ␊ |
72 | static char␉gCacheNameAdler[64 + 256];␊ |
73 | char␉␉*gPlatformName = gCacheNameAdler;␊ |
74 | ␊ |
75 | char␉␉gRootDevice[512];␊ |
76 | char␉␉gMKextName[512];␊ |
77 | char␉␉gMacOSVersion[8];␊ |
78 | static char␉gBootKernelCacheFile[512];␊ |
79 | int␉␉␉bvCount = 0, gDeviceCount = 0;␊ |
80 | //int␉␉menucount = 0;␊ |
81 | long␉␉gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
82 | BVRef␉␉bvr, menuBVR, bvChain;␊ |
83 | ␊ |
84 | static bool␉␉␉␉checkOSVersion(const char * version);␊ |
85 | static bool␉␉␉␉getOSVersion();␊ |
86 | static unsigned long␉Adler32(unsigned char *buffer, long length);␊ |
87 | //static void␉␉␉selectBiosDevice(void);␊ |
88 | ␊ |
89 | /** options.c **/␊ |
90 | extern char* msgbuf;␊ |
91 | void showTextBuffer(char *buf, int size);␊ |
92 | ␊ |
93 | ␊ |
94 | //==========================================================================␊ |
95 | // Zero the BSS.␊ |
96 | ␊ |
97 | static void zeroBSS(void)␊ |
98 | {␊ |
99 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
100 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
101 | ␉␊ |
102 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
103 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
104 | }␊ |
105 | ␊ |
106 | //==========================================================================␊ |
107 | // Malloc error function␊ |
108 | ␊ |
109 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
110 | {␊ |
111 | ␉stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",␊ |
112 | ␉␉␉␉␉␉␉␉␉ (unsigned)addr, (unsigned)size, file, line);␊ |
113 | }␊ |
114 | ␊ |
115 | //==========================================================================␊ |
116 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
117 | //␊ |
118 | void initialize_runtime(void)␊ |
119 | {␊ |
120 | ␉zeroBSS();␊ |
121 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
122 | }␊ |
123 | ␊ |
124 | //==========================================================================␊ |
125 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
126 | ␊ |
127 | static int ExecKernel(void *binary)␊ |
128 | {␊ |
129 | ␉int␉␉␉ret;␊ |
130 | ␉entry_t␉␉kernelEntry;␊ |
131 | ␉␊ |
132 | ␉bootArgs->kaddr = bootArgs->ksize = 0;␊ |
133 | ␉execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);␊ |
134 | ␉␊ |
135 | ␉ret = DecodeKernel(binary,␊ |
136 | ␉␉␉␉␉ &kernelEntry,␊ |
137 | ␉␉␉␉␉ (char **) &bootArgs->kaddr,␊ |
138 | ␉␉␉␉␉ (int *)&bootArgs->ksize );␊ |
139 | ␉␊ |
140 | ␉if ( ret != 0 )␊ |
141 | ␉␉return ret;␊ |
142 | ␉␊ |
143 | ␉// Reserve space for boot args␊ |
144 | ␉reserveKernBootStruct();␊ |
145 | ␉␊ |
146 | ␉// Notify modules that the kernel has been decoded␊ |
147 | ␉execute_hook("DecodedKernel", (void*)binary, NULL, NULL, NULL);␊ |
148 | ␉␊ |
149 | ␉setupFakeEfi();␊ |
150 | ␉␊ |
151 | ␉// Load boot drivers from the specifed root path.␊ |
152 | ␉//if (!gHaveKernelCache)␊ |
153 | ␉LoadDrivers("/");␊ |
154 | ␉␊ |
155 | ␉execute_hook("DriversLoaded", (void*)binary, NULL, NULL, NULL);␊ |
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 | ␉md0Ramdisk();␊ |
166 | ␉␊ |
167 | ␉verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
168 | ␉␊ |
169 | ␉// Cleanup the PXE base code.␊ |
170 | ␉␊ |
171 | ␉if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {␊ |
172 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
173 | ␉␉{␊ |
174 | ␉␉␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
175 | ␉␉␉sleep(2);␊ |
176 | ␉␉}␊ |
177 | ␉}␊ |
178 | ␉␊ |
179 | ␉bool dummyVal;␊ |
180 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {␊ |
181 | ␉␉showTextBuffer(msgbuf, strlen(msgbuf));␊ |
182 | ␉}␊ |
183 | ␉␊ |
184 | ␉usb_loop();␊ |
185 | ␉␊ |
186 | ␉// Notify modules that the kernel is about to be started␊ |
187 | ␉if (checkOSVersion("10.7"))␊ |
188 | ␉{␊ |
189 | ␉␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);␊ |
190 | ␉}␊ |
191 | ␉else␊ |
192 | ␉{␊ |
193 | ␉␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);␊ |
194 | ␉}␊ |
195 | ␉␊ |
196 | ␉// If we were in text mode, switch to graphics mode.␊ |
197 | ␉// This will draw the boot graphics unless we are in␊ |
198 | ␉// verbose mode.␊ |
199 | ␉if (gVerboseMode)␊ |
200 | ␉␉setVideoMode( GRAPHICS_MODE, 0 );␊ |
201 | ␉else␊ |
202 | ␉␉drawBootGraphics();␊ |
203 | ␉␊ |
204 | ␉setupBooterLog();␊ |
205 | ␉␊ |
206 | ␉finalizeBootStruct();␊ |
207 | ␉␊ |
208 | ␉// Jump to kernel's entry point. There's no going back now.␊ |
209 | ␉if (checkOSVersion("10.7")) {␊ |
210 | ␉␉␊ |
211 | ␉␉// Masking out so that Lion doesn't doublefault␊ |
212 | ␉␉outb(0x21, 0xff);␉/* Maskout all interrupts Pic1 */␊ |
213 | ␉␉outb(0xa1, 0xff);␉/* Maskout all interrupts Pic2 */␊ |
214 | ␉␉␊ |
215 | ␉␉startprog( kernelEntry, bootArgs );␊ |
216 | ␉}␊ |
217 | ␉else {␊ |
218 | ␉␉startprog( kernelEntry, bootArgsPreLion );␊ |
219 | ␉}␊ |
220 | ␉␊ |
221 | ␉// Not reached␊ |
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 | ␉bool␉ ␉␉quiet;␊ |
251 | ␉bool␉ ␉␉firstRun = true;␊ |
252 | ␉bool␉ ␉␉instantMenu;␊ |
253 | ␉bool␉ ␉␉rescanPrompt;␊ |
254 | ␉char␉ ␉␉*bootFile;␊ |
255 | ␉int␉␉␉␉status;␊ |
256 | ␉unsigned int␉allowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;␊ |
257 | ␉unsigned int␉denyBVFlags = kBVFlagEFISystem;␊ |
258 | ␉unsigned long␉adler32;␊ |
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 = loadChameleonConfig(&bootInfo->chameleonConfig);␊ |
293 | ␉␊ |
294 | ␉if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && 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(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && 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->chameleonConfig)␊ |
311 | ␉␉&& gEnableCDROMRescan) {␊ |
312 | ␉␉gEnableCDROMRescan = true;␊ |
313 | ␉}␊ |
314 | ␉␊ |
315 | ␉// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
316 | ␉rescanPrompt = false;␊ |
317 | ␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig)␊ |
318 | ␉␉&& rescanPrompt && biosDevIsCDROM(gBIOSDev))␊ |
319 | ␉{␊ |
320 | ␉␉gEnableCDROMRescan = promptForRescanOption();␊ |
321 | ␉}␊ |
322 | ␉␊ |
323 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
324 | ␉if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig)␊ |
325 | ␉␉&& gScanSingleDrive) {␊ |
326 | ␉␉gScanSingleDrive = true;␊ |
327 | ␉}␊ |
328 | ␉␊ |
329 | ␉// Create a list of partitions on device(s).␊ |
330 | ␉if (gScanSingleDrive) {␊ |
331 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
332 | ␉} else {␊ |
333 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
334 | ␉}␊ |
335 | ␉␊ |
336 | ␉// Create a separated bvr chain using the specified filters.␊ |
337 | ␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
338 | ␉␊ |
339 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
340 | ␉␊ |
341 | ␉// Intialize module system ␊ |
342 | ␉init_module_system();␊ |
343 | ␉␉␊ |
344 | #if DEBUG␊ |
345 | ␉printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",␊ |
346 | ␉␉␉ gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
347 | ␉printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",␊ |
348 | ␉␉␉ 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 | ␉{␊ |
366 | ␉␉bool␉␉tryresume, tryresumedefault, forceresume;␊ |
367 | ␉␉bool␉␉useKernelCache = false; // by default don't use prelink kernel cache␊ |
368 | ␉␉const char␉*val;␊ |
369 | ␉␉int␉␉␉len, trycache, ret = -1;␊ |
370 | ␉␉long␉␉flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
371 | ␉␉void␉␉*binary = (void *)kLoadAddr;␊ |
372 | ␉␉␊ |
373 | ␉␉// additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
374 | ␉␉char␉␉bootFileSpec[512];␊ |
375 | ␉␉␊ |
376 | ␉␉// Initialize globals.␊ |
377 | ␉␉sysConfigValid = false;␊ |
378 | ␉␉gErrors␉␉ = false;␊ |
379 | ␉␉␊ |
380 | ␉␉status = getBootOptions(firstRun);␊ |
381 | ␉␉firstRun = false;␊ |
382 | ␉␉if (status == -1) continue;␊ |
383 | ␉␉ ␊ |
384 | ␉␉status = processBootOptions();␊ |
385 | ␉␉// Status == 1 means to chainboot␊ |
386 | ␉␉if ( status ==␉1 ) break;␊ |
387 | ␉␉// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
388 | ␉␉if ( status == -1 )␊ |
389 | ␉␉{␊ |
390 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
391 | ␉␉␉if (gBootVolume == NULL)␊ |
392 | ␉␉␉{␊ |
393 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
394 | ␉␉␉␉␊ |
395 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
396 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
397 | ␉␉␉␉␊ |
398 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
399 | ␉␉␉␉setBootGlobals(bvChain);␊ |
400 | ␉␉␉␉setupDeviceList(&bootInfo->themeConfig);␊ |
401 | ␉␉␉}␊ |
402 | ␉␉␉continue;␊ |
403 | ␉␉}␊ |
404 | ␉␉␊ |
405 | ␉␉// Other status (e.g. 0) means that we should proceed with boot.␊ |
406 | ␉␉␊ |
407 | ␉␉// Turn off any GUI elements␊ |
408 | ␉␉if ( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
409 | ␉␉{␊ |
410 | ␉␉␉gui.devicelist.draw = false;␊ |
411 | ␉␉␉gui.bootprompt.draw = false;␊ |
412 | ␉␉␉gui.menu.draw = false;␊ |
413 | ␉␉␉gui.infobox.draw = false;␊ |
414 | ␉␉␉gui.logo.draw = false;␊ |
415 | ␉␉␉drawBackground();␊ |
416 | ␉␉␉updateVRAM();␊ |
417 | ␉␉}␊ |
418 | ␉␉␊ |
419 | ␉␉// Find out which version mac os we're booting.␊ |
420 | ␉␉getOSVersion();␊ |
421 | ␉␉␊ |
422 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T)) {␊ |
423 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
424 | ␉␉} else {␊ |
425 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
426 | ␉␉}␊ |
427 | ␉␉␊ |
428 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig)) {␊ |
429 | ␉␉␉if (strncmp(val, "i386", 4) == 0) {␊ |
430 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
431 | ␉␉␉}␊ |
432 | ␉␉}␊ |
433 | ␉␉␊ |
434 | ␉␉if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {␊ |
435 | ␉␉␉if (strncmp(val, "i386", 4) == 0) {␊ |
436 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
437 | ␉␉␉}␊ |
438 | ␉␉}␊ |
439 | ␉␉␊ |
440 | ␉␉// Notify modules that we are attempting to boot␊ |
441 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL);␊ |
442 | ␉␉␊ |
443 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig)) {␊ |
444 | ␉␉␉tryresume = true;␊ |
445 | ␉␉␉tryresumedefault = true;␊ |
446 | ␉␉} else {␊ |
447 | ␉␉␉tryresumedefault = false;␊ |
448 | ␉␉}␊ |
449 | ␉␉␊ |
450 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {␊ |
451 | ␉␉␉forceresume = false;␊ |
452 | ␉␉}␊ |
453 | ␉␉␊ |
454 | ␉␉if (forceresume) {␊ |
455 | ␉␉␉tryresume = true;␊ |
456 | ␉␉␉tryresumedefault = false;␊ |
457 | ␉␉}␊ |
458 | ␉␉␊ |
459 | ␉␉while (tryresume) {␊ |
460 | ␉␉␉const char *tmp;␊ |
461 | ␉␉␉BVRef bvr;␊ |
462 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))␊ |
463 | ␉␉␉␉val = "/private/var/vm/sleepimage";␊ |
464 | ␉␉␉␊ |
465 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
466 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
467 | ␉␉␉␊ |
468 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
469 | ␉␉␉␉break;␊ |
470 | ␉␉␉␊ |
471 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
472 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
473 | ␉␉␉␉break;␊ |
474 | ␉␉␉␊ |
475 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
476 | ␉␉␉␉break;␊ |
477 | ␉␉␉␊ |
478 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
479 | #if DEBUG␉␊ |
480 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",␊ |
481 | ␉␉␉␉␉␉bvr->modTime-sleeptime);␊ |
482 | #endif␉␉␉␉ ␊ |
483 | ␉␉␉␉break;␊ |
484 | ␉␉␉}␊ |
485 | ␉␉␉␊ |
486 | ␉␉␉HibernateBoot((char *)val);␊ |
487 | ␉␉␉break;␊ |
488 | ␉␉}␊ |
489 | ␉␉␊ |
490 | ␉␉// If boot from boot helper partitions and OS is Lion use prelink kernel.␊ |
491 | ␉␉// We need to find a solution to load extra mkext with a prelink kernel.␊ |
492 | ␉␉if (gBootVolume->flags & kBVFlagBooter && checkOSVersion("10.7")) {␊ |
493 | ␉␉␉verbose("Booting from Lion RAID volume so forcing to use KernelCache\n");␊ |
494 | ␉␉␉useKernelCache = true;␊ |
495 | ␉␉} else {␊ |
496 | ␉␉␉getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);␊ |
497 | ␉␉}␊ |
498 | ␊ |
499 | ␉␉if (useKernelCache) {␊ |
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 prelink kernel cache file␊ |
510 | ␉␉␉␉if (checkOSVersion("10.7")) {␊ |
511 | ␉␉␉␉␉sprintf(gBootKernelCacheFile, "%skernelcache", kDefaultCachePathSnow);␊ |
512 | ␉␉␉␉}␊ |
513 | ␉␉␉␉// Snow Leopard prelink kernel cache file␊ |
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 = (useKernelCache &&␊ |
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 | ␉␉// Check if the kernel cache file exists and is more recent (mtime) than␊ |
558 | ␉␉// the kernel file or the S/L/E directory␊ |
559 | ␊ |
560 | ␉␉if (trycache) do {␊ |
561 | ␊ |
562 | ␉␉␉ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
563 | ␉␉␉// Check if the kernel cache file exist␊ |
564 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
565 | ␉␉␉␉trycache = 0;␊ |
566 | ␉␉␉␉break;␊ |
567 | ␉␉␉}␊ |
568 | ␊ |
569 | ␉␉␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
570 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
571 | ␉␉␉␉kerneltime = -1;␊ |
572 | ␉␉␉// Check if the kernel file is more recent than the cache file␊ |
573 | ␉␉␉if (kerneltime > cachetime) {␊ |
574 | ␉␉␉␉trycache = 0;␊ |
575 | ␉␉␉␉break;␊ |
576 | ␉␉␉}␊ |
577 | ␊ |
578 | ␉␉␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
579 | ␉␉␉if ((ret != 0) && ((flags & kFileTypeMask) != kFileTypeDirectory))␊ |
580 | ␉␉␉␉exttime = -1;␊ |
581 | ␉␉␉// Check if the S/L/E directory time is more recent than the cache file␊ |
582 | ␉␉␉if (exttime > cachetime) {␊ |
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 | ␉␉␉if (checkOSVersion("10.7")) {␊ |
609 | ␉␉␉␉bootFile = gBootKernelCacheFile;␊ |
610 | ␉␉␉}␊ |
611 | ␉␉␉else {␊ |
612 | ␉␉␉␉sprintf(bootFile, "/%s", bootInfo->bootFile);␊ |
613 | ␉␉␉}␊ |
614 | ␉␉␉␊ |
615 | ␉␉␉// Try to load kernel image from alternate locations on boot helper partitions.␊ |
616 | ␉␉␉sprintf(bootFileSpec, "com.apple.boot.P%s", bootFile);␊ |
617 | ␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
618 | ␉␉␉if (ret == -1)␊ |
619 | ␉␉␉{␊ |
620 | ␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.R%s", bootFile);␊ |
621 | ␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
622 | ␉␉␉␉if (ret == -1)␊ |
623 | ␉␉␉␉{␊ |
624 | ␉␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.S%s", bootFile);␊ |
625 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
626 | ␉␉␉␉␉if (ret == -1)␊ |
627 | ␉␉␉␉␉{␊ |
628 | ␉␉␉␉␉␉// No alternate location found, using the original kernel image path.␊ |
629 | ␉␉␉␉␉␉strcpy(bootFileSpec, bootInfo->bootFile);␊ |
630 | ␉␉␉␉␉}␊ |
631 | ␉␉␉␉}␊ |
632 | ␉␉␉}␊ |
633 | ␉␉␉␊ |
634 | ␉␉␉if (checkOSVersion("10.7"))␊ |
635 | ␉␉␉{␊ |
636 | ␉␉␉␉//Lion, dont load kernel if haz cache␊ |
637 | ␉␉␉␉if (!trycache) ␊ |
638 | ␉␉␉␉{␊ |
639 | ␉␉␉␉␉verbose("Loading kernel %s\n", bootFileSpec);␊ |
640 | ␉␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
641 | ␉␉␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64) ␊ |
642 | ␉␉␉␉␉{␊ |
643 | ␉␉␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
644 | ␉␉␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
645 | ␉␉␉␉␉}␊ |
646 | ␉␉␉␉} ␊ |
647 | ␉␉␉␉else ret = 1;␊ |
648 | ␉␉␉} ␊ |
649 | ␉␉␉else ␊ |
650 | ␉␉␉{␊ |
651 | ␉␉␉␉//Snow Leopard or older␊ |
652 | ␉␉␉␉verbose("Loading kernel %s\n", bootFileSpec);␊ |
653 | ␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
654 | ␉␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64) ␊ |
655 | ␉␉␉␉{␊ |
656 | ␉␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
657 | ␉␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
658 | ␉␉␉␉}␊ |
659 | ␉␉␉}␊ |
660 | ␉␉} while (0);␊ |
661 | ␉␉␊ |
662 | ␉␉clearActivityIndicator();␊ |
663 | ␉␉␊ |
664 | #if DEBUG␊ |
665 | ␉␉printf("Pausing...");␊ |
666 | ␉␉sleep(8);␊ |
667 | #endif␊ |
668 | ␉␉␊ |
669 | ␉␉if (ret <= 0) {␊ |
670 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
671 | ␉␉␉␊ |
672 | ␉␉␉sleep(1);␊ |
673 | ␉␉␉␊ |
674 | ␉␉␉if (gBootFileType == kNetworkDeviceType) {␊ |
675 | ␉␉␉␉// Return control back to PXE. Don't unload PXE base code.␊ |
676 | ␉␉␉␉gUnloadPXEOnExit = false;␊ |
677 | ␉␉␉␉break;␊ |
678 | ␉␉␉}␊ |
679 | ␉␉} else {␊ |
680 | ␉␉␉/* Won't return if successful. */␊ |
681 | ␉␉␉ret = ExecKernel(binary);␊ |
682 | ␉␉}␊ |
683 | ␉}␊ |
684 | ␉␊ |
685 | ␉// chainboot␊ |
686 | ␉if (status == 1) {␊ |
687 | ␉␉// if we are already in graphics-mode,␊ |
688 | ␉␉if (getVideoMode() == GRAPHICS_MODE) {␊ |
689 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.␊ |
690 | ␉␉}␊ |
691 | ␉}␊ |
692 | ␉␊ |
693 | ␉if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
694 | ␉␉nbpUnloadBaseCode();␊ |
695 | ␉}␊ |
696 | }␊ |
697 | ␊ |
698 | /*!␊ |
699 | ␉Selects a new BIOS device, taking care to update the global state appropriately.␊ |
700 | */␊ |
701 | /*␊ |
702 | static void selectBiosDevice(void)␊ |
703 | {␊ |
704 | ␉struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
705 | ␉CacheReset();␊ |
706 | ␉diskFreeMap(oldMap);␊ |
707 | ␉oldMap = NULL;␊ |
708 | ␉␊ |
709 | ␉int dev = selectAlternateBootDevice(gBIOSDev);␊ |
710 | ␉␊ |
711 | ␉BVRef bvchain = scanBootVolumes(dev, 0);␊ |
712 | ␉BVRef bootVol = selectBootVolume(bvchain);␊ |
713 | ␉gBootVolume = bootVol;␊ |
714 | ␉setRootVolume(bootVol);␊ |
715 | ␉gBIOSDev = dev;␊ |
716 | }␊ |
717 | */␊ |
718 | ␊ |
719 | bool checkOSVersion(const char * version) ␊ |
720 | {␊ |
721 | ␉return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])␊ |
722 | ␉␉␉&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));␊ |
723 | }␊ |
724 | ␊ |
725 | bool getOSVersion()␊ |
726 | {␊ |
727 | ␉bool␉␉␉valid = false;␊ |
728 | ␉const char␉␉*val;␊ |
729 | ␉int␉␉␉␉len;␊ |
730 | ␉config_file_t␉systemVersion;␊ |
731 | ␉␊ |
732 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
733 | ␉{␊ |
734 | ␉␉valid = true;␊ |
735 | ␉}␊ |
736 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
737 | ␉{␊ |
738 | ␉␉valid = true;␊ |
739 | ␉}␊ |
740 | ␉␊ |
741 | ␉if (valid)␊ |
742 | ␉{␊ |
743 | ␉␉if␉(getValueForKey(kProductVersion, &val, &len, &systemVersion))␊ |
744 | ␉␉{␊ |
745 | ␉␉␉// getValueForKey uses const char for val␊ |
746 | ␉␉␉// so copy it and trim␊ |
747 | ␉␉␉*gMacOSVersion = '\0';␊ |
748 | ␉␉␉strncat(gMacOSVersion, val, MIN(len, 4));␊ |
749 | ␉␉}␊ |
750 | ␉␉else␊ |
751 | ␉␉␉valid = false;␊ |
752 | ␉}␊ |
753 | ␉␊ |
754 | ␉return valid;␊ |
755 | }␊ |
756 | ␊ |
757 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
758 | #define NMAX 5000␊ |
759 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
760 | ␊ |
761 | #define DO1(buf, i)␉{s1 += buf[i]; s2 += s1;}␊ |
762 | #define DO2(buf, i)␉DO1(buf, i); DO1(buf, i + 1);␊ |
763 | #define DO4(buf, i)␉DO2(buf, i); DO2(buf, i + 2);␊ |
764 | #define DO8(buf, i)␉DO4(buf, i); DO4(buf, i + 4);␊ |
765 | #define DO16(buf)␉DO8(buf, 0); DO8(buf, 8);␊ |
766 | ␊ |
767 | unsigned long Adler32(unsigned char *buf, long len)␊ |
768 | {␊ |
769 | ␉unsigned long s1 = 1; // adler & 0xffff;␊ |
770 | ␉unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
771 | ␉unsigned long result;␊ |
772 | ␉int k;␊ |
773 | ␉␊ |
774 | ␉while (len > 0) {␊ |
775 | ␉␉k = len < NMAX ? len : NMAX;␊ |
776 | ␉␉len -= k;␊ |
777 | ␉␉while (k >= 16) {␊ |
778 | ␉␉␉DO16(buf);␊ |
779 | ␉␉␉buf += 16;␊ |
780 | ␉␉␉k -= 16;␊ |
781 | ␉␉}␊ |
782 | ␉␉if (k != 0) do {␊ |
783 | ␉␉␉s1 += *buf++;␊ |
784 | ␉␉␉s2 += s1;␊ |
785 | ␉␉} while (--k);␊ |
786 | ␉␉s1 %= BASE;␊ |
787 | ␉␉s2 %= BASE;␊ |
788 | ␉}␊ |
789 | ␉result = (s2 << 16) | s1;␊ |
790 | ␉return OSSwapHostToBigInt32(result);␊ |
791 | }␊ |
792 | |