forked from yabits/ebcvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
81 lines (67 loc) · 2.06 KB
/
main.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
#include "ebcvm.h"
char *bin_path;
static void init() {
FLAGS_debug = false;
FLAGS_mem = AUTO_MEM_SIZE;
FLAGS_stack = STACK_SIZE;
FLAGS_heap = HEAP_SIZE;
FLAGS_step = false;
FLAGS_reloc = true;
}
static void print_usage(const char *argv0) {
fprintf(stderr, "Usage: %s [OPTION] FILE\n", argv0);
fprintf(stderr, "EFI Byte Code Interpreter\n");
fprintf(stderr, "[OPTION]\n");
fprintf(stderr, " --debug={0,1}\t\tdebug mode\n");
fprintf(stderr, " --mem=INT\t\tsize of memory\n");
fprintf(stderr, " --stack=INT\t\tsize of stack\n");
fprintf(stderr, " --heap=INT\t\tsize of heap\n");
fprintf(stderr, " --step={0,1}\t\tstep execution\n");
fprintf(stderr, " --reloc={0,1}\t\trelocate sections\n");
fprintf(stderr, " --help\t\tshow this help\n");
}
int main(int argc, char *argv[]) {
if (argc < 2) {
print_usage(argv[0]);
exit(0);
}
init();
for (int i = 1; i < argc; i++) {
int n;
char junk;
if (!strcmp(argv[i], "--help")) {
print_usage(argv[0]);
exit(0);
} else if (i == argc - 1) {
bin_path = argv[i];
} else if (sscanf(argv[i], "--mem=%d%c", &n, &junk) == 1 && n > 0) {
FLAGS_mem = n;
} else if (sscanf(argv[i], "--stack=%d%c", &n, &junk) == 1 && n > 0) {
FLAGS_stack = n;
} else if (sscanf(argv[i], "--heap=%d%c", &n, &junk) == 1 && n > 0) {
FLAGS_heap = n;
} else if (sscanf(argv[i], "--step=%d%c", &n, &junk) == 1 &&
(n == 0 || n == 1)) {
FLAGS_step = n;
} else if (sscanf(argv[i], "--debug=%d%c", &n, &junk) == 1 &&
(n == 0 || n == 1)) {
FLAGS_debug = n;
} else if (sscanf(argv[i], "--reloc=%d%c", &n, &junk) == 1 &&
(n == 0 || n == 1)) {
FLAGS_reloc = n;
} else {
error("invalid flag '%s'\n", argv[i]);
}
}
if (FLAGS_mem != AUTO_MEM_SIZE && FLAGS_mem < FLAGS_heap + FLAGS_stack)
error("memory size is too small");
vm *_vm = init_vm();
if (FLAGS_debug)
_dbg = init_dbg(_vm);
_vm = load_exe(bin_path, _vm);
exec_vm(_vm);
fini_vm(_vm);
if (FLAGS_debug)
fini_dbg(_dbg);
return 0;
}