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