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