Skip to content

Commit 02fb8ef

Browse files
committed
vmbuild: use struct for bootvars + cleanups
1 parent 5f3f6a8 commit 02fb8ef

1 file changed

Lines changed: 34 additions & 30 deletions

File tree

vmbuild/vmbuild.cpp

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,40 @@ bool verb = false;
4444
#define ERROR(X,...) fprintf(stderr, "[ vmbuild ] Error: " X "\n", ##__VA_ARGS__); std::terminate()
4545

4646

47-
using namespace std;
48-
49-
// Location of special variables inside the bootloader
50-
static const int bootvar_size = 4;
51-
static const int bootvar_entry = bootvar_size + 4;
52-
static const int bootvar_load_addr = bootvar_entry + 4;
47+
// Special variables inside the bootloader
48+
struct bootvars {
49+
const uint32_t __first_instruction;
50+
uint32_t size;
51+
uint32_t entry;
52+
uint32_t load_addr;
53+
};
5354

5455
static bool test {false};
5556

56-
static const string info {"Create a bootable disk image for IncludeOS.\n"};
57-
static const string usage {"Usage: vmbuild <service_binary> [<bootloader>][-test]\n"};
57+
const std::string info {"Create a bootable disk image for IncludeOS.\n"};
58+
const std::string usage {"Usage: vmbuild <service_binary> [<bootloader>][-test]\n"};
59+
60+
std::string includeos_install = "/usr/local/includeos/";
5861

5962
class Vmbuild_error : public std::runtime_error {
6063
using runtime_error::runtime_error;
6164
};
6265

63-
string get_bootloader_path(int argc, char** argv) {
66+
std::string get_bootloader_path(int argc, char** argv) {
6467

6568
if (argc == 2) {
6669
// Determine IncludeOS install location from environment, or set to default
67-
std::string includeos_install;
6870
std::string arch = "x86_64";
6971
auto env_arch = getenv("ARCH");
7072

7173
if (env_arch)
7274
arch = std::string(env_arch);
7375

74-
if (auto env_install = getenv("INCLUDEOS_PREFIX")) {
75-
includeos_install = std::string{env_install} + "/includeos/" + arch;
76-
} else {
77-
includeos_install = std::string{getenv("HOME")} + "/IncludeOS_install";
78-
}
76+
std::string includeos_install = "/usr/local/includeos/" + arch;
77+
78+
if (auto env_prefix = getenv("INCLUDEOS_PREFIX"))
79+
includeos_install = std::string{env_prefix} + "/includeos/" + arch;
80+
7981
return includeos_install + "/boot/bootloader";
8082
} else {
8183
return argv[2];
@@ -86,40 +88,39 @@ int main(int argc, char** argv)
8688
{
8789
// Verify proper command usage
8890
if (argc < 2) {
89-
cout << info << usage;
91+
std::cout << info << usage;
9092
exit(EXIT_FAILURE);
9193
}
9294

93-
// VERBOSE=...
95+
// Set verbose from environment
9496
const char* env_verb = getenv("VERBOSE");
9597
if (env_verb && strlen(env_verb) > 0)
9698
verb = true;
9799

98-
const string bootloader_path = get_bootloader_path(argc, argv);
100+
const std::string bootloader_path = get_bootloader_path(argc, argv);
99101

100102
if (argc > 2)
101-
const string bootloader_path {argv[2]};
103+
const std::string bootloader_path {argv[2]};
102104

103105
INFO("Using bootloader %s" , bootloader_path.c_str());
104106

105-
const string elf_binary_path {argv[1]};
106-
const string img_name {elf_binary_path.substr(elf_binary_path.find_last_of("/") + 1, string::npos) + ".img"};
107+
const std::string elf_binary_path {argv[1]};
108+
const std::string img_name {elf_binary_path.substr(elf_binary_path.find_last_of("/") + 1, std::string::npos) + ".img"};
107109

108110
INFO("Creating image '%s'" , img_name.c_str());
109111

110112
if (argc > 3) {
111-
if (string{argv[3]} == "-test") {
113+
if (std::string{argv[3]} == "-test") {
112114
test = true;
113115
verb = true;
114-
} else if (string{argv[3]} == "-v"){
116+
} else if (std::string{argv[3]} == "-v"){
115117
verb = true;
116118
}
117119
}
118120

119121
struct stat stat_boot;
120122
struct stat stat_binary;
121123

122-
// Validate boot loader
123124
// Validate boot loader
124125
if (stat(bootloader_path.c_str(), &stat_boot) == -1) {
125126
INFO("Could not open %s, exiting\n" , bootloader_path.c_str());
@@ -131,6 +132,7 @@ int main(int argc, char** argv)
131132
stat_boot.st_size, SECT_SIZE);
132133
return SECT_SIZE_ERR;
133134
}
135+
134136
INFO("Size of bootloader: %ld\t" , stat_boot.st_size);
135137

136138
// Validate service binary location
@@ -156,16 +158,16 @@ int main(int argc, char** argv)
156158
INFO("Creating disk of size %ld sectors / %ld bytes" ,
157159
(disk_size / SECT_SIZE), disk_size);
158160

159-
vector<char> disk (disk_size);
161+
std::vector<char> disk (disk_size);
160162

161163
auto* disk_head = disk.data();
162164

163-
ifstream file_boot {bootloader_path}; //< Load the boot loader into memory
165+
std::ifstream file_boot {bootloader_path}; //< Load the boot loader into memory
164166

165167
auto read_bytes = file_boot.read(disk_head, stat_boot.st_size).gcount();
166168
INFO("Read %ld bytes from boot image", read_bytes);
167169

168-
ifstream file_binary {elf_binary_path}; //< Load the service into memory
170+
std::ifstream file_binary {elf_binary_path}; //< Load the service into memory
169171

170172
auto* binary_imgloc = disk_head + SECT_SIZE; //< Location of service code within the image
171173

@@ -263,9 +265,11 @@ int main(int argc, char** argv)
263265
srv_load_addr -= binary_load_offs;
264266

265267
// Write binary size and entry point to the bootloader
266-
*(reinterpret_cast<uint32_t*>(disk_head + bootvar_size)) = srv_size;
267-
*(reinterpret_cast<uint32_t*>(disk_head + bootvar_entry)) = srv_entry;
268-
*(reinterpret_cast<uint32_t*>(disk_head + bootvar_load_addr)) = srv_load_addr;
268+
bootvars* boot = reinterpret_cast<bootvars*>(disk_head);
269+
270+
boot->size = srv_size;
271+
boot->entry = srv_entry;
272+
boot->load_addr = srv_load_addr;
269273

270274
INFO("srv_size: %i", srv_size);
271275
INFO("srv_entry: 0x%x", srv_entry);

0 commit comments

Comments
 (0)