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-2013 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"
56 #define LINUX_OPTIONS_FILENAMES L"refind_linux.conf,refind-linux.conf"
57 #define MAXCONFIGFILESIZE (128*1024)
59 #define ENCODING_ISO8859_1 (0)
60 #define ENCODING_UTF8 (1)
61 #define ENCODING_UTF16_LE (2)
63 #define GetTime ST->RuntimeServices->GetTime
64 #define LAST_MINUTE 1439 /* Last minute of a day */
66 static REFIT_MENU_ENTRY MenuEntryReturn
= { L
"Return to Main Menu", TAG_RETURN
, 0, 0, 0, NULL
, NULL
, NULL
};
69 // read a file into a buffer
72 EFI_STATUS
ReadFile(IN EFI_FILE_HANDLE BaseDir
, IN CHAR16
*FileName
, IN OUT REFIT_FILE
*File
, OUT UINTN
*size
)
75 EFI_FILE_HANDLE FileHandle
;
76 EFI_FILE_INFO
*FileInfo
;
83 // read the file, allocating a buffer on the way
84 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
85 SPrint(Message
, 255, L
"while loading the file '%s'", FileName
);
86 if (CheckError(Status
, Message
))
89 FileInfo
= LibFileInfo(FileHandle
);
90 if (FileInfo
== NULL
) {
91 // TODO: print and register the error
92 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
93 return EFI_LOAD_ERROR
;
95 ReadSize
= FileInfo
->FileSize
;
98 File
->BufferSize
= (UINTN
)ReadSize
;
99 File
->Buffer
= AllocatePool(File
->BufferSize
);
100 if (File
->Buffer
== NULL
) {
102 return EFI_OUT_OF_RESOURCES
;
104 *size
= File
->BufferSize
;
106 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &File
->BufferSize
, File
->Buffer
);
107 if (CheckError(Status
, Message
)) {
108 MyFreePool(File
->Buffer
);
110 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
113 Status
= refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
116 File
->Current8Ptr
= (CHAR8
*)File
->Buffer
;
117 File
->End8Ptr
= File
->Current8Ptr
+ File
->BufferSize
;
118 File
->Current16Ptr
= (CHAR16
*)File
->Buffer
;
119 File
->End16Ptr
= File
->Current16Ptr
+ (File
->BufferSize
>> 1);
122 File
->Encoding
= ENCODING_ISO8859_1
; // default: 1:1 translation of CHAR8 to CHAR16
123 if (File
->BufferSize
>= 4) {
124 if (File
->Buffer
[0] == 0xFF && File
->Buffer
[1] == 0xFE) {
125 // BOM in UTF-16 little endian (or UTF-32 little endian)
126 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
127 File
->Current16Ptr
++;
128 } else if (File
->Buffer
[0] == 0xEF && File
->Buffer
[1] == 0xBB && File
->Buffer
[2] == 0xBF) {
130 File
->Encoding
= ENCODING_UTF8
; // translate from UTF-8 to UTF-16
131 File
->Current8Ptr
+= 3;
132 } else if (File
->Buffer
[1] == 0 && File
->Buffer
[3] == 0) {
133 File
->Encoding
= ENCODING_UTF16_LE
; // use CHAR16 as is
135 // TODO: detect other encodings as they are implemented
142 // get a single line of text from a file
145 static CHAR16
*ReadLine(REFIT_FILE
*File
)
150 if (File
->Buffer
== NULL
)
153 if (File
->Encoding
== ENCODING_ISO8859_1
|| File
->Encoding
== ENCODING_UTF8
) {
155 CHAR8
*p
, *LineStart
, *LineEnd
;
157 p
= File
->Current8Ptr
;
158 if (p
>= File
->End8Ptr
)
162 for (; p
< File
->End8Ptr
; p
++)
163 if (*p
== 13 || *p
== 10)
166 for (; p
< File
->End8Ptr
; p
++)
167 if (*p
!= 13 && *p
!= 10)
169 File
->Current8Ptr
= p
;
171 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
172 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
177 if (File
->Encoding
== ENCODING_ISO8859_1
) {
178 for (p
= LineStart
; p
< LineEnd
; )
180 } else if (File
->Encoding
== ENCODING_UTF8
) {
181 // TODO: actually handle UTF-8
182 for (p
= LineStart
; p
< LineEnd
; )
187 } else if (File
->Encoding
== ENCODING_UTF16_LE
) {
189 CHAR16
*p
, *LineStart
, *LineEnd
;
191 p
= File
->Current16Ptr
;
192 if (p
>= File
->End16Ptr
)
196 for (; p
< File
->End16Ptr
; p
++)
197 if (*p
== 13 || *p
== 10)
200 for (; p
< File
->End16Ptr
; p
++)
201 if (*p
!= 13 && *p
!= 10)
203 File
->Current16Ptr
= p
;
205 LineLength
= (UINTN
)(LineEnd
- LineStart
) + 1;
206 Line
= AllocatePool(LineLength
* sizeof(CHAR16
));
210 for (p
= LineStart
, q
= Line
; p
< LineEnd
; )
215 return NULL
; // unsupported encoding
220 // Returns FALSE if *p points to the end of a token, TRUE otherwise.
221 // Also modifies *p **IF** the first and second characters are both
222 // quotes ('"'); it deletes one of them.
223 static BOOLEAN
KeepReading(IN OUT CHAR16
*p
, IN OUT BOOLEAN
*IsQuoted
) {
224 BOOLEAN MoreToRead
= FALSE
;
227 if ((p
== NULL
) || (IsQuoted
== NULL
))
233 if ((*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '#' && *p
!= ',') || *IsQuoted
) {
238 Temp
= StrDuplicate(&p
[1]);
245 *IsQuoted
= !(*IsQuoted
);
247 } // if/else second character is a quote
248 } // if first character is a quote
251 } // BOOLEAN KeepReading()
254 // get a line of tokens from a file
256 UINTN
ReadTokenLine(IN REFIT_FILE
*File
, OUT CHAR16
***TokenList
)
258 BOOLEAN LineFinished
, IsQuoted
= FALSE
;
259 CHAR16
*Line
, *Token
, *p
;
260 UINTN TokenCount
= 0;
264 while (TokenCount
== 0) {
265 Line
= ReadLine(File
);
270 LineFinished
= FALSE
;
271 while (!LineFinished
) {
272 // skip whitespace & find start of token
273 while ((*p
== ' ' || *p
== '\t' || *p
== '=' || *p
== ',') && !IsQuoted
)
275 if (*p
== 0 || *p
== '#')
279 IsQuoted
= !IsQuoted
;
285 while (KeepReading(p
, &IsQuoted
)) {
286 if ((*p
== L
'/') && !IsQuoted
) // Switch Unix-style to DOS-style directory separators
290 if (*p
== L
'\0' || *p
== L
'#')
294 AddListElement((VOID
***)TokenList
, &TokenCount
, (VOID
*)StrDuplicate(Token
));
300 } /* ReadTokenLine() */
302 VOID
FreeTokenLine(IN OUT CHAR16
***TokenList
, IN OUT UINTN
*TokenCount
)
304 // TODO: also free the items
305 FreeList((VOID
***)TokenList
, TokenCount
);
308 // handle a parameter with a single integer argument
309 static VOID
HandleInt(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT UINTN
*Value
)
311 if (TokenCount
== 2) {
312 if (StriCmp(TokenList
[1], L
"-1") == 0)
315 *Value
= Atoi(TokenList
[1]);
319 // handle a parameter with a single string argument
320 static VOID
HandleString(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
321 if (TokenCount
== 2) {
323 *Target
= StrDuplicate(TokenList
[1]);
325 } // static VOID HandleString()
327 // Handle a parameter with a series of string arguments, to replace or be added to a
328 // comma-delimited list. Passes each token through the CleanUpPathNameSlashes() function
329 // to ensure consistency in subsequent comparisons of filenames. If the first
330 // non-keyword token is "+", the list is added to the existing target string; otherwise,
331 // the tokens replace the current string.
332 static VOID
HandleStrings(IN CHAR16
**TokenList
, IN UINTN TokenCount
, OUT CHAR16
**Target
) {
334 BOOLEAN AddMode
= FALSE
;
336 if ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") == 0)) {
340 if ((*Target
!= NULL
) && !AddMode
) {
344 for (i
= 1; i
< TokenCount
; i
++) {
345 if ((i
!= 1) || !AddMode
) {
346 CleanUpPathNameSlashes(TokenList
[i
]);
347 MergeStrings(Target
, TokenList
[i
], L
',');
350 } // static VOID HandleStrings()
352 // Convert TimeString (in "HH:MM" format) to a pure-minute format. Values should be
353 // in the range from 0 (for 00:00, or midnight) to 1439 (for 23:59; aka LAST_MINUTE).
354 // Any value outside that range denotes an error in the specification. Note that if
355 // the input is a number that includes no colon, this function will return the original
356 // number in UINTN form.
357 static UINTN
HandleTime(IN CHAR16
*TimeString
) {
358 UINTN Hour
= 0, Minute
= 0, TimeLength
, i
= 0;
360 TimeLength
= StrLen(TimeString
);
361 while (i
< TimeLength
) {
362 if (TimeString
[i
] == L
':') {
366 if ((TimeString
[i
] >= L
'0') && (TimeString
[i
] <= '9')) {
368 Minute
+= (TimeString
[i
] - L
'0');
372 return (Hour
* 60 + Minute
);
373 } // BOOLEAN HandleTime()
375 // Sets the default boot loader IF the current time is within the bounds
376 // defined by the third and fourth tokens in the TokenList.
377 static VOID
SetDefaultByTime(IN CHAR16
**TokenList
, OUT CHAR16
**Default
) {
379 EFI_TIME CurrentTime
;
380 UINTN StartTime
= LAST_MINUTE
+ 1, EndTime
= LAST_MINUTE
+ 1, Now
;
381 BOOLEAN SetIt
= FALSE
;
383 StartTime
= HandleTime(TokenList
[2]);
384 EndTime
= HandleTime(TokenList
[3]);
386 if ((StartTime
<= LAST_MINUTE
) && (EndTime
<= LAST_MINUTE
)) {
387 Status
= refit_call2_wrapper(GetTime
, &CurrentTime
, NULL
);
388 if (Status
!= EFI_SUCCESS
)
390 Now
= CurrentTime
.Hour
* 60 + CurrentTime
.Minute
;
392 if (Now
> LAST_MINUTE
) { // Shouldn't happen; just being paranoid
393 Print(L
"Warning: Impossible system time: %d:%d\n", CurrentTime
.Hour
, CurrentTime
.Minute
);
395 } // if impossible time
397 if (StartTime
< EndTime
) { // Time range does NOT cross midnight
398 if ((Now
>= StartTime
) && (Now
<= EndTime
))
400 } else { // Time range DOES cross midnight
401 if ((Now
>= StartTime
) && (Now
<= EndTime
))
403 } // if/else time range crosses midnight
406 MyFreePool(*Default
);
407 *Default
= StrDuplicate(TokenList
[1]);
409 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
410 } // VOID SetDefaultByTime()
413 VOID
ReadConfig(CHAR16
*FileName
)
419 CHAR16
*TempStr
= NULL
;
422 // Set a few defaults only if we're loading the default file.
423 if (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0) {
424 MyFreePool(GlobalConfig
.AlsoScan
);
425 GlobalConfig
.AlsoScan
= StrDuplicate(ALSO_SCAN_DIRS
);
426 MyFreePool(GlobalConfig
.DontScanDirs
);
428 if (SelfVolume
->VolName
) {
429 TempStr
= SelfVolume
->VolName
? StrDuplicate(SelfVolume
->VolName
) : NULL
;
431 TempStr
= AllocateZeroPool(256 * sizeof(CHAR16
));
433 SPrint(TempStr
, 255, L
"fs%d", SelfVolume
->VolNumber
);
436 MergeStrings(&TempStr
, SelfDirPath
, L
':');
437 MergeStrings(&TempStr
, MEMTEST_LOCATIONS
, L
',');
438 GlobalConfig
.DontScanDirs
= TempStr
;
439 MyFreePool(GlobalConfig
.DontScanFiles
);
440 GlobalConfig
.DontScanFiles
= StrDuplicate(DONT_SCAN_FILES
);
441 MergeStrings(&(GlobalConfig
.DontScanFiles
), MOK_NAMES
, L
',');
442 MyFreePool(GlobalConfig
.DontScanVolumes
);
443 GlobalConfig
.DontScanVolumes
= StrDuplicate(DONT_SCAN_VOLUMES
);
444 GlobalConfig
.WindowsRecoveryFiles
= StrDuplicate(WINDOWS_RECOVERY_FILES
);
447 if (!FileExists(SelfDir
, FileName
)) {
448 Print(L
"Configuration file '%s' missing!\n", FileName
);
452 Status
= ReadFile(SelfDir
, FileName
, &File
, &i
);
453 if (EFI_ERROR(Status
))
457 TokenCount
= ReadTokenLine(&File
, &TokenList
);
461 if (StriCmp(TokenList
[0], L
"timeout") == 0) {
462 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.Timeout
));
464 } else if (StriCmp(TokenList
[0], L
"hideui") == 0) {
465 for (i
= 1; i
< TokenCount
; i
++) {
466 FlagName
= TokenList
[i
];
467 if (StriCmp(FlagName
, L
"banner") == 0) {
468 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_BANNER
;
469 } else if (StriCmp(FlagName
, L
"label") == 0) {
470 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_LABEL
;
471 } else if (StriCmp(FlagName
, L
"singleuser") == 0) {
472 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SINGLEUSER
;
473 } else if (StriCmp(FlagName
, L
"hwtest") == 0) {
474 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HWTEST
;
475 } else if (StriCmp(FlagName
, L
"arrows") == 0) {
476 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_ARROWS
;
477 } else if (StriCmp(FlagName
, L
"hints") == 0) {
478 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_HINTS
;
479 } else if (StriCmp(FlagName
, L
"editor") == 0) {
480 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_EDITOR
;
481 } else if (StriCmp(FlagName
, L
"safemode") == 0) {
482 GlobalConfig
.HideUIFlags
|= HIDEUI_FLAG_SAFEMODE
;
483 } else if (StriCmp(FlagName
, L
"all") == 0) {
484 GlobalConfig
.HideUIFlags
= HIDEUI_FLAG_ALL
;
486 Print(L
" unknown hideui flag: '%s'\n", FlagName
);
490 } else if (StriCmp(TokenList
[0], L
"icons_dir") == 0) {
491 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.IconsDir
));
493 } else if (StriCmp(TokenList
[0], L
"scanfor") == 0) {
494 for (i
= 0; i
< NUM_SCAN_OPTIONS
; i
++) {
496 GlobalConfig
.ScanFor
[i
] = TokenList
[i
][0];
498 GlobalConfig
.ScanFor
[i
] = ' ';
501 } else if ((StriCmp(TokenList
[0], L
"scan_delay") == 0) && (TokenCount
== 2)) {
502 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScanDelay
));
504 } else if (StriCmp(TokenList
[0], L
"also_scan_dirs") == 0) {
505 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.AlsoScan
));
507 } else if ((StriCmp(TokenList
[0], L
"don't_scan_volumes") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_volumes") == 0)) {
508 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
509 MyFreePool(GlobalConfig
.DontScanVolumes
);
510 GlobalConfig
.DontScanVolumes
= NULL
;
511 for (i
= 1; i
< TokenCount
; i
++) {
512 MergeStrings(&GlobalConfig
.DontScanVolumes
, TokenList
[i
], L
',');
515 } else if ((StriCmp(TokenList
[0], L
"don't_scan_dirs") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_dirs") == 0)) {
516 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanDirs
));
518 } else if ((StriCmp(TokenList
[0], L
"don't_scan_files") == 0) || (StriCmp(TokenList
[0], L
"dont_scan_files") == 0)) {
519 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DontScanFiles
));
521 } else if (StriCmp(TokenList
[0], L
"windows_recovery_files") == 0) {
522 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.WindowsRecoveryFiles
));
524 } else if (StriCmp(TokenList
[0], L
"scan_driver_dirs") == 0) {
525 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DriverDirs
));
527 } else if (StriCmp(TokenList
[0], L
"showtools") == 0) {
528 SetMem(GlobalConfig
.ShowTools
, NUM_TOOLS
* sizeof(UINTN
), 0);
529 for (i
= 1; (i
< TokenCount
) && (i
< NUM_TOOLS
); i
++) {
530 FlagName
= TokenList
[i
];
531 if (StriCmp(FlagName
, L
"shell") == 0) {
532 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHELL
;
533 } else if (StriCmp(FlagName
, L
"gptsync") == 0) {
534 GlobalConfig
.ShowTools
[i
- 1] = TAG_GPTSYNC
;
535 } else if (StriCmp(FlagName
, L
"about") == 0) {
536 GlobalConfig
.ShowTools
[i
- 1] = TAG_ABOUT
;
537 } else if (StriCmp(FlagName
, L
"exit") == 0) {
538 GlobalConfig
.ShowTools
[i
- 1] = TAG_EXIT
;
539 } else if (StriCmp(FlagName
, L
"reboot") == 0) {
540 GlobalConfig
.ShowTools
[i
- 1] = TAG_REBOOT
;
541 } else if (StriCmp(FlagName
, L
"shutdown") == 0) {
542 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHUTDOWN
;
543 } else if (StriCmp(FlagName
, L
"apple_recovery") == 0) {
544 GlobalConfig
.ShowTools
[i
- 1] = TAG_APPLE_RECOVERY
;
545 } else if (StriCmp(FlagName
, L
"windows_recovery") == 0) {
546 GlobalConfig
.ShowTools
[i
- 1] = TAG_WINDOWS_RECOVERY
;
547 } else if (StriCmp(FlagName
, L
"mok_tool") == 0) {
548 GlobalConfig
.ShowTools
[i
- 1] = TAG_MOK_TOOL
;
549 } else if (StriCmp(FlagName
, L
"firmware") == 0) {
550 GlobalConfig
.ShowTools
[i
- 1] = TAG_FIRMWARE
;
551 } else if ((StriCmp(FlagName
, L
"memtest86") == 0) || (StriCmp(FlagName
, L
"memtest") == 0)) {
552 GlobalConfig
.ShowTools
[i
- 1] = TAG_MEMTEST
;
554 Print(L
" unknown showtools flag: '%s'\n", FlagName
);
556 } // showtools options
558 } else if (StriCmp(TokenList
[0], L
"banner") == 0) {
559 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.BannerFileName
));
561 } else if ((StriCmp(TokenList
[0], L
"banner_scale") == 0) && (TokenCount
== 2)) {
562 if (StriCmp(TokenList
[1], L
"noscale") == 0) {
563 GlobalConfig
.BannerScale
= BANNER_NOSCALE
;
564 } else if ((StriCmp(TokenList
[1], L
"fillscreen") == 0) || (StriCmp(TokenList
[1], L
"fullscreen") == 0)) {
565 GlobalConfig
.BannerScale
= BANNER_FILLSCREEN
;
567 Print(L
" unknown banner_type flag: '%s'\n", TokenList
[1]);
570 } else if ((StriCmp(TokenList
[0], L
"small_icon_size") == 0) && (TokenCount
== 2)) {
571 HandleInt(TokenList
, TokenCount
, &i
);
573 GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] = i
;
575 } else if ((StriCmp(TokenList
[0], L
"big_icon_size") == 0) && (TokenCount
== 2)) {
576 HandleInt(TokenList
, TokenCount
, &i
);
578 GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] = i
;
579 GlobalConfig
.IconSizes
[ICON_SIZE_BADGE
] = i
/ 4;
582 } else if (StriCmp(TokenList
[0], L
"selection_small") == 0) {
583 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionSmallFileName
));
585 } else if (StriCmp(TokenList
[0], L
"selection_big") == 0) {
586 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionBigFileName
));
588 } else if (StriCmp(TokenList
[0], L
"default_selection") == 0) {
589 if (TokenCount
== 4) {
590 SetDefaultByTime(TokenList
, &(GlobalConfig
.DefaultSelection
));
592 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.DefaultSelection
));
595 } else if (StriCmp(TokenList
[0], L
"textonly") == 0) {
596 if ((TokenCount
>= 2) && (StriCmp(TokenList
[1], L
"0") == 0)) {
597 GlobalConfig
.TextOnly
= FALSE
;
599 GlobalConfig
.TextOnly
= TRUE
;
602 } else if (StriCmp(TokenList
[0], L
"textmode") == 0) {
603 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.RequestedTextMode
));
605 } else if ((StriCmp(TokenList
[0], L
"resolution") == 0) && ((TokenCount
== 2) || (TokenCount
== 3))) {
606 GlobalConfig
.RequestedScreenWidth
= Atoi(TokenList
[1]);
608 GlobalConfig
.RequestedScreenHeight
= Atoi(TokenList
[2]);
610 GlobalConfig
.RequestedScreenHeight
= 0;
612 } else if (StriCmp(TokenList
[0], L
"screensaver") == 0) {
613 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScreensaverTime
));
615 } else if (StriCmp(TokenList
[0], L
"use_graphics_for") == 0) {
616 if ((TokenCount
== 2) || ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") != 0)))
617 GlobalConfig
.GraphicsFor
= 0;
618 for (i
= 1; i
< TokenCount
; i
++) {
619 if (StriCmp(TokenList
[i
], L
"osx") == 0) {
620 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_OSX
;
621 } else if (StriCmp(TokenList
[i
], L
"linux") == 0) {
622 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_LINUX
;
623 } else if (StriCmp(TokenList
[i
], L
"elilo") == 0) {
624 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_ELILO
;
625 } else if (StriCmp(TokenList
[i
], L
"grub") == 0) {
626 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_GRUB
;
627 } else if (StriCmp(TokenList
[i
], L
"windows") == 0) {
628 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_WINDOWS
;
630 } // for (graphics_on tokens)
632 } else if ((StriCmp(TokenList
[0], L
"font") == 0) && (TokenCount
== 2)) {
633 egLoadFont(TokenList
[1]);
635 } else if (StriCmp(TokenList
[0], L
"scan_all_linux_kernels") == 0) {
636 if ((TokenCount
>= 2) && (StriCmp(TokenList
[1], L
"0") == 0)) {
637 GlobalConfig
.ScanAllLinux
= FALSE
;
639 GlobalConfig
.ScanAllLinux
= TRUE
;
642 } else if (StriCmp(TokenList
[0], L
"max_tags") == 0) {
643 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.MaxTags
));
645 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
646 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
647 if (StriCmp(TokenList
[1], FileName
) != 0) {
648 ReadConfig(TokenList
[1]);
653 FreeTokenLine(&TokenList
, &TokenCount
);
655 if ((GlobalConfig
.DontScanFiles
) && (GlobalConfig
.WindowsRecoveryFiles
))
656 MergeStrings(&(GlobalConfig
.DontScanFiles
), GlobalConfig
.WindowsRecoveryFiles
, L
',');
657 MyFreePool(File
.Buffer
);
658 } /* VOID ReadConfig() */
660 // Finds a volume with the specified Identifier (a volume label or a number
661 // followed by a colon, for the moment). If found, sets *Volume to point to
662 // that volume. If not, leaves it unchanged.
663 // Returns TRUE if a match was found, FALSE if not.
664 static BOOLEAN
FindVolume(REFIT_VOLUME
**Volume
, CHAR16
*Identifier
) {
665 UINTN i
= 0, CountedVolumes
= 0;
667 BOOLEAN Found
= FALSE
;
669 if ((StrLen(Identifier
) >= 2) && (Identifier
[StrLen(Identifier
) - 1] == L
':') &&
670 (Identifier
[0] >= L
'0') && (Identifier
[0] <= L
'9')) {
671 Number
= (INTN
) Atoi(Identifier
);
673 while ((i
< VolumesCount
) && (!Found
)) {
674 if (Number
>= 0) { // User specified a volume by number
675 if (Volumes
[i
]->IsReadable
) {
676 if (CountedVolumes
== Number
) {
677 *Volume
= Volumes
[i
];
682 } else { // User specified a volume by label
683 if (StriCmp(Identifier
, Volumes
[i
]->VolName
) == 0) {
684 *Volume
= Volumes
[i
];
691 } // static VOID FindVolume()
693 static VOID
AddSubmenu(LOADER_ENTRY
*Entry
, REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
694 REFIT_MENU_SCREEN
*SubScreen
;
695 LOADER_ENTRY
*SubEntry
;
699 SubScreen
= InitializeSubScreen(Entry
);
701 // Set defaults for the new entry; will be modified based on lines read from the config. file....
702 SubEntry
= InitializeLoaderEntry(Entry
);
704 if ((SubEntry
== NULL
) || (SubScreen
== NULL
))
706 SubEntry
->me
.Title
= StrDuplicate(Title
);
708 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
710 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
711 MyFreePool(SubEntry
->LoaderPath
);
712 SubEntry
->LoaderPath
= StrDuplicate(TokenList
[1]);
713 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
715 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
716 if (FindVolume(&Volume
, TokenList
[1])) {
717 MyFreePool(SubEntry
->me
.Title
);
718 SubEntry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
719 SPrint(SubEntry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", Volume
->VolName
);
720 SubEntry
->me
.BadgeImage
= Volume
->VolBadgeImage
;
721 SubEntry
->VolName
= Volume
->VolName
;
724 } else if (StriCmp(TokenList
[0], L
"initrd") == 0) {
725 MyFreePool(SubEntry
->InitrdPath
);
726 SubEntry
->InitrdPath
= NULL
;
727 if (TokenCount
> 1) {
728 SubEntry
->InitrdPath
= StrDuplicate(TokenList
[1]);
731 } else if (StriCmp(TokenList
[0], L
"options") == 0) {
732 MyFreePool(SubEntry
->LoadOptions
);
733 SubEntry
->LoadOptions
= NULL
;
734 if (TokenCount
> 1) {
735 SubEntry
->LoadOptions
= StrDuplicate(TokenList
[1]);
738 } else if ((StriCmp(TokenList
[0], L
"add_options") == 0) && (TokenCount
> 1)) {
739 MergeStrings(&SubEntry
->LoadOptions
, TokenList
[1], L
' ');
741 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
742 SubEntry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
744 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
745 SubEntry
->Enabled
= FALSE
;
748 FreeTokenLine(&TokenList
, &TokenCount
);
751 if (SubEntry
->InitrdPath
!= NULL
) {
752 MergeStrings(&SubEntry
->LoadOptions
, L
"initrd=", L
' ');
753 MergeStrings(&SubEntry
->LoadOptions
, SubEntry
->InitrdPath
, 0);
754 MyFreePool(SubEntry
->InitrdPath
);
755 SubEntry
->InitrdPath
= NULL
;
757 if (SubEntry
->Enabled
== TRUE
) {
758 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
760 Entry
->me
.SubScreen
= SubScreen
;
761 } // VOID AddSubmenu()
763 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
764 // that entry. The calling function is then responsible for adding the entry to the
766 static LOADER_ENTRY
* AddStanzaEntries(REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
770 BOOLEAN DefaultsSet
= FALSE
, AddedSubmenu
= FALSE
;
771 REFIT_VOLUME
*CurrentVolume
= Volume
;
773 // prepare the menu entry
774 Entry
= InitializeLoaderEntry(NULL
);
778 Entry
->Title
= StrDuplicate(Title
);
779 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
780 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
782 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
783 Entry
->VolName
= CurrentVolume
->VolName
;
785 // Parse the config file to add options for a single stanza, terminating when the token
786 // is "}" or when the end of file is reached.
787 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
788 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
789 Entry
->LoaderPath
= StrDuplicate(TokenList
[1]);
790 Entry
->DevicePath
= FileDevicePath(CurrentVolume
->DeviceHandle
, Entry
->LoaderPath
);
791 SetLoaderDefaults(Entry
, TokenList
[1], CurrentVolume
);
792 MyFreePool(Entry
->LoadOptions
);
793 Entry
->LoadOptions
= NULL
; // Discard default options, if any
796 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
797 if (FindVolume(&CurrentVolume
, TokenList
[1])) {
798 MyFreePool(Entry
->me
.Title
);
799 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
800 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
801 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
802 Entry
->VolName
= CurrentVolume
->VolName
;
805 } else if ((StriCmp(TokenList
[0], L
"icon") == 0) && (TokenCount
> 1)) {
806 MyFreePool(Entry
->me
.Image
);
807 Entry
->me
.Image
= egLoadIcon(CurrentVolume
->RootDir
, TokenList
[1], GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
808 if (Entry
->me
.Image
== NULL
) {
809 Entry
->me
.Image
= DummyImage(GlobalConfig
.IconSizes
[ICON_SIZE_BIG
]);
812 } else if ((StriCmp(TokenList
[0], L
"initrd") == 0) && (TokenCount
> 1)) {
813 MyFreePool(Entry
->InitrdPath
);
814 Entry
->InitrdPath
= StrDuplicate(TokenList
[1]);
816 } else if ((StriCmp(TokenList
[0], L
"options") == 0) && (TokenCount
> 1)) {
817 MyFreePool(Entry
->LoadOptions
);
818 Entry
->LoadOptions
= StrDuplicate(TokenList
[1]);
820 } else if ((StriCmp(TokenList
[0], L
"ostype") == 0) && (TokenCount
> 1)) {
821 if (TokenCount
> 1) {
822 Entry
->OSType
= TokenList
[1][0];
825 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
826 Entry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
828 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
829 Entry
->Enabled
= FALSE
;
831 } else if ((StriCmp(TokenList
[0], L
"submenuentry") == 0) && (TokenCount
> 1)) {
832 AddSubmenu(Entry
, File
, CurrentVolume
, TokenList
[1]);
835 } // set options to pass to the loader program
836 FreeTokenLine(&TokenList
, &TokenCount
);
840 AddMenuEntry(Entry
->me
.SubScreen
, &MenuEntryReturn
);
842 if (Entry
->InitrdPath
) {
843 MergeStrings(&Entry
->LoadOptions
, L
"initrd=", L
' ');
844 MergeStrings(&Entry
->LoadOptions
, Entry
->InitrdPath
, 0);
845 MyFreePool(Entry
->InitrdPath
);
846 Entry
->InitrdPath
= NULL
;
850 SetLoaderDefaults(Entry
, L
"\\EFI\\BOOT\\nemo.efi", CurrentVolume
); // user included no "loader" line; use bogus one
853 } // static VOID AddStanzaEntries()
855 // Read the user-configured menu entries from refind.conf and add or delete
856 // entries based on the contents of that file....
857 VOID
ScanUserConfigured(CHAR16
*FileName
)
861 REFIT_VOLUME
*Volume
;
863 CHAR16
*Title
= NULL
;
864 UINTN TokenCount
, size
;
867 if (FileExists(SelfDir
, FileName
)) {
868 Status
= ReadFile(SelfDir
, FileName
, &File
, &size
);
869 if (EFI_ERROR(Status
))
874 while ((TokenCount
= ReadTokenLine(&File
, &TokenList
)) > 0) {
875 if ((StriCmp(TokenList
[0], L
"menuentry") == 0) && (TokenCount
> 1)) {
876 Title
= StrDuplicate(TokenList
[1]);
877 Entry
= AddStanzaEntries(&File
, Volume
, TokenList
[1]);
878 if (Entry
->Enabled
) {
879 if (Entry
->me
.SubScreen
== NULL
)
880 GenerateSubScreen(Entry
, Volume
);
881 AddPreparedLoaderEntry(Entry
);
887 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
888 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
889 if (StriCmp(TokenList
[1], FileName
) != 0) {
890 ScanUserConfigured(TokenList
[1]);
894 FreeTokenLine(&TokenList
, &TokenCount
);
897 } // VOID ScanUserConfigured()
899 // Create an options file based on /etc/fstab. The resulting file has two options
900 // lines, one of which boots the system with "ro root={rootfs}" and the other of
901 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
902 // filesystem identifier associated with the "/" line in /etc/fstab.
903 static REFIT_FILE
* GenerateOptionsFromEtcFstab(REFIT_VOLUME
*Volume
) {
905 REFIT_FILE
*Options
= NULL
, *Fstab
= NULL
;
907 CHAR16
**TokenList
, *Line
, Root
[100];
909 if (FileExists(Volume
->RootDir
, L
"\\etc\\fstab")) {
910 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
911 Fstab
= AllocateZeroPool(sizeof(REFIT_FILE
));
912 Status
= ReadFile(Volume
->RootDir
, L
"\\etc\\fstab", Fstab
, &i
);
913 if (CheckError(Status
, L
"while reading /etc/fstab")) {
920 } else { // File read; locate root fs and create entries
921 Options
->Encoding
= ENCODING_UTF16_LE
;
922 while ((TokenCount
= ReadTokenLine(Fstab
, &TokenList
)) > 0) {
923 if (TokenCount
> 2) {
925 if (StriCmp(TokenList
[1], L
"\\") == 0) {
926 SPrint(Root
, 99, L
"%s", TokenList
[0]);
927 } else if (StriCmp(TokenList
[2], L
"\\") == 0) {
928 SPrint(Root
, 99, L
"%s=%s", TokenList
[0], TokenList
[1]);
929 } // if/elseif/elseif
930 if (Root
[0] != L
'\0') {
931 for (i
= 0; i
< StrLen(Root
); i
++)
934 Line
= PoolPrint(L
"\"Boot with normal options\" \"ro root=%s\"\n", Root
);
935 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
937 Line
= PoolPrint(L
"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root
);
938 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
939 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
942 FreeTokenLine(&TokenList
, &TokenCount
);
945 Options
->Current8Ptr
= (CHAR8
*)Options
->Buffer
;
946 Options
->End8Ptr
= Options
->Current8Ptr
+ Options
->BufferSize
;
947 Options
->Current16Ptr
= (CHAR16
*)Options
->Buffer
;
948 Options
->End16Ptr
= Options
->Current16Ptr
+ (Options
->BufferSize
>> 1);
950 MyFreePool(Fstab
->Buffer
);
952 } // if/else file read error
953 } // if /etc/fstab exists
955 } // GenerateOptionsFromEtcFstab()
958 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
959 // and Volume variables identify the location of the options file, but not its name --
960 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
961 // file in the target directory, and this function finds the file with a name in the
962 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
963 // directory and loads it. This function tries multiple files because I originally
964 // used the filename linux.conf, but close on the heels of that decision, the Linux
965 // kernel developers decided to use that name for a similar purpose, but with a
966 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
967 // but I want a migration period in which both names are used.
968 // If a rEFInd options file can't be found, try to generate minimal options from
969 // /etc/fstab on the same volume as the kernel. This typically works only if the
970 // kernel is being read from the Linux root filesystem.
972 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
974 REFIT_FILE
* ReadLinuxOptionsFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
975 CHAR16
*OptionsFilename
, *FullFilename
;
976 BOOLEAN GoOn
= TRUE
, FileFound
= FALSE
;
978 REFIT_FILE
*File
= NULL
;
982 OptionsFilename
= FindCommaDelimited(LINUX_OPTIONS_FILENAMES
, i
++);
983 FullFilename
= FindPath(LoaderPath
);
984 if ((OptionsFilename
!= NULL
) && (FullFilename
!= NULL
)) {
985 MergeStrings(&FullFilename
, OptionsFilename
, '\\');
986 if (FileExists(Volume
->RootDir
, FullFilename
)) {
987 File
= AllocateZeroPool(sizeof(REFIT_FILE
));
988 Status
= ReadFile(Volume
->RootDir
, FullFilename
, File
, &size
);
989 if (CheckError(Status
, L
"while loading the Linux options file")) {
998 } else { // a filename string is NULL
1002 File
= GenerateOptionsFromEtcFstab(Volume
);
1003 MyFreePool(OptionsFilename
);
1004 MyFreePool(FullFilename
);
1005 OptionsFilename
= FullFilename
= NULL
;
1008 } // static REFIT_FILE * ReadLinuxOptionsFile()
1010 // Retrieve a single line of options from a Linux kernel options file
1011 CHAR16
* GetFirstOptionsFromFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
1013 CHAR16
*Options
= NULL
;
1017 File
= ReadLinuxOptionsFile(LoaderPath
, Volume
);
1019 TokenCount
= ReadTokenLine(File
, &TokenList
);
1021 Options
= StrDuplicate(TokenList
[1]);
1022 FreeTokenLine(&TokenList
, &TokenCount
);
1026 } // static CHAR16 * GetOptionsFile()