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