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