Chameleon

Chameleon Commit Details

Date:2011-08-20 17:35:49 (12 years 8 months ago)
Author:JrCs
Commit:1443
Parents: 1442
Message:Rewrote the loading of kernel cache file
Changes:
M/trunk/i386/boot2/boot.c
M/trunk/i386/boot2/options.c

File differences

trunk/i386/boot2/boot.c
7575
7676
7777
78
7978
8079
8180
......
222221
223222
224223
224
225225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
226350
227351
228352
......
251375
252376
253377
254
255378
256379
257380
258
259381
260382
261383
......
366488
367489
368490
369
370
491
492
371493
372494
373495
374
496
497
375498
376499
377500
......
487610
488611
489612
613
614
490615
491616
492617
493618
494619
495620
496
621
622
623
624
625
626
627
497628
498629
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588630
589
590
591
592
593
594
595
596
597
598
599
600
601
631
632
602633
603634
604
605
606635
607
608
609
610
611
636
637
638
612639
613
614
640
641
642
615643
616
617
618
619
620
621
644
645
646
647
622648
623649
624
625
650
651
626652
627653
628
629
654
655
630656
631657
632658
659
660
661
662
633663
634
664
665
666
635667
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
668
669
659670
660671
661672
chargRootDevice[512];
chargMKextName[512];
chargMacOSVersion[8];
static chargBootKernelCacheFile[512];
intbvCount = 0, gDeviceCount = 0;
//intmenucount = 0;
longgBootMode; /* defaults to 0 == kBootModeNormal */
return 0;
}
//==========================================================================
// LoadKernelCache - Try to load Kernel Cache.
// return the length of the loaded cache file or -1 on error
long LoadKernelCache(void **binary) {
charkernelCacheFile[512];
charkernelCachePath[512];
const char*val;
int len;
longflags, time, cachetime, kerneltime, exttime, ret=-1;
unsigned long adler32;
// Determine the name of the Kernel Cache
if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
if (val[0] == '\\')
{
len--;
val++;
}
strlcpy(kernelCacheFile, val, len + 1);
}
else {
// Lion prelink kernel cache file
if (checkOSVersion("10.7")) {
sprintf(kernelCacheFile, "%skernelcache", kDefaultCachePathSnow);
}
// Snow Leopard prelink kernel cache file
else if (checkOSVersion("10.6")) {
sprintf(kernelCacheFile, "kernelcache_%s", (archCpuType == CPU_TYPE_I386)
? "i386" : "x86_64");
int lnam = strlen(kernelCacheFile) + 9; //with adler32
char* name;
long prev_time = 0;
struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
{
if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))
{
sprintf(kernelCacheFile, "%s%s", kDefaultCachePathSnow, name);
prev_time = time;
}
}
}
else {
// Reset cache name.
bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
sprintf(kernelCacheFile, "%s.%08lX", kDefaultCachePathLeo, adler32);
}
}
// kernelCacheFile must start with a /
if (kernelCacheFile[0] != '/') {
char *str = strdup(kernelCacheFile);
if (str == NULL)
return -1;
sprintf(kernelCacheFile, "/%s", str);
free(str);
}
// Check if the kernel cache file exists
ret = -1;
// If boot from a boot helper partition check the kernel cache file on it
if (gBootVolume->flags & kBVFlagBooter) {
sprintf(kernelCachePath, "com.apple.boot.P%s", kernelCacheFile);
ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
{
sprintf(kernelCachePath, "com.apple.boot.R%s", kernelCacheFile);
ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
{
sprintf(kernelCachePath, "com.apple.boot.S%s", kernelCacheFile);
ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
if ((flags & kFileTypeMask) != kFileTypeFlat)
ret = -1;
}
}
}
// If not found, use the original kernel cache path.
if (ret == -1) {
strcpy(kernelCachePath, kernelCacheFile);
ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
if ((flags & kFileTypeMask) != kFileTypeFlat)
ret = -1;
}
// Exit if kernel cache file wasn't found
if (ret == -1) {
verbose("No Kernel Cache File '%s' found\n", kernelCacheFile);
return -1;
}
// Check if the kernel cache file is more recent (mtime)
// than the kernel file or the S/L/E directory
ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
// Check if the kernel file is more recent than the cache file
if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
&& (kerneltime > cachetime))
return -1;
ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
// Check if the S/L/E directory time is more recent than the cache file
if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
&& (exttime > cachetime))
return -1;
// Since the kernel cache file exists and is the most recent try to load it
verbose("Loading kernel cache %s\n", kernelCachePath);
if (checkOSVersion("10.7")) {
ret = LoadThinFatFile(kernelCachePath, binary);
} else {
ret = LoadFile(kernelCachePath);
*binary = (void *)kLoadAddr;
}
return ret; // ret contain the length of the binary
}
//==========================================================================
// This is the entrypoint from real-mode which functions exactly as it did
// before. Multiboot does its own runtime initialization, does some of its
// own things, and then calls common_boot.
bool firstRun = true;
bool instantMenu;
bool rescanPrompt;
char *bootFile;
intstatus;
unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
unsigned intdenyBVFlags = kBVFlagEFISystem;
unsigned longadler32;
// Set reminder to unload the PXE base code. Neglect to unload
// the base code will result in a hang or kernel panic.
booltryresume, tryresumedefault, forceresume;
booluseKernelCache = false; // by default don't use prelink kernel cache
const char*val;
intlen, trycache, ret = -1;
longflags, cachetime, kerneltime, exttime, sleeptime, time;
intlen, ret = -1;
longflags, sleeptime, time;
void*binary = (void *)kLoadAddr;
// additional variable for testing alternate kernel image locations on boot helper partitions.
charbootFileSpec[512];
char bootFile[512];
charbootFilePath[512];
// Initialize globals.
sysConfigValid = false;
break;
}
verbose("Loading Darwin %s\n", gMacOSVersion);
// If boot from boot helper partitions and OS is Lion use prelink kernel.
// We need to find a solution to load extra mkext with a prelink kernel.
if (gBootVolume->flags & kBVFlagBooter && checkOSVersion("10.7")) {
verbose("Booting from Lion RAID volume so forcing to use KernelCache\n");
useKernelCache = true;
} else {
getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
bool useKernelCacheConfig = false; // by default don't use prelink kernel cache
getBoolForKey(kUseKernelCache, &useKernelCacheConfig, &bootInfo->chameleonConfig);
useKernelCache = (useKernelCacheConfig &&
((gBootMode & kBootModeSafe) == 0) &&
!gOverrideKernel &&
(gMKextName[0] == 0) &&
(gBootFileType == kBlockDeviceType));
}
if (useKernelCache) {
if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
if (val[0] == '\\')
{
len--;
val++;
}
strlcpy(gBootKernelCacheFile, val, len + 1);
}
else {
// Lion prelink kernel cache file
if (checkOSVersion("10.7")) {
sprintf(gBootKernelCacheFile, "%skernelcache", kDefaultCachePathSnow);
}
// Snow Leopard prelink kernel cache file
else if (checkOSVersion("10.6")) {
sprintf(gBootKernelCacheFile, "kernelcache_%s", (archCpuType == CPU_TYPE_I386)
? "i386" : "x86_64");
int lnam = sizeof(gBootKernelCacheFile) + 9; //with adler32
char* name;
long prev_time = 0;
struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
{
if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
&& strstr(name, gBootKernelCacheFile) && (name[lnam] != '.'))
{
sprintf(gBootKernelCacheFile, "%s%s", kDefaultCachePathSnow, name);
prev_time = time;
}
}
}
else {
// Reset cache name.
bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePathLeo, adler32);
}
}
}
// Check for cache file.
trycache = (useKernelCache &&
((gBootMode & kBootModeSafe) == 0) &&
!gOverrideKernel &&
(gBootFileType == kBlockDeviceType) &&
(gMKextName[0] == '\0') &&
(gBootKernelCacheFile[0] != '\0'));
verbose("Loading Darwin %s\n", gMacOSVersion);
// Check if the kernel cache file exists and is more recent (mtime) than
// the kernel file or the S/L/E directory
if (trycache) do {
ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
// Check if the kernel cache file exist
if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
trycache = 0;
break;
}
ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
kerneltime = -1;
// Check if the kernel file is more recent than the cache file
if (kerneltime > cachetime) {
trycache = 0;
break;
}
ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
if ((ret != 0) && ((flags & kFileTypeMask) != kFileTypeDirectory))
exttime = -1;
// Check if the S/L/E directory time is more recent than the cache file
if (exttime > cachetime) {
trycache = 0;
break;
}
} while (0);
do {
if (trycache) {
bootFile = gBootKernelCacheFile;
verbose("Loading kernel cache %s\n", bootFile);
if (checkOSVersion("10.7")) {
ret = LoadThinFatFile(bootFile, &binary);
}
else {
ret = LoadFile(bootFile);
binary = (void *)kLoadAddr;
}
if (useKernelCache) {
ret = LoadKernelCache(&binary);
if (ret >= 0)
break;
verbose("Kernel cache did not load %s\n ", bootFile);
}
if (checkOSVersion("10.7")) {
bootFile = gBootKernelCacheFile;
}
else {
// bootFile must start with a /
if ((bootInfo->bootFile)[0] != '/')
sprintf(bootFile, "/%s", bootInfo->bootFile);
}
else
strcpy(bootFile, bootInfo->bootFile);
// Try to load kernel image from alternate locations on boot helper partitions.
sprintf(bootFileSpec, "com.apple.boot.P%s", bootFile);
ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
if (ret == -1)
{
sprintf(bootFileSpec, "com.apple.boot.R%s", bootFile);
ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
ret = -1;
if (gBootVolume->flags & kBVFlagBooter) {
sprintf(bootFilePath, "com.apple.boot.P%s", bootFile);
ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
if (ret == -1)
{
sprintf(bootFileSpec, "com.apple.boot.S%s", bootFile);
ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
sprintf(bootFilePath, "com.apple.boot.R%s", bootFile);
ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
if (ret == -1)
{
// No alternate location found, using the original kernel image path.
strcpy(bootFileSpec, bootInfo->bootFile);
sprintf(bootFilePath, "com.apple.boot.S%s", bootFile);
ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
}
}
}
if (ret == -1) {
// No alternate location found, using the original kernel image path.
strcpy(bootFilePath, bootFile);
}
if (checkOSVersion("10.7"))
verbose("Loading kernel %s\n", bootFilePath);
ret = LoadThinFatFile(bootFilePath, &binary);
if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
{
//Lion, dont load kernel if haz cache
if (!trycache)
{
verbose("Loading kernel %s\n", bootFileSpec);
ret = LoadThinFatFile(bootFileSpec, &binary);
if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
{
archCpuType = CPU_TYPE_I386;
ret = LoadThinFatFile(bootFileSpec, &binary);
}
}
else ret = 1;
}
else
{
//Snow Leopard or older
verbose("Loading kernel %s\n", bootFileSpec);
ret = LoadThinFatFile(bootFileSpec, &binary);
if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
{
archCpuType = CPU_TYPE_I386;
ret = LoadThinFatFile(bootFileSpec, &binary);
}
archCpuType = CPU_TYPE_I386;
ret = LoadThinFatFile(bootFilePath, &binary);
}
} while (0);
trunk/i386/boot2/options.c
11971197
11981198
11991199
1200
12011200
12021201
12031202
1204
1205
1206
12071203
12081204
12091205
12101206
1207
1208
1209
12111210
12121211
12131212
......
13231322
13241323
13251324
1326
13271325
1328
1326
1327
13291328
13301329
13311330
gOverrideKernel = false;
if (( kernel = extractKernelName((char **)&cp) )) {
strcpy( bootInfo->bootFile, kernel );
gOverrideKernel = true;
} else {
if ( getValueForKey( kKernelNameKey, &val, &cnt, &bootInfo->bootConfig ) ) {
strlcpy( bootInfo->bootFile, val, cnt+1 );
if (strcmp( bootInfo->bootFile, kDefaultKernel ) != 0) {
gOverrideKernel = true;
}
} else {
strcpy( bootInfo->bootFile, kDefaultKernel );
}
}
if (strcmp( bootInfo->bootFile, kDefaultKernel ) != 0) {
gOverrideKernel = true;
}
cntRemaining = BOOT_STRING_LEN - 2; // save 1 for NULL, 1 for space
argP = bootArgs->CommandLine;
}
if ( getValueForKey( kMKextCacheKey, &val, &cnt, &bootInfo->bootConfig ) )
{
strlcpy(gMKextName, val, cnt + 1);
}
else
gMKextName[0]=0;
free(configKernelFlags);
free(valueBuffer);

Archive Download the corresponding diff file

Revision: 1443