[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.
This commit is contained in:
Justin C. Miller
2023-01-17 18:48:28 -07:00
parent 5a3e0ba541
commit 6ef15a2721
10 changed files with 290 additions and 3 deletions

View File

@@ -2,14 +2,13 @@
init = module("srv.init",
targets = [ "user" ],
deps = [ "libc", "elf", "bootproto" ],
deps = [ "libc", "elf", "bootproto", "zstd" ],
description = "Init server",
sources = [
"loader.cpp",
"main.cpp",
"modules.cpp",
"ramdisk.cpp",
"service_locator.cpp",
"start.s",
])
init.variables['ldflags'] = ["${ldflags}", "-section-start=.rodata=0x800000"]

View File

@@ -0,0 +1,61 @@
#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
}

View File

@@ -0,0 +1,30 @@
#pragma once
/// \file loader.h
/// Data structure for a ramdisk archive, based on djb's CDB format
#include <unordered_map>
#include <vector>
#include <j6/types.h>
#include <util/counted.h>
class ramdisk
{
public:
ramdisk(util::buffer data);
util::buffer load_file(const char *name);
private:
util::buffer m_data;
};
class manifest
{
public:
manifest(util::buffer data);
private:
std::vector<const char*> m_services;
std::unordered_map<const char*, const char*> m_drivers;
};