From 98b3d37277254a5c75af529f699e27a8504c2582 Mon Sep 17 00:00:00 2001 From: SergeySlice Date: Fri, 5 Jun 2020 12:43:12 +0300 Subject: [PATCH] security update for startup sound Signed-off-by: SergeySlice --- Drivers/AudioDxe/AudioDxe.c | 8 ++--- rEFIt_UEFI/Platform/Nvram.cpp | 3 +- rEFIt_UEFI/Platform/StartupSound.cpp | 53 ++++++++++++++-------------- rEFIt_UEFI/Platform/StartupSound.h | 2 +- rEFIt_UEFI/gui/REFIT_MENU_SCREEN.cpp | 2 +- rEFIt_UEFI/refit/main.cpp | 5 +-- rEFIt_UEFI/refit/menu.cpp | 16 ++++++--- 7 files changed, 50 insertions(+), 39 deletions(-) diff --git a/Drivers/AudioDxe/AudioDxe.c b/Drivers/AudioDxe/AudioDxe.c index b854cc796..42739bd8d 100644 --- a/Drivers/AudioDxe/AudioDxe.c +++ b/Drivers/AudioDxe/AudioDxe.c @@ -61,16 +61,16 @@ AudioDxeInit( // Register HdaController Driver Binding. Status = EfiLibInstallDriverBindingComponentName2(ImageHandle, SystemTable, &gHdaControllerDriverBinding, ImageHandle, &gHdaControllerComponentName, &gHdaControllerComponentName2); - ASSERT_EFI_ERROR(Status); +// ASSERT_EFI_ERROR(Status); if (EFI_ERROR(Status)) return Status; // Register HdaCodec Driver Binding. Status = EfiLibInstallDriverBindingComponentName2(ImageHandle, SystemTable, &gHdaCodecDriverBinding, NULL, &gHdaCodecComponentName, &gHdaCodecComponentName2); - ASSERT_EFI_ERROR(Status); - if (EFI_ERROR(Status)) - return Status; +// ASSERT_EFI_ERROR(Status); +// if (EFI_ERROR(Status)) +// return Status; return Status; } diff --git a/rEFIt_UEFI/Platform/Nvram.cpp b/rEFIt_UEFI/Platform/Nvram.cpp index 531779883..413e0ff5f 100644 --- a/rEFIt_UEFI/Platform/Nvram.cpp +++ b/rEFIt_UEFI/Platform/Nvram.cpp @@ -1176,7 +1176,8 @@ FindStartupDiskVolume ( } DBG(" - [!] not found\n"); } - + + //Slice - why all further? For legacy boot which is not working??? // // 2. gEfiBootVolume - partition volume // PciRoot(0x0)/.../Sata(...)/HD(...) - set by Clover or macOS diff --git a/rEFIt_UEFI/Platform/StartupSound.cpp b/rEFIt_UEFI/Platform/StartupSound.cpp index 99391d3f8..a985dcfc9 100644 --- a/rEFIt_UEFI/Platform/StartupSound.cpp +++ b/rEFIt_UEFI/Platform/StartupSound.cpp @@ -63,15 +63,22 @@ EFI_AUDIO_IO_PROTOCOL *AudioIo = NULL; EFI_STATUS StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) { - EFI_STATUS Status = EFI_NOT_FOUND; + EFI_STATUS Status; UINT8 *FileData = NULL; UINTN FileDataLength = 0U; WAVE_FILE_DATA WaveData; - UINT8 OutputIndex = (OldChosenAudio & 0xFF); + UINT8 OutputIndex; UINT8 OutputVolume = DefaultAudioVolume; UINT16 *TempData = NULL; - BOOLEAN AllocAsPage = FALSE; + UINTN Len; + + if (OldChosenAudio > AudioNum) { + OldChosenAudio = 0; //security correction + } + OutputIndex = (OldChosenAudio & 0xFF); + WaveData.Samples = NULL; + WaveData.SamplesLength = 0; if (!AudioIo) { Status = EFI_DEVICE_ERROR; // DBG("not found AudioIo to play\n"); @@ -82,7 +89,7 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) Status = egLoadFile(Dir, SoundFile, &FileData, &FileDataLength); if (EFI_ERROR(Status)) { // DBG("file sound read: %ls %s\n", SoundFile, strerror(Status)); - return Status; + goto DONE_ERROR; } } else { FileData = EmbeddedSound; @@ -90,14 +97,14 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) // DBG("got embedded sound\n"); } - WaveData.Samples = NULL; + Status = WaveGetFileData(FileData, (UINT32)FileDataLength, &WaveData); // if (EFI_ERROR(Status)) { MsgLog(" wrong sound file, wave status=%s\n", strerror(Status)); //if error then data not allocated - return Status; + goto DONE_ERROR; } - AllocAsPage = TRUE; + Len = WaveData.SamplesLength; //byte length MsgLog(" Channels: %hu Sample rate: %u Hz Bits: %hu\n", WaveData.Format->Channels, WaveData.Format->SamplesPerSec, WaveData.Format->BitsPerSample); EFI_AUDIO_IO_PROTOCOL_BITS bits; @@ -168,7 +175,7 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) if ((freq == EfiAudioIoFreq8kHz) && (bits == EfiAudioIoBits16)) { //making conversion - UINTN Len = WaveData.SamplesLength * 6; //8000<->48000 + Len *= 6; //8000<->48000 UINTN Ind, Out=0, Tact; INT16 Tmp, Next; float Delta; @@ -178,7 +185,8 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) // DBG("not found wave data\n"); goto DONE_ERROR; } - TempData = (__typeof__(TempData))AllocateZeroPool(Len * sizeof(INT16)); +// TempData = (__typeof__(TempData))AllocateZeroPool(Len * sizeof(INT16)); + TempData = (__typeof__(TempData))AllocateAlignedPages(EFI_SIZE_TO_PAGES(Len + 4095), 128); Tmp = *(Ptr++); for (Ind = 0; Ind < WaveData.SamplesLength / 2 - 1; Ind++) { Next = *(Ptr++); @@ -191,13 +199,10 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) } freq = EfiAudioIoFreq48kHz; // Samples was allocated via AllocateAlignedPages, so it must not be freed with FreePool, but according to the number of pages allocated - FreePages(WaveData.Samples,EFI_SIZE_TO_PAGES(WaveData.SamplesLength+4095)); + FreeAlignedPages(WaveData.Samples,EFI_SIZE_TO_PAGES(WaveData.SamplesLength + 4095)); WaveData.SamplesLength *= 6; DBG("sound converted to 48kHz\n"); WaveData.Samples = (UINT8*)TempData; - AllocAsPage = FALSE; - } else { - TempData = (UINT16*)WaveData.Samples; } // Setup playback. @@ -214,10 +219,10 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) // DBG("playback set\n"); // Start playback. if (gSettings.PlayAsync) { - Status = AudioIo->StartPlaybackAsync(AudioIo, (UINT8*)TempData, WaveData.SamplesLength, 0, NULL, NULL); + Status = AudioIo->StartPlaybackAsync(AudioIo, WaveData.Samples, WaveData.SamplesLength, 0, NULL, NULL); // DBG("async started, status=%s\n", strerror(Status)); } else { - Status = AudioIo->StartPlayback(AudioIo, (UINT8*)TempData, WaveData.SamplesLength, 0); + Status = AudioIo->StartPlayback(AudioIo, WaveData.Samples, WaveData.SamplesLength, 0); // DBG("sync started, status=%s\n", strerror(Status)); // if (!EFI_ERROR(Status)) { // FreePool(TempData); @@ -229,19 +234,15 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile) } DONE_ERROR: - // here we have memory leak with TempData == WaveData.Samples - // TempData allocated as AllocatePool while Samples allocated as AllocatePages - // and we can't keep the info up to stop AsyncPlay if (FileData && SoundFile) { //dont free embedded sound // DBG("free sound\n"); FreePool(FileData); } - if (!gSettings.PlayAsync) { //dont free sound when async play - if (AllocAsPage) { - FreePages(WaveData.Samples,EFI_SIZE_TO_PAGES(WaveData.SamplesLength+4095)); - } else { - FreePool(TempData); - } + if (!gSettings.PlayAsync && WaveData.Samples) { + //dont free sound when async play + // here we have memory leak with WaveData.Samples + // and we can't free memory up to stop AsyncPlay + FreeAlignedPages(WaveData.Samples, EFI_SIZE_TO_PAGES(WaveData.SamplesLength + 4095)); } DBG("sound play end with status=%s\n", strerror(Status)); return Status; @@ -373,7 +374,7 @@ DONE: return Status; } -EFI_STATUS CheckSyncSound() +EFI_STATUS CheckSyncSound(BOOLEAN Stop) { EFI_STATUS Status; AUDIO_IO_PRIVATE_DATA *AudioIoPrivateData; @@ -391,7 +392,7 @@ EFI_STATUS CheckSyncSound() HdaIo = AudioIoPrivateData->HdaCodecDev->HdaIo; Status = HdaIo->GetStream(HdaIo, EfiHdaIoTypeOutput, &StreamRunning); - if (EFI_ERROR(Status) && StreamRunning) { + if ((EFI_ERROR(Status) || Stop) && StreamRunning) { DBG("stream stopping\n"); HdaIo->StopStream(HdaIo, EfiHdaIoTypeOutput); } diff --git a/rEFIt_UEFI/Platform/StartupSound.h b/rEFIt_UEFI/Platform/StartupSound.h index 161941acb..3b0f17ec5 100644 --- a/rEFIt_UEFI/Platform/StartupSound.h +++ b/rEFIt_UEFI/Platform/StartupSound.h @@ -25,6 +25,6 @@ StartupSoundPlay(EFI_FILE *Dir, CONST CHAR16* SoundFile); VOID GetOutputs(); -EFI_STATUS CheckSyncSound(); +EFI_STATUS CheckSyncSound(BOOLEAN Stop); #endif /* PLATFORM_STARTUPSOUND_H_ */ diff --git a/rEFIt_UEFI/gui/REFIT_MENU_SCREEN.cpp b/rEFIt_UEFI/gui/REFIT_MENU_SCREEN.cpp index 4f90f8598..56b612abb 100644 --- a/rEFIt_UEFI/gui/REFIT_MENU_SCREEN.cpp +++ b/rEFIt_UEFI/gui/REFIT_MENU_SCREEN.cpp @@ -709,7 +709,7 @@ EFI_STATUS REFIT_MENU_SCREEN::WaitForInputEventPoll(UINTN TimeoutDefault) } UpdateFilm(); if (gSettings.PlayAsync) { - CheckSyncSound(); + CheckSyncSound(false); } TimeoutRemain--; if (mPointer.isAlive()) { diff --git a/rEFIt_UEFI/refit/main.cpp b/rEFIt_UEFI/refit/main.cpp index 1f5821d6e..926bb0414 100644 --- a/rEFIt_UEFI/refit/main.cpp +++ b/rEFIt_UEFI/refit/main.cpp @@ -831,7 +831,8 @@ VOID LOADER_ENTRY::StartLoader() } if (AudioIo) { - AudioIo->StopPlayback(AudioIo); +// AudioIo->StopPlayback(AudioIo); + CheckSyncSound(true); } // DBG("Set FakeCPUID: 0x%X\n", gSettings.FakeCPUID); @@ -2415,7 +2416,7 @@ RefitMain (IN EFI_HANDLE ImageHandle, } } - // log Audio devices in boot-log. Thisis for clients like Clover.app + // log Audio devices in boot-log. This is for clients like Clover.app GetOutputs(); for (i = 0; i < AudioNum; i++) { if (AudioList[i].Name) { diff --git a/rEFIt_UEFI/refit/menu.cpp b/rEFIt_UEFI/refit/menu.cpp index 4fbd07633..39b036726 100644 --- a/rEFIt_UEFI/refit/menu.cpp +++ b/rEFIt_UEFI/refit/menu.cpp @@ -1166,10 +1166,17 @@ VOID ApplyInputs(VOID) i++; //119 if (InputItems[i].Valid) { EFI_DEVICE_PATH_PROTOCOL* DevicePath = NULL; - UINT8 TmpIndex = OldChosenAudio & 0xFF; - DBG("Chosen output %llu:%ls_%s\n", OldChosenAudio, AudioList[OldChosenAudio].Name, AudioOutputNames[OldChosenAudio]); + UINT8 TmpIndex; + if (OldChosenAudio > AudioNum) { + DBG("crasy OldChosenAudio = %lld\n", OldChosenAudio); + OldChosenAudio = 0; + } + TmpIndex = OldChosenAudio & 0x2F; + DBG("Chosen output %u:%ls_%s\n", TmpIndex, AudioList[TmpIndex].Name, + AudioOutputNames[AudioList[TmpIndex].Device]); - DevicePath = DevicePathFromHandle(AudioList[OldChosenAudio].Handle); + DevicePath = DevicePathFromHandle(AudioList[TmpIndex].Handle); + DBG("choosen sound devicepath=%ls\n", DevicePathToStr(DevicePath)); if (DevicePath != NULL) { SetNvramVariable(L"Clover.SoundDevice", &gEfiAppleBootGuid, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, @@ -1177,12 +1184,13 @@ VOID ApplyInputs(VOID) SetNvramVariable(L"Clover.SoundIndex", &gEfiAppleBootGuid, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, 1, (UINT8 *)&TmpIndex); - + DBG(" sound written to nvram variables\n"); } } i++; //120 if (InputItems[i].Valid) { DefaultAudioVolume = (UINT8)StrDecimalToUintn(InputItems[i].SValue); + DBG(" set output volume to %d\n", DefaultAudioVolume); if (DefaultAudioVolume > 100) { // correct wrong input DefaultAudioVolume = 90;