/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (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 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma ident "%Z%%M% %I% %E% SMI" #include #include #include #include #include #include #include #include #include #include #include #include #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" /* * Old sun drives have a vendor specific mode page for setting/getting speed. * Also they use a different method for extracting audio. * We have the device inquiry strings at this time. This is used to enable * us to use older sun drives to extract audio. */ static int is_old_sun_drive(cd_device *dev) { /* * If we have a SONY CDU 561, CDU 8012, or TOSHIBA model with XMa we * need to handle these drives a bit differently. */ 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]; /* Changing speed is not allowed for 32X TOSHIBA drives */ 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); } /* * returns a cd_device handle for a node returned by lookup_device() * also takes the user supplied name and stores it inside the node */ 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; /* * we need to resolve any link paths to avoid fake files * such as /dev/rdsk/../../export/file. */ TRACE(traceall_msg("get_device(%s, %s)\n", user_supplied ? user_supplied : "", node ? node : "")); size = resolvepath(node, devnode, PATH_MAX); if ((size <= 0) || (size >= (PATH_MAX - 1))) return (NULL); /* resolvepath may not return a null terminated string */ devnode[size] = '\0'; /* the device node must be in /devices/ or /vol/dev/rdsk */ if ((strncmp(devnode, "/devices/", 9) != 0) && (strncmp(devnode, "/vol/dev/rdsk", 13) != 0)) return (NULL); /* * Since we are currently running with the user euid it is * safe to try to open the file without checking access. */ 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); } /* Detect if it's a Lite-ON drive with a streaming CD problem */ 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; } 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 (use_cd_speed) { /* * Work around for Lite-on FW bug in which rt_speed_ctrl * doesn't work correctly. */ dev->d_speed_ctrl = cd_speed_ctrl; } else { /* * If feature 8 (see GET CONF MMC command) is supported, * READ CD will work and will return jitter free audio data. * Otherwise look at page code 2A for this info. */ if (get_configuration(fd, 0x1E, 8, cap)) { 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 feature 0x0107 is suported then Real-time streaming * commands can be used for speed control. Otherwise try * SET CD SPEED. */ if (get_configuration(fd, 0x0107, 8, cap)) { 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; /* * Find the block size of the device so we can translate * the reads/writes to the device blocksize. */ if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) { /* * If DKIOCGMEDIAINFO fails we'll try to get * the blocksize from the device itself. */ 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)); } /* * Some devices will return invalid blocksizes. ie. Toshiba * drives will return 2352 when an audio CD is inserted. * Older Sun drives will use 512 byte block sizes. All newer * drives should have 2k blocksizes. */ 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 int vol_name_to_dev_node(char *vname, char *found) { struct stat statbuf; char *p1; int i; if (vname == NULL) return (0); if (vol_running) (void) volmgt_check(vname); p1 = media_findname(vname); if (p1 == NULL) return (0); if (stat(p1, &statbuf) < 0) { free(p1); return (0); } if (statbuf.st_mode & S_IFDIR) { 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) { free(p1); return (0); } } else { (void) strlcpy(found, p1, PATH_MAX); } free(p1); return (1); } /* * Searches for volume manager's equivalent char device for the * supplied pathname which is of the form of /dev/rdsk/cxtxdxsx */ static int vol_lookup(char *supplied, char *found) { char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p; int i, ret; (void) strlcpy(tmpstr, supplied, PATH_MAX); if ((p = volmgt_symname(tmpstr)) == NULL) { if (strrchr(tmpstr, 's') == NULL) return (0); *((char *)(strrchr(tmpstr, 's') + 1)) = 0; for (i = 0; i < 16; i++) { (void) snprintf(tmpstr1, PATH_MAX, "%s%d", tmpstr, i); if ((p = volmgt_symname(tmpstr1)) != NULL) break; } if (p == NULL) return (0); } ret = vol_name_to_dev_node(p, found); free(p); return (ret); } /* * Builds an open()able device path from a user supplied node which can be * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like * cdrom[n] * returns the path found in 'found' and returns 1. Otherwise returns 0. */ int lookup_device(char *supplied, char *found) { struct stat statbuf; int fd; char tmpstr[PATH_MAX]; /* If everything is fine and proper, no need to analyze */ if ((stat(supplied, &statbuf) == 0) && (statbuf.st_mode & S_IFCHR) && ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) { (void) close(fd); (void) strlcpy(found, supplied, PATH_MAX); return (1); } if (strncmp(supplied, "/dev/rdsk/", 10) == 0) return (vol_lookup(supplied, found)); if (strncmp(supplied, "/dev/dsk/", 9) == 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); } if ((access(tmpstr, F_OK) == 0) && vol_running) return (vol_lookup(tmpstr, found)); else 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); } if ((access(tmpstr, F_OK) == 0) && vol_running) return (vol_lookup(tmpstr, found)); } return (vol_name_to_dev_node(supplied, found)); } /* * Opens the device node name passed and returns 1 (true) if the * device is a CD. */ 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) { /* l10n_NOTE : Column spacing should be kept same */ (void) printf(gettext(" Node Connected Device")); /* l10n_NOTE : Column spacing should be kept same */ (void) printf(gettext(" Device type\n")); (void) printf( "----------------------+--------------------------------"); (void) printf("+-----------------\n"); } /* * returns the number of writers or CD/DVD-roms found and the path of * the first device found depending on the mode argument. * possible mode values are: * SCAN_ALL_CDS Scan all CD/DVD devices. Return first CD-RW found. * SCAN_WRITERS Scan all CD-RW devices. Return first one found. * SCAN_LISTDEVS List all devices found. */ 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 retry = 0; int total_devices_found; int total_devices_found_last_time = 0; int defer = 0; #define MAX_RETRIES_FOR_SCANNING 5 TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode)); if (mode) { if (vol_running) defer = 1; (void) printf(gettext("Looking for CD devices...\n")); } try_again: 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) { /* We found a CD-ROM or DVD-ROM */ *found = t_dev; } if ((mode == SCAN_LISTDEVS) && (!defer)) { char *sn; sn = volmgt_symname(sdev); if (!header_printed) { print_header(); header_printed = 1; } /* show vendor, model, firmware rev and device type */ (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 volume manager is running we'll do a retry in case the * user has just inserted media, This should allow vold * enough time to create the device links. If we cannot * find any devices, or we find any new devices we'll retry * again up to MAX_RETRIES_FOR_SCANNING */ retry++; if ((retry <= MAX_RETRIES_FOR_SCANNING) && vol_running) { if (defer || ((mode != SCAN_LISTDEVS) && (writers_found == 0))) { if ((total_devices_found == 0) || (total_devices_found != total_devices_found_last_time)) { /* before we rescan remove the device found */ if (total_devices_found != 0 && t_dev) { fini_device(t_dev); } total_devices_found_last_time = total_devices_found; (void) sleep(2); goto try_again; } else { /* Do the printing this time */ defer = 0; goto try_again; } } } if ((mode & SCAN_WRITERS) || writers_found) return (writers_found); else return (total_devices_found); } /* * Check device for various conditions/capabilities * If EXIT_IF_CHECK_FAILED set in cond then it will also exit after * printing a message. */ 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"); } /* Look at the capabilities page for this information */ 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))) { /* medium not present */ errmsg = gettext("No media in device"); } } /* Issue READ DISC INFORMATION mmc command */ 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); } /* * Generic routine for writing whatever the next track is and taking * care of the progress bar. Mode tells the track type (audio or data). * Data from track is taken from the byte stream h */ 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(TRACK_INFO_SIZE); 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)) { /* Write track depends upon this bit */ 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, (void *)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) { /* l10n_NOTE : 'failed' as in Writing Track...failed */ (void) str_print(gettext("failed.\n"), progress_pos); } } } /* l10n_NOTE : 'done' as in "Writing track 1...done" */ (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 or no media in the drive.\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[DVD_CONFIG_SIZE]; uint_t i; (void) memset(cap, 0, DVD_CONFIG_SIZE); if (get_configuration(fd, 0, DVD_CONFIG_SIZE, cap)) { if (debug && verbose) { (void) printf("\nprofile = "); for (i = 7; i < 0x20; i += 4) (void) printf(" 0x%x", cap[i]); (void) printf("\n"); } switch (cap[7]) { case 0x8: /* CD-ROM */ if (debug) (void) printf("CD-ROM found\n"); /* * To avoid regression issues, treat as * A cdrw, we will check the writable * mode page to see if the media is * actually writable. */ device_type = CD_RW; break; case 0x9: /* CD-R */ if (debug) (void) printf("CD-R found\n"); device_type = CD_RW; break; case 0x10: /* DVD-ROM */ /* * Have seen drives return DVD+RW media * DVD-ROM, so try treating it as a DVD+RW * profile. checking for writable media * is done through mode page 5. */ if (debug) (void) printf("DVD-ROM found\n"); device_type = DVD_PLUS_W; break; case 0xA: /* CD-RW */ if (debug) (void) printf("CD-RW found\n"); device_type = CD_RW; break; case 0x11: /* DVD-R */ if (debug) (void) printf("DVD-R found\n"); device_type = DVD_MINUS; break; case 0x12: /* DVD-RAM */ if (debug) (void) printf("DVD-RAM found\n"); /* treat as CD-RW, may be a legacy drive */ device_type = CD_RW; break; case 0x13: /* DVD-RW restricted overwrite */ case 0x14: /* DVD-RW sequencial */ if (debug) (void) printf("DVD-RW found\n"); device_type = DVD_MINUS; break; case 0x1A: /* DVD+RW */ if (debug) (void) printf("DVD+RW found\n"); device_type = DVD_PLUS_W; break; case 0x1B: /* DVD+R */ if (debug) (void) printf("DVD+R found\n"); device_type = DVD_PLUS; break; default: if (debug) (void) printf( "unknown drive found\n type = 0x%x", cap[7]); /* * Treat as CD_RW to avoid regression, may * be a legacy drive. */ device_type = CD_RW; } } }