Updated InitRD to use virtual mappings

This commit is contained in:
apio 2022-09-10 22:15:19 +02:00
parent 4aeada05d3
commit f98a45aefe
2 changed files with 21 additions and 5 deletions

View File

@ -36,9 +36,11 @@ namespace InitRD
int get_total_blocks(); int get_total_blocks();
File get_file(TarHeader* header); File get_file(TarHeader* header);
void free_file(File& file);
TarHeader* get_block(int block_index); TarHeader* get_block(int block_index);
bool is_valid_header(TarHeader* header); bool is_valid_header(TarHeader* header);
File find_file(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));
} }

View File

@ -1,6 +1,7 @@
#include "init/InitRD.h" #include "init/InitRD.h"
#include "bootboot.h" #include "bootboot.h"
#include "io/Serial.h" #include "io/Serial.h"
#include "memory/KernelMemoryManager.h"
#include "std/stdlib.h" #include "std/stdlib.h"
#include <string.h> #include <string.h>
@ -31,7 +32,6 @@ InitRD::File InitRD::get_file(TarHeader* header)
File result; File result;
result.size = 0; result.size = 0;
memcpy(result.name, header->name, 100); memcpy(result.name, header->name, 100);
result.addr = (void*)(header + TAR_BLOCKSIZE);
int multiplier = int multiplier =
1; // why they decided to store the size as an octal-encoded string instead of an integer is beyond me 1; // why they decided to store the size as an octal-encoded string instead of an integer is beyond me
for (int i = 10; i >= 0; i--) for (int i = 10; i >= 0; i--)
@ -39,21 +39,28 @@ 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 = KernelMemoryManager::get_unaligned_mappings((void*)(header + TAR_BLOCKSIZE), result.size / 4096 + 1);
result.size_in_blocks = result.size / TAR_BLOCKSIZE + 1; result.size_in_blocks = result.size / TAR_BLOCKSIZE + 1;
return result; return result;
} }
InitRD::File InitRD::find_file(const char* filename) void InitRD::free_file(File& file)
{
KernelMemoryManager::release_unaligned_mappings(file.addr, file.size / 4096 + 1);
}
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 = get_block(block); TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block));
block++; block++;
if (hdr->typeflag == 53) { continue; } // Directory if (hdr->typeflag == 53) { continue; } // Directory
if (!is_valid_header(hdr)) { continue; } if (!is_valid_header(hdr)) { 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); block += get_file_size_in_blocks(f);
} }
@ -64,18 +71,25 @@ InitRD::File InitRD::find_file(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 = get_block(block); TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block));
block++; block++;
if (hdr->typeflag == 53) { continue; } // Directory if (hdr->typeflag == 53) { continue; } // Directory
if (!is_valid_header(hdr)) { continue; } if (!is_valid_header(hdr)) { continue; }
auto f = get_file(hdr); auto f = get_file(hdr);
block += get_file_size_in_blocks(f); block += get_file_size_in_blocks(f);
KernelMemoryManager::release_unaligned_mapping(hdr);
callback(f); callback(f);
close(f);
} }
} }