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