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