e928d6
From a7eaefd9bc4a91a4ca26146f784d40725cfe15fa Mon Sep 17 00:00:00 2001
e928d6
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
e928d6
Date: Wed, 29 Sep 2021 15:33:33 +0200
e928d6
Subject: [PATCH] Fix incorrect byte order of partition names on big-endian
e928d6
 systems
e928d6
e928d6
---
e928d6
 gdisk.8    |  8 ++++++++
e928d6
 gptcl.cc   | 11 +++++++++++
e928d6
 gptpart.cc | 14 +++++++-------
e928d6
 gptpart.h  |  1 +
e928d6
 gpttext.cc | 20 ++++++++++++++++++++
e928d6
 gpttext.h  |  1 +
e928d6
 sgdisk.8   |  8 ++++++++
e928d6
 7 files changed, 56 insertions(+), 7 deletions(-)
e928d6
e928d6
diff --git a/gdisk.8 b/gdisk.8
e928d6
index c2cf83d..071756c 100644
e928d6
--- a/gdisk.8
e928d6
+++ b/gdisk.8
e928d6
@@ -416,6 +416,14 @@ set features for each partition. \fBgdisk\fR supports four attributes:
e928d6
 aren't translated into anything useful. In practice, most OSes seem to
e928d6
 ignore these attributes.
e928d6
 
e928d6
+.TP 
e928d6
+.B b
e928d6
+Swap the byte order for the name of the specified partition. Some
e928d6
+partitioning tools, including GPT fdisk 1.0.7 and earlier, can write the
e928d6
+partition name in the wrong byte order on big-endian computers, such as the
e928d6
+IBM s390 mainframes and PowerPC-based Macs. This feature corrects this
e928d6
+problem.
e928d6
+
e928d6
 .TP 
e928d6
 .B c
e928d6
 Change partition GUID. You can enter a custom unique GUID for a partition
e928d6
diff --git a/gptcl.cc b/gptcl.cc
e928d6
index 6c36738..58afc8a 100644
e928d6
--- a/gptcl.cc
e928d6
+++ b/gptcl.cc
e928d6
@@ -64,6 +64,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) {
e928d6
    GPTData secondDevice;
e928d6
    int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
e928d6
    int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0;
e928d6
+   int byteSwapPartNum = 0;
e928d6
    uint64_t low, high, startSector, endSector, sSize, mainTableLBA;
e928d6
    uint64_t temp; // temporary variable; free to use in any case
e928d6
    char *device;
e928d6
@@ -76,6 +77,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) {
e928d6
           "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
e928d6
       {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},
e928d6
       {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},
e928d6
+      {"byte-swap-name", 'B',  POPT_ARG_INT, &byteSwapPartNum, 'B', "byte-swap partition's name", "partnum"},
e928d6
       {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},
e928d6
       {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
e928d6
       {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
e928d6
@@ -191,6 +193,15 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) {
e928d6
                case 'a':
e928d6
                   SetAlignment(alignment);
e928d6
                   break;
e928d6
+               case 'B':
e928d6
+                  if (IsUsedPartNum(byteSwapPartNum - 1)) {
e928d6
+                     partitions[byteSwapPartNum - 1].ReverseNameBytes();
e928d6
+                     cout << "Changed partition " << byteSwapPartNum << "'s name to "
e928d6
+                          << partitions[byteSwapPartNum - 1].GetDescription() << "\n";
e928d6
+                     JustLooking(0);
e928d6
+                     saveData = 1;
e928d6
+                  }
e928d6
+                  break;
e928d6
                case 'b':
e928d6
                   SaveGPTBackup(backupFile);
e928d6
                   free(backupFile);
e928d6
diff --git a/gptpart.cc b/gptpart.cc
e928d6
index 17d6f15..82aeab0 100644
e928d6
--- a/gptpart.cc
e928d6
+++ b/gptpart.cc
e928d6
@@ -83,7 +83,6 @@ string GPTPart::GetDescription(void) {
e928d6
    size_t pos = 0 ;
e928d6
    while ( ( pos < NAME_SIZE ) && ( name[ pos ] != 0 ) ) {
e928d6
       uint16_t cp = name[ pos ++ ] ;
e928d6
-      if ( ! IsLittleEndian() ) ReverseBytes( & cp , 2 ) ;
e928d6
       // first to utf32
e928d6
       uint32_t uni ;
e928d6
       if ( cp < 0xd800 || cp > 0xdfff ) {
e928d6
@@ -234,7 +233,6 @@ void GPTPart::SetName(const string & theName) {
e928d6
       // then to utf16le
e928d6
       if ( uni < 0x10000 ) {
e928d6
          name[ pos ] = (uint16_t) uni ;
e928d6
-         if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
e928d6
          pos ++ ;
e928d6
       } // if
e928d6
       else {
e928d6
@@ -244,10 +242,8 @@ void GPTPart::SetName(const string & theName) {
e928d6
          } // if
e928d6
          uni -= 0x10000 ;
e928d6
          name[ pos ] = (uint16_t)( uni >> 10 ) | 0xd800 ;
e928d6
-         if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
e928d6
          pos ++ ;
e928d6
          name[ pos ] = (uint16_t)( uni & 0x3ff ) | 0xdc00 ;
e928d6
-         if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
e928d6
          pos ++ ;
e928d6
       }
e928d6
    } // for
e928d6
@@ -407,14 +403,18 @@ int GPTPart::DoTheyOverlap(const GPTPart & other) {
e928d6
 // Reverse the bytes of integral data types and of the UTF-16LE name;
e928d6
 // used on big-endian systems.
e928d6
 void GPTPart::ReversePartBytes(void) {
e928d6
-   int i;
e928d6
-
e928d6
    ReverseBytes(&firstLBA, 8);
e928d6
    ReverseBytes(&lastLBA, 8);
e928d6
    ReverseBytes(&attributes, 8);
e928d6
+   ReverseNameBytes();
e928d6
+} // GPTPart::ReversePartBytes()
e928d6
+
e928d6
+void GPTPart::ReverseNameBytes(void) {
e928d6
+   int i;
e928d6
+
e928d6
    for (i = 0; i < NAME_SIZE; i ++ )
e928d6
       ReverseBytes(name + i, 2);
e928d6
-} // GPTPart::ReverseBytes()
e928d6
+} // GPTPart::ReverseNameBytes()
e928d6
 
e928d6
 /****************************************
e928d6
  * Functions requiring user interaction *
e928d6
diff --git a/gptpart.h b/gptpart.h
e928d6
index 657b3f9..ac8a725 100644
e928d6
--- a/gptpart.h
e928d6
+++ b/gptpart.h
e928d6
@@ -93,6 +93,7 @@ class GPTPart {
e928d6
       void BlankPartition(void); // empty partition of data
e928d6
       int DoTheyOverlap(const GPTPart & other); // returns 1 if there's overlap
e928d6
       void ReversePartBytes(void); // reverse byte order of all integer fields
e928d6
+      void ReverseNameBytes(void); // reverse byte order of partition's name field
e928d6
 
e928d6
       // Functions requiring user interaction
e928d6
       void ChangeType(void); // Change the type code
e928d6
diff --git a/gpttext.cc b/gpttext.cc
e928d6
index 732d861..6de7121 100644
e928d6
--- a/gpttext.cc
e928d6
+++ b/gpttext.cc
e928d6
@@ -341,6 +341,22 @@ int GPTDataTextUI::SetName(uint32_t partNum) {
e928d6
    return retval;
e928d6
 } // GPTDataTextUI::SetName()
e928d6
 
e928d6
+// Enable the user to byte-swap the name of the partition. Used to correct
e928d6
+// partition names damaged by incorrect byte order, as could be created by
e928d6
+// GPT fdisk 1.0.7 and earlier on big-endian systems, and perhaps other tools.
e928d6
+void GPTDataTextUI::ReverseName(uint32_t partNum) {
e928d6
+   int swapBytes;
e928d6
+
e928d6
+   cout << "Current name is: " << partitions[partNum].GetDescription() << "\n";
e928d6
+   partitions[partNum].ReverseNameBytes();
e928d6
+   cout << "Byte-swapped name is: " << partitions[partNum].GetDescription() << "\n";
e928d6
+   cout << "Do you want to byte-swap the name? ";
e928d6
+   swapBytes = (GetYN() == 'Y');
e928d6
+   // Already swapped for display, so undo if necessary....
e928d6
+   if (!swapBytes)
e928d6
+      partitions[partNum].ReverseNameBytes();
e928d6
+} // GPTDataTextUI::ReverseName()
e928d6
+
e928d6
 // Ask user for two partition numbers and swap them in the table. Note that
e928d6
 // this just reorders table entries; it doesn't adjust partition layout on
e928d6
 // the disk.
e928d6
@@ -799,6 +815,9 @@ void GPTDataTextUI::ExpertsMenu(string filename) {
e928d6
             else
e928d6
                cout << "No partitions\n";
e928d6
             break;
e928d6
+         case 'b': case 'B':
e928d6
+            ReverseName(GetPartNum());
e928d6
+            break;
e928d6
          case 'c': case 'C':
e928d6
             ChangeUniqueGuid();
e928d6
             break;
e928d6
@@ -896,6 +915,7 @@ void GPTDataTextUI::ExpertsMenu(string filename) {
e928d6
 
e928d6
 void GPTDataTextUI::ShowExpertCommands(void) {
e928d6
    cout << "a\tset attributes\n";
e928d6
+   cout << "b\tbyte-swap a partition's name\n";
e928d6
    cout << "c\tchange partition GUID\n";
e928d6
    cout << "d\tdisplay the sector alignment value\n";
e928d6
    cout << "e\trelocate backup data structures to the end of the disk\n";
e928d6
diff --git a/gpttext.h b/gpttext.h
e928d6
index 98e59af..db27246 100644
e928d6
--- a/gpttext.h
e928d6
+++ b/gpttext.h
e928d6
@@ -49,6 +49,7 @@ class GPTDataTextUI : public GPTData {
e928d6
       void ChangeUniqueGuid(void);
e928d6
       void SetAttributes(uint32_t partNum);
e928d6
       int SetName(uint32_t partNum);
e928d6
+      void ReverseName(uint32_t partNum);
e928d6
       int SwapPartitions(void);
e928d6
       int DestroyGPTwPrompt(void); // Returns 1 if user proceeds
e928d6
       void ShowDetails(void);
e928d6
diff --git a/sgdisk.8 b/sgdisk.8
e928d6
index 2cb18b9..3bc51f2 100644
e928d6
--- a/sgdisk.8
e928d6
+++ b/sgdisk.8
e928d6
@@ -182,6 +182,14 @@ backup will reflect your changes. If the GPT data structures are damaged,
e928d6
 the backup may not accurately reflect the damaged state; instead, they
e928d6
 will reflect GPT fdisk's first\-pass interpretation of the GPT.
e928d6
 
e928d6
+.TP 
e928d6
+.B \-B, \-\-byte\-swap\-name=partnum
e928d6
+Swap the byte order for the name of the specified partition. Some
e928d6
+partitioning tools, including GPT fdisk 1.0.7 and earlier, can write the
e928d6
+partition name in the wrong byte order on big-endian computers, such as the
e928d6
+IBM s390 mainframes and PowerPC-based Macs. This feature corrects this
e928d6
+problem.
e928d6
+
e928d6
 .TP 
e928d6
 .B \-c, \-\-change\-name=partnum:name
e928d6
 Change the GPT name of a partition. This name is encoded as a UTF\-16
e928d6
-- 
e928d6
2.35.1
e928d6