2022-12-23 11:33:23 +01:00
|
|
|
#include "InitRD.h"
|
2023-03-12 16:55:46 +01:00
|
|
|
#include "Log.h"
|
2022-12-23 11:33:23 +01:00
|
|
|
#include "arch/MMU.h"
|
|
|
|
#include "boot/bootboot.h"
|
2023-03-11 10:23:46 +01:00
|
|
|
#include "fs/VFS.h"
|
2023-03-16 23:02:53 +01:00
|
|
|
#include "memory/MemoryManager.h"
|
2023-04-08 13:12:49 +02:00
|
|
|
#include "thread/Thread.h"
|
2023-03-12 16:55:46 +01:00
|
|
|
#include <bits/modes.h>
|
2023-08-08 11:58:33 +02:00
|
|
|
#include <luna/Common.h>
|
2022-12-23 11:33:23 +01:00
|
|
|
|
|
|
|
TarStream g_initrd;
|
|
|
|
extern const BOOTBOOT bootboot;
|
|
|
|
|
|
|
|
void InitRD::initialize()
|
|
|
|
{
|
2023-02-27 13:27:21 +01:00
|
|
|
u64 virtual_initrd_address = MMU::translate_physical_address(bootboot.initrd_ptr);
|
2022-12-23 11:33:23 +01:00
|
|
|
|
|
|
|
g_initrd.initialize((void*)virtual_initrd_address, bootboot.initrd_size);
|
2023-01-02 13:07:29 +01:00
|
|
|
}
|
2023-03-11 10:23:46 +01:00
|
|
|
|
2023-08-01 17:20:28 +02:00
|
|
|
static Result<void> vfs_create_dir_if_not_exists(const char* path, mode_t mode)
|
2023-03-11 10:23:46 +01:00
|
|
|
{
|
2023-08-01 17:20:28 +02:00
|
|
|
auto rc = VFS::create_directory(path, mode & (mode_t)~S_IFMT, Credentials {});
|
2023-03-11 10:23:46 +01:00
|
|
|
if (rc.has_error())
|
|
|
|
{
|
|
|
|
if (rc.error() == EEXIST) return {};
|
|
|
|
return rc.release_error();
|
|
|
|
}
|
2023-05-06 12:11:35 +02:00
|
|
|
auto dir = rc.value();
|
2023-03-11 10:23:46 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> InitRD::populate_vfs()
|
|
|
|
{
|
|
|
|
TarStream::Entry entry;
|
|
|
|
while (TRY(g_initrd.read_next_entry(entry)))
|
|
|
|
{
|
|
|
|
if (entry.type == TarStream::EntryType::RegularFile)
|
|
|
|
{
|
2023-08-01 17:20:28 +02:00
|
|
|
auto file = TRY(VFS::create_file(entry.name.chars(), entry.mode & (mode_t)~S_IFMT, Credentials {}));
|
2023-03-11 10:23:46 +01:00
|
|
|
file->write(entry.data(), 0, entry.size);
|
2023-03-12 16:55:46 +01:00
|
|
|
}
|
|
|
|
else if (entry.type == TarStream::EntryType::Directory)
|
|
|
|
{
|
2023-08-01 17:20:28 +02:00
|
|
|
TRY(vfs_create_dir_if_not_exists(entry.name.chars(), entry.mode));
|
2023-03-11 10:23:46 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 23:02:53 +01:00
|
|
|
|
|
|
|
// Now we don't need the original initrd anymore
|
2023-08-08 11:58:33 +02:00
|
|
|
MemoryManager::free_frames(bootboot.initrd_ptr, ceil_div(bootboot.initrd_size, ARCH_PAGE_SIZE));
|
2023-03-16 23:02:53 +01:00
|
|
|
|
2023-03-11 10:23:46 +01:00
|
|
|
return {};
|
|
|
|
}
|