-
Notifications
You must be signed in to change notification settings - Fork 2
/
PICoBoot_Utility.cpp
116 lines (92 loc) · 3.29 KB
/
PICoBoot_Utility.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
/*
This file is part of PICoBoot Utility.
Copyright (C) 2021 ReimuNotMoe <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "PICoBoot_Utility.hpp"
uint8_t crc8(const uint8_t *data, size_t len) {
uint8_t crc = 0xff;
size_t i, j;
for (i = 0; i < len; i++) {
crc ^= data[i];
for (j = 0; j < 8; j++) {
if ((crc & 0x80) != 0)
crc = (uint8_t)((crc << 1) ^ 0x31);
else
crc <<= 1;
}
}
return crc;
}
void ShowHelpExtra() {
puts(
"\n"
" Commands:\n"
" devices List devices in bootloader\n"
" info Show device info\n"
" reboot [bootloader] Reboot device\n"
" flash <FILE.hex> Flash from image\n"
" readback <FILE.hex> Readback to image\n"
" erase <app|all> Erase device\n"
" env <show|get|set|save> Manipulate bootloader environment\n"
);
}
std::string global_device_path;
int main(int argc, char **argv) {
cxxopts::Options options("picoboot", "picoboot - Utility for the PICoBoot bootloader.");
std::vector<std::string> positional_args;
options.add_options("Main")
("h,help", "Show this help")
("d,device", "Manually specify device path", cxxopts::value<std::string>(global_device_path)->default_value(""))
;
options.add_options("positional")
("positional", "Positional parameters: The file to write/read", cxxopts::value<std::vector<std::string>>(positional_args))
;
options.parse_positional("positional");
options.positional_help("COMMAND...").show_positional_help();
try {
auto cmd = options.parse(argc, argv);
if (cmd.count("help")) {
std::cout << options.help({"Main"});
ShowHelpExtra();
return 0;
}
if (positional_args.empty()) {
std::cout << options.help({"Main"});
ShowHelpExtra();
return 0;
}
} catch (std::exception &e) {
std::cout << "Error: " << e.what() << "\n";
std::cout << options.help({"Main"});
ShowHelpExtra();
return 1;
}
if (geteuid()) {
printf("Warning: You may encounter problems without root permissions\n");
}
std::unordered_map<std::string, std::function<int(const std::vector<std::string>&)>> command_map = {
{"devices", [](auto &arg){return Command_Devices(arg);}},
{"info", [](auto &arg){return Command_Info(arg);}},
{"reboot", [](auto &arg){return Command_Reboot(arg);}},
{"flash", [](auto &arg){return Command_Flash(arg);}},
{"erase", [](auto &arg){return Command_Erase(arg);}},
{"env", [](auto &arg){return Command_Env(arg);}},
};
auto it = command_map.find(positional_args[0]);
if (it != command_map.end()) {
return it->second(positional_args);
} else {
printf("error: no such command: %s, read the help!\n", positional_args[0].c_str());
}
return 2;
}