5872
|
1
|
|
2 #include <sys/types.h>
|
|
3 #include <sys/inttypes.h>
|
|
4 #include <sys/cdio.h>
|
|
5 #include <sys/scsiio.h>
|
|
6
|
|
7 #define CDROM_LEADOUT 0xAA
|
|
8
|
|
9 static struct ioc_read_toc_entry vcd_entry;
|
|
10 static struct cd_toc_entry vcd_entry_data;
|
|
11 static char vcd_buf[VCD_SECTOR_SIZE];
|
|
12
|
|
13 static inline void
|
|
14 vcd_set_msf(unsigned int sect)
|
|
15 {
|
|
16 unsigned int s = sect;
|
|
17 vcd_entry_data.addr.msf.frame = sect % 75;
|
|
18 sect = sect / 75;
|
|
19 vcd_entry_data.addr.msf.second = sect % 60;
|
|
20 sect = sect / 60;
|
|
21 vcd_entry_data.addr.msf.minute = sect;
|
|
22 }
|
|
23
|
|
24 static inline unsigned int
|
|
25 vcd_get_msf()
|
|
26 {
|
|
27 return vcd_entry_data.addr.msf.frame +
|
|
28 (vcd_entry_data.addr.msf.second +
|
|
29 vcd_entry_data.addr.msf.minute * 60) * 75;
|
|
30 }
|
|
31
|
|
32 int
|
|
33 vcd_seek_to_track(int fd, int track)
|
|
34 {
|
|
35 vcd_entry.address_format = CD_MSF_FORMAT;
|
|
36 vcd_entry.starting_track = track;
|
|
37 vcd_entry.data_len = sizeof(struct cd_toc_entry);
|
|
38 vcd_entry.data = &vcd_entry_data;
|
|
39 if (ioctl(fd, CDIOREADTOCENTRIES, &vcd_entry)) {
|
|
40 perror("ioctl dif1");
|
|
41 return -1;
|
|
42 }
|
|
43 return VCD_SECTOR_DATA * vcd_get_msf();
|
|
44 }
|
|
45
|
|
46 int
|
|
47 vcd_get_track_end(int fd, int track)
|
|
48 {
|
|
49 struct ioc_toc_header tochdr;
|
|
50 if (ioctl(fd, CDIOREADTOCHEADER, &tochdr) == -1) {
|
|
51 perror("read CDROM toc header: ");
|
|
52 return -1;
|
|
53 }
|
|
54 vcd_entry.address_format = CD_MSF_FORMAT;
|
|
55 vcd_entry.starting_track = track < tochdr.ending_track ? (track + 1) : CDROM_LEADOUT;
|
|
56 vcd_entry.data_len = sizeof(struct cd_toc_entry);
|
|
57 vcd_entry.data = &vcd_entry_data;
|
|
58 if (ioctl(fd, CDIOREADTOCENTRYS, &vcd_entry)) {
|
|
59 perror("ioctl dif2");
|
|
60 return -1;
|
|
61 }
|
|
62 return VCD_SECTOR_DATA * vcd_get_msf();
|
|
63 }
|
|
64
|
|
65 void
|
|
66 vcd_read_toc(int fd)
|
|
67 {
|
|
68 struct ioc_toc_header tochdr;
|
|
69 int i;
|
|
70 if (ioctl(fd, CDIOREADTOCHEADER, &tochdr) == -1) {
|
|
71 perror("read CDROM toc header: ");
|
|
72 return;
|
|
73 }
|
|
74 for (i = tochdr.starting_track; i <= tochdr.ending_track; i++) {
|
|
75 struct ioc_read_toc_entry tocentry;
|
|
76 struct cd_toc_entry tocentry_data;
|
|
77
|
|
78 tocentry.starting_track = i;
|
|
79 tocentry.address_format = CD_MSF_FORMAT;
|
|
80 tocentry.data_len = sizeof(struct cd_toc_entry);
|
|
81 tocentry.data = &tocentry_data;
|
|
82
|
|
83 if (ioctl(fd, CDIOREADTOCENTRYS, &tocentry) == -1) {
|
|
84 perror("read CDROM toc entry: ");
|
|
85 return;
|
|
86 }
|
|
87 printf("track %02d: adr=%d ctrl=%d format=%d %02d:%02d:%02d\n",
|
|
88 (int) tocentry.starting_track,
|
|
89 (int) tocentry.data->addr_type,
|
|
90 (int) tocentry.data->control,
|
|
91 (int) tocentry.address_format,
|
|
92 (int) tocentry.data->addr.msf.minute,
|
|
93 (int) tocentry.data->addr.msf.second,
|
|
94 (int) tocentry.data->addr.msf.frame
|
|
95 );
|
|
96 }
|
|
97 }
|
|
98
|
|
99 static int
|
|
100 vcd_read(int fd, char *mem)
|
|
101 {
|
|
102 struct scsireq sc;
|
|
103 int lba = vcd_get_msf();
|
|
104 int blocks;
|
|
105 int sector_type;
|
|
106 int sync, header_code, user_data, edc_ecc, error_field;
|
|
107 int sub_channel;
|
|
108 int rc;
|
|
109
|
|
110 blocks = 1;
|
|
111 sector_type = 5; /* mode2/form2 */
|
|
112 sync = 0;
|
|
113 header_code = 0;
|
|
114 user_data = 1;
|
|
115 edc_ecc = 0;
|
|
116 error_field = 0;
|
|
117 sub_channel = 0;
|
|
118
|
|
119 memset(&sc, 0, sizeof(sc));
|
|
120 sc.cmd[0] = 0xBE;
|
|
121 sc.cmd[1] = (sector_type) << 2;
|
|
122 sc.cmd[2] = (lba >> 24) & 0xff;
|
|
123 sc.cmd[3] = (lba >> 16) & 0xff;
|
|
124 sc.cmd[4] = (lba >> 8) & 0xff;
|
|
125 sc.cmd[5] = lba & 0xff;
|
|
126 sc.cmd[6] = (blocks >> 16) & 0xff;
|
|
127 sc.cmd[7] = (blocks >> 8) & 0xff;
|
|
128 sc.cmd[8] = blocks & 0xff;
|
|
129 sc.cmd[9] = (sync << 7) | (header_code << 5) | (user_data << 4) |
|
|
130 (edc_ecc << 3) | (error_field << 1);
|
|
131 sc.cmd[10] = sub_channel;
|
|
132 sc.cmdlen = 12;
|
|
133 sc.databuf = (caddr_t) mem;
|
|
134 sc.datalen = 2328;
|
|
135 sc.senselen = sizeof(sc.sense);
|
|
136 sc.flags = SCCMD_READ;
|
|
137 sc.timeout = 10000;
|
|
138 rc = ioctl(fd, SCIOCCOMMAND, &sc);
|
|
139 if (rc == -1) {
|
|
140 perror("SCIOCCOMMAND");
|
|
141 return -1;
|
|
142 }
|
|
143 if (sc.retsts || sc.error) {
|
|
144 fprintf(stderr, "scsi command failed: status %d error %d\n", sc.retsts,
|
|
145 sc.error);
|
|
146 return -1;
|
|
147 }
|
|
148 return VCD_SECTOR_DATA;
|
|
149 }
|
|
150
|
|
151 #ifdef VCD_CACHE
|
|
152
|
|
153 static int vcd_cache_size = 0;
|
|
154 static char *vcd_cache_data = NULL;
|
|
155 static int *vcd_cache_sectors = NULL;
|
|
156 static int vcd_cache_index = 0;
|
|
157 static int vcd_cache_current = -1;
|
|
158
|
|
159 void
|
|
160 vcd_cache_init(int s)
|
|
161 {
|
|
162 vcd_cache_size = s;
|
|
163 vcd_cache_sectors = malloc(s * sizeof(int));
|
|
164 vcd_cache_data = malloc(s * VCD_SECTOR_SIZE);
|
|
165 memset(vcd_cache_sectors, 255, s * sizeof(int));
|
|
166 }
|
|
167
|
|
168 static inline void
|
|
169 vcd_cache_seek(int sect)
|
|
170 {
|
|
171 vcd_cache_current = sect;
|
|
172 }
|
|
173
|
|
174 int
|
|
175 vcd_cache_read(int fd, char *mem)
|
|
176 {
|
|
177 int i;
|
|
178 char *vcd_buf;
|
|
179 for (i = 0; i < vcd_cache_size; i++)
|
|
180 if (vcd_cache_sectors[i] == vcd_cache_current) {
|
|
181 vcd_buf = &vcd_cache_data[i * VCD_SECTOR_SIZE];
|
|
182 ++vcd_cache_current;
|
|
183 memcpy(mem, &vcd_buf[VCD_SECTOR_OFFS], VCD_SECTOR_DATA);
|
|
184 return VCD_SECTOR_DATA;
|
|
185 }
|
|
186 vcd_buf = &vcd_cache_data[vcd_cache_index * VCD_SECTOR_SIZE];
|
|
187 vcd_cache_sectors[vcd_cache_index] = vcd_cache_current;
|
|
188 ++vcd_cache_index;
|
|
189 if (vcd_cache_index >= vcd_cache_size)
|
|
190 vcd_cache_index = 0;
|
|
191 vcd_set_msf(vcd_cache_current);
|
|
192 memcpy(vcd_buf, &vcd_entry_data.addr.msf, sizeof(vcd_entry_data.addr.msf));
|
|
193 ++vcd_cache_current;
|
|
194 memcpy(mem, &vcd_buf[VCD_SECTOR_OFFS], VCD_SECTOR_DATA);
|
|
195 return VCD_SECTOR_DATA;
|
|
196 }
|
|
197 #endif
|
|
198
|