#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.62 2026/07/14 13:34:37 thorpej Exp $");
#ifdef _KERNEL_OPT
#include "opt_mbr.h"
#include "opt_disklabel.h"
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <sys/bootblock.h>
#include <sys/buf.h>
#include <sys/cdio.h>
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/fcntl.h>
#include <sys/kauth.h>
#include <sys/sdt.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <fs/udf/ecma167-udf.h>
typedef struct mbr_partition mbr_partition_t;
#define SCANBLOCKS 3
#define DISKLABEL_SIZE 404
#if LABELSECTOR*DEV_BSIZE + LABELOFFSET > SCANBLOCKS*DEV_BSIZE - DISKLABEL_SIZE
#if _MACHINE != ews4800mips
#error Invalid LABELSECTOR or LABELOFFSET
#endif
#endif
#define MBR_LABELSECTOR 1
#define SCAN_CONTINUE 0
#define SCAN_FOUND 1
#define SCAN_ERROR 2
typedef struct mbr_args {
struct disklabel *lp;
void (*strat)(struct buf *);
struct buf *bp;
const char *msg;
int error;
int written;
int found_mbr;
uint label_sector;
int action;
uint32_t secperunit;
#define READ_LABEL 1
#define UPDATE_LABEL 2
#define WRITE_LABEL 3
} mbr_args_t;
static int validate_label(mbr_args_t *, uint);
static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
static int
read_sector(mbr_args_t *a, uint sector, int count)
{
int error;
error = disk_read_sectors(a->strat, a->lp, a->bp, sector, count);
if (error != 0)
a->error = error;
return error;
}
static int
scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
{
mbr_partition_t ptns[MBR_PART_COUNT];
mbr_partition_t *dp;
struct mbr_sector *mbr;
uint ext_base, this_ext, next_ext;
int rval;
int i;
int j;
#ifdef COMPAT_386BSD_MBRPART
int dp_386bsd = -1;
int ap_386bsd = -1;
#endif
ext_base = 0;
this_ext = 0;
for (;;) {
if (read_sector(a, this_ext, 1)) {
a->msg = "dos partition I/O error";
return SCAN_ERROR;
}
mbr = (void *)a->bp->b_data;
if (mbr->mbr_magic != htole16(MBR_MAGIC))
return SCAN_CONTINUE;
if (mbr->mbr_parts[0].mbrp_type == MBR_PTYPE_PMBR
&& mbr->mbr_parts[1].mbrp_type == MBR_PTYPE_UNUSED
&& mbr->mbr_parts[2].mbrp_type == MBR_PTYPE_UNUSED
&& mbr->mbr_parts[3].mbrp_type == MBR_PTYPE_UNUSED)
return SCAN_CONTINUE;
memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
if (ext_base == 0 && ptns[0].mbrp_type == MBR_PTYPE_DM6_DDO) {
bool ok = true;
for (i = 1; i < MBR_PART_COUNT; i++)
if (ptns[i].mbrp_type != MBR_PTYPE_UNUSED)
ok = false;
if (ok) {
this_ext = le32toh(a->lp->d_secpercyl /
a->lp->d_ntracks);
continue;
}
}
next_ext = 0;
dp = ptns;
j = 0;
for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
if (dp->mbrp_type == MBR_PTYPE_UNUSED)
continue;
if ((uint64_t)ext_base + le32toh(dp->mbrp_start) +
le32toh(dp->mbrp_size) > a->lp->d_secperunit) {
a->msg = "mbr partition exceeds disk size";
return SCAN_CONTINUE;
}
a->found_mbr = 1;
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 (this_ext == 0) {
dp_386bsd = i;
ap_386bsd = j;
}
continue;
}
#endif
rval = (*actn)(a, dp, j, this_ext);
if (rval != SCAN_CONTINUE)
return rval;
j++;
}
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], ap_386bsd, 0);
#endif
return SCAN_CONTINUE;
}
static void
scan_iso_vrs_session(mbr_args_t *a, uint32_t first_sector,
int *is_iso9660, int *is_udf)
{
struct vrs_desc *vrsd;
uint64_t vrs;
int sector_size;
int inc;
sector_size = a->lp->d_secsize;
inc = MAX(1, 2048 / sector_size);
vrs = ((32*1024 + sector_size - 1) / sector_size)
+ first_sector;
if (read_sector(a, vrs, 1))
return;
vrsd = a->bp->b_data;
while (memcmp(vrsd->identifier, "CD001", 5) == 0) {
*is_iso9660 = first_sector;
vrs += inc;
if (read_sector(a, vrs, 1))
return;
}
vrsd = a->bp->b_data;
if (memcmp(vrsd->identifier, "BEA01", 5))
return;
vrs += inc;
if (read_sector(a, vrs, 1))
return;
vrsd = a->bp->b_data;
if (memcmp(vrsd->identifier, "NSR0", 4))
return;
*is_udf = first_sector;
}
static int
scan_iso_vrs(mbr_args_t *a)
{
struct mmc_discinfo di;
struct mmc_trackinfo ti;
dev_t dev;
uint64_t sector;
int is_iso9660, is_udf;
int tracknr, sessionnr;
int new_session, error;
is_iso9660 = is_udf = -1;
if (a->lp->d_flags & D_SCSI_MMC) {
dev = a->bp->b_dev;
error = bdev_ioctl(dev, MMCGETDISCINFO, &di, FKIOCTL, curlwp);
if (error)
goto notmmc;
sessionnr = -1;
for (tracknr = di.first_track;
tracknr <= di.first_track_last_session; tracknr++)
{
ti.tracknr = tracknr;
error = bdev_ioctl(dev, MMCGETTRACKINFO, &ti,
FKIOCTL, curlwp);
if (error)
goto notmmc;
new_session = (ti.sessionnr != sessionnr);
sessionnr = ti.sessionnr;
if (new_session) {
if (ti.flags & MMC_TRACKINFO_BLANK)
continue;
if (!(ti.flags & MMC_TRACKINFO_DATA))
continue;
sector = ti.track_start;
scan_iso_vrs_session(a, sector,
&is_iso9660, &is_udf);
}
}
} else {
notmmc:
sector = 0;
scan_iso_vrs_session(a, sector, &is_iso9660, &is_udf);
}
if ((is_iso9660 < 0) && (is_udf < 0))
return SCAN_CONTINUE;
strncpy(a->lp->d_typename, "iso partition", 16);
if (is_iso9660 >= 0) {
a->lp->d_partitions[0].p_offset = 0;
a->lp->d_partitions[0].p_size = a->lp->d_secperunit;
a->lp->d_partitions[0].p_cdsession = is_iso9660;
a->lp->d_partitions[0].p_fstype = FS_ISO9660;
}
return SCAN_FOUND;
}
const char *
readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
struct cpu_disklabel *osdep)
{
int rval;
int i;
mbr_args_t a;
memset(&a, 0, sizeof a);
a.lp = lp;
a.strat = strat;
a.action = READ_LABEL;
if (lp->d_secsize == 0)
lp->d_secsize = DEV_BSIZE;
if (lp->d_secperunit == 0)
lp->d_secperunit = 0x1fffffff;
a.secperunit = lp->d_secperunit;
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[RAW_PART].p_size == 0)
lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
lp->d_partitions[RAW_PART].p_offset = 0;
lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
lp->d_partitions[0].p_fstype = FS_BSDFFS;
a.bp = geteblk(SCANBLOCKS * (int)lp->d_secsize);
a.bp->b_dev = dev;
if (osdep)
rval = scan_mbr(&a, look_netbsd_part);
else
rval = SCAN_CONTINUE;
if (rval == SCAN_CONTINUE) {
rval = validate_label(&a, 0);
}
if (rval == SCAN_CONTINUE) {
rval = scan_iso_vrs(&a);
}
#if 0
if (rval == SCAN_FOUND)
xxx->label_sector = a.label_sector;
#endif
brelse(a.bp, 0);
if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
return a.msg;
return NULL;
}
static int
look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
{
struct partition *pp;
int ptn_base = ext_base + le32toh(dp->mbrp_start);
int rval;
if (
#ifdef COMPAT_386BSD_MBRPART
dp->mbrp_type == MBR_PTYPE_386BSD ||
#endif
dp->mbrp_type == MBR_PTYPE_NETBSD) {
rval = validate_label(a, ptn_base);
#if RAW_PART == 3
if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
a->lp->d_partitions[2].p_offset = ptn_base;
}
#endif
if (rval == SCAN_FOUND)
return rval;
}
if (ext_base == 0)
slot += 4;
else {
slot = 4 + MBR_PART_COUNT;
pp = &a->lp->d_partitions[slot];
for (; slot < MAXPARTITIONS; pp++, slot++) {
if (pp->p_offset == ptn_base &&
pp->p_size == le32toh(dp->mbrp_size))
break;
if (pp->p_size == 0)
break;
}
}
if (slot < MAXPARTITIONS) {
a->lp->d_partitions[0].p_size = 0;
a->lp->d_partitions[0].p_fstype = 0;
pp = &a->lp->d_partitions[slot];
pp->p_offset = ptn_base;
pp->p_size = le32toh(dp->mbrp_size);
pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
if (slot >= a->lp->d_npartitions)
a->lp->d_npartitions = slot + 1;
}
return SCAN_CONTINUE;
}
__noubsan
static bool
check_label_magic(const struct disklabel *dlp, uint32_t diskmagic)
{
return memcmp(&dlp->d_magic, &diskmagic, sizeof(diskmagic)) == 0 &&
memcmp(&dlp->d_magic2, &diskmagic, sizeof(diskmagic)) == 0;
}
#ifdef DISKLABEL_EI
#endif
static int
validate_label(mbr_args_t *a, uint label_sector)
{
struct disklabel *dlp;
char *dlp_lim, *dlp_byte;
int error;
#ifdef DISKLABEL_EI
int swapped = 0;
uint16_t npartitions;
#endif
if (read_sector(a, label_sector, SCANBLOCKS)) {
a->msg = "disk label read failed";
return SCAN_ERROR;
}
dlp = (void *)a->bp->b_data;
dlp_lim = (char *)a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
for (;; dlp = (void *)((char *)dlp + sizeof(uint32_t))) {
if ((char *)dlp > dlp_lim) {
if (a->action != WRITE_LABEL)
return SCAN_CONTINUE;
dlp_byte = (char *)a->bp->b_data + LABELOFFSET;
if (label_sector)
dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
else
dlp_byte += LABELSECTOR * a->lp->d_secsize;
dlp = (void *)dlp_byte;
break;
}
if (!check_label_magic(dlp, DISKMAGIC))
#ifdef DISKLABEL_EI
{
if (!check_label_magic(dlp, bswap32(DISKMAGIC)))
continue;
npartitions = bswap16(dlp->d_npartitions);
if (npartitions > MAXPARTITIONS ||
dkcksum_sized(dlp, npartitions) != 0)
goto corrupted;
swapped = 1;
}
#else
continue;
#endif
else if (dlp->d_npartitions > MAXPARTITIONS ||
dkcksum(dlp) != 0) {
#ifdef DISKLABEL_EI
corrupted:
#endif
a->msg = "disk label corrupted";
continue;
}
break;
}
switch (a->action) {
case READ_LABEL:
#ifdef DISKLABEL_EI
if (swapped)
disklabel_swap(a->lp, dlp);
else
*a->lp = *dlp;
#else
*a->lp = *dlp;
#endif
if ((a->msg = convertdisklabel(a->lp, a->strat, a->bp,
a->secperunit)) != NULL)
return SCAN_ERROR;
a->label_sector = label_sector;
return SCAN_FOUND;
case UPDATE_LABEL:
case WRITE_LABEL:
#ifdef DISKLABEL_EI
if (swapped)
disklabel_swap(dlp, a->lp);
else
*dlp = *a->lp;
#else
*dlp = *a->lp;
#endif
a->bp->b_oflags &= ~BO_DONE;
a->bp->b_flags &= ~B_READ;
a->bp->b_flags |= B_WRITE;
(*a->strat)(a->bp);
error = biowait(a->bp);
if (error != 0) {
a->error = error;
a->msg = "disk label write failed";
return SCAN_ERROR;
}
a->written++;
return SCAN_CONTINUE;
default:
return SCAN_ERROR;
}
}
int
writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
struct cpu_disklabel *osdep)
{
mbr_args_t a;
memset(&a, 0, sizeof a);
a.lp = lp;
a.strat = strat;
a.bp = geteblk(SCANBLOCKS * (int)lp->d_secsize);
a.bp->b_dev = dev;
a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
scan_mbr(&a, write_netbsd_label);
a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
validate_label(&a, 0);
if (a.written == 0 && a.error == 0)
a.error = SET_ERROR(ESRCH);
brelse(a.bp, 0);
return a.error;
}
static int
write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
{
int ptn_base = ext_base + le32toh(dp->mbrp_start);
if (dp->mbrp_type != MBR_PTYPE_NETBSD)
return SCAN_CONTINUE;
return validate_label(a, ptn_base);
}