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