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