/*
* 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.
*/
static int
dkcksum(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(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);
}
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(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: auxiliary 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 auxiliary 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);
}