816 EFI_STATUS status = EFI_SUCCESS;
817 void *data =
nullptr;
819 auto section_page_count = EFI_SIZE_TO_PAGES(_phdr.p_memsz);
822 status = uefi_call_wrapper(
elf->SetPosition, 2,
elf, _phdr.p_offset);
823 if (EFI_ERROR(status)) {
825 throw std::runtime_error(
"memory_map == nullptr");
831 status = uefi_call_wrapper(gBS->AllocatePages, 4, AllocateAnyPages,
832 EfiLoaderData, section_page_count, &aaa);
833 debug << L
"_phdr.p_paddr: [" << status << L
"] [" << section_page_count
835 if (EFI_ERROR(status)) {
836 debug << L
"AllocatePages AllocateAddress failed: " << status
838 throw std::runtime_error(
"EFI_ERROR(status)");
841 if (_phdr.p_filesz > 0) {
842 auto buffer_read_size = _phdr.p_filesz;
844 status = uefi_call_wrapper(gBS->AllocatePool, 3, EfiLoaderCode,
845 buffer_read_size, (
void **)&data);
846 if (EFI_ERROR(status)) {
848 throw std::runtime_error(
"EFI_ERROR(status)");
852 uefi_call_wrapper(
elf->Read, 3,
elf, &buffer_read_size, (
void *)data);
853 if (EFI_ERROR(status)) {
855 throw std::runtime_error(
"EFI_ERROR(status)");
859 uefi_call_wrapper(gBS->CopyMem, 3,
860 reinterpret_cast<void *
>(aaa + _phdr.p_paddr), data,
864 status = uefi_call_wrapper(gBS->FreePool, 1, data);
865 if (EFI_ERROR(status)) {
867 throw std::runtime_error(
"EFI_ERROR(status)");
872 auto *zero_fill_start =
873 reinterpret_cast<void *
>(aaa + _phdr.p_paddr + _phdr.p_filesz);
874 auto zero_fill_count = _phdr.p_memsz - _phdr.p_filesz;
875 if (zero_fill_count > 0) {
876 debug << L
"Debug: Zero-filling " << zero_fill_count
877 << L
" bytes at address '" <<
ostream::hex_x << zero_fill_start << L
"'"
881 uefi_call_wrapper(gBS->SetMem, 3, zero_fill_start, zero_fill_count, 0);
static auto hex_x(ostream &_ostream) -> ostream &