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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This file contains the routines for the IDE drive interface
*/
#include "global.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <sys/fcntl.h>
#include <memory.h>
#include <malloc.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/byteorder.h>
#include <errno.h>
#if defined(i386)
#include <sys/dktp/altsctr.h>
#endif
#include <sys/dktp/dadkio.h>
#include "startup.h"
#include "misc.h"
#include "ctlr_ata.h"
#include "analyze.h"
#include "param.h"
#include "io.h"
#include "badsec.h"
#include "menu_fdisk.h"
int wr_altsctr();
int read_altsctr();
int updatebadsec();
#ifdef __STDC__
static int ata_ck_format(void);
#ifdef i386
static int ata_ex_cur(struct defect_list *);
static int ata_wr_cur(struct defect_list *);
static int ata_repair(diskaddr_t, int);
#endif /* i386 */
#else /* __STDC__ */
static int ata_ck_format();
#ifdef i386
static int ata_ex_cur();
static int ata_wr_cur();
static int ata_repair();
#endif /* i386 */
#endif
struct ctlr_ops ataops = {
#if defined(sparc)
ata_rdwr,
ata_ck_format,
0,
0,
0,
0,
0,
0,
#else
ata_rdwr,
ata_ck_format,
0,
0,
ata_ex_cur,
ata_repair,
0,
ata_wr_cur,
#endif /* defined(sparc) */
};
struct ctlr_ops pcmcia_ataops = {
ata_rdwr,
ata_ck_format,
0,
0,
0,
0,
0,
0,
};
#if defined(i386)
static struct dkl_partition *dpart = NULL;
#endif /* defined(i386) */
extern struct badsec_lst *badsl_chain;
extern int badsl_chain_cnt;
extern struct badsec_lst *gbadsl_chain;
extern int gbadsl_chain_cnt;
extern struct alts_mempart *ap;
static char *dadkrawioerrs[] = {
"cmd was successful", /* DADKIO_STAT_NO_ERROR */
"device not ready", /* DADKIO_STAT_NOT_READY */
"error on medium blkno: %d", /* DADKIO_STAT_MEDIUM_ERROR */
"other hardware error", /* DADKIO_STAT_HARDWARE_ERROR */
"illegal request", /* DADKIO_STAT_ILLEGAL_REQUEST */
"illegal block address: %d", /* DADKIO_STAT_ILLEGAL_ADDRESS */
"device write-protected", /* DADKIO_STAT_WRITE_PROTECTED */
"no response from device", /* DADKIO_STAT_TIMED_OUT */
"parity error in data", /* DADKIO_STAT_PARITY */
"error on bus", /* DADKIO_STAT_BUS_ERROR */
"data recovered via ECC", /* DADKIO_STAT_SOFT_ERROR */
"no resources for cmd", /* DADKIO_STAT_NO_RESOURCES */
"device is not formatted", /* DADKIO_STAT_NOT_FORMATTED */
"device is reserved", /* DADKIO_STAT_RESERVED */
"feature not supported", /* DADKIO_STAT_NOT_SUPPORTED */
};
/*ARGSUSED6*/
#if defined(i386)
int
ata_rdwr(int dir, int fd, diskaddr_t blk64, int secnt, caddr_t bufaddr,
int flags, int *xfercntp)
#else /* defined(i386) */
static int
ata_rdwr(int dir, int fd, diskaddr_t blk64, int secnt, caddr_t bufaddr,
int flags, int *xfercntp)
#endif /* defined(i386) */
{
int tmpsec;
struct dadkio_rwcmd dadkio_rwcmd;
blkaddr_t blkno;
blkno = (blkaddr_t)blk64;
bzero((caddr_t)&dadkio_rwcmd, sizeof (struct dadkio_rwcmd));
tmpsec = secnt * cur_blksz;
/* Doing raw read */
dadkio_rwcmd.cmd = (dir == DIR_READ) ? DADKIO_RWCMD_READ :
DADKIO_RWCMD_WRITE;
dadkio_rwcmd.blkaddr = blkno;
dadkio_rwcmd.buflen = tmpsec;
dadkio_rwcmd.flags = flags;
dadkio_rwcmd.bufaddr = bufaddr;
media_error = 0;
if (cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
/*
* PCATA requires to use "p0" when calling
* DIOCTL_RWCMD ioctl() to read/write the label
*/
(void) close(fd);
(void) open_cur_file(FD_USE_P0_PATH);
fd = cur_file;
}
if (ioctl(fd, DIOCTL_RWCMD, &dadkio_rwcmd) == -1) {
err_print("DIOCTL_RWCMD: %s\n", strerror(errno));
return (1);
}
if (cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
/* Restore cur_file with cur_disk->disk_path */
(void) open_cur_file(FD_USE_CUR_DISK_PATH);
}
switch (dadkio_rwcmd.status.status) {
case DADKIO_STAT_NOT_READY:
disk_error = DISK_STAT_NOTREADY;
break;
case DADKIO_STAT_RESERVED:
disk_error = DISK_STAT_RESERVED;
break;
case DADKIO_STAT_WRITE_PROTECTED:
disk_error = DISK_STAT_DATA_PROTECT;
break;
case DADKIO_STAT_MEDIUM_ERROR:
media_error = 1;
break;
}
if (dadkio_rwcmd.status.status) {
if ((flags & F_SILENT) == 0)
err_print(dadkrawioerrs[dadkio_rwcmd.status.status],
dadkio_rwcmd.status.failed_blk);
return (1);
}
return (0);
}
int
ata_ck_format()
{
char *bufaddr;
int status;
bufaddr = (char *)zalloc(4 * cur_blksz);
status = ata_rdwr(DIR_READ, cur_file, (diskaddr_t)1, 4,
(caddr_t)bufaddr, 0, NULL);
free(bufaddr);
return (!status);
}
#if defined(i386)
static int
get_alts_slice()
{
int i;
int alts_slice = -1;
if (cur_parts == NULL) {
(void) fprintf(stderr, "No current partition list\n");
return (-1);
}
for (i = 0; i < V_NUMPAR && alts_slice == -1; i++) {
if (cur_parts->vtoc.v_part[i].p_tag == V_ALTSCTR) {
alts_slice = i;
dpart = &cur_parts->vtoc.v_part[i];
}
}
if (alts_slice == -1) {
(void) fprintf(stderr, "NO Alt slice\n");
return (-1);
}
if (!solaris_offset)
if (copy_solaris_part(&cur_disk->fdisk_part))
return (-1);
altsec_offset = dpart->p_start + solaris_offset;
return (SUCCESS);
}
static int
put_alts_slice()
{
int status;
status = wr_altsctr();
if (status) {
return (status);
}
if (ioctl(cur_file, DKIOCADDBAD, NULL) == -1) {
(void) fprintf(stderr, "Warning: DKIOCADDBAD ioctl failed\n");
sync();
return (-1);
}
sync();
return (0);
}
static int
ata_convert_list(struct defect_list *list, int list_format)
{
int i;
struct defect_entry *new_defect;
switch (list_format) {
case BFI_FORMAT:
if (ap->ap_tblp->alts_ent_used) {
new_defect = calloc(ap->ap_tblp->alts_ent_used,
sizeof (struct defect_entry));
if (new_defect == NULL) {
err_print(
"ata_convert_list: calloc failed\n");
fullabort();
}
list->header.count = ap->ap_tblp->alts_ent_used;
list->header.magicno = (uint_t)DEFECT_MAGIC;
list->list = new_defect;
for (i = 0; i < ap->ap_tblp->alts_ent_used;
i++, new_defect++) {
new_defect->cyl =
bn2c((ap->ap_entp)[i].bad_start);
new_defect->head =
bn2h((ap->ap_entp)[i].bad_start);
new_defect->bfi = UNKNOWN;
new_defect->sect =
bn2s((ap->ap_entp)[i].bad_start);
new_defect->nbits = UNKNOWN;
}
} else {
list->header.count = 0;
list->header.magicno = (uint_t)DEFECT_MAGIC;
new_defect = calloc(1,
sizeof (struct defect_entry));
if (new_defect == NULL) {
err_print(
"ata_convert_list: calloc failed\n");
fullabort();
}
list->list = new_defect;
}
break;
default:
err_print("ata_convert_list: can't deal with it\n");
exit(0);
}
(void) checkdefsum(list, CK_MAKESUM);
return (0);
}
/*
* NB - there used to be a ata_ex_man() which was identical to
* ata_ex_cur; since it's really not a "manufacturer's list",
* it's gone; if we ever want that exact functionality back,
* we can add ata_ex_cur() to the ctlr_ops above. Otherwise,
* if this is ever modified to support formatting of IDE drives,
* we should probably add something that issues the
* drive Read Defect list rather than getting the s9 info
* as ata_ex_cur() does.
*/
static int
ata_ex_cur(struct defect_list *list)
{
int status;
status = get_alts_slice();
if (status)
return (status);
status = read_altsctr(dpart);
if (status) {
return (status);
}
(void) ata_convert_list(list, BFI_FORMAT);
return (status);
}
int
ata_repair(diskaddr_t bn, int flag)
{
int status;
struct badsec_lst *blc_p;
struct badsec_lst *blc_p_nxt;
#ifdef lint
flag++;
#endif
(void) get_alts_slice();
if (!gbadsl_chain) {
blc_p = (struct badsec_lst *)calloc(1, BADSLSZ);
if (!blc_p) {
(void) fprintf(stderr,
"Unable to allocate memory for additional bad sectors\n");
return (-1);
}
gbadsl_chain = blc_p;
}
for (blc_p = gbadsl_chain; blc_p->bl_nxt; )
blc_p = blc_p->bl_nxt;
if (blc_p->bl_cnt == MAXBLENT) {
blc_p->bl_nxt = (struct badsec_lst *)calloc(1, BADSLSZ);
if (!blc_p->bl_nxt) {
(void) fprintf(stderr,
"Unable to allocate memory for additional bad sectors\n");
return (-1);
}
blc_p = blc_p->bl_nxt;
}
blc_p->bl_sec[blc_p->bl_cnt++] = (uint_t)bn;
gbadsl_chain_cnt++;
(void) updatebadsec(dpart, 0);
status = put_alts_slice();
/* clear out the bad sector list chains that were generated */
if (badsl_chain) {
if (badsl_chain->bl_nxt == NULL) {
free(badsl_chain);
} else {
for (blc_p = badsl_chain; blc_p; ) {
blc_p_nxt = blc_p->bl_nxt;
free(blc_p);
blc_p = blc_p_nxt;
}
}
badsl_chain = NULL;
badsl_chain_cnt = 0;
}
if (gbadsl_chain) {
if (gbadsl_chain->bl_nxt == NULL) {
free(gbadsl_chain);
} else {
for (blc_p = gbadsl_chain; blc_p; ) {
blc_p_nxt = blc_p->bl_nxt;
free(blc_p);
blc_p = blc_p_nxt;
}
}
gbadsl_chain = NULL;
gbadsl_chain_cnt = 0;
}
return (status);
}
int
ata_wr_cur(struct defect_list *list)
{
int status;
int sec_count;
int x;
struct badsec_lst *blc_p;
struct badsec_lst *blc_p_nxt;
struct defect_entry *dlist;
if (list->header.magicno != (uint_t)DEFECT_MAGIC)
return (-1);
sec_count = list->header.count;
dlist = list->list;
(void) get_alts_slice();
for (x = 0; x < sec_count; x++) {
/* test for unsupported list format */
if ((dlist->bfi != UNKNOWN) || (dlist->nbits != UNKNOWN)) {
(void) fprintf(stderr,
"BFI unsuported format for bad sectors\n");
return (-1);
}
if (!gbadsl_chain) {
blc_p = (struct badsec_lst *)calloc(1, BADSLSZ);
if (!blc_p) {
(void) fprintf(stderr,
"Unable to allocate memory for additional bad sectors\n");
return (-1);
}
gbadsl_chain = blc_p;
}
for (blc_p = gbadsl_chain; blc_p->bl_nxt; )
blc_p = blc_p->bl_nxt;
if (blc_p->bl_cnt == MAXBLENT) {
blc_p->bl_nxt = (struct badsec_lst *)calloc(1, BADSLSZ);
if (!blc_p->bl_nxt) {
(void) fprintf(stderr,
"Unable to allocate memory for additional bad sectors\n");
return (-1);
}
blc_p = blc_p->bl_nxt;
}
blc_p->bl_sec[blc_p->bl_cnt++] =
(uint_t)chs2bn(dlist->cyl, dlist->head, dlist->sect);
gbadsl_chain_cnt++;
dlist++;
}
(void) updatebadsec(dpart, 0);
status = put_alts_slice();
/* clear out the bad sector list chains that were generated */
if (badsl_chain) {
if (badsl_chain->bl_nxt == NULL) {
free(badsl_chain);
} else {
for (blc_p = badsl_chain; blc_p; ) {
blc_p_nxt = blc_p->bl_nxt;
free(blc_p);
blc_p = blc_p_nxt;
}
}
badsl_chain = NULL;
badsl_chain_cnt = 0;
}
if (gbadsl_chain) {
if (gbadsl_chain->bl_nxt == NULL) {
free(gbadsl_chain);
} else {
for (blc_p = gbadsl_chain; blc_p; ) {
blc_p_nxt = blc_p->bl_nxt;
free(blc_p);
blc_p = blc_p_nxt;
}
}
gbadsl_chain = NULL;
gbadsl_chain_cnt = 0;
}
return (status);
}
#endif /* defined(i386) */
|