Skip to content

Commit 658ce57

Browse files
committed
Add sanity check and memory initialization.
1 parent 2e79618 commit 658ce57

1 file changed

Lines changed: 84 additions & 18 deletions

File tree

src/EFIApp.c

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "EFIApp.h"
22

3-
VOID JumpToAddress(EFI_HANDLE ImageHandle, uint32_t addr)
3+
VOID JumpToAddress(
4+
EFI_HANDLE ImageHandle,
5+
uint32_t addr
6+
)
47
{
58

69
EFI_STATUS Status;
@@ -13,10 +16,20 @@ VOID JumpToAddress(EFI_HANDLE ImageHandle, uint32_t addr)
1316
/* Entry */
1417
VOID(*entry)() = (VOID*) addr;
1518

16-
gBS->GetMemoryMap(&MemMapSize, MemMap, &MapKey, &DesSize, &DesVersion);
19+
gBS->GetMemoryMap(
20+
&MemMapSize,
21+
MemMap,
22+
&MapKey,
23+
&DesSize,
24+
&DesVersion
25+
);
1726

1827
/* Shutdown */
19-
Status = gBS->ExitBootServices(ImageHandle, MapKey);
28+
Status = gBS->ExitBootServices(
29+
ImageHandle,
30+
MapKey
31+
);
32+
2033
if (EFI_ERROR(Status))
2134
{
2235
Print(L"Failed to exit BS\n");
@@ -65,15 +78,24 @@ BOOLEAN CheckElf32Header(Elf32_Ehdr* bl_elf_hdr)
6578

6679
// Sanity check: entry point and size
6780
ElfEntryPoint = bl_elf_hdr->e_entry;
68-
Status = gBS->AllocatePages(AllocateAddress, EfiBootServicesCode, 1, &ElfEntryPoint);
81+
Status = gBS->AllocatePages(
82+
AllocateAddress,
83+
EfiLoaderCode,
84+
1,
85+
&ElfEntryPoint
86+
);
87+
6988
if (EFI_ERROR(Status))
7089
{
7190
Print(L"Fail: Invalid entry point\n");
7291
return FALSE;
7392
}
7493

7594
// Free page allocated
76-
gBS->FreePages(ElfEntryPoint, 1);
95+
gBS->FreePages(
96+
ElfEntryPoint,
97+
1
98+
);
7799

78100
// Sanity check: program header entries. At least one should present.
79101
if (bl_elf_hdr->e_phnum < 1)
@@ -87,7 +109,10 @@ BOOLEAN CheckElf32Header(Elf32_Ehdr* bl_elf_hdr)
87109

88110
// This is the actual entrypoint.
89111
// Application entrypoint (must be set to 'efi_main' for gnu-efi crt0 compatibility)
90-
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
112+
EFI_STATUS efi_main(
113+
EFI_HANDLE ImageHandle,
114+
EFI_SYSTEM_TABLE *SystemTable
115+
)
91116
{
92117

93118
EFI_STATUS Status = EFI_SUCCESS;
@@ -116,7 +141,10 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
116141
UINTN PayloadLength = 0;
117142

118143
#if defined(_GNU_EFI)
119-
InitializeLib(ImageHandle, SystemTable);
144+
InitializeLib(
145+
ImageHandle,
146+
SystemTable
147+
);
120148
#endif
121149

122150
// Load emmc_appsboot.mbn
@@ -125,7 +153,8 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
125153
&gEfiSimpleFileSystemProtocolGuid,
126154
NULL,
127155
&NumHandles,
128-
&SfsHandles);
156+
&SfsHandles
157+
);
129158

130159
if (EFI_ERROR(Status))
131160
{
@@ -184,16 +213,23 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
184213

185214
if (Status == EFI_BUFFER_TOO_SMALL)
186215
{
187-
Status = gBS->AllocatePool(EfiBootServicesData, PayloadFileInformationSize, &PayloadFileInformation);
216+
Status = gBS->AllocatePool(
217+
EfiLoaderData,
218+
PayloadFileInformationSize,
219+
&PayloadFileInformation
220+
);
221+
188222
if (EFI_ERROR(Status))
189223
{
190224
Print(L"Failed to allocate pool for file info: %r\n", Status);
191225
goto local_cleanup;
192226
}
193227

194-
SetMem((VOID *) PayloadFileInformation,
228+
SetMem(
229+
(VOID *) PayloadFileInformation,
195230
PayloadFileInformationSize,
196-
0xFF);
231+
0xFF
232+
);
197233

198234
Status = PayloadFileProtocol->GetInfo(
199235
PayloadFileProtocol,
@@ -219,15 +255,20 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
219255
PayloadFileBufferSize = (UINTN) PayloadFileInformation->FileSize;
220256

221257
/* Allocate pool for reading file */
222-
Status = gBS->AllocatePool(EfiLoaderData, PayloadFileBufferSize, &PayloadFileBuffer);
258+
Status = gBS->AllocatePool(
259+
EfiLoaderData,
260+
PayloadFileBufferSize,
261+
&PayloadFileBuffer
262+
);
223263

224264
if (EFI_ERROR(Status))
225265
{
226266
Print(L"Failed to allocate pool for file: %r\n", Status);
227267
goto local_cleanup_free_info;
228268
}
229269

230-
SetMem(PayloadFileBuffer,
270+
SetMem(
271+
PayloadFileBuffer,
231272
PayloadFileBufferSize,
232273
0xFF);
233274

@@ -325,24 +366,49 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
325366
Print(L"Allocate memory at 0x%x\n", PayloadElf32Phdr->p_paddr);
326367
Print(L"Allocate 0x%x pages memory\n", PayloadLoadPages);
327368

328-
Status = gBS->AllocatePages(AllocateAddress, EfiLoaderCode, PayloadLoadPages, &LkEntryPoint);
369+
Status = gBS->AllocatePages(
370+
AllocateAddress,
371+
EfiLoaderCode,
372+
PayloadLoadPages,
373+
&LkEntryPoint
374+
);
375+
329376
if (EFI_ERROR(Status))
330377
{
331378
Print(L"Failed to allocate memory for ELF payload\n");
332379
goto local_cleanup_file_pool;
333380
}
381+
382+
/* Move LOAD section to actual location */
334383
SetMem(
335384
(VOID*) LkEntryPoint,
336385
PayloadLength,
337386
0xFF);
338387

339-
/* Move LOAD section to actual location */
340-
CopyMem((VOID*) (PayloadElf32Phdr->p_paddr), PayloadLoadSec, PayloadLength);
388+
CopyMem(
389+
(VOID*) LkEntryPoint,
390+
PayloadLoadSec,
391+
PayloadLength
392+
);
393+
341394
Print(L"Memory copied!\n");
342395

343396
/* Jump to LOAD section entry point and never returns */
344-
Print(L"\nJump to address 0x%x\n", PayloadElf32Phdr->p_paddr);
345-
JumpToAddress(ImageHandle, PayloadElf32Phdr->p_paddr);
397+
Print(L"\nJump to address 0x%x\n", LkEntryPoint);
398+
399+
/* Ensure loader is not located too high */
400+
if (LkEntryPoint > UINT32_MAX)
401+
{
402+
Print(L"Loader located too high\n");
403+
Status = EFI_INVALID_PARAMETER;
404+
goto local_cleanup_file_pool;
405+
}
406+
407+
/* Jump to address securely */
408+
JumpToAddress(
409+
ImageHandle,
410+
(uint32_t) LkEntryPoint
411+
);
346412

347413
local_cleanup_file_pool:
348414
gBS->FreePool(PayloadFileBuffer);

0 commit comments

Comments
 (0)