1 /* $NetBSD: main.c,v 1.6 2018/01/23 21:06:25 sevan Exp $ */
2
3 /*
4 * Copyright (c) 2002, 2005
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 /*
30 * Generate an in-core disklabel for a CD, containing entries for
31 * previous data tracks (supposed to be of previous sessions).
32 * TODO:
33 * - support simulation of multisession CDs in a vnd(4) disk
34 */
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/cdio.h>
39 #include <sys/disklabel.h>
40 #include <sys/ioctl.h>
41 #include <sys/stat.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <err.h>
48 #include <util.h>
49 #include <string.h>
50
51 #include "dkcksum.h"
52 #include "mscdlabel.h"
53
54 static int getcdtoc(int);
55 static int getfaketoc(int);
56
57 const char *disk = "cd0";
58 int ntracks;
59 struct cd_toc_entry *tocbuf;
60
61 static int
getcdtoc(int fd)62 getcdtoc(int fd)
63 {
64 int res;
65 struct ioc_toc_header th;
66 struct ioc_read_toc_entry te;
67 size_t tocbufsize;
68
69 memset(&th, 0, sizeof(th));
70 res = ioctl(fd, CDIOREADTOCHEADER, &th);
71 if (res < 0) {
72 warn("CDIOREADTOCHEADER");
73 return (-1);
74 }
75
76 ntracks = th.ending_track - th.starting_track + 1;
77 /* one more for leadout track, for tracklen calculation */
78 tocbufsize = (ntracks + 1) * sizeof(struct cd_toc_entry);
79 tocbuf = malloc(tocbufsize);
80 if (!tocbuf)
81 err(3, "alloc TOC buffer");
82 memset(&te, 0, sizeof(te));
83 te.address_format = CD_LBA_FORMAT;
84 te.starting_track = th.starting_track; /* always 1 ??? */
85 te.data_len = tocbufsize;
86 te.data = tocbuf;
87 res = ioctl(fd, CDIOREADTOCENTRIES, &te);
88 if (res < 0)
89 err(4, "CDIOREADTOCENTRIES");
90 return (0);
91 }
92
93 static int
getfaketoc(int fd)94 getfaketoc(int fd)
95 {
96 int res;
97 struct stat st;
98
99 res = fstat(fd, &st);
100 if (res < 0)
101 err(4, "fstat");
102
103 if (st.st_size % 2048) {
104 warnx("size not multiple of 2048");
105 return (-1);
106 }
107
108 tocbuf = malloc(2 * sizeof(struct cd_toc_entry));
109 if (!tocbuf)
110 err(3, "alloc TOC buffer");
111
112 /*
113 * fake up a data track spanning the whole file and a leadout track,
114 * just as much as necessary for the scan below
115 */
116 tocbuf[0].addr.lba = 0;
117 tocbuf[0].control = 4;
118 tocbuf[1].addr.lba = st.st_size / 2048;
119 tocbuf[1].control = 0;
120 ntracks = 1;
121 return (0);
122 }
123
124 int
main(int argc,char * argv[])125 main(int argc, char *argv[])
126 {
127 int fd, res, i, j, rawpart;
128 char fullname[MAXPATHLEN];
129 struct cd_toc_entry *track;
130 struct disklabel label;
131 struct partition *p;
132 int readonly = 0;
133
134 if (argc > 1)
135 disk = argv[1];
136
137 fd = opendisk(disk, O_RDWR, fullname, MAXPATHLEN, 0);
138 if (fd < 0) {
139 warn("opendisk (read-write) %s", disk);
140 fd = opendisk(disk, O_RDONLY, fullname, MAXPATHLEN, 0);
141 if (fd < 0)
142 err(1, "opendisk %s", disk);
143 readonly = 1;
144 }
145
146 /*
147 * Get the TOC: first try to read a real one from a CD drive.
148 * If this fails we might have something else keeping an ISO image
149 * (eg. vnd(4) or plain file).
150 */
151 if (getcdtoc(fd) < 0 && getfaketoc(fd) < 0)
152 exit(2);
153
154 /*
155 * Get label template. If this fails we might have a plain file.
156 * Proceed to print out possible ISO label information, but
157 * don't try to write a label back.
158 */
159 res = ioctl(fd, DIOCGDINFO, &label);
160 if (res < 0) {
161 warn("DIOCGDINFO");
162 readonly = 1;
163 }
164
165 /*
166 * We want entries for the sessions beginning with the most recent
167 * one, in reversed time order.
168 * We don't have session information here, but it is uncommon
169 * to have more than one data track in one session, so we get
170 * the same result.
171 */
172 if ((rawpart = getrawpartition()) == -1)
173 err(1, "Cannot get raw partition");
174 i = ntracks;
175 j = 0;
176 while (--i >= 0 && j < MAXPARTITIONS) {
177 track = &tocbuf[i];
178 printf("track (ctl=%d) at sector %d\n", track->control,
179 track->addr.lba);
180 if ((track->control & 4) /* data track */
181 && check_primary_vd(fd, track->addr.lba,
182 (track+1)->addr.lba - track->addr.lba)) {
183 printf(" adding as '%c'\n", 'a' + j);
184 p = &label.d_partitions[j];
185 memset(p, 0, sizeof(struct partition));
186 p->p_size = label.d_partitions[rawpart].p_size;
187 p->p_fstype = FS_ISO9660;
188 p->p_cdsession = track->addr.lba;
189 if (++j == rawpart)
190 j++;
191 }
192 }
193 if (!j) /* no ISO track, let the label alone */
194 readonly = 1;
195
196 if (!readonly) {
197 /* write back label */
198 if (label.d_npartitions < j)
199 label.d_npartitions = j;
200 strncpy(label.d_packname, "mscdlabel's", 16);
201 label.d_checksum = 0;
202 label.d_checksum = dkcksum(&label);
203 res = ioctl(fd, DIOCSDINFO, &label);
204 if (res < 0)
205 err(6, "DIOCSDINFO");
206 } else
207 printf("disklabel not written\n");
208
209 free(tocbuf);
210 close(fd);
211 return (0);
212 }
213