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