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