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␉␉usecache = false;//true;␊ |
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 | ␉␉getBoolForKey(kUseKernelCache, &usecache, &bootInfo->chameleonConfig);␊ |
491 | ␉␉if (usecache) {␊ |
492 | ␉␉␉if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
493 | ␉␉␉␉if (val[0] == '\\')␊ |
494 | ␉␉␉␉{␊ |
495 | ␉␉␉␉␉len--;␊ |
496 | ␉␉␉␉␉val++;␊ |
497 | ␉␉␉␉}␊ |
498 | ␉␉␉␉strlcpy(gBootKernelCacheFile, val, len + 1);␊ |
499 | ␉␉␉}␊ |
500 | ␉␉␉else {␊ |
501 | ␉␉␉␉//Lion␊ |
502 | ␉␉␉␉if (checkOSVersion("10.7")) {␊ |
503 | ␉␉␉␉␉sprintf(gBootKernelCacheFile, "%skernelcache", kDefaultCachePathSnow);␊ |
504 | ␉␉␉␉}␊ |
505 | ␉␉␉␉// Snow Leopard␊ |
506 | ␉␉␉␉else if (checkOSVersion("10.6")) {␊ |
507 | ␉␉␉␉␉sprintf(gBootKernelCacheFile, "kernelcache_%s", (archCpuType == CPU_TYPE_I386)␊ |
508 | ␉␉␉␉␉␉␉? "i386" : "x86_64");␊ |
509 | ␉␉␉␉␉int lnam = sizeof(gBootKernelCacheFile) + 9; //with adler32␊ |
510 | ␉␉␉␉␉␊ |
511 | ␉␉␉␉␉char* name;␊ |
512 | ␉␉␉␉␉long prev_time = 0;␊ |
513 | ␉␉␉␉␉␊ |
514 | ␉␉␉␉␉struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);␊ |
515 | ␉␉␉␉␉␊ |
516 | ␉␉␉␉␉while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)␊ |
517 | ␉␉␉␉␉{␊ |
518 | ␉␉␉␉␉␉if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time␊ |
519 | ␉␉␉␉␉␉␉&& strstr(name, gBootKernelCacheFile) && (name[lnam] != '.'))␊ |
520 | ␉␉␉␉␉␉{␊ |
521 | ␉␉␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s%s", kDefaultCachePathSnow, name);␊ |
522 | ␉␉␉␉␉␉␉prev_time = time;␊ |
523 | ␉␉␉␉␉␉}␊ |
524 | ␉␉␉␉␉}␊ |
525 | ␉␉␉␉}␊ |
526 | ␉␉␉␉else {␊ |
527 | ␉␉␉␉␉// Reset cache name.␊ |
528 | ␉␉␉␉␉bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
529 | ␉␉␉␉␉␊ |
530 | ␉␉␉␉␉sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
531 | ␉␉␉␉␉␊ |
532 | ␉␉␉␉␉adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
533 | ␉␉␉␉␉␊ |
534 | ␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePathLeo, adler32);␊ |
535 | ␉␉␉␉}␊ |
536 | ␉␉␉}␊ |
537 | ␉␉}␊ |
538 | ␉␉␊ |
539 | ␉␉// Check for cache file.␊ |
540 | ␉␉trycache = (usecache && ␊ |
541 | ␉␉␉␉␉((gBootMode & kBootModeSafe) == 0) &&␊ |
542 | ␉␉␉␉␉!gOverrideKernel &&␊ |
543 | ␉␉␉␉␉(gBootFileType == kBlockDeviceType) &&␊ |
544 | ␉␉␉␉␉(gMKextName[0] == '\0') &&␊ |
545 | ␉␉␉␉␉(gBootKernelCacheFile[0] != '\0'));␊ |
546 | ␉␉␊ |
547 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
548 | ␉␉␊ |
549 | ␉␉if (trycache) do {␊ |
550 | ␉␉␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
551 | ␉␉␉if (ret != 0) kerneltime = 0;␊ |
552 | ␉␉␉else if ((flags & kFileTypeMask) != kFileTypeFlat) {␊ |
553 | ␉␉␉␉trycache = 0;␊ |
554 | ␉␉␉␉break;␊ |
555 | ␉␉␉}␊ |
556 | ␉␉␉␊ |
557 | ␉␉␉ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
558 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
559 | ␉␉␉␉|| (cachetime < kerneltime)) {␊ |
560 | ␉␉␉␉trycache = 0;␊ |
561 | ␉␉␉␉break;␊ |
562 | ␉␉␉}␊ |
563 | ␉␉␉␊ |
564 | ␉␉␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
565 | ␉␉␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
566 | ␉␉␉␉&& (cachetime < exttime)) {␊ |
567 | ␉␉␉␉trycache = 0;␊ |
568 | ␉␉␉␉break;␊ |
569 | ␉␉␉}␊ |
570 | ␉␉␉␊ |
571 | ␉␉␉if (ret == 0 && kerneltime > exttime) {␊ |
572 | ␉␉␉␉exttime = kerneltime;␊ |
573 | ␉␉␉}␊ |
574 | ␉␉␉␊ |
575 | ␉␉␉if (ret == 0 && cachetime != (exttime + 1)) {␊ |
576 | ␉␉␉␉trycache = 0;␊ |
577 | ␉␉␉␉break;␊ |
578 | ␉␉␉}␊ |
579 | ␉␉} while (0);␊ |
580 | ␊ |
581 | ␉␉do {␊ |
582 | ␉␉␉if (trycache) {␊ |
583 | ␉␉␉␉bootFile = gBootKernelCacheFile;␊ |
584 | ␉␉␉␉␊ |
585 | ␉␉␉␉verbose("Loading kernel cache %s\n", bootFile);␊ |
586 | ␉␉␉␉␊ |
587 | ␉␉␉␉if (checkOSVersion("10.7")) {␊ |
588 | ␉␉␉␉␉ret = LoadThinFatFile(bootFile, &binary);␊ |
589 | ␉␉␉␉}␊ |
590 | ␉␉␉␉else {␊ |
591 | ␉␉␉␉␉ret = LoadFile(bootFile);␊ |
592 | ␉␉␉␉␉binary = (void *)kLoadAddr;␊ |
593 | ␉␉␉␉}␊ |
594 | ␉␉␉␉␊ |
595 | ␉␉␉␉if (ret >= 0)␊ |
596 | ␉␉␉␉␉break;␊ |
597 | ␉␉␉␉␊ |
598 | ␉␉␉␉verbose("Kernel cache did not load %s\n ", bootFile);␊ |
599 | ␉␉␉}␊ |
600 | ␉␉␉␊ |
601 | ␉␉␉if (checkOSVersion("10.7")) {␊ |
602 | ␉␉␉␉bootFile = gBootKernelCacheFile;␊ |
603 | ␉␉␉}␊ |
604 | ␉␉␉else {␊ |
605 | ␉␉␉␉sprintf(bootFile, "/%s", bootInfo->bootFile);␊ |
606 | ␉␉␉}␊ |
607 | ␉␉␉␊ |
608 | ␉␉␉// Try to load kernel image from alternate locations on boot helper partitions.␊ |
609 | ␉␉␉sprintf(bootFileSpec, "com.apple.boot.P%s", bootFile);␊ |
610 | ␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
611 | ␉␉␉if (ret == -1)␊ |
612 | ␉␉␉{␊ |
613 | ␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.R%s", bootFile);␊ |
614 | ␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
615 | ␉␉␉␉if (ret == -1)␊ |
616 | ␉␉␉␉{␊ |
617 | ␉␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.S%s", bootFile);␊ |
618 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
619 | ␉␉␉␉␉if (ret == -1)␊ |
620 | ␉␉␉␉␉{␊ |
621 | ␉␉␉␉␉␉// No alternate location found, using the original kernel image path.␊ |
622 | ␉␉␉␉␉␉strcpy(bootFileSpec, bootInfo->bootFile);␊ |
623 | ␉␉␉␉␉}␊ |
624 | ␉␉␉␉}␊ |
625 | ␉␉␉}␊ |
626 | ␉␉␉␊ |
627 | ␉␉␉if (checkOSVersion("10.7"))␊ |
628 | ␉␉␉{␊ |
629 | ␉␉␉␉//Lion, dont load kernel if haz cache␊ |
630 | ␉␉␉␉if (!trycache) ␊ |
631 | ␉␉␉␉{␊ |
632 | ␉␉␉␉␉verbose("Loading kernel %s\n", bootFileSpec);␊ |
633 | ␉␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
634 | ␉␉␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64) ␊ |
635 | ␉␉␉␉␉{␊ |
636 | ␉␉␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
637 | ␉␉␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
638 | ␉␉␉␉␉}␊ |
639 | ␉␉␉␉} ␊ |
640 | ␉␉␉␉else ret = 1;␊ |
641 | ␉␉␉} ␊ |
642 | ␉␉␉else ␊ |
643 | ␉␉␉{␊ |
644 | ␉␉␉␉//Snow Leopard or older␊ |
645 | ␉␉␉␉verbose("Loading kernel %s\n", bootFileSpec);␊ |
646 | ␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
647 | ␉␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64) ␊ |
648 | ␉␉␉␉{␊ |
649 | ␉␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
650 | ␉␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
651 | ␉␉␉␉}␊ |
652 | ␉␉␉}␊ |
653 | ␉␉} while (0);␊ |
654 | ␉␉␊ |
655 | ␉␉clearActivityIndicator();␊ |
656 | ␉␉␊ |
657 | #if DEBUG␊ |
658 | ␉␉printf("Pausing...");␊ |
659 | ␉␉sleep(8);␊ |
660 | #endif␊ |
661 | ␉␉␊ |
662 | ␉␉if (ret <= 0) {␊ |
663 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
664 | ␉␉␉␊ |
665 | ␉␉␉sleep(1);␊ |
666 | ␉␉␉␊ |
667 | ␉␉␉if (gBootFileType == kNetworkDeviceType) {␊ |
668 | ␉␉␉␉// Return control back to PXE. Don't unload PXE base code.␊ |
669 | ␉␉␉␉gUnloadPXEOnExit = false;␊ |
670 | ␉␉␉␉break;␊ |
671 | ␉␉␉}␊ |
672 | ␉␉} else {␊ |
673 | ␉␉␉/* Won't return if successful. */␊ |
674 | ␉␉␉ret = ExecKernel(binary);␊ |
675 | ␉␉}␊ |
676 | ␉}␊ |
677 | ␉␊ |
678 | ␉// chainboot␊ |
679 | ␉if (status == 1) {␊ |
680 | ␉␉// if we are already in graphics-mode,␊ |
681 | ␉␉if (getVideoMode() == 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])␊ |
715 | ␉␉␉&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));␊ |
716 | }␊ |
717 | ␊ |
718 | bool getOSVersion()␊ |
719 | {␊ |
720 | ␉bool␉␉␉valid = false;␊ |
721 | ␉const char␉␉*val;␊ |
722 | ␉int␉␉␉␉len;␊ |
723 | ␉config_file_t␉systemVersion;␊ |
724 | ␉␊ |
725 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
726 | ␉{␊ |
727 | ␉␉valid = true;␊ |
728 | ␉}␊ |
729 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
730 | ␉{␊ |
731 | ␉␉valid = true;␊ |
732 | ␉}␊ |
733 | ␉␊ |
734 | ␉if (valid)␊ |
735 | ␉{␊ |
736 | ␉␉if␉(getValueForKey(kProductVersion, &val, &len, &systemVersion))␊ |
737 | ␉␉{␊ |
738 | ␉␉␉// getValueForKey uses const char for val␊ |
739 | ␉␉␉// so copy it and trim␊ |
740 | ␉␉␉*gMacOSVersion = '\0';␊ |
741 | ␉␉␉strncat(gMacOSVersion, val, MIN(len, 4));␊ |
742 | ␉␉}␊ |
743 | ␉␉else␊ |
744 | ␉␉␉valid = false;␊ |
745 | ␉}␊ |
746 | ␉␊ |
747 | ␉return valid;␊ |
748 | }␊ |
749 | ␊ |
750 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
751 | #define NMAX 5000␊ |
752 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
753 | ␊ |
754 | #define DO1(buf, i)␉{s1 += buf[i]; s2 += s1;}␊ |
755 | #define DO2(buf, i)␉DO1(buf, i); DO1(buf, i + 1);␊ |
756 | #define DO4(buf, i)␉DO2(buf, i); DO2(buf, i + 2);␊ |
757 | #define DO8(buf, i)␉DO4(buf, i); DO4(buf, i + 4);␊ |
758 | #define DO16(buf)␉DO8(buf, 0); DO8(buf, 8);␊ |
759 | ␊ |
760 | unsigned long Adler32(unsigned char *buf, long len)␊ |
761 | {␊ |
762 | ␉unsigned long s1 = 1; // adler & 0xffff;␊ |
763 | ␉unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
764 | ␉unsigned long result;␊ |
765 | ␉int k;␊ |
766 | ␉␊ |
767 | ␉while (len > 0) {␊ |
768 | ␉␉k = len < NMAX ? len : NMAX;␊ |
769 | ␉␉len -= k;␊ |
770 | ␉␉while (k >= 16) {␊ |
771 | ␉␉␉DO16(buf);␊ |
772 | ␉␉␉buf += 16;␊ |
773 | ␉␉␉k -= 16;␊ |
774 | ␉␉}␊ |
775 | ␉␉if (k != 0) do {␊ |
776 | ␉␉␉s1 += *buf++;␊ |
777 | ␉␉␉s2 += s1;␊ |
778 | ␉␉} while (--k);␊ |
779 | ␉␉s1 %= BASE;␊ |
780 | ␉␉s2 %= BASE;␊ |
781 | ␉}␊ |
782 | ␉result = (s2 << 16) | s1;␊ |
783 | ␉return OSSwapHostToBigInt32(result);␊ |
784 | }␊ |
785 | |