-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·117 lines (98 loc) · 2.68 KB
/
main.cpp
File metadata and controls
executable file
·117 lines (98 loc) · 2.68 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
#include "EDBInterface.h"
#include <cstring>
#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <vector>
using namespace std;
vector<flashImg> imglist;
EDBInterface edb;
void showUsage() {
cout << "Note: Serial mode is deprecated by the King and will NOT be implemented." << endl;
cout << "Usage:" << endl;
cout << "\t-f <bin file> <page> [b] (Specify 'b' to flash as boot image.)" << endl;
cout << "\t-r Reboot if all operations are done." << endl;
cout << "\t-m Enter Mass Storage mode." << endl;
}
void handleInterrupt(int id) {
printf("\nInterrupted\n");
edb.close();
exit(-1);
}
int main(int argc, char *argv[]) {
struct sigaction sigHandler;
sigHandler.sa_handler = handleInterrupt;
sigaction(SIGINT, &sigHandler, NULL);
bool reboot = false;
bool mscmode = false;
if (argc < 2) {
showUsage();
return -1;
}
// if (geteuid() != 0) {
// cout << "Please run with root privileges!" << endl;
// return -1;
// }
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-f") == 0) {
if (i + 2 > argc) {
showUsage();
return -1;
}
flashImg item;
memset(&item, 0, sizeof(item));
// printf("Open: %s\n", argv[i + 1]);
item.f = fopen(argv[i + 1], "rb");
if (!item.f) {
printf("Open: %s Failed.\n", argv[i + 1]);
return -1;
}
item.filename = argv[i + 1];
item.toPage = atoi(argv[i + 2]);
if (i + 3 < argc) {
if (strcmp(argv[i + 3], "b") == 0) {
printf("Set as boot img.\n");
item.bootImg = true;
i++;
}
}
printf("Flash to page: %d\n", item.toPage);
imglist.push_back(item);
i += 2;
}
if (strcmp(argv[i], "-r") == 0) {
reboot = true;
}
if (strcmp(argv[i], "-m") == 0) {
mscmode = true;
}
}
if (edb.open()) {
edb.close();
return -1;
}
if (edb.ping() == false) {
cout << "Device not responding." << endl;
edb.close();
return 10;
}
if (mscmode) {
edb.vm_suspend();
edb.mscmode();
}
edb.vm_suspend();
for (flashImg &item : imglist) {
edb.flash(item);
int ret = fclose(item.f);
if (ret) {
printf("fclose(item.f) failed with code %d\n", ret);
}
}
// edb.vm_reset();
// edb.vm_resume();
if (reboot) {
edb.reboot();
}
edb.close();
return 0;
}