-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
212 lines (180 loc) · 5.65 KB
/
main.cpp
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
#include "bus.hpp"
#include "clint.hpp"
#include "cpu.hpp"
#include "cpu_config.hpp"
#include "gpu.hpp"
#include "helper.hpp"
#include "plic.hpp"
#include "ram.hpp"
#include "syscon.hpp"
#include "virtio.hpp"
#include <algorithm>
#include <array>
#include <cstring>
#include <filesystem>
#include <fmt/core.h>
#include <getopt.h>
#include <iostream>
#include <utility>
void print_usage(char* argv[])
{
std::cerr << fmt::format(
"Usage: {} [options]\n"
"Options:\n"
" -b, --bios Path to the BIOS file (mandatory)\n"
#if !NATIVE_CLI
" -f, --font Path to the font file (mandatory)\n"
#endif
" -d, --dtb Path to the device tree blob file (optional, "
"mandatory if kernel is present)\n"
" -k, --kernel Path to the kernel file (optional)\n"
" -m, --memory Emulator RAM buffer size in MiB (optional, default 64 MiB)\n"
" -v, --virtual-drive Path to virtual disk image to use as a filesystem (optional)\n",
argv[0]);
}
void error_exit(char* argv[], const std::string& error_message)
{
std::cerr << fmt::format("Error: {}\n", error_message);
print_usage(argv);
exit(1);
}
bool file_exists(const char* path)
{
return std::filesystem::exists(path);
}
bool patch_dtb_ram_size(std::vector<uint8_t>& dtb_data, uint32_t ram_size)
{
static constexpr std::array<uint8_t, sizeof(uint32_t)> search_pattern = {0x0b, 0xad, 0xc0,
0xde};
auto it =
std::search(dtb_data.begin(), dtb_data.end(), search_pattern.begin(), search_pattern.end());
if (it == dtb_data.end())
{
return false;
}
uint32_t ram_size_be = ((ram_size & 0xff000000U) >> 24U) | ((ram_size & 0x00ff0000U) >> 8U) |
((ram_size & 0x0000ff00U) << 8U) | ((ram_size & 0x000000ffU) << 24U);
memcpy(&*it, &ram_size_be, sizeof(ram_size_be));
return true;
}
int main(int argc, char* argv[])
{
const char* bios_path = nullptr;
const char* font_path = nullptr;
const char* dtb_path = nullptr;
const char* kernel_path = nullptr;
const char* virt_drive_path = nullptr;
uint64_t ram_size = SIZE_MIB(64);
// clang-format off
static constexpr option long_options[] = {
{"bios", required_argument, nullptr, 'b'},
{"font", required_argument, nullptr, 'f'},
{"dtb", required_argument, nullptr, 'd'},
{"kernel", required_argument, nullptr, 'k'},
{"memory", required_argument, nullptr, 'm'},
{"virtual-drive", required_argument, nullptr, 'v'},
{}
};
// clang-format on
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, "b:f:d:k:m:v:", long_options, &option_index)) != -1)
{
switch (opt)
{
case 'b':
bios_path = optarg;
break;
case 'f':
font_path = optarg;
break;
case 'd':
dtb_path = optarg;
break;
case 'k':
kernel_path = optarg;
break;
case 'm':
ram_size = SIZE_MIB(atoi(optarg));
break;
case 'v':
virt_drive_path = optarg;
break;
default:
print_usage(argv);
exit(1);
}
}
if (bios_path == nullptr
#if !NATIVE_CLI
|| font_path == nullptr
#endif
)
{
#if !NATIVE_CLI
error_exit(argv, "both bios_path and font_path must be provided");
#else
error_exit(argv, "bios_path must be provided");
#endif
}
if (kernel_path != nullptr && dtb_path == nullptr)
{
error_exit(argv, "dtb path must be provided when kernel path is provided");
}
if (!file_exists(bios_path))
{
error_exit(argv, "bios path invalid");
}
#if !NATIVE_CLI
if (!file_exists(font_path))
{
error_exit(argv, "font path invalid");
}
#endif
uint64_t ram_size_total = ram_size;
if (dtb_path != nullptr)
{
ram_size_total += SIZE_MIB(2);
}
virtio::VirtioBlkDevice* virtio_device = nullptr;
if (virt_drive_path)
{
if (!file_exists(virt_drive_path))
{
error_exit(argv, "virt_drive path invalid");
}
std::vector<uint8_t> virt_drive = helper::load_file(virt_drive_path);
virtio_device = new virtio::VirtioBlkDevice(std::move(virt_drive));
}
RamDevice dram = RamDevice(DRAM_BASE, ram_size_total, helper::load_file(bios_path));
gpu::GpuDevice gpu = gpu::GpuDevice("RISC V emulator", font_path, 960, 540);
SysconDevice syscon = SysconDevice();
Cpu cpu = Cpu(&dram, &gpu, virtio_device, &syscon);
if (dtb_path)
{
if (!file_exists(dtb_path))
{
error_exit(argv, "dtb path invalid");
}
std::vector<uint8_t> dtb = helper::load_file(dtb_path);
uint64_t dtb_offset = dram.data.size() - SIZE_MIB(2);
cpu.regs[Cpu::reg_abi_name::a1] = dram.get_base_address() + dtb_offset;
if (!patch_dtb_ram_size(dtb, ram_size))
{
std::cout << "Warning: couldn't find dtb memory size magic value "
"(0x0badc0de), make sure that the memory the emulator allocates is equal "
"or greater than one specified in the dtb\n";
}
memcpy(dram.data.data() + dtb_offset, dtb.data(), dtb.size());
}
if (kernel_path)
{
if (!file_exists(kernel_path))
{
error_exit(argv, "kernel path invalid");
}
std::vector<uint8_t> kernel = helper::load_file(kernel_path);
memcpy(dram.data.data() + KERNEL_OFFSET, kernel.data(), kernel.size());
}
cpu.run();
}