OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
CsrUtil.c
Go to the documentation of this file.
1
8#include <Uefi.h>
10#include <Library/OcMiscLib.h>
12#include <Library/UefiApplicationEntryPoint.h>
13#include <Library/UefiBootServicesTableLib.h>
14#include <Library/UefiLib.h>
16#include <Guid/AppleVariable.h>
17
18// #define PRINT_ARGUMENTS
19
20#define MAX_FIRST_ARG_LEN 15
21
22STATIC
23EFI_STATUS
25 UINTN *Argc,
26 CHAR16 ***Argv
27 )
28{
29 CHAR16 *Space;
30
31 STATIC CHAR16 *NewArgs[3];
32 STATIC CHAR16 FirstArg[MAX_FIRST_ARG_LEN + 1];
33
34 if (*Argc != 2) {
35 return EFI_SUCCESS;
36 }
37
38 Space = StrStr ((*Argv)[1], L" ");
39 if (Space == NULL) {
40 return EFI_SUCCESS;
41 }
42
43 if (Space - (*Argv)[1] > MAX_FIRST_ARG_LEN) {
44 return EFI_OUT_OF_RESOURCES;
45 }
46
47 StrnCpyS (FirstArg, ARRAY_SIZE (FirstArg), (*Argv)[1], Space - (*Argv)[1]);
48
49 NewArgs[0] = L"Self";
50 NewArgs[1] = FirstArg;
51 NewArgs[2] = ++Space;
52
53 *Argc = 3;
54 *Argv = NewArgs;
55
56 return EFI_SUCCESS;
57}
58
59#ifdef PRINT_ARGUMENTS
60STATIC
61VOID
62PrintArguments (
63 UINTN Argc,
64 CHAR16 **Argv
65 )
66{
67 UINTN Index;
68
69 for (Index = 0; Index < Argc; ++Index) {
70 Print (L"%u: %s\n", Index, Argv[Index]);
71 }
72}
73
74#endif
75
76STATIC
77VOID
79 VOID
80 )
81{
82 Print (L"usage: csrutil <command> [<csr-value>]\n");
83 Print (L"Modify the System Integrity Protection configuration.\n");
84 Print (L"Available commands:\n");
85 Print (L"\n");
86 Print (L" clear\n");
87 Print (L" Clear the existing configuration.\n");
88 Print (L" disable [<csr-value>]\n");
89 Print (L" Disable the protection on the machine (use default 0x%x or csr value).\n", OC_CSR_DISABLE_FLAGS);
90 Print (L" enable [<csr-value>]\n");
91 Print (L" Enable the protection on the machine (use 0 or other legal csr value).\n");
92 Print (L" toggle [<csr-value>]\n");
93 Print (L" Toggle the protection on the machine (use default 0x%x or csr value).\n", OC_CSR_DISABLE_FLAGS);
94 Print (L" status\n");
95 Print (L" Display the current configuration.\n");
96 Print (L"\n");
97}
98
99EFI_STATUS
100EFIAPI
102 IN EFI_HANDLE ImageHandle,
103 IN EFI_SYSTEM_TABLE *SystemTable
104 )
105{
106 EFI_STATUS OldStatus;
107 EFI_STATUS Status;
108 UINTN Data;
109 CHAR16 *EndPtr;
110 UINTN Argc;
111 CHAR16 **Argv;
112 UINT32 CsrConfig;
113 UINT32 Attributes;
114
115 Status = GetArguments (&Argc, &Argv);
116
117 if (EFI_ERROR (Status)) {
118 Print (L"GetArguments - %r\n", Status);
119 return Status;
120 }
121
122 Status = SplitArguments (&Argc, &Argv);
123
124 #ifdef PRINT_ARGUMENTS
125 if (!EFI_ERROR (Status)) {
126 PrintArguments (Argc, Argv);
127 }
128
129 #endif
130
131 if (EFI_ERROR (Status) || (Argc < 2)) {
132 PrintUsage ();
133 return EFI_SUCCESS;
134 }
135
136 if (Argc > 2) {
137 Data = 0;
138 if (OcUnicodeStartsWith (Argv[2], L"0x", TRUE)) {
139 Status = StrHexToUintnS (Argv[2], &EndPtr, &Data);
140 } else {
141 Status = StrDecimalToUintnS (Argv[2], &EndPtr, &Data);
142 }
143
144 if (!EFI_ERROR (Status) && ((EndPtr != &Argv[2][StrLen (Argv[2])]) || ((Data & MAX_UINT32) != Data))) {
145 Status = EFI_UNSUPPORTED;
146 }
147 }
148
149 if (EFI_ERROR (Status)) {
150 PrintUsage ();
151 return Status;
152 }
153
154 if ((Argc == 2) && (StrCmp (Argv[1], L"status") == 0)) {
155 //
156 // Status
157 //
158 Print (L"System Integrity Protection status: ");
159 } else {
160 //
161 // When changing status, use existing attributes where present
162 // (e.g. keep changes made while WriteFlash=false as volatile only)
163 //
164 Status = OcGetSip (&CsrConfig, &Attributes);
165
166 if ((Status != EFI_NOT_FOUND) && EFI_ERROR (Status)) {
167 Print (L"Error getting SIP status - %r\n", Status);
168 return Status;
169 }
170
171 if (Status == EFI_NOT_FOUND) {
172 Attributes = CSR_APPLE_SIP_NVRAM_NV_ATTR;
173 } else {
174 //
175 // We are finding other bits set on Apl, specifically 0x80000000,
176 // so only consider relevant bits.
177 //
178 Attributes &= CSR_APPLE_SIP_NVRAM_NV_ATTR;
179 }
180
181 if ((Argc == 2) && (StrCmp (Argv[1], L"clear") == 0)) {
182 //
183 // Clear
184 //
185 OldStatus = Status;
186
187 Status = OcSetSip (NULL, Attributes);
188
189 if (EFI_ERROR (Status) && !((OldStatus == EFI_NOT_FOUND) && (Status == EFI_NOT_FOUND))) {
190 Print (L"Error clearing SIP - r\n", Status);
191 return Status;
192 }
193
194 Print (L"Successfully cleared system integrity configuration: ");
195 } else if ((Argc <= 3) && (StrCmp (Argv[1], L"disable") == 0)) {
196 //
197 // Disable; allow anything except valid enable values
198 //
199 if (Argc == 2) {
200 CsrConfig = OC_CSR_DISABLE_FLAGS;
201 } else {
202 if ((Data & ~CSR_ALLOW_APPLE_INTERNAL) == 0) {
203 Print (L"Illegal value for %s\n", L"disable");
204 return EFI_UNSUPPORTED;
205 }
206
207 CsrConfig = (UINT32)Data;
208 }
209
210 Status = OcSetSip (&CsrConfig, Attributes);
211
212 if (EFI_ERROR (Status)) {
213 Print (L"Error disabling SIP - r\n", Status);
214 return Status;
215 }
216
217 Print (L"System Integrity Protection is ");
218 } else if ((Argc <= 3) && (StrCmp (Argv[1], L"enable") == 0)) {
219 //
220 // Enable; allow user-specified Apple internal (which reports as enabled) or zero only
221 //
222 if (Argc == 2) {
223 CsrConfig = 0;
224 } else {
225 if ((Data & ~CSR_ALLOW_APPLE_INTERNAL) != 0) {
226 Print (L"Illegal value for %s\n", L"enable");
227 return EFI_UNSUPPORTED;
228 }
229
230 CsrConfig = (UINT32)Data;
231 }
232
233 Status = OcSetSip (&CsrConfig, Attributes);
234
235 if (EFI_ERROR (Status)) {
236 Print (L"Error enabling SIP - r\n", Status);
237 return Status;
238 }
239
240 Print (L"System Integrity Protection is ");
241 } else if ((Argc <= 3) && (StrCmp (Argv[1], L"toggle") == 0)) {
242 //
243 // Toggle; allow anything except valid enable values
244 //
245 if (Argc == 2) {
246 CsrConfig = OC_CSR_DISABLE_FLAGS;
247 } else {
248 if ((Data & ~CSR_ALLOW_APPLE_INTERNAL) == 0) {
249 Print (L"Illegal value for %s\n", L"toggle");
250 return EFI_UNSUPPORTED;
251 }
252
253 CsrConfig = (UINT32)Data;
254 }
255
256 Status = OcToggleSip (CsrConfig);
257
258 if (EFI_ERROR (Status)) {
259 Print (L"Error toggling SIP - r\n", Status);
260 return Status;
261 }
262
263 Print (L"System Integrity Protection value was toggled to ");
264 } else {
265 //
266 // Unsupported
267 //
268 PrintUsage ();
269 return EFI_UNSUPPORTED;
270 }
271 }
272
273 //
274 // Add result
275 //
276 Status = OcGetSip (&CsrConfig, &Attributes);
277
278 if ((Status != EFI_NOT_FOUND) && EFI_ERROR (Status)) {
279 Print (L"error getting SIP status - %r\n", Status);
280 return Status;
281 }
282
283 if (Status != EFI_NOT_FOUND) {
284 Attributes &= CSR_APPLE_SIP_NVRAM_NV_ATTR;
285 }
286
287 Print (L"%sd (", OcIsSipEnabled (Status, CsrConfig) ? L"enable" : L"disable");
288 if (Status == EFI_NOT_FOUND) {
289 Print (L"nvram var not set");
290 } else {
291 Print (L"0x%x", CsrConfig);
292 if ((Attributes & ~EFI_VARIABLE_NON_VOLATILE) != CSR_APPLE_SIP_NVRAM_ATTR) {
293 Print (L", UNKNOWN: 0x%x", Attributes);
294 } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
295 Print (L", volatile");
296 }
297 }
298
299 Print (L")\n");
300
301 if (StrCmp (Argv[0], L"Self") == 0) {
302 //
303 // Pause if detect called as tool
304 //
305 WaitForKeyPress (L"Press any key to continue...");
306 }
307
308 return EFI_SUCCESS;
309}
#define CSR_APPLE_SIP_NVRAM_ATTR
#define OC_CSR_DISABLE_FLAGS
#define CSR_APPLE_SIP_NVRAM_NV_ATTR
#define CSR_ALLOW_APPLE_INTERNAL
#define ARRAY_SIZE(Array)
Definition AppleMacEfi.h:34
#define MAX_FIRST_ARG_LEN
Definition CsrUtil.c:20
EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition CsrUtil.c:101
STATIC VOID PrintUsage(VOID)
Definition CsrUtil.c:78
STATIC EFI_STATUS SplitArguments(UINTN *Argc, CHAR16 ***Argv)
Definition CsrUtil.c:24
VOID WaitForKeyPress(IN CONST CHAR16 *Message)
Definition DebugHelp.c:35
EFI_STATUS GetArguments(OUT UINTN *Argc, OUT CHAR16 ***Argv)
BOOLEAN EFIAPI OcUnicodeStartsWith(IN CONST CHAR16 *String, IN CONST CHAR16 *SearchString, IN BOOLEAN CaseInsensitiveMatch)
EFI_STATUS OcGetSip(OUT UINT32 *CsrActiveConfig, OUT UINT32 *Attributes OPTIONAL)
Definition Sip.c:15
BOOLEAN OcIsSipEnabled(IN EFI_STATUS GetStatus, IN UINT32 CsrActiveConfig)
Definition Sip.c:58
EFI_STATUS OcToggleSip(IN UINT32 CsrActiveConfig)
Definition Sip.c:73
EFI_STATUS OcSetSip(IN UINT32 *CsrActiveConfig, IN UINT32 Attributes)
Definition Sip.c:39