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