60 lines
1.2 KiB
C
60 lines
1.2 KiB
C
#include <kernel/loader.h>
|
|
#include <kernel/fs/smem.h>
|
|
#include <kernel/fs/file.h>
|
|
#include <kernel/util.h>
|
|
#include <kernel/elf.h>
|
|
|
|
void *loader(const char *path, process_t *process)
|
|
{
|
|
Elf32_Ehdr header;
|
|
FILE file;
|
|
|
|
// Check error.
|
|
if (process == NULL)
|
|
return (NULL);
|
|
|
|
// TODO: use VFS !
|
|
if (casio_smem_open(&file, path, O_RDONLY) != 0)
|
|
return (NULL);
|
|
|
|
// Debug !
|
|
kvram_clear();
|
|
printk(0, 0, "File found !!");
|
|
printk(0, 1, "inode = %p", file.abstract);
|
|
printk(0, 2, "path = %s", path);
|
|
kvram_display();
|
|
DBG_WAIT;
|
|
|
|
//@NOTE: Only ELF supported !!!
|
|
//TODO: USE VFS !!
|
|
//
|
|
// Get / Check ELF header
|
|
if (loader_get_header(&file, &header) != 0)
|
|
{
|
|
kvram_clear();
|
|
printk(0, 0, "loader: ELF file header error !");
|
|
kvram_display();
|
|
DBG_WAIT;
|
|
return (NULL);
|
|
}
|
|
|
|
// Get / Check program validity
|
|
void *entry = loader_load_image(&file, &header, process);
|
|
if (entry == NULL)
|
|
{
|
|
kvram_clear();
|
|
printk(0, 0, "loader: ELF file image error !");
|
|
kvram_display();
|
|
DBG_WAIT;
|
|
return (NULL);
|
|
}
|
|
|
|
// Success !
|
|
kvram_clear();
|
|
printk(0, 0, "loader: file loaded sucees !!");
|
|
printk(0, 1, "loader: Entry at %p", entry);
|
|
kvram_display();
|
|
DBG_WAIT;
|
|
return (entry);
|
|
}
|