blob: afdb60cc3dadf78778f29fc6224c9bda27ac0f08 [file] [log] [blame]
srs5694a0eb11a2009-08-29 15:00:08 -04001/* bsd.h -- BSD disklabel data structure definitions, types, and functions */
2
3/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
4 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
5
6#include <stdint.h>
7#include <sys/types.h>
8#include <sys/ioctl.h>
9#include "gptpart.h"
10
11#ifndef __BSD_STRUCTS
12#define __BSD_STRUCTS
13
srs5694e4ac11e2009-08-31 10:13:04 -040014#define BSD_SIGNATURE UINT32_C(0x82564557) /* BSD disklabel signature ("magic") */
srs5694a0eb11a2009-08-29 15:00:08 -040015
srs5694ba00fed2010-01-12 18:18:36 -050016#define LABEL_OFFSET1 64 /* BSD disklabels can start at any of these three */
17#define LABEL_OFFSET2 512 /* values; check all for valid signatures */
18#define LABEL_OFFSET3 2048
srs5694a0eb11a2009-08-29 15:00:08 -040019
20// FreeBSD documents a maximum # of partitions of 8, but I saw 16 on a NetBSD
21// disk. I'm quadrupling that for further safety. Note that BSDReadData()
srs5694ba00fed2010-01-12 18:18:36 -050022// uses a 4096-byte I/O buffer. In combination with LABEL_OFFSET3 and the
srs5694a0eb11a2009-08-29 15:00:08 -040023// additional 148-byte offset to the actual partition data, that gives a
srs5694ba00fed2010-01-12 18:18:36 -050024// theoretical maximum of 118.75 partitions that the program can handle before
25// memory errors will occur.
srs5694a0eb11a2009-08-29 15:00:08 -040026#define MAX_BSD_PARTS 64
27
28
29using namespace std;
30
31/****************************************
32 * *
33 * BSDData class and related structures *
34 * *
35 ****************************************/
36
37// Possible states of the MBR
38enum BSDValidity {unknown, bsd_invalid, bsd};
39
40// Data for a single BSD partition record
srs5694e4ac11e2009-08-31 10:13:04 -040041// Create entries for all fields, although we only use lengthLBA, firstLBA,
42// and fsType, to simplify loading the data from disk....
43struct BSDRecord { // the partition table
44 uint32_t lengthLBA; // number of sectors in partition
45 uint32_t firstLBA; // starting sector
46 uint32_t fragSize; // filesystem basic fragment size
47 uint8_t fsType; // filesystem type, see below
48 uint8_t frag; // filesystem fragments per block
49 uint16_t pcpg; // filesystem cylinders per group
srs5694a0eb11a2009-08-29 15:00:08 -040050};
51
srs5694ba00fed2010-01-12 18:18:36 -050052// Full data in tweaked BSD format
srs5694a0eb11a2009-08-29 15:00:08 -040053class BSDData {
54 protected:
55 // We only need a few items from the main BSD disklabel data structure....
srs5694e4ac11e2009-08-31 10:13:04 -040056 uint32_t signature; // the magic number
57 uint32_t sectorSize; // # of bytes per sector
58 uint32_t signature2; // the magic number (again)
59 uint16_t numParts; // number of partitions in table
60 BSDRecord* partitions; // partition array
srs5694a0eb11a2009-08-29 15:00:08 -040061
62 // Above are basic BSD disklabel data; now add more stuff....
srs5694e4ac11e2009-08-31 10:13:04 -040063 uint64_t labelFirstLBA; // first sector of BSD disklabel (partition or disk)
64 uint64_t labelLastLBA; // final sector of BSD disklabel
65 uint64_t labelStart; // BSD disklabel start point in bytes from labelFirstLBA
srs5694a0eb11a2009-08-29 15:00:08 -040066 BSDValidity state;
srs5694a0eb11a2009-08-29 15:00:08 -040067 public:
68 BSDData(void);
69 ~BSDData(void);
70 int ReadBSDData(char* deviceFilename, uint64_t startSector, uint64_t endSector);
71 void ReadBSDData(int fd, uint64_t startSector, uint64_t endSector);
72 void ReverseMetaBytes(void);
73 void DisplayBSDData(void);
srs5694a0eb11a2009-08-29 15:00:08 -040074 int ShowState(void); // returns 1 if BSD disklabel detected
75 int IsDisklabel(void) {return (state == bsd);}
76
77 // Functions to extract data on specific partitions....
78 uint8_t GetType(int i);
79 uint64_t GetFirstSector(int i);
80 uint64_t GetLength(int i);
81 int GetNumParts(void);
82 GPTPart AsGPT(int i); // Return BSD part. as GPT part.
83}; // struct MBRData
84
85#endif