xref: /third_party/gptfdisk/bsd.h (revision cf200d32)
1/* 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#ifndef __BSD_STRUCTS
7#define __BSD_STRUCTS
8
9#include <stdint.h>
10#include <sys/types.h>
11#include "gptpart.h"
12#include "diskio.h"
13
14#define BSD_SIGNATURE UINT32_C(0x82564557)  /* BSD disklabel signature ("magic") */
15
16// 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
24
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()
27// uses a 4096-byte I/O buffer. In combination with LABEL_OFFSET3 and the
28// additional 148-byte offset to the actual partition data, that gives a
29// theoretical maximum of 118.75 partitions that the program can handle before
30// memory errors will occur.
31#define MAX_BSD_PARTS 64
32
33/****************************************
34 *                                      *
35 * BSDData class and related structures *
36 *                                      *
37 ****************************************/
38
39// Possible states of the MBR
40enum BSDValidity {unknown, bsd_invalid, bsd};
41
42// Data for a single BSD partition record
43// Create entries for all fields, although we only use lengthLBA, firstLBA,
44// and fsType, to simplify loading the data from disk....
45struct  BSDRecord {      // the partition table
46   uint32_t lengthLBA;   // number of sectors in partition
47   uint32_t firstLBA;    // starting sector
48   uint32_t fragSize;    // filesystem basic fragment size
49   uint8_t  fsType;      // filesystem type, see below
50   uint8_t  frag;        // filesystem fragments per block
51   uint16_t pcpg;        // filesystem cylinders per group
52};
53
54// Full data in tweaked BSD format
55// For some reason this has to be packed or MS Visual C++'s debugger complains
56// about memory errors whenever a BSDData variable is destroyed.
57#pragma pack (8)
58class BSDData {
59   protected:
60      // We only need a few items from the main BSD disklabel data structure....
61      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      struct BSDRecord* partitions;     // partition array
66
67      // Above are basic BSD disklabel data; now add more stuff....
68      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
71      BSDValidity state;
72   public:
73      BSDData(void);
74      ~BSDData(void);
75      int ReadBSDData(const std::string & deviceFilename, uint64_t startSector, uint64_t endSector);
76      int ReadBSDData(DiskIO *myDisk, uint64_t startSector, uint64_t endSector);
77      void ReverseMetaBytes(void);
78      void DisplayBSDData(void);
79      int ShowState(void); // returns 1 if BSD disklabel detected
80      int IsDisklabel(void);
81
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#pragma pack ()
90
91#endif
92