Usenet comp.os.linux.alpha (1997-2013)
 help / color / mirror / Atom feed
From: "B. James Phillippe" <bryan@terran.org>
To: Raymond Doetjes <rdoetjes@node102bd.a2000.nl>
Subject: Re: convert DOS to BSD partitions?
Date: 1999/12/30	[thread overview]
Message-ID: <4854D13580C5D211AD190008C73393D086D9FC@tgxch01.taylornet.com> (raw)
In-Reply-To: 386A65D5.EBF6F4C2@node102bd.a2000.nl

[-- Attachment #1: Type: text/plain, Size: 669 bytes --]

On Wed, 29 Dec 1999, Raymond Doetjes wrote:

> The only program that "might" do this is partiotion magic from
Symantec.
> If that can't do it than it can't be done.
> 
> Raymond

Richard Henderson might be offended.  He posted a program to do this on
axp-list@redhat.com several months ago.  I have included his original
email
and the attachment, a small C program.  YMMV...

> Steeve McCauley wrote:
> 
> > Somehow I have a feeling that this ain't possible.
> >
> > Anyone have any success converting a 'dos' partition
> > to a BSD partition?
> >
> > Or do I just get to reinstall?
> >
> > --
> > :wq

-bp
--
# bryan at terran dot org
# http://www.terran.org/~bryan



[-- Attachment #2: Type: message/rfc822, Size: 1811 bytes --]

From: "B. James Phillippe" <bryan@terran.org>
To: Raymond Doetjes <rdoetjes@node102bd.a2000.nl>
Cc: Steeve McCauley <steeve@oneguycoding.com>
Subject: Re: convert DOS to BSD partitions?
Date: Thu, 30 Dec 1999 20:08:12 -0500
Message-ID: <44C2247EAFF4D011B0BD00805F85B09C1C5B8F61@news.maricopa.gov>

On Wed, 29 Dec 1999, Raymond Doetjes wrote:

> The only program that "might" do this is partiotion magic from
Symantec.
> If that can't do it than it can't be done.
> 
> Raymond

Richard Henderson might be offended.  He posted a program to do this on
axp-list@redhat.com several months ago.  I have included his original
email
and the attachment, a small C program.  YMMV...

> Steeve McCauley wrote:
> 
> > Somehow I have a feeling that this ain't possible.
> >
> > Anyone have any success converting a 'dos' partition
> > to a BSD partition?
> >
> > Or do I just get to reinstall?
> >
> > --
> > :wq

-bp
--
# bryan at terran dot org
# http://www.terran.org/~bryan

[-- Attachment #3: Type: message/rfc822, Size: 1811 bytes --]

From: "B. James Phillippe" <bryan@terran.org>
To: Raymond Doetjes <rdoetjes@node102bd.a2000.nl>
Cc: Steeve McCauley <steeve@oneguycoding.com>
Subject: Re: convert DOS to BSD partitions?
Date: Thu, 30 Dec 1999 20:08:12 -0500
Message-ID: <44C2247EAFF4D011B0BD00805F85B09C1C5B8F35@news.maricopa.gov>

On Wed, 29 Dec 1999, Raymond Doetjes wrote:

> The only program that "might" do this is partiotion magic from
Symantec.
> If that can't do it than it can't be done.
> 
> Raymond

Richard Henderson might be offended.  He posted a program to do this on
axp-list@redhat.com several months ago.  I have included his original
email
and the attachment, a small C program.  YMMV...

> Steeve McCauley wrote:
> 
> > Somehow I have a feeling that this ain't possible.
> >
> > Anyone have any success converting a 'dos' partition
> > to a BSD partition?
> >
> > Or do I just get to reinstall?
> >
> > --
> > :wq

-bp
--
# bryan at terran dot org
# http://www.terran.org/~bryan

[-- Attachment #4: Type: message/rfc822, Size: 14315 bytes --]

[-- Attachment #4.1.1: Type: text/plain, Size: 669 bytes --]

On Wed, 29 Dec 1999, Raymond Doetjes wrote:

> The only program that "might" do this is partiotion magic from
Symantec.
> If that can't do it than it can't be done.
> 
> Raymond

Richard Henderson might be offended.  He posted a program to do this on
axp-list@redhat.com several months ago.  I have included his original
email
and the attachment, a small C program.  YMMV...

> Steeve McCauley wrote:
> 
> > Somehow I have a feeling that this ain't possible.
> >
> > Anyone have any success converting a 'dos' partition
> > to a BSD partition?
> >
> > Or do I just get to reinstall?
> >
> > --
> > :wq

-bp
--
# bryan at terran dot org
# http://www.terran.org/~bryan



[-- Attachment #4.1.2: labelcvt.c --]
[-- Type: TEXT/PLAIN, Size: 12384 bytes --]

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#define MAXPARTITIONS		16

#define MSDOS_LABEL_OFFSET	446
#define MSDOS_DISKMAGIC		0xAA55

#define MSDOS_FS_EMPTY		0
#define MSDOS_FS_DOS_12		1
#define MSDOS_FS_XENIX_ROOT	2
#define MSDOS_FS_XENIX_USR	3
#define MSDOS_FS_DOS_16		4
#define MSDOS_FS_DOS_EXT	5
#define MSDOS_FS_DOS_16_BIG	6
#define MSDOS_FS_OS2		7
#define MSDOS_FS_AIX		8
#define MSDOS_FS_AIX_BOOTABLE	9
#define MSDOS_FS_OS2_BOOTMAN	10
#define MSDOS_FS_WIN95_FAT32	11
#define MSDOS_FS_WIN98_EXT	15
#define MSDOS_FS_GNU_HURD	0x63
#define MSDOS_FS_MINIX		0x81
#define MSDOS_FS_LINUX_SWAP	0x82
#define MSDOS_FS_LINUX		0x83
#define MSDOS_FS_LINUX_EXT	0x85
#define MSDOS_FS_BSDI		0xb7
#define MSDOS_FS_BSDI_SWAP	0xb8
#define MSDOS_FS_UNKNOWN	0xf0

struct dos_partition
{
  u_int8_t boot_ind;         /* 0x80 - active */
  u_int8_t head;             /* starting head */
  u_int8_t sector;           /* starting sector */
  u_int8_t cyl;              /* starting cylinder */
  u_int8_t sys_ind;          /* What partition type */
  u_int8_t end_head;         /* end head */
  u_int8_t end_sector;       /* end sector */
  u_int8_t end_cyl;          /* end cylinder */
  u_int32_t start_sect;      /* starting sector counting from 0 */
  u_int32_t nr_sects;        /* nr of sectors in partition */
};

struct dos_disklabel
{
  struct dos_partition d_partitions[4];
  u_int16_t magic;
};

#define BSD_LABEL_OFFSET	64
#define BSD_DISKMAGIC		0x82564557

#define BSD_FS_UNUSED		0
#define BSD_FS_SWAP		1
#define BSD_FS_V6		2
#define BSD_FS_V7		3
#define BSD_FS_SYSV		4
#define BSD_FS_V71K		5
#define	BSD_FS_V8		6
#define BSD_FS_BSDFFS		7
#define BSD_FS_EXT2		8
#define BSD_FS_ADVFS		16
#define BSD_FS_LSMpubl		17
#define BSD_FS_LSMpriv		18
#define BSD_FS_LSMsimp		19
#define BSD_FS_UNKNOWN		2

#define BSD_DTYPE_SMD		1
#define BSD_DTYPE_MSCP		2
#define BSD_DTYPE_DEC		3
#define BSD_DTYPE_SCSI		4
#define BSD_DTYPE_ESDI		5
#define BSD_DTYPE_ST506		6

struct bsd_partition
{
  u_int32_t  p_size;               /* number of sectors in partition */
  u_int32_t  p_offset;             /* starting sector */
  u_int32_t  p_fsize;              /* filesystem basic fragment size */
  u_int8_t   p_fstype;             /* filesystem type, see below */
  u_int8_t   p_frag;               /* filesystem fragments per block */
  u_int16_t  p_cpg;                /* filesystem cylinders per group */
};

struct bsd_disklabel {
  u_int32_t  d_magic;                /* the magic number */
  int16_t    d_type;                 /* drive type */
  int16_t    d_subtype;              /* controller/d_type specific */
  char       d_typename[16];         /* type name, e.g. "eagle" */
  char       d_packname[16];                 /* pack identifier */ 
  u_int32_t  d_secsize;              /* # of bytes per sector */
  u_int32_t  d_nsectors;             /* # of data sectors per track */
  u_int32_t  d_ntracks;              /* # of tracks per cylinder */
  u_int32_t  d_ncylinders;           /* # of data cylinders per unit */
  u_int32_t  d_secpercyl;            /* # of data sectors per cylinder */
  u_int32_t  d_secperunit;           /* # of data sectors per unit */
  u_int16_t  d_sparespertrack;       /* # of spare sectors per track */
  u_int16_t  d_sparespercyl;         /* # of spare sectors per cylinder */
  u_int32_t  d_acylinders;           /* # of alt. cylinders per unit */
  u_int16_t  d_rpm;                  /* rotational speed */
  u_int16_t  d_interleave;           /* hardware sector interleave */
  u_int16_t  d_trackskew;            /* sector 0 skew, per track */
  u_int16_t  d_cylskew;              /* sector 0 skew, per cylinder */
  u_int32_t  d_headswitch;           /* head switch time, usec */
  u_int32_t  d_trkseek;              /* track-to-track seek, usec */
  u_int32_t  d_flags;                /* generic flags */
  u_int32_t  d_drivedata[5];         /* drive-type specific information */
  u_int32_t  d_spare[5];             /* reserved for future use */
  u_int32_t  d_magic2;               /* the magic number (again) */
  u_int16_t  d_checksum;             /* xor of data incl. partitions */

  /* filesystem and partition information: */
  u_int16_t  d_npartitions;          /* number of partitions in following */
  u_int32_t  d_bbsize;               /* size of boot area at sn0, bytes */
  u_int32_t  d_sbsize;               /* max size of fs superblock, bytes */
  struct bsd_partition d_partitions[MAXPARTITIONS]; /* actually may be more */
};

struct canonical_partition
{
  u_int64_t start, size;
  int type;
};

static struct canonical_partition partitions[MAXPARTITIONS];

static int
read_dos_disklabel(int fd, u_int64_t disk_size)
{
  struct dos_disklabel disklabel;
  u_int64_t first_sector = 0;
  u_int64_t extended_offset = 0;
  int current_part = 0;

  while (1)
    {
      int i, extended = -1;

      if (lseek(fd, first_sector*512+MSDOS_LABEL_OFFSET, SEEK_SET) < 0)
	return -1;
      if (read(fd, &disklabel, sizeof(disklabel)) != sizeof(disklabel))
	return -1;

      if (disklabel.magic != MSDOS_DISKMAGIC)
	return -2;

      for (i = 0; i < 4; ++i)
	{
	  u_int32_t p_start_sect, p_nr_sects, p_sys_ind;

	  p_start_sect = disklabel.d_partitions[i].start_sect;
	  p_nr_sects = disklabel.d_partitions[i].nr_sects;
	  p_sys_ind = disklabel.d_partitions[i].sys_ind;

	  if (p_nr_sects == 0)
	    continue;

	  if (p_sys_ind == MSDOS_FS_DOS_EXT
	      || p_sys_ind == MSDOS_FS_WIN98_EXT
	      || p_sys_ind == MSDOS_FS_LINUX_EXT)
	    {
	      if (extended == -1)
		extended = i;
	      continue;
	    }

	  /* Check the 3rd and 4th entries -- these sometimes contain
	     random garbage.  */
	  if (i >= 2
	      && (p_start_sect + p_nr_sects > disk_size
		  || first_sector + p_start_sect < first_sector
		  || first_sector + p_start_sect + p_nr_sects < first_sector))
	    continue;

	  partitions[current_part].start = first_sector + p_start_sect;
	  partitions[current_part].size = p_nr_sects;
	  partitions[current_part].type = p_sys_ind;
	  current_part++;
	}

      if (extended < 0)
	break;
      if (current_part < 4)
	current_part = 4;

      first_sector =
	extended_offset + disklabel.d_partitions[extended].start_sect;
      if (extended_offset == 0)
	{
          disk_size = disklabel.d_partitions[extended].nr_sects;
	  extended_offset = first_sector;
	}
    }

  return 0;
}

static int
read_bsd_disklabel(int fd, u_int64_t disk_size)
{
  struct bsd_disklabel disklabel;
  int max_partitions, i, j;

  if (lseek(fd, BSD_LABEL_OFFSET, SEEK_SET) < 0)
    return -1;
  if (read(fd, &disklabel, sizeof(disklabel)) != sizeof(disklabel))
    return -1;

  if (disklabel.d_magic != BSD_DISKMAGIC
      || disklabel.d_magic2 != BSD_DISKMAGIC)
    return -2;

  max_partitions = disklabel.d_npartitions;
  if (max_partitions > MAXPARTITIONS)
    max_partitions = MAXPARTITIONS;

  for (i = 0; i < max_partitions; ++i)
    {
      u_int64_t start, size;
      int type;

      if (disklabel.d_partitions[i].p_fstype == BSD_FS_UNUSED)
	continue;

      start = disklabel.d_partitions[i].p_offset;
      size = disklabel.d_partitions[i].p_size;

      for (j = 0; j < i; ++j)
	{
	  if (partitions[j].type == 0)
	    continue;

	  /* No relationship -- try again.  */
	  if (partitions[j].start + partitions[j].size <= start
	      || partitions[j].start >= start + size)
	    continue;

	  /* Equal -- no need to add.  */
	  if (partitions[j].start == start
	      && partitions[j].size == size)
	    goto no_add;

	  /* BSD living in dos partition.  */
	  if (partitions[j].start <= start
	      && partitions[j].start + partitions[j].size >= start + size)
	    goto add;

	  /* Overlap.  Don't try to add.  */
	  goto no_add;
	}

      add:
	partitions[i].start = start;
	partitions[i].size = size;

	switch (disklabel.d_partitions[i].p_fstype)
	  {
	  case BSD_FS_SWAP:
	    type = MSDOS_FS_LINUX_SWAP;
	    break;
	  case BSD_FS_EXT2:
	    type = MSDOS_FS_LINUX;
	    break;
	  case BSD_FS_BSDFFS:
	    type = MSDOS_FS_BSDI;
	    break;
	  default:
	    type = MSDOS_FS_UNKNOWN;
	    break;
	  }
	partitions[i].type = type;
      no_add:;
    }

  return 0;
}

static int
write_dos_disklabel(int fd)
{
  return -2;
}

static int
write_bsd_disklabel(int fd)
{
  struct bsd_disklabel disklabel;
  int i, num_partitions;

  memset(&disklabel, 0, sizeof(disklabel));
  disklabel.d_magic = BSD_DISKMAGIC;
  disklabel.d_magic2 = BSD_DISKMAGIC;
  disklabel.d_type = BSD_DTYPE_SCSI;
  memcpy(disklabel.d_typename, "SCSI", 4);
  disklabel.d_secsize = 512;

  num_partitions = 0;
  for (i = 0; i < MAXPARTITIONS; ++i)
    {
      int type = partitions[i].type;

      if (type == 0)
	continue;
      switch (type)
	{
	case MSDOS_FS_LINUX:
	  type = BSD_FS_EXT2;
	  break;
	case MSDOS_FS_LINUX_SWAP:
	  type = BSD_FS_SWAP;
	  break;
	case MSDOS_FS_BSDI:
	  type = BSD_FS_BSDFFS;
	  break;
	default:
	  type = BSD_FS_UNKNOWN;
	  break;
	}

      disklabel.d_partitions[i].p_offset = partitions[i].start;
      disklabel.d_partitions[i].p_size = partitions[i].size;
      disklabel.d_partitions[i].p_fstype = type;
      num_partitions = i+1;
    }
  disklabel.d_npartitions = num_partitions;

  if (lseek(fd, BSD_LABEL_OFFSET, SEEK_SET) < 0)
    return -1;
  if (write(fd, &disklabel, sizeof(disklabel)) != sizeof(disklabel))
    return -1;

  return 0;
}

static void
print_partitions(void)
{
  int i;
  for (i = 0; i < MAXPARTITIONS; ++i)
    {
      if (partitions[i].type == 0)
	continue;
      printf("%2d %9ld %9ld %02x\n", i+1, partitions[i].start,
	     partitions[i].size, partitions[i].type);
    }
}

static void __attribute__((noreturn))
usage(void)
{
  fprintf(stderr, "\
Usage: labelcvt <option> <input device> [<output device>]\n\
  Where option is one of\n\
	--dos		Convert to MSDOS disklabel\n\
	--bsd		Convert to BSD disklabel\n\
	--print		Print the current disklabel\n");
  exit(1);
}


int
main(int ac, char **av)
{
  int action, fd, outfd;
  u_int64_t disk_size;

  if (ac != 3 && ac != 4)
    usage();

  if (strcmp(av[1], "--dos") == 0)
    action = 0;
  else if (strcmp(av[1], "--bsd") == 0)
    action = 1;
  else if (strcmp(av[1], "--print") == 0)
    action = 2;
  else
    usage();

  fd = open(av[2], O_RDONLY);
  if (fd < 0)
    {
      perror(av[2]);
      exit(1);
    }

  if (action != 2)
    {
      outfd = open(av[ac-1], O_WRONLY|O_CREAT);
      if (outfd < 0)
	{
	  perror(av[ac-1]);
	  exit(1);
	}
    }

  disk_size = -1;

  switch (action)
    {
    case 0:
      switch (read_bsd_disklabel(fd, disk_size))
	{
	case -1:
	  perror("read bsd disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Not a BSD disklabel.\n");
	  exit(1);
	}
      switch (write_dos_disklabel(outfd))
	{
	case -1:
	  perror("write dos disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Error writing DOS disklabel.\n");
	  exit(1);
	}
      break;

    case 1:
      switch (read_dos_disklabel(fd, disk_size))
	{
	case -1:
	  perror("read dos disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Not a DOS disklabel.\n");
	  exit(1);
	}
      switch (write_bsd_disklabel(outfd))
	{
	case -1:
	  perror("write bsd disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Error writing BSD disklabel.\n");
	  exit(1);
	}
      break;

    case 2:
      {
	const char *current_type;

	errno = 0;
	switch (read_dos_disklabel(fd, disk_size))
	  {
	  case -1:
	    if (errno != 0)
	      {
		perror("read dos disklabel");
		exit(1);
	      }
	    /* FALLTHRU */
	  case -2:
	    switch (read_bsd_disklabel(fd, disk_size))
	      {
	      case -1:
		perror("read bsd disklabel");
		exit(1);
	      case -2:
		fprintf(stderr, "Unknown disklabel format.\n");
		exit(1);
	      default:
		current_type = "BSD";
		break;
	      }
	    break;
	  default:
	    current_type = "DOS";
	    break;
	  }

	printf("Current format: %s\n", current_type);
	print_partitions();
	break;
      }
    }

  exit(0); 
}

[-- Attachment #5: Type: message/rfc822, Size: 1811 bytes --]

From: "B. James Phillippe" <bryan@terran.org>
To: Raymond Doetjes <rdoetjes@node102bd.a2000.nl>
Cc: Steeve McCauley <steeve@oneguycoding.com>
Subject: Re: convert DOS to BSD partitions?
Date: Thu, 30 Dec 1999 20:08:12 -0500
Message-ID: <44C2247EAFF4D011B0BD00805F85B09C1C5B8F7D@news.maricopa.gov>

On Wed, 29 Dec 1999, Raymond Doetjes wrote:

> The only program that "might" do this is partiotion magic from
Symantec.
> If that can't do it than it can't be done.
> 
> Raymond

Richard Henderson might be offended.  He posted a program to do this on
axp-list@redhat.com several months ago.  I have included his original
email
and the attachment, a small C program.  YMMV...

> Steeve McCauley wrote:
> 
> > Somehow I have a feeling that this ain't possible.
> >
> > Anyone have any success converting a 'dos' partition
> > to a BSD partition?
> >
> > Or do I just get to reinstall?
> >
> > --
> > :wq

-bp
--
# bryan at terran dot org
# http://www.terran.org/~bryan

[-- Attachment #6: Type: message/rfc822, Size: 1393 bytes --]

From: "B. James Phillippe" <bryan@terran.org>
To: Raymond Doetjes <rdoetjes@node102bd.a2000.nl>
Cc: Steeve McCauley <steeve@oneguycoding.com>
Subject: Re: convert DOS to BSD partitions?
Date: Thu, 30 Dec 1999 20:08:12 -0500
Message-ID: <44C2247EAFF4D011B0BD00805F85B09C1C5B8F8E@news.maricopa.gov>

On Wed, 29 Dec 1999, Raymond Doetjes wrote:

> The only program that "might" do this is partiotion magic from
Symantec.
> If that can't do it than it can't be done.
> 
> Raymond

Richard Henderson might be offended.  He posted a program to do this on
axp-list@redhat.com several months ago.  I have included his original
email
and the attachment, a small C program.  YMMV...

> Steeve McCauley wrote:
> 
> > Somehow I have a feeling that this ain't possible.
> >
> > Anyone have any success converting a 'dos' partition
> > to a BSD partition?
> >
> > Or do I just get to reinstall?
> >
> > --
> > :wq

-bp
--
# bryan at terran dot org
# http://www.terran.org/~bryan

  parent reply	other threads:[~1999-12-30  5:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-27  5:00 convert DOS to BSD partitions? Steeve McCauley
1999-12-29  5:00 ` Raymond Doetjes
1999-12-30  5:00   ` B. James Phillippe
1999-12-30  5:00   ` B. James Phillippe
1999-12-30  5:00   ` B. James Phillippe [this message]
1999-12-30  5:00   ` B. James Phillippe
2000-01-02  5:00     ` Apology (was Re: convert DOS to BSD partitions?) B. James Phillippe
1999-12-30  5:00   ` convert DOS to BSD partitions? B. James Phillippe
1999-12-30  5:00 ` Geerten Kuiper
1999-12-31  5:00   ` Steeve McCauley
2000-01-02  5:00     ` Steeve McCauley
2000-01-06  5:00 ` Raydar X
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox