xref: /illumos-gate/usr/src/cmd/fs.d/udfs/common/ud_lib.c (revision eee96f10)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50e42dee6Sartem  * Common Development and Distribution License (the "License").
60e42dee6Sartem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220e42dee6Sartem  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230e42dee6Sartem  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <malloc.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate #include <libgen.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/int_types.h>
377c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
387c478bd9Sstevel@tonic-gate #include <sys/cdio.h>
397c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/fs/udf_volume.h>
437c478bd9Sstevel@tonic-gate #include "ud_lib.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern char *getfullrawname(char *);
467c478bd9Sstevel@tonic-gate 
470e42dee6Sartem static int32_t ud_get_ecma_ver(ud_handle_t, uint32_t);
480e42dee6Sartem static int32_t ud_get_fs_bsize(ud_handle_t, uint32_t, uint32_t *);
490e42dee6Sartem static int32_t ud_parse_fill_vds(ud_handle_t, struct vds *, uint32_t, uint32_t);
500e42dee6Sartem static int32_t	ud_read_and_translate_lvd(ud_handle_t, uint32_t, uint32_t);
510e42dee6Sartem static int32_t ud_get_latest_lvid(ud_handle_t, uint32_t, uint32_t);
520e42dee6Sartem static int32_t	ud_get_latest_fsd(ud_handle_t, uint16_t, uint32_t, uint32_t);
530e42dee6Sartem 
540e42dee6Sartem static uint16_t ud_crc(uint8_t *, int32_t);
550e42dee6Sartem static int32_t UdfTxName(uint16_t *, int32_t);
560e42dee6Sartem static int32_t UncompressUnicode(int32_t, uint8_t *, uint16_t *);
570e42dee6Sartem static int32_t ud_compressunicode(int32_t, int32_t, uint16_t *, uint8_t *);
580e42dee6Sartem static int32_t ud_convert2utf8(uint8_t *, uint8_t *, int32_t);
590e42dee6Sartem static int32_t ud_convert2utf16(uint8_t *, uint8_t *, int32_t);
600e42dee6Sartem 
610e42dee6Sartem 
620e42dee6Sartem int
ud_init(int fd,ud_handle_t * hp)630e42dee6Sartem ud_init(int fd, ud_handle_t *hp)
640e42dee6Sartem {
650e42dee6Sartem 	struct ud_handle *h;
660e42dee6Sartem 
670e42dee6Sartem 	if ((h = calloc(1, sizeof (struct ud_handle))) == NULL) {
680e42dee6Sartem 		return (ENOMEM);
690e42dee6Sartem 	}
700e42dee6Sartem 	h->fd = fd;
710e42dee6Sartem 	*hp = h;
720e42dee6Sartem 	return (0);
730e42dee6Sartem }
740e42dee6Sartem 
750e42dee6Sartem void
ud_fini(ud_handle_t h)760e42dee6Sartem ud_fini(ud_handle_t h)
770e42dee6Sartem {
780e42dee6Sartem 	free(h);
790e42dee6Sartem }
807c478bd9Sstevel@tonic-gate 
810e42dee6Sartem /* ARGSUSED */
827c478bd9Sstevel@tonic-gate int32_t
ud_open_dev(ud_handle_t h,char * special,uint32_t flags)830e42dee6Sartem ud_open_dev(ud_handle_t h, char *special, uint32_t flags)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	char *temp;
867c478bd9Sstevel@tonic-gate 	struct stat i_stat, r_stat;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	(void) bzero(&i_stat, sizeof (struct stat));
897c478bd9Sstevel@tonic-gate 	(void) bzero(&r_stat, sizeof (struct stat));
907c478bd9Sstevel@tonic-gate 
91*eee96f10SToomas Soome 	temp = special;
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Get the stat structure
947c478bd9Sstevel@tonic-gate 	 */
95*eee96f10SToomas Soome 	if (stat(special, &i_stat) == 0) {
96*eee96f10SToomas Soome 		if ((i_stat.st_mode & S_IFMT) == S_IFBLK) {
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 			/*
997c478bd9Sstevel@tonic-gate 			 * Block device try to convert to raw device
1007c478bd9Sstevel@tonic-gate 			 */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 			temp = getfullrawname(special);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 			/*
1057c478bd9Sstevel@tonic-gate 			 * Stat the converted device name and verify
1067c478bd9Sstevel@tonic-gate 			 * both the raw and block device belong to
1077c478bd9Sstevel@tonic-gate 			 * the same device
1087c478bd9Sstevel@tonic-gate 			 */
1097c478bd9Sstevel@tonic-gate 			if (stat(temp, &r_stat) < 0) {
1107c478bd9Sstevel@tonic-gate 				temp = special;
1117c478bd9Sstevel@tonic-gate 			} else {
1127c478bd9Sstevel@tonic-gate 				if (((r_stat.st_mode & S_IFMT) == S_IFBLK) ||
1137c478bd9Sstevel@tonic-gate 					(r_stat.st_rdev != i_stat.st_rdev)) {
1147c478bd9Sstevel@tonic-gate 					temp = special;
1157c478bd9Sstevel@tonic-gate 				}
1167c478bd9Sstevel@tonic-gate 			}
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * Now finally open the device
1227c478bd9Sstevel@tonic-gate 	 */
1230e42dee6Sartem 	h->fd = open(temp, flags);
1247c478bd9Sstevel@tonic-gate 
1250e42dee6Sartem 	return (h->fd);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1280e42dee6Sartem /* ARGSUSED */
1297c478bd9Sstevel@tonic-gate void
ud_close_dev(ud_handle_t h)1300e42dee6Sartem ud_close_dev(ud_handle_t h)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * Too simple Just close it
1347c478bd9Sstevel@tonic-gate 	 */
1350e42dee6Sartem 	(void) close(h->fd);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate int32_t
ud_read_dev(ud_handle_t h,uint64_t offset,uint8_t * buf,uint32_t count)1390e42dee6Sartem ud_read_dev(ud_handle_t h, uint64_t offset, uint8_t *buf, uint32_t count)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * Seek to the given offset
1437c478bd9Sstevel@tonic-gate 	 */
1440e42dee6Sartem 	if (lseek(h->fd, offset, SEEK_SET) == -1) {
1457c478bd9Sstevel@tonic-gate 		return (1);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * Read the required number of bytes
1507c478bd9Sstevel@tonic-gate 	 */
1510e42dee6Sartem 	if (read(h->fd, buf, count) != count) {
1527c478bd9Sstevel@tonic-gate 		return (1);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	return (0);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int32_t
ud_write_dev(ud_handle_t h,uint64_t offset,uint8_t * buf,uint32_t count)1580e42dee6Sartem ud_write_dev(ud_handle_t h, uint64_t offset, uint8_t *buf, uint32_t count)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * Seek to the given offset
1627c478bd9Sstevel@tonic-gate 	 */
1630e42dee6Sartem 	if (lseek(h->fd, offset, SEEK_SET) == -1) {
1647c478bd9Sstevel@tonic-gate 		return (1);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Read the appropriate number of bytes
1697c478bd9Sstevel@tonic-gate 	 */
1700e42dee6Sartem 	if (write(h->fd, buf, count) != count) {
1717c478bd9Sstevel@tonic-gate 		return (1);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return (0);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /* ----- BEGIN Read and translate the on disk VDS to IN CORE format -------- */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate int32_t
ud_fill_udfs_info(ud_handle_t h)1790e42dee6Sartem ud_fill_udfs_info(ud_handle_t h)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	struct	anch_vol_desc_ptr	*avdp = NULL;
1827c478bd9Sstevel@tonic-gate 	uint32_t			offset = 0;
1837c478bd9Sstevel@tonic-gate 
1840e42dee6Sartem 	if (ioctl(h->fd, CDROMREADOFFSET, &offset) == -1) {
1857c478bd9Sstevel@tonic-gate 		offset = 0;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1880e42dee6Sartem 	h->udfs.flags = INVALID_UDFS;
1897c478bd9Sstevel@tonic-gate 
1900e42dee6Sartem 	h->udfs.ecma_version = ud_get_ecma_ver(h, offset);
1910e42dee6Sartem 	if (h->udfs.ecma_version == UD_ECMA_UNKN) {
1927c478bd9Sstevel@tonic-gate 		return (1);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1950e42dee6Sartem 	h->udfs.lbsize = ud_get_fs_bsize(h, offset, &h->udfs.avdp_loc);
1960e42dee6Sartem 	if (h->udfs.lbsize == 0) {
1977c478bd9Sstevel@tonic-gate 		return (2);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2000e42dee6Sartem 	h->udfs.avdp_len = lb_roundup(512, h->udfs.lbsize);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if ((avdp = (struct anch_vol_desc_ptr *)
2040e42dee6Sartem 			malloc(h->udfs.lbsize)) == NULL) {
2057c478bd9Sstevel@tonic-gate 		return (3);
2067c478bd9Sstevel@tonic-gate 	}
2070e42dee6Sartem 	if (ud_read_dev(h, h->udfs.avdp_loc * h->udfs.lbsize,
2080e42dee6Sartem 			(uint8_t *)avdp, h->udfs.lbsize) != 0) {
2097c478bd9Sstevel@tonic-gate 		free(avdp);
2107c478bd9Sstevel@tonic-gate 		return (4);
2117c478bd9Sstevel@tonic-gate 	}
2120e42dee6Sartem 	if (ud_verify_tag(h, &avdp->avd_tag, UD_ANCH_VOL_DESC,
2130e42dee6Sartem 			h->udfs.avdp_loc, 1, 0) != 0) {
2147c478bd9Sstevel@tonic-gate 		free(avdp);
2157c478bd9Sstevel@tonic-gate 		return (5);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2180e42dee6Sartem 	h->udfs.mvds_loc = SWAP_32(avdp->avd_main_vdse.ext_loc);
2190e42dee6Sartem 	h->udfs.mvds_len = SWAP_32(avdp->avd_main_vdse.ext_len);
2207c478bd9Sstevel@tonic-gate 
2210e42dee6Sartem 	h->udfs.rvds_loc = SWAP_32(avdp->avd_res_vdse.ext_loc);
2220e42dee6Sartem 	h->udfs.rvds_len = SWAP_32(avdp->avd_res_vdse.ext_len);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	free(avdp);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * get information from mvds and rvds
2287c478bd9Sstevel@tonic-gate 	 */
2290e42dee6Sartem 	if (ud_parse_fill_vds(h, &h->udfs.mvds,
2300e42dee6Sartem 			h->udfs.mvds_loc, h->udfs.mvds_len) == 0) {
2310e42dee6Sartem 		h->udfs.flags |= VALID_MVDS;
2327c478bd9Sstevel@tonic-gate 	}
2330e42dee6Sartem 	if (ud_parse_fill_vds(h, &h->udfs.rvds,
2340e42dee6Sartem 			h->udfs.rvds_loc, h->udfs.rvds_len) == 0) {
2350e42dee6Sartem 		h->udfs.flags |= VALID_RVDS;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2380e42dee6Sartem 	if ((h->udfs.flags & (VALID_MVDS | VALID_RVDS)) == 0) {
2397c478bd9Sstevel@tonic-gate 		return (6);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * If we are here we have
2447c478bd9Sstevel@tonic-gate 	 * a valid Volume Descriptor Seqence
2457c478bd9Sstevel@tonic-gate 	 * Read and understand lvd
2467c478bd9Sstevel@tonic-gate 	 */
2470e42dee6Sartem 	if (h->udfs.flags & VALID_MVDS) {
2480e42dee6Sartem 		if (ud_read_and_translate_lvd(h, h->udfs.mvds.lvd_loc,
2490e42dee6Sartem 				h->udfs.mvds.lvd_len) != 0) {
2507c478bd9Sstevel@tonic-gate 			return (7);
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 	} else {
2530e42dee6Sartem 		if (ud_read_and_translate_lvd(h, h->udfs.rvds.lvd_loc,
2540e42dee6Sartem 				h->udfs.rvds.lvd_len) != 0) {
2557c478bd9Sstevel@tonic-gate 			return (8);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2590e42dee6Sartem 	h->udfs.flags |= VALID_UDFS;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	return (0);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2640e42dee6Sartem static int32_t
ud_get_ecma_ver(ud_handle_t h,uint32_t offset)2650e42dee6Sartem ud_get_ecma_ver(ud_handle_t h, uint32_t offset)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	uint8_t *buf;
2687c478bd9Sstevel@tonic-gate 	uint64_t off;
2697c478bd9Sstevel@tonic-gate 	uint64_t end_off;
2707c478bd9Sstevel@tonic-gate 	struct nsr_desc *ndsc;
2717c478bd9Sstevel@tonic-gate 	uint32_t ecma_ver = UD_ECMA_UNKN;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * Allocate a buffer of size UD_VOL_REC_BSZ
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if ((buf = (uint8_t *)malloc(UD_VOL_REC_BSZ)) == NULL) {
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		/*
2797c478bd9Sstevel@tonic-gate 		 * Uh could not even allocate this much
2807c478bd9Sstevel@tonic-gate 		 */
2817c478bd9Sstevel@tonic-gate 		goto end;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * Start from 32k and keep reading 2k blocks we
2867c478bd9Sstevel@tonic-gate 	 * should be able to find NSR if we have one by 256 * 2k bytes
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 	off = offset * 2048 + UD_VOL_REC_START;
2897c478bd9Sstevel@tonic-gate 	end_off = offset * 2048 + UD_VOL_REC_END;
2907c478bd9Sstevel@tonic-gate 	for (; off < end_off; off += UD_VOL_REC_BSZ) {
2917c478bd9Sstevel@tonic-gate 
2920e42dee6Sartem 		if (ud_read_dev(h, off, buf, UD_VOL_REC_BSZ) == 0) {
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 			ndsc = (struct nsr_desc *)buf;
2957c478bd9Sstevel@tonic-gate 			/*
2967c478bd9Sstevel@tonic-gate 			 * Is this either NSR02 or NSR03
2977c478bd9Sstevel@tonic-gate 			 */
2987c478bd9Sstevel@tonic-gate 			if ((ndsc->nsr_str_type == 0) &&
2997c478bd9Sstevel@tonic-gate 				(ndsc->nsr_ver == 1) &&
3007c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[0] == 'N') &&
3017c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[1] == 'S') &&
3027c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[2] == 'R') &&
3037c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[3] == '0') &&
3047c478bd9Sstevel@tonic-gate 					((ndsc->nsr_id[4] == '2') ||
3057c478bd9Sstevel@tonic-gate 					(ndsc->nsr_id[4] == '3'))) {
3067c478bd9Sstevel@tonic-gate 
3070e42dee6Sartem 				(void) strncpy((char *)h->udfs.ecma_id,
3087c478bd9Sstevel@tonic-gate 					(char *)ndsc->nsr_id, 5);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 				switch (ndsc->nsr_id[4]) {
3117c478bd9Sstevel@tonic-gate 				case '2' :
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 					/*
3147c478bd9Sstevel@tonic-gate 					 * ECMA 167/2
3157c478bd9Sstevel@tonic-gate 					 */
3167c478bd9Sstevel@tonic-gate 					ecma_ver = UD_ECMA_VER2;
3177c478bd9Sstevel@tonic-gate 					goto end;
3187c478bd9Sstevel@tonic-gate 				case '3' :
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 					/*
3217c478bd9Sstevel@tonic-gate 					 * ECMA 167/3
3227c478bd9Sstevel@tonic-gate 					 */
3237c478bd9Sstevel@tonic-gate 					ecma_ver = UD_ECMA_VER3;
3247c478bd9Sstevel@tonic-gate 					goto end;
3257c478bd9Sstevel@tonic-gate 				}
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate end:
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * Cleanup
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 	free(buf);
3357c478bd9Sstevel@tonic-gate 	return (ecma_ver);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3380e42dee6Sartem static uint32_t last_block_index[] = {0, 0, 256, 2, 2 + 256,
3397c478bd9Sstevel@tonic-gate 		150, 150 + 256, 152, 152 + 256};
3407c478bd9Sstevel@tonic-gate 
3410e42dee6Sartem static int32_t
ud_get_fs_bsize(ud_handle_t h,uint32_t offset,uint32_t * avd_loc)3420e42dee6Sartem ud_get_fs_bsize(ud_handle_t h, uint32_t offset, uint32_t *avd_loc)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	uint64_t off;
3457c478bd9Sstevel@tonic-gate 	int32_t index, bsize, shift, end_index;
3467c478bd9Sstevel@tonic-gate 	uint32_t num_blocks, sub_blk;
3477c478bd9Sstevel@tonic-gate 	uint8_t *buf = NULL;
3487c478bd9Sstevel@tonic-gate 	struct anch_vol_desc_ptr *avdp;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if ((buf = (uint8_t *)malloc(MAXBSIZE)) == NULL) {
3517c478bd9Sstevel@tonic-gate 		return (0);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * If we could figure out the last block
3567c478bd9Sstevel@tonic-gate 	 * search at 256, N, N - 256 blocks
3577c478bd9Sstevel@tonic-gate 	 * otherwise just check at 256
3587c478bd9Sstevel@tonic-gate 	 */
3590e42dee6Sartem 	if (ud_get_num_blks(h, &num_blocks) != 0) {
3607c478bd9Sstevel@tonic-gate 		end_index = 1;
3617c478bd9Sstevel@tonic-gate 		num_blocks = 0;
3627c478bd9Sstevel@tonic-gate 	} else {
3637c478bd9Sstevel@tonic-gate 		end_index = sizeof (last_block_index) / 4;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	for (index = 0; index < end_index; index++) {
3677c478bd9Sstevel@tonic-gate 		sub_blk = last_block_index[index];
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		/*
3707c478bd9Sstevel@tonic-gate 		 * Start guessing from DEV_BSIZE to MAXBSIZE
3717c478bd9Sstevel@tonic-gate 		 */
3727c478bd9Sstevel@tonic-gate 		for (bsize = DEV_BSIZE, shift = 0;
3737c478bd9Sstevel@tonic-gate 			bsize <= MAXBSIZE; bsize <<= 1, shift++) {
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 			if (index == 0) {
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 				/*
3787c478bd9Sstevel@tonic-gate 				 * Check if we atleast have 256 of bsize
3797c478bd9Sstevel@tonic-gate 				 * blocks on the device
3807c478bd9Sstevel@tonic-gate 				 */
3817c478bd9Sstevel@tonic-gate 				if ((end_index == 0) ||
3827c478bd9Sstevel@tonic-gate 					(num_blocks > (256 << shift))) {
3837c478bd9Sstevel@tonic-gate 					*avd_loc = 256;
3847c478bd9Sstevel@tonic-gate 					if (bsize <= 2048) {
3857c478bd9Sstevel@tonic-gate 						*avd_loc +=
3867c478bd9Sstevel@tonic-gate 							offset * 2048 / bsize;
3877c478bd9Sstevel@tonic-gate 					} else {
3887c478bd9Sstevel@tonic-gate 						*avd_loc +=
3897c478bd9Sstevel@tonic-gate 							offset / (bsize / 2048);
3907c478bd9Sstevel@tonic-gate 					}
3917c478bd9Sstevel@tonic-gate 				} else {
3927c478bd9Sstevel@tonic-gate 					continue;
3937c478bd9Sstevel@tonic-gate 				}
3947c478bd9Sstevel@tonic-gate 			} else {
3957c478bd9Sstevel@tonic-gate 				/*
3960e42dee6Sartem 				 * Calculate the bsize avd block
3977c478bd9Sstevel@tonic-gate 				 */
3987c478bd9Sstevel@tonic-gate 				if ((num_blocks) &&
3997c478bd9Sstevel@tonic-gate 					(num_blocks > (sub_blk << shift))) {
4007c478bd9Sstevel@tonic-gate 					*avd_loc = (num_blocks >> shift) -
4017c478bd9Sstevel@tonic-gate 						sub_blk;
4027c478bd9Sstevel@tonic-gate 				} else {
4037c478bd9Sstevel@tonic-gate 					continue;
4047c478bd9Sstevel@tonic-gate 				}
4057c478bd9Sstevel@tonic-gate 			}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 			off = (uint64_t)*avd_loc * bsize;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 			/*
4107c478bd9Sstevel@tonic-gate 			 * Read bsize bytes at off
4117c478bd9Sstevel@tonic-gate 			 */
4120e42dee6Sartem 			if (ud_read_dev(h, off, buf, bsize) != 0) {
4137c478bd9Sstevel@tonic-gate 				continue;
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 			/*
4177c478bd9Sstevel@tonic-gate 			 * Check if we have a Anchor Volume Descriptor here
4187c478bd9Sstevel@tonic-gate 			 */
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			/* LINTED */
4217c478bd9Sstevel@tonic-gate 			avdp = (struct anch_vol_desc_ptr *)buf;
4220e42dee6Sartem 			if (ud_verify_tag(h, &avdp->avd_tag,
4237c478bd9Sstevel@tonic-gate 				UD_ANCH_VOL_DESC, *avd_loc, 1, 0) != 0) {
4247c478bd9Sstevel@tonic-gate 				continue;
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 			goto end;
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate end:
4317c478bd9Sstevel@tonic-gate 	if (bsize > MAXBSIZE) {
4327c478bd9Sstevel@tonic-gate 		bsize = 0;
4337c478bd9Sstevel@tonic-gate 		*avd_loc = 0;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	free(buf);
4367c478bd9Sstevel@tonic-gate 	return (bsize);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4390e42dee6Sartem static int32_t
ud_parse_fill_vds(ud_handle_t h,struct vds * v,uint32_t vds_loc,uint32_t vds_len)4400e42dee6Sartem ud_parse_fill_vds(ud_handle_t h, struct vds *v,
4417c478bd9Sstevel@tonic-gate 	uint32_t vds_loc, uint32_t vds_len)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	uint8_t *addr, *taddr, *eaddr;
4447c478bd9Sstevel@tonic-gate 	uint16_t id;
4457c478bd9Sstevel@tonic-gate 	int32_t i;
4467c478bd9Sstevel@tonic-gate 	uint64_t off;
4477c478bd9Sstevel@tonic-gate 	struct tag *tag;
4487c478bd9Sstevel@tonic-gate 	struct pri_vol_desc *pvd;
4497c478bd9Sstevel@tonic-gate 	struct log_vol_desc *lvd;
4507c478bd9Sstevel@tonic-gate 	struct vol_desc_ptr *vds;
4517c478bd9Sstevel@tonic-gate 	struct unall_spc_desc *usd;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate begin:
4547c478bd9Sstevel@tonic-gate 	if ((addr = (uint8_t *)malloc(vds_len)) == NULL) {
4557c478bd9Sstevel@tonic-gate 		return (1);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4580e42dee6Sartem 	off = vds_loc * h->udfs.lbsize;
4590e42dee6Sartem 	if (ud_read_dev(h, off, addr, vds_len) != 0) {
4607c478bd9Sstevel@tonic-gate 		goto end;
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4630e42dee6Sartem 	for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
4640e42dee6Sartem 			taddr += h->udfs.lbsize, vds_loc ++) {
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		/* LINTED */
4677c478bd9Sstevel@tonic-gate 		tag = (struct tag *)taddr;
4687c478bd9Sstevel@tonic-gate 		id = SWAP_16(tag->tag_id);
4697c478bd9Sstevel@tonic-gate 		/*
4707c478bd9Sstevel@tonic-gate 		 * If you cannot verify the tag just skip it
4717c478bd9Sstevel@tonic-gate 		 * This is not a fatal error
4727c478bd9Sstevel@tonic-gate 		 */
4730e42dee6Sartem 		if (ud_verify_tag(h, tag, id, vds_loc, 1, 0) != 0) {
4747c478bd9Sstevel@tonic-gate 			continue;
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		switch (id) {
4777c478bd9Sstevel@tonic-gate 		case UD_PRI_VOL_DESC :
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 			/*
4807c478bd9Sstevel@tonic-gate 			 * Primary Volume Descriptor
4817c478bd9Sstevel@tonic-gate 			 */
4827c478bd9Sstevel@tonic-gate 			/* LINTED */
4837c478bd9Sstevel@tonic-gate 			pvd = (struct pri_vol_desc *)taddr;
4847c478bd9Sstevel@tonic-gate 			if ((v->pvd_len == 0) ||
4857c478bd9Sstevel@tonic-gate 				(SWAP_32(pvd->pvd_vdsn) > v->pvd_vdsn)) {
4867c478bd9Sstevel@tonic-gate 				v->pvd_vdsn = SWAP_32(pvd->pvd_vdsn);
4877c478bd9Sstevel@tonic-gate 				v->pvd_loc = vds_loc;
4880e42dee6Sartem 				v->pvd_len = h->udfs.lbsize;
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 			break;
4917c478bd9Sstevel@tonic-gate 		case UD_VOL_DESC_PTR :
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 			/*
4947c478bd9Sstevel@tonic-gate 			 * Curent sequence is continued from
4957c478bd9Sstevel@tonic-gate 			 * the location pointed by vdp
4967c478bd9Sstevel@tonic-gate 			 */
4977c478bd9Sstevel@tonic-gate 			/* LINTED */
4987c478bd9Sstevel@tonic-gate 			vds = (struct vol_desc_ptr *)taddr;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 			if (SWAP_32(vds->vdp_nvdse.ext_len) != 0) {
5017c478bd9Sstevel@tonic-gate 				vds_loc = SWAP_32(vds->vdp_nvdse.ext_loc);
5027c478bd9Sstevel@tonic-gate 				vds_len = SWAP_32(vds->vdp_nvdse.ext_len);
5037c478bd9Sstevel@tonic-gate 				free(addr);
5047c478bd9Sstevel@tonic-gate 				goto begin;
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 			break;
5077c478bd9Sstevel@tonic-gate 		case UD_IMPL_USE_DESC :
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 			/*
5107c478bd9Sstevel@tonic-gate 			 * Implementation Use Volume Descriptor
5117c478bd9Sstevel@tonic-gate 			 */
5127c478bd9Sstevel@tonic-gate 			v->iud_loc = vds_loc;
5130e42dee6Sartem 			v->iud_len = lb_roundup(512, h->udfs.lbsize);
5147c478bd9Sstevel@tonic-gate 			break;
5157c478bd9Sstevel@tonic-gate 		case UD_PART_DESC :
5167c478bd9Sstevel@tonic-gate 			{
5177c478bd9Sstevel@tonic-gate 				struct ud_part *p;
5180e42dee6Sartem 				struct phdr_desc *ph;
5197c478bd9Sstevel@tonic-gate 				struct part_desc *pd;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 				/*
5227c478bd9Sstevel@tonic-gate 				 * Partition Descriptor
5237c478bd9Sstevel@tonic-gate 				 */
5247c478bd9Sstevel@tonic-gate 				/* LINTED */
5257c478bd9Sstevel@tonic-gate 				pd = (struct part_desc *)taddr;
5267c478bd9Sstevel@tonic-gate 
5270e42dee6Sartem 				for (i = 0; i < h->n_parts; i++) {
5280e42dee6Sartem 					p = &h->part[i];
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 					if ((SWAP_16(pd->pd_pnum) ==
5317c478bd9Sstevel@tonic-gate 							p->udp_number) &&
5327c478bd9Sstevel@tonic-gate 						(SWAP_32(pd->pd_vdsn) >
5337c478bd9Sstevel@tonic-gate 							p->udp_seqno)) {
5347c478bd9Sstevel@tonic-gate 						break;
5357c478bd9Sstevel@tonic-gate 					}
5367c478bd9Sstevel@tonic-gate 				}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 				v->part_loc[i] = vds_loc;
5397c478bd9Sstevel@tonic-gate 				v->part_len[i] =
5400e42dee6Sartem 					lb_roundup(512, h->udfs.lbsize);
5417c478bd9Sstevel@tonic-gate 
5420e42dee6Sartem 				p = &h->part[i];
5437c478bd9Sstevel@tonic-gate 				p->udp_number = SWAP_16(pd->pd_pnum);
5447c478bd9Sstevel@tonic-gate 				p->udp_seqno = SWAP_32(pd->pd_vdsn);
5457c478bd9Sstevel@tonic-gate 				p->udp_access = SWAP_32(pd->pd_acc_type);
5467c478bd9Sstevel@tonic-gate 				p->udp_start = SWAP_32(pd->pd_part_start);
5477c478bd9Sstevel@tonic-gate 				p->udp_length = SWAP_32(pd->pd_part_length);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 				/* LINTED */
5500e42dee6Sartem 				ph = (struct phdr_desc *)pd->pd_pc_use;
5510e42dee6Sartem 				if (ph->phdr_ust.sad_ext_len) {
5527c478bd9Sstevel@tonic-gate 			p->udp_flags = UDP_SPACETBLS;
5530e42dee6Sartem 			p->udp_unall_loc = SWAP_32(ph->phdr_ust.sad_ext_loc);
5540e42dee6Sartem 			p->udp_unall_len = SWAP_32(ph->phdr_ust.sad_ext_len);
5550e42dee6Sartem 			p->udp_freed_loc = SWAP_32(ph->phdr_fst.sad_ext_loc);
5560e42dee6Sartem 			p->udp_freed_len = SWAP_32(ph->phdr_fst.sad_ext_len);
5577c478bd9Sstevel@tonic-gate 				} else {
5587c478bd9Sstevel@tonic-gate 			p->udp_flags = UDP_BITMAPS;
5590e42dee6Sartem 			p->udp_unall_loc = SWAP_32(ph->phdr_usb.sad_ext_loc);
5600e42dee6Sartem 			p->udp_unall_len = SWAP_32(ph->phdr_usb.sad_ext_len);
5610e42dee6Sartem 			p->udp_freed_loc = SWAP_32(ph->phdr_fsb.sad_ext_loc);
5620e42dee6Sartem 			p->udp_freed_len = SWAP_32(ph->phdr_fsb.sad_ext_len);
5637c478bd9Sstevel@tonic-gate 				}
5647c478bd9Sstevel@tonic-gate 
5650e42dee6Sartem 				if (i == h->n_parts) {
5660e42dee6Sartem 					h->n_parts ++;
5677c478bd9Sstevel@tonic-gate 				}
5687c478bd9Sstevel@tonic-gate 			}
5697c478bd9Sstevel@tonic-gate 			break;
5707c478bd9Sstevel@tonic-gate 		case UD_LOG_VOL_DESC :
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 			/*
5737c478bd9Sstevel@tonic-gate 			 * Logical Volume Descriptor
5747c478bd9Sstevel@tonic-gate 			 */
5757c478bd9Sstevel@tonic-gate 			/* LINTED */
5767c478bd9Sstevel@tonic-gate 			lvd = (struct log_vol_desc *)taddr;
5777c478bd9Sstevel@tonic-gate 			if ((v->lvd_len == 0) ||
5787c478bd9Sstevel@tonic-gate 				(SWAP_32(lvd->lvd_vdsn) > v->lvd_vdsn)) {
5797c478bd9Sstevel@tonic-gate 				v->lvd_vdsn = SWAP_32(lvd->lvd_vdsn);
5807c478bd9Sstevel@tonic-gate 				v->lvd_loc = vds_loc;
5817c478bd9Sstevel@tonic-gate 				v->lvd_len = ((uint32_t)
5827c478bd9Sstevel@tonic-gate 					&((struct log_vol_desc *)0)->lvd_pmaps);
5837c478bd9Sstevel@tonic-gate 				v->lvd_len =
5840e42dee6Sartem 					lb_roundup(v->lvd_len, h->udfs.lbsize);
5857c478bd9Sstevel@tonic-gate 			}
5867c478bd9Sstevel@tonic-gate 			break;
5877c478bd9Sstevel@tonic-gate 		case UD_UNALL_SPA_DESC :
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 			/*
5907c478bd9Sstevel@tonic-gate 			 * Unallocated Space Descriptor
5917c478bd9Sstevel@tonic-gate 			 */
5927c478bd9Sstevel@tonic-gate 			/* LINTED */
5937c478bd9Sstevel@tonic-gate 			usd = (struct unall_spc_desc *)taddr;
5947c478bd9Sstevel@tonic-gate 			v->usd_loc = vds_loc;
5957c478bd9Sstevel@tonic-gate 			v->usd_len = ((uint32_t)
5967c478bd9Sstevel@tonic-gate 			&((unall_spc_desc_t *)0)->ua_al_dsc) +
5977c478bd9Sstevel@tonic-gate 				SWAP_32(usd->ua_nad) *
5987c478bd9Sstevel@tonic-gate 				sizeof (struct extent_ad);
5990e42dee6Sartem 			v->usd_len = lb_roundup(v->usd_len, h->udfs.lbsize);
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 		case UD_TERM_DESC :
6027c478bd9Sstevel@tonic-gate 			/*
6037c478bd9Sstevel@tonic-gate 			 * Success fully completed
6047c478bd9Sstevel@tonic-gate 			 */
6057c478bd9Sstevel@tonic-gate 			goto end;
6067c478bd9Sstevel@tonic-gate 		default :
6077c478bd9Sstevel@tonic-gate 			/*
6087c478bd9Sstevel@tonic-gate 			 * If you donot undetstand any tag just skip
6097c478bd9Sstevel@tonic-gate 			 * it. This is not a fatal error
6107c478bd9Sstevel@tonic-gate 			 */
6117c478bd9Sstevel@tonic-gate 			break;
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate end:
6167c478bd9Sstevel@tonic-gate 	free(addr);
6177c478bd9Sstevel@tonic-gate 	if ((v->pvd_len == 0) ||
6187c478bd9Sstevel@tonic-gate 		(v->part_len[0] == 0) ||
6197c478bd9Sstevel@tonic-gate 		(v->lvd_len == 0)) {
6207c478bd9Sstevel@tonic-gate 		return (1);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	return (0);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6260e42dee6Sartem static int32_t
ud_read_and_translate_lvd(ud_handle_t h,uint32_t lvd_loc,uint32_t lvd_len)6270e42dee6Sartem ud_read_and_translate_lvd(ud_handle_t h, uint32_t lvd_loc, uint32_t lvd_len)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	caddr_t addr;
6307c478bd9Sstevel@tonic-gate 	uint16_t fsd_prn;
6317c478bd9Sstevel@tonic-gate 	uint32_t fsd_loc, fsd_len;
6327c478bd9Sstevel@tonic-gate 	uint32_t lvds_loc, lvds_len;
6337c478bd9Sstevel@tonic-gate 	uint64_t off;
6347c478bd9Sstevel@tonic-gate 	struct log_vol_desc *lvd = NULL;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	int32_t max_maps, i, mp_sz, index;
6377c478bd9Sstevel@tonic-gate 	struct ud_map *m;
6380e42dee6Sartem 	struct pmap_hdr *ph;
6397c478bd9Sstevel@tonic-gate 	struct pmap_typ1 *typ1;
6407c478bd9Sstevel@tonic-gate 	struct pmap_typ2 *typ2;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (lvd_len == 0) {
6437c478bd9Sstevel@tonic-gate 		return (1);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if ((lvd = (struct log_vol_desc *)
6477c478bd9Sstevel@tonic-gate 			malloc(lvd_len)) == NULL) {
6487c478bd9Sstevel@tonic-gate 		return (1);
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6510e42dee6Sartem 	off = lvd_loc * h->udfs.lbsize;
6520e42dee6Sartem 	if (ud_read_dev(h, off, (uint8_t *)lvd, lvd_len) != 0) {
6537c478bd9Sstevel@tonic-gate 		free(lvd);
6547c478bd9Sstevel@tonic-gate 		return (1);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6570e42dee6Sartem 	if (ud_verify_tag(h, &lvd->lvd_tag, UD_LOG_VOL_DESC,
6587c478bd9Sstevel@tonic-gate 			lvd_loc, 1, 0) != 0) {
6597c478bd9Sstevel@tonic-gate 		free(lvd);
6607c478bd9Sstevel@tonic-gate 		return (1);
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
6647c478bd9Sstevel@tonic-gate 	 * Take care of maps
6657c478bd9Sstevel@tonic-gate 	 */
6667c478bd9Sstevel@tonic-gate 	max_maps = SWAP_32(lvd->lvd_num_pmaps);
6670e42dee6Sartem 	ph = (struct pmap_hdr *)lvd->lvd_pmaps;
6680e42dee6Sartem 	for (h->n_maps = index = 0; index < max_maps; index++) {
6690e42dee6Sartem 		m = &h->maps[h->n_maps];
6700e42dee6Sartem 		switch (ph->maph_type) {
6717c478bd9Sstevel@tonic-gate 		case MAP_TYPE1 :
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 			/* LINTED */
6740e42dee6Sartem 			typ1 = (struct pmap_typ1 *)ph;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			m->udm_flags = UDM_MAP_NORM;
6777c478bd9Sstevel@tonic-gate 			m->udm_vsn = SWAP_16(typ1->map1_vsn);
6787c478bd9Sstevel@tonic-gate 			m->udm_pn = SWAP_16(typ1->map1_pn);
6790e42dee6Sartem 			h->n_maps++;
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		case MAP_TYPE2 :
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 			/* LINTED */
6850e42dee6Sartem 			typ2 = (struct pmap_typ2 *)ph;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 			if (strncmp(typ2->map2_pti.reg_id,
6887c478bd9Sstevel@tonic-gate 					UDF_VIRT_PART, 23) == 0) {
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 				m->udm_flags = UDM_MAP_VPM;
6917c478bd9Sstevel@tonic-gate 				m->udm_vsn = SWAP_16(typ2->map2_vsn);
6927c478bd9Sstevel@tonic-gate 				m->udm_pn = SWAP_16(typ2->map2_pn);
6937c478bd9Sstevel@tonic-gate 			} else if (strncmp(typ2->map2_pti.reg_id,
6947c478bd9Sstevel@tonic-gate 					UDF_SPAR_PART, 23) == 0) {
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 				if ((SWAP_16(typ2->map2_pl) != 32) ||
6977c478bd9Sstevel@tonic-gate 						(typ2->map2_nst < 1) ||
6987c478bd9Sstevel@tonic-gate 						(typ2->map2_nst > 4)) {
6997c478bd9Sstevel@tonic-gate 					break;
7007c478bd9Sstevel@tonic-gate 				}
7017c478bd9Sstevel@tonic-gate 				m->udm_flags = UDM_MAP_SPM;
7027c478bd9Sstevel@tonic-gate 				m->udm_vsn = SWAP_16(typ2->map2_vsn);
7037c478bd9Sstevel@tonic-gate 				m->udm_pn = SWAP_16(typ2->map2_pn);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 				m->udm_plen = SWAP_16(typ2->map2_pl);
7067c478bd9Sstevel@tonic-gate 				m->udm_nspm = typ2->map2_nst;
7077c478bd9Sstevel@tonic-gate 				m->udm_spsz = SWAP_32(typ2->map2_sest);
7087c478bd9Sstevel@tonic-gate 
7090e42dee6Sartem 				mp_sz = lb_roundup(m->udm_spsz, h->udfs.lbsize);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 				if ((addr = malloc(mp_sz * m->udm_nspm)) ==
7127c478bd9Sstevel@tonic-gate 						NULL) {
7137c478bd9Sstevel@tonic-gate 					break;
7147c478bd9Sstevel@tonic-gate 				}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 				for (i = 0; i < m->udm_nspm; i++) {
7177c478bd9Sstevel@tonic-gate 					m->udm_loc[i] =
7187c478bd9Sstevel@tonic-gate 						SWAP_32(typ2->map2_st[index]);
7197c478bd9Sstevel@tonic-gate 					m->udm_spaddr[i] = addr + i * mp_sz;
7207c478bd9Sstevel@tonic-gate 
7210e42dee6Sartem 					off = m->udm_loc[i] * h->udfs.lbsize;
7220e42dee6Sartem 					if (ud_read_dev(h, off,
7237c478bd9Sstevel@tonic-gate 						(uint8_t *)m->udm_spaddr[i],
7247c478bd9Sstevel@tonic-gate 							mp_sz) != 0) {
7257c478bd9Sstevel@tonic-gate 						m->udm_spaddr[i] = NULL;
7267c478bd9Sstevel@tonic-gate 						continue;
7277c478bd9Sstevel@tonic-gate 					}
7287c478bd9Sstevel@tonic-gate 				}
7297c478bd9Sstevel@tonic-gate 			}
7300e42dee6Sartem 			h->n_maps++;
7317c478bd9Sstevel@tonic-gate 		default :
7327c478bd9Sstevel@tonic-gate 			break;
7337c478bd9Sstevel@tonic-gate 		}
7340e42dee6Sartem 		ph = (struct pmap_hdr *)(((uint8_t *)h) + ph->maph_length);
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	lvds_loc = SWAP_32(lvd->lvd_int_seq_ext.ext_loc);
7387c478bd9Sstevel@tonic-gate 	lvds_len = SWAP_32(lvd->lvd_int_seq_ext.ext_len);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	fsd_prn = SWAP_16(lvd->lvd_lvcu.lad_ext_prn);
7417c478bd9Sstevel@tonic-gate 	fsd_loc = SWAP_32(lvd->lvd_lvcu.lad_ext_loc);
7427c478bd9Sstevel@tonic-gate 	fsd_len = SWAP_32(lvd->lvd_lvcu.lad_ext_len);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	free(lvd);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/*
7477c478bd9Sstevel@tonic-gate 	 * Get the latest LVID
7487c478bd9Sstevel@tonic-gate 	 */
7490e42dee6Sartem 	if (ud_get_latest_lvid(h, lvds_loc, lvds_len) != 0) {
7507c478bd9Sstevel@tonic-gate 		return (1);
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7530e42dee6Sartem 	if (ud_get_latest_fsd(h, fsd_prn, fsd_loc, fsd_len) != 0) {
7547c478bd9Sstevel@tonic-gate 		return (1);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	return (0);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7600e42dee6Sartem static int32_t
ud_get_latest_lvid(ud_handle_t h,uint32_t lvds_loc,uint32_t lvds_len)7610e42dee6Sartem ud_get_latest_lvid(ud_handle_t h, uint32_t lvds_loc, uint32_t lvds_len)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	uint8_t *addr, *taddr, *eaddr;
7647c478bd9Sstevel@tonic-gate 	uint16_t id;
7657c478bd9Sstevel@tonic-gate 	uint64_t off;
7667c478bd9Sstevel@tonic-gate 	struct tag *tag;
7677c478bd9Sstevel@tonic-gate 	struct log_vol_int_desc *lvid;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate begin:
7707c478bd9Sstevel@tonic-gate 	if ((addr = (uint8_t *)malloc(lvds_len)) == NULL) {
7717c478bd9Sstevel@tonic-gate 		return (1);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7740e42dee6Sartem 	off = lvds_loc * h->udfs.lbsize;
7750e42dee6Sartem 	if (ud_read_dev(h, off, addr, lvds_len) != 0) {
7767c478bd9Sstevel@tonic-gate 		goto end;
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7790e42dee6Sartem 	for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
7800e42dee6Sartem 			taddr += h->udfs.lbsize, lvds_loc ++) {
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		/* LINTED */
7837c478bd9Sstevel@tonic-gate 		tag = (struct tag *)taddr;
7847c478bd9Sstevel@tonic-gate 		id = SWAP_16(tag->tag_id);
7857c478bd9Sstevel@tonic-gate 		/*
7867c478bd9Sstevel@tonic-gate 		 * If you cannot verify the tag just skip it
7877c478bd9Sstevel@tonic-gate 		 * This is not a fatal error
7887c478bd9Sstevel@tonic-gate 		 */
7890e42dee6Sartem 		if (ud_verify_tag(h, tag, id, lvds_loc, 1, 0) != 0) {
7907c478bd9Sstevel@tonic-gate 			continue;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 		switch (id) {
7937c478bd9Sstevel@tonic-gate 		case UD_LOG_VOL_INT :
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 			/*
7967c478bd9Sstevel@tonic-gate 			 * Logical Volume Integrity Descriptor
7977c478bd9Sstevel@tonic-gate 			 */
7987c478bd9Sstevel@tonic-gate 			/* LINTED */
7997c478bd9Sstevel@tonic-gate 			lvid = (struct log_vol_int_desc *)taddr;
8000e42dee6Sartem 			h->udfs.lvid_loc = lvds_loc;
8010e42dee6Sartem 			h->udfs.lvid_len = ((uint32_t)
8027c478bd9Sstevel@tonic-gate 			&((struct log_vol_int_desc *)0)->lvid_fst) +
8037c478bd9Sstevel@tonic-gate 				SWAP_32(lvid->lvid_npart) * 8 +
8047c478bd9Sstevel@tonic-gate 				SWAP_32(lvid->lvid_liu);
8050e42dee6Sartem 			h->udfs.lvid_len = lb_roundup(h->udfs.lvid_len,
8060e42dee6Sartem 				h->udfs.lbsize);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 			/*
8097c478bd9Sstevel@tonic-gate 			 * It seems we have a next integrity
8107c478bd9Sstevel@tonic-gate 			 * sequence
8117c478bd9Sstevel@tonic-gate 			 */
8127c478bd9Sstevel@tonic-gate 			if (SWAP_32(lvid->lvid_nie.ext_len) != 0) {
8137c478bd9Sstevel@tonic-gate 				free(addr);
8147c478bd9Sstevel@tonic-gate 				lvds_loc = SWAP_32(lvid->lvid_nie.ext_loc);
8157c478bd9Sstevel@tonic-gate 				lvds_len = SWAP_32(lvid->lvid_nie.ext_len);
8167c478bd9Sstevel@tonic-gate 				goto begin;
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 			goto end;
8197c478bd9Sstevel@tonic-gate 		case UD_TERM_DESC :
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 			/*
8227c478bd9Sstevel@tonic-gate 			 * Success fully completed
8237c478bd9Sstevel@tonic-gate 			 */
8247c478bd9Sstevel@tonic-gate 				goto end;
8257c478bd9Sstevel@tonic-gate 		default :
8267c478bd9Sstevel@tonic-gate 			/*
8277c478bd9Sstevel@tonic-gate 			 * If you donot undetstand any tag just skip
8287c478bd9Sstevel@tonic-gate 			 * it. This is not a fatal error
8297c478bd9Sstevel@tonic-gate 			 */
8307c478bd9Sstevel@tonic-gate 			break;
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate end:
8347c478bd9Sstevel@tonic-gate 	free(addr);
8350e42dee6Sartem 	if (h->udfs.lvid_len == 0) {
8367c478bd9Sstevel@tonic-gate 		return (1);
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 	return (0);
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8410e42dee6Sartem static int32_t
ud_get_latest_fsd(ud_handle_t h,uint16_t fsd_prn,uint32_t fsd_loc,uint32_t fsd_len)8420e42dee6Sartem ud_get_latest_fsd(ud_handle_t h, uint16_t fsd_prn,
8437c478bd9Sstevel@tonic-gate 	uint32_t fsd_loc, uint32_t fsd_len)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	uint8_t *addr, *taddr, *eaddr;
8467c478bd9Sstevel@tonic-gate 	uint16_t id;
8477c478bd9Sstevel@tonic-gate 	uint64_t off;
8487c478bd9Sstevel@tonic-gate 	uint32_t fsds_loc, fsds_len;
8497c478bd9Sstevel@tonic-gate 	struct tag *tag;
8507c478bd9Sstevel@tonic-gate 	struct file_set_desc *fsd;
8517c478bd9Sstevel@tonic-gate 	uint32_t old_fsn = 0;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate begin:
8540e42dee6Sartem 	h->udfs.fsds_prn = fsd_prn;
8550e42dee6Sartem 	h->udfs.fsds_loc = fsd_loc;
8560e42dee6Sartem 	h->udfs.fsds_len = fsd_len;
8577c478bd9Sstevel@tonic-gate 
8580e42dee6Sartem 	fsds_loc = ud_xlate_to_daddr(h, fsd_prn, fsd_loc);
8590e42dee6Sartem 	fsds_len = lb_roundup(fsd_len, h->udfs.lbsize);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if ((addr = (uint8_t *)malloc(fsds_len)) == NULL) {
8627c478bd9Sstevel@tonic-gate 		return (1);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8650e42dee6Sartem 	off = fsds_loc * h->udfs.lbsize;
8660e42dee6Sartem 	if (ud_read_dev(h, off, addr, fsds_len) != 0) {
8677c478bd9Sstevel@tonic-gate 		goto end;
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8700e42dee6Sartem 	for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
8710e42dee6Sartem 			taddr += h->udfs.lbsize, fsds_loc ++) {
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		/* LINTED */
8747c478bd9Sstevel@tonic-gate 		tag = (struct tag *)taddr;
8757c478bd9Sstevel@tonic-gate 		id = SWAP_16(tag->tag_id);
8767c478bd9Sstevel@tonic-gate 		/*
8777c478bd9Sstevel@tonic-gate 		 * If you cannot verify the tag just skip it
8787c478bd9Sstevel@tonic-gate 		 * This is not a fatal error
8797c478bd9Sstevel@tonic-gate 		 */
8800e42dee6Sartem 		if (ud_verify_tag(h, tag, id, fsds_loc, 1, 0) != 0) {
8817c478bd9Sstevel@tonic-gate 			continue;
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 		switch (id) {
8847c478bd9Sstevel@tonic-gate 		case UD_FILE_SET_DESC :
8857c478bd9Sstevel@tonic-gate 			/* LINTED */
8867c478bd9Sstevel@tonic-gate 			fsd = (struct file_set_desc *)taddr;
8870e42dee6Sartem 			if ((h->udfs.fsd_len == 0) ||
8887c478bd9Sstevel@tonic-gate 				(SWAP_32(fsd->fsd_fs_no) > old_fsn)) {
8897c478bd9Sstevel@tonic-gate 				old_fsn = SWAP_32(fsd->fsd_fs_no);
8900e42dee6Sartem 				h->udfs.fsd_loc = fsds_loc;
8910e42dee6Sartem 				h->udfs.fsd_len = lb_roundup(512,
8920e42dee6Sartem 					h->udfs.lbsize);
8930e42dee6Sartem 				h->udfs.ricb_prn =
8947c478bd9Sstevel@tonic-gate 					SWAP_16(fsd->fsd_root_icb.lad_ext_prn);
8950e42dee6Sartem 				h->udfs.ricb_loc =
8967c478bd9Sstevel@tonic-gate 					SWAP_32(fsd->fsd_root_icb.lad_ext_loc);
8970e42dee6Sartem 				h->udfs.ricb_len =
8987c478bd9Sstevel@tonic-gate 					SWAP_32(fsd->fsd_root_icb.lad_ext_len);
8997c478bd9Sstevel@tonic-gate 			}
9007c478bd9Sstevel@tonic-gate 			if (SWAP_32(fsd->fsd_next.lad_ext_len) != 0) {
9017c478bd9Sstevel@tonic-gate 				fsd_prn = SWAP_16(fsd->fsd_next.lad_ext_prn);
9027c478bd9Sstevel@tonic-gate 				fsd_loc = SWAP_32(fsd->fsd_next.lad_ext_loc);
9037c478bd9Sstevel@tonic-gate 				fsd_len = SWAP_32(fsd->fsd_next.lad_ext_len);
9047c478bd9Sstevel@tonic-gate 				goto begin;
9057c478bd9Sstevel@tonic-gate 			}
9067c478bd9Sstevel@tonic-gate 			break;
9077c478bd9Sstevel@tonic-gate 		case UD_TERM_DESC :
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 			/*
9107c478bd9Sstevel@tonic-gate 			 * Success fully completed
9117c478bd9Sstevel@tonic-gate 			 */
9127c478bd9Sstevel@tonic-gate 			goto end;
9137c478bd9Sstevel@tonic-gate 		default :
9147c478bd9Sstevel@tonic-gate 			/*
9157c478bd9Sstevel@tonic-gate 			 * If you donot undetstand any tag just skip
9167c478bd9Sstevel@tonic-gate 			 * it. This is not a fatal error
9177c478bd9Sstevel@tonic-gate 			 */
9187c478bd9Sstevel@tonic-gate 			break;
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate end:
9237c478bd9Sstevel@tonic-gate 	free(addr);
9240e42dee6Sartem 	if (h->udfs.fsd_len == 0) {
9257c478bd9Sstevel@tonic-gate 		return (1);
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	return (0);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate int32_t
ud_get_num_blks(ud_handle_t h,uint32_t * blkno)9310e42dee6Sartem ud_get_num_blks(ud_handle_t h, uint32_t *blkno)
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate 	struct vtoc vtoc;
9347c478bd9Sstevel@tonic-gate 	struct dk_cinfo dki_info;
9357c478bd9Sstevel@tonic-gate 	int32_t error;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * Get VTOC from driver
9397c478bd9Sstevel@tonic-gate 	 */
9400e42dee6Sartem 	if ((error = ioctl(h->fd, DKIOCGVTOC, (intptr_t)&vtoc)) != 0) {
9417c478bd9Sstevel@tonic-gate 		return (error);
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * Verify if is proper
9467c478bd9Sstevel@tonic-gate 	 */
9477c478bd9Sstevel@tonic-gate 	if (vtoc.v_sanity != VTOC_SANE) {
9487c478bd9Sstevel@tonic-gate 		return (EINVAL);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * Get dk_cinfo from driver
9537c478bd9Sstevel@tonic-gate 	 */
9540e42dee6Sartem 	if ((error = ioctl(h->fd, DKIOCINFO, (intptr_t)&dki_info)) != 0) {
9557c478bd9Sstevel@tonic-gate 		return (error);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (dki_info.dki_partition >= V_NUMPAR) {
9597c478bd9Sstevel@tonic-gate 		return (EINVAL);
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	/*
9637c478bd9Sstevel@tonic-gate 	 * Return the size of the partition
9647c478bd9Sstevel@tonic-gate 	 */
9657c478bd9Sstevel@tonic-gate 	*blkno = vtoc.v_part[dki_info.dki_partition].p_size;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	return (0);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate uint32_t
ud_xlate_to_daddr(ud_handle_t h,uint16_t prn,uint32_t blkno)9710e42dee6Sartem ud_xlate_to_daddr(ud_handle_t h, uint16_t prn, uint32_t blkno)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	int32_t i;
9747c478bd9Sstevel@tonic-gate 	struct ud_map *m;
9757c478bd9Sstevel@tonic-gate 	struct ud_part *p;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 
9780e42dee6Sartem 	if (prn < h->n_maps) {
9790e42dee6Sartem 		m = &h->maps[prn];
9800e42dee6Sartem 		for (i = 0; i < h->n_parts; i++) {
9810e42dee6Sartem 			p = &h->part[i];
9827c478bd9Sstevel@tonic-gate 			if (m->udm_pn == p->udp_number) {
9837c478bd9Sstevel@tonic-gate 				return (p->udp_start + blkno);
9847c478bd9Sstevel@tonic-gate 			}
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 	return (0);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate /* ------ END Read and translate the on disk VDS to IN CORE format -------- */
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate int32_t
ud_verify_tag(ud_handle_t h,struct tag * tag,uint16_t id,uint32_t blockno,int32_t do_crc,int32_t print_msg)9930e42dee6Sartem ud_verify_tag(ud_handle_t h, struct tag *tag, uint16_t id,
9947c478bd9Sstevel@tonic-gate 	uint32_t blockno, int32_t do_crc, int32_t print_msg)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate 	int32_t i;
9977c478bd9Sstevel@tonic-gate 	uint8_t *addr, cksum = 0;
9987c478bd9Sstevel@tonic-gate 	uint16_t crc;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * Verify Tag Identifier
10037c478bd9Sstevel@tonic-gate 	 */
10047c478bd9Sstevel@tonic-gate 	if (tag->tag_id != SWAP_16(id)) {
10057c478bd9Sstevel@tonic-gate 		if (print_msg != 0) {
10060e42dee6Sartem 			(void) fprintf(stderr,
10077c478bd9Sstevel@tonic-gate 				gettext("tag does not verify tag %x req %x\n"),
10087c478bd9Sstevel@tonic-gate 				SWAP_16(tag->tag_id), id);
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 		return (1);
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	/*
10147c478bd9Sstevel@tonic-gate 	 * Verify Tag Descriptor Version
10157c478bd9Sstevel@tonic-gate 	 */
10160e42dee6Sartem 	if (SWAP_16(tag->tag_desc_ver) != h->udfs.ecma_version) {
10177c478bd9Sstevel@tonic-gate 		if (print_msg != 0) {
10180e42dee6Sartem 			(void) fprintf(stderr,
10197c478bd9Sstevel@tonic-gate 				gettext("tag version does not match with "
10207c478bd9Sstevel@tonic-gate 				"NSR descriptor version TAG %x NSR %x\n"),
10210e42dee6Sartem 				SWAP_16(tag->tag_desc_ver),
10220e42dee6Sartem 				h->udfs.ecma_version);
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 		return (1);
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * Caliculate Tag Checksum
10297c478bd9Sstevel@tonic-gate 	 */
10307c478bd9Sstevel@tonic-gate 	addr = (uint8_t *)tag;
10317c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 15; i++) {
10327c478bd9Sstevel@tonic-gate 		if (i != 4) {
10337c478bd9Sstevel@tonic-gate 			cksum += addr[i];
10347c478bd9Sstevel@tonic-gate 		}
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	/*
10387c478bd9Sstevel@tonic-gate 	 * Verify Tag Checksum
10397c478bd9Sstevel@tonic-gate 	 */
10407c478bd9Sstevel@tonic-gate 	if (cksum != tag->tag_cksum) {
10417c478bd9Sstevel@tonic-gate 		if (print_msg != 0) {
10420e42dee6Sartem 			(void) fprintf(stderr,
10437c478bd9Sstevel@tonic-gate 				gettext("Checksum Does not Verify TAG"
10447c478bd9Sstevel@tonic-gate 				" %x CALC %x\n"), tag->tag_cksum, cksum);
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 		return (1);
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	/*
10517c478bd9Sstevel@tonic-gate 	 * Do we want to do crc
10527c478bd9Sstevel@tonic-gate 	 */
10537c478bd9Sstevel@tonic-gate 	if (do_crc) {
10547c478bd9Sstevel@tonic-gate 		if (tag->tag_crc_len) {
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 			/*
10577c478bd9Sstevel@tonic-gate 			 * Caliculate CRC for the descriptor
10587c478bd9Sstevel@tonic-gate 			 */
10597c478bd9Sstevel@tonic-gate 			crc = ud_crc(addr + 0x10, SWAP_16(tag->tag_crc_len));
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 			/*
10627c478bd9Sstevel@tonic-gate 			 * Verify CRC
10637c478bd9Sstevel@tonic-gate 			 */
10647c478bd9Sstevel@tonic-gate 			if (crc != SWAP_16(tag->tag_crc)) {
10657c478bd9Sstevel@tonic-gate 				if (print_msg != 0) {
10660e42dee6Sartem 					(void) fprintf(stderr,
10677c478bd9Sstevel@tonic-gate 						gettext("CRC Does not verify"
10687c478bd9Sstevel@tonic-gate 						" TAG %x CALC %x %x\n"),
10697c478bd9Sstevel@tonic-gate 						SWAP_16(tag->tag_crc),
10707c478bd9Sstevel@tonic-gate 						crc, addr);
10717c478bd9Sstevel@tonic-gate 				}
10727c478bd9Sstevel@tonic-gate 			}
10737c478bd9Sstevel@tonic-gate 		}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		/*
10767c478bd9Sstevel@tonic-gate 		 * Verify Tag Location
10777c478bd9Sstevel@tonic-gate 		 */
10787c478bd9Sstevel@tonic-gate 		if (SWAP_32(blockno) != tag->tag_loc) {
10797c478bd9Sstevel@tonic-gate 			if (print_msg != 0) {
10800e42dee6Sartem 				(void) fprintf(stderr,
10817c478bd9Sstevel@tonic-gate 					gettext("Tag Location Does not verify"
10827c478bd9Sstevel@tonic-gate 					" blockno %x tag_blockno %x\n"),
10837c478bd9Sstevel@tonic-gate 					blockno, SWAP_32(tag->tag_loc));
10847c478bd9Sstevel@tonic-gate 			}
10857c478bd9Sstevel@tonic-gate 		}
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	return (0);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
10937c478bd9Sstevel@tonic-gate void
ud_make_tag(ud_handle_t h,struct tag * tag,uint16_t tag_id,uint32_t blkno,uint16_t crc_len)10940e42dee6Sartem ud_make_tag(ud_handle_t h, struct tag *tag, uint16_t tag_id,
10957c478bd9Sstevel@tonic-gate 	uint32_t blkno, uint16_t crc_len)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 	int32_t i;
10987c478bd9Sstevel@tonic-gate 	uint16_t crc;
10997c478bd9Sstevel@tonic-gate 	uint8_t *addr, cksum = 0;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	tag->tag_id = SWAP_16(tag_id);
11020e42dee6Sartem 	tag->tag_desc_ver = SWAP_16(h->udfs.ecma_version);
11037c478bd9Sstevel@tonic-gate 	tag->tag_cksum = 0;
11047c478bd9Sstevel@tonic-gate 	tag->tag_res = 0;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	/*
11077c478bd9Sstevel@tonic-gate 	 * Calicualte and assign CRC, CRC_LEN
11087c478bd9Sstevel@tonic-gate 	 */
11097c478bd9Sstevel@tonic-gate 	addr = (uint8_t *)tag;
11107c478bd9Sstevel@tonic-gate 	crc = ud_crc(addr + 0x10, crc_len);
11117c478bd9Sstevel@tonic-gate 	tag->tag_crc = SWAP_16(crc);
11127c478bd9Sstevel@tonic-gate 	tag->tag_crc_len = SWAP_16(crc_len);
11137c478bd9Sstevel@tonic-gate 	tag->tag_loc = SWAP_32(blkno);
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	/*
11167c478bd9Sstevel@tonic-gate 	 * Caliculate Checksum
11177c478bd9Sstevel@tonic-gate 	 */
11187c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 15; i++) {
11197c478bd9Sstevel@tonic-gate 		cksum += addr[i];
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * Assign Checksum
11247c478bd9Sstevel@tonic-gate 	 */
11257c478bd9Sstevel@tonic-gate 	tag->tag_cksum = cksum;
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate /* **************** udf specific subroutines *********************** */
11297c478bd9Sstevel@tonic-gate 
11300e42dee6Sartem static uint16_t ud_crc_table[256] = {
11317c478bd9Sstevel@tonic-gate 	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
11327c478bd9Sstevel@tonic-gate 	0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
11337c478bd9Sstevel@tonic-gate 	0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
11347c478bd9Sstevel@tonic-gate 	0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
11357c478bd9Sstevel@tonic-gate 	0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
11367c478bd9Sstevel@tonic-gate 	0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
11377c478bd9Sstevel@tonic-gate 	0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
11387c478bd9Sstevel@tonic-gate 	0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
11397c478bd9Sstevel@tonic-gate 	0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
11407c478bd9Sstevel@tonic-gate 	0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
11417c478bd9Sstevel@tonic-gate 	0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
11427c478bd9Sstevel@tonic-gate 	0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
11437c478bd9Sstevel@tonic-gate 	0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
11447c478bd9Sstevel@tonic-gate 	0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
11457c478bd9Sstevel@tonic-gate 	0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
11467c478bd9Sstevel@tonic-gate 	0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
11477c478bd9Sstevel@tonic-gate 	0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
11487c478bd9Sstevel@tonic-gate 	0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
11497c478bd9Sstevel@tonic-gate 	0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
11507c478bd9Sstevel@tonic-gate 	0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
11517c478bd9Sstevel@tonic-gate 	0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
11527c478bd9Sstevel@tonic-gate 	0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
11537c478bd9Sstevel@tonic-gate 	0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
11547c478bd9Sstevel@tonic-gate 	0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
11557c478bd9Sstevel@tonic-gate 	0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
11567c478bd9Sstevel@tonic-gate 	0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
11577c478bd9Sstevel@tonic-gate 	0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
11587c478bd9Sstevel@tonic-gate 	0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
11597c478bd9Sstevel@tonic-gate 	0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
11607c478bd9Sstevel@tonic-gate 	0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
11617c478bd9Sstevel@tonic-gate 	0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
11627c478bd9Sstevel@tonic-gate 	0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
11637c478bd9Sstevel@tonic-gate };
11647c478bd9Sstevel@tonic-gate 
11650e42dee6Sartem static uint16_t
ud_crc(uint8_t * addr,int32_t len)11667c478bd9Sstevel@tonic-gate ud_crc(uint8_t *addr, int32_t len)
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	uint16_t crc = 0;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	while (len-- > 0) {
11717c478bd9Sstevel@tonic-gate 		crc = ud_crc_table[(crc >> 8 ^ *addr++) & 0xff] ^ (crc<<8);
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	return (crc);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate #define	MAXNAMLEN	0x200
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate #define	POUND		0x0023
11817c478bd9Sstevel@tonic-gate #define	DOT		0x002E
11827c478bd9Sstevel@tonic-gate #define	SLASH		0x002F
11837c478bd9Sstevel@tonic-gate #define	UNDERBAR	0x005F
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate static uint16_t htoc[16] = {'0', '1', '2', '3',
11877c478bd9Sstevel@tonic-gate 	'4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
11887c478bd9Sstevel@tonic-gate /*
11897c478bd9Sstevel@tonic-gate  * unicode is the string of 16-bot characters
11907c478bd9Sstevel@tonic-gate  * length is the number of 16-bit characters
11917c478bd9Sstevel@tonic-gate  */
11920e42dee6Sartem static int32_t
UdfTxName(uint16_t * unicode,int32_t count)11937c478bd9Sstevel@tonic-gate UdfTxName(uint16_t *unicode, int32_t count)
11947c478bd9Sstevel@tonic-gate {
11957c478bd9Sstevel@tonic-gate 	int32_t i, j, k, lic, make_crc, dot_loc;
11967c478bd9Sstevel@tonic-gate 	uint16_t crc;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	if ((unicode[0] == DOT) &&
11997c478bd9Sstevel@tonic-gate 		((count == 1) || ((count == 2) && (unicode[1] == DOT)))) {
12007c478bd9Sstevel@tonic-gate 		crc = DOT;
12017c478bd9Sstevel@tonic-gate 		if (count == 2) {
12027c478bd9Sstevel@tonic-gate 			crc += DOT;
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 		unicode[0] = UNDERBAR;
12057c478bd9Sstevel@tonic-gate 		unicode[1] = POUND;
12067c478bd9Sstevel@tonic-gate 		unicode[2] = htoc[(uint16_t)(crc & 0xf000) >> 12];
12077c478bd9Sstevel@tonic-gate 		unicode[3] = htoc[(uint16_t)(crc & 0xf00) >> 8];
12087c478bd9Sstevel@tonic-gate 		unicode[4] = htoc[(uint16_t)(crc & 0xf0) >> 4];
12097c478bd9Sstevel@tonic-gate 		unicode[5] = htoc[crc & 0xf];
12107c478bd9Sstevel@tonic-gate 		return (6);
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate 	crc = 0;
12137c478bd9Sstevel@tonic-gate 	j = make_crc = 0;
12147c478bd9Sstevel@tonic-gate 	lic = dot_loc = -1;
12157c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
12167c478bd9Sstevel@tonic-gate 		if (make_crc) {
12177c478bd9Sstevel@tonic-gate 			crc += unicode[i];
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 		if (unicode[i] == DOT) {
12207c478bd9Sstevel@tonic-gate 			dot_loc = j;
12217c478bd9Sstevel@tonic-gate 		}
12227c478bd9Sstevel@tonic-gate 		if ((unicode[i] == SLASH) ||
12238509e9caSToomas Soome 			(unicode[i] == 0)) {
12247c478bd9Sstevel@tonic-gate 			if (make_crc == 0) {
12257c478bd9Sstevel@tonic-gate 				for (k = 0; k <= i; k++) {
12267c478bd9Sstevel@tonic-gate 					crc += unicode[k];
12277c478bd9Sstevel@tonic-gate 				}
12287c478bd9Sstevel@tonic-gate 				make_crc = 1;
12297c478bd9Sstevel@tonic-gate 			}
12307c478bd9Sstevel@tonic-gate 			if (lic != (i - 1)) {
12317c478bd9Sstevel@tonic-gate 				unicode[j++] = UNDERBAR;
12327c478bd9Sstevel@tonic-gate 			}
12337c478bd9Sstevel@tonic-gate 			lic = i;
12347c478bd9Sstevel@tonic-gate 		} else {
12357c478bd9Sstevel@tonic-gate 			unicode[j++] = unicode[i];
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	if (make_crc) {
12407c478bd9Sstevel@tonic-gate 		if (dot_loc != -1) {
12417c478bd9Sstevel@tonic-gate 			if ((j + 5) > MAXNAMLEN) {
12427c478bd9Sstevel@tonic-gate 				if ((j - dot_loc + 5) > MAXNAMLEN) {
12437c478bd9Sstevel@tonic-gate 					j = MAXNAMLEN - 5 + dot_loc;
12447c478bd9Sstevel@tonic-gate 					for (k = MAXNAMLEN;
12457c478bd9Sstevel@tonic-gate 						j >= dot_loc; k --, j--) {
12467c478bd9Sstevel@tonic-gate 						unicode[k] = unicode[j];
12477c478bd9Sstevel@tonic-gate 					}
12487c478bd9Sstevel@tonic-gate 					k = 0;
12497c478bd9Sstevel@tonic-gate 				} else {
12507c478bd9Sstevel@tonic-gate 					for (k = MAXNAMLEN;
12517c478bd9Sstevel@tonic-gate 						j >= dot_loc; k--, j--) {
12527c478bd9Sstevel@tonic-gate 						unicode[k] = unicode[j];
12537c478bd9Sstevel@tonic-gate 					}
12547c478bd9Sstevel@tonic-gate 					k -= 4;
12557c478bd9Sstevel@tonic-gate 				}
12567c478bd9Sstevel@tonic-gate 				j = MAXNAMLEN;
12577c478bd9Sstevel@tonic-gate 			} else {
12587c478bd9Sstevel@tonic-gate 				for (k = j; k >= dot_loc; k--) {
12597c478bd9Sstevel@tonic-gate 					unicode[k + 5] = unicode[k];
12607c478bd9Sstevel@tonic-gate 				}
12617c478bd9Sstevel@tonic-gate 				k = dot_loc;
12627c478bd9Sstevel@tonic-gate 				j += 5;
12637c478bd9Sstevel@tonic-gate 			}
12647c478bd9Sstevel@tonic-gate 		} else {
12657c478bd9Sstevel@tonic-gate 			if ((j + 5) > MAXNAMLEN) {
12667c478bd9Sstevel@tonic-gate 				j = MAXNAMLEN;
12677c478bd9Sstevel@tonic-gate 				k = MAXNAMLEN - 5;
12687c478bd9Sstevel@tonic-gate 			} else {
12697c478bd9Sstevel@tonic-gate 				k = j;
12707c478bd9Sstevel@tonic-gate 				j += 5;
12717c478bd9Sstevel@tonic-gate 			}
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 		unicode[k++] = POUND;
12747c478bd9Sstevel@tonic-gate 		unicode[k++] = htoc[(uint16_t)(crc & 0xf000) >> 12];
12757c478bd9Sstevel@tonic-gate 		unicode[k++] = htoc[(uint16_t)(crc & 0xf00) >> 8];
12767c478bd9Sstevel@tonic-gate 		unicode[k++] = htoc[(uint16_t)(crc & 0xf0) >> 4];
12777c478bd9Sstevel@tonic-gate 		unicode[k++] = htoc[crc & 0xf];
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 	return (j);
12807c478bd9Sstevel@tonic-gate }
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate /*
12837c478bd9Sstevel@tonic-gate  * Assumes the output buffer is large
12847c478bd9Sstevel@tonic-gate  * enough to hold the uncompressed
12857c478bd9Sstevel@tonic-gate  * code
12867c478bd9Sstevel@tonic-gate  */
12870e42dee6Sartem static int32_t
UncompressUnicode(int32_t numberOfBytes,uint8_t * UDFCompressed,uint16_t * unicode)12887c478bd9Sstevel@tonic-gate UncompressUnicode(
12897c478bd9Sstevel@tonic-gate 	int32_t numberOfBytes,	/* (Input) number of bytes read from media. */
12907c478bd9Sstevel@tonic-gate 	uint8_t *UDFCompressed,	/* (Input) bytes read from media. */
12917c478bd9Sstevel@tonic-gate 	uint16_t *unicode)	/* (Output) uncompressed unicode characters. */
12927c478bd9Sstevel@tonic-gate {
12937c478bd9Sstevel@tonic-gate 	int32_t compID;
12947c478bd9Sstevel@tonic-gate 	int32_t returnValue, unicodeIndex, byteIndex;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	/*
12987c478bd9Sstevel@tonic-gate 	 * Use UDFCompressed to store current byte being read.
12997c478bd9Sstevel@tonic-gate 	 */
13007c478bd9Sstevel@tonic-gate 	compID = UDFCompressed[0];
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	/* First check for valid compID. */
13037c478bd9Sstevel@tonic-gate 	if (compID != 8 && compID != 16) {
13047c478bd9Sstevel@tonic-gate 		returnValue = -1;
13057c478bd9Sstevel@tonic-gate 	} else {
13067c478bd9Sstevel@tonic-gate 		unicodeIndex = 0;
13077c478bd9Sstevel@tonic-gate 		byteIndex = 1;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 		/* Loop through all the bytes. */
13107c478bd9Sstevel@tonic-gate 		while (byteIndex < numberOfBytes) {
13117c478bd9Sstevel@tonic-gate 			if (compID == 16) {
13127c478bd9Sstevel@tonic-gate 				/*
13137c478bd9Sstevel@tonic-gate 				 * Move the first byte to the
13147c478bd9Sstevel@tonic-gate 				 * high bits of the unicode char.
13157c478bd9Sstevel@tonic-gate 				 */
13167c478bd9Sstevel@tonic-gate 				unicode[unicodeIndex] =
13177c478bd9Sstevel@tonic-gate 					UDFCompressed[byteIndex++] << 8;
13187c478bd9Sstevel@tonic-gate 			} else {
13197c478bd9Sstevel@tonic-gate 				unicode[unicodeIndex] = 0;
13207c478bd9Sstevel@tonic-gate 			}
13217c478bd9Sstevel@tonic-gate 			if (byteIndex < numberOfBytes) {
13227c478bd9Sstevel@tonic-gate 				/*
13237c478bd9Sstevel@tonic-gate 				 * Then the next byte to the low bits.
13247c478bd9Sstevel@tonic-gate 				 */
13257c478bd9Sstevel@tonic-gate 				unicode[unicodeIndex] |=
13267c478bd9Sstevel@tonic-gate 					UDFCompressed[byteIndex++];
13277c478bd9Sstevel@tonic-gate 			}
13287c478bd9Sstevel@tonic-gate 			unicodeIndex++;
13297c478bd9Sstevel@tonic-gate 		}
13307c478bd9Sstevel@tonic-gate 		returnValue = unicodeIndex;
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 	return (returnValue);
13337c478bd9Sstevel@tonic-gate }
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 
13390e42dee6Sartem static int32_t
ud_compressunicode(int32_t numberOfChars,int32_t compID,uint16_t * unicode,uint8_t * UDFCompressed)13407c478bd9Sstevel@tonic-gate ud_compressunicode(
13417c478bd9Sstevel@tonic-gate 	int32_t numberOfChars,	/* (Input) number of unicode characters. */
13427c478bd9Sstevel@tonic-gate 	int32_t compID,		/* (Input) compression ID to be used. */
13437c478bd9Sstevel@tonic-gate 	uint16_t *unicode,	/* (Input) unicode characters to compress. */
13447c478bd9Sstevel@tonic-gate 	uint8_t *UDFCompressed) /* (Output) compressed string, as bytes. */
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	int32_t byteIndex;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	if (compID != 8 && compID != 16) {
13497c478bd9Sstevel@tonic-gate 		/*
13507c478bd9Sstevel@tonic-gate 		 * Unsupported compression ID !
13517c478bd9Sstevel@tonic-gate 		 */
13527c478bd9Sstevel@tonic-gate 		byteIndex = -1;
13537c478bd9Sstevel@tonic-gate 	} else {
13547c478bd9Sstevel@tonic-gate 		/*
13557c478bd9Sstevel@tonic-gate 		 * Place compression code in first byte.
13567c478bd9Sstevel@tonic-gate 		 */
13577c478bd9Sstevel@tonic-gate 		UDFCompressed[0] = (uint8_t)compID;
13587c478bd9Sstevel@tonic-gate 		(void) strncpy((caddr_t)&UDFCompressed[1],
13597c478bd9Sstevel@tonic-gate 			(caddr_t)unicode, numberOfChars);
13607c478bd9Sstevel@tonic-gate 		byteIndex = numberOfChars + 1;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 	return (byteIndex);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 
13660e42dee6Sartem static int32_t
ud_convert2utf8(uint8_t * ibuf,uint8_t * obuf,int32_t length)13677c478bd9Sstevel@tonic-gate ud_convert2utf8(uint8_t *ibuf, uint8_t *obuf, int32_t length)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	int i, size;
13707c478bd9Sstevel@tonic-gate 	uint16_t *buf;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	/* LINTED */
13737c478bd9Sstevel@tonic-gate 	buf = (uint16_t *)obuf;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	size = UncompressUnicode(length, ibuf, buf);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	size = UdfTxName(buf, size);
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
13807c478bd9Sstevel@tonic-gate 		obuf[i] = (uint8_t)buf[i];
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 	obuf[i] = '\0';
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	return (size);
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate 
13870e42dee6Sartem static int32_t
ud_convert2utf16(uint8_t * ibuf,uint8_t * obuf,int32_t length)13887c478bd9Sstevel@tonic-gate ud_convert2utf16(uint8_t *ibuf, uint8_t *obuf, int32_t length)
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate 	int32_t comp_len;
13917c478bd9Sstevel@tonic-gate 	uint16_t *ptr;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	/* LINTED */
13947c478bd9Sstevel@tonic-gate 	ptr = (uint16_t *)ibuf;
13957c478bd9Sstevel@tonic-gate 	comp_len = ud_compressunicode(length, 8, ptr, obuf);
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	return (comp_len);
13987c478bd9Sstevel@tonic-gate }
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate /*
14017c478bd9Sstevel@tonic-gate  * Assumption code set is zero in udfs
14027c478bd9Sstevel@tonic-gate  */
14037c478bd9Sstevel@tonic-gate void
ud_convert2local(int8_t * ibuf,int8_t * obuf,int32_t length)14047c478bd9Sstevel@tonic-gate ud_convert2local(int8_t *ibuf, int8_t *obuf, int32_t length)
14057c478bd9Sstevel@tonic-gate {
14067c478bd9Sstevel@tonic-gate 	wchar_t buf4c[128];
14077c478bd9Sstevel@tonic-gate 	int32_t i, comp, index;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	/*
14107c478bd9Sstevel@tonic-gate 	 * Special uncompress code
14117c478bd9Sstevel@tonic-gate 	 * written to accomodate solaris wchar_t
14127c478bd9Sstevel@tonic-gate 	 */
14137c478bd9Sstevel@tonic-gate 	comp = ibuf[0];
14147c478bd9Sstevel@tonic-gate 	for (i = 0, index = 1; i < length; i++) {
14157c478bd9Sstevel@tonic-gate 		if (comp == 16) {
14167c478bd9Sstevel@tonic-gate 			buf4c[i] = ibuf[index++] << 8;
14177c478bd9Sstevel@tonic-gate 		} else {
14187c478bd9Sstevel@tonic-gate 			buf4c[i] = 0;
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 		if (index < length) {
14217c478bd9Sstevel@tonic-gate 			buf4c[i] |= ibuf[index++];
14227c478bd9Sstevel@tonic-gate 		}
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	(void) wcstombs((char *)obuf, buf4c, 128);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate /* ------------ Routines to print basic structures Part 1 ---------------- */
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate void
print_charspec(FILE * fout,char * name,struct charspec * cspec)14330e42dee6Sartem print_charspec(FILE *fout, char *name, struct charspec *cspec)
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate 	int i = 0;
14367c478bd9Sstevel@tonic-gate 
14370e42dee6Sartem 	(void) fprintf(fout,
14387c478bd9Sstevel@tonic-gate 		"%s : %x - \"", name, cspec->cs_type);
14397c478bd9Sstevel@tonic-gate 	for (i = 0; i < 63; i++) {
14400e42dee6Sartem 		(void) fprintf(fout,
14417c478bd9Sstevel@tonic-gate 			"%c", cspec->cs_info[i]);
14427c478bd9Sstevel@tonic-gate 	}
14430e42dee6Sartem 	(void) fprintf(fout, "\n");
14447c478bd9Sstevel@tonic-gate }
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate /* ARGSUSED */
14477c478bd9Sstevel@tonic-gate void
print_dstring(FILE * fout,char * name,uint16_t cset,char * bufc,uint8_t length)14480e42dee6Sartem print_dstring(FILE *fout, char *name, uint16_t cset, char *bufc, uint8_t length)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	int8_t bufmb[1024];
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	ud_convert2local(bufc, bufmb, length);
14537c478bd9Sstevel@tonic-gate 
14540e42dee6Sartem 	(void) fprintf(fout,
14557c478bd9Sstevel@tonic-gate 		"%s %s\n", name, bufmb);
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate void
set_dstring(dstring_t * dp,char * cp,int32_t len)14597c478bd9Sstevel@tonic-gate set_dstring(dstring_t *dp, char *cp, int32_t len)
14607c478bd9Sstevel@tonic-gate {
14617c478bd9Sstevel@tonic-gate 	int32_t length;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	bzero(dp, len);
14647c478bd9Sstevel@tonic-gate 	length = strlen(cp);
14657c478bd9Sstevel@tonic-gate 	if (length > len - 1) {
14667c478bd9Sstevel@tonic-gate 		length = len - 1;
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 	(void) strncpy(dp, cp, length);
14697c478bd9Sstevel@tonic-gate 	dp[len - 1] = length;
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate void
print_tstamp(FILE * fout,char * name,tstamp_t * ts)14730e42dee6Sartem print_tstamp(FILE *fout, char *name, tstamp_t *ts)
14747c478bd9Sstevel@tonic-gate {
14750e42dee6Sartem 	(void) fprintf(fout, "%s tz : %d yr : %d mo : %d da : %d "
14767c478bd9Sstevel@tonic-gate 		"Time : %d : %d : %d : %d : %d : %d\n", name,
14777c478bd9Sstevel@tonic-gate 		SWAP_16(ts->ts_tzone), SWAP_16(ts->ts_year), ts->ts_month,
14787c478bd9Sstevel@tonic-gate 		ts->ts_day, ts->ts_hour, ts->ts_min, ts->ts_sec, ts->ts_csec,
14797c478bd9Sstevel@tonic-gate 		ts->ts_husec, ts->ts_usec);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate void
make_regid(ud_handle_t h,struct regid * reg,char * id,int32_t type)14850e42dee6Sartem make_regid(ud_handle_t h, struct regid *reg, char *id, int32_t type)
14867c478bd9Sstevel@tonic-gate {
14877c478bd9Sstevel@tonic-gate 	reg->reg_flags = 0;
14887c478bd9Sstevel@tonic-gate 	(void) strncpy(reg->reg_id, id, 23);
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	if (type == REG_DOM_ID) {
14917c478bd9Sstevel@tonic-gate 		struct dom_id_suffix *dis;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 		/* LINTED */
14947c478bd9Sstevel@tonic-gate 		dis = (struct dom_id_suffix *)reg->reg_ids;
14950e42dee6Sartem 		dis->dis_udf_revison = SWAP_16(h->udfs.ma_write);
14967c478bd9Sstevel@tonic-gate 		dis->dis_domain_flags = 0;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	} else if (type == REG_UDF_ID) {
14997c478bd9Sstevel@tonic-gate 		struct udf_id_suffix *uis;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 		/* LINTED */
15027c478bd9Sstevel@tonic-gate 		uis = (struct udf_id_suffix *)reg->reg_ids;
15030e42dee6Sartem 		uis->uis_udf_revision = SWAP_16(h->udfs.ma_write);
15047c478bd9Sstevel@tonic-gate 		uis->uis_os_class = OS_CLASS_UNIX;
15057c478bd9Sstevel@tonic-gate 		uis->uis_os_identifier = OS_IDENTIFIER_SOLARIS;
15067c478bd9Sstevel@tonic-gate 	} else if (type == REG_UDF_II) {
15077c478bd9Sstevel@tonic-gate 		struct impl_id_suffix *iis;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 		iis = (struct impl_id_suffix *)reg->reg_ids;
15107c478bd9Sstevel@tonic-gate 		iis->iis_os_class = OS_CLASS_UNIX;
15117c478bd9Sstevel@tonic-gate 		iis->iis_os_identifier = OS_IDENTIFIER_SOLARIS;
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate void
print_regid(FILE * fout,char * name,struct regid * reg,int32_t type)15160e42dee6Sartem print_regid(FILE *fout, char *name, struct regid *reg, int32_t type)
15177c478bd9Sstevel@tonic-gate {
15180e42dee6Sartem 	(void) fprintf(fout, "%s : 0x%x : \"%s\" :",
15197c478bd9Sstevel@tonic-gate 		name, reg->reg_flags, reg->reg_id);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	if (type == REG_DOM_ID) {
15227c478bd9Sstevel@tonic-gate 		struct dom_id_suffix *dis;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 		/* LINTED */
15257c478bd9Sstevel@tonic-gate 		dis = (struct dom_id_suffix *)reg->reg_ids;
15260e42dee6Sartem 		(void) fprintf(fout, " 0x%x : %s : %s\n",
15277c478bd9Sstevel@tonic-gate 			SWAP_16(dis->dis_udf_revison),
15287c478bd9Sstevel@tonic-gate 			(dis->dis_domain_flags & PROTECT_SOFT_WRITE) ?
15297c478bd9Sstevel@tonic-gate 				"HW Protect" : "No HW Write Protect",
15307c478bd9Sstevel@tonic-gate 			(dis->dis_domain_flags & PROTECT_HARD_WRITE) ?
15317c478bd9Sstevel@tonic-gate 				"SW Protect" : "No SW Protect");
15327c478bd9Sstevel@tonic-gate 	} else if (type == REG_UDF_ID) {
15337c478bd9Sstevel@tonic-gate 		struct udf_id_suffix *uis;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 		/* LINTED */
15367c478bd9Sstevel@tonic-gate 		uis = (struct udf_id_suffix *)reg->reg_ids;
15370e42dee6Sartem 		(void) fprintf(fout,
15387c478bd9Sstevel@tonic-gate 			" 0x%x : OS Class 0x%x : OS Identifier 0x%x\n",
15397c478bd9Sstevel@tonic-gate 			SWAP_16(uis->uis_udf_revision),
15407c478bd9Sstevel@tonic-gate 			uis->uis_os_class, uis->uis_os_identifier);
15417c478bd9Sstevel@tonic-gate 	} else {
15427c478bd9Sstevel@tonic-gate 		struct impl_id_suffix *iis;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 		iis = (struct impl_id_suffix *)reg->reg_ids;
15450e42dee6Sartem 		(void) fprintf(fout,
15467c478bd9Sstevel@tonic-gate 			" OS Class 0x%x : OS Identifier 0x%x\n",
15477c478bd9Sstevel@tonic-gate 			iis->iis_os_class, iis->iis_os_identifier);
15487c478bd9Sstevel@tonic-gate 	}
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate #ifdef	OLD
15527c478bd9Sstevel@tonic-gate void
print_regid(FILE * fout,char * name,struct regid * reg)15530e42dee6Sartem print_regid(FILE *fout, char *name, struct regid *reg)
15547c478bd9Sstevel@tonic-gate {
15550e42dee6Sartem 	(void) fprintf(fout, "%s : 0x%x : \"%s\" :",
15567c478bd9Sstevel@tonic-gate 		name, reg->reg_flags, reg->reg_id);
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	if (strncmp(reg->reg_id, "*OSTA UDF Compliant", 19) == 0) {
15590e42dee6Sartem 		(void) fprintf(fout, " 0x%x : %s : %s\n",
15607c478bd9Sstevel@tonic-gate 			reg->reg_ids[0] | (reg->reg_ids[1] << 8),
15617c478bd9Sstevel@tonic-gate 			(reg->reg_ids[2] & 1) ?
15627c478bd9Sstevel@tonic-gate 				"HW Protect" : "No HW Write Protect",
15637c478bd9Sstevel@tonic-gate 			(reg->reg_ids[2] & 2) ?
15647c478bd9Sstevel@tonic-gate 				"SW Protect" : "No SW Protect");
15657c478bd9Sstevel@tonic-gate 	} else if ((strncmp(reg->reg_id, "*UDF Virtual Partition", 22) == 0) ||
15667c478bd9Sstevel@tonic-gate 		(strncmp(reg->reg_id, "*UDF Sparable Partition", 23) == 0) ||
15677c478bd9Sstevel@tonic-gate 		(strncmp(reg->reg_id, "*UDF Virtual Alloc Tbl", 22) == 0) ||
15687c478bd9Sstevel@tonic-gate 		(strncmp(reg->reg_id, "*UDF Sparing Table", 18) == 0)) {
15690e42dee6Sartem 		(void) fprintf(fout,
15707c478bd9Sstevel@tonic-gate 			" 0x%x : OS Class 0x%x : OS Identifier 0x%x\n",
15717c478bd9Sstevel@tonic-gate 			reg->reg_ids[0] | (reg->reg_ids[1] << 8),
15727c478bd9Sstevel@tonic-gate 			reg->reg_ids[2], reg->reg_ids[3]);
15737c478bd9Sstevel@tonic-gate 	} else {
15740e42dee6Sartem 		(void) fprintf(fout,
15757c478bd9Sstevel@tonic-gate 			" OS Class 0x%x : OS Identifier 0x%x\n",
15767c478bd9Sstevel@tonic-gate 			reg->reg_ids[0], reg->reg_ids[1]);
15777c478bd9Sstevel@tonic-gate 	}
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate #endif
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate /* ------------ Routines to print basic structures Part 2 ---------------- */
15837c478bd9Sstevel@tonic-gate /*
15847c478bd9Sstevel@tonic-gate  * Part 2
15857c478bd9Sstevel@tonic-gate  * This part is OS specific and is currently
15867c478bd9Sstevel@tonic-gate  * not supported
15877c478bd9Sstevel@tonic-gate  */
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate /* ------------ Routines to print basic structures Part 3 ---------------- */
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate void
print_ext_ad(FILE * fout,char * name,struct extent_ad * ead)15920e42dee6Sartem print_ext_ad(FILE *fout, char *name, struct extent_ad *ead)
15937c478bd9Sstevel@tonic-gate {
15940e42dee6Sartem 	(void) fprintf(fout,
15957c478bd9Sstevel@tonic-gate 		"%s EAD Len %x Loc %x\n",
15967c478bd9Sstevel@tonic-gate 		name, SWAP_32(ead->ext_len), SWAP_32(ead->ext_loc));
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate void
print_tag(FILE * fout,struct tag * tag)16000e42dee6Sartem print_tag(FILE *fout, struct tag *tag)
16017c478bd9Sstevel@tonic-gate {
16020e42dee6Sartem 	(void) fprintf(fout,
16037c478bd9Sstevel@tonic-gate 		"tag_id : %x ver : %x cksum : %x "
16047c478bd9Sstevel@tonic-gate 		"sno : %x crc : %x crc_len : %x loc : %x\n",
16057c478bd9Sstevel@tonic-gate 		SWAP_16(tag->tag_id), SWAP_16(tag->tag_desc_ver),
16067c478bd9Sstevel@tonic-gate 		tag->tag_cksum, SWAP_16(tag->tag_sno),
16077c478bd9Sstevel@tonic-gate 		SWAP_16(tag->tag_crc), SWAP_16(tag->tag_crc_len),
16087c478bd9Sstevel@tonic-gate 		SWAP_32(tag->tag_loc));
16097c478bd9Sstevel@tonic-gate }
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate void
print_pvd(FILE * fout,struct pri_vol_desc * pvd)16130e42dee6Sartem print_pvd(FILE *fout, struct pri_vol_desc *pvd)
16147c478bd9Sstevel@tonic-gate {
16150e42dee6Sartem 	(void) fprintf(fout,
16167c478bd9Sstevel@tonic-gate 		"\n\t\t\tPrimary Volume Descriptor\n");
16170e42dee6Sartem 	print_tag(fout, &pvd->pvd_tag);
16180e42dee6Sartem 	(void) fprintf(fout, "vdsn : %x vdn : %x\n",
16197c478bd9Sstevel@tonic-gate 		SWAP_32(pvd->pvd_vdsn), SWAP_32(pvd->pvd_pvdn));
16200e42dee6Sartem 	print_dstring(fout, "volid : ", pvd->pvd_desc_cs.cs_type,
16217c478bd9Sstevel@tonic-gate 			pvd->pvd_vol_id, 32);
16220e42dee6Sartem 	(void) fprintf(fout,
16237c478bd9Sstevel@tonic-gate 		"vsn : %x mvsn : %x il : %x mil :"
16247c478bd9Sstevel@tonic-gate 		" %x csl : %x mcsl %x\n",
16257c478bd9Sstevel@tonic-gate 		SWAP_16(pvd->pvd_vsn), SWAP_16(pvd->pvd_mvsn),
16267c478bd9Sstevel@tonic-gate 		SWAP_16(pvd->pvd_il), SWAP_16(pvd->pvd_mil),
16277c478bd9Sstevel@tonic-gate 		SWAP_32(pvd->pvd_csl), SWAP_32(pvd->pvd_mcsl));
16280e42dee6Sartem 	print_dstring(fout, "vsid :", pvd->pvd_desc_cs.cs_type,
16297c478bd9Sstevel@tonic-gate 			pvd->pvd_vsi, 128);
16300e42dee6Sartem 	print_charspec(fout, "desc_cs", &pvd->pvd_desc_cs);
16310e42dee6Sartem 	print_charspec(fout, "exp_cs", &pvd->pvd_exp_cs);
16320e42dee6Sartem 	print_ext_ad(fout, "val ", &pvd->pvd_vol_abs);
16330e42dee6Sartem 	print_ext_ad(fout, "vcnl ", &pvd->pvd_vcn);
16340e42dee6Sartem 	print_regid(fout, "ai", &pvd->pvd_appl_id, REG_UDF_II);
16350e42dee6Sartem 	print_regid(fout, "ii", &pvd->pvd_ii, REG_UDF_II);
16360e42dee6Sartem 	(void) fprintf(fout, "pvdsl : %x flags : %x\n",
16377c478bd9Sstevel@tonic-gate 		SWAP_32(pvd->pvd_pvdsl),
16387c478bd9Sstevel@tonic-gate 		SWAP_16(pvd->pvd_flags));
16397c478bd9Sstevel@tonic-gate }
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate void
print_avd(FILE * fout,struct anch_vol_desc_ptr * avdp)16420e42dee6Sartem print_avd(FILE *fout, struct anch_vol_desc_ptr *avdp)
16437c478bd9Sstevel@tonic-gate {
16440e42dee6Sartem 	(void) fprintf(fout,
16457c478bd9Sstevel@tonic-gate 		"\n\t\t\tAnchor Volume Descriptor\n");
16460e42dee6Sartem 	print_tag(fout, &avdp->avd_tag);
16470e42dee6Sartem 	print_ext_ad(fout, "Main Volume Descriptor Sequence : ",
16487c478bd9Sstevel@tonic-gate 			&avdp->avd_main_vdse);
16490e42dee6Sartem 	print_ext_ad(fout, "Reserve Volume Descriptor Sequence : ",
16507c478bd9Sstevel@tonic-gate 			&avdp->avd_res_vdse);
16517c478bd9Sstevel@tonic-gate }
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate void
print_vdp(FILE * fout,struct vol_desc_ptr * vdp)16540e42dee6Sartem print_vdp(FILE *fout, struct vol_desc_ptr *vdp)
16557c478bd9Sstevel@tonic-gate {
16560e42dee6Sartem 	(void) fprintf(fout,
16577c478bd9Sstevel@tonic-gate 		"\n\t\t\tVolume Descriptor Pointer\n");
16580e42dee6Sartem 	print_tag(fout, &vdp->vdp_tag);
16590e42dee6Sartem 	(void) fprintf(fout, "vdsn : %x ",
16607c478bd9Sstevel@tonic-gate 		SWAP_32(vdp->vdp_vdsn));
16610e42dee6Sartem 	print_ext_ad(fout, "vdse ", &vdp->vdp_nvdse);
16627c478bd9Sstevel@tonic-gate }
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate void
print_iuvd(FILE * fout,struct iuvd_desc * iuvd)16650e42dee6Sartem print_iuvd(FILE *fout, struct iuvd_desc *iuvd)
16667c478bd9Sstevel@tonic-gate {
16670e42dee6Sartem 	(void) fprintf(fout,
16687c478bd9Sstevel@tonic-gate 		"\n\t\t\tImplementation Use Volume Descriptor\n");
16690e42dee6Sartem 	print_tag(fout, &iuvd->iuvd_tag);
16700e42dee6Sartem 	(void) fprintf(fout,
16717c478bd9Sstevel@tonic-gate 		"vdsn : %x ", SWAP_32(iuvd->iuvd_vdsn));
16720e42dee6Sartem 	print_regid(fout, "Impl Id : ", &iuvd->iuvd_ii, REG_UDF_ID);
16730e42dee6Sartem 	print_charspec(fout, "cset ", &iuvd->iuvd_cset);
16740e42dee6Sartem 	print_dstring(fout, "lvi : ", iuvd->iuvd_cset.cs_type,
16757c478bd9Sstevel@tonic-gate 			iuvd->iuvd_lvi, 128);
16760e42dee6Sartem 	print_dstring(fout, "ifo1 : ", iuvd->iuvd_cset.cs_type,
16777c478bd9Sstevel@tonic-gate 			iuvd->iuvd_ifo1, 36);
16780e42dee6Sartem 	print_dstring(fout, "ifo2 : ", iuvd->iuvd_cset.cs_type,
16797c478bd9Sstevel@tonic-gate 			iuvd->iuvd_ifo2, 36);
16800e42dee6Sartem 	print_dstring(fout, "ifo3 : ", iuvd->iuvd_cset.cs_type,
16817c478bd9Sstevel@tonic-gate 			iuvd->iuvd_ifo3, 36);
16827c478bd9Sstevel@tonic-gate 
16830e42dee6Sartem 	print_regid(fout, "iid ", &iuvd->iuvd_iid, REG_UDF_II);
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate void
print_part(FILE * fout,struct part_desc * pd)16870e42dee6Sartem print_part(FILE *fout, struct part_desc *pd)
16887c478bd9Sstevel@tonic-gate {
16890e42dee6Sartem 	(void) fprintf(fout,
16907c478bd9Sstevel@tonic-gate 		"\n\t\t\tPartition Descriptor\n");
16910e42dee6Sartem 	print_tag(fout, &pd->pd_tag);
16920e42dee6Sartem 	(void) fprintf(fout,
16937c478bd9Sstevel@tonic-gate 		"vdsn : %x flags : %x num : %x ",
16947c478bd9Sstevel@tonic-gate 		SWAP_32(pd->pd_vdsn),
16957c478bd9Sstevel@tonic-gate 		SWAP_16(pd->pd_pflags),
16967c478bd9Sstevel@tonic-gate 		SWAP_16(pd->pd_pnum));
16970e42dee6Sartem 	print_regid(fout, "contents ", &pd->pd_pcontents, REG_UDF_II);
16987c478bd9Sstevel@tonic-gate 	/* LINTED */
16990e42dee6Sartem 	print_phdr(fout, (struct phdr_desc *)(&pd->pd_pc_use));
17000e42dee6Sartem 	(void) fprintf(fout,
17017c478bd9Sstevel@tonic-gate 		"acc : %x start : %x length : %x ",
17027c478bd9Sstevel@tonic-gate 		SWAP_32(pd->pd_acc_type),
17037c478bd9Sstevel@tonic-gate 		SWAP_32(pd->pd_part_start),
17047c478bd9Sstevel@tonic-gate 		SWAP_32(pd->pd_part_length));
17050e42dee6Sartem 	print_regid(fout, "Impl Id : ", &pd->pd_ii, REG_UDF_II);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate void
print_lvd(FILE * fout,struct log_vol_desc * lvd)17090e42dee6Sartem print_lvd(FILE *fout, struct log_vol_desc *lvd)
17107c478bd9Sstevel@tonic-gate {
17110e42dee6Sartem 	(void) fprintf(fout,
17127c478bd9Sstevel@tonic-gate 		"\n\t\t\tLogical Volume Descriptor\n");
17130e42dee6Sartem 	print_tag(fout, &lvd->lvd_tag);
17140e42dee6Sartem 	(void) fprintf(fout,
17157c478bd9Sstevel@tonic-gate 		"vdsn : %x ", SWAP_32(lvd->lvd_vdsn));
17160e42dee6Sartem 	print_charspec(fout, "Desc Char Set ", &lvd->lvd_desc_cs);
17170e42dee6Sartem 	print_dstring(fout, "lvid : ", lvd->lvd_desc_cs.cs_type,
17187c478bd9Sstevel@tonic-gate 			lvd->lvd_lvid, 28);
17190e42dee6Sartem 	(void) fprintf(fout,
17207c478bd9Sstevel@tonic-gate 		"lbsize : %x ",
17217c478bd9Sstevel@tonic-gate 		SWAP_32(lvd->lvd_log_bsize));
17220e42dee6Sartem 	print_regid(fout, "Dom Id", &lvd->lvd_dom_id, REG_DOM_ID);
17230e42dee6Sartem 	print_long_ad(fout, "lvcu", &lvd->lvd_lvcu);
17240e42dee6Sartem 	(void) fprintf(fout,
17257c478bd9Sstevel@tonic-gate 		"mtlen : %x nmaps : %x ",
17267c478bd9Sstevel@tonic-gate 		SWAP_32(lvd->lvd_mtbl_len),
17277c478bd9Sstevel@tonic-gate 		SWAP_32(lvd->lvd_num_pmaps));
17280e42dee6Sartem 	print_regid(fout, "Impl Id : ", &lvd->lvd_ii, REG_UDF_II);
17290e42dee6Sartem 	print_ext_ad(fout, "Int Seq", &lvd->lvd_int_seq_ext);
17300e42dee6Sartem 	print_pmaps(fout, lvd->lvd_pmaps, SWAP_32(lvd->lvd_num_pmaps));
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate void
print_usd(FILE * fout,struct unall_spc_desc * ua)17340e42dee6Sartem print_usd(FILE *fout, struct unall_spc_desc *ua)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate 	int32_t i, count;
17377c478bd9Sstevel@tonic-gate 
17380e42dee6Sartem 	(void) fprintf(fout,
17397c478bd9Sstevel@tonic-gate 		"\n\t\t\tUnallocated Space Descriptor\n");
17400e42dee6Sartem 	print_tag(fout, &ua->ua_tag);
17417c478bd9Sstevel@tonic-gate 	count = SWAP_32(ua->ua_nad);
17420e42dee6Sartem 	(void) fprintf(fout,
17437c478bd9Sstevel@tonic-gate 		"vdsn : %x nad : %x\n",
17447c478bd9Sstevel@tonic-gate 		SWAP_32(ua->ua_vdsn), count);
17457c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
17460e42dee6Sartem 		(void) fprintf(fout,
17477c478bd9Sstevel@tonic-gate 			"loc : %x len : %x\n",
17487c478bd9Sstevel@tonic-gate 			SWAP_32(ua->ua_al_dsc[i * 2]),
17497c478bd9Sstevel@tonic-gate 			SWAP_32(ua->ua_al_dsc[i * 2 + 1]));
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate void
print_lvid(FILE * fout,struct log_vol_int_desc * lvid)17540e42dee6Sartem print_lvid(FILE *fout, struct log_vol_int_desc *lvid)
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate 	int32_t i, count;
17577c478bd9Sstevel@tonic-gate 	caddr_t addr;
17587c478bd9Sstevel@tonic-gate 	struct lvid_iu *liu;
17597c478bd9Sstevel@tonic-gate 
17600e42dee6Sartem 	(void) fprintf(fout,
17617c478bd9Sstevel@tonic-gate 		"\n\t\t\tLogical Volume Integrity Descriptor\n");
17620e42dee6Sartem 	print_tag(fout, &lvid->lvid_tag);
17630e42dee6Sartem 	print_tstamp(fout, "Rec TM ", &lvid->lvid_tstamp);
17647c478bd9Sstevel@tonic-gate 	if (SWAP_32(lvid->lvid_int_type) == 0) {
17650e42dee6Sartem 		(void) fprintf(fout,
17667c478bd9Sstevel@tonic-gate 			"int_typ : Open\n");
17677c478bd9Sstevel@tonic-gate 	} else if (SWAP_32(lvid->lvid_int_type) == 1) {
17680e42dee6Sartem 		(void) fprintf(fout, "int_typ : Closed\n");
17697c478bd9Sstevel@tonic-gate 	} else {
17700e42dee6Sartem 		(void) fprintf(fout, "int_typ : Unknown\n");
17717c478bd9Sstevel@tonic-gate 	}
17720e42dee6Sartem 	print_ext_ad(fout, "Nie ", &lvid->lvid_nie);
17737c478bd9Sstevel@tonic-gate 	count = SWAP_32(lvid->lvid_npart);
17740e42dee6Sartem 	(void) fprintf(fout,
17757c478bd9Sstevel@tonic-gate 		"Uniq : %llx npart : %x liu : %x\n",
17767c478bd9Sstevel@tonic-gate 		SWAP_64(lvid->lvid_lvcu.lvhd_uniqid),
17777c478bd9Sstevel@tonic-gate 		count, SWAP_32(lvid->lvid_liu));
17787c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
17790e42dee6Sartem 		(void) fprintf(fout,
17807c478bd9Sstevel@tonic-gate 			"Part : %x Free : %x Size : %x\n",
17817c478bd9Sstevel@tonic-gate 			i, SWAP_32(lvid->lvid_fst[i]),
17827c478bd9Sstevel@tonic-gate 			SWAP_32(lvid->lvid_fst[count + i]));
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	addr = (caddr_t)lvid->lvid_fst;
17867c478bd9Sstevel@tonic-gate 	/* LINTED */
17877c478bd9Sstevel@tonic-gate 	liu = (struct lvid_iu *)(addr + 2 * count * 4);
17880e42dee6Sartem 	print_regid(fout, "Impl Id :", &liu->lvidiu_regid, REG_UDF_II);
17890e42dee6Sartem 	(void) fprintf(fout,
17907c478bd9Sstevel@tonic-gate 		"nfiles : %x ndirs : %x miread : %x"
17917c478bd9Sstevel@tonic-gate 		" miwrite : %x mawrite : %x\n",
17927c478bd9Sstevel@tonic-gate 		SWAP_32(liu->lvidiu_nfiles), SWAP_32(liu->lvidiu_ndirs),
17937c478bd9Sstevel@tonic-gate 		SWAP_16(liu->lvidiu_mread), SWAP_16(liu->lvidiu_mwrite),
17947c478bd9Sstevel@tonic-gate 		SWAP_16(liu->lvidiu_maxwr));
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate /* ------------ Routines to print basic structures Part 4 ---------------- */
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate void
print_fsd(FILE * fout,ud_handle_t h,struct file_set_desc * fsd)18010e42dee6Sartem print_fsd(FILE *fout, ud_handle_t h, struct file_set_desc *fsd)
18027c478bd9Sstevel@tonic-gate {
18030e42dee6Sartem 	(void) fprintf(fout,
18047c478bd9Sstevel@tonic-gate 		"\n\t\t\tFile Set Descriptor\n");
18057c478bd9Sstevel@tonic-gate 
18060e42dee6Sartem 	print_tag(fout, &fsd->fsd_tag);
18070e42dee6Sartem 	print_tstamp(fout, "Rec TM ", &fsd->fsd_time);
18080e42dee6Sartem 	(void) fprintf(fout,
18097c478bd9Sstevel@tonic-gate 		"ilvl : %x milvl : %x csl : %x"
18107c478bd9Sstevel@tonic-gate 		" mcsl : %x fsn : %x fsdn : %x\n",
18117c478bd9Sstevel@tonic-gate 		SWAP_16(fsd->fsd_ilevel), SWAP_16(fsd->fsd_mi_level),
18127c478bd9Sstevel@tonic-gate 		SWAP_32(fsd->fsd_cs_list), SWAP_32(fsd->fsd_mcs_list),
18137c478bd9Sstevel@tonic-gate 		SWAP_32(fsd->fsd_fs_no), SWAP_32(fsd->fsd_fsd_no));
18140e42dee6Sartem 	print_charspec(fout, "ID CS ", &fsd->fsd_lvidcs);
18150e42dee6Sartem 	print_dstring(fout, "lvi : ", fsd->fsd_lvidcs.cs_type,
18167c478bd9Sstevel@tonic-gate 			fsd->fsd_lvid, 128);
18170e42dee6Sartem 	print_charspec(fout, "ID CS ", &fsd->fsd_fscs);
18180e42dee6Sartem 	print_dstring(fout, "fsi : ", fsd->fsd_lvidcs.cs_type,
18197c478bd9Sstevel@tonic-gate 			fsd->fsd_fsi, 32);
18200e42dee6Sartem 	print_dstring(fout, "cfi : ", fsd->fsd_lvidcs.cs_type,
18217c478bd9Sstevel@tonic-gate 			fsd->fsd_cfi, 32);
18220e42dee6Sartem 	print_dstring(fout, "afi : ", fsd->fsd_lvidcs.cs_type,
18237c478bd9Sstevel@tonic-gate 			fsd->fsd_afi, 32);
18240e42dee6Sartem 	print_long_ad(fout, "Ricb ", &fsd->fsd_root_icb);
18250e42dee6Sartem 	print_regid(fout, "DI ", &fsd->fsd_did, REG_DOM_ID);
18260e42dee6Sartem 	print_long_ad(fout, "Next Fsd ", &fsd->fsd_next);
18270e42dee6Sartem 	if (h->udfs.ecma_version == UD_ECMA_VER3) {
18280e42dee6Sartem 		print_long_ad(fout, "System Stream Directory ICB ",
18290e42dee6Sartem 				&fsd->fsd_next);
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate }
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate void
print_phdr(FILE * fout,struct phdr_desc * ph)18340e42dee6Sartem print_phdr(FILE *fout, struct phdr_desc *ph)
18357c478bd9Sstevel@tonic-gate {
18360e42dee6Sartem 	print_short_ad(fout, "ust ", &ph->phdr_ust);
18370e42dee6Sartem 	print_short_ad(fout, "usb ", &ph->phdr_usb);
18380e42dee6Sartem 	print_short_ad(fout, "int ", &ph->phdr_it);
18390e42dee6Sartem 	print_short_ad(fout, "fst ", &ph->phdr_fst);
18400e42dee6Sartem 	print_short_ad(fout, "fsh ", &ph->phdr_fsb);
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate void
print_fid(FILE * fout,struct file_id * fid)18440e42dee6Sartem print_fid(FILE *fout, struct file_id *fid)
18457c478bd9Sstevel@tonic-gate {
18467c478bd9Sstevel@tonic-gate 	int32_t i;
18477c478bd9Sstevel@tonic-gate 	uint8_t *addr;
18487c478bd9Sstevel@tonic-gate 
18490e42dee6Sartem 	(void) fprintf(fout,
18507c478bd9Sstevel@tonic-gate 		"File Identifier Descriptor\n");
18510e42dee6Sartem 	print_tag(fout, &fid->fid_tag);
18520e42dee6Sartem 	(void) fprintf(fout, "fvn : %x fc : %x length : %x ",
18537c478bd9Sstevel@tonic-gate 		fid->fid_ver, fid->fid_flags, fid->fid_idlen);
18540e42dee6Sartem 	print_long_ad(fout, "ICB", &fid->fid_icb);
18557c478bd9Sstevel@tonic-gate 	addr = &fid->fid_spec[SWAP_16(fid->fid_iulen)];
18560e42dee6Sartem 	(void) fprintf(fout, "iulen : %x comp : %x name : ",
18577c478bd9Sstevel@tonic-gate 		SWAP_16(fid->fid_iulen), *addr);
18587c478bd9Sstevel@tonic-gate 	addr++;
18597c478bd9Sstevel@tonic-gate 	for (i = 0; i < fid->fid_idlen; i++) {
18600e42dee6Sartem 		(void) fprintf(fout, "%c", *addr++);
18617c478bd9Sstevel@tonic-gate 	}
18620e42dee6Sartem 	(void) fprintf(fout, "\n");
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate void
print_aed(FILE * fout,struct alloc_ext_desc * aed)18660e42dee6Sartem print_aed(FILE *fout, struct alloc_ext_desc *aed)
18677c478bd9Sstevel@tonic-gate {
18680e42dee6Sartem 	(void) fprintf(fout,
18697c478bd9Sstevel@tonic-gate 		"Allocation Extent Descriptor\n");
18700e42dee6Sartem 	print_tag(fout, &aed->aed_tag);
18710e42dee6Sartem 	(void) fprintf(fout, "prev ael loc : %x laed : %x\n",
18727c478bd9Sstevel@tonic-gate 		SWAP_32(aed->aed_rev_ael), SWAP_32(aed->aed_len_aed));
18737c478bd9Sstevel@tonic-gate }
18747c478bd9Sstevel@tonic-gate 
18750e42dee6Sartem static char *ftype[] = {
18767c478bd9Sstevel@tonic-gate 	"NON",  "USE",  "PIE",  "IE",
18777c478bd9Sstevel@tonic-gate 	"DIR",  "REG",  "BDEV", "CDEV",
18787c478bd9Sstevel@tonic-gate 	"EATT", "FIFO", "SOCK", "TERM",
18797c478bd9Sstevel@tonic-gate 	"SYML", "SDIR"
18807c478bd9Sstevel@tonic-gate };
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate void
print_icb_tag(FILE * fout,struct icb_tag * itag)18830e42dee6Sartem print_icb_tag(FILE *fout, struct icb_tag *itag)
18847c478bd9Sstevel@tonic-gate {
18850e42dee6Sartem 	(void) fprintf(fout,
18867c478bd9Sstevel@tonic-gate 		"prnde : %x strat : %x param : %x max_ent %x\n",
18877c478bd9Sstevel@tonic-gate 		SWAP_32(itag->itag_prnde), SWAP_16(itag->itag_strategy),
18887c478bd9Sstevel@tonic-gate 		SWAP_16(itag->itag_param), SWAP_16(itag->itag_max_ent));
18890e42dee6Sartem 	(void) fprintf(fout,
18907c478bd9Sstevel@tonic-gate 		"ftype : %s prn : %x loc : %x flags : %x\n",
18917c478bd9Sstevel@tonic-gate 		(itag->itag_ftype >= 14) ? ftype[0] : ftype[itag->itag_ftype],
18927c478bd9Sstevel@tonic-gate 		SWAP_16(itag->itag_lb_prn),
18937c478bd9Sstevel@tonic-gate 		SWAP_32(itag->itag_lb_loc), SWAP_16(itag->itag_flags));
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate void
print_ie(FILE * fout,struct indirect_entry * ie)18980e42dee6Sartem print_ie(FILE *fout, struct indirect_entry *ie)
18997c478bd9Sstevel@tonic-gate {
19000e42dee6Sartem 	(void) fprintf(fout,
19017c478bd9Sstevel@tonic-gate 		"Indirect Entry\n");
19020e42dee6Sartem 	print_tag(fout, &ie->ie_tag);
19030e42dee6Sartem 	print_icb_tag(fout, &ie->ie_icb_tag);
19040e42dee6Sartem 	print_long_ad(fout, "ICB", &ie->ie_indirecticb);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate void
print_td(FILE * fout,struct term_desc * td)19080e42dee6Sartem print_td(FILE *fout, struct term_desc *td)
19097c478bd9Sstevel@tonic-gate {
19100e42dee6Sartem 	(void) fprintf(fout,
19117c478bd9Sstevel@tonic-gate 		"Terminating Descriptor\n");
19120e42dee6Sartem 	print_tag(fout, &td->td_tag);
19137c478bd9Sstevel@tonic-gate }
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate void
print_fe(FILE * fout,struct file_entry * fe)19160e42dee6Sartem print_fe(FILE *fout, struct file_entry *fe)
19177c478bd9Sstevel@tonic-gate {
19180e42dee6Sartem 	(void) fprintf(fout,
19197c478bd9Sstevel@tonic-gate 		"File Entry\n");
19200e42dee6Sartem 	print_tag(fout, &fe->fe_tag);
19210e42dee6Sartem 	print_icb_tag(fout, &fe->fe_icb_tag);
19220e42dee6Sartem 	(void) fprintf(fout,
19237c478bd9Sstevel@tonic-gate 		"uid : %x gid : %x perms : %x nlnk : %x\n",
19247c478bd9Sstevel@tonic-gate 		SWAP_32(fe->fe_uid), SWAP_32(fe->fe_gid),
19257c478bd9Sstevel@tonic-gate 		SWAP_32(fe->fe_perms), SWAP_16(fe->fe_lcount));
19260e42dee6Sartem 	(void) fprintf(fout,
19277c478bd9Sstevel@tonic-gate 		"rec_for : %x rec_dis : %x rec_len : %x "
19287c478bd9Sstevel@tonic-gate 		"sz : %llx blks : %llx\n",
19297c478bd9Sstevel@tonic-gate 		fe->fe_rec_for, fe->fe_rec_dis, SWAP_32(fe->fe_rec_len),
19307c478bd9Sstevel@tonic-gate 		SWAP_64(fe->fe_info_len), SWAP_64(fe->fe_lbr));
19310e42dee6Sartem 	print_tstamp(fout, "ctime ", &fe->fe_acc_time);
19320e42dee6Sartem 	print_tstamp(fout, "mtime ", &fe->fe_mod_time);
19330e42dee6Sartem 	print_tstamp(fout, "atime ", &fe->fe_attr_time);
19340e42dee6Sartem 	(void) fprintf(fout,
19357c478bd9Sstevel@tonic-gate 		"ckpoint : %x ", SWAP_32(fe->fe_ckpoint));
19360e42dee6Sartem 	print_long_ad(fout, "ICB", &fe->fe_ea_icb);
19370e42dee6Sartem 	print_regid(fout, "impl", &fe->fe_impl_id, REG_UDF_II);
19380e42dee6Sartem 	(void) fprintf(fout,
19397c478bd9Sstevel@tonic-gate 		"uniq_id : %llx len_ear : %x len_adesc %x\n",
19407c478bd9Sstevel@tonic-gate 		SWAP_64(fe->fe_uniq_id), SWAP_32(fe->fe_len_ear),
19417c478bd9Sstevel@tonic-gate 		SWAP_32(fe->fe_len_adesc));
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate void
print_pmaps(FILE * fout,uint8_t * addr,int32_t count)19450e42dee6Sartem print_pmaps(FILE *fout, uint8_t *addr, int32_t count)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate 	struct pmap_hdr *hdr;
19487c478bd9Sstevel@tonic-gate 	struct pmap_typ1 *map1;
19497c478bd9Sstevel@tonic-gate 	struct pmap_typ2 *map2;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	while (count--) {
19527c478bd9Sstevel@tonic-gate 		hdr = (struct pmap_hdr *)addr;
19537c478bd9Sstevel@tonic-gate 		switch (hdr->maph_type) {
19547c478bd9Sstevel@tonic-gate 		case 1 :
19557c478bd9Sstevel@tonic-gate 			/* LINTED */
19567c478bd9Sstevel@tonic-gate 			map1 = (struct pmap_typ1 *)hdr;
19570e42dee6Sartem 			(void) fprintf(fout, "Map type 1 ");
19580e42dee6Sartem 			(void) fprintf(fout, "VSN %x prn %x\n",
19597c478bd9Sstevel@tonic-gate 					SWAP_16(map1->map1_vsn),
19607c478bd9Sstevel@tonic-gate 					SWAP_16(map1->map1_pn));
19617c478bd9Sstevel@tonic-gate 			break;
19627c478bd9Sstevel@tonic-gate 		case 2 :
19637c478bd9Sstevel@tonic-gate 			/* LINTED */
19647c478bd9Sstevel@tonic-gate 			map2 = (struct pmap_typ2 *)hdr;
19650e42dee6Sartem 			(void) fprintf(fout, "Map type 2 ");
19660e42dee6Sartem 			(void) fprintf(fout, "VSN %x prn %x\n",
19677c478bd9Sstevel@tonic-gate 					SWAP_16(map2->map2_vsn),
19687c478bd9Sstevel@tonic-gate 					SWAP_16(map2->map2_pn));
19690e42dee6Sartem 			print_regid(fout, "Partition Type Identifier",
19707c478bd9Sstevel@tonic-gate 					&map2->map2_pti, REG_UDF_ID);
19717c478bd9Sstevel@tonic-gate 			break;
19727c478bd9Sstevel@tonic-gate 		default :
19730e42dee6Sartem 			(void) fprintf(fout, "unknown map type\n");
19747c478bd9Sstevel@tonic-gate 		}
19757c478bd9Sstevel@tonic-gate 		addr += hdr->maph_length;
19767c478bd9Sstevel@tonic-gate 	}
19777c478bd9Sstevel@tonic-gate }
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate void
print_short_ad(FILE * fout,char * name,struct short_ad * sad)19820e42dee6Sartem print_short_ad(FILE *fout, char *name, struct short_ad *sad)
19837c478bd9Sstevel@tonic-gate {
19840e42dee6Sartem 	(void) fprintf(fout,
19857c478bd9Sstevel@tonic-gate 		"%s loc : %x len : %x\n", name,
19867c478bd9Sstevel@tonic-gate 		SWAP_32(sad->sad_ext_loc), SWAP_32(sad->sad_ext_len));
19877c478bd9Sstevel@tonic-gate }
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate void
print_long_ad(FILE * fout,char * name,struct long_ad * lad)19900e42dee6Sartem print_long_ad(FILE *fout, char *name, struct long_ad *lad)
19917c478bd9Sstevel@tonic-gate {
19920e42dee6Sartem 	(void) fprintf(fout,
19937c478bd9Sstevel@tonic-gate 		"%s prn : %x loc : %x len : %x\n", name,
19947c478bd9Sstevel@tonic-gate 		SWAP_16(lad->lad_ext_prn), SWAP_32(lad->lad_ext_loc),
19957c478bd9Sstevel@tonic-gate 		SWAP_32(lad->lad_ext_len));
19967c478bd9Sstevel@tonic-gate }
1997