Blame bsd.cc

Packit 062bc7
/* bsd.cc -- Functions for loading and manipulating legacy BSD disklabel
Packit 062bc7
   data. */
Packit 062bc7
Packit 062bc7
/* By Rod Smith, initial coding August, 2009 */
Packit 062bc7
Packit 062bc7
/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
Packit 062bc7
  under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
Packit 062bc7
Packit 062bc7
#define __STDC_LIMIT_MACROS
Packit 062bc7
#define __STDC_CONSTANT_MACROS
Packit 062bc7
Packit 062bc7
#include <stdio.h>
Packit 062bc7
//#include <unistd.h>
Packit 062bc7
#include <stdlib.h>
Packit 062bc7
#include <stdint.h>
Packit 062bc7
#include <fcntl.h>
Packit 062bc7
#include <sys/stat.h>
Packit 062bc7
#include <errno.h>
Packit 062bc7
#include <iostream>
Packit 062bc7
#include <string>
Packit 062bc7
#include "support.h"
Packit 062bc7
#include "bsd.h"
Packit 062bc7
Packit 062bc7
using namespace std;
Packit 062bc7
Packit 062bc7
Packit 062bc7
BSDData::BSDData(void) {
Packit 062bc7
   state = unknown;
Packit 062bc7
   signature = UINT32_C(0);
Packit 062bc7
   signature2 = UINT32_C(0);
Packit 062bc7
   sectorSize = 512;
Packit 062bc7
   numParts = 0;
Packit 062bc7
   labelFirstLBA = 0;
Packit 062bc7
   labelLastLBA = 0;
Packit 062bc7
   labelStart = LABEL_OFFSET1; // assume raw disk format
Packit 062bc7
   partitions = NULL;
Packit 062bc7
} // default constructor
Packit 062bc7
Packit 062bc7
BSDData::~BSDData(void) {
Packit 062bc7
   delete[] partitions;
Packit 062bc7
} // destructor
Packit 062bc7
Packit 062bc7
// Read BSD disklabel data from the specified device filename. This function
Packit 062bc7
// just opens the device file and then calls an overloaded function to do
Packit 062bc7
// the bulk of the work. Returns 1 on success, 0 on failure.
Packit 062bc7
int BSDData::ReadBSDData(const string & device, uint64_t startSector, uint64_t endSector) {
Packit 062bc7
   int allOK = 1;
Packit 062bc7
   DiskIO myDisk;
Packit 062bc7
Packit 062bc7
   if (device != "") {
Packit 062bc7
      if (myDisk.OpenForRead(device)) {
Packit 062bc7
         allOK = ReadBSDData(&myDisk, startSector, endSector);
Packit 062bc7
      } else {
Packit 062bc7
         allOK = 0;
Packit 062bc7
      } // if/else
Packit 062bc7
Packit 062bc7
      myDisk.Close();
Packit 062bc7
   } else {
Packit 062bc7
      allOK = 0;
Packit 062bc7
   } // if/else
Packit 062bc7
   return allOK;
Packit 062bc7
} // BSDData::ReadBSDData() (device filename version)
Packit 062bc7
Packit 062bc7
// Load the BSD disklabel data from an already-opened disk
Packit 062bc7
// file, starting with the specified sector number.
Packit 062bc7
int BSDData::ReadBSDData(DiskIO *theDisk, uint64_t startSector, uint64_t endSector) {
Packit 062bc7
   int allOK = 1;
Packit 062bc7
   int i, foundSig = 0, bigEnd = 0;
Packit 062bc7
   int relative = 0; // assume absolute partition sector numbering
Packit 062bc7
   uint8_t buffer[4096]; // I/O buffer
Packit 062bc7
   uint32_t realSig;
Packit 062bc7
   uint32_t* temp32;
Packit 062bc7
   uint16_t* temp16;
Packit 062bc7
   BSDRecord* tempRecords;
Packit 062bc7
   int offset[NUM_OFFSETS] = { LABEL_OFFSET1, LABEL_OFFSET2 };
Packit 062bc7
Packit 062bc7
   labelFirstLBA = startSector;
Packit 062bc7
   labelLastLBA = endSector;
Packit 062bc7
   offset[1] = theDisk->GetBlockSize();
Packit 062bc7
Packit 062bc7
   // Read 4096 bytes (eight 512-byte sectors or equivalent)
Packit 062bc7
   // into memory; we'll extract data from this buffer.
Packit 062bc7
   // (Done to work around FreeBSD limitation on size of reads
Packit 062bc7
   // from block devices.)
Packit 062bc7
   allOK = theDisk->Seek(startSector);
Packit 062bc7
   if (allOK) allOK = theDisk->Read(buffer, 4096);
Packit 062bc7
Packit 062bc7
   // Do some strangeness to support big-endian architectures...
Packit 062bc7
   bigEnd = (IsLittleEndian() == 0);
Packit 062bc7
   realSig = BSD_SIGNATURE;
Packit 062bc7
   if (bigEnd && allOK)
Packit 062bc7
      ReverseBytes(&realSig, 4);
Packit 062bc7
Packit 062bc7
   // Look for the signature at any of two locations.
Packit 062bc7
   // Note that the signature is repeated at both the original
Packit 062bc7
   // offset and 132 bytes later, so we need two checks....
Packit 062bc7
   if (allOK) {
Packit 062bc7
      i = 0;
Packit 062bc7
      do {
Packit 062bc7
         temp32 = (uint32_t*) &buffer[offset[i]];
Packit 062bc7
         signature = *temp32;
Packit 062bc7
         if (signature == realSig) { // found first, look for second
Packit 062bc7
            temp32 = (uint32_t*) &buffer[offset[i] + 132];
Packit 062bc7
            signature2 = *temp32;
Packit 062bc7
            if (signature2 == realSig) {
Packit 062bc7
               foundSig = 1;
Packit 062bc7
               labelStart = offset[i];
Packit 062bc7
            } // if found signature
Packit 062bc7
         } // if/else
Packit 062bc7
         i++;
Packit 062bc7
      } while ((!foundSig) && (i < NUM_OFFSETS));
Packit 062bc7
      allOK = foundSig;
Packit 062bc7
   } // if
Packit 062bc7
Packit 062bc7
   // Load partition metadata from the buffer....
Packit 062bc7
   if (allOK) {
Packit 062bc7
      temp32 = (uint32_t*) &buffer[labelStart + 40];
Packit 062bc7
      sectorSize = *temp32;
Packit 062bc7
      temp16 = (uint16_t*) &buffer[labelStart + 138];
Packit 062bc7
      numParts = *temp16;
Packit 062bc7
   } // if
Packit 062bc7
Packit 062bc7
   // Make it big-endian-aware....
Packit 062bc7
   if ((IsLittleEndian() == 0) && allOK)
Packit 062bc7
      ReverseMetaBytes();
Packit 062bc7
Packit 062bc7
   // Check validity of the data and flag it appropriately....
Packit 062bc7
   if (foundSig && (numParts <= MAX_BSD_PARTS) && allOK) {
Packit 062bc7
      state = bsd;
Packit 062bc7
   } else {
Packit 062bc7
      state = bsd_invalid;
Packit 062bc7
   } // if/else
Packit 062bc7
Packit 062bc7
   // If the state is good, go ahead and load the main partition data....
Packit 062bc7
   if (state == bsd) {
Packit 062bc7
      partitions = new struct BSDRecord[numParts * sizeof(struct BSDRecord)];
Packit 062bc7
      if (partitions == NULL) {
Packit 062bc7
         cerr << "Unable to allocate memory in BSDData::ReadBSDData()! Terminating!\n";
Packit 062bc7
         exit(1);
Packit 062bc7
      } // if
Packit 062bc7
      for (i = 0; i < numParts; i++) {
Packit 062bc7
         // Once again, we use the buffer, but index it using a BSDRecord
Packit 062bc7
         // pointer (dangerous, but effective)....
Packit 062bc7
         tempRecords = (BSDRecord*) &buffer[labelStart + 148];
Packit 062bc7
         partitions[i].lengthLBA = tempRecords[i].lengthLBA;
Packit 062bc7
         partitions[i].firstLBA = tempRecords[i].firstLBA;
Packit 062bc7
         partitions[i].fsType = tempRecords[i].fsType;
Packit 062bc7
         if (bigEnd) { // reverse data (fsType is a single byte)
Packit 062bc7
            ReverseBytes(&partitions[i].lengthLBA, 4);
Packit 062bc7
            ReverseBytes(&partitions[i].firstLBA, 4);
Packit 062bc7
         } // if big-endian
Packit 062bc7
         // Check for signs of relative sector numbering: A "0" first sector
Packit 062bc7
         // number on a partition with a non-zero length -- but ONLY if the
Packit 062bc7
         // length is less than the disk size, since NetBSD has a habit of
Packit 062bc7
         // creating a disk-sized partition within a carrier MBR partition
Packit 062bc7
         // that's too small to house it, and this throws off everything....
Packit 062bc7
         if ((partitions[i].firstLBA == 0) && (partitions[i].lengthLBA > 0)
Packit 062bc7
             && (partitions[i].lengthLBA < labelLastLBA))
Packit 062bc7
            relative = 1;
Packit 062bc7
      } // for
Packit 062bc7
      // Some disklabels use sector numbers relative to the enclosing partition's
Packit 062bc7
      // start, others use absolute sector numbers. If relative numbering was
Packit 062bc7
      // detected above, apply a correction to all partition start sectors....
Packit 062bc7
      if (relative) {
Packit 062bc7
         for (i = 0; i < numParts; i++) {
Packit 062bc7
            partitions[i].firstLBA += (uint32_t) startSector;
Packit 062bc7
         } // for
Packit 062bc7
      } // if
Packit 062bc7
   } // if signatures OK
Packit 062bc7
//   DisplayBSDData();
Packit 062bc7
   return allOK;
Packit 062bc7
} // BSDData::ReadBSDData(DiskIO* theDisk, uint64_t startSector)
Packit 062bc7
Packit 062bc7
// Reverse metadata's byte order; called only on big-endian systems
Packit 062bc7
void BSDData::ReverseMetaBytes(void) {
Packit 062bc7
   ReverseBytes(&signature, 4);
Packit 062bc7
   ReverseBytes(&sectorSize, 4);
Packit 062bc7
   ReverseBytes(&signature2, 4);
Packit 062bc7
   ReverseBytes(&numParts, 2);
Packit 062bc7
} // BSDData::ReverseMetaByteOrder()
Packit 062bc7
Packit 062bc7
// Display basic BSD partition data. Used for debugging.
Packit 062bc7
void BSDData::DisplayBSDData(void) {
Packit 062bc7
   int i;
Packit 062bc7
Packit 062bc7
   if (state == bsd) {
Packit 062bc7
      cout << "BSD partitions:\n";
Packit 062bc7
      for (i = 0; i < numParts; i++) {
Packit 062bc7
         cout.width(4);
Packit 062bc7
         cout << i + 1 << "\t";
Packit 062bc7
         cout.width(13);
Packit 062bc7
         cout << partitions[i].firstLBA << "\t";
Packit 062bc7
         cout.width(15);
Packit 062bc7
         cout << partitions[i].lengthLBA << " \t0x";
Packit 062bc7
         cout.width(2);
Packit 062bc7
         cout.fill('0');
Packit 062bc7
         cout.setf(ios::uppercase);
Packit 062bc7
         cout << hex << (int) partitions[i].fsType << "\n" << dec;
Packit 062bc7
         cout.fill(' ');
Packit 062bc7
      } // for
Packit 062bc7
   } // if
Packit 062bc7
} // BSDData::DisplayBSDData()
Packit 062bc7
Packit 062bc7
// Displays the BSD disklabel state. Called during program launch to inform
Packit 062bc7
// the user about the partition table(s) status
Packit 062bc7
int BSDData::ShowState(void) {
Packit 062bc7
   int retval = 0;
Packit 062bc7
Packit 062bc7
   switch (state) {
Packit 062bc7
      case bsd_invalid:
Packit 062bc7
         cout << "  BSD: not present\n";
Packit 062bc7
         break;
Packit 062bc7
      case bsd:
Packit 062bc7
         cout << "  BSD: present\n";
Packit 062bc7
         retval = 1;
Packit 062bc7
         break;
Packit 062bc7
      default:
Packit 062bc7
         cout << "\a  BSD: unknown -- bug!\n";
Packit 062bc7
         break;
Packit 062bc7
   } // switch
Packit 062bc7
   return retval;
Packit 062bc7
} // BSDData::ShowState()
Packit 062bc7
Packit 062bc7
// Weirdly, this function has stopped working when defined inline,
Packit 062bc7
// but it's OK here....
Packit 062bc7
int BSDData::IsDisklabel(void) {
Packit 062bc7
   return (state == bsd);
Packit 062bc7
} // BSDData::IsDiskLabel()
Packit 062bc7
Packit 062bc7
// Returns the BSD table's partition type code
Packit 062bc7
uint8_t BSDData::GetType(int i) {
Packit 062bc7
   uint8_t retval = 0; // 0 = "unused"
Packit 062bc7
Packit 062bc7
   if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
Packit 062bc7
      retval = partitions[i].fsType;
Packit 062bc7
Packit 062bc7
   return(retval);
Packit 062bc7
} // BSDData::GetType()
Packit 062bc7
Packit 062bc7
// Returns the number of the first sector of the specified partition
Packit 062bc7
uint64_t BSDData::GetFirstSector(int i) {
Packit 062bc7
   uint64_t retval = UINT64_C(0);
Packit 062bc7
Packit 062bc7
   if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
Packit 062bc7
      retval = (uint64_t) partitions[i].firstLBA;
Packit 062bc7
Packit 062bc7
   return retval;
Packit 062bc7
} // BSDData::GetFirstSector
Packit 062bc7
Packit 062bc7
// Returns the length (in sectors) of the specified partition
Packit 062bc7
uint64_t BSDData::GetLength(int i) {
Packit 062bc7
   uint64_t retval = UINT64_C(0);
Packit 062bc7
Packit 062bc7
   if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
Packit 062bc7
      retval = (uint64_t) partitions[i].lengthLBA;
Packit 062bc7
Packit 062bc7
   return retval;
Packit 062bc7
} // BSDData::GetLength()
Packit 062bc7
Packit 062bc7
// Returns the number of partitions defined in the current table
Packit 062bc7
int BSDData::GetNumParts(void) {
Packit 062bc7
   return numParts;
Packit 062bc7
} // BSDData::GetNumParts()
Packit 062bc7
Packit 062bc7
// Returns the specified partition as a GPT partition. Used in BSD-to-GPT
Packit 062bc7
// conversion process
Packit 062bc7
GPTPart BSDData::AsGPT(int i) {
Packit 062bc7
   GPTPart guid;                  // dump data in here, then return it
Packit 062bc7
   uint64_t sectorOne, sectorEnd; // first & last sectors of partition
Packit 062bc7
   int passItOn = 1;              // Set to 0 if partition is empty or invalid
Packit 062bc7
Packit 062bc7
   guid.BlankPartition();
Packit 062bc7
   sectorOne = (uint64_t) partitions[i].firstLBA;
Packit 062bc7
   sectorEnd = sectorOne + (uint64_t) partitions[i].lengthLBA;
Packit 062bc7
   if (sectorEnd > 0) sectorEnd--;
Packit 062bc7
   // Note on above: BSD partitions sometimes have a length of 0 and a start
Packit 062bc7
   // sector of 0. With unsigned ints, the usual way (start + length - 1) to
Packit 062bc7
   // find the end will result in a huge number, which will be confusing.
Packit 062bc7
   // Thus, apply the "-1" part only if it's reasonable to do so.
Packit 062bc7
Packit 062bc7
   // Do a few sanity checks on the partition before we pass it on....
Packit 062bc7
   // First, check that it falls within the bounds of its container
Packit 062bc7
   // and that it starts before it ends....
Packit 062bc7
   if ((sectorOne < labelFirstLBA) || (sectorEnd > labelLastLBA) || (sectorOne > sectorEnd))
Packit 062bc7
      passItOn = 0;
Packit 062bc7
   // Some disklabels include a pseudo-partition that's the size of the entire
Packit 062bc7
   // disk or containing partition. Don't return it.
Packit 062bc7
   if ((sectorOne <= labelFirstLBA) && (sectorEnd >= labelLastLBA) &&
Packit 062bc7
       (GetType(i) == 0))
Packit 062bc7
      passItOn = 0;
Packit 062bc7
   // If the end point is 0, it's not a valid partition.
Packit 062bc7
   if ((sectorEnd == 0) || (sectorEnd == labelFirstLBA))
Packit 062bc7
      passItOn = 0;
Packit 062bc7
Packit 062bc7
   if (passItOn) {
Packit 062bc7
      guid.SetFirstLBA(sectorOne);
Packit 062bc7
      guid.SetLastLBA(sectorEnd);
Packit 062bc7
      // Now set a random unique GUID for the partition....
Packit 062bc7
      guid.RandomizeUniqueGUID();
Packit 062bc7
      // ... zero out the attributes and name fields....
Packit 062bc7
      guid.SetAttributes(UINT64_C(0));
Packit 062bc7
      // Most BSD disklabel type codes seem to be archaic or rare.
Packit 062bc7
      // They're also ambiguous; a FreeBSD filesystem is impossible
Packit 062bc7
      // to distinguish from a NetBSD one. Thus, these code assignment
Packit 062bc7
      // are going to be rough to begin with. For a list of meanings,
Packit 062bc7
      // see http://fxr.watson.org/fxr/source/sys/dtype.h?v=DFBSD,
Packit 062bc7
      // or Google it.
Packit 062bc7
      switch (GetType(i)) {
Packit 062bc7
         case 1: // BSD swap
Packit 062bc7
            guid.SetType(0xa502); break;
Packit 062bc7
         case 7: // BSD FFS
Packit 062bc7
            guid.SetType(0xa503); break;
Packit 062bc7
         case 8: case 11: // MS-DOS or HPFS
Packit 062bc7
            guid.SetType(0x0700); break;
Packit 062bc7
         case 9: // log-structured fs
Packit 062bc7
            guid.SetType(0xa903); break;
Packit 062bc7
         case 13: // bootstrap
Packit 062bc7
            guid.SetType(0xa501); break;
Packit 062bc7
         case 14: // vinum
Packit 062bc7
            guid.SetType(0xa505); break;
Packit 062bc7
         case 15: // RAID
Packit 062bc7
            guid.SetType(0xa903); break;
Packit 062bc7
         case 27: // FreeBSD ZFS
Packit 062bc7
            guid.SetType(0xa504); break;
Packit 062bc7
         default:
Packit 062bc7
            guid.SetType(0xa503); break;
Packit 062bc7
      } // switch
Packit 062bc7
      // Set the partition name to the name of the type code....
Packit 062bc7
      guid.SetName(guid.GetTypeName());
Packit 062bc7
   } // if
Packit 062bc7
   return guid;
Packit 062bc7
} // BSDData::AsGPT()