Files
jsix/src/user/srv.init/ramdisk.cpp
Justin C. Miller 6ef15a2721 [init] Add new initrd format
A new compressed initrd format for srv.init to load drivers, services,
and data from, instead of every file getting loaded by the bootloader.
This will allow for less memory allocated by the bootloader and passed
to init if not every driver or data file is loaded.

Loading, passing, and using the new initrd will be done in a coming
commit.
2023-01-18 10:59:34 -08:00

62 lines
1.3 KiB
C++

#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <util/cdb.h>
#include <zstd.h>
#include "ramdisk.h"
inline constexpr uint64_t manifest_magic = 0x74696e697869736a; // "jsixinit"
inline constexpr size_t manifest_min = 18;
inline constexpr size_t manifest_version = 1;
using util::read;
ramdisk::ramdisk(util::buffer data) : m_data {data} {}
util::buffer
ramdisk::load_file(const char *name)
{
util::cdb cdb {m_data};
util::buffer c = cdb.retrieve(name);
if (!c.count)
return c;
size_t size = ZSTD_getFrameContentSize(c.pointer, c.count);
util::buffer d {malloc(size), size};
size_t out = ZSTD_decompress(
d.pointer, d.count,
c.pointer, c.count);
if (out != size) {
free(d.pointer);
return {0,0};
}
return d;
}
manifest::manifest(util::buffer data)
{
if (data.count < manifest_min)
return;
char const *base = reinterpret_cast<char const *>(data.pointer);
if (*read<uint64_t>(data) != manifest_magic)
return;
uint8_t version = *read<uint8_t>(data);
if (version != manifest_version)
return;
read<uint8_t>(data); // reserved byte
uint16_t services_len = *read<uint16_t>(data);
uint16_t drivers_len = *read<uint16_t>(data);
base += *read<uint16_t>(data); // start of the string section
}