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
5cfcc56eMark Logan * Common Development and Distribution License (the "License").
6cfcc56eMark Logan * 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/*
22cfcc56eMark Logan * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
24cfcc56eMark Logan *
25cfcc56eMark Logan * copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
26cfcc56eMark Logan * copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
27cfcc56eMark Logan * 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>
43cfcc56eMark Logan#include <stdlib.h>
44cfcc56eMark Logan#include <unistd.h>
457c478bdstevel@tonic-gate#include <string.h>
467c478bdstevel@tonic-gate#include <sys/vtoc.h>
477c478bdstevel@tonic-gate#include <sys/param.h>
487c478bdstevel@tonic-gate#include <sys/dkio.h>
497c478bdstevel@tonic-gate#include <sys/dktp/altsctr.h>
507c478bdstevel@tonic-gate#include <sys/dktp/fdisk.h>
517c478bdstevel@tonic-gate#include "badsec.h"
527c478bdstevel@tonic-gate
53cfcc56eMark Logan#define	FAILURE	0
547c478bdstevel@tonic-gate#define	SUCCESS	1
557c478bdstevel@tonic-gate
56cfcc56eMark Logan#define	CMD_READ	0
57cfcc56eMark Logan#define	CMD_WRITE	1
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gatestruct	badsec_lst *badsl_chain;
607c478bdstevel@tonic-gateint	badsl_chain_cnt;
617c478bdstevel@tonic-gatestruct	badsec_lst *gbadsl_chain;
627c478bdstevel@tonic-gateint	gbadsl_chain_cnt;
637c478bdstevel@tonic-gate
64cfcc56eMark Loganextern struct dk_geom	dkg;
657c478bdstevel@tonic-gateextern int	alts_fd;
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gatestruct	alts_mempart alts_part = { 0, NULL, 0 };
68cfcc56eMark Loganstruct	alts_mempart *ap = &alts_part;	/* pointer to incore alts tables */
69cfcc56eMark Logan
70cfcc56eMark Loganstatic void read_altsctr(struct extpartition *part, int badok);
71cfcc56eMark Loganstatic void chk_badsec(void);
72cfcc56eMark Loganstatic void init_altsctr(void);
73cfcc56eMark Loganvoid wr_altsctr(void);
74cfcc56eMark Loganstatic void get_badsec(void);
75cfcc56eMark Loganstatic int count_badsec(void);
76cfcc56eMark Loganstatic void gen_alts_ent(void);
77052b6e8bgstatic void assign_altsctr(void);
78cfcc56eMark Loganstatic void expand_map(void);
79cfcc56eMark Loganstatic void compress_map(void);
80cfcc56eMark Loganstatic int altsmap_getbit(blkaddr_t badsec);
81cfcc56eMark Loganstatic blkaddr_t altsmap_alloc(blkaddr_t srt_ind, blkaddr_t end_ind,
82cfcc56eMark Logan	int cnt, int dir);
83052b6e8bgstatic void ent_sort(struct alts_ent buf[], int cnt);
84052b6e8bgstatic void ent_compress(struct alts_ent buf[], int cnt);
85052b6e8bgstatic int  ent_merge(
86052b6e8bg	struct alts_ent buf[],
87052b6e8bg	struct alts_ent list1[],
88052b6e8bg	int    lcnt1,
89052b6e8bg	struct alts_ent list2[],
90052b6e8bg	int    lcnt2);
91052b6e8bgstatic int  ent_bsearch(struct alts_ent buf[], int cnt, struct alts_ent *key);
92cfcc56eMark Loganstatic int  chk_bad_altsctr(blkaddr_t badsec);
93cfcc56eMark Loganint	print_altsec(struct extpartition *part);
94cfcc56eMark Loganint	get_altsctr(int);
95052b6e8bgstatic void print_altsctr(void);
96cfcc56eMark Loganstatic int  absdsk_io(int fd, uint_t srtsec, char *bufp, uint_t len,
97cfcc56eMark Logan	int ioflag);
98052b6e8bg
997c478bdstevel@tonic-gate/*
1007c478bdstevel@tonic-gate * updatebadsec () -- update bad sector/track mapping tables
1017c478bdstevel@tonic-gate */
102052b6e8bgint
103cfcc56eMark Loganupdatebadsec(struct extpartition *part, int init_flag)
1047c478bdstevel@tonic-gate{
1057c478bdstevel@tonic-gate	if (init_flag)
1067c478bdstevel@tonic-gate		ap->ap_flag |= ALTS_ADDPART;
1077c478bdstevel@tonic-gate	get_badsec();
1087c478bdstevel@tonic-gate	read_altsctr(part, 1);
1097c478bdstevel@tonic-gate	ent_sort(ap->ap_gbadp, ap->ap_gbadcnt);
1107c478bdstevel@tonic-gate	ent_compress(ap->ap_gbadp, ap->ap_gbadcnt);
1117c478bdstevel@tonic-gate	gen_alts_ent();
1127c478bdstevel@tonic-gate	compress_map();
113cfcc56eMark Logan	return (SUCCESS);
1147c478bdstevel@tonic-gate}
1157c478bdstevel@tonic-gate
1167c478bdstevel@tonic-gate/*
1177c478bdstevel@tonic-gate * read_altsctr( ptr to alternate sector partition )
1187c478bdstevel@tonic-gate *		-- read the alternate sector partition tables
1197c478bdstevel@tonic-gate */
120052b6e8bgstatic void
121cfcc56eMark Loganread_altsctr(struct extpartition *part, int badok)
1227c478bdstevel@tonic-gate{
1237c478bdstevel@tonic-gate	if (ap->ap_tblp == NULL) {
1247c478bdstevel@tonic-gate/*	    allocate buffer for the alts partition table (sector size)	*/
125cfcc56eMark Logan	    ap->ap_tbl_secsiz = byte_to_secsiz(ALTS_PARTTBL_SIZE, NBPSCTR);
1267c478bdstevel@tonic-gate	    ap->ap_tblp = (struct alts_parttbl *)malloc(ap->ap_tbl_secsiz);
1277c478bdstevel@tonic-gate	    if (ap->ap_tblp == NULL) {
128cfcc56eMark Logan		(void) fprintf(stderr,
129cfcc56eMark Logan			"Unable to malloc alternate partition table.\n");
1307c478bdstevel@tonic-gate		exit(50);
1317c478bdstevel@tonic-gate	    }
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate/*	    allocate buffer for the alts partition map (sector size)	*/
1347c478bdstevel@tonic-gate/*	    buffers include the disk image bit map 			*/
1357c478bdstevel@tonic-gate/*	    and the incore transformed char map				*/
1367c478bdstevel@tonic-gate
137cfcc56eMark Logan	    if ((ap->ap_memmapp = (uchar_t *)malloc(part->p_size)) == NULL) {
138cfcc56eMark Logan		(void) fprintf(stderr,
139cfcc56eMark Logan			"Unable to malloc incore alternate partition map.\n");
1407c478bdstevel@tonic-gate		exit(51);
1417c478bdstevel@tonic-gate	    }
1427c478bdstevel@tonic-gate	    ap->ap_tblp->alts_map_len = (part->p_size + 8 - 1) / 8;
143cfcc56eMark Logan	    ap->ap_map_secsiz = byte_to_secsiz(ap->ap_tblp->alts_map_len,
144cfcc56eMark Logan		NBPSCTR);
1457c478bdstevel@tonic-gate	    ap->ap_map_sectot = ap->ap_map_secsiz / NBPSCTR;
146cfcc56eMark Logan	    if ((ap->ap_mapp = (uchar_t *)malloc(ap->ap_map_secsiz)) == NULL) {
147cfcc56eMark Logan		(void) fprintf(stderr,
148cfcc56eMark Logan		    "Unable to malloc alternate partition map.\n");
1497c478bdstevel@tonic-gate		exit(52);
1507c478bdstevel@tonic-gate	    }
1517c478bdstevel@tonic-gate/*	    clear the buffers to zero					*/
152cfcc56eMark Logan	    (void) memset(ap->ap_memmapp, 0, part->p_size);
153cfcc56eMark Logan	    (void) memset(ap->ap_mapp, 0, ap->ap_map_secsiz);
1547c478bdstevel@tonic-gate	    ap->part = *part;		/* struct copy			*/
155cfcc56eMark Logan
1567c478bdstevel@tonic-gate/*
1577c478bdstevel@tonic-gate *	    if add alternate partition flag is set, then install the partition
1587c478bdstevel@tonic-gate *	    otherwise read the alts partition info from disk
159cfcc56eMark Logan *	    if failed, then assume the first installation
1607c478bdstevel@tonic-gate */
161cfcc56eMark Logan	    if (ap->ap_flag & ALTS_ADDPART) {
162cfcc56eMark Logan		(void) fprintf(stderr,
163cfcc56eMark Logan		    "WARNING: Manually initializing alternate table.\n");
1647c478bdstevel@tonic-gate		init_altsctr();
165cfcc56eMark Logan	    } else {
1667c478bdstevel@tonic-gate	    	if (get_altsctr(badok) == SUCCESS)
167cfcc56eMark Logan		    chk_badsec();
1687c478bdstevel@tonic-gate	    	else
1697c478bdstevel@tonic-gate		    init_altsctr();
1707c478bdstevel@tonic-gate	    }
1717c478bdstevel@tonic-gate	}
1727c478bdstevel@tonic-gate}
1737c478bdstevel@tonic-gate
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate/*
1767c478bdstevel@tonic-gate *	checking duplicate bad sectors or bad sectors in ALTSCTR partition
1777c478bdstevel@tonic-gate */
178052b6e8bgstatic void
179052b6e8bgchk_badsec(void)
1807c478bdstevel@tonic-gate{
181cfcc56eMark Logan	blkaddr_t	badsec;
182cfcc56eMark Logan	blkaddr_t	altsp_srtsec = ap->part.p_start;
183cfcc56eMark Logan	blkaddr_t	altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
1847c478bdstevel@tonic-gate	int	cnt;
1857c478bdstevel@tonic-gate	int	status;
1867c478bdstevel@tonic-gate
187cfcc56eMark Logan	for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
1887c478bdstevel@tonic-gate	    badsec = (ap->ap_gbadp)[cnt].bad_start;
189cfcc56eMark Logan
190cfcc56eMark Logan	    /* if bad sector is within the ATLSCTR partition */
1917c478bdstevel@tonic-gate	    if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
1927c478bdstevel@tonic-gate		if ((ap->ap_memmapp)[badsec - altsp_srtsec] != ALTS_BAD) {
1937c478bdstevel@tonic-gate		    if ((badsec >= altsp_srtsec) && (badsec <= (altsp_srtsec +
1947c478bdstevel@tonic-gate			ap->ap_tbl_secsiz / NBPSCTR - 1))) {
195cfcc56eMark Logan		    	(void) fprintf(stderr,
196cfcc56eMark Logan			    "Alternate partition information table is bad.\n");
1977c478bdstevel@tonic-gate		    	exit(53);
1987c478bdstevel@tonic-gate	    	    }
199cfcc56eMark Logan		    if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_map_base) &&
2007c478bdstevel@tonic-gate			(badsec <= (altsp_srtsec + ap->ap_tblp->alts_map_base +
2017c478bdstevel@tonic-gate			ap->ap_map_sectot - 1))) {
202cfcc56eMark Logan		    	(void) fprintf(stderr,
203cfcc56eMark Logan			    "Alternate partition map is bad.\n");
2047c478bdstevel@tonic-gate		    	exit(54);
2057c478bdstevel@tonic-gate	    	    }
206cfcc56eMark Logan		    if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_ent_base) &&
2077c478bdstevel@tonic-gate			(badsec <= (altsp_srtsec + ap->ap_tblp->alts_ent_base +
2087c478bdstevel@tonic-gate			ap->ap_ent_secsiz / NBPSCTR - 1))) {
209cfcc56eMark Logan		    	(void) fprintf(stderr,
210cfcc56eMark Logan			    "Alternate partition entry table is bad.\n");
2117c478bdstevel@tonic-gate		    	exit(55);
2127c478bdstevel@tonic-gate	    	    }
2137c478bdstevel@tonic-gate		    (ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
214cfcc56eMark Logan		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2157c478bdstevel@tonic-gate		} else {
2167c478bdstevel@tonic-gate		    status = chk_bad_altsctr(badsec);
217cfcc56eMark Logan		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2187c478bdstevel@tonic-gate		}
2197c478bdstevel@tonic-gate	    } else {
2207c478bdstevel@tonic-gate/*
2217c478bdstevel@tonic-gate *		binary search for bad sector in the alts entry table
2227c478bdstevel@tonic-gate */
2237c478bdstevel@tonic-gate		status = ent_bsearch(ap->ap_entp, ap->ap_tblp->alts_ent_used,
224cfcc56eMark Logan					&((ap->ap_gbadp)[cnt]));
2257c478bdstevel@tonic-gate/*
2267c478bdstevel@tonic-gate *		if the bad sector had already been remapped(found in alts_entry)
2277c478bdstevel@tonic-gate *		then ignore the bad sector
2287c478bdstevel@tonic-gate */
2297c478bdstevel@tonic-gate		if (status != -1) {
230cfcc56eMark Logan		    (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2317c478bdstevel@tonic-gate		}
2327c478bdstevel@tonic-gate	    }
2337c478bdstevel@tonic-gate	}
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate}
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate/*
2387c478bdstevel@tonic-gate *	initialize the alternate partition tables
2397c478bdstevel@tonic-gate */
240052b6e8bgstatic void
241052b6e8bginit_altsctr(void)
2427c478bdstevel@tonic-gate{
243cfcc56eMark Logan	blkaddr_t	badsec;
244cfcc56eMark Logan	blkaddr_t	altsp_srtsec = ap->part.p_start;
245cfcc56eMark Logan	blkaddr_t	altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
2467c478bdstevel@tonic-gate	int	cnt;
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate	ap->ap_entp = NULL;
2497c478bdstevel@tonic-gate	ap->ap_ent_secsiz = 0;
2507c478bdstevel@tonic-gate	ap->ap_tblp->alts_sanity = ALTS_SANITY;
251cfcc56eMark Logan	ap->ap_tblp->alts_version = ALTS_VERSION1;
2527c478bdstevel@tonic-gate	ap->ap_tblp->alts_map_len = (ap->part.p_size + 8 - 1) / 8;
2537c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_used = 0;
2547c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_base = 0;
2557c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_end  = 0;
2567c478bdstevel@tonic-gate	ap->ap_tblp->alts_resv_base = ap->part.p_size - 1;
257cfcc56eMark Logan	for (cnt = 0; cnt < 5; cnt++)
258cfcc56eMark Logan	    ap->ap_tblp->alts_pad[cnt] = 0;
2597c478bdstevel@tonic-gate
260cfcc56eMark Logan	for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
2617c478bdstevel@tonic-gate	    badsec = (ap->ap_gbadp)[cnt].bad_start;
2627c478bdstevel@tonic-gate	    if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
2637c478bdstevel@tonic-gate		if (badsec == altsp_srtsec) {
264cfcc56eMark Logan		    (void) fprintf(stderr,
265cfcc56eMark Logan			"First sector of alternate partition is bad.\n");
2667c478bdstevel@tonic-gate		    exit(56);
2677c478bdstevel@tonic-gate	    	}
2687c478bdstevel@tonic-gate		(ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
269cfcc56eMark Logan		(ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
2707c478bdstevel@tonic-gate	    }
2717c478bdstevel@tonic-gate	}
272cfcc56eMark Logan
2737c478bdstevel@tonic-gate/*	allocate the alts_map on disk skipping possible bad sectors	*/
274cfcc56eMark Logan	ap->ap_tblp->alts_map_base =
275cfcc56eMark Logan		altsmap_alloc(ap->ap_tbl_secsiz / NBPSCTR,
2767c478bdstevel@tonic-gate			ap->part.p_size, ap->ap_map_sectot, ALTS_MAP_UP);
2778d44205Toomas Soome	if (ap->ap_tblp->alts_map_base == 0) {
2787c478bdstevel@tonic-gate	    perror("Unable to allocate alternate map on disk: ");
2797c478bdstevel@tonic-gate	    exit(57);
2807c478bdstevel@tonic-gate	}
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate}
2837c478bdstevel@tonic-gate
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate/*
2867c478bdstevel@tonic-gate * 	read the alternate partition tables from disk
2877c478bdstevel@tonic-gate */
2887c478bdstevel@tonic-gateint
2897c478bdstevel@tonic-gateget_altsctr(badok)
2907c478bdstevel@tonic-gateint badok;
2917c478bdstevel@tonic-gate{
2927c478bdstevel@tonic-gate/*	get alts partition table info					*/
293cfcc56eMark Logan	if (absdsk_io(alts_fd, 0, (char *)ap->ap_tblp,
294cfcc56eMark Logan			ap->ap_tbl_secsiz, CMD_READ) == FAILURE) {
2957c478bdstevel@tonic-gate	    if (badok)
2967c478bdstevel@tonic-gate		return(FAILURE);
2977c478bdstevel@tonic-gate	    perror("Unable to read alternate sector partition: ");
2987c478bdstevel@tonic-gate	    exit(58);
2997c478bdstevel@tonic-gate	}
3007c478bdstevel@tonic-gate	if (ap->ap_tblp->alts_sanity != ALTS_SANITY) {
3017c478bdstevel@tonic-gate	    if (badok)
3027c478bdstevel@tonic-gate		return(FAILURE);
303cfcc56eMark Logan	    (void) fprintf(stderr, "Bad alternate sector magic number.\n");
3047c478bdstevel@tonic-gate	    exit(69);
3057c478bdstevel@tonic-gate	}
3067c478bdstevel@tonic-gate
3077c478bdstevel@tonic-gate/*	get the alts map						*/
3087c478bdstevel@tonic-gate	if (absdsk_io(alts_fd, ap->ap_tblp->alts_map_base,
3097c478bdstevel@tonic-gate		(char *)ap->ap_mapp, ap->ap_map_secsiz, CMD_READ) == FAILURE) {
3107c478bdstevel@tonic-gate	    if (badok)
3117c478bdstevel@tonic-gate		return(FAILURE);
3127c478bdstevel@tonic-gate	    perror("Unable to read alternate sector partition map: ");
3137c478bdstevel@tonic-gate	    exit(59);
3147c478bdstevel@tonic-gate	}
3157c478bdstevel@tonic-gate
3167c478bdstevel@tonic-gate/*	transform the disk image bit-map to incore char map		*/
3177c478bdstevel@tonic-gate	expand_map();
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gate	if (ap->ap_tblp->alts_ent_used == 0) {
3207c478bdstevel@tonic-gate	    ap->ap_entp = NULL;
3217c478bdstevel@tonic-gate	    ap->ap_ent_secsiz = 0;
3227c478bdstevel@tonic-gate	} else {
3237c478bdstevel@tonic-gate	    ap->ap_ent_secsiz = byte_to_secsiz(
3247c478bdstevel@tonic-gate			(ap->ap_tblp->alts_ent_used*ALTS_ENT_SIZE),NBPSCTR);
325cfcc56eMark Logan	    if ((ap->ap_entp =
326cfcc56eMark Logan		(struct alts_ent *)malloc(ap->ap_ent_secsiz)) == NULL) {
3277c478bdstevel@tonic-gate		if (badok)
3287c478bdstevel@tonic-gate		    return(FAILURE);
329cfcc56eMark Logan		(void) fprintf(stderr,
330cfcc56eMark Logan		    "Unable to malloc alternate sector entry table.\n");
3317c478bdstevel@tonic-gate		exit(60);
3327c478bdstevel@tonic-gate	    }
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gate	    if (absdsk_io(alts_fd, ap->ap_tblp->alts_ent_base,
335cfcc56eMark Logan	    	(char *)ap->ap_entp, ap->ap_ent_secsiz,
336cfcc56eMark Logan		CMD_READ) ==FAILURE){
3377c478bdstevel@tonic-gate		if (badok)
3387c478bdstevel@tonic-gate		    return(FAILURE);
3397c478bdstevel@tonic-gate		perror("Unable to read alternate sector entry table: ");
3407c478bdstevel@tonic-gate		exit(61);
3417c478bdstevel@tonic-gate	    }
3427c478bdstevel@tonic-gate	}
3437c478bdstevel@tonic-gate	return(SUCCESS);
3447c478bdstevel@tonic-gate}
3457c478bdstevel@tonic-gate
3467c478bdstevel@tonic-gate
3477c478bdstevel@tonic-gate/*
3487c478bdstevel@tonic-gate *	update the new alternate partition tables on disk
3497c478bdstevel@tonic-gate */
350052b6e8bgvoid
351052b6e8bgwr_altsctr(void)
3527c478bdstevel@tonic-gate{
3537c478bdstevel@tonic-gate	if (ap->ap_tblp == NULL)
3547c478bdstevel@tonic-gate		return;
355052b6e8bg	if (absdsk_io(alts_fd, 0, (char *)ap->ap_tblp,
3567c478bdstevel@tonic-gate		ap->ap_tbl_secsiz, CMD_WRITE) == FAILURE) {
3577c478bdstevel@tonic-gate	    perror("Unable to write alternate sector partition: ");
3587c478bdstevel@tonic-gate	    exit(62);
3597c478bdstevel@tonic-gate	}
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate	if (absdsk_io(alts_fd, ap->ap_tblp->alts_map_base,
362052b6e8bg		(char *)ap->ap_mapp, ap->ap_map_secsiz, CMD_WRITE) == FAILURE) {
3637c478bdstevel@tonic-gate	    perror("Unable to write alternate sector partition map: ");
3647c478bdstevel@tonic-gate	    exit(63);
3657c478bdstevel@tonic-gate	}
3667c478bdstevel@tonic-gate
3677c478bdstevel@tonic-gate	if (ap->ap_tblp->alts_ent_used != 0) {
3687c478bdstevel@tonic-gate	    if (absdsk_io(alts_fd, ap->ap_tblp->alts_ent_base,
3697c478bdstevel@tonic-gate	    			(char *)ap->ap_entp, ap->ap_ent_secsiz,
3707c478bdstevel@tonic-gate				CMD_WRITE) == FAILURE) {
3717c478bdstevel@tonic-gate		perror("Unable to write alternate sector entry table: ");
3727c478bdstevel@tonic-gate		exit(64);
3737c478bdstevel@tonic-gate	    }
3747c478bdstevel@tonic-gate	}
3757c478bdstevel@tonic-gate}
3767c478bdstevel@tonic-gate
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate/*
3797c478bdstevel@tonic-gate *	get a list of bad sector
3807c478bdstevel@tonic-gate */
381052b6e8bgstatic void
382052b6e8bgget_badsec(void)
3837c478bdstevel@tonic-gate{
3847c478bdstevel@tonic-gate	int	cnt;
3857c478bdstevel@tonic-gate	struct	badsec_lst *blc_p;
386cfcc56eMark Logan	blkaddr_t	curbad;
387cfcc56eMark Logan	blkaddr_t	maxsec = (blkaddr_t)dkg.dkg_nhead *
388cfcc56eMark Logan					dkg.dkg_ncyl * dkg.dkg_nsect;
3897c478bdstevel@tonic-gate	struct	alts_ent *growbadp;
3907c478bdstevel@tonic-gate	int	i;
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate	cnt = count_badsec();
3937c478bdstevel@tonic-gate	if (!cnt) {
3947c478bdstevel@tonic-gate	    ap->ap_gbadp = NULL;
3957c478bdstevel@tonic-gate	    ap->ap_gbadcnt = 0;
396cfcc56eMark Logan	} else {
397cfcc56eMark Logan	    ap->ap_gbadp = (struct alts_ent *)malloc(cnt*ALTS_ENT_SIZE);
398cfcc56eMark Logan	    (void) memset(ap->ap_gbadp,0,cnt*ALTS_ENT_SIZE);
3997c478bdstevel@tonic-gate
400cfcc56eMark Logan	    for (growbadp = ap->ap_gbadp, cnt=0, blc_p=badsl_chain;
4017c478bdstevel@tonic-gate		blc_p; blc_p=blc_p->bl_nxt) {
4027c478bdstevel@tonic-gate		for (i=0; i<blc_p->bl_cnt; i++) {
4037c478bdstevel@tonic-gate		    curbad = blc_p->bl_sec[i];
404cfcc56eMark Logan		    if (curbad < (blkaddr_t)dkg.dkg_nsect) {
405cfcc56eMark Logan		    	(void) fprintf(stderr,
406cfcc56eMark Logan			    "Ignoring bad sector %ld which is in first"
407cfcc56eMark Logan			    " track of the drive.\n", curbad);
4087c478bdstevel@tonic-gate		    	continue;
4097c478bdstevel@tonic-gate		    }
4107c478bdstevel@tonic-gate		    if (curbad >= maxsec) {
411cfcc56eMark Logan		    	(void) fprintf(stderr,
412cfcc56eMark Logan			    "Ignoring bad sector %ld which is past"
413cfcc56eMark Logan			    " the end of the drive.\n", curbad);
4147c478bdstevel@tonic-gate		    	continue;
4157c478bdstevel@tonic-gate		    }
4167c478bdstevel@tonic-gate		    growbadp[cnt].bad_start = curbad;
4177c478bdstevel@tonic-gate		    growbadp[cnt].bad_end = curbad;
4187c478bdstevel@tonic-gate		    cnt++;
4197c478bdstevel@tonic-gate		}
4207c478bdstevel@tonic-gate	    }
4217c478bdstevel@tonic-gate	}
4227c478bdstevel@tonic-gate	ap->ap_gbadcnt = cnt;
4237c478bdstevel@tonic-gate}
4247c478bdstevel@tonic-gate
4257c478bdstevel@tonic-gate/*
4267c478bdstevel@tonic-gate *	count number of bad sector on list
4277c478bdstevel@tonic-gate *	merging the bad sector list from surface analysis and the
4287c478bdstevel@tonic-gate *	one given through the command line
4297c478bdstevel@tonic-gate */
430052b6e8bgstatic int
431052b6e8bgcount_badsec(void)
4327c478bdstevel@tonic-gate{
4337c478bdstevel@tonic-gate
4347c478bdstevel@tonic-gate	struct badsec_lst *blc_p;
4357c478bdstevel@tonic-gate
4367c478bdstevel@tonic-gate	if (!badsl_chain)
4377c478bdstevel@tonic-gate		badsl_chain = gbadsl_chain;
4387c478bdstevel@tonic-gate	else {
439cfcc56eMark Logan		for (blc_p = badsl_chain; blc_p->bl_nxt; blc_p = blc_p->bl_nxt)
4407c478bdstevel@tonic-gate			;
4417c478bdstevel@tonic-gate		blc_p->bl_nxt = gbadsl_chain;
4427c478bdstevel@tonic-gate	}
4437c478bdstevel@tonic-gate
4447c478bdstevel@tonic-gate	badsl_chain_cnt += gbadsl_chain_cnt;
4457c478bdstevel@tonic-gate	return(badsl_chain_cnt);
4467c478bdstevel@tonic-gate}
4477c478bdstevel@tonic-gate
4487c478bdstevel@tonic-gate
4497c478bdstevel@tonic-gate/*
4507c478bdstevel@tonic-gate *	generate alternate entry table by merging the existing and
4517c478bdstevel@tonic-gate *	the new entry list.
4527c478bdstevel@tonic-gate */
453052b6e8bgstatic void
454052b6e8bggen_alts_ent(void)
455052b6e8bg{
456cfcc56eMark Logan	uint_t	ent_used;
4577c478bdstevel@tonic-gate	struct	alts_ent *entp;
4587c478bdstevel@tonic-gate
4597c478bdstevel@tonic-gate	if (ap->ap_gbadcnt == 0)
4607c478bdstevel@tonic-gate	    return;
4617c478bdstevel@tonic-gate
4627c478bdstevel@tonic-gate	ent_used = ap->ap_tblp->alts_ent_used + ap->ap_gbadcnt;
4637c478bdstevel@tonic-gate	ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE,NBPSCTR);
4647c478bdstevel@tonic-gate	entp=(struct alts_ent *) malloc (ap->ap_ent_secsiz);
4657c478bdstevel@tonic-gate	ent_used = ent_merge(entp, ap->ap_entp, ap->ap_tblp->alts_ent_used,
4667c478bdstevel@tonic-gate			     ap->ap_gbadp, ap->ap_gbadcnt);
467cfcc56eMark Logan	if (ap->ap_entp)
4687c478bdstevel@tonic-gate	    free(ap->ap_entp);
469cfcc56eMark Logan	if (ap->ap_gbadp)
4707c478bdstevel@tonic-gate	    free(ap->ap_gbadp);
4717c478bdstevel@tonic-gate	ap->ap_entp = entp;
472cfcc56eMark Logan	ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE, NBPSCTR);
4737c478bdstevel@tonic-gate	ap->ap_tblp->alts_ent_used = ent_used;
4747c478bdstevel@tonic-gate	ap->ap_gbadp = NULL;
4757c478bdstevel@tonic-gate	ap->ap_gbadcnt = 0;
4767c478bdstevel@tonic-gate
4777c478bdstevel@tonic-gate/*	assign alternate sectors to the bad sectors			*/
4787c478bdstevel@tonic-gate	assign_altsctr();
4797c478bdstevel@tonic-gate
4807c478bdstevel@tonic-gate/*	allocate the alts_entry on disk skipping possible bad sectors	*/
481cfcc56eMark Logan	ap->ap_tblp->alts_ent_base =
4827c478bdstevel@tonic-gate		altsmap_alloc(ap->ap_tblp->alts_map_base + ap->ap_map_sectot,
483cfcc56eMark Logan			ap->part.p_size,
4847c478bdstevel@tonic-gate			ap->ap_ent_secsiz / NBPSCTR, ALTS_MAP_UP);
4858d44205Toomas Soome	if (ap->ap_tblp->alts_ent_base == 0) {
4867c478bdstevel@tonic-gate	    perror("Unable to allocate alternate entry table on disk: ");
4877c478bdstevel@tonic-gate	    exit(65);
4887c478bdstevel@tonic-gate	}
4897c478bdstevel@tonic-gate
490cfcc56eMark Logan	ap->ap_tblp->alts_ent_end = ap->ap_tblp->alts_ent_base +
491cfcc56eMark Logan			(ap->ap_ent_secsiz / NBPSCTR) - 1;
4927c478bdstevel@tonic-gate}
4937c478bdstevel@tonic-gate
4947c478bdstevel@tonic-gate
4957c478bdstevel@tonic-gate/*
4967c478bdstevel@tonic-gate *	assign alternate sectors for bad sector mapping
4977c478bdstevel@tonic-gate */
498052b6e8bgstatic void
499052b6e8bgassign_altsctr(void)
5007c478bdstevel@tonic-gate{
501cfcc56eMark Logan	uint_t	i;
502cfcc56eMark Logan	uint_t	j;
503cfcc56eMark Logan	blkaddr_t	alts_ind;
504cfcc56eMark Logan	uint_t	cluster;
5057c478bdstevel@tonic-gate
506cfcc56eMark Logan	for (i = 0; i < ap->ap_tblp->alts_ent_used; i++) {
507cfcc56eMark Logan	    if ((ap->ap_entp)[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
5087c478bdstevel@tonic-gate		continue;
5097c478bdstevel@tonic-gate	    if ((ap->ap_entp)[i].good_start != 0)
5107c478bdstevel@tonic-gate		continue;
511cfcc56eMark Logan	    cluster = (ap->ap_entp)[i].bad_end-(ap->ap_entp)[i].bad_start +1;
512cfcc56eMark Logan	    alts_ind =
513cfcc56eMark Logan		altsmap_alloc(ap->part.p_size-1, ap->ap_tblp->alts_map_base +
5147c478bdstevel@tonic-gate			ap->ap_map_sectot - 1, cluster, ALTS_MAP_DOWN);
5158d44205Toomas Soome	    if (alts_ind == 0) {
516cfcc56eMark Logan	    	(void) fprintf(stderr,
517cfcc56eMark Logan		    "Unable to allocate alternates for bad starting"
518cfcc56eMark Logan		    " sector %u.\n", (ap->ap_entp)[i].bad_start);
5197c478bdstevel@tonic-gate	    	exit(65);
5207c478bdstevel@tonic-gate	    }
5217c478bdstevel@tonic-gate	    alts_ind = alts_ind - cluster + 1;
522cfcc56eMark Logan	    (ap->ap_entp)[i].good_start = alts_ind + ap->part.p_start;
523cfcc56eMark Logan	    for (j = 0; j < cluster; j++) {
5247c478bdstevel@tonic-gate		(ap->ap_memmapp)[alts_ind+j] = ALTS_BAD;
5257c478bdstevel@tonic-gate	    }
5267c478bdstevel@tonic-gate
5277c478bdstevel@tonic-gate	}
5287c478bdstevel@tonic-gate
5297c478bdstevel@tonic-gate}
5307c478bdstevel@tonic-gate
5317c478bdstevel@tonic-gate/*
5327c478bdstevel@tonic-gate *	transform the disk image alts bit map to incore char map
5337c478bdstevel@tonic-gate */
534052b6e8bgstatic void
535052b6e8bgexpand_map(void)
5367c478bdstevel@tonic-gate{
5377c478bdstevel@tonic-gate	int 	i;
5387c478bdstevel@tonic-gate
539cfcc56eMark Logan	for (i = 0; i < ap->part.p_size; i++) {
5407c478bdstevel@tonic-gate	    (ap->ap_memmapp)[i] = altsmap_getbit(i);
5417c478bdstevel@tonic-gate	}
5427c478bdstevel@tonic-gate}
5437c478bdstevel@tonic-gate
5447c478bdstevel@tonic-gate/*
5457c478bdstevel@tonic-gate *	transform the incore alts char map to the disk image bit map
5467c478bdstevel@tonic-gate */
547052b6e8bgstatic void
548052b6e8bgcompress_map(void)
5497c478bdstevel@tonic-gate{
5507c478bdstevel@tonic-gate
5517c478bdstevel@tonic-gate	int 	i;
5527c478bdstevel@tonic-gate	int	bytesz;
5537c478bdstevel@tonic-gate	char	mask = 0;
554cfcc56eMark Logan	int	maplen = 0;
5557c478bdstevel@tonic-gate
556cfcc56eMark Logan	for (i = 0, bytesz = 7; i < ap->part.p_size; i++) {
5577c478bdstevel@tonic-gate	    mask |= ((ap->ap_memmapp)[i] << bytesz--);
5587c478bdstevel@tonic-gate	    if (bytesz < 0) {
5597c478bdstevel@tonic-gate		(ap->ap_mapp)[maplen++] = mask;
5607c478bdstevel@tonic-gate		bytesz = 7;
5617c478bdstevel@tonic-gate		mask = 0;
5627c478bdstevel@tonic-gate	    }
5637c478bdstevel@tonic-gate	}
5647c478bdstevel@tonic-gate/*
565cfcc56eMark Logan *	if partition size != multiple number of bytes
566cfcc56eMark Logan *	then record the last partial byte
5677c478bdstevel@tonic-gate */
5687c478bdstevel@tonic-gate	if (bytesz != 7)
5697c478bdstevel@tonic-gate	    (ap->ap_mapp)[maplen] = mask;
570cfcc56eMark Logan
5717c478bdstevel@tonic-gate}
5727c478bdstevel@tonic-gate
5737c478bdstevel@tonic-gate/*
5747c478bdstevel@tonic-gate *	given a bad sector number, search in the alts bit map
5757c478bdstevel@tonic-gate *	and identify the sector as good or bad
5767c478bdstevel@tonic-gate */
577052b6e8bgstatic int
578cfcc56eMark Loganaltsmap_getbit(blkaddr_t badsec)
5797c478bdstevel@tonic-gate{
580cfcc56eMark Logan	uint_t	slot = badsec / 8;
581cfcc56eMark Logan	uint_t	field = badsec % 8;
582cfcc56eMark Logan	uchar_t	mask;
5837c478bdstevel@tonic-gate
584cfcc56eMark Logan	mask = ALTS_BAD<<7;
5857c478bdstevel@tonic-gate	mask >>= field;
5867c478bdstevel@tonic-gate	if ((ap->ap_mapp)[slot] & mask)
5877c478bdstevel@tonic-gate	     return(ALTS_BAD);
5887c478bdstevel@tonic-gate	return(ALTS_GOOD);
5897c478bdstevel@tonic-gate}
5907c478bdstevel@tonic-gate
5917c478bdstevel@tonic-gate
5927c478bdstevel@tonic-gate/*
5937c478bdstevel@tonic-gate *	allocate a range of sectors from the alternate partition
5947c478bdstevel@tonic-gate */
595cfcc56eMark Loganstatic blkaddr_t
596cfcc56eMark Loganaltsmap_alloc(blkaddr_t srt_ind, blkaddr_t end_ind, int cnt, int dir)
5977c478bdstevel@tonic-gate{
598cfcc56eMark Logan	blkaddr_t	i;
599cfcc56eMark Logan	blkaddr_t	total;
600cfcc56eMark Logan	blkaddr_t	first_ind;
6017c478bdstevel@tonic-gate
602cfcc56eMark Logan	for (i = srt_ind, first_ind = srt_ind, total = 0;
603cfcc56eMark Logan	    i != end_ind; i += dir) {
6047c478bdstevel@tonic-gate	    if ((ap->ap_memmapp)[i] == ALTS_BAD) {
6057c478bdstevel@tonic-gate		total = 0;
6067c478bdstevel@tonic-gate		first_ind = i + dir;
6077c478bdstevel@tonic-gate		continue;
6087c478bdstevel@tonic-gate	    }
6097c478bdstevel@tonic-gate	    total++;
6107c478bdstevel@tonic-gate	    if (total == cnt)
6117c478bdstevel@tonic-gate		return(first_ind);
6127c478bdstevel@tonic-gate
6137c478bdstevel@tonic-gate	}
614052b6e8bg	return(0);
6157c478bdstevel@tonic-gate}
6167c478bdstevel@tonic-gate
6177c478bdstevel@tonic-gate
6187c478bdstevel@tonic-gate
6197c478bdstevel@tonic-gate/*
6207c478bdstevel@tonic-gate * 	bubble sort the entry table into ascending order
6217c478bdstevel@tonic-gate */
622052b6e8bgstatic void
623052b6e8bgent_sort(struct alts_ent buf[], int cnt)
6247c478bdstevel@tonic-gate{
6257c478bdstevel@tonic-gatestruct	alts_ent temp;
6267c478bdstevel@tonic-gateint	flag;
627cfcc56eMark Loganint	i, j;
6287c478bdstevel@tonic-gate
629cfcc56eMark Logan	for (i = 0; i < cnt-1; i++) {
6307c478bdstevel@tonic-gate	    temp = buf[cnt-1];
6317c478bdstevel@tonic-gate	    flag = 1;
632cfcc56eMark Logan
633cfcc56eMark Logan	    for (j = cnt-1; j > i; j--) {
6347c478bdstevel@tonic-gate		if (buf[j-1].bad_start < temp.bad_start) {
6357c478bdstevel@tonic-gate		    buf[j] = temp;
6367c478bdstevel@tonic-gate		    temp = buf[j-1];
6377c478bdstevel@tonic-gate		} else {
6387c478bdstevel@tonic-gate		    buf[j] = buf[j-1];
6397c478bdstevel@tonic-gate		    flag = 0;
6407c478bdstevel@tonic-gate		}
6417c478bdstevel@tonic-gate	    }
6427c478bdstevel@tonic-gate	    buf[i] = temp;
6437c478bdstevel@tonic-gate	    if (flag) break;
6447c478bdstevel@tonic-gate	}
6457c478bdstevel@tonic-gate
6467c478bdstevel@tonic-gate}
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate
6497c478bdstevel@tonic-gate/*
650cfcc56eMark Logan *	compress all the contiguous bad sectors into a single entry
6517c478bdstevel@tonic-gate *	in the entry table. The entry table must be sorted into ascending
6527c478bdstevel@tonic-gate *	before the compression.
6537c478bdstevel@tonic-gate */
654052b6e8bgstatic void
655052b6e8bgent_compress(struct alts_ent buf[], int cnt)
6567c478bdstevel@tonic-gate{
6577c478bdstevel@tonic-gateint	keyp;
6587c478bdstevel@tonic-gateint	movp;
6597c478bdstevel@tonic-gateint	i;
6607c478bdstevel@tonic-gate
661cfcc56eMark Logan	for (i = 0; i < cnt; i++) {
662cfcc56eMark Logan	    if (buf[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
6637c478bdstevel@tonic-gate		continue;
664cfcc56eMark Logan	    for (keyp = i, movp = i+1; movp < cnt; movp++) {
665cfcc56eMark Logan		if (buf[movp].bad_start == (uint32_t)ALTS_ENT_EMPTY)
6667c478bdstevel@tonic-gate			continue;
6677c478bdstevel@tonic-gate		if (buf[keyp].bad_end+1 != buf[movp].bad_start)
6687c478bdstevel@tonic-gate		    break;
6697c478bdstevel@tonic-gate		buf[keyp].bad_end++;
670cfcc56eMark Logan		buf[movp].bad_start = (uint32_t)ALTS_ENT_EMPTY;
6717c478bdstevel@tonic-gate	    }
6727c478bdstevel@tonic-gate	    if (movp == cnt) break;
6737c478bdstevel@tonic-gate	}
6747c478bdstevel@tonic-gate}
6757c478bdstevel@tonic-gate
6767c478bdstevel@tonic-gate
6777c478bdstevel@tonic-gate/*
6787c478bdstevel@tonic-gate *	merging two entry tables into a single table. In addition,
6797c478bdstevel@tonic-gate *	all empty slots in the entry table will be removed.
6807c478bdstevel@tonic-gate */
681052b6e8bgstatic int
682052b6e8bgent_merge(
683052b6e8bg	struct alts_ent buf[],
684052b6e8bg	struct alts_ent list1[],
685052b6e8bg	int    lcnt1,
686052b6e8bg	struct alts_ent list2[],
687052b6e8bg	int    lcnt2)
6887c478bdstevel@tonic-gate{
6897c478bdstevel@tonic-gate	int	i;
690cfcc56eMark Logan	int	j1, j2;
6917c478bdstevel@tonic-gate
692cfcc56eMark Logan	for (i = 0, j1 = 0, j2 = 0; j1 < lcnt1 && j2 < lcnt2; ) {
693cfcc56eMark Logan	    if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
6947c478bdstevel@tonic-gate		j1++;
695cfcc56eMark Logan		continue;
6967c478bdstevel@tonic-gate	    }
697cfcc56eMark Logan	    if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
6987c478bdstevel@tonic-gate		j2++;
6997c478bdstevel@tonic-gate		continue;
7007c478bdstevel@tonic-gate	    }
7017c478bdstevel@tonic-gate	    if (list1[j1].bad_start < list2[j2].bad_start)
7027c478bdstevel@tonic-gate		buf[i++] = list1[j1++];
703cfcc56eMark Logan	    else
7047c478bdstevel@tonic-gate		buf[i++] = list2[j2++];
7057c478bdstevel@tonic-gate	}
706cfcc56eMark Logan	for (; j1 < lcnt1; j1++) {
707cfcc56eMark Logan	    if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY)
708cfcc56eMark Logan		continue;
7097c478bdstevel@tonic-gate	    buf[i++] = list1[j1];
7107c478bdstevel@tonic-gate	}
711cfcc56eMark Logan	for (; j2 < lcnt2; j2++) {
712cfcc56eMark Logan	    if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY)
713cfcc56eMark Logan		continue;
7147c478bdstevel@tonic-gate	    buf[i++] = list2[j2];
7157c478bdstevel@tonic-gate	}
7167c478bdstevel@tonic-gate	return (i);
7177c478bdstevel@tonic-gate}
7187c478bdstevel@tonic-gate
7197c478bdstevel@tonic-gate
7207c478bdstevel@tonic-gate/*
7217c478bdstevel@tonic-gate *	binary search for bad sector in the alternate entry table
7227c478bdstevel@tonic-gate */
723052b6e8bgstatic int
724052b6e8bgent_bsearch(struct alts_ent buf[], int cnt, struct alts_ent *key)
7257c478bdstevel@tonic-gate{
7267c478bdstevel@tonic-gate	int	i;
7277c478bdstevel@tonic-gate	int	ind;
7287c478bdstevel@tonic-gate	int	interval;
7297c478bdstevel@tonic-gate	int	mystatus = -1;
7307c478bdstevel@tonic-gate
731cfcc56eMark Logan	if (!cnt)
732cfcc56eMark Logan		return(mystatus);
7337c478bdstevel@tonic-gate
734cfcc56eMark Logan	for (i = 1; i <= cnt; i <<= 1)
735cfcc56eMark Logan	    ind = i;
7367c478bdstevel@tonic-gate
737cfcc56eMark Logan	for (interval = ind; interval; ) {
7387c478bdstevel@tonic-gate/*
7397c478bdstevel@tonic-gate	    printf("ind= %d, intv= %d; ",ind, interval);
7407c478bdstevel@tonic-gate*/
741cfcc56eMark Logan	    if ((key->bad_start >= buf[ind-1].bad_start) &&
7427c478bdstevel@tonic-gate		(key->bad_start <= buf[ind-1].bad_end)) {
743cfcc56eMark Logan		return(mystatus = ind-1);
7447c478bdstevel@tonic-gate	    } else {
745cfcc56eMark Logan		interval >>= 1;
7467c478bdstevel@tonic-gate		if (!interval) break;
7477c478bdstevel@tonic-gate		if (key->bad_start < buf[ind-1].bad_start) {
7487c478bdstevel@tonic-gate		    ind = ind - interval;
7497c478bdstevel@tonic-gate		} else {
7507c478bdstevel@tonic-gate /* 		    if key is larger than the last element then break	*/
7517c478bdstevel@tonic-gate		    if (ind == cnt) break;
7527c478bdstevel@tonic-gate		    if ((ind+interval) <= cnt)
7537c478bdstevel@tonic-gate		    	ind += interval;
7547c478bdstevel@tonic-gate		}
7557c478bdstevel@tonic-gate	    }
7567c478bdstevel@tonic-gate	}
7577c478bdstevel@tonic-gate	return(mystatus);
7587c478bdstevel@tonic-gate}
7597c478bdstevel@tonic-gate
7607c478bdstevel@tonic-gate/*
7617c478bdstevel@tonic-gate *	check for bad sector in assigned alternate sectors
7627c478bdstevel@tonic-gate */
763052b6e8bgstatic int
764cfcc56eMark Loganchk_bad_altsctr(blkaddr_t badsec)
7657c478bdstevel@tonic-gate{
7667c478bdstevel@tonic-gate	int	i;
767cfcc56eMark Logan	blkaddr_t	numsec;
7687c478bdstevel@tonic-gate	int	cnt = ap->ap_tblp->alts_ent_used;
769cfcc56eMark Logan/*
770cfcc56eMark Logan *	blkaddr_t intv[3];
771cfcc56eMark Logan */
7727c478bdstevel@tonic-gate
773cfcc56eMark Logan	for (i = 0; i < cnt; i++) {
7747c478bdstevel@tonic-gate	    numsec = (ap->ap_entp)[i].bad_end - (ap->ap_entp)[i].bad_start;
7757c478bdstevel@tonic-gate	    if ((badsec >= (ap->ap_entp)[i].good_start) &&
7767c478bdstevel@tonic-gate		(badsec <= ((ap->ap_entp)[i].good_start + numsec))) {
777cfcc56eMark Logan		(void) fprintf(stderr, "Bad sector %ld is an assigned"
778cfcc56eMark Logan		    " alternate sector.\n", badsec);
7797c478bdstevel@tonic-gate		exit(66);
7807c478bdstevel@tonic-gate/*
781cfcc56eMark Logan *		if (!numsec) {
782cfcc56eMark Logan *		    (ap->ap_entp)[i].good_start = 0;
783cfcc56eMark Logan *		    return (FAILURE);
784cfcc56eMark Logan *		}
785cfcc56eMark Logan *		intv[0] = badsec - (ap->ap_entp)[i].good_start;
786cfcc56eMark Logan *		intv[1] = 1;
787cfcc56eMark Logan *		intv[2] = (ap->ap_entp)[i].good_start + numsec - badsec;
788cfcc56eMark Logan */
789cfcc56eMark Logan	    }
7907c478bdstevel@tonic-gate	}
791cfcc56eMark Logan	/* the bad sector has already been identified as bad */
792cfcc56eMark Logan	return(SUCCESS);
7937c478bdstevel@tonic-gate}
7947c478bdstevel@tonic-gate
7957c478bdstevel@tonic-gate
7967c478bdstevel@tonic-gate/*
7977c478bdstevel@tonic-gate * print_altsec () -- print alternate sector information
7987c478bdstevel@tonic-gate */
799052b6e8bgint
800cfcc56eMark Loganprint_altsec(struct extpartition *part)
8017c478bdstevel@tonic-gate{
8027c478bdstevel@tonic-gate	ap->ap_tblp = NULL;
8037c478bdstevel@tonic-gate	ap->ap_flag &= ~ALTS_ADDPART;
8047c478bdstevel@tonic-gate	read_altsctr(part, 0);
8057c478bdstevel@tonic-gate	print_altsctr();
8067c478bdstevel@tonic-gate	return(SUCCESS);
8077c478bdstevel@tonic-gate}
8087c478bdstevel@tonic-gate
809052b6e8bgstatic void
810052b6e8bgprint_altsctr(void)
8117c478bdstevel@tonic-gate{
8127c478bdstevel@tonic-gate	int	i;
8137c478bdstevel@tonic-gate	int	totalloc;
8147c478bdstevel@tonic-gate	int	avail;
8157c478bdstevel@tonic-gate
8167c478bdstevel@tonic-gate/*	find # of available alternate sectors				*/
8177c478bdstevel@tonic-gate	for (i=0, totalloc=0; i<ap->part.p_size; i++) {
8187c478bdstevel@tonic-gate	    if ((ap->ap_memmapp)[i])
8197c478bdstevel@tonic-gate		totalloc++;
8207c478bdstevel@tonic-gate	}
8217c478bdstevel@tonic-gate/*
8227c478bdstevel@tonic-gate *	available = size of partition - allocated sectors/bad sectors
8237c478bdstevel@tonic-gate *		    - partition table - partition map
8247c478bdstevel@tonic-gate *		    - entry table
8257c478bdstevel@tonic-gate */
8267c478bdstevel@tonic-gate	avail = ap->part.p_size - totalloc;
827cfcc56eMark Logan	avail = avail - (ap->ap_tbl_secsiz/NBPSCTR)
8287c478bdstevel@tonic-gate		- ap->ap_map_sectot;
8297c478bdstevel@tonic-gate	avail = avail-(ap->ap_tblp->alts_ent_end -ap->ap_tblp->alts_ent_base+1);
8307c478bdstevel@tonic-gate	if (avail < 0) avail = 0;
8317c478bdstevel@tonic-gate
832cfcc56eMark Logan	(void) printf("\nALTERNATE SECTOR/TRACK MAPPING TABLE:\n");
833cfcc56eMark Logan	(void) printf("\nBad Sector Start\tAlternate Sector Start\t\tCount\n");
8347c478bdstevel@tonic-gate
8357c478bdstevel@tonic-gate	for (i=0; i<ap->ap_tblp->alts_ent_used; i++) {
836cfcc56eMark Logan	     (void) printf("\t%u\t     ->\t\t%u\t\t\t   %u\n",
8377c478bdstevel@tonic-gate		(ap->ap_entp)[i].bad_start,
8387c478bdstevel@tonic-gate		(ap->ap_entp)[i].good_start,
8397c478bdstevel@tonic-gate		((ap->ap_entp)[i].bad_end - (ap->ap_entp)[i].bad_start + 1));
8407c478bdstevel@tonic-gate	}
841cfcc56eMark Logan	(void) printf("\n      %d alternate sector(s) left for allocation.\n",
842cfcc56eMark Logan	    avail);
8437c478bdstevel@tonic-gate
8447c478bdstevel@tonic-gate}
8457c478bdstevel@tonic-gate
846052b6e8bgstatic int
847cfcc56eMark Loganabsdsk_io(int fd, uint_t srtsec, char *bufp, uint_t len, int ioflag)
8487c478bdstevel@tonic-gate{
8497c478bdstevel@tonic-gate	int	rc;
8507c478bdstevel@tonic-gate
851cfcc56eMark Logan	if (llseek (fd, (offset_t)srtsec * NBPSCTR, SEEK_SET) == -1)
8527c478bdstevel@tonic-gate		return(FAILURE);
8537c478bdstevel@tonic-gate	switch (ioflag)
8547c478bdstevel@tonic-gate	{
8557c478bdstevel@tonic-gate	case CMD_READ:
8567c478bdstevel@tonic-gate		rc = read (fd, bufp, len);
8577c478bdstevel@tonic-gate		break;
8587c478bdstevel@tonic-gate	case CMD_WRITE:
8597c478bdstevel@tonic-gate		rc = write (fd, bufp, len);
8607c478bdstevel@tonic-gate		break;
8617c478bdstevel@tonic-gate	default:
8627c478bdstevel@tonic-gate		break;
8637c478bdstevel@tonic-gate	}
8647c478bdstevel@tonic-gate	if (rc == -1)
8657c478bdstevel@tonic-gate		return(FAILURE);
8667c478bdstevel@tonic-gate	return(SUCCESS);
8677c478bdstevel@tonic-gate}
868