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