blob: 54f5899002aa8c7ce1b37c967f452779bb129bb1 [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>
srs5694a0eb11a2009-08-29 15:00:08 -04008#include "gptpart.h"
srs5694546a9c72010-01-26 16:00:26 -05009#include "diskio.h"
srs5694a0eb11a2009-08-29 15:00:08 -040010
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
srs5694546a9c72010-01-26 16:00:26 -050016// BSD disklabels can start at offsets of 64 or the sector size -- at least,
17// I *THINK* that's what's going on. I've seen them at 64 or 512 on disks
18// with 512-byte blocks and at 2048 on disks with 2048-byte blocks. The
19// LABEL_OFFSET2 value will be replaced by the block size in the
20// ReadBSDData() function....
21#define LABEL_OFFSET1 64
22#define LABEL_OFFSET2 512
23#define NUM_OFFSETS 2
srs5694a0eb11a2009-08-29 15:00:08 -040024
25// FreeBSD documents a maximum # of partitions of 8, but I saw 16 on a NetBSD
26// disk. I'm quadrupling that for further safety. Note that BSDReadData()
srs5694ba00fed2010-01-12 18:18:36 -050027// uses a 4096-byte I/O buffer. In combination with LABEL_OFFSET3 and the
srs5694a0eb11a2009-08-29 15:00:08 -040028// additional 148-byte offset to the actual partition data, that gives a
srs5694ba00fed2010-01-12 18:18:36 -050029// theoretical maximum of 118.75 partitions that the program can handle before
30// memory errors will occur.
srs5694a0eb11a2009-08-29 15:00:08 -040031#define MAX_BSD_PARTS 64
32
33
34using namespace std;
35
36/****************************************
37 * *
38 * BSDData class and related structures *
39 * *
40 ****************************************/
41
42// Possible states of the MBR
43enum BSDValidity {unknown, bsd_invalid, bsd};
44
45// Data for a single BSD partition record
srs5694e4ac11e2009-08-31 10:13:04 -040046// Create entries for all fields, although we only use lengthLBA, firstLBA,
47// and fsType, to simplify loading the data from disk....
48struct BSDRecord { // the partition table
49 uint32_t lengthLBA; // number of sectors in partition
50 uint32_t firstLBA; // starting sector
51 uint32_t fragSize; // filesystem basic fragment size
52 uint8_t fsType; // filesystem type, see below
53 uint8_t frag; // filesystem fragments per block
54 uint16_t pcpg; // filesystem cylinders per group
srs5694a0eb11a2009-08-29 15:00:08 -040055};
56
srs5694ba00fed2010-01-12 18:18:36 -050057// Full data in tweaked BSD format
srs5694a0eb11a2009-08-29 15:00:08 -040058class BSDData {
59 protected:
60 // We only need a few items from the main BSD disklabel data structure....
srs5694e4ac11e2009-08-31 10:13:04 -040061 uint32_t signature; // the magic number
62 uint32_t sectorSize; // # of bytes per sector
63 uint32_t signature2; // the magic number (again)
64 uint16_t numParts; // number of partitions in table
65 BSDRecord* partitions; // partition array
srs5694a0eb11a2009-08-29 15:00:08 -040066
67 // Above are basic BSD disklabel data; now add more stuff....
srs5694e4ac11e2009-08-31 10:13:04 -040068 uint64_t labelFirstLBA; // first sector of BSD disklabel (partition or disk)
69 uint64_t labelLastLBA; // final sector of BSD disklabel
70 uint64_t labelStart; // BSD disklabel start point in bytes from labelFirstLBA
srs5694a0eb11a2009-08-29 15:00:08 -040071 BSDValidity state;
srs5694a0eb11a2009-08-29 15:00:08 -040072 public:
73 BSDData(void);
74 ~BSDData(void);
srs5694fed16d02010-01-27 23:03:40 -050075 int ReadBSDData(string *deviceFilename, uint64_t startSector, uint64_t endSector);
76 int ReadBSDData(DiskIO *myDisk, uint64_t startSector, uint64_t endSector);
srs5694a0eb11a2009-08-29 15:00:08 -040077 void ReverseMetaBytes(void);
78 void DisplayBSDData(void);
srs5694a0eb11a2009-08-29 15:00:08 -040079 int ShowState(void); // returns 1 if BSD disklabel detected
srs5694a6022b02010-01-19 16:17:20 -050080 int IsDisklabel(void);
srs5694a0eb11a2009-08-29 15:00:08 -040081
82 // Functions to extract data on specific partitions....
83 uint8_t GetType(int i);
84 uint64_t GetFirstSector(int i);
85 uint64_t GetLength(int i);
86 int GetNumParts(void);
87 GPTPart AsGPT(int i); // Return BSD part. as GPT part.
88}; // struct MBRData
89
90#endif