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