X-Git-Url: https://code.delx.au/refind/blobdiff_plain/4cbcc9d19f8bfa11a4bc396645c5689d4c6f9482..b3f1088e428ec704586ecfdddf75f757f213b97e:/refind/main.c diff --git a/refind/main.c b/refind/main.c index 7c998cf..35ecc4c 100644 --- a/refind/main.c +++ b/refind/main.c @@ -37,10 +37,23 @@ * Modifications copyright (c) 2012-2015 Roderick W. Smith * * Modifications distributed under the terms of the GNU General Public - * License (GPL) version 3 (GPLv3), a copy of which must be distributed - * with this source code or binaries made from it. + * License (GPL) version 3 (GPLv3), or (at your option) any later version. * */ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include "global.h" #include "config.h" @@ -51,6 +64,8 @@ #include "menu.h" #include "mok.h" #include "gpt.h" +#include "apple.h" +#include "mystrings.h" #include "security_policy.h" #include "driver_support.h" #include "../include/Handle.h" @@ -99,6 +114,17 @@ EFI_GUID gFreedesktopRootGuid = { 0x4f68bce3, 0xe8cd, 0x4db1, { 0x96, 0xe7, 0xfb #define FALLBACK_BASENAME L"bootia32.efi" #define EFI_STUB_ARCH 0x014c EFI_GUID gFreedesktopRootGuid = { 0x44479540, 0xf297, 0x41b2, { 0x9a, 0xf7, 0xd1, 0x31, 0xd5, 0xf0, 0x45, 0x8a }}; +#elif defined (EFIAARCH64) +#define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\EFI\\tools\\shellaa64.efi,\\shell.efi,\\shellaa64.efi" +#define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi,\\EFI\\tools\\gptsync_aa64.efi" +#define GDISK_NAMES L"\\EFI\\tools\\gdisk.efi,\\EFI\\tools\\gdisk_aa64.efi" +#define NETBOOT_NAMES L"\\EFI\\tools\\ipxe.efi" +#define MEMTEST_NAMES L"memtest86.efi,memtest86_aa64.efi,memtest86aa64.efi,bootaa64.efi" +#define DRIVER_DIRS L"drivers,drivers_aa64" +#define FALLBACK_FULLNAME L"EFI\\BOOT\\bootaa64.efi" +#define FALLBACK_BASENAME L"bootaa64.efi" +#define EFI_STUB_ARCH 0xaa64 +EFI_GUID gFreedesktopRootGuid = { 0xb921b045, 0x1df0, 0x41c3, { 0xaf, 0x44, 0x4c, 0x6f, 0x28, 0x0d, 0x3f, 0xae }}; #else #define SHELL_NAMES L"\\EFI\\tools\\shell.efi,\\shell.efi" #define GPTSYNC_NAMES L"\\EFI\\tools\\gptsync.efi" @@ -126,7 +152,7 @@ EFI_GUID gFreedesktopRootGuid = { 0xb921b045, 0x1df0, 0x41c3, { 0xaf, 0x44, 0x4c // Patterns that identify Linux kernels. Added to the loader match pattern when the // scan_all_linux_kernels option is set in the configuration file. Causes kernels WITHOUT // a ".efi" extension to be found when scanning for boot loaders. -#define LINUX_MATCH_PATTERNS L"vmlinuz*,bzImage*" +#define LINUX_MATCH_PATTERNS L"vmlinuz*,bzImage*,kernel*" // Maximum length of a text string in certain menus #define MAX_LINE_LENGTH 65 @@ -137,17 +163,21 @@ static REFIT_MENU_ENTRY MenuEntryShutdown = { L"Shut Down Computer", TAG_SHUTDOW REFIT_MENU_ENTRY MenuEntryReturn = { L"Return to Main Menu", TAG_RETURN, 1, 0, 0, NULL, NULL, NULL }; static REFIT_MENU_ENTRY MenuEntryExit = { L"Exit rEFInd", TAG_EXIT, 1, 0, 0, NULL, NULL, NULL }; static REFIT_MENU_ENTRY MenuEntryFirmware = { L"Reboot to Computer Setup Utility", TAG_FIRMWARE, 1, 0, 0, NULL, NULL, NULL }; +static REFIT_MENU_ENTRY MenuEntryRotateCsr = { L"Change SIP Policy", TAG_CSR_ROTATE, 1, 0, 0, NULL, NULL, NULL }; REFIT_MENU_SCREEN MainMenu = { L"Main Menu", NULL, 0, NULL, 0, NULL, 0, L"Automatic boot", L"Use arrow keys to move cursor; Enter to boot;", L"Insert or F2 for more options; Esc to refresh" }; static REFIT_MENU_SCREEN AboutMenu = { L"About", NULL, 0, NULL, 0, NULL, 0, NULL, L"Press Enter to return to main menu", L"" }; -REFIT_CONFIG GlobalConfig = { FALSE, TRUE, FALSE, FALSE, TRUE, 0, 0, 0, DONT_CHANGE_TEXT_MODE, 20, 0, 0, GRAPHICS_FOR_OSX, LEGACY_TYPE_MAC, - 0, 0, { DEFAULT_BIG_ICON_SIZE / 4, DEFAULT_SMALL_ICON_SIZE, DEFAULT_BIG_ICON_SIZE }, BANNER_NOSCALE, - NULL, NULL, NULL, CONFIG_FILE_NAME, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - { TAG_SHELL, TAG_MEMTEST, TAG_GDISK, TAG_APPLE_RECOVERY, TAG_WINDOWS_RECOVERY, TAG_MOK_TOOL, - TAG_ABOUT, TAG_SHUTDOWN, TAG_REBOOT, TAG_FIRMWARE, 0, 0, 0, 0, 0, 0, 0, 0 } +REFIT_CONFIG GlobalConfig = { FALSE, TRUE, FALSE, FALSE, TRUE, 0, 0, 0, DONT_CHANGE_TEXT_MODE, + 20, 0, 0, GRAPHICS_FOR_OSX, LEGACY_TYPE_MAC, + 0, 0, { DEFAULT_BIG_ICON_SIZE / 4, DEFAULT_SMALL_ICON_SIZE, DEFAULT_BIG_ICON_SIZE }, + BANNER_NOSCALE, NULL, NULL, NULL, NULL, CONFIG_FILE_NAME, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + { TAG_SHELL, TAG_MEMTEST, TAG_GDISK, TAG_APPLE_RECOVERY, TAG_WINDOWS_RECOVERY, + TAG_MOK_TOOL, TAG_ABOUT, TAG_SHUTDOWN, TAG_REBOOT, TAG_FIRMWARE, + 0, 0, 0, 0, 0, 0, 0, 0 } }; EFI_GUID GlobalGuid = EFI_GLOBAL_VARIABLE; @@ -169,11 +199,12 @@ struct LOADER_LIST { static VOID AboutrEFInd(VOID) { - CHAR16 *FirmwareVendor; + CHAR16 *FirmwareVendor; + UINT32 CsrStatus; if (AboutMenu.EntryCount == 0) { AboutMenu.TitleImage = BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT); - AddMenuInfoLine(&AboutMenu, L"rEFInd Version 0.9.0.4"); + AddMenuInfoLine(&AboutMenu, L"rEFInd Version 0.10.0.4"); AddMenuInfoLine(&AboutMenu, L""); AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2006-2010 Christoph Pfisterer"); AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2012-2015 Roderick W. Smith"); @@ -188,9 +219,16 @@ static VOID AboutrEFInd(VOID) #elif defined(EFIX64) AddMenuInfoLine(&AboutMenu, PoolPrint(L" Platform: x86_64 (64 bit); Secure Boot %s", secure_mode() ? L"active" : L"inactive")); +#elif defined(EFIAARCH64) + AddMenuInfoLine(&AboutMenu, PoolPrint(L" Platform: ARM (64 bit); Secure Boot %s", + secure_mode() ? L"active" : L"inactive")); #else AddMenuInfoLine(&AboutMenu, L" Platform: unknown"); #endif + if (GetCsrStatus(&CsrStatus) == EFI_SUCCESS) { + RecordgCsrStatus(CsrStatus, FALSE); + AddMenuInfoLine(&AboutMenu, gCsrStatus); + } FirmwareVendor = StrDuplicate(ST->FirmwareVendor); LimitStringLength(FirmwareVendor, MAX_LINE_LENGTH); // More than ~65 causes empty info page on 800x600 display AddMenuInfoLine(&AboutMenu, PoolPrint(L" Firmware: %s %d.%02d", FirmwareVendor, ST->FirmwareRevision >> 16, @@ -235,7 +273,7 @@ static VOID WarnSecureBootError(CHAR16 *Name, BOOLEAN Verbose) { // Returns TRUE if this file is a valid EFI loader file, and is proper ARCH static BOOLEAN IsValidLoader(EFI_FILE *RootDir, CHAR16 *FileName) { BOOLEAN IsValid = TRUE; -#if defined (EFIX64) | defined (EFI32) +#if defined (EFIX64) | defined (EFI32) | defined (EFIAARCH64) EFI_STATUS Status; EFI_FILE_HANDLE FileHandle; CHAR8 Header[512]; @@ -276,7 +314,7 @@ EFI_STATUS StartEFIImageList(IN EFI_DEVICE_PATH **DevicePaths, IN BOOLEAN IsDriver) { EFI_STATUS Status, ReturnStatus; - EFI_HANDLE ChildImageHandle; + EFI_HANDLE ChildImageHandle, ChildImageHandle2; EFI_LOADED_IMAGE *ChildLoadedImage = NULL; REFIT_VOLUME *Volume = NULL; UINTN DevicePathIndex; @@ -300,7 +338,7 @@ EFI_STATUS StartEFIImageList(IN EFI_DEVICE_PATH **DevicePaths, if (Verbose) Print(L"Starting %s\nUsing load options '%s'\n", ImageTitle, FullLoadOptions ? FullLoadOptions : L""); - // load the image into memory (and execute it, in the case of a shim/MOK image). + // load the image into memory ReturnStatus = Status = EFI_NOT_FOUND; // in case the list is empty for (DevicePathIndex = 0; DevicePaths[DevicePathIndex] != NULL; DevicePathIndex++) { FindVolumeAndFilename(DevicePaths[DevicePathIndex], &Volume, &Filename); @@ -325,6 +363,22 @@ EFI_STATUS StartEFIImageList(IN EFI_DEVICE_PATH **DevicePaths, // ImageData, ImageSize, &ChildImageHandle); ReturnStatus = Status = refit_call6_wrapper(BS->LoadImage, FALSE, SelfImageHandle, DevicePaths[DevicePathIndex], NULL, 0, &ChildImageHandle); + if (secure_mode() && ShimLoaded()) { + // Load ourself into memory. This is a trick to work around a bug in Shim 0.8, + // which ties itself into the BS->LoadImage() and BS->StartImage() functions and + // then unregisters itself from the EFI system table when its replacement + // StartImage() function is called *IF* the previous LoadImage() was for the same + // program. The result is that rEFInd can validate only the first program it + // launches (often a filesystem driver). Loading a second program (rEFInd itself, + // here, to keep it smaller than a kernel) works around this problem. See the + // replacements.c file in Shim, and especially its start_image() function, for + // the source of the problem. + // NOTE: This doesn't check the return status or handle errors. It could + // conceivably do weird things if, say, rEFInd were on a USB drive that the + // user pulls before launching a program. + refit_call6_wrapper(BS->LoadImage, FALSE, SelfImageHandle, GlobalConfig.SelfDevicePath, + NULL, 0, &ChildImageHandle2); + } } else { Print(L"Invalid loader file!\n"); ReturnStatus = EFI_LOAD_ERROR; @@ -443,6 +497,7 @@ VOID StoreLoaderName(IN CHAR16 *Name) { // for information on Intel VMX features static VOID DoEnableAndLockVMX(VOID) { +#if defined (EFIX64) | defined (EFI32) UINT32 msr = 0x3a; UINT32 low_bits = 0, high_bits = 0; @@ -455,7 +510,8 @@ static VOID DoEnableAndLockVMX(VOID) low_bits = 0x05; msr = 0x3a; __asm__ volatile ("wrmsr" : : "c" (msr), "a" (low_bits), "d" (high_bits)); - } + } +#endif } // VOID DoEnableAndLockVMX() static VOID StartLoader(LOADER_ENTRY *Entry, CHAR16 *SelectionName) @@ -963,7 +1019,7 @@ VOID SetLoaderDefaults(LOADER_ENTRY *Entry, CHAR16 *LoaderPath, REFIT_VOLUME *Vo } // if/else network boot // detect specific loaders - if (StriSubCmp(L"bzImage", NameClues) || StriSubCmp(L"vmlinuz", NameClues)) { + if (StriSubCmp(L"bzImage", NameClues) || StriSubCmp(L"vmlinuz", NameClues) || StriSubCmp(L"kernel", NameClues)) { if (Volume->DiskKind != DISK_KIND_NET) { GuessLinuxDistribution(&OSIconName, Volume, LoaderPath); Entry->LoadOptions = GetMainLinuxOptions(LoaderPath, Volume); @@ -1377,7 +1433,9 @@ static BOOLEAN ScanLoaderDir(IN REFIT_VOLUME *Volume, IN CHAR16 *Path, IN CHAR16 NewLoader = LoaderList; while (NewLoader != NULL) { - IsLinux = (StriSubCmp(L"bzImage", NewLoader->FileName) || StriSubCmp(L"vmlinuz", NewLoader->FileName)); + IsLinux = (StriSubCmp(L"bzImage", NewLoader->FileName) || + StriSubCmp(L"vmlinuz", NewLoader->FileName) || + StriSubCmp(L"kernel", NewLoader->FileName)); if ((FirstKernel != NULL) && IsLinux && GlobalConfig.FoldLinuxKernels) { AddKernelToSubmenu(FirstKernel, NewLoader->FileName, Volume); } else { @@ -1873,6 +1931,7 @@ static VOID ScanForTools(VOID) { UINTN i, j, VolumeIndex; UINT64 osind; CHAR8 *b = 0; + UINT32 CsrValue; MokLocations = StrDuplicate(MOK_LOCATIONS); if (MokLocations != NULL) @@ -2003,6 +2062,14 @@ static VOID ScanForTools(VOID) { FindTool(MokLocations, MOK_NAMES, L"MOK utility", BUILTIN_ICON_TOOL_MOK_TOOL); break; + case TAG_CSR_ROTATE: + if ((GetCsrStatus(&CsrValue) == EFI_SUCCESS) && (GlobalConfig.CsrValues)) { + TempMenuEntry = CopyMenuEntry(&MenuEntryRotateCsr); + TempMenuEntry->Image = BuiltinIcon(BUILTIN_ICON_FUNC_CSR_ROTATE); + AddMenuEntry(&MainMenu, TempMenuEntry); + } // if + break; + case TAG_MEMTEST: FindTool(MEMTEST_LOCATIONS, MEMTEST_NAMES, L"Memory test utility", BUILTIN_ICON_TOOL_MEMTEST); break; @@ -2024,9 +2091,9 @@ static VOID RescanAll(BOOLEAN DisplayMessage) { FreeList((VOID ***) &(MainMenu.Entries), &MainMenu.EntryCount); MainMenu.Entries = NULL; MainMenu.EntryCount = 0; - ReadConfig(GlobalConfig.ConfigFilename); ConnectAllDriversToAllControllers(); ScanVolumes(); + ReadConfig(GlobalConfig.ConfigFilename); ScanForBootloaders(); ScanForTools(); SetupScreen(); @@ -2058,6 +2125,7 @@ static BOOLEAN SecureBootSetup(VOID) { Success = TRUE; } else { Print(L"Failed to install MOK Secure Boot extensions"); + PauseForKey(); } } return Success; @@ -2087,20 +2155,19 @@ static BOOLEAN SecureBootUninstall(VOID) { // If an error is encountered, leaves the value alone (it should be set to // CONFIG_FILE_NAME when GlobalConfig is initialized). static VOID SetConfigFilename(EFI_HANDLE ImageHandle) { -EFI_LOADED_IMAGE *Info; -CHAR16 *Options, *FileName; -EFI_STATUS Status; -INTN Where; + EFI_LOADED_IMAGE *Info; + CHAR16 *Options, *FileName, *SubString; + EFI_STATUS Status; -Status = refit_call3_wrapper(BS->HandleProtocol, ImageHandle, &LoadedImageProtocol, (VOID **) &Info); + Status = refit_call3_wrapper(BS->HandleProtocol, ImageHandle, &LoadedImageProtocol, (VOID **) &Info); if ((Status == EFI_SUCCESS) && (Info->LoadOptionsSize > 0)) { Options = (CHAR16 *) Info->LoadOptions; - Where = FindSubString(L" -c ", Options); - if (Where >= 0) { - FileName = StrDuplicate(&Options[Where + 4]); - Where = FindSubString(L" ", FileName); - if (Where > 0) - FileName[Where] = L'\0'; + SubString = MyStrStr(Options, L" -c "); + if (SubString) { + FileName = StrDuplicate(&SubString[4]); + if (FileName) { + LimitStringLength(FileName, 256); + } if (FileExists(SelfDir, FileName)) { GlobalConfig.ConfigFilename = FileName; @@ -2139,8 +2206,14 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) if (GlobalConfig.LegacyType == LEGACY_TYPE_MAC) CopyMem(GlobalConfig.ScanFor, "ihebocm ", NUM_SCAN_OPTIONS); SetConfigFilename(ImageHandle); + MokProtocol = SecureBootSetup(); + LoadDrivers(); + ScanVolumes(); // Do before ReadConfig() because it needs SelfVolume->VolName ReadConfig(GlobalConfig.ConfigFilename); + if (GlobalConfig.SpoofOSXVersion && GlobalConfig.SpoofOSXVersion[0] != L'\0') + SetAppleOSInfo(); + InitScreen(); WarnIfLegacyProblems(); MainMenu.TimeoutSeconds = GlobalConfig.Timeout; @@ -2149,9 +2222,6 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) refit_call4_wrapper(BS->SetWatchdogTimer, 0x0000, 0x0000, 0x0000, NULL); // further bootstrap (now with config available) - MokProtocol = SecureBootSetup(); - LoadDrivers(); - ScanVolumes(); ScanForBootloaders(); ScanForTools(); SetupScreen(); @@ -2228,6 +2298,10 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) RebootIntoFirmware(); break; + case TAG_CSR_ROTATE: + RotateCsrValue(); + break; + } // switch() } // while()