srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1 | /* bsd.cc -- Functions for loading and manipulating legacy BSD disklabel |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 2 | data. */ |
| 3 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 4 | /* By Rod Smith, initial coding August, 2009 */ |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 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> |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 18 | #include <sys/stat.h> |
| 19 | #include <errno.h> |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 20 | #include "support.h" |
| 21 | #include "bsd.h" |
| 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | |
| 26 | BSDData::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 |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 35 | partitions = NULL; |
| 36 | } // default constructor |
| 37 | |
| 38 | BSDData::~BSDData(void) { |
| 39 | free(partitions); |
| 40 | } // destructor |
| 41 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 42 | // 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. |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 45 | int BSDData::ReadBSDData(char* device, uint64_t startSector, uint64_t endSector) { |
| 46 | int fd, allOK = 1; |
| 47 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 48 | 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); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 56 | } else { |
| 57 | allOK = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 58 | } // if/else |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 59 | |
| 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. |
| 65 | void BSDData::ReadBSDData(int fd, uint64_t startSector, uint64_t endSector) { |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 66 | uint8_t buffer[4096]; // I/O buffer |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 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 | |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 77 | // Read eight sectors into memory; we'll extract data from |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 78 | // this buffer. (Done to work around FreeBSD limitation) |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 79 | lseek64(fd, startSector * GetBlockSize(fd), SEEK_SET); |
| 80 | err = read(fd, buffer, 4096); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 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 |
| 163 | void BSDData::ReverseMetaBytes(void) { |
| 164 | ReverseBytes(&signature, 4); |
| 165 | ReverseBytes(§orSize, 4); |
| 166 | ReverseBytes(&signature2, 4); |
| 167 | ReverseBytes(&numParts, 2); |
| 168 | } // BSDData::ReverseMetaByteOrder() |
| 169 | |
| 170 | // Display basic BSD partition data. Used for debugging. |
| 171 | void 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 |
| 187 | int 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 |
| 206 | uint8_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 |
| 216 | uint64_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 |
| 226 | uint64_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 |
| 236 | int 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 |
| 242 | GPTPart 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 |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 253 | // sector of 0. With unsigned ints, the usual way (start + length - 1) to |
| 254 | // find the end will result in a huge number, which will be confusing. |
| 255 | // Thus, apply the "-1" part only if it's reasonable to do so. |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 256 | |
| 257 | // Do a few sanity checks on the partition before we pass it on.... |
| 258 | // First, check that it falls within the bounds of its container |
| 259 | // and that it starts before it ends.... |
| 260 | if ((sectorOne < labelFirstLBA) || (sectorEnd > labelLastLBA) || (sectorOne > sectorEnd)) |
| 261 | passItOn = 0; |
| 262 | // Some disklabels include a pseudo-partition that's the size of the entire |
| 263 | // disk or containing partition. Don't return it. |
| 264 | if ((sectorOne <= labelFirstLBA) && (sectorEnd >= labelLastLBA) && |
| 265 | (GetType(i) == 0)) |
| 266 | passItOn = 0; |
| 267 | // If the end point is 0, it's not a valid partition. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 268 | if ((sectorEnd == 0) || (sectorEnd == labelFirstLBA)) |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 269 | passItOn = 0; |
| 270 | |
| 271 | if (passItOn) { |
| 272 | guid.SetFirstLBA(sectorOne); |
| 273 | guid.SetLastLBA(sectorEnd); |
| 274 | // Now set a random unique GUID for the partition.... |
| 275 | guid.SetUniqueGUID(1); |
| 276 | // ... zero out the attributes and name fields.... |
| 277 | guid.SetAttributes(UINT64_C(0)); |
| 278 | // Most BSD disklabel type codes seem to be archaic or rare. |
| 279 | // They're also ambiguous; a FreeBSD filesystem is impossible |
| 280 | // to distinguish from a NetBSD one. Thus, these code assignment |
| 281 | // are going to be rough to begin with. For a list of meanings, |
| 282 | // see http://fxr.watson.org/fxr/source/sys/dtype.h?v=DFBSD, |
| 283 | // or Google it. |
| 284 | switch (GetType(i)) { |
| 285 | case 1: // BSD swap |
| 286 | guid.SetType(0xa502); break; |
| 287 | case 7: // BSD FFS |
| 288 | guid.SetType(0xa503); break; |
| 289 | case 8: case 11: // MS-DOS or HPFS |
| 290 | guid.SetType(0x0700); break; |
| 291 | case 9: // log-structured fs |
| 292 | guid.SetType(0xa903); break; |
| 293 | case 13: // bootstrap |
| 294 | guid.SetType(0xa501); break; |
| 295 | case 14: // vinum |
| 296 | guid.SetType(0xa505); break; |
| 297 | case 15: // RAID |
| 298 | guid.SetType(0xa903); break; |
| 299 | case 27: // FreeBSD ZFS |
| 300 | guid.SetType(0xa504); break; |
| 301 | default: |
| 302 | guid.SetType(0x0700); break; |
| 303 | } // switch |
| 304 | // Set the partition name to the name of the type code.... |
| 305 | guid.SetName((unsigned char*) guid.GetNameType(tempStr)); |
| 306 | } // if |
| 307 | return guid; |
| 308 | } // BSDData::AsGPT() |