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