xref: /netbsd-src/usr.sbin/mscdlabel/main.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /* $NetBSD: main.c,v 1.5 2017/01/10 21:03:36 christos 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 int main(int, char **);
57 
58 const char *disk = "cd0";
59 int ntracks;
60 struct cd_toc_entry *tocbuf;
61 
62 static int
63 getcdtoc(int fd)
64 {
65 	int res;
66 	struct ioc_toc_header th;
67 	struct ioc_read_toc_entry te;
68 	size_t tocbufsize;
69 
70 	memset(&th, 0, sizeof(th));
71 	res = ioctl(fd, CDIOREADTOCHEADER, &th);
72 	if (res < 0) {
73 		warn("CDIOREADTOCHEADER");
74 		return (-1);
75 	}
76 
77 	ntracks = th.ending_track - th.starting_track + 1;
78 	/* one more for leadout track, for tracklen calculation */
79 	tocbufsize = (ntracks + 1) * sizeof(struct cd_toc_entry);
80 	tocbuf = malloc(tocbufsize);
81 	if (!tocbuf)
82 		err(3, "alloc TOC buffer");
83 	memset(&te, 0, sizeof(te));
84 	te.address_format = CD_LBA_FORMAT;
85 	te.starting_track = th.starting_track; /* always 1 ??? */
86 	te.data_len = tocbufsize;
87 	te.data = tocbuf;
88 	res = ioctl(fd, CDIOREADTOCENTRIES, &te);
89 	if (res < 0)
90 		err(4, "CDIOREADTOCENTRIES");
91 	return (0);
92 }
93 
94 static int
95 getfaketoc(int fd)
96 {
97 	int res;
98 	struct stat st;
99 
100 	res = fstat(fd, &st);
101 	if (res < 0)
102 		err(4, "fstat");
103 
104 	if (st.st_size % 2048) {
105 		warnx("size not multiple of 2048");
106 		return (-1);
107 	}
108 
109 	tocbuf = malloc(2 * sizeof(struct cd_toc_entry));
110 	if (!tocbuf)
111 		err(3, "alloc TOC buffer");
112 
113 	/*
114 	 * fake up a data track spanning the whole file and a leadout track,
115 	 * just as much as necessary for the scan below
116 	 */
117 	tocbuf[0].addr.lba = 0;
118 	tocbuf[0].control = 4;
119 	tocbuf[1].addr.lba = st.st_size / 2048;
120 	tocbuf[1].control = 0;
121 	ntracks = 1;
122 	return (0);
123 }
124 
125 int
126 main(argc, argv)
127 	int argc;
128 	char **argv;
129 {
130 	int fd, res, i, j, rawpart;
131 	char fullname[MAXPATHLEN];
132 	struct cd_toc_entry *track;
133 	struct disklabel label;
134 	struct partition *p;
135 	int readonly = 0;
136 
137 	if (argc > 1)
138 		disk = argv[1];
139 
140 	fd = opendisk(disk, O_RDWR, fullname, MAXPATHLEN, 0);
141 	if (fd < 0) {
142 		warn("opendisk (read-write) %s", disk);
143 		fd = opendisk(disk, O_RDONLY, fullname, MAXPATHLEN, 0);
144 		if (fd < 0)
145 			err(1, "opendisk %s", disk);
146 		readonly = 1;
147 	}
148 
149 	/*
150 	 * Get the TOC: first try to read a real one from a CD drive.
151 	 * If this fails we might have something else keeping an ISO image
152 	 * (eg. vnd(4) or plain file).
153 	 */
154 	if (getcdtoc(fd) < 0 && getfaketoc(fd) < 0)
155 		exit(2);
156 
157 	/*
158 	 * Get label template. If this fails we might have a plain file.
159 	 * Proceed to print out possible ISO label information, but
160 	 * don't try to write a label back.
161 	 */
162 	res = ioctl(fd, DIOCGDINFO, &label);
163 	if (res < 0) {
164 		warn("DIOCGDINFO");
165 		readonly = 1;
166 	}
167 
168 	/*
169 	 * We want entries for the sessions beginning with the most recent
170 	 * one, in reversed time order.
171 	 * We don't have session information here, but it is uncommon
172 	 * to have more than one data track in one session, so we get
173 	 * the same result.
174 	 */
175 	if ((rawpart = getrawpartition()) == -1)
176 		err(1, "Cannot get raw partition");
177 	i = ntracks;
178 	j = 0;
179 	while (--i >= 0 && j < MAXPARTITIONS) {
180 		track = &tocbuf[i];
181 		printf("track (ctl=%d) at sector %d\n", track->control,
182 		       track->addr.lba);
183 		if ((track->control & 4) /* data track */
184 		    && check_primary_vd(fd, track->addr.lba,
185 		      (track+1)->addr.lba - track->addr.lba)) {
186 			printf(" adding as '%c'\n", 'a' + j);
187 			p = &label.d_partitions[j];
188 			memset(p, 0, sizeof(struct partition));
189 			p->p_size = label.d_partitions[rawpart].p_size;
190 			p->p_fstype = FS_ISO9660;
191 			p->p_cdsession = track->addr.lba;
192 			if (++j == rawpart)
193 				j++;
194 		}
195 	}
196 	if (!j) /* no ISO track, let the label alone */
197 		readonly = 1;
198 
199 	if (!readonly) {
200 		/* write back label */
201 		if (label.d_npartitions < j)
202 			label.d_npartitions = j;
203 		strncpy(label.d_packname, "mscdlabel's", 16);
204 		label.d_checksum = 0;
205 		label.d_checksum = dkcksum(&label);
206 		res = ioctl(fd, DIOCSDINFO, &label);
207 		if (res < 0)
208 			err(6, "DIOCSDINFO");
209 	} else
210 		printf("disklabel not written\n");
211 
212 	free(tocbuf);
213 	close(fd);
214 	return (0);
215 }
216