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