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; disabled␊ |
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 | ␊ |
181 | ␉//Azi: Wait=y is breaking other keys when typed "after them" at boot prompt.␊ |
182 | ␉// Works properly if typed in first place or used on Boot.plist.␊ |
183 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {␊ |
184 | ␉␉printf("Press any key to continue...");␊ |
185 | ␉␉getc();␊ |
186 | ␉}␊ |
187 | ␊ |
188 | ␉usb_loop();␊ |
189 | ␊ |
190 | // If we were in text mode, switch to graphics mode.␊ |
191 | // This will draw the boot graphics unless we are in␊ |
192 | // verbose mode.␊ |
193 | ␊ |
194 | if(gVerboseMode)␊ |
195 | setVideoMode( GRAPHICS_MODE, 0 );␊ |
196 | else␊ |
197 | drawBootGraphics();␊ |
198 | ␉␊ |
199 | finalizeBootStruct();␊ |
200 | ␊ |
201 | // Jump to kernel's entry point. There's no going back now.␊ |
202 | ␊ |
203 | startprog( kernelEntry, bootArgs );␊ |
204 | ␊ |
205 | // Not reached␊ |
206 | ␊ |
207 | return 0;␊ |
208 | }␊ |
209 | ␊ |
210 | //==========================================================================␊ |
211 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
212 | // before. Multiboot does its own runtime initialization, does some of its␊ |
213 | // own things, and then calls common_boot.␊ |
214 | void boot(int biosdev)␊ |
215 | {␊ |
216 | ␉initialize_runtime();␊ |
217 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
218 | ␉enableA20();␊ |
219 | ␉common_boot(biosdev);␊ |
220 | }␊ |
221 | ␊ |
222 | //==========================================================================␊ |
223 | // The 'main' function for the booter. Called by boot0 when booting␊ |
224 | // from a block device, or by the network booter.␊ |
225 | //␊ |
226 | // arguments:␊ |
227 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
228 | // that the booter was loaded from.␊ |
229 | //␊ |
230 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
231 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
232 | // next boot device on its list.␊ |
233 | void common_boot(int biosdev)␊ |
234 | {␊ |
235 | int status;␊ |
236 | char *bootFile;␊ |
237 | unsigned long adler32;␊ |
238 | bool quiet;␊ |
239 | bool firstRun = true;␊ |
240 | bool instantMenu;␊ |
241 | bool rescanPrompt;␊ |
242 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
243 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
244 | ␊ |
245 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
246 | // the base code will result in a hang or kernel panic.␊ |
247 | gUnloadPXEOnExit = true;␊ |
248 | ␊ |
249 | // Record the device that the booter was loaded from.␊ |
250 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
251 | ␊ |
252 | // Initialize boot info structure.␊ |
253 | initKernBootStruct();␊ |
254 | ␊ |
255 | // Setup VGA text mode.␊ |
256 | // Not sure if it is safe to call setVideoMode() before the␊ |
257 | // config table has been loaded. Call video_mode() instead.␊ |
258 | #if DEBUG␊ |
259 | printf("before video_mode\n");␊ |
260 | #endif␊ |
261 | video_mode( 2 ); // 80x25 mono text mode.␊ |
262 | #if DEBUG␊ |
263 | printf("after video_mode\n");␊ |
264 | #endif␊ |
265 | ␊ |
266 | // Scan and record the system's hardware information.␊ |
267 | scan_platform();␊ |
268 | ␊ |
269 | // First get info for boot volume.␊ |
270 | scanBootVolumes(gBIOSDev, 0);␊ |
271 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
272 | setBootGlobals(bvChain);␊ |
273 | ␊ |
274 | // Load boot.plist config file␊ |
275 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
276 | ␊ |
277 | if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {␊ |
278 | gBootMode |= kBootModeQuiet;␊ |
279 | }␊ |
280 | ␊ |
281 | // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
282 | if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {␊ |
283 | firstRun = false;␊ |
284 | }␊ |
285 | ␊ |
286 | // Loading preboot ramdisk if exists.␊ |
287 | loadPrebootRAMDisk();␊ |
288 | ␊ |
289 | // Disable rescan option by default␊ |
290 | gEnableCDROMRescan = false;␊ |
291 | ␊ |
292 | // Enable it with Rescan=y in system config␊ |
293 | if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {␊ |
294 | gEnableCDROMRescan = true;␊ |
295 | }␊ |
296 | ␊ |
297 | // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
298 | rescanPrompt = false;␊ |
299 | if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {␊ |
300 | gEnableCDROMRescan = promptForRescanOption();␊ |
301 | }␊ |
302 | ␊ |
303 | // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
304 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {␊ |
305 | gScanSingleDrive = true;␊ |
306 | }␊ |
307 | ␊ |
308 | // Create a list of partitions on device(s).␊ |
309 | if (gScanSingleDrive) {␊ |
310 | scanBootVolumes(gBIOSDev, &bvCount);␊ |
311 | } else {␊ |
312 | scanDisks(gBIOSDev, &bvCount);␊ |
313 | }␊ |
314 | ␊ |
315 | // Create a separated bvr chain using the specified filters.␊ |
316 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
317 | ␊ |
318 | gBootVolume = selectBootVolume(bvChain);␊ |
319 | ␊ |
320 | #if DEBUG␊ |
321 | printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
322 | printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
323 | getc();␊ |
324 | #endif␊ |
325 | ␊ |
326 | useGUI = true;␊ |
327 | // Override useGUI default␊ |
328 | getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);␊ |
329 | if (useGUI && initGUI())␊ |
330 | ␉{␊ |
331 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
332 | ␉␉useGUI = false;␊ |
333 | ␉}␊ |
334 | ␊ |
335 | setBootGlobals(bvChain);␊ |
336 | ␊ |
337 | // Parse args, load and start kernel.␊ |
338 | while (1) {␊ |
339 | const char *val;␊ |
340 | int len;␊ |
341 | int trycache;␊ |
342 | long flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
343 | int ret = -1;␊ |
344 | void *binary = (void *)kLoadAddr;␊ |
345 | bool tryresume;␊ |
346 | bool tryresumedefault;␊ |
347 | bool forceresume;␊ |
348 | ␊ |
349 | config_file_t systemVersion;␉␉// system.plist of booting partition␊ |
350 | ␊ |
351 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
352 | char bootFileSpec[512];␊ |
353 | ␉␉␊ |
354 | // Initialize globals.␊ |
355 | ␊ |
356 | sysConfigValid = false;␊ |
357 | gErrors = false;␊ |
358 | ␊ |
359 | status = getBootOptions(firstRun);␊ |
360 | firstRun = false;␊ |
361 | if (status == -1) continue;␊ |
362 | ␉␉ ␊ |
363 | status = processBootOptions();␊ |
364 | // Status==1 means to chainboot␊ |
365 | if ( status == 1 ) break;␊ |
366 | // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
367 | if ( status == -1 )␊ |
368 | {␊ |
369 | // gBootVolume == NULL usually means the user hit escape.␊ |
370 | if(gBootVolume == NULL)␊ |
371 | {␊ |
372 | freeFilteredBVChain(bvChain);␊ |
373 | ␊ |
374 | if (gEnableCDROMRescan)␊ |
375 | rescanBIOSDevice(gBIOSDev);␊ |
376 | ␊ |
377 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
378 | setBootGlobals(bvChain);␊ |
379 | }␊ |
380 | continue;␊ |
381 | }␊ |
382 | ␉␉␊ |
383 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
384 | ␉␉␊ |
385 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
386 | ␉␉␉drawBackground();␊ |
387 | ␉␉␉␊ |
388 | // Found and loaded a config file. Proceed with boot.␊ |
389 | ␊ |
390 | ␉␉// Turn off any GUI elements␊ |
391 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
392 | ␉␉{␊ |
393 | ␉␉␉gui.devicelist.draw = false;␊ |
394 | ␉␉␉gui.bootprompt.draw = false;␊ |
395 | ␉␉␉gui.menu.draw = false;␊ |
396 | ␉␉␉gui.infobox.draw = false;␊ |
397 | ␉␉␉drawBackground();␊ |
398 | ␉␉␉updateVRAM();␊ |
399 | ␉␉}␊ |
400 | ␉␉␊ |
401 | ␉␉// Find out which version mac os we're booting.␊ |
402 | ␉␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion)) {␊ |
403 | ␉␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion)) {␉␊ |
404 | ␉␉␉␉// getValueForKey uses const char for val␊ |
405 | ␉␉␉␉// so copy it and trim␊ |
406 | ␉␉␉␉strncpy(gMacOSVersion, val, MIN(len, 4));␊ |
407 | ␉␉␉␉gMacOSVersion[MIN(len, 4)] = '\0';␊ |
408 | ␉␉␉}␊ |
409 | ␉␉}␊ |
410 | ␊ |
411 | ␉␉// If cpu doesn't handle 64 bit instructions,...␊ |
412 | ␉␉if (!platformCPUFeature(CPU_FEATURE_EM64T) ||␊ |
413 | ␉␉␉// ... user forced i386 kernel architecture on cpu with "em64t"...␊ |
414 | ␉␉␉getValueForKey(kArchI386Flag, &val, &len, &bootInfo->bootConfig) ||␊ |
415 | ␉␉␉// ... or forced Legacy Mode...␊ |
416 | ␉␉␉getValueForKey(kLegacyModeFlag, &val, &len, &bootInfo->bootConfig))␊ |
417 | ␉␉{␊ |
418 | ␉␉␉// ... use i386 kernel arch.␊ |
419 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
420 | ␉␉}␊ |
421 | ␉␉else␊ |
422 | ␉␉{␊ |
423 | ␉␉␉// Else use x86_64 kernel arch.␊ |
424 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
425 | ␉␉}␊ |
426 | ␉␉// Override i386/-legacy, if flagged on Boot.plist.␊ |
427 | ␉␉if (getValueForKey(kArchX86_64Flag, &val, &len, &bootInfo->bootConfig))␊ |
428 | ␉␉{␊ |
429 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
430 | ␉␉}␊ |
431 | ␉␉␊ |
432 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {␊ |
433 | ␉␉␉tryresume = true;␊ |
434 | ␉␉␉tryresumedefault = true;␊ |
435 | ␉␉} else {␊ |
436 | ␉␉␉tryresumedefault = false;␊ |
437 | ␉␉}␊ |
438 | ␊ |
439 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {␊ |
440 | ␉␉␉forceresume = false;␊ |
441 | ␉␉}␊ |
442 | ␉␉␊ |
443 | ␉␉if (forceresume) {␊ |
444 | ␉␉␉tryresume = true;␊ |
445 | ␉␉␉tryresumedefault = false;␊ |
446 | ␉␉}␊ |
447 | ␉␉␊ |
448 | ␉␉while (tryresume) {␊ |
449 | ␉␉␉const char *tmp;␊ |
450 | ␉␉␉BVRef bvr;␊ |
451 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))␊ |
452 | ␉␉␉␉val="/private/var/vm/sleepimage";␊ |
453 | ␉␉␉␊ |
454 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
455 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
456 | ␊ |
457 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
458 | ␉␉␉␉break;␊ |
459 | ␉␉␉␊ |
460 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
461 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
462 | ␉␉␉␉break;␊ |
463 | ␉␉␉␊ |
464 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
465 | ␉␉␉␉break;␊ |
466 | ␉␉␉␊ |
467 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
468 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);␊ |
469 | ␉␉␉␉break;␊ |
470 | ␉␉␉}␊ |
471 | ␉␉␉␉␊ |
472 | ␉␉␉HibernateBoot((char *)val);␊ |
473 | ␉␉␉break;␊ |
474 | ␉␉}␊ |
475 | ␊ |
476 | // Reset cache name.␊ |
477 | bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
478 | ␊ |
479 | sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
480 | ␊ |
481 | adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
482 | ␊ |
483 | if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
484 | strlcpy(gBootKernelCacheFile, val, len+1);␊ |
485 | } else {␊ |
486 | sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);␊ |
487 | }␊ |
488 | ␊ |
489 | // Check for cache file.␊ |
490 | trycache = (((gBootMode & kBootModeSafe) == 0) &&␊ |
491 | !gOverrideKernel &&␊ |
492 | (gBootFileType == kBlockDeviceType) &&␊ |
493 | (gMKextName[0] == '\0') &&␊ |
494 | (gBootKernelCacheFile[0] != '\0'));␊ |
495 | ␊ |
496 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
497 | ␉␉␊ |
498 | if (trycache) do {␊ |
499 | ␊ |
500 | // if we haven't found the kernel yet, don't use the cache␊ |
501 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
502 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
503 | trycache = 0;␊ |
504 | break;␊ |
505 | }␊ |
506 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
507 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
508 | || (cachetime < kerneltime)) {␊ |
509 | trycache = 0;␊ |
510 | break;␊ |
511 | }␊ |
512 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
513 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
514 | && (cachetime < exttime)) {␊ |
515 | trycache = 0;␊ |
516 | break;␊ |
517 | }␊ |
518 | if (kerneltime > exttime) {␊ |
519 | exttime = kerneltime;␊ |
520 | }␊ |
521 | if (cachetime != (exttime + 1)) {␊ |
522 | trycache = 0;␊ |
523 | break;␊ |
524 | }␊ |
525 | } while (0);␊ |
526 | ␊ |
527 | do {␊ |
528 | if (trycache) {␊ |
529 | bootFile = gBootKernelCacheFile;␊ |
530 | verbose("Loading kernel cache %s\n", bootFile);␊ |
531 | ret = LoadFile(bootFile);␊ |
532 | binary = (void *)kLoadAddr;␊ |
533 | if (ret >= 0) {␊ |
534 | break;␊ |
535 | }␊ |
536 | }␊ |
537 | bootFile = bootInfo->bootFile;␊ |
538 | ␊ |
539 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
540 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
541 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
542 | ␉ ␉ if (ret == -1)␊ |
543 | ␉ ␉ {␊ |
544 | sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
545 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
546 | if (ret == -1)␊ |
547 | {␊ |
548 | sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
549 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
550 | if (ret == -1)␊ |
551 | {␊ |
552 | // Not found any alternate locations, using the original kernel image path.␊ |
553 | strcpy(bootFileSpec, bootFile);␊ |
554 | }␊ |
555 | }␊ |
556 | }␊ |
557 | ␉␉␉␊ |
558 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
559 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
560 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
561 | {␊ |
562 | archCpuType = CPU_TYPE_I386;␊ |
563 | ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
564 | }␊ |
565 | ␉␉␉␊ |
566 | } while (0);␊ |
567 | ␊ |
568 | clearActivityIndicator();␊ |
569 | #if DEBUG␊ |
570 | printf("Pausing...");␊ |
571 | sleep(8);␊ |
572 | #endif␊ |
573 | ␊ |
574 | if (ret <= 0) {␊ |
575 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
576 | ␊ |
577 | ␉␉␉if(gui.initialised) {␊ |
578 | ␉␉␉␉sleep(1);␊ |
579 | ␉␉␉␉drawBackground();␊ |
580 | ␉␉␉␉gui.devicelist.draw = true;␊ |
581 | ␉␉␉␉gui.redraw = true;␊ |
582 | ␉␉␉}␊ |
583 | if (gBootFileType == kNetworkDeviceType) {␊ |
584 | // Return control back to PXE. Don't unload PXE base code.␊ |
585 | gUnloadPXEOnExit = false;␊ |
586 | break;␊ |
587 | }␊ |
588 | } else {␊ |
589 | /* Won't return if successful. */␊ |
590 | ret = ExecKernel(binary);␊ |
591 | }␊ |
592 | }␊ |
593 | ␊ |
594 | // chainboot␊ |
595 | if (status==1) {␊ |
596 | ␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
597 | ␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
598 | ␉}␊ |
599 | }␊ |
600 | ␉␊ |
601 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
602 | ␉nbpUnloadBaseCode();␊ |
603 | }␊ |
604 | }␊ |
605 | ␊ |
606 | /*!␊ |
607 | Selects a new BIOS device, taking care to update the global state appropriately.␊ |
608 | */␊ |
609 | /*␊ |
610 | static void selectBiosDevice(void)␊ |
611 | {␊ |
612 | struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
613 | CacheReset();␊ |
614 | diskFreeMap(oldMap);␊ |
615 | oldMap = NULL;␊ |
616 | ␊ |
617 | int dev = selectAlternateBootDevice(gBIOSDev);␊ |
618 | ␊ |
619 | BVRef bvchain = scanBootVolumes(dev, 0);␊ |
620 | BVRef bootVol = selectBootVolume(bvchain);␊ |
621 | gBootVolume = bootVol;␊ |
622 | setRootVolume(bootVol);␊ |
623 | gBIOSDev = dev;␊ |
624 | }␊ |
625 | */␊ |
626 | ␊ |
627 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
628 | #define NMAX 5000␊ |
629 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
630 | ␊ |
631 | #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}␊ |
632 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);␊ |
633 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);␊ |
634 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);␊ |
635 | #define DO16(buf) DO8(buf,0); DO8(buf,8);␊ |
636 | ␊ |
637 | unsigned long Adler32(unsigned char *buf, long len)␊ |
638 | {␊ |
639 | unsigned long s1 = 1; // adler & 0xffff;␊ |
640 | unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
641 | unsigned long result;␊ |
642 | int k;␊ |
643 | ␊ |
644 | while (len > 0) {␊ |
645 | k = len < NMAX ? len : NMAX;␊ |
646 | len -= k;␊ |
647 | while (k >= 16) {␊ |
648 | DO16(buf);␊ |
649 | buf += 16;␊ |
650 | k -= 16;␊ |
651 | }␊ |
652 | if (k != 0) do {␊ |
653 | s1 += *buf++;␊ |
654 | s2 += s1;␊ |
655 | } while (--k);␊ |
656 | s1 %= BASE;␊ |
657 | s2 %= BASE;␊ |
658 | }␊ |
659 | result = (s2 << 16) | s1;␊ |
660 | return OSSwapHostToBigInt32(result);␊ |
661 | }␊ |
662 | |