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