xref: /titanic_44/usr/src/cmd/format/ix_altsctr.c (revision 342440ec94087b8c751c580ab9ed6c693d31d418)
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
5*342440ecSPrasad Singamsetty  * Common Development and Distribution License (the "License").
6*342440ecSPrasad Singamsetty  * 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 /*
22*342440ecSPrasad Singamsetty  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
267c478bd9Sstevel@tonic-gate  * copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
277c478bd9Sstevel@tonic-gate  * All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Copyrighted as an unpublished work.
327c478bd9Sstevel@tonic-gate  * (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990
337c478bd9Sstevel@tonic-gate  * All rights reserved.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <malloc.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/swap.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
477c478bd9Sstevel@tonic-gate #include <sys/dktp/altsctr.h>
487c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
497c478bd9Sstevel@tonic-gate #include "badsec.h"
507c478bd9Sstevel@tonic-gate #include "global.h"
517c478bd9Sstevel@tonic-gate #include "ctlr_ata.h"
527c478bd9Sstevel@tonic-gate #include "misc.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	FAILURE	1
557c478bd9Sstevel@tonic-gate #define	SUCCESS	0
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	CMD_READ	0
587c478bd9Sstevel@tonic-gate #define	CMD_WRITE	1
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate struct	badsec_lst *badsl_chain = NULL;
617c478bd9Sstevel@tonic-gate int	badsl_chain_cnt = 0;
627c478bd9Sstevel@tonic-gate struct	badsec_lst *gbadsl_chain = NULL;
637c478bd9Sstevel@tonic-gate int	gbadsl_chain_cnt = 0;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static struct	alts_mempart alts_part = { 0, NULL, 0 };
667c478bd9Sstevel@tonic-gate struct	alts_mempart	*ap = &alts_part;	/* pointer to incore */
677c478bd9Sstevel@tonic-gate 						/*  alts tables	*/
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* prototypes */
70*342440ecSPrasad Singamsetty int updatebadsec(struct  dkl_partition *, int);
71*342440ecSPrasad Singamsetty int read_altsctr(struct  dkl_partition *);
727c478bd9Sstevel@tonic-gate static int chk_badsec();
737c478bd9Sstevel@tonic-gate static int init_altsctr();
747c478bd9Sstevel@tonic-gate static int get_altsctr();
757c478bd9Sstevel@tonic-gate int wr_altsctr();
767c478bd9Sstevel@tonic-gate static void get_badsec();
777c478bd9Sstevel@tonic-gate static int count_badsec();
787c478bd9Sstevel@tonic-gate static int gen_alts_ent();
797c478bd9Sstevel@tonic-gate static int assign_altsctr();
807c478bd9Sstevel@tonic-gate static void expand_map();
817c478bd9Sstevel@tonic-gate static void compress_map();
82*342440ecSPrasad Singamsetty static int altsmap_getbit(blkaddr_t);
83*342440ecSPrasad Singamsetty static blkaddr_t altsmap_alloc(blkaddr_t, blkaddr_t, int, int);
847c478bd9Sstevel@tonic-gate static void ent_sort(struct  alts_ent *, int);
857c478bd9Sstevel@tonic-gate static void ent_compress(struct  alts_ent *, int);
867c478bd9Sstevel@tonic-gate static int ent_merge(struct alts_ent *, struct alts_ent *, int,
877c478bd9Sstevel@tonic-gate 		struct alts_ent *, int);
887c478bd9Sstevel@tonic-gate static int ent_bsearch(struct  alts_ent *, int, struct  alts_ent *);
89*342440ecSPrasad Singamsetty static int chk_bad_altsctr(blkaddr_t);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * updatebadsec () -- update bad sector/track mapping tables
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate int
updatebadsec(part,init_flag)957c478bd9Sstevel@tonic-gate updatebadsec(part, init_flag)
967c478bd9Sstevel@tonic-gate int	init_flag;
97*342440ecSPrasad Singamsetty struct  dkl_partition *part;
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	if (init_flag)
1007c478bd9Sstevel@tonic-gate 		ap->ap_flag |= ALTS_ADDPART;
1017c478bd9Sstevel@tonic-gate 	get_badsec();
1027c478bd9Sstevel@tonic-gate 	(void) read_altsctr(part);
1037c478bd9Sstevel@tonic-gate 	ent_sort(ap->ap_gbadp, ap->ap_gbadcnt);
1047c478bd9Sstevel@tonic-gate 	ent_compress(ap->ap_gbadp, ap->ap_gbadcnt);
1057c478bd9Sstevel@tonic-gate 	(void) gen_alts_ent();
1067c478bd9Sstevel@tonic-gate 	compress_map();
1077c478bd9Sstevel@tonic-gate 	return (SUCCESS);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * read_altsctr( ptr to alternate sector partition )
1127c478bd9Sstevel@tonic-gate  *		-- read the alternate sector partition tables
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate int
read_altsctr(part)1157c478bd9Sstevel@tonic-gate read_altsctr(part)
116*342440ecSPrasad Singamsetty struct 	dkl_partition *part;
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	if (ap->ap_tblp == NULL) {
1197c478bd9Sstevel@tonic-gate /*	    allocate buffer for the alts partition table (sector size)	*/
1207c478bd9Sstevel@tonic-gate 	    ap->ap_tbl_secsiz = byte_to_secsiz(ALTS_PARTTBL_SIZE, NBPSCTR);
1217c478bd9Sstevel@tonic-gate 	    ap->ap_tblp = (struct alts_parttbl *)malloc(ap->ap_tbl_secsiz);
1227c478bd9Sstevel@tonic-gate 	    if (ap->ap_tblp == NULL) {
1237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1247c478bd9Sstevel@tonic-gate 			"Unable to malloc alternate partition table.\n");
1257c478bd9Sstevel@tonic-gate 		return (50);
1267c478bd9Sstevel@tonic-gate 	    }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*	    allocate buffer for the alts partition map (sector size)	*/
1297c478bd9Sstevel@tonic-gate /*	    buffers include the disk image bit map 			*/
1307c478bd9Sstevel@tonic-gate /*	    and the incore transformed char map				*/
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	    if ((ap->ap_memmapp = (uchar_t *)malloc(part->p_size)) == NULL) {
1337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1347c478bd9Sstevel@tonic-gate 			"Unable to malloc incore alternate partition map.\n");
1357c478bd9Sstevel@tonic-gate 		return (51);
1367c478bd9Sstevel@tonic-gate 	    }
1377c478bd9Sstevel@tonic-gate 	    ap->ap_tblp->alts_map_len = (part->p_size + 8 - 1) / 8;
1387c478bd9Sstevel@tonic-gate 	    ap->ap_map_secsiz = byte_to_secsiz(ap->ap_tblp->alts_map_len,
1397c478bd9Sstevel@tonic-gate 						NBPSCTR);
1407c478bd9Sstevel@tonic-gate 	    ap->ap_map_sectot = ap->ap_map_secsiz / NBPSCTR;
1417c478bd9Sstevel@tonic-gate 	    if ((ap->ap_mapp = (uchar_t *)malloc(ap->ap_map_secsiz)) == NULL) {
1427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1437c478bd9Sstevel@tonic-gate 				"Unable to malloc alternate partition map.\n");
1447c478bd9Sstevel@tonic-gate 		return (52);
1457c478bd9Sstevel@tonic-gate 	    }
1467c478bd9Sstevel@tonic-gate /*	    clear the buffers to zero					*/
1477c478bd9Sstevel@tonic-gate 	    (void) memset(ap->ap_memmapp, 0, part->p_size);
1487c478bd9Sstevel@tonic-gate 	    (void) memset(ap->ap_mapp, 0, ap->ap_map_secsiz);
1497c478bd9Sstevel@tonic-gate 	    ap->part = *part;		/* struct copy			*/
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  *	    if add alternate partition flag is set, then install the partition
1537c478bd9Sstevel@tonic-gate  *	    otherwise read the alts partition info from disk
1547c478bd9Sstevel@tonic-gate  *	    if failed, then assume the first installation
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 	    if (ap->ap_flag & ALTS_ADDPART)
1577c478bd9Sstevel@tonic-gate 	    {
1587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1597c478bd9Sstevel@tonic-gate 			"WARNING: Manually initializing alternate table.\n");
1607c478bd9Sstevel@tonic-gate 		(void) init_altsctr();
1617c478bd9Sstevel@tonic-gate 	    } else {
1627c478bd9Sstevel@tonic-gate 		if (get_altsctr() == SUCCESS)
1637c478bd9Sstevel@tonic-gate 		    (void) chk_badsec();
1647c478bd9Sstevel@tonic-gate 		else
1657c478bd9Sstevel@tonic-gate 		    (void) init_altsctr();
1667c478bd9Sstevel@tonic-gate 	    }
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	return (SUCCESS);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  *	checking duplicate bad sectors or bad sectors in ALTSCTR partition
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate static int
chk_badsec()1767c478bd9Sstevel@tonic-gate chk_badsec()
1777c478bd9Sstevel@tonic-gate {
178*342440ecSPrasad Singamsetty 	blkaddr_t	badsec;
179*342440ecSPrasad Singamsetty 	blkaddr_t	altsp_srtsec = ap->part.p_start;
180*342440ecSPrasad Singamsetty 	blkaddr_t	altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
1817c478bd9Sstevel@tonic-gate 	int	cnt;
1827c478bd9Sstevel@tonic-gate 	int	status;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
1857c478bd9Sstevel@tonic-gate 	    badsec = (ap->ap_gbadp)[cnt].bad_start;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	    /* if bad sector is within the ATLSCTR partition */
1887c478bd9Sstevel@tonic-gate 	    if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
1897c478bd9Sstevel@tonic-gate 		if ((ap->ap_memmapp)[badsec - altsp_srtsec] != ALTS_BAD) {
1907c478bd9Sstevel@tonic-gate 		    if ((badsec >= altsp_srtsec) && (badsec <= (altsp_srtsec +
1917c478bd9Sstevel@tonic-gate 			ap->ap_tbl_secsiz / NBPSCTR - 1))) {
1927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1937c478bd9Sstevel@tonic-gate 			"Alternate partition information table is bad.\n");
1947c478bd9Sstevel@tonic-gate 			return (53);
1957c478bd9Sstevel@tonic-gate 		    }
1967c478bd9Sstevel@tonic-gate 		    if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_map_base) &&
1977c478bd9Sstevel@tonic-gate 			(badsec <= (altsp_srtsec + ap->ap_tblp->alts_map_base +
1987c478bd9Sstevel@tonic-gate 			ap->ap_map_sectot - 1))) {
1997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2007c478bd9Sstevel@tonic-gate 					"Alternate partition map is bad.\n");
2017c478bd9Sstevel@tonic-gate 			return (54);
2027c478bd9Sstevel@tonic-gate 		    }
2037c478bd9Sstevel@tonic-gate 		    if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_ent_base) &&
2047c478bd9Sstevel@tonic-gate 			(badsec <= (altsp_srtsec + ap->ap_tblp->alts_ent_base +
2057c478bd9Sstevel@tonic-gate 			ap->ap_ent_secsiz / NBPSCTR - 1))) {
2067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2077c478bd9Sstevel@tonic-gate 				"Alternate partition entry table is bad.\n");
2087c478bd9Sstevel@tonic-gate 			return (55);
2097c478bd9Sstevel@tonic-gate 		    }
2107c478bd9Sstevel@tonic-gate 		    (ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
211*342440ecSPrasad Singamsetty 		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2127c478bd9Sstevel@tonic-gate 		} else {
2137c478bd9Sstevel@tonic-gate 		    status = chk_bad_altsctr(badsec);
214*342440ecSPrasad Singamsetty 		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 	    } else {
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  *		binary search for bad sector in the alts entry table
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate 		status = ent_bsearch(ap->ap_entp, ap->ap_tblp->alts_ent_used,
2217c478bd9Sstevel@tonic-gate 					&((ap->ap_gbadp)[cnt]));
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  *		if the bad sector had already been remapped(found in alts_entry)
2247c478bd9Sstevel@tonic-gate  *		then ignore the bad sector
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 		if (status != -1) {
227*342440ecSPrasad Singamsetty 		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 	    }
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	return (SUCCESS);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  *	initialize the alternate partition tables
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate static int
init_altsctr()2387c478bd9Sstevel@tonic-gate init_altsctr()
2397c478bd9Sstevel@tonic-gate {
240*342440ecSPrasad Singamsetty 	blkaddr_t	badsec;
241*342440ecSPrasad Singamsetty 	blkaddr_t	altsp_srtsec = ap->part.p_start;
242*342440ecSPrasad Singamsetty 	blkaddr_t	altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
2437c478bd9Sstevel@tonic-gate 	int	cnt;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	ap->ap_entp = NULL;
2467c478bd9Sstevel@tonic-gate 	ap->ap_ent_secsiz = 0;
2477c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_sanity = ALTS_SANITY;
2487c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_version = ALTS_VERSION1;
2497c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_map_len = (ap->part.p_size + 8 - 1) / 8;
2507c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_ent_used = 0;
2517c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_ent_base = 0;
2527c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_ent_end  = 0;
2537c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_resv_base = ap->part.p_size - 1;
2547c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < 5; cnt++)
2557c478bd9Sstevel@tonic-gate 	    ap->ap_tblp->alts_pad[cnt] = 0;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
2587c478bd9Sstevel@tonic-gate 	    badsec = (ap->ap_gbadp)[cnt].bad_start;
2597c478bd9Sstevel@tonic-gate 	    if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
2607c478bd9Sstevel@tonic-gate 		if (badsec == altsp_srtsec) {
2617c478bd9Sstevel@tonic-gate 		    (void) fprintf(stderr,
2627c478bd9Sstevel@tonic-gate 			"First sector of alternate partition is bad.\n");
2637c478bd9Sstevel@tonic-gate 		    return (56);
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 		(ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
266*342440ecSPrasad Singamsetty 		(ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2677c478bd9Sstevel@tonic-gate 	    }
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*	allocate the alts_map on disk skipping possible bad sectors	*/
2717c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_map_base =
2727c478bd9Sstevel@tonic-gate 		altsmap_alloc(ap->ap_tbl_secsiz / NBPSCTR,
2737c478bd9Sstevel@tonic-gate 			ap->part.p_size, ap->ap_map_sectot, ALTS_MAP_UP);
2747c478bd9Sstevel@tonic-gate 	if (ap->ap_tblp->alts_map_base == NULL) {
2757c478bd9Sstevel@tonic-gate 	    perror("Unable to allocate alternate map on disk: ");
2767c478bd9Sstevel@tonic-gate 	    return (57);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	(void) wr_altsctr();
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (SUCCESS);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * 	read the alternate partition tables from disk
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate static int
get_altsctr()2887c478bd9Sstevel@tonic-gate get_altsctr()
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	int	mystatus = FAILURE;
2917c478bd9Sstevel@tonic-gate 	int	status = 0;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*	get alts partition table info					*/
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	status = ata_rdwr(DIR_READ, cur_file, altsec_offset,
2967c478bd9Sstevel@tonic-gate 			ap->ap_tbl_secsiz / UBSIZE, (char *)ap->ap_tblp,
2977c478bd9Sstevel@tonic-gate 			0, NULL);
2987c478bd9Sstevel@tonic-gate 	if (status == FAILURE) {
2997c478bd9Sstevel@tonic-gate 	    perror("Unable to read alternate sector partition: ");
3007c478bd9Sstevel@tonic-gate 	    return (58);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	if (ap->ap_tblp->alts_sanity != ALTS_SANITY)
3037c478bd9Sstevel@tonic-gate 	    return (mystatus);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*	get the alts map						*/
3067c478bd9Sstevel@tonic-gate 	status = ata_rdwr(DIR_READ, cur_file,
3077c478bd9Sstevel@tonic-gate 		(ap->ap_tblp->alts_map_base) + altsec_offset,
3087c478bd9Sstevel@tonic-gate 		ap->ap_map_secsiz / UBSIZE, (char *)ap->ap_mapp, 0, NULL);
3097c478bd9Sstevel@tonic-gate 	if (status == FAILURE) {
3107c478bd9Sstevel@tonic-gate 	    perror("Unable to read alternate sector partition map: ");
3117c478bd9Sstevel@tonic-gate 	    return (59);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*	transform the disk image bit-map to incore char map		*/
3157c478bd9Sstevel@tonic-gate 	expand_map();
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (ap->ap_tblp->alts_ent_used == 0) {
3187c478bd9Sstevel@tonic-gate 	    ap->ap_entp = NULL;
3197c478bd9Sstevel@tonic-gate 	    ap->ap_ent_secsiz = 0;
3207c478bd9Sstevel@tonic-gate 	} else {
3217c478bd9Sstevel@tonic-gate 	    ap->ap_ent_secsiz = byte_to_secsiz(
3227c478bd9Sstevel@tonic-gate 			(ap->ap_tblp->alts_ent_used*ALTS_ENT_SIZE), NBPSCTR);
3237c478bd9Sstevel@tonic-gate 	    if ((ap->ap_entp =
3247c478bd9Sstevel@tonic-gate 		(struct alts_ent *)malloc(ap->ap_ent_secsiz)) == NULL) {
3257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3267c478bd9Sstevel@tonic-gate 			"Unable to malloc alternate sector entry table.\n");
3277c478bd9Sstevel@tonic-gate 		return (60);
3287c478bd9Sstevel@tonic-gate 	    }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	status = ata_rdwr(DIR_READ, cur_file,
3317c478bd9Sstevel@tonic-gate 			(ap->ap_tblp->alts_ent_base) + altsec_offset,
3327c478bd9Sstevel@tonic-gate 			ap->ap_ent_secsiz / UBSIZE, (char *)ap->ap_entp,
3337c478bd9Sstevel@tonic-gate 			0, NULL);
3347c478bd9Sstevel@tonic-gate 	if (status == FAILURE) {
3357c478bd9Sstevel@tonic-gate 		perror("Unable to read alternate sector entry table: ");
3367c478bd9Sstevel@tonic-gate 		return (61);
3377c478bd9Sstevel@tonic-gate 	    }
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	return (SUCCESS);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  *	update the new alternate partition tables on disk
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate int
wr_altsctr()3487c478bd9Sstevel@tonic-gate wr_altsctr()
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	int	status;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (ap->ap_tblp == NULL)
3537c478bd9Sstevel@tonic-gate 		return (0);
3547c478bd9Sstevel@tonic-gate 	status = ata_rdwr(DIR_WRITE, cur_file, altsec_offset,
3557c478bd9Sstevel@tonic-gate 	    ap->ap_tbl_secsiz / UBSIZE, (char *)ap->ap_tblp, 0, NULL);
3567c478bd9Sstevel@tonic-gate 	if (status) {
3577c478bd9Sstevel@tonic-gate 		(void) printf("ata_rdwr status = %d need = %d\n",
3587c478bd9Sstevel@tonic-gate 		    status, ap->ap_tbl_secsiz / 512);
3597c478bd9Sstevel@tonic-gate 		perror("Unable to write with ata_rdwr the alt sector part: ");
3607c478bd9Sstevel@tonic-gate 		return (62);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (ata_rdwr(DIR_WRITE, cur_file, (ap->ap_tblp->alts_map_base) +
3647c478bd9Sstevel@tonic-gate 			altsec_offset, ap->ap_map_secsiz / UBSIZE,
3657c478bd9Sstevel@tonic-gate 			(char *)ap->ap_mapp, 0, NULL) == FAILURE) {
3667c478bd9Sstevel@tonic-gate 	    perror("Unable to write alternate sector partition map: ");
3677c478bd9Sstevel@tonic-gate 	    return (63);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (ap->ap_tblp->alts_ent_used != 0) {
3717c478bd9Sstevel@tonic-gate 	    if (ata_rdwr(DIR_WRITE, cur_file,
3727c478bd9Sstevel@tonic-gate 				(ap->ap_tblp->alts_ent_base)+ altsec_offset,
3737c478bd9Sstevel@tonic-gate 				ap->ap_ent_secsiz / UBSIZE,
3747c478bd9Sstevel@tonic-gate 				(char *)ap->ap_entp, 0, NULL) == FAILURE) {
3757c478bd9Sstevel@tonic-gate 		perror("Unable to write alternate sector entry table: ");
3767c478bd9Sstevel@tonic-gate 		return (64);
3777c478bd9Sstevel@tonic-gate 	    }
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	return (0);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  *	get a list of bad sector
3857c478bd9Sstevel@tonic-gate  */
3867c478bd9Sstevel@tonic-gate static void
get_badsec()3877c478bd9Sstevel@tonic-gate get_badsec()
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	int	cnt;
3907c478bd9Sstevel@tonic-gate 	struct	badsec_lst *blc_p;
391*342440ecSPrasad Singamsetty 	blkaddr_t	curbad;
392*342440ecSPrasad Singamsetty 	blkaddr_t	maxsec = cur_dtype->dtype_nhead *
3937c478bd9Sstevel@tonic-gate 				cur_dtype->dtype_ncyl *
3947c478bd9Sstevel@tonic-gate 				cur_dtype->dtype_nsect;
3957c478bd9Sstevel@tonic-gate 	struct	alts_ent *growbadp;
3967c478bd9Sstevel@tonic-gate 	int	i;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	cnt = count_badsec();
3997c478bd9Sstevel@tonic-gate 	if (!cnt) {
4007c478bd9Sstevel@tonic-gate 	    ap->ap_gbadp = NULL;
4017c478bd9Sstevel@tonic-gate 	    ap->ap_gbadcnt = 0;
4027c478bd9Sstevel@tonic-gate 	} else {
4037c478bd9Sstevel@tonic-gate 	    ap->ap_gbadp = malloc(cnt*ALTS_ENT_SIZE);
4047c478bd9Sstevel@tonic-gate 	    if (ap->ap_gbadp == NULL) {
4057c478bd9Sstevel@tonic-gate 		    err_print("get_badsec: unable to malloc %d bytes\n",
4067c478bd9Sstevel@tonic-gate 			cnt*ALTS_ENT_SIZE);
4077c478bd9Sstevel@tonic-gate 		    fullabort();
4087c478bd9Sstevel@tonic-gate 	    }
4097c478bd9Sstevel@tonic-gate 	    (void) memset(ap->ap_gbadp, 0, cnt*ALTS_ENT_SIZE);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	    for (growbadp = ap->ap_gbadp, cnt = 0, blc_p = badsl_chain;
4127c478bd9Sstevel@tonic-gate 		blc_p; blc_p = blc_p->bl_nxt) {
4137c478bd9Sstevel@tonic-gate 		for (i = 0; i < blc_p->bl_cnt; i++) {
4147c478bd9Sstevel@tonic-gate 		    curbad = blc_p->bl_sec[i];
415*342440ecSPrasad Singamsetty 		    if (curbad < (blkaddr_t)cur_dtype->dtype_nsect) {
4167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4177c478bd9Sstevel@tonic-gate "Ignoring bad sector %ld which is in first track of the drive.\n", curbad);
4187c478bd9Sstevel@tonic-gate 			continue;
4197c478bd9Sstevel@tonic-gate 		    }
4207c478bd9Sstevel@tonic-gate 		    if (curbad >= maxsec) {
4217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4227c478bd9Sstevel@tonic-gate "Ignoring bad sector %ld which is past the end of the drive.\n", curbad);
4237c478bd9Sstevel@tonic-gate 			continue;
4247c478bd9Sstevel@tonic-gate 		    }
4257c478bd9Sstevel@tonic-gate 		    growbadp[cnt].bad_start = curbad;
4267c478bd9Sstevel@tonic-gate 		    growbadp[cnt].bad_end = curbad;
4277c478bd9Sstevel@tonic-gate 		    cnt++;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 	    }
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	ap->ap_gbadcnt = cnt;
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate  *	count number of bad sector on list
4367c478bd9Sstevel@tonic-gate  *	merging the bad sector list from surface analysis and the
4377c478bd9Sstevel@tonic-gate  *	one given through the command line
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate static int
count_badsec()4407c478bd9Sstevel@tonic-gate count_badsec()
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	struct badsec_lst *blc_p;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (!badsl_chain)
4467c478bd9Sstevel@tonic-gate 		badsl_chain = gbadsl_chain;
4477c478bd9Sstevel@tonic-gate 	else {
4487c478bd9Sstevel@tonic-gate 		for (blc_p = badsl_chain; blc_p->bl_nxt; blc_p = blc_p->bl_nxt)
4497c478bd9Sstevel@tonic-gate 			;
4507c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = gbadsl_chain;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	badsl_chain_cnt += gbadsl_chain_cnt;
4547c478bd9Sstevel@tonic-gate 	return (badsl_chain_cnt);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate  *	generate alternate entry table by merging the existing and
4607c478bd9Sstevel@tonic-gate  *	the new entry list.
4617c478bd9Sstevel@tonic-gate  */
4627c478bd9Sstevel@tonic-gate static int
gen_alts_ent()4637c478bd9Sstevel@tonic-gate gen_alts_ent() {
464*342440ecSPrasad Singamsetty 	uint_t	ent_used;
4657c478bd9Sstevel@tonic-gate 	struct	alts_ent *entp;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (ap->ap_gbadcnt == 0)
4687c478bd9Sstevel@tonic-gate 	    return (0);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	ent_used = ap->ap_tblp->alts_ent_used + ap->ap_gbadcnt;
4717c478bd9Sstevel@tonic-gate 	ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE, NBPSCTR);
4727c478bd9Sstevel@tonic-gate 	entp = malloc(ap->ap_ent_secsiz);
4737c478bd9Sstevel@tonic-gate 	if (entp == NULL) {
4747c478bd9Sstevel@tonic-gate 		err_print("get_alts_ent: unable to malloc %d bytes\n",
4757c478bd9Sstevel@tonic-gate 		    ap->ap_ent_secsiz);
4767c478bd9Sstevel@tonic-gate 		fullabort();
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	ent_used = ent_merge(entp, ap->ap_entp, ap->ap_tblp->alts_ent_used,
4807c478bd9Sstevel@tonic-gate 			    ap->ap_gbadp, ap->ap_gbadcnt);
4817c478bd9Sstevel@tonic-gate 	if (ap->ap_entp)
4827c478bd9Sstevel@tonic-gate 	    free(ap->ap_entp);
4837c478bd9Sstevel@tonic-gate 	if (ap->ap_gbadp)
4847c478bd9Sstevel@tonic-gate 	    free(ap->ap_gbadp);
4857c478bd9Sstevel@tonic-gate 	ap->ap_entp = entp;
4867c478bd9Sstevel@tonic-gate 	ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE, NBPSCTR);
4877c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_ent_used = ent_used;
4887c478bd9Sstevel@tonic-gate 	ap->ap_gbadp = NULL;
4897c478bd9Sstevel@tonic-gate 	ap->ap_gbadcnt = 0;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*	assign alternate sectors to the bad sectors			*/
4927c478bd9Sstevel@tonic-gate 	(void) assign_altsctr();
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate /*	allocate the alts_entry on disk skipping possible bad sectors	*/
4957c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_ent_base =
496*342440ecSPrasad Singamsetty 		altsmap_alloc((blkaddr_t)ap->ap_tblp->alts_map_base +
497*342440ecSPrasad Singamsetty 			ap->ap_map_sectot, (blkaddr_t)ap->part.p_size,
4987c478bd9Sstevel@tonic-gate 			ap->ap_ent_secsiz / NBPSCTR, ALTS_MAP_UP);
4997c478bd9Sstevel@tonic-gate 	if (ap->ap_tblp->alts_ent_base == NULL) {
5007c478bd9Sstevel@tonic-gate 	    perror("Unable to allocate alternate entry table on disk: ");
5017c478bd9Sstevel@tonic-gate 	    return (65);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	ap->ap_tblp->alts_ent_end = ap->ap_tblp->alts_ent_base +
5057c478bd9Sstevel@tonic-gate 			(ap->ap_ent_secsiz / NBPSCTR) - 1;
5067c478bd9Sstevel@tonic-gate 	return (0);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate  *	assign alternate sectors for bad sector mapping
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate static int
assign_altsctr()5147c478bd9Sstevel@tonic-gate assign_altsctr()
5157c478bd9Sstevel@tonic-gate {
516*342440ecSPrasad Singamsetty 	uint_t	i;
517*342440ecSPrasad Singamsetty 	uint_t	j;
518*342440ecSPrasad Singamsetty 	blkaddr_t	alts_ind;
519*342440ecSPrasad Singamsetty 	uint_t	cluster;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	for (i = 0; i < ap->ap_tblp->alts_ent_used; i++) {
522*342440ecSPrasad Singamsetty 	    if ((ap->ap_entp)[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
5237c478bd9Sstevel@tonic-gate 		continue;
5247c478bd9Sstevel@tonic-gate 	    if ((ap->ap_entp)[i].good_start != 0)
5257c478bd9Sstevel@tonic-gate 		continue;
5267c478bd9Sstevel@tonic-gate 	    cluster = (ap->ap_entp)[i].bad_end-(ap->ap_entp)[i].bad_start +1;
5277c478bd9Sstevel@tonic-gate 	    alts_ind =
5287c478bd9Sstevel@tonic-gate 		altsmap_alloc(ap->part.p_size-1, ap->ap_tblp->alts_map_base +
5297c478bd9Sstevel@tonic-gate 			ap->ap_map_sectot - 1, cluster, ALTS_MAP_DOWN);
5307c478bd9Sstevel@tonic-gate 	    if (alts_ind == NULL) {
5317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5323ccda647Slclee 	"Unable to allocate alternates for bad starting sector %u.\n",
5337c478bd9Sstevel@tonic-gate 			(ap->ap_entp)[i].bad_start);
5347c478bd9Sstevel@tonic-gate 		return (65);
5357c478bd9Sstevel@tonic-gate 	    }
5367c478bd9Sstevel@tonic-gate 	    alts_ind = alts_ind - cluster + 1;
5377c478bd9Sstevel@tonic-gate 	    (ap->ap_entp)[i].good_start = alts_ind +ap->part.p_start;
5387c478bd9Sstevel@tonic-gate 	    for (j = 0; j < cluster; j++) {
5397c478bd9Sstevel@tonic-gate 		(ap->ap_memmapp)[alts_ind+j] = ALTS_BAD;
5407c478bd9Sstevel@tonic-gate 	    }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	return (SUCCESS);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  *	transform the disk image alts bit map to incore char map
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate static void
expand_map()5507c478bd9Sstevel@tonic-gate expand_map()
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	int 	i;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	for (i = 0; i < ap->part.p_size; i++) {
5557c478bd9Sstevel@tonic-gate 	    (ap->ap_memmapp)[i] = altsmap_getbit(i);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  *	transform the incore alts char map to the disk image bit map
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate static void
compress_map()5637c478bd9Sstevel@tonic-gate compress_map()
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	int 	i;
5677c478bd9Sstevel@tonic-gate 	int	bytesz;
5687c478bd9Sstevel@tonic-gate 	char	mask = 0;
5697c478bd9Sstevel@tonic-gate 	int	maplen = 0;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	for (i = 0, bytesz = 7; i < ap->part.p_size; i++) {
5727c478bd9Sstevel@tonic-gate 	    mask |= ((ap->ap_memmapp)[i] << bytesz--);
5737c478bd9Sstevel@tonic-gate 	    if (bytesz < 0) {
5747c478bd9Sstevel@tonic-gate 		(ap->ap_mapp)[maplen++] = mask;
5757c478bd9Sstevel@tonic-gate 		bytesz = 7;
5767c478bd9Sstevel@tonic-gate 		mask = 0;
5777c478bd9Sstevel@tonic-gate 	    }
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  *	if partition size != multiple number of bytes
5817c478bd9Sstevel@tonic-gate  *	then record the last partial byte
5827c478bd9Sstevel@tonic-gate  */
5837c478bd9Sstevel@tonic-gate 	if (bytesz != 7)
5847c478bd9Sstevel@tonic-gate 	    (ap->ap_mapp)[maplen] = mask;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate  *	given a bad sector number, search in the alts bit map
5907c478bd9Sstevel@tonic-gate  *	and identify the sector as good or bad
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate static int
altsmap_getbit(badsec)5937c478bd9Sstevel@tonic-gate altsmap_getbit(badsec)
594*342440ecSPrasad Singamsetty blkaddr_t	badsec;
5957c478bd9Sstevel@tonic-gate {
596*342440ecSPrasad Singamsetty 	uint_t	slot = badsec / 8;
597*342440ecSPrasad Singamsetty 	uint_t	field = badsec % 8;
5987c478bd9Sstevel@tonic-gate 	uchar_t	mask;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	mask = ALTS_BAD<<7;
6017c478bd9Sstevel@tonic-gate 	mask >>= field;
6027c478bd9Sstevel@tonic-gate 	if ((ap->ap_mapp)[slot] & mask)
6037c478bd9Sstevel@tonic-gate 	    return (ALTS_BAD);
6047c478bd9Sstevel@tonic-gate 	return (ALTS_GOOD);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  *	allocate a range of sectors from the alternate partition
6107c478bd9Sstevel@tonic-gate  */
611*342440ecSPrasad Singamsetty static blkaddr_t
altsmap_alloc(srt_ind,end_ind,cnt,dir)6127c478bd9Sstevel@tonic-gate altsmap_alloc(srt_ind, end_ind, cnt, dir)
613*342440ecSPrasad Singamsetty blkaddr_t	srt_ind;
614*342440ecSPrasad Singamsetty blkaddr_t	end_ind;
6157c478bd9Sstevel@tonic-gate int	cnt;
6167c478bd9Sstevel@tonic-gate int	dir;
6177c478bd9Sstevel@tonic-gate {
618*342440ecSPrasad Singamsetty 	blkaddr_t	i;
619*342440ecSPrasad Singamsetty 	blkaddr_t	total;
620*342440ecSPrasad Singamsetty 	blkaddr_t	first_ind;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	for (i = srt_ind, first_ind = srt_ind, total = 0;
6237c478bd9Sstevel@tonic-gate 	    i != end_ind; i += dir) {
6247c478bd9Sstevel@tonic-gate 	    if ((ap->ap_memmapp)[i] == ALTS_BAD) {
6257c478bd9Sstevel@tonic-gate 		total = 0;
6267c478bd9Sstevel@tonic-gate 		first_ind = i + dir;
6277c478bd9Sstevel@tonic-gate 		continue;
6287c478bd9Sstevel@tonic-gate 	    }
6297c478bd9Sstevel@tonic-gate 	    total++;
6307c478bd9Sstevel@tonic-gate 	    if (total == cnt)
6317c478bd9Sstevel@tonic-gate 		return (first_ind);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	return (NULL);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * 	bubble sort the entry table into ascending order
6417c478bd9Sstevel@tonic-gate  */
6427c478bd9Sstevel@tonic-gate static void
ent_sort(buf,cnt)6437c478bd9Sstevel@tonic-gate ent_sort(buf, cnt)
6447c478bd9Sstevel@tonic-gate struct	alts_ent buf[];
6457c478bd9Sstevel@tonic-gate int	cnt;
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate struct	alts_ent temp;
6487c478bd9Sstevel@tonic-gate int	flag;
6497c478bd9Sstevel@tonic-gate int	i, j;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt-1; i++) {
6527c478bd9Sstevel@tonic-gate 	    temp = buf[cnt-1];
6537c478bd9Sstevel@tonic-gate 	    flag = 1;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	    for (j = cnt-1; j > i; j--) {
6567c478bd9Sstevel@tonic-gate 		if (buf[j-1].bad_start < temp.bad_start) {
6577c478bd9Sstevel@tonic-gate 		    buf[j] = temp;
6587c478bd9Sstevel@tonic-gate 		    temp = buf[j-1];
6597c478bd9Sstevel@tonic-gate 		} else {
6607c478bd9Sstevel@tonic-gate 		    buf[j] = buf[j-1];
6617c478bd9Sstevel@tonic-gate 		    flag = 0;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 	    }
6647c478bd9Sstevel@tonic-gate 	    buf[i] = temp;
6657c478bd9Sstevel@tonic-gate 	    if (flag) break;
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate  *	compress all the contiguous bad sectors into a single entry
6737c478bd9Sstevel@tonic-gate  *	in the entry table. The entry table must be sorted into ascending
6747c478bd9Sstevel@tonic-gate  *	before the compression.
6757c478bd9Sstevel@tonic-gate  */
6767c478bd9Sstevel@tonic-gate static void
ent_compress(buf,cnt)6777c478bd9Sstevel@tonic-gate ent_compress(buf, cnt)
6787c478bd9Sstevel@tonic-gate struct	alts_ent buf[];
6797c478bd9Sstevel@tonic-gate int	cnt;
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate int	keyp;
6827c478bd9Sstevel@tonic-gate int	movp;
6837c478bd9Sstevel@tonic-gate int	i;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
686*342440ecSPrasad Singamsetty 	    if (buf[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
6877c478bd9Sstevel@tonic-gate 		continue;
6887c478bd9Sstevel@tonic-gate 	    for (keyp = i, movp = i+1; movp < cnt; movp++) {
689*342440ecSPrasad Singamsetty 		if (buf[movp].bad_start == (uint32_t)ALTS_ENT_EMPTY)
6907c478bd9Sstevel@tonic-gate 			continue;
6917c478bd9Sstevel@tonic-gate 		if (buf[keyp].bad_end+1 != buf[movp].bad_start)
6927c478bd9Sstevel@tonic-gate 		    break;
6937c478bd9Sstevel@tonic-gate 		buf[keyp].bad_end++;
694*342440ecSPrasad Singamsetty 		buf[movp].bad_start = (uint32_t)ALTS_ENT_EMPTY;
6957c478bd9Sstevel@tonic-gate 	    }
6967c478bd9Sstevel@tonic-gate 	    if (movp == cnt) break;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  *	merging two entry tables into a single table. In addition,
7037c478bd9Sstevel@tonic-gate  *	all empty slots in the entry table will be removed.
7047c478bd9Sstevel@tonic-gate  */
7057c478bd9Sstevel@tonic-gate static int
ent_merge(buf,list1,lcnt1,list2,lcnt2)7067c478bd9Sstevel@tonic-gate ent_merge(buf, list1, lcnt1, list2, lcnt2)
7077c478bd9Sstevel@tonic-gate struct	alts_ent buf[];
7087c478bd9Sstevel@tonic-gate struct	alts_ent list1[];
7097c478bd9Sstevel@tonic-gate int	lcnt1;
7107c478bd9Sstevel@tonic-gate struct	alts_ent list2[];
7117c478bd9Sstevel@tonic-gate int	lcnt2;
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	int	i;
7147c478bd9Sstevel@tonic-gate 	int	j1, j2;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	for (i = 0, j1 = 0, j2 = 0; j1 < lcnt1 && j2 < lcnt2; ) {
717*342440ecSPrasad Singamsetty 	    if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
7187c478bd9Sstevel@tonic-gate 		j1++;
7197c478bd9Sstevel@tonic-gate 		continue;
7207c478bd9Sstevel@tonic-gate 	    }
721*342440ecSPrasad Singamsetty 	    if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
7227c478bd9Sstevel@tonic-gate 		j2++;
7237c478bd9Sstevel@tonic-gate 		continue;
7247c478bd9Sstevel@tonic-gate 	    }
7257c478bd9Sstevel@tonic-gate 	    if (list1[j1].bad_start < list2[j2].bad_start)
7267c478bd9Sstevel@tonic-gate 		buf[i++] = list1[j1++];
7277c478bd9Sstevel@tonic-gate 	    else
7287c478bd9Sstevel@tonic-gate 		buf[i++] = list2[j2++];
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 	for (; j1 < lcnt1; j1++) {
731*342440ecSPrasad Singamsetty 	    if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY)
7327c478bd9Sstevel@tonic-gate 		continue;
7337c478bd9Sstevel@tonic-gate 	    buf[i++] = list1[j1];
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	for (; j2 < lcnt2; j2++) {
736*342440ecSPrasad Singamsetty 	    if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY)
7377c478bd9Sstevel@tonic-gate 		continue;
7387c478bd9Sstevel@tonic-gate 	    buf[i++] = list2[j2];
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 	return (i);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  *	binary search for bad sector in the alternate entry table
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate static int
ent_bsearch(buf,cnt,key)7487c478bd9Sstevel@tonic-gate ent_bsearch(buf, cnt, key)
7497c478bd9Sstevel@tonic-gate struct	alts_ent buf[];
7507c478bd9Sstevel@tonic-gate int	cnt;
7517c478bd9Sstevel@tonic-gate struct	alts_ent *key;
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	int	i;
7547c478bd9Sstevel@tonic-gate 	int	ind;
7557c478bd9Sstevel@tonic-gate 	int	interval;
7567c478bd9Sstevel@tonic-gate 	int	mystatus = -1;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	if (!cnt)
7597c478bd9Sstevel@tonic-gate 	    return (mystatus);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	for (i = 1; i <= cnt; i <<= 1)
7627c478bd9Sstevel@tonic-gate 	    ind = i;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	for (interval = ind; interval; ) {
7657c478bd9Sstevel@tonic-gate 	    if ((key->bad_start >= buf[ind-1].bad_start) &&
7667c478bd9Sstevel@tonic-gate 		(key->bad_start <= buf[ind-1].bad_end)) {
7677c478bd9Sstevel@tonic-gate 		return (mystatus = ind-1);
7687c478bd9Sstevel@tonic-gate 	    } else {
7697c478bd9Sstevel@tonic-gate 		interval >>= 1;
7707c478bd9Sstevel@tonic-gate 		if (!interval) break;
7717c478bd9Sstevel@tonic-gate 		if (key->bad_start < buf[ind-1].bad_start) {
7727c478bd9Sstevel@tonic-gate 		    ind = ind - interval;
7737c478bd9Sstevel@tonic-gate 		} else {
7747c478bd9Sstevel@tonic-gate /*	if key is larger than the last element then break	*/
7757c478bd9Sstevel@tonic-gate 		    if (ind == cnt) break;
7767c478bd9Sstevel@tonic-gate 		    if ((ind+interval) <= cnt)
7777c478bd9Sstevel@tonic-gate 			ind += interval;
7787c478bd9Sstevel@tonic-gate 		}
7797c478bd9Sstevel@tonic-gate 	    }
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 	return (mystatus);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate  *	check for bad sector in assigned alternate sectors
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate static int
chk_bad_altsctr(badsec)7887c478bd9Sstevel@tonic-gate chk_bad_altsctr(badsec)
789*342440ecSPrasad Singamsetty blkaddr_t	badsec;
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	int	i;
792*342440ecSPrasad Singamsetty 	blkaddr_t	numsec;
7937c478bd9Sstevel@tonic-gate 	int	cnt = ap->ap_tblp->alts_ent_used;
7947c478bd9Sstevel@tonic-gate /*
7957c478bd9Sstevel@tonic-gate  *	daddr_t intv[3];
7967c478bd9Sstevel@tonic-gate  */
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
7997c478bd9Sstevel@tonic-gate 	    numsec = (ap->ap_entp)[i].bad_end - (ap->ap_entp)[i].bad_start;
8007c478bd9Sstevel@tonic-gate 	    if ((badsec >= (ap->ap_entp)[i].good_start) &&
8017c478bd9Sstevel@tonic-gate 		(badsec <= ((ap->ap_entp)[i].good_start + numsec))) {
8027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8037c478bd9Sstevel@tonic-gate 		"Bad sector %ld is an assigned alternate sector.\n", badsec);
8047c478bd9Sstevel@tonic-gate 		return (66);
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  *		if (!numsec) {
8077c478bd9Sstevel@tonic-gate  *		    (ap->ap_entp)[i].good_start = 0;
8087c478bd9Sstevel@tonic-gate  *		    return (FAILURE);
8097c478bd9Sstevel@tonic-gate  *		}
8107c478bd9Sstevel@tonic-gate  *		intv[0] = badsec - (ap->ap_entp)[i].good_start;
8117c478bd9Sstevel@tonic-gate  *		intv[1] = 1;
8127c478bd9Sstevel@tonic-gate  *		intv[2] = (ap->ap_entp)[i].good_start + numsec - badsec;
8137c478bd9Sstevel@tonic-gate  */
8147c478bd9Sstevel@tonic-gate 	    }
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate /*	the bad sector has already been identified as bad		*/
8177c478bd9Sstevel@tonic-gate 	return (SUCCESS);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate }
820