disklbl.c revision 1.1 1 /* $NetBSD: disklbl.c,v 1.1 1996/01/16 15:15:48 leo 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(dd)
49 disk_t *dd;
50 {
51 int e;
52
53 printf("Device : %s (%s) [%s]\n", dd->sname, dd->fname, 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(dd, offset)
89 disk_t *dd;
90 u_int offset;
91 {
92 u_char *bblk;
93 u_int nsec;
94 int rv;
95
96 nsec = (BBSIZE + (dd->bsize - 1)) / dd->bsize;
97 bblk = disk_read(dd, offset, nsec);
98 if (bblk) {
99 u_short *end, *p;
100
101 end = (u_short *)&bblk[BBSIZE - sizeof(struct disklabel)];
102 rv = 1;
103 for (p = (u_short *)bblk; p < end; ++p) {
104 struct disklabel *dl = (struct disklabel *)p;
105 if (dl->d_magic == DISKMAGIC && dl->d_magic2 == DISKMAGIC
106 && dl->d_npartitions <= MAXPARTITIONS && !dkcksum(dl)) {
107 dd->lblofs = (u_char *)p - bblk;
108 dd->bblock = offset;
109 rv = 0;
110 break;
111 }
112 }
113 free(bblk);
114 }
115 else rv = -1;
116
117 return(rv);
118 }
119
120 static int
121 dkcksum(dl)
122 struct disklabel *dl;
123 {
124 u_short *start, *end, sum = 0;
125
126 start = (u_short *)dl;
127 end = (u_short *)&dl->d_partitions[dl->d_npartitions];
128 while (start < end)
129 sum ^= *start++;
130 return(sum);
131 }
132
133 int
134 ahdi_label(dd)
135 disk_t *dd;
136 {
137 u_int i;
138 int e;
139
140 /*
141 * The AHDI format requires a specific block size.
142 */
143 if (dd->bsize != AHDI_BSIZE)
144 return(1);
145
146 /*
147 * Fetch the AHDI partition descriptors.
148 */
149 i = ahdi_getparts(dd, AHDI_BBLOCK, AHDI_BBLOCK);
150 if (i) {
151 if (i < dd->msize)
152 return(-1); /* disk read error */
153 else return(1); /* reading past end of medium */
154 }
155
156 /*
157 * Display and perform sanity checks.
158 */
159 i = ahdi_display(dd);
160 if (i)
161 return(i);
162
163 /*
164 * Search for a NetBSD disk label
165 */
166 dd->bblock = NO_BOOT_BLOCK;
167 for (i = 0; i < dd->nparts; ++i) {
168 part_t *pd = &dd->parts[i];
169 u_int id = *((u_int32_t *)&pd->id) >> 8;
170 if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
171 u_int offs = pd->start;
172 if ((e = bsd_label(dd, offs)) < 0) {
173 return(e); /* I/O error */
174 }
175 if (!e) {
176 dd->bblock = offs; /* got it */
177 return(0);
178 }
179 if (id == AHDI_PID_NBD && dd->bblock == NO_BOOT_BLOCK)
180 dd->bblock = offs;
181 }
182 }
183 return(0);
184 }
185
186 static int
187 root_cmp(x1, x2)
188 const void *x1, *x2;
189 {
190 const u_int *r1 = x1,
191 *r2 = x2;
192
193 if (*r1 < *r2)
194 return(-1);
195 if (*r1 > *r2)
196 return(1);
197 return(0);
198 }
199
200 static int
201 part_cmp(x1, x2)
202 const void *x1, *x2;
203 {
204 const part_t *p1 = x1,
205 *p2 = x2;
206
207 if (p1->start < p2->start)
208 return(-1);
209 if (p1->start > p2->start)
210 return(1);
211 if (p1->end < p2->end)
212 return(-1);
213 if (p1->end > p2->end)
214 return(1);
215 if (p1->rsec < p2->rsec)
216 return(-1);
217 if (p1->rsec > p2->rsec)
218 return(1);
219 if (p1->rent < p2->rent)
220 return(-1);
221 if (p1->rent > p2->rent)
222 return(1);
223 return(0);
224 }
225
226 static int
227 ahdi_display(dd)
228 disk_t *dd;
229 {
230 int i, j, rv = 0;
231
232 printf("Start of bad sector list : %u\n", dd->bslst);
233 if (dd->bslst == 0) {
234 printf("* Illegal value (zero) *\n"); rv = 1;
235 }
236 printf("End of bad sector list : %u\n", dd->bslend);
237 if (dd->bslend == 0) {
238 printf("* Illegal value (zero) *\n"); rv = 1;
239 }
240 printf("Medium size (in root sec): %u\n", dd->hdsize);
241 if (dd->hdsize == 0) {
242 printf("* Illegal value (zero) *\n"); rv = 1;
243 }
244
245 qsort(dd->roots, dd->nroots, sizeof *dd->roots, root_cmp);
246 qsort(dd->parts, dd->nparts, sizeof *dd->parts, part_cmp);
247 printf("\n root desc id start end MBs\n");
248
249 for (i = 0; i < dd->nparts; ++i) {
250 part_t *p1 = &dd->parts[i];
251 u_int megs = p1->end - p1->start + 1,
252 blpm = (1024 * 1024) / dd->bsize;
253 megs = (megs + (blpm >> 1)) / blpm;
254 printf("%8u %4u %s %8u %8u (%3u)\n",
255 p1->rsec, p1->rent, p1->id,
256 p1->start, p1->end, megs);
257 for (j = 0; j < dd->nroots; ++j) {
258 u_int aux = dd->roots[j];
259 if (aux >= p1->start && aux <= p1->end) {
260 printf("FATAL: auxilary root at %u\n", aux); rv = 1;
261 }
262 }
263 for (j = i; j--;) {
264 part_t *p2 = &dd->parts[j];
265 if (p1->start >= p2->start && p1->start <= p2->end) {
266 printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent); rv = 1;
267 }
268 if (p2->start >= p1->start && p2->start <= p1->end) {
269 printf("FATAL: clash with %u/%u\n", p2->rsec, p2->rent); rv = 1;
270 }
271 }
272 if (p1->start >= dd->bslst && p1->start <= dd->bslend) {
273 printf("FATAL: partition overlaps with bad sector list\n"); rv = 1;
274 }
275 if (dd->bslst >= p1->start && dd->bslst <= p1->end) {
276 printf("FATAL: partition overlaps with bad sector list\n"); rv = 1;
277 }
278 }
279
280 printf("\nTotal number of auxilary roots: %u\n", dd->nroots);
281 printf("Total number of partitions : %u\n", dd->nparts);
282 if (dd->nparts == 0) {
283 printf("* Weird # of partitions (zero) *\n"); rv = 1;
284 }
285 if (dd->nparts > AHDI_MAXPARTS) {
286 printf("* Too many AHDI partitions for the default NetBSD "
287 "kernel *\n Increase MAXAUXROOTS in src/sys/arch/"
288 "atari/include/disklabel.h\n to at least %u, and "
289 "recompile the NetBSD kernel.\n", dd->nroots);
290 rv = -1;
291 }
292 return(rv);
293 }
294
295 static u_int
296 ahdi_getparts(dd, rsec, esec)
297 disk_t *dd;
298 u_int rsec,
299 esec;
300 {
301 struct ahdi_part *part, *end;
302 struct ahdi_root *root;
303 u_int rv;
304
305 root = disk_read(dd, rsec, 1);
306 if (!root) {
307 rv = rsec + (rsec == 0);
308 goto done;
309 }
310
311 if (rsec == AHDI_BBLOCK)
312 end = &root->ar_parts[AHDI_MAXRPD];
313 else end = &root->ar_parts[AHDI_MAXARPD];
314 for (part = root->ar_parts; part < end; ++part) {
315 u_int id = *((u_int32_t *)&part->ap_flg);
316 if (!(id & 0x01000000))
317 continue;
318 if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
319 u_int offs = part->ap_offs + esec;
320 u_int i = ++dd->nroots;
321 dd->roots = xrealloc(dd->roots, i * sizeof *dd->roots);
322 dd->roots[--i] = offs;
323 rv = ahdi_getparts(dd, offs, esec == AHDI_BBLOCK ? offs : esec);
324 if (rv)
325 goto done;
326 } else {
327 part_t *p;
328 u_int i = ++dd->nparts;
329 dd->parts = xrealloc(dd->parts, i * sizeof *dd->parts);
330 p = &dd->parts[--i];
331 *((u_int32_t *)&p->id) = id << 8;
332 p->start = part->ap_offs + rsec;
333 p->end = p->start + part->ap_size - 1;
334 p->rsec = rsec;
335 p->rent = part - root->ar_parts;
336 }
337 }
338 dd->hdsize = root->ar_hdsize;
339 dd->bslst = root->ar_bslst;
340 dd->bslend = root->ar_bslst + root->ar_bslsize - 1;
341 rv = 0;
342 done:
343 free(root);
344 return(rv);
345 }
346