xref: /illumos-gate/usr/src/cmd/fs.d/hsfs/labelit/labelit.c (revision bde334a8dbd66dfa70ce4d7fc9dcad6e1ae45fe4)
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 		/* FALLTHROUGH */
338 	case 1:
339 		copystring(sysid, (char *)ISO_sys_id(iso_buf), 32);
340 		copystring(volid, (char *)ISO_vol_id(iso_buf), 32);
341 		copystring(volsetid, (char *)ISO_vol_set_id(iso_buf), 128);
342 		copystring(pubid, (char *)ISO_pub_id(iso_buf), 128);
343 		copystring(prepid, (char *)ISO_prep_id(iso_buf), 128);
344 		copystring(applid, (char *)ISO_appl_id(iso_buf), 128);
345 		copystring(copyfile, (char *)ISO_copyr_id(iso_buf), 37);
346 		copystring(absfile, (char *)ISO_abstr_id(iso_buf), 37);
347 		copystring(bibfile, (char *)ISO_bibli_id(iso_buf), 37);
348 		PUTSECTOR(iso_buf, iso_pvd_sec_no, 1);
349 		break;
350 	}
351 }
352 
353 static void
354 prntlabel(void)
355 {
356 	int i;
357 	switch (cd_type) {
358 	case 0:
359 		printf("CD-ROM is in High Sierra format\n");
360 		sysid = (char *)HSV_sys_id(hs_buf);
361 		volid = (char *)HSV_vol_id(hs_buf);
362 		volsetid = (char *)HSV_vol_set_id(hs_buf);
363 		pubid = (char *)HSV_pub_id(hs_buf);
364 		prepid = (char *)HSV_prep_id(hs_buf);
365 		applid = (char *)HSV_appl_id(hs_buf);
366 		copyfile = (char *)HSV_copyr_id(hs_buf);
367 		absfile = (char *)HSV_abstr_id(hs_buf);
368 		bibfile = NULL;
369 		volsetsize = HSV_SET_SIZE(hs_buf);
370 		volsetseq = HSV_SET_SEQ(hs_buf);
371 		blksize = HSV_BLK_SIZE(hs_buf);
372 		volsize = HSV_VOL_SIZE(hs_buf);
373 		break;
374 	case 1:
375 		printf("CD-ROM is in ISO 9660 format\n");
376 		sysid = (char *)ISO_sys_id(iso_buf);
377 		volid = (char *)ISO_vol_id(iso_buf);
378 		volsetid = (char *)ISO_vol_set_id(iso_buf);
379 		pubid = (char *)ISO_pub_id(iso_buf);
380 		prepid = (char *)ISO_prep_id(iso_buf);
381 		applid = (char *)ISO_appl_id(iso_buf);
382 		copyfile = (char *)ISO_copyr_id(iso_buf);
383 		absfile = (char *)ISO_abstr_id(iso_buf);
384 		bibfile = (char *)ISO_bibli_id(iso_buf);
385 		volsetsize = ISO_SET_SIZE(iso_buf);
386 		volsetseq = ISO_SET_SEQ(iso_buf);
387 		blksize = ISO_BLK_SIZE(iso_buf);
388 		volsize = ISO_VOL_SIZE(iso_buf);
389 		break;
390 	case 2:
391 		printf("CD-ROM is in ISO 9660 format with UNIX extension\n");
392 		sysid = (char *)ISO_sys_id(unix_buf);
393 		volid = (char *)ISO_vol_id(unix_buf);
394 		volsetid = (char *)ISO_vol_set_id(unix_buf);
395 		pubid = (char *)ISO_pub_id(unix_buf);
396 		prepid = (char *)ISO_prep_id(unix_buf);
397 		applid = (char *)ISO_appl_id(unix_buf);
398 		copyfile = (char *)ISO_copyr_id(unix_buf);
399 		absfile = (char *)ISO_abstr_id(unix_buf);
400 		bibfile = (char *)ISO_bibli_id(unix_buf);
401 		volsetsize = ISO_SET_SIZE(unix_buf);
402 		volsetseq = ISO_SET_SEQ(unix_buf);
403 		blksize = ISO_BLK_SIZE(unix_buf);
404 		volsize = ISO_VOL_SIZE(unix_buf);
405 		break;
406 	default:
407 		return;
408 	}
409 	/* system id */
410 	prntstring("System id", sysid, 32);
411 	/* read volume id */
412 	prntstring("Volume id", volid, 32);
413 	/* read volume set id */
414 	prntstring("Volume set id", volsetid, 128);
415 	/* publisher id */
416 	prntstring("Publisher id", pubid, 128);
417 	/* data preparer id */
418 	prntstring("Data preparer id", prepid, 128);
419 	/* application id */
420 	prntstring("Application id", applid, 128);
421 	/* copyright file identifier */
422 	prntstring("Copyright File id", copyfile, 37);
423 	/* Abstract file identifier */
424 	prntstring("Abstract File id", absfile, 37);
425 	/* Bibliographic file identifier */
426 	prntstring("Bibliographic File id", bibfile, 37);
427 	/* print volume set size */
428 	printf("Volume set size is %d\n", volsetsize);
429 	/* print volume set sequnce number */
430 	printf("Volume set sequence number is %d\n", volsetseq);
431 	/* print logical block size */
432 	printf("Logical block size is %d\n", blksize);
433 	/* print volume size */
434 	printf("Volume size is %d\n", volsize);
435 }
436 
437 static void
438 copystring(char *from, char *to, int size)
439 {
440 	int i;
441 
442 	if (from == NULL)
443 		return;
444 	for (i = 0; i < size; i++) {
445 		if (*from == '\0')
446 			break;
447 		else *to++ = *from++;
448 	}
449 	for (; i < size; i++) *to++ = ' ';
450 }
451 
452 static void
453 prntstring(char *heading, char *s, int maxlen)
454 {
455 	int i;
456 	if (maxlen < 1)
457 		return;
458 	if (heading == NULL || s == NULL)
459 		return;
460 	/* print heading */
461 	printf("%s: ", heading);
462 
463 	/* strip off trailing zeros */
464 	for (i = maxlen-1; i >= 0; i--)
465 		if (s[i] != ' ')
466 			break;
467 
468 	maxlen = i+1;
469 	for (i = 0; i < maxlen; i++)
470 		printf("%c", s[i]);
471 	printf("\n");
472 }
473 
474 static int
475 match(char *s)
476 {
477 	char *cs;
478 
479 	cs = string;
480 	while (*cs++ == *s)
481 		if (*s++ == '\0')
482 			goto true;
483 	if (*s != '\0')
484 		return (0);
485 
486 true:
487 	cs--;
488 	string = cs;
489 	return (1);
490 }
491 
492 /* readdisk - read from cdrom image file */
493 static void
494 getdisk(char *buf, int daddr, int size)
495 {
496 
497 	if (lseek(cdfd, daddr, L_SET) == -1) {
498 		sprintf(errstrng, "%s: getdisk: lseek()", callname);
499 		perror(errstrng);
500 		exit(32);
501 	}
502 	if (read(cdfd, buf, size) != size) {
503 		sprintf(errstrng, "%s: getdisk: read()", callname);
504 		perror(errstrng);
505 		exit(32);
506 	}
507 }
508 
509 /* putdisk - write to cdrom image file */
510 static void
511 putdisk(char *buf, int daddr, int size)
512 {
513 
514 	if (lseek(cdfd, daddr, L_SET) == -1) {
515 		sprintf(errstrng, "%s: putdisk: lseek()", callname);
516 		perror(errstrng);
517 		exit(32);
518 	}
519 	if (write(cdfd, buf, size) != size) {
520 		sprintf(errstrng, "%s: putdisk: write()", callname);
521 		perror(errstrng);
522 		exit(32);
523 	}
524 }
525