3 * Functions specific to Apple computers
5 * Copyright (c) 2015 Roderick W. Smith
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "refit_call_wrapper.h"
29 CHAR16 gCsrStatus
[256];
31 // Get CSR (Apple's System Integrity Protection [SIP], or "rootless") status
33 EFI_STATUS
GetCsrStatus(UINT32
*CsrStatus
) {
34 UINT32
*ReturnValue
= NULL
;
36 EFI_GUID CsrGuid
= CSR_GUID
;
37 EFI_STATUS Status
= EFI_INVALID_PARAMETER
;
40 Status
= EfivarGetRaw(&CsrGuid
, L
"csr-active-config", (CHAR8
**) &ReturnValue
, &CsrLength
);
41 if (Status
== EFI_SUCCESS
) {
43 *CsrStatus
= *ReturnValue
;
45 Status
= EFI_BAD_BUFFER_SIZE
;
46 SPrint(gCsrStatus
, 255, L
" Unknown System Integrity Protection version");
48 MyFreePool(ReturnValue
);
49 } // if (Status == EFI_SUCCESS)
52 } // INTN GetCsrStatus()
54 // Store string describing CSR status value in gCsrStatus variable, which appears
55 // on the Info page. If DisplayMessage is TRUE, displays the new value of
56 // gCsrStatus on the screen for three seconds.
57 VOID
RecordgCsrStatus(UINT32 CsrStatus
, BOOLEAN DisplayMessage
) {
67 SPrint(gCsrStatus
, 255, L
" System Integrity Protection is enabled (0x%02x)", CsrStatus
);
70 SPrint(gCsrStatus
, 255, L
" System Integrity Protection is disabled (0x%02x)", CsrStatus
);
73 SPrint(gCsrStatus
, 255, L
" System Integrity Protection status: 0x%02x", CsrStatus
);
76 egDisplayMessage(gCsrStatus
, &BGColor
);
79 } // VOID RecordgCsrStatus
81 // Find the current CSR status and reset it to the next one in the
82 // GlobalConfig.CsrValues list, or to the first value if the current
83 // value is not on the list.
84 VOID
RotateCsrValue(VOID
) {
85 UINT32 CurrentValue
, TargetCsr
;
86 UINT32_LIST
*ListItem
;
87 EFI_GUID CsrGuid
= CSR_GUID
;
90 Status
= GetCsrStatus(&CurrentValue
);
91 if ((Status
== EFI_SUCCESS
) && GlobalConfig
.CsrValues
) {
92 ListItem
= GlobalConfig
.CsrValues
;
93 while ((ListItem
!= NULL
) && (ListItem
->Value
!= CurrentValue
))
94 ListItem
= ListItem
->Next
;
95 if (ListItem
== NULL
|| ListItem
->Next
== NULL
) {
96 TargetCsr
= GlobalConfig
.CsrValues
->Value
;
98 TargetCsr
= ListItem
->Next
->Value
;
100 Status
= EfivarSetRaw(&CsrGuid
, L
"csr-active-config", (CHAR8
*) &TargetCsr
, 4, TRUE
);
101 if (Status
== EFI_SUCCESS
)
102 RecordgCsrStatus(TargetCsr
, TRUE
);
104 SPrint(gCsrStatus
, 255, L
" Error setting System Integrity Protection code.");
106 } // VOID RotateCsrValue()
110 * The below definitions and SetAppleOSInfo() function are based on a GRUB patch
112 * https://lists.gnu.org/archive/html/grub-devel/2013-12/msg00442.html
115 #define EFI_APPLE_SET_OS_PROTOCOL_GUID \
116 { 0xc5c5da95, 0x7d5c, 0x45e6, \
117 { 0xb2, 0xf1, 0x3f, 0xd5, 0x2b, 0xb1, 0x00, 0x77 } \
120 typedef struct EfiAppleSetOsInterface
{
122 EFI_STATUS
EFIAPI (*SetOsVersion
) (IN CHAR8
*Version
);
123 EFI_STATUS
EFIAPI (*SetOsVendor
) (IN CHAR8
*Vendor
);
124 } EfiAppleSetOsInterface
;
126 // Function to tell the firmware that OS X is being launched. This is
127 // required to work around problems on some Macs that don't fully
128 // initialize some hardware (especially video displays) when third-party
129 // OSes are launched in EFI mode.
130 EFI_STATUS
SetAppleOSInfo() {
131 CHAR16
*AppleOSVersion
= NULL
;
132 CHAR8
*AppleOSVersion8
= NULL
;
134 EFI_GUID apple_set_os_guid
= EFI_APPLE_SET_OS_PROTOCOL_GUID
;
135 EfiAppleSetOsInterface
*SetOs
= NULL
;
137 Status
= refit_call3_wrapper(BS
->LocateProtocol
, &apple_set_os_guid
, NULL
, (VOID
**) &SetOs
);
139 // If not a Mac, ignore the call....
140 if ((Status
!= EFI_SUCCESS
) || (!SetOs
))
143 if ((SetOs
->Version
!= 0) && GlobalConfig
.SpoofOSXVersion
) {
144 AppleOSVersion
= StrDuplicate(L
"Mac OS X");
145 MergeStrings(&AppleOSVersion
, GlobalConfig
.SpoofOSXVersion
, ' ');
146 if (AppleOSVersion
) {
147 AppleOSVersion8
= AllocateZeroPool((StrLen(AppleOSVersion
) + 1) * sizeof(CHAR8
));
148 UnicodeStrToAsciiStr(AppleOSVersion
, AppleOSVersion8
);
149 if (AppleOSVersion8
) {
150 Status
= refit_call1_wrapper (SetOs
->SetOsVersion
, AppleOSVersion8
);
151 if (!EFI_ERROR(Status
))
152 Status
= EFI_SUCCESS
;
153 MyFreePool(AppleOSVersion8
);
155 Status
= EFI_OUT_OF_RESOURCES
;
156 Print(L
"Out of resources in SetAppleOSInfo!\n");
158 if ((Status
== EFI_SUCCESS
) && (SetOs
->Version
== 2))
159 Status
= refit_call1_wrapper (SetOs
->SetOsVendor
, "Apple Inc.");
160 MyFreePool(AppleOSVersion
);
161 } // if (AppleOSVersion)
163 if (Status
!= EFI_SUCCESS
)
164 Print(L
"Unable to set firmware boot type!\n");
167 } // EFI_STATUS SetAppleOSInfo()