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