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