/
tool_main_back.c
354 lines (275 loc) · 7.71 KB
/
tool_main_back.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "tool.h"
int8_t *align_ptr, *buf;
int free_aligned()
{
// Free the parent not just he alligned address.
free(align_ptr);
}
uint8_t *malloc_aligned(uint32_t size, int align) {
align_ptr = malloc((size + align - 1) * sizeof(uint8_t));
uint32_t mask = (align - 1);
if (align_ptr == NULL) return NULL;
buf = (uint8_t *)(((uintptr_t)(align_ptr + align - 1)) & ~(uintptr_t)mask);
return buf;
}
void *alloc_aligned_buffer(uint32_t size) {
void *ptr = NULL;
if (size) {
// Alloc the buffer using the aligned version.
ptr = malloc_aligned(size, MALLOC_ALIGN);
}
return ptr;
}
void free_request(struct ata_ioc_request *rq)
{
free_aligned();
free(rq);
}
struct ata_ioc_request* build_basic_command(uint32_t lba, uint32_t cnt, uint8_t feat,uint8_t comm, uint32_t op, uint32_t size)
{
struct ata_ioc_request *rq = NULL;
rq = malloc(sizeof(struct ata_ioc_request));
memset(rq, 0, sizeof(*rq));
rq->timeout = 30;
rq->u.ata.lba = lba;
rq->u.ata.count = cnt;
rq->u.ata.feature = feat;
rq->u.ata.command = comm;
rq->count= size;
rq->flags = ATA_CMD_CONTROL;
// Dont allocate for the writes, we need bigger
rq->data = (uint8_t*)(void*)alloc_aligned_buffer(size);
if (rq->data) {
memset(rq->data, 0, size);
}
if (op == ATA_CMD_READ) {
rq->flags |= ATA_CMD_READ;
}
else if (op == ATA_CMD_WRITE) {
rq->flags |= ATA_CMD_WRITE;
}
return rq;
}
int validate_card(uint8_t *data)
{
int i, valmfg = 0;
for (i=54; i<69; i+=2) {
if(data[i+1] != mfgname[i-54]) valmfg = 1;
if(data[i] != mfgname[i-53]) valmfg = 1;
}
// Not the right type, just return.
if(valmfg == 1) {
return -1;
}
return 0;
}
int validate_mfg_card(char *device, int fd)
{
int ret = 0;
struct ata_ioc_request *req;
req = build_basic_command(0, 1, 0, ATA_ATA_IDENTIFY, ATA_CMD_READ, SECTOR_SIZE);
ret = execute_command(req, fd);
if(ret)
err(1, "Validate mfg execute command failed.%d %d ",ret, errno);
ret = validate_card(req->data);
if (ret == 0) {
show_details_disk(req->data, device);
}
else {
noupdate_details_disk(device);
}
free_request(req);
return ret;
}
int print_anchor_block(uint8_t *data)
{
int i;
for (i=0;i<512; i++) {
if (i%16 == 0) {
printf("%3.3d-%3.3d ",i,i+15);
}
// Only char strings for now.
printf("%2.2x", data[i] & 0xff);
if (i%2 == 1) {
printf("\n");
}
}
printf("\nMaxBlockEraseCount = 0x%2.2x%2.2x%2.2x%2.2x",data[100] & 0xff,data[101] & 0xff ,data[102] & 0xff,data[103] & 0xff );
if(((data[100] & 0xff) == 0x00)
&& ((data[101] & 0xff) == 0x00)
&& ((data[102] & 0xff) == 0x00)
&& ((data[103] & 0xff) == 0x00))
{
return 0;
}
return 1;
}
int read_anchor_block(int fd)
{
int ret = 0;
struct ata_ioc_request *req;
req = build_basic_command(0, 0x88, 0x57, ATA_SETFEATURES, 0, 0);
if((ret = execute_command(req, fd))) {
err(1, "Read Anchor block set features failed..");
return ret;
}
req = build_basic_command(0, 1, 0x75, ATA_READ, ATA_CMD_READ, SECTOR_SIZE);
if((ret = execute_command(req, fd))) {
err(1, "Read Anchor block Read failed..");
return ret;
}
// Return the error code.
return print_anchor_block(req->data);
}
int prepare_write_buffer(uint8_t *data, int flag)
{
SAVE_U32(data, MASK_OFFSET, 100, 0xFFFFFFFF);
if(!flag)
{
SAVE_U32(data, DATA_OFFSET, 100, 100000);
SAVE_U32(data, DATA_OFFSET, 140, 80000);
SAVE_U32(data, DATA_OFFSET, 144, 100000);
SAVE_U32(data, DATA_OFFSET, 148, 100002);
}
else
{
// Reset this to 0.
SAVE_U32(data, DATA_OFFSET, 100, 0);
}
printf("rq->data.:%x %x %x %x\n",data[1024 + 100], data[1024+101], data[1024+102], data[1024+103]);
}
int fix_anchor_block(int fd, int flag)
{
int ret = 0;
struct ata_ioc_request *req;
req = build_basic_command(0, 0x08, 0x07, 0x92, ATA_CMD_WRITE, 8192);
// Prepare the write buffer.
prepare_write_buffer(req->data , flag);
ret = execute_command(req, fd);
return ret;
}
int execute_command(struct ata_ioc_request *req, int fd)
{
int ret = 0;
ret = ioctl(fd, SYSTEM_CODE, req);
return ret;
}
int reset_channel(int fd)
{
struct ata_ioc_request *req;
int ret = 0, channel = 0;
req = build_basic_command(0, 0x03, 0x07, 0xc2, 0, 0);
ret = execute_command(req, fd);
return ret;
}
char *compute_disks() {
int disk_cnt;
int error;
size_t listsize;
char *disklist;
error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
if (error) {
warn("kern.disks sysctl not available");
return NULL;
}
if (listsize == 0)
return (NULL);
disklist = (char *)malloc(listsize + 1);
if (disklist == NULL) {
return NULL;
}
memset(disklist, 0, listsize + 1);
error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
if (error || disklist[0] == 0) {
free(disklist);
return NULL;
}
return disklist;
}
void usage()
{
fprintf(stderr,
"usage: virt-diag <command> args:\n"
" virt-diag -r <read the card details> \n"
" virt-diag -w < read card details and update> \n"
" virt-diag -f < Reset the card to 0 (Only for testing)> \n"
);
exit(EX_USAGE);
}
void
show_details_disk(uint8_t *data, char *disk)
{
int i;
fprintf(stdout, " The disk :%s requires an update. Serial# ",disk);
for (i=54; i<93; i+=2) {
fprintf(stdout, "%c%c",data[i+1],data[i]);
}
fprintf(stdout, "\n");
}
void
noupdate_details_disk(char *disk)
{
fprintf(stdout, " The disk :%s doesnt need an update. ",disk);
fprintf(stdout, "\n");
}
// This is the diagnostic tool. It should be run only once to correct the wear level value on the virtium cards.
int main(int argc, char *argv)
{
char *disklist;
char *disk, ch;
int reset = 0, disk_cnt, ret, fd, read = 0;
if (argc < 2) {
usage();
}
while ((ch = getopt(argc, argv, "rw")) != -1)
switch (ch) {
case 'r':
read = 1;
break;
case 'w':
read = 0;
break;
case 'f':
reset = 1;
break;
case '?':
default:
usage();
/* NOTREACHED */
}
// Disks_computed.
disklist = compute_disks();
for (disk_cnt = 0; disk_cnt < MAX_NO_DISKS; disk_cnt++)
{
int mode;
char device[64];
disk = strsep(&disklist, " ");
if (disk == NULL)
break;
sprintf(device, "/dev/%s", disk);
if ((fd = open(device, O_RDONLY)) < 0)
err(1, "device not found");
// If mfg is different or only reading move on.
if(((ret = validate_mfg_card(device, fd))) || read == 1) {
close(fd);
continue;
}
if((ret = read_anchor_block(fd)) && !reset) {
printf("The value is Already Set. \n");
close(fd);
continue;
}
printf(" Setting the Card ..Might Take a Moment ..Please wait..\n");
if((ret = fix_anchor_block(fd ,reset))) {
err(1, "Fixing Anchor block failed.");
}
if((ret = reset_channel(fd))) {
err(1, "Reset of channel failed for some reason.\n");
}
if((ret = read_anchor_block(fd))) {
printf("Set to correct value .\n");
}
close(fd);
}
exit(0);
}