#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/dkio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <libintl.h>
#include <limits.h>
#include <dbus/dbus.h>
#include <hal/libhal.h>
#include "transport.h"
#include "mmc.h"
#include "device.h"
#include "util.h"
#include "msgs.h"
#include "misc_scsi.h"
#include "toshiba.h"
#include "main.h"
static int
is_old_sun_drive(cd_device *dev)
{
if (strncmp("SONY", (const char *)&dev->d_inq[8], 4) == 0) {
if (strncmp("CDU 561", (const char *)&dev->d_inq[16], 7) == 0)
return (1);
if (strncmp("CDU-8012", (const char *)&dev->d_inq[16], 8) == 0)
return (1);
}
if ((strncmp("TOSHIBA", (const char *)&dev->d_inq[8], 7) == 0) &&
(strncmp("XM", (const char *)&dev->d_inq[16], 2) == 0)) {
char product_id[17];
if (strncmp("SUN32XCD", (const char *)&dev->d_inq[24], 8) == 0)
dev->d_cap |= DEV_CAP_SETTING_SPEED_NOT_ALLOWED;
(void) strncpy(product_id, (const char *)&dev->d_inq[16], 16);
product_id[16] = 0;
if (strstr(product_id, "SUN") != NULL)
return (1);
}
return (0);
}
cd_device *
get_device(char *user_supplied, char *node)
{
cd_device *dev;
int fd;
uchar_t *cap;
char devnode[PATH_MAX];
int size;
struct dk_minfo mediainfo;
int use_cd_speed = 0;
TRACE(traceall_msg("get_device(%s, %s)\n", user_supplied ?
user_supplied : "<nil>", node ? node : "<nil>"));
size = resolvepath(node, devnode, PATH_MAX);
if ((size <= 0) || (size >= (PATH_MAX - 1)))
return (NULL);
devnode[size] = '\0';
if ((strncmp(devnode, "/devices/", 9) != 0) &&
(strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
return (NULL);
fd = open(devnode, O_RDONLY|O_NDELAY);
if (fd < 0) {
TRACE(traceall_msg("Cannot open %s: %s\n", node,
strerror(errno)));
return (NULL);
}
dev = (cd_device *)my_zalloc(sizeof (cd_device));
dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
(void) strcpy(dev->d_node, devnode);
dev->d_fd = fd;
dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);
if (!inquiry(fd, dev->d_inq)) {
TRACE(traceall_msg("Inquiry failed on device %s\n", node));
if (debug) {
(void) printf("USCSI ioctl failed %d\n",
uscsi_error);
}
free(dev->d_inq);
free(dev->d_node);
(void) close(dev->d_fd);
free(dev);
return (NULL);
}
if (debug) {
cap = (uchar_t *)my_zalloc(18);
(void) printf("Checking device type\n");
if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
if (cap[2] & 0x10)
(void) printf("DVD-R read support\n");
if (cap[3] & 0x10)
(void) printf("DVD-R write support\n");
if (cap[5] & 0x4)
(void) printf("R-W supported\n");
if (cap[2] & 0x20)
(void) printf("DVD-RAM read supported\n");
if (cap[3] & 0x20)
(void) printf("DVD-RAM write supported\n");
} else {
(void) printf("Could not read mode page 2A! \n");
}
free(cap);
}
if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
(strncmp("LTR-48", (const char *)&dev->d_inq[16], 6) == 0)) {
use_cd_speed = 1;
}
if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
(strncmp("COMBO SOHC-4836VS",
(const char *)&dev->d_inq[16], 17) == 0))
if (requested_speed < 48)
use_cd_speed = 1;
cap = (uchar_t *)my_zalloc(8);
if (is_old_sun_drive(dev)) {
dev->d_read_audio = toshiba_read_audio;
dev->d_speed_ctrl = toshiba_speed_ctrl;
} else {
if (ftr_supported(fd, MMC_FTR_CD_READ) == 1) {
dev->d_read_audio = read_audio_through_read_cd;
dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
} else if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
if (cap[5] & 1) {
dev->d_read_audio = read_audio_through_read_cd;
if (cap[5] & 2)
dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
}
}
if ((ftr_supported(fd, MMC_FTR_RT_STREAM) == 1) &&
!use_cd_speed) {
dev->d_speed_ctrl = rt_streaming_ctrl;
if (debug)
(void) printf("using rt speed ctrl\n");
} else {
dev->d_speed_ctrl = cd_speed_ctrl;
if (debug)
(void) printf("using cd speed ctrl\n");
}
}
if (dev->d_read_audio != NULL)
dev->d_cap |= DEV_CAP_EXTRACT_CDDA;
dev->d_blksize = 0;
if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) {
if (debug)
(void) printf("DKIOCGMEDIAINFO failed\n");
if (read_capacity(fd, cap))
dev->d_blksize = read_scsi32(cap + 4);
} else {
dev->d_blksize = mediainfo.dki_lbsize;
}
if (debug) {
uint_t bsize;
(void) printf("blocksize = %d\n", dev->d_blksize);
(void) printf("read_format_capacity = %d \n",
read_format_capacity(fd, &bsize));
}
if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
if (is_old_sun_drive(dev)) {
dev->d_blksize = 512;
} else {
dev->d_blksize = 2048;
}
if (debug)
(void) printf(" switching to %d\n", dev->d_blksize);
}
free(cap);
if (user_supplied) {
dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
(void) strcpy(dev->d_name, user_supplied);
}
TRACE(traceall_msg("Got device %s\n", node));
return (dev);
}
void
fini_device(cd_device *dev)
{
free(dev->d_inq);
free(dev->d_node);
(void) close(dev->d_fd);
if (dev->d_name)
free(dev->d_name);
free(dev);
}
static char *
hald_symname(char *path)
{
LibHalContext *ctx = NULL;
DBusError error;
char **udi, *p = NULL;
int ndevs = 0, i;
if (vol_running == 0)
return (p);
dbus_error_init(&error);
if ((ctx = attach_to_hald()) == NULL)
return (p);
if ((udi = libhal_manager_find_device_string_match(ctx,
HAL_RDSK_PROP, path, &ndevs, &error)) == NULL)
goto done;
for (i = 0; i < ndevs; i++) {
if ((p = libhal_device_get_property_string(ctx, udi[i],
HAL_SYMDEV_PROP, NULL)) != NULL)
break;
else
libhal_free_string(p);
}
done:
if (udi != NULL)
libhal_free_string_array(udi);
if (dbus_error_is_set(&error))
dbus_error_free(&error);
detach_from_hald(ctx, HAL_INITIALIZED);
return (p);
}
static char *
hald_findname(char *symname)
{
LibHalContext *ctx = NULL;
DBusError error;
char **udi, *path = NULL;
int ndevs = 0, i;
if (symname[0] == '/') {
path = my_zalloc(strlen(symname) + 1);
(void) strlcpy(path, symname, (strlen(symname) + 1));
return (path);
}
if (vol_running != 0) {
dbus_error_init(&error);
if ((ctx = attach_to_hald()) == NULL)
return (path);
if ((udi = libhal_manager_find_device_string_match(ctx,
HAL_SYMDEV_PROP, symname, &ndevs,
&error)) == NULL)
goto done;
for (i = 0; i < ndevs; i++) {
if ((path = libhal_device_get_property_string(ctx,
udi[i], HAL_RDSK_PROP, NULL)) != NULL)
break;
else
libhal_free_string(path);
}
done:
if (udi != NULL)
libhal_free_string_array(udi);
if (dbus_error_is_set(&error))
dbus_error_free(&error);
detach_from_hald(ctx, HAL_INITIALIZED);
return (path);
} else {
return (NULL);
}
}
static int
vol_name_to_dev_node(char *vname, char *found)
{
struct stat statbuf;
char *p1;
int i;
if (vname == NULL)
return (0);
p1 = hald_findname(vname);
if (p1 == NULL)
return (0);
if (stat(p1, &statbuf) < 0) {
libhal_free_string(p1);
return (0);
}
if (S_ISDIR(statbuf.st_mode)) {
for (i = 0; i < 16; i++) {
(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
if (access(found, F_OK) >= 0)
break;
}
if (i == 16) {
libhal_free_string(p1);
return (0);
}
} else {
(void) strlcpy(found, p1, PATH_MAX);
}
libhal_free_string(p1);
return (1);
}
int
lookup_device(char *supplied, char *found)
{
struct stat statbuf;
int fd;
char tmpstr[PATH_MAX];
if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
(void) close(fd);
(void) strlcpy(found, supplied, PATH_MAX);
return (1);
}
if (fd < 0 && ((strncmp(supplied, "/dev/rdsk/", 10) == 0) ||
(strncmp(supplied, "/dev/removable-media/rdsk/", 26) == 0)))
return (0);
if ((strncmp(supplied, "/dev/dsk/", 9) == 0) ||
(strncmp(supplied, "/dev/removable-media/dsk/", 25) == 0)) {
(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
(char *)strrchr(supplied, '/'));
if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
(void) close(fd);
(void) strlcpy(found, supplied, PATH_MAX);
return (1);
}
return (0);
}
if ((strncmp(supplied, "cdrom", 5) != 0) &&
(strlen(supplied) < 32)) {
(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
supplied);
if (access(tmpstr, F_OK) < 0) {
(void) strcat(tmpstr, "s2");
}
if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
(void) close(fd);
(void) strlcpy(found, tmpstr, PATH_MAX);
return (1);
}
return (0);
}
return (vol_name_to_dev_node(supplied, found));
}
static int
is_cd(char *node)
{
int fd;
struct dk_cinfo cinfo;
int ret = 1;
fd = open(node, O_RDONLY|O_NDELAY);
if (fd < 0) {
ret = 0;
} else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
ret = 0;
} else if (cinfo.dki_ctype != DKC_CDROM) {
ret = 0;
}
if (fd >= 0) {
(void) close(fd);
}
return (ret);
}
static void
print_header(void)
{
(void) printf(gettext(" Node Connected Device"));
(void) printf(gettext(" Device type\n"));
(void) printf(
"----------------------+--------------------------------");
(void) printf("+-----------------\n");
}
int
scan_for_cd_device(int mode, cd_device **found)
{
DIR *dir;
struct dirent *dirent;
char sdev[PATH_MAX], dev[PATH_MAX];
cd_device *t_dev;
int writers_found = 0;
int header_printed = 0;
int is_writer;
int total_devices_found;
TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
if (mode) {
(void) printf(gettext("Looking for CD devices...\n"));
}
dir = opendir("/dev/rdsk");
if (dir == NULL)
return (0);
writers_found = 0;
total_devices_found = 0;
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_name[0] == '.')
continue;
(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
dirent->d_name);
if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
continue;
if (!lookup_device(sdev, dev))
continue;
if (!is_cd(dev))
continue;
if ((t_dev = get_device(NULL, dev)) == NULL) {
continue;
}
total_devices_found++;
is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
if (is_writer) {
writers_found++;
if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
*found = t_dev;
}
} else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
(total_devices_found == 1) && found) {
*found = t_dev;
}
if (mode == SCAN_LISTDEVS) {
char *sn;
sn = hald_symname(sdev);
if (!header_printed) {
print_header();
header_printed = 1;
}
(void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
sn ? sn : sdev, &t_dev->d_inq[8],
&t_dev->d_inq[16], &t_dev->d_inq[32],
gettext("CD Reader"),
is_writer ? gettext("/Writer") : "");
if (sn)
free(sn);
}
if ((found != NULL) && ((*found) != t_dev))
fini_device(t_dev);
}
(void) closedir(dir);
if ((mode & SCAN_WRITERS) || writers_found)
return (writers_found);
else
return (total_devices_found);
}
int
check_device(cd_device *dev, int cond)
{
uchar_t *disc_info, disc_status = 0, erasable = 0;
uchar_t page_code[4];
char *errmsg = NULL;
if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
((dev->d_inq[0] & 0x1f) != 5)) {
errmsg =
gettext("Specified device does not appear to be a CDROM");
}
if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
!test_unit_ready(dev->d_fd)) {
errmsg = gettext("Device not ready");
}
if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
((page_code[3] & 1) == 0)) {
errmsg = gettext("Target device is not a CD writer");
}
}
if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
((RQBUFLEN - rqresid) >= 14) &&
((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
(ASCQ(rqbuf) == 2))) {
errmsg = gettext("No media in device");
}
}
if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
(cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
(cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
if (!read_disc_info(dev->d_fd, disc_info)) {
errmsg = gettext("Cannot obtain disc information");
} else {
disc_status = disc_info[2] & 0x03;
erasable = disc_info[2] & 0x10;
}
free(disc_info);
if (errmsg == NULL) {
if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
errmsg = gettext(
"Media in the device is not erasable");
else if ((disc_status != 0) &&
(cond & CHECK_MEDIA_IS_NOT_BLANK))
errmsg = gettext(
"Media in the device is not blank");
else if ((disc_status == 2) &&
(cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
((device_type != DVD_PLUS_W) &&
(device_type != DVD_PLUS)))
errmsg = gettext(
"Media in the device is not writable");
}
}
if (errmsg) {
if (cond & EXIT_IF_CHECK_FAILED) {
err_msg("%s.\n", errmsg);
exit(1);
}
return (1);
}
return (0);
}
void
write_next_track(int mode, bstreamhandle h)
{
struct track_info *ti;
struct trackio_error *te;
off_t size;
ti = (struct track_info *)my_zalloc(sizeof (*ti));
if ((build_track_info(target, -1, ti) == 0) ||
((ti->ti_flags & TI_NWA_VALID) == 0)) {
if ((device_type == DVD_PLUS) || (device_type ==
DVD_PLUS_W)) {
ti->ti_flags |= TI_NWA_VALID;
} else {
err_msg(gettext(
"Cannot get writable address for the media.\n"));
exit(1);
}
}
if (ti->ti_nwa != ti->ti_start_address) {
err_msg(gettext(
"Media state is not suitable for this write mode.\n"));
exit(1);
}
if (mode == TRACK_MODE_DATA) {
if (!(ti->ti_track_mode & 4)) {
ti->ti_track_mode |= TRACK_MODE_DATA;
}
}
size = 0;
h->bstr_size(h, &size);
h->bstr_rewind(h);
te = (struct trackio_error *)my_zalloc(sizeof (*te));
print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
init_progress();
if (!write_track(target, ti, h, progress, size, te)) {
if (te->err_type == TRACKIO_ERR_USER_ABORT) {
(void) str_print(gettext("Aborted.\n"), progress_pos);
} else {
if (device_type != DVD_PLUS_W) {
(void) str_print(gettext("failed.\n"),
progress_pos);
}
}
}
(void) str_print(gettext("done.\n"), progress_pos);
free(ti);
free(te);
}
void
list(void)
{
if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
if (vol_running) {
err_msg(gettext(
"No CD writers found, no media in the drive "
"or not on the console.\n"));
} else {
if (cur_uid != 0) {
err_msg(gettext(
"Volume manager is not running.\n"));
err_msg(gettext(
"Please start volume manager or run cdrw as root to access all devices.\n"));
} else {
err_msg(gettext("No CD writers found.\n"));
}
}
}
exit(0);
}
void
get_media_type(int fd)
{
uchar_t *cap = (uchar_t *)my_zalloc(MMC_FTR_HDR_LEN);
if (get_configuration(fd, MMC_FTR_PRFL_LIST,
MMC_FTR_HDR_LEN, cap)) {
if (debug)
(void) print_profile_list(fd);
switch (read_scsi16(&cap[6])) {
case 0x8:
if (debug)
(void) printf("CD-ROM found\n");
device_type = CD_RW;
break;
case 0x9:
if (debug)
(void) printf("CD-R found\n");
device_type = CD_RW;
break;
case 0x10:
if (debug)
(void) printf("DVD-ROM found\n");
device_type = DVD_PLUS_W;
break;
case 0xA:
if (debug)
(void) printf("CD-RW found\n");
device_type = CD_RW;
break;
case 0x11:
if (debug)
(void) printf("DVD-R found\n");
device_type = DVD_MINUS;
break;
case 0x12:
if (debug)
(void) printf("DVD-RAM found\n");
device_type = CD_RW;
break;
case 0x13:
case 0x14:
if (debug)
(void) printf("DVD-RW found\n");
device_type = DVD_MINUS;
break;
case 0x15:
case 0x16:
if (debug)
(void) printf("DVD-R DL found\n");
device_type = DVD_MINUS;
break;
case 0x17:
if (debug)
(void) printf("DVD-RW DL found\n");
device_type = DVD_MINUS;
break;
case 0x1A:
if (debug)
(void) printf("DVD+RW found\n");
device_type = DVD_PLUS_W;
break;
case 0x1B:
if (debug)
(void) printf("DVD+R found\n");
device_type = DVD_PLUS;
break;
case 0x2A:
if (debug)
(void) printf("DVD+RW DL found\n");
device_type = DVD_PLUS_W;
break;
case 0x2B:
if (debug)
(void) printf("DVD+R DL found\n");
device_type = DVD_PLUS;
break;
default:
if (debug)
(void) printf(
"unknown drive found\n type = 0x%x",
cap[7]);
device_type = CD_RW;
}
}
free(cap);
}
uint_t
cdrw_bandwidth_to_x(uint_t rate)
{
switch (device_type) {
case DVD_PLUS_W:
case DVD_MINUS:
case DVD_PLUS:
return (DVD_RATE_TO_X(rate));
default:
case CD_RW:
return (CD_RATE_TO_X(rate));
}
}
uint_t
cdrw_x_to_bandwidth(uint_t x)
{
switch (device_type) {
case DVD_PLUS_W:
case DVD_MINUS:
case DVD_PLUS:
return (DVD_X_TO_RATE(x));
default:
case CD_RW:
return (CD_X_TO_RATE(x));
}
}