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