]> code.delx.au - refind/blob - refind/config.c
Version 0.9.2 release
[refind] / refind / config.c
1 /*
2 * refind/config.c
3 * Configuration file functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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.
23 *
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.
35 */
36
37 /*
38 * Modifications copyright (c) 2012-2015 Roderick W. Smith
39 *
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.
43 *
44 */
45
46 #include "global.h"
47 #include "lib.h"
48 #include "icns.h"
49 #include "menu.h"
50 #include "config.h"
51 #include "screen.h"
52 #include "../include/refit_call_wrapper.h"
53 #include "../mok/mok.h"
54
55 // constants
56
57 #define LINUX_OPTIONS_FILENAMES L"refind_linux.conf,refind-linux.conf"
58 #define MAXCONFIGFILESIZE (128*1024)
59
60 #define ENCODING_ISO8859_1 (0)
61 #define ENCODING_UTF8 (1)
62 #define ENCODING_UTF16_LE (2)
63
64 #define GetTime ST->RuntimeServices->GetTime
65 #define LAST_MINUTE 1439 /* Last minute of a day */
66
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 };
69
70 //
71 // read a file into a buffer
72 //
73
74 EFI_STATUS ReadFile(IN EFI_FILE_HANDLE BaseDir, IN CHAR16 *FileName, IN OUT REFIT_FILE *File, OUT UINTN *size)
75 {
76 EFI_STATUS Status;
77 EFI_FILE_HANDLE FileHandle;
78 EFI_FILE_INFO *FileInfo;
79 UINT64 ReadSize;
80 CHAR16 Message[256];
81
82 File->Buffer = NULL;
83 File->BufferSize = 0;
84
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))
89 return Status;
90
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;
96 }
97 ReadSize = FileInfo->FileSize;
98 FreePool(FileInfo);
99
100 File->BufferSize = (UINTN)ReadSize;
101 File->Buffer = AllocatePool(File->BufferSize);
102 if (File->Buffer == NULL) {
103 size = 0;
104 return EFI_OUT_OF_RESOURCES;
105 } else {
106 *size = File->BufferSize;
107 } // if/else
108 Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &File->BufferSize, File->Buffer);
109 if (CheckError(Status, Message)) {
110 MyFreePool(File->Buffer);
111 File->Buffer = NULL;
112 refit_call1_wrapper(FileHandle->Close, FileHandle);
113 return Status;
114 }
115 Status = refit_call1_wrapper(FileHandle->Close, FileHandle);
116
117 // setup for reading
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);
122
123 // detect encoding
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) {
131 // BOM in UTF-8
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
136 }
137 // TODO: detect other encodings as they are implemented
138 }
139
140 return EFI_SUCCESS;
141 }
142
143 //
144 // get a single line of text from a file
145 //
146
147 static CHAR16 *ReadLine(REFIT_FILE *File)
148 {
149 CHAR16 *Line, *q;
150 UINTN LineLength;
151
152 if (File->Buffer == NULL)
153 return NULL;
154
155 if (File->Encoding == ENCODING_ISO8859_1 || File->Encoding == ENCODING_UTF8) {
156
157 CHAR8 *p, *LineStart, *LineEnd;
158
159 p = File->Current8Ptr;
160 if (p >= File->End8Ptr)
161 return NULL;
162
163 LineStart = p;
164 for (; p < File->End8Ptr; p++)
165 if (*p == 13 || *p == 10)
166 break;
167 LineEnd = p;
168 for (; p < File->End8Ptr; p++)
169 if (*p != 13 && *p != 10)
170 break;
171 File->Current8Ptr = p;
172
173 LineLength = (UINTN)(LineEnd - LineStart) + 1;
174 Line = AllocatePool(LineLength * sizeof(CHAR16));
175 if (Line == NULL)
176 return NULL;
177
178 q = Line;
179 if (File->Encoding == ENCODING_ISO8859_1) {
180 for (p = LineStart; p < LineEnd; )
181 *q++ = *p++;
182 } else if (File->Encoding == ENCODING_UTF8) {
183 // TODO: actually handle UTF-8
184 for (p = LineStart; p < LineEnd; )
185 *q++ = *p++;
186 }
187 *q = 0;
188
189 } else if (File->Encoding == ENCODING_UTF16_LE) {
190
191 CHAR16 *p, *LineStart, *LineEnd;
192
193 p = File->Current16Ptr;
194 if (p >= File->End16Ptr)
195 return NULL;
196
197 LineStart = p;
198 for (; p < File->End16Ptr; p++)
199 if (*p == 13 || *p == 10)
200 break;
201 LineEnd = p;
202 for (; p < File->End16Ptr; p++)
203 if (*p != 13 && *p != 10)
204 break;
205 File->Current16Ptr = p;
206
207 LineLength = (UINTN)(LineEnd - LineStart) + 1;
208 Line = AllocatePool(LineLength * sizeof(CHAR16));
209 if (Line == NULL)
210 return NULL;
211
212 for (p = LineStart, q = Line; p < LineEnd; )
213 *q++ = *p++;
214 *q = 0;
215
216 } else
217 return NULL; // unsupported encoding
218
219 return Line;
220 }
221
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;
227 CHAR16 *Temp = NULL;
228
229 if ((p == NULL) || (IsQuoted == NULL))
230 return FALSE;
231
232 if (*p == L'\0')
233 return FALSE;
234
235 if ((*p != ' ' && *p != '\t' && *p != '=' && *p != '#' && *p != ',') || *IsQuoted) {
236 MoreToRead = TRUE;
237 }
238 if (*p == L'"') {
239 if (p[1] == L'"') {
240 Temp = StrDuplicate(&p[1]);
241 if (Temp != NULL) {
242 StrCpy(p, Temp);
243 FreePool(Temp);
244 }
245 MoreToRead = TRUE;
246 } else {
247 *IsQuoted = !(*IsQuoted);
248 MoreToRead = FALSE;
249 } // if/else second character is a quote
250 } // if first character is a quote
251
252 return MoreToRead;
253 } // BOOLEAN KeepReading()
254
255 //
256 // get a line of tokens from a file
257 //
258 UINTN ReadTokenLine(IN REFIT_FILE *File, OUT CHAR16 ***TokenList)
259 {
260 BOOLEAN LineFinished, IsQuoted = FALSE;
261 CHAR16 *Line, *Token, *p;
262 UINTN TokenCount = 0;
263
264 *TokenList = NULL;
265
266 while (TokenCount == 0) {
267 Line = ReadLine(File);
268 if (Line == NULL)
269 return(0);
270 if (Line[0] == L'\0') {
271 MyFreePool(Line);
272 return(0);
273 } // if
274
275 p = Line;
276 LineFinished = FALSE;
277 while (!LineFinished) {
278 // skip whitespace & find start of token
279 while ((*p == ' ' || *p == '\t' || *p == '=' || *p == ',') && !IsQuoted)
280 p++;
281 if (*p == 0 || *p == '#')
282 break;
283
284 if (*p == '"') {
285 IsQuoted = !IsQuoted;
286 p++;
287 } // if
288 Token = p;
289
290 // find end of token
291 while (KeepReading(p, &IsQuoted)) {
292 if ((*p == L'/') && !IsQuoted) // Switch Unix-style to DOS-style directory separators
293 *p = L'\\';
294 p++;
295 } // while
296 if (*p == L'\0' || *p == L'#')
297 LineFinished = TRUE;
298 *p++ = 0;
299
300 AddListElement((VOID ***)TokenList, &TokenCount, (VOID *)StrDuplicate(Token));
301 }
302
303 FreePool(Line);
304 }
305 return (TokenCount);
306 } /* ReadTokenLine() */
307
308 VOID FreeTokenLine(IN OUT CHAR16 ***TokenList, IN OUT UINTN *TokenCount)
309 {
310 // TODO: also free the items
311 FreeList((VOID ***)TokenList, TokenCount);
312 }
313
314 // handle a parameter with a single integer argument
315 static VOID HandleInt(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT UINTN *Value)
316 {
317 if (TokenCount == 2) {
318 if (StrCmp(TokenList[1], L"-1") == 0)
319 *Value = -1;
320 else
321 *Value = Atoi(TokenList[1]);
322 }
323 }
324
325 // handle a parameter with a single string argument
326 static VOID HandleString(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT CHAR16 **Target) {
327 if ((TokenCount == 2) && Target) {
328 if ((StrLen(TokenList[1]) > 1) && (TokenList[1][0] == L'+') &&
329 ((TokenList[1][1] == L',') || (TokenList[1][1] == L' '))) {
330 if (*Target) {
331 MergeStrings(Target, TokenList[1] + 2, L',');
332 } else {
333 *Target = StrDuplicate(TokenList[1] + 2);
334 } // if/else
335 } else {
336 MyFreePool(*Target);
337 *Target = StrDuplicate(TokenList[1]);
338 } // if/else
339 } // if
340 } // static VOID HandleString()
341
342 // Handle a parameter with a series of string arguments, to replace or be added to a
343 // comma-delimited list. Passes each token through the CleanUpPathNameSlashes() function
344 // to ensure consistency in subsequent comparisons of filenames. If the first
345 // non-keyword token is "+", the list is added to the existing target string; otherwise,
346 // the tokens replace the current string.
347 static VOID HandleStrings(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT CHAR16 **Target) {
348 UINTN i;
349 BOOLEAN AddMode = FALSE;
350
351 if ((TokenCount > 2) && (StrCmp(TokenList[1], L"+") == 0)) {
352 AddMode = TRUE;
353 }
354
355 if ((*Target != NULL) && !AddMode) {
356 FreePool(*Target);
357 *Target = NULL;
358 } // if
359 for (i = 1; i < TokenCount; i++) {
360 if ((i != 1) || !AddMode) {
361 CleanUpPathNameSlashes(TokenList[i]);
362 MergeStrings(Target, TokenList[i], L',');
363 } // if
364 } // for
365 } // static VOID HandleStrings()
366
367 // Convert TimeString (in "HH:MM" format) to a pure-minute format. Values should be
368 // in the range from 0 (for 00:00, or midnight) to 1439 (for 23:59; aka LAST_MINUTE).
369 // Any value outside that range denotes an error in the specification. Note that if
370 // the input is a number that includes no colon, this function will return the original
371 // number in UINTN form.
372 static UINTN HandleTime(IN CHAR16 *TimeString) {
373 UINTN Hour = 0, Minute = 0, TimeLength, i = 0;
374
375 TimeLength = StrLen(TimeString);
376 while (i < TimeLength) {
377 if (TimeString[i] == L':') {
378 Hour = Minute;
379 Minute = 0;
380 } // if
381 if ((TimeString[i] >= L'0') && (TimeString[i] <= '9')) {
382 Minute *= 10;
383 Minute += (TimeString[i] - L'0');
384 } // if
385 i++;
386 } // while
387 return (Hour * 60 + Minute);
388 } // BOOLEAN HandleTime()
389
390 static BOOLEAN HandleBoolean(IN CHAR16 **TokenList, IN UINTN TokenCount) {
391 BOOLEAN TruthValue = TRUE;
392
393 if ((TokenCount >= 2) && ((StrCmp(TokenList[1], L"0") == 0) ||
394 MyStriCmp(TokenList[1], L"false") ||
395 MyStriCmp(TokenList[1], L"off"))) {
396 TruthValue = FALSE;
397 } // if
398
399 return TruthValue;
400 } // BOOLEAN HandleBoolean
401
402 // Sets the default boot loader IF the current time is within the bounds
403 // defined by the third and fourth tokens in the TokenList.
404 static VOID SetDefaultByTime(IN CHAR16 **TokenList, OUT CHAR16 **Default) {
405 EFI_STATUS Status;
406 EFI_TIME CurrentTime;
407 UINTN StartTime, EndTime, Now;
408 BOOLEAN SetIt = FALSE;
409
410 StartTime = HandleTime(TokenList[2]);
411 EndTime = HandleTime(TokenList[3]);
412
413 if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE)) {
414 Status = refit_call2_wrapper(GetTime, &CurrentTime, NULL);
415 if (Status != EFI_SUCCESS)
416 return;
417 Now = CurrentTime.Hour * 60 + CurrentTime.Minute;
418
419 if (Now > LAST_MINUTE) { // Shouldn't happen; just being paranoid
420 Print(L"Warning: Impossible system time: %d:%d\n", CurrentTime.Hour, CurrentTime.Minute);
421 return;
422 } // if impossible time
423
424 if (StartTime < EndTime) { // Time range does NOT cross midnight
425 if ((Now >= StartTime) && (Now <= EndTime))
426 SetIt = TRUE;
427 } else { // Time range DOES cross midnight
428 if ((Now >= StartTime) && (Now <= EndTime))
429 SetIt = TRUE;
430 } // if/else time range crosses midnight
431
432 if (SetIt) {
433 MyFreePool(*Default);
434 *Default = StrDuplicate(TokenList[1]);
435 } // if (SetIt)
436 } // if ((StartTime <= LAST_MINUTE) && (EndTime <= LAST_MINUTE))
437 } // VOID SetDefaultByTime()
438
439 // read config file
440 VOID ReadConfig(CHAR16 *FileName)
441 {
442 EFI_STATUS Status;
443 REFIT_FILE File;
444 CHAR16 **TokenList;
445 CHAR16 *FlagName;
446 CHAR16 *TempStr = NULL;
447 UINTN TokenCount, i;
448 EFI_GUID RefindGuid = REFIND_GUID_VALUE;
449
450 // Set a few defaults only if we're loading the default file.
451 if (MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
452 MyFreePool(GlobalConfig.AlsoScan);
453 GlobalConfig.AlsoScan = StrDuplicate(ALSO_SCAN_DIRS);
454 MyFreePool(GlobalConfig.DontScanDirs);
455 if (SelfVolume) {
456 if (SelfVolume->VolName) {
457 TempStr = SelfVolume->VolName ? StrDuplicate(SelfVolume->VolName) : NULL;
458 } else {
459 TempStr = AllocateZeroPool(256 * sizeof(CHAR16));
460 if (TempStr != NULL)
461 SPrint(TempStr, 255, L"fs%d", SelfVolume->VolNumber);
462 } // if/else
463 }
464 MergeStrings(&TempStr, SelfDirPath, L':');
465 MergeStrings(&TempStr, MEMTEST_LOCATIONS, L',');
466 GlobalConfig.DontScanDirs = TempStr;
467 MyFreePool(GlobalConfig.DontScanFiles);
468 GlobalConfig.DontScanFiles = StrDuplicate(DONT_SCAN_FILES);
469 MergeStrings(&(GlobalConfig.DontScanFiles), MOK_NAMES, L',');
470 MyFreePool(GlobalConfig.DontScanVolumes);
471 GlobalConfig.DontScanVolumes = StrDuplicate(DONT_SCAN_VOLUMES);
472 GlobalConfig.WindowsRecoveryFiles = StrDuplicate(WINDOWS_RECOVERY_FILES);
473 if (GlobalConfig.DefaultSelection != NULL) {
474 MyFreePool(GlobalConfig.DefaultSelection);
475 GlobalConfig.DefaultSelection = NULL;
476 }
477 Status = EfivarGetRaw(&RefindGuid, L"PreviousBoot", (CHAR8**) &(GlobalConfig.DefaultSelection), &i);
478 if (Status != EFI_SUCCESS)
479 GlobalConfig.DefaultSelection = NULL;
480 } // if
481
482 if (!FileExists(SelfDir, FileName)) {
483 Print(L"Configuration file '%s' missing!\n", FileName);
484 if (!FileExists(SelfDir, L"icons")) {
485 Print(L"Icons directory doesn't exist; setting textonly = TRUE!\n");
486 GlobalConfig.TextOnly = TRUE;
487 }
488 return;
489 }
490
491 Status = ReadFile(SelfDir, FileName, &File, &i);
492 if (EFI_ERROR(Status))
493 return;
494
495 for (;;) {
496 TokenCount = ReadTokenLine(&File, &TokenList);
497 if (TokenCount == 0)
498 break;
499
500 if (MyStriCmp(TokenList[0], L"timeout")) {
501 HandleInt(TokenList, TokenCount, &(GlobalConfig.Timeout));
502
503 } else if (MyStriCmp(TokenList[0], L"hideui")) {
504 for (i = 1; i < TokenCount; i++) {
505 FlagName = TokenList[i];
506 if (MyStriCmp(FlagName, L"banner")) {
507 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BANNER;
508 } else if (MyStriCmp(FlagName, L"label")) {
509 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_LABEL;
510 } else if (MyStriCmp(FlagName, L"singleuser")) {
511 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_SINGLEUSER;
512 } else if (MyStriCmp(FlagName, L"hwtest")) {
513 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_HWTEST;
514 } else if (MyStriCmp(FlagName, L"arrows")) {
515 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_ARROWS;
516 } else if (MyStriCmp(FlagName, L"hints")) {
517 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_HINTS;
518 } else if (MyStriCmp(FlagName, L"editor")) {
519 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_EDITOR;
520 } else if (MyStriCmp(FlagName, L"safemode")) {
521 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_SAFEMODE;
522 } else if (MyStriCmp(FlagName, L"badges")) {
523 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BADGES;
524 } else if (MyStriCmp(FlagName, L"all")) {
525 GlobalConfig.HideUIFlags = HIDEUI_FLAG_ALL;
526 } else {
527 Print(L" unknown hideui flag: '%s'\n", FlagName);
528 }
529 }
530
531 } else if (MyStriCmp(TokenList[0], L"icons_dir")) {
532 HandleString(TokenList, TokenCount, &(GlobalConfig.IconsDir));
533
534 } else if (MyStriCmp(TokenList[0], L"scanfor")) {
535 for (i = 0; i < NUM_SCAN_OPTIONS; i++) {
536 if (i < TokenCount)
537 GlobalConfig.ScanFor[i] = TokenList[i][0];
538 else
539 GlobalConfig.ScanFor[i] = ' ';
540 }
541
542 } else if (MyStriCmp(TokenList[0], L"uefi_deep_legacy_scan")) {
543 GlobalConfig.DeepLegacyScan = HandleBoolean(TokenList, TokenCount);
544
545 } else if (MyStriCmp(TokenList[0], L"scan_delay") && (TokenCount == 2)) {
546 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScanDelay));
547
548 } else if (MyStriCmp(TokenList[0], L"also_scan_dirs")) {
549 HandleStrings(TokenList, TokenCount, &(GlobalConfig.AlsoScan));
550
551 } else if (MyStriCmp(TokenList[0], L"don't_scan_volumes") || MyStriCmp(TokenList[0], L"dont_scan_volumes")) {
552 // Note: Don't use HandleStrings() because it modifies slashes, which might be present in volume name
553 MyFreePool(GlobalConfig.DontScanVolumes);
554 GlobalConfig.DontScanVolumes = NULL;
555 for (i = 1; i < TokenCount; i++) {
556 MergeStrings(&GlobalConfig.DontScanVolumes, TokenList[i], L',');
557 }
558
559 } else if (MyStriCmp(TokenList[0], L"don't_scan_dirs") || MyStriCmp(TokenList[0], L"dont_scan_dirs")) {
560 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DontScanDirs));
561
562 } else if (MyStriCmp(TokenList[0], L"don't_scan_files") || MyStriCmp(TokenList[0], L"dont_scan_files")) {
563 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DontScanFiles));
564
565 } else if (MyStriCmp(TokenList[0], L"windows_recovery_files")) {
566 HandleStrings(TokenList, TokenCount, &(GlobalConfig.WindowsRecoveryFiles));
567
568 } else if (MyStriCmp(TokenList[0], L"scan_driver_dirs")) {
569 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DriverDirs));
570
571 } else if (MyStriCmp(TokenList[0], L"showtools")) {
572 SetMem(GlobalConfig.ShowTools, NUM_TOOLS * sizeof(UINTN), 0);
573 for (i = 1; (i < TokenCount) && (i < NUM_TOOLS); i++) {
574 FlagName = TokenList[i];
575 if (MyStriCmp(FlagName, L"shell")) {
576 GlobalConfig.ShowTools[i - 1] = TAG_SHELL;
577 } else if (MyStriCmp(FlagName, L"gptsync")) {
578 GlobalConfig.ShowTools[i - 1] = TAG_GPTSYNC;
579 } else if (MyStriCmp(FlagName, L"gdisk")) {
580 GlobalConfig.ShowTools[i - 1] = TAG_GDISK;
581 } else if (MyStriCmp(FlagName, L"about")) {
582 GlobalConfig.ShowTools[i - 1] = TAG_ABOUT;
583 } else if (MyStriCmp(FlagName, L"exit")) {
584 GlobalConfig.ShowTools[i - 1] = TAG_EXIT;
585 } else if (MyStriCmp(FlagName, L"reboot")) {
586 GlobalConfig.ShowTools[i - 1] = TAG_REBOOT;
587 } else if (MyStriCmp(FlagName, L"shutdown")) {
588 GlobalConfig.ShowTools[i - 1] = TAG_SHUTDOWN;
589 } else if (MyStriCmp(FlagName, L"apple_recovery")) {
590 GlobalConfig.ShowTools[i - 1] = TAG_APPLE_RECOVERY;
591 } else if (MyStriCmp(FlagName, L"windows_recovery")) {
592 GlobalConfig.ShowTools[i - 1] = TAG_WINDOWS_RECOVERY;
593 } else if (MyStriCmp(FlagName, L"mok_tool")) {
594 GlobalConfig.ShowTools[i - 1] = TAG_MOK_TOOL;
595 } else if (MyStriCmp(FlagName, L"firmware")) {
596 GlobalConfig.ShowTools[i - 1] = TAG_FIRMWARE;
597 } else if (MyStriCmp(FlagName, L"memtest86") || MyStriCmp(FlagName, L"memtest")) {
598 GlobalConfig.ShowTools[i - 1] = TAG_MEMTEST;
599 } else if (MyStriCmp(FlagName, L"netboot")) {
600 GlobalConfig.ShowTools[i - 1] = TAG_NETBOOT;
601 } else {
602 Print(L" unknown showtools flag: '%s'\n", FlagName);
603 }
604 } // showtools options
605
606 } else if (MyStriCmp(TokenList[0], L"banner")) {
607 HandleString(TokenList, TokenCount, &(GlobalConfig.BannerFileName));
608
609 } else if (MyStriCmp(TokenList[0], L"banner_scale") && (TokenCount == 2)) {
610 if (MyStriCmp(TokenList[1], L"noscale")) {
611 GlobalConfig.BannerScale = BANNER_NOSCALE;
612 } else if (MyStriCmp(TokenList[1], L"fillscreen") || MyStriCmp(TokenList[1], L"fullscreen")) {
613 GlobalConfig.BannerScale = BANNER_FILLSCREEN;
614 } else {
615 Print(L" unknown banner_type flag: '%s'\n", TokenList[1]);
616 } // if/else
617
618 } else if (MyStriCmp(TokenList[0], L"small_icon_size") && (TokenCount == 2)) {
619 HandleInt(TokenList, TokenCount, &i);
620 if (i >= 32)
621 GlobalConfig.IconSizes[ICON_SIZE_SMALL] = i;
622
623 } else if (MyStriCmp(TokenList[0], L"big_icon_size") && (TokenCount == 2)) {
624 HandleInt(TokenList, TokenCount, &i);
625 if (i >= 32) {
626 GlobalConfig.IconSizes[ICON_SIZE_BIG] = i;
627 GlobalConfig.IconSizes[ICON_SIZE_BADGE] = i / 4;
628 }
629
630 } else if (MyStriCmp(TokenList[0], L"selection_small")) {
631 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionSmallFileName));
632
633 } else if (MyStriCmp(TokenList[0], L"selection_big")) {
634 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionBigFileName));
635
636 } else if (MyStriCmp(TokenList[0], L"default_selection")) {
637 if (TokenCount == 4) {
638 SetDefaultByTime(TokenList, &(GlobalConfig.DefaultSelection));
639 } else {
640 HandleString(TokenList, TokenCount, &(GlobalConfig.DefaultSelection));
641 }
642
643 } else if (MyStriCmp(TokenList[0], L"textonly")) {
644 GlobalConfig.TextOnly = HandleBoolean(TokenList, TokenCount);
645
646 } else if (MyStriCmp(TokenList[0], L"textmode")) {
647 HandleInt(TokenList, TokenCount, &(GlobalConfig.RequestedTextMode));
648
649 } else if (MyStriCmp(TokenList[0], L"resolution") && ((TokenCount == 2) || (TokenCount == 3))) {
650 GlobalConfig.RequestedScreenWidth = Atoi(TokenList[1]);
651 if (TokenCount == 3)
652 GlobalConfig.RequestedScreenHeight = Atoi(TokenList[2]);
653 else
654 GlobalConfig.RequestedScreenHeight = 0;
655
656 } else if (MyStriCmp(TokenList[0], L"screensaver")) {
657 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScreensaverTime));
658
659 } else if (MyStriCmp(TokenList[0], L"use_graphics_for")) {
660 if ((TokenCount == 2) || ((TokenCount > 2) && (!MyStriCmp(TokenList[1], L"+"))))
661 GlobalConfig.GraphicsFor = 0;
662 for (i = 1; i < TokenCount; i++) {
663 if (MyStriCmp(TokenList[i], L"osx")) {
664 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_OSX;
665 } else if (MyStriCmp(TokenList[i], L"linux")) {
666 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_LINUX;
667 } else if (MyStriCmp(TokenList[i], L"elilo")) {
668 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_ELILO;
669 } else if (MyStriCmp(TokenList[i], L"grub")) {
670 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_GRUB;
671 } else if (MyStriCmp(TokenList[i], L"windows")) {
672 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_WINDOWS;
673 }
674 } // for (graphics_on tokens)
675
676 } else if (MyStriCmp(TokenList[0], L"font") && (TokenCount == 2)) {
677 egLoadFont(TokenList[1]);
678
679 } else if (MyStriCmp(TokenList[0], L"scan_all_linux_kernels")) {
680 GlobalConfig.ScanAllLinux = HandleBoolean(TokenList, TokenCount);
681
682 } else if (MyStriCmp(TokenList[0], L"fold_linux_kernels")) {
683 GlobalConfig.FoldLinuxKernels = HandleBoolean(TokenList, TokenCount);
684
685 } else if (MyStriCmp(TokenList[0], L"max_tags")) {
686 HandleInt(TokenList, TokenCount, &(GlobalConfig.MaxTags));
687
688 } else if (MyStriCmp(TokenList[0], L"enable_and_lock_vmx")) {
689 GlobalConfig.EnableAndLockVMX = HandleBoolean(TokenList, TokenCount);
690
691 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) && MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
692 if (!MyStriCmp(TokenList[1], FileName)) {
693 ReadConfig(TokenList[1]);
694 }
695
696 }
697
698 FreeTokenLine(&TokenList, &TokenCount);
699 }
700 if ((GlobalConfig.DontScanFiles) && (GlobalConfig.WindowsRecoveryFiles))
701 MergeStrings(&(GlobalConfig.DontScanFiles), GlobalConfig.WindowsRecoveryFiles, L',');
702 MyFreePool(File.Buffer);
703
704 if (!FileExists(SelfDir, L"icons") && !FileExists(SelfDir, GlobalConfig.IconsDir)) {
705 Print(L"Icons directory doesn't exist; setting textonly = TRUE!\n");
706 GlobalConfig.TextOnly = TRUE;
707 }
708 } /* VOID ReadConfig() */
709
710 // Finds a volume with the specified Identifier (a filesystem label, a
711 // partition name, a partition GUID, or a number followed by a colon). If
712 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
713 // Returns TRUE if a match was found, FALSE if not.
714 static BOOLEAN FindVolume(REFIT_VOLUME **Volume, CHAR16 *Identifier) {
715 UINTN i = 0, CountedVolumes = 0, Length;
716 INTN Number = -1;
717 BOOLEAN Found = FALSE, IdIsGuid = FALSE;
718 EFI_GUID VolGuid, NullGuid = NULL_GUID_VALUE;
719
720 VolGuid = StringAsGuid(Identifier);
721 Length = StrLen(Identifier);
722 if ((Length >= 2) && (Identifier[Length - 1] == L':') &&
723 (Identifier[0] >= L'0') && (Identifier[0] <= L'9')) {
724 Number = (INTN) Atoi(Identifier);
725 } else if (IsGuid(Identifier)) {
726 IdIsGuid = TRUE;
727 }
728 while ((i < VolumesCount) && (!Found)) {
729 if (Number >= 0) { // User specified a volume by number
730 if (Volumes[i]->IsReadable) {
731 if (CountedVolumes == Number) {
732 *Volume = Volumes[i];
733 Found = TRUE;
734 }
735 CountedVolumes++;
736 } // if
737 } else { // User specified a volume by label or GUID
738 if (MyStriCmp(Identifier, Volumes[i]->VolName) || MyStriCmp(Identifier, Volumes[i]->PartName)) {
739 *Volume = Volumes[i];
740 Found = TRUE;
741 } // if
742 if (IdIsGuid && !Found) {
743 if (GuidsAreEqual(&VolGuid, &(Volumes[i]->PartGuid)) && !GuidsAreEqual(&NullGuid, &(Volumes[i]->PartGuid))) {
744 *Volume = Volumes[i];
745 Found = TRUE;
746 } // if
747 } // if
748 } // if/else
749 i++;
750 } // while()
751 return (Found);
752 } // static VOID FindVolume()
753
754 static VOID AddSubmenu(LOADER_ENTRY *Entry, REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
755 REFIT_MENU_SCREEN *SubScreen;
756 LOADER_ENTRY *SubEntry;
757 UINTN TokenCount;
758 CHAR16 **TokenList;
759
760 SubScreen = InitializeSubScreen(Entry);
761
762 // Set defaults for the new entry; will be modified based on lines read from the config. file....
763 SubEntry = InitializeLoaderEntry(Entry);
764
765 if ((SubEntry == NULL) || (SubScreen == NULL))
766 return;
767 SubEntry->me.Title = StrDuplicate(Title);
768
769 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
770
771 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
772 MyFreePool(SubEntry->LoaderPath);
773 SubEntry->LoaderPath = StrDuplicate(TokenList[1]);
774 SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
775
776 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
777 if (FindVolume(&Volume, TokenList[1])) {
778 if ((Volume != NULL) && (Volume->IsReadable) && (Volume->RootDir)) {
779 MyFreePool(SubEntry->me.Title);
780 SubEntry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
781 SPrint(SubEntry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", Volume->VolName);
782 SubEntry->me.BadgeImage = Volume->VolBadgeImage;
783 SubEntry->VolName = Volume->VolName;
784 } // if volume is readable
785 } // if match found
786
787 } else if (MyStriCmp(TokenList[0], L"initrd")) {
788 MyFreePool(SubEntry->InitrdPath);
789 SubEntry->InitrdPath = NULL;
790 if (TokenCount > 1) {
791 SubEntry->InitrdPath = StrDuplicate(TokenList[1]);
792 }
793
794 } else if (MyStriCmp(TokenList[0], L"options")) {
795 MyFreePool(SubEntry->LoadOptions);
796 SubEntry->LoadOptions = NULL;
797 if (TokenCount > 1) {
798 SubEntry->LoadOptions = StrDuplicate(TokenList[1]);
799 } // if/else
800
801 } else if (MyStriCmp(TokenList[0], L"add_options") && (TokenCount > 1)) {
802 MergeStrings(&SubEntry->LoadOptions, TokenList[1], L' ');
803
804 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
805 SubEntry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
806
807 } else if (MyStriCmp(TokenList[0], L"disabled")) {
808 SubEntry->Enabled = FALSE;
809 } // ief/elseif
810
811 FreeTokenLine(&TokenList, &TokenCount);
812 } // while()
813
814 if (SubEntry->InitrdPath != NULL) {
815 MergeStrings(&SubEntry->LoadOptions, L"initrd=", L' ');
816 MergeStrings(&SubEntry->LoadOptions, SubEntry->InitrdPath, 0);
817 MyFreePool(SubEntry->InitrdPath);
818 SubEntry->InitrdPath = NULL;
819 } // if
820 if (SubEntry->Enabled == TRUE) {
821 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
822 }
823 Entry->me.SubScreen = SubScreen;
824 } // VOID AddSubmenu()
825
826 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
827 // that entry. The calling function is then responsible for adding the entry to the
828 // list of entries.
829 static LOADER_ENTRY * AddStanzaEntries(REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
830 CHAR16 **TokenList;
831 UINTN TokenCount;
832 LOADER_ENTRY *Entry;
833 BOOLEAN DefaultsSet = FALSE, AddedSubmenu = FALSE;
834 REFIT_VOLUME *CurrentVolume = Volume;
835
836 // prepare the menu entry
837 Entry = InitializeLoaderEntry(NULL);
838 if (Entry == NULL)
839 return NULL;
840
841 Entry->Title = StrDuplicate(Title);
842 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
843 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
844 Entry->me.Row = 0;
845 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
846 Entry->VolName = CurrentVolume->VolName;
847
848 // Parse the config file to add options for a single stanza, terminating when the token
849 // is "}" or when the end of file is reached.
850 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
851 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
852 Entry->LoaderPath = StrDuplicate(TokenList[1]);
853 Entry->DevicePath = FileDevicePath(CurrentVolume->DeviceHandle, Entry->LoaderPath);
854 SetLoaderDefaults(Entry, TokenList[1], CurrentVolume);
855 MyFreePool(Entry->LoadOptions);
856 Entry->LoadOptions = NULL; // Discard default options, if any
857 DefaultsSet = TRUE;
858
859 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
860 if (FindVolume(&CurrentVolume, TokenList[1])) {
861 if ((CurrentVolume != NULL) && (CurrentVolume->IsReadable) && (CurrentVolume->RootDir)) {
862 MyFreePool(Entry->me.Title);
863 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
864 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
865 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
866 Entry->VolName = CurrentVolume->VolName;
867 } // if volume is readable
868 } // if match found
869
870 } else if (MyStriCmp(TokenList[0], L"icon") && (TokenCount > 1)) {
871 MyFreePool(Entry->me.Image);
872 Entry->me.Image = egLoadIcon(CurrentVolume->RootDir, TokenList[1], GlobalConfig.IconSizes[ICON_SIZE_BIG]);
873 if (Entry->me.Image == NULL) {
874 Entry->me.Image = DummyImage(GlobalConfig.IconSizes[ICON_SIZE_BIG]);
875 }
876
877 } else if (MyStriCmp(TokenList[0], L"initrd") && (TokenCount > 1)) {
878 MyFreePool(Entry->InitrdPath);
879 Entry->InitrdPath = StrDuplicate(TokenList[1]);
880
881 } else if (MyStriCmp(TokenList[0], L"options") && (TokenCount > 1)) {
882 MyFreePool(Entry->LoadOptions);
883 Entry->LoadOptions = StrDuplicate(TokenList[1]);
884
885 } else if (MyStriCmp(TokenList[0], L"ostype") && (TokenCount > 1)) {
886 if (TokenCount > 1) {
887 Entry->OSType = TokenList[1][0];
888 }
889
890 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
891 Entry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
892
893 } else if (MyStriCmp(TokenList[0], L"disabled")) {
894 Entry->Enabled = FALSE;
895
896 } else if (MyStriCmp(TokenList[0], L"submenuentry") && (TokenCount > 1)) {
897 AddSubmenu(Entry, File, CurrentVolume, TokenList[1]);
898 AddedSubmenu = TRUE;
899
900 } // set options to pass to the loader program
901 FreeTokenLine(&TokenList, &TokenCount);
902 } // while()
903
904 if (AddedSubmenu)
905 AddMenuEntry(Entry->me.SubScreen, &MenuEntryReturn);
906
907 if (Entry->InitrdPath) {
908 MergeStrings(&Entry->LoadOptions, L"initrd=", L' ');
909 MergeStrings(&Entry->LoadOptions, Entry->InitrdPath, 0);
910 MyFreePool(Entry->InitrdPath);
911 Entry->InitrdPath = NULL;
912 } // if
913
914 if (!DefaultsSet)
915 SetLoaderDefaults(Entry, L"\\EFI\\BOOT\\nemo.efi", CurrentVolume); // user included no "loader" line; use bogus one
916
917 return(Entry);
918 } // static VOID AddStanzaEntries()
919
920 // Read the user-configured menu entries from refind.conf and add or delete
921 // entries based on the contents of that file....
922 VOID ScanUserConfigured(CHAR16 *FileName)
923 {
924 EFI_STATUS Status;
925 REFIT_FILE File;
926 REFIT_VOLUME *Volume;
927 CHAR16 **TokenList;
928 CHAR16 *Title = NULL;
929 UINTN TokenCount, size;
930 LOADER_ENTRY *Entry;
931
932 if (FileExists(SelfDir, FileName)) {
933 Status = ReadFile(SelfDir, FileName, &File, &size);
934 if (EFI_ERROR(Status))
935 return;
936
937 Volume = SelfVolume;
938
939 while ((TokenCount = ReadTokenLine(&File, &TokenList)) > 0) {
940 if (MyStriCmp(TokenList[0], L"menuentry") && (TokenCount > 1)) {
941 Title = StrDuplicate(TokenList[1]);
942 Entry = AddStanzaEntries(&File, Volume, TokenList[1]);
943 if (Entry->Enabled) {
944 if (Entry->me.SubScreen == NULL)
945 GenerateSubScreen(Entry, Volume, TRUE);
946 AddPreparedLoaderEntry(Entry);
947 } else {
948 MyFreePool(Entry);
949 } // if/else
950 MyFreePool(Title);
951
952 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) &&
953 MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
954 if (!MyStriCmp(TokenList[1], FileName)) {
955 ScanUserConfigured(TokenList[1]);
956 }
957
958 } // if/else if...
959 FreeTokenLine(&TokenList, &TokenCount);
960 } // while()
961 } // if()
962 } // VOID ScanUserConfigured()
963
964 // Create an options file based on /etc/fstab. The resulting file has two options
965 // lines, one of which boots the system with "ro root={rootfs}" and the other of
966 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
967 // filesystem identifier associated with the "/" line in /etc/fstab.
968 static REFIT_FILE * GenerateOptionsFromEtcFstab(REFIT_VOLUME *Volume) {
969 UINTN TokenCount, i;
970 REFIT_FILE *Options = NULL, *Fstab = NULL;
971 EFI_STATUS Status;
972 CHAR16 **TokenList, *Line, Root[100];
973
974 if (FileExists(Volume->RootDir, L"\\etc\\fstab")) {
975 Options = AllocateZeroPool(sizeof(REFIT_FILE));
976 Fstab = AllocateZeroPool(sizeof(REFIT_FILE));
977 Status = ReadFile(Volume->RootDir, L"\\etc\\fstab", Fstab, &i);
978 if (CheckError(Status, L"while reading /etc/fstab")) {
979 if (Options != NULL)
980 FreePool(Options);
981 if (Fstab != NULL)
982 FreePool(Fstab);
983 Options = NULL;
984 Fstab = NULL;
985 } else { // File read; locate root fs and create entries
986 Options->Encoding = ENCODING_UTF16_LE;
987 while ((TokenCount = ReadTokenLine(Fstab, &TokenList)) > 0) {
988 if (TokenCount > 2) {
989 Root[0] = '\0';
990 if (StrCmp(TokenList[1], L"\\") == 0) {
991 SPrint(Root, 99, L"%s", TokenList[0]);
992 } else if (StrCmp(TokenList[2], L"\\") == 0) {
993 SPrint(Root, 99, L"%s=%s", TokenList[0], TokenList[1]);
994 } // if/elseif/elseif
995 if (Root[0] != L'\0') {
996 for (i = 0; i < StrLen(Root); i++)
997 if (Root[i] == '\\')
998 Root[i] = '/';
999 Line = PoolPrint(L"\"Boot with normal options\" \"ro root=%s\"\n", Root);
1000 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1001 MyFreePool(Line);
1002 Line = PoolPrint(L"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root);
1003 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1004 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1005 } // if
1006 } // if
1007 FreeTokenLine(&TokenList, &TokenCount);
1008 } // while
1009
1010 if (Options->Buffer) {
1011 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1012 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1013 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1014 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1015 } else {
1016 MyFreePool(Options);
1017 Options = NULL;
1018 }
1019
1020 MyFreePool(Fstab->Buffer);
1021 MyFreePool(Fstab);
1022 } // if/else file read error
1023 } // if /etc/fstab exists
1024 return Options;
1025 } // GenerateOptionsFromEtcFstab()
1026
1027 // Create options from partition type codes. Specifically, if the earlier
1028 // partition scan found a partition with a type code corresponding to a root
1029 // filesystem according to the Freedesktop.org Discoverable Partitions Spec
1030 // (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/),
1031 // this function returns an appropriate file with two lines, one with
1032 // "ro root=/dev/disk/by-partuuid/{GUID}" and the other with that plus "single".
1033 // Note that this function returns the LAST partition found with the
1034 // appropriate type code, so this will work poorly on dual-boot systems or
1035 // if the type code is set incorrectly.
1036 static REFIT_FILE * GenerateOptionsFromPartTypes(VOID) {
1037 REFIT_FILE *Options = NULL;
1038 CHAR16 *Line, *GuidString, *WriteStatus;
1039
1040 if (GlobalConfig.DiscoveredRoot) {
1041 Options = AllocateZeroPool(sizeof(REFIT_FILE));
1042 if (Options) {
1043 Options->Encoding = ENCODING_UTF16_LE;
1044 GuidString = GuidAsString(&(GlobalConfig.DiscoveredRoot->PartGuid));
1045 WriteStatus = GlobalConfig.DiscoveredRoot->IsMarkedReadOnly ? L"ro" : L"rw";
1046 ToLower(GuidString);
1047 if (GuidString) {
1048 Line = PoolPrint(L"\"Boot with normal options\" \"%s root=/dev/disk/by-partuuid/%s\"\n", WriteStatus, GuidString);
1049 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1050 MyFreePool(Line);
1051 Line = PoolPrint(L"\"Boot into single-user mode\" \"%s root=/dev/disk/by-partuuid/%s single\"\n", WriteStatus, GuidString);
1052 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1053 MyFreePool(Line);
1054 MyFreePool(GuidString);
1055 } // if (GuidString)
1056 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1057
1058 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1059 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1060 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1061 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1062 } // if (Options allocated OK)
1063 } // if (partition has root GUID)
1064 return Options;
1065 } // REFIT_FILE * GenerateOptionsFromPartTypes()
1066
1067 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1068 // and Volume variables identify the location of the options file, but not its name --
1069 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1070 // file in the target directory, and this function finds the file with a name in the
1071 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1072 // directory and loads it. This function tries multiple files because I originally
1073 // used the filename linux.conf, but close on the heels of that decision, the Linux
1074 // kernel developers decided to use that name for a similar purpose, but with a
1075 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1076 // but I want a migration period in which both names are used.
1077 // If a rEFInd options file can't be found, try to generate minimal options from
1078 // /etc/fstab on the same volume as the kernel. This typically works only if the
1079 // kernel is being read from the Linux root filesystem.
1080 //
1081 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1082 // it wasn't found.
1083 REFIT_FILE * ReadLinuxOptionsFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1084 CHAR16 *OptionsFilename, *FullFilename;
1085 BOOLEAN GoOn = TRUE, FileFound = FALSE;
1086 UINTN i = 0, size;
1087 REFIT_FILE *File = NULL;
1088 EFI_STATUS Status;
1089
1090 do {
1091 OptionsFilename = FindCommaDelimited(LINUX_OPTIONS_FILENAMES, i++);
1092 FullFilename = FindPath(LoaderPath);
1093 if ((OptionsFilename != NULL) && (FullFilename != NULL)) {
1094 MergeStrings(&FullFilename, OptionsFilename, '\\');
1095 if (FileExists(Volume->RootDir, FullFilename)) {
1096 File = AllocateZeroPool(sizeof(REFIT_FILE));
1097 Status = ReadFile(Volume->RootDir, FullFilename, File, &size);
1098 if (CheckError(Status, L"while loading the Linux options file")) {
1099 if (File != NULL)
1100 FreePool(File);
1101 File = NULL;
1102 } else {
1103 GoOn = FALSE;
1104 FileFound = TRUE;
1105 } // if/else error
1106 } // if file exists
1107 } else { // a filename string is NULL
1108 GoOn = FALSE;
1109 } // if/else
1110 MyFreePool(OptionsFilename);
1111 MyFreePool(FullFilename);
1112 OptionsFilename = FullFilename = NULL;
1113 } while (GoOn);
1114 if (!FileFound) {
1115 // No refind_linux.conf file; look for /etc/fstab and try to pull values from there....
1116 File = GenerateOptionsFromEtcFstab(Volume);
1117 // If still no joy, try to use Freedesktop.org Discoverable Partitions Spec....
1118 if (!File)
1119 File = GenerateOptionsFromPartTypes();
1120 } // if
1121 return (File);
1122 } // static REFIT_FILE * ReadLinuxOptionsFile()
1123
1124 // Retrieve a single line of options from a Linux kernel options file
1125 CHAR16 * GetFirstOptionsFromFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1126 UINTN TokenCount;
1127 CHAR16 *Options = NULL;
1128 CHAR16 **TokenList;
1129 REFIT_FILE *File;
1130
1131 File = ReadLinuxOptionsFile(LoaderPath, Volume);
1132 if (File != NULL) {
1133 TokenCount = ReadTokenLine(File, &TokenList);
1134 if (TokenCount > 1)
1135 Options = StrDuplicate(TokenList[1]);
1136 FreeTokenLine(&TokenList, &TokenCount);
1137 FreePool(File);
1138 } // if
1139 return Options;
1140 } // static CHAR16 * GetOptionsFile()
1141