#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <unistd.h>
#include <stdlib.h>
#include <libintl.h>
#include <locale.h>
#include <langinfo.h>
#include <regex.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/dktp/fdisk.h>
#include <sys/dkio.h>
#include <sys/fs/pc_fs.h>
#include <sys/fs/pc_dir.h>
#include <sys/fs/pc_label.h>
#include "pcfs_common.h"
#include "pcfs_bpb.h"
#ifdef _BIG_ENDIAN
void swap_pack_grab32bpb(bpb_t *wbpb, struct _boot_sector *bsp);
void swap_pack_grabbpb(bpb_t *wbpb, struct _boot_sector *bsp);
#endif
bool
is_sector_size_valid(size_t size)
{
if (size != 512 && size != 1024 && size != 2048 && size != 4096)
return (false);
return (true);
}
int
get_media_sector_size(int fd, size_t *sizep)
{
struct dk_minfo dkminfo;
if (ioctl(fd, DKIOCGMEDIAINFO, &dkminfo) != -1) {
*sizep = dkminfo.dki_lbsize;
return (0);
}
if (errno == ENOTTY) {
*sizep = MINBPS;
return (0);
}
return (errno);
}
void
store_16_bits(uchar_t **bp, uint32_t v)
{
uchar_t *l = *bp;
*l++ = v & 0xff;
*l = (v >> 8) & 0xff;
*bp += 2;
}
void
read_16_bits(uchar_t *bp, uint32_t *value)
{
*value = *bp++;
*value += *bp << 8;
}
void
store_32_bits(uchar_t **bp, uint32_t v)
{
uchar_t *l = *bp;
int b;
for (b = 0; b < 4; b++) {
*l++ = v & 0xff;
v = v >> 8;
}
*bp += 4;
}
void
read_32_bits(uchar_t *bp, uint32_t *value)
{
*value = *bp++;
*value += *bp++ << 8;
*value += *bp++ << 16;
*value += *bp++ << 24;
}
#define BPL 16
void
dump_bytes(uchar_t *buf, int n)
{
int printedCount;
int countdown = n;
int countup = 0;
int offset = 0;
int byte;
while (countdown > 0) {
printedCount = 0;
(void) fprintf(stderr, "\n%06x: ", offset);
for (byte = 0; byte < BPL; byte++) {
if (countup + byte < n) {
(void) fprintf(stderr,
"%02x ", (buf[countup + byte] & 0xff));
printedCount++;
} else {
(void) fprintf(stderr, " ");
}
}
for (byte = 0; byte < BPL; byte++) {
if ((countup + byte < n) &&
((buf[countup + byte] >= ' ') &&
(buf[countup + byte] <= '~'))) {
(void) fprintf(stderr, "%c",
buf[countup + byte]);
} else {
(void) fprintf(stderr, ".");
}
}
countup += printedCount;
offset += printedCount;
countdown -= printedCount;
}
(void) fprintf(stderr, "\n\n");
}
void
header_for_dump(void)
{
int byte;
(void) fprintf(stderr, "\n ");
for (byte = 0; byte < BPL; byte++)
(void) fprintf(stderr, "%02x ", byte);
(void) fprintf(stderr, "\n ");
byte = 3 * BPL;
while (byte-- > 0)
(void) fprintf(stderr, "-");
}
#ifdef _BIG_ENDIAN
void
swap_pack_grabbpb(bpb_t *wbpb, struct _boot_sector *bsp)
{
uchar_t *grabp;
grabp = (uchar_t *)&(bsp->bs_filler[ORIG_BPB_START_INDEX]);
((uchar_t *)&(wbpb->bpb.bytes_per_sector))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.bytes_per_sector))[0] = *grabp++;
wbpb->bpb.sectors_per_cluster = *grabp++;
((uchar_t *)&(wbpb->bpb.resv_sectors))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.resv_sectors))[0] = *grabp++;
wbpb->bpb.num_fats = *grabp++;
((uchar_t *)&(wbpb->bpb.num_root_entries))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.num_root_entries))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_volume))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_volume))[0] = *grabp++;
wbpb->bpb.media = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_fat))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_fat))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_track))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_per_track))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.heads))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.heads))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.hidden_sectors))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[0] = *grabp++;
wbpb->ebpb.phys_drive_num = *grabp++;
wbpb->ebpb.reserved = *grabp++;
wbpb->ebpb.ext_signature = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[3] = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[2] = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[1] = *grabp++;
((uchar_t *)&(wbpb->ebpb.volume_id))[0] = *grabp++;
(void) strncpy((char *)wbpb->ebpb.volume_label, (char *)grabp, 11);
grabp += 11;
(void) strncpy((char *)wbpb->ebpb.type, (char *)grabp, 8);
}
void
swap_pack_grab32bpb(bpb_t *wbpb, struct _boot_sector *bsp)
{
uchar_t *grabp;
grabp = (uchar_t *)&(bsp->bs_filler[BPB_32_START_INDEX]);
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.ext_flags))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.ext_flags))[0] = *grabp++;
wbpb->bpb32.fs_vers_lo = *grabp++;
wbpb->bpb32.fs_vers_hi = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[3] = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[2] = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.root_dir_clust))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.fsinfosec))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.fsinfosec))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.backupboot))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.backupboot))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[0]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[0]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[1]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[1]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[2]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[2]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[3]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[3]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[4]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[4]))[0] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[5]))[1] = *grabp++;
((uchar_t *)&(wbpb->bpb32.reserved[5]))[0] = *grabp++;
}
#endif
char *
stat_actual_disk(const char *diskname, struct stat *info, char **suffix)
{
char *actualdisk;
if (stat(diskname, info) != 0) {
if ((actualdisk = strdup(diskname)) == NULL) {
(void) fprintf(stderr,
gettext("Out of memory for disk name.\n"));
exit(2);
}
if ((*suffix = strchr(actualdisk, ':')) != NULL) {
**suffix = '\0';
(*suffix)++;
}
if (stat(actualdisk, info)) {
err(2, "Failed to stat disk device %s",
actualdisk);
}
} else {
if ((actualdisk = strdup(diskname)) == NULL) {
(void) fprintf(stderr,
gettext("Out of memory for disk name.\n"));
exit(2);
}
}
return (actualdisk);
}
extern void usage(void);
void
bad_arg(char *option)
{
(void) fprintf(stderr,
gettext("Unrecognized option -o %s.\n"), option);
usage();
exit(2);
}
void
missing_arg(char *option)
{
(void) fprintf(stderr,
gettext("Option %s requires a value.\n"), option);
usage();
exit(3);
}
static int
parse_drvnum(char *pn)
{
int drvnum;
if ((strlen(pn) == 1) && ((*pn >= 'c') && (*pn <= 'z'))) {
drvnum = *pn - 'c' + 1;
} else if ((*pn >= '0') && (*pn <= '9')) {
char *d;
int v = 0;
d = pn;
while ((*d != '\0') && (*d >= '0') && (*d <= '9')) {
v *= 10;
v += *d - '0';
d++;
}
if ((*d != '\0') || (v > 24)) {
(void) fprintf(stderr,
gettext("%s: bogus logical drive specification.\n"),
pn);
return (-1);
}
drvnum = v;
} else if (strcmp(pn, "boot") == 0) {
drvnum = 99;
} else {
(void) fprintf(stderr,
gettext("%s: bogus logical drive specification.\n"), pn);
return (-1);
}
return (drvnum);
}
static int
isDosDrive(uchar_t checkMe)
{
return ((checkMe == DOSOS12) || (checkMe == DOSOS16) ||
(checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) ||
(checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) ||
(checkMe == DIAGPART));
}
static int
isDosExtended(uchar_t checkMe)
{
return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
}
static int
isBootPart(uchar_t checkMe)
{
return (checkMe == X86BOOT);
}
off64_t
findPartitionOffset(int fd, size_t bpsec, char *ldrive)
{
struct ipart part[FD_NUMPART];
struct mboot extmboot;
struct mboot mb;
diskaddr_t xstartsect;
off64_t nextseek = 0;
off64_t lastseek = 0;
off64_t found = 0;
off64_t error = -1;
int logicalDriveCount = 0;
int extendedPart = -1;
int primaryPart = -1;
int bootPart = -1;
uint32_t xnumsect = 0;
int drvnum;
int driveIndex;
int i;
int extndDrives[FD_NUMPART];
int numDrives = 0;
int extraDrives[FD_NUMPART];
int numExtraDrives = 0;
if ((drvnum = parse_drvnum(ldrive)) < 0)
return (error);
if (read(fd, &mb, bpsec) != (ssize_t)bpsec) {
(void) fprintf(stderr,
gettext("Couldn't read a Master Boot Record\n"));
return (error);
}
if (ltohs(mb.signature) != BOOTSECSIG) {
(void) fprintf(stderr,
gettext("Bad signature on master boot record (%x)\n"),
ltohs(mb.signature));
return (error);
}
(void) memcpy(part, mb.parts, sizeof (part));
for (i = 0; i < FD_NUMPART; i++) {
if (isDosDrive(part[i].systid)) {
if (primaryPart < 0) {
logicalDriveCount++;
primaryPart = i;
} else {
extraDrives[numExtraDrives++] = i;
}
continue;
}
if ((extendedPart < 0) && isDosExtended(part[i].systid)) {
extendedPart = i;
continue;
}
if ((bootPart < 0) && isBootPart(part[i].systid)) {
bootPart = i;
continue;
}
}
if (drvnum == BOOT_PARTITION_DRIVE) {
if (bootPart < 0) {
(void) fprintf(stderr,
gettext("No boot partition found on drive\n"));
return (error);
}
found = ltohi(part[bootPart].relsect) * bpsec;
return (found);
}
if (drvnum == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
found = ltohi(part[primaryPart].relsect) * bpsec;
return (found);
}
if ((extendedPart < 0) && (numExtraDrives == 0)) {
(void) fprintf(stderr,
gettext("No such logical drive "
"(missing extended partition entry)\n"));
return (error);
}
if (extendedPart >= 0) {
nextseek = xstartsect = ltohi(part[extendedPart].relsect);
xnumsect = ltohi(part[extendedPart].numsect);
do {
if (nextseek == lastseek)
break;
logicalDriveCount += numDrives;
if (lseek64(fd, nextseek * bpsec, SEEK_SET) < 0 ||
read(fd, &extmboot, sizeof (extmboot)) !=
sizeof (extmboot)) {
perror(gettext("Unable to read extended "
"partition record"));
return (error);
}
(void) memcpy(part, extmboot.parts, sizeof (part));
lastseek = nextseek;
if (ltohs(extmboot.signature) != MBB_MAGIC) {
(void) fprintf(stderr,
gettext("Bad signature on "
"extended partition\n"));
return (error);
}
numDrives = 0;
for (i = 0; i < FD_NUMPART; i++) {
if (isDosDrive(part[i].systid)) {
extndDrives[numDrives++] = i;
continue;
} else if (isDosExtended(part[i].systid)) {
if (nextseek != lastseek) {
(void) fprintf(stderr,
gettext("WARNING: "
"Ignoring unexpected "
"additional extended "
"partition"));
continue;
}
nextseek = xstartsect +
ltohi(part[i].relsect);
continue;
}
}
} while (drvnum > logicalDriveCount + numDrives);
if (drvnum <= logicalDriveCount + numDrives) {
driveIndex = logicalDriveCount + numDrives - drvnum;
found =
ltohi(part[extndDrives[driveIndex]].relsect) +
lastseek;
if (found > (xstartsect + xnumsect)) {
(void) fprintf(stderr,
gettext("Logical drive start sector (%d) "
"is not within the partition!\n"), found);
return (error);
} else {
found *= bpsec;
}
return (found);
} else {
logicalDriveCount += numDrives;
(void) memcpy(part, mb.parts, sizeof (part));
}
}
if (drvnum <= logicalDriveCount + numExtraDrives) {
driveIndex = logicalDriveCount + numExtraDrives - drvnum;
found = ltohi(part[extraDrives[driveIndex]].relsect) * bpsec;
return (found);
}
return (error);
}