2022-09-05 14:13:51 +00:00
|
|
|
#include "init/InitRD.h"
|
|
|
|
#include "bootboot.h"
|
|
|
|
#include "io/Serial.h"
|
2022-09-10 20:15:19 +00:00
|
|
|
#include "memory/KernelMemoryManager.h"
|
2022-09-05 14:13:51 +00:00
|
|
|
#include "std/stdlib.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
extern BOOTBOOT bootboot;
|
|
|
|
|
|
|
|
static inline int get_file_size_in_blocks(InitRD::File f)
|
|
|
|
{
|
|
|
|
return f.size_in_blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int InitRD::get_total_blocks()
|
|
|
|
{
|
|
|
|
return bootboot.initrd_size / TAR_BLOCKSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InitRD::TarHeader* InitRD::get_block(int block_index)
|
|
|
|
{
|
|
|
|
return (TarHeader*)(bootboot.initrd_ptr + block_index * TAR_BLOCKSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool InitRD::is_valid_header(TarHeader* header)
|
|
|
|
{
|
|
|
|
return strncmp(header->magic, TAR_MAGIC, 5) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
InitRD::File InitRD::get_file(TarHeader* header)
|
|
|
|
{
|
|
|
|
File result;
|
|
|
|
result.size = 0;
|
|
|
|
memcpy(result.name, header->name, 100);
|
|
|
|
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--)
|
|
|
|
{
|
|
|
|
result.size += (multiplier * (header->size[i] - 48));
|
|
|
|
multiplier *= 8;
|
|
|
|
}
|
2022-09-10 20:15:19 +00:00
|
|
|
result.addr = KernelMemoryManager::get_unaligned_mappings((void*)(header + TAR_BLOCKSIZE), result.size / 4096 + 1);
|
2022-09-05 14:13:51 +00:00
|
|
|
result.size_in_blocks = result.size / TAR_BLOCKSIZE + 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:15:19 +00:00
|
|
|
void InitRD::free_file(File& file)
|
|
|
|
{
|
|
|
|
KernelMemoryManager::release_unaligned_mappings(file.addr, file.size / 4096 + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
InitRD::File InitRD::open(const char* filename)
|
2022-09-05 14:13:51 +00:00
|
|
|
{
|
|
|
|
int block = 0;
|
|
|
|
int total_blocks = get_total_blocks();
|
|
|
|
while (block < total_blocks)
|
|
|
|
{
|
2022-09-10 20:15:19 +00:00
|
|
|
TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block));
|
2022-09-05 14:13:51 +00:00
|
|
|
block++;
|
|
|
|
if (hdr->typeflag == 53) { continue; } // Directory
|
|
|
|
if (!is_valid_header(hdr)) { continue; }
|
|
|
|
auto f = get_file(hdr);
|
2022-09-10 20:15:19 +00:00
|
|
|
KernelMemoryManager::release_unaligned_mapping(hdr);
|
2022-09-05 14:13:51 +00:00
|
|
|
if (strncmp(hdr->name, filename, strlen(filename)) == 0) { return f; }
|
|
|
|
block += get_file_size_in_blocks(f);
|
|
|
|
}
|
|
|
|
File nullFile;
|
|
|
|
nullFile.addr = NULL;
|
|
|
|
nullFile.size = 0;
|
|
|
|
memcpy(nullFile.name, "NULL", 5);
|
|
|
|
return nullFile;
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:15:19 +00:00
|
|
|
void InitRD::close(File& file)
|
|
|
|
{
|
|
|
|
free_file(file);
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:13:51 +00:00
|
|
|
void InitRD::for_each(void (*callback)(File& f))
|
|
|
|
{
|
|
|
|
int block = 0;
|
|
|
|
int total_blocks = get_total_blocks();
|
|
|
|
while (block < total_blocks)
|
|
|
|
{
|
2022-09-10 20:15:19 +00:00
|
|
|
TarHeader* hdr = (TarHeader*)KernelMemoryManager::get_unaligned_mapping(get_block(block));
|
2022-09-05 14:13:51 +00:00
|
|
|
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);
|
2022-09-10 20:15:19 +00:00
|
|
|
KernelMemoryManager::release_unaligned_mapping(hdr);
|
2022-09-05 14:13:51 +00:00
|
|
|
callback(f);
|
2022-09-10 20:15:19 +00:00
|
|
|
close(f);
|
2022-09-05 14:13:51 +00:00
|
|
|
}
|
|
|
|
}
|