xref: /netbsd-src/sys/arch/atari/stand/tostools/libtos/ahdi.c (revision fa1cc128972346d89f5633ec30779004c106a0e2)
1*fa1cc128Sleo /*	$NetBSD: ahdi.c,v 1.1 2002/02/24 20:51:07 leo Exp $	*/
2*fa1cc128Sleo 
3*fa1cc128Sleo /*
4*fa1cc128Sleo  * Copyright (c) 1996 Leo Weppelman, Waldi Ravens.
5*fa1cc128Sleo  * All rights reserved.
6*fa1cc128Sleo  *
7*fa1cc128Sleo  * Redistribution and use in source and binary forms, with or without
8*fa1cc128Sleo  * modification, are permitted provided that the following conditions
9*fa1cc128Sleo  * are met:
10*fa1cc128Sleo  * 1. Redistributions of source code must retain the above copyright
11*fa1cc128Sleo  *    notice, this list of conditions and the following disclaimer.
12*fa1cc128Sleo  * 2. Redistributions in binary form must reproduce the above copyright
13*fa1cc128Sleo  *    notice, this list of conditions and the following disclaimer in the
14*fa1cc128Sleo  *    documentation and/or other materials provided with the distribution.
15*fa1cc128Sleo  * 3. All advertising materials mentioning features or use of this software
16*fa1cc128Sleo  *    must display the following acknowledgement:
17*fa1cc128Sleo  *      This product includes software developed by
18*fa1cc128Sleo  *			Leo Weppelman and Waldi Ravens.
19*fa1cc128Sleo  * 4. The name of the author may not be used to endorse or promote products
20*fa1cc128Sleo  *    derived from this software without specific prior written permission.
21*fa1cc128Sleo  *
22*fa1cc128Sleo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23*fa1cc128Sleo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24*fa1cc128Sleo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25*fa1cc128Sleo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26*fa1cc128Sleo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27*fa1cc128Sleo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*fa1cc128Sleo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29*fa1cc128Sleo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30*fa1cc128Sleo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31*fa1cc128Sleo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*fa1cc128Sleo  */
33*fa1cc128Sleo 
34*fa1cc128Sleo #include <sys/types.h>
35*fa1cc128Sleo #include <stdlib.h>
36*fa1cc128Sleo #include <string.h>
37*fa1cc128Sleo #include <ctype.h>
38*fa1cc128Sleo #include <stdio.h>
39*fa1cc128Sleo #include <xhdi.h>
40*fa1cc128Sleo #include "libtos.h"
41*fa1cc128Sleo #include "diskio.h"
42*fa1cc128Sleo #include "ahdilbl.h"
43*fa1cc128Sleo 
44*fa1cc128Sleo u_int
ahdi_getparts(dd,ptable,rsec,esec)45*fa1cc128Sleo ahdi_getparts(dd, ptable, rsec, esec)
46*fa1cc128Sleo 	disk_t			*dd;
47*fa1cc128Sleo 	ptable_t		*ptable;
48*fa1cc128Sleo 	u_int			rsec,
49*fa1cc128Sleo 				esec;
50*fa1cc128Sleo {
51*fa1cc128Sleo 	struct ahdi_part	*part, *end;
52*fa1cc128Sleo 	struct ahdi_root	*root;
53*fa1cc128Sleo 	u_int			rv;
54*fa1cc128Sleo 
55*fa1cc128Sleo 	root = disk_read(dd, rsec, 1);
56*fa1cc128Sleo 	if (!root) {
57*fa1cc128Sleo 		rv = rsec + (rsec == 0);
58*fa1cc128Sleo 		goto done;
59*fa1cc128Sleo 	}
60*fa1cc128Sleo 
61*fa1cc128Sleo 	if (rsec == AHDI_BBLOCK)
62*fa1cc128Sleo 		end = &root->ar_parts[AHDI_MAXRPD];
63*fa1cc128Sleo 	else end = &root->ar_parts[AHDI_MAXARPD];
64*fa1cc128Sleo 	for (part = root->ar_parts; part < end; ++part) {
65*fa1cc128Sleo 		u_int	id = *((u_int32_t *)&part->ap_flg);
66*fa1cc128Sleo 		if (!(id & 0x01000000))
67*fa1cc128Sleo 			continue;
68*fa1cc128Sleo 		if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
69*fa1cc128Sleo 			u_int	offs = part->ap_st + esec;
70*fa1cc128Sleo 			rv = ahdi_getparts(dd, ptable, offs,
71*fa1cc128Sleo 					esec == AHDI_BBLOCK ? offs : esec);
72*fa1cc128Sleo 			if (rv)
73*fa1cc128Sleo 				goto done;
74*fa1cc128Sleo 		} else {
75*fa1cc128Sleo 			part_t	*p;
76*fa1cc128Sleo 			u_int	i = ++ptable->nparts;
77*fa1cc128Sleo 			ptable->parts = xrealloc(ptable->parts,
78*fa1cc128Sleo 						i * sizeof *ptable->parts);
79*fa1cc128Sleo 			p = &ptable->parts[--i];
80*fa1cc128Sleo 			*((u_int32_t *)&p->id) = id << 8;
81*fa1cc128Sleo 			p->start = part->ap_st + rsec;
82*fa1cc128Sleo 			p->end   = p->start + part->ap_size - 1;
83*fa1cc128Sleo 			p->rsec  = rsec;
84*fa1cc128Sleo 			p->rent  = part - root->ar_parts;
85*fa1cc128Sleo 			p->mod   = 0;
86*fa1cc128Sleo 		}
87*fa1cc128Sleo 	}
88*fa1cc128Sleo 	rv = 0;
89*fa1cc128Sleo done:
90*fa1cc128Sleo 	free(root);
91*fa1cc128Sleo 	return(rv);
92*fa1cc128Sleo }
93