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