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 | //#define DEBUG 1␊ |
53 | ␊ |
54 | //#include "bootstruct.h"␊ |
55 | //#include "libsa.h"␊ |
56 | #include "boot.h"␊ |
57 | //#include "io_inline.h" // Lion␊ |
58 | #include "ramdisk.h" // bootstruct.h: memory.h instead of saio_internal.h␊ |
59 | #include "gui.h"␊ |
60 | #include "modules.h"␊ |
61 | #include "fake_efi.h"␊ |
62 | #include "platform.h" // bootstruct.h - device_tree.h␊ |
63 | #include "sl.h" // bootstruct.h: memory.h instead of saio_types.h␊ |
64 | ␊ |
65 | long gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
66 | bool gOverrideKernel;␊ |
67 | //--- testing␊ |
68 | #define PLATFORM_NAME_LEN 64␊ |
69 | #define ROOT_PATH_LEN 256␊ |
70 | static char gCacheNameAdler[PLATFORM_NAME_LEN + ROOT_PATH_LEN];␊ |
71 | #define BOOT_DEVICE_PATH "\\System\\Library\\CoreServices\\boot.efi"␊ |
72 | static char gBootKernelCacheFile[512];␊ |
73 | //---␊ |
74 | char *gPlatformName = gCacheNameAdler; // disabled ??␊ |
75 | char gRootDevice[512];␊ |
76 | char gMKextName[512];␊ |
77 | bool gEnableCDROMRescan;␊ |
78 | bool gScanSingleDrive;␊ |
79 | ␊ |
80 | int bvCount = 0; // global ?? - Slice␊ |
81 | //int␉␉menucount = 0;␊ |
82 | int gDeviceCount = 0;␊ |
83 | ␊ |
84 | BVRef bvr;␊ |
85 | //BVRef menuBVR; - doesn't seem used here␊ |
86 | BVRef bvChain;␊ |
87 | bool useGUI;␊ |
88 | ␊ |
89 | //static void selectBiosDevice(void);␊ |
90 | //Azi: this doesn't match the function; matches the "Alder32" on drivers.c␊ |
91 | static unsigned long Adler32(unsigned char *buffer, long length);␊ |
92 | ␊ |
93 | ␊ |
94 | static bool gUnloadPXEOnExit = false;␊ |
95 | ␊ |
96 | /*␊ |
97 | * How long to wait (in seconds) to load the␊ |
98 | * kernel after displaying the "boot:" prompt.␊ |
99 | */␊ |
100 | #define kBootErrorTimeout 5␊ |
101 | ␊ |
102 | /*␊ |
103 | * Default path to kernel cache file␊ |
104 | */␊ |
105 | // OS X 10.5 & 10.4␊ |
106 | #define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"␊ |
107 | // OS X 10.6␊ |
108 | #define kDefaultCachePathSnow "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"␊ |
109 | ␊ |
110 | //==========================================================================␊ |
111 | // Zero the BSS.␊ |
112 | ␊ |
113 | static void zeroBSS(void)␊ |
114 | {␊ |
115 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
116 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
117 | ␊ |
118 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
119 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
120 | }␊ |
121 | ␊ |
122 | //==========================================================================␊ |
123 | // Malloc error function␊ |
124 | ␊ |
125 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
126 | {␊ |
127 | ␉stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);␊ |
128 | }␊ |
129 | ␊ |
130 | //==========================================================================␊ |
131 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
132 | //␊ |
133 | void initialize_runtime(void)␊ |
134 | {␊ |
135 | ␉zeroBSS();␊ |
136 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
137 | }␊ |
138 | ␊ |
139 | //==========================================================================␊ |
140 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
141 | ␊ |
142 | //Azi:autoresolution␊ |
143 | extern void initAutoRes();␊ |
144 | extern void finishAutoRes();␊ |
145 | ␊ |
146 | static int ExecKernel(void *binary)␊ |
147 | {␊ |
148 | entry_t kernelEntry;␊ |
149 | int ret;␊ |
150 | ␊ |
151 | bootArgs->kaddr = bootArgs->ksize = 0;␊ |
152 | ␉//Azi: here we get to know if we have a kernel or a prelinked kernel␊ |
153 | ret = DecodeKernel(binary,␊ |
154 | &kernelEntry,␊ |
155 | (char **) &bootArgs->kaddr,␊ |
156 | (int *)&bootArgs->ksize );␊ |
157 | ␊ |
158 | if ( ret != 0 )␊ |
159 | return ret;␊ |
160 | ␊ |
161 | // Reserve space for boot args␊ |
162 | reserveKernBootStruct();␊ |
163 | ␊ |
164 | //Azi: ...␊ |
165 | // Load boot drivers from the specifed root path,␊ |
166 | // if we don't have a prelinked kernel - check load.c 43 & 264␊ |
167 | if (!gHaveKernelCache) {␊ |
168 | LoadDrivers("/");␊ |
169 | }␊ |
170 | ␊ |
171 | clearActivityIndicator();␊ |
172 | ␊ |
173 | if (gErrors) {␊ |
174 | printf("Errors encountered while starting up the computer.\n");␊ |
175 | printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
176 | sleep(kBootErrorTimeout);␊ |
177 | }␊ |
178 | ␊ |
179 | setupFakeEfi(); //Azi: check position on Mek (plkernel)␊ |
180 | ␊ |
181 | md0Ramdisk();␊ |
182 | ␊ |
183 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
184 | ␊ |
185 | // Cleanup the PXE base code.␊ |
186 | ␊ |
187 | if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {␊ |
188 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
189 | {␊ |
190 | ␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
191 | sleep(2);␊ |
192 | }␊ |
193 | }␊ |
194 | ␊ |
195 | bool dummyVal;␊ |
196 | ␊ |
197 | ␉//Azi: Wait=y is breaking other keys when typed "after them" at boot prompt.␊ |
198 | ␉// Works properly if typed in first place or used on Boot.plist.␊ |
199 | ␉if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {␊ |
200 | ␉␉verbose("(Wait) ");␊ |
201 | ␉␉pause();␊ |
202 | ␉}␊ |
203 | ␊ |
204 | ␉usb_loop();␊ |
205 | ␊ |
206 | //autoresolution - Check if user disabled AutoResolution at the boot prompt.␊ |
207 | ␉// we can't check the plist here if we have AutoResolution=n there and we anabled it␊ |
208 | ␉// at boot prompt...?????␊ |
209 | //␉getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);␊ |
210 | ␉␊ |
211 | ␉finishAutoRes();␊ |
212 | ␉␊ |
213 | ␉//Azi: closing Vbios after "if (gVerboseMode)" stuff eliminates the need for setting␊ |
214 | ␉// AutoResolution = true above; but creates another bug when booting in TextMode with -v arg.␊ |
215 | ␉// Simptoms include: staring some seconds at a nicely drawn white screen, after boot prompt.␊ |
216 | ␉// Think i'm just going to end up removing setting gAutoResolution = false␊ |
217 | ␉// on closeVbios().. the more i think, the less sense it makes doing it there!!␊ |
218 | //autoresolution - end␊ |
219 | ␊ |
220 | // If we were in text mode, switch to graphics mode.␊ |
221 | // This will draw the boot graphics unless we are in␊ |
222 | // verbose mode.␊ |
223 | ␊ |
224 | if(gVerboseMode)␊ |
225 | setVideoMode( GRAPHICS_MODE, 0 );␊ |
226 | else␊ |
227 | drawBootGraphics();␊ |
228 | ␊ |
229 | ␉// Notify modules that the kernel is about to be started␊ |
230 | ␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);␊ |
231 | ␊ |
232 | ␉setupBooterLog();␊ |
233 | ␉␊ |
234 | finalizeBootStruct();␊ |
235 | ␊ |
236 | // Jump to kernel's entry point. There's no going back now.␊ |
237 | //Azi: Lion - http://netkas.org/?p=745␊ |
238 | // see asm.s - same stuff by DHP http://www.insanelymac.com/forum/index.php?s=&showtopic=255866&view=findpost&p=1677779␊ |
239 | //␉outb(0x21, 0xff);␊ |
240 | //␉outb(0xa1, 0xff);␊ |
241 | startprog( kernelEntry, bootArgs );␊ |
242 | ␊ |
243 | // Not reached␊ |
244 | ␊ |
245 | return 0;␊ |
246 | }␊ |
247 | ␊ |
248 | //==========================================================================␊ |
249 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
250 | // before. Multiboot does its own runtime initialization, does some of its␊ |
251 | // own things, and then calls common_boot.␊ |
252 | void boot(int biosdev)␊ |
253 | {␊ |
254 | ␉initialize_runtime();␊ |
255 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
256 | ␉enableA20();␊ |
257 | ␉common_boot(biosdev);␊ |
258 | }␊ |
259 | ␊ |
260 | //==========================================================================␊ |
261 | // The 'main' function for the booter. Called by boot0 when booting␊ |
262 | // from a block device, or by the network booter.␊ |
263 | //␊ |
264 | // arguments:␊ |
265 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
266 | // that the booter was loaded from.␊ |
267 | //␊ |
268 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
269 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
270 | // next boot device on its list.␊ |
271 | void common_boot(int biosdev)␊ |
272 | {␊ |
273 | int status;␊ |
274 | char *bootFile;␊ |
275 | unsigned long adler32;␊ |
276 | bool quiet;␊ |
277 | bool firstRun = true;␊ |
278 | bool instantMenu;␊ |
279 | bool rescanPrompt = false;␊ |
280 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
281 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
282 | ␊ |
283 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
284 | // the base code will result in a hang or kernel panic.␊ |
285 | gUnloadPXEOnExit = true;␊ |
286 | ␊ |
287 | // Record the device that the booter was loaded from.␊ |
288 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
289 | ␊ |
290 | // Initialize boot info structure.␊ |
291 | initKernBootStruct();␊ |
292 | ␊ |
293 | ␉initBooterLog();␊ |
294 | ␉␊ |
295 | ␉//Azi: log booter version, revision & build date, for bdmesg.␊ |
296 | ␉msglog(bootLogBanner);␊ |
297 | ␊ |
298 | // Setup VGA text mode.␊ |
299 | // Not sure if it is safe to call setVideoMode() before the␊ |
300 | // config table has been loaded. Call video_mode() instead.␊ |
301 | #if DEBUG␊ |
302 | printf("before video_mode\n"); //Azi: this one is not printing... i remember it did.. check trunk.␊ |
303 | #endif␊ |
304 | video_mode( 2 ); // 80x25 mono text mode.␊ |
305 | #if DEBUG␊ |
306 | printf("after video_mode\n");␊ |
307 | #endif␊ |
308 | ␊ |
309 | // Scan and record the system's hardware information.␊ |
310 | scan_platform();␊ |
311 | ␊ |
312 | // First get info for boot volume.␊ |
313 | scanBootVolumes(gBIOSDev, 0);␊ |
314 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
315 | ␉//Azi: initialising gBIOSBootVolume & gBootVolume for the first time.. i think!?␊ |
316 | ␉// also, kDefaultPartitionKey is checked here, on selectBootVolume.␊ |
317 | setBootGlobals(bvChain);␊ |
318 | ␉msglog("setBootGlobals:\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
319 | msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
320 | ␊ |
321 | ␉// Boot Volume is set as Root at this point so, pointing to Extra, /Extra or bt(0,0)/Extra␊ |
322 | ␉// is exactly the same.␉Review bt(0,0)/bla bla paths......␉␉␉(Reviewing...)␊ |
323 | ␉␊ |
324 | ␉//Azi: works as expected but... trying this because Kernel=mach_kernel doesn't work on a␊ |
325 | ␉// override Boot.plist; this makes it impossible to override e.g. Kernel=bt(0,0)mach_kernel␊ |
326 | ␉// on the main Boot.plist, when loading kernel from ramdisk btAliased.␊ |
327 | ␉loadPrebootRAMDisk();␊ |
328 | ␉␊ |
329 | // Load boot.plist config file␊ |
330 | status = loadSystemConfig(&bootInfo->bootConfig);␊ |
331 | ␊ |
332 | if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {␊ |
333 | gBootMode |= kBootModeQuiet;␊ |
334 | }␊ |
335 | ␊ |
336 | // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
337 | if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {␊ |
338 | firstRun = false;␊ |
339 | }␊ |
340 | ␊ |
341 | ␉// Loading preboot ramdisk if exists.␊ |
342 | //␉loadPrebootRAMDisk(); //Azi: this needs to be done before load_all_modules()␊ |
343 | ␉// because of btAlias...␉␉␉(Reviewing...)␊ |
344 | ␊ |
345 | ␉// Intialize module system␊ |
346 | ␉if (init_module_system())␊ |
347 | ␉{␊ |
348 | ␉␉load_all_modules();␊ |
349 | ␉}␊ |
350 | ␊ |
351 | // Disable rescan option by default␊ |
352 | gEnableCDROMRescan = false;␊ |
353 | ␊ |
354 | // If we're loading the booter from optical media...␉␉␉(Reviewing...)␊ |
355 | ␉if (biosDevIsCDROM(gBIOSDev))␊ |
356 | ␉{␊ |
357 | ␉␉// ... ask the user for Rescan option by setting "Rescan Prompt"=y in system config...␊ |
358 | ␉␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt, &bootInfo->bootConfig) && rescanPrompt)␊ |
359 | ␉␉{␊ |
360 | ␉ gEnableCDROMRescan = promptForRescanOption();␊ |
361 | ␉ }␊ |
362 | ␉␉else // ... or enable it with Rescan=y in system config.␊ |
363 | ␉ if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)␊ |
364 | ␉␉{␊ |
365 | ␉ gEnableCDROMRescan = true;␊ |
366 | ␉ }␊ |
367 | ␉}␊ |
368 | ␊ |
369 | ␉//Azi: Is this a cdrom only thing?␉␉␉(Reviewing...)␊ |
370 | // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
371 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)␊ |
372 | ␉{␊ |
373 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
374 | }␊ |
375 | ␉else␊ |
376 | ␉{␊ |
377 | ␉␉//Azi: scanDisks uses scanBootVolumes.␊ |
378 | ␉␉scanDisks(gBIOSDev, &bvCount);␊ |
379 | ␉}␊ |
380 | ␉␊ |
381 | // Create a separated bvr chain using the specified filters.␊ |
382 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
383 | ␊ |
384 | gBootVolume = selectBootVolume(bvChain);␊ |
385 | ␊ |
386 | //#if DEBUG␊ |
387 | //printf␊ |
388 | msglog(":something...???\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
389 | msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
390 | // getc();␊ |
391 | //#endif␊ |
392 | ␊ |
393 | useGUI = true;␊ |
394 | // Override useGUI default␊ |
395 | getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);␊ |
396 | ␊ |
397 | ␉// AutoResolution - Azi: default to false␊ |
398 | ␉// http://forum.voodooprojects.org/index.php/topic,1227.0.html␊ |
399 | ␉gAutoResolution = false;␊ |
400 | ␉␊ |
401 | ␉// Check if user enabled AutoResolution on Boot.plist...␊ |
402 | ␉getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);␊ |
403 | ␉␊ |
404 | ␉// Patch the Video Bios with the extracted resolution, before initGui.␊ |
405 | ␉if (gAutoResolution == true)␊ |
406 | ␉{␊ |
407 | ␉␉initAutoRes();␊ |
408 | ␉}␊ |
409 | ␊ |
410 | if (useGUI && initGUI())␊ |
411 | ␉{␊ |
412 | ␉␉// initGUI() returned with an error, disabling GUI.␊ |
413 | ␉␉useGUI = false;␊ |
414 | ␉}␊ |
415 | ␊ |
416 | setBootGlobals(bvChain);␊ |
417 | ␉msglog("setBootGlobals:\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
418 | msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
419 | ␊ |
420 | // Parse args, load and start kernel.␊ |
421 | while (1) {␊ |
422 | const char *val;␊ |
423 | int len;␊ |
424 | int trycache;␊ |
425 | ␉␉long flags, cachetime, kerneltime, exttime, sleeptime, time;␊ |
426 | int ret = -1;␊ |
427 | void *binary = (void *)kLoadAddr;␊ |
428 | bool tryresume;␊ |
429 | bool tryresumedefault;␊ |
430 | bool forceresume;␊ |
431 | ␉␉bool ignoreKC = false;␊ |
432 | ␊ |
433 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
434 | char bootFileSpec[512];␊ |
435 | ␉␉␊ |
436 | // Initialize globals.␊ |
437 | ␊ |
438 | sysConfigValid = false;␊ |
439 | gErrors = false;␊ |
440 | ␊ |
441 | status = getBootOptions(firstRun);␊ |
442 | firstRun = false;␊ |
443 | if (status == -1) continue;␊ |
444 | ␊ |
445 | ␉␉//Azi: test (gBootVolume == NULL) - so far Ok!␊ |
446 | ␉␉// test with optical media again...?␊ |
447 | ␉␉// Turn off any GUI elements, draw background and update VRAM.␊ |
448 | ␉␉if ( bootArgs->Video.v_display == GRAPHICS_MODE )␊ |
449 | ␉␉{␊ |
450 | ␉␉␉gui.devicelist.draw = false;␊ |
451 | ␉␉␉gui.bootprompt.draw = false;␊ |
452 | ␉␉␉gui.menu.draw = false;␊ |
453 | ␉␉␉gui.infobox.draw = false;␊ |
454 | ␉␉␉gui.logo.draw = false;␊ |
455 | ␉␉␉drawBackground();␊ |
456 | ␉␉␉updateVRAM();␊ |
457 | ␉␉}␊ |
458 | ␊ |
459 | ␉␉status = processBootOptions();␊ |
460 | ␊ |
461 | ␉␉//Azi: AutoResolution - closing Vbios here without restoring, causes an allocation error,␊ |
462 | ␉␉// if the user tries to boot, after a e.g."Can't find bla_kernel" msg.␊ |
463 | ␉␉// Doing it on execKernel() instead.␊ |
464 | ␊ |
465 | ␉␉// Status == 1 means to chainboot␊ |
466 | ␉␉if ( status ==␉1 ) break;␊ |
467 | ␉␉␊ |
468 | ␉␉// Status == -1 means that gBootVolume is NULL. Config file is not mandatory anymore! ␊ |
469 | ␉␉if ( status == -1 )␊ |
470 | ␉␉{␊ |
471 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␉␉␉(Reviewing...)␊ |
472 | ␉␉␉if (gBootVolume == NULL)␊ |
473 | ␉␉␉{␊ |
474 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
475 | ␉␉␉␉␊ |
476 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
477 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
478 | ␉␉␉␉␊ |
479 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
480 | ␉␉␉␉setBootGlobals(bvChain);␊ |
481 | ␉␉␉␉setupDeviceList(&bootInfo->themeConfig);␊ |
482 | ␉␉␉}␊ |
483 | ␉␉␉continue;␊ |
484 | ␉␉}␊ |
485 | ␉␉␊ |
486 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
487 | ␊ |
488 | ␉␉// If cpu handles 64 bit instructions...␊ |
489 | ␉␉if (platformCPUFeature(CPU_FEATURE_EM64T))␊ |
490 | ␉␉{␊ |
491 | ␉␉␉// use x86_64 kernel arch,...␊ |
492 | ␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
493 | ␉␉}␊ |
494 | ␉␉else␊ |
495 | ␉␉{␊ |
496 | ␉␉␉// else use i386 kernel arch.␊ |
497 | ␉␉␉archCpuType = CPU_TYPE_I386;␊ |
498 | ␉␉}␊ |
499 | ␉␉// If user override...␊ |
500 | ␉␉if (getValueForKey(kArchKey, &val, &len, &bootInfo->bootConfig))␊ |
501 | ␉␉{␊ |
502 | ␉␉␉// matches i386...␊ |
503 | ␉␉␉if (strncmp(val, "i386", 4) == 0)␊ |
504 | ␉␉␉{␊ |
505 | ␉␉␉␉// use i386 kernel arch.␊ |
506 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
507 | ␉␉␉}␊ |
508 | ␉␉}␊ |
509 | ␉␉␊ |
510 | ␉␉if (!getBoolForKey (kWakeKey, &tryresume, &bootInfo->bootConfig)) {␊ |
511 | ␉␉␉tryresume = true;␊ |
512 | ␉␉␉tryresumedefault = true;␊ |
513 | ␉␉} else {␊ |
514 | ␉␉␉tryresumedefault = false;␊ |
515 | ␉␉}␊ |
516 | ␊ |
517 | ␉␉if (!getBoolForKey (kForceWakeKey, &forceresume, &bootInfo->bootConfig)) {␊ |
518 | ␉␉␉forceresume = false;␊ |
519 | ␉␉}␊ |
520 | ␉␉␊ |
521 | ␉␉if (forceresume) {␊ |
522 | ␉␉␉tryresume = true;␊ |
523 | ␉␉␉tryresumedefault = false;␊ |
524 | ␉␉}␊ |
525 | ␉␉␊ |
526 | ␉␉while (tryresume) {␊ |
527 | ␉␉␉const char *tmp;␊ |
528 | ␉␉␉BVRef bvr;␊ |
529 | ␉␉␉if (!getValueForKey(kWakeKeyImageKey, &val, &len, &bootInfo->bootConfig))␊ |
530 | ␉␉␉␉val="/private/var/vm/sleepimage";␊ |
531 | ␉␉␉␊ |
532 | ␉␉␉// Do this first to be sure that root volume is mounted␊ |
533 | ␉␉␉ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
534 | ␊ |
535 | ␉␉␉if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)␊ |
536 | ␉␉␉␉break;␊ |
537 | ␉␉␉␊ |
538 | ␉␉␉// Can't check if it was hibernation Wake=y is required␊ |
539 | ␉␉␉if (bvr->modTime == 0 && tryresumedefault)␊ |
540 | ␉␉␉␉break;␊ |
541 | ␉␉␉␊ |
542 | ␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
543 | ␉␉␉␉break;␊ |
544 | ␉␉␉␊ |
545 | ␉␉␉if (!forceresume && ((sleeptime+3)<bvr->modTime)) {␊ |
546 | ␉␉␉␉//Azi: no need for printf at this point - reminder␊ |
547 | ␉␉␉␉printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override.\n",bvr->modTime-sleeptime);␊ |
548 | ␉␉␉␉break;␊ |
549 | ␉␉␉}␊ |
550 | ␉␉␉␉␊ |
551 | ␉␉␉HibernateBoot((char *)val);␊ |
552 | ␉␉␉break;␊ |
553 | ␉␉}␊ |
554 | ␊ |
555 | //Azi:kernelcache stuff␊ |
556 | ␉␉//Azi: avoiding having to use -f to ignore kernel cache␊ |
557 | ␉␉//Azi: ignore kernel cache but still use kext cache (E/E.mkext & S/L/E.mkext). - explain...␊ |
558 | ␉␉getBoolForKey(kIgnoreKCKey, &ignoreKC, &bootInfo->bootConfig);␊ |
559 | ␉␉if (ignoreKC)␊ |
560 | ␉␉{␊ |
561 | ␉␉␉verbose("KC: cache ignored by user.\n");␊ |
562 | ␉␉␉// make sure the damn thing get's cleaned, just in case... :)*␊ |
563 | ␉␉␉bzero(gBootKernelCacheFile, sizeof(gBootKernelCacheFile));␊ |
564 | ␉␉}␊ |
565 | ␉␉else if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))␊ |
566 | ␉␉{␊ |
567 | strlcpy(gBootKernelCacheFile, val, len + 1);␊ |
568 | ␉␉␉verbose("KC: path set by user = %s\n", gBootKernelCacheFile);␊ |
569 | ␉␉␉//Azi: bypass time check when user sets path ???␊ |
570 | ␉␉␉// cache is still ignored if time doesn't match... (e.g. usb stick)␊ |
571 | }␊ |
572 | ␉␉else␊ |
573 | ␉␉{␊ |
574 | ␉␉␉// Reset cache name.␊ |
575 | ␉␉␉bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);␊ |
576 | ␉␉␉␊ |
577 | ␉␉␉// kextcache_main.c: Construct entry from UUID of boot volume...(reminder)␊ |
578 | ␉␉␉// assemble ?string? to generate adler from...␊ |
579 | //␉␉␉sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);␊ |
580 | ␉␉␉const char *ProductName = getStringForKey("SMproductname", &bootInfo->smbiosConfig);␊ |
581 | ␉␉␉sprintf(gCacheNameAdler, ProductName); // well, at least the smbios.plist can be loaded this early...␊ |
582 | ␉␉␉// to set/get "ProductName" this early, booter needs complete rewrite!!␊ |
583 | ␉␉␉verbose("KC: gCacheNameAdler 1 = %s\n", gCacheNameAdler);␊ |
584 | ␉␉␉sprintf(gCacheNameAdler + 64, "%s", "\\System\\Library\\CoreServices\\boot.efi");␊ |
585 | ␉␉␉verbose("KC: gCacheNameAdler 2 = %s\n", gCacheNameAdler + 64);␊ |
586 | ␉␉␉sprintf(gCacheNameAdler + (64 + 38), "%s", bootInfo->bootFile);␊ |
587 | ␉␉␉verbose("KC: gCacheNameAdler 3 = %s\n", gCacheNameAdler + (64 + 38));␊ |
588 | ␊ |
589 | ␉␉␉// generate adler␊ |
590 | ␉␉␉adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));␊ |
591 | ␉␉␉verbose("KC: Adler32 = %08X\n", adler32);␊ |
592 | ␉␉␉␊ |
593 | ␉␉␉// append arch and/or adler (checksum) to kc path...␊ |
594 | ␉␉␉if (gMacOSVersion[3] < '6') // change to >= for Lion?␊ |
595 | ␉␉␉{␊ |
596 | ␉␉␉␉sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);␊ |
597 | ␉␉␉␉verbose("KC: adler added to path = %s\n", gBootKernelCacheFile);␊ |
598 | ␉␉␉}␊ |
599 | ␉␉␉else␊ |
600 | ␉␉␉{␊ |
601 | ␉␉␉␉sprintf(gBootKernelCacheFile, "%s_%s.%08X", kDefaultCachePathSnow,␊ |
602 | ␉␉␉␉␉␉(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", adler32);␊ |
603 | ␉␉␉␉verbose("KC: arch & adler added to path = %s\n", gBootKernelCacheFile);␊ |
604 | ␉␉␉}␊ |
605 | }␊ |
606 | ␊ |
607 | // Check for cache file.␊ |
608 | ␉␉//Azi: trycache is done if...␊ |
609 | trycache = ( ( (gBootMode & kBootModeSafe) == 0) //... we're not booting in safe mode (-x arg),␊ |
610 | ␉␉␉␉␉&& !gOverrideKernel // we're not overriding default kernel "name",␊ |
611 | ␉␉␉␉␉&& (gBootFileType == kBlockDeviceType) // we're booting from local storage device,␊ |
612 | ␉␉␉␉␉&& (gMKextName[0] == '\0') // "MKext Cache" key IS NOT in use, and␊ |
613 | ␉␉␉␉␉&& (gBootKernelCacheFile[0] != '\0') ); // gBootKernelCacheFile is populated.␊ |
614 | ␉␉␉␉␉// we could add the use of "kernelpatcher" to this bunch..??␊ |
615 | ␊ |
616 | //␉␉verbose("Loading Darwin %s\n", gMacOSVersion); //Azi: move?? to getOSVersion? :)␊ |
617 | ␉␉␊ |
618 | if (trycache) do␊ |
619 | ␉␉{␊ |
620 | ␉␉␉verbose("KC: checking kernel cache (system prelinked kernel)...\n");␊ |
621 | // if we haven't found the kernel yet, don't use the cache␊ |
622 | ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
623 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
624 | {␊ |
625 | ␉␉␉␉verbose("KC: no kernel found (shouldn't happen?!?)\n");␊ |
626 | trycache = 0; // ignore kernel cache...␊ |
627 | break;␊ |
628 | }␊ |
629 | ␉␉␉verbose("KC: kerneltime = %d\n", kerneltime);␊ |
630 | ␉␉␉␊ |
631 | ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
632 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
633 | || (cachetime < kerneltime))␊ |
634 | {␊ |
635 | ␉␉␉␉if (cachetime <= 100) // confirm: 100 = inexistent path, -xxxxxxxxx = wrong name␊ |
636 | ␉␉␉␉// not confirming... i also get -xxxxxxxxx with inexisting prelinked kernel␊ |
637 | ␉␉␉␉␉verbose("KC: cachetime = %d, kernel cache path/adler is incorrect, ignoring it. ??? \n",␊ |
638 | ␉␉␉␉␉␉␉cachetime);␊ |
639 | ␉␉␉␉else␊ |
640 | ␉␉␉␉␉verbose("KC: cachetime = %d, kernel cache is older than the kernel, ignoring it.\n",␊ |
641 | ␉␉␉␉␉␉␉cachetime);␊ |
642 | trycache = 0;␊ |
643 | break;␊ |
644 | }␊ |
645 | ␉␉␉verbose("KC: cachetime = %d\n", cachetime);␊ |
646 | ␉␉␉␊ |
647 | ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
648 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
649 | && (cachetime < exttime))␊ |
650 | {␊ |
651 | ␉␉␉␉verbose("KC: exttime = %d, kernel cache is older than S/L/E, ignoring it.\n", exttime);␊ |
652 | trycache = 0;␊ |
653 | break;␊ |
654 | }␊ |
655 | ␉␉␉verbose("KC: exttime = %d\n", exttime);␊ |
656 | ␉␉␉␊ |
657 | if (kerneltime > exttime) // if S/L/E is older than the kernel...␊ |
658 | {␊ |
659 | ␉␉␉␉verbose("KC: S/L/E is older than the kernel, matching exttime with kerneltime...\n");␊ |
660 | exttime = kerneltime;␊ |
661 | }␊ |
662 | ␉␉␉verbose("KC: exttime +1 = %d\n", exttime + 1);␊ |
663 | ␉␉␉␊ |
664 | if (cachetime != (exttime + 1))␊ |
665 | {␊ |
666 | ␉␉␉␉verbose("KC: kernel cache time is diff from S/L/E time, ignoring it.\n");␊ |
667 | trycache = 0;␊ |
668 | break;␊ |
669 | }␊ |
670 | ␉␉␉verbose("KC: kernel cache found and up to date, will be used.\n");␊ |
671 | ␊ |
672 | } while (0);␊ |
673 | ␊ |
674 | do␊ |
675 | {␊ |
676 | ␉␉␉// Load kernel cache if not ignored.␊ |
677 | if (trycache)␊ |
678 | {␊ |
679 | bootFile = gBootKernelCacheFile;␊ |
680 | verbose("Loading kernel cache %s\n", bootFile);␊ |
681 | ␊ |
682 | ret = LoadFile(bootFile);␊ |
683 | binary = (void *)kLoadAddr;␊ |
684 | ␊ |
685 | if (ret >= 0)␊ |
686 | {␊ |
687 | break;␊ |
688 | }␊ |
689 | }␊ |
690 | ␊ |
691 | bootFile = bootInfo->bootFile;␊ |
692 | ␊ |
693 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
694 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
695 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
696 | ␉ ␉ if (ret == -1)␊ |
697 | ␉ ␉ {␊ |
698 | ␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
699 | ␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
700 | ␉␉␉␉if (ret == -1)␊ |
701 | ␉␉␉␉{␊ |
702 | ␉␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
703 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
704 | ␉␉␉␉␉if (ret == -1)␊ |
705 | ␉␉␉␉␉{␊ |
706 | ␉␉␉␉␉␉// No alternate locations found, using the original kernel image path.␊ |
707 | ␉␉␉␉␉␉strcpy(bootFileSpec, bootFile);␊ |
708 | ␉␉␉␉␉}␊ |
709 | ␉␉␉␉}␊ |
710 | }␊ |
711 | ␉␉␉␊ |
712 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
713 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
714 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
715 | {␊ |
716 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
717 | ␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
718 | }␊ |
719 | ␉␉␉␊ |
720 | } while (0);␊ |
721 | ␊ |
722 | clearActivityIndicator();␊ |
723 | /*#if DEBUG␊ |
724 | printf("Pausing...");␊ |
725 | sleep(8);␊ |
726 | #endif␊ |
727 | Azi: annoying stuff :P */␊ |
728 | if (ret <= 0)␊ |
729 | ␉␉{␊ |
730 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
731 | ␊ |
732 | ␉␉␉sleep(1);␊ |
733 | ␊ |
734 | if (gBootFileType == kNetworkDeviceType) {␊ |
735 | // Return control back to PXE. Don't unload PXE base code.␊ |
736 | gUnloadPXEOnExit = false;␊ |
737 | break;␊ |
738 | }␊ |
739 | }␊ |
740 | ␉␉else␊ |
741 | ␉␉{␊ |
742 | // Won't return if successful.␊ |
743 | ␉␉␉// Notify modules that ExecKernel is about to be called␊ |
744 | ␉␉␉execute_hook("ExecKernel", binary, NULL, NULL, NULL);␊ |
745 | ␉␉␉␊ |
746 | ret = ExecKernel(binary);␊ |
747 | }␊ |
748 | } // while (1)␊ |
749 | ␊ |
750 | // chainboot␊ |
751 | if (status==1) {␊ |
752 | ␉␉if (getVideoMode() == GRAPHICS_MODE) {␉// if we are already in graphics-mode,␊ |
753 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
754 | ␉␉}␊ |
755 | }␊ |
756 | ␉␊ |
757 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)␊ |
758 | ␉{␊ |
759 | ␉␉nbpUnloadBaseCode();␊ |
760 | }␊ |
761 | }␊ |
762 | ␊ |
763 | /*!␊ |
764 | Selects a new BIOS device, taking care to update the global state appropriately.␊ |
765 | */␊ |
766 | /*␊ |
767 | static void selectBiosDevice(void)␊ |
768 | {␊ |
769 | struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);␊ |
770 | CacheReset();␊ |
771 | diskFreeMap(oldMap);␊ |
772 | oldMap = NULL;␊ |
773 | ␊ |
774 | int dev = selectAlternateBootDevice(gBIOSDev);␊ |
775 | ␊ |
776 | BVRef bvchain = scanBootVolumes(dev, 0);␊ |
777 | BVRef bootVol = selectBootVolume(bvchain);␊ |
778 | gBootVolume = bootVol;␊ |
779 | setRootVolume(bootVol);␊ |
780 | gBIOSDev = dev;␊ |
781 | }␊ |
782 | */␊ |
783 | ␊ |
784 | #define BASE 65521L /* largest prime smaller than 65536 */␊ |
785 | #define NMAX 5000␊ |
786 | // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1␊ |
787 | ␊ |
788 | #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}␊ |
789 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);␊ |
790 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);␊ |
791 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);␊ |
792 | #define DO16(buf) DO8(buf,0); DO8(buf,8);␊ |
793 | ␊ |
794 | unsigned long Adler32(unsigned char *buf, long len)␊ |
795 | {␊ |
796 | unsigned long s1 = 1; // adler & 0xffff;␊ |
797 | unsigned long s2 = 0; // (adler >> 16) & 0xffff;␊ |
798 | unsigned long result;␊ |
799 | int k;␊ |
800 | ␊ |
801 | while (len > 0) {␊ |
802 | k = len < NMAX ? len : NMAX;␊ |
803 | len -= k;␊ |
804 | while (k >= 16) {␊ |
805 | DO16(buf);␊ |
806 | buf += 16;␊ |
807 | k -= 16;␊ |
808 | }␊ |
809 | if (k != 0) do {␊ |
810 | s1 += *buf++;␊ |
811 | s2 += s1;␊ |
812 | } while (--k);␊ |
813 | s1 %= BASE;␊ |
814 | s2 %= BASE;␊ |
815 | }␊ |
816 | result = (s2 << 16) | s1;␊ |
817 | return OSSwapHostToBigInt32(result);␊ |
818 | }␊ |
819 | |