vxBoot/include/vxBoot/loader.h

190 lines
4.5 KiB
C

#ifndef __VXBOOT_LOADER_H__
# define __VXBOOT_LOADER_H__
#include <stddef.h>
#include <stdint.h>
#include "vxBoot/hardware.h"
#include "vxBoot/fs/smemfs.h"
//---
// ELF struct definition
//---
/* The ELF file header. This appears at the start of every ELF file. */
#define EI_NIDENT (16)
typedef struct {
uint8_t e_ident[EI_NIDENT]; /* Magic number and other info */
uint16_t e_type; /* Object file type */
uint16_t e_machine; /* Architecture */
uint32_t e_version; /* Object file version */
uint32_t e_entry; /* Entry point virtual address */
uint32_t e_phoff; /* Program header table file offset */
uint32_t e_shoff; /* Section header table file offset */
uint32_t e_flags; /* Processor-specific flags */
uint16_t e_ehsize; /* ELF header size in bytes */
uint16_t e_phentsize; /* Program header table entry size */
uint16_t e_phnum; /* Program header table entry count */
uint16_t e_shentsize; /* Section header table entry size */
uint16_t e_shnum; /* Section header table entry count */
uint16_t e_shstrndx; /* Section header string table index */
} Elf32_Ehdr;
/* Fields in the e_ident array */
#define EI_MAG0 0 /* File identification byte 0 index */
#define EI_MAG1 1 /* File identification byte 1 index */
#define EI_MAG2 2 /* File identification byte 2 index */
#define EI_MAG3 3 /* File identification byte 3 index */
#define ELFMAG0 0x7f /* Magic number byte 0 */
#define ELFMAG1 'E' /* Magic number byte 1 */
#define ELFMAG2 'L' /* Magic number byte 2 */
#define ELFMAG3 'F' /* Magic number byte 3 */
/* class information */
#define EI_CLASS 4 /* File class byte index */
#define ELFCLASS32 1 /* 32-bit objects */
/* data encoding information */
#define EI_DATA 5 /* Data encoding byte index */
#define ELFDATA2MSB 2 /* 2's complement, big endian */
/* object file type */
#define ET_EXEC 2 /* Executable file */
#define ET_DYN 3 /* Shared object file */
/* architecture */
#define EM_SH 42 /* Hitachi SH */
/* ELF version */
#define EV_CURRENT 1 /* Current version */
/* Program segment header. */
typedef struct {
uint32_t p_type; /* Segment type */
uint32_t p_offset; /* Segment file offset */
uint32_t p_vaddr; /* Segment virtual address */
uint32_t p_paddr; /* Segment physical address */
uint32_t p_filesz; /* Segment size in file */
uint32_t p_memsz; /* Segment size in memory */
uint32_t p_flags; /* Segment flags */
uint32_t p_align; /* Segment alignment */
} Elf32_Phdr;
//---
// Loader API
//---
/* kernel loading information */
struct kernel_info {
/* kernel entry */
uintptr_t entry;
/* memory information */
struct {
struct {
void *start;
size_t size;
struct {
uintptr_t vmin;
uintptr_t vmax;
} elf;
} program;
} memory;
/* hardware information */
struct hwinfo hardware;
};
/* internal kernel image list */
struct ldimg {
struct smemfs_inode * inode;
struct ldimg *next;
};
/* loader_scan() : scan the storage memory and try to find ELF PIE file */
extern int loader_scan(void);
/* loader() : try to load a ELF PIE file */
#define LOADER_DEFAULT 0
#define LOADER_DUMP 1
#define LOADER_TRACE 2
extern int loader(struct smemfs_inode const * restrict const inode, int mode);
//---
// Error ldpers
//---
struct ld_error_db {
const int id;
const char *strerror;
};
extern int loader_error(
struct ld_error_db const * const db,
char const * restrict const prefix,
int const errnum
);
//---
// ELF Header ldpers
//---
enum {
ld_header_valid = 0,
ld_header_size_error = -1,
ld_header_magic_error = -2,
ld_header_class_error = -3,
ld_header_indent_error = -4,
ld_header_type_error = -5,
ld_header_machine_error = -6,
ld_header_version_error = -7,
};
/* loader_header_get() : get / check ELF header information */
extern int loader_header_get(
struct smemfs_inode const * restrict const inode,
Elf32_Ehdr *hdr
);
/* loader_header_check() : load and check the file header validity */
extern int loader_header_check(struct smemfs_inode * restrict const inode);
extern int loader_header_error(int errnum);
//---
// ELF image ldpers
//---
enum {
ld_image_success = 0,
ld_image_size_error = -1,
ld_image_type_error = -2,
ld_image_mem_error = -3,
ld_image_mem_not_available = -4,
};
/* loader_image_load() : try to load the entier image data */
extern int loader_image_load(
struct smemfs_inode const * restrict const inode,
struct kernel_info * restrict const info,
Elf32_Ehdr *hdr
);
extern int loader_image_error(int const errnum);
#endif /*__VXBOOT_LOADER_H__*/