xref: /illumos-gate/usr/src/cmd/fs.d/udfs/fstyp/ud_lib.c (revision 8509e9ca)
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 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * Get the stat structure
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	if (stat(special, &i_stat) < 0) {
957c478bd9Sstevel@tonic-gate 		temp = special;
967c478bd9Sstevel@tonic-gate 	} else {
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		if ((i_stat.st_mode & S_IFMT) == S_IFCHR) {
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 			/*
1017c478bd9Sstevel@tonic-gate 			 * If Raw deivce is given use it as it is
1027c478bd9Sstevel@tonic-gate 			 */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 			temp = special;
1057c478bd9Sstevel@tonic-gate 		} else if ((i_stat.st_mode & S_IFMT) == S_IFBLK) {
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 			/*
1087c478bd9Sstevel@tonic-gate 			 * Block device try to convert to raw device
1097c478bd9Sstevel@tonic-gate 			 */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 			temp = getfullrawname(special);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 			/*
1147c478bd9Sstevel@tonic-gate 			 * Stat the converted device name and verify
1157c478bd9Sstevel@tonic-gate 			 * both the raw and block device belong to
1167c478bd9Sstevel@tonic-gate 			 * the same device
1177c478bd9Sstevel@tonic-gate 			 */
1187c478bd9Sstevel@tonic-gate 			if (stat(temp, &r_stat) < 0) {
1197c478bd9Sstevel@tonic-gate 				temp = special;
1207c478bd9Sstevel@tonic-gate 			} else {
1217c478bd9Sstevel@tonic-gate 				if (((r_stat.st_mode & S_IFMT) == S_IFBLK) ||
1227c478bd9Sstevel@tonic-gate 					(r_stat.st_rdev != i_stat.st_rdev)) {
1237c478bd9Sstevel@tonic-gate 					temp = special;
1247c478bd9Sstevel@tonic-gate 				}
1257c478bd9Sstevel@tonic-gate 			}
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Now finally open the device
1317c478bd9Sstevel@tonic-gate 	 */
1320e42dee6Sartem 	h->fd = open(temp, flags);
1337c478bd9Sstevel@tonic-gate 
1340e42dee6Sartem 	return (h->fd);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1370e42dee6Sartem /* ARGSUSED */
1387c478bd9Sstevel@tonic-gate void
ud_close_dev(ud_handle_t h)1390e42dee6Sartem ud_close_dev(ud_handle_t h)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * Too simple Just close it
1437c478bd9Sstevel@tonic-gate 	 */
1440e42dee6Sartem 	(void) close(h->fd);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate int32_t
ud_read_dev(ud_handle_t h,uint64_t offset,uint8_t * buf,uint32_t count)1480e42dee6Sartem ud_read_dev(ud_handle_t h, uint64_t offset, uint8_t *buf, uint32_t count)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * Seek to the given offset
1527c478bd9Sstevel@tonic-gate 	 */
1530e42dee6Sartem 	if (lseek(h->fd, offset, SEEK_SET) == -1) {
1547c478bd9Sstevel@tonic-gate 		return (1);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * Read the required number of bytes
1597c478bd9Sstevel@tonic-gate 	 */
1600e42dee6Sartem 	if (read(h->fd, buf, count) != count) {
1617c478bd9Sstevel@tonic-gate 		return (1);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	return (0);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate int32_t
ud_write_dev(ud_handle_t h,uint64_t offset,uint8_t * buf,uint32_t count)1670e42dee6Sartem ud_write_dev(ud_handle_t h, uint64_t offset, uint8_t *buf, uint32_t count)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Seek to the given offset
1717c478bd9Sstevel@tonic-gate 	 */
1720e42dee6Sartem 	if (lseek(h->fd, offset, SEEK_SET) == -1) {
1737c478bd9Sstevel@tonic-gate 		return (1);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Read the appropriate number of bytes
1787c478bd9Sstevel@tonic-gate 	 */
1790e42dee6Sartem 	if (write(h->fd, buf, count) != count) {
1807c478bd9Sstevel@tonic-gate 		return (1);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	return (0);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* ----- BEGIN Read and translate the on disk VDS to IN CORE format -------- */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate int32_t
ud_fill_udfs_info(ud_handle_t h)1880e42dee6Sartem ud_fill_udfs_info(ud_handle_t h)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	struct	anch_vol_desc_ptr	*avdp = NULL;
1917c478bd9Sstevel@tonic-gate 	uint32_t			offset = 0;
1927c478bd9Sstevel@tonic-gate 
1930e42dee6Sartem 	if (ioctl(h->fd, CDROMREADOFFSET, &offset) == -1) {
1947c478bd9Sstevel@tonic-gate 		offset = 0;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1970e42dee6Sartem 	h->udfs.flags = INVALID_UDFS;
1987c478bd9Sstevel@tonic-gate 
1990e42dee6Sartem 	h->udfs.ecma_version = ud_get_ecma_ver(h, offset);
2000e42dee6Sartem 	if (h->udfs.ecma_version == UD_ECMA_UNKN) {
2017c478bd9Sstevel@tonic-gate 		return (1);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2040e42dee6Sartem 	h->udfs.lbsize = ud_get_fs_bsize(h, offset, &h->udfs.avdp_loc);
2050e42dee6Sartem 	if (h->udfs.lbsize == 0) {
2067c478bd9Sstevel@tonic-gate 		return (2);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2090e42dee6Sartem 	h->udfs.avdp_len = lb_roundup(512, h->udfs.lbsize);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if ((avdp = (struct anch_vol_desc_ptr *)
2130e42dee6Sartem 			malloc(h->udfs.lbsize)) == NULL) {
2147c478bd9Sstevel@tonic-gate 		return (3);
2157c478bd9Sstevel@tonic-gate 	}
2160e42dee6Sartem 	if (ud_read_dev(h, h->udfs.avdp_loc * h->udfs.lbsize,
2170e42dee6Sartem 			(uint8_t *)avdp, h->udfs.lbsize) != 0) {
2187c478bd9Sstevel@tonic-gate 		free(avdp);
2197c478bd9Sstevel@tonic-gate 		return (4);
2207c478bd9Sstevel@tonic-gate 	}
2210e42dee6Sartem 	if (ud_verify_tag(h, &avdp->avd_tag, UD_ANCH_VOL_DESC,
2220e42dee6Sartem 			h->udfs.avdp_loc, 1, 0) != 0) {
2237c478bd9Sstevel@tonic-gate 		free(avdp);
2247c478bd9Sstevel@tonic-gate 		return (5);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2270e42dee6Sartem 	h->udfs.mvds_loc = SWAP_32(avdp->avd_main_vdse.ext_loc);
2280e42dee6Sartem 	h->udfs.mvds_len = SWAP_32(avdp->avd_main_vdse.ext_len);
2297c478bd9Sstevel@tonic-gate 
2300e42dee6Sartem 	h->udfs.rvds_loc = SWAP_32(avdp->avd_res_vdse.ext_loc);
2310e42dee6Sartem 	h->udfs.rvds_len = SWAP_32(avdp->avd_res_vdse.ext_len);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	free(avdp);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * get information from mvds and rvds
2377c478bd9Sstevel@tonic-gate 	 */
2380e42dee6Sartem 	if (ud_parse_fill_vds(h, &h->udfs.mvds,
2390e42dee6Sartem 			h->udfs.mvds_loc, h->udfs.mvds_len) == 0) {
2400e42dee6Sartem 		h->udfs.flags |= VALID_MVDS;
2417c478bd9Sstevel@tonic-gate 	}
2420e42dee6Sartem 	if (ud_parse_fill_vds(h, &h->udfs.rvds,
2430e42dee6Sartem 			h->udfs.rvds_loc, h->udfs.rvds_len) == 0) {
2440e42dee6Sartem 		h->udfs.flags |= VALID_RVDS;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2470e42dee6Sartem 	if ((h->udfs.flags & (VALID_MVDS | VALID_RVDS)) == 0) {
2487c478bd9Sstevel@tonic-gate 		return (6);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * If we are here we have
2537c478bd9Sstevel@tonic-gate 	 * a valid Volume Descriptor Seqence
2547c478bd9Sstevel@tonic-gate 	 * Read and understand lvd
2557c478bd9Sstevel@tonic-gate 	 */
2560e42dee6Sartem 	if (h->udfs.flags & VALID_MVDS) {
2570e42dee6Sartem 		if (ud_read_and_translate_lvd(h, h->udfs.mvds.lvd_loc,
2580e42dee6Sartem 				h->udfs.mvds.lvd_len) != 0) {
2597c478bd9Sstevel@tonic-gate 			return (7);
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	} else {
2620e42dee6Sartem 		if (ud_read_and_translate_lvd(h, h->udfs.rvds.lvd_loc,
2630e42dee6Sartem 				h->udfs.rvds.lvd_len) != 0) {
2647c478bd9Sstevel@tonic-gate 			return (8);
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2680e42dee6Sartem 	h->udfs.flags |= VALID_UDFS;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	return (0);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2730e42dee6Sartem static int32_t
ud_get_ecma_ver(ud_handle_t h,uint32_t offset)2740e42dee6Sartem ud_get_ecma_ver(ud_handle_t h, uint32_t offset)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	uint8_t *buf;
2777c478bd9Sstevel@tonic-gate 	uint64_t off;
2787c478bd9Sstevel@tonic-gate 	uint64_t end_off;
2797c478bd9Sstevel@tonic-gate 	struct nsr_desc *ndsc;
2807c478bd9Sstevel@tonic-gate 	uint32_t ecma_ver = UD_ECMA_UNKN;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * Allocate a buffer of size UD_VOL_REC_BSZ
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	if ((buf = (uint8_t *)malloc(UD_VOL_REC_BSZ)) == NULL) {
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		/*
2887c478bd9Sstevel@tonic-gate 		 * Uh could not even allocate this much
2897c478bd9Sstevel@tonic-gate 		 */
2907c478bd9Sstevel@tonic-gate 		goto end;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * Start from 32k and keep reading 2k blocks we
2957c478bd9Sstevel@tonic-gate 	 * should be able to find NSR if we have one by 256 * 2k bytes
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 	off = offset * 2048 + UD_VOL_REC_START;
2987c478bd9Sstevel@tonic-gate 	end_off = offset * 2048 + UD_VOL_REC_END;
2997c478bd9Sstevel@tonic-gate 	for (; off < end_off; off += UD_VOL_REC_BSZ) {
3007c478bd9Sstevel@tonic-gate 
3010e42dee6Sartem 		if (ud_read_dev(h, off, buf, UD_VOL_REC_BSZ) == 0) {
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			ndsc = (struct nsr_desc *)buf;
3047c478bd9Sstevel@tonic-gate 			/*
3057c478bd9Sstevel@tonic-gate 			 * Is this either NSR02 or NSR03
3067c478bd9Sstevel@tonic-gate 			 */
3077c478bd9Sstevel@tonic-gate 			if ((ndsc->nsr_str_type == 0) &&
3087c478bd9Sstevel@tonic-gate 				(ndsc->nsr_ver == 1) &&
3097c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[0] == 'N') &&
3107c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[1] == 'S') &&
3117c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[2] == 'R') &&
3127c478bd9Sstevel@tonic-gate 				(ndsc->nsr_id[3] == '0') &&
3137c478bd9Sstevel@tonic-gate 					((ndsc->nsr_id[4] == '2') ||
3147c478bd9Sstevel@tonic-gate 					(ndsc->nsr_id[4] == '3'))) {
3157c478bd9Sstevel@tonic-gate 
3160e42dee6Sartem 				(void) strncpy((char *)h->udfs.ecma_id,
3177c478bd9Sstevel@tonic-gate 					(char *)ndsc->nsr_id, 5);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 				switch (ndsc->nsr_id[4]) {
3207c478bd9Sstevel@tonic-gate 				case '2' :
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 					/*
3237c478bd9Sstevel@tonic-gate 					 * ECMA 167/2
3247c478bd9Sstevel@tonic-gate 					 */
3257c478bd9Sstevel@tonic-gate 					ecma_ver = UD_ECMA_VER2;
3267c478bd9Sstevel@tonic-gate 					goto end;
3277c478bd9Sstevel@tonic-gate 				case '3' :
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 					/*
3307c478bd9Sstevel@tonic-gate 					 * ECMA 167/3
3317c478bd9Sstevel@tonic-gate 					 */
3327c478bd9Sstevel@tonic-gate 					ecma_ver = UD_ECMA_VER3;
3337c478bd9Sstevel@tonic-gate 					goto end;
3347c478bd9Sstevel@tonic-gate 				}
3357c478bd9Sstevel@tonic-gate 			}
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate end:
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * Cleanup
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	free(buf);
3447c478bd9Sstevel@tonic-gate 	return (ecma_ver);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3470e42dee6Sartem static uint32_t last_block_index[] = {0, 0, 256, 2, 2 + 256,
3487c478bd9Sstevel@tonic-gate 		150, 150 + 256, 152, 152 + 256};
3497c478bd9Sstevel@tonic-gate 
3500e42dee6Sartem static int32_t
ud_get_fs_bsize(ud_handle_t h,uint32_t offset,uint32_t * avd_loc)3510e42dee6Sartem ud_get_fs_bsize(ud_handle_t h, uint32_t offset, uint32_t *avd_loc)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	uint64_t off;
3547c478bd9Sstevel@tonic-gate 	int32_t index, bsize, shift, end_index;
3557c478bd9Sstevel@tonic-gate 	uint32_t num_blocks, sub_blk;
3567c478bd9Sstevel@tonic-gate 	uint8_t *buf = NULL;
3577c478bd9Sstevel@tonic-gate 	struct anch_vol_desc_ptr *avdp;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if ((buf = (uint8_t *)malloc(MAXBSIZE)) == NULL) {
3607c478bd9Sstevel@tonic-gate 		return (0);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
3647c478bd9Sstevel@tonic-gate 	 * If we could figure out the last block
3657c478bd9Sstevel@tonic-gate 	 * search at 256, N, N - 256 blocks
3667c478bd9Sstevel@tonic-gate 	 * otherwise just check at 256
3677c478bd9Sstevel@tonic-gate 	 */
3680e42dee6Sartem 	if (ud_get_num_blks(h, &num_blocks) != 0) {
3697c478bd9Sstevel@tonic-gate 		end_index = 1;
3707c478bd9Sstevel@tonic-gate 		num_blocks = 0;
3717c478bd9Sstevel@tonic-gate 	} else {
3727c478bd9Sstevel@tonic-gate 		end_index = sizeof (last_block_index) / 4;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	for (index = 0; index < end_index; index++) {
3767c478bd9Sstevel@tonic-gate 		sub_blk = last_block_index[index];
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * Start guessing from DEV_BSIZE to MAXBSIZE
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		for (bsize = DEV_BSIZE, shift = 0;
3827c478bd9Sstevel@tonic-gate 			bsize <= MAXBSIZE; bsize <<= 1, shift++) {
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 			if (index == 0) {
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 				/*
3877c478bd9Sstevel@tonic-gate 				 * Check if we atleast have 256 of bsize
3887c478bd9Sstevel@tonic-gate 				 * blocks on the device
3897c478bd9Sstevel@tonic-gate 				 */
3907c478bd9Sstevel@tonic-gate 				if ((end_index == 0) ||
3917c478bd9Sstevel@tonic-gate 					(num_blocks > (256 << shift))) {
3927c478bd9Sstevel@tonic-gate 					*avd_loc = 256;
3937c478bd9Sstevel@tonic-gate 					if (bsize <= 2048) {
3947c478bd9Sstevel@tonic-gate 						*avd_loc +=
3957c478bd9Sstevel@tonic-gate 							offset * 2048 / bsize;
3967c478bd9Sstevel@tonic-gate 					} else {
3977c478bd9Sstevel@tonic-gate 						*avd_loc +=
3987c478bd9Sstevel@tonic-gate 							offset / (bsize / 2048);
3997c478bd9Sstevel@tonic-gate 					}
4007c478bd9Sstevel@tonic-gate 				} else {
4017c478bd9Sstevel@tonic-gate 					continue;
4027c478bd9Sstevel@tonic-gate 				}
4037c478bd9Sstevel@tonic-gate 			} else {
4047c478bd9Sstevel@tonic-gate 				/*
4050e42dee6Sartem 				 * Calculate the bsize avd block
4067c478bd9Sstevel@tonic-gate 				 */
4077c478bd9Sstevel@tonic-gate 				if ((num_blocks) &&
4087c478bd9Sstevel@tonic-gate 					(num_blocks > (sub_blk << shift))) {
4097c478bd9Sstevel@tonic-gate 					*avd_loc = (num_blocks >> shift) -
4107c478bd9Sstevel@tonic-gate 						sub_blk;
4117c478bd9Sstevel@tonic-gate 				} else {
4127c478bd9Sstevel@tonic-gate 					continue;
4137c478bd9Sstevel@tonic-gate 				}
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 			off = (uint64_t)*avd_loc * bsize;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 			/*
4197c478bd9Sstevel@tonic-gate 			 * Read bsize bytes at off
4207c478bd9Sstevel@tonic-gate 			 */
4210e42dee6Sartem 			if (ud_read_dev(h, off, buf, bsize) != 0) {
4227c478bd9Sstevel@tonic-gate 				continue;
4237c478bd9Sstevel@tonic-gate 			}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 			/*
4267c478bd9Sstevel@tonic-gate 			 * Check if we have a Anchor Volume Descriptor here
4277c478bd9Sstevel@tonic-gate 			 */
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 			/* LINTED */
4307c478bd9Sstevel@tonic-gate 			avdp = (struct anch_vol_desc_ptr *)buf;
4310e42dee6Sartem 			if (ud_verify_tag(h, &avdp->avd_tag,
4327c478bd9Sstevel@tonic-gate 				UD_ANCH_VOL_DESC, *avd_loc, 1, 0) != 0) {
4337c478bd9Sstevel@tonic-gate 				continue;
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 			goto end;
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate end:
4407c478bd9Sstevel@tonic-gate 	if (bsize > MAXBSIZE) {
4417c478bd9Sstevel@tonic-gate 		bsize = 0;
4427c478bd9Sstevel@tonic-gate 		*avd_loc = 0;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	free(buf);
4457c478bd9Sstevel@tonic-gate 	return (bsize);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4480e42dee6Sartem static int32_t
ud_parse_fill_vds(ud_handle_t h,struct vds * v,uint32_t vds_loc,uint32_t vds_len)4490e42dee6Sartem ud_parse_fill_vds(ud_handle_t h, struct vds *v,
4507c478bd9Sstevel@tonic-gate 	uint32_t vds_loc, uint32_t vds_len)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	uint8_t *addr, *taddr, *eaddr;
4537c478bd9Sstevel@tonic-gate 	uint16_t id;
4547c478bd9Sstevel@tonic-gate 	int32_t i;
4557c478bd9Sstevel@tonic-gate 	uint64_t off;
4567c478bd9Sstevel@tonic-gate 	struct tag *tag;
4577c478bd9Sstevel@tonic-gate 	struct pri_vol_desc *pvd;
4587c478bd9Sstevel@tonic-gate 	struct log_vol_desc *lvd;
4597c478bd9Sstevel@tonic-gate 	struct vol_desc_ptr *vds;
4607c478bd9Sstevel@tonic-gate 	struct unall_spc_desc *usd;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate begin:
4637c478bd9Sstevel@tonic-gate 	if ((addr = (uint8_t *)malloc(vds_len)) == NULL) {
4647c478bd9Sstevel@tonic-gate 		return (1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4670e42dee6Sartem 	off = vds_loc * h->udfs.lbsize;
4680e42dee6Sartem 	if (ud_read_dev(h, off, addr, vds_len) != 0) {
4697c478bd9Sstevel@tonic-gate 		goto end;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4720e42dee6Sartem 	for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
4730e42dee6Sartem 			taddr += h->udfs.lbsize, vds_loc ++) {
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		/* LINTED */
4767c478bd9Sstevel@tonic-gate 		tag = (struct tag *)taddr;
4777c478bd9Sstevel@tonic-gate 		id = SWAP_16(tag->tag_id);
4787c478bd9Sstevel@tonic-gate 		/*
4797c478bd9Sstevel@tonic-gate 		 * If you cannot verify the tag just skip it
4807c478bd9Sstevel@tonic-gate 		 * This is not a fatal error
4817c478bd9Sstevel@tonic-gate 		 */
4820e42dee6Sartem 		if (ud_verify_tag(h, tag, id, vds_loc, 1, 0) != 0) {
4837c478bd9Sstevel@tonic-gate 			continue;
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 		switch (id) {
4867c478bd9Sstevel@tonic-gate 		case UD_PRI_VOL_DESC :
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 			/*
4897c478bd9Sstevel@tonic-gate 			 * Primary Volume Descriptor
4907c478bd9Sstevel@tonic-gate 			 */
4917c478bd9Sstevel@tonic-gate 			/* LINTED */
4927c478bd9Sstevel@tonic-gate 			pvd = (struct pri_vol_desc *)taddr;
4937c478bd9Sstevel@tonic-gate 			if ((v->pvd_len == 0) ||
4947c478bd9Sstevel@tonic-gate 				(SWAP_32(pvd->pvd_vdsn) > v->pvd_vdsn)) {
4957c478bd9Sstevel@tonic-gate 				v->pvd_vdsn = SWAP_32(pvd->pvd_vdsn);
4967c478bd9Sstevel@tonic-gate 				v->pvd_loc = vds_loc;
4970e42dee6Sartem 				v->pvd_len = h->udfs.lbsize;
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 			break;
5007c478bd9Sstevel@tonic-gate 		case UD_VOL_DESC_PTR :
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 			/*
5037c478bd9Sstevel@tonic-gate 			 * Curent sequence is continued from
5047c478bd9Sstevel@tonic-gate 			 * the location pointed by vdp
5057c478bd9Sstevel@tonic-gate 			 */
5067c478bd9Sstevel@tonic-gate 			/* LINTED */
5077c478bd9Sstevel@tonic-gate 			vds = (struct vol_desc_ptr *)taddr;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 			if (SWAP_32(vds->vdp_nvdse.ext_len) != 0) {
5107c478bd9Sstevel@tonic-gate 				vds_loc = SWAP_32(vds->vdp_nvdse.ext_loc);
5117c478bd9Sstevel@tonic-gate 				vds_len = SWAP_32(vds->vdp_nvdse.ext_len);
5127c478bd9Sstevel@tonic-gate 				free(addr);
5137c478bd9Sstevel@tonic-gate 				goto begin;
5147c478bd9Sstevel@tonic-gate 			}
5157c478bd9Sstevel@tonic-gate 			break;
5167c478bd9Sstevel@tonic-gate 		case UD_IMPL_USE_DESC :
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 			/*
5197c478bd9Sstevel@tonic-gate 			 * Implementation Use Volume Descriptor
5207c478bd9Sstevel@tonic-gate 			 */
5217c478bd9Sstevel@tonic-gate 			v->iud_loc = vds_loc;
5220e42dee6Sartem 			v->iud_len = lb_roundup(512, h->udfs.lbsize);
5237c478bd9Sstevel@tonic-gate 			break;
5247c478bd9Sstevel@tonic-gate 		case UD_PART_DESC :
5257c478bd9Sstevel@tonic-gate 			{
5267c478bd9Sstevel@tonic-gate 				struct ud_part *p;
5270e42dee6Sartem 				struct phdr_desc *ph;
5287c478bd9Sstevel@tonic-gate 				struct part_desc *pd;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 				/*
5317c478bd9Sstevel@tonic-gate 				 * Partition Descriptor
5327c478bd9Sstevel@tonic-gate 				 */
5337c478bd9Sstevel@tonic-gate 				/* LINTED */
5347c478bd9Sstevel@tonic-gate 				pd = (struct part_desc *)taddr;
5357c478bd9Sstevel@tonic-gate 
5360e42dee6Sartem 				for (i = 0; i < h->n_parts; i++) {
5370e42dee6Sartem 					p = &h->part[i];
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 					if ((SWAP_16(pd->pd_pnum) ==
5407c478bd9Sstevel@tonic-gate 							p->udp_number) &&
5417c478bd9Sstevel@tonic-gate 						(SWAP_32(pd->pd_vdsn) >
5427c478bd9Sstevel@tonic-gate 							p->udp_seqno)) {
5437c478bd9Sstevel@tonic-gate 						break;
5447c478bd9Sstevel@tonic-gate 					}
5457c478bd9Sstevel@tonic-gate 				}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 				v->part_loc[i] = vds_loc;
5487c478bd9Sstevel@tonic-gate 				v->part_len[i] =
5490e42dee6Sartem 					lb_roundup(512, h->udfs.lbsize);
5507c478bd9Sstevel@tonic-gate 
5510e42dee6Sartem 				p = &h->part[i];
5527c478bd9Sstevel@tonic-gate 				p->udp_number = SWAP_16(pd->pd_pnum);
5537c478bd9Sstevel@tonic-gate 				p->udp_seqno = SWAP_32(pd->pd_vdsn);
5547c478bd9Sstevel@tonic-gate 				p->udp_access = SWAP_32(pd->pd_acc_type);
5557c478bd9Sstevel@tonic-gate 				p->udp_start = SWAP_32(pd->pd_part_start);
5567c478bd9Sstevel@tonic-gate 				p->udp_length = SWAP_32(pd->pd_part_length);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 				/* LINTED */
5590e42dee6Sartem 				ph = (struct phdr_desc *)pd->pd_pc_use;
5600e42dee6Sartem 				if (ph->phdr_ust.sad_ext_len) {
5617c478bd9Sstevel@tonic-gate 			p->udp_flags = UDP_SPACETBLS;
5620e42dee6Sartem 			p->udp_unall_loc = SWAP_32(ph->phdr_ust.sad_ext_loc);
5630e42dee6Sartem 			p->udp_unall_len = SWAP_32(ph->phdr_ust.sad_ext_len);
5640e42dee6Sartem 			p->udp_freed_loc = SWAP_32(ph->phdr_fst.sad_ext_loc);
5650e42dee6Sartem 			p->udp_freed_len = SWAP_32(ph->phdr_fst.sad_ext_len);
5667c478bd9Sstevel@tonic-gate 				} else {
5677c478bd9Sstevel@tonic-gate 			p->udp_flags = UDP_BITMAPS;
5680e42dee6Sartem 			p->udp_unall_loc = SWAP_32(ph->phdr_usb.sad_ext_loc);
5690e42dee6Sartem 			p->udp_unall_len = SWAP_32(ph->phdr_usb.sad_ext_len);
5700e42dee6Sartem 			p->udp_freed_loc = SWAP_32(ph->phdr_fsb.sad_ext_loc);
5710e42dee6Sartem 			p->udp_freed_len = SWAP_32(ph->phdr_fsb.sad_ext_len);
5727c478bd9Sstevel@tonic-gate 				}
5737c478bd9Sstevel@tonic-gate 
5740e42dee6Sartem 				if (i == h->n_parts) {
5750e42dee6Sartem 					h->n_parts ++;
5767c478bd9Sstevel@tonic-gate 				}
5777c478bd9Sstevel@tonic-gate 			}
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 		case UD_LOG_VOL_DESC :
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 			/*
5827c478bd9Sstevel@tonic-gate 			 * Logical Volume Descriptor
5837c478bd9Sstevel@tonic-gate 			 */
5847c478bd9Sstevel@tonic-gate 			/* LINTED */
5857c478bd9Sstevel@tonic-gate 			lvd = (struct log_vol_desc *)taddr;
5867c478bd9Sstevel@tonic-gate 			if ((v->lvd_len == 0) ||
5877c478bd9Sstevel@tonic-gate 				(SWAP_32(lvd->lvd_vdsn) > v->lvd_vdsn)) {
5887c478bd9Sstevel@tonic-gate 				v->lvd_vdsn = SWAP_32(lvd->lvd_vdsn);
5897c478bd9Sstevel@tonic-gate 				v->lvd_loc = vds_loc;
5907c478bd9Sstevel@tonic-gate 				v->lvd_len = ((uint32_t)
5917c478bd9Sstevel@tonic-gate 					&((struct log_vol_desc *)0)->lvd_pmaps);
5927c478bd9Sstevel@tonic-gate 				v->lvd_len =
5930e42dee6Sartem 					lb_roundup(v->lvd_len, h->udfs.lbsize);
5947c478bd9Sstevel@tonic-gate 			}
5957c478bd9Sstevel@tonic-gate 			break;
5967c478bd9Sstevel@tonic-gate 		case UD_UNALL_SPA_DESC :
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 			/*
5997c478bd9Sstevel@tonic-gate 			 * Unallocated Space Descriptor
6007c478bd9Sstevel@tonic-gate 			 */
6017c478bd9Sstevel@tonic-gate 			/* LINTED */
6027c478bd9Sstevel@tonic-gate 			usd = (struct unall_spc_desc *)taddr;
6037c478bd9Sstevel@tonic-gate 			v->usd_loc = vds_loc;
6047c478bd9Sstevel@tonic-gate 			v->usd_len = ((uint32_t)
6057c478bd9Sstevel@tonic-gate 			&((unall_spc_desc_t *)0)->ua_al_dsc) +
6067c478bd9Sstevel@tonic-gate 				SWAP_32(usd->ua_nad) *
6077c478bd9Sstevel@tonic-gate 				sizeof (struct extent_ad);
6080e42dee6Sartem 			v->usd_len = lb_roundup(v->usd_len, h->udfs.lbsize);
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		case UD_TERM_DESC :
6117c478bd9Sstevel@tonic-gate 			/*
6127c478bd9Sstevel@tonic-gate 			 * Success fully completed
6137c478bd9Sstevel@tonic-gate 			 */
6147c478bd9Sstevel@tonic-gate 			goto end;
6157c478bd9Sstevel@tonic-gate 		default :
6167c478bd9Sstevel@tonic-gate 			/*
6177c478bd9Sstevel@tonic-gate 			 * If you donot undetstand any tag just skip
6187c478bd9Sstevel@tonic-gate 			 * it. This is not a fatal error
6197c478bd9Sstevel@tonic-gate 			 */
6207c478bd9Sstevel@tonic-gate 			break;
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate end:
6257c478bd9Sstevel@tonic-gate 	free(addr);
6267c478bd9Sstevel@tonic-gate 	if ((v->pvd_len == 0) ||
6277c478bd9Sstevel@tonic-gate 		(v->part_len[0] == 0) ||
6287c478bd9Sstevel@tonic-gate 		(v->lvd_len == 0)) {
6297c478bd9Sstevel@tonic-gate 		return (1);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	return (0);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6350e42dee6Sartem static int32_t
ud_read_and_translate_lvd(ud_handle_t h,uint32_t lvd_loc,uint32_t lvd_len)6360e42dee6Sartem ud_read_and_translate_lvd(ud_handle_t h, uint32_t lvd_loc, uint32_t lvd_len)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	caddr_t addr;
6397c478bd9Sstevel@tonic-gate 	uint16_t fsd_prn;
6407c478bd9Sstevel@tonic-gate 	uint32_t fsd_loc, fsd_len;
6417c478bd9Sstevel@tonic-gate 	uint32_t lvds_loc, lvds_len;
6427c478bd9Sstevel@tonic-gate 	uint64_t off;
6437c478bd9Sstevel@tonic-gate 	struct log_vol_desc *lvd = NULL;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	int32_t max_maps, i, mp_sz, index;
6467c478bd9Sstevel@tonic-gate 	struct ud_map *m;
6470e42dee6Sartem 	struct pmap_hdr *ph;
6487c478bd9Sstevel@tonic-gate 	struct pmap_typ1 *typ1;
6497c478bd9Sstevel@tonic-gate 	struct pmap_typ2 *typ2;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (lvd_len == 0) {
6527c478bd9Sstevel@tonic-gate 		return (1);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if ((lvd = (struct log_vol_desc *)
6567c478bd9Sstevel@tonic-gate 			malloc(lvd_len)) == NULL) {
6577c478bd9Sstevel@tonic-gate 		return (1);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6600e42dee6Sartem 	off = lvd_loc * h->udfs.lbsize;
6610e42dee6Sartem 	if (ud_read_dev(h, off, (uint8_t *)lvd, lvd_len) != 0) {
6627c478bd9Sstevel@tonic-gate 		free(lvd);
6637c478bd9Sstevel@tonic-gate 		return (1);
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 
6660e42dee6Sartem 	if (ud_verify_tag(h, &lvd->lvd_tag, UD_LOG_VOL_DESC,
6677c478bd9Sstevel@tonic-gate 			lvd_loc, 1, 0) != 0) {
6687c478bd9Sstevel@tonic-gate 		free(lvd);
6697c478bd9Sstevel@tonic-gate 		return (1);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/*
6737c478bd9Sstevel@tonic-gate 	 * Take care of maps
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 	max_maps = SWAP_32(lvd->lvd_num_pmaps);
6760e42dee6Sartem 	ph = (struct pmap_hdr *)lvd->lvd_pmaps;
6770e42dee6Sartem 	for (h->n_maps = index = 0; index < max_maps; index++) {
6780e42dee6Sartem 		m = &h->maps[h->n_maps];
6790e42dee6Sartem 		switch (ph->maph_type) {
6807c478bd9Sstevel@tonic-gate 		case MAP_TYPE1 :
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			/* LINTED */
6830e42dee6Sartem 			typ1 = (struct pmap_typ1 *)ph;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			m->udm_flags = UDM_MAP_NORM;
6867c478bd9Sstevel@tonic-gate 			m->udm_vsn = SWAP_16(typ1->map1_vsn);
6877c478bd9Sstevel@tonic-gate 			m->udm_pn = SWAP_16(typ1->map1_pn);
6880e42dee6Sartem 			h->n_maps++;
6897c478bd9Sstevel@tonic-gate 			break;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		case MAP_TYPE2 :
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 			/* LINTED */
6940e42dee6Sartem 			typ2 = (struct pmap_typ2 *)ph;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 			if (strncmp(typ2->map2_pti.reg_id,
6977c478bd9Sstevel@tonic-gate 					UDF_VIRT_PART, 23) == 0) {
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 				m->udm_flags = UDM_MAP_VPM;
7007c478bd9Sstevel@tonic-gate 				m->udm_vsn = SWAP_16(typ2->map2_vsn);
7017c478bd9Sstevel@tonic-gate 				m->udm_pn = SWAP_16(typ2->map2_pn);
7027c478bd9Sstevel@tonic-gate 			} else if (strncmp(typ2->map2_pti.reg_id,
7037c478bd9Sstevel@tonic-gate 					UDF_SPAR_PART, 23) == 0) {
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 				if ((SWAP_16(typ2->map2_pl) != 32) ||
7067c478bd9Sstevel@tonic-gate 						(typ2->map2_nst < 1) ||
7077c478bd9Sstevel@tonic-gate 						(typ2->map2_nst > 4)) {
7087c478bd9Sstevel@tonic-gate 					break;
7097c478bd9Sstevel@tonic-gate 				}
7107c478bd9Sstevel@tonic-gate 				m->udm_flags = UDM_MAP_SPM;
7117c478bd9Sstevel@tonic-gate 				m->udm_vsn = SWAP_16(typ2->map2_vsn);
7127c478bd9Sstevel@tonic-gate 				m->udm_pn = SWAP_16(typ2->map2_pn);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 				m->udm_plen = SWAP_16(typ2->map2_pl);
7157c478bd9Sstevel@tonic-gate 				m->udm_nspm = typ2->map2_nst;
7167c478bd9Sstevel@tonic-gate 				m->udm_spsz = SWAP_32(typ2->map2_sest);
7177c478bd9Sstevel@tonic-gate 
7180e42dee6Sartem 				mp_sz = lb_roundup(m->udm_spsz, h->udfs.lbsize);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 				if ((addr = malloc(mp_sz * m->udm_nspm)) ==
7217c478bd9Sstevel@tonic-gate 						NULL) {
7227c478bd9Sstevel@tonic-gate 					break;
7237c478bd9Sstevel@tonic-gate 				}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 				for (i = 0; i < m->udm_nspm; i++) {
7267c478bd9Sstevel@tonic-gate 					m->udm_loc[i] =
7277c478bd9Sstevel@tonic-gate 						SWAP_32(typ2->map2_st[index]);
7287c478bd9Sstevel@tonic-gate 					m->udm_spaddr[i] = addr + i * mp_sz;
7297c478bd9Sstevel@tonic-gate 
7300e42dee6Sartem 					off = m->udm_loc[i] * h->udfs.lbsize;
7310e42dee6Sartem 					if (ud_read_dev(h, off,
7327c478bd9Sstevel@tonic-gate 						(uint8_t *)m->udm_spaddr[i],
7337c478bd9Sstevel@tonic-gate 							mp_sz) != 0) {
7347c478bd9Sstevel@tonic-gate 						m->udm_spaddr[i] = NULL;
7357c478bd9Sstevel@tonic-gate 						continue;
7367c478bd9Sstevel@tonic-gate 					}
7377c478bd9Sstevel@tonic-gate 				}
7387c478bd9Sstevel@tonic-gate 			}
7390e42dee6Sartem 			h->n_maps++;
7407c478bd9Sstevel@tonic-gate 		default :
7417c478bd9Sstevel@tonic-gate 			break;
7427c478bd9Sstevel@tonic-gate 		}
7430e42dee6Sartem 		ph = (struct pmap_hdr *)(((uint8_t *)h) + ph->maph_length);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	lvds_loc = SWAP_32(lvd->lvd_int_seq_ext.ext_loc);
7477c478bd9Sstevel@tonic-gate 	lvds_len = SWAP_32(lvd->lvd_int_seq_ext.ext_len);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	fsd_prn = SWAP_16(lvd->lvd_lvcu.lad_ext_prn);
7507c478bd9Sstevel@tonic-gate 	fsd_loc = SWAP_32(lvd->lvd_lvcu.lad_ext_loc);
7517c478bd9Sstevel@tonic-gate 	fsd_len = SWAP_32(lvd->lvd_lvcu.lad_ext_len);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	free(lvd);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	/*
7567c478bd9Sstevel@tonic-gate 	 * Get the latest LVID
7577c478bd9Sstevel@tonic-gate 	 */
7580e42dee6Sartem 	if (ud_get_latest_lvid(h, lvds_loc, lvds_len) != 0) {
7597c478bd9Sstevel@tonic-gate 		return (1);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
7620e42dee6Sartem 	if (ud_get_latest_fsd(h, fsd_prn, fsd_loc, fsd_len) != 0) {
7637c478bd9Sstevel@tonic-gate 		return (1);
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	return (0);
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7690e42dee6Sartem static int32_t
ud_get_latest_lvid(ud_handle_t h,uint32_t lvds_loc,uint32_t lvds_len)7700e42dee6Sartem ud_get_latest_lvid(ud_handle_t h, uint32_t lvds_loc, uint32_t lvds_len)
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate 	uint8_t *addr, *taddr, *eaddr;
7737c478bd9Sstevel@tonic-gate 	uint16_t id;
7747c478bd9Sstevel@tonic-gate 	uint64_t off;
7757c478bd9Sstevel@tonic-gate 	struct tag *tag;
7767c478bd9Sstevel@tonic-gate 	struct log_vol_int_desc *lvid;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate begin:
7797c478bd9Sstevel@tonic-gate 	if ((addr = (uint8_t *)malloc(lvds_len)) == NULL) {
7807c478bd9Sstevel@tonic-gate 		return (1);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7830e42dee6Sartem 	off = lvds_loc * h->udfs.lbsize;
7840e42dee6Sartem 	if (ud_read_dev(h, off, addr, lvds_len) != 0) {
7857c478bd9Sstevel@tonic-gate 		goto end;
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7880e42dee6Sartem 	for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
7890e42dee6Sartem 			taddr += h->udfs.lbsize, lvds_loc ++) {
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		/* LINTED */
7927c478bd9Sstevel@tonic-gate 		tag = (struct tag *)taddr;
7937c478bd9Sstevel@tonic-gate 		id = SWAP_16(tag->tag_id);
7947c478bd9Sstevel@tonic-gate 		/*
7957c478bd9Sstevel@tonic-gate 		 * If you cannot verify the tag just skip it
7967c478bd9Sstevel@tonic-gate 		 * This is not a fatal error
7977c478bd9Sstevel@tonic-gate 		 */
7980e42dee6Sartem 		if (ud_verify_tag(h, tag, id, lvds_loc, 1, 0) != 0) {
7997c478bd9Sstevel@tonic-gate 			continue;
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 		switch (id) {
8027c478bd9Sstevel@tonic-gate 		case UD_LOG_VOL_INT :
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 			/*
8057c478bd9Sstevel@tonic-gate 			 * Logical Volume Integrity Descriptor
8067c478bd9Sstevel@tonic-gate 			 */
8077c478bd9Sstevel@tonic-gate 			/* LINTED */
8087c478bd9Sstevel@tonic-gate 			lvid = (struct log_vol_int_desc *)taddr;
8090e42dee6Sartem 			h->udfs.lvid_loc = lvds_loc;
8100e42dee6Sartem 			h->udfs.lvid_len = ((uint32_t)
8117c478bd9Sstevel@tonic-gate 			&((struct log_vol_int_desc *)0)->lvid_fst) +
8127c478bd9Sstevel@tonic-gate 				SWAP_32(lvid->lvid_npart) * 8 +
8137c478bd9Sstevel@tonic-gate 				SWAP_32(lvid->lvid_liu);
8140e42dee6Sartem 			h->udfs.lvid_len = lb_roundup(h->udfs.lvid_len,
8150e42dee6Sartem 				h->udfs.lbsize);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 			/*
8187c478bd9Sstevel@tonic-gate 			 * It seems we have a next integrity
8197c478bd9Sstevel@tonic-gate 			 * sequence
8207c478bd9Sstevel@tonic-gate 			 */
8217c478bd9Sstevel@tonic-gate 			if (SWAP_32(lvid->lvid_nie.ext_len) != 0) {
8227c478bd9Sstevel@tonic-gate 				free(addr);
8237c478bd9Sstevel@tonic-gate 				lvds_loc = SWAP_32(lvid->lvid_nie.ext_loc);
8247c478bd9Sstevel@tonic-gate 				lvds_len = SWAP_32(lvid->lvid_nie.ext_len);
8257c478bd9Sstevel@tonic-gate 				goto begin;
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 			goto end;
8287c478bd9Sstevel@tonic-gate 		case UD_TERM_DESC :
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 			/*
8317c478bd9Sstevel@tonic-gate 			 * Success fully completed
8327c478bd9Sstevel@tonic-gate 			 */
8337c478bd9Sstevel@tonic-gate 				goto end;
8347c478bd9Sstevel@tonic-gate 		default :
8357c478bd9Sstevel@tonic-gate 			/*
8367c478bd9Sstevel@tonic-gate 			 * If you donot undetstand any tag just skip
8377c478bd9Sstevel@tonic-gate 			 * it. This is not a fatal error
8387c478bd9Sstevel@tonic-gate 			 */
8397c478bd9Sstevel@tonic-gate 			break;
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate end:
8437c478bd9Sstevel@tonic-gate 	free(addr);
8440e42dee6Sartem 	if (h->udfs.lvid_len == 0) {
8457c478bd9Sstevel@tonic-gate 		return (1);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	return (0);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8500e42dee6Sartem static int32_t
ud_get_latest_fsd(ud_handle_t h,uint16_t fsd_prn,uint32_t fsd_loc,uint32_t fsd_len)8510e42dee6Sartem ud_get_latest_fsd(ud_handle_t h, uint16_t fsd_prn,
8527c478bd9Sstevel@tonic-gate 	uint32_t fsd_loc, uint32_t fsd_len)
8537c478bd9Sstevel@tonic-gate {
8547c478bd9Sstevel@tonic-gate 	uint8_t *addr, *taddr, *eaddr;
8557c478bd9Sstevel@tonic-gate 	uint16_t id;
8567c478bd9Sstevel@tonic-gate 	uint64_t off;
8577c478bd9Sstevel@tonic-gate 	uint32_t fsds_loc, fsds_len;
8587c478bd9Sstevel@tonic-gate 	struct tag *tag;
8597c478bd9Sstevel@tonic-gate 	struct file_set_desc *fsd;
8607c478bd9Sstevel@tonic-gate 	uint32_t old_fsn = 0;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate begin:
8630e42dee6Sartem 	h->udfs.fsds_prn = fsd_prn;
8640e42dee6Sartem 	h->udfs.fsds_loc = fsd_loc;
8650e42dee6Sartem 	h->udfs.fsds_len = fsd_len;
8667c478bd9Sstevel@tonic-gate 
8670e42dee6Sartem 	fsds_loc = ud_xlate_to_daddr(h, fsd_prn, fsd_loc);
8680e42dee6Sartem 	fsds_len = lb_roundup(fsd_len, h->udfs.lbsize);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if ((addr = (uint8_t *)malloc(fsds_len)) == NULL) {
8717c478bd9Sstevel@tonic-gate 		return (1);
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8740e42dee6Sartem 	off = fsds_loc * h->udfs.lbsize;
8750e42dee6Sartem 	if (ud_read_dev(h, off, addr, fsds_len) != 0) {
8767c478bd9Sstevel@tonic-gate 		goto end;
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8790e42dee6Sartem 	for (taddr = addr, eaddr = addr + h->udfs.mvds_len; taddr < eaddr;
8800e42dee6Sartem 			taddr += h->udfs.lbsize, fsds_loc ++) {
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		/* LINTED */
8837c478bd9Sstevel@tonic-gate 		tag = (struct tag *)taddr;
8847c478bd9Sstevel@tonic-gate 		id = SWAP_16(tag->tag_id);
8857c478bd9Sstevel@tonic-gate 		/*
8867c478bd9Sstevel@tonic-gate 		 * If you cannot verify the tag just skip it
8877c478bd9Sstevel@tonic-gate 		 * This is not a fatal error
8887c478bd9Sstevel@tonic-gate 		 */
8890e42dee6Sartem 		if (ud_verify_tag(h, tag, id, fsds_loc, 1, 0) != 0) {
8907c478bd9Sstevel@tonic-gate 			continue;
8917c478bd9Sstevel@tonic-gate 		}
8927c478bd9Sstevel@tonic-gate 		switch (id) {
8937c478bd9Sstevel@tonic-gate 		case UD_FILE_SET_DESC :
8947c478bd9Sstevel@tonic-gate 			/* LINTED */
8957c478bd9Sstevel@tonic-gate 			fsd = (struct file_set_desc *)taddr;
8960e42dee6Sartem 			if ((h->udfs.fsd_len == 0) ||
8977c478bd9Sstevel@tonic-gate 				(SWAP_32(fsd->fsd_fs_no) > old_fsn)) {
8987c478bd9Sstevel@tonic-gate 				old_fsn = SWAP_32(fsd->fsd_fs_no);
8990e42dee6Sartem 				h->udfs.fsd_loc = fsds_loc;
9000e42dee6Sartem 				h->udfs.fsd_len = lb_roundup(512,
9010e42dee6Sartem 					h->udfs.lbsize);
9020e42dee6Sartem 				h->udfs.ricb_prn =
9037c478bd9Sstevel@tonic-gate 					SWAP_16(fsd->fsd_root_icb.lad_ext_prn);
9040e42dee6Sartem 				h->udfs.ricb_loc =
9057c478bd9Sstevel@tonic-gate 					SWAP_32(fsd->fsd_root_icb.lad_ext_loc);
9060e42dee6Sartem 				h->udfs.ricb_len =
9077c478bd9Sstevel@tonic-gate 					SWAP_32(fsd->fsd_root_icb.lad_ext_len);
9087c478bd9Sstevel@tonic-gate 			}
9097c478bd9Sstevel@tonic-gate 			if (SWAP_32(fsd->fsd_next.lad_ext_len) != 0) {
9107c478bd9Sstevel@tonic-gate 				fsd_prn = SWAP_16(fsd->fsd_next.lad_ext_prn);
9117c478bd9Sstevel@tonic-gate 				fsd_loc = SWAP_32(fsd->fsd_next.lad_ext_loc);
9127c478bd9Sstevel@tonic-gate 				fsd_len = SWAP_32(fsd->fsd_next.lad_ext_len);
9137c478bd9Sstevel@tonic-gate 				goto begin;
9147c478bd9Sstevel@tonic-gate 			}
9157c478bd9Sstevel@tonic-gate 			break;
9167c478bd9Sstevel@tonic-gate 		case UD_TERM_DESC :
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 			/*
9197c478bd9Sstevel@tonic-gate 			 * Success fully completed
9207c478bd9Sstevel@tonic-gate 			 */
9217c478bd9Sstevel@tonic-gate 			goto end;
9227c478bd9Sstevel@tonic-gate 		default :
9237c478bd9Sstevel@tonic-gate 			/*
9247c478bd9Sstevel@tonic-gate 			 * If you donot undetstand any tag just skip
9257c478bd9Sstevel@tonic-gate 			 * it. This is not a fatal error
9267c478bd9Sstevel@tonic-gate 			 */
9277c478bd9Sstevel@tonic-gate 			break;
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate end:
9327c478bd9Sstevel@tonic-gate 	free(addr);
9330e42dee6Sartem 	if (h->udfs.fsd_len == 0) {
9347c478bd9Sstevel@tonic-gate 		return (1);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 	return (0);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate int32_t
ud_get_num_blks(ud_handle_t h,uint32_t * blkno)9400e42dee6Sartem ud_get_num_blks(ud_handle_t h, uint32_t *blkno)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	struct vtoc vtoc;
9437c478bd9Sstevel@tonic-gate 	struct dk_cinfo dki_info;
9447c478bd9Sstevel@tonic-gate 	int32_t error;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * Get VTOC from driver
9487c478bd9Sstevel@tonic-gate 	 */
9490e42dee6Sartem 	if ((error = ioctl(h->fd, DKIOCGVTOC, (intptr_t)&vtoc)) != 0) {
9507c478bd9Sstevel@tonic-gate 		return (error);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	/*
9547c478bd9Sstevel@tonic-gate 	 * Verify if is proper
9557c478bd9Sstevel@tonic-gate 	 */
9567c478bd9Sstevel@tonic-gate 	if (vtoc.v_sanity != VTOC_SANE) {
9577c478bd9Sstevel@tonic-gate 		return (EINVAL);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/*
9617c478bd9Sstevel@tonic-gate 	 * Get dk_cinfo from driver
9627c478bd9Sstevel@tonic-gate 	 */
9630e42dee6Sartem 	if ((error = ioctl(h->fd, DKIOCINFO, (intptr_t)&dki_info)) != 0) {
9647c478bd9Sstevel@tonic-gate 		return (error);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	if (dki_info.dki_partition >= V_NUMPAR) {
9687c478bd9Sstevel@tonic-gate 		return (EINVAL);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	/*
9727c478bd9Sstevel@tonic-gate 	 * Return the size of the partition
9737c478bd9Sstevel@tonic-gate 	 */
9747c478bd9Sstevel@tonic-gate 	*blkno = vtoc.v_part[dki_info.dki_partition].p_size;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	return (0);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate uint32_t
ud_xlate_to_daddr(ud_handle_t h,uint16_t prn,uint32_t blkno)9800e42dee6Sartem ud_xlate_to_daddr(ud_handle_t h, uint16_t prn, uint32_t blkno)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	int32_t i;
9837c478bd9Sstevel@tonic-gate 	struct ud_map *m;
9847c478bd9Sstevel@tonic-gate 	struct ud_part *p;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 
9870e42dee6Sartem 	if (prn < h->n_maps) {
9880e42dee6Sartem 		m = &h->maps[prn];
9890e42dee6Sartem 		for (i = 0; i < h->n_parts; i++) {
9900e42dee6Sartem 			p = &h->part[i];
9917c478bd9Sstevel@tonic-gate 			if (m->udm_pn == p->udp_number) {
9927c478bd9Sstevel@tonic-gate 				return (p->udp_start + blkno);
9937c478bd9Sstevel@tonic-gate 			}
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 	return (0);
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate /* ------ END Read and translate the on disk VDS to IN CORE format -------- */
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@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)10020e42dee6Sartem ud_verify_tag(ud_handle_t h, struct tag *tag, uint16_t id,
10037c478bd9Sstevel@tonic-gate 	uint32_t blockno, int32_t do_crc, int32_t print_msg)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	int32_t i;
10067c478bd9Sstevel@tonic-gate 	uint8_t *addr, cksum = 0;
10077c478bd9Sstevel@tonic-gate 	uint16_t crc;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Verify Tag Identifier
10127c478bd9Sstevel@tonic-gate 	 */
10137c478bd9Sstevel@tonic-gate 	if (tag->tag_id != SWAP_16(id)) {
10147c478bd9Sstevel@tonic-gate 		if (print_msg != 0) {
10150e42dee6Sartem 			(void) fprintf(stderr,
10167c478bd9Sstevel@tonic-gate 				gettext("tag does not verify tag %x req %x\n"),
10177c478bd9Sstevel@tonic-gate 				SWAP_16(tag->tag_id), id);
10187c478bd9Sstevel@tonic-gate 		}
10197c478bd9Sstevel@tonic-gate 		return (1);
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/*
10237c478bd9Sstevel@tonic-gate 	 * Verify Tag Descriptor Version
10247c478bd9Sstevel@tonic-gate 	 */
10250e42dee6Sartem 	if (SWAP_16(tag->tag_desc_ver) != h->udfs.ecma_version) {
10267c478bd9Sstevel@tonic-gate 		if (print_msg != 0) {
10270e42dee6Sartem 			(void) fprintf(stderr,
10287c478bd9Sstevel@tonic-gate 				gettext("tag version does not match with "
10297c478bd9Sstevel@tonic-gate 				"NSR descriptor version TAG %x NSR %x\n"),
10300e42dee6Sartem 				SWAP_16(tag->tag_desc_ver),
10310e42dee6Sartem 				h->udfs.ecma_version);
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 		return (1);
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/*
10377c478bd9Sstevel@tonic-gate 	 * Caliculate Tag Checksum
10387c478bd9Sstevel@tonic-gate 	 */
10397c478bd9Sstevel@tonic-gate 	addr = (uint8_t *)tag;
10407c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 15; i++) {
10417c478bd9Sstevel@tonic-gate 		if (i != 4) {
10427c478bd9Sstevel@tonic-gate 			cksum += addr[i];
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/*
10477c478bd9Sstevel@tonic-gate 	 * Verify Tag Checksum
10487c478bd9Sstevel@tonic-gate 	 */
10497c478bd9Sstevel@tonic-gate 	if (cksum != tag->tag_cksum) {
10507c478bd9Sstevel@tonic-gate 		if (print_msg != 0) {
10510e42dee6Sartem 			(void) fprintf(stderr,
10527c478bd9Sstevel@tonic-gate 				gettext("Checksum Does not Verify TAG"
10537c478bd9Sstevel@tonic-gate 				" %x CALC %x\n"), tag->tag_cksum, cksum);
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 		return (1);
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * Do we want to do crc
10617c478bd9Sstevel@tonic-gate 	 */
10627c478bd9Sstevel@tonic-gate 	if (do_crc) {
10637c478bd9Sstevel@tonic-gate 		if (tag->tag_crc_len) {
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 			/*
10667c478bd9Sstevel@tonic-gate 			 * Caliculate CRC for the descriptor
10677c478bd9Sstevel@tonic-gate 			 */
10687c478bd9Sstevel@tonic-gate 			crc = ud_crc(addr + 0x10, SWAP_16(tag->tag_crc_len));
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 			/*
10717c478bd9Sstevel@tonic-gate 			 * Verify CRC
10727c478bd9Sstevel@tonic-gate 			 */
10737c478bd9Sstevel@tonic-gate 			if (crc != SWAP_16(tag->tag_crc)) {
10747c478bd9Sstevel@tonic-gate 				if (print_msg != 0) {
10750e42dee6Sartem 					(void) fprintf(stderr,
10767c478bd9Sstevel@tonic-gate 						gettext("CRC Does not verify"
10777c478bd9Sstevel@tonic-gate 						" TAG %x CALC %x %x\n"),
10787c478bd9Sstevel@tonic-gate