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), 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 extern REFIT_MENU_ENTRY MenuEntryReturn
;
68 //static REFIT_MENU_ENTRY MenuEntryReturn = { L"Return to Main Menu", TAG_RETURN, 0, 0, 0, NULL, NULL, NULL };
71 // read a file into a buffer
74 EFI_STATUS
ReadFile(IN EFI_FILE_HANDLE BaseDir
, IN CHAR16
*FileName
, IN OUT REFIT_FILE
*File
, OUT UINTN
*size
)
77 EFI_FILE_HANDLE FileHandle
;
78 EFI_FILE_INFO
*FileInfo
;
85 // read the file, allocating a buffer on the way
86 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
87 SPrint(Message
, 255, L
"while loading the file '%s'", FileName
);
88 if (CheckError(Status
, Message
))
91 FileInfo
= LibFileInfo(FileHandle
);
92 if (FileInfo
== NULL
) {
93 // TODO: print and register the error
94 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
95 return EFI_LOAD_ERROR
;
97 ReadSize
= FileInfo
->FileSize
;
100 File
->BufferSize
= (UINTN
)ReadSize
;
101 File
->Buffer
= AllocatePool(File
->BufferSize
);
102 if (File
->Buffer
== NULL
) {
104 return EFI_OUT_OF_RESOURCES
;
106 *size
= File
->BufferSize
;
108 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &File
->BufferSize
, File
->Buffer
);
109 if (CheckError(Status
, Message
)) {
110 MyFreePool(File
->Buffer
);
112 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
115 Status
= refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
118 File
->Current8Ptr
= (CHAR8
*)File
->Buffer
;
119 File
->End8Ptr
= File
->Current8Ptr
+ File
->BufferSize
;
120 File
->Current16Ptr
= (CHAR16
*)File
->Buffer
;
121 File
->End16Ptr
= File
->Current16Ptr
+ (File
->BufferSize
>> 1);
124 File
->Encoding
= ENCODING_ISO8859_1
; // default: 1:1 translation of CHAR8 to CHAR16
125 if (File
->BufferSize
>= 4) {
126 if (File
->Buffer
[0] == 0xFF && File
->Buffer
[1] == 0xFE) {
127 // BOM in UTF-16 little endian (or UTF-32 little endian)
128 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
129 File
->Current16Ptr
++;
130 } else if (File
->Buffer
[0] == 0xEF && File
->Buffer
[1] == 0xBB && File
->Buffer
[2] == 0xBF) {
132 File
->Encoding
= ENCODING_UTF8
; // translate from UTF-8 to UTF-16
133 File
->Current8Ptr
+= 3;
134 } else if (File
->Buffer
[1] == 0 && File
->Buffer
[3] == 0) {
135 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
137 // TODO: detect other encodings as they are implemented
144 // get a single line of text from a file
147 static CHAR16
*ReadLine(REFIT_FILE
*File
)
152 if (File
->Buffer
== NULL
)
155 if (File
->Encoding
== ENCODING_ISO8859_1
|| File
->Encoding
== ENCODING_UTF8
) {
157 CHAR8
*p
, *LineStart
, *LineEnd
;
159 p
= File
->Current8Ptr
;
160 if (p
>= File
->End8Ptr
)
164 for (; p
< File
->End8Ptr
; p
++)
165 if (*p
== 13 || *p
== 10)
168 for (; p
< File
->End8Ptr
; p
++)
169 if (*p
!= 13 && *p
!= 10)
171 File
->Current8Ptr
= p
;
173 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
174 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
179 if (File
->Encoding
== ENCODING_ISO8859_1
) {
180 for (p
= LineStart
; p
< LineEnd
; )
182 } else if (File
->Encoding
== ENCODING_UTF8
) {
183 // TODO: actually handle UTF-8
184 for (p
= LineStart
; p
< LineEnd
; )
189 } else if (File
->Encoding
== ENCODING_UTF16_LE
) {
191 CHAR16
*p
, *LineStart
, *LineEnd
;
193 p
= File
->Current16Ptr
;
194 if (p
>= File
->End16Ptr
)
198 for (; p
< File
->End16Ptr
; p
++)
199 if (*p
== 13 || *p
== 10)
202 for (; p
< File
->End16Ptr
; p
++)
203 if (*p
!= 13 && *p
!= 10)
205 File
->Current16Ptr
= p
;
207 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
208 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
212 for (p
= LineStart
, q
= Line
; p
< LineEnd
; )
217 return NULL
; // unsupported encoding
222 // Returns FALSE if *p points to the end of a token, TRUE otherwise.
223 // Also modifies *p **IF** the first and second characters are both
224 // quotes ('"'); it deletes one of them.
225 static BOOLEAN
KeepReading(IN OUT CHAR16
*p
, IN OUT BOOLEAN
*IsQuoted
) {
226 BOOLEAN MoreToRead
= FALSE
;
229 if ((p
== NULL
) || (IsQuoted
== NULL
))
235 if ((*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '#' && *p
!= ',') || *IsQuoted
) {
240 Temp
= StrDuplicate(&p
[1]);
247 *IsQuoted
= !(*IsQuoted
);
249 } // if/else second character is a quote
250 } // if first character is a quote
253 } // BOOLEAN KeepReading()
256 // get a line of tokens from a file
258 UINTN
ReadTokenLine(IN REFIT_FILE
*File
, OUT CHAR16
***TokenList
)
260 BOOLEAN LineFinished
, IsQuoted
= FALSE
;
261 CHAR16
*Line
, *Token
, *p
;
262 UINTN TokenCount
= 0;
266 while (TokenCount
== 0) {
267 Line
= ReadLine(File
);
272 LineFinished
= FALSE
;
273 while (!LineFinished
) {
274 // skip whitespace & find start of token
275 while ((*p
== ' ' || *p
== '\t' || *p
== '=' || *p
== ',') && !IsQuoted
)
277 if (*p
== 0 || *p
== '#')
281 IsQuoted
= !IsQuoted
;
287 while (KeepReading(p
, &IsQuoted
)) {
288 if ((*p
== L
'/') && !IsQuoted
) // Switch Unix-style to DOS-style directory separators
292 if (*p
== L
'\0' || *p
== L
'#')
296 AddListElement((VOID
***)TokenList
, &TokenCount
, (VOID
*)StrDuplicate(Token
));
302 } /* ReadTokenLine() */
304 VOID
FreeTokenLine(IN OUT CHAR16
***TokenList
, IN OUT UINTN
*TokenCount
)
306 // TODO: also free the items
307 FreeList((VOID
***)TokenList
, TokenCount
);
310 // handle a parameter with a single integer argument
311 static VOID
HandleInt(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT UINTN
*Value
)
313 if (TokenCount
== 2) {
314 if (StriCmp(TokenList
[1], L
"-1") == 0)
317 *Value
= Atoi(TokenList
[1]);
321 // handle a parameter with a single string argument
322 static VOID
HandleString(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
323 if ((TokenCount
== 2) && Target
) {
324 if ((StrLen(TokenList
[1]) > 1) && (TokenList
[1][0] == L
'+') &&
325 ((TokenList
[1][1] == L
',') || (TokenList
[1][1] == L
' '))) {
327 MergeStrings(Target
, TokenList
[1] + 2, L
',');
329 *Target
= StrDuplicate(TokenList
[1] + 2);
333 *Target
= StrDuplicate(TokenList
[1]);
336 } // static VOID HandleString()
338 // Handle a parameter with a series of string arguments, to replace or be added to a
339 // comma-delimited list. Passes each token through the CleanUpPathNameSlashes() function
340 // to ensure consistency in subsequent comparisons of filenames. If the first
341 // non-keyword token is "+", the list is added to the existing target string; otherwise,
342 // the tokens replace the current string.
343 static VOID
HandleStrings(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
345 BOOLEAN AddMode
= FALSE
;
347 if ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") == 0)) {
351 if ((*Target
!= NULL
) && !AddMode
) {
355 for (i
= 1; i
< TokenCount
; i
++) {
356 if ((i
!= 1) || !AddMode
) {
357 CleanUpPathNameSlashes(TokenList
[i
]);
358 MergeStrings(Target
, TokenList
[i
], L
',');
361 } // static VOID HandleStrings()
363 // Convert TimeString (in "HH:MM" format) to a pure-minute format. Values should be
364 // in the range from 0 (for 00:00, or midnight) to 1439 (for 23:59; aka LAST_MINUTE).
365 // Any value outside that range denotes an error in the specification. Note that if
366 // the input is a number that includes no colon, this function will return the original
367 // number in UINTN form.
368 static UINTN
HandleTime(IN CHAR16
*TimeString
) {
369 UINTN Hour
= 0, Minute
= 0, TimeLength
, i
= 0;
371 TimeLength
= StrLen(TimeString
);
372 while (i
< TimeLength
) {
373 if (TimeString
[i
] == L
':') {
377 if ((TimeString
[i
] >= L
'0') && (TimeString
[i
] <= '9')) {
379 Minute
+= (TimeString
[i
] - L
'0');
383 return (Hour
* 60 + Minute
);
384 } // BOOLEAN HandleTime()
386 static BOOLEAN
HandleBoolean(IN CHAR16
**TokenList
, IN UINTN TokenCount
) {
387 BOOLEAN TruthValue
= TRUE
;
389 if ((TokenCount
>= 2) && ((StriCmp(TokenList
[1], L
"0") == 0) ||
390 (StriCmp(TokenList
[1], L
"false") == 0) ||
391 (StriCmp(TokenList
[1], L
"off") == 0))) {
396 } // BOOLEAN HandleBoolean
398 // Sets the default boot loader IF the current time is within the bounds
399 // defined by the third and fourth tokens in the TokenList.
400 static VOID
SetDefaultByTime(IN CHAR16
**TokenList
, OUT CHAR16
**Default
) {
402 EFI_TIME CurrentTime
;
403 UINTN StartTime
= LAST_MINUTE
+ 1, EndTime
= LAST_MINUTE
+ 1, Now
;
404 BOOLEAN SetIt
= FALSE
;
406 StartTime
= HandleTime(TokenList
[2]);
407 EndTime
= HandleTime(TokenList
[3]);
409 if ((StartTime
<= LAST_MINUTE
) && (EndTime
<= LAST_MINUTE
)) {
410 Status
= refit_call2_wrapper(GetTime
, &CurrentTime
, NULL
);
411 if (Status
!= EFI_SUCCESS
)
413 Now
= CurrentTime
.Hour
* 60 + CurrentTime
.Minute
;
415 if (Now
> LAST_MINUTE
) { // Shouldn't happen; just being paranoid
416 Print(L
"Warning: Impossible system time: %d:%d\n", CurrentTime
.Hour
, CurrentTime
.Minute
);
418 } // if impossible time
420 if (StartTime
< EndTime
) { // Time range does NOT cross midnight
421 if ((Now
>= StartTime
) && (Now
<= EndTime
))
423 } else { // Time range DOES cross midnight
424 if ((Now
>= StartTime
) && (Now
<= EndTime
))
426 } // if/else time range crosses midnight
429 MyFreePool(*Default
);
430 *Default
= StrDuplicate(TokenList
[1]);
432 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
433 } // VOID SetDefaultByTime()
436 VOID
ReadConfig(CHAR16
*FileName
)
442 CHAR16
*TempStr
= NULL
;
444 EFI_GUID RefindGuid
= REFIND_GUID_VALUE
;
446 // Set a few defaults only if we're loading the default file.
447 if (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0) {
448 MyFreePool(GlobalConfig
.AlsoScan
);
449 GlobalConfig
.AlsoScan
= StrDuplicate(ALSO_SCAN_DIRS
);
450 MyFreePool(GlobalConfig
.DontScanDirs
);
452 if (SelfVolume
->VolName
) {
453 TempStr
= SelfVolume
->VolName
? StrDuplicate(SelfVolume
->VolName
) : NULL
;
455 TempStr
= AllocateZeroPool(256 * sizeof(CHAR16
));
457 SPrint(TempStr
, 255, L
"fs%d", SelfVolume
->VolNumber
);
460 MergeStrings(&TempStr
, SelfDirPath
, L
':');
461 MergeStrings(&TempStr
, MEMTEST_LOCATIONS
, L
',');
462 GlobalConfig
.DontScanDirs
= TempStr
;
463 MyFreePool(GlobalConfig
.DontScanFiles
);
464 GlobalConfig
.DontScanFiles
= StrDuplicate(DONT_SCAN_FILES
);
465 MergeStrings(&(GlobalConfig
.DontScanFiles
), MOK_NAMES
, L
',');
466 MyFreePool(GlobalConfig
.DontScanVolumes
);
467 GlobalConfig
.DontScanVolumes
= StrDuplicate(DONT_SCAN_VOLUMES
);
468 GlobalConfig
.WindowsRecoveryFiles
= StrDuplicate(WINDOWS_RECOVERY_FILES
);
469 if (GlobalConfig
.DefaultSelection
!= NULL
) {
470 MyFreePool(GlobalConfig
.DefaultSelection
);
471 GlobalConfig
.DefaultSelection
= NULL
;
473 Status
= EfivarGetRaw(&RefindGuid
, L
"PreviousBoot", (CHAR8
**) &(GlobalConfig
.DefaultSelection
), &i
);
474 if (Status
!= EFI_SUCCESS
)
475 GlobalConfig
.DefaultSelection
= NULL
;
478 if (!FileExists(SelfDir
, FileName
)) {
479 Print(L
"Configuration file '%s' missing!\n", FileName
);
480 if (!FileExists(SelfDir
, L
"icons")) {
481 Print(L
"Icons directory doesn't exist; setting textonly = TRUE!\n");
482 GlobalConfig
.TextOnly
= TRUE
;
487 Status
= ReadFile(SelfDir
, FileName
, &File
, &i
);
488 if (EFI_ERROR(Status
))
492 TokenCount
= ReadTokenLine(&File
, &TokenList
);
496 if (StriCmp(TokenList
[0], L
"timeout") == 0) {
497 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.Timeout
));
499 } else if (StriCmp(TokenList
[0], L
"hideui") == 0) {
500 for (i
= 1; i
< TokenCount
; i
++) {
501 FlagName
= TokenList
[i
];
502 if (StriCmp(FlagName
, L
"banner") == 0) {
503 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_BANNER
;
504 } else if (StriCmp(FlagName
, L
"label") == 0) {
505 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_LABEL
;
506 } else if (StriCmp(FlagName
, L
"singleuser") == 0) {
507 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SINGLEUSER
;
508 } else if (StriCmp(FlagName
, L
"hwtest") == 0) {
509 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HWTEST
;
510 } else if (StriCmp(FlagName
, L
"arrows") == 0) {
511 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_ARROWS
;
512 } else if (StriCmp(FlagName
, L
"hints") == 0) {
513 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HINTS
;
514 } else if (StriCmp(FlagName
, L
"editor") == 0) {
515 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_EDITOR
;
516 } else if (StriCmp(FlagName
, L
"safemode") == 0) {
517 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SAFEMODE
;
518 } else if (StriCmp(FlagName
, L
"badges") == 0) {
519 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_BADGES
;
520 } else if (StriCmp(FlagName
, L
"all") == 0) {
521 GlobalConfig
.HideUIFlags
= HIDEUI_FLAG_ALL
;
523 Print(L
" unknown hideui flag: '%s'\n", FlagName
);
527 } else if (StriCmp(TokenList
[0], L
"icons_dir") == 0) {
528 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.IconsDir
));
530 } else if (StriCmp(TokenList
[0], L
"scanfor") == 0) {
531 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
533 GlobalConfig
.ScanFor
[i
] = TokenList
[i
][0];
535 GlobalConfig
.ScanFor
[i
] = ' ';
538 } else if (StriCmp(TokenList
[0], L
"uefi_deep_legacy_scan") == 0) {
539 GlobalConfig
.DeepLegacyScan
= HandleBoolean(TokenList
, TokenCount
);
541 } else if ((StriCmp(TokenList
[0], L
"scan_delay") == 0) && (TokenCount
== 2)) {
542 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScanDelay
));
544 } else if (StriCmp(TokenList
[0], L
"also_scan_dirs") == 0) {
545 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.AlsoScan
));
547 } else if ((StriCmp(TokenList
[0], L
"don't_scan_volumes") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_volumes") == 0)) {
548 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
549 MyFreePool(GlobalConfig
.DontScanVolumes
);
550 GlobalConfig
.DontScanVolumes
= NULL
;
551 for (i
= 1; i
< TokenCount
; i
++) {
552 MergeStrings(&GlobalConfig
.DontScanVolumes
, TokenList
[i
], L
',');
555 } else if ((StriCmp(TokenList
[0], L
"don't_scan_dirs") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_dirs") == 0)) {
556 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanDirs
));
558 } else if ((StriCmp(TokenList
[0], L
"don't_scan_files") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_files") == 0)) {
559 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanFiles
));
561 } else if (StriCmp(TokenList
[0], L
"windows_recovery_files") == 0) {
562 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.WindowsRecoveryFiles
));
564 } else if (StriCmp(TokenList
[0], L
"scan_driver_dirs") == 0) {
565 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DriverDirs
));
567 } else if (StriCmp(TokenList
[0], L
"showtools") == 0) {
568 SetMem(GlobalConfig
.ShowTools
, NUM_TOOLS
* sizeof(UINTN
), 0);
569 for (i
= 1; (i
< TokenCount
) && (i
< NUM_TOOLS
); i
++) {
570 FlagName
= TokenList
[i
];
571 if (StriCmp(FlagName
, L
"shell") == 0) {
572 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHELL
;
573 } else if (StriCmp(FlagName
, L
"gptsync") == 0) {
574 GlobalConfig
.ShowTools
[i
- 1] = TAG_GPTSYNC
;
575 } else if (StriCmp(FlagName
, L
"gdisk") == 0) {
576 GlobalConfig
.ShowTools
[i
- 1] = TAG_GDISK
;
577 } else if (StriCmp(FlagName
, L
"about") == 0) {
578 GlobalConfig
.ShowTools
[i
- 1] = TAG_ABOUT
;
579 } else if (StriCmp(FlagName
, L
"exit") == 0) {
580 GlobalConfig
.ShowTools
[i
- 1] = TAG_EXIT
;
581 } else if (StriCmp(FlagName
, L
"reboot") == 0) {
582 GlobalConfig
.ShowTools
[i
- 1] = TAG_REBOOT
;
583 } else if (StriCmp(FlagName
, L
"shutdown") == 0) {
584 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHUTDOWN
;
585 } else if (StriCmp(FlagName
, L
"apple_recovery") == 0) {
586 GlobalConfig
.ShowTools
[i
- 1] = TAG_APPLE_RECOVERY
;
587 } else if (StriCmp(FlagName
, L
"windows_recovery") == 0) {
588 GlobalConfig
.ShowTools
[i
- 1] = TAG_WINDOWS_RECOVERY
;
589 } else if (StriCmp(FlagName
, L
"mok_tool") == 0) {
590 GlobalConfig
.ShowTools
[i
- 1] = TAG_MOK_TOOL
;
591 } else if (StriCmp(FlagName
, L
"firmware") == 0) {
592 GlobalConfig
.ShowTools
[i
- 1] = TAG_FIRMWARE
;
593 } else if ((StriCmp(FlagName
, L
"memtest86") == 0) || (StriCmp(FlagName
, L
"memtest") == 0)) {
594 GlobalConfig
.ShowTools
[i
- 1] = TAG_MEMTEST
;
595 } else if (StriCmp(FlagName
, L
"netboot") == 0) {
596 GlobalConfig
.ShowTools
[i
- 1] = TAG_NETBOOT
;
598 Print(L
" unknown showtools flag: '%s'\n", FlagName
);
600 } // showtools options
602 } else if (StriCmp(TokenList
[0], L
"banner") == 0) {
603 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.BannerFileName
));
605 } else if ((StriCmp(TokenList
[0], L
"banner_scale") == 0) && (TokenCount
== 2)) {
606 if (StriCmp(TokenList
[1], L
"noscale") == 0) {
607 GlobalConfig
.BannerScale
= BANNER_NOSCALE
;
608 } else if ((StriCmp(TokenList
[1], L
"fillscreen") == 0) || (StriCmp(TokenList
[1], L
"fullscreen") == 0)) {
609 GlobalConfig
.BannerScale
= BANNER_FILLSCREEN
;
611 Print(L
" unknown banner_type flag: '%s'\n", TokenList
[1]);
614 } else if ((StriCmp(TokenList
[0], L
"small_icon_size") == 0) && (TokenCount
== 2)) {
615 HandleInt(TokenList
, TokenCount
, &i
);
617 GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] = i
;
619 } else if ((StriCmp(TokenList
[0], L
"big_icon_size") == 0) && (TokenCount
== 2)) {
620 HandleInt(TokenList
, TokenCount
, &i
);
622 GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] = i
;
623 GlobalConfig
.IconSizes
[ICON_SIZE_BADGE
] = i
/ 4;
626 } else if (StriCmp(TokenList
[0], L
"selection_small") == 0) {
627 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionSmallFileName
));
629 } else if (StriCmp(TokenList
[0], L
"selection_big") == 0) {
630 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionBigFileName
));
632 } else if (StriCmp(TokenList
[0], L
"default_selection") == 0) {
633 if (TokenCount
== 4) {
634 SetDefaultByTime(TokenList
, &(GlobalConfig
.DefaultSelection
));
636 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.DefaultSelection
));
639 } else if (StriCmp(TokenList
[0], L
"textonly") == 0) {
640 GlobalConfig
.TextOnly
= HandleBoolean(TokenList
, TokenCount
);
642 } else if (StriCmp(TokenList
[0], L
"textmode") == 0) {
643 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.RequestedTextMode
));
645 } else if ((StriCmp(TokenList
[0], L
"resolution") == 0) && ((TokenCount
== 2) || (TokenCount
== 3))) {
646 GlobalConfig
.RequestedScreenWidth
= Atoi(TokenList
[1]);
648 GlobalConfig
.RequestedScreenHeight
= Atoi(TokenList
[2]);
650 GlobalConfig
.RequestedScreenHeight
= 0;
652 } else if (StriCmp(TokenList
[0], L
"screensaver") == 0) {
653 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScreensaverTime
));
655 } else if (StriCmp(TokenList
[0], L
"use_graphics_for") == 0) {
656 if ((TokenCount
== 2) || ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") != 0)))
657 GlobalConfig
.GraphicsFor
= 0;
658 for (i
= 1; i
< TokenCount
; i
++) {
659 if (StriCmp(TokenList
[i
], L
"osx") == 0) {
660 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_OSX
;
661 } else if (StriCmp(TokenList
[i
], L
"linux") == 0) {
662 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_LINUX
;
663 } else if (StriCmp(TokenList
[i
], L
"elilo") == 0) {
664 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_ELILO
;
665 } else if (StriCmp(TokenList
[i
], L
"grub") == 0) {
666 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_GRUB
;
667 } else if (StriCmp(TokenList
[i
], L
"windows") == 0) {
668 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_WINDOWS
;
670 } // for (graphics_on tokens)
672 } else if ((StriCmp(TokenList
[0], L
"font") == 0) && (TokenCount
== 2)) {
673 egLoadFont(TokenList
[1]);
675 } else if (StriCmp(TokenList
[0], L
"scan_all_linux_kernels") == 0) {
676 GlobalConfig
.ScanAllLinux
= HandleBoolean(TokenList
, TokenCount
);
678 } else if (StriCmp(TokenList
[0], L
"max_tags") == 0) {
679 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.MaxTags
));
681 } else if (StriCmp(TokenList
[0], L
"enable_and_lock_vmx") == 0) {
682 GlobalConfig
.EnableAndLockVMX
= HandleBoolean(TokenList
, TokenCount
);
684 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
685 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
686 if (StriCmp(TokenList
[1], FileName
) != 0) {
687 ReadConfig(TokenList
[1]);
692 FreeTokenLine(&TokenList
, &TokenCount
);
694 if ((GlobalConfig
.DontScanFiles
) && (GlobalConfig
.WindowsRecoveryFiles
))
695 MergeStrings(&(GlobalConfig
.DontScanFiles
), GlobalConfig
.WindowsRecoveryFiles
, L
',');
696 MyFreePool(File
.Buffer
);
698 if (!FileExists(SelfDir
, L
"icons") && !FileExists(SelfDir
, GlobalConfig
.IconsDir
)) {
699 Print(L
"Icons directory doesn't exist; setting textonly = TRUE!\n");
700 GlobalConfig
.TextOnly
= TRUE
;
702 } /* VOID ReadConfig() */
704 // Finds a volume with the specified Identifier (a filesystem label, a
705 // partition name, a partition GUID, or a number followed by a colon). If
706 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
707 // Returns TRUE if a match was found, FALSE if not.
708 static BOOLEAN
FindVolume(REFIT_VOLUME
**Volume
, CHAR16
*Identifier
) {
709 UINTN i
= 0, CountedVolumes
= 0, Length
;
711 BOOLEAN Found
= FALSE
, IdIsGuid
= FALSE
;
712 EFI_GUID VolGuid
, NullGuid
= NULL_GUID_VALUE
;
714 VolGuid
= StringAsGuid(Identifier
);
715 Length
= StrLen(Identifier
);
716 if ((Length
>= 2) && (Identifier
[Length
- 1] == L
':') &&
717 (Identifier
[0] >= L
'0') && (Identifier
[0] <= L
'9')) {
718 Number
= (INTN
) Atoi(Identifier
);
719 } else if (IsGuid(Identifier
)) {
722 while ((i
< VolumesCount
) && (!Found
)) {
723 if (Number
>= 0) { // User specified a volume by number
724 if (Volumes
[i
]->IsReadable
) {
725 if (CountedVolumes
== Number
) {
726 *Volume
= Volumes
[i
];
731 } else { // User specified a volume by label or GUID
732 if ((StriCmp(Identifier
, Volumes
[i
]->VolName
) == 0) ||
733 (StriCmp(Identifier
, Volumes
[i
]->PartName
) == 0)) {
734 *Volume
= Volumes
[i
];
737 if (IdIsGuid
&& !Found
) {
738 if (GuidsAreEqual(&VolGuid
, &(Volumes
[i
]->PartGuid
)) && !GuidsAreEqual(&NullGuid
, &(Volumes
[i
]->PartGuid
))) {
739 *Volume
= Volumes
[i
];
747 } // static VOID FindVolume()
749 static VOID
AddSubmenu(LOADER_ENTRY
*Entry
, REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
750 REFIT_MENU_SCREEN
*SubScreen
;
751 LOADER_ENTRY
*SubEntry
;
755 SubScreen
= InitializeSubScreen(Entry
);
757 // Set defaults for the new entry; will be modified based on lines read from the config. file....
758 SubEntry
= InitializeLoaderEntry(Entry
);
760 if ((SubEntry
== NULL
) || (SubScreen
== NULL
))
762 SubEntry
->me
.Title
= StrDuplicate(Title
);
764 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
766 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
767 MyFreePool(SubEntry
->LoaderPath
);
768 SubEntry
->LoaderPath
= StrDuplicate(TokenList
[1]);
769 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
771 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
772 if (FindVolume(&Volume
, TokenList
[1])) {
773 MyFreePool(SubEntry
->me
.Title
);
774 SubEntry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
775 SPrint(SubEntry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", Volume
->VolName
);
776 SubEntry
->me
.BadgeImage
= Volume
->VolBadgeImage
;
777 SubEntry
->VolName
= Volume
->VolName
;
780 } else if (StriCmp(TokenList
[0], L
"initrd") == 0) {
781 MyFreePool(SubEntry
->InitrdPath
);
782 SubEntry
->InitrdPath
= NULL
;
783 if (TokenCount
> 1) {
784 SubEntry
->InitrdPath
= StrDuplicate(TokenList
[1]);
787 } else if (StriCmp(TokenList
[0], L
"options") == 0) {
788 MyFreePool(SubEntry
->LoadOptions
);
789 SubEntry
->LoadOptions
= NULL
;
790 if (TokenCount
> 1) {
791 SubEntry
->LoadOptions
= StrDuplicate(TokenList
[1]);
794 } else if ((StriCmp(TokenList
[0], L
"add_options") == 0) && (TokenCount
> 1)) {
795 MergeStrings(&SubEntry
->LoadOptions
, TokenList
[1], L
' ');
797 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
798 SubEntry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
800 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
801 SubEntry
->Enabled
= FALSE
;
804 FreeTokenLine(&TokenList
, &TokenCount
);
807 if (SubEntry
->InitrdPath
!= NULL
) {
808 MergeStrings(&SubEntry
->LoadOptions
, L
"initrd=", L
' ');
809 MergeStrings(&SubEntry
->LoadOptions
, SubEntry
->InitrdPath
, 0);
810 MyFreePool(SubEntry
->InitrdPath
);
811 SubEntry
->InitrdPath
= NULL
;
813 if (SubEntry
->Enabled
== TRUE
) {
814 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
816 Entry
->me
.SubScreen
= SubScreen
;
817 } // VOID AddSubmenu()
819 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
820 // that entry. The calling function is then responsible for adding the entry to the
822 static LOADER_ENTRY
* AddStanzaEntries(REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
826 BOOLEAN DefaultsSet
= FALSE
, AddedSubmenu
= FALSE
;
827 REFIT_VOLUME
*CurrentVolume
= Volume
;
829 // prepare the menu entry
830 Entry
= InitializeLoaderEntry(NULL
);
834 Entry
->Title
= StrDuplicate(Title
);
835 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
836 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
838 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
839 Entry
->VolName
= CurrentVolume
->VolName
;
841 // Parse the config file to add options for a single stanza, terminating when the token
842 // is "}" or when the end of file is reached.
843 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
844 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
845 Entry
->LoaderPath
= StrDuplicate(TokenList
[1]);
846 Entry
->DevicePath
= FileDevicePath(CurrentVolume
->DeviceHandle
, Entry
->LoaderPath
);
847 SetLoaderDefaults(Entry
, TokenList
[1], CurrentVolume
);
848 MyFreePool(Entry
->LoadOptions
);
849 Entry
->LoadOptions
= NULL
; // Discard default options, if any
852 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
853 if (FindVolume(&CurrentVolume
, TokenList
[1])) {
854 MyFreePool(Entry
->me
.Title
);
855 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
856 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
857 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
858 Entry
->VolName
= CurrentVolume
->VolName
;
861 } else if ((StriCmp(TokenList
[0], L
"icon") == 0) && (TokenCount
> 1)) {
862 MyFreePool(Entry
->me
.Image
);
863 Entry
->me
.Image
= egLoadIcon(CurrentVolume
->RootDir
, TokenList
[1], GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
864 if (Entry
->me
.Image
== NULL
) {
865 Entry
->me
.Image
= DummyImage(GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
868 } else if ((StriCmp(TokenList
[0], L
"initrd") == 0) && (TokenCount
> 1)) {
869 MyFreePool(Entry
->InitrdPath
);
870 Entry
->InitrdPath
= StrDuplicate(TokenList
[1]);
872 } else if ((StriCmp(TokenList
[0], L
"options") == 0) && (TokenCount
> 1)) {
873 MyFreePool(Entry
->LoadOptions
);
874 Entry
->LoadOptions
= StrDuplicate(TokenList
[1]);
876 } else if ((StriCmp(TokenList
[0], L
"ostype") == 0) && (TokenCount
> 1)) {
877 if (TokenCount
> 1) {
878 Entry
->OSType
= TokenList
[1][0];
881 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
882 Entry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
884 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
885 Entry
->Enabled
= FALSE
;
887 } else if ((StriCmp(TokenList
[0], L
"submenuentry") == 0) && (TokenCount
> 1)) {
888 AddSubmenu(Entry
, File
, CurrentVolume
, TokenList
[1]);
891 } // set options to pass to the loader program
892 FreeTokenLine(&TokenList
, &TokenCount
);
896 AddMenuEntry(Entry
->me
.SubScreen
, &MenuEntryReturn
);
898 if (Entry
->InitrdPath
) {
899 MergeStrings(&Entry
->LoadOptions
, L
"initrd=", L
' ');
900 MergeStrings(&Entry
->LoadOptions
, Entry
->InitrdPath
, 0);
901 MyFreePool(Entry
->InitrdPath
);
902 Entry
->InitrdPath
= NULL
;
906 SetLoaderDefaults(Entry
, L
"\\EFI\\BOOT\\nemo.efi", CurrentVolume
); // user included no "loader" line; use bogus one
909 } // static VOID AddStanzaEntries()
911 // Read the user-configured menu entries from refind.conf and add or delete
912 // entries based on the contents of that file....
913 VOID
ScanUserConfigured(CHAR16
*FileName
)
917 REFIT_VOLUME
*Volume
;
919 CHAR16
*Title
= NULL
;
920 UINTN TokenCount
, size
;
923 if (FileExists(SelfDir
, FileName
)) {
924 Status
= ReadFile(SelfDir
, FileName
, &File
, &size
);
925 if (EFI_ERROR(Status
))
930 while ((TokenCount
= ReadTokenLine(&File
, &TokenList
)) > 0) {
931 if ((StriCmp(TokenList
[0], L
"menuentry") == 0) && (TokenCount
> 1)) {
932 Title
= StrDuplicate(TokenList
[1]);
933 Entry
= AddStanzaEntries(&File
, Volume
, TokenList
[1]);
934 if (Entry
->Enabled
) {
935 if (Entry
->me
.SubScreen
== NULL
)
936 GenerateSubScreen(Entry
, Volume
);
937 AddPreparedLoaderEntry(Entry
);
943 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
944 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
945 if (StriCmp(TokenList
[1], FileName
) != 0) {
946 ScanUserConfigured(TokenList
[1]);
950 FreeTokenLine(&TokenList
, &TokenCount
);
953 } // VOID ScanUserConfigured()
955 // Create an options file based on /etc/fstab. The resulting file has two options
956 // lines, one of which boots the system with "ro root={rootfs}" and the other of
957 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
958 // filesystem identifier associated with the "/" line in /etc/fstab.
959 static REFIT_FILE
* GenerateOptionsFromEtcFstab(REFIT_VOLUME
*Volume
) {
961 REFIT_FILE
*Options
= NULL
, *Fstab
= NULL
;
963 CHAR16
**TokenList
, *Line
, Root
[100];
965 if (FileExists(Volume
->RootDir
, L
"\\etc\\fstab")) {
966 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
967 Fstab
= AllocateZeroPool(sizeof(REFIT_FILE
));
968 Status
= ReadFile(Volume
->RootDir
, L
"\\etc\\fstab", Fstab
, &i
);
969 if (CheckError(Status
, L
"while reading /etc/fstab")) {
976 } else { // File read; locate root fs and create entries
977 Options
->Encoding
= ENCODING_UTF16_LE
;
978 while ((TokenCount
= ReadTokenLine(Fstab
, &TokenList
)) > 0) {
979 if (TokenCount
> 2) {
981 if (StriCmp(TokenList
[1], L
"\\") == 0) {
982 SPrint(Root
, 99, L
"%s", TokenList
[0]);
983 } else if (StriCmp(TokenList
[2], L
"\\") == 0) {
984 SPrint(Root
, 99, L
"%s=%s", TokenList
[0], TokenList
[1]);
985 } // if/elseif/elseif
986 if (Root
[0] != L
'\0') {
987 for (i
= 0; i
< StrLen(Root
); i
++)
990 Line
= PoolPrint(L
"\"Boot with normal options\" \"ro root=%s\"\n", Root
);
991 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
993 Line
= PoolPrint(L
"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root
);
994 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
995 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
998 FreeTokenLine(&TokenList
, &TokenCount
);
1001 Options
->Current8Ptr
= (CHAR8
*)Options
->Buffer
;
1002 Options
->End8Ptr
= Options
->Current8Ptr
+ Options
->BufferSize
;
1003 Options
->Current16Ptr
= (CHAR16
*)Options
->Buffer
;
1004 Options
->End16Ptr
= Options
->Current16Ptr
+ (Options
->BufferSize
>> 1);
1006 MyFreePool(Fstab
->Buffer
);
1008 } // if/else file read error
1009 } // if /etc/fstab exists
1011 } // GenerateOptionsFromEtcFstab()
1014 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1015 // and Volume variables identify the location of the options file, but not its name --
1016 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1017 // file in the target directory, and this function finds the file with a name in the
1018 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1019 // directory and loads it. This function tries multiple files because I originally
1020 // used the filename linux.conf, but close on the heels of that decision, the Linux
1021 // kernel developers decided to use that name for a similar purpose, but with a
1022 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1023 // but I want a migration period in which both names are used.
1024 // If a rEFInd options file can't be found, try to generate minimal options from
1025 // /etc/fstab on the same volume as the kernel. This typically works only if the
1026 // kernel is being read from the Linux root filesystem.
1028 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1030 REFIT_FILE
* ReadLinuxOptionsFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1031 CHAR16
*OptionsFilename
, *FullFilename
;
1032 BOOLEAN GoOn
= TRUE
, FileFound
= FALSE
;
1034 REFIT_FILE
*File
= NULL
;
1038 OptionsFilename
= FindCommaDelimited(LINUX_OPTIONS_FILENAMES
, i
++);
1039 FullFilename
= FindPath(LoaderPath
);
1040 if ((OptionsFilename
!= NULL
) && (FullFilename
!= NULL
)) {
1041 MergeStrings(&FullFilename
, OptionsFilename
, '\\');
1042 if (FileExists(Volume
->RootDir
, FullFilename
)) {
1043 File
= AllocateZeroPool(sizeof(REFIT_FILE
));
1044 Status
= ReadFile(Volume
->RootDir
, FullFilename
, File
, &size
);
1045 if (CheckError(Status
, L
"while loading the Linux options file")) {
1054 } else { // a filename string is NULL
1058 File
= GenerateOptionsFromEtcFstab(Volume
);
1059 MyFreePool(OptionsFilename
);
1060 MyFreePool(FullFilename
);
1061 OptionsFilename
= FullFilename
= NULL
;
1064 } // static REFIT_FILE * ReadLinuxOptionsFile()
1066 // Retrieve a single line of options from a Linux kernel options file
1067 CHAR16
* GetFirstOptionsFromFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1069 CHAR16
*Options
= NULL
;
1073 File
= ReadLinuxOptionsFile(LoaderPath
, Volume
);
1075 TokenCount
= ReadTokenLine(File
, &TokenList
);
1077 Options
= StrDuplicate(TokenList
[1]);
1078 FreeTokenLine(&TokenList
, &TokenCount
);
1082 } // static CHAR16 * GetOptionsFile()