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