chiark / gitweb /
cdrom_id: Don't read beyond "last track" in TOC
[elogind.git] / extras / cdrom_id / cdrom_id.c
index 338b3782cb1782ac65aca443c20961b1e2ccf45d..1b43fe43ae8decf378d7969a96bac5c470f61172 100644 (file)
@@ -297,14 +297,9 @@ static int feature_profiles(struct udev *udev, const unsigned char *profiles, si
                        cd_dvd_ram = 1;
                        break;
                case 0x13:
-                       info(udev, "profile 0x%02x media_dvd_rw\n", profile);
-                       cd_media_dvd_rw = 1;
-                       cd_media_dvd_rw_ro = 1;
-                       break;
                case 0x14:
                        info(udev, "profile 0x%02x dvd_rw\n", profile);
                        cd_dvd_rw = 1;
-                       cd_media_dvd_rw_seq = 1;
                        break;
                case 0x1B:
                        info(udev, "profile 0x%02x dvd_plus_r\n", profile);
@@ -369,8 +364,14 @@ static int cd_profiles_old_mmc(struct udev *udev, int fd)
        err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
-               info(udev, "no current profile, assuming no media\n");
-               return -1;
+               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;
@@ -378,14 +379,12 @@ static int cd_profiles_old_mmc(struct udev *udev, int fd)
        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) { 
+       } 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");
+       } else {
+               cd_media_cd_rom = 1;
+               info(udev, "profile 0x08 media_cd_rom\n");
        }
        return 0;
 }
@@ -488,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);
@@ -575,6 +580,7 @@ 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)
@@ -590,7 +596,7 @@ static int cd_media_info(struct udev *udev, int fd)
         * 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[17 * 2048];
+               unsigned char buffer[32 * 2048];
                unsigned char result, len;
                int block, offset;
 
@@ -661,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)) {
@@ -691,8 +697,9 @@ static int cd_media_info(struct udev *udev, int fd)
        }
 
 determined:
-       /* "other" is e. g. DVD-RAM, can't append sessions there either */
-       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];
@@ -705,7 +712,7 @@ static int cd_media_toc(struct udev *udev, int fd)
        struct scsi_cmd sc;
        unsigned char header[12];
        unsigned char toc[2048];
-       unsigned int len, i;
+       unsigned int len, i, num_tracks;
        unsigned char *p;
        int err;
 
@@ -721,11 +728,13 @@ static int cd_media_toc(struct udev *udev, int fd)
        }
 
        len = (header[0] << 8 | header[1]) + 2;
-       info(udev, "READ TOC: len: %d\n", len);
+       info(udev, "READ TOC: len: %d, start track: %d, end track: %d\n", len, header[2], header[3]);
        if (len > sizeof(toc))
                return -1;
        if (len < 2)
                return -1;
+       /* 2: first track, 3: last track */
+       num_tracks = header[3] - header[2] + 1;
 
        /* empty media has no tracks */
        if (len < 8)
@@ -743,7 +752,10 @@ static int cd_media_toc(struct udev *udev, int fd)
                return -1;
        }
 
-       for (p = toc+4, i = 4; i < len-8; i += 8, p += 8) {
+       /* Take care to not iterate beyond the last valid track as specified in
+        * the TOC, but also avoid going beyond the TOC length, just in case
+        * the last track number is invalidly large */
+       for (p = toc+4, i = 4; i < len-8 && num_tracks > 0; i += 8, p += 8, --num_tracks) {
                unsigned int block;
                unsigned int is_data_track;
 
@@ -870,13 +882,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");