5 * Copyright (c) 2006-2009 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), a copy of which must be distributed
41 * with this source code or binaries made from it.
48 #ifdef __MAKEWITH_GNUEFI
52 #include "../include/tiano_includes.h"
54 #include "../EfiLib/GenericBdsLib.h"
58 #define REFIT_DEBUG (0)
60 // Tag classifications; used in various ways.
62 #define TAG_REBOOT (2)
63 #define TAG_SHUTDOWN (3)
65 #define TAG_LOADER (5)
66 #define TAG_LEGACY (6)
69 #define TAG_GPTSYNC (9)
70 #define TAG_LEGACY_UEFI (10)
71 #define TAG_APPLE_RECOVERY (11)
72 #define TAG_WINDOWS_RECOVERY (12)
73 #define TAG_MOK_TOOL (13)
74 #define TAG_FIRMWARE (14)
75 #define TAG_MEMTEST (15)
76 #define TAG_GDISK (16)
77 #define TAG_NETBOOT (17)
78 #define TAG_CSR_ROTATE (18)
79 #define TAG_FWUPDATE_TOOL (19)
80 #define NUM_TOOLS (20)
82 #define NUM_SCAN_OPTIONS 10
84 #define DEFAULT_ICONS_DIR L"icons"
86 // OS bit codes; used in GlobalConfig.GraphicsOn
87 #define GRAPHICS_FOR_OSX 1
88 #define GRAPHICS_FOR_LINUX 2
89 #define GRAPHICS_FOR_ELILO 4
90 #define GRAPHICS_FOR_GRUB 8
91 #define GRAPHICS_FOR_WINDOWS 16
97 // Type of legacy (BIOS) boot support detected
98 #define LEGACY_TYPE_NONE 0
99 #define LEGACY_TYPE_MAC 1
100 #define LEGACY_TYPE_UEFI 2
102 #ifdef __MAKEWITH_GNUEFI
104 // define BBS Device Types
106 #define BBS_FLOPPY 0x01
107 #define BBS_HARDDISK 0x02
108 #define BBS_CDROM 0x03
109 #define BBS_PCMCIA 0x04
111 #define BBS_EMBED_NETWORK 0x06
112 #define BBS_BEV_DEVICE 0x80
113 #define BBS_UNKNOWN 0xff
116 // BIOS Boot Specification (BBS) device types, as returned in DevicePath->Type field
117 #define DEVICE_TYPE_HW 0x01
118 #define DEVICE_TYPE_ACPI 0x02 /* returned by UEFI boot loader on USB */
119 #define DEVICE_TYPE_MESSAGING 0x03
120 #define DEVICE_TYPE_MEDIA 0x04 /* returned by EFI boot loaders on hard disk */
121 #define DEVICE_TYPE_BIOS 0x05 /* returned by legacy (BIOS) boot loaders */
122 #define DEVICE_TYPE_END 0x75 /* end of path */
124 // Filesystem type identifiers. Not all are yet used....
125 #define FS_TYPE_UNKNOWN 0
126 #define FS_TYPE_WHOLEDISK 1
127 #define FS_TYPE_FAT 2
128 #define FS_TYPE_EXFAT 3
129 #define FS_TYPE_NTFS 4
130 #define FS_TYPE_EXT2 5
131 #define FS_TYPE_EXT3 6
132 #define FS_TYPE_EXT4 7
133 #define FS_TYPE_HFSPLUS 8
134 #define FS_TYPE_REISERFS 9
135 #define FS_TYPE_BTRFS 10
136 #define FS_TYPE_XFS 11
137 #define FS_TYPE_ISO9660 12
139 // How to scale banner images
140 #define BANNER_NOSCALE 0
141 #define BANNER_FILLSCREEN 1
143 // Sizes of the default icons; badges are 1/4 the big icon size
144 #define DEFAULT_SMALL_ICON_SIZE 48
145 #define DEFAULT_BIG_ICON_SIZE 128
147 // Codes for types of icon sizes; used for indexing into GlobalConfig.IconSizes[]
148 #define ICON_SIZE_BADGE 0
149 #define ICON_SIZE_SMALL 1
150 #define ICON_SIZE_BIG 2
152 // Names of binaries that can manage MOKs....
153 #define MOK_NAMES L"MokManager.efi,HashTool.efi,HashTool-signed.efi,KeyTool.efi,KeyTool-signed.efi"
154 // Names of binaries that can update firmware....
156 #define FWUPDATE_NAMES L"fwupx64.efi"
158 #define FWUPDATE_NAMES L"fwupia32.efi"
159 #elif defined(EFIAARCH64)
160 #define FWUPDATE_NAMES L"fwupaa64.efi"
162 #define FWUPDATE_NAMES L"fwup.efi"
164 // Directories to search for these MOK-managing programs. Note that SelfDir is
165 // searched in addition to these locations....
166 #define MOK_LOCATIONS L"\\,EFI\\tools,EFI\\fedora,EFI\\redhat,EFI\\ubuntu,EFI\\suse,EFI\\opensuse,EFI\\altlinux"
167 // Directories to search for memtest86....
168 #define MEMTEST_LOCATIONS L"EFI\\tools,EFI\\tools\\memtest86,EFI\\tools\\memtest,EFI\\memtest86,EFI\\memtest"
169 // Files that may be Windows recovery files
170 #define WINDOWS_RECOVERY_FILES L"EFI\\Microsoft\\Boot\\LrsBootmgr.efi,Recovery:\\EFI\\BOOT\\bootx64.efi,Recovery:\\EFI\\BOOT\\bootia32.efi"
172 // Filename patterns that identify EFI boot loaders. Note that a single case (either L"*.efi" or
173 // L"*.EFI") is fine for most systems; but Gigabyte's buggy Hybrid EFI does a case-sensitive
174 // comparison when it should do a case-insensitive comparison, so I'm doubling this up. It does
175 // no harm on other computers, AFAIK. In theory, every case variation should be done for
176 // completeness, but that's ridiculous....
177 #define LOADER_MATCH_PATTERNS L"*.efi,*.EFI"
179 // Definitions for the "hideui" option in refind.conf
180 #define HIDEUI_FLAG_NONE (0x0000)
181 #define HIDEUI_FLAG_BANNER (0x0001)
182 #define HIDEUI_FLAG_LABEL (0x0002)
183 #define HIDEUI_FLAG_SINGLEUSER (0x0004)
184 #define HIDEUI_FLAG_HWTEST (0x0008)
185 #define HIDEUI_FLAG_ARROWS (0x0010)
186 #define HIDEUI_FLAG_HINTS (0x0020)
187 #define HIDEUI_FLAG_EDITOR (0x0040)
188 #define HIDEUI_FLAG_SAFEMODE (0x0080)
189 #define HIDEUI_FLAG_BADGES (0x0100)
190 #define HIDEUI_FLAG_ALL ((0xffff))
192 // Default hint text for program-launch submenus
193 #define SUBSCREEN_HINT1 L"Use arrow keys to move cursor; Enter to boot;"
194 #define SUBSCREEN_HINT2 L"Insert or F2 to edit options; Esc to return to main menu"
195 #define SUBSCREEN_HINT2_NO_EDITOR L"Esc to return to main menu"
197 #define NULL_GUID_VALUE { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
198 #define REFIND_GUID_VALUE { 0x36D08FA7, 0xCF0B, 0x42F5, {0x8F, 0x14, 0x68, 0xDF, 0x73, 0xED, 0x37, 0x40} };
201 // global definitions
206 typedef struct _uint32_list
{
208 struct _uint32_list
*Next
;
222 } MBR_PARTITION_INFO
;
225 EFI_DEVICE_PATH
*DevicePath
;
226 EFI_HANDLE DeviceHandle
;
232 EFI_GUID PartTypeGuid
;
233 BOOLEAN IsMarkedReadOnly
;
235 EG_IMAGE
*VolIconImage
;
236 EG_IMAGE
*VolBadgeImage
;
238 BOOLEAN IsAppleLegacy
;
242 BOOLEAN IsMbrPartition
;
243 UINTN MbrPartitionIndex
;
244 EFI_BLOCK_IO
*BlockIO
;
245 UINT64 BlockIOOffset
;
246 EFI_BLOCK_IO
*WholeDiskBlockIO
;
247 EFI_DEVICE_PATH
*WholeDiskDevicePath
;
248 MBR_PARTITION_INFO
*MbrPartitionTable
;
253 typedef struct _refit_menu_entry
{
257 CHAR16 ShortcutDigit
;
258 CHAR16 ShortcutLetter
;
260 EG_IMAGE
*BadgeImage
;
261 struct _refit_menu_screen
*SubScreen
;
264 typedef struct _refit_menu_screen
{
266 EG_IMAGE
*TitleImage
;
269 UINTN EntryCount
; // total number of entries registered
270 REFIT_MENU_ENTRY
**Entries
;
271 UINTN TimeoutSeconds
;
282 EFI_DEVICE_PATH
*DevicePath
;
283 BOOLEAN UseGraphicsMode
;
286 CHAR16
*InitrdPath
; // Linux stub loader only
292 REFIT_VOLUME
*Volume
;
293 BDS_COMMON_OPTION
*BdsOption
;
300 BOOLEAN ScanAllLinux
;
301 BOOLEAN DeepLegacyScan
;
302 BOOLEAN EnableAndLockVMX
;
303 BOOLEAN FoldLinuxKernels
;
305 UINTN RequestedScreenWidth
;
306 UINTN RequestedScreenHeight
;
307 UINTN BannerBottomEdge
;
308 UINTN RequestedTextMode
;
311 UINTN MaxTags
; // max. number of OS entries to show simultaneously in graphics mode
315 UINTN ScreensaverTime
;
318 REFIT_VOLUME
*DiscoveredRoot
;
319 EFI_DEVICE_PATH
*SelfDevicePath
;
320 CHAR16
*BannerFileName
;
321 EG_IMAGE
*ScreenBackground
;
322 CHAR16
*ConfigFilename
;
323 CHAR16
*SelectionSmallFileName
;
324 CHAR16
*SelectionBigFileName
;
325 CHAR16
*DefaultSelection
;
327 CHAR16
*DontScanVolumes
;
328 CHAR16
*DontScanDirs
;
329 CHAR16
*DontScanFiles
;
330 CHAR16
*WindowsRecoveryFiles
;
333 CHAR16
*SpoofOSXVersion
;
334 UINT32_LIST
*CsrValues
;
335 UINTN ShowTools
[NUM_TOOLS
];
336 CHAR8 ScanFor
[NUM_SCAN_OPTIONS
]; // codes of types of loaders for which to scan
341 extern EFI_HANDLE SelfImageHandle
;
342 extern EFI_LOADED_IMAGE
*SelfLoadedImage
;
343 extern EFI_FILE
*SelfRootDir
;
344 extern EFI_FILE
*SelfDir
;
345 extern CHAR16
*SelfDirPath
;
347 extern REFIT_VOLUME
*SelfVolume
;
348 extern REFIT_VOLUME
**Volumes
;
349 extern UINTN VolumesCount
;
351 extern REFIT_CONFIG GlobalConfig
;
353 extern EFI_GUID gEfiLegacyBootProtocolGuid
;
354 extern EFI_GUID gEfiGlobalVariableGuid
;
356 EFI_STATUS
StartEFIImageList(IN EFI_DEVICE_PATH
**DevicePaths
,
357 IN CHAR16
*LoadOptions
, IN UINTN LoaderType
,
358 IN CHAR16
*ImageTitle
, IN CHAR8 OSType
,
359 OUT UINTN
*ErrorInStep
,
361 IN BOOLEAN IsDriver
);
362 EFI_STATUS
StartEFIImage(IN EFI_DEVICE_PATH
*DevicePath
,
363 IN CHAR16
*LoadOptions
, IN UINTN LoaderType
,
364 IN CHAR16
*ImageTitle
, IN CHAR8 OSType
,
365 OUT UINTN
*ErrorInStep
,
367 IN BOOLEAN IsDriver
);
368 LOADER_ENTRY
*InitializeLoaderEntry(IN LOADER_ENTRY
*Entry
);
369 REFIT_MENU_SCREEN
*InitializeSubScreen(IN LOADER_ENTRY
*Entry
);
370 VOID
GenerateSubScreen(LOADER_ENTRY
*Entry
, IN REFIT_VOLUME
*Volume
, IN BOOLEAN GenerateReturn
);
371 EG_IMAGE
* GetDiskBadge(IN UINTN DiskType
);
372 LOADER_ENTRY
* MakeGenericLoaderEntry(VOID
);
373 VOID
SetLoaderDefaults(LOADER_ENTRY
*Entry
, CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
);
374 LOADER_ENTRY
* AddPreparedLoaderEntry(LOADER_ENTRY
*Entry
);
375 VOID
StoreLoaderName(IN CHAR16
*Name
);