1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /* ␊ |
25 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * All rights reserved. The CMU software License Agreement specifies␊ |
29 | * the terms and conditions for use and redistribution.␊ |
30 | */␊ |
31 | ␊ |
32 | /*␊ |
33 | * INTEL CORPORATION PROPRIETARY INFORMATION␊ |
34 | *␊ |
35 | * This software is supplied under the terms of a license agreement or ␊ |
36 | * nondisclosure agreement with Intel Corporation and may not be copied ␊ |
37 | * nor disclosed except in accordance with the terms of that agreement.␊ |
38 | *␊ |
39 | * Copyright 1988, 1989 by Intel Corporation␊ |
40 | */␊ |
41 | ␊ |
42 | /*␊ |
43 | * Copyright 1993 NeXT Computer, Inc.␊ |
44 | * All rights reserved.␊ |
45 | */␊ |
46 | ␊ |
47 | /*␊ |
48 | * Completely reworked by Sam Streeper (sam_s@NeXT.com)␊ |
49 | * Reworked again by Curtis Galloway (galloway@NeXT.com)␊ |
50 | */␊ |
51 | ␊ |
52 | ␊ |
53 | #include "boot.h"␊ |
54 | #include "bootstruct.h"␊ |
55 | #include "fake_efi.h"␊ |
56 | #include "sl.h"␊ |
57 | #include "libsa.h"␊ |
58 | #include "ramdisk.h"␊ |
59 | #include "gui.h"␊ |
60 | #include "platform.h"␊ |
61 | ␊ |
62 | long gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
63 | bool gOverrideKernel;␊ |
64 | static char gBootKernelCacheFile[512];␊ |
65 | static char gCacheNameAdler[64 + 256];␊ |
66 | char *gPlatformName = gCacheNameAdler;␊ |
67 | char gRootDevice[512];␊ |
68 | char gMKextName[512];␊ |
69 | char gMacOSVersion[8];␊ |
70 | bool gEnableCDROMRescan;␊ |
71 | bool gScanSingleDrive;␊ |
72 | ␊ |
73 | int bvCount = 0;␊ |
74 | //int␉␉menucount = 0;␊ |
75 | int gDeviceCount = 0; ␊ |
76 | ␊ |
77 | BVRef bvr;␊ |
78 | BVRef menuBVR;␊ |
79 | BVRef bvChain;␊ |
80 | bool useGUI;␊ |
81 | ␊ |
82 | //static void selectBiosDevice(void);␊ |
83 | static unsigned long Adler32(unsigned char *buffer, long length);␊ |
84 | ␊ |
85 | ␊ |
86 | static bool gUnloadPXEOnExit = false;␊ |
87 | ␊ |
88 | /*␊ |
89 | * How long to wait (in seconds) to load the␊ |
90 | * kernel after displaying the "boot:" prompt.␊ |
91 | */␊ |
92 | #define kBootErrorTimeout 5␊ |
93 | ␊ |
94 | /*␊ |
95 | * Default path to kernel cache file␊ |
96 | */␊ |
97 | #define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"␊ |
98 | ␊ |
99 | //==========================================================================␊ |
100 | // Zero the BSS.␊ |
101 | ␊ |
102 | static void zeroBSS(void)␊ |
103 | {␊ |
104 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
105 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
106 | ␊ |
107 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
108 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
109 | }␊ |
110 | ␊ |
111 | //==========================================================================␊ |
112 | // Malloc error function␊ |
113 | ␊ |
114 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
115 | {␊ |
116 | ␉stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);␊ |
117 | }␊ |
118 | ␊ |
119 | //==========================================================================␊ |
120 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
121 | //␊ |
122 | void initialize_runtime(void)␊ |
123 | {␊ |
124 | ␉zeroBSS();␊ |
125 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
126 | }␊ |
127 | ␊ |
128 | //==========================================================================␊ |
129 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
130 | ␊ |
131 | static int ExecKernel(void *binary)␊ |
132 | {␊ |
133 | entry_t kernelEntry;␊ |
134 | int ret;␊ |
135 | ␊ |
136 | bootArgs->kaddr = bootArgs->ksize = 0;␊ |
137 | ␊ |
138 | ret = DecodeKernel(binary,␊ |
139 | &kernelEntry,␊ |
140 | (char **) &bootArgs->kaddr,␊ |
141 | (int *)&bootArgs->ksize );␊ |
142 | ␊ |
143 | if ( ret != 0 )␊ |
144 | return ret;␊ |
145 | ␊ |
146 | // Reserve space for boot args␊ |
147 | reserveKernBootStruct();␊ |
148 | ␊ |
149 | // Load boot drivers from the specifed root path.␊ |
150 | ␊ |
151 | if (!gHaveKernelCache) {␊ |
152 | LoadDrivers("/");␊ |
153 | }␊ |
154 | ␊ |
155 | clearActivityIndicator();␊ |
156 | ␊ |
157 | if (gErrors) {␊ |
158 | printf("Errors encountered while starting up the computer.\n");␊ |
159 | printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
160 | sleep(kBootErrorTimeout);␊ |
161 | }␊ |
162 | ␊ |
163 | setupFakeEfi();␊ |
164 | ␊ |
165 | md0Ramdisk();␊ |
166 | ␊ |
167 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
168 | ␊ |
169 | // Cleanup the PXE base code.␊ |
170 | ␊ |
171 | if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {␊ |
172 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
173 | {␊ |
174 | ␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
175 | sleep(2);␊ |
176 | }␊ |
177 | }␊ |
178 | ␊ |
179 | bool dummyVal;␊ |
180 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {␊ |
181 | ␉␉printf("Press any key to continue...");␊ |
182 | ␉␉getc();␊ |
183 | ␉}␊ |
184 | ␊ |
185 | ␉usb_loop();␊ |
186 | ␊ |
187 | // If we were in text mode, switch to graphics mode.␊ |
188 | // This will draw the boot graphics unless we are in␊ |
189 | // verbose mode.␊ |
190 | ␊ |
191 | if(gVerboseMode)␊ |
192 | setVideoMode( GRAPHICS_MODE, 0 );␊ |
193 | else␊ |
194 | drawBootGraphics();␊ |
195 | ␉␊ |
196 | finalizeBootStruct();␊ |
197 | ␊ |
198 | // Jump to kernel's entry point. There's no going back now.␊ |
199 | ␊ |
200 | startprog( kernelEntry, bootArgs );␊ |
201 | ␊ |
202 | // Not reached␊ |
203 | ␊ |
204 | return 0;␊ |
205 | }␊ |
206 | ␊ |
207 | //==========================================================================␊ |
208 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
209 | // before. Multiboot does its own runtime initialization, does some of its␊ |
210 | // own things, and then calls common_boot.␊ |
211 | void boot(int biosdev)␊ |
212 | {␊ |
213 | ␉initialize_runtime();␊ |
214 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
215 | ␉enableA20();␊ |
216 | ␉common_boot(biosdev);␊ |
217 | }␊ |
218 | ␊ |
219 | //==========================================================================␊ |
220 | // The 'main' function for the booter. Called by boot0 when booting␊ |
221 | // from a block device, or by the network booter.␊ |
222 | //␊ |
223 | // arguments:␊ |
224 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
225 | // that the booter was loaded from.␊ |
226 | //␊ |
227 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
228 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
229 | // next boot device on its list.␊ |
230 | void common_boot(int biosdev)␊ |
231 | {␊ |
232 | int status;␊ |
233 | char *bootFile;␊ |
234 | unsigned long adler32;␊ |
235 | bool quiet;␊ |
236 | bool firstRun = true;␊ |
237 | bool instantMenu;␊ |
238 | bool rescanPrompt;␊ |
239 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
240 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
241 | ␊ |
242 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
243 | // the base code will result in a hang or kernel panic.␊ |
244 | gUnloadPXEOnExit = true;␊ |
245 | ␊ |
246 | // Record the device that the booter was loaded from.␊ |
247 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
248 | ␊ |
249 | // Initialize boot info structure.␊ |
250 | initKernBootStruct();␊ |
251 | ␊ |
252 | // Setup VGA text mode.␊ |
253 | // Not sure if it is safe to call setVideoMode() before the␊ |
254 | // config table has been loaded. Call video_mode() instead.␊ |
255 | #if DEBUG␊ |
256 | printf("before video_mode\n");␊ |
257 | #endif␊ |
258 | video_mode( 2 ); // 80x25 mono text mode.␊ |
259 | #if DEBUG␊ |
260 | printf("after video_mode\n");␊ |
261 | #endif␊ |
262 | ␊ |
263 | // Scan and record the system's hardware information.␊ |
264 | scan_platform();␊ |
265 | ␊ |
266 | // First get info for boot volume.␊ |
267 | scanBootVolumes(gBIOSDev, 0);␊ |
268 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
269 | setBootGlobals(bvChain);␊ |
270 | ␊ |
271 | // Load boot.plist config file␊ |
272 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
273 | ␊ |
274 | if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {␊ |
275 | gBootMode |= kBootModeQuiet;␊ |
276 | }␊ |
277 | ␊ |
278 | // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
279 | if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {␊ |
280 | firstRun = false;␊ |
281 | }␊ |
282 | ␊ |
283 | // Loading preboot ramdisk if exists.␊ |
284 | loadPrebootRAMDisk();␊ |
285 | ␊ |
286 | // Disable rescan option by default␊ |
287 | gEnableCDROMRescan = false;␊ |
288 | ␊ |
289 | // Enable it with Rescan=y in system config␊ |
290 | if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {␊ |
291 | gEnableCDROMRescan = true;␊ |
292 | }␊ |
293 | ␊ |
294 | // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
295 | rescanPrompt = false;␊ |
296 | if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {␊ |
297 | gEnableCDROMRescan = promptForRescanOption();␊ |
298 | }␊ |
299 | ␊ |
300 | // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
301 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {␊ |
302 | gScanSingleDrive = true;␊ |
303 | }␊ |
304 | ␊ |
305 | // Create a list of partitions on device(s).␊ |
306 | if (gScanSingleDrive) {␊ |
307 | scanBootVolumes(gBIOSDev, &bvCount);␊ |
308 | } else {␊ |
309 | scanDisks(gBIOSDev, &bvCount);␊ |
310 | }␊ |
311 | ␊ |
312 | // Create a separated bvr chain using the specified filters.␊ |
313 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
314 | ␊ |
315 | gBootVolume = selectBootVolume(bvChain);␊ |
316 | ␊ |
317 | #if DEBUG␊ |
318 | printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
319 | printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
320 | getc();␊ |
321 | #endif␊ |
322 | ␊ |
323 | ␉useGUI = true;␊ |
324 | ␉// Override useGUI default␊ |
325 | ␉getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);␊ |
326 | ␉if (useGUI && initGUI())␊ |
327 | ␉{␊ |
328 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
329 | ␉␉useGUI = false;␊ |
330 | ␉}␊ |
331 | ␊ |
332 | setBootGlobals(bvChain);␊ |
333 | ␊ |
334 | // Parse args, load and start kernel.␊ |
335 | while (1) {␊ |
336 | const char *val;␊ |
337 | int len;␊ |
338 | int trycache;␊ |
339 | long flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
340 | int ret = -1;␊ |
341 | void *binary = (void *)kLoadAddr;␊ |
342 | bool tryresume;␊ |
343 | bool tryresumedefault;␊ |
344 | bool forceresume;␊ |
345 | ␊ |
346 | config_file_t systemVersion;␉␉// system.plist of booting partition␊ |
347 | ␊ |
348 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
349 | char bootFileSpec[512];␊ |
350 | ␉␉␊ |
351 | // Initialize globals.␊ |
352 | ␊ |
353 | sysConfigValid = false;␊ |
354 | gErrors = false;␊ |
355 | ␊ |
356 | status = getBootOptions(firstRun);␊ |
357 | firstRun = false;␊ |
358 | if (status == -1) continue;␊ |
359 | ␉␉ ␊ |
360 | status = processBootOptions();␊ |
361 | // Status==1 means to chainboot␊ |
362 | if ( status == 1 ) break;␊ |
363 | // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
364 | if ( status == -1 )␊ |
365 | {␊ |
366 | // gBootVolume == NULL usually means the user hit escape.␊ |
367 | if(gBootVolume == NULL)␊ |
368 | {␊ |
369 | freeFilteredBVChain(bvChain);␊ |
370 | ␊ |
371 | if (gEnableCDROMRescan)␊ |
372 | rescanBIOSDevice(gBIOSDev);␊ |
373 | ␊ |
374 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
375 | setBootGlobals(bvChain);␊ |
376 | }␊ |
377 | continue;␊ |
378 | }␊ |
379 | ␉␉␊ |
380 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
381 | ␉␉␊ |
382 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
383 | ␉␉␉drawBackground();␊ |
384 | ␉␉␉␊ |
385 | // Found and loaded a config file. Proceed with boot.␊ |
386 | ␊ |
387 | ␉␉// Turn off any GUI elements␊ |
388 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
389 | ␉␉{␊ |
390 | ␉␉␉gui.devicelist.draw = false;␊ |
391 | ␉␉␉gui.bootprompt.draw = false;␊ |
392 | ␉␉␉gui.menu.draw = false;␊ |
393 | ␉␉␉gui.infobox.draw = false;␊ |
394 | ␉␉␉drawBackground();␊ |
395 | ␉␉␉updateVRAM();␊ |
396 | ␉␉}␊ |
397 | ␉␉␊ |
398 | ␉␉// Find out which version mac os we're booting.␊ |
399 | ␉␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion)) {␊ |
400 | ␉␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion)) {␉␊ |
401 | ␉␉␉␉// getValueForKey uses const char for val␊ |
402 | ␉␉␉␉// so copy it and trim␊ |
403 | ␉␉␉␉strncpy(gMacOSVersion, val, MIN(len, 4));␊ |
404 | ␉␉␉␉gMacOSVersion[MIN(len, 4)] = '\0';␊ |
405 | ␉␉␉}␊ |
406 | ␉␉}␊ |
407 | ␊ |
408 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T)) {␊ |
409 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
410 | ␉␉} else {␊ |
411 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
412 | ␉␉}␊ |
413 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {␊ |
414 | ␉␉␉if (strncmp(val, "i386", 4) == 0) {␊ |
415 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
416 | ␉␉␉}␊ |
417 | ␉␉}␊ |
418 | ␉␉if (getValueForKey(k32BitModeFlag, &val, &len, &bootInfo->bootConfig)) {␊ |
419 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
420 | ␉␉}␊ |
421 | ␉␉␊ |
422 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {␊ |
423 | ␉␉␉tryresume = true;␊ |
424 | ␉␉␉tryresumedefault = true;␊ |
425 | ␉␉} else {␊ |
426 | ␉␉␉tryresumedefault = false;␊ |
427 | ␉␉}␊ |
428 | ␊ |
429 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {␊ |
430 | ␉␉␉forceresume = false;␊ |
431 | ␉␉}␊ |
432 | ␉␉␊ |
433 | ␉␉if (forceresume) {␊ |
434 | ␉␉␉tryresume = true;␊ |
435 | ␉␉␉tryresumedefault = false;␊ |
436 | ␉␉}␊ |
437 | ␉␉␊ |
438 | ␉␉while (tryresume) {␊ |
439 | ␉␉␉const char *tmp;␊ |
440 | ␉␉␉BVRef bvr;␊ |
441 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))␊ |
442 | ␉␉␉␉val="/private/var/vm/sleepimage";␊ |
443 | ␉␉␉␊ |
444 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
445 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
446 | ␊ |
447 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
448 | ␉␉␉␉break;␊ |
449 | ␉␉␉␊ |
450 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
451 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
452 | ␉␉␉␉break;␊ |
453 | ␉␉␉␊ |
454 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
455 | ␉␉␉␉break;␊ |
456 | ␉␉␉␊ |
457 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
458 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);␊ |
459 | ␉␉␉␉break;␊ |
460 | ␉␉␉}␊ |
461 | ␉␉␉␉␊ |
462 | ␉␉␉HibernateBoot((char *)val);␊ |
463 | ␉␉␉break;␊ |
464 | ␉␉}␊ |
465 | ␊ |
466 | // Reset cache name.␊ |
467 | bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
468 | ␊ |
469 | sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
470 | ␊ |
471 | adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
472 | ␊ |
473 | if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
474 | strlcpy(gBootKernelCacheFile, val, len+1);␊ |
475 | } else {␊ |
476 | sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);␊ |
477 | }␊ |
478 | ␊ |
479 | // Check for cache file.␊ |
480 | trycache = (((gBootMode & kBootModeSafe) == 0) &&␊ |
481 | !gOverrideKernel &&␊ |
482 | (gBootFileType == kBlockDeviceType) &&␊ |
483 | (gMKextName[0] == '\0') &&␊ |
484 | (gBootKernelCacheFile[0] != '\0'));␊ |
485 | ␊ |
486 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
487 | ␉␉␊ |
488 | if (trycache) do {␊ |
489 | ␊ |
490 | // if we haven't found the kernel yet, don't use the cache␊ |
491 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
492 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
493 | trycache = 0;␊ |
494 | break;␊ |
495 | }␊ |
496 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
497 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
498 | || (cachetime < kerneltime)) {␊ |
499 | trycache = 0;␊ |
500 | break;␊ |
501 | }␊ |
502 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
503 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
504 | && (cachetime < exttime)) {␊ |
505 | trycache = 0;␊ |
506 | break;␊ |
507 | }␊ |
508 | if (kerneltime > exttime) {␊ |
509 | exttime = kerneltime;␊ |
510 | }␊ |
511 | if (cachetime != (exttime + 1)) {␊ |
512 | trycache = 0;␊ |
513 | break;␊ |
514 | }␊ |
515 | } while (0);␊ |
516 | ␊ |
517 | do {␊ |
518 | if (trycache) {␊ |
519 | bootFile = gBootKernelCacheFile;␊ |
520 | verbose("Loading kernel cache %s\n", bootFile);␊ |
521 | ret = LoadFile(bootFile);␊ |
522 | binary = (void *)kLoadAddr;␊ |
523 | if (ret >= 0) {␊ |
524 | break;␊ |
525 | }␊ |
526 | }␊ |
527 | bootFile = bootInfo->bootFile;␊ |
528 | ␊ |
529 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
530 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
531 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
532 | ␉ ␉ if (ret == -1)␊ |
533 | ␉ ␉ {␊ |
534 | sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
535 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
536 | if (ret == -1)␊ |
537 | {␊ |
538 | sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
539 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
540 | if (ret == -1)␊ |
541 | {␊ |
542 | // Not found any alternate locations, using the original kernel image path.␊ |
543 | strcpy(bootFileSpec, bootFile);␊ |
544 | }␊ |
545 | }␊ |
546 | }␊ |
547 | ␉␉␉␊ |
548 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
549 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
550 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
551 | {␊ |
552 | archCpuType = CPU_TYPE_I386;␊ |
553 | ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
554 | }␊ |
555 | ␉␉␉␊ |
556 | } while (0);␊ |
557 | ␊ |
558 | clearActivityIndicator();␊ |
559 | #if DEBUG␊ |
560 | printf("Pausing...");␊ |
561 | sleep(8);␊ |
562 | #endif␊ |
563 | ␊ |
564 | if (ret <= 0) {␊ |
565 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
566 | ␊ |
567 | ␉␉␉if(gui.initialised) {␊ |
568 | ␉␉␉␉sleep(1);␊ |
569 | ␉␉␉␉drawBackground();␊ |
570 | ␉␉␉␉gui.devicelist.draw = true;␊ |
571 | ␉␉␉␉gui.redraw = true;␊ |
572 | ␉␉␉}␊ |
573 | if (gBootFileType == kNetworkDeviceType) {␊ |
574 | // Return control back to PXE. Don't unload PXE base code.␊ |
575 | gUnloadPXEOnExit = false;␊ |
576 | break;␊ |
577 | }␊ |
578 | } else {␊ |
579 | /* Won't return if successful. */␊ |
580 | ret = ExecKernel(binary);␊ |
581 | }␊ |
582 | }␊ |
583 | ␊ |
584 | // chainboot␊ |
585 | if (status==1) {␊ |
586 | ␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
587 | ␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
588 | ␉}␊ |
589 | }␊ |
590 | ␉␊ |
591 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
592 | ␉nbpUnloadBaseCode();␊ |
593 | }␊ |
594 | }␊ |
595 | ␊ |
596 | /*!␊ |
597 | Selects a new BIOS device, taking care to update the global state appropriately.␊ |
598 | */␊ |
599 | /*␊ |
600 | static void selectBiosDevice(void)␊ |
601 | {␊ |
602 | struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
603 | CacheReset();␊ |
604 | diskFreeMap(oldMap);␊ |
605 | oldMap = NULL;␊ |
606 | ␊ |
607 | int dev = selectAlternateBootDevice(gBIOSDev);␊ |
608 | ␊ |
609 | BVRef bvchain = scanBootVolumes(dev, 0);␊ |
610 | BVRef bootVol = selectBootVolume(bvchain);␊ |
611 | gBootVolume = bootVol;␊ |
612 | setRootVolume(bootVol);␊ |
613 | gBIOSDev = dev;␊ |
614 | }␊ |
615 | */␊ |
616 | ␊ |
617 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
618 | #define NMAX 5000␊ |
619 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
620 | ␊ |
621 | #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}␊ |
622 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);␊ |
623 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);␊ |
624 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);␊ |
625 | #define DO16(buf) DO8(buf,0); DO8(buf,8);␊ |
626 | ␊ |
627 | unsigned long Adler32(unsigned char *buf, long len)␊ |
628 | {␊ |
629 | unsigned long s1 = 1; // adler & 0xffff;␊ |
630 | unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
631 | unsigned long result;␊ |
632 | int k;␊ |
633 | ␊ |
634 | while (len > 0) {␊ |
635 | k = len < NMAX ? len : NMAX;␊ |
636 | len -= k;␊ |
637 | while (k >= 16) {␊ |
638 | DO16(buf);␊ |
639 | buf += 16;␊ |
640 | k -= 16;␊ |
641 | }␊ |
642 | if (k != 0) do {␊ |
643 | s1 += *buf++;␊ |
644 | s2 += s1;␊ |
645 | } while (--k);␊ |
646 | s1 %= BASE;␊ |
647 | s2 %= BASE;␊ |
648 | }␊ |
649 | result = (s2 << 16) | s1;␊ |
650 | return OSSwapHostToBigInt32(result);␊ |
651 | }␊ |
652 | |