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-2014 Roderick W. Smith
40 * Modifications distributed under the terms of the GNU General Public
41 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
42 * with this source code or binaries made from it.
52 #include "../include/refit_call_wrapper.h"
53 #include "../mok/mok.h"
57 #define LINUX_OPTIONS_FILENAMES L"refind_linux.conf,refind-linux.conf"
58 #define MAXCONFIGFILESIZE (128*1024)
60 #define ENCODING_ISO8859_1 (0)
61 #define ENCODING_UTF8 (1)
62 #define ENCODING_UTF16_LE (2)
64 #define GetTime ST->RuntimeServices->GetTime
65 #define LAST_MINUTE 1439 /* Last minute of a day */
67 static REFIT_MENU_ENTRY MenuEntryReturn
= { L
"Return to Main Menu", TAG_RETURN
, 0, 0, 0, NULL
, NULL
, NULL
};
70 // read a file into a buffer
73 EFI_STATUS
ReadFile(IN EFI_FILE_HANDLE BaseDir
, IN CHAR16
*FileName
, IN OUT REFIT_FILE
*File
, OUT UINTN
*size
)
76 EFI_FILE_HANDLE FileHandle
;
77 EFI_FILE_INFO
*FileInfo
;
84 // read the file, allocating a buffer on the way
85 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
86 SPrint(Message
, 255, L
"while loading the file '%s'", FileName
);
87 if (CheckError(Status
, Message
))
90 FileInfo
= LibFileInfo(FileHandle
);
91 if (FileInfo
== NULL
) {
92 // TODO: print and register the error
93 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
94 return EFI_LOAD_ERROR
;
96 ReadSize
= FileInfo
->FileSize
;
99 File
->BufferSize
= (UINTN
)ReadSize
;
100 File
->Buffer
= AllocatePool(File
->BufferSize
);
101 if (File
->Buffer
== NULL
) {
103 return EFI_OUT_OF_RESOURCES
;
105 *size
= File
->BufferSize
;
107 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &File
->BufferSize
, File
->Buffer
);
108 if (CheckError(Status
, Message
)) {
109 MyFreePool(File
->Buffer
);
111 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
114 Status
= refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
117 File
->Current8Ptr
= (CHAR8
*)File
->Buffer
;
118 File
->End8Ptr
= File
->Current8Ptr
+ File
->BufferSize
;
119 File
->Current16Ptr
= (CHAR16
*)File
->Buffer
;
120 File
->End16Ptr
= File
->Current16Ptr
+ (File
->BufferSize
>> 1);
123 File
->Encoding
= ENCODING_ISO8859_1
; // default: 1:1 translation of CHAR8 to CHAR16
124 if (File
->BufferSize
>= 4) {
125 if (File
->Buffer
[0] == 0xFF && File
->Buffer
[1] == 0xFE) {
126 // BOM in UTF-16 little endian (or UTF-32 little endian)
127 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
128 File
->Current16Ptr
++;
129 } else if (File
->Buffer
[0] == 0xEF && File
->Buffer
[1] == 0xBB && File
->Buffer
[2] == 0xBF) {
131 File
->Encoding
= ENCODING_UTF8
; // translate from UTF-8 to UTF-16
132 File
->Current8Ptr
+= 3;
133 } else if (File
->Buffer
[1] == 0 && File
->Buffer
[3] == 0) {
134 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
136 // TODO: detect other encodings as they are implemented
143 // get a single line of text from a file
146 static CHAR16
*ReadLine(REFIT_FILE
*File
)
151 if (File
->Buffer
== NULL
)
154 if (File
->Encoding
== ENCODING_ISO8859_1
|| File
->Encoding
== ENCODING_UTF8
) {
156 CHAR8
*p
, *LineStart
, *LineEnd
;
158 p
= File
->Current8Ptr
;
159 if (p
>= File
->End8Ptr
)
163 for (; p
< File
->End8Ptr
; p
++)
164 if (*p
== 13 || *p
== 10)
167 for (; p
< File
->End8Ptr
; p
++)
168 if (*p
!= 13 && *p
!= 10)
170 File
->Current8Ptr
= p
;
172 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
173 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
178 if (File
->Encoding
== ENCODING_ISO8859_1
) {
179 for (p
= LineStart
; p
< LineEnd
; )
181 } else if (File
->Encoding
== ENCODING_UTF8
) {
182 // TODO: actually handle UTF-8
183 for (p
= LineStart
; p
< LineEnd
; )
188 } else if (File
->Encoding
== ENCODING_UTF16_LE
) {
190 CHAR16
*p
, *LineStart
, *LineEnd
;
192 p
= File
->Current16Ptr
;
193 if (p
>= File
->End16Ptr
)
197 for (; p
< File
->End16Ptr
; p
++)
198 if (*p
== 13 || *p
== 10)
201 for (; p
< File
->End16Ptr
; p
++)
202 if (*p
!= 13 && *p
!= 10)
204 File
->Current16Ptr
= p
;
206 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
207 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
211 for (p
= LineStart
, q
= Line
; p
< LineEnd
; )
216 return NULL
; // unsupported encoding
221 // Returns FALSE if *p points to the end of a token, TRUE otherwise.
222 // Also modifies *p **IF** the first and second characters are both
223 // quotes ('"'); it deletes one of them.
224 static BOOLEAN
KeepReading(IN OUT CHAR16
*p
, IN OUT BOOLEAN
*IsQuoted
) {
225 BOOLEAN MoreToRead
= FALSE
;
228 if ((p
== NULL
) || (IsQuoted
== NULL
))
234 if ((*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '#' && *p
!= ',') || *IsQuoted
) {
239 Temp
= StrDuplicate(&p
[1]);
246 *IsQuoted
= !(*IsQuoted
);
248 } // if/else second character is a quote
249 } // if first character is a quote
252 } // BOOLEAN KeepReading()
255 // get a line of tokens from a file
257 UINTN
ReadTokenLine(IN REFIT_FILE
*File
, OUT CHAR16
***TokenList
)
259 BOOLEAN LineFinished
, IsQuoted
= FALSE
;
260 CHAR16
*Line
, *Token
, *p
;
261 UINTN TokenCount
= 0;
265 while (TokenCount
== 0) {
266 Line
= ReadLine(File
);
271 LineFinished
= FALSE
;
272 while (!LineFinished
) {
273 // skip whitespace & find start of token
274 while ((*p
== ' ' || *p
== '\t' || *p
== '=' || *p
== ',') && !IsQuoted
)
276 if (*p
== 0 || *p
== '#')
280 IsQuoted
= !IsQuoted
;
286 while (KeepReading(p
, &IsQuoted
)) {
287 if ((*p
== L
'/') && !IsQuoted
) // Switch Unix-style to DOS-style directory separators
291 if (*p
== L
'\0' || *p
== L
'#')
295 AddListElement((VOID
***)TokenList
, &TokenCount
, (VOID
*)StrDuplicate(Token
));
301 } /* ReadTokenLine() */
303 VOID
FreeTokenLine(IN OUT CHAR16
***TokenList
, IN OUT UINTN
*TokenCount
)
305 // TODO: also free the items
306 FreeList((VOID
***)TokenList
, TokenCount
);
309 // handle a parameter with a single integer argument
310 static VOID
HandleInt(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT UINTN
*Value
)
312 if (TokenCount
== 2) {
313 if (StriCmp(TokenList
[1], L
"-1") == 0)
316 *Value
= Atoi(TokenList
[1]);
320 // handle a parameter with a single string argument
321 static VOID
HandleString(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
322 if ((TokenCount
== 2) && Target
) {
323 if ((StrLen(TokenList
[1]) > 1) && (TokenList
[1][0] == L
'+') &&
324 ((TokenList
[1][1] == L
',') || (TokenList
[1][1] == L
' '))) {
326 MergeStrings(Target
, TokenList
[1] + 2, L
',');
328 *Target
= StrDuplicate(TokenList
[1] + 2);
332 *Target
= StrDuplicate(TokenList
[1]);
335 } // static VOID HandleString()
337 // Handle a parameter with a series of string arguments, to replace or be added to a
338 // comma-delimited list. Passes each token through the CleanUpPathNameSlashes() function
339 // to ensure consistency in subsequent comparisons of filenames. If the first
340 // non-keyword token is "+", the list is added to the existing target string; otherwise,
341 // the tokens replace the current string.
342 static VOID
HandleStrings(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
344 BOOLEAN AddMode
= FALSE
;
346 if ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") == 0)) {
350 if ((*Target
!= NULL
) && !AddMode
) {
354 for (i
= 1; i
< TokenCount
; i
++) {
355 if ((i
!= 1) || !AddMode
) {
356 CleanUpPathNameSlashes(TokenList
[i
]);
357 MergeStrings(Target
, TokenList
[i
], L
',');
360 } // static VOID HandleStrings()
362 // Convert TimeString (in "HH:MM" format) to a pure-minute format. Values should be
363 // in the range from 0 (for 00:00, or midnight) to 1439 (for 23:59; aka LAST_MINUTE).
364 // Any value outside that range denotes an error in the specification. Note that if
365 // the input is a number that includes no colon, this function will return the original
366 // number in UINTN form.
367 static UINTN
HandleTime(IN CHAR16
*TimeString
) {
368 UINTN Hour
= 0, Minute
= 0, TimeLength
, i
= 0;
370 TimeLength
= StrLen(TimeString
);
371 while (i
< TimeLength
) {
372 if (TimeString
[i
] == L
':') {
376 if ((TimeString
[i
] >= L
'0') && (TimeString
[i
] <= '9')) {
378 Minute
+= (TimeString
[i
] - L
'0');
382 return (Hour
* 60 + Minute
);
383 } // BOOLEAN HandleTime()
385 static BOOLEAN
HandleBoolean(IN CHAR16
**TokenList
, IN UINTN TokenCount
) {
386 BOOLEAN TruthValue
= TRUE
;
388 if ((TokenCount
>= 2) && ((StriCmp(TokenList
[1], L
"0") == 0) ||
389 (StriCmp(TokenList
[1], L
"false") == 0) ||
390 (StriCmp(TokenList
[1], L
"off") == 0))) {
395 } // BOOLEAN HandleBoolean
397 // Sets the default boot loader IF the current time is within the bounds
398 // defined by the third and fourth tokens in the TokenList.
399 static VOID
SetDefaultByTime(IN CHAR16
**TokenList
, OUT CHAR16
**Default
) {
401 EFI_TIME CurrentTime
;
402 UINTN StartTime
= LAST_MINUTE
+ 1, EndTime
= LAST_MINUTE
+ 1, Now
;
403 BOOLEAN SetIt
= FALSE
;
405 StartTime
= HandleTime(TokenList
[2]);
406 EndTime
= HandleTime(TokenList
[3]);
408 if ((StartTime
<= LAST_MINUTE
) && (EndTime
<= LAST_MINUTE
)) {
409 Status
= refit_call2_wrapper(GetTime
, &CurrentTime
, NULL
);
410 if (Status
!= EFI_SUCCESS
)
412 Now
= CurrentTime
.Hour
* 60 + CurrentTime
.Minute
;
414 if (Now
> LAST_MINUTE
) { // Shouldn't happen; just being paranoid
415 Print(L
"Warning: Impossible system time: %d:%d\n", CurrentTime
.Hour
, CurrentTime
.Minute
);
417 } // if impossible time
419 if (StartTime
< EndTime
) { // Time range does NOT cross midnight
420 if ((Now
>= StartTime
) && (Now
<= EndTime
))
422 } else { // Time range DOES cross midnight
423 if ((Now
>= StartTime
) && (Now
<= EndTime
))
425 } // if/else time range crosses midnight
428 MyFreePool(*Default
);
429 *Default
= StrDuplicate(TokenList
[1]);
431 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
432 } // VOID SetDefaultByTime()
435 VOID
ReadConfig(CHAR16
*FileName
)
441 CHAR16
*TempStr
= NULL
;
443 EFI_GUID RefindGuid
= REFIND_GUID_VALUE
;
445 // Set a few defaults only if we're loading the default file.
446 if (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0) {
447 MyFreePool(GlobalConfig
.AlsoScan
);
448 GlobalConfig
.AlsoScan
= StrDuplicate(ALSO_SCAN_DIRS
);
449 MyFreePool(GlobalConfig
.DontScanDirs
);
451 if (SelfVolume
->VolName
) {
452 TempStr
= SelfVolume
->VolName
? StrDuplicate(SelfVolume
->VolName
) : NULL
;
454 TempStr
= AllocateZeroPool(256 * sizeof(CHAR16
));
456 SPrint(TempStr
, 255, L
"fs%d", SelfVolume
->VolNumber
);
459 MergeStrings(&TempStr
, SelfDirPath
, L
':');
460 MergeStrings(&TempStr
, MEMTEST_LOCATIONS
, L
',');
461 GlobalConfig
.DontScanDirs
= TempStr
;
462 MyFreePool(GlobalConfig
.DontScanFiles
);
463 GlobalConfig
.DontScanFiles
= StrDuplicate(DONT_SCAN_FILES
);
464 MergeStrings(&(GlobalConfig
.DontScanFiles
), MOK_NAMES
, L
',');
465 MyFreePool(GlobalConfig
.DontScanVolumes
);
466 GlobalConfig
.DontScanVolumes
= StrDuplicate(DONT_SCAN_VOLUMES
);
467 GlobalConfig
.WindowsRecoveryFiles
= StrDuplicate(WINDOWS_RECOVERY_FILES
);
468 if (GlobalConfig
.DefaultSelection
!= NULL
) {
469 MyFreePool(GlobalConfig
.DefaultSelection
);
470 GlobalConfig
.DefaultSelection
= NULL
;
472 Status
= EfivarGetRaw(&RefindGuid
, L
"PreviousBoot", (CHAR8
**) &(GlobalConfig
.DefaultSelection
), &i
);
473 if (Status
!= EFI_SUCCESS
)
474 GlobalConfig
.DefaultSelection
= NULL
;
477 if (!FileExists(SelfDir
, FileName
)) {
478 Print(L
"Configuration file '%s' missing!\n", FileName
);
479 if (!FileExists(SelfDir
, L
"icons")) {
480 Print(L
"Icons directory doesn't exist; setting textonly = TRUE!\n");
481 GlobalConfig
.TextOnly
= TRUE
;
486 Status
= ReadFile(SelfDir
, FileName
, &File
, &i
);
487 if (EFI_ERROR(Status
))
491 TokenCount
= ReadTokenLine(&File
, &TokenList
);
495 if (StriCmp(TokenList
[0], L
"timeout") == 0) {
496 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.Timeout
));
498 } else if (StriCmp(TokenList
[0], L
"hideui") == 0) {
499 for (i
= 1; i
< TokenCount
; i
++) {
500 FlagName
= TokenList
[i
];
501 if (StriCmp(FlagName
, L
"banner") == 0) {
502 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_BANNER
;
503 } else if (StriCmp(FlagName
, L
"label") == 0) {
504 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_LABEL
;
505 } else if (StriCmp(FlagName
, L
"singleuser") == 0) {
506 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SINGLEUSER
;
507 } else if (StriCmp(FlagName
, L
"hwtest") == 0) {
508 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HWTEST
;
509 } else if (StriCmp(FlagName
, L
"arrows") == 0) {
510 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_ARROWS
;
511 } else if (StriCmp(FlagName
, L
"hints") == 0) {
512 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HINTS
;
513 } else if (StriCmp(FlagName
, L
"editor") == 0) {
514 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_EDITOR
;
515 } else if (StriCmp(FlagName
, L
"safemode") == 0) {
516 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SAFEMODE
;
517 } else if (StriCmp(FlagName
, L
"all") == 0) {
518 GlobalConfig
.HideUIFlags
= HIDEUI_FLAG_ALL
;
520 Print(L
" unknown hideui flag: '%s'\n", FlagName
);
524 } else if (StriCmp(TokenList
[0], L
"icons_dir") == 0) {
525 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.IconsDir
));
527 } else if (StriCmp(TokenList
[0], L
"scanfor") == 0) {
528 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
530 GlobalConfig
.ScanFor
[i
] = TokenList
[i
][0];
532 GlobalConfig
.ScanFor
[i
] = ' ';
535 } else if (StriCmp(TokenList
[0], L
"uefi_deep_legacy_scan") == 0) {
536 GlobalConfig
.DeepLegacyScan
= HandleBoolean(TokenList
, TokenCount
);
538 } else if ((StriCmp(TokenList
[0], L
"scan_delay") == 0) && (TokenCount
== 2)) {
539 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScanDelay
));
541 } else if (StriCmp(TokenList
[0], L
"also_scan_dirs") == 0) {
542 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.AlsoScan
));
544 } else if ((StriCmp(TokenList
[0], L
"don't_scan_volumes") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_volumes") == 0)) {
545 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
546 MyFreePool(GlobalConfig
.DontScanVolumes
);
547 GlobalConfig
.DontScanVolumes
= NULL
;
548 for (i
= 1; i
< TokenCount
; i
++) {
549 MergeStrings(&GlobalConfig
.DontScanVolumes
, TokenList
[i
], L
',');
552 } else if ((StriCmp(TokenList
[0], L
"don't_scan_dirs") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_dirs") == 0)) {
553 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanDirs
));
555 } else if ((StriCmp(TokenList
[0], L
"don't_scan_files") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_files") == 0)) {
556 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanFiles
));
558 } else if (StriCmp(TokenList
[0], L
"windows_recovery_files") == 0) {
559 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.WindowsRecoveryFiles
));
561 } else if (StriCmp(TokenList
[0], L
"scan_driver_dirs") == 0) {
562 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DriverDirs
));
564 } else if (StriCmp(TokenList
[0], L
"showtools") == 0) {
565 SetMem(GlobalConfig
.ShowTools
, NUM_TOOLS
* sizeof(UINTN
), 0);
566 for (i
= 1; (i
< TokenCount
) && (i
< NUM_TOOLS
); i
++) {
567 FlagName
= TokenList
[i
];
568 if (StriCmp(FlagName
, L
"shell") == 0) {
569 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHELL
;
570 } else if (StriCmp(FlagName
, L
"gptsync") == 0) {
571 GlobalConfig
.ShowTools
[i
- 1] = TAG_GPTSYNC
;
572 } else if (StriCmp(FlagName
, L
"gdisk") == 0) {
573 GlobalConfig
.ShowTools
[i
- 1] = TAG_GDISK
;
574 } else if (StriCmp(FlagName
, L
"about") == 0) {
575 GlobalConfig
.ShowTools
[i
- 1] = TAG_ABOUT
;
576 } else if (StriCmp(FlagName
, L
"exit") == 0) {
577 GlobalConfig
.ShowTools
[i
- 1] = TAG_EXIT
;
578 } else if (StriCmp(FlagName
, L
"reboot") == 0) {
579 GlobalConfig
.ShowTools
[i
- 1] = TAG_REBOOT
;
580 } else if (StriCmp(FlagName
, L
"shutdown") == 0) {
581 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHUTDOWN
;
582 } else if (StriCmp(FlagName
, L
"apple_recovery") == 0) {
583 GlobalConfig
.ShowTools
[i
- 1] = TAG_APPLE_RECOVERY
;
584 } else if (StriCmp(FlagName
, L
"windows_recovery") == 0) {
585 GlobalConfig
.ShowTools
[i
- 1] = TAG_WINDOWS_RECOVERY
;
586 } else if (StriCmp(FlagName
, L
"mok_tool") == 0) {
587 GlobalConfig
.ShowTools
[i
- 1] = TAG_MOK_TOOL
;
588 } else if (StriCmp(FlagName
, L
"firmware") == 0) {
589 GlobalConfig
.ShowTools
[i
- 1] = TAG_FIRMWARE
;
590 } else if ((StriCmp(FlagName
, L
"memtest86") == 0) || (StriCmp(FlagName
, L
"memtest") == 0)) {
591 GlobalConfig
.ShowTools
[i
- 1] = TAG_MEMTEST
;
592 } else if (StriCmp(FlagName
, L
"netboot") == 0) {
593 GlobalConfig
.ShowTools
[i
- 1] = TAG_NETBOOT
;
595 Print(L
" unknown showtools flag: '%s'\n", FlagName
);
597 } // showtools options
599 } else if (StriCmp(TokenList
[0], L
"banner") == 0) {
600 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.BannerFileName
));
602 } else if ((StriCmp(TokenList
[0], L
"banner_scale") == 0) && (TokenCount
== 2)) {
603 if (StriCmp(TokenList
[1], L
"noscale") == 0) {
604 GlobalConfig
.BannerScale
= BANNER_NOSCALE
;
605 } else if ((StriCmp(TokenList
[1], L
"fillscreen") == 0) || (StriCmp(TokenList
[1], L
"fullscreen") == 0)) {
606 GlobalConfig
.BannerScale
= BANNER_FILLSCREEN
;
608 Print(L
" unknown banner_type flag: '%s'\n", TokenList
[1]);
611 } else if ((StriCmp(TokenList
[0], L
"small_icon_size") == 0) && (TokenCount
== 2)) {
612 HandleInt(TokenList
, TokenCount
, &i
);
614 GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] = i
;
616 } else if ((StriCmp(TokenList
[0], L
"big_icon_size") == 0) && (TokenCount
== 2)) {
617 HandleInt(TokenList
, TokenCount
, &i
);
619 GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] = i
;
620 GlobalConfig
.IconSizes
[ICON_SIZE_BADGE
] = i
/ 4;
623 } else if (StriCmp(TokenList
[0], L
"selection_small") == 0) {
624 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionSmallFileName
));
626 } else if (StriCmp(TokenList
[0], L
"selection_big") == 0) {
627 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionBigFileName
));
629 } else if (StriCmp(TokenList
[0], L
"default_selection") == 0) {
630 if (TokenCount
== 4) {
631 SetDefaultByTime(TokenList
, &(GlobalConfig
.DefaultSelection
));
633 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.DefaultSelection
));
636 } else if (StriCmp(TokenList
[0], L
"textonly") == 0) {
637 GlobalConfig
.TextOnly
= HandleBoolean(TokenList
, TokenCount
);
639 } else if (StriCmp(TokenList
[0], L
"textmode") == 0) {
640 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.RequestedTextMode
));
642 } else if ((StriCmp(TokenList
[0], L
"resolution") == 0) && ((TokenCount
== 2) || (TokenCount
== 3))) {
643 GlobalConfig
.RequestedScreenWidth
= Atoi(TokenList
[1]);
645 GlobalConfig
.RequestedScreenHeight
= Atoi(TokenList
[2]);
647 GlobalConfig
.RequestedScreenHeight
= 0;
649 } else if (StriCmp(TokenList
[0], L
"screensaver") == 0) {
650 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScreensaverTime
));
652 } else if (StriCmp(TokenList
[0], L
"use_graphics_for") == 0) {
653 if ((TokenCount
== 2) || ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") != 0)))
654 GlobalConfig
.GraphicsFor
= 0;
655 for (i
= 1; i
< TokenCount
; i
++) {
656 if (StriCmp(TokenList
[i
], L
"osx") == 0) {
657 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_OSX
;
658 } else if (StriCmp(TokenList
[i
], L
"linux") == 0) {
659 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_LINUX
;
660 } else if (StriCmp(TokenList
[i
], L
"elilo") == 0) {
661 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_ELILO
;
662 } else if (StriCmp(TokenList
[i
], L
"grub") == 0) {
663 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_GRUB
;
664 } else if (StriCmp(TokenList
[i
], L
"windows") == 0) {
665 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_WINDOWS
;
667 } // for (graphics_on tokens)
669 } else if ((StriCmp(TokenList
[0], L
"font") == 0) && (TokenCount
== 2)) {
670 egLoadFont(TokenList
[1]);
672 } else if (StriCmp(TokenList
[0], L
"scan_all_linux_kernels") == 0) {
673 GlobalConfig
.ScanAllLinux
= HandleBoolean(TokenList
, TokenCount
);
675 } else if (StriCmp(TokenList
[0], L
"max_tags") == 0) {
676 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.MaxTags
));
678 } else if (StriCmp(TokenList
[0], L
"enable_and_lock_vmx") == 0) {
679 GlobalConfig
.EnableAndLockVMX
= HandleBoolean(TokenList
, TokenCount
);
681 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
682 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
683 if (StriCmp(TokenList
[1], FileName
) != 0) {
684 ReadConfig(TokenList
[1]);
689 FreeTokenLine(&TokenList
, &TokenCount
);
691 if ((GlobalConfig
.DontScanFiles
) && (GlobalConfig
.WindowsRecoveryFiles
))
692 MergeStrings(&(GlobalConfig
.DontScanFiles
), GlobalConfig
.WindowsRecoveryFiles
, L
',');
693 MyFreePool(File
.Buffer
);
695 if (!FileExists(SelfDir
, L
"icons") && !FileExists(SelfDir
, GlobalConfig
.IconsDir
)) {
696 Print(L
"Icons directory doesn't exist; setting textonly = TRUE!\n");
697 GlobalConfig
.TextOnly
= TRUE
;
699 } /* VOID ReadConfig() */
701 // Finds a volume with the specified Identifier (a filesystem label, a
702 // partition name, a partition GUID, or a number followed by a colon). If
703 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
704 // Returns TRUE if a match was found, FALSE if not.
705 static BOOLEAN
FindVolume(REFIT_VOLUME
**Volume
, CHAR16
*Identifier
) {
706 UINTN i
= 0, CountedVolumes
= 0, Length
;
708 BOOLEAN Found
= FALSE
, IdIsGuid
= FALSE
;
709 EFI_GUID VolGuid
, NullGuid
= NULL_GUID_VALUE
;
711 VolGuid
= StringAsGuid(Identifier
);
712 Length
= StrLen(Identifier
);
713 if ((Length
>= 2) && (Identifier
[Length
- 1] == L
':') &&
714 (Identifier
[0] >= L
'0') && (Identifier
[0] <= L
'9')) {
715 Number
= (INTN
) Atoi(Identifier
);
716 } else if (IsGuid(Identifier
)) {
719 while ((i
< VolumesCount
) && (!Found
)) {
720 if (Number
>= 0) { // User specified a volume by number
721 if (Volumes
[i
]->IsReadable
) {
722 if (CountedVolumes
== Number
) {
723 *Volume
= Volumes
[i
];
728 } else { // User specified a volume by label or GUID
729 if ((StriCmp(Identifier
, Volumes
[i
]->VolName
) == 0) ||
730 (StriCmp(Identifier
, Volumes
[i
]->PartName
) == 0)) {
731 *Volume
= Volumes
[i
];
734 if (IdIsGuid
&& !Found
) {
735 if (GuidsAreEqual(&VolGuid
, &(Volumes
[i
]->PartGuid
)) && !GuidsAreEqual(&NullGuid
, &(Volumes
[i
]->PartGuid
))) {
736 *Volume
= Volumes
[i
];
744 } // static VOID FindVolume()
746 static VOID
AddSubmenu(LOADER_ENTRY
*Entry
, REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
747 REFIT_MENU_SCREEN
*SubScreen
;
748 LOADER_ENTRY
*SubEntry
;
752 SubScreen
= InitializeSubScreen(Entry
);
754 // Set defaults for the new entry; will be modified based on lines read from the config. file....
755 SubEntry
= InitializeLoaderEntry(Entry
);
757 if ((SubEntry
== NULL
) || (SubScreen
== NULL
))
759 SubEntry
->me
.Title
= StrDuplicate(Title
);
761 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
763 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
764 MyFreePool(SubEntry
->LoaderPath
);
765 SubEntry
->LoaderPath
= StrDuplicate(TokenList
[1]);
766 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
768 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
769 if (FindVolume(&Volume
, TokenList
[1])) {
770 MyFreePool(SubEntry
->me
.Title
);
771 SubEntry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
772 SPrint(SubEntry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", Volume
->VolName
);
773 SubEntry
->me
.BadgeImage
= Volume
->VolBadgeImage
;
774 SubEntry
->VolName
= Volume
->VolName
;
777 } else if (StriCmp(TokenList
[0], L
"initrd") == 0) {
778 MyFreePool(SubEntry
->InitrdPath
);
779 SubEntry
->InitrdPath
= NULL
;
780 if (TokenCount
> 1) {
781 SubEntry
->InitrdPath
= StrDuplicate(TokenList
[1]);
784 } else if (StriCmp(TokenList
[0], L
"options") == 0) {
785 MyFreePool(SubEntry
->LoadOptions
);
786 SubEntry
->LoadOptions
= NULL
;
787 if (TokenCount
> 1) {
788 SubEntry
->LoadOptions
= StrDuplicate(TokenList
[1]);
791 } else if ((StriCmp(TokenList
[0], L
"add_options") == 0) && (TokenCount
> 1)) {
792 MergeStrings(&SubEntry
->LoadOptions
, TokenList
[1], L
' ');
794 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
795 SubEntry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
797 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
798 SubEntry
->Enabled
= FALSE
;
801 FreeTokenLine(&TokenList
, &TokenCount
);
804 if (SubEntry
->InitrdPath
!= NULL
) {
805 MergeStrings(&SubEntry
->LoadOptions
, L
"initrd=", L
' ');
806 MergeStrings(&SubEntry
->LoadOptions
, SubEntry
->InitrdPath
, 0);
807 MyFreePool(SubEntry
->InitrdPath
);
808 SubEntry
->InitrdPath
= NULL
;
810 if (SubEntry
->Enabled
== TRUE
) {
811 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
813 Entry
->me
.SubScreen
= SubScreen
;
814 } // VOID AddSubmenu()
816 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
817 // that entry. The calling function is then responsible for adding the entry to the
819 static LOADER_ENTRY
* AddStanzaEntries(REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
823 BOOLEAN DefaultsSet
= FALSE
, AddedSubmenu
= FALSE
;
824 REFIT_VOLUME
*CurrentVolume
= Volume
;
826 // prepare the menu entry
827 Entry
= InitializeLoaderEntry(NULL
);
831 Entry
->Title
= StrDuplicate(Title
);
832 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
833 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
835 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
836 Entry
->VolName
= CurrentVolume
->VolName
;
838 // Parse the config file to add options for a single stanza, terminating when the token
839 // is "}" or when the end of file is reached.
840 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
841 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
842 Entry
->LoaderPath
= StrDuplicate(TokenList
[1]);
843 Entry
->DevicePath
= FileDevicePath(CurrentVolume
->DeviceHandle
, Entry
->LoaderPath
);
844 SetLoaderDefaults(Entry
, TokenList
[1], CurrentVolume
);
845 MyFreePool(Entry
->LoadOptions
);
846 Entry
->LoadOptions
= NULL
; // Discard default options, if any
849 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
850 if (FindVolume(&CurrentVolume
, TokenList
[1])) {
851 MyFreePool(Entry
->me
.Title
);
852 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
853 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
854 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
855 Entry
->VolName
= CurrentVolume
->VolName
;
858 } else if ((StriCmp(TokenList
[0], L
"icon") == 0) && (TokenCount
> 1)) {
859 MyFreePool(Entry
->me
.Image
);
860 Entry
->me
.Image
= egLoadIcon(CurrentVolume
->RootDir
, TokenList
[1], GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
861 if (Entry
->me
.Image
== NULL
) {
862 Entry
->me
.Image
= DummyImage(GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
865 } else if ((StriCmp(TokenList
[0], L
"initrd") == 0) && (TokenCount
> 1)) {
866 MyFreePool(Entry
->InitrdPath
);
867 Entry
->InitrdPath
= StrDuplicate(TokenList
[1]);
869 } else if ((StriCmp(TokenList
[0], L
"options") == 0) && (TokenCount
> 1)) {
870 MyFreePool(Entry
->LoadOptions
);
871 Entry
->LoadOptions
= StrDuplicate(TokenList
[1]);
873 } else if ((StriCmp(TokenList
[0], L
"ostype") == 0) && (TokenCount
> 1)) {
874 if (TokenCount
> 1) {
875 Entry
->OSType
= TokenList
[1][0];
878 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
879 Entry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
881 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
882 Entry
->Enabled
= FALSE
;
884 } else if ((StriCmp(TokenList
[0], L
"submenuentry") == 0) && (TokenCount
> 1)) {
885 AddSubmenu(Entry
, File
, CurrentVolume
, TokenList
[1]);
888 } // set options to pass to the loader program
889 FreeTokenLine(&TokenList
, &TokenCount
);
893 AddMenuEntry(Entry
->me
.SubScreen
, &MenuEntryReturn
);
895 if (Entry
->InitrdPath
) {
896 MergeStrings(&Entry
->LoadOptions
, L
"initrd=", L
' ');
897 MergeStrings(&Entry
->LoadOptions
, Entry
->InitrdPath
, 0);
898 MyFreePool(Entry
->InitrdPath
);
899 Entry
->InitrdPath
= NULL
;
903 SetLoaderDefaults(Entry
, L
"\\EFI\\BOOT\\nemo.efi", CurrentVolume
); // user included no "loader" line; use bogus one
906 } // static VOID AddStanzaEntries()
908 // Read the user-configured menu entries from refind.conf and add or delete
909 // entries based on the contents of that file....
910 VOID
ScanUserConfigured(CHAR16
*FileName
)
914 REFIT_VOLUME
*Volume
;
916 CHAR16
*Title
= NULL
;
917 UINTN TokenCount
, size
;
920 if (FileExists(SelfDir
, FileName
)) {
921 Status
= ReadFile(SelfDir
, FileName
, &File
, &size
);
922 if (EFI_ERROR(Status
))
927 while ((TokenCount
= ReadTokenLine(&File
, &TokenList
)) > 0) {
928 if ((StriCmp(TokenList
[0], L
"menuentry") == 0) && (TokenCount
> 1)) {
929 Title
= StrDuplicate(TokenList
[1]);
930 Entry
= AddStanzaEntries(&File
, Volume
, TokenList
[1]);
931 if (Entry
->Enabled
) {
932 if (Entry
->me
.SubScreen
== NULL
)
933 GenerateSubScreen(Entry
, Volume
);
934 AddPreparedLoaderEntry(Entry
);
940 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
941 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
942 if (StriCmp(TokenList
[1], FileName
) != 0) {
943 ScanUserConfigured(TokenList
[1]);
947 FreeTokenLine(&TokenList
, &TokenCount
);
950 } // VOID ScanUserConfigured()
952 // Create an options file based on /etc/fstab. The resulting file has two options
953 // lines, one of which boots the system with "ro root={rootfs}" and the other of
954 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
955 // filesystem identifier associated with the "/" line in /etc/fstab.
956 static REFIT_FILE
* GenerateOptionsFromEtcFstab(REFIT_VOLUME
*Volume
) {
958 REFIT_FILE
*Options
= NULL
, *Fstab
= NULL
;
960 CHAR16
**TokenList
, *Line
, Root
[100];
962 if (FileExists(Volume
->RootDir
, L
"\\etc\\fstab")) {
963 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
964 Fstab
= AllocateZeroPool(sizeof(REFIT_FILE
));
965 Status
= ReadFile(Volume
->RootDir
, L
"\\etc\\fstab", Fstab
, &i
);
966 if (CheckError(Status
, L
"while reading /etc/fstab")) {
973 } else { // File read; locate root fs and create entries
974 Options
->Encoding
= ENCODING_UTF16_LE
;
975 while ((TokenCount
= ReadTokenLine(Fstab
, &TokenList
)) > 0) {
976 if (TokenCount
> 2) {
978 if (StriCmp(TokenList
[1], L
"\\") == 0) {
979 SPrint(Root
, 99, L
"%s", TokenList
[0]);
980 } else if (StriCmp(TokenList
[2], L
"\\") == 0) {
981 SPrint(Root
, 99, L
"%s=%s", TokenList
[0], TokenList
[1]);
982 } // if/elseif/elseif
983 if (Root
[0] != L
'\0') {
984 for (i
= 0; i
< StrLen(Root
); i
++)
987 Line
= PoolPrint(L
"\"Boot with normal options\" \"ro root=%s\"\n", Root
);
988 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
990 Line
= PoolPrint(L
"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root
);
991 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
992 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
995 FreeTokenLine(&TokenList
, &TokenCount
);
998 Options
->Current8Ptr
= (CHAR8
*)Options
->Buffer
;
999 Options
->End8Ptr
= Options
->Current8Ptr
+ Options
->BufferSize
;
1000 Options
->Current16Ptr
= (CHAR16
*)Options
->Buffer
;
1001 Options
->End16Ptr
= Options
->Current16Ptr
+ (Options
->BufferSize
>> 1);
1003 MyFreePool(Fstab
->Buffer
);
1005 } // if/else file read error
1006 } // if /etc/fstab exists
1008 } // GenerateOptionsFromEtcFstab()
1011 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1012 // and Volume variables identify the location of the options file, but not its name --
1013 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1014 // file in the target directory, and this function finds the file with a name in the
1015 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1016 // directory and loads it. This function tries multiple files because I originally
1017 // used the filename linux.conf, but close on the heels of that decision, the Linux
1018 // kernel developers decided to use that name for a similar purpose, but with a
1019 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1020 // but I want a migration period in which both names are used.
1021 // If a rEFInd options file can't be found, try to generate minimal options from
1022 // /etc/fstab on the same volume as the kernel. This typically works only if the
1023 // kernel is being read from the Linux root filesystem.
1025 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1027 REFIT_FILE
* ReadLinuxOptionsFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1028 CHAR16
*OptionsFilename
, *FullFilename
;
1029 BOOLEAN GoOn
= TRUE
, FileFound
= FALSE
;
1031 REFIT_FILE
*File
= NULL
;
1035 OptionsFilename
= FindCommaDelimited(LINUX_OPTIONS_FILENAMES
, i
++);
1036 FullFilename
= FindPath(LoaderPath
);
1037 if ((OptionsFilename
!= NULL
) && (FullFilename
!= NULL
)) {
1038 MergeStrings(&FullFilename
, OptionsFilename
, '\\');
1039 if (FileExists(Volume
->RootDir
, FullFilename
)) {
1040 File
= AllocateZeroPool(sizeof(REFIT_FILE
));
1041 Status
= ReadFile(Volume
->RootDir
, FullFilename
, File
, &size
);
1042 if (CheckError(Status
, L
"while loading the Linux options file")) {
1051 } else { // a filename string is NULL
1055 File
= GenerateOptionsFromEtcFstab(Volume
);
1056 MyFreePool(OptionsFilename
);
1057 MyFreePool(FullFilename
);
1058 OptionsFilename
= FullFilename
= NULL
;
1061 } // static REFIT_FILE * ReadLinuxOptionsFile()
1063 // Retrieve a single line of options from a Linux kernel options file
1064 CHAR16
* GetFirstOptionsFromFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1066 CHAR16
*Options
= NULL
;
1070 File
= ReadLinuxOptionsFile(LoaderPath
, Volume
);
1072 TokenCount
= ReadTokenLine(File
, &TokenList
);
1074 Options
= StrDuplicate(TokenList
[1]);
1075 FreeTokenLine(&TokenList
, &TokenCount
);
1079 } // static CHAR16 * GetOptionsFile()