3 * Main code for the boot menu
5 * Copyright (c) 2006-2010 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Modifications copyright (c) 2012-2015 Roderick W. Smith
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), or (at your option) any later version.
44 * This program is free software: you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation, either version 3 of the License, or
47 * (at your option) any later version.
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
54 * You should have received a copy of the GNU General Public License
55 * along with this program. If not, see <http://www.gnu.org/licenses/>.
68 #include "mystrings.h"
69 #include "security_policy.h"
70 #include "driver_support.h"
71 #include "../include/Handle.h"
72 #include "../include/refit_call_wrapper.h"
73 #include "../EfiLib/BdsHelper.h"
74 #include "../EfiLib/legacy.h"
76 #ifdef __MAKEWITH_GNUEFI
77 #ifndef EFI_SECURITY_VIOLATION
78 #define EFI_SECURITY_VIOLATION EFIERR (26)
82 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
83 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
86 #ifdef __MAKEWITH_TIANO
87 #define LibLocateHandle gBS->LocateHandleBuffer
93 #define MACOSX_LOADER_DIR L"System\\Library\\CoreServices"
94 #define MACOSX_LOADER_PATH ( MACOSX_LOADER_DIR L"\\boot.efi" )
96 #define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\EFI\\tools\\shellx64.efi,\\shell.efi,\\shellx64.efi"
97 #define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi,\\EFI\\tools\\gptsync_x64.efi"
98 #define GDISK_NAMES L"\\EFI\\tools\\gdisk.efi,\\EFI\\tools\\gdisk_x64.efi"
99 #define NETBOOT_NAMES L"\\EFI\\tools\\ipxe.efi"
100 #define MEMTEST_NAMES L"memtest86.efi,memtest86_x64.efi,memtest86x64.efi,bootx64.efi"
101 #define DRIVER_DIRS L"drivers,drivers_x64"
102 #define FALLBACK_FULLNAME L"EFI\\BOOT\\bootx64.efi"
103 #define FALLBACK_BASENAME L"bootx64.efi"
104 #define EFI_STUB_ARCH 0x8664
105 EFI_GUID gFreedesktopRootGuid
= { 0x4f68bce3, 0xe8cd, 0x4db1, { 0x96, 0xe7, 0xfb, 0xca, 0xf9, 0x84, 0xb7, 0x09 }};
106 #elif defined (EFI32)
107 #define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\EFI\\tools\\shellia32.efi,\\shell.efi,\\shellia32.efi"
108 #define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi,\\EFI\\tools\\gptsync_ia32.efi"
109 #define GDISK_NAMES L"\\EFI\\tools\\gdisk.efi,\\EFI\\tools\\gdisk_ia32.efi"
110 #define NETBOOT_NAMES L"\\EFI\\tools\\ipxe.efi"
111 #define MEMTEST_NAMES L"memtest86.efi,memtest86_ia32.efi,memtest86ia32.efi,bootia32.efi"
112 #define DRIVER_DIRS L"drivers,drivers_ia32"
113 #define FALLBACK_FULLNAME L"EFI\\BOOT\\bootia32.efi"
114 #define FALLBACK_BASENAME L"bootia32.efi"
115 #define EFI_STUB_ARCH 0x014c
116 EFI_GUID gFreedesktopRootGuid
= { 0x44479540, 0xf297, 0x41b2, { 0x9a, 0xf7, 0xd1, 0x31, 0xd5, 0xf0, 0x45, 0x8a }};
118 #define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\shell.efi"
119 #define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi"
120 #define GDISK_NAMES L"\\EFI\\tools\\gdisk.efi"
121 #define NETBOOT_NAMES L"\\EFI\\tools\\ipxe.efi"
122 #define MEMTEST_NAMES L"memtest86.efi"
123 #define DRIVER_DIRS L"drivers"
124 #define FALLBACK_FULLNAME L"EFI\\BOOT\\boot.efi" /* Not really correct */
125 #define FALLBACK_BASENAME L"boot.efi" /* Not really correct */
126 // Below is GUID for ARM64
127 EFI_GUID gFreedesktopRootGuid
= { 0xb921b045, 0x1df0, 0x41c3, { 0xaf, 0x44, 0x4c, 0x6f, 0x28, 0x0d, 0x3f, 0xae }};
129 #define FAT_ARCH 0x0ef1fab9 /* ID for Apple "fat" binary */
131 #define IPXE_DISCOVER_NAME L"\\efi\\tools\\ipxe_discover.efi"
132 #define IPXE_NAME L"\\efi\\tools\\ipxe.efi"
134 // Filename patterns that identify EFI boot loaders. Note that a single case (either L"*.efi" or
135 // L"*.EFI") is fine for most systems; but Gigabyte's buggy Hybrid EFI does a case-sensitive
136 // comparison when it should do a case-insensitive comparison, so I'm doubling this up. It does
137 // no harm on other computers, AFAIK. In theory, every case variation should be done for
138 // completeness, but that's ridiculous....
139 #define LOADER_MATCH_PATTERNS L"*.efi,*.EFI"
141 // Patterns that identify Linux kernels. Added to the loader match pattern when the
142 // scan_all_linux_kernels option is set in the configuration file. Causes kernels WITHOUT
143 // a ".efi" extension to be found when scanning for boot loaders.
144 #define LINUX_MATCH_PATTERNS L"vmlinuz*,bzImage*"
146 // Maximum length of a text string in certain menus
147 #define MAX_LINE_LENGTH 65
149 static REFIT_MENU_ENTRY MenuEntryAbout
= { L
"About rEFInd", TAG_ABOUT
, 1, 0, 'A', NULL
, NULL
, NULL
};
150 static REFIT_MENU_ENTRY MenuEntryReset
= { L
"Reboot Computer", TAG_REBOOT
, 1, 0, 'R', NULL
, NULL
, NULL
};
151 static REFIT_MENU_ENTRY MenuEntryShutdown
= { L
"Shut Down Computer", TAG_SHUTDOWN
, 1, 0, 'U', NULL
, NULL
, NULL
};
152 REFIT_MENU_ENTRY MenuEntryReturn
= { L
"Return to Main Menu", TAG_RETURN
, 1, 0, 0, NULL
, NULL
, NULL
};
153 static REFIT_MENU_ENTRY MenuEntryExit
= { L
"Exit rEFInd", TAG_EXIT
, 1, 0, 0, NULL
, NULL
, NULL
};
154 static REFIT_MENU_ENTRY MenuEntryFirmware
= { L
"Reboot to Computer Setup Utility", TAG_FIRMWARE
, 1, 0, 0, NULL
, NULL
, NULL
};
155 static REFIT_MENU_ENTRY MenuEntryRotateCsr
= { L
"Change SIP Policy", TAG_CSR_ROTATE
, 1, 0, 0, NULL
, NULL
, NULL
};
157 REFIT_MENU_SCREEN MainMenu
= { L
"Main Menu", NULL
, 0, NULL
, 0, NULL
, 0, L
"Automatic boot",
158 L
"Use arrow keys to move cursor; Enter to boot;",
159 L
"Insert or F2 for more options; Esc to refresh" };
160 static REFIT_MENU_SCREEN AboutMenu
= { L
"About", NULL
, 0, NULL
, 0, NULL
, 0, NULL
, L
"Press Enter to return to main menu", L
"" };
162 REFIT_CONFIG GlobalConfig
= { FALSE
, TRUE
, FALSE
, FALSE
, TRUE
, 0, 0, 0, DONT_CHANGE_TEXT_MODE
,
163 20, 0, 0, GRAPHICS_FOR_OSX
, LEGACY_TYPE_MAC
,
164 0, 0, { DEFAULT_BIG_ICON_SIZE
/ 4, DEFAULT_SMALL_ICON_SIZE
, DEFAULT_BIG_ICON_SIZE
},
165 BANNER_NOSCALE
, NULL
, NULL
, NULL
, NULL
, CONFIG_FILE_NAME
, NULL
, NULL
, NULL
, NULL
,
166 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
167 { TAG_SHELL
, TAG_MEMTEST
, TAG_GDISK
, TAG_APPLE_RECOVERY
, TAG_WINDOWS_RECOVERY
,
168 TAG_MOK_TOOL
, TAG_ABOUT
, TAG_SHUTDOWN
, TAG_REBOOT
, TAG_FIRMWARE
,
169 0, 0, 0, 0, 0, 0, 0, 0 }
172 EFI_GUID GlobalGuid
= EFI_GLOBAL_VARIABLE
;
173 EFI_GUID RefindGuid
= REFIND_GUID_VALUE
;
175 GPT_DATA
*gPartitions
= NULL
;
177 // Structure used to hold boot loader filenames and time stamps in
178 // a linked list; used to sort entries within a directory.
182 struct LOADER_LIST
*NextEntry
;
189 static VOID
AboutrEFInd(VOID
)
191 CHAR16
*FirmwareVendor
;
194 if (AboutMenu
.EntryCount
== 0) {
195 AboutMenu
.TitleImage
= BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT
);
196 AddMenuInfoLine(&AboutMenu
, L
"rEFInd Version 0.10.0.1");
197 AddMenuInfoLine(&AboutMenu
, L
"");
198 AddMenuInfoLine(&AboutMenu
, L
"Copyright (c) 2006-2010 Christoph Pfisterer");
199 AddMenuInfoLine(&AboutMenu
, L
"Copyright (c) 2012-2015 Roderick W. Smith");
200 AddMenuInfoLine(&AboutMenu
, L
"Portions Copyright (c) Intel Corporation and others");
201 AddMenuInfoLine(&AboutMenu
, L
"Distributed under the terms of the GNU GPLv3 license");
202 AddMenuInfoLine(&AboutMenu
, L
"");
203 AddMenuInfoLine(&AboutMenu
, L
"Running on:");
204 AddMenuInfoLine(&AboutMenu
, PoolPrint(L
" EFI Revision %d.%02d", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& ((1 << 16) - 1)));
206 AddMenuInfoLine(&AboutMenu
, PoolPrint(L
" Platform: x86 (32 bit); Secure Boot %s",
207 secure_mode() ? L
"active" : L
"inactive"));
208 #elif defined(EFIX64)
209 AddMenuInfoLine(&AboutMenu
, PoolPrint(L
" Platform: x86_64 (64 bit); Secure Boot %s",
210 secure_mode() ? L
"active" : L
"inactive"));
212 AddMenuInfoLine(&AboutMenu
, L
" Platform: unknown");
214 if (GetCsrStatus(&CsrStatus
) == EFI_SUCCESS
) {
215 RecordgCsrStatus(CsrStatus
, FALSE
);
216 AddMenuInfoLine(&AboutMenu
, gCsrStatus
);
218 FirmwareVendor
= StrDuplicate(ST
->FirmwareVendor
);
219 LimitStringLength(FirmwareVendor
, MAX_LINE_LENGTH
); // More than ~65 causes empty info page on 800x600 display
220 AddMenuInfoLine(&AboutMenu
, PoolPrint(L
" Firmware: %s %d.%02d", FirmwareVendor
, ST
->FirmwareRevision
>> 16,
221 ST
->FirmwareRevision
& ((1 << 16) - 1)));
222 AddMenuInfoLine(&AboutMenu
, PoolPrint(L
" Screen Output: %s", egScreenDescription()));
223 AddMenuInfoLine(&AboutMenu
, L
"");
224 #if defined(__MAKEWITH_GNUEFI)
225 AddMenuInfoLine(&AboutMenu
, L
"Built with GNU-EFI");
227 AddMenuInfoLine(&AboutMenu
, L
"Built with TianoCore EDK2");
229 AddMenuInfoLine(&AboutMenu
, L
"");
230 AddMenuInfoLine(&AboutMenu
, L
"For more information, see the rEFInd Web site:");
231 AddMenuInfoLine(&AboutMenu
, L
"http://www.rodsbooks.com/refind/");
232 AddMenuEntry(&AboutMenu
, &MenuEntryReturn
);
235 RunMenu(&AboutMenu
, NULL
);
236 } /* VOID AboutrEFInd() */
238 static VOID
WarnSecureBootError(CHAR16
*Name
, BOOLEAN Verbose
) {
240 Name
= L
"the loader";
242 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
243 Print(L
"Secure Boot validation failure loading %s!\n", Name
);
244 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
245 if (Verbose
&& secure_mode()) {
246 Print(L
"\nThis computer is configured with Secure Boot active, but\n%s has failed validation.\n", Name
);
247 Print(L
"\nYou can:\n * Launch another boot loader\n");
248 Print(L
" * Disable Secure Boot in your firmware\n");
249 Print(L
" * Sign %s with a machine owner key (MOK)\n", Name
);
250 Print(L
" * Use a MOK utility (often present on the second row) to add a MOK with which\n");
251 Print(L
" %s has already been signed.\n", Name
);
252 Print(L
" * Use a MOK utility to register %s (\"enroll its hash\") without\n", Name
);
253 Print(L
" signing it.\n");
254 Print(L
"\nSee http://www.rodsbooks.com/refind/secureboot.html for more information\n");
257 } // VOID WarnSecureBootError()
259 // Returns TRUE if this file is a valid EFI loader file, and is proper ARCH
260 static BOOLEAN
IsValidLoader(EFI_FILE
*RootDir
, CHAR16
*FileName
) {
261 BOOLEAN IsValid
= TRUE
;
262 #if defined (EFIX64) | defined (EFI32)
264 EFI_FILE_HANDLE FileHandle
;
266 UINTN Size
= sizeof(Header
);
268 if ((RootDir
== NULL
) || (FileName
== NULL
)) {
269 // Assume valid here, because Macs produce NULL RootDir (& maybe FileName)
270 // when launching from a Firewire drive. This should be handled better, but
271 // fix would have to be in StartEFIImageList() and/or in FindVolumeAndFilename().
275 Status
= refit_call5_wrapper(RootDir
->Open
, RootDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
276 if (EFI_ERROR(Status
))
279 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &Size
, Header
);
280 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
282 IsValid
= !EFI_ERROR(Status
) &&
283 Size
== sizeof(Header
) &&
284 ((Header
[0] == 'M' && Header
[1] == 'Z' &&
285 (Size
= *(UINT32
*)&Header
[0x3c]) < 0x180 &&
286 Header
[Size
] == 'P' && Header
[Size
+1] == 'E' &&
287 Header
[Size
+2] == 0 && Header
[Size
+3] == 0 &&
288 *(UINT16
*)&Header
[Size
+4] == EFI_STUB_ARCH
) ||
289 (*(UINT32
*)&Header
== FAT_ARCH
));
292 } // BOOLEAN IsValidLoader()
294 // Launch an EFI binary.
295 EFI_STATUS
StartEFIImageList(IN EFI_DEVICE_PATH
**DevicePaths
,
296 IN CHAR16
*LoadOptions
, IN UINTN LoaderType
,
297 IN CHAR16
*ImageTitle
, IN CHAR8 OSType
,
298 OUT UINTN
*ErrorInStep
,
302 EFI_STATUS Status
, ReturnStatus
;
303 EFI_HANDLE ChildImageHandle
, ChildImageHandle2
;
304 EFI_LOADED_IMAGE
*ChildLoadedImage
= NULL
;
305 REFIT_VOLUME
*Volume
= NULL
;
306 UINTN DevicePathIndex
;
307 CHAR16 ErrorInfo
[256];
308 CHAR16
*FullLoadOptions
= NULL
;
309 CHAR16
*Filename
= NULL
;
312 if (ErrorInStep
!= NULL
)
316 if (LoadOptions
!= NULL
) {
317 FullLoadOptions
= StrDuplicate(LoadOptions
);
318 if ((LoaderType
== TYPE_EFI
) && (OSType
== 'M')) {
319 MergeStrings(&FullLoadOptions
, L
" ", 0);
320 // NOTE: That last space is also added by the EFI shell and seems to be significant
321 // when passing options to Apple's boot.efi...
323 } // if (LoadOptions != NULL)
325 Print(L
"Starting %s\nUsing load options '%s'\n", ImageTitle
, FullLoadOptions
? FullLoadOptions
: L
"");
327 // load the image into memory
328 ReturnStatus
= Status
= EFI_NOT_FOUND
; // in case the list is empty
329 for (DevicePathIndex
= 0; DevicePaths
[DevicePathIndex
] != NULL
; DevicePathIndex
++) {
330 FindVolumeAndFilename(DevicePaths
[DevicePathIndex
], &Volume
, &Filename
);
331 // Some EFIs crash if attempting to load driver for invalid architecture, so
332 // protect for this condition; but sometimes Volume comes back NULL, so provide
333 // an exception. (TODO: Handle this special condition better.)
334 if ((LoaderType
== TYPE_LEGACY
) || (Volume
== NULL
) || IsValidLoader(Volume
->RootDir
, Filename
)) {
335 if (Filename
&& (LoaderType
!= TYPE_LEGACY
)) {
336 Temp
= PoolPrint(L
"\\%s %s", Filename
, FullLoadOptions
? FullLoadOptions
: L
"");
338 MyFreePool(FullLoadOptions
);
339 FullLoadOptions
= Temp
;
343 // NOTE: Below commented-out line could be more efficient if file were read ahead of
344 // time and passed as a pre-loaded image to LoadImage(), but it doesn't work on my
345 // 32-bit Mac Mini or my 64-bit Intel box when launching a Linux kernel; the
346 // kernel returns a "Failed to handle fs_proto" error message.
347 // TODO: Track down the cause of this error and fix it, if possible.
348 // ReturnStatus = Status = refit_call6_wrapper(BS->LoadImage, FALSE, SelfImageHandle, DevicePaths[DevicePathIndex],
349 // ImageData, ImageSize, &ChildImageHandle);
350 ReturnStatus
= Status
= refit_call6_wrapper(BS
->LoadImage
, FALSE
, SelfImageHandle
, DevicePaths
[DevicePathIndex
],
351 NULL
, 0, &ChildImageHandle
);
352 if (secure_mode() && ShimLoaded()) {
353 // Load ourself into memory. This is a trick to work around a bug in Shim 0.8,
354 // which ties itself into the BS->LoadImage() and BS->StartImage() functions and
355 // then unregisters itself from the EFI system table when its replacement
356 // StartImage() function is called *IF* the previous LoadImage() was for the same
357 // program. The result is that rEFInd can validate only the first program it
358 // launches (often a filesystem driver). Loading a second program (rEFInd itself,
359 // here, to keep it smaller than a kernel) works around this problem. See the
360 // replacements.c file in Shim, and especially its start_image() function, for
361 // the source of the problem.
362 // NOTE: This doesn't check the return status or handle errors. It could
363 // conceivably do weird things if, say, rEFInd were on a USB drive that the
364 // user pulls before launching a program.
365 refit_call6_wrapper(BS
->LoadImage
, FALSE
, SelfImageHandle
, GlobalConfig
.SelfDevicePath
,
366 NULL
, 0, &ChildImageHandle2
);
369 Print(L
"Invalid loader file!\n");
370 ReturnStatus
= EFI_LOAD_ERROR
;
372 if (ReturnStatus
!= EFI_NOT_FOUND
) {
376 if ((Status
== EFI_ACCESS_DENIED
) || (Status
== EFI_SECURITY_VIOLATION
)) {
377 WarnSecureBootError(ImageTitle
, Verbose
);
380 SPrint(ErrorInfo
, 255, L
"while loading %s", ImageTitle
);
381 if (CheckError(Status
, ErrorInfo
)) {
382 if (ErrorInStep
!= NULL
)
387 ReturnStatus
= Status
= refit_call3_wrapper(BS
->HandleProtocol
, ChildImageHandle
, &LoadedImageProtocol
,
388 (VOID
**) &ChildLoadedImage
);
389 if (CheckError(Status
, L
"while getting a LoadedImageProtocol handle")) {
390 if (ErrorInStep
!= NULL
)
394 ChildLoadedImage
->LoadOptions
= (VOID
*)FullLoadOptions
;
395 ChildLoadedImage
->LoadOptionsSize
= FullLoadOptions
? ((UINT32
)StrLen(FullLoadOptions
) + 1) * sizeof(CHAR16
) : 0;
396 // turn control over to the image
397 // TODO: (optionally) re-enable the EFI watchdog timer!
399 // close open file handles
401 ReturnStatus
= Status
= refit_call3_wrapper(BS
->StartImage
, ChildImageHandle
, NULL
, NULL
);
403 // control returns here when the child image calls Exit()
404 SPrint(ErrorInfo
, 255, L
"returned from %s", ImageTitle
);
405 if (CheckError(Status
, ErrorInfo
)) {
406 if (ErrorInStep
!= NULL
)
410 // re-open file handles
414 // unload the image, we don't care if it works or not...
416 Status
= refit_call1_wrapper(BS
->UnloadImage
, ChildImageHandle
);
419 MyFreePool(FullLoadOptions
);
421 } /* EFI_STATUS StartEFIImageList() */
423 static EFI_STATUS
StartEFIImage(IN EFI_DEVICE_PATH
*DevicePath
,
424 IN CHAR16
*LoadOptions
, IN UINTN LoaderType
,
425 IN CHAR16
*ImageTitle
, IN CHAR8 OSType
,
426 OUT UINTN
*ErrorInStep
,
431 EFI_DEVICE_PATH
*DevicePaths
[2];
433 DevicePaths
[0] = DevicePath
;
434 DevicePaths
[1] = NULL
;
435 return StartEFIImageList(DevicePaths
, LoadOptions
, LoaderType
, ImageTitle
, OSType
, ErrorInStep
, Verbose
, IsDriver
);
436 } /* static EFI_STATUS StartEFIImage() */
438 // From gummiboot: Reboot the computer into its built-in user interface
439 static EFI_STATUS
RebootIntoFirmware(VOID
) {
445 osind
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
447 err
= EfivarGetRaw(&GlobalGuid
, L
"OsIndications", &b
, &size
);
448 if (err
== EFI_SUCCESS
)
452 err
= EfivarSetRaw(&GlobalGuid
, L
"OsIndications", (CHAR8
*)&osind
, sizeof(UINT64
), TRUE
);
453 if (err
!= EFI_SUCCESS
)
456 refit_call4_wrapper(RT
->ResetSystem
, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
457 Print(L
"Error calling ResetSystem: %r", err
);
462 // Record the value of the loader's name/description in rEFInd's "PreviousBoot" EFI variable,
463 // if it's different from what's already stored there.
464 VOID
StoreLoaderName(IN CHAR16
*Name
) {
466 CHAR16
*OldName
= NULL
;
470 Status
= EfivarGetRaw(&RefindGuid
, L
"PreviousBoot", (CHAR8
**) &OldName
, &Length
);
471 if ((Status
!= EFI_SUCCESS
) || (StrCmp(OldName
, Name
) != 0)) {
472 EfivarSetRaw(&RefindGuid
, L
"PreviousBoot", (CHAR8
*) Name
, StrLen(Name
) * 2 + 2, TRUE
);
476 } // VOID StoreLoaderName()
479 // EFI OS loader functions
482 // See http://www.thomas-krenn.com/en/wiki/Activating_the_Intel_VT_Virtualization_Feature
483 // for information on Intel VMX features
484 static VOID
DoEnableAndLockVMX(VOID
)
487 UINT32 low_bits
= 0, high_bits
= 0;
490 __asm__
volatile ("rdmsr" : "=a" (low_bits
), "=d" (high_bits
) : "c" (msr
));
492 // enable and lock vmx if not locked
493 if ((low_bits
& 1) == 0) {
497 __asm__
volatile ("wrmsr" : : "c" (msr
), "a" (low_bits
), "d" (high_bits
));
499 } // VOID DoEnableAndLockVMX()
501 static VOID
StartLoader(LOADER_ENTRY
*Entry
, CHAR16
*SelectionName
)
503 UINTN ErrorInStep
= 0;
505 if (GlobalConfig
.EnableAndLockVMX
) {
506 DoEnableAndLockVMX();
509 BeginExternalScreen(Entry
->UseGraphicsMode
, L
"Booting OS");
510 StoreLoaderName(SelectionName
);
511 StartEFIImage(Entry
->DevicePath
, Entry
->LoadOptions
, TYPE_EFI
,
512 Basename(Entry
->LoaderPath
), Entry
->OSType
, &ErrorInStep
, !Entry
->UseGraphicsMode
, FALSE
);
513 FinishExternalScreen();
516 // Locate an initrd or initramfs file that matches the kernel specified by LoaderPath.
517 // The matching file has a name that begins with "init" and includes the same version
518 // number string as is found in LoaderPath -- but not a longer version number string.
519 // For instance, if LoaderPath is \EFI\kernels\bzImage-3.3.0.efi, and if \EFI\kernels
520 // has a file called initramfs-3.3.0.img, this function will return the string
521 // '\EFI\kernels\initramfs-3.3.0.img'. If the directory ALSO contains the file
522 // initramfs-3.3.0-rc7.img or initramfs-13.3.0.img, those files will NOT match;
523 // however, initmine-3.3.0.img might match. (FindInitrd() returns the first match it
524 // finds.) Thus, care should be taken to avoid placing duplicate matching files in
525 // the kernel's directory.
526 // If no matching init file can be found, returns NULL.
527 static CHAR16
* FindInitrd(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
528 CHAR16
*InitrdName
= NULL
, *FileName
, *KernelVersion
, *InitrdVersion
, *Path
;
529 REFIT_DIR_ITER DirIter
;
530 EFI_FILE_INFO
*DirEntry
;
532 FileName
= Basename(LoaderPath
);
533 KernelVersion
= FindNumbers(FileName
);
534 Path
= FindPath(LoaderPath
);
536 // Add trailing backslash for root directory; necessary on some systems, but must
537 // NOT be added to all directories, since on other systems, a trailing backslash on
538 // anything but the root directory causes them to flake out!
539 if (StrLen(Path
) == 0) {
540 MergeStrings(&Path
, L
"\\", 0);
542 DirIterOpen(Volume
->RootDir
, Path
, &DirIter
);
543 // Now add a trailing backslash if it was NOT added earlier, for consistency in
544 // building the InitrdName later....
545 if ((StrLen(Path
) > 0) && (Path
[StrLen(Path
) - 1] != L
'\\'))
546 MergeStrings(&Path
, L
"\\", 0);
547 while ((DirIterNext(&DirIter
, 2, L
"init*", &DirEntry
)) && (InitrdName
== NULL
)) {
548 InitrdVersion
= FindNumbers(DirEntry
->FileName
);
549 if (KernelVersion
!= NULL
) {
550 if (MyStriCmp(InitrdVersion
, KernelVersion
)) {
551 InitrdName
= PoolPrint(L
"%s%s", Path
, DirEntry
->FileName
);
554 if (InitrdVersion
== NULL
) {
555 InitrdName
= PoolPrint(L
"%s%s", Path
, DirEntry
->FileName
);
558 MyFreePool(InitrdVersion
);
560 DirIterClose(&DirIter
);
562 // Note: Don't FreePool(FileName), since Basename returns a pointer WITHIN the string it's passed.
563 MyFreePool(KernelVersion
);
566 } // static CHAR16 * FindInitrd()
568 LOADER_ENTRY
* AddPreparedLoaderEntry(LOADER_ENTRY
*Entry
) {
569 AddMenuEntry(&MainMenu
, (REFIT_MENU_ENTRY
*)Entry
);
572 } // LOADER_ENTRY * AddPreparedLoaderEntry()
574 // Creates a copy of a menu screen.
575 // Returns a pointer to the copy of the menu screen.
576 static REFIT_MENU_SCREEN
* CopyMenuScreen(REFIT_MENU_SCREEN
*Entry
) {
577 REFIT_MENU_SCREEN
*NewEntry
;
580 NewEntry
= AllocateZeroPool(sizeof(REFIT_MENU_SCREEN
));
581 if ((Entry
!= NULL
) && (NewEntry
!= NULL
)) {
582 CopyMem(NewEntry
, Entry
, sizeof(REFIT_MENU_SCREEN
));
583 NewEntry
->Title
= (Entry
->Title
) ? StrDuplicate(Entry
->Title
) : NULL
;
584 NewEntry
->TimeoutText
= (Entry
->TimeoutText
) ? StrDuplicate(Entry
->TimeoutText
) : NULL
;
585 if (Entry
->TitleImage
!= NULL
) {
586 NewEntry
->TitleImage
= AllocatePool(sizeof(EG_IMAGE
));
587 if (NewEntry
->TitleImage
!= NULL
)
588 CopyMem(NewEntry
->TitleImage
, Entry
->TitleImage
, sizeof(EG_IMAGE
));
590 NewEntry
->InfoLines
= (CHAR16
**) AllocateZeroPool(Entry
->InfoLineCount
* (sizeof(CHAR16
*)));
591 for (i
= 0; i
< Entry
->InfoLineCount
&& NewEntry
->InfoLines
; i
++) {
592 NewEntry
->InfoLines
[i
] = (Entry
->InfoLines
[i
]) ? StrDuplicate(Entry
->InfoLines
[i
]) : NULL
;
594 NewEntry
->Entries
= (REFIT_MENU_ENTRY
**) AllocateZeroPool(Entry
->EntryCount
* (sizeof (REFIT_MENU_ENTRY
*)));
595 for (i
= 0; i
< Entry
->EntryCount
&& NewEntry
->Entries
; i
++) {
596 AddMenuEntry(NewEntry
, Entry
->Entries
[i
]);
598 NewEntry
->Hint1
= (Entry
->Hint1
) ? StrDuplicate(Entry
->Hint1
) : NULL
;
599 NewEntry
->Hint2
= (Entry
->Hint2
) ? StrDuplicate(Entry
->Hint2
) : NULL
;
602 } // static REFIT_MENU_SCREEN* CopyMenuScreen()
604 // Creates a copy of a menu entry. Intended to enable moving a stack-based
605 // menu entry (such as the ones for the "reboot" and "exit" functions) to
606 // to the heap. This enables easier deletion of the whole set of menu
607 // entries when re-scanning.
608 // Returns a pointer to the copy of the menu entry.
609 static REFIT_MENU_ENTRY
* CopyMenuEntry(REFIT_MENU_ENTRY
*Entry
) {
610 REFIT_MENU_ENTRY
*NewEntry
;
612 NewEntry
= AllocateZeroPool(sizeof(REFIT_MENU_ENTRY
));
613 if ((Entry
!= NULL
) && (NewEntry
!= NULL
)) {
614 CopyMem(NewEntry
, Entry
, sizeof(REFIT_MENU_ENTRY
));
615 NewEntry
->Title
= (Entry
->Title
) ? StrDuplicate(Entry
->Title
) : NULL
;
616 if (Entry
->BadgeImage
!= NULL
) {
617 NewEntry
->BadgeImage
= AllocatePool(sizeof(EG_IMAGE
));
618 if (NewEntry
->BadgeImage
!= NULL
)
619 CopyMem(NewEntry
->BadgeImage
, Entry
->BadgeImage
, sizeof(EG_IMAGE
));
621 if (Entry
->Image
!= NULL
) {
622 NewEntry
->Image
= AllocatePool(sizeof(EG_IMAGE
));
623 if (NewEntry
->Image
!= NULL
)
624 CopyMem(NewEntry
->Image
, Entry
->Image
, sizeof(EG_IMAGE
));
626 if (Entry
->SubScreen
!= NULL
) {
627 NewEntry
->SubScreen
= CopyMenuScreen(Entry
->SubScreen
);
631 } // REFIT_MENU_ENTRY* CopyMenuEntry()
633 // Creates a new LOADER_ENTRY data structure and populates it with
634 // default values from the specified Entry, or NULL values if Entry
635 // is unspecified (NULL).
636 // Returns a pointer to the new data structure, or NULL if it
637 // couldn't be allocated
638 LOADER_ENTRY
*InitializeLoaderEntry(IN LOADER_ENTRY
*Entry
) {
639 LOADER_ENTRY
*NewEntry
= NULL
;
641 NewEntry
= AllocateZeroPool(sizeof(LOADER_ENTRY
));
642 if (NewEntry
!= NULL
) {
643 NewEntry
->me
.Title
= NULL
;
644 NewEntry
->me
.Tag
= TAG_LOADER
;
645 NewEntry
->Enabled
= TRUE
;
646 NewEntry
->UseGraphicsMode
= FALSE
;
647 NewEntry
->OSType
= 0;
649 NewEntry
->LoaderPath
= (Entry
->LoaderPath
) ? StrDuplicate(Entry
->LoaderPath
) : NULL
;
650 NewEntry
->VolName
= (Entry
->VolName
) ? StrDuplicate(Entry
->VolName
) : NULL
;
651 NewEntry
->DevicePath
= Entry
->DevicePath
;
652 NewEntry
->UseGraphicsMode
= Entry
->UseGraphicsMode
;
653 NewEntry
->LoadOptions
= (Entry
->LoadOptions
) ? StrDuplicate(Entry
->LoadOptions
) : NULL
;
654 NewEntry
->InitrdPath
= (Entry
->InitrdPath
) ? StrDuplicate(Entry
->InitrdPath
) : NULL
;
658 } // LOADER_ENTRY *InitializeLoaderEntry()
660 // Adds InitrdPath to Options, but only if Options doesn't already include an
661 // initrd= line. Done to enable overriding the default initrd selection in a
662 // refind_linux.conf file's options list.
663 // Returns a pointer to a new string. The calling function is responsible for
664 // freeing its memory.
665 static CHAR16
*AddInitrdToOptions(CHAR16
*Options
, CHAR16
*InitrdPath
) {
666 CHAR16
*NewOptions
= NULL
;
669 NewOptions
= StrDuplicate(Options
);
670 if ((InitrdPath
!= NULL
) && !StriSubCmp(L
"initrd=", Options
)) {
671 MergeStrings(&NewOptions
, L
"initrd=", L
' ');
672 MergeStrings(&NewOptions
, InitrdPath
, 0);
675 } // CHAR16 *AddInitrdToOptions()
677 // Prepare a REFIT_MENU_SCREEN data structure for a subscreen entry. This sets up
678 // the default entry that launches the boot loader using the same options as the
679 // main Entry does. Subsequent options can be added by the calling function.
680 // If a subscreen already exists in the Entry that's passed to this function,
681 // it's left unchanged and a pointer to it is returned.
682 // Returns a pointer to the new subscreen data structure, or NULL if there
683 // were problems allocating memory.
684 REFIT_MENU_SCREEN
*InitializeSubScreen(IN LOADER_ENTRY
*Entry
) {
685 CHAR16
*FileName
, *MainOptions
= NULL
;
686 REFIT_MENU_SCREEN
*SubScreen
= NULL
;
687 LOADER_ENTRY
*SubEntry
;
689 FileName
= Basename(Entry
->LoaderPath
);
690 if (Entry
->me
.SubScreen
== NULL
) { // No subscreen yet; initialize default entry....
691 SubScreen
= AllocateZeroPool(sizeof(REFIT_MENU_SCREEN
));
692 if (SubScreen
!= NULL
) {
693 SubScreen
->Title
= AllocateZeroPool(sizeof(CHAR16
) * 256);
694 SPrint(SubScreen
->Title
, 255, L
"Boot Options for %s on %s",
695 (Entry
->Title
!= NULL
) ? Entry
->Title
: FileName
, Entry
->VolName
);
696 SubScreen
->TitleImage
= Entry
->me
.Image
;
698 SubEntry
= InitializeLoaderEntry(Entry
);
699 if (SubEntry
!= NULL
) {
700 SubEntry
->me
.Title
= StrDuplicate(L
"Boot using default options");
701 MainOptions
= SubEntry
->LoadOptions
;
702 SubEntry
->LoadOptions
= AddInitrdToOptions(MainOptions
, SubEntry
->InitrdPath
);
703 MyFreePool(MainOptions
);
704 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
705 } // if (SubEntry != NULL)
706 SubScreen
->Hint1
= StrDuplicate(SUBSCREEN_HINT1
);
707 if (GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_EDITOR
) {
708 SubScreen
->Hint2
= StrDuplicate(SUBSCREEN_HINT2_NO_EDITOR
);
710 SubScreen
->Hint2
= StrDuplicate(SUBSCREEN_HINT2
);
712 } // if (SubScreen != NULL)
713 } else { // existing subscreen; less initialization, and just add new entry later....
714 SubScreen
= Entry
->me
.SubScreen
;
717 } // REFIT_MENU_SCREEN *InitializeSubScreen()
719 VOID
GenerateSubScreen(LOADER_ENTRY
*Entry
, IN REFIT_VOLUME
*Volume
, IN BOOLEAN GenerateReturn
) {
720 REFIT_MENU_SCREEN
*SubScreen
;
721 LOADER_ENTRY
*SubEntry
;
723 CHAR16 DiagsFileName
[256];
728 // create the submenu
729 if (StrLen(Entry
->Title
) == 0) {
730 MyFreePool(Entry
->Title
);
733 SubScreen
= InitializeSubScreen(Entry
);
735 // loader-specific submenu entries
736 if (Entry
->OSType
== 'M') { // entries for Mac OS X
738 SubEntry
= InitializeLoaderEntry(Entry
);
739 if (SubEntry
!= NULL
) {
740 SubEntry
->me
.Title
= L
"Boot Mac OS X with a 64-bit kernel";
741 SubEntry
->LoadOptions
= L
"arch=x86_64";
742 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_OSX
;
743 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
746 SubEntry
= InitializeLoaderEntry(Entry
);
747 if (SubEntry
!= NULL
) {
748 SubEntry
->me
.Title
= L
"Boot Mac OS X with a 32-bit kernel";
749 SubEntry
->LoadOptions
= L
"arch=i386";
750 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_OSX
;
751 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
755 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_SINGLEUSER
)) {
756 SubEntry
= InitializeLoaderEntry(Entry
);
757 if (SubEntry
!= NULL
) {
758 SubEntry
->me
.Title
= L
"Boot Mac OS X in verbose mode";
759 SubEntry
->UseGraphicsMode
= FALSE
;
760 SubEntry
->LoadOptions
= L
"-v";
761 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
765 SubEntry
= InitializeLoaderEntry(Entry
);
766 if (SubEntry
!= NULL
) {
767 SubEntry
->me
.Title
= L
"Boot Mac OS X in verbose mode (64-bit)";
768 SubEntry
->UseGraphicsMode
= FALSE
;
769 SubEntry
->LoadOptions
= L
"-v arch=x86_64";
770 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
773 SubEntry
= InitializeLoaderEntry(Entry
);
774 if (SubEntry
!= NULL
) {
775 SubEntry
->me
.Title
= L
"Boot Mac OS X in verbose mode (32-bit)";
776 SubEntry
->UseGraphicsMode
= FALSE
;
777 SubEntry
->LoadOptions
= L
"-v arch=i386";
778 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
782 SubEntry
= InitializeLoaderEntry(Entry
);
783 if (SubEntry
!= NULL
) {
784 SubEntry
->me
.Title
= L
"Boot Mac OS X in single user mode";
785 SubEntry
->UseGraphicsMode
= FALSE
;
786 SubEntry
->LoadOptions
= L
"-v -s";
787 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
789 } // single-user mode allowed
791 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_SAFEMODE
)) {
792 SubEntry
= InitializeLoaderEntry(Entry
);
793 if (SubEntry
!= NULL
) {
794 SubEntry
->me
.Title
= L
"Boot Mac OS X in safe mode";
795 SubEntry
->UseGraphicsMode
= FALSE
;
796 SubEntry
->LoadOptions
= L
"-v -x";
797 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
799 } // safe mode allowed
801 // check for Apple hardware diagnostics
802 StrCpy(DiagsFileName
, L
"System\\Library\\CoreServices\\.diagnostics\\diags.efi");
803 if (FileExists(Volume
->RootDir
, DiagsFileName
) && !(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HWTEST
)) {
804 SubEntry
= InitializeLoaderEntry(Entry
);
805 if (SubEntry
!= NULL
) {
806 SubEntry
->me
.Title
= L
"Run Apple Hardware Test";
807 MyFreePool(SubEntry
->LoaderPath
);
808 SubEntry
->LoaderPath
= StrDuplicate(DiagsFileName
);
809 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
810 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_OSX
;
811 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
813 } // if diagnostics entry found
815 } else if (Entry
->OSType
== 'L') { // entries for Linux kernels with EFI stub loaders
816 File
= ReadLinuxOptionsFile(Entry
->LoaderPath
, Volume
);
818 InitrdName
= FindInitrd(Entry
->LoaderPath
, Volume
);
819 TokenCount
= ReadTokenLine(File
, &TokenList
);
820 // first entry requires special processing, since it was initially set
821 // up with a default title but correct options by InitializeSubScreen(),
823 if ((TokenCount
> 1) && (SubScreen
->Entries
!= NULL
) && (SubScreen
->Entries
[0] != NULL
)) {
824 MyFreePool(SubScreen
->Entries
[0]->Title
);
825 SubScreen
->Entries
[0]->Title
= TokenList
[0] ? StrDuplicate(TokenList
[0]) : StrDuplicate(L
"Boot Linux");
827 FreeTokenLine(&TokenList
, &TokenCount
);
828 while ((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 1) {
829 SubEntry
= InitializeLoaderEntry(Entry
);
830 SubEntry
->me
.Title
= TokenList
[0] ? StrDuplicate(TokenList
[0]) : StrDuplicate(L
"Boot Linux");
831 MyFreePool(SubEntry
->LoadOptions
);
832 SubEntry
->LoadOptions
= AddInitrdToOptions(TokenList
[1], InitrdName
);
833 FreeTokenLine(&TokenList
, &TokenCount
);
834 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_LINUX
;
835 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
837 MyFreePool(InitrdName
);
841 } else if (Entry
->OSType
== 'E') { // entries for ELILO
842 SubEntry
= InitializeLoaderEntry(Entry
);
843 if (SubEntry
!= NULL
) {
844 SubEntry
->me
.Title
= L
"Run ELILO in interactive mode";
845 SubEntry
->LoadOptions
= L
"-p";
846 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_ELILO
;
847 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
850 SubEntry
= InitializeLoaderEntry(Entry
);
851 if (SubEntry
!= NULL
) {
852 SubEntry
->me
.Title
= L
"Boot Linux for a 17\" iMac or a 15\" MacBook Pro (*)";
853 SubEntry
->LoadOptions
= L
"-d 0 i17";
854 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_ELILO
;
855 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
858 SubEntry
= InitializeLoaderEntry(Entry
);
859 if (SubEntry
!= NULL
) {
860 SubEntry
->me
.Title
= L
"Boot Linux for a 20\" iMac (*)";
861 SubEntry
->LoadOptions
= L
"-d 0 i20";
862 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_ELILO
;
863 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
866 SubEntry
= InitializeLoaderEntry(Entry
);
867 if (SubEntry
!= NULL
) {
868 SubEntry
->me
.Title
= L
"Boot Linux for a Mac Mini (*)";
869 SubEntry
->LoadOptions
= L
"-d 0 mini";
870 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_ELILO
;
871 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
874 AddMenuInfoLine(SubScreen
, L
"NOTE: This is an example. Entries");
875 AddMenuInfoLine(SubScreen
, L
"marked with (*) may not work.");
877 } else if (Entry
->OSType
== 'X') { // entries for xom.efi
878 // by default, skip the built-in selection and boot from hard disk only
879 Entry
->LoadOptions
= L
"-s -h";
881 SubEntry
= InitializeLoaderEntry(Entry
);
882 if (SubEntry
!= NULL
) {
883 SubEntry
->me
.Title
= L
"Boot Windows from Hard Disk";
884 SubEntry
->LoadOptions
= L
"-s -h";
885 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_WINDOWS
;
886 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
889 SubEntry
= InitializeLoaderEntry(Entry
);
890 if (SubEntry
!= NULL
) {
891 SubEntry
->me
.Title
= L
"Boot Windows from CD-ROM";
892 SubEntry
->LoadOptions
= L
"-s -c";
893 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_WINDOWS
;
894 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
897 SubEntry
= InitializeLoaderEntry(Entry
);
898 if (SubEntry
!= NULL
) {
899 SubEntry
->me
.Title
= L
"Run XOM in text mode";
900 SubEntry
->LoadOptions
= L
"-v";
901 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_WINDOWS
;
902 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
904 } // entries for xom.efi
906 AddMenuEntry(SubScreen
, &MenuEntryReturn
);
907 Entry
->me
.SubScreen
= SubScreen
;
908 } // VOID GenerateSubScreen()
910 // Returns options for a Linux kernel. Reads them from an options file in the
911 // kernel's directory; and if present, adds an initrd= option for an initial
912 // RAM disk file with the same version number as the kernel file.
913 static CHAR16
* GetMainLinuxOptions(IN CHAR16
* LoaderPath
, IN REFIT_VOLUME
*Volume
) {
914 CHAR16
*Options
= NULL
, *InitrdName
, *FullOptions
= NULL
;
916 Options
= GetFirstOptionsFromFile(LoaderPath
, Volume
);
917 InitrdName
= FindInitrd(LoaderPath
, Volume
);
918 FullOptions
= AddInitrdToOptions(Options
, InitrdName
);
921 MyFreePool(InitrdName
);
922 return (FullOptions
);
923 } // static CHAR16 * GetMainLinuxOptions()
925 // Read the specified file and add values of "ID", "NAME", or "DISTRIB_ID" tokens to
926 // OSIconName list. Intended for adding Linux distribution clues gleaned from
927 // /etc/lsb-release and /etc/os-release files.
928 static VOID
ParseReleaseFile(CHAR16
**OSIconName
, REFIT_VOLUME
*Volume
, CHAR16
*FileName
) {
932 UINTN TokenCount
= 0;
934 if ((Volume
== NULL
) || (FileName
== NULL
) || (OSIconName
== NULL
) || (*OSIconName
== NULL
))
937 if (FileExists(Volume
->RootDir
, FileName
) &&
938 (ReadFile(Volume
->RootDir
, FileName
, &File
, &FileSize
) == EFI_SUCCESS
)) {
940 TokenCount
= ReadTokenLine(&File
, &TokenList
);
941 if ((TokenCount
> 1) && (MyStriCmp(TokenList
[0], L
"ID") ||
942 MyStriCmp(TokenList
[0], L
"NAME") ||
943 MyStriCmp(TokenList
[0], L
"DISTRIB_ID"))) {
944 MergeWords(OSIconName
, TokenList
[1], L
',');
946 FreeTokenLine(&TokenList
, &TokenCount
);
947 } while (TokenCount
> 0);
948 MyFreePool(File
.Buffer
);
950 } // VOID ParseReleaseFile()
952 // Try to guess the name of the Linux distribution & add that name to
954 static VOID
GuessLinuxDistribution(CHAR16
**OSIconName
, REFIT_VOLUME
*Volume
, CHAR16
*LoaderPath
) {
955 // If on Linux root fs, /etc/os-release or /etc/lsb-release file probably has clues....
956 ParseReleaseFile(OSIconName
, Volume
, L
"etc\\lsb-release");
957 ParseReleaseFile(OSIconName
, Volume
, L
"etc\\os-release");
959 // Search for clues in the kernel's filename....
960 if (StriSubCmp(L
".fc", LoaderPath
))
961 MergeStrings(OSIconName
, L
"fedora", L
',');
962 if (StriSubCmp(L
".el", LoaderPath
))
963 MergeStrings(OSIconName
, L
"redhat", L
',');
964 } // VOID GuessLinuxDistribution()
966 // Sets a few defaults for a loader entry -- mainly the icon, but also the OS type
967 // code and shortcut letter. For Linux EFI stub loaders, also sets kernel options
968 // that will (with luck) work fairly automatically.
969 VOID
SetLoaderDefaults(LOADER_ENTRY
*Entry
, CHAR16
*LoaderPath
, REFIT_VOLUME
*Volume
) {
970 CHAR16
*NameClues
, *PathOnly
, *NoExtension
, *OSIconName
= NULL
, *Temp
;
971 CHAR16 ShortcutLetter
= 0;
973 NameClues
= Basename(LoaderPath
);
974 PathOnly
= FindPath(LoaderPath
);
975 NoExtension
= StripEfiExtension(NameClues
);
977 if (Volume
->DiskKind
== DISK_KIND_NET
) {
978 MergeStrings(&NameClues
, Entry
->me
.Title
, L
' ');
980 // locate a custom icon for the loader
981 // Anything found here takes precedence over the "hints" in the OSIconName variable
982 if (!Entry
->me
.Image
) {
983 Entry
->me
.Image
= egLoadIconAnyType(Volume
->RootDir
, PathOnly
, NoExtension
, GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
985 if (!Entry
->me
.Image
) {
986 Entry
->me
.Image
= egCopyImage(Volume
->VolIconImage
);
989 // Begin creating icon "hints" by using last part of directory path leading
991 Temp
= FindLastDirName(LoaderPath
);
992 MergeStrings(&OSIconName
, Temp
, L
',');
995 if (OSIconName
!= NULL
) {
996 ShortcutLetter
= OSIconName
[0];
999 // Add every "word" in the volume label, delimited by spaces, dashes (-), or
1000 // underscores (_), to the list of hints to be used in searching for OS
1002 MergeWords(&OSIconName
, Volume
->VolName
, L
',');
1003 } // if/else network boot
1005 // detect specific loaders
1006 if (StriSubCmp(L
"bzImage", NameClues
) || StriSubCmp(L
"vmlinuz", NameClues
)) {
1007 if (Volume
->DiskKind
!= DISK_KIND_NET
) {
1008 GuessLinuxDistribution(&OSIconName
, Volume
, LoaderPath
);
1009 Entry
->LoadOptions
= GetMainLinuxOptions(LoaderPath
, Volume
);
1011 MergeStrings(&OSIconName
, L
"linux", L
',');
1012 Entry
->OSType
= 'L';
1013 if (ShortcutLetter
== 0)
1014 ShortcutLetter
= 'L';
1015 Entry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_LINUX
;
1016 } else if (StriSubCmp(L
"refit", LoaderPath
)) {
1017 MergeStrings(&OSIconName
, L
"refit", L
',');
1018 Entry
->OSType
= 'R';
1019 ShortcutLetter
= 'R';
1020 } else if (StriSubCmp(L
"refind", LoaderPath
)) {
1021 MergeStrings(&OSIconName
, L
"refind", L
',');
1022 Entry
->OSType
= 'R';
1023 ShortcutLetter
= 'R';
1024 } else if (MyStriCmp(LoaderPath
, MACOSX_LOADER_PATH
)) {
1025 MergeStrings(&OSIconName
, L
"mac", L
',');
1026 Entry
->OSType
= 'M';
1027 ShortcutLetter
= 'M';
1028 Entry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_OSX
;
1029 } else if (MyStriCmp(NameClues
, L
"diags.efi")) {
1030 MergeStrings(&OSIconName
, L
"hwtest", L
',');
1031 } else if (MyStriCmp(NameClues
, L
"e.efi") || MyStriCmp(NameClues
, L
"elilo.efi") || StriSubCmp(L
"elilo", NameClues
)) {
1032 MergeStrings(&OSIconName
, L
"elilo,linux", L
',');
1033 Entry
->OSType
= 'E';
1034 if (ShortcutLetter
== 0)
1035 ShortcutLetter
= 'L';
1036 Entry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_ELILO
;
1037 } else if (StriSubCmp(L
"grub", NameClues
)) {
1038 MergeStrings(&OSIconName
, L
"grub,linux", L
',');
1039 Entry
->OSType
= 'G';
1040 ShortcutLetter
= 'G';
1041 Entry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_GRUB
;
1042 } else if (MyStriCmp(NameClues
, L
"cdboot.efi") ||
1043 MyStriCmp(NameClues
, L
"bootmgr.efi") ||
1044 MyStriCmp(NameClues
, L
"bootmgfw.efi") ||
1045 MyStriCmp(NameClues
, L
"bkpbootmgfw.efi")) {
1046 MergeStrings(&OSIconName
, L
"win8", L
',');
1047 Entry
->OSType
= 'W';
1048 ShortcutLetter
= 'W';
1049 Entry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_WINDOWS
;
1050 } else if (MyStriCmp(NameClues
, L
"xom.efi")) {
1051 MergeStrings(&OSIconName
, L
"xom,win,win8", L
',');
1052 Entry
->OSType
= 'X';
1053 ShortcutLetter
= 'W';
1054 Entry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_WINDOWS
;
1056 else if (StriSubCmp(L
"ipxe", NameClues
)) {
1057 Entry
->OSType
= 'N';
1058 ShortcutLetter
= 'N';
1059 MergeStrings(&OSIconName
, L
"network", L
',');
1062 if ((ShortcutLetter
>= 'a') && (ShortcutLetter
<= 'z'))
1063 ShortcutLetter
= ShortcutLetter
- 'a' + 'A'; // convert lowercase to uppercase
1064 Entry
->me
.ShortcutLetter
= ShortcutLetter
;
1065 if (Entry
->me
.Image
== NULL
)
1066 Entry
->me
.Image
= LoadOSIcon(OSIconName
, L
"unknown", FALSE
);
1067 MyFreePool(PathOnly
);
1068 } // VOID SetLoaderDefaults()
1070 // Add a specified EFI boot loader to the list, using automatic settings
1071 // for icons, options, etc.
1072 static LOADER_ENTRY
* AddLoaderEntry(IN CHAR16
*LoaderPath
, IN CHAR16
*LoaderTitle
, IN REFIT_VOLUME
*Volume
, IN BOOLEAN SubScreenReturn
) {
1073 LOADER_ENTRY
*Entry
;
1075 CleanUpPathNameSlashes(LoaderPath
);
1076 Entry
= InitializeLoaderEntry(NULL
);
1077 if (Entry
!= NULL
) {
1078 Entry
->Title
= StrDuplicate((LoaderTitle
!= NULL
) ? LoaderTitle
: LoaderPath
);
1079 Entry
->me
.Title
= AllocateZeroPool(sizeof(CHAR16
) * 256);
1080 // Extra space at end of Entry->me.Title enables searching on Volume->VolName even if another volume
1081 // name is identical except for something added to the end (e.g., VolB1 vs. VolB12).
1082 // Note: Volume->VolName will be NULL for network boot programs.
1083 if (Volume
->VolName
)
1084 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s ", (LoaderTitle
!= NULL
) ? LoaderTitle
: LoaderPath
, Volume
->VolName
);
1086 SPrint(Entry
->me
.Title
, 255, L
"Boot %s ", (LoaderTitle
!= NULL
) ? LoaderTitle
: LoaderPath
);
1088 Entry
->me
.BadgeImage
= Volume
->VolBadgeImage
;
1089 if ((LoaderPath
!= NULL
) && (LoaderPath
[0] != L
'\\')) {
1090 Entry
->LoaderPath
= StrDuplicate(L
"\\");
1092 Entry
->LoaderPath
= NULL
;
1094 MergeStrings(&(Entry
->LoaderPath
), LoaderPath
, 0);
1095 Entry
->VolName
= Volume
->VolName
;
1096 Entry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, Entry
->LoaderPath
);
1097 SetLoaderDefaults(Entry
, LoaderPath
, Volume
);
1098 GenerateSubScreen(Entry
, Volume
, SubScreenReturn
);
1099 AddMenuEntry(&MainMenu
, (REFIT_MENU_ENTRY
*)Entry
);
1103 } // LOADER_ENTRY * AddLoaderEntry()
1105 // Add a Linux kernel as a submenu entry for another (pre-existing) Linux kernel entry.
1106 static VOID
AddKernelToSubmenu(LOADER_ENTRY
* TargetLoader
, CHAR16
*FileName
, REFIT_VOLUME
*Volume
) {
1108 CHAR16
**TokenList
= NULL
, *InitrdName
, *SubmenuName
= NULL
, *VolName
= NULL
, *Path
= NULL
, *Title
;
1109 REFIT_MENU_SCREEN
*SubScreen
;
1110 LOADER_ENTRY
*SubEntry
;
1113 File
= ReadLinuxOptionsFile(TargetLoader
->LoaderPath
, Volume
);
1115 SubScreen
= TargetLoader
->me
.SubScreen
;
1116 InitrdName
= FindInitrd(FileName
, Volume
);
1117 while ((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 1) {
1118 SubEntry
= InitializeLoaderEntry(TargetLoader
);
1119 SplitPathName(FileName
, &VolName
, &Path
, &SubmenuName
);
1120 MergeStrings(&SubmenuName
, L
": ", '\0');
1121 MergeStrings(&SubmenuName
, TokenList
[0] ? StrDuplicate(TokenList
[0]) : StrDuplicate(L
"Boot Linux"), '\0');
1122 Title
= StrDuplicate(SubmenuName
);
1123 LimitStringLength(Title
, MAX_LINE_LENGTH
);
1124 SubEntry
->me
.Title
= Title
;
1125 MyFreePool(SubEntry
->LoadOptions
);
1126 SubEntry
->LoadOptions
= AddInitrdToOptions(TokenList
[1], InitrdName
);
1127 MyFreePool(SubEntry
->LoaderPath
);
1128 SubEntry
->LoaderPath
= StrDuplicate(FileName
);
1129 CleanUpPathNameSlashes(SubEntry
->LoaderPath
);
1130 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
1131 FreeTokenLine(&TokenList
, &TokenCount
);
1132 SubEntry
->UseGraphicsMode
= GlobalConfig
.GraphicsFor
& GRAPHICS_FOR_LINUX
;
1133 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
1135 MyFreePool(VolName
);
1137 MyFreePool(SubmenuName
);
1138 MyFreePool(InitrdName
);
1141 } // static VOID AddKernelToSubmenu()
1143 // Returns -1 if (Time1 < Time2), +1 if (Time1 > Time2), or 0 if
1144 // (Time1 == Time2). Precision is only to the nearest second; since
1145 // this is used for sorting boot loader entries, differences smaller
1146 // than this are likely to be meaningless (and unlikely!).
1147 INTN
TimeComp(IN EFI_TIME
*Time1
, IN EFI_TIME
*Time2
) {
1148 INT64 Time1InSeconds
, Time2InSeconds
;
1150 // Following values are overestimates; I'm assuming 31 days in every month.
1151 // This is fine for the purpose of this function, which is limited
1152 Time1InSeconds
= Time1
->Second
+ (Time1
->Minute
* 60) + (Time1
->Hour
* 3600) + (Time1
->Day
* 86400) +
1153 (Time1
->Month
* 2678400) + ((Time1
->Year
- 1998) * 32140800);
1154 Time2InSeconds
= Time2
->Second
+ (Time2
->Minute
* 60) + (Time2
->Hour
* 3600) + (Time2
->Day
* 86400) +
1155 (Time2
->Month
* 2678400) + ((Time2
->Year
- 1998) * 32140800);
1156 if (Time1InSeconds
< Time2InSeconds
)
1158 else if (Time1InSeconds
> Time2InSeconds
)
1162 } // INTN TimeComp()
1164 // Adds a loader list element, keeping it sorted by date. Returns the new
1165 // first element (the one with the most recent date).
1166 static struct LOADER_LIST
* AddLoaderListEntry(struct LOADER_LIST
*LoaderList
, struct LOADER_LIST
*NewEntry
) {
1167 struct LOADER_LIST
*LatestEntry
, *CurrentEntry
, *PrevEntry
= NULL
;
1169 LatestEntry
= CurrentEntry
= LoaderList
;
1170 if (LoaderList
== NULL
) {
1171 LatestEntry
= NewEntry
;
1173 while ((CurrentEntry
!= NULL
) && (TimeComp(&(NewEntry
->TimeStamp
), &(CurrentEntry
->TimeStamp
)) < 0)) {
1174 PrevEntry
= CurrentEntry
;
1175 CurrentEntry
= CurrentEntry
->NextEntry
;
1177 NewEntry
->NextEntry
= CurrentEntry
;
1178 if (PrevEntry
== NULL
) {
1179 LatestEntry
= NewEntry
;
1181 PrevEntry
->NextEntry
= NewEntry
;
1184 return (LatestEntry
);
1185 } // static VOID AddLoaderListEntry()
1187 // Delete the LOADER_LIST linked list
1188 static VOID
CleanUpLoaderList(struct LOADER_LIST
*LoaderList
) {
1189 struct LOADER_LIST
*Temp
;
1191 while (LoaderList
!= NULL
) {
1193 LoaderList
= LoaderList
->NextEntry
;
1194 MyFreePool(Temp
->FileName
);
1197 } // static VOID CleanUpLoaderList()
1199 // Returns FALSE if the specified file/volume matches the GlobalConfig.DontScanDirs
1200 // or GlobalConfig.DontScanVolumes specification, or if Path points to a volume
1201 // other than the one specified by Volume, or if the specified path is SelfDir.
1202 // Returns TRUE if none of these conditions is met -- that is, if the path is
1203 // eligible for scanning.
1204 static BOOLEAN
ShouldScan(REFIT_VOLUME
*Volume
, CHAR16
*Path
) {
1205 CHAR16
*VolName
= NULL
, *DontScanDir
, *PathCopy
= NULL
;
1207 BOOLEAN ScanIt
= TRUE
;
1209 if ((IsIn(Volume
->VolName
, GlobalConfig
.DontScanVolumes
)) || (IsIn(Volume
->PartName
, GlobalConfig
.DontScanVolumes
)))
1212 if (MyStriCmp(Path
, SelfDirPath
) && (Volume
->DeviceHandle
== SelfVolume
->DeviceHandle
))
1215 // See if Path includes an explicit volume declaration that's NOT Volume....
1216 PathCopy
= StrDuplicate(Path
);
1217 if (SplitVolumeAndFilename(&PathCopy
, &VolName
)) {
1218 VolumeNumberToName(Volume
, &VolName
);
1219 if (VolName
&& !MyStriCmp(VolName
, Volume
->VolName
)) {
1222 } // if Path includes volume specification
1223 MyFreePool(PathCopy
);
1224 MyFreePool(VolName
);
1227 // See if Volume is in GlobalConfig.DontScanDirs....
1228 while (ScanIt
&& (DontScanDir
= FindCommaDelimited(GlobalConfig
.DontScanDirs
, i
++))) {
1229 SplitVolumeAndFilename(&DontScanDir
, &VolName
);
1230 CleanUpPathNameSlashes(DontScanDir
);
1231 VolumeNumberToName(Volume
, &VolName
);
1232 if (VolName
!= NULL
) {
1233 if (MyStriCmp(VolName
, Volume
->VolName
) && MyStriCmp(DontScanDir
, Path
))
1236 if (MyStriCmp(DontScanDir
, Path
))
1239 MyFreePool(DontScanDir
);
1240 MyFreePool(VolName
);
1246 } // BOOLEAN ShouldScan()
1248 // Returns TRUE if the file is byte-for-byte identical with the fallback file
1249 // on the volume AND if the file is not itself the fallback file; returns
1250 // FALSE if the file is not identical to the fallback file OR if the file
1251 // IS the fallback file. Intended for use in excluding the fallback boot
1252 // loader when it's a duplicate of another boot loader.
1253 static BOOLEAN
DuplicatesFallback(IN REFIT_VOLUME
*Volume
, IN CHAR16
*FileName
) {
1254 CHAR8
*FileContents
, *FallbackContents
;
1255 EFI_FILE_HANDLE FileHandle
, FallbackHandle
;
1256 EFI_FILE_INFO
*FileInfo
, *FallbackInfo
;
1257 UINTN FileSize
= 0, FallbackSize
= 0;
1259 BOOLEAN AreIdentical
= FALSE
;
1261 if (!FileExists(Volume
->RootDir
, FileName
) || !FileExists(Volume
->RootDir
, FALLBACK_FULLNAME
))
1264 CleanUpPathNameSlashes(FileName
);
1266 if (MyStriCmp(FileName
, FALLBACK_FULLNAME
))
1267 return FALSE
; // identical filenames, so not a duplicate....
1269 Status
= refit_call5_wrapper(Volume
->RootDir
->Open
, Volume
->RootDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
1270 if (Status
== EFI_SUCCESS
) {
1271 FileInfo
= LibFileInfo(FileHandle
);
1272 FileSize
= FileInfo
->FileSize
;
1277 Status
= refit_call5_wrapper(Volume
->RootDir
->Open
, Volume
->RootDir
, &FallbackHandle
, FALLBACK_FULLNAME
, EFI_FILE_MODE_READ
, 0);
1278 if (Status
== EFI_SUCCESS
) {
1279 FallbackInfo
= LibFileInfo(FallbackHandle
);
1280 FallbackSize
= FallbackInfo
->FileSize
;
1282 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
1286 if (FallbackSize
!= FileSize
) { // not same size, so can't be identical
1287 AreIdentical
= FALSE
;
1288 } else { // could be identical; do full check....
1289 FileContents
= AllocatePool(FileSize
);
1290 FallbackContents
= AllocatePool(FallbackSize
);
1291 if (FileContents
&& FallbackContents
) {
1292 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &FileSize
, FileContents
);
1293 if (Status
== EFI_SUCCESS
) {
1294 Status
= refit_call3_wrapper(FallbackHandle
->Read
, FallbackHandle
, &FallbackSize
, FallbackContents
);
1296 if (Status
== EFI_SUCCESS
) {
1297 AreIdentical
= (CompareMem(FileContents
, FallbackContents
, FileSize
) == 0);
1300 MyFreePool(FileContents
);
1301 MyFreePool(FallbackContents
);
1304 // BUG ALERT: Some systems (e.g., DUET, some Macs with large displays) crash if the
1305 // following two calls are reversed. Go figure....
1306 refit_call1_wrapper(FileHandle
->Close
, FallbackHandle
);
1307 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
1308 return AreIdentical
;
1309 } // BOOLEAN DuplicatesFallback()
1311 // Returns FALSE if two measures of file size are identical for a single file,
1312 // TRUE if not or if the file can't be opened and the other measure is non-0.
1313 // Despite the function's name, this isn't really a direct test of symbolic
1314 // link status, since EFI doesn't officially support symlinks. It does seem
1315 // to be a reliable indicator, though. (OTOH, some disk errors might cause a
1316 // file to fail to open, which would return a false positive -- but as I use
1317 // this function to exclude symbolic links from the list of boot loaders,
1318 // that would be fine, since such boot loaders wouldn't work.)
1319 static BOOLEAN
IsSymbolicLink(REFIT_VOLUME
*Volume
, CHAR16
*Path
, EFI_FILE_INFO
*DirEntry
) {
1320 EFI_FILE_HANDLE FileHandle
;
1321 EFI_FILE_INFO
*FileInfo
= NULL
;
1323 UINTN FileSize2
= 0;
1326 FileName
= StrDuplicate(Path
);
1327 MergeStrings(&FileName
, DirEntry
->FileName
, L
'\\');
1328 CleanUpPathNameSlashes(FileName
);
1330 Status
= refit_call5_wrapper(Volume
->RootDir
->Open
, Volume
->RootDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
1331 if (Status
== EFI_SUCCESS
) {
1332 FileInfo
= LibFileInfo(FileHandle
);
1333 if (FileInfo
!= NULL
)
1334 FileSize2
= FileInfo
->FileSize
;
1337 MyFreePool(FileName
);
1338 MyFreePool(FileInfo
);
1340 return (DirEntry
->FileSize
!= FileSize2
);
1341 } // BOOLEAN IsSymbolicLink()
1343 // Returns TRUE if a file with the same name as the original but with
1344 // ".efi.signed" is also present in the same directory. Ubuntu is using
1345 // this filename as a signed version of the original unsigned kernel, and
1346 // there's no point in cluttering the display with two kernels that will
1347 // behave identically on non-SB systems, or when one will fail when SB
1349 static BOOLEAN
HasSignedCounterpart(IN REFIT_VOLUME
*Volume
, IN CHAR16
*Path
, IN CHAR16
*Filename
) {
1350 CHAR16
*NewFile
= NULL
;
1351 BOOLEAN retval
= FALSE
;
1353 MergeStrings(&NewFile
, Path
, 0);
1354 MergeStrings(&NewFile
, Filename
, L
'\\');
1355 MergeStrings(&NewFile
, L
".efi.signed", 0);
1356 if (NewFile
!= NULL
) {
1357 CleanUpPathNameSlashes(NewFile
);
1358 if (FileExists(Volume
->RootDir
, NewFile
))
1360 MyFreePool(NewFile
);
1364 } // BOOLEAN HasSignedCounterpart()
1366 // Scan an individual directory for EFI boot loader files and, if found,
1367 // add them to the list. Exception: Ignores FALLBACK_FULLNAME, which is picked
1368 // up in ScanEfiFiles(). Sorts the entries within the loader directory so that
1369 // the most recent one appears first in the list.
1370 // Returns TRUE if a duplicate for FALLBACK_FILENAME was found, FALSE if not.
1371 static BOOLEAN
ScanLoaderDir(IN REFIT_VOLUME
*Volume
, IN CHAR16
*Path
, IN CHAR16
*Pattern
)
1374 REFIT_DIR_ITER DirIter
;
1375 EFI_FILE_INFO
*DirEntry
;
1376 CHAR16 FileName
[256], *Extension
;
1377 struct LOADER_LIST
*LoaderList
= NULL
, *NewLoader
;
1378 LOADER_ENTRY
*FirstKernel
= NULL
, *LatestEntry
= NULL
;
1379 BOOLEAN FoundFallbackDuplicate
= FALSE
, IsLinux
= FALSE
, InSelfPath
;
1381 InSelfPath
= MyStriCmp(Path
, SelfDirPath
);
1382 if ((!SelfDirPath
|| !Path
|| (InSelfPath
&& (Volume
->DeviceHandle
!= SelfVolume
->DeviceHandle
)) ||
1383 (!InSelfPath
)) && (ShouldScan(Volume
, Path
))) {
1384 // look through contents of the directory
1385 DirIterOpen(Volume
->RootDir
, Path
, &DirIter
);
1386 while (DirIterNext(&DirIter
, 2, Pattern
, &DirEntry
)) {
1387 Extension
= FindExtension(DirEntry
->FileName
);
1388 if (DirEntry
->FileName
[0] == '.' ||
1389 MyStriCmp(Extension
, L
".icns") ||
1390 MyStriCmp(Extension
, L
".png") ||
1391 (MyStriCmp(DirEntry
->FileName
, FALLBACK_BASENAME
) && (MyStriCmp(Path
, L
"EFI\\BOOT"))) ||
1392 StriSubCmp(L
"shell", DirEntry
->FileName
) ||
1393 IsSymbolicLink(Volume
, Path
, DirEntry
) || /* is symbolic link */
1394 HasSignedCounterpart(Volume
, Path
, DirEntry
->FileName
) || /* a file with same name plus ".efi.signed" is present */
1395 FilenameIn(Volume
, Path
, DirEntry
->FileName
, GlobalConfig
.DontScanFiles
))
1396 continue; // skip this
1399 SPrint(FileName
, 255, L
"\\%s\\%s", Path
, DirEntry
->FileName
);
1401 SPrint(FileName
, 255, L
"\\%s", DirEntry
->FileName
);
1402 CleanUpPathNameSlashes(FileName
);
1404 if(!IsValidLoader(Volume
->RootDir
, FileName
))
1407 NewLoader
= AllocateZeroPool(sizeof(struct LOADER_LIST
));
1408 if (NewLoader
!= NULL
) {
1409 NewLoader
->FileName
= StrDuplicate(FileName
);
1410 NewLoader
->TimeStamp
= DirEntry
->ModificationTime
;
1411 LoaderList
= AddLoaderListEntry(LoaderList
, NewLoader
);
1412 if (DuplicatesFallback(Volume
, FileName
))
1413 FoundFallbackDuplicate
= TRUE
;
1415 MyFreePool(Extension
);
1418 NewLoader
= LoaderList
;
1419 while (NewLoader
!= NULL
) {
1420 IsLinux
= (StriSubCmp(L
"bzImage", NewLoader
->FileName
) || StriSubCmp(L
"vmlinuz", NewLoader
->FileName
));
1421 if ((FirstKernel
!= NULL
) && IsLinux
&& GlobalConfig
.FoldLinuxKernels
) {
1422 AddKernelToSubmenu(FirstKernel
, NewLoader
->FileName
, Volume
);
1424 LatestEntry
= AddLoaderEntry(NewLoader
->FileName
, NULL
, Volume
, !(IsLinux
&& GlobalConfig
.FoldLinuxKernels
));
1425 if (IsLinux
&& (FirstKernel
== NULL
))
1426 FirstKernel
= LatestEntry
;
1428 NewLoader
= NewLoader
->NextEntry
;
1430 if ((NewLoader
!= NULL
) && (FirstKernel
!= NULL
) && IsLinux
&& GlobalConfig
.FoldLinuxKernels
)
1431 AddMenuEntry(FirstKernel
->me
.SubScreen
, &MenuEntryReturn
);
1433 CleanUpLoaderList(LoaderList
);
1434 Status
= DirIterClose(&DirIter
);
1435 // NOTE: EFI_INVALID_PARAMETER really is an error that should be reported;
1436 // but I've gotten reports from users who are getting this error occasionally
1437 // and I can't find anything wrong or reproduce the problem, so I'm putting
1438 // it down to buggy EFI implementations and ignoring that particular error....
1439 if ((Status
!= EFI_NOT_FOUND
) && (Status
!= EFI_INVALID_PARAMETER
)) {
1441 SPrint(FileName
, 255, L
"while scanning the %s directory", Path
);
1443 StrCpy(FileName
, L
"while scanning the root directory");
1444 CheckError(Status
, FileName
);
1445 } // if (Status != EFI_NOT_FOUND)
1446 } // if not scanning a blacklisted directory
1448 return FoundFallbackDuplicate
;
1449 } /* static VOID ScanLoaderDir() */
1451 // Run the IPXE_DISCOVER_NAME program, which obtains the IP address of the boot
1452 // server and the name of the boot file it delivers.
1453 CHAR16
* RuniPXEDiscover(EFI_HANDLE Volume
)
1456 EFI_DEVICE_PATH
*FilePath
;
1457 EFI_HANDLE iPXEHandle
;
1458 CHAR16
*boot_info
= NULL
;
1459 UINTN boot_info_size
= 0;
1461 FilePath
= FileDevicePath (Volume
, IPXE_DISCOVER_NAME
);
1462 Status
= refit_call6_wrapper(BS
->LoadImage
, FALSE
, SelfImageHandle
, FilePath
, NULL
, 0, &iPXEHandle
);
1466 Status
= refit_call3_wrapper(BS
->StartImage
, iPXEHandle
, &boot_info_size
, &boot_info
);
1469 } // RuniPXEDiscover()
1471 // Scan for network (PXE) boot servers. This function relies on the presence
1472 // of the IPXE_DISCOVER_NAME and IPXE_NAME program files on the volume from
1473 // which rEFInd launched. As of December 6, 2014, these tools aren't entirely
1474 // reliable. See BUILDING.txt for information on building them.
1475 static VOID
ScanNetboot() {
1476 CHAR16
*iPXEFileName
= IPXE_NAME
;
1478 REFIT_VOLUME
*NetVolume
;
1480 if (FileExists(SelfVolume
->RootDir
, IPXE_DISCOVER_NAME
) &&
1481 FileExists(SelfVolume
->RootDir
, IPXE_NAME
) &&
1482 IsValidLoader(SelfVolume
->RootDir
, IPXE_DISCOVER_NAME
) &&
1483 IsValidLoader(SelfVolume
->RootDir
, IPXE_NAME
)) {
1484 Location
= RuniPXEDiscover(SelfVolume
->DeviceHandle
);
1485 if (Location
!= NULL
&& FileExists(SelfVolume
->RootDir
, iPXEFileName
)) {
1486 NetVolume
= AllocatePool(sizeof(REFIT_VOLUME
));
1487 CopyMem(NetVolume
, SelfVolume
, sizeof(REFIT_VOLUME
));
1488 NetVolume
->DiskKind
= DISK_KIND_NET
;
1489 NetVolume
->VolBadgeImage
= BuiltinIcon(BUILTIN_ICON_VOL_NET
);
1490 NetVolume
->PartName
= NetVolume
->VolName
= NULL
;
1491 AddLoaderEntry(iPXEFileName
, Location
, NetVolume
, TRUE
);
1492 MyFreePool(NetVolume
);
1493 } // if support files exist and are valid
1495 } // VOID ScanNetBoot()
1497 static VOID
ScanEfiFiles(REFIT_VOLUME
*Volume
) {
1499 REFIT_DIR_ITER EfiDirIter
;
1500 EFI_FILE_INFO
*EfiDirEntry
;
1501 CHAR16 FileName
[256], *Directory
= NULL
, *MatchPatterns
, *VolName
= NULL
, *SelfPath
;
1503 BOOLEAN ScanFallbackLoader
= TRUE
;
1504 BOOLEAN FoundBRBackup
= FALSE
;
1506 if (Volume
&& (Volume
->RootDir
!= NULL
) && (Volume
->VolName
!= NULL
) && (Volume
->IsReadable
)) {
1507 MatchPatterns
= StrDuplicate(LOADER_MATCH_PATTERNS
);
1508 if (GlobalConfig
.ScanAllLinux
)
1509 MergeStrings(&MatchPatterns
, LINUX_MATCH_PATTERNS
, L
',');
1511 // check for Mac OS X boot loader
1512 if (ShouldScan(Volume
, MACOSX_LOADER_DIR
)) {
1513 StrCpy(FileName
, MACOSX_LOADER_PATH
);
1514 if (FileExists(Volume
->RootDir
, FileName
) && !FilenameIn(Volume
, MACOSX_LOADER_DIR
, L
"boot.efi", GlobalConfig
.DontScanFiles
)) {
1515 AddLoaderEntry(FileName
, L
"Mac OS X", Volume
, TRUE
);
1516 if (DuplicatesFallback(Volume
, FileName
))
1517 ScanFallbackLoader
= FALSE
;
1521 StrCpy(FileName
, L
"System\\Library\\CoreServices\\xom.efi");
1522 if (FileExists(Volume
->RootDir
, FileName
) && !FilenameIn(Volume
, MACOSX_LOADER_DIR
, L
"xom.efi", GlobalConfig
.DontScanFiles
)) {
1523 AddLoaderEntry(FileName
, L
"Windows XP (XoM)", Volume
, TRUE
);
1524 if (DuplicatesFallback(Volume
, FileName
))
1525 ScanFallbackLoader
= FALSE
;
1527 } // if should scan Mac directory
1529 // check for Microsoft boot loader/menu
1530 if (ShouldScan(Volume
, L
"EFI\\Microsoft\\Boot")) {
1531 StrCpy(FileName
, L
"EFI\\Microsoft\\Boot\\bkpbootmgfw.efi");
1532 if (FileExists(Volume
->RootDir
, FileName
) && !FilenameIn(Volume
, L
"EFI\\Microsoft\\Boot", L
"bkpbootmgfw.efi",
1533 GlobalConfig
.DontScanFiles
)) {
1534 AddLoaderEntry(FileName
, L
"Microsoft EFI boot (Boot Repair backup)", Volume
, TRUE
);
1535 FoundBRBackup
= TRUE
;
1536 if (DuplicatesFallback(Volume
, FileName
))
1537 ScanFallbackLoader
= FALSE
;
1539 StrCpy(FileName
, L
"EFI\\Microsoft\\Boot\\bootmgfw.efi");
1540 if (FileExists(Volume
->RootDir
, FileName
) &&
1541 !FilenameIn(Volume
, L
"EFI\\Microsoft\\Boot", L
"bootmgfw.efi", GlobalConfig
.DontScanFiles
)) {
1543 AddLoaderEntry(FileName
, L
"Supposed Microsoft EFI boot (probably GRUB)", Volume
, TRUE
);
1545 AddLoaderEntry(FileName
, L
"Microsoft EFI boot", Volume
, TRUE
);
1546 if (DuplicatesFallback(Volume
, FileName
))
1547 ScanFallbackLoader
= FALSE
;
1551 // scan the root directory for EFI executables
1552 if (ScanLoaderDir(Volume
, L
"\\", MatchPatterns
))
1553 ScanFallbackLoader
= FALSE
;
1555 // scan subdirectories of the EFI directory (as per the standard)
1556 DirIterOpen(Volume
->RootDir
, L
"EFI", &EfiDirIter
);
1557 while (DirIterNext(&EfiDirIter
, 1, NULL
, &EfiDirEntry
)) {
1558 if (MyStriCmp(EfiDirEntry
->FileName
, L
"tools") || EfiDirEntry
->FileName
[0] == '.')
1559 continue; // skip this, doesn't contain boot loaders or is scanned later
1560 SPrint(FileName
, 255, L
"EFI\\%s", EfiDirEntry
->FileName
);
1561 if (ScanLoaderDir(Volume
, FileName
, MatchPatterns
))
1562 ScanFallbackLoader
= FALSE
;
1564 Status
= DirIterClose(&EfiDirIter
);
1565 if ((Status
!= EFI_NOT_FOUND
) && (Status
!= EFI_INVALID_PARAMETER
))
1566 CheckError(Status
, L
"while scanning the EFI directory");
1568 // Scan user-specified (or additional default) directories....
1570 while ((Directory
= FindCommaDelimited(GlobalConfig
.AlsoScan
, i
++)) != NULL
) {
1571 if (ShouldScan(Volume
, Directory
)) {
1572 SplitVolumeAndFilename(&Directory
, &VolName
);
1573 CleanUpPathNameSlashes(Directory
);
1574 Length
= StrLen(Directory
);
1575 if ((Length
> 0) && ScanLoaderDir(Volume
, Directory
, MatchPatterns
))
1576 ScanFallbackLoader
= FALSE
;
1577 MyFreePool(VolName
);
1578 } // if should scan dir
1579 MyFreePool(Directory
);
1582 // Don't scan the fallback loader if it's on the same volume and a duplicate of rEFInd itself....
1583 SelfPath
= DevicePathToStr(SelfLoadedImage
->FilePath
);
1584 CleanUpPathNameSlashes(SelfPath
);
1585 if ((Volume
->DeviceHandle
== SelfLoadedImage
->DeviceHandle
) && DuplicatesFallback(Volume
, SelfPath
))
1586 ScanFallbackLoader
= FALSE
;
1588 // If not a duplicate & if it exists & if it's not us, create an entry
1589 // for the fallback boot loader
1590 if (ScanFallbackLoader
&& FileExists(Volume
->RootDir
, FALLBACK_FULLNAME
) && ShouldScan(Volume
, L
"EFI\\BOOT")) {
1591 AddLoaderEntry(FALLBACK_FULLNAME
, L
"Fallback boot loader", Volume
, TRUE
);
1594 } // static VOID ScanEfiFiles()
1596 // Scan internal disks for valid EFI boot loaders....
1597 static VOID
ScanInternal(VOID
) {
1600 for (VolumeIndex
= 0; VolumeIndex
< VolumesCount
; VolumeIndex
++) {
1601 if (Volumes
[VolumeIndex
]->DiskKind
== DISK_KIND_INTERNAL
) {
1602 ScanEfiFiles(Volumes
[VolumeIndex
]);
1605 } // static VOID ScanInternal()
1607 // Scan external disks for valid EFI boot loaders....
1608 static VOID
ScanExternal(VOID
) {
1611 for (VolumeIndex
= 0; VolumeIndex
< VolumesCount
; VolumeIndex
++) {
1612 if (Volumes
[VolumeIndex
]->DiskKind
== DISK_KIND_EXTERNAL
) {
1613 ScanEfiFiles(Volumes
[VolumeIndex
]);
1616 } // static VOID ScanExternal()
1618 // Scan internal disks for valid EFI boot loaders....
1619 static VOID
ScanOptical(VOID
) {
1622 for (VolumeIndex
= 0; VolumeIndex
< VolumesCount
; VolumeIndex
++) {
1623 if (Volumes
[VolumeIndex
]->DiskKind
== DISK_KIND_OPTICAL
) {
1624 ScanEfiFiles(Volumes
[VolumeIndex
]);
1627 } // static VOID ScanOptical()
1629 // default volume badge icon based on disk kind
1630 EG_IMAGE
* GetDiskBadge(IN UINTN DiskType
) {
1631 EG_IMAGE
* Badge
= NULL
;
1635 Badge
= BuiltinIcon(BUILTIN_ICON_VOL_INTERNAL
);
1638 Badge
= BuiltinIcon(BUILTIN_ICON_VOL_EXTERNAL
);
1641 Badge
= BuiltinIcon(BUILTIN_ICON_VOL_OPTICAL
);
1645 } // EG_IMAGE * GetDiskBadge()
1648 // pre-boot tool functions
1651 static VOID
StartTool(IN LOADER_ENTRY
*Entry
)
1653 BeginExternalScreen(Entry
->UseGraphicsMode
, Entry
->me
.Title
+ 6); // assumes "Start <title>" as assigned below
1654 StoreLoaderName(Entry
->me
.Title
);
1655 StartEFIImage(Entry
->DevicePath
, Entry
->LoadOptions
, TYPE_EFI
,
1656 Basename(Entry
->LoaderPath
), Entry
->OSType
, NULL
, TRUE
, FALSE
);
1657 FinishExternalScreen();
1658 } /* static VOID StartTool() */
1660 static LOADER_ENTRY
* AddToolEntry(EFI_HANDLE DeviceHandle
, IN CHAR16
*LoaderPath
, IN CHAR16
*LoaderTitle
, IN EG_IMAGE
*Image
,
1661 IN CHAR16 ShortcutLetter
, IN BOOLEAN UseGraphicsMode
)
1663 LOADER_ENTRY
*Entry
;
1664 CHAR16
*TitleStr
= NULL
;
1666 Entry
= AllocateZeroPool(sizeof(LOADER_ENTRY
));
1668 TitleStr
= PoolPrint(L
"Start %s", LoaderTitle
);
1669 Entry
->me
.Title
= TitleStr
;
1670 Entry
->me
.Tag
= TAG_TOOL
;
1672 Entry
->me
.ShortcutLetter
= ShortcutLetter
;
1673 Entry
->me
.Image
= Image
;
1674 Entry
->LoaderPath
= (LoaderPath
) ? StrDuplicate(LoaderPath
) : NULL
;
1675 Entry
->DevicePath
= FileDevicePath(DeviceHandle
, Entry
->LoaderPath
);
1676 Entry
->UseGraphicsMode
= UseGraphicsMode
;
1678 AddMenuEntry(&MainMenu
, (REFIT_MENU_ENTRY
*)Entry
);
1680 } /* static LOADER_ENTRY * AddToolEntry() */
1683 // pre-boot driver functions
1686 static UINTN
ScanDriverDir(IN CHAR16
*Path
)
1689 REFIT_DIR_ITER DirIter
;
1691 EFI_FILE_INFO
*DirEntry
;
1692 CHAR16 FileName
[256];
1694 CleanUpPathNameSlashes(Path
);
1695 // look through contents of the directory
1696 DirIterOpen(SelfRootDir
, Path
, &DirIter
);
1697 while (DirIterNext(&DirIter
, 2, LOADER_MATCH_PATTERNS
, &DirEntry
)) {
1698 if (DirEntry
->FileName
[0] == '.')
1699 continue; // skip this
1701 SPrint(FileName
, 255, L
"%s\\%s", Path
, DirEntry
->FileName
);
1703 Status
= StartEFIImage(FileDevicePath(SelfLoadedImage
->DeviceHandle
, FileName
),
1704 L
"", TYPE_EFI
, DirEntry
->FileName
, 0, NULL
, FALSE
, TRUE
);
1706 Status
= DirIterClose(&DirIter
);
1707 if ((Status
!= EFI_NOT_FOUND
) && (Status
!= EFI_INVALID_PARAMETER
)) {
1708 SPrint(FileName
, 255, L
"while scanning the %s directory", Path
);
1709 CheckError(Status
, FileName
);
1714 #ifdef __MAKEWITH_GNUEFI
1715 static EFI_STATUS
ConnectAllDriversToAllControllers(VOID
)
1718 UINTN AllHandleCount
;
1719 EFI_HANDLE
*AllHandleBuffer
;
1722 EFI_HANDLE
*HandleBuffer
;
1728 Status
= LibLocateHandle(AllHandles
,
1733 if (EFI_ERROR(Status
))
1736 for (Index
= 0; Index
< AllHandleCount
; Index
++) {
1738 // Scan the handle database
1740 Status
= LibScanHandleDatabase(NULL
,
1742 AllHandleBuffer
[Index
],
1747 if (EFI_ERROR (Status
))
1751 if (HandleType
[Index
] & EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE
)
1753 if (HandleType
[Index
] & EFI_HANDLE_TYPE_IMAGE_HANDLE
)
1758 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
1759 if (HandleType
[HandleIndex
] & EFI_HANDLE_TYPE_PARENT_HANDLE
)
1764 if (HandleType
[Index
] & EFI_HANDLE_TYPE_DEVICE_HANDLE
) {
1765 Status
= refit_call4_wrapper(BS
->ConnectController
,
1766 AllHandleBuffer
[Index
],
1774 MyFreePool (HandleBuffer
);
1775 MyFreePool (HandleType
);
1779 MyFreePool (AllHandleBuffer
);
1781 } /* EFI_STATUS ConnectAllDriversToAllControllers() */
1783 static EFI_STATUS
ConnectAllDriversToAllControllers(VOID
) {
1784 BdsLibConnectAllDriversToAllControllers();
1789 // Load all EFI drivers from rEFInd's "drivers" subdirectory and from the
1790 // directories specified by the user in the "scan_driver_dirs" configuration
1792 static VOID
LoadDrivers(VOID
)
1794 CHAR16
*Directory
, *SelfDirectory
;
1795 UINTN i
= 0, Length
, NumFound
= 0;
1797 // load drivers from the subdirectories of rEFInd's home directory specified
1798 // in the DRIVER_DIRS constant.
1799 while ((Directory
= FindCommaDelimited(DRIVER_DIRS
, i
++)) != NULL
) {
1800 SelfDirectory
= SelfDirPath
? StrDuplicate(SelfDirPath
) : NULL
;
1801 CleanUpPathNameSlashes(SelfDirectory
);
1802 MergeStrings(&SelfDirectory
, Directory
, L
'\\');
1803 NumFound
+= ScanDriverDir(SelfDirectory
);
1804 MyFreePool(Directory
);
1805 MyFreePool(SelfDirectory
);
1808 // Scan additional user-specified driver directories....
1810 while ((Directory
= FindCommaDelimited(GlobalConfig
.DriverDirs
, i
++)) != NULL
) {
1811 CleanUpPathNameSlashes(Directory
);
1812 Length
= StrLen(Directory
);
1814 NumFound
+= ScanDriverDir(Directory
);
1816 MyFreePool(Directory
);
1819 // connect all devices
1821 ConnectAllDriversToAllControllers();
1823 } /* static VOID LoadDrivers() */
1825 // Locates boot loaders. NOTE: This assumes that GlobalConfig.LegacyType is set correctly.
1826 static VOID
ScanForBootloaders(VOID
) {
1829 BOOLEAN ScanForLegacy
= FALSE
;
1831 // Determine up-front if we'll be scanning for legacy loaders....
1832 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
1833 s
= GlobalConfig
.ScanFor
[i
];
1834 if ((s
== 'c') || (s
== 'C') || (s
== 'h') || (s
== 'H') || (s
== 'b') || (s
== 'B'))
1835 ScanForLegacy
= TRUE
;
1838 // If UEFI & scanning for legacy loaders & deep legacy scan, update NVRAM boot manager list
1839 if ((GlobalConfig
.LegacyType
== LEGACY_TYPE_UEFI
) && ScanForLegacy
&& GlobalConfig
.DeepLegacyScan
) {
1840 BdsDeleteAllInvalidLegacyBootOptions();
1841 BdsAddNonExistingLegacyBootOptions();
1844 // scan for loaders and tools, add them to the menu
1845 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
1846 switch(GlobalConfig
.ScanFor
[i
]) {
1851 ScanLegacyInternal();
1854 ScanLegacyExternal();
1857 ScanUserConfigured(GlobalConfig
.ConfigFilename
);
1874 // assign shortcut keys
1875 for (i
= 0; i
< MainMenu
.EntryCount
&& MainMenu
.Entries
[i
]->Row
== 0 && i
< 9; i
++)
1876 MainMenu
.Entries
[i
]->ShortcutDigit
= (CHAR16
)('1' + i
);
1878 // wait for user ACK when there were errors
1879 FinishTextScreen(FALSE
);
1880 } // static VOID ScanForBootloaders()
1882 // Locate a single tool from the specified Locations using one of the
1883 // specified Names and add it to the menu.
1884 static VOID
FindTool(CHAR16
*Locations
, CHAR16
*Names
, CHAR16
*Description
, UINTN Icon
) {
1885 UINTN j
= 0, k
, VolumeIndex
;
1886 CHAR16
*DirName
, *FileName
, *PathName
, FullDescription
[256];
1888 while ((DirName
= FindCommaDelimited(Locations
, j
++)) != NULL
) {
1890 while ((FileName
= FindCommaDelimited(Names
, k
++)) != NULL
) {
1891 PathName
= StrDuplicate(DirName
);
1892 MergeStrings(&PathName
, FileName
, MyStriCmp(PathName
, L
"\\") ? 0 : L
'\\');
1893 for (VolumeIndex
= 0; VolumeIndex
< VolumesCount
; VolumeIndex
++) {
1894 if ((Volumes
[VolumeIndex
]->RootDir
!= NULL
) &&
1895 (FileExists(Volumes
[VolumeIndex
]->RootDir
, PathName
)) &&
1896 IsValidLoader(Volumes
[VolumeIndex
]->RootDir
, PathName
)) {
1897 SPrint(FullDescription
, 255, L
"%s at %s on %s", Description
, PathName
, Volumes
[VolumeIndex
]->VolName
);
1898 AddToolEntry(Volumes
[VolumeIndex
]->DeviceHandle
, PathName
, FullDescription
, BuiltinIcon(Icon
), 'S', FALSE
);
1901 MyFreePool(PathName
);
1902 MyFreePool(FileName
);
1904 MyFreePool(DirName
);
1905 } // while Locations
1906 } // VOID FindTool()
1908 // Add the second-row tags containing built-in and external tools (EFI shell,
1910 static VOID
ScanForTools(VOID
) {
1911 CHAR16
*FileName
= NULL
, *VolName
= NULL
, *MokLocations
, Description
[256];
1912 REFIT_MENU_ENTRY
*TempMenuEntry
;
1913 UINTN i
, j
, VolumeIndex
;
1918 MokLocations
= StrDuplicate(MOK_LOCATIONS
);
1919 if (MokLocations
!= NULL
)
1920 MergeStrings(&MokLocations
, SelfDirPath
, L
',');
1922 for (i
= 0; i
< NUM_TOOLS
; i
++) {
1923 switch(GlobalConfig
.ShowTools
[i
]) {
1924 // NOTE: Be sure that FileName is NULL at the end of each case.
1926 TempMenuEntry
= CopyMenuEntry(&MenuEntryShutdown
);
1927 TempMenuEntry
->Image
= BuiltinIcon(BUILTIN_ICON_FUNC_SHUTDOWN
);
1928 AddMenuEntry(&MainMenu
, TempMenuEntry
);
1932 TempMenuEntry
= CopyMenuEntry(&MenuEntryReset
);
1933 TempMenuEntry
->Image
= BuiltinIcon(BUILTIN_ICON_FUNC_RESET
);
1934 AddMenuEntry(&MainMenu
, TempMenuEntry
);
1938 TempMenuEntry
= CopyMenuEntry(&MenuEntryAbout
);
1939 TempMenuEntry
->Image
= BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT
);
1940 AddMenuEntry(&MainMenu
, TempMenuEntry
);
1944 TempMenuEntry
= CopyMenuEntry(&MenuEntryExit
);
1945 TempMenuEntry
->Image
= BuiltinIcon(BUILTIN_ICON_FUNC_EXIT
);
1946 AddMenuEntry(&MainMenu
, TempMenuEntry
);
1950 if (EfivarGetRaw(&GlobalGuid
, L
"OsIndicationsSupported", &b
, &j
) == EFI_SUCCESS
) {
1952 if (osind
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
) {
1953 TempMenuEntry
= CopyMenuEntry(&MenuEntryFirmware
);
1954 TempMenuEntry
->Image
= BuiltinIcon(BUILTIN_ICON_FUNC_FIRMWARE
);
1955 AddMenuEntry(&MainMenu
, TempMenuEntry
);
1962 while ((FileName
= FindCommaDelimited(SHELL_NAMES
, j
++)) != NULL
) {
1963 if (FileExists(SelfRootDir
, FileName
) && IsValidLoader(SelfRootDir
, FileName
)) {
1964 AddToolEntry(SelfLoadedImage
->DeviceHandle
, FileName
, L
"EFI Shell", BuiltinIcon(BUILTIN_ICON_TOOL_SHELL
),
1967 MyFreePool(FileName
);
1973 while ((FileName
= FindCommaDelimited(GPTSYNC_NAMES
, j
++)) != NULL
) {
1974 if (FileExists(SelfRootDir
, FileName
) && IsValidLoader(SelfRootDir
, FileName
)) {
1975 AddToolEntry(SelfLoadedImage
->DeviceHandle
, FileName
, L
"Hybrid MBR tool", BuiltinIcon(BUILTIN_ICON_TOOL_PART
),
1978 MyFreePool(FileName
);
1985 while ((FileName
= FindCommaDelimited(GDISK_NAMES
, j
++)) != NULL
) {
1986 if (FileExists(SelfRootDir
, FileName
) && IsValidLoader(SelfRootDir
, FileName
)) {
1987 AddToolEntry(SelfLoadedImage
->DeviceHandle
, FileName
, L
"disk partitioning tool",
1988 BuiltinIcon(BUILTIN_ICON_TOOL_PART
), 'G', FALSE
);
1990 MyFreePool(FileName
);
1997 while ((FileName
= FindCommaDelimited(NETBOOT_NAMES
, j
++)) != NULL
) {
1998 if (FileExists(SelfRootDir
, FileName
) && IsValidLoader(SelfRootDir
, FileName
)) {
1999 AddToolEntry(SelfLoadedImage
->DeviceHandle
, FileName
, L
"Netboot",
2000 BuiltinIcon(BUILTIN_ICON_TOOL_NETBOOT
), 'N', FALSE
);
2002 MyFreePool(FileName
);
2007 case TAG_APPLE_RECOVERY
:
2008 FileName
= StrDuplicate(L
"\\com.apple.recovery.boot\\boot.efi");
2009 for (VolumeIndex
= 0; VolumeIndex
< VolumesCount
; VolumeIndex
++) {
2010 if ((Volumes
[VolumeIndex
]->RootDir
!= NULL
) &&
2011 (FileExists(Volumes
[VolumeIndex
]->RootDir
, FileName
)) &&
2012 IsValidLoader(Volumes
[VolumeIndex
]->RootDir
, FileName
)) {
2013 SPrint(Description
, 255, L
"Apple Recovery on %s", Volumes
[VolumeIndex
]->VolName
);
2014 AddToolEntry(Volumes
[VolumeIndex
]->DeviceHandle
, FileName
, Description
,
2015 BuiltinIcon(BUILTIN_ICON_TOOL_APPLE_RESCUE
), 'R', TRUE
);
2018 MyFreePool(FileName
);
2022 case TAG_WINDOWS_RECOVERY
:
2024 while ((FileName
= FindCommaDelimited(GlobalConfig
.WindowsRecoveryFiles
, j
++)) != NULL
) {
2025 SplitVolumeAndFilename(&FileName
, &VolName
);
2026 for (VolumeIndex
= 0; VolumeIndex
< VolumesCount
; VolumeIndex
++) {
2027 if ((Volumes
[VolumeIndex
]->RootDir
!= NULL
) &&
2028 (FileExists(Volumes
[VolumeIndex
]->RootDir
, FileName
)) &&
2029 IsValidLoader(Volumes
[VolumeIndex
]->RootDir
, FileName
) &&
2030 ((VolName
== NULL
) || MyStriCmp(VolName
, Volumes
[VolumeIndex
]->VolName
))) {
2031 SPrint(Description
, 255, L
"Microsoft Recovery on %s", Volumes
[VolumeIndex
]->VolName
);
2032 AddToolEntry(Volumes
[VolumeIndex
]->DeviceHandle
, FileName
, Description
,
2033 BuiltinIcon(BUILTIN_ICON_TOOL_WINDOWS_RESCUE
), 'R', TRUE
);
2037 MyFreePool(FileName
);
2039 MyFreePool(VolName
);
2044 FindTool(MokLocations
, MOK_NAMES
, L
"MOK utility", BUILTIN_ICON_TOOL_MOK_TOOL
);
2047 case TAG_CSR_ROTATE
:
2048 if ((GetCsrStatus(&CsrValue
) == EFI_SUCCESS
) && (GlobalConfig
.CsrValues
)) {
2049 TempMenuEntry
= CopyMenuEntry(&MenuEntryRotateCsr
);
2050 TempMenuEntry
->Image
= BuiltinIcon(BUILTIN_ICON_FUNC_CSR_ROTATE
);
2051 AddMenuEntry(&MainMenu
, TempMenuEntry
);
2056 FindTool(MEMTEST_LOCATIONS
, MEMTEST_NAMES
, L
"Memory test utility", BUILTIN_ICON_TOOL_MEMTEST
);
2061 } // static VOID ScanForTools
2063 // Rescan for boot loaders
2064 static VOID
RescanAll(BOOLEAN DisplayMessage
) {
2072 egDisplayMessage(L
"Scanning for new boot loaders; please wait....", &BGColor
);
2073 FreeList((VOID
***) &(MainMenu
.Entries
), &MainMenu
.EntryCount
);
2074 MainMenu
.Entries
= NULL
;
2075 MainMenu
.EntryCount
= 0;
2076 ReadConfig(GlobalConfig
.ConfigFilename
);
2077 ConnectAllDriversToAllControllers();
2079 ScanForBootloaders();
2082 } // VOID RescanAll()
2084 #ifdef __MAKEWITH_TIANO
2086 // Minimal initialization function
2087 static VOID
InitializeLib(IN EFI_HANDLE ImageHandle
, IN EFI_SYSTEM_TABLE
*SystemTable
) {
2089 // gImageHandle = ImageHandle;
2090 gBS
= SystemTable
->BootServices
;
2091 // gRS = SystemTable->RuntimeServices;
2092 gRT
= SystemTable
->RuntimeServices
; // Some BDS functions need gRT to be set
2093 EfiGetSystemConfigurationTable (&gEfiDxeServicesTableGuid
, (VOID
**) &gDS
);
2098 // Set up our own Secure Boot extensions....
2099 // Returns TRUE on success, FALSE otherwise
2100 static BOOLEAN
SecureBootSetup(VOID
) {
2102 BOOLEAN Success
= FALSE
;
2104 if (secure_mode() && ShimLoaded()) {
2105 Status
= security_policy_install();
2106 if (Status
== EFI_SUCCESS
) {
2109 Print(L
"Failed to install MOK Secure Boot extensions");
2114 } // VOID SecureBootSetup()
2116 // Remove our own Secure Boot extensions....
2117 // Returns TRUE on success, FALSE otherwise
2118 static BOOLEAN
SecureBootUninstall(VOID
) {
2120 BOOLEAN Success
= TRUE
;
2122 if (secure_mode()) {
2123 Status
= security_policy_uninstall();
2124 if (Status
!= EFI_SUCCESS
) {
2126 BeginTextScreen(L
"Secure Boot Policy Failure");
2127 Print(L
"Failed to uninstall MOK Secure Boot extensions; forcing a reboot.");
2129 refit_call4_wrapper(RT
->ResetSystem
, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
2133 } // VOID SecureBootUninstall
2135 // Sets the global configuration filename; will be CONFIG_FILE_NAME unless the
2136 // "-c" command-line option is set, in which case that takes precedence.
2137 // If an error is encountered, leaves the value alone (it should be set to
2138 // CONFIG_FILE_NAME when GlobalConfig is initialized).
2139 static VOID
SetConfigFilename(EFI_HANDLE ImageHandle
) {
2140 EFI_LOADED_IMAGE
*Info
;
2141 CHAR16
*Options
, *FileName
, *SubString
;
2144 Status
= refit_call3_wrapper(BS
->HandleProtocol
, ImageHandle
, &LoadedImageProtocol
, (VOID
**) &Info
);
2145 if ((Status
== EFI_SUCCESS
) && (Info
->LoadOptionsSize
> 0)) {
2146 Options
= (CHAR16
*) Info
->LoadOptions
;
2147 SubString
= MyStrStr(Options
, L
" -c ");
2149 FileName
= StrDuplicate(&SubString
[4]);
2151 LimitStringLength(FileName
, 256);
2154 if (FileExists(SelfDir
, FileName
)) {
2155 GlobalConfig
.ConfigFilename
= FileName
;
2157 Print(L
"Specified configuration file (%s) doesn't exist; using\n'refind.conf' default\n", FileName
);
2158 MyFreePool(FileName
);
2162 } // VOID SetConfigFilename()
2169 efi_main (EFI_HANDLE ImageHandle
, EFI_SYSTEM_TABLE
*SystemTable
)
2172 BOOLEAN MainLoopRunning
= TRUE
;
2173 BOOLEAN MokProtocol
;
2174 REFIT_MENU_ENTRY
*ChosenEntry
;
2176 CHAR16
*SelectionName
= NULL
;
2180 InitializeLib(ImageHandle
, SystemTable
);
2181 Status
= InitRefitLib(ImageHandle
);
2182 if (EFI_ERROR(Status
))
2185 // read configuration
2186 CopyMem(GlobalConfig
.ScanFor
, "ieom ", NUM_SCAN_OPTIONS
);
2187 FindLegacyBootType();
2188 if (GlobalConfig
.LegacyType
== LEGACY_TYPE_MAC
)
2189 CopyMem(GlobalConfig
.ScanFor
, "ihebocm ", NUM_SCAN_OPTIONS
);
2190 SetConfigFilename(ImageHandle
);
2191 ReadConfig(GlobalConfig
.ConfigFilename
);
2193 if (GlobalConfig
.SpoofOSXVersion
&& GlobalConfig
.SpoofOSXVersion
[0] != L
'\0')
2197 WarnIfLegacyProblems();
2198 MainMenu
.TimeoutSeconds
= GlobalConfig
.Timeout
;
2200 // disable EFI watchdog timer
2201 refit_call4_wrapper(BS
->SetWatchdogTimer
, 0x0000, 0x0000, 0x0000, NULL
);
2203 // further bootstrap (now with config available)
2204 MokProtocol
= SecureBootSetup();
2207 ScanForBootloaders();
2211 if (GlobalConfig
.ScanDelay
> 0) {
2216 if (GlobalConfig
.ScanDelay
> 1)
2217 egDisplayMessage(L
"Pausing before disk scan; please wait....", &BGColor
);
2218 for (i
= 0; i
< GlobalConfig
.ScanDelay
; i
++)
2219 refit_call1_wrapper(BS
->Stall
, 1000000);
2220 RescanAll(GlobalConfig
.ScanDelay
> 1);
2223 if (GlobalConfig
.DefaultSelection
)
2224 SelectionName
= StrDuplicate(GlobalConfig
.DefaultSelection
);
2226 while (MainLoopRunning
) {
2227 MenuExit
= RunMainMenu(&MainMenu
, &SelectionName
, &ChosenEntry
);
2229 // The Escape key triggers a re-scan operation....
2230 if (MenuExit
== MENU_EXIT_ESCAPE
) {
2236 switch (ChosenEntry
->Tag
) {
2238 case TAG_REBOOT
: // Reboot
2240 refit_call4_wrapper(RT
->ResetSystem
, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
2241 MainLoopRunning
= FALSE
; // just in case we get this far
2244 case TAG_SHUTDOWN
: // Shut Down
2246 refit_call4_wrapper(RT
->ResetSystem
, EfiResetShutdown
, EFI_SUCCESS
, 0, NULL
);
2247 MainLoopRunning
= FALSE
; // just in case we get this far
2250 case TAG_ABOUT
: // About rEFInd
2254 case TAG_LOADER
: // Boot OS via .EFI loader
2255 StartLoader((LOADER_ENTRY
*)ChosenEntry
, SelectionName
);
2258 case TAG_LEGACY
: // Boot legacy OS
2259 StartLegacy((LEGACY_ENTRY
*)ChosenEntry
, SelectionName
);
2262 case TAG_LEGACY_UEFI
: // Boot a legacy OS on a non-Mac
2263 StartLegacyUEFI((LEGACY_ENTRY
*)ChosenEntry
, SelectionName
);
2266 case TAG_TOOL
: // Start a EFI tool
2267 StartTool((LOADER_ENTRY
*)ChosenEntry
);
2270 case TAG_EXIT
: // Terminate rEFInd
2271 if ((MokProtocol
) && !SecureBootUninstall()) {
2272 MainLoopRunning
= FALSE
; // just in case we get this far
2274 BeginTextScreen(L
" ");
2279 case TAG_FIRMWARE
: // Reboot into firmware's user interface
2280 RebootIntoFirmware();
2283 case TAG_CSR_ROTATE
:
2290 // If we end up here, things have gone wrong. Try to reboot, and if that
2291 // fails, go into an endless loop.
2292 refit_call4_wrapper(RT
->ResetSystem
, EfiResetCold
, EFI_SUCCESS
, 0, NULL
);