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