#include "global.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <errno.h>
#include <memory.h>
#include <dirent.h>
#include <sys/fcntl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include "startup.h"
#include "param.h"
#include "label.h"
#include "misc.h"
#include "menu_command.h"
#include "partition.h"
#include "ctlr_scsi.h"
#include "auto_sense.h"
extern struct ctlr_type ctlr_types[];
extern int nctypes;
extern struct ctlr_ops genericops;
extern long strtol();
extern int errno;
char *file_name;
char *option_d;
char *option_f;
char *option_l;
char *option_p;
char option_s;
char *option_t;
char *option_x;
char diag_msg;
char option_msg;
int need_newline;
int dev_expert;
int expert_mode;
uint_t cur_blksz;
struct ctlr_info *ctlr_list;
struct disk_info *disk_list;
struct mctlr_list *controlp;
char x86_devname[MAXNAMELEN];
FILE *data_file;
#ifdef __STDC__
static void usage(void);
static int sup_prxfile(void);
static void sup_setpath(void);
static void sup_setdtype(void);
static int sup_change_spec(struct disk_type *, char *);
static void sup_setpart(void);
static void search_for_logical_dev(char *devname);
static void add_device_to_disklist(char *devname, char *devpath);
static int disk_is_known(struct dk_cinfo *dkinfo);
static void datafile_error(char *errmsg, char *token);
static void search_duplicate_dtypes(void);
static void search_duplicate_pinfo(void);
static void check_dtypes_for_inconsistency(struct disk_type *dp1,
struct disk_type *dp2);
static void check_pinfo_for_inconsistency(struct partition_info *pp1,
struct partition_info *pp2);
static uint_t str2blks(char *str);
static int str2cyls(char *str);
static struct chg_list *new_chg_list(struct disk_type *);
static char *get_physical_name(char *);
static void sort_disk_list(void);
static int disk_name_compare(const void *, const void *);
static void make_controller_list(void);
static void check_for_duplicate_disknames(char *arglist[]);
#else
static void usage();
static int sup_prxfile();
static void sup_setpath();
static void sup_setdtype();
static int sup_change_spec();
static void sup_setpart();
static void search_for_logical_dev();
static void add_device_to_disklist();
static int disk_is_known();
static void datafile_error();
static void search_duplicate_dtypes();
static void search_duplicate_pinfo();
static void check_dtypes_for_inconsistency();
static void check_pinfo_for_inconsistency();
static uint_t str2blks();
static int str2cyls();
static struct chg_list *new_chg_list();
static char *get_physical_name();
static void sort_disk_list();
static int disk_name_compare();
static void make_controller_list();
static void check_for_duplicate_disknames();
#endif
#if defined(sparc)
static char *other_ctlrs[] = {
"ata"
};
#define OTHER_CTLRS 1
#elif defined(i386)
static char *other_ctlrs[] = {
"ISP-80"
};
#define OTHER_CTLRS 2
#else
#error No Platform defined.
#endif
int data_lineno;
static char **search_path = NULL;
static int name_represents_wholedisk(char *name);
static void get_disk_name(int fd, char *disk_name, struct disk_info *disk_info);
int
do_options(int argc, char *argv[])
{
char *ptr;
int i;
int next;
option_msg = 0;
diag_msg = 0;
expert_mode = 0;
need_newline = 0;
dev_expert = 0;
for (i = 1; i < argc; i = next) {
next = i + 1;
if (*argv[i] != '-')
return (i);
for (ptr = argv[i] + 1; *ptr != '\0'; ptr++) {
switch (*ptr) {
case 's':
case 'S':
option_s = 1;
break;
case 'f':
case 'F':
option_f = argv[next++];
if (next > argc)
goto badopt;
break;
case 'l':
case 'L':
option_l = argv[next++];
if (next > argc)
goto badopt;
break;
case 'x':
case 'X':
option_x = argv[next++];
if (next > argc)
goto badopt;
break;
case 'd':
case 'D':
option_d = argv[next++];
if (next > argc)
goto badopt;
break;
case 't':
case 'T':
option_t = argv[next++];
if (next > argc)
goto badopt;
break;
case 'p':
case 'P':
option_p = argv[next++];
if (next > argc)
goto badopt;
break;
case 'm':
option_msg = 1;
break;
case 'M':
option_msg = 1;
diag_msg = 1;
break;
case 'e':
expert_mode = 1;
break;
#ifdef DEBUG
case 'z':
dev_expert = 1;
break;
#endif
default:
badopt:
usage();
break;
}
}
}
return (-1);
}
static void
usage(void)
{
err_print("Usage: format [-s][-d disk_name]");
err_print("[-t disk_type][-p partition_name]\n");
err_print("\t[-f cmd_file][-l log_file]");
err_print("[-x data_file] [-m] [-M] [-e] disk_list\n");
fullabort();
}
void
sup_init(void)
{
int nopened_files = 0;
char fname[MAXPATHLEN];
char *path;
char *p;
struct stat stbuf;
make_controller_list();
if (option_x) {
file_name = option_x;
if (sup_prxfile()) {
nopened_files++;
} else {
err_print("Unable to open data file '%s' - %s.\n",
file_name, strerror(errno));
fullabort();
}
}
path = getenv("FORMAT_PATH");
if (path == NULL) {
path = "/etc";
}
while (*path != 0) {
p = fname;
while (*path != 0 && *path != ':')
*p++ = *path++;
if (p == fname)
continue;
*p = 0;
if (*path == ':')
path++;
if (stat(fname, &stbuf) == -1) {
err_print("Unable to access '%s' - %s.\n",
fname, strerror(errno));
} else {
if (S_ISDIR(stbuf.st_mode)) {
if (*(p-1) != '/')
*p++ = '/';
(void) strcpy(p, "format.dat");
}
file_name = alloc_string(fname);
if (sup_prxfile()) {
nopened_files++;
}
}
}
search_duplicate_dtypes();
search_duplicate_pinfo();
}
static int
sup_prxfile(void)
{
int status;
TOKEN token;
TOKEN cleaned;
data_file = fopen(file_name, "r");
if (data_file == NULL) {
return (0);
}
data_lineno = 0;
for (;;) {
data_lineno++;
status = sup_gettoken(token);
if (status == SUP_EOF)
break;
if (status == SUP_EOL)
continue;
if (status != SUP_STRING) {
datafile_error("Expecting keyword, found '%s'", token);
continue;
}
clean_token(cleaned, token);
if (strcmp(cleaned, "search_path") == 0)
sup_setpath();
else if (strcmp(cleaned, "disk_type") == 0)
sup_setdtype();
else if (strcmp(cleaned, "partition") == 0)
sup_setpart();
else {
datafile_error("Unknown keyword '%s'", cleaned);
}
}
(void) fclose(data_file);
return (1);
}
static void
sup_setpath(void)
{
TOKEN token;
TOKEN cleaned;
int status;
static int path_size;
static int path_alloc;
status = sup_gettoken(token);
if (status != SUP_EQL) {
datafile_error("Expecting '=', found '%s'", token);
return;
}
for (;;) {
status = sup_gettoken(token);
if (status == SUP_EOL)
break;
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'", token);
break;
}
clean_token(cleaned, token);
search_path = build_argvlist(search_path, &path_size,
&path_alloc, cleaned);
status = sup_gettoken(token);
if (status == SUP_EOL)
break;
if (status != SUP_COMMA) {
datafile_error("Expecting ', ', found '%s'", token);
break;
}
}
}
static void
sup_setdtype(void)
{
TOKEN token, cleaned, ident;
int val, status, i;
ulong_t flags = 0;
struct disk_type *dtype, *type;
struct ctlr_type *ctype;
char *dtype_name, *ptr;
struct mctlr_list *mlp;
status = sup_gettoken(token);
if (status != SUP_EQL) {
datafile_error("Expecting '=', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'", token);
return;
}
clean_token(cleaned, token);
dtype_name = (char *)zalloc(strlen(cleaned) + 1);
(void) strcpy(dtype_name, cleaned);
dtype = (struct disk_type *)zalloc(sizeof (struct disk_type));
dtype->dtype_asciilabel = dtype_name;
dtype->dtype_filename = file_name;
dtype->dtype_lineno = data_lineno;
for (;;) {
status = sup_gettoken(token);
if (status == SUP_EOL)
break;
if (status != SUP_COLON) {
datafile_error("Expecting ':', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status == SUP_EOL)
break;
if (status != SUP_STRING) {
datafile_error("Expecting keyword, found '%s'", token);
return;
}
clean_token(ident, token);
if (sup_change_spec(dtype, ident)) {
continue;
}
status = sup_gettoken(token);
if (status != SUP_EQL) {
datafile_error("Expecting '=', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'", token);
return;
}
clean_token(cleaned, token);
if (strcmp(ident, "ctlr") == 0) {
mlp = controlp;
while (mlp != NULL) {
if (strcmp(mlp->ctlr_type->ctype_name,
cleaned) == 0)
break;
mlp = mlp->next;
}
if (mlp == NULL) {
for (i = 0; i < OTHER_CTLRS; i++) {
if (strcmp(other_ctlrs[i], cleaned)
== 0) {
datafile_error(NULL, NULL);
return;
}
}
if (i == OTHER_CTLRS) {
datafile_error(
"Unknown controller '%s'",
cleaned);
return;
}
}
ctype = mlp->ctlr_type;
flags |= SUP_CTLR;
continue;
}
val = (int)strtol(cleaned, &ptr, 0);
if (*ptr != '\0') {
datafile_error("Expecting an integer, found '%s'",
cleaned);
return;
}
if (strcmp(ident, "ncyl") == 0) {
dtype->dtype_ncyl = val;
flags |= SUP_NCYL;
} else if (strcmp(ident, "acyl") == 0) {
dtype->dtype_acyl = val;
flags |= SUP_ACYL;
} else if (strcmp(ident, "pcyl") == 0) {
dtype->dtype_pcyl = val;
flags |= SUP_PCYL;
} else if (strcmp(ident, "nhead") == 0) {
dtype->dtype_nhead = val;
flags |= SUP_NHEAD;
} else if (strcmp(ident, "nsect") == 0) {
dtype->dtype_nsect = val;
flags |= SUP_NSECT;
} else if (strcmp(ident, "rpm") == 0) {
dtype->dtype_rpm = val;
flags |= SUP_RPM;
} else if (strcmp(ident, "bpt") == 0) {
dtype->dtype_bpt = val;
flags |= SUP_BPT;
} else if (strcmp(ident, "bps") == 0) {
dtype->dtype_bps = val;
flags |= SUP_BPS;
} else if (strcmp(ident, "drive_type") == 0) {
dtype->dtype_dr_type = val;
flags |= SUP_DRTYPE;
} else if (strcmp(ident, "cache") == 0) {
dtype->dtype_cache = val;
flags |= SUP_CACHE;
} else if (strcmp(ident, "prefetch") == 0) {
dtype->dtype_threshold = val;
flags |= SUP_PREFETCH;
} else if (strcmp(ident, "read_retries") == 0) {
dtype->dtype_read_retries = val;
flags |= SUP_READ_RETRIES;
} else if (strcmp(ident, "write_retries") == 0) {
dtype->dtype_write_retries = val;
flags |= SUP_WRITE_RETRIES;
} else if (strcmp(ident, "min_prefetch") == 0) {
dtype->dtype_prefetch_min = val;
flags |= SUP_CACHE_MIN;
} else if (strcmp(ident, "max_prefetch") == 0) {
dtype->dtype_prefetch_max = val;
flags |= SUP_CACHE_MAX;
} else if (strcmp(ident, "trks_zone") == 0) {
dtype->dtype_trks_zone = val;
flags |= SUP_TRKS_ZONE;
} else if (strcmp(ident, "atrks") == 0) {
dtype->dtype_atrks = val;
flags |= SUP_ATRKS;
} else if (strcmp(ident, "asect") == 0) {
dtype->dtype_asect = val;
flags |= SUP_ASECT;
} else if (strcmp(ident, "psect") == 0) {
dtype->dtype_psect = val;
flags |= SUP_PSECT;
} else if (strcmp(ident, "phead") == 0) {
dtype->dtype_phead = val;
flags |= SUP_PHEAD;
} else if (strcmp(ident, "fmt_time") == 0) {
dtype->dtype_fmt_time = val;
flags |= SUP_FMTTIME;
} else if (strcmp(ident, "cyl_skew") == 0) {
dtype->dtype_cyl_skew = val;
flags |= SUP_CYLSKEW;
} else if (strcmp(ident, "trk_skew") == 0) {
dtype->dtype_trk_skew = val;
flags |= SUP_TRKSKEW;
} else {
datafile_error("Unknown keyword '%s'", ident);
}
}
dtype->dtype_options = flags;
if ((flags & SUP_MIN_DRIVE) != SUP_MIN_DRIVE) {
datafile_error("Incomplete specification", "");
return;
}
if ((!(ctype->ctype_flags & CF_SCSI)) && (!(flags & SUP_BPT)) &&
(!(ctype->ctype_flags & CF_NOFORMAT))) {
datafile_error("Incomplete specification", "");
return;
}
if ((ctype->ctype_flags & CF_SMD_DEFS) && (!(flags & SUP_BPS))) {
datafile_error("Incomplete specification", "");
return;
}
assert(flags & SUP_CTLR);
type = ctype->ctype_dlist;
if (type == NULL) {
ctype->ctype_dlist = dtype;
} else {
while (type->dtype_next != NULL)
type = type->dtype_next;
type->dtype_next = dtype;
}
}
static int
sup_change_spec(struct disk_type *disk, char *id)
{
char *p;
char *p2;
int pageno;
int byteno;
int mode;
int value;
TOKEN token;
TOKEN ident;
struct chg_list *cp;
int tilde;
int i;
if (*id != 'p') {
return (0);
}
pageno = (int)strtol(id+1, &p2, 0);
if (*p2 != 0) {
return (0);
}
if (!scsi_supported_page(pageno)) {
datafile_error("Unsupported mode page '%s'", id);
return (1);
}
if (sup_gettoken(token) != SUP_STRING) {
datafile_error("Unexpected value '%s'", token);
return (1);
}
clean_token(ident, token);
p = ident;
if (*p++ != 'b') {
datafile_error("Unknown keyword '%s'", ident);
return (1);
}
byteno = (int)strtol(p, &p2, 10);
if (*p2 != 0) {
datafile_error("Unknown keyword '%s'", ident);
return (1);
}
if (byteno == 0 || byteno == 1) {
datafile_error("Unsupported byte offset '%s'", ident);
return (1);
}
mode = CHG_MODE_UNDEFINED;
switch (sup_gettoken(token)) {
case SUP_EQL:
mode = CHG_MODE_ABS;
break;
case SUP_OR:
if (sup_gettoken(token) == SUP_EQL)
mode = CHG_MODE_SET;
break;
case SUP_AND:
if (sup_gettoken(token) == SUP_EQL)
mode = CHG_MODE_CLR;
break;
}
if (mode == CHG_MODE_UNDEFINED) {
datafile_error("Unexpected operator: '%s'", token);
return (1);
}
tilde = 0;
if ((i = sup_gettoken(token)) == SUP_TILDE) {
tilde = 1;
i = sup_gettoken(token);
}
if (i != SUP_STRING) {
datafile_error("Expecting value, found '%s'", token);
return (1);
}
clean_token(ident, token);
value = (int)strtol(ident, &p, 0);
if (*p != 0) {
datafile_error("Expecting value, found '%s'", token);
return (1);
}
if (tilde) {
value = ~value;
}
value &= 0xff;
cp = new_chg_list(disk);
cp->pageno = pageno;
cp->byteno = byteno;
cp->mode = mode;
cp->value = value;
return (1);
}
static void
sup_setpart(void)
{
TOKEN token, cleaned, disk, ctlr, ident;
struct disk_type *dtype = NULL;
struct ctlr_type *ctype = NULL;
struct partition_info *pinfo, *parts;
char *pinfo_name;
int i, index, status, flags = 0;
uint_t val1, val2;
ushort_t vtoc_tag;
ushort_t vtoc_flag;
struct mctlr_list *mlp;
status = sup_gettoken(token);
if (status != SUP_EQL) {
datafile_error("Expecting '=', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'", token);
return;
}
clean_token(cleaned, token);
pinfo_name = (char *)zalloc(strlen(cleaned) + 1);
(void) strcpy(pinfo_name, cleaned);
pinfo = (struct partition_info *)zalloc(sizeof (struct partition_info));
pinfo->pinfo_name = pinfo_name;
pinfo->pinfo_filename = file_name;
pinfo->pinfo_lineno = data_lineno;
set_vtoc_defaults(pinfo);
for (;;) {
status = sup_gettoken(token);
if (status == SUP_EOL)
break;
if (status != SUP_COLON) {
datafile_error("Expecting ':', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status == SUP_EOL)
break;
if (status != SUP_STRING) {
datafile_error("Expecting keyword, found '%s'", token);
return;
}
clean_token(ident, token);
status = sup_gettoken(token);
if (status != SUP_EQL) {
datafile_error("Expecting '=', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'", token);
return;
}
clean_token(cleaned, token);
if (strcmp(ident, "ctlr") == 0) {
(void) strcpy(ctlr, cleaned);
flags |= SUP_CTLR;
continue;
} else if (strcmp(ident, "disk") == 0) {
(void) strcpy(disk, cleaned);
flags |= SUP_DISK;
continue;
}
if (((flags & (SUP_DISK|SUP_CTLR)) == (SUP_DISK|SUP_CTLR)) &&
dtype == NULL && ctype == NULL) {
mlp = controlp;
while (mlp != NULL) {
if (strcmp(mlp->ctlr_type->ctype_name,
ctlr) == 0)
break;
mlp = mlp->next;
}
if (mlp == NULL) {
for (i = 0; i < OTHER_CTLRS; i++) {
if (strcmp(other_ctlrs[i], ctlr) == 0) {
datafile_error(NULL, NULL);
return;
}
}
if (i == OTHER_CTLRS) {
datafile_error(
"Unknown controller '%s'", ctlr);
return;
}
}
ctype = mlp->ctlr_type;
for (dtype = ctype->ctype_dlist; dtype != NULL;
dtype = dtype->dtype_next) {
if (strcmp(dtype->dtype_asciilabel, disk) == 0)
break;
}
if (dtype == NULL) {
datafile_error("Unknown disk '%s'", disk);
return;
}
nhead = dtype->dtype_nhead;
nsect = dtype->dtype_nsect;
acyl = dtype->dtype_acyl;
ncyl = dtype->dtype_ncyl;
}
if (dtype == NULL || ctype == NULL) {
datafile_error("Incomplete specification", "");
return;
}
if (strlen(ident) != 1) {
datafile_error("Unknown keyword '%s'", ident);
return;
}
if (ident[0] < PARTITION_BASE || ident[0] > PARTITION_BASE+9) {
datafile_error("Unknown keyword '%s'", ident);
return;
}
index = ident[0] - PARTITION_BASE;
vtoc_tag = pinfo->vtoc.v_part[index].p_tag;
vtoc_flag = pinfo->vtoc.v_part[index].p_flag;
if (find_value(ptag_choices, cleaned, &i) == 1) {
vtoc_tag = (ushort_t)i;
status = sup_gettoken(token);
if (status != SUP_COMMA) {
datafile_error(
"Expecting ', ', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'",
token);
return;
}
clean_token(cleaned, token);
}
if (find_value(pflag_choices, cleaned, &i) == 1) {
vtoc_flag = (ushort_t)i;
status = sup_gettoken(token);
if (status != SUP_COMMA) {
datafile_error("Expecting ', ', found '%s'",
token);
return;
}
status = sup_gettoken(token);
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'",
token);
return;
}
clean_token(cleaned, token);
}
val1 = str2cyls(cleaned);
if (val1 == (uint_t)(-1)) {
datafile_error("Expecting an integer, found '%s'",
cleaned);
return;
}
status = sup_gettoken(token);
if (status != SUP_COMMA) {
datafile_error("Expecting ', ', found '%s'", token);
return;
}
status = sup_gettoken(token);
if (status != SUP_STRING) {
datafile_error("Expecting value, found '%s'", token);
return;
}
clean_token(cleaned, token);
val2 = str2blks(cleaned);
if (val2 == (uint_t)(-1)) {
datafile_error("Expecting an integer, found '%s'",
cleaned);
return;
}
pinfo->pinfo_map[index].dkl_cylno = val1;
pinfo->pinfo_map[index].dkl_nblk = val2;
pinfo->vtoc.v_part[index].p_tag = vtoc_tag;
pinfo->vtoc.v_part[index].p_flag = vtoc_flag;
#if defined(_SUNOS_VTOC_16)
pinfo->vtoc.v_part[index].p_start = val1 * (nhead * nsect);
pinfo->vtoc.v_part[index].p_size = val2;
if (val2 == 0) {
pinfo->vtoc.v_part[index].p_tag = 0;
pinfo->vtoc.v_part[index].p_flag = 0;
pinfo->vtoc.v_part[index].p_start = 0;
pinfo->pinfo_map[index].dkl_cylno = 0;
}
#endif
}
if ((flags & SUP_MIN_PART) != SUP_MIN_PART) {
datafile_error("Incomplete specification", "");
return;
}
parts = dtype->dtype_plist;
if (parts == NULL)
dtype->dtype_plist = pinfo;
else {
while (parts->pinfo_next != NULL)
parts = parts->pinfo_next;
parts->pinfo_next = pinfo;
}
}
int
open_disk(char *diskname, int flags)
{
return (open(diskname, flags));
}
void
do_search(char *arglist[])
{
char **sp;
DIR *dir;
struct dirent *dp;
char s[MAXPATHLEN];
char path[MAXPATHLEN];
char curdir[MAXPATHLEN];
char *directory = "/dev/rdsk";
struct disk_info *disk;
int i;
if (getcwd(curdir, sizeof (curdir)) == NULL) {
err_print("Cannot get current directory - %s\n",
strerror(errno));
fullabort();
}
if (chdir(directory) == -1) {
err_print("Cannot set directory to %s - %s\n",
directory, strerror(errno));
fullabort();
}
if (arglist != NULL) {
check_for_duplicate_disknames(arglist);
for (; *arglist != NULL; arglist++) {
search_for_logical_dev(*arglist);
}
} else {
fmt_print("Searching for disks...");
(void) fflush(stdout);
need_newline = 1;
sp = search_path;
if (sp != NULL) {
while (*sp != NULL) {
search_for_logical_dev(*sp++);
}
}
if ((dir = opendir(".")) == NULL) {
err_print("Cannot open %s - %s\n",
directory, strerror(errno));
fullabort();
}
while ((dp = readdir(dir)) != NULL) {
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0)
continue;
if (!conventional_name(dp->d_name)) {
if (!fdisk_physical_name(dp->d_name)) {
if (!name_represents_wholedisk
(dp->d_name)) {
(void) strcpy(path, directory);
(void) strcat(path, "/");
(void) strcat(path, dp->d_name);
add_device_to_disklist(
dp->d_name, path);
}
}
}
}
rewinddir(dir);
while ((dp = readdir(dir)) != NULL) {
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0)
continue;
if (whole_disk_name(dp->d_name)) {
(void) strcpy(path, directory);
(void) strcat(path, "/");
(void) strcat(path, dp->d_name);
canonicalize_name(s, dp->d_name);
add_device_to_disklist(s, path);
}
}
if (closedir(dir) == -1) {
err_print("Cannot close directory %s - %s\n",
directory, strerror(errno));
fullabort();
}
need_newline = 0;
fmt_print("done\n");
}
if (chdir(curdir) == -1) {
err_print("Cannot set directory to %s - %s\n",
curdir, strerror(errno));
fullabort();
}
if (disk_list == NULL) {
if (geteuid() == 0) {
err_print("No disks found!\n");
} else {
err_print("No permission (or no disks found)!\n");
}
(void) fflush(stdout);
fullabort();
}
sort_disk_list();
i = 0;
for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
float scaled;
diskaddr_t nblks;
struct disk_type *type;
if (disk->disk_flags & DSK_AUTO_CONFIG) {
if (i++ == 0) {
fmt_print("\n");
}
fmt_print("%s: ", disk->disk_name);
if (disk->disk_flags & DSK_LABEL_DIRTY) {
fmt_print("configured ");
} else {
fmt_print("configured and labeled ");
}
type = disk->disk_type;
nblks = type->dtype_ncyl * type->dtype_nhead *
type->dtype_nsect;
if (disk->label_type == L_TYPE_SOLARIS)
scaled = bn2mb(nblks);
else
scaled = bn2mb(type->capacity);
fmt_print("with capacity of ");
if (scaled > 1024.0) {
fmt_print("%1.2fGB\n", scaled/1024.0);
} else {
fmt_print("%1.2fMB\n", scaled);
}
}
}
}
static void
search_for_logical_dev(char *devname)
{
char path[MAXPATHLEN];
char *directory = "/dev/rdsk/";
char *partition = "s2";
if (*devname == '/') {
(void) strcpy(path, devname);
} else if (canonical_name(devname)) {
(void) strcpy(path, directory);
(void) strcat(path, devname);
(void) strcat(path, partition);
} else if (canonical4x_name(devname)) {
(void) strcpy(path, "/dev/r");
(void) strcat(path, devname);
(void) strcat(path, "c");
} else {
(void) strcpy(path, directory);
(void) strcat(path, devname);
}
add_device_to_disklist(devname, path);
}
static void
get_disk_name(int fd, char *disk_name, struct disk_info *disk_info)
{
struct scsi_inquiry inquiry;
char *vid, *pid, *rid;
if (get_disk_inquiry_prop(disk_info->devfs_name, &vid, &pid, &rid)
== 0) {
(void) snprintf(disk_name, MAXNAMELEN - 1, "%s-%s-%s",
vid, pid, rid);
free(vid);
free(pid);
free(rid);
return;
}
if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
if (option_msg)
err_print("\nInquiry failed - %s\n", strerror(errno));
(void) strcpy(disk_name, "Unknown-Unknown-0001");
return;
}
(void) get_generic_disk_name(disk_name, &inquiry);
}
static void
add_device_to_disklist(char *devname, char *devpath)
{
struct disk_info *search_disk;
struct ctlr_info *search_ctlr;
struct disk_type *search_dtype, *efi_disk;
struct partition_info *search_parts;
struct disk_info *dptr;
struct ctlr_info *cptr;
struct disk_type *type;
struct partition_info *parts;
struct dk_label search_label;
struct dk_cinfo dkinfo;
struct stat stbuf;
struct ctlr_type *ctlr, *tctlr;
struct mctlr_list *mlp;
struct efi_info efi_info;
struct dk_minfo mediainfo;
int search_file;
int status;
int i;
int access_flags = 0;
char disk_name[MAXNAMELEN];
if ((search_file = open_disk(devpath, O_RDWR | O_NDELAY)) < 0) {
return;
}
if (fstat(search_file, &stbuf) == -1 || !S_ISCHR(stbuf.st_mode)) {
(void) close(search_file);
return;
}
if (ioctl(search_file, DKIOCINFO, &dkinfo) < 0) {
(void) close(search_file);
return;
}
if (!expert_mode) {
int isremovable, ret;
ret = ioctl(search_file, DKIOCREMOVABLE, &isremovable);
if ((ret >= 0) && (isremovable != 0)) {
(void) close(search_file);
return;
}
}
if (ioctl(search_file, DKIOCGMEDIAINFO, &mediainfo) == -1) {
cur_blksz = DEV_BSIZE;
} else {
cur_blksz = mediainfo.dki_lbsize;
}
mlp = controlp;
while (mlp != NULL) {
if (mlp->ctlr_type->ctype_ctype == dkinfo.dki_ctype) {
break;
}
mlp = mlp->next;
}
if (mlp == NULL) {
if (dkinfo.dki_ctype == DKC_CDROM) {
if (ioctl(search_file, DKIOCGMEDIAINFO,
&mediainfo) < 0) {
mediainfo.dki_media_type = DK_UNKNOWN;
}
}
if ((dkinfo.dki_ctype == DKC_CDROM) &&
(mediainfo.dki_media_type != DK_REMOVABLE_DISK)) {
(void) close(search_file);
return;
}
tctlr = zalloc(sizeof (struct ctlr_type));
tctlr->ctype_ctype = dkinfo.dki_ctype;
tctlr->ctype_name = zalloc(DK_DEVLEN);
if (strlcpy(tctlr->ctype_name, dkinfo.dki_cname,
DK_DEVLEN) > DK_DEVLEN) {
err_print("\nError: Device %s: controller "
"name (%s)\nis invalid. Device will not "
"be displayed.\n", devname, dkinfo.dki_cname);
(void) close(search_file);
destroy_data(tctlr->ctype_name);
destroy_data((char *)tctlr);
return;
} else {
tctlr->ctype_ops = zalloc(sizeof (struct ctlr_ops));
*(tctlr->ctype_ops) = genericops;
tctlr->ctype_flags = CF_WLIST;
mlp = controlp;
while (mlp->next != NULL) {
mlp = mlp->next;
}
mlp->next = zalloc(sizeof (struct mctlr_list));
mlp->next->ctlr_type = tctlr;
}
}
if (disk_is_known(&dkinfo)) {
(void) close(search_file);
return;
}
#if defined(sparc)
if (dkinfo.dki_ctype == DKC_SCSI_CCS) {
char *first_sector;
first_sector = zalloc(cur_blksz);
i = scsi_rdwr(DIR_READ, search_file, (diskaddr_t)0,
1, first_sector, F_SILENT, NULL);
switch (i) {
case DSK_RESERVED:
access_flags |= DSK_RESERVED;
break;
case DSK_UNAVAILABLE:
access_flags |= DSK_UNAVAILABLE;
break;
default:
break;
}
free(first_sector);
}
#endif
search_disk = (struct disk_info *)zalloc(sizeof (struct disk_info));
if (disk_list == NULL)
disk_list = search_disk;
else {
for (dptr = disk_list; dptr->disk_next != NULL;
dptr = dptr->disk_next)
;
dptr->disk_next = search_disk;
}
search_disk->disk_dkinfo = dkinfo;
if (is_efi_type(search_file)) {
search_disk->label_type = L_TYPE_EFI;
} else {
search_disk->label_type = L_TYPE_SOLARIS;
}
search_disk->disk_name = alloc_string(devname);
search_disk->disk_path = alloc_string(devpath);
search_disk->disk_lbasize = cur_blksz;
(void) strcpy(x86_devname, devname);
search_disk->devfs_name = get_physical_name(devpath);
for (search_ctlr = ctlr_list; search_ctlr != NULL;
search_ctlr = search_ctlr->ctlr_next)
if (search_ctlr->ctlr_addr == dkinfo.dki_addr &&
search_ctlr->ctlr_space == dkinfo.dki_space &&
search_ctlr->ctlr_ctype->ctype_ctype ==
dkinfo.dki_ctype)
break;
if (search_ctlr == NULL) {
mlp = controlp;
while (mlp != NULL) {
if (mlp->ctlr_type->ctype_ctype == dkinfo.dki_ctype)
break;
mlp = mlp->next;
}
if (mlp == NULL) {
err_print("\nError: found disk attached to ");
err_print("unsupported controller type '%d'.\n",
dkinfo.dki_ctype);
(void) close(search_file);
return;
}
search_ctlr = (struct ctlr_info *)
zalloc(sizeof (struct ctlr_info));
search_ctlr->ctlr_ctype = mlp->ctlr_type;
if (ctlr_list == NULL)
ctlr_list = search_ctlr;
else {
for (cptr = ctlr_list; cptr->ctlr_next != NULL;
cptr = cptr->ctlr_next)
;
cptr->ctlr_next = search_ctlr;
}
for (i = 0; i < DK_DEVLEN; i++) {
search_ctlr->ctlr_cname[i] = dkinfo.dki_cname[i];
search_ctlr->ctlr_dname[i] = dkinfo.dki_dname[i];
}
search_ctlr->ctlr_cname[i] = 0;
search_ctlr->ctlr_dname[i] = 0;
search_ctlr->ctlr_flags = dkinfo.dki_flags;
search_ctlr->ctlr_num = dkinfo.dki_cnum;
search_ctlr->ctlr_addr = dkinfo.dki_addr;
search_ctlr->ctlr_space = dkinfo.dki_space;
search_ctlr->ctlr_prio = dkinfo.dki_prio;
search_ctlr->ctlr_vec = dkinfo.dki_vec;
}
search_disk->disk_ctlr = search_ctlr;
if (access_flags & (DSK_RESERVED | DSK_UNAVAILABLE)) {
if (access_flags & DSK_RESERVED)
search_disk->disk_flags |= DSK_RESERVED;
else
search_disk->disk_flags |= DSK_UNAVAILABLE;
(void) close(search_file);
return;
} else {
search_disk->disk_flags &= ~(DSK_RESERVED | DSK_UNAVAILABLE);
}
if (search_disk->label_type == L_TYPE_SOLARIS) {
status = read_label(search_file, &search_label);
} else {
status = read_efi_label(search_file, &efi_info, search_disk);
}
ctlr = search_ctlr->ctlr_ctype;
if ((status == -1) &&
(ctlr->ctype_ctype == DKC_SCSI_CCS ||
ctlr->ctype_ctype == DKC_BLKDEV)) {
if (option_msg && diag_msg) {
err_print("%s: attempting auto configuration\n",
search_disk->disk_name);
}
switch (search_disk->label_type) {
case (L_TYPE_SOLARIS):
if (auto_sense(search_file, 0, &search_label) != NULL) {
status = 0;
search_disk->disk_flags |=
(DSK_LABEL_DIRTY | DSK_AUTO_CONFIG);
break;
}
case (L_TYPE_EFI):
efi_disk = auto_efi_sense(search_file, &efi_info);
if (efi_disk != NULL) {
search_disk->label_type = L_TYPE_EFI;
status = 0;
search_disk->disk_flags |=
(DSK_LABEL_DIRTY | DSK_AUTO_CONFIG);
}
break;
default:
break;
}
}
if (status == -1) {
(void) close(search_file);
return;
}
if (search_disk->label_type == L_TYPE_SOLARIS) {
if (!checklabel(&search_label)) {
(void) close(search_file);
return;
}
if (trim_id(search_label.dkl_asciilabel)) {
(void) close(search_file);
return;
}
}
search_disk->disk_flags |= DSK_LABEL;
if (search_disk->label_type == L_TYPE_EFI) {
search_dtype = (struct disk_type *)
zalloc(sizeof (struct disk_type));
type = search_ctlr->ctlr_ctype->ctype_dlist;
if (type == NULL) {
search_ctlr->ctlr_ctype->ctype_dlist =
search_dtype;
} else {
while (type->dtype_next != NULL) {
type = type->dtype_next;
}
type->dtype_next = search_dtype;
}
search_dtype->dtype_next = NULL;
search_dtype->vendor = strdup(efi_info.vendor);
search_dtype->product = strdup(efi_info.product);
search_dtype->revision = strdup(efi_info.revision);
if (search_dtype->vendor == NULL ||
search_dtype->product == NULL ||
search_dtype->revision == NULL) {
free(search_dtype->vendor);
free(search_dtype->product);
free(search_dtype->revision);
free(search_dtype);
goto out;
}
search_dtype->capacity = efi_info.capacity;
search_disk->disk_type = search_dtype;
search_parts = (struct partition_info *)
zalloc(sizeof (struct partition_info));
search_dtype->dtype_plist = search_parts;
search_parts->pinfo_name = alloc_string("original");
search_parts->pinfo_next = NULL;
search_parts->etoc = efi_info.e_parts;
search_disk->disk_parts = search_parts;
for (i = 0; i < search_parts->etoc->efi_nparts; i++) {
if (search_parts->etoc->efi_parts[i].p_tag ==
V_RESERVED) {
bcopy(search_parts->etoc->efi_parts[i].p_name,
search_disk->v_volume, LEN_DKL_VVOL);
break;
}
}
out:
(void) close(search_file);
free(efi_info.vendor);
free(efi_info.product);
free(efi_info.revision);
return;
}
for (search_dtype = search_ctlr->ctlr_ctype->ctype_dlist;
search_dtype != NULL;
search_dtype = search_dtype->dtype_next)
if (dtype_match(&search_label, search_dtype))
break;
if (search_dtype == NULL) {
search_dtype = (struct disk_type *)
zalloc(sizeof (struct disk_type));
type = search_ctlr->ctlr_ctype->ctype_dlist;
if (type == NULL)
search_ctlr->ctlr_ctype->ctype_dlist =
search_dtype;
else {
while (type->dtype_next != NULL)
type = type->dtype_next;
type->dtype_next = search_dtype;
}
search_dtype->dtype_next = NULL;
if (strncmp(search_label.dkl_asciilabel, "DEFAULT",
strlen("DEFAULT")) == 0) {
(void) get_disk_name(search_file, disk_name,
search_disk);
search_dtype->dtype_asciilabel = (char *)
zalloc(strlen(disk_name) + 1);
(void) strcpy(search_dtype->dtype_asciilabel,
disk_name);
} else {
search_dtype->dtype_asciilabel = (char *)
zalloc(strlen(search_label.dkl_asciilabel) + 1);
(void) strcpy(search_dtype->dtype_asciilabel,
search_label.dkl_asciilabel);
}
search_dtype->dtype_pcyl = search_label.dkl_pcyl;
search_dtype->dtype_ncyl = search_label.dkl_ncyl;
search_dtype->dtype_acyl = search_label.dkl_acyl;
search_dtype->dtype_nhead = search_label.dkl_nhead;
search_dtype->dtype_nsect = search_label.dkl_nsect;
search_dtype->dtype_rpm = search_label.dkl_rpm;
if ((search_ctlr->ctlr_ctype->ctype_ctype != DKC_SCSI_CCS) &&
(search_ctlr->ctlr_ctype->ctype_ctype != DKC_DIRECT) &&
(search_ctlr->ctlr_ctype->ctype_ctype != DKC_VBD) &&
(search_ctlr->ctlr_ctype->ctype_ctype != DKC_PCMCIA_ATA) &&
(search_ctlr->ctlr_ctype->ctype_ctype != DKC_BLKDEV)) {
search_dtype->dtype_flags |= DT_NEED_SPEFS;
}
}
search_disk->disk_type = search_dtype;
(void) close(search_file);
for (search_parts = search_dtype->dtype_plist;
search_parts != NULL;
search_parts = search_parts->pinfo_next)
if (parts_match(&search_label, search_parts)) {
break;
}
if (search_parts == NULL) {
search_parts = (struct partition_info *)
zalloc(sizeof (struct partition_info));
parts = search_dtype->dtype_plist;
if (parts == NULL)
search_dtype->dtype_plist = search_parts;
else {
while (parts->pinfo_next != NULL)
parts = parts->pinfo_next;
parts->pinfo_next = search_parts;
}
search_parts->pinfo_next = NULL;
search_parts->pinfo_name = alloc_string("original");
for (i = 0; i < NDKMAP; i++) {
#if defined(_SUNOS_VTOC_8)
search_parts->pinfo_map[i] =
search_label.dkl_map[i];
#elif defined(_SUNOS_VTOC_16)
search_parts->pinfo_map[i].dkl_cylno =
search_label.dkl_vtoc.v_part[i].p_start /
((blkaddr32_t)(search_label.dkl_nhead *
search_label.dkl_nsect));
search_parts->pinfo_map[i].dkl_nblk =
search_label.dkl_vtoc.v_part[i].p_size;
#else
#error No VTOC format defined.
#endif
}
}
if (search_label.dkl_vtoc.v_version == V_VERSION) {
bcopy(search_label.dkl_vtoc.v_volume,
search_disk->v_volume, LEN_DKL_VVOL);
search_parts->vtoc = search_label.dkl_vtoc;
} else {
bzero(search_disk->v_volume, LEN_DKL_VVOL);
set_vtoc_defaults(search_parts);
}
search_disk->disk_parts = search_parts;
}
static int
disk_is_known(struct dk_cinfo *dkinfo)
{
struct disk_info *dp;
dp = disk_list;
while (dp != NULL) {
if (dp->disk_dkinfo.dki_ctype == dkinfo->dki_ctype &&
dp->disk_dkinfo.dki_cnum == dkinfo->dki_cnum &&
dp->disk_dkinfo.dki_unit == dkinfo->dki_unit &&
strcmp(dp->disk_dkinfo.dki_dname, dkinfo->dki_dname) == 0) {
return (1);
}
dp = dp->disk_next;
}
return (0);
}
int
dtype_match(struct dk_label *label, struct disk_type *dtype)
{
if (dtype->dtype_asciilabel == NULL) {
return (0);
}
if ((strcmp(label->dkl_asciilabel, dtype->dtype_asciilabel) != 0) ||
(label->dkl_ncyl != dtype->dtype_ncyl) ||
(label->dkl_acyl != dtype->dtype_acyl) ||
(label->dkl_nhead != dtype->dtype_nhead) ||
(label->dkl_nsect != dtype->dtype_nsect)) {
return (0);
}
return (1);
}
int
parts_match(struct dk_label *label, struct partition_info *pinfo)
{
int i;
for (i = 0; i < NDKMAP; i++)
#if defined(_SUNOS_VTOC_8)
if ((label->dkl_map[i].dkl_cylno !=
pinfo->pinfo_map[i].dkl_cylno) ||
(label->dkl_map[i].dkl_nblk !=
pinfo->pinfo_map[i].dkl_nblk))
#elif defined(_SUNOS_VTOC_16)
if ((pinfo->pinfo_map[i].dkl_cylno !=
label->dkl_vtoc.v_part[i].p_start /
(label->dkl_nhead * label->dkl_nsect)) ||
(pinfo->pinfo_map[i].dkl_nblk !=
label->dkl_vtoc.v_part[i].p_size))
#else
#error No VTOC format defined.
#endif
return (0);
if (label->dkl_vtoc.v_version != pinfo->vtoc.v_version)
return (0);
if (label->dkl_vtoc.v_nparts != pinfo->vtoc.v_nparts)
return (0);
for (i = 0; i < NDKMAP; i++) {
if (label->dkl_vtoc.v_part[i].p_tag !=
pinfo->vtoc.v_part[i].p_tag)
return (0);
if (label->dkl_vtoc.v_part[i].p_flag !=
pinfo->vtoc.v_part[i].p_flag)
return (0);
}
return (1);
}
int
diskname_match(char *name, struct disk_info *disk)
{
struct dk_cinfo dkinfo;
char s[MAXPATHLEN];
int fd;
if (strcmp(name, disk->disk_name) == 0) {
return (1);
}
if (canonical4x_name(name) == 0) {
return (0);
}
(void) strcpy(s, "/dev/r");
(void) strcat(s, name);
(void) strcat(s, "c");
if ((fd = open_disk(s, O_RDWR | O_NDELAY)) < 0) {
return (0);
}
if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
(void) close(fd);
return (0);
}
(void) close(fd);
if (disk->disk_dkinfo.dki_ctype == dkinfo.dki_ctype &&
disk->disk_dkinfo.dki_cnum == dkinfo.dki_cnum &&
disk->disk_dkinfo.dki_unit == dkinfo.dki_unit &&
strcmp(disk->disk_dkinfo.dki_dname, dkinfo.dki_dname) == 0) {
return (1);
}
return (0);
}
static void
datafile_error(char *errmsg, char *token)
{
int token_type;
TOKEN token_buf;
if (errmsg != NULL) {
err_print(errmsg, token);
err_print(" - %s (%d)\n", file_name, data_lineno);
}
if (last_token_type != SUP_EOF && last_token_type != SUP_EOL) {
do {
token_type = sup_gettoken(token_buf);
} while (token_type != SUP_EOF && token_type != SUP_EOL);
if (token_type == SUP_EOF) {
sup_pushtoken(token_buf, token_type);
}
}
}
static void
search_duplicate_dtypes(void)
{
struct disk_type *dp1;
struct disk_type *dp2;
struct mctlr_list *mlp;
mlp = controlp;
while (mlp != NULL) {
dp1 = mlp->ctlr_type->ctype_dlist;
while (dp1 != NULL) {
dp2 = dp1->dtype_next;
while (dp2 != NULL) {
check_dtypes_for_inconsistency(dp1, dp2);
dp2 = dp2->dtype_next;
}
dp1 = dp1->dtype_next;
}
mlp = mlp->next;
}
}
static void
search_duplicate_pinfo(void)
{
struct disk_type *dp;
struct partition_info *pp1;
struct partition_info *pp2;
struct mctlr_list *mlp;
mlp = controlp;
while (mlp != NULL) {
dp = mlp->ctlr_type->ctype_dlist;
while (dp != NULL) {
pp1 = dp->dtype_plist;
while (pp1 != NULL) {
pp2 = pp1->pinfo_next;
while (pp2 != NULL) {
check_pinfo_for_inconsistency(pp1, pp2);
pp2 = pp2->pinfo_next;
}
pp1 = pp1->pinfo_next;
}
dp = dp->dtype_next;
}
mlp = mlp->next;
}
}
static void
check_dtypes_for_inconsistency(struct disk_type *dp1, struct disk_type *dp2)
{
int i;
int result;
struct chg_list *cp1;
struct chg_list *cp2;
if (strcmp(dp1->dtype_asciilabel, dp2->dtype_asciilabel) != 0) {
return;
}
result = 0;
result |= (dp1->dtype_flags != dp2->dtype_flags);
result |= (dp1->dtype_options != dp2->dtype_options);
result |= (dp1->dtype_fmt_time != dp2->dtype_fmt_time);
result |= (dp1->dtype_bpt != dp2->dtype_bpt);
result |= (dp1->dtype_ncyl != dp2->dtype_ncyl);
result |= (dp1->dtype_acyl != dp2->dtype_acyl);
result |= (dp1->dtype_pcyl != dp2->dtype_pcyl);
result |= (dp1->dtype_nhead != dp2->dtype_nhead);
result |= (dp1->dtype_nsect != dp2->dtype_nsect);
result |= (dp1->dtype_rpm != dp2->dtype_rpm);
result |= (dp1->dtype_cyl_skew != dp2->dtype_cyl_skew);
result |= (dp1->dtype_trk_skew != dp2->dtype_trk_skew);
result |= (dp1->dtype_trks_zone != dp2->dtype_trks_zone);
result |= (dp1->dtype_atrks != dp2->dtype_atrks);
result |= (dp1->dtype_asect != dp2->dtype_asect);
result |= (dp1->dtype_cache != dp2->dtype_cache);
result |= (dp1->dtype_threshold != dp2->dtype_threshold);
result |= (dp1->dtype_read_retries != dp2->dtype_read_retries);
result |= (dp1->dtype_write_retries != dp2->dtype_write_retries);
result |= (dp1->dtype_prefetch_min != dp2->dtype_prefetch_min);
result |= (dp1->dtype_prefetch_max != dp2->dtype_prefetch_max);
for (i = 0; i < NSPECIFICS; i++) {
result |= (dp1->dtype_specifics[i] != dp2->dtype_specifics[i]);
}
cp1 = dp1->dtype_chglist;
cp2 = dp2->dtype_chglist;
while (cp1 != NULL && cp2 != NULL) {
if (cp1 == NULL || cp2 == NULL) {
result = 1;
break;
}
result |= (cp1->pageno != cp2->pageno);
result |= (cp1->byteno != cp2->byteno);
result |= (cp1->mode != cp2->mode);
result |= (cp1->value != cp2->value);
cp1 = cp1->next;
cp2 = cp2->next;
}
if (result) {
err_print("Inconsistent definitions for disk type '%s'\n",
dp1->dtype_asciilabel);
if (dp1->dtype_filename != NULL &&
dp2->dtype_filename != NULL) {
err_print("%s (%d) - %s (%d)\n",
dp1->dtype_filename, dp1->dtype_lineno,
dp2->dtype_filename, dp2->dtype_lineno);
}
fullabort();
}
}
static void
check_pinfo_for_inconsistency(struct partition_info *pp1,
struct partition_info *pp2)
{
int i;
int result;
struct dk_map32 *map1;
struct dk_map32 *map2;
#if defined(_SUNOS_VTOC_8)
struct dk_map2 *vp1;
struct dk_map2 *vp2;
#elif defined(_SUNOS_VTOC_16)
struct dkl_partition *vp1;
struct dkl_partition *vp2;
#else
#error No VTOC layout defined.
#endif
if (strcmp(pp1->pinfo_name, pp2->pinfo_name) != 0) {
return;
}
result = 0;
map1 = pp1->pinfo_map;
map2 = pp2->pinfo_map;
for (i = 0; i < NDKMAP; i++, map1++, map2++) {
result |= (map1->dkl_cylno != map2->dkl_cylno);
result |= (map1->dkl_nblk != map2->dkl_nblk);
}
vp1 = pp1->vtoc.v_part;
vp2 = pp2->vtoc.v_part;
for (i = 0; i < NDKMAP; i++, vp1++, vp2++) {
result |= (vp1->p_tag != vp2->p_tag);
result |= (vp1->p_flag != vp2->p_flag);
}
if (result) {
err_print("Inconsistent definitions for partition type '%s'\n",
pp1->pinfo_name);
if (pp1->pinfo_filename != NULL &&
pp2->pinfo_filename != NULL) {
err_print("%s (%d) - %s (%d)\n",
pp1->pinfo_filename, pp1->pinfo_lineno,
pp2->pinfo_filename, pp2->pinfo_lineno);
}
fullabort();
}
}
static uint_t
str2blks(char *str)
{
int blks;
char *p;
blks = (int)strtol(str, &p, 0);
if (*p != 0) {
if (*p == 'c') {
p++;
blks = blks * spc();
} else if (*p == 'b') {
p++;
}
if (*p != 0) {
blks = -1;
}
}
return (blks);
}
int
str2cyls(char *str)
{
int cyls;
char *p;
cyls = (int)strtol(str, &p, 0);
if (*p != 0) {
if (*p == 'c') {
p++;
}
if (*p != 0) {
cyls = -1;
}
}
return (cyls);
}
static struct chg_list *
new_chg_list(struct disk_type *disk)
{
struct chg_list *cp;
struct chg_list *nc;
nc = zalloc(sizeof (struct chg_list));
if (disk->dtype_chglist == NULL) {
disk->dtype_chglist = nc;
} else {
for (cp = disk->dtype_chglist; cp->next; cp = cp->next)
;
cp->next = nc;
}
nc->next = NULL;
return (nc);
}
static char *
get_physical_name(char *path)
{
struct stat stbuf;
int i;
int level;
char *p;
char s[MAXPATHLEN];
char buf[MAXPATHLEN];
char dir[MAXPATHLEN];
char savedir[MAXPATHLEN];
char *result = NULL;
if (getcwd(savedir, sizeof (savedir)) == NULL) {
err_print("getcwd() failed - %s\n", strerror(errno));
return (NULL);
}
(void) strcpy(s, path);
if ((p = strrchr(s, '/')) != NULL) {
*p = 0;
}
if (s[0] == 0) {
(void) strcpy(s, "/");
}
if (chdir(s) == -1) {
err_print("cannot chdir() to %s - %s\n",
s, strerror(errno));
goto exit;
}
level = 0;
(void) strcpy(s, path);
for (;;) {
if (stat(s, &stbuf) == -1) {
goto exit;
}
if (lstat(s, &stbuf) == -1) {
err_print("%s: lstat() failed - %s\n",
s, strerror(errno));
goto exit;
}
if (!S_ISLNK(stbuf.st_mode)) {
if (level > 0) {
if ((p = strrchr(s, ':')) != NULL) {
*p = 0;
}
if (getcwd(dir, sizeof (dir)) == NULL) {
err_print("getcwd() failed - %s\n",
strerror(errno));
goto exit;
}
(void) strcat(dir, "/");
(void) strcat(dir, s);
p = (strstr(dir, DEVFS_PREFIX) == dir) ?
dir+strlen(DEVFS_PREFIX) : dir;
result = alloc_string(p);
}
goto exit;
}
i = readlink(s, buf, sizeof (buf));
if (i == -1) {
err_print("%s: readlink() failed - %s\n",
s, strerror(errno));
goto exit;
}
level++;
buf[i] = 0;
(void) strcpy(dir, buf);
if ((p = strrchr(dir, '/')) != NULL) {
*p = 0;
if (chdir(dir) == -1) {
err_print("cannot chdir() to %s - %s\n",
dir, strerror(errno));
goto exit;
}
(void) strcpy(s, p+1);
} else {
(void) strcpy(s, buf);
}
}
exit:
if (chdir(savedir) == -1) {
err_print("cannot chdir() to %s - %s\n",
savedir, strerror(errno));
}
return (result);
}
static void
sort_disk_list(void)
{
int n;
struct disk_info **disks;
struct disk_info *d;
struct disk_info **dp;
struct disk_info **dp2;
n = 0;
for (d = disk_list; d != NULL; d = d->disk_next) {
n++;
}
if (n == 0) {
return;
}
disks = (struct disk_info **)
zalloc((n+1) * sizeof (struct disk_info *));
dp = disks;
for (d = disk_list; d != NULL; d = d->disk_next) {
*dp++ = d;
}
*dp = NULL;
qsort((void *) disks, n, sizeof (struct disk_info *),
disk_name_compare);
dp = disks;
disk_list = *dp;
dp2 = dp + 1;
do {
(*dp++)->disk_next = *dp2++;
} while (*dp != NULL);
(void) destroy_data((void *)disks);
}
static int
disk_name_compare(
const void *arg1,
const void *arg2)
{
char *s1;
char *s2;
int n1;
int n2;
char *p1;
char *p2;
s1 = (*((struct disk_info **)arg1))->disk_name;
s2 = (*((struct disk_info **)arg2))->disk_name;
for (;;) {
if (*s1 == 0 || *s2 == 0)
break;
if (isdigit(*s1) && isdigit(*s2)) {
n1 = strtol(s1, &p1, 10);
n2 = strtol(s2, &p2, 10);
if (n1 != n2) {
return (n1 - n2);
}
s1 = p1;
s2 = p2;
} else if (*s1 != *s2) {
break;
} else {
s1++;
s2++;
}
}
return (*s1 - *s2);
}
static void
make_controller_list(void)
{
int x;
struct mctlr_list *ctlrp;
ctlrp = controlp;
for (x = nctypes; x != 0; x--) {
ctlrp = zalloc(sizeof (struct mctlr_list));
ctlrp->next = controlp;
ctlrp->ctlr_type = &ctlr_types[x - 1];
controlp = ctlrp;
}
}
static void
check_for_duplicate_disknames(char *arglist[])
{
char *directory = "/dev/rdsk/";
char **disklist;
int len;
char s[MAXPATHLEN], t[MAXPATHLEN];
int diskno = 0;
int i;
len = strlen(directory);
disklist = arglist;
for (; *disklist != NULL; disklist++) {
if (strncmp(directory, *disklist, len) == 0) {
canonicalize_name(s, *disklist);
for (i = 0; i < diskno; i++) {
canonicalize_name(t, arglist[i]);
if (strncmp(s, t, strlen(t)) == 0)
break;
}
if (i != diskno)
continue;
}
(void) strcpy(arglist[diskno], *disklist);
diskno++;
}
arglist[diskno] = NULL;
}
#define DISK_PREFIX "/dev/rdsk/"
static int
name_represents_wholedisk(char *name)
{
char symname[MAXPATHLEN];
char localname[MAXPATHLEN];
char *nameptr;
ssize_t symname_size;
if (strlcpy(localname, name, MAXPATHLEN) >= MAXPATHLEN)
return (1);
while ((symname_size = readlink(
localname, symname, MAXPATHLEN - 1)) != -1) {
symname[symname_size] = '\0';
nameptr = symname;
if (strncmp(symname, DISK_PREFIX,
(sizeof (DISK_PREFIX) - 1)) == 0)
nameptr += (sizeof (DISK_PREFIX) - 1);
if (conventional_name(nameptr)) {
if (whole_disk_name(nameptr))
return (0);
else
return (1);
}
(void) strcpy(localname, symname);
}
return (0);
}