blob: 59557c2e37e574925379aaaf4cbc7a5aaa4b33a7 [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
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070010#ifndef __STDC_CONSTANT_MACROS
srs5694a0eb11a2009-08-29 15:00:08 -040011#define __STDC_CONSTANT_MACROS
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070012#endif
srs5694a0eb11a2009-08-29 15:00:08 -040013
14#include <stdio.h>
srs569408bb0da2010-02-19 17:19:55 -050015//#include <unistd.h>
srs5694a0eb11a2009-08-29 15:00:08 -040016#include <stdlib.h>
17#include <stdint.h>
18#include <fcntl.h>
srs5694a0eb11a2009-08-29 15:00:08 -040019#include <sys/stat.h>
20#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050021#include <iostream>
22#include <string>
srs5694a0eb11a2009-08-29 15:00:08 -040023#include "support.h"
24#include "bsd.h"
25
26using namespace std;
27
28
29BSDData::BSDData(void) {
30 state = unknown;
31 signature = UINT32_C(0);
32 signature2 = UINT32_C(0);
33 sectorSize = 512;
34 numParts = 0;
35 labelFirstLBA = 0;
36 labelLastLBA = 0;
37 labelStart = LABEL_OFFSET1; // assume raw disk format
srs5694a0eb11a2009-08-29 15:00:08 -040038 partitions = NULL;
39} // default constructor
40
41BSDData::~BSDData(void) {
srs56949a46b042011-03-15 00:34:10 -040042 delete[] partitions;
srs5694a0eb11a2009-08-29 15:00:08 -040043} // destructor
44
srs5694e4ac11e2009-08-31 10:13:04 -040045// Read BSD disklabel data from the specified device filename. This function
46// just opens the device file and then calls an overloaded function to do
srs5694fed16d02010-01-27 23:03:40 -050047// the bulk of the work. Returns 1 on success, 0 on failure.
srs56940a697312010-01-28 21:10:52 -050048int BSDData::ReadBSDData(const string & device, uint64_t startSector, uint64_t endSector) {
srs5694fed16d02010-01-27 23:03:40 -050049 int allOK = 1;
50 DiskIO myDisk;
srs5694a0eb11a2009-08-29 15:00:08 -040051
srs56940a697312010-01-28 21:10:52 -050052 if (device != "") {
53 if (myDisk.OpenForRead(device)) {
srs5694fed16d02010-01-27 23:03:40 -050054 allOK = ReadBSDData(&myDisk, startSector, endSector);
srs5694e4ac11e2009-08-31 10:13:04 -040055 } else {
56 allOK = 0;
57 } // if/else
58
srs5694fed16d02010-01-27 23:03:40 -050059 myDisk.Close();
srs5694a0eb11a2009-08-29 15:00:08 -040060 } else {
61 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -040062 } // if/else
srs5694a0eb11a2009-08-29 15:00:08 -040063 return allOK;
64} // BSDData::ReadBSDData() (device filename version)
65
66// Load the BSD disklabel data from an already-opened disk
67// file, starting with the specified sector number.
srs5694fed16d02010-01-27 23:03:40 -050068int BSDData::ReadBSDData(DiskIO *theDisk, uint64_t startSector, uint64_t endSector) {
srs569408bb0da2010-02-19 17:19:55 -050069 int allOK = 1;
70 int i, foundSig = 0, bigEnd = 0;
srs5694a0eb11a2009-08-29 15:00:08 -040071 int relative = 0; // assume absolute partition sector numbering
srs569408bb0da2010-02-19 17:19:55 -050072 uint8_t buffer[4096]; // I/O buffer
srs5694a0eb11a2009-08-29 15:00:08 -040073 uint32_t realSig;
74 uint32_t* temp32;
75 uint16_t* temp16;
76 BSDRecord* tempRecords;
srs5694546a9c72010-01-26 16:00:26 -050077 int offset[NUM_OFFSETS] = { LABEL_OFFSET1, LABEL_OFFSET2 };
srs5694a0eb11a2009-08-29 15:00:08 -040078
79 labelFirstLBA = startSector;
80 labelLastLBA = endSector;
srs5694fed16d02010-01-27 23:03:40 -050081 offset[1] = theDisk->GetBlockSize();
srs5694a0eb11a2009-08-29 15:00:08 -040082
srs5694ba00fed2010-01-12 18:18:36 -050083 // Read 4096 bytes (eight 512-byte sectors or equivalent)
84 // into memory; we'll extract data from this buffer.
85 // (Done to work around FreeBSD limitation on size of reads
86 // from block devices.)
srs5694fed16d02010-01-27 23:03:40 -050087 allOK = theDisk->Seek(startSector);
88 if (allOK) allOK = theDisk->Read(buffer, 4096);
srs5694a0eb11a2009-08-29 15:00:08 -040089
90 // Do some strangeness to support big-endian architectures...
91 bigEnd = (IsLittleEndian() == 0);
92 realSig = BSD_SIGNATURE;
srs5694fed16d02010-01-27 23:03:40 -050093 if (bigEnd && allOK)
srs5694a0eb11a2009-08-29 15:00:08 -040094 ReverseBytes(&realSig, 4);
95
srs5694546a9c72010-01-26 16:00:26 -050096 // Look for the signature at any of two locations.
srs5694ba00fed2010-01-12 18:18:36 -050097 // Note that the signature is repeated at both the original
98 // offset and 132 bytes later, so we need two checks....
srs5694fed16d02010-01-27 23:03:40 -050099 if (allOK) {
100 i = 0;
101 do {
102 temp32 = (uint32_t*) &buffer[offset[i]];
103 signature = *temp32;
104 if (signature == realSig) { // found first, look for second
105 temp32 = (uint32_t*) &buffer[offset[i] + 132];
106 signature2 = *temp32;
107 if (signature2 == realSig) {
108 foundSig = 1;
109 labelStart = offset[i];
110 } // if found signature
111 } // if/else
112 i++;
113 } while ((!foundSig) && (i < NUM_OFFSETS));
114 allOK = foundSig;
115 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400116
117 // Load partition metadata from the buffer....
srs5694fed16d02010-01-27 23:03:40 -0500118 if (allOK) {
119 temp32 = (uint32_t*) &buffer[labelStart + 40];
120 sectorSize = *temp32;
121 temp16 = (uint16_t*) &buffer[labelStart + 138];
122 numParts = *temp16;
123 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400124
125 // Make it big-endian-aware....
srs5694fed16d02010-01-27 23:03:40 -0500126 if ((IsLittleEndian() == 0) && allOK)
srs5694a0eb11a2009-08-29 15:00:08 -0400127 ReverseMetaBytes();
128
129 // Check validity of the data and flag it appropriately....
srs5694fed16d02010-01-27 23:03:40 -0500130 if (foundSig && (numParts <= MAX_BSD_PARTS) && allOK) {
srs5694a0eb11a2009-08-29 15:00:08 -0400131 state = bsd;
132 } else {
133 state = bsd_invalid;
134 } // if/else
135
136 // If the state is good, go ahead and load the main partition data....
137 if (state == bsd) {
srs5694cb76c672010-02-11 22:22:22 -0500138 partitions = new struct BSDRecord[numParts * sizeof(struct BSDRecord)];
srs56946aae2a92011-06-10 01:16:51 -0400139 if (partitions == NULL) {
140 cerr << "Unable to allocate memory in BSDData::ReadBSDData()! Terminating!\n";
141 exit(1);
142 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400143 for (i = 0; i < numParts; i++) {
144 // Once again, we use the buffer, but index it using a BSDRecord
145 // pointer (dangerous, but effective)....
146 tempRecords = (BSDRecord*) &buffer[labelStart + 148];
147 partitions[i].lengthLBA = tempRecords[i].lengthLBA;
148 partitions[i].firstLBA = tempRecords[i].firstLBA;
149 partitions[i].fsType = tempRecords[i].fsType;
150 if (bigEnd) { // reverse data (fsType is a single byte)
151 ReverseBytes(&partitions[i].lengthLBA, 4);
152 ReverseBytes(&partitions[i].firstLBA, 4);
153 } // if big-endian
154 // Check for signs of relative sector numbering: A "0" first sector
155 // number on a partition with a non-zero length -- but ONLY if the
156 // length is less than the disk size, since NetBSD has a habit of
157 // creating a disk-sized partition within a carrier MBR partition
158 // that's too small to house it, and this throws off everything....
159 if ((partitions[i].firstLBA == 0) && (partitions[i].lengthLBA > 0)
160 && (partitions[i].lengthLBA < labelLastLBA))
161 relative = 1;
162 } // for
163 // Some disklabels use sector numbers relative to the enclosing partition's
164 // start, others use absolute sector numbers. If relative numbering was
165 // detected above, apply a correction to all partition start sectors....
166 if (relative) {
167 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -0500168 partitions[i].firstLBA += (uint32_t) startSector;
srs5694a0eb11a2009-08-29 15:00:08 -0400169 } // for
170 } // if
171 } // if signatures OK
172// DisplayBSDData();
srs5694fed16d02010-01-27 23:03:40 -0500173 return allOK;
174} // BSDData::ReadBSDData(DiskIO* theDisk, uint64_t startSector)
srs5694a0eb11a2009-08-29 15:00:08 -0400175
176// Reverse metadata's byte order; called only on big-endian systems
177void BSDData::ReverseMetaBytes(void) {
178 ReverseBytes(&signature, 4);
179 ReverseBytes(&sectorSize, 4);
180 ReverseBytes(&signature2, 4);
181 ReverseBytes(&numParts, 2);
182} // BSDData::ReverseMetaByteOrder()
183
184// Display basic BSD partition data. Used for debugging.
185void BSDData::DisplayBSDData(void) {
186 int i;
187
188 if (state == bsd) {
srs5694fed16d02010-01-27 23:03:40 -0500189 cout << "BSD partitions:\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400190 for (i = 0; i < numParts; i++) {
srs5694fed16d02010-01-27 23:03:40 -0500191 cout.width(4);
192 cout << i + 1 << "\t";
193 cout.width(13);
194 cout << partitions[i].firstLBA << "\t";
195 cout.width(15);
196 cout << partitions[i].lengthLBA << " \t0x";
197 cout.width(2);
198 cout.fill('0');
199 cout.setf(ios::uppercase);
200 cout << hex << (int) partitions[i].fsType << "\n" << dec;
201 cout.fill(' ');
srs5694a0eb11a2009-08-29 15:00:08 -0400202 } // for
203 } // if
204} // BSDData::DisplayBSDData()
205
206// Displays the BSD disklabel state. Called during program launch to inform
207// the user about the partition table(s) status
208int BSDData::ShowState(void) {
209 int retval = 0;
210
211 switch (state) {
212 case bsd_invalid:
srs5694fed16d02010-01-27 23:03:40 -0500213 cout << " BSD: not present\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400214 break;
215 case bsd:
srs5694fed16d02010-01-27 23:03:40 -0500216 cout << " BSD: present\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400217 retval = 1;
218 break;
219 default:
srs5694fed16d02010-01-27 23:03:40 -0500220 cout << "\a BSD: unknown -- bug!\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400221 break;
222 } // switch
223 return retval;
224} // BSDData::ShowState()
225
srs5694a6022b02010-01-19 16:17:20 -0500226// Weirdly, this function has stopped working when defined inline,
227// but it's OK here....
228int BSDData::IsDisklabel(void) {
229 return (state == bsd);
230} // BSDData::IsDiskLabel()
231
srs5694a0eb11a2009-08-29 15:00:08 -0400232// Returns the BSD table's partition type code
233uint8_t BSDData::GetType(int i) {
234 uint8_t retval = 0; // 0 = "unused"
235
236 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
237 retval = partitions[i].fsType;
238
239 return(retval);
240} // BSDData::GetType()
241
242// Returns the number of the first sector of the specified partition
243uint64_t BSDData::GetFirstSector(int i) {
244 uint64_t retval = UINT64_C(0);
245
246 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
247 retval = (uint64_t) partitions[i].firstLBA;
248
249 return retval;
250} // BSDData::GetFirstSector
251
252// Returns the length (in sectors) of the specified partition
253uint64_t BSDData::GetLength(int i) {
254 uint64_t retval = UINT64_C(0);
255
256 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
257 retval = (uint64_t) partitions[i].lengthLBA;
258
259 return retval;
260} // BSDData::GetLength()
261
262// Returns the number of partitions defined in the current table
263int BSDData::GetNumParts(void) {
264 return numParts;
265} // BSDData::GetNumParts()
266
267// Returns the specified partition as a GPT partition. Used in BSD-to-GPT
268// conversion process
269GPTPart BSDData::AsGPT(int i) {
270 GPTPart guid; // dump data in here, then return it
271 uint64_t sectorOne, sectorEnd; // first & last sectors of partition
srs5694a0eb11a2009-08-29 15:00:08 -0400272 int passItOn = 1; // Set to 0 if partition is empty or invalid
273
274 guid.BlankPartition();
275 sectorOne = (uint64_t) partitions[i].firstLBA;
276 sectorEnd = sectorOne + (uint64_t) partitions[i].lengthLBA;
277 if (sectorEnd > 0) sectorEnd--;
278 // Note on above: BSD partitions sometimes have a length of 0 and a start
srs5694e4ac11e2009-08-31 10:13:04 -0400279 // sector of 0. With unsigned ints, the usual way (start + length - 1) to
280 // find the end will result in a huge number, which will be confusing.
281 // Thus, apply the "-1" part only if it's reasonable to do so.
srs5694a0eb11a2009-08-29 15:00:08 -0400282
283 // Do a few sanity checks on the partition before we pass it on....
284 // First, check that it falls within the bounds of its container
285 // and that it starts before it ends....
286 if ((sectorOne < labelFirstLBA) || (sectorEnd > labelLastLBA) || (sectorOne > sectorEnd))
287 passItOn = 0;
288 // Some disklabels include a pseudo-partition that's the size of the entire
289 // disk or containing partition. Don't return it.
290 if ((sectorOne <= labelFirstLBA) && (sectorEnd >= labelLastLBA) &&
291 (GetType(i) == 0))
292 passItOn = 0;
293 // If the end point is 0, it's not a valid partition.
srs5694e4ac11e2009-08-31 10:13:04 -0400294 if ((sectorEnd == 0) || (sectorEnd == labelFirstLBA))
srs5694a0eb11a2009-08-29 15:00:08 -0400295 passItOn = 0;
296
297 if (passItOn) {
298 guid.SetFirstLBA(sectorOne);
299 guid.SetLastLBA(sectorEnd);
300 // Now set a random unique GUID for the partition....
srs56946699b012010-02-04 00:55:30 -0500301 guid.RandomizeUniqueGUID();
srs5694a0eb11a2009-08-29 15:00:08 -0400302 // ... zero out the attributes and name fields....
303 guid.SetAttributes(UINT64_C(0));
304 // Most BSD disklabel type codes seem to be archaic or rare.
305 // They're also ambiguous; a FreeBSD filesystem is impossible
306 // to distinguish from a NetBSD one. Thus, these code assignment
307 // are going to be rough to begin with. For a list of meanings,
308 // see http://fxr.watson.org/fxr/source/sys/dtype.h?v=DFBSD,
309 // or Google it.
310 switch (GetType(i)) {
311 case 1: // BSD swap
312 guid.SetType(0xa502); break;
313 case 7: // BSD FFS
314 guid.SetType(0xa503); break;
315 case 8: case 11: // MS-DOS or HPFS
316 guid.SetType(0x0700); break;
317 case 9: // log-structured fs
318 guid.SetType(0xa903); break;
319 case 13: // bootstrap
320 guid.SetType(0xa501); break;
321 case 14: // vinum
322 guid.SetType(0xa505); break;
323 case 15: // RAID
324 guid.SetType(0xa903); break;
325 case 27: // FreeBSD ZFS
326 guid.SetType(0xa504); break;
327 default:
srs569400b6d7a2011-06-26 22:40:06 -0400328 guid.SetType(0xa503); break;
srs5694a0eb11a2009-08-29 15:00:08 -0400329 } // switch
330 // Set the partition name to the name of the type code....
srs56946699b012010-02-04 00:55:30 -0500331 guid.SetName(guid.GetTypeName());
srs5694a0eb11a2009-08-29 15:00:08 -0400332 } // if
333 return guid;
334} // BSDData::AsGPT()