+// Create an options file based on /etc/fstab. The resulting file has two options
+// lines, one of which boots the system with "ro root={rootfs}" and the other of
+// which boots the system with "ro root={rootfs} single", where "{rootfs}" is the
+// filesystem identifier associated with the "/" line in /etc/fstab.
+static REFIT_FILE * GenerateOptionsFromEtcFstab(REFIT_VOLUME *Volume) {
+ UINTN TokenCount, i;
+ REFIT_FILE *Options = NULL, *Fstab = NULL;
+ EFI_STATUS Status;
+ CHAR16 **TokenList, *Line, Root[100];
+
+ if (FileExists(Volume->RootDir, L"\\etc\\fstab")) {
+ Options = AllocateZeroPool(sizeof(REFIT_FILE));
+ Fstab = AllocateZeroPool(sizeof(REFIT_FILE));
+ Status = ReadFile(Volume->RootDir, L"\\etc\\fstab", Fstab, &i);
+ if (CheckError(Status, L"while reading /etc/fstab")) {
+ if (Options != NULL)
+ FreePool(Options);
+ if (Fstab != NULL)
+ FreePool(Fstab);
+ Options = NULL;
+ Fstab = NULL;
+ } else { // File read; locate root fs and create entries
+ Options->Encoding = ENCODING_UTF16_LE;
+ while ((TokenCount = ReadTokenLine(Fstab, &TokenList)) > 0) {
+ if (TokenCount > 2) {
+ Root[0] = '\0';
+ if (StriCmp(TokenList[1], L"\\") == 0) {
+ SPrint(Root, 99, L"%s", TokenList[0]);
+ } else if (StriCmp(TokenList[2], L"\\") == 0) {
+ SPrint(Root, 99, L"%s=%s", TokenList[0], TokenList[1]);
+ } // if/elseif/elseif
+ if (Root[0] != L'\0') {
+ for (i = 0; i < StrLen(Root); i++)
+ if (Root[i] == '\\')
+ Root[i] = '/';
+ Line = PoolPrint(L"\"Boot with normal options\" \"ro root=%s\"\n", Root);
+ MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
+ MyFreePool(Line);
+ Line = PoolPrint(L"\"Boot into single-user mode\" \"ro root=%s single\"\n", Root);
+ MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
+ Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
+ } // if
+ } // if
+ FreeTokenLine(&TokenList, &TokenCount);
+ } // while
+
+ Options->Current8Ptr = (CHAR8 *)Options->Buffer;
+ Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
+ Options->Current16Ptr = (CHAR16 *)Options->Buffer;
+ Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
+
+ MyFreePool(Fstab->Buffer);
+ MyFreePool(Fstab);
+ } // if/else file read error
+ } // if /etc/fstab exists
+ return Options;
+} // GenerateOptionsFromEtcFstab()
+
+// Create options from partition type codes. Specifically, if the earlier
+// partition scan found a partition with a type code corresponding to a root
+// filesystem according to the Freedesktop.org Discoverable Partitions Spec
+// (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/),
+// this function returns an appropriate file with two lines, one with
+// "ro root=PARTUUID={GUID}" and the other with that plus "single".
+// Note that this function returns the LAST partition found with the
+// appropriate type code, so this will work poorly on dual-boot systems or
+// if the type code is set incorrectly.
+static REFIT_FILE * GenerateOptionsFromPartTypes(VOID) {
+ REFIT_FILE *Options = NULL;
+ CHAR16 *Line, *GuidString;
+
+ if (GlobalConfig.DiscoveredRoot) {
+ Options = AllocateZeroPool(sizeof(REFIT_FILE));
+ if (Options) {
+ Options->Encoding = ENCODING_UTF16_LE;
+ GuidString = GuidAsString(&(GlobalConfig.DiscoveredRoot->PartGuid));
+ ToLower(GuidString);
+ if (GuidString) {
+ Line = PoolPrint(L"\"Boot with normal options\" \"ro root=/dev/disk/by-partuuid/%s\"\n", GuidString);
+ MergeStrings((CHAR16 **) &(Options->Buffer), Line, 0);
+ MyFreePool(Line);
+ Line = PoolPrint(L"\"Boot into single-user mode\" \"ro root=/dev/disk/by-partuuid/%s single\"\n", GuidString);
+ MergeStrings((CHAR16**) &(Options->Buffer), Line, 0);
+ MyFreePool(Line);
+ MyFreePool(GuidString);
+ } // if (GuidString)
+ Options->BufferSize = StrLen((CHAR16*) Options->Buffer) * sizeof(CHAR16);
+
+ Options->Current8Ptr = (CHAR8 *)Options->Buffer;
+ Options->End8Ptr = Options->Current8Ptr + Options->BufferSize;
+ Options->Current16Ptr = (CHAR16 *)Options->Buffer;
+ Options->End16Ptr = Options->Current16Ptr + (Options->BufferSize >> 1);
+ } // if (Options allocated OK)
+ } // if (partition has root GUID)
+ return Options;
+} // REFIT_FILE * GenerateOptionsFromPartTypes()
+