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