1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /* ␊ |
25 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * All rights reserved. The CMU software License Agreement specifies␊ |
29 | * the terms and conditions for use and redistribution.␊ |
30 | */␊ |
31 | ␊ |
32 | /*␊ |
33 | * INTEL CORPORATION PROPRIETARY INFORMATION␊ |
34 | *␊ |
35 | * This software is supplied under the terms of a license agreement or ␊ |
36 | * nondisclosure agreement with Intel Corporation and may not be copied ␊ |
37 | * nor disclosed except in accordance with the terms of that agreement.␊ |
38 | *␊ |
39 | * Copyright 1988, 1989 by Intel Corporation␊ |
40 | */␊ |
41 | ␊ |
42 | /*␊ |
43 | * Copyright 1993 NeXT Computer, Inc.␊ |
44 | * All rights reserved.␊ |
45 | */␊ |
46 | ␊ |
47 | /*␊ |
48 | * Completely reworked by Sam Streeper (sam_s@NeXT.com)␊ |
49 | * Reworked again by Curtis Galloway (galloway@NeXT.com)␊ |
50 | */␊ |
51 | ␊ |
52 | ␊ |
53 | #include "boot.h"␊ |
54 | #include "bootstruct.h"␊ |
55 | #include "fake_efi.h"␊ |
56 | #include "sl.h"␊ |
57 | #include "libsa.h"␊ |
58 | #include "ramdisk.h"␊ |
59 | #include "gui.h"␊ |
60 | #include "platform.h"␊ |
61 | ␊ |
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; disabled␊ |
67 | char gRootDevice[512];␊ |
68 | char gMKextName[512];␊ |
69 | bool gEnableCDROMRescan;␊ |
70 | bool gScanSingleDrive;␊ |
71 | ␊ |
72 | int bvCount = 0;␊ |
73 | //int␉␉menucount = 0;␊ |
74 | int gDeviceCount = 0; ␊ |
75 | ␊ |
76 | BVRef bvr;␊ |
77 | BVRef menuBVR;␊ |
78 | BVRef bvChain;␊ |
79 | bool useGUI;␊ |
80 | ␊ |
81 | //static void selectBiosDevice(void);␊ |
82 | static unsigned long Adler32(unsigned char *buffer, long length);␊ |
83 | ␊ |
84 | ␊ |
85 | static bool gUnloadPXEOnExit = false;␊ |
86 | ␊ |
87 | /*␊ |
88 | * How long to wait (in seconds) to load the␊ |
89 | * kernel after displaying the "boot:" prompt.␊ |
90 | */␊ |
91 | #define kBootErrorTimeout 5␊ |
92 | ␊ |
93 | /*␊ |
94 | * Default path to kernel cache file␊ |
95 | */␊ |
96 | #define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"␊ |
97 | ␊ |
98 | //==========================================================================␊ |
99 | // Zero the BSS.␊ |
100 | ␊ |
101 | static void zeroBSS(void)␊ |
102 | {␊ |
103 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
104 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
105 | ␊ |
106 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
107 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
108 | }␊ |
109 | ␊ |
110 | //==========================================================================␊ |
111 | // Malloc error function␊ |
112 | ␊ |
113 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
114 | {␊ |
115 | ␉stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);␊ |
116 | }␊ |
117 | ␊ |
118 | //==========================================================================␊ |
119 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
120 | //␊ |
121 | void initialize_runtime(void)␊ |
122 | {␊ |
123 | ␉zeroBSS();␊ |
124 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
125 | }␊ |
126 | ␊ |
127 | //==========================================================================␊ |
128 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
129 | ␊ |
130 | static int ExecKernel(void *binary)␊ |
131 | {␊ |
132 | entry_t kernelEntry;␊ |
133 | int ret;␊ |
134 | ␊ |
135 | bootArgs->kaddr = bootArgs->ksize = 0;␊ |
136 | ␊ |
137 | ret = DecodeKernel(binary,␊ |
138 | &kernelEntry,␊ |
139 | (char **) &bootArgs->kaddr,␊ |
140 | (int *)&bootArgs->ksize );␊ |
141 | ␊ |
142 | if ( ret != 0 )␊ |
143 | return ret;␊ |
144 | ␊ |
145 | // Reserve space for boot args␊ |
146 | reserveKernBootStruct();␊ |
147 | ␊ |
148 | // Load boot drivers from the specifed root path.␊ |
149 | ␊ |
150 | if (!gHaveKernelCache) {␊ |
151 | LoadDrivers("/");␊ |
152 | }␊ |
153 | ␊ |
154 | clearActivityIndicator();␊ |
155 | ␊ |
156 | if (gErrors) {␊ |
157 | printf("Errors encountered while starting up the computer.\n");␊ |
158 | printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
159 | sleep(kBootErrorTimeout);␊ |
160 | }␊ |
161 | ␊ |
162 | setupFakeEfi();␊ |
163 | ␊ |
164 | md0Ramdisk();␊ |
165 | ␊ |
166 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
167 | ␊ |
168 | // Cleanup the PXE base code.␊ |
169 | ␊ |
170 | if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {␊ |
171 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
172 | {␊ |
173 | ␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
174 | sleep(2);␊ |
175 | }␊ |
176 | }␊ |
177 | ␊ |
178 | bool dummyVal;␊ |
179 | ␊ |
180 | ␉//Azi: Wait=y is breaking other keys when typed "after them" at boot prompt.␊ |
181 | ␉// Works properly if typed in first place or used on Boot.plist.␊ |
182 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {␊ |
183 | ␉␉pause();␊ |
184 | ␉}␊ |
185 | ␊ |
186 | ␉usb_loop();␊ |
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 | setVideoMode( GRAPHICS_MODE, 0 );␊ |
194 | else␊ |
195 | drawBootGraphics();␊ |
196 | ␉␊ |
197 | ␉setupBooterLog();␊ |
198 | ␉␊ |
199 | finalizeBootStruct();␊ |
200 | ␊ |
201 | // Jump to kernel's entry point. There's no going back now.␊ |
202 | ␊ |
203 | startprog( kernelEntry, bootArgs );␊ |
204 | ␊ |
205 | // Not reached␊ |
206 | ␊ |
207 | return 0;␊ |
208 | }␊ |
209 | ␊ |
210 | //==========================================================================␊ |
211 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
212 | // before. Multiboot does its own runtime initialization, does some of its␊ |
213 | // own things, and then calls common_boot.␊ |
214 | void boot(int biosdev)␊ |
215 | {␊ |
216 | ␉initialize_runtime();␊ |
217 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
218 | ␉enableA20();␊ |
219 | ␉common_boot(biosdev);␊ |
220 | }␊ |
221 | ␊ |
222 | //==========================================================================␊ |
223 | // The 'main' function for the booter. Called by boot0 when booting␊ |
224 | // from a block device, or by the network booter.␊ |
225 | //␊ |
226 | // arguments:␊ |
227 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
228 | // that the booter was loaded from.␊ |
229 | //␊ |
230 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
231 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
232 | // next boot device on its list.␊ |
233 | void common_boot(int biosdev)␊ |
234 | {␊ |
235 | int status;␊ |
236 | char *bootFile;␊ |
237 | unsigned long adler32;␊ |
238 | bool quiet;␊ |
239 | bool firstRun = true;␊ |
240 | bool instantMenu;␊ |
241 | bool rescanPrompt;␊ |
242 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
243 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
244 | ␊ |
245 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
246 | // the base code will result in a hang or kernel panic.␊ |
247 | gUnloadPXEOnExit = true;␊ |
248 | ␊ |
249 | // Record the device that the booter was loaded from.␊ |
250 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
251 | ␊ |
252 | // Initialize boot info structure.␊ |
253 | initKernBootStruct();␊ |
254 | ␊ |
255 | ␉initBooterLog();␊ |
256 | ␊ |
257 | // Setup VGA text mode.␊ |
258 | // Not sure if it is safe to call setVideoMode() before the␊ |
259 | // config table has been loaded. Call video_mode() instead.␊ |
260 | #if DEBUG␊ |
261 | printf("before video_mode\n");␊ |
262 | #endif␊ |
263 | video_mode( 2 ); // 80x25 mono text mode.␊ |
264 | #if DEBUG␊ |
265 | printf("after video_mode\n");␊ |
266 | #endif␊ |
267 | ␊ |
268 | // Scan and record the system's hardware information.␊ |
269 | scan_platform();␊ |
270 | ␊ |
271 | // First get info for boot volume.␊ |
272 | scanBootVolumes(gBIOSDev, 0);␊ |
273 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
274 | setBootGlobals(bvChain);␊ |
275 | ␊ |
276 | // Load boot.plist config file␊ |
277 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
278 | ␊ |
279 | if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {␊ |
280 | gBootMode |= kBootModeQuiet;␊ |
281 | }␊ |
282 | ␊ |
283 | // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
284 | if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {␊ |
285 | firstRun = false;␊ |
286 | }␊ |
287 | ␊ |
288 | // Loading preboot ramdisk if exists.␊ |
289 | loadPrebootRAMDisk();␊ |
290 | ␊ |
291 | // Disable rescan option by default␊ |
292 | gEnableCDROMRescan = false;␊ |
293 | ␊ |
294 | // Enable it with Rescan=y in system config␊ |
295 | if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {␊ |
296 | gEnableCDROMRescan = true;␊ |
297 | }␊ |
298 | ␊ |
299 | // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
300 | rescanPrompt = false;␊ |
301 | if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {␊ |
302 | gEnableCDROMRescan = promptForRescanOption();␊ |
303 | }␊ |
304 | ␊ |
305 | // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
306 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {␊ |
307 | gScanSingleDrive = true;␊ |
308 | }␊ |
309 | ␊ |
310 | // Create a list of partitions on device(s).␊ |
311 | if (gScanSingleDrive) {␊ |
312 | scanBootVolumes(gBIOSDev, &bvCount);␊ |
313 | } else {␊ |
314 | scanDisks(gBIOSDev, &bvCount);␊ |
315 | }␊ |
316 | ␊ |
317 | // Create a separated bvr chain using the specified filters.␊ |
318 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
319 | ␊ |
320 | gBootVolume = selectBootVolume(bvChain);␊ |
321 | ␊ |
322 | #if DEBUG␊ |
323 | printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
324 | printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
325 | getc();␊ |
326 | #endif␊ |
327 | ␊ |
328 | useGUI = true;␊ |
329 | // Override useGUI default␊ |
330 | getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);␊ |
331 | if (useGUI && initGUI())␊ |
332 | ␉{␊ |
333 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
334 | ␉␉useGUI = false;␊ |
335 | ␉}␊ |
336 | ␊ |
337 | setBootGlobals(bvChain);␊ |
338 | ␊ |
339 | // Parse args, load and start kernel.␊ |
340 | while (1) {␊ |
341 | const char *val;␊ |
342 | int len;␊ |
343 | int trycache;␊ |
344 | long flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
345 | int ret = -1;␊ |
346 | void *binary = (void *)kLoadAddr;␊ |
347 | bool tryresume;␊ |
348 | bool tryresumedefault;␊ |
349 | bool forceresume;␊ |
350 | ␊ |
351 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
352 | char bootFileSpec[512];␊ |
353 | ␉␉␊ |
354 | // Initialize globals.␊ |
355 | ␊ |
356 | sysConfigValid = false;␊ |
357 | gErrors = false;␊ |
358 | ␊ |
359 | status = getBootOptions(firstRun);␊ |
360 | firstRun = false;␊ |
361 | if (status == -1) continue;␊ |
362 | ␊ |
363 | ␉␉//Azi: i'm now almost sure that here is the right place to do this! - test (gBootVolume == NULL)␊ |
364 | ␉␉// Turn off any GUI elements, draw background and update VRAM.␊ |
365 | ␉␉if ( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
366 | ␉␉{␊ |
367 | ␉␉␉gui.devicelist.draw = false;␊ |
368 | ␉␉␉gui.bootprompt.draw = false;␊ |
369 | ␉␉␉gui.menu.draw = false;␊ |
370 | ␉␉␉gui.infobox.draw = false;␊ |
371 | ␉␉␉gui.logo.draw = false;␊ |
372 | ␉␉␉drawBackground();␊ |
373 | ␉␉␉updateVRAM();␊ |
374 | ␉␉}␊ |
375 | ␉␉ ␊ |
376 | status = processBootOptions();␊ |
377 | ␉␉// Status == 1 means to chainboot␊ |
378 | ␉␉if ( status ==␉1 ) break;␊ |
379 | ␉␉// Status == -1 means that gBootVolume is NULL. Config file is not mandatory anymore! ␊ |
380 | ␉␉if ( status == -1 )␊ |
381 | ␉␉{␊ |
382 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
383 | ␉␉␉if (gBootVolume == NULL)␊ |
384 | ␉␉␉{␊ |
385 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
386 | ␉␉␉␉␊ |
387 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
388 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
389 | ␉␉␉␉␊ |
390 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
391 | ␉␉␉␉setBootGlobals(bvChain);␊ |
392 | ␉␉␉␉setupDeviceList(&bootInfo->themeConfig);␊ |
393 | ␉␉␉}␊ |
394 | ␉␉␉continue;␊ |
395 | ␉␉}␊ |
396 | ␉␉␊ |
397 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
398 | ␊ |
399 | ␉␉// If cpu handles 64 bit instructions...␊ |
400 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T))␊ |
401 | ␉␉{␊ |
402 | ␉␉␉// use x86_64 kernel arch,...␊ |
403 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
404 | ␉␉}␊ |
405 | ␉␉else␊ |
406 | ␉␉{␊ |
407 | ␉␉␉// else use i386 kernel arch.␊ |
408 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
409 | ␉␉}␊ |
410 | ␉␉// If user override...␊ |
411 | ␉␉if (getValueForKey(kArchKey, &val, &len, &bootInfo->bootConfig))␊ |
412 | ␉␉{␊ |
413 | ␉␉␉// matches i386...␊ |
414 | ␉␉␉if (strncmp(val, "i386", 4) == 0)␊ |
415 | ␉␉␉{␊ |
416 | ␉␉␉␉// use i386 kernel arch.␊ |
417 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
418 | ␉␉␉}␊ |
419 | ␉␉}␊ |
420 | ␉␉␊ |
421 | ␉␉if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {␊ |
422 | ␉␉␉tryresume = true;␊ |
423 | ␉␉␉tryresumedefault = true;␊ |
424 | ␉␉} else {␊ |
425 | ␉␉␉tryresumedefault = false;␊ |
426 | ␉␉}␊ |
427 | ␊ |
428 | ␉␉if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {␊ |
429 | ␉␉␉forceresume = false;␊ |
430 | ␉␉}␊ |
431 | ␉␉␊ |
432 | ␉␉if (forceresume) {␊ |
433 | ␉␉␉tryresume = true;␊ |
434 | ␉␉␉tryresumedefault = false;␊ |
435 | ␉␉}␊ |
436 | ␉␉␊ |
437 | ␉␉while (tryresume) {␊ |
438 | ␉␉␉const char *tmp;␊ |
439 | ␉␉␉BVRef bvr;␊ |
440 | ␉␉␉if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))␊ |
441 | ␉␉␉␉val="/private/var/vm/sleepimage";␊ |
442 | ␉␉␉␊ |
443 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
444 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
445 | ␊ |
446 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
447 | ␉␉␉␉break;␊ |
448 | ␉␉␉␊ |
449 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
450 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
451 | ␉␉␉␉break;␊ |
452 | ␉␉␉␊ |
453 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
454 | ␉␉␉␉break;␊ |
455 | ␉␉␉␊ |
456 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
457 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);␊ |
458 | ␉␉␉␉break;␊ |
459 | ␉␉␉}␊ |
460 | ␉␉␉␉␊ |
461 | ␉␉␉HibernateBoot((char *)val);␊ |
462 | ␉␉␉break;␊ |
463 | ␉␉}␊ |
464 | ␊ |
465 | // Reset cache name.␊ |
466 | bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
467 | ␊ |
468 | sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
469 | ␊ |
470 | adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
471 | ␊ |
472 | if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {␊ |
473 | strlcpy(gBootKernelCacheFile, val, len+1);␊ |
474 | } else {␊ |
475 | sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);␊ |
476 | }␊ |
477 | ␊ |
478 | // Check for cache file.␊ |
479 | trycache = (((gBootMode & kBootModeSafe) == 0) &&␊ |
480 | !gOverrideKernel &&␊ |
481 | (gBootFileType == kBlockDeviceType) &&␊ |
482 | (gMKextName[0] == '\0') &&␊ |
483 | (gBootKernelCacheFile[0] != '\0'));␊ |
484 | ␊ |
485 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
486 | ␉␉␊ |
487 | if (trycache) do {␊ |
488 | ␊ |
489 | // if we haven't found the kernel yet, don't use the cache␊ |
490 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
491 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
492 | trycache = 0;␊ |
493 | break;␊ |
494 | }␊ |
495 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
496 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
497 | || (cachetime < kerneltime)) {␊ |
498 | trycache = 0;␊ |
499 | break;␊ |
500 | }␊ |
501 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
502 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
503 | && (cachetime < exttime)) {␊ |
504 | trycache = 0;␊ |
505 | break;␊ |
506 | }␊ |
507 | if (kerneltime > exttime) {␊ |
508 | exttime = kerneltime;␊ |
509 | }␊ |
510 | if (cachetime != (exttime + 1)) {␊ |
511 | trycache = 0;␊ |
512 | break;␊ |
513 | }␊ |
514 | } while (0);␊ |
515 | ␊ |
516 | do {␊ |
517 | if (trycache) {␊ |
518 | bootFile = gBootKernelCacheFile;␊ |
519 | verbose("Loading kernel cache %s\n", bootFile);␊ |
520 | ret = LoadFile(bootFile);␊ |
521 | binary = (void *)kLoadAddr;␊ |
522 | if (ret >= 0) {␊ |
523 | break;␊ |
524 | }␊ |
525 | }␊ |
526 | bootFile = bootInfo->bootFile;␊ |
527 | ␊ |
528 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
529 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
530 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
531 | ␉ ␉ if (ret == -1)␊ |
532 | ␉ ␉ {␊ |
533 | sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
534 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
535 | if (ret == -1)␊ |
536 | {␊ |
537 | sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
538 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
539 | if (ret == -1)␊ |
540 | {␊ |
541 | // Not found any alternate locations, using the original kernel image path.␊ |
542 | strcpy(bootFileSpec, bootFile);␊ |
543 | }␊ |
544 | }␊ |
545 | }␊ |
546 | ␉␉␉␊ |
547 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
548 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
549 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
550 | {␊ |
551 | archCpuType = CPU_TYPE_I386;␊ |
552 | ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
553 | }␊ |
554 | ␉␉␉␊ |
555 | } while (0);␊ |
556 | ␊ |
557 | clearActivityIndicator();␊ |
558 | #if DEBUG␊ |
559 | printf("Pausing...");␊ |
560 | sleep(8);␊ |
561 | #endif␊ |
562 | ␊ |
563 | if (ret <= 0) {␊ |
564 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
565 | ␊ |
566 | ␉␉␉sleep(1);␊ |
567 | ␉␉␉␊ |
568 | if (gBootFileType == kNetworkDeviceType) {␊ |
569 | // Return control back to PXE. Don't unload PXE base code.␊ |
570 | gUnloadPXEOnExit = false;␊ |
571 | break;␊ |
572 | }␊ |
573 | } else {␊ |
574 | /* Won't return if successful. */␊ |
575 | ret = ExecKernel(binary);␊ |
576 | }␊ |
577 | }␊ |
578 | ␊ |
579 | // chainboot␊ |
580 | if (status==1) {␊ |
581 | ␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
582 | ␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
583 | ␉}␊ |
584 | }␊ |
585 | ␉␊ |
586 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {␊ |
587 | ␉nbpUnloadBaseCode();␊ |
588 | }␊ |
589 | }␊ |
590 | ␊ |
591 | /*!␊ |
592 | Selects a new BIOS device, taking care to update the global state appropriately.␊ |
593 | */␊ |
594 | /*␊ |
595 | static void selectBiosDevice(void)␊ |
596 | {␊ |
597 | struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
598 | CacheReset();␊ |
599 | diskFreeMap(oldMap);␊ |
600 | oldMap = NULL;␊ |
601 | ␊ |
602 | int dev = selectAlternateBootDevice(gBIOSDev);␊ |
603 | ␊ |
604 | BVRef bvchain = scanBootVolumes(dev, 0);␊ |
605 | BVRef bootVol = selectBootVolume(bvchain);␊ |
606 | gBootVolume = bootVol;␊ |
607 | setRootVolume(bootVol);␊ |
608 | gBIOSDev = dev;␊ |
609 | }␊ |
610 | */␊ |
611 | ␊ |
612 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
613 | #define NMAX 5000␊ |
614 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
615 | ␊ |
616 | #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}␊ |
617 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);␊ |
618 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);␊ |
619 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);␊ |
620 | #define DO16(buf) DO8(buf,0); DO8(buf,8);␊ |
621 | ␊ |
622 | unsigned long Adler32(unsigned char *buf, long len)␊ |
623 | {␊ |
624 | unsigned long s1 = 1; // adler & 0xffff;␊ |
625 | unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
626 | unsigned long result;␊ |
627 | int k;␊ |
628 | ␊ |
629 | while (len > 0) {␊ |
630 | k = len < NMAX ? len : NMAX;␊ |
631 | len -= k;␊ |
632 | while (k >= 16) {␊ |
633 | DO16(buf);␊ |
634 | buf += 16;␊ |
635 | k -= 16;␊ |
636 | }␊ |
637 | if (k != 0) do {␊ |
638 | s1 += *buf++;␊ |
639 | s2 += s1;␊ |
640 | } while (--k);␊ |
641 | s1 %= BASE;␊ |
642 | s2 %= BASE;␊ |
643 | }␊ |
644 | result = (s2 << 16) | s1;␊ |
645 | return OSSwapHostToBigInt32(result);␊ |
646 | }␊ |
647 | |