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