xref: /titanic_51/usr/src/cmd/fs.d/hsfs/labelit/labelit.c (revision 0c79d02b29618f74322989ec8ceafaa5486ac1db)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * labelit [option=value ...] cdimage
29  * where options are:
30  *      sysid		system identifier               (a characters, 32 max)
31  *      volid:          volume identifier               (d-characters, 32 max)
32  *      volsetid:       volume set identifier           (d-characters, 128 max)
33  *      pubid:          publisher identifier            (d-characters, 128 max)
34  *      prepid:         data preparer identifier        (d-charcter, 128 max)
35  *      applid:         application identifier          (d-charcter, 128 max)
36  *      copyfile:       copyright file identifier       (d-characters, 128 max)
37  *      absfile:        abstract file identifier        (d-characters, 37 max)
38  *      bibfile:        bibliographic file identifier   (d-charcters, 37 max)
39  */
40 
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <sys/file.h>
48 #include <dirent.h>
49 
50 #include <sys/fs/hsfs_isospec.h>
51 #include <sys/fs/hsfs_spec.h>
52 
53 #define	PUTSECTOR(buf, secno, nosec) (putdisk(buf, (secno)*ISO_SECTOR_SIZE, \
54 	(nosec)*ISO_SECTOR_SIZE))
55 #define	GETSECTOR(buf, secno, nosec) (getdisk(buf, (secno)*ISO_SECTOR_SIZE, \
56 	(nosec)*ISO_SECTOR_SIZE))
57 
58 char *string;
59 #define	MAXERRSTRNG	80
60 char	errstrng[MAXERRSTRNG];
61 char	callname[160];
62 
63 int  cdfd;
64 int cd_type;
65 char hs_buf[ISO_SECTOR_SIZE];
66 int  hs_pvd_sec_no;
67 char iso_buf[ISO_SECTOR_SIZE];
68 int  iso_pvd_sec_no;
69 char unix_buf[ISO_SECTOR_SIZE];
70 int  unix_pvd_sec_no;
71 char *vdp;
72 char *sysid;
73 char *volid;
74 char *volsetid;
75 char *pubid;
76 char *prepid;
77 char *applid;
78 char *copyfile;
79 char *absfile;
80 char *bibfile;
81 int volsetsize;
82 int volsetseq;
83 int blksize;
84 int volsize;
85 
86 static int match(char *s);
87 static void usage(void);
88 static void putdisk(char *buf, int daddr, int size);
89 static void getdisk(char *buf, int daddr, int size);
90 static void prntstring(char *heading, char *s, int maxlen);
91 static void copystring(char *from, char *to, int size);
92 static void prntlabel(void);
93 static void updatelabel(void);
94 static void ckvoldesc(void);
95 
96 int
97 main(int argc, char **argv)
98 {
99 	int c;
100 	int openopt;
101 
102 	strcpy(callname, argv[0]);
103 	for (c = 1; c < argc; c++) {
104 		string = argv[c];
105 		if (match("sysid=")) {
106 			sysid = string;
107 			continue;
108 		}
109 		if (match("volid=")) {
110 			volid = string;
111 			continue;
112 		}
113 		if (match("volsetid=")) {
114 			volsetid = string;
115 			continue;
116 		}
117 		if (match("pubid=")) {
118 			pubid = string;
119 			continue;
120 		}
121 		if (match("prepid=")) {
122 			prepid = string;
123 			continue;
124 		}
125 		if (match("applid=")) {
126 			applid = string;
127 			continue;
128 		}
129 		if (match("copyfile=")) {
130 			copyfile = string;
131 			continue;
132 		}
133 		if (match("absfile=")) {
134 			absfile = string;
135 			continue;
136 		}
137 		if (match("bibfile=")) {
138 			bibfile = string;
139 			continue;
140 		}
141 		break;
142 	}
143 	/* the last argument must be the cdrom iamge file */
144 	if (argc != c+1) {
145 		if (argc > 1)
146 			fprintf(stderr, "%s: Illegal option %s in input\n",
147 			    callname, string);
148 			usage();
149 	}
150 
151 	/* open image file in read write only if necessary */
152 	if (argc == 2) openopt = O_RDONLY;
153 	else openopt = O_RDWR;
154 
155 	if ((cdfd = open(argv[c], openopt)) < 0) {
156 		if (strchr(argv[c], '=') ||
157 		    strchr(argv[c], '-')) {
158 			usage();
159 		}
160 		sprintf(errstrng, "%s: main: open(): ", callname);
161 		perror(errstrng);
162 		exit(32);
163 	}
164 
165 	/* check volume descriptor */
166 	(void) ckvoldesc();
167 
168 	if (cd_type < 0) {
169 		fprintf(stderr, "%s: unknown cdrom format label\n", callname);
170 		exit(32);
171 	}
172 
173 	/* update label, if needed */
174 	if (argc != 2) updatelabel();
175 
176 	/* print the (updated) image label */
177 	prntlabel();
178 
179 	close(cdfd);
180 	return (0);
181 }
182 
183 static void
184 usage(void)
185 {
186 	fprintf(stderr, "usage: %s [-F ufs] [option=value ...] cdimage\n",
187 	    callname);
188 	exit(32);
189 }
190 
191 /*
192  * findhsvol: check if the disk is in high sierra format
193  *            return(1) if found, (0) otherwise
194  *	      if found, volp will point to the descriptor
195  *
196  */
197 int
198 findhsvol(volp)
199 char *volp;
200 {
201 int secno;
202 int i;
203 
204 	secno = HS_VOLDESC_SEC;
205 	GETSECTOR(volp, secno++, 1);
206 	while (HSV_DESC_TYPE(volp) != VD_EOV) {
207 		for (i = 0; i < HSV_ID_STRLEN; i++)
208 			if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
209 				goto cantfind;
210 		if (HSV_STD_VER(volp) != HSV_ID_VER)
211 			goto cantfind;
212 		switch (HSV_DESC_TYPE(volp)) {
213 		case VD_SFS:
214 			hs_pvd_sec_no = secno-1;
215 			return (1);
216 		case VD_EOV:
217 			goto cantfind;
218 		}
219 		GETSECTOR(volp, secno++, 1);
220 	}
221 cantfind:
222 	return (0);
223 }
224 
225 /*
226  * findisovol: check if the disk is in ISO 9660 format
227  *            return(1) if found, (0) otherwise
228  *	      if found, volp will point to the descriptor
229  *
230  */
231 int
232 findisovol(volp)
233 char *volp;
234 {
235 int secno;
236 int i;
237 
238 	secno = ISO_VOLDESC_SEC;
239 	GETSECTOR(volp, secno++, 1);
240 	while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
241 		for (i = 0; i < ISO_ID_STRLEN; i++)
242 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
243 				goto cantfind;
244 		if (ISO_STD_VER(volp) != ISO_ID_VER)
245 			goto cantfind;
246 		switch (ISO_DESC_TYPE(volp)) {
247 		case ISO_VD_PVD:
248 			iso_pvd_sec_no = secno-1;
249 			return (1);
250 		case ISO_VD_EOV:
251 			goto cantfind;
252 		}
253 		GETSECTOR(volp, secno++, 1);
254 	}
255 cantfind:
256 	return (0);
257 }
258 
259 /*
260  * findunixvol: check if the disk is in UNIX extension format
261  *            return(1) if found, (0) otherwise
262  *	      if found, volp will point to the descriptor
263  *
264  */
265 int
266 findunixvol(volp)
267 char *volp;
268 {
269 int secno;
270 int i;
271 
272 	secno = ISO_VOLDESC_SEC;
273 	GETSECTOR(volp, secno++, 1);
274 	while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
275 		for (i = 0; i < ISO_ID_STRLEN; i++)
276 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
277 				goto cantfind;
278 		if (ISO_STD_VER(volp) != ISO_ID_VER)
279 			goto cantfind;
280 		switch (ISO_DESC_TYPE(volp)) {
281 		case ISO_VD_UNIX:
282 			unix_pvd_sec_no = secno-1;
283 			return (1);
284 		case ISO_VD_EOV:
285 			goto cantfind;
286 		}
287 		GETSECTOR(volp, secno++, 1);
288 	}
289 cantfind:
290 	return (0);
291 }
292 
293 static void
294 ckvoldesc(void)
295 {
296 	if (findhsvol(hs_buf))
297 		cd_type = 0;
298 	else if (findisovol(iso_buf)) {
299 		if (findunixvol(unix_buf))
300 			cd_type = 2;
301 		else cd_type = 1;
302 	} else {
303 		cd_type = -1;
304 	}
305 }
306 
307 static void
308 updatelabel(void)
309 {
310 	switch (cd_type) {
311 	case 0:
312 		copystring(sysid, (char *)HSV_sys_id(hs_buf), 32);
313 		copystring(volid, (char *)HSV_vol_id(hs_buf), 32);
314 		copystring(volsetid, (char *)HSV_vol_set_id(hs_buf), 128);
315 		copystring(pubid, (char *)HSV_pub_id(hs_buf), 128);
316 		copystring(prepid, (char *)HSV_prep_id(hs_buf), 128);
317 		copystring(applid, (char *)HSV_appl_id(hs_buf), 128);
318 		copystring(copyfile, (char *)HSV_copyr_id(hs_buf), 37);
319 		copystring(absfile, (char *)HSV_abstr_id(hs_buf), 37);
320 		PUTSECTOR(hs_buf, hs_pvd_sec_no, 1);
321 		break;
322 	case 2:
323 		copystring(sysid, (char *)ISO_sys_id(unix_buf), 32);
324 		copystring(volid, (char *)ISO_vol_id(unix_buf), 32);
325 		copystring(volsetid, (char *)ISO_vol_set_id(unix_buf), 128);
326 		copystring(pubid, (char *)ISO_pub_id(unix_buf), 128);
327 		copystring(prepid, (char *)ISO_prep_id(unix_buf), 128);
328 		copystring(applid, (char *)ISO_appl_id(unix_buf), 128);
329 		copystring(copyfile, (char *)ISO_copyr_id(unix_buf), 37);
330 		copystring(absfile, (char *)ISO_abstr_id(unix_buf), 37);
331 		copystring(bibfile, (char *)ISO_bibli_id(unix_buf), 37);
332 		PUTSECTOR(unix_buf, unix_pvd_sec_no, 1);
333 		/*
334 		 * after update unix volume descriptor,
335 		 * fall thru to update the iso primary vol descriptor
336 		 */
337 	case 1:
338 		copystring(sysid, (char *)ISO_sys_id(iso_buf), 32);
339 		copystring(volid, (char *)ISO_vol_id(iso_buf), 32);
340 		copystring(volsetid, (char *)ISO_vol_set_id(iso_buf), 128);
341 		copystring(pubid, (char *)ISO_pub_id(iso_buf), 128);
342 		copystring(prepid, (char *)ISO_prep_id(iso_buf), 128);
343 		copystring(applid, (char *)ISO_appl_id(iso_buf), 128);
344 		copystring(copyfile, (char *)ISO_copyr_id(iso_buf), 37);
345 		copystring(absfile, (char *)ISO_abstr_id(iso_buf), 37);
346 		copystring(bibfile, (char *)ISO_bibli_id(iso_buf), 37);
347 		PUTSECTOR(iso_buf, iso_pvd_sec_no, 1);
348 		break;
349 	}
350 }
351 
352 static void
353 prntlabel(void)
354 {
355 	int i;
356 	switch (cd_type) {
357 	case 0:
358 		printf("CD-ROM is in High Sierra format\n");
359 		sysid = (char *)HSV_sys_id(hs_buf);
360 		volid = (char *)HSV_vol_id(hs_buf);
361 		volsetid = (char *)HSV_vol_set_id(hs_buf);
362 		pubid = (char *)HSV_pub_id(hs_buf);
363 		prepid = (char *)HSV_prep_id(hs_buf);
364 		applid = (char *)HSV_appl_id(hs_buf);
365 		copyfile = (char *)HSV_copyr_id(hs_buf);
366 		absfile = (char *)HSV_abstr_id(hs_buf);
367 		bibfile = NULL;
368 		volsetsize = HSV_SET_SIZE(hs_buf);
369 		volsetseq = HSV_SET_SEQ(hs_buf);
370 		blksize = HSV_BLK_SIZE(hs_buf);
371 		volsize = HSV_VOL_SIZE(hs_buf);
372 		break;
373 	case 1:
374 		printf("CD-ROM is in ISO 9660 format\n");
375 		sysid = (char *)ISO_sys_id(iso_buf);
376 		volid = (char *)ISO_vol_id(iso_buf);
377 		volsetid = (char *)ISO_vol_set_id(iso_buf);
378 		pubid = (char *)ISO_pub_id(iso_buf);
379 		prepid = (char *)ISO_prep_id(iso_buf);
380 		applid = (char *)ISO_appl_id(iso_buf);
381 		copyfile = (char *)ISO_copyr_id(iso_buf);
382 		absfile = (char *)ISO_abstr_id(iso_buf);
383 		bibfile = (char *)ISO_bibli_id(iso_buf);
384 		volsetsize = ISO_SET_SIZE(iso_buf);
385 		volsetseq = ISO_SET_SEQ(iso_buf);
386 		blksize = ISO_BLK_SIZE(iso_buf);
387 		volsize = ISO_VOL_SIZE(iso_buf);
388 		break;
389 	case 2:
390 		printf("CD-ROM is in ISO 9660 format with UNIX extension\n");
391 		sysid = (char *)ISO_sys_id(unix_buf);
392 		volid = (char *)ISO_vol_id(unix_buf);
393 		volsetid = (char *)ISO_vol_set_id(unix_buf);
394 		pubid = (char *)ISO_pub_id(unix_buf);
395 		prepid = (char *)ISO_prep_id(unix_buf);
396 		applid = (char *)ISO_appl_id(unix_buf);
397 		copyfile = (char *)ISO_copyr_id(unix_buf);
398 		absfile = (char *)ISO_abstr_id(unix_buf);
399 		bibfile = (char *)ISO_bibli_id(unix_buf);
400 		volsetsize = ISO_SET_SIZE(unix_buf);
401 		volsetseq = ISO_SET_SEQ(unix_buf);
402 		blksize = ISO_BLK_SIZE(unix_buf);
403 		volsize = ISO_VOL_SIZE(unix_buf);
404 		break;
405 	default:
406 		return;
407 	}
408 	/* system id */
409 	prntstring("System id", sysid, 32);
410 	/* read volume id */
411 	prntstring("Volume id", volid, 32);
412 	/* read volume set id */
413 	prntstring("Volume set id", volsetid, 128);
414 	/* publisher id */
415 	prntstring("Publisher id", pubid, 128);
416 	/* data preparer id */
417 	prntstring("Data preparer id", prepid, 128);
418 	/* application id */
419 	prntstring("Application id", applid, 128);
420 	/* copyright file identifier */
421 	prntstring("Copyright File id", copyfile, 37);
422 	/* Abstract file identifier */
423 	prntstring("Abstract File id", absfile, 37);
424 	/* Bibliographic file identifier */
425 	prntstring("Bibliographic File id", bibfile, 37);
426 	/* print volume set size */
427 	printf("Volume set size is %d\n", volsetsize);
428 	/* print volume set sequnce number */
429 	printf("Volume set sequence number is %d\n", volsetseq);
430 	/* print logical block size */
431 	printf("Logical block size is %d\n", blksize);
432 	/* print volume size */
433 	printf("Volume size is %d\n", volsize);
434 }
435 
436 static void
437 copystring(char *from, char *to, int size)
438 {
439 	int i;
440 
441 	if (from == NULL)
442 		return;
443 	for (i = 0; i < size; i++) {
444 		if (*from == '\0')
445 			break;
446 		else *to++ = *from++;
447 	}
448 	for (; i < size; i++) *to++ = ' ';
449 }
450 
451 static void
452 prntstring(char *heading, char *s, int maxlen)
453 {
454 	int i;
455 	if (maxlen < 1)
456 		return;
457 	if (heading == NULL || s == NULL)
458 		return;
459 	/* print heading */
460 	printf("%s: ", heading);
461 
462 	/* strip off trailing zeros */
463 	for (i = maxlen-1; i >= 0; i--)
464 		if (s[i] != ' ')
465 			break;
466 
467 	maxlen = i+1;
468 	for (i = 0; i < maxlen; i++)
469 		printf("%c", s[i]);
470 	printf("\n");
471 }
472 
473 static int
474 match(char *s)
475 {
476 	char *cs;
477 
478 	cs = string;
479 	while (*cs++ == *s)
480 		if (*s++ == '\0')
481 			goto true;
482 	if (*s != '\0')
483 		return (0);
484 
485 true:
486 	cs--;
487 	string = cs;
488 	return (1);
489 }
490 
491 /* readdisk - read from cdrom image file */
492 static void
493 getdisk(char *buf, int daddr, int size)
494 {
495 
496 	if (lseek(cdfd, daddr, L_SET) == -1) {
497 		sprintf(errstrng, "%s: getdisk: lseek()", callname);
498 		perror(errstrng);
499 		exit(32);
500 	}
501 	if (read(cdfd, buf, size) != size) {
502 		sprintf(errstrng, "%s: getdisk: read()", callname);
503 		perror(errstrng);
504 		exit(32);
505 	}
506 }
507 
508 /* putdisk - write to cdrom image file */
509 static void
510 putdisk(char *buf, int daddr, int size)
511 {
512 
513 	if (lseek(cdfd, daddr, L_SET) == -1) {
514 		sprintf(errstrng, "%s: putdisk: lseek()", callname);
515 		perror(errstrng);
516 		exit(32);
517 	}
518 	if (write(cdfd, buf, size) != size) {
519 		sprintf(errstrng, "%s: putdisk: write()", callname);
520 		perror(errstrng);
521 		exit(32);
522 	}
523 }
524