summaryrefslogtreecommitdiff
path: root/mbr.cc
diff options
context:
space:
mode:
authorsrs5694 <srs5694@users.sourceforge.net>2009-08-26 00:48:01 -0400
committersrs5694 <srs5694@users.sourceforge.net>2009-08-26 00:48:01 -0400
commit2a9f5da3c3c4ccccd291462bda9d2aefcd485ff8 (patch)
treee328d746064b4d8c19d82644322a60847b0d3c53 /mbr.cc
parente19ba095c0c78fbd76a823ed300fada07ad258a9 (diff)
downloadsgdisk-2a9f5da3c3c4ccccd291462bda9d2aefcd485ff8.tar.gz
Added support for big-endian architectures.
New support seems OK so far for me, but I want to test it a bit more before making an official 0.3.5 release....
Diffstat (limited to 'mbr.cc')
-rw-r--r--mbr.cc59
1 files changed, 55 insertions, 4 deletions
diff --git a/mbr.cc b/mbr.cc
index e09d6b6..d907b56 100644
--- a/mbr.cc
+++ b/mbr.cc
@@ -141,6 +141,18 @@ void MBRData::ReadMBRData(int fd) {
read(fd, &nulls, 2);
read(fd, partitions, 64);
read(fd, &MBRSignature, 2);
+
+ // Reverse the byte order, if necessary
+ if (IsLittleEndian() == 0) {
+ ReverseBytes((char*) &diskSignature, 4);
+ ReverseBytes((char*) &nulls, 2);
+ ReverseBytes((char*) &MBRSignature, 2);
+ for (i = 0; i < 4; i++) {
+ ReverseBytes((char*) &partitions[i].firstLBA, 4);
+ ReverseBytes((char*) &partitions[i].lengthLBA, 4);
+ } // for
+ } // if
+
if (MBRSignature != MBR_SIGNATURE) {
allOK = 0;
state = invalid;
@@ -179,9 +191,19 @@ void MBRData::ReadMBRData(int fd) {
for (i = 0; i < 4; i++) {
if (partitions[i].partitionType == UINT8_C(0xEE)) {
state = gpt;
- } /* if */
- } /* for */
- } /* if */
+ } // if
+ } // for
+ } // if
+
+ // If there's an EFI GPT partition, look for other partition types,
+ // to flag as hybrid
+ if (state == gpt) {
+ for (i = 0 ; i < 4; i++) {
+ if ((partitions[i].partitionType != UINT8_C(0xEE)) &&
+ (partitions[i].partitionType != UINT8_C(0x00)))
+ state = hybrid;
+ } // for
+ } // if hybrid
/* // Tell the user what the MBR state is...
switch (state) {
@@ -216,11 +238,35 @@ int MBRData::WriteMBRData(void) {
// Save the MBR data to a file. Note that this function writes ONLY the
// MBR data, not the logical partitions (if any are defined).
void MBRData::WriteMBRData(int fd) {
+ int i;
+
+ // Reverse the byte order, if necessary
+ if (IsLittleEndian() == 0) {
+ ReverseBytes((char*) &diskSignature, 4);
+ ReverseBytes((char*) &nulls, 2);
+ ReverseBytes((char*) &MBRSignature, 2);
+ for (i = 0; i < 4; i++) {
+ ReverseBytes((char*) &partitions[i].firstLBA, 4);
+ ReverseBytes((char*) &partitions[i].lengthLBA, 4);
+ } // for
+ } // if
+
write(fd, code, 440);
write(fd, &diskSignature, 4);
write(fd, &nulls, 2);
write(fd, partitions, 64);
write(fd, &MBRSignature, 2);
+
+ // Reverse the byte order, if necessary
+ if (IsLittleEndian() == 0) {
+ ReverseBytes((char*) &diskSignature, 4);
+ ReverseBytes((char*) &nulls, 2);
+ ReverseBytes((char*) &MBRSignature, 2);
+ for (i = 0; i < 4; i++) {
+ ReverseBytes((char*) &partitions[i].firstLBA, 4);
+ ReverseBytes((char*) &partitions[i].lengthLBA, 4);
+ } // for
+ }// if
} // MBRData::WriteMBRData(int fd)
// This is a recursive function to read all the logical partitions, following the
@@ -240,7 +286,12 @@ int MBRData::ReadLogicalPart(int fd, uint32_t extendedStart,
fprintf(stderr, "Error seeking to or reading logical partition data from %lu!\nAborting!\n",
(unsigned long) offset);
allOK = 0;
- }
+ } else if (IsLittleEndian() != 1) { // Reverse byte ordering of some data....
+ ReverseBytes((char*) &ebr.MBRSignature, 2);
+ ReverseBytes((char*) &ebr.partitions[0].firstLBA, 4);
+ ReverseBytes((char*) &ebr.partitions[0].lengthLBA, 4);
+ } // if/else/if
+
if (ebr.MBRSignature != MBR_SIGNATURE) {
allOK = 0;
printf("MBR signature in logical partition invalid; read 0x%04X, but should be 0x%04X\n",