summaryrefslogtreecommitdiff
path: root/sys/arch/atari/stand/tostools/aptck/disklbl.c
blob: 869edb52513ff5128b1c19940b88f8a5128da423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*	$NetBSD: disklbl.c,v 1.2 1996/01/20 13:54:46 leo Exp $	*/

/*
 * Copyright (c) 1995 Waldi Ravens.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by Waldi Ravens.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include "libtos.h"
#include "aptck.h"
#include "ahdilbl.h"
#include "disklbl.h"

static int	dkcksum    PROTO((struct disklabel *));
static int	bsd_label  PROTO((disk_t *, u_int));
static int	ahdi_label PROTO((disk_t *));
static int	ahdi_display PROTO((disk_t *));
static u_int	ahdi_getparts PROTO((disk_t *, u_int, u_int));

int
readdisklabel(dd)
	disk_t	*dd;
{
	int	e;

	printf("Device     : %s (%s) [%s]\n", dd->sname, dd->fname, dd->product);
	printf("Medium size: %lu sectors\n", (u_long)dd->msize);
	printf("Sector size: %lu bytes\n\n", (u_long)dd->bsize);

	e = bsd_label(dd, LABELSECTOR);
	if (e < 0) {
		printf("Device I/O error (hardware problem?)\n\n");
		return(-1);
	}
	if (!e) {
		printf("NetBSD/Atari format, boot block: "
		       "sector %u labeloffset %u\n\n",
		       dd->bblock, dd->lblofs);
		return(0);
	}

	e = ahdi_label(dd);
	if (e < 0) {
		printf("Device I/O error (hardware problem?)\n\n");
		return(-1);
	}
	if (!e) {
		printf("AHDI format, NetBSD boot block: ");
		if (dd->bblock != NO_BOOT_BLOCK)
			printf("sector %u labeloffset %u\n\n",
			       dd->bblock, dd->lblofs);
		else printf("none\n\n");
		return(0);
	}

	printf("Unknown label format.\n\n");
	return(-1);
}

static int
bsd_label(dd, offset)
	disk_t		*dd;
	u_int		offset;
{
	u_char		*bblk;
	u_int		nsec;
	int		rv;

	nsec = (BBMINSIZE + (dd->bsize - 1)) / dd->bsize;
	bblk = disk_read(dd, offset, nsec);
	if (bblk) {
		u_short	*end, *p;
		
		end = (u_short *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
		rv = 1;
		for (p = (u_short *)bblk; p < end; ++p) {
			struct disklabel *dl = (struct disklabel *)p;
			if (dl->d_magic == DISKMAGIC && dl->d_magic2 == DISKMAGIC
		    	    && dl->d_npartitions <= MAXPARTITIONS && !dkcksum(dl)) {
		    		dd->lblofs = (u_char *)p - bblk;
		    		dd->bblock = offset;
				rv = 0;
				break;
			}
		}
		free(bblk);
	}
	else rv = -1;

	return(rv);
}

static int
dkcksum(dl)
	struct disklabel *dl;
{
	u_short	*start, *end, sum = 0;

	start = (u_short *)dl;
	end   = (u_short *)&dl->d_partitions[dl->d_npartitions];
	while (start < end)
		sum ^= *start++;
	return(sum);
}

int
ahdi_label(dd)
	disk_t	*dd;
{
	u_int	i;
	int	e;

	/*
	 * The AHDI format requires a specific block size.
	 */
	if (dd->bsize != AHDI_BSIZE)
		return(1);

	/*
	 * Fetch the AHDI partition descriptors.
	 */
	i = ahdi_getparts(dd, AHDI_BBLOCK, AHDI_BBLOCK);
	if (i) {
		if (i < dd->msize)
			return(-1);	/* disk read error		*/
		else return(1);		/* reading past end of medium	*/
	}

	/*
	 * Display and perform sanity checks.
	 */
	i = ahdi_display(dd);
	if (i)
		return(i);

	/*
	 * Search for a NetBSD disk label
	 */
	dd->bblock = NO_BOOT_BLOCK;
	for (i = 0; i < dd->nparts; ++i) {
		part_t	*pd = &dd->parts[i];
		u_int	id  = *((u_int32_t *)&pd->id) >> 8;
		if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
			u_int	offs = pd->start;
			if ((e = bsd_label(dd, offs)) < 0) {
				return(e);		/* I/O error */
			}
			if (!e) {
				dd->bblock = offs;	/* got it */
				return(0);
			}
			if (id == AHDI_PID_NBD && dd->bblock == NO_BOOT_BLOCK)
				dd->bblock = offs;
		}
	}
	return(0);
}

static int
root_cmp(x1, x2)
	const void	*x1, *x2;
{
	const u_int	*r1 = x1,
			*r2 = x2;

	if (*r1 < *r2)
		return(-1);
	if (*r1 > *r2)
		return(1);
	return(0);
}

static int
part_cmp(x1, x2)
	const void	*x1, *x2;
{
	const part_t	*p1 = x1,
			*p2 = x2;

	if (p1->start < p2->start)
		return(-1);
	if (p1->start > p2->start)
		return(1);
	if (p1->end < p2->end)
		return(-1);
	if (p1->end > p2->end)
		return(1);
	if (p1->rsec < p2->rsec)
		return(-1);
	if (p1->rsec > p2->rsec)
		return(1);
	if (p1->rent < p2->rent)
		return(-1);
	if (p1->rent > p2->rent)
		return(1);
	return(0);
}

static int
ahdi_display(dd)
	disk_t	*dd;
{
	int	i, j, rv = 0;

	printf("Start of bad sector list : %u\n", dd->bslst);
	if (dd->bslst == 0) {
		printf("* Illegal value (zero) *\n"); rv = 1;
	}
	printf("End of bad sector list   : %u\n", dd->bslend);
	if (dd->bslend == 0) {
		printf("* Illegal value (zero) *\n"); rv = 1;
	}
	printf("Medium size (in root sec): %u\n", dd->hdsize);
	if (dd->hdsize == 0) {
		printf("* Illegal value (zero) *\n"); rv = 1;
	}

	qsort(dd->roots, dd->nroots, sizeof *dd->roots, root_cmp);
	qsort(dd->parts, dd->nparts, sizeof *dd->parts, part_cmp);
	printf("\n    root  desc   id     start       end    MBs\n");

	for (i = 0; i < dd->nparts; ++i) {
		part_t	*p1 = &dd->parts[i];
		u_int	megs = p1->end - p1->start + 1,
			blpm = (1024 * 1024) / dd->bsize;
		megs = (megs + (blpm >> 1)) / blpm;
		printf("%8u  %4u  %s  %8u  %8u  (%3u)\n",
			p1->rsec, p1->rent, p1->id,
			p1->start, p1->end, megs);
		for (j = 0; j < dd->nroots; ++j) {
			u_int	aux = dd->roots[j];
			if (aux >= p1->start && aux <= p1->end) {
				printf("FATAL: auxilary root at %u\n", aux); rv = 1;
			}
		}
		for (j = i; j--;) {
			part_t	*p2 = &dd->parts[j];
			if (p1->start >= p2->start && p1->start <= p2->end) {
				printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent); rv = 1;
			}
			if (p2->start >= p1->start && p2->start <= p1->end) {
				printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent); rv = 1;
			}
		}
		if (p1->start >= dd->bslst && p1->start <= dd->bslend) {
			printf("FATAL: partition overlaps with bad sector list\n"); rv = 1;
		}
		if (dd->bslst >= p1->start && dd->bslst <= p1->end) {
			printf("FATAL: partition overlaps with bad sector list\n"); rv = 1;
		}
	}

	printf("\nTotal number of auxilary roots: %u\n", dd->nroots);
	printf("Total number of partitions    : %u\n", dd->nparts);
	if (dd->nparts == 0) {
		printf("* Weird # of partitions (zero) *\n"); rv = 1;
	}
	if (dd->nparts > AHDI_MAXPARTS) {
		printf("* Too many AHDI partitions for the default NetBSD "
			"kernel *\n  Increase MAXAUXROOTS in src/sys/arch/"
			"atari/include/disklabel.h\n  to at least %u, and "
			"recompile the NetBSD kernel.\n", dd->nroots);
		rv = -1;
	}
	return(rv);
}

static u_int
ahdi_getparts(dd, rsec, esec)
	disk_t			*dd;
	u_int			rsec,
				esec;
{
	struct ahdi_part	*part, *end;
	struct ahdi_root	*root;
	u_int			rv;

	root = disk_read(dd, rsec, 1);
	if (!root) {
		rv = rsec + (rsec == 0);
		goto done;
	}

	if (rsec == AHDI_BBLOCK)
		end = &root->ar_parts[AHDI_MAXRPD];
	else end = &root->ar_parts[AHDI_MAXARPD];
	for (part = root->ar_parts; part < end; ++part) {
		u_int	id = *((u_int32_t *)&part->ap_flg);
		if (!(id & 0x01000000))
			continue;
		if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
			u_int	offs = part->ap_offs + esec;
			u_int	i = ++dd->nroots;
			dd->roots = xrealloc(dd->roots, i * sizeof *dd->roots);
			dd->roots[--i] = offs;
			rv = ahdi_getparts(dd, offs, esec == AHDI_BBLOCK ? offs : esec);
			if (rv)
				goto done;
		} else {
			part_t	*p;
			u_int	i = ++dd->nparts;
			dd->parts = xrealloc(dd->parts, i * sizeof *dd->parts);
			p = &dd->parts[--i];
			*((u_int32_t *)&p->id) = id << 8;
			p->start = part->ap_offs + rsec;
			p->end   = p->start + part->ap_size - 1;
			p->rsec  = rsec;
			p->rent  = part - root->ar_parts;
		}
	}
	dd->hdsize = root->ar_hdsize;
	dd->bslst  = root->ar_bslst;
	dd->bslend = root->ar_bslst + root->ar_bslsize - 1;
	rv = 0;
done:
	free(root);
	return(rv);
}