]> code.delx.au - refind/blob - refind/config.c
Further refinements to SIP/CSR/rootless support, including
[refind] / refind / config.c
1 /*
2 * refind/config.c
3 * Configuration file functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /*
38 * Modifications copyright (c) 2012-2015 Roderick W. Smith
39 *
40 * Modifications distributed under the terms of the GNU General Public
41 * License (GPL) version 3 (GPLv3) or (at your option) any later version.
42 *
43 */
44 /*
45 * This program is free software: you can redistribute it and/or modify
46 * it under the terms of the GNU General Public License as published by
47 * the Free Software Foundation, either version 3 of the License, or
48 * (at your option) any later version.
49 *
50 * This program is distributed in the hope that it will be useful,
51 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 * GNU General Public License for more details.
54 *
55 * You should have received a copy of the GNU General Public License
56 * along with this program. If not, see <http://www.gnu.org/licenses/>.
57 */
58
59 #include "global.h"
60 #include "lib.h"
61 #include "icns.h"
62 #include "menu.h"
63 #include "config.h"
64 #include "screen.h"
65 #include "apple.h"
66 #include "../include/refit_call_wrapper.h"
67 #include "../mok/mok.h"
68
69 // constants
70
71 #define LINUX_OPTIONS_FILENAMES L"refind_linux.conf,refind-linux.conf"
72 #define MAXCONFIGFILESIZE (128*1024)
73
74 #define ENCODING_ISO8859_1 (0)
75 #define ENCODING_UTF8 (1)
76 #define ENCODING_UTF16_LE (2)
77
78 #define GetTime ST->RuntimeServices->GetTime
79 #define LAST_MINUTE 1439 /* Last minute of a day */
80
81 extern REFIT_MENU_ENTRY MenuEntryReturn;
82 //static REFIT_MENU_ENTRY MenuEntryReturn = { L"Return to Main Menu", TAG_RETURN, 0, 0, 0, NULL, NULL, NULL };
83
84 //
85 // read a file into a buffer
86 //
87
88 EFI_STATUS ReadFile(IN EFI_FILE_HANDLE BaseDir, IN CHAR16 *FileName, IN OUT REFIT_FILE *File, OUT UINTN *size)
89 {
90 EFI_STATUS Status;
91 EFI_FILE_HANDLE FileHandle;
92 EFI_FILE_INFO *FileInfo;
93 UINT64 ReadSize;
94 CHAR16 Message[256];
95
96 File->Buffer = NULL;
97 File->BufferSize = 0;
98
99 // read the file, allocating a buffer on the way
100 Status = refit_call5_wrapper(BaseDir->Open, BaseDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
101 SPrint(Message, 255, L"while loading the file '%s'", FileName);
102 if (CheckError(Status, Message))
103 return Status;
104
105 FileInfo = LibFileInfo(FileHandle);
106 if (FileInfo == NULL) {
107 // TODO: print and register the error
108 refit_call1_wrapper(FileHandle->Close, FileHandle);
109 return EFI_LOAD_ERROR;
110 }
111 ReadSize = FileInfo->FileSize;
112 FreePool(FileInfo);
113
114 File->BufferSize = (UINTN)ReadSize;
115 File->Buffer = AllocatePool(File->BufferSize);
116 if (File->Buffer == NULL) {
117 size = 0;
118 return EFI_OUT_OF_RESOURCES;
119 } else {
120 *size = File->BufferSize;
121 } // if/else
122 Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &File->BufferSize, File->Buffer);
123 if (CheckError(Status, Message)) {
124 MyFreePool(File->Buffer);
125 File->Buffer = NULL;
126 refit_call1_wrapper(FileHandle->Close, FileHandle);
127 return Status;
128 }
129 Status = refit_call1_wrapper(FileHandle->Close, FileHandle);
130
131 // setup for reading
132 File->Current8Ptr = (CHAR8 *)File->Buffer;
133 File->End8Ptr = File->Current8Ptr + File->BufferSize;
134 File->Current16Ptr = (CHAR16 *)File->Buffer;
135 File->End16Ptr = File->Current16Ptr + (File->BufferSize >> 1);
136
137 // detect encoding
138 File->Encoding = ENCODING_ISO8859_1; // default: 1:1 translation of CHAR8 to CHAR16
139 if (File->BufferSize >= 4) {
140 if (File->Buffer[0] == 0xFF && File->Buffer[1] == 0xFE) {
141 // BOM in UTF-16 little endian (or UTF-32 little endian)
142 File->Encoding = ENCODING_UTF16_LE; // use CHAR16 as is
143 File->Current16Ptr++;
144 } else if (File->Buffer[0] == 0xEF && File->Buffer[1] == 0xBB && File->Buffer[2] == 0xBF) {
145 // BOM in UTF-8
146 File->Encoding = ENCODING_UTF8; // translate from UTF-8 to UTF-16
147 File->Current8Ptr += 3;
148 } else if (File->Buffer[1] == 0 && File->Buffer[3] == 0) {
149 File->Encoding = ENCODING_UTF16_LE; // use CHAR16 as is
150 }
151 // TODO: detect other encodings as they are implemented
152 }
153
154 return EFI_SUCCESS;
155 }
156
157 //
158 // get a single line of text from a file
159 //
160
161 static CHAR16 *ReadLine(REFIT_FILE *File)
162 {
163 CHAR16 *Line, *q;
164 UINTN LineLength;
165
166 if (File->Buffer == NULL)
167 return NULL;
168
169 if (File->Encoding == ENCODING_ISO8859_1 || File->Encoding == ENCODING_UTF8) {
170
171 CHAR8 *p, *LineStart, *LineEnd;
172
173 p = File->Current8Ptr;
174 if (p >= File->End8Ptr)
175 return NULL;
176
177 LineStart = p;
178 for (; p < File->End8Ptr; p++)
179 if (*p == 13 || *p == 10)
180 break;
181 LineEnd = p;
182 for (; p < File->End8Ptr; p++)
183 if (*p != 13 && *p != 10)
184 break;
185 File->Current8Ptr = p;
186
187 LineLength = (UINTN)(LineEnd - LineStart) + 1;
188 Line = AllocatePool(LineLength * sizeof(CHAR16));
189 if (Line == NULL)
190 return NULL;
191
192 q = Line;
193 if (File->Encoding == ENCODING_ISO8859_1) {
194 for (p = LineStart; p < LineEnd; )
195 *q++ = *p++;
196 } else if (File->Encoding == ENCODING_UTF8) {
197 // TODO: actually handle UTF-8
198 for (p = LineStart; p < LineEnd; )
199 *q++ = *p++;
200 }
201 *q = 0;
202
203 } else if (File->Encoding == ENCODING_UTF16_LE) {
204
205 CHAR16 *p, *LineStart, *LineEnd;
206
207 p = File->Current16Ptr;
208 if (p >= File->End16Ptr)
209 return NULL;
210
211 LineStart = p;
212 for (; p < File->End16Ptr; p++)
213 if (*p == 13 || *p == 10)
214 break;
215 LineEnd = p;
216 for (; p < File->End16Ptr; p++)
217 if (*p != 13 && *p != 10)
218 break;
219 File->Current16Ptr = p;
220
221 LineLength = (UINTN)(LineEnd - LineStart) + 1;
222 Line = AllocatePool(LineLength * sizeof(CHAR16));
223 if (Line == NULL)
224 return NULL;
225
226 for (p = LineStart, q = Line; p < LineEnd; )
227 *q++ = *p++;
228 *q = 0;
229
230 } else
231 return NULL; // unsupported encoding
232
233 return Line;
234 }
235
236 // Returns FALSE if *p points to the end of a token, TRUE otherwise.
237 // Also modifies *p **IF** the first and second characters are both
238 // quotes ('"'); it deletes one of them.
239 static BOOLEAN KeepReading(IN OUT CHAR16 *p, IN OUT BOOLEAN *IsQuoted) {
240 BOOLEAN MoreToRead = FALSE;
241 CHAR16 *Temp = NULL;
242
243 if ((p == NULL) || (IsQuoted == NULL))
244 return FALSE;
245
246 if (*p == L'\0')
247 return FALSE;
248
249 if ((*p != ' ' && *p != '\t' && *p != '=' && *p != '#' && *p != ',') || *IsQuoted) {
250 MoreToRead = TRUE;
251 }
252 if (*p == L'"') {
253 if (p[1] == L'"') {
254 Temp = StrDuplicate(&p[1]);
255 if (Temp != NULL) {
256 StrCpy(p, Temp);
257 FreePool(Temp);
258 }
259 MoreToRead = TRUE;
260 } else {
261 *IsQuoted = !(*IsQuoted);
262 MoreToRead = FALSE;
263 } // if/else second character is a quote
264 } // if first character is a quote
265
266 return MoreToRead;
267 } // BOOLEAN KeepReading()
268
269 //
270 // get a line of tokens from a file
271 //
272 UINTN ReadTokenLine(IN REFIT_FILE *File, OUT CHAR16 ***TokenList)
273 {
274 BOOLEAN LineFinished, IsQuoted = FALSE;
275 CHAR16 *Line, *Token, *p;
276 UINTN TokenCount = 0;
277
278 *TokenList = NULL;
279
280 while (TokenCount == 0) {
281 Line = ReadLine(File);
282 if (Line == NULL)
283 return(0);
284 if (Line[0] == L'\0') {
285 MyFreePool(Line);
286 return(0);
287 } // if
288
289 p = Line;
290 LineFinished = FALSE;
291 while (!LineFinished) {
292 // skip whitespace & find start of token
293 while ((*p == ' ' || *p == '\t' || *p == '=' || *p == ',') && !IsQuoted)
294 p++;
295 if (*p == 0 || *p == '#')
296 break;
297
298 if (*p == '"') {
299 IsQuoted = !IsQuoted;
300 p++;
301 } // if
302 Token = p;
303
304 // find end of token
305 while (KeepReading(p, &IsQuoted)) {
306 if ((*p == L'/') && !IsQuoted) // Switch Unix-style to DOS-style directory separators
307 *p = L'\\';
308 p++;
309 } // while
310 if (*p == L'\0' || *p == L'#')
311 LineFinished = TRUE;
312 *p++ = 0;
313
314 AddListElement((VOID ***)TokenList, &TokenCount, (VOID *)StrDuplicate(Token));
315 }
316
317 FreePool(Line);
318 }
319 return (TokenCount);
320 } /* ReadTokenLine() */
321
322 VOID FreeTokenLine(IN OUT CHAR16 ***TokenList, IN OUT UINTN *TokenCount)
323 {
324 // TODO: also free the items
325 FreeList((VOID ***)TokenList, TokenCount);
326 }
327
328 // handle a parameter with a single integer argument
329 static VOID HandleInt(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT UINTN *Value)
330 {
331 if (TokenCount == 2) {
332 if (StrCmp(TokenList[1], L"-1") == 0)
333 *Value = -1;
334 else
335 *Value = Atoi(TokenList[1]);
336 }
337 }
338
339 // handle a parameter with a single string argument
340 static VOID HandleString(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT CHAR16 **Target) {
341 if ((TokenCount == 2) && Target) {
342 if ((StrLen(TokenList[1]) > 1) && (TokenList[1][0] == L'+') &&
343 ((TokenList[1][1] == L',') || (TokenList[1][1] == L' '))) {
344 if (*Target) {
345 MergeStrings(Target, TokenList[1] + 2, L',');
346 } else {
347 *Target = StrDuplicate(TokenList[1] + 2);
348 } // if/else
349 } else {
350 MyFreePool(*Target);
351 *Target = StrDuplicate(TokenList[1]);
352 } // if/else
353 } // if
354 } // static VOID HandleString()
355
356 // Handle a parameter with a series of string arguments, to replace or be added to a
357 // comma-delimited list. Passes each token through the CleanUpPathNameSlashes() function
358 // to ensure consistency in subsequent comparisons of filenames. If the first
359 // non-keyword token is "+", the list is added to the existing target string; otherwise,
360 // the tokens replace the current string.
361 static VOID HandleStrings(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT CHAR16 **Target) {
362 UINTN i;
363 BOOLEAN AddMode = FALSE;
364
365 if ((TokenCount > 2) && (StrCmp(TokenList[1], L"+") == 0)) {
366 AddMode = TRUE;
367 }
368
369 if ((*Target != NULL) && !AddMode) {
370 FreePool(*Target);
371 *Target = NULL;
372 } // if
373 for (i = 1; i < TokenCount; i++) {
374 if ((i != 1) || !AddMode) {
375 CleanUpPathNameSlashes(TokenList[i]);
376 MergeStrings(Target, TokenList[i], L',');
377 } // if
378 } // for
379 } // static VOID HandleStrings()
380
381 // Handle a parameter with a series of hexadecimal arguments, to replace or be added to a
382 // linked list of UINT32 values. Any item with a non-hexadecimal value is discarded, as is
383 // any value that exceeds MaxValue. If the first non-keyword token is "+", the new list is
384 // added to the existing Target; otherwise, the interpreted tokens replace the current
385 // Target.
386 static VOID HandleHexes(IN CHAR16 **TokenList, IN UINTN TokenCount, IN UINTN MaxValue, OUT UINT32_LIST **Target) {
387 UINTN InputIndex = 1, i;
388 UINT32 Value;
389 UINT32_LIST *EndOfList = NULL;
390 UINT32_LIST *NewEntry;
391
392 if ((TokenCount > 2) && (StrCmp(TokenList[1], L"+") == 0)) {
393 InputIndex = 2;
394 EndOfList = *Target;
395 while (EndOfList && (EndOfList->Next != NULL)) {
396 EndOfList = EndOfList->Next;
397 }
398 } else {
399 EraseUint32List(Target);
400 }
401
402 for (i = InputIndex; i < TokenCount; i++) {
403 if (IsValidHex(TokenList[i])) {
404 Value = (UINT32) StrToHex(TokenList[i], 0, 8);
405 if (Value <= MaxValue) {
406 NewEntry = AllocatePool(sizeof(UINT32_LIST));
407 if (NewEntry) {
408 NewEntry->Value = Value;
409 NewEntry->Next = NULL;
410 if (EndOfList == NULL) {
411 EndOfList = NewEntry;
412 *Target = NewEntry;
413 } else {
414 EndOfList->Next = NewEntry;
415 EndOfList = NewEntry;
416 } // if/else
417 } // if allocated memory for NewEntry
418 } // if (Value < MaxValue)
419 } // if is valid hex value
420 } // for
421 } // static VOID HandleHexes()
422
423 // Convert TimeString (in "HH:MM" format) to a pure-minute format. Values should be
424 // in the range from 0 (for 00:00, or midnight) to 1439 (for 23:59; aka LAST_MINUTE).
425 // Any value outside that range denotes an error in the specification. Note that if
426 // the input is a number that includes no colon, this function will return the original
427 // number in UINTN form.
428 static UINTN HandleTime(IN CHAR16 *TimeString) {
429 UINTN Hour = 0, Minute = 0, TimeLength, i = 0;
430
431 TimeLength = StrLen(TimeString);
432 while (i < TimeLength) {
433 if (TimeString[i] == L':') {
434 Hour = Minute;
435 Minute = 0;
436 } // if
437 if ((TimeString[i] >= L'0') && (TimeString[i] <= '9')) {
438 Minute *= 10;
439 Minute += (TimeString[i] - L'0');
440 } // if
441 i++;
442 } // while
443 return (Hour * 60 + Minute);
444 } // BOOLEAN HandleTime()
445
446 static BOOLEAN HandleBoolean(IN CHAR16 **TokenList, IN UINTN TokenCount) {
447 BOOLEAN TruthValue = TRUE;
448
449 if ((TokenCount >= 2) && ((StrCmp(TokenList[1], L"0") == 0) ||
450 MyStriCmp(TokenList[1], L"false") ||
451 MyStriCmp(TokenList[1], L"off"))) {
452 TruthValue = FALSE;
453 } // if
454
455 return TruthValue;
456 } // BOOLEAN HandleBoolean
457
458 // Sets the default boot loader IF the current time is within the bounds
459 // defined by the third and fourth tokens in the TokenList.
460 static VOID SetDefaultByTime(IN CHAR16 **TokenList, OUT CHAR16 **Default) {
461 EFI_STATUS Status;
462 EFI_TIME CurrentTime;
463 UINTN StartTime, EndTime, Now;
464 BOOLEAN SetIt = FALSE;
465
466 StartTime = HandleTime(TokenList[2]);
467 EndTime = HandleTime(TokenList[3]);
468
469 if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE)) {
470 Status = refit_call2_wrapper(GetTime, &CurrentTime, NULL);
471 if (Status != EFI_SUCCESS)
472 return;
473 Now = CurrentTime.Hour * 60 + CurrentTime.Minute;
474
475 if (Now > LAST_MINUTE) { // Shouldn't happen; just being paranoid
476 Print(L"Warning: Impossible system time: %d:%d\n", CurrentTime.Hour, CurrentTime.Minute);
477 return;
478 } // if impossible time
479
480 if (StartTime < EndTime) { // Time range does NOT cross midnight
481 if ((Now >= StartTime) && (Now <= EndTime))
482 SetIt = TRUE;
483 } else { // Time range DOES cross midnight
484 if ((Now >= StartTime) && (Now <= EndTime))
485 SetIt = TRUE;
486 } // if/else time range crosses midnight
487
488 if (SetIt) {
489 MyFreePool(*Default);
490 *Default = StrDuplicate(TokenList[1]);
491 } // if (SetIt)
492 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
493 } // VOID SetDefaultByTime()
494
495 // read config file
496 VOID ReadConfig(CHAR16 *FileName)
497 {
498 EFI_STATUS Status;
499 REFIT_FILE File;
500 CHAR16 **TokenList;
501 CHAR16 *FlagName;
502 CHAR16 *TempStr = NULL;
503 UINTN TokenCount, i;
504 EFI_GUID RefindGuid = REFIND_GUID_VALUE;
505
506 // Set a few defaults only if we're loading the default file.
507 if (MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
508 MyFreePool(GlobalConfig.AlsoScan);
509 GlobalConfig.AlsoScan = StrDuplicate(ALSO_SCAN_DIRS);
510 MyFreePool(GlobalConfig.DontScanDirs);
511 if (SelfVolume) {
512 if (SelfVolume->VolName) {
513 TempStr = SelfVolume->VolName ? StrDuplicate(SelfVolume->VolName) : NULL;
514 } else {
515 TempStr = AllocateZeroPool(256 * sizeof(CHAR16));
516 if (TempStr != NULL)
517 SPrint(TempStr, 255, L"fs%d", SelfVolume->VolNumber);
518 } // if/else
519 }
520 MergeStrings(&TempStr, SelfDirPath, L':');
521 MergeStrings(&TempStr, MEMTEST_LOCATIONS, L',');
522 GlobalConfig.DontScanDirs = TempStr;
523 MyFreePool(GlobalConfig.DontScanFiles);
524 GlobalConfig.DontScanFiles = StrDuplicate(DONT_SCAN_FILES);
525 MergeStrings(&(GlobalConfig.DontScanFiles), MOK_NAMES, L',');
526 MyFreePool(GlobalConfig.DontScanVolumes);
527 GlobalConfig.DontScanVolumes = StrDuplicate(DONT_SCAN_VOLUMES);
528 GlobalConfig.WindowsRecoveryFiles = StrDuplicate(WINDOWS_RECOVERY_FILES);
529 if (GlobalConfig.DefaultSelection != NULL) {
530 MyFreePool(GlobalConfig.DefaultSelection);
531 GlobalConfig.DefaultSelection = NULL;
532 }
533 Status = EfivarGetRaw(&RefindGuid, L"PreviousBoot", (CHAR8**) &(GlobalConfig.DefaultSelection), &i);
534 if (Status != EFI_SUCCESS)
535 GlobalConfig.DefaultSelection = NULL;
536 } // if
537
538 if (!FileExists(SelfDir, FileName)) {
539 Print(L"Configuration file '%s' missing!\n", FileName);
540 if (!FileExists(SelfDir, L"icons")) {
541 Print(L"Icons directory doesn't exist; setting textonly = TRUE!\n");
542 GlobalConfig.TextOnly = TRUE;
543 }
544 return;
545 }
546
547 Status = ReadFile(SelfDir, FileName, &File, &i);
548 if (EFI_ERROR(Status))
549 return;
550
551 for (;;) {
552 TokenCount = ReadTokenLine(&File, &TokenList);
553 if (TokenCount == 0)
554 break;
555
556 if (MyStriCmp(TokenList[0], L"timeout")) {
557 HandleInt(TokenList, TokenCount, &(GlobalConfig.Timeout));
558
559 } else if (MyStriCmp(TokenList[0], L"hideui")) {
560 for (i = 1; i < TokenCount; i++) {
561 FlagName = TokenList[i];
562 if (MyStriCmp(FlagName, L"banner")) {
563 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BANNER;
564 } else if (MyStriCmp(FlagName, L"label")) {
565 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_LABEL;
566 } else if (MyStriCmp(FlagName, L"singleuser")) {
567 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_SINGLEUSER;
568 } else if (MyStriCmp(FlagName, L"hwtest")) {
569 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_HWTEST;
570 } else if (MyStriCmp(FlagName, L"arrows")) {
571 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_ARROWS;
572 } else if (MyStriCmp(FlagName, L"hints")) {
573 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_HINTS;
574 } else if (MyStriCmp(FlagName, L"editor")) {
575 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_EDITOR;
576 } else if (MyStriCmp(FlagName, L"safemode")) {
577 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_SAFEMODE;
578 } else if (MyStriCmp(FlagName, L"badges")) {
579 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BADGES;
580 } else if (MyStriCmp(FlagName, L"all")) {
581 GlobalConfig.HideUIFlags = HIDEUI_FLAG_ALL;
582 } else {
583 Print(L" unknown hideui flag: '%s'\n", FlagName);
584 }
585 }
586
587 } else if (MyStriCmp(TokenList[0], L"icons_dir")) {
588 HandleString(TokenList, TokenCount, &(GlobalConfig.IconsDir));
589
590 } else if (MyStriCmp(TokenList[0], L"scanfor")) {
591 for (i = 0; i < NUM_SCAN_OPTIONS; i++) {
592 if (i < TokenCount)
593 GlobalConfig.ScanFor[i] = TokenList[i][0];
594 else
595 GlobalConfig.ScanFor[i] = ' ';
596 }
597
598 } else if (MyStriCmp(TokenList[0], L"uefi_deep_legacy_scan")) {
599 GlobalConfig.DeepLegacyScan = HandleBoolean(TokenList, TokenCount);
600
601 } else if (MyStriCmp(TokenList[0], L"scan_delay") && (TokenCount == 2)) {
602 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScanDelay));
603
604 } else if (MyStriCmp(TokenList[0], L"also_scan_dirs")) {
605 HandleStrings(TokenList, TokenCount, &(GlobalConfig.AlsoScan));
606
607 } else if (MyStriCmp(TokenList[0], L"don't_scan_volumes") || MyStriCmp(TokenList[0], L"dont_scan_volumes")) {
608 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
609 MyFreePool(GlobalConfig.DontScanVolumes);
610 GlobalConfig.DontScanVolumes = NULL;
611 for (i = 1; i < TokenCount; i++) {
612 MergeStrings(&GlobalConfig.DontScanVolumes, TokenList[i], L',');
613 }
614
615 } else if (MyStriCmp(TokenList[0], L"don't_scan_dirs") || MyStriCmp(TokenList[0], L"dont_scan_dirs")) {
616 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DontScanDirs));
617
618 } else if (MyStriCmp(TokenList[0], L"don't_scan_files") || MyStriCmp(TokenList[0], L"dont_scan_files")) {
619 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DontScanFiles));
620
621 } else if (MyStriCmp(TokenList[0], L"windows_recovery_files")) {
622 HandleStrings(TokenList, TokenCount, &(GlobalConfig.WindowsRecoveryFiles));
623
624 } else if (MyStriCmp(TokenList[0], L"scan_driver_dirs")) {
625 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DriverDirs));
626
627 } else if (MyStriCmp(TokenList[0], L"showtools")) {
628 SetMem(GlobalConfig.ShowTools, NUM_TOOLS * sizeof(UINTN), 0);
629 for (i = 1; (i < TokenCount) && (i < NUM_TOOLS); i++) {
630 FlagName = TokenList[i];
631 if (MyStriCmp(FlagName, L"shell")) {
632 GlobalConfig.ShowTools[i - 1] = TAG_SHELL;
633 } else if (MyStriCmp(FlagName, L"gptsync")) {
634 GlobalConfig.ShowTools[i - 1] = TAG_GPTSYNC;
635 } else if (MyStriCmp(FlagName, L"gdisk")) {
636 GlobalConfig.ShowTools[i - 1] = TAG_GDISK;
637 } else if (MyStriCmp(FlagName, L"about")) {
638 GlobalConfig.ShowTools[i - 1] = TAG_ABOUT;
639 } else if (MyStriCmp(FlagName, L"exit")) {
640 GlobalConfig.ShowTools[i - 1] = TAG_EXIT;
641 } else if (MyStriCmp(FlagName, L"reboot")) {
642 GlobalConfig.ShowTools[i - 1] = TAG_REBOOT;
643 } else if (MyStriCmp(FlagName, L"shutdown")) {
644 GlobalConfig.ShowTools[i - 1] = TAG_SHUTDOWN;
645 } else if (MyStriCmp(FlagName, L"apple_recovery")) {
646 GlobalConfig.ShowTools[i - 1] = TAG_APPLE_RECOVERY;
647 } else if (MyStriCmp(FlagName, L"windows_recovery")) {
648 GlobalConfig.ShowTools[i - 1] = TAG_WINDOWS_RECOVERY;
649 } else if (MyStriCmp(FlagName, L"mok_tool")) {
650 GlobalConfig.ShowTools[i - 1] = TAG_MOK_TOOL;
651 } else if (MyStriCmp(FlagName, L"csr_rotate")) {
652 GlobalConfig.ShowTools[i - 1] = TAG_CSR_ROTATE;
653 } else if (MyStriCmp(FlagName, L"firmware")) {
654 GlobalConfig.ShowTools[i - 1] = TAG_FIRMWARE;
655 } else if (MyStriCmp(FlagName, L"memtest86") || MyStriCmp(FlagName, L"memtest")) {
656 GlobalConfig.ShowTools[i - 1] = TAG_MEMTEST;
657 } else if (MyStriCmp(FlagName, L"netboot")) {
658 GlobalConfig.ShowTools[i - 1] = TAG_NETBOOT;
659 } else {
660 Print(L" unknown showtools flag: '%s'\n", FlagName);
661 }
662 } // showtools options
663
664 } else if (MyStriCmp(TokenList[0], L"banner")) {
665 HandleString(TokenList, TokenCount, &(GlobalConfig.BannerFileName));
666
667 } else if (MyStriCmp(TokenList[0], L"banner_scale") && (TokenCount == 2)) {
668 if (MyStriCmp(TokenList[1], L"noscale")) {
669 GlobalConfig.BannerScale = BANNER_NOSCALE;
670 } else if (MyStriCmp(TokenList[1], L"fillscreen") || MyStriCmp(TokenList[1], L"fullscreen")) {
671 GlobalConfig.BannerScale = BANNER_FILLSCREEN;
672 } else {
673 Print(L" unknown banner_type flag: '%s'\n", TokenList[1]);
674 } // if/else
675
676 } else if (MyStriCmp(TokenList[0], L"small_icon_size") && (TokenCount == 2)) {
677 HandleInt(TokenList, TokenCount, &i);
678 if (i >= 32)
679 GlobalConfig.IconSizes[ICON_SIZE_SMALL] = i;
680
681 } else if (MyStriCmp(TokenList[0], L"big_icon_size") && (TokenCount == 2)) {
682 HandleInt(TokenList, TokenCount, &i);
683 if (i >= 32) {
684 GlobalConfig.IconSizes[ICON_SIZE_BIG] = i;
685 GlobalConfig.IconSizes[ICON_SIZE_BADGE] = i / 4;
686 }
687
688 } else if (MyStriCmp(TokenList[0], L"selection_small")) {
689 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionSmallFileName));
690
691 } else if (MyStriCmp(TokenList[0], L"selection_big")) {
692 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionBigFileName));
693
694 } else if (MyStriCmp(TokenList[0], L"default_selection")) {
695 if (TokenCount == 4) {
696 SetDefaultByTime(TokenList, &(GlobalConfig.DefaultSelection));
697 } else {
698 HandleString(TokenList, TokenCount, &(GlobalConfig.DefaultSelection));
699 }
700
701 } else if (MyStriCmp(TokenList[0], L"textonly")) {
702 GlobalConfig.TextOnly = HandleBoolean(TokenList, TokenCount);
703
704 } else if (MyStriCmp(TokenList[0], L"textmode")) {
705 HandleInt(TokenList, TokenCount, &(GlobalConfig.RequestedTextMode));
706
707 } else if (MyStriCmp(TokenList[0], L"resolution") && ((TokenCount == 2) || (TokenCount == 3))) {
708 GlobalConfig.RequestedScreenWidth = Atoi(TokenList[1]);
709 if (TokenCount == 3)
710 GlobalConfig.RequestedScreenHeight = Atoi(TokenList[2]);
711 else
712 GlobalConfig.RequestedScreenHeight = 0;
713
714 } else if (MyStriCmp(TokenList[0], L"screensaver")) {
715 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScreensaverTime));
716
717 } else if (MyStriCmp(TokenList[0], L"use_graphics_for")) {
718 if ((TokenCount == 2) || ((TokenCount > 2) && (!MyStriCmp(TokenList[1], L"+"))))
719 GlobalConfig.GraphicsFor = 0;
720 for (i = 1; i < TokenCount; i++) {
721 if (MyStriCmp(TokenList[i], L"osx")) {
722 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_OSX;
723 } else if (MyStriCmp(TokenList[i], L"linux")) {
724 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_LINUX;
725 } else if (MyStriCmp(TokenList[i], L"elilo")) {
726 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_ELILO;
727 } else if (MyStriCmp(TokenList[i], L"grub")) {
728 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_GRUB;
729 } else if (MyStriCmp(TokenList[i], L"windows")) {
730 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_WINDOWS;
731 }
732 } // for (graphics_on tokens)
733
734 } else if (MyStriCmp(TokenList[0], L"font") && (TokenCount == 2)) {
735 egLoadFont(TokenList[1]);
736
737 } else if (MyStriCmp(TokenList[0], L"scan_all_linux_kernels")) {
738 GlobalConfig.ScanAllLinux = HandleBoolean(TokenList, TokenCount);
739
740 } else if (MyStriCmp(TokenList[0], L"fold_linux_kernels")) {
741 GlobalConfig.FoldLinuxKernels = HandleBoolean(TokenList, TokenCount);
742
743 } else if (MyStriCmp(TokenList[0], L"max_tags")) {
744 HandleInt(TokenList, TokenCount, &(GlobalConfig.MaxTags));
745
746 } else if (MyStriCmp(TokenList[0], L"enable_and_lock_vmx")) {
747 GlobalConfig.EnableAndLockVMX = HandleBoolean(TokenList, TokenCount);
748
749 } else if (MyStriCmp(TokenList[0], L"spoof_osx_version")) {
750 HandleString(TokenList, TokenCount, &(GlobalConfig.SpoofOSXVersion));
751
752 } else if (MyStriCmp(TokenList[0], L"csr_values")) {
753 HandleHexes(TokenList, TokenCount, CSR_MAX_LEGAL_VALUE, &(GlobalConfig.CsrValues));
754
755 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) && MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
756 if (!MyStriCmp(TokenList[1], FileName)) {
757 ReadConfig(TokenList[1]);
758 }
759
760 }
761
762 FreeTokenLine(&TokenList, &TokenCount);
763 }
764 if ((GlobalConfig.DontScanFiles) && (GlobalConfig.WindowsRecoveryFiles))
765 MergeStrings(&(GlobalConfig.DontScanFiles), GlobalConfig.WindowsRecoveryFiles, L',');
766 MyFreePool(File.Buffer);
767
768 if (!FileExists(SelfDir, L"icons") && !FileExists(SelfDir, GlobalConfig.IconsDir)) {
769 Print(L"Icons directory doesn't exist; setting textonly = TRUE!\n");
770 GlobalConfig.TextOnly = TRUE;
771 }
772 } /* VOID ReadConfig() */
773
774 // Finds a volume with the specified Identifier (a filesystem label, a
775 // partition name, a partition GUID, or a number followed by a colon). If
776 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
777 // Returns TRUE if a match was found, FALSE if not.
778 static BOOLEAN FindVolume(REFIT_VOLUME **Volume, CHAR16 *Identifier) {
779 UINTN i = 0, CountedVolumes = 0, Length;
780 INTN Number = -1;
781 BOOLEAN Found = FALSE, IdIsGuid = FALSE;
782 EFI_GUID VolGuid, NullGuid = NULL_GUID_VALUE;
783
784 VolGuid = StringAsGuid(Identifier);
785 Length = StrLen(Identifier);
786 if ((Length >= 2) && (Identifier[Length - 1] == L':') &&
787 (Identifier[0] >= L'0') && (Identifier[0] <= L'9')) {
788 Number = (INTN) Atoi(Identifier);
789 } else if (IsGuid(Identifier)) {
790 IdIsGuid = TRUE;
791 }
792 while ((i < VolumesCount) && (!Found)) {
793 if (Number >= 0) { // User specified a volume by number
794 if (Volumes[i]->IsReadable) {
795 if (CountedVolumes == Number) {
796 *Volume = Volumes[i];
797 Found = TRUE;
798 }
799 CountedVolumes++;
800 } // if
801 } else { // User specified a volume by label or GUID
802 if (MyStriCmp(Identifier, Volumes[i]->VolName) || MyStriCmp(Identifier, Volumes[i]->PartName)) {
803 *Volume = Volumes[i];
804 Found = TRUE;
805 } // if
806 if (IdIsGuid && !Found) {
807 if (GuidsAreEqual(&VolGuid, &(Volumes[i]->PartGuid)) && !GuidsAreEqual(&NullGuid, &(Volumes[i]->PartGuid))) {
808 *Volume = Volumes[i];
809 Found = TRUE;
810 } // if
811 } // if
812 } // if/else
813 i++;
814 } // while()
815 return (Found);
816 } // static VOID FindVolume()
817
818 static VOID AddSubmenu(LOADER_ENTRY *Entry, REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
819 REFIT_MENU_SCREEN *SubScreen;
820 LOADER_ENTRY *SubEntry;
821 UINTN TokenCount;
822 CHAR16 **TokenList;
823
824 SubScreen = InitializeSubScreen(Entry);
825
826 // Set defaults for the new entry; will be modified based on lines read from the config. file....
827 SubEntry = InitializeLoaderEntry(Entry);
828
829 if ((SubEntry == NULL) || (SubScreen == NULL))
830 return;
831 SubEntry->me.Title = StrDuplicate(Title);
832
833 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
834
835 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
836 MyFreePool(SubEntry->LoaderPath);
837 SubEntry->LoaderPath = StrDuplicate(TokenList[1]);
838 SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
839
840 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
841 if (FindVolume(&Volume, TokenList[1])) {
842 if ((Volume != NULL) && (Volume->IsReadable) && (Volume->RootDir)) {
843 MyFreePool(SubEntry->me.Title);
844 SubEntry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
845 SPrint(SubEntry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", Volume->VolName);
846 SubEntry->me.BadgeImage = Volume->VolBadgeImage;
847 SubEntry->VolName = Volume->VolName;
848 } // if volume is readable
849 } // if match found
850
851 } else if (MyStriCmp(TokenList[0], L"initrd")) {
852 MyFreePool(SubEntry->InitrdPath);
853 SubEntry->InitrdPath = NULL;
854 if (TokenCount > 1) {
855 SubEntry->InitrdPath = StrDuplicate(TokenList[1]);
856 }
857
858 } else if (MyStriCmp(TokenList[0], L"options")) {
859 MyFreePool(SubEntry->LoadOptions);
860 SubEntry->LoadOptions = NULL;
861 if (TokenCount > 1) {
862 SubEntry->LoadOptions = StrDuplicate(TokenList[1]);
863 } // if/else
864
865 } else if (MyStriCmp(TokenList[0], L"add_options") && (TokenCount > 1)) {
866 MergeStrings(&SubEntry->LoadOptions, TokenList[1], L' ');
867
868 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
869 SubEntry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
870
871 } else if (MyStriCmp(TokenList[0], L"disabled")) {
872 SubEntry->Enabled = FALSE;
873 } // ief/elseif
874
875 FreeTokenLine(&TokenList, &TokenCount);
876 } // while()
877
878 if (SubEntry->InitrdPath != NULL) {
879 MergeStrings(&SubEntry->LoadOptions, L"initrd=", L' ');
880 MergeStrings(&SubEntry->LoadOptions, SubEntry->InitrdPath, 0);
881 MyFreePool(SubEntry->InitrdPath);
882 SubEntry->InitrdPath = NULL;
883 } // if
884 if (SubEntry->Enabled == TRUE) {
885 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
886 }
887 Entry->me.SubScreen = SubScreen;
888 } // VOID AddSubmenu()
889
890 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
891 // that entry. The calling function is then responsible for adding the entry to the
892 // list of entries.
893 static LOADER_ENTRY * AddStanzaEntries(REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
894 CHAR16 **TokenList;
895 UINTN TokenCount;
896 LOADER_ENTRY *Entry;
897 BOOLEAN DefaultsSet = FALSE, AddedSubmenu = FALSE;
898 REFIT_VOLUME *CurrentVolume = Volume;
899
900 // prepare the menu entry
901 Entry = InitializeLoaderEntry(NULL);
902 if (Entry == NULL)
903 return NULL;
904
905 Entry->Title = StrDuplicate(Title);
906 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
907 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
908 Entry->me.Row = 0;
909 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
910 Entry->VolName = CurrentVolume->VolName;
911
912 // Parse the config file to add options for a single stanza, terminating when the token
913 // is "}" or when the end of file is reached.
914 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
915 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
916 Entry->LoaderPath = StrDuplicate(TokenList[1]);
917 Entry->DevicePath = FileDevicePath(CurrentVolume->DeviceHandle, Entry->LoaderPath);
918 SetLoaderDefaults(Entry, TokenList[1], CurrentVolume);
919 MyFreePool(Entry->LoadOptions);
920 Entry->LoadOptions = NULL; // Discard default options, if any
921 DefaultsSet = TRUE;
922
923 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
924 if (FindVolume(&CurrentVolume, TokenList[1])) {
925 if ((CurrentVolume != NULL) && (CurrentVolume->IsReadable) && (CurrentVolume->RootDir)) {
926 MyFreePool(Entry->me.Title);
927 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
928 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
929 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
930 Entry->VolName = CurrentVolume->VolName;
931 } // if volume is readable
932 } // if match found
933
934 } else if (MyStriCmp(TokenList[0], L"icon") && (TokenCount > 1)) {
935 MyFreePool(Entry->me.Image);
936 Entry->me.Image = egLoadIcon(CurrentVolume->RootDir, TokenList[1], GlobalConfig.IconSizes[ICON_SIZE_BIG]);
937 if (Entry->me.Image == NULL) {
938 Entry->me.Image = DummyImage(GlobalConfig.IconSizes[ICON_SIZE_BIG]);
939 }
940
941 } else if (MyStriCmp(TokenList[0], L"initrd") && (TokenCount > 1)) {
942 MyFreePool(Entry->InitrdPath);
943 Entry->InitrdPath = StrDuplicate(TokenList[1]);
944
945 } else if (MyStriCmp(TokenList[0], L"options") && (TokenCount > 1)) {
946 MyFreePool(Entry->LoadOptions);
947 Entry->LoadOptions = StrDuplicate(TokenList[1]);
948
949 } else if (MyStriCmp(TokenList[0], L"ostype") && (TokenCount > 1)) {
950 if (TokenCount > 1) {
951 Entry->OSType = TokenList[1][0];
952 }
953
954 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
955 Entry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
956
957 } else if (MyStriCmp(TokenList[0], L"disabled")) {
958 Entry->Enabled = FALSE;
959
960 } else if (MyStriCmp(TokenList[0], L"submenuentry") && (TokenCount > 1)) {
961 AddSubmenu(Entry, File, CurrentVolume, TokenList[1]);
962 AddedSubmenu = TRUE;
963
964 } // set options to pass to the loader program
965 FreeTokenLine(&TokenList, &TokenCount);
966 } // while()
967
968 if (AddedSubmenu)
969 AddMenuEntry(Entry->me.SubScreen, &MenuEntryReturn);
970
971 if (Entry->InitrdPath) {
972 MergeStrings(&Entry->LoadOptions, L"initrd=", L' ');
973 MergeStrings(&Entry->LoadOptions, Entry->InitrdPath, 0);
974 MyFreePool(Entry->InitrdPath);
975 Entry->InitrdPath = NULL;
976 } // if
977
978 if (!DefaultsSet)
979 SetLoaderDefaults(Entry, L"\\EFI\\BOOT\\nemo.efi", CurrentVolume); // user included no "loader" line; use bogus one
980
981 return(Entry);
982 } // static VOID AddStanzaEntries()
983
984 // Read the user-configured menu entries from refind.conf and add or delete
985 // entries based on the contents of that file....
986 VOID ScanUserConfigured(CHAR16 *FileName)
987 {
988 EFI_STATUS Status;
989 REFIT_FILE File;
990 REFIT_VOLUME *Volume;
991 CHAR16 **TokenList;
992 CHAR16 *Title = NULL;
993 UINTN TokenCount, size;
994 LOADER_ENTRY *Entry;
995
996 if (FileExists(SelfDir, FileName)) {
997 Status = ReadFile(SelfDir, FileName, &File, &size);
998 if (EFI_ERROR(Status))
999 return;
1000
1001 Volume = SelfVolume;
1002
1003 while ((TokenCount = ReadTokenLine(&File, &TokenList)) > 0) {
1004 if (MyStriCmp(TokenList[0], L"menuentry") && (TokenCount > 1)) {
1005 Title = StrDuplicate(TokenList[1]);
1006 Entry = AddStanzaEntries(&File, Volume, TokenList[1]);
1007 if (Entry->Enabled) {
1008 if (Entry->me.SubScreen == NULL)
1009 GenerateSubScreen(Entry, Volume, TRUE);
1010 AddPreparedLoaderEntry(Entry);
1011 } else {
1012 MyFreePool(Entry);
1013 } // if/else
1014 MyFreePool(Title);
1015
1016 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) &&
1017 MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
1018 if (!MyStriCmp(TokenList[1], FileName)) {
1019 ScanUserConfigured(TokenList[1]);
1020 }
1021
1022 } // if/else if...
1023 FreeTokenLine(&TokenList, &TokenCount);
1024 } // while()
1025 } // if()
1026 } // VOID ScanUserConfigured()
1027
1028 // Create an options file based on /etc/fstab. The resulting file has two options
1029 // lines, one of which boots the system with "ro root={rootfs}" and the other of
1030 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
1031 // filesystem identifier associated with the "/" line in /etc/fstab.
1032 static REFIT_FILE * GenerateOptionsFromEtcFstab(REFIT_VOLUME *Volume) {
1033 UINTN TokenCount, i;
1034 REFIT_FILE *Options = NULL, *Fstab = NULL;
1035 EFI_STATUS Status;
1036 CHAR16 **TokenList, *Line, Root[100];
1037
1038 if (FileExists(Volume->RootDir, L"\\etc\\fstab")) {
1039 Options = AllocateZeroPool(sizeof(REFIT_FILE));
1040 Fstab = AllocateZeroPool(sizeof(REFIT_FILE));
1041 Status = ReadFile(Volume->RootDir, L"\\etc\\fstab", Fstab, &i);
1042 if (CheckError(Status, L"while reading /etc/fstab")) {
1043 if (Options != NULL)
1044 FreePool(Options);
1045 if (Fstab != NULL)
1046 FreePool(Fstab);
1047 Options = NULL;
1048 Fstab = NULL;
1049 } else { // File read; locate root fs and create entries
1050 Options->Encoding = ENCODING_UTF16_LE;
1051 while ((TokenCount = ReadTokenLine(Fstab, &TokenList)) > 0) {
1052 if (TokenCount > 2) {
1053 Root[0] = '\0';
1054 if (StrCmp(TokenList[1], L"\\") == 0) {
1055 SPrint(Root, 99, L"%s", TokenList[0]);
1056 } else if (StrCmp(TokenList[2], L"\\") == 0) {
1057 SPrint(Root, 99, L"%s=%s", TokenList[0], TokenList[1]);
1058 } // if/elseif/elseif
1059 if (Root[0] != L'\0') {
1060 for (i = 0; i < StrLen(Root); i++)
1061 if (Root[i] == '\\')
1062 Root[i] = '/';
1063 Line = PoolPrint(L"\"Boot with normal options\" \"ro root=%s\"\n", Root);
1064 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1065 MyFreePool(Line);
1066 Line = PoolPrint(L"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root);
1067 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1068 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1069 } // if
1070 } // if
1071 FreeTokenLine(&TokenList, &TokenCount);
1072 } // while
1073
1074 if (Options->Buffer) {
1075 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1076 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1077 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1078 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1079 } else {
1080 MyFreePool(Options);
1081 Options = NULL;
1082 }
1083
1084 MyFreePool(Fstab->Buffer);
1085 MyFreePool(Fstab);
1086 } // if/else file read error
1087 } // if /etc/fstab exists
1088 return Options;
1089 } // GenerateOptionsFromEtcFstab()
1090
1091 // Create options from partition type codes. Specifically, if the earlier
1092 // partition scan found a partition with a type code corresponding to a root
1093 // filesystem according to the Freedesktop.org Discoverable Partitions Spec
1094 // (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/),
1095 // this function returns an appropriate file with two lines, one with
1096 // "ro root=/dev/disk/by-partuuid/{GUID}" and the other with that plus "single".
1097 // Note that this function returns the LAST partition found with the
1098 // appropriate type code, so this will work poorly on dual-boot systems or
1099 // if the type code is set incorrectly.
1100 static REFIT_FILE * GenerateOptionsFromPartTypes(VOID) {
1101 REFIT_FILE *Options = NULL;
1102 CHAR16 *Line, *GuidString, *WriteStatus;
1103
1104 if (GlobalConfig.DiscoveredRoot) {
1105 Options = AllocateZeroPool(sizeof(REFIT_FILE));
1106 if (Options) {
1107 Options->Encoding = ENCODING_UTF16_LE;
1108 GuidString = GuidAsString(&(GlobalConfig.DiscoveredRoot->PartGuid));
1109 WriteStatus = GlobalConfig.DiscoveredRoot->IsMarkedReadOnly ? L"ro" : L"rw";
1110 ToLower(GuidString);
1111 if (GuidString) {
1112 Line = PoolPrint(L"\"Boot with normal options\" \"%s root=/dev/disk/by-partuuid/%s\"\n", WriteStatus, GuidString);
1113 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1114 MyFreePool(Line);
1115 Line = PoolPrint(L"\"Boot into single-user mode\" \"%s root=/dev/disk/by-partuuid/%s single\"\n", WriteStatus, GuidString);
1116 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1117 MyFreePool(Line);
1118 MyFreePool(GuidString);
1119 } // if (GuidString)
1120 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1121
1122 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1123 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1124 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1125 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1126 } // if (Options allocated OK)
1127 } // if (partition has root GUID)
1128 return Options;
1129 } // REFIT_FILE * GenerateOptionsFromPartTypes()
1130
1131 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1132 // and Volume variables identify the location of the options file, but not its name --
1133 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1134 // file in the target directory, and this function finds the file with a name in the
1135 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1136 // directory and loads it. This function tries multiple files because I originally
1137 // used the filename linux.conf, but close on the heels of that decision, the Linux
1138 // kernel developers decided to use that name for a similar purpose, but with a
1139 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1140 // but I want a migration period in which both names are used.
1141 // If a rEFInd options file can't be found, try to generate minimal options from
1142 // /etc/fstab on the same volume as the kernel. This typically works only if the
1143 // kernel is being read from the Linux root filesystem.
1144 //
1145 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1146 // it wasn't found.
1147 REFIT_FILE * ReadLinuxOptionsFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1148 CHAR16 *OptionsFilename, *FullFilename;
1149 BOOLEAN GoOn = TRUE, FileFound = FALSE;
1150 UINTN i = 0, size;
1151 REFIT_FILE *File = NULL;
1152 EFI_STATUS Status;
1153
1154 do {
1155 OptionsFilename = FindCommaDelimited(LINUX_OPTIONS_FILENAMES, i++);
1156 FullFilename = FindPath(LoaderPath);
1157 if ((OptionsFilename != NULL) && (FullFilename != NULL)) {
1158 MergeStrings(&FullFilename, OptionsFilename, '\\');
1159 if (FileExists(Volume->RootDir, FullFilename)) {
1160 File = AllocateZeroPool(sizeof(REFIT_FILE));
1161 Status = ReadFile(Volume->RootDir, FullFilename, File, &size);
1162 if (CheckError(Status, L"while loading the Linux options file")) {
1163 if (File != NULL)
1164 FreePool(File);
1165 File = NULL;
1166 } else {
1167 GoOn = FALSE;
1168 FileFound = TRUE;
1169 } // if/else error
1170 } // if file exists
1171 } else { // a filename string is NULL
1172 GoOn = FALSE;
1173 } // if/else
1174 MyFreePool(OptionsFilename);
1175 MyFreePool(FullFilename);
1176 OptionsFilename = FullFilename = NULL;
1177 } while (GoOn);
1178 if (!FileFound) {
1179 // No refind_linux.conf file; look for /etc/fstab and try to pull values from there....
1180 File = GenerateOptionsFromEtcFstab(Volume);
1181 // If still no joy, try to use Freedesktop.org Discoverable Partitions Spec....
1182 if (!File)
1183 File = GenerateOptionsFromPartTypes();
1184 } // if
1185 return (File);
1186 } // static REFIT_FILE * ReadLinuxOptionsFile()
1187
1188 // Retrieve a single line of options from a Linux kernel options file
1189 CHAR16 * GetFirstOptionsFromFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1190 UINTN TokenCount;
1191 CHAR16 *Options = NULL;
1192 CHAR16 **TokenList;
1193 REFIT_FILE *File;
1194
1195 File = ReadLinuxOptionsFile(LoaderPath, Volume);
1196 if (File != NULL) {
1197 TokenCount = ReadTokenLine(File, &TokenList);
1198 if (TokenCount > 1)
1199 Options = StrDuplicate(TokenList[1]);
1200 FreeTokenLine(&TokenList, &TokenCount);
1201 FreePool(File);
1202 } // if
1203 return Options;
1204 } // static CHAR16 * GetOptionsFile()
1205