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