forked from Trel725/chavrprog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
255 lines (218 loc) · 5.69 KB
/
main.c
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
/*
* This file is part of the chavrprog project.
*
* Copyright (C) 2016 Andrii Trelin ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "ch341a.h"
#include "cintelhex.h"
#include <unistd.h>
#include "chavrprog.h"
#include "config.h"
int file_exists(const char * filename){
FILE *file= fopen(filename, "r");
if (file!=0){
fclose(file);
return 1;
}
return 0;
}
int delay = 4500; //default value of writing delay
int main(int argc, char * argv[]){
if(argc == 1) {
print_help();
exit(0);
}
int opts;
while ( (opts = getopt(argc,argv,"d:r:w:ea:c:fl:H:x:t:Lp:")) != -1){
switch (opts){
case 'd':
for(int i=0; i<(sizeof confset / sizeof confset[0]); i++){
if(strcmp(optarg, confset[i].name)==0){
assign_cfg(i);
break;
}
if(i==((sizeof confset / sizeof confset[0])-1)){
printf("No such chip\n");
printf("Available devices:\n");
for(int i=0; i<CONF_LENGTH; i++){
printf("%s\n", confset[i].name);
}
exit(0);
}
}
break;
case 'r':
if(optarg[0]=='f'){
if(argv[optind]!=NULL) {
ch_init();
printf("Reading to %s...\n", argv[optind]);
read_flash(TOT_MEM*2);
FILE *fp;
fp = fopen(argv[optind], "w");
fwrite(data_buffer, 1, (TOT_MEM*2), fp);
fclose(fp);
}
else
printf("Please, specify file\n");
}
else {
ch_init();
read_flash(atoi(optarg));
for(int i=0; i<atoi(optarg); i++){
printf("%x ", data_buffer[i]);
}
}
break;
case 'w':
if(file_exists(optarg)){
ch_init();
printf("Write device using %s\n",optarg);
main_write_stream(optarg);
}
else printf("No such file\n");
break;
case 'e':
ch_init();
printf("Erasing chip\n");
chip_erace();
break;
case 'c':
if(file_exists(optarg)){
printf("Checking...\n");
ch_init();
check_flash_strict(optarg);
}
else printf("No such file\n");
break;
case 't':
delay = (int)strtol(optarg, NULL, 0);
debug_call();
case 'a':
if(argv[optind]==NULL) {
if(file_exists(optarg)){
printf("%s", argv[optind]);
printf("Erasing...\n");
ch_init();
chip_erace();
printf("Writing...\n");
main_write_stream(optarg);
printf("Checking...\n");
check_flash(optarg);
}
else printf("No such file\n");
}
else if(*optarg=='s'){
if(file_exists(argv[optind])){
printf("Erasing...\n");
ch_init();
chip_erace();
printf("Writing...\n");
main_write_stream(argv[optind]);
printf("Checking strictly...\n");
check_flash_strict(argv[optind]);
}
else printf("No such file\n");
}
break;
case 'f':
ch_init();
printf("Fuses: low high ext\n");
read_fuses();
printf("\n");
break;
int fuse_val = -1;
case 'l':
ch_init();
fuse_val = (int)strtol(optarg, NULL, 0);
if ((fuse_val >= 0) && (fuse_val < 256)){
write_fuses(fuse_val,0);
}
else{
printf("Wrong fuse value, refusing to write...\n");
}
break;
case 'H':
ch_init();
fuse_val = (int)strtol(optarg, NULL, 0);
if ((fuse_val >= 0) && (fuse_val < 256)){
write_fuses(fuse_val,1);
}
else{
printf("Wrong fuse value, refusing to write...\n");
}
break;
case 'x':
ch_init();
fuse_val = (int)strtol(optarg, NULL, 0);
if ((fuse_val >= 0) && (fuse_val < 256)){
write_fuses(fuse_val,2);
}
else{
printf("Wrong fuse value, refusing to write...\n");
}
break;
case 'L':
ch_init();
read_locks();
break;
case 'p':
if(optarg[0]=='r'){
if(argv[optind]!=NULL) {
ch_init();
read_eeprom();
printf("Reading to %s...\n", argv[optind]);
FILE *fp;
fp = fopen(argv[optind], "w");
fwrite(data_buffer, 1, cfg_eeprom, fp);
fclose(fp);
}
else{
ch_init();
read_eeprom();
for (int i=0; i<cfg_eeprom; i++){
printf("%x ", data_buffer[i]);
}
}
}
else if(optarg[0]=='w' && argv[optind]!=NULL){
printf("Writing eeprom using %s\n", argv[optind]);
write_eeprom(argv[optind]);
}
else{
print_help();
}
break;
case 'v':
if(file_exists(optarg)){
ch_init();
printf("Write eeprom using %s\n",optarg);
write_eeprom(optarg);
}
else printf("No such file\n");
break;
default:
print_help();
exit(0);
};
}
ch_exit();
}