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 "../include/refit_call_wrapper.h"
67 #include "../mok/mok.h"
71 #define LINUX_OPTIONS_FILENAMES L"refind_linux.conf,refind-linux.conf"
72 #define MAXCONFIGFILESIZE (128*1024)
74 #define ENCODING_ISO8859_1 (0)
75 #define ENCODING_UTF8 (1)
76 #define ENCODING_UTF16_LE (2)
78 #define GetTime ST->RuntimeServices->GetTime
79 #define LAST_MINUTE 1439 /* Last minute of a day */
81 extern REFIT_MENU_ENTRY MenuEntryReturn
;
82 //static REFIT_MENU_ENTRY MenuEntryReturn = { L"Return to Main Menu", TAG_RETURN, 0, 0, 0, NULL, NULL, NULL };
85 // read a file into a buffer
88 EFI_STATUS
ReadFile(IN EFI_FILE_HANDLE BaseDir
, IN CHAR16
*FileName
, IN OUT REFIT_FILE
*File
, OUT UINTN
*size
)
91 EFI_FILE_HANDLE FileHandle
;
92 EFI_FILE_INFO
*FileInfo
;
99 // read the file, allocating a buffer on the way
100 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
101 SPrint(Message
, 255, L
"while loading the file '%s'", FileName
);
102 if (CheckError(Status
, Message
))
105 FileInfo
= LibFileInfo(FileHandle
);
106 if (FileInfo
== NULL
) {
107 // TODO: print and register the error
108 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
109 return EFI_LOAD_ERROR
;
111 ReadSize
= FileInfo
->FileSize
;
114 File
->BufferSize
= (UINTN
)ReadSize
;
115 File
->Buffer
= AllocatePool(File
->BufferSize
);
116 if (File
->Buffer
== NULL
) {
118 return EFI_OUT_OF_RESOURCES
;
120 *size
= File
->BufferSize
;
122 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &File
->BufferSize
, File
->Buffer
);
123 if (CheckError(Status
, Message
)) {
124 MyFreePool(File
->Buffer
);
126 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
129 Status
= refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
132 File
->Current8Ptr
= (CHAR8
*)File
->Buffer
;
133 File
->End8Ptr
= File
->Current8Ptr
+ File
->BufferSize
;
134 File
->Current16Ptr
= (CHAR16
*)File
->Buffer
;
135 File
->End16Ptr
= File
->Current16Ptr
+ (File
->BufferSize
>> 1);
138 File
->Encoding
= ENCODING_ISO8859_1
; // default: 1:1 translation of CHAR8 to CHAR16
139 if (File
->BufferSize
>= 4) {
140 if (File
->Buffer
[0] == 0xFF && File
->Buffer
[1] == 0xFE) {
141 // BOM in UTF-16 little endian (or UTF-32 little endian)
142 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
143 File
->Current16Ptr
++;
144 } else if (File
->Buffer
[0] == 0xEF && File
->Buffer
[1] == 0xBB && File
->Buffer
[2] == 0xBF) {
146 File
->Encoding
= ENCODING_UTF8
; // translate from UTF-8 to UTF-16
147 File
->Current8Ptr
+= 3;
148 } else if (File
->Buffer
[1] == 0 && File
->Buffer
[3] == 0) {
149 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
151 // TODO: detect other encodings as they are implemented
158 // get a single line of text from a file
161 static CHAR16
*ReadLine(REFIT_FILE
*File
)
166 if (File
->Buffer
== NULL
)
169 if (File
->Encoding
== ENCODING_ISO8859_1
|| File
->Encoding
== ENCODING_UTF8
) {
171 CHAR8
*p
, *LineStart
, *LineEnd
;
173 p
= File
->Current8Ptr
;
174 if (p
>= File
->End8Ptr
)
178 for (; p
< File
->End8Ptr
; p
++)
179 if (*p
== 13 || *p
== 10)
182 for (; p
< File
->End8Ptr
; p
++)
183 if (*p
!= 13 && *p
!= 10)
185 File
->Current8Ptr
= p
;
187 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
188 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
193 if (File
->Encoding
== ENCODING_ISO8859_1
) {
194 for (p
= LineStart
; p
< LineEnd
; )
196 } else if (File
->Encoding
== ENCODING_UTF8
) {
197 // TODO: actually handle UTF-8
198 for (p
= LineStart
; p
< LineEnd
; )
203 } else if (File
->Encoding
== ENCODING_UTF16_LE
) {
205 CHAR16
*p
, *LineStart
, *LineEnd
;
207 p
= File
->Current16Ptr
;
208 if (p
>= File
->End16Ptr
)
212 for (; p
< File
->End16Ptr
; p
++)
213 if (*p
== 13 || *p
== 10)
216 for (; p
< File
->End16Ptr
; p
++)
217 if (*p
!= 13 && *p
!= 10)
219 File
->Current16Ptr
= p
;
221 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
222 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
226 for (p
= LineStart
, q
= Line
; p
< LineEnd
; )
231 return NULL
; // unsupported encoding
236 // Returns FALSE if *p points to the end of a token, TRUE otherwise.
237 // Also modifies *p **IF** the first and second characters are both
238 // quotes ('"'); it deletes one of them.
239 static BOOLEAN
KeepReading(IN OUT CHAR16
*p
, IN OUT BOOLEAN
*IsQuoted
) {
240 BOOLEAN MoreToRead
= FALSE
;
243 if ((p
== NULL
) || (IsQuoted
== NULL
))
249 if ((*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '#' && *p
!= ',') || *IsQuoted
) {
254 Temp
= StrDuplicate(&p
[1]);
261 *IsQuoted
= !(*IsQuoted
);
263 } // if/else second character is a quote
264 } // if first character is a quote
267 } // BOOLEAN KeepReading()
270 // get a line of tokens from a file
272 UINTN
ReadTokenLine(IN REFIT_FILE
*File
, OUT CHAR16
***TokenList
)
274 BOOLEAN LineFinished
, IsQuoted
= FALSE
;
275 CHAR16
*Line
, *Token
, *p
;
276 UINTN TokenCount
= 0;
280 while (TokenCount
== 0) {
281 Line
= ReadLine(File
);
284 if (Line
[0] == L
'\0') {
290 LineFinished
= FALSE
;
291 while (!LineFinished
) {
292 // skip whitespace & find start of token
293 while ((*p
== ' ' || *p
== '\t' || *p
== '=' || *p
== ',') && !IsQuoted
)
295 if (*p
== 0 || *p
== '#')
299 IsQuoted
= !IsQuoted
;
305 while (KeepReading(p
, &IsQuoted
)) {
306 if ((*p
== L
'/') && !IsQuoted
) // Switch Unix-style to DOS-style directory separators
310 if (*p
== L
'\0' || *p
== L
'#')
314 AddListElement((VOID
***)TokenList
, &TokenCount
, (VOID
*)StrDuplicate(Token
));
320 } /* ReadTokenLine() */
322 VOID
FreeTokenLine(IN OUT CHAR16
***TokenList
, IN OUT UINTN
*TokenCount
)
324 // TODO: also free the items
325 FreeList((VOID
***)TokenList
, TokenCount
);
328 // handle a parameter with a single integer argument
329 static VOID
HandleInt(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT UINTN
*Value
)
331 if (TokenCount
== 2) {
332 if (StrCmp(TokenList
[1], L
"-1") == 0)
335 *Value
= Atoi(TokenList
[1]);
339 // handle a parameter with a single string argument
340 static VOID
HandleString(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
341 if ((TokenCount
== 2) && Target
) {
342 if ((StrLen(TokenList
[1]) > 1) && (TokenList
[1][0] == L
'+') &&
343 ((TokenList
[1][1] == L
',') || (TokenList
[1][1] == L
' '))) {
345 MergeStrings(Target
, TokenList
[1] + 2, L
',');
347 *Target
= StrDuplicate(TokenList
[1] + 2);
351 *Target
= StrDuplicate(TokenList
[1]);
354 } // static VOID HandleString()
356 // Handle a parameter with a series of string arguments, to replace or be added to a
357 // comma-delimited list. Passes each token through the CleanUpPathNameSlashes() function
358 // to ensure consistency in subsequent comparisons of filenames. If the first
359 // non-keyword token is "+", the list is added to the existing target string; otherwise,
360 // the tokens replace the current string.
361 static VOID
HandleStrings(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
363 BOOLEAN AddMode
= FALSE
;
365 if ((TokenCount
> 2) && (StrCmp(TokenList
[1], L
"+") == 0)) {
369 if ((*Target
!= NULL
) && !AddMode
) {
373 for (i
= 1; i
< TokenCount
; i
++) {
374 if ((i
!= 1) || !AddMode
) {
375 CleanUpPathNameSlashes(TokenList
[i
]);
376 MergeStrings(Target
, TokenList
[i
], L
',');
379 } // static VOID HandleStrings()
381 // Handle a parameter with a series of hexadecimal arguments, to replace or be added to a
382 // linked list of UINT32 values. Any item with a non-hexadecimal value is discarded, as is
383 // any value that exceeds MaxValue. If the first non-keyword token is "+", the new list is
384 // added to the existing Target; otherwise, the interpreted tokens replace the current
386 static VOID
HandleHexes(IN CHAR16
**TokenList
, IN UINTN TokenCount
, IN UINTN MaxValue
, OUT UINT32_LIST
**Target
) {
387 UINTN InputIndex
= 1, i
;
389 UINT32_LIST
*EndOfList
= NULL
;
390 UINT32_LIST
*NewEntry
;
392 if ((TokenCount
> 2) && (StrCmp(TokenList
[1], L
"+") == 0)) {
395 while (EndOfList
&& (EndOfList
->Next
!= NULL
)) {
396 EndOfList
= EndOfList
->Next
;
399 EraseUint32List(Target
);
402 for (i
= InputIndex
; i
< TokenCount
; i
++) {
403 if (IsValidHex(TokenList
[i
])) {
404 Value
= (UINT32
) StrToHex(TokenList
[i
], 0, 8);
405 if (Value
<= MaxValue
) {
406 NewEntry
= AllocatePool(sizeof(UINT32_LIST
));
408 NewEntry
->Value
= Value
;
409 NewEntry
->Next
= NULL
;
410 if (EndOfList
== NULL
) {
411 EndOfList
= NewEntry
;
414 EndOfList
->Next
= NewEntry
;
415 EndOfList
= NewEntry
;
417 } // if allocated memory for NewEntry
418 } // if (Value < MaxValue)
419 } // if is valid hex value
421 } // static VOID HandleHexes()
423 // Convert TimeString (in "HH:MM" format) to a pure-minute format. Values should be
424 // in the range from 0 (for 00:00, or midnight) to 1439 (for 23:59; aka LAST_MINUTE).
425 // Any value outside that range denotes an error in the specification. Note that if
426 // the input is a number that includes no colon, this function will return the original
427 // number in UINTN form.
428 static UINTN
HandleTime(IN CHAR16
*TimeString
) {
429 UINTN Hour
= 0, Minute
= 0, TimeLength
, i
= 0;
431 TimeLength
= StrLen(TimeString
);
432 while (i
< TimeLength
) {
433 if (TimeString
[i
] == L
':') {
437 if ((TimeString
[i
] >= L
'0') && (TimeString
[i
] <= '9')) {
439 Minute
+= (TimeString
[i
] - L
'0');
443 return (Hour
* 60 + Minute
);
444 } // BOOLEAN HandleTime()
446 static BOOLEAN
HandleBoolean(IN CHAR16
**TokenList
, IN UINTN TokenCount
) {
447 BOOLEAN TruthValue
= TRUE
;
449 if ((TokenCount
>= 2) && ((StrCmp(TokenList
[1], L
"0") == 0) ||
450 MyStriCmp(TokenList
[1], L
"false") ||
451 MyStriCmp(TokenList
[1], L
"off"))) {
456 } // BOOLEAN HandleBoolean
458 // Sets the default boot loader IF the current time is within the bounds
459 // defined by the third and fourth tokens in the TokenList.
460 static VOID
SetDefaultByTime(IN CHAR16
**TokenList
, OUT CHAR16
**Default
) {
462 EFI_TIME CurrentTime
;
463 UINTN StartTime
, EndTime
, Now
;
464 BOOLEAN SetIt
= FALSE
;
466 StartTime
= HandleTime(TokenList
[2]);
467 EndTime
= HandleTime(TokenList
[3]);
469 if ((StartTime
<= LAST_MINUTE
) && (EndTime
<= LAST_MINUTE
)) {
470 Status
= refit_call2_wrapper(GetTime
, &CurrentTime
, NULL
);
471 if (Status
!= EFI_SUCCESS
)
473 Now
= CurrentTime
.Hour
* 60 + CurrentTime
.Minute
;
475 if (Now
> LAST_MINUTE
) { // Shouldn't happen; just being paranoid
476 Print(L
"Warning: Impossible system time: %d:%d\n", CurrentTime
.Hour
, CurrentTime
.Minute
);
478 } // if impossible time
480 if (StartTime
< EndTime
) { // Time range does NOT cross midnight
481 if ((Now
>= StartTime
) && (Now
<= EndTime
))
483 } else { // Time range DOES cross midnight
484 if ((Now
>= StartTime
) && (Now
<= EndTime
))
486 } // if/else time range crosses midnight
489 MyFreePool(*Default
);
490 *Default
= StrDuplicate(TokenList
[1]);
492 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
493 } // VOID SetDefaultByTime()
496 VOID
ReadConfig(CHAR16
*FileName
)
502 CHAR16
*TempStr
= NULL
;
504 EFI_GUID RefindGuid
= REFIND_GUID_VALUE
;
506 // Set a few defaults only if we're loading the default file.
507 if (MyStriCmp(FileName
, GlobalConfig
.ConfigFilename
)) {
508 MyFreePool(GlobalConfig
.AlsoScan
);
509 GlobalConfig
.AlsoScan
= StrDuplicate(ALSO_SCAN_DIRS
);
510 MyFreePool(GlobalConfig
.DontScanDirs
);
512 if (SelfVolume
->VolName
) {
513 TempStr
= SelfVolume
->VolName
? StrDuplicate(SelfVolume
->VolName
) : NULL
;
515 TempStr
= AllocateZeroPool(256 * sizeof(CHAR16
));
517 SPrint(TempStr
, 255, L
"fs%d", SelfVolume
->VolNumber
);
520 MergeStrings(&TempStr
, SelfDirPath
, L
':');
521 MergeStrings(&TempStr
, MEMTEST_LOCATIONS
, L
',');
522 GlobalConfig
.DontScanDirs
= TempStr
;
523 MyFreePool(GlobalConfig
.DontScanFiles
);
524 GlobalConfig
.DontScanFiles
= StrDuplicate(DONT_SCAN_FILES
);
525 MergeStrings(&(GlobalConfig
.DontScanFiles
), MOK_NAMES
, L
',');
526 MyFreePool(GlobalConfig
.DontScanVolumes
);
527 GlobalConfig
.DontScanVolumes
= StrDuplicate(DONT_SCAN_VOLUMES
);
528 GlobalConfig
.WindowsRecoveryFiles
= StrDuplicate(WINDOWS_RECOVERY_FILES
);
529 if (GlobalConfig
.DefaultSelection
!= NULL
) {
530 MyFreePool(GlobalConfig
.DefaultSelection
);
531 GlobalConfig
.DefaultSelection
= NULL
;
533 Status
= EfivarGetRaw(&RefindGuid
, L
"PreviousBoot", (CHAR8
**) &(GlobalConfig
.DefaultSelection
), &i
);
534 if (Status
!= EFI_SUCCESS
)
535 GlobalConfig
.DefaultSelection
= NULL
;
538 if (!FileExists(SelfDir
, FileName
)) {
539 Print(L
"Configuration file '%s' missing!\n", FileName
);
540 if (!FileExists(SelfDir
, L
"icons")) {
541 Print(L
"Icons directory doesn't exist; setting textonly = TRUE!\n");
542 GlobalConfig
.TextOnly
= TRUE
;
547 Status
= ReadFile(SelfDir
, FileName
, &File
, &i
);
548 if (EFI_ERROR(Status
))
552 TokenCount
= ReadTokenLine(&File
, &TokenList
);
556 if (MyStriCmp(TokenList
[0], L
"timeout")) {
557 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.Timeout
));
559 } else if (MyStriCmp(TokenList
[0], L
"hideui")) {
560 for (i
= 1; i
< TokenCount
; i
++) {
561 FlagName
= TokenList
[i
];
562 if (MyStriCmp(FlagName
, L
"banner")) {
563 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_BANNER
;
564 } else if (MyStriCmp(FlagName
, L
"label")) {
565 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_LABEL
;
566 } else if (MyStriCmp(FlagName
, L
"singleuser")) {
567 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SINGLEUSER
;
568 } else if (MyStriCmp(FlagName
, L
"hwtest")) {
569 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HWTEST
;
570 } else if (MyStriCmp(FlagName
, L
"arrows")) {
571 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_ARROWS
;
572 } else if (MyStriCmp(FlagName
, L
"hints")) {
573 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HINTS
;
574 } else if (MyStriCmp(FlagName
, L
"editor")) {
575 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_EDITOR
;
576 } else if (MyStriCmp(FlagName
, L
"safemode")) {
577 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SAFEMODE
;
578 } else if (MyStriCmp(FlagName
, L
"badges")) {
579 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_BADGES
;
580 } else if (MyStriCmp(FlagName
, L
"all")) {
581 GlobalConfig
.HideUIFlags
= HIDEUI_FLAG_ALL
;
583 Print(L
" unknown hideui flag: '%s'\n", FlagName
);
587 } else if (MyStriCmp(TokenList
[0], L
"icons_dir")) {
588 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.IconsDir
));
590 } else if (MyStriCmp(TokenList
[0], L
"scanfor")) {
591 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
593 GlobalConfig
.ScanFor
[i
] = TokenList
[i
][0];
595 GlobalConfig
.ScanFor
[i
] = ' ';
598 } else if (MyStriCmp(TokenList
[0], L
"uefi_deep_legacy_scan")) {
599 GlobalConfig
.DeepLegacyScan
= HandleBoolean(TokenList
, TokenCount
);
601 } else if (MyStriCmp(TokenList
[0], L
"scan_delay") && (TokenCount
== 2)) {
602 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScanDelay
));
604 } else if (MyStriCmp(TokenList
[0], L
"also_scan_dirs")) {
605 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.AlsoScan
));
607 } else if (MyStriCmp(TokenList
[0], L
"don't_scan_volumes") || MyStriCmp(TokenList
[0], L
"dont_scan_volumes")) {
608 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
609 MyFreePool(GlobalConfig
.DontScanVolumes
);
610 GlobalConfig
.DontScanVolumes
= NULL
;
611 for (i
= 1; i
< TokenCount
; i
++) {
612 MergeStrings(&GlobalConfig
.DontScanVolumes
, TokenList
[i
], L
',');
615 } else if (MyStriCmp(TokenList
[0], L
"don't_scan_dirs") || MyStriCmp(TokenList
[0], L
"dont_scan_dirs")) {
616 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanDirs
));
618 } else if (MyStriCmp(TokenList
[0], L
"don't_scan_files") || MyStriCmp(TokenList
[0], L
"dont_scan_files")) {
619 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanFiles
));
621 } else if (MyStriCmp(TokenList
[0], L
"windows_recovery_files")) {
622 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.WindowsRecoveryFiles
));
624 } else if (MyStriCmp(TokenList
[0], L
"scan_driver_dirs")) {
625 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DriverDirs
));
627 } else if (MyStriCmp(TokenList
[0], L
"showtools")) {
628 SetMem(GlobalConfig
.ShowTools
, NUM_TOOLS
* sizeof(UINTN
), 0);
629 for (i
= 1; (i
< TokenCount
) && (i
< NUM_TOOLS
); i
++) {
630 FlagName
= TokenList
[i
];
631 if (MyStriCmp(FlagName
, L
"shell")) {
632 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHELL
;
633 } else if (MyStriCmp(FlagName
, L
"gptsync")) {
634 GlobalConfig
.ShowTools
[i
- 1] = TAG_GPTSYNC
;
635 } else if (MyStriCmp(FlagName
, L
"gdisk")) {
636 GlobalConfig
.ShowTools
[i
- 1] = TAG_GDISK
;
637 } else if (MyStriCmp(FlagName
, L
"about")) {
638 GlobalConfig
.ShowTools
[i
- 1] = TAG_ABOUT
;
639 } else if (MyStriCmp(FlagName
, L
"exit")) {
640 GlobalConfig
.ShowTools
[i
- 1] = TAG_EXIT
;
641 } else if (MyStriCmp(FlagName
, L
"reboot")) {
642 GlobalConfig
.ShowTools
[i
- 1] = TAG_REBOOT
;
643 } else if (MyStriCmp(FlagName
, L
"shutdown")) {
644 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHUTDOWN
;
645 } else if (MyStriCmp(FlagName
, L
"apple_recovery")) {
646 GlobalConfig
.ShowTools
[i
- 1] = TAG_APPLE_RECOVERY
;
647 } else if (MyStriCmp(FlagName
, L
"windows_recovery")) {
648 GlobalConfig
.ShowTools
[i
- 1] = TAG_WINDOWS_RECOVERY
;
649 } else if (MyStriCmp(FlagName
, L
"mok_tool")) {
650 GlobalConfig
.ShowTools
[i
- 1] = TAG_MOK_TOOL
;
651 } else if (MyStriCmp(FlagName
, L
"csr_rotate")) {
652 GlobalConfig
.ShowTools
[i
- 1] = TAG_CSR_ROTATE
;
653 } else if (MyStriCmp(FlagName
, L
"firmware")) {
654 GlobalConfig
.ShowTools
[i
- 1] = TAG_FIRMWARE
;
655 } else if (MyStriCmp(FlagName
, L
"memtest86") || MyStriCmp(FlagName
, L
"memtest")) {
656 GlobalConfig
.ShowTools
[i
- 1] = TAG_MEMTEST
;
657 } else if (MyStriCmp(FlagName
, L
"netboot")) {
658 GlobalConfig
.ShowTools
[i
- 1] = TAG_NETBOOT
;
660 Print(L
" unknown showtools flag: '%s'\n", FlagName
);
662 } // showtools options
664 } else if (MyStriCmp(TokenList
[0], L
"banner")) {
665 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.BannerFileName
));
667 } else if (MyStriCmp(TokenList
[0], L
"banner_scale") && (TokenCount
== 2)) {
668 if (MyStriCmp(TokenList
[1], L
"noscale")) {
669 GlobalConfig
.BannerScale
= BANNER_NOSCALE
;
670 } else if (MyStriCmp(TokenList
[1], L
"fillscreen") || MyStriCmp(TokenList
[1], L
"fullscreen")) {
671 GlobalConfig
.BannerScale
= BANNER_FILLSCREEN
;
673 Print(L
" unknown banner_type flag: '%s'\n", TokenList
[1]);
676 } else if (MyStriCmp(TokenList
[0], L
"small_icon_size") && (TokenCount
== 2)) {
677 HandleInt(TokenList
, TokenCount
, &i
);
679 GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] = i
;
681 } else if (MyStriCmp(TokenList
[0], L
"big_icon_size") && (TokenCount
== 2)) {
682 HandleInt(TokenList
, TokenCount
, &i
);
684 GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] = i
;
685 GlobalConfig
.IconSizes
[ICON_SIZE_BADGE
] = i
/ 4;
688 } else if (MyStriCmp(TokenList
[0], L
"selection_small")) {
689 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionSmallFileName
));
691 } else if (MyStriCmp(TokenList
[0], L
"selection_big")) {
692 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionBigFileName
));
694 } else if (MyStriCmp(TokenList
[0], L
"default_selection")) {
695 if (TokenCount
== 4) {
696 SetDefaultByTime(TokenList
, &(GlobalConfig
.DefaultSelection
));
698 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.DefaultSelection
));
701 } else if (MyStriCmp(TokenList
[0], L
"textonly")) {
702 GlobalConfig
.TextOnly
= HandleBoolean(TokenList
, TokenCount
);
704 } else if (MyStriCmp(TokenList
[0], L
"textmode")) {
705 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.RequestedTextMode
));
707 } else if (MyStriCmp(TokenList
[0], L
"resolution") && ((TokenCount
== 2) || (TokenCount
== 3))) {
708 GlobalConfig
.RequestedScreenWidth
= Atoi(TokenList
[1]);
710 GlobalConfig
.RequestedScreenHeight
= Atoi(TokenList
[2]);
712 GlobalConfig
.RequestedScreenHeight
= 0;
714 } else if (MyStriCmp(TokenList
[0], L
"screensaver")) {
715 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScreensaverTime
));
717 } else if (MyStriCmp(TokenList
[0], L
"use_graphics_for")) {
718 if ((TokenCount
== 2) || ((TokenCount
> 2) && (!MyStriCmp(TokenList
[1], L
"+"))))
719 GlobalConfig
.GraphicsFor
= 0;
720 for (i
= 1; i
< TokenCount
; i
++) {
721 if (MyStriCmp(TokenList
[i
], L
"osx")) {
722 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_OSX
;
723 } else if (MyStriCmp(TokenList
[i
], L
"linux")) {
724 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_LINUX
;
725 } else if (MyStriCmp(TokenList
[i
], L
"elilo")) {
726 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_ELILO
;
727 } else if (MyStriCmp(TokenList
[i
], L
"grub")) {
728 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_GRUB
;
729 } else if (MyStriCmp(TokenList
[i
], L
"windows")) {
730 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_WINDOWS
;
732 } // for (graphics_on tokens)
734 } else if (MyStriCmp(TokenList
[0], L
"font") && (TokenCount
== 2)) {
735 egLoadFont(TokenList
[1]);
737 } else if (MyStriCmp(TokenList
[0], L
"scan_all_linux_kernels")) {
738 GlobalConfig
.ScanAllLinux
= HandleBoolean(TokenList
, TokenCount
);
740 } else if (MyStriCmp(TokenList
[0], L
"fold_linux_kernels")) {
741 GlobalConfig
.FoldLinuxKernels
= HandleBoolean(TokenList
, TokenCount
);
743 } else if (MyStriCmp(TokenList
[0], L
"max_tags")) {
744 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.MaxTags
));
746 } else if (MyStriCmp(TokenList
[0], L
"enable_and_lock_vmx")) {
747 GlobalConfig
.EnableAndLockVMX
= HandleBoolean(TokenList
, TokenCount
);
749 } else if (MyStriCmp(TokenList
[0], L
"spoof_osx_version")) {
750 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SpoofOSXVersion
));
752 } else if (MyStriCmp(TokenList
[0], L
"csr_values")) {
753 HandleHexes(TokenList
, TokenCount
, CSR_MAX_LEGAL_VALUE
, &(GlobalConfig
.CsrValues
));
755 } else if (MyStriCmp(TokenList
[0], L
"include") && (TokenCount
== 2) && MyStriCmp(FileName
, GlobalConfig
.ConfigFilename
)) {
756 if (!MyStriCmp(TokenList
[1], FileName
)) {
757 ReadConfig(TokenList
[1]);
762 FreeTokenLine(&TokenList
, &TokenCount
);
764 if ((GlobalConfig
.DontScanFiles
) && (GlobalConfig
.WindowsRecoveryFiles
))
765 MergeStrings(&(GlobalConfig
.DontScanFiles
), GlobalConfig
.WindowsRecoveryFiles
, L
',');
766 MyFreePool(File
.Buffer
);
768 if (!FileExists(SelfDir
, L
"icons") && !FileExists(SelfDir
, GlobalConfig
.IconsDir
)) {
769 Print(L
"Icons directory doesn't exist; setting textonly = TRUE!\n");
770 GlobalConfig
.TextOnly
= TRUE
;
772 } /* VOID ReadConfig() */
774 // Finds a volume with the specified Identifier (a filesystem label, a
775 // partition name, a partition GUID, or a number followed by a colon). If
776 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
777 // Returns TRUE if a match was found, FALSE if not.
778 static BOOLEAN
FindVolume(REFIT_VOLUME
**Volume
, CHAR16
*Identifier
) {
779 UINTN i
= 0, CountedVolumes
= 0, Length
;
781 BOOLEAN Found
= FALSE
, IdIsGuid
= FALSE
;
782 EFI_GUID VolGuid
, NullGuid
= NULL_GUID_VALUE
;
784 VolGuid
= StringAsGuid(Identifier
);
785 Length
= StrLen(Identifier
);
786 if ((Length
>= 2) && (Identifier
[Length
- 1] == L
':') &&
787 (Identifier
[0] >= L
'0') && (Identifier
[0] <= L
'9')) {
788 Number
= (INTN
) Atoi(Identifier
);
789 } else if (IsGuid(Identifier
)) {
792 while ((i
< VolumesCount
) && (!Found
)) {
793 if (Number
>= 0) { // User specified a volume by number
794 if (Volumes
[i
]->IsReadable
) {
795 if (CountedVolumes
== Number
) {
796 *Volume
= Volumes
[i
];
801 } else { // User specified a volume by label or GUID
802 if (MyStriCmp(Identifier
, Volumes
[i
]->VolName
) || MyStriCmp(Identifier
, Volumes
[i
]->PartName
)) {
803 *Volume
= Volumes
[i
];
806 if (IdIsGuid
&& !Found
) {
807 if (GuidsAreEqual(&VolGuid
, &(Volumes
[i
]->PartGuid
)) && !GuidsAreEqual(&NullGuid
, &(Volumes
[i
]->PartGuid
))) {
808 *Volume
= Volumes
[i
];
816 } // static VOID FindVolume()
818 static VOID
AddSubmenu(LOADER_ENTRY
*Entry
, REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
819 REFIT_MENU_SCREEN
*SubScreen
;
820 LOADER_ENTRY
*SubEntry
;
824 SubScreen
= InitializeSubScreen(Entry
);
826 // Set defaults for the new entry; will be modified based on lines read from the config. file....
827 SubEntry
= InitializeLoaderEntry(Entry
);
829 if ((SubEntry
== NULL
) || (SubScreen
== NULL
))
831 SubEntry
->me
.Title
= StrDuplicate(Title
);
833 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StrCmp(TokenList
[0], L
"}") != 0)) {
835 if (MyStriCmp(TokenList
[0], L
"loader") && (TokenCount
> 1)) { // set the boot loader filename
836 MyFreePool(SubEntry
->LoaderPath
);
837 SubEntry
->LoaderPath
= StrDuplicate(TokenList
[1]);
838 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
840 } else if (MyStriCmp(TokenList
[0], L
"volume") && (TokenCount
> 1)) {
841 if (FindVolume(&Volume
, TokenList
[1])) {
842 if ((Volume
!= NULL
) && (Volume
->IsReadable
) && (Volume
->RootDir
)) {
843 MyFreePool(SubEntry
->me
.Title
);
844 SubEntry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
845 SPrint(SubEntry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", Volume
->VolName
);
846 SubEntry
->me
.BadgeImage
= Volume
->VolBadgeImage
;
847 SubEntry
->VolName
= Volume
->VolName
;
848 } // if volume is readable
851 } else if (MyStriCmp(TokenList
[0], L
"initrd")) {
852 MyFreePool(SubEntry
->InitrdPath
);
853 SubEntry
->InitrdPath
= NULL
;
854 if (TokenCount
> 1) {
855 SubEntry
->InitrdPath
= StrDuplicate(TokenList
[1]);
858 } else if (MyStriCmp(TokenList
[0], L
"options")) {
859 MyFreePool(SubEntry
->LoadOptions
);
860 SubEntry
->LoadOptions
= NULL
;
861 if (TokenCount
> 1) {
862 SubEntry
->LoadOptions
= StrDuplicate(TokenList
[1]);
865 } else if (MyStriCmp(TokenList
[0], L
"add_options") && (TokenCount
> 1)) {
866 MergeStrings(&SubEntry
->LoadOptions
, TokenList
[1], L
' ');
868 } else if (MyStriCmp(TokenList
[0], L
"graphics") && (TokenCount
> 1)) {
869 SubEntry
->UseGraphicsMode
= MyStriCmp(TokenList
[1], L
"on");
871 } else if (MyStriCmp(TokenList
[0], L
"disabled")) {
872 SubEntry
->Enabled
= FALSE
;
875 FreeTokenLine(&TokenList
, &TokenCount
);
878 if (SubEntry
->InitrdPath
!= NULL
) {
879 MergeStrings(&SubEntry
->LoadOptions
, L
"initrd=", L
' ');
880 MergeStrings(&SubEntry
->LoadOptions
, SubEntry
->InitrdPath
, 0);
881 MyFreePool(SubEntry
->InitrdPath
);
882 SubEntry
->InitrdPath
= NULL
;
884 if (SubEntry
->Enabled
== TRUE
) {
885 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
887 Entry
->me
.SubScreen
= SubScreen
;
888 } // VOID AddSubmenu()
890 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
891 // that entry. The calling function is then responsible for adding the entry to the
893 static LOADER_ENTRY
* AddStanzaEntries(REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
897 BOOLEAN DefaultsSet
= FALSE
, AddedSubmenu
= FALSE
;
898 REFIT_VOLUME
*CurrentVolume
= Volume
;
900 // prepare the menu entry
901 Entry
= InitializeLoaderEntry(NULL
);
905 Entry
->Title
= StrDuplicate(Title
);
906 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
907 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
909 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
910 Entry
->VolName
= CurrentVolume
->VolName
;
912 // Parse the config file to add options for a single stanza, terminating when the token
913 // is "}" or when the end of file is reached.
914 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StrCmp(TokenList
[0], L
"}") != 0)) {
915 if (MyStriCmp(TokenList
[0], L
"loader") && (TokenCount
> 1)) { // set the boot loader filename
916 Entry
->LoaderPath
= StrDuplicate(TokenList
[1]);
917 Entry
->DevicePath
= FileDevicePath(CurrentVolume
->DeviceHandle
, Entry
->LoaderPath
);
918 SetLoaderDefaults(Entry
, TokenList
[1], CurrentVolume
);
919 MyFreePool(Entry
->LoadOptions
);
920 Entry
->LoadOptions
= NULL
; // Discard default options, if any
923 } else if (MyStriCmp(TokenList
[0], L
"volume") && (TokenCount
> 1)) {
924 if (FindVolume(&CurrentVolume
, TokenList
[1])) {
925 if ((CurrentVolume
!= NULL
) && (CurrentVolume
->IsReadable
) && (CurrentVolume
->RootDir
)) {
926 MyFreePool(Entry
->me
.Title
);
927 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
928 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
929 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
930 Entry
->VolName
= CurrentVolume
->VolName
;
931 } // if volume is readable
934 } else if (MyStriCmp(TokenList
[0], L
"icon") && (TokenCount
> 1)) {
935 MyFreePool(Entry
->me
.Image
);
936 Entry
->me
.Image
= egLoadIcon(CurrentVolume
->RootDir
, TokenList
[1], GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
937 if (Entry
->me
.Image
== NULL
) {
938 Entry
->me
.Image
= DummyImage(GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
941 } else if (MyStriCmp(TokenList
[0], L
"initrd") && (TokenCount
> 1)) {
942 MyFreePool(Entry
->InitrdPath
);
943 Entry
->InitrdPath
= StrDuplicate(TokenList
[1]);
945 } else if (MyStriCmp(TokenList
[0], L
"options") && (TokenCount
> 1)) {
946 MyFreePool(Entry
->LoadOptions
);
947 Entry
->LoadOptions
= StrDuplicate(TokenList
[1]);
949 } else if (MyStriCmp(TokenList
[0], L
"ostype") && (TokenCount
> 1)) {
950 if (TokenCount
> 1) {
951 Entry
->OSType
= TokenList
[1][0];
954 } else if (MyStriCmp(TokenList
[0], L
"graphics") && (TokenCount
> 1)) {
955 Entry
->UseGraphicsMode
= MyStriCmp(TokenList
[1], L
"on");
957 } else if (MyStriCmp(TokenList
[0], L
"disabled")) {
958 Entry
->Enabled
= FALSE
;
960 } else if (MyStriCmp(TokenList
[0], L
"submenuentry") && (TokenCount
> 1)) {
961 AddSubmenu(Entry
, File
, CurrentVolume
, TokenList
[1]);
964 } // set options to pass to the loader program
965 FreeTokenLine(&TokenList
, &TokenCount
);
969 AddMenuEntry(Entry
->me
.SubScreen
, &MenuEntryReturn
);
971 if (Entry
->InitrdPath
) {
972 MergeStrings(&Entry
->LoadOptions
, L
"initrd=", L
' ');
973 MergeStrings(&Entry
->LoadOptions
, Entry
->InitrdPath
, 0);
974 MyFreePool(Entry
->InitrdPath
);
975 Entry
->InitrdPath
= NULL
;
979 SetLoaderDefaults(Entry
, L
"\\EFI\\BOOT\\nemo.efi", CurrentVolume
); // user included no "loader" line; use bogus one
982 } // static VOID AddStanzaEntries()
984 // Read the user-configured menu entries from refind.conf and add or delete
985 // entries based on the contents of that file....
986 VOID
ScanUserConfigured(CHAR16
*FileName
)
990 REFIT_VOLUME
*Volume
;
992 CHAR16
*Title
= NULL
;
993 UINTN TokenCount
, size
;
996 if (FileExists(SelfDir
, FileName
)) {
997 Status
= ReadFile(SelfDir
, FileName
, &File
, &size
);
998 if (EFI_ERROR(Status
))
1001 Volume
= SelfVolume
;
1003 while ((TokenCount
= ReadTokenLine(&File
, &TokenList
)) > 0) {
1004 if (MyStriCmp(TokenList
[0], L
"menuentry") && (TokenCount
> 1)) {
1005 Title
= StrDuplicate(TokenList
[1]);
1006 Entry
= AddStanzaEntries(&File
, Volume
, TokenList
[1]);
1007 if (Entry
->Enabled
) {
1008 if (Entry
->me
.SubScreen
== NULL
)
1009 GenerateSubScreen(Entry
, Volume
, TRUE
);
1010 AddPreparedLoaderEntry(Entry
);
1016 } else if (MyStriCmp(TokenList
[0], L
"include") && (TokenCount
== 2) &&
1017 MyStriCmp(FileName
, GlobalConfig
.ConfigFilename
)) {
1018 if (!MyStriCmp(TokenList
[1], FileName
)) {
1019 ScanUserConfigured(TokenList
[1]);
1023 FreeTokenLine(&TokenList
, &TokenCount
);
1026 } // VOID ScanUserConfigured()
1028 // Create an options file based on /etc/fstab. The resulting file has two options
1029 // lines, one of which boots the system with "ro root={rootfs}" and the other of
1030 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
1031 // filesystem identifier associated with the "/" line in /etc/fstab.
1032 static REFIT_FILE
* GenerateOptionsFromEtcFstab(REFIT_VOLUME
*Volume
) {
1033 UINTN TokenCount
, i
;
1034 REFIT_FILE
*Options
= NULL
, *Fstab
= NULL
;
1036 CHAR16
**TokenList
, *Line
, Root
[100];
1038 if (FileExists(Volume
->RootDir
, L
"\\etc\\fstab")) {
1039 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
1040 Fstab
= AllocateZeroPool(sizeof(REFIT_FILE
));
1041 Status
= ReadFile(Volume
->RootDir
, L
"\\etc\\fstab", Fstab
, &i
);
1042 if (CheckError(Status
, L
"while reading /etc/fstab")) {
1043 if (Options
!= NULL
)
1049 } else { // File read; locate root fs and create entries
1050 Options
->Encoding
= ENCODING_UTF16_LE
;
1051 while ((TokenCount
= ReadTokenLine(Fstab
, &TokenList
)) > 0) {
1052 if (TokenCount
> 2) {
1054 if (StrCmp(TokenList
[1], L
"\\") == 0) {
1055 SPrint(Root
, 99, L
"%s", TokenList
[0]);
1056 } else if (StrCmp(TokenList
[2], L
"\\") == 0) {
1057 SPrint(Root
, 99, L
"%s=%s", TokenList
[0], TokenList
[1]);
1058 } // if/elseif/elseif
1059 if (Root
[0] != L
'\0') {
1060 for (i
= 0; i
< StrLen(Root
); i
++)
1061 if (Root
[i
] == '\\')
1063 Line
= PoolPrint(L
"\"Boot with normal options\" \"ro root=%s\"\n", Root
);
1064 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
1066 Line
= PoolPrint(L
"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root
);
1067 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
1068 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
1071 FreeTokenLine(&TokenList
, &TokenCount
);
1074 if (Options
->Buffer
) {
1075 Options
->Current8Ptr
= (CHAR8
*)Options
->Buffer
;
1076 Options
->End8Ptr
= Options
->Current8Ptr
+ Options
->BufferSize
;
1077 Options
->Current16Ptr
= (CHAR16
*)Options
->Buffer
;
1078 Options
->End16Ptr
= Options
->Current16Ptr
+ (Options
->BufferSize
>> 1);
1080 MyFreePool(Options
);
1084 MyFreePool(Fstab
->Buffer
);
1086 } // if/else file read error
1087 } // if /etc/fstab exists
1089 } // GenerateOptionsFromEtcFstab()
1091 // Create options from partition type codes. Specifically, if the earlier
1092 // partition scan found a partition with a type code corresponding to a root
1093 // filesystem according to the Freedesktop.org Discoverable Partitions Spec
1094 // (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/),
1095 // this function returns an appropriate file with two lines, one with
1096 // "ro root=/dev/disk/by-partuuid/{GUID}" and the other with that plus "single".
1097 // Note that this function returns the LAST partition found with the
1098 // appropriate type code, so this will work poorly on dual-boot systems or
1099 // if the type code is set incorrectly.
1100 static REFIT_FILE
* GenerateOptionsFromPartTypes(VOID
) {
1101 REFIT_FILE
*Options
= NULL
;
1102 CHAR16
*Line
, *GuidString
, *WriteStatus
;
1104 if (GlobalConfig
.DiscoveredRoot
) {
1105 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
1107 Options
->Encoding
= ENCODING_UTF16_LE
;
1108 GuidString
= GuidAsString(&(GlobalConfig
.DiscoveredRoot
->PartGuid
));
1109 WriteStatus
= GlobalConfig
.DiscoveredRoot
->IsMarkedReadOnly
? L
"ro" : L
"rw";
1110 ToLower(GuidString
);
1112 Line
= PoolPrint(L
"\"Boot with normal options\" \"%s root=/dev/disk/by-partuuid/%s\"\n", WriteStatus
, GuidString
);
1113 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
1115 Line
= PoolPrint(L
"\"Boot into single-user mode\" \"%s root=/dev/disk/by-partuuid/%s single\"\n", WriteStatus
, GuidString
);
1116 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
1118 MyFreePool(GuidString
);
1119 } // if (GuidString)
1120 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
1122 Options
->Current8Ptr
= (CHAR8
*)Options
->Buffer
;
1123 Options
->End8Ptr
= Options
->Current8Ptr
+ Options
->BufferSize
;
1124 Options
->Current16Ptr
= (CHAR16
*)Options
->Buffer
;
1125 Options
->End16Ptr
= Options
->Current16Ptr
+ (Options
->BufferSize
>> 1);
1126 } // if (Options allocated OK)
1127 } // if (partition has root GUID)
1129 } // REFIT_FILE * GenerateOptionsFromPartTypes()
1131 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1132 // and Volume variables identify the location of the options file, but not its name --
1133 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1134 // file in the target directory, and this function finds the file with a name in the
1135 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1136 // directory and loads it. This function tries multiple files because I originally
1137 // used the filename linux.conf, but close on the heels of that decision, the Linux
1138 // kernel developers decided to use that name for a similar purpose, but with a
1139 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1140 // but I want a migration period in which both names are used.
1141 // If a rEFInd options file can't be found, try to generate minimal options from
1142 // /etc/fstab on the same volume as the kernel. This typically works only if the
1143 // kernel is being read from the Linux root filesystem.
1145 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1147 REFIT_FILE
* ReadLinuxOptionsFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1148 CHAR16
*OptionsFilename
, *FullFilename
;
1149 BOOLEAN GoOn
= TRUE
, FileFound
= FALSE
;
1151 REFIT_FILE
*File
= NULL
;
1155 OptionsFilename
= FindCommaDelimited(LINUX_OPTIONS_FILENAMES
, i
++);
1156 FullFilename
= FindPath(LoaderPath
);
1157 if ((OptionsFilename
!= NULL
) && (FullFilename
!= NULL
)) {
1158 MergeStrings(&FullFilename
, OptionsFilename
, '\\');
1159 if (FileExists(Volume
->RootDir
, FullFilename
)) {
1160 File
= AllocateZeroPool(sizeof(REFIT_FILE
));
1161 Status
= ReadFile(Volume
->RootDir
, FullFilename
, File
, &size
);
1162 if (CheckError(Status
, L
"while loading the Linux options file")) {
1171 } else { // a filename string is NULL
1174 MyFreePool(OptionsFilename
);
1175 MyFreePool(FullFilename
);
1176 OptionsFilename
= FullFilename
= NULL
;
1179 // No refind_linux.conf file; look for /etc/fstab and try to pull values from there....
1180 File
= GenerateOptionsFromEtcFstab(Volume
);
1181 // If still no joy, try to use Freedesktop.org Discoverable Partitions Spec....
1183 File
= GenerateOptionsFromPartTypes();
1186 } // static REFIT_FILE * ReadLinuxOptionsFile()
1188 // Retrieve a single line of options from a Linux kernel options file
1189 CHAR16
* GetFirstOptionsFromFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1191 CHAR16
*Options
= NULL
;
1195 File
= ReadLinuxOptionsFile(LoaderPath
, Volume
);
1197 TokenCount
= ReadTokenLine(File
, &TokenList
);
1199 Options
= StrDuplicate(TokenList
[1]);
1200 FreeTokenLine(&TokenList
, &TokenCount
);
1204 } // static CHAR16 * GetOptionsFile()