2019-12-30 16:31:03 +01:00
|
|
|
#include <kernel/loader.h>
|
2020-01-05 09:00:43 +01:00
|
|
|
#include <kernel/fs/vfs.h>
|
2019-12-30 16:31:03 +01:00
|
|
|
#include <kernel/fs/file.h>
|
2020-02-24 00:05:27 +01:00
|
|
|
#include <kernel/util/elf.h>
|
2020-03-15 00:56:31 +01:00
|
|
|
#include <kernel/devices/earlyterm.h>
|
2020-03-30 00:40:59 +02:00
|
|
|
#include <fcntl.h>
|
2019-12-30 16:31:03 +01:00
|
|
|
|
2020-03-15 00:56:31 +01:00
|
|
|
//
|
|
|
|
// TODO: write doc
|
|
|
|
//
|
|
|
|
int loader(struct process *process, const char *path)
|
2019-12-30 16:31:03 +01:00
|
|
|
{
|
2020-01-01 14:19:18 +01:00
|
|
|
Elf32_Ehdr header;
|
2019-12-30 16:31:03 +01:00
|
|
|
FILE file;
|
2020-03-23 15:58:48 +01:00
|
|
|
int err;
|
2019-12-30 16:31:03 +01:00
|
|
|
|
2020-03-15 00:56:31 +01:00
|
|
|
// Check error and try to open the file.
|
|
|
|
if (process == NULL || vfs_open(&file, path, O_RDONLY) != 0)
|
2020-01-12 15:58:35 +01:00
|
|
|
{
|
2020-03-23 15:58:48 +01:00
|
|
|
earlyterm_write("loader: Fault error !\n");
|
|
|
|
earlyterm_write("* path: %s$\n", path);
|
|
|
|
earlyterm_write("* process: %p\n", process);
|
2020-01-12 15:58:35 +01:00
|
|
|
DBG_WAIT;
|
2020-03-15 00:56:31 +01:00
|
|
|
return (-1);
|
2020-01-12 15:58:35 +01:00
|
|
|
}
|
2020-01-01 14:19:18 +01:00
|
|
|
|
|
|
|
//@NOTE: Only ELF supported !!!
|
|
|
|
// Get / Check ELF header
|
2020-03-15 00:56:31 +01:00
|
|
|
// FIXME: return the file type (elf, g1a, bin, ...)
|
2020-03-23 15:58:48 +01:00
|
|
|
err = loader_get_header(&file, &header);
|
|
|
|
if (err != 0)
|
2020-01-01 14:19:18 +01:00
|
|
|
{
|
2020-03-23 15:58:48 +01:00
|
|
|
earlyterm_write("loader: ELF file header error ! (%d)\n", err);
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
2020-01-01 14:19:18 +01:00
|
|
|
DBG_WAIT;
|
2020-03-15 00:56:31 +01:00
|
|
|
return (-2);
|
2020-01-01 14:19:18 +01:00
|
|
|
}
|
2019-12-30 16:31:03 +01:00
|
|
|
|
2020-01-01 14:19:18 +01:00
|
|
|
// Get / Check program validity
|
2020-03-23 15:58:48 +01:00
|
|
|
err = loader_load_image(process, &file, &header);
|
|
|
|
if (err != 0)
|
2020-01-01 14:19:18 +01:00
|
|
|
{
|
2020-03-23 15:58:48 +01:00
|
|
|
earlyterm_write("loader: ELF file image error ! (%d)\n", err);
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
2020-01-01 14:19:18 +01:00
|
|
|
DBG_WAIT;
|
2020-03-15 00:56:31 +01:00
|
|
|
return (-3);
|
2020-01-01 14:19:18 +01:00
|
|
|
}
|
2020-03-18 17:48:53 +01:00
|
|
|
|
|
|
|
// Relocalise sections / symbols
|
2020-03-23 15:58:48 +01:00
|
|
|
err = loader_reloc_sym(process, &file, &header);
|
|
|
|
if (err != 0)
|
2020-03-18 17:48:53 +01:00
|
|
|
{
|
2020-03-23 15:58:48 +01:00
|
|
|
earlyterm_write("loader: ELF relo error ! (%d)\n", err);
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
|
|
|
DBG_WAIT;
|
2020-03-18 17:48:53 +01:00
|
|
|
DBG_WAIT;
|
|
|
|
return (-4);
|
|
|
|
}
|
2020-03-28 19:52:59 +01:00
|
|
|
vfs_close(&file);
|
2020-03-15 00:56:31 +01:00
|
|
|
return (0);
|
2019-12-30 16:31:03 +01:00
|
|
|
}
|