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