aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/fs/elf.c
blob: dd1e764a6387188c0278fd40ea54ede2c63f73b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <kernel/system.h>
#include <kernel/fs/elf.h>
#include <kernel/lib/stdio.h>
#include <kernel/memory/alloc.h>
#include <kernel/lib/lib.h>
#include <kernel/memory/paging.h>
#include <kernel/fs/ext2.h>

int is_elf(elf_header_t *header)
{
	if (header->ident[0] == ELF_MAG && header->ident[1] == 'E' && header->ident[2] == 'L' &&
	    header->ident[3] == 'F' && header->ident[4] == ELF_32 &&
	    header->ident[5] == ELF_LITTLE && header->ident[6] == ELF_CURRENT &&
	    header->machine == ELF_386 && (header->type == ET_REL || header->type == ET_EXEC)) {
		return 1;
	}
	return 0;
}

void elf_load(char *path)
{
	uint8_t *file = read_file(path);
	if (!file) {
		warn("File or directory not found: %s", file);
		return;
	}

	elf_header_t *header = (elf_header_t *)file;
	elf_program_header_t *program_header = (void *)header + header->phoff;

	if (!is_elf(header)) {
		warn("File not valid: %s", path);
		return;
	} else {
		debug("File is valid: %s", path);
	}

	uint32_t seg_begin, seg_end;
	for (int i = 0; i < header->phnum; i++) {
		if (program_header->type == 1) {
			seg_begin = program_header->vaddr;
			seg_end = seg_begin + program_header->memsz;

			for (uint32_t z = 0; z < seg_end - seg_begin; z += 4096)
				paging_map((uint32_t)seg_begin + z, (uint32_t)seg_begin + z,
					   PT_PRESENT | PT_RW | PT_USED | PT_ALL_PRIV);

			memcpy((void *)seg_begin, file + program_header->offset,
			       program_header->filesz);
			memset((void *)(seg_begin + program_header->filesz), 0,
			       program_header->memsz - program_header->filesz);

			// Code segment
			if (program_header->flags == PF_X + PF_R + PF_W ||
			    program_header->flags == PF_X + PF_R) {
				debug("Found code segment");
				// current_process->regs.eip = header->entry + seg_begin;
			}
		}
		program_header++;
	}
}