1/* $NetBSD: main.c,v 1.3 2005/07/25 11:26:40 drochner 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 <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 51static int getcdtoc(int); 52static int getfaketoc(int); 53int main(int, char **); 54 55const char *disk = "cd0"; 56int ntracks; 57struct cd_toc_entry *tocbuf; 58 59static int 60getcdtoc(int fd) 61{ 62 int res; 63 struct ioc_toc_header th; 64 struct ioc_read_toc_entry te; 65 size_t tocbufsize; 66 67 memset(&th, 0, sizeof(th)); 68 res = ioctl(fd, CDIOREADTOCHEADER, &th); 69 if (res < 0) { 70 warn("CDIOREADTOCHEADER"); 71 return (-1); 72 } 73 74 ntracks = th.ending_track - th.starting_track + 1; 75 /* one more for leadout track, for tracklen calculation */ 76 tocbufsize = (ntracks + 1) * sizeof(struct cd_toc_entry); 77 tocbuf = malloc(tocbufsize); 78 if (!tocbuf) 79 err(3, "alloc TOC buffer"); 80 memset(&te, 0, sizeof(te)); 81 te.address_format = CD_LBA_FORMAT; 82 te.starting_track = th.starting_track; /* always 1 ??? */ 83 te.data_len = tocbufsize; 84 te.data = tocbuf; 85 res = ioctl(fd, CDIOREADTOCENTRIES, &te); 86 if (res < 0) 87 err(4, "CDIOREADTOCENTRIES"); 88 return (0); 89} 90 91static int 92getfaketoc(int fd) 93{ 94 int res; 95 struct stat st; 96 97 res = fstat(fd, &st); 98 if (res < 0) 99 err(4, "fstat"); 100 101 if (st.st_size % 2048) { 102 warnx("size not multiple of 2048"); 103 return (-1); 104 } 105 106 tocbuf = malloc(2 * sizeof(struct cd_toc_entry)); 107 if (!tocbuf) 108 err(3, "alloc TOC buffer"); 109 110 /* 111 * fake up a data track spanning the whole file and a leadout track, 112 * just as much as necessary for the scan below 113 */ 114 tocbuf[0].addr.lba = 0; 115 tocbuf[0].control = 4; 116 tocbuf[1].addr.lba = st.st_size / 2048; 117 tocbuf[1].control = 0; 118 ntracks = 1; 119 return (0); 120} 121 122int 123main(argc, argv) 124 int argc; 125 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