Root/
Source at commit 543 created 13 years 7 months ago. By meklort, Chameleon code shrinkage. Also moved a few things to modules. | |
---|---|
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 "modules.h"␊ |
61 | ␊ |
62 | long gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
63 | bool gOverrideKernel;␊ |
64 | static char gBootKernelCacheFile[512];␊ |
65 | static char gCacheNameAdler[64 + 256];␊ |
66 | char *gPlatformName = gCacheNameAdler;␊ |
67 | char gRootDevice[512];␊ |
68 | char gMKextName[512];␊ |
69 | char gMacOSVersion[8];␊ |
70 | #ifndef OPTION_ROM␊ |
71 | bool gEnableCDROMRescan;␊ |
72 | bool gScanSingleDrive;␊ |
73 | #endif␊ |
74 | ␊ |
75 | int bvCount = 0;␊ |
76 | //int␉menucount = 0;␊ |
77 | int gDeviceCount = 0; ␊ |
78 | ␊ |
79 | BVRef bvr;␊ |
80 | BVRef menuBVR;␊ |
81 | BVRef bvChain;␊ |
82 | ␊ |
83 | //static void selectBiosDevice(void);␊ |
84 | static unsigned long Adler32(unsigned char *buffer, long length);␊ |
85 | static bool getOSVersion(char *str);␊ |
86 | ␊ |
87 | #ifndef OPTION_ROM␊ |
88 | static bool gUnloadPXEOnExit = false;␊ |
89 | #endif␊ |
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 | ␉md0Ramdisk();␊ |
167 | ␉␊ |
168 | setupFakeEfi();␊ |
169 | ␉␉␊ |
170 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
171 | #ifndef OPTION_ROM␊ |
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 | #endif ␊ |
182 | bool dummyVal;␊ |
183 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {␊ |
184 | ␉␉printf("Press any key to continue...");␊ |
185 | ␉␉getc();␊ |
186 | ␉}␊ |
187 | ␉␉␊ |
188 | // If we were in text mode, switch to graphics mode.␊ |
189 | // This will draw the boot graphics unless we are in␊ |
190 | // verbose mode.␊ |
191 | ␉␊ |
192 | if(gVerboseMode)␊ |
193 | ␉{␊ |
194 | ␉␉if (bootArgs->Video.v_display == VGA_TEXT_MODE)␊ |
195 | ␉␉{␊ |
196 | ␉␉␉setVideoMode( GRAPHICS_MODE, 0 );␊ |
197 | ␉␉}␊ |
198 | ␉␉␊ |
199 | ␉}␊ |
200 | ␊ |
201 | ␉setupBooterLog();␊ |
202 | ␉␊ |
203 | finalizeBootStruct();␊ |
204 | ␉␊ |
205 | ␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);␉// Notify modules that the kernel is about to be started␊ |
206 | ␉␊ |
207 | ␉// Jump to kernel's entry point. There's no going back now.␊ |
208 | startprog( kernelEntry, bootArgs );␊ |
209 | ␉␊ |
210 | // Not reached␊ |
211 | ␉␊ |
212 | return 0;␊ |
213 | }␊ |
214 | ␊ |
215 | //==========================================================================␊ |
216 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
217 | // before. Multiboot does its own runtime initialization, does some of its␊ |
218 | // own things, and then calls common_boot.␊ |
219 | void boot(int biosdev)␊ |
220 | {␊ |
221 | ␉initialize_runtime();␊ |
222 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
223 | ␉enableA20();␊ |
224 | ␉common_boot(biosdev);␊ |
225 | }␊ |
226 | ␊ |
227 | //==========================================================================␊ |
228 | // The 'main' function for the booter. Called by boot0 when booting␊ |
229 | // from a block device, or by the network booter.␊ |
230 | //␊ |
231 | // arguments:␊ |
232 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
233 | // that the booter was loaded from.␊ |
234 | //␊ |
235 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
236 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
237 | // next boot device on its list.␊ |
238 | void common_boot(int biosdev)␊ |
239 | {␊ |
240 | int status;␊ |
241 | char *bootFile;␊ |
242 | unsigned long adler32;␊ |
243 | bool quiet;␊ |
244 | bool firstRun = true;␊ |
245 | bool instantMenu;␊ |
246 | #ifndef OPTION_ROM␊ |
247 | bool rescanPrompt;␊ |
248 | #endif␊ |
249 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
250 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
251 | ␉␊ |
252 | #ifndef OPTION_ROM␊ |
253 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
254 | // the base code will result in a hang or kernel panic.␊ |
255 | gUnloadPXEOnExit = true;␊ |
256 | #endif␊ |
257 | ␉␊ |
258 | // Record the device that the booter was loaded from.␊ |
259 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
260 | ␉␊ |
261 | // Initialize boot info structure.␊ |
262 | initKernBootStruct();␊ |
263 | ␉␊ |
264 | ␉initBooterLog();␊ |
265 | ␉␊ |
266 | // Setup VGA text mode.␊ |
267 | // Not sure if it is safe to call setVideoMode() before the␊ |
268 | // config table has been loaded. Call video_mode() instead.␊ |
269 | #if DEBUG␊ |
270 | printf("before video_mode\n");␊ |
271 | #endif␊ |
272 | video_mode( 2 ); // 80x25 mono text mode.␊ |
273 | #if DEBUG␊ |
274 | printf("after video_mode\n");␊ |
275 | #endif␊ |
276 | ␉␊ |
277 | // Scan and record the system's hardware information.␊ |
278 | scan_platform();␊ |
279 | ␉␊ |
280 | // First get info for boot volume.␊ |
281 | scanBootVolumes(gBIOSDev, 0);␊ |
282 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
283 | setBootGlobals(bvChain);␊ |
284 | ␉␊ |
285 | // Load boot.plist config file␊ |
286 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
287 | ␉␊ |
288 | if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {␊ |
289 | gBootMode |= kBootModeQuiet;␊ |
290 | }␊ |
291 | ␉␊ |
292 | // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
293 | if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {␊ |
294 | firstRun = false;␊ |
295 | }␊ |
296 | ␉␊ |
297 | #ifndef OPTION_ROM␊ |
298 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
299 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {␊ |
300 | gScanSingleDrive = true;␊ |
301 | }␊ |
302 | ␉␊ |
303 | ␉// Create a list of partitions on device(s).␊ |
304 | if (gScanSingleDrive)␊ |
305 | ␉{␊ |
306 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
307 | } ␊ |
308 | ␉else ␊ |
309 | #endif␊ |
310 | ␉{␊ |
311 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
312 | }␊ |
313 | ␉␊ |
314 | ␉// Create a separated bvr chain using the specified filters.␊ |
315 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
316 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
317 | ␉␊ |
318 | ␉␊ |
319 | ␉// Intialize module system␊ |
320 | ␉if(init_module_system())␊ |
321 | ␉{␊ |
322 | ␉␉load_all_modules();␊ |
323 | ␉}␊ |
324 | ␉␊ |
325 | ␉␊ |
326 | #ifndef OPTION_ROM␊ |
327 | // Loading preboot ramdisk if exists.␊ |
328 | loadPrebootRAMDisk();␊ |
329 | ␉␊ |
330 | // Disable rescan option by default␊ |
331 | gEnableCDROMRescan = false;␊ |
332 | ␉␊ |
333 | // Enable it with Rescan=y in system config␊ |
334 | if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {␊ |
335 | gEnableCDROMRescan = true;␊ |
336 | }␊ |
337 | ␉␊ |
338 | // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
339 | rescanPrompt = false;␊ |
340 | if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {␊ |
341 | gEnableCDROMRescan = promptForRescanOption();␊ |
342 | }␊ |
343 | #endif␊ |
344 | ␉␊ |
345 | ␊ |
346 | ␉␊ |
347 | ␉␊ |
348 | #if DEBUG␊ |
349 | printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
350 | printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
351 | getc();␊ |
352 | #endif␊ |
353 | ␉␊ |
354 | ␉␊ |
355 | ␉␊ |
356 | setBootGlobals(bvChain);␊ |
357 | ␉␊ |
358 | // Parse args, load and start kernel.␊ |
359 | while (1) {␊ |
360 | const char *val;␊ |
361 | int len;␊ |
362 | int trycache;␊ |
363 | long flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
364 | int ret = -1;␊ |
365 | void *binary = (void *)kLoadAddr;␊ |
366 | bool tryresume;␊ |
367 | bool tryresumedefault;␊ |
368 | bool forceresume;␊ |
369 | ␉␉␊ |
370 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
371 | char bootFileSpec[512];␊ |
372 | ␉␉␊ |
373 | // Initialize globals.␊ |
374 | ␉␉␊ |
375 | sysConfigValid = false;␊ |
376 | gErrors = false;␊ |
377 | ␉␉␊ |
378 | status = getBootOptions(firstRun);␊ |
379 | firstRun = false;␊ |
380 | if (status == -1) continue;␊ |
381 | ␉␉␊ |
382 | status = processBootOptions();␊ |
383 | // Status==1 means to chainboot␊ |
384 | if ( status == 1 ) break;␊ |
385 | // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
386 | if ( status == -1 )␊ |
387 | {␊ |
388 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
389 | ␉␉␉if(gBootVolume == NULL)␊ |
390 | ␉␉␉{␊ |
391 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
392 | #ifndef OPTION_ROM␊ |
393 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
394 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
395 | #endif␊ |
396 | ␉␉␉␉␊ |
397 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
398 | ␉␉␉␉setBootGlobals(bvChain);␊ |
399 | ␉␉␉}␊ |
400 | ␉␉␉continue;␊ |
401 | }␊ |
402 | ␉␉␊ |
403 | ␉␉␊ |
404 | ␉␉␊ |
405 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
406 | ␉␉␉␉␊ |
407 | ␉␉// Notify moduals that we are attempting to boot␊ |
408 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL);␊ |
409 | ␉␉␊ |
410 | ␉␉// Find out which version mac os we're booting.␊ |
411 | ␉␉getOSVersion(gMacOSVersion);␊ |
412 | ␉␉␊ |
413 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T)) {␊ |
414 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
415 | ␉␉} else {␊ |
416 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
417 | ␉␉}␊ |
418 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {␊ |
419 | ␉␉␉if (strncmp(val, "i386", 4) == 0) {␊ |
420 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
421 | ␉␉␉}␊ |
422 | ␉␉}␊ |
423 | ␉␉␊ |
424 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {␊ |
425 | ␉␉␉tryresume = true;␊ |
426 | ␉␉␉tryresumedefault = true;␊ |
427 | ␉␉} else {␊ |
428 | ␉␉␉tryresumedefault = false;␊ |
429 | ␉␉}␊ |
430 | ␉␉␊ |
431 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {␊ |
432 | ␉␉␉forceresume = false;␊ |
433 | ␉␉}␊ |
434 | ␉␉␊ |
435 | ␉␉if (forceresume) {␊ |
436 | ␉␉␉tryresume = true;␊ |
437 | ␉␉␉tryresumedefault = false;␊ |
438 | ␉␉}␊ |
439 | ␉␉␊ |
440 | ␉␉while (tryresume) {␊ |
441 | ␉␉␉const char *tmp;␊ |
442 | ␉␉␉BVRef bvr;␊ |
443 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))␊ |
444 | ␉␉␉␉val="/private/var/vm/sleepimage";␊ |
445 | ␉␉␉␊ |
446 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
447 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
448 | ␉␉␉␊ |
449 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
450 | ␉␉␉␉break;␊ |
451 | ␉␉␉␊ |
452 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
453 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
454 | ␉␉␉␉break;␊ |
455 | ␉␉␉␊ |
456 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
457 | ␉␉␉␉break;␊ |
458 | ␉␉␉␊ |
459 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
460 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);␊ |
461 | ␉␉␉␉break;␊ |
462 | ␉␉␉}␊ |
463 | ␉␉␉␊ |
464 | ␉␉␉HibernateBoot((char *)val);␊ |
465 | ␉␉␉break;␊ |
466 | ␉␉}␊ |
467 | ␉␉␊ |
468 | // Reset cache name.␊ |
469 | bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
470 | ␉␉␊ |
471 | sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
472 | ␉␉␊ |
473 | adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
474 | ␉␉␊ |
475 | if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
476 | strlcpy(gBootKernelCacheFile, val, len+1);␊ |
477 | } else {␊ |
478 | sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);␊ |
479 | }␊ |
480 | ␉␉␊ |
481 | // Check for cache file.␊ |
482 | trycache = (((gBootMode & kBootModeSafe) == 0) &&␊ |
483 | !gOverrideKernel &&␊ |
484 | (gBootFileType == kBlockDeviceType) &&␊ |
485 | (gMKextName[0] == '\0') &&␊ |
486 | (gBootKernelCacheFile[0] != '\0'));␊ |
487 | ␉␉␊ |
488 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
489 | ␉␉␊ |
490 | if (trycache) do {␊ |
491 | ␉␉␉␊ |
492 | // if we haven't found the kernel yet, don't use the cache␊ |
493 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
494 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
495 | trycache = 0;␊ |
496 | break;␊ |
497 | }␊ |
498 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
499 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
500 | || (cachetime < kerneltime)) {␊ |
501 | trycache = 0;␊ |
502 | break;␊ |
503 | }␊ |
504 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
505 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
506 | && (cachetime < exttime)) {␊ |
507 | trycache = 0;␊ |
508 | break;␊ |
509 | }␊ |
510 | if (kerneltime > exttime) {␊ |
511 | exttime = kerneltime;␊ |
512 | }␊ |
513 | if (cachetime != (exttime + 1)) {␊ |
514 | trycache = 0;␊ |
515 | break;␊ |
516 | }␊ |
517 | } while (0);␊ |
518 | ␉␉␊ |
519 | do {␊ |
520 | if (trycache) {␊ |
521 | bootFile = gBootKernelCacheFile;␊ |
522 | verbose("Loading kernel cache %s\n", bootFile);␊ |
523 | ret = LoadFile(bootFile);␊ |
524 | binary = (void *)kLoadAddr;␊ |
525 | if (ret >= 0) {␊ |
526 | break;␊ |
527 | }␊ |
528 | }␊ |
529 | bootFile = bootInfo->bootFile;␊ |
530 | ␉␉␉␊ |
531 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
532 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
533 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
534 | ␉ ␉ if (ret == -1)␊ |
535 | ␉ ␉ {␊ |
536 | ␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
537 | ␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
538 | ␉␉␉␉if (ret == -1)␊ |
539 | ␉␉␉␉{␊ |
540 | ␉␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
541 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
542 | ␉␉␉␉␉if (ret == -1)␊ |
543 | ␉␉␉␉␉{␊ |
544 | ␉␉␉␉␉␉// Not found any alternate locations, using the original kernel image path.␊ |
545 | ␉␉␉␉␉␉strcpy(bootFileSpec, bootFile);␊ |
546 | ␉␉␉␉␉}␊ |
547 | ␉␉␉␉}␊ |
548 | }␊ |
549 | ␉␉␉␊ |
550 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
551 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
552 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
553 | {␊ |
554 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
555 | ␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
556 | }␊ |
557 | ␉␉␉␊ |
558 | } while (0);␊ |
559 | ␉␉␊ |
560 | clearActivityIndicator();␊ |
561 | #if DEBUG␊ |
562 | printf("Pausing...");␊ |
563 | sleep(8);␊ |
564 | #endif␊ |
565 | ␉␉␊ |
566 | if (ret <= 0) {␊ |
567 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
568 | ␉␉␉␊ |
569 | ␉␉␉sleep(1);␊ |
570 | #ifndef OPTION_ROM␊ |
571 | if (gBootFileType == kNetworkDeviceType) {␊ |
572 | // Return control back to PXE. Don't unload PXE base code.␊ |
573 | gUnloadPXEOnExit = false;␊ |
574 | break;␊ |
575 | }␊ |
576 | #endif␊ |
577 | } else {␊ |
578 | /* Won't return if successful. */␊ |
579 | ␉␉␉// Notify modules that ExecKernel is about to be called␊ |
580 | ␉␉␉execute_hook("ExecKernel", binary, NULL, NULL, NULL);␊ |
581 | ret = ExecKernel(binary);␊ |
582 | }␊ |
583 | }␊ |
584 | ␊ |
585 | // chainboot␊ |
586 | if (status==1) {␊ |
587 | ␉␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
588 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
589 | ␉␉}␊ |
590 | }␊ |
591 | #ifndef OPTION_ROM␊ |
592 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
593 | ␉␉nbpUnloadBaseCode();␊ |
594 | }␊ |
595 | #endif␊ |
596 | }␊ |
597 | ␊ |
598 | /*!␊ |
599 | Selects a new BIOS device, taking care to update the global state appropriately.␊ |
600 | */␊ |
601 | /*␊ |
602 | static void selectBiosDevice(void)␊ |
603 | {␊ |
604 | struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
605 | CacheReset();␊ |
606 | diskFreeMap(oldMap);␊ |
607 | oldMap = NULL;␊ |
608 | ␊ |
609 | int dev = selectAlternateBootDevice(gBIOSDev);␊ |
610 | ␊ |
611 | BVRef bvchain = scanBootVolumes(dev, 0);␊ |
612 | BVRef bootVol = selectBootVolume(bvchain);␊ |
613 | gBootVolume = bootVol;␊ |
614 | setRootVolume(bootVol);␊ |
615 | gBIOSDev = dev;␊ |
616 | }␊ |
617 | */␊ |
618 | ␊ |
619 | static bool getOSVersion(char *str)␊ |
620 | {␊ |
621 | ␉bool valid = false;␊ |
622 | ␉config_file_t systemVersion;␊ |
623 | ␉const char *val;␊ |
624 | ␉int len;␊ |
625 | ␉␊ |
626 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
627 | ␉{␊ |
628 | ␉␉valid = true;␊ |
629 | ␉}␊ |
630 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
631 | ␉{␊ |
632 | ␉␉valid = true;␊ |
633 | ␉}␊ |
634 | ␉␊ |
635 | ␉if (valid)␊ |
636 | ␉{␊ |
637 | ␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion))␊ |
638 | ␉␉{␊ |
639 | ␉␉␉// getValueForKey uses const char for val␊ |
640 | ␉␉␉// so copy it and trim␊ |
641 | ␉␉␉*str = '\0';␊ |
642 | ␉␉␉strncat(str, val, MIN(len, 4));␊ |
643 | ␉␉}␊ |
644 | ␉␉else␊ |
645 | ␉␉␉valid = false;␊ |
646 | ␉}␊ |
647 | ␉␊ |
648 | ␉return valid;␊ |
649 | }␊ |
650 | ␊ |
651 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
652 | #define NMAX 5000␊ |
653 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
654 | ␊ |
655 | #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}␊ |
656 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);␊ |
657 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);␊ |
658 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);␊ |
659 | #define DO16(buf) DO8(buf,0); DO8(buf,8);␊ |
660 | ␊ |
661 | unsigned long Adler32(unsigned char *buf, long len)␊ |
662 | {␊ |
663 | unsigned long s1 = 1; // adler & 0xffff;␊ |
664 | unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
665 | unsigned long result;␊ |
666 | int k;␊ |
667 | ␉␊ |
668 | while (len > 0) {␊ |
669 | k = len < NMAX ? len : NMAX;␊ |
670 | len -= k;␊ |
671 | while (k >= 16) {␊ |
672 | DO16(buf);␊ |
673 | buf += 16;␊ |
674 | k -= 16;␊ |
675 | }␊ |
676 | if (k != 0) do {␊ |
677 | s1 += *buf++;␊ |
678 | s2 += s1;␊ |
679 | } while (--k);␊ |
680 | s1 %= BASE;␊ |
681 | s2 %= BASE;␊ |
682 | }␊ |
683 | result = (s2 << 16) | s1;␊ |
684 | return OSSwapHostToBigInt32(result);␊ |
685 | }␊ |
686 |