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