From d0ba4b73ce7b8f78c205eeb20025895c99c54c59 Mon Sep 17 00:00:00 2001 From: ddeptford <136014411+ddeptford@users.noreply.github.com> Date: Fri, 7 Jul 2023 08:49:45 -0700 Subject: [PATCH] Change default alignment for ramdisk booting. (#480) The ramdisk is modelled as an NVDIMM which have a naturally higher alignment than 4K. Operating systems may wish to map NVDIMMs using large pages, so force the allocation alignment to 2MB. For each item, place an "x" in between `[` and `]` if true. Example: `[x]`. _(you can also check items in the GitHub UI)_ - [X] Impacts functionality? - **Functionality** - Does the change ultimately impact how firmware functions? - Alignment of ramdisk is changed from 4KB to 2MB. - [ ] Impacts security? - **Security** - Does the change have a direct security impact on an application, flow, or firmware? - Examples: Crypto algorithm change, buffer overflow fix, parameter validation improvement, ... - [ ] Breaking change? - **Breaking change** - Will anyone consuming this change experience a break in build or boot behavior? - Examples: Add a new library class, move a module to a different repo, call a function in a new library class in a pre-existing module, ... - [ ] Includes tests? - **Tests** - Does the change include any explicit test code? - Examples: Unit tests, integration tests, robot tests, ... - [ ] Includes documentation? - **Documentation** - Does the change contain explicit documentation additions outside direct code modifications (and comments)? - Examples: Update readme file, add feature readme file, link to documentation on an a separate Web page, ... ## How This Was Tested This change was tested using HTTP ramdisk boot, targeting a flat windows image. ## Integration Instructions N/A --- MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c index 8b3195b5541..0227784de12 100644 --- a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c @@ -1435,7 +1435,8 @@ BmDestroyRamDisk ( Status = mRamDisk->Unregister (RamDiskDevicePath); ASSERT_EFI_ERROR (Status); - FreePages (RamDiskBuffer, RamDiskSizeInPages); + // MU_CHANGE - Ramdisk is now allocated with alignment. + FreeAlignedPages (RamDiskBuffer, RamDiskSizeInPages); } /** @@ -1483,10 +1484,17 @@ BmExpandLoadFile ( return DuplicateDevicePath (DevicePathFromHandle (LoadFileHandle)); } + // MU_CHANGE [BEGIN] - Ramdisk is now allocated with 2MB alignment. + // // The load option resides in a RAM disk. + // Use a reasonable default of 2MB for alignment as the ramdisk device is + // implemented as an NVDIMM persistent memory and operating systems may + // wish to map this with huge page support. // - FileBuffer = AllocateReservedPages (EFI_SIZE_TO_PAGES (BufferSize)); + + FileBuffer = AllocateAlignedReservedPages (EFI_SIZE_TO_PAGES (BufferSize), SIZE_2MB); + // MU_CHANGE [END] - Ramdisk is now allocated with 2MB alignment. if (FileBuffer == NULL) { DEBUG_CODE_BEGIN (); EFI_DEVICE_PATH *LoadFilePath; @@ -1527,7 +1535,8 @@ BmExpandLoadFile ( Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer); if (EFI_ERROR (Status)) { - FreePages (FileBuffer, EFI_SIZE_TO_PAGES (BufferSize)); + // MU_CHANGE - Ramdisk is now allocated with alignment. + FreeAlignedPages (FileBuffer, EFI_SIZE_TO_PAGES (BufferSize)); return NULL; }