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