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