xref: /illumos-gate/usr/src/cmd/fs.d/udfs/fstyp/fstyp.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <locale.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <strings.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/fs/udf_volume.h>
42*7c478bd9Sstevel@tonic-gate #include "ud_lib.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate extern int optind;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate static int verbose;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate static int check_if_udfs(int32_t);
50*7c478bd9Sstevel@tonic-gate static int print_vds(struct vds *, int32_t);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate int
53*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	int errflag = 0, c, rval, fd;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
58*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
59*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "v")) != EOF) {
64*7c478bd9Sstevel@tonic-gate 		switch (c) {
65*7c478bd9Sstevel@tonic-gate 			case 'v':
66*7c478bd9Sstevel@tonic-gate 				verbose++;
67*7c478bd9Sstevel@tonic-gate 				break;
68*7c478bd9Sstevel@tonic-gate 			default:
69*7c478bd9Sstevel@tonic-gate 				errflag++;
70*7c478bd9Sstevel@tonic-gate 				break;
71*7c478bd9Sstevel@tonic-gate 		}
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if (errflag || (argc <= optind)) {
75*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
76*7c478bd9Sstevel@tonic-gate 			gettext("Usage: fstyp -v special\n"));
77*7c478bd9Sstevel@tonic-gate 		exit(1);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if ((fd = ud_open_dev(argv[optind], O_RDONLY)) < 0) {
81*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
82*7c478bd9Sstevel@tonic-gate 			gettext("udfs fstyp: cannot open <%s> errorno <%d>\n"),
83*7c478bd9Sstevel@tonic-gate 					argv[optind], errno);
84*7c478bd9Sstevel@tonic-gate 		exit(1);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	/*
88*7c478bd9Sstevel@tonic-gate 	 * check the volume
89*7c478bd9Sstevel@tonic-gate 	 */
90*7c478bd9Sstevel@tonic-gate 	rval = check_if_udfs(fd);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	ud_close_dev(fd);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	return (rval);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * Assumption is that we will confirm to level-1
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate int
102*7c478bd9Sstevel@tonic-gate check_if_udfs(int32_t fd)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	int32_t ret;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if ((ret = ud_fill_udfs_info(fd)) != 0) {
107*7c478bd9Sstevel@tonic-gate 		return (ret);
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if ((udfs.flags & VALID_UDFS) == 0) {
111*7c478bd9Sstevel@tonic-gate 		return (1);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "udfs\n");
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if (verbose == 0) {
117*7c478bd9Sstevel@tonic-gate 		return (0);
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout,
121*7c478bd9Sstevel@tonic-gate 		"Standard Identifier %5s\n", udfs.ecma_id);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (udfs.flags & VALID_MVDS) {
124*7c478bd9Sstevel@tonic-gate 		ret = print_vds(&udfs.mvds, fd);
125*7c478bd9Sstevel@tonic-gate 	} else {
126*7c478bd9Sstevel@tonic-gate 		ret = print_vds(&udfs.rvds, fd);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	return (ret);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate int
133*7c478bd9Sstevel@tonic-gate print_vds(struct vds *v, int32_t fd)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	int32_t i;
136*7c478bd9Sstevel@tonic-gate 	uint32_t len;
137*7c478bd9Sstevel@tonic-gate 	uint64_t off;
138*7c478bd9Sstevel@tonic-gate 	uint8_t *buf;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	/*
141*7c478bd9Sstevel@tonic-gate 	 * All descriptors are 512 bytes
142*7c478bd9Sstevel@tonic-gate 	 * except lvd, usd and lvid
143*7c478bd9Sstevel@tonic-gate 	 * findout the largest and allocate space
144*7c478bd9Sstevel@tonic-gate 	 */
145*7c478bd9Sstevel@tonic-gate 	len = udfs.lbsize;
146*7c478bd9Sstevel@tonic-gate 	if (v->lvd_len > len) {
147*7c478bd9Sstevel@tonic-gate 		len = v->lvd_len;
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	if (v->usd_len > len) {
150*7c478bd9Sstevel@tonic-gate 		len = v->usd_len;
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 	if (udfs.lvid_len > len) {
153*7c478bd9Sstevel@tonic-gate 		len = udfs.lvid_len;
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if ((buf = (uint8_t *)malloc(len)) == NULL) {
157*7c478bd9Sstevel@tonic-gate 		return (1);
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	/*
161*7c478bd9Sstevel@tonic-gate 	 * Anchor Volume Descriptor
162*7c478bd9Sstevel@tonic-gate 	 */
163*7c478bd9Sstevel@tonic-gate 	if (udfs.avdp_len != 0) {
164*7c478bd9Sstevel@tonic-gate 		off = udfs.avdp_loc * udfs.lbsize;
165*7c478bd9Sstevel@tonic-gate 		if (ud_read_dev(fd, off, buf, udfs.avdp_len) != 0) {
166*7c478bd9Sstevel@tonic-gate 			return (2);
167*7c478bd9Sstevel@tonic-gate 		}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		/* LINTED */
170*7c478bd9Sstevel@tonic-gate 		print_avd((struct anch_vol_desc_ptr *)buf);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	/*
174*7c478bd9Sstevel@tonic-gate 	 * Primary Volume Descriptor
175*7c478bd9Sstevel@tonic-gate 	 */
176*7c478bd9Sstevel@tonic-gate 	if (v->pvd_len != 0) {
177*7c478bd9Sstevel@tonic-gate 		off = v->pvd_loc * udfs.lbsize;
178*7c478bd9Sstevel@tonic-gate 		if (ud_read_dev(fd, off, buf, v->pvd_len) != 0) {
179*7c478bd9Sstevel@tonic-gate 			return (3);
180*7c478bd9Sstevel@tonic-gate 		}
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 		/* LINTED */
183*7c478bd9Sstevel@tonic-gate 		print_pvd((struct pri_vol_desc *)buf);
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	/*
187*7c478bd9Sstevel@tonic-gate 	 * Implementation Use descriptor
188*7c478bd9Sstevel@tonic-gate 	 */
189*7c478bd9Sstevel@tonic-gate 	if (v->iud_len != 0) {
190*7c478bd9Sstevel@tonic-gate 		off = v->iud_loc * udfs.lbsize;
191*7c478bd9Sstevel@tonic-gate 		if (ud_read_dev(fd, off, buf, v->iud_len) != 0) {
192*7c478bd9Sstevel@tonic-gate 			return (3);
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 		/* LINTED */
196*7c478bd9Sstevel@tonic-gate 		print_iuvd((struct iuvd_desc *)buf);
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	/*
200*7c478bd9Sstevel@tonic-gate 	 * Paritions
201*7c478bd9Sstevel@tonic-gate 	 */
202*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_parts; i++) {
203*7c478bd9Sstevel@tonic-gate 		if (v->part_len[i] != 0) {
204*7c478bd9Sstevel@tonic-gate 			off = v->part_loc[i] * udfs.lbsize;
205*7c478bd9Sstevel@tonic-gate 			if (ud_read_dev(fd, off, buf, v->part_len[i]) != 0) {
206*7c478bd9Sstevel@tonic-gate 				return (3);
207*7c478bd9Sstevel@tonic-gate 			}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 			/* LINTED */
210*7c478bd9Sstevel@tonic-gate 			print_part((struct part_desc *)buf);
211*7c478bd9Sstevel@tonic-gate 		}
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	/*
215*7c478bd9Sstevel@tonic-gate 	 * Logical Volume Descriptor
216*7c478bd9Sstevel@tonic-gate 	 */
217*7c478bd9Sstevel@tonic-gate 	if (v->lvd_len != 0) {
218*7c478bd9Sstevel@tonic-gate 		off = v->lvd_loc * udfs.lbsize;
219*7c478bd9Sstevel@tonic-gate 		if (ud_read_dev(fd, off, buf, v->lvd_len) != 0) {
220*7c478bd9Sstevel@tonic-gate 			return (3);
221*7c478bd9Sstevel@tonic-gate 		}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 		/* LINTED */
224*7c478bd9Sstevel@tonic-gate 		print_lvd((struct log_vol_desc *)buf);
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	/*
228*7c478bd9Sstevel@tonic-gate 	 * Unallocated Space Descriptor
229*7c478bd9Sstevel@tonic-gate 	 */
230*7c478bd9Sstevel@tonic-gate 	if (v->usd_len != 0) {
231*7c478bd9Sstevel@tonic-gate 		off = v->usd_loc * udfs.lbsize;
232*7c478bd9Sstevel@tonic-gate 		if (ud_read_dev(fd, off, buf, v->usd_len) != 0) {
233*7c478bd9Sstevel@tonic-gate 			return (3);
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 		/* LINTED */
237*7c478bd9Sstevel@tonic-gate 		print_usd((struct unall_spc_desc *)buf);
238*7c478bd9Sstevel@tonic-gate 	}
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	/*
241*7c478bd9Sstevel@tonic-gate 	 * Logical Volume Integrity Descriptor
242*7c478bd9Sstevel@tonic-gate 	 */
243*7c478bd9Sstevel@tonic-gate 	if (udfs.lvid_len != 0) {
244*7c478bd9Sstevel@tonic-gate 		off = udfs.lvid_loc * udfs.lbsize;
245*7c478bd9Sstevel@tonic-gate 		if (ud_read_dev(fd, off, buf, udfs.lvid_len) != 0) {
246*7c478bd9Sstevel@tonic-gate 			return (3);
247*7c478bd9Sstevel@tonic-gate 		}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		/* LINTED */
250*7c478bd9Sstevel@tonic-gate 		print_lvid((struct log_vol_int_desc *)buf);
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	/*
254*7c478bd9Sstevel@tonic-gate 	 * File Set Descriptor
255*7c478bd9Sstevel@tonic-gate 	 */
256*7c478bd9Sstevel@tonic-gate 	if (udfs.fsd_len != 0) {
257*7c478bd9Sstevel@tonic-gate 		off = udfs.fsd_loc * udfs.lbsize;
258*7c478bd9Sstevel@tonic-gate 		if (ud_read_dev(fd, off, buf, udfs.fsd_len) != 0) {
259*7c478bd9Sstevel@tonic-gate 			return (3);
260*7c478bd9Sstevel@tonic-gate 		}
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 		/* LINTED */
263*7c478bd9Sstevel@tonic-gate 		print_fsd((struct file_set_desc *)buf);
264*7c478bd9Sstevel@tonic-gate 	}
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	return (0);
267*7c478bd9Sstevel@tonic-gate }
268