diff options
| author | Ada Christine <adachristine18@gmail.com> | 2025-01-23 00:24:57 +0000 |
|---|---|---|
| committer | Ada Christine <adachristine18@gmail.com> | 2025-01-23 00:24:57 +0000 |
| commit | d855c0a219a180497c2e50f9bc19bfce69b937a6 (patch) | |
| tree | 1ff8523d41b222fb356fb3404d2a53c26a7d6a3d /boot/efi/image.c | |
| parent | edd52bd1464ee9ae4f0cdf7ff90a20ca175580fe (diff) | |
it printfs with the efi interface
Diffstat (limited to 'boot/efi/image.c')
| -rw-r--r-- | boot/efi/image.c | 105 |
1 files changed, 25 insertions, 80 deletions
diff --git a/boot/efi/image.c b/boot/efi/image.c index 42dab7d..25e1911 100644 --- a/boot/efi/image.c +++ b/boot/efi/image.c @@ -2,7 +2,11 @@ #include <posix/fcntl.h> #include <kjarna/interface.h> #include <libc/string.h> +#include <libc/stdio.h> +#include <libc/stdlib.h> #include <lib/elf.h> +#include <posix/unistd.h> +#include <posix/sys/mman.h> #include "kjarna_efi.h" @@ -16,77 +20,26 @@ struct image_buffer size_t length; }; -static ssize_t read_ehdr(int fd, Elf64_Ehdr *ehdr) +static int create_image_buffer(int fd, struct image_buffer *buffer) { - ssize_t result; - - if (efi_lseek(fd, 0, SEEK_SET) == -1) - { - return -1; - } - else if ((result = efi_read(fd, ehdr, sizeof(*ehdr))) == -1) + if ((buffer->length = elf64_size_fd(fd)) == 0) { return -1; } - else if (!elf64_validate(ehdr, ET_DYN, EM_X86_64)) - { - return -1; - } - - return result; -} -static ssize_t read_phdrs(int fd, Elf64_Ehdr *ehdr, Elf64_Phdr *phdrs) -{ - if (efi_lseek(fd, ehdr->e_phoff, SEEK_SET) != -1) - { - return efi_read(fd, phdrs, ehdr->e_phnum * ehdr->e_phentsize); - } - - return -1; -} - -static void create_image_buffer(Elf64_Ehdr *ehdr, Elf64_Phdr *phdrs, struct image_buffer *buffer) -{ - buffer->length = elf64_size(ehdr, phdrs); - buffer->base = efi_mmap(nullptr, buffer->length, 0, 0, -1, 0); -} + printf("creating image buffer, %d bytes\n", buffer->length); -static ssize_t load_segment(int fd, Elf64_Phdr *phdr, char *buffer) -{ - if (efi_lseek(fd, phdr->p_offset, SEEK_SET) == -1) + if((buffer->base = mmap(nullptr, buffer->length, 0, 0, -1, 0)) == MAP_FAILED) { + buffer->length = 0; + buffer->base = nullptr; + return -1; } - int result = efi_read(fd, &buffer[phdr->p_offset], phdr->p_filesz); + printf("image buffer base %p\n", buffer->base); - if (result > 0 && phdr->p_memsz > phdr->p_filesz) - { - memset(&buffer[phdr->p_offset + phdr->p_filesz], 0, phdr->p_memsz - phdr->p_filesz); - } - - return result; -} - -static ssize_t load_segments(int fd, Elf64_Ehdr *ehdr, Elf64_Phdr *phdrs, char *buffer) -{ - int i; - - for (i = 0; i < ehdr->e_phnum; i++) - { - if (phdrs[i].p_type != PT_LOAD) - { - continue; - } - - if (load_segment(fd, &phdrs[i], buffer) == -1) - { - return -1; - } - } - - return i; + return 0; } static struct image_buffer load_image(void) @@ -97,37 +50,29 @@ static struct image_buffer load_image(void) struct image_buffer buffer = { nullptr, 0 }; - if ((image_fd = efi_open(image_entry_path, O_RDONLY, 0)) == -1) + if ((image_fd = open(image_entry_path, O_RDONLY, 0)) == -1) { return buffer; } - Elf64_Ehdr ehdr; - - if (read_ehdr(image_fd, &ehdr) == -1) + if (!elf64_validate_fd(image_fd, ET_DYN, EM_X86_64)) { return buffer; } - - Elf64_Phdr *phdrs = efi_calloc_pool(ehdr.e_phnum, ehdr.e_phentsize); - - if (phdrs == nullptr) + + if (create_image_buffer(image_fd, &buffer) < 0) { return buffer; } - else if(read_phdrs(image_fd, &ehdr, phdrs) != -1) - { - create_image_buffer(&ehdr, phdrs, &buffer); - } - if (buffer.base != nullptr && load_segments(image_fd, &ehdr, phdrs, buffer.base) == -1) + if (buffer.base != nullptr && elf64_load_segments(image_fd, buffer.length, buffer.base) < 0) { - efi_munmap(buffer.base, buffer.length); + printf("error detected: buffer {base: %p, length: %zu}\n", buffer.base, buffer.length); + munmap(buffer.base, buffer.length); buffer.base = nullptr; } - efi_close(image_fd); - efi_free_pool(phdrs); + close(image_fd); return buffer; } @@ -179,17 +124,17 @@ int image_start(void) image_entry(¶ms); } - if (efi_munmap(buffer.base, buffer.length) == -1) + if (munmap(buffer.base, buffer.length) == -1) { - efi_write(STDOUT_FILENO, "failed unmap\r\n", -1); + write(STDOUT_FILENO, "failed unmap\r\n", -1); if (efi_errno == EFI_NOT_FOUND) { - efi_write(STDOUT_FILENO, "not found\r\n", -1); + write(STDOUT_FILENO, "not found\r\n", -1); } else { - efi_write(STDOUT_FILENO, "bad alignment?\r\n", -1); + write(STDOUT_FILENO, "bad alignment?\r\n", -1); } } |
