1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /* ␊ |
25 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * All rights reserved. The CMU software License Agreement specifies␊ |
29 | * the terms and conditions for use and redistribution.␊ |
30 | */␊ |
31 | ␊ |
32 | /*␊ |
33 | * INTEL CORPORATION PROPRIETARY INFORMATION␊ |
34 | *␊ |
35 | * This software is supplied under the terms of a license agreement or ␊ |
36 | * nondisclosure agreement with Intel Corporation and may not be copied ␊ |
37 | * nor disclosed except in accordance with the terms of that agreement.␊ |
38 | *␊ |
39 | * Copyright 1988, 1989 by Intel Corporation␊ |
40 | */␊ |
41 | ␊ |
42 | /*␊ |
43 | * Copyright 1993 NeXT Computer, Inc.␊ |
44 | * All rights reserved.␊ |
45 | */␊ |
46 | ␊ |
47 | /*␊ |
48 | * Completely reworked by Sam Streeper (sam_s@NeXT.com)␊ |
49 | * Reworked again by Curtis Galloway (galloway@NeXT.com)␊ |
50 | */␊ |
51 | ␊ |
52 | ␊ |
53 | #include "boot.h"␊ |
54 | #include "bootstruct.h"␊ |
55 | #include "fake_efi.h"␊ |
56 | #include "sl.h"␊ |
57 | #include "libsa.h"␊ |
58 | #include "platform.h"␊ |
59 | #include "graphics.h"␊ |
60 | #ifndef OPTION_ROM␊ |
61 | #include "appleboot.h"␊ |
62 | #endif␊ |
63 | #include "modules.h"␊ |
64 | #include "xml.h"␊ |
65 | ␊ |
66 | #ifndef DEBUG_BOOT␊ |
67 | #define DEBUG_BOOT 0␊ |
68 | #endif␊ |
69 | ␊ |
70 | #if DEBUG_BOOT␊ |
71 | #define DBG(x...)␉␉printf(x)␊ |
72 | #else␊ |
73 | #define DBG(x...)␉␉␊ |
74 | #endif␊ |
75 | ␊ |
76 | ␊ |
77 | typedef struct platform_info {␊ |
78 | ␉char platformName[PLATFORM_NAME_LEN];␊ |
79 | ␉char rootPath[ROOT_PATH_LEN];␊ |
80 | } PlatformInfo;␊ |
81 | ␊ |
82 | ␊ |
83 | char *gboardproduct = NULL;␊ |
84 | char *gPlatformName = NULL;␊ |
85 | //char gRootPath[256];␊ |
86 | long gBootMode; /* defaults to 0 == kBootModeNormal */␊ |
87 | bool gOverrideKernel;␊ |
88 | char gBootKernelCacheFile[512];␊ |
89 | char gMKextName[512];␊ |
90 | char gMacOSVersion[8];␊ |
91 | char *gRootDevice = NULL;␊ |
92 | ␊ |
93 | #ifndef OPTION_ROM␊ |
94 | bool gEnableCDROMRescan;␊ |
95 | bool gScanSingleDrive;␊ |
96 | #endif␊ |
97 | ␊ |
98 | int bvCount = 0;␊ |
99 | //int␉menucount = 0;␊ |
100 | int gDeviceCount = 0; ␊ |
101 | ␊ |
102 | BVRef bvr;␊ |
103 | BVRef menuBVR;␊ |
104 | BVRef bvChain;␊ |
105 | static bool forcecache = false;␊ |
106 | ␊ |
107 | static void zeroBSS(void);␊ |
108 | #ifdef SAFE_MALLOC␊ |
109 | static inline void malloc_error(char *addr, size_t size, const char *file, int line);␊ |
110 | #else␊ |
111 | static inline void malloc_error(char *addr, size_t size);␊ |
112 | #endif␊ |
113 | static int ExecKernel(void *binary);␊ |
114 | static bool getOSVersion(char *str);␊ |
115 | static void getRootDevice();␊ |
116 | #ifdef NBP_SUPPORT␊ |
117 | static bool gUnloadPXEOnExit = false;␊ |
118 | #endif␊ |
119 | static bool find_file_with_ext(const char* dir, const char *ext, const char * name_compare, size_t ext_size);␊ |
120 | static bool found_extra_kext(void);␊ |
121 | static void determineCpuArch(void);␊ |
122 | ␊ |
123 | void getKernelCachePath();␊ |
124 | ␊ |
125 | ␊ |
126 | /*␊ |
127 | * How long to wait (in seconds) to load the␊ |
128 | * kernel after displaying the "boot:" prompt.␊ |
129 | */␊ |
130 | #define kBootErrorTimeout 5␊ |
131 | ␊ |
132 | /*␊ |
133 | * Default path to kernel cache file␊ |
134 | */␊ |
135 | #define kDefaultCachePath "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"␊ |
136 | ␊ |
137 | //==========================================================================␊ |
138 | // Zero the BSS.␊ |
139 | ␊ |
140 | static void zeroBSS(void)␊ |
141 | {␊ |
142 | ␉extern char _DATA__bss__begin, _DATA__bss__end;␊ |
143 | ␉extern char _DATA__common__begin, _DATA__common__end;␊ |
144 | ␉␊ |
145 | ␉bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));␊ |
146 | ␉bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));␊ |
147 | }␊ |
148 | ␊ |
149 | //==========================================================================␊ |
150 | // Malloc error function␊ |
151 | ␊ |
152 | #ifdef SAFE_MALLOC␊ |
153 | static inline void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
154 | {␊ |
155 | stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);␊ |
156 | }␊ |
157 | #else␊ |
158 | static inline void malloc_error(char *addr, size_t size)␊ |
159 | {␊ |
160 | printf("\nMemory allocation error (0x%x, 0x%x)\n", (unsigned)addr, (unsigned)size);␊ |
161 | asm volatile ("hlt");␊ |
162 | }␊ |
163 | #endif␊ |
164 | ␊ |
165 | //==========================================================================␊ |
166 | //Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.␊ |
167 | //␊ |
168 | void initialize_runtime(void)␊ |
169 | {␊ |
170 | ␉zeroBSS();␊ |
171 | ␉malloc_init(0, 0, 0, malloc_error);␊ |
172 | }␊ |
173 | ␊ |
174 | //==========================================================================␊ |
175 | // execKernel - Load the kernel image (mach-o) and jump to its entry point.␊ |
176 | ␊ |
177 | static int ExecKernel(void *binary)␊ |
178 | {␊ |
179 | entry_t kernelEntry;␊ |
180 | int ret;␊ |
181 | ␉␊ |
182 | bootArgs->kaddr = bootArgs->ksize = 0;␊ |
183 | ␉␊ |
184 | ␉if(gMacOSVersion[3] <= '6')␊ |
185 | ␉{␊ |
186 | ␉␉bootArgs->Version = kBootArgsVersion1;␉␉␊ |
187 | ␉␉bootArgs->Revision = gMacOSVersion[3];␉␊ |
188 | ␉}␊ |
189 | ␉else ␊ |
190 | ␉{␉␉␊ |
191 | #if kBootArgsVersion > 1␊ |
192 | ␉␉␊ |
193 | ␉␉bootArgs->Version = kBootArgsVersion;␉␉␊ |
194 | ␉␉bootArgs->Revision = kBootArgsRevision;␊ |
195 | #else␊ |
196 | ␉␉bootArgs->Version = 2;␊ |
197 | bootArgs->Revision = 0;␊ |
198 | #endif␊ |
199 | ␉}␊ |
200 | ␉␊ |
201 | ␉execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL, NULL, NULL);␊ |
202 | ␊ |
203 | ret = DecodeKernel(binary,␊ |
204 | &kernelEntry,␊ |
205 | (char **) &bootArgs->kaddr,␊ |
206 | (int *)&bootArgs->ksize );␊ |
207 | ␊ |
208 | if ( ret != 0 )␊ |
209 | return ret;␊ |
210 | ␉␊ |
211 | // Reserve space for boot args for 10.7 only (for 10.6 and earlier, we will convert (to legacy) the structure and reserve kernel memory for it later.)␊ |
212 | ␉if(gMacOSVersion[3] == '7')␊ |
213 | reserveKernBootStruct();␊ |
214 | ␉␊ |
215 | // Load boot drivers from the specifed root path.␊ |
216 | ␉␊ |
217 | if (!gHaveKernelCache)␊ |
218 | ␉{␊ |
219 | ␉␉LoadDrivers("/");␊ |
220 | }␊ |
221 | ␉␊ |
222 | #if TEXT_SPINNER␊ |
223 | ␉clearActivityIndicator();␊ |
224 | #endif␊ |
225 | ␉␊ |
226 | if (gErrors)␊ |
227 | ␉{␊ |
228 | printf("Errors encountered while starting up the computer.\n");␊ |
229 | #if DEBUG_BOOT␊ |
230 | printf("Pausing %d seconds...\n", kBootErrorTimeout);␊ |
231 | sleep(kBootErrorTimeout);␊ |
232 | #endif␊ |
233 | }␊ |
234 | ␉␊ |
235 | ␉execute_hook("md0Ramdisk", NULL, NULL, NULL, NULL, NULL, NULL);␊ |
236 | ␊ |
237 | setupFakeEfi();␊ |
238 | ␉␉␊ |
239 | verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");␊ |
240 | #ifdef NBP_SUPPORT␊ |
241 | // Cleanup the PXE base code.␊ |
242 | ␉␊ |
243 | if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )␊ |
244 | ␉{␊ |
245 | ␉␉if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )␊ |
246 | {␊ |
247 | ␉printf("nbpUnloadBaseCode error %d\n", (int) ret);␊ |
248 | sleep(2);␊ |
249 | }␊ |
250 | }␊ |
251 | #endif ␊ |
252 | ␉{␊ |
253 | ␉␉bool wait = false;␊ |
254 | ␉␉const char *strval = 0;␊ |
255 | ␉␉int dummysize /*= 0*/;␉␊ |
256 | ␉␉␊ |
257 | ␉␉getBoolForKey(kWaitForKeypressKey, &wait, &bootInfo->bootConfig);␊ |
258 | ␉␉␊ |
259 | ␉␉if (getValueForBootKey(bootArgs->CommandLine, "-wait", &strval, &dummysize))␊ |
260 | ␉␉{␊ |
261 | ␉␉␉wait = true;␊ |
262 | ␉␉␉␊ |
263 | ␉␉␉if (strval && ((strcmp(strval, "no") == 0) || (strcmp(strval, "No") == 0)))␊ |
264 | ␉␉␉{␊ |
265 | ␉␉␉␉wait = false;␊ |
266 | ␉␉␉} ␉␉␊ |
267 | ␉␉}␊ |
268 | ␉␉␊ |
269 | ␉␉if (wait == true)␊ |
270 | ␉␉{␊ |
271 | ␉␉␉pause();␊ |
272 | ␉␉}␊ |
273 | ␉} ␊ |
274 | ␉␉␉␊ |
275 | ␉if ((execute_hook("GUI_ExecKernel", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)) // (bootArgs->Video.v_display == VGA_TEXT_MODE)␉␊ |
276 | ␉{␊ |
277 | #if UNUSED␊ |
278 | ␉␉setVideoMode( GRAPHICS_MODE, 0 );␊ |
279 | #else␊ |
280 | ␉␉setVideoMode( GRAPHICS_MODE );␊ |
281 | #endif␊ |
282 | ␉␉␊ |
283 | #ifndef OPTION_ROM␊ |
284 | ␉␉ ␊ |
285 | ␉␉if(!gVerboseMode)␊ |
286 | ␉␉{␉␉␉␊ |
287 | ␉␉␉drawColorRectangle(0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 0x01); ␊ |
288 | ␉␉␊ |
289 | ␉␉␉uint8_t *appleBootPict; ␊ |
290 | ␉␉␉uint16_t bootImageWidth = kAppleBootWidth; ␊ |
291 | ␉␉␉uint16_t bootImageHeight = kAppleBootHeight; ␊ |
292 | ␉␉␉uint8_t *bootImageData = NULL; ␉␉ ␊ |
293 | ␉␉␉uint16_t x, y;␊ |
294 | ␉␉␉␊ |
295 | ␉␉␉unsigned long screen_params[4] = {DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32, 0};␉// here we store the used screen resolution␊ |
296 | ␉␉␉// Prepare the data for the default Apple boot image. ␊ |
297 | ␉␉␉appleBootPict = (uint8_t *) decodeRLE(gAppleBootPictRLE, kAppleBootRLEBlocks, bootImageWidth * bootImageHeight); ␊ |
298 | ␉␉␉if (appleBootPict)␊ |
299 | ␉␉␉{ ␊ |
300 | ␉␉␉␉convertImage(bootImageWidth, bootImageHeight, appleBootPict, &bootImageData); ␊ |
301 | ␉␉␉␉if (bootImageData)␊ |
302 | ␉␉␉␉{␉␊ |
303 | ␉␉␉␉␉x = (screen_params[0] - MIN(kAppleBootWidth, screen_params[0])) / 2; ␊ |
304 | ␉␉␉␉␉y = (screen_params[1] - MIN(kAppleBootHeight, screen_params[1])) / 2; ␊ |
305 | ␉␉␉␉␉drawDataRectangle(x, y, kAppleBootWidth, kAppleBootHeight, bootImageData);␊ |
306 | ␉␉␉␉␉free(bootImageData);␊ |
307 | ␉␉␉␉}␊ |
308 | ␉␉␉␉free(appleBootPict); ␊ |
309 | ␉␉␉}␊ |
310 | ␉␉␊ |
311 | ␉␉}␊ |
312 | #endif␊ |
313 | ␉}␊ |
314 | ␊ |
315 | finalizeEFIConfigTable();␊ |
316 | ␊ |
317 | ␉setupBooterLog();␊ |
318 | ␉␊ |
319 | finalizeBootStruct(); ␊ |
320 | ␊ |
321 | ␉␊ |
322 | ␉if (gMacOSVersion[3] <= '6') ␊ |
323 | ␉␉reserveKernLegacyBootStruct(); ␉␉␊ |
324 | ␉␊ |
325 | ␉execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL, NULL, NULL);␉// Notify modules that the kernel is about to be started␊ |
326 | #if UNUSED␊ |
327 | ␉turnOffFloppy();␊ |
328 | #endif␊ |
329 | #if BETA␊ |
330 | #include "smp-imps.h"␊ |
331 | #include "apic.h"␊ |
332 | ␉IMPS_LAPIC_WRITE(LAPIC_LVT1, LAPIC_ICR_DM_NMI);␊ |
333 | #endif␊ |
334 | ␉␊ |
335 | ␉if (gMacOSVersion[3] <= '6')␊ |
336 | ␉{␉␉␊ |
337 | ␉␉// Jump to kernel's entry point. There's no going back now. XXX LEGACY OS XXX␊ |
338 | ␉␉startprog( kernelEntry, bootArgsLegacy );␊ |
339 | ␉}␉␊ |
340 | ␉␉␉␊ |
341 | ␉outb(0x21, 0xff); /* Maskout all interrupts Pic1 */␊ |
342 | ␉outb(0xa1, 0xff); /* Maskout all interrupts Pic2 */␊ |
343 | ␉␉␊ |
344 | ␉// Jump to kernel's entry point. There's no going back now. XXX LION XXX␊ |
345 | startprog( kernelEntry, bootArgs );␊ |
346 | ␉␊ |
347 | // Not reached␊ |
348 | ␉␊ |
349 | return 0;␊ |
350 | }␊ |
351 | ␊ |
352 | //==========================================================================␊ |
353 | // This is the entrypoint from real-mode which functions exactly as it did␊ |
354 | // before. Multiboot does its own runtime initialization, does some of its␊ |
355 | // own things, and then calls common_boot.␊ |
356 | void boot(int biosdev)␊ |
357 | {␊ |
358 | ␉initialize_runtime();␊ |
359 | ␉// Enable A20 gate before accessing memory above 1Mb.␊ |
360 | ␉enableA20();␊ |
361 | ␉common_boot(biosdev);␊ |
362 | }␊ |
363 | ␊ |
364 | //==========================================================================␊ |
365 | // The 'main' function for the booter. Called by boot0 when booting␊ |
366 | // from a block device, or by the network booter.␊ |
367 | //␊ |
368 | // arguments:␊ |
369 | // biosdev - Value passed from boot1/NBP to specify the device␊ |
370 | // that the booter was loaded from.␊ |
371 | //␊ |
372 | // If biosdev is kBIOSDevNetwork, then this function will return if␊ |
373 | // booting was unsuccessful. This allows the PXE firmware to try the␊ |
374 | // next boot device on its list.␊ |
375 | void common_boot(int biosdev)␊ |
376 | {␊ |
377 | int status; ␊ |
378 | bool firstRun = true; ␊ |
379 | ␊ |
380 | unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;␊ |
381 | unsigned int denyBVFlags = kBVFlagEFISystem;␊ |
382 | ␉␊ |
383 | #ifdef NBP_SUPPORT␊ |
384 | // Set reminder to unload the PXE base code. Neglect to unload␊ |
385 | // the base code will result in a hang or kernel panic.␊ |
386 | gUnloadPXEOnExit = true;␊ |
387 | #endif␊ |
388 | // Record the device that the booter was loaded from.␊ |
389 | gBIOSDev = biosdev & kBIOSDevMask;␊ |
390 | ␉␊ |
391 | ␉␊ |
392 | ␉␊ |
393 | // Setup VGA text mode.␊ |
394 | // Not sure if it is safe to call setVideoMode() before the␊ |
395 | // config table has been loaded. Call video_mode() instead.␊ |
396 | #if DEBUG␊ |
397 | printf("before video_mode\n");␊ |
398 | #endif␊ |
399 | video_mode( 2 ); // 80x25 mono text mode.␊ |
400 | #if DEBUG␊ |
401 | printf("after video_mode\n");␊ |
402 | #endif␊ |
403 | #if !TEXT_SPINNER␊ |
404 | ␉printf("Starting Chameleon ...\n");␊ |
405 | #endif␊ |
406 | ␉␊ |
407 | ␉initBooterLog();␊ |
408 | ␉␊ |
409 | ␉// Initialize boot info structure.␊ |
410 | initKernBootStruct();␊ |
411 | ␉␊ |
412 | ␉␊ |
413 | // Scan and record the system's hardware information.␊ |
414 | scan_platform();␊ |
415 | ␉␊ |
416 | // First get info for boot volume.␊ |
417 | scanBootVolumes(gBIOSDev, 0);␊ |
418 | bvChain = getBVChainForBIOSDev(gBIOSDev);␊ |
419 | setBootGlobals(bvChain);␊ |
420 | ␉␊ |
421 | // Load Booter boot.plist config file␊ |
422 | status = loadBooterConfig(&bootInfo->bootConfig);␊ |
423 | ␉␊ |
424 | ␉Platform->CPU.isServer = false; ␊ |
425 | getBoolForKey(kIsServer, &Platform->CPU.isServer, &bootInfo->bootConfig); // set this as soon as possible␊ |
426 | ␉␊ |
427 | ␉{␊ |
428 | ␉␉bool quiet = false;␊ |
429 | ␉␉if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet)␊ |
430 | ␉␉{␊ |
431 | ␉␉␉gBootMode |= kBootModeQuiet;␊ |
432 | ␉␉}␊ |
433 | ␉}␉␊ |
434 | ␉␊ |
435 | ␉{␊ |
436 | ␉␉bool instantMenu = false;␊ |
437 | ␉␉// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config␊ |
438 | ␉␉if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu)␊ |
439 | ␉␉{␊ |
440 | ␉␉␉firstRun = false;␊ |
441 | ␉␉}␊ |
442 | ␉}␉␊ |
443 | ␉␊ |
444 | #ifndef OPTION_ROM␊ |
445 | ␉// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.␊ |
446 | if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)␊ |
447 | ␉{␊ |
448 | gScanSingleDrive = true;␊ |
449 | }␊ |
450 | ␉␊ |
451 | ␉// Create a list of partitions on device(s).␊ |
452 | if (gScanSingleDrive)␊ |
453 | ␉{␊ |
454 | ␉␉scanBootVolumes(gBIOSDev, &bvCount);␊ |
455 | } ␊ |
456 | ␉else ␊ |
457 | #endif␊ |
458 | ␉{␊ |
459 | #if UNUSED␊ |
460 | scanDisks(gBIOSDev, &bvCount);␊ |
461 | #else␊ |
462 | scanDisks();␊ |
463 | #endif␊ |
464 | }␊ |
465 | ␉␊ |
466 | ␉// Create a separated bvr chain using the specified filters.␊ |
467 | bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
468 | ␉gBootVolume = selectBootVolume(bvChain);␊ |
469 | ␉␊ |
470 | ␉{␊ |
471 | ␉␉// Intialize module system␊ |
472 | ␉␉EFI_STATUS sysinit = init_module_system();␊ |
473 | ␉␉if((sysinit == EFI_SUCCESS) || (sysinit == EFI_ALREADY_STARTED) /*should never happen*/ ) ␊ |
474 | ␉␉{␊ |
475 | ␉␉␉load_all_modules();␊ |
476 | ␉␉}␊ |
477 | ␉}␉␊ |
478 | ␉␉␉␊ |
479 | // Loading preboot ramdisk if exists.␊ |
480 | ␉execute_hook("loadPrebootRAMDisk", NULL, NULL, NULL, NULL, NULL, NULL);␉␉␊ |
481 | ␊ |
482 | #ifndef OPTION_ROM␊ |
483 | ␊ |
484 | // Disable rescan option by default␊ |
485 | gEnableCDROMRescan = false;␊ |
486 | ␉␊ |
487 | // Enable it with Rescan=y in system config␊ |
488 | if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)␊ |
489 | ␉{␊ |
490 | gEnableCDROMRescan = true;␊ |
491 | }␊ |
492 | ␉␊ |
493 | ␉{␊ |
494 | ␉␉bool rescanPrompt = false;␊ |
495 | ␉␉// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.␊ |
496 | ␉␉if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))␊ |
497 | ␉␉{␊ |
498 | ␉␉␉gEnableCDROMRescan = promptForRescanOption();␊ |
499 | ␉␉}␊ |
500 | ␉}␊ |
501 | ␊ |
502 | #endif␉␊ |
503 | ␉␊ |
504 | #if DEBUG␊ |
505 | printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);␊ |
506 | printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);␊ |
507 | getc();␊ |
508 | #endif␊ |
509 | ␉␉␉␊ |
510 | setBootGlobals(bvChain);␊ |
511 | ␉␊ |
512 | ␉// Display the GUI␊ |
513 | ␉execute_hook("GUI_Display", NULL, NULL, NULL, NULL, NULL, NULL);␊ |
514 | ␊ |
515 | // Parse args, load and start kernel.␊ |
516 | while (1) {␊ |
517 | const char *val;␊ |
518 | int len;␊ |
519 | ␉␉char *bootFile;␊ |
520 | ␉␉bool trycache = true; // Always try to catch the kernelcache first␊ |
521 | ␉␉␊ |
522 | long flags;␊ |
523 | #ifdef BOOT_HELPER_SUPPORT␉␉␊ |
524 | ␉␉long time;␊ |
525 | #endif␊ |
526 | int ret = -1;␊ |
527 | void *binary = (void *)kLoadAddr;␊ |
528 | ␉␉␊ |
529 | // additional variable for testing alternate kernel image locations on boot helper partitions.␊ |
530 | char bootFileSpec[512];␊ |
531 | ␉␉␊ |
532 | // Initialize globals.␊ |
533 | ␉␉␊ |
534 | sysConfigValid = false;␊ |
535 | gErrors = false;␊ |
536 | ␉␉␊ |
537 | status = getBootOptions(firstRun);␊ |
538 | firstRun = false;␊ |
539 | if (status == -1) continue;␊ |
540 | ␉␉␊ |
541 | status = processBootOptions();␊ |
542 | // Status==1 means to chainboot␊ |
543 | if ( status == 1 ) break;␊ |
544 | // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL␊ |
545 | if ( status == -1 )␊ |
546 | {␊ |
547 | ␉␉␉// gBootVolume == NULL usually means the user hit escape.␊ |
548 | ␉␉␉if(gBootVolume == NULL)␊ |
549 | ␉␉␉{␊ |
550 | ␉␉␉␉freeFilteredBVChain(bvChain);␊ |
551 | #ifndef OPTION_ROM␊ |
552 | ␉␉␉␉if (gEnableCDROMRescan)␊ |
553 | ␉␉␉␉␉rescanBIOSDevice(gBIOSDev);␊ |
554 | #endif␊ |
555 | ␉␉␉␉␊ |
556 | ␉␉␉␉bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);␊ |
557 | ␉␉␉␉setBootGlobals(bvChain);␊ |
558 | ␉␉␉}␊ |
559 | ␉␉␉continue;␊ |
560 | }␊ |
561 | ␉␉␉␉␉␉␊ |
562 | // Other status (e.g. 0) means that we should proceed with boot.␊ |
563 | ␉␉execute_hook("GUI_PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);␊ |
564 | ␉␉␊ |
565 | ␉␉// Find out which version mac os we're booting.␊ |
566 | ␉␉getOSVersion(gMacOSVersion);␊ |
567 | ␉␉␊ |
568 | ␉␉if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig) && val)␊ |
569 | ␉␉{␊ |
570 | ␉␉␉if (strncmp(val, "x86_64", 4) == 0)␊ |
571 | ␉␉␉{␊ |
572 | ␉␉␉␉archCpuType = CPU_TYPE_X86_64;␊ |
573 | ␉␉␉} ␊ |
574 | ␉␉␉else if (strncmp(val, "i386", 4) == 0)␊ |
575 | ␉␉␉{␊ |
576 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
577 | ␉␉␉}␊ |
578 | ␉␉␉else ␊ |
579 | ␉␉␉{␊ |
580 | ␉␉␉␉DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");␊ |
581 | ␉␉␉␉determineCpuArch();␊ |
582 | ␉␉␉}␊ |
583 | ␊ |
584 | ␉␉}␊ |
585 | ␉␉else determineCpuArch();␊ |
586 | ␊ |
587 | ␊ |
588 | ␉␉getRootDevice();␊ |
589 | ␊ |
590 | ␉␉// Notify to all modules that we are attempting to boot␊ |
591 | ␉␉execute_hook("PreBoot", NULL, NULL, NULL, NULL, NULL, NULL); ␊ |
592 | ␉␉␊ |
593 | ␉␉if (execute_hook("getProductNamePatched", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)␉␉␉␉␉␊ |
594 | ␉␉␉readSMBIOS(thePlatformName); // read smbios Platform Name␊ |
595 | ␉␉␊ |
596 | ␉␉␊ |
597 | ␉␉if (((gBootMode & kBootModeSafe) == 0) &&␊ |
598 | ␉␉␉!gOverrideKernel &&␊ |
599 | ␉␉␉(gBootFileType == kBlockDeviceType) &&␊ |
600 | ␉␉␉(gMKextName[0] == '\0') && ␊ |
601 | ␉␉␉!getValueForBootKey(bootArgs->CommandLine, kIgnorePrelinkKern, &val, &len))␊ |
602 | ␉␉{ ␉␉␊ |
603 | ␉␉␉getBoolForKey(kUseKernelCache, &trycache, &bootInfo->bootConfig);␊ |
604 | ␉␉␉if (trycache == true)␊ |
605 | ␉␉␉{␊ |
606 | ␉␉␉␉// try to find the cache and fill the gBootKernelCacheFile string␊ |
607 | ␉␉␉␉getKernelCachePath();␊ |
608 | ␉␉␉␉␊ |
609 | ␉␉␉␉// Check for cache file␊ |
610 | ␉␉␉␉trycache = (gBootKernelCacheFile[0] != '\0') ? true : false; // if gBootKernelCacheFile is filled et bla bla bla.... :-)␊ |
611 | ␉␉␉}␊ |
612 | ␉␉␉␊ |
613 | ␉␉}␊ |
614 | ␉␉else ␊ |
615 | ␉␉{␊ |
616 | ␉␉␉trycache = false;␊ |
617 | ␉␉}␊ |
618 | ␉␉␉␉␉␉␊ |
619 | ␉␉verbose("Loading Darwin %s\n", gMacOSVersion);␊ |
620 | ␉␉{␊ |
621 | ␉␉␉long cachetime, kerneltime, exttime;␊ |
622 | ␉␉␉if (trycache && !forcecache) do {␊ |
623 | ␉␉␉␉␊ |
624 | ␉␉␉␉// if we haven't found the kernel yet, don't use the cache␊ |
625 | ␉␉␉␉ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);␊ |
626 | ␉␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
627 | ␉␉␉␉{␊ |
628 | ␉␉␉␉␉trycache = 0;␊ |
629 | ␉␉␉␉␉bootInfo->adler32 = 0;␊ |
630 | ␉␉␉␉␉DBG("No kernel found, kernelcache disabled !!!\n");␊ |
631 | ␉␉␉␉␉break;␊ |
632 | ␉␉␉␉}␊ |
633 | ␉␉␉␉ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
634 | ␉␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)␊ |
635 | ␉␉␉␉␉|| (cachetime < kerneltime))␊ |
636 | ␉␉␉␉{␊ |
637 | ␉␉␉␉␉trycache = 0;␊ |
638 | ␉␉␉␉␉bootInfo->adler32 = 0;␊ |
639 | ␉␉␉␉␉DBG("Warning: No kernelcache found or kernelcache too old (timestamp of the kernel > timestamp of the cache), kernelcache disabled !!!\n");␊ |
640 | ␊ |
641 | ␉␉␉␉␉break;␉␉␉␉ ␊ |
642 | ␉␉␉␉} ␊ |
643 | ␉␉␉␉ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);␊ |
644 | ␉␉␉␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)␊ |
645 | ␉␉␉␉␉&& (cachetime < exttime))␊ |
646 | ␉␉␉␉{␊ |
647 | ␉␉␉␉␉trycache = 0;␊ |
648 | ␉␉␉␉␉bootInfo->adler32 = 0;␊ |
649 | ␉␉␉␉␉DBG("Warning: kernelcache too old, timestamp of S/L/E > timestamp of the cache, kernelcache disabled !!! \n");␊ |
650 | ␊ |
651 | ␉␉␉␉␉break;␊ |
652 | ␉␉␉␉}␊ |
653 | ␉␉␉␉if (kerneltime > exttime)␊ |
654 | ␉␉␉␉{␊ |
655 | ␉␉␉␉␉exttime = kerneltime;␊ |
656 | ␉␉␉␉}␊ |
657 | ␉␉␉␉if (cachetime != (exttime + 1))␊ |
658 | ␉␉␉␉{␊ |
659 | ␉␉␉␉␉trycache = 0;␊ |
660 | ␉␉␉␉␉bootInfo->adler32 = 0;␊ |
661 | ␉␉␉␉␉DBG("Warning: invalid timestamp, kernelcache disabled !!!\n");␊ |
662 | ␊ |
663 | ␉␉␉␉␉break;␊ |
664 | ␉␉␉␉}␊ |
665 | ␉␉␉} while (0);␊ |
666 | ␉␉}␉␉␊ |
667 | ␊ |
668 | do {␊ |
669 | if (trycache)␊ |
670 | ␉␉␉{␊ |
671 | bootFile = gBootKernelCacheFile;␊ |
672 | verbose("Loading kernel cache %s\n", bootFile);␊ |
673 | ␉␉␉␉if (gMacOSVersion[3] == '7')␊ |
674 | ␉␉␉␉{␉␉␉␉␉␊ |
675 | ␉␉␉␉␉ret = LoadThinFatFile(bootFile, &binary);␊ |
676 | ␉␉␉␉␉if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
677 | ␉␉␉␉␉{␊ |
678 | ␉␉␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
679 | ␉␉␉␉␉␉ret = LoadThinFatFile(bootFile, &binary);␉␉␉␉␊ |
680 | ␉␉␉␉␉}␊ |
681 | ␉␉␉␉}␊ |
682 | ␉␉␉␉else␊ |
683 | ␉␉␉␉{␊ |
684 | ␉␉␉␉␉ret = LoadFile(bootFile);␊ |
685 | ␉␉␉␉␉binary = (void *)kLoadAddr;␊ |
686 | ␉␉␉␉}␊ |
687 | ␉␉␉␉␊ |
688 | if (ret >= 0)␊ |
689 | ␉␉␉␉{␊ |
690 | break;␊ |
691 | }␊ |
692 | ␉␉␉␉␊ |
693 | }␊ |
694 | ␉␉␉bootInfo->adler32 = 0;␊ |
695 | bootFile = bootInfo->bootFile;␊ |
696 | #ifdef BOOT_HELPER_SUPPORT␊ |
697 | ␉␉␉␊ |
698 | // Try to load kernel image from alternate locations on boot helper partitions.␊ |
699 | sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);␊ |
700 | ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
701 | ␉ ␉ if (ret == -1)␊ |
702 | ␉ ␉ {␊ |
703 | ␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);␊ |
704 | ␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
705 | ␉␉␉␉if (ret == -1)␊ |
706 | ␉␉␉␉{␊ |
707 | ␉␉␉␉␉sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);␊ |
708 | ␉␉␉␉␉ret = GetFileInfo(NULL, bootFileSpec, &flags, &time); ␊ |
709 | ␉␉␉␉␉if (ret == -1)␊ |
710 | ␉␉␉␉␉{␊ |
711 | ␉␉␉␉␉␉// Not found any alternate locations, using the original kernel image path.␊ |
712 | ␉␉␉␉␉␉strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);␊ |
713 | ␉␉␉␉␉}␊ |
714 | ␉␉␉␉}␊ |
715 | }␊ |
716 | #else␊ |
717 | ␉␉␉strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);␊ |
718 | #endif␊ |
719 | ␉␉␉␊ |
720 | verbose("Loading kernel %s\n", bootFileSpec);␊ |
721 | ret = LoadThinFatFile(bootFileSpec, &binary);␊ |
722 | if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)␊ |
723 | {␊ |
724 | ␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
725 | ␉␉␉␉ret = LoadThinFatFile(bootFileSpec, &binary);␉␉␉␉␊ |
726 | }␊ |
727 | ␉␉␉␊ |
728 | } while (0);␊ |
729 | #if TEXT_SPINNER␊ |
730 | clearActivityIndicator();␊ |
731 | #endif␊ |
732 | ␉␉␊ |
733 | #if DEBUG␊ |
734 | printf("Pausing...");␊ |
735 | sleep(8);␊ |
736 | #endif␊ |
737 | ␉␉␊ |
738 | if (ret <= 0)␊ |
739 | ␉␉{␊ |
740 | ␉␉␉printf("Can't find %s\n", bootFile);␊ |
741 | ␉␉␉␊ |
742 | ␉␉␉sleep(1);␊ |
743 | #ifdef NBP_SUPPORT␊ |
744 | if (gBootFileType == kNetworkDeviceType)␊ |
745 | ␉␉␉{␊ |
746 | // Return control back to PXE. Don't unload PXE base code.␊ |
747 | gUnloadPXEOnExit = false;␊ |
748 | break;␊ |
749 | }␊ |
750 | #endif␊ |
751 | }␊ |
752 | ␉␉else␊ |
753 | ␉␉{␊ |
754 | /* Won't return if successful. */␊ |
755 | ret = ExecKernel(binary);␊ |
756 | }␊ |
757 | }␊ |
758 | ␊ |
759 | // chainboot␊ |
760 | if (status==1)␊ |
761 | ␉{␊ |
762 | ␉␉if (getVideoMode() == GRAPHICS_MODE)␊ |
763 | ␉␉{␉// if we are already in graphics-mode,␊ |
764 | #if UNUSED␊ |
765 | ␉␉␉setVideoMode(VGA_TEXT_MODE, 0);␉// switch back to text mode␊ |
766 | #else␊ |
767 | ␉␉␉setVideoMode(VGA_TEXT_MODE);␉// switch back to text mode␊ |
768 | #endif␊ |
769 | ␉␉}␊ |
770 | }␊ |
771 | #ifdef NBP_SUPPORT␊ |
772 | if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)␊ |
773 | ␉{␊ |
774 | ␉␉nbpUnloadBaseCode();␊ |
775 | }␊ |
776 | #endif␊ |
777 | }␊ |
778 | ␊ |
779 | static void determineCpuArch(void)␊ |
780 | {␊ |
781 | ␉if (cpu_mode_is64bit())␊ |
782 | ␉{␊ |
783 | ␉␉archCpuType = CPU_TYPE_X86_64;␊ |
784 | ␉}␊ |
785 | ␉else␊ |
786 | ␉{␊ |
787 | ␉␉archCpuType = CPU_TYPE_I386;␊ |
788 | ␉}␊ |
789 | }␊ |
790 | ␊ |
791 | void getKernelCachePath()␊ |
792 | {␊ |
793 | ␉{␊ |
794 | ␉␉// If there is an extra kext/mkext, we return immediatly and we skip the kernelCache ␊ |
795 | ␉␉// since kexts/mkexts are not loaded properly when the kernelCache is used.␊ |
796 | ␉␉// Another method would be to re-build the kernelCache one the fly␊ |
797 | ␉␉if (found_extra_kext() == true) return; ␊ |
798 | ␉}␊ |
799 | ␉␊ |
800 | ␉{␊ |
801 | ␉␉const char *val;␊ |
802 | ␉␉int len;␊ |
803 | ␉␉␊ |
804 | ␉␉if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))␊ |
805 | ␉␉{␊ |
806 | char * buffer = newString(val);␊ |
807 | ␊ |
808 | ␉␉␉if (val[0] == '\\')␊ |
809 | ␉␉␉{␊ |
810 | ␉␉␉␉// Flip the back slash's to slash's .␊ |
811 | len = 0;␊ |
812 | while (buffer[len] != '\0') {␊ |
813 | if (buffer[len] == '\\')␊ |
814 | {␊ |
815 | buffer[len] = '/'; ␊ |
816 | }␊ |
817 | len++;␊ |
818 | }␊ |
819 | ␉␉␉} ␊ |
820 | ␉␉␉strlcpy(gBootKernelCacheFile, buffer, sizeof(gBootKernelCacheFile)+1);␊ |
821 | forcecache = true;␊ |
822 | ␉␉}␊ |
823 | ␉␉else␊ |
824 | ␉␉{␊ |
825 | ␉␉␉if(gMacOSVersion[3] == '7')␊ |
826 | ␉␉␉{␉␉␉␉␉␊ |
827 | ␉␉␉␉sprintf(gBootKernelCacheFile, "%s", kDefaultCachePath);␊ |
828 | ␉␉␉}␊ |
829 | ␉␉␉else if(gMacOSVersion[3] <= '6')␊ |
830 | ␉␉␉{␉␉␉ ␊ |
831 | ␉␉␉␉␊ |
832 | ␉␉␉␉PlatformInfo *platformInfo = malloc(sizeof(PlatformInfo));␊ |
833 | ␉␉␉␉if (platformInfo)␊ |
834 | ␉␉␉␉{␊ |
835 | ␉␉␉␉␉␊ |
836 | ␉␉␉␉␉bzero(platformInfo, sizeof(PlatformInfo));␊ |
837 | ␉␉␉␉␉␊ |
838 | ␉␉␉␉␉if (gPlatformName)␊ |
839 | ␉␉␉␉␉␉strlcpy(platformInfo->platformName,gPlatformName, sizeof(platformInfo->platformName)+1);␊ |
840 | ␉␉␉␉␉␊ |
841 | ␉␉␉␉␉if (gRootDevice)␊ |
842 | ␉␉␉␉␉{␊ |
843 | ␉␉␉␉␉␉char *rootPath_p = platformInfo->rootPath;␊ |
844 | ␉␉␉␉␉␉len = strlen(gRootDevice) + 1;␊ |
845 | ␉␉␉␉␉␉if ((unsigned)len > sizeof(platformInfo->rootPath))␊ |
846 | ␉␉␉␉␉␉{␊ |
847 | ␉␉␉␉␉␉␉len = sizeof(platformInfo->rootPath);␊ |
848 | ␉␉␉␉␉␉}␊ |
849 | ␉␉␉␉␉␉memcpy(rootPath_p, gRootDevice,len);␊ |
850 | ␉␉␉␉␉␉␊ |
851 | ␉␉␉␉␉␉rootPath_p += len;␊ |
852 | ␉␉␉␉␉␉␊ |
853 | ␉␉␉␉␉␉len = strlen(bootInfo->bootFile);␊ |
854 | ␉␉␉␉␉␉␊ |
855 | ␉␉␉␉␉␉if ((unsigned)(rootPath_p - platformInfo->rootPath + len) >=␊ |
856 | ␉␉␉␉␉␉␉sizeof(platformInfo->rootPath))␊ |
857 | ␉␉␉␉␉␉{␊ |
858 | ␉␉␉␉␉␉␉␊ |
859 | ␉␉␉␉␉␉␉len = sizeof(platformInfo->rootPath) -␊ |
860 | ␉␉␉␉␉␉␉(rootPath_p - platformInfo->rootPath);␊ |
861 | ␉␉␉␉␉␉} ␊ |
862 | ␉␉␉␉␉␉memcpy(rootPath_p, bootInfo->bootFile, len);␉␉␉␉␉␉␉␊ |
863 | ␉␉␉␉␉␉␊ |
864 | ␉␉␉␉␉}␉␊ |
865 | ␉␉␉␉␉␊ |
866 | ␉␉␉␉␉if (!platformInfo->platformName[0] || !platformInfo->rootPath[0])␊ |
867 | ␉␉␉␉␉{␊ |
868 | ␉␉␉␉␉␉platformInfo->platformName[0] = platformInfo->rootPath[0] = 0;␊ |
869 | ␉␉␉␉␉}␊ |
870 | ␉␉␉␉␉//memcpy(gRootPath,platformInfo->rootPath, sizeof(gRootPath));␊ |
871 | ␉␉␉␉␉␊ |
872 | ␉␉␉␉␉␊ |
873 | ␉␉␉␉␉bootInfo->adler32 = OSSwapHostToBigInt32(adler32((unsigned char *)platformInfo, sizeof(*platformInfo)));␊ |
874 | ␉␉␉␉␉␊ |
875 | ␉␉␉␉␉free(platformInfo);␉␊ |
876 | ␉␉␉␉}␊ |
877 | ␉␉␉␉␊ |
878 | ␉␉␉␉DBG("Adler32: %08lX\n",bootInfo->adler32);␊ |
879 | ␉␉␉␉␊ |
880 | ␉␉␉␉if (gMacOSVersion[3] < '6')␊ |
881 | ␉␉␉␉{␊ |
882 | ␉␉␉␉␉long flags, cachetime;␊ |
883 | ␉␉␉␉␉int ret = -1;␊ |
884 | ␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s.%08lX", "/System/Library/Caches/com.apple.kernelcaches/kernelcache",bootInfo->adler32);␊ |
885 | ␉␉␉␉␉ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);␊ |
886 | ␉␉␉␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
887 | ␉␉␉␉␉{␊ |
888 | ␉␉␉␉␉␉bootInfo->adler32 = 0;␊ |
889 | ␉␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s", "/System/Library/Caches/com.apple.kernelcaches/kernelcache"); ␊ |
890 | ␉␉␉␉␉}␊ |
891 | ␉␉␉␉␉␊ |
892 | ␉␉␉␉} else␊ |
893 | ␉␉␉␉␉sprintf(gBootKernelCacheFile, "%s_%s.%08lX", kDefaultCachePath, (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", bootInfo->adler32); //Snow Leopard␊ |
894 | ␉␉␉␉␊ |
895 | ␉␉␉}␉␊ |
896 | ␉␉}␊ |
897 | ␉}␉␊ |
898 | }␊ |
899 | ␊ |
900 | ␊ |
901 | static void getRootDevice()␊ |
902 | {␊ |
903 | ␉// Maximum config table value size␊ |
904 | #define VALUE_SIZE 2048␊ |
905 | ␉bool uuidSet = false;␊ |
906 | ␉const char *val = 0;␊ |
907 | int cnt = 0;␊ |
908 | ␉␊ |
909 | ␉if (getValueForKey(kBootUUIDKey, &val, &cnt, &bootInfo->bootConfig))␊ |
910 | ␉{␊ |
911 | ␉␉uuidSet = true;␉␉␊ |
912 | ␉}␊ |
913 | ␉else␊ |
914 | ␉{␊ |
915 | ␉␉if (getValueForBootKey(bootArgs->CommandLine, kRootDeviceKey, &val, &cnt))␊ |
916 | ␉␉{␊ |
917 | ␉␉␉if (*val == '*' && *(val + 1) != '/' && *(val + 1) != 'u')␊ |
918 | ␉␉␉{ ␊ |
919 | ␉␉␉␉val += 1; //skip the *␊ |
920 | ␉␉␉␉uuidSet = true;␉␉␉␉␊ |
921 | ␉␉␉␉␊ |
922 | ␉␉␉}␊ |
923 | ␉␉␉else if (*val == '*' && *(val + 1) == 'u')␊ |
924 | ␉␉␉{␊ |
925 | ␉␉␉␉␊ |
926 | ␉␉␉␉if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig)) ␊ |
927 | ␉␉␉␉␉uuidSet = true;␉␉␉␉␊ |
928 | ␉␉␉␉␊ |
929 | ␉␉␉} ␉␉␉␊ |
930 | ␉␉}␊ |
931 | ␉␉else␊ |
932 | ␉␉{␊ |
933 | #ifdef BOOT_HELPER_SUPPORT␊ |
934 | ␉␉␉//␊ |
935 | ␉␉␉// Try an alternate method for getting the root UUID on boot helper partitions.␊ |
936 | ␉␉␉//␊ |
937 | ␉␉␉if (gBootVolume->flags & kBVFlagBooter)␊ |
938 | ␉␉␉{␊ |
939 | ␉␉␉␉if((loadHelperConfig(&bootInfo->helperConfig) == 0)␊ |
940 | ␉␉␉␉ && getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig) )␊ |
941 | ␉␉␉␉{␊ |
942 | ␉␉␉␉␉getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig);␊ |
943 | ␉␉␉␉␉uuidSet = true;␊ |
944 | ␉␉␉␉␉goto out;␊ |
945 | ␉␉␉␉}␊ |
946 | ␉␉␉}␊ |
947 | #endif␉␉␉␊ |
948 | ␉␉␉if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))␊ |
949 | ␉␉␉{␊ |
950 | ␉␉␉␉extern int ArgCntRemaining;␊ |
951 | ␉␉␉␉uuidSet = false;␊ |
952 | ␉␉␉␉char * valueBuffer;␊ |
953 | ␉␉␉␉valueBuffer = malloc(VALUE_SIZE);␊ |
954 | ␉␉␉␉char * argP = bootArgs->CommandLine;␊ |
955 | ␉␉␉␉valueBuffer[0] = '*';␊ |
956 | ␉␉␉␉if (cnt > VALUE_SIZE)␊ |
957 | ␉␉␉␉{␊ |
958 | ␉␉␉␉␉cnt = VALUE_SIZE;␊ |
959 | ␉␉␉␉}␊ |
960 | ␉␉␉␉//cnt++;␊ |
961 | ␉␉␉␉strlcpy(valueBuffer + 1, val, cnt+1);␉␉␉␉␊ |
962 | ␉␉␉␉if (!copyArgument( kRootDeviceKey, valueBuffer, cnt, &argP, &ArgCntRemaining))␊ |
963 | ␉␉␉␉{␊ |
964 | ␉␉␉␉␉free(valueBuffer);␊ |
965 | ␉␉␉␉␉printf("Error: boot arguments too long, unable to set root device !!");␊ |
966 | ␉␉␉␉␉getc();␊ |
967 | ␉␉␉␉␉return;␊ |
968 | ␉␉␉␉}␊ |
969 | ␉␉␉␉free(valueBuffer);␊ |
970 | ␉␉␉␉goto out;␊ |
971 | ␉␉␉}␊ |
972 | ␉␉␉␊ |
973 | ␉␉␉if (gBootVolume->fs_getuuid && gBootVolume->fs_getuuid (gBootVolume, bootInfo->uuidStr) == 0)␊ |
974 | ␉␉␉{␊ |
975 | ␉␉␉␉verbose("Setting boot-uuid to: %s\n", bootInfo->uuidStr);␊ |
976 | ␉␉␉␉uuidSet = true;␊ |
977 | ␉␉␉␉gRootDevice = bootInfo->uuidStr;␊ |
978 | ␉␉␉␉return;␊ |
979 | ␉␉␉}␊ |
980 | ␉␉␊ |
981 | ␉␉}␉␊ |
982 | ␉}␊ |
983 | ␉␉␊ |
984 | out:␉␊ |
985 | ␉verbose("Setting %s to: %s\n", uuidSet ? kBootUUIDKey : "root device", (char* )val); ␊ |
986 | ␉gRootDevice = (char* )val;␊ |
987 | }␊ |
988 | ␊ |
989 | static bool getOSVersion(char *str)␊ |
990 | {␊ |
991 | ␉bool valid = false;␉␊ |
992 | ␉config_file_t systemVersion;␊ |
993 | ␉␊ |
994 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
995 | ␉{␊ |
996 | ␉␉valid = true;␊ |
997 | ␉}␊ |
998 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
999 | ␉{␊ |
1000 | ␉␉valid = true;␊ |
1001 | ␉}␊ |
1002 | ␉␊ |
1003 | ␉if (valid)␊ |
1004 | ␉{␉␉␊ |
1005 | ␉␉const char *val;␊ |
1006 | ␉␉int len;␊ |
1007 | ␉␉␊ |
1008 | ␉␉if (getValueForKey(kProductVersion, &val, &len, &systemVersion))␊ |
1009 | ␉␉{␊ |
1010 | ␉␉␉// getValueForKey uses const char for val␊ |
1011 | ␉␉␉// so copy it and trim␊ |
1012 | ␉␉␉*str = '\0';␊ |
1013 | ␉␉␉strncat(str, val, MIN(len, 4));␊ |
1014 | ␉␉}␊ |
1015 | ␉␉else␊ |
1016 | ␉␉␉valid = false;␊ |
1017 | ␉}␊ |
1018 | ␉␊ |
1019 | ␉return valid;␊ |
1020 | }␊ |
1021 | ␊ |
1022 | static bool find_file_with_ext(const char* dir, const char *ext, const char * name_compare, size_t ext_size)␊ |
1023 | {␊ |
1024 | ␉char* name;␊ |
1025 | ␉long flags;␊ |
1026 | ␉long time;␊ |
1027 | ␉struct dirstuff* moduleDir = opendir(dir);␊ |
1028 | ␉while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)␊ |
1029 | ␉{␉␉␊ |
1030 | ␉␉int len = strlen(name);␊ |
1031 | ␉␉␊ |
1032 | ␉␉if (len >= ext_size)␊ |
1033 | ␉␉{␊ |
1034 | ␉␉␉if(strcmp(&name[len - ext_size], ext) == 0)␊ |
1035 | ␉␉␉{␉␊ |
1036 | ␉␉␉␉if (name_compare)␊ |
1037 | ␉␉␉␉{␊ |
1038 | ␉␉␉␉␉if (strcmp(name, name_compare) == 0)␊ |
1039 | ␉␉␉␉␉{␊ |
1040 | ␉␉␉␉␉␉DBG("found : %s\n", name);␉␊ |
1041 | ␉␉␉␉␉␉return true;␊ |
1042 | ␉␉␉␉␉}␊ |
1043 | ␉␉␉␉}␊ |
1044 | ␉␉␉␉else␊ |
1045 | ␉␉␉␉{␊ |
1046 | ␉␉␉␉␉DBG("found : %s\n", name);␉␊ |
1047 | ␉␉␉␉␉return true;␊ |
1048 | ␉␉␉␉}␉␉␉␊ |
1049 | ␉␉␉}␊ |
1050 | #if DEBUG_BOOT␊ |
1051 | ␉␉␉else ␊ |
1052 | ␉␉␉{␊ |
1053 | ␉␉␉␉DBG("Ignoring %s\n", name);␊ |
1054 | ␉␉␉}␊ |
1055 | #endif␊ |
1056 | ␉␉}␉␉␊ |
1057 | #if DEBUG_BOOT␊ |
1058 | ␉␉else ␊ |
1059 | ␉␉{␊ |
1060 | ␉␉␉DBG("Ignoring %s\n", name);␊ |
1061 | ␉␉}␊ |
1062 | #endif␉␉␊ |
1063 | ␉}␊ |
1064 | ␉␊ |
1065 | ␉return false;␊ |
1066 | }␊ |
1067 | ␊ |
1068 | // If a kext is found in /Extra/Extentions return true␊ |
1069 | // If a mkext is found in /Extra return true␊ |
1070 | // Otherwise return false␊ |
1071 | // Tips (if you still use extra kext(s)/mkext(s) ): With Lion it's recommended to create a system mkext (see the kextcache commandline) to decrease boot time,␊ |
1072 | // otherwise it will act like a -f each time a extra kext/kext is detected␊ |
1073 | static bool found_extra_kext(void) ␊ |
1074 | {␉␊ |
1075 | #define EXTENSIONS "Extensions"␊ |
1076 | #define MKEXT_EXT ".mkext"␉␊ |
1077 | #define MKEXT_EXT_SIZE sizeof("mkext")␊ |
1078 | #define KEXT_EXT ".kext"␉␊ |
1079 | #define KEXT_EXT_SIZE sizeof("kext")␊ |
1080 | ␉␊ |
1081 | ␉long flags;␊ |
1082 | ␉long exttime;␊ |
1083 | ␉int ret = -1;␊ |
1084 | ␉␊ |
1085 | ␉ret = GetFileInfo("rd(0,0)/Extra/", EXTENSIONS, &flags, &exttime);␊ |
1086 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
1087 | ␉{␉␉␉␊ |
1088 | ␉␉if (((flags & kFileTypeMask) == kFileTypeFlat))␊ |
1089 | ␉␉{␊ |
1090 | ␉␉␉if (find_file_with_ext("rd(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))␊ |
1091 | ␉␉␉{␊ |
1092 | ␉␉␉␉return true;␉␉␉␉␊ |
1093 | ␉␉␉}␊ |
1094 | ␉␉} ␊ |
1095 | ␉␉else if (((flags & kFileTypeMask) == kFileTypeDirectory)) ␊ |
1096 | ␉␉{␊ |
1097 | ␉␉␉if (find_file_with_ext("rd(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))␊ |
1098 | ␉␉␉{␊ |
1099 | ␉␉␉␉return true;␊ |
1100 | ␉␉␉} ␊ |
1101 | ␉␉}␊ |
1102 | ␉}␊ |
1103 | ␉ret = GetFileInfo("/Extra/", EXTENSIONS, &flags, &exttime);␊ |
1104 | ␉if (ret == 0) ␊ |
1105 | ␉{␊ |
1106 | ␉␉if (((flags & kFileTypeMask) == kFileTypeFlat))␊ |
1107 | ␉␉{␊ |
1108 | ␉␉␉if (find_file_with_ext("/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))␊ |
1109 | ␉␉␉{␊ |
1110 | ␉␉␉␉return true;␉␉␉␉␊ |
1111 | ␉␉␉}␊ |
1112 | ␉␉} ␊ |
1113 | ␉␉else if (((flags & kFileTypeMask) == kFileTypeDirectory)) ␊ |
1114 | ␉␉{␊ |
1115 | ␉␉␉if (find_file_with_ext("/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))␊ |
1116 | ␉␉␉{␊ |
1117 | ␉␉␉␉return true;␉␉␉␉␊ |
1118 | ␉␉␉} ␊ |
1119 | ␉␉}␉␉␉␊ |
1120 | ␉}␊ |
1121 | ␉ret = GetFileInfo("bt(0,0)/Extra/", EXTENSIONS, &flags, &exttime);␊ |
1122 | ␉if (ret == 0)␊ |
1123 | ␉{␊ |
1124 | ␉␉if (((flags & kFileTypeMask) == kFileTypeFlat))␊ |
1125 | ␉␉{␊ |
1126 | ␉␉␉if (find_file_with_ext("bt(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))␊ |
1127 | ␉␉␉{␊ |
1128 | ␉␉␉␉return true;␉␉␉␉␊ |
1129 | ␉␉␉}␊ |
1130 | ␉␉}␊ |
1131 | ␉␉else if (((flags & kFileTypeMask) == kFileTypeDirectory)) ␊ |
1132 | ␉␉{␊ |
1133 | ␉␉␉if (find_file_with_ext("bt(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))␊ |
1134 | ␉␉␉{␊ |
1135 | ␉␉␉␉return true;␊ |
1136 | ␉␉␉}␊ |
1137 | ␉␉}␊ |
1138 | ␉}␊ |
1139 | ␉DBG("NO Extra Mkext/Kext found\n");␉␉␊ |
1140 | ␉␊ |
1141 | ␉// nothing found␊ |
1142 | ␉return false;␊ |
1143 | }␊ |
1144 | ␊ |
1145 | #if 0␊ |
1146 | static char *FIXED_BOOTFILE_PATH(char * str)␊ |
1147 | {␊ |
1148 | ␉char bootfile[128];␊ |
1149 | ␉␊ |
1150 | ␉bool bootFileWithDevice = false;␊ |
1151 | ␉// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel␊ |
1152 | ␉if (strncmp(str,"bt(",3) == 0 ||␊ |
1153 | ␉␉strncmp(str,"hd(",3) == 0 ||␊ |
1154 | ␉␉strncmp(str,"rd(",3) == 0)␊ |
1155 | ␉{␊ |
1156 | ␉␉bootFileWithDevice = true;␊ |
1157 | ␉}␊ |
1158 | ␉␉␊ |
1159 | ␉// bootFile must start with a / if it not start with a device name␊ |
1160 | ␉if (!bootFileWithDevice && (str)[0] != '/')␊ |
1161 | ␉␉sprintf(bootFile, "/%s", str); // append a leading /␊ |
1162 | ␉else␊ |
1163 | ␉␉strlcpy(bootFile, bootInfo->bootFile, sizeof(bootInfo->bootFile)+1);␊ |
1164 | ␉␊ |
1165 | ␉return bootfile;␊ |
1166 | }␊ |
1167 | #endif␊ |
1168 | |