blob: e1b3907f5319e49fb91c155985d8a824b21a5263 [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) {
srs56941e093722010-01-05 00:14:19 -050066 uint8_t buffer[4096]; // I/O buffer
srs5694a0eb11a2009-08-29 15:00:08 -040067 int i, err, foundSig = 0, bigEnd = 0;
68 int relative = 0; // assume absolute partition sector numbering
69 uint32_t realSig;
70 uint32_t* temp32;
71 uint16_t* temp16;
72 BSDRecord* tempRecords;
srs5694ba00fed2010-01-12 18:18:36 -050073 int offset[3] = { LABEL_OFFSET1, LABEL_OFFSET2, LABEL_OFFSET3 };
srs5694a0eb11a2009-08-29 15:00:08 -040074
75 labelFirstLBA = startSector;
76 labelLastLBA = endSector;
77
srs5694ba00fed2010-01-12 18:18:36 -050078 // Read 4096 bytes (eight 512-byte sectors or equivalent)
79 // into memory; we'll extract data from this buffer.
80 // (Done to work around FreeBSD limitation on size of reads
81 // from block devices.)
srs56941e093722010-01-05 00:14:19 -050082 lseek64(fd, startSector * GetBlockSize(fd), SEEK_SET);
83 err = read(fd, buffer, 4096);
srs5694a0eb11a2009-08-29 15:00:08 -040084
85 // Do some strangeness to support big-endian architectures...
86 bigEnd = (IsLittleEndian() == 0);
87 realSig = BSD_SIGNATURE;
88 if (bigEnd)
89 ReverseBytes(&realSig, 4);
90
srs5694ba00fed2010-01-12 18:18:36 -050091 // Look for the signature at any of three locations.
92 // Note that the signature is repeated at both the original
93 // offset and 132 bytes later, so we need two checks....
94 i = 0;
95 do {
96 temp32 = (uint32_t*) &buffer[offset[i]];
srs5694a0eb11a2009-08-29 15:00:08 -040097 signature = *temp32;
srs5694ba00fed2010-01-12 18:18:36 -050098 if (signature == realSig) { // found first, look for second
99 temp32 = (uint32_t*) &buffer[offset[i] + 132];
srs5694a0eb11a2009-08-29 15:00:08 -0400100 signature2 = *temp32;
srs5694ba00fed2010-01-12 18:18:36 -0500101 if (signature2 == realSig) {
srs5694a0eb11a2009-08-29 15:00:08 -0400102 foundSig = 1;
srs5694ba00fed2010-01-12 18:18:36 -0500103 labelStart = offset[i];
104 } // if found signature
srs5694a0eb11a2009-08-29 15:00:08 -0400105 } // if/else
srs5694ba00fed2010-01-12 18:18:36 -0500106 i++;
107 } while ((!foundSig) && (i < 3));
srs5694a0eb11a2009-08-29 15:00:08 -0400108
109 // Load partition metadata from the buffer....
110 temp32 = (uint32_t*) &buffer[labelStart + 40];
111 sectorSize = *temp32;
112 temp16 = (uint16_t*) &buffer[labelStart + 138];
113 numParts = *temp16;
114
115 // Make it big-endian-aware....
116 if (IsLittleEndian() == 0)
117 ReverseMetaBytes();
118
119 // Check validity of the data and flag it appropriately....
120 if (foundSig && (numParts <= MAX_BSD_PARTS)) {
121 state = bsd;
122 } else {
123 state = bsd_invalid;
124 } // if/else
125
126 // If the state is good, go ahead and load the main partition data....
127 if (state == bsd) {
128 partitions = (struct BSDRecord*) malloc(numParts * sizeof (struct BSDRecord));
129 for (i = 0; i < numParts; i++) {
130 // Once again, we use the buffer, but index it using a BSDRecord
131 // pointer (dangerous, but effective)....
132 tempRecords = (BSDRecord*) &buffer[labelStart + 148];
133 partitions[i].lengthLBA = tempRecords[i].lengthLBA;
134 partitions[i].firstLBA = tempRecords[i].firstLBA;
135 partitions[i].fsType = tempRecords[i].fsType;
136 if (bigEnd) { // reverse data (fsType is a single byte)
137 ReverseBytes(&partitions[i].lengthLBA, 4);
138 ReverseBytes(&partitions[i].firstLBA, 4);
139 } // if big-endian
140 // Check for signs of relative sector numbering: A "0" first sector
141 // number on a partition with a non-zero length -- but ONLY if the
142 // length is less than the disk size, since NetBSD has a habit of
143 // creating a disk-sized partition within a carrier MBR partition
144 // that's too small to house it, and this throws off everything....
145 if ((partitions[i].firstLBA == 0) && (partitions[i].lengthLBA > 0)
146 && (partitions[i].lengthLBA < labelLastLBA))
147 relative = 1;
148 } // for
149 // Some disklabels use sector numbers relative to the enclosing partition's
150 // start, others use absolute sector numbers. If relative numbering was
151 // detected above, apply a correction to all partition start sectors....
152 if (relative) {
153 for (i = 0; i < numParts; i++) {
154 partitions[i].firstLBA += startSector;
155 } // for
156 } // if
157 } // if signatures OK
158// DisplayBSDData();
159} // BSDData::ReadBSDData(int fd, uint64_t startSector)
160
161// Reverse metadata's byte order; called only on big-endian systems
162void BSDData::ReverseMetaBytes(void) {
163 ReverseBytes(&signature, 4);
164 ReverseBytes(&sectorSize, 4);
165 ReverseBytes(&signature2, 4);
166 ReverseBytes(&numParts, 2);
167} // BSDData::ReverseMetaByteOrder()
168
169// Display basic BSD partition data. Used for debugging.
170void BSDData::DisplayBSDData(void) {
171 int i;
172
173 if (state == bsd) {
174 printf("BSD partitions:\n");
175 printf("Number\t Start (sector)\t Length (sectors)\tType\n");
176 for (i = 0; i < numParts; i++) {
177 printf("%4d\t%13lu\t%15lu \t0x%02X\n", i + 1,
178 (unsigned long) partitions[i].firstLBA,
179 (unsigned long) partitions[i].lengthLBA, partitions[i].fsType);
180 } // for
181 } // if
182} // BSDData::DisplayBSDData()
183
184// Displays the BSD disklabel state. Called during program launch to inform
185// the user about the partition table(s) status
186int BSDData::ShowState(void) {
187 int retval = 0;
188
189 switch (state) {
190 case bsd_invalid:
191 printf(" BSD: not present\n");
192 break;
193 case bsd:
194 printf(" BSD: present\n");
195 retval = 1;
196 break;
197 default:
198 printf("\a BSD: unknown -- bug!\n");
199 break;
200 } // switch
201 return retval;
202} // BSDData::ShowState()
203
srs5694a6022b02010-01-19 16:17:20 -0500204// Weirdly, this function has stopped working when defined inline,
205// but it's OK here....
206int BSDData::IsDisklabel(void) {
207 return (state == bsd);
208} // BSDData::IsDiskLabel()
209
srs5694a0eb11a2009-08-29 15:00:08 -0400210// Returns the BSD table's partition type code
211uint8_t BSDData::GetType(int i) {
212 uint8_t retval = 0; // 0 = "unused"
213
214 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
215 retval = partitions[i].fsType;
216
217 return(retval);
218} // BSDData::GetType()
219
220// Returns the number of the first sector of the specified partition
221uint64_t BSDData::GetFirstSector(int i) {
222 uint64_t retval = UINT64_C(0);
223
224 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
225 retval = (uint64_t) partitions[i].firstLBA;
226
227 return retval;
228} // BSDData::GetFirstSector
229
230// Returns the length (in sectors) of the specified partition
231uint64_t BSDData::GetLength(int i) {
232 uint64_t retval = UINT64_C(0);
233
234 if ((i < numParts) && (i >= 0) && (state == bsd) && (partitions != 0))
235 retval = (uint64_t) partitions[i].lengthLBA;
236
237 return retval;
238} // BSDData::GetLength()
239
240// Returns the number of partitions defined in the current table
241int BSDData::GetNumParts(void) {
242 return numParts;
243} // BSDData::GetNumParts()
244
245// Returns the specified partition as a GPT partition. Used in BSD-to-GPT
246// conversion process
247GPTPart BSDData::AsGPT(int i) {
248 GPTPart guid; // dump data in here, then return it
249 uint64_t sectorOne, sectorEnd; // first & last sectors of partition
250 char tempStr[NAME_SIZE]; // temporary string for holding GPT name
251 int passItOn = 1; // Set to 0 if partition is empty or invalid
252
253 guid.BlankPartition();
254 sectorOne = (uint64_t) partitions[i].firstLBA;
255 sectorEnd = sectorOne + (uint64_t) partitions[i].lengthLBA;
256 if (sectorEnd > 0) sectorEnd--;
257 // Note on above: BSD partitions sometimes have a length of 0 and a start
srs5694e4ac11e2009-08-31 10:13:04 -0400258 // sector of 0. With unsigned ints, the usual way (start + length - 1) to
259 // find the end will result in a huge number, which will be confusing.
260 // Thus, apply the "-1" part only if it's reasonable to do so.
srs5694a0eb11a2009-08-29 15:00:08 -0400261
262 // Do a few sanity checks on the partition before we pass it on....
263 // First, check that it falls within the bounds of its container
264 // and that it starts before it ends....
265 if ((sectorOne < labelFirstLBA) || (sectorEnd > labelLastLBA) || (sectorOne > sectorEnd))
266 passItOn = 0;
267 // Some disklabels include a pseudo-partition that's the size of the entire
268 // disk or containing partition. Don't return it.
269 if ((sectorOne <= labelFirstLBA) && (sectorEnd >= labelLastLBA) &&
270 (GetType(i) == 0))
271 passItOn = 0;
272 // If the end point is 0, it's not a valid partition.
srs5694e4ac11e2009-08-31 10:13:04 -0400273 if ((sectorEnd == 0) || (sectorEnd == labelFirstLBA))
srs5694a0eb11a2009-08-29 15:00:08 -0400274 passItOn = 0;
275
276 if (passItOn) {
277 guid.SetFirstLBA(sectorOne);
278 guid.SetLastLBA(sectorEnd);
279 // Now set a random unique GUID for the partition....
280 guid.SetUniqueGUID(1);
281 // ... zero out the attributes and name fields....
282 guid.SetAttributes(UINT64_C(0));
283 // Most BSD disklabel type codes seem to be archaic or rare.
284 // They're also ambiguous; a FreeBSD filesystem is impossible
285 // to distinguish from a NetBSD one. Thus, these code assignment
286 // are going to be rough to begin with. For a list of meanings,
287 // see http://fxr.watson.org/fxr/source/sys/dtype.h?v=DFBSD,
288 // or Google it.
289 switch (GetType(i)) {
290 case 1: // BSD swap
291 guid.SetType(0xa502); break;
292 case 7: // BSD FFS
293 guid.SetType(0xa503); break;
294 case 8: case 11: // MS-DOS or HPFS
295 guid.SetType(0x0700); break;
296 case 9: // log-structured fs
297 guid.SetType(0xa903); break;
298 case 13: // bootstrap
299 guid.SetType(0xa501); break;
300 case 14: // vinum
301 guid.SetType(0xa505); break;
302 case 15: // RAID
303 guid.SetType(0xa903); break;
304 case 27: // FreeBSD ZFS
305 guid.SetType(0xa504); break;
306 default:
307 guid.SetType(0x0700); break;
308 } // switch
309 // Set the partition name to the name of the type code....
310 guid.SetName((unsigned char*) guid.GetNameType(tempStr));
311 } // if
312 return guid;
313} // BSDData::AsGPT()