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)) {
337 Print(L
"Entering add mode in HandleStrings for '%s'\n", TokenList
[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 // Sets the default boot loader IF the current time is within the bounds
377 // defined by the third and fourth tokens in the TokenList.
378 static VOID
SetDefaultByTime(IN CHAR16
**TokenList
, OUT CHAR16
**Default
) {
380 EFI_TIME CurrentTime
;
381 UINTN StartTime
= LAST_MINUTE
+ 1, EndTime
= LAST_MINUTE
+ 1, Now
;
382 BOOLEAN SetIt
= FALSE
;
384 StartTime
= HandleTime(TokenList
[2]);
385 EndTime
= HandleTime(TokenList
[3]);
387 if ((StartTime
<= LAST_MINUTE
) && (EndTime
<= LAST_MINUTE
)) {
388 Status
= refit_call2_wrapper(GetTime
, &CurrentTime
, NULL
);
389 if (Status
!= EFI_SUCCESS
)
391 Now
= CurrentTime
.Hour
* 60 + CurrentTime
.Minute
;
393 if (Now
> LAST_MINUTE
) { // Shouldn't happen; just being paranoid
394 Print(L
"Warning: Impossible system time: %d:%d\n", CurrentTime
.Hour
, CurrentTime
.Minute
);
396 } // if impossible time
398 if (StartTime
< EndTime
) { // Time range does NOT cross midnight
399 if ((Now
>= StartTime
) && (Now
<= EndTime
))
401 } else { // Time range DOES cross midnight
402 if ((Now
>= StartTime
) && (Now
<= EndTime
))
404 } // if/else time range crosses midnight
407 MyFreePool(*Default
);
408 *Default
= StrDuplicate(TokenList
[1]);
410 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
411 } // VOID SetDefaultByTime()
414 VOID
ReadConfig(CHAR16
*FileName
)
420 CHAR16
*TempStr
= NULL
;
423 // Set a few defaults only if we're loading the default file.
424 if (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0) {
425 MyFreePool(GlobalConfig
.AlsoScan
);
426 GlobalConfig
.AlsoScan
= StrDuplicate(ALSO_SCAN_DIRS
);
427 MyFreePool(GlobalConfig
.DontScanDirs
);
429 if (SelfVolume
->VolName
) {
430 TempStr
= SelfVolume
->VolName
? StrDuplicate(SelfVolume
->VolName
) : NULL
;
432 TempStr
= AllocateZeroPool(256 * sizeof(CHAR16
));
434 SPrint(TempStr
, 255, L
"fs%d", SelfVolume
->VolNumber
);
437 MergeStrings(&TempStr
, SelfDirPath
, L
':');
438 MergeStrings(&TempStr
, MEMTEST_LOCATIONS
, L
',');
439 GlobalConfig
.DontScanDirs
= TempStr
;
440 MyFreePool(GlobalConfig
.DontScanFiles
);
441 GlobalConfig
.DontScanFiles
= StrDuplicate(DONT_SCAN_FILES
);
442 MergeStrings(&(GlobalConfig
.DontScanFiles
), MOK_NAMES
, L
',');
443 MyFreePool(GlobalConfig
.DontScanVolumes
);
444 GlobalConfig
.DontScanVolumes
= StrDuplicate(DONT_SCAN_VOLUMES
);
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
"scan_driver_dirs") == 0) {
522 HandleStrings(TokenList
, TokenCount
, &(GlobalConfig
.DriverDirs
));
524 } else if (StriCmp(TokenList
[0], L
"showtools") == 0) {
525 SetMem(GlobalConfig
.ShowTools
, NUM_TOOLS
* sizeof(UINTN
), 0);
526 for (i
= 1; (i
< TokenCount
) && (i
< NUM_TOOLS
); i
++) {
527 FlagName
= TokenList
[i
];
528 if (StriCmp(FlagName
, L
"shell") == 0) {
529 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHELL
;
530 } else if (StriCmp(FlagName
, L
"gptsync") == 0) {
531 GlobalConfig
.ShowTools
[i
- 1] = TAG_GPTSYNC
;
532 } else if (StriCmp(FlagName
, L
"about") == 0) {
533 GlobalConfig
.ShowTools
[i
- 1] = TAG_ABOUT
;
534 } else if (StriCmp(FlagName
, L
"exit") == 0) {
535 GlobalConfig
.ShowTools
[i
- 1] = TAG_EXIT
;
536 } else if (StriCmp(FlagName
, L
"reboot") == 0) {
537 GlobalConfig
.ShowTools
[i
- 1] = TAG_REBOOT
;
538 } else if (StriCmp(FlagName
, L
"shutdown") == 0) {
539 GlobalConfig
.ShowTools
[i
- 1] = TAG_SHUTDOWN
;
540 } else if (StriCmp(FlagName
, L
"apple_recovery") == 0) {
541 GlobalConfig
.ShowTools
[i
- 1] = TAG_APPLE_RECOVERY
;
542 } else if (StriCmp(FlagName
, L
"mok_tool") == 0) {
543 GlobalConfig
.ShowTools
[i
- 1] = TAG_MOK_TOOL
;
544 } else if (StriCmp(FlagName
, L
"firmware") == 0) {
545 GlobalConfig
.ShowTools
[i
- 1] = TAG_FIRMWARE
;
546 } else if ((StriCmp(FlagName
, L
"memtest86") == 0) || (StriCmp(FlagName
, L
"memtest") == 0)) {
547 GlobalConfig
.ShowTools
[i
- 1] = TAG_MEMTEST
;
549 Print(L
" unknown showtools flag: '%s'\n", FlagName
);
551 } // showtools options
553 } else if (StriCmp(TokenList
[0], L
"banner") == 0) {
554 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.BannerFileName
));
556 } else if (StriCmp(TokenList
[0], L
"selection_small") == 0) {
557 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionSmallFileName
));
559 } else if (StriCmp(TokenList
[0], L
"selection_big") == 0) {
560 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.SelectionBigFileName
));
562 } else if (StriCmp(TokenList
[0], L
"default_selection") == 0) {
563 if (TokenCount
== 4) {
564 SetDefaultByTime(TokenList
, &(GlobalConfig
.DefaultSelection
));
566 HandleString(TokenList
, TokenCount
, &(GlobalConfig
.DefaultSelection
));
569 } else if (StriCmp(TokenList
[0], L
"textonly") == 0) {
570 if ((TokenCount
>= 2) && (StriCmp(TokenList
[1], L
"0") == 0)) {
571 GlobalConfig
.TextOnly
= FALSE
;
573 GlobalConfig
.TextOnly
= TRUE
;
576 } else if (StriCmp(TokenList
[0], L
"textmode") == 0) {
577 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.RequestedTextMode
));
579 } else if ((StriCmp(TokenList
[0], L
"resolution") == 0) && ((TokenCount
== 2) || (TokenCount
== 3))) {
580 GlobalConfig
.RequestedScreenWidth
= Atoi(TokenList
[1]);
582 GlobalConfig
.RequestedScreenHeight
= Atoi(TokenList
[2]);
584 GlobalConfig
.RequestedScreenHeight
= 0;
586 } else if (StriCmp(TokenList
[0], L
"screensaver") == 0) {
587 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.ScreensaverTime
));
589 } else if (StriCmp(TokenList
[0], L
"use_graphics_for") == 0) {
590 if ((TokenCount
== 2) || ((TokenCount
> 2) && (StriCmp(TokenList
[1], L
"+") != 0)))
591 GlobalConfig
.GraphicsFor
= 0;
592 for (i
= 1; i
< TokenCount
; i
++) {
593 if (StriCmp(TokenList
[i
], L
"osx") == 0) {
594 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_OSX
;
595 } else if (StriCmp(TokenList
[i
], L
"linux") == 0) {
596 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_LINUX
;
597 } else if (StriCmp(TokenList
[i
], L
"elilo") == 0) {
598 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_ELILO
;
599 } else if (StriCmp(TokenList
[i
], L
"grub") == 0) {
600 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_GRUB
;
601 } else if (StriCmp(TokenList
[i
], L
"windows") == 0) {
602 GlobalConfig
.GraphicsFor
|= GRAPHICS_FOR_WINDOWS
;
604 } // for (graphics_on tokens)
606 } else if ((StriCmp(TokenList
[0], L
"font") == 0) && (TokenCount
== 2)) {
607 egLoadFont(TokenList
[1]);
609 } else if (StriCmp(TokenList
[0], L
"scan_all_linux_kernels") == 0) {
610 if ((TokenCount
>= 2) && (StriCmp(TokenList
[1], L
"0") == 0)) {
611 GlobalConfig
.ScanAllLinux
= FALSE
;
613 GlobalConfig
.ScanAllLinux
= TRUE
;
616 } else if (StriCmp(TokenList
[0], L
"max_tags") == 0) {
617 HandleInt(TokenList
, TokenCount
, &(GlobalConfig
.MaxTags
));
619 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
620 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
621 if (StriCmp(TokenList
[1], FileName
) != 0) {
622 ReadConfig(TokenList
[1]);
627 FreeTokenLine(&TokenList
, &TokenCount
);
629 Print(L
"also_scan_dirs = '%s'\n", GlobalConfig
.AlsoScan
);
630 Print(L
"dont_scan_dirs = '%s'\n", GlobalConfig
.DontScanDirs
);
631 Print(L
"dont_scan_files = '%s'\n", GlobalConfig
.DontScanFiles
);
632 Print(L
"scan_driver_dirs = '%s'\n", GlobalConfig
.DriverDirs
);
633 Print(L
"use_graphics_for = %d\n", GlobalConfig
.GraphicsFor
);
635 MyFreePool(File
.Buffer
);
636 } /* VOID ReadConfig() */
638 // Finds a volume with the specified Identifier (a volume label or a number
639 // followed by a colon, for the moment). If found, sets *Volume to point to
640 // that volume. If not, leaves it unchanged.
641 // Returns TRUE if a match was found, FALSE if not.
642 static BOOLEAN
FindVolume(REFIT_VOLUME
**Volume
, CHAR16
*Identifier
) {
643 UINTN i
= 0, CountedVolumes
= 0;
645 BOOLEAN Found
= FALSE
;
647 if ((StrLen(Identifier
) >= 2) && (Identifier
[StrLen(Identifier
) - 1] == L
':') &&
648 (Identifier
[0] >= L
'0') && (Identifier
[0] <= L
'9')) {
649 Number
= (INTN
) Atoi(Identifier
);
651 while ((i
< VolumesCount
) && (!Found
)) {
652 if (Number
>= 0) { // User specified a volume by number
653 if (Volumes
[i
]->IsReadable
) {
654 if (CountedVolumes
== Number
) {
655 *Volume
= Volumes
[i
];
660 } else { // User specified a volume by label
661 if (StriCmp(Identifier
, Volumes
[i
]->VolName
) == 0) {
662 *Volume
= Volumes
[i
];
669 } // static VOID FindVolume()
671 static VOID
AddSubmenu(LOADER_ENTRY
*Entry
, REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
672 REFIT_MENU_SCREEN
*SubScreen
;
673 LOADER_ENTRY
*SubEntry
;
677 SubScreen
= InitializeSubScreen(Entry
);
679 // Set defaults for the new entry; will be modified based on lines read from the config. file....
680 SubEntry
= InitializeLoaderEntry(Entry
);
682 if ((SubEntry
== NULL
) || (SubScreen
== NULL
))
684 SubEntry
->me
.Title
= StrDuplicate(Title
);
686 while (((TokenCount
= ReadTokenLine(File
, &TokenList
)) > 0) && (StriCmp(TokenList
[0], L
"}") != 0)) {
688 if ((StriCmp(TokenList
[0], L
"loader") == 0) && (TokenCount
> 1)) { // set the boot loader filename
689 MyFreePool(SubEntry
->LoaderPath
);
690 SubEntry
->LoaderPath
= StrDuplicate(TokenList
[1]);
691 SubEntry
->DevicePath
= FileDevicePath(Volume
->DeviceHandle
, SubEntry
->LoaderPath
);
693 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
694 if (FindVolume(&Volume
, TokenList
[1])) {
695 MyFreePool(SubEntry
->me
.Title
);
696 SubEntry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
697 SPrint(SubEntry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", Volume
->VolName
);
698 SubEntry
->me
.BadgeImage
= Volume
->VolBadgeImage
;
699 SubEntry
->VolName
= Volume
->VolName
;
702 } else if (StriCmp(TokenList
[0], L
"initrd") == 0) {
703 MyFreePool(SubEntry
->InitrdPath
);
704 SubEntry
->InitrdPath
= NULL
;
705 if (TokenCount
> 1) {
706 SubEntry
->InitrdPath
= StrDuplicate(TokenList
[1]);
709 } else if (StriCmp(TokenList
[0], L
"options") == 0) {
710 MyFreePool(SubEntry
->LoadOptions
);
711 SubEntry
->LoadOptions
= NULL
;
712 if (TokenCount
> 1) {
713 SubEntry
->LoadOptions
= StrDuplicate(TokenList
[1]);
716 } else if ((StriCmp(TokenList
[0], L
"add_options") == 0) && (TokenCount
> 1)) {
717 MergeStrings(&SubEntry
->LoadOptions
, TokenList
[1], L
' ');
719 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
720 SubEntry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
722 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
723 SubEntry
->Enabled
= FALSE
;
726 FreeTokenLine(&TokenList
, &TokenCount
);
729 if (SubEntry
->InitrdPath
!= NULL
) {
730 MergeStrings(&SubEntry
->LoadOptions
, L
"initrd=", L
' ');
731 MergeStrings(&SubEntry
->LoadOptions
, SubEntry
->InitrdPath
, 0);
732 MyFreePool(SubEntry
->InitrdPath
);
733 SubEntry
->InitrdPath
= NULL
;
735 if (SubEntry
->Enabled
== TRUE
) {
736 AddMenuEntry(SubScreen
, (REFIT_MENU_ENTRY
*)SubEntry
);
738 Entry
->me
.SubScreen
= SubScreen
;
739 } // VOID AddSubmenu()
741 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
742 // that entry. The calling function is then responsible for adding the entry to the
744 static LOADER_ENTRY
* AddStanzaEntries(REFIT_FILE
*File
, REFIT_VOLUME
*Volume
, CHAR16
*Title
) {
748 BOOLEAN DefaultsSet
= FALSE
, AddedSubmenu
= FALSE
;
749 REFIT_VOLUME
*CurrentVolume
= Volume
;
751 // prepare the menu entry
752 Entry
= InitializeLoaderEntry(NULL
);
756 Entry
->Title
= StrDuplicate(Title
);
757 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
758 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
760 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
761 Entry
->VolName
= CurrentVolume
->VolName
;
763 // Parse the config file to add options for a single stanza, terminating when the token
764 // is "}" or when the end of file is reached.
765 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 Entry
->LoaderPath
= StrDuplicate(TokenList
[1]);
768 Entry
->DevicePath
= FileDevicePath(CurrentVolume
->DeviceHandle
, Entry
->LoaderPath
);
769 SetLoaderDefaults(Entry
, TokenList
[1], CurrentVolume
);
770 MyFreePool(Entry
->LoadOptions
);
771 Entry
->LoadOptions
= NULL
; // Discard default options, if any
774 } else if ((StriCmp(TokenList
[0], L
"volume") == 0) && (TokenCount
> 1)) {
775 if (FindVolume(&CurrentVolume
, TokenList
[1])) {
776 MyFreePool(Entry
->me
.Title
);
777 Entry
->me
.Title
= AllocateZeroPool(256 * sizeof(CHAR16
));
778 SPrint(Entry
->me
.Title
, 255, L
"Boot %s from %s", (Title
!= NULL
) ? Title
: L
"Unknown", CurrentVolume
->VolName
);
779 Entry
->me
.BadgeImage
= CurrentVolume
->VolBadgeImage
;
780 Entry
->VolName
= CurrentVolume
->VolName
;
783 } else if ((StriCmp(TokenList
[0], L
"icon") == 0) && (TokenCount
> 1)) {
784 MyFreePool(Entry
->me
.Image
);
785 Entry
->me
.Image
= egLoadIcon(CurrentVolume
->RootDir
, TokenList
[1], 128);
786 if (Entry
->me
.Image
== NULL
) {
787 Entry
->me
.Image
= DummyImage(128);
790 } else if ((StriCmp(TokenList
[0], L
"initrd") == 0) && (TokenCount
> 1)) {
791 MyFreePool(Entry
->InitrdPath
);
792 Entry
->InitrdPath
= StrDuplicate(TokenList
[1]);
794 } else if ((StriCmp(TokenList
[0], L
"options") == 0) && (TokenCount
> 1)) {
795 MyFreePool(Entry
->LoadOptions
);
796 Entry
->LoadOptions
= StrDuplicate(TokenList
[1]);
798 } else if ((StriCmp(TokenList
[0], L
"ostype") == 0) && (TokenCount
> 1)) {
799 if (TokenCount
> 1) {
800 Entry
->OSType
= TokenList
[1][0];
803 } else if ((StriCmp(TokenList
[0], L
"graphics") == 0) && (TokenCount
> 1)) {
804 Entry
->UseGraphicsMode
= (StriCmp(TokenList
[1], L
"on") == 0);
806 } else if (StriCmp(TokenList
[0], L
"disabled") == 0) {
807 Entry
->Enabled
= FALSE
;
809 } else if ((StriCmp(TokenList
[0], L
"submenuentry") == 0) && (TokenCount
> 1)) {
810 AddSubmenu(Entry
, File
, CurrentVolume
, TokenList
[1]);
813 } // set options to pass to the loader program
814 FreeTokenLine(&TokenList
, &TokenCount
);
818 AddMenuEntry(Entry
->me
.SubScreen
, &MenuEntryReturn
);
820 if (Entry
->InitrdPath
) {
821 MergeStrings(&Entry
->LoadOptions
, L
"initrd=", L
' ');
822 MergeStrings(&Entry
->LoadOptions
, Entry
->InitrdPath
, 0);
823 MyFreePool(Entry
->InitrdPath
);
824 Entry
->InitrdPath
= NULL
;
828 SetLoaderDefaults(Entry
, L
"\\EFI\\BOOT\\nemo.efi", CurrentVolume
); // user included no "loader" line; use bogus one
831 } // static VOID AddStanzaEntries()
833 // Read the user-configured menu entries from refind.conf and add or delete
834 // entries based on the contents of that file....
835 VOID
ScanUserConfigured(CHAR16
*FileName
)
839 REFIT_VOLUME
*Volume
;
841 CHAR16
*Title
= NULL
;
842 UINTN TokenCount
, size
;
845 if (FileExists(SelfDir
, FileName
)) {
846 Status
= ReadFile(SelfDir
, FileName
, &File
, &size
);
847 if (EFI_ERROR(Status
))
852 while ((TokenCount
= ReadTokenLine(&File
, &TokenList
)) > 0) {
853 if ((StriCmp(TokenList
[0], L
"menuentry") == 0) && (TokenCount
> 1)) {
854 Title
= StrDuplicate(TokenList
[1]);
855 Entry
= AddStanzaEntries(&File
, Volume
, TokenList
[1]);
856 if (Entry
->Enabled
) {
857 if (Entry
->me
.SubScreen
== NULL
)
858 GenerateSubScreen(Entry
, Volume
);
859 AddPreparedLoaderEntry(Entry
);
865 } else if ((StriCmp(TokenList
[0], L
"include") == 0) && (TokenCount
== 2) &&
866 (StriCmp(FileName
, GlobalConfig
.ConfigFilename
) == 0)) {
867 if (StriCmp(TokenList
[1], FileName
) != 0) {
868 ScanUserConfigured(TokenList
[1]);
872 FreeTokenLine(&TokenList
, &TokenCount
);
875 } // VOID ScanUserConfigured()
877 // Create an options file based on /etc/fstab. The resulting file has two options
878 // lines, one of which boots the system with "ro root={rootfs}" and the other of
879 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
880 // filesystem identifier associated with the "/" line in /etc/fstab.
881 static REFIT_FILE
* GenerateOptionsFromEtcFstab(REFIT_VOLUME
*Volume
) {
883 REFIT_FILE
*Options
= NULL
, *Fstab
= NULL
;
885 CHAR16
**TokenList
, *Line
, Root
[100];
887 if (FileExists(Volume
->RootDir
, L
"\\etc\\fstab")) {
888 Options
= AllocateZeroPool(sizeof(REFIT_FILE
));
889 Fstab
= AllocateZeroPool(sizeof(REFIT_FILE
));
890 Status
= ReadFile(Volume
->RootDir
, L
"\\etc\\fstab", Fstab
, &i
);
891 if (CheckError(Status
, L
"while reading /etc/fstab")) {
898 } else { // File read; locate root fs and create entries
899 Options
->Encoding
= ENCODING_UTF16_LE
;
900 while ((TokenCount
= ReadTokenLine(Fstab
, &TokenList
)) > 0) {
901 if (TokenCount
> 2) {
903 if (StriCmp(TokenList
[1], L
"\\") == 0) {
904 SPrint(Root
, 99, L
"%s", TokenList
[0]);
905 } else if (StriCmp(TokenList
[2], L
"\\") == 0) {
906 SPrint(Root
, 99, L
"%s=%s", TokenList
[0], TokenList
[1]);
907 } // if/elseif/elseif
908 if (Root
[0] != L
'\0') {
909 for (i
= 0; i
< StrLen(Root
); i
++)
912 Line
= PoolPrint(L
"\"Boot with normal options\" \"ro root=%s\"\n", Root
);
913 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
915 Line
= PoolPrint(L
"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root
);
916 MergeStrings((CHAR16
**) &(Options
->Buffer
), Line
, 0);
917 Options
->BufferSize
= StrLen((CHAR16
*) Options
->Buffer
) * sizeof(CHAR16
);
920 FreeTokenLine(&TokenList
, &TokenCount
);
923 Options
->Current8Ptr
= (CHAR8
*)Options
->Buffer
;
924 Options
->End8Ptr
= Options
->Current8Ptr
+ Options
->BufferSize
;
925 Options
->Current16Ptr
= (CHAR16
*)Options
->Buffer
;
926 Options
->End16Ptr
= Options
->Current16Ptr
+ (Options
->BufferSize
>> 1);
928 MyFreePool(Fstab
->Buffer
);
930 } // if/else file read error
931 } // if /etc/fstab exists
933 } // GenerateOptionsFromEtcFstab()
936 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
937 // and Volume variables identify the location of the options file, but not its name --
938 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
939 // file in the target directory, and this function finds the file with a name in the
940 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
941 // directory and loads it. This function tries multiple files because I originally
942 // used the filename linux.conf, but close on the heels of that decision, the Linux
943 // kernel developers decided to use that name for a similar purpose, but with a
944 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
945 // but I want a migration period in which both names are used.
946 // If a rEFInd options file can't be found, try to generate minimal options from
947 // /etc/fstab on the same volume as the kernel. This typically works only if the
948 // kernel is being read from the Linux root filesystem.
950 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
952 REFIT_FILE
* ReadLinuxOptionsFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
953 CHAR16
*OptionsFilename
, *FullFilename
;
954 BOOLEAN GoOn
= TRUE
, FileFound
= FALSE
;
956 REFIT_FILE
*File
= NULL
;
960 OptionsFilename
= FindCommaDelimited(LINUX_OPTIONS_FILENAMES
, i
++);
961 FullFilename
= FindPath(LoaderPath
);
962 if ((OptionsFilename
!= NULL
) && (FullFilename
!= NULL
)) {
963 MergeStrings(&FullFilename
, OptionsFilename
, '\\');
964 if (FileExists(Volume
->RootDir
, FullFilename
)) {
965 File
= AllocateZeroPool(sizeof(REFIT_FILE
));
966 Status
= ReadFile(Volume
->RootDir
, FullFilename
, File
, &size
);
967 if (CheckError(Status
, L
"while loading the Linux options file")) {
976 } else { // a filename string is NULL
980 File
= GenerateOptionsFromEtcFstab(Volume
);
981 MyFreePool(OptionsFilename
);
982 MyFreePool(FullFilename
);
983 OptionsFilename
= FullFilename
= NULL
;
986 } // static REFIT_FILE * ReadLinuxOptionsFile()
988 // Retrieve a single line of options from a Linux kernel options file
989 CHAR16
* GetFirstOptionsFromFile(IN CHAR16
*LoaderPath
, IN REFIT_VOLUME
*Volume
) {
991 CHAR16
*Options
= NULL
;
995 File
= ReadLinuxOptionsFile(LoaderPath
, Volume
);
997 TokenCount
= ReadTokenLine(File
, &TokenList
);
999 Options
= StrDuplicate(TokenList
[1]);
1000 FreeTokenLine(&TokenList
, &TokenCount
);
1004 } // static CHAR16 * GetOptionsFile()