]> code.delx.au - refind/blob - refind/config.c
Added support for "volume" token in "submenuentry" items in manual
[refind] / refind / config.c
1 /*
2 * refit/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 Roderick W. Smith
39 *
40 * Modifications distributed under the terms of the GNU General Public
41 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
42 * with this source code or binaries made from it.
43 *
44 */
45
46 #include "global.h"
47 #include "lib.h"
48 #include "icns.h"
49 #include "menu.h"
50 #include "config.h"
51 #include "screen.h"
52 #include "../include/refit_call_wrapper.h"
53
54 // constants
55
56 #define CONFIG_FILE_NAME L"refind.conf"
57 #define LINUX_OPTIONS_FILENAMES L"refind_linux.conf,refind-linux.conf"
58 #define MAXCONFIGFILESIZE (128*1024)
59
60 #define ENCODING_ISO8859_1 (0)
61 #define ENCODING_UTF8 (1)
62 #define ENCODING_UTF16_LE (2)
63
64 static REFIT_MENU_ENTRY MenuEntryReturn = { L"Return to Main Menu", TAG_RETURN, 0, 0, 0, NULL, NULL, NULL };
65
66 //
67 // read a file into a buffer
68 //
69
70 static EFI_STATUS ReadFile(IN EFI_FILE_HANDLE BaseDir, CHAR16 *FileName, REFIT_FILE *File)
71 {
72 EFI_STATUS Status;
73 EFI_FILE_HANDLE FileHandle;
74 EFI_FILE_INFO *FileInfo;
75 UINT64 ReadSize;
76
77 File->Buffer = NULL;
78 File->BufferSize = 0;
79
80 // read the file, allocating a buffer on the way
81 Status = refit_call5_wrapper(BaseDir->Open, BaseDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
82 if (CheckError(Status, L"while loading the configuration file"))
83 return Status;
84
85 FileInfo = LibFileInfo(FileHandle);
86 if (FileInfo == NULL) {
87 // TODO: print and register the error
88 refit_call1_wrapper(FileHandle->Close, FileHandle);
89 return EFI_LOAD_ERROR;
90 }
91 ReadSize = FileInfo->FileSize;
92 if (ReadSize > MAXCONFIGFILESIZE)
93 ReadSize = MAXCONFIGFILESIZE;
94 FreePool(FileInfo);
95
96 File->BufferSize = (UINTN)ReadSize; // was limited to a few K before, so this is safe
97 File->Buffer = AllocatePool(File->BufferSize);
98 Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &File->BufferSize, File->Buffer);
99 if (CheckError(Status, L"while loading the configuration file")) {
100 MyFreePool(File->Buffer);
101 File->Buffer = NULL;
102 refit_call1_wrapper(FileHandle->Close, FileHandle);
103 return Status;
104 }
105 Status = refit_call1_wrapper(FileHandle->Close, FileHandle);
106
107 // setup for reading
108 File->Current8Ptr = (CHAR8 *)File->Buffer;
109 File->End8Ptr = File->Current8Ptr + File->BufferSize;
110 File->Current16Ptr = (CHAR16 *)File->Buffer;
111 File->End16Ptr = File->Current16Ptr + (File->BufferSize >> 1);
112
113 // detect encoding
114 File->Encoding = ENCODING_ISO8859_1; // default: 1:1 translation of CHAR8 to CHAR16
115 if (File->BufferSize >= 4) {
116 if (File->Buffer[0] == 0xFF && File->Buffer[1] == 0xFE) {
117 // BOM in UTF-16 little endian (or UTF-32 little endian)
118 File->Encoding = ENCODING_UTF16_LE; // use CHAR16 as is
119 File->Current16Ptr++;
120 } else if (File->Buffer[0] == 0xEF && File->Buffer[1] == 0xBB && File->Buffer[2] == 0xBF) {
121 // BOM in UTF-8
122 File->Encoding = ENCODING_UTF8; // translate from UTF-8 to UTF-16
123 File->Current8Ptr += 3;
124 } else if (File->Buffer[1] == 0 && File->Buffer[3] == 0) {
125 File->Encoding = ENCODING_UTF16_LE; // use CHAR16 as is
126 }
127 // TODO: detect other encodings as they are implemented
128 }
129
130 return EFI_SUCCESS;
131 }
132
133 //
134 // get a single line of text from a file
135 //
136
137 static CHAR16 *ReadLine(REFIT_FILE *File)
138 {
139 CHAR16 *Line, *q;
140 UINTN LineLength;
141
142 if (File->Buffer == NULL)
143 return NULL;
144
145 if (File->Encoding == ENCODING_ISO8859_1 || File->Encoding == ENCODING_UTF8) {
146
147 CHAR8 *p, *LineStart, *LineEnd;
148
149 p = File->Current8Ptr;
150 if (p >= File->End8Ptr)
151 return NULL;
152
153 LineStart = p;
154 for (; p < File->End8Ptr; p++)
155 if (*p == 13 || *p == 10)
156 break;
157 LineEnd = p;
158 for (; p < File->End8Ptr; p++)
159 if (*p != 13 && *p != 10)
160 break;
161 File->Current8Ptr = p;
162
163 LineLength = (UINTN)(LineEnd - LineStart) + 1;
164 Line = AllocatePool(LineLength * sizeof(CHAR16));
165 if (Line == NULL)
166 return NULL;
167
168 q = Line;
169 if (File->Encoding == ENCODING_ISO8859_1) {
170 for (p = LineStart; p < LineEnd; )
171 *q++ = *p++;
172 } else if (File->Encoding == ENCODING_UTF8) {
173 // TODO: actually handle UTF-8
174 for (p = LineStart; p < LineEnd; )
175 *q++ = *p++;
176 }
177 *q = 0;
178
179 } else if (File->Encoding == ENCODING_UTF16_LE) {
180
181 CHAR16 *p, *LineStart, *LineEnd;
182
183 p = File->Current16Ptr;
184 if (p >= File->End16Ptr)
185 return NULL;
186
187 LineStart = p;
188 for (; p < File->End16Ptr; p++)
189 if (*p == 13 || *p == 10)
190 break;
191 LineEnd = p;
192 for (; p < File->End16Ptr; p++)
193 if (*p != 13 && *p != 10)
194 break;
195 File->Current16Ptr = p;
196
197 LineLength = (UINTN)(LineEnd - LineStart) + 1;
198 Line = AllocatePool(LineLength * sizeof(CHAR16));
199 if (Line == NULL)
200 return NULL;
201
202 for (p = LineStart, q = Line; p < LineEnd; )
203 *q++ = *p++;
204 *q = 0;
205
206 } else
207 return NULL; // unsupported encoding
208
209 return Line;
210 }
211
212 //
213 // get a line of tokens from a file
214 //
215
216 UINTN ReadTokenLine(IN REFIT_FILE *File, OUT CHAR16 ***TokenList)
217 {
218 BOOLEAN LineFinished, IsQuoted = FALSE;
219 CHAR16 *Line, *Token, *p;
220 UINTN TokenCount = 0;
221
222 *TokenList = NULL;
223
224 while (TokenCount == 0) {
225 Line = ReadLine(File);
226 if (Line == NULL)
227 return(0);
228
229 p = Line;
230 LineFinished = FALSE;
231 while (!LineFinished) {
232 // skip whitespace
233 while ((*p == ' ' || *p == '\t' || *p == '=' || *p == ',') && !IsQuoted)
234 p++;
235 if (*p == 0 || *p == '#')
236 break;
237
238 if (*p == '"') {
239 IsQuoted = !IsQuoted;
240 p++;
241 } // if
242 Token = p;
243
244 // find end of token
245 while (*p && *p != '"' && ((*p != ' ' && *p != '\t' && *p != '=' && *p != '#' && *p != ',') || IsQuoted)) {
246 if ((*p == '/') && !IsQuoted) // Switch Unix-style to DOS-style directory separators
247 *p = '\\';
248 p++;
249 } // if
250 if (*p == '"')
251 IsQuoted = !IsQuoted;
252 if (*p == 0 || *p == '#')
253 LineFinished = TRUE;
254 *p++ = 0;
255
256 AddListElement((VOID ***)TokenList, &TokenCount, (VOID *)StrDuplicate(Token));
257 }
258
259 FreePool(Line);
260 }
261 return (TokenCount);
262 } /* ReadTokenLine() */
263
264 VOID FreeTokenLine(IN OUT CHAR16 ***TokenList, IN OUT UINTN *TokenCount)
265 {
266 // TODO: also free the items
267 FreeList((VOID ***)TokenList, TokenCount);
268 }
269
270 // handle a parameter with a single integer argument
271 static VOID HandleInt(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT UINTN *Value)
272 {
273 if (TokenCount == 2)
274 *Value = Atoi(TokenList[1]);
275 }
276
277 // handle a parameter with a single string argument
278 static VOID HandleString(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT CHAR16 **Target) {
279 if (TokenCount == 2) {
280 MyFreePool(*Target);
281 *Target = StrDuplicate(TokenList[1]);
282 } // if
283 } // static VOID HandleString()
284
285 // Handle a parameter with a series of string arguments, to be added to a comma-delimited
286 // list. Passes each token through the CleanUpPathNameSlashes() function to ensure
287 // consistency in subsequent comparisons of filenames.
288 static VOID HandleStrings(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT CHAR16 **Target) {
289 UINTN i;
290
291 if (*Target != NULL) {
292 FreePool(*Target);
293 *Target = NULL;
294 } // if
295 for (i = 1; i < TokenCount; i++) {
296 CleanUpPathNameSlashes(TokenList[i]);
297 MergeStrings(Target, TokenList[i], L',');
298 }
299 } // static VOID HandleStrings()
300
301 // read config file
302 VOID ReadConfig(VOID)
303 {
304 EFI_STATUS Status;
305 REFIT_FILE File;
306 CHAR16 **TokenList;
307 CHAR16 *FlagName;
308 UINTN TokenCount, i;
309
310 if (!FileExists(SelfDir, CONFIG_FILE_NAME)) {
311 Print(L"Configuration file missing!\n");
312 return;
313 }
314
315 Status = ReadFile(SelfDir, CONFIG_FILE_NAME, &File);
316 if (EFI_ERROR(Status))
317 return;
318
319 for (;;) {
320 TokenCount = ReadTokenLine(&File, &TokenList);
321 if (TokenCount == 0)
322 break;
323
324 if (StriCmp(TokenList[0], L"timeout") == 0) {
325 HandleInt(TokenList, TokenCount, &(GlobalConfig.Timeout));
326
327 } else if (StriCmp(TokenList[0], L"hideui") == 0) {
328 for (i = 1; i < TokenCount; i++) {
329 FlagName = TokenList[i];
330 if (StriCmp(FlagName, L"banner") == 0) {
331 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BANNER;
332 } else if (StriCmp(FlagName, L"label") == 0) {
333 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_LABEL;
334 } else if (StriCmp(FlagName, L"singleuser") == 0) {
335 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_SINGLEUSER;
336 } else if (StriCmp(FlagName, L"hwtest") == 0) {
337 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_HWTEST;
338 } else if (StriCmp(FlagName, L"arrows") == 0) {
339 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_ARROWS;
340 } else if (StriCmp(FlagName, L"all") == 0) {
341 GlobalConfig.HideUIFlags = HIDEUI_ALL;
342 } else {
343 Print(L" unknown hideui flag: '%s'\n", FlagName);
344 }
345 }
346
347 } else if (StriCmp(TokenList[0], L"icons_dir") == 0) {
348 HandleString(TokenList, TokenCount, &(GlobalConfig.IconsDir));
349
350 } else if (StriCmp(TokenList[0], L"scanfor") == 0) {
351 for (i = 0; i < NUM_SCAN_OPTIONS; i++) {
352 if (i < TokenCount)
353 GlobalConfig.ScanFor[i] = TokenList[i][0];
354 else
355 GlobalConfig.ScanFor[i] = ' ';
356 }
357
358 } else if ((StriCmp(TokenList[0], L"scan_delay") == 0) && (TokenCount == 2)) {
359 HandleInt(TokenList, TokenCount, &(GlobalConfig.ScanDelay));
360
361 } else if (StriCmp(TokenList[0], L"also_scan_dirs") == 0) {
362 HandleStrings(TokenList, TokenCount, &(GlobalConfig.AlsoScan));
363
364 } else if ((StriCmp(TokenList[0], L"don't_scan_dirs") == 0) || (StriCmp(TokenList[0], L"dont_scan_dirs") == 0)) {
365 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DontScan));
366
367 } else if (StriCmp(TokenList[0], L"scan_driver_dirs") == 0) {
368 HandleStrings(TokenList, TokenCount, &(GlobalConfig.DriverDirs));
369
370 } else if (StriCmp(TokenList[0], L"showtools") == 0) {
371 SetMem(GlobalConfig.ShowTools, NUM_TOOLS * sizeof(UINTN), 0);
372 for (i = 1; (i < TokenCount) && (i < NUM_TOOLS); i++) {
373 FlagName = TokenList[i];
374 if (StriCmp(FlagName, L"shell") == 0) {
375 GlobalConfig.ShowTools[i - 1] = TAG_SHELL;
376 } else if (StriCmp(FlagName, L"gptsync") == 0) {
377 GlobalConfig.ShowTools[i - 1] = TAG_GPTSYNC;
378 } else if (StriCmp(FlagName, L"about") == 0) {
379 GlobalConfig.ShowTools[i - 1] = TAG_ABOUT;
380 } else if (StriCmp(FlagName, L"exit") == 0) {
381 GlobalConfig.ShowTools[i - 1] = TAG_EXIT;
382 } else if (StriCmp(FlagName, L"reboot") == 0) {
383 GlobalConfig.ShowTools[i - 1] = TAG_REBOOT;
384 } else if (StriCmp(FlagName, L"shutdown") == 0) {
385 GlobalConfig.ShowTools[i - 1] = TAG_SHUTDOWN;
386 } else {
387 Print(L" unknown showtools flag: '%s'\n", FlagName);
388 }
389 } // showtools options
390
391 } else if (StriCmp(TokenList[0], L"banner") == 0) {
392 HandleString(TokenList, TokenCount, &(GlobalConfig.BannerFileName));
393
394 } else if (StriCmp(TokenList[0], L"selection_small") == 0) {
395 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionSmallFileName));
396
397 } else if (StriCmp(TokenList[0], L"selection_big") == 0) {
398 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionBigFileName));
399
400 } else if (StriCmp(TokenList[0], L"default_selection") == 0) {
401 HandleString(TokenList, TokenCount, &(GlobalConfig.DefaultSelection));
402
403 } else if (StriCmp(TokenList[0], L"textonly") == 0) {
404 GlobalConfig.TextOnly = TRUE;
405
406 } else if ((StriCmp(TokenList[0], L"resolution") == 0) && (TokenCount == 3)) {
407 GlobalConfig.RequestedScreenWidth = Atoi(TokenList[1]);
408 GlobalConfig.RequestedScreenHeight = Atoi(TokenList[2]);
409
410 } else if (StriCmp(TokenList[0], L"use_graphics_for") == 0) {
411 GlobalConfig.GraphicsFor = 0;
412 for (i = 1; i < TokenCount; i++) {
413 if (StriCmp(TokenList[i], L"osx") == 0) {
414 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_OSX;
415 } else if (StriCmp(TokenList[i], L"linux") == 0) {
416 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_LINUX;
417 } else if (StriCmp(TokenList[i], L"elilo") == 0) {
418 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_ELILO;
419 } else if (StriCmp(TokenList[i], L"grub") == 0) {
420 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_GRUB;
421 } else if (StriCmp(TokenList[i], L"windows") == 0) {
422 GlobalConfig.GraphicsFor |= GRAPHICS_FOR_WINDOWS;
423 }
424 } // for (graphics_on tokens)
425
426 } else if (StriCmp(TokenList[0], L"scan_all_linux_kernels") == 0) {
427 GlobalConfig.ScanAllLinux = TRUE;
428
429 } else if (StriCmp(TokenList[0], L"max_tags") == 0) {
430 HandleInt(TokenList, TokenCount, &(GlobalConfig.MaxTags));
431
432 }
433
434 FreeTokenLine(&TokenList, &TokenCount);
435 }
436 MyFreePool(File.Buffer);
437 } /* VOID ReadConfig() */
438
439 // Finds a volume with the specified Identifier (a volume label or a number
440 // followed by a colon, for the moment). If found, sets *Volume to point to
441 // that volume. If not, leaves it unchanged.
442 // Returns TRUE if a match was found, FALSE if not.
443 static BOOLEAN FindVolume(REFIT_VOLUME **Volume, CHAR16 *Identifier) {
444 UINTN i = 0, CountedVolumes = 0;
445 INTN Number = -1;
446 BOOLEAN Found = FALSE;
447
448 if ((StrLen(Identifier) >= 2) && (Identifier[StrLen(Identifier) - 1] == L':') &&
449 (Identifier[0] >= L'0') && (Identifier[0] <= L'9')) {
450 Number = (INTN) Atoi(Identifier);
451 }
452 while ((i < VolumesCount) && (!Found)) {
453 if (Number >= 0) { // User specified a volume by number
454 if (Volumes[i]->IsReadable) {
455 if (CountedVolumes == Number) {
456 *Volume = Volumes[i];
457 Found = TRUE;
458 }
459 CountedVolumes++;
460 } // if
461 } else { // User specified a volume by label
462 if (StriCmp(Identifier, Volumes[i]->VolName) == 0) {
463 *Volume = Volumes[i];
464 Found = TRUE;
465 } // if
466 } // if/else
467 i++;
468 } // while()
469 return (Found);
470 } // static VOID FindVolume()
471
472 static VOID AddSubmenu(LOADER_ENTRY *Entry, REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
473 REFIT_MENU_SCREEN *SubScreen;
474 LOADER_ENTRY *SubEntry;
475 UINTN TokenCount;
476 CHAR16 **TokenList;
477
478 SubScreen = InitializeSubScreen(Entry);
479
480 // Set defaults for the new entry; will be modified based on lines read from the config. file....
481 SubEntry = InitializeLoaderEntry(Entry);
482
483 if ((SubEntry == NULL) || (SubScreen == NULL))
484 return;
485 SubEntry->me.Title = StrDuplicate(Title);
486
487 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StriCmp(TokenList[0], L"}") != 0)) {
488
489 if ((StriCmp(TokenList[0], L"loader") == 0) && (TokenCount > 1)) { // set the boot loader filename
490 MyFreePool(SubEntry->LoaderPath);
491 SubEntry->LoaderPath = StrDuplicate(TokenList[1]);
492 SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
493
494 } else if ((StriCmp(TokenList[0], L"volume") == 0) && (TokenCount > 1)) {
495 if (FindVolume(&Volume, TokenList[1])) {
496 MyFreePool(SubEntry->me.Title);
497 SubEntry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
498 SPrint(SubEntry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", Volume->VolName);
499 SubEntry->me.BadgeImage = Volume->VolBadgeImage;
500 SubEntry->VolName = Volume->VolName;
501 } // if match found
502
503 } else if (StriCmp(TokenList[0], L"initrd") == 0) {
504 MyFreePool(SubEntry->InitrdPath);
505 SubEntry->InitrdPath = NULL;
506 if (TokenCount > 1) {
507 SubEntry->InitrdPath = StrDuplicate(TokenList[1]);
508 }
509
510 } else if (StriCmp(TokenList[0], L"options") == 0) {
511 MyFreePool(SubEntry->LoadOptions);
512 SubEntry->LoadOptions = NULL;
513 if (TokenCount > 1) {
514 SubEntry->LoadOptions = StrDuplicate(TokenList[1]);
515 } // if/else
516
517 } else if ((StriCmp(TokenList[0], L"add_options") == 0) && (TokenCount > 1)) {
518 MergeStrings(&SubEntry->LoadOptions, TokenList[1], L' ');
519
520 } else if ((StriCmp(TokenList[0], L"graphics") == 0) && (TokenCount > 1)) {
521 SubEntry->UseGraphicsMode = (StriCmp(TokenList[1], L"on") == 0);
522
523 } else if (StriCmp(TokenList[0], L"disabled") == 0) {
524 SubEntry->Enabled = FALSE;
525 } // ief/elseif
526
527 FreeTokenLine(&TokenList, &TokenCount);
528 } // while()
529
530 if (SubEntry->InitrdPath != NULL) {
531 MergeStrings(&SubEntry->LoadOptions, L"initrd=", L' ');
532 MergeStrings(&SubEntry->LoadOptions, SubEntry->InitrdPath, 0);
533 MyFreePool(SubEntry->InitrdPath);
534 SubEntry->InitrdPath = NULL;
535 } // if
536 if (SubEntry->Enabled == TRUE) {
537 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
538 }
539 Entry->me.SubScreen = SubScreen;
540 } // VOID AddSubmenu()
541
542 // Adds the options from a SINGLE refind.conf stanza to a new loader entry and returns
543 // that entry. The calling function is then responsible for adding the entry to the
544 // list of entries.
545 static LOADER_ENTRY * AddStanzaEntries(REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
546 CHAR16 **TokenList;
547 UINTN TokenCount;
548 LOADER_ENTRY *Entry;
549 BOOLEAN DefaultsSet = FALSE, AddedSubmenu = FALSE;
550 REFIT_VOLUME *CurrentVolume = Volume;
551
552 // prepare the menu entry
553 Entry = InitializeLoaderEntry(NULL);
554 if (Entry == NULL)
555 return NULL;
556
557 Entry->Title = StrDuplicate(Title);
558 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
559 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
560 Entry->me.Row = 0;
561 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
562 Entry->VolName = CurrentVolume->VolName;
563
564 // Parse the config file to add options for a single stanza, terminating when the token
565 // is "}" or when the end of file is reached.
566 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StriCmp(TokenList[0], L"}") != 0)) {
567 if ((StriCmp(TokenList[0], L"loader") == 0) && (TokenCount > 1)) { // set the boot loader filename
568 Entry->LoaderPath = StrDuplicate(TokenList[1]);
569 Entry->DevicePath = FileDevicePath(CurrentVolume->DeviceHandle, Entry->LoaderPath);
570 SetLoaderDefaults(Entry, TokenList[1], CurrentVolume);
571 MyFreePool(Entry->LoadOptions);
572 Entry->LoadOptions = NULL; // Discard default options, if any
573 DefaultsSet = TRUE;
574 } else if ((StriCmp(TokenList[0], L"volume") == 0) && (TokenCount > 1)) {
575 if (FindVolume(&CurrentVolume, TokenList[1])) {
576 MyFreePool(Entry->me.Title);
577 Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
578 SPrint(Entry->me.Title, 255, L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", CurrentVolume->VolName);
579 Entry->me.BadgeImage = CurrentVolume->VolBadgeImage;
580 Entry->VolName = CurrentVolume->VolName;
581 } // if match found
582 } else if ((StriCmp(TokenList[0], L"icon") == 0) && (TokenCount > 1)) {
583 MyFreePool(Entry->me.Image);
584 Entry->me.Image = LoadIcns(CurrentVolume->RootDir, TokenList[1], 128);
585 if (Entry->me.Image == NULL) {
586 Entry->me.Image = DummyImage(128);
587 }
588 } else if ((StriCmp(TokenList[0], L"initrd") == 0) && (TokenCount > 1)) {
589 MyFreePool(Entry->InitrdPath);
590 Entry->InitrdPath = StrDuplicate(TokenList[1]);
591 } else if ((StriCmp(TokenList[0], L"options") == 0) && (TokenCount > 1)) {
592 MyFreePool(Entry->LoadOptions);
593 Entry->LoadOptions = StrDuplicate(TokenList[1]);
594 } else if ((StriCmp(TokenList[0], L"ostype") == 0) && (TokenCount > 1)) {
595 if (TokenCount > 1) {
596 Entry->OSType = TokenList[1][0];
597 }
598 } else if ((StriCmp(TokenList[0], L"graphics") == 0) && (TokenCount > 1)) {
599 Entry->UseGraphicsMode = (StriCmp(TokenList[1], L"on") == 0);
600 } else if (StriCmp(TokenList[0], L"disabled") == 0) {
601 Entry->Enabled = FALSE;
602 } else if ((StriCmp(TokenList[0], L"submenuentry") == 0) && (TokenCount > 1)) {
603 AddSubmenu(Entry, File, CurrentVolume, TokenList[1]);
604 AddedSubmenu = TRUE;
605 } // set options to pass to the loader program
606 FreeTokenLine(&TokenList, &TokenCount);
607 } // while()
608
609 if (AddedSubmenu)
610 AddMenuEntry(Entry->me.SubScreen, &MenuEntryReturn);
611
612 if (Entry->InitrdPath) {
613 MergeStrings(&Entry->LoadOptions, L"initrd=", L' ');
614 MergeStrings(&Entry->LoadOptions, Entry->InitrdPath, 0);
615 MyFreePool(Entry->InitrdPath);
616 Entry->InitrdPath = NULL;
617 } // if
618
619 if (!DefaultsSet)
620 SetLoaderDefaults(Entry, L"\\EFI\\BOOT\\nemo.efi", CurrentVolume); // user included no entry; use bogus one
621
622 return(Entry);
623 } // static VOID AddStanzaEntries()
624
625 // Read the user-configured loaders file, refind_loaders.conf, and add or delete
626 // entries based on the contents of that file....
627 VOID ScanUserConfigured(VOID)
628 {
629 EFI_STATUS Status;
630 REFIT_FILE File;
631 REFIT_VOLUME *Volume;
632 CHAR16 **TokenList;
633 CHAR16 *Title = NULL;
634 UINTN TokenCount;
635 LOADER_ENTRY *Entry;
636
637 if (FileExists(SelfDir, CONFIG_FILE_NAME)) {
638 Status = ReadFile(SelfDir, CONFIG_FILE_NAME, &File);
639 if (EFI_ERROR(Status))
640 return;
641
642 Volume = SelfVolume;
643
644 while ((TokenCount = ReadTokenLine(&File, &TokenList)) > 0) {
645 if ((StriCmp(TokenList[0], L"menuentry") == 0) && (TokenCount > 1)) {
646 Title = StrDuplicate(TokenList[1]);
647 Entry = AddStanzaEntries(&File, Volume, TokenList[1]);
648 if (Entry->Enabled) {
649 if (Entry->me.SubScreen == NULL)
650 GenerateSubScreen(Entry, Volume);
651 AddPreparedLoaderEntry(Entry);
652 } else {
653 MyFreePool(Entry);
654 } // if/else
655 MyFreePool(Title);
656 } // if
657 FreeTokenLine(&TokenList, &TokenCount);
658 } // while()
659 } // if()
660 } // VOID ScanUserConfigured()
661
662 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
663 // and Volume variables identify the location of the options file, but not its name --
664 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
665 // file in the target directory, and this function finds the file with a name in the
666 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
667 // directory and loads it. This function tries multiple files because I originally
668 // used the filename linux.conf, but close on the heels of that decision, the Linux
669 // kernel developers decided to use that name for a similar purpose, but with a
670 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
671 // but I want a migration period in which both names are used.
672 //
673 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
674 // it wasn't found.
675 REFIT_FILE * ReadLinuxOptionsFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
676 CHAR16 *OptionsFilename, *FullFilename;
677 BOOLEAN GoOn = TRUE;
678 UINTN i = 0;
679 REFIT_FILE *File = NULL;
680 EFI_STATUS Status;
681
682 do {
683 OptionsFilename = FindCommaDelimited(LINUX_OPTIONS_FILENAMES, i++);
684 FullFilename = FindPath(LoaderPath);
685 if ((OptionsFilename != NULL) && (FullFilename != NULL)) {
686 MergeStrings(&FullFilename, OptionsFilename, '\\');
687 if (FileExists(Volume->RootDir, FullFilename)) {
688 File = AllocateZeroPool(sizeof(REFIT_FILE));
689 Status = ReadFile(Volume->RootDir, FullFilename, File);
690 GoOn = FALSE;
691 if (CheckError(Status, L"while loading the Linux options file")) {
692 if (File != NULL)
693 FreePool(File);
694 File = NULL;
695 GoOn = TRUE;
696 } // if error
697 } // if file exists
698 } else { // a filename string is NULL
699 GoOn = FALSE;
700 } // if/else
701 MyFreePool(OptionsFilename);
702 MyFreePool(FullFilename);
703 OptionsFilename = FullFilename = NULL;
704 } while (GoOn);
705 return (File);
706 } // static REFIT_FILE * ReadLinuxOptionsFile()
707
708 // Retrieve a single line of options from a Linux kernel options file
709 CHAR16 * GetFirstOptionsFromFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
710 UINTN TokenCount;
711 CHAR16 *Options = NULL;
712 CHAR16 **TokenList;
713 REFIT_FILE *File;
714
715 File = ReadLinuxOptionsFile(LoaderPath, Volume);
716 if (File != NULL) {
717 TokenCount = ReadTokenLine(File, &TokenList);
718 if (TokenCount > 1)
719 Options = StrDuplicate(TokenList[1]);
720 FreeTokenLine(&TokenList, &TokenCount);
721 FreePool(File);
722 } // if
723 return Options;
724 } // static CHAR16 * GetOptionsFile()
725