-//
-// legacy boot functions
-//
-
-static EFI_STATUS ActivateMbrPartition(IN EFI_BLOCK_IO *BlockIO, IN UINTN PartitionIndex)
-{
- EFI_STATUS Status;
- UINT8 SectorBuffer[512];
- MBR_PARTITION_INFO *MbrTable, *EMbrTable;
- UINT32 ExtBase, ExtCurrent, NextExtCurrent;
- UINTN LogicalPartitionIndex = 4;
- UINTN i;
- BOOLEAN HaveBootCode;
-
- // read MBR
- Status = refit_call5_wrapper(BlockIO->ReadBlocks, BlockIO, BlockIO->Media->MediaId, 0, 512, SectorBuffer);
- if (EFI_ERROR(Status))
- return Status;
- if (*((UINT16 *)(SectorBuffer + 510)) != 0xaa55)
- return EFI_NOT_FOUND; // safety measure #1
-
- // add boot code if necessary
- HaveBootCode = FALSE;
- for (i = 0; i < MBR_BOOTCODE_SIZE; i++) {
- if (SectorBuffer[i] != 0) {
- HaveBootCode = TRUE;
- break;
- }
- }
- if (!HaveBootCode) {
- // no boot code found in the MBR, add the syslinux MBR code
- SetMem(SectorBuffer, MBR_BOOTCODE_SIZE, 0);
- CopyMem(SectorBuffer, syslinux_mbr, SYSLINUX_MBR_SIZE);
- }
-
- // set the partition active
- MbrTable = (MBR_PARTITION_INFO *)(SectorBuffer + 446);
- ExtBase = 0;
- for (i = 0; i < 4; i++) {
- if (MbrTable[i].Flags != 0x00 && MbrTable[i].Flags != 0x80)
- return EFI_NOT_FOUND; // safety measure #2
- if (i == PartitionIndex)
- MbrTable[i].Flags = 0x80;
- else if (PartitionIndex >= 4 && IS_EXTENDED_PART_TYPE(MbrTable[i].Type)) {
- MbrTable[i].Flags = 0x80;
- ExtBase = MbrTable[i].StartLBA;
- } else
- MbrTable[i].Flags = 0x00;
- }
-
- // write MBR
- Status = refit_call5_wrapper(BlockIO->WriteBlocks, BlockIO, BlockIO->Media->MediaId, 0, 512, SectorBuffer);
- if (EFI_ERROR(Status))
- return Status;
-
- if (PartitionIndex >= 4) {
- // we have to activate a logical partition, so walk the EMBR chain
-
- // NOTE: ExtBase was set above while looking at the MBR table
- for (ExtCurrent = ExtBase; ExtCurrent; ExtCurrent = NextExtCurrent) {
- // read current EMBR
- Status = refit_call5_wrapper(BlockIO->ReadBlocks, BlockIO, BlockIO->Media->MediaId, ExtCurrent, 512, SectorBuffer);
- if (EFI_ERROR(Status))
- return Status;
- if (*((UINT16 *)(SectorBuffer + 510)) != 0xaa55)
- return EFI_NOT_FOUND; // safety measure #3
-
- // scan EMBR, set appropriate partition active
- EMbrTable = (MBR_PARTITION_INFO *)(SectorBuffer + 446);
- NextExtCurrent = 0;
- for (i = 0; i < 4; i++) {
- if (EMbrTable[i].Flags != 0x00 && EMbrTable[i].Flags != 0x80)
- return EFI_NOT_FOUND; // safety measure #4
- if (EMbrTable[i].StartLBA == 0 || EMbrTable[i].Size == 0)
- break;
- if (IS_EXTENDED_PART_TYPE(EMbrTable[i].Type)) {
- // link to next EMBR
- NextExtCurrent = ExtBase + EMbrTable[i].StartLBA;
- EMbrTable[i].Flags = (PartitionIndex >= LogicalPartitionIndex) ? 0x80 : 0x00;
- break;
- } else {
- // logical partition
- EMbrTable[i].Flags = (PartitionIndex == LogicalPartitionIndex) ? 0x80 : 0x00;
- LogicalPartitionIndex++;
- }
- }
-
- // write current EMBR
- Status = refit_call5_wrapper(BlockIO->WriteBlocks, BlockIO, BlockIO->Media->MediaId, ExtCurrent, 512, SectorBuffer);
- if (EFI_ERROR(Status))
- return Status;
-
- if (PartitionIndex < LogicalPartitionIndex)
- break; // stop the loop, no need to touch further EMBRs
- }
-
- }
-
- return EFI_SUCCESS;
-} /* static EFI_STATUS ActivateMbrPartition() */
-
-// early 2006 Core Duo / Core Solo models
-static UINT8 LegacyLoaderDevicePath1Data[] = {
- 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0xFF, 0xF9, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
- 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
- 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
-};
-// mid-2006 Mac Pro (and probably other Core 2 models)
-static UINT8 LegacyLoaderDevicePath2Data[] = {
- 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0xFF, 0xF7, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
- 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
- 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
-};
-// mid-2007 MBP ("Santa Rosa" based models)
-static UINT8 LegacyLoaderDevicePath3Data[] = {
- 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0xFF, 0xF8, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
- 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
- 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
-};
-// early-2008 MBA
-static UINT8 LegacyLoaderDevicePath4Data[] = {
- 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xC0, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0xFF, 0xF8, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
- 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
- 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
-};
-// late-2008 MB/MBP (NVidia chipset)
-static UINT8 LegacyLoaderDevicePath5Data[] = {
- 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
- 0x00, 0x40, 0xCB, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0xBF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
- 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
- 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
- 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
-};
-
-static EFI_DEVICE_PATH *LegacyLoaderList[] = {
- (EFI_DEVICE_PATH *)LegacyLoaderDevicePath1Data,
- (EFI_DEVICE_PATH *)LegacyLoaderDevicePath2Data,
- (EFI_DEVICE_PATH *)LegacyLoaderDevicePath3Data,
- (EFI_DEVICE_PATH *)LegacyLoaderDevicePath4Data,
- (EFI_DEVICE_PATH *)LegacyLoaderDevicePath5Data,
- NULL
-};
-
-#define MAX_DISCOVERED_PATHS (16)
-
-static VOID StartLegacy(IN LEGACY_ENTRY *Entry)
-{
- EFI_STATUS Status;
- EG_IMAGE *BootLogoImage;
- UINTN ErrorInStep = 0;
- EFI_DEVICE_PATH *DiscoveredPathList[MAX_DISCOVERED_PATHS];
-
- BeginExternalScreen(TRUE, L"Booting Legacy OS (Mac mode)");
-
- BootLogoImage = LoadOSIcon(Entry->Volume->OSIconName, L"legacy", TRUE);
- if (BootLogoImage != NULL)
- BltImageAlpha(BootLogoImage,
- (UGAWidth - BootLogoImage->Width ) >> 1,
- (UGAHeight - BootLogoImage->Height) >> 1,
- &StdBackgroundPixel);
-
- if (Entry->Volume->IsMbrPartition) {
- ActivateMbrPartition(Entry->Volume->WholeDiskBlockIO, Entry->Volume->MbrPartitionIndex);
- }
-
- ExtractLegacyLoaderPaths(DiscoveredPathList, MAX_DISCOVERED_PATHS, LegacyLoaderList);
-
- Status = StartEFIImageList(DiscoveredPathList, Entry->LoadOptions, NULL, L"legacy loader", 0, &ErrorInStep, TRUE);
- if (Status == EFI_NOT_FOUND) {
- if (ErrorInStep == 1) {
- Print(L"\nPlease make sure that you have the latest firmware update installed.\n");
- } else if (ErrorInStep == 3) {
- Print(L"\nThe firmware refused to boot from the selected volume. Note that external\n"
- L"hard drives are not well-supported by Apple's firmware for legacy OS booting.\n");
- }
- }
- FinishExternalScreen();
-} /* static VOID StartLegacy() */
-
-// Start a device on a non-Mac using the EFI_LEGACY_BIOS_PROTOCOL
-#ifdef __MAKEWITH_TIANO
-static VOID StartLegacyUEFI(IN LEGACY_ENTRY *Entry)
-{
- BeginExternalScreen(TRUE, L"Booting Legacy OS (UEFI mode)");
-
- BdsLibConnectDevicePath (Entry->BdsOption->DevicePath);
- BdsLibDoLegacyBoot(Entry->BdsOption);
-
- // If we get here, it means that there was a failure....
- Print(L"Failure booting legacy (BIOS) OS.");
- PauseForKey();
- FinishExternalScreen();
-} // static VOID StartLegacyUEFI()
-#endif // __MAKEWITH_TIANO
-
-static LEGACY_ENTRY * AddLegacyEntry(IN CHAR16 *LoaderTitle, IN REFIT_VOLUME *Volume)
-{
- LEGACY_ENTRY *Entry, *SubEntry;
- REFIT_MENU_SCREEN *SubScreen;
- CHAR16 *VolDesc;
- CHAR16 ShortcutLetter = 0;
-
- if (LoaderTitle == NULL) {
- if (Volume->OSName != NULL) {
- LoaderTitle = Volume->OSName;
- if (LoaderTitle[0] == 'W' || LoaderTitle[0] == 'L')
- ShortcutLetter = LoaderTitle[0];
- } else
- LoaderTitle = L"Legacy OS";
- }
- if (Volume->VolName != NULL)
- VolDesc = Volume->VolName;
- else
- VolDesc = (Volume->DiskKind == DISK_KIND_OPTICAL) ? L"CD" : L"HD";
-
- // prepare the menu entry
- Entry = AllocateZeroPool(sizeof(LEGACY_ENTRY));
- Entry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
- SPrint(Entry->me.Title, 255, L"Boot %s from %s", LoaderTitle, VolDesc);
- Entry->me.Tag = TAG_LEGACY;
- Entry->me.Row = 0;
- Entry->me.ShortcutLetter = ShortcutLetter;
- Entry->me.Image = LoadOSIcon(Volume->OSIconName, L"legacy", FALSE);
- Entry->me.BadgeImage = Volume->VolBadgeImage;
- Entry->Volume = Volume;
- Entry->LoadOptions = (Volume->DiskKind == DISK_KIND_OPTICAL) ? L"CD" :
- ((Volume->DiskKind == DISK_KIND_EXTERNAL) ? L"USB" : L"HD");
- Entry->Enabled = TRUE;
-
- // create the submenu
- SubScreen = AllocateZeroPool(sizeof(REFIT_MENU_SCREEN));
- SubScreen->Title = AllocateZeroPool(256 * sizeof(CHAR16));
- SPrint(SubScreen->Title, 255, L"Boot Options for %s on %s", LoaderTitle, VolDesc);
- SubScreen->TitleImage = Entry->me.Image;
- SubScreen->Hint1 = StrDuplicate(SUBSCREEN_HINT1);
- if (GlobalConfig.HideUIFlags & HIDEUI_FLAG_EDITOR) {
- SubScreen->Hint2 = StrDuplicate(SUBSCREEN_HINT2_NO_EDITOR);
- } else {
- SubScreen->Hint2 = StrDuplicate(SUBSCREEN_HINT2);
- } // if/else
-
- // default entry
- SubEntry = AllocateZeroPool(sizeof(LEGACY_ENTRY));
- SubEntry->me.Title = AllocateZeroPool(256 * sizeof(CHAR16));
- SPrint(SubEntry->me.Title, 255, L"Boot %s", LoaderTitle);
- SubEntry->me.Tag = TAG_LEGACY;
- SubEntry->Volume = Entry->Volume;
- SubEntry->LoadOptions = Entry->LoadOptions;
- AddMenuEntry(SubScreen, (REFIT_MENU_ENTRY *)SubEntry);
-
- AddMenuEntry(SubScreen, &MenuEntryReturn);
- Entry->me.SubScreen = SubScreen;
- AddMenuEntry(&MainMenu, (REFIT_MENU_ENTRY *)Entry);
- return Entry;
-} /* static LEGACY_ENTRY * AddLegacyEntry() */
-
-
-#ifdef __MAKEWITH_GNUEFI
-static VOID ScanLegacyUEFI(IN UINTN DiskType){}
-#else