1cf200d32Sopenharmony_ci/*
2cf200d32Sopenharmony_ci    MBRPart class, part of GPT fdisk program family.
3cf200d32Sopenharmony_ci    Copyright (C) 2011  Roderick W. Smith
4cf200d32Sopenharmony_ci
5cf200d32Sopenharmony_ci    This program is free software; you can redistribute it and/or modify
6cf200d32Sopenharmony_ci    it under the terms of the GNU General Public License as published by
7cf200d32Sopenharmony_ci    the Free Software Foundation; either version 2 of the License, or
8cf200d32Sopenharmony_ci    (at your option) any later version.
9cf200d32Sopenharmony_ci
10cf200d32Sopenharmony_ci    This program is distributed in the hope that it will be useful,
11cf200d32Sopenharmony_ci    but WITHOUT ANY WARRANTY; without even the implied warranty of
12cf200d32Sopenharmony_ci    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13cf200d32Sopenharmony_ci    GNU General Public License for more details.
14cf200d32Sopenharmony_ci
15cf200d32Sopenharmony_ci    You should have received a copy of the GNU General Public License along
16cf200d32Sopenharmony_ci    with this program; if not, write to the Free Software Foundation, Inc.,
17cf200d32Sopenharmony_ci    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18cf200d32Sopenharmony_ci*/
19cf200d32Sopenharmony_ci
20cf200d32Sopenharmony_ci#ifndef MBRPART_H
21cf200d32Sopenharmony_ci#define MBRPART_H
22cf200d32Sopenharmony_ci
23cf200d32Sopenharmony_ci#include <stdint.h>
24cf200d32Sopenharmony_ci
25cf200d32Sopenharmony_ci#define MAX_HEADS 255        /* numbered 0 - 254 */
26cf200d32Sopenharmony_ci#define MAX_SECSPERTRACK 63  /* numbered 1 - 63 */
27cf200d32Sopenharmony_ci#define MAX_CYLINDERS 1024   /* numbered 0 - 1023 */
28cf200d32Sopenharmony_ci
29cf200d32Sopenharmony_ci#define NONE 0    /* don't include partition when writing */
30cf200d32Sopenharmony_ci#define PRIMARY 1 /* write partition as primary */
31cf200d32Sopenharmony_ci#define LOGICAL 2 /* write partition as logical */
32cf200d32Sopenharmony_ci#define EBR 4     /* sector is used as an EBR or MBR */
33cf200d32Sopenharmony_ci#define INVALID 8 /* sector number is too large for disk */
34cf200d32Sopenharmony_ci
35cf200d32Sopenharmony_ci// Data for a single MBR partition record
36cf200d32Sopenharmony_ci// Note that firstSector and lastSector are in CHS addressing, which
37cf200d32Sopenharmony_ci// splits the bits up in a weird way.
38cf200d32Sopenharmony_ci// On read or write of MBR entries, firstLBA is an absolute disk sector.
39cf200d32Sopenharmony_ci// On read of logical entries, it's relative to the EBR record for that
40cf200d32Sopenharmony_ci// partition. When writing EBR records, it's relative to the extended
41cf200d32Sopenharmony_ci// partition's start.
42cf200d32Sopenharmony_ci#pragma pack(1)
43cf200d32Sopenharmony_cistruct MBRRecord {
44cf200d32Sopenharmony_ci   uint8_t status;
45cf200d32Sopenharmony_ci   uint8_t firstSector[3];
46cf200d32Sopenharmony_ci   uint8_t partitionType;
47cf200d32Sopenharmony_ci   uint8_t lastSector[3];
48cf200d32Sopenharmony_ci   uint32_t firstLBA; // see above
49cf200d32Sopenharmony_ci   uint32_t lengthLBA;
50cf200d32Sopenharmony_ci}; // struct MBRRecord
51cf200d32Sopenharmony_ci#pragma pack ()
52cf200d32Sopenharmony_ci
53cf200d32Sopenharmony_ciclass MBRPart {
54cf200d32Sopenharmony_ciprotected:
55cf200d32Sopenharmony_ci   uint8_t status;
56cf200d32Sopenharmony_ci   uint8_t firstSector[3];
57cf200d32Sopenharmony_ci   uint8_t partitionType;
58cf200d32Sopenharmony_ci   uint8_t lastSector[3];
59cf200d32Sopenharmony_ci   uint32_t firstLBA; // see above
60cf200d32Sopenharmony_ci   uint32_t lengthLBA;
61cf200d32Sopenharmony_ci   int includeAs; // PRIMARY, LOGICAL, or NONE
62cf200d32Sopenharmony_ci   int canBeLogical;
63cf200d32Sopenharmony_ci   int canBePrimary;
64cf200d32Sopenharmony_ci   static uint32_t numHeads;
65cf200d32Sopenharmony_ci   static uint32_t numSecspTrack;
66cf200d32Sopenharmony_ci   static uint64_t diskSize;
67cf200d32Sopenharmony_ci   static uint32_t blockSize;
68cf200d32Sopenharmony_ci   static int numInstances;
69cf200d32Sopenharmony_ci
70cf200d32Sopenharmony_cipublic:
71cf200d32Sopenharmony_ci    MBRPart();
72cf200d32Sopenharmony_ci    MBRPart(const MBRPart& other);
73cf200d32Sopenharmony_ci    virtual ~MBRPart();
74cf200d32Sopenharmony_ci    virtual MBRPart& operator=(const MBRPart& orig);
75cf200d32Sopenharmony_ci    virtual MBRPart& operator=(const struct MBRRecord& orig);
76cf200d32Sopenharmony_ci    bool operator<(const MBRPart &other) const;
77cf200d32Sopenharmony_ci
78cf200d32Sopenharmony_ci    // Set information on partitions or disks...
79cf200d32Sopenharmony_ci    void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs);
80cf200d32Sopenharmony_ci    void Empty(void);
81cf200d32Sopenharmony_ci    void SetStartLBA(uint64_t s);
82cf200d32Sopenharmony_ci    void SetLengthLBA(uint64_t l);
83cf200d32Sopenharmony_ci    void SetLocation(uint64_t start, uint64_t length);
84cf200d32Sopenharmony_ci    int SetType(uint8_t typeCode, int isExtended = 0);
85cf200d32Sopenharmony_ci    void SetStatus(uint8_t s) {status = s;}
86cf200d32Sopenharmony_ci    void SetInclusion(int status = PRIMARY) {includeAs = status;}
87cf200d32Sopenharmony_ci    void SetCanBeLogical(int c) {canBeLogical = c;}
88cf200d32Sopenharmony_ci    void SetCanBePrimary(int c) {canBePrimary = c;}
89cf200d32Sopenharmony_ci    void StoreInStruct(struct MBRRecord *theStruct);
90cf200d32Sopenharmony_ci
91cf200d32Sopenharmony_ci    // Get information on partitions or disk....
92cf200d32Sopenharmony_ci    uint8_t GetType(void) {return partitionType;}
93cf200d32Sopenharmony_ci    uint8_t GetStatus(void) {return status;}
94cf200d32Sopenharmony_ci    uint64_t GetStartLBA(void) {return firstLBA;}
95cf200d32Sopenharmony_ci    uint64_t GetLengthLBA(void) {return lengthLBA;}
96cf200d32Sopenharmony_ci    uint64_t GetLastLBA(void) const;
97cf200d32Sopenharmony_ci    uint8_t GetInclusion(void) {return includeAs;}
98cf200d32Sopenharmony_ci    int CanBeLogical(void) {return canBeLogical;}
99cf200d32Sopenharmony_ci    int CanBePrimary(void) {return canBePrimary;}
100cf200d32Sopenharmony_ci    int DoTheyOverlap (const MBRPart& other);
101cf200d32Sopenharmony_ci
102cf200d32Sopenharmony_ci    // Adjust information on partitions or disks...
103cf200d32Sopenharmony_ci    int RecomputeCHS(void);
104cf200d32Sopenharmony_ci    int LBAtoCHS(uint32_t lba, uint8_t * chs);
105cf200d32Sopenharmony_ci    void ReverseByteOrder(void);
106cf200d32Sopenharmony_ci
107cf200d32Sopenharmony_ci    // User I/O...
108cf200d32Sopenharmony_ci    void ShowData(int isGpt);
109cf200d32Sopenharmony_ci}; // MBRPart
110cf200d32Sopenharmony_ci
111cf200d32Sopenharmony_ci#endif // MBRPART_H
112