From 6054bf8a069bdb759e921efdceea4727f94ef28e Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sun, 19 Jan 2014 15:23:04 +0100 Subject: [PATCH] Cleanup --- kernel/src/paging.cpp | 6 +- kernel/src/shell.cpp | 133 +++++++++++++++++++++++++----------------- 2 files changed, 83 insertions(+), 56 deletions(-) diff --git a/kernel/src/paging.cpp b/kernel/src/paging.cpp index 6f3cf0b3..78d0ca4b 100644 --- a/kernel/src/paging.cpp +++ b/kernel/src/paging.cpp @@ -18,9 +18,9 @@ typedef pt_t* pdt_t; typedef pdt_t* pdpt_t; typedef pdpt_t* pml4t_t; -constexpr const int PRESENT = 0x1; -constexpr const int WRITEABLE = 0x2; -constexpr const int USER = 0x4; +constexpr const uint8_t PRESENT = 0x1; +constexpr const uint8_t WRITEABLE = 0x2; +constexpr const uint8_t USER = 0x4; //Memory from 0x70000 can be used for pages uintptr_t last_page = 0x73000; diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index ee67cbb6..5dcccf60 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -743,54 +743,10 @@ std::optional read_elf_file(const std::string& file, const std::str return {std::move(content)}; } -void exec_command(const std::vector& params){ - if(params.size() < 2){ - k_print_line("exec: Need the name of the executable to read"); - - return; - } - - if(!disks::mounted_partition() || !disks::mounted_disk()){ - k_print_line("Nothing is mounted"); - - return; - } - - auto content = read_elf_file(params[1], "exec"); - - if(!content){ - return; - } - - //TODO -} - -void execin_command(const std::vector& params){ - if(params.size() < 2){ - k_print_line("execin: Need the name of the executable to read"); - - return; - } - - if(!disks::mounted_partition() || !disks::mounted_disk()){ - k_print_line("Nothing is mounted"); - - return; - } - - auto content = read_elf_file(params[1], "execin"); - - if(!content){ - return; - } - - auto buffer = content->c_str(); +bool allocate_segments(char* buffer, void** allocated_segments){ auto header = reinterpret_cast(buffer); - auto program_header_table = reinterpret_cast(buffer + header->e_phoff); - auto allocated_segments = new void*[header->e_phnum]; - bool failed = false; for(size_t p = 0; p < header->e_phnum; ++p){ auto& p_header = program_header_table[p]; @@ -851,15 +807,12 @@ void execin_command(const std::vector& params){ } } - if(!failed){ - auto main_function = reinterpret_cast(header->e_entry); + return failed; +} - auto return_code = main_function(); - - k_printf("Returned %d\n", return_code); - } else { - k_print_line("execin: Unable to execute the program"); - } +void release_segments(char* buffer, void** allocated_segments){ + auto header = reinterpret_cast(buffer); + auto program_header_table = reinterpret_cast(buffer + header->e_phoff); //Release physical memory for(size_t p = 0; p < header->e_phnum; ++p){ @@ -882,6 +835,80 @@ void execin_command(const std::vector& params){ } } +void exec_command(const std::vector& params){ + if(params.size() < 2){ + k_print_line("exec: Need the name of the executable to read"); + + return; + } + + if(!disks::mounted_partition() || !disks::mounted_disk()){ + k_print_line("Nothing is mounted"); + + return; + } + + auto content = read_elf_file(params[1], "exec"); + + if(!content){ + return; + } + + auto buffer = content->c_str(); + auto header = reinterpret_cast(buffer); + + std::unique_heap_array allocated_segments(header->e_phnum); + + auto failed = allocate_segments(buffer, allocated_segments.get()); + + if(!failed){ + //TODO + } else { + k_print_line("execin: Unable to execute the program"); + } + + release_segments(buffer, allocated_segments.get()); +} + +void execin_command(const std::vector& params){ + if(params.size() < 2){ + k_print_line("execin: Need the name of the executable to read"); + + return; + } + + if(!disks::mounted_partition() || !disks::mounted_disk()){ + k_print_line("Nothing is mounted"); + + return; + } + + auto content = read_elf_file(params[1], "execin"); + + if(!content){ + return; + } + + auto buffer = content->c_str(); + auto header = reinterpret_cast(buffer); + + std::unique_heap_array allocated_segments(header->e_phnum); + + auto failed = allocate_segments(buffer, allocated_segments.get()); + + if(!failed){ + auto main_function = reinterpret_cast(header->e_entry); + + auto return_code = main_function(); + + k_printf("Returned %d\n", return_code); + } else { + k_print_line("execin: Unable to execute the program"); + } + + release_segments(buffer, allocated_segments.get()); +} + void vesainfo_command(const std::vector&){ if(vesa::vesa_enabled){ auto& block = vesa::mode_info_block;