-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloader.c
214 lines (172 loc) · 5.53 KB
/
loader.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <setjmp.h>
#include <string.h>
#include <setjmp.h>
#include <loader.h>
#include <allocator.h>
#include <multiboot.h>
#include <sys/queue.h>
struct loader_callbacks *callbacks;
void *callbacks_arg;
size_t lowmem = 0;
size_t highmem = 0;
jmp_buf jb;
struct args loader_args = {
.kernel_filename = NULL,
.cmdline = NULL,
.modules = SLIST_HEAD_INITIALIZER(.modules),
};
uint32_t
parse_args(struct args* args)
{
const char *var, *delim, *delim2, *value, *value2, *end;
int i = 0;
struct module *new_module;
/* iterate over environment */
while ( (var = CALLBACK(getenv, i++)) ) {
delim = strchr(var, '=');
value = delim+1;
end = var + strlen(var);
if (!delim) {
/* no value specified */
delim = var + strlen(var);
value = NULL;
}
printf("%s\r\n", var);
if (!strncmp(var, "kernel", delim-var)) {
if (!value) {
ERROR(EINVAL,"no kernel filename provided");
return EINVAL;
}
args->kernel_filename = value;
}
if (!strncmp(var, "cmdline", delim-var)) {
args->cmdline = value;
}
if (!strncmp(var, "module", delim-var)) {
delim2 = strchr(value, ':');
/* If no delimiter was found, no optional string was given */
if (!delim2)
delim2 = end;
/* If the delimiter is the last character, remove it. */
if (delim2 == end - 1) {
end --;
}
/* The optional string starts directly after the deliminator */
value2 = delim2 + 1;
new_module = calloc(1, sizeof(struct module));
new_module->filename = malloc(delim2 - value + 1);
memcpy(new_module->filename, value, delim2 - value);
new_module->filename[delim2 - value] = '\0';
new_module->string = NULL;
if (delim2 != end) {
new_module->string = malloc(end - value2 + 1);
memcpy(new_module->string, value2, end - value2 + 1);
new_module->string[end - value2] = '\0';
}
SLIST_INSERT_HEAD(&loader_args.modules, new_module, next);
}
}
return 0;
}
void
loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
{
void *kernfile = NULL;
int mode = 0, uid = 0, gid = 0;
size_t kernsz = 0, resid = 0;
void *kernel = NULL;
struct multiboot *mb = NULL;
if (version < USERBOOT_VERSION)
abort();
callbacks = cb;
callbacks_arg = arg;
/* setjmp error anchor */
if (setjmp(jb)) {
CALLBACK(exit, errno);
return;
}
if (parse_args(&loader_args))
goto error;
if (callbacks->open(callbacks_arg, loader_args.kernel_filename, &kernfile))
{
ERROR(errno, "could not open kernel");
goto error;
}
/* Get the memory layout */
callbacks->getmem(callbacks_arg, &lowmem, &highmem);
printf("lowmem = %lu, highmem = %lu\r\n", lowmem, highmem);
/* Initialize the allocator */
init_allocator(lowmem, highmem);
/* Check that a kernel file was provided */
if (!kernfile) {
ERROR(EINVAL, "No kernel given.");
goto error;
}
/* Get the kernel file size */
callbacks->stat(callbacks_arg, kernfile, &mode, &uid, &gid, &kernsz);
printf("kernel size = %ld\r\n", kernsz);
/* Read the kernel */
kernel = malloc(kernsz);
if (!kernel || callbacks->read(callbacks_arg, kernfile,
kernel, kernsz, &resid))
{
ERROR(errno, "Unable to read kernel");
goto error;
}
/* Scan for the multiboot header */
if (!(mb = mb_scan(kernel, kernsz))) {
ERROR(EINVAL, "No multiboot header found.");
goto error;
}
/* We don't support multiboot2 yet */
if (mb->magic == MULTIBOOT2_MAGIC) {
ERROR(ENOTSUP, "Multiboot2 is not supported yet.");
goto error;
}
else {
if (multiboot_load(kernel, kernsz, mb)) {
goto error;
}
if (multiboot_info_set_meminfo(&mb->info, lowmem, highmem)) {
ERROR(EINVAL, "Could not set up memory information");
goto error;
}
if (multiboot_info_set_mmap(&mb->info)) {
ERROR(EINVAL, "Could not set up memory map");
goto error;
}
if (loader_args.cmdline &&
multiboot_info_set_cmdline(&mb->info, loader_args.cmdline))
{
ERROR(EINVAL, "Could not set kernel command line");
goto error;
}
if (multiboot_info_set_loader_name(&mb->info, BOOTLOADER_NAME)) {
ERROR(EINVAL, "Could not set bootloader name");
goto error;
}
if (mb->header.mb.header->flags & MULTIBOOT_FLAG_GRAPHICS) {
ERROR(ENOTSUP, "VBE info requested by kernel, but not supported.");
}
if (multiboot_load_modules(mb, &loader_args.modules)) {
ERROR(ECANCELED, "Could not load modules");
}
if (multiboot_info_finalize(mb)) {
ERROR(ECANCELED, "Could not set up multiboot information structure");
}
}
/* Cleanup. */
if (mb) free(mb);
if (kernel) free(kernel);
if (kernfile) callbacks->close(callbacks_arg, kernfile);
CALLBACK(exit, 0);
return;
error:
if (mb) free(mb);
if (kernel) free(kernel);
if (kernfile) callbacks->close(callbacks_arg, kernfile);
longjmp(jb, 1);
}