Root/
Source at commit 129 created 14 years 1 month ago. By diebuche, Patching my branch. Added typo fixes. | |
---|---|
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 | bool autoResolution;␊ |
84 | ␊ |
85 | //static void selectBiosDevice(void);␊ |
86 | static unsigned long Adler32(unsigned char *buffer, long length);␊ |
87 | ␊ |
88 | ␊ |
89 | static bool gUnloadPXEOnExit = false;␊ |
90 | ␊ |
91 | /*␊ |
92 | * How long to wait (in seconds) to load the␊ |
93 | * kernel after displaying the "boot:" prompt.␊ |
94 | */␊ |
95 | #define kBootErrorTimeout 5␊ |
96 | ␊ |
97 | /*␊ |
98 | * Default path to kernel cache file␊ |
99 | */␊ |
100 | #define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"␊ |
101 | ␊ |
102 | //==========================================================================␊ |
103 | // Zero the BSS.␊ |
104 | ␊ |
105 | static void zeroBSS(void)␊ |
106 | {␊ |
107 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
108 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
109 | ␊ |
110 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
111 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
112 | }␊ |
113 | ␊ |
114 | //==========================================================================␊ |
115 | // Malloc error function␊ |
116 | ␊ |
117 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
118 | {␊ |
119 | ␉stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);␊ |
120 | }␊ |
121 | ␊ |
122 | //==========================================================================␊ |
123 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
124 | //␊ |
125 | void initialize_runtime(void)␊ |
126 | {␊ |
127 | ␉zeroBSS();␊ |
128 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
129 | }␊ |
130 | ␊ |
131 | //==========================================================================␊ |
132 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
133 | ␊ |
134 | static int ExecKernel(void *binary, vbios_map *map)␊ |
135 | {␊ |
136 | entry_t kernelEntry;␊ |
137 | int ret;␊ |
138 | ␉␊ |
139 | ␉UInt32 params[4];␊ |
140 | ␉int count;␊ |
141 | ␉params[3] = 0;␊ |
142 | ␊ |
143 | bootArgs->kaddr = bootArgs->ksize = 0;␊ |
144 | ␊ |
145 | ret = DecodeKernel(binary,␊ |
146 | &kernelEntry,␊ |
147 | (char **) &bootArgs->kaddr,␊ |
148 | (int *)&bootArgs->ksize );␊ |
149 | ␊ |
150 | if ( ret != 0 )␊ |
151 | return ret;␊ |
152 | ␊ |
153 | // Reserve space for boot args␊ |
154 | reserveKernBootStruct();␊ |
155 | ␊ |
156 | // Load boot drivers from the specifed root path.␊ |
157 | ␊ |
158 | if (!gHaveKernelCache) {␊ |
159 | LoadDrivers("/");␊ |
160 | }␊ |
161 | ␊ |
162 | clearActivityIndicator();␊ |
163 | ␊ |
164 | if (gErrors) {␊ |
165 | printf("Errors encountered while starting up the computer.\n");␊ |
166 | printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
167 | sleep(kBootErrorTimeout);␊ |
168 | }␉␉␊ |
169 | ␉␊ |
170 | ␉//if the vbios patch have been applied retore it before performing fake efi stuff␊ |
171 | ␉if (autoResolution == TRUE) {␊ |
172 | ␉␉unlock_vbios(map);␊ |
173 | ␉␉restore_vbios(map);␊ |
174 | ␉␉relock_vbios(map);␊ |
175 | ␉}␉␊ |
176 | ␊ |
177 | setupFakeEfi();␊ |
178 | ␉␊ |
179 | ␉//Reapply the vbios patch before setting mode for boot graphics␊ |
180 | ␉if (autoResolution == TRUE) {␊ |
181 | ␉␉count = getNumberArrayFromProperty(kGraphicsModeKey, params, 4);␊ |
182 | ␉␉if ( count < 3 ) {␊ |
183 | ␉␉␉getResolution(¶ms[0], ¶ms[1], ¶ms[2]);␊ |
184 | ␉␉}␊ |
185 | ␉␉else ␊ |
186 | ␉␉{␊ |
187 | ␉␉␉if ( params[2] == 256 ) params[2] = 8;␊ |
188 | ␉␉␉if ( params[2] == 555 ) params[2] = 16;␊ |
189 | ␉␉␉if ( params[2] == 888 ) params[2] = 32;␊ |
190 | ␉␉}␊ |
191 | ␉␉␊ |
192 | ␉␉if (params[0]!=0 && params[1]!=0) {␊ |
193 | ␉␉␉␊ |
194 | ␉␉␉unlock_vbios(map);␊ |
195 | ␉␉␉␊ |
196 | ␉␉␉set_mode(map, params[0], params[1], params[2], 0, 0);␊ |
197 | ␉␉␉␊ |
198 | ␉␉␉relock_vbios(map);␊ |
199 | ␉␉}␊ |
200 | ␉}␊ |
201 | ␊ |
202 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
203 | ␊ |
204 | // Cleanup the PXE base code.␊ |
205 | ␊ |
206 | if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {␊ |
207 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
208 | {␊ |
209 | ␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
210 | sleep(2);␊ |
211 | }␊ |
212 | }␊ |
213 | ␊ |
214 | bool dummyVal;␊ |
215 | if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {␊ |
216 | ␉printf("Press any key to continue...");␊ |
217 | ␉getc();␊ |
218 | }␊ |
219 | ␊ |
220 | // If we were in text mode, switch to graphics mode.␊ |
221 | // This will draw the boot graphics unless we are in␊ |
222 | // verbose mode.␊ |
223 | ␊ |
224 | if(gVerboseMode)␊ |
225 | setVideoMode( GRAPHICS_MODE, 0 );␊ |
226 | else␊ |
227 | drawBootGraphics();␊ |
228 | ␉␊ |
229 | ␉//Boot Graphis are set finalize and free vbios patch structures␊ |
230 | ␉if (autoResolution == TRUE) {␊ |
231 | ␉␉unlock_vbios(map);␊ |
232 | ␉␉restore_vbios(map);␊ |
233 | ␉␉relock_vbios(map);␊ |
234 | ␉␉close_vbios(map);␊ |
235 | ␉}␊ |
236 | ␉␊ |
237 | finalizeBootStruct();␊ |
238 | ␊ |
239 | // Jump to kernel's entry point. There's no going back now.␊ |
240 | ␊ |
241 | startprog( kernelEntry, bootArgs );␊ |
242 | ␊ |
243 | // Not reached␊ |
244 | ␊ |
245 | return 0;␊ |
246 | }␊ |
247 | ␊ |
248 | //==========================================================================␊ |
249 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
250 | // before. Multiboot does its own runtime initialization, does some of its␊ |
251 | // own things, and then calls common_boot.␊ |
252 | void boot(int biosdev)␊ |
253 | {␊ |
254 | ␉initialize_runtime();␊ |
255 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
256 | ␉enableA20();␊ |
257 | ␉common_boot(biosdev);␊ |
258 | }␊ |
259 | ␊ |
260 | //==========================================================================␊ |
261 | // The 'main' function for the booter. Called by boot0 when booting␊ |
262 | // from a block device, or by the network booter.␊ |
263 | //␊ |
264 | // arguments:␊ |
265 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
266 | // that the booter was loaded from.␊ |
267 | //␊ |
268 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
269 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
270 | // next boot device on its list.␊ |
271 | void common_boot(int biosdev)␊ |
272 | {␊ |
273 | int status;␊ |
274 | char *bootFile;␊ |
275 | unsigned long adler32;␊ |
276 | bool quiet;␊ |
277 | bool firstRun = true;␊ |
278 | bool instantMenu;␊ |
279 | bool rescanPrompt;␊ |
280 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
281 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
282 | ␊ |
283 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
284 | // the base code will result in a hang or kernel panic.␊ |
285 | gUnloadPXEOnExit = true;␊ |
286 | ␊ |
287 | // Record the device that the booter was loaded from.␊ |
288 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
289 | ␊ |
290 | // Initialize boot info structure.␊ |
291 | initKernBootStruct();␊ |
292 | ␊ |
293 | // Setup VGA text mode.␊ |
294 | // Not sure if it is safe to call setVideoMode() before the␊ |
295 | // config table has been loaded. Call video_mode() instead.␊ |
296 | #if DEBUG␊ |
297 | printf("before video_mode\n");␊ |
298 | #endif␊ |
299 | video_mode( 2 ); // 80x25 mono text mode.␊ |
300 | #if DEBUG␊ |
301 | printf("after video_mode\n");␊ |
302 | #endif␊ |
303 | ␊ |
304 | // Scan and record the system's hardware information.␊ |
305 | scan_platform();␊ |
306 | ␊ |
307 | // First get info for boot volume.␊ |
308 | scanBootVolumes(gBIOSDev, 0);␊ |
309 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
310 | setBootGlobals(bvChain);␊ |
311 | ␊ |
312 | // Load boot.plist config file␊ |
313 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
314 | ␊ |
315 | if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {␊ |
316 | gBootMode |= kBootModeQuiet;␊ |
317 | }␊ |
318 | ␊ |
319 | // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
320 | if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {␊ |
321 | firstRun = false;␊ |
322 | }␊ |
323 | ␊ |
324 | // Loading preboot ramdisk if exists.␊ |
325 | loadPrebootRAMDisk();␊ |
326 | ␊ |
327 | // Disable rescan option by default␊ |
328 | gEnableCDROMRescan = false;␊ |
329 | ␊ |
330 | // Enable it with Rescan=y in system config␊ |
331 | if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {␊ |
332 | gEnableCDROMRescan = true;␊ |
333 | }␊ |
334 | ␊ |
335 | // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
336 | rescanPrompt = false;␊ |
337 | if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {␊ |
338 | gEnableCDROMRescan = promptForRescanOption();␊ |
339 | }␊ |
340 | ␊ |
341 | // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
342 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {␊ |
343 | gScanSingleDrive = true;␊ |
344 | }␊ |
345 | ␊ |
346 | // Create a list of partitions on device(s).␊ |
347 | if (gScanSingleDrive) {␊ |
348 | scanBootVolumes(gBIOSDev, &bvCount);␊ |
349 | } else {␊ |
350 | scanDisks(gBIOSDev, &bvCount);␊ |
351 | }␊ |
352 | ␊ |
353 | // Create a separated bvr chain using the specified filters.␊ |
354 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
355 | ␊ |
356 | gBootVolume = selectBootVolume(bvChain);␊ |
357 | ␊ |
358 | #if DEBUG␊ |
359 | printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
360 | printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
361 | getc();␊ |
362 | #endif␊ |
363 | ␊ |
364 | ␉␊ |
365 | ␉useGUI = TRUE;␊ |
366 | ␉// Override useGUI default␊ |
367 | ␉getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);␊ |
368 | ␉␊ |
369 | ␉// Before initGui, patch the video bios with the correct resolution␊ |
370 | ␉␊ |
371 | ␉UInt32 params[4];␊ |
372 | ␉int count;␊ |
373 | ␉params[3] = 0;␊ |
374 | ␉vbios_map * map = open_vbios(CT_UNKWN);␊ |
375 | ␉␊ |
376 | ␉autoResolution = TRUE;␊ |
377 | ␉// Override AutoResolution default␊ |
378 | ␉getBoolForKey(kAutoResolutionKey, &autoResolution, &bootInfo->bootConfig);␊ |
379 | ␉␊ |
380 | ␉if (autoResolution == TRUE) {␊ |
381 | ␉␉count = getNumberArrayFromProperty(kGraphicsModeKey, params, 4);␊ |
382 | ␉␉if ( count < 3 )␊ |
383 | ␉␉␉getResolution(¶ms[0], ¶ms[1], ¶ms[2]);␊ |
384 | ␉␉else ␊ |
385 | ␉␉{␊ |
386 | ␉␉␉if ( params[2] == 256 ) params[2] = 8;␊ |
387 | ␉␉␉if ( params[2] == 555 ) params[2] = 16;␊ |
388 | ␉␉␉if ( params[2] == 888 ) params[2] = 32;␊ |
389 | ␉␉}␊ |
390 | ␉␉␊ |
391 | ␉␉if (params[0]!=0 && params[1]!=0) {␉␊ |
392 | ␉␉␉vbios_map * map = open_vbios(CT_UNKWN);␊ |
393 | ␉␉␉␊ |
394 | ␉␉␉unlock_vbios(map);␊ |
395 | ␉␉␉␊ |
396 | ␉␉␉save_vbios(map);␊ |
397 | ␉␉␉␊ |
398 | ␉␉␉set_mode(map, params[0], params[1], params[2], 0, 0);␊ |
399 | ␉␉␉␊ |
400 | ␉␉␉relock_vbios(map);␊ |
401 | ␉␉␉␊ |
402 | ␉␉␉verbose("Patched resolution mode to %dx%d.\n", params[0], params[1]);␊ |
403 | ␉␉}␊ |
404 | ␉}␊ |
405 | ␉␊ |
406 | ␉␊ |
407 | ␉␊ |
408 | ␉␊ |
409 | ␉␊ |
410 | ␉␊ |
411 | ␉␊ |
412 | if (useGUI) {␊ |
413 | /* XXX AsereBLN handle error */␊ |
414 | ␉initGUI();␊ |
415 | }␊ |
416 | ␊ |
417 | setBootGlobals(bvChain);␊ |
418 | ␊ |
419 | // Parse args, load and start kernel.␊ |
420 | while (1) {␊ |
421 | const char *val;␊ |
422 | int len;␊ |
423 | int trycache;␊ |
424 | long flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
425 | int ret = -1;␊ |
426 | void *binary = (void *)kLoadAddr;␊ |
427 | bool tryresume;␊ |
428 | bool tryresumedefault;␊ |
429 | bool forceresume;␊ |
430 | ␊ |
431 | config_file_t systemVersion;␉␉// system.plist of booting partition␊ |
432 | ␊ |
433 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
434 | char bootFileSpec[512];␊ |
435 | ␉␉␊ |
436 | // Initialize globals.␊ |
437 | ␊ |
438 | sysConfigValid = false;␊ |
439 | gErrors = false;␊ |
440 | ␊ |
441 | status = getBootOptions(firstRun);␊ |
442 | firstRun = false;␊ |
443 | if (status == -1) continue;␊ |
444 | ␉␉ ␊ |
445 | status = processBootOptions();␊ |
446 | // Status==1 means to chainboot␊ |
447 | if ( status == 1 ) break;␊ |
448 | // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
449 | if ( status == -1 )␊ |
450 | {␊ |
451 | // gBootVolume == NULL usually means the user hit escape.␊ |
452 | if(gBootVolume == NULL)␊ |
453 | {␊ |
454 | freeFilteredBVChain(bvChain);␊ |
455 | ␊ |
456 | if (gEnableCDROMRescan)␊ |
457 | rescanBIOSDevice(gBIOSDev);␊ |
458 | ␊ |
459 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
460 | setBootGlobals(bvChain);␊ |
461 | }␊ |
462 | continue;␊ |
463 | }␊ |
464 | ␉␉␊ |
465 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
466 | ␉␉␊ |
467 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
468 | ␉␉␉drawBackground();␊ |
469 | ␉␉␉␊ |
470 | // Found and loaded a config file. Proceed with boot.␊ |
471 | ␊ |
472 | ␉␉// Turn off any GUI elements␊ |
473 | ␉␉if( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
474 | ␉␉{␊ |
475 | ␉␉␉gui.devicelist.draw = false;␊ |
476 | ␉␉␉gui.bootprompt.draw = false;␊ |
477 | ␉␉␉gui.menu.draw = false;␊ |
478 | ␉␉␉gui.infobox.draw = false;␊ |
479 | ␉␉␉drawBackground();␊ |
480 | ␉␉␉updateVRAM();␊ |
481 | ␉␉}␊ |
482 | ␉␉␊ |
483 | ␉␉// Find out which version mac os we're booting.␊ |
484 | ␉␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion)) {␊ |
485 | ␉␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion)) {␉␊ |
486 | ␉␉␉␉// getValueForKey uses const char for val␊ |
487 | ␉␉␉␉// so copy it and trim␊ |
488 | ␉␉␉␉strncpy(gMacOSVersion, val, MIN(len, 4));␊ |
489 | ␉␉␉␉gMacOSVersion[MIN(len, 4)] = '\0';␊ |
490 | ␉␉␉}␊ |
491 | ␉␉}␊ |
492 | ␊ |
493 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T)) {␊ |
494 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
495 | ␉␉} else {␊ |
496 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
497 | ␉␉}␊ |
498 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {␊ |
499 | ␉␉␉if (strncmp(val, "i386", 4) == 0) {␊ |
500 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
501 | ␉␉␉}␊ |
502 | ␉␉}␊ |
503 | ␉␉if (getValueForKey(k32BitModeFlag, &val, &len, &bootInfo->bootConfig)) {␊ |
504 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
505 | ␉␉}␊ |
506 | ␉␉␊ |
507 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {␊ |
508 | ␉␉␉tryresume = true;␊ |
509 | ␉␉␉tryresumedefault = true;␊ |
510 | ␉␉} else {␊ |
511 | ␉␉␉tryresumedefault = false;␊ |
512 | ␉␉}␊ |
513 | ␊ |
514 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {␊ |
515 | ␉␉␉forceresume = false;␊ |
516 | ␉␉}␊ |
517 | ␉␉␊ |
518 | ␉␉if (forceresume) {␊ |
519 | ␉␉␉tryresume = true;␊ |
520 | ␉␉␉tryresumedefault = false;␊ |
521 | ␉␉}␊ |
522 | ␉␉␊ |
523 | ␉␉while (tryresume) {␊ |
524 | ␉␉␉const char *tmp;␊ |
525 | ␉␉␉BVRef bvr;␊ |
526 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))␊ |
527 | ␉␉␉␉val="/private/var/vm/sleepimage";␊ |
528 | ␉␉␉␊ |
529 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
530 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
531 | ␊ |
532 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
533 | ␉␉␉␉break;␊ |
534 | ␉␉␉␊ |
535 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
536 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
537 | ␉␉␉␉break;␊ |
538 | ␉␉␉␊ |
539 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
540 | ␉␉␉␉break;␊ |
541 | ␉␉␉␊ |
542 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
543 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);␊ |
544 | ␉␉␉␉break;␊ |
545 | ␉␉␉}␊ |
546 | ␉␉␉␉␊ |
547 | ␉␉␉HibernateBoot((char *)val);␊ |
548 | ␉␉␉break;␊ |
549 | ␉␉}␊ |
550 | ␊ |
551 | // Reset cache name.␊ |
552 | bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
553 | ␊ |
554 | sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
555 | ␊ |
556 | adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
557 | ␊ |
558 | if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
559 | strlcpy(gBootKernelCacheFile, val, len+1);␊ |
560 | } else {␊ |
561 | sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);␊ |
562 | }␊ |
563 | ␊ |
564 | // Check for cache file.␊ |
565 | trycache = (((gBootMode & kBootModeSafe) == 0) &&␊ |
566 | !gOverrideKernel &&␊ |
567 | (gBootFileType == kBlockDeviceType) &&␊ |
568 | (gMKextName[0] == '\0') &&␊ |
569 | (gBootKernelCacheFile[0] != '\0'));␊ |
570 | ␊ |
571 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
572 | ␉␉␊ |
573 | if (trycache) do {␊ |
574 | ␊ |
575 | // if we haven't found the kernel yet, don't use the cache␊ |
576 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
577 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
578 | trycache = 0;␊ |
579 | break;␊ |
580 | }␊ |
581 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
582 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
583 | || (cachetime < kerneltime)) {␊ |
584 | trycache = 0;␊ |
585 | break;␊ |
586 | }␊ |
587 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
588 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
589 | && (cachetime < exttime)) {␊ |
590 | trycache = 0;␊ |
591 | break;␊ |
592 | }␊ |
593 | if (kerneltime > exttime) {␊ |
594 | exttime = kerneltime;␊ |
595 | }␊ |
596 | if (cachetime != (exttime + 1)) {␊ |
597 | trycache = 0;␊ |
598 | break;␊ |
599 | }␊ |
600 | } while (0);␊ |
601 | ␊ |
602 | do {␊ |
603 | if (trycache) {␊ |
604 | bootFile = gBootKernelCacheFile;␊ |
605 | verbose("Loading kernel cache %s\n", bootFile);␊ |
606 | ret = LoadFile(bootFile);␊ |
607 | binary = (void *)kLoadAddr;␊ |
608 | if (ret >= 0) {␊ |
609 | break;␊ |
610 | }␊ |
611 | }␊ |
612 | bootFile = bootInfo->bootFile;␊ |
613 | ␊ |
614 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
615 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
616 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
617 | ␉ ␉ if (ret == -1)␊ |
618 | ␉ ␉ {␊ |
619 | sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
620 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
621 | if (ret == -1)␊ |
622 | {␊ |
623 | sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
624 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
625 | if (ret == -1)␊ |
626 | {␊ |
627 | // Not found any alternate locations, using the original kernel image path.␊ |
628 | strcpy(bootFileSpec, bootFile);␊ |
629 | }␊ |
630 | }␊ |
631 | }␊ |
632 | ␉␉␉␊ |
633 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
634 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
635 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
636 | {␊ |
637 | archCpuType = CPU_TYPE_I386;␊ |
638 | ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
639 | }␊ |
640 | ␉␉␉␊ |
641 | } while (0);␊ |
642 | ␊ |
643 | clearActivityIndicator();␊ |
644 | #if DEBUG␊ |
645 | printf("Pausing...");␊ |
646 | sleep(8);␊ |
647 | #endif␊ |
648 | ␊ |
649 | if (ret <= 0) {␊ |
650 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
651 | ␊ |
652 | ␉␉␉if(gui.initialised) {␊ |
653 | ␉␉␉␉sleep(1);␊ |
654 | ␉␉␉␉drawBackground();␊ |
655 | ␉␉␉␉gui.devicelist.draw = true;␊ |
656 | ␉␉␉␉gui.redraw = true;␊ |
657 | ␉␉␉}␊ |
658 | if (gBootFileType == kNetworkDeviceType) {␊ |
659 | // Return control back to PXE. Don't unload PXE base code.␊ |
660 | gUnloadPXEOnExit = false;␊ |
661 | break;␊ |
662 | }␊ |
663 | } else {␊ |
664 | /* Won't return if successful. */␊ |
665 | ret = ExecKernel(binary, map);␊ |
666 | }␊ |
667 | }␊ |
668 | ␊ |
669 | // chainboot␊ |
670 | if (status==1) {␊ |
671 | ␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
672 | ␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
673 | ␉}␊ |
674 | }␊ |
675 | ␉␊ |
676 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
677 | ␉nbpUnloadBaseCode();␊ |
678 | }␊ |
679 | }␊ |
680 | ␊ |
681 | /*!␊ |
682 | Selects a new BIOS device, taking care to update the global state appropriately.␊ |
683 | */␊ |
684 | /*␊ |
685 | static void selectBiosDevice(void)␊ |
686 | {␊ |
687 | struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
688 | CacheReset();␊ |
689 | diskFreeMap(oldMap);␊ |
690 | oldMap = NULL;␊ |
691 | ␊ |
692 | int dev = selectAlternateBootDevice(gBIOSDev);␊ |
693 | ␊ |
694 | BVRef bvchain = scanBootVolumes(dev, 0);␊ |
695 | BVRef bootVol = selectBootVolume(bvchain);␊ |
696 | gBootVolume = bootVol;␊ |
697 | setRootVolume(bootVol);␊ |
698 | gBIOSDev = dev;␊ |
699 | }␊ |
700 | */␊ |
701 | ␊ |
702 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
703 | #define NMAX 5000␊ |
704 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
705 | ␊ |
706 | #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}␊ |
707 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);␊ |
708 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);␊ |
709 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);␊ |
710 | #define DO16(buf) DO8(buf,0); DO8(buf,8);␊ |
711 | ␊ |
712 | unsigned long Adler32(unsigned char *buf, long len)␊ |
713 | {␊ |
714 | unsigned long s1 = 1; // adler & 0xffff;␊ |
715 | unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
716 | unsigned long result;␊ |
717 | int k;␊ |
718 | ␊ |
719 | while (len > 0) {␊ |
720 | k = len < NMAX ? len : NMAX;␊ |
721 | len -= k;␊ |
722 | while (k >= 16) {␊ |
723 | DO16(buf);␊ |
724 | buf += 16;␊ |
725 | k -= 16;␊ |
726 | }␊ |
727 | if (k != 0) do {␊ |
728 | s1 += *buf++;␊ |
729 | s2 += s1;␊ |
730 | } while (--k);␊ |
731 | s1 %= BASE;␊ |
732 | s2 %= BASE;␊ |
733 | }␊ |
734 | result = (s2 << 16) | s1;␊ |
735 | return OSSwapHostToBigInt32(result);␊ |
736 | }␊ |
737 |