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) {
324 *Target
= StrDuplicate(TokenList
[1]);
326 } // static VOID HandleString()
328 // Handle a parameter with a series of string arguments, to replace or be added to a
329 // comma-delimited list. Passes each token through the CleanUpPathNameSlashes() function
330 // to ensure consistency in subsequent comparisons of filenames. If the first
331 // non-keyword token is "+", the list is added to the existing target string; otherwise,
332 // the tokens replace the current string.
333 static VOID
HandleStrings(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
335 BOOLEAN AddMode
= FALSE
;
337 if ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") == 0)) {
341 if ((*Target
!= NULL
) && !AddMode
) {
345 for (i
= 1; i
< TokenCount
; i
++) {
346 if ((i
!= 1) || !AddMode
) {
347 CleanUpPathNameSlashes(TokenList
[i
]);
348 MergeStrings(Target
, TokenList
[i
], L
',');
351 } // static VOID HandleStrings()
353 // Convert TimeString (in "HH:MM" format) to a pure-minute format. Values should be
354 // in the range from 0 (for 00:00, or midnight) to 1439 (for 23:59; aka LAST_MINUTE).
355 // Any value outside that range denotes an error in the specification. Note that if
356 // the input is a number that includes no colon, this function will return the original
357 // number in UINTN form.
358 static UINTN
HandleTime(IN CHAR16
*TimeString
) {
359 UINTN Hour
= 0, Minute
= 0, TimeLength
, i
= 0;
361 TimeLength
= StrLen(TimeString
);
362 while (i
< TimeLength
) {
363 if (TimeString
[i
] == L
':') {
367 if ((TimeString
[i
] >= L
'0') && (TimeString
[i
] <= '9')) {
369 Minute
+= (TimeString
[i
] - L
'0');
373 return (Hour
* 60 + Minute
);
374 } // BOOLEAN HandleTime()
376 static BOOLEAN
HandleBoolean(IN CHAR16
**TokenList
, IN UINTN TokenCount
) {
377 BOOLEAN TruthValue
= TRUE
;
379 if ((TokenCount
>= 2) && ((StriCmp(TokenList
[1], L
"0") == 0) ||
380 (StriCmp(TokenList
[1], L
"false") == 0) ||
381 (StriCmp(TokenList
[1], L
"off") == 0))) {
386 } // BOOLEAN HandleBoolean
388 // Sets the default boot loader IF the current time is within the bounds
389 // defined by the third and fourth tokens in the TokenList.
390 static VOID
SetDefaultByTime(IN CHAR16
**TokenList
, OUT CHAR16
**Default
) {
392 EFI_TIME CurrentTime
;
393 UINTN StartTime
= LAST_MINUTE
+ 1, EndTime
= LAST_MINUTE
+ 1, Now
;
394 BOOLEAN SetIt
= FALSE
;
396 StartTime
= HandleTime(TokenList
[2]);
397 EndTime
= HandleTime(TokenList
[3]);
399 if ((StartTime
<= LAST_MINUTE
) && (EndTime
<= LAST_MINUTE
)) {
400 Status
= refit_call2_wrapper(GetTime
, &CurrentTime
, NULL
);
401 if (Status
!= EFI_SUCCESS
)
403 Now
= CurrentTime
.Hour
* 60 + CurrentTime
.Minute
;
405 if (Now
> LAST_MINUTE
) { // Shouldn't happen; just being paranoid
406 Print(L
"Warning: Impossible system time: %d:%d\n", CurrentTime
.Hour
, CurrentTime
.Minute
);
408 } // if impossible time
410 if (StartTime
< EndTime
) { // Time range does NOT cross midnight
411 if ((Now
>= StartTime
) && (Now
<= EndTime
))
413 } else { // Time range DOES cross midnight
414 if ((Now
>= StartTime
) && (Now
<= EndTime
))
416 } // if/else time range crosses midnight
419 MyFreePool(*Default
);
420 *Default
= StrDuplicate(TokenList
[1]);
422 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
423 } // VOID SetDefaultByTime()
426 VOID
ReadConfig(CHAR16
*FileName
)
432 CHAR16
*TempStr
= NULL
;
434 EFI_GUID RefindGuid
= REFIND_GUID_VALUE
;
436 // Set a few defaults only if we're loading the default file.
437 if (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0) {
438 MyFreePool(GlobalConfig
.AlsoScan
);
439 GlobalConfig
.AlsoScan
= StrDuplicate(ALSO_SCAN_DIRS
);
440 MyFreePool(GlobalConfig
.DontScanDirs
);
442 if (SelfVolume
->VolName
) {
443 TempStr
= SelfVolume
->VolName
? StrDuplicate(SelfVolume
->VolName
) : NULL
;
445 TempStr
= AllocateZeroPool(256 * sizeof(CHAR16
));
447 SPrint(TempStr
, 255, L
"fs%d", SelfVolume
->VolNumber
);
450 MergeStrings(&TempStr
, SelfDirPath
, L
':');
451 MergeStrings(&TempStr
, MEMTEST_LOCATIONS
, L
',');
452 GlobalConfig
.DontScanDirs
= TempStr
;
453 MyFreePool(GlobalConfig
.DontScanFiles
);
454 GlobalConfig
.DontScanFiles
= StrDuplicate(DONT_SCAN_FILES
);
455 MergeStrings(&(GlobalConfig
.DontScanFiles
), MOK_NAMES
, L
',');
456 MyFreePool(GlobalConfig
.DontScanVolumes
);
457 GlobalConfig
.DontScanVolumes
= StrDuplicate(DONT_SCAN_VOLUMES
);
458 GlobalConfig
.WindowsRecoveryFiles
= StrDuplicate(WINDOWS_RECOVERY_FILES
);
459 if (GlobalConfig
.DefaultSelection
!= NULL
) {
460 MyFreePool(GlobalConfig
.DefaultSelection
);
461 GlobalConfig
.DefaultSelection
= NULL
;
463 // GlobalConfig.DefaultSelection = AllocatePool(255 * sizeof(CHAR16));
464 // Print(L"About to call EfivarGetRaw()\n");
465 Status
= EfivarGetRaw(&RefindGuid
, L
"PreviousBoot", (CHAR8
**) &(GlobalConfig
.DefaultSelection
), &i
);
466 // i = 255 * sizeof(CHAR16);
467 // Print(L"About to call RT->GetVariable()\n");
468 // Status = refit_call5_wrapper(RT->GetVariable, L"PreviousBoot", &RefindGuid, &Attributes, &i, GlobalConfig.DefaultSelection);
469 if (Status
!= EFI_SUCCESS
)
470 GlobalConfig
.DefaultSelection
= NULL
;
473 if (!FileExists(SelfDir
, FileName
)) {
474 Print(L
"Configuration file '%s' missing!\n", FileName
);
478 Status
= ReadFile(SelfDir
, FileName
, &File
, &i
);
479 if (EFI_ERROR(Status
))
483 TokenCount
= ReadTokenLine(&File
, &TokenList
);
487 if (StriCmp(TokenList
[0], L
"timeout") == 0) {
488 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.Timeout
));
490 } else if (StriCmp(TokenList
[0], L
"hideui") == 0) {
491 for (i
= 1; i
< TokenCount
; i
++) {
492 FlagName
= TokenList
[i
];
493 if (StriCmp(FlagName
, L
"banner") == 0) {
494 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_BANNER
;
495 } else if (StriCmp(FlagName
, L
"label") == 0) {
496 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_LABEL
;
497 } else if (StriCmp(FlagName
, L
"singleuser") == 0) {
498 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SINGLEUSER
;
499 } else if (StriCmp(FlagName
, L
"hwtest") == 0) {
500 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HWTEST
;
501 } else if (StriCmp(FlagName
, L
"arrows") == 0) {
502 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_ARROWS
;
503 } else if (StriCmp(FlagName
, L
"hints") == 0) {
504 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HINTS
;
505 } else if (StriCmp(FlagName
, L
"editor") == 0) {
506 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_EDITOR
;
507 } else if (StriCmp(FlagName
, L
"safemode") == 0) {
508 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SAFEMODE
;
509 } else if (StriCmp(FlagName
, L
"all") == 0) {
510 GlobalConfig
.HideUIFlags
= HIDEUI_FLAG_ALL
;
512 Print(L
" unknown hideui flag: '%s'\n", FlagName
);
516 } else if (StriCmp(TokenList
[0], L
"icons_dir") == 0) {
517 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.IconsDir
));
519 } else if (StriCmp(TokenList
[0], L
"scanfor") == 0) {
520 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
522 GlobalConfig
.ScanFor
[i
] = TokenList
[i
][0];
524 GlobalConfig
.ScanFor
[i
] = ' ';
527 } else if (StriCmp(TokenList
[0], L
"uefi_deep_legacy_scan") == 0) {
528 GlobalConfig
.DeepLegacyScan
= HandleBoolean(TokenList
, TokenCount
);
530 } else if ((StriCmp(TokenList
[0], L
"scan_delay") == 0) && (TokenCount
== 2)) {
531 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScanDelay
));
533 } else if (StriCmp(TokenList
[0], L
"also_scan_dirs") == 0) {
534 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.AlsoScan
));
536 } else if ((StriCmp(TokenList
[0], L
"don't_scan_volumes") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_volumes") == 0)) {
537 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
538 MyFreePool(GlobalConfig
.DontScanVolumes
);
539 GlobalConfig
.DontScanVolumes
= NULL
;
540 for (i
= 1; i
< TokenCount
; i
++) {
541 MergeStrings(&GlobalConfig
.DontScanVolumes
, TokenList
[i
], L
',');
544 } else if ((StriCmp(TokenList
[0], L
"don't_scan_dirs") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_dirs") == 0)) {
545 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanDirs
));
547 } else if ((StriCmp(TokenList
[0], L
"don't_scan_files") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_files") == 0)) {
548 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanFiles
));
550 } else if (StriCmp(TokenList
[0], L
"windows_recovery_files") == 0) {
551 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.WindowsRecoveryFiles
));
553 } else if (StriCmp(TokenList
[0], L
"scan_driver_dirs") == 0) {
554 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DriverDirs
));
556 } else if (StriCmp(TokenList
[0], L
"showtools") == 0) {
557 SetMem(GlobalConfig
.ShowTools
, NUM_TOOLS
* sizeof(UINTN
), 0);
558 for (i
= 1; (i
< TokenCount
) && (i
< NUM_TOOLS
); i
++) {
559 FlagName
= TokenList
[i
];
560 if (StriCmp(FlagName
, L
"shell") == 0) {
561 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHELL
;
562 } else if (StriCmp(FlagName
, L
"gptsync") == 0) {
563 GlobalConfig
.ShowTools
[i
- 1] = TAG_GPTSYNC
;
564 } else if (StriCmp(FlagName
, L
"gdisk") == 0) {
565 GlobalConfig
.ShowTools
[i
- 1] = TAG_GDISK
;
566 } else if (StriCmp(FlagName
, L
"about") == 0) {
567 GlobalConfig
.ShowTools
[i
- 1] = TAG_ABOUT
;
568 } else if (StriCmp(FlagName
, L
"exit") == 0) {
569 GlobalConfig
.ShowTools
[i
- 1] = TAG_EXIT
;
570 } else if (StriCmp(FlagName
, L
"reboot") == 0) {
571 GlobalConfig
.ShowTools
[i
- 1] = TAG_REBOOT
;
572 } else if (StriCmp(FlagName
, L
"shutdown") == 0) {
573 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHUTDOWN
;
574 } else if (StriCmp(FlagName
, L
"apple_recovery") == 0) {
575 GlobalConfig
.ShowTools
[i
- 1] = TAG_APPLE_RECOVERY
;
576 } else if (StriCmp(FlagName
, L
"windows_recovery") == 0) {
577 GlobalConfig
.ShowTools
[i
- 1] = TAG_WINDOWS_RECOVERY
;
578 } else if (StriCmp(FlagName
, L
"mok_tool") == 0) {
579 GlobalConfig
.ShowTools
[i
- 1] = TAG_MOK_TOOL
;
580 } else if (StriCmp(FlagName
, L
"firmware") == 0) {
581 GlobalConfig
.ShowTools
[i
- 1] = TAG_FIRMWARE
;
582 } else if ((StriCmp(FlagName
, L
"memtest86") == 0) || (StriCmp(FlagName
, L
"memtest") == 0)) {
583 GlobalConfig
.ShowTools
[i
- 1] = TAG_MEMTEST
;
585 Print(L
" unknown showtools flag: '%s'\n", FlagName
);
587 } // showtools options
589 } else if (StriCmp(TokenList
[0], L
"banner") == 0) {
590 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.BannerFileName
));
592 } else if ((StriCmp(TokenList
[0], L
"banner_scale") == 0) && (TokenCount
== 2)) {
593 if (StriCmp(TokenList
[1], L
"noscale") == 0) {
594 GlobalConfig
.BannerScale
= BANNER_NOSCALE
;
595 } else if ((StriCmp(TokenList
[1], L
"fillscreen") == 0) || (StriCmp(TokenList
[1], L
"fullscreen") == 0)) {
596 GlobalConfig
.BannerScale
= BANNER_FILLSCREEN
;
598 Print(L
" unknown banner_type flag: '%s'\n", TokenList
[1]);
601 } else if ((StriCmp(TokenList
[0], L
"small_icon_size") == 0) && (TokenCount
== 2)) {
602 HandleInt(TokenList
, TokenCount
, &i
);
604 GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] = i
;
606 } else if ((StriCmp(TokenList
[0], L
"big_icon_size") == 0) && (TokenCount
== 2)) {
607 HandleInt(TokenList
, TokenCount
, &i
);
609 GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] = i
;
610 GlobalConfig
.IconSizes
[ICON_SIZE_BADGE
] = i
/ 4;
613 } else if (StriCmp(TokenList
[0], L
"selection_small") == 0) {
614 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionSmallFileName
));
616 } else if (StriCmp(TokenList
[0], L
"selection_big") == 0) {
617 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionBigFileName
));
619 } else if (StriCmp(TokenList
[0], L
"default_selection") == 0) {
620 if (TokenCount
== 4) {
621 SetDefaultByTime(TokenList
, &(GlobalConfig
.DefaultSelection
));
623 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.DefaultSelection
));
626 } else if (StriCmp(TokenList
[0], L
"textonly") == 0) {
627 GlobalConfig
.TextOnly
= HandleBoolean(TokenList
, TokenCount
);
628 // if ((TokenCount >= 2) && (StriCmp(TokenList[1], L"0") == 0)) {
629 // GlobalConfig.TextOnly = FALSE;
631 // GlobalConfig.TextOnly = TRUE;
634 } else if (StriCmp(TokenList
[0], L
"textmode") == 0) {
635 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.RequestedTextMode
));
637 } else if ((StriCmp(TokenList
[0], L
"resolution") == 0) && ((TokenCount
== 2) || (TokenCount
== 3))) {
638 GlobalConfig
.RequestedScreenWidth
= Atoi(TokenList
[1]);
640 GlobalConfig
.RequestedScreenHeight
= Atoi(TokenList
[2]);
642 GlobalConfig
.RequestedScreenHeight
= 0;
644 } else if (StriCmp(TokenList
[0], L
"screensaver") == 0) {
645 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScreensaverTime
));
647 } else if (StriCmp(TokenList
[0], L
"use_graphics_for") == 0) {
648 if ((TokenCount
== 2) || ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") != 0)))
649 GlobalConfig
.GraphicsFor
= 0;
650 for (i
= 1; i
< TokenCount
; i
++) {
651 if (StriCmp(TokenList
[i
], L
"osx") == 0) {
652 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_OSX
;
653 } else if (StriCmp(TokenList
[i
], L
"linux") == 0) {
654 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_LINUX
;
655 } else if (StriCmp(TokenList
[i
], L
"elilo") == 0) {
656 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_ELILO
;
657 } else if (StriCmp(TokenList
[i
], L
"grub") == 0) {
658 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_GRUB
;
659 } else if (StriCmp(TokenList
[i
], L
"windows") == 0) {
660 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_WINDOWS
;
662 } // for (graphics_on tokens)
664 } else if ((StriCmp(TokenList
[0], L
"font") == 0) && (TokenCount
== 2)) {
665 egLoadFont(TokenList
[1]);
667 } else if (StriCmp(TokenList
[0], L
"scan_all_linux_kernels") == 0) {
668 GlobalConfig
.ScanAllLinux
= HandleBoolean(TokenList
, TokenCount
);
669 // if ((TokenCount >= 2) && (StriCmp(TokenList[1], L"0") == 0)) {
670 // GlobalConfig.ScanAllLinux = FALSE;
672 // GlobalConfig.ScanAllLinux = TRUE;
675 } else if (StriCmp(TokenList
[0], L
"max_tags") == 0) {
676 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.MaxTags
));
678 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
679 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
680 if (StriCmp(TokenList
[1], FileName
) != 0) {
681 ReadConfig(TokenList
[1]);
686 FreeTokenLine(&TokenList
, &TokenCount
);
688 if ((GlobalConfig
.DontScanFiles
) && (GlobalConfig
.WindowsRecoveryFiles
))
689 MergeStrings(&(GlobalConfig
.DontScanFiles
), GlobalConfig
.WindowsRecoveryFiles
, L
',');
690 MyFreePool(File
.Buffer
);
691 } /* VOID ReadConfig() */
693 // Finds a volume with the specified Identifier (a filesystem label, a
694 // partition name, a partition GUID, or a number followed by a colon). If
695 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
696 // Returns TRUE if a match was found, FALSE if not.
697 static BOOLEAN
FindVolume(REFIT_VOLUME
**Volume
, CHAR16
*Identifier
) {
698 UINTN i
= 0, CountedVolumes
= 0, Length
;
700 BOOLEAN Found
= FALSE
, IdIsGuid
= FALSE
;
701 EFI_GUID VolGuid
, NullGuid
= NULL_GUID_VALUE
;
703 VolGuid
= StringAsGuid(Identifier
);
704 Length
= StrLen(Identifier
);
705 if ((Length
>= 2) && (Identifier
[Length
- 1] == L
':') &&
706 (Identifier
[0] >= L
'0') && (Identifier
[0] <= L
'9')) {
707 Number
= (INTN
) Atoi(Identifier
);
708 } else if (IsGuid(Identifier
)) {
711 while ((i
< VolumesCount
) && (!Found
)) {
712 if (Number
>= 0) { // User specified a volume by number
713 if (Volumes
[i
]->IsReadable
) {
714 if (CountedVolumes
== Number
) {
715 *Volume
= Volumes
[i
];
720 } else { // User specified a volume by label or GUID
721 if ((StriCmp(Identifier
, Volumes
[i
]->VolName
) == 0) ||
722 (StriCmp(Identifier
, Volumes
[i
]->PartName
) == 0)) {
723 *Volume
= Volumes
[i
];
726 if (IdIsGuid
&& !Found
) {
727 if (GuidsAreEqual(&VolGuid
, &(Volumes
[i
]->PartGuid
)) && !GuidsAreEqual(&NullGuid
, &(Volumes
[i
]->PartGuid
))) {
728 *Volume
= Volumes
[i
];
736 } // static VOID FindVolume()
738 static VOID
AddSubmenu(LOADER_ENTRY
*Entry
, REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
739 REFIT_MENU_SCREEN
*SubScreen
;
740 LOADER_ENTRY
*SubEntry
;
744 SubScreen
= InitializeSubScreen(Entry
);
746 // Set defaults for the new entry; will be modified based on lines read from the config. file....
747 SubEntry
= InitializeLoaderEntry(Entry
);
749 if ((SubEntry
== NULL
) || (SubScreen
== NULL
))
751 SubEntry
->me
.Title
= StrDuplicate(Title
);
753 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
755 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
756 MyFreePool(SubEntry
->LoaderPath
);
757 SubEntry
->LoaderPath
= StrDuplicate(TokenList
[1]);
758 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
760 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
761 if (FindVolume(&Volume
, TokenList
[1])) {
762 MyFreePool(SubEntry
->me
.Title
);
763 SubEntry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
764 SPrint(SubEntry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", Volume
->VolName
);
765 SubEntry
->me
.BadgeImage
= Volume
->VolBadgeImage
;
766 SubEntry
->VolName
= Volume
->VolName
;
769 } else if (StriCmp(TokenList
[0], L
"initrd") == 0) {
770 MyFreePool(SubEntry
->InitrdPath
);
771 SubEntry
->InitrdPath
= NULL
;
772 if (TokenCount
> 1) {
773 SubEntry
->InitrdPath
= StrDuplicate(TokenList
[1]);
776 } else if (StriCmp(TokenList
[0], L
"options") == 0) {
777 MyFreePool(SubEntry
->LoadOptions
);
778 SubEntry
->LoadOptions
= NULL
;
779 if (TokenCount
> 1) {
780 SubEntry
->LoadOptions
= StrDuplicate(TokenList
[1]);
783 } else if ((StriCmp(TokenList
[0], L
"add_options") == 0) && (TokenCount
> 1)) {
784 MergeStrings(&SubEntry
->LoadOptions
, TokenList
[1], L
' ');
786 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
787 SubEntry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
789 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
790 SubEntry
->Enabled
= FALSE
;
793 FreeTokenLine(&TokenList
, &TokenCount
);
796 if (SubEntry
->InitrdPath
!= NULL
) {
797 MergeStrings(&SubEntry
->LoadOptions
, L
"initrd=", L
' ');
798 MergeStrings(&SubEntry
->LoadOptions
, SubEntry
->InitrdPath
, 0);
799 MyFreePool(SubEntry
->InitrdPath
);
800 SubEntry
->InitrdPath
= NULL
;
802 if (SubEntry
->Enabled
== TRUE
) {
803 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
805 Entry
->me
.SubScreen
= SubScreen
;
806 } // VOID AddSubmenu()
808 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
809 // that entry. The calling function is then responsible for adding the entry to the
811 static LOADER_ENTRY
* AddStanzaEntries(REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
815 BOOLEAN DefaultsSet
= FALSE
, AddedSubmenu
= FALSE
;
816 REFIT_VOLUME
*CurrentVolume
= Volume
;
818 // prepare the menu entry
819 Entry
= InitializeLoaderEntry(NULL
);
823 Entry
->Title
= StrDuplicate(Title
);
824 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
825 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
827 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
828 Entry
->VolName
= CurrentVolume
->VolName
;
830 // Parse the config file to add options for a single stanza, terminating when the token
831 // is "}" or when the end of file is reached.
832 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
833 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
834 Entry
->LoaderPath
= StrDuplicate(TokenList
[1]);
835 Entry
->DevicePath
= FileDevicePath(CurrentVolume
->DeviceHandle
, Entry
->LoaderPath
);
836 SetLoaderDefaults(Entry
, TokenList
[1], CurrentVolume
);
837 MyFreePool(Entry
->LoadOptions
);
838 Entry
->LoadOptions
= NULL
; // Discard default options, if any
841 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
842 if (FindVolume(&CurrentVolume
, TokenList
[1])) {
843 MyFreePool(Entry
->me
.Title
);
844 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
845 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
846 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
847 Entry
->VolName
= CurrentVolume
->VolName
;
850 } else if ((StriCmp(TokenList
[0], L
"icon") == 0) && (TokenCount
> 1)) {
851 MyFreePool(Entry
->me
.Image
);
852 Entry
->me
.Image
= egLoadIcon(CurrentVolume
->RootDir
, TokenList
[1], GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
853 if (Entry
->me
.Image
== NULL
) {
854 Entry
->me
.Image
= DummyImage(GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
857 } else if ((StriCmp(TokenList
[0], L
"initrd") == 0) && (TokenCount
> 1)) {
858 MyFreePool(Entry
->InitrdPath
);
859 Entry
->InitrdPath
= StrDuplicate(TokenList
[1]);
861 } else if ((StriCmp(TokenList
[0], L
"options") == 0) && (TokenCount
> 1)) {
862 MyFreePool(Entry
->LoadOptions
);
863 Entry
->LoadOptions
= StrDuplicate(TokenList
[1]);
865 } else if ((StriCmp(TokenList
[0], L
"ostype") == 0) && (TokenCount
> 1)) {
866 if (TokenCount
> 1) {
867 Entry
->OSType
= TokenList
[1][0];
870 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
871 Entry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
873 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
874 Entry
->Enabled
= FALSE
;
876 } else if ((StriCmp(TokenList
[0], L
"submenuentry") == 0) && (TokenCount
> 1)) {
877 AddSubmenu(Entry
, File
, CurrentVolume
, TokenList
[1]);
880 } // set options to pass to the loader program
881 FreeTokenLine(&TokenList
, &TokenCount
);
885 AddMenuEntry(Entry
->me
.SubScreen
, &MenuEntryReturn
);
887 if (Entry
->InitrdPath
) {
888 MergeStrings(&Entry
->LoadOptions
, L
"initrd=", L
' ');
889 MergeStrings(&Entry
->LoadOptions
, Entry
->InitrdPath
, 0);
890 MyFreePool(Entry
->InitrdPath
);
891 Entry
->InitrdPath
= NULL
;
895 SetLoaderDefaults(Entry
, L
"\\EFI\\BOOT\\nemo.efi", CurrentVolume
); // user included no "loader" line; use bogus one
898 } // static VOID AddStanzaEntries()
900 // Read the user-configured menu entries from refind.conf and add or delete
901 // entries based on the contents of that file....
902 VOID
ScanUserConfigured(CHAR16
*FileName
)
906 REFIT_VOLUME
*Volume
;
908 CHAR16
*Title
= NULL
;
909 UINTN TokenCount
, size
;
912 if (FileExists(SelfDir
, FileName
)) {
913 Status
= ReadFile(SelfDir
, FileName
, &File
, &size
);
914 if (EFI_ERROR(Status
))
919 while ((TokenCount
= ReadTokenLine(&File
, &TokenList
)) > 0) {
920 if ((StriCmp(TokenList
[0], L
"menuentry") == 0) && (TokenCount
> 1)) {
921 Title
= StrDuplicate(TokenList
[1]);
922 Entry
= AddStanzaEntries(&File
, Volume
, TokenList
[1]);
923 if (Entry
->Enabled
) {
924 if (Entry
->me
.SubScreen
== NULL
)
925 GenerateSubScreen(Entry
, Volume
);
926 AddPreparedLoaderEntry(Entry
);
932 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
933 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
934 if (StriCmp(TokenList
[1], FileName
) != 0) {
935 ScanUserConfigured(TokenList
[1]);
939 FreeTokenLine(&TokenList
, &TokenCount
);
942 } // VOID ScanUserConfigured()
944 // Create an options file based on /etc/fstab. The resulting file has two options
945 // lines, one of which boots the system with "ro root={rootfs}" and the other of
946 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
947 // filesystem identifier associated with the "/" line in /etc/fstab.
948 static REFIT_FILE
* GenerateOptionsFromEtcFstab(REFIT_VOLUME
*Volume
) {
950 REFIT_FILE
*Options
= NULL
, *Fstab
= NULL
;
952 CHAR16
**TokenList
, *Line
, Root
[100];
954 if (FileExists(Volume
->RootDir
, L
"\\etc\\fstab")) {
955 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
956 Fstab
= AllocateZeroPool(sizeof(REFIT_FILE
));
957 Status
= ReadFile(Volume
->RootDir
, L
"\\etc\\fstab", Fstab
, &i
);
958 if (CheckError(Status
, L
"while reading /etc/fstab")) {
965 } else { // File read; locate root fs and create entries
966 Options
->Encoding
= ENCODING_UTF16_LE
;
967 while ((TokenCount
= ReadTokenLine(Fstab
, &TokenList
)) > 0) {
968 if (TokenCount
> 2) {
970 if (StriCmp(TokenList
[1], L
"\\") == 0) {
971 SPrint(Root
, 99, L
"%s", TokenList
[0]);
972 } else if (StriCmp(TokenList
[2], L
"\\") == 0) {
973 SPrint(Root
, 99, L
"%s=%s", TokenList
[0], TokenList
[1]);
974 } // if/elseif/elseif
975 if (Root
[0] != L
'\0') {
976 for (i
= 0; i
< StrLen(Root
); i
++)
979 Line
= PoolPrint(L
"\"Boot with normal options\" \"ro root=%s\"\n", Root
);
980 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
982 Line
= PoolPrint(L
"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root
);
983 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
984 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
987 FreeTokenLine(&TokenList
, &TokenCount
);
990 Options
->Current8Ptr
= (CHAR8
*)Options
->Buffer
;
991 Options
->End8Ptr
= Options
->Current8Ptr
+ Options
->BufferSize
;
992 Options
->Current16Ptr
= (CHAR16
*)Options
->Buffer
;
993 Options
->End16Ptr
= Options
->Current16Ptr
+ (Options
->BufferSize
>> 1);
995 MyFreePool(Fstab
->Buffer
);
997 } // if/else file read error
998 } // if /etc/fstab exists
1000 } // GenerateOptionsFromEtcFstab()
1003 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1004 // and Volume variables identify the location of the options file, but not its name --
1005 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1006 // file in the target directory, and this function finds the file with a name in the
1007 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1008 // directory and loads it. This function tries multiple files because I originally
1009 // used the filename linux.conf, but close on the heels of that decision, the Linux
1010 // kernel developers decided to use that name for a similar purpose, but with a
1011 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1012 // but I want a migration period in which both names are used.
1013 // If a rEFInd options file can't be found, try to generate minimal options from
1014 // /etc/fstab on the same volume as the kernel. This typically works only if the
1015 // kernel is being read from the Linux root filesystem.
1017 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1019 REFIT_FILE
* ReadLinuxOptionsFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1020 CHAR16
*OptionsFilename
, *FullFilename
;
1021 BOOLEAN GoOn
= TRUE
, FileFound
= FALSE
;
1023 REFIT_FILE
*File
= NULL
;
1027 OptionsFilename
= FindCommaDelimited(LINUX_OPTIONS_FILENAMES
, i
++);
1028 FullFilename
= FindPath(LoaderPath
);
1029 if ((OptionsFilename
!= NULL
) && (FullFilename
!= NULL
)) {
1030 MergeStrings(&FullFilename
, OptionsFilename
, '\\');
1031 if (FileExists(Volume
->RootDir
, FullFilename
)) {
1032 File
= AllocateZeroPool(sizeof(REFIT_FILE
));
1033 Status
= ReadFile(Volume
->RootDir
, FullFilename
, File
, &size
);
1034 if (CheckError(Status
, L
"while loading the Linux options file")) {
1043 } else { // a filename string is NULL
1047 File
= GenerateOptionsFromEtcFstab(Volume
);
1048 MyFreePool(OptionsFilename
);
1049 MyFreePool(FullFilename
);
1050 OptionsFilename
= FullFilename
= NULL
;
1053 } // static REFIT_FILE * ReadLinuxOptionsFile()
1055 // Retrieve a single line of options from a Linux kernel options file
1056 CHAR16
* GetFirstOptionsFromFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1058 CHAR16
*Options
= NULL
;
1062 File
= ReadLinuxOptionsFile(LoaderPath
, Volume
);
1064 TokenCount
= ReadTokenLine(File
, &TokenList
);
1066 Options
= StrDuplicate(TokenList
[1]);
1067 FreeTokenLine(&TokenList
, &TokenCount
);
1071 } // static CHAR16 * GetOptionsFile()