Updated InitRD to use virtual mappings
This commit is contained in:
parent
4aeada05d3
commit
f98a45aefe
@ -36,9 +36,11 @@ namespace InitRD
|
||||
|
||||
int get_total_blocks();
|
||||
File get_file(TarHeader* header);
|
||||
void free_file(File& file);
|
||||
TarHeader* get_block(int block_index);
|
||||
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));
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#include "init/InitRD.h"
|
||||
#include "bootboot.h"
|
||||
#include "io/Serial.h"
|
||||
#include "memory/KernelMemoryManager.h"
|
||||
#include "std/stdlib.h"
|
||||
#include <string.h>
|
||||
|
||||
@ -31,7 +32,6 @@ InitRD::File InitRD::get_file(TarHeader* header)
|
||||
File result;
|
||||
result.size = 0;
|
||||
memcpy(result.name, header->name, 100);
|
||||
result.addr = (void*)(header + TAR_BLOCKSIZE);
|
||||
int multiplier =
|
||||
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--)
|
||||
@ -39,21 +39,28 @@ InitRD::File InitRD::get_file(TarHeader* header)
|
||||
result.size += (multiplier * (header->size[i] - 48));
|
||||
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;
|
||||
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 total_blocks = get_total_blocks();
|
||||
while (block < total_blocks)
|
||||
{
|
||||
TarHeader* hdr = get_block(block);
|
||||
TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block));
|
||||
block++;
|
||||
if (hdr->typeflag == 53) { continue; } // Directory
|
||||
if (!is_valid_header(hdr)) { continue; }
|
||||
auto f = get_file(hdr);
|
||||
KernelMemoryManager::release_unaligned_mapping(hdr);
|
||||
if (strncmp(hdr->name, filename, strlen(filename)) == 0) { return f; }
|
||||
block += get_file_size_in_blocks(f);
|
||||
}
|
||||
@ -64,18 +71,25 @@ InitRD::File InitRD::find_file(const char* filename)
|
||||
return nullFile;
|
||||
}
|
||||
|
||||
void InitRD::close(File& file)
|
||||
{
|
||||
free_file(file);
|
||||
}
|
||||
|
||||
void InitRD::for_each(void (*callback)(File& f))
|
||||
{
|
||||
int block = 0;
|
||||
int total_blocks = get_total_blocks();
|
||||
while (block < total_blocks)
|
||||
{
|
||||
TarHeader* hdr = get_block(block);
|
||||
TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block));
|
||||
block++;
|
||||
if (hdr->typeflag == 53) { continue; } // Directory
|
||||
if (!is_valid_header(hdr)) { continue; }
|
||||
auto f = get_file(hdr);
|
||||
block += get_file_size_in_blocks(f);
|
||||
KernelMemoryManager::release_unaligned_mapping(hdr);
|
||||
callback(f);
|
||||
close(f);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user