summaryrefslogtreecommitdiff
path: root/boot/efi/image.c
blob: 25e19114177920102a2d8fd737c4d56fcbf03261 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <posix/unistd.h>
#include <posix/fcntl.h>
#include <kjarna/interface.h>
#include <libc/string.h>
#include <libc/stdio.h>
#include <libc/stdlib.h>
#include <lib/elf.h>
#include <posix/unistd.h>
#include <posix/sys/mman.h>

#include "kjarna_efi.h"

#include "config.h"

#include <efi/error.h>

struct image_buffer
{
	char *base;
	size_t length;
};

static int create_image_buffer(int fd, struct image_buffer *buffer)
{
	if ((buffer->length = elf64_size_fd(fd)) == 0)
	{
		return -1;
	}

	printf("creating image buffer, %d bytes\n", buffer->length);

	if((buffer->base = mmap(nullptr, buffer->length, 0, 0, -1, 0)) == MAP_FAILED)
	{
		buffer->length = 0;
		buffer->base = nullptr;
		
		return -1;
	}

	printf("image buffer base %p\n", buffer->base);

	return 0;
}

static struct image_buffer load_image(void)
{
	const char *image_entry_path = SERVICE_FILE_PATH;

	int image_fd;

	struct image_buffer buffer = { nullptr, 0 };

	if ((image_fd = open(image_entry_path, O_RDONLY, 0)) == -1)
	{
		return buffer;
	}

	if (!elf64_validate_fd(image_fd, ET_DYN, EM_X86_64))
	{
		return buffer;
	}

	if (create_image_buffer(image_fd, &buffer) < 0)
	{
		return buffer;
	}

	if (buffer.base != nullptr && elf64_load_segments(image_fd, buffer.length, buffer.base) < 0)
	{
		printf("error detected: buffer {base: %p, length: %zu}\n", buffer.base, buffer.length);
		munmap(buffer.base, buffer.length);
		buffer.base = nullptr;
	}

	close(image_fd);

	return buffer;
}

kjarna_image_entry_func *image_entry_addr(struct image_buffer *buffer)
{
	if (buffer->base == nullptr)
	{
		return nullptr;
	}

	Elf64_Ehdr *ehdr = (Elf64_Ehdr *)buffer->base;

	return (kjarna_image_entry_func *)(buffer->base + ehdr->e_entry);
}

int image_start(void)
{
	struct kjarna_interface interface =
	{
		efi_open,
		efi_close,
		efi_lseek,
		efi_read,
		efi_write,
		efi_mmap,
		efi_munmap
	};

	struct kjarna_entry_params params =
	{
		&interface,
		0,
		nullptr,
		nullptr
	};

	struct image_buffer buffer = load_image();

	kjarna_image_entry_func *image_entry = nullptr;

	if (buffer.base != nullptr)
	{
		image_entry = image_entry_addr(&buffer);
	}

	if (image_entry != nullptr)
	{
		image_entry(&params);
	}

	if (munmap(buffer.base, buffer.length) == -1)
	{
		write(STDOUT_FILENO, "failed unmap\r\n", -1);

		if (efi_errno == EFI_NOT_FOUND)
		{
			write(STDOUT_FILENO, "not found\r\n", -1);
		}
		else
		{
			write(STDOUT_FILENO, "bad alignment?\r\n", -1);
		}
	}

	return -1;
}