]> code.delx.au - refind/blob - refind/main.c
Minor refinement to FAT over-detection bug.
[refind] / refind / main.c
1 /*
2 * refind/main.c
3 * Main code for the boot menu
4 *
5 * Copyright (c) 2006-2010 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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.
23 *
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.
35 */
36 /*
37 * Modifications copyright (c) 2012-2015 Roderick W. Smith
38 *
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
42 *
43 */
44
45 #include "global.h"
46 #include "config.h"
47 #include "screen.h"
48 #include "legacy.h"
49 #include "lib.h"
50 #include "icns.h"
51 #include "menu.h"
52 #include "mok.h"
53 #include "gpt.h"
54 #include "security_policy.h"
55 #include "driver_support.h"
56 #include "../include/Handle.h"
57 #include "../include/refit_call_wrapper.h"
58 #include "../EfiLib/BdsHelper.h"
59 #include "../EfiLib/legacy.h"
60
61 #ifdef __MAKEWITH_GNUEFI
62 #ifndef EFI_SECURITY_VIOLATION
63 #define EFI_SECURITY_VIOLATION EFIERR (26)
64 #endif
65 #endif
66
67 #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
68 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
69 #endif
70
71 #ifdef __MAKEWITH_TIANO
72 #define LibLocateHandle gBS->LocateHandleBuffer
73 #endif
74
75 //
76 // constants
77
78 #define MACOSX_LOADER_DIR L"System\\Library\\CoreServices"
79 #define MACOSX_LOADER_PATH ( MACOSX_LOADER_DIR L"\\boot.efi" )
80 #if defined (EFIX64)
81 #define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\EFI\\tools\\shellx64.efi,\\shell.efi,\\shellx64.efi"
82 #define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi,\\EFI\\tools\\gptsync_x64.efi"
83 #define GDISK_NAMES L"\\EFI\\tools\\gdisk.efi,\\EFI\\tools\\gdisk_x64.efi"
84 #define NETBOOT_NAMES L"\\EFI\\tools\\ipxe.efi"
85 #define MEMTEST_NAMES L"memtest86.efi,memtest86_x64.efi,memtest86x64.efi,bootx64.efi"
86 #define DRIVER_DIRS L"drivers,drivers_x64"
87 #define FALLBACK_FULLNAME L"EFI\\BOOT\\bootx64.efi"
88 #define FALLBACK_BASENAME L"bootx64.efi"
89 #define EFI_STUB_ARCH 0x8664
90 #elif defined (EFI32)
91 #define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\EFI\\tools\\shellia32.efi,\\shell.efi,\\shellia32.efi"
92 #define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi,\\EFI\\tools\\gptsync_ia32.efi"
93 #define GDISK_NAMES L"\\EFI\\tools\\gdisk.efi,\\EFI\\tools\\gdisk_ia32.efi"
94 #define NETBOOT_NAMES L"\\EFI\\tools\\ipxe.efi"
95 #define MEMTEST_NAMES L"memtest86.efi,memtest86_ia32.efi,memtest86ia32.efi,bootia32.efi"
96 #define DRIVER_DIRS L"drivers,drivers_ia32"
97 #define FALLBACK_FULLNAME L"EFI\\BOOT\\bootia32.efi"
98 #define FALLBACK_BASENAME L"bootia32.efi"
99 #define EFI_STUB_ARCH 0x014c
100 #else
101 #define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\shell.efi"
102 #define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi"
103 #define GDISK_NAMES L"\\EFI\\tools\\gdisk.efi"
104 #define NETBOOT_NAMES L"\\EFI\\tools\\ipxe.efi"
105 #define MEMTEST_NAMES L"memtest86.efi"
106 #define DRIVER_DIRS L"drivers"
107 #define FALLBACK_FULLNAME L"EFI\\BOOT\\boot.efi" /* Not really correct */
108 #define FALLBACK_BASENAME L"boot.efi" /* Not really correct */
109 #endif
110 #define FAT_ARCH 0x0ef1fab9 /* ID for Apple "fat" binary */
111
112 #define IPXE_DISCOVER_NAME L"\\efi\\tools\\ipxe_discover.efi"
113 #define IPXE_NAME L"\\efi\\tools\\ipxe.efi"
114
115 // Filename patterns that identify EFI boot loaders. Note that a single case (either L"*.efi" or
116 // L"*.EFI") is fine for most systems; but Gigabyte's buggy Hybrid EFI does a case-sensitive
117 // comparison when it should do a case-insensitive comparison, so I'm doubling this up. It does
118 // no harm on other computers, AFAIK. In theory, every case variation should be done for
119 // completeness, but that's ridiculous....
120 #define LOADER_MATCH_PATTERNS L"*.efi,*.EFI"
121
122 // Patterns that identify Linux kernels. Added to the loader match pattern when the
123 // scan_all_linux_kernels option is set in the configuration file. Causes kernels WITHOUT
124 // a ".efi" extension to be found when scanning for boot loaders.
125 #define LINUX_MATCH_PATTERNS L"vmlinuz*,bzImage*"
126
127 static REFIT_MENU_ENTRY MenuEntryAbout = { L"About rEFInd", TAG_ABOUT, 1, 0, 'A', NULL, NULL, NULL };
128 static REFIT_MENU_ENTRY MenuEntryReset = { L"Reboot Computer", TAG_REBOOT, 1, 0, 'R', NULL, NULL, NULL };
129 static REFIT_MENU_ENTRY MenuEntryShutdown = { L"Shut Down Computer", TAG_SHUTDOWN, 1, 0, 'U', NULL, NULL, NULL };
130 REFIT_MENU_ENTRY MenuEntryReturn = { L"Return to Main Menu", TAG_RETURN, 1, 0, 0, NULL, NULL, NULL };
131 static REFIT_MENU_ENTRY MenuEntryExit = { L"Exit rEFInd", TAG_EXIT, 1, 0, 0, NULL, NULL, NULL };
132 static REFIT_MENU_ENTRY MenuEntryFirmware = { L"Reboot to Computer Setup Utility", TAG_FIRMWARE, 1, 0, 0, NULL, NULL, NULL };
133
134 REFIT_MENU_SCREEN MainMenu = { L"Main Menu", NULL, 0, NULL, 0, NULL, 0, L"Automatic boot",
135 L"Use arrow keys to move cursor; Enter to boot;",
136 L"Insert or F2 for more options; Esc to refresh" };
137 static REFIT_MENU_SCREEN AboutMenu = { L"About", NULL, 0, NULL, 0, NULL, 0, NULL, L"Press Enter to return to main menu", L"" };
138
139 REFIT_CONFIG GlobalConfig = { FALSE, TRUE, FALSE, FALSE, 0, 0, 0, DONT_CHANGE_TEXT_MODE, 20, 0, 0, GRAPHICS_FOR_OSX, LEGACY_TYPE_MAC,
140 0, 0, { DEFAULT_BIG_ICON_SIZE / 4, DEFAULT_SMALL_ICON_SIZE, DEFAULT_BIG_ICON_SIZE }, BANNER_NOSCALE,
141 NULL, NULL, CONFIG_FILE_NAME, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
142 { TAG_SHELL, TAG_MEMTEST, TAG_GDISK, TAG_APPLE_RECOVERY, TAG_WINDOWS_RECOVERY, TAG_MOK_TOOL,
143 TAG_ABOUT, TAG_SHUTDOWN, TAG_REBOOT, TAG_FIRMWARE, 0, 0, 0, 0, 0, 0, 0, 0 }
144 };
145
146 EFI_GUID GlobalGuid = EFI_GLOBAL_VARIABLE;
147 EFI_GUID RefindGuid = REFIND_GUID_VALUE;
148
149 GPT_DATA *gPartitions = NULL;
150
151 // Structure used to hold boot loader filenames and time stamps in
152 // a linked list; used to sort entries within a directory.
153 struct LOADER_LIST {
154 CHAR16 *FileName;
155 EFI_TIME TimeStamp;
156 struct LOADER_LIST *NextEntry;
157 };
158
159 //
160 // misc functions
161 //
162
163 static VOID AboutrEFInd(VOID)
164 {
165 CHAR16 *FirmwareVendor;
166
167 if (AboutMenu.EntryCount == 0) {
168 AboutMenu.TitleImage = BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT);
169 AddMenuInfoLine(&AboutMenu, L"rEFInd Version 0.8.5.5");
170 AddMenuInfoLine(&AboutMenu, L"");
171 AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2006-2010 Christoph Pfisterer");
172 AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2012-2015 Roderick W. Smith");
173 AddMenuInfoLine(&AboutMenu, L"Portions Copyright (c) Intel Corporation and others");
174 AddMenuInfoLine(&AboutMenu, L"Distributed under the terms of the GNU GPLv3 license");
175 AddMenuInfoLine(&AboutMenu, L"");
176 AddMenuInfoLine(&AboutMenu, L"Running on:");
177 AddMenuInfoLine(&AboutMenu, PoolPrint(L" EFI Revision %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & ((1 << 16) - 1)));
178 #if defined(EFI32)
179 AddMenuInfoLine(&AboutMenu, L" Platform: x86 (32 bit)");
180 #elif defined(EFIX64)
181 AddMenuInfoLine(&AboutMenu, PoolPrint(L" Platform: x86_64 (64 bit); Secure Boot %s",
182 secure_mode() ? L"active" : L"inactive"));
183 #else
184 AddMenuInfoLine(&AboutMenu, L" Platform: unknown");
185 #endif
186 FirmwareVendor = StrDuplicate(ST->FirmwareVendor);
187 LimitStringLength(FirmwareVendor, 65); // More than ~65 causes empty info page on 800x600 display
188 AddMenuInfoLine(&AboutMenu, PoolPrint(L" Firmware: %s %d.%02d", FirmwareVendor, ST->FirmwareRevision >> 16,
189 ST->FirmwareRevision & ((1 << 16) - 1)));
190 AddMenuInfoLine(&AboutMenu, PoolPrint(L" Screen Output: %s", egScreenDescription()));
191 AddMenuInfoLine(&AboutMenu, L"");
192 #if defined(__MAKEWITH_GNUEFI)
193 AddMenuInfoLine(&AboutMenu, L"Built with GNU-EFI");
194 #else
195 AddMenuInfoLine(&AboutMenu, L"Built with TianoCore EDK2");
196 #endif
197 AddMenuInfoLine(&AboutMenu, L"");
198 AddMenuInfoLine(&AboutMenu, L"For more information, see the rEFInd Web site:");
199 AddMenuInfoLine(&AboutMenu, L"http://www.rodsbooks.com/refind/");
200 AddMenuEntry(&AboutMenu, &MenuEntryReturn);
201 }
202
203 RunMenu(&AboutMenu, NULL);
204 } /* VOID AboutrEFInd() */
205
206 static VOID WarnSecureBootError(CHAR16 *Name, BOOLEAN Verbose) {
207 if (Name == NULL)
208 Name = L"the loader";
209
210 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
211 Print(L"Secure Boot validation failure loading %s!\n", Name);
212 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
213 if (Verbose && secure_mode()) {
214 Print(L"\nThis computer is configured with Secure Boot active, but\n%s has failed validation.\n", Name);
215 Print(L"\nYou can:\n * Launch another boot loader\n");
216 Print(L" * Disable Secure Boot in your firmware\n");
217 Print(L" * Sign %s with a machine owner key (MOK)\n", Name);
218 Print(L" * Use a MOK utility (often present on the second row) to add a MOK with which\n");
219 Print(L" %s has already been signed.\n", Name);
220 Print(L" * Use a MOK utility to register %s (\"enroll its hash\") without\n", Name);
221 Print(L" signing it.\n");
222 Print(L"\nSee http://www.rodsbooks.com/refind/secureboot.html for more information\n");
223 PauseForKey();
224 } // if
225 } // VOID WarnSecureBootError()
226
227 // Returns TRUE if this file is a valid EFI loader file, and is proper ARCH
228 static BOOLEAN IsValidLoader(EFI_FILE *RootDir, CHAR16 *FileName) {
229 BOOLEAN IsValid = TRUE;
230 #if defined (EFIX64) | defined (EFI32)
231 EFI_STATUS Status;
232 EFI_FILE_HANDLE FileHandle;
233 CHAR8 Header[512];
234 UINTN Size = sizeof(Header);
235
236 if ((RootDir == NULL) || (FileName == NULL)) {
237 // Assume valid here, because Macs produce NULL RootDir (& maybe FileName)
238 // when launching from a Firewire drive. This should be handled better, but
239 // fix would have to be in StartEFIImageList() and/or in FindVolumeAndFilename().
240 return TRUE;
241 } // if
242
243 Status = refit_call5_wrapper(RootDir->Open, RootDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
244 if (EFI_ERROR(Status))
245 return FALSE;
246
247 Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &Size, Header);
248 refit_call1_wrapper(FileHandle->Close, FileHandle);
249
250 IsValid = !EFI_ERROR(Status) &&
251 Size == sizeof(Header) &&
252 ((Header[0] == 'M' && Header[1] == 'Z' &&
253 (Size = *(UINT32 *)&Header[0x3c]) < 0x180 &&
254 Header[Size] == 'P' && Header[Size+1] == 'E' &&
255 Header[Size+2] == 0 && Header[Size+3] == 0 &&
256 *(UINT16 *)&Header[Size+4] == EFI_STUB_ARCH) ||
257 (*(UINT32 *)&Header == FAT_ARCH));
258 #endif
259 return IsValid;
260 } // BOOLEAN IsValidLoader()
261
262 // Launch an EFI binary.
263 EFI_STATUS StartEFIImageList(IN EFI_DEVICE_PATH **DevicePaths,
264 IN CHAR16 *LoadOptions, IN UINTN LoaderType,
265 IN CHAR16 *ImageTitle, IN CHAR8 OSType,
266 OUT UINTN *ErrorInStep,
267 IN BOOLEAN Verbose,
268 IN BOOLEAN IsDriver)
269 {
270 EFI_STATUS Status, ReturnStatus;
271 EFI_HANDLE ChildImageHandle;
272 EFI_LOADED_IMAGE *ChildLoadedImage = NULL;
273 REFIT_VOLUME *Volume = NULL;
274 UINTN DevicePathIndex;
275 CHAR16 ErrorInfo[256];
276 CHAR16 *FullLoadOptions = NULL;
277 CHAR16 *Filename = NULL;
278 CHAR16 *Temp;
279
280 if (ErrorInStep != NULL)
281 *ErrorInStep = 0;
282
283 // set load options
284 if (LoadOptions != NULL) {
285 FullLoadOptions = StrDuplicate(LoadOptions);
286 if ((LoaderType == TYPE_EFI) && (OSType == 'M')) {
287 MergeStrings(&FullLoadOptions, L" ", 0);
288 // NOTE: That last space is also added by the EFI shell and seems to be significant
289 // when passing options to Apple's boot.efi...
290 } // if
291 } // if (LoadOptions != NULL)
292 if (Verbose)
293 Print(L"Starting %s\nUsing load options '%s'\n", ImageTitle, FullLoadOptions ? FullLoadOptions : L"");
294
295 // load the image into memory (and execute it, in the case of a shim/MOK image).
296 ReturnStatus = Status = EFI_NOT_FOUND; // in case the list is empty
297 for (DevicePathIndex = 0; DevicePaths[DevicePathIndex] != NULL; DevicePathIndex++) {
298 FindVolumeAndFilename(DevicePaths[DevicePathIndex], &Volume, &Filename);
299 // Some EFIs crash if attempting to load driver for invalid architecture, so
300 // protect for this condition; but sometimes Volume comes back NULL, so provide
301 // an exception. (TODO: Handle this special condition better.)
302 if ((LoaderType == TYPE_LEGACY) || (Volume == NULL) || IsValidLoader(Volume->RootDir, Filename)) {
303 if (Filename && (LoaderType != TYPE_LEGACY)) {
304 Temp = PoolPrint(L"\\%s %s", Filename, FullLoadOptions ? FullLoadOptions : L"");
305 if (Temp != NULL) {
306 MyFreePool(FullLoadOptions);
307 FullLoadOptions = Temp;
308 }
309 } // if (Filename)
310
311 // NOTE: Below commented-out line could be more efficient if file were read ahead of
312 // time and passed as a pre-loaded image to LoadImage(), but it doesn't work on my
313 // 32-bit Mac Mini or my 64-bit Intel box when launching a Linux kernel; the
314 // kernel returns a "Failed to handle fs_proto" error message.
315 // TODO: Track down the cause of this error and fix it, if possible.
316 // ReturnStatus = Status = refit_call6_wrapper(BS->LoadImage, FALSE, SelfImageHandle, DevicePaths[DevicePathIndex],
317 // ImageData, ImageSize, &ChildImageHandle);
318 ReturnStatus = Status = refit_call6_wrapper(BS->LoadImage, FALSE, SelfImageHandle, DevicePaths[DevicePathIndex],
319 NULL, 0, &ChildImageHandle);
320 } else {
321 Print(L"Invalid loader file!\n");
322 ReturnStatus = EFI_LOAD_ERROR;
323 }
324 if (ReturnStatus != EFI_NOT_FOUND) {
325 break;
326 }
327 }
328 if ((Status == EFI_ACCESS_DENIED) || (Status == EFI_SECURITY_VIOLATION)) {
329 WarnSecureBootError(ImageTitle, Verbose);
330 goto bailout;
331 }
332 SPrint(ErrorInfo, 255, L"while loading %s", ImageTitle);
333 if (CheckError(Status, ErrorInfo)) {
334 if (ErrorInStep != NULL)
335 *ErrorInStep = 1;
336 goto bailout;
337 }
338
339 ReturnStatus = Status = refit_call3_wrapper(BS->HandleProtocol, ChildImageHandle, &LoadedImageProtocol,
340 (VOID **) &ChildLoadedImage);
341 if (CheckError(Status, L"while getting a LoadedImageProtocol handle")) {
342 if (ErrorInStep != NULL)
343 *ErrorInStep = 2;
344 goto bailout_unload;
345 }
346 ChildLoadedImage->LoadOptions = (VOID *)FullLoadOptions;
347 ChildLoadedImage->LoadOptionsSize = FullLoadOptions ? ((UINT32)StrLen(FullLoadOptions) + 1) * sizeof(CHAR16) : 0;
348 // turn control over to the image
349 // TODO: (optionally) re-enable the EFI watchdog timer!
350
351 // close open file handles
352 UninitRefitLib();
353 ReturnStatus = Status = refit_call3_wrapper(BS->StartImage, ChildImageHandle, NULL, NULL);
354
355 // control returns here when the child image calls Exit()
356 SPrint(ErrorInfo, 255, L"returned from %s", ImageTitle);
357 if (CheckError(Status, ErrorInfo)) {
358 if (ErrorInStep != NULL)
359 *ErrorInStep = 3;
360 }
361
362 // re-open file handles
363 ReinitRefitLib();
364
365 bailout_unload:
366 // unload the image, we don't care if it works or not...
367 if (!IsDriver)
368 Status = refit_call1_wrapper(BS->UnloadImage, ChildImageHandle);
369
370 bailout:
371 MyFreePool(FullLoadOptions);
372 return ReturnStatus;
373 } /* EFI_STATUS StartEFIImageList() */
374
375 static EFI_STATUS StartEFIImage(IN EFI_DEVICE_PATH *DevicePath,
376 IN CHAR16 *LoadOptions, IN UINTN LoaderType,
377 IN CHAR16 *ImageTitle, IN CHAR8 OSType,
378 OUT UINTN *ErrorInStep,
379 IN BOOLEAN Verbose,
380 IN BOOLEAN IsDriver
381 )
382 {
383 EFI_DEVICE_PATH *DevicePaths[2];
384
385 DevicePaths[0] = DevicePath;
386 DevicePaths[1] = NULL;
387 return StartEFIImageList(DevicePaths, LoadOptions, LoaderType, ImageTitle, OSType, ErrorInStep, Verbose, IsDriver);
388 } /* static EFI_STATUS StartEFIImage() */
389
390 // From gummiboot: Reboot the computer into its built-in user interface
391 static EFI_STATUS RebootIntoFirmware(VOID) {
392 CHAR8 *b;
393 UINTN size;
394 UINT64 osind;
395 EFI_STATUS err;
396
397 osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
398
399 err = EfivarGetRaw(&GlobalGuid, L"OsIndications", &b, &size);
400 if (err == EFI_SUCCESS)
401 osind |= (UINT64)*b;
402 MyFreePool(b);
403
404 err = EfivarSetRaw(&GlobalGuid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE);
405 if (err != EFI_SUCCESS)
406 return err;
407
408 refit_call4_wrapper(RT->ResetSystem, EfiResetCold, EFI_SUCCESS, 0, NULL);
409 Print(L"Error calling ResetSystem: %r", err);
410 PauseForKey();
411 return err;
412 }
413
414 // Record the value of the loader's name/description in rEFInd's "PreviousBoot" EFI variable,
415 // if it's different from what's already stored there.
416 VOID StoreLoaderName(IN CHAR16 *Name) {
417 EFI_STATUS Status;
418 CHAR16 *OldName = NULL;
419 UINTN Length;
420
421 if (Name) {
422 Status = EfivarGetRaw(&RefindGuid, L"PreviousBoot", (CHAR8**) &OldName, &Length);
423 if ((Status != EFI_SUCCESS) || (StrCmp(OldName, Name) != 0)) {
424 EfivarSetRaw(&RefindGuid, L"PreviousBoot", (CHAR8*) Name, StrLen(Name) * 2 + 2, TRUE);
425 } // if
426 MyFreePool(OldName);
427 } // if
428 } // VOID StoreLoaderName()
429
430 //
431 // EFI OS loader functions
432 //
433
434 // See http://www.thomas-krenn.com/en/wiki/Activating_the_Intel_VT_Virtualization_Feature
435 // for information on Intel VMX features
436 static VOID DoEnableAndLockVMX(VOID)
437 {
438 UINT32 msr = 0x3a;
439 UINT32 low_bits = 0, high_bits = 0;
440
441 // is VMX active ?
442 __asm__ volatile ("rdmsr" : "=a" (low_bits), "=d" (high_bits) : "c" (msr));
443
444 // enable and lock vmx if not locked
445 if ((low_bits & 1) == 0) {
446 high_bits = 0;
447 low_bits = 0x05;
448 msr = 0x3a;
449 __asm__ volatile ("wrmsr" : : "c" (msr), "a" (low_bits), "d" (high_bits));
450 }
451 } // VOID DoEnableAndLockVMX()
452
453 static VOID StartLoader(LOADER_ENTRY *Entry, CHAR16 *SelectionName)
454 {
455 UINTN ErrorInStep = 0;
456
457 if (GlobalConfig.EnableAndLockVMX) {
458 DoEnableAndLockVMX();
459 }
460
461 BeginExternalScreen(Entry->UseGraphicsMode, L"Booting OS");
462 StoreLoaderName(SelectionName);
463 StartEFIImage(Entry->DevicePath, Entry->LoadOptions, TYPE_EFI,
464 Basename(Entry->LoaderPath), Entry->OSType, &ErrorInStep, !Entry->UseGraphicsMode, FALSE);
465 FinishExternalScreen();
466 }
467
468 // Locate an initrd or initramfs file that matches the kernel specified by LoaderPath.
469 // The matching file has a name that begins with "init" and includes the same version
470 // number string as is found in LoaderPath -- but not a longer version number string.
471 // For instance, if LoaderPath is \EFI\kernels\bzImage-3.3.0.efi, and if \EFI\kernels
472 // has a file called initramfs-3.3.0.img, this function will return the string
473 // '\EFI\kernels\initramfs-3.3.0.img'. If the directory ALSO contains the file
474 // initramfs-3.3.0-rc7.img or initramfs-13.3.0.img, those files will NOT match;
475 // however, initmine-3.3.0.img might match. (FindInitrd() returns the first match it
476 // finds.) Thus, care should be taken to avoid placing duplicate matching files in
477 // the kernel's directory.
478 // If no matching init file can be found, returns NULL.
479 static CHAR16 * FindInitrd(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
480 CHAR16 *InitrdName = NULL, *FileName, *KernelVersion, *InitrdVersion, *Path;
481 REFIT_DIR_ITER DirIter;
482 EFI_FILE_INFO *DirEntry;
483
484 FileName = Basename(LoaderPath);
485 KernelVersion = FindNumbers(FileName);
486 Path = FindPath(LoaderPath);
487
488 // Add trailing backslash for root directory; necessary on some systems, but must
489 // NOT be added to all directories, since on other systems, a trailing backslash on
490 // anything but the root directory causes them to flake out!
491 if (StrLen(Path) == 0) {
492 MergeStrings(&Path, L"\\", 0);
493 } // if
494 DirIterOpen(Volume->RootDir, Path, &DirIter);
495 // Now add a trailing backslash if it was NOT added earlier, for consistency in
496 // building the InitrdName later....
497 if ((StrLen(Path) > 0) && (Path[StrLen(Path) - 1] != L'\\'))
498 MergeStrings(&Path, L"\\", 0);
499 while ((DirIterNext(&DirIter, 2, L"init*", &DirEntry)) && (InitrdName == NULL)) {
500 InitrdVersion = FindNumbers(DirEntry->FileName);
501 if (KernelVersion != NULL) {
502 if (StriCmp(InitrdVersion, KernelVersion) == 0) {
503 InitrdName = PoolPrint(L"%s%s", Path, DirEntry->FileName);
504 } // if
505 } else {
506 if (InitrdVersion == NULL) {
507 InitrdName = PoolPrint(L"%s%s", Path, DirEntry->FileName);
508 } // if
509 } // if/else
510 MyFreePool(InitrdVersion);
511 } // while
512 DirIterClose(&DirIter);
513
514 // Note: Don't FreePool(FileName), since Basename returns a pointer WITHIN the string it's passed.
515 MyFreePool(KernelVersion);
516 MyFreePool(Path);
517 return (InitrdName);
518 } // static CHAR16 * FindInitrd()
519
520 LOADER_ENTRY * AddPreparedLoaderEntry(LOADER_ENTRY *Entry) {
521 AddMenuEntry(&MainMenu, (REFIT_MENU_ENTRY *)Entry);
522
523 return(Entry);
524 } // LOADER_ENTRY * AddPreparedLoaderEntry()
525
526 // Creates a copy of a menu screen.
527 // Returns a pointer to the copy of the menu screen.
528 static REFIT_MENU_SCREEN* CopyMenuScreen(REFIT_MENU_SCREEN *Entry) {
529 REFIT_MENU_SCREEN *NewEntry;
530 UINTN i;
531
532 NewEntry = AllocateZeroPool(sizeof(REFIT_MENU_SCREEN));
533 if ((Entry != NULL) && (NewEntry != NULL)) {
534 CopyMem(NewEntry, Entry, sizeof(REFIT_MENU_SCREEN));
535 NewEntry->Title = (Entry->Title) ? StrDuplicate(Entry->Title) : NULL;
536 NewEntry->TimeoutText = (Entry->TimeoutText) ? StrDuplicate(Entry->TimeoutText) : NULL;
537 if (Entry->TitleImage != NULL) {
538 NewEntry->TitleImage = AllocatePool(sizeof(EG_IMAGE));
539 if (NewEntry->TitleImage != NULL)
540 CopyMem(NewEntry->TitleImage, Entry->TitleImage, sizeof(EG_IMAGE));
541 } // if
542 NewEntry->InfoLines = (CHAR16**) AllocateZeroPool(Entry->InfoLineCount * (sizeof(CHAR16*)));
543 for (i = 0; i < Entry->InfoLineCount && NewEntry->InfoLines; i++) {
544 NewEntry->InfoLines[i] = (Entry->InfoLines[i]) ? StrDuplicate(Entry->InfoLines[i]) : NULL;
545 } // for
546 NewEntry->Entries = (REFIT_MENU_ENTRY**) AllocateZeroPool(Entry->EntryCount * (sizeof (REFIT_MENU_ENTRY*)));
547 for (i = 0; i < Entry->EntryCount && NewEntry->Entries; i++) {
548 AddMenuEntry(NewEntry, Entry->Entries[i]);
549 } // for
550 NewEntry->Hint1 = (Entry->Hint1) ? StrDuplicate(Entry->Hint1) : NULL;
551 NewEntry->Hint2 = (Entry->Hint2) ? StrDuplicate(Entry->Hint2) : NULL;
552 } // if
553 return (NewEntry);
554 } // static REFIT_MENU_SCREEN* CopyMenuScreen()
555
556 // Creates a copy of a menu entry. Intended to enable moving a stack-based
557 // menu entry (such as the ones for the "reboot" and "exit" functions) to
558 // to the heap. This enables easier deletion of the whole set of menu
559 // entries when re-scanning.
560 // Returns a pointer to the copy of the menu entry.
561 static REFIT_MENU_ENTRY* CopyMenuEntry(REFIT_MENU_ENTRY *Entry) {
562 REFIT_MENU_ENTRY *NewEntry;
563
564 NewEntry = AllocateZeroPool(sizeof(REFIT_MENU_ENTRY));
565 if ((Entry != NULL) && (NewEntry != NULL)) {
566 CopyMem(NewEntry, Entry, sizeof(REFIT_MENU_ENTRY));
567 NewEntry->Title = (Entry->Title) ? StrDuplicate(Entry->Title) : NULL;
568 if (Entry->BadgeImage != NULL) {
569 NewEntry->BadgeImage = AllocatePool(sizeof(EG_IMAGE));
570 if (NewEntry->BadgeImage != NULL)
571 CopyMem(NewEntry->BadgeImage, Entry->BadgeImage, sizeof(EG_IMAGE));
572 }
573 if (Entry->Image != NULL) {
574 NewEntry->Image = AllocatePool(sizeof(EG_IMAGE));
575 if (NewEntry->Image != NULL)
576 CopyMem(NewEntry->Image, Entry->Image, sizeof(EG_IMAGE));
577 }
578 if (Entry->SubScreen != NULL) {
579 NewEntry->SubScreen = CopyMenuScreen(Entry->SubScreen);
580 }
581 } // if
582 return (NewEntry);
583 } // REFIT_MENU_ENTRY* CopyMenuEntry()
584
585 // Creates a new LOADER_ENTRY data structure and populates it with
586 // default values from the specified Entry, or NULL values if Entry
587 // is unspecified (NULL).
588 // Returns a pointer to the new data structure, or NULL if it
589 // couldn't be allocated
590 LOADER_ENTRY *InitializeLoaderEntry(IN LOADER_ENTRY *Entry) {
591 LOADER_ENTRY *NewEntry = NULL;
592
593 NewEntry = AllocateZeroPool(sizeof(LOADER_ENTRY));
594 if (NewEntry != NULL) {
595 NewEntry->me.Title = NULL;
596 NewEntry->me.Tag = TAG_LOADER;
597 NewEntry->Enabled = TRUE;
598 NewEntry->UseGraphicsMode = FALSE;
599 NewEntry->OSType = 0;
600 if (Entry != NULL) {
601 NewEntry->LoaderPath = (Entry->LoaderPath) ? StrDuplicate(Entry->LoaderPath) : NULL;
602 NewEntry->VolName = (Entry->VolName) ? StrDuplicate(Entry->VolName) : NULL;
603 NewEntry->DevicePath = Entry->DevicePath;
604 NewEntry->UseGraphicsMode = Entry->UseGraphicsMode;
605 NewEntry->LoadOptions = (Entry->LoadOptions) ? StrDuplicate(Entry->LoadOptions) : NULL;
606 NewEntry->InitrdPath = (Entry->InitrdPath) ? StrDuplicate(Entry->InitrdPath) : NULL;
607 }
608 } // if
609 return (NewEntry);
610 } // LOADER_ENTRY *InitializeLoaderEntry()
611
612 // Adds InitrdPath to Options, but only if Options doesn't already include an
613 // initrd= line. Done to enable overriding the default initrd selection in a
614 // refind_linux.conf file's options list.
615 // Returns a pointer to a new string. The calling function is responsible for
616 // freeing its memory.
617 static CHAR16 *AddInitrdToOptions(CHAR16 *Options, CHAR16 *InitrdPath) {
618 CHAR16 *NewOptions = NULL;
619
620 if (Options != NULL)
621 NewOptions = StrDuplicate(Options);
622 if ((InitrdPath != NULL) && !StriSubCmp(L"initrd=", Options)) {
623 MergeStrings(&NewOptions, L"initrd=", L' ');
624 MergeStrings(&NewOptions, InitrdPath, 0);
625 }
626 return NewOptions;
627 } // CHAR16 *AddInitrdToOptions()
628
629 // Prepare a REFIT_MENU_SCREEN data structure for a subscreen entry. This sets up
630 // the default entry that launches the boot loader using the same options as the
631 // main Entry does. Subsequent options can be added by the calling function.
632 // If a subscreen already exists in the Entry that's passed to this function,
633 // it's left unchanged and a pointer to it is returned.
634 // Returns a pointer to the new subscreen data structure, or NULL if there
635 // were problems allocating memory.
636 REFIT_MENU_SCREEN *InitializeSubScreen(IN LOADER_ENTRY *Entry) {
637 CHAR16 *FileName, *MainOptions = NULL;
638 REFIT_MENU_SCREEN *SubScreen = NULL;
639 LOADER_ENTRY *SubEntry;
640
641 FileName = Basename(Entry->LoaderPath);
642 if (Entry->me.SubScreen == NULL) { // No subscreen yet; initialize default entry....
643 SubScreen = AllocateZeroPool(sizeof(REFIT_MENU_SCREEN));
644 if (SubScreen != NULL) {
645 SubScreen->Title = AllocateZeroPool(sizeof(CHAR16) * 256);
646 SPrint(SubScreen->Title, 255, L"Boot Options for %s on %s",
647 (Entry->Title != NULL) ? Entry->Title : FileName, Entry->VolName);
648 SubScreen->TitleImage = Entry->me.Image;
649 // default entry
650 SubEntry = InitializeLoaderEntry(Entry);
651 if (SubEntry != NULL) {
652 SubEntry->me.Title = StrDuplicate(L"Boot using default options");
653 MainOptions = SubEntry->LoadOptions;
654 SubEntry->LoadOptions = AddInitrdToOptions(MainOptions, SubEntry->InitrdPath);
655 MyFreePool(MainOptions);
656 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
657 } // if (SubEntry != NULL)
658 SubScreen->Hint1 = StrDuplicate(SUBSCREEN_HINT1);
659 if (GlobalConfig.HideUIFlags & HIDEUI_FLAG_EDITOR) {
660 SubScreen->Hint2 = StrDuplicate(SUBSCREEN_HINT2_NO_EDITOR);
661 } else {
662 SubScreen->Hint2 = StrDuplicate(SUBSCREEN_HINT2);
663 } // if/else
664 } // if (SubScreen != NULL)
665 } else { // existing subscreen; less initialization, and just add new entry later....
666 SubScreen = Entry->me.SubScreen;
667 } // if/else
668 return SubScreen;
669 } // REFIT_MENU_SCREEN *InitializeSubScreen()
670
671 VOID GenerateSubScreen(LOADER_ENTRY *Entry, IN REFIT_VOLUME *Volume) {
672 REFIT_MENU_SCREEN *SubScreen;
673 LOADER_ENTRY *SubEntry;
674 CHAR16 *InitrdName;
675 CHAR16 DiagsFileName[256];
676 REFIT_FILE *File;
677 UINTN TokenCount;
678 CHAR16 **TokenList;
679
680 // create the submenu
681 if (StrLen(Entry->Title) == 0) {
682 MyFreePool(Entry->Title);
683 Entry->Title = NULL;
684 }
685 SubScreen = InitializeSubScreen(Entry);
686
687 // loader-specific submenu entries
688 if (Entry->OSType == 'M') { // entries for Mac OS X
689 #if defined(EFIX64)
690 SubEntry = InitializeLoaderEntry(Entry);
691 if (SubEntry != NULL) {
692 SubEntry->me.Title = L"Boot Mac OS X with a 64-bit kernel";
693 SubEntry->LoadOptions = L"arch=x86_64";
694 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_OSX;
695 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
696 } // if
697
698 SubEntry = InitializeLoaderEntry(Entry);
699 if (SubEntry != NULL) {
700 SubEntry->me.Title = L"Boot Mac OS X with a 32-bit kernel";
701 SubEntry->LoadOptions = L"arch=i386";
702 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_OSX;
703 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
704 } // if
705 #endif
706
707 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_SINGLEUSER)) {
708 SubEntry = InitializeLoaderEntry(Entry);
709 if (SubEntry != NULL) {
710 SubEntry->me.Title = L"Boot Mac OS X in verbose mode";
711 SubEntry->UseGraphicsMode = FALSE;
712 SubEntry->LoadOptions = L"-v";
713 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
714 } // if
715
716 #if defined(EFIX64)
717 SubEntry = InitializeLoaderEntry(Entry);
718 if (SubEntry != NULL) {
719 SubEntry->me.Title = L"Boot Mac OS X in verbose mode (64-bit)";
720 SubEntry->UseGraphicsMode = FALSE;
721 SubEntry->LoadOptions = L"-v arch=x86_64";
722 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
723 }
724
725 SubEntry = InitializeLoaderEntry(Entry);
726 if (SubEntry != NULL) {
727 SubEntry->me.Title = L"Boot Mac OS X in verbose mode (32-bit)";
728 SubEntry->UseGraphicsMode = FALSE;
729 SubEntry->LoadOptions = L"-v arch=i386";
730 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
731 }
732 #endif
733
734 SubEntry = InitializeLoaderEntry(Entry);
735 if (SubEntry != NULL) {
736 SubEntry->me.Title = L"Boot Mac OS X in single user mode";
737 SubEntry->UseGraphicsMode = FALSE;
738 SubEntry->LoadOptions = L"-v -s";
739 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
740 } // if
741 } // single-user mode allowed
742
743 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_SAFEMODE)) {
744 SubEntry = InitializeLoaderEntry(Entry);
745 if (SubEntry != NULL) {
746 SubEntry->me.Title = L"Boot Mac OS X in safe mode";
747 SubEntry->UseGraphicsMode = FALSE;
748 SubEntry->LoadOptions = L"-v -x";
749 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
750 } // if
751 } // safe mode allowed
752
753 // check for Apple hardware diagnostics
754 StrCpy(DiagsFileName, L"System\\Library\\CoreServices\\.diagnostics\\diags.efi");
755 if (FileExists(Volume->RootDir, DiagsFileName) && !(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HWTEST)) {
756 SubEntry = InitializeLoaderEntry(Entry);
757 if (SubEntry != NULL) {
758 SubEntry->me.Title = L"Run Apple Hardware Test";
759 MyFreePool(SubEntry->LoaderPath);
760 SubEntry->LoaderPath = StrDuplicate(DiagsFileName);
761 SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
762 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_OSX;
763 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
764 } // if
765 } // if diagnostics entry found
766
767 } else if (Entry->OSType == 'L') { // entries for Linux kernels with EFI stub loaders
768 File = ReadLinuxOptionsFile(Entry->LoaderPath, Volume);
769 if (File != NULL) {
770 InitrdName = FindInitrd(Entry->LoaderPath, Volume);
771 TokenCount = ReadTokenLine(File, &TokenList);
772 // first entry requires special processing, since it was initially set
773 // up with a default title but correct options by InitializeSubScreen(),
774 // earlier....
775 if ((SubScreen->Entries != NULL) && (SubScreen->Entries[0] != NULL)) {
776 MyFreePool(SubScreen->Entries[0]->Title);
777 SubScreen->Entries[0]->Title = TokenList[0] ? StrDuplicate(TokenList[0]) : StrDuplicate(L"Boot Linux");
778 } // if
779 FreeTokenLine(&TokenList, &TokenCount);
780 while ((TokenCount = ReadTokenLine(File, &TokenList)) > 1) {
781 SubEntry = InitializeLoaderEntry(Entry);
782 SubEntry->me.Title = TokenList[0] ? StrDuplicate(TokenList[0]) : StrDuplicate(L"Boot Linux");
783 MyFreePool(SubEntry->LoadOptions);
784 SubEntry->LoadOptions = AddInitrdToOptions(TokenList[1], InitrdName);
785 FreeTokenLine(&TokenList, &TokenCount);
786 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_LINUX;
787 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
788 } // while
789 MyFreePool(InitrdName);
790 MyFreePool(File);
791 } // if
792
793 } else if (Entry->OSType == 'E') { // entries for ELILO
794 SubEntry = InitializeLoaderEntry(Entry);
795 if (SubEntry != NULL) {
796 SubEntry->me.Title = L"Run ELILO in interactive mode";
797 SubEntry->LoadOptions = L"-p";
798 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_ELILO;
799 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
800 }
801
802 SubEntry = InitializeLoaderEntry(Entry);
803 if (SubEntry != NULL) {
804 SubEntry->me.Title = L"Boot Linux for a 17\" iMac or a 15\" MacBook Pro (*)";
805 SubEntry->UseGraphicsMode = TRUE;
806 SubEntry->LoadOptions = L"-d 0 i17";
807 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_ELILO;
808 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
809 }
810
811 SubEntry = InitializeLoaderEntry(Entry);
812 if (SubEntry != NULL) {
813 SubEntry->me.Title = L"Boot Linux for a 20\" iMac (*)";
814 SubEntry->UseGraphicsMode = TRUE;
815 SubEntry->LoadOptions = L"-d 0 i20";
816 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_ELILO;
817 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
818 }
819
820 SubEntry = InitializeLoaderEntry(Entry);
821 if (SubEntry != NULL) {
822 SubEntry->me.Title = L"Boot Linux for a Mac Mini (*)";
823 SubEntry->UseGraphicsMode = TRUE;
824 SubEntry->LoadOptions = L"-d 0 mini";
825 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_ELILO;
826 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
827 }
828
829 AddMenuInfoLine(SubScreen, L"NOTE: This is an example. Entries");
830 AddMenuInfoLine(SubScreen, L"marked with (*) may not work.");
831
832 } else if (Entry->OSType == 'X') { // entries for xom.efi
833 // by default, skip the built-in selection and boot from hard disk only
834 Entry->LoadOptions = L"-s -h";
835
836 SubEntry = InitializeLoaderEntry(Entry);
837 if (SubEntry != NULL) {
838 SubEntry->me.Title = L"Boot Windows from Hard Disk";
839 SubEntry->LoadOptions = L"-s -h";
840 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_WINDOWS;
841 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
842 }
843
844 SubEntry = InitializeLoaderEntry(Entry);
845 if (SubEntry != NULL) {
846 SubEntry->me.Title = L"Boot Windows from CD-ROM";
847 SubEntry->LoadOptions = L"-s -c";
848 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_WINDOWS;
849 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
850 }
851
852 SubEntry = InitializeLoaderEntry(Entry);
853 if (SubEntry != NULL) {
854 SubEntry->me.Title = L"Run XOM in text mode";
855 SubEntry->UseGraphicsMode = FALSE;
856 SubEntry->LoadOptions = L"-v";
857 SubEntry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_WINDOWS;
858 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
859 }
860 } // entries for xom.efi
861 AddMenuEntry(SubScreen, &MenuEntryReturn);
862 Entry->me.SubScreen = SubScreen;
863 } // VOID GenerateSubScreen()
864
865 // Returns options for a Linux kernel. Reads them from an options file in the
866 // kernel's directory; and if present, adds an initrd= option for an initial
867 // RAM disk file with the same version number as the kernel file.
868 static CHAR16 * GetMainLinuxOptions(IN CHAR16 * LoaderPath, IN REFIT_VOLUME *Volume) {
869 CHAR16 *Options = NULL, *InitrdName, *FullOptions = NULL;
870
871 Options = GetFirstOptionsFromFile(LoaderPath, Volume);
872 InitrdName = FindInitrd(LoaderPath, Volume);
873 FullOptions = AddInitrdToOptions(Options, InitrdName);
874
875 MyFreePool(Options);
876 MyFreePool(InitrdName);
877 return (FullOptions);
878 } // static CHAR16 * GetMainLinuxOptions()
879
880 // Try to guess the name of the Linux distribution & add that name to
881 // OSIconName list.
882 static VOID GuessLinuxDistribution(CHAR16 **OSIconName, REFIT_VOLUME *Volume, CHAR16 *LoaderPath) {
883 UINTN FileSize = 0;
884 REFIT_FILE File;
885 CHAR16** TokenList;
886 UINTN TokenCount = 0;
887
888 // If on Linux root fs, /etc/os-release file probably has clues....
889 if (FileExists(Volume->RootDir, L"etc\\os-release") &&
890 (ReadFile(Volume->RootDir, L"etc\\os-release", &File, &FileSize) == EFI_SUCCESS)) {
891 do {
892 TokenCount = ReadTokenLine(&File, &TokenList);
893 if ((TokenCount > 1) && ((StriCmp(TokenList[0], L"ID") == 0) || (StriCmp(TokenList[0], L"NAME") == 0))) {
894 MergeStrings(OSIconName, TokenList[1], L',');
895 } // if
896 FreeTokenLine(&TokenList, &TokenCount);
897 } while (TokenCount > 0);
898 MyFreePool(File.Buffer);
899 } // if
900
901 // Search for clues in the kernel's filename....
902 if (StriSubCmp(L".fc", LoaderPath))
903 MergeStrings(OSIconName, L"fedora", L',');
904 if (StriSubCmp(L".el", LoaderPath))
905 MergeStrings(OSIconName, L"redhat", L',');
906 } // VOID GuessLinuxDistribution()
907
908 // Sets a few defaults for a loader entry -- mainly the icon, but also the OS type
909 // code and shortcut letter. For Linux EFI stub loaders, also sets kernel options
910 // that will (with luck) work fairly automatically.
911 VOID SetLoaderDefaults(LOADER_ENTRY *Entry, CHAR16 *LoaderPath, REFIT_VOLUME *Volume) {
912 CHAR16 *NameClues, *PathOnly, *NoExtension, *OSIconName = NULL, *Temp, *SubString;
913 CHAR16 ShortcutLetter = 0;
914 UINTN i = 0, Length;
915
916 NameClues = Basename(LoaderPath);
917 PathOnly = FindPath(LoaderPath);
918 NoExtension = StripEfiExtension(NameClues);
919
920 if (Volume->DiskKind == DISK_KIND_NET) {
921 MergeStrings(&NameClues, Entry->me.Title, L' ');
922 } else {
923 // locate a custom icon for the loader
924 // Anything found here takes precedence over the "hints" in the OSIconName variable
925 if (!Entry->me.Image) {
926 Entry->me.Image = egLoadIconAnyType(Volume->RootDir, PathOnly, NoExtension, GlobalConfig.IconSizes[ICON_SIZE_BIG]);
927 }
928 if (!Entry->me.Image) {
929 Entry->me.Image = egCopyImage(Volume->VolIconImage);
930 }
931
932 // Begin creating icon "hints" by using last part of directory path leading
933 // to the loader
934 Temp = FindLastDirName(LoaderPath);
935 MergeStrings(&OSIconName, Temp, L',');
936 MyFreePool(Temp);
937 Temp = NULL;
938 if (OSIconName != NULL) {
939 ShortcutLetter = OSIconName[0];
940 }
941
942 // Add every "word" in the volume label, delimited by spaces, dashes (-), or
943 // underscores (_), to the list of hints to be used in searching for OS
944 // icons.
945 if ((Volume->VolName) && (StrLen(Volume->VolName) > 0)) {
946 Temp = SubString = StrDuplicate(Volume->VolName);
947 if (Temp != NULL) {
948 Length = StrLen(Temp);
949 for (i = 0; i < Length; i++) {
950 if ((Temp[i] == L' ') || (Temp[i] == L'_') || (Temp[i] == L'-')) {
951 Temp[i] = 0;
952 if (StrLen(SubString) > 0)
953 MergeStrings(&OSIconName, SubString, L',');
954 SubString = Temp + i + 1;
955 } // if
956 } // for
957 MergeStrings(&OSIconName, SubString, L',');
958 MyFreePool(Temp);
959 } // if
960 } // if
961 } // if/else network boot
962
963 // detect specific loaders
964 if (StriSubCmp(L"bzImage", NameClues) || StriSubCmp(L"vmlinuz", NameClues)) {
965 if (Volume->DiskKind != DISK_KIND_NET) {
966 GuessLinuxDistribution(&OSIconName, Volume, LoaderPath);
967 Entry->LoadOptions = GetMainLinuxOptions(LoaderPath, Volume);
968 }
969 MergeStrings(&OSIconName, L"linux", L',');
970 Entry->OSType = 'L';
971 if (ShortcutLetter == 0)
972 ShortcutLetter = 'L';
973 Entry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_LINUX;
974 } else if (StriSubCmp(L"refit", LoaderPath)) {
975 MergeStrings(&OSIconName, L"refit", L',');
976 Entry->OSType = 'R';
977 ShortcutLetter = 'R';
978 } else if (StriSubCmp(L"refind", LoaderPath)) {
979 MergeStrings(&OSIconName, L"refind", L',');
980 Entry->OSType = 'R';
981 ShortcutLetter = 'R';
982 } else if (StriCmp(LoaderPath, MACOSX_LOADER_PATH) == 0) {
983 MergeStrings(&OSIconName, L"mac", L',');
984 Entry->OSType = 'M';
985 ShortcutLetter = 'M';
986 Entry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_OSX;
987 } else if (StriCmp(NameClues, L"diags.efi") == 0) {
988 MergeStrings(&OSIconName, L"hwtest", L',');
989 } else if (StriCmp(NameClues, L"e.efi") == 0 || StriCmp(NameClues, L"elilo.efi") == 0 || StriSubCmp(L"elilo", NameClues)) {
990 MergeStrings(&OSIconName, L"elilo,linux", L',');
991 Entry->OSType = 'E';
992 if (ShortcutLetter == 0)
993 ShortcutLetter = 'L';
994 Entry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_ELILO;
995 } else if (StriSubCmp(L"grub", NameClues)) {
996 MergeStrings(&OSIconName, L"grub,linux", L',');
997 Entry->OSType = 'G';
998 ShortcutLetter = 'G';
999 Entry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_GRUB;
1000 } else if (StriCmp(NameClues, L"cdboot.efi") == 0 ||
1001 StriCmp(NameClues, L"bootmgr.efi") == 0 ||
1002 StriCmp(NameClues, L"bootmgfw.efi") == 0 ||
1003 StriCmp(NameClues, L"bkpbootmgfw.efi") == 0) {
1004 MergeStrings(&OSIconName, L"win", L',');
1005 Entry->OSType = 'W';
1006 ShortcutLetter = 'W';
1007 Entry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_WINDOWS;
1008 } else if (StriCmp(NameClues, L"xom.efi") == 0) {
1009 MergeStrings(&OSIconName, L"xom,win", L',');
1010 Entry->UseGraphicsMode = TRUE;
1011 Entry->OSType = 'X';
1012 ShortcutLetter = 'W';
1013 Entry->UseGraphicsMode = GlobalConfig.GraphicsFor & GRAPHICS_FOR_WINDOWS;
1014 }
1015 else if (StriSubCmp(L"ipxe", NameClues)) {
1016 Entry->OSType = 'N';
1017 ShortcutLetter = 'N';
1018 MergeStrings(&OSIconName, L"network", L',');
1019 }
1020
1021 if ((ShortcutLetter >= 'a') && (ShortcutLetter <= 'z'))
1022 ShortcutLetter = ShortcutLetter - 'a' + 'A'; // convert lowercase to uppercase
1023 Entry->me.ShortcutLetter = ShortcutLetter;
1024 if (Entry->me.Image == NULL)
1025 Entry->me.Image = LoadOSIcon(OSIconName, L"unknown", FALSE);
1026 MyFreePool(PathOnly);
1027 } // VOID SetLoaderDefaults()
1028
1029 // Add a specified EFI boot loader to the list, using automatic settings
1030 // for icons, options, etc.
1031 LOADER_ENTRY * AddLoaderEntry(IN CHAR16 *LoaderPath, IN CHAR16 *LoaderTitle, IN REFIT_VOLUME *Volume) {
1032 LOADER_ENTRY *Entry;
1033
1034 CleanUpPathNameSlashes(LoaderPath);
1035 Entry = InitializeLoaderEntry(NULL);
1036 if (Entry != NULL) {
1037 Entry->Title = StrDuplicate((LoaderTitle != NULL) ? LoaderTitle : LoaderPath);
1038 Entry->me.Title = AllocateZeroPool(sizeof(CHAR16) * 256);
1039 // Extra space at end of Entry->me.Title enables searching on Volume->VolName even if another volume
1040 // name is identical except for something added to the end (e.g., VolB1 vs. VolB12).
1041 // Note: Volume->VolName will be NULL for network boot programs.
1042 if (Volume->VolName)
1043 SPrint(Entry->me.Title, 255, L"Boot %s from %s ", (LoaderTitle != NULL) ? LoaderTitle : LoaderPath, Volume->VolName);
1044 else
1045 SPrint(Entry->me.Title, 255, L"Boot %s ", (LoaderTitle != NULL) ? LoaderTitle : LoaderPath);
1046 Entry->me.Row = 0;
1047 Entry->me.BadgeImage = Volume->VolBadgeImage;
1048 if ((LoaderPath != NULL) && (LoaderPath[0] != L'\\')) {
1049 Entry->LoaderPath = StrDuplicate(L"\\");
1050 } else {
1051 Entry->LoaderPath = NULL;
1052 }
1053 MergeStrings(&(Entry->LoaderPath), LoaderPath, 0);
1054 Entry->VolName = Volume->VolName;
1055 Entry->DevicePath = FileDevicePath(Volume->DeviceHandle, Entry->LoaderPath);
1056 SetLoaderDefaults(Entry, LoaderPath, Volume);
1057 GenerateSubScreen(Entry, Volume);
1058 AddMenuEntry(&MainMenu, (REFIT_MENU_ENTRY *)Entry);
1059 }
1060
1061 return(Entry);
1062 } // LOADER_ENTRY * AddLoaderEntry()
1063
1064 // Returns -1 if (Time1 < Time2), +1 if (Time1 > Time2), or 0 if
1065 // (Time1 == Time2). Precision is only to the nearest second; since
1066 // this is used for sorting boot loader entries, differences smaller
1067 // than this are likely to be meaningless (and unlikely!).
1068 INTN TimeComp(IN EFI_TIME *Time1, IN EFI_TIME *Time2) {
1069 INT64 Time1InSeconds, Time2InSeconds;
1070
1071 // Following values are overestimates; I'm assuming 31 days in every month.
1072 // This is fine for the purpose of this function, which is limited
1073 Time1InSeconds = Time1->Second + (Time1->Minute * 60) + (Time1->Hour * 3600) + (Time1->Day * 86400) +
1074 (Time1->Month * 2678400) + ((Time1->Year - 1998) * 32140800);
1075 Time2InSeconds = Time2->Second + (Time2->Minute * 60) + (Time2->Hour * 3600) + (Time2->Day * 86400) +
1076 (Time2->Month * 2678400) + ((Time2->Year - 1998) * 32140800);
1077 if (Time1InSeconds < Time2InSeconds)
1078 return (-1);
1079 else if (Time1InSeconds > Time2InSeconds)
1080 return (1);
1081
1082 return 0;
1083 } // INTN TimeComp()
1084
1085 // Adds a loader list element, keeping it sorted by date. Returns the new
1086 // first element (the one with the most recent date).
1087 static struct LOADER_LIST * AddLoaderListEntry(struct LOADER_LIST *LoaderList, struct LOADER_LIST *NewEntry) {
1088 struct LOADER_LIST *LatestEntry, *CurrentEntry, *PrevEntry = NULL;
1089
1090 LatestEntry = CurrentEntry = LoaderList;
1091 if (LoaderList == NULL) {
1092 LatestEntry = NewEntry;
1093 } else {
1094 while ((CurrentEntry != NULL) && (TimeComp(&(NewEntry->TimeStamp), &(CurrentEntry->TimeStamp)) < 0)) {
1095 PrevEntry = CurrentEntry;
1096 CurrentEntry = CurrentEntry->NextEntry;
1097 } // while
1098 NewEntry->NextEntry = CurrentEntry;
1099 if (PrevEntry == NULL) {
1100 LatestEntry = NewEntry;
1101 } else {
1102 PrevEntry->NextEntry = NewEntry;
1103 } // if/else
1104 } // if/else
1105 return (LatestEntry);
1106 } // static VOID AddLoaderListEntry()
1107
1108 // Delete the LOADER_LIST linked list
1109 static VOID CleanUpLoaderList(struct LOADER_LIST *LoaderList) {
1110 struct LOADER_LIST *Temp;
1111
1112 while (LoaderList != NULL) {
1113 Temp = LoaderList;
1114 LoaderList = LoaderList->NextEntry;
1115 MyFreePool(Temp->FileName);
1116 MyFreePool(Temp);
1117 } // while
1118 } // static VOID CleanUpLoaderList()
1119
1120 // Returns FALSE if the specified file/volume matches the GlobalConfig.DontScanDirs
1121 // or GlobalConfig.DontScanVolumes specification, or if Path points to a volume
1122 // other than the one specified by Volume, or if the specified path is SelfDir.
1123 // Returns TRUE if none of these conditions is met -- that is, if the path is
1124 // eligible for scanning.
1125 static BOOLEAN ShouldScan(REFIT_VOLUME *Volume, CHAR16 *Path) {
1126 CHAR16 *VolName = NULL, *DontScanDir, *PathCopy = NULL;
1127 UINTN i = 0;
1128 BOOLEAN ScanIt = TRUE;
1129
1130 if ((IsIn(Volume->VolName, GlobalConfig.DontScanVolumes)) || (IsIn(Volume->PartName, GlobalConfig.DontScanVolumes)))
1131 return FALSE;
1132
1133 if ((StriCmp(Path, SelfDirPath) == 0) && (Volume->DeviceHandle == SelfVolume->DeviceHandle))
1134 return FALSE;
1135
1136 // See if Path includes an explicit volume declaration that's NOT Volume....
1137 PathCopy = StrDuplicate(Path);
1138 if (SplitVolumeAndFilename(&PathCopy, &VolName)) {
1139 VolumeNumberToName(Volume, &VolName);
1140 if (VolName && StriCmp(VolName, Volume->VolName) != 0) {
1141 ScanIt = FALSE;
1142 } // if
1143 } // if Path includes volume specification
1144 MyFreePool(PathCopy);
1145 MyFreePool(VolName);
1146 VolName = NULL;
1147
1148 // See if Volume is in GlobalConfig.DontScanDirs....
1149 while (ScanIt && (DontScanDir = FindCommaDelimited(GlobalConfig.DontScanDirs, i++))) {
1150 SplitVolumeAndFilename(&DontScanDir, &VolName);
1151 CleanUpPathNameSlashes(DontScanDir);
1152 VolumeNumberToName(Volume, &VolName);
1153 if (VolName != NULL) {
1154 if ((StriCmp(VolName, Volume->VolName) == 0) && (StriCmp(DontScanDir, Path) == 0))
1155 ScanIt = FALSE;
1156 } else {
1157 if (StriCmp(DontScanDir, Path) == 0)
1158 ScanIt = FALSE;
1159 }
1160 MyFreePool(DontScanDir);
1161 MyFreePool(VolName);
1162 DontScanDir = NULL;
1163 VolName = NULL;
1164 } // while()
1165
1166 return ScanIt;
1167 } // BOOLEAN ShouldScan()
1168
1169 // Returns TRUE if the file is byte-for-byte identical with the fallback file
1170 // on the volume AND if the file is not itself the fallback file; returns
1171 // FALSE if the file is not identical to the fallback file OR if the file
1172 // IS the fallback file. Intended for use in excluding the fallback boot
1173 // loader when it's a duplicate of another boot loader.
1174 static BOOLEAN DuplicatesFallback(IN REFIT_VOLUME *Volume, IN CHAR16 *FileName) {
1175 CHAR8 *FileContents, *FallbackContents;
1176 EFI_FILE_HANDLE FileHandle, FallbackHandle;
1177 EFI_FILE_INFO *FileInfo, *FallbackInfo;
1178 UINTN FileSize = 0, FallbackSize = 0;
1179 EFI_STATUS Status;
1180 BOOLEAN AreIdentical = FALSE;
1181
1182 if (!FileExists(Volume->RootDir, FileName) || !FileExists(Volume->RootDir, FALLBACK_FULLNAME))
1183 return FALSE;
1184
1185 CleanUpPathNameSlashes(FileName);
1186
1187 if (StriCmp(FileName, FALLBACK_FULLNAME) == 0)
1188 return FALSE; // identical filenames, so not a duplicate....
1189
1190 Status = refit_call5_wrapper(Volume->RootDir->Open, Volume->RootDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
1191 if (Status == EFI_SUCCESS) {
1192 FileInfo = LibFileInfo(FileHandle);
1193 FileSize = FileInfo->FileSize;
1194 } else {
1195 return FALSE;
1196 }
1197
1198 Status = refit_call5_wrapper(Volume->RootDir->Open, Volume->RootDir, &FallbackHandle, FALLBACK_FULLNAME, EFI_FILE_MODE_READ, 0);
1199 if (Status == EFI_SUCCESS) {
1200 FallbackInfo = LibFileInfo(FallbackHandle);
1201 FallbackSize = FallbackInfo->FileSize;
1202 } else {
1203 refit_call1_wrapper(FileHandle->Close, FileHandle);
1204 return FALSE;
1205 }
1206
1207 if (FallbackSize != FileSize) { // not same size, so can't be identical
1208 AreIdentical = FALSE;
1209 } else { // could be identical; do full check....
1210 FileContents = AllocatePool(FileSize);
1211 FallbackContents = AllocatePool(FallbackSize);
1212 if (FileContents && FallbackContents) {
1213 Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &FileSize, FileContents);
1214 if (Status == EFI_SUCCESS) {
1215 Status = refit_call3_wrapper(FallbackHandle->Read, FallbackHandle, &FallbackSize, FallbackContents);
1216 }
1217 if (Status == EFI_SUCCESS) {
1218 AreIdentical = (CompareMem(FileContents, FallbackContents, FileSize) == 0);
1219 } // if
1220 } // if
1221 MyFreePool(FileContents);
1222 MyFreePool(FallbackContents);
1223 } // if/else
1224
1225 // BUG ALERT: Some systems (e.g., DUET, some Macs with large displays) crash if the
1226 // following two calls are reversed. Go figure....
1227 refit_call1_wrapper(FileHandle->Close, FallbackHandle);
1228 refit_call1_wrapper(FileHandle->Close, FileHandle);
1229 return AreIdentical;
1230 } // BOOLEAN DuplicatesFallback()
1231
1232 // Returns FALSE if two measures of file size are identical for a single file,
1233 // TRUE if not or if the file can't be opened and the other measure is non-0.
1234 // Despite the function's name, this isn't really a direct test of symbolic
1235 // link status, since EFI doesn't officially support symlinks. It does seem
1236 // to be a reliable indicator, though. (OTOH, some disk errors might cause a
1237 // file to fail to open, which would return a false positive -- but as I use
1238 // this function to exclude symbolic links from the list of boot loaders,
1239 // that would be fine, since such boot loaders wouldn't work.)
1240 static BOOLEAN IsSymbolicLink(REFIT_VOLUME *Volume, CHAR16 *Path, EFI_FILE_INFO *DirEntry) {
1241 EFI_FILE_HANDLE FileHandle;
1242 EFI_FILE_INFO *FileInfo = NULL;
1243 EFI_STATUS Status;
1244 UINTN FileSize2 = 0;
1245 CHAR16 *FileName;
1246
1247 FileName = StrDuplicate(Path);
1248 MergeStrings(&FileName, DirEntry->FileName, L'\\');
1249 CleanUpPathNameSlashes(FileName);
1250
1251 Status = refit_call5_wrapper(Volume->RootDir->Open, Volume->RootDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
1252 if (Status == EFI_SUCCESS) {
1253 FileInfo = LibFileInfo(FileHandle);
1254 if (FileInfo != NULL)
1255 FileSize2 = FileInfo->FileSize;
1256 }
1257
1258 MyFreePool(FileName);
1259 MyFreePool(FileInfo);
1260
1261 return (DirEntry->FileSize != FileSize2);
1262 } // BOOLEAN IsSymbolicLink()
1263
1264 // Returns TRUE if a file with the same name as the original but with
1265 // ".efi.signed" is also present in the same directory. Ubuntu is using
1266 // this filename as a signed version of the original unsigned kernel, and
1267 // there's no point in cluttering the display with two kernels that will
1268 // behave identically on non-SB systems, or when one will fail when SB
1269 // is active.
1270 static BOOLEAN HasSignedCounterpart(IN REFIT_VOLUME *Volume, IN CHAR16 *Path, IN CHAR16 *Filename) {
1271 CHAR16 *NewFile = NULL;
1272 BOOLEAN retval = FALSE;
1273
1274 MergeStrings(&NewFile, Path, 0);
1275 MergeStrings(&NewFile, Filename, L'\\');
1276 MergeStrings(&NewFile, L".efi.signed", 0);
1277 if (NewFile != NULL) {
1278 CleanUpPathNameSlashes(NewFile);
1279 if (FileExists(Volume->RootDir, NewFile))
1280 retval = TRUE;
1281 MyFreePool(NewFile);
1282 } // if
1283
1284 return retval;
1285 } // BOOLEAN HasSignedCounterpart()
1286
1287 // Scan an individual directory for EFI boot loader files and, if found,
1288 // add them to the list. Exception: Ignores FALLBACK_FULLNAME, which is picked
1289 // up in ScanEfiFiles(). Sorts the entries within the loader directory so that
1290 // the most recent one appears first in the list.
1291 // Returns TRUE if a duplicate for FALLBACK_FILENAME was found, FALSE if not.
1292 static BOOLEAN ScanLoaderDir(IN REFIT_VOLUME *Volume, IN CHAR16 *Path, IN CHAR16 *Pattern)
1293 {
1294 EFI_STATUS Status;
1295 REFIT_DIR_ITER DirIter;
1296 EFI_FILE_INFO *DirEntry;
1297 CHAR16 FileName[256], *Extension;
1298 struct LOADER_LIST *LoaderList = NULL, *NewLoader;
1299 BOOLEAN FoundFallbackDuplicate = FALSE;
1300
1301 if ((!SelfDirPath || !Path || ((StriCmp(Path, SelfDirPath) == 0) && (Volume->DeviceHandle != SelfVolume->DeviceHandle)) ||
1302 (StriCmp(Path, SelfDirPath) != 0)) && (ShouldScan(Volume, Path))) {
1303 // look through contents of the directory
1304 DirIterOpen(Volume->RootDir, Path, &DirIter);
1305 while (DirIterNext(&DirIter, 2, Pattern, &DirEntry)) {
1306 Extension = FindExtension(DirEntry->FileName);
1307 if (DirEntry->FileName[0] == '.' ||
1308 StriCmp(Extension, L".icns") == 0 ||
1309 StriCmp(Extension, L".png") == 0 ||
1310 (StriCmp(DirEntry->FileName, FALLBACK_BASENAME) == 0 && (StriCmp(Path, L"EFI\\BOOT") == 0)) ||
1311 StriSubCmp(L"shell", DirEntry->FileName) ||
1312 IsSymbolicLink(Volume, Path, DirEntry) || /* is symbolic link */
1313 HasSignedCounterpart(Volume, Path, DirEntry->FileName) || /* a file with same name plus ".efi.signed" is present */
1314 FilenameIn(Volume, Path, DirEntry->FileName, GlobalConfig.DontScanFiles))
1315 continue; // skip this
1316
1317 if (Path)
1318 SPrint(FileName, 255, L"\\%s\\%s", Path, DirEntry->FileName);
1319 else
1320 SPrint(FileName, 255, L"\\%s", DirEntry->FileName);
1321 CleanUpPathNameSlashes(FileName);
1322
1323 if(!IsValidLoader(Volume->RootDir, FileName))
1324 continue;
1325
1326 NewLoader = AllocateZeroPool(sizeof(struct LOADER_LIST));
1327 if (NewLoader != NULL) {
1328 NewLoader->FileName = StrDuplicate(FileName);
1329 NewLoader->TimeStamp = DirEntry->ModificationTime;
1330 LoaderList = AddLoaderListEntry(LoaderList, NewLoader);
1331 if (DuplicatesFallback(Volume, FileName))
1332 FoundFallbackDuplicate = TRUE;
1333 } // if
1334 MyFreePool(Extension);
1335 } // while
1336
1337 NewLoader = LoaderList;
1338 while (NewLoader != NULL) {
1339 AddLoaderEntry(NewLoader->FileName, NULL, Volume);
1340 NewLoader = NewLoader->NextEntry;
1341 } // while
1342
1343 CleanUpLoaderList(LoaderList);
1344 Status = DirIterClose(&DirIter);
1345 // NOTE: EFI_INVALID_PARAMETER really is an error that should be reported;
1346 // but I've gotten reports from users who are getting this error occasionally
1347 // and I can't find anything wrong or reproduce the problem, so I'm putting
1348 // it down to buggy EFI implementations and ignoring that particular error....
1349 if ((Status != EFI_NOT_FOUND) && (Status != EFI_INVALID_PARAMETER)) {
1350 if (Path)
1351 SPrint(FileName, 255, L"while scanning the %s directory", Path);
1352 else
1353 StrCpy(FileName, L"while scanning the root directory");
1354 CheckError(Status, FileName);
1355 } // if (Status != EFI_NOT_FOUND)
1356 } // if not scanning a blacklisted directory
1357
1358 return FoundFallbackDuplicate;
1359 } /* static VOID ScanLoaderDir() */
1360
1361 // Run the IPXE_DISCOVER_NAME program, which obtains the IP address of the boot
1362 // server and the name of the boot file it delivers.
1363 CHAR16* RuniPXEDiscover(EFI_HANDLE Volume)
1364 {
1365 EFI_STATUS Status;
1366 EFI_DEVICE_PATH *FilePath;
1367 EFI_HANDLE iPXEHandle;
1368 CHAR16 *boot_info = NULL;
1369 UINTN boot_info_size = 0;
1370
1371 FilePath = FileDevicePath (Volume, IPXE_DISCOVER_NAME);
1372 Status = refit_call6_wrapper(BS->LoadImage, FALSE, SelfImageHandle, FilePath,
1373 NULL, 0, &iPXEHandle);
1374 if (Status != 0)
1375 return NULL;
1376
1377 Status = refit_call3_wrapper(BS->StartImage, iPXEHandle, &boot_info_size, &boot_info);
1378
1379 return boot_info;
1380 } // RuniPXEDiscover()
1381
1382 // Scan for network (PXE) boot servers. This function relies on the presence
1383 // of the IPXE_DISCOVER_NAME and IPXE_NAME program files on the volume from
1384 // which rEFInd launched. As of December 6, 2014, these tools aren't entirely
1385 // reliable. See BUILDING.txt for information on building them.
1386 static VOID ScanNetboot() {
1387 CHAR16 *iPXEFileName = IPXE_NAME;
1388 CHAR16 *Location;
1389 REFIT_VOLUME *NetVolume;
1390
1391 if (FileExists(SelfVolume->RootDir, IPXE_DISCOVER_NAME) &&
1392 FileExists(SelfVolume->RootDir, IPXE_NAME) &&
1393 IsValidLoader(SelfVolume->RootDir, IPXE_DISCOVER_NAME) &&
1394 IsValidLoader(SelfVolume->RootDir, IPXE_NAME)) {
1395 Location = RuniPXEDiscover(SelfVolume->DeviceHandle);
1396 if (Location != NULL && FileExists(SelfVolume->RootDir, iPXEFileName)) {
1397 NetVolume = AllocatePool(sizeof(REFIT_VOLUME));
1398 CopyMem(NetVolume, SelfVolume, sizeof(REFIT_VOLUME));
1399 NetVolume->DiskKind = DISK_KIND_NET;
1400 NetVolume->VolBadgeImage = BuiltinIcon(BUILTIN_ICON_VOL_NET);
1401 NetVolume->PartName = NetVolume->VolName = NULL;
1402 AddLoaderEntry(iPXEFileName, Location, NetVolume);
1403 MyFreePool(NetVolume);
1404 } // if support files exist and are valid
1405 }
1406 } // VOID ScanNetBoot()
1407
1408 static VOID ScanEfiFiles(REFIT_VOLUME *Volume) {
1409 EFI_STATUS Status;
1410 REFIT_DIR_ITER EfiDirIter;
1411 EFI_FILE_INFO *EfiDirEntry;
1412 CHAR16 FileName[256], *Directory = NULL, *MatchPatterns, *VolName = NULL, *SelfPath;
1413 UINTN i, Length;
1414 BOOLEAN ScanFallbackLoader = TRUE;
1415 BOOLEAN FoundBRBackup = FALSE;
1416
1417 if ((Volume->RootDir != NULL) && (Volume->VolName != NULL) && (Volume->IsReadable)) {
1418 MatchPatterns = StrDuplicate(LOADER_MATCH_PATTERNS);
1419 if (GlobalConfig.ScanAllLinux)
1420 MergeStrings(&MatchPatterns, LINUX_MATCH_PATTERNS, L',');
1421
1422 // check for Mac OS X boot loader
1423 if (ShouldScan(Volume, MACOSX_LOADER_DIR)) {
1424 StrCpy(FileName, MACOSX_LOADER_PATH);
1425 if (FileExists(Volume->RootDir, FileName) && !FilenameIn(Volume, MACOSX_LOADER_DIR, L"boot.efi", GlobalConfig.DontScanFiles)) {
1426 AddLoaderEntry(FileName, L"Mac OS X", Volume);
1427 if (DuplicatesFallback(Volume, FileName))
1428 ScanFallbackLoader = FALSE;
1429 }
1430
1431 // check for XOM
1432 StrCpy(FileName, L"System\\Library\\CoreServices\\xom.efi");
1433 if (FileExists(Volume->RootDir, FileName) && !FilenameIn(Volume, MACOSX_LOADER_DIR, L"xom.efi", GlobalConfig.DontScanFiles)) {
1434 AddLoaderEntry(FileName, L"Windows XP (XoM)", Volume);
1435 if (DuplicatesFallback(Volume, FileName))
1436 ScanFallbackLoader = FALSE;
1437 }
1438 } // if should scan Mac directory
1439
1440 // check for Microsoft boot loader/menu
1441 if (ShouldScan(Volume, L"EFI\\Microsoft\\Boot")) {
1442 StrCpy(FileName, L"EFI\\Microsoft\\Boot\\bkpbootmgfw.efi");
1443 if (FileExists(Volume->RootDir, FileName) && !FilenameIn(Volume, L"EFI\\Microsoft\\Boot", L"bkpbootmgfw.efi",
1444 GlobalConfig.DontScanFiles)) {
1445 AddLoaderEntry(FileName, L"Microsoft EFI boot (Boot Repair backup)", Volume);
1446 FoundBRBackup = TRUE;
1447 if (DuplicatesFallback(Volume, FileName))
1448 ScanFallbackLoader = FALSE;
1449 }
1450 StrCpy(FileName, L"EFI\\Microsoft\\Boot\\bootmgfw.efi");
1451 if (FileExists(Volume->RootDir, FileName) && !FilenameIn(Volume, L"EFI\\Microsoft\\Boot", L"bootmgfw.efi", GlobalConfig.DontScanFiles)) {
1452 if (FoundBRBackup)
1453 AddLoaderEntry(FileName, L"Supposed Microsoft EFI boot (probably GRUB)", Volume);
1454 else
1455 AddLoaderEntry(FileName, L"Microsoft EFI boot", Volume);
1456 if (DuplicatesFallback(Volume, FileName))
1457 ScanFallbackLoader = FALSE;
1458 }
1459 } // if
1460
1461 // scan the root directory for EFI executables
1462 if (ScanLoaderDir(Volume, L"\\", MatchPatterns))
1463 ScanFallbackLoader = FALSE;
1464
1465 // scan subdirectories of the EFI directory (as per the standard)
1466 DirIterOpen(Volume->RootDir, L"EFI", &EfiDirIter);
1467 while (DirIterNext(&EfiDirIter, 1, NULL, &EfiDirEntry)) {
1468 if (StriCmp(EfiDirEntry->FileName, L"tools") == 0 || EfiDirEntry->FileName[0] == '.')
1469 continue; // skip this, doesn't contain boot loaders or is scanned later
1470 SPrint(FileName, 255, L"EFI\\%s", EfiDirEntry->FileName);
1471 if (ScanLoaderDir(Volume, FileName, MatchPatterns))
1472 ScanFallbackLoader = FALSE;
1473 } // while()
1474 Status = DirIterClose(&EfiDirIter);
1475 if (Status != EFI_NOT_FOUND)
1476 CheckError(Status, L"while scanning the EFI directory");
1477
1478 // Scan user-specified (or additional default) directories....
1479 i = 0;
1480 while ((Directory = FindCommaDelimited(GlobalConfig.AlsoScan, i++)) != NULL) {
1481 if (ShouldScan(Volume, Directory)) {
1482 SplitVolumeAndFilename(&Directory, &VolName);
1483 CleanUpPathNameSlashes(Directory);
1484 Length = StrLen(Directory);
1485 if ((Length > 0) && ScanLoaderDir(Volume, Directory, MatchPatterns))
1486 ScanFallbackLoader = FALSE;
1487 MyFreePool(VolName);
1488 } // if should scan dir
1489 MyFreePool(Directory);
1490 } // while
1491
1492 // Don't scan the fallback loader if it's on the same volume and a duplicate of rEFInd itself....
1493 SelfPath = DevicePathToStr(SelfLoadedImage->FilePath);
1494 CleanUpPathNameSlashes(SelfPath);
1495 if ((Volume->DeviceHandle == SelfLoadedImage->DeviceHandle) && DuplicatesFallback(Volume, SelfPath))
1496 ScanFallbackLoader = FALSE;
1497
1498 // If not a duplicate & if it exists & if it's not us, create an entry
1499 // for the fallback boot loader
1500 if (ScanFallbackLoader && FileExists(Volume->RootDir, FALLBACK_FULLNAME) && ShouldScan(Volume, L"EFI\\BOOT"))
1501 AddLoaderEntry(FALLBACK_FULLNAME, L"Fallback boot loader", Volume);
1502 } // if
1503 } // static VOID ScanEfiFiles()
1504
1505 // Scan internal disks for valid EFI boot loaders....
1506 static VOID ScanInternal(VOID) {
1507 UINTN VolumeIndex;
1508
1509 for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
1510 if (Volumes[VolumeIndex]->DiskKind == DISK_KIND_INTERNAL) {
1511 ScanEfiFiles(Volumes[VolumeIndex]);
1512 }
1513 } // for
1514 } // static VOID ScanInternal()
1515
1516 // Scan external disks for valid EFI boot loaders....
1517 static VOID ScanExternal(VOID) {
1518 UINTN VolumeIndex;
1519
1520 for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
1521 if (Volumes[VolumeIndex]->DiskKind == DISK_KIND_EXTERNAL) {
1522 ScanEfiFiles(Volumes[VolumeIndex]);
1523 }
1524 } // for
1525 } // static VOID ScanExternal()
1526
1527 // Scan internal disks for valid EFI boot loaders....
1528 static VOID ScanOptical(VOID) {
1529 UINTN VolumeIndex;
1530
1531 for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
1532 if (Volumes[VolumeIndex]->DiskKind == DISK_KIND_OPTICAL) {
1533 ScanEfiFiles(Volumes[VolumeIndex]);
1534 }
1535 } // for
1536 } // static VOID ScanOptical()
1537
1538 // default volume badge icon based on disk kind
1539 EG_IMAGE * GetDiskBadge(IN UINTN DiskType) {
1540 EG_IMAGE * Badge = NULL;
1541
1542 switch (DiskType) {
1543 case BBS_HARDDISK:
1544 Badge = BuiltinIcon(BUILTIN_ICON_VOL_INTERNAL);
1545 break;
1546 case BBS_USB:
1547 Badge = BuiltinIcon(BUILTIN_ICON_VOL_EXTERNAL);
1548 break;
1549 case BBS_CDROM:
1550 Badge = BuiltinIcon(BUILTIN_ICON_VOL_OPTICAL);
1551 break;
1552 } // switch()
1553 return Badge;
1554 } // EG_IMAGE * GetDiskBadge()
1555
1556 //
1557 // pre-boot tool functions
1558 //
1559
1560 static VOID StartTool(IN LOADER_ENTRY *Entry)
1561 {
1562 BeginExternalScreen(Entry->UseGraphicsMode, Entry->me.Title + 6); // assumes "Start <title>" as assigned below
1563 StoreLoaderName(Entry->me.Title);
1564 StartEFIImage(Entry->DevicePath, Entry->LoadOptions, TYPE_EFI,
1565 Basename(Entry->LoaderPath), Entry->OSType, NULL, TRUE, FALSE);
1566 FinishExternalScreen();
1567 } /* static VOID StartTool() */
1568
1569 static LOADER_ENTRY * AddToolEntry(EFI_HANDLE DeviceHandle, IN CHAR16 *LoaderPath, IN CHAR16 *LoaderTitle, IN EG_IMAGE *Image,
1570 IN CHAR16 ShortcutLetter, IN BOOLEAN UseGraphicsMode)
1571 {
1572 LOADER_ENTRY *Entry;
1573 CHAR16 *TitleStr = NULL;
1574
1575 Entry = AllocateZeroPool(sizeof(LOADER_ENTRY));
1576
1577 TitleStr = PoolPrint(L"Start %s", LoaderTitle);
1578 Entry->me.Title = TitleStr;
1579 Entry->me.Tag = TAG_TOOL;
1580 Entry->me.Row = 1;
1581 Entry->me.ShortcutLetter = ShortcutLetter;
1582 Entry->me.Image = Image;
1583 Entry->LoaderPath = (LoaderPath) ? StrDuplicate(LoaderPath) : NULL;
1584 Entry->DevicePath = FileDevicePath(DeviceHandle, Entry->LoaderPath);
1585 Entry->UseGraphicsMode = UseGraphicsMode;
1586
1587 AddMenuEntry(&MainMenu, (REFIT_MENU_ENTRY *)Entry);
1588 return Entry;
1589 } /* static LOADER_ENTRY * AddToolEntry() */
1590
1591 //
1592 // pre-boot driver functions
1593 //
1594
1595 static UINTN ScanDriverDir(IN CHAR16 *Path)
1596 {
1597 EFI_STATUS Status;
1598 REFIT_DIR_ITER DirIter;
1599 UINTN NumFound = 0;
1600 EFI_FILE_INFO *DirEntry;
1601 CHAR16 FileName[256];
1602
1603 CleanUpPathNameSlashes(Path);
1604 // look through contents of the directory
1605 DirIterOpen(SelfRootDir, Path, &DirIter);
1606 while (DirIterNext(&DirIter, 2, LOADER_MATCH_PATTERNS, &DirEntry)) {
1607 if (DirEntry->FileName[0] == '.')
1608 continue; // skip this
1609
1610 SPrint(FileName, 255, L"%s\\%s", Path, DirEntry->FileName);
1611 NumFound++;
1612 Status = StartEFIImage(FileDevicePath(SelfLoadedImage->DeviceHandle, FileName),
1613 L"", TYPE_EFI, DirEntry->FileName, 0, NULL, FALSE, TRUE);
1614 }
1615 Status = DirIterClose(&DirIter);
1616 if (Status != EFI_NOT_FOUND) {
1617 SPrint(FileName, 255, L"while scanning the %s directory", Path);
1618 CheckError(Status, FileName);
1619 }
1620 return (NumFound);
1621 }
1622
1623 #ifdef __MAKEWITH_GNUEFI
1624 static EFI_STATUS ConnectAllDriversToAllControllers(VOID)
1625 {
1626 EFI_STATUS Status;
1627 UINTN AllHandleCount;
1628 EFI_HANDLE *AllHandleBuffer;
1629 UINTN Index;
1630 UINTN HandleCount;
1631 EFI_HANDLE *HandleBuffer;
1632 UINT32 *HandleType;
1633 UINTN HandleIndex;
1634 BOOLEAN Parent;
1635 BOOLEAN Device;
1636
1637 Status = LibLocateHandle(AllHandles,
1638 NULL,
1639 NULL,
1640 &AllHandleCount,
1641 &AllHandleBuffer);
1642 if (EFI_ERROR(Status))
1643 return Status;
1644
1645 for (Index = 0; Index < AllHandleCount; Index++) {
1646 //
1647 // Scan the handle database
1648 //
1649 Status = LibScanHandleDatabase(NULL,
1650 NULL,
1651 AllHandleBuffer[Index],
1652 NULL,
1653 &HandleCount,
1654 &HandleBuffer,
1655 &HandleType);
1656 if (EFI_ERROR (Status))
1657 goto Done;
1658
1659 Device = TRUE;
1660 if (HandleType[Index] & EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE)
1661 Device = FALSE;
1662 if (HandleType[Index] & EFI_HANDLE_TYPE_IMAGE_HANDLE)
1663 Device = FALSE;
1664
1665 if (Device) {
1666 Parent = FALSE;
1667 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
1668 if (HandleType[HandleIndex] & EFI_HANDLE_TYPE_PARENT_HANDLE)
1669 Parent = TRUE;
1670 } // for
1671
1672 if (!Parent) {
1673 if (HandleType[Index] & EFI_HANDLE_TYPE_DEVICE_HANDLE) {
1674 Status = refit_call4_wrapper(BS->ConnectController,
1675 AllHandleBuffer[Index],
1676 NULL,
1677 NULL,
1678 TRUE);
1679 }
1680 }
1681 }
1682
1683 MyFreePool (HandleBuffer);
1684 MyFreePool (HandleType);
1685 }
1686
1687 Done:
1688 MyFreePool (AllHandleBuffer);
1689 return Status;
1690 } /* EFI_STATUS ConnectAllDriversToAllControllers() */
1691 #else
1692 static EFI_STATUS ConnectAllDriversToAllControllers(VOID) {
1693 BdsLibConnectAllDriversToAllControllers();
1694 return 0;
1695 }
1696 #endif
1697
1698 // Load all EFI drivers from rEFInd's "drivers" subdirectory and from the
1699 // directories specified by the user in the "scan_driver_dirs" configuration
1700 // file line.
1701 static VOID LoadDrivers(VOID)
1702 {
1703 CHAR16 *Directory, *SelfDirectory;
1704 UINTN i = 0, Length, NumFound = 0;
1705
1706 // load drivers from the subdirectories of rEFInd's home directory specified
1707 // in the DRIVER_DIRS constant.
1708 while ((Directory = FindCommaDelimited(DRIVER_DIRS, i++)) != NULL) {
1709 SelfDirectory = SelfDirPath ? StrDuplicate(SelfDirPath) : NULL;
1710 CleanUpPathNameSlashes(SelfDirectory);
1711 MergeStrings(&SelfDirectory, Directory, L'\\');
1712 NumFound += ScanDriverDir(SelfDirectory);
1713 MyFreePool(Directory);
1714 MyFreePool(SelfDirectory);
1715 }
1716
1717 // Scan additional user-specified driver directories....
1718 i = 0;
1719 while ((Directory = FindCommaDelimited(GlobalConfig.DriverDirs, i++)) != NULL) {
1720 CleanUpPathNameSlashes(Directory);
1721 Length = StrLen(Directory);
1722 if (Length > 0) {
1723 NumFound += ScanDriverDir(Directory);
1724 } // if
1725 MyFreePool(Directory);
1726 } // while
1727
1728 // connect all devices
1729 if (NumFound > 0)
1730 ConnectAllDriversToAllControllers();
1731 } /* static VOID LoadDrivers() */
1732
1733 // Locates boot loaders. NOTE: This assumes that GlobalConfig.LegacyType is set correctly.
1734 static VOID ScanForBootloaders(VOID) {
1735 UINTN i;
1736 CHAR8 s;
1737 BOOLEAN ScanForLegacy = FALSE;
1738
1739 // Determine up-front if we'll be scanning for legacy loaders....
1740 for (i = 0; i < NUM_SCAN_OPTIONS; i++) {
1741 s = GlobalConfig.ScanFor[i];
1742 if ((s == 'c') || (s == 'C') || (s == 'h') || (s == 'H') || (s == 'b') || (s == 'B'))
1743 ScanForLegacy = TRUE;
1744 } // for
1745
1746 // If UEFI & scanning for legacy loaders & deep legacy scan, update NVRAM boot manager list
1747 if ((GlobalConfig.LegacyType == LEGACY_TYPE_UEFI) && ScanForLegacy && GlobalConfig.DeepLegacyScan) {
1748 BdsDeleteAllInvalidLegacyBootOptions();
1749 BdsAddNonExistingLegacyBootOptions();
1750 } // if
1751
1752 // scan for loaders and tools, add them to the menu
1753 for (i = 0; i < NUM_SCAN_OPTIONS; i++) {
1754 switch(GlobalConfig.ScanFor[i]) {
1755 case 'c': case 'C':
1756 ScanLegacyDisc();
1757 break;
1758 case 'h': case 'H':
1759 ScanLegacyInternal();
1760 break;
1761 case 'b': case 'B':
1762 ScanLegacyExternal();
1763 break;
1764 case 'm': case 'M':
1765 ScanUserConfigured(GlobalConfig.ConfigFilename);
1766 break;
1767 case 'e': case 'E':
1768 ScanExternal();
1769 break;
1770 case 'i': case 'I':
1771 ScanInternal();
1772 break;
1773 case 'o': case 'O':
1774 ScanOptical();
1775 break;
1776 case 'n': case 'N':
1777 ScanNetboot();
1778 break;
1779 } // switch()
1780 } // for
1781
1782 // assign shortcut keys
1783 for (i = 0; i < MainMenu.EntryCount && MainMenu.Entries[i]->Row == 0 && i < 9; i++)
1784 MainMenu.Entries[i]->ShortcutDigit = (CHAR16)('1' + i);
1785
1786 // wait for user ACK when there were errors
1787 FinishTextScreen(FALSE);
1788 } // static VOID ScanForBootloaders()
1789
1790 // Locate a single tool from the specified Locations using one of the
1791 // specified Names and add it to the menu.
1792 static VOID FindTool(CHAR16 *Locations, CHAR16 *Names, CHAR16 *Description, UINTN Icon) {
1793 UINTN j = 0, k, VolumeIndex;
1794 CHAR16 *DirName, *FileName, *PathName, FullDescription[256];
1795
1796 while ((DirName = FindCommaDelimited(Locations, j++)) != NULL) {
1797 k = 0;
1798 while ((FileName = FindCommaDelimited(Names, k++)) != NULL) {
1799 PathName = StrDuplicate(DirName);
1800 MergeStrings(&PathName, FileName, (StriCmp(PathName, L"\\") == 0) ? 0 : L'\\');
1801 for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
1802 if ((Volumes[VolumeIndex]->RootDir != NULL) && (FileExists(Volumes[VolumeIndex]->RootDir, PathName)) &&
1803 IsValidLoader(Volumes[VolumeIndex]->RootDir, PathName)) {
1804 SPrint(FullDescription, 255, L"%s at %s on %s", Description, PathName, Volumes[VolumeIndex]->VolName);
1805 AddToolEntry(Volumes[VolumeIndex]->DeviceHandle, PathName, FullDescription, BuiltinIcon(Icon), 'S', FALSE);
1806 } // if
1807 } // for
1808 MyFreePool(PathName);
1809 MyFreePool(FileName);
1810 } // while Names
1811 MyFreePool(DirName);
1812 } // while Locations
1813 } // VOID FindTool()
1814
1815 // Add the second-row tags containing built-in and external tools (EFI shell,
1816 // reboot, etc.)
1817 static VOID ScanForTools(VOID) {
1818 CHAR16 *FileName = NULL, *VolName = NULL, *MokLocations, Description[256];
1819 REFIT_MENU_ENTRY *TempMenuEntry;
1820 UINTN i, j, VolumeIndex;
1821 UINT64 osind;
1822 CHAR8 *b = 0;
1823
1824 MokLocations = StrDuplicate(MOK_LOCATIONS);
1825 if (MokLocations != NULL)
1826 MergeStrings(&MokLocations, SelfDirPath, L',');
1827
1828 for (i = 0; i < NUM_TOOLS; i++) {
1829 switch(GlobalConfig.ShowTools[i]) {
1830 // NOTE: Be sure that FileName is NULL at the end of each case.
1831 case TAG_SHUTDOWN:
1832 TempMenuEntry = CopyMenuEntry(&MenuEntryShutdown);
1833 TempMenuEntry->Image = BuiltinIcon(BUILTIN_ICON_FUNC_SHUTDOWN);
1834 AddMenuEntry(&MainMenu, TempMenuEntry);
1835 break;
1836
1837 case TAG_REBOOT:
1838 TempMenuEntry = CopyMenuEntry(&MenuEntryReset);
1839 TempMenuEntry->Image = BuiltinIcon(BUILTIN_ICON_FUNC_RESET);
1840 AddMenuEntry(&MainMenu, TempMenuEntry);
1841 break;
1842
1843 case TAG_ABOUT:
1844 TempMenuEntry = CopyMenuEntry(&MenuEntryAbout);
1845 TempMenuEntry->Image = BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT);
1846 AddMenuEntry(&MainMenu, TempMenuEntry);
1847 break;
1848
1849 case TAG_EXIT:
1850 TempMenuEntry = CopyMenuEntry(&MenuEntryExit);
1851 TempMenuEntry->Image = BuiltinIcon(BUILTIN_ICON_FUNC_EXIT);
1852 AddMenuEntry(&MainMenu, TempMenuEntry);
1853 break;
1854
1855 case TAG_FIRMWARE:
1856 if (EfivarGetRaw(&GlobalGuid, L"OsIndicationsSupported", &b, &j) == EFI_SUCCESS) {
1857 osind = (UINT64)*b;
1858 if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) {
1859 TempMenuEntry = CopyMenuEntry(&MenuEntryFirmware);
1860 TempMenuEntry->Image = BuiltinIcon(BUILTIN_ICON_FUNC_FIRMWARE);
1861 AddMenuEntry(&MainMenu, TempMenuEntry);
1862 } // if
1863 } // if
1864 break;
1865
1866 case TAG_SHELL:
1867 j = 0;
1868 while ((FileName = FindCommaDelimited(SHELL_NAMES, j++)) != NULL) {
1869 if (FileExists(SelfRootDir, FileName) && IsValidLoader(SelfRootDir, FileName)) {
1870 AddToolEntry(SelfLoadedImage->DeviceHandle, FileName, L"EFI Shell", BuiltinIcon(BUILTIN_ICON_TOOL_SHELL),
1871 'S', FALSE);
1872 }
1873 MyFreePool(FileName);
1874 } // while
1875 break;
1876
1877 case TAG_GPTSYNC:
1878 j = 0;
1879 while ((FileName = FindCommaDelimited(GPTSYNC_NAMES, j++)) != NULL) {
1880 if (FileExists(SelfRootDir, FileName) && IsValidLoader(SelfRootDir, FileName)) {
1881 AddToolEntry(SelfLoadedImage->DeviceHandle, FileName, L"Hybrid MBR tool", BuiltinIcon(BUILTIN_ICON_TOOL_PART),
1882 'P', FALSE);
1883 } // if
1884 MyFreePool(FileName);
1885 } // while
1886 FileName = NULL;
1887 break;
1888
1889 case TAG_GDISK:
1890 j = 0;
1891 while ((FileName = FindCommaDelimited(GDISK_NAMES, j++)) != NULL) {
1892 if (FileExists(SelfRootDir, FileName) && IsValidLoader(SelfRootDir, FileName)) {
1893 AddToolEntry(SelfLoadedImage->DeviceHandle, FileName, L"disk partitioning tool",
1894 BuiltinIcon(BUILTIN_ICON_TOOL_PART), 'G', FALSE);
1895 } // if
1896 MyFreePool(FileName);
1897 } // while
1898 FileName = NULL;
1899 break;
1900
1901 case TAG_NETBOOT:
1902 j = 0;
1903 while ((FileName = FindCommaDelimited(NETBOOT_NAMES, j++)) != NULL) {
1904 if (FileExists(SelfRootDir, FileName) && IsValidLoader(SelfRootDir, FileName)) {
1905 AddToolEntry(SelfLoadedImage->DeviceHandle, FileName, L"Netboot",
1906 BuiltinIcon(BUILTIN_ICON_TOOL_NETBOOT), 'N', FALSE);
1907 } // if
1908 MyFreePool(FileName);
1909 } // while
1910 FileName = NULL;
1911 break;
1912
1913 case TAG_APPLE_RECOVERY:
1914 FileName = StrDuplicate(L"\\com.apple.recovery.boot\\boot.efi");
1915 for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
1916 if ((Volumes[VolumeIndex]->RootDir != NULL) && (FileExists(Volumes[VolumeIndex]->RootDir, FileName)) &&
1917 IsValidLoader(Volumes[VolumeIndex]->RootDir, FileName)) {
1918 SPrint(Description, 255, L"Apple Recovery on %s", Volumes[VolumeIndex]->VolName);
1919 AddToolEntry(Volumes[VolumeIndex]->DeviceHandle, FileName, Description,
1920 BuiltinIcon(BUILTIN_ICON_TOOL_APPLE_RESCUE), 'R', TRUE);
1921 } // if
1922 } // for
1923 MyFreePool(FileName);
1924 FileName = NULL;
1925 break;
1926
1927 case TAG_WINDOWS_RECOVERY:
1928 j = 0;
1929 while ((FileName = FindCommaDelimited(GlobalConfig.WindowsRecoveryFiles, j++)) != NULL) {
1930 SplitVolumeAndFilename(&FileName, &VolName);
1931 for (VolumeIndex = 0; VolumeIndex < VolumesCount; VolumeIndex++) {
1932 if ((Volumes[VolumeIndex]->RootDir != NULL) && (FileExists(Volumes[VolumeIndex]->RootDir, FileName)) &&
1933 IsValidLoader(Volumes[VolumeIndex]->RootDir, FileName) &&
1934 ((VolName == NULL) || (StriCmp(VolName, Volumes[VolumeIndex]->VolName) == 0))) {
1935 SPrint(Description, 255, L"Microsoft Recovery on %s", Volumes[VolumeIndex]->VolName);
1936 AddToolEntry(Volumes[VolumeIndex]->DeviceHandle, FileName, Description,
1937 BuiltinIcon(BUILTIN_ICON_TOOL_WINDOWS_RESCUE), 'R', TRUE);
1938 } // if
1939 } // for
1940 } // while
1941 MyFreePool(FileName);
1942 FileName = NULL;
1943 MyFreePool(VolName);
1944 VolName = NULL;
1945 break;
1946
1947 case TAG_MOK_TOOL:
1948 FindTool(MokLocations, MOK_NAMES, L"MOK utility", BUILTIN_ICON_TOOL_MOK_TOOL);
1949 break;
1950
1951 case TAG_MEMTEST:
1952 FindTool(MEMTEST_LOCATIONS, MEMTEST_NAMES, L"Memory test utility", BUILTIN_ICON_TOOL_MEMTEST);
1953 break;
1954
1955 } // switch()
1956 } // for
1957 } // static VOID ScanForTools
1958
1959 // Rescan for boot loaders
1960 static VOID RescanAll(BOOLEAN DisplayMessage) {
1961 EG_PIXEL BGColor;
1962
1963 BGColor.b = 255;
1964 BGColor.g = 175;
1965 BGColor.r = 100;
1966 BGColor.a = 0;
1967 if (DisplayMessage)
1968 egDisplayMessage(L"Scanning for new boot loaders; please wait....", &BGColor);
1969 FreeList((VOID ***) &(MainMenu.Entries), &MainMenu.EntryCount);
1970 MainMenu.Entries = NULL;
1971 MainMenu.EntryCount = 0;
1972 ReadConfig(GlobalConfig.ConfigFilename);
1973 ConnectAllDriversToAllControllers();
1974 ScanVolumes();
1975 ScanForBootloaders();
1976 ScanForTools();
1977 SetupScreen();
1978 } // VOID RescanAll()
1979
1980 #ifdef __MAKEWITH_TIANO
1981
1982 // Minimal initialization function
1983 static VOID InitializeLib(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable) {
1984 gST = SystemTable;
1985 // gImageHandle = ImageHandle;
1986 gBS = SystemTable->BootServices;
1987 // gRS = SystemTable->RuntimeServices;
1988 gRT = SystemTable->RuntimeServices; // Some BDS functions need gRT to be set
1989 EfiGetSystemConfigurationTable (&gEfiDxeServicesTableGuid, (VOID **) &gDS);
1990
1991 // InitializeConsoleSim();
1992 }
1993
1994 #endif
1995
1996 // Set up our own Secure Boot extensions....
1997 // Returns TRUE on success, FALSE otherwise
1998 static BOOLEAN SecureBootSetup(VOID) {
1999 EFI_STATUS Status;
2000 BOOLEAN Success = FALSE;
2001
2002 if (secure_mode() && ShimLoaded()) {
2003 Status = security_policy_install();
2004 if (Status == EFI_SUCCESS) {
2005 Success = TRUE;
2006 } else {
2007 Print(L"Failed to install MOK Secure Boot extensions");
2008 }
2009 }
2010 return Success;
2011 } // VOID SecureBootSetup()
2012
2013 // Remove our own Secure Boot extensions....
2014 // Returns TRUE on success, FALSE otherwise
2015 static BOOLEAN SecureBootUninstall(VOID) {
2016 EFI_STATUS Status;
2017 BOOLEAN Success = TRUE;
2018
2019 if (secure_mode()) {
2020 Status = security_policy_uninstall();
2021 if (Status != EFI_SUCCESS) {
2022 Success = FALSE;
2023 BeginTextScreen(L"Secure Boot Policy Failure");
2024 Print(L"Failed to uninstall MOK Secure Boot extensions; forcing a reboot.");
2025 PauseForKey();
2026 refit_call4_wrapper(RT->ResetSystem, EfiResetCold, EFI_SUCCESS, 0, NULL);
2027 }
2028 }
2029 return Success;
2030 } // VOID SecureBootUninstall
2031
2032 // Sets the global configuration filename; will be CONFIG_FILE_NAME unless the
2033 // "-c" command-line option is set, in which case that takes precedence.
2034 // If an error is encountered, leaves the value alone (it should be set to
2035 // CONFIG_FILE_NAME when GlobalConfig is initialized).
2036 static VOID SetConfigFilename(EFI_HANDLE ImageHandle) {
2037 EFI_LOADED_IMAGE *Info;
2038 CHAR16 *Options, *FileName;
2039 EFI_STATUS Status;
2040 INTN Where;
2041
2042 Status = refit_call3_wrapper(BS->HandleProtocol, ImageHandle, &LoadedImageProtocol, (VOID **) &Info);
2043 if ((Status == EFI_SUCCESS) && (Info->LoadOptionsSize > 0)) {
2044 Options = (CHAR16 *) Info->LoadOptions;
2045 Where = FindSubString(L" -c ", Options);
2046 if (Where >= 0) {
2047 FileName = StrDuplicate(&Options[Where + 4]);
2048 Where = FindSubString(L" ", FileName);
2049 if (Where > 0)
2050 FileName[Where] = L'\0';
2051
2052 if (FileExists(SelfDir, FileName)) {
2053 GlobalConfig.ConfigFilename = FileName;
2054 } else {
2055 Print(L"Specified configuration file (%s) doesn't exist; using\n'refind.conf' default\n", FileName);
2056 MyFreePool(FileName);
2057 } // if/else
2058 } // if
2059 } // if
2060 } // VOID SetConfigFilename()
2061
2062 //
2063 // main entry point
2064 //
2065 EFI_STATUS
2066 EFIAPI
2067 efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
2068 {
2069 EFI_STATUS Status;
2070 BOOLEAN MainLoopRunning = TRUE;
2071 BOOLEAN MokProtocol;
2072 REFIT_MENU_ENTRY *ChosenEntry;
2073 UINTN MenuExit, i;
2074 CHAR16 *SelectionName = NULL;
2075 EG_PIXEL BGColor;
2076
2077 // bootstrap
2078 InitializeLib(ImageHandle, SystemTable);
2079 Status = InitRefitLib(ImageHandle);
2080 if (EFI_ERROR(Status))
2081 return Status;
2082
2083 // read configuration
2084 CopyMem(GlobalConfig.ScanFor, "ieom ", NUM_SCAN_OPTIONS);
2085 FindLegacyBootType();
2086 if (GlobalConfig.LegacyType == LEGACY_TYPE_MAC)
2087 CopyMem(GlobalConfig.ScanFor, "ihebocm ", NUM_SCAN_OPTIONS);
2088 SetConfigFilename(ImageHandle);
2089 ReadConfig(GlobalConfig.ConfigFilename);
2090
2091 InitScreen();
2092 WarnIfLegacyProblems();
2093 MainMenu.TimeoutSeconds = GlobalConfig.Timeout;
2094
2095 // disable EFI watchdog timer
2096 refit_call4_wrapper(BS->SetWatchdogTimer, 0x0000, 0x0000, 0x0000, NULL);
2097
2098 // further bootstrap (now with config available)
2099 MokProtocol = SecureBootSetup();
2100 LoadDrivers();
2101 ScanVolumes();
2102 ScanForBootloaders();
2103 ScanForTools();
2104 SetupScreen();
2105
2106 if (GlobalConfig.ScanDelay > 0) {
2107 BGColor.b = 255;
2108 BGColor.g = 175;
2109 BGColor.r = 100;
2110 BGColor.a = 0;
2111 if (GlobalConfig.ScanDelay > 1)
2112 egDisplayMessage(L"Pausing before disk scan; please wait....", &BGColor);
2113 for (i = 0; i < GlobalConfig.ScanDelay; i++)
2114 refit_call1_wrapper(BS->Stall, 1000000);
2115 RescanAll(GlobalConfig.ScanDelay > 1);
2116 } // if
2117
2118 if (GlobalConfig.DefaultSelection)
2119 SelectionName = StrDuplicate(GlobalConfig.DefaultSelection);
2120
2121 while (MainLoopRunning) {
2122 MenuExit = RunMainMenu(&MainMenu, &SelectionName, &ChosenEntry);
2123
2124 // The Escape key triggers a re-scan operation....
2125 if (MenuExit == MENU_EXIT_ESCAPE) {
2126 MenuExit = 0;
2127 RescanAll(TRUE);
2128 continue;
2129 }
2130
2131 switch (ChosenEntry->Tag) {
2132
2133 case TAG_REBOOT: // Reboot
2134 TerminateScreen();
2135 refit_call4_wrapper(RT->ResetSystem, EfiResetCold, EFI_SUCCESS, 0, NULL);
2136 MainLoopRunning = FALSE; // just in case we get this far
2137 break;
2138
2139 case TAG_SHUTDOWN: // Shut Down
2140 TerminateScreen();
2141 refit_call4_wrapper(RT->ResetSystem, EfiResetShutdown, EFI_SUCCESS, 0, NULL);
2142 MainLoopRunning = FALSE; // just in case we get this far
2143 break;
2144
2145 case TAG_ABOUT: // About rEFInd
2146 AboutrEFInd();
2147 break;
2148
2149 case TAG_LOADER: // Boot OS via .EFI loader
2150 StartLoader((LOADER_ENTRY *)ChosenEntry, SelectionName);
2151 break;
2152
2153 case TAG_LEGACY: // Boot legacy OS
2154 StartLegacy((LEGACY_ENTRY *)ChosenEntry, SelectionName);
2155 break;
2156
2157 case TAG_LEGACY_UEFI: // Boot a legacy OS on a non-Mac
2158 StartLegacyUEFI((LEGACY_ENTRY *)ChosenEntry, SelectionName);
2159 break;
2160
2161 case TAG_TOOL: // Start a EFI tool
2162 StartTool((LOADER_ENTRY *)ChosenEntry);
2163 break;
2164
2165 case TAG_EXIT: // Terminate rEFInd
2166 if ((MokProtocol) && !SecureBootUninstall()) {
2167 MainLoopRunning = FALSE; // just in case we get this far
2168 } else {
2169 BeginTextScreen(L" ");
2170 return EFI_SUCCESS;
2171 }
2172 break;
2173
2174 case TAG_FIRMWARE: // Reboot into firmware's user interface
2175 RebootIntoFirmware();
2176 break;
2177
2178 } // switch()
2179 } // while()
2180
2181 // If we end up here, things have gone wrong. Try to reboot, and if that
2182 // fails, go into an endless loop.
2183 refit_call4_wrapper(RT->ResetSystem, EfiResetCold, EFI_SUCCESS, 0, NULL);
2184 EndlessIdleLoop();
2185
2186 return EFI_SUCCESS;
2187 } /* efi_main() */