]> code.delx.au - refind/blob - refind/apple.c
Pull string functions from refind/lib.[ch] into refind/mystrings.[ch]
[refind] / refind / apple.c
1 /*
2 * refind/apple.c
3 * Functions specific to Apple computers
4 *
5 * Copyright (c) 2015 Roderick W. Smith
6 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 #include "global.h"
23 #include "config.h"
24 #include "lib.h"
25 #include "screen.h"
26 #include "apple.h"
27 #include "mystrings.h"
28 #include "refit_call_wrapper.h"
29
30 CHAR16 gCsrStatus[256];
31
32 // Get CSR (Apple's System Integrity Protection [SIP], or "rootless") status
33 // information.
34 EFI_STATUS GetCsrStatus(UINT32 *CsrStatus) {
35 UINT32 *ReturnValue = NULL;
36 UINTN CsrLength;
37 EFI_GUID CsrGuid = CSR_GUID;
38 EFI_STATUS Status = EFI_INVALID_PARAMETER;
39
40 if (CsrStatus) {
41 Status = EfivarGetRaw(&CsrGuid, L"csr-active-config", (CHAR8**) &ReturnValue, &CsrLength);
42 if (Status == EFI_SUCCESS) {
43 if (CsrLength == 4) {
44 *CsrStatus = *ReturnValue;
45 } else {
46 Status = EFI_BAD_BUFFER_SIZE;
47 SPrint(gCsrStatus, 255, L" Unknown System Integrity Protection version");
48 }
49 MyFreePool(ReturnValue);
50 } // if (Status == EFI_SUCCESS)
51 } // if (CsrStatus)
52 return Status;
53 } // INTN GetCsrStatus()
54
55 // Store string describing CSR status value in gCsrStatus variable, which appears
56 // on the Info page. If DisplayMessage is TRUE, displays the new value of
57 // gCsrStatus on the screen for three seconds.
58 VOID RecordgCsrStatus(UINT32 CsrStatus, BOOLEAN DisplayMessage) {
59 EG_PIXEL BGColor;
60
61 BGColor.b = 255;
62 BGColor.g = 175;
63 BGColor.r = 100;
64 BGColor.a = 0;
65
66 switch (CsrStatus) {
67 case SIP_ENABLED:
68 SPrint(gCsrStatus, 255, L" System Integrity Protection is enabled (0x%02x)", CsrStatus);
69 break;
70 case SIP_DISABLED:
71 SPrint(gCsrStatus, 255, L" System Integrity Protection is disabled (0x%02x)", CsrStatus);
72 break;
73 default:
74 SPrint(gCsrStatus, 255, L" System Integrity Protection status: 0x%02x", CsrStatus);
75 } // switch
76 if (DisplayMessage) {
77 egDisplayMessage(gCsrStatus, &BGColor);
78 PauseSeconds(3);
79 } // if
80 } // VOID RecordgCsrStatus
81
82 // Find the current CSR status and reset it to the next one in the
83 // GlobalConfig.CsrValues list, or to the first value if the current
84 // value is not on the list.
85 VOID RotateCsrValue(VOID) {
86 UINT32 CurrentValue, TargetCsr;
87 UINT32_LIST *ListItem;
88 EFI_GUID CsrGuid = CSR_GUID;
89 EFI_STATUS Status;
90
91 Status = GetCsrStatus(&CurrentValue);
92 if ((Status == EFI_SUCCESS) && GlobalConfig.CsrValues) {
93 ListItem = GlobalConfig.CsrValues;
94 while ((ListItem != NULL) && (ListItem->Value != CurrentValue))
95 ListItem = ListItem->Next;
96 if (ListItem == NULL || ListItem->Next == NULL) {
97 TargetCsr = GlobalConfig.CsrValues->Value;
98 } else {
99 TargetCsr = ListItem->Next->Value;
100 }
101 Status = EfivarSetRaw(&CsrGuid, L"csr-active-config", (CHAR8 *) &TargetCsr, 4, TRUE);
102 if (Status == EFI_SUCCESS)
103 RecordgCsrStatus(TargetCsr, TRUE);
104 else
105 SPrint(gCsrStatus, 255, L" Error setting System Integrity Protection code.");
106 } // if
107 } // VOID RotateCsrValue()
108
109
110 /*
111 * The below definitions and SetAppleOSInfo() function are based on a GRUB patch
112 * by Andreas Heider:
113 * https://lists.gnu.org/archive/html/grub-devel/2013-12/msg00442.html
114 */
115
116 #define EFI_APPLE_SET_OS_PROTOCOL_GUID \
117 { 0xc5c5da95, 0x7d5c, 0x45e6, \
118 { 0xb2, 0xf1, 0x3f, 0xd5, 0x2b, 0xb1, 0x00, 0x77 } \
119 }
120
121 typedef struct EfiAppleSetOsInterface {
122 UINT64 Version;
123 EFI_STATUS EFIAPI (*SetOsVersion) (IN CHAR8 *Version);
124 EFI_STATUS EFIAPI (*SetOsVendor) (IN CHAR8 *Vendor);
125 } EfiAppleSetOsInterface;
126
127 // Function to tell the firmware that OS X is being launched. This is
128 // required to work around problems on some Macs that don't fully
129 // initialize some hardware (especially video displays) when third-party
130 // OSes are launched in EFI mode.
131 EFI_STATUS SetAppleOSInfo() {
132 CHAR16 *AppleOSVersion = NULL;
133 CHAR8 *AppleOSVersion8 = NULL;
134 EFI_STATUS Status;
135 EFI_GUID apple_set_os_guid = EFI_APPLE_SET_OS_PROTOCOL_GUID;
136 EfiAppleSetOsInterface *SetOs = NULL;
137
138 Status = refit_call3_wrapper(BS->LocateProtocol, &apple_set_os_guid, NULL, (VOID**) &SetOs);
139
140 // If not a Mac, ignore the call....
141 if ((Status != EFI_SUCCESS) || (!SetOs))
142 return EFI_SUCCESS;
143
144 if ((SetOs->Version != 0) && GlobalConfig.SpoofOSXVersion) {
145 AppleOSVersion = StrDuplicate(L"Mac OS X");
146 MergeStrings(&AppleOSVersion, GlobalConfig.SpoofOSXVersion, ' ');
147 if (AppleOSVersion) {
148 AppleOSVersion8 = AllocateZeroPool((StrLen(AppleOSVersion) + 1) * sizeof(CHAR8));
149 UnicodeStrToAsciiStr(AppleOSVersion, AppleOSVersion8);
150 if (AppleOSVersion8) {
151 Status = refit_call1_wrapper (SetOs->SetOsVersion, AppleOSVersion8);
152 if (!EFI_ERROR(Status))
153 Status = EFI_SUCCESS;
154 MyFreePool(AppleOSVersion8);
155 } else {
156 Status = EFI_OUT_OF_RESOURCES;
157 Print(L"Out of resources in SetAppleOSInfo!\n");
158 }
159 if ((Status == EFI_SUCCESS) && (SetOs->Version == 2))
160 Status = refit_call1_wrapper (SetOs->SetOsVendor, "Apple Inc.");
161 MyFreePool(AppleOSVersion);
162 } // if (AppleOSVersion)
163 } // if
164 if (Status != EFI_SUCCESS)
165 Print(L"Unable to set firmware boot type!\n");
166
167 return (Status);
168 } // EFI_STATUS SetAppleOSInfo()