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