chiark / gitweb /
cdrom_id: Fall back to CDROM_DRIVE_STATUS if all MMC commands fail
[elogind.git] / extras / cdrom_id / cdrom_id.c
index 9c4f6f31b40277c80153e02ba8eb36577b572050..e7a3e40a85e22f8a2cbb3f61a649cb842df26332 100644 (file)
@@ -85,6 +85,8 @@ static unsigned int cd_media_cd_rw = 0;
 static unsigned int cd_media_dvd_rom = 0;
 static unsigned int cd_media_dvd_r = 0;
 static unsigned int cd_media_dvd_rw = 0;
+static unsigned int cd_media_dvd_rw_ro = 0; /* restricted overwrite mode */
+static unsigned int cd_media_dvd_rw_seq = 0; /* sequential mode */
 static unsigned int cd_media_dvd_ram = 0;
 static unsigned int cd_media_dvd_plus_r = 0;
 static unsigned int cd_media_dvd_plus_rw = 0;
@@ -348,6 +350,45 @@ static int feature_profiles(struct udev *udev, const unsigned char *profiles, si
        return 0;
 }
 
+static int cd_profiles_old_mmc(struct udev *udev, int fd)
+{
+       struct scsi_cmd sc;
+       int err;
+
+       unsigned char header[32];
+
+       scsi_cmd_init(udev, &sc, header, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x51);
+       scsi_cmd_set(udev, &sc, 8, sizeof(header));
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
+       if ((err != 0)) {
+               info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
+               if (cd_media == 1) {
+                       info(udev, "no current profile, but disc is present; assuming CD-ROM\n");
+                       cd_media_cd_rom = 1;
+                       return 0;
+               } else {
+                       info(udev, "no current profile, assuming no media\n");
+                       return -1;
+               }
+       };
+
+       cd_media = 1;
+
+       if (header[2] & 16) {
+               cd_media_cd_rw = 1;
+               info(udev, "profile 0x0a media_cd_rw\n");
+       } else if ((header[2] & 3) < 2 && cd_cd_r) {
+               cd_media_cd_r = 1;
+               info(udev, "profile 0x09 media_cd_r\n");
+       } else {
+               cd_media_cd_rom = 1;
+               info(udev, "profile 0x08 media_cd_rom\n");
+       }
+       return 0;
+}
+
 static int cd_profiles(struct udev *udev, int fd)
 {
        struct scsi_cmd sc;
@@ -365,6 +406,12 @@ static int cd_profiles(struct udev *udev, int fd)
        err = scsi_cmd_run(udev, &sc, fd, features, sizeof(features));
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
+               /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
+               if (SK(err) == 0x5 && ASC(err) == 0x20) {
+                       info(udev, "drive is pre-MMC2 and does not support 46h get configuration command\n");
+                       info(udev, "trying to work around the problem\n");
+                       return cd_profiles_old_mmc(udev, fd);
+               }
                return -1;
        }
 
@@ -440,10 +487,16 @@ static int cd_profiles(struct udev *udev, int fd)
                cd_media_dvd_ram = 1;
                break;
        case 0x13:
+               info(udev, "profile 0x%02x media_dvd_rw_ro\n", cur_profile);
+               cd_media = 1;
+               cd_media_dvd_rw = 1;
+               cd_media_dvd_rw_ro = 1;
+               break;
        case 0x14:
-               info(udev, "profile 0x%02x media_dvd_rw\n", cur_profile);
+               info(udev, "profile 0x%02x media_dvd_rw_seq\n", cur_profile);
                cd_media = 1;
                cd_media_dvd_rw = 1;
+               cd_media_dvd_rw_seq = 1;
                break;
        case 0x1B:
                info(udev, "profile 0x%02x media_dvd_plus_r\n", cur_profile);
@@ -527,16 +580,23 @@ static int cd_media_info(struct udev *udev, int fd)
 
        cd_media = 1;
        info(udev, "disk type %02x\n", header[8]);
+       info(udev, "hardware reported media status: %s\n", media_status[header[2] & 3]);
 
        /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
        if (!cd_media_cd_rom)
                cd_media_state = media_status[header[2] & 3];
 
-       /* DVD+RW discs once formatted are always "complete", DVD-RAM are
-        * "other" or "complete" if the disc is write protected; we need to
-        * check the contents if it is blank */
-       if ((cd_media_dvd_plus_rw || cd_media_dvd_plus_rw_dl || cd_media_dvd_ram) && (header[2] & 3) > 1) {
-               unsigned char buffer[17 * 2048];
+       /* fresh DVD-RW in restricted overwite mode reports itself as
+        * "appendable"; change it to "blank" to make it consistent with what
+        * gets reported after blanking, and what userspace expects  */
+       if (cd_media_dvd_rw_ro && (header[2] & 3) == 1)
+               cd_media_state = media_status[0];
+
+       /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
+        * always "complete", DVD-RAM are "other" or "complete" if the disc is
+        * write protected; we need to check the contents if it is blank */
+       if ((cd_media_dvd_rw_ro || cd_media_dvd_plus_rw || cd_media_dvd_plus_rw_dl || cd_media_dvd_ram) && (header[2] & 3) > 1) {
+               unsigned char buffer[32 * 2048];
                unsigned char result, len;
                int block, offset;
 
@@ -607,7 +667,7 @@ static int cd_media_info(struct udev *udev, int fd)
                scsi_cmd_init(udev, &sc, buffer, sizeof(buffer));
                scsi_cmd_set(udev, &sc, 0, 0x28);
                scsi_cmd_set(udev, &sc, 5, 0);
-               scsi_cmd_set(udev, &sc, 8, 17);
+               scsi_cmd_set(udev, &sc, 8, 32);
                scsi_cmd_set(udev, &sc, 9, 0);
                err = scsi_cmd_run(udev, &sc, fd, buffer, sizeof(buffer));
                if ((err != 0)) {
@@ -637,7 +697,9 @@ static int cd_media_info(struct udev *udev, int fd)
        }
 
 determined:
-       if ((header[2] & 3) != 2)
+       /* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
+        * restricted overwrite mode can never append, only in sequential mode */
+       if ((header[2] & 3) < 2 && !cd_media_dvd_rw_ro)
                cd_media_session_next = header[10] << 8 | header[5];
        cd_media_session_count = header[9] << 8 | header[4];
        cd_media_track_count = header[11] << 8 | header[6];
@@ -815,13 +877,11 @@ int main(int argc, char *argv[])
        if (cd_profiles(udev, fd) < 0)
                goto print;
 
-       /* get writable media state */
-       if (cd_media_info(udev, fd) < 0)
-               goto print;
-
        /* get session/track info */
-       if (cd_media_toc(udev, fd) < 0)
-               goto print;
+       cd_media_toc(udev, fd);
+
+       /* get writable media state */
+       cd_media_info(udev, fd);
 
 print:
        printf("ID_CDROM=1\n");