I.... think I fixed the initrd code?

This commit is contained in:
apio 2022-09-19 17:06:08 +02:00
parent 245d5e514d
commit 31460fe148
3 changed files with 18 additions and 23 deletions

View File

@ -41,6 +41,7 @@ namespace InitRD
bool is_valid_header(TarHeader* header); bool is_valid_header(TarHeader* header);
File open(const char* filename); File open(const char* filename);
void close(File& f);
void for_each(void (*callback)(File& file)); void for_each(void (*callback)(File& file));
void init();
} }

View File

@ -2,6 +2,7 @@
#include "assert.h" #include "assert.h"
#include "bootboot.h" #include "bootboot.h"
#include "cpu/CPU.h" #include "cpu/CPU.h"
#include "init/InitRD.h"
#include "interrupts/Interrupts.h" #include "interrupts/Interrupts.h"
#include "io/Serial.h" #include "io/Serial.h"
#include "log/Log.h" #include "log/Log.h"
@ -42,6 +43,8 @@ void Init::early_init()
kernelPMM.init_from_mmap(); kernelPMM.init_from_mmap();
kernelVMM.init(); kernelVMM.init();
InitRD::init();
if (strstr(environment, "quiet=1")) if (strstr(environment, "quiet=1"))
{ {
KernelLog::toggle_log_level(LogLevel::DEBUG); KernelLog::toggle_log_level(LogLevel::DEBUG);

View File

@ -10,6 +10,8 @@
extern BOOTBOOT bootboot; extern BOOTBOOT bootboot;
static void* initrd_base;
static inline int get_file_size_in_blocks(InitRD::File f) static inline int get_file_size_in_blocks(InitRD::File f)
{ {
return f.size_in_blocks; return f.size_in_blocks;
@ -22,7 +24,7 @@ inline int InitRD::get_total_blocks()
inline InitRD::TarHeader* InitRD::get_block(int block_index) inline InitRD::TarHeader* InitRD::get_block(int block_index)
{ {
return (TarHeader*)(bootboot.initrd_ptr + block_index * TAR_BLOCKSIZE); return (TarHeader*)((uintptr_t)initrd_base + block_index * TAR_BLOCKSIZE);
} }
inline bool InitRD::is_valid_header(TarHeader* header) inline bool InitRD::is_valid_header(TarHeader* header)
@ -42,35 +44,26 @@ InitRD::File InitRD::get_file(TarHeader* header)
result.size += (multiplier * (header->size[i] - 48)); result.size += (multiplier * (header->size[i] - 48));
multiplier *= 8; multiplier *= 8;
} }
result.addr = result.addr = (void*)((uint64_t)header + TAR_BLOCKSIZE);
KernelMemoryManager::get_unaligned_mappings((void*)((uint64_t)header + TAR_BLOCKSIZE), result.size / 4096 + 1); result.size_in_blocks = (result.size + (TAR_BLOCKSIZE - 1)) / TAR_BLOCKSIZE;
result.size_in_blocks = result.size / TAR_BLOCKSIZE + 1;
return result; return result;
} }
void InitRD::free_file(File& file)
{
KernelMemoryManager::release_unaligned_mappings(file.addr, file.size / 4096 + 1);
}
InitRD::File InitRD::open(const char* filename) InitRD::File InitRD::open(const char* filename)
{ {
int block = 0; int block = 0;
int total_blocks = get_total_blocks(); int total_blocks = get_total_blocks();
while (block < total_blocks) while (block < total_blocks)
{ {
TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block)); TarHeader* hdr = (TarHeader*)get_block(block);
if (hdr->typeflag == 53 || !is_valid_header(hdr)) if (hdr->typeflag == 53 || !is_valid_header(hdr))
{ {
block++; block++;
KernelMemoryManager::release_unaligned_mapping(hdr);
continue; continue;
} }
auto f = get_file(hdr); auto f = get_file(hdr);
KernelMemoryManager::release_unaligned_mapping(hdr);
if (strncmp(hdr->name, filename, strlen(filename)) == 0) { return f; } if (strncmp(hdr->name, filename, strlen(filename)) == 0) { return f; }
block += get_file_size_in_blocks(f) + 1; block += get_file_size_in_blocks(f) + 1;
close(f);
} }
File nullFile; File nullFile;
nullFile.addr = NULL; nullFile.addr = NULL;
@ -79,28 +72,26 @@ InitRD::File InitRD::open(const char* filename)
return nullFile; return nullFile;
} }
void InitRD::close(File& file)
{
free_file(file);
}
void InitRD::for_each(void (*callback)(File& f)) void InitRD::for_each(void (*callback)(File& f))
{ {
int block = 0; int block = 0;
int total_blocks = get_total_blocks(); int total_blocks = get_total_blocks();
while (block < total_blocks) while (block < total_blocks)
{ {
TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block)); TarHeader* hdr = (TarHeader*)get_block(block);
if (hdr->typeflag == 53 || !is_valid_header(hdr)) if (hdr->typeflag == 53 || !is_valid_header(hdr))
{ {
block++; block++;
KernelMemoryManager::release_unaligned_mapping(hdr);
continue; continue;
} }
auto f = get_file(hdr); auto f = get_file(hdr);
block += get_file_size_in_blocks(f) + 1; block += get_file_size_in_blocks(f) + 1;
KernelMemoryManager::release_unaligned_mapping(hdr);
callback(f); callback(f);
close(f);
} }
} }
void InitRD::init()
{
initrd_base =
KernelMemoryManager::get_unaligned_mappings((void*)bootboot.initrd_ptr, bootboot.initrd_size / 4096 + 1);
}