-
Notifications
You must be signed in to change notification settings - Fork 0
/
libscsi.c
306 lines (265 loc) · 6.2 KB
/
libscsi.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#include <unistd.h>
#include <linux/fs.h> /* for BLKGETSIZE */
#include "libscsi.h"
//lock/unlock feature implementation
int do_lock_unlock(char *device, int cmd42,char *pwd_arg)
{
int fd, ret = 0;
int cmd42_para; //parameter of cmd42
char pwd[MAX_PWD_LENGTH+1]; //password
int *status;
fd = open(device, O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
// if(set_cmd_para(&cmd42_para, argv[2]) < 0)
// {
// close(fd);
// exit(1);
// }
cmd42_para = cmd42;
if(cmd42_para==CMD42_ERASE)
{
char *warnInfo = "Warning: all card data will be erased together with PWD";
if(ask_yes_or_no(warnInfo) == 0)
{
close(fd);
exit(1);
}
status = read_status(&fd);
printf("lock/unlock: %d, result: %d\n", status[0], status[1]);
if(status[0]==0)
{
show_cmd42_error_msg(cmd42_para, status[0]);
close(fd);
exit(1);
}
}
else
{
strcpy(pwd, pwd_arg);
}
ret = set_cmd42(cmd42_para, pwd, &fd);
close(fd);
return ret;
}
int set_cmd_para(int *cmd_para, char * arg)
{
if (!strcmp("s", arg)) {
*cmd_para = CMD42_SET_PWD;
printf("Set password...\n");
}
else if (!strcmp("c", arg)) {
*cmd_para = CMD42_CLR_PWD;
printf("Clear password...\n");
}
else if (!strcmp("l", arg)) {
*cmd_para = CMD42_LOCK;
printf("Lock the card...\n");
}
else if (!strcmp("sl", arg)) {
*cmd_para = CMD42_SET_LOCK;
printf("Set password and lock the card ...\n");
}
else if (!strcmp("u", arg)) {
*cmd_para = CMD42_UNLOCK;
printf("Unlock the card ...\n");
}
else if (!strcmp("e", arg)) {
*cmd_para = CMD42_ERASE;
printf("Force erase ...\n");
}
else {
printf("Invalid parameter:\n" "s\tset password\n" "c\tclear password\n" "l\tlock\n"
"sl\tset password and lock\n" "u\tunlock\n" "e\tforce erase\n");
return -1;
}
return 1;
}
void show_cmd42_error_msg(int cmd_para, int lock_status)
{
printf("Warning: ");
if (cmd_para == CMD42_SET_PWD) {
printf("Already set password, try to use 'PasswordNewPassword' format to set new password\n");
}
else if (cmd_para == CMD42_CLR_PWD) {
printf("Wrong password or already no set password\n");
}
else if (cmd_para == CMD42_LOCK) {
if(lock_status==1)
printf("Already lock\n");
else
printf("Wrong password or no password set yet\n");
}
else if (cmd_para == CMD42_SET_LOCK) {
printf("Set password and lock the card fail\n");
}
else if (cmd_para == CMD42_UNLOCK) {
if(lock_status==1)
printf("Wrong password\n");
else
printf("Already unlock\n");
}
else if (cmd_para == CMD42_ERASE) {
printf("Need to lock the card first\n");
}
}
void disk_format(char *device)
{
char cmd[30];
sprintf(cmd, "sudo umount %s", device);
if(system(cmd) == -1)
{
printf("Error to unmount\n");
return ;
}
sprintf(cmd, "sudo mkfs.vfat %s", device);
if(system(cmd) == -1)
printf("Error to format\n");
}
int do_read_status(int nargs, char **argv)
{
int fd, ret = 0;
int *curr_status;
char *device;
device = argv[nargs-1];
fd = open(device, O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
curr_status = read_status(&fd);
ret = curr_status[0];
printf("lock/unlock: %d, result: %d\n", curr_status[0], curr_status[1]);
if(ret)
printf("Device is lock\n");
else
printf("Device is unlock\n");
close(fd);
return ret;
}
int * read_status(int *fd)
{
int *ret = malloc(2);
__u8 data_block[16]={0};
unsigned char sense_b[16]={0};
unsigned char CmdBlk16[16] = RDF5CMD13;
sg_io_hdr_t io_hdr;
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = sizeof(CmdBlk16);
io_hdr.cmdp = CmdBlk16;
io_hdr.mx_sb_len = sizeof(sense_b);
io_hdr.sbp = sense_b;
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
io_hdr.dxfer_len = sizeof(data_block);
io_hdr.dxferp = data_block;
io_hdr.timeout = 20000;
if(ioctl(*fd, SG_IO, &io_hdr) < 0)
{
printf("ioctl fail\n");
ret[0] = -1;
return ret;
}
if(data_block[1] & 0x02 )
ret[0] = 1;
else
ret[0] = 0;
if(data_block[1] & 0x01 )
ret[1] = 1;
else
ret[1] = 0;
return ret;
}
int send_status(int fd, __u32 *response)
{
int ret = 0;
struct mmc_ioc_cmd idata;
memset(&idata, 0, sizeof(idata));
idata.opcode = MMC_SEND_STATUS;
idata.arg = (1 << 16);
idata.flags = MMC_RSP_R1 | MMC_CMD_AC;
ret = ioctl(fd, MMC_IOC_CMD, &idata);
if (ret)
{
printf("ioctl ret is %d\n", ret);
perror("ioctl");
}
*response = idata.response[0];
for(int i=0 ; i<4 ;i++)
printf("%d\n", idata.response[i]);
return ret;
}
int set_cmd42(int cmd_para, char *pwd, int *fd)
{
int ret=0;
__u8 data_block[DATA_BLOCK_SIZE]={0};
__u8 data_block_onebyte[1]={0};
int block_size = 0;
int pwd_len = strlen(pwd);
if (cmd_para==CMD42_ERASE)
block_size = 2; //set blk size to 2-byte for Force Erase @DDR50 compability
else
block_size = DATA_BLOCK_SIZE;
// printf("Set to data block length = %d byte(s).\n", block_size);
if (cmd_para==CMD42_ERASE) {
data_block_onebyte[0] = cmd_para;
}
else {
data_block[0] = cmd_para;
data_block[1] = pwd_len;
memcpy((char *)(data_block+2), pwd, pwd_len);
}
unsigned char sense_b[32];
unsigned char CmdBlk16[16] = RDF5CMD42;
sg_io_hdr_t io_hdr;
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = sizeof(CmdBlk16);
io_hdr.cmdp = CmdBlk16;
io_hdr.mx_sb_len = sizeof(sense_b);
io_hdr.sbp = sense_b;
io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
io_hdr.dxfer_len = block_size;
if(cmd_para==CMD42_ERASE)
io_hdr.dxferp = data_block_onebyte;
else
io_hdr.dxferp = data_block;
io_hdr.timeout = 20000;
if(ioctl(*fd, SG_IO, &io_hdr) < 0)
{
printf("ioctl fail\n");
ret = -1;
}
else
{
ret = 1;
}
return ret;
}
int ask_yes_or_no(char * warnInfo)
{
printf("%s\n",warnInfo);
printf("Do you want to continue? [Y/n] ");
char answer[5];
int c;
while (1)
{
scanf("%3s", answer);
if(answer[0] == 'Y' || answer[0] == 'y')
return 1;
else if(answer[0] == 'N' || answer[0] == 'n')
return 0;
else
printf("Please press [Y/y] to continue or [N/n] to cancel :");
while((c = fgetc(stdin)) != '\n' && c != EOF);//Flush stdin
}
}