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