3 * Configuration file functions
5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 * Modifications copyright (c) 2012-2015 Roderick W. Smith
40 * Modifications distributed under the terms of the GNU General Public
41 * License (GPL) version 3 (GPLv3) or (at your option) any later version.
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.
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.
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/>.
66 #include "mystrings.h"
67 #include "../include/refit_call_wrapper.h"
68 #include "../mok/mok.h"
72 #define LINUX_OPTIONS_FILENAMES L"refind_linux.conf,refind-linux.conf"
73 #define MAXCONFIGFILESIZE (128*1024)
75 #define ENCODING_ISO8859_1 (0)
76 #define ENCODING_UTF8 (1)
77 #define ENCODING_UTF16_LE (2)
79 #define GetTime ST->RuntimeServices->GetTime
80 #define LAST_MINUTE 1439 /* Last minute of a day */
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 };
86 // read a file into a buffer
89 EFI_STATUS
ReadFile(IN EFI_FILE_HANDLE BaseDir
, IN CHAR16
*FileName
, IN OUT REFIT_FILE
*File
, OUT UINTN
*size
)
92 EFI_FILE_HANDLE FileHandle
;
93 EFI_FILE_INFO
*FileInfo
;
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
))
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
;
112 ReadSize
= FileInfo
->FileSize
;
115 File
->BufferSize
= (UINTN
)ReadSize
;
116 File
->Buffer
= AllocatePool(File
->BufferSize
);
117 if (File
->Buffer
== NULL
) {
119 return EFI_OUT_OF_RESOURCES
;
121 *size
= File
->BufferSize
;
123 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &File
->BufferSize
, File
->Buffer
);
124 if (CheckError(Status
, Message
)) {
125 MyFreePool(File
->Buffer
);
127 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
130 Status
= refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
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);
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) {
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
152 // TODO: detect other encodings as they are implemented
159 // get a single line of text from a file
162 static CHAR16
*ReadLine(REFIT_FILE
*File
)
167 if (File
->Buffer
== NULL
)
170 if (File
->Encoding
== ENCODING_ISO8859_1
|| File
->Encoding
== ENCODING_UTF8
) {
172 CHAR8
*p
, *LineStart
, *LineEnd
;
174 p
= File
->Current8Ptr
;
175 if (p
>= File
->End8Ptr
)
179 for (; p
< File
->End8Ptr
; p
++)
180 if (*p
== 13 || *p
== 10)
183 for (; p
< File
->End8Ptr
; p
++)
184 if (*p
!= 13 && *p
!= 10)
186 File
->Current8Ptr
= p
;
188 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
189 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
194 if (File
->Encoding
== ENCODING_ISO8859_1
) {
195 for (p
= LineStart
; p
< LineEnd
; )
197 } else if (File
->Encoding
== ENCODING_UTF8
) {
198 // TODO: actually handle UTF-8
199 for (p
= LineStart
; p
< LineEnd
; )
204 } else if (File
->Encoding
== ENCODING_UTF16_LE
) {
206 CHAR16
*p
, *LineStart
, *LineEnd
;
208 p
= File
->Current16Ptr
;
209 if (p
>= File
->End16Ptr
)
213 for (; p
< File
->End16Ptr
; p
++)
214 if (*p
== 13 || *p
== 10)
217 for (; p
< File
->End16Ptr
; p
++)
218 if (*p
!= 13 && *p
!= 10)
220 File
->Current16Ptr
= p
;
222 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
223 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
227 for (p
= LineStart
, q
= Line
; p
< LineEnd
; )
232 return NULL
; // unsupported encoding
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
;
244 if ((p
== NULL
) || (IsQuoted
== NULL
))
250 if ((*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '#' && *p
!= ',') || *IsQuoted
) {
255 Temp
= StrDuplicate(&p
[1]);
262 *IsQuoted
= !(*IsQuoted
);
264 } // if/else second character is a quote
265 } // if first character is a quote
268 } // BOOLEAN KeepReading()
271 // get a line of tokens from a file
273 UINTN
ReadTokenLine(IN REFIT_FILE
*File
, OUT CHAR16
***TokenList
)
275 BOOLEAN LineFinished
, IsQuoted
= FALSE
;
276 CHAR16
*Line
, *Token
, *p
;
277 UINTN TokenCount
= 0;
281 while (TokenCount
== 0) {
282 Line
= ReadLine(File
);
285 if (Line
[0] == L
'\0') {
291 LineFinished
= FALSE
;
292 while (!LineFinished
) {
293 // skip whitespace & find start of token
294 while ((*p
== ' ' || *p
== '\t' || *p
== '=' || *p
== ',') && !IsQuoted
)
296 if (*p
== 0 || *p
== '#')
300 IsQuoted
= !IsQuoted
;
306 while (KeepReading(p
, &IsQuoted
)) {
307 if ((*p
== L
'/') && !IsQuoted
) // Switch Unix-style to DOS-style directory separators
311 if (*p
== L
'\0' || *p
== L
'#')
315 AddListElement((VOID
***)TokenList
, &TokenCount
, (VOID
*)StrDuplicate(Token
));
321 } /* ReadTokenLine() */
323 VOID
FreeTokenLine(IN OUT CHAR16
***TokenList
, IN OUT UINTN
*TokenCount
)
325 // TODO: also free the items
326 FreeList((VOID
***)TokenList
, TokenCount
);
329 // handle a parameter with a single integer argument
330 static VOID
HandleInt(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT UINTN
*Value
)
332 if (TokenCount
== 2) {
333 if (StrCmp(TokenList
[1], L
"-1") == 0)
336 *Value
= Atoi(TokenList
[1]);
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
' '))) {
346 MergeStrings(Target
, TokenList
[1] + 2, L
',');
348 *Target
= StrDuplicate(TokenList
[1] + 2);
352 *Target
= StrDuplicate(TokenList
[1]);
355 } // static VOID HandleString()
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
) {
364 BOOLEAN AddMode
= FALSE
;
366 if ((TokenCount
> 2) && (StrCmp(TokenList
[1], L
"+") == 0)) {
370 if ((*Target
!= NULL
) && !AddMode
) {
374 for (i
= 1; i
< TokenCount
; i
++) {
375 if ((i
!= 1) || !AddMode
) {
376 CleanUpPathNameSlashes(TokenList
[i
]);
377 MergeStrings(Target
, TokenList
[i
], L
',');
380 } // static VOID HandleStrings()
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
387 static VOID
HandleHexes(IN CHAR16
**TokenList
, IN UINTN TokenCount
, IN UINTN MaxValue
, OUT UINT32_LIST
**Target
) {
388 UINTN InputIndex
= 1, i
;
390 UINT32_LIST
*EndOfList
= NULL
;
391 UINT32_LIST
*NewEntry
;
393 if ((TokenCount
> 2) && (StrCmp(TokenList
[1], L
"+") == 0)) {
396 while (EndOfList
&& (EndOfList
->Next
!= NULL
)) {
397 EndOfList
= EndOfList
->Next
;
400 EraseUint32List(Target
);
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
));
409 NewEntry
->Value
= Value
;
410 NewEntry
->Next
= NULL
;
411 if (EndOfList
== NULL
) {
412 EndOfList
= NewEntry
;
415 EndOfList
->Next
= NewEntry
;
416 EndOfList
= NewEntry
;
418 } // if allocated memory for NewEntry
419 } // if (Value < MaxValue)
420 } // if is valid hex value
422 } // static VOID HandleHexes()
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;
432 TimeLength
= StrLen(TimeString
);
433 while (i
< TimeLength
) {
434 if (TimeString
[i
] == L
':') {
438 if ((TimeString
[i
] >= L
'0') && (TimeString
[i
] <= '9')) {
440 Minute
+= (TimeString
[i
] - L
'0');
444 return (Hour
* 60 + Minute
);
445 } // BOOLEAN HandleTime()
447 static BOOLEAN
HandleBoolean(IN CHAR16
**TokenList
, IN UINTN TokenCount
) {
448 BOOLEAN TruthValue
= TRUE
;
450 if ((TokenCount
>= 2) && ((StrCmp(TokenList
[1], L
"0") == 0) ||
451 MyStriCmp(TokenList
[1], L
"false") ||
452 MyStriCmp(TokenList
[1], L
"off"))) {
457 } // BOOLEAN HandleBoolean
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
) {
463 EFI_TIME CurrentTime
;
464 UINTN StartTime
, EndTime
, Now
;
465 BOOLEAN SetIt
= FALSE
;
467 StartTime
= HandleTime(TokenList
[2]);
468 EndTime
= HandleTime(TokenList
[3]);
470 if ((StartTime
<= LAST_MINUTE
) && (EndTime
<= LAST_MINUTE
)) {
471 Status
= refit_call2_wrapper(GetTime
, &CurrentTime
, NULL
);
472 if (Status
!= EFI_SUCCESS
)
474 Now
= CurrentTime
.Hour
* 60 + CurrentTime
.Minute
;
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
);
479 } // if impossible time
481 if (StartTime
< EndTime
) { // Time range does NOT cross midnight
482 if ((Now
>= StartTime
) && (Now
<= EndTime
))
484 } else { // Time range DOES cross midnight
485 if ((Now
>= StartTime
) && (Now
<= EndTime
))
487 } // if/else time range crosses midnight
490 MyFreePool(*Default
);
491 *Default
= StrDuplicate(TokenList
[1]);
493 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
494 } // VOID SetDefaultByTime()
497 VOID
ReadConfig(CHAR16
*FileName
)
503 CHAR16
*TempStr
= NULL
;
505 EFI_GUID RefindGuid
= REFIND_GUID_VALUE
;
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
);
513 if (SelfVolume
->VolName
) {
514 TempStr
= SelfVolume
->VolName
? StrDuplicate(SelfVolume
->VolName
) : NULL
;
516 TempStr
= AllocateZeroPool(256 * sizeof(CHAR16
));
518 SPrint(TempStr
, 255, L
"fs%d", SelfVolume
->VolNumber
);
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
;
534 Status
= EfivarGetRaw(&RefindGuid
, L
"PreviousBoot", (CHAR8
**) &(GlobalConfig
.DefaultSelection
), &i
);
535 if (Status
!= EFI_SUCCESS
)
536 GlobalConfig
.DefaultSelection
= NULL
;
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
;
548 Status
= ReadFile(SelfDir
, FileName
, &File
, &i
);
549 if (EFI_ERROR(Status
))
553 TokenCount
= ReadTokenLine(&File
, &TokenList
);
557 if (MyStriCmp(TokenList
[0], L
"timeout")) {
558 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.Timeout
));
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
;
584 Print(L
" unknown hideui flag: '%s'\n", FlagName
);
588 } else if (MyStriCmp(TokenList
[0], L
"icons_dir")) {
589 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.IconsDir
));
591 } else if (MyStriCmp(TokenList
[0], L
"scanfor")) {
592 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
594 GlobalConfig
.ScanFor
[i
] = TokenList
[i
][0];
596 GlobalConfig
.ScanFor
[i
] = ' ';
599 } else if (MyStriCmp(TokenList
[0], L
"uefi_deep_legacy_scan")) {
600 GlobalConfig
.DeepLegacyScan
= HandleBoolean(TokenList
, TokenCount
);
602 } else if (MyStriCmp(TokenList
[0], L
"scan_delay") && (TokenCount
== 2)) {
603 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScanDelay
));
605 } else if (MyStriCmp(TokenList
[0], L
"also_scan_dirs")) {
606 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.AlsoScan
));
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
',');
616 } else if (MyStriCmp(TokenList
[0], L
"don't_scan_dirs") || MyStriCmp(TokenList
[0], L
"dont_scan_dirs")) {
617 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanDirs
));
619 } else if (MyStriCmp(TokenList
[0], L
"don't_scan_files") || MyStriCmp(TokenList
[0], L
"dont_scan_files")) {
620 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanFiles
));
622 } else if (MyStriCmp(TokenList
[0], L
"windows_recovery_files")) {
623 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.WindowsRecoveryFiles
));
625 } else if (MyStriCmp(TokenList
[0], L
"scan_driver_dirs")) {
626 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DriverDirs
));
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
;
661 Print(L
" unknown showtools flag: '%s'\n", FlagName
);
663 } // showtools options
665 } else if (MyStriCmp(TokenList
[0], L
"banner")) {
666 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.BannerFileName
));
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
;
674 Print(L
" unknown banner_type flag: '%s'\n", TokenList
[1]);
677 } else if (MyStriCmp(TokenList
[0], L
"small_icon_size") && (TokenCount
== 2)) {
678 HandleInt(TokenList
, TokenCount
, &i
);
680 GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] = i
;
682 } else if (MyStriCmp(TokenList
[0], L
"big_icon_size") && (TokenCount
== 2)) {
683 HandleInt(TokenList
, TokenCount
, &i
);
685 GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] = i
;
686 GlobalConfig
.IconSizes
[ICON_SIZE_BADGE
] = i
/ 4;
689 } else if (MyStriCmp(TokenList
[0], L
"selection_small")) {
690 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionSmallFileName
));
692 } else if (MyStriCmp(TokenList
[0], L
"selection_big")) {
693 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionBigFileName
));
695 } else if (MyStriCmp(TokenList
[0], L
"default_selection")) {
696 if (TokenCount
== 4) {
697 SetDefaultByTime(TokenList
, &(GlobalConfig
.DefaultSelection
));
699 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.DefaultSelection
));
702 } else if (MyStriCmp(TokenList
[0], L
"textonly")) {
703 GlobalConfig
.TextOnly
= HandleBoolean(TokenList
, TokenCount
);
705 } else if (MyStriCmp(TokenList
[0], L
"textmode")) {
706 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.RequestedTextMode
));
708 } else if (MyStriCmp(TokenList
[0], L
"resolution") && ((TokenCount
== 2) || (TokenCount
== 3))) {
709 GlobalConfig
.RequestedScreenWidth
= Atoi(TokenList
[1]);
711 GlobalConfig
.RequestedScreenHeight
= Atoi(TokenList
[2]);
713 GlobalConfig
.RequestedScreenHeight
= 0;
715 } else if (MyStriCmp(TokenList
[0], L
"screensaver")) {
716 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScreensaverTime
));
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
;
733 } // for (graphics_on tokens)
735 } else if (MyStriCmp(TokenList
[0], L
"font") && (TokenCount
== 2)) {
736 egLoadFont(TokenList
[1]);
738 } else if (MyStriCmp(TokenList
[0], L
"scan_all_linux_kernels")) {
739 GlobalConfig
.ScanAllLinux
= HandleBoolean(TokenList
, TokenCount
);
741 } else if (MyStriCmp(TokenList
[0], L
"fold_linux_kernels")) {
742 GlobalConfig
.FoldLinuxKernels
= HandleBoolean(TokenList
, TokenCount
);
744 } else if (MyStriCmp(TokenList
[0], L
"max_tags")) {
745 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.MaxTags
));
747 } else if (MyStriCmp(TokenList
[0], L
"enable_and_lock_vmx")) {
748 GlobalConfig
.EnableAndLockVMX
= HandleBoolean(TokenList
, TokenCount
);
750 } else if (MyStriCmp(TokenList
[0], L
"spoof_osx_version")) {
751 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SpoofOSXVersion
));
753 } else if (MyStriCmp(TokenList
[0], L
"csr_values")) {
754 HandleHexes(TokenList
, TokenCount
, CSR_MAX_LEGAL_VALUE
, &(GlobalConfig
.CsrValues
));
756 } else if (MyStriCmp(TokenList
[0], L
"include") && (TokenCount
== 2) && MyStriCmp(FileName
, GlobalConfig
.ConfigFilename
)) {
757 if (!MyStriCmp(TokenList
[1], FileName
)) {
758 ReadConfig(TokenList
[1]);
763 FreeTokenLine(&TokenList
, &TokenCount
);
765 if ((GlobalConfig
.DontScanFiles
) && (GlobalConfig
.WindowsRecoveryFiles
))
766 MergeStrings(&(GlobalConfig
.DontScanFiles
), GlobalConfig
.WindowsRecoveryFiles
, L
',');
767 MyFreePool(File
.Buffer
);
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
;
773 } /* VOID ReadConfig() */
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
;
782 BOOLEAN Found
= FALSE
, IdIsGuid
= FALSE
;
783 EFI_GUID VolGuid
, NullGuid
= NULL_GUID_VALUE
;
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
)) {
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
];
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
];
807 if (IdIsGuid
&& !Found
) {
808 if (GuidsAreEqual(&VolGuid
, &(Volumes
[i
]->PartGuid
)) && !GuidsAreEqual(&NullGuid
, &(Volumes
[i
]->PartGuid
))) {
809 *Volume
= Volumes
[i
];
817 } // static VOID FindVolume()
819 static VOID
AddSubmenu(LOADER_ENTRY
*Entry
, REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
820 REFIT_MENU_SCREEN
*SubScreen
;
821 LOADER_ENTRY
*SubEntry
;
825 SubScreen
= InitializeSubScreen(Entry
);
827 // Set defaults for the new entry; will be modified based on lines read from the config. file....
828 SubEntry
= InitializeLoaderEntry(Entry
);
830 if ((SubEntry
== NULL
) || (SubScreen
== NULL
))
832 SubEntry
->me
.Title
= StrDuplicate(Title
);
834 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StrCmp(TokenList
[0], L
"}") != 0)) {
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
);
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
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]);
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]);
866 } else if (MyStriCmp(TokenList
[0], L
"add_options") && (TokenCount
> 1)) {
867 MergeStrings(&SubEntry
->LoadOptions
, TokenList
[1], L
' ');
869 } else if (MyStriCmp(TokenList
[0], L
"graphics") && (TokenCount
> 1)) {
870 SubEntry
->UseGraphicsMode
= MyStriCmp(TokenList
[1], L
"on");
872 } else if (MyStriCmp(TokenList
[0], L
"disabled")) {
873 SubEntry
->Enabled
= FALSE
;
876 FreeTokenLine(&TokenList
, &TokenCount
);
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
;
885 if (SubEntry
->Enabled
== TRUE
) {
886 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
888 Entry
->me
.SubScreen
= SubScreen
;
889 } // VOID AddSubmenu()
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
894 static LOADER_ENTRY
* AddStanzaEntries(REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
898 BOOLEAN DefaultsSet
= FALSE
, AddedSubmenu
= FALSE
;
899 REFIT_VOLUME
*CurrentVolume
= Volume
;
901 // prepare the menu entry
902 Entry
= InitializeLoaderEntry(NULL
);
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
);
910 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
911 Entry
->VolName
= CurrentVolume
->VolName
;
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
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
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
]);
942 } else if (MyStriCmp(TokenList
[0], L
"initrd") && (TokenCount
> 1)) {
943 MyFreePool(Entry
->InitrdPath
);
944 Entry
->InitrdPath
= StrDuplicate(TokenList
[1]);
946 } else if (MyStriCmp(TokenList
[0], L
"options") && (TokenCount
> 1)) {
947 MyFreePool(Entry
->LoadOptions
);
948 Entry
->LoadOptions
= StrDuplicate(TokenList
[1]);
950 } else if (MyStriCmp(TokenList
[0], L
"ostype") && (TokenCount
> 1)) {
951 if (TokenCount
> 1) {
952 Entry
->OSType
= TokenList
[1][0];
955 } else if (MyStriCmp(TokenList
[0], L
"graphics") && (TokenCount
> 1)) {
956 Entry
->UseGraphicsMode
= MyStriCmp(TokenList
[1], L
"on");
958 } else if (MyStriCmp(TokenList
[0], L
"disabled")) {
959 Entry
->Enabled
= FALSE
;
961 } else if (MyStriCmp(TokenList
[0], L
"submenuentry") && (TokenCount
> 1)) {
962 AddSubmenu(Entry
, File
, CurrentVolume
, TokenList
[1]);
965 } // set options to pass to the loader program
966 FreeTokenLine(&TokenList
, &TokenCount
);
970 AddMenuEntry(Entry
->me
.SubScreen
, &MenuEntryReturn
);
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
;
980 SetLoaderDefaults(Entry
, L
"\\EFI\\BOOT\\nemo.efi", CurrentVolume
); // user included no "loader" line; use bogus one
983 } // static VOID AddStanzaEntries()
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
)
991 REFIT_VOLUME
*Volume
;
993 CHAR16
*Title
= NULL
;
994 UINTN TokenCount
, size
;
997 if (FileExists(SelfDir
, FileName
)) {
998 Status
= ReadFile(SelfDir
, FileName
, &File
, &size
);
999 if (EFI_ERROR(Status
))
1002 Volume
= SelfVolume
;
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
);
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]);
1024 FreeTokenLine(&TokenList
, &TokenCount
);
1027 } // VOID ScanUserConfigured()
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
;
1037 CHAR16
**TokenList
, *Line
, Root
[100];
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
)
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) {
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
] == '\\')
1064 Line
= PoolPrint(L
"\"Boot with normal options\" \"ro root=%s\"\n", Root
);
1065 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
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
);
1072 FreeTokenLine(&TokenList
, &TokenCount
);
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);
1081 MyFreePool(Options
);
1085 MyFreePool(Fstab
->Buffer
);
1087 } // if/else file read error
1088 } // if /etc/fstab exists
1090 } // GenerateOptionsFromEtcFstab()
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
;
1105 if (GlobalConfig
.DiscoveredRoot
) {
1106 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
1108 Options
->Encoding
= ENCODING_UTF16_LE
;
1109 GuidString
= GuidAsString(&(GlobalConfig
.DiscoveredRoot
->PartGuid
));
1110 WriteStatus
= GlobalConfig
.DiscoveredRoot
->IsMarkedReadOnly
? L
"ro" : L
"rw";
1111 ToLower(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);
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);
1119 MyFreePool(GuidString
);
1120 } // if (GuidString)
1121 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
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)
1130 } // REFIT_FILE * GenerateOptionsFromPartTypes()
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.
1146 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1148 REFIT_FILE
* ReadLinuxOptionsFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1149 CHAR16
*OptionsFilename
, *FullFilename
;
1150 BOOLEAN GoOn
= TRUE
, FileFound
= FALSE
;
1152 REFIT_FILE
*File
= NULL
;
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")) {
1172 } else { // a filename string is NULL
1175 MyFreePool(OptionsFilename
);
1176 MyFreePool(FullFilename
);
1177 OptionsFilename
= FullFilename
= NULL
;
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....
1184 File
= GenerateOptionsFromPartTypes();
1187 } // static REFIT_FILE * ReadLinuxOptionsFile()
1189 // Retrieve a single line of options from a Linux kernel options file
1190 CHAR16
* GetFirstOptionsFromFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1192 CHAR16
*Options
= NULL
;
1196 File
= ReadLinuxOptionsFile(LoaderPath
, Volume
);
1198 TokenCount
= ReadTokenLine(File
, &TokenList
);
1200 Options
= StrDuplicate(TokenList
[1]);
1201 FreeTokenLine(&TokenList
, &TokenCount
);
1205 } // static CHAR16 * GetOptionsFile()