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 | #include "edid.h"␊ |
62 | #include "915resolution.h"␊ |
63 | ␊ |
64 | long gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
65 | bool gOverrideKernel;␊ |
66 | static char gBootKernelCacheFile[512];␊ |
67 | static char gCacheNameAdler[64 + 256];␊ |
68 | char *gPlatformName = gCacheNameAdler;␊ |
69 | char gRootDevice[512];␊ |
70 | char gMKextName[512];␊ |
71 | char gMacOSVersion[8];␊ |
72 | bool gEnableCDROMRescan;␊ |
73 | bool gScanSingleDrive;␊ |
74 | ␊ |
75 | int bvCount = 0;␊ |
76 | //int␉␉menucount = 0;␊ |
77 | int gDeviceCount = 0; ␊ |
78 | ␊ |
79 | BVRef bvr;␊ |
80 | BVRef menuBVR;␊ |
81 | BVRef bvChain;␊ |
82 | bool useGUI;␊ |
83 | ␊ |
84 | //static void selectBiosDevice(void);␊ |
85 | static unsigned long Adler32(unsigned char *buffer, long length);␊ |
86 | ␊ |
87 | ␊ |
88 | static bool gUnloadPXEOnExit = false;␊ |
89 | ␊ |
90 | /*␊ |
91 | * How long to wait (in seconds) to load the␊ |
92 | * kernel after displaying the "boot:" prompt.␊ |
93 | */␊ |
94 | #define kBootErrorTimeout 5␊ |
95 | ␊ |
96 | /*␊ |
97 | * Default path to kernel cache file␊ |
98 | */␊ |
99 | #define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"␊ |
100 | ␊ |
101 | //==========================================================================␊ |
102 | // Zero the BSS.␊ |
103 | ␊ |
104 | static void zeroBSS(void)␊ |
105 | {␊ |
106 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
107 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
108 | ␊ |
109 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
110 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
111 | }␊ |
112 | ␊ |
113 | //==========================================================================␊ |
114 | // Malloc error function␊ |
115 | ␊ |
116 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
117 | {␊ |
118 | ␉stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);␊ |
119 | }␊ |
120 | ␊ |
121 | //==========================================================================␊ |
122 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
123 | //␊ |
124 | void initialize_runtime(void)␊ |
125 | {␊ |
126 | ␉zeroBSS();␊ |
127 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
128 | }␊ |
129 | ␊ |
130 | //==========================================================================␊ |
131 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
132 | ␊ |
133 | static int ExecKernel(void *binary)␊ |
134 | {␊ |
135 | entry_t kernelEntry;␊ |
136 | int ret;␊ |
137 | ␊ |
138 | bootArgs->kaddr = bootArgs->ksize = 0;␊ |
139 | ␊ |
140 | ret = DecodeKernel(binary,␊ |
141 | &kernelEntry,␊ |
142 | (char **) &bootArgs->kaddr,␊ |
143 | (int *)&bootArgs->ksize );␊ |
144 | ␊ |
145 | if ( ret != 0 )␊ |
146 | return ret;␊ |
147 | ␊ |
148 | // Reserve space for boot args␊ |
149 | reserveKernBootStruct();␊ |
150 | ␊ |
151 | // Load boot drivers from the specifed root path.␊ |
152 | ␊ |
153 | if (!gHaveKernelCache) {␊ |
154 | LoadDrivers("/");␊ |
155 | }␊ |
156 | ␊ |
157 | clearActivityIndicator();␊ |
158 | ␊ |
159 | if (gErrors) {␊ |
160 | printf("Errors encountered while starting up the computer.\n");␊ |
161 | printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
162 | sleep(kBootErrorTimeout);␊ |
163 | }␊ |
164 | ␊ |
165 | ␉md0Ramdisk();␊ |
166 | ␊ |
167 | setupFakeEfi();␊ |
168 | ␊ |
169 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
170 | ␊ |
171 | // Cleanup the PXE base code.␊ |
172 | ␊ |
173 | if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {␊ |
174 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
175 | {␊ |
176 | ␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
177 | sleep(2);␊ |
178 | }␊ |
179 | }␊ |
180 | ␊ |
181 | bool dummyVal;␊ |
182 | if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {␊ |
183 | ␉printf("Press any key to continue...");␊ |
184 | ␉getc();␊ |
185 | }␊ |
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 | // First get info for boot volume.␊ |
264 | scanBootVolumes(gBIOSDev, 0);␊ |
265 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
266 | setBootGlobals(bvChain);␊ |
267 | ␊ |
268 | // Load boot.plist config file␊ |
269 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
270 | ␊ |
271 | // Scan and record the system's hardware information.␊ |
272 | scan_platform();␊ |
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 | ␉␊ |
324 | useGUI = true;␊ |
325 | // Override useGUI default␊ |
326 | getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);␊ |
327 | ␊ |
328 | ␉// Before initGui, path the video bios with the correct resolution␊ |
329 | ␉␊ |
330 | ␉UInt32 x = 0, y = 0; ␊ |
331 | ␉UInt32 bp = 0;␊ |
332 | ␉␊ |
333 | ␉getResolution(&x, &y, &bp);␉␊ |
334 | ␊ |
335 | ␉if (x!=0 && y!=0) {␊ |
336 | ␉␉vbios_map * map;␊ |
337 | ␉␉␊ |
338 | ␉␉map = open_vbios(CT_UNKWN);␊ |
339 | ␉␉␊ |
340 | ␉␉unlock_vbios(map);␊ |
341 | ␉␉␊ |
342 | ␉␉set_mode(map, x, y, bp, 0, 0);␊ |
343 | ␉␉␊ |
344 | ␉␉relock_vbios(map);␊ |
345 | ␉␉␊ |
346 | ␉␉close_vbios(map);␊ |
347 | ␉␉␊ |
348 | ␉␉verbose("Patched first resolution mode to %dx%d.\n", x, y);␊ |
349 | ␉}␊ |
350 | ␉␊ |
351 | if (useGUI) {␊ |
352 | /* XXX AsereBLN handle error */␊ |
353 | ␉initGUI();␊ |
354 | }␊ |
355 | ␊ |
356 | setBootGlobals(bvChain);␊ |
357 | ␊ |
358 | // Parse args, load and start kernel.␊ |
359 | while (1) {␊ |
360 | const char *val;␊ |
361 | int len;␊ |
362 | int trycache;␊ |
363 | long flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
364 | int ret = -1;␊ |
365 | void *binary = (void *)kLoadAddr;␊ |
366 | bool tryresume;␊ |
367 | bool tryresumedefault;␊ |
368 | bool forceresume;␊ |
369 | ␊ |
370 | config_file_t systemVersion;␉␉// system.plist of booting partition␊ |
371 | ␊ |
372 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
373 | char bootFileSpec[512];␊ |
374 | ␉␉␊ |
375 | // Initialize globals.␊ |
376 | ␊ |
377 | sysConfigValid = false;␊ |
378 | gErrors = false;␊ |
379 | ␊ |
380 | status = getBootOptions(firstRun);␊ |
381 | firstRun = false;␊ |
382 | if (status == -1) continue;␊ |
383 | ␉␉ ␊ |
384 | status = processBootOptions();␊ |
385 | // Status==1 means to chainboot␊ |
386 | if ( status == 1 ) break;␊ |
387 | // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
388 | if ( status == -1 )␊ |
389 | {␊ |
390 | // gBootVolume == NULL usually means the user hit escape.␊ |
391 | if(gBootVolume == NULL)␊ |
392 | {␊ |
393 | freeFilteredBVChain(bvChain);␊ |
394 | ␊ |
395 | if (gEnableCDROMRescan)␊ |
396 | rescanBIOSDevice(gBIOSDev);␊ |
397 | ␊ |
398 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
399 | setBootGlobals(bvChain);␊ |
400 | }␊ |
401 | continue;␊ |
402 | }␊ |
403 | ␉␉␊ |
404 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
405 | ␉␉␊ |
406 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
407 | ␉␉␉drawBackground();␊ |
408 | ␉␉␉␊ |
409 | // Found and loaded a config file. Proceed with boot.␊ |
410 | ␊ |
411 | ␉␉// Turn off any GUI elements␊ |
412 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
413 | ␉␉{␊ |
414 | ␉␉␉gui.devicelist.draw = false;␊ |
415 | ␉␉␉gui.bootprompt.draw = false;␊ |
416 | ␉␉␉gui.menu.draw = false;␊ |
417 | ␉␉␉gui.infobox.draw = false;␊ |
418 | ␉␉␉drawBackground();␊ |
419 | ␉␉␉updateVRAM();␊ |
420 | ␉␉}␊ |
421 | ␉␉␊ |
422 | ␉␉// Find out which version mac os we're booting.␊ |
423 | ␉␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion)) {␊ |
424 | ␉␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion)) {␉␊ |
425 | ␉␉␉␉// getValueForKey uses const char for val␊ |
426 | ␉␉␉␉// so copy it and trim␊ |
427 | ␉␉␉␉strncpy(gMacOSVersion, val, MIN(len, 4));␊ |
428 | ␉␉␉␉gMacOSVersion[MIN(len, 4)] = '\0';␊ |
429 | ␉␉␉}␊ |
430 | ␉␉}␊ |
431 | ␊ |
432 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T)) {␊ |
433 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
434 | ␉␉} else {␊ |
435 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
436 | ␉␉}␊ |
437 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {␊ |
438 | ␉␉␉if (strncmp(val, "i386", 4) == 0) {␊ |
439 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
440 | ␉␉␉}␊ |
441 | ␉␉}␊ |
442 | ␉␉if (getValueForKey(k32BitModeFlag, &val, &len, &bootInfo->bootConfig)) {␊ |
443 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
444 | ␉␉}␊ |
445 | ␉␉if (getValueForKey(k64BitModeFlag, &val, &len, &bootInfo->bootConfig)) {␊ |
446 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
447 | ␉␉}␊ |
448 | ␉␉␊ |
449 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {␊ |
450 | ␉␉␉tryresume = true;␊ |
451 | ␉␉␉tryresumedefault = true;␊ |
452 | ␉␉} else {␊ |
453 | ␉␉␉tryresumedefault = false;␊ |
454 | ␉␉}␊ |
455 | ␊ |
456 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {␊ |
457 | ␉␉␉forceresume = false;␊ |
458 | ␉␉}␊ |
459 | ␉␉␊ |
460 | ␉␉if (forceresume) {␊ |
461 | ␉␉␉tryresume = true;␊ |
462 | ␉␉␉tryresumedefault = false;␊ |
463 | ␉␉}␊ |
464 | ␉␉␊ |
465 | ␉␉while (tryresume) {␊ |
466 | ␉␉␉const char *tmp;␊ |
467 | ␉␉␉BVRef bvr;␊ |
468 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))␊ |
469 | ␉␉␉␉val="/private/var/vm/sleepimage";␊ |
470 | ␉␉␉␊ |
471 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
472 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
473 | ␊ |
474 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
475 | ␉␉␉␉break;␊ |
476 | ␉␉␉␊ |
477 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
478 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
479 | ␉␉␉␉break;␊ |
480 | ␉␉␉␊ |
481 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
482 | ␉␉␉␉break;␊ |
483 | ␉␉␉␊ |
484 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
485 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);␊ |
486 | ␉␉␉␉break;␊ |
487 | ␉␉␉}␊ |
488 | ␉␉␉␉␊ |
489 | ␉␉␉HibernateBoot((char *)val);␊ |
490 | ␉␉␉break;␊ |
491 | ␉␉}␊ |
492 | ␊ |
493 | // Reset cache name.␊ |
494 | bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
495 | ␊ |
496 | sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
497 | ␊ |
498 | adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
499 | ␊ |
500 | if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
501 | strlcpy(gBootKernelCacheFile, val, len+1);␊ |
502 | } else {␊ |
503 | sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);␊ |
504 | }␊ |
505 | ␊ |
506 | // Check for cache file.␊ |
507 | trycache = (((gBootMode & kBootModeSafe) == 0) &&␊ |
508 | !gOverrideKernel &&␊ |
509 | (gBootFileType == kBlockDeviceType) &&␊ |
510 | (gMKextName[0] == '\0') &&␊ |
511 | (gBootKernelCacheFile[0] != '\0'));␊ |
512 | ␊ |
513 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
514 | ␉␉␊ |
515 | if (trycache) do {␊ |
516 | ␊ |
517 | // if we haven't found the kernel yet, don't use the cache␊ |
518 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
519 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
520 | trycache = 0;␊ |
521 | break;␊ |
522 | }␊ |
523 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
524 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
525 | || (cachetime < kerneltime)) {␊ |
526 | trycache = 0;␊ |
527 | break;␊ |
528 | }␊ |
529 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
530 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
531 | && (cachetime < exttime)) {␊ |
532 | trycache = 0;␊ |
533 | break;␊ |
534 | }␊ |
535 | if (kerneltime > exttime) {␊ |
536 | exttime = kerneltime;␊ |
537 | }␊ |
538 | if (cachetime != (exttime + 1)) {␊ |
539 | trycache = 0;␊ |
540 | break;␊ |
541 | }␊ |
542 | } while (0);␊ |
543 | ␊ |
544 | do {␊ |
545 | if (trycache) {␊ |
546 | bootFile = gBootKernelCacheFile;␊ |
547 | verbose("Loading kernel cache %s\n", bootFile);␊ |
548 | ret = LoadFile(bootFile);␊ |
549 | binary = (void *)kLoadAddr;␊ |
550 | if (ret >= 0) {␊ |
551 | break;␊ |
552 | }␊ |
553 | }␊ |
554 | bootFile = bootInfo->bootFile;␊ |
555 | ␊ |
556 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
557 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
558 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
559 | ␉ ␉ if (ret == -1)␊ |
560 | ␉ ␉ {␊ |
561 | sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
562 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
563 | if (ret == -1)␊ |
564 | {␊ |
565 | sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
566 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
567 | if (ret == -1)␊ |
568 | {␊ |
569 | // Not found any alternate locations, using the original kernel image path.␊ |
570 | strcpy(bootFileSpec, bootFile);␊ |
571 | }␊ |
572 | }␊ |
573 | }␊ |
574 | ␉␉␉␊ |
575 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
576 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
577 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
578 | {␊ |
579 | archCpuType = CPU_TYPE_I386;␊ |
580 | ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
581 | }␊ |
582 | ␉␉␉␊ |
583 | } while (0);␊ |
584 | ␊ |
585 | clearActivityIndicator();␊ |
586 | #if DEBUG␊ |
587 | printf("Pausing...");␊ |
588 | sleep(8);␊ |
589 | #endif␊ |
590 | ␊ |
591 | if (ret <= 0) {␊ |
592 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
593 | ␊ |
594 | ␉␉␉if(gui.initialised) {␊ |
595 | ␉␉␉␉sleep(1);␊ |
596 | ␉␉␉␉drawBackground();␊ |
597 | ␉␉␉␉gui.devicelist.draw = true;␊ |
598 | ␉␉␉␉gui.redraw = true;␊ |
599 | ␉␉␉}␊ |
600 | if (gBootFileType == kNetworkDeviceType) {␊ |
601 | // Return control back to PXE. Don't unload PXE base code.␊ |
602 | gUnloadPXEOnExit = false;␊ |
603 | break;␊ |
604 | }␊ |
605 | } else {␊ |
606 | /* Won't return if successful. */␊ |
607 | ret = ExecKernel(binary);␊ |
608 | }␊ |
609 | }␊ |
610 | ␊ |
611 | // chainboot␊ |
612 | if (status==1) {␊ |
613 | ␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
614 | ␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
615 | ␉}␊ |
616 | }␊ |
617 | ␉␊ |
618 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
619 | ␉nbpUnloadBaseCode();␊ |
620 | }␊ |
621 | }␊ |
622 | ␊ |
623 | /*!␊ |
624 | Selects a new BIOS device, taking care to update the global state appropriately.␊ |
625 | */␊ |
626 | /*␊ |
627 | static void selectBiosDevice(void)␊ |
628 | {␊ |
629 | struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
630 | CacheReset();␊ |
631 | diskFreeMap(oldMap);␊ |
632 | oldMap = NULL;␊ |
633 | ␊ |
634 | int dev = selectAlternateBootDevice(gBIOSDev);␊ |
635 | ␊ |
636 | BVRef bvchain = scanBootVolumes(dev, 0);␊ |
637 | BVRef bootVol = selectBootVolume(bvchain);␊ |
638 | gBootVolume = bootVol;␊ |
639 | setRootVolume(bootVol);␊ |
640 | gBIOSDev = dev;␊ |
641 | }␊ |
642 | */␊ |
643 | ␊ |
644 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
645 | #define NMAX 5000␊ |
646 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
647 | ␊ |
648 | #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}␊ |
649 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);␊ |
650 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);␊ |
651 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);␊ |
652 | #define DO16(buf) DO8(buf,0); DO8(buf,8);␊ |
653 | ␊ |
654 | unsigned long Adler32(unsigned char *buf, long len)␊ |
655 | {␊ |
656 | unsigned long s1 = 1; // adler & 0xffff;␊ |
657 | unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
658 | unsigned long result;␊ |
659 | int k;␊ |
660 | ␊ |
661 | while (len > 0) {␊ |
662 | k = len < NMAX ? len : NMAX;␊ |
663 | len -= k;␊ |
664 | while (k >= 16) {␊ |
665 | DO16(buf);␊ |
666 | buf += 16;␊ |
667 | k -= 16;␊ |
668 | }␊ |
669 | if (k != 0) do {␊ |
670 | s1 += *buf++;␊ |
671 | s2 += s1;␊ |
672 | } while (--k);␊ |
673 | s1 %= BASE;␊ |
674 | s2 %= BASE;␊ |
675 | }␊ |
676 | result = (s2 << 16) | s1;␊ |
677 | return OSSwapHostToBigInt32(result);␊ |
678 | }␊ |
679 | |