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