summaryrefslogtreecommitdiff
path: root/src/extras/cdrom_id/cdrom_id.c
blob: f90d52ec9c487d5193b0c5efff4c4b00daab232e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
/*
 * cdrom_id - optical drive and media information prober
 *
 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
#include <time.h>
#include <scsi/sg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>

#include "libudev.h"
#include "libudev-private.h"

static bool debug;

static void log_fn(struct udev *udev, int priority,
                   const char *file, int line, const char *fn,
                   const char *format, va_list args)
{
        if (debug) {
                fprintf(stderr, "%s: ", fn);
                vfprintf(stderr, format, args);
        } else {
                vsyslog(priority, format, args);
        }
}

/* device info */
static unsigned int cd_cd_rom;
static unsigned int cd_cd_r;
static unsigned int cd_cd_rw;
static unsigned int cd_dvd_rom;
static unsigned int cd_dvd_r;
static unsigned int cd_dvd_rw;
static unsigned int cd_dvd_ram;
static unsigned int cd_dvd_plus_r;
static unsigned int cd_dvd_plus_rw;
static unsigned int cd_dvd_plus_r_dl;
static unsigned int cd_dvd_plus_rw_dl;
static unsigned int cd_bd;
static unsigned int cd_bd_r;
static unsigned int cd_bd_re;
static unsigned int cd_hddvd;
static unsigned int cd_hddvd_r;
static unsigned int cd_hddvd_rw;
static unsigned int cd_mo;
static unsigned int cd_mrw;
static unsigned int cd_mrw_w;

/* media info */
static unsigned int cd_media;
static unsigned int cd_media_cd_rom;
static unsigned int cd_media_cd_r;
static unsigned int cd_media_cd_rw;
static unsigned int cd_media_dvd_rom;
static unsigned int cd_media_dvd_r;
static unsigned int cd_media_dvd_rw;
static unsigned int cd_media_dvd_rw_ro; /* restricted overwrite mode */
static unsigned int cd_media_dvd_rw_seq; /* sequential mode */
static unsigned int cd_media_dvd_ram;
static unsigned int cd_media_dvd_plus_r;
static unsigned int cd_media_dvd_plus_rw;
static unsigned int cd_media_dvd_plus_r_dl;
static unsigned int cd_media_dvd_plus_rw_dl;
static unsigned int cd_media_bd;
static unsigned int cd_media_bd_r;
static unsigned int cd_media_bd_re;
static unsigned int cd_media_hddvd;
static unsigned int cd_media_hddvd_r;
static unsigned int cd_media_hddvd_rw;
static unsigned int cd_media_mo;
static unsigned int cd_media_mrw;
static unsigned int cd_media_mrw_w;

static const char *cd_media_state = NULL;
static unsigned int cd_media_session_next;
static unsigned int cd_media_session_count;
static unsigned int cd_media_track_count;
static unsigned int cd_media_track_count_data;
static unsigned int cd_media_track_count_audio;
static unsigned long long int cd_media_session_last_offset;

#define ERRCODE(s)        ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
#define SK(errcode)        (((errcode) >> 16) & 0xF)
#define ASC(errcode)        (((errcode) >> 8) & 0xFF)
#define ASCQ(errcode)        ((errcode) & 0xFF)

static bool is_mounted(const char *device)
{
        struct stat statbuf;
        FILE *fp;
        int maj, min;
        bool mounted = false;

        if (stat(device, &statbuf) < 0)
                return -ENODEV;

        fp = fopen("/proc/self/mountinfo", "r");
        if (fp == NULL)
                return -ENOSYS;
        while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
                if (makedev(maj, min) == statbuf.st_rdev) {
                        mounted = true;
                        break;
                }
        }
        fclose(fp);
        return mounted;
}

static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
{
        if (err == -1) {
                info(udev, "%s failed\n", cmd);
                return;
        }
        info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
}

struct scsi_cmd {
        struct cdrom_generic_command cgc;
        union {
                struct request_sense s;
                unsigned char u[18];
        } _sense;
        struct sg_io_hdr sg_io;
};

static void scsi_cmd_init(struct udev *udev, struct scsi_cmd *cmd)
{
        memset(cmd, 0x00, sizeof(struct scsi_cmd));
        cmd->cgc.quiet = 1;
        cmd->cgc.sense = &cmd->_sense.s;
        cmd->sg_io.interface_id = 'S';
        cmd->sg_io.mx_sb_len = sizeof(cmd->_sense);
        cmd->sg_io.cmdp = cmd->cgc.cmd;
        cmd->sg_io.sbp = cmd->_sense.u;
        cmd->sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO;
}

static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, unsigned char arg)
{
        cmd->sg_io.cmd_len = i + 1;
        cmd->cgc.cmd[i] = arg;
}

#define CHECK_CONDITION 0x01

static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
{
        int ret = 0;

        if (bufsize > 0) {
                cmd->sg_io.dxferp = buf;
                cmd->sg_io.dxfer_len = bufsize;
                cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
        } else {
                cmd->sg_io.dxfer_direction = SG_DXFER_NONE;
        }
        if (ioctl(fd, SG_IO, &cmd->sg_io))
                return -1;

        if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
                errno = EIO;
                ret = -1;
                if (cmd->sg_io.masked_status & CHECK_CONDITION) {
                        ret = ERRCODE(cmd->_sense.u);
                        if (ret == 0)
                                ret = -1;
                }
        }
        return ret;
}

static int media_lock(struct udev *udev, int fd, bool lock)
{
        int err;

        /* disable the kernel's lock logic */
        err = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_LOCK);
        if (err < 0)
                info(udev, "CDROM_CLEAR_OPTIONS, CDO_LOCK failed\n");

        err = ioctl(fd, CDROM_LOCKDOOR, lock ? 1 : 0);
        if (err < 0)
                info(udev, "CDROM_LOCKDOOR failed\n");

        return err;
}

static int media_eject(struct udev *udev, int fd)
{
        struct scsi_cmd sc;
        int err;

        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x1b);
        scsi_cmd_set(udev, &sc, 4, 0x02);
        scsi_cmd_set(udev, &sc, 5, 0);
        err = scsi_cmd_run(udev, &sc, fd, NULL, 0);
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "START_STOP_UNIT", err);
                return -1;
        }
        return 0;
}

static int cd_capability_compat(struct udev *udev, int fd)
{
        int capability;

        capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
        if (capability < 0) {
                info(udev, "CDROM_GET_CAPABILITY failed\n");
                return -1;
        }

        if (capability & CDC_CD_R)
                cd_cd_r = 1;
        if (capability & CDC_CD_RW)
                cd_cd_rw = 1;
        if (capability & CDC_DVD)
                cd_dvd_rom = 1;
        if (capability & CDC_DVD_R)
                cd_dvd_r = 1;
        if (capability & CDC_DVD_RAM)
                cd_dvd_ram = 1;
        if (capability & CDC_MRW)
                cd_mrw = 1;
        if (capability & CDC_MRW_W)
                cd_mrw_w = 1;
        return 0;
}

static int cd_media_compat(struct udev *udev, int fd)
{
        if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
                info(udev, "CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
                return -1;
        }
        cd_media = 1;
        return 0;
}

static int cd_inquiry(struct udev *udev, int fd)
{
        struct scsi_cmd sc;
        unsigned char inq[128];
        int err;

        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x12);
        scsi_cmd_set(udev, &sc, 4, 36);
        scsi_cmd_set(udev, &sc, 5, 0);
        err = scsi_cmd_run(udev, &sc, fd, inq, 36);
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "INQUIRY", err);
                return -1;
        }

        if ((inq[0] & 0x1F) != 5) {
                info(udev, "not an MMC unit\n");
                return -1;
        }

        info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
        return 0;
}

static void feature_profile_media(struct udev *udev, int cur_profile)
{
        switch (cur_profile) {
        case 0x03:
        case 0x04:
        case 0x05:
                info(udev, "profile 0x%02x \n", cur_profile);
                cd_media = 1;
                cd_media_mo = 1;
                break;
        case 0x08:
                info(udev, "profile 0x%02x media_cd_rom\n", cur_profile);
                cd_media = 1;
                cd_media_cd_rom = 1;
                break;
        case 0x09:
                info(udev, "profile 0x%02x media_cd_r\n", cur_profile);
                cd_media = 1;
                cd_media_cd_r = 1;
                break;
        case 0x0a:
                info(udev, "profile 0x%02x media_cd_rw\n", cur_profile);
                cd_media = 1;
                cd_media_cd_rw = 1;
                break;
        case 0x10:
                info(udev, "profile 0x%02x media_dvd_ro\n", cur_profile);
                cd_media = 1;
                cd_media_dvd_rom = 1;
                break;
        case 0x11:
                info(udev, "profile 0x%02x media_dvd_r\n", cur_profile);
                cd_media = 1;
                cd_media_dvd_r = 1;
                break;
        case 0x12:
                info(udev, "profile 0x%02x media_dvd_ram\n", cur_profile);
                cd_media = 1;
                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_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);
                cd_media = 1;
                cd_media_dvd_plus_r = 1;
                break;
        case 0x1A:
                info(udev, "profile 0x%02x media_dvd_plus_rw\n", cur_profile);
                cd_media = 1;
                cd_media_dvd_plus_rw = 1;
                break;
        case 0x2A:
                info(udev, "profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile);
                cd_media = 1;
                cd_media_dvd_plus_rw_dl = 1;
                break;
        case 0x2B:
                info(udev, "profile 0x%02x media_dvd_plus_r_dl\n", cur_profile);
                cd_media = 1;
                cd_media_dvd_plus_r_dl = 1;
                break;
        case 0x40:
                info(udev, "profile 0x%02x media_bd\n", cur_profile);
                cd_media = 1;
                cd_media_bd = 1;
                break;
        case 0x41:
        case 0x42:
                info(udev, "profile 0x%02x media_bd_r\n", cur_profile);
                cd_media = 1;
                cd_media_bd_r = 1;
                break;
        case 0x43:
                info(udev, "profile 0x%02x media_bd_re\n", cur_profile);
                cd_media = 1;
                cd_media_bd_re = 1;
                break;
        case 0x50:
                info(udev, "profile 0x%02x media_hddvd\n", cur_profile);
                cd_media = 1;
                cd_media_hddvd = 1;
                break;
        case 0x51:
                info(udev, "profile 0x%02x media_hddvd_r\n", cur_profile);
                cd_media = 1;
                cd_media_hddvd_r = 1;
                break;
        case 0x52:
                info(udev, "profile 0x%02x media_hddvd_rw\n", cur_profile);
                cd_media = 1;
                cd_media_hddvd_rw = 1;
                break;
        default:
                info(udev, "profile 0x%02x <ignored>\n", cur_profile);
                break;
        }
}

static int feature_profiles(struct udev *udev, const unsigned char *profiles, size_t size)
{
        unsigned int i;

        for (i = 0; i+4 <= size; i += 4) {
                int profile;

                profile = profiles[i] << 8 | profiles[i+1];
                switch (profile) {
                case 0x03:
                case 0x04:
                case 0x05:
                        info(udev, "profile 0x%02x mo\n", profile);
                        cd_mo = 1;
                        break;
                case 0x08:
                        info(udev, "profile 0x%02x cd_rom\n", profile);
                        cd_cd_rom = 1;
                        break;
                case 0x09:
                        info(udev, "profile 0x%02x cd_r\n", profile);
                        cd_cd_r = 1;
                        break;
                case 0x0A:
                        info(udev, "profile 0x%02x cd_rw\n", profile);
                        cd_cd_rw = 1;
                        break;
                case 0x10:
                        info(udev, "profile 0x%02x dvd_rom\n", profile);
                        cd_dvd_rom = 1;
                        break;
                case 0x12:
                        info(udev, "profile 0x%02x dvd_ram\n", profile);
                        cd_dvd_ram = 1;
                        break;
                case 0x13:
                case 0x14:
                        info(udev, "profile 0x%02x dvd_rw\n", profile);
                        cd_dvd_rw = 1;
                        break;
                case 0x1B:
                        info(udev, "profile 0x%02x dvd_plus_r\n", profile);
                        cd_dvd_plus_r = 1;
                        break;
                case 0x1A:
                        info(udev, "profile 0x%02x dvd_plus_rw\n", profile);
                        cd_dvd_plus_rw = 1;
                        break;
                case 0x2A:
                        info(udev, "profile 0x%02x dvd_plus_rw_dl\n", profile);
                        cd_dvd_plus_rw_dl = 1;
                        break;
                case 0x2B:
                        info(udev, "profile 0x%02x dvd_plus_r_dl\n", profile);
                        cd_dvd_plus_r_dl = 1;
                        break;
                case 0x40:
                        cd_bd = 1;
                        info(udev, "profile 0x%02x bd\n", profile);
                        break;
                case 0x41:
                case 0x42:
                        cd_bd_r = 1;
                        info(udev, "profile 0x%02x bd_r\n", profile);
                        break;
                case 0x43:
                        cd_bd_re = 1;
                        info(udev, "profile 0x%02x bd_re\n", profile);
                        break;
                case 0x50:
                        cd_hddvd = 1;
                        info(udev, "profile 0x%02x hddvd\n", profile);
                        break;
                case 0x51:
                        cd_hddvd_r = 1;
                        info(udev, "profile 0x%02x hddvd_r\n", profile);
                        break;
                case 0x52:
                        cd_hddvd_rw = 1;
                        info(udev, "profile 0x%02x hddvd_rw\n", profile);
                        break;
                default:
                        info(udev, "profile 0x%02x <ignored>\n", profile);
                        break;
                }
        }
        return 0;
}

/* returns 0 if media was detected */
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);
        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;
}

/* returns 0 if media was detected */
static int cd_profiles(struct udev *udev, int fd)
{
        struct scsi_cmd sc;
        unsigned char features[65530];
        unsigned int cur_profile = 0;
        unsigned int len;
        unsigned int i;
        int err;
        int ret;

        ret = -1;

        /* First query the current profile */
        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x46);
        scsi_cmd_set(udev, &sc, 8, 8);
        scsi_cmd_set(udev, &sc, 9, 0);
        err = scsi_cmd_run(udev, &sc, fd, features, 8);
        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");
                        ret = cd_profiles_old_mmc(udev, fd);
                }
                goto out;
        }

        cur_profile = features[6] << 8 | features[7];
        if (cur_profile > 0) {
                info(udev, "current profile 0x%02x\n", cur_profile);
                feature_profile_media (udev, cur_profile);
                ret = 0; /* we have media */
        } else {
                info(udev, "no current profile, assuming no media\n");
        }

        len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
        info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);

        if (len > sizeof(features)) {
                info(udev, "can not get features in a single query, truncating\n");
                len = sizeof(features);
        } else if (len <= 8) {
                len = sizeof(features);
        }

        /* Now get the full feature buffer */
        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x46);
        scsi_cmd_set(udev, &sc, 7, ( len >> 8 ) & 0xff);
        scsi_cmd_set(udev, &sc, 8, len & 0xff);
        scsi_cmd_set(udev, &sc, 9, 0);
        err = scsi_cmd_run(udev, &sc, fd, features, len);
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
                return -1;
        }

        /* parse the length once more, in case the drive decided to have other features suddenly :) */
        len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
        info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);

        if (len > sizeof(features)) {
                info(udev, "can not get features in a single query, truncating\n");
                len = sizeof(features);
        }

        /* device features */
        for (i = 8; i+4 < len; i += (4 + features[i+3])) {
                unsigned int feature;

                feature = features[i] << 8 | features[i+1];

                switch (feature) {
                case 0x00:
                        info(udev, "GET CONFIGURATION: feature 'profiles', with %i entries\n", features[i+3] / 4);
                        feature_profiles(udev, &features[i]+4, features[i+3]);
                        break;
                default:
                        info(udev, "GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature, features[i+3]);
                        break;
                }
        }
out:
        return ret;
}

static int cd_media_info(struct udev *udev, int fd)
{
        struct scsi_cmd sc;
        unsigned char header[32];
        static const char *media_status[] = {
                "blank",
                "appendable",
                "complete",
                "other"
        };
        int err;

        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x51);
        scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
        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);
                return -1;
        };

        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];

        /* 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;

                if (cd_media_dvd_ram) {
                        /* a write protected dvd-ram may report "complete" status */

                        unsigned char dvdstruct[8];
                        unsigned char format[12];

                        scsi_cmd_init(udev, &sc);
                        scsi_cmd_set(udev, &sc, 0, 0xAD);
                        scsi_cmd_set(udev, &sc, 7, 0xC0);
                        scsi_cmd_set(udev, &sc, 9, sizeof(dvdstruct));
                        scsi_cmd_set(udev, &sc, 11, 0);
                        err = scsi_cmd_run(udev, &sc, fd, dvdstruct, sizeof(dvdstruct));
                        if ((err != 0)) {
                                info_scsi_cmd_err(udev, "READ DVD STRUCTURE", err);
                                return -1;
                        }
                        if (dvdstruct[4] & 0x02) {
                                cd_media_state = media_status[2];
                                info(udev, "write-protected DVD-RAM media inserted\n");
                                goto determined;
                        }

                        /* let's make sure we don't try to read unformatted media */
                        scsi_cmd_init(udev, &sc);
                        scsi_cmd_set(udev, &sc, 0, 0x23);
                        scsi_cmd_set(udev, &sc, 8, sizeof(format));
                        scsi_cmd_set(udev, &sc, 9, 0);
                        err = scsi_cmd_run(udev, &sc, fd, format, sizeof(format));
                        if ((err != 0)) {
                                info_scsi_cmd_err(udev, "READ DVD FORMAT CAPACITIES", err);
                                return -1;
                        }

                        len = format[3];
                        if (len & 7 || len < 16) {
                                info(udev, "invalid format capacities length\n");
                                return -1;
                        }

                        switch(format[8] & 3) {
                            case 1:
                                info(udev, "unformatted DVD-RAM media inserted\n");
                                /* This means that last format was interrupted
                                 * or failed, blank dvd-ram discs are factory
                                 * formatted. Take no action here as it takes
                                 * quite a while to reformat a dvd-ram and it's
                                 * not automatically started */
                                goto determined;

                            case 2:
                                info(udev, "formatted DVD-RAM media inserted\n");
                                break;

                            case 3:
                                cd_media = 0; //return no media
                                info(udev, "format capacities returned no media\n");
                                return -1;
                        }
                }

                /* Take a closer look at formatted media (unformatted DVD+RW
                 * has "blank" status", DVD-RAM was examined earlier) and check
                 * for ISO and UDF PVDs or a fs superblock presence and do it
                 * in one ioctl (we need just sectors 0 and 16) */
                scsi_cmd_init(udev, &sc);
                scsi_cmd_set(udev, &sc, 0, 0x28);
                scsi_cmd_set(udev, &sc, 5, 0);
                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)) {
                        cd_media = 0;
                        info_scsi_cmd_err(udev, "READ FIRST 32 BLOCKS", err);
                        return -1;
                }

                /* if any non-zero data is found in sector 16 (iso and udf) or
                 * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
                 * is assumed non-blank */
                result = 0;

                for (block = 32768; block >= 0 && !result; block -= 32768) {
                        offset = block;
                        while (offset < (block + 2048) && !result) {
                                result = buffer [offset];
                                offset++;
                        }
                }

                if (!result) {
                        cd_media_state = media_status[0];
                        info(udev, "no data in blocks 0 or 16, assuming blank\n");
                } else {
                        info(udev, "data in blocks 0 or 16, assuming complete\n");
                }
        }

determined:
        /* "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];

        return 0;
}

static int cd_media_toc(struct udev *udev, int fd)
{
        struct scsi_cmd sc;
        unsigned char header[12];
        unsigned char toc[65536];
        unsigned int len, i, num_tracks;
        unsigned char *p;
        int err;

        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x43);
        scsi_cmd_set(udev, &sc, 6, 1);
        scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
        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 TOC", err);
                return -1;
        }

        len = (header[0] << 8 | header[1]) + 2;
        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)
                return 0;

        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x43);
        scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
        scsi_cmd_set(udev, &sc, 7, (len >> 8) & 0xff);
        scsi_cmd_set(udev, &sc, 8, len & 0xff);
        scsi_cmd_set(udev, &sc, 9, 0);
        err = scsi_cmd_run(udev, &sc, fd, toc, len);
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
                return -1;
        }

        /* 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;

                is_data_track = (p[1] & 0x04) != 0;

                block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
                info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
                     p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);

                if (is_data_track)
                        cd_media_track_count_data++;
                else
                        cd_media_track_count_audio++;
        }

        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x43);
        scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
        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 TOC (multi session)", err);
                return -1;
        }
        len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
        info(udev, "last track %u starts at block %u\n", header[4+2], len);
        cd_media_session_last_offset = (unsigned long long int)len * 2048;
        return 0;
}

int main(int argc, char *argv[])
{
        struct udev *udev;
        static const struct option options[] = {
                { "lock-media", no_argument, NULL, 'l' },
                { "unlock-media", no_argument, NULL, 'u' },
                { "eject-media", no_argument, NULL, 'e' },
                { "debug", no_argument, NULL, 'd' },
                { "help", no_argument, NULL, 'h' },
                {}
        };
        bool eject = false;
        bool lock = false;
        bool unlock = false;
        const char *node = NULL;
        int fd = -1;
        int cnt;
        int rc = 0;

        udev = udev_new();
        if (udev == NULL)
                goto exit;

        udev_log_init("cdrom_id");
        udev_set_log_fn(udev, log_fn);

        while (1) {
                int option;

                option = getopt_long(argc, argv, "deluh", options, NULL);
                if (option == -1)
                        break;

                switch (option) {
                case 'l':
                        lock = true;
                        break;
                case 'u':
                        unlock = true;
                        break;
                case 'e':
                        eject = true;
                        break;
                case 'd':
                        debug = true;
                        if (udev_get_log_priority(udev) < LOG_INFO)
                                udev_set_log_priority(udev, LOG_INFO);
                        break;
                case 'h':
                        printf("Usage: cdrom_id [options] <device>\n"
                               "  --lock-media    lock the media (to enable eject request events)\n"
                               "  --unlock-media  unlock the media\n"
                               "  --eject-media   eject the media\n"
                               "  --debug         debug to stderr\n"
                               "  --help          print this help text\n\n");
                        goto exit;
                default:
                        rc = 1;
                        goto exit;
                }
        }

        node = argv[optind];
        if (!node) {
                err(udev, "no device\n");
                fprintf(stderr, "no device\n");
                rc = 1;
                goto exit;
        }

        srand((unsigned int)getpid());
        for (cnt = 20; cnt > 0; cnt--) {
                struct timespec duration;

                fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
                if (fd >= 0 || errno != EBUSY)
                        break;
                duration.tv_sec = 0;
                duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
                nanosleep(&duration, NULL);
        }
        if (fd < 0) {
                info(udev, "unable to open '%s'\n", node);
                fprintf(stderr, "unable to open '%s'\n", node);
                rc = 1;
                goto exit;
        }
        info(udev, "probing: '%s'\n", node);

        /* same data as original cdrom_id */
        if (cd_capability_compat(udev, fd) < 0) {
                rc = 1;
                goto exit;
        }

        /* check for media - don't bail if there's no media as we still need to
         * to read profiles */
        cd_media_compat(udev, fd);

        /* check if drive talks MMC */
        if (cd_inquiry(udev, fd) < 0)
                goto work;

        /* read drive and possibly current profile */
        if (cd_profiles(udev, fd) != 0)
                goto work;

        /* at this point we are guaranteed to have media in the drive - find out more about it */

        /* get session/track info */
        cd_media_toc(udev, fd);

        /* get writable media state */
        cd_media_info(udev, fd);

work:
        /* lock the media, so we enable eject button events */
        if (lock && cd_media) {
                info(udev, "PREVENT_ALLOW_MEDIUM_REMOVAL (lock)\n");
                media_lock(udev, fd, true);
        }

        if (unlock && cd_media) {
                info(udev, "PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n");
                media_lock(udev, fd, false);
        }

        if (eject) {
                info(udev, "PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n");
                media_lock(udev, fd, false);
                info(udev, "START_STOP_UNIT (eject)\n");
                media_eject(udev, fd);
        }

        printf("ID_CDROM=1\n");
        if (cd_cd_rom)
                printf("ID_CDROM_CD=1\n");
        if (cd_cd_r)
                printf("ID_CDROM_CD_R=1\n");
        if (cd_cd_rw)
                printf("ID_CDROM_CD_RW=1\n");
        if (cd_dvd_rom)
                printf("ID_CDROM_DVD=1\n");
        if (cd_dvd_r)
                printf("ID_CDROM_DVD_R=1\n");
        if (cd_dvd_rw)
                printf("ID_CDROM_DVD_RW=1\n");
        if (cd_dvd_ram)
                printf("ID_CDROM_DVD_RAM=1\n");
        if (cd_dvd_plus_r)
                printf("ID_CDROM_DVD_PLUS_R=1\n");
        if (cd_dvd_plus_rw)
                printf("ID_CDROM_DVD_PLUS_RW=1\n");
        if (cd_dvd_plus_r_dl)
                printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
        if (cd_dvd_plus_rw_dl)
                printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
        if (cd_bd)
                printf("ID_CDROM_BD=1\n");
        if (cd_bd_r)
                printf("ID_CDROM_BD_R=1\n");
        if (cd_bd_re)
                printf("ID_CDROM_BD_RE=1\n");
        if (cd_hddvd)
                printf("ID_CDROM_HDDVD=1\n");
        if (cd_hddvd_r)
                printf("ID_CDROM_HDDVD_R=1\n");
        if (cd_hddvd_rw)
                printf("ID_CDROM_HDDVD_RW=1\n");
        if (cd_mo)
                printf("ID_CDROM_MO=1\n");
        if (cd_mrw)
                printf("ID_CDROM_MRW=1\n");
        if (cd_mrw_w)
                printf("ID_CDROM_MRW_W=1\n");

        if (cd_media)
                printf("ID_CDROM_MEDIA=1\n");
        if (cd_media_mo)
                printf("ID_CDROM_MEDIA_MO=1\n");
        if (cd_media_mrw)
                printf("ID_CDROM_MEDIA_MRW=1\n");
        if (cd_media_mrw_w)
                printf("ID_CDROM_MEDIA_MRW_W=1\n");
        if (cd_media_cd_rom)
                printf("ID_CDROM_MEDIA_CD=1\n");
        if (cd_media_cd_r)
                printf("ID_CDROM_MEDIA_CD_R=1\n");
        if (cd_media_cd_rw)
                printf("ID_CDROM_MEDIA_CD_RW=1\n");
        if (cd_media_dvd_rom)
                printf("ID_CDROM_MEDIA_DVD=1\n");
        if (cd_media_dvd_r)
                printf("ID_CDROM_MEDIA_DVD_R=1\n");
        if (cd_media_dvd_ram)
                printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
        if (cd_media_dvd_rw)
                printf("ID_CDROM_MEDIA_DVD_RW=1\n");
        if (cd_media_dvd_plus_r)
                printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
        if (cd_media_dvd_plus_rw)
                printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
        if (cd_media_dvd_plus_rw_dl)
                printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
        if (cd_media_dvd_plus_r_dl)
                printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
        if (cd_media_bd)
                printf("ID_CDROM_MEDIA_BD=1\n");
        if (cd_media_bd_r)
                printf("ID_CDROM_MEDIA_BD_R=1\n");
        if (cd_media_bd_re)
                printf("ID_CDROM_MEDIA_BD_RE=1\n");
        if (cd_media_hddvd)
                printf("ID_CDROM_MEDIA_HDDVD=1\n");
        if (cd_media_hddvd_r)
                printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
        if (cd_media_hddvd_rw)
                printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");

        if (cd_media_state != NULL)
                printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
        if (cd_media_session_next > 0)
                printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
        if (cd_media_session_count > 0)
                printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
        if (cd_media_session_count > 1 && cd_media_session_last_offset > 0)
                printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
        if (cd_media_track_count > 0)
                printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
        if (cd_media_track_count_audio > 0)
                printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
        if (cd_media_track_count_data > 0)
                printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);
exit:
        if (fd >= 0)
                close(fd);
        udev_unref(udev);
        udev_log_close();
        return rc;
}