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