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