disklbl.c revision 1.4.6.2 1 1.4.6.2 wiz /* $NetBSD: disklbl.c,v 1.4.6.2 2001/07/26 23:07:58 wiz Exp $ */
2 1.4.6.2 wiz
3 1.4.6.2 wiz /*
4 1.4.6.2 wiz * Copyright (c) 1995 Waldi Ravens.
5 1.4.6.2 wiz * All rights reserved.
6 1.4.6.2 wiz *
7 1.4.6.2 wiz * Redistribution and use in source and binary forms, with or without
8 1.4.6.2 wiz * modification, are permitted provided that the following conditions
9 1.4.6.2 wiz * are met:
10 1.4.6.2 wiz * 1. Redistributions of source code must retain the above copyright
11 1.4.6.2 wiz * notice, this list of conditions and the following disclaimer.
12 1.4.6.2 wiz * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.6.2 wiz * notice, this list of conditions and the following disclaimer in the
14 1.4.6.2 wiz * documentation and/or other materials provided with the distribution.
15 1.4.6.2 wiz * 3. All advertising materials mentioning features or use of this software
16 1.4.6.2 wiz * must display the following acknowledgement:
17 1.4.6.2 wiz * This product includes software developed by Waldi Ravens.
18 1.4.6.2 wiz * 4. The name of the author may not be used to endorse or promote products
19 1.4.6.2 wiz * derived from this software without specific prior written permission.
20 1.4.6.2 wiz *
21 1.4.6.2 wiz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.4.6.2 wiz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.4.6.2 wiz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.4.6.2 wiz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.4.6.2 wiz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.4.6.2 wiz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.4.6.2 wiz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.4.6.2 wiz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.4.6.2 wiz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.4.6.2 wiz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.4.6.2 wiz */
32 1.4.6.2 wiz
33 1.4.6.2 wiz #include <sys/types.h>
34 1.4.6.2 wiz #include <stdlib.h>
35 1.4.6.2 wiz #include <stdio.h>
36 1.4.6.2 wiz #include "libtos.h"
37 1.4.6.2 wiz #include "aptck.h"
38 1.4.6.2 wiz #include "ahdilbl.h"
39 1.4.6.2 wiz #include "disklbl.h"
40 1.4.6.2 wiz
41 1.4.6.2 wiz static int dkcksum PROTO((struct disklabel *));
42 1.4.6.2 wiz static int bsd_label PROTO((disk_t *, u_int));
43 1.4.6.2 wiz static int ahdi_label PROTO((disk_t *));
44 1.4.6.2 wiz static int ahdi_display PROTO((disk_t *));
45 1.4.6.2 wiz static u_int ahdi_getparts PROTO((disk_t *, u_int, u_int));
46 1.4.6.2 wiz
47 1.4.6.2 wiz int
48 1.4.6.2 wiz readdisklabel(dd)
49 1.4.6.2 wiz disk_t *dd;
50 1.4.6.2 wiz {
51 1.4.6.2 wiz int e;
52 1.4.6.2 wiz
53 1.4.6.2 wiz printf("Device : %s (%s) [%s]\n", dd->sname, dd->fname,
54 1.4.6.2 wiz dd->product);
55 1.4.6.2 wiz printf("Medium size: %lu sectors\n", (u_long)dd->msize);
56 1.4.6.2 wiz printf("Sector size: %lu bytes\n\n", (u_long)dd->bsize);
57 1.4.6.2 wiz
58 1.4.6.2 wiz e = bsd_label(dd, LABELSECTOR);
59 1.4.6.2 wiz if (e < 0) {
60 1.4.6.2 wiz printf("Device I/O error (hardware problem?)\n\n");
61 1.4.6.2 wiz return(-1);
62 1.4.6.2 wiz }
63 1.4.6.2 wiz if (!e) {
64 1.4.6.2 wiz printf("NetBSD/Atari format, boot block: "
65 1.4.6.2 wiz "sector %u labeloffset %u\n\n",
66 1.4.6.2 wiz dd->bblock, dd->lblofs);
67 1.4.6.2 wiz return(0);
68 1.4.6.2 wiz }
69 1.4.6.2 wiz
70 1.4.6.2 wiz e = ahdi_label(dd);
71 1.4.6.2 wiz if (e < 0) {
72 1.4.6.2 wiz printf("Device I/O error (hardware problem?)\n\n");
73 1.4.6.2 wiz return(-1);
74 1.4.6.2 wiz }
75 1.4.6.2 wiz if (!e) {
76 1.4.6.2 wiz printf("AHDI format, NetBSD boot block: ");
77 1.4.6.2 wiz if (dd->bblock != NO_BOOT_BLOCK)
78 1.4.6.2 wiz printf("sector %u labeloffset %u\n\n",
79 1.4.6.2 wiz dd->bblock, dd->lblofs);
80 1.4.6.2 wiz else printf("none\n\n");
81 1.4.6.2 wiz return(0);
82 1.4.6.2 wiz }
83 1.4.6.2 wiz
84 1.4.6.2 wiz printf("Unknown label format.\n\n");
85 1.4.6.2 wiz return(-1);
86 1.4.6.2 wiz }
87 1.4.6.2 wiz
88 1.4.6.2 wiz static int
89 1.4.6.2 wiz bsd_label(dd, offset)
90 1.4.6.2 wiz disk_t *dd;
91 1.4.6.2 wiz u_int offset;
92 1.4.6.2 wiz {
93 1.4.6.2 wiz u_char *bblk;
94 1.4.6.2 wiz u_int nsec;
95 1.4.6.2 wiz int rv;
96 1.4.6.2 wiz
97 1.4.6.2 wiz nsec = (BBMINSIZE + (dd->bsize - 1)) / dd->bsize;
98 1.4.6.2 wiz bblk = disk_read(dd, offset, nsec);
99 1.4.6.2 wiz if (bblk) {
100 1.4.6.2 wiz u_int *end, *p;
101 1.4.6.2 wiz
102 1.4.6.2 wiz end = (u_int *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
103 1.4.6.2 wiz rv = 1;
104 1.4.6.2 wiz for (p = (u_int *)bblk; p < end; ++p) {
105 1.4.6.2 wiz struct disklabel *dl = (struct disklabel *)&p[1];
106 1.4.6.2 wiz if ( ( (p[0] == NBDAMAGIC && offset == 0)
107 1.4.6.2 wiz || (p[0] == AHDIMAGIC && offset != 0)
108 1.4.6.2 wiz || (u_char *)dl - bblk == 7168
109 1.4.6.2 wiz )
110 1.4.6.2 wiz && dl->d_npartitions <= MAXPARTITIONS
111 1.4.6.2 wiz && dl->d_magic2 == DISKMAGIC
112 1.4.6.2 wiz && dl->d_magic == DISKMAGIC
113 1.4.6.2 wiz && dkcksum(dl) == 0
114 1.4.6.2 wiz ) {
115 1.4.6.2 wiz dd->lblofs = (u_char *)dl - bblk;
116 1.4.6.2 wiz dd->bblock = offset;
117 1.4.6.2 wiz rv = 0;
118 1.4.6.2 wiz break;
119 1.4.6.2 wiz }
120 1.4.6.2 wiz }
121 1.4.6.2 wiz free(bblk);
122 1.4.6.2 wiz }
123 1.4.6.2 wiz else rv = -1;
124 1.4.6.2 wiz
125 1.4.6.2 wiz return(rv);
126 1.4.6.2 wiz }
127 1.4.6.2 wiz
128 1.4.6.2 wiz static int
129 1.4.6.2 wiz dkcksum(dl)
130 1.4.6.2 wiz struct disklabel *dl;
131 1.4.6.2 wiz {
132 1.4.6.2 wiz u_short *start, *end, sum = 0;
133 1.4.6.2 wiz
134 1.4.6.2 wiz start = (u_short *)dl;
135 1.4.6.2 wiz end = (u_short *)&dl->d_partitions[dl->d_npartitions];
136 1.4.6.2 wiz while (start < end)
137 1.4.6.2 wiz sum ^= *start++;
138 1.4.6.2 wiz return(sum);
139 1.4.6.2 wiz }
140 1.4.6.2 wiz
141 1.4.6.2 wiz int
142 1.4.6.2 wiz ahdi_label(dd)
143 1.4.6.2 wiz disk_t *dd;
144 1.4.6.2 wiz {
145 1.4.6.2 wiz u_int i;
146 1.4.6.2 wiz int e;
147 1.4.6.2 wiz
148 1.4.6.2 wiz /*
149 1.4.6.2 wiz * The AHDI format requires a specific block size.
150 1.4.6.2 wiz */
151 1.4.6.2 wiz if (dd->bsize != AHDI_BSIZE)
152 1.4.6.2 wiz return(1);
153 1.4.6.2 wiz
154 1.4.6.2 wiz /*
155 1.4.6.2 wiz * Fetch the AHDI partition descriptors.
156 1.4.6.2 wiz */
157 1.4.6.2 wiz i = ahdi_getparts(dd, AHDI_BBLOCK, AHDI_BBLOCK);
158 1.4.6.2 wiz if (i) {
159 1.4.6.2 wiz if (i < dd->msize)
160 1.4.6.2 wiz return(-1); /* disk read error */
161 1.4.6.2 wiz else return(1); /* reading past end of medium */
162 1.4.6.2 wiz }
163 1.4.6.2 wiz
164 1.4.6.2 wiz /*
165 1.4.6.2 wiz * Display and perform sanity checks.
166 1.4.6.2 wiz */
167 1.4.6.2 wiz i = ahdi_display(dd);
168 1.4.6.2 wiz if (i)
169 1.4.6.2 wiz return(i);
170 1.4.6.2 wiz
171 1.4.6.2 wiz /*
172 1.4.6.2 wiz * Search for a NetBSD disk label
173 1.4.6.2 wiz */
174 1.4.6.2 wiz dd->bblock = NO_BOOT_BLOCK;
175 1.4.6.2 wiz for (i = 0; i < dd->nparts; ++i) {
176 1.4.6.2 wiz part_t *pd = &dd->parts[i];
177 1.4.6.2 wiz u_int id = *((u_int32_t *)&pd->id) >> 8;
178 1.4.6.2 wiz if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
179 1.4.6.2 wiz u_int offs = pd->start;
180 1.4.6.2 wiz if ((e = bsd_label(dd, offs)) < 0) {
181 1.4.6.2 wiz return(e); /* I/O error */
182 1.4.6.2 wiz }
183 1.4.6.2 wiz if (!e) {
184 1.4.6.2 wiz dd->bblock = offs; /* got it */
185 1.4.6.2 wiz return(0);
186 1.4.6.2 wiz }
187 1.4.6.2 wiz if (id == AHDI_PID_NBD && dd->bblock == NO_BOOT_BLOCK)
188 1.4.6.2 wiz dd->bblock = offs;
189 1.4.6.2 wiz }
190 1.4.6.2 wiz }
191 1.4.6.2 wiz return(0);
192 1.4.6.2 wiz }
193 1.4.6.2 wiz
194 1.4.6.2 wiz static int
195 1.4.6.2 wiz root_cmp(x1, x2)
196 1.4.6.2 wiz const void *x1, *x2;
197 1.4.6.2 wiz {
198 1.4.6.2 wiz const u_int *r1 = x1,
199 1.4.6.2 wiz *r2 = x2;
200 1.4.6.2 wiz
201 1.4.6.2 wiz if (*r1 < *r2)
202 1.4.6.2 wiz return(-1);
203 1.4.6.2 wiz if (*r1 > *r2)
204 1.4.6.2 wiz return(1);
205 1.4.6.2 wiz return(0);
206 1.4.6.2 wiz }
207 1.4.6.2 wiz
208 1.4.6.2 wiz static int
209 1.4.6.2 wiz part_cmp(x1, x2)
210 1.4.6.2 wiz const void *x1, *x2;
211 1.4.6.2 wiz {
212 1.4.6.2 wiz const part_t *p1 = x1,
213 1.4.6.2 wiz *p2 = x2;
214 1.4.6.2 wiz
215 1.4.6.2 wiz if (p1->start < p2->start)
216 1.4.6.2 wiz return(-1);
217 1.4.6.2 wiz if (p1->start > p2->start)
218 1.4.6.2 wiz return(1);
219 1.4.6.2 wiz if (p1->end < p2->end)
220 1.4.6.2 wiz return(-1);
221 1.4.6.2 wiz if (p1->end > p2->end)
222 1.4.6.2 wiz return(1);
223 1.4.6.2 wiz if (p1->rsec < p2->rsec)
224 1.4.6.2 wiz return(-1);
225 1.4.6.2 wiz if (p1->rsec > p2->rsec)
226 1.4.6.2 wiz return(1);
227 1.4.6.2 wiz if (p1->rent < p2->rent)
228 1.4.6.2 wiz return(-1);
229 1.4.6.2 wiz if (p1->rent > p2->rent)
230 1.4.6.2 wiz return(1);
231 1.4.6.2 wiz return(0);
232 1.4.6.2 wiz }
233 1.4.6.2 wiz
234 1.4.6.2 wiz static int
235 1.4.6.2 wiz ahdi_display(dd)
236 1.4.6.2 wiz disk_t *dd;
237 1.4.6.2 wiz {
238 1.4.6.2 wiz int i, j, rv = 0;
239 1.4.6.2 wiz
240 1.4.6.2 wiz printf("Start of bad sector list : %u\n", dd->bslst);
241 1.4.6.2 wiz if (dd->bslst == 0) {
242 1.4.6.2 wiz printf("* Illegal value (zero) *\n"); rv = 1;
243 1.4.6.2 wiz }
244 1.4.6.2 wiz printf("End of bad sector list : %u\n", dd->bslend);
245 1.4.6.2 wiz if (dd->bslend == 0) {
246 1.4.6.2 wiz printf("* Illegal value (zero) *\n"); rv = 1;
247 1.4.6.2 wiz }
248 1.4.6.2 wiz printf("Medium size (in root sec): %u\n", dd->hdsize);
249 1.4.6.2 wiz if (dd->hdsize == 0) {
250 1.4.6.2 wiz printf("* Illegal value (zero) *\n"); rv = 1;
251 1.4.6.2 wiz }
252 1.4.6.2 wiz
253 1.4.6.2 wiz qsort(dd->roots, dd->nroots, sizeof *dd->roots, root_cmp);
254 1.4.6.2 wiz qsort(dd->parts, dd->nparts, sizeof *dd->parts, part_cmp);
255 1.4.6.2 wiz printf("\n root desc id start end MBs\n");
256 1.4.6.2 wiz
257 1.4.6.2 wiz for (i = 0; i < dd->nparts; ++i) {
258 1.4.6.2 wiz part_t *p1 = &dd->parts[i];
259 1.4.6.2 wiz u_int megs = p1->end - p1->start + 1,
260 1.4.6.2 wiz blpm = (1024 * 1024) / dd->bsize;
261 1.4.6.2 wiz megs = (megs + (blpm >> 1)) / blpm;
262 1.4.6.2 wiz printf("%8u %4u %s %8u %8u (%3u)\n",
263 1.4.6.2 wiz p1->rsec, p1->rent, p1->id,
264 1.4.6.2 wiz p1->start, p1->end, megs);
265 1.4.6.2 wiz for (j = 0; j < dd->nroots; ++j) {
266 1.4.6.2 wiz u_int aux = dd->roots[j];
267 1.4.6.2 wiz if (aux >= p1->start && aux <= p1->end) {
268 1.4.6.2 wiz printf("FATAL: auxiliary root at %u\n", aux);
269 1.4.6.2 wiz rv = 1;
270 1.4.6.2 wiz }
271 1.4.6.2 wiz }
272 1.4.6.2 wiz for (j = i; j--;) {
273 1.4.6.2 wiz part_t *p2 = &dd->parts[j];
274 1.4.6.2 wiz if (p1->start >= p2->start && p1->start <= p2->end) {
275 1.4.6.2 wiz printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent);
276 1.4.6.2 wiz rv = 1;
277 1.4.6.2 wiz }
278 1.4.6.2 wiz if (p2->start >= p1->start && p2->start <= p1->end) {
279 1.4.6.2 wiz printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent);
280 1.4.6.2 wiz rv = 1;
281 1.4.6.2 wiz }
282 1.4.6.2 wiz }
283 1.4.6.2 wiz if (p1->start >= dd->bslst && p1->start <= dd->bslend) {
284 1.4.6.2 wiz printf("FATAL: partition overlaps with bad sector list\n");
285 1.4.6.2 wiz rv = 1;
286 1.4.6.2 wiz }
287 1.4.6.2 wiz if (dd->bslst >= p1->start && dd->bslst <= p1->end) {
288 1.4.6.2 wiz printf("FATAL: partition overlaps with bad sector list\n");
289 1.4.6.2 wiz rv = 1;
290 1.4.6.2 wiz }
291 1.4.6.2 wiz }
292 1.4.6.2 wiz
293 1.4.6.2 wiz printf("\nTotal number of auxiliary roots: %u\n", dd->nroots);
294 1.4.6.2 wiz printf("Total number of partitions : %u\n", dd->nparts);
295 1.4.6.2 wiz if (dd->nparts == 0) {
296 1.4.6.2 wiz printf("* Weird # of partitions (zero) *\n"); rv = 1;
297 1.4.6.2 wiz }
298 1.4.6.2 wiz if (dd->nparts > AHDI_MAXPARTS) {
299 1.4.6.2 wiz printf("* Too many AHDI partitions for the default NetBSD "
300 1.4.6.2 wiz "kernel *\n Increase MAXAUXROOTS in src/sys/arch/"
301 1.4.6.2 wiz "atari/include/disklabel.h\n to at least %u, and "
302 1.4.6.2 wiz "recompile the NetBSD kernel.\n", dd->nroots);
303 1.4.6.2 wiz rv = -1;
304 1.4.6.2 wiz }
305 1.4.6.2 wiz return(rv);
306 1.4.6.2 wiz }
307 1.4.6.2 wiz
308 1.4.6.2 wiz static u_int
309 1.4.6.2 wiz ahdi_getparts(dd, rsec, esec)
310 1.4.6.2 wiz disk_t *dd;
311 1.4.6.2 wiz u_int rsec,
312 1.4.6.2 wiz esec;
313 1.4.6.2 wiz {
314 1.4.6.2 wiz struct ahdi_part *part, *end;
315 1.4.6.2 wiz struct ahdi_root *root;
316 1.4.6.2 wiz u_int rv;
317 1.4.6.2 wiz
318 1.4.6.2 wiz root = disk_read(dd, rsec, 1);
319 1.4.6.2 wiz if (!root) {
320 1.4.6.2 wiz rv = rsec + (rsec == 0);
321 1.4.6.2 wiz goto done;
322 1.4.6.2 wiz }
323 1.4.6.2 wiz
324 1.4.6.2 wiz if (rsec == AHDI_BBLOCK)
325 1.4.6.2 wiz end = &root->ar_parts[AHDI_MAXRPD];
326 1.4.6.2 wiz else end = &root->ar_parts[AHDI_MAXARPD];
327 1.4.6.2 wiz for (part = root->ar_parts; part < end; ++part) {
328 1.4.6.2 wiz u_int id = *((u_int32_t *)&part->ap_flg);
329 1.4.6.2 wiz if (!(id & 0x01000000))
330 1.4.6.2 wiz continue;
331 1.4.6.2 wiz if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
332 1.4.6.2 wiz u_int offs = part->ap_offs + esec;
333 1.4.6.2 wiz u_int i = ++dd->nroots;
334 1.4.6.2 wiz dd->roots = xrealloc(dd->roots, i * sizeof *dd->roots);
335 1.4.6.2 wiz dd->roots[--i] = offs;
336 1.4.6.2 wiz rv = ahdi_getparts(dd, offs,
337 1.4.6.2 wiz esec == AHDI_BBLOCK ? offs : esec);
338 1.4.6.2 wiz if (rv)
339 1.4.6.2 wiz goto done;
340 1.4.6.2 wiz } else {
341 1.4.6.2 wiz part_t *p;
342 1.4.6.2 wiz u_int i = ++dd->nparts;
343 1.4.6.2 wiz dd->parts = xrealloc(dd->parts, i * sizeof *dd->parts);
344 1.4.6.2 wiz p = &dd->parts[--i];
345 1.4.6.2 wiz *((u_int32_t *)&p->id) = id << 8;
346 1.4.6.2 wiz p->start = part->ap_offs + rsec;
347 1.4.6.2 wiz p->end = p->start + part->ap_size - 1;
348 1.4.6.2 wiz p->rsec = rsec;
349 1.4.6.2 wiz p->rent = part - root->ar_parts;
350 1.4.6.2 wiz }
351 1.4.6.2 wiz }
352 1.4.6.2 wiz dd->hdsize = root->ar_hdsize;
353 1.4.6.2 wiz dd->bslst = root->ar_bslst;
354 1.4.6.2 wiz dd->bslend = root->ar_bslst + root->ar_bslsize - 1;
355 1.4.6.2 wiz rv = 0;
356 1.4.6.2 wiz done:
357 1.4.6.2 wiz free(root);
358 1.4.6.2 wiz return(rv);
359 1.4.6.2 wiz }
360