blob: c92cfb02e468cb8b3a25ecf1484acdff089eb047 [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
16#define LABEL_OFFSET1 64 /* BSD disklabels can start at one of these two */
17#define LABEL_OFFSET2 512 /* values; check both for valid signatures */
18
19// FreeBSD documents a maximum # of partitions of 8, but I saw 16 on a NetBSD
20// disk. I'm quadrupling that for further safety. Note that BSDReadData()
21// uses a 2048-byte I/O buffer. In combination with LABEL_OFFSET2 and the
22// additional 148-byte offset to the actual partition data, that gives a
23// theoretical maximum of 86.75 partitions that the program can handle.
24#define MAX_BSD_PARTS 64
25
26
27using namespace std;
28
29/****************************************
30 * *
31 * BSDData class and related structures *
32 * *
33 ****************************************/
34
35// Possible states of the MBR
36enum BSDValidity {unknown, bsd_invalid, bsd};
37
38// Data for a single BSD partition record
srs5694e4ac11e2009-08-31 10:13:04 -040039// Create entries for all fields, although we only use lengthLBA, firstLBA,
40// and fsType, to simplify loading the data from disk....
41struct BSDRecord { // the partition table
42 uint32_t lengthLBA; // number of sectors in partition
43 uint32_t firstLBA; // starting sector
44 uint32_t fragSize; // filesystem basic fragment size
45 uint8_t fsType; // filesystem type, see below
46 uint8_t frag; // filesystem fragments per block
47 uint16_t pcpg; // filesystem cylinders per group
srs5694a0eb11a2009-08-29 15:00:08 -040048};
49
50// Full data in tweaked MBR format
51class BSDData {
52 protected:
53 // We only need a few items from the main BSD disklabel data structure....
srs5694e4ac11e2009-08-31 10:13:04 -040054 uint32_t signature; // the magic number
55 uint32_t sectorSize; // # of bytes per sector
56 uint32_t signature2; // the magic number (again)
57 uint16_t numParts; // number of partitions in table
58 BSDRecord* partitions; // partition array
srs5694a0eb11a2009-08-29 15:00:08 -040059
60 // Above are basic BSD disklabel data; now add more stuff....
srs5694e4ac11e2009-08-31 10:13:04 -040061 uint64_t labelFirstLBA; // first sector of BSD disklabel (partition or disk)
62 uint64_t labelLastLBA; // final sector of BSD disklabel
63 uint64_t labelStart; // BSD disklabel start point in bytes from labelFirstLBA
srs5694a0eb11a2009-08-29 15:00:08 -040064 BSDValidity state;
srs5694a0eb11a2009-08-29 15:00:08 -040065 public:
66 BSDData(void);
67 ~BSDData(void);
68 int ReadBSDData(char* deviceFilename, uint64_t startSector, uint64_t endSector);
69 void ReadBSDData(int fd, uint64_t startSector, uint64_t endSector);
70 void ReverseMetaBytes(void);
71 void DisplayBSDData(void);
srs5694a0eb11a2009-08-29 15:00:08 -040072 int ShowState(void); // returns 1 if BSD disklabel detected
73 int IsDisklabel(void) {return (state == bsd);}
74
75 // Functions to extract data on specific partitions....
76 uint8_t GetType(int i);
77 uint64_t GetFirstSector(int i);
78 uint64_t GetLength(int i);
79 int GetNumParts(void);
80 GPTPart AsGPT(int i); // Return BSD part. as GPT part.
81}; // struct MBRData
82
83#endif