blob: ffbe5cccc78ce75ef873e646332b3ef1cb034d5b [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
srs569408bb0da2010-02-19 17:19:55 -050058// For some reason this has to be packed or MS Visual C++'s debugger complains
59// about memory errors whenever a BSDData variable is destroyed.
60#pragma pack (8)
srs5694a0eb11a2009-08-29 15:00:08 -040061class BSDData {
62 protected:
63 // We only need a few items from the main BSD disklabel data structure....
srs5694e4ac11e2009-08-31 10:13:04 -040064 uint32_t signature; // the magic number
65 uint32_t sectorSize; // # of bytes per sector
66 uint32_t signature2; // the magic number (again)
67 uint16_t numParts; // number of partitions in table
srs569408bb0da2010-02-19 17:19:55 -050068 struct BSDRecord* partitions; // partition array
srs5694a0eb11a2009-08-29 15:00:08 -040069
70 // Above are basic BSD disklabel data; now add more stuff....
srs5694e4ac11e2009-08-31 10:13:04 -040071 uint64_t labelFirstLBA; // first sector of BSD disklabel (partition or disk)
72 uint64_t labelLastLBA; // final sector of BSD disklabel
73 uint64_t labelStart; // BSD disklabel start point in bytes from labelFirstLBA
srs5694a0eb11a2009-08-29 15:00:08 -040074 BSDValidity state;
srs5694a0eb11a2009-08-29 15:00:08 -040075 public:
76 BSDData(void);
77 ~BSDData(void);
srs56940a697312010-01-28 21:10:52 -050078 int ReadBSDData(const string & deviceFilename, uint64_t startSector, uint64_t endSector);
srs5694fed16d02010-01-27 23:03:40 -050079 int ReadBSDData(DiskIO *myDisk, uint64_t startSector, uint64_t endSector);
srs5694a0eb11a2009-08-29 15:00:08 -040080 void ReverseMetaBytes(void);
81 void DisplayBSDData(void);
srs5694a0eb11a2009-08-29 15:00:08 -040082 int ShowState(void); // returns 1 if BSD disklabel detected
srs5694a6022b02010-01-19 16:17:20 -050083 int IsDisklabel(void);
srs5694a0eb11a2009-08-29 15:00:08 -040084
85 // Functions to extract data on specific partitions....
86 uint8_t GetType(int i);
87 uint64_t GetFirstSector(int i);
88 uint64_t GetLength(int i);
89 int GetNumParts(void);
90 GPTPart AsGPT(int i); // Return BSD part. as GPT part.
91}; // struct MBRData
92
93#endif