#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.34 2026/07/14 13:34:36 thorpej Exp $");
#include "opt_mbr.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/syslog.h>
#include <sys/bswap.h>
int fat_types[] = { MBR_PTYPE_FAT12, MBR_PTYPE_FAT16S,
MBR_PTYPE_FAT16B, MBR_PTYPE_FAT32,
MBR_PTYPE_FAT32L, MBR_PTYPE_FAT16L,
-1 };
#define NO_MBR_SIGNATURE ((struct mbr_partition *) -1)
#ifdef BSDDISKLABEL_EI
void swap_endian_disklabel(struct disklabel *, struct disklabel *);
uint16_t dkcksum_re(struct disklabel *);
#endif
#ifdef COMPAT_MMEYE_OLDLABEL
void swap_mmeye_disklabel(struct disklabel *, struct disklabel *);
uint16_t dkcksum_mmeye(struct disklabel *);
#endif
static struct mbr_partition *mbr_findslice(struct mbr_partition *,
struct buf *);
#ifdef BSDDISKLABEL_EI
void
swap_endian_disklabel(struct disklabel *nlp, struct disklabel *olp)
{
int i;
#define SW16(X) nlp->X = bswap16(olp->X)
#define SW32(X) nlp->X = bswap32(olp->X)
SW32(d_magic);
SW16(d_type);
SW16(d_subtype);
memcpy(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename));
memcpy(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname));
SW32(d_secsize);
SW32(d_nsectors);
SW32(d_ntracks);
SW32(d_ncylinders);
SW32(d_secpercyl);
SW32(d_secperunit);
SW16(d_sparespertrack);
SW16(d_sparespercyl);
SW32(d_acylinders);
SW16(d_rpm);
SW16(d_interleave);
SW16(d_trackskew);
SW16(d_cylskew);
SW32(d_headswitch);
SW32(d_trkseek);
SW32(d_flags);
for (i = 0; i < NDDATA; i++)
SW32(d_drivedata[i]);
for (i = 0; i < NSPARE; i++)
SW32(d_spare[i]);
SW32(d_magic2);
SW16(d_checksum);
SW16(d_npartitions);
SW32(d_bbsize);
SW32(d_sbsize);
for (i = 0; i < MAXPARTITIONS; i++) {
SW32(d_partitions[i].p_size);
SW32(d_partitions[i].p_offset);
SW32(d_partitions[i].p_fsize);
nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
SW16(d_partitions[i].p_cpg);
}
#undef SW32
#undef SW16
}
uint16_t
dkcksum_re(struct disklabel *lp)
{
uint16_t *start, *end;
uint16_t sum = 0;
start = (uint16_t *)lp;
end = (uint16_t *)&lp->d_partitions[bswap16(lp->d_npartitions)];
while (start < end)
sum ^= *start++;
return (sum);
}
#endif
#ifdef COMPAT_MMEYE_OLDLABEL
void
swap_mmeye_disklabel(struct disklabel *nlp, struct disklabel *olp)
{
int i;
uint16_t *np, *op;
#if BYTE_ORDER == BIG_ENDIAN
#define SW16(X) nlp->X = bswap16(olp->X)
#define SW32(X) nlp->X = bswap32(olp->X)
#else
#define SW16(X) nlp->X = olp->X
#define SW32(X) nlp->X = olp->X
#endif
SW32(d_magic);
SW16(d_type);
SW16(d_subtype);
op = (uint16_t *)&olp->d_typename[0];
np = (uint16_t *)&nlp->d_typename[0];
for (i = 0; i < sizeof(olp->d_typename) / sizeof(uint16_t); i++)
*np++ = bswap16(*op++);
op = (uint16_t *)&olp->d_un.un_d_packname[0];
np = (uint16_t *)&nlp->d_un.un_d_packname[0];
for (i = 0; i < sizeof(olp->d_un) / sizeof(uint16_t); i++)
*np++ = bswap16(*op++);
SW32(d_secsize);
SW32(d_nsectors);
SW32(d_ntracks);
SW32(d_ncylinders);
SW32(d_secpercyl);
SW32(d_secperunit);
SW16(d_sparespertrack);
SW16(d_sparespercyl);
SW32(d_acylinders);
SW16(d_rpm);
SW16(d_interleave);
SW16(d_trackskew);
SW16(d_cylskew);
SW32(d_headswitch);
SW32(d_trkseek);
SW32(d_flags);
for (i = 0; i < NDDATA; i++)
SW32(d_drivedata[i]);
for (i = 0; i < NSPARE; i++)
SW32(d_spare[i]);
SW32(d_magic2);
SW16(d_checksum);
SW16(d_npartitions);
SW32(d_bbsize);
SW32(d_sbsize);
for (i = 0; i < MAXPARTITIONS; i++) {
SW32(d_partitions[i].p_size);
SW32(d_partitions[i].p_offset);
SW32(d_partitions[i].p_fsize);
nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
SW16(d_partitions[i].p_cpg);
}
#undef SW32
#undef SW16
}
uint16_t
dkcksum_mmeye(struct disklabel *lp)
{
struct disklabel tdl;
int i, offset;
uint16_t *start, *end, *fstype;
uint16_t sum = 0;
tdl = *lp;
for (i = 0; i < MAXPARTITIONS; i++) {
fstype = (uint16_t *)&tdl.d_partitions[i].p_fstype;
*fstype = bswap16(*fstype);
}
offset = offsetof(struct disklabel,
d_partitions[le16toh(lp->d_npartitions)]);
start = (uint16_t *)&tdl;
end = start + offset;
while (start < end)
sum ^= *start++;
#ifdef COMPAT_MMEYE_OLDLABEL_BROKEN
if (sum != 0 &&
tdl.d_checksum == 0) {
printf("disklabel: mmeye oldlabel broken found\n");
sum = 0;
}
#endif
return (sum);
}
#endif
static struct mbr_partition *
mbr_findslice(struct mbr_partition *dp, struct buf *bp)
{
struct mbr_partition *ourdp = NULL;
uint16_t *mbrmagicp;
int i;
mbrmagicp = (uint16_t *)((char *)bp->b_data + MBR_MAGIC_OFFSET);
if (le16toh(*mbrmagicp) != MBR_MAGIC)
return (NO_MBR_SIGNATURE);
memcpy(dp, (char *)bp->b_data + MBR_PART_OFFSET,
MBR_PART_COUNT * sizeof(*dp));
for (i = 0; i < MBR_PART_COUNT; i++) {
if (dp[i].mbrp_type == MBR_PTYPE_NETBSD) {
ourdp = &dp[i];
break;
}
}
return (ourdp);
}
const char *
readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
struct cpu_disklabel *osdep)
{
struct mbr_partition *dp;
struct partition *pp;
struct buf *bp;
struct disklabel *dlp;
const char *msg = NULL;
int dospartoff, cyl, i, *ip;
if (lp->d_secsize == 0)
lp->d_secsize = DEV_BSIZE;
if (lp->d_secperunit == 0)
lp->d_secperunit = 0x1fffffff;
#if 0
if (lp->d_ncylinders == 16383) {
printf("disklabel: Disk > 8G ... readjusting chs %d/%d/%d to ",
lp->d_ncylinders, lp->d_ntracks, lp->d_nsectors);
lp->d_ncylinders = lp->d_secperunit / lp->d_ntracks / lp->d_nsectors;
printf("%d/%d/%d\n",
lp->d_ncylinders, lp->d_ntracks, lp->d_nsectors);
}
#endif
lp->d_npartitions = RAW_PART + 1;
for (i = 0; i < RAW_PART; i++) {
lp->d_partitions[i].p_size = 0;
lp->d_partitions[i].p_offset = 0;
}
if (lp->d_partitions[i].p_size == 0)
lp->d_partitions[i].p_size = 0x1fffffff;
lp->d_partitions[i].p_offset = 0;
bp = geteblk((int)lp->d_secsize);
bp->b_dev = dev;
dospartoff = 0;
cyl = LABELSECTOR / lp->d_secpercyl;
if (!osdep)
goto nombrpart;
dp = osdep->mbrparts;
bp->b_blkno = MBR_BBSECTOR;
bp->b_bcount = lp->d_secsize;
bp->b_flags |= B_READ;
bp->b_cylinder = MBR_BBSECTOR / lp->d_secpercyl;
(*strat)(bp);
if (biowait(bp)) {
msg = "dos partition I/O error";
goto done;
} else {
struct mbr_partition *ourdp = NULL;
ourdp = mbr_findslice(dp, bp);
if (ourdp == NO_MBR_SIGNATURE)
goto nombrpart;
for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
pp = &lp->d_partitions[RAW_PART + 1 + i];
pp->p_offset = le32toh(dp->mbrp_start);
pp->p_size = le32toh(dp->mbrp_size);
for (ip = fat_types; *ip != -1; ip++) {
if (dp->mbrp_type == *ip)
pp->p_fstype = FS_MSDOS;
}
if (dp->mbrp_type == MBR_PTYPE_LNXEXT2)
pp->p_fstype = FS_EX2FS;
if (dp->mbrp_type == MBR_PTYPE_NTFS)
pp->p_fstype = FS_NTFS;
if (dp == ourdp) {
dospartoff = le32toh(dp->mbrp_start);
cyl = MBR_PCYL(dp->mbrp_scyl, dp->mbrp_ssect);
lp->d_partitions[2].p_size =
le32toh(dp->mbrp_size);
lp->d_partitions[2].p_offset =
le32toh(dp->mbrp_start);
#if 0
if (lp->d_ntracks != dp->mbrp_ehd + 1 ||
lp->d_nsectors != MBR_PSECT(dp->mbrp_esect)) {
printf("disklabel: BIOS sees chs %d/%d/%d as ",
lp->d_ncylinders, lp->d_ntracks,
lp->d_nsectors);
lp->d_ntracks = dp->mbrp_ehd + 1;
lp->d_nsectors = MBR_PSECT(dp->mbrp_esect);
lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
lp->d_ncylinders = lp->d_secperunit / lp->d_secpercyl;
if (! lp->d_ncylinders)
lp->d_ncylinders = 1;
printf("%d/%d/%d\n",
lp->d_ncylinders, lp->d_ntracks,
lp->d_nsectors);
}
#endif
}
}
lp->d_npartitions = RAW_PART + 1 + i;
}
nombrpart:
bp->b_blkno = dospartoff + LABELSECTOR;
bp->b_cylinder = cyl;
bp->b_bcount = lp->d_secsize;
bp->b_oflags &= ~(BO_DONE);
bp->b_flags |= B_READ;
(*strat)(bp);
if (biowait(bp)) {
msg = "disk label I/O error";
goto done;
}
for (dlp = (struct disklabel *)bp->b_data;
dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize -
sizeof(*dlp));
dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC) {
if (dlp->d_npartitions > MAXPARTITIONS ||
dkcksum(dlp) != 0)
msg = "disk label corrupted";
else {
*lp = *dlp;
msg = NULL;
break;
}
}
#ifdef BSDDISKLABEL_EI
if (bswap32(dlp->d_magic) == DISKMAGIC &&
bswap32(dlp->d_magic2) == DISKMAGIC) {
if (bswap16(dlp->d_npartitions) > MAXPARTITIONS ||
dkcksum_re(dlp) != 0)
msg = "disk label corrupted";
else {
swap_endian_disklabel(lp, dlp);
lp->d_checksum = 0;
lp->d_checksum = dkcksum(lp);
msg = NULL;
break;
}
}
#endif
#ifdef COMPAT_MMEYE_OLDLABEL
if (le32toh(dlp->d_magic) == DISKMAGIC &&
le32toh(dlp->d_magic2) == DISKMAGIC) {
if (le16toh(dlp->d_npartitions) > MAXPARTITIONS ||
dkcksum_mmeye(dlp) != 0)
msg = "disk label corrupted";
else {
swap_mmeye_disklabel(lp, dlp);
lp->d_checksum = 0;
lp->d_checksum = dkcksum(lp);
msg = NULL;
break;
}
}
#endif
if (msg == NULL)
msg = "no disk label";
}
done:
brelse(bp, 0);
return (msg);
}
int
writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
struct cpu_disklabel *osdep)
{
struct mbr_partition *dp;
struct buf *bp;
struct disklabel *dlp;
int error, dospartoff, cyl;
bp = geteblk((int)lp->d_secsize);
bp->b_dev = dev;
dospartoff = 0;
cyl = LABELSECTOR / lp->d_secpercyl;
if (!osdep)
goto nombrpart;
dp = osdep->mbrparts;
bp->b_blkno = MBR_BBSECTOR;
bp->b_bcount = lp->d_secsize;
bp->b_flags |= B_READ;
bp->b_cylinder = MBR_BBSECTOR / lp->d_secpercyl;
(*strat)(bp);
if ((error = biowait(bp)) == 0) {
struct mbr_partition *ourdp = NULL;
ourdp = mbr_findslice(dp, bp);
if (ourdp == NO_MBR_SIGNATURE)
goto nombrpart;
if (ourdp) {
dospartoff = le32toh(ourdp->mbrp_start);
cyl = MBR_PCYL(ourdp->mbrp_scyl, ourdp->mbrp_ssect);
}
}
nombrpart:
#ifdef maybe
if (lp->d_partitions[2].p_offset != 0
&& lp->d_partitions[2].p_offset != dospartoff) {
error = EXDEV;
goto done;
}
#endif
bp->b_blkno = dospartoff + LABELSECTOR;
bp->b_cylinder = cyl;
bp->b_bcount = lp->d_secsize;
bp->b_oflags &= ~(BO_DONE);
bp->b_flags |= B_READ;
(*strat)(bp);
if ((error = biowait(bp)) != 0)
goto done;
for (dlp = (struct disklabel *)bp->b_data;
dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize -
sizeof(*dlp));
dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
dlp->d_npartitions <= MAXPARTITIONS &&
dkcksum(dlp) == 0) {
*dlp = *lp;
goto found;
#ifdef BSDDISKLABEL_EI
} else if (bswap32(dlp->d_magic) == DISKMAGIC &&
bswap32(dlp->d_magic2) == DISKMAGIC &&
bswap16(dlp->d_npartitions) <= MAXPARTITIONS &&
dkcksum_re(dlp) == 0) {
swap_endian_disklabel(dlp, lp);
dlp->d_checksum = 0;
dlp->d_checksum = dkcksum_re(dlp);
goto found;
#endif
#ifdef COMPAT_MMEYE_OLDLABEL
} else if (le32toh(dlp->d_magic) == DISKMAGIC &&
le32toh(dlp->d_magic2) == DISKMAGIC &&
le16toh(dlp->d_npartitions) <= MAXPARTITIONS &&
dkcksum_mmeye(dlp) == 0) {
swap_mmeye_disklabel(dlp, lp);
dlp->d_checksum = 0;
dlp->d_checksum = dkcksum_mmeye(dlp);
goto found;
#endif
}
}
error = ESRCH;
goto done;
found:
bp->b_oflags &= ~(BO_DONE);
bp->b_flags &= ~(B_READ);
bp->b_flags |= B_WRITE;
(*strat)(bp);
error = biowait(bp);
done:
brelse(bp, 0);
return (error);
}