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