1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 1989, 1990 by Sun Microsystems, Inc. 24 * 25 */ 26 #ifndef lint 27 #ident "%Z%%M% %I% %E% SMI" 28 #endif lint 29 30 #include <fcntl.h> 31 #include <stdio.h> 32 #include <sys/param.h> 33 #include <sys/stat.h> 34 #include <sys/time.h> 35 #include <sys/types.h> 36 #include <sys/file.h> 37 #include <sys/cdio.h> 38 #include <sys/dkio.h> 39 #include "hsfs_spec.h" 40 #include "iso_spec.h" 41 #include "iso_impl.h" 42 43 #define GETCDSECTOR(buf, secno, nosec) (getdisk(buf, \ 44 ((secno)+cdroff)*ISO_SECTOR_SIZE, \ 45 (nosec)*ISO_SECTOR_SIZE)) 46 char hs_buf[ISO_SECTOR_SIZE]; 47 int hs_pvd_sec_no; 48 char iso_buf[ISO_SECTOR_SIZE]; 49 int iso_pvd_sec_no; 50 char unix_buf[ISO_SECTOR_SIZE]; 51 int unix_pvd_sec_no; 52 53 int vflag; 54 int cdfd; 55 extern int optind; 56 57 int cdroff = 0; 58 59 static int rdev_is_a_cd(int rdevfd); 60 61 main(argc, argv) 62 int argc; 63 char **argv; 64 { 65 register c; 66 char *special; 67 int errflag = 0; 68 69 while ((c=getopt(argc, argv, "v")) != EOF) { 70 switch (c) { 71 case 'v': 72 vflag++; 73 break; 74 default: 75 errflag++; 76 break; 77 } 78 } 79 80 if (errflag || (argc <= optind)) { 81 usage(); 82 exit(1); 83 } 84 85 special = argv[optind]; 86 87 dumpfs(special); 88 89 /* NOTREACHED */ 90 } 91 92 usage() 93 { 94 fprintf(stderr, "Usage: fstyp -v special\n"); 95 } 96 97 /* 98 * findhsvol: check if the disk is in high sierra format 99 * return(1) if found, (0) otherwise 100 * if found, volp will point to the descriptor 101 * 102 */ 103 int 104 findhsvol(volp) 105 char *volp; 106 { 107 int secno; 108 int i; 109 110 secno = HS_VOLDESC_SEC; 111 GETCDSECTOR(volp, secno++, 1); 112 while (HSV_DESC_TYPE(volp) != VD_EOV) { 113 for (i = 0; i < HSV_ID_STRLEN; i++) 114 if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i]) 115 goto cantfind; 116 if (HSV_STD_VER(volp) != HSV_ID_VER) 117 goto cantfind; 118 switch (HSV_DESC_TYPE(volp)) { 119 case VD_SFS: 120 hs_pvd_sec_no = secno-1; 121 return (1); 122 case VD_EOV: 123 goto cantfind; 124 } 125 GETCDSECTOR(volp, secno++, 1); 126 } 127 cantfind: 128 return (0); 129 } 130 131 /* 132 * findisovol: check if the disk is in ISO 9660 format 133 * return(1) if found, (0) otherwise 134 * if found, volp will point to the descriptor 135 * 136 */ 137 int 138 findisovol(volp) 139 char *volp; 140 { 141 int secno; 142 int i; 143 144 secno = ISO_VOLDESC_SEC; 145 GETCDSECTOR(volp, secno++, 1); 146 while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) { 147 for (i = 0; i < ISO_ID_STRLEN; i++) 148 if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i]) 149 goto cantfind; 150 if (ISO_STD_VER(volp) != ISO_ID_VER) 151 goto cantfind; 152 switch (ISO_DESC_TYPE(volp)) { 153 case ISO_VD_PVD: 154 iso_pvd_sec_no = secno-1; 155 return (1); 156 case ISO_VD_EOV: 157 goto cantfind; 158 } 159 GETCDSECTOR(volp, secno++, 1); 160 } 161 cantfind: 162 return (0); 163 } 164 165 /* 166 * findunixvol: check if the disk is in UNIX extension format 167 * return(1) if found, (0) otherwise 168 * if found, volp will point to the descriptor 169 * 170 */ 171 int 172 findunixvol(volp) 173 char *volp; 174 { 175 int secno; 176 int i; 177 178 secno = ISO_VOLDESC_SEC; 179 GETCDSECTOR(volp, secno++, 1); 180 while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) { 181 for (i = 0; i < ISO_ID_STRLEN; i++) 182 if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i]) 183 goto cantfind; 184 if (ISO_STD_VER(volp) != ISO_ID_VER) 185 goto cantfind; 186 switch (ISO_DESC_TYPE(volp)) { 187 case ISO_VD_UNIX: 188 unix_pvd_sec_no = secno-1; 189 return (1); 190 case ISO_VD_EOV: 191 goto cantfind; 192 } 193 GETCDSECTOR(volp, secno++, 1); 194 } 195 cantfind: 196 return (0); 197 } 198 199 ckvoldesc() 200 { 201 int cd_type; 202 203 if (findhsvol(hs_buf)) 204 cd_type = 0; 205 else if (findisovol(iso_buf)) { 206 if (findunixvol(unix_buf)) 207 cd_type = 2; 208 else cd_type = 1; 209 } 210 else cd_type = -1; 211 212 return(cd_type); 213 214 } 215 216 dumpfs(special) 217 char *special; 218 { 219 int err; 220 int cd_type; 221 222 if ((cdfd = open(special, O_RDONLY)) < 0) { 223 fprintf(stderr, "hsfs fstyp: cannot open <%s>\n", special); 224 exit(1); 225 } 226 227 #ifdef CDROMREADOFFSET 228 if (rdev_is_a_cd(cdfd)) { 229 err = ioctl(cdfd, CDROMREADOFFSET, &cdroff); 230 if (err == -1) 231 /* 232 * This device doesn't support this ioctl. 233 * That's OK. 234 */ 235 cdroff = 0; 236 } 237 #endif 238 /* check volume descriptor */ 239 cd_type = ckvoldesc(); 240 241 if (cd_type < 0) 242 exit(1); 243 else 244 fprintf(stdout, "hsfs\n"); 245 246 if (vflag) 247 prntlabel(cd_type); 248 249 exit(0); 250 } 251 252 prntlabel(cd_type) 253 int cd_type; 254 { 255 char *vdp; 256 char *sysid; 257 char *volid; 258 char *volsetid; 259 char *pubid; 260 char *prepid; 261 char *applid; 262 char *copyfile; 263 char *absfile; 264 char *bibfile; 265 int volsetsize; 266 int volsetseq; 267 int blksize; 268 int volsize; 269 int i; 270 271 switch (cd_type) { 272 case 0: 273 fprintf(stdout, "CD-ROM is in High Sierra format\n"); 274 sysid=(char *)HSV_sys_id(hs_buf); 275 volid=(char *)HSV_vol_id(hs_buf); 276 volsetid=(char *)HSV_vol_set_id(hs_buf); 277 pubid=(char *)HSV_pub_id(hs_buf); 278 prepid=(char *)HSV_prep_id(hs_buf); 279 applid=(char *)HSV_appl_id(hs_buf); 280 copyfile=(char *)HSV_copyr_id(hs_buf); 281 absfile=(char *)HSV_abstr_id(hs_buf); 282 bibfile=NULL; 283 volsetsize= HSV_SET_SIZE(hs_buf); 284 volsetseq= HSV_SET_SEQ(hs_buf); 285 blksize= HSV_BLK_SIZE(hs_buf); 286 volsize= HSV_VOL_SIZE(hs_buf); 287 break; 288 case 1: 289 fprintf(stdout, "CD-ROM is in ISO 9660 format\n"); 290 sysid=(char *) ISO_sys_id(iso_buf); 291 volid=(char *)ISO_vol_id(iso_buf); 292 volsetid=(char *)ISO_vol_set_id(iso_buf); 293 pubid=(char *)ISO_pub_id(iso_buf); 294 prepid=(char *)ISO_prep_id(iso_buf); 295 applid=(char *)ISO_appl_id(iso_buf); 296 copyfile=(char *)ISO_copyr_id(iso_buf); 297 absfile=(char *)ISO_abstr_id(iso_buf); 298 bibfile=(char *)ISO_bibli_id(iso_buf); 299 volsetsize=ISO_SET_SIZE(iso_buf); 300 volsetseq=ISO_SET_SEQ(iso_buf); 301 blksize=ISO_BLK_SIZE(iso_buf); 302 volsize=ISO_VOL_SIZE(iso_buf); 303 break; 304 case 2: 305 fprintf(stdout, "CD-ROM is in ISO 9660 format with UNIX extension\n"); 306 sysid=(char *)ISO_sys_id(unix_buf); 307 volid=(char *)ISO_vol_id(unix_buf); 308 volsetid=(char *)ISO_vol_set_id(unix_buf); 309 pubid=(char *)ISO_pub_id(unix_buf); 310 prepid=(char *)ISO_prep_id(unix_buf); 311 applid=(char *)ISO_appl_id(unix_buf); 312 copyfile=(char *)ISO_copyr_id(unix_buf); 313 absfile=(char *)ISO_abstr_id(unix_buf); 314 bibfile=(char *)ISO_bibli_id(unix_buf); 315 volsetsize=ISO_SET_SIZE(unix_buf); 316 volsetseq=ISO_SET_SEQ(unix_buf); 317 blksize=ISO_BLK_SIZE(unix_buf); 318 volsize=ISO_VOL_SIZE(unix_buf); 319 break; 320 default: 321 return; 322 } 323 /* system id */ 324 prntstring("System id", sysid, 32); 325 /* read volume id */ 326 prntstring("Volume id", volid, 32); 327 /* read volume set id */ 328 prntstring("Volume set id", volsetid, 128); 329 /* publisher id */ 330 prntstring("Publisher id", pubid, 128); 331 /* data preparer id */ 332 prntstring("Data preparer id", prepid, 128); 333 /* application id */ 334 prntstring("Application id", applid, 128); 335 /* copyright file identifier */ 336 prntstring("Copyright File id", copyfile, 37); 337 /* Abstract file identifier */ 338 prntstring("Abstract File id", absfile, 37); 339 /* Bibliographic file identifier */ 340 prntstring("Bibliographic File id", bibfile, 37); 341 /* print volume set size */ 342 fprintf(stdout, "Volume set size is %d\n", volsetsize); 343 /* print volume set sequnce number */ 344 fprintf(stdout, "Volume set sequence number is %d\n", volsetseq); 345 /* print logical block size */ 346 fprintf(stdout, "Logical block size is %d\n", blksize); 347 /* print volume size */ 348 fprintf(stdout, "Volume size is %d\n", volsize); 349 } 350 351 prntstring(heading, s, maxlen) 352 char * heading; 353 char *s; 354 int maxlen; 355 { 356 int i; 357 if (maxlen < 1) return; 358 if (heading == NULL || s == NULL) return; 359 /* print heading */ 360 fprintf(stdout, "%s: ", heading); 361 362 /* strip off trailing zeros */ 363 for (i=maxlen-1;i >= 0; i--) 364 if (s[i] != ' ') break; 365 366 maxlen = i+1; 367 for (i=0;i<maxlen;i++) fprintf(stdout, "%c", s[i]); 368 fprintf(stdout, "\n"); 369 } 370 371 /*readdisk - read from cdrom image file */ 372 getdisk(buf, daddr, size) 373 char *buf; /* buffer area */ 374 int daddr; /* disk addr */ 375 int size; /* no. of byte */ 376 { 377 if (lseek(cdfd, daddr, L_SET) == -1) { 378 perror("getdisk/lseek"); 379 exit(1); 380 } 381 if (read(cdfd, buf, size) != size) { 382 perror("getdisk/read"); 383 exit(1); 384 } 385 386 } 387 388 /* 389 * rdev_is_a_cd - return TRUE if the raw device identified by 390 * a file descriptor is a CDROM device. 391 * 392 * return FALSE if the device can't be accessed 393 * or is not a CDROM. 394 */ 395 static int 396 rdev_is_a_cd(int rdevfd) 397 { 398 struct dk_cinfo dkc; 399 400 if (ioctl(rdevfd, DKIOCINFO, &dkc) < 0) 401 return (0); 402 if (dkc.dki_ctype == DKC_CDROM) 403 return (1); 404 else 405 return (0); 406 } 407