Initial ramdisk support
- Create initrd library to support definitions and loading - Allow tools compiled for the host machine to be built by wscript - Create makerd tool to build initrd from manifest - Move screenfont to initrd, so don't load framebuffer initially
This commit is contained in:
38
src/libraries/initrd/headers.h
Normal file
38
src/libraries/initrd/headers.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "kutil/enum_bitfields.h"
|
||||
|
||||
namespace initrd {
|
||||
|
||||
|
||||
enum class disk_flags : uint16_t
|
||||
{
|
||||
};
|
||||
|
||||
struct disk_header
|
||||
{
|
||||
uint16_t file_count;
|
||||
disk_flags flags;
|
||||
uint32_t length;
|
||||
uint8_t checksum;
|
||||
uint8_t reserved[3];
|
||||
} __attribute__ ((packed));
|
||||
|
||||
|
||||
enum class file_flags : uint16_t
|
||||
{
|
||||
executable = 0x01
|
||||
};
|
||||
|
||||
struct file_header
|
||||
{
|
||||
uint32_t offset;
|
||||
uint32_t length;
|
||||
uint16_t name_offset;
|
||||
file_flags flags;
|
||||
} __attribute__ ((packed));
|
||||
|
||||
} // namepsace initrd
|
||||
|
||||
IS_BITFIELD(initrd::disk_flags);
|
||||
IS_BITFIELD(initrd::file_flags);
|
||||
41
src/libraries/initrd/initrd.cpp
Normal file
41
src/libraries/initrd/initrd.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "initrd/initrd.h"
|
||||
#include "kutil/assert.h"
|
||||
#include "kutil/enum_bitfields.h"
|
||||
#include "headers.h"
|
||||
|
||||
namespace initrd {
|
||||
|
||||
file::file(const file_header *header, const void *start) :
|
||||
m_header(header)
|
||||
{
|
||||
m_data = kutil::offset_pointer(start, m_header->offset);
|
||||
auto *name = kutil::offset_pointer(start, m_header->name_offset);
|
||||
m_name = reinterpret_cast<const char *>(name);
|
||||
}
|
||||
|
||||
const char * file::name() const { return m_name; }
|
||||
const size_t file::size() const { return m_header->length; }
|
||||
const void * file::data() const { return m_data; }
|
||||
|
||||
bool
|
||||
file::executable() const {
|
||||
return bitfield_has(m_header->flags, file_flags::executable);
|
||||
}
|
||||
|
||||
|
||||
disk::disk(const void *start)
|
||||
{
|
||||
auto *header = reinterpret_cast<const disk_header *>(start);
|
||||
size_t length = header->length;
|
||||
uint8_t sum = kutil::checksum(start, length);
|
||||
kassert(sum == 0, "initrd checksum failed");
|
||||
|
||||
auto *files = reinterpret_cast<const file_header *>(header + 1);
|
||||
|
||||
m_files.ensure_capacity(header->file_count);
|
||||
for (int i = 0; i < header->file_count; ++i) {
|
||||
m_files.emplace(&files[i], start);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace initrd
|
||||
61
src/libraries/initrd/initrd.h
Normal file
61
src/libraries/initrd/initrd.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
/// \file initrd.h
|
||||
/// Definitions defining the simple inital ramdisk file format used by the
|
||||
/// popcorn kernel.
|
||||
|
||||
#include <stdint.h>
|
||||
#include "kutil/vector.h"
|
||||
|
||||
// File format:
|
||||
// 1x disk_header
|
||||
// Nx file_header
|
||||
// filename string data
|
||||
// file data
|
||||
|
||||
namespace initrd {
|
||||
|
||||
struct disk_header;
|
||||
struct file_header;
|
||||
|
||||
|
||||
/// Encasulates methods for a file on the ramdisk
|
||||
class file
|
||||
{
|
||||
public:
|
||||
file(const file_header *header, const void *start);
|
||||
|
||||
/// Get the filename
|
||||
const char * name() const;
|
||||
|
||||
/// Get the file size
|
||||
const size_t size() const;
|
||||
|
||||
/// Get a pointer to the file data
|
||||
const void * data() const;
|
||||
|
||||
/// Whether this file is an executable
|
||||
bool executable() const;
|
||||
|
||||
private:
|
||||
const file_header *m_header;
|
||||
void const *m_data;
|
||||
char const *m_name;
|
||||
};
|
||||
|
||||
|
||||
/// Encasulates access methods for the ramdisk
|
||||
class disk
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
/// \arg start The start of the initrd in memory
|
||||
disk(const void *start);
|
||||
|
||||
/// Get the vector of files on the disk
|
||||
const kutil::vector<file> & files() const { return m_files; }
|
||||
|
||||
private:
|
||||
kutil::vector<file> m_files;
|
||||
};
|
||||
|
||||
} // namespace initrd
|
||||
14
src/libraries/initrd/wscript
Normal file
14
src/libraries/initrd/wscript
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
def configure(ctx):
|
||||
pass
|
||||
|
||||
def build(bld):
|
||||
sources = bld.path.ant_glob("**/*.cpp")
|
||||
|
||||
bld.stlib(
|
||||
source = sources,
|
||||
name = 'initrd',
|
||||
target = 'initrd',
|
||||
)
|
||||
|
||||
# vim: ft=python et
|
||||
@@ -25,4 +25,13 @@ memcpy(void *dest, void *src, size_t n)
|
||||
return d;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
checksum(const void *p, size_t len, size_t off)
|
||||
{
|
||||
uint8_t sum = 0;
|
||||
const uint8_t *c = reinterpret_cast<const uint8_t *>(p);
|
||||
for (int i = off; i < len; ++i) sum += c[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
} // namespace kutil
|
||||
|
||||
@@ -66,4 +66,10 @@ inline T* mask_pointer(T *p, addr_t mask)
|
||||
return reinterpret_cast<T *>(reinterpret_cast<addr_t>(p) & ~mask);
|
||||
}
|
||||
|
||||
/// Do a simple byte-wise checksum of an area of memory.
|
||||
/// \arg p The start of the memory region
|
||||
/// \arg len The number of bytes in the region
|
||||
/// \arg off An optional offset into the region
|
||||
uint8_t checksum(const void *p, size_t len, size_t off = 0);
|
||||
|
||||
} // namespace kutil
|
||||
|
||||
Reference in New Issue
Block a user