xref: /netbsd/usr.sbin/mscdlabel/main.c (revision c4a72b64)
1 /* $NetBSD: main.c,v 1.1 2002/05/29 19:39:07 drochner Exp $ */
2 
3 /*
4  * Copyright (c) 2002
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  *  - check ISO9660 volume descriptor, print volume labels, perhaps
34  *    allow selection by creation date
35  *  - support simulation of multisession CDs in a vnd(4) disk
36  */
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <sys/ioctl.h>
43 #include <sys/cdio.h>
44 #include <sys/disklabel.h>
45 #include <sys/param.h>
46 #include <err.h>
47 #include <util.h>
48 #include <string.h>
49 
50 #include "dkcksum.h"
51 
52 int main(int, char **);
53 
54 char *disk = "cd0";
55 
56 int
57 main(argc, argv)
58 	int argc;
59 	char **argv;
60 {
61 	int fd, res, i, j, rawpart;
62 	char fullname[MAXPATHLEN];
63 	struct ioc_toc_header th;
64 	struct ioc_read_toc_entry te;
65 	int ntracks;
66 	size_t tocbufsize;
67 	struct cd_toc_entry *tocbuf, *track;
68 	struct disklabel label;
69 	struct partition *p;
70 
71 	if (argc > 1)
72 		disk = argv[1];
73 
74 	fd = opendisk(disk, O_RDWR, fullname, MAXPATHLEN, 0);
75 	if (fd < 0)
76 		err(1, "opendisk %s", disk);
77 
78 	/*
79 	 * get the TOC
80 	 */
81 	memset(&th, 0, sizeof(th));
82 	res = ioctl(fd, CDIOREADTOCHEADER, &th);
83 	if (res < 0)
84 		err(2, "CDIOREADTOCHEADER");
85 	ntracks = th.ending_track - th.starting_track + 1;
86 	tocbufsize = ntracks * sizeof(struct cd_toc_entry);
87 	tocbuf = malloc(tocbufsize);
88 	if (!tocbuf)
89 		err(3, "alloc TOC buffer");
90 	memset(&te, 0, sizeof(te));
91 	te.address_format = CD_LBA_FORMAT;
92 	te.starting_track = th.starting_track; /* always 1 ??? */
93 	te.data_len = tocbufsize;
94 	te.data = tocbuf;
95 	res = ioctl(fd, CDIOREADTOCENTRIES, &te);
96 	if (res < 0)
97 		err(4, "CDIOREADTOCENTRIES");
98 
99 	/* get label template */
100 	res = ioctl(fd, DIOCGDINFO, &label);
101 	if (res < 0)
102 		err(5, "DIOCGDINFO");
103 
104 	/*
105 	 * We want entries for the sessions beginning with the most recent
106 	 * one, in reversed time order.
107 	 * We don't have session information here, but it is uncommon
108 	 * to have more than one data track in one session, so we get
109 	 * the same result.
110 	 */
111 	rawpart = getrawpartition();
112 	i = ntracks;
113 	j = 0;
114 	while (--i >= 0 && j < MAXPARTITIONS) {
115 		track = &tocbuf[i];
116 		printf("track (ctl=%d) at sector %d\n", track->control,
117 		       track->addr.lba);
118 		if (track->control & 4) { /* data track */
119 			printf(" adding as '%c'\n", 'a' + j);
120 			p = &label.d_partitions[j];
121 			memset(p, 0, sizeof(struct partition));
122 			p->p_size = label.d_partitions[rawpart].p_size;
123 			p->p_fstype = FS_ISO9660;
124 			p->p_cdsession = track->addr.lba;
125 			if (++j == rawpart)
126 				j++;
127 		}
128 	}
129 	if (label.d_npartitions < j)
130 		label.d_npartitions = j;
131 	strncpy(label.d_packname, "mscdlabel's", 16);
132 
133 	/* write back label */
134 	label.d_checksum = 0;
135 	label.d_checksum = dkcksum(&label);
136 	res = ioctl(fd, DIOCSDINFO, &label);
137 	if (res < 0)
138 		err(6, "DIOCSDINFO");
139 
140 	free(tocbuf);
141 	close(fd);
142 	return (0);
143 }
144