]> code.delx.au - refind/blob - refind/config.c
Fixed bug that caused volume icons to be read only from default icons
[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 MyFreePool(GlobalConfig.DontScanVolumes);
528 GlobalConfig.DontScanVolumes = StrDuplicate(DONT_SCAN_VOLUMES);
529 GlobalConfig.WindowsRecoveryFiles = StrDuplicate(WINDOWS_RECOVERY_FILES);
530 if (GlobalConfig.DefaultSelection != NULL) {
531 MyFreePool(GlobalConfig.DefaultSelection);
532 GlobalConfig.DefaultSelection = NULL;
533 }
534 Status = EfivarGetRaw(&RefindGuid, L"PreviousBoot", (CHAR8**) &(GlobalConfig.DefaultSelection), &i);
535 if (Status != EFI_SUCCESS)
536 GlobalConfig.DefaultSelection = NULL;
537 } // if
538
539 if (!FileExists(SelfDir, FileName)) {
540 Print(L"Configuration file '%s' missing!\n", FileName);
541 if (!FileExists(SelfDir, L"icons")) {
542 Print(L"Icons directory doesn't exist; setting textonly = TRUE!\n");
543 GlobalConfig.TextOnly = TRUE;
544 }
545 return;
546 }
547
548 Status = ReadFile(SelfDir, FileName, &File, &i);
549 if (EFI_ERROR(Status))
550 return;
551
552 for (;;) {
553 TokenCount = ReadTokenLine(&File, &TokenList);
554 if (TokenCount == 0)
555 break;
556
557 if (MyStriCmp(TokenList[0], L"timeout")) {
558 HandleInt(TokenList, TokenCount, &(GlobalConfig.Timeout));
559
560 } else if (MyStriCmp(TokenList[0], L"hideui")) {
561 for (i = 1; i < TokenCount; i++) {
562 FlagName = TokenList[i];
563 if (MyStriCmp(FlagName, L"banner")) {
564 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BANNER;
565 } else if (MyStriCmp(FlagName, L"label")) {
566 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_LABEL;
567 } else if (MyStriCmp(FlagName, L"singleuser")) {
568 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_SINGLEUSER;
569 } else if (MyStriCmp(FlagName, L"hwtest")) {
570 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_HWTEST;
571 } else if (MyStriCmp(FlagName, L"arrows")) {
572 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_ARROWS;
573 } else if (MyStriCmp(FlagName, L"hints")) {
574 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_HINTS;
575 } else if (MyStriCmp(FlagName, L"editor")) {
576 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_EDITOR;
577 } else if (MyStriCmp(FlagName, L"safemode")) {
578 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_SAFEMODE;
579 } else if (MyStriCmp(FlagName, L"badges")) {
580 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BADGES;
581 } else if (MyStriCmp(FlagName, L"all")) {
582 GlobalConfig.HideUIFlags = HIDEUI_FLAG_ALL;
583 } else {
584 Print(L" unknown hideui flag: '%s'\n", FlagName);
585 }
586 }
587
588 } else if (MyStriCmp(TokenList[0], L"icons_dir")) {
589 HandleString(TokenList, TokenCount, &(GlobalConfig.IconsDir));
590
591 } else if (MyStriCmp(TokenList[0], L"scanfor")) {
592 for (i = 0; i < NUM_SCAN_OPTIONS; i++) {
593 if (i < TokenCount)
594 GlobalConfig.ScanFor[i] = TokenList[i][0];
595 else
596 GlobalConfig.ScanFor[i] = ' ';
597 }
598
599 } else if (MyStriCmp(TokenList[0], L"uefi_deep_legacy_scan")) {
600 GlobalConfig.DeepLegacyScan = HandleBoolean(TokenList, TokenCount);
601
602 } else if (MyStriCmp(TokenList[0], L"scan_delay") && (TokenCount == 2)) {
603 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScanDelay));
604
605 } else if (MyStriCmp(TokenList[0], L"also_scan_dirs")) {
606 HandleStrings(TokenList, TokenCount, &(GlobalConfig.AlsoScan));
607
608 } else if (MyStriCmp(TokenList[0], L"don't_scan_volumes") || MyStriCmp(TokenList[0], L"dont_scan_volumes")) {
609 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
610 MyFreePool(GlobalConfig.DontScanVolumes);
611 GlobalConfig.DontScanVolumes = NULL;
612 for (i = 1; i < TokenCount; i++) {
613 MergeStrings(&GlobalConfig.DontScanVolumes, TokenList[i], L',');
614 }
615
616 } else if (MyStriCmp(TokenList[0], L"don't_scan_dirs") || MyStriCmp(TokenList[0], L"dont_scan_dirs")) {
617 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DontScanDirs));
618
619 } else if (MyStriCmp(TokenList[0], L"don't_scan_files") || MyStriCmp(TokenList[0], L"dont_scan_files")) {
620 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DontScanFiles));
621
622 } else if (MyStriCmp(TokenList[0], L"windows_recovery_files")) {
623 HandleStrings(TokenList, TokenCount, &(GlobalConfig.WindowsRecoveryFiles));
624
625 } else if (MyStriCmp(TokenList[0], L"scan_driver_dirs")) {
626 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DriverDirs));
627
628 } else if (MyStriCmp(TokenList[0], L"showtools")) {
629 SetMem(GlobalConfig.ShowTools, NUM_TOOLS * sizeof(UINTN), 0);
630 for (i = 1; (i < TokenCount) && (i < NUM_TOOLS); i++) {
631 FlagName = TokenList[i];
632 if (MyStriCmp(FlagName, L"shell")) {
633 GlobalConfig.ShowTools[i - 1] = TAG_SHELL;
634 } else if (MyStriCmp(FlagName, L"gptsync")) {
635 GlobalConfig.ShowTools[i - 1] = TAG_GPTSYNC;
636 } else if (MyStriCmp(FlagName, L"gdisk")) {
637 GlobalConfig.ShowTools[i - 1] = TAG_GDISK;
638 } else if (MyStriCmp(FlagName, L"about")) {
639 GlobalConfig.ShowTools[i - 1] = TAG_ABOUT;
640 } else if (MyStriCmp(FlagName, L"exit")) {
641 GlobalConfig.ShowTools[i - 1] = TAG_EXIT;
642 } else if (MyStriCmp(FlagName, L"reboot")) {
643 GlobalConfig.ShowTools[i - 1] = TAG_REBOOT;
644 } else if (MyStriCmp(FlagName, L"shutdown")) {
645 GlobalConfig.ShowTools[i - 1] = TAG_SHUTDOWN;
646 } else if (MyStriCmp(FlagName, L"apple_recovery")) {
647 GlobalConfig.ShowTools[i - 1] = TAG_APPLE_RECOVERY;
648 } else if (MyStriCmp(FlagName, L"windows_recovery")) {
649 GlobalConfig.ShowTools[i - 1] = TAG_WINDOWS_RECOVERY;
650 } else if (MyStriCmp(FlagName, L"mok_tool")) {
651 GlobalConfig.ShowTools[i - 1] = TAG_MOK_TOOL;
652 } else if (MyStriCmp(FlagName, L"csr_rotate")) {
653 GlobalConfig.ShowTools[i - 1] = TAG_CSR_ROTATE;
654 } else if (MyStriCmp(FlagName, L"firmware")) {
655 GlobalConfig.ShowTools[i - 1] = TAG_FIRMWARE;
656 } else if (MyStriCmp(FlagName, L"memtest86") || MyStriCmp(FlagName, L"memtest")) {
657 GlobalConfig.ShowTools[i - 1] = TAG_MEMTEST;
658 } else if (MyStriCmp(FlagName, L"netboot")) {
659 GlobalConfig.ShowTools[i - 1] = TAG_NETBOOT;
660 } else {
661 Print(L" unknown showtools flag: '%s'\n", FlagName);
662 }
663 } // showtools options
664
665 } else if (MyStriCmp(TokenList[0], L"banner")) {
666 HandleString(TokenList, TokenCount, &(GlobalConfig.BannerFileName));
667
668 } else if (MyStriCmp(TokenList[0], L"banner_scale") && (TokenCount == 2)) {
669 if (MyStriCmp(TokenList[1], L"noscale")) {
670 GlobalConfig.BannerScale = BANNER_NOSCALE;
671 } else if (MyStriCmp(TokenList[1], L"fillscreen") || MyStriCmp(TokenList[1], L"fullscreen")) {
672 GlobalConfig.BannerScale = BANNER_FILLSCREEN;
673 } else {
674 Print(L" unknown banner_type flag: '%s'\n", TokenList[1]);
675 } // if/else
676
677 } else if (MyStriCmp(TokenList[0], L"small_icon_size") && (TokenCount == 2)) {
678 HandleInt(TokenList, TokenCount, &i);
679 if (i >= 32)
680 GlobalConfig.IconSizes[ICON_SIZE_SMALL] = i;
681
682 } else if (MyStriCmp(TokenList[0], L"big_icon_size") && (TokenCount == 2)) {
683 HandleInt(TokenList, TokenCount, &i);
684 if (i >= 32) {
685 GlobalConfig.IconSizes[ICON_SIZE_BIG] = i;
686 GlobalConfig.IconSizes[ICON_SIZE_BADGE] = i / 4;
687 }
688
689 } else if (MyStriCmp(TokenList[0], L"selection_small")) {
690 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionSmallFileName));
691
692 } else if (MyStriCmp(TokenList[0], L"selection_big")) {
693 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionBigFileName));
694
695 } else if (MyStriCmp(TokenList[0], L"default_selection")) {
696 if (TokenCount == 4) {
697 SetDefaultByTime(TokenList, &(GlobalConfig.DefaultSelection));
698 } else {
699 HandleString(TokenList, TokenCount, &(GlobalConfig.DefaultSelection));
700 }
701
702 } else if (MyStriCmp(TokenList[0], L"textonly")) {
703 GlobalConfig.TextOnly = HandleBoolean(TokenList, TokenCount);
704
705 } else if (MyStriCmp(TokenList[0], L"textmode")) {
706 HandleInt(TokenList, TokenCount, &(GlobalConfig.RequestedTextMode));
707
708 } else if (MyStriCmp(TokenList[0], L"resolution") && ((TokenCount == 2) || (TokenCount == 3))) {
709 GlobalConfig.RequestedScreenWidth = Atoi(TokenList[1]);
710 if (TokenCount == 3)
711 GlobalConfig.RequestedScreenHeight = Atoi(TokenList[2]);
712 else
713 GlobalConfig.RequestedScreenHeight = 0;
714
715 } else if (MyStriCmp(TokenList[0], L"screensaver")) {
716 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScreensaverTime));
717
718 } else if (MyStriCmp(TokenList[0], L"use_graphics_for")) {
719 if ((TokenCount == 2) || ((TokenCount > 2) && (!MyStriCmp(TokenList[1], L"+"))))
720 GlobalConfig.GraphicsFor = 0;
721 for (i = 1; i < TokenCount; i++) {
722 if (MyStriCmp(TokenList[i], L"osx")) {
723 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_OSX;
724 } else if (MyStriCmp(TokenList[i], L"linux")) {
725 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_LINUX;
726 } else if (MyStriCmp(TokenList[i], L"elilo")) {
727 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_ELILO;
728 } else if (MyStriCmp(TokenList[i], L"grub")) {
729 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_GRUB;
730 } else if (MyStriCmp(TokenList[i], L"windows")) {
731 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_WINDOWS;
732 }
733 } // for (graphics_on tokens)
734
735 } else if (MyStriCmp(TokenList[0], L"font") && (TokenCount == 2)) {
736 egLoadFont(TokenList[1]);
737
738 } else if (MyStriCmp(TokenList[0], L"scan_all_linux_kernels")) {
739 GlobalConfig.ScanAllLinux = HandleBoolean(TokenList, TokenCount);
740
741 } else if (MyStriCmp(TokenList[0], L"fold_linux_kernels")) {
742 GlobalConfig.FoldLinuxKernels = HandleBoolean(TokenList, TokenCount);
743
744 } else if (MyStriCmp(TokenList[0], L"max_tags")) {
745 HandleInt(TokenList, TokenCount, &(GlobalConfig.MaxTags));
746
747 } else if (MyStriCmp(TokenList[0], L"enable_and_lock_vmx")) {
748 GlobalConfig.EnableAndLockVMX = HandleBoolean(TokenList, TokenCount);
749
750 } else if (MyStriCmp(TokenList[0], L"spoof_osx_version")) {
751 HandleString(TokenList, TokenCount, &(GlobalConfig.SpoofOSXVersion));
752
753 } else if (MyStriCmp(TokenList[0], L"csr_values")) {
754 HandleHexes(TokenList, TokenCount, CSR_MAX_LEGAL_VALUE, &(GlobalConfig.CsrValues));
755
756 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) && MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
757 if (!MyStriCmp(TokenList[1], FileName)) {
758 ReadConfig(TokenList[1]);
759 }
760
761 }
762
763 FreeTokenLine(&TokenList, &TokenCount);
764 }
765 if ((GlobalConfig.DontScanFiles) && (GlobalConfig.WindowsRecoveryFiles))
766 MergeStrings(&(GlobalConfig.DontScanFiles), GlobalConfig.WindowsRecoveryFiles, L',');
767 MyFreePool(File.Buffer);
768
769 if (!FileExists(SelfDir, L"icons") && !FileExists(SelfDir, GlobalConfig.IconsDir)) {
770 Print(L"Icons directory doesn't exist; setting textonly = TRUE!\n");
771 GlobalConfig.TextOnly = TRUE;
772 }
773 } /* VOID ReadConfig() */
774
775 // Finds a volume with the specified Identifier (a filesystem label, a
776 // partition name, a partition GUID, or a number followed by a colon). If
777 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
778 // Returns TRUE if a match was found, FALSE if not.
779 static BOOLEAN FindVolume(REFIT_VOLUME **Volume, CHAR16 *Identifier) {
780 UINTN i = 0, CountedVolumes = 0, Length;
781 INTN Number = -1;
782 BOOLEAN Found = FALSE, IdIsGuid = FALSE;
783 EFI_GUID VolGuid, NullGuid = NULL_GUID_VALUE;
784
785 VolGuid = StringAsGuid(Identifier);
786 Length = StrLen(Identifier);
787 if ((Length >= 2) && (Identifier[Length - 1] == L':') &&
788 (Identifier[0] >= L'0') && (Identifier[0] <= L'9')) {
789 Number = (INTN) Atoi(Identifier);
790 } else if (IsGuid(Identifier)) {
791 IdIsGuid = TRUE;
792 }
793 while ((i < VolumesCount) && (!Found)) {
794 if (Number >= 0) { // User specified a volume by number
795 if (Volumes[i]->IsReadable) {
796 if (CountedVolumes == Number) {
797 *Volume = Volumes[i];
798 Found = TRUE;
799 }
800 CountedVolumes++;
801 } // if
802 } else { // User specified a volume by label or GUID
803 if (MyStriCmp(Identifier, Volumes[i]->VolName) || MyStriCmp(Identifier, Volumes[i]->PartName)) {
804 *Volume = Volumes[i];
805 Found = TRUE;
806 } // if
807 if (IdIsGuid && !Found) {
808 if (GuidsAreEqual(&VolGuid, &(Volumes[i]->PartGuid)) && !GuidsAreEqual(&NullGuid, &(Volumes[i]->PartGuid))) {
809 *Volume = Volumes[i];
810 Found = TRUE;
811 } // if
812 } // if
813 } // if/else
814 i++;
815 } // while()
816 return (Found);
817 } // static VOID FindVolume()
818
819 static VOID AddSubmenu(LOADER_ENTRY *Entry, REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
820 REFIT_MENU_SCREEN *SubScreen;
821 LOADER_ENTRY *SubEntry;
822 UINTN TokenCount;
823 CHAR16 **TokenList;
824
825 SubScreen = InitializeSubScreen(Entry);
826
827 // Set defaults for the new entry; will be modified based on lines read from the config. file....
828 SubEntry = InitializeLoaderEntry(Entry);
829
830 if ((SubEntry == NULL) || (SubScreen == NULL))
831 return;
832 SubEntry->me.Title = StrDuplicate(Title);
833
834 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
835
836 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
837 MyFreePool(SubEntry->LoaderPath);
838 SubEntry->LoaderPath = StrDuplicate(TokenList[1]);
839 SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
840
841 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
842 if (FindVolume(&Volume, TokenList[1])) {
843 if ((Volume != NULL) && (Volume->IsReadable) && (Volume->RootDir)) {
844 MyFreePool(SubEntry->me.Title);
845 SubEntry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
846 SPrint(SubEntry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", Volume->VolName);
847 SubEntry->me.BadgeImage = Volume->VolBadgeImage;
848 SubEntry->VolName = Volume->VolName;
849 } // if volume is readable
850 } // if match found
851
852 } else if (MyStriCmp(TokenList[0], L"initrd")) {
853 MyFreePool(SubEntry->InitrdPath);
854 SubEntry->InitrdPath = NULL;
855 if (TokenCount > 1) {
856 SubEntry->InitrdPath = StrDuplicate(TokenList[1]);
857 }
858
859 } else if (MyStriCmp(TokenList[0], L"options")) {
860 MyFreePool(SubEntry->LoadOptions);
861 SubEntry->LoadOptions = NULL;
862 if (TokenCount > 1) {
863 SubEntry->LoadOptions = StrDuplicate(TokenList[1]);
864 } // if/else
865
866 } else if (MyStriCmp(TokenList[0], L"add_options") && (TokenCount > 1)) {
867 MergeStrings(&SubEntry->LoadOptions, TokenList[1], L' ');
868
869 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
870 SubEntry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
871
872 } else if (MyStriCmp(TokenList[0], L"disabled")) {
873 SubEntry->Enabled = FALSE;
874 } // ief/elseif
875
876 FreeTokenLine(&TokenList, &TokenCount);
877 } // while()
878
879 if (SubEntry->InitrdPath != NULL) {
880 MergeStrings(&SubEntry->LoadOptions, L"initrd=", L' ');
881 MergeStrings(&SubEntry->LoadOptions, SubEntry->InitrdPath, 0);
882 MyFreePool(SubEntry->InitrdPath);
883 SubEntry->InitrdPath = NULL;
884 } // if
885 if (SubEntry->Enabled == TRUE) {
886 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
887 }
888 Entry->me.SubScreen = SubScreen;
889 } // VOID AddSubmenu()
890
891 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
892 // that entry. The calling function is then responsible for adding the entry to the
893 // list of entries.
894 static LOADER_ENTRY * AddStanzaEntries(REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
895 CHAR16 **TokenList;
896 UINTN TokenCount;
897 LOADER_ENTRY *Entry;
898 BOOLEAN DefaultsSet = FALSE, AddedSubmenu = FALSE;
899 REFIT_VOLUME *CurrentVolume = Volume;
900
901 // prepare the menu entry
902 Entry = InitializeLoaderEntry(NULL);
903 if (Entry == NULL)
904 return NULL;
905
906 Entry->Title = StrDuplicate(Title);
907 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
908 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
909 Entry->me.Row = 0;
910 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
911 Entry->VolName = CurrentVolume->VolName;
912
913 // Parse the config file to add options for a single stanza, terminating when the token
914 // is "}" or when the end of file is reached.
915 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
916 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
917 Entry->LoaderPath = StrDuplicate(TokenList[1]);
918 Entry->DevicePath = FileDevicePath(CurrentVolume->DeviceHandle, Entry->LoaderPath);
919 SetLoaderDefaults(Entry, TokenList[1], CurrentVolume);
920 MyFreePool(Entry->LoadOptions);
921 Entry->LoadOptions = NULL; // Discard default options, if any
922 DefaultsSet = TRUE;
923
924 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
925 if (FindVolume(&CurrentVolume, TokenList[1])) {
926 if ((CurrentVolume != NULL) && (CurrentVolume->IsReadable) && (CurrentVolume->RootDir)) {
927 MyFreePool(Entry->me.Title);
928 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
929 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
930 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
931 Entry->VolName = CurrentVolume->VolName;
932 } // if volume is readable
933 } // if match found
934
935 } else if (MyStriCmp(TokenList[0], L"icon") && (TokenCount > 1)) {
936 MyFreePool(Entry->me.Image);
937 Entry->me.Image = egLoadIcon(CurrentVolume->RootDir, TokenList[1], GlobalConfig.IconSizes[ICON_SIZE_BIG]);
938 if (Entry->me.Image == NULL) {
939 Entry->me.Image = DummyImage(GlobalConfig.IconSizes[ICON_SIZE_BIG]);
940 }
941
942 } else if (MyStriCmp(TokenList[0], L"initrd") && (TokenCount > 1)) {
943 MyFreePool(Entry->InitrdPath);
944 Entry->InitrdPath = StrDuplicate(TokenList[1]);
945
946 } else if (MyStriCmp(TokenList[0], L"options") && (TokenCount > 1)) {
947 MyFreePool(Entry->LoadOptions);
948 Entry->LoadOptions = StrDuplicate(TokenList[1]);
949
950 } else if (MyStriCmp(TokenList[0], L"ostype") && (TokenCount > 1)) {
951 if (TokenCount > 1) {
952 Entry->OSType = TokenList[1][0];
953 }
954
955 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
956 Entry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
957
958 } else if (MyStriCmp(TokenList[0], L"disabled")) {
959 Entry->Enabled = FALSE;
960
961 } else if (MyStriCmp(TokenList[0], L"submenuentry") && (TokenCount > 1)) {
962 AddSubmenu(Entry, File, CurrentVolume, TokenList[1]);
963 AddedSubmenu = TRUE;
964
965 } // set options to pass to the loader program
966 FreeTokenLine(&TokenList, &TokenCount);
967 } // while()
968
969 if (AddedSubmenu)
970 AddMenuEntry(Entry->me.SubScreen, &MenuEntryReturn);
971
972 if (Entry->InitrdPath) {
973 MergeStrings(&Entry->LoadOptions, L"initrd=", L' ');
974 MergeStrings(&Entry->LoadOptions, Entry->InitrdPath, 0);
975 MyFreePool(Entry->InitrdPath);
976 Entry->InitrdPath = NULL;
977 } // if
978
979 if (!DefaultsSet)
980 SetLoaderDefaults(Entry, L"\\EFI\\BOOT\\nemo.efi", CurrentVolume); // user included no "loader" line; use bogus one
981
982 return(Entry);
983 } // static VOID AddStanzaEntries()
984
985 // Read the user-configured menu entries from refind.conf and add or delete
986 // entries based on the contents of that file....
987 VOID ScanUserConfigured(CHAR16 *FileName)
988 {
989 EFI_STATUS Status;
990 REFIT_FILE File;
991 REFIT_VOLUME *Volume;
992 CHAR16 **TokenList;
993 CHAR16 *Title = NULL;
994 UINTN TokenCount, size;
995 LOADER_ENTRY *Entry;
996
997 if (FileExists(SelfDir, FileName)) {
998 Status = ReadFile(SelfDir, FileName, &File, &size);
999 if (EFI_ERROR(Status))
1000 return;
1001
1002 Volume = SelfVolume;
1003
1004 while ((TokenCount = ReadTokenLine(&File, &TokenList)) > 0) {
1005 if (MyStriCmp(TokenList[0], L"menuentry") && (TokenCount > 1)) {
1006 Title = StrDuplicate(TokenList[1]);
1007 Entry = AddStanzaEntries(&File, Volume, TokenList[1]);
1008 if (Entry->Enabled) {
1009 if (Entry->me.SubScreen == NULL)
1010 GenerateSubScreen(Entry, Volume, TRUE);
1011 AddPreparedLoaderEntry(Entry);
1012 } else {
1013 MyFreePool(Entry);
1014 } // if/else
1015 MyFreePool(Title);
1016
1017 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) &&
1018 MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
1019 if (!MyStriCmp(TokenList[1], FileName)) {
1020 ScanUserConfigured(TokenList[1]);
1021 }
1022
1023 } // if/else if...
1024 FreeTokenLine(&TokenList, &TokenCount);
1025 } // while()
1026 } // if()
1027 } // VOID ScanUserConfigured()
1028
1029 // Create an options file based on /etc/fstab. The resulting file has two options
1030 // lines, one of which boots the system with "ro root={rootfs}" and the other of
1031 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
1032 // filesystem identifier associated with the "/" line in /etc/fstab.
1033 static REFIT_FILE * GenerateOptionsFromEtcFstab(REFIT_VOLUME *Volume) {
1034 UINTN TokenCount, i;
1035 REFIT_FILE *Options = NULL, *Fstab = NULL;
1036 EFI_STATUS Status;
1037 CHAR16 **TokenList, *Line, Root[100];
1038
1039 if (FileExists(Volume->RootDir, L"\\etc\\fstab")) {
1040 Options = AllocateZeroPool(sizeof(REFIT_FILE));
1041 Fstab = AllocateZeroPool(sizeof(REFIT_FILE));
1042 Status = ReadFile(Volume->RootDir, L"\\etc\\fstab", Fstab, &i);
1043 if (CheckError(Status, L"while reading /etc/fstab")) {
1044 if (Options != NULL)
1045 FreePool(Options);
1046 if (Fstab != NULL)
1047 FreePool(Fstab);
1048 Options = NULL;
1049 Fstab = NULL;
1050 } else { // File read; locate root fs and create entries
1051 Options->Encoding = ENCODING_UTF16_LE;
1052 while ((TokenCount = ReadTokenLine(Fstab, &TokenList)) > 0) {
1053 if (TokenCount > 2) {
1054 Root[0] = '\0';
1055 if (StrCmp(TokenList[1], L"\\") == 0) {
1056 SPrint(Root, 99, L"%s", TokenList[0]);
1057 } else if (StrCmp(TokenList[2], L"\\") == 0) {
1058 SPrint(Root, 99, L"%s=%s", TokenList[0], TokenList[1]);
1059 } // if/elseif/elseif
1060 if (Root[0] != L'\0') {
1061 for (i = 0; i < StrLen(Root); i++)
1062 if (Root[i] == '\\')
1063 Root[i] = '/';
1064 Line = PoolPrint(L"\"Boot with normal options\" \"ro root=%s\"\n", Root);
1065 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1066 MyFreePool(Line);
1067 Line = PoolPrint(L"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root);
1068 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1069 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1070 } // if
1071 } // if
1072 FreeTokenLine(&TokenList, &TokenCount);
1073 } // while
1074
1075 if (Options->Buffer) {
1076 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1077 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1078 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1079 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1080 } else {
1081 MyFreePool(Options);
1082 Options = NULL;
1083 }
1084
1085 MyFreePool(Fstab->Buffer);
1086 MyFreePool(Fstab);
1087 } // if/else file read error
1088 } // if /etc/fstab exists
1089 return Options;
1090 } // GenerateOptionsFromEtcFstab()
1091
1092 // Create options from partition type codes. Specifically, if the earlier
1093 // partition scan found a partition with a type code corresponding to a root
1094 // filesystem according to the Freedesktop.org Discoverable Partitions Spec
1095 // (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/),
1096 // this function returns an appropriate file with two lines, one with
1097 // "ro root=/dev/disk/by-partuuid/{GUID}" and the other with that plus "single".
1098 // Note that this function returns the LAST partition found with the
1099 // appropriate type code, so this will work poorly on dual-boot systems or
1100 // if the type code is set incorrectly.
1101 static REFIT_FILE * GenerateOptionsFromPartTypes(VOID) {
1102 REFIT_FILE *Options = NULL;
1103 CHAR16 *Line, *GuidString, *WriteStatus;
1104
1105 if (GlobalConfig.DiscoveredRoot) {
1106 Options = AllocateZeroPool(sizeof(REFIT_FILE));
1107 if (Options) {
1108 Options->Encoding = ENCODING_UTF16_LE;
1109 GuidString = GuidAsString(&(GlobalConfig.DiscoveredRoot->PartGuid));
1110 WriteStatus = GlobalConfig.DiscoveredRoot->IsMarkedReadOnly ? L"ro" : L"rw";
1111 ToLower(GuidString);
1112 if (GuidString) {
1113 Line = PoolPrint(L"\"Boot with normal options\" \"%s root=/dev/disk/by-partuuid/%s\"\n", WriteStatus, GuidString);
1114 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1115 MyFreePool(Line);
1116 Line = PoolPrint(L"\"Boot into single-user mode\" \"%s root=/dev/disk/by-partuuid/%s single\"\n", WriteStatus, GuidString);
1117 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1118 MyFreePool(Line);
1119 MyFreePool(GuidString);
1120 } // if (GuidString)
1121 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1122
1123 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1124 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1125 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1126 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1127 } // if (Options allocated OK)
1128 } // if (partition has root GUID)
1129 return Options;
1130 } // REFIT_FILE * GenerateOptionsFromPartTypes()
1131
1132 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1133 // and Volume variables identify the location of the options file, but not its name --
1134 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1135 // file in the target directory, and this function finds the file with a name in the
1136 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1137 // directory and loads it. This function tries multiple files because I originally
1138 // used the filename linux.conf, but close on the heels of that decision, the Linux
1139 // kernel developers decided to use that name for a similar purpose, but with a
1140 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1141 // but I want a migration period in which both names are used.
1142 // If a rEFInd options file can't be found, try to generate minimal options from
1143 // /etc/fstab on the same volume as the kernel. This typically works only if the
1144 // kernel is being read from the Linux root filesystem.
1145 //
1146 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1147 // it wasn't found.
1148 REFIT_FILE * ReadLinuxOptionsFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1149 CHAR16 *OptionsFilename, *FullFilename;
1150 BOOLEAN GoOn = TRUE, FileFound = FALSE;
1151 UINTN i = 0, size;
1152 REFIT_FILE *File = NULL;
1153 EFI_STATUS Status;
1154
1155 do {
1156 OptionsFilename = FindCommaDelimited(LINUX_OPTIONS_FILENAMES, i++);
1157 FullFilename = FindPath(LoaderPath);
1158 if ((OptionsFilename != NULL) && (FullFilename != NULL)) {
1159 MergeStrings(&FullFilename, OptionsFilename, '\\');
1160 if (FileExists(Volume->RootDir, FullFilename)) {
1161 File = AllocateZeroPool(sizeof(REFIT_FILE));
1162 Status = ReadFile(Volume->RootDir, FullFilename, File, &size);
1163 if (CheckError(Status, L"while loading the Linux options file")) {
1164 if (File != NULL)
1165 FreePool(File);
1166 File = NULL;
1167 } else {
1168 GoOn = FALSE;
1169 FileFound = TRUE;
1170 } // if/else error
1171 } // if file exists
1172 } else { // a filename string is NULL
1173 GoOn = FALSE;
1174 } // if/else
1175 MyFreePool(OptionsFilename);
1176 MyFreePool(FullFilename);
1177 OptionsFilename = FullFilename = NULL;
1178 } while (GoOn);
1179 if (!FileFound) {
1180 // No refind_linux.conf file; look for /etc/fstab and try to pull values from there....
1181 File = GenerateOptionsFromEtcFstab(Volume);
1182 // If still no joy, try to use Freedesktop.org Discoverable Partitions Spec....
1183 if (!File)
1184 File = GenerateOptionsFromPartTypes();
1185 } // if
1186 return (File);
1187 } // static REFIT_FILE * ReadLinuxOptionsFile()
1188
1189 // Retrieve a single line of options from a Linux kernel options file
1190 CHAR16 * GetFirstOptionsFromFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1191 UINTN TokenCount;
1192 CHAR16 *Options = NULL;
1193 CHAR16 **TokenList;
1194 REFIT_FILE *File;
1195
1196 File = ReadLinuxOptionsFile(LoaderPath, Volume);
1197 if (File != NULL) {
1198 TokenCount = ReadTokenLine(File, &TokenList);
1199 if (TokenCount > 1)
1200 Options = StrDuplicate(TokenList[1]);
1201 FreeTokenLine(&TokenList, &TokenCount);
1202 FreePool(File);
1203 } // if
1204 return Options;
1205 } // static CHAR16 * GetOptionsFile()
1206