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