Blame SOURCES/gdisk-1.0.3-byteswap.patch

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