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