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