-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcmdline.cpp
More file actions
334 lines (280 loc) · 8.76 KB
/
cmdline.cpp
File metadata and controls
334 lines (280 loc) · 8.76 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include "archive.h"
#include "disk.h"
#include "cmdparser.h"
using namespace std;
void help();
void copy_device(FILE* in, FILE* out, long long kbytes, bool verbose);
int main(int argc, char* argv[]) {
if(argc == 1) {
help();
exit(0);
}
CommandParser* p = new CommandParser(argc, argv);
bool verbose = p->hasFlag("verbose", "v");
// get action
string action(argv[1]);
if(action == "install" || action == "i") {
cout << "Install" << endl;
string dest, bootl, kernel, rootfs;
bool repart = false;
int bsize;
// get parameter
dest = p->getParameter("device", "d");
bootl = p->getParameter("bootloader", "b");
kernel = p->getParameter("kernel", "k");
rootfs = p->getParameter("rootfs", "r");
bsize = atoi(p->getParameter("bsize", "s").c_str());
if(bsize == 0) {
bsize = 8;
}
if(bsize < 4) {
bsize = 4;
}
if(p->hasFlag("repartition", "p")) {
repart = true;
}
// no destination given
if(dest.empty()) {
cout << "Error: no destination given!" << endl;
exit(1);
}
// nothing to do
if(bootl.empty() && kernel.empty() && rootfs.empty() && !repart) {
cout << "Nothing to do. Exiting." << endl;
exit(0);
}
// verbose output details
if(verbose) {
cout << "Device: \t" << dest << endl;
if(!bootl.empty()) {
cout << "Bootloader: \t" << bootl << endl;
}
if(!kernel.empty()) {
cout << "Kernel: \t" << kernel << endl;
}
if(!rootfs.empty()) {
cout << "RootFS: \t" << rootfs << endl;
}
cout << "Bootloader size: " << bsize << "MB" << endl;
cout << "Repartition: " << (repart ? "Yes" : "No") << endl;
}
// unmounting partitions
int c;
for(c = 0; c < 2; c++) {
string part = dest + (char)(c + 1 + '0');
if(is_mounted(part.c_str())) {
if(verbose) {
cout << "Unmounting '" << part << "'" << endl;
}
unmount_partition(get_mountpoint(part.c_str()));
if(is_mounted(part.c_str())) {
cout << "Error: can not unmount '" << part << "'" << endl;
exit(1);
}
}
}
// repartition
if(repart) {
if(verbose) {
cout << "Creating partitions (bootloader partition: " << bsize << "MB)" << endl;
}
create_partitions(dest.c_str(), bsize * 1024 * 1024);
}
// write bootloader
if(!bootl.empty()) {
string boot_part = dest + "2";
if(verbose) {
cout << "Writing bootloader to partition '" << boot_part << "'" << endl;
}
dd(bootl.c_str(), boot_part.c_str(), 512);
}
// mounting partition
if(verbose) {
cout << "Mounting partition" << endl;
}
mkdir("linux", 0777);
string linux_part = dest + "1";
mount_partition(linux_part.c_str(), "linux");
if(!is_mounted(linux_part.c_str())) {
cout << "Error: can not mount partition." << endl;
exit(1);
}
// extract rootfs
if(!rootfs.empty()) {
if(verbose) {
cout << "Extracting RootFS" << endl;
}
extract_archive(rootfs.c_str(), "linux");
}
// copy kernel
if(!kernel.empty()) {
if(verbose) {
cout << "Copying kernel" << endl;
}
copy_file(kernel.c_str(), "linux/zImage");
}
// unmounting
if(verbose) {
cout << "Unmounting partition" << endl;
}
unmount_partition("linux");
//syncing
if(verbose) {
cout << "Syncing card" << endl;
}
sync_card();
cout << "Installation successful!" << endl;
} else if(action == "backup" || action == "b") {
cout << "Backup" << endl;
string device, file;
bool verbose = p->hasFlag("verbose", "v");
device = p->getParameter("device", "d");
file = p->getParameter("out", "o");
// check if output file and device are given
if(device.empty() || file.empty()) {
cout << "Error: device and output file must be given!" << endl;
exit(1);
}
long dev_size = get_device_size(device.c_str()) / 1024;
if(dev_size == 0) {
cout << "Error: device size is 0" << endl;
exit(1);
}
FILE* in = fopen(device.c_str(), "rb");
if(in == NULL) {
cout << "Error: can not open device" << endl;
exit(1);
}
FILE* out = fopen(file.c_str(), "wb");
if(out == NULL) {
fclose(in);
cout << "Error: can not open file '" << file << "'" << endl;
exit(1);
}
if(verbose) {
cout << dev_size << " kb to copy" << endl;
}
if(verbose) {
cout << "Creating backup" << endl;
}
copy_device(in, out, dev_size, verbose);
cout << "Creating backup was successful" << endl;
exit(0);
} else if(action == "restore" || action == "r") {
cout << "Restore" << endl;
string device, file;
bool verbose = p->hasFlag("verbose", "v");
device = p->getParameter("device", "d");
file = p->getParameter("in", "i");
// check if output file and device are given
if(device.empty() || file.empty()) {
cout << "Error: device and input file must be given!" << endl;
exit(1);
}
// check if mounted
int c;
for(c = 0; c < 2; c++) {
string part = device + (char)(c + 1 + '0');
if(is_mounted(part.c_str())) {
if(verbose) {
cout << "Unmounting '" << part << "'" << endl;
}
unmount_partition(get_mountpoint(part.c_str()));
if(is_mounted(part.c_str())) {
cout << "Error: can not unmount '" << part << "'" << endl;
exit(1);
}
}
}
long dev_size = get_device_size(device.c_str()) / 1024;
if(dev_size == 0) {
cout << "Error: device size is 0" << endl;
exit(1);
}
FILE* out = fopen(device.c_str(), "wb");
if(out == NULL) {
cout << "Error: can not open device" << endl;
exit(1);
}
FILE* in = fopen(file.c_str(), "rb");
if(in == NULL) {
fclose(out);
cout << "Error: can not open file '" << file << "'" << endl;
exit(1);
}
if(verbose) {
cout << dev_size << " kb to copy" << endl;
}
if(verbose) {
cout << "Restoring backup" << endl;
}
copy_device(in, out, dev_size, verbose);
cout << "Restoring backup was successful" << endl;
exit(0);
} else if(action == "version" || action == "v") {
cout << VERSION << endl;
} else {
help();
exit(0);
}
}
void copy_device(FILE* in, FILE* out, long long kbytes, bool verbose) {
int blocksize = 1024 * 1024;
char* buffer = (char*)malloc(blocksize);
unsigned long total = 0;
long long dev_size = kbytes;
//long start_time = time(NULL);
int last_ten = 0;
while(!feof(in)) {
fread(buffer, blocksize, 1, in);
fwrite(buffer, blocksize, 1, out);
total++;
// calculate remaining time
//long time_delta = time(NULL) - start_time;
int percent = (int)(100.f * total * (blocksize / 1024.f)) / dev_size;
if(percent != 0) {
//long total_time = (100 * time_delta / percent);
//long remaining_time = (time_delta / percent) * (100 - percent);
if(verbose) {
if((int)(percent / 2) != last_ten) {
last_ten = (int)(percent / 2);
cout << "." << flush;
}
}
}
}
free(buffer);
cout << endl;
}
void help() {
cout << endl;
cout << "GNUBLIN Installer " << VERSION << " Commandline \t\t Copyright (c) 2012 Michael Schwarz" << endl << endl;
cout << "Usage: \t gnublin-cmdline <action> <parameter 1> <parameter N>" << endl << endl;
cout << "<Action>" << endl;
cout << " install \tInstall GNUBLIN system" << endl;
cout << " backup \tBackup existing system to file" << endl;
cout << " restore \tRestore system from backup file" << endl;
cout << " version \tShows version of program" << endl << endl;
cout << "<Parameter> " << endl;
//cout << "--help -h \tShow this help" << endl;
cout << " -d \t--device \tSelect device (e.g. /dev/sda)" << endl;
cout << " -p \t--repartition \tCreate new partition table" << endl;
cout << " -b \t--bootloader \tSelect bootloader" << endl;
cout << " -k \t--kernel \tSelect kernel" << endl;
cout << " -r \t--rootfs \tSelect RootFS" << endl;
cout << " -s \t--bsize \tSelect Bootloader size (in MB)" << endl;
cout << " -i \t--in \t\tSelect file to restore from" << endl;
cout << " -o \t--out \t\tSelect file to backup to" << endl;
cout << " -v \t--verbose \tVerbose output" << endl;
cout << endl;
cout << "Examples:" << endl;
cout << "Full-Install, repartition device with 4MB partition for bootloader" << endl;
cout << "\tgnublin-cmdline install -d /dev/sdj -p -b apex.bin -k zImage -r rootfs.tar.gz -s 4" << endl;
cout << "Backup existing installation" << endl;
cout << "\tgnublin-cmdline backup --device /dev/sdj --out backup.img" << endl;
}