2020-01-01 14:19:18 +01:00
|
|
|
#include <kernel/loader.h>
|
2020-01-05 09:00:43 +01:00
|
|
|
#include <kernel/fs/vfs.h>
|
2020-01-01 14:19:18 +01:00
|
|
|
|
|
|
|
/* loader_get_header() - get ELF header and check validity */
|
|
|
|
int loader_get_header(FILE *file, Elf32_Ehdr *header)
|
|
|
|
{
|
|
|
|
// Read ELF header.
|
2020-01-05 09:00:43 +01:00
|
|
|
if (vfs_read(file, header, sizeof(Elf32_Ehdr)) != sizeof(Elf32_Ehdr))
|
2020-01-01 14:19:18 +01:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
// Check magic number
|
2020-03-18 17:48:53 +01:00
|
|
|
if (header->e_ident[EI_MAG0] != ELFMAG0 ||
|
|
|
|
header->e_ident[EI_MAG1] != ELFMAG1 ||
|
|
|
|
header->e_ident[EI_MAG2] != ELFMAG2 ||
|
|
|
|
header->e_ident[EI_MAG3] != ELFMAG3)
|
2020-01-01 14:19:18 +01:00
|
|
|
return (-2);
|
|
|
|
|
|
|
|
// Check class
|
2020-03-18 17:48:53 +01:00
|
|
|
if (header->e_ident[EI_CLASS] != ELFCLASS32)
|
2020-01-01 14:19:18 +01:00
|
|
|
return (-3);
|
|
|
|
|
|
|
|
//TODO: Check OSABI (?)
|
|
|
|
|
|
|
|
// Check ELF type.
|
2020-03-18 17:48:53 +01:00
|
|
|
if (header->e_type != ET_DYN)
|
2020-01-01 14:19:18 +01:00
|
|
|
return (-4);
|
|
|
|
|
|
|
|
// Check ELF specifique instruction
|
2020-03-18 17:48:53 +01:00
|
|
|
if (header->e_machine != EM_SH)
|
2020-01-01 14:19:18 +01:00
|
|
|
return (-5);
|
|
|
|
|
|
|
|
// Check ELF version
|
2020-03-18 17:48:53 +01:00
|
|
|
if (header->e_version != EV_CURRENT)
|
2020-01-01 14:19:18 +01:00
|
|
|
return (-6);
|
|
|
|
return (0);
|
|
|
|
}
|