/*-
* Copyright (c) 2004 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/*
* Adapted from kern/subr_disk_mbr.c:
*
* Copyright (c) 1982, 1986, 1988 Regents of the University of California.
* 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. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*
* @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
*/
/*
* 4.4BSD disklabel support for disk wedges
*
* Here is the basic search algorithm in use here:
*
* For historical reasons, we scan for x86-style MBR partitions looking
* for a MBR_PTYPE_NETBSD (or MBR_PTYPE_386BSD) partition. The first
* 4.4BSD disklabel found in the 2nd sector of such a partition is used.
* We assume that the 4.4BSD disklabel describes all partitions on the
* disk; we do not use any partition information from the MBR partition
* table.
*
* If that fails, then we fall back on a table of known locations for
* various platforms.
*/
/*
* Note the smallest MAXPARTITIONS was 8, so we allow a disklabel
* that size to be locted at the end of the sector.
*/
#define DISKLABEL_MINSIZE DISKLABEL_SIZE(8)
/*
* For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
* a suitable case branch will convert the type number to a string.
*/
switch (fstype) {
#define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
case __CONCAT(FS_,tag): str = __CONCAT(DKW_PTYPE_,tag); break;
FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
#undef FSTYPE_TO_STR_CASE
default: str = NULL; break;
}
return (str);
}
static void
swap_disklabel(struct disklabel *lp)
{
int i;
for (i = 0; i < lp->d_npartitions; i++) {
SWAP32(d_partitions[i].p_size);
SWAP32(d_partitions[i].p_offset);
SWAP32(d_partitions[i].p_fsize);
SWAP16(d_partitions[i].p_cpg);
}
#undef SWAP16
#undef SWAP32
}
/*
* Add wedges for a valid NetBSD disklabel.
*/
static void
addwedges(const mbr_args_t *a, const struct disklabel *lp)
{
int error, i;
for (i = 0; i < lp->d_npartitions; i++) {
struct dkwedge_info dkw;
const struct partition *p;
const char *ptype;
/*
* If the label defines a name, append the partition
* letter and use it as the wedge name.
* Otherwise use historical disk naming style
* wedge names.
*/
if (lp->d_packname[0] &&
strcmp(lp->d_packname,"fictitious") != 0) {
snprintf((char *)&dkw.dkw_wname, sizeof(dkw.dkw_wname),
"%.*s/%c", (int)sizeof(dkw.dkw_wname)-3,
lp->d_packname, 'a' + i);
} else {
snprintf((char *)&dkw.dkw_wname, sizeof(dkw.dkw_wname),
"%s%c", a->pdk->dk_name, 'a' + i);
}
error = dkwedge_add(&dkw);
if (error == EEXIST)
aprint_error("%s: wedge named '%s' already "
"exists, manual intervention required\n",
a->pdk->dk_name, dkw.dkw_wname);
else if (error)
aprint_error("%s: error %d adding partition "
"%d type %d\n", a->pdk->dk_name, error,
i, p->p_fstype);
}
}
static int
validate_label(mbr_args_t *a, daddr_t label_sector, size_t label_offset)
{
struct disklabel *lp;
void *lp_lim;
int error, swapped;
uint16_t npartitions;
/*
* We ignore label_offset; this seems to have not been used
* consistently in the old code, requiring us to do the search
* in the sector.
*/
lp = a->bp->b_data;
lp_lim = (char *)a->bp->b_data + a->secsize - DISKLABEL_MINSIZE;
for (;; lp = (void *)((char *)lp + sizeof(uint32_t))) {
if ((char *)lp > (char *)lp_lim)
return (SCAN_CONTINUE);
label_offset = (size_t)((char *)lp - (char *)a->bp->b_data);
if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
if (lp->d_magic != bswap32(DISKMAGIC) ||
lp->d_magic2 != bswap32(DISKMAGIC))
continue;
/* Label is in the other byte order. */
swapped = 1;
} else
swapped = 0;
/*
* We have validated the partition count. Checksum it.
* Note that we purposefully checksum before swapping
* the byte order.
*/
if (dkcksum_sized(lp, npartitions) != 0) {
aprint_error("%s: BSD disklabel @ %" PRId64
"+%zd has bad checksum\n", a->pdk->dk_name,
label_sector, label_offset);
continue;
}
/* Put the disklabel in the right order. */
if (swapped)
swap_disklabel(lp);
addwedges(a, lp);
return (SCAN_FOUND);
}
}
static int
scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, struct mbr_partition *,
int, u_int))
{
struct mbr_partition ptns[MBR_PART_COUNT];
struct mbr_partition *dp;
struct mbr_sector *mbr;
u_int ext_base, this_ext, next_ext;
int i, rval;
#ifdef COMPAT_386BSD_MBRPART
int dp_386bsd = -1;
#endif
mbr = a->bp->b_data;
if (mbr->mbr_magic != htole16(MBR_MAGIC))
return (SCAN_CONTINUE);
/* Copy data out of buffer so action can use the buffer. */
memcpy(ptns, &mbr->mbr_parts, sizeof(ptns));
/* Looks for NetBSD partition. */
next_ext = 0;
dp = ptns;
for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
if (dp->mbrp_type == 0)
continue;
if (MBR_IS_EXTENDED(dp->mbrp_type)) {
next_ext = le32toh(dp->mbrp_start);
continue;
}
#ifdef COMPAT_386BSD_MBRPART
if (dp->mbrp_type == MBR_PTYPE_386BSD) {
/*
* If more than one matches, take last,
* as NetBSD install tool does.
*/
if (this_ext == 0)
dp_386bsd = i;
continue;
}
#endif
rval = (*actn)(a, dp, i, this_ext);
if (rval != SCAN_CONTINUE)
return (rval);
}
if (next_ext == 0)
break;
if (ext_base == 0) {
ext_base = next_ext;
next_ext = 0;
}
next_ext += ext_base;
if (next_ext <= this_ext)
break;
this_ext = next_ext;
}
#ifdef COMPAT_386BSD_MBRPART
if (this_ext == 0 && dp_386bsd != -1)
return ((*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0));
#endif
return (SCAN_CONTINUE);
}
static int
look_netbsd_part(mbr_args_t *a, struct mbr_partition *dp, int slot,
u_int ext_base)
{
int ptn_base = ext_base + le32toh(dp->mbrp_start);
int rval;