blob: 1db68ea89f95fcfc1f679eca6f500e199567d3f5 [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>
srs5694a0eb11a2009-08-29 15:00:08 -040017#include <sys/stat.h>
18#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050019#include <iostream>
20#include <string>
srs5694a0eb11a2009-08-29 15:00:08 -040021#include "support.h"
22#include "bsd.h"
23
24using namespace std;
25
26
27BSDData::BSDData(void) {
28 state = unknown;
29 signature = UINT32_C(0);
30 signature2 = UINT32_C(0);
31 sectorSize = 512;
32 numParts = 0;
33 labelFirstLBA = 0;
34 labelLastLBA = 0;
35 labelStart = LABEL_OFFSET1; // assume raw disk format
srs5694a0eb11a2009-08-29 15:00:08 -040036 partitions = NULL;
37} // default constructor
38
39BSDData::~BSDData(void) {
40 free(partitions);
41} // destructor
42
srs5694e4ac11e2009-08-31 10:13:04 -040043// Read BSD disklabel data from the specified device filename. This function
44// just opens the device file and then calls an overloaded function to do
srs5694fed16d02010-01-27 23:03:40 -050045// the bulk of the work. Returns 1 on success, 0 on failure.
46int BSDData::ReadBSDData(string *device, uint64_t startSector, uint64_t endSector) {
47 int allOK = 1;
48 DiskIO myDisk;
srs5694a0eb11a2009-08-29 15:00:08 -040049
srs5694fed16d02010-01-27 23:03:40 -050050 if (*device != "") {
51 if (myDisk.OpenForRead(*device)) {
52 allOK = ReadBSDData(&myDisk, startSector, endSector);
srs5694e4ac11e2009-08-31 10:13:04 -040053 } else {
54 allOK = 0;
55 } // if/else
56
srs5694fed16d02010-01-27 23:03:40 -050057 myDisk.Close();
srs5694a0eb11a2009-08-29 15:00:08 -040058 } else {
59 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -040060 } // if/else
srs5694a0eb11a2009-08-29 15:00:08 -040061 return allOK;
62} // BSDData::ReadBSDData() (device filename version)
63
64// Load the BSD disklabel data from an already-opened disk
65// file, starting with the specified sector number.
srs5694fed16d02010-01-27 23:03:40 -050066int BSDData::ReadBSDData(DiskIO *theDisk, uint64_t startSector, uint64_t endSector) {
srs56941e093722010-01-05 00:14:19 -050067 uint8_t buffer[4096]; // I/O buffer
srs5694fed16d02010-01-27 23:03:40 -050068 int i, err, foundSig = 0, bigEnd = 0, allOK = 1;
srs5694a0eb11a2009-08-29 15:00:08 -040069 int relative = 0; // assume absolute partition sector numbering
70 uint32_t realSig;
71 uint32_t* temp32;
72 uint16_t* temp16;
73 BSDRecord* tempRecords;
srs5694546a9c72010-01-26 16:00:26 -050074 int offset[NUM_OFFSETS] = { LABEL_OFFSET1, LABEL_OFFSET2 };
srs5694a0eb11a2009-08-29 15:00:08 -040075
srs5694fed16d02010-01-27 23:03:40 -050076// myDisk = theDisk;
srs5694a0eb11a2009-08-29 15:00:08 -040077 labelFirstLBA = startSector;
78 labelLastLBA = endSector;
srs5694fed16d02010-01-27 23:03:40 -050079 offset[1] = theDisk->GetBlockSize();
srs5694a0eb11a2009-08-29 15:00:08 -040080
srs5694ba00fed2010-01-12 18:18:36 -050081 // Read 4096 bytes (eight 512-byte sectors or equivalent)
82 // into memory; we'll extract data from this buffer.
83 // (Done to work around FreeBSD limitation on size of reads
84 // from block devices.)
srs5694fed16d02010-01-27 23:03:40 -050085 allOK = theDisk->Seek(startSector);
86 if (allOK) allOK = theDisk->Read(buffer, 4096);
srs5694a0eb11a2009-08-29 15:00:08 -040087
88 // Do some strangeness to support big-endian architectures...
89 bigEnd = (IsLittleEndian() == 0);
90 realSig = BSD_SIGNATURE;
srs5694fed16d02010-01-27 23:03:40 -050091 if (bigEnd && allOK)
srs5694a0eb11a2009-08-29 15:00:08 -040092 ReverseBytes(&realSig, 4);
93
srs5694546a9c72010-01-26 16:00:26 -050094 // Look for the signature at any of two locations.
srs5694ba00fed2010-01-12 18:18:36 -050095 // Note that the signature is repeated at both the original
96 // offset and 132 bytes later, so we need two checks....
srs5694fed16d02010-01-27 23:03:40 -050097 if (allOK) {
98 i = 0;
99 do {
100 temp32 = (uint32_t*) &buffer[offset[i]];
101 signature = *temp32;
102 if (signature == realSig) { // found first, look for second
103 temp32 = (uint32_t*) &buffer[offset[i] + 132];
104 signature2 = *temp32;
105 if (signature2 == realSig) {
106 foundSig = 1;
107 labelStart = offset[i];
108 } // if found signature
109 } // if/else
110 i++;
111 } while ((!foundSig) && (i < NUM_OFFSETS));
112 allOK = foundSig;
113 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400114
115 // Load partition metadata from the buffer....
srs5694fed16d02010-01-27 23:03:40 -0500116 if (allOK) {
117 temp32 = (uint32_t*) &buffer[labelStart + 40];
118 sectorSize = *temp32;
119 temp16 = (uint16_t*) &buffer[labelStart + 138];
120 numParts = *temp16;
121 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400122
123 // Make it big-endian-aware....
srs5694fed16d02010-01-27 23:03:40 -0500124 if ((IsLittleEndian() == 0) && allOK)
srs5694a0eb11a2009-08-29 15:00:08 -0400125 ReverseMetaBytes();
126
127 // Check validity of the data and flag it appropriately....
srs5694fed16d02010-01-27 23:03:40 -0500128 if (foundSig && (numParts <= MAX_BSD_PARTS) && allOK) {
srs5694a0eb11a2009-08-29 15:00:08 -0400129 state = bsd;
130 } else {
131 state = bsd_invalid;
132 } // if/else
133
134 // If the state is good, go ahead and load the main partition data....
135 if (state == bsd) {
136 partitions = (struct BSDRecord*) malloc(numParts * sizeof (struct BSDRecord));
137 for (i = 0; i < numParts; i++) {
138 // Once again, we use the buffer, but index it using a BSDRecord
139 // pointer (dangerous, but effective)....
140 tempRecords = (BSDRecord*) &buffer[labelStart + 148];
141 partitions[i].lengthLBA = tempRecords[i].lengthLBA;
142 partitions[i].firstLBA = tempRecords[i].firstLBA;
143 partitions[i].fsType = tempRecords[i].fsType;
144 if (bigEnd) { // reverse data (fsType is a single byte)
145 ReverseBytes(&partitions[i].lengthLBA, 4);
146 ReverseBytes(&partitions[i].firstLBA, 4);
147 } // if big-endian
148 // Check for signs of relative sector numbering: A "0" first sector
149 // number on a partition with a non-zero length -- but ONLY if the
150 // length is less than the disk size, since NetBSD has a habit of
151 // creating a disk-sized partition within a carrier MBR partition
152 // that's too small to house it, and this throws off everything....
153 if ((partitions[i].firstLBA == 0) && (partitions[i].lengthLBA > 0)
154 && (partitions[i].lengthLBA < labelLastLBA))
155 relative = 1;
156 } // for
157 // Some disklabels use sector numbers relative to the enclosing partition's
158 // start, others use absolute sector numbers. If relative numbering was
159 // detected above, apply a correction to all partition start sectors....
160 if (relative) {
161 for (i = 0; i < numParts; i++) {
162 partitions[i].firstLBA += startSector;
163 } // for
164 } // if
165 } // if signatures OK
166// DisplayBSDData();
srs5694fed16d02010-01-27 23:03:40 -0500167 return allOK;
168} // BSDData::ReadBSDData(DiskIO* theDisk, uint64_t startSector)
srs5694a0eb11a2009-08-29 15:00:08 -0400169
170// Reverse metadata's byte order; called only on big-endian systems
171void BSDData::ReverseMetaBytes(void) {
172 ReverseBytes(&signature, 4);
173 ReverseBytes(&sectorSize, 4);
174 ReverseBytes(&signature2, 4);
175 ReverseBytes(&numParts, 2);
176} // BSDData::ReverseMetaByteOrder()
177
178// Display basic BSD partition data. Used for debugging.
179void BSDData::DisplayBSDData(void) {
180 int i;
181
182 if (state == bsd) {
srs5694fed16d02010-01-27 23:03:40 -0500183 cout << "BSD partitions:\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400184 for (i = 0; i < numParts; i++) {
srs5694fed16d02010-01-27 23:03:40 -0500185 cout.width(4);
186 cout << i + 1 << "\t";
187 cout.width(13);
188 cout << partitions[i].firstLBA << "\t";
189 cout.width(15);
190 cout << partitions[i].lengthLBA << " \t0x";
191 cout.width(2);
192 cout.fill('0');
193 cout.setf(ios::uppercase);
194 cout << hex << (int) partitions[i].fsType << "\n" << dec;
195 cout.fill(' ');
srs5694a0eb11a2009-08-29 15:00:08 -0400196 } // for
197 } // if
198} // BSDData::DisplayBSDData()
199
200// Displays the BSD disklabel state. Called during program launch to inform
201// the user about the partition table(s) status
202int BSDData::ShowState(void) {
203 int retval = 0;
204
205 switch (state) {
206 case bsd_invalid:
srs5694fed16d02010-01-27 23:03:40 -0500207 cout << " BSD: not present\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400208 break;
209 case bsd:
srs5694fed16d02010-01-27 23:03:40 -0500210 cout << " BSD: present\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400211 retval = 1;
212 break;
213 default:
srs5694fed16d02010-01-27 23:03:40 -0500214 cout << "\a BSD: unknown -- bug!\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400215 break;
216 } // switch
217 return retval;
218} // BSDData::ShowState()
219
srs5694a6022b02010-01-19 16:17:20 -0500220// Weirdly, this function has stopped working when defined inline,
221// but it's OK here....
222int BSDData::IsDisklabel(void) {
223 return (state == bsd);
224} // BSDData::IsDiskLabel()
225
srs5694a0eb11a2009-08-29 15:00:08 -0400226// Returns the BSD table's partition type code
227uint8_t BSDData::GetType(int i) {
228 uint8_t retval = 0; // 0 = "unused"
229
230 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
231 retval = partitions[i].fsType;
232
233 return(retval);
234} // BSDData::GetType()
235
236// Returns the number of the first sector of the specified partition
237uint64_t BSDData::GetFirstSector(int i) {
238 uint64_t retval = UINT64_C(0);
239
240 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
241 retval = (uint64_t) partitions[i].firstLBA;
242
243 return retval;
244} // BSDData::GetFirstSector
245
246// Returns the length (in sectors) of the specified partition
247uint64_t BSDData::GetLength(int i) {
248 uint64_t retval = UINT64_C(0);
249
250 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
251 retval = (uint64_t) partitions[i].lengthLBA;
252
253 return retval;
254} // BSDData::GetLength()
255
256// Returns the number of partitions defined in the current table
257int BSDData::GetNumParts(void) {
258 return numParts;
259} // BSDData::GetNumParts()
260
261// Returns the specified partition as a GPT partition. Used in BSD-to-GPT
262// conversion process
263GPTPart BSDData::AsGPT(int i) {
264 GPTPart guid; // dump data in here, then return it
265 uint64_t sectorOne, sectorEnd; // first & last sectors of partition
266 char tempStr[NAME_SIZE]; // temporary string for holding GPT name
267 int passItOn = 1; // Set to 0 if partition is empty or invalid
268
269 guid.BlankPartition();
270 sectorOne = (uint64_t) partitions[i].firstLBA;
271 sectorEnd = sectorOne + (uint64_t) partitions[i].lengthLBA;
272 if (sectorEnd > 0) sectorEnd--;
273 // Note on above: BSD partitions sometimes have a length of 0 and a start
srs5694e4ac11e2009-08-31 10:13:04 -0400274 // sector of 0. With unsigned ints, the usual way (start + length - 1) to
275 // find the end will result in a huge number, which will be confusing.
276 // Thus, apply the "-1" part only if it's reasonable to do so.
srs5694a0eb11a2009-08-29 15:00:08 -0400277
278 // Do a few sanity checks on the partition before we pass it on....
279 // First, check that it falls within the bounds of its container
280 // and that it starts before it ends....
281 if ((sectorOne < labelFirstLBA) || (sectorEnd > labelLastLBA) || (sectorOne > sectorEnd))
282 passItOn = 0;
283 // Some disklabels include a pseudo-partition that's the size of the entire
284 // disk or containing partition. Don't return it.
285 if ((sectorOne <= labelFirstLBA) && (sectorEnd >= labelLastLBA) &&
286 (GetType(i) == 0))
287 passItOn = 0;
288 // If the end point is 0, it's not a valid partition.
srs5694e4ac11e2009-08-31 10:13:04 -0400289 if ((sectorEnd == 0) || (sectorEnd == labelFirstLBA))
srs5694a0eb11a2009-08-29 15:00:08 -0400290 passItOn = 0;
291
292 if (passItOn) {
293 guid.SetFirstLBA(sectorOne);
294 guid.SetLastLBA(sectorEnd);
295 // Now set a random unique GUID for the partition....
296 guid.SetUniqueGUID(1);
297 // ... zero out the attributes and name fields....
298 guid.SetAttributes(UINT64_C(0));
299 // Most BSD disklabel type codes seem to be archaic or rare.
300 // They're also ambiguous; a FreeBSD filesystem is impossible
301 // to distinguish from a NetBSD one. Thus, these code assignment
302 // are going to be rough to begin with. For a list of meanings,
303 // see http://fxr.watson.org/fxr/source/sys/dtype.h?v=DFBSD,
304 // or Google it.
305 switch (GetType(i)) {
306 case 1: // BSD swap
307 guid.SetType(0xa502); break;
308 case 7: // BSD FFS
309 guid.SetType(0xa503); break;
310 case 8: case 11: // MS-DOS or HPFS
311 guid.SetType(0x0700); break;
312 case 9: // log-structured fs
313 guid.SetType(0xa903); break;
314 case 13: // bootstrap
315 guid.SetType(0xa501); break;
316 case 14: // vinum
317 guid.SetType(0xa505); break;
318 case 15: // RAID
319 guid.SetType(0xa903); break;
320 case 27: // FreeBSD ZFS
321 guid.SetType(0xa504); break;
322 default:
323 guid.SetType(0x0700); break;
324 } // switch
325 // Set the partition name to the name of the type code....
srs5694fed16d02010-01-27 23:03:40 -0500326 guid.SetName(guid.GetNameType());
srs5694a0eb11a2009-08-29 15:00:08 -0400327 } // if
328 return guid;
329} // BSDData::AsGPT()