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