blob: 4bc28f45a29d9b01afba683e25f0dd346a5ba6a0 [file] [log] [blame]
srs5694e4ac11e2009-08-31 10:13:04 -04001/* bsd.cc -- Functions for loading and manipulating legacy BSD disklabel
srs5694a0eb11a2009-08-29 15:00:08 -04002 data. */
3
srs5694e4ac11e2009-08-31 10:13:04 -04004/* By Rod Smith, initial coding August, 2009 */
srs5694a0eb11a2009-08-29 15:00:08 -04005
6/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
7 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
9#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
12#include <stdio.h>
13#include <unistd.h>
14#include <stdlib.h>
15#include <stdint.h>
16#include <fcntl.h>
17#include <string.h>
srs5694a0eb11a2009-08-29 15:00:08 -040018#include <sys/stat.h>
19#include <errno.h>
srs5694a0eb11a2009-08-29 15:00:08 -040020#include "support.h"
21#include "bsd.h"
22
23using namespace std;
24
25
26BSDData::BSDData(void) {
27 state = unknown;
28 signature = UINT32_C(0);
29 signature2 = UINT32_C(0);
30 sectorSize = 512;
31 numParts = 0;
32 labelFirstLBA = 0;
33 labelLastLBA = 0;
34 labelStart = LABEL_OFFSET1; // assume raw disk format
srs5694a0eb11a2009-08-29 15:00:08 -040035 partitions = NULL;
36} // default constructor
37
38BSDData::~BSDData(void) {
39 free(partitions);
40} // destructor
41
srs5694e4ac11e2009-08-31 10:13:04 -040042// Read BSD disklabel data from the specified device filename. This function
43// just opens the device file and then calls an overloaded function to do
44// the bulk of the work.
srs5694a0eb11a2009-08-29 15:00:08 -040045int BSDData::ReadBSDData(char* device, uint64_t startSector, uint64_t endSector) {
46 int fd, allOK = 1;
47
srs5694e4ac11e2009-08-31 10:13:04 -040048 if (device != NULL) {
49 if ((fd = open(device, O_RDONLY)) != -1) {
50 ReadBSDData(fd, startSector, endSector);
51 } else {
52 allOK = 0;
53 } // if/else
54
55 close(fd);
srs5694a0eb11a2009-08-29 15:00:08 -040056 } else {
57 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -040058 } // if/else
srs5694a0eb11a2009-08-29 15:00:08 -040059
60 return allOK;
61} // BSDData::ReadBSDData() (device filename version)
62
63// Load the BSD disklabel data from an already-opened disk
64// file, starting with the specified sector number.
65void BSDData::ReadBSDData(int fd, uint64_t startSector, uint64_t endSector) {
66 uint8_t buffer[2048]; // I/O buffer
67 uint64_t startByte;
68 int i, err, foundSig = 0, bigEnd = 0;
69 int relative = 0; // assume absolute partition sector numbering
70 uint32_t realSig;
71 uint32_t* temp32;
72 uint16_t* temp16;
73 BSDRecord* tempRecords;
74
75 labelFirstLBA = startSector;
76 labelLastLBA = endSector;
77
78 // Read two sectors into memory; we'll extract data from
79 // this buffer. (Done to work around FreeBSD limitation)
80 lseek64(fd, startSector * 512, SEEK_SET);
81 err = read(fd, buffer, 2048);
82
83 // Do some strangeness to support big-endian architectures...
84 bigEnd = (IsLittleEndian() == 0);
85 realSig = BSD_SIGNATURE;
86 if (bigEnd)
87 ReverseBytes(&realSig, 4);
88
89 // Look for the signature at one of two locations
90 labelStart = LABEL_OFFSET1;
91 temp32 = (uint32_t*) &buffer[labelStart];
92 signature = *temp32;
93 if (signature == realSig) {
94 temp32 = (uint32_t*) &buffer[labelStart + 132];
95 signature2 = *temp32;
96 if (signature2 == realSig)
97 foundSig = 1;
98 } // if/else
99 if (!foundSig) { // look in second location
100 labelStart = LABEL_OFFSET2;
101 temp32 = (uint32_t*) &buffer[labelStart];
102 signature = *temp32;
103 if (signature == realSig) {
104 temp32 = (uint32_t*) &buffer[labelStart + 132];
105 signature2 = *temp32;
106 if (signature2 == realSig)
107 foundSig = 1;
108 } // if/else
109 } // if
110
111 // Load partition metadata from the buffer....
112 temp32 = (uint32_t*) &buffer[labelStart + 40];
113 sectorSize = *temp32;
114 temp16 = (uint16_t*) &buffer[labelStart + 138];
115 numParts = *temp16;
116
117 // Make it big-endian-aware....
118 if (IsLittleEndian() == 0)
119 ReverseMetaBytes();
120
121 // Check validity of the data and flag it appropriately....
122 if (foundSig && (numParts <= MAX_BSD_PARTS)) {
123 state = bsd;
124 } else {
125 state = bsd_invalid;
126 } // if/else
127
128 // If the state is good, go ahead and load the main partition data....
129 if (state == bsd) {
130 partitions = (struct BSDRecord*) malloc(numParts * sizeof (struct BSDRecord));
131 for (i = 0; i < numParts; i++) {
132 // Once again, we use the buffer, but index it using a BSDRecord
133 // pointer (dangerous, but effective)....
134 tempRecords = (BSDRecord*) &buffer[labelStart + 148];
135 partitions[i].lengthLBA = tempRecords[i].lengthLBA;
136 partitions[i].firstLBA = tempRecords[i].firstLBA;
137 partitions[i].fsType = tempRecords[i].fsType;
138 if (bigEnd) { // reverse data (fsType is a single byte)
139 ReverseBytes(&partitions[i].lengthLBA, 4);
140 ReverseBytes(&partitions[i].firstLBA, 4);
141 } // if big-endian
142 // Check for signs of relative sector numbering: A "0" first sector
143 // number on a partition with a non-zero length -- but ONLY if the
144 // length is less than the disk size, since NetBSD has a habit of
145 // creating a disk-sized partition within a carrier MBR partition
146 // that's too small to house it, and this throws off everything....
147 if ((partitions[i].firstLBA == 0) && (partitions[i].lengthLBA > 0)
148 && (partitions[i].lengthLBA < labelLastLBA))
149 relative = 1;
150 } // for
151 // Some disklabels use sector numbers relative to the enclosing partition's
152 // start, others use absolute sector numbers. If relative numbering was
153 // detected above, apply a correction to all partition start sectors....
154 if (relative) {
155 for (i = 0; i < numParts; i++) {
156 partitions[i].firstLBA += startSector;
157 } // for
158 } // if
159 } // if signatures OK
160// DisplayBSDData();
161} // BSDData::ReadBSDData(int fd, uint64_t startSector)
162
163// Reverse metadata's byte order; called only on big-endian systems
164void BSDData::ReverseMetaBytes(void) {
165 ReverseBytes(&signature, 4);
166 ReverseBytes(&sectorSize, 4);
167 ReverseBytes(&signature2, 4);
168 ReverseBytes(&numParts, 2);
169} // BSDData::ReverseMetaByteOrder()
170
171// Display basic BSD partition data. Used for debugging.
172void BSDData::DisplayBSDData(void) {
173 int i;
174
175 if (state == bsd) {
176 printf("BSD partitions:\n");
177 printf("Number\t Start (sector)\t Length (sectors)\tType\n");
178 for (i = 0; i < numParts; i++) {
179 printf("%4d\t%13lu\t%15lu \t0x%02X\n", i + 1,
180 (unsigned long) partitions[i].firstLBA,
181 (unsigned long) partitions[i].lengthLBA, partitions[i].fsType);
182 } // for
183 } // if
184} // BSDData::DisplayBSDData()
185
186// Displays the BSD disklabel state. Called during program launch to inform
187// the user about the partition table(s) status
188int BSDData::ShowState(void) {
189 int retval = 0;
190
191 switch (state) {
192 case bsd_invalid:
193 printf(" BSD: not present\n");
194 break;
195 case bsd:
196 printf(" BSD: present\n");
197 retval = 1;
198 break;
199 default:
200 printf("\a BSD: unknown -- bug!\n");
201 break;
202 } // switch
203 return retval;
204} // BSDData::ShowState()
205
206// Returns the BSD table's partition type code
207uint8_t BSDData::GetType(int i) {
208 uint8_t retval = 0; // 0 = "unused"
209
210 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
211 retval = partitions[i].fsType;
212
213 return(retval);
214} // BSDData::GetType()
215
216// Returns the number of the first sector of the specified partition
217uint64_t BSDData::GetFirstSector(int i) {
218 uint64_t retval = UINT64_C(0);
219
220 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
221 retval = (uint64_t) partitions[i].firstLBA;
222
223 return retval;
224} // BSDData::GetFirstSector
225
226// Returns the length (in sectors) of the specified partition
227uint64_t BSDData::GetLength(int i) {
228 uint64_t retval = UINT64_C(0);
229
230 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
231 retval = (uint64_t) partitions[i].lengthLBA;
232
233 return retval;
234} // BSDData::GetLength()
235
236// Returns the number of partitions defined in the current table
237int BSDData::GetNumParts(void) {
238 return numParts;
239} // BSDData::GetNumParts()
240
241// Returns the specified partition as a GPT partition. Used in BSD-to-GPT
242// conversion process
243GPTPart BSDData::AsGPT(int i) {
244 GPTPart guid; // dump data in here, then return it
245 uint64_t sectorOne, sectorEnd; // first & last sectors of partition
246 char tempStr[NAME_SIZE]; // temporary string for holding GPT name
247 int passItOn = 1; // Set to 0 if partition is empty or invalid
248
249 guid.BlankPartition();
250 sectorOne = (uint64_t) partitions[i].firstLBA;
251 sectorEnd = sectorOne + (uint64_t) partitions[i].lengthLBA;
252 if (sectorEnd > 0) sectorEnd--;
253 // Note on above: BSD partitions sometimes have a length of 0 and a start
srs5694e4ac11e2009-08-31 10:13:04 -0400254 // sector of 0. With unsigned ints, the usual way (start + length - 1) to
255 // find the end will result in a huge number, which will be confusing.
256 // Thus, apply the "-1" part only if it's reasonable to do so.
srs5694a0eb11a2009-08-29 15:00:08 -0400257
258 // Do a few sanity checks on the partition before we pass it on....
259 // First, check that it falls within the bounds of its container
260 // and that it starts before it ends....
261 if ((sectorOne < labelFirstLBA) || (sectorEnd > labelLastLBA) || (sectorOne > sectorEnd))
262 passItOn = 0;
263 // Some disklabels include a pseudo-partition that's the size of the entire
264 // disk or containing partition. Don't return it.
265 if ((sectorOne <= labelFirstLBA) && (sectorEnd >= labelLastLBA) &&
266 (GetType(i) == 0))
267 passItOn = 0;
268 // If the end point is 0, it's not a valid partition.
srs5694e4ac11e2009-08-31 10:13:04 -0400269 if ((sectorEnd == 0) || (sectorEnd == labelFirstLBA))
srs5694a0eb11a2009-08-29 15:00:08 -0400270 passItOn = 0;
271
272 if (passItOn) {
273 guid.SetFirstLBA(sectorOne);
274 guid.SetLastLBA(sectorEnd);
275 // Now set a random unique GUID for the partition....
276 guid.SetUniqueGUID(1);
277 // ... zero out the attributes and name fields....
278 guid.SetAttributes(UINT64_C(0));
279 // Most BSD disklabel type codes seem to be archaic or rare.
280 // They're also ambiguous; a FreeBSD filesystem is impossible
281 // to distinguish from a NetBSD one. Thus, these code assignment
282 // are going to be rough to begin with. For a list of meanings,
283 // see http://fxr.watson.org/fxr/source/sys/dtype.h?v=DFBSD,
284 // or Google it.
285 switch (GetType(i)) {
286 case 1: // BSD swap
287 guid.SetType(0xa502); break;
288 case 7: // BSD FFS
289 guid.SetType(0xa503); break;
290 case 8: case 11: // MS-DOS or HPFS
291 guid.SetType(0x0700); break;
292 case 9: // log-structured fs
293 guid.SetType(0xa903); break;
294 case 13: // bootstrap
295 guid.SetType(0xa501); break;
296 case 14: // vinum
297 guid.SetType(0xa505); break;
298 case 15: // RAID
299 guid.SetType(0xa903); break;
300 case 27: // FreeBSD ZFS
301 guid.SetType(0xa504); break;
302 default:
303 guid.SetType(0x0700); break;
304 } // switch
305 // Set the partition name to the name of the type code....
306 guid.SetName((unsigned char*) guid.GetNameType(tempStr));
307 } // if
308 return guid;
309} // BSDData::AsGPT()