Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r474]: trunk / refit / refit / main.c Maximize Restore History

Download this file

main.c    1172 lines (1015 with data), 46.5 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 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
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 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
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
/*
* refit/main.c
* Main code for the boot menu
*
* Copyright (c) 2006-2010 Christoph Pfisterer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Christoph Pfisterer nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "lib.h"
#include "syslinux_mbr.h"
// types
typedef struct {
REFIT_MENU_ENTRY me;
CHAR16 *LoaderPath;
CHAR16 *VolName;
EFI_DEVICE_PATH *DevicePath;
BOOLEAN UseGraphicsMode;
CHAR16 *LoadOptions;
} LOADER_ENTRY;
typedef struct {
REFIT_MENU_ENTRY me;
REFIT_VOLUME *Volume;
CHAR16 *LoadOptions;
} LEGACY_ENTRY;
// variables
#define MACOSX_LOADER_PATH L"\\System\\Library\\CoreServices\\boot.efi"
#define TAG_ABOUT (1)
#define TAG_RESET (2)
#define TAG_SHUTDOWN (3)
#define TAG_TOOL (4)
#define TAG_LOADER (5)
#define TAG_LEGACY (6)
static REFIT_MENU_ENTRY MenuEntryAbout = { L"About rEFIt", TAG_ABOUT, 1, 0, 'A', NULL, NULL, NULL };
static REFIT_MENU_ENTRY MenuEntryReset = { L"Restart Computer", TAG_RESET, 1, 0, 'R', NULL, NULL, NULL };
static REFIT_MENU_ENTRY MenuEntryShutdown = { L"Shut Down Computer", TAG_SHUTDOWN, 1, 0, 'U', NULL, NULL, NULL };
static REFIT_MENU_ENTRY MenuEntryReturn = { L"Return to Main Menu", TAG_RETURN, 0, 0, 0, NULL, NULL, NULL };
static REFIT_MENU_SCREEN MainMenu = { L"Main Menu", NULL, 0, NULL, 0, NULL, 0, L"Automatic boot" };
static REFIT_MENU_SCREEN AboutMenu = { L"About", NULL, 0, NULL, 0, NULL, 0, NULL };
//
// misc functions
//
static VOID AboutRefit(VOID)
{
if (AboutMenu.EntryCount == 0) {
AboutMenu.TitleImage = BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT);
AddMenuInfoLine(&AboutMenu, L"rEFIt Version 0.14");
AddMenuInfoLine(&AboutMenu, L"");
AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2006-2010 Christoph Pfisterer");
AddMenuInfoLine(&AboutMenu, L"Portions Copyright (c) Intel Corporation and others");
AddMenuInfoLine(&AboutMenu, L"");
AddMenuInfoLine(&AboutMenu, L"Running on:");
AddMenuInfoLine(&AboutMenu, PoolPrint(L" EFI Revision %d.%02d",
ST->Hdr.Revision >> 16, ST->Hdr.Revision & ((1 << 16) - 1)));
#if defined(EFI32)
AddMenuInfoLine(&AboutMenu, L" Platform: x86 (32 bit)");
#elif defined(EFIX64)
AddMenuInfoLine(&AboutMenu, L" Platform: x86_64 (64 bit)");
#else
AddMenuInfoLine(&AboutMenu, L" Platform: unknown");
#endif
AddMenuInfoLine(&AboutMenu, PoolPrint(L" Firmware: %s %d.%02d",
ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & ((1 << 16) - 1)));
AddMenuInfoLine(&AboutMenu, PoolPrint(L" Screen Output: %s", egScreenDescription()));
AddMenuEntry(&AboutMenu, &MenuEntryReturn);
}
RunMenu(&AboutMenu, NULL);
}
static EFI_STATUS StartEFIImageList(IN EFI_DEVICE_PATH **DevicePaths,
IN CHAR16 *LoadOptions, IN CHAR16 *LoadOptionsPrefix,
IN CHAR16 *ImageTitle,
OUT UINTN *ErrorInStep)
{
EFI_STATUS Status, ReturnStatus;
EFI_HANDLE ChildImageHandle;
EFI_LOADED_IMAGE *ChildLoadedImage;
UINTN DevicePathIndex;
CHAR16 ErrorInfo[256];
CHAR16 *FullLoadOptions = NULL;
Print(L"Starting %s\n", ImageTitle);
if (ErrorInStep != NULL)
*ErrorInStep = 0;
// load the image into memory
ReturnStatus = Status = EFI_NOT_FOUND; // in case the list is empty
for (DevicePathIndex = 0; DevicePaths[DevicePathIndex] != NULL; DevicePathIndex++) {
ReturnStatus = Status = BS->LoadImage(FALSE, SelfImageHandle, DevicePaths[DevicePathIndex], NULL, 0, &ChildImageHandle);
if (ReturnStatus != EFI_NOT_FOUND)
break;
}
SPrint(ErrorInfo, 255, L"while loading %s", ImageTitle);
if (CheckError(Status, ErrorInfo)) {
if (ErrorInStep != NULL)
*ErrorInStep = 1;
goto bailout;
}
// set load options
if (LoadOptions != NULL) {
ReturnStatus = Status = BS->HandleProtocol(ChildImageHandle, &LoadedImageProtocol, (VOID **) &ChildLoadedImage);
if (CheckError(Status, L"while getting a LoadedImageProtocol handle")) {
if (ErrorInStep != NULL)
*ErrorInStep = 2;
goto bailout_unload;
}
if (LoadOptionsPrefix != NULL) {
FullLoadOptions = PoolPrint(L"%s %s ", LoadOptionsPrefix, LoadOptions);
// NOTE: That last space is also added by the EFI shell and seems to be significant
// when passing options to Apple's boot.efi...
LoadOptions = FullLoadOptions;
}
// NOTE: We also include the terminating null in the length for safety.
ChildLoadedImage->LoadOptions = (VOID *)LoadOptions;
ChildLoadedImage->LoadOptionsSize = ((UINT32)StrLen(LoadOptions) + 1) * sizeof(CHAR16);
Print(L"Using load options '%s'\n", LoadOptions);
}
// close open file handles
UninitRefitLib();
// turn control over to the image
// TODO: (optionally) re-enable the EFI watchdog timer!
ReturnStatus = Status = BS->StartImage(ChildImageHandle, NULL, NULL);
// control returns here when the child image calls Exit()
SPrint(ErrorInfo, 255, L"returned from %s", ImageTitle);
if (CheckError(Status, ErrorInfo)) {
if (ErrorInStep != NULL)
*ErrorInStep = 3;
}
// re-open file handles
ReinitRefitLib();
bailout_unload:
// unload the image, we don't care if it works or not...
Status = BS->UnloadImage(ChildImageHandle);
bailout:
if (FullLoadOptions != NULL)
FreePool(FullLoadOptions);
return ReturnStatus;
}
static EFI_STATUS StartEFIImage(IN EFI_DEVICE_PATH *DevicePath,
IN CHAR16 *LoadOptions, IN CHAR16 *LoadOptionsPrefix,
IN CHAR16 *ImageTitle,
OUT UINTN *ErrorInStep)
{
EFI_DEVICE_PATH *DevicePaths[2];
DevicePaths[0] = DevicePath;
DevicePaths[1] = NULL;
return StartEFIImageList(DevicePaths, LoadOptions, LoadOptionsPrefix, ImageTitle, ErrorInStep);
}
//
// EFI OS loader functions
//
static VOID StartLoader(IN LOADER_ENTRY *Entry)
{
BeginExternalScreen(Entry->UseGraphicsMode, L"Booting OS");
StartEFIImage(Entry->DevicePath, Entry->LoadOptions,
Basename(Entry->LoaderPath), Basename(Entry->LoaderPath), NULL);
FinishExternalScreen();
}
static LOADER_ENTRY * AddLoaderEntry(IN CHAR16 *LoaderPath, IN CHAR16 *LoaderTitle, IN REFIT_VOLUME *Volume)
{
CHAR16 *FileName, *OSIconName;
CHAR16 IconFileName[256];
CHAR16 DiagsFileName[256];
CHAR16 ShortcutLetter;
UINTN LoaderKind;
LOADER_ENTRY *Entry, *SubEntry;
REFIT_MENU_SCREEN *SubScreen;
FileName = Basename(LoaderPath);
// prepare the menu entry
Entry = AllocateZeroPool(sizeof(LOADER_ENTRY));
Entry->me.Title = PoolPrint(L"Boot %s from %s", (LoaderTitle != NULL) ? LoaderTitle : LoaderPath + 1, Volume->VolName);
Entry->me.Tag = TAG_LOADER;
Entry->me.Row = 0;
if (GlobalConfig.HideBadges == 0 ||
(GlobalConfig.HideBadges == 1 && Volume->DiskKind != DISK_KIND_INTERNAL))
Entry->me.BadgeImage = Volume->VolBadgeImage;
Entry->LoaderPath = StrDuplicate(LoaderPath);
Entry->VolName = Volume->VolName;
Entry->DevicePath = FileDevicePath(Volume->DeviceHandle, Entry->LoaderPath);
Entry->UseGraphicsMode = FALSE;
// locate a custom icon for the loader
StrCpy(IconFileName, LoaderPath);
ReplaceExtension(IconFileName, L".icns");
if (FileExists(Volume->RootDir, IconFileName))
Entry->me.Image = LoadIcns(Volume->RootDir, IconFileName, 128);
// detect specific loaders
OSIconName = NULL;
LoaderKind = 0;
ShortcutLetter = 0;
if (StriCmp(LoaderPath, MACOSX_LOADER_PATH) == 0) {
OSIconName = L"mac";
Entry->UseGraphicsMode = TRUE;
LoaderKind = 1;
ShortcutLetter = 'M';
} else if (StriCmp(FileName, L"diags.efi") == 0) {
OSIconName = L"hwtest";
} else if (StriCmp(FileName, L"e.efi") == 0 ||
StriCmp(FileName, L"elilo.efi") == 0) {
OSIconName = L"elilo,linux";
LoaderKind = 2;
ShortcutLetter = 'L';
} else if (StriCmp(FileName, L"cdboot.efi") == 0 ||
StriCmp(FileName, L"bootmgr.efi") == 0 ||
StriCmp(FileName, L"Bootmgfw.efi") == 0) {
OSIconName = L"win";
ShortcutLetter = 'W';
} else if (StriCmp(FileName, L"xom.efi") == 0) {
OSIconName = L"xom,win";
Entry->UseGraphicsMode = TRUE;
LoaderKind = 3;
ShortcutLetter = 'W';
}
Entry->me.ShortcutLetter = ShortcutLetter;
if (Entry->me.Image == NULL)
Entry->me.Image = LoadOSIcon(OSIconName, L"unknown", FALSE);
// create the submenu
SubScreen = AllocateZeroPool(sizeof(REFIT_MENU_SCREEN));
SubScreen->Title = PoolPrint(L"Boot Options for %s on %s", (LoaderTitle != NULL) ? LoaderTitle : FileName, Volume->VolName);
SubScreen->TitleImage = Entry->me.Image;
// default entry
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = (LoaderKind == 1) ? L"Boot Mac OS X" : PoolPrint(L"Run %s", FileName);
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = Entry->UseGraphicsMode;
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
// loader-specific submenu entries
if (LoaderKind == 1) { // entries for Mac OS X
#if defined(EFIX64)
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Mac OS X with a 64-bit kernel";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = Entry->UseGraphicsMode;
SubEntry->LoadOptions = L"arch=x86_64";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Mac OS X with a 32-bit kernel";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = Entry->UseGraphicsMode;
SubEntry->LoadOptions = L"arch=i386";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
#endif
if (!(GlobalConfig.DisableFlags & DISABLE_FLAG_SINGLEUSER)) {
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Mac OS X in verbose mode";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = FALSE;
SubEntry->LoadOptions = L"-v";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
#if defined(EFIX64)
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Mac OS X in verbose mode (64-bit)";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = FALSE;
SubEntry->LoadOptions = L"-v arch=x86_64";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Mac OS X in verbose mode (32-bit)";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = FALSE;
SubEntry->LoadOptions = L"-v arch=i386";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
#endif
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Mac OS X in single user mode";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = FALSE;
SubEntry->LoadOptions = L"-v -s";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
}
// check for Apple hardware diagnostics
StrCpy(DiagsFileName, L"\\System\\Library\\CoreServices\\.diagnostics\\diags.efi");
if (FileExists(Volume->RootDir, DiagsFileName) && !(GlobalConfig.DisableFlags & DISABLE_FLAG_HWTEST)) {
Print(L" - Apple Hardware Test found\n");
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Run Apple Hardware Test";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = StrDuplicate(DiagsFileName);
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
SubEntry->UseGraphicsMode = TRUE;
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
}
} else if (LoaderKind == 2) { // entries for elilo
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = PoolPrint(L"Run %s in interactive mode", FileName);
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = Entry->UseGraphicsMode;
SubEntry->LoadOptions = L"-p";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Linux for a 17\" iMac or a 15\" MacBook Pro (*)";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = TRUE;
SubEntry->LoadOptions = L"-d 0 i17";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Linux for a 20\" iMac (*)";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = TRUE;
SubEntry->LoadOptions = L"-d 0 i20";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Linux for a Mac Mini (*)";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = TRUE;
SubEntry->LoadOptions = L"-d 0 mini";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
AddMenuInfoLine(SubScreen, L"NOTE: This is an example. Entries");
AddMenuInfoLine(SubScreen, L"marked with (*) may not work.");
} else if (LoaderKind == 3) { // entries for xom.efi
// by default, skip the built-in selection and boot from hard disk only
Entry->LoadOptions = L"-s -h";
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Windows from Hard Disk";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = Entry->UseGraphicsMode;
SubEntry->LoadOptions = L"-s -h";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = L"Boot Windows from CD-ROM";
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = Entry->UseGraphicsMode;
SubEntry->LoadOptions = L"-s -c";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
SubEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
SubEntry->me.Title = PoolPrint(L"Run %s in text mode", FileName);
SubEntry->me.Tag = TAG_LOADER;
SubEntry->LoaderPath = Entry->LoaderPath;
SubEntry->VolName = Entry->VolName;
SubEntry->DevicePath = Entry->DevicePath;
SubEntry->UseGraphicsMode = FALSE;
SubEntry->LoadOptions = L"-v";
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
}
AddMenuEntry(SubScreen, &MenuEntryReturn);
Entry->me.SubScreen = SubScreen;
AddMenuEntry(&MainMenu, (REFIT_MENU_ENTRY *)Entry);
return Entry;
}
static VOID ScanLoaderDir(IN REFIT_VOLUME *Volume, IN CHAR16 *Path)
{
EFI_STATUS Status;
REFIT_DIR_ITER DirIter;
EFI_FILE_INFO *DirEntry;
CHAR16 FileName[256];
// look through contents of the directory
DirIterOpen(Volume->RootDir, Path, &DirIter);
while (DirIterNext(&DirIter, 2, L"*.EFI", &DirEntry)) {
if (DirEntry->FileName[0] == '.' ||
StriCmp(DirEntry->FileName, L"TextMode.efi") == 0 ||
StriCmp(DirEntry->FileName, L"ebounce.efi") == 0 ||
StriCmp(DirEntry->FileName, L"GraphicsConsole.efi") == 0)
continue; // skip this
if (Path)
SPrint(FileName, 255, L"\\%s\\%s", Path, DirEntry->FileName);
else
SPrint(FileName, 255, L"\\%s", DirEntry->FileName);
AddLoaderEntry(FileName, NULL, Volume);
}
Status = DirIterClose(&DirIter);
if (Status != EFI_NOT_FOUND) {
if (Path)
SPrint(FileName, 255, L"while scanning the %s directory", Path);
else
StrCpy(FileName, L"while scanning the root directory");
CheckError(Status, FileName);
}
}
static VOID ScanLoader(VOID)
{
EFI_STATUS Status;
UINTN VolumeIndex;
REFIT_VOLUME *Volume;
REFIT_DIR_ITER EfiDirIter;
EFI_FILE_INFO *EfiDirEntry;
CHAR16 FileName[256];
LOADER_ENTRY *Entry;
Print(L"Scanning for boot loaders...\n");
for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
Volume = Volumes[VolumeIndex];
if (Volume->RootDir == NULL || Volume->VolName == NULL)
continue;
// skip volume if its kind is configured as disabled
if ((Volume->DiskKind == DISK_KIND_OPTICAL && (GlobalConfig.DisableFlags & DISABLE_FLAG_OPTICAL)) ||
(Volume->DiskKind == DISK_KIND_EXTERNAL && (GlobalConfig.DisableFlags & DISABLE_FLAG_EXTERNAL)) ||
(Volume->DiskKind == DISK_KIND_INTERNAL && (GlobalConfig.DisableFlags & DISABLE_FLAG_INTERNAL)))
continue;
// check for Mac OS X boot loader
StrCpy(FileName, MACOSX_LOADER_PATH);
if (FileExists(Volume->RootDir, FileName)) {
Print(L" - Mac OS X boot file found\n");
Entry = AddLoaderEntry(FileName, L"Mac OS X", Volume);
}
// check for XOM
StrCpy(FileName, L"\\System\\Library\\CoreServices\\xom.efi");
if (FileExists(Volume->RootDir, FileName)) {
AddLoaderEntry(FileName, L"Windows XP (XoM)", Volume);
}
// check for Microsoft boot loader/menu
StrCpy(FileName, L"\\EFI\\Microsoft\\Boot\\Bootmgfw.efi");
if (FileExists(Volume->RootDir, FileName)) {
Print(L" - Microsoft boot menu found\n");
AddLoaderEntry(FileName, L"Microsoft boot menu", Volume);
}
// scan the root directory for EFI executables
ScanLoaderDir(Volume, NULL);
// scan the elilo directory (as used on gimli's first Live CD)
ScanLoaderDir(Volume, L"elilo");
// scan the boot directory
ScanLoaderDir(Volume, L"boot");
// scan subdirectories of the EFI directory (as per the standard)
DirIterOpen(Volume->RootDir, L"EFI", &EfiDirIter);
while (DirIterNext(&EfiDirIter, 1, NULL, &EfiDirEntry)) {
if (StriCmp(EfiDirEntry->FileName, L"TOOLS") == 0 ||
EfiDirEntry->FileName[0] == '.')
continue; // skip this, doesn't contain boot loaders
if (StriCmp(EfiDirEntry->FileName, L"REFIT") == 0 ||
StriCmp(EfiDirEntry->FileName, L"REFITL") == 0 ||
StriCmp(EfiDirEntry->FileName, L"RESCUE") == 0)
continue; // skip ourselves
Print(L" - Directory EFI\\%s found\n", EfiDirEntry->FileName);
SPrint(FileName, 255, L"EFI\\%s", EfiDirEntry->FileName);
ScanLoaderDir(Volume, FileName);
}
Status = DirIterClose(&EfiDirIter);
if (Status != EFI_NOT_FOUND)
CheckError(Status, L"while scanning the EFI directory");
}
}
//
// legacy boot functions
//
static EFI_STATUS ActivateMbrPartition(IN EFI_BLOCK_IO *BlockIO, IN UINTN PartitionIndex)
{
EFI_STATUS Status;
UINT8 SectorBuffer[512];
MBR_PARTITION_INFO *MbrTable, *EMbrTable;
UINT32 ExtBase, ExtCurrent, NextExtCurrent;
UINTN LogicalPartitionIndex = 4;
UINTN i;
BOOLEAN HaveBootCode;
// read MBR
Status = BlockIO->ReadBlocks(BlockIO, BlockIO->Media->MediaId, 0, 512, SectorBuffer);
if (EFI_ERROR(Status))
return Status;
if (*((UINT16 *)(SectorBuffer + 510)) != 0xaa55)
return EFI_NOT_FOUND; // safety measure #1
// add boot code if necessary
HaveBootCode = FALSE;
for (i = 0; i < MBR_BOOTCODE_SIZE; i++) {
if (SectorBuffer[i] != 0) {
HaveBootCode = TRUE;
break;
}
}
if (!HaveBootCode) {
// no boot code found in the MBR, add the syslinux MBR code
SetMem(SectorBuffer, MBR_BOOTCODE_SIZE, 0);
CopyMem(SectorBuffer, syslinux_mbr, SYSLINUX_MBR_SIZE);
}
// set the partition active
MbrTable = (MBR_PARTITION_INFO *)(SectorBuffer + 446);
ExtBase = 0;
for (i = 0; i < 4; i++) {
if (MbrTable[i].Flags != 0x00 && MbrTable[i].Flags != 0x80)
return EFI_NOT_FOUND; // safety measure #2
if (i == PartitionIndex)
MbrTable[i].Flags = 0x80;
else if (PartitionIndex >= 4 && IS_EXTENDED_PART_TYPE(MbrTable[i].Type)) {
MbrTable[i].Flags = 0x80;
ExtBase = MbrTable[i].StartLBA;
} else
MbrTable[i].Flags = 0x00;
}
// write MBR
Status = BlockIO->WriteBlocks(BlockIO, BlockIO->Media->MediaId, 0, 512, SectorBuffer);
if (EFI_ERROR(Status))
return Status;
if (PartitionIndex >= 4) {
// we have to activate a logical partition, so walk the EMBR chain
// NOTE: ExtBase was set above while looking at the MBR table
for (ExtCurrent = ExtBase; ExtCurrent; ExtCurrent = NextExtCurrent) {
// read current EMBR
Status = BlockIO->ReadBlocks(BlockIO, BlockIO->Media->MediaId, ExtCurrent, 512, SectorBuffer);
if (EFI_ERROR(Status))
return Status;
if (*((UINT16 *)(SectorBuffer + 510)) != 0xaa55)
return EFI_NOT_FOUND; // safety measure #3
// scan EMBR, set appropriate partition active
EMbrTable = (MBR_PARTITION_INFO *)(SectorBuffer + 446);
NextExtCurrent = 0;
for (i = 0; i < 4; i++) {
if (EMbrTable[i].Flags != 0x00 && EMbrTable[i].Flags != 0x80)
return EFI_NOT_FOUND; // safety measure #4
if (EMbrTable[i].StartLBA == 0 || EMbrTable[i].Size == 0)
break;
if (IS_EXTENDED_PART_TYPE(EMbrTable[i].Type)) {
// link to next EMBR
NextExtCurrent = ExtBase + EMbrTable[i].StartLBA;
EMbrTable[i].Flags = (PartitionIndex >= LogicalPartitionIndex) ? 0x80 : 0x00;
break;
} else {
// logical partition
EMbrTable[i].Flags = (PartitionIndex == LogicalPartitionIndex) ? 0x80 : 0x00;
LogicalPartitionIndex++;
}
}
// write current EMBR
Status = BlockIO->WriteBlocks(BlockIO, BlockIO->Media->MediaId, ExtCurrent, 512, SectorBuffer);
if (EFI_ERROR(Status))
return Status;
if (PartitionIndex < LogicalPartitionIndex)
break; // stop the loop, no need to touch further EMBRs
}
}
return EFI_SUCCESS;
}
static EFI_GUID AppleVariableVendorID = {
0x7C436110, 0xAB2A, 0x4BBB, 0xA8, 0x80, 0xFE, 0x41, 0x99, 0x5C, 0x9F, 0x82 };
static EFI_STATUS WriteBootDiskHint(IN EFI_DEVICE_PATH *WholeDiskDevicePath)
{
EFI_STATUS Status;
Status = RT->SetVariable(L"BootCampHD", &AppleVariableVendorID,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
DevicePathSize(WholeDiskDevicePath), WholeDiskDevicePath);
if (EFI_ERROR(Status))
return Status;
return EFI_SUCCESS;
}
// early 2006 Core Duo / Core Solo models
static UINT8 LegacyLoaderDevicePath1Data[] = {
0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xF9, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
};
// mid-2006 Mac Pro (and probably other Core 2 models)
static UINT8 LegacyLoaderDevicePath2Data[] = {
0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xF7, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
};
// mid-2007 MBP ("Santa Rosa" based models)
static UINT8 LegacyLoaderDevicePath3Data[] = {
0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xF8, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
};
// early-2008 MBA
static UINT8 LegacyLoaderDevicePath4Data[] = {
0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
0x00, 0x00, 0xC0, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xF8, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
};
// late-2008 MB/MBP (NVidia chipset)
static UINT8 LegacyLoaderDevicePath5Data[] = {
0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
0x00, 0x40, 0xCB, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xBF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
};
static EFI_DEVICE_PATH *LegacyLoaderList[] = {
(EFI_DEVICE_PATH *)LegacyLoaderDevicePath1Data,
(EFI_DEVICE_PATH *)LegacyLoaderDevicePath2Data,
(EFI_DEVICE_PATH *)LegacyLoaderDevicePath3Data,
(EFI_DEVICE_PATH *)LegacyLoaderDevicePath4Data,
(EFI_DEVICE_PATH *)LegacyLoaderDevicePath5Data,
NULL
};
#define MAX_DISCOVERED_PATHS (16)
static VOID StartLegacy(IN LEGACY_ENTRY *Entry)
{
EFI_STATUS Status;
EG_IMAGE *BootLogoImage;
UINTN ErrorInStep = 0;
EFI_DEVICE_PATH *DiscoveredPathList[MAX_DISCOVERED_PATHS];
BeginExternalScreen(TRUE, L"Booting Legacy OS");
BootLogoImage = LoadOSIcon(Entry->Volume->OSIconName, L"legacy", TRUE);
if (BootLogoImage != NULL)
BltImageAlpha(BootLogoImage,
(UGAWidth - BootLogoImage->Width ) >> 1,
(UGAHeight - BootLogoImage->Height) >> 1,
&StdBackgroundPixel);
if (Entry->Volume->IsMbrPartition)
ActivateMbrPartition(Entry->Volume->WholeDiskBlockIO, Entry->Volume->MbrPartitionIndex);
if (Entry->Volume->DiskKind != DISK_KIND_OPTICAL && Entry->Volume->WholeDiskDevicePath != NULL)
WriteBootDiskHint(Entry->Volume->WholeDiskDevicePath);
ExtractLegacyLoaderPaths(DiscoveredPathList, MAX_DISCOVERED_PATHS, LegacyLoaderList);
Status = StartEFIImageList(DiscoveredPathList, Entry->LoadOptions, NULL, L"legacy loader", &ErrorInStep);
if (Status == EFI_NOT_FOUND) {
if (ErrorInStep == 1)
Print(L"\nPlease make sure that you have the latest firmware update installed.\n");
else if (ErrorInStep == 3)
Print(L"\nThe firmware refused to boot from the selected volume. Note that external\n"
L"hard drives are not well-supported by Apple's firmware for legacy OS booting.\n");
}
FinishExternalScreen();
}
static LEGACY_ENTRY * AddLegacyEntry(IN CHAR16 *LoaderTitle, IN REFIT_VOLUME *Volume)
{
LEGACY_ENTRY *Entry, *SubEntry;
REFIT_MENU_SCREEN *SubScreen;
CHAR16 *VolDesc;
CHAR16 ShortcutLetter = 0;
if (LoaderTitle == NULL) {
if (Volume->OSName != NULL) {
LoaderTitle = Volume->OSName;
if (LoaderTitle[0] == 'W' || LoaderTitle[0] == 'L')
ShortcutLetter = LoaderTitle[0];
} else
LoaderTitle = L"Legacy OS";
}
if (Volume->VolName != NULL)
VolDesc = Volume->VolName;
else
VolDesc = (Volume->DiskKind == DISK_KIND_OPTICAL) ? L"CD" : L"HD";
// prepare the menu entry
Entry = AllocateZeroPool(sizeof(LEGACY_ENTRY));
Entry->me.Title = PoolPrint(L"Boot %s from %s", LoaderTitle, VolDesc);
Entry->me.Tag = TAG_LEGACY;
Entry->me.Row = 0;
Entry->me.ShortcutLetter = ShortcutLetter;
Entry->me.Image = LoadOSIcon(Volume->OSIconName, L"legacy", FALSE);
if (GlobalConfig.HideBadges == 0 ||
(GlobalConfig.HideBadges == 1 && Volume->DiskKind != DISK_KIND_INTERNAL))
Entry->me.BadgeImage = Volume->VolBadgeImage;
Entry->Volume = Volume;
Entry->LoadOptions = (Volume->DiskKind == DISK_KIND_OPTICAL) ? L"CD" :
((Volume->DiskKind == DISK_KIND_EXTERNAL) ? L"USB" : L"HD");
// create the submenu
SubScreen = AllocateZeroPool(sizeof(REFIT_MENU_SCREEN));
SubScreen->Title = PoolPrint(L"Boot Options for %s on %s", LoaderTitle, VolDesc);
SubScreen->TitleImage = Entry->me.Image;
// default entry
SubEntry = AllocateZeroPool(sizeof(LEGACY_ENTRY));
SubEntry->me.Title = PoolPrint(L"Boot %s", LoaderTitle);
SubEntry->me.Tag = TAG_LEGACY;
SubEntry->Volume = Entry->Volume;
SubEntry->LoadOptions = Entry->LoadOptions;
AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
AddMenuEntry(SubScreen, &MenuEntryReturn);
Entry->me.SubScreen = SubScreen;
AddMenuEntry(&MainMenu, (REFIT_MENU_ENTRY *)Entry);
return Entry;
}
static VOID ScanLegacy(VOID)
{
UINTN VolumeIndex, VolumeIndex2;
BOOLEAN ShowVolume, HideIfOthersFound;
REFIT_VOLUME *Volume;
Print(L"Scanning for legacy boot volumes...\n");
for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
Volume = Volumes[VolumeIndex];
#if REFIT_DEBUG > 0
Print(L" %d %s\n %d %d %s %d %s\n",
VolumeIndex, DevicePathToStr(Volume->DevicePath),
Volume->DiskKind, Volume->MbrPartitionIndex,
Volume->IsAppleLegacy ? L"AL" : L"--", Volume->HasBootCode,
Volume->VolName ? Volume->VolName : L"(no name)");
#endif
// skip volume if its kind is configured as disabled
if ((Volume->DiskKind == DISK_KIND_OPTICAL && (GlobalConfig.DisableFlags & DISABLE_FLAG_OPTICAL)) ||
(Volume->DiskKind == DISK_KIND_EXTERNAL && (GlobalConfig.DisableFlags & DISABLE_FLAG_EXTERNAL)) ||
(Volume->DiskKind == DISK_KIND_INTERNAL && (GlobalConfig.DisableFlags & DISABLE_FLAG_INTERNAL)))
continue;
ShowVolume = FALSE;
HideIfOthersFound = FALSE;
if (Volume->IsAppleLegacy) {
ShowVolume = TRUE;
HideIfOthersFound = TRUE;
} else if (Volume->HasBootCode) {
ShowVolume = TRUE;
if (Volume->BlockIO == Volume->WholeDiskBlockIO &&
Volume->BlockIOOffset == 0 &&
Volume->OSName == NULL)
// this is a whole disk (MBR) entry; hide if we have entries for partitions
HideIfOthersFound = TRUE;
}
if (HideIfOthersFound) {
// check for other bootable entries on the same disk
for (VolumeIndex2 = 0; VolumeIndex2 < VolumesCount; VolumeIndex2++) {
if (VolumeIndex2 != VolumeIndex && Volumes[VolumeIndex2]->HasBootCode &&
Volumes[VolumeIndex2]->WholeDiskBlockIO == Volume->WholeDiskBlockIO)
ShowVolume = FALSE;
}
}
if (ShowVolume)
AddLegacyEntry(NULL, Volume);
}
}
//
// pre-boot tool functions
//
static VOID StartTool(IN LOADER_ENTRY *Entry)
{
BeginExternalScreen(Entry->UseGraphicsMode, Entry->me.Title + 6); // assumes "Start <title>" as assigned below
StartEFIImage(Entry->DevicePath, Entry->LoadOptions, Basename(Entry->LoaderPath),
Basename(Entry->LoaderPath), NULL);
FinishExternalScreen();
}
static LOADER_ENTRY * AddToolEntry(IN CHAR16 *LoaderPath, IN CHAR16 *LoaderTitle, IN EG_IMAGE *Image,
IN CHAR16 ShortcutLetter, IN BOOLEAN UseGraphicsMode)
{
LOADER_ENTRY *Entry;
Entry = AllocateZeroPool(sizeof(LOADER_ENTRY));
Entry->me.Title = PoolPrint(L"Start %s", LoaderTitle);
Entry->me.Tag = TAG_TOOL;
Entry->me.Row = 1;
Entry->me.ShortcutLetter = ShortcutLetter;
Entry->me.Image = Image;
Entry->LoaderPath = StrDuplicate(LoaderPath);
Entry->DevicePath = FileDevicePath(SelfLoadedImage->DeviceHandle, Entry->LoaderPath);
Entry->UseGraphicsMode = UseGraphicsMode;
AddMenuEntry(&MainMenu, (REFIT_MENU_ENTRY *)Entry);
return Entry;
}
static VOID ScanTool(VOID)
{
//EFI_STATUS Status;
CHAR16 FileName[256];
LOADER_ENTRY *Entry;
if (GlobalConfig.DisableFlags & DISABLE_FLAG_TOOLS)
return;
Print(L"Scanning for tools...\n");
// look for the EFI shell
if (!(GlobalConfig.DisableFlags & DISABLE_FLAG_SHELL)) {
SPrint(FileName, 255, L"%s\\apps\\shell.efi", SelfDirPath);
if (FileExists(SelfRootDir, FileName)) {
AddToolEntry(FileName, L"EFI Shell", BuiltinIcon(BUILTIN_ICON_TOOL_SHELL), 'S', FALSE);
} else {
StrCpy(FileName, L"\\efi\\tools\\shell.efi");
if (FileExists(SelfRootDir, FileName)) {
AddToolEntry(FileName, L"EFI Shell", BuiltinIcon(BUILTIN_ICON_TOOL_SHELL), 'S', FALSE);
}
}
}
// look for the GPT/MBR sync tool
StrCpy(FileName, L"\\efi\\tools\\gptsync.efi");
if (FileExists(SelfRootDir, FileName)) {
AddToolEntry(FileName, L"Partitioning Tool", BuiltinIcon(BUILTIN_ICON_TOOL_PART), 'P', FALSE);
}
// look for rescue Linux
StrCpy(FileName, L"\\efi\\rescue\\elilo.efi");
if (SelfVolume != NULL && FileExists(SelfRootDir, FileName)) {
Entry = AddToolEntry(FileName, L"Rescue Linux", BuiltinIcon(BUILTIN_ICON_TOOL_RESCUE), 0, FALSE);
if (UGAWidth == 1440 && UGAHeight == 900)
Entry->LoadOptions = L"-d 0 i17";
else if (UGAWidth == 1680 && UGAHeight == 1050)
Entry->LoadOptions = L"-d 0 i20";
else
Entry->LoadOptions = L"-d 0 mini";
}
}
//
// pre-boot driver functions
//
static VOID ScanDriverDir(IN CHAR16 *Path)
{
EFI_STATUS Status;
REFIT_DIR_ITER DirIter;
EFI_FILE_INFO *DirEntry;
CHAR16 FileName[256];
// look through contents of the directory
DirIterOpen(SelfRootDir, Path, &DirIter);
while (DirIterNext(&DirIter, 2, L"*.EFI", &DirEntry)) {
if (DirEntry->FileName[0] == '.')
continue; // skip this
SPrint(FileName, 255, L"%s\\%s", Path, DirEntry->FileName);
Status = StartEFIImage(FileDevicePath(SelfLoadedImage->DeviceHandle, FileName),
L"", DirEntry->FileName, DirEntry->FileName, NULL);
}
Status = DirIterClose(&DirIter);
if (Status != EFI_NOT_FOUND) {
SPrint(FileName, 255, L"while scanning the %s directory", Path);
CheckError(Status, FileName);
}
}
static EFI_STATUS ConnectAllDriversToAllControllers(VOID)
{
EFI_STATUS Status;
UINTN AllHandleCount;
EFI_HANDLE *AllHandleBuffer;
UINTN Index;
UINTN HandleCount;
EFI_HANDLE *HandleBuffer;
UINT32 *HandleType;
UINTN HandleIndex;
BOOLEAN Parent;
BOOLEAN Device;
Status = LibLocateHandle(AllHandles,
NULL,
NULL,
&AllHandleCount,
&AllHandleBuffer);
if (EFI_ERROR(Status))
return Status;
for (Index = 0; Index < AllHandleCount; Index++) {
//
// Scan the handle database
//
Status = LibScanHandleDatabase(NULL,
NULL,
AllHandleBuffer[Index],
NULL,
&HandleCount,
&HandleBuffer,
&HandleType);
if (EFI_ERROR (Status))
goto Done;
Device = TRUE;
if (HandleType[Index] & EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE)
Device = FALSE;
if (HandleType[Index] & EFI_HANDLE_TYPE_IMAGE_HANDLE)
Device = FALSE;
if (Device) {
Parent = FALSE;
for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
if (HandleType[HandleIndex] & EFI_HANDLE_TYPE_PARENT_HANDLE)
Parent = TRUE;
}
if (!Parent) {
if (HandleType[Index] & EFI_HANDLE_TYPE_DEVICE_HANDLE) {
Status = BS->ConnectController(AllHandleBuffer[Index],
NULL,
NULL,
TRUE);
}
}
}
FreePool (HandleBuffer);
FreePool (HandleType);
}
Done:
FreePool (AllHandleBuffer);
return Status;
}
static VOID LoadDrivers(VOID)
{
CHAR16 DirName[256];
Print(L"Scanning for drivers...\n");
// load drivers from /efi/refit/drivers
SPrint(DirName, 255, L"%s\\drivers", SelfDirPath);
ScanDriverDir(DirName);
// load drivers from /efi/tools/drivers
ScanDriverDir(L"\\efi\\tools\\drivers");
// connect all devices
ConnectAllDriversToAllControllers();
}
//
// main entry point
//
#ifdef __GNUC__
#define RefitMain efi_main
#endif
EFI_STATUS
EFIAPI
RefitMain (IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable)
{
EFI_STATUS Status;
BOOLEAN MainLoopRunning = TRUE;
REFIT_MENU_ENTRY *ChosenEntry;
UINTN MenuExit;
UINTN i;
// bootstrap
InitializeLib(ImageHandle, SystemTable);
InitScreen();
Status = InitRefitLib(ImageHandle);
if (EFI_ERROR(Status))
return Status;
// read configuration
ReadConfig();
MainMenu.TimeoutSeconds = GlobalConfig.Timeout;
// disable EFI watchdog timer
BS->SetWatchdogTimer(0x0000, 0x0000, 0x0000, NULL);
// further bootstrap (now with config available)
SetupScreen();
LoadDrivers();
ScanVolumes();
DebugPause();
// scan for loaders and tools, add them to the menu
if (GlobalConfig.LegacyFirst)
ScanLegacy();
ScanLoader();
if (!GlobalConfig.LegacyFirst)
ScanLegacy();
ScanTool();
DebugPause();
// fixed other menu entries
if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_FUNCS)) {
MenuEntryAbout.Image = BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT);
AddMenuEntry(&MainMenu, &MenuEntryAbout);
}
if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_FUNCS) || MainMenu.EntryCount == 0) {
MenuEntryShutdown.Image = BuiltinIcon(BUILTIN_ICON_FUNC_SHUTDOWN);
AddMenuEntry(&MainMenu, &MenuEntryShutdown);
MenuEntryReset.Image = BuiltinIcon(BUILTIN_ICON_FUNC_RESET);
AddMenuEntry(&MainMenu, &MenuEntryReset);
}
// assign shortcut keys
for (i = 0; i < MainMenu.EntryCount && MainMenu.Entries[i]->Row == 0 && i < 9; i++)
MainMenu.Entries[i]->ShortcutDigit = (CHAR16)('1' + i);
// wait for user ACK when there were errors
FinishTextScreen(FALSE);
while (MainLoopRunning) {
MenuExit = RunMainMenu(&MainMenu, GlobalConfig.DefaultSelection, &ChosenEntry);
// We don't allow exiting the main menu with the Escape key.
if (MenuExit == MENU_EXIT_ESCAPE)
continue;
switch (ChosenEntry->Tag) {
case TAG_RESET: // Restart
TerminateScreen();
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
MainLoopRunning = FALSE; // just in case we get this far
break;
case TAG_SHUTDOWN: // Shut Down
TerminateScreen();
RT->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
MainLoopRunning = FALSE; // just in case we get this far
break;
case TAG_ABOUT: // About rEFIt
AboutRefit();
break;
case TAG_LOADER: // Boot OS via .EFI loader
StartLoader((LOADER_ENTRY *)ChosenEntry);
break;
case TAG_LEGACY: // Boot legacy OS
StartLegacy((LEGACY_ENTRY *)ChosenEntry);
break;
case TAG_TOOL: // Start a EFI tool
StartTool((LOADER_ENTRY *)ChosenEntry);
break;
}
}
// If we end up here, things have gone wrong. Try to reboot, and if that
// fails, go into an endless loop.
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
EndlessIdleLoop();
return EFI_SUCCESS;
}