summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAda Christine <adachristine18@gmail.com>2021-03-19 16:39:24 +0000
committerAda Christine <adachristine18@gmail.com>2021-03-19 16:39:24 +0000
commitd182b40fde059899bc65347e6669e6cc23c09a6c (patch)
treeee782ac7ea55a300a4d3ebe8babbd5acc00524cc
parent31efbc82cb0e72a161719b5242d9016e92fb1e9c (diff)
buffer size was not properly set when allocated
-rw-r--r--loader/loader_efi.c96
1 files changed, 51 insertions, 45 deletions
diff --git a/loader/loader_efi.c b/loader/loader_efi.c
index 5e238f6..de9ed39 100644
--- a/loader/loader_efi.c
+++ b/loader/loader_efi.c
@@ -22,7 +22,6 @@ struct system_image
Elf64_Phdr *phdrs;
};
-static struct system_buffer get_system_buffer(size_t size);
static struct system_image *open_system_image(CHAR16 *path);
static bool load_system_image(struct system_image *image);
static struct efi_memory_map_data *get_memory_map_data(void);
@@ -35,6 +34,8 @@ void loader_main(void)
struct system_image *kernel_image = open_system_image(kernel_path);
+ Print(L"loading %s\r\n", kernel_path);
+
if (load_system_image(kernel_image))
{
Print(L"loaded kernel image at base %16.0x size %d bytes\r\n",
@@ -68,26 +69,6 @@ void loader_main(void)
kernel_entry(&data);
}
-static struct system_buffer get_system_buffer(UINTN size)
-{
- EFI_STATUS status;
- struct system_buffer buffer;
-
- status = e_bs->AllocatePages(AllocateAnyPages,
- SystemMemoryType,
- EFI_SIZE_TO_PAGES(size),
- &buffer.base);
-
- if (EFI_ERROR(status))
- {
- Print(L"error allocating system buffer: %r\r\n", status);
- buffer.base = 0;
- buffer.size = 0;
- }
-
- return buffer;
-}
-
static bool validate_image(Elf64_Ehdr *ehdr)
{
if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
@@ -108,29 +89,6 @@ static bool validate_image(Elf64_Ehdr *ehdr)
return true;
}
-static UINTN get_image_size(struct system_image *image)
-{
- UINTN size = 0;
- Elf64_Phdr *phdrs = image->phdrs;
-
- for (int i = 0; i < image->ehdr.e_phnum; i++)
- {
- if (phdrs[i].p_type != PT_LOAD)
- {
- continue;
- }
-
- if (phdrs[i].p_paddr + phdrs[i].p_memsz > size)
- {
- size = phdrs[i].p_paddr + phdrs[i].p_memsz;
- if (phdrs[i].p_align > 1)
- size = (size + phdrs[i].p_align - 1) & ~(phdrs[i].p_align - 1);
- }
- }
-
- return size;
-}
-
static struct system_image *open_system_image(CHAR16 *path)
{
EFI_STATUS status;
@@ -203,6 +161,55 @@ failure:
return NULL;
}
+static struct system_buffer get_system_buffer(UINTN size)
+{
+ EFI_STATUS status;
+ struct system_buffer buffer;
+
+ buffer.size = size;
+
+ status = e_bs->AllocatePages(AllocateAnyPages,
+ SystemMemoryType,
+ EFI_SIZE_TO_PAGES(buffer.size),
+ &buffer.base);
+
+ if (EFI_ERROR(status))
+ {
+ Print(L"error allocating system buffer: %r\r\n", status);
+ buffer.base = 0;
+ buffer.size = 0;
+ }
+
+ Print(L"allocated system buffer at %16.0x of %d bytes\r\n",
+ buffer.base,
+ buffer.size);
+
+ return buffer;
+}
+
+static UINTN get_image_size(struct system_image *image)
+{
+ UINTN size = 0;
+ Elf64_Phdr *phdrs = image->phdrs;
+
+ for (int i = 0; i < image->ehdr.e_phnum; i++)
+ {
+ if (phdrs[i].p_type != PT_LOAD)
+ {
+ continue;
+ }
+
+ if (phdrs[i].p_paddr + phdrs[i].p_memsz > size)
+ {
+ size = phdrs[i].p_paddr + phdrs[i].p_memsz;
+ if (phdrs[i].p_align > 1)
+ size = (size + phdrs[i].p_align - 1) & ~(phdrs[i].p_align - 1);
+ }
+ }
+
+ return size;
+}
+
static bool load_system_image(struct system_image *image)
{
image->buffer = get_system_buffer(get_image_size(image));
@@ -250,7 +257,6 @@ static bool load_system_image(struct system_image *image)
return true;
}
-
static struct efi_memory_map_data *get_memory_map_data(void)
{
EFI_STATUS status;