17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5342440ePrasad Singamsetty * Common Development and Distribution License (the "License").
6342440ePrasad Singamsetty * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22342440ePrasad Singamsetty * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate *
257c478bdstevel@tonic-gate * copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
267c478bdstevel@tonic-gate * copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
277c478bdstevel@tonic-gate * All rights reserved.
287c478bdstevel@tonic-gate */
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * Copyrighted as an unpublished work.
327c478bdstevel@tonic-gate * (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990
337c478bdstevel@tonic-gate * All rights reserved.
347c478bdstevel@tonic-gate */
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate#include <sys/types.h>
377c478bdstevel@tonic-gate#include <ctype.h>
387c478bdstevel@tonic-gate#include <fcntl.h>
397c478bdstevel@tonic-gate#include <malloc.h>
407c478bdstevel@tonic-gate#include <sys/stat.h>
417c478bdstevel@tonic-gate#include <sys/swap.h>
427c478bdstevel@tonic-gate#include <stdio.h>
437c478bdstevel@tonic-gate#include <string.h>
447c478bdstevel@tonic-gate#include <sys/vtoc.h>
457c478bdstevel@tonic-gate#include <sys/param.h>
467c478bdstevel@tonic-gate#include <sys/dkio.h>
477c478bdstevel@tonic-gate#include <sys/dktp/altsctr.h>
487c478bdstevel@tonic-gate#include <sys/dktp/fdisk.h>
497c478bdstevel@tonic-gate#include "badsec.h"
507c478bdstevel@tonic-gate#include "global.h"
517c478bdstevel@tonic-gate#include "ctlr_ata.h"
527c478bdstevel@tonic-gate#include "misc.h"
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate#define	FAILURE	1
557c478bdstevel@tonic-gate#define	SUCCESS	0
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#define	CMD_READ	0
587c478bdstevel@tonic-gate#define	CMD_WRITE	1
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gatestruct	badsec_lst *badsl_chain = NULL;
617c478bdstevel@tonic-gateint	badsl_chain_cnt = 0;
627c478bdstevel@tonic-gatestruct	badsec_lst *gbadsl_chain = NULL;
637c478bdstevel@tonic-gateint	gbadsl_chain_cnt = 0;
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gatestatic struct	alts_mempart alts_part = { 0, NULL, 0 };
667c478bdstevel@tonic-gatestruct	alts_mempart	*ap = &alts_part;	/* pointer to incore */
677c478bdstevel@tonic-gate						/*  alts tables	*/
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate/* prototypes */
70342440ePrasad Singamsettyint updatebadsec(struct  dkl_partition *, int);
71342440ePrasad Singamsettyint read_altsctr(struct  dkl_partition *);
727c478bdstevel@tonic-gatestatic int chk_badsec();
737c478bdstevel@tonic-gatestatic int init_altsctr();
747c478bdstevel@tonic-gatestatic int get_altsctr();
757c478bdstevel@tonic-gateint wr_altsctr();
767c478bdstevel@tonic-gatestatic void get_badsec();
777c478bdstevel@tonic-gatestatic int count_badsec();
787c478bdstevel@tonic-gatestatic int gen_alts_ent();
797c478bdstevel@tonic-gatestatic int assign_altsctr();
807c478bdstevel@tonic-gatestatic void expand_map();
817c478bdstevel@tonic-gatestatic void compress_map();
82342440ePrasad Singamsettystatic int altsmap_getbit(blkaddr_t);
83342440ePrasad Singamsettystatic blkaddr_t altsmap_alloc(blkaddr_t, blkaddr_t, int, int);
847c478bdstevel@tonic-gatestatic void ent_sort(struct  alts_ent *, int);
857c478bdstevel@tonic-gatestatic void ent_compress(struct  alts_ent *, int);
867c478bdstevel@tonic-gatestatic int ent_merge(struct alts_ent *, struct alts_ent *, int,
877c478bdstevel@tonic-gate		struct alts_ent *, int);
887c478bdstevel@tonic-gatestatic int ent_bsearch(struct  alts_ent *, int, struct  alts_ent *);
89342440ePrasad Singamsettystatic int chk_bad_altsctr(blkaddr_t);
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate/*
927c478bdstevel@tonic-gate * updatebadsec () -- update bad sector/track mapping tables
937c478bdstevel@tonic-gate */
947c478bdstevel@tonic-gateint
957c478bdstevel@tonic-gateupdatebadsec(part, init_flag)
967c478bdstevel@tonic-gateint	init_flag;
97342440ePrasad Singamsettystruct  dkl_partition *part;
987c478bdstevel@tonic-gate{
997c478bdstevel@tonic-gate	if (init_flag)
1007c478bdstevel@tonic-gate		ap->ap_flag |= ALTS_ADDPART;
1017c478bdstevel@tonic-gate	get_badsec();
1027c478bdstevel@tonic-gate	(void) read_altsctr(part);
1037c478bdstevel@tonic-gate	ent_sort(ap->ap_gbadp, ap->ap_gbadcnt);
1047c478bdstevel@tonic-gate	ent_compress(ap->ap_gbadp, ap->ap_gbadcnt);
1057c478bdstevel@tonic-gate	(void) gen_alts_ent();
1067c478bdstevel@tonic-gate	compress_map();
1077c478bdstevel@tonic-gate	return (SUCCESS);
1087c478bdstevel@tonic-gate}
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate/*
1117c478bdstevel@tonic-gate * read_altsctr( ptr to alternate sector partition )
1127c478bdstevel@tonic-gate *		-- read the alternate sector partition tables
1137c478bdstevel@tonic-gate */
1147c478bdstevel@tonic-gateint
1154d7452fToomas Soomeread_altsctr(struct dkl_partition *part)
1167c478bdstevel@tonic-gate{
1177c478bdstevel@tonic-gate	if (ap->ap_tblp == NULL) {
1187c478bdstevel@tonic-gate/*	    allocate buffer for the alts partition table (sector size)	*/
1197c478bdstevel@tonic-gate	    ap->ap_tbl_secsiz = byte_to_secsiz(ALTS_PARTTBL_SIZE, NBPSCTR);
1207c478bdstevel@tonic-gate	    ap->ap_tblp = (struct alts_parttbl *)malloc(ap->ap_tbl_secsiz);
1217c478bdstevel@tonic-gate	    if (ap->ap_tblp == NULL) {
1227c478bdstevel@tonic-gate		(void) fprintf(stderr,
1237c478bdstevel@tonic-gate			"Unable to malloc alternate partition table.\n");
1247c478bdstevel@tonic-gate		return (50);
1257c478bdstevel@tonic-gate	    }
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate/*	    allocate buffer for the alts partition map (sector size)	*/
1284d7452fToomas Soome/*	    buffers include the disk image bit map			*/
1297c478bdstevel@tonic-gate/*	    and the incore transformed char map				*/
1307c478bdstevel@tonic-gate
1317c478bdstevel@tonic-gate	    if ((ap->ap_memmapp = (uchar_t *)malloc(part->p_size)) == NULL) {
1327c478bdstevel@tonic-gate		(void) fprintf(stderr,
1337c478bdstevel@tonic-gate			"Unable to malloc incore alternate partition map.\n");
1347c478bdstevel@tonic-gate		return (51);
1357c478bdstevel@tonic-gate	    }
1367c478bdstevel@tonic-gate	    ap->ap_tblp->alts_map_len = (part->p_size + 8 - 1) / 8;
1377c478bdstevel@tonic-gate	    ap->ap_map_secsiz = byte_to_secsiz(ap->ap_tblp->alts_map_len,
1387c478bdstevel@tonic-gate						NBPSCTR);
1397c478bdstevel@tonic-gate	    ap->ap_map_sectot = ap->ap_map_secsiz / NBPSCTR;
1407c478bdstevel@tonic-gate	    if ((ap->ap_mapp = (uchar_t *)malloc(ap->ap_map_secsiz)) == NULL) {
1417c478bdstevel@tonic-gate		(void) fprintf(stderr,
1427c478bdstevel@tonic-gate				"Unable to malloc alternate partition map.\n");
1437c478bdstevel@tonic-gate		return (52);
1447c478bdstevel@tonic-gate	    }
1457c478bdstevel@tonic-gate/*	    clear the buffers to zero					*/
1467c478bdstevel@tonic-gate	    (void) memset(ap->ap_memmapp, 0, part->p_size);
1477c478bdstevel@tonic-gate	    (void) memset(ap->ap_mapp, 0, ap->ap_map_secsiz);
1487c478bdstevel@tonic-gate	    ap->part = *part;		/* struct copy			*/
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate/*
1517c478bdstevel@tonic-gate *	    if add alternate partition flag is set, then install the partition
1527c478bdstevel@tonic-gate *	    otherwise read the alts partition info from disk
1537c478bdstevel@tonic-gate *	    if failed, then assume the first installation
1547c478bdstevel@tonic-gate */
1557c478bdstevel@tonic-gate	    if (ap->ap_flag & ALTS_ADDPART)
1567c478bdstevel@tonic-gate	    {
1577c478bdstevel@tonic-gate		(void) fprintf(stderr,
1587c478bdstevel@tonic-gate			"WARNING: Manually initializing alternate table.\n");
1597c478bdstevel@tonic-gate		(void) init_altsctr();
1607c478bdstevel@tonic-gate	    } else {
1617c478bdstevel@tonic-gate		if (get_altsctr() == SUCCESS)
1627c478bdstevel@tonic-gate		    (void) chk_badsec();
1637c478bdstevel@tonic-gate		else
1647c478bdstevel@tonic-gate		    (void) init_altsctr();
1657c478bdstevel@tonic-gate	    }
1667c478bdstevel@tonic-gate	}
1677c478bdstevel@tonic-gate	return (SUCCESS);
1687c478bdstevel@tonic-gate}
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate/*
1727c478bdstevel@tonic-gate *	checking duplicate bad sectors or bad sectors in ALTSCTR partition
1737c478bdstevel@tonic-gate */
1747c478bdstevel@tonic-gatestatic int
1757c478bdstevel@tonic-gatechk_badsec()
1767c478bdstevel@tonic-gate{
177342440ePrasad Singamsetty	blkaddr_t	badsec;
178342440ePrasad Singamsetty	blkaddr_t	altsp_srtsec = ap->part.p_start;
179342440ePrasad Singamsetty	blkaddr_t	altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
1807c478bdstevel@tonic-gate	int	cnt;
1817c478bdstevel@tonic-gate	int	status;
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate	for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
1847c478bdstevel@tonic-gate	    badsec = (ap->ap_gbadp)[cnt].bad_start;
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate	    /* if bad sector is within the ATLSCTR partition */
1877c478bdstevel@tonic-gate	    if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
1887c478bdstevel@tonic-gate		if ((ap->ap_memmapp)[badsec - altsp_srtsec] != ALTS_BAD) {
1897c478bdstevel@tonic-gate		    if ((badsec >= altsp_srtsec) && (badsec <= (altsp_srtsec +
1907c478bdstevel@tonic-gate			ap->ap_tbl_secsiz / NBPSCTR - 1))) {
1917c478bdstevel@tonic-gate			(void) fprintf(stderr,
1927c478bdstevel@tonic-gate			"Alternate partition information table is bad.\n");
1937c478bdstevel@tonic-gate			return (53);
1947c478bdstevel@tonic-gate		    }
1957c478bdstevel@tonic-gate		    if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_map_base) &&
1967c478bdstevel@tonic-gate			(badsec <= (altsp_srtsec + ap->ap_tblp->alts_map_base +
1977c478bdstevel@tonic-gate			ap->ap_map_sectot - 1))) {
1987c478bdstevel@tonic-gate			(void) fprintf(stderr,
1997c478bdstevel@tonic-gate					"Alternate partition map is bad.\n");
2007c478bdstevel@tonic-gate			return (54);
2017c478bdstevel@tonic-gate		    }
2027c478bdstevel@tonic-gate		    if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_ent_base) &&
2037c478bdstevel@tonic-gate			(badsec <= (altsp_srtsec + ap->ap_tblp->alts_ent_base +
2047c478bdstevel@tonic-gate			ap->ap_ent_secsiz / NBPSCTR - 1))) {
2057c478bdstevel@tonic-gate			(void) fprintf(stderr,
2067c478bdstevel@tonic-gate				"Alternate partition entry table is bad.\n");
2077c478bdstevel@tonic-gate			return (55);
2087c478bdstevel@tonic-gate		    }
2097c478bdstevel@tonic-gate		    (ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
210342440ePrasad Singamsetty		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2117c478bdstevel@tonic-gate		} else {
2127c478bdstevel@tonic-gate		    status = chk_bad_altsctr(badsec);
213342440ePrasad Singamsetty		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2147c478bdstevel@tonic-gate		}
2157c478bdstevel@tonic-gate	    } else {
2167c478bdstevel@tonic-gate/*
2177c478bdstevel@tonic-gate *		binary search for bad sector in the alts entry table
2187c478bdstevel@tonic-gate */
2197c478bdstevel@tonic-gate		status = ent_bsearch(ap->ap_entp, ap->ap_tblp->alts_ent_used,
2207c478bdstevel@tonic-gate					&((ap->ap_gbadp)[cnt]));
2217c478bdstevel@tonic-gate/*
2227c478bdstevel@tonic-gate *		if the bad sector had already been remapped(found in alts_entry)
2237c478bdstevel@tonic-gate *		then ignore the bad sector
2247c478bdstevel@tonic-gate */
2257c478bdstevel@tonic-gate		if (status != -1) {
226342440ePrasad Singamsetty		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2277c478bdstevel@tonic-gate		}
2287c478bdstevel@tonic-gate	    }
2297c478bdstevel@tonic-gate	}
2307c478bdstevel@tonic-gate	return (SUCCESS);
2317c478bdstevel@tonic-gate}
2327c478bdstevel@tonic-gate
2337c478bdstevel@tonic-gate/*
2347c478bdstevel@tonic-gate *	initialize the alternate partition tables
2357c478bdstevel@tonic-gate */
2367c478bdstevel@tonic-gatestatic int
2377c478bdstevel@tonic-gateinit_altsctr()
2387c478bdstevel@tonic-gate{
239342440ePrasad Singamsetty	blkaddr_t	badsec;
240342440ePrasad Singamsetty	blkaddr_t	altsp_srtsec = ap->part.p_start;
241342440ePrasad Singamsetty	blkaddr_t	altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
2427c478bdstevel@tonic-gate	int	cnt;
2437c478bdstevel@tonic-gate
2447c478bdstevel@tonic-gate	ap->ap_entp = NULL;
2457c478bdstevel@tonic-gate	ap->ap_ent_secsiz = 0;
2467c478bdstevel@tonic-gate	ap->ap_tblp->alts_sanity = ALTS_SANITY;
2477c478bdstevel@tonic-gate	ap->ap_tblp->alts_version = ALTS_VERSION1;
2487c478bdstevel@tonic-gate	ap->ap_tblp->alts_map_len = (ap->part.p_size + 8 - 1) / 8;
2497c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_used = 0;
2507c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_base = 0;
2517c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_end  = 0;
2527c478bdstevel@tonic-gate	ap->ap_tblp->alts_resv_base = ap->part.p_size - 1;
2537c478bdstevel@tonic-gate	for (cnt = 0; cnt < 5; cnt++)
2547c478bdstevel@tonic-gate	    ap->ap_tblp->alts_pad[cnt] = 0;
2557c478bdstevel@tonic-gate
2567c478bdstevel@tonic-gate	for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
2577c478bdstevel@tonic-gate	    badsec = (ap->ap_gbadp)[cnt].bad_start;
2587c478bdstevel@tonic-gate	    if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
2597c478bdstevel@tonic-gate		if (badsec == altsp_srtsec) {
2607c478bdstevel@tonic-gate		    (void) fprintf(stderr,
2617c478bdstevel@tonic-gate			"First sector of alternate partition is bad.\n");
2627c478bdstevel@tonic-gate		    return (56);
2637c478bdstevel@tonic-gate		}
2647c478bdstevel@tonic-gate		(ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
265342440ePrasad Singamsetty		(ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2667c478bdstevel@tonic-gate	    }
2677c478bdstevel@tonic-gate	}
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate/*	allocate the alts_map on disk skipping possible bad sectors	*/
2707c478bdstevel@tonic-gate	ap->ap_tblp->alts_map_base =
2717c478bdstevel@tonic-gate		altsmap_alloc(ap->ap_tbl_secsiz / NBPSCTR,
2727c478bdstevel@tonic-gate			ap->part.p_size, ap->ap_map_sectot, ALTS_MAP_UP);
2734d7452fToomas Soome	if (ap->ap_tblp->alts_map_base == 0) {
2747c478bdstevel@tonic-gate	    perror("Unable to allocate alternate map on disk: ");
2757c478bdstevel@tonic-gate	    return (57);
2767c478bdstevel@tonic-gate	}
2777c478bdstevel@tonic-gate	(void) wr_altsctr();
2787c478bdstevel@tonic-gate
2797c478bdstevel@tonic-gate	return (SUCCESS);
2807c478bdstevel@tonic-gate}
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate
2837c478bdstevel@tonic-gate/*
2844d7452fToomas Soome *	read the alternate partition tables from disk
2857c478bdstevel@tonic-gate */
2867c478bdstevel@tonic-gatestatic int
2874d7452fToomas Soomeget_altsctr(void)
2887c478bdstevel@tonic-gate{
2897c478bdstevel@tonic-gate	int	mystatus = FAILURE;
2907c478bdstevel@tonic-gate	int	status = 0;
2917c478bdstevel@tonic-gate
2927c478bdstevel@tonic-gate/*	get alts partition table info					*/
2937c478bdstevel@tonic-gate
2947c478bdstevel@tonic-gate	status = ata_rdwr(DIR_READ, cur_file, altsec_offset,
2957c478bdstevel@tonic-gate			ap->ap_tbl_secsiz / UBSIZE, (char *)ap->ap_tblp,
2967c478bdstevel@tonic-gate			0, NULL);
2977c478bdstevel@tonic-gate	if (status == FAILURE) {
2987c478bdstevel@tonic-gate	    perror("Unable to read alternate sector partition: ");
2997c478bdstevel@tonic-gate	    return (58);
3007c478bdstevel@tonic-gate	}
3017c478bdstevel@tonic-gate	if (ap->ap_tblp->alts_sanity != ALTS_SANITY)
3027c478bdstevel@tonic-gate	    return (mystatus);
3037c478bdstevel@tonic-gate
3047c478bdstevel@tonic-gate/*	get the alts map						*/
3057c478bdstevel@tonic-gate	status = ata_rdwr(DIR_READ, cur_file,
3067c478bdstevel@tonic-gate		(ap->ap_tblp->alts_map_base) + altsec_offset,
3077c478bdstevel@tonic-gate		ap->ap_map_secsiz / UBSIZE, (char *)ap->ap_mapp, 0, NULL);
3087c478bdstevel@tonic-gate	if (status == FAILURE) {
3097c478bdstevel@tonic-gate	    perror("Unable to read alternate sector partition map: ");
3107c478bdstevel@tonic-gate	    return (59);
3117c478bdstevel@tonic-gate	}
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate/*	transform the disk image bit-map to incore char map		*/
3147c478bdstevel@tonic-gate	expand_map();
3157c478bdstevel@tonic-gate
3167c478bdstevel@tonic-gate	if (ap->ap_tblp->alts_ent_used == 0) {
3177c478bdstevel@tonic-gate	    ap->ap_entp = NULL;
3187c478bdstevel@tonic-gate	    ap->ap_ent_secsiz = 0;
3197c478bdstevel@tonic-gate	} else {
3207c478bdstevel@tonic-gate	    ap->ap_ent_secsiz = byte_to_secsiz(
3217c478bdstevel@tonic-gate			(ap->ap_tblp->alts_ent_used*ALTS_ENT_SIZE), NBPSCTR);
3227c478bdstevel@tonic-gate	    if ((ap->ap_entp =
3237c478bdstevel@tonic-gate		(struct alts_ent *)malloc(ap->ap_ent_secsiz)) == NULL) {
3247c478bdstevel@tonic-gate		(void) fprintf(stderr,
3257c478bdstevel@tonic-gate			"Unable to malloc alternate sector entry table.\n");
3267c478bdstevel@tonic-gate		return (60);
3277c478bdstevel@tonic-gate	    }
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate	status = ata_rdwr(DIR_READ, cur_file,
3307c478bdstevel@tonic-gate			(ap->ap_tblp->alts_ent_base) + altsec_offset,
3317c478bdstevel@tonic-gate			ap->ap_ent_secsiz / UBSIZE, (char *)ap->ap_entp,
3327c478bdstevel@tonic-gate			0, NULL);
3337c478bdstevel@tonic-gate	if (status == FAILURE) {
3347c478bdstevel@tonic-gate		perror("Unable to read alternate sector entry table: ");
3357c478bdstevel@tonic-gate		return (61);
3367c478bdstevel@tonic-gate	    }
3377c478bdstevel@tonic-gate	}
3387c478bdstevel@tonic-gate
3397c478bdstevel@tonic-gate	return (SUCCESS);
3407c478bdstevel@tonic-gate}
3417c478bdstevel@tonic-gate
3427c478bdstevel@tonic-gate
3437c478bdstevel@tonic-gate/*
3447c478bdstevel@tonic-gate *	update the new alternate partition tables on disk
3457c478bdstevel@tonic-gate */
3467c478bdstevel@tonic-gateint
3477c478bdstevel@tonic-gatewr_altsctr()
3487c478bdstevel@tonic-gate{
3497c478bdstevel@tonic-gate	int	status;
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	if (ap->ap_tblp == NULL)
3527c478bdstevel@tonic-gate		return (0);
3537c478bdstevel@tonic-gate	status = ata_rdwr(DIR_WRITE, cur_file, altsec_offset,
3547c478bdstevel@tonic-gate	    ap->ap_tbl_secsiz / UBSIZE, (char *)ap->ap_tblp, 0, NULL);
3557c478bdstevel@tonic-gate	if (status) {
3567c478bdstevel@tonic-gate		(void) printf("ata_rdwr status = %d need = %d\n",
3577c478bdstevel@tonic-gate		    status, ap->ap_tbl_secsiz / 512);
3587c478bdstevel@tonic-gate		perror("Unable to write with ata_rdwr the alt sector part: ");
3597c478bdstevel@tonic-gate		return (62);
3607c478bdstevel@tonic-gate	}
3617c478bdstevel@tonic-gate
3627c478bdstevel@tonic-gate	if (ata_rdwr(DIR_WRITE, cur_file, (ap->ap_tblp->alts_map_base) +
3637c478bdstevel@tonic-gate			altsec_offset, ap->ap_map_secsiz / UBSIZE,
3647c478bdstevel@tonic-gate			(char *)ap->ap_mapp, 0, NULL) == FAILURE) {
3657c478bdstevel@tonic-gate	    perror("Unable to write alternate sector partition map: ");
3667c478bdstevel@tonic-gate	    return (63);
3677c478bdstevel@tonic-gate	}
3687c478bdstevel@tonic-gate
3697c478bdstevel@tonic-gate	if (ap->ap_tblp->alts_ent_used != 0) {
3707c478bdstevel@tonic-gate	    if (ata_rdwr(DIR_WRITE, cur_file,
3717c478bdstevel@tonic-gate				(ap->ap_tblp->alts_ent_base)+ altsec_offset,
3727c478bdstevel@tonic-gate				ap->ap_ent_secsiz / UBSIZE,
3737c478bdstevel@tonic-gate				(char *)ap->ap_entp, 0, NULL) == FAILURE) {
3747c478bdstevel@tonic-gate		perror("Unable to write alternate sector entry table: ");
3757c478bdstevel@tonic-gate		return (64);
3767c478bdstevel@tonic-gate	    }
3777c478bdstevel@tonic-gate	}
3787c478bdstevel@tonic-gate	return (0);
3797c478bdstevel@tonic-gate}
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate
3827c478bdstevel@tonic-gate/*
3837c478bdstevel@tonic-gate *	get a list of bad sector
3847c478bdstevel@tonic-gate */
3857c478bdstevel@tonic-gatestatic void
3867c478bdstevel@tonic-gateget_badsec()
3877c478bdstevel@tonic-gate{
3887c478bdstevel@tonic-gate	int	cnt;
3897c478bdstevel@tonic-gate	struct	badsec_lst *blc_p;
390342440ePrasad Singamsetty	blkaddr_t	curbad;
391342440ePrasad Singamsetty	blkaddr_t	maxsec = cur_dtype->dtype_nhead *
3927c478bdstevel@tonic-gate				cur_dtype->dtype_ncyl *
3937c478bdstevel@tonic-gate				cur_dtype->dtype_nsect;
3947c478bdstevel@tonic-gate	struct	alts_ent *growbadp;
3957c478bdstevel@tonic-gate	int	i;
3967c478bdstevel@tonic-gate
3977c478bdstevel@tonic-gate	cnt = count_badsec();
3987c478bdstevel@tonic-gate	if (!cnt) {
3997c478bdstevel@tonic-gate	    ap->ap_gbadp = NULL;
4007c478bdstevel@tonic-gate	    ap->ap_gbadcnt = 0;
4017c478bdstevel@tonic-gate	} else {
4027c478bdstevel@tonic-gate	    ap->ap_gbadp = malloc(cnt*ALTS_ENT_SIZE);
4037c478bdstevel@tonic-gate	    if (ap->ap_gbadp == NULL) {
4047c478bdstevel@tonic-gate		    err_print("get_badsec: unable to malloc %d bytes\n",
4057c478bdstevel@tonic-gate			cnt*ALTS_ENT_SIZE);
4067c478bdstevel@tonic-gate		    fullabort();
4077c478bdstevel@tonic-gate	    }
4087c478bdstevel@tonic-gate	    (void) memset(ap->ap_gbadp, 0, cnt*ALTS_ENT_SIZE);
4097c478bdstevel@tonic-gate
4107c478bdstevel@tonic-gate	    for (growbadp = ap->ap_gbadp, cnt = 0, blc_p = badsl_chain;
4117c478bdstevel@tonic-gate		blc_p; blc_p = blc_p->bl_nxt) {
4127c478bdstevel@tonic-gate		for (i = 0; i < blc_p->bl_cnt; i++) {
4137c478bdstevel@tonic-gate		    curbad = blc_p->bl_sec[i];
414342440ePrasad Singamsetty		    if (curbad < (blkaddr_t)cur_dtype->dtype_nsect) {
4157c478bdstevel@tonic-gate			(void) fprintf(stderr,
4167c478bdstevel@tonic-gate"Ignoring bad sector %ld which is in first track of the drive.\n", curbad);
4177c478bdstevel@tonic-gate			continue;
4187c478bdstevel@tonic-gate		    }
4197c478bdstevel@tonic-gate		    if (curbad >= maxsec) {
4207c478bdstevel@tonic-gate			(void) fprintf(stderr,
4217c478bdstevel@tonic-gate"Ignoring bad sector %ld which is past the end of the drive.\n", curbad);
4227c478bdstevel@tonic-gate			continue;
4237c478bdstevel@tonic-gate		    }
4247c478bdstevel@tonic-gate		    growbadp[cnt].bad_start = curbad;
4257c478bdstevel@tonic-gate		    growbadp[cnt].bad_end = curbad;
4267c478bdstevel@tonic-gate		    cnt++;
4277c478bdstevel@tonic-gate		}
4287c478bdstevel@tonic-gate	    }
4297c478bdstevel@tonic-gate	}
4307c478bdstevel@tonic-gate	ap->ap_gbadcnt = cnt;
4317c478bdstevel@tonic-gate}
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate/*
4347c478bdstevel@tonic-gate *	count number of bad sector on list
4357c478bdstevel@tonic-gate *	merging the bad sector list from surface analysis and the
4367c478bdstevel@tonic-gate *	one given through the command line
4377c478bdstevel@tonic-gate */
4387c478bdstevel@tonic-gatestatic int
4397c478bdstevel@tonic-gatecount_badsec()
4407c478bdstevel@tonic-gate{
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gate	struct badsec_lst *blc_p;
4437c478bdstevel@tonic-gate
4447c478bdstevel@tonic-gate	if (!badsl_chain)
4457c478bdstevel@tonic-gate		badsl_chain = gbadsl_chain;
4467c478bdstevel@tonic-gate	else {
4477c478bdstevel@tonic-gate		for (blc_p = badsl_chain; blc_p->bl_nxt; blc_p = blc_p->bl_nxt)
4487c478bdstevel@tonic-gate			;
4497c478bdstevel@tonic-gate		blc_p->bl_nxt = gbadsl_chain;
4507c478bdstevel@tonic-gate	}
4517c478bdstevel@tonic-gate
4527c478bdstevel@tonic-gate	badsl_chain_cnt += gbadsl_chain_cnt;
4537c478bdstevel@tonic-gate	return (badsl_chain_cnt);
4547c478bdstevel@tonic-gate}
4557c478bdstevel@tonic-gate
4567c478bdstevel@tonic-gate
4577c478bdstevel@tonic-gate/*
4587c478bdstevel@tonic-gate *	generate alternate entry table by merging the existing and
4597c478bdstevel@tonic-gate *	the new entry list.
4607c478bdstevel@tonic-gate */
4617c478bdstevel@tonic-gatestatic int
4627c478bdstevel@tonic-gategen_alts_ent() {
463342440ePrasad Singamsetty	uint_t	ent_used;
4647c478bdstevel@tonic-gate	struct	alts_ent *entp;
4657c478bdstevel@tonic-gate
4667c478bdstevel@tonic-gate	if (ap->ap_gbadcnt == 0)
4677c478bdstevel@tonic-gate	    return (0);
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate	ent_used = ap->ap_tblp->alts_ent_used + ap->ap_gbadcnt;
4707c478bdstevel@tonic-gate	ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE, NBPSCTR);
4717c478bdstevel@tonic-gate	entp = malloc(ap->ap_ent_secsiz);
4727c478bdstevel@tonic-gate	if (entp == NULL) {
4737c478bdstevel@tonic-gate		err_print("get_alts_ent: unable to malloc %d bytes\n",
4747c478bdstevel@tonic-gate		    ap->ap_ent_secsiz);
4757c478bdstevel@tonic-gate		fullabort();
4767c478bdstevel@tonic-gate	}
4777c478bdstevel@tonic-gate
4787c478bdstevel@tonic-gate	ent_used = ent_merge(entp, ap->ap_entp, ap->ap_tblp->alts_ent_used,
4797c478bdstevel@tonic-gate			    ap->ap_gbadp, ap->ap_gbadcnt);
4807c478bdstevel@tonic-gate	if (ap->ap_entp)
4817c478bdstevel@tonic-gate	    free(ap->ap_entp);
4827c478bdstevel@tonic-gate	if (ap->ap_gbadp)
4837c478bdstevel@tonic-gate	    free(ap->ap_gbadp);
4847c478bdstevel@tonic-gate	ap->ap_entp = entp;
4857c478bdstevel@tonic-gate	ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE, NBPSCTR);
4867c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_used = ent_used;
4877c478bdstevel@tonic-gate	ap->ap_gbadp = NULL;
4887c478bdstevel@tonic-gate	ap->ap_gbadcnt = 0;
4897c478bdstevel@tonic-gate
4907c478bdstevel@tonic-gate/*	assign alternate sectors to the bad sectors			*/
4917c478bdstevel@tonic-gate	(void) assign_altsctr();
4927c478bdstevel@tonic-gate
4937c478bdstevel@tonic-gate/*	allocate the alts_entry on disk skipping possible bad sectors	*/
4947c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_base =
495342440ePrasad Singamsetty		altsmap_alloc((blkaddr_t)ap->ap_tblp->alts_map_base +
496342440ePrasad Singamsetty			ap->ap_map_sectot, (blkaddr_t)ap->part.p_size,
4977c478bdstevel@tonic-gate			ap->ap_ent_secsiz / NBPSCTR, ALTS_MAP_UP);
4984d7452fToomas Soome	if (ap->ap_tblp->alts_ent_base == 0) {
4997c478bdstevel@tonic-gate	    perror("Unable to allocate alternate entry table on disk: ");
5007c478bdstevel@tonic-gate	    return (65);
5017c478bdstevel@tonic-gate	}
5027c478bdstevel@tonic-gate
5037c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_end = ap->ap_tblp->alts_ent_base +
5047c478bdstevel@tonic-gate			(ap->ap_ent_secsiz / NBPSCTR) - 1;
5057c478bdstevel@tonic-gate	return (0);
5067c478bdstevel@tonic-gate}
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate
5097c478bdstevel@tonic-gate/*
5107c478bdstevel@tonic-gate *	assign alternate sectors for bad sector mapping
5117c478bdstevel@tonic-gate */
5127c478bdstevel@tonic-gatestatic int
5137c478bdstevel@tonic-gateassign_altsctr()
5147c478bdstevel@tonic-gate{
515342440ePrasad Singamsetty	uint_t	i;
516342440ePrasad Singamsetty	uint_t	j;
517342440ePrasad Singamsetty	blkaddr_t	alts_ind;
518342440ePrasad Singamsetty	uint_t	cluster;
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate	for (i = 0; i < ap->ap_tblp->alts_ent_used; i++) {
521342440ePrasad Singamsetty	    if ((ap->ap_entp)[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
5227c478bdstevel@tonic-gate		continue;
5237c478bdstevel@tonic-gate	    if ((ap->ap_entp)[i].good_start != 0)
5247c478bdstevel@tonic-gate		continue;
5257c478bdstevel@tonic-gate	    cluster = (ap->ap_entp)[i].bad_end-(ap->ap_entp)[i].bad_start +1;
5267c478bdstevel@tonic-gate	    alts_ind =
5277c478bdstevel@tonic-gate		altsmap_alloc(ap->part.p_size-1, ap->ap_tblp->alts_map_base +
5287c478bdstevel@tonic-gate			ap->ap_map_sectot - 1, cluster, ALTS_MAP_DOWN);
5294d7452fToomas Soome	    if (alts_ind == 0) {
5307c478bdstevel@tonic-gate		(void) fprintf(stderr,
5313ccda64lclee	"Unable to allocate alternates for bad starting sector %u.\n",
5327c478bdstevel@tonic-gate			(ap->ap_entp)[i].bad_start);
5337c478bdstevel@tonic-gate		return (65);
5347c478bdstevel@tonic-gate	    }
5357c478bdstevel@tonic-gate	    alts_ind = alts_ind - cluster + 1;
5367c478bdstevel@tonic-gate	    (ap->ap_entp)[i].good_start = alts_ind +ap->part.p_start;
5377c478bdstevel@tonic-gate	    for (j = 0; j < cluster; j++) {
5387c478bdstevel@tonic-gate		(ap->ap_memmapp)[alts_ind+j] = ALTS_BAD;
5397c478bdstevel@tonic-gate	    }
5407c478bdstevel@tonic-gate
5417c478bdstevel@tonic-gate	}
5427c478bdstevel@tonic-gate	return (SUCCESS);
5437c478bdstevel@tonic-gate}
5447c478bdstevel@tonic-gate
5457c478bdstevel@tonic-gate/*
5467c478bdstevel@tonic-gate *	transform the disk image alts bit map to incore char map
5477c478bdstevel@tonic-gate */
5487c478bdstevel@tonic-gatestatic void
5494d7452fToomas Soomeexpand_map(void)
5507c478bdstevel@tonic-gate{
5514d7452fToomas Soome	int	i;
5527c478bdstevel@tonic-gate
5537c478bdstevel@tonic-gate	for (i = 0; i < ap->part.p_size; i++) {
5547c478bdstevel@tonic-gate	    (ap->ap_memmapp)[i] = altsmap_getbit(i);
5557c478bdstevel@tonic-gate	}
5567c478bdstevel@tonic-gate}
5577c478bdstevel@tonic-gate
5587c478bdstevel@tonic-gate/*
5597c478bdstevel@tonic-gate *	transform the incore alts char map to the disk image bit map
5607c478bdstevel@tonic-gate */
5617c478bdstevel@tonic-gatestatic void
5624d7452fToomas Soomecompress_map(void)
5637c478bdstevel@tonic-gate{
5644d7452fToomas Soome	int	i;
5657c478bdstevel@tonic-gate	int	bytesz;
5667c478bdstevel@tonic-gate	char	mask = 0;
5677c478bdstevel@tonic-gate	int	maplen = 0;
5687c478bdstevel@tonic-gate
5697c478bdstevel@tonic-gate	for (i = 0, bytesz = 7; i < ap->part.p_size; i++) {
5707c478bdstevel@tonic-gate	    mask |= ((ap->ap_memmapp)[i] << bytesz--);
5717c478bdstevel@tonic-gate	    if (bytesz < 0) {
5727c478bdstevel@tonic-gate		(ap->ap_mapp)[maplen++] = mask;
5737c478bdstevel@tonic-gate		bytesz = 7;
5747c478bdstevel@tonic-gate		mask = 0;
5757c478bdstevel@tonic-gate	    }
5767c478bdstevel@tonic-gate	}
5777c478bdstevel@tonic-gate/*
5787c478bdstevel@tonic-gate *	if partition size != multiple number of bytes
5797c478bdstevel@tonic-gate *	then record the last partial byte
5807c478bdstevel@tonic-gate */
5817c478bdstevel@tonic-gate	if (bytesz != 7)
5827c478bdstevel@tonic-gate	    (ap->ap_mapp)[maplen] = mask;
5837c478bdstevel@tonic-gate
5847c478bdstevel@tonic-gate}
5857c478bdstevel@tonic-gate
5867c478bdstevel@tonic-gate/*
5877c478bdstevel@tonic-gate *	given a bad sector number, search in the alts bit map
5887c478bdstevel@tonic-gate *	and identify the sector as good or bad
5897c478bdstevel@tonic-gate */
5907c478bdstevel@tonic-gatestatic int
5917c478bdstevel@tonic-gatealtsmap_getbit(badsec)
592342440ePrasad Singamsettyblkaddr_t	badsec;
5937c478bdstevel@tonic-gate{
594342440ePrasad Singamsetty	uint_t	slot = badsec / 8;
595342440ePrasad Singamsetty	uint_t	field = badsec % 8;
5967c478bdstevel@tonic-gate	uchar_t	mask;
5977c478bdstevel@tonic-gate
5987c478bdstevel@tonic-gate	mask = ALTS_BAD<<7;
5997c478bdstevel@tonic-gate	mask >>= field;
6007c478bdstevel@tonic-gate	if ((ap->ap_mapp)[slot] & mask)
6017c478bdstevel@tonic-gate	    return (ALTS_BAD);
6027c478bdstevel@tonic-gate	return (ALTS_GOOD);
6037c478bdstevel@tonic-gate}
6047c478bdstevel@tonic-gate
6057c478bdstevel@tonic-gate
6067c478bdstevel@tonic-gate/*
6077c478bdstevel@tonic-gate *	allocate a range of sectors from the alternate partition
6087c478bdstevel@tonic-gate */
609342440ePrasad Singamsettystatic blkaddr_t
6107c478bdstevel@tonic-gatealtsmap_alloc(srt_ind, end_ind, cnt, dir)
611342440ePrasad Singamsettyblkaddr_t	srt_ind;
612342440ePrasad Singamsettyblkaddr_t	end_ind;
6137c478bdstevel@tonic-gateint	cnt;
6147c478bdstevel@tonic-gateint	dir;
6157c478bdstevel@tonic-gate{
616342440ePrasad Singamsetty	blkaddr_t	i;
617342440ePrasad Singamsetty	blkaddr_t	total;
618342440ePrasad Singamsetty	blkaddr_t	first_ind;
6197c478bdstevel@tonic-gate
6207c478bdstevel@tonic-gate	for (i = srt_ind, first_ind = srt_ind, total = 0;
6217c478bdstevel@tonic-gate	    i != end_ind; i += dir) {
6227c478bdstevel@tonic-gate	    if ((ap->ap_memmapp)[i] == ALTS_BAD) {
6237c478bdstevel@tonic-gate		total = 0;
6247c478bdstevel@tonic-gate		first_ind = i + dir;
6257c478bdstevel@tonic-gate		continue;
6267c478bdstevel@tonic-gate	    }
6277c478bdstevel@tonic-gate	    total++;
6287c478bdstevel@tonic-gate	    if (total == cnt)
6297c478bdstevel@tonic-gate		return (first_ind);
6307c478bdstevel@tonic-gate
6317c478bdstevel@tonic-gate	}
6324d7452fToomas Soome	return (0);
6337c478bdstevel@tonic-gate}
6347c478bdstevel@tonic-gate
6357c478bdstevel@tonic-gate
6367c478bdstevel@tonic-gate
6377c478bdstevel@tonic-gate/*
6384d7452fToomas Soome *	bubble sort the entry table into ascending order
6397c478bdstevel@tonic-gate */
6407c478bdstevel@tonic-gatestatic void
6417c478bdstevel@tonic-gateent_sort(buf, cnt)
6427c478bdstevel@tonic-gatestruct	alts_ent buf[];
6437c478bdstevel@tonic-gateint	cnt;
6447c478bdstevel@tonic-gate{
6457c478bdstevel@tonic-gatestruct	alts_ent temp;
6467c478bdstevel@tonic-gateint	flag;
6477c478bdstevel@tonic-gateint	i, j;
6487c478bdstevel@tonic-gate
6497c478bdstevel@tonic-gate	for (i = 0; i < cnt-1; i++) {
6507c478bdstevel@tonic-gate	    temp = buf[cnt-1];
6517c478bdstevel@tonic-gate	    flag = 1;
6527c478bdstevel@tonic-gate
6537c478bdstevel@tonic-gate	    for (j = cnt-1; j > i; j--) {
6547c478bdstevel@tonic-gate		if (buf[j-1].bad_start < temp.bad_start) {
6557c478bdstevel@tonic-gate		    buf[j] = temp;
6567c478bdstevel@tonic-gate		    temp = buf[j-1];
6577c478bdstevel@tonic-gate		} else {
6587c478bdstevel@tonic-gate		    buf[j] = buf[j-1];
6597c478bdstevel@tonic-gate		    flag = 0;
6607c478bdstevel@tonic-gate		}
6617c478bdstevel@tonic-gate	    }
6627c478bdstevel@tonic-gate	    buf[i] = temp;
6637c478bdstevel@tonic-gate	    if (flag) break;
6647c478bdstevel@tonic-gate	}
6657c478bdstevel@tonic-gate
6667c478bdstevel@tonic-gate}
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate
6697c478bdstevel@tonic-gate/*
6707c478bdstevel@tonic-gate *	compress all the contiguous bad sectors into a single entry
6717c478bdstevel@tonic-gate *	in the entry table. The entry table must be sorted into ascending
6727c478bdstevel@tonic-gate *	before the compression.
6737c478bdstevel@tonic-gate */
6747c478bdstevel@tonic-gatestatic void
6757c478bdstevel@tonic-gateent_compress(buf, cnt)
6767c478bdstevel@tonic-gatestruct	alts_ent buf[];
6777c478bdstevel@tonic-gateint	cnt;
6787c478bdstevel@tonic-gate{
6797c478bdstevel@tonic-gateint	keyp;
6807c478bdstevel@tonic-gateint	movp;
6817c478bdstevel@tonic-gateint	i;
6827c478bdstevel@tonic-gate
6837c478bdstevel@tonic-gate	for (i = 0; i < cnt; i++) {
684342440ePrasad Singamsetty	    if (buf[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
6857c478bdstevel@tonic-gate		continue;
6867c478bdstevel@tonic-gate	    for (keyp = i, movp = i+1; movp < cnt; movp++) {
687342440ePrasad Singamsetty		if (buf[movp].bad_start == (uint32_t)ALTS_ENT_EMPTY)
6887c478bdstevel@tonic-gate			continue;
6897c478bdstevel@tonic-gate		if (buf[keyp].bad_end+1 != buf[movp].bad_start)
6907c478bdstevel@tonic-gate		    break;
6917c478bdstevel@tonic-gate		buf[keyp].bad_end++;
692342440ePrasad Singamsetty		buf[movp].bad_start = (uint32_t)ALTS_ENT_EMPTY;
6937c478bdstevel@tonic-gate	    }
6947c478bdstevel@tonic-gate	    if (movp == cnt) break;
6957c478bdstevel@tonic-gate	}
6967c478bdstevel@tonic-gate}
6977c478bdstevel@tonic-gate
6987c478bdstevel@tonic-gate
6997c478bdstevel@tonic-gate/*
7007c478bdstevel@tonic-gate *	merging two entry tables into a single table. In addition,
7017c478bdstevel@tonic-gate *	all empty slots in the entry table will be removed.
7027c478bdstevel@tonic-gate */
7037c478bdstevel@tonic-gatestatic int
7047c478bdstevel@tonic-gateent_merge(buf, list1, lcnt1, list2, lcnt2)
7057c478bdstevel@tonic-gatestruct	alts_ent buf[];
7067c478bdstevel@tonic-gatestruct	alts_ent list1[];
7077c478bdstevel@tonic-gateint	lcnt1;
7087c478bdstevel@tonic-gatestruct	alts_ent list2[];
7097c478bdstevel@tonic-gateint	lcnt2;
7107c478bdstevel@tonic-gate{
7117c478bdstevel@tonic-gate	int	i;
7127c478bdstevel@tonic-gate	int	j1, j2;
7137c478bdstevel@tonic-gate
7147c478bdstevel@tonic-gate	for (i = 0, j1 = 0, j2 = 0; j1 < lcnt1 && j2 < lcnt2; ) {
715342440ePrasad Singamsetty	    if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
7167c478bdstevel@tonic-gate		j1++;
7177c478bdstevel@tonic-gate		continue;
7187c478bdstevel@tonic-gate	    }
719342440ePrasad Singamsetty	    if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
7207c478bdstevel@tonic-gate		j2++;
7217c478bdstevel@tonic-gate		continue;
7227c478bdstevel@tonic-gate	    }
7237c478bdstevel@tonic-gate	    if (list1[j1].bad_start < list2[j2].bad_start)
7247c478bdstevel@tonic-gate		buf[i++] = list1[j1++];
7257c478bdstevel@tonic-gate	    else
7267c478bdstevel@tonic-gate		buf[i++] = list2[j2++];
7277c478bdstevel@tonic-gate	}
7287c478bdstevel@tonic-gate	for (; j1 < lcnt1; j1++) {
729342440ePrasad Singamsetty	    if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY)
7307c478bdstevel@tonic-gate		continue;
7317c478bdstevel@tonic-gate	    buf[i++] = list1[j1];
7327c478bdstevel@tonic-gate	}
7337c478bdstevel@tonic-gate	for (; j2 < lcnt2; j2++) {
734342440ePrasad Singamsetty	    if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY)
7357c478bdstevel@tonic-gate		continue;
7367c478bdstevel@tonic-gate	    buf[i++] = list2[j2];
7377c478bdstevel@tonic-gate	}
7387c478bdstevel@tonic-gate	return (i);
7397c478bdstevel@tonic-gate}
7407c478bdstevel@tonic-gate
7417c478bdstevel@tonic-gate
7427c478bdstevel@tonic-gate/*
7437c478bdstevel@tonic-gate *	binary search for bad sector in the alternate entry table
7447c478bdstevel@tonic-gate */
7457c478bdstevel@tonic-gatestatic int
7467c478bdstevel@tonic-gateent_bsearch(buf, cnt, key)
7477c478bdstevel@tonic-gatestruct	alts_ent buf[];
7487c478bdstevel@tonic-gateint	cnt;
7497c478bdstevel@tonic-gatestruct	alts_ent *key;
7507c478bdstevel@tonic-gate{
7517c478bdstevel@tonic-gate	int	i;
7527c478bdstevel@tonic-gate	int	ind;
7537c478bdstevel@tonic-gate	int	interval;
7547c478bdstevel@tonic-gate	int	mystatus = -1;
7557c478bdstevel@tonic-gate
7567c478bdstevel@tonic-gate	if (!cnt)
7577c478bdstevel@tonic-gate	    return (mystatus);
7587c478bdstevel@tonic-gate
7597c478bdstevel@tonic-gate	for (i = 1; i <= cnt; i <<= 1)
7607c478bdstevel@tonic-gate	    ind = i;
7617c478bdstevel@tonic-gate
7627c478bdstevel@tonic-gate	for (interval = ind; interval; ) {
7637c478bdstevel@tonic-gate	    if ((key->bad_start >= buf[ind-1].bad_start) &&
7647c478bdstevel@tonic-gate		(key->bad_start <= buf[ind-1].bad_end)) {
7657c478bdstevel@tonic-gate		return (mystatus = ind-1);
7667c478bdstevel@tonic-gate	    } else {
7677c478bdstevel@tonic-gate		interval >>= 1;
7687c478bdstevel@tonic-gate		if (!interval) break;
7697c478bdstevel@tonic-gate		if (key->bad_start < buf[ind-1].bad_start) {
7707c478bdstevel@tonic-gate		    ind = ind - interval;
7717c478bdstevel@tonic-gate		} else {
7727c478bdstevel@tonic-gate/*	if key is larger than the last element then break	*/
7737c478bdstevel@tonic-gate		    if (ind == cnt) break;
7747c478bdstevel@tonic-gate		    if ((ind+interval) <= cnt)
7757c478bdstevel@tonic-gate			ind += interval;
7767c478bdstevel@tonic-gate		}
7777c478bdstevel@tonic-gate	    }
7787c478bdstevel@tonic-gate	}
7797c478bdstevel@tonic-gate	return (mystatus);
7807c478bdstevel@tonic-gate}
7817c478bdstevel@tonic-gate
7827c478bdstevel@tonic-gate/*
7837c478bdstevel@tonic-gate *	check for bad sector in assigned alternate sectors
7847c478bdstevel@tonic-gate */
7857c478bdstevel@tonic-gatestatic int
7867c478bdstevel@tonic-gatechk_bad_altsctr(badsec)
787342440ePrasad Singamsettyblkaddr_t	badsec;
7887c478bdstevel@tonic-gate{
7897c478bdstevel@tonic-gate	int	i;
790342440ePrasad Singamsetty	blkaddr_t	numsec;
7917c478bdstevel@tonic-gate	int	cnt = ap->ap_tblp->alts_ent_used;
7927c478bdstevel@tonic-gate/*
7937c478bdstevel@tonic-gate *	daddr_t intv[3];
7947c478bdstevel@tonic-gate */
7957c478bdstevel@tonic-gate
7967c478bdstevel@tonic-gate	for (i = 0; i < cnt; i++) {
7977c478bdstevel@tonic-gate	    numsec = (ap->ap_entp)[i].bad_end - (ap->ap_entp)[i].bad_start;
7987c478bdstevel@tonic-gate	    if ((badsec >= (ap->ap_entp)[i].good_start) &&
7997c478bdstevel@tonic-gate		(badsec <= ((ap->ap_entp)[i].good_start + numsec))) {
8007c478bdstevel@tonic-gate		(void) fprintf(stderr,
8017c478bdstevel@tonic-gate		"Bad sector %ld is an assigned alternate sector.\n", badsec);
8027c478bdstevel@tonic-gate		return (66);
8037c478bdstevel@tonic-gate/*
8047c478bdstevel@tonic-gate *		if (!numsec) {
8057c478bdstevel@tonic-gate *		    (ap->ap_entp)[i].good_start = 0;
8067c478bdstevel@tonic-gate *		    return (FAILURE);
8077c478bdstevel@tonic-gate *		}
8087c478bdstevel@tonic-gate *		intv[0] = badsec - (ap->ap_entp)[i].good_start;
8097c478bdstevel@tonic-gate *		intv[1] = 1;
8107c478bdstevel@tonic-gate *		intv[2] = (ap->ap_entp)[i].good_start + numsec - badsec;
8117c478bdstevel@tonic-gate */
8127c478bdstevel@tonic-gate	    }
8137c478bdstevel@tonic-gate	}
8147c478bdstevel@tonic-gate/*	the bad sector has already been identified as bad		*/
8157c478bdstevel@tonic-gate	return (SUCCESS);
8167c478bdstevel@tonic-gate
8177c478bdstevel@tonic-gate}
818