#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <dev/sun/disklabel.h>
#include "cd.h"
static int disklabel_sun_to_bsd(dev_t dev, struct sun_disklabel *,
struct disklabel *);
static int disklabel_bsd_to_sun(struct disklabel *, struct sun_disklabel *);
static __inline u_int sun_extended_sum(struct sun_disklabel *, void *);
#if NCD > 0
extern void cdstrategy(struct buf *);
#endif
int
readdisklabel(dev_t dev, void (*strat)(struct buf *),
struct disklabel *lp, int spoofonly)
{
struct sun_disklabel *slp;
struct buf *bp = NULL;
int error;
if ((error = initdisklabel(lp)))
goto done;
lp->d_flags |= D_VENDOR;
#if NCD > 0
if (strat == cdstrategy) {
#if defined(CD9660)
if (iso_disklabelspoof(dev, strat, lp) == 0)
goto done;
#endif
#if defined(UDF)
if (udf_disklabelspoof(dev, strat, lp) == 0)
goto done;
#endif
}
#endif
bp = geteblk(lp->d_secsize);
bp->b_dev = dev;
if (spoofonly)
goto doslabel;
error = readdisksector(bp, strat, lp, DL_BLKTOSEC(lp, LABELSECTOR));
if (error)
goto done;
slp = (struct sun_disklabel *)bp->b_data;
if (slp->sl_magic == SUN_DKMAGIC) {
error = disklabel_sun_to_bsd(bp->b_dev, slp, lp);
goto done;
}
error = checkdisklabel(bp->b_dev, bp->b_data + LABELOFFSET, lp, 0,
DL_GETDSIZE(lp));
if (error == 0)
goto done;
doslabel:
error = readdoslabel(bp, strat, lp, NULL, spoofonly);
if (error == 0)
goto done;
#if defined(CD9660)
error = iso_disklabelspoof(dev, strat, lp);
if (error == 0)
goto done;
#endif
#if defined(UDF)
error = udf_disklabelspoof(dev, strat, lp);
if (error == 0)
goto done;
#endif
done:
if (bp) {
bp->b_flags |= B_INVAL;
brelse(bp);
}
disk_change = 1;
return (error);
}
int
writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
{
struct buf *bp = NULL;
int error;
bp = geteblk(lp->d_secsize);
bp->b_dev = dev;
error = disklabel_bsd_to_sun(lp, (struct sun_disklabel *)bp->b_data);
if (error)
goto done;
bp->b_blkno = LABELSECTOR;
bp->b_bcount = lp->d_secsize;
CLR(bp->b_flags, B_READ | B_WRITE | B_DONE);
SET(bp->b_flags, B_BUSY | B_WRITE | B_RAW);
(*strat)(bp);
error = biowait(bp);
done:
if (bp) {
bp->b_flags |= B_INVAL;
brelse(bp);
}
disk_change = 1;
return (error);
}
static u_char
sun_fstypes[16] = {
FS_BSDFFS,
FS_SWAP,
FS_UNUSED,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS,
FS_BSDFFS
};
static __inline u_int
sun_extended_sum(struct sun_disklabel *sl, void *end)
{
u_int sum, *xp, *ep;
xp = (u_int *)&sl->sl_xpmag;
ep = (u_int *)end;
sum = 0;
for (; xp < ep; xp++)
sum += *xp;
return (sum);
}
static int
disklabel_sun_to_bsd(dev_t dev, struct sun_disklabel *sl, struct disklabel *lp)
{
struct sun_preamble *preamble = (struct sun_preamble *)sl;
struct sun_partinfo *ppp;
struct sun_dkpart *spp;
struct partition *npp;
u_short cksum = 0, *sp1, *sp2;
int i, secpercyl;
sp1 = (u_short *)sl;
sp2 = (u_short *)(sl + 1);
while (sp1 < sp2)
cksum ^= *sp1++;
if (cksum != 0)
return (EINVAL);
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_flags = D_VENDOR;
memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
lp->d_secsize = DEV_BSIZE;
lp->d_nsectors = sl->sl_nsectors;
lp->d_ntracks = sl->sl_ntracks;
lp->d_ncylinders = sl->sl_ncylinders;
secpercyl = sl->sl_nsectors * sl->sl_ntracks;
lp->d_secpercyl = secpercyl;
if (DL_GETDSIZE(lp) == 0 || DL_GETDSIZE(lp) == MAXDISKSIZE)
DL_SETDSIZE(lp, (u_int64_t)secpercyl * sl->sl_ncylinders);
lp->d_version = 1;
memcpy(&lp->d_uid, &sl->sl_uid, sizeof(lp->d_uid));
lp->d_acylinders = sl->sl_acylinders;
lp->d_npartitions = MAXPARTITIONS16;
for (i = 0; i < 8; i++) {
spp = &sl->sl_part[i];
npp = &lp->d_partitions[i];
DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl);
DL_SETPSIZE(npp, spp->sdkp_nsectors);
if (DL_GETPSIZE(npp) == 0) {
npp->p_fstype = FS_UNUSED;
} else {
npp->p_fstype = sun_fstypes[i];
if (npp->p_fstype == FS_BSDFFS) {
npp->p_fragblock =
DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
npp->p_cpg = 16;
}
}
}
for (i = 0; i < SUNXPART; i++) {
npp = &lp->d_partitions[i+8];
DL_SETPOFFSET(npp, 0);
DL_SETPSIZE(npp, 0);
npp->p_fstype = FS_UNUSED;
}
if ((sl->sl_xpmag == SL_XPMAG &&
sun_extended_sum(sl, &sl->sl_types) == sl->sl_xpsum) ||
(sl->sl_xpmag == SL_XPMAGTYP &&
sun_extended_sum(sl, &sl->sl_uid) == sl->sl_xpsum) ||
(sl->sl_xpmag == SL_XPMAGTYP &&
sun_extended_sum(sl, &sl->sl_xxx1) == sl->sl_xpsum)) {
for (i = 0; i < SUNXPART; i++) {
spp = &sl->sl_xpart[i];
npp = &lp->d_partitions[i+8];
DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl);
DL_SETPSIZE(npp, spp->sdkp_nsectors);
if (DL_GETPSIZE(npp) == 0) {
npp->p_fstype = FS_UNUSED;
continue;
}
npp->p_fstype = sun_fstypes[i+8];
if (npp->p_fstype == FS_BSDFFS) {
npp->p_fragblock =
DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
npp->p_cpg = 16;
}
}
if (sl->sl_xpmag == SL_XPMAGTYP) {
for (i = 0; i < MAXPARTITIONS16; i++) {
npp = &lp->d_partitions[i];
npp->p_fstype = sl->sl_types[i];
npp->p_fragblock = sl->sl_fragblock[i];
npp->p_cpg = sl->sl_cpg[i];
}
}
} else if (preamble->sl_nparts <= 8) {
i = preamble->sl_nparts;
if (i == 0)
i = 8;
npp = &lp->d_partitions[i-1];
ppp = &preamble->sl_part[i-1];
for (; i > 0; i--, npp--, ppp--) {
if (npp->p_size == 0)
continue;
if ((ppp->spi_tag == 0) && (ppp->spi_flag == 0))
continue;
switch (ppp->spi_tag) {
case SPTAG_SUNOS_ROOT:
case SPTAG_SUNOS_USR:
case SPTAG_SUNOS_VAR:
case SPTAG_SUNOS_HOME:
npp->p_fstype = FS_BSDFFS;
npp->p_fragblock =
DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
npp->p_cpg = 16;
break;
case SPTAG_LINUX_EXT2:
npp->p_fstype = FS_EXT2FS;
break;
default:
npp->p_fstype = FS_UNUSED;
break;
}
}
}
lp->d_checksum = 0;
lp->d_checksum = dkcksum(lp);
return (checkdisklabel(dev, lp, lp, 0, DL_GETDSIZE(lp)));
}
static int
disklabel_bsd_to_sun(struct disklabel *lp, struct sun_disklabel *sl)
{
struct partition *npp;
struct sun_dkpart *spp;
int i, secpercyl;
u_short cksum, *sp1, *sp2;
if (lp->d_secsize != DEV_BSIZE || lp->d_nsectors == 0 ||
lp->d_ntracks == 0)
return (EINVAL);
bzero(sl, sizeof(*sl));
memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
sl->sl_pcylinders = lp->d_ncylinders + lp->d_acylinders;
sl->sl_ncylinders = lp->d_ncylinders;
sl->sl_acylinders = lp->d_acylinders;
sl->sl_ntracks = lp->d_ntracks;
sl->sl_nsectors = lp->d_nsectors;
memcpy(&sl->sl_uid, &lp->d_uid, sizeof(lp->d_uid));
secpercyl = sl->sl_nsectors * sl->sl_ntracks;
for (i = 0; i < 8; i++) {
spp = &sl->sl_part[i];
npp = &lp->d_partitions[i];
spp->sdkp_cyloffset = 0;
spp->sdkp_nsectors = 0;
if (DL_GETPSIZE(npp)) {
if (DL_GETPOFFSET(npp) % secpercyl)
return (EINVAL);
spp->sdkp_cyloffset = DL_GETPOFFSET(npp) / secpercyl;
spp->sdkp_nsectors = DL_GETPSIZE(npp);
}
}
sl->sl_magic = SUN_DKMAGIC;
for (i = 0; i < SUNXPART; i++) {
spp = &sl->sl_xpart[i];
npp = &lp->d_partitions[i+8];
spp->sdkp_cyloffset = 0;
spp->sdkp_nsectors = 0;
if (DL_GETPSIZE(npp)) {
if (DL_GETPOFFSET(npp) % secpercyl)
return (EINVAL);
spp->sdkp_cyloffset = DL_GETPOFFSET(npp) / secpercyl;
spp->sdkp_nsectors = DL_GETPSIZE(npp);
}
}
for (i = 0; i < MAXPARTITIONS16; i++) {
npp = &lp->d_partitions[i];
sl->sl_types[i] = npp->p_fstype;
sl->sl_fragblock[i] = npp->p_fragblock;
sl->sl_cpg[i] = npp->p_cpg;
}
sl->sl_xpmag = SL_XPMAGTYP;
sl->sl_xpsum = sun_extended_sum(sl, &sl->sl_xxx1);
sp1 = (u_short *)sl;
sp2 = (u_short *)(sl + 1);
sl->sl_cksum = cksum = 0;
while (sp1 < sp2)
cksum ^= *sp1++;
sl->sl_cksum = cksum;
return (0);
}