#include "global.h"
#include <unistd.h>
#include <string.h>
#include "misc.h"
#include "menu_defect.h"
#include "param.h"
#include "ctlr_scsi.h"
struct defect_list work_list;
static int commit_list(void);
int
d_restore(void)
{
int i;
assert(!EMBEDDED_SCSI);
if (!(work_list.flags & LIST_DIRTY)) {
err_print("working list was not modified.\n");
return (0);
}
if (check("Ready to update working list, continue"))
return (-1);
enter_critical();
kill_deflist(&work_list);
if (cur_list.list != NULL) {
work_list.header = cur_list.header;
work_list.list = (struct defect_entry *)zalloc(
deflist_size(cur_blksz, work_list.header.count) *
cur_blksz);
for (i = 0; i < work_list.header.count; i++)
*(work_list.list + i) = *(cur_list.list + i);
}
work_list.flags = 0;
if (work_list.list == NULL)
fmt_print("working list set to null.\n\n");
else
fmt_print("working list updated, total of %d defects.\n\n",
work_list.header.count);
exit_critical();
return (0);
}
int
d_original(void)
{
int status;
if (cur_ops->op_ex_man == NULL) {
err_print("Controller does not support extracting ");
err_print("manufacturer's defect list.\n");
return (-1);
}
if (!(cur_ctype->ctype_flags & CF_CONFIRM) &&
check(
"Ready to update working list. This cannot be interrupted\n\
and may take a long while. Continue"))
return (-1);
enter_critical();
kill_deflist(&work_list);
fmt_print("Extracting manufacturer's defect list...");
status = (*cur_ops->op_ex_man)(&work_list);
if (status)
fmt_print("Extraction failed.\n\n");
else {
fmt_print("Extraction complete.\n");
fmt_print("Working list updated, total of %d defects.\n\n",
work_list.header.count);
}
work_list.flags |= LIST_DIRTY;
exit_critical();
return (status);
}
int
d_extract(void)
{
int status;
if (cur_ops->op_ex_cur == NULL) {
err_print("Controller does not support extracting ");
err_print("current defect list.\n");
return (-1);
}
if (! (cur_flags & DISK_FORMATTED) &&
(check(
"Cannot extract defect list from an unformatted disk. Continue")))
return (-1);
if (! (cur_ctype->ctype_flags & CF_CONFIRM) &&
check(
"Ready to extract working list. This cannot be interrupted\n\
and may take a long while. Continue"))
return (-1);
enter_critical();
kill_deflist(&work_list);
fmt_print("Extracting defect list...");
status = (*cur_ops->op_ex_cur)(&work_list);
if (status) {
if (!EMBEDDED_SCSI) {
if (cur_flags & DISK_FORMATTED)
read_list(&work_list);
if (work_list.list != NULL) {
status = 0;
fmt_print("Extraction complete.\n");
fmt_print(
"Working list updated, total of %d defects.\n\n",
work_list.header.count);
} else {
fmt_print("Extraction failed.\n\n");
}
} else {
fmt_print("Extraction failed.\n\n");
}
} else {
fmt_print("Extraction complete.\n");
fmt_print("Working list updated, total of %d defects.\n\n",
work_list.header.count);
}
work_list.flags |= LIST_DIRTY;
exit_critical();
return (status);
}
int
d_add(void)
{
int type, deflt, index;
diskaddr_t bn;
u_ioparam_t ioparam;
struct defect_entry def;
assert(!EMBEDDED_SCSI);
fmt_print(" 0. bytes-from-index\n");
fmt_print(" 1. logical block\n");
deflt = 0;
ioparam.io_bounds.lower = 0;
ioparam.io_bounds.upper = 1;
type = input(FIO_INT, "Select input format (enter its number)", ':',
&ioparam, &deflt, DATA_INPUT);
fmt_print("\nEnter Control-C to terminate.\n");
loop:
if (type) {
def.bfi = def.nbits = UNKNOWN;
ioparam.io_bounds.lower = 0;
if (cur_disk->label_type == L_TYPE_SOLARIS) {
ioparam.io_bounds.upper = physsects() - 1;
} else {
ioparam.io_bounds.upper = cur_parts->etoc->efi_last_lba;
}
bn = input(FIO_BN, "Enter defective block number", ':',
&ioparam, NULL, DATA_INPUT);
def.cyl = bn2c(bn);
def.head = bn2h(bn);
def.sect = bn2s(bn);
} else {
def.sect = UNKNOWN;
ioparam.io_bounds.lower = 0;
ioparam.io_bounds.upper = pcyl - 1;
def.cyl = input(FIO_INT,
"Enter defect's cylinder number", ':',
&ioparam, NULL, DATA_INPUT);
ioparam.io_bounds.upper = nhead - 1;
def.head = input(FIO_INT, "Enter defect's head number",
':', &ioparam, NULL, DATA_INPUT);
ioparam.io_bounds.upper = cur_dtype->dtype_bpt - 1;
def.bfi = input(FIO_INT, "Enter defect's bytes-from-index",
':', &ioparam, NULL, DATA_INPUT);
ioparam.io_bounds.lower = -1;
ioparam.io_bounds.upper = (cur_dtype->dtype_bpt - def.bfi) * 8;
if (ioparam.io_bounds.upper >= 32 * 1024)
ioparam.io_bounds.upper = 32 * 1024 - 1;
deflt = -1;
def.nbits = input(FIO_INT, "Enter defect's length (in bits)",
':', &ioparam, &deflt, DATA_INPUT);
}
index = sort_defect(&def, &work_list);
fmt_print(DEF_PRINTHEADER);
pr_defect(&def, index);
enter_critical();
work_list.flags |= LIST_DIRTY;
if (work_list.list == NULL) {
work_list.header.magicno = (uint_t)DEFECT_MAGIC;
work_list.header.count = 0;
work_list.list = (struct defect_entry *)zalloc(
deflist_size(cur_blksz, 0) * cur_blksz);
}
add_def(&def, &work_list, index);
fmt_print("defect number %d added.\n\n", index + 1);
exit_critical();
goto loop;
#ifdef lint
return (0);
#endif
}
int
d_delete(void)
{
int i, count, num;
u_ioparam_t ioparam;
assert(!EMBEDDED_SCSI);
count = work_list.header.count;
if (work_list.list == NULL || count == 0) {
err_print("No defects to delete.\n");
return (-1);
}
ioparam.io_bounds.lower = 1;
ioparam.io_bounds.upper = count;
num = input(FIO_INT, "Specify defect to be deleted (enter its number)",
':', &ioparam, NULL, DATA_INPUT);
--num;
fmt_print(DEF_PRINTHEADER);
pr_defect(work_list.list + num, num);
enter_critical();
for (i = num; i < count - 1; i++)
*(work_list.list + i) = *(work_list.list + i + 1);
if (deflist_size(cur_blksz, count - 1) <
deflist_size(cur_blksz, count))
work_list.list = (struct defect_entry *)rezalloc(
(void *)work_list.list,
deflist_size(cur_blksz, count - 1) * cur_blksz);
work_list.header.count--;
(void) checkdefsum(&work_list, CK_MAKESUM);
work_list.flags |= LIST_DIRTY;
fmt_print("defect number %d deleted.\n\n", ++num);
exit_critical();
return (0);
}
int
d_print(void)
{
int i, nomore = 0;
int c, one_line = 0;
int tty_lines = get_tty_lines();
if (work_list.list == NULL) {
if (EMBEDDED_SCSI)
err_print(
"No list defined,extract primary or grown or both defects list first.\n");
else
err_print("No working list defined.\n");
return (-1);
}
if (option_f || (!isatty(0)) || (!isatty(1)))
nomore++;
else {
enter_critical();
echo_off();
charmode_on();
exit_critical();
}
if (work_list.header.count != 0)
fmt_print(DEF_PRINTHEADER);
for (i = 0; i < work_list.header.count; i++) {
if (one_line ||
(!nomore && ((i + 1) % (tty_lines - 1) == 0))) {
fmt_print("- hit space for more - ");
c = getchar();
fmt_print("\015");
one_line = 0;
if (c == '\012') {
one_line++;
}
if (c == 'q') {
fmt_print(" \015");
goto PRINT_EXIT;
}
if (c == '\004')
fullabort();
}
pr_defect(work_list.list + i, i);
}
fmt_print("total of %d defects.\n\n", i);
PRINT_EXIT:
if (!nomore) {
enter_critical();
charmode_off();
echo_on();
exit_critical();
}
return (0);
}
int
d_dump(void)
{
int i, status = 0;
char *str;
FILE *fptr;
struct defect_entry *dptr;
if (work_list.list == NULL) {
if (EMBEDDED_SCSI)
err_print(
"No list defined,extract primary or grown or both defects list first.\n");
else
err_print("No working list defined.\n");
return (-1);
}
str = (char *)(uintptr_t)input(FIO_OSTR, "Enter name of defect file",
':', NULL, NULL, DATA_INPUT);
enter_critical();
if ((fptr = fopen(str, "w+")) == NULL) {
err_print("unable to open defect file.\n");
status = -1;
goto out;
}
(void) fprintf(fptr, "0x%08x%8d 0x%08x\n",
work_list.header.magicno,
work_list.header.count, work_list.header.cksum);
for (i = 0; i < work_list.header.count; i++) {
dptr = work_list.list + i;
(void) fprintf(fptr, "%4d%8d%7d%8d%8d%8d\n",
i+1, dptr->cyl, dptr->head,
dptr->bfi, dptr->nbits, dptr->sect);
}
fmt_print("defect file updated, total of %d defects.\n", i);
(void) fclose(fptr);
out:
destroy_data(str);
exit_critical();
fmt_print("\n");
return (status);
}
int
d_load(void)
{
int i, items, status = 0, count, cksum;
uint_t magicno;
char *str;
TOKEN filename;
FILE *fptr;
struct defect_entry *dptr;
assert(!EMBEDDED_SCSI);
str = (char *)(uintptr_t)input(FIO_OSTR, "Enter name of defect file",
':', NULL, NULL, DATA_INPUT);
enter_critical();
(void) strcpy(filename, str);
destroy_data(str);
exit_critical();
status = access(filename, 4);
if (status) {
err_print("defect file not accessable.\n");
return (-1);
}
if (check("ready to update working list, continue"))
return (-1);
enter_critical();
if ((fptr = fopen(filename, "r")) == NULL) {
err_print("unable to open defect file.\n");
exit_critical();
return (-1);
}
items = fscanf(fptr, "0x%x%d 0x%x\n", &magicno,
&count, (uint_t *)&cksum);
if (items != 3 || count < 0 ||
(magicno != (uint_t)DEFECT_MAGIC &&
magicno != (uint_t)NO_CHECKSUM)) {
err_print("Defect file is corrupted.\n");
status = -1;
goto out;
}
kill_deflist(&work_list);
if (magicno == NO_CHECKSUM)
work_list.header.magicno = (uint_t)DEFECT_MAGIC;
else
work_list.header.magicno = magicno;
work_list.header.count = count;
work_list.header.cksum = cksum;
work_list.list = (struct defect_entry *)zalloc(
deflist_size(cur_blksz, count) * cur_blksz);
work_list.flags |= LIST_DIRTY;
for (i = 0; i < count; i++) {
dptr = work_list.list + i;
items = fscanf(fptr, "%*d%hd%hd%d%hd%hd\n", &dptr->cyl,
&dptr->head, &dptr->bfi, &dptr->nbits, &dptr->sect);
if (items != 5)
goto bad;
}
if (magicno != NO_CHECKSUM && checkdefsum(&work_list, CK_CHECKSUM))
goto bad;
fmt_print("working list updated, total of %d defects.\n", i);
goto out;
bad:
err_print("Defect file is corrupted, working list set to NULL.\n");
kill_deflist(&work_list);
status = -1;
out:
(void) fclose(fptr);
exit_critical();
fmt_print("\n");
return (status);
}
int
d_commit(void)
{
if (work_list.list != NULL && !(work_list.flags & LIST_DIRTY)) {
err_print("working list was not modified.\n");
return (0);
}
if (check("Ready to update Current Defect List, continue"))
return (-1);
return (do_commit());
}
int
do_commit(void)
{
int status;
if ((status = commit_list()) == 0) {
fmt_print(\
"Disk must be reformatted for changes to take effect.\n\n");
}
return (status);
}
static int
commit_list(void)
{
int i;
enter_critical();
kill_deflist(&cur_list);
if (work_list.list == NULL) {
work_list.header.magicno = (uint_t)DEFECT_MAGIC;
work_list.header.count = 0;
work_list.list = (struct defect_entry *)zalloc(
deflist_size(cur_blksz, 0) * cur_blksz);
}
cur_list.header = work_list.header;
cur_list.list = (struct defect_entry *)zalloc(
deflist_size(cur_blksz, cur_list.header.count) * cur_blksz);
for (i = 0; i < cur_list.header.count; i++)
*(cur_list.list + i) = *(work_list.list + i);
work_list.flags &= ~(LIST_DIRTY|LIST_RELOAD);
cur_list.flags = work_list.flags;
if (EMBEDDED_SCSI)
fmt_print("Defect List has a total of %d defects.\n",
cur_list.header.count);
else
fmt_print("Current Defect List updated, total of %d defects.\n",
cur_list.header.count);
exit_critical();
return (0);
}
int
d_create(void)
{
int status;
assert(!EMBEDDED_SCSI);
if (cur_ops->op_create == NULL) {
err_print("Controller does not support creating ");
err_print("manufacturer's defect list.\n");
return (-1);
}
if (! (cur_ctype->ctype_flags & CF_SCSI) &&
check(
"Ready to create the manufacturers defect information on the disk.\n\
This cannot be interrupted and may take a long while.\n\
IT WILL DESTROY ALL OF THE DATA ON THE DISK! Continue"))
return (-1);
enter_critical();
fmt_print("Creating manufacturer's defect list...");
status = (*cur_ops->op_create)(&work_list);
if (status) {
fmt_print("Creation failed.\n\n");
} else {
fmt_print("Creation complete.\n");
}
exit_critical();
return (status);
}
int
d_primary(void)
{
int status;
assert(EMBEDDED_SCSI);
enter_critical();
kill_deflist(&work_list);
fmt_print("Extracting primary defect list...");
status = scsi_ex_man(&work_list);
if (status) {
fmt_print("Extraction failed.\n\n");
} else {
fmt_print("Extraction complete.\n");
work_list.flags |= LIST_DIRTY;
status = commit_list();
fmt_print("\n");
}
exit_critical();
return (status);
}
int
d_grown(void)
{
int status;
assert(EMBEDDED_SCSI);
enter_critical();
kill_deflist(&work_list);
fmt_print("Extracting grown defects list...");
status = scsi_ex_grown(&work_list);
if (status) {
fmt_print("Extraction failed.\n\n");
} else {
fmt_print("Extraction complete.\n");
work_list.flags |= LIST_DIRTY;
status = commit_list();
fmt_print("\n");
}
exit_critical();
return (status);
}
int
d_both(void)
{
int status;
assert(EMBEDDED_SCSI);
enter_critical();
kill_deflist(&work_list);
fmt_print("Extracting both primary and grown defects lists...");
status = scsi_ex_cur(&work_list);
if (status) {
fmt_print("Extraction failed.\n\n");
} else {
fmt_print("Extraction complete.\n");
work_list.flags |= LIST_DIRTY;
status = commit_list();
fmt_print("\n");
}
exit_critical();
return (status);
}