blob: 75dca603e81ad8cacac25f49db83ad60c2262430 [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.
srs56940a697312010-01-28 21:10:52 -050046int BSDData::ReadBSDData(const string & device, uint64_t startSector, uint64_t endSector) {
srs5694fed16d02010-01-27 23:03:40 -050047 int allOK = 1;
48 DiskIO myDisk;
srs5694a0eb11a2009-08-29 15:00:08 -040049
srs56940a697312010-01-28 21:10:52 -050050 if (device != "") {
51 if (myDisk.OpenForRead(device)) {
srs5694fed16d02010-01-27 23:03:40 -050052 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
srs5694e321d442010-01-29 17:44:04 -050068 int i, 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
76 labelFirstLBA = startSector;
77 labelLastLBA = endSector;
srs5694fed16d02010-01-27 23:03:40 -050078 offset[1] = theDisk->GetBlockSize();
srs5694a0eb11a2009-08-29 15:00:08 -040079
srs5694ba00fed2010-01-12 18:18:36 -050080 // Read 4096 bytes (eight 512-byte sectors or equivalent)
81 // into memory; we'll extract data from this buffer.
82 // (Done to work around FreeBSD limitation on size of reads
83 // from block devices.)
srs5694fed16d02010-01-27 23:03:40 -050084 allOK = theDisk->Seek(startSector);
85 if (allOK) allOK = theDisk->Read(buffer, 4096);
srs5694a0eb11a2009-08-29 15:00:08 -040086
87 // Do some strangeness to support big-endian architectures...
88 bigEnd = (IsLittleEndian() == 0);
89 realSig = BSD_SIGNATURE;
srs5694fed16d02010-01-27 23:03:40 -050090 if (bigEnd && allOK)
srs5694a0eb11a2009-08-29 15:00:08 -040091 ReverseBytes(&realSig, 4);
92
srs5694546a9c72010-01-26 16:00:26 -050093 // Look for the signature at any of two locations.
srs5694ba00fed2010-01-12 18:18:36 -050094 // Note that the signature is repeated at both the original
95 // offset and 132 bytes later, so we need two checks....
srs5694fed16d02010-01-27 23:03:40 -050096 if (allOK) {
97 i = 0;
98 do {
99 temp32 = (uint32_t*) &buffer[offset[i]];
100 signature = *temp32;
101 if (signature == realSig) { // found first, look for second
102 temp32 = (uint32_t*) &buffer[offset[i] + 132];
103 signature2 = *temp32;
104 if (signature2 == realSig) {
105 foundSig = 1;
106 labelStart = offset[i];
107 } // if found signature
108 } // if/else
109 i++;
110 } while ((!foundSig) && (i < NUM_OFFSETS));
111 allOK = foundSig;
112 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400113
114 // Load partition metadata from the buffer....
srs5694fed16d02010-01-27 23:03:40 -0500115 if (allOK) {
116 temp32 = (uint32_t*) &buffer[labelStart + 40];
117 sectorSize = *temp32;
118 temp16 = (uint16_t*) &buffer[labelStart + 138];
119 numParts = *temp16;
120 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400121
122 // Make it big-endian-aware....
srs5694fed16d02010-01-27 23:03:40 -0500123 if ((IsLittleEndian() == 0) && allOK)
srs5694a0eb11a2009-08-29 15:00:08 -0400124 ReverseMetaBytes();
125
126 // Check validity of the data and flag it appropriately....
srs5694fed16d02010-01-27 23:03:40 -0500127 if (foundSig && (numParts <= MAX_BSD_PARTS) && allOK) {
srs5694a0eb11a2009-08-29 15:00:08 -0400128 state = bsd;
129 } else {
130 state = bsd_invalid;
131 } // if/else
132
133 // If the state is good, go ahead and load the main partition data....
134 if (state == bsd) {
135 partitions = (struct BSDRecord*) malloc(numParts * sizeof (struct BSDRecord));
136 for (i = 0; i < numParts; i++) {
137 // Once again, we use the buffer, but index it using a BSDRecord
138 // pointer (dangerous, but effective)....
139 tempRecords = (BSDRecord*) &buffer[labelStart + 148];
140 partitions[i].lengthLBA = tempRecords[i].lengthLBA;
141 partitions[i].firstLBA = tempRecords[i].firstLBA;
142 partitions[i].fsType = tempRecords[i].fsType;
143 if (bigEnd) { // reverse data (fsType is a single byte)
144 ReverseBytes(&partitions[i].lengthLBA, 4);
145 ReverseBytes(&partitions[i].firstLBA, 4);
146 } // if big-endian
147 // Check for signs of relative sector numbering: A "0" first sector
148 // number on a partition with a non-zero length -- but ONLY if the
149 // length is less than the disk size, since NetBSD has a habit of
150 // creating a disk-sized partition within a carrier MBR partition
151 // that's too small to house it, and this throws off everything....
152 if ((partitions[i].firstLBA == 0) && (partitions[i].lengthLBA > 0)
153 && (partitions[i].lengthLBA < labelLastLBA))
154 relative = 1;
155 } // for
156 // Some disklabels use sector numbers relative to the enclosing partition's
157 // start, others use absolute sector numbers. If relative numbering was
158 // detected above, apply a correction to all partition start sectors....
159 if (relative) {
160 for (i = 0; i < numParts; i++) {
161 partitions[i].firstLBA += startSector;
162 } // for
163 } // if
164 } // if signatures OK
165// DisplayBSDData();
srs5694fed16d02010-01-27 23:03:40 -0500166 return allOK;
167} // BSDData::ReadBSDData(DiskIO* theDisk, uint64_t startSector)
srs5694a0eb11a2009-08-29 15:00:08 -0400168
169// Reverse metadata's byte order; called only on big-endian systems
170void BSDData::ReverseMetaBytes(void) {
171 ReverseBytes(&signature, 4);
172 ReverseBytes(&sectorSize, 4);
173 ReverseBytes(&signature2, 4);
174 ReverseBytes(&numParts, 2);
175} // BSDData::ReverseMetaByteOrder()
176
177// Display basic BSD partition data. Used for debugging.
178void BSDData::DisplayBSDData(void) {
179 int i;
180
181 if (state == bsd) {
srs5694fed16d02010-01-27 23:03:40 -0500182 cout << "BSD partitions:\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400183 for (i = 0; i < numParts; i++) {
srs5694fed16d02010-01-27 23:03:40 -0500184 cout.width(4);
185 cout << i + 1 << "\t";
186 cout.width(13);
187 cout << partitions[i].firstLBA << "\t";
188 cout.width(15);
189 cout << partitions[i].lengthLBA << " \t0x";
190 cout.width(2);
191 cout.fill('0');
192 cout.setf(ios::uppercase);
193 cout << hex << (int) partitions[i].fsType << "\n" << dec;
194 cout.fill(' ');
srs5694a0eb11a2009-08-29 15:00:08 -0400195 } // for
196 } // if
197} // BSDData::DisplayBSDData()
198
199// Displays the BSD disklabel state. Called during program launch to inform
200// the user about the partition table(s) status
201int BSDData::ShowState(void) {
202 int retval = 0;
203
204 switch (state) {
205 case bsd_invalid:
srs5694fed16d02010-01-27 23:03:40 -0500206 cout << " BSD: not present\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400207 break;
208 case bsd:
srs5694fed16d02010-01-27 23:03:40 -0500209 cout << " BSD: present\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400210 retval = 1;
211 break;
212 default:
srs5694fed16d02010-01-27 23:03:40 -0500213 cout << "\a BSD: unknown -- bug!\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400214 break;
215 } // switch
216 return retval;
217} // BSDData::ShowState()
218
srs5694a6022b02010-01-19 16:17:20 -0500219// Weirdly, this function has stopped working when defined inline,
220// but it's OK here....
221int BSDData::IsDisklabel(void) {
222 return (state == bsd);
223} // BSDData::IsDiskLabel()
224
srs5694a0eb11a2009-08-29 15:00:08 -0400225// Returns the BSD table's partition type code
226uint8_t BSDData::GetType(int i) {
227 uint8_t retval = 0; // 0 = "unused"
228
229 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
230 retval = partitions[i].fsType;
231
232 return(retval);
233} // BSDData::GetType()
234
235// Returns the number of the first sector of the specified partition
236uint64_t BSDData::GetFirstSector(int i) {
237 uint64_t retval = UINT64_C(0);
238
239 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
240 retval = (uint64_t) partitions[i].firstLBA;
241
242 return retval;
243} // BSDData::GetFirstSector
244
245// Returns the length (in sectors) of the specified partition
246uint64_t BSDData::GetLength(int i) {
247 uint64_t retval = UINT64_C(0);
248
249 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
250 retval = (uint64_t) partitions[i].lengthLBA;
251
252 return retval;
253} // BSDData::GetLength()
254
255// Returns the number of partitions defined in the current table
256int BSDData::GetNumParts(void) {
257 return numParts;
258} // BSDData::GetNumParts()
259
260// Returns the specified partition as a GPT partition. Used in BSD-to-GPT
261// conversion process
262GPTPart BSDData::AsGPT(int i) {
263 GPTPart guid; // dump data in here, then return it
264 uint64_t sectorOne, sectorEnd; // first & last sectors of partition
srs5694a0eb11a2009-08-29 15:00:08 -0400265 int passItOn = 1; // Set to 0 if partition is empty or invalid
266
267 guid.BlankPartition();
268 sectorOne = (uint64_t) partitions[i].firstLBA;
269 sectorEnd = sectorOne + (uint64_t) partitions[i].lengthLBA;
270 if (sectorEnd > 0) sectorEnd--;
271 // Note on above: BSD partitions sometimes have a length of 0 and a start
srs5694e4ac11e2009-08-31 10:13:04 -0400272 // sector of 0. With unsigned ints, the usual way (start + length - 1) to
273 // find the end will result in a huge number, which will be confusing.
274 // Thus, apply the "-1" part only if it's reasonable to do so.
srs5694a0eb11a2009-08-29 15:00:08 -0400275
276 // Do a few sanity checks on the partition before we pass it on....
277 // First, check that it falls within the bounds of its container
278 // and that it starts before it ends....
279 if ((sectorOne < labelFirstLBA) || (sectorEnd > labelLastLBA) || (sectorOne > sectorEnd))
280 passItOn = 0;
281 // Some disklabels include a pseudo-partition that's the size of the entire
282 // disk or containing partition. Don't return it.
283 if ((sectorOne <= labelFirstLBA) && (sectorEnd >= labelLastLBA) &&
284 (GetType(i) == 0))
285 passItOn = 0;
286 // If the end point is 0, it's not a valid partition.
srs5694e4ac11e2009-08-31 10:13:04 -0400287 if ((sectorEnd == 0) || (sectorEnd == labelFirstLBA))
srs5694a0eb11a2009-08-29 15:00:08 -0400288 passItOn = 0;
289
290 if (passItOn) {
291 guid.SetFirstLBA(sectorOne);
292 guid.SetLastLBA(sectorEnd);
293 // Now set a random unique GUID for the partition....
294 guid.SetUniqueGUID(1);
295 // ... zero out the attributes and name fields....
296 guid.SetAttributes(UINT64_C(0));
297 // Most BSD disklabel type codes seem to be archaic or rare.
298 // They're also ambiguous; a FreeBSD filesystem is impossible
299 // to distinguish from a NetBSD one. Thus, these code assignment
300 // are going to be rough to begin with. For a list of meanings,
301 // see http://fxr.watson.org/fxr/source/sys/dtype.h?v=DFBSD,
302 // or Google it.
303 switch (GetType(i)) {
304 case 1: // BSD swap
305 guid.SetType(0xa502); break;
306 case 7: // BSD FFS
307 guid.SetType(0xa503); break;
308 case 8: case 11: // MS-DOS or HPFS
309 guid.SetType(0x0700); break;
310 case 9: // log-structured fs
311 guid.SetType(0xa903); break;
312 case 13: // bootstrap
313 guid.SetType(0xa501); break;
314 case 14: // vinum
315 guid.SetType(0xa505); break;
316 case 15: // RAID
317 guid.SetType(0xa903); break;
318 case 27: // FreeBSD ZFS
319 guid.SetType(0xa504); break;
320 default:
321 guid.SetType(0x0700); break;
322 } // switch
323 // Set the partition name to the name of the type code....
srs5694fed16d02010-01-27 23:03:40 -0500324 guid.SetName(guid.GetNameType());
srs5694a0eb11a2009-08-29 15:00:08 -0400325 } // if
326 return guid;
327} // BSDData::AsGPT()