forked from forth32/qtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qefs.c
202 lines (163 loc) · 5.02 KB
/
qefs.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
#include "include.h"
//%%%%%%%%% Общие переменные %%%%%%%%%%%%%%%%55
unsigned int altflag=0; // флаг альтернативной EFS
unsigned int fixname=0; // индикатор явного уазания имени файла
char filename[50]; // имя выходного файла
//****************************************************
//* Чтение дампа EFS (efs.mbn)
//****************************************************
void back_efs() {
unsigned char iobuf[14048];
unsigned char cmd_efsh1[16]= {0x4B, 0x13,0x19, 0};
unsigned char cmd_efsopen[16]= {0x4B, 0x13,0x16, 0};
unsigned char cmd_efsdata[16]={0x4B, 0x13,0x17,0,0,0,0,0,0,0,0,0};
unsigned char cmd_efsclose[16]= {0x4B, 0x13,0x18, 0};
int iolen;
FILE* out;
// настройка на альтернативную EFS
if (altflag) {
cmd_efsh1[1]=0x3e;
cmd_efsopen[1]=0x3e;
cmd_efsdata[1]=0x3e;
cmd_efsclose[1]=0x3e;
if (!fixname) strcpy(filename,"efs_alt.mbn");
}
// настройка на стандартную EFS
else strcpy(filename,"efs.mbn");
out=fopen(filename,"w");
iolen=send_cmd_base(cmd_efsh1, 4, iobuf, 0);
if ((iolen != 11) || test_zero(iobuf+3,5)) {
printf("\n Неправильный ответ на команду 19\n");
dump(iobuf,iolen,0);
fclose(out);
return;
}
iolen=send_cmd_base(cmd_efsopen, 4, iobuf, 0);
if ((iolen != 11) || test_zero(iobuf+3,5)) {
printf("\n Неправильный ответ на команду открытия (16)\n");
dump(iobuf,iolen,0);
fclose(out);
return;
}
printf("\n");
// главный цикл получения efs.mbn
while(1) {
iolen=send_cmd_base(cmd_efsdata, 12, iobuf, 0);
if (iolen != 532) {
printf("\n Неправильный ответ на команду 4b 13 17\n");
dump(iobuf,iolen,0);
return;
}
printf("\r Читаем раздел: %i блок: %08x",*((unsigned int*)&iobuf[8]),*((unsigned int*)&iobuf[12]));
fflush(stdout);
if (iobuf[8] == 0) break;
fwrite(iobuf+16,512,1,out);
memcpy(cmd_efsdata+4,iobuf+8,8);
}
// закрываем EFS
iolen=send_cmd_base(cmd_efsclose,4,iobuf,0);
if ((iolen != 11) || test_zero(iobuf+3,5)) {
printf("\n Неправильный ответ на команду закрытия (18)\n");
dump(iobuf,iolen,0);
fclose(out);
}
}
//@@@@@@@@@@@@ Головная программа
void main(int argc, char* argv[]) {
unsigned int opt;
enum{
MODE_BACK_EFS,
};
int mode=-1;
#ifndef WIN32
char devname[50]="/dev/ttyUSB0";
#else
char devname[50]="";
#endif
while ((opt = getopt(argc, argv, "hp:o:ab:r:")) != -1) {
switch (opt) {
case 'h':
printf("\n Утилита предназначена для работы с разделом efs \n\
%s [ключи] [параметр или имя файла]\n\
Допустимы следующие ключи:\n\n\
`Ключи, определяюще выполняемую операцию:\n\
-be - дамп efs\n\
\n\
Ключи-модификаторы:\n\
-p <tty> - указывает имя устройства диагностического порта модема\n\
-a - использовать альтернативную EFS\n\
-o <file> - имя файла для сохранения efs\n\
\n",argv[0]);
return;
case 'o':
strcpy(filename,optarg);
fixname=1;
break;
// === группа ключей backup ==
case 'b':
if (mode != -1) {
printf("\n В командной строке задано более 1 ключа режима работы");
return;
}
switch(*optarg) {
case 'e':
mode=MODE_BACK_EFS;
break;
default:
printf("\n Неправильно задано значение ключа -b\n");
return;
}
break;
// === группа ключей read ==
case 'r':
if (mode != -1) {
printf("\n В командной строке задано более 1 ключа режима работы");
return;
}
switch(*optarg) {
default:
printf("\n Неправильно задано значение ключа -r\n");
return;
}
break;
case 'p':
strcpy(devname,optarg);
break;
case 'a':
altflag=1;
break;
case '?':
case ':':
return;
}
}
if (mode == -1) {
printf("\n Не указан ключ выполняемой операции\n");
return;
}
#ifdef WIN32
if (*devname == '\0')
{
printf("\n - Последовательный порт не задан\n");
return;
}
#endif
if (!open_port(devname)) {
#ifndef WIN32
printf("\n - Последовательный порт %s не открывается\n", devname);
#else
printf("\n - Последовательный порт COM%s не открывается\n", devname);
#endif
return;
}
//////////////////
switch (mode) {
case MODE_BACK_EFS:
back_efs();
break;
default:
printf("\n Не указан ключ выполняемой операции\n");
return;
}
printf("\n");
}