Skip to content

Commit

Permalink
Tar-based initrd finished, modules now load from initrd.tar at boot
Browse files Browse the repository at this point in the history
  • Loading branch information
mushrom committed Jun 7, 2014
1 parent 2d38877 commit 7c97551
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 37 deletions.
6 changes: 0 additions & 6 deletions Makefile
Expand Up @@ -47,9 +47,6 @@ image:
@echo -e "[\033[0;32mMaking image\033[0;0m]"
@cp -r boot-image build
@cp kernel/helix_kernel-i586 build
@# The order of the files in kernel/modules is important, the modules
@# are loaded in this order
@#tools/mkinitrd build/initrd.img kernel/modobjs/{pci,ata,vga,devfs,fatfs,ps2keybd,dummy}_mod.o
@tar c kernel/modobjs kernel/config > build/initrd.tar
@./tools/mk_image.sh
@echo "To boot: $(EMULATOR) $(EMU_FLAGS)"
Expand Down Expand Up @@ -80,6 +77,3 @@ clean:
@-rm -rf build

.PHONY: all

meh:
tools/mkinitrd ./initrd.img kernel/modules/*
23 changes: 23 additions & 0 deletions kernel/base/initrd.c
Expand Up @@ -13,6 +13,10 @@ static unsigned int octal_to_dec( int oct ){
return ret;
}

/** \brief Initializes an \ref initrd_t struct from a pointer to a tar archive in memory.
* @param header Pointer to a tar archive entirely in memory.
* @return An \ref initrd_t struct representing the archive, for quick lookups.
*/
initrd_t *init_initrd( tar_header_t *header ){
tar_header_t *move;
initrd_t *ret = 0;
Expand All @@ -39,10 +43,21 @@ initrd_t *init_initrd( tar_header_t *header ){
return ret;
}

/** \brief Gets a given tar header from an initrd_t struct by index.
* @param rd initrd_t struct returned from \ref init_initrd.
* @param index Which file to get from the archive, starting from 0
* @return If the file exists, the tar_header_t for the index given, otherwise it returns 0.
*/

tar_header_t *initrd_get( initrd_t *rd, int index ){
return dlist_get( rd->headers, index );
}

/** \brief Gets a tar header from an initrd_t struct by name.
* @param rd initrd_t struct returned from \ref init_initrd.
* @param name The filename to find in the archive.
* @return If the file exists, the tar_header_t for the name given, otherwise it returns 0.
*/
tar_header_t *initrd_get_file( initrd_t *rd, char *name ){
tar_header_t *ret = 0,
*move;
Expand All @@ -59,6 +74,14 @@ tar_header_t *initrd_get_file( initrd_t *rd, char *name ){
return ret;
}


/** \brief Gets the size of the file a header points to.
*
* This is needed for cleanliness, since tar uses ascii strings for numerical fields.
*
* @param header The header to get the size of.
* @return The size of the file.
*/
unsigned initrd_get_size( tar_header_t *header ){
return octal_to_dec( atoi( header->size ));
}
19 changes: 3 additions & 16 deletions kernel/base/kmain.c
Expand Up @@ -23,7 +23,7 @@ extern unsigned int early_placement;
void kmain( multiboot_header_t *mboot, int blarg, int magic ){
void *modules;
multiboot_elf_t *elfinfo = 0;
initrd_t *rd;
initrd_t *initrd;

kprintf( "-==[ Helix kernel booting\n" );

Expand All @@ -35,7 +35,6 @@ void kmain( multiboot_header_t *mboot, int blarg, int magic ){
elfinfo = &mboot->elf_headers;

if ( mboot->flags & MULTIBOOT_FLAG_MODS && mboot->mods_count ){
//modules = (int *)*(int *)mboot->mods_addr;
modules = *(int **)mboot->mods_addr;
early_placement = *(int *)(mboot->mods_addr + 4);
}
Expand All @@ -47,31 +46,19 @@ void kmain( multiboot_header_t *mboot, int blarg, int magic ){
kheap = kmalloc_early( sizeof( mheap_t ), 0 );
init_heap( kheap, kernel_dir, 0xd0000000, PAGE_SIZE * 8 );

rd = init_initrd( modules );
tar_header_t *meh = initrd_get_file( rd, "kernel/config/modtab" );
char *testptr;
unsigned i, size;

testptr = (char *)(meh + 1);
size = initrd_get_size( meh );
for ( i = 0; i < size; i++ )
kprintf( "%c", testptr[i] );

while( 1 );
initrd = init_initrd( modules );

asm volatile( "sti" );
init_syscalls( );
init_pitimer( 1000 );
init_tasking( );

//create_thread( sometest );

init_hal( );
init_vfs( );

// Initialize module system
init_module_system( elfinfo );
load_init_modules( modules );
load_init_modules( initrd );

hal_dump_devices( );
dump_aheap_blocks( kheap );
Expand Down
56 changes: 42 additions & 14 deletions kernel/base/module.c
Expand Up @@ -75,26 +75,54 @@ void init_module_system( multiboot_elf_t *elfinfo ){
mods_inited = 1;
}

void load_init_modules( mhead_t *initmods ){
mtable_t *table;
/** \brief Loads the initial modules listed in "kernel/config/modtab" in the initrd.
* On failing to load a module, it continues and tries the next module listed.
* @param initmods The initrd object returned from \ref init_initrd.
*/
void load_init_modules( initrd_t *initmods ){
tar_header_t *tarhead;
Elf32_Ehdr *elf_buf;
int i;
char *namebuf;
char *modtab;
int i, j;
unsigned size;

if ( !initmods ){
kprintf( "[load_init_modules] Was passed a null pointer! Can't load modules\n" );
return;
}
if ( initmods ){
kprintf( "Initmods at 0x%x\n", initmods );

tarhead = initrd_get_file( initmods, "kernel/config/modtab" );
if ( tarhead ){
modtab = (char *)(tarhead + 1);
size = initrd_get_size( tarhead );
namebuf = knew( char[128] );

for ( i = 0; i < size; ){
for ( j = 0; modtab[i + j] != '\n' && j < size; j++ )
namebuf[j] = modtab[i + j];

kprintf( "Initmods at 0x%x: 0x%x: 0x%x\n", initmods, initmods->entries, initmods->magic );
namebuf[j] = 0;

table = (mtable_t *)((unsigned)initmods + initmods->table_offset );
for ( i = 0; i < initmods->entries; i++ ){
kprintf( "0x%x: 0x%x, 0x%x, 0x%x\n",
table + i, table[i].magic, table[i].offset, table[i].len );
kprintf( "[%s] Have module \"%s\"\n", __func__, namebuf );
tarhead = initrd_get_file( initmods, namebuf );
if ( tarhead ){
elf_buf = (Elf32_Ehdr *)( tarhead + 1 );
load_module( elf_buf );

elf_buf = (Elf32_Ehdr *)((unsigned)initmods + table[i].offset );
} else {
kprintf( "[%s] Could not find module \"%s\"\n", __func__, namebuf );
}

i += j + 1;
}

kfree( namebuf );

} else {
kprintf( "[load_init_modules] Could not find modtab file, can't load modules\n" );
}

load_module( elf_buf );
} else {
kprintf( "[load_init_modules] Was passed a null pointer, Can't load modules\n" );
}
}

Expand Down
4 changes: 3 additions & 1 deletion kernel/include/base/module.h
Expand Up @@ -7,6 +7,7 @@
#include <base/datastructs/list.h>
#include <base/datastructs/dlist.h>
#include <base/datastructs/hashmap.h>
#include <base/initrd.h>

// Some structs to access the initmods images
typedef struct mtable_header {
Expand Down Expand Up @@ -49,7 +50,8 @@ typedef struct module {
} module_t;

void init_module_system( multiboot_elf_t *elfinfo );
void load_init_modules( mhead_t *initmods );
//void load_init_modules( mhead_t *initmods );
void load_init_modules( initrd_t *initmods );
int load_module( Elf32_Ehdr *elf_obj );

#endif

0 comments on commit 7c97551

Please sign in to comment.