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