]> code.delx.au - refind/blob - refind/config.c
Version 0.2.3
[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 "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,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 FreePool(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 //
271 // handle a parameter with a single integer argument
272 //
273
274 static VOID HandleInt(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT UINTN *Value)
275 {
276 if (TokenCount < 2) {
277 return;
278 }
279 if (TokenCount > 2) {
280 return;
281 }
282 *Value = Atoi(TokenList[1]);
283 }
284
285 //
286 // handle a parameter with a single string argument
287 //
288
289 static VOID HandleString(IN CHAR16 **TokenList, IN UINTN TokenCount, OUT CHAR16 **Value)
290 {
291 if (TokenCount < 2) {
292 return;
293 }
294 if (TokenCount > 2) {
295 return;
296 }
297 *Value = StrDuplicate(TokenList[1]);
298 }
299
300 //
301 // read config file
302 //
303
304 VOID ReadConfig(VOID)
305 {
306 EFI_STATUS Status;
307 REFIT_FILE File;
308 CHAR16 **TokenList;
309 CHAR16 *FlagName;
310 UINTN TokenCount, i;
311
312 if (!FileExists(SelfDir, CONFIG_FILE_NAME)) {
313 Print(L"Configuration file missing!\n");
314 return;
315 }
316
317 Status = ReadFile(SelfDir, CONFIG_FILE_NAME, &File);
318 if (EFI_ERROR(Status))
319 return;
320
321 for (;;) {
322 TokenCount = ReadTokenLine(&File, &TokenList);
323 if (TokenCount == 0)
324 break;
325
326 if (StriCmp(TokenList[0], L"timeout") == 0) {
327 HandleInt(TokenList, TokenCount, &(GlobalConfig.Timeout));
328
329 } else if (StriCmp(TokenList[0], L"disable") == 0) {
330 for (i = 1; i < TokenCount; i++) {
331 FlagName = TokenList[i];
332 if (StriCmp(FlagName, L"shell") == 0) {
333 GlobalConfig.DisableFlags |= DISABLE_FLAG_SHELL;
334 } else if (StriCmp(FlagName, L"tools") == 0) {
335 GlobalConfig.DisableFlags |= DISABLE_FLAG_TOOLS;
336 } else if (StriCmp(FlagName, L"singleuser") == 0) {
337 GlobalConfig.DisableFlags |= DISABLE_FLAG_SINGLEUSER;
338 } else if (StriCmp(FlagName, L"hwtest") == 0) {
339 GlobalConfig.DisableFlags |= DISABLE_FLAG_HWTEST;
340 } else if (StriCmp(FlagName, L"all") == 0) {
341 GlobalConfig.DisableFlags = DISABLE_ALL;
342 } else {
343 Print(L" unknown disable flag: '%s'\n", FlagName);
344 }
345 }
346
347 } else if (StriCmp(TokenList[0], L"scanfor") == 0) {
348 for (i = 0; i < NUM_SCAN_OPTIONS; i++) {
349 if (i < TokenCount)
350 GlobalConfig.ScanFor[i] = TokenList[i][0];
351 else
352 GlobalConfig.ScanFor[i] = ' ';
353 }
354 } else if (StriCmp(TokenList[0], L"hideui") == 0) {
355 for (i = 1; i < TokenCount; i++) {
356 FlagName = TokenList[i];
357 if (StriCmp(FlagName, L"banner") == 0) {
358 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_BANNER;
359 } else if (StriCmp(FlagName, L"shell") == 0) {
360 GlobalConfig.DisableFlags |= DISABLE_FLAG_SHELL;
361 } else if (StriCmp(FlagName, L"tools") == 0) {
362 GlobalConfig.DisableFlags |= DISABLE_FLAG_TOOLS;
363 } else if (StriCmp(FlagName, L"funcs") == 0) {
364 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_FUNCS;
365 } else if (StriCmp(FlagName, L"label") == 0) {
366 GlobalConfig.HideUIFlags |= HIDEUI_FLAG_LABEL;
367 } else if (StriCmp(FlagName, L"all") == 0) {
368 GlobalConfig.HideUIFlags = HIDEUI_ALL;
369 GlobalConfig.DisableFlags |= DISABLE_FLAG_SHELL | DISABLE_FLAG_TOOLS;
370 } else {
371 Print(L" unknown hideui flag: '%s'\n", FlagName);
372 }
373 }
374
375 } else if (StriCmp(TokenList[0], L"banner") == 0) {
376 HandleString(TokenList, TokenCount, &(GlobalConfig.BannerFileName));
377
378 } else if (StriCmp(TokenList[0], L"selection_small") == 0) {
379 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionSmallFileName));
380
381 } else if (StriCmp(TokenList[0], L"selection_big") == 0) {
382 HandleString(TokenList, TokenCount, &(GlobalConfig.SelectionBigFileName));
383
384 } else if (StriCmp(TokenList[0], L"default_selection") == 0) {
385 HandleString(TokenList, TokenCount, &(GlobalConfig.DefaultSelection));
386
387 } else if (StriCmp(TokenList[0], L"textonly") == 0) {
388 GlobalConfig.TextOnly = TRUE;
389
390 } else if ((StriCmp(TokenList[0], L"}") == 0) || (StriCmp(TokenList[0], L"loader") == 0) ||
391 (StriCmp(TokenList[0], L"icon") == 0) || (StriCmp(TokenList[0], L"options") == 0)) {
392 // Do nothing; handled by ScanUserConfigured()
393 }
394
395 FreeTokenLine(&TokenList, &TokenCount);
396 }
397
398 FreePool(File.Buffer);
399 } /* VOID ReadConfig() */
400
401 static VOID AddSubmenu(LOADER_ENTRY *Entry, REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
402 REFIT_MENU_SCREEN *SubScreen;
403 LOADER_ENTRY *SubEntry;
404 UINTN TokenCount;
405 CHAR16 **TokenList;
406
407 SubScreen = InitializeSubScreen(Entry);
408
409 // Set defaults for the new entry; will be modified based on lines read from the config. file....
410 SubEntry = InitializeLoaderEntry(Entry);
411
412 if ((SubEntry == NULL) || (SubScreen == NULL))
413 return;
414 SubEntry->me.Title = StrDuplicate(Title);
415
416 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StriCmp(TokenList[0], L"}") != 0)) {
417 if ((StriCmp(TokenList[0], L"loader") == 0) && (TokenCount > 1)) { // set the boot loader filename
418 if (SubEntry->LoaderPath != NULL)
419 FreePool(SubEntry->LoaderPath);
420 SubEntry->LoaderPath = StrDuplicate(TokenList[1]);
421 SubEntry->DevicePath = FileDevicePath(Volume->DeviceHandle, SubEntry->LoaderPath);
422 } else if (StriCmp(TokenList[0], L"initrd") == 0) {
423 if (SubEntry->InitrdPath != NULL)
424 FreePool(SubEntry->InitrdPath);
425 SubEntry->InitrdPath = NULL;
426 if (TokenCount > 1) {
427 SubEntry->InitrdPath = StrDuplicate(TokenList[1]);
428 }
429 } else if (StriCmp(TokenList[0], L"options") == 0) {
430 if (SubEntry->LoadOptions != NULL)
431 FreePool(SubEntry->LoadOptions);
432 SubEntry->LoadOptions = NULL;
433 if (TokenCount > 1) {
434 SubEntry->LoadOptions = StrDuplicate(TokenList[1]);
435 } // if/else
436 } else if ((StriCmp(TokenList[0], L"add_options") == 0) && (TokenCount > 1)) {
437 MergeStrings(&SubEntry->LoadOptions, TokenList[1], L' ');
438 } else if ((StriCmp(TokenList[0], L"graphics") == 0) && (TokenCount > 1)) {
439 SubEntry->UseGraphicsMode = (StriCmp(TokenList[1], L"on") == 0);
440 } else if (StriCmp(TokenList[0], L"disabled") == 0) {
441 SubEntry->Enabled = FALSE;
442 } // ief/elseif
443 FreeTokenLine(&TokenList, &TokenCount);
444 } // while()
445 if (SubEntry->InitrdPath != NULL) {
446 MergeStrings(&SubEntry->LoadOptions, L"initrd=", L' ');
447 MergeStrings(&SubEntry->LoadOptions, SubEntry->InitrdPath, 0);
448 FreePool(SubEntry->InitrdPath);
449 SubEntry->InitrdPath = NULL;
450 } // if
451 if (SubEntry->Enabled == TRUE) {
452 AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
453 }
454 Entry->me.SubScreen = SubScreen;
455 } // VOID AddSubmenu()
456
457 // Adds the options from a SINGLE loaders.conf stanza to a new loader entry and returns
458 // that entry. The calling function is then responsible for adding the entry to the
459 // list of entries.
460 static LOADER_ENTRY * AddStanzaEntries(REFIT_FILE *File, REFIT_VOLUME *Volume, CHAR16 *Title) {
461 CHAR16 **TokenList;
462 UINTN TokenCount;
463 LOADER_ENTRY *Entry;
464 BOOLEAN DefaultsSet = FALSE, AddedSubmenu = FALSE;
465
466 // prepare the menu entry
467 Entry = InitializeLoaderEntry(NULL);
468 if (Entry == NULL)
469 return NULL;
470
471 Entry->Title = StrDuplicate(Title);
472 Entry->me.Title = PoolPrint(L"Boot %s from %s", (Title != NULL) ? Title : L"Unknown", Volume->VolName);
473 Entry->me.Row = 0;
474 Entry->me.BadgeImage = Volume->VolBadgeImage;
475 Entry->VolName = Volume->VolName;
476
477 // Parse the config file to add options for a single stanza, terminating when the token
478 // is "}" or when the end of file is reached.
479 while (((TokenCount = ReadTokenLine(File, &TokenList)) > 0) && (StriCmp(TokenList[0], L"}") != 0)) {
480 if ((StriCmp(TokenList[0], L"loader") == 0) && (TokenCount > 1)) { // set the boot loader filename
481 Entry->LoaderPath = StrDuplicate(TokenList[1]);
482 Entry->DevicePath = FileDevicePath(Volume->DeviceHandle, Entry->LoaderPath);
483 SetLoaderDefaults(Entry, TokenList[1], Volume);
484 FreePool(Entry->LoadOptions);
485 Entry->LoadOptions = NULL; // Discard default options, if any
486 DefaultsSet = TRUE;
487 } else if ((StriCmp(TokenList[0], L"icon") == 0) && (TokenCount > 1)) {
488 FreePool(Entry->me.Image);
489 Entry->me.Image = LoadIcns(Volume->RootDir, TokenList[1], 128);
490 if (Entry->me.Image == NULL) {
491 Entry->me.Image = DummyImage(128);
492 }
493 } else if ((StriCmp(TokenList[0], L"initrd") == 0) && (TokenCount > 1)) {
494 if (Entry->InitrdPath)
495 FreePool(Entry->InitrdPath);
496 Entry->InitrdPath = StrDuplicate(TokenList[1]);
497 } else if ((StriCmp(TokenList[0], L"options") == 0) && (TokenCount > 1)) {
498 if (Entry->LoadOptions)
499 FreePool(Entry->LoadOptions);
500 Entry->LoadOptions = StrDuplicate(TokenList[1]);
501 } else if ((StriCmp(TokenList[0], L"ostype") == 0) && (TokenCount > 1)) {
502 if (TokenCount > 1) {
503 Entry->OSType = TokenList[1][0];
504 }
505 } else if ((StriCmp(TokenList[0], L"graphics") == 0) && (TokenCount > 1)) {
506 Entry->UseGraphicsMode = (StriCmp(TokenList[1], L"on") == 0);
507 } else if (StriCmp(TokenList[0], L"disabled") == 0) {
508 Entry->Enabled = FALSE;
509 } else if ((StriCmp(TokenList[0], L"submenuentry") == 0) && (TokenCount > 1)) {
510 AddSubmenu(Entry, File, Volume, TokenList[1]);
511 AddedSubmenu = TRUE;
512 } // set options to pass to the loader program
513 FreeTokenLine(&TokenList, &TokenCount);
514 } // while()
515
516 if (AddedSubmenu)
517 AddMenuEntry(Entry->me.SubScreen, &MenuEntryReturn);
518
519 if (Entry->InitrdPath) {
520 MergeStrings(&Entry->LoadOptions, L"initrd=", L' ');
521 MergeStrings(&Entry->LoadOptions, Entry->InitrdPath, 0);
522 FreePool(Entry->InitrdPath);
523 Entry->InitrdPath = NULL;
524 } // if
525
526 if (!DefaultsSet)
527 SetLoaderDefaults(Entry, L"\\EFI\\BOOT\\nemo.efi", Volume); // user included no entry; use bogus one
528
529 return(Entry);
530 } // static VOID AddStanzaEntries()
531
532 // Read the user-configured loaders file, loaders.conf, and add or delete
533 // entries based on the contents of that file....
534 VOID ScanUserConfigured(VOID)
535 {
536 EFI_STATUS Status;
537 REFIT_FILE File;
538 REFIT_VOLUME *Volume;
539 CHAR16 **TokenList;
540 CHAR16 *Title = NULL;
541 UINTN TokenCount;
542 LOADER_ENTRY *Entry;
543
544 if (FileExists(SelfDir, CONFIG_FILE_NAME)) {
545 Status = ReadFile(SelfDir, CONFIG_FILE_NAME, &File);
546 if (EFI_ERROR(Status))
547 return;
548
549 Volume = SelfVolume;
550 // TODO: Figure out how to set volumes (on per-image basis, preferably)
551
552 while ((TokenCount = ReadTokenLine(&File, &TokenList)) > 0) {
553 if ((StriCmp(TokenList[0], L"menuentry") == 0) && (TokenCount > 1)) {
554 Title = StrDuplicate(TokenList[1]);
555 Entry = AddStanzaEntries(&File, Volume, TokenList[1]);
556 if (Entry->Enabled) {
557 if (Entry->me.SubScreen == NULL)
558 GenerateSubScreen(Entry, Volume);
559 AddPreparedLoaderEntry(Entry);
560 } else {
561 FreePool(Entry);
562 } // if/else
563 FreePool(Title);
564 } // if
565 FreeTokenLine(&TokenList, &TokenCount);
566 } // while()
567 } // if()
568 } // VOID ScanUserConfigured()
569
570 // Read a Linux kernel options file for a Linux boot loader into memory. The LoaderPath
571 // and Volume variables identify the location of the options file, but not its name --
572 // you pass this function the filename of the Linux kernel, initial RAM disk, or other
573 // file in the target directory, and this function finds the file with a name in the
574 // comma-delimited list of names specified by LINUX_OPTIONS_FILENAMES within that
575 // directory and loads it. This function tries multiple files because I originally
576 // used the filename linux.conf, but close on the heels of that decision, the Linux
577 // kernel developers decided to use that name for a similar purpose, but with a
578 // different file format. Thus, I'm migrating rEFInd to use the name refind_linux.conf,
579 // but I want a migration period in which both names are used.
580 //
581 // The return value is a pointer to the REFIT_FILE handle for the file, or NULL if
582 // it wasn't found.
583 REFIT_FILE * ReadLinuxOptionsFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
584 CHAR16 *OptionsFilename, *FullFilename;
585 BOOLEAN GoOn = TRUE;
586 UINTN i = 0;
587 REFIT_FILE *File = NULL;
588 EFI_STATUS Status;
589
590 do {
591 OptionsFilename = FindCommaDelimited(LINUX_OPTIONS_FILENAMES, i++);
592 FullFilename = FindPath(LoaderPath);
593 if ((OptionsFilename != NULL) && (FullFilename != NULL)) {
594 MergeStrings(&FullFilename, OptionsFilename, '\\');
595 if (FileExists(Volume->RootDir, FullFilename)) {
596 File = AllocateZeroPool(sizeof(REFIT_FILE));
597 Status = ReadFile(Volume->RootDir, FullFilename, File);
598 GoOn = FALSE;
599 if (CheckError(Status, L"while loading the Linux options file")) {
600 if (File != NULL)
601 FreePool(File);
602 File = NULL;
603 GoOn = TRUE;
604 } // if error
605 } // if file exists
606 } else { // a filename string is NULL
607 GoOn = FALSE;
608 } // if/else
609 if (OptionsFilename != NULL)
610 FreePool(OptionsFilename);
611 if (FullFilename != NULL)
612 FreePool(FullFilename);
613 OptionsFilename = FullFilename = NULL;
614 } while (GoOn);
615 return (File);
616 } // static REFIT_FILE * ReadLinuxOptionsFile()
617
618 // Retrieve a single line of options from a Linux kernel options file
619 CHAR16 * GetFirstOptionsFromFile(IN CHAR16 *LoaderPath, IN REFIT_VOLUME *Volume) {
620 UINTN TokenCount;
621 CHAR16 *Options = NULL;
622 CHAR16 **TokenList;
623 REFIT_FILE *File;
624
625 File = ReadLinuxOptionsFile(LoaderPath, Volume);
626 if (File != NULL) {
627 TokenCount = ReadTokenLine(File, &TokenList);
628 if (TokenCount > 1)
629 Options = StrDuplicate(TokenList[1]);
630 FreeTokenLine(&TokenList, &TokenCount);
631 FreePool(File);
632 }
633 return Options;
634 } // static CHAR16 * GetOptionsFile()
635