]> code.delx.au - refind/blob - refind/config.c
e54e56bc0cf8a0516a96fb9a8e65b4bf873f2a14
[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"csr_rotate")) {
609 GlobalConfig.ShowTools[i - 1] = TAG_CSR_ROTATE;
610 } else if (MyStriCmp(FlagName, L"firmware")) {
611 GlobalConfig.ShowTools[i - 1] = TAG_FIRMWARE;
612 } else if (MyStriCmp(FlagName, L"memtest86") || MyStriCmp(FlagName, L"memtest")) {
613 GlobalConfig.ShowTools[i - 1] = TAG_MEMTEST;
614 } else if (MyStriCmp(FlagName, L"netboot")) {
615 GlobalConfig.ShowTools[i - 1] = TAG_NETBOOT;
616 } else {
617 Print(L" unknown showtools flag: '%s'\n", FlagName);
618 }
619 } // showtools options
620
621 } else if (MyStriCmp(TokenList[0], L"banner")) {
622 HandleString(TokenList, TokenCount, &(GlobalConfig.BannerFileName));
623
624 } else if (MyStriCmp(TokenList[0], L"banner_scale") && (TokenCount == 2)) {
625 if (MyStriCmp(TokenList[1], L"noscale")) {
626 GlobalConfig.BannerScale = BANNER_NOSCALE;
627 } else if (MyStriCmp(TokenList[1], L"fillscreen") || MyStriCmp(TokenList[1], L"fullscreen")) {
628 GlobalConfig.BannerScale = BANNER_FILLSCREEN;
629 } else {
630 Print(L" unknown banner_type flag: '%s'\n", TokenList[1]);
631 } // if/else
632
633 } else if (MyStriCmp(TokenList[0], L"small_icon_size") && (TokenCount == 2)) {
634 HandleInt(TokenList, TokenCount, &i);
635 if (i >= 32)
636 GlobalConfig.IconSizes[ICON_SIZE_SMALL] = i;
637
638 } else if (MyStriCmp(TokenList[0], L"big_icon_size") && (TokenCount == 2)) {
639 HandleInt(TokenList, TokenCount, &i);
640 if (i >= 32) {
641 GlobalConfig.IconSizes[ICON_SIZE_BIG] = i;
642 GlobalConfig.IconSizes[ICON_SIZE_BADGE] = i / 4;
643 }
644
645 } else if (MyStriCmp(TokenList[0], L"selection_small")) {
646 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionSmallFileName));
647
648 } else if (MyStriCmp(TokenList[0], L"selection_big")) {
649 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionBigFileName));
650
651 } else if (MyStriCmp(TokenList[0], L"default_selection")) {
652 if (TokenCount == 4) {
653 SetDefaultByTime(TokenList, &(GlobalConfig.DefaultSelection));
654 } else {
655 HandleString(TokenList, TokenCount, &(GlobalConfig.DefaultSelection));
656 }
657
658 } else if (MyStriCmp(TokenList[0], L"textonly")) {
659 GlobalConfig.TextOnly = HandleBoolean(TokenList, TokenCount);
660
661 } else if (MyStriCmp(TokenList[0], L"textmode")) {
662 HandleInt(TokenList, TokenCount, &(GlobalConfig.RequestedTextMode));
663
664 } else if (MyStriCmp(TokenList[0], L"resolution") && ((TokenCount == 2) || (TokenCount == 3))) {
665 GlobalConfig.RequestedScreenWidth = Atoi(TokenList[1]);
666 if (TokenCount == 3)
667 GlobalConfig.RequestedScreenHeight = Atoi(TokenList[2]);
668 else
669 GlobalConfig.RequestedScreenHeight = 0;
670
671 } else if (MyStriCmp(TokenList[0], L"screensaver")) {
672 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScreensaverTime));
673
674 } else if (MyStriCmp(TokenList[0], L"use_graphics_for")) {
675 if ((TokenCount == 2) || ((TokenCount > 2) && (!MyStriCmp(TokenList[1], L"+"))))
676 GlobalConfig.GraphicsFor = 0;
677 for (i = 1; i < TokenCount; i++) {
678 if (MyStriCmp(TokenList[i], L"osx")) {
679 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_OSX;
680 } else if (MyStriCmp(TokenList[i], L"linux")) {
681 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_LINUX;
682 } else if (MyStriCmp(TokenList[i], L"elilo")) {
683 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_ELILO;
684 } else if (MyStriCmp(TokenList[i], L"grub")) {
685 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_GRUB;
686 } else if (MyStriCmp(TokenList[i], L"windows")) {
687 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_WINDOWS;
688 }
689 } // for (graphics_on tokens)
690
691 } else if (MyStriCmp(TokenList[0], L"font") && (TokenCount == 2)) {
692 egLoadFont(TokenList[1]);
693
694 } else if (MyStriCmp(TokenList[0], L"scan_all_linux_kernels")) {
695 GlobalConfig.ScanAllLinux = HandleBoolean(TokenList, TokenCount);
696
697 } else if (MyStriCmp(TokenList[0], L"fold_linux_kernels")) {
698 GlobalConfig.FoldLinuxKernels = HandleBoolean(TokenList, TokenCount);
699
700 } else if (MyStriCmp(TokenList[0], L"max_tags")) {
701 HandleInt(TokenList, TokenCount, &(GlobalConfig.MaxTags));
702
703 } else if (MyStriCmp(TokenList[0], L"enable_and_lock_vmx")) {
704 GlobalConfig.EnableAndLockVMX = HandleBoolean(TokenList, TokenCount);
705
706 } else if (MyStriCmp(TokenList[0], L"spoof_osx_version")) {
707 HandleString(TokenList, TokenCount, &(GlobalConfig.SpoofOSXVersion));
708
709 } else if (MyStriCmp(TokenList[0], L"csr_values")) {
710 HandleStrings(TokenList, TokenCount, &(GlobalConfig.CsrValues));
711
712 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) && MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
713 if (!MyStriCmp(TokenList[1], FileName)) {
714 ReadConfig(TokenList[1]);
715 }
716
717 }
718
719 FreeTokenLine(&TokenList, &TokenCount);
720 }
721 if ((GlobalConfig.DontScanFiles) && (GlobalConfig.WindowsRecoveryFiles))
722 MergeStrings(&(GlobalConfig.DontScanFiles), GlobalConfig.WindowsRecoveryFiles, L',');
723 MyFreePool(File.Buffer);
724
725 if (!FileExists(SelfDir, L"icons") && !FileExists(SelfDir, GlobalConfig.IconsDir)) {
726 Print(L"Icons directory doesn't exist; setting textonly = TRUE!\n");
727 GlobalConfig.TextOnly = TRUE;
728 }
729 } /* VOID ReadConfig() */
730
731 // Finds a volume with the specified Identifier (a filesystem label, a
732 // partition name, a partition GUID, or a number followed by a colon). If
733 // found, sets *Volume to point to that volume. If not, leaves it unchanged.
734 // Returns TRUE if a match was found, FALSE if not.
735 static BOOLEAN FindVolume(REFIT_VOLUME **Volume, CHAR16 *Identifier) {
736 UINTN i = 0, CountedVolumes = 0, Length;
737 INTN Number = -1;
738 BOOLEAN Found = FALSE, IdIsGuid = FALSE;
739 EFI_GUID VolGuid, NullGuid = NULL_GUID_VALUE;
740
741 VolGuid = StringAsGuid(Identifier);
742 Length = StrLen(Identifier);
743 if ((Length >= 2) && (Identifier[Length - 1] == L':') &&
744 (Identifier[0] >= L'0') && (Identifier[0] <= L'9')) {
745 Number = (INTN) Atoi(Identifier);
746 } else if (IsGuid(Identifier)) {
747 IdIsGuid = TRUE;
748 }
749 while ((i < VolumesCount) && (!Found)) {
750 if (Number >= 0) { // User specified a volume by number
751 if (Volumes[i]->IsReadable) {
752 if (CountedVolumes == Number) {
753 *Volume = Volumes[i];
754 Found = TRUE;
755 }
756 CountedVolumes++;
757 } // if
758 } else { // User specified a volume by label or GUID
759 if (MyStriCmp(Identifier, Volumes[i]->VolName) || MyStriCmp(Identifier, Volumes[i]->PartName)) {
760 *Volume = Volumes[i];
761 Found = TRUE;
762 } // if
763 if (IdIsGuid && !Found) {
764 if (GuidsAreEqual(&VolGuid, &(Volumes[i]->PartGuid)) && !GuidsAreEqual(&NullGuid, &(Volumes[i]->PartGuid))) {
765 *Volume = Volumes[i];
766 Found = TRUE;
767 } // if
768 } // if
769 } // if/else
770 i++;
771 } // while()
772 return (Found);
773 } // static VOID FindVolume()
774
775 static VOID AddSubmenu(LOADER_ENTRY *Entry, REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
776 REFIT_MENU_SCREEN *SubScreen;
777 LOADER_ENTRY *SubEntry;
778 UINTN TokenCount;
779 CHAR16 **TokenList;
780
781 SubScreen = InitializeSubScreen(Entry);
782
783 // Set defaults for the new entry; will be modified based on lines read from the config. file....
784 SubEntry = InitializeLoaderEntry(Entry);
785
786 if ((SubEntry == NULL) || (SubScreen == NULL))
787 return;
788 SubEntry->me.Title = StrDuplicate(Title);
789
790 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
791
792 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
793 MyFreePool(SubEntry->LoaderPath);
794 SubEntry->LoaderPath = StrDuplicate(TokenList[1]);
795 SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
796
797 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
798 if (FindVolume(&Volume, TokenList[1])) {
799 if ((Volume != NULL) && (Volume->IsReadable) && (Volume->RootDir)) {
800 MyFreePool(SubEntry->me.Title);
801 SubEntry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
802 SPrint(SubEntry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", Volume->VolName);
803 SubEntry->me.BadgeImage = Volume->VolBadgeImage;
804 SubEntry->VolName = Volume->VolName;
805 } // if volume is readable
806 } // if match found
807
808 } else if (MyStriCmp(TokenList[0], L"initrd")) {
809 MyFreePool(SubEntry->InitrdPath);
810 SubEntry->InitrdPath = NULL;
811 if (TokenCount > 1) {
812 SubEntry->InitrdPath = StrDuplicate(TokenList[1]);
813 }
814
815 } else if (MyStriCmp(TokenList[0], L"options")) {
816 MyFreePool(SubEntry->LoadOptions);
817 SubEntry->LoadOptions = NULL;
818 if (TokenCount > 1) {
819 SubEntry->LoadOptions = StrDuplicate(TokenList[1]);
820 } // if/else
821
822 } else if (MyStriCmp(TokenList[0], L"add_options") && (TokenCount > 1)) {
823 MergeStrings(&SubEntry->LoadOptions, TokenList[1], L' ');
824
825 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
826 SubEntry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
827
828 } else if (MyStriCmp(TokenList[0], L"disabled")) {
829 SubEntry->Enabled = FALSE;
830 } // ief/elseif
831
832 FreeTokenLine(&TokenList, &TokenCount);
833 } // while()
834
835 if (SubEntry->InitrdPath != NULL) {
836 MergeStrings(&SubEntry->LoadOptions, L"initrd=", L' ');
837 MergeStrings(&SubEntry->LoadOptions, SubEntry->InitrdPath, 0);
838 MyFreePool(SubEntry->InitrdPath);
839 SubEntry->InitrdPath = NULL;
840 } // if
841 if (SubEntry->Enabled == TRUE) {
842 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
843 }
844 Entry->me.SubScreen = SubScreen;
845 } // VOID AddSubmenu()
846
847 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
848 // that entry. The calling function is then responsible for adding the entry to the
849 // list of entries.
850 static LOADER_ENTRY * AddStanzaEntries(REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
851 CHAR16 **TokenList;
852 UINTN TokenCount;
853 LOADER_ENTRY *Entry;
854 BOOLEAN DefaultsSet = FALSE, AddedSubmenu = FALSE;
855 REFIT_VOLUME *CurrentVolume = Volume;
856
857 // prepare the menu entry
858 Entry = InitializeLoaderEntry(NULL);
859 if (Entry == NULL)
860 return NULL;
861
862 Entry->Title = StrDuplicate(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.Row = 0;
866 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
867 Entry->VolName = CurrentVolume->VolName;
868
869 // Parse the config file to add options for a single stanza, terminating when the token
870 // is "}" or when the end of file is reached.
871 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StrCmp(TokenList[0], L"}") != 0)) {
872 if (MyStriCmp(TokenList[0], L"loader") && (TokenCount > 1)) { // set the boot loader filename
873 Entry->LoaderPath = StrDuplicate(TokenList[1]);
874 Entry->DevicePath = FileDevicePath(CurrentVolume->DeviceHandle, Entry->LoaderPath);
875 SetLoaderDefaults(Entry, TokenList[1], CurrentVolume);
876 MyFreePool(Entry->LoadOptions);
877 Entry->LoadOptions = NULL; // Discard default options, if any
878 DefaultsSet = TRUE;
879
880 } else if (MyStriCmp(TokenList[0], L"volume") && (TokenCount > 1)) {
881 if (FindVolume(&CurrentVolume, TokenList[1])) {
882 if ((CurrentVolume != NULL) && (CurrentVolume->IsReadable) && (CurrentVolume->RootDir)) {
883 MyFreePool(Entry->me.Title);
884 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
885 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
886 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
887 Entry->VolName = CurrentVolume->VolName;
888 } // if volume is readable
889 } // if match found
890
891 } else if (MyStriCmp(TokenList[0], L"icon") && (TokenCount > 1)) {
892 MyFreePool(Entry->me.Image);
893 Entry->me.Image = egLoadIcon(CurrentVolume->RootDir, TokenList[1], GlobalConfig.IconSizes[ICON_SIZE_BIG]);
894 if (Entry->me.Image == NULL) {
895 Entry->me.Image = DummyImage(GlobalConfig.IconSizes[ICON_SIZE_BIG]);
896 }
897
898 } else if (MyStriCmp(TokenList[0], L"initrd") && (TokenCount > 1)) {
899 MyFreePool(Entry->InitrdPath);
900 Entry->InitrdPath = StrDuplicate(TokenList[1]);
901
902 } else if (MyStriCmp(TokenList[0], L"options") && (TokenCount > 1)) {
903 MyFreePool(Entry->LoadOptions);
904 Entry->LoadOptions = StrDuplicate(TokenList[1]);
905
906 } else if (MyStriCmp(TokenList[0], L"ostype") && (TokenCount > 1)) {
907 if (TokenCount > 1) {
908 Entry->OSType = TokenList[1][0];
909 }
910
911 } else if (MyStriCmp(TokenList[0], L"graphics") && (TokenCount > 1)) {
912 Entry->UseGraphicsMode = MyStriCmp(TokenList[1], L"on");
913
914 } else if (MyStriCmp(TokenList[0], L"disabled")) {
915 Entry->Enabled = FALSE;
916
917 } else if (MyStriCmp(TokenList[0], L"submenuentry") && (TokenCount > 1)) {
918 AddSubmenu(Entry, File, CurrentVolume, TokenList[1]);
919 AddedSubmenu = TRUE;
920
921 } // set options to pass to the loader program
922 FreeTokenLine(&TokenList, &TokenCount);
923 } // while()
924
925 if (AddedSubmenu)
926 AddMenuEntry(Entry->me.SubScreen, &MenuEntryReturn);
927
928 if (Entry->InitrdPath) {
929 MergeStrings(&Entry->LoadOptions, L"initrd=", L' ');
930 MergeStrings(&Entry->LoadOptions, Entry->InitrdPath, 0);
931 MyFreePool(Entry->InitrdPath);
932 Entry->InitrdPath = NULL;
933 } // if
934
935 if (!DefaultsSet)
936 SetLoaderDefaults(Entry, L"\\EFI\\BOOT\\nemo.efi", CurrentVolume); // user included no "loader" line; use bogus one
937
938 return(Entry);
939 } // static VOID AddStanzaEntries()
940
941 // Read the user-configured menu entries from refind.conf and add or delete
942 // entries based on the contents of that file....
943 VOID ScanUserConfigured(CHAR16 *FileName)
944 {
945 EFI_STATUS Status;
946 REFIT_FILE File;
947 REFIT_VOLUME *Volume;
948 CHAR16 **TokenList;
949 CHAR16 *Title = NULL;
950 UINTN TokenCount, size;
951 LOADER_ENTRY *Entry;
952
953 if (FileExists(SelfDir, FileName)) {
954 Status = ReadFile(SelfDir, FileName, &File, &size);
955 if (EFI_ERROR(Status))
956 return;
957
958 Volume = SelfVolume;
959
960 while ((TokenCount = ReadTokenLine(&File, &TokenList)) > 0) {
961 if (MyStriCmp(TokenList[0], L"menuentry") && (TokenCount > 1)) {
962 Title = StrDuplicate(TokenList[1]);
963 Entry = AddStanzaEntries(&File, Volume, TokenList[1]);
964 if (Entry->Enabled) {
965 if (Entry->me.SubScreen == NULL)
966 GenerateSubScreen(Entry, Volume, TRUE);
967 AddPreparedLoaderEntry(Entry);
968 } else {
969 MyFreePool(Entry);
970 } // if/else
971 MyFreePool(Title);
972
973 } else if (MyStriCmp(TokenList[0], L"include") && (TokenCount == 2) &&
974 MyStriCmp(FileName, GlobalConfig.ConfigFilename)) {
975 if (!MyStriCmp(TokenList[1], FileName)) {
976 ScanUserConfigured(TokenList[1]);
977 }
978
979 } // if/else if...
980 FreeTokenLine(&TokenList, &TokenCount);
981 } // while()
982 } // if()
983 } // VOID ScanUserConfigured()
984
985 // Create an options file based on /etc/fstab. The resulting file has two options
986 // lines, one of which boots the system with "ro root={rootfs}" and the other of
987 // which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
988 // filesystem identifier associated with the "/" line in /etc/fstab.
989 static REFIT_FILE * GenerateOptionsFromEtcFstab(REFIT_VOLUME *Volume) {
990 UINTN TokenCount, i;
991 REFIT_FILE *Options = NULL, *Fstab = NULL;
992 EFI_STATUS Status;
993 CHAR16 **TokenList, *Line, Root[100];
994
995 if (FileExists(Volume->RootDir, L"\\etc\\fstab")) {
996 Options = AllocateZeroPool(sizeof(REFIT_FILE));
997 Fstab = AllocateZeroPool(sizeof(REFIT_FILE));
998 Status = ReadFile(Volume->RootDir, L"\\etc\\fstab", Fstab, &i);
999 if (CheckError(Status, L"while reading /etc/fstab")) {
1000 if (Options != NULL)
1001 FreePool(Options);
1002 if (Fstab != NULL)
1003 FreePool(Fstab);
1004 Options = NULL;
1005 Fstab = NULL;
1006 } else { // File read; locate root fs and create entries
1007 Options->Encoding = ENCODING_UTF16_LE;
1008 while ((TokenCount = ReadTokenLine(Fstab, &TokenList)) > 0) {
1009 if (TokenCount > 2) {
1010 Root[0] = '\0';
1011 if (StrCmp(TokenList[1], L"\\") == 0) {
1012 SPrint(Root, 99, L"%s", TokenList[0]);
1013 } else if (StrCmp(TokenList[2], L"\\") == 0) {
1014 SPrint(Root, 99, L"%s=%s", TokenList[0], TokenList[1]);
1015 } // if/elseif/elseif
1016 if (Root[0] != L'\0') {
1017 for (i = 0; i < StrLen(Root); i++)
1018 if (Root[i] == '\\')
1019 Root[i] = '/';
1020 Line = PoolPrint(L"\"Boot with normal options\" \"ro root=%s\"\n", Root);
1021 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1022 MyFreePool(Line);
1023 Line = PoolPrint(L"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root);
1024 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1025 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1026 } // if
1027 } // if
1028 FreeTokenLine(&TokenList, &TokenCount);
1029 } // while
1030
1031 if (Options->Buffer) {
1032 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1033 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1034 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1035 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1036 } else {
1037 MyFreePool(Options);
1038 Options = NULL;
1039 }
1040
1041 MyFreePool(Fstab->Buffer);
1042 MyFreePool(Fstab);
1043 } // if/else file read error
1044 } // if /etc/fstab exists
1045 return Options;
1046 } // GenerateOptionsFromEtcFstab()
1047
1048 // Create options from partition type codes. Specifically, if the earlier
1049 // partition scan found a partition with a type code corresponding to a root
1050 // filesystem according to the Freedesktop.org Discoverable Partitions Spec
1051 // (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/),
1052 // this function returns an appropriate file with two lines, one with
1053 // "ro root=/dev/disk/by-partuuid/{GUID}" and the other with that plus "single".
1054 // Note that this function returns the LAST partition found with the
1055 // appropriate type code, so this will work poorly on dual-boot systems or
1056 // if the type code is set incorrectly.
1057 static REFIT_FILE * GenerateOptionsFromPartTypes(VOID) {
1058 REFIT_FILE *Options = NULL;
1059 CHAR16 *Line, *GuidString, *WriteStatus;
1060
1061 if (GlobalConfig.DiscoveredRoot) {
1062 Options = AllocateZeroPool(sizeof(REFIT_FILE));
1063 if (Options) {
1064 Options->Encoding = ENCODING_UTF16_LE;
1065 GuidString = GuidAsString(&(GlobalConfig.DiscoveredRoot->PartGuid));
1066 WriteStatus = GlobalConfig.DiscoveredRoot->IsMarkedReadOnly ? L"ro" : L"rw";
1067 ToLower(GuidString);
1068 if (GuidString) {
1069 Line = PoolPrint(L"\"Boot with normal options\" \"%s root=/dev/disk/by-partuuid/%s\"\n", WriteStatus, GuidString);
1070 MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
1071 MyFreePool(Line);
1072 Line = PoolPrint(L"\"Boot into single-user mode\" \"%s root=/dev/disk/by-partuuid/%s single\"\n", WriteStatus, GuidString);
1073 MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
1074 MyFreePool(Line);
1075 MyFreePool(GuidString);
1076 } // if (GuidString)
1077 Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
1078
1079 Options->Current8Ptr = (CHAR8 *)Options->Buffer;
1080 Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
1081 Options->Current16Ptr = (CHAR16 *)Options->Buffer;
1082 Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
1083 } // if (Options allocated OK)
1084 } // if (partition has root GUID)
1085 return Options;
1086 } // REFIT_FILE * GenerateOptionsFromPartTypes()
1087
1088 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
1089 // and Volume variables identify the location of the options file, but not its name --
1090 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
1091 // file in the target directory, and this function finds the file with a name in the
1092 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
1093 // directory and loads it. This function tries multiple files because I originally
1094 // used the filename linux.conf, but close on the heels of that decision, the Linux
1095 // kernel developers decided to use that name for a similar purpose, but with a
1096 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
1097 // but I want a migration period in which both names are used.
1098 // If a rEFInd options file can't be found, try to generate minimal options from
1099 // /etc/fstab on the same volume as the kernel. This typically works only if the
1100 // kernel is being read from the Linux root filesystem.
1101 //
1102 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
1103 // it wasn't found.
1104 REFIT_FILE * ReadLinuxOptionsFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1105 CHAR16 *OptionsFilename, *FullFilename;
1106 BOOLEAN GoOn = TRUE, FileFound = FALSE;
1107 UINTN i = 0, size;
1108 REFIT_FILE *File = NULL;
1109 EFI_STATUS Status;
1110
1111 do {
1112 OptionsFilename = FindCommaDelimited(LINUX_OPTIONS_FILENAMES, i++);
1113 FullFilename = FindPath(LoaderPath);
1114 if ((OptionsFilename != NULL) && (FullFilename != NULL)) {
1115 MergeStrings(&FullFilename, OptionsFilename, '\\');
1116 if (FileExists(Volume->RootDir, FullFilename)) {
1117 File = AllocateZeroPool(sizeof(REFIT_FILE));
1118 Status = ReadFile(Volume->RootDir, FullFilename, File, &size);
1119 if (CheckError(Status, L"while loading the Linux options file")) {
1120 if (File != NULL)
1121 FreePool(File);
1122 File = NULL;
1123 } else {
1124 GoOn = FALSE;
1125 FileFound = TRUE;
1126 } // if/else error
1127 } // if file exists
1128 } else { // a filename string is NULL
1129 GoOn = FALSE;
1130 } // if/else
1131 MyFreePool(OptionsFilename);
1132 MyFreePool(FullFilename);
1133 OptionsFilename = FullFilename = NULL;
1134 } while (GoOn);
1135 if (!FileFound) {
1136 // No refind_linux.conf file; look for /etc/fstab and try to pull values from there....
1137 File = GenerateOptionsFromEtcFstab(Volume);
1138 // If still no joy, try to use Freedesktop.org Discoverable Partitions Spec....
1139 if (!File)
1140 File = GenerateOptionsFromPartTypes();
1141 } // if
1142 return (File);
1143 } // static REFIT_FILE * ReadLinuxOptionsFile()
1144
1145 // Retrieve a single line of options from a Linux kernel options file
1146 CHAR16 * GetFirstOptionsFromFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
1147 UINTN TokenCount;
1148 CHAR16 *Options = NULL;
1149 CHAR16 **TokenList;
1150 REFIT_FILE *File;
1151
1152 File = ReadLinuxOptionsFile(LoaderPath, Volume);
1153 if (File != NULL) {
1154 TokenCount = ReadTokenLine(File, &TokenList);
1155 if (TokenCount > 1)
1156 Options = StrDuplicate(TokenList[1]);
1157 FreeTokenLine(&TokenList, &TokenCount);
1158 FreePool(File);
1159 } // if
1160 return Options;
1161 } // static CHAR16 * GetOptionsFile()
1162