]> code.delx.au - refind/blobdiff - refind/main.c
Added summary of Apple System Integrity Protection (SIP) status to
[refind] / refind / main.c
index 9d191077a84ad05f7037906fa538966dc4093173..690816996b2e4a93b0eb475225448710166a0dcb 100644 (file)
  * 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 <http://www.gnu.org/licenses/>.
+ */
 
 #include "global.h"
 #include "config.h"
@@ -143,11 +156,14 @@ REFIT_MENU_SCREEN MainMenu       = { L"Main Menu", NULL, 0, NULL, 0, NULL, 0, L"
                                      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, 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,
+                              { 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;
@@ -167,13 +183,26 @@ struct LOADER_LIST {
 // misc functions
 //
 
+static INTN GetCsrStatus(VOID) {
+    CHAR8 *CsrValues;
+    UINTN CsrLength;
+    EFI_GUID CsrGuid = CSR_GUID;
+    EFI_STATUS Status;
+
+    Status = EfivarGetRaw(&CsrGuid, L"csr-active-config", &CsrValues, &CsrLength);
+    if ((Status == EFI_SUCCESS) && (CsrLength == 4))
+        return CsrValues[0];
+    else
+        return -1;
+} // INTN GetCsrStatus()
+
 static VOID AboutrEFInd(VOID)
 {
     CHAR16 *FirmwareVendor;
 
     if (AboutMenu.EntryCount == 0) {
         AboutMenu.TitleImage = BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT);
-        AddMenuInfoLine(&AboutMenu, L"rEFInd Version 0.9.2");
+        AddMenuInfoLine(&AboutMenu, L"rEFInd Version 0.9.2.5");
         AddMenuInfoLine(&AboutMenu, L"");
         AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2006-2010 Christoph Pfisterer");
         AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2012-2015 Roderick W. Smith");
@@ -191,6 +220,18 @@ static VOID AboutrEFInd(VOID)
 #else
         AddMenuInfoLine(&AboutMenu, L" Platform: unknown");
 #endif
+        if (StriSubCmp(L"Apple", ST->FirmwareVendor)) {
+            switch (GetCsrStatus()) {
+                case SIP_ENABLED:
+                    AddMenuInfoLine(&AboutMenu, L" System Integrity Protection is enabled");
+                    break;
+                case SIP_DISABLED:
+                    AddMenuInfoLine(&AboutMenu, L" System Integrity Protection is disabled");
+                    break;
+                default:
+                    AddMenuInfoLine(&AboutMenu, L" System Integrity Protection status is unrecognized");
+            } // switch
+        } // if
         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,
@@ -2128,6 +2169,66 @@ static VOID SetConfigFilename(EFI_HANDLE ImageHandle) {
     } // if
 } // VOID SetConfigFilename()
 
+/*
+ * The below definitions and SetAppleOSInfo() function are based on a GRUB patch
+ * by Andreas Heider:
+ * https://lists.gnu.org/archive/html/grub-devel/2013-12/msg00442.html
+ */
+
+#define EFI_APPLE_SET_OS_PROTOCOL_GUID  \
+  { 0xc5c5da95, 0x7d5c, 0x45e6, \
+    { 0xb2, 0xf1, 0x3f, 0xd5, 0x2b, 0xb1, 0x00, 0x77 } \
+  }
+
+typedef struct EfiAppleSetOsInterface {
+    UINT64 Version;
+    EFI_STATUS EFIAPI (*SetOsVersion) (IN CHAR8 *Version);
+    EFI_STATUS EFIAPI (*SetOsVendor) (IN CHAR8 *Vendor);
+} EfiAppleSetOsInterface;
+
+// Function to tell the firmware that OS X is being launched. This is
+// required to work around problems on some Macs that don't fully
+// initialize some hardware (especially video displays) when third-party
+// OSes are launched in EFI mode.
+static EFI_STATUS SetAppleOSInfo() {
+    CHAR16 *AppleOSVersion = NULL;
+    CHAR8 *AppleOSVersion8 = NULL;
+    EFI_STATUS Status;
+    EFI_GUID apple_set_os_guid = EFI_APPLE_SET_OS_PROTOCOL_GUID;
+    EfiAppleSetOsInterface *SetOs = NULL;
+
+    Status = refit_call3_wrapper(BS->LocateProtocol, &apple_set_os_guid, NULL, (VOID**) &SetOs);
+
+    // Not a Mac, so ignore the call....
+    if ((Status != EFI_SUCCESS) || (!SetOs))
+        return EFI_SUCCESS;
+
+    if ((SetOs->Version != 0) && GlobalConfig.SpoofOSXVersion) {
+        AppleOSVersion = StrDuplicate(L"Mac OS X");
+        MergeStrings(&AppleOSVersion, GlobalConfig.SpoofOSXVersion, ' ');
+        if (AppleOSVersion) {
+            AppleOSVersion8 = AllocateZeroPool((StrLen(AppleOSVersion) + 1) * sizeof(CHAR8));
+            UnicodeStrToAsciiStr(AppleOSVersion, AppleOSVersion8);
+            if (AppleOSVersion8) {
+                Status = refit_call1_wrapper (SetOs->SetOsVersion, AppleOSVersion8);
+                if (!EFI_ERROR(Status))
+                    Status = EFI_SUCCESS;
+                MyFreePool(AppleOSVersion8);
+            } else {
+                Status = EFI_OUT_OF_RESOURCES;
+                Print(L"Out of resources in SetAppleOSInfo!\n");
+            }
+            if ((Status == EFI_SUCCESS) && (SetOs->Version == 2))
+                Status = refit_call1_wrapper (SetOs->SetOsVendor, "Apple Inc.");
+            MyFreePool(AppleOSVersion);
+        } // if (AppleOSVersion)
+    } // if
+    if (Status != EFI_SUCCESS)
+        Print(L"Unable to set firmware boot type!\n");
+
+    return (Status);
+} // EFI_STATUS SetAppleOSInfo()
+
 //
 // main entry point
 //
@@ -2157,6 +2258,9 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
     SetConfigFilename(ImageHandle);
     ReadConfig(GlobalConfig.ConfigFilename);
 
+    if (GlobalConfig.SpoofOSXVersion && GlobalConfig.SpoofOSXVersion[0] != L'\0')
+        SetAppleOSInfo();
+
     InitScreen();
     WarnIfLegacyProblems();
     MainMenu.TimeoutSeconds = GlobalConfig.Timeout;